raft_test.go 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136
  1. package raft
  2. import (
  3. "bytes"
  4. "fmt"
  5. "math/rand"
  6. "reflect"
  7. "sort"
  8. "testing"
  9. )
  10. func TestLeaderElection(t *testing.T) {
  11. tests := []struct {
  12. *network
  13. state stateType
  14. }{
  15. {newNetwork(nil, nil, nil), stateLeader},
  16. {newNetwork(nil, nil, nopStepper), stateLeader},
  17. {newNetwork(nil, nopStepper, nopStepper), stateCandidate},
  18. {newNetwork(nil, nopStepper, nopStepper, nil), stateCandidate},
  19. {newNetwork(nil, nopStepper, nopStepper, nil, nil), stateLeader},
  20. // three logs further along than 0
  21. {newNetwork(nil, ents(1), ents(2), ents(1, 3), nil), stateFollower},
  22. // logs converge
  23. {newNetwork(ents(1), nil, ents(2), ents(1), nil), stateLeader},
  24. }
  25. for i, tt := range tests {
  26. tt.send(Message{From: 0, To: 0, Type: msgHup})
  27. sm := tt.network.peers[0].(*stateMachine)
  28. if sm.state != tt.state {
  29. t.Errorf("#%d: state = %s, want %s", i, sm.state, tt.state)
  30. }
  31. if g := sm.term.Get(); g != 1 {
  32. t.Errorf("#%d: term = %d, want %d", i, g, 1)
  33. }
  34. }
  35. }
  36. func TestLogReplication(t *testing.T) {
  37. tests := []struct {
  38. *network
  39. msgs []Message
  40. wcommitted int64
  41. }{
  42. {
  43. newNetwork(nil, nil, nil),
  44. []Message{
  45. {From: 0, To: 0, Type: msgProp, Entries: []Entry{{Data: []byte("somedata")}}},
  46. },
  47. 2,
  48. },
  49. {
  50. newNetwork(nil, nil, nil),
  51. []Message{
  52. {From: 0, To: 0, Type: msgProp, Entries: []Entry{{Data: []byte("somedata")}}},
  53. {From: 0, To: 1, Type: msgHup},
  54. {From: 0, To: 1, Type: msgProp, Entries: []Entry{{Data: []byte("somedata")}}},
  55. },
  56. 4,
  57. },
  58. }
  59. for i, tt := range tests {
  60. tt.send(Message{From: 0, To: 0, Type: msgHup})
  61. for _, m := range tt.msgs {
  62. tt.send(m)
  63. }
  64. for j, x := range tt.network.peers {
  65. sm := x.(*stateMachine)
  66. if sm.raftLog.committed != tt.wcommitted {
  67. t.Errorf("#%d.%d: committed = %d, want %d", i, j, sm.raftLog.committed, tt.wcommitted)
  68. }
  69. ents := make([]Entry, 0)
  70. for _, e := range sm.nextEnts() {
  71. if e.Data != nil {
  72. ents = append(ents, e)
  73. }
  74. }
  75. props := make([]Message, 0)
  76. for _, m := range tt.msgs {
  77. if m.Type == msgProp {
  78. props = append(props, m)
  79. }
  80. }
  81. for k, m := range props {
  82. if !bytes.Equal(ents[k].Data, m.Entries[0].Data) {
  83. t.Errorf("#%d.%d: data = %d, want %d", i, j, ents[k].Data, m.Entries[0].Data)
  84. }
  85. }
  86. }
  87. }
  88. }
  89. func TestSingleNodeCommit(t *testing.T) {
  90. tt := newNetwork(nil)
  91. tt.send(Message{From: 0, To: 0, Type: msgHup})
  92. tt.send(Message{From: 0, To: 0, Type: msgProp, Entries: []Entry{{Data: []byte("some data")}}})
  93. tt.send(Message{From: 0, To: 0, Type: msgProp, Entries: []Entry{{Data: []byte("some data")}}})
  94. sm := tt.peers[0].(*stateMachine)
  95. if sm.raftLog.committed != 3 {
  96. t.Errorf("committed = %d, want %d", sm.raftLog.committed, 3)
  97. }
  98. }
  99. // TestCannotCommitWithoutNewTermEntry tests the entries cannot be committed
  100. // when leader changes, no new proposal comes in and ChangeTerm proposal is
  101. // filtered.
  102. func TestCannotCommitWithoutNewTermEntry(t *testing.T) {
  103. tt := newNetwork(nil, nil, nil, nil, nil)
  104. tt.send(Message{From: 0, To: 0, Type: msgHup})
  105. // 0 cannot reach 2,3,4
  106. tt.cut(0, 2)
  107. tt.cut(0, 3)
  108. tt.cut(0, 4)
  109. tt.send(Message{From: 0, To: 0, Type: msgProp, Entries: []Entry{{Data: []byte("some data")}}})
  110. tt.send(Message{From: 0, To: 0, Type: msgProp, Entries: []Entry{{Data: []byte("some data")}}})
  111. sm := tt.peers[0].(*stateMachine)
  112. if sm.raftLog.committed != 1 {
  113. t.Errorf("committed = %d, want %d", sm.raftLog.committed, 1)
  114. }
  115. // network recovery
  116. tt.recover()
  117. // avoid committing ChangeTerm proposal
  118. tt.ignore(msgApp)
  119. // elect 1 as the new leader with term 2
  120. tt.send(Message{From: 1, To: 1, Type: msgHup})
  121. // no log entries from previous term should be committed
  122. sm = tt.peers[1].(*stateMachine)
  123. if sm.raftLog.committed != 1 {
  124. t.Errorf("committed = %d, want %d", sm.raftLog.committed, 1)
  125. }
  126. tt.recover()
  127. // send out a heartbeat
  128. // after append a ChangeTerm entry from the current term, all entries
  129. // should be committed
  130. tt.send(Message{From: 1, To: 1, Type: msgBeat})
  131. if sm.raftLog.committed != 4 {
  132. t.Errorf("committed = %d, want %d", sm.raftLog.committed, 4)
  133. }
  134. // still be able to append a entry
  135. tt.send(Message{From: 1, To: 1, Type: msgProp, Entries: []Entry{{Data: []byte("some data")}}})
  136. if sm.raftLog.committed != 5 {
  137. t.Errorf("committed = %d, want %d", sm.raftLog.committed, 5)
  138. }
  139. }
  140. // TestCommitWithoutNewTermEntry tests the entries could be committed
  141. // when leader changes, no new proposal comes in.
  142. func TestCommitWithoutNewTermEntry(t *testing.T) {
  143. tt := newNetwork(nil, nil, nil, nil, nil)
  144. tt.send(Message{From: 0, To: 0, Type: msgHup})
  145. // 0 cannot reach 2,3,4
  146. tt.cut(0, 2)
  147. tt.cut(0, 3)
  148. tt.cut(0, 4)
  149. tt.send(Message{From: 0, To: 0, Type: msgProp, Entries: []Entry{{Data: []byte("some data")}}})
  150. tt.send(Message{From: 0, To: 0, Type: msgProp, Entries: []Entry{{Data: []byte("some data")}}})
  151. sm := tt.peers[0].(*stateMachine)
  152. if sm.raftLog.committed != 1 {
  153. t.Errorf("committed = %d, want %d", sm.raftLog.committed, 1)
  154. }
  155. // network recovery
  156. tt.recover()
  157. // elect 1 as the new leader with term 2
  158. // after append a ChangeTerm entry from the current term, all entries
  159. // should be committed
  160. tt.send(Message{From: 1, To: 1, Type: msgHup})
  161. if sm.raftLog.committed != 4 {
  162. t.Errorf("committed = %d, want %d", sm.raftLog.committed, 4)
  163. }
  164. }
  165. func TestDuelingCandidates(t *testing.T) {
  166. a := newStateMachine(0, nil) // k, id are set later
  167. b := newStateMachine(0, nil)
  168. c := newStateMachine(0, nil)
  169. nt := newNetwork(a, b, c)
  170. nt.cut(0, 2)
  171. nt.send(Message{From: 0, To: 0, Type: msgHup})
  172. nt.send(Message{From: 2, To: 2, Type: msgHup})
  173. nt.recover()
  174. nt.send(Message{From: 2, To: 2, Type: msgHup})
  175. wlog := &raftLog{ents: []Entry{{}, Entry{Type: Normal, Data: nil, Term: 1, Index: 1}}, committed: 1}
  176. tests := []struct {
  177. sm *stateMachine
  178. state stateType
  179. term int64
  180. raftLog *raftLog
  181. }{
  182. {a, stateFollower, 2, wlog},
  183. {b, stateFollower, 2, wlog},
  184. {c, stateFollower, 2, newLog()},
  185. }
  186. for i, tt := range tests {
  187. if g := tt.sm.state; g != tt.state {
  188. t.Errorf("#%d: state = %s, want %s", i, g, tt.state)
  189. }
  190. if g := tt.sm.term.Get(); g != tt.term {
  191. t.Errorf("#%d: term = %d, want %d", i, g, tt.term)
  192. }
  193. base := ltoa(tt.raftLog)
  194. if sm, ok := nt.peers[int64(i)].(*stateMachine); ok {
  195. l := ltoa(sm.raftLog)
  196. if g := diffu(base, l); g != "" {
  197. t.Errorf("#%d: diff:\n%s", i, g)
  198. }
  199. } else {
  200. t.Logf("#%d: empty log", i)
  201. }
  202. }
  203. }
  204. func TestCandidateConcede(t *testing.T) {
  205. tt := newNetwork(nil, nil, nil)
  206. tt.isolate(0)
  207. tt.send(Message{From: 0, To: 0, Type: msgHup})
  208. tt.send(Message{From: 2, To: 2, Type: msgHup})
  209. // heal the partition
  210. tt.recover()
  211. data := []byte("force follower")
  212. // send a proposal to 2 to flush out a msgApp to 0
  213. tt.send(Message{From: 2, To: 2, Type: msgProp, Entries: []Entry{{Data: data}}})
  214. a := tt.peers[0].(*stateMachine)
  215. if g := a.state; g != stateFollower {
  216. t.Errorf("state = %s, want %s", g, stateFollower)
  217. }
  218. if g := a.term; g != 1 {
  219. t.Errorf("term = %d, want %d", g, 1)
  220. }
  221. wantLog := ltoa(&raftLog{ents: []Entry{{}, {Type: Normal, Data: nil, Term: 1, Index: 1}, {Term: 1, Index: 2, Data: data}}, committed: 2})
  222. for i, p := range tt.peers {
  223. if sm, ok := p.(*stateMachine); ok {
  224. l := ltoa(sm.raftLog)
  225. if g := diffu(wantLog, l); g != "" {
  226. t.Errorf("#%d: diff:\n%s", i, g)
  227. }
  228. } else {
  229. t.Logf("#%d: empty log", i)
  230. }
  231. }
  232. }
  233. func TestSingleNodeCandidate(t *testing.T) {
  234. tt := newNetwork(nil)
  235. tt.send(Message{From: 0, To: 0, Type: msgHup})
  236. sm := tt.peers[0].(*stateMachine)
  237. if sm.state != stateLeader {
  238. t.Errorf("state = %d, want %d", sm.state, stateLeader)
  239. }
  240. }
  241. func TestOldMessages(t *testing.T) {
  242. tt := newNetwork(nil, nil, nil)
  243. // make 0 leader @ term 3
  244. tt.send(Message{From: 0, To: 0, Type: msgHup})
  245. tt.send(Message{From: 1, To: 1, Type: msgHup})
  246. tt.send(Message{From: 0, To: 0, Type: msgHup})
  247. // pretend we're an old leader trying to make progress
  248. tt.send(Message{From: 0, To: 0, Type: msgApp, Term: 1, Entries: []Entry{{Term: 1}}})
  249. l := &raftLog{
  250. ents: []Entry{
  251. {}, {Type: Normal, Data: nil, Term: 1, Index: 1},
  252. {Type: Normal, Data: nil, Term: 2, Index: 2}, {Type: Normal, Data: nil, Term: 3, Index: 3},
  253. },
  254. committed: 3,
  255. }
  256. base := ltoa(l)
  257. for i, p := range tt.peers {
  258. if sm, ok := p.(*stateMachine); ok {
  259. l := ltoa(sm.raftLog)
  260. if g := diffu(base, l); g != "" {
  261. t.Errorf("#%d: diff:\n%s", i, g)
  262. }
  263. } else {
  264. t.Logf("#%d: empty log", i)
  265. }
  266. }
  267. }
  268. // TestOldMessagesReply - optimization - reply with new term.
  269. func TestProposal(t *testing.T) {
  270. tests := []struct {
  271. *network
  272. success bool
  273. }{
  274. {newNetwork(nil, nil, nil), true},
  275. {newNetwork(nil, nil, nopStepper), true},
  276. {newNetwork(nil, nopStepper, nopStepper), false},
  277. {newNetwork(nil, nopStepper, nopStepper, nil), false},
  278. {newNetwork(nil, nopStepper, nopStepper, nil, nil), true},
  279. }
  280. for i, tt := range tests {
  281. send := func(m Message) {
  282. defer func() {
  283. // only recover is we expect it to panic so
  284. // panics we don't expect go up.
  285. if !tt.success {
  286. e := recover()
  287. if e != nil {
  288. t.Logf("#%d: err: %s", i, e)
  289. }
  290. }
  291. }()
  292. tt.send(m)
  293. }
  294. data := []byte("somedata")
  295. // promote 0 the leader
  296. send(Message{From: 0, To: 0, Type: msgHup})
  297. send(Message{From: 0, To: 0, Type: msgProp, Entries: []Entry{{Data: data}}})
  298. wantLog := newLog()
  299. if tt.success {
  300. wantLog = &raftLog{ents: []Entry{{}, {Type: Normal, Data: nil, Term: 1, Index: 1}, {Term: 1, Index: 2, Data: data}}, committed: 2}
  301. }
  302. base := ltoa(wantLog)
  303. for i, p := range tt.peers {
  304. if sm, ok := p.(*stateMachine); ok {
  305. l := ltoa(sm.raftLog)
  306. if g := diffu(base, l); g != "" {
  307. t.Errorf("#%d: diff:\n%s", i, g)
  308. }
  309. } else {
  310. t.Logf("#%d: empty log", i)
  311. }
  312. }
  313. sm := tt.network.peers[0].(*stateMachine)
  314. if g := sm.term.Get(); g != 1 {
  315. t.Errorf("#%d: term = %d, want %d", i, g, 1)
  316. }
  317. }
  318. }
  319. func TestProposalByProxy(t *testing.T) {
  320. data := []byte("somedata")
  321. tests := []*network{
  322. newNetwork(nil, nil, nil),
  323. newNetwork(nil, nil, nopStepper),
  324. }
  325. for i, tt := range tests {
  326. // promote 0 the leader
  327. tt.send(Message{From: 0, To: 0, Type: msgHup})
  328. // propose via follower
  329. tt.send(Message{From: 1, To: 1, Type: msgProp, Entries: []Entry{{Data: []byte("somedata")}}})
  330. wantLog := &raftLog{ents: []Entry{{}, {Type: Normal, Data: nil, Term: 1, Index: 1}, {Term: 1, Data: data, Index: 2}}, committed: 2}
  331. base := ltoa(wantLog)
  332. for i, p := range tt.peers {
  333. if sm, ok := p.(*stateMachine); ok {
  334. l := ltoa(sm.raftLog)
  335. if g := diffu(base, l); g != "" {
  336. t.Errorf("#%d: diff:\n%s", i, g)
  337. }
  338. } else {
  339. t.Logf("#%d: empty log", i)
  340. }
  341. }
  342. sm := tt.peers[0].(*stateMachine)
  343. if g := sm.term.Get(); g != 1 {
  344. t.Errorf("#%d: term = %d, want %d", i, g, 1)
  345. }
  346. }
  347. }
  348. func TestCommit(t *testing.T) {
  349. tests := []struct {
  350. matches []int64
  351. logs []Entry
  352. smTerm int64
  353. w int64
  354. }{
  355. // single
  356. {[]int64{1}, []Entry{{}, {Term: 1}}, 1, 1},
  357. {[]int64{1}, []Entry{{}, {Term: 1}}, 2, 0},
  358. {[]int64{2}, []Entry{{}, {Term: 1}, {Term: 2}}, 2, 2},
  359. {[]int64{1}, []Entry{{}, {Term: 2}}, 2, 1},
  360. // odd
  361. {[]int64{2, 1, 1}, []Entry{{}, {Term: 1}, {Term: 2}}, 1, 1},
  362. {[]int64{2, 1, 1}, []Entry{{}, {Term: 1}, {Term: 1}}, 2, 0},
  363. {[]int64{2, 1, 2}, []Entry{{}, {Term: 1}, {Term: 2}}, 2, 2},
  364. {[]int64{2, 1, 2}, []Entry{{}, {Term: 1}, {Term: 1}}, 2, 0},
  365. // even
  366. {[]int64{2, 1, 1, 1}, []Entry{{}, {Term: 1}, {Term: 2}}, 1, 1},
  367. {[]int64{2, 1, 1, 1}, []Entry{{}, {Term: 1}, {Term: 1}}, 2, 0},
  368. {[]int64{2, 1, 1, 2}, []Entry{{}, {Term: 1}, {Term: 2}}, 1, 1},
  369. {[]int64{2, 1, 1, 2}, []Entry{{}, {Term: 1}, {Term: 1}}, 2, 0},
  370. {[]int64{2, 1, 2, 2}, []Entry{{}, {Term: 1}, {Term: 2}}, 2, 2},
  371. {[]int64{2, 1, 2, 2}, []Entry{{}, {Term: 1}, {Term: 1}}, 2, 0},
  372. }
  373. for i, tt := range tests {
  374. ins := make(map[int64]*index)
  375. for j := 0; j < len(tt.matches); j++ {
  376. ins[int64(j)] = &index{tt.matches[j], tt.matches[j] + 1}
  377. }
  378. sm := &stateMachine{raftLog: &raftLog{ents: tt.logs}, ins: ins, term: atomicInt(tt.smTerm)}
  379. sm.maybeCommit()
  380. if g := sm.raftLog.committed; g != tt.w {
  381. t.Errorf("#%d: committed = %d, want %d", i, g, tt.w)
  382. }
  383. }
  384. }
  385. // TestHandleMsgApp ensures:
  386. // 1. Reply false if log doesn’t contain an entry at prevLogIndex whose term matches prevLogTerm.
  387. // 2. If an existing entry conflicts with a new one (same index but different terms),
  388. // delete the existing entry and all that follow it; append any new entries not already in the log.
  389. // 3. If leaderCommit > commitIndex, set commitIndex = min(leaderCommit, index of last new entry).
  390. func TestHandleMsgApp(t *testing.T) {
  391. tests := []struct {
  392. m Message
  393. wIndex int64
  394. wCommit int64
  395. wAccept bool
  396. }{
  397. // Ensure 1
  398. {Message{Type: msgApp, Term: 2, LogTerm: 3, Index: 2, Commit: 3}, 2, 0, false}, // previous log mismatch
  399. {Message{Type: msgApp, Term: 2, LogTerm: 3, Index: 3, Commit: 3}, 2, 0, false}, // previous log non-exist
  400. // Ensure 2
  401. {Message{Type: msgApp, Term: 2, LogTerm: 1, Index: 1, Commit: 1}, 2, 1, true},
  402. {Message{Type: msgApp, Term: 2, LogTerm: 0, Index: 0, Commit: 1, Entries: []Entry{{Term: 2}}}, 1, 1, true},
  403. {Message{Type: msgApp, Term: 2, LogTerm: 2, Index: 2, Commit: 3, Entries: []Entry{{Term: 2}, {Term: 2}}}, 4, 3, true},
  404. {Message{Type: msgApp, Term: 2, LogTerm: 2, Index: 2, Commit: 4, Entries: []Entry{{Term: 2}}}, 3, 3, true},
  405. {Message{Type: msgApp, Term: 2, LogTerm: 1, Index: 1, Commit: 4, Entries: []Entry{{Term: 2}}}, 2, 2, true},
  406. // Ensure 3
  407. {Message{Type: msgApp, Term: 2, LogTerm: 2, Index: 2, Commit: 2}, 2, 2, true},
  408. {Message{Type: msgApp, Term: 2, LogTerm: 2, Index: 2, Commit: 4}, 2, 2, true}, // commit upto min(commit, last)
  409. }
  410. for i, tt := range tests {
  411. sm := &stateMachine{
  412. state: stateFollower,
  413. term: 2,
  414. raftLog: &raftLog{committed: 0, ents: []Entry{{}, {Term: 1}, {Term: 2}}},
  415. }
  416. sm.handleAppendEntries(tt.m)
  417. if sm.raftLog.lastIndex() != tt.wIndex {
  418. t.Errorf("#%d: lastIndex = %d, want %d", i, sm.raftLog.lastIndex(), tt.wIndex)
  419. }
  420. if sm.raftLog.committed != tt.wCommit {
  421. t.Errorf("#%d: committed = %d, want %d", i, sm.raftLog.committed, tt.wCommit)
  422. }
  423. m := sm.Msgs()
  424. if len(m) != 1 {
  425. t.Errorf("#%d: msg = nil, want 1")
  426. }
  427. gaccept := true
  428. if m[0].Index == -1 {
  429. gaccept = false
  430. }
  431. if gaccept != tt.wAccept {
  432. t.Errorf("#%d: accept = %v, want %v", gaccept, tt.wAccept)
  433. }
  434. }
  435. }
  436. func TestRecvMsgVote(t *testing.T) {
  437. tests := []struct {
  438. state stateType
  439. i, term int64
  440. voteFor int64
  441. w int64
  442. }{
  443. {stateFollower, 0, 0, none, -1},
  444. {stateFollower, 0, 1, none, -1},
  445. {stateFollower, 0, 2, none, -1},
  446. {stateFollower, 0, 3, none, 2},
  447. {stateFollower, 1, 0, none, -1},
  448. {stateFollower, 1, 1, none, -1},
  449. {stateFollower, 1, 2, none, -1},
  450. {stateFollower, 1, 3, none, 2},
  451. {stateFollower, 2, 0, none, -1},
  452. {stateFollower, 2, 1, none, -1},
  453. {stateFollower, 2, 2, none, 2},
  454. {stateFollower, 2, 3, none, 2},
  455. {stateFollower, 3, 0, none, -1},
  456. {stateFollower, 3, 1, none, -1},
  457. {stateFollower, 3, 2, none, 2},
  458. {stateFollower, 3, 3, none, 2},
  459. {stateFollower, 3, 2, 1, 2},
  460. {stateFollower, 3, 2, 0, -1},
  461. {stateLeader, 3, 3, 0, -1},
  462. {stateCandidate, 3, 3, 0, -1},
  463. }
  464. for i, tt := range tests {
  465. sm := &stateMachine{
  466. state: tt.state,
  467. vote: tt.voteFor,
  468. raftLog: &raftLog{ents: []Entry{{}, {Term: 2}, {Term: 2}}},
  469. }
  470. sm.Step(Message{Type: msgVote, From: 1, Index: tt.i, LogTerm: tt.term})
  471. msgs := sm.Msgs()
  472. if g := len(msgs); g != 1 {
  473. t.Errorf("#%d: len(msgs) = %d, want 1", i, g)
  474. continue
  475. }
  476. if g := msgs[0].Index; g != tt.w {
  477. t.Errorf("#%d, m.Index = %d, want %d", i, g, tt.w)
  478. }
  479. }
  480. }
  481. func TestStateTransition(t *testing.T) {
  482. tests := []struct {
  483. from stateType
  484. to stateType
  485. wallow bool
  486. wterm int64
  487. wlead int64
  488. }{
  489. {stateFollower, stateFollower, true, 1, none},
  490. {stateFollower, stateCandidate, true, 1, none},
  491. {stateFollower, stateLeader, false, -1, none},
  492. {stateCandidate, stateFollower, true, 0, none},
  493. {stateCandidate, stateCandidate, true, 1, none},
  494. {stateCandidate, stateLeader, true, 0, 0},
  495. {stateLeader, stateFollower, true, 1, none},
  496. {stateLeader, stateCandidate, false, 1, none},
  497. {stateLeader, stateLeader, true, 0, 0},
  498. }
  499. for i, tt := range tests {
  500. func() {
  501. defer func() {
  502. if r := recover(); r != nil {
  503. if tt.wallow == true {
  504. t.Errorf("%d: allow = %v, want %v", i, false, true)
  505. }
  506. }
  507. }()
  508. sm := newStateMachine(0, []int64{0})
  509. sm.state = tt.from
  510. switch tt.to {
  511. case stateFollower:
  512. sm.becomeFollower(tt.wterm, tt.wlead)
  513. case stateCandidate:
  514. sm.becomeCandidate()
  515. case stateLeader:
  516. sm.becomeLeader()
  517. }
  518. if sm.term.Get() != tt.wterm {
  519. t.Errorf("%d: term = %d, want %d", i, sm.term.Get(), tt.wterm)
  520. }
  521. if sm.lead.Get() != tt.wlead {
  522. t.Errorf("%d: lead = %d, want %d", i, sm.lead, tt.wlead)
  523. }
  524. }()
  525. }
  526. }
  527. func TestConf(t *testing.T) {
  528. sm := newStateMachine(0, []int64{0})
  529. sm.becomeCandidate()
  530. sm.becomeLeader()
  531. sm.Step(Message{From: 0, To: 0, Type: msgProp, Entries: []Entry{{Type: AddNode}}})
  532. if sm.raftLog.lastIndex() != 2 {
  533. t.Errorf("lastindex = %d, want %d", sm.raftLog.lastIndex(), 1)
  534. }
  535. if !sm.pendingConf {
  536. t.Errorf("pendingConf = %v, want %v", sm.pendingConf, true)
  537. }
  538. if sm.raftLog.ents[2].Type != AddNode {
  539. t.Errorf("type = %d, want %d", sm.raftLog.ents[1].Type, AddNode)
  540. }
  541. // deny the second configuration change request if there is a pending one
  542. sm.Step(Message{From: 0, To: 0, Type: msgProp, Entries: []Entry{{Type: AddNode}}})
  543. if sm.raftLog.lastIndex() != 2 {
  544. t.Errorf("lastindex = %d, want %d", sm.raftLog.lastIndex(), 1)
  545. }
  546. }
  547. // Ensures that the new leader sets the pendingConf flag correctly according to
  548. // the uncommitted log entries
  549. func TestConfChangeLeader(t *testing.T) {
  550. tests := []struct {
  551. et int64
  552. wPending bool
  553. }{
  554. {Normal, false},
  555. {AddNode, true},
  556. {RemoveNode, true},
  557. }
  558. for i, tt := range tests {
  559. sm := newStateMachine(0, []int64{0})
  560. sm.raftLog = &raftLog{ents: []Entry{{}, {Type: tt.et}}}
  561. sm.becomeCandidate()
  562. sm.becomeLeader()
  563. if sm.pendingConf != tt.wPending {
  564. t.Errorf("#%d: pendingConf = %v, want %v", i, sm.pendingConf, tt.wPending)
  565. }
  566. }
  567. }
  568. func TestAllServerStepdown(t *testing.T) {
  569. tests := []struct {
  570. state stateType
  571. wstate stateType
  572. wterm int64
  573. windex int64
  574. }{
  575. {stateFollower, stateFollower, 3, 1},
  576. {stateCandidate, stateFollower, 3, 1},
  577. {stateLeader, stateFollower, 3, 2},
  578. }
  579. tmsgTypes := [...]messageType{msgVote, msgApp}
  580. tterm := int64(3)
  581. for i, tt := range tests {
  582. sm := newStateMachine(0, []int64{0, 1, 2})
  583. switch tt.state {
  584. case stateFollower:
  585. sm.becomeFollower(1, 0)
  586. case stateCandidate:
  587. sm.becomeCandidate()
  588. case stateLeader:
  589. sm.becomeCandidate()
  590. sm.becomeLeader()
  591. }
  592. for j, msgType := range tmsgTypes {
  593. sm.Step(Message{From: 1, Type: msgType, Term: tterm, LogTerm: tterm})
  594. if sm.state != tt.wstate {
  595. t.Errorf("#%d.%d state = %v , want %v", i, j, sm.state, tt.wstate)
  596. }
  597. if sm.term.Get() != tt.wterm {
  598. t.Errorf("#%d.%d term = %v , want %v", i, j, sm.term.Get(), tt.wterm)
  599. }
  600. if int64(len(sm.raftLog.ents)) != tt.windex {
  601. t.Errorf("#%d.%d index = %v , want %v", i, j, len(sm.raftLog.ents), tt.windex)
  602. }
  603. wlead := int64(1)
  604. if msgType == msgVote {
  605. wlead = none
  606. }
  607. if sm.lead.Get() != wlead {
  608. t.Errorf("#%d, sm.lead = %d, want %d", i, sm.lead.Get(), none)
  609. }
  610. }
  611. }
  612. }
  613. func TestLeaderAppResp(t *testing.T) {
  614. tests := []struct {
  615. index int64
  616. wmsgNum int
  617. windex int64
  618. wcommitted int64
  619. }{
  620. {-1, 1, 1, 0}, // bad resp; leader does not commit; reply with log entries
  621. {2, 2, 2, 2}, // good resp; leader commits; broadcast with commit index
  622. }
  623. for i, tt := range tests {
  624. // sm term is 1 after it becomes the leader.
  625. // thus the last log term must be 1 to be committed.
  626. sm := newStateMachine(0, []int64{0, 1, 2})
  627. sm.raftLog = &raftLog{ents: []Entry{{}, {Term: 0}, {Term: 1}}}
  628. sm.becomeCandidate()
  629. sm.becomeLeader()
  630. sm.Msgs()
  631. sm.Step(Message{From: 1, Type: msgAppResp, Index: tt.index, Term: sm.term.Get()})
  632. msgs := sm.Msgs()
  633. if len(msgs) != tt.wmsgNum {
  634. t.Errorf("#%d msgNum = %d, want %d", i, len(msgs), tt.wmsgNum)
  635. }
  636. for j, msg := range msgs {
  637. if msg.Index != tt.windex {
  638. t.Errorf("#%d.%d index = %d, want %d", i, j, msg.Index, tt.windex)
  639. }
  640. if msg.Commit != tt.wcommitted {
  641. t.Errorf("#%d.%d commit = %d, want %d", i, j, msg.Commit, tt.wcommitted)
  642. }
  643. }
  644. }
  645. }
  646. // tests the output of the statemachine when receiving msgBeat
  647. func TestRecvMsgBeat(t *testing.T) {
  648. tests := []struct {
  649. state stateType
  650. wMsg int
  651. }{
  652. {stateLeader, 2},
  653. // candidate and follower should ignore msgBeat
  654. {stateCandidate, 0},
  655. {stateFollower, 0},
  656. }
  657. for i, tt := range tests {
  658. sm := newStateMachine(0, []int64{0, 1, 2})
  659. sm.raftLog = &raftLog{ents: []Entry{{}, {Term: 0}, {Term: 1}}}
  660. sm.term.Set(1)
  661. sm.state = tt.state
  662. sm.Step(Message{From: 0, To: 0, Type: msgBeat})
  663. msgs := sm.Msgs()
  664. if len(msgs) != tt.wMsg {
  665. t.Errorf("%d: len(msgs) = %d, want %d", i, len(msgs), tt.wMsg)
  666. }
  667. for _, m := range msgs {
  668. if m.Type != msgApp {
  669. t.Errorf("%d: msg.type = %v, want %v", m.Type, msgApp)
  670. }
  671. }
  672. }
  673. }
  674. func TestMaybeCompact(t *testing.T) {
  675. tests := []struct {
  676. snapshoter Snapshoter
  677. applied int64
  678. wCompact bool
  679. }{
  680. {nil, defaultCompactThreshold + 1, false},
  681. {new(logSnapshoter), defaultCompactThreshold - 1, false},
  682. {new(logSnapshoter), defaultCompactThreshold + 1, true},
  683. }
  684. for i, tt := range tests {
  685. sm := newStateMachine(0, []int64{0, 1, 2})
  686. sm.setSnapshoter(tt.snapshoter)
  687. for i := 0; i < defaultCompactThreshold*2; i++ {
  688. sm.raftLog.append(int64(i), Entry{Term: int64(i + 1)})
  689. }
  690. sm.raftLog.applied = tt.applied
  691. sm.raftLog.committed = tt.applied
  692. if g := sm.maybeCompact(); g != tt.wCompact {
  693. t.Errorf("#%d: compact = %v, want %v", i, g, tt.wCompact)
  694. }
  695. if tt.wCompact {
  696. s := sm.snapshoter.GetSnap()
  697. if s.Index != tt.applied {
  698. t.Errorf("#%d: snap.Index = %v, want %v", i, s.Index, tt.applied)
  699. }
  700. if s.Term != tt.applied {
  701. t.Errorf("#%d: snap.Term = %v, want %v", i, s.Index, tt.applied)
  702. }
  703. w := sm.nodes()
  704. sw := int64Slice(w)
  705. sg := int64Slice(s.Nodes)
  706. sort.Sort(sw)
  707. sort.Sort(sg)
  708. if !reflect.DeepEqual(sg, sw) {
  709. t.Errorf("#%d: snap.Nodes = %+v, want %+v", i, sg, sw)
  710. }
  711. }
  712. }
  713. }
  714. func TestRestore(t *testing.T) {
  715. s := Snapshot{
  716. Index: defaultCompactThreshold + 1,
  717. Term: defaultCompactThreshold + 1,
  718. Nodes: []int64{0, 1, 2},
  719. }
  720. tests := []struct {
  721. snapshoter Snapshoter
  722. wallow bool
  723. }{
  724. {nil, false},
  725. {new(logSnapshoter), true},
  726. }
  727. for i, tt := range tests {
  728. func() {
  729. defer func() {
  730. if r := recover(); r != nil {
  731. if tt.wallow == true {
  732. t.Errorf("%d: allow = %v, want %v", i, false, true)
  733. }
  734. }
  735. }()
  736. sm := newStateMachine(0, []int64{0, 1})
  737. sm.setSnapshoter(tt.snapshoter)
  738. sm.restore(s)
  739. if sm.raftLog.lastIndex() != s.Index {
  740. t.Errorf("#%d: log.lastIndex = %d, want %d", i, sm.raftLog.lastIndex(), s.Index)
  741. }
  742. if sm.raftLog.term(s.Index) != s.Term {
  743. t.Errorf("#%d: log.lastTerm = %d, want %d", i, sm.raftLog.term(s.Index), s.Term)
  744. }
  745. sg := int64Slice(sm.nodes())
  746. sw := int64Slice(s.Nodes)
  747. sort.Sort(sg)
  748. sort.Sort(sw)
  749. if !reflect.DeepEqual(sg, sw) {
  750. t.Errorf("#%d: sm.Nodes = %+v, want %+v", i, sg, sw)
  751. }
  752. if !reflect.DeepEqual(sm.snapshoter.GetSnap(), s) {
  753. t.Errorf("%d: snapshoter.getSnap = %+v, want %+v", sm.snapshoter.GetSnap(), s)
  754. }
  755. }()
  756. }
  757. }
  758. func TestProvideSnap(t *testing.T) {
  759. s := Snapshot{
  760. Index: defaultCompactThreshold + 1,
  761. Term: defaultCompactThreshold + 1,
  762. Nodes: []int64{0, 1},
  763. }
  764. sm := newStateMachine(0, []int64{0})
  765. sm.setSnapshoter(new(logSnapshoter))
  766. // restore the statemachin from a snapshot
  767. // so it has a compacted log and a snapshot
  768. sm.restore(s)
  769. sm.becomeCandidate()
  770. sm.becomeLeader()
  771. sm.Step(Message{From: 0, To: 0, Type: msgBeat})
  772. msgs := sm.Msgs()
  773. if len(msgs) != 1 {
  774. t.Errorf("len(msgs) = %d, want 1", len(msgs))
  775. }
  776. m := msgs[0]
  777. if m.Type != msgApp {
  778. t.Errorf("m.Type = %v, want %v", m.Type, msgApp)
  779. }
  780. // force set the next of node 1, so that
  781. // node 1 needs a snapshot
  782. sm.ins[1].next = sm.raftLog.offset
  783. sm.Step(Message{From: 1, To: 0, Type: msgAppResp, Index: -1})
  784. msgs = sm.Msgs()
  785. if len(msgs) != 1 {
  786. t.Errorf("len(msgs) = %d, want 1", len(msgs))
  787. }
  788. m = msgs[0]
  789. if m.Type != msgSnap {
  790. t.Errorf("m.Type = %v, want %v", m.Type, msgSnap)
  791. }
  792. }
  793. func TestRestoreFromSnapMsg(t *testing.T) {
  794. s := Snapshot{
  795. Index: defaultCompactThreshold + 1,
  796. Term: defaultCompactThreshold + 1,
  797. Nodes: []int64{0, 1},
  798. }
  799. m := Message{Type: msgSnap, From: 0, Term: 1, Snapshot: s}
  800. sm := newStateMachine(1, []int64{0, 1})
  801. sm.setSnapshoter(new(logSnapshoter))
  802. sm.Step(m)
  803. if !reflect.DeepEqual(sm.snapshoter.GetSnap(), s) {
  804. t.Errorf("snapshot = %+v, want %+v", sm.snapshoter.GetSnap(), s)
  805. }
  806. }
  807. func TestSlowNodeRestore(t *testing.T) {
  808. nt := newNetwork(nil, nil, nil)
  809. nt.send(Message{From: 0, To: 0, Type: msgHup})
  810. nt.isolate(2)
  811. for j := 0; j < defaultCompactThreshold+1; j++ {
  812. nt.send(Message{From: 0, To: 0, Type: msgProp, Entries: []Entry{{}}})
  813. }
  814. lead := nt.peers[0].(*stateMachine)
  815. lead.nextEnts()
  816. if !lead.maybeCompact() {
  817. t.Errorf("compacted = false, want true")
  818. }
  819. nt.recover()
  820. nt.send(Message{From: 0, To: 0, Type: msgBeat})
  821. follower := nt.peers[2].(*stateMachine)
  822. if !reflect.DeepEqual(follower.snapshoter.GetSnap(), lead.snapshoter.GetSnap()) {
  823. t.Errorf("follower.snap = %+v, want %+v", follower.snapshoter.GetSnap(), lead.snapshoter.GetSnap())
  824. }
  825. committed := follower.raftLog.lastIndex()
  826. nt.send(Message{From: 0, To: 0, Type: msgProp, Entries: []Entry{{}}})
  827. if follower.raftLog.committed != committed+1 {
  828. t.Errorf("follower.comitted = %d, want %d", follower.raftLog.committed, committed+1)
  829. }
  830. }
  831. func TestUnstableState(t *testing.T) {
  832. sm := newStateMachine(0, []int64{0})
  833. w := State{}
  834. sm.setVote(1)
  835. w.Vote = 1
  836. if !reflect.DeepEqual(sm.unstableState, w) {
  837. t.Errorf("unstableState = %v, want %v", sm.unstableState, w)
  838. }
  839. sm.clearState()
  840. sm.setTerm(1)
  841. w.Term = 1
  842. if !reflect.DeepEqual(sm.unstableState, w) {
  843. t.Errorf("unstableState = %v, want %v", sm.unstableState, w)
  844. }
  845. sm.clearState()
  846. sm.raftLog.committed = 1
  847. sm.addIns(1, 0, 0)
  848. w.Commit = 1
  849. if !reflect.DeepEqual(sm.unstableState, w) {
  850. t.Errorf("unstableState = %v, want %v", sm.unstableState, w)
  851. }
  852. sm.clearState()
  853. sm.raftLog.committed = 2
  854. sm.deleteIns(1)
  855. w.Commit = 2
  856. if !reflect.DeepEqual(sm.unstableState, w) {
  857. t.Errorf("unstableState = %v, want %v", sm.unstableState, w)
  858. }
  859. sm.clearState()
  860. }
  861. func ents(terms ...int64) *stateMachine {
  862. ents := []Entry{{}}
  863. for _, term := range terms {
  864. ents = append(ents, Entry{Term: term})
  865. }
  866. sm := &stateMachine{raftLog: &raftLog{ents: ents}}
  867. sm.reset(0)
  868. return sm
  869. }
  870. type network struct {
  871. peers map[int64]Interface
  872. dropm map[connem]float64
  873. ignorem map[messageType]bool
  874. }
  875. // newNetwork initializes a network from peers.
  876. // A nil node will be replaced with a new *stateMachine.
  877. // A *stateMachine will get its k, id.
  878. // When using stateMachine, the address list is always [0, n).
  879. func newNetwork(peers ...Interface) *network {
  880. size := len(peers)
  881. defaultPeerAddrs := make([]int64, size)
  882. for i := 0; i < size; i++ {
  883. defaultPeerAddrs[i] = int64(i)
  884. }
  885. npeers := make(map[int64]Interface, size)
  886. for id, p := range peers {
  887. nid := int64(id)
  888. switch v := p.(type) {
  889. case nil:
  890. sm := newStateMachine(nid, defaultPeerAddrs)
  891. sm.setSnapshoter(new(logSnapshoter))
  892. npeers[nid] = sm
  893. case *stateMachine:
  894. v.id = nid
  895. v.ins = make(map[int64]*index)
  896. for i := 0; i < size; i++ {
  897. v.ins[int64(i)] = &index{}
  898. }
  899. v.reset(0)
  900. npeers[nid] = v
  901. case *Node:
  902. npeers[v.sm.id] = v
  903. default:
  904. npeers[nid] = v
  905. }
  906. }
  907. return &network{
  908. peers: npeers,
  909. dropm: make(map[connem]float64),
  910. ignorem: make(map[messageType]bool),
  911. }
  912. }
  913. func (nw *network) send(msgs ...Message) {
  914. for len(msgs) > 0 {
  915. m := msgs[0]
  916. p := nw.peers[m.To]
  917. p.Step(m)
  918. msgs = append(msgs[1:], nw.filter(p.Msgs())...)
  919. }
  920. }
  921. func (nw *network) drop(from, to int64, perc float64) {
  922. nw.dropm[connem{from, to}] = perc
  923. }
  924. func (nw *network) cut(one, other int64) {
  925. nw.drop(one, other, 1)
  926. nw.drop(other, one, 1)
  927. }
  928. func (nw *network) isolate(id int64) {
  929. for i := 0; i < len(nw.peers); i++ {
  930. nid := int64(i)
  931. if nid != id {
  932. nw.drop(id, nid, 1.0)
  933. nw.drop(nid, id, 1.0)
  934. }
  935. }
  936. }
  937. func (nw *network) ignore(t messageType) {
  938. nw.ignorem[t] = true
  939. }
  940. func (nw *network) recover() {
  941. nw.dropm = make(map[connem]float64)
  942. nw.ignorem = make(map[messageType]bool)
  943. }
  944. func (nw *network) filter(msgs []Message) []Message {
  945. mm := make([]Message, 0)
  946. for _, m := range msgs {
  947. if nw.ignorem[m.Type] {
  948. continue
  949. }
  950. switch m.Type {
  951. case msgHup:
  952. // hups never go over the network, so don't drop them but panic
  953. panic("unexpected msgHup")
  954. default:
  955. perc := nw.dropm[connem{m.From, m.To}]
  956. if n := rand.Float64(); n < perc {
  957. continue
  958. }
  959. }
  960. mm = append(mm, m)
  961. }
  962. return mm
  963. }
  964. type connem struct {
  965. from, to int64
  966. }
  967. type blackHole struct{}
  968. func (blackHole) Step(Message) bool { return true }
  969. func (blackHole) Msgs() []Message { return nil }
  970. var nopStepper = &blackHole{}
  971. type logSnapshoter struct {
  972. snapshot Snapshot
  973. }
  974. func (s *logSnapshoter) Snap(index, term int64, nodes []int64) {
  975. s.snapshot = Snapshot{
  976. Index: index,
  977. Term: term,
  978. Nodes: nodes,
  979. Data: []byte(fmt.Sprintf("%d:%d", term, index)),
  980. }
  981. }
  982. func (s *logSnapshoter) Restore(ss Snapshot) {
  983. s.snapshot = ss
  984. }
  985. func (s *logSnapshoter) GetSnap() Snapshot {
  986. return s.snapshot
  987. }