raft_test.go 6.3 KB

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