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