5.1 Raft basics
raft基础
A Raft cluster contains several servers; five is a typical number, which allows the system to tolerate two failures. At any given time each server is in one of three states: leader, follower, or candidate. In normal operation there is exactly one leader and all of the other servers are fol- lowers. Followers are passive: they issue no requests on their own but simply respond to requests from leaders and candidates. The leader handles all client requests (if a client contacts a follower, the follower redirects it to the leader). The third state, candidate, is used to elect a new leader as described in Section 5.2. Figure 4 shows the states and their transitions; the transitions are discussed below.
Raft集群包含多个服务器;5是一个典型数字,允许系统承受2次故障。在任何给定时间,每个服务器都处于三种状态之一:leader、follower或candidate。在正常操作中,只有一个leader服务器,而所有其他服务器都是以下服务器。follower是被动的:他们自己不发出请求,只是简单地回应领导人和候选人的请求。leader处理所有client请求(如果客户机与追随者联系,追随者会将其重定向到领导者)。第三个州候选人用于选举第5.2节所述的新领导人。图4显示了状态及其转换;下面将讨论这些转变。
图4:Server states. Followers only respond to requests from other servers. If a follower receives no communication, it becomes a candidate and initiates an election. A candidate that receives votes from a majority of the full cluster becomes the new leader. Leaders typically operate until they fail.
服务器状态。follower只响应来自其他服务器的请求。如果follower没有收到任何信息,他将成为candidate并发起选举。一位获得全团多数选票的candidate成为新的leader。leader通常会一直工作到失败。
Raft divides time into terms of arbitrary length, as shown in Figure 5. Terms are numbered with consecutive integers. Each term begins with an election, in which one or more candidates attempt to become leader as described in Section 5.2. If a candidate wins the election, then it serves as leader for the rest of the term. In some situations an election will result in a split vote. In this case the term will end with no leader; a new term (with a new election) will begin shortly. Raft ensures that there is at most one leader in a given term.
Raft将时间划分为任意长度,如图5所示。术语用连续整数编号。每届term以选举开始,其中一名或多名candidate试图成为第5.2节所述的leader。如果一名candidate赢得选举,那么他将在这个任期余下的时间内担任leader。在某些情况下,选举将导致分裂投票。在这种情况下,term将结束,没有leader;新的term(新的选举)即将开始。Raft确保在给定的期限内最多有一个leader。
Different servers may observe the transitions between terms at different times, and in some situations a server may not observe an election or even entire terms. Terms act as a logical clock [14] in Raft, and they allow servers to detect obsolete information such as stale leaders. Each server stores a current term number, which increases monotonically over time. Current terms are exchanged whenever servers communicate; if one server’s current term is smaller than the other’s, then it updates its current term to the larger value. If a candidate or leader discovers that its term is out of date, it immediately reverts to fol- lower state. If a server receives a request with a stale term number, it rejects the request.
不同的服务器可能会在不同的时间观察条款之间的转换,在某些情况下,服务器可能不会观察选举,甚至不会观察整个条款。术语在Raft中起着逻辑时钟的作用[14],它们允许服务器检测过时的信息,例如过时的领导者。每个服务器存储一个当前术语编号,该编号随时间单调增加。当服务器通信时,交换当前术语;如果一台服务器的当前术语小于另一台服务器的当前术语,则它会将其当前术语更新为更大的值。如果候选人或领导人发现其任期已过期,则会立即恢复到较低的状态。如果服务器接收到带有过时术语编号的请求,则会拒绝该请求。
Raft servers communicate using remote procedure calls (RPCs), and the basic consensus algorithm requires only two types of RPCs. RequestVote RPCs are initiated by candidates during elections (Section 5.2), and Append- Entries RPCs are initiated by leaders to replicate log en- tries and to provide a form of heartbeat (Section 5.3). Sec- tion 7 adds a third RPC for transferring snapshots between servers. Servers retry RPCs if they do not receive a re- sponse in a timely manner, and they issue RPCs in parallel for best performance.
图5:Time is divided into terms, and each term begins with an election. After a successful election, a single leader manages the cluster until the end of the term. Some elections fail, in which case the term ends without choosing a leader. The transitions between terms may be observed at different times on different servers.
时间分为几个term,每届term以选举开始。选举成功后,由一位领导人管理集群直至任期结束。有些选举失败,在这种情况下,任期结束时没有选出领导人。在不同的服务器上,可以在不同的时间观察到term之间的转换。



