将新节点添加到 MongoDB 副本集时更改流反应

Change stream reaction in adding new node to MongoDB replica set

我正在使用 MongoDB 4.0 作为 3 节点副本集。 2 个数据节点和 1 个仲裁器。我的应用程序 (ASP.NET Core 2.2) 使用更改流。考虑这种情况,我的主节点出现故障,我的辅助节点成为主要节点,我用新节点替换我的副本集中出现故障的节点并开始同步。在这种情况下,我的更改流会发生什么情况?

我的第二个问题是,在我的应用程序中,readPreferencesecondary,当我的辅助节点出现故障时,我希望 MongoDB 自动从 primary 读取,但是它没有发生,我的应用程序在读取时有问题。正常吗?

Changestreams 对副本集选举具有弹性,并且会像什么都没发生一样继续。来自 changestream manual:

The change stream cursor remains open until one of the following occurs:

  • The cursor is explicitly closed.
  • An invalidate event occurs; for example, a collection drop or rename.
  • The connection to the MongoDB deployment is closed.
  • If the deployment is a sharded cluster, a shard removal may cause an open change stream cursor to close, and the closed change stream cursor may not be fully resumable.

副本集选举等所有其他事件不会影响更改流。

对于你的第二个问题,这是预料之中的,因为你的阅读偏好明确 secondary。要在辅助设备不可用时从主设备读取,您需要使用 secondaryPreferred 设置。