Documentation ¶
Overview ¶
Package settings provides a central registry of runtime editable settings and accompanying helper functions for retrieving their current values.
Settings values are stored in the system.settings table (which is gossiped). A gossip-driven worker updates this package's cached value when the table changes (see the `RefreshSettings` worker in the `sql` package).
The package's cache is global -- while all the usual drawbacks of mutable global state obviously apply, it is needed to make the package's functionality available to a wide variety of callsites, that may or may not have a *Server or similar available to plumb though.
To add a new setting, call one of the `Register` methods in `registry.go` and save the accessor created by the register function in the package where the setting is to be used. For example, to add an "enterprise" flag, adding into license_check.go:
var enterpriseEnabled = settings.RegisterBoolSetting(
"enterprise.enabled", "some doc for the setting", false,
)
Then use with `if enterpriseEnabled.Get() ...`
Settings should always be defined with "safe" default values -- until a node receives values via gossip, or even after that, if it cannot read them for some reason, it will use the default values, so define defaults that "fail safe".
In cases where the "safe" default doesn't actually match the desired default, like respecting an opt-*out* setting, we can default to `false` (opted out) and then use a migration to write an explicit `true`: in practice you'd still expect to read `true` unless a preference is expressed, but in the rare cases where you read a default, you don't risk ignoring an expressed opt-out.
Ideally, when passing configuration into some structure or subsystem, e.g. a rate limit into a client or something, passing a `*FooSetting` rather than a `Foo` and waiting to call `.Get()` until the value is actually used ensures observing the latest value.
Existing/off-the-shelf systems generally will not be defined in terms of our settings, but if they can either be swapped at runtime or expose some `setFoo` method, that can be used in conjunction with a change callback registered via OnChange.
Index ¶
- Variables
- func EncodeBool(b bool) string
- func EncodeDuration(d time.Duration) string
- func EncodeFloat(f float64) string
- func EncodeInt(i int64) string
- func Keys() (res []string)
- func SetCanonicalValuesContainer(v *Values)
- type BoolSetting
- func (i BoolSetting) Description() string
- func (b *BoolSetting) Get(sv *Values) bool
- func (i BoolSetting) Hidden() bool
- func (i *BoolSetting) Hide()
- func (b *BoolSetting) Override(sv *Values, v bool)
- func (i *BoolSetting) SetOnChange(sv *Values, fn func())
- func (b *BoolSetting) String(sv *Values) string
- func (*BoolSetting) Typ() string
- type ByteSizeSetting
- type DurationSetting
- func RegisterDurationSetting(key, desc string, defaultValue time.Duration) *DurationSetting
- func RegisterNonNegativeDurationSetting(key, desc string, defaultValue time.Duration) *DurationSetting
- func RegisterValidatedDurationSetting(key, desc string, defaultValue time.Duration, ...) *DurationSetting
- func (i DurationSetting) Description() string
- func (d *DurationSetting) Get(sv *Values) time.Duration
- func (i DurationSetting) Hidden() bool
- func (i *DurationSetting) Hide()
- func (d *DurationSetting) Override(sv *Values, v time.Duration)
- func (i *DurationSetting) SetOnChange(sv *Values, fn func())
- func (d *DurationSetting) String(sv *Values) string
- func (*DurationSetting) Typ() string
- func (d *DurationSetting) Validate(v time.Duration) error
- type EnumSetting
- type FloatSetting
- func (i FloatSetting) Description() string
- func (f *FloatSetting) Get(sv *Values) float64
- func (i FloatSetting) Hidden() bool
- func (i *FloatSetting) Hide()
- func (i *FloatSetting) SetOnChange(sv *Values, fn func())
- func (f *FloatSetting) String(sv *Values) string
- func (*FloatSetting) Typ() string
- func (f *FloatSetting) Validate(v float64) error
- type IntSetting
- func (i IntSetting) Description() string
- func (i *IntSetting) Get(sv *Values) int64
- func (i IntSetting) Hidden() bool
- func (i *IntSetting) Hide()
- func (i *IntSetting) Override(sv *Values, v int64)
- func (i *IntSetting) SetOnChange(sv *Values, fn func())
- func (i *IntSetting) String(sv *Values) string
- func (*IntSetting) Typ() string
- func (i *IntSetting) Validate(v int64) error
- type NoopUpdater
- type Setting
- type StateMachineSetting
- func (i StateMachineSetting) Description() string
- func (s *StateMachineSetting) Get(sv *Values) string
- func (i StateMachineSetting) Hidden() bool
- func (i *StateMachineSetting) Hide()
- func (i *StateMachineSetting) SetOnChange(sv *Values, fn func())
- func (s *StateMachineSetting) String(sv *Values) string
- func (*StateMachineSetting) Typ() string
- func (s *StateMachineSetting) Validate(sv *Values, old []byte, update *string) ([]byte, interface{}, error)
- type StringSetting
- func (i StringSetting) Description() string
- func (s *StringSetting) Get(sv *Values) string
- func (i StringSetting) Hidden() bool
- func (i *StringSetting) Hide()
- func (i *StringSetting) SetOnChange(sv *Values, fn func())
- func (s *StringSetting) String(sv *Values) string
- func (*StringSetting) Typ() string
- func (s *StringSetting) Validate(v string) error
- type TransformerFn
- type Updater
- type Values
Constants ¶
This section is empty.
Variables ¶
var Registry = make(map[string]Setting)
Registry contains all defined settings, their types and default values.
The registry does not store the current values of the settings; those are stored separately in Values, allowing multiple independent instances of each setting in the registry.
Registry should never be mutated after creation (except in tests), as it is read concurrently by different callers.
var TestOpaque interface{} = testOpaqueType{}
TestOpaque can be passed to Values.Init when we are testing the settings infrastructure.
Functions ¶
func EncodeBool ¶
EncodeBool encodes a bool in the format parseRaw expects.
func EncodeDuration ¶
EncodeDuration encodes a duration in the format parseRaw expects.
func EncodeFloat ¶
EncodeFloat encodes a bool in the format parseRaw expects.
func SetCanonicalValuesContainer ¶
func SetCanonicalValuesContainer(v *Values)
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.
Types ¶
type BoolSetting ¶
type BoolSetting struct {
// contains filtered or unexported fields
}
BoolSetting is the interface of a setting variable that will be updated automatically when the corresponding cluster-wide setting of type "bool" is updated.
func RegisterBoolSetting ¶
func RegisterBoolSetting(key, desc string, defaultValue bool) *BoolSetting
RegisterBoolSetting defines a new setting with type bool.
func (BoolSetting) Description ¶
func (i BoolSetting) Description() string
func (*BoolSetting) Get ¶
func (b *BoolSetting) Get(sv *Values) bool
Get retrieves the bool value in the setting.
func (*BoolSetting) Hide ¶
func (i *BoolSetting) Hide()
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 (*BoolSetting) Override ¶
func (b *BoolSetting) Override(sv *Values, v bool)
Override changes the setting without validation. For testing usage only.
func (*BoolSetting) SetOnChange ¶
func (i *BoolSetting) SetOnChange(sv *Values, fn func())
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 (*BoolSetting) String ¶
func (b *BoolSetting) String(sv *Values) string
func (*BoolSetting) Typ ¶
func (*BoolSetting) Typ() string
Typ returns the short (1 char) string denoting the type of setting.
type ByteSizeSetting ¶
type ByteSizeSetting struct {
IntSetting
}
ByteSizeSetting is the interface of a setting variable that will be updated automatically when the corresponding cluster-wide setting of type "bytesize" is updated.
func RegisterByteSizeSetting ¶
func RegisterByteSizeSetting(key, desc string, defaultValue int64) *ByteSizeSetting
RegisterByteSizeSetting defines a new setting with type bytesize.
func RegisterValidatedByteSizeSetting ¶
func RegisterValidatedByteSizeSetting( key, desc string, defaultValue int64, validateFn func(int64) error, ) *ByteSizeSetting
RegisterValidatedByteSizeSetting defines a new setting with type bytesize with a validation function.
func (ByteSizeSetting) Description ¶
func (i ByteSizeSetting) Description() string
func (*ByteSizeSetting) Hide ¶
func (i *ByteSizeSetting) Hide()
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 (*ByteSizeSetting) SetOnChange ¶
func (i *ByteSizeSetting) SetOnChange(sv *Values, fn func())
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 (*ByteSizeSetting) String ¶
func (b *ByteSizeSetting) String(sv *Values) string
func (*ByteSizeSetting) Typ ¶
func (*ByteSizeSetting) Typ() string
Typ returns the short (1 char) string denoting the type of setting.
type DurationSetting ¶
type DurationSetting struct {
// contains filtered or unexported fields
}
DurationSetting is the interface of a setting variable that will be updated automatically when the corresponding cluster-wide setting of type "duration" is updated.
func RegisterDurationSetting ¶
func RegisterDurationSetting(key, desc string, defaultValue time.Duration) *DurationSetting
RegisterDurationSetting defines a new setting with type duration.
func RegisterNonNegativeDurationSetting ¶
func RegisterNonNegativeDurationSetting( key, desc string, defaultValue time.Duration, ) *DurationSetting
RegisterNonNegativeDurationSetting defines a new setting with type duration.
func RegisterValidatedDurationSetting ¶
func RegisterValidatedDurationSetting( key, desc string, defaultValue time.Duration, validateFn func(time.Duration) error, ) *DurationSetting
RegisterValidatedDurationSetting defines a new setting with type duration.
func (DurationSetting) Description ¶
func (i DurationSetting) Description() string
func (*DurationSetting) Get ¶
func (d *DurationSetting) Get(sv *Values) time.Duration
Get retrieves the duration value in the setting.
func (*DurationSetting) Hide ¶
func (i *DurationSetting) Hide()
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 (*DurationSetting) Override ¶
func (d *DurationSetting) Override(sv *Values, v time.Duration)
Override changes the setting without validation. For testing usage only.
func (*DurationSetting) SetOnChange ¶
func (i *DurationSetting) SetOnChange(sv *Values, fn func())
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 (*DurationSetting) String ¶
func (d *DurationSetting) String(sv *Values) string
func (*DurationSetting) Typ ¶
func (*DurationSetting) Typ() string
Typ returns the short (1 char) string denoting the type of setting.
type EnumSetting ¶
type EnumSetting struct { IntSetting // contains filtered or unexported fields }
EnumSetting is a StringSetting that restricts the values to be one of the `enumValues`
func RegisterEnumSetting ¶
func RegisterEnumSetting( key, desc string, defaultValue string, enumValues map[int64]string, ) *EnumSetting
RegisterEnumSetting defines a new setting with type int.
func (EnumSetting) Description ¶
func (i EnumSetting) Description() string
func (*EnumSetting) Hide ¶
func (i *EnumSetting) Hide()
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 (*EnumSetting) ParseEnum ¶
func (e *EnumSetting) ParseEnum(raw string) (int64, bool)
ParseEnum returns the enum value, and a boolean that indicates if it was parseable.
func (*EnumSetting) SetOnChange ¶
func (i *EnumSetting) SetOnChange(sv *Values, fn func())
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 (*EnumSetting) Typ ¶
func (e *EnumSetting) Typ() string
Typ returns the short (1 char) string denoting the type of setting.
type FloatSetting ¶
type FloatSetting struct {
// contains filtered or unexported fields
}
FloatSetting is the interface of a setting variable that will be updated automatically when the corresponding cluster-wide setting of type "float" is updated.
func RegisterFloatSetting ¶
func RegisterFloatSetting(key, desc string, defaultValue float64) *FloatSetting
RegisterFloatSetting defines a new setting with type float.
func RegisterNonNegativeFloatSetting ¶
func RegisterNonNegativeFloatSetting(key, desc string, defaultValue float64) *FloatSetting
RegisterNonNegativeFloatSetting defines a new setting with type float.
func RegisterValidatedFloatSetting ¶
func RegisterValidatedFloatSetting( key, desc string, defaultValue float64, validateFn func(float64) error, ) *FloatSetting
RegisterValidatedFloatSetting defines a new setting with type float.
func (FloatSetting) Description ¶
func (i FloatSetting) Description() string
func (*FloatSetting) Get ¶
func (f *FloatSetting) Get(sv *Values) float64
Get retrieves the float value in the setting.
func (*FloatSetting) Hide ¶
func (i *FloatSetting) Hide()
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 (*FloatSetting) SetOnChange ¶
func (i *FloatSetting) SetOnChange(sv *Values, fn func())
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 (*FloatSetting) String ¶
func (f *FloatSetting) String(sv *Values) string
func (*FloatSetting) Typ ¶
func (*FloatSetting) Typ() string
Typ returns the short (1 char) string denoting the type of setting.
func (*FloatSetting) Validate ¶
func (f *FloatSetting) Validate(v float64) error
Validate that a value conforms with the validation function.
type IntSetting ¶
type IntSetting struct {
// contains filtered or unexported fields
}
IntSetting is the interface of a setting variable that will be updated automatically when the corresponding cluster-wide setting of type "int" is updated.
func RegisterIntSetting ¶
func RegisterIntSetting(key, desc string, defaultValue int64) *IntSetting
RegisterIntSetting defines a new setting with type int.
func RegisterValidatedIntSetting ¶
func RegisterValidatedIntSetting( key, desc string, defaultValue int64, validateFn func(int64) error, ) *IntSetting
RegisterValidatedIntSetting defines a new setting with type int with a validation function.
func (IntSetting) Description ¶
func (i IntSetting) Description() string
func (*IntSetting) Get ¶
func (i *IntSetting) Get(sv *Values) int64
Get retrieves the int value in the setting.
func (*IntSetting) Hide ¶
func (i *IntSetting) Hide()
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 (*IntSetting) Override ¶
func (i *IntSetting) Override(sv *Values, v int64)
Override changes the setting without validation. For testing usage only.
func (*IntSetting) SetOnChange ¶
func (i *IntSetting) SetOnChange(sv *Values, fn func())
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 (*IntSetting) String ¶
func (i *IntSetting) String(sv *Values) string
func (*IntSetting) Typ ¶
func (*IntSetting) Typ() string
Typ returns the short (1 char) string denoting the type of setting.
func (*IntSetting) Validate ¶
func (i *IntSetting) Validate(v int64) error
Validate that a value conforms with the validation function.
type NoopUpdater ¶
type NoopUpdater struct{}
A NoopUpdater ignores all updates.
func (NoopUpdater) ResetRemaining ¶
func (u NoopUpdater) ResetRemaining()
ResetRemaining implements Updater. It is a no-op.
func (NoopUpdater) Set ¶
func (u NoopUpdater) Set(_, _, _ string) error
Set implements Updater. It is a no-op.
type Setting ¶
type Setting interface { // Typ returns the short (1 char) string denoting the type of setting. Typ() string String(sv *Values) string Description() string Hidden() bool SetOnChange(sv *Values, fn func()) // contains filtered or unexported methods }
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 StateMachineSetting ¶
type StateMachineSetting struct {
// contains filtered or unexported fields
}
A StateMachineSetting is a setting that keeps a state machine driven by user input.
For (a nonsensical) example, a StateMachineSetting can be used to maintain an encoded protobuf containing an integer that the user can only increment by 3 if the int is odd and by two if it is even. More generally, the setting starts from an initial state, and can take the current state into account when determining user input. Initially this is motivated for use in cluster version upgrades.
The state machine as well as its encoding are represented by the TransformerFn backing this StateMachineSetting. It is a method that takes
- the Values instance
- the known previous encoded value, if any (i.e. the current state)
- the update the user wants to make to the encoded struct (i.e. the desired transition)
and returns
- the new encoded value (i.e. the new state)
- an interface backed by a "user-friendly" representation of the new state (i.e. something that can be printed)
- an error if the input was illegal.
Furthermore,
- with a `nil` input byte slice, the default value (hidden in the transformer) is used internally
- with a `nil` update, the previous (or default) value is returned
- with both non-nil, a transition from the old state using the given update is carried out, and the new state and representation (or an error) returned.
The opaque member of Values can be used to associate a higher-level object to the Values instance (making it accessible to the function).
Updates to the setting via an Updater validate the new state syntactically, but not semantically. Users must call Validate with the authoritative previous state, the suggested state transition, and then overwrite the state machine with the result.
func RegisterStateMachineSetting ¶
func RegisterStateMachineSetting(key, desc string, transformer TransformerFn) *StateMachineSetting
RegisterStateMachineSetting registers a StateMachineSetting. See the comment for StateMachineSetting for details.
func (StateMachineSetting) Description ¶
func (i StateMachineSetting) Description() string
func (*StateMachineSetting) Get ¶
func (s *StateMachineSetting) Get(sv *Values) string
Get retrieves the (encoded) value in the setting (or the encoded default).
func (*StateMachineSetting) Hide ¶
func (i *StateMachineSetting) Hide()
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 (*StateMachineSetting) SetOnChange ¶
func (i *StateMachineSetting) SetOnChange(sv *Values, fn func())
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 (*StateMachineSetting) String ¶
func (s *StateMachineSetting) String(sv *Values) string
func (*StateMachineSetting) Typ ¶
func (*StateMachineSetting) Typ() string
Typ returns the short (1 char) string denoting the type of setting.
func (*StateMachineSetting) Validate ¶
func (s *StateMachineSetting) Validate( sv *Values, old []byte, update *string, ) ([]byte, interface{}, error)
Validate that the state machine accepts the user input. Returns new encoded state, unencoded state, or an error. If no update is given, round trips current state.
type StringSetting ¶
type StringSetting struct {
// contains filtered or unexported fields
}
StringSetting is the interface of a setting variable that will be updated automatically when the corresponding cluster-wide setting of type "string" is updated.
func RegisterStringSetting ¶
func RegisterStringSetting(key, desc string, defaultValue string) *StringSetting
RegisterStringSetting defines a new setting with type string.
func RegisterValidatedStringSetting ¶
func RegisterValidatedStringSetting( key, desc string, defaultValue string, validateFn func(string) error, ) *StringSetting
RegisterValidatedStringSetting defines a new setting with type string with a validation function.
func (StringSetting) Description ¶
func (i StringSetting) Description() string
func (*StringSetting) Get ¶
func (s *StringSetting) Get(sv *Values) string
Get retrieves the string value in the setting.
func (*StringSetting) Hide ¶
func (i *StringSetting) Hide()
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 (*StringSetting) SetOnChange ¶
func (i *StringSetting) SetOnChange(sv *Values, fn func())
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 (*StringSetting) String ¶
func (s *StringSetting) String(sv *Values) string
func (*StringSetting) Typ ¶
func (*StringSetting) Typ() string
Typ returns the short (1 char) string denoting the type of setting.
func (*StringSetting) Validate ¶
func (s *StringSetting) Validate(v string) error
Validate that a value conforms with the validation function.
type TransformerFn ¶
type TransformerFn func(sv *Values, old []byte, update *string) (finalV []byte, finalObj interface{}, _ error)
A TransformerFn encapsulates the logic of a StateMachineSetting.
type Updater ¶
Updater is a helper for updating the in-memory settings.
RefreshSettings passes the serialized representations of all individual settings -- e.g. the rows read from the system.settings table. We update the wrapped atomic settings values as we go and note which settings were updated, then set the rest to default in ResetRemaining().
type Values ¶
type Values struct {
// contains filtered or unexported fields
}
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).
func TODO ¶
func TODO() *Values
TODO is usable at callsites that do not have *settings.Values available. Please don't use this.