node_test.go 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020
  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. package raft
  15. import (
  16. "bytes"
  17. "context"
  18. "fmt"
  19. "math"
  20. "reflect"
  21. "strings"
  22. "testing"
  23. "time"
  24. "go.etcd.io/etcd/pkg/testutil"
  25. "go.etcd.io/etcd/raft/raftpb"
  26. )
  27. // readyWithTimeout selects from n.Ready() with a 1-second timeout. It
  28. // panics on timeout, which is better than the indefinite wait that
  29. // would occur if this channel were read without being wrapped in a
  30. // select.
  31. func readyWithTimeout(n Node) Ready {
  32. select {
  33. case rd := <-n.Ready():
  34. return rd
  35. case <-time.After(time.Second):
  36. panic("timed out waiting for ready")
  37. }
  38. }
  39. // TestNodeStep ensures that node.Step sends msgProp to propc chan
  40. // and other kinds of messages to recvc chan.
  41. func TestNodeStep(t *testing.T) {
  42. for i, msgn := range raftpb.MessageType_name {
  43. n := &node{
  44. propc: make(chan msgWithResult, 1),
  45. recvc: make(chan raftpb.Message, 1),
  46. }
  47. msgt := raftpb.MessageType(i)
  48. n.Step(context.TODO(), raftpb.Message{Type: msgt})
  49. // Proposal goes to proc chan. Others go to recvc chan.
  50. if msgt == raftpb.MsgProp {
  51. select {
  52. case <-n.propc:
  53. default:
  54. t.Errorf("%d: cannot receive %s on propc chan", msgt, msgn)
  55. }
  56. } else {
  57. if IsLocalMsg(msgt) {
  58. select {
  59. case <-n.recvc:
  60. t.Errorf("%d: step should ignore %s", msgt, msgn)
  61. default:
  62. }
  63. } else {
  64. select {
  65. case <-n.recvc:
  66. default:
  67. t.Errorf("%d: cannot receive %s on recvc chan", msgt, msgn)
  68. }
  69. }
  70. }
  71. }
  72. }
  73. // Cancel and Stop should unblock Step()
  74. func TestNodeStepUnblock(t *testing.T) {
  75. // a node without buffer to block step
  76. n := &node{
  77. propc: make(chan msgWithResult),
  78. done: make(chan struct{}),
  79. }
  80. ctx, cancel := context.WithCancel(context.Background())
  81. stopFunc := func() { close(n.done) }
  82. tests := []struct {
  83. unblock func()
  84. werr error
  85. }{
  86. {stopFunc, ErrStopped},
  87. {cancel, context.Canceled},
  88. }
  89. for i, tt := range tests {
  90. errc := make(chan error, 1)
  91. go func() {
  92. err := n.Step(ctx, raftpb.Message{Type: raftpb.MsgProp})
  93. errc <- err
  94. }()
  95. tt.unblock()
  96. select {
  97. case err := <-errc:
  98. if err != tt.werr {
  99. t.Errorf("#%d: err = %v, want %v", i, err, tt.werr)
  100. }
  101. //clean up side-effect
  102. if ctx.Err() != nil {
  103. ctx = context.TODO()
  104. }
  105. select {
  106. case <-n.done:
  107. n.done = make(chan struct{})
  108. default:
  109. }
  110. case <-time.After(1 * time.Second):
  111. t.Fatalf("#%d: failed to unblock step", i)
  112. }
  113. }
  114. }
  115. // TestNodePropose ensures that node.Propose sends the given proposal to the underlying raft.
  116. func TestNodePropose(t *testing.T) {
  117. msgs := []raftpb.Message{}
  118. appendStep := func(r *raft, m raftpb.Message) error {
  119. msgs = append(msgs, m)
  120. return nil
  121. }
  122. n := newNode()
  123. s := NewMemoryStorage()
  124. rn := newTestRawNode(1, []uint64{1}, 10, 1, s)
  125. r := rn.raft
  126. go n.run(rn)
  127. if err := n.Campaign(context.TODO()); err != nil {
  128. t.Fatal(err)
  129. }
  130. for {
  131. rd := <-n.Ready()
  132. s.Append(rd.Entries)
  133. // change the step function to appendStep until this raft becomes leader
  134. if rd.SoftState.Lead == r.id {
  135. r.step = appendStep
  136. n.Advance()
  137. break
  138. }
  139. n.Advance()
  140. }
  141. n.Propose(context.TODO(), []byte("somedata"))
  142. n.Stop()
  143. if len(msgs) != 1 {
  144. t.Fatalf("len(msgs) = %d, want %d", len(msgs), 1)
  145. }
  146. if msgs[0].Type != raftpb.MsgProp {
  147. t.Errorf("msg type = %d, want %d", msgs[0].Type, raftpb.MsgProp)
  148. }
  149. if !bytes.Equal(msgs[0].Entries[0].Data, []byte("somedata")) {
  150. t.Errorf("data = %v, want %v", msgs[0].Entries[0].Data, []byte("somedata"))
  151. }
  152. }
  153. // TestNodeReadIndex ensures that node.ReadIndex sends the MsgReadIndex message to the underlying raft.
  154. // It also ensures that ReadState can be read out through ready chan.
  155. func TestNodeReadIndex(t *testing.T) {
  156. msgs := []raftpb.Message{}
  157. appendStep := func(r *raft, m raftpb.Message) error {
  158. msgs = append(msgs, m)
  159. return nil
  160. }
  161. wrs := []ReadState{{Index: uint64(1), RequestCtx: []byte("somedata")}}
  162. n := newNode()
  163. s := NewMemoryStorage()
  164. rn := newTestRawNode(1, []uint64{1}, 10, 1, s)
  165. r := rn.raft
  166. r.readStates = wrs
  167. go n.run(rn)
  168. n.Campaign(context.TODO())
  169. for {
  170. rd := <-n.Ready()
  171. if !reflect.DeepEqual(rd.ReadStates, wrs) {
  172. t.Errorf("ReadStates = %v, want %v", rd.ReadStates, wrs)
  173. }
  174. s.Append(rd.Entries)
  175. if rd.SoftState.Lead == r.id {
  176. n.Advance()
  177. break
  178. }
  179. n.Advance()
  180. }
  181. r.step = appendStep
  182. wrequestCtx := []byte("somedata2")
  183. n.ReadIndex(context.TODO(), wrequestCtx)
  184. n.Stop()
  185. if len(msgs) != 1 {
  186. t.Fatalf("len(msgs) = %d, want %d", len(msgs), 1)
  187. }
  188. if msgs[0].Type != raftpb.MsgReadIndex {
  189. t.Errorf("msg type = %d, want %d", msgs[0].Type, raftpb.MsgReadIndex)
  190. }
  191. if !bytes.Equal(msgs[0].Entries[0].Data, wrequestCtx) {
  192. t.Errorf("data = %v, want %v", msgs[0].Entries[0].Data, wrequestCtx)
  193. }
  194. }
  195. // TestDisableProposalForwarding ensures that proposals are not forwarded to
  196. // the leader when DisableProposalForwarding is true.
  197. func TestDisableProposalForwarding(t *testing.T) {
  198. r1 := newTestRaft(1, []uint64{1, 2, 3}, 10, 1, NewMemoryStorage())
  199. r2 := newTestRaft(2, []uint64{1, 2, 3}, 10, 1, NewMemoryStorage())
  200. cfg3 := newTestConfig(3, []uint64{1, 2, 3}, 10, 1, NewMemoryStorage())
  201. cfg3.DisableProposalForwarding = true
  202. r3 := newRaft(cfg3)
  203. nt := newNetwork(r1, r2, r3)
  204. // elect r1 as leader
  205. nt.send(raftpb.Message{From: 1, To: 1, Type: raftpb.MsgHup})
  206. var testEntries = []raftpb.Entry{{Data: []byte("testdata")}}
  207. // send proposal to r2(follower) where DisableProposalForwarding is false
  208. r2.Step(raftpb.Message{From: 2, To: 2, Type: raftpb.MsgProp, Entries: testEntries})
  209. // verify r2(follower) does forward the proposal when DisableProposalForwarding is false
  210. if len(r2.msgs) != 1 {
  211. t.Fatalf("len(r2.msgs) expected 1, got %d", len(r2.msgs))
  212. }
  213. // send proposal to r3(follower) where DisableProposalForwarding is true
  214. r3.Step(raftpb.Message{From: 3, To: 3, Type: raftpb.MsgProp, Entries: testEntries})
  215. // verify r3(follower) does not forward the proposal when DisableProposalForwarding is true
  216. if len(r3.msgs) != 0 {
  217. t.Fatalf("len(r3.msgs) expected 0, got %d", len(r3.msgs))
  218. }
  219. }
  220. // TestNodeReadIndexToOldLeader ensures that raftpb.MsgReadIndex to old leader
  221. // gets forwarded to the new leader and 'send' method does not attach its term.
  222. func TestNodeReadIndexToOldLeader(t *testing.T) {
  223. r1 := newTestRaft(1, []uint64{1, 2, 3}, 10, 1, NewMemoryStorage())
  224. r2 := newTestRaft(2, []uint64{1, 2, 3}, 10, 1, NewMemoryStorage())
  225. r3 := newTestRaft(3, []uint64{1, 2, 3}, 10, 1, NewMemoryStorage())
  226. nt := newNetwork(r1, r2, r3)
  227. // elect r1 as leader
  228. nt.send(raftpb.Message{From: 1, To: 1, Type: raftpb.MsgHup})
  229. var testEntries = []raftpb.Entry{{Data: []byte("testdata")}}
  230. // send readindex request to r2(follower)
  231. r2.Step(raftpb.Message{From: 2, To: 2, Type: raftpb.MsgReadIndex, Entries: testEntries})
  232. // verify r2(follower) forwards this message to r1(leader) with term not set
  233. if len(r2.msgs) != 1 {
  234. t.Fatalf("len(r2.msgs) expected 1, got %d", len(r2.msgs))
  235. }
  236. readIndxMsg1 := raftpb.Message{From: 2, To: 1, Type: raftpb.MsgReadIndex, Entries: testEntries}
  237. if !reflect.DeepEqual(r2.msgs[0], readIndxMsg1) {
  238. t.Fatalf("r2.msgs[0] expected %+v, got %+v", readIndxMsg1, r2.msgs[0])
  239. }
  240. // send readindex request to r3(follower)
  241. r3.Step(raftpb.Message{From: 3, To: 3, Type: raftpb.MsgReadIndex, Entries: testEntries})
  242. // verify r3(follower) forwards this message to r1(leader) with term not set as well.
  243. if len(r3.msgs) != 1 {
  244. t.Fatalf("len(r3.msgs) expected 1, got %d", len(r3.msgs))
  245. }
  246. readIndxMsg2 := raftpb.Message{From: 3, To: 1, Type: raftpb.MsgReadIndex, Entries: testEntries}
  247. if !reflect.DeepEqual(r3.msgs[0], readIndxMsg2) {
  248. t.Fatalf("r3.msgs[0] expected %+v, got %+v", readIndxMsg2, r3.msgs[0])
  249. }
  250. // now elect r3 as leader
  251. nt.send(raftpb.Message{From: 3, To: 3, Type: raftpb.MsgHup})
  252. // let r1 steps the two messages previously we got from r2, r3
  253. r1.Step(readIndxMsg1)
  254. r1.Step(readIndxMsg2)
  255. // verify r1(follower) forwards these messages again to r3(new leader)
  256. if len(r1.msgs) != 2 {
  257. t.Fatalf("len(r1.msgs) expected 1, got %d", len(r1.msgs))
  258. }
  259. readIndxMsg3 := raftpb.Message{From: 1, To: 3, Type: raftpb.MsgReadIndex, Entries: testEntries}
  260. if !reflect.DeepEqual(r1.msgs[0], readIndxMsg3) {
  261. t.Fatalf("r1.msgs[0] expected %+v, got %+v", readIndxMsg3, r1.msgs[0])
  262. }
  263. if !reflect.DeepEqual(r1.msgs[1], readIndxMsg3) {
  264. t.Fatalf("r1.msgs[1] expected %+v, got %+v", readIndxMsg3, r1.msgs[1])
  265. }
  266. }
  267. // TestNodeProposeConfig ensures that node.ProposeConfChange sends the given configuration proposal
  268. // to the underlying raft.
  269. func TestNodeProposeConfig(t *testing.T) {
  270. msgs := []raftpb.Message{}
  271. appendStep := func(r *raft, m raftpb.Message) error {
  272. msgs = append(msgs, m)
  273. return nil
  274. }
  275. n := newNode()
  276. s := NewMemoryStorage()
  277. rn := newTestRawNode(1, []uint64{1}, 10, 1, s)
  278. r := rn.raft
  279. go n.run(rn)
  280. n.Campaign(context.TODO())
  281. for {
  282. rd := <-n.Ready()
  283. s.Append(rd.Entries)
  284. // change the step function to appendStep until this raft becomes leader
  285. if rd.SoftState.Lead == r.id {
  286. r.step = appendStep
  287. n.Advance()
  288. break
  289. }
  290. n.Advance()
  291. }
  292. cc := raftpb.ConfChange{Type: raftpb.ConfChangeAddNode, NodeID: 1}
  293. ccdata, err := cc.Marshal()
  294. if err != nil {
  295. t.Fatal(err)
  296. }
  297. n.ProposeConfChange(context.TODO(), cc)
  298. n.Stop()
  299. if len(msgs) != 1 {
  300. t.Fatalf("len(msgs) = %d, want %d", len(msgs), 1)
  301. }
  302. if msgs[0].Type != raftpb.MsgProp {
  303. t.Errorf("msg type = %d, want %d", msgs[0].Type, raftpb.MsgProp)
  304. }
  305. if !bytes.Equal(msgs[0].Entries[0].Data, ccdata) {
  306. t.Errorf("data = %v, want %v", msgs[0].Entries[0].Data, ccdata)
  307. }
  308. }
  309. // TestNodeProposeAddDuplicateNode ensures that two proposes to add the same node should
  310. // not affect the later propose to add new node.
  311. func TestNodeProposeAddDuplicateNode(t *testing.T) {
  312. n := newNode()
  313. s := NewMemoryStorage()
  314. rn := newTestRawNode(1, []uint64{1}, 10, 1, s)
  315. go n.run(rn)
  316. n.Campaign(context.TODO())
  317. rdyEntries := make([]raftpb.Entry, 0)
  318. ticker := time.NewTicker(time.Millisecond * 100)
  319. defer ticker.Stop()
  320. done := make(chan struct{})
  321. stop := make(chan struct{})
  322. applyConfChan := make(chan struct{})
  323. go func() {
  324. defer close(done)
  325. for {
  326. select {
  327. case <-stop:
  328. return
  329. case <-ticker.C:
  330. n.Tick()
  331. case rd := <-n.Ready():
  332. s.Append(rd.Entries)
  333. applied := false
  334. for _, e := range rd.Entries {
  335. rdyEntries = append(rdyEntries, e)
  336. switch e.Type {
  337. case raftpb.EntryNormal:
  338. case raftpb.EntryConfChange:
  339. var cc raftpb.ConfChange
  340. cc.Unmarshal(e.Data)
  341. n.ApplyConfChange(cc)
  342. applied = true
  343. }
  344. }
  345. n.Advance()
  346. if applied {
  347. applyConfChan <- struct{}{}
  348. }
  349. }
  350. }
  351. }()
  352. cc1 := raftpb.ConfChange{Type: raftpb.ConfChangeAddNode, NodeID: 1}
  353. ccdata1, _ := cc1.Marshal()
  354. n.ProposeConfChange(context.TODO(), cc1)
  355. <-applyConfChan
  356. // try add the same node again
  357. n.ProposeConfChange(context.TODO(), cc1)
  358. <-applyConfChan
  359. // the new node join should be ok
  360. cc2 := raftpb.ConfChange{Type: raftpb.ConfChangeAddNode, NodeID: 2}
  361. ccdata2, _ := cc2.Marshal()
  362. n.ProposeConfChange(context.TODO(), cc2)
  363. <-applyConfChan
  364. close(stop)
  365. <-done
  366. if len(rdyEntries) != 4 {
  367. t.Errorf("len(entry) = %d, want %d, %v\n", len(rdyEntries), 4, rdyEntries)
  368. }
  369. if !bytes.Equal(rdyEntries[1].Data, ccdata1) {
  370. t.Errorf("data = %v, want %v", rdyEntries[1].Data, ccdata1)
  371. }
  372. if !bytes.Equal(rdyEntries[3].Data, ccdata2) {
  373. t.Errorf("data = %v, want %v", rdyEntries[3].Data, ccdata2)
  374. }
  375. n.Stop()
  376. }
  377. // TestBlockProposal ensures that node will block proposal when it does not
  378. // know who is the current leader; node will accept proposal when it knows
  379. // who is the current leader.
  380. func TestBlockProposal(t *testing.T) {
  381. n := newNode()
  382. rn := newTestRawNode(1, []uint64{1}, 10, 1, NewMemoryStorage())
  383. go n.run(rn)
  384. defer n.Stop()
  385. errc := make(chan error, 1)
  386. go func() {
  387. errc <- n.Propose(context.TODO(), []byte("somedata"))
  388. }()
  389. testutil.WaitSchedule()
  390. select {
  391. case err := <-errc:
  392. t.Errorf("err = %v, want blocking", err)
  393. default:
  394. }
  395. n.Campaign(context.TODO())
  396. select {
  397. case err := <-errc:
  398. if err != nil {
  399. t.Errorf("err = %v, want %v", err, nil)
  400. }
  401. case <-time.After(10 * time.Second):
  402. t.Errorf("blocking proposal, want unblocking")
  403. }
  404. }
  405. func TestNodeProposeWaitDropped(t *testing.T) {
  406. msgs := []raftpb.Message{}
  407. droppingMsg := []byte("test_dropping")
  408. dropStep := func(r *raft, m raftpb.Message) error {
  409. if m.Type == raftpb.MsgProp && strings.Contains(m.String(), string(droppingMsg)) {
  410. t.Logf("dropping message: %v", m.String())
  411. return ErrProposalDropped
  412. }
  413. msgs = append(msgs, m)
  414. return nil
  415. }
  416. n := newNode()
  417. s := NewMemoryStorage()
  418. rn := newTestRawNode(1, []uint64{1}, 10, 1, s)
  419. r := rn.raft
  420. go n.run(rn)
  421. n.Campaign(context.TODO())
  422. for {
  423. rd := <-n.Ready()
  424. s.Append(rd.Entries)
  425. // change the step function to dropStep until this raft becomes leader
  426. if rd.SoftState.Lead == r.id {
  427. r.step = dropStep
  428. n.Advance()
  429. break
  430. }
  431. n.Advance()
  432. }
  433. proposalTimeout := time.Millisecond * 100
  434. ctx, cancel := context.WithTimeout(context.Background(), proposalTimeout)
  435. // propose with cancel should be cancelled earyly if dropped
  436. err := n.Propose(ctx, droppingMsg)
  437. if err != ErrProposalDropped {
  438. t.Errorf("should drop proposal : %v", err)
  439. }
  440. cancel()
  441. n.Stop()
  442. if len(msgs) != 0 {
  443. t.Fatalf("len(msgs) = %d, want %d", len(msgs), 1)
  444. }
  445. }
  446. // TestNodeTick ensures that node.Tick() will increase the
  447. // elapsed of the underlying raft state machine.
  448. func TestNodeTick(t *testing.T) {
  449. n := newNode()
  450. s := NewMemoryStorage()
  451. rn := newTestRawNode(1, []uint64{1}, 10, 1, s)
  452. r := rn.raft
  453. go n.run(rn)
  454. elapsed := r.electionElapsed
  455. n.Tick()
  456. for len(n.tickc) != 0 {
  457. time.Sleep(100 * time.Millisecond)
  458. }
  459. n.Stop()
  460. if r.electionElapsed != elapsed+1 {
  461. t.Errorf("elapsed = %d, want %d", r.electionElapsed, elapsed+1)
  462. }
  463. }
  464. // TestNodeStop ensures that node.Stop() blocks until the node has stopped
  465. // processing, and that it is idempotent
  466. func TestNodeStop(t *testing.T) {
  467. n := newNode()
  468. s := NewMemoryStorage()
  469. rn := newTestRawNode(1, []uint64{1}, 10, 1, s)
  470. donec := make(chan struct{})
  471. go func() {
  472. n.run(rn)
  473. close(donec)
  474. }()
  475. status := n.Status()
  476. n.Stop()
  477. select {
  478. case <-donec:
  479. case <-time.After(time.Second):
  480. t.Fatalf("timed out waiting for node to stop!")
  481. }
  482. emptyStatus := Status{}
  483. if reflect.DeepEqual(status, emptyStatus) {
  484. t.Errorf("status = %v, want not empty", status)
  485. }
  486. // Further status should return be empty, the node is stopped.
  487. status = n.Status()
  488. if !reflect.DeepEqual(status, emptyStatus) {
  489. t.Errorf("status = %v, want empty", status)
  490. }
  491. // Subsequent Stops should have no effect.
  492. n.Stop()
  493. }
  494. func TestReadyContainUpdates(t *testing.T) {
  495. tests := []struct {
  496. rd Ready
  497. wcontain bool
  498. }{
  499. {Ready{}, false},
  500. {Ready{SoftState: &SoftState{Lead: 1}}, true},
  501. {Ready{HardState: raftpb.HardState{Vote: 1}}, true},
  502. {Ready{Entries: make([]raftpb.Entry, 1)}, true},
  503. {Ready{CommittedEntries: make([]raftpb.Entry, 1)}, true},
  504. {Ready{Messages: make([]raftpb.Message, 1)}, true},
  505. {Ready{Snapshot: raftpb.Snapshot{Metadata: raftpb.SnapshotMetadata{Index: 1}}}, true},
  506. }
  507. for i, tt := range tests {
  508. if g := tt.rd.containsUpdates(); g != tt.wcontain {
  509. t.Errorf("#%d: containUpdates = %v, want %v", i, g, tt.wcontain)
  510. }
  511. }
  512. }
  513. // TestNodeStart ensures that a node can be started correctly. The node should
  514. // start with correct configuration change entries, and can accept and commit
  515. // proposals.
  516. func TestNodeStart(t *testing.T) {
  517. ctx, cancel := context.WithCancel(context.Background())
  518. defer cancel()
  519. cc := raftpb.ConfChange{Type: raftpb.ConfChangeAddNode, NodeID: 1}
  520. ccdata, err := cc.Marshal()
  521. if err != nil {
  522. t.Fatalf("unexpected marshal error: %v", err)
  523. }
  524. wants := []Ready{
  525. {
  526. HardState: raftpb.HardState{Term: 1, Commit: 1, Vote: 0},
  527. Entries: []raftpb.Entry{
  528. {Type: raftpb.EntryConfChange, Term: 1, Index: 1, Data: ccdata},
  529. },
  530. CommittedEntries: []raftpb.Entry{
  531. {Type: raftpb.EntryConfChange, Term: 1, Index: 1, Data: ccdata},
  532. },
  533. MustSync: true,
  534. },
  535. {
  536. HardState: raftpb.HardState{Term: 2, Commit: 3, Vote: 1},
  537. Entries: []raftpb.Entry{{Term: 2, Index: 3, Data: []byte("foo")}},
  538. CommittedEntries: []raftpb.Entry{{Term: 2, Index: 3, Data: []byte("foo")}},
  539. MustSync: true,
  540. },
  541. }
  542. storage := NewMemoryStorage()
  543. c := &Config{
  544. ID: 1,
  545. ElectionTick: 10,
  546. HeartbeatTick: 1,
  547. Storage: storage,
  548. MaxSizePerMsg: noLimit,
  549. MaxInflightMsgs: 256,
  550. }
  551. n := StartNode(c, []Peer{{ID: 1}})
  552. defer n.Stop()
  553. g := <-n.Ready()
  554. if !reflect.DeepEqual(g, wants[0]) {
  555. t.Fatalf("#%d: g = %+v,\n w %+v", 1, g, wants[0])
  556. } else {
  557. storage.Append(g.Entries)
  558. n.Advance()
  559. }
  560. if err := n.Campaign(ctx); err != nil {
  561. t.Fatal(err)
  562. }
  563. rd := <-n.Ready()
  564. storage.Append(rd.Entries)
  565. n.Advance()
  566. n.Propose(ctx, []byte("foo"))
  567. if g2 := <-n.Ready(); !reflect.DeepEqual(g2, wants[1]) {
  568. t.Errorf("#%d: g = %+v,\n w %+v", 2, g2, wants[1])
  569. } else {
  570. storage.Append(g2.Entries)
  571. n.Advance()
  572. }
  573. select {
  574. case rd := <-n.Ready():
  575. t.Errorf("unexpected Ready: %+v", rd)
  576. case <-time.After(time.Millisecond):
  577. }
  578. }
  579. func TestNodeRestart(t *testing.T) {
  580. entries := []raftpb.Entry{
  581. {Term: 1, Index: 1},
  582. {Term: 1, Index: 2, Data: []byte("foo")},
  583. }
  584. st := raftpb.HardState{Term: 1, Commit: 1}
  585. want := Ready{
  586. // No HardState is emitted because there was no change.
  587. HardState: raftpb.HardState{},
  588. // commit up to index commit index in st
  589. CommittedEntries: entries[:st.Commit],
  590. // MustSync is false because no HardState or new entries are provided.
  591. MustSync: false,
  592. }
  593. storage := NewMemoryStorage()
  594. storage.SetHardState(st)
  595. storage.Append(entries)
  596. c := &Config{
  597. ID: 1,
  598. ElectionTick: 10,
  599. HeartbeatTick: 1,
  600. Storage: storage,
  601. MaxSizePerMsg: noLimit,
  602. MaxInflightMsgs: 256,
  603. }
  604. n := RestartNode(c)
  605. defer n.Stop()
  606. if g := <-n.Ready(); !reflect.DeepEqual(g, want) {
  607. t.Errorf("g = %+v,\n w %+v", g, want)
  608. }
  609. n.Advance()
  610. select {
  611. case rd := <-n.Ready():
  612. t.Errorf("unexpected Ready: %+v", rd)
  613. case <-time.After(time.Millisecond):
  614. }
  615. }
  616. func TestNodeRestartFromSnapshot(t *testing.T) {
  617. snap := raftpb.Snapshot{
  618. Metadata: raftpb.SnapshotMetadata{
  619. ConfState: raftpb.ConfState{Nodes: []uint64{1, 2}},
  620. Index: 2,
  621. Term: 1,
  622. },
  623. }
  624. entries := []raftpb.Entry{
  625. {Term: 1, Index: 3, Data: []byte("foo")},
  626. }
  627. st := raftpb.HardState{Term: 1, Commit: 3}
  628. want := Ready{
  629. // No HardState is emitted because nothing changed relative to what is
  630. // already persisted.
  631. HardState: raftpb.HardState{},
  632. // commit up to index commit index in st
  633. CommittedEntries: entries,
  634. // MustSync is only true when there is a new HardState or new entries;
  635. // neither is the case here.
  636. MustSync: false,
  637. }
  638. s := NewMemoryStorage()
  639. s.SetHardState(st)
  640. s.ApplySnapshot(snap)
  641. s.Append(entries)
  642. c := &Config{
  643. ID: 1,
  644. ElectionTick: 10,
  645. HeartbeatTick: 1,
  646. Storage: s,
  647. MaxSizePerMsg: noLimit,
  648. MaxInflightMsgs: 256,
  649. }
  650. n := RestartNode(c)
  651. defer n.Stop()
  652. if g := <-n.Ready(); !reflect.DeepEqual(g, want) {
  653. t.Errorf("g = %+v,\n w %+v", g, want)
  654. } else {
  655. n.Advance()
  656. }
  657. select {
  658. case rd := <-n.Ready():
  659. t.Errorf("unexpected Ready: %+v", rd)
  660. case <-time.After(time.Millisecond):
  661. }
  662. }
  663. func TestNodeAdvance(t *testing.T) {
  664. ctx, cancel := context.WithCancel(context.Background())
  665. defer cancel()
  666. storage := NewMemoryStorage()
  667. c := &Config{
  668. ID: 1,
  669. ElectionTick: 10,
  670. HeartbeatTick: 1,
  671. Storage: storage,
  672. MaxSizePerMsg: noLimit,
  673. MaxInflightMsgs: 256,
  674. }
  675. n := StartNode(c, []Peer{{ID: 1}})
  676. defer n.Stop()
  677. rd := <-n.Ready()
  678. storage.Append(rd.Entries)
  679. n.Advance()
  680. n.Campaign(ctx)
  681. <-n.Ready()
  682. n.Propose(ctx, []byte("foo"))
  683. select {
  684. case rd = <-n.Ready():
  685. t.Fatalf("unexpected Ready before Advance: %+v", rd)
  686. case <-time.After(time.Millisecond):
  687. }
  688. storage.Append(rd.Entries)
  689. n.Advance()
  690. select {
  691. case <-n.Ready():
  692. case <-time.After(100 * time.Millisecond):
  693. t.Errorf("expect Ready after Advance, but there is no Ready available")
  694. }
  695. }
  696. func TestSoftStateEqual(t *testing.T) {
  697. tests := []struct {
  698. st *SoftState
  699. we bool
  700. }{
  701. {&SoftState{}, true},
  702. {&SoftState{Lead: 1}, false},
  703. {&SoftState{RaftState: StateLeader}, false},
  704. }
  705. for i, tt := range tests {
  706. if g := tt.st.equal(&SoftState{}); g != tt.we {
  707. t.Errorf("#%d, equal = %v, want %v", i, g, tt.we)
  708. }
  709. }
  710. }
  711. func TestIsHardStateEqual(t *testing.T) {
  712. tests := []struct {
  713. st raftpb.HardState
  714. we bool
  715. }{
  716. {emptyState, true},
  717. {raftpb.HardState{Vote: 1}, false},
  718. {raftpb.HardState{Commit: 1}, false},
  719. {raftpb.HardState{Term: 1}, false},
  720. }
  721. for i, tt := range tests {
  722. if isHardStateEqual(tt.st, emptyState) != tt.we {
  723. t.Errorf("#%d, equal = %v, want %v", i, isHardStateEqual(tt.st, emptyState), tt.we)
  724. }
  725. }
  726. }
  727. func TestNodeProposeAddLearnerNode(t *testing.T) {
  728. ticker := time.NewTicker(time.Millisecond * 100)
  729. defer ticker.Stop()
  730. n := newNode()
  731. s := NewMemoryStorage()
  732. rn := newTestRawNode(1, []uint64{1}, 10, 1, s)
  733. go n.run(rn)
  734. n.Campaign(context.TODO())
  735. stop := make(chan struct{})
  736. done := make(chan struct{})
  737. applyConfChan := make(chan struct{})
  738. go func() {
  739. defer close(done)
  740. for {
  741. select {
  742. case <-stop:
  743. return
  744. case <-ticker.C:
  745. n.Tick()
  746. case rd := <-n.Ready():
  747. s.Append(rd.Entries)
  748. t.Logf("raft: %v", rd.Entries)
  749. for _, ent := range rd.Entries {
  750. if ent.Type != raftpb.EntryConfChange {
  751. continue
  752. }
  753. var cc raftpb.ConfChange
  754. cc.Unmarshal(ent.Data)
  755. state := n.ApplyConfChange(cc)
  756. if len(state.Learners) == 0 ||
  757. state.Learners[0] != cc.NodeID ||
  758. cc.NodeID != 2 {
  759. t.Errorf("apply conf change should return new added learner: %v", state.String())
  760. }
  761. if len(state.Nodes) != 1 {
  762. t.Errorf("add learner should not change the nodes: %v", state.String())
  763. }
  764. t.Logf("apply raft conf %v changed to: %v", cc, state.String())
  765. applyConfChan <- struct{}{}
  766. }
  767. n.Advance()
  768. }
  769. }
  770. }()
  771. cc := raftpb.ConfChange{Type: raftpb.ConfChangeAddLearnerNode, NodeID: 2}
  772. n.ProposeConfChange(context.TODO(), cc)
  773. <-applyConfChan
  774. close(stop)
  775. <-done
  776. }
  777. func TestAppendPagination(t *testing.T) {
  778. const maxSizePerMsg = 2048
  779. n := newNetworkWithConfig(func(c *Config) {
  780. c.MaxSizePerMsg = maxSizePerMsg
  781. }, nil, nil, nil)
  782. seenFullMessage := false
  783. // Inspect all messages to see that we never exceed the limit, but
  784. // we do see messages of larger than half the limit.
  785. n.msgHook = func(m raftpb.Message) bool {
  786. if m.Type == raftpb.MsgApp {
  787. size := 0
  788. for _, e := range m.Entries {
  789. size += len(e.Data)
  790. }
  791. if size > maxSizePerMsg {
  792. t.Errorf("sent MsgApp that is too large: %d bytes", size)
  793. }
  794. if size > maxSizePerMsg/2 {
  795. seenFullMessage = true
  796. }
  797. }
  798. return true
  799. }
  800. n.send(raftpb.Message{From: 1, To: 1, Type: raftpb.MsgHup})
  801. // Partition the network while we make our proposals. This forces
  802. // the entries to be batched into larger messages.
  803. n.isolate(1)
  804. blob := []byte(strings.Repeat("a", 1000))
  805. for i := 0; i < 5; i++ {
  806. n.send(raftpb.Message{From: 1, To: 1, Type: raftpb.MsgProp, Entries: []raftpb.Entry{{Data: blob}}})
  807. }
  808. n.recover()
  809. // After the partition recovers, tick the clock to wake everything
  810. // back up and send the messages.
  811. n.send(raftpb.Message{From: 1, To: 1, Type: raftpb.MsgBeat})
  812. if !seenFullMessage {
  813. t.Error("didn't see any messages more than half the max size; something is wrong with this test")
  814. }
  815. }
  816. func TestCommitPagination(t *testing.T) {
  817. s := NewMemoryStorage()
  818. cfg := newTestConfig(1, []uint64{1}, 10, 1, s)
  819. cfg.MaxCommittedSizePerReady = 2048
  820. rn, err := NewRawNode(cfg)
  821. if err != nil {
  822. t.Fatal(err)
  823. }
  824. n := newNode()
  825. go n.run(rn)
  826. n.Campaign(context.TODO())
  827. rd := readyWithTimeout(&n)
  828. if len(rd.CommittedEntries) != 1 {
  829. t.Fatalf("expected 1 (empty) entry, got %d", len(rd.CommittedEntries))
  830. }
  831. s.Append(rd.Entries)
  832. n.Advance()
  833. blob := []byte(strings.Repeat("a", 1000))
  834. for i := 0; i < 3; i++ {
  835. if err := n.Propose(context.TODO(), blob); err != nil {
  836. t.Fatal(err)
  837. }
  838. }
  839. // The 3 proposals will commit in two batches.
  840. rd = readyWithTimeout(&n)
  841. if len(rd.CommittedEntries) != 2 {
  842. t.Fatalf("expected 2 entries in first batch, got %d", len(rd.CommittedEntries))
  843. }
  844. s.Append(rd.Entries)
  845. n.Advance()
  846. rd = readyWithTimeout(&n)
  847. if len(rd.CommittedEntries) != 1 {
  848. t.Fatalf("expected 1 entry in second batch, got %d", len(rd.CommittedEntries))
  849. }
  850. s.Append(rd.Entries)
  851. n.Advance()
  852. }
  853. type ignoreSizeHintMemStorage struct {
  854. *MemoryStorage
  855. }
  856. func (s *ignoreSizeHintMemStorage) Entries(lo, hi uint64, maxSize uint64) ([]raftpb.Entry, error) {
  857. return s.MemoryStorage.Entries(lo, hi, math.MaxUint64)
  858. }
  859. // TestNodeCommitPaginationAfterRestart regression tests a scenario in which the
  860. // Storage's Entries size limitation is slightly more permissive than Raft's
  861. // internal one. The original bug was the following:
  862. //
  863. // - node learns that index 11 (or 100, doesn't matter) is committed
  864. // - nextEnts returns index 1..10 in CommittedEntries due to size limiting. However,
  865. // index 10 already exceeds maxBytes, due to a user-provided impl of Entries.
  866. // - Commit index gets bumped to 10
  867. // - the node persists the HardState, but crashes before applying the entries
  868. // - upon restart, the storage returns the same entries, but `slice` takes a different code path
  869. // (since it is now called with an upper bound of 10) and removes the last entry.
  870. // - Raft emits a HardState with a regressing commit index.
  871. //
  872. // A simpler version of this test would have the storage return a lot less entries than dictated
  873. // by maxSize (for example, exactly one entry) after the restart, resulting in a larger regression.
  874. // This wouldn't need to exploit anything about Raft-internal code paths to fail.
  875. func TestNodeCommitPaginationAfterRestart(t *testing.T) {
  876. s := &ignoreSizeHintMemStorage{
  877. MemoryStorage: NewMemoryStorage(),
  878. }
  879. persistedHardState := raftpb.HardState{
  880. Term: 1,
  881. Vote: 1,
  882. Commit: 10,
  883. }
  884. s.hardState = persistedHardState
  885. s.ents = make([]raftpb.Entry, 10)
  886. var size uint64
  887. for i := range s.ents {
  888. ent := raftpb.Entry{
  889. Term: 1,
  890. Index: uint64(i + 1),
  891. Type: raftpb.EntryNormal,
  892. Data: []byte("a"),
  893. }
  894. s.ents[i] = ent
  895. size += uint64(ent.Size())
  896. }
  897. cfg := newTestConfig(1, []uint64{1}, 10, 1, s)
  898. // Set a MaxSizePerMsg that would suggest to Raft that the last committed entry should
  899. // not be included in the initial rd.CommittedEntries. However, our storage will ignore
  900. // this and *will* return it (which is how the Commit index ended up being 10 initially).
  901. cfg.MaxSizePerMsg = size - uint64(s.ents[len(s.ents)-1].Size()) - 1
  902. rn, err := NewRawNode(cfg)
  903. if err != nil {
  904. t.Fatal(err)
  905. }
  906. n := newNode()
  907. go n.run(rn)
  908. defer n.Stop()
  909. rd := readyWithTimeout(&n)
  910. if !IsEmptyHardState(rd.HardState) && rd.HardState.Commit < persistedHardState.Commit {
  911. t.Errorf("HardState regressed: Commit %d -> %d\nCommitting:\n%+v",
  912. persistedHardState.Commit, rd.HardState.Commit,
  913. DescribeEntries(rd.CommittedEntries, func(data []byte) string { return fmt.Sprintf("%q", data) }),
  914. )
  915. }
  916. }