-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
testcluster.go
690 lines (627 loc) · 21.8 KB
/
testcluster.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
// Copyright 2016 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 testcluster
import (
"context"
gosql "database/sql"
"fmt"
"sync"
"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/roachpb"
"github.com/cockroachdb/cockroach/pkg/server"
"github.com/cockroachdb/cockroach/pkg/server/serverpb"
"github.com/cockroachdb/cockroach/pkg/storage"
"github.com/cockroachdb/cockroach/pkg/storage/storagepb"
"github.com/cockroachdb/cockroach/pkg/testutils"
"github.com/cockroachdb/cockroach/pkg/testutils/serverutils"
"github.com/cockroachdb/cockroach/pkg/util/hlc"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/cockroach/pkg/util/retry"
"github.com/cockroachdb/cockroach/pkg/util/stop"
"github.com/cockroachdb/cockroach/pkg/util/syncutil"
"github.com/cockroachdb/cockroach/pkg/util/timeutil"
"github.com/pkg/errors"
)
// TestCluster represents a set of TestServers. The hope is that it can be used
// analoguous to TestServer, but with control over range replication.
type TestCluster struct {
Servers []*server.TestServer
Conns []*gosql.DB
stopper *stop.Stopper
replicationMode base.TestClusterReplicationMode
mu struct {
syncutil.Mutex
serverStoppers []*stop.Stopper
}
}
var _ serverutils.TestClusterInterface = &TestCluster{}
// NumServers is part of TestClusterInterface.
func (tc *TestCluster) NumServers() int {
return len(tc.Servers)
}
// Server is part of TestClusterInterface.
func (tc *TestCluster) Server(idx int) serverutils.TestServerInterface {
return tc.Servers[idx]
}
// ServerConn is part of TestClusterInterface.
func (tc *TestCluster) ServerConn(idx int) *gosql.DB {
return tc.Conns[idx]
}
// Stopper returns the stopper for this testcluster.
func (tc *TestCluster) Stopper() *stop.Stopper {
return tc.stopper
}
// stopServers stops the stoppers for each individual server in the cluster.
// This method ensures that servers that were previously stopped explicitly are
// not double-stopped.
func (tc *TestCluster) stopServers() {
tc.mu.Lock()
defer tc.mu.Unlock()
// Quiesce the servers in parallel to avoid deadlocks. If we stop servers
// serially when we lose quorum (2 out of 3 servers have stopped) the last
// server may never finish due to waiting for a Raft command that can't
// commit due to the lack of quorum.
var wg sync.WaitGroup
wg.Add(len(tc.mu.serverStoppers))
for _, s := range tc.mu.serverStoppers {
go func(s *stop.Stopper) {
defer wg.Done()
if s != nil {
s.Quiesce(context.TODO())
}
}(s)
}
wg.Wait()
for i := range tc.mu.serverStoppers {
if tc.mu.serverStoppers[i] != nil {
tc.mu.serverStoppers[i].Stop(context.TODO())
tc.mu.serverStoppers[i] = nil
}
}
}
// StopServer stops an individual server in the cluster.
func (tc *TestCluster) StopServer(idx int) {
tc.mu.Lock()
defer tc.mu.Unlock()
if tc.mu.serverStoppers[idx] != nil {
tc.mu.serverStoppers[idx].Stop(context.TODO())
tc.mu.serverStoppers[idx] = nil
}
}
// StartTestCluster starts up a TestCluster made up of `nodes` in-memory testing
// servers.
// The cluster should be stopped using cluster.Stop().
func StartTestCluster(t testing.TB, nodes int, args base.TestClusterArgs) *TestCluster {
if nodes < 1 {
t.Fatal("invalid cluster size: ", nodes)
}
if err := checkServerArgsForCluster(
args.ServerArgs, args.ReplicationMode, disallowJoinAddr,
); err != nil {
t.Fatal(err)
}
for _, sargs := range args.ServerArgsPerNode {
if err := checkServerArgsForCluster(
sargs, args.ReplicationMode, disallowJoinAddr,
); err != nil {
t.Fatal(err)
}
}
tc := &TestCluster{
stopper: stop.NewStopper(),
replicationMode: args.ReplicationMode,
}
tc.stopper = stop.NewStopper()
for i := 0; i < nodes; i++ {
var serverArgs base.TestServerArgs
if perNodeServerArgs, ok := args.ServerArgsPerNode[i]; ok {
serverArgs = perNodeServerArgs
} else {
serverArgs = args.ServerArgs
}
// If there are multiple nodes, place them in different localities by
// default.
if nodes > 0 {
tiers := []roachpb.Tier{
{Key: "region", Value: "test"},
{Key: "dc", Value: fmt.Sprintf("dc%d", i+1)},
}
serverArgs.Locality = roachpb.Locality{Tiers: tiers}
}
if i > 0 {
serverArgs.JoinAddr = tc.Servers[0].ServingAddr()
}
if err := tc.doAddServer(t, serverArgs); err != nil {
t.Fatal(err)
}
// We want to wait for stores for each server in order to have predictable
// store IDs. Otherwise, stores can be asynchronously bootstrapped in an
// unexpected order (#22342).
tc.WaitForStores(t, tc.Servers[0].Gossip())
}
// Create a closer that will stop the individual server stoppers when the
// cluster stopper is stopped.
tc.stopper.AddCloser(stop.CloserFn(tc.stopServers))
if tc.replicationMode == base.ReplicationAuto {
if err := tc.WaitForFullReplication(); err != nil {
t.Fatal(err)
}
}
// Wait until a NodeStatus is persisted for every node (see #25488, #25649, #31574).
tc.WaitForNodeStatuses(t)
return tc
}
type checkType bool
const (
disallowJoinAddr checkType = false
allowJoinAddr checkType = true
)
// checkServerArgsForCluster sanity-checks TestServerArgs to work for a cluster
// with a given replicationMode.
func checkServerArgsForCluster(
args base.TestServerArgs, replicationMode base.TestClusterReplicationMode, checkType checkType,
) error {
if checkType == disallowJoinAddr && args.JoinAddr != "" {
return errors.Errorf("can't specify a join addr when starting a cluster: %s",
args.JoinAddr)
}
if args.Stopper != nil {
return errors.Errorf("can't set individual server stoppers when starting a cluster")
}
if args.Knobs.Store != nil {
storeKnobs := args.Knobs.Store.(*storage.StoreTestingKnobs)
if storeKnobs.DisableSplitQueue || storeKnobs.DisableReplicateQueue {
return errors.Errorf("can't disable an individual server's queues when starting a cluster; " +
"the cluster controls replication")
}
}
if replicationMode != base.ReplicationAuto && replicationMode != base.ReplicationManual {
return errors.Errorf("unexpected replication mode: %s", replicationMode)
}
return nil
}
// AddServer creates a server with the specified arguments and appends it to
// the TestCluster.
//
// The new Server's copy of serverArgs might be changed according to the
// cluster's ReplicationMode.
func (tc *TestCluster) AddServer(t testing.TB, serverArgs base.TestServerArgs) {
if serverArgs.JoinAddr == "" && len(tc.Servers) > 0 {
serverArgs.JoinAddr = tc.Servers[0].ServingAddr()
}
if err := tc.doAddServer(t, serverArgs); err != nil {
t.Fatal(err)
}
}
func (tc *TestCluster) doAddServer(t testing.TB, serverArgs base.TestServerArgs) error {
serverArgs.PartOfCluster = true
// Check args even though they might have been checked in StartTestCluster;
// this method might be called for servers being added after the cluster was
// started, in which case the check has not been performed.
if err := checkServerArgsForCluster(
serverArgs,
tc.replicationMode,
// Allow JoinAddr here; servers being added after the TestCluster has been
// started should have a JoinAddr filled in at this point.
allowJoinAddr,
); err != nil {
return err
}
serverArgs.Stopper = stop.NewStopper()
if tc.replicationMode == base.ReplicationManual {
var stkCopy storage.StoreTestingKnobs
if stk := serverArgs.Knobs.Store; stk != nil {
stkCopy = *stk.(*storage.StoreTestingKnobs)
}
stkCopy.DisableSplitQueue = true
stkCopy.DisableMergeQueue = true
stkCopy.DisableReplicateQueue = true
serverArgs.Knobs.Store = &stkCopy
}
s, conn, _ := serverutils.StartServer(t, serverArgs)
if tc.replicationMode == base.ReplicationManual && len(tc.Servers) == 0 {
// We've already disabled the merge queue via testing knobs above, but ALTER
// TABLE ... SPLIT AT will throw an error unless we also disable merges via
// the cluster setting.
//
// TODO(benesch): this won't be necessary once we have sticky bits for
// splits.
if _, err := conn.Exec(`SET CLUSTER SETTING kv.range_merge.queue_enabled = false`); err != nil {
t.Fatal(err)
}
}
// Disable LBS if the server being added has a scan interval arg this low.
if serverArgs.ScanInterval > 0 && serverArgs.ScanInterval <= 100*time.Millisecond {
if _, err := conn.Exec(`SET CLUSTER SETTING kv.range_split.by_load_enabled = false`); err != nil {
t.Fatal(err)
}
}
tc.Servers = append(tc.Servers, s.(*server.TestServer))
tc.Conns = append(tc.Conns, conn)
tc.mu.Lock()
tc.mu.serverStoppers = append(tc.mu.serverStoppers, serverArgs.Stopper)
tc.mu.Unlock()
return nil
}
// WaitForStores waits for all of the store descriptors to be gossiped. Servers
// other than the first "bootstrap" their stores asynchronously, but we'd like
// to wait for all of the stores to be initialized before returning the
// TestCluster.
func (tc *TestCluster) WaitForStores(t testing.TB, g *gossip.Gossip) {
// Register a gossip callback for the store descriptors.
var storesMu syncutil.Mutex
stores := map[roachpb.StoreID]struct{}{}
storesDone := make(chan error)
storesDoneOnce := storesDone
unregister := g.RegisterCallback(gossip.MakePrefixPattern(gossip.KeyStorePrefix),
func(_ string, content roachpb.Value) {
storesMu.Lock()
defer storesMu.Unlock()
if storesDoneOnce == nil {
return
}
var desc roachpb.StoreDescriptor
if err := content.GetProto(&desc); err != nil {
storesDoneOnce <- err
return
}
stores[desc.StoreID] = struct{}{}
if len(stores) == len(tc.Servers) {
close(storesDoneOnce)
storesDoneOnce = nil
}
})
defer unregister()
// Wait for the store descriptors to be gossiped.
for err := range storesDone {
if err != nil {
t.Fatal(err)
}
}
}
// LookupRange is part of TestClusterInterface.
func (tc *TestCluster) LookupRange(key roachpb.Key) (roachpb.RangeDescriptor, error) {
return tc.Servers[0].LookupRange(key)
}
// SplitRange splits the range containing splitKey.
// The right range created by the split starts at the split key and extends to the
// original range's end key.
// Returns the new descriptors of the left and right ranges.
//
// splitKey must correspond to a SQL table key (it must end with a family ID /
// col ID).
func (tc *TestCluster) SplitRange(
splitKey roachpb.Key,
) (roachpb.RangeDescriptor, roachpb.RangeDescriptor, error) {
return tc.Servers[0].SplitRange(splitKey)
}
// Target returns a ReplicationTarget for the specified server.
func (tc *TestCluster) Target(serverIdx int) roachpb.ReplicationTarget {
s := tc.Servers[serverIdx]
return roachpb.ReplicationTarget{
NodeID: s.GetNode().Descriptor.NodeID,
StoreID: s.GetFirstStoreID(),
}
}
func (tc *TestCluster) changeReplicas(
changeType roachpb.ReplicaChangeType, startKey roachpb.RKey, targets ...roachpb.ReplicationTarget,
) (roachpb.RangeDescriptor, error) {
ctx := context.TODO()
var beforeDesc roachpb.RangeDescriptor
if err := tc.Servers[0].DB().GetProto(
ctx, keys.RangeDescriptorKey(startKey), &beforeDesc,
); err != nil {
return roachpb.RangeDescriptor{}, errors.Wrap(err, "range descriptor lookup error")
}
desc, err := tc.Servers[0].DB().AdminChangeReplicas(
ctx, startKey.AsRawKey(), changeType, targets, beforeDesc,
)
if err != nil {
return roachpb.RangeDescriptor{}, errors.Wrap(err, "AdminChangeReplicas error")
}
return *desc, nil
}
// AddReplicas is part of TestClusterInterface.
func (tc *TestCluster) AddReplicas(
startKey roachpb.Key, targets ...roachpb.ReplicationTarget,
) (roachpb.RangeDescriptor, error) {
rKey := keys.MustAddr(startKey)
errRetry := errors.Errorf("target not found")
for {
rangeDesc, err := tc.changeReplicas(
roachpb.ADD_REPLICA, rKey, targets...,
)
if err != nil {
return roachpb.RangeDescriptor{}, err
}
// Wait for the replication to complete on all destination nodes.
if err := retry.ForDuration(time.Second*25, func() error {
for _, target := range targets {
// Use LookupReplica(keys) instead of GetRange(rangeID) to ensure that the
// snapshot has been transferred and the descriptor initialized.
store, err := tc.findMemberStore(target.StoreID)
if err != nil {
log.Errorf(context.TODO(), "unexpected error: %s", err)
return err
}
repl := store.LookupReplica(rKey)
if repl == nil {
return errors.Wrapf(errRetry, "for target %s", target)
}
desc := repl.Desc()
if _, ok := desc.GetReplicaDescriptor(target.StoreID); !ok {
return errors.Errorf("target store %d not yet in range descriptor %v", target.StoreID, desc)
}
}
return nil
}); errors.Cause(err) == errRetry {
log.Warningf(context.Background(), "target was likely downreplicated again; retrying after %s", err)
continue
} else if err != nil {
return roachpb.RangeDescriptor{}, err
}
return rangeDesc, nil
}
}
// RemoveReplicas is part of the TestServerInterface.
func (tc *TestCluster) RemoveReplicas(
startKey roachpb.Key, targets ...roachpb.ReplicationTarget,
) (roachpb.RangeDescriptor, error) {
return tc.changeReplicas(roachpb.REMOVE_REPLICA, keys.MustAddr(startKey), targets...)
}
// TransferRangeLease is part of the TestServerInterface.
func (tc *TestCluster) TransferRangeLease(
rangeDesc roachpb.RangeDescriptor, dest roachpb.ReplicationTarget,
) error {
err := tc.Servers[0].DB().AdminTransferLease(context.TODO(),
rangeDesc.StartKey.AsRawKey(), dest.StoreID)
if err != nil {
return errors.Wrapf(err, "%q: transfer lease unexpected error", rangeDesc.StartKey)
}
return nil
}
// FindRangeLease is similar to FindRangeLeaseHolder but returns a Lease proto
// without verifying if the lease is still active. Instead, it returns a time-
// stamp taken off the queried node's clock.
func (tc *TestCluster) FindRangeLease(
rangeDesc roachpb.RangeDescriptor, hint *roachpb.ReplicationTarget,
) (_ roachpb.Lease, now hlc.Timestamp, _ error) {
if hint != nil {
var ok bool
if _, ok = rangeDesc.GetReplicaDescriptor(hint.StoreID); !ok {
return roachpb.Lease{}, hlc.Timestamp{}, errors.Errorf(
"bad hint: %+v; store doesn't have a replica of the range", hint)
}
} else {
hint = &roachpb.ReplicationTarget{
NodeID: rangeDesc.Replicas().All()[0].NodeID,
StoreID: rangeDesc.Replicas().All()[0].StoreID}
}
// Find the server indicated by the hint and send a LeaseInfoRequest through
// it.
var hintServer *server.TestServer
for _, s := range tc.Servers {
if s.GetNode().Descriptor.NodeID == hint.NodeID {
hintServer = s
break
}
}
if hintServer == nil {
return roachpb.Lease{}, hlc.Timestamp{}, errors.Errorf("bad hint: %+v; no such node", hint)
}
return hintServer.GetRangeLease(context.TODO(), rangeDesc.StartKey.AsRawKey())
}
// FindRangeLeaseHolder is part of TestClusterInterface.
func (tc *TestCluster) FindRangeLeaseHolder(
rangeDesc roachpb.RangeDescriptor, hint *roachpb.ReplicationTarget,
) (roachpb.ReplicationTarget, error) {
lease, now, err := tc.FindRangeLease(rangeDesc, hint)
if err != nil {
return roachpb.ReplicationTarget{}, err
}
// Find lease replica in order to examine the lease state.
store, err := tc.findMemberStore(lease.Replica.StoreID)
if err != nil {
return roachpb.ReplicationTarget{}, err
}
replica, err := store.GetReplica(rangeDesc.RangeID)
if err != nil {
return roachpb.ReplicationTarget{}, err
}
if !replica.IsLeaseValid(lease, now) {
return roachpb.ReplicationTarget{}, errors.New("no valid lease")
}
replicaDesc := lease.Replica
return roachpb.ReplicationTarget{NodeID: replicaDesc.NodeID, StoreID: replicaDesc.StoreID}, nil
}
// WaitForSplitAndReplication waits for a range which starts with
// startKey and then verifies that each replica in the range
// descriptor has been created.
func (tc *TestCluster) WaitForSplitAndReplication(startKey roachpb.Key) error {
return retry.ForDuration(testutils.DefaultSucceedsSoonDuration, func() error {
desc, err := tc.LookupRange(startKey)
if err != nil {
return errors.Wrapf(err, "unable to lookup range for %s", startKey)
}
// Verify the split first.
if !desc.StartKey.Equal(startKey) {
return errors.Errorf("expected range start key %s; got %s",
startKey, desc.StartKey)
}
// A learner replicas is still up-replicating, so if we have any, we're not
// replicated yet.
if learnerReplicas := desc.Replicas().Learners(); len(learnerReplicas) > 0 {
return errors.Errorf("have %d learners, still replicating %s", len(learnerReplicas), desc)
}
// Once we've verified the split and that there aren't any learners, make
// sure that the voter replicas exist.
for _, rDesc := range desc.Replicas().Voters() {
store, err := tc.findMemberStore(rDesc.StoreID)
if err != nil {
return err
}
repl, err := store.GetReplica(desc.RangeID)
if err != nil {
return err
}
actualReplicaDesc, err := repl.GetReplicaDescriptor()
if err != nil {
return err
}
if actualReplicaDesc != rDesc {
return errors.Errorf("expected replica %s; got %s", rDesc, actualReplicaDesc)
}
}
return nil
})
}
// findMemberStore returns the store containing a given replica.
func (tc *TestCluster) findMemberStore(storeID roachpb.StoreID) (*storage.Store, error) {
for _, server := range tc.Servers {
if server.Stores().HasStore(storeID) {
store, err := server.Stores().GetStore(storeID)
if err != nil {
return nil, err
}
return store, nil
}
}
return nil, errors.Errorf("store not found")
}
// WaitForFullReplication waits until all stores in the cluster
// have no ranges with replication pending.
//
// TODO(andrei): This method takes inexplicably long.
// I think it shouldn't need any retries. See #38565.
func (tc *TestCluster) WaitForFullReplication() error {
start := timeutil.Now()
defer func() {
end := timeutil.Now()
log.Infof(context.TODO(), "WaitForFullReplication took: %s", end.Sub(start))
}()
if len(tc.Servers) < 3 {
// If we have less than three nodes, we will never have full replication.
return nil
}
opts := retry.Options{
InitialBackoff: time.Millisecond * 10,
MaxBackoff: time.Millisecond * 100,
Multiplier: 2,
}
notReplicated := true
for r := retry.Start(opts); r.Next() && notReplicated; {
notReplicated = false
for _, s := range tc.Servers {
err := s.Stores().VisitStores(func(s *storage.Store) error {
if n := s.ClusterNodeCount(); n != len(tc.Servers) {
log.Infof(context.TODO(), "%s only sees %d/%d available nodes", s, n, len(tc.Servers))
notReplicated = true
return nil
}
// Force upreplication. Otherwise, if we rely on the scanner to do it,
// it'll take a while.
if err := s.ForceReplicationScanAndProcess(); err != nil {
return err
}
if err := s.ComputeMetrics(context.TODO(), 0); err != nil {
// This can sometimes fail since ComputeMetrics calls
// updateReplicationGauges which needs the system config gossiped.
log.Info(context.TODO(), err)
notReplicated = true
return nil
}
if n := s.Metrics().UnderReplicatedRangeCount.Value(); n > 0 {
log.Infof(context.TODO(), "%s has %d underreplicated ranges", s, n)
notReplicated = true
}
return nil
})
if err != nil {
return err
}
if notReplicated {
break
}
}
}
return nil
}
// WaitForNodeStatuses waits until a NodeStatus is persisted for every node and
// store in the cluster.
func (tc *TestCluster) WaitForNodeStatuses(t testing.TB) {
testutils.SucceedsSoon(t, func() error {
url := tc.Server(0).ServingAddr()
nodeID := tc.Server(0).NodeID()
conn, err := tc.Server(0).RPCContext().GRPCDialNode(url, nodeID).Connect(context.Background())
if err != nil {
return err
}
client := serverpb.NewStatusClient(conn)
response, err := client.Nodes(context.Background(), &serverpb.NodesRequest{})
if err != nil {
return err
}
if len(response.Nodes) < tc.NumServers() {
return fmt.Errorf("expected %d nodes registered, got %+v", tc.NumServers(), response)
}
// Check that all the nodes in the testcluster have a status. We tolerate
// other nodes having statuses (in some tests the cluster is configured with
// a pre-existing store).
nodeIDs := make(map[roachpb.NodeID]bool)
for _, node := range response.Nodes {
if len(node.StoreStatuses) == 0 {
return fmt.Errorf("missing StoreStatuses in NodeStatus: %+v", node)
}
nodeIDs[node.Desc.NodeID] = true
}
for _, s := range tc.Servers {
if id := s.GetNode().Descriptor.NodeID; !nodeIDs[id] {
return fmt.Errorf("missing n%d in NodeStatus: %+v", id, response)
}
}
return nil
})
}
// WaitForNodeLiveness waits until a liveness record is persisted for every
// node in the cluster.
func (tc *TestCluster) WaitForNodeLiveness(t testing.TB) {
testutils.SucceedsSoon(t, func() error {
db := tc.Servers[0].DB()
for _, s := range tc.Servers {
key := keys.NodeLivenessKey(s.NodeID())
var liveness storagepb.Liveness
if err := db.GetProto(context.Background(), key, &liveness); err != nil {
return err
}
if (liveness == storagepb.Liveness{}) {
return fmt.Errorf("no liveness record")
}
fmt.Printf("n%d: found liveness\n", s.NodeID())
}
return nil
})
}
// ReplicationMode implements TestClusterInterface.
func (tc *TestCluster) ReplicationMode() base.TestClusterReplicationMode {
return tc.replicationMode
}
type testClusterFactoryImpl struct{}
// TestClusterFactory can be passed to serverutils.InitTestClusterFactory
var TestClusterFactory serverutils.TestClusterFactory = testClusterFactoryImpl{}
// StartTestCluster is part of TestClusterFactory interface.
func (testClusterFactoryImpl) StartTestCluster(
t testing.TB, numNodes int, args base.TestClusterArgs,
) serverutils.TestClusterInterface {
return StartTestCluster(t, numNodes, args)
}