forked from cockroachdb/cockroach
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtenant.go
1488 lines (1328 loc) · 52.4 KB
/
tenant.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 2021 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"
"net/http"
"os"
"path/filepath"
"strconv"
"sync"
"time"
"github.com/cockroachdb/cockroach/pkg/base"
"github.com/cockroachdb/cockroach/pkg/build"
"github.com/cockroachdb/cockroach/pkg/gossip"
"github.com/cockroachdb/cockroach/pkg/inspectz"
"github.com/cockroachdb/cockroach/pkg/jobs"
"github.com/cockroachdb/cockroach/pkg/jobs/jobsprotectedts"
"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/kvclient/rangefeed"
"github.com/cockroachdb/cockroach/pkg/kv/kvserver/kvserverbase"
"github.com/cockroachdb/cockroach/pkg/kv/kvserver/protectedts"
"github.com/cockroachdb/cockroach/pkg/kv/kvserver/protectedts/ptprovider"
"github.com/cockroachdb/cockroach/pkg/kv/kvserver/protectedts/ptreconcile"
"github.com/cockroachdb/cockroach/pkg/multitenant"
"github.com/cockroachdb/cockroach/pkg/multitenant/mtinfopb"
"github.com/cockroachdb/cockroach/pkg/multitenant/multitenantcpu"
"github.com/cockroachdb/cockroach/pkg/multitenant/tenantcapabilities"
"github.com/cockroachdb/cockroach/pkg/multitenant/tenantcapabilities/tenantcapabilitiesauthorizer"
"github.com/cockroachdb/cockroach/pkg/multitenant/tenantcostmodel"
"github.com/cockroachdb/cockroach/pkg/obs"
"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/username"
"github.com/cockroachdb/cockroach/pkg/server/apiutil"
"github.com/cockroachdb/cockroach/pkg/server/authserver"
"github.com/cockroachdb/cockroach/pkg/server/debug"
"github.com/cockroachdb/cockroach/pkg/server/privchecker"
"github.com/cockroachdb/cockroach/pkg/server/serverctl"
"github.com/cockroachdb/cockroach/pkg/server/serverpb"
"github.com/cockroachdb/cockroach/pkg/server/status"
"github.com/cockroachdb/cockroach/pkg/server/structlogging"
"github.com/cockroachdb/cockroach/pkg/server/systemconfigwatcher"
"github.com/cockroachdb/cockroach/pkg/settings/cluster"
"github.com/cockroachdb/cockroach/pkg/spanconfig"
"github.com/cockroachdb/cockroach/pkg/spanconfig/spanconfiglimiter"
"github.com/cockroachdb/cockroach/pkg/sql"
"github.com/cockroachdb/cockroach/pkg/sql/flowinfra"
"github.com/cockroachdb/cockroach/pkg/sql/isql"
"github.com/cockroachdb/cockroach/pkg/sql/optionalnodeliveness"
"github.com/cockroachdb/cockroach/pkg/sql/pgwire"
"github.com/cockroachdb/cockroach/pkg/sql/sessionprotectedts"
"github.com/cockroachdb/cockroach/pkg/sql/sqlinstance"
"github.com/cockroachdb/cockroach/pkg/sql/sqlliveness"
"github.com/cockroachdb/cockroach/pkg/testutils/serverutils"
"github.com/cockroachdb/cockroach/pkg/ts"
"github.com/cockroachdb/cockroach/pkg/util"
"github.com/cockroachdb/cockroach/pkg/util/admission"
"github.com/cockroachdb/cockroach/pkg/util/hlc"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/cockroach/pkg/util/log/logmetrics"
"github.com/cockroachdb/cockroach/pkg/util/metric"
"github.com/cockroachdb/cockroach/pkg/util/netutil"
"github.com/cockroachdb/cockroach/pkg/util/schedulerlatency"
"github.com/cockroachdb/cockroach/pkg/util/stop"
"github.com/cockroachdb/cockroach/pkg/util/timeutil"
"github.com/cockroachdb/cockroach/pkg/util/uuid"
"github.com/cockroachdb/errors"
"github.com/cockroachdb/logtags"
"github.com/cockroachdb/redact"
sentry "github.com/getsentry/sentry-go"
)
// SQLServerWrapper is a utility struct that encapsulates
// a SQLServer and its helpers that make it a networked service.
type SQLServerWrapper struct {
// NB: This struct definition mirrors that of Server.
// The fields are kept in a similar order to make their comparison easier
// during reviews.
//
// TODO(knz): Find a way to merge these two togethers so there is just
// one implementation.
cfg *BaseConfig
clock *hlc.Clock
rpcContext *rpc.Context
// The gRPC server on which the different RPC handlers will be registered.
grpc *grpcServer
kvNodeDialer *nodedialer.Dialer
db *kv.DB
// Metric registries.
// See the explanatory comments in server.go and status/recorder.g o
// for details.
registry *metric.Registry
sysRegistry *metric.Registry
recorder *status.MetricsRecorder
runtime *status.RuntimeStatSampler
http *httpServer
adminAuthzCheck privchecker.CheckerForRPCHandlers
tenantAdmin *adminServer
tenantStatus *statusServer
drainServer *drainServer
authentication authserver.Server
// eventsExporter exports data to the Observability Service.
eventsExporter obs.EventsExporterInterface
stopper *stop.Stopper
debug *debug.Server
// pgL is the SQL listener.
pgL net.Listener
// loopbackPgL is the SQL listener for internal pgwire connections.
loopbackPgL *netutil.LoopbackListener
// pgPreServer handles SQL connections prior to routing them to a
// specific tenant.
pgPreServer *pgwire.PreServeConnHandler
sqlServer *SQLServer
sqlCfg *SQLConfig
// Created in NewServer but initialized (made usable) in `(*Server).PreStart`.
externalStorageBuilder *externalStorageBuilder
// Used for multi-tenant cost control (on the tenant side).
costController multitenant.TenantSideCostController
// promRuleExporter is used by the tenant to expose the prometheus rules.
promRuleExporter *metric.PrometheusRuleExporter
tenantTimeSeries *ts.TenantServer
}
// Drain idempotently activates the draining mode.
// Note: new code should not be taught to use this method
// directly. Use the Drain() RPC instead with a suitably crafted
// DrainRequest.
//
// On failure, the system may be in a partially drained
// state; the client should either continue calling Drain() or shut
// down the server.
//
// The reporter function, if non-nil, is called for each
// packet of load shed away from the server during the drain.
//
// TODO(knz): This method is currently exported for use by the
// shutdown code in cli/start.go; however, this is a mis-design. The
// start code should use the Drain() RPC like quit does.
func (s *SQLServerWrapper) Drain(
ctx context.Context, verbose bool,
) (remaining uint64, info redact.RedactableString, err error) {
return s.drainServer.runDrain(ctx, verbose)
}
// tenantServerDeps holds dependencies for the SQL server that we want
// to vary based on whether we are in a shared process or separate
// process tenant.
type tenantServerDeps struct {
instanceIDContainer *base.SQLIDContainer
nodeIDGetter func() roachpb.NodeID
// The following should eventually be connected to tenant
// capabilities.
costControllerFactory costControllerFactory
spanLimiterFactory spanLimiterFactory
}
type spanLimiterFactory func(isql.Executor, *cluster.Settings, *spanconfig.TestingKnobs) spanconfig.Limiter
type costControllerFactory func(*cluster.Settings, roachpb.TenantID, kvtenant.TokenBucketProvider) (multitenant.TenantSideCostController, error)
// NewSeparateProcessTenantServer creates a tenant-specific, SQL-only
// server against a KV backend, with defaults appropriate for a
// SQLServer that is not located in the same process as a KVServer.
//
// The caller is responsible for listening to the server's ShutdownRequested()
// channel and stopping cfg.stopper when signaled.
func NewSeparateProcessTenantServer(
ctx context.Context,
stopper *stop.Stopper,
baseCfg BaseConfig,
sqlCfg SQLConfig,
tenantNameContainer *roachpb.TenantNameContainer,
) (*SQLServerWrapper, error) {
deps := tenantServerDeps{
instanceIDContainer: baseCfg.IDContainer.SwitchToSQLIDContainerForStandaloneSQLInstance(),
// The kvcoord.DistSender uses the node ID to preferentially route
// requests to a local replica (if one exists). In separate-process
// mode, not knowing the node ID, and thus not being able to take
// advantage of this optimization is okay, given tenants not running
// in-process with KV instances have no such optimization to take
// advantage of to begin with.
nodeIDGetter: nil,
costControllerFactory: NewTenantSideCostController,
spanLimiterFactory: func(ie isql.Executor, st *cluster.Settings, knobs *spanconfig.TestingKnobs) spanconfig.Limiter {
return spanconfiglimiter.New(ie, st, knobs)
},
}
return newTenantServer(ctx, stopper, baseCfg, sqlCfg, tenantNameContainer, deps, mtinfopb.ServiceModeExternal)
}
// newSharedProcessTenantServer creates a tenant-specific, SQL-only
// server against a KV backend, with defaults appropriate for a
// SQLServer that is not located in the same process as a KVServer.
//
// The caller is responsible for listening to the server's ShutdownRequested()
// channel and stopping cfg.stopper when signaled.
func newSharedProcessTenantServer(
ctx context.Context,
stopper *stop.Stopper,
baseCfg BaseConfig,
sqlCfg SQLConfig,
tenantNameContainer *roachpb.TenantNameContainer,
) (*SQLServerWrapper, error) {
if baseCfg.IDContainer.Get() == 0 {
return nil, errors.AssertionFailedf("programming error: NewSharedProcessTenantServer called before NodeID was assigned.")
}
deps := tenantServerDeps{
instanceIDContainer: base.NewSQLIDContainerForNode(baseCfg.IDContainer),
// The kvcoord.DistSender uses the node ID to preferentially route
// requests to a local replica (if one exists). In shared-process mode
// we can easily provide that without accessing the gossip.
nodeIDGetter: baseCfg.IDContainer.Get,
// TODO(ssd): The cost controller should instead be able to
// read from the capability system and return immediately if
// the tenant is exempt. For now we are turning off the
// tenant-side cost controller for shared-memory tenants until
// we have the abilility to read capabilities tenant-side.
//
// https://github.com/cockroachdb/cockroach/issues/84586
costControllerFactory: NewNoopTenantSideCostController,
spanLimiterFactory: func(isql.Executor, *cluster.Settings, *spanconfig.TestingKnobs) spanconfig.Limiter {
return spanconfiglimiter.NoopLimiter{}
},
}
return newTenantServer(ctx, stopper, baseCfg, sqlCfg, tenantNameContainer, deps, mtinfopb.ServiceModeShared)
}
// newTenantServer constructs a SQLServerWrapper.
//
// The tenant's metrics registry is registered with parentRecorder, if not nil.
func newTenantServer(
ctx context.Context,
stopper *stop.Stopper,
baseCfg BaseConfig,
sqlCfg SQLConfig,
tenantNameContainer *roachpb.TenantNameContainer,
deps tenantServerDeps,
serviceMode mtinfopb.TenantServiceMode,
) (*SQLServerWrapper, error) {
// TODO(knz): Make the license application a per-server thing
// instead of a global thing.
err := ApplyTenantLicense()
if err != nil {
return nil, err
}
// Start the SQL listener early so any delay that happen from this point onward
// (until the server is ready) won't cause client connections to be rejected.
if baseCfg.SplitListenSQL && !baseCfg.DisableSQLListener {
sqlAddrListener, err := ListenAndUpdateAddrs(
ctx, &baseCfg.SQLAddr, &baseCfg.SQLAdvertiseAddr, "sql")
if err != nil {
return nil, err
}
baseCfg.SQLAddrListener = sqlAddrListener
}
// The setting of tenant id may have not been done until now. If this is the
// case, DelayedSetTenantID will be set and should be used to populate
// TenantID in the config. We call it here as we need a valid TenantID below.
if sqlCfg.DelayedSetTenantID != nil {
cfgTenantID, err := sqlCfg.DelayedSetTenantID(ctx)
if err != nil {
return nil, err
}
sqlCfg.TenantID = cfgTenantID
}
log.Ops.Infof(ctx, "server starting for tenant %q", redact.Safe(sqlCfg.TenantID))
// Inform the server identity provider that we're operating
// for a tenant server.
//
// TODO(#77336): we would like to set the tenant name here too.
// Unfortunately, this is not possible for now because the name is
// only known after the SQL server has initialized the connector (in
// preStart), which cannot be called yet.
// Instead, the tenant name is currently added to the idProvider
// inside preStart().
// The better approach would be to have the CLI flag use a name,
// then rely on some mechanism to retrieve the ID from the name to
// initialize the rest of the server.
baseCfg.idProvider.SetTenantID(sqlCfg.TenantID)
args, err := makeTenantSQLServerArgs(ctx, stopper, baseCfg, sqlCfg, tenantNameContainer, deps, serviceMode)
if err != nil {
return nil, err
}
err = args.ValidateAddrs(ctx)
if err != nil {
return nil, err
}
// The following initialization mirrors that of NewServer().
// Please keep them in sync.
// Instantiate the API privilege checker.
//
// TODO(tbg): give adminServer only what it needs (and avoid circular deps).
adminAuthzCheck := privchecker.NewChecker(args.circularInternalExecutor, args.Settings)
// Instantiate the HTTP server.
// These callbacks help us avoid a dependency on gossip in httpServer.
parseNodeIDFn := func(s string) (roachpb.NodeID, bool, error) {
return roachpb.NodeID(0), false, errors.New("tenants cannot proxy to KV Nodes")
}
getNodeIDHTTPAddressFn := func(id roachpb.NodeID) (*util.UnresolvedAddr, error) {
return nil, errors.New("tenants cannot proxy to KV Nodes")
}
sHTTP := newHTTPServer(baseCfg, args.rpcContext, parseNodeIDFn, getNodeIDHTTPAddressFn)
// This is where we would be instantiating the SQL session registry
// in NewServer().
// This is currently performed in makeTenantSQLServerArgs().
// Instantiate the cache of closed SQL sessions.
closedSessionCache := sql.NewClosedSessionCache(
baseCfg.Settings, args.monitorAndMetrics.rootSQLMemoryMonitor, time.Now)
args.closedSessionCache = closedSessionCache
// Instantiate the serverIterator to provide fanout to SQL instances. The
// serverIterator needs access to sqlServer which is assigned below once we
// have an instance.
serverIterator := &tenantFanoutClient{
sqlServer: nil,
rpcCtx: args.rpcContext,
stopper: args.stopper,
}
// Instantiate the status API server. The statusServer needs access to the
// sqlServer, but we also need the same object to set up the sqlServer. So
// construct the status server with a nil sqlServer, and then assign it once
// an SQL server gets created. We are going to assume that the status server
// won't require the SQL server object until later.
var serverKnobs TestingKnobs
if s, ok := baseCfg.TestingKnobs.Server.(*TestingKnobs); ok {
serverKnobs = *s
}
sStatus := newStatusServer(
baseCfg.AmbientCtx,
baseCfg.Settings,
baseCfg.Config,
adminAuthzCheck,
args.db,
args.recorder,
args.rpcContext,
stopper,
args.sessionRegistry,
closedSessionCache,
args.remoteFlowRunner,
args.circularInternalExecutor,
serverIterator,
args.clock,
&serverKnobs,
)
args.sqlStatusServer = sStatus
// This is the location in NewServer() where we would be configuring
// the path to the special file that blocks background jobs.
// This should probably done here.
// See: https://github.com/cockroachdb/cockroach/issues/90524
// This is the location in NewServer() where we would be creating
// the eventsExporter. This is currently performed in
// makeTenantSQLServerArgs().
var pgPreServer *pgwire.PreServeConnHandler
if !baseCfg.DisableSQLListener {
// Initialize the pgwire pre-server, which initializes connections,
// sets up TLS and reads client status parameters.
pgPreServer = pgwire.NewPreServeConnHandler(
baseCfg.AmbientCtx,
baseCfg.Config,
args.Settings,
args.rpcContext.GetServerTLSConfig,
baseCfg.HistogramWindowInterval(),
args.monitorAndMetrics.rootSQLMemoryMonitor,
false, /* acceptTenantName */
)
for _, m := range pgPreServer.Metrics() {
args.registry.AddMetricStruct(m)
}
}
// Instantiate the SQL server proper.
sqlServer, err := newSQLServer(ctx, args)
if err != nil {
return nil, err
}
// Instantiate the migration API server.
tms := newTenantMigrationServer(sqlServer)
serverpb.RegisterMigrationServer(args.grpc.Server, tms)
sqlServer.migrationServer = tms // only for testing via testTenant
// Tell the authz server how to connect to SQL.
adminAuthzCheck.SetAuthzAccessorFactory(func(opName string) (sql.AuthorizationAccessor, func()) {
// This is a hack to get around a Go package dependency cycle. See comment
// in sql/jobs/registry.go on planHookMaker.
txn := args.db.NewTxn(ctx, "check-system-privilege")
p, cleanup := sql.NewInternalPlanner(
opName,
txn,
username.NodeUserName(),
&sql.MemoryMetrics{},
sqlServer.execCfg,
sql.NewInternalSessionData(ctx, sqlServer.execCfg.Settings, opName),
)
return p.(sql.AuthorizationAccessor), cleanup
})
// Create the authentication RPC server (login/logout).
sAuth := authserver.NewServer(baseCfg.Config, sqlServer)
// Create a drain server.
drainServer := newDrainServer(baseCfg, args.stopper, args.stopTrigger, args.grpc, sqlServer)
// Instantiate the admin API server.
sAdmin := newAdminServer(
sqlServer,
args.Settings,
adminAuthzCheck,
sqlServer.internalExecutor,
args.BaseConfig.AmbientCtx,
args.recorder,
args.db,
args.rpcContext,
serverIterator,
args.clock,
args.distSender,
args.grpc,
drainServer,
)
// Connect the various servers to RPC.
for _, gw := range []grpcGatewayServer{sAdmin, sStatus, sAuth, args.tenantTimeSeriesServer} {
gw.RegisterService(args.grpc.Server)
}
// Tell the status/admin servers how to access SQL structures.
sStatus.setStmtDiagnosticsRequester(sqlServer.execCfg.StmtDiagnosticsRecorder)
serverIterator.sqlServer = sqlServer
sStatus.baseStatusServer.sqlServer = sqlServer
sAdmin.sqlServer = sqlServer
var processCapAuthz tenantcapabilities.Authorizer = &tenantcapabilitiesauthorizer.AllowEverythingAuthorizer{}
if lsi := sqlCfg.LocalKVServerInfo; lsi != nil {
processCapAuthz = lsi.SameProcessCapabilityAuthorizer
}
// Create the debug API server.
debugServer := debug.NewServer(
baseCfg.AmbientCtx,
args.Settings,
sqlServer.pgServer.HBADebugFn(),
sqlServer.execCfg.SQLStatusServer,
sqlCfg.TenantID,
processCapAuthz,
)
return &SQLServerWrapper{
cfg: args.BaseConfig,
clock: args.clock,
rpcContext: args.rpcContext,
grpc: args.grpc,
kvNodeDialer: args.kvNodeDialer,
db: args.db,
registry: args.registry,
sysRegistry: args.sysRegistry,
recorder: args.recorder,
runtime: args.runtime,
http: sHTTP,
adminAuthzCheck: adminAuthzCheck,
tenantAdmin: sAdmin,
tenantStatus: sStatus,
drainServer: drainServer,
authentication: sAuth,
eventsExporter: args.eventsExporter,
stopper: args.stopper,
debug: debugServer,
pgPreServer: pgPreServer,
sqlServer: sqlServer,
sqlCfg: args.SQLConfig,
externalStorageBuilder: args.externalStorageBuilder,
costController: args.costController,
promRuleExporter: args.promRuleExporter,
tenantTimeSeries: args.tenantTimeSeriesServer,
}, nil
}
// PreStart starts the server on the specified port(s) and
// initializes subsystems.
//
// It does not activate the pgwire listener over the network / unix
// socket, which is done by the AcceptClients() method. The separation
// between the two exists so that SQL initialization can take place
// before the first client is accepted.
func (s *SQLServerWrapper) PreStart(ctx context.Context) error {
// NB: This logic mirrors the relevants bits in (*Server).PreStart.
// They should be kept in sync.
// We also use the same order so they can be positioned side-by-side
// for easier comparison during reviews.
//
// TODO(knz): Find a way to combine this common logic for both methods.
// Start a context for the asynchronous network workers.
workersCtx := s.AnnotateCtx(context.Background())
if !s.sqlServer.cfg.Insecure {
cm, err := s.rpcContext.GetCertificateManager()
if err != nil {
return err
}
// Ensure that SIGHUP will make this cert manager reload its certs
// from disk.
if err := cm.RegisterSignalHandler(workersCtx, s.stopper); err != nil {
return err
}
}
// If DisableHTTPListener is set, we are relying on the HTTP request
// routing performed by the serverController.
if !s.sqlServer.cfg.DisableHTTPListener {
// Load the TLS configuration for the HTTP server.
uiTLSConfig, err := s.rpcContext.GetUIServerTLSConfig()
if err != nil {
return err
}
// Start the admin UI server. This opens the HTTP listen socket,
// optionally sets up TLS, and dispatches the server worker for the
// web UI.
if err := startHTTPService(ctx, workersCtx, s.sqlServer.cfg, uiTLSConfig, s.stopper, s.http.baseHandler); err != nil {
return err
}
}
// Start the RPC server. This opens the RPC/SQL listen socket,
// and dispatches the server worker for the RPC.
// The SQL listener is returned, to start the SQL server later
// below when the server has initialized.
enableSQLListener := !s.sqlServer.cfg.DisableSQLListener
lf := ListenAndUpdateAddrs
if s.sqlServer.cfg.RPCListenerFactory != nil {
lf = s.sqlServer.cfg.RPCListenerFactory
}
pgL, loopbackPgL, rpcLoopbackDialFn, startRPCServer, err := startListenRPCAndSQL(ctx, workersCtx, *s.sqlServer.cfg, s.stopper, s.grpc, lf, enableSQLListener)
if err != nil {
return err
}
if enableSQLListener {
s.pgL = pgL
}
s.loopbackPgL = loopbackPgL
// Tell the RPC context how to connect in-memory.
s.rpcContext.SetLoopbackDialer(rpcLoopbackDialFn)
// NB: This is where (*Server).PreStart() reports the listener readiness
// via testing knobs: PauseAfterGettingRPCAddress, SignalAfterGettingRPCAddress.
// As of this writing, only `cockroach demo` uses those, to coordinate
// the initialization of the demo cluster. We do not need this logic
// in secondary tenants.
// Initialize grpc-gateway mux and context in order to get the /health
// endpoint working even before the node has fully initialized.
gwMux, gwCtx, conn, err := configureGRPCGateway(
ctx,
workersCtx,
s.sqlServer.cfg.AmbientCtx,
s.rpcContext,
s.stopper,
s.grpc,
s.sqlServer.cfg.AdvertiseAddr,
)
if err != nil {
return err
}
// Connect the various RPC handlers to the gRPC gateway.
for _, gw := range []grpcGatewayServer{s.tenantAdmin, s.tenantStatus, s.authentication, s.tenantTimeSeries} {
if err := gw.RegisterGateway(gwCtx, gwMux, conn); err != nil {
return err
}
}
// Handle /health early. This is necessary for orchestration. Note
// that /health is not authenticated, on purpose. This is both
// because it needs to be available before the cluster is up and can
// serve authentication requests, and also because it must work for
// monitoring tools which operate without authentication.
s.http.handleHealth(gwMux)
// Write listener info files early in the startup sequence. `listenerInfo` has a comment.
listenerFiles := listenerInfo{
listenRPC: s.sqlServer.cfg.Addr,
advertiseRPC: s.sqlServer.cfg.AdvertiseAddr,
listenSQL: s.sqlServer.cfg.SQLAddr,
advertiseSQL: s.sqlServer.cfg.SQLAdvertiseAddr,
listenHTTP: s.sqlServer.cfg.HTTPAdvertiseAddr,
}.Iter()
encryptedStore := false
for _, storeSpec := range s.sqlServer.cfg.Stores.Specs {
if storeSpec.InMemory {
continue
}
if storeSpec.IsEncrypted() {
encryptedStore = true
}
for name, val := range listenerFiles {
file := filepath.Join(storeSpec.Path, name)
if err := os.WriteFile(file, []byte(val), 0644); err != nil {
return errors.Wrapf(err, "failed to write %s", file)
}
}
// TODO(knz): Do we really want to write the listener files
// in _every_ store directory? Not just the first one?
}
// Set up calling s.cfg.ReadyFn at the right time. Essentially, this call
// determines when `./cockroach [...] --background` returns.
var onSuccessfulReturnFn func()
{
readyFn := func(bool) {}
if s.sqlServer.cfg.ReadyFn != nil {
readyFn = s.sqlServer.cfg.ReadyFn
}
onSuccessfulReturnFn = func() { readyFn(false /* waitForInit */) }
}
// This opens the main listener.
startRPCServer(workersCtx)
// Ensure components in the DistSQLPlanner that rely on the node ID are
// initialized before store startup continues.
s.sqlServer.execCfg.DistSQLPlanner.ConstructAndSetSpanResolver(ctx, 0 /* NodeID */, s.sqlServer.execCfg.Locality)
// Start measuring the Go scheduler latency.
if err := schedulerlatency.StartSampler(
workersCtx, s.sqlServer.cfg.Settings, s.stopper, s.sysRegistry, base.DefaultMetricsSampleInterval,
nil, /* listener */
); err != nil {
return err
}
// TODO(knz): This is the point where we could call
// checkHLCUpperBoundExistsAndEnsureMonotonicity(). Why is this not
// needed?
// Record a walltime that is lower than the lowest hlc timestamp this current
// instance of the node can use. We do not use startTime because it is lower
// than the timestamp used to create the bootstrap schema.
//
// TODO(tbg): clarify the contract here and move closer to usage if possible.
orphanedLeasesTimeThresholdNanos := s.clock.Now().WallTime
// Signal server readiness to the caller.
onSuccessfulReturnFn()
// Configure the Sentry reporter to add some additional context to reports.
//
// NB: In (*Server).PreStart(), we can also configure the cluster ID
// and node ID in Sentry reports as early as this point.
// However, for a secondary tenant we must wait on sqlServer.preStart()
// to add this information. See below.
sentry.ConfigureScope(func(scope *sentry.Scope) {
scope.SetTags(map[string]string{
"engine_type": s.sqlServer.cfg.StorageEngine.String(),
"encrypted_store": strconv.FormatBool(encryptedStore),
})
})
// Init a log metrics registry.
logRegistry := logmetrics.NewRegistry()
if logRegistry == nil {
panic(errors.AssertionFailedf("nil log metrics registry at server startup"))
}
// We can now connect the metric registries to the recorder.
s.recorder.AddNode(
metric.NewRegistry(), // node registry -- unused here
s.registry,
logRegistry, s.sysRegistry,
roachpb.NodeDescriptor{
NodeID: s.rpcContext.NodeID.Get(),
},
timeutil.Now().UnixNano(),
s.sqlServer.cfg.AdvertiseAddr,
s.sqlServer.cfg.HTTPAdvertiseAddr,
s.sqlServer.cfg.SQLAdvertiseAddr,
)
// If there's a higher-level recorder, we link our metrics registry to it.
if s.sqlCfg.NodeMetricsRecorder != nil {
s.sqlCfg.NodeMetricsRecorder.AddTenantRegistry(s.sqlCfg.TenantID, s.registry)
s.stopper.AddCloser(stop.CloserFn(func() {
s.sqlCfg.NodeMetricsRecorder.RemoveTenantRegistry(s.sqlCfg.TenantID)
}))
} else {
// Export statistics to graphite, if enabled by configuration. We only do
// this if there isn't a higher-level recorder; if there is, that one takes
// responsibility for exporting to Graphite.
var graphiteOnce sync.Once
graphiteEndpoint.SetOnChange(&s.ClusterSettings().SV, func(context.Context) {
if graphiteEndpoint.Get(&s.ClusterSettings().SV) != "" {
graphiteOnce.Do(func() {
startGraphiteStatsExporter(workersCtx, s.stopper, s.recorder, s.ClusterSettings())
})
}
})
}
if !s.sqlServer.cfg.DisableRuntimeStatsMonitor {
// Begin recording runtime statistics.
if err := startSampleEnvironment(workersCtx,
s.ClusterSettings(),
s.stopper,
s.sqlServer.cfg.GoroutineDumpDirName,
s.sqlServer.cfg.HeapProfileDirName,
s.sqlServer.cfg.CPUProfileDirName,
s.runtime,
s.tenantStatus.sessionRegistry,
s.sqlServer.execCfg.RootMemoryMonitor,
); err != nil {
return err
}
}
// After setting modeOperational, we can block until all stores are fully
// initialized.
s.grpc.setMode(modeOperational)
// Report server listen addresses to logs.
log.Ops.Infof(ctx, "starting %s server at %s (use: %s)",
redact.Safe(s.sqlServer.cfg.HTTPRequestScheme()),
log.SafeManaged(s.sqlServer.cfg.HTTPAddr),
log.SafeManaged(s.sqlServer.cfg.HTTPAdvertiseAddr))
rpcConnType := redact.SafeString("grpc/postgres")
if s.sqlServer.cfg.SplitListenSQL {
rpcConnType = "grpc"
log.Ops.Infof(ctx, "starting postgres server at %s (use: %s)",
log.SafeManaged(s.sqlServer.cfg.SQLAddr),
log.SafeManaged(s.sqlServer.cfg.SQLAdvertiseAddr))
}
log.Ops.Infof(ctx, "starting %s server at %s", log.SafeManaged(rpcConnType), log.SafeManaged(s.sqlServer.cfg.Addr))
log.Ops.Infof(ctx, "advertising SQL server node at %s", log.SafeManaged(s.sqlServer.cfg.AdvertiseAddr))
log.Event(ctx, "accepting connections")
// Start garbage collecting system events.
if err := startSystemLogsGC(workersCtx, s.sqlServer); err != nil {
return err
}
// Connect the HTTP endpoints. This also wraps the privileged HTTP
// endpoints served by gwMux by the HTTP cookie authentication
// check.
if err := s.http.setupRoutes(ctx,
s.authentication, /* authnServer */
s.adminAuthzCheck, /* adminAuthzCheck */
s.recorder, /* metricSource */
s.runtime, /* runtimeStatsSampler */
gwMux, /* handleRequestsUnauthenticated */
s.debug, /* handleDebugUnauthenticated */
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
apiutil.WriteJSONResponse(r.Context(), w, http.StatusNotImplemented, nil)
}),
newAPIV2Server(workersCtx, &apiV2ServerOpts{
admin: s.tenantAdmin,
status: s.tenantStatus,
promRuleExporter: s.promRuleExporter,
sqlServer: s.sqlServer,
db: s.db,
}), /* apiServer */
serverpb.FeatureFlags{
CanViewKvMetricDashboards: s.rpcContext.TenantID.Equal(roachpb.SystemTenantID),
DisableKvLevelAdvancedDebug: true,
},
); err != nil {
return err
}
// Start the SQL subsystem.
if err := s.sqlServer.preStart(
workersCtx,
s.stopper,
s.sqlServer.cfg.TestingKnobs,
orphanedLeasesTimeThresholdNanos,
); err != nil {
return err
}
// Initialize the external storage builders configuration params now that the
// engines have been created. The object can be used to create ExternalStorage
// objects hereafter.
ieMon := sql.MakeInternalExecutorMemMonitor(sql.MemoryMetrics{}, s.ClusterSettings())
ieMon.StartNoReserved(ctx, s.PGServer().SQLServer.GetBytesMonitor())
s.stopper.AddCloser(stop.CloserFn(func() { ieMon.Stop(ctx) }))
s.externalStorageBuilder.init(
s.cfg.EarlyBootExternalStorageAccessor,
s.cfg.ExternalIODirConfig,
s.sqlServer.cfg.Settings,
s.sqlServer.sqlIDContainer,
s.kvNodeDialer,
s.sqlServer.cfg.TestingKnobs,
false, /* allowLocalFastpath */
s.sqlServer.execCfg.InternalDB.
CloneWithMemoryMonitor(sql.MemoryMetrics{}, ieMon),
s.costController,
s.registry,
)
// Start the job scheduler now that the SQL Server and
// external storage is initialized.
if err := s.initJobScheduler(ctx); err != nil {
return err
}
// If enabled, start reporting diagnostics.
if s.sqlServer.cfg.StartDiagnosticsReporting && !cluster.TelemetryOptOut {
s.startDiagnostics(workersCtx)
}
// Enable the Obs Server.
// There is more logic here than in (*Server).PreStart() because
// we care about the SQL instance ID too.
clusterID := s.rpcContext.LogicalClusterID.Get()
instanceID := s.sqlServer.SQLInstanceID()
if clusterID.Equal(uuid.Nil) {
log.Fatalf(ctx, "expected LogicalClusterID to be initialized after preStart")
}
if instanceID == 0 {
log.Fatalf(ctx, "expected SQLInstanceID to be initialized after preStart")
}
s.eventsExporter.SetNodeInfo(obs.NodeInfo{
ClusterID: clusterID,
TenantID: int64(s.rpcContext.TenantID.InternalValue),
NodeID: int32(instanceID),
BinaryVersion: build.BinaryVersion(),
})
if err := s.eventsExporter.Start(ctx, s.stopper); err != nil {
return errors.Wrap(err, "failed to start the event exporter")
}
// Add more context to the Sentry reporter.
sentry.ConfigureScope(func(scope *sentry.Scope) {
scope.SetTags(map[string]string{
"cluster": clusterID.String(),
"instance": instanceID.String(),
"server_id": fmt.Sprintf("%s-%s", clusterID.Short(), instanceID.String()),
})
})
// externalUsageFn measures the CPU time, for use by tenant
// resource usage accounting in costController.Start below.
externalUsageFn := func(ctx context.Context) multitenant.ExternalUsage {
return multitenant.ExternalUsage{
CPUSecs: multitenantcpu.GetCPUSeconds(ctx),
PGWireEgressBytes: s.sqlServer.pgServer.BytesOut(),
}
}
nextLiveInstanceIDFn := makeNextLiveInstanceIDFn(s.sqlServer.sqlInstanceReader, instanceID)
// Start the cost controller for this secondary tenant.
if err := s.costController.Start(
workersCtx, s.stopper, instanceID, s.sqlServer.sqlLivenessSessionID,
externalUsageFn, nextLiveInstanceIDFn,
); err != nil {
return err
}
return nil
}
func (s *SQLServerWrapper) serveConn(
ctx context.Context, conn net.Conn, status pgwire.PreServeStatus,
) error {
pgServer := s.PGServer()
switch status.State {
case pgwire.PreServeCancel:
// Cancel requests are unauthenticated so run the cancel async to prevent
// the client from deriving any info about the cancel based on how long it
// takes.
return s.stopper.RunAsyncTask(ctx, "cancel", func(ctx context.Context) {
pgServer.HandleCancel(ctx, status.CancelKey)
})
case pgwire.PreServeReady:
return pgServer.ServeConn(ctx, conn, status)
default:
return errors.AssertionFailedf("programming error: missing case %v", status.State)
}
}
// initJobScheduler starts the job scheduler. This must be called
// after sqlServer.preStart and after our external storage providers
// have been initialized.
//
// TODO(ssd): We need to clean up the ordering/ownership here. The SQL
// server owns the job scheduler because the job scheduler needs an
// internal executor. But, the topLevelServer owns initialization of
// the external storage providers.
//
// TODO(ssd): Remove duplication with *topLevelServer.
func (s *SQLServerWrapper) initJobScheduler(ctx context.Context) error {
if s.cfg.DisableSQLServer {
return nil
}
// The job scheduler may immediately start jobs that require
// external storage providers to be available. We expect the
// server start up ordering to ensure this. Hitting this error
// is a programming error somewhere in server startup.
if err := s.externalStorageBuilder.assertInitComplete(); err != nil {
return err
}
s.sqlServer.startJobScheduler(ctx, s.cfg.TestingKnobs)
return nil
}
// AcceptClients starts listening for incoming SQL clients over the network.
// This mirrors the implementation of (*Server).AcceptClients.
// TODO(knz): Find a way to implement this method only once for both.
func (s *SQLServerWrapper) AcceptClients(ctx context.Context) error {
if s.sqlServer.cfg.DisableSQLServer {
return serverutils.PreventDisableSQLForTenantError()
}
if !s.sqlServer.cfg.DisableSQLListener {
if err := startServeSQL(
s.AnnotateCtx(context.Background()),
s.stopper,
s.pgPreServer,
s.serveConn,
s.pgL,
s.ClusterSettings(),
&s.sqlServer.cfg.SocketFile,
); err != nil {
return err
}
}
if err := structlogging.StartHotRangesLoggingScheduler(
ctx,
s.stopper,
s.sqlServer.tenantConnect,
*s.sqlServer.internalExecutor,
s.ClusterSettings(),
); err != nil {
return err
}
s.sqlServer.isReady.Set(true)
log.Event(ctx, "server ready")
return nil
}
// AcceptInternalClients starts listening for incoming SQL connections on the
// internal loopback interface.
func (s *SQLServerWrapper) AcceptInternalClients(ctx context.Context) error {
if s.sqlServer.cfg.DisableSQLServer {
return serverutils.PreventDisableSQLForTenantError()
}
connManager := netutil.MakeTCPServer(ctx, s.stopper)