github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/settings/cluster/cluster_settings.go (about) 1 // Copyright 2017 The Cockroach Authors. 2 // 3 // Use of this software is governed by the Business Source License 4 // included in the file licenses/BSL.txt. 5 // 6 // As of the Change Date specified in that file, in accordance with 7 // the Business Source License, use of this software will be governed 8 // by the Apache License, Version 2.0, included in the file 9 // licenses/APL.txt. 10 11 package cluster 12 13 import ( 14 "context" 15 "sync/atomic" 16 17 "github.com/cockroachdb/cockroach/pkg/clusterversion" 18 "github.com/cockroachdb/cockroach/pkg/roachpb" 19 "github.com/cockroachdb/cockroach/pkg/settings" 20 "github.com/cockroachdb/cockroach/pkg/util/envutil" 21 "github.com/cockroachdb/cockroach/pkg/util/log" 22 "github.com/cockroachdb/cockroach/pkg/util/tracing" 23 ) 24 25 // Settings is the collection of cluster settings. For a running CockroachDB 26 // node, there is a single instance of Settings which is shared across various 27 // components. 28 type Settings struct { 29 SV settings.Values 30 31 // Manual defaults to false. If set, lets this ClusterSetting's MakeUpdater 32 // method return a dummy updater that simply throws away all values. This is 33 // for use in tests for which manual control is desired. 34 // 35 // Also see the Override() method that different types of settings provide for 36 // overwriting the default of a single setting. 37 Manual atomic.Value // bool 38 39 Tracer *tracing.Tracer 40 ExternalIODir string 41 42 // Set to 1 if a profile is active (if the profile is being grabbed through 43 // the `pprofui` server as opposed to the raw endpoint). 44 cpuProfiling int32 // atomic 45 46 // Version provides a read-only view to the active cluster version and this 47 // binary's version details. 48 Version clusterversion.Handle 49 } 50 51 // TelemetryOptOut is a place for controlling whether to opt out of telemetry or not. 52 func TelemetryOptOut() bool { 53 return envutil.EnvOrDefaultBool("COCKROACH_SKIP_ENABLING_DIAGNOSTIC_REPORTING", false) 54 } 55 56 // NoSettings is used when a func requires a Settings but none is available 57 // (for example, a CLI subcommand that does not connect to a cluster). 58 var NoSettings *Settings // = nil 59 60 // IsCPUProfiling returns true if a pprofui CPU profile is being recorded. This can 61 // be used by moving parts across the system to add profiler labels which are 62 // too expensive to be enabled at all times. 63 func (s *Settings) IsCPUProfiling() bool { 64 return atomic.LoadInt32(&s.cpuProfiling) == 1 65 } 66 67 // SetCPUProfiling is called from the pprofui to inform the system that a CPU 68 // profile is being recorded. 69 func (s *Settings) SetCPUProfiling(to bool) { 70 i := int32(0) 71 if to { 72 i = 1 73 } 74 atomic.StoreInt32(&s.cpuProfiling, i) 75 } 76 77 // MakeUpdater returns a new Updater, pre-alloced to the registry size. Note 78 // that if the Setting has the Manual flag set, this Updater simply ignores all 79 // updates. 80 func (s *Settings) MakeUpdater() settings.Updater { 81 if isManual, ok := s.Manual.Load().(bool); ok && isManual { 82 return &settings.NoopUpdater{} 83 } 84 return settings.NewUpdater(&s.SV) 85 } 86 87 // MakeClusterSettings returns a Settings object that has its binary and 88 // minimum supported versions set to this binary's build and it's minimum 89 // supported versions respectively. The cluster version setting is not 90 // initialized. 91 func MakeClusterSettings() *Settings { 92 s := &Settings{} 93 94 sv := &s.SV 95 s.Version = clusterversion.MakeVersionHandle(&s.SV) 96 sv.Init(s.Version) 97 98 s.Tracer = tracing.NewTracer() 99 s.Tracer.Configure(sv) 100 101 return s 102 } 103 104 // MakeTestingClusterSettings returns a Settings object that has its binary and 105 // minimum supported versions set to the baked in binary version. It also 106 // initializes the cluster version setting to the binary version. 107 // 108 // It is typically used for testing or one-off situations in which a Settings 109 // object is needed, but cluster settings don't play a crucial role. 110 func MakeTestingClusterSettings() *Settings { 111 return MakeTestingClusterSettingsWithVersions( 112 clusterversion.TestingBinaryVersion, clusterversion.TestingBinaryVersion, true /* initializeVersion */) 113 } 114 115 // MakeTestingClusterSettingsWithVersions returns a Settings object that has its 116 // binary and minimum supported versions set to the provided versions. 117 // It also can also initialize the cluster version setting to the specified 118 // binaryVersion. 119 // 120 // It is typically used in tests that want to override the default binary and 121 // minimum supported versions. 122 func MakeTestingClusterSettingsWithVersions( 123 binaryVersion, binaryMinSupportedVersion roachpb.Version, initializeVersion bool, 124 ) *Settings { 125 s := &Settings{} 126 127 sv := &s.SV 128 s.Version = clusterversion.MakeVersionHandleWithOverride( 129 &s.SV, binaryVersion, binaryMinSupportedVersion) 130 sv.Init(s.Version) 131 132 s.Tracer = tracing.NewTracer() 133 s.Tracer.Configure(sv) 134 135 if initializeVersion { 136 // Initialize cluster version to specified binaryVersion. 137 if err := clusterversion.Initialize(context.TODO(), binaryVersion, &s.SV); err != nil { 138 log.Fatalf(context.TODO(), "unable to initialize version: %s", err) 139 } 140 } 141 return s 142 }