raft_test.go 27 KB

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