raft_test.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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 etcdserver
  15. import (
  16. "encoding/json"
  17. "reflect"
  18. "sync"
  19. "testing"
  20. "time"
  21. "go.etcd.io/etcd/etcdserver/api/membership"
  22. "go.etcd.io/etcd/pkg/mock/mockstorage"
  23. "go.etcd.io/etcd/pkg/pbutil"
  24. "go.etcd.io/etcd/pkg/types"
  25. "go.etcd.io/etcd/raft"
  26. "go.etcd.io/etcd/raft/raftpb"
  27. "go.uber.org/zap"
  28. )
  29. func TestGetIDs(t *testing.T) {
  30. addcc := &raftpb.ConfChange{Type: raftpb.ConfChangeAddNode, NodeID: 2}
  31. addEntry := raftpb.Entry{Type: raftpb.EntryConfChange, Data: pbutil.MustMarshal(addcc)}
  32. removecc := &raftpb.ConfChange{Type: raftpb.ConfChangeRemoveNode, NodeID: 2}
  33. removeEntry := raftpb.Entry{Type: raftpb.EntryConfChange, Data: pbutil.MustMarshal(removecc)}
  34. normalEntry := raftpb.Entry{Type: raftpb.EntryNormal}
  35. updatecc := &raftpb.ConfChange{Type: raftpb.ConfChangeUpdateNode, NodeID: 2}
  36. updateEntry := raftpb.Entry{Type: raftpb.EntryConfChange, Data: pbutil.MustMarshal(updatecc)}
  37. tests := []struct {
  38. confState *raftpb.ConfState
  39. ents []raftpb.Entry
  40. widSet []uint64
  41. }{
  42. {nil, []raftpb.Entry{}, []uint64{}},
  43. {&raftpb.ConfState{Voters: []uint64{1}},
  44. []raftpb.Entry{}, []uint64{1}},
  45. {&raftpb.ConfState{Voters: []uint64{1}},
  46. []raftpb.Entry{addEntry}, []uint64{1, 2}},
  47. {&raftpb.ConfState{Voters: []uint64{1}},
  48. []raftpb.Entry{addEntry, removeEntry}, []uint64{1}},
  49. {&raftpb.ConfState{Voters: []uint64{1}},
  50. []raftpb.Entry{addEntry, normalEntry}, []uint64{1, 2}},
  51. {&raftpb.ConfState{Voters: []uint64{1}},
  52. []raftpb.Entry{addEntry, normalEntry, updateEntry}, []uint64{1, 2}},
  53. {&raftpb.ConfState{Voters: []uint64{1}},
  54. []raftpb.Entry{addEntry, removeEntry, normalEntry}, []uint64{1}},
  55. }
  56. for i, tt := range tests {
  57. var snap raftpb.Snapshot
  58. if tt.confState != nil {
  59. snap.Metadata.ConfState = *tt.confState
  60. }
  61. idSet := getIDs(testLogger, &snap, tt.ents)
  62. if !reflect.DeepEqual(idSet, tt.widSet) {
  63. t.Errorf("#%d: idset = %#v, want %#v", i, idSet, tt.widSet)
  64. }
  65. }
  66. }
  67. func TestCreateConfigChangeEnts(t *testing.T) {
  68. m := membership.Member{
  69. ID: types.ID(1),
  70. RaftAttributes: membership.RaftAttributes{PeerURLs: []string{"http://localhost:2380"}},
  71. }
  72. ctx, err := json.Marshal(m)
  73. if err != nil {
  74. t.Fatal(err)
  75. }
  76. addcc1 := &raftpb.ConfChange{Type: raftpb.ConfChangeAddNode, NodeID: 1, Context: ctx}
  77. removecc2 := &raftpb.ConfChange{Type: raftpb.ConfChangeRemoveNode, NodeID: 2}
  78. removecc3 := &raftpb.ConfChange{Type: raftpb.ConfChangeRemoveNode, NodeID: 3}
  79. tests := []struct {
  80. ids []uint64
  81. self uint64
  82. term, index uint64
  83. wents []raftpb.Entry
  84. }{
  85. {
  86. []uint64{1},
  87. 1,
  88. 1, 1,
  89. nil,
  90. },
  91. {
  92. []uint64{1, 2},
  93. 1,
  94. 1, 1,
  95. []raftpb.Entry{{Term: 1, Index: 2, Type: raftpb.EntryConfChange, Data: pbutil.MustMarshal(removecc2)}},
  96. },
  97. {
  98. []uint64{1, 2},
  99. 1,
  100. 2, 2,
  101. []raftpb.Entry{{Term: 2, Index: 3, Type: raftpb.EntryConfChange, Data: pbutil.MustMarshal(removecc2)}},
  102. },
  103. {
  104. []uint64{1, 2, 3},
  105. 1,
  106. 2, 2,
  107. []raftpb.Entry{
  108. {Term: 2, Index: 3, Type: raftpb.EntryConfChange, Data: pbutil.MustMarshal(removecc2)},
  109. {Term: 2, Index: 4, Type: raftpb.EntryConfChange, Data: pbutil.MustMarshal(removecc3)},
  110. },
  111. },
  112. {
  113. []uint64{2, 3},
  114. 2,
  115. 2, 2,
  116. []raftpb.Entry{
  117. {Term: 2, Index: 3, Type: raftpb.EntryConfChange, Data: pbutil.MustMarshal(removecc3)},
  118. },
  119. },
  120. {
  121. []uint64{2, 3},
  122. 1,
  123. 2, 2,
  124. []raftpb.Entry{
  125. {Term: 2, Index: 3, Type: raftpb.EntryConfChange, Data: pbutil.MustMarshal(addcc1)},
  126. {Term: 2, Index: 4, Type: raftpb.EntryConfChange, Data: pbutil.MustMarshal(removecc2)},
  127. {Term: 2, Index: 5, Type: raftpb.EntryConfChange, Data: pbutil.MustMarshal(removecc3)},
  128. },
  129. },
  130. }
  131. for i, tt := range tests {
  132. gents := createConfigChangeEnts(testLogger, tt.ids, tt.self, tt.term, tt.index)
  133. if !reflect.DeepEqual(gents, tt.wents) {
  134. t.Errorf("#%d: ents = %v, want %v", i, gents, tt.wents)
  135. }
  136. }
  137. }
  138. func TestStopRaftWhenWaitingForApplyDone(t *testing.T) {
  139. n := newNopReadyNode()
  140. r := newRaftNode(raftNodeConfig{
  141. lg: zap.NewExample(),
  142. Node: n,
  143. storage: mockstorage.NewStorageRecorder(""),
  144. raftStorage: raft.NewMemoryStorage(),
  145. transport: newNopTransporter(),
  146. })
  147. srv := &EtcdServer{lgMu: new(sync.RWMutex), lg: zap.NewExample(), r: *r}
  148. srv.r.start(nil)
  149. n.readyc <- raft.Ready{}
  150. select {
  151. case <-srv.r.applyc:
  152. case <-time.After(time.Second):
  153. t.Fatalf("failed to receive apply struct")
  154. }
  155. srv.r.stopped <- struct{}{}
  156. select {
  157. case <-srv.r.done:
  158. case <-time.After(time.Second):
  159. t.Fatalf("failed to stop raft loop")
  160. }
  161. }
  162. // TestConfigChangeBlocksApply ensures apply blocks if committed entries contain config-change.
  163. func TestConfigChangeBlocksApply(t *testing.T) {
  164. n := newNopReadyNode()
  165. r := newRaftNode(raftNodeConfig{
  166. lg: zap.NewExample(),
  167. Node: n,
  168. storage: mockstorage.NewStorageRecorder(""),
  169. raftStorage: raft.NewMemoryStorage(),
  170. transport: newNopTransporter(),
  171. })
  172. srv := &EtcdServer{lgMu: new(sync.RWMutex), lg: zap.NewExample(), r: *r}
  173. srv.r.start(&raftReadyHandler{
  174. getLead: func() uint64 { return 0 },
  175. updateLead: func(uint64) {},
  176. updateLeadership: func(bool) {},
  177. })
  178. defer srv.r.Stop()
  179. n.readyc <- raft.Ready{
  180. SoftState: &raft.SoftState{RaftState: raft.StateFollower},
  181. CommittedEntries: []raftpb.Entry{{Type: raftpb.EntryConfChange}},
  182. }
  183. ap := <-srv.r.applyc
  184. continueC := make(chan struct{})
  185. go func() {
  186. n.readyc <- raft.Ready{}
  187. <-srv.r.applyc
  188. close(continueC)
  189. }()
  190. select {
  191. case <-continueC:
  192. t.Fatalf("unexpected execution: raft routine should block waiting for apply")
  193. case <-time.After(time.Second):
  194. }
  195. // finish apply, unblock raft routine
  196. <-ap.notifyc
  197. select {
  198. case <-continueC:
  199. case <-time.After(time.Second):
  200. t.Fatalf("unexpected blocking on execution")
  201. }
  202. }
  203. func TestProcessDuplicatedAppRespMessage(t *testing.T) {
  204. n := newNopReadyNode()
  205. cl := membership.NewCluster(zap.NewExample(), "abc")
  206. rs := raft.NewMemoryStorage()
  207. p := mockstorage.NewStorageRecorder("")
  208. tr, sendc := newSendMsgAppRespTransporter()
  209. r := newRaftNode(raftNodeConfig{
  210. lg: zap.NewExample(),
  211. isIDRemoved: func(id uint64) bool { return cl.IsIDRemoved(types.ID(id)) },
  212. Node: n,
  213. transport: tr,
  214. storage: p,
  215. raftStorage: rs,
  216. })
  217. s := &EtcdServer{
  218. lgMu: new(sync.RWMutex),
  219. lg: zap.NewExample(),
  220. r: *r,
  221. cluster: cl,
  222. SyncTicker: &time.Ticker{},
  223. }
  224. s.start()
  225. defer s.Stop()
  226. lead := uint64(1)
  227. n.readyc <- raft.Ready{Messages: []raftpb.Message{
  228. {Type: raftpb.MsgAppResp, From: 2, To: lead, Term: 1, Index: 1},
  229. {Type: raftpb.MsgAppResp, From: 2, To: lead, Term: 1, Index: 2},
  230. {Type: raftpb.MsgAppResp, From: 2, To: lead, Term: 1, Index: 3},
  231. }}
  232. got, want := <-sendc, 1
  233. if got != want {
  234. t.Errorf("count = %d, want %d", got, want)
  235. }
  236. }