-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
client_relocate_range_test.go
226 lines (208 loc) · 6.79 KB
/
client_relocate_range_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
// Copyright 2019 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package kvserver_test
import (
"context"
"sort"
"testing"
"github.com/cockroachdb/cockroach/pkg/base"
"github.com/cockroachdb/cockroach/pkg/keys"
"github.com/cockroachdb/cockroach/pkg/kv/kvserver"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/testutils"
"github.com/cockroachdb/cockroach/pkg/testutils/testcluster"
"github.com/cockroachdb/cockroach/pkg/util/leaktest"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/errors"
"github.com/stretchr/testify/require"
)
func relocateAndCheck(
t *testing.T,
tc *testcluster.TestCluster,
startKey roachpb.RKey,
voterTargets []roachpb.ReplicationTarget,
nonVoterTargets []roachpb.ReplicationTarget,
) (retries int) {
testutils.SucceedsSoon(t, func() error {
err := tc.Servers[0].DB().
AdminRelocateRange(
context.Background(), startKey.AsRawKey(), voterTargets, nonVoterTargets,
)
if err != nil {
retries++
}
return err
})
desc, err := tc.Servers[0].LookupRange(startKey.AsRawKey())
require.NoError(t, err)
requireDescMembers(t, desc, append(voterTargets, nonVoterTargets...))
if len(voterTargets) > 0 {
requireLeaseAt(t, tc, desc, voterTargets[0])
}
return retries
}
func requireDescMembers(
t *testing.T, desc roachpb.RangeDescriptor, targets []roachpb.ReplicationTarget,
) {
t.Helper()
targets = append([]roachpb.ReplicationTarget(nil), targets...)
sort.Slice(targets, func(i, j int) bool { return targets[i].StoreID < targets[j].StoreID })
have := make([]roachpb.ReplicationTarget, 0, len(targets))
for _, rDesc := range desc.Replicas().All() {
have = append(have, roachpb.ReplicationTarget{
NodeID: rDesc.NodeID,
StoreID: rDesc.StoreID,
})
}
sort.Slice(have, func(i, j int) bool { return have[i].StoreID < have[j].StoreID })
require.Equal(t, targets, have)
}
func requireLeaseAt(
t *testing.T,
tc *testcluster.TestCluster,
desc roachpb.RangeDescriptor,
target roachpb.ReplicationTarget,
) {
t.Helper()
// NB: under stressrace the lease will sometimes be inactive by the time
// it's returned here, so don't use FindRangeLeaseHolder which fails when
// that happens.
testutils.SucceedsSoon(t, func() error {
lease, _, err := tc.FindRangeLease(desc, &target)
if err != nil {
return err
}
if target != (roachpb.ReplicationTarget{
NodeID: lease.Replica.NodeID,
StoreID: lease.Replica.StoreID,
}) {
return errors.Errorf("lease %v is not held by %+v", lease, target)
}
return nil
})
}
func TestAdminRelocateRange(t *testing.T) {
defer leaktest.AfterTest(t)()
defer log.Scope(t).Close(t)
ctx := context.Background()
type intercept struct {
ops []roachpb.ReplicationChange
leaseTarget *roachpb.ReplicationTarget
err error
}
var intercepted []intercept
requireNumAtomic := func(expAtomic int, expSingle int, f func() (retries int)) {
t.Helper()
intercepted = nil
retries := f()
var actAtomic, actSingle int
for _, ic := range intercepted {
if ic.err != nil {
continue
}
if len(ic.ops) == 2 && ic.ops[0].ChangeType == roachpb.ADD_VOTER && ic.ops[1].ChangeType == roachpb.REMOVE_VOTER {
actAtomic++
} else {
actSingle += len(ic.ops)
}
}
actAtomic -= retries
require.Equal(t, expAtomic, actAtomic, "wrong number of atomic changes: %+v", intercepted)
require.Equal(t, expSingle, actSingle, "wrong number of single changes: %+v", intercepted)
}
knobs := base.TestingKnobs{
Store: &kvserver.StoreTestingKnobs{
BeforeRelocateOne: func(ops []roachpb.ReplicationChange, leaseTarget *roachpb.ReplicationTarget, err error) {
intercepted = append(intercepted, intercept{
ops: ops,
leaseTarget: leaseTarget,
err: err,
})
},
},
}
args := base.TestClusterArgs{
ServerArgs: base.TestServerArgs{Knobs: knobs},
ReplicationMode: base.ReplicationManual,
}
tc := testcluster.StartTestCluster(t, 6, args)
defer tc.Stopper().Stop(ctx)
// s1 (LH) ---> s2 (LH) s1 s3
// Pure upreplication.
k := keys.MustAddr(tc.ScratchRange(t))
{
targets := tc.Targets(1, 0, 2)
// Expect two single additions, and that's it.
requireNumAtomic(0, 2, func() int {
return relocateAndCheck(t, tc, k, targets, []roachpb.ReplicationTarget{})
})
}
// s1 (LH) s2 s3 ---> s4 (LH) s5 s6.
// This is trickier because the leaseholder gets removed, and so do all
// other replicas (i.e. a simple lease transfer at the beginning won't solve
// the problem).
{
targets := tc.Targets(3, 4, 5)
// Should carry out three swaps. Note that the leaseholder gets removed
// in the process (i.e. internally the lease must've been moved around
// to achieve that).
requireNumAtomic(3, 0, func() int {
return relocateAndCheck(t, tc, k, targets, []roachpb.ReplicationTarget{})
})
}
// s4 (LH) s5 s6 ---> s5 (LH)
// Pure downreplication.
{
requireNumAtomic(0, 2, func() int {
return relocateAndCheck(t, tc, k, tc.Targets(4), []roachpb.ReplicationTarget{})
})
}
// s5 (LH) ---> s3 (LH)
// Lateral movement while at replication factor one. In this case atomic
// replication changes cannot be used; we add-then-remove instead.
{
requireNumAtomic(0, 2, func() int {
return relocateAndCheck(t, tc, k, tc.Targets(2), []roachpb.ReplicationTarget{})
})
}
// s3 (LH) ---> s2 (LH) s4 s1 --> s4 (LH) s2 s6 s1 --> s3 (LH) s5
// A grab bag.
{
// s3 -(add)-> s3 s2 -(swap)-> s4 s2 -(add)-> s4 s2 s1 (=s2 s4 s1)
requireNumAtomic(1, 2, func() int {
return relocateAndCheck(t, tc, k, tc.Targets(1, 3, 0), []roachpb.ReplicationTarget{})
})
// s2 s4 s1 -(add)-> s2 s4 s1 s6 (=s4 s2 s6 s1)
requireNumAtomic(0, 1, func() int {
return relocateAndCheck(t, tc, k, tc.Targets(3, 1, 5, 0), []roachpb.ReplicationTarget{})
})
// s4 s2 s6 s1 -(swap)-> s3 s2 s6 s1 -(swap)-> s3 s5 s6 s1 -(del)-> s3 s5 s6 -(del)-> s3 s5
requireNumAtomic(2, 2, func() int {
return relocateAndCheck(t, tc, k, tc.Targets(2, 4), []roachpb.ReplicationTarget{})
})
}
// Relocation of non-voting replicas is not done atomically under any
// scenario.
// NOTE: We intend on supporting large atomic swaps of non-voting replicas
// soon.
{
requireNumAtomic(0, 2, func() int {
return relocateAndCheck(t, tc, k, tc.Targets(2, 4), tc.Targets(1, 3))
})
// Add & remove.
requireNumAtomic(0, 2, func() int {
return relocateAndCheck(t, tc, k, tc.Targets(2, 4), tc.Targets(1, 5))
})
// 2 add and 2 remove operations.
requireNumAtomic(0, 4, func() int {
return relocateAndCheck(t, tc, k, tc.Targets(2, 4), tc.Targets(0, 3))
})
}
}