-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
start.go
1228 lines (1101 loc) · 45.3 KB
/
start.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 2015 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 cli
import (
"bytes"
"context"
"flag"
"fmt"
"io/ioutil"
"math"
"net"
"net/url"
"os"
"os/signal"
"path/filepath"
"runtime"
"strings"
"text/tabwriter"
"time"
"github.com/cockroachdb/cockroach/pkg/base"
"github.com/cockroachdb/cockroach/pkg/build"
"github.com/cockroachdb/cockroach/pkg/cli/cliflags"
"github.com/cockroachdb/cockroach/pkg/cli/exit"
"github.com/cockroachdb/cockroach/pkg/docs"
"github.com/cockroachdb/cockroach/pkg/geo/geos"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/rpc"
"github.com/cockroachdb/cockroach/pkg/security"
"github.com/cockroachdb/cockroach/pkg/server"
"github.com/cockroachdb/cockroach/pkg/server/status"
"github.com/cockroachdb/cockroach/pkg/server/telemetry"
"github.com/cockroachdb/cockroach/pkg/settings/cluster"
"github.com/cockroachdb/cockroach/pkg/storage"
"github.com/cockroachdb/cockroach/pkg/storage/enginepb"
"github.com/cockroachdb/cockroach/pkg/util/cgroups"
"github.com/cockroachdb/cockroach/pkg/util/envutil"
"github.com/cockroachdb/cockroach/pkg/util/grpcutil"
"github.com/cockroachdb/cockroach/pkg/util/hlc"
"github.com/cockroachdb/cockroach/pkg/util/humanizeutil"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/cockroach/pkg/util/log/logcrash"
"github.com/cockroachdb/cockroach/pkg/util/log/logflags"
"github.com/cockroachdb/cockroach/pkg/util/log/severity"
"github.com/cockroachdb/cockroach/pkg/util/sdnotify"
"github.com/cockroachdb/cockroach/pkg/util/stop"
"github.com/cockroachdb/cockroach/pkg/util/syncutil"
"github.com/cockroachdb/cockroach/pkg/util/sysutil"
"github.com/cockroachdb/cockroach/pkg/util/timeutil"
"github.com/cockroachdb/cockroach/pkg/util/tracing"
"github.com/cockroachdb/errors"
"github.com/cockroachdb/redact"
"github.com/spf13/cobra"
"google.golang.org/grpc"
)
// startCmd starts a node by initializing the stores and joining
// the cluster.
var startCmd = &cobra.Command{
Use: "start",
Short: "start a node in a multi-node cluster",
Long: `
Start a CockroachDB node, which will export data from one or more
storage devices, specified via --store flags.
Specify the --join flag to point to another node or nodes that are
part of the same cluster. The other nodes do not need to be started
yet, and if the address of the other nodes to be added are not yet
known it is legal for the first node to join itself.
To initialize the cluster, use 'cockroach init'.
`,
Example: ` cockroach start --insecure --store=attrs=ssd,path=/mnt/ssd1 --join=host:port,[host:port]`,
Args: cobra.NoArgs,
RunE: maybeShoutError(MaybeDecorateGRPCError(runStartJoin)),
}
// startSingleNodeCmd starts a node by initializing the stores.
var startSingleNodeCmd = &cobra.Command{
Use: "start-single-node",
Short: "start a single-node cluster",
Long: `
Start a CockroachDB node, which will export data from one or more
storage devices, specified via --store flags.
The cluster will also be automatically initialized with
replication disabled (replication factor = 1).
`,
Example: ` cockroach start-single-node --insecure --store=attrs=ssd,path=/mnt/ssd1`,
Args: cobra.NoArgs,
RunE: maybeShoutError(MaybeDecorateGRPCError(runStartSingleNode)),
}
// StartCmds exports startCmd and startSingleNodeCmds so that other
// packages can add flags to them.
var StartCmds = []*cobra.Command{startCmd, startSingleNodeCmd}
func initBlockProfile() {
// Enable the block profile for a sample of mutex and channel operations.
// Smaller values provide more accurate profiles but are more
// expensive. 0 and 1 are special: 0 disables the block profile and
// 1 captures 100% of block events. For other values, the profiler
// will sample one event per X nanoseconds spent blocking.
//
// The block profile can be viewed with `pprof http://HOST:PORT/debug/pprof/block`
//
// The utility of the block profile (aka blocking profile) has diminished
// with the advent of the mutex profile. We currently leave the block profile
// disabled by default as it has a non-zero performance impact.
d := envutil.EnvOrDefaultInt64("COCKROACH_BLOCK_PROFILE_RATE", 0)
runtime.SetBlockProfileRate(int(d))
}
func initMutexProfile() {
// Enable the mutex profile for a fraction of mutex contention events.
// Smaller values provide more accurate profiles but are more expensive. 0
// and 1 are special: 0 disables the mutex profile and 1 captures 100% of
// mutex contention events. For other values, the profiler will sample on
// average 1/X events.
//
// The mutex profile can be viewed with `pprof http://HOST:PORT/debug/pprof/mutex`
d := envutil.EnvOrDefaultInt("COCKROACH_MUTEX_PROFILE_RATE",
1000 /* 1 sample per 1000 mutex contention events */)
runtime.SetMutexProfileFraction(d)
}
var cacheSizeValue = newBytesOrPercentageValue(&serverCfg.CacheSize, memoryPercentResolver)
var sqlSizeValue = newBytesOrPercentageValue(&serverCfg.MemoryPoolSize, memoryPercentResolver)
var diskTempStorageSizeValue = newBytesOrPercentageValue(nil /* v */, nil /* percentResolver */)
func initExternalIODir(ctx context.Context, firstStore base.StoreSpec) (string, error) {
externalIODir := startCtx.externalIODir
if externalIODir == "" && !firstStore.InMemory {
externalIODir = filepath.Join(firstStore.Path, "extern")
}
if externalIODir == "" || externalIODir == "disabled" {
return "", nil
}
if !filepath.IsAbs(externalIODir) {
return "", errors.Errorf("%s path must be absolute", cliflags.ExternalIODir.Name)
}
return externalIODir, nil
}
func initTempStorageConfig(
ctx context.Context, st *cluster.Settings, stopper *stop.Stopper, useStore base.StoreSpec,
) (base.TempStorageConfig, error) {
var recordPath string
if !useStore.InMemory {
recordPath = filepath.Join(useStore.Path, server.TempDirsRecordFilename)
}
var err error
// Need to first clean up any abandoned temporary directories from
// the temporary directory record file before creating any new
// temporary directories in case the disk is completely full.
if recordPath != "" {
if err = storage.CleanupTempDirs(recordPath); err != nil {
return base.TempStorageConfig{}, errors.Wrap(err, "could not cleanup temporary directories from record file")
}
}
// The temp store size can depend on the location of the first regular store
// (if it's expressed as a percentage), so we resolve that flag here.
var tempStorePercentageResolver percentResolverFunc
if !useStore.InMemory {
dir := useStore.Path
// Create the store dir, if it doesn't exist. The dir is required to exist
// by diskPercentResolverFactory.
if err = os.MkdirAll(dir, 0755); err != nil {
return base.TempStorageConfig{}, errors.Wrapf(err, "failed to create dir for first store: %s", dir)
}
tempStorePercentageResolver, err = diskPercentResolverFactory(dir)
if err != nil {
return base.TempStorageConfig{}, errors.Wrapf(err, "failed to create resolver for: %s", dir)
}
} else {
tempStorePercentageResolver = memoryPercentResolver
}
var tempStorageMaxSizeBytes int64
if err = diskTempStorageSizeValue.Resolve(
&tempStorageMaxSizeBytes, tempStorePercentageResolver,
); err != nil {
return base.TempStorageConfig{}, err
}
if !diskTempStorageSizeValue.IsSet() {
// The default temp storage size is different when the temp
// storage is in memory (which occurs when no temp directory
// is specified and the first store is in memory).
if startCtx.tempDir == "" && useStore.InMemory {
tempStorageMaxSizeBytes = base.DefaultInMemTempStorageMaxSizeBytes
} else {
tempStorageMaxSizeBytes = base.DefaultTempStorageMaxSizeBytes
}
}
// Initialize a base.TempStorageConfig based on first store's spec and
// cli flags.
tempStorageConfig := base.TempStorageConfigFromEnv(
ctx,
st,
useStore,
startCtx.tempDir,
tempStorageMaxSizeBytes,
)
// Set temp directory to first store's path if the temp storage is not
// in memory.
tempDir := startCtx.tempDir
if tempDir == "" && !tempStorageConfig.InMemory {
tempDir = useStore.Path
}
// Create the temporary subdirectory for the temp engine.
if tempStorageConfig.Path, err = storage.CreateTempDir(tempDir, server.TempDirPrefix, stopper); err != nil {
return base.TempStorageConfig{}, errors.Wrap(err, "could not create temporary directory for temp storage")
}
// We record the new temporary directory in the record file (if it
// exists) for cleanup in case the node crashes.
if recordPath != "" {
if err = storage.RecordTempDir(recordPath, tempStorageConfig.Path); err != nil {
return base.TempStorageConfig{}, errors.Wrapf(
err,
"could not record temporary directory path to record file: %s",
recordPath,
)
}
}
return tempStorageConfig, nil
}
var errCannotUseJoin = errors.New("cannot use --join with 'cockroach start-single-node' -- use 'cockroach start' instead")
func runStartSingleNode(cmd *cobra.Command, args []string) error {
joinFlag := flagSetForCmd(cmd).Lookup(cliflags.Join.Name)
if joinFlag.Changed {
return errCannotUseJoin
}
// Now actually set the flag as changed so that the start code
// doesn't warn that it was not set. This is all to let `start-single-node`
// get by without the use of --join flags.
joinFlag.Changed = true
// Make the node auto-init the cluster if not done already.
serverCfg.AutoInitializeCluster = true
return runStart(cmd, args, true /*startSingleNode*/)
}
func runStartJoin(cmd *cobra.Command, args []string) error {
return runStart(cmd, args, false /*startSingleNode*/)
}
// runStart starts the cockroach node using --store as the list of
// storage devices ("stores") on this machine and --join as the list
// of other active nodes used to join this node to the cockroach
// cluster, if this is its first time connecting.
//
// If the argument startSingleNode is set the replication factor
// will be set to 1 all zone configs (see initial_sql.go).
func runStart(cmd *cobra.Command, args []string, startSingleNode bool) (returnErr error) {
tBegin := timeutil.Now()
// First things first: if the user wants background processing,
// relinquish the terminal ASAP by forking and exiting.
//
// If executing in the background, the function returns ok == true in
// the parent process (regardless of err) and the parent exits at
// this point.
if ok, err := maybeRerunBackground(); ok {
return err
}
// Change the permission mask for all created files.
//
// We're considering everything produced by a cockroach node
// to potentially contain sensitive information, so it should
// not be world-readable.
disableOtherPermissionBits()
// Set up the signal handlers. This also ensures that any of these
// signals received beyond this point do not interrupt the startup
// sequence until the point signals are checked below.
// We want to set up signal handling before starting logging, because
// logging uses buffering, and we want to be able to sync
// the buffers in the signal handler below. If we started capturing
// signals later, some startup logging might be lost.
signalCh := make(chan os.Signal, 1)
signal.Notify(signalCh, drainSignals...)
// SIGQUIT is handled differently: for SIGQUIT we spawn a goroutine
// and we always handle it, no matter at which point during
// execution we are. This makes it possible to use SIGQUIT to
// inspect a running process and determine what it is currently
// doing, even if it gets stuck somewhere.
if quitSignal != nil {
quitSignalCh := make(chan os.Signal, 1)
signal.Notify(quitSignalCh, quitSignal)
go func() {
for {
<-quitSignalCh
log.DumpStacks(context.Background())
}
}()
}
// Set up a cancellable context for the entire start command.
// The context will be canceled at the end.
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// Set up a tracing span for the start process. We want any logging
// happening beyond this point to be accounted to this start
// context, including logging related to the initialization of
// the logging infrastructure below.
// This span concludes when the startup goroutine started below
// has completed.
// TODO(andrei): we don't close the span on the early returns below.
tracer := serverCfg.Settings.Tracer
sp := tracer.StartSpan("server start")
ctx = tracing.ContextWithSpan(ctx, sp)
// Set up the logging and profiling output.
//
// We want to do this as early as possible, because most of the code
// in CockroachDB may use logging, and until logging has been
// initialized log files will be created in $TMPDIR instead of their
// expected location.
//
// This initialization uses the various configuration parameters
// initialized by flag handling (before runStart was called). Any
// additional server configuration tweaks for the startup process
// must be necessarily non-logging-related, as logging parameters
// cannot be picked up beyond this point.
stopper, err := setupAndInitializeLoggingAndProfiling(ctx, cmd)
if err != nil {
return err
}
// If any store has something to say against a server start-up
// (e.g. previously detected corruption), listen to them now.
if err := serverCfg.Stores.PriorCriticalAlertError(); err != nil {
return &cliError{exitCode: exit.FatalError(), cause: err}
}
// We don't care about GRPCs fairly verbose logs in most client commands,
// but when actually starting a server, we enable them.
grpcutil.LowerSeverity(severity.WARNING)
// Tweak GOMAXPROCS if we're in a cgroup / container that has cpu limits set.
// The GO default for GOMAXPROCS is runtime.NumCPU(), however this is less
// than ideal if the cgruop is limited to a number lower than that.
if _, set := os.LookupEnv("GOMAXPROCS"); !set {
if cpuInfo, err := cgroups.GetCgroupCPU(); err == nil {
numCPUToUse := int(math.Ceil(cpuInfo.CPUShares()))
if numCPUToUse > runtime.NumCPU() || numCPUToUse <= 0 {
numCPUToUse = runtime.NumCPU()
}
log.Infof(ctx, "running in a container; setting GOMAXPROCS to %d", numCPUToUse)
runtime.GOMAXPROCS(numCPUToUse)
}
}
// Check the --join flag.
if !flagSetForCmd(cmd).Lookup(cliflags.Join.Name).Changed {
err := errors.WithHint(
errors.New("no --join flags provided to 'cockroach start'"),
"Consider using 'cockroach init' or 'cockroach start-single-node' instead")
return err
}
// Now perform additional configuration tweaks specific to the start
// command.
// Derive temporary/auxiliary directory specifications.
if serverCfg.Settings.ExternalIODir, err = initExternalIODir(ctx, serverCfg.Stores.Specs[0]); err != nil {
return err
}
// Next we initialize the target directory for temporary storage.
// If encryption at rest is enabled in any fashion, we'll want temp
// storage to be encrypted too. To achieve this, we use
// the first encrypted store as temp dir target, if any.
// If we can't find one, we use the first StoreSpec in the list.
var specIdx = 0
for i := range serverCfg.Stores.Specs {
if serverCfg.Stores.Specs[i].ExtraOptions != nil {
specIdx = i
}
}
if serverCfg.TempStorageConfig, err = initTempStorageConfig(
ctx, serverCfg.Settings, stopper, serverCfg.Stores.Specs[specIdx],
); err != nil {
return err
}
if serverCfg.StorageEngine == enginepb.EngineTypeDefault {
serverCfg.StorageEngine = enginepb.EngineTypePebble
}
// Initialize the node's configuration from startup parameters.
// This also reads the part of the configuration that comes from
// environment variables.
if err := serverCfg.InitNode(ctx); err != nil {
return errors.Wrap(err, "failed to initialize node")
}
// The configuration is now ready to report to the user and the log
// file. We had to wait after InitNode() so that all configuration
// environment variables, which are reported too, have been read and
// registered.
reportConfiguration(ctx)
// ReadyFn will be called when the server has started listening on
// its network sockets, but perhaps before it has done bootstrapping
// and thus before Start() completes.
serverCfg.ReadyFn = func(waitForInit bool) {
// Inform the user if the network settings are suspicious. We need
// to do that after starting to listen because we need to know
// which advertise address NewServer() has decided.
hintServerCmdFlags(ctx, cmd)
// If another process was waiting on the PID (e.g. using a FIFO),
// this is when we can tell them the node has started listening.
if startCtx.pidFile != "" {
log.Infof(ctx, "PID file: %s", startCtx.pidFile)
if err := ioutil.WriteFile(startCtx.pidFile, []byte(fmt.Sprintf("%d\n", os.Getpid())), 0644); err != nil {
log.Errorf(ctx, "failed writing the PID: %v", err)
}
}
// If the invoker has requested an URL update, do it now that
// the server is ready to accept SQL connections.
// (Note: as stated above, ReadyFn is called after the server
// has started listening on its socket, but possibly before
// the cluster has been initialized and can start processing requests.
// This is OK for SQL clients, as the connection will be accepted
// by the network listener and will just wait/suspend until
// the cluster initializes, at which point it will be picked up
// and let the client go through, transparently.)
if startCtx.listeningURLFile != "" {
log.Infof(ctx, "listening URL file: %s", startCtx.listeningURLFile)
// (Re-)compute the client connection URL. We cannot do this
// earlier (e.g. above, in the runStart function) because
// at this time the address and port have not been resolved yet.
sCtx := rpc.MakeSecurityContext(serverCfg.Config, security.ClusterTLSSettings(serverCfg.Settings), roachpb.SystemTenantID)
pgURL, err := sCtx.PGURL(url.User(security.RootUser))
if err != nil {
log.Errorf(ctx, "failed computing the URL: %v", err)
return
}
if err = ioutil.WriteFile(startCtx.listeningURLFile, []byte(fmt.Sprintf("%s\n", pgURL)), 0644); err != nil {
log.Errorf(ctx, "failed writing the URL: %v", err)
}
}
if waitForInit {
log.Shout(ctx, severity.INFO,
"initial startup completed\n"+
"Node will now attempt to join a running cluster, or wait for `cockroach init`.\n"+
"Client connections will be accepted after this completes successfully.\n"+
"Check the log file(s) for progress. ")
}
// Ensure the configuration logging is written to disk in case a
// process is waiting for the sdnotify readiness to read important
// information from there.
log.Flush()
// Signal readiness. This unblocks the process when running with
// --background or under systemd.
if err := sdnotify.Ready(); err != nil {
log.Errorf(ctx, "failed to signal readiness using systemd protocol: %s", err)
}
}
// DelayedBoostrapFn will be called if the boostrap process is
// taking a bit long.
serverCfg.DelayedBootstrapFn = func() {
const 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 '--listen'/'--advertise' flags are set up correctly;
- running the 'cockroach init' command if you are trying to initialize a new cluster.
If problems persist, please see %s.`
docLink := docs.URL("cluster-setup-troubleshooting.html")
if !startCtx.inBackground {
log.Shoutf(context.Background(), severity.WARNING, msg, docLink)
} else {
// Don't shout to stderr since the server will have detached by
// the time this function gets called.
log.Warningf(ctx, msg, docLink)
}
}
// Set up the Geospatial library.
// We need to make sure this happens before any queries involving geospatial data is executed.
loc, err := geos.EnsureInit(geos.EnsureInitErrorDisplayPrivate, startCtx.geoLibsDir)
if err != nil {
log.Infof(ctx, "could not initialize GEOS - spatial functions may not be available: %v", err)
} else {
log.Infof(ctx, "GEOS loaded from directory %s", loc)
}
// Beyond this point, the configuration is set and the server is
// ready to start.
log.Info(ctx, "starting cockroach node")
// Run the rest of the startup process in a goroutine separate from
// the main goroutine to avoid preventing proper handling of signals
// if we get stuck on something during initialization (#10138).
var serverStatusMu struct {
syncutil.Mutex
// Used to synchronize server startup with server shutdown if something
// interrupts the process during initialization (it isn't safe to try to
// drain a server that doesn't exist or is in the middle of starting up,
// or to start a server after draining has begun).
started, draining bool
}
var s *server.Server
errChan := make(chan error, 1)
go func() {
// Ensure that the log files see the startup messages immediately.
defer log.Flush()
// If anything goes dramatically wrong, use Go's panic/recover
// mechanism to intercept the panic and log the panic details to
// the error reporting server.
defer func() {
if s != nil {
// We only attempt to log the panic details if the server has
// actually been started successfully. If there's no server,
// we won't know enough to decide whether reporting is
// permitted.
logcrash.RecoverAndReportPanic(ctx, &s.ClusterSettings().SV)
}
}()
// When the start up goroutine completes, so can the start up span
// defined above.
defer sp.Finish()
// Any error beyond this point should be reported through the
// errChan defined above. However, in Go the code pattern "if err
// != nil { return err }" is more common. Expecting contributors
// to remember to write "if err != nil { errChan <- err }" beyond
// this point is optimistic. To avoid any error, we capture all
// the error returns in a closure, and do the errChan reporting,
// if needed, when that function returns.
if err := func() error {
// Instantiate the server.
var err error
s, err = server.NewServer(serverCfg, stopper)
if err != nil {
return errors.Wrap(err, "failed to start server")
}
// Have we already received a signal to terminate? If so, just
// stop here.
serverStatusMu.Lock()
draining := serverStatusMu.draining
serverStatusMu.Unlock()
if draining {
return nil
}
// Attempt to start the server.
if err := s.PreStart(ctx); err != nil {
if le := (*server.ListenError)(nil); errors.As(err, &le) {
const errorPrefix = "consider changing the port via --%s"
if le.Addr == serverCfg.Addr {
err = errors.Wrapf(err, errorPrefix, cliflags.ListenAddr.Name)
} else if le.Addr == serverCfg.HTTPAddr {
err = errors.Wrapf(err, errorPrefix, cliflags.ListenHTTPAddr.Name)
}
}
return errors.Wrap(err, "cockroach server exited with error")
}
// Server started, notify the shutdown monitor running concurrently.
serverStatusMu.Lock()
serverStatusMu.started = true
serverStatusMu.Unlock()
// Start up the update check loop.
// We don't do this in (*server.Server).Start() because we don't want it
// in tests.
if !cluster.TelemetryOptOut() {
s.PeriodicallyCheckForUpdates(ctx)
}
initialStart := s.InitialStart()
// Run SQL for new clusters.
// TODO(knz): If/when we want auto-creation of an initial admin user,
// this can be achieved here.
if _, err := runInitialSQL(ctx, s, startSingleNode, "" /* adminUser */); err != nil {
return err
}
// Now let SQL clients in.
if err := s.AcceptClients(ctx); err != nil {
return err
}
// Now inform the user that the server is running and tell the
// user about its run-time derived parameters.
var buf redact.StringBuilder
info := build.GetInfo()
buf.Printf("CockroachDB node starting at %s (took %0.1fs)\n", timeutil.Now(), timeutil.Since(tBegin).Seconds())
buf.Printf("build:\t%s %s @ %s (%s)\n",
redact.Safe(info.Distribution), redact.Safe(info.Tag), redact.Safe(info.Time), redact.Safe(info.GoVersion))
buf.Printf("webui:\t%s\n", serverCfg.AdminURL())
// (Re-)compute the client connection URL. We cannot do this
// earlier (e.g. above, in the runStart function) because
// at this time the address and port have not been resolved yet.
sCtx := rpc.MakeSecurityContext(serverCfg.Config, security.ClusterTLSSettings(serverCfg.Settings), roachpb.SystemTenantID)
pgURL, err := sCtx.PGURL(url.User(security.RootUser))
if err != nil {
log.Errorf(ctx, "failed computing the URL: %v", err)
return err
}
buf.Printf("sql:\t%s\n", pgURL)
buf.Printf("RPC client flags:\t%s\n", clientFlagsRPC())
if len(serverCfg.SocketFile) != 0 {
buf.Printf("socket:\t%s\n", serverCfg.SocketFile)
}
buf.Printf("logs:\t%s\n", flag.Lookup("log-dir").Value)
if serverCfg.AuditLogDirName.IsSet() {
buf.Printf("SQL audit logs:\t%s\n", serverCfg.AuditLogDirName)
}
if serverCfg.Attrs != "" {
buf.Printf("attrs:\t%s\n", serverCfg.Attrs)
}
if len(serverCfg.Locality.Tiers) > 0 {
buf.Printf("locality:\t%s\n", serverCfg.Locality)
}
if s.TempDir() != "" {
buf.Printf("temp dir:\t%s\n", s.TempDir())
}
if ext := s.ClusterSettings().ExternalIODir; ext != "" {
buf.Printf("external I/O path: \t%s\n", ext)
} else {
buf.Printf("external I/O path: \t<disabled>\n")
}
for i, spec := range serverCfg.Stores.Specs {
buf.Printf("store[%d]:\t%s\n", i, spec)
}
buf.Printf("storage engine: \t%s\n", &serverCfg.StorageEngine)
nodeID := s.NodeID()
if initialStart {
if nodeID == server.FirstNodeID {
buf.Printf("status:\tinitialized new cluster\n")
} else {
buf.Printf("status:\tinitialized new node, joined pre-existing cluster\n")
}
} else {
buf.Printf("status:\trestarted pre-existing node\n")
}
if baseCfg.ClusterName != "" {
buf.Printf("cluster name:\t%s\n", baseCfg.ClusterName)
}
// Remember the cluster ID for log file rotation.
clusterID := s.ClusterID().String()
log.SetClusterID(clusterID)
buf.Printf("clusterID:\t%s\n", clusterID)
buf.Printf("nodeID:\t%d\n", nodeID)
// Collect the formatted string and show it to the user.
msg, err := expandTabsInRedactableBytes(buf.RedactableBytes())
if err != nil {
return err
}
msgS := msg.ToString()
log.Infof(ctx, "node startup completed:\n%s", msgS)
if !startCtx.inBackground && !log.LoggingToStderr(severity.INFO) {
fmt.Print(msgS.StripMarkers())
}
return nil
}(); err != nil {
errChan <- err
}
}()
// The remainder of the main function executes concurrently with the
// start up goroutine started above.
//
// It is concerned with determining when the server should stop
// because the main process is being shut down -- either via a stop
// message received from `cockroach quit` / `cockroach
// decommission`, or a signal.
// We'll want to log any shutdown activity against a separate span.
shutdownSpan := tracer.StartSpan("server shutdown")
defer shutdownSpan.Finish()
shutdownCtx := tracing.ContextWithSpan(context.Background(), shutdownSpan)
stopWithoutDrain := make(chan struct{}) // closed if interrupted very early
// Block until one of the signals above is received or the stopper
// is stopped externally (for example, via the quit endpoint).
select {
case err := <-errChan:
// SetSync both flushes and ensures that subsequent log writes are flushed too.
log.StartSync()
return err
case <-stopper.ShouldStop():
// Server is being stopped externally and our job is finished
// here since we don't know if it's a graceful shutdown or not.
<-stopper.IsStopped()
// StartSync both flushes and ensures that subsequent log writes are flushed too.
log.StartSync()
return nil
case sig := <-signalCh:
// We start synchronizing log writes from here, because if a
// signal was received there is a non-zero chance the sender of
// this signal will follow up with SIGKILL if the shutdown is not
// timely, and we don't want logs to be lost.
log.StartSync()
log.Infof(shutdownCtx, "received signal '%s'", sig)
switch sig {
case os.Interrupt:
// Graceful shutdown after an interrupt should cause the process
// to terminate with a non-zero exit code; however SIGTERM is
// "legitimate" and should be acknowledged with a success exit
// code. So we keep the error state here for later.
returnErr = &cliError{
exitCode: exit.Interrupted(),
// INFO because a single interrupt is rather innocuous.
severity: severity.INFO,
cause: errors.New("interrupted"),
}
msgDouble := "Note: a second interrupt will skip graceful shutdown and terminate forcefully"
fmt.Fprintln(os.Stdout, msgDouble)
}
// Start the draining process in a separate goroutine so that it
// runs concurrently with the timeout check below.
go func() {
serverStatusMu.Lock()
serverStatusMu.draining = true
drainingIsSafe := serverStatusMu.started
serverStatusMu.Unlock()
// drainingIsSafe may have been set in the meantime, but that's ok.
// In the worst case, we're not draining a Server that has *just*
// started. Not desirable, but not terrible either.
if !drainingIsSafe {
close(stopWithoutDrain)
return
}
// Don't use shutdownCtx because this is in a goroutine that may
// still be running after shutdownCtx's span has been finished.
ac := log.AmbientContext{}
ac.AddLogTag("server drain process", nil)
drainCtx := ac.AnnotateCtx(context.Background())
// Perform a graceful drain. We keep retrying forever, in
// case there are many range leases or some unavailability
// preventing progress. If the operator wants to expedite
// the shutdown, they will need to make it ungraceful
// via a 2nd signal.
for {
remaining, _, err := s.Drain(drainCtx)
if err != nil {
log.Errorf(drainCtx, "graceful drain failed: %v", err)
break
}
if remaining == 0 {
// No more work to do.
break
}
// Avoid a busy wait with high CPU usage if the server replies
// with an incomplete drain too quickly.
time.Sleep(200 * time.Millisecond)
}
stopper.Stop(drainCtx)
}()
// Don't return: we're shutting down gracefully.
case <-log.FatalChan():
// A fatal error has occurred. Stop everything (gracelessly) to
// avoid serving incorrect data while the final log messages are
// being written.
// https://github.com/cockroachdb/cockroach/issues/23414
// TODO(bdarnell): This could be more graceless, for example by
// reaching into the server objects and closing all the
// connections while they're in use. That would be more in line
// with the expected effect of a log.Fatal.
stopper.Stop(shutdownCtx)
// The logging goroutine is now responsible for killing this
// process, so just block this goroutine.
select {}
}
// At this point, a signal has been received to shut down the
// process, and a goroutine is busy telling the server to drain and
// stop. From this point on, we just have to wait until the server
// indicates it has stopped.
const msgDrain = "initiating graceful shutdown of server"
log.Info(shutdownCtx, msgDrain)
fmt.Fprintln(os.Stdout, msgDrain)
// Notify the user every 5 second of the shutdown progress.
go func() {
ticker := time.NewTicker(5 * time.Second)
defer ticker.Stop()
for {
select {
case <-ticker.C:
log.Infof(context.Background(), "%d running tasks", stopper.NumTasks())
case <-stopper.ShouldStop():
return
case <-stopWithoutDrain:
return
}
}
}()
// Meanwhile, we don't want to wait too long either, in case the
// server is getting stuck and doesn't shut down in a timely manner.
//
// So we also pay attention to any additional signal received beyond
// this point (maybe some service monitor was impatient and sends
// another signal to hasten the shutdown process).
//
// If any such trigger to hasten occurs, we simply return, which
// will cause the process to exit and the server goroutines to be
// forcefully terminated.
const hardShutdownHint = " - node may take longer to restart & clients may need to wait for leases to expire"
for {
select {
case sig := <-signalCh:
switch sig {
case termSignal:
// Double SIGTERM, or SIGTERM after another signal: continue
// the graceful shutdown.
log.Infof(shutdownCtx, "received additional signal '%s'; continuing graceful shutdown", sig)
continue
}
// This new signal is not welcome, as it interferes with the graceful
// shutdown process.
log.Shoutf(shutdownCtx, severity.ERROR,
"received signal '%s' during shutdown, initiating hard shutdown%s",
log.Safe(sig), log.Safe(hardShutdownHint))
handleSignalDuringShutdown(sig)
panic("unreachable")
case <-stopper.IsStopped():
const msgDone = "server drained and shutdown completed"
log.Infof(shutdownCtx, msgDone)
fmt.Fprintln(os.Stdout, msgDone)
case <-stopWithoutDrain:
const msgDone = "too early to drain; used hard shutdown instead"
log.Infof(shutdownCtx, msgDone)
fmt.Fprintln(os.Stdout, msgDone)
}
break
}
return returnErr
}
// expandTabsInRedactableBytes expands tabs in the redactable byte
// slice, so that columns are aligned. The correctness of this
// function depends on the assumption that the `tabwriter` does not
// replace characters.
func expandTabsInRedactableBytes(s redact.RedactableBytes) (redact.RedactableBytes, error) {
var buf bytes.Buffer
tw := tabwriter.NewWriter(&buf, 2, 1, 2, ' ', 0)
if _, err := tw.Write([]byte(s)); err != nil {
return nil, err
}
if err := tw.Flush(); err != nil {
return nil, err
}
return redact.RedactableBytes(buf.Bytes()), nil
}
func hintServerCmdFlags(ctx context.Context, cmd *cobra.Command) {
pf := flagSetForCmd(cmd)
listenAddrSpecified := pf.Lookup(cliflags.ListenAddr.Name).Changed || pf.Lookup(cliflags.ServerHost.Name).Changed
advAddrSpecified := pf.Lookup(cliflags.AdvertiseAddr.Name).Changed || pf.Lookup(cliflags.AdvertiseHost.Name).Changed
if !listenAddrSpecified && !advAddrSpecified {
host, _, _ := net.SplitHostPort(serverCfg.AdvertiseAddr)
log.Shoutf(ctx, severity.WARNING,
"neither --listen-addr nor --advertise-addr was specified.\n"+
"The server will advertise %q to other nodes, is this routable?\n\n"+
"Consider using:\n"+
"- for local-only servers: --listen-addr=localhost\n"+
"- for multi-node clusters: --advertise-addr=<host/IP addr>\n", host)
}
}
func clientFlagsRPC() string {
flags := []string{os.Args[0], "<client cmd>"}
if serverCfg.AdvertiseAddr != "" {
flags = append(flags, "--host="+serverCfg.AdvertiseAddr)
}
if startCtx.serverInsecure {
flags = append(flags, "--insecure")
} else {
flags = append(flags, "--certs-dir="+startCtx.serverSSLCertsDir)
}
return strings.Join(flags, " ")
}
func reportConfiguration(ctx context.Context) {
serverCfg.Report(ctx)
if envVarsUsed := envutil.GetEnvVarsUsed(); len(envVarsUsed) > 0 {
log.Infof(ctx, "using local environment variables: %s", strings.Join(envVarsUsed, ", "))
}
// If a user ever reports "bad things have happened", any
// troubleshooting steps will want to rule out that the user was
// running as root in a multi-user environment, or using different
// uid/gid across runs in the same data directory. To determine
// this, it's easier if the information appears in the log file.
log.Infof(ctx, "process identity: %s", sysutil.ProcessIdentity())
}
func maybeWarnMemorySizes(ctx context.Context) {
// Is the cache configuration OK?
if !cacheSizeValue.IsSet() {
var buf bytes.Buffer
fmt.Fprintf(&buf, "Using the default setting for --cache (%s).\n", cacheSizeValue)
fmt.Fprintf(&buf, " A significantly larger value is usually needed for good performance.\n")
if size, err := status.GetTotalMemory(context.Background()); err == nil {
fmt.Fprintf(&buf, " If you have a dedicated server a reasonable setting is --cache=.25 (%s).",
humanizeutil.IBytes(size/4))
} else {
fmt.Fprintf(&buf, " If you have a dedicated server a reasonable setting is 25%% of physical memory.")
}
log.Warningf(ctx, "%s", buf.String())
}
// Check that the total suggested "max" memory is well below the available memory.
if maxMemory, err := status.GetTotalMemory(ctx); err == nil {
requestedMem := serverCfg.CacheSize + serverCfg.MemoryPoolSize
maxRecommendedMem := int64(.75 * float64(maxMemory))
if requestedMem > maxRecommendedMem {
log.Shoutf(ctx, severity.WARNING,
"the sum of --max-sql-memory (%s) and --cache (%s) is larger than 75%% of total RAM (%s).\nThis server is running at increased risk of memory-related failures.",
sqlSizeValue, cacheSizeValue, humanizeutil.IBytes(maxRecommendedMem))
}
}
}
func logOutputDirectory() string {
return startCtx.logDir.String()
}
// setupAndInitializeLoggingAndProfiling does what it says on the label.
// Prior to this however it determines suitable defaults for the
// logging output directory and the verbosity level of stderr logging.
// We only do this for the "start" and "start-sql" commands which is why this work
// occurs here and not in an OnInitialize function.
func setupAndInitializeLoggingAndProfiling(
ctx context.Context, cmd *cobra.Command,
) (stopper *stop.Stopper, err error) {
if active, firstUse := log.IsActive(); active {
panic(errors.Newf("logging already active; first used at:\n%s", firstUse))
}
fl := flagSetForCmd(cmd)
// Default the log directory to the "logs" subdirectory of the first
// non-memory store. If more than one non-memory stores is detected,
// print a warning.
ambiguousLogDirs := false
lf := fl.Lookup(cliflags.LogDir.Name)
if !startCtx.logDir.IsSet() && !lf.Changed {
// We only override the log directory if the user has not explicitly
// disabled file logging using --log-dir="".
newDir := ""