Docs Home → MongoDB Manual
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驱动程序使用服务器选择算法来选择使用哪个副本集成员,或者在连接到多个mongos
实例时,选择使用哪个mongos
示例。
Server selection occurs once per operation.每次操作都会选择一次服务器。
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. The read preference is re-evaluated for each operation.服务器选择在每次操作中发生一次,由读取首选项和localThresholdMS
设置控制,以确定成员是否有资格进行读取。每次操作都会重新评估读取首选项。
Read Preference Mode读取首选项模式 | Selection Process选择过程 |
primary (Default) |
The driver selects the primary.驱动程序选择primary。
|
secondary |
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 和标记集可以进一步限制成员的资格。
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.驱动程序使用此延迟窗口将符合条件的成员列表缩减为此窗口内的成员。
From this list of eligible members that fall within the latency window, the driver randomly chooses an eligible member.从延迟窗口内的合格成员列表中,驱动程序随机选择一个合格成员。
|
nearest |
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 和标记集可能会进一步限制成员的资格。
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.驱动程序使用此延迟窗口将符合条件的成员列表缩减为此窗口内的成员。
From this list of eligible members that fall within the latency window, the driver randomly chooses an eligible member.从延迟窗口内的合格成员列表中,驱动程序随机选择一个合格成员。
|
primaryPreferred |
If the primary is available, driver selects the primary.如果主驱动器可用,则驱动程序选择主驱动器。
If the primary is unavailable, server selection follows the process for the read preference secondary to select an eligible secondary member.如果主成员不可用,则服务器选择遵循读取首选项secondary 选择合格辅助成员的过程。
|
secondaryPreferred |
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 的服务器选择过程之后,如果符合条件的次要成员列表不为空,则驱动程序将选择符合条件的辅助成员。
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. The read preference is re-evaluated for each operation.服务器选择由读取首选项和replication.localPingThresholdMs
设置控制。每次操作都会重新评估读取首选项。
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. 也就是说,如果使用非primary
读取首选项,mongos
可以向另一个成员发送额外的读取(如果可用),以对冲读取操作。The additional read sent to hedge the read operation uses the maxTimeMS
value of maxTimeMSForHedgedReads
.为对冲读取操作而发送的附加读取使用maxTimeMSForHedgedReads
的maxTimeMS
值。
Hedged reads are supported for the following operations:套期保值读数支持以下操作:
To use hedged reads:要使用对冲读数:
Read Preference Mode读取首选项模式 | Selection Process选择过程 |
primary (Default) |
The mongos selects the primary.mongos 选择primary。
|
secondary |
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 和标记集可以进一步限制成员的资格。
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 使用这个延迟窗口将符合条件的成员列表缩减为该窗口内的成员。
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 |
The mongos assembles a list of eligible members (primary and secondaries). mongos 集合了一份合格成员(primary和secondary)的名单。maxStalenessSeconds and tag sets specified in the read preference can further limit the eligibility of the members.读取首选项中指定的maxStalenessSeconds 和标记集可能会进一步限制成员的资格。
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 使用这个延迟窗口将符合条件的成员列表缩减为该窗口内的成员。
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 |
If the primary is available, mongos selects the primary.如果主选项可用,mongos 将选择主选项。
If the primary is unavailable or if mongos is using hedged reads, server selection follows the process for the read preference secondary . For hedged reads, 如果主服务器不可用,或者mongos 正在使用对冲读取,则服务器选择将遵循读取首选项辅助服务器的过程。对于对冲读取,
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 |
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 的服务器选择过程之后,如果符合条件的secondary成员列表不为空,mongos 将选择符合条件的辅助成员。If using hedged reads, mongos selects another secondary if available.如果使用对冲读取,mongos 会选择另一个辅助读取(如果可用)。
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毫秒。 |