rawnode_test.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  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. "reflect"
  18. "testing"
  19. "go.etcd.io/etcd/raft/raftpb"
  20. )
  21. // TestRawNodeStep ensures that RawNode.Step ignore local message.
  22. func TestRawNodeStep(t *testing.T) {
  23. for i, msgn := range raftpb.MessageType_name {
  24. s := NewMemoryStorage()
  25. rawNode, err := NewRawNode(newTestConfig(1, nil, 10, 1, s), []Peer{{ID: 1}})
  26. if err != nil {
  27. t.Fatal(err)
  28. }
  29. msgt := raftpb.MessageType(i)
  30. err = rawNode.Step(raftpb.Message{Type: msgt})
  31. // LocalMsg should be ignored.
  32. if IsLocalMsg(msgt) {
  33. if err != ErrStepLocalMsg {
  34. t.Errorf("%d: step should ignore %s", msgt, msgn)
  35. }
  36. }
  37. }
  38. }
  39. // TestNodeStepUnblock from node_test.go has no equivalent in rawNode because there is
  40. // no goroutine in RawNode.
  41. // TestRawNodeProposeAndConfChange ensures that RawNode.Propose and RawNode.ProposeConfChange
  42. // send the given proposal and ConfChange to the underlying raft.
  43. func TestRawNodeProposeAndConfChange(t *testing.T) {
  44. s := NewMemoryStorage()
  45. var err error
  46. rawNode, err := NewRawNode(newTestConfig(1, nil, 10, 1, s), []Peer{{ID: 1}})
  47. if err != nil {
  48. t.Fatal(err)
  49. }
  50. rd := rawNode.Ready()
  51. s.Append(rd.Entries)
  52. rawNode.Advance(rd)
  53. if d := rawNode.Ready(); d.MustSync || !IsEmptyHardState(d.HardState) || len(d.Entries) > 0 {
  54. t.Fatalf("expected empty hard state with must-sync=false: %#v", d)
  55. }
  56. rawNode.Campaign()
  57. proposed := false
  58. var (
  59. lastIndex uint64
  60. ccdata []byte
  61. )
  62. for {
  63. rd = rawNode.Ready()
  64. s.Append(rd.Entries)
  65. // Once we are the leader, propose a command and a ConfChange.
  66. if !proposed && rd.SoftState.Lead == rawNode.raft.id {
  67. rawNode.Propose([]byte("somedata"))
  68. cc := raftpb.ConfChange{Type: raftpb.ConfChangeAddNode, NodeID: 1}
  69. ccdata, err = cc.Marshal()
  70. if err != nil {
  71. t.Fatal(err)
  72. }
  73. rawNode.ProposeConfChange(cc)
  74. proposed = true
  75. }
  76. rawNode.Advance(rd)
  77. // Exit when we have four entries: one ConfChange, one no-op for the election,
  78. // our proposed command and proposed ConfChange.
  79. lastIndex, err = s.LastIndex()
  80. if err != nil {
  81. t.Fatal(err)
  82. }
  83. if lastIndex >= 4 {
  84. break
  85. }
  86. }
  87. entries, err := s.Entries(lastIndex-1, lastIndex+1, noLimit)
  88. if err != nil {
  89. t.Fatal(err)
  90. }
  91. if len(entries) != 2 {
  92. t.Fatalf("len(entries) = %d, want %d", len(entries), 2)
  93. }
  94. if !bytes.Equal(entries[0].Data, []byte("somedata")) {
  95. t.Errorf("entries[0].Data = %v, want %v", entries[0].Data, []byte("somedata"))
  96. }
  97. if entries[1].Type != raftpb.EntryConfChange {
  98. t.Fatalf("type = %v, want %v", entries[1].Type, raftpb.EntryConfChange)
  99. }
  100. if !bytes.Equal(entries[1].Data, ccdata) {
  101. t.Errorf("data = %v, want %v", entries[1].Data, ccdata)
  102. }
  103. }
  104. // TestRawNodeProposeAddDuplicateNode ensures that two proposes to add the same node should
  105. // not affect the later propose to add new node.
  106. func TestRawNodeProposeAddDuplicateNode(t *testing.T) {
  107. s := NewMemoryStorage()
  108. rawNode, err := NewRawNode(newTestConfig(1, nil, 10, 1, s), []Peer{{ID: 1}})
  109. if err != nil {
  110. t.Fatal(err)
  111. }
  112. rd := rawNode.Ready()
  113. s.Append(rd.Entries)
  114. rawNode.Advance(rd)
  115. rawNode.Campaign()
  116. for {
  117. rd = rawNode.Ready()
  118. s.Append(rd.Entries)
  119. if rd.SoftState.Lead == rawNode.raft.id {
  120. rawNode.Advance(rd)
  121. break
  122. }
  123. rawNode.Advance(rd)
  124. }
  125. proposeConfChangeAndApply := func(cc raftpb.ConfChange) {
  126. rawNode.ProposeConfChange(cc)
  127. rd = rawNode.Ready()
  128. s.Append(rd.Entries)
  129. for _, entry := range rd.CommittedEntries {
  130. if entry.Type == raftpb.EntryConfChange {
  131. var cc raftpb.ConfChange
  132. cc.Unmarshal(entry.Data)
  133. rawNode.ApplyConfChange(cc)
  134. }
  135. }
  136. rawNode.Advance(rd)
  137. }
  138. cc1 := raftpb.ConfChange{Type: raftpb.ConfChangeAddNode, NodeID: 1}
  139. ccdata1, err := cc1.Marshal()
  140. if err != nil {
  141. t.Fatal(err)
  142. }
  143. proposeConfChangeAndApply(cc1)
  144. // try to add the same node again
  145. proposeConfChangeAndApply(cc1)
  146. // the new node join should be ok
  147. cc2 := raftpb.ConfChange{Type: raftpb.ConfChangeAddNode, NodeID: 2}
  148. ccdata2, err := cc2.Marshal()
  149. if err != nil {
  150. t.Fatal(err)
  151. }
  152. proposeConfChangeAndApply(cc2)
  153. lastIndex, err := s.LastIndex()
  154. if err != nil {
  155. t.Fatal(err)
  156. }
  157. // the last three entries should be: ConfChange cc1, cc1, cc2
  158. entries, err := s.Entries(lastIndex-2, lastIndex+1, noLimit)
  159. if err != nil {
  160. t.Fatal(err)
  161. }
  162. if len(entries) != 3 {
  163. t.Fatalf("len(entries) = %d, want %d", len(entries), 3)
  164. }
  165. if !bytes.Equal(entries[0].Data, ccdata1) {
  166. t.Errorf("entries[0].Data = %v, want %v", entries[0].Data, ccdata1)
  167. }
  168. if !bytes.Equal(entries[2].Data, ccdata2) {
  169. t.Errorf("entries[2].Data = %v, want %v", entries[2].Data, ccdata2)
  170. }
  171. }
  172. // TestRawNodeReadIndex ensures that Rawnode.ReadIndex sends the MsgReadIndex message
  173. // to the underlying raft. It also ensures that ReadState can be read out.
  174. func TestRawNodeReadIndex(t *testing.T) {
  175. msgs := []raftpb.Message{}
  176. appendStep := func(r *raft, m raftpb.Message) error {
  177. msgs = append(msgs, m)
  178. return nil
  179. }
  180. wrs := []ReadState{{Index: uint64(1), RequestCtx: []byte("somedata")}}
  181. s := NewMemoryStorage()
  182. c := newTestConfig(1, nil, 10, 1, s)
  183. rawNode, err := NewRawNode(c, []Peer{{ID: 1}})
  184. if err != nil {
  185. t.Fatal(err)
  186. }
  187. rawNode.raft.readStates = wrs
  188. // ensure the ReadStates can be read out
  189. hasReady := rawNode.HasReady()
  190. if !hasReady {
  191. t.Errorf("HasReady() returns %t, want %t", hasReady, true)
  192. }
  193. rd := rawNode.Ready()
  194. if !reflect.DeepEqual(rd.ReadStates, wrs) {
  195. t.Errorf("ReadStates = %d, want %d", rd.ReadStates, wrs)
  196. }
  197. s.Append(rd.Entries)
  198. rawNode.Advance(rd)
  199. // ensure raft.readStates is reset after advance
  200. if rawNode.raft.readStates != nil {
  201. t.Errorf("readStates = %v, want %v", rawNode.raft.readStates, nil)
  202. }
  203. wrequestCtx := []byte("somedata2")
  204. rawNode.Campaign()
  205. for {
  206. rd = rawNode.Ready()
  207. s.Append(rd.Entries)
  208. if rd.SoftState.Lead == rawNode.raft.id {
  209. rawNode.Advance(rd)
  210. // Once we are the leader, issue a ReadIndex request
  211. rawNode.raft.step = appendStep
  212. rawNode.ReadIndex(wrequestCtx)
  213. break
  214. }
  215. rawNode.Advance(rd)
  216. }
  217. // ensure that MsgReadIndex message is sent to the underlying raft
  218. if len(msgs) != 1 {
  219. t.Fatalf("len(msgs) = %d, want %d", len(msgs), 1)
  220. }
  221. if msgs[0].Type != raftpb.MsgReadIndex {
  222. t.Errorf("msg type = %d, want %d", msgs[0].Type, raftpb.MsgReadIndex)
  223. }
  224. if !bytes.Equal(msgs[0].Entries[0].Data, wrequestCtx) {
  225. t.Errorf("data = %v, want %v", msgs[0].Entries[0].Data, wrequestCtx)
  226. }
  227. }
  228. // TestBlockProposal from node_test.go has no equivalent in rawNode because there is
  229. // no leader check in RawNode.
  230. // TestNodeTick from node_test.go has no equivalent in rawNode because
  231. // it reaches into the raft object which is not exposed.
  232. // TestNodeStop from node_test.go has no equivalent in rawNode because there is
  233. // no goroutine in RawNode.
  234. // TestRawNodeStart ensures that a node can be started correctly. The node should
  235. // start with correct configuration change entries, and can accept and commit
  236. // proposals.
  237. func TestRawNodeStart(t *testing.T) {
  238. cc := raftpb.ConfChange{Type: raftpb.ConfChangeAddNode, NodeID: 1}
  239. ccdata, err := cc.Marshal()
  240. if err != nil {
  241. t.Fatalf("unexpected marshal error: %v", err)
  242. }
  243. wants := []Ready{
  244. {
  245. HardState: raftpb.HardState{Term: 1, Commit: 1, Vote: 0},
  246. Entries: []raftpb.Entry{
  247. {Type: raftpb.EntryConfChange, Term: 1, Index: 1, Data: ccdata},
  248. },
  249. CommittedEntries: []raftpb.Entry{
  250. {Type: raftpb.EntryConfChange, Term: 1, Index: 1, Data: ccdata},
  251. },
  252. MustSync: true,
  253. },
  254. {
  255. HardState: raftpb.HardState{Term: 2, Commit: 3, Vote: 1},
  256. Entries: []raftpb.Entry{{Term: 2, Index: 3, Data: []byte("foo")}},
  257. CommittedEntries: []raftpb.Entry{{Term: 2, Index: 3, Data: []byte("foo")}},
  258. MustSync: true,
  259. },
  260. }
  261. storage := NewMemoryStorage()
  262. rawNode, err := NewRawNode(newTestConfig(1, nil, 10, 1, storage), []Peer{{ID: 1}})
  263. if err != nil {
  264. t.Fatal(err)
  265. }
  266. rd := rawNode.Ready()
  267. t.Logf("rd %v", rd)
  268. if !reflect.DeepEqual(rd, wants[0]) {
  269. t.Fatalf("#%d: g = %+v,\n w %+v", 1, rd, wants[0])
  270. } else {
  271. storage.Append(rd.Entries)
  272. rawNode.Advance(rd)
  273. }
  274. storage.Append(rd.Entries)
  275. rawNode.Advance(rd)
  276. rawNode.Campaign()
  277. rd = rawNode.Ready()
  278. storage.Append(rd.Entries)
  279. rawNode.Advance(rd)
  280. rawNode.Propose([]byte("foo"))
  281. if rd = rawNode.Ready(); !reflect.DeepEqual(rd, wants[1]) {
  282. t.Errorf("#%d: g = %+v,\n w %+v", 2, rd, wants[1])
  283. } else {
  284. storage.Append(rd.Entries)
  285. rawNode.Advance(rd)
  286. }
  287. if rawNode.HasReady() {
  288. t.Errorf("unexpected Ready: %+v", rawNode.Ready())
  289. }
  290. }
  291. func TestRawNodeRestart(t *testing.T) {
  292. entries := []raftpb.Entry{
  293. {Term: 1, Index: 1},
  294. {Term: 1, Index: 2, Data: []byte("foo")},
  295. }
  296. st := raftpb.HardState{Term: 1, Commit: 1}
  297. want := Ready{
  298. HardState: emptyState,
  299. // commit up to commit index in st
  300. CommittedEntries: entries[:st.Commit],
  301. MustSync: false,
  302. }
  303. storage := NewMemoryStorage()
  304. storage.SetHardState(st)
  305. storage.Append(entries)
  306. rawNode, err := NewRawNode(newTestConfig(1, nil, 10, 1, storage), nil)
  307. if err != nil {
  308. t.Fatal(err)
  309. }
  310. rd := rawNode.Ready()
  311. if !reflect.DeepEqual(rd, want) {
  312. t.Errorf("g = %+v,\n w %+v", rd, want)
  313. }
  314. rawNode.Advance(rd)
  315. if rawNode.HasReady() {
  316. t.Errorf("unexpected Ready: %+v", rawNode.Ready())
  317. }
  318. }
  319. func TestRawNodeRestartFromSnapshot(t *testing.T) {
  320. snap := raftpb.Snapshot{
  321. Metadata: raftpb.SnapshotMetadata{
  322. ConfState: raftpb.ConfState{Nodes: []uint64{1, 2}},
  323. Index: 2,
  324. Term: 1,
  325. },
  326. }
  327. entries := []raftpb.Entry{
  328. {Term: 1, Index: 3, Data: []byte("foo")},
  329. }
  330. st := raftpb.HardState{Term: 1, Commit: 3}
  331. want := Ready{
  332. HardState: emptyState,
  333. // commit up to commit index in st
  334. CommittedEntries: entries,
  335. MustSync: false,
  336. }
  337. s := NewMemoryStorage()
  338. s.SetHardState(st)
  339. s.ApplySnapshot(snap)
  340. s.Append(entries)
  341. rawNode, err := NewRawNode(newTestConfig(1, nil, 10, 1, s), nil)
  342. if err != nil {
  343. t.Fatal(err)
  344. }
  345. if rd := rawNode.Ready(); !reflect.DeepEqual(rd, want) {
  346. t.Errorf("g = %+v,\n w %+v", rd, want)
  347. } else {
  348. rawNode.Advance(rd)
  349. }
  350. if rawNode.HasReady() {
  351. t.Errorf("unexpected Ready: %+v", rawNode.HasReady())
  352. }
  353. }
  354. // TestNodeAdvance from node_test.go has no equivalent in rawNode because there is
  355. // no dependency check between Ready() and Advance()
  356. func TestRawNodeStatus(t *testing.T) {
  357. storage := NewMemoryStorage()
  358. rawNode, err := NewRawNode(newTestConfig(1, nil, 10, 1, storage), []Peer{{ID: 1}})
  359. if err != nil {
  360. t.Fatal(err)
  361. }
  362. status := rawNode.Status()
  363. if status == nil {
  364. t.Errorf("expected status struct, got nil")
  365. }
  366. }
  367. // TestRawNodeCommitPaginationAfterRestart is the RawNode version of
  368. // TestNodeCommitPaginationAfterRestart. The anomaly here was even worse as the
  369. // Raft group would forget to apply entries:
  370. //
  371. // - node learns that index 11 is committed
  372. // - nextEnts returns index 1..10 in CommittedEntries (but index 10 already
  373. // exceeds maxBytes), which isn't noticed internally by Raft
  374. // - Commit index gets bumped to 10
  375. // - the node persists the HardState, but crashes before applying the entries
  376. // - upon restart, the storage returns the same entries, but `slice` takes a
  377. // different code path and removes the last entry.
  378. // - Raft does not emit a HardState, but when the app calls Advance(), it bumps
  379. // its internal applied index cursor to 10 (when it should be 9)
  380. // - the next Ready asks the app to apply index 11 (omitting index 10), losing a
  381. // write.
  382. func TestRawNodeCommitPaginationAfterRestart(t *testing.T) {
  383. s := &ignoreSizeHintMemStorage{
  384. MemoryStorage: NewMemoryStorage(),
  385. }
  386. persistedHardState := raftpb.HardState{
  387. Term: 1,
  388. Vote: 1,
  389. Commit: 10,
  390. }
  391. s.hardState = persistedHardState
  392. s.ents = make([]raftpb.Entry, 10)
  393. var size uint64
  394. for i := range s.ents {
  395. ent := raftpb.Entry{
  396. Term: 1,
  397. Index: uint64(i + 1),
  398. Type: raftpb.EntryNormal,
  399. Data: []byte("a"),
  400. }
  401. s.ents[i] = ent
  402. size += uint64(ent.Size())
  403. }
  404. cfg := newTestConfig(1, []uint64{1}, 10, 1, s)
  405. // Set a MaxSizePerMsg that would suggest to Raft that the last committed entry should
  406. // not be included in the initial rd.CommittedEntries. However, our storage will ignore
  407. // this and *will* return it (which is how the Commit index ended up being 10 initially).
  408. cfg.MaxSizePerMsg = size - uint64(s.ents[len(s.ents)-1].Size()) - 1
  409. s.ents = append(s.ents, raftpb.Entry{
  410. Term: 1,
  411. Index: uint64(11),
  412. Type: raftpb.EntryNormal,
  413. Data: []byte("boom"),
  414. })
  415. rawNode, err := NewRawNode(cfg, []Peer{{ID: 1}})
  416. if err != nil {
  417. t.Fatal(err)
  418. }
  419. for highestApplied := uint64(0); highestApplied != 11; {
  420. rd := rawNode.Ready()
  421. n := len(rd.CommittedEntries)
  422. if n == 0 {
  423. t.Fatalf("stopped applying entries at index %d", highestApplied)
  424. }
  425. if next := rd.CommittedEntries[0].Index; highestApplied != 0 && highestApplied+1 != next {
  426. t.Fatalf("attempting to apply index %d after index %d, leaving a gap", next, highestApplied)
  427. }
  428. highestApplied = rd.CommittedEntries[n-1].Index
  429. rawNode.Advance(rd)
  430. rawNode.Step(raftpb.Message{
  431. Type: raftpb.MsgHeartbeat,
  432. To: 1,
  433. From: 1, // illegal, but we get away with it
  434. Term: 1,
  435. Commit: 11,
  436. })
  437. }
  438. }
  439. // TestRawNodeBoundedLogGrowthWithPartition tests a scenario where a leader is
  440. // partitioned from a quorum of nodes. It verifies that the leader's log is
  441. // protected from unbounded growth even as new entries continue to be proposed.
  442. // This protection is provided by the MaxUncommittedEntriesSize configuration.
  443. func TestRawNodeBoundedLogGrowthWithPartition(t *testing.T) {
  444. const maxEntries = 16
  445. data := []byte("testdata")
  446. testEntry := raftpb.Entry{Data: data}
  447. maxEntrySize := uint64(maxEntries * PayloadSize(testEntry))
  448. s := NewMemoryStorage()
  449. cfg := newTestConfig(1, []uint64{1}, 10, 1, s)
  450. cfg.MaxUncommittedEntriesSize = maxEntrySize
  451. rawNode, err := NewRawNode(cfg, []Peer{{ID: 1}})
  452. if err != nil {
  453. t.Fatal(err)
  454. }
  455. rd := rawNode.Ready()
  456. s.Append(rd.Entries)
  457. rawNode.Advance(rd)
  458. // Become the leader.
  459. rawNode.Campaign()
  460. for {
  461. rd = rawNode.Ready()
  462. s.Append(rd.Entries)
  463. if rd.SoftState.Lead == rawNode.raft.id {
  464. rawNode.Advance(rd)
  465. break
  466. }
  467. rawNode.Advance(rd)
  468. }
  469. // Simulate a network partition while we make our proposals by never
  470. // committing anything. These proposals should not cause the leader's
  471. // log to grow indefinitely.
  472. for i := 0; i < 1024; i++ {
  473. rawNode.Propose(data)
  474. }
  475. // Check the size of leader's uncommitted log tail. It should not exceed the
  476. // MaxUncommittedEntriesSize limit.
  477. checkUncommitted := func(exp uint64) {
  478. t.Helper()
  479. if a := rawNode.raft.uncommittedSize; exp != a {
  480. t.Fatalf("expected %d uncommitted entry bytes, found %d", exp, a)
  481. }
  482. }
  483. checkUncommitted(maxEntrySize)
  484. // Recover from the partition. The uncommitted tail of the Raft log should
  485. // disappear as entries are committed.
  486. rd = rawNode.Ready()
  487. if len(rd.CommittedEntries) != maxEntries {
  488. t.Fatalf("expected %d entries, got %d", maxEntries, len(rd.CommittedEntries))
  489. }
  490. s.Append(rd.Entries)
  491. rawNode.Advance(rd)
  492. checkUncommitted(0)
  493. }