rawnode_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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. rawNode.Campaign()
  54. proposed := false
  55. var (
  56. lastIndex uint64
  57. ccdata []byte
  58. )
  59. for {
  60. rd = rawNode.Ready()
  61. s.Append(rd.Entries)
  62. // Once we are the leader, propose a command and a ConfChange.
  63. if !proposed && rd.SoftState.Lead == rawNode.raft.id {
  64. rawNode.Propose([]byte("somedata"))
  65. cc := raftpb.ConfChange{Type: raftpb.ConfChangeAddNode, NodeID: 1}
  66. ccdata, err = cc.Marshal()
  67. if err != nil {
  68. t.Fatal(err)
  69. }
  70. rawNode.ProposeConfChange(cc)
  71. proposed = true
  72. }
  73. rawNode.Advance(rd)
  74. // Exit when we have four entries: one ConfChange, one no-op for the election,
  75. // our proposed command and proposed ConfChange.
  76. lastIndex, err = s.LastIndex()
  77. if err != nil {
  78. t.Fatal(err)
  79. }
  80. if lastIndex >= 4 {
  81. break
  82. }
  83. }
  84. entries, err := s.Entries(lastIndex-1, lastIndex+1, noLimit)
  85. if err != nil {
  86. t.Fatal(err)
  87. }
  88. if len(entries) != 2 {
  89. t.Fatalf("len(entries) = %d, want %d", len(entries), 2)
  90. }
  91. if !bytes.Equal(entries[0].Data, []byte("somedata")) {
  92. t.Errorf("entries[0].Data = %v, want %v", entries[0].Data, []byte("somedata"))
  93. }
  94. if entries[1].Type != raftpb.EntryConfChange {
  95. t.Fatalf("type = %v, want %v", entries[1].Type, raftpb.EntryConfChange)
  96. }
  97. if !bytes.Equal(entries[1].Data, ccdata) {
  98. t.Errorf("data = %v, want %v", entries[1].Data, ccdata)
  99. }
  100. }
  101. // TestRawNodeProposeAddDuplicateNode ensures that two proposes to add the same node should
  102. // not affect the later propose to add new node.
  103. func TestRawNodeProposeAddDuplicateNode(t *testing.T) {
  104. s := NewMemoryStorage()
  105. rawNode, err := NewRawNode(newTestConfig(1, nil, 10, 1, s), []Peer{{ID: 1}})
  106. if err != nil {
  107. t.Fatal(err)
  108. }
  109. rd := rawNode.Ready()
  110. s.Append(rd.Entries)
  111. rawNode.Advance(rd)
  112. rawNode.Campaign()
  113. for {
  114. rd = rawNode.Ready()
  115. s.Append(rd.Entries)
  116. if rd.SoftState.Lead == rawNode.raft.id {
  117. rawNode.Advance(rd)
  118. break
  119. }
  120. rawNode.Advance(rd)
  121. }
  122. proposeConfChangeAndApply := func(cc raftpb.ConfChange) {
  123. rawNode.ProposeConfChange(cc)
  124. rd = rawNode.Ready()
  125. s.Append(rd.Entries)
  126. for _, entry := range rd.CommittedEntries {
  127. if entry.Type == raftpb.EntryConfChange {
  128. var cc raftpb.ConfChange
  129. cc.Unmarshal(entry.Data)
  130. rawNode.ApplyConfChange(cc)
  131. }
  132. }
  133. rawNode.Advance(rd)
  134. }
  135. cc1 := raftpb.ConfChange{Type: raftpb.ConfChangeAddNode, NodeID: 1}
  136. ccdata1, err := cc1.Marshal()
  137. if err != nil {
  138. t.Fatal(err)
  139. }
  140. proposeConfChangeAndApply(cc1)
  141. // try to add the same node again
  142. proposeConfChangeAndApply(cc1)
  143. // the new node join should be ok
  144. cc2 := raftpb.ConfChange{Type: raftpb.ConfChangeAddNode, NodeID: 2}
  145. ccdata2, err := cc2.Marshal()
  146. if err != nil {
  147. t.Fatal(err)
  148. }
  149. proposeConfChangeAndApply(cc2)
  150. lastIndex, err := s.LastIndex()
  151. if err != nil {
  152. t.Fatal(err)
  153. }
  154. // the last three entries should be: ConfChange cc1, cc1, cc2
  155. entries, err := s.Entries(lastIndex-2, lastIndex+1, noLimit)
  156. if err != nil {
  157. t.Fatal(err)
  158. }
  159. if len(entries) != 3 {
  160. t.Fatalf("len(entries) = %d, want %d", len(entries), 3)
  161. }
  162. if !bytes.Equal(entries[0].Data, ccdata1) {
  163. t.Errorf("entries[0].Data = %v, want %v", entries[0].Data, ccdata1)
  164. }
  165. if !bytes.Equal(entries[2].Data, ccdata2) {
  166. t.Errorf("entries[2].Data = %v, want %v", entries[2].Data, ccdata2)
  167. }
  168. }
  169. // TestRawNodeReadIndex ensures that Rawnode.ReadIndex sends the MsgReadIndex message
  170. // to the underlying raft. It also ensures that ReadState can be read out.
  171. func TestRawNodeReadIndex(t *testing.T) {
  172. msgs := []raftpb.Message{}
  173. appendStep := func(r *raft, m raftpb.Message) error {
  174. msgs = append(msgs, m)
  175. return nil
  176. }
  177. wrs := []ReadState{{Index: uint64(1), RequestCtx: []byte("somedata")}}
  178. s := NewMemoryStorage()
  179. c := newTestConfig(1, nil, 10, 1, s)
  180. rawNode, err := NewRawNode(c, []Peer{{ID: 1}})
  181. if err != nil {
  182. t.Fatal(err)
  183. }
  184. rawNode.raft.readStates = wrs
  185. // ensure the ReadStates can be read out
  186. hasReady := rawNode.HasReady()
  187. if !hasReady {
  188. t.Errorf("HasReady() returns %t, want %t", hasReady, true)
  189. }
  190. rd := rawNode.Ready()
  191. if !reflect.DeepEqual(rd.ReadStates, wrs) {
  192. t.Errorf("ReadStates = %d, want %d", rd.ReadStates, wrs)
  193. }
  194. s.Append(rd.Entries)
  195. rawNode.Advance(rd)
  196. // ensure raft.readStates is reset after advance
  197. if rawNode.raft.readStates != nil {
  198. t.Errorf("readStates = %v, want %v", rawNode.raft.readStates, nil)
  199. }
  200. wrequestCtx := []byte("somedata2")
  201. rawNode.Campaign()
  202. for {
  203. rd = rawNode.Ready()
  204. s.Append(rd.Entries)
  205. if rd.SoftState.Lead == rawNode.raft.id {
  206. rawNode.Advance(rd)
  207. // Once we are the leader, issue a ReadIndex request
  208. rawNode.raft.step = appendStep
  209. rawNode.ReadIndex(wrequestCtx)
  210. break
  211. }
  212. rawNode.Advance(rd)
  213. }
  214. // ensure that MsgReadIndex message is sent to the underlying raft
  215. if len(msgs) != 1 {
  216. t.Fatalf("len(msgs) = %d, want %d", len(msgs), 1)
  217. }
  218. if msgs[0].Type != raftpb.MsgReadIndex {
  219. t.Errorf("msg type = %d, want %d", msgs[0].Type, raftpb.MsgReadIndex)
  220. }
  221. if !bytes.Equal(msgs[0].Entries[0].Data, wrequestCtx) {
  222. t.Errorf("data = %v, want %v", msgs[0].Entries[0].Data, wrequestCtx)
  223. }
  224. }
  225. // TestBlockProposal from node_test.go has no equivalent in rawNode because there is
  226. // no leader check in RawNode.
  227. // TestNodeTick from node_test.go has no equivalent in rawNode because
  228. // it reaches into the raft object which is not exposed.
  229. // TestNodeStop from node_test.go has no equivalent in rawNode because there is
  230. // no goroutine in RawNode.
  231. // TestRawNodeStart ensures that a node can be started correctly. The node should
  232. // start with correct configuration change entries, and can accept and commit
  233. // proposals.
  234. func TestRawNodeStart(t *testing.T) {
  235. cc := raftpb.ConfChange{Type: raftpb.ConfChangeAddNode, NodeID: 1}
  236. ccdata, err := cc.Marshal()
  237. if err != nil {
  238. t.Fatalf("unexpected marshal error: %v", err)
  239. }
  240. wants := []Ready{
  241. {
  242. HardState: raftpb.HardState{Term: 1, Commit: 1, Vote: 0},
  243. Entries: []raftpb.Entry{
  244. {Type: raftpb.EntryConfChange, Term: 1, Index: 1, Data: ccdata},
  245. },
  246. CommittedEntries: []raftpb.Entry{
  247. {Type: raftpb.EntryConfChange, Term: 1, Index: 1, Data: ccdata},
  248. },
  249. MustSync: true,
  250. },
  251. {
  252. HardState: raftpb.HardState{Term: 2, Commit: 3, Vote: 1},
  253. Entries: []raftpb.Entry{{Term: 2, Index: 3, Data: []byte("foo")}},
  254. CommittedEntries: []raftpb.Entry{{Term: 2, Index: 3, Data: []byte("foo")}},
  255. MustSync: true,
  256. },
  257. }
  258. storage := NewMemoryStorage()
  259. rawNode, err := NewRawNode(newTestConfig(1, nil, 10, 1, storage), []Peer{{ID: 1}})
  260. if err != nil {
  261. t.Fatal(err)
  262. }
  263. rd := rawNode.Ready()
  264. t.Logf("rd %v", rd)
  265. if !reflect.DeepEqual(rd, wants[0]) {
  266. t.Fatalf("#%d: g = %+v,\n w %+v", 1, rd, wants[0])
  267. } else {
  268. storage.Append(rd.Entries)
  269. rawNode.Advance(rd)
  270. }
  271. storage.Append(rd.Entries)
  272. rawNode.Advance(rd)
  273. rawNode.Campaign()
  274. rd = rawNode.Ready()
  275. storage.Append(rd.Entries)
  276. rawNode.Advance(rd)
  277. rawNode.Propose([]byte("foo"))
  278. if rd = rawNode.Ready(); !reflect.DeepEqual(rd, wants[1]) {
  279. t.Errorf("#%d: g = %+v,\n w %+v", 2, rd, wants[1])
  280. } else {
  281. storage.Append(rd.Entries)
  282. rawNode.Advance(rd)
  283. }
  284. if rawNode.HasReady() {
  285. t.Errorf("unexpected Ready: %+v", rawNode.Ready())
  286. }
  287. }
  288. func TestRawNodeRestart(t *testing.T) {
  289. entries := []raftpb.Entry{
  290. {Term: 1, Index: 1},
  291. {Term: 1, Index: 2, Data: []byte("foo")},
  292. }
  293. st := raftpb.HardState{Term: 1, Commit: 1}
  294. want := Ready{
  295. HardState: emptyState,
  296. // commit up to commit index in st
  297. CommittedEntries: entries[:st.Commit],
  298. MustSync: true,
  299. }
  300. storage := NewMemoryStorage()
  301. storage.SetHardState(st)
  302. storage.Append(entries)
  303. rawNode, err := NewRawNode(newTestConfig(1, nil, 10, 1, storage), nil)
  304. if err != nil {
  305. t.Fatal(err)
  306. }
  307. rd := rawNode.Ready()
  308. if !reflect.DeepEqual(rd, want) {
  309. t.Errorf("g = %+v,\n w %+v", rd, want)
  310. }
  311. rawNode.Advance(rd)
  312. if rawNode.HasReady() {
  313. t.Errorf("unexpected Ready: %+v", rawNode.Ready())
  314. }
  315. }
  316. func TestRawNodeRestartFromSnapshot(t *testing.T) {
  317. snap := raftpb.Snapshot{
  318. Metadata: raftpb.SnapshotMetadata{
  319. ConfState: raftpb.ConfState{Nodes: []uint64{1, 2}},
  320. Index: 2,
  321. Term: 1,
  322. },
  323. }
  324. entries := []raftpb.Entry{
  325. {Term: 1, Index: 3, Data: []byte("foo")},
  326. }
  327. st := raftpb.HardState{Term: 1, Commit: 3}
  328. want := Ready{
  329. HardState: emptyState,
  330. // commit up to commit index in st
  331. CommittedEntries: entries,
  332. MustSync: true,
  333. }
  334. s := NewMemoryStorage()
  335. s.SetHardState(st)
  336. s.ApplySnapshot(snap)
  337. s.Append(entries)
  338. rawNode, err := NewRawNode(newTestConfig(1, nil, 10, 1, s), nil)
  339. if err != nil {
  340. t.Fatal(err)
  341. }
  342. if rd := rawNode.Ready(); !reflect.DeepEqual(rd, want) {
  343. t.Errorf("g = %+v,\n w %+v", rd, want)
  344. } else {
  345. rawNode.Advance(rd)
  346. }
  347. if rawNode.HasReady() {
  348. t.Errorf("unexpected Ready: %+v", rawNode.HasReady())
  349. }
  350. }
  351. // TestNodeAdvance from node_test.go has no equivalent in rawNode because there is
  352. // no dependency check between Ready() and Advance()
  353. func TestRawNodeStatus(t *testing.T) {
  354. storage := NewMemoryStorage()
  355. rawNode, err := NewRawNode(newTestConfig(1, nil, 10, 1, storage), []Peer{{ID: 1}})
  356. if err != nil {
  357. t.Fatal(err)
  358. }
  359. status := rawNode.Status()
  360. if status == nil {
  361. t.Errorf("expected status struct, got nil")
  362. }
  363. }