-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
testserver.go
717 lines (640 loc) · 21.6 KB
/
testserver.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
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
// Copyright 2014 The Cockroach 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 server
import (
"fmt"
"net/http"
"net/http/cookiejar"
"net/url"
"sync"
"time"
"github.com/pkg/errors"
"golang.org/x/net/context"
"github.com/cockroachdb/cockroach/pkg/base"
"github.com/cockroachdb/cockroach/pkg/config"
"github.com/cockroachdb/cockroach/pkg/gossip"
"github.com/cockroachdb/cockroach/pkg/internal/client"
"github.com/cockroachdb/cockroach/pkg/keys"
"github.com/cockroachdb/cockroach/pkg/kv"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/rpc"
"github.com/cockroachdb/cockroach/pkg/security"
"github.com/cockroachdb/cockroach/pkg/server/serverpb"
"github.com/cockroachdb/cockroach/pkg/settings/cluster"
"github.com/cockroachdb/cockroach/pkg/sql/distsqlplan"
"github.com/cockroachdb/cockroach/pkg/sql/pgwire"
migrations "github.com/cockroachdb/cockroach/pkg/sqlmigrations"
"github.com/cockroachdb/cockroach/pkg/storage"
"github.com/cockroachdb/cockroach/pkg/storage/engine"
"github.com/cockroachdb/cockroach/pkg/ts"
"github.com/cockroachdb/cockroach/pkg/util"
"github.com/cockroachdb/cockroach/pkg/util/hlc"
"github.com/cockroachdb/cockroach/pkg/util/metric"
"github.com/cockroachdb/cockroach/pkg/util/stop"
)
const (
// TestUser is a fixed user used in unittests.
// It has valid embedded client certs.
TestUser = "testuser"
// initialSplitsTimeout is the amount of time to wait for initial splits to
// occur on a freshly started server.
// Note: this needs to be fairly high or tests become flaky.
initialSplitsTimeout = 10 * time.Second
)
// makeTestConfig returns a config for testing. It overrides the
// Certs with the test certs directory.
// We need to override the certs loader.
func makeTestConfig(st *cluster.Settings) Config {
cfg := MakeConfig(st)
// Test servers start in secure mode by default.
cfg.Insecure = false
// Override the DistSQL local store with an in-memory store.
cfg.TempStoreSpec = base.DefaultTestStoreSpec
// Load test certs. In addition, the tests requiring certs
// need to call security.SetAssetLoader(securitytest.EmbeddedAssets)
// in their init to mock out the file system calls for calls to AssetFS,
// which has the test certs compiled in. Typically this is done
// once per package, in main_test.go.
cfg.SSLCertsDir = security.EmbeddedCertsDir
// Addr defaults to localhost with port set at time of call to
// Start() to an available port. May be overridden later (as in
// makeTestConfigFromParams). Call TestServer.ServingAddr() for the
// full address (including bound port).
cfg.Addr = util.TestAddr.String()
cfg.AdvertiseAddr = util.TestAddr.String()
cfg.HTTPAddr = util.TestAddr.String()
// Set standard user for intra-cluster traffic.
cfg.User = security.NodeUser
cfg.MetricsSampleInterval = metric.TestSampleInterval
// Enable web session authentication.
cfg.EnableWebSessionAuthentication = true
return cfg
}
// makeTestConfigFromParams creates a Config from a TestServerParams.
func makeTestConfigFromParams(params base.TestServerArgs) Config {
st := params.Settings
if params.Settings == nil {
st = cluster.MakeClusterSettings(cluster.BinaryMinimumSupportedVersion, cluster.BinaryServerVersion)
}
cfg := makeTestConfig(st)
cfg.TestingKnobs = params.Knobs
cfg.RaftConfig = params.RaftConfig
cfg.RaftConfig.SetDefaults()
if params.JoinAddr != "" {
cfg.JoinList = []string{params.JoinAddr}
}
cfg.Insecure = params.Insecure
cfg.SocketFile = params.SocketFile
cfg.RetryOptions = params.RetryOptions
if params.MetricsSampleInterval != 0 {
cfg.MetricsSampleInterval = params.MetricsSampleInterval
}
if knobs := params.Knobs.Store; knobs != nil {
if mo := knobs.(*storage.StoreTestingKnobs).MaxOffset; mo != 0 {
cfg.MaxOffset = MaxOffsetType(mo)
}
}
if params.ScanInterval != 0 {
cfg.ScanInterval = params.ScanInterval
}
if params.ScanMaxIdleTime != 0 {
cfg.ScanMaxIdleTime = params.ScanMaxIdleTime
}
if params.SSLCertsDir != "" {
cfg.SSLCertsDir = params.SSLCertsDir
}
if params.TimeSeriesQueryWorkerMax != 0 {
cfg.TimeSeriesServerConfig.QueryWorkerMax = params.TimeSeriesQueryWorkerMax
}
if params.DisableEventLog {
cfg.EventLogEnabled = false
}
if params.SQLMemoryPoolSize != 0 {
cfg.SQLMemoryPoolSize = params.SQLMemoryPoolSize
}
cfg.JoinList = []string{params.JoinAddr}
if cfg.Insecure {
// Whenever we can (i.e. in insecure mode), use IsolatedTestAddr
// to prevent issues that can occur when running a test under
// stress.
cfg.Addr = util.IsolatedTestAddr.String()
cfg.AdvertiseAddr = util.IsolatedTestAddr.String()
cfg.HTTPAddr = util.IsolatedTestAddr.String()
} else {
cfg.Addr = util.TestAddr.String()
cfg.AdvertiseAddr = util.TestAddr.String()
cfg.HTTPAddr = util.TestAddr.String()
}
if params.Addr != "" {
cfg.Addr = params.Addr
cfg.AdvertiseAddr = params.Addr
}
if params.HTTPAddr != "" {
cfg.HTTPAddr = params.HTTPAddr
}
if params.ListeningURLFile != "" {
cfg.ListeningURLFile = params.ListeningURLFile
}
if params.DisableWebSessionAuthentication {
cfg.EnableWebSessionAuthentication = false
}
// Ensure we have the correct number of engines. Add in-memory ones where
// needed. There must be at least one store/engine.
if len(params.StoreSpecs) == 0 {
params.StoreSpecs = []base.StoreSpec{base.DefaultTestStoreSpec}
}
// Validate the store specs.
for _, storeSpec := range params.StoreSpecs {
if storeSpec.InMemory {
if storeSpec.SizePercent > 0 {
panic(fmt.Sprintf("test server does not yet support in memory stores based on percentage of total memory: %s", storeSpec))
}
}
// The default store spec is in-memory, so if this one is on-disk then
// one specific test must have requested it. A failure is returned if
// the Path field is empty, which means the test is then forced to pick
// the dir (and the test is then responsible for cleaning it up, not
// TestServer).
}
// Copy over the store specs.
cfg.Stores = base.StoreSpecList{Specs: params.StoreSpecs}
if cfg.TestingKnobs.Store == nil {
cfg.TestingKnobs.Store = &storage.StoreTestingKnobs{}
}
cfg.TestingKnobs.Store.(*storage.StoreTestingKnobs).SkipMinSizeCheck = true
return cfg
}
// A TestServer encapsulates an in-memory instantiation of a cockroach node with
// a single store. It provides tests with access to Server internals.
// Where possible, it should be used through the
// testingshim.TestServerInterface.
//
// Example usage of a TestServer:
//
// s, db, kvDB := serverutils.StartServer(t, base.TestServerArgs{})
// defer s.Stopper().Stop()
// // If really needed, in tests that can depend on server, downcast to
// // server.TestServer:
// ts := s.(*server.TestServer)
//
type TestServer struct {
Cfg *Config
// server is the embedded Cockroach server struct.
*Server
// authClient is an http.Client that has been authenticated to access the
// Admin UI.
authClient struct {
httpClient http.Client
once sync.Once
err error
}
}
// Stopper returns the embedded server's Stopper.
func (ts *TestServer) Stopper() *stop.Stopper {
return ts.stopper
}
// Gossip returns the gossip instance used by the TestServer.
func (ts *TestServer) Gossip() *gossip.Gossip {
if ts != nil {
return ts.gossip
}
return nil
}
// Clock returns the clock used by the TestServer.
func (ts *TestServer) Clock() *hlc.Clock {
if ts != nil {
return ts.clock
}
return nil
}
// JobRegistry returns the *jobs.Registry as an interface{}.
func (ts *TestServer) JobRegistry() interface{} {
if ts != nil {
return ts.jobRegistry
}
return nil
}
// RPCContext returns the rpc context used by the TestServer.
func (ts *TestServer) RPCContext() *rpc.Context {
if ts != nil {
return ts.rpcContext
}
return nil
}
// TsDB returns the ts.DB instance used by the TestServer.
func (ts *TestServer) TsDB() *ts.DB {
if ts != nil {
return ts.tsDB
}
return nil
}
// DB returns the client.DB instance used by the TestServer.
func (ts *TestServer) DB() *client.DB {
if ts != nil {
return ts.db
}
return nil
}
// PGServer returns the pgwire.Server used by the TestServer.
func (ts *TestServer) PGServer() *pgwire.Server {
if ts != nil {
return ts.pgServer
}
return nil
}
// Start starts the TestServer by bootstrapping an in-memory store
// (defaults to maximum of 100M). The server is started, launching the
// node RPC server and all HTTP endpoints. Use the value of
// TestServer.ServingAddr() after Start() for client connections.
// Use TestServer.Stopper().Stop() to shutdown the server after the test
// completes.
func (ts *TestServer) Start(params base.TestServerArgs) error {
if ts.Cfg == nil {
panic("Cfg not set")
}
if params.Stopper == nil {
params.Stopper = stop.NewStopper()
}
// TODO(andrei): Running two TestServers concurrently with
// PartOfCluster==false can result in the default zone config not be reset
// properly. It would be nice if this were more robust.
if !params.PartOfCluster {
// Change the replication requirements so we don't get log spam about ranges
// not being replicated enough.
cfg := config.DefaultZoneConfig()
cfg.NumReplicas = 1
fn := config.TestingSetDefaultZoneConfig(cfg)
params.Stopper.AddCloser(stop.CloserFn(fn))
}
// Needs to be called before NewServer to ensure resolvers are initialized.
if err := ts.Cfg.InitNode(); err != nil {
return err
}
var err error
ts.Server, err = NewServer(*ts.Cfg, params.Stopper)
if err != nil {
return err
}
// Our context must be shared with our server.
ts.Cfg = &ts.Server.cfg
if err := ts.Server.Start(context.Background()); err != nil {
return err
}
// If enabled, wait for initial splits to complete before returning control.
// If initial splits do not complete, the server is stopped before
// returning.
if stk, ok := ts.cfg.TestingKnobs.Store.(*storage.StoreTestingKnobs); ok &&
stk.DisableSplitQueue {
return nil
}
if err := ts.WaitForInitialSplits(); err != nil {
ts.Stop()
return err
}
return nil
}
// ExpectedInitialRangeCount returns the expected number of ranges that should
// be on the server after initial (asynchronous) splits have been completed,
// assuming no additional information is added outside of the normal bootstrap
// process.
func (ts *TestServer) ExpectedInitialRangeCount() (int, error) {
return ExpectedInitialRangeCount(ts.DB())
}
// ExpectedInitialRangeCount returns the expected number of ranges that should
// be on the server after initial (asynchronous) splits have been completed,
// assuming no additional information is added outside of the normal bootstrap
// process.
func ExpectedInitialRangeCount(db *client.DB) (int, error) {
_, migrationRangeCount, err := migrations.AdditionalInitialDescriptors(
context.Background(), db)
if err != nil {
return 0, errors.Wrap(err, "counting initial migration ranges")
}
return GetBootstrapSchema().InitialRangeCount() + migrationRangeCount, nil
}
// WaitForInitialSplits waits for the server to complete its expected initial
// splits at startup. If the expected range count is not reached within a
// configured timeout, an error is returned.
func (ts *TestServer) WaitForInitialSplits() error {
return WaitForInitialSplits(ts.DB())
}
// WaitForInitialSplits waits for the expected number of initial ranges to be
// populated in the meta2 table. If the expected range count is not reached
// within a configured timeout, an error is returned.
func WaitForInitialSplits(db *client.DB) error {
expectedRanges, err := ExpectedInitialRangeCount(db)
if err != nil {
return err
}
return util.RetryForDuration(initialSplitsTimeout, func() error {
// Scan all keys in the Meta2Prefix; we only need a count.
rows, err := db.Scan(context.TODO(), keys.Meta2Prefix, keys.MetaMax, 0)
if err != nil {
return err
}
if a, e := len(rows), expectedRanges; a != e {
return errors.Errorf("had %d ranges at startup, expected %d", a, e)
}
return nil
})
}
// Stores returns the collection of stores from this TestServer's node.
func (ts *TestServer) Stores() *storage.Stores {
return ts.node.stores
}
// GetStores is part of TestServerInterface.
func (ts *TestServer) GetStores() interface{} {
return ts.node.stores
}
// ClusterSettings returns the ClusterSettings.
func (ts *TestServer) ClusterSettings() *cluster.Settings {
return ts.Cfg.Settings
}
// Engines returns the TestServer's engines.
func (ts *TestServer) Engines() []engine.Engine {
return ts.engines
}
// ServingAddr returns the server's address. Should be used by clients.
func (ts *TestServer) ServingAddr() string {
return ts.cfg.AdvertiseAddr
}
// HTTPAddr returns the server's HTTP address. Should be used by clients.
func (ts *TestServer) HTTPAddr() string {
return ts.cfg.HTTPAddr
}
// Addr returns the server's listening address.
func (ts *TestServer) Addr() string {
return ts.cfg.Addr
}
// WriteSummaries implements TestServerInterface.
func (ts *TestServer) WriteSummaries() error {
return ts.node.writeSummaries(context.TODO())
}
// AdminURL implements TestServerInterface.
func (ts *TestServer) AdminURL() string {
return ts.Cfg.AdminURL().String()
}
// GetHTTPClient implements TestServerInterface.
func (ts *TestServer) GetHTTPClient() (http.Client, error) {
return ts.Cfg.GetHTTPClient()
}
// GetAuthenticatedHTTPClient implements TestServerInterface.
func (ts *TestServer) GetAuthenticatedHTTPClient() (http.Client, error) {
ts.authClient.once.Do(func() {
// Create an authentication session for an arbitrary user. We do not
// currently have an authorization mechanism, so a specific user is not
// necessary.
ts.authClient.err = func() error {
id, secret, err := ts.authentication.newAuthSession(context.TODO(), "authentic_user")
if err != nil {
return err
}
// Encode a session cookie and store it in a cookie jar.
cookie, err := encodeSessionCookie(&serverpb.SessionCookie{
ID: id,
Secret: secret,
})
if err != nil {
return err
}
cookieJar, err := cookiejar.New(nil)
if err != nil {
return err
}
url, err := url.Parse(ts.AdminURL())
if err != nil {
return err
}
cookieJar.SetCookies(url, []*http.Cookie{cookie})
// Create an httpClient and attach the cookie jar to the client.
ts.authClient.httpClient, err = ts.Cfg.GetHTTPClient()
if err != nil {
return err
}
ts.authClient.httpClient.Jar = cookieJar
return nil
}()
})
return ts.authClient.httpClient, ts.authClient.err
}
// MustGetSQLCounter implements TestServerInterface.
func (ts *TestServer) MustGetSQLCounter(name string) int64 {
var c int64
var found bool
ts.registry.Each(func(n string, v interface{}) {
if name == n {
c = v.(*metric.Counter).Count()
found = true
}
})
if !found {
panic(fmt.Sprintf("couldn't find metric %s", name))
}
return c
}
// MustGetSQLNetworkCounter implements TestServerInterface.
func (ts *TestServer) MustGetSQLNetworkCounter(name string) int64 {
var c int64
var found bool
reg := metric.NewRegistry()
reg.AddMetricStruct(ts.pgServer.Metrics())
reg.Each(func(n string, v interface{}) {
if name == n {
c = v.(*metric.Counter).Count()
found = true
}
})
if !found {
panic(fmt.Sprintf("couldn't find metric %s", name))
}
return c
}
// KVClient is part of TestServerInterface.
func (ts *TestServer) KVClient() interface{} { return ts.db }
// KVDB is part of TestServerInterface.
func (ts *TestServer) KVDB() interface{} { return ts.kvDB }
// LeaseManager is part of TestServerInterface.
func (ts *TestServer) LeaseManager() interface{} {
return ts.leaseMgr
}
// Executor is part of TestServerInterface.
func (ts *TestServer) Executor() interface{} {
return ts.sqlExecutor
}
// GetNode exposes the Server's Node.
func (ts *TestServer) GetNode() *Node {
return ts.node
}
// DistSender exposes the Server's DistSender.
func (ts *TestServer) DistSender() *kv.DistSender {
return ts.distSender
}
// DistSQLServer is part of TestServerInterface.
func (ts *TestServer) DistSQLServer() interface{} {
return ts.distSQLServer
}
// SetDistSQLSpanResolver is part of TestServerInterface.
func (ts *Server) SetDistSQLSpanResolver(spanResolver interface{}) {
ts.sqlExecutor.SetDistSQLSpanResolver(spanResolver.(distsqlplan.SpanResolver))
}
// GetFirstStoreID is part of TestServerInterface.
func (ts *TestServer) GetFirstStoreID() roachpb.StoreID {
firstStoreID := roachpb.StoreID(-1)
err := ts.Stores().VisitStores(func(s *storage.Store) error {
if firstStoreID == -1 {
firstStoreID = s.Ident.StoreID
}
return nil
})
if err != nil {
panic(err)
}
return firstStoreID
}
// LookupRange returns the descriptor of the range containing key.
func (ts *TestServer) LookupRange(key roachpb.Key) (roachpb.RangeDescriptor, error) {
rangeLookupReq := roachpb.RangeLookupRequest{
Span: roachpb.Span{
Key: keys.RangeMetaKey(keys.MustAddr(key)),
},
MaxRanges: 1,
}
resp, pErr := client.SendWrapped(context.Background(), ts.DistSender(), &rangeLookupReq)
if pErr != nil {
return roachpb.RangeDescriptor{}, errors.Errorf(
"%q: lookup range unexpected error: %s", key, pErr)
}
return resp.(*roachpb.RangeLookupResponse).Ranges[0], nil
}
// 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 (ts *TestServer) SplitRange(
splitKey roachpb.Key,
) (roachpb.RangeDescriptor, roachpb.RangeDescriptor, error) {
ctx := context.Background()
splitRKey, err := keys.Addr(splitKey)
if err != nil {
return roachpb.RangeDescriptor{}, roachpb.RangeDescriptor{}, err
}
splitReq := roachpb.AdminSplitRequest{
Span: roachpb.Span{
Key: splitKey,
},
SplitKey: splitKey,
}
_, pErr := client.SendWrapped(ctx, ts.DistSender(), &splitReq)
if pErr != nil {
return roachpb.RangeDescriptor{}, roachpb.RangeDescriptor{},
errors.Errorf(
"%q: split unexpected error: %s", splitReq.SplitKey, pErr)
}
// The split point may not be exactly at the key we requested (we request
// splits at valid table keys, and the split point corresponds to the row's
// prefix). We scan for the range that includes the key we requested and the
// one that precedes it.
// We use a transaction so that we get consistent results between the two
// scans (in case there are other splits happening).
var leftRangeDesc, rightRangeDesc roachpb.RangeDescriptor
// Errors returned from scanMeta cannot be wrapped or retryable errors won't
// be retried. Instead, the message to wrap is stored in case of
// non-retryable failures and then wrapped when the full transaction fails.
var wrappedMsg string
if err := ts.DB().Txn(ctx, func(ctx context.Context, txn *client.Txn) error {
scanMeta := func(key roachpb.RKey, reverse bool) (desc roachpb.RangeDescriptor, err error) {
var kvs []client.KeyValue
if reverse {
// Find the last range that ends at or before key.
kvs, err = txn.ReverseScan(
ctx, keys.Meta2Prefix, keys.RangeMetaKey(key.Next()), 1, /* one result */
)
} else {
// Find the first range that ends after key.
kvs, err = txn.Scan(
ctx, keys.RangeMetaKey(key.Next()), keys.Meta2Prefix.PrefixEnd(), 1, /* one result */
)
}
if err != nil {
return desc, err
}
if len(kvs) != 1 {
return desc, fmt.Errorf("expected 1 result, got %d", len(kvs))
}
err = kvs[0].ValueProto(&desc)
return desc, err
}
rightRangeDesc, err = scanMeta(splitRKey, false /* !reverse */)
if err != nil {
wrappedMsg = "could not look up right-hand side descriptor"
return err
}
leftRangeDesc, err = scanMeta(splitRKey, true /* reverse */)
if err != nil {
wrappedMsg = "could not look up left-hand side descriptor"
return err
}
if !leftRangeDesc.EndKey.Equal(rightRangeDesc.StartKey) {
return errors.Errorf(
"inconsistent left (%v) and right (%v) descriptors", leftRangeDesc, rightRangeDesc,
)
}
return nil
}); err != nil {
if len(wrappedMsg) > 0 {
return roachpb.RangeDescriptor{}, roachpb.RangeDescriptor{}, errors.Wrap(err, wrappedMsg)
}
return roachpb.RangeDescriptor{}, roachpb.RangeDescriptor{}, err
}
return leftRangeDesc, rightRangeDesc, nil
}
// GetRangeLease returns the current lease for the range containing key, and a
// timestamp taken from the node.
//
// The lease is returned regardless of its status.
func (ts *TestServer) GetRangeLease(
ctx context.Context, key roachpb.Key,
) (_ roachpb.Lease, now hlc.Timestamp, _ error) {
leaseReq := roachpb.LeaseInfoRequest{
Span: roachpb.Span{
Key: key,
},
}
leaseResp, pErr := client.SendWrappedWith(
ctx,
ts.DB().GetSender(),
roachpb.Header{
// INCONSISTENT read, since we want to make sure that the node used to
// send this is the one that processes the command, for the hint to
// matter.
ReadConsistency: roachpb.INCONSISTENT,
},
&leaseReq,
)
if pErr != nil {
return roachpb.Lease{}, hlc.Timestamp{}, pErr.GoError()
}
return leaseResp.(*roachpb.LeaseInfoResponse).Lease, ts.Clock().Now(), nil
}
type testServerFactoryImpl struct{}
// TestServerFactory can be passed to serverutils.InitTestServerFactory
var TestServerFactory = testServerFactoryImpl{}
// New is part of TestServerFactory interface.
func (testServerFactoryImpl) New(params base.TestServerArgs) interface{} {
cfg := makeTestConfigFromParams(params)
return &TestServer{Cfg: &cfg}
}