-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
testserver.go
1294 lines (1167 loc) · 40.3 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
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2014 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 server
import (
"context"
"fmt"
"net/http"
"net/http/cookiejar"
"net/url"
"path/filepath"
"sync"
"time"
"github.com/cenkalti/backoff"
circuit "github.com/cockroachdb/circuitbreaker"
"github.com/cockroachdb/cockroach/pkg/base"
"github.com/cockroachdb/cockroach/pkg/config"
"github.com/cockroachdb/cockroach/pkg/config/zonepb"
"github.com/cockroachdb/cockroach/pkg/gossip"
"github.com/cockroachdb/cockroach/pkg/jobs"
"github.com/cockroachdb/cockroach/pkg/keys"
"github.com/cockroachdb/cockroach/pkg/kv"
"github.com/cockroachdb/cockroach/pkg/kv/kvclient/kvcoord"
"github.com/cockroachdb/cockroach/pkg/kv/kvclient/kvtenant"
"github.com/cockroachdb/cockroach/pkg/kv/kvserver"
"github.com/cockroachdb/cockroach/pkg/kv/kvserver/protectedts"
"github.com/cockroachdb/cockroach/pkg/kv/kvserver/protectedts/ptpb"
"github.com/cockroachdb/cockroach/pkg/kv/kvserver/protectedts/ptprovider"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/rpc"
"github.com/cockroachdb/cockroach/pkg/rpc/nodedialer"
"github.com/cockroachdb/cockroach/pkg/security"
"github.com/cockroachdb/cockroach/pkg/server/debug"
"github.com/cockroachdb/cockroach/pkg/server/serverpb"
"github.com/cockroachdb/cockroach/pkg/server/status"
"github.com/cockroachdb/cockroach/pkg/settings/cluster"
"github.com/cockroachdb/cockroach/pkg/sql"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb"
"github.com/cockroachdb/cockroach/pkg/sql/optionalnodeliveness"
"github.com/cockroachdb/cockroach/pkg/sql/pgwire"
"github.com/cockroachdb/cockroach/pkg/sql/physicalplan"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/sql/sessiondata"
"github.com/cockroachdb/cockroach/pkg/sqlmigrations"
"github.com/cockroachdb/cockroach/pkg/storage"
"github.com/cockroachdb/cockroach/pkg/storage/cloud"
"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/log"
"github.com/cockroachdb/cockroach/pkg/util/metric"
"github.com/cockroachdb/cockroach/pkg/util/netutil"
"github.com/cockroachdb/cockroach/pkg/util/stop"
"github.com/cockroachdb/cockroach/pkg/util/timeutil"
"github.com/cockroachdb/errors"
"github.com/gogo/protobuf/proto"
"google.golang.org/grpc"
)
const (
// TestUser is a fixed user used in unittests.
// It has valid embedded client certs.
TestUser = "testuser"
)
// 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 {
return Config{
BaseConfig: makeTestBaseConfig(st),
KVConfig: makeTestKVConfig(),
SQLConfig: makeTestSQLConfig(st, roachpb.SystemTenantID),
}
}
func makeTestBaseConfig(st *cluster.Settings) BaseConfig {
baseCfg := MakeBaseConfig(st)
// Test servers start in secure mode by default.
baseCfg.Insecure = false
// Configure test storage engine.
baseCfg.StorageEngine = storage.DefaultStorageEngine
// 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.
baseCfg.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.ServingRPCAddr() and
// .ServingSQLAddr() for the full address (including bound port).
baseCfg.Addr = util.TestAddr.String()
baseCfg.AdvertiseAddr = util.TestAddr.String()
baseCfg.SQLAddr = util.TestAddr.String()
baseCfg.SQLAdvertiseAddr = util.TestAddr.String()
baseCfg.SplitListenSQL = true
baseCfg.HTTPAddr = util.TestAddr.String()
// Set standard user for intra-cluster traffic.
baseCfg.User = security.NodeUser
return baseCfg
}
func makeTestKVConfig() KVConfig {
kvCfg := MakeKVConfig(base.DefaultTestStoreSpec)
// Enable web session authentication.
kvCfg.EnableWebSessionAuthentication = true
return kvCfg
}
func makeTestSQLConfig(st *cluster.Settings, tenID roachpb.TenantID) SQLConfig {
sqlCfg := MakeSQLConfig(tenID, base.DefaultTestTempStorageConfig(st))
// Configure the default in-memory temp storage for all tests unless
// otherwise configured.
sqlCfg.TempStorageConfig = base.DefaultTestTempStorageConfig(st)
return sqlCfg
}
// makeTestConfigFromParams creates a Config from a TestServerParams.
func makeTestConfigFromParams(params base.TestServerArgs) Config {
st := params.Settings
if params.Settings == nil {
st = cluster.MakeClusterSettings()
}
st.ExternalIODir = params.ExternalIODir
cfg := makeTestConfig(st)
cfg.TestingKnobs = params.Knobs
cfg.RaftConfig = params.RaftConfig
cfg.RaftConfig.SetDefaults()
if params.LeaseManagerConfig != nil {
cfg.LeaseManagerConfig = params.LeaseManagerConfig
} else {
cfg.LeaseManagerConfig = base.NewLeaseManagerConfig()
}
if params.JoinAddr != "" {
cfg.JoinList = []string{params.JoinAddr}
}
cfg.ClusterName = params.ClusterName
cfg.Insecure = params.Insecure
cfg.AutoInitializeCluster = !params.NoAutoInitializeCluster
cfg.SocketFile = params.SocketFile
cfg.RetryOptions = params.RetryOptions
cfg.Locality = params.Locality
if knobs := params.Knobs.Store; knobs != nil {
if mo := knobs.(*kvserver.StoreTestingKnobs).MaxOffset; mo != 0 {
cfg.MaxOffset = MaxOffsetType(mo)
}
}
if params.Knobs.Server != nil {
if zoneConfig := params.Knobs.Server.(*TestingKnobs).DefaultZoneConfigOverride; zoneConfig != nil {
cfg.DefaultZoneConfig = *zoneConfig
}
if systemZoneConfig := params.Knobs.Server.(*TestingKnobs).DefaultSystemZoneConfigOverride; systemZoneConfig != nil {
cfg.DefaultSystemZoneConfig = *systemZoneConfig
}
}
if params.ScanInterval != 0 {
cfg.ScanInterval = params.ScanInterval
}
if params.ScanMinIdleTime != 0 {
cfg.ScanMinIdleTime = params.ScanMinIdleTime
}
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.TimeSeriesQueryMemoryBudget != 0 {
cfg.TimeSeriesServerConfig.QueryMemoryMax = params.TimeSeriesQueryMemoryBudget
}
if params.DisableEventLog {
cfg.EventLogEnabled = false
}
if params.SQLMemoryPoolSize != 0 {
cfg.MemoryPoolSize = params.SQLMemoryPoolSize
}
if params.CacheSize != 0 {
cfg.CacheSize = params.CacheSize
}
if params.JoinAddr != "" {
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.SQLAddr = util.IsolatedTestAddr.String()
cfg.SQLAdvertiseAddr = util.IsolatedTestAddr.String()
cfg.HTTPAddr = util.IsolatedTestAddr.String()
}
if params.Addr != "" {
cfg.Addr = params.Addr
cfg.AdvertiseAddr = params.Addr
}
if params.SQLAddr != "" {
cfg.SQLAddr = params.SQLAddr
cfg.SQLAdvertiseAddr = params.SQLAddr
}
if params.HTTPAddr != "" {
cfg.HTTPAddr = params.HTTPAddr
}
cfg.DisableTLSForHTTP = params.DisableTLSForHTTP
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.Size.Percent > 0 {
panic(fmt.Sprintf("test server does not yet support in memory stores based on percentage of total memory: %s", storeSpec))
}
} else {
// 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).
// HeapProfileDirName and GoroutineDumpDirName are normally set by the
// cli, once, to the path of the first store.
if cfg.HeapProfileDirName == "" {
cfg.HeapProfileDirName = filepath.Join(storeSpec.Path, "logs", base.HeapProfileDir)
}
if cfg.GoroutineDumpDirName == "" {
cfg.GoroutineDumpDirName = filepath.Join(storeSpec.Path, "logs", base.GoroutineDumpDir)
}
}
}
cfg.Stores = base.StoreSpecList{Specs: params.StoreSpecs}
if params.TempStorageConfig.InMemory || params.TempStorageConfig.Path != "" {
cfg.TempStorageConfig = params.TempStorageConfig
}
if cfg.TestingKnobs.Store == nil {
cfg.TestingKnobs.Store = &kvserver.StoreTestingKnobs{}
}
cfg.TestingKnobs.Store.(*kvserver.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
params base.TestServerArgs
// server is the embedded Cockroach server struct.
*Server
// authClient is an http.Client that has been authenticated to access the
// Admin UI.
authClient [2]struct {
httpClient http.Client
cookie *serverpb.SessionCookie
once sync.Once
err error
}
}
// Node returns the Node as an interface{}.
func (ts *TestServer) Node() interface{} {
return ts.node
}
// Stopper returns the embedded server's Stopper.
func (ts *TestServer) Stopper() *stop.Stopper {
return ts.stopper
}
// GossipI is part of TestServerInterface.
func (ts *TestServer) GossipI() interface{} {
return ts.Gossip()
}
// Gossip is like GossipI but returns the real type instead of interface{}.
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
}
// SQLLivenessProvider returns the sqlliveness.Provider as an interface{}.
func (ts *TestServer) SQLLivenessProvider() interface{} {
if ts != nil {
return ts.sqlServer.execCfg.SQLLivenessReader
}
return nil
}
// JobRegistry returns the *jobs.Registry as an interface{}.
func (ts *TestServer) JobRegistry() interface{} {
if ts != nil {
return ts.sqlServer.jobRegistry
}
return nil
}
// MigrationManager returns the *sqlmigrations.Manager as an interface{}.
func (ts *TestServer) MigrationManager() interface{} {
if ts != nil {
return ts.sqlServer.migMgr
}
return nil
}
// NodeLiveness exposes the NodeLiveness instance used by the TestServer as an
// interface{}.
func (ts *TestServer) NodeLiveness() interface{} {
if ts != nil {
return ts.nodeLiveness
}
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() *kv.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.sqlServer.pgServer
}
return nil
}
// RaftTransport returns the RaftTransport used by the TestServer.
func (ts *TestServer) RaftTransport() *kvserver.RaftTransport {
if ts != nil {
return ts.raftTransport
}
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.ServingRPCAddr() after Start() for client connections.
// Use TestServer.Stopper().Stop() to shutdown the server after the test
// completes.
func (ts *TestServer) Start() error {
ctx := context.Background()
return ts.Server.Start(ctx)
}
type dummyProtectedTSProvider struct {
protectedts.Provider
}
func (d dummyProtectedTSProvider) Protect(context.Context, *kv.Txn, *ptpb.Record) error {
return errors.New("fake protectedts.Provider")
}
func makeSQLServerArgs(
stopper *stop.Stopper, kvClusterName string, baseCfg BaseConfig, sqlCfg SQLConfig,
) (sqlServerArgs, error) {
st := baseCfg.Settings
baseCfg.AmbientCtx.AddLogTag("sql", nil)
// TODO(tbg): this is needed so that the RPC heartbeats between the testcluster
// and this tenant work.
//
// TODO(tbg): address this when we introduce the real tenant RPCs in:
// https://github.com/cockroachdb/cockroach/issues/47898
baseCfg.ClusterName = kvClusterName
clock := hlc.NewClock(hlc.UnixNano, time.Duration(baseCfg.MaxOffset))
registry := metric.NewRegistry()
var rpcTestingKnobs rpc.ContextTestingKnobs
if p, ok := baseCfg.TestingKnobs.Server.(*TestingKnobs); ok {
rpcTestingKnobs = p.ContextTestingKnobs
}
rpcContext := rpc.NewContext(rpc.ContextOptions{
TenantID: sqlCfg.TenantID,
AmbientCtx: baseCfg.AmbientCtx,
Config: baseCfg.Config,
Clock: clock,
Stopper: stopper,
Settings: st,
Knobs: rpcTestingKnobs,
})
var dsKnobs kvcoord.ClientTestingKnobs
if dsKnobsP, ok := baseCfg.TestingKnobs.DistSQL.(*kvcoord.ClientTestingKnobs); ok {
dsKnobs = *dsKnobsP
}
rpcRetryOptions := base.DefaultRetryOptions()
tcCfg := kvtenant.ConnectorConfig{
AmbientCtx: baseCfg.AmbientCtx,
RPCContext: rpcContext,
RPCRetryOptions: rpcRetryOptions,
DefaultZoneConfig: &baseCfg.DefaultZoneConfig,
}
tenantConnect, err := kvtenant.Factory.NewConnector(tcCfg, sqlCfg.TenantKVAddrs)
if err != nil {
return sqlServerArgs{}, err
}
resolver := kvtenant.AddressResolver(tenantConnect)
nodeDialer := nodedialer.New(rpcContext, resolver)
dsCfg := kvcoord.DistSenderConfig{
AmbientCtx: baseCfg.AmbientCtx,
Settings: st,
Clock: clock,
NodeDescs: tenantConnect,
RPCRetryOptions: &rpcRetryOptions,
RPCContext: rpcContext,
NodeDialer: nodeDialer,
RangeDescriptorDB: tenantConnect,
TestingKnobs: dsKnobs,
}
ds := kvcoord.NewDistSender(dsCfg)
var clientKnobs kvcoord.ClientTestingKnobs
if p, ok := baseCfg.TestingKnobs.KVClient.(*kvcoord.ClientTestingKnobs); ok {
clientKnobs = *p
}
txnMetrics := kvcoord.MakeTxnMetrics(baseCfg.HistogramWindowInterval())
registry.AddMetricStruct(txnMetrics)
tcsFactory := kvcoord.NewTxnCoordSenderFactory(
kvcoord.TxnCoordSenderFactoryConfig{
AmbientCtx: baseCfg.AmbientCtx,
Settings: st,
Clock: clock,
Stopper: stopper,
HeartbeatInterval: base.DefaultTxnHeartbeatInterval,
Linearizable: false,
Metrics: txnMetrics,
TestingKnobs: clientKnobs,
},
ds,
)
db := kv.NewDB(baseCfg.AmbientCtx, tcsFactory, clock, stopper)
circularInternalExecutor := &sql.InternalExecutor{}
// Protected timestamps won't be available (at first) in multi-tenant
// clusters.
var protectedTSProvider protectedts.Provider
{
pp, err := ptprovider.New(ptprovider.Config{
DB: db,
InternalExecutor: circularInternalExecutor,
Settings: st,
})
if err != nil {
panic(err)
}
protectedTSProvider = dummyProtectedTSProvider{pp}
}
recorder := status.NewMetricsRecorder(clock, nil, rpcContext, nil, st)
const sqlInstanceID = base.SQLInstanceID(10001)
idContainer := base.NewSQLIDContainer(sqlInstanceID, nil /* nodeID */)
runtime := status.NewRuntimeStatSampler(context.Background(), clock)
registry.AddMetricStruct(runtime)
// We don't need this for anything except some services that want a gRPC
// server to register against (but they'll never get RPCs at the time of
// writing): the blob service and DistSQL.
dummyRPCServer := grpc.NewServer()
sessionRegistry := sql.NewSessionRegistry()
return sqlServerArgs{
sqlServerOptionalKVArgs: sqlServerOptionalKVArgs{
nodesStatusServer: serverpb.MakeOptionalNodesStatusServer(nil),
nodeLiveness: optionalnodeliveness.MakeContainer(nil),
gossip: gossip.MakeOptionalGossip(nil),
grpcServer: dummyRPCServer,
isMeta1Leaseholder: func(_ context.Context, timestamp hlc.Timestamp) (bool, error) {
return false, errors.New("isMeta1Leaseholder is not available to secondary tenants")
},
nodeIDContainer: idContainer,
externalStorage: func(ctx context.Context, dest roachpb.ExternalStorage) (cloud.ExternalStorage, error) {
return nil, errors.New("external storage is not available to secondary tenants")
},
externalStorageFromURI: func(ctx context.Context,
uri, user string) (cloud.ExternalStorage, error) {
return nil, errors.New("external uri storage is not available to secondary tenants")
},
},
sqlServerOptionalTenantArgs: sqlServerOptionalTenantArgs{
tenantConnect: tenantConnect,
},
SQLConfig: &sqlCfg,
BaseConfig: &baseCfg,
stopper: stopper,
clock: clock,
runtime: runtime,
rpcContext: rpcContext,
nodeDescs: tenantConnect,
systemConfigProvider: tenantConnect,
nodeDialer: nodeDialer,
distSender: ds,
db: db,
registry: registry,
recorder: recorder,
sessionRegistry: sessionRegistry,
circularInternalExecutor: circularInternalExecutor,
circularJobRegistry: &jobs.Registry{},
protectedtsProvider: protectedTSProvider,
sqlStatusServer: newTenantStatusServer(
baseCfg.AmbientCtx, &adminPrivilegeChecker{ie: circularInternalExecutor}, sessionRegistry, baseCfg.Settings,
),
}, nil
}
// StartTenant starts a SQL tenant communicating with this TestServer.
func (ts *TestServer) StartTenant(
params base.TestTenantArgs,
) (pgAddr string, httpAddr string, _ error) {
ctx := context.Background()
if !params.Existing {
if _, err := ts.InternalExecutor().(*sql.InternalExecutor).Exec(
ctx, "testserver-create-tenant", nil /* txn */, "SELECT crdb_internal.create_tenant($1)", params.TenantID.ToUint64(),
); err != nil {
return "", "", err
}
}
st := cluster.MakeTestingClusterSettings()
sqlCfg := makeTestSQLConfig(st, params.TenantID)
sqlCfg.TenantKVAddrs = []string{ts.ServingRPCAddr()}
sqlCfg.TenantIDCodecOverride = params.TenantIDCodecOverride
baseCfg := makeTestBaseConfig(st)
if params.AllowSettingClusterSettings {
baseCfg.TestingKnobs.TenantTestingKnobs = &sql.TenantTestingKnobs{
ClusterSettingsUpdater: st.MakeUpdater(),
}
}
stopper := params.Stopper
if stopper == nil {
stopper = ts.Stopper()
}
return StartTenant(
ctx,
stopper,
ts.Cfg.ClusterName,
baseCfg,
sqlCfg,
)
}
// StartTenant starts a stand-alone SQL server against a KV backend.
func StartTenant(
ctx context.Context,
stopper *stop.Stopper,
kvClusterName string, // NB: gone after https://github.com/cockroachdb/cockroach/issues/42519
baseCfg BaseConfig,
sqlCfg SQLConfig,
) (pgAddr string, httpAddr string, _ error) {
args, err := makeSQLServerArgs(stopper, kvClusterName, baseCfg, sqlCfg)
if err != nil {
return "", "", err
}
s, err := newSQLServer(ctx, args)
if err != nil {
return "", "", err
}
// TODO(asubiotto): remove this. Right now it is needed to initialize the
// SpanResolver.
s.execCfg.DistSQLPlanner.SetNodeInfo(roachpb.NodeDescriptor{NodeID: 0})
connManager := netutil.MakeServer(
args.stopper,
// The SQL server only uses connManager.ServeWith. The both below
// are unused.
nil, // tlsConfig
nil, // handler
)
pgL, err := listen(ctx, &args.Config.SQLAddr, &args.Config.SQLAdvertiseAddr, "sql")
if err != nil {
return "", "", err
}
args.stopper.RunWorker(ctx, func(ctx context.Context) {
<-args.stopper.ShouldQuiesce()
// NB: we can't do this as a Closer because (*Server).ServeWith is
// running in a worker and usually sits on accept(pgL) which unblocks
// only when pgL closes. In other words, pgL needs to close when
// quiescing starts to allow that worker to shut down.
_ = pgL.Close()
})
httpL, err := listen(ctx, &args.Config.HTTPAddr, &args.Config.HTTPAdvertiseAddr, "http")
if err != nil {
return "", "", err
}
args.stopper.RunWorker(ctx, func(ctx context.Context) {
<-args.stopper.ShouldQuiesce()
_ = httpL.Close()
})
pgLAddr := pgL.Addr().String()
httpLAddr := httpL.Addr().String()
args.recorder.AddNode(
args.registry,
roachpb.NodeDescriptor{},
timeutil.Now().UnixNano(),
pgLAddr, // advertised addr
httpLAddr, // http addr
pgLAddr, // sql addr
)
args.stopper.RunWorker(ctx, func(ctx context.Context) {
mux := http.NewServeMux()
debugServer := debug.NewServer(args.Settings, s.pgServer.HBADebugFn())
mux.Handle("/", debugServer)
mux.HandleFunc("/health", func(w http.ResponseWriter, req *http.Request) {
// Return Bad Request if called with arguments.
if err := req.ParseForm(); err != nil || len(req.Form) != 0 {
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}
})
f := varsHandler{metricSource: args.recorder, st: args.Settings}.handleVars
mux.Handle(statusVars, http.HandlerFunc(f))
_ = http.Serve(httpL, mux)
})
const (
socketFile = "" // no unix socket
)
orphanedLeasesTimeThresholdNanos := args.clock.Now().WallTime
// TODO(tbg): the log dir is not configurable at this point
// since it is integrated too tightly with the `./cockroach start` command.
if err := startSampleEnvironment(ctx, sampleEnvironmentCfg{
st: args.Settings,
stopper: args.stopper,
minSampleInterval: base.DefaultMetricsSampleInterval,
goroutineDumpDirName: args.GoroutineDumpDirName,
heapProfileDirName: args.HeapProfileDirName,
runtime: args.runtime,
}); err != nil {
return "", "", err
}
if err := s.start(ctx,
args.stopper,
args.TestingKnobs,
connManager,
pgL,
socketFile,
orphanedLeasesTimeThresholdNanos,
); err != nil {
return "", "", err
}
// Inform the logging package of the cluster identifiers.
clusterID := args.rpcContext.ClusterID.Get().String()
log.SetClusterID(clusterID)
log.SetTenantIDs(args.TenantID.String(), int32(args.sqlServerOptionalKVArgs.nodeIDContainer.SQLInstanceID()))
return pgLAddr, httpLAddr, 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(), &ts.cfg.DefaultZoneConfig, &ts.cfg.DefaultSystemZoneConfig)
}
// ExpectedInitialRangeCount returns the expected number of ranges that should
// be on the server after bootstrap.
func ExpectedInitialRangeCount(
db *kv.DB, defaultZoneConfig *zonepb.ZoneConfig, defaultSystemZoneConfig *zonepb.ZoneConfig,
) (int, error) {
descriptorIDs, err := sqlmigrations.ExpectedDescriptorIDs(
context.Background(), db, keys.SystemSQLCodec, defaultZoneConfig, defaultSystemZoneConfig,
)
if err != nil {
return 0, err
}
// System table splits occur at every possible table boundary between the end
// of the system config ID space (keys.MaxSystemConfigDescID) and the system
// table with the maximum ID (maxSystemDescriptorID), even when an ID within
// the span does not have an associated descriptor.
maxSystemDescriptorID := descriptorIDs[0]
for _, descID := range descriptorIDs {
if descID > maxSystemDescriptorID && descID <= keys.MaxReservedDescID {
maxSystemDescriptorID = descID
}
}
if maxSystemDescriptorID < descpb.ID(keys.MaxPseudoTableID) {
maxSystemDescriptorID = descpb.ID(keys.MaxPseudoTableID)
}
systemTableSplits := int(maxSystemDescriptorID - keys.MaxSystemConfigDescID)
// `n` splits create `n+1` ranges.
return len(config.StaticSplits()) + systemTableSplits + 1, nil
}
// Stores returns the collection of stores from this TestServer's node.
func (ts *TestServer) Stores() *kvserver.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() []storage.Engine {
return ts.engines
}
// ServingRPCAddr returns the server's RPC address. Should be used by clients.
func (ts *TestServer) ServingRPCAddr() string {
return ts.cfg.AdvertiseAddr
}
// ServingSQLAddr returns the server's SQL address. Should be used by clients.
func (ts *TestServer) ServingSQLAddr() string {
return ts.cfg.SQLAdvertiseAddr
}
// HTTPAddr returns the server's HTTP address. Should be used by clients.
func (ts *TestServer) HTTPAddr() string {
return ts.cfg.HTTPAddr
}
// RPCAddr returns the server's listening RPC address.
// Note: use ServingRPCAddr() instead unless there is a specific reason not to.
func (ts *TestServer) RPCAddr() string {
return ts.cfg.Addr
}
// SQLAddr returns the server's listening SQL address.
// Note: use ServingSQLAddr() instead unless there is a specific reason not to.
func (ts *TestServer) SQLAddr() string {
return ts.cfg.SQLAddr
}
// DrainClients exports the drainClients() method for use by tests.
func (ts *TestServer) DrainClients(ctx context.Context) error {
return ts.drainClients(ctx, nil /* reporter */)
}
// WriteSummaries implements TestServerInterface.
func (ts *TestServer) WriteSummaries() error {
return ts.node.writeNodeStatus(context.TODO(), time.Hour)
}
// 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.Server.rpcContext.GetHTTPClient()
}
const authenticatedUserName = "authentic_user"
const authenticatedUserNameNoAdmin = "authentic_user_noadmin"
// GetAdminAuthenticatedHTTPClient implements the TestServerInterface.
func (ts *TestServer) GetAdminAuthenticatedHTTPClient() (http.Client, error) {
httpClient, _, err := ts.getAuthenticatedHTTPClientAndCookie(authenticatedUserName, true)
return httpClient, err
}
// GetAuthenticatedHTTPClient implements the TestServerInterface.
func (ts *TestServer) GetAuthenticatedHTTPClient(isAdmin bool) (http.Client, error) {
authUser := authenticatedUserName
if !isAdmin {
authUser = authenticatedUserNameNoAdmin
}
httpClient, _, err := ts.getAuthenticatedHTTPClientAndCookie(authUser, isAdmin)
return httpClient, err
}
func (ts *TestServer) getAuthenticatedHTTPClientAndCookie(
authUser string, isAdmin bool,
) (http.Client, *serverpb.SessionCookie, error) {
authIdx := 0
if isAdmin {
authIdx = 1
}
authClient := &ts.authClient[authIdx]
authClient.once.Do(func() {
// Create an authentication session for an arbitrary admin user.
authClient.err = func() error {
// The user needs to exist as the admin endpoints will check its role.
if err := ts.createAuthUser(authUser, isAdmin); err != nil {
return err
}
id, secret, err := ts.authentication.newAuthSession(context.TODO(), authUser)
if err != nil {
return err
}
rawCookie := &serverpb.SessionCookie{
ID: id,
Secret: secret,
}
// Encode a session cookie and store it in a cookie jar.
cookie, err := EncodeSessionCookie(rawCookie, false /* forHTTPSOnly */)
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.
authClient.httpClient, err = ts.rpcContext.GetHTTPClient()
if err != nil {
return err
}
authClient.httpClient.Jar = cookieJar
authClient.cookie = rawCookie
return nil
}()
})
return authClient.httpClient, authClient.cookie, authClient.err
}
func (ts *TestServer) createAuthUser(userName string, isAdmin bool) error {
if _, err := ts.Server.sqlServer.internalExecutor.ExecEx(context.TODO(),
"create-auth-user", nil,
sessiondata.InternalExecutorOverride{User: security.RootUser},
"CREATE USER $1", userName,
); err != nil {
return err
}
if isAdmin {
// We can't use the GRANT statement here because we don't want
// to rely on CCL code.
if _, err := ts.Server.sqlServer.internalExecutor.ExecEx(context.TODO(),
"grant-admin", nil,
sessiondata.InternalExecutorOverride{User: security.RootUser},
"INSERT INTO system.role_members (role, member, \"isAdmin\") VALUES ('admin', $1, true)", userName,
); err != nil {
return err
}
}
return nil
}
// 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 {
switch t := v.(type) {
case *metric.Counter:
c = t.Count()
found = true
case *metric.Gauge:
c = t.Value()
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()
for _, m := range ts.sqlServer.pgServer.Metrics() {
reg.AddMetricStruct(m)
}
reg.Each(func(n string, v interface{}) {
if name == n {
switch t := v.(type) {
case *metric.Counter:
c = t.Count()
found = true
case *metric.Gauge:
c = t.Value()
found = true
}
}
})
if !found {
panic(fmt.Sprintf("couldn't find metric %s", name))
}
return c
}
// LeaseManager is part of TestServerInterface.
func (ts *TestServer) LeaseManager() interface{} {
return ts.sqlServer.leaseMgr
}
// InternalExecutor is part of TestServerInterface.
func (ts *TestServer) InternalExecutor() interface{} {
return ts.sqlServer.internalExecutor
}
// GetNode exposes the Server's Node.
func (ts *TestServer) GetNode() *Node {
return ts.node
}
// DistSenderI is part of DistSendeInterface.
func (ts *TestServer) DistSenderI() interface{} {
return ts.distSender
}
// DistSender is like DistSenderI(), but returns the real type instead of
// interface{}.
func (ts *TestServer) DistSender() *kvcoord.DistSender {
return ts.DistSenderI().(*kvcoord.DistSender)
}