rawnode_test.go 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. "github.com/coreos/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. // TestRawNodeReadIndex ensures that Rawnode.ReadIndex sends the MsgReadIndex message
  102. // to the underlying raft. It also ensures that ReadState can be read out.
  103. func TestRawNodeReadIndex(t *testing.T) {
  104. msgs := []raftpb.Message{}
  105. appendStep := func(r *raft, m raftpb.Message) {
  106. msgs = append(msgs, m)
  107. }
  108. wrs := []ReadState{{Index: uint64(1), RequestCtx: []byte("somedata")}}
  109. s := NewMemoryStorage()
  110. c := newTestConfig(1, nil, 10, 1, s)
  111. rawNode, err := NewRawNode(c, []Peer{{ID: 1}})
  112. if err != nil {
  113. t.Fatal(err)
  114. }
  115. rawNode.raft.readStates = wrs
  116. // ensure the ReadStates can be read out
  117. hasReady := rawNode.HasReady()
  118. if hasReady != true {
  119. t.Errorf("HasReady() returns %t, want %t", hasReady, true)
  120. }
  121. rd := rawNode.Ready()
  122. if !reflect.DeepEqual(rd.ReadStates, wrs) {
  123. t.Errorf("ReadStates = %d, want %d", rd.ReadStates, wrs)
  124. }
  125. s.Append(rd.Entries)
  126. rawNode.Advance(rd)
  127. // ensure raft.readStates is reset after advance
  128. if rawNode.raft.readStates != nil {
  129. t.Errorf("readStates = %v, want %v", rawNode.raft.readStates, nil)
  130. }
  131. wrequestCtx := []byte("somedata2")
  132. rawNode.Campaign()
  133. for {
  134. rd = rawNode.Ready()
  135. s.Append(rd.Entries)
  136. if rd.SoftState.Lead == rawNode.raft.id {
  137. rawNode.Advance(rd)
  138. // Once we are the leader, issue a ReadIndex request
  139. rawNode.raft.step = appendStep
  140. rawNode.ReadIndex(wrequestCtx)
  141. break
  142. }
  143. rawNode.Advance(rd)
  144. }
  145. // ensure that MsgReadIndex message is sent to the underlying raft
  146. if len(msgs) != 1 {
  147. t.Fatalf("len(msgs) = %d, want %d", len(msgs), 1)
  148. }
  149. if msgs[0].Type != raftpb.MsgReadIndex {
  150. t.Errorf("msg type = %d, want %d", msgs[0].Type, raftpb.MsgReadIndex)
  151. }
  152. if !bytes.Equal(msgs[0].Entries[0].Data, wrequestCtx) {
  153. t.Errorf("data = %v, want %v", msgs[0].Entries[0].Data, wrequestCtx)
  154. }
  155. }
  156. // TestBlockProposal from node_test.go has no equivalent in rawNode because there is
  157. // no leader check in RawNode.
  158. // TestNodeTick from node_test.go has no equivalent in rawNode because
  159. // it reaches into the raft object which is not exposed.
  160. // TestNodeStop from node_test.go has no equivalent in rawNode because there is
  161. // no goroutine in RawNode.
  162. // TestRawNodeStart ensures that a node can be started correctly. The node should
  163. // start with correct configuration change entries, and can accept and commit
  164. // proposals.
  165. func TestRawNodeStart(t *testing.T) {
  166. cc := raftpb.ConfChange{Type: raftpb.ConfChangeAddNode, NodeID: 1}
  167. ccdata, err := cc.Marshal()
  168. if err != nil {
  169. t.Fatalf("unexpected marshal error: %v", err)
  170. }
  171. wants := []Ready{
  172. {
  173. HardState: raftpb.HardState{Term: 1, Commit: 1, Vote: 0},
  174. Entries: []raftpb.Entry{
  175. {Type: raftpb.EntryConfChange, Term: 1, Index: 1, Data: ccdata},
  176. },
  177. CommittedEntries: []raftpb.Entry{
  178. {Type: raftpb.EntryConfChange, Term: 1, Index: 1, Data: ccdata},
  179. },
  180. },
  181. {
  182. HardState: raftpb.HardState{Term: 2, Commit: 3, Vote: 1},
  183. Entries: []raftpb.Entry{{Term: 2, Index: 3, Data: []byte("foo")}},
  184. CommittedEntries: []raftpb.Entry{{Term: 2, Index: 3, Data: []byte("foo")}},
  185. },
  186. }
  187. storage := NewMemoryStorage()
  188. rawNode, err := NewRawNode(newTestConfig(1, nil, 10, 1, storage), []Peer{{ID: 1}})
  189. if err != nil {
  190. t.Fatal(err)
  191. }
  192. rd := rawNode.Ready()
  193. t.Logf("rd %v", rd)
  194. if !reflect.DeepEqual(rd, wants[0]) {
  195. t.Fatalf("#%d: g = %+v,\n w %+v", 1, rd, wants[0])
  196. } else {
  197. storage.Append(rd.Entries)
  198. rawNode.Advance(rd)
  199. }
  200. storage.Append(rd.Entries)
  201. rawNode.Advance(rd)
  202. rawNode.Campaign()
  203. rd = rawNode.Ready()
  204. storage.Append(rd.Entries)
  205. rawNode.Advance(rd)
  206. rawNode.Propose([]byte("foo"))
  207. if rd = rawNode.Ready(); !reflect.DeepEqual(rd, wants[1]) {
  208. t.Errorf("#%d: g = %+v,\n w %+v", 2, rd, wants[1])
  209. } else {
  210. storage.Append(rd.Entries)
  211. rawNode.Advance(rd)
  212. }
  213. if rawNode.HasReady() {
  214. t.Errorf("unexpected Ready: %+v", rawNode.Ready())
  215. }
  216. }
  217. func TestRawNodeRestart(t *testing.T) {
  218. entries := []raftpb.Entry{
  219. {Term: 1, Index: 1},
  220. {Term: 1, Index: 2, Data: []byte("foo")},
  221. }
  222. st := raftpb.HardState{Term: 1, Commit: 1}
  223. want := Ready{
  224. HardState: emptyState,
  225. // commit up to commit index in st
  226. CommittedEntries: entries[:st.Commit],
  227. }
  228. storage := NewMemoryStorage()
  229. storage.SetHardState(st)
  230. storage.Append(entries)
  231. rawNode, err := NewRawNode(newTestConfig(1, nil, 10, 1, storage), nil)
  232. if err != nil {
  233. t.Fatal(err)
  234. }
  235. rd := rawNode.Ready()
  236. if !reflect.DeepEqual(rd, want) {
  237. t.Errorf("g = %+v,\n w %+v", rd, want)
  238. }
  239. rawNode.Advance(rd)
  240. if rawNode.HasReady() {
  241. t.Errorf("unexpected Ready: %+v", rawNode.Ready())
  242. }
  243. }
  244. func TestRawNodeRestartFromSnapshot(t *testing.T) {
  245. snap := raftpb.Snapshot{
  246. Metadata: raftpb.SnapshotMetadata{
  247. ConfState: raftpb.ConfState{Nodes: []uint64{1, 2}},
  248. Index: 2,
  249. Term: 1,
  250. },
  251. }
  252. entries := []raftpb.Entry{
  253. {Term: 1, Index: 3, Data: []byte("foo")},
  254. }
  255. st := raftpb.HardState{Term: 1, Commit: 3}
  256. want := Ready{
  257. HardState: emptyState,
  258. // commit up to commit index in st
  259. CommittedEntries: entries,
  260. }
  261. s := NewMemoryStorage()
  262. s.SetHardState(st)
  263. s.ApplySnapshot(snap)
  264. s.Append(entries)
  265. rawNode, err := NewRawNode(newTestConfig(1, nil, 10, 1, s), nil)
  266. if err != nil {
  267. t.Fatal(err)
  268. }
  269. if rd := rawNode.Ready(); !reflect.DeepEqual(rd, want) {
  270. t.Errorf("g = %+v,\n w %+v", rd, want)
  271. } else {
  272. rawNode.Advance(rd)
  273. }
  274. if rawNode.HasReady() {
  275. t.Errorf("unexpected Ready: %+v", rawNode.HasReady())
  276. }
  277. }
  278. // TestNodeAdvance from node_test.go has no equivalent in rawNode because there is
  279. // no dependency check between Ready() and Advance()
  280. func TestRawNodeStatus(t *testing.T) {
  281. storage := NewMemoryStorage()
  282. rawNode, err := NewRawNode(newTestConfig(1, nil, 10, 1, storage), []Peer{{ID: 1}})
  283. if err != nil {
  284. t.Fatal(err)
  285. }
  286. status := rawNode.Status()
  287. if status == nil {
  288. t.Errorf("expected status struct, got nil")
  289. }
  290. }