mirror of
https://github.com/etcd-io/etcd.git
synced 2024-09-27 06:25:44 +00:00
raft: fix restoring joint configurations
While writing interaction tests for joint configuration changes, I realized that this wasn't working yet - restoring had no notion of the joint configuration and was simply dropping it on the floor. This commit introduces a helper `confchange.Restore` which takes a `ConfState` and initializes a `Tracker` from it. This is then used both in `(*raft).restore` as well as in `newRaft`.
This commit is contained in:
45
raft/raftpb/confstate.go
Normal file
45
raft/raftpb/confstate.go
Normal file
@@ -0,0 +1,45 @@
|
||||
// Copyright 2019 The etcd Authors
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package raftpb
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"sort"
|
||||
)
|
||||
|
||||
// Equivalent returns a nil error if the inputs describe the same configuration.
|
||||
// On mismatch, returns a descriptive error showing the differences.
|
||||
func (cs ConfState) Equivalent(cs2 ConfState) error {
|
||||
cs1 := cs
|
||||
orig1, orig2 := cs1, cs2
|
||||
s := func(sl *[]uint64) {
|
||||
*sl = append([]uint64(nil), *sl...)
|
||||
sort.Slice(*sl, func(i, j int) bool { return (*sl)[i] < (*sl)[j] })
|
||||
}
|
||||
|
||||
for _, cs := range []*ConfState{&cs1, &cs2} {
|
||||
s(&cs.Voters)
|
||||
s(&cs.Learners)
|
||||
s(&cs.VotersOutgoing)
|
||||
s(&cs.LearnersNext)
|
||||
cs.XXX_unrecognized = nil
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(cs1, cs2) {
|
||||
return fmt.Errorf("ConfStates not equivalent after sorting:\n%+#v\n%+#v\nInputs were:\n%+#v\n%+#v", cs1, cs2, orig1, orig2)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
58
raft/raftpb/confstate_test.go
Normal file
58
raft/raftpb/confstate_test.go
Normal file
@@ -0,0 +1,58 @@
|
||||
// Copyright 2019 The etcd Authors
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package raftpb
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestConfState_Equivalent(t *testing.T) {
|
||||
type testCase struct {
|
||||
cs, cs2 ConfState
|
||||
ok bool
|
||||
}
|
||||
|
||||
testCases := []testCase{
|
||||
// Reordered voters and learners.
|
||||
{ConfState{
|
||||
Voters: []uint64{1, 2, 3},
|
||||
Learners: []uint64{5, 4, 6},
|
||||
VotersOutgoing: []uint64{9, 8, 7},
|
||||
LearnersNext: []uint64{10, 20, 15},
|
||||
}, ConfState{
|
||||
Voters: []uint64{1, 2, 3},
|
||||
Learners: []uint64{4, 5, 6},
|
||||
VotersOutgoing: []uint64{7, 9, 8},
|
||||
LearnersNext: []uint64{20, 10, 15},
|
||||
}, true},
|
||||
// Not sensitive to nil vs empty slice.
|
||||
{ConfState{Voters: []uint64{}}, ConfState{Voters: []uint64(nil)}, true},
|
||||
// Non-equivalent voters.
|
||||
{ConfState{Voters: []uint64{1, 2, 3, 4}}, ConfState{Voters: []uint64{2, 1, 3}}, false},
|
||||
{ConfState{Voters: []uint64{1, 4, 3}}, ConfState{Voters: []uint64{2, 1, 3}}, false},
|
||||
// Non-equivalent learners.
|
||||
{ConfState{Voters: []uint64{1, 2, 3, 4}}, ConfState{Voters: []uint64{2, 1, 3}}, false},
|
||||
// Sensitive to AutoLeave flag.
|
||||
{ConfState{AutoLeave: true}, ConfState{}, false},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run("", func(t *testing.T) {
|
||||
if err := tc.cs.Equivalent(tc.cs2); (err == nil) != tc.ok {
|
||||
t.Fatalf("wanted error: %t, got:\n%s", tc.ok, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user