Replica Set Elections副本集选择
On this page本页内容
Replica sets副本集 use elections to determine which set member will become primary. 使用选举来确定哪个集合成员将成为primary。Replica sets can trigger an election in response to a variety of events, such as:副本集可以触发选择以响应各种事件,例如:
Adding a new node to the replica set,将新节点添加到副本集,启动复制集,initiating a replica set
,performing replica set maintenance using methods such as使用rs.stepDown()
orrs.reconfig()
, andrs.stepDown()
或rs.reconfig()
等方法执行副本集维护,以及the secondary members losing connectivity to the primary for more than the configuredsecondary成员失去与主要成员的连接的时间超过配置的timeout
(10 seconds by default).timeout
(默认为10秒)。
In the following diagram, the primary node was unavailable for longer than the 在下图中,主节点不可用的时间超过了配置的超时时间,并触发了自动故障转移过程。configured timeout
and triggers the automatic failover process. One of the remaining secondaries calls for an election to select a new primary and automatically resume normal operations.剩下的一个中学要求进行选举,选择一个新的小学,并自动恢复正常运作。
The replica set cannot process write operations until the election completes successfully. 在选举成功完成之前,副本集无法处理写入操作。The replica set can continue to serve read queries if such queries are configured to run on secondaries.如果将读取查询配置为在辅助设备上运行,则副本集可以继续为这些查询提供服务。
The median time before a cluster elects a new primary should not typically exceed 12 seconds, assuming default 假设默认的复制副本配置设置,群集选择新主服务器之前的中间时间通常不应超过12秒。replica configuration settings
. This includes time required to mark the primary as unavailable and call and complete an election. 这包括将初选标记为不可用、召集并完成选举所需的时间。You can tune this time period by modifying the 您可以通过修改settings.electionTimeoutMillis
replication configuration option. settings.electionTimeoutMillis
复制配置选项来调整此时间段。Factors such as network latency may extend the time required for replica set elections to complete, which in turn affects the amount of time your cluster may operate without a primary. 网络延迟等因素可能会延长副本集选举完成所需的时间,这反过来又会影响集群在没有主服务器的情况下运行的时间。These factors are dependent on your particular cluster architecture.这些因素取决于您的特定集群体系结构。
Your application connection logic should include tolerance for automatic failovers and the subsequent elections. 您的应用程序连接逻辑应该包括对自动故障切换和后续选择的容忍度。MongoDB drivers can detect the loss of the primary and automatically retry certain write operations a single time, providing additional built-in handling of automatic failovers and elections:MongoDB驱动程序可以检测到主写入操作的丢失,并一次自动重试某些写入操作,从而提供对自动故障切换和选择的额外内置处理:
Compatible drivers enable retryable writes by default兼容的驱动程序默认情况下启用可重试写入
Factors and Conditions that Affect Elections影响选举的因素和条件
Replication Election Protocol复制选择协议
Replication 复制protocolVersion: 1
reduces replica set failover time and accelerate the detection of multiple simultaneous primaries.protocolVersion: 1
减少了复制集故障切换时间,并加快了对多个同时主服务器的检测。
You can use 您可以使用catchUpTimeoutMillis
to prioritize between faster failovers and preservation of w:1
writes.catchUpTimeoutMillis
在更快的故障切换和保留w:1
写入之间进行优先级排序。
For more information on 有关pv1
, see Replica Set Protocol Version.pv1
的更多信息,请参阅副本集协议版本。
Heartbeats心跳
Replica set members send heartbeats (pings) to each other every two seconds. 副本集成员每两秒钟向对方发送一次检测信号(ping)。If a heartbeat does not return within 10 seconds, the other members mark the delinquent member as inaccessible.如果心跳在10秒内没有返回,其他成员会将违规成员标记为无法访问。
Member Priority成员优先级
After a replica set has a stable primary, the election algorithm will make a "best-effort" attempt to have the secondary with the highest 在副本集具有稳定的主集合后,选举算法将“尽最大努力”尝试让具有最高priority
available call an election. priority
的辅助集合调用选举。Member priority affects both the timing and the outcome of elections; secondaries with higher priority call elections relatively sooner than secondaries with lower priority, and are also more likely to win. 成员的优先次序影响选举的时间和结果;优先级较高的中学比优先级较低的中学更快地举行选举,也更有可能获胜。However, a lower priority instance can be elected as primary for brief periods, even if a higher priority secondary is available. 但是,优先级较低的实例可以在短时间内被选为主实例,即使优先级较高的辅助实例可用。Replica set members continue to call elections until the highest priority member available becomes primary.副本集成员继续调用选举,直到可用的最高优先级成员成为主要成员。
Members with a priority value of 优先级值为0
cannot become primary and do not seek election. 0
的成员不能成为主要成员,也不寻求选举。For details, see Priority 0 Replica Set Members.有关详细信息,请参阅优先级为0的副本集成员。
Mirrored Reads镜像读取
Starting in version 4.4, MongoDB provides mirrored reads to pre-warm electable secondary members' cache with the most recently accessed data. 从4.4版本开始,MongoDB为预热可选举辅助成员的缓存提供镜像读取,其中包含最近访问的数据。With mirrored reads, the primary can mirror a subset of operations that it receives and send them to a subset of electable secondaries. 通过镜像读取,主设备可以镜像其接收的操作的子集,并将其发送到可选择的辅助设备的子集。Pre-warming the cache of a secondary can help restore performance more quickly after an election.预先预热辅助缓存可以帮助在选举后更快地恢复性能。
For details, see Mirrored Reads.有关详细信息,请参阅镜像读取。
Loss of a Data Center数据中心丢失
With a distributed replica set, the loss of a data center may affect the ability of the remaining members in other data center or data centers to elect a primary.对于分布式副本集,数据中心的丢失可能会影响其他一个或多个数据中心中其余成员选择主副本的能力。
If possible, distribute the replica set members across data centers to maximize the likelihood that even with a loss of a data center, one of the remaining replica set members can become the new primary.如果可能,将副本集成员分布在数据中心之间,以最大限度地提高即使丢失数据中心,剩余副本集成员之一也可能成为新的主副本集成员的可能性。
Network Partition网络分区
A network partition may segregate a primary into a partition with a minority of nodes. 网络分区可以将主分区分隔为具有少数节点的分区。When the primary detects that it can only see a minority of nodes in the replica set, the primary steps down as primary and becomes a secondary. 当主节点检测到它只能看到复制集中的少数节点时,主节点将不再是主节点,而成为辅助节点。Independently, a member in the partition that can communicate with a 独立地,分区中可以与多数节点(包括其自身)通信的成员举行选举,成为新的主节点。majority
of the nodes (including itself) holds an election to become the new primary.
Voting Members有表决权的成员
The replica set member configuration setting 副本集成员配置设置members[n].votes
and member state
determine whether a member votes in an election.members[n].votes
,成员state
决定成员是否在选举中投票。
All replica set members that have their在选举中将其members[n].votes
setting equal to 1 vote in elections.members[n].votes
设置为等于1票的所有副本集成员。To exclude a member from voting in an election, change the value of the member's若要在选举中排除成员的投票,请将该成员的members[n].votes
configuration to0
.members[n].votes
配置值更改为0
。Only voting members in the following states are eligible to vote:只有以下州的有投票权的成员才有资格投票:
Non-Voting Members无表决权成员
Although non-voting members do not vote in elections, these members hold copies of the replica set's data and can accept read operations from client applications.尽管没有投票权的成员不在选举中投票,但这些成员持有副本集数据的副本,并且可以接受来自客户端应用程序的读取操作。
Because a replica set can have up to 50 members, but only 7 voting members, non-voting members allow a replica set to have more than seven members.因为副本集最多可以有50个成员,但只有7个投票成员,所以无投票成员允许副本集有7个以上的成员。
Non-voting (i.e. 无表决权(即votes
is 0
) members must have priority
of 0.votes
为0
)成员的priority
必须为0。
For instance, the following nine-member replica set has seven voting members and two non-voting members.例如,下面的九人复制集有七名有投票权的成员和两名无投票权的会员。
A non-voting member has both 无表决权成员的votes
and priority
equal to 0
:votes
和priority
均等于0:
{
"_id" : <num>,
"host" : <hostname:port>,
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 0,
"tags" : {
},
"secondaryDelaySecs" : NumberLong(0),
"votes" : 0
}
Do not alter the number of votes to control which members will become primary. 不要改变票数来控制哪些成员将成为初选成员。Instead, modify the 相反,请修改members[n].priority
option. Only alter the number of votes in exceptional cases. For example, to permit more than seven members.members[n].priority
选项。只在特殊情况下更改票数。例如,允许七名以上的成员。
To configure a non-voting member, see Configure Non-Voting Replica Set Member.要配置无表决权成员,请参阅配置无表决的副本集成员。