change事件流停止工作

bkkx9g8r  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(445)

我对mongodb(版本4.2)有一个问题,特别是在变更流功能方面。
我有一个由1个主、1个辅助和1个仲裁器组成的replicaset集群,在使用api mongodb driver sync 4.2.0-beta1的java代码中,我对感兴趣的集合有一个.watch()进程。如下所示:

MongoClient mongoClient = MongoClients.create("mongodb://localhost:27017,localhost:27018,localhost:27019/?replicaSet=replica");
    MongoDatabase database = mongoClient.getDatabase("test");       
    MongoCollection<Document> collectionStream = database.getCollection("myCollection");
    List<Bson> pipeline = Arrays.asList(Aggregates.match(Filters.and(Filters.in("operationType", Arrays.asList("insert", "update", "replace", "invalidate")))));
    MongoCursor<ChangeStreamDocument<Document>> cursor = collectionStream.watch(pipeline).fullDocument(FullDocument.UPDATE_LOOKUP).iterator();
    ChangeStreamDocument<Document> streamedEvent = cursor.next();
    System.out.println("Streamed event: " + streamedEvent);

基本上,这条小溪很好用。发生插入/更新操作时,将识别事件并正确传输文档。但是,当两个节点中的一个(主节点或辅助节点,一个承载数据的节点)发生故障时,观察者停止流式传输任何内容。数据库上的更新/插入操作继续正常,但流被阻止。一旦我重新启动两个节点中的一个,流立即正确地恢复,并向我显示以前没有流的事件。另一方面,如果仲裁器节点宕机,则流继续正常工作。
在我的rs.conf()文件下面,如您所见,writeconcern参数是1。

{
"_id" : "replica",
"version" : 31,
"protocolVersion" : NumberLong(1),
"writeConcernMajorityJournalDefault" : true,
"members" : [
    {
        "_id" : 0,
        "host" : "host1:27017",
        "arbiterOnly" : false,
        "buildIndexes" : true,
        "hidden" : false,
        "priority" : 1000,
        "tags" : {

        },
        "slaveDelay" : NumberLong(0),
        "votes" : 1
    },
    {
        "_id" : 1,
        "host" : "host2:27017",
        "arbiterOnly" : false,
        "buildIndexes" : true,
        "hidden" : false,
        "priority" : 1,
        "tags" : {

        },
        "slaveDelay" : NumberLong(0),
        "votes" : 1
    },
    {
        "_id" : 4,
        "host" : "host2:27018",
        "arbiterOnly" : true,
        "buildIndexes" : true,
        "hidden" : false,
        "priority" : 0,
        "tags" : {

        },
        "slaveDelay" : NumberLong(0),
        "votes" : 1
    }
],
"settings" : {
    "chainingAllowed" : true,
    "heartbeatIntervalMillis" : 500,
    "heartbeatTimeoutSecs" : 3,
    "electionTimeoutMillis" : 3000,
    "catchUpTimeoutMillis" : -1,
    "catchUpTakeoverDelayMillis" : 30000,
    "getLastErrorModes" : {

    },
    "getLastErrorDefaults" : {
        "w" : 1,
        "j" : false,
        "wtimeout" : 0
    },
    "replicaSetId" : ObjectId("5f16a4e1e1c622bbea578576")
}}

有人能帮我解决这个问题吗?
更新:为了解决此行为,在每个节点的每个.conf文件中,我将replication.enablemajorityreadconness设置为false,以便禁用readconcernmarity。无论如何,按照此设置,通过停止一个主节点或辅助节点,我总是在控制台中得到以下异常:

Exception in thread "main" com.mongodb.MongoExecutionTimeoutException: Error waiting for snapshot not less than { ts: Timestamp(1605805914, 1), t: -1 }, current relevant optime is { ts: Timestamp(1605805864, 1), t: 71 }. :: caused by :: operation exceeded time limit
    at com.mongodb.internal.connection.ProtocolHelper.createSpecialException(ProtocolHelper.java:239)
    at com.mongodb.internal.connection.ProtocolHelper.getCommandFailureException(ProtocolHelper.java:171)
    at com.mongodb.internal.connection.InternalStreamConnection.receiveCommandMessageResponse(InternalStreamConnection.java:359)
    at com.mongodb.internal.connection.InternalStreamConnection.sendAndReceive(InternalStreamConnection.java:280)
    at com.mongodb.internal.connection.UsageTrackingInternalConnection.sendAndReceive(UsageTrackingInternalConnection.java:100)
    at com.mongodb.internal.connection.DefaultConnectionPool$PooledConnection.sendAndReceive(DefaultConnectionPool.java:490)
    at com.mongodb.internal.connection.CommandProtocolImpl.execute(CommandProtocolImpl.java:71)
    at com.mongodb.internal.connection.DefaultServer$DefaultServerProtocolExecutor.execute(DefaultServer.java:259)
    at com.mongodb.internal.connection.DefaultServerConnection.executeProtocol(DefaultServerConnection.java:202)
    at com.mongodb.internal.connection.DefaultServerConnection.command(DefaultServerConnection.java:118)
    at com.mongodb.internal.connection.DefaultServerConnection.command(DefaultServerConnection.java:110)
    at com.mongodb.internal.operation.CommandOperationHelper.executeCommand(CommandOperationHelper.java:345)
    at com.mongodb.internal.operation.CommandOperationHelper.executeCommand(CommandOperationHelper.java:336)
    at com.mongodb.internal.operation.CommandOperationHelper.executeCommandWithConnection(CommandOperationHelper.java:222)
    at com.mongodb.internal.operation.CommandOperationHelper$5.call(CommandOperationHelper.java:208)
    at com.mongodb.internal.operation.OperationHelper.withReadConnectionSource(OperationHelper.java:583)
    at com.mongodb.internal.operation.CommandOperationHelper.executeCommand(CommandOperationHelper.java:205)
    at com.mongodb.internal.operation.AggregateOperationImpl.execute(AggregateOperationImpl.java:189)
    at com.mongodb.internal.operation.ChangeStreamOperation$1.call(ChangeStreamOperation.java:325)
    at com.mongodb.internal.operation.ChangeStreamOperation$1.call(ChangeStreamOperation.java:321)
    at com.mongodb.internal.operation.OperationHelper.withReadConnectionSource(OperationHelper.java:583)
    at com.mongodb.internal.operation.ChangeStreamOperation.execute(ChangeStreamOperation.java:321)
    at com.mongodb.internal.operation.ChangeStreamOperation.execute(ChangeStreamOperation.java:60)
    at com.mongodb.client.internal.MongoClientDelegate$DelegateOperationExecutor.execute(MongoClientDelegate.java:178)
    at com.mongodb.client.internal.ChangeStreamIterableImpl.execute(ChangeStreamIterableImpl.java:204)
    at com.mongodb.client.internal.ChangeStreamIterableImpl.cursor(ChangeStreamIterableImpl.java:158)
    at com.mongodb.client.internal.ChangeStreamIterableImpl.iterator(ChangeStreamIterableImpl.java:153)
    at com.softstrategy.ProvaWatcher.ProvaWatcherApplication.main(ProvaWatcherApplication.java:34)

另一方面,如果我将every file.conf节点中的enablemajorityreadconcern注解为默认值,则不会出现该异常。
因此,我的问题有以下两个:
为什么只有当readconcern设置为false并且节点down是数据承载节点时才会引发异常?
为什么当仲裁器节点关闭时,不管readconcern设置如何,都不会引发异常?
谢谢!

k5hmc34c

k5hmc34c1#

对于psa体系结构,如果其中一个数据承载节点不可用,则不再存在大多数数据承载节点。也就是说,您可以使用w:1插入,但不能使用w:maist,并且您将无法执行多数读取。
根据更改流https://docs.mongodb.com/manual/reference/read-concern-majority/#disable-阅读关注多数使用多数阅读关注:
禁用“多数”读取关注将禁用对mongodb 4.0及更早版本的更改流的支持。对于mongodb4.2+,禁用读关注点“多数”对变更流的可用性没有影响。
这也暗示了https://www.mongodb.com/blog/post/an-introduction-to-change-streams 鉴于
总订购量
mongodb3.6有一个全局逻辑时钟,使服务器能够在一个分片集群中对所有更改进行排序。应用程序将始终按照应用于数据库的顺序接收更改。
只有在多数读取的情况下,才能进行总排序。
如果希望更改流在其中一个数据承载节点不可用时继续生成事件,请使用pss体系结构。
您也可以尝试在4.2+上禁用read concern多数,但这还有其他问题,如第一个链接中所述。

相关问题