confstate.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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 raftpb
  15. import (
  16. "fmt"
  17. "reflect"
  18. "sort"
  19. )
  20. // Equivalent returns a nil error if the inputs describe the same configuration.
  21. // On mismatch, returns a descriptive error showing the differences.
  22. func (cs ConfState) Equivalent(cs2 ConfState) error {
  23. cs1 := cs
  24. orig1, orig2 := cs1, cs2
  25. s := func(sl *[]uint64) {
  26. *sl = append([]uint64(nil), *sl...)
  27. sort.Slice(*sl, func(i, j int) bool { return (*sl)[i] < (*sl)[j] })
  28. }
  29. for _, cs := range []*ConfState{&cs1, &cs2} {
  30. s(&cs.Voters)
  31. s(&cs.Learners)
  32. s(&cs.VotersOutgoing)
  33. s(&cs.LearnersNext)
  34. cs.XXX_unrecognized = nil
  35. }
  36. if !reflect.DeepEqual(cs1, cs2) {
  37. return fmt.Errorf("ConfStates not equivalent after sorting:\n%+#v\n%+#v\nInputs were:\n%+#v\n%+#v", cs1, cs2, orig1, orig2)
  38. }
  39. return nil
  40. }