-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
Copy pathclient_relocate_range_test.go
697 lines (633 loc) · 21.5 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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
// Copyright 2019 The Cockroach Authors.
//
// Use of this software is governed by the CockroachDB Software License
// included in the /LICENSE file.
package kvserver_test
import (
"context"
"math/rand"
"sort"
"testing"
"time"
"github.com/cockroachdb/cockroach/pkg/base"
"github.com/cockroachdb/cockroach/pkg/gossip"
"github.com/cockroachdb/cockroach/pkg/keys"
"github.com/cockroachdb/cockroach/pkg/kv"
"github.com/cockroachdb/cockroach/pkg/kv/kvpb"
"github.com/cockroachdb/cockroach/pkg/kv/kvserver"
"github.com/cockroachdb/cockroach/pkg/kv/kvserver/kvserverbase"
"github.com/cockroachdb/cockroach/pkg/kv/kvtestutils"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/server"
"github.com/cockroachdb/cockroach/pkg/testutils"
"github.com/cockroachdb/cockroach/pkg/testutils/testcluster"
"github.com/cockroachdb/cockroach/pkg/util"
"github.com/cockroachdb/cockroach/pkg/util/hlc"
"github.com/cockroachdb/cockroach/pkg/util/leaktest"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func relocateAndCheck(
t *testing.T,
tc *testcluster.TestCluster,
startKey roachpb.RKey,
voterTargets []roachpb.ReplicationTarget,
nonVoterTargets []roachpb.ReplicationTarget,
) (retries int) {
t.Helper()
every := log.Every(1 * time.Second)
testutils.SucceedsSoon(t, func() error {
err := tc.Servers[0].DB().
AdminRelocateRange(
context.Background(),
startKey.AsRawKey(),
voterTargets,
nonVoterTargets,
true, /* transferLeaseToFirstVoter */
)
if err != nil {
if every.ShouldLog() {
log.Infof(context.Background(), "AdminRelocateRange failed with error: %s", err)
}
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 requireRelocationFailure(
ctx context.Context,
t *testing.T,
tc *testcluster.TestCluster,
startKey roachpb.RKey,
voterTargets []roachpb.ReplicationTarget,
nonVoterTargets []roachpb.ReplicationTarget,
errRegExp string,
) {
testutils.SucceedsSoon(t, func() error {
err := tc.Servers[0].DB().AdminRelocateRange(
ctx,
startKey.AsRawKey(),
voterTargets,
nonVoterTargets,
true, /* transferLeaseToFirstVoter */
)
if kvtestutils.IsExpectedRelocateError(err) {
return err
}
require.Regexp(t, errRegExp, err)
return nil
})
}
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().Descriptors() {
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 {
// NB: Specifying a `hint` here does not play well with multi-store
// TestServers. See TODO inside `TestServer.GetRangeLease()`.
lease, _, err := tc.FindRangeLease(desc, nil /* hint */)
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 usesAtomicReplicationChange(ops []kvpb.ReplicationChange) bool {
// There are 4 sets of operations that are executed atomically:
// 1. Voter rebalances (ADD_VOTER, REMOVE_VOTER)
// 2. Non-voter promoted to voter (ADD_VOTER, REMOVE_NON_VOTER)
// 3. Voter demoted to non-voter (ADD_NON_VOTER, REMOVE_VOTER)
// 4. Voter swapped with non-voter (ADD_VOTER, REMOVE_NON_VOTER,
// ADD_NON_VOTER, REMOVE_VOTER)
if len(ops) >= 2 {
// Either a simple voter rebalance, or its a non-voter promotion.
if ops[0].ChangeType == roachpb.ADD_VOTER && ops[1].ChangeType.IsRemoval() {
return true
}
}
// Demotion of a voter.
if len(ops) == 2 &&
ops[0].ChangeType == roachpb.ADD_NON_VOTER && ops[1].ChangeType == roachpb.REMOVE_VOTER {
return true
}
return false
}
func TestAdminRelocateRange(t *testing.T) {
defer leaktest.AfterTest(t)()
defer log.Scope(t).Close(t)
ctx := context.Background()
type intercept struct {
ops []kvpb.ReplicationChange
leaseTarget *roachpb.ReplicationTarget
}
var intercepted []intercept
requireNumAtomic := func(expAtomic int, expSingle int, f func()) {
t.Helper()
intercepted = nil
f()
var actAtomic, actSingle int
for _, ic := range intercepted {
if usesAtomicReplicationChange(ic.ops) {
actAtomic++
} else {
actSingle += len(ic.ops)
}
}
assert.Equal(t, expAtomic, actAtomic, "wrong number of atomic changes")
assert.Equal(t, expSingle, actSingle, "wrong number of single changes")
if t.Failed() {
t.Log("all changes:")
for i, ic := range intercepted {
t.Logf("%d: %v", i+1, ic.ops)
}
t.FailNow()
}
}
knobs := base.TestingKnobs{
Store: &kvserver.StoreTestingKnobs{
OnRelocatedOne: func(ops []kvpb.ReplicationChange, leaseTarget *roachpb.ReplicationTarget) {
intercepted = append(intercepted, intercept{
ops: ops,
leaseTarget: leaseTarget,
})
},
},
}
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() {
relocateAndCheck(t, tc, k, targets, nil /* nonVoterTargets */)
})
}
// 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() {
relocateAndCheck(t, tc, k, targets, nil /* nonVoterTargets */)
})
}
// s4 (LH) s5 s6 ---> s5 (LH)
// Pure downreplication.
{
requireNumAtomic(0, 2, func() {
relocateAndCheck(t, tc, k, tc.Targets(4), nil /* nonVoterTargets */)
})
}
// s5 (LH) ---> s3 (LH)
// Lateral movement while at replication factor one.
{
requireNumAtomic(1, 0, func() {
relocateAndCheck(t, tc, k, tc.Targets(2), nil /* nonVoterTargets */)
})
}
// 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() {
relocateAndCheck(t, tc, k, tc.Targets(1, 3, 0), nil /* nonVoterTargets */)
})
// s2 s4 s1 -(add)-> s2 s4 s1 s6 (=s4 s2 s6 s1)
requireNumAtomic(0, 1, func() {
relocateAndCheck(t, tc, k, tc.Targets(3, 1, 5, 0), nil /* nonVoterTargets */)
})
// 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() {
relocateAndCheck(t, tc, k, tc.Targets(2, 4), nil /* nonVoterTargets */)
})
}
// Simple non-voter relocations.
{
requireNumAtomic(0, 2, func() {
relocateAndCheck(t, tc, k, tc.Targets(2, 4), tc.Targets(1, 3))
})
// Add & remove.
requireNumAtomic(0, 2, func() {
relocateAndCheck(t, tc, k, tc.Targets(2, 4), tc.Targets(1, 5))
})
// 2 add and 2 remove operations.
requireNumAtomic(0, 4, func() {
relocateAndCheck(t, tc, k, tc.Targets(2, 4), tc.Targets(0, 3))
})
}
// Relocation scenarios that require swapping of voters with non-voters.
{
// Single swap of voter and non-voter.
requireNumAtomic(1, 0, func() {
relocateAndCheck(t, tc, k, tc.Targets(0, 4), tc.Targets(2, 3))
})
// Multiple swaps.
requireNumAtomic(2, 0, func() {
relocateAndCheck(t, tc, k, tc.Targets(2, 3), tc.Targets(0, 4))
})
// Single promotion of non-voter to a voter.
requireNumAtomic(1, 0, func() {
relocateAndCheck(t, tc, k, tc.Targets(2, 3, 4), tc.Targets(0))
})
// Single demotion of voter to a non-voter.
requireNumAtomic(1, 0, func() {
relocateAndCheck(t, tc, k, tc.Targets(2, 4), tc.Targets(0, 3))
})
}
}
// TestAdminRelocateRangeWithoutLeaseTransfer tests that `AdminRelocateRange`
// only transfers the lease away to the first voting replica in the target slice
// if the callers asks it to.
func TestAdminRelocateRangeWithoutLeaseTransfer(t *testing.T) {
defer leaktest.AfterTest(t)()
defer log.Scope(t).Close(t)
ctx := context.Background()
args := base.TestClusterArgs{
ReplicationMode: base.ReplicationManual,
}
tc := testcluster.StartTestCluster(t, 5 /* numNodes */, args)
defer tc.Stopper().Stop(ctx)
k := keys.MustAddr(tc.ScratchRange(t))
// Add voters to the first three nodes.
relocateAndCheck(t, tc, k, tc.Targets(0, 1, 2), nil /* nonVoterTargets */)
// Move the last voter without asking for the lease to move.
err := tc.Servers[0].DB().AdminRelocateRange(
context.Background(),
k.AsRawKey(),
tc.Targets(3, 1, 0),
nil, /* nonVoterTargets */
false, /* transferLeaseToFirstVoter */
)
require.NoError(t, err)
leaseholder, err := tc.FindRangeLeaseHolder(tc.LookupRangeOrFatal(t, k.AsRawKey()), nil /* hint */)
require.NoError(t, err)
require.Equal(
t,
roachpb.ReplicationTarget{NodeID: leaseholder.NodeID, StoreID: leaseholder.StoreID},
tc.Target(0),
)
}
func TestAdminRelocateRangeFailsWithDuplicates(t *testing.T) {
defer leaktest.AfterTest(t)()
defer log.Scope(t).Close(t)
ctx := context.Background()
args := base.TestClusterArgs{
ReplicationMode: base.ReplicationManual,
}
tc := testcluster.StartTestCluster(t, 3, args)
defer tc.Stopper().Stop(ctx)
k := keys.MustAddr(tc.ScratchRange(t))
tests := []struct {
voterTargets, nonVoterTargets []int
expectedErr string
}{
{
voterTargets: []int{1, 1, 2},
expectedErr: "list of desired voter targets contains duplicates",
},
{
voterTargets: []int{1, 2},
nonVoterTargets: []int{0, 1, 0},
expectedErr: "list of desired non-voter targets contains duplicates",
},
{
voterTargets: []int{1, 2},
nonVoterTargets: []int{1},
expectedErr: "list of voter targets overlaps with the list of non-voter targets",
},
{
voterTargets: []int{1, 2},
nonVoterTargets: []int{1, 2},
expectedErr: "list of voter targets overlaps with the list of non-voter targets",
},
}
for _, subtest := range tests {
err := tc.Servers[0].DB().AdminRelocateRange(
context.Background(),
k.AsRawKey(),
tc.Targets(subtest.voterTargets...),
tc.Targets(subtest.nonVoterTargets...),
true, /* transferLeaseToFirstVoter */
)
require.Regexp(t, subtest.expectedErr, err)
}
}
// TestAdminRelocateRangeRandom runs a series of random relocations on a scratch
// range and checks to ensure that the relocations were successfully executed.
func TestAdminRelocateRangeRandom(t *testing.T) {
defer leaktest.AfterTest(t)()
defer log.Scope(t).Close(t)
ctx := context.Background()
args := base.TestClusterArgs{
ReplicationMode: base.ReplicationManual,
ServerArgs: base.TestServerArgs{
Knobs: base.TestingKnobs{
Store: &kvserver.StoreTestingKnobs{
DontIgnoreFailureToTransferLease: true,
},
NodeLiveness: kvserver.NodeLivenessTestingKnobs{
// Use a long liveness duration to avoid flakiness under stress on the
// lease check performed by `relocateAndCheck`.
LivenessDuration: 20 * time.Second,
},
},
},
}
numNodes, numIterations := 5, 10
if util.RaceEnabled {
numNodes, numIterations = 3, 1
}
randomRelocationTargets := func() (voterTargets, nonVoterTargets []int) {
targets := make([]int, numNodes)
for i := 0; i < numNodes; i++ {
targets[i] = i
}
numVoters := 1 + rand.Intn(numNodes) // Need at least one voter.
rand.Shuffle(numNodes, func(i, j int) {
targets[i], targets[j] = targets[j], targets[i]
})
return targets[:numVoters], targets[numVoters:]
}
tc := testcluster.StartTestCluster(t, numNodes, args)
defer tc.Stopper().Stop(ctx)
k := keys.MustAddr(tc.ScratchRange(t))
for i := 0; i < numIterations; i++ {
voters, nonVoters := randomRelocationTargets()
relocateAndCheck(t, tc, k, tc.Targets(voters...), tc.Targets(nonVoters...))
}
}
// Regression test for https://github.com/cockroachdb/cockroach/issues/64325
// which makes sure an in-flight read operation during replica removal won't
// return empty results.
func TestReplicaRemovalDuringGet(t *testing.T) {
defer leaktest.AfterTest(t)()
defer log.Scope(t).Close(t)
ctx := context.Background()
tc, key, evalDuringReplicaRemoval := setupReplicaRemovalTest(t, ctx)
defer tc.Stopper().Stop(ctx)
// Perform write.
pArgs := putArgs(key, []byte("foo"))
_, pErr := kv.SendWrapped(ctx, tc.Servers[0].DistSenderI().(kv.Sender), pArgs)
require.Nil(t, pErr)
// Perform delayed read during replica removal.
resp, pErr := evalDuringReplicaRemoval(ctx, getArgs(key))
require.Nil(t, pErr)
require.NotNil(t, resp)
require.NotNil(t, resp.(*kvpb.GetResponse).Value)
val, err := resp.(*kvpb.GetResponse).Value.GetBytes()
require.NoError(t, err)
require.Equal(t, []byte("foo"), val)
}
// Regression test for https://github.com/cockroachdb/cockroach/issues/46329
// which makes sure an in-flight conditional put operation during replica
// removal won't spuriously error due to an unexpectedly missing value.
func TestReplicaRemovalDuringCPut(t *testing.T) {
defer leaktest.AfterTest(t)()
defer log.Scope(t).Close(t)
ctx := context.Background()
tc, key, evalDuringReplicaRemoval := setupReplicaRemovalTest(t, ctx)
defer tc.Stopper().Stop(ctx)
// Perform write.
pArgs := putArgs(key, []byte("foo"))
_, pErr := kv.SendWrapped(ctx, tc.Servers[0].DistSenderI().(kv.Sender), pArgs)
require.Nil(t, pErr)
// Perform delayed conditional put during replica removal. This will cause
// an ambiguous result error, as outstanding proposals in the leaseholder
// replica's proposal queue will be aborted when the replica is removed.
// If the replica was removed from under us, it would instead return a
// ConditionFailedError since it finds nil in place of "foo".
req := cPutArgs(key, []byte("bar"), []byte("foo"))
_, pErr = evalDuringReplicaRemoval(ctx, req)
require.NotNil(t, pErr)
require.IsType(t, &kvpb.AmbiguousResultError{}, pErr.GetDetail())
}
// setupReplicaRemovalTest sets up a test cluster that can be used to test
// request evaluation during replica removal. It returns a running test
// cluster, the first key of a blank scratch range on the replica to be
// removed, and a function that can execute a delayed request just as the
// replica is being removed.
func setupReplicaRemovalTest(
t *testing.T, ctx context.Context,
) (
*testcluster.TestCluster,
roachpb.Key,
func(context.Context, kvpb.Request) (kvpb.Response, *kvpb.Error),
) {
t.Helper()
type magicKey struct{}
requestReadyC := make(chan struct{}) // signals main thread that request is teed up
requestEvalC := make(chan struct{}) // signals cluster to evaluate the request
evalFilter := func(args kvserverbase.FilterArgs) *kvpb.Error {
if args.Ctx.Value(magicKey{}) != nil {
requestReadyC <- struct{}{}
<-requestEvalC
}
return nil
}
manual := hlc.NewHybridManualClock()
args := base.TestClusterArgs{
ReplicationMode: base.ReplicationManual,
ServerArgs: base.TestServerArgs{
Knobs: base.TestingKnobs{
Store: &kvserver.StoreTestingKnobs{
EvalKnobs: kvserverbase.BatchEvalTestingKnobs{
TestingEvalFilter: evalFilter,
},
// Required by TestCluster.MoveRangeLeaseNonCooperatively.
AllowLeaseRequestProposalsWhenNotLeader: true,
},
Server: &server.TestingKnobs{
WallClock: manual,
},
},
},
}
tc := testcluster.StartTestCluster(t, 2, args)
// Create range and upreplicate.
key := tc.ScratchRange(t)
tc.AddVotersOrFatal(t, key, tc.Target(1))
// Return a function that can be used to evaluate a delayed request
// during replica removal.
evalDuringReplicaRemoval := func(ctx context.Context, req kvpb.Request) (kvpb.Response, *kvpb.Error) {
// Submit request and wait for it to block.
type result struct {
resp kvpb.Response
err *kvpb.Error
}
resultC := make(chan result)
srv := tc.Servers[0]
err := srv.Stopper().RunAsyncTask(ctx, "request", func(ctx context.Context) {
reqCtx := context.WithValue(ctx, magicKey{}, struct{}{})
resp, pErr := kv.SendWrapped(reqCtx, srv.DistSenderI().(kv.Sender), req)
resultC <- result{resp, pErr}
})
require.NoError(t, err)
<-requestReadyC
// Transfer leaseholder to other store.
rangeDesc, err := tc.LookupRange(key)
require.NoError(t, err)
repl, err := tc.GetFirstStoreFromServer(t, 0).GetReplica(rangeDesc.RangeID)
require.NoError(t, err)
_, err = tc.MoveRangeLeaseNonCooperatively(t, ctx, rangeDesc, tc.Target(1), manual)
require.NoError(t, err)
// Remove first store from raft group.
tc.RemoveVotersOrFatal(t, key, tc.Target(0))
// Wait for replica removal. This is a bit iffy. We want to make sure
// that, in the buggy case, we will typically fail (i.e. the request
// returns incorrect results because the replica was removed). However,
// in the non-buggy case the in-flight request will be holding
// readOnlyCmdMu until evaluated, blocking the replica removal, so
// waiting for replica removal would deadlock. We therefore take the
// easy way out by starting an async replica GC and sleeping for a bit.
err = tc.Stopper().RunAsyncTask(ctx, "replicaGC", func(ctx context.Context) {
assert.NoError(t, tc.GetFirstStoreFromServer(t, 0).ManualReplicaGC(repl))
})
require.NoError(t, err)
time.Sleep(500 * time.Millisecond)
// Allow request to resume, and return the result.
close(requestEvalC)
r := <-resultC
return r.resp, r.err
}
return tc, key, evalDuringReplicaRemoval
}
// TestAdminRelocateRangeLaterallyAmongStores tests that `AdminRelocateRange` is
// able to relocate ranges laterally (i.e. between stores on the same node).
func TestAdminRelocateRangeLaterallyAmongStores(t *testing.T) {
defer leaktest.AfterTest(t)()
defer log.Scope(t).Close(t)
ctx := context.Background()
// Set up a test cluster with each node having 2 stores.
args := base.TestClusterArgs{
ServerArgs: base.TestServerArgs{
StoreSpecs: []base.StoreSpec{
{InMemory: true},
{InMemory: true},
},
},
ReplicationMode: base.ReplicationManual,
}
tc := testcluster.StartTestCluster(t, 5, args)
defer tc.Stopper().Stop(ctx)
for i := 0; i < tc.NumServers(); i++ {
tc.WaitForNStores(t, tc.NumServers()*2, tc.Server(i).GossipI().(*gossip.Gossip))
}
scratchKey := keys.MustAddr(tc.ScratchRange(t))
// Place replicas for the scratch range on stores 1, 3, 5 (i.e. the first
// store on each of the nodes). Note that the test cluster will start off with
// (n1,s1) already having a replica.
scratchDesc := tc.LookupRangeOrFatal(t, scratchKey.AsRawKey())
_, found := scratchDesc.GetReplicaDescriptor(1)
require.True(t, found)
tc.AddVotersOrFatal(t, scratchKey.AsRawKey(), []roachpb.ReplicationTarget{
{NodeID: 2, StoreID: 3},
{NodeID: 3, StoreID: 5},
}...)
// Now, ask `AdminRelocateRange()` to move all of these replicas laterally.
relocateAndCheck(
t, tc, scratchKey, []roachpb.ReplicationTarget{
{NodeID: 1, StoreID: 2},
{NodeID: 2, StoreID: 4},
{NodeID: 3, StoreID: 5},
}, nil, /* nonVoterTargets */
)
// Ensure that this sort of lateral relocation works even across non-voters
// and voters.
relocateAndCheck(
t, tc, scratchKey, []roachpb.ReplicationTarget{
{NodeID: 2, StoreID: 4},
{NodeID: 3, StoreID: 5},
}, []roachpb.ReplicationTarget{
{NodeID: 1, StoreID: 1},
},
)
relocateAndCheck(
t, tc, scratchKey, []roachpb.ReplicationTarget{
{NodeID: 2, StoreID: 4},
{NodeID: 3, StoreID: 5},
}, []roachpb.ReplicationTarget{
{NodeID: 1, StoreID: 2},
},
)
// Ensure that, in case a caller of `AdminRelocateRange` tries to place 2
// replicas on the same node, a safeguard inside `AdminChangeReplicas()`
// rejects the operation.
requireRelocationFailure(
ctx, t, tc, scratchKey, []roachpb.ReplicationTarget{
{NodeID: 1, StoreID: 1},
{NodeID: 1, StoreID: 2},
{NodeID: 2, StoreID: 4},
{NodeID: 3, StoreID: 5},
}, nil, /* nonVoterTargets */
"node 1 already has a replica", /* errRegExp */
)
// Same as above, but for non-voting replicas.
requireRelocationFailure(
ctx, t, tc, scratchKey, []roachpb.ReplicationTarget{
{NodeID: 2, StoreID: 4},
{NodeID: 3, StoreID: 5},
}, []roachpb.ReplicationTarget{
{NodeID: 1, StoreID: 1},
{NodeID: 1, StoreID: 2},
}, "node 1 already has a replica", /* errRegExp */
)
// Ensure that we can't place 2 replicas on the same node even if one is a
// voter and the other is a non-voter.
requireRelocationFailure(
ctx, t, tc, scratchKey, []roachpb.ReplicationTarget{
{NodeID: 1, StoreID: 1},
{NodeID: 2, StoreID: 4},
{NodeID: 3, StoreID: 5},
}, []roachpb.ReplicationTarget{
{NodeID: 1, StoreID: 2},
}, "node 1 already has a replica", /* errRegExp */
)
}