OPTIONS
翻译或纠错本页面

复制集选举

Replica sets use elections to determine which set member will become primary. Elections occur after initiating a replica set, and also any time the primary becomes unavailable. The primary is the only member in the set that can accept write operations. If a primary becomes unavailable, elections allow the set to recover normal operations without manual intervention. Elections are part of the failover process.

重要

对于复制集而言,选举是相对独立的操作,但是也需要时间来全部完成的。当选举开始的时候,复制集中没有主节点也不能接收处理写请求。除非遇到必要的情况,Mongodb是尽量不进行选举的。

在下述这样由三个节点组成的复制集中,主节点不可用了。剩余的从节点将进行选举并推选出一个新的主节点。

Diagram of an election of a new primary. In a three member replica set with two secondaries, the primary becomes unreachable. The loss of a primary triggers an election where one of the secondaries becomes the new primary

Diagram of an election of a new primary. In a three member replica set with two secondaries, the primary becomes unreachable. The loss of a primary triggers an election where one of the secondaries becomes the new primary

影响选举的因素

心跳检测

复制集成员每两秒向复制集中其他成员进行心跳检测。如果某个节点在10秒内没有返回,那么它将被标记为不可用。

优先级

The priority setting affects elections. Members will prefer to vote for members with the highest priority value.

优先级为0的节点将不能成为主节点,也不会发起选举。参见 优先级为0的复制集成员 获得更多信息。

A replica set does not hold an election as long as the current primary has the highest priority value or no secondary with higher priority is within 10 seconds of the latest oplog entry in the set.

If a higher-priority member catches up to within 10 seconds of the latest oplog entry of the current primary, the set holds an election in order to provide the higher-priority node a chance to become primary.

Optime

The optime is the timestamp of the last operation that a member applied from the oplog. A replica set member cannot become primary unless it has the highest (i.e. most recent) optime of any visible member in the set.

连接

如果复制集中的某个节点不能连接上其他多数节点,那么它将不能升职为主节点。在选举中,多数是指多数 投票 而不是多数节点个数。

如果复制集是由三个节点组成的,且三个节点均可投票,只要其中两个节点能够互相沟通那么复制集就能选举出新的主节点。如果有两个节点不可用了,那么剩下的节点将为 从节点 ,因为它不能与复制集中多数节点进行沟通。 如果两个从节点不可用了,剩下的 主节点 将降职为从节点。

网络隔离

网络隔离影响了选举中多数选票的结构。如果主节点不可用了,且每个相互隔离的网络中都没有多数选票的出现,那么复制集将 不会 选举出新的主节点。复制集将变为只读的。

为了避免这种情况的出现,我们需要将多数节点置于主数据中心,少数节点放于其他数据中心。

选举步骤

触发选举的情况

当复制集中没有主节点可用的时候将触发选举,比如:

  • 新复制集的初始化。

  • 一个从节点无法与主节点进行连接。当从节点们无法与主节点进行沟通的时候将会触发选举。

  • 主节点辞职了。

注解

Priority 0 members, do not trigger elections, even when they cannot connect to the primary.

主节点将在以下几种情况下辞职:

  • 在接收到 replSetStepDown 命令后。

  • 现有的某个从节点在选举中合格,且它又有更高的优先级。

  • 当主节点无法与复制集中多数节点进行沟通的时候。

有些情况下,在我们需要修改一些复制集配置的时候会触发选举,导致主节点辞职。

重要

当主节点辞职后,它将关闭所有已经建立的连接来确保客户端不会在从节点中进行写操作。这将对客户端对复制集的架构获取与防止 回滚 提供帮助。

参与选举

每个复制集的节点都有一个优先级来在选举中确认谁更适合做 主节点 。在选举中,复制集选举出一个合格的具有最高 priority 的节点作为新的主节点。所有节点默认的优先级都是 1,都有相同的机会来成为主节点。默认情况下,每个节点都可以触发选举。

我们可以通过设定 priority 来加重某个或者某些特殊的节点在选举中获得选票的优先级。比如,当我们有一个 异地分布式架构的复制集 ,我们可以通过设置优先级来使只有特定数据中心中的节点能够升职为主节点。

第一个获得多数选票的节点将会升职为主节点。默认情况下,每个节点都有一张选票,除非我们修改 votes不参与投票的节点 有0张 votes 。其他的节点有 ``1``张选票。

注解

2.6 版后已移除: votes 大于 1

MongoDB的早期版本中,是允许某个节点通过将 votes 设置大于 1 来让其拥有超过 1 张选票。现在将 votes 设置大于 1 会收到警告信息。

节点的 state 也讲决定其是否能够进行投票。只有在以下状态的节点,才能参与投票: PRIMARY(主节点) , SECONDARY(从节点) , RECOVERING(恢复中) , ARBITER(投票节点)ROLLBACK(回滚)

重要

我们应该通过设置 priority 的值来控制选举结果 ,而不是修改复制集成员的票数。

在选举中投票

每个复制集中的成员都可以否决选举,包括 不参与投票的节点 。在以下情况中,复制集中的节点会否决选举:

  • 当发起选举的节点与投票节点不处于同一个复制集中。

  • 当发起选举的节点中得数据在复制集中不是最新、最近的。

  • 当发起选举的节点的优先级比复制集中其他某个同样合格的节点的优先级低的时候。

  • 优先级为0的节点 [1] 是复制集中可用节点中数据最新的节点。这种情况下,另一个复制集中合格的节点将会追上该节点的数据并尝试升职为主节点。

  • 当当前的主节点比发起选举的节点拥有更新的数据 (i.e. 更高的 optime ) 的时候。

  • 当当前的主节点相比发起选举的节点拥有更新或相同的数据 (i.e. 更高或相同的 optime ) 的时候。

[1]

切记: 隐藏节点延时节点 意味着 优先级为0

不参与投票的节点

不参与投票的节点也拥有复制集的数据集副本,且可以接受请求。不参与投票的节点将不在选举中投票,但是 可以 否决 选举,也可以升职为主节点。

由于复制集可以最多拥有12个节点但是却只能有7个节点参与投票,不参与投票节点的存在就使得复制集可以拥有超过7个节点。

下述这样的拥有9个节点的复制集中就包含了7个参与投票的节点和2各不参与投票的节点。

Diagram of a 9 member replica set with the maximum of 7 voting members.

Diagram of a 9 member replica set with the maximum of 7 voting members.

不参与投票的节点的 votes 设置是 0

{
  "_id" : <num>
  "host" : <hostname:port>,
  "votes" : 0
}

重要

Do not alter the number of votes to control which members will become primary. Instead, modify the priority option. Only alter the number of votes in exceptional cases. For example, to permit more than seven members.

如果可能的话,尽量让每个节点都有一张选票。修改了节点拥有的选票数将会导致不合适的节点升职为主节点。

关于如何配置一个不参与投票的节点,请参见 配置一个不参与投票的节点