Server Selection Algorithm

On this page本页内容

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驱动程序使用服务器选择算法来选择要使用哪个副本集成员,或者当连接到多个mongos实例时,选择要使用哪一个mongo实例。

Server selection occurs once per operation.每次操作都会选择一次服务器。

Note注意

Multi-document transactions that contain read operations must use read preference primary. 包含读取操作的多文档事务必须使用读取首选项primaryAll operations in a given transaction must route to the same member.给定事务中的所有操作都必须路由到同一成员。

Read Preference for Replica Sets副本集的读取首选项

Server selection occurs once per operation and is governed by the read preference and localThresholdMS settings to determine member eligibility for reads. 服务器选择在每个操作中发生一次,并由读取首选项localThresholdMS设置控制,以确定成员的读取资格。The read preference is re-evaluated for each operation.对每个操作重新评估读取首选项。

Read Preference Mode读取首选项模式Selection Process选择过程
primary (Default)
  1. The driver selects the primary.驾驶员选择主。
secondary
  1. The driver assembles a list of eligible secondary members. 驱动程序汇总合格的次要成员列表。maxStalenessSeconds and tag sets specified in the read preference can further restrict the eligibility of the members.maxStalenessSeconds和读取首选项中指定的标记集可以进一步限制成员的资格。
  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. 如果合格成员列表不为空,则驱动程序确定哪个合格成员是“最近”的成员(即平均网络往返时间最低的成员),并通过将此“最近”服务器的平均往返时间与localThresholdMS相加来计算延迟窗口。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.从属于延迟窗口的合格成员列表中,驱动程序随机选择一个合格成员。
nearest
  1. The driver assembles a list of eligible members (primary and secondaries). 驱动程序汇总合格成员(主要成员和次要成员)的列表。maxStalenessSeconds and tag sets specified in the read preference can further limit the eligibility of the members.maxStalenessSeconds和读取首选项中指定的标记集可以进一步限制成员的资格。
  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]. 如果合格成员列表不为空,则驱动程序确定哪个合格成员是“最近”的成员(即平均网络往返时间最低的成员),并通过将该“最近”服务器的平均往返时间与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. If the primary is available, driver selects the primary.如果主系统可用,驾驶员将选择主系统。
  2. If the primary is unavailable, server selection follows the process for the read preference secondary to select an eligible secondary member.如果主服务器不可用,则按照读取首选项secondary选择符合条件的辅助成员的过程进行服务器选择。
secondaryPreferred
  1. Following the server selection process for the read preference secondary, if a list of eligible secondary members is non-empty, driver chooses an eligible secondary member.在读取首选项secondary的服务器选择过程之后,如果符合条件的辅助成员列表为非空,则驱动程序将选择符合条件的次要成员。
  2. Otherwise, if the list is empty, driver selects the primary.否则,如果列表为空,驱动程序将选择主列表。

Read Preference for Sharded Clusters读取分片群集的首选项

Load Balancing负载平衡

If there is more than one mongos instance 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. 如果连接种子列表中有多个mongos实例,则驱动程序将确定哪个mongos是“最近的”(即平均网络往返时间最低的成员),并通过将该“最近”mongos的平均往返时间与localThresholdMS相加来计算延迟窗口。The driver will load balance randomly across the mongos instances that fall within the latency window.驱动程序将在处于延迟窗口内的mongos实例之间随机进行负载平衡。

Read Preference and Shards读取首选项和分片

For sharded clusters that have replica set shards, mongos applies the read preference when reading from the shards. 对于具有副本集分片的分片化集群,mongos在读取分片时应用读取首选项。Server selection is governed by the read preference and replication.localPingThresholdMs settings. 服务器选择由读取首选项replication.localPingThresholdMs设置控制。The read preference is re-evaluated for each operation.对每个操作重新评估读取首选项。

Hedged Reads对冲读取

Starting in version 4.4, mongos supports hedged reads for non-primary read preferences modes. 从4.4版开始,mongos支持非primary读取偏好模式的对冲读取That is, mongos can send an additional read to another member, if available, to hedge the read operation if using non-primary read preferences. 也就是说,mongos可以向另一个成员发送额外的读取(如果可用),以在使用非primary读取首选项时对冲读取操作。The additional read sent to hedge the read operation uses the maxTimeMS value of maxTimeMSForHedgedReads.为对冲读取操作而发送的附加读取使用maxTimeMSForHedgedReadsmaxTimeMS值。

Hedged reads are supported for the following operations:以下操作支持对冲读取:

To use hedged reads:要使用对冲读数:

Read Preference Mode读取首选项模式Selection Process选择过程
primary (Default)
  1. The mongos selects the primary.蒙哥人选择初选。
secondary
  1. The mongos assembles a list of eligible secondary members. mongos集合了一份合格的二级成员名单。maxStalenessSeconds and tag sets specified in the read preference can further restrict the eligibility of the members.maxStalenessSeconds和读取首选项中指定的标记集可以进一步限制成员的资格。
  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). 如果符合条件的成员列表不为空,mongos将确定哪个符合条件的会员是“最近的”(即平均网络往返时间最低的会员),并通过将此“最近”服务器的平均往返时间与replication.localPingThresholdMs(或--localThreshold命令行选项)相加来计算延迟窗口。The mongos uses this latency window to pare down the list of eligible members to those members that fall within this window.mongos使用此延迟窗口将符合条件的成员列表缩减为属于此窗口的成员。
  3. From this list of eligible members that fall within the latency window, the mongos randomly chooses an eligible member. 从属于延迟窗口的合格成员列表中,mongos随机选择一个合格成员。If using hedged reads, mongos selects a second eligible member if available.如果使用对冲读取mongos会选择第二个符合条件的成员(如果可用)。
nearest
  1. The mongos assembles a list of eligible members (primary and secondaries). mongos集合了一份合格成员名单(初级和次级)。maxStalenessSeconds and tag sets specified in the read preference can further limit the eligibility of the members.maxStalenessSeconds和读取首选项中指定的标记集可以进一步限制成员的资格。
  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]. 如果符合条件的成员列表不为空,mongos将确定哪个符合条件的会员是“最近的”(即平均网络往返时间最低的会员),并通过将此“最近”服务器的平均往返时间与replication.localPingThresholdMs(或--localThreshold命令行选项)相加来计算延迟窗口[1]The mongos uses this latency window to pare down the list of eligible members to those members that fall within this window.mongos使用此延迟窗口将符合条件的成员列表缩减为属于此窗口的成员。
  3. From this list of eligible members that fall within the latency window, the mongos randomly chooses an eligible member. 从属于延迟窗口的合格成员列表中,mongos随机选择一个合格成员。If using hedged reads, mongos selects a second eligible member if available.如果使用对冲读取mongos会选择第二个符合条件的成员(如果可用)。
primaryPreferred
  1. If the primary is available, mongos selects the primary.如果主节点可用,mongos将选择主节点。
  2. If the primary is unavailable or if mongos is using hedged reads, server selection follows the process for the read preference secondary. 如果主服务器不可用或mongos使用对冲读取,则服务器选择遵循读取首选项辅助服务器的过程。For hedged reads,对于对冲读取,

    • If the primary is available, mongos selects a single eligible secondary, if available.如果一级可用,mongos会选择一个合格的二级(如果可用)。
    • If the primary is unavailable, mongos selects two eligible secondaries, if available.如果主服务器不可用,mongos会选择两个符合条件的辅助服务器(如果可用)。
secondaryPreferred
  1. Following the server selection process for the read preference secondary, if a list of eligible secondary members is non-empty, mongos chooses an eligible secondary. 在读取首选项secondary的服务器选择过程之后,如果符合条件的辅助成员列表为非空,mongos将选择符合条件的辅服务器。If using hedged reads, mongos selects another secondary if available.如果使用对冲读取mongos会选择另一个secondary(如果可用)。
  2. If the list of eligible secondary members is empty or if mongos is using hedged reads and only one eligible secondary is available, mongos selects the primary.如果合格次级成员列表为空,或者mongos正在使用对冲读取,并且只有一个合格次级成员可用,mongos将选择主要成员。
[1](1, 2) The default threshold value is 15 milliseconds.默认阈值为15毫秒。
←  Read Preference Use CasesReplica Set Deployment Tutorials →