raft_test.go 6.1 KB

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