raft_paper_test.go 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936
  1. // Copyright 2015 The etcd Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /*
  15. This file contains tests which verify that the scenarios described
  16. in the raft paper (https://raft.github.io/raft.pdf) are
  17. handled by the raft implementation correctly. Each test focuses on
  18. several sentences written in the paper. This could help us to prevent
  19. most implementation bugs.
  20. Each test is composed of three parts: init, test and check.
  21. Init part uses simple and understandable way to simulate the init state.
  22. Test part uses Step function to generate the scenario. Check part checks
  23. outgoing messages and state.
  24. */
  25. package raft
  26. import (
  27. "fmt"
  28. "reflect"
  29. "sort"
  30. "testing"
  31. pb "go.etcd.io/etcd/raft/raftpb"
  32. )
  33. func TestFollowerUpdateTermFromMessage(t *testing.T) {
  34. testUpdateTermFromMessage(t, StateFollower)
  35. }
  36. func TestCandidateUpdateTermFromMessage(t *testing.T) {
  37. testUpdateTermFromMessage(t, StateCandidate)
  38. }
  39. func TestLeaderUpdateTermFromMessage(t *testing.T) {
  40. testUpdateTermFromMessage(t, StateLeader)
  41. }
  42. // testUpdateTermFromMessage tests that if one server’s current term is
  43. // smaller than the other’s, then it updates its current term to the larger
  44. // value. If a candidate or leader discovers that its term is out of date,
  45. // it immediately reverts to follower state.
  46. // Reference: section 5.1
  47. func testUpdateTermFromMessage(t *testing.T, state StateType) {
  48. r := newTestRaft(1, []uint64{1, 2, 3}, 10, 1, NewMemoryStorage())
  49. switch state {
  50. case StateFollower:
  51. r.becomeFollower(1, 2)
  52. case StateCandidate:
  53. r.becomeCandidate()
  54. case StateLeader:
  55. r.becomeCandidate()
  56. r.becomeLeader()
  57. }
  58. r.Step(pb.Message{Type: pb.MsgApp, Term: 2})
  59. if r.Term != 2 {
  60. t.Errorf("term = %d, want %d", r.Term, 2)
  61. }
  62. if r.state != StateFollower {
  63. t.Errorf("state = %v, want %v", r.state, StateFollower)
  64. }
  65. }
  66. // TestRejectStaleTermMessage tests that if a server receives a request with
  67. // a stale term number, it rejects the request.
  68. // Our implementation ignores the request instead.
  69. // Reference: section 5.1
  70. func TestRejectStaleTermMessage(t *testing.T) {
  71. called := false
  72. fakeStep := func(r *raft, m pb.Message) error {
  73. called = true
  74. return nil
  75. }
  76. r := newTestRaft(1, []uint64{1, 2, 3}, 10, 1, NewMemoryStorage())
  77. r.step = fakeStep
  78. r.loadState(pb.HardState{Term: 2})
  79. r.Step(pb.Message{Type: pb.MsgApp, Term: r.Term - 1})
  80. if called {
  81. t.Errorf("stepFunc called = %v, want %v", called, false)
  82. }
  83. }
  84. // TestStartAsFollower tests that when servers start up, they begin as followers.
  85. // Reference: section 5.2
  86. func TestStartAsFollower(t *testing.T) {
  87. r := newTestRaft(1, []uint64{1, 2, 3}, 10, 1, NewMemoryStorage())
  88. if r.state != StateFollower {
  89. t.Errorf("state = %s, want %s", r.state, StateFollower)
  90. }
  91. }
  92. // TestLeaderBcastBeat tests that if the leader receives a heartbeat tick,
  93. // it will send a MsgHeartbeat with m.Index = 0, m.LogTerm=0 and empty entries
  94. // as heartbeat to all followers.
  95. // Reference: section 5.2
  96. func TestLeaderBcastBeat(t *testing.T) {
  97. // heartbeat interval
  98. hi := 1
  99. r := newTestRaft(1, []uint64{1, 2, 3}, 10, hi, NewMemoryStorage())
  100. r.becomeCandidate()
  101. r.becomeLeader()
  102. for i := 0; i < 10; i++ {
  103. mustAppendEntry(r, pb.Entry{Index: uint64(i) + 1})
  104. }
  105. for i := 0; i < hi; i++ {
  106. r.tick()
  107. }
  108. msgs := r.readMessages()
  109. sort.Sort(messageSlice(msgs))
  110. wmsgs := []pb.Message{
  111. {From: 1, To: 2, Term: 1, Type: pb.MsgHeartbeat},
  112. {From: 1, To: 3, Term: 1, Type: pb.MsgHeartbeat},
  113. }
  114. if !reflect.DeepEqual(msgs, wmsgs) {
  115. t.Errorf("msgs = %v, want %v", msgs, wmsgs)
  116. }
  117. }
  118. func TestFollowerStartElection(t *testing.T) {
  119. testNonleaderStartElection(t, StateFollower)
  120. }
  121. func TestCandidateStartNewElection(t *testing.T) {
  122. testNonleaderStartElection(t, StateCandidate)
  123. }
  124. // testNonleaderStartElection tests that if a follower receives no communication
  125. // over election timeout, it begins an election to choose a new leader. It
  126. // increments its current term and transitions to candidate state. It then
  127. // votes for itself and issues RequestVote RPCs in parallel to each of the
  128. // other servers in the cluster.
  129. // Reference: section 5.2
  130. // Also if a candidate fails to obtain a majority, it will time out and
  131. // start a new election by incrementing its term and initiating another
  132. // round of RequestVote RPCs.
  133. // Reference: section 5.2
  134. func testNonleaderStartElection(t *testing.T, state StateType) {
  135. // election timeout
  136. et := 10
  137. r := newTestRaft(1, []uint64{1, 2, 3}, et, 1, NewMemoryStorage())
  138. switch state {
  139. case StateFollower:
  140. r.becomeFollower(1, 2)
  141. case StateCandidate:
  142. r.becomeCandidate()
  143. }
  144. for i := 1; i < 2*et; i++ {
  145. r.tick()
  146. }
  147. if r.Term != 2 {
  148. t.Errorf("term = %d, want 2", r.Term)
  149. }
  150. if r.state != StateCandidate {
  151. t.Errorf("state = %s, want %s", r.state, StateCandidate)
  152. }
  153. if !r.prs.Votes[r.id] {
  154. t.Errorf("vote for self = false, want true")
  155. }
  156. msgs := r.readMessages()
  157. sort.Sort(messageSlice(msgs))
  158. wmsgs := []pb.Message{
  159. {From: 1, To: 2, Term: 2, Type: pb.MsgVote},
  160. {From: 1, To: 3, Term: 2, Type: pb.MsgVote},
  161. }
  162. if !reflect.DeepEqual(msgs, wmsgs) {
  163. t.Errorf("msgs = %v, want %v", msgs, wmsgs)
  164. }
  165. }
  166. // TestLeaderElectionInOneRoundRPC tests all cases that may happen in
  167. // leader election during one round of RequestVote RPC:
  168. // a) it wins the election
  169. // b) it loses the election
  170. // c) it is unclear about the result
  171. // Reference: section 5.2
  172. func TestLeaderElectionInOneRoundRPC(t *testing.T) {
  173. tests := []struct {
  174. size int
  175. votes map[uint64]bool
  176. state StateType
  177. }{
  178. // win the election when receiving votes from a majority of the servers
  179. {1, map[uint64]bool{}, StateLeader},
  180. {3, map[uint64]bool{2: true, 3: true}, StateLeader},
  181. {3, map[uint64]bool{2: true}, StateLeader},
  182. {5, map[uint64]bool{2: true, 3: true, 4: true, 5: true}, StateLeader},
  183. {5, map[uint64]bool{2: true, 3: true, 4: true}, StateLeader},
  184. {5, map[uint64]bool{2: true, 3: true}, StateLeader},
  185. // return to follower state if it receives vote denial from a majority
  186. {3, map[uint64]bool{2: false, 3: false}, StateFollower},
  187. {5, map[uint64]bool{2: false, 3: false, 4: false, 5: false}, StateFollower},
  188. {5, map[uint64]bool{2: true, 3: false, 4: false, 5: false}, StateFollower},
  189. // stay in candidate if it does not obtain the majority
  190. {3, map[uint64]bool{}, StateCandidate},
  191. {5, map[uint64]bool{2: true}, StateCandidate},
  192. {5, map[uint64]bool{2: false, 3: false}, StateCandidate},
  193. {5, map[uint64]bool{}, StateCandidate},
  194. }
  195. for i, tt := range tests {
  196. r := newTestRaft(1, idsBySize(tt.size), 10, 1, NewMemoryStorage())
  197. r.Step(pb.Message{From: 1, To: 1, Type: pb.MsgHup})
  198. for id, vote := range tt.votes {
  199. r.Step(pb.Message{From: id, To: 1, Term: r.Term, Type: pb.MsgVoteResp, Reject: !vote})
  200. }
  201. if r.state != tt.state {
  202. t.Errorf("#%d: state = %s, want %s", i, r.state, tt.state)
  203. }
  204. if g := r.Term; g != 1 {
  205. t.Errorf("#%d: term = %d, want %d", i, g, 1)
  206. }
  207. }
  208. }
  209. // TestFollowerVote tests that each follower will vote for at most one
  210. // candidate in a given term, on a first-come-first-served basis.
  211. // Reference: section 5.2
  212. func TestFollowerVote(t *testing.T) {
  213. tests := []struct {
  214. vote uint64
  215. nvote uint64
  216. wreject bool
  217. }{
  218. {None, 1, false},
  219. {None, 2, false},
  220. {1, 1, false},
  221. {2, 2, false},
  222. {1, 2, true},
  223. {2, 1, true},
  224. }
  225. for i, tt := range tests {
  226. r := newTestRaft(1, []uint64{1, 2, 3}, 10, 1, NewMemoryStorage())
  227. r.loadState(pb.HardState{Term: 1, Vote: tt.vote})
  228. r.Step(pb.Message{From: tt.nvote, To: 1, Term: 1, Type: pb.MsgVote})
  229. msgs := r.readMessages()
  230. wmsgs := []pb.Message{
  231. {From: 1, To: tt.nvote, Term: 1, Type: pb.MsgVoteResp, Reject: tt.wreject},
  232. }
  233. if !reflect.DeepEqual(msgs, wmsgs) {
  234. t.Errorf("#%d: msgs = %v, want %v", i, msgs, wmsgs)
  235. }
  236. }
  237. }
  238. // TestCandidateFallback tests that while waiting for votes,
  239. // if a candidate receives an AppendEntries RPC from another server claiming
  240. // to be leader whose term is at least as large as the candidate's current term,
  241. // it recognizes the leader as legitimate and returns to follower state.
  242. // Reference: section 5.2
  243. func TestCandidateFallback(t *testing.T) {
  244. tests := []pb.Message{
  245. {From: 2, To: 1, Term: 1, Type: pb.MsgApp},
  246. {From: 2, To: 1, Term: 2, Type: pb.MsgApp},
  247. }
  248. for i, tt := range tests {
  249. r := newTestRaft(1, []uint64{1, 2, 3}, 10, 1, NewMemoryStorage())
  250. r.Step(pb.Message{From: 1, To: 1, Type: pb.MsgHup})
  251. if r.state != StateCandidate {
  252. t.Fatalf("unexpected state = %s, want %s", r.state, StateCandidate)
  253. }
  254. r.Step(tt)
  255. if g := r.state; g != StateFollower {
  256. t.Errorf("#%d: state = %s, want %s", i, g, StateFollower)
  257. }
  258. if g := r.Term; g != tt.Term {
  259. t.Errorf("#%d: term = %d, want %d", i, g, tt.Term)
  260. }
  261. }
  262. }
  263. func TestFollowerElectionTimeoutRandomized(t *testing.T) {
  264. SetLogger(discardLogger)
  265. defer SetLogger(defaultLogger)
  266. testNonleaderElectionTimeoutRandomized(t, StateFollower)
  267. }
  268. func TestCandidateElectionTimeoutRandomized(t *testing.T) {
  269. SetLogger(discardLogger)
  270. defer SetLogger(defaultLogger)
  271. testNonleaderElectionTimeoutRandomized(t, StateCandidate)
  272. }
  273. // testNonleaderElectionTimeoutRandomized tests that election timeout for
  274. // follower or candidate is randomized.
  275. // Reference: section 5.2
  276. func testNonleaderElectionTimeoutRandomized(t *testing.T, state StateType) {
  277. et := 10
  278. r := newTestRaft(1, []uint64{1, 2, 3}, et, 1, NewMemoryStorage())
  279. timeouts := make(map[int]bool)
  280. for round := 0; round < 50*et; round++ {
  281. switch state {
  282. case StateFollower:
  283. r.becomeFollower(r.Term+1, 2)
  284. case StateCandidate:
  285. r.becomeCandidate()
  286. }
  287. time := 0
  288. for len(r.readMessages()) == 0 {
  289. r.tick()
  290. time++
  291. }
  292. timeouts[time] = true
  293. }
  294. for d := et + 1; d < 2*et; d++ {
  295. if !timeouts[d] {
  296. t.Errorf("timeout in %d ticks should happen", d)
  297. }
  298. }
  299. }
  300. func TestFollowersElectionTimeoutNonconflict(t *testing.T) {
  301. SetLogger(discardLogger)
  302. defer SetLogger(defaultLogger)
  303. testNonleadersElectionTimeoutNonconflict(t, StateFollower)
  304. }
  305. func TestCandidatesElectionTimeoutNonconflict(t *testing.T) {
  306. SetLogger(discardLogger)
  307. defer SetLogger(defaultLogger)
  308. testNonleadersElectionTimeoutNonconflict(t, StateCandidate)
  309. }
  310. // testNonleadersElectionTimeoutNonconflict tests that in most cases only a
  311. // single server(follower or candidate) will time out, which reduces the
  312. // likelihood of split vote in the new election.
  313. // Reference: section 5.2
  314. func testNonleadersElectionTimeoutNonconflict(t *testing.T, state StateType) {
  315. et := 10
  316. size := 5
  317. rs := make([]*raft, size)
  318. ids := idsBySize(size)
  319. for k := range rs {
  320. rs[k] = newTestRaft(ids[k], ids, et, 1, NewMemoryStorage())
  321. }
  322. conflicts := 0
  323. for round := 0; round < 1000; round++ {
  324. for _, r := range rs {
  325. switch state {
  326. case StateFollower:
  327. r.becomeFollower(r.Term+1, None)
  328. case StateCandidate:
  329. r.becomeCandidate()
  330. }
  331. }
  332. timeoutNum := 0
  333. for timeoutNum == 0 {
  334. for _, r := range rs {
  335. r.tick()
  336. if len(r.readMessages()) > 0 {
  337. timeoutNum++
  338. }
  339. }
  340. }
  341. // several rafts time out at the same tick
  342. if timeoutNum > 1 {
  343. conflicts++
  344. }
  345. }
  346. if g := float64(conflicts) / 1000; g > 0.3 {
  347. t.Errorf("probability of conflicts = %v, want <= 0.3", g)
  348. }
  349. }
  350. // TestLeaderStartReplication tests that when receiving client proposals,
  351. // the leader appends the proposal to its log as a new entry, then issues
  352. // AppendEntries RPCs in parallel to each of the other servers to replicate
  353. // the entry. Also, when sending an AppendEntries RPC, the leader includes
  354. // the index and term of the entry in its log that immediately precedes
  355. // the new entries.
  356. // Also, it writes the new entry into stable storage.
  357. // Reference: section 5.3
  358. func TestLeaderStartReplication(t *testing.T) {
  359. s := NewMemoryStorage()
  360. r := newTestRaft(1, []uint64{1, 2, 3}, 10, 1, s)
  361. r.becomeCandidate()
  362. r.becomeLeader()
  363. commitNoopEntry(r, s)
  364. li := r.raftLog.lastIndex()
  365. ents := []pb.Entry{{Data: []byte("some data")}}
  366. r.Step(pb.Message{From: 1, To: 1, Type: pb.MsgProp, Entries: ents})
  367. if g := r.raftLog.lastIndex(); g != li+1 {
  368. t.Errorf("lastIndex = %d, want %d", g, li+1)
  369. }
  370. if g := r.raftLog.committed; g != li {
  371. t.Errorf("committed = %d, want %d", g, li)
  372. }
  373. msgs := r.readMessages()
  374. sort.Sort(messageSlice(msgs))
  375. wents := []pb.Entry{{Index: li + 1, Term: 1, Data: []byte("some data")}}
  376. wmsgs := []pb.Message{
  377. {From: 1, To: 2, Term: 1, Type: pb.MsgApp, Index: li, LogTerm: 1, Entries: wents, Commit: li},
  378. {From: 1, To: 3, Term: 1, Type: pb.MsgApp, Index: li, LogTerm: 1, Entries: wents, Commit: li},
  379. }
  380. if !reflect.DeepEqual(msgs, wmsgs) {
  381. t.Errorf("msgs = %+v, want %+v", msgs, wmsgs)
  382. }
  383. if g := r.raftLog.unstableEntries(); !reflect.DeepEqual(g, wents) {
  384. t.Errorf("ents = %+v, want %+v", g, wents)
  385. }
  386. }
  387. // TestLeaderCommitEntry tests that when the entry has been safely replicated,
  388. // the leader gives out the applied entries, which can be applied to its state
  389. // machine.
  390. // Also, the leader keeps track of the highest index it knows to be committed,
  391. // and it includes that index in future AppendEntries RPCs so that the other
  392. // servers eventually find out.
  393. // Reference: section 5.3
  394. func TestLeaderCommitEntry(t *testing.T) {
  395. s := NewMemoryStorage()
  396. r := newTestRaft(1, []uint64{1, 2, 3}, 10, 1, s)
  397. r.becomeCandidate()
  398. r.becomeLeader()
  399. commitNoopEntry(r, s)
  400. li := r.raftLog.lastIndex()
  401. r.Step(pb.Message{From: 1, To: 1, Type: pb.MsgProp, Entries: []pb.Entry{{Data: []byte("some data")}}})
  402. for _, m := range r.readMessages() {
  403. r.Step(acceptAndReply(m))
  404. }
  405. if g := r.raftLog.committed; g != li+1 {
  406. t.Errorf("committed = %d, want %d", g, li+1)
  407. }
  408. wents := []pb.Entry{{Index: li + 1, Term: 1, Data: []byte("some data")}}
  409. if g := r.raftLog.nextEnts(); !reflect.DeepEqual(g, wents) {
  410. t.Errorf("nextEnts = %+v, want %+v", g, wents)
  411. }
  412. msgs := r.readMessages()
  413. sort.Sort(messageSlice(msgs))
  414. for i, m := range msgs {
  415. if w := uint64(i + 2); m.To != w {
  416. t.Errorf("to = %x, want %x", m.To, w)
  417. }
  418. if m.Type != pb.MsgApp {
  419. t.Errorf("type = %v, want %v", m.Type, pb.MsgApp)
  420. }
  421. if m.Commit != li+1 {
  422. t.Errorf("commit = %d, want %d", m.Commit, li+1)
  423. }
  424. }
  425. }
  426. // TestLeaderAcknowledgeCommit tests that a log entry is committed once the
  427. // leader that created the entry has replicated it on a majority of the servers.
  428. // Reference: section 5.3
  429. func TestLeaderAcknowledgeCommit(t *testing.T) {
  430. tests := []struct {
  431. size int
  432. acceptors map[uint64]bool
  433. wack bool
  434. }{
  435. {1, nil, true},
  436. {3, nil, false},
  437. {3, map[uint64]bool{2: true}, true},
  438. {3, map[uint64]bool{2: true, 3: true}, true},
  439. {5, nil, false},
  440. {5, map[uint64]bool{2: true}, false},
  441. {5, map[uint64]bool{2: true, 3: true}, true},
  442. {5, map[uint64]bool{2: true, 3: true, 4: true}, true},
  443. {5, map[uint64]bool{2: true, 3: true, 4: true, 5: true}, true},
  444. }
  445. for i, tt := range tests {
  446. s := NewMemoryStorage()
  447. r := newTestRaft(1, idsBySize(tt.size), 10, 1, s)
  448. r.becomeCandidate()
  449. r.becomeLeader()
  450. commitNoopEntry(r, s)
  451. li := r.raftLog.lastIndex()
  452. r.Step(pb.Message{From: 1, To: 1, Type: pb.MsgProp, Entries: []pb.Entry{{Data: []byte("some data")}}})
  453. for _, m := range r.readMessages() {
  454. if tt.acceptors[m.To] {
  455. r.Step(acceptAndReply(m))
  456. }
  457. }
  458. if g := r.raftLog.committed > li; g != tt.wack {
  459. t.Errorf("#%d: ack commit = %v, want %v", i, g, tt.wack)
  460. }
  461. }
  462. }
  463. // TestLeaderCommitPrecedingEntries tests that when leader commits a log entry,
  464. // it also commits all preceding entries in the leader’s log, including
  465. // entries created by previous leaders.
  466. // Also, it applies the entry to its local state machine (in log order).
  467. // Reference: section 5.3
  468. func TestLeaderCommitPrecedingEntries(t *testing.T) {
  469. tests := [][]pb.Entry{
  470. {},
  471. {{Term: 2, Index: 1}},
  472. {{Term: 1, Index: 1}, {Term: 2, Index: 2}},
  473. {{Term: 1, Index: 1}},
  474. }
  475. for i, tt := range tests {
  476. storage := NewMemoryStorage()
  477. storage.Append(tt)
  478. r := newTestRaft(1, []uint64{1, 2, 3}, 10, 1, storage)
  479. r.loadState(pb.HardState{Term: 2})
  480. r.becomeCandidate()
  481. r.becomeLeader()
  482. r.Step(pb.Message{From: 1, To: 1, Type: pb.MsgProp, Entries: []pb.Entry{{Data: []byte("some data")}}})
  483. for _, m := range r.readMessages() {
  484. r.Step(acceptAndReply(m))
  485. }
  486. li := uint64(len(tt))
  487. wents := append(tt, pb.Entry{Term: 3, Index: li + 1}, pb.Entry{Term: 3, Index: li + 2, Data: []byte("some data")})
  488. if g := r.raftLog.nextEnts(); !reflect.DeepEqual(g, wents) {
  489. t.Errorf("#%d: ents = %+v, want %+v", i, g, wents)
  490. }
  491. }
  492. }
  493. // TestFollowerCommitEntry tests that once a follower learns that a log entry
  494. // is committed, it applies the entry to its local state machine (in log order).
  495. // Reference: section 5.3
  496. func TestFollowerCommitEntry(t *testing.T) {
  497. tests := []struct {
  498. ents []pb.Entry
  499. commit uint64
  500. }{
  501. {
  502. []pb.Entry{
  503. {Term: 1, Index: 1, Data: []byte("some data")},
  504. },
  505. 1,
  506. },
  507. {
  508. []pb.Entry{
  509. {Term: 1, Index: 1, Data: []byte("some data")},
  510. {Term: 1, Index: 2, Data: []byte("some data2")},
  511. },
  512. 2,
  513. },
  514. {
  515. []pb.Entry{
  516. {Term: 1, Index: 1, Data: []byte("some data2")},
  517. {Term: 1, Index: 2, Data: []byte("some data")},
  518. },
  519. 2,
  520. },
  521. {
  522. []pb.Entry{
  523. {Term: 1, Index: 1, Data: []byte("some data")},
  524. {Term: 1, Index: 2, Data: []byte("some data2")},
  525. },
  526. 1,
  527. },
  528. }
  529. for i, tt := range tests {
  530. r := newTestRaft(1, []uint64{1, 2, 3}, 10, 1, NewMemoryStorage())
  531. r.becomeFollower(1, 2)
  532. r.Step(pb.Message{From: 2, To: 1, Type: pb.MsgApp, Term: 1, Entries: tt.ents, Commit: tt.commit})
  533. if g := r.raftLog.committed; g != tt.commit {
  534. t.Errorf("#%d: committed = %d, want %d", i, g, tt.commit)
  535. }
  536. wents := tt.ents[:int(tt.commit)]
  537. if g := r.raftLog.nextEnts(); !reflect.DeepEqual(g, wents) {
  538. t.Errorf("#%d: nextEnts = %v, want %v", i, g, wents)
  539. }
  540. }
  541. }
  542. // TestFollowerCheckMsgApp tests that if the follower does not find an
  543. // entry in its log with the same index and term as the one in AppendEntries RPC,
  544. // then it refuses the new entries. Otherwise it replies that it accepts the
  545. // append entries.
  546. // Reference: section 5.3
  547. func TestFollowerCheckMsgApp(t *testing.T) {
  548. ents := []pb.Entry{{Term: 1, Index: 1}, {Term: 2, Index: 2}}
  549. tests := []struct {
  550. term uint64
  551. index uint64
  552. windex uint64
  553. wreject bool
  554. wrejectHint uint64
  555. }{
  556. // match with committed entries
  557. {0, 0, 1, false, 0},
  558. {ents[0].Term, ents[0].Index, 1, false, 0},
  559. // match with uncommitted entries
  560. {ents[1].Term, ents[1].Index, 2, false, 0},
  561. // unmatch with existing entry
  562. {ents[0].Term, ents[1].Index, ents[1].Index, true, 2},
  563. // unexisting entry
  564. {ents[1].Term + 1, ents[1].Index + 1, ents[1].Index + 1, true, 2},
  565. }
  566. for i, tt := range tests {
  567. storage := NewMemoryStorage()
  568. storage.Append(ents)
  569. r := newTestRaft(1, []uint64{1, 2, 3}, 10, 1, storage)
  570. r.loadState(pb.HardState{Commit: 1})
  571. r.becomeFollower(2, 2)
  572. r.Step(pb.Message{From: 2, To: 1, Type: pb.MsgApp, Term: 2, LogTerm: tt.term, Index: tt.index})
  573. msgs := r.readMessages()
  574. wmsgs := []pb.Message{
  575. {From: 1, To: 2, Type: pb.MsgAppResp, Term: 2, Index: tt.windex, Reject: tt.wreject, RejectHint: tt.wrejectHint},
  576. }
  577. if !reflect.DeepEqual(msgs, wmsgs) {
  578. t.Errorf("#%d: msgs = %+v, want %+v", i, msgs, wmsgs)
  579. }
  580. }
  581. }
  582. // TestFollowerAppendEntries tests that when AppendEntries RPC is valid,
  583. // the follower will delete the existing conflict entry and all that follow it,
  584. // and append any new entries not already in the log.
  585. // Also, it writes the new entry into stable storage.
  586. // Reference: section 5.3
  587. func TestFollowerAppendEntries(t *testing.T) {
  588. tests := []struct {
  589. index, term uint64
  590. ents []pb.Entry
  591. wents []pb.Entry
  592. wunstable []pb.Entry
  593. }{
  594. {
  595. 2, 2,
  596. []pb.Entry{{Term: 3, Index: 3}},
  597. []pb.Entry{{Term: 1, Index: 1}, {Term: 2, Index: 2}, {Term: 3, Index: 3}},
  598. []pb.Entry{{Term: 3, Index: 3}},
  599. },
  600. {
  601. 1, 1,
  602. []pb.Entry{{Term: 3, Index: 2}, {Term: 4, Index: 3}},
  603. []pb.Entry{{Term: 1, Index: 1}, {Term: 3, Index: 2}, {Term: 4, Index: 3}},
  604. []pb.Entry{{Term: 3, Index: 2}, {Term: 4, Index: 3}},
  605. },
  606. {
  607. 0, 0,
  608. []pb.Entry{{Term: 1, Index: 1}},
  609. []pb.Entry{{Term: 1, Index: 1}, {Term: 2, Index: 2}},
  610. nil,
  611. },
  612. {
  613. 0, 0,
  614. []pb.Entry{{Term: 3, Index: 1}},
  615. []pb.Entry{{Term: 3, Index: 1}},
  616. []pb.Entry{{Term: 3, Index: 1}},
  617. },
  618. }
  619. for i, tt := range tests {
  620. storage := NewMemoryStorage()
  621. storage.Append([]pb.Entry{{Term: 1, Index: 1}, {Term: 2, Index: 2}})
  622. r := newTestRaft(1, []uint64{1, 2, 3}, 10, 1, storage)
  623. r.becomeFollower(2, 2)
  624. r.Step(pb.Message{From: 2, To: 1, Type: pb.MsgApp, Term: 2, LogTerm: tt.term, Index: tt.index, Entries: tt.ents})
  625. if g := r.raftLog.allEntries(); !reflect.DeepEqual(g, tt.wents) {
  626. t.Errorf("#%d: ents = %+v, want %+v", i, g, tt.wents)
  627. }
  628. if g := r.raftLog.unstableEntries(); !reflect.DeepEqual(g, tt.wunstable) {
  629. t.Errorf("#%d: unstableEnts = %+v, want %+v", i, g, tt.wunstable)
  630. }
  631. }
  632. }
  633. // TestLeaderSyncFollowerLog tests that the leader could bring a follower's log
  634. // into consistency with its own.
  635. // Reference: section 5.3, figure 7
  636. func TestLeaderSyncFollowerLog(t *testing.T) {
  637. ents := []pb.Entry{
  638. {},
  639. {Term: 1, Index: 1}, {Term: 1, Index: 2}, {Term: 1, Index: 3},
  640. {Term: 4, Index: 4}, {Term: 4, Index: 5},
  641. {Term: 5, Index: 6}, {Term: 5, Index: 7},
  642. {Term: 6, Index: 8}, {Term: 6, Index: 9}, {Term: 6, Index: 10},
  643. }
  644. term := uint64(8)
  645. tests := [][]pb.Entry{
  646. {
  647. {},
  648. {Term: 1, Index: 1}, {Term: 1, Index: 2}, {Term: 1, Index: 3},
  649. {Term: 4, Index: 4}, {Term: 4, Index: 5},
  650. {Term: 5, Index: 6}, {Term: 5, Index: 7},
  651. {Term: 6, Index: 8}, {Term: 6, Index: 9},
  652. },
  653. {
  654. {},
  655. {Term: 1, Index: 1}, {Term: 1, Index: 2}, {Term: 1, Index: 3},
  656. {Term: 4, Index: 4},
  657. },
  658. {
  659. {},
  660. {Term: 1, Index: 1}, {Term: 1, Index: 2}, {Term: 1, Index: 3},
  661. {Term: 4, Index: 4}, {Term: 4, Index: 5},
  662. {Term: 5, Index: 6}, {Term: 5, Index: 7},
  663. {Term: 6, Index: 8}, {Term: 6, Index: 9}, {Term: 6, Index: 10}, {Term: 6, Index: 11},
  664. },
  665. {
  666. {},
  667. {Term: 1, Index: 1}, {Term: 1, Index: 2}, {Term: 1, Index: 3},
  668. {Term: 4, Index: 4}, {Term: 4, Index: 5},
  669. {Term: 5, Index: 6}, {Term: 5, Index: 7},
  670. {Term: 6, Index: 8}, {Term: 6, Index: 9}, {Term: 6, Index: 10},
  671. {Term: 7, Index: 11}, {Term: 7, Index: 12},
  672. },
  673. {
  674. {},
  675. {Term: 1, Index: 1}, {Term: 1, Index: 2}, {Term: 1, Index: 3},
  676. {Term: 4, Index: 4}, {Term: 4, Index: 5}, {Term: 4, Index: 6}, {Term: 4, Index: 7},
  677. },
  678. {
  679. {},
  680. {Term: 1, Index: 1}, {Term: 1, Index: 2}, {Term: 1, Index: 3},
  681. {Term: 2, Index: 4}, {Term: 2, Index: 5}, {Term: 2, Index: 6},
  682. {Term: 3, Index: 7}, {Term: 3, Index: 8}, {Term: 3, Index: 9}, {Term: 3, Index: 10}, {Term: 3, Index: 11},
  683. },
  684. }
  685. for i, tt := range tests {
  686. leadStorage := NewMemoryStorage()
  687. leadStorage.Append(ents)
  688. lead := newTestRaft(1, []uint64{1, 2, 3}, 10, 1, leadStorage)
  689. lead.loadState(pb.HardState{Commit: lead.raftLog.lastIndex(), Term: term})
  690. followerStorage := NewMemoryStorage()
  691. followerStorage.Append(tt)
  692. follower := newTestRaft(2, []uint64{1, 2, 3}, 10, 1, followerStorage)
  693. follower.loadState(pb.HardState{Term: term - 1})
  694. // It is necessary to have a three-node cluster.
  695. // The second may have more up-to-date log than the first one, so the
  696. // first node needs the vote from the third node to become the leader.
  697. n := newNetwork(lead, follower, nopStepper)
  698. n.send(pb.Message{From: 1, To: 1, Type: pb.MsgHup})
  699. // The election occurs in the term after the one we loaded with
  700. // lead.loadState above.
  701. n.send(pb.Message{From: 3, To: 1, Type: pb.MsgVoteResp, Term: term + 1})
  702. n.send(pb.Message{From: 1, To: 1, Type: pb.MsgProp, Entries: []pb.Entry{{}}})
  703. if g := diffu(ltoa(lead.raftLog), ltoa(follower.raftLog)); g != "" {
  704. t.Errorf("#%d: log diff:\n%s", i, g)
  705. }
  706. }
  707. }
  708. // TestVoteRequest tests that the vote request includes information about the candidate’s log
  709. // and are sent to all of the other nodes.
  710. // Reference: section 5.4.1
  711. func TestVoteRequest(t *testing.T) {
  712. tests := []struct {
  713. ents []pb.Entry
  714. wterm uint64
  715. }{
  716. {[]pb.Entry{{Term: 1, Index: 1}}, 2},
  717. {[]pb.Entry{{Term: 1, Index: 1}, {Term: 2, Index: 2}}, 3},
  718. }
  719. for j, tt := range tests {
  720. r := newTestRaft(1, []uint64{1, 2, 3}, 10, 1, NewMemoryStorage())
  721. r.Step(pb.Message{
  722. From: 2, To: 1, Type: pb.MsgApp, Term: tt.wterm - 1, LogTerm: 0, Index: 0, Entries: tt.ents,
  723. })
  724. r.readMessages()
  725. for i := 1; i < r.electionTimeout*2; i++ {
  726. r.tickElection()
  727. }
  728. msgs := r.readMessages()
  729. sort.Sort(messageSlice(msgs))
  730. if len(msgs) != 2 {
  731. t.Fatalf("#%d: len(msg) = %d, want %d", j, len(msgs), 2)
  732. }
  733. for i, m := range msgs {
  734. if m.Type != pb.MsgVote {
  735. t.Errorf("#%d: msgType = %d, want %d", i, m.Type, pb.MsgVote)
  736. }
  737. if m.To != uint64(i+2) {
  738. t.Errorf("#%d: to = %d, want %d", i, m.To, i+2)
  739. }
  740. if m.Term != tt.wterm {
  741. t.Errorf("#%d: term = %d, want %d", i, m.Term, tt.wterm)
  742. }
  743. windex, wlogterm := tt.ents[len(tt.ents)-1].Index, tt.ents[len(tt.ents)-1].Term
  744. if m.Index != windex {
  745. t.Errorf("#%d: index = %d, want %d", i, m.Index, windex)
  746. }
  747. if m.LogTerm != wlogterm {
  748. t.Errorf("#%d: logterm = %d, want %d", i, m.LogTerm, wlogterm)
  749. }
  750. }
  751. }
  752. }
  753. // TestVoter tests the voter denies its vote if its own log is more up-to-date
  754. // than that of the candidate.
  755. // Reference: section 5.4.1
  756. func TestVoter(t *testing.T) {
  757. tests := []struct {
  758. ents []pb.Entry
  759. logterm uint64
  760. index uint64
  761. wreject bool
  762. }{
  763. // same logterm
  764. {[]pb.Entry{{Term: 1, Index: 1}}, 1, 1, false},
  765. {[]pb.Entry{{Term: 1, Index: 1}}, 1, 2, false},
  766. {[]pb.Entry{{Term: 1, Index: 1}, {Term: 1, Index: 2}}, 1, 1, true},
  767. // candidate higher logterm
  768. {[]pb.Entry{{Term: 1, Index: 1}}, 2, 1, false},
  769. {[]pb.Entry{{Term: 1, Index: 1}}, 2, 2, false},
  770. {[]pb.Entry{{Term: 1, Index: 1}, {Term: 1, Index: 2}}, 2, 1, false},
  771. // voter higher logterm
  772. {[]pb.Entry{{Term: 2, Index: 1}}, 1, 1, true},
  773. {[]pb.Entry{{Term: 2, Index: 1}}, 1, 2, true},
  774. {[]pb.Entry{{Term: 2, Index: 1}, {Term: 1, Index: 2}}, 1, 1, true},
  775. }
  776. for i, tt := range tests {
  777. storage := NewMemoryStorage()
  778. storage.Append(tt.ents)
  779. r := newTestRaft(1, []uint64{1, 2}, 10, 1, storage)
  780. r.Step(pb.Message{From: 2, To: 1, Type: pb.MsgVote, Term: 3, LogTerm: tt.logterm, Index: tt.index})
  781. msgs := r.readMessages()
  782. if len(msgs) != 1 {
  783. t.Fatalf("#%d: len(msg) = %d, want %d", i, len(msgs), 1)
  784. }
  785. m := msgs[0]
  786. if m.Type != pb.MsgVoteResp {
  787. t.Errorf("#%d: msgType = %d, want %d", i, m.Type, pb.MsgVoteResp)
  788. }
  789. if m.Reject != tt.wreject {
  790. t.Errorf("#%d: reject = %t, want %t", i, m.Reject, tt.wreject)
  791. }
  792. }
  793. }
  794. // TestLeaderOnlyCommitsLogFromCurrentTerm tests that only log entries from the leader’s
  795. // current term are committed by counting replicas.
  796. // Reference: section 5.4.2
  797. func TestLeaderOnlyCommitsLogFromCurrentTerm(t *testing.T) {
  798. ents := []pb.Entry{{Term: 1, Index: 1}, {Term: 2, Index: 2}}
  799. tests := []struct {
  800. index uint64
  801. wcommit uint64
  802. }{
  803. // do not commit log entries in previous terms
  804. {1, 0},
  805. {2, 0},
  806. // commit log in current term
  807. {3, 3},
  808. }
  809. for i, tt := range tests {
  810. storage := NewMemoryStorage()
  811. storage.Append(ents)
  812. r := newTestRaft(1, []uint64{1, 2}, 10, 1, storage)
  813. r.loadState(pb.HardState{Term: 2})
  814. // become leader at term 3
  815. r.becomeCandidate()
  816. r.becomeLeader()
  817. r.readMessages()
  818. // propose a entry to current term
  819. r.Step(pb.Message{From: 1, To: 1, Type: pb.MsgProp, Entries: []pb.Entry{{}}})
  820. r.Step(pb.Message{From: 2, To: 1, Type: pb.MsgAppResp, Term: r.Term, Index: tt.index})
  821. if r.raftLog.committed != tt.wcommit {
  822. t.Errorf("#%d: commit = %d, want %d", i, r.raftLog.committed, tt.wcommit)
  823. }
  824. }
  825. }
  826. type messageSlice []pb.Message
  827. func (s messageSlice) Len() int { return len(s) }
  828. func (s messageSlice) Less(i, j int) bool { return fmt.Sprint(s[i]) < fmt.Sprint(s[j]) }
  829. func (s messageSlice) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
  830. func commitNoopEntry(r *raft, s *MemoryStorage) {
  831. if r.state != StateLeader {
  832. panic("it should only be used when it is the leader")
  833. }
  834. r.bcastAppend()
  835. // simulate the response of MsgApp
  836. msgs := r.readMessages()
  837. for _, m := range msgs {
  838. if m.Type != pb.MsgApp || len(m.Entries) != 1 || m.Entries[0].Data != nil {
  839. panic("not a message to append noop entry")
  840. }
  841. r.Step(acceptAndReply(m))
  842. }
  843. // ignore further messages to refresh followers' commit index
  844. r.readMessages()
  845. s.Append(r.raftLog.unstableEntries())
  846. r.raftLog.appliedTo(r.raftLog.committed)
  847. r.raftLog.stableTo(r.raftLog.lastIndex(), r.raftLog.lastTerm())
  848. }
  849. func acceptAndReply(m pb.Message) pb.Message {
  850. if m.Type != pb.MsgApp {
  851. panic("type should be MsgApp")
  852. }
  853. return pb.Message{
  854. From: m.To,
  855. To: m.From,
  856. Term: m.Term,
  857. Type: pb.MsgAppResp,
  858. Index: m.Index + uint64(len(m.Entries)),
  859. }
  860. }