restore_test.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // Copyright 2019 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 confchange
  15. import (
  16. "math/rand"
  17. "reflect"
  18. "sort"
  19. "testing"
  20. "testing/quick"
  21. pb "go.etcd.io/etcd/raft/raftpb"
  22. "go.etcd.io/etcd/raft/tracker"
  23. )
  24. type rndConfChange pb.ConfState
  25. // Generate creates a random (valid) ConfState for use with quickcheck.
  26. func (rndConfChange) Generate(rand *rand.Rand, _ int) reflect.Value {
  27. conv := func(sl []int) []uint64 {
  28. // We want IDs but the incoming slice is zero-indexed, so add one to
  29. // each.
  30. out := make([]uint64, len(sl))
  31. for i := range sl {
  32. out[i] = uint64(sl[i] + 1)
  33. }
  34. return out
  35. }
  36. var cs pb.ConfState
  37. // NB: never generate the empty ConfState, that one should be unit tested.
  38. nVoters := 1 + rand.Intn(5)
  39. nLearners := rand.Intn(5)
  40. // The number of voters that are in the outgoing config but not in the
  41. // incoming one. (We'll additionally retain a random number of the
  42. // incoming voters below).
  43. nRemovedVoters := rand.Intn(3)
  44. // Voters, learners, and removed voters must not overlap. A "removed voter"
  45. // is one that we have in the outgoing config but not the incoming one.
  46. ids := conv(rand.Perm(2 * (nVoters + nLearners + nRemovedVoters)))
  47. cs.Voters = ids[:nVoters]
  48. ids = ids[nVoters:]
  49. if nLearners > 0 {
  50. cs.Learners = ids[:nLearners]
  51. ids = ids[nLearners:]
  52. }
  53. // Roll the dice on how many of the incoming voters we decide were also
  54. // previously voters.
  55. //
  56. // NB: this code avoids creating non-nil empty slices (here and below).
  57. nOutgoingRetainedVoters := rand.Intn(nVoters + 1)
  58. if nOutgoingRetainedVoters > 0 || nRemovedVoters > 0 {
  59. cs.VotersOutgoing = append([]uint64(nil), cs.Voters[:nOutgoingRetainedVoters]...)
  60. cs.VotersOutgoing = append(cs.VotersOutgoing, ids[:nRemovedVoters]...)
  61. }
  62. // Only outgoing voters that are not also incoming voters can be in
  63. // LearnersNext (they represent demotions).
  64. if nRemovedVoters > 0 {
  65. if nLearnersNext := rand.Intn(nRemovedVoters + 1); nLearnersNext > 0 {
  66. cs.LearnersNext = ids[:nLearnersNext]
  67. }
  68. }
  69. cs.AutoLeave = len(cs.VotersOutgoing) > 0 && rand.Intn(2) == 1
  70. return reflect.ValueOf(rndConfChange(cs))
  71. }
  72. func TestRestore(t *testing.T) {
  73. cfg := quick.Config{MaxCount: 1000}
  74. f := func(cs pb.ConfState) bool {
  75. chg := Changer{
  76. Tracker: tracker.MakeProgressTracker(20),
  77. LastIndex: 10,
  78. }
  79. cfg, prs, err := Restore(chg, cs)
  80. if err != nil {
  81. t.Error(err)
  82. return false
  83. }
  84. chg.Tracker.Config = cfg
  85. chg.Tracker.Progress = prs
  86. for _, sl := range [][]uint64{
  87. cs.Voters,
  88. cs.Learners,
  89. cs.VotersOutgoing,
  90. cs.LearnersNext,
  91. } {
  92. sort.Slice(sl, func(i, j int) bool { return sl[i] < sl[j] })
  93. }
  94. cs2 := chg.Tracker.ConfState()
  95. // NB: cs.Equivalent does the same "sorting" dance internally, but let's
  96. // test it a bit here instead of relying on it.
  97. if reflect.DeepEqual(cs, cs2) && cs.Equivalent(cs2) == nil && cs2.Equivalent(cs) == nil {
  98. return true // success
  99. }
  100. t.Errorf(`
  101. before: %+#v
  102. after: %+#v`, cs, cs2)
  103. return false
  104. }
  105. ids := func(sl ...uint64) []uint64 {
  106. return sl
  107. }
  108. // Unit tests.
  109. for _, cs := range []pb.ConfState{
  110. {},
  111. {Voters: ids(1, 2, 3)},
  112. {Voters: ids(1, 2, 3), Learners: ids(4, 5, 6)},
  113. {Voters: ids(1, 2, 3), Learners: ids(5), VotersOutgoing: ids(1, 2, 4, 6), LearnersNext: ids(4)},
  114. } {
  115. if !f(cs) {
  116. t.FailNow() // f() already logged a nice t.Error()
  117. }
  118. }
  119. if err := quick.Check(func(cs rndConfChange) bool {
  120. return f(pb.ConfState(cs))
  121. }, &cfg); err != nil {
  122. t.Error(err)
  123. }
  124. }