forked from cockroachdb/cockroach
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setting.go
183 lines (156 loc) · 5.1 KB
/
setting.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
// Copyright 2017 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 settings
import (
"fmt"
"sync/atomic"
)
const maxSettings = 128
// Values is a container that stores values for all registered settings.
// Each setting is assigned a unique slot (up to maxSettings).
// Note that slot indices are 1-based (this is to trigger panics if an
// uninitialized slot index is used).
type Values struct {
intVals [maxSettings]int64
genericVals [maxSettings]atomic.Value
onChange [maxSettings]func()
// opaque is an arbitrary object that can be set by a higher layer to make it
// accessible from certain callbacks (like state machine transformers).
opaque interface{}
}
var (
canonicalValues atomic.Value
)
// TODO is usable at callsites that do not have *settings.Values available.
// Please don't use this.
func TODO() *Values {
if ptr := canonicalValues.Load(); ptr != nil {
return ptr.(*Values)
}
return nil
}
// SetCanonicalValuesContainer sets the Values container that will be refreshed
// at runtime -- ideally we should have no other *Values containers floating
// around, as they will be stale / lies.
func SetCanonicalValuesContainer(v *Values) {
canonicalValues.Store(v)
}
type testOpaqueType struct{}
// TestOpaque can be passed to Values.Init when we are testing the settings
// infrastructure.
var TestOpaque interface{} = testOpaqueType{}
// Init must be called before using a Values instance; it initializes all
// variables to their defaults.
//
// The opaque argument can be retrieved later via Opaque().
func (sv *Values) Init(opaque interface{}) {
sv.opaque = opaque
for _, s := range Registry {
s.setToDefault(sv)
}
}
// Opaque returns the argument passed to Init.
func (sv *Values) Opaque() interface{} {
return sv.opaque
}
func (sv *Values) settingChanged(slotIdx int) {
if fn := sv.onChange[slotIdx]; fn != nil {
fn()
}
}
func (sv *Values) getInt64(slotIdx int) int64 {
return atomic.LoadInt64(&sv.intVals[slotIdx-1])
}
func (sv *Values) setInt64(slotIdx int, newVal int64) {
if atomic.SwapInt64(&sv.intVals[slotIdx-1], newVal) != newVal {
sv.settingChanged(slotIdx)
}
}
func (sv *Values) getGeneric(slotIdx int) interface{} {
return sv.genericVals[slotIdx-1].Load()
}
func (sv *Values) setGeneric(slotIdx int, newVal interface{}) {
sv.genericVals[slotIdx-1].Store(newVal)
sv.settingChanged(slotIdx)
}
// setOnChange installs a callback to be called when a setting's value changes.
// `fn` should avoid doing long-running or blocking work as it is called on the
// goroutine which handles all settings updates.
//
// Cannot be called concurrently with setGeneric/setInt64.
func (sv *Values) setOnChange(slotIdx int, fn func()) {
if sv.onChange[slotIdx] != nil {
panic("registered multiple on-change callbacks")
}
sv.onChange[slotIdx] = fn
}
// Setting is a descriptor for each setting; once it is initialized, it is
// immutable. The values for the settings are stored separately, in
// Values. This way we can have a global set of registered settings, each
// with potentially multiple instances.
type Setting interface {
setToDefault(sv *Values)
// Typ returns the short (1 char) string denoting the type of setting.
Typ() string
String(sv *Values) string
Description() string
setDescription(desc string)
setSlotIdx(slotIdx int)
Hidden() bool
SetOnChange(sv *Values, fn func())
}
type common struct {
description string
hidden bool
// Each setting has a slotIdx which is used as a handle with Values.
slotIdx int
}
func (i *common) setSlotIdx(slotIdx int) {
if slotIdx < 1 {
panic(fmt.Sprintf("Invalid slot index %d", slotIdx))
}
if slotIdx > maxSettings {
panic(fmt.Sprintf("too many settings; increase maxSettings"))
}
i.slotIdx = slotIdx
}
func (i *common) setDescription(s string) {
i.description = s
}
func (i common) Description() string {
return i.description
}
func (i common) Hidden() bool {
return i.hidden
}
// Hide prevents a setting from showing up in SHOW ALL CLUSTER SETTINGS. It can
// still be used with SET and SHOW if the exact setting name is known. Use Hide
// for in-development features and other settings that should not be
// user-visible.
func (i *common) Hide() {
i.hidden = true
}
// SetOnChange installs a callback to be called when a setting's value changes.
// `fn` should avoid doing long-running or blocking work as it is called on the
// goroutine which handles all settings updates.
//
// Cannot be called while the setting is being changed.
func (i *common) SetOnChange(sv *Values, fn func()) {
sv.setOnChange(i.slotIdx, fn)
}
type numericSetting interface {
Setting
Validate(i int64) error
set(sv *Values, i int64) error
}