-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Copy pathserver_sql.go
1105 lines (991 loc) · 41.3 KB
/
server_sql.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 2020 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"
"math"
"net"
"os"
"path/filepath"
"time"
"github.com/cockroachdb/cockroach/pkg/base"
"github.com/cockroachdb/cockroach/pkg/blobs"
"github.com/cockroachdb/cockroach/pkg/blobs/blobspb"
"github.com/cockroachdb/cockroach/pkg/cloud"
"github.com/cockroachdb/cockroach/pkg/clusterversion"
"github.com/cockroachdb/cockroach/pkg/config"
"github.com/cockroachdb/cockroach/pkg/featureflag"
"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/bulk"
"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"
"github.com/cockroachdb/cockroach/pkg/kv/kvserver/kvserverbase"
"github.com/cockroachdb/cockroach/pkg/kv/kvserver/protectedts"
"github.com/cockroachdb/cockroach/pkg/migration"
"github.com/cockroachdb/cockroach/pkg/migration/migrationcluster"
"github.com/cockroachdb/cockroach/pkg/migration/migrationmanager"
"github.com/cockroachdb/cockroach/pkg/multitenant"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/rpc"
"github.com/cockroachdb/cockroach/pkg/rpc/nodedialer"
"github.com/cockroachdb/cockroach/pkg/scheduledjobs"
"github.com/cockroachdb/cockroach/pkg/security"
"github.com/cockroachdb/cockroach/pkg/server/diagnostics"
"github.com/cockroachdb/cockroach/pkg/server/serverpb"
"github.com/cockroachdb/cockroach/pkg/server/settingswatcher"
"github.com/cockroachdb/cockroach/pkg/server/status"
"github.com/cockroachdb/cockroach/pkg/server/tracedumper"
"github.com/cockroachdb/cockroach/pkg/settings/cluster"
"github.com/cockroachdb/cockroach/pkg/spanconfig"
"github.com/cockroachdb/cockroach/pkg/spanconfig/spanconfigmanager"
"github.com/cockroachdb/cockroach/pkg/sql"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/catalogkeys"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/descs"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/hydratedtables"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/lease"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/systemschema"
"github.com/cockroachdb/cockroach/pkg/sql/colexec"
"github.com/cockroachdb/cockroach/pkg/sql/contention"
"github.com/cockroachdb/cockroach/pkg/sql/distsql"
"github.com/cockroachdb/cockroach/pkg/sql/execinfra"
"github.com/cockroachdb/cockroach/pkg/sql/execinfrapb"
"github.com/cockroachdb/cockroach/pkg/sql/flowinfra"
"github.com/cockroachdb/cockroach/pkg/sql/gcjob/gcjobnotifier"
"github.com/cockroachdb/cockroach/pkg/sql/idxusage"
"github.com/cockroachdb/cockroach/pkg/sql/optionalnodeliveness"
"github.com/cockroachdb/cockroach/pkg/sql/pgwire"
"github.com/cockroachdb/cockroach/pkg/sql/querycache"
"github.com/cockroachdb/cockroach/pkg/sql/schemachanger/scexec"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/sql/sessiondata"
"github.com/cockroachdb/cockroach/pkg/sql/sessiondatapb"
"github.com/cockroachdb/cockroach/pkg/sql/sessioninit"
"github.com/cockroachdb/cockroach/pkg/sql/sqlinstance"
"github.com/cockroachdb/cockroach/pkg/sql/sqlinstance/instanceprovider"
"github.com/cockroachdb/cockroach/pkg/sql/sqlliveness"
"github.com/cockroachdb/cockroach/pkg/sql/sqlliveness/slprovider"
"github.com/cockroachdb/cockroach/pkg/sql/sqlstats/persistedsqlstats"
"github.com/cockroachdb/cockroach/pkg/sql/sqlutil"
"github.com/cockroachdb/cockroach/pkg/sql/stats"
"github.com/cockroachdb/cockroach/pkg/sql/stmtdiagnostics"
"github.com/cockroachdb/cockroach/pkg/startupmigrations"
"github.com/cockroachdb/cockroach/pkg/storage"
"github.com/cockroachdb/cockroach/pkg/util/admission"
"github.com/cockroachdb/cockroach/pkg/util/envutil"
"github.com/cockroachdb/cockroach/pkg/util/errorutil"
"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/mon"
"github.com/cockroachdb/cockroach/pkg/util/netutil"
"github.com/cockroachdb/cockroach/pkg/util/stop"
"github.com/cockroachdb/cockroach/pkg/util/syncutil"
"github.com/cockroachdb/cockroach/pkg/util/timeutil"
"github.com/cockroachdb/cockroach/pkg/util/tracing/collector"
"github.com/cockroachdb/cockroach/pkg/util/tracing/service"
"github.com/cockroachdb/cockroach/pkg/util/tracing/tracingservicepb"
"github.com/cockroachdb/errors"
"github.com/marusama/semaphore"
"google.golang.org/grpc"
)
// SQLServer encapsulates the part of a CRDB server that is dedicated to SQL
// processing. All SQL commands are reduced to primitive operations on the
// lower-level KV layer. Multi-tenant installations of CRDB run zero or more
// standalone SQLServer instances per tenant (the KV layer is shared across all
// tenants).
type SQLServer struct {
stopper *stop.Stopper
sqlIDContainer *base.SQLIDContainer
pgServer *pgwire.Server
distSQLServer *distsql.ServerImpl
execCfg *sql.ExecutorConfig
internalExecutor *sql.InternalExecutor
leaseMgr *lease.Manager
blobService *blobs.Service
tracingService *service.Service
tenantConnect kvtenant.Connector
// sessionRegistry can be queried for info on running SQL sessions. It is
// shared between the sql.Server and the statusServer.
sessionRegistry *sql.SessionRegistry
jobRegistry *jobs.Registry
startupMigrationsMgr *startupmigrations.Manager
statsRefresher *stats.Refresher
temporaryObjectCleaner *sql.TemporaryObjectCleaner
internalMemMetrics sql.MemoryMetrics
// sqlMemMetrics are used to track memory usage of sql sessions.
sqlMemMetrics sql.MemoryMetrics
stmtDiagnosticsRegistry *stmtdiagnostics.Registry
sqlLivenessProvider sqlliveness.Provider
sqlInstanceProvider sqlinstance.Provider
metricsRegistry *metric.Registry
diagnosticsReporter *diagnostics.Reporter
// settingsWatcher is utilized by secondary tenants to watch for settings
// changes. It is nil on the system tenant.
settingsWatcher *settingswatcher.SettingsWatcher
// pgL is the shared RPC/SQL listener, opened when RPC was initialized.
pgL net.Listener
// connManager is the connection manager to use to set up additional
// SQL listeners in AcceptClients().
connManager netutil.Server
// set to true when the server has started accepting client conns.
// Used by health checks.
acceptingClients syncutil.AtomicBool
}
// sqlServerOptionalKVArgs are the arguments supplied to newSQLServer which are
// only available if the SQL server runs as part of a KV node.
//
// TODO(tbg): give all of these fields a wrapper that can signal whether the
// respective object is available. When it is not, return
// UnsupportedWithMultiTenancy.
type sqlServerOptionalKVArgs struct {
// nodesStatusServer gives access to the NodesStatus service.
nodesStatusServer serverpb.OptionalNodesStatusServer
// Narrowed down version of *NodeLiveness. Used by jobs, DistSQLPlanner, and
// migration manager.
nodeLiveness optionalnodeliveness.Container
// Gossip is relied upon by distSQLCfg (execinfra.ServerConfig), the executor
// config, the DistSQL planner, the table statistics cache, the statements
// diagnostics registry, and the lease manager.
gossip gossip.OptionalGossip
// To register blob and DistSQL servers.
grpcServer *grpc.Server
// For the temporaryObjectCleaner.
isMeta1Leaseholder func(context.Context, hlc.ClockTimestamp) (bool, error)
// DistSQL, lease management, and others want to know the node they're on.
nodeIDContainer *base.SQLIDContainer
// Used by backup/restore.
externalStorage cloud.ExternalStorageFactory
externalStorageFromURI cloud.ExternalStorageFromURIFactory
// The admission queue to use for SQLSQLResponseWork.
sqlSQLResponseAdmissionQ *admission.WorkQueue
}
// sqlServerOptionalTenantArgs are the arguments supplied to newSQLServer which
// are only available if the SQL server runs as part of a standalone SQL node.
type sqlServerOptionalTenantArgs struct {
tenantConnect kvtenant.Connector
// addr stores the SQL address binding for the pod.
addr string
}
type sqlServerArgs struct {
sqlServerOptionalKVArgs
sqlServerOptionalTenantArgs
*SQLConfig
*BaseConfig
stopper *stop.Stopper
// SQL uses the clock to assign timestamps to transactions, among many
// other things.
clock *hlc.Clock
// The RuntimeStatSampler provides metrics data to the recorder.
runtime *status.RuntimeStatSampler
// DistSQL uses rpcContext to set up flows. Less centrally, the executor
// also uses rpcContext in a number of places to learn whether the server
// is running insecure, and to read the cluster name.
rpcContext *rpc.Context
// Used by DistSQLPlanner.
nodeDescs kvcoord.NodeDescStore
// Used by the executor config.
systemConfigProvider config.SystemConfigProvider
// Used by DistSQLPlanner.
nodeDialer *nodedialer.Dialer
// SQL mostly uses the DistSender "wrapped" under a *kv.DB, but SQL also
// uses range descriptors and leaseholders, which DistSender maintains,
// for debugging and DistSQL planning purposes.
distSender *kvcoord.DistSender
// SQL uses KV, both for non-DistSQL and DistSQL execution.
db *kv.DB
// Various components want to register themselves with metrics.
registry *metric.Registry
// Recorder exposes metrics to the prometheus endpoint.
recorder *status.MetricsRecorder
// Used for SHOW/CANCEL QUERIE(S)/SESSION(S).
sessionRegistry *sql.SessionRegistry
// Used to track the contention events on this node.
contentionRegistry *contention.Registry
// Used to track the DistSQL flows scheduled on this node but initiated on
// behalf of other nodes.
flowScheduler *flowinfra.FlowScheduler
// KV depends on the internal executor, so we pass a pointer to an empty
// struct in this configuration, which newSQLServer fills.
//
// TODO(tbg): make this less hacky.
circularInternalExecutor *sql.InternalExecutor // empty initially
// Stores and deletes expired liveness sessions.
sqlLivenessProvider sqlliveness.Provider
// Stores and manages sql instance information.
sqlInstanceProvider sqlinstance.Provider
// The protected timestamps KV subsystem depends on this, so we pass a
// pointer to an empty struct in this configuration, which newSQLServer
// fills.
circularJobRegistry *jobs.Registry
jobAdoptionStopFile string
// The executorConfig uses the provider.
protectedtsProvider protectedts.Provider
// Used to list activity (sessions, queries, contention, DistSQL flows) on
// the node/cluster and cancel sessions/queries.
sqlStatusServer serverpb.SQLStatusServer
// Used to watch settings and descriptor changes.
rangeFeedFactory *rangefeed.Factory
// Used to query valid regions on the server.
regionsServer serverpb.RegionsServer
// Used for multi-tenant cost control (on the host cluster side).
tenantUsageServer multitenant.TenantUsageServer
// Used for multi-tenant cost control (on the tenant side).
costController multitenant.TenantSideCostController
// monitorAndMetrics contains the return value of newRootSQLMemoryMonitor.
monitorAndMetrics monitorAndMetrics
}
type monitorAndMetrics struct {
rootSQLMemoryMonitor *mon.BytesMonitor
rootSQLMetrics sql.BaseMemoryMetrics
}
type monitorAndMetricsOptions struct {
memoryPoolSize int64
histogramWindowInterval time.Duration
settings *cluster.Settings
}
// newRootSQLMemoryMonitor returns a started BytesMonitor and corresponding
// metrics.
func newRootSQLMemoryMonitor(opts monitorAndMetricsOptions) monitorAndMetrics {
rootSQLMetrics := sql.MakeBaseMemMetrics("root", opts.histogramWindowInterval)
// We do not set memory monitors or a noteworthy limit because the children of
// this monitor will be setting their own noteworthy limits.
rootSQLMemoryMonitor := mon.NewMonitor(
"root",
mon.MemoryResource,
rootSQLMetrics.CurBytesCount,
rootSQLMetrics.MaxBytesHist,
-1, /* increment: use default increment */
math.MaxInt64, /* noteworthy */
opts.settings,
)
// Set the limit to the memoryPoolSize. Note that this memory monitor also
// serves as a parent for a memory monitor that accounts for memory used in
// the KV layer at the same node.
rootSQLMemoryMonitor.Start(
context.Background(), nil, mon.MakeStandaloneBudget(opts.memoryPoolSize))
return monitorAndMetrics{
rootSQLMemoryMonitor: rootSQLMemoryMonitor,
rootSQLMetrics: rootSQLMetrics,
}
}
func newSQLServer(ctx context.Context, cfg sqlServerArgs) (*SQLServer, error) {
// NB: ValidateAddrs also fills in defaults.
if err := cfg.Config.ValidateAddrs(ctx); err != nil {
return nil, err
}
execCfg := &sql.ExecutorConfig{}
codec := keys.MakeSQLCodec(cfg.SQLConfig.TenantID)
if knobs := cfg.TestingKnobs.TenantTestingKnobs; knobs != nil {
override := knobs.(*sql.TenantTestingKnobs).TenantIDCodecOverride
if override != (roachpb.TenantID{}) {
codec = keys.MakeSQLCodec(override)
}
}
// Create blob service for inter-node file sharing.
blobService, err := blobs.NewBlobService(cfg.Settings.ExternalIODir)
if err != nil {
return nil, errors.Wrap(err, "creating blob service")
}
blobspb.RegisterBlobServer(cfg.grpcServer, blobService)
// Create trace service for inter-node sharing of inflight trace spans.
tracingService := service.New(cfg.Settings.Tracer)
tracingservicepb.RegisterTracingServer(cfg.grpcServer, tracingService)
cfg.sqlLivenessProvider = slprovider.New(
cfg.stopper, cfg.clock, cfg.db, codec, cfg.Settings,
)
cfg.sqlInstanceProvider = instanceprovider.New(
cfg.stopper, cfg.db, codec, cfg.sqlLivenessProvider, cfg.addr,
)
jobRegistry := cfg.circularJobRegistry
{
cfg.registry.AddMetricStruct(cfg.sqlLivenessProvider.Metrics())
var jobsKnobs *jobs.TestingKnobs
if cfg.TestingKnobs.JobsTestingKnobs != nil {
jobsKnobs = cfg.TestingKnobs.JobsTestingKnobs.(*jobs.TestingKnobs)
}
td := tracedumper.NewTraceDumper(ctx, cfg.InflightTraceDirName, cfg.Settings)
*jobRegistry = *jobs.MakeRegistry(
cfg.AmbientCtx,
cfg.stopper,
cfg.clock,
cfg.db,
cfg.circularInternalExecutor,
cfg.nodeIDContainer,
cfg.sqlLivenessProvider,
cfg.Settings,
cfg.HistogramWindowInterval(),
func(opName string, user security.SQLUsername) (interface{}, func()) {
// This is a hack to get around a Go package dependency cycle. See comment
// in sql/jobs/registry.go on planHookMaker.
return sql.MakeJobExecContext(opName, user, &sql.MemoryMetrics{}, execCfg)
},
cfg.jobAdoptionStopFile,
td,
jobsKnobs,
)
}
cfg.registry.AddMetricStruct(jobRegistry.MetricsStruct())
distSQLMetrics := execinfra.MakeDistSQLMetrics(cfg.HistogramWindowInterval())
cfg.registry.AddMetricStruct(distSQLMetrics)
// Set up Lease Manager
var lmKnobs lease.ManagerTestingKnobs
if leaseManagerTestingKnobs := cfg.TestingKnobs.SQLLeaseManager; leaseManagerTestingKnobs != nil {
lmKnobs = *leaseManagerTestingKnobs.(*lease.ManagerTestingKnobs)
}
leaseMgr := lease.NewLeaseManager(
cfg.AmbientCtx,
cfg.nodeIDContainer,
cfg.db,
cfg.clock,
cfg.circularInternalExecutor,
cfg.Settings,
codec,
lmKnobs,
cfg.stopper,
cfg.rangeFeedFactory,
)
cfg.registry.AddMetricStruct(leaseMgr.MetricsStruct())
rootSQLMetrics := cfg.monitorAndMetrics.rootSQLMetrics
cfg.registry.AddMetricStruct(rootSQLMetrics)
// Set up internal memory metrics for use by internal SQL executors.
internalMemMetrics := sql.MakeMemMetrics("internal", cfg.HistogramWindowInterval())
cfg.registry.AddMetricStruct(internalMemMetrics)
rootSQLMemoryMonitor := cfg.monitorAndMetrics.rootSQLMemoryMonitor
// bulkMemoryMonitor is the parent to all child SQL monitors tracking bulk
// operations (IMPORT, index backfill). It is itself a child of the
// ParentMemoryMonitor.
bulkMemoryMonitor := mon.NewMonitorInheritWithLimit("bulk-mon", 0 /* limit */, rootSQLMemoryMonitor)
bulkMetrics := bulk.MakeBulkMetrics(cfg.HistogramWindowInterval())
cfg.registry.AddMetricStruct(bulkMetrics)
bulkMemoryMonitor.SetMetrics(bulkMetrics.CurBytesCount, bulkMetrics.MaxBytesHist)
bulkMemoryMonitor.Start(context.Background(), rootSQLMemoryMonitor, mon.BoundAccount{})
backfillMemoryMonitor := execinfra.NewMonitor(ctx, bulkMemoryMonitor, "backfill-mon")
serverCacheMemoryMonitor := mon.NewMonitorInheritWithLimit(
"server-cache-mon", 0 /* limit */, rootSQLMemoryMonitor,
)
serverCacheMemoryMonitor.Start(context.Background(), rootSQLMemoryMonitor, mon.BoundAccount{})
// Set up the DistSQL temp engine.
useStoreSpec := cfg.TempStorageConfig.Spec
tempEngine, tempFS, err := storage.NewTempEngine(ctx, cfg.TempStorageConfig, useStoreSpec)
if err != nil {
return nil, errors.Wrap(err, "creating temp storage")
}
cfg.stopper.AddCloser(tempEngine)
// Remove temporary directory linked to tempEngine after closing
// tempEngine.
cfg.stopper.AddCloser(stop.CloserFn(func() {
useStore := cfg.TempStorageConfig.Spec
var err error
if useStore.InMemory {
// Used store is in-memory so we remove the temp
// directory directly since there is no record file.
err = os.RemoveAll(cfg.TempStorageConfig.Path)
} else {
// If record file exists, we invoke CleanupTempDirs to
// also remove the record after the temp directory is
// removed.
recordPath := filepath.Join(useStore.Path, TempDirsRecordFilename)
err = storage.CleanupTempDirs(recordPath)
}
if err != nil {
log.Errorf(ctx, "could not remove temporary store directory: %v", err.Error())
}
}))
virtualSchemas, err := sql.NewVirtualSchemaHolder(ctx, cfg.Settings)
if err != nil {
return nil, errors.Wrap(err, "creating virtual schema holder")
}
hydratedTablesCache := hydratedtables.NewCache(cfg.Settings)
cfg.registry.AddMetricStruct(hydratedTablesCache.Metrics())
gcJobNotifier := gcjobnotifier.New(cfg.Settings, cfg.systemConfigProvider, codec, cfg.stopper)
var compactEngineSpanFunc tree.CompactEngineSpanFunc
if !codec.ForSystemTenant() {
compactEngineSpanFunc = func(
ctx context.Context, nodeID, storeID int32, startKey, endKey []byte,
) error {
return errorutil.UnsupportedWithMultiTenancy(errorutil.FeatureNotAvailableToNonSystemTenantsIssue)
}
} else {
cli := kvserver.NewCompactEngineSpanClient(cfg.nodeDialer)
compactEngineSpanFunc = cli.CompactEngineSpan
}
collectionFactory := descs.NewCollectionFactory(
cfg.Settings,
leaseMgr,
virtualSchemas,
hydratedTablesCache,
)
// Set up the DistSQL server.
distSQLCfg := execinfra.ServerConfig{
AmbientContext: cfg.AmbientCtx,
Settings: cfg.Settings,
RuntimeStats: cfg.runtime,
ClusterID: &cfg.rpcContext.ClusterID,
ClusterName: cfg.ClusterName,
NodeID: cfg.nodeIDContainer,
Locality: cfg.Locality,
Codec: codec,
DB: cfg.db,
Executor: cfg.circularInternalExecutor,
RPCContext: cfg.rpcContext,
Stopper: cfg.stopper,
TempStorage: tempEngine,
TempStoragePath: cfg.TempStorageConfig.Path,
TempFS: tempFS,
// COCKROACH_VEC_MAX_OPEN_FDS specifies the maximum number of open file
// descriptors that the vectorized execution engine may have open at any
// one time. This limit is implemented as a weighted semaphore acquired
// before opening files.
VecFDSemaphore: semaphore.New(envutil.EnvOrDefaultInt("COCKROACH_VEC_MAX_OPEN_FDS", colexec.VecMaxOpenFDsLimit)),
ParentDiskMonitor: cfg.TempStorageConfig.Mon,
BackfillerMonitor: backfillMemoryMonitor,
ParentMemoryMonitor: rootSQLMemoryMonitor,
BulkAdder: func(
ctx context.Context, db *kv.DB, ts hlc.Timestamp, opts kvserverbase.BulkAdderOptions,
) (kvserverbase.BulkAdder, error) {
// Attach a child memory monitor to enable control over the BulkAdder's
// memory usage.
bulkMon := execinfra.NewMonitor(ctx, bulkMemoryMonitor, "bulk-adder-monitor")
if !codec.ForSystemTenant() {
// Tenants aren't allowed to split, so force off the split-after opt.
opts.SplitAndScatterAfter = func() int64 { return kvserverbase.DisableExplicitSplits }
}
return bulk.MakeBulkAdder(ctx, db, cfg.distSender.RangeDescriptorCache(), cfg.Settings, ts, opts, bulkMon)
},
Metrics: &distSQLMetrics,
SQLLivenessReader: cfg.sqlLivenessProvider,
JobRegistry: jobRegistry,
Gossip: cfg.gossip,
NodeDialer: cfg.nodeDialer,
LeaseManager: leaseMgr,
ExternalStorage: cfg.externalStorage,
ExternalStorageFromURI: cfg.externalStorageFromURI,
RangeCache: cfg.distSender.RangeDescriptorCache(),
SQLSQLResponseAdmissionQ: cfg.sqlSQLResponseAdmissionQ,
CollectionFactory: collectionFactory,
}
cfg.TempStorageConfig.Mon.SetMetrics(distSQLMetrics.CurDiskBytesCount, distSQLMetrics.MaxDiskBytesHist)
if distSQLTestingKnobs := cfg.TestingKnobs.DistSQL; distSQLTestingKnobs != nil {
distSQLCfg.TestingKnobs = *distSQLTestingKnobs.(*execinfra.TestingKnobs)
}
if cfg.TestingKnobs.JobsTestingKnobs != nil {
distSQLCfg.TestingKnobs.JobsTestingKnobs = cfg.TestingKnobs.JobsTestingKnobs
}
distSQLServer := distsql.NewServer(ctx, distSQLCfg, cfg.flowScheduler)
execinfrapb.RegisterDistSQLServer(cfg.grpcServer, distSQLServer)
// Set up Executor
var sqlExecutorTestingKnobs sql.ExecutorTestingKnobs
if k := cfg.TestingKnobs.SQLExecutor; k != nil {
sqlExecutorTestingKnobs = *k.(*sql.ExecutorTestingKnobs)
} else {
sqlExecutorTestingKnobs = sql.ExecutorTestingKnobs{}
}
nodeInfo := sql.NodeInfo{
AdminURL: cfg.AdminURL,
PGURL: cfg.rpcContext.PGURL,
ClusterID: cfg.rpcContext.ClusterID.Get,
NodeID: cfg.nodeIDContainer,
}
var isAvailable func(roachpb.NodeID) bool
nodeLiveness, hasNodeLiveness := cfg.nodeLiveness.Optional(47900)
if hasNodeLiveness {
// TODO(erikgrinaker): We may want to use IsAvailableNotDraining instead, to
// avoid scheduling long-running flows (e.g. rangefeeds or backups) on nodes
// that are being drained/decommissioned. However, these nodes can still be
// leaseholders, and preventing processor scheduling on them can cause a
// performance cliff for e.g. table reads that then hit the network.
isAvailable = nodeLiveness.IsAvailable
} else {
// We're on a SQL tenant, so this is the only node DistSQL will ever
// schedule on - always returning true is fine.
isAvailable = func(roachpb.NodeID) bool {
return true
}
}
// Setup the trace collector that is used to fetch inflight trace spans from
// all nodes in the cluster.
// The collector requires nodeliveness to get a list of all the nodes in the
// cluster.
var traceCollector *collector.TraceCollector
if hasNodeLiveness {
traceCollector = collector.New(cfg.nodeDialer, nodeLiveness, cfg.Settings.Tracer)
}
*execCfg = sql.ExecutorConfig{
Settings: cfg.Settings,
NodeInfo: nodeInfo,
Codec: codec,
DefaultZoneConfig: &cfg.DefaultZoneConfig,
Locality: cfg.Locality,
AmbientCtx: cfg.AmbientCtx,
DB: cfg.db,
Gossip: cfg.gossip,
NodeLiveness: cfg.nodeLiveness,
SystemConfig: cfg.systemConfigProvider,
MetricsRecorder: cfg.recorder,
DistSender: cfg.distSender,
RPCContext: cfg.rpcContext,
LeaseManager: leaseMgr,
Clock: cfg.clock,
DistSQLSrv: distSQLServer,
NodesStatusServer: cfg.nodesStatusServer,
SQLStatusServer: cfg.sqlStatusServer,
RegionsServer: cfg.regionsServer,
SessionRegistry: cfg.sessionRegistry,
ContentionRegistry: cfg.contentionRegistry,
SQLLivenessReader: cfg.sqlLivenessProvider,
JobRegistry: jobRegistry,
VirtualSchemas: virtualSchemas,
HistogramWindowInterval: cfg.HistogramWindowInterval(),
RangeDescriptorCache: cfg.distSender.RangeDescriptorCache(),
RoleMemberCache: sql.NewMembershipCache(serverCacheMemoryMonitor.MakeBoundAccount()),
SessionInitCache: sessioninit.NewCache(serverCacheMemoryMonitor.MakeBoundAccount()),
RootMemoryMonitor: rootSQLMemoryMonitor,
TestingKnobs: sqlExecutorTestingKnobs,
CompactEngineSpanFunc: compactEngineSpanFunc,
TraceCollector: traceCollector,
TenantUsageServer: cfg.tenantUsageServer,
DistSQLPlanner: sql.NewDistSQLPlanner(
ctx,
execinfra.Version,
cfg.Settings,
roachpb.NodeID(cfg.nodeIDContainer.SQLInstanceID()),
cfg.rpcContext,
distSQLServer,
cfg.distSender,
cfg.nodeDescs,
cfg.gossip,
cfg.stopper,
isAvailable,
cfg.nodeDialer,
),
TableStatsCache: stats.NewTableStatisticsCache(
ctx,
cfg.TableStatCacheSize,
cfg.db,
cfg.circularInternalExecutor,
codec,
cfg.Settings,
cfg.rangeFeedFactory,
collectionFactory,
),
QueryCache: querycache.New(cfg.QueryCacheSize),
ProtectedTimestampProvider: cfg.protectedtsProvider,
ExternalIODirConfig: cfg.ExternalIODirConfig,
GCJobNotifier: gcJobNotifier,
RangeFeedFactory: cfg.rangeFeedFactory,
CollectionFactory: collectionFactory,
}
if sqlSchemaChangerTestingKnobs := cfg.TestingKnobs.SQLSchemaChanger; sqlSchemaChangerTestingKnobs != nil {
execCfg.SchemaChangerTestingKnobs = sqlSchemaChangerTestingKnobs.(*sql.SchemaChangerTestingKnobs)
} else {
execCfg.SchemaChangerTestingKnobs = new(sql.SchemaChangerTestingKnobs)
}
if sqlNewSchemaChangerTestingKnobs := cfg.TestingKnobs.SQLNewSchemaChanger; sqlNewSchemaChangerTestingKnobs != nil {
execCfg.NewSchemaChangerTestingKnobs = sqlNewSchemaChangerTestingKnobs.(*scexec.NewSchemaChangerTestingKnobs)
} else {
execCfg.NewSchemaChangerTestingKnobs = new(scexec.NewSchemaChangerTestingKnobs)
}
if sqlTypeSchemaChangerTestingKnobs := cfg.TestingKnobs.SQLTypeSchemaChanger; sqlTypeSchemaChangerTestingKnobs != nil {
execCfg.TypeSchemaChangerTestingKnobs = sqlTypeSchemaChangerTestingKnobs.(*sql.TypeSchemaChangerTestingKnobs)
} else {
execCfg.TypeSchemaChangerTestingKnobs = new(sql.TypeSchemaChangerTestingKnobs)
}
execCfg.SchemaChangerMetrics = sql.NewSchemaChangerMetrics()
cfg.registry.AddMetricStruct(execCfg.SchemaChangerMetrics)
execCfg.FeatureFlagMetrics = featureflag.NewFeatureFlagMetrics()
cfg.registry.AddMetricStruct(execCfg.FeatureFlagMetrics)
if gcJobTestingKnobs := cfg.TestingKnobs.GCJob; gcJobTestingKnobs != nil {
execCfg.GCJobTestingKnobs = gcJobTestingKnobs.(*sql.GCJobTestingKnobs)
} else {
execCfg.GCJobTestingKnobs = new(sql.GCJobTestingKnobs)
}
if distSQLRunTestingKnobs := cfg.TestingKnobs.DistSQL; distSQLRunTestingKnobs != nil {
execCfg.DistSQLRunTestingKnobs = distSQLRunTestingKnobs.(*execinfra.TestingKnobs)
} else {
execCfg.DistSQLRunTestingKnobs = new(execinfra.TestingKnobs)
}
if sqlEvalContext := cfg.TestingKnobs.SQLEvalContext; sqlEvalContext != nil {
execCfg.EvalContextTestingKnobs = *sqlEvalContext.(*tree.EvalContextTestingKnobs)
}
if pgwireKnobs := cfg.TestingKnobs.PGWireTestingKnobs; pgwireKnobs != nil {
execCfg.PGWireTestingKnobs = pgwireKnobs.(*sql.PGWireTestingKnobs)
}
if tenantKnobs := cfg.TestingKnobs.TenantTestingKnobs; tenantKnobs != nil {
execCfg.TenantTestingKnobs = tenantKnobs.(*sql.TenantTestingKnobs)
}
if backupRestoreKnobs := cfg.TestingKnobs.BackupRestore; backupRestoreKnobs != nil {
execCfg.BackupRestoreTestingKnobs = backupRestoreKnobs.(*sql.BackupRestoreTestingKnobs)
}
if indexUsageStatsKnobs := cfg.TestingKnobs.IndexUsageStatsKnobs; indexUsageStatsKnobs != nil {
execCfg.IndexUsageStatsTestingKnobs = indexUsageStatsKnobs.(*idxusage.TestingKnobs)
}
if sqlStatsKnobs := cfg.TestingKnobs.SQLStatsKnobs; sqlStatsKnobs != nil {
execCfg.SQLStatsTestingKnobs = sqlStatsKnobs.(*persistedsqlstats.TestingKnobs)
}
statsRefresher := stats.MakeRefresher(
cfg.Settings,
cfg.circularInternalExecutor,
execCfg.TableStatsCache,
stats.DefaultAsOfTime,
)
execCfg.StatsRefresher = statsRefresher
// Set up internal memory metrics for use by internal SQL executors.
// Don't add them to the registry now because it will be added as part of pgServer metrics.
sqlMemMetrics := sql.MakeMemMetrics("sql", cfg.HistogramWindowInterval())
pgServer := pgwire.MakeServer(
cfg.AmbientCtx,
cfg.Config,
cfg.Settings,
sqlMemMetrics,
rootSQLMemoryMonitor,
cfg.HistogramWindowInterval(),
execCfg,
)
distSQLServer.ServerConfig.SQLStatsController = pgServer.SQLServer.GetSQLStatsController()
// Now that we have a pgwire.Server (which has a sql.Server), we can close a
// circular dependency between the rowexec.Server and sql.Server and set
// SessionBoundInternalExecutorFactory. The same applies for setting a
// SessionBoundInternalExecutor on the job registry.
ieFactory := func(
ctx context.Context, sessionData *sessiondata.SessionData,
) sqlutil.InternalExecutor {
ie := sql.MakeInternalExecutor(
ctx,
pgServer.SQLServer,
internalMemMetrics,
cfg.Settings,
)
ie.SetSessionData(sessionData)
return &ie
}
distSQLServer.ServerConfig.SessionBoundInternalExecutorFactory = ieFactory
jobRegistry.SetSessionBoundInternalExecutorFactory(ieFactory)
execCfg.IndexBackfiller = sql.NewIndexBackfiller(execCfg, ieFactory)
distSQLServer.ServerConfig.ProtectedTimestampProvider = execCfg.ProtectedTimestampProvider
for _, m := range pgServer.Metrics() {
cfg.registry.AddMetricStruct(m)
}
*cfg.circularInternalExecutor = sql.MakeInternalExecutor(
ctx, pgServer.SQLServer, internalMemMetrics, cfg.Settings,
)
execCfg.InternalExecutor = cfg.circularInternalExecutor
stmtDiagnosticsRegistry := stmtdiagnostics.NewRegistry(
cfg.circularInternalExecutor,
cfg.db,
cfg.gossip,
cfg.Settings,
)
execCfg.StmtDiagnosticsRecorder = stmtDiagnosticsRegistry
{
// We only need to attach a version upgrade hook if we're the system
// tenant. Regular tenants are disallowed from changing cluster
// versions.
var c migration.Cluster
if codec.ForSystemTenant() {
c = migrationcluster.New(migrationcluster.ClusterConfig{
NodeLiveness: nodeLiveness,
Dialer: cfg.nodeDialer,
DB: cfg.db,
})
} else {
c = migrationcluster.NewTenantCluster(cfg.db)
}
knobs, _ := cfg.TestingKnobs.MigrationManager.(*migrationmanager.TestingKnobs)
migrationMgr := migrationmanager.NewManager(
c, cfg.circularInternalExecutor, jobRegistry, codec, cfg.Settings, knobs,
)
execCfg.MigrationJobDeps = migrationMgr
execCfg.VersionUpgradeHook = migrationMgr.Migrate
}
{
// Instantiate a span config manager which exposes a hook to start the auto
// span config reconciliation during server startup. The span config manager
// provides the reconciliation job with access to dependencies it needs to
// perform its task. The job does so through the executor config.
knobs, _ := cfg.TestingKnobs.SpanConfig.(*spanconfig.TestingKnobs)
reconciliationMgr := spanconfigmanager.New(
cfg.db,
jobRegistry,
cfg.circularInternalExecutor,
cfg.stopper,
knobs,
)
execCfg.SpanConfigReconciliationJobDeps = reconciliationMgr
execCfg.StartSpanConfigReconciliationJobHook = reconciliationMgr.StartJobIfNoneExists
}
temporaryObjectCleaner := sql.NewTemporaryObjectCleaner(
cfg.Settings,
cfg.db,
codec,
cfg.registry,
distSQLServer.ServerConfig.SessionBoundInternalExecutorFactory,
cfg.sqlStatusServer,
cfg.isMeta1Leaseholder,
sqlExecutorTestingKnobs,
collectionFactory,
)
reporter := &diagnostics.Reporter{
StartTime: timeutil.Now(),
AmbientCtx: &cfg.AmbientCtx,
Config: cfg.BaseConfig.Config,
Settings: cfg.Settings,
ClusterID: cfg.rpcContext.ClusterID.Get,
TenantID: cfg.rpcContext.TenantID,
SQLInstanceID: cfg.nodeIDContainer.SQLInstanceID,
SQLServer: pgServer.SQLServer,
InternalExec: cfg.circularInternalExecutor,
DB: cfg.db,
Recorder: cfg.recorder,
Locality: cfg.Locality,
}
if cfg.TestingKnobs.Server != nil {
reporter.TestingKnobs = &cfg.TestingKnobs.Server.(*TestingKnobs).DiagnosticsTestingKnobs
}
var settingsWatcher *settingswatcher.SettingsWatcher
if !codec.ForSystemTenant() {
settingsWatcher = settingswatcher.New(
cfg.clock, codec, cfg.Settings, cfg.rangeFeedFactory, cfg.stopper,
)
}
return &SQLServer{
stopper: cfg.stopper,
sqlIDContainer: cfg.nodeIDContainer,
pgServer: pgServer,
distSQLServer: distSQLServer,
execCfg: execCfg,
internalExecutor: cfg.circularInternalExecutor,
leaseMgr: leaseMgr,
blobService: blobService,
tracingService: tracingService,
tenantConnect: cfg.tenantConnect,
sessionRegistry: cfg.sessionRegistry,
jobRegistry: jobRegistry,
statsRefresher: statsRefresher,
temporaryObjectCleaner: temporaryObjectCleaner,
internalMemMetrics: internalMemMetrics,
sqlMemMetrics: sqlMemMetrics,
stmtDiagnosticsRegistry: stmtDiagnosticsRegistry,
sqlLivenessProvider: cfg.sqlLivenessProvider,
sqlInstanceProvider: cfg.sqlInstanceProvider,
metricsRegistry: cfg.registry,
diagnosticsReporter: reporter,
settingsWatcher: settingsWatcher,
}, nil
}
// Checks if tenant exists. This function does a very superficial check to see if the system db
// has been bootstrapped for the tenant. This is not a complete check and is only sufficient
// to be used in the dev environment.
func maybeCheckTenantExists(ctx context.Context, codec keys.SQLCodec, db *kv.DB) error {
if codec.ForSystemTenant() {
// Skip check for system tenant and return early.
return nil
}
key := catalogkeys.MakeDatabaseNameKey(codec, systemschema.SystemDatabaseName)
result, err := db.Get(ctx, key)
if err != nil {
return err
}
if result.Value == nil || result.ValueInt() != keys.SystemDatabaseID {
return errors.New("system DB uninitialized, check if tenant is non existent")
}
// Tenant has been confirmed to be bootstrapped successfully
// as the system database, which is a part of the bootstrap data for
// a tenant keyspace, exists in the namespace table.
return nil
}
func (s *SQLServer) startSQLLivenessAndInstanceProviders(ctx context.Context) error {
// If necessary, start the tenant proxy first, to ensure all other
// components can properly route to KV nodes. The Start method will block
// until a connection is established to the cluster and its ID has been
// determined.
if s.tenantConnect != nil {
if err := s.tenantConnect.Start(ctx); err != nil {
return err
}
}
s.sqlLivenessProvider.Start(ctx)
return nil
}
func (s *SQLServer) initInstanceID(ctx context.Context) error {
if _, ok := s.sqlIDContainer.OptionalNodeID(); ok {
// sqlIDContainer has already been initialized with a node ID,
// we don't need to initialize a SQL instance ID in this case
// as this is not a SQL pod server.
return nil
}
instanceID, err := s.sqlInstanceProvider.Instance(ctx)
if err != nil {
return err
}
err = s.sqlIDContainer.SetSQLInstanceID(instanceID)
if err != nil {
return err
}
s.execCfg.DistSQLPlanner.SetNodeInfo(roachpb.NodeDescriptor{NodeID: roachpb.NodeID(instanceID)})
return nil
}
func (s *SQLServer) preStart(
ctx context.Context,
stopper *stop.Stopper,
knobs base.TestingKnobs,
connManager netutil.Server,
pgL net.Listener,
socketFile string,
orphanedLeasesTimeThresholdNanos int64,
) error {
// The sqlliveness and sqlinstance subsystem should be started first to ensure instance ID is
// initialized prior to any other systems that need it.
if err := s.startSQLLivenessAndInstanceProviders(ctx); err != nil {
return err
}
// Confirm tenant exists prior to initialization. This is a sanity
// check for the dev environment to ensure that a tenant has been
// successfully created before attempting to initialize a SQL
// server for it.
if err := maybeCheckTenantExists(ctx, s.execCfg.Codec, s.execCfg.DB); err != nil {
return err
}
if err := s.initInstanceID(ctx); err != nil {
return err
}
s.connManager = connManager
s.pgL = pgL
s.execCfg.GCJobNotifier.Start(ctx)
s.temporaryObjectCleaner.Start(ctx, stopper)
s.distSQLServer.Start()
s.pgServer.Start(ctx, stopper)
if err := s.statsRefresher.Start(ctx, stopper, stats.DefaultRefreshInterval); err != nil {
return err
}
s.stmtDiagnosticsRegistry.Start(ctx, stopper)
// Before serving SQL requests, we have to make sure the database is
// in an acceptable form for this version of the software.
// We have to do this after actually starting up the server to be able to
// seamlessly use the kv client against other nodes in the cluster.
var mmKnobs startupmigrations.MigrationManagerTestingKnobs
if migrationManagerTestingKnobs := knobs.StartupMigrationManager; migrationManagerTestingKnobs != nil {
mmKnobs = *migrationManagerTestingKnobs.(*startupmigrations.MigrationManagerTestingKnobs)
}
s.leaseMgr.RefreshLeases(ctx, stopper, s.execCfg.DB)
s.leaseMgr.PeriodicallyRefreshSomeLeases(ctx)
migrationsExecutor := sql.MakeInternalExecutor(
ctx, s.pgServer.SQLServer, s.internalMemMetrics, s.execCfg.Settings)
migrationsExecutor.SetSessionData(
&sessiondata.SessionData{
LocalOnlySessionData: sessiondata.LocalOnlySessionData{
// Migrations need an executor with query distribution turned off. This is
// because the node crashes if migrations fail to execute, and query
// distribution introduces more moving parts. Local execution is more
// robust; for example, the DistSender has retries if it can't connect to
// another node, but DistSQL doesn't. Also see #44101 for why DistSQL is
// particularly fragile immediately after a node is started (i.e. the
// present situation).
DistSQLMode: sessiondata.DistSQLOff,
},
})