2015-11-26 11:05:16.0|分类: MongoDB|浏览量: 1772
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. In the following three-member replica set, the primary is unavailable. The remaining secondaries hold an election to choose a new primary. BehaviorElections are essential for independent operation of a replica set; however, elections take time to complete. While an election is in process, the replica set has no primary and cannot accept writes and all remaining members become read-only. MongoDB avoids elections unless necessary. If a majority of the replica set is inaccessible or unavailable, the replica set cannot accept writes and all remaining members become read-only. 如果副本集大多数成员不可用,副本集不能写,只能读 Factors and Conditions that Affect Elections影响选举的因素和条件Heartbeats心跳 10sReplica set members send heartbeats (pings) to each other every two seconds. If a heartbeat does not return within 10 seconds, the other members mark the delinquent member as inaccessible. Priority Comparisons优先级比较The priority setting affects elections. Members will prefer to vote for members with the highest priority value. Members with a priority value of 0 cannot become primary and do not seek election. For details, see Priority 0 Replica Set Members. 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. OptimeThe 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. ConnectionsA replica set member cannot become primary unless it can connect to a majority of the members in the replica set. For the purposes of elections, a majority refers to the total number of votes, rather than the total number of members. If you have a three-member replica set, where every member has one vote, the set can elect a primary as long as two members can connect to each other. If two members are unavailable, the remaining member remains a secondary because it cannot connect to a majority of the set’s members. If the remaining member is a primary and two members become unavailable, the primary steps down and becomes a secondary. Network Partitions网络分区Network partitions affect the formation of a majority for an election. If a primary steps down and neither portion of the replica set has a majority the set will not elect a new primary. The replica set becomes read-only. To avoid this situation, place a majority of instances in one data center and a minority of instances in any other data centers combined. Election Mechanics 选举机制Election Triggering EventsReplica sets hold an election any time there is no primary. Specifically, the following:
NOTE Priority 0 members, do not trigger elections, even when they cannot connect to the primary. A primary will step down:
In some cases, modifying a replica set’s configuration will trigger an election by modifying the set so that the primary must step down. IMPORTANT When a primary steps down, it closes all open client connections, so that clients don’t attempt to write data to a secondary. This helps clients maintain an accurate view of the replica set and helps preventrollbacks. Participation in Elections 参与选举Every replica set member has a priority that helps determine its eligibility to become a primary. In an election, the replica set elects an eligible member with the highest priority value as primary. By default, all members have a priority of 1 and have an equal chance of becoming primary. In the default, all members also can trigger an election.默认所有成员优先级是1,相同机会成为主副本 You can set the priority value to weight the election in favor of a particular member or group of members. For example, if you have a geographically distributed replica set, you can adjust priorities so that only members in a specific data center can become primary. The first member to receive the majority of votes becomes primary. By default, all members have a single vote, unless you modify the votes setting. Non-voting members have votes value of 0. All other members have 1 vote. NOTE Deprecated since version 2.6: votes values greater than 1. Earlier versions of MongoDB allowed a member to have more than 1 vote by setting votes to a value greater than 1. Setting votes to value greater than 1 now produces a warning message. The state of a member also affects its eligibility to vote. Only members in the following states can vote:PRIMARY, SECONDARY, RECOVERING, ARBITER, and ROLLBACK. IMPORTANT Do not alter the number of votes in a replica set to control the outcome of an election. Instead, modify the priority value. Vetoes in ElectionsAll members of a replica set can veto an election, including non-voting members. A member will veto an election:
Non-Voting Members 投票值为0Non-voting members hold copies of the replica set’s data and can accept read operations from client applications. Non-voting members do not vote in elections, but can veto an election and become primary. Because a replica set can have up to 12 members but only up to seven voting members, non-voting members allow a replica set to have more than seven members. For instance, the following nine-member replica set has seven voting members and two non-voting members. A non-voting member has a votes setting equal to 0 in its member configuration: { "_id" : <num> "host" : <hostname:port>, "votes" : 0} IMPORTANT Do not alter the number of votes to control which members will become primary. Instead, modify thepriority option. Only alter the number of votes in exceptional cases. For example, to permit more than seven members. When possible, all members should have one vote. Changing the number of votes can cause the wrong members to become primary. To configure a non-voting member, see Configure Non-Voting Replica Set Member. |