翻译或纠错本页面

Server Selection Algorithm

MongoDB drivers use a Server Selection algorithm to choose which replica set member to use or, when connected to multiple mongos instances, which mongos instance to use.

MongoDB驱动通过下列的流程来管理复制集和分片集群中的操作。为了决定如何路由它的操作,应用程序定期的获取复制集的状态视图,包括:节点是否活着,哪个节点是 主节点 ,检查每个 mongod 实例的延时情况。

Read Preference for Replica Sets

客户端经由他们的驱动或是分片集群中得 mongos 实例来定期的获取复制集的状态视图。

当我们选择 non-primary 模式的复制集读选项的时候,驱动将通过下列过流程决定将操作的发送目标:

收集可用的节点的名单,并按节点类型分类(如 从节点,主节点或是所有节点)。

Selection Process
primary (Default)
  1. The driver selects the primary.
secondary
  1. The driver assembles a list of eligible secondary members. Tag sets and read concern can further restrict the eligibility of the members.
  2. If the list of eligible members is not empty, the driver determines which eligible member is the “closest” (i.e. the member with the lowest average network round-trip-time) and calculates a latency window by adding the average round-trip-time of this “closest” server and the localThresholdMS. The driver uses this latency window to pare down the list of eligible members to those members that fall within this window.
  3. 接下来驱动就会与其建立连接。应用程序可以设置 请求绑定 。参考你所用的 驱动 的文档来获得更多有关请求绑定的配置和默认状态。

nearest
  1. The driver assembles a list of eligible members (primary and secondaries). Tag sets and read concern can further limit the eligibility of the members.
  2. If the list of eligible members is not empty, the driver determines which eligible member is the “closest” (i.e. the member with the lowest average network round-trip-time) and calculates a latency window by adding the average round-trip-time of this “closest” server and the localThresholdMS [1]. The driver uses this latency window to pare down the list of eligible members to those members that fall within this window.
  3. From this list of eligible members that fall within the latency window, the driver randomly chooses an eligible member.
primaryPreferred
  1. 客户端接收到socket异常,比如:有网络错误或是 mongod 进程在一次 故障切换 中关闭了连接。这将触发 retry ,对于应用程序来说这个过程是透明的(transparent)。

  2. 当使用请求绑定的时候,如果客户端发现复制集选举出了新的 主节点 ,那么驱动会解除所有线程与节点之间的关联。

secondaryPreferred
  1. MongoDB驱动与 复制集 中得 mongod 实例之间的连接需要考虑以下两个平衡点:

  2. Otherwise, if the list is empty, driver selects the primary.

客户端需要尽量减少因为连接、网络问题或是复制集的 故障切换 导致的无法连接数据库的时间。

Load Balancing

If there is more than one mongos instances in the connection seed list, the driver determines which mongos is the “closest” (i.e. the member with the lowest average network round-trip-time) and calculates the latency window by adding the average round-trip-time of this “closest” mongos instance and the localThresholdMS. The driver will load balance randomly across the mongos instances that fall within the latency window.

如果 mongod 上的连接丢失了,应用程序会尝试重连到一个符合现有 复制集读选项 的新节点上。

For sharded clusters that have replica set shards, mongos applies the read preference when reading from the shards. Server selection is governed by the read preference and replication.localPingThresholdMs settings.

在 3.0.0 版更改: 当我们选择 non-primary 模式的复制集读选项的时候,驱动将通过下列过流程决定将操作的发送目标:

收集可用的节点的名单,并按节点类型分类(如 从节点,主节点或是所有节点)。

Selection Process
primary (Default)
  1. The mongos selects the primary.
secondary
  1. 在报错后,驱动会根据指定的复制集读选项重新选择一个节点来连接。在没有指定复制集读选项的时候,驱动会使用 primary 模式。

  2. If the list of eligible members is not empty, the mongos determines which eligible member is the “closest” (i.e. the member with the lowest average network round-trip-time) and calculates a latency window by adding the average round-trip-time of this “closest” server and the replication.localPingThresholdMs (or --localThreshold command line option). The mongos uses this latency window to pare down the list of eligible members to those members that fall within this window.
  3. From this list of eligible members that fall within the latency window, the mongos randomly chooses an eligible member.
nearest
  1. 当发生了 故障切换 ,复制集的所有节点都会关闭所有现有连接并返回一个报错。这样可以防止或者最小化 回滚 的影响。

  2. If the list of eligible members is not empty, the mongos determines which eligible member is the “closest” (i.e. the member with the lowest average network round-trip-time) and calculates a latency window by adding the average round-trip-time of this “closest” server and the replication.localPingThresholdMs (or --localThreshold command line option) [1]. The mongos uses this latency window to pare down the list of eligible members to those members that fall within this window.
  3. 与普通复制集不同的是,在分片集群中,所有分片之间的交互是通过客户端连接到 mongos 实例后再连接到各个复制集的。 mongos 实例负责处理应用程序传来的复制集读选项,且对应用程序来说是透明的(transparent)。

primaryPreferred
  1. 在2.2版本后的分片集群架构中,复制集读选项的配置与复制集中完全一致。 所有的 mongos 实例都维护着连接到复制集的连接池,例如:

  2. 当使用请求绑定的时候,如果客户端发现复制集选举出了新的 主节点 ,那么驱动会解除所有线程与节点之间的关联。

secondaryPreferred
  1. 没有指定复制集读选项的请求默认使用 primary 模式,除非 mongos

  2. Otherwise, if the list is empty, mongos selects the primary.
[1](1, 2)

这是显而易见的,因为所有的数据都必须通过 mongos 实例传输到客户端中。