-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.go
1301 lines (1140 loc) · 42.7 KB
/
server.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.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.
package server
import (
"compress/gzip"
"crypto/tls"
"fmt"
"io"
"io/ioutil"
"math"
"net"
"net/http"
"net/url"
"os"
"path/filepath"
"strconv"
"strings"
"sync"
"sync/atomic"
"time"
"golang.org/x/net/context"
"github.com/elazarl/go-bindata-assetfs"
raven "github.com/getsentry/raven-go"
gwruntime "github.com/grpc-ecosystem/grpc-gateway/runtime"
opentracing "github.com/opentracing/opentracing-go"
"github.com/pkg/errors"
"google.golang.org/grpc"
"github.com/cockroachdb/cmux"
"github.com/cockroachdb/cockroach/pkg/base"
"github.com/cockroachdb/cockroach/pkg/gossip"
"github.com/cockroachdb/cockroach/pkg/internal/client"
"github.com/cockroachdb/cockroach/pkg/kv"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/rpc"
"github.com/cockroachdb/cockroach/pkg/security"
"github.com/cockroachdb/cockroach/pkg/server/serverpb"
"github.com/cockroachdb/cockroach/pkg/server/status"
"github.com/cockroachdb/cockroach/pkg/settings/cluster"
"github.com/cockroachdb/cockroach/pkg/sql"
"github.com/cockroachdb/cockroach/pkg/sql/distsqlrun"
"github.com/cockroachdb/cockroach/pkg/sql/jobs"
"github.com/cockroachdb/cockroach/pkg/sql/mon"
"github.com/cockroachdb/cockroach/pkg/sql/pgwire"
migrations "github.com/cockroachdb/cockroach/pkg/sqlmigrations"
"github.com/cockroachdb/cockroach/pkg/storage"
"github.com/cockroachdb/cockroach/pkg/storage/engine"
"github.com/cockroachdb/cockroach/pkg/ts"
"github.com/cockroachdb/cockroach/pkg/ui"
"github.com/cockroachdb/cockroach/pkg/util"
"github.com/cockroachdb/cockroach/pkg/util/envutil"
"github.com/cockroachdb/cockroach/pkg/util/hlc"
"github.com/cockroachdb/cockroach/pkg/util/httputil"
"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/protoutil"
"github.com/cockroachdb/cockroach/pkg/util/retry"
"github.com/cockroachdb/cockroach/pkg/util/sdnotify"
"github.com/cockroachdb/cockroach/pkg/util/stop"
"github.com/cockroachdb/cockroach/pkg/util/timeutil"
"github.com/cockroachdb/cockroach/pkg/util/uuid"
)
var (
// Allocation pool for gzip writers.
gzipWriterPool sync.Pool
// GracefulDrainModes is the standard succession of drain modes entered
// for a graceful shutdown.
GracefulDrainModes = []serverpb.DrainMode{serverpb.DrainMode_CLIENT, serverpb.DrainMode_LEASES}
// LicenseCheckFn is used to check if the current cluster has any enterprise
// features enabled. This function is overridden by an init hook in CCL
// builds.
LicenseCheckFn = func(
st *cluster.Settings, cluster uuid.UUID, org, feature string,
) error {
return errors.New("OSS build does not include Enterprise features")
}
)
// Server is the cockroach server node.
type Server struct {
nodeIDContainer base.NodeIDContainer
cfg Config
st *cluster.Settings
mux *http.ServeMux
clock *hlc.Clock
rpcContext *rpc.Context
grpc *grpc.Server
gossip *gossip.Gossip
nodeLiveness *storage.NodeLiveness
storePool *storage.StorePool
txnCoordSender *kv.TxnCoordSender
distSender *kv.DistSender
db *client.DB
kvDB *kv.DBServer
pgServer *pgwire.Server
distSQLServer *distsqlrun.ServerImpl
node *Node
registry *metric.Registry
recorder *status.MetricsRecorder
runtime status.RuntimeStatSampler
admin *adminServer
status *statusServer
authentication *authenticationServer
tsDB *ts.DB
tsServer ts.Server
raftTransport *storage.RaftTransport
stopper *stop.Stopper
sqlExecutor *sql.Executor
leaseMgr *sql.LeaseManager
sessionRegistry *sql.SessionRegistry
jobRegistry *jobs.Registry
engines Engines
internalMemMetrics sql.MemoryMetrics
adminMemMetrics sql.MemoryMetrics
}
// NewServer creates a Server from a server.Context.
func NewServer(cfg Config, stopper *stop.Stopper) (*Server, error) {
if _, err := net.ResolveTCPAddr("tcp", cfg.AdvertiseAddr); err != nil {
return nil, errors.Errorf("unable to resolve RPC address %q: %v", cfg.AdvertiseAddr, err)
}
st := cfg.Settings
if cfg.AmbientCtx.Tracer == nil {
panic(errors.New("no tracer set in AmbientCtx"))
}
s := &Server{
st: st,
mux: http.NewServeMux(),
clock: hlc.NewClock(hlc.UnixNano, time.Duration(cfg.MaxOffset)),
stopper: stopper,
cfg: cfg,
registry: metric.NewRegistry(),
}
// If the tracer has a Close function, call it after the server stops.
if tr, ok := cfg.AmbientCtx.Tracer.(stop.Closer); ok {
stopper.AddCloser(tr)
}
// Attempt to load TLS configs right away, failures are permanent.
if certMgr, err := cfg.InitializeNodeTLSConfigs(stopper); err != nil {
return nil, err
} else if certMgr != nil {
// The certificate manager is non-nil in secure mode.
s.registry.AddMetricStruct(certMgr.Metrics())
}
// Add a dynamic log tag value for the node ID.
//
// We need to pass an ambient context to the various server components, but we
// won't know the node ID until we Start(). At that point it's too late to
// change the ambient contexts in the components (various background processes
// will have already started using them).
//
// NodeIDContainer allows us to add the log tag to the context now and update
// the value asynchronously. It's not significantly more expensive than a
// regular tag since it's just doing an (atomic) load when a log/trace message
// is constructed. The node ID is set by the Store if this host was
// bootstrapped; otherwise a new one is allocated in Node.
s.cfg.AmbientCtx.AddLogTag("n", &s.nodeIDContainer)
ctx := s.AnnotateCtx(context.Background())
s.rpcContext = rpc.NewContext(s.cfg.AmbientCtx, s.cfg.Config, s.clock, s.stopper)
s.rpcContext.HeartbeatCB = func() {
if err := s.rpcContext.RemoteClocks.VerifyClockOffset(ctx); err != nil {
log.Fatal(ctx, err)
}
}
s.grpc = rpc.NewServer(s.rpcContext)
s.gossip = gossip.New(
s.cfg.AmbientCtx,
&s.nodeIDContainer,
s.rpcContext,
s.grpc,
s.stopper,
s.registry,
)
// A custom RetryOptions is created which uses stopper.ShouldQuiesce() as
// the Closer. This prevents infinite retry loops from occurring during
// graceful server shutdown
//
// Such a loop occurs when the DistSender attempts a connection to the
// local server during shutdown, and receives an internal server error (HTTP
// Code 5xx). This is the correct error for a server to return when it is
// shutting down, and is normally retryable in a cluster environment.
// However, on a single-node setup (such as a test), retries will never
// succeed because the only server has been shut down; thus, the
// DistSender needs to know that it should not retry in this situation.
retryOpts := s.cfg.RetryOptions
if retryOpts == (retry.Options{}) {
retryOpts = base.DefaultRetryOptions()
}
retryOpts.Closer = s.stopper.ShouldQuiesce()
distSenderCfg := kv.DistSenderConfig{
AmbientCtx: s.cfg.AmbientCtx,
Settings: st,
Clock: s.clock,
RPCContext: s.rpcContext,
RPCRetryOptions: &retryOpts,
}
if distSenderTestingKnobs := s.cfg.TestingKnobs.DistSender; distSenderTestingKnobs != nil {
distSenderCfg.TestingKnobs = *distSenderTestingKnobs.(*kv.DistSenderTestingKnobs)
}
s.distSender = kv.NewDistSender(distSenderCfg, s.gossip)
s.registry.AddMetricStruct(s.distSender.Metrics())
txnMetrics := kv.MakeTxnMetrics(s.cfg.HistogramWindowInterval())
s.registry.AddMetricStruct(txnMetrics)
s.txnCoordSender = kv.NewTxnCoordSender(
s.cfg.AmbientCtx,
st,
s.distSender,
s.clock,
s.cfg.Linearizable,
s.stopper,
txnMetrics,
)
s.db = client.NewDB(s.txnCoordSender, s.clock)
nlActive, nlRenewal := s.cfg.NodeLivenessDurations()
s.nodeLiveness = storage.NewNodeLiveness(
s.cfg.AmbientCtx, s.clock, s.db, s.gossip, nlActive, nlRenewal,
)
s.registry.AddMetricStruct(s.nodeLiveness.Metrics())
s.storePool = storage.NewStorePool(
s.cfg.AmbientCtx,
s.st,
s.gossip,
s.clock,
storage.MakeStorePoolNodeLivenessFunc(s.nodeLiveness),
/* deterministic */ false,
)
s.raftTransport = storage.NewRaftTransport(
s.cfg.AmbientCtx, st, storage.GossipAddressResolver(s.gossip), s.grpc, s.rpcContext,
)
s.kvDB = kv.NewDBServer(s.cfg.Config, s.txnCoordSender, s.stopper)
roachpb.RegisterExternalServer(s.grpc, s.kvDB)
// Set up internal memory metrics for use by internal SQL executors.
s.internalMemMetrics = sql.MakeMemMetrics("internal", cfg.HistogramWindowInterval())
s.registry.AddMetricStruct(s.internalMemMetrics)
// Set up Lease Manager
var lmKnobs sql.LeaseManagerTestingKnobs
if leaseManagerTestingKnobs := cfg.TestingKnobs.SQLLeaseManager; leaseManagerTestingKnobs != nil {
lmKnobs = *leaseManagerTestingKnobs.(*sql.LeaseManagerTestingKnobs)
}
s.leaseMgr = sql.NewLeaseManager(&s.nodeIDContainer, *s.db, s.clock, lmKnobs,
s.stopper, &s.internalMemMetrics)
s.leaseMgr.RefreshLeases(s.stopper, s.db, s.gossip)
// 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.MakeMonitor(
"root",
mon.MemoryResource,
nil, /* curCount */
nil, /* maxHist */
-1, /* increment: use default increment */
math.MaxInt64, /* noteworthy */
)
rootSQLMemoryMonitor.Start(context.Background(), nil, mon.MakeStandaloneBudget(s.cfg.SQLMemoryPoolSize))
// Set up the DistSQL temp engine.
tempEngine, err := engine.NewTempEngine(ctx, s.cfg.TempStoreSpec)
if err != nil {
log.Fatalf(ctx, "could not create temporary store: %v", err)
}
s.stopper.AddCloser(tempEngine)
// Set up admin memory metrics for use by admin SQL executors.
s.adminMemMetrics = sql.MakeMemMetrics("admin", cfg.HistogramWindowInterval())
s.registry.AddMetricStruct(s.adminMemMetrics)
s.tsDB = ts.NewDB(s.db)
s.tsServer = ts.MakeServer(s.cfg.AmbientCtx, s.tsDB, s.cfg.TimeSeriesServerConfig, s.stopper)
sqlExecutor := sql.InternalExecutor{LeaseManager: s.leaseMgr}
// TODO(bdarnell): make StoreConfig configurable.
storeCfg := storage.StoreConfig{
Settings: st,
AmbientCtx: s.cfg.AmbientCtx,
RaftConfig: s.cfg.RaftConfig,
Clock: s.clock,
DB: s.db,
Gossip: s.gossip,
NodeLiveness: s.nodeLiveness,
Transport: s.raftTransport,
RPCContext: s.rpcContext,
ScanInterval: s.cfg.ScanInterval,
ScanMaxIdleTime: s.cfg.ScanMaxIdleTime,
MetricsSampleInterval: s.cfg.MetricsSampleInterval,
HistogramWindowInterval: s.cfg.HistogramWindowInterval(),
StorePool: s.storePool,
SQLExecutor: sqlExecutor,
LogRangeEvents: s.cfg.EventLogEnabled,
TimeSeriesDataStore: s.tsDB,
EnableEpochRangeLeases: true,
}
if storeTestingKnobs := s.cfg.TestingKnobs.Store; storeTestingKnobs != nil {
storeCfg.TestingKnobs = *storeTestingKnobs.(*storage.StoreTestingKnobs)
}
s.recorder = status.NewMetricsRecorder(s.clock, s.nodeLiveness, s.rpcContext.RemoteClocks, s.gossip)
s.registry.AddMetricStruct(s.rpcContext.RemoteClocks.Metrics())
s.runtime = status.MakeRuntimeStatSampler(s.clock)
s.registry.AddMetricStruct(s.runtime)
s.node = NewNode(storeCfg, s.recorder, s.registry, s.stopper, txnMetrics, sql.MakeEventLogger(s.leaseMgr))
roachpb.RegisterInternalServer(s.grpc, s.node)
storage.RegisterConsistencyServer(s.grpc, s.node.storesServer)
serverpb.RegisterInitServer(s.grpc, &noopInitServer{clusterID: s.ClusterID})
s.sessionRegistry = sql.MakeSessionRegistry()
s.jobRegistry = jobs.MakeRegistry(
s.clock, s.db, sqlExecutor, s.gossip, &s.nodeIDContainer, s.ClusterID)
distSQLMetrics := distsqlrun.MakeDistSQLMetrics(cfg.HistogramWindowInterval())
s.registry.AddMetricStruct(distSQLMetrics)
// Set up the DistSQL server.
distSQLCfg := distsqlrun.ServerConfig{
AmbientContext: s.cfg.AmbientCtx,
Settings: st,
DB: s.db,
// DistSQL also uses a DB that bypasses the TxnCoordSender.
FlowDB: client.NewDB(s.distSender, s.clock),
RPCContext: s.rpcContext,
Stopper: s.stopper,
NodeID: &s.nodeIDContainer,
TempStorage: tempEngine,
TempStorageMaxSizeBytes: s.cfg.TempStoreMaxSizeBytes,
ParentMemoryMonitor: &rootSQLMemoryMonitor,
Metrics: &distSQLMetrics,
JobRegistry: s.jobRegistry,
Gossip: s.gossip,
}
if distSQLTestingKnobs := s.cfg.TestingKnobs.DistSQL; distSQLTestingKnobs != nil {
distSQLCfg.TestingKnobs = *distSQLTestingKnobs.(*distsqlrun.TestingKnobs)
}
s.distSQLServer = distsqlrun.NewServer(ctx, distSQLCfg)
distsqlrun.RegisterDistSQLServer(s.grpc, s.distSQLServer)
s.admin = newAdminServer(s)
s.status = newStatusServer(
s.cfg.AmbientCtx,
s.cfg.Config,
s.admin,
s.db,
s.gossip,
s.recorder,
s.nodeLiveness,
s.rpcContext,
s.node.stores,
s.stopper,
s.sessionRegistry,
)
s.authentication = newAuthenticationServer(s)
for _, gw := range []grpcGatewayServer{s.admin, s.status, s.authentication, &s.tsServer} {
gw.RegisterService(s.grpc)
}
nodeInfo := sql.NodeInfo{
AdminURL: cfg.AdminURL,
PGURL: cfg.PGURL,
ClusterID: s.ClusterID,
NodeID: &s.nodeIDContainer,
}
// Set up Executor
execCfg := sql.ExecutorConfig{
Settings: s.st,
NodeInfo: nodeInfo,
AmbientCtx: s.cfg.AmbientCtx,
DB: s.db,
Gossip: s.gossip,
DistSender: s.distSender,
RPCContext: s.rpcContext,
LeaseManager: s.leaseMgr,
Clock: s.clock,
DistSQLSrv: s.distSQLServer,
StatusServer: s.status,
SessionRegistry: s.sessionRegistry,
JobRegistry: s.jobRegistry,
HistogramWindowInterval: s.cfg.HistogramWindowInterval(),
RangeDescriptorCache: s.distSender.RangeDescriptorCache(),
LeaseHolderCache: s.distSender.LeaseHolderCache(),
}
if sqlExecutorTestingKnobs := s.cfg.TestingKnobs.SQLExecutor; sqlExecutorTestingKnobs != nil {
execCfg.TestingKnobs = sqlExecutorTestingKnobs.(*sql.ExecutorTestingKnobs)
} else {
execCfg.TestingKnobs = new(sql.ExecutorTestingKnobs)
}
if sqlSchemaChangerTestingKnobs := s.cfg.TestingKnobs.SQLSchemaChanger; sqlSchemaChangerTestingKnobs != nil {
execCfg.SchemaChangerTestingKnobs = sqlSchemaChangerTestingKnobs.(*sql.SchemaChangerTestingKnobs)
} else {
execCfg.SchemaChangerTestingKnobs = new(sql.SchemaChangerTestingKnobs)
}
s.sqlExecutor = sql.NewExecutor(execCfg, s.stopper)
s.registry.AddMetricStruct(s.sqlExecutor)
s.pgServer = pgwire.MakeServer(
s.cfg.AmbientCtx,
s.cfg.Config,
s.sqlExecutor,
&s.internalMemMetrics,
&rootSQLMemoryMonitor,
s.cfg.HistogramWindowInterval(),
)
s.registry.AddMetricStruct(s.pgServer.Metrics())
return s, nil
}
// ClusterSettings returns the cluster settings.
func (s *Server) ClusterSettings() *cluster.Settings {
return s.st
}
// AnnotateCtx is a convenience wrapper; see AmbientContext.
func (s *Server) AnnotateCtx(ctx context.Context) context.Context {
return s.cfg.AmbientCtx.AnnotateCtx(ctx)
}
// AnnotateCtxWithSpan is a convenience wrapper; see AmbientContext.
func (s *Server) AnnotateCtxWithSpan(
ctx context.Context, opName string,
) (context.Context, opentracing.Span) {
return s.cfg.AmbientCtx.AnnotateCtxWithSpan(ctx, opName)
}
// ClusterID returns the ID of the cluster this server is a part of.
func (s *Server) ClusterID() uuid.UUID {
return s.node.ClusterID
}
// NodeID returns the ID of this node within its cluster.
func (s *Server) NodeID() roachpb.NodeID {
return s.node.Descriptor.NodeID
}
// InitialBoot returns whether this is the first time the node has booted.
// Only intended to help print debugging info during server startup.
func (s *Server) InitialBoot() bool {
return s.node.initialBoot
}
// grpcGatewayServer represents a grpc service with HTTP endpoints through GRPC
// gateway.
type grpcGatewayServer interface {
RegisterService(g *grpc.Server)
RegisterGateway(
ctx context.Context,
mux *gwruntime.ServeMux,
conn *grpc.ClientConn,
) error
}
// ListenError is returned from Start when we fail to start listening on either
// the main Cockroach port or the HTTP port, so that the CLI can instruct the
// user on what might have gone wrong.
type ListenError struct {
error
Addr string
}
func inspectEngines(
ctx context.Context, engines []engine.Engine, minVersion, serverVersion roachpb.Version,
) (
bootstrappedEngines []engine.Engine,
emptyEngines []engine.Engine,
_ cluster.ClusterVersion,
_ error,
) {
for _, engine := range engines {
_, err := storage.ReadStoreIdent(ctx, engine)
if _, notBootstrapped := err.(*storage.NotBootstrappedError); notBootstrapped {
emptyEngines = append(emptyEngines, engine)
continue
} else if err != nil {
return nil, nil, cluster.ClusterVersion{}, err
}
bootstrappedEngines = append(bootstrappedEngines, engine)
}
cv, err := storage.SynthesizeClusterVersionFromEngines(ctx, bootstrappedEngines, minVersion, serverVersion)
if err != nil {
return nil, nil, cluster.ClusterVersion{}, err
}
return bootstrappedEngines, emptyEngines, cv, nil
}
// listenerInfo is a helper used to write files containing various listener
// information to the store directories. In contrast to the "listening url
// file", these are written once the listeners are available, before the server
// is necessarily ready to serve.
type listenerInfo struct {
listen string // the (RPC) listen address
advertise string // equals `listen` unless --advertise-host is used
http string // the HTTP endpoint
}
// Iter returns a mapping of file names to desired contents.
func (li listenerInfo) Iter() map[string]string {
return map[string]string{
"cockroach.advertise-addr": li.advertise,
"cockroach.http-addr": li.http,
"cockroach.listen-addr": li.listen,
}
}
// Start starts the server on the specified port, starts gossip and initializes
// the node using the engines from the server's context.
//
// The passed context can be used to trace the server startup. The context
// should represent the general startup operation.
func (s *Server) Start(ctx context.Context) error {
if !s.st.Initialized {
return errors.New("must pass initialized ClusterSettings")
}
ctx = s.AnnotateCtx(ctx)
startTime := timeutil.Now()
tlsConfig, err := s.cfg.GetServerTLSConfig()
if err != nil {
return err
}
httpServer := netutil.MakeServer(s.stopper, tlsConfig, s)
// The following code is a specialization of util/net.go's ListenAndServe
// which adds pgwire support. A single port is used to serve all protocols
// (pg, http, h2) via the following construction:
//
// non-TLS case:
// net.Listen -> cmux.New
// |
// - -> pgwire.Match -> pgwire.Server.ServeConn
// - -> cmux.Any -> grpc.(*Server).Serve
//
// TLS case:
// net.Listen -> cmux.New
// |
// - -> pgwire.Match -> pgwire.Server.ServeConn
// - -> cmux.Any -> grpc.(*Server).Serve
//
// Note that the difference between the TLS and non-TLS cases exists due to
// Go's lack of an h2c (HTTP2 Clear Text) implementation. See inline comments
// in util.ListenAndServe for an explanation of how h2c is implemented there
// and here.
ln, err := net.Listen("tcp", s.cfg.Addr)
if err != nil {
return ListenError{
error: err,
Addr: s.cfg.Addr,
}
}
log.Eventf(ctx, "listening on port %s", s.cfg.Addr)
unresolvedListenAddr, err := officialAddr(ctx, s.cfg.Addr, ln.Addr(), os.Hostname)
if err != nil {
return err
}
s.cfg.Addr = unresolvedListenAddr.String()
unresolvedAdvertAddr, err := officialAddr(ctx, s.cfg.AdvertiseAddr, ln.Addr(), os.Hostname)
if err != nil {
return err
}
s.cfg.AdvertiseAddr = unresolvedAdvertAddr.String()
s.rpcContext.SetLocalInternalServer(s.node)
// The cmux matches don't shut down properly unless serve is called on the
// cmux at some point. Use serveOnMux to ensure it's called during shutdown
// if we wouldn't otherwise reach the point where we start serving on it.
var serveOnMux sync.Once
m := cmux.New(ln)
// Inject an initialization listener that will intercept all
// connections while the cluster is initializing.
initLActive := int32(0)
initL := m.Match(func(_ io.Reader) bool {
return atomic.LoadInt32(&initLActive) != 0
})
pgL := m.Match(pgwire.Match)
anyL := m.Match(cmux.Any())
httpLn, err := net.Listen("tcp", s.cfg.HTTPAddr)
if err != nil {
return ListenError{
error: err,
Addr: s.cfg.HTTPAddr,
}
}
unresolvedHTTPAddr, err := officialAddr(ctx, s.cfg.HTTPAddr, httpLn.Addr(), os.Hostname)
if err != nil {
return err
}
s.cfg.HTTPAddr = unresolvedHTTPAddr.String()
workersCtx := s.AnnotateCtx(context.Background())
s.stopper.RunWorker(workersCtx, func(workersCtx context.Context) {
<-s.stopper.ShouldQuiesce()
if err := httpLn.Close(); err != nil {
log.Fatal(workersCtx, err)
}
})
if tlsConfig != nil {
httpMux := cmux.New(httpLn)
clearL := httpMux.Match(cmux.HTTP1())
tlsL := httpMux.Match(cmux.Any())
s.stopper.RunWorker(workersCtx, func(context.Context) {
netutil.FatalIfUnexpected(httpMux.Serve())
})
s.stopper.RunWorker(workersCtx, func(context.Context) {
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "https://"+r.Host+r.RequestURI, http.StatusPermanentRedirect)
})
mux.Handle("/health", s)
plainRedirectServer := netutil.MakeServer(s.stopper, tlsConfig, mux)
netutil.FatalIfUnexpected(plainRedirectServer.Serve(clearL))
})
httpLn = tls.NewListener(tlsL, tlsConfig)
}
s.stopper.RunWorker(workersCtx, func(context.Context) {
netutil.FatalIfUnexpected(httpServer.Serve(httpLn))
})
s.stopper.RunWorker(workersCtx, func(context.Context) {
<-s.stopper.ShouldQuiesce()
// TODO(bdarnell): Do we need to also close the other listeners?
netutil.FatalIfUnexpected(anyL.Close())
<-s.stopper.ShouldStop()
s.grpc.Stop()
serveOnMux.Do(func() {
// A cmux can't gracefully shut down without Serve being called on it.
netutil.FatalIfUnexpected(m.Serve())
})
})
s.stopper.RunWorker(workersCtx, func(context.Context) {
netutil.FatalIfUnexpected(s.grpc.Serve(anyL))
})
// Running the SQL migrations safely requires that we aren't serving SQL
// requests at the same time -- to ensure that, block the serving of SQL
// traffic until the migrations are done, as indicated by this channel.
serveSQL := make(chan bool)
tcpKeepAlive := envutil.EnvOrDefaultDuration("COCKROACH_SQL_TCP_KEEP_ALIVE", time.Minute)
var loggedKeepAliveStatus int32
// Attempt to set TCP keep-alive on connection. Don't fail on errors.
setTCPKeepAlive := func(ctx context.Context, conn net.Conn) {
if tcpKeepAlive == 0 {
return
}
muxConn, ok := conn.(*cmux.MuxConn)
if !ok {
return
}
tcpConn, ok := muxConn.Conn.(*net.TCPConn)
if !ok {
return
}
// Only log success/failure once.
doLog := atomic.CompareAndSwapInt32(&loggedKeepAliveStatus, 0, 1)
if err := tcpConn.SetKeepAlive(true); err != nil {
if doLog {
log.Warningf(ctx, "failed to enable TCP keep-alive for pgwire: %v", err)
}
return
}
if err := tcpConn.SetKeepAlivePeriod(tcpKeepAlive); err != nil {
if doLog {
log.Warningf(ctx, "failed to set TCP keep-alive duration for pgwire: %v", err)
}
return
}
if doLog {
log.VEventf(ctx, 2, "setting TCP keep-alive to %s for pgwire", tcpKeepAlive)
}
}
pgCtx := s.pgServer.AmbientCtx.AnnotateCtx(context.Background())
s.stopper.RunWorker(pgCtx, func(pgCtx context.Context) {
select {
case <-serveSQL:
case <-s.stopper.ShouldQuiesce():
return
}
netutil.FatalIfUnexpected(httpServer.ServeWith(pgCtx, s.stopper, pgL, func(conn net.Conn) {
connCtx := log.WithLogTagStr(pgCtx, "client", conn.RemoteAddr().String())
setTCPKeepAlive(connCtx, conn)
if err := s.pgServer.ServeConn(connCtx, conn); err != nil && !netutil.IsClosedConnection(err) {
// Report the error on this connection's context, so that we
// know which remote client caused the error when looking at
// the logs.
log.Error(connCtx, err)
}
}))
})
// Enable the debug endpoints first to provide an earlier window
// into what's going on with the node in advance of exporting node
// functionality.
// TODO(marc): when cookie-based authentication exists,
// apply it for all web endpoints.
s.mux.Handle(debugEndpoint, authorizedHandler(http.HandlerFunc(handleDebug)))
// Filter the gossip bootstrap resolvers based on the listen and
// advertise addresses.
filtered := s.cfg.FilterGossipBootstrapResolvers(ctx, unresolvedListenAddr, unresolvedAdvertAddr)
s.gossip.Start(unresolvedAdvertAddr, filtered)
log.Event(ctx, "started gossip")
s.engines, err = s.cfg.CreateEngines(ctx)
if err != nil {
return errors.Wrap(err, "failed to create engines")
}
s.stopper.AddCloser(&s.engines)
// Write listener info files early in the startup sequence. `listenerInfo` has a comment.
listenerFiles := listenerInfo{
advertise: unresolvedAdvertAddr.String(),
http: unresolvedHTTPAddr.String(),
listen: unresolvedListenAddr.String(),
}.Iter()
for _, storeSpec := range s.cfg.Stores.Specs {
if storeSpec.InMemory {
continue
}
for base, val := range listenerFiles {
file := filepath.Join(storeSpec.Path, base)
if err := ioutil.WriteFile(file, []byte(val), 0644); err != nil {
return errors.Wrapf(err, "failed to write %s", file)
}
}
}
if bootstrappedEngines, _, _, err := inspectEngines(ctx, s.engines, s.cfg.Settings.Version.MinSupportedVersion, s.cfg.Settings.Version.ServerVersion); err != nil {
return errors.Wrap(err, "inspecting engines")
} else if len(bootstrappedEngines) > 0 {
// We might have to sleep a bit to protect against this node producing non-
// monotonic timestamps. Before restarting, its clock might have been driven
// by other nodes' fast clocks, but when we restarted, we lost all this
// information. For example, a client might have written a value at a
// timestamp that's in the future of the restarted node's clock, and if we
// don't do something, the same client's read would not return the written
// value. So, we wait up to MaxOffset; we couldn't have served timestamps more
// than MaxOffset in the future (assuming that MaxOffset was not changed, see
// #9733).
//
// As an optimization for tests, we don't sleep if all the stores are brand
// new. In this case, the node will not serve anything anyway until it
// synchronizes with other nodes.
var sleepDuration time.Duration
// Don't have to sleep for monotonicity when using clockless reads
// (nor can we, for we would sleep forever).
if maxOffset := s.clock.MaxOffset(); maxOffset != timeutil.ClocklessMaxOffset {
sleepDuration = maxOffset - timeutil.Since(startTime)
}
if sleepDuration > 0 {
log.Infof(ctx, "sleeping for %s to guarantee HLC monotonicity", sleepDuration)
time.Sleep(sleepDuration)
}
} else if len(s.cfg.GossipBootstrapResolvers) == 0 {
// If the _unfiltered_ list of hosts from the --join flag is
// empty, then this node can bootstrap a new cluster. We disallow
// this if this node is being started with itself specified as a
// --join host, because that's too likely to be operator error.
bootstrapVersion := s.cfg.Settings.Version.BootstrapVersion()
if s.cfg.TestingKnobs.Store != nil {
if storeKnobs, ok := s.cfg.TestingKnobs.Store.(*storage.StoreTestingKnobs); ok && storeKnobs.BootstrapVersion != nil {
bootstrapVersion = *storeKnobs.BootstrapVersion
}
}
if err = s.node.bootstrap(ctx, s.engines, bootstrapVersion); err != nil {
return err
}
log.Infof(ctx, "**** add additional nodes by specifying --join=%s", s.cfg.AdvertiseAddr)
} else {
log.Info(ctx, "no stores bootstrapped and --join flag specified, awaiting init command.")
initServer := newInitServer(s)
initServer.serve(ctx, initL)
atomic.StoreInt32(&initLActive, 1)
s.stopper.RunWorker(workersCtx, func(context.Context) {
serveOnMux.Do(func() {
netutil.FatalIfUnexpected(m.Serve())
})
})
if err = initServer.awaitBootstrap(); err != nil {
return nil
}
atomic.StoreInt32(&initLActive, 0)
}
// We ran this before, but might've bootstrapped in the meantime. This time
// we'll get the actual list of bootstrapped and empty engines.
bootstrappedEngines, emptyEngines, cv, err := inspectEngines(ctx, s.engines, s.cfg.Settings.Version.MinSupportedVersion, s.cfg.Settings.Version.ServerVersion)
if err != nil {
return errors.Wrap(err, "inspecting engines")
}
defer time.AfterFunc(30*time.Second, func() {
serverVersion := s.cfg.Settings.Version.ServerVersion
// If the version is an unstable development version, treat it as the last
// stable version so that the docs link will exist (for example, 1.1-5
// becomes 1.1).
serverVersion.Unstable = 0
msg := `The server appears to be unable to contact the other nodes in the cluster. Please try
- starting the other nodes, if you haven't already
- double-checking that the '--join' and '--host' flags are set up correctly
- not using the '--background' flag.
If problems persist, please see https://www.cockroachlabs.com/docs/v` + serverVersion.String() + `/cluster-setup-troubleshooting.html.`
log.Shout(context.Background(), log.Severity_WARNING,
msg)
}).Stop()
// Now that we have a monotonic HLC wrt previous incarnations of the process,
// init all the replicas. At this point *some* store has been bootstrapped or
// we're joining an existing cluster for the first time.
err = s.node.start(
ctx,
unresolvedAdvertAddr,
bootstrappedEngines, emptyEngines,
s.cfg.NodeAttributes,
s.cfg.Locality,
cv,
)
if err != nil {
return err
}
log.Event(ctx, "started node")
s.refreshSettings()
raven.SetTagsContext(map[string]string{
"cluster": s.ClusterID().String(),
"node": s.NodeID().String(),
"server_id": fmt.Sprintf("%s-%s", s.ClusterID().Short(), s.NodeID()),
})
// We can now add the node registry.
s.recorder.AddNode(s.registry, s.node.Descriptor, s.node.startedAt, s.cfg.AdvertiseAddr, s.cfg.HTTPAddr)
// Begin recording runtime statistics.
s.startSampleEnvironment(s.cfg.MetricsSampleInterval)
// Begin recording time series data collected by the status monitor.
s.tsDB.PollSource(
s.cfg.AmbientCtx, s.recorder, s.cfg.MetricsSampleInterval, ts.Resolution10s, s.stopper,
)
// Begin recording status summaries.
s.node.startWriteSummaries(s.cfg.MetricsSampleInterval)
// Create and start the schema change manager only after a NodeID
// has been assigned.
var testingKnobs *sql.SchemaChangerTestingKnobs
if s.cfg.TestingKnobs.SQLSchemaChanger != nil {
testingKnobs = s.cfg.TestingKnobs.SQLSchemaChanger.(*sql.SchemaChangerTestingKnobs)
} else {
testingKnobs = new(sql.SchemaChangerTestingKnobs)
}
sql.NewSchemaChangeManager(
s.st,
testingKnobs,
*s.db,
s.node.Descriptor,
s.rpcContext,
s.distSQLServer,
s.distSender,
s.gossip,
s.leaseMgr,
s.clock,
s.jobRegistry,
).Start(s.stopper)
s.sqlExecutor.Start(ctx, &s.adminMemMetrics, s.node.Descriptor)
s.distSQLServer.Start()
log.Infof(ctx, "starting %s server at %s", s.cfg.HTTPRequestScheme(), unresolvedHTTPAddr)
log.Infof(ctx, "starting grpc/postgres server at %s", unresolvedListenAddr)
log.Infof(ctx, "advertising CockroachDB node at %s", unresolvedAdvertAddr)
s.stopper.RunWorker(workersCtx, func(context.Context) {
serveOnMux.Do(func() {
netutil.FatalIfUnexpected(m.Serve())
})
})
if len(s.cfg.SocketFile) != 0 {
log.Infof(ctx, "starting postgres server at unix:%s", s.cfg.SocketFile)
// Unix socket enabled: postgres protocol only.
unixLn, err := net.Listen("unix", s.cfg.SocketFile)
if err != nil {
return err
}
s.stopper.RunWorker(workersCtx, func(workersCtx context.Context) {
<-s.stopper.ShouldQuiesce()
if err := unixLn.Close(); err != nil {
log.Fatal(workersCtx, err)
}
})
pgCtx := s.pgServer.AmbientCtx.AnnotateCtx(context.Background())
s.stopper.RunWorker(pgCtx, func(pgCtx context.Context) {
select {
case <-serveSQL:
case <-s.stopper.ShouldQuiesce():
return
}
netutil.FatalIfUnexpected(httpServer.ServeWith(pgCtx, s.stopper, unixLn, func(conn net.Conn) {
connCtx := log.WithLogTagStr(pgCtx, "client", conn.RemoteAddr().String())
if err := s.pgServer.ServeConn(connCtx, conn); err != nil &&
!netutil.IsClosedConnection(err) {
// Report the error on this connection's context, so that we
// know which remote client caused the error when looking at
// the logs.
log.Error(connCtx, err)
}
}))
})
}
log.Event(ctx, "accepting connections")
// Begin the node liveness heartbeat. Add a callback which
// 1. records the local store "last up" timestamp for every store whenever the
// liveness record is updated.
// 2. sets Draining if Decommissioning is set in the liveness record
decommissionSem := make(chan struct{}, 1)
s.nodeLiveness.StartHeartbeat(ctx, s.stopper, func(ctx context.Context) {
now := s.clock.Now()
if err := s.node.stores.VisitStores(func(s *storage.Store) error {