Skip to content

Commit f13f008

Browse files
dtmaryliag
authored andcommitted
settings: track the origin of the current values
This adds an API to the settings package to track the origin of the current value of each setting, such as whether it is set by the the default value, an explicit override or an external override. Release note: none. Epic: none.
1 parent 2c8e559 commit f13f008

File tree

5 files changed

+64
-5
lines changed

5 files changed

+64
-5
lines changed

pkg/server/settingswatcher/settings_watcher.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ func (s *SettingsWatcher) maybeSet(ctx context.Context, name string, sv settings
292292
}
293293
} else {
294294
if !hasOverride {
295-
s.setLocked(ctx, name, sv.val)
295+
s.setLocked(ctx, name, sv.val, settings.OriginExplicitlySet)
296296
}
297297
}
298298
}
@@ -309,7 +309,9 @@ type settingsValue struct {
309309
const versionSettingKey = "version"
310310

311311
// set the current value of a setting.
312-
func (s *SettingsWatcher) setLocked(ctx context.Context, key string, val settings.EncodedValue) {
312+
func (s *SettingsWatcher) setLocked(
313+
ctx context.Context, key string, val settings.EncodedValue, origin settings.ValueOrigin,
314+
) {
313315
// Both the system tenant and secondary tenants no longer use this code
314316
// path to propagate cluster version changes (they rely on
315317
// BumpClusterVersion instead). The secondary tenants however, still rely
@@ -335,6 +337,7 @@ func (s *SettingsWatcher) setLocked(ctx context.Context, key string, val setting
335337
if err := s.mu.updater.Set(ctx, key, val); err != nil {
336338
log.Warningf(ctx, "failed to set setting %s to %s: %v", redact.Safe(key), val.Value, err)
337339
}
340+
s.mu.updater.SetValueOrigin(ctx, key, origin)
338341
}
339342

340343
// setDefaultLocked sets a setting to its default value.
@@ -348,7 +351,7 @@ func (s *SettingsWatcher) setDefaultLocked(ctx context.Context, key string) {
348351
Value: setting.EncodedDefault(),
349352
Type: setting.Typ(),
350353
}
351-
s.setLocked(ctx, key, val)
354+
s.setLocked(ctx, key, val, settings.OriginDefault)
352355
}
353356

354357
// updateOverrides updates the overrides map and updates any settings
@@ -384,7 +387,7 @@ func (s *SettingsWatcher) updateOverrides(ctx context.Context) {
384387
}
385388
// A new override was added or an existing override has changed.
386389
s.mu.overrides[key] = val
387-
s.setLocked(ctx, key, val)
390+
s.setLocked(ctx, key, val, settings.OriginExternallySet)
388391
}
389392

390393
// Clean up any overrides that were removed.
@@ -395,7 +398,7 @@ func (s *SettingsWatcher) updateOverrides(ctx context.Context) {
395398
// Reset the setting to the value in the settings table (or the default
396399
// value).
397400
if sv, ok := s.mu.values[key]; ok && !sv.tombstone {
398-
s.setLocked(ctx, key, sv.val)
401+
s.setLocked(ctx, key, sv.val, settings.OriginExplicitlySet)
399402
} else {
400403
s.setDefaultLocked(ctx, key)
401404
}

pkg/settings/common.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,15 @@ func (c *common) ErrorHint() (bool, string) {
7474
return false, ""
7575
}
7676

77+
func (c *common) getSlot() slotIdx {
78+
return c.slot
79+
}
80+
81+
// ValueOrigin returns the origin of the current value of the setting.
82+
func (c *common) ValueOrigin(ctx context.Context, sv *Values) ValueOrigin {
83+
return sv.getValueOrigin(ctx, c.slot)
84+
}
85+
7786
// SetReportable indicates whether a setting's value can show up in SHOW ALL
7887
// CLUSTER SETTINGS and telemetry reports.
7988
//
@@ -114,6 +123,8 @@ type internalSetting interface {
114123
isRetired() bool
115124
setToDefault(ctx context.Context, sv *Values)
116125

126+
getSlot() slotIdx
127+
117128
// isReportable indicates whether the value of the setting can be
118129
// included in user-facing reports such as that produced by SHOW ALL
119130
// CLUSTER SETTINGS.

pkg/settings/setting.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ type NonMaskedSetting interface {
7979
// ErrorHint returns a hint message to be displayed to the user when there's
8080
// an error.
8181
ErrorHint() (bool, string)
82+
83+
// ValueOrigin returns the origin of the current value.
84+
ValueOrigin(ctx context.Context, sv *Values) ValueOrigin
8285
}
8386

8487
// Class describes the scope of a setting in multi-tenant scenarios. While all
@@ -142,3 +145,17 @@ const (
142145
// In short: "Go ahead but be careful."
143146
Public
144147
)
148+
149+
// ValueOrigin indicates the origin of the current value of a setting, e.g. if
150+
// it is coming from the in-code default or an explicit override.
151+
type ValueOrigin uint32
152+
153+
const (
154+
// OriginDefault indicates the value in use is the default value.
155+
OriginDefault ValueOrigin = iota
156+
// OriginExplicitlySet indicates the value is has been set explicitly.
157+
OriginExplicitlySet
158+
// OriginExternallySet indicates the value has been set externally, such as
159+
// via a host-cluster override for this or all tenant(s).
160+
OriginExternallySet
161+
)

pkg/settings/updater.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ type updater struct {
7272
type Updater interface {
7373
Set(ctx context.Context, key string, value EncodedValue) error
7474
ResetRemaining(ctx context.Context)
75+
SetValueOrigin(ctx context.Context, key string, origin ValueOrigin)
7576
}
7677

7778
// A NoopUpdater ignores all updates.
@@ -83,6 +84,8 @@ func (u NoopUpdater) Set(ctx context.Context, key string, value EncodedValue) er
8384
// ResetRemaining implements Updater. It is a no-op.
8485
func (u NoopUpdater) ResetRemaining(context.Context) {}
8586

87+
func (u NoopUpdater) SetValueOrigin(ctx context.Context, key string, origin ValueOrigin) {}
88+
8689
// NewUpdater makes an Updater.
8790
func NewUpdater(sv *Values) Updater {
8891
if ignoreAllUpdates {
@@ -165,6 +168,13 @@ func (u updater) Set(ctx context.Context, key string, value EncodedValue) error
165168
// ResetRemaining sets all settings not updated by the updater to their default values.
166169
func (u updater) ResetRemaining(ctx context.Context) {
167170
for k, v := range registry {
171+
172+
if _, hasOverride := u.m[k]; hasOverride {
173+
u.sv.setValueOrigin(ctx, v.getSlot(), OriginExplicitlySet)
174+
} else {
175+
u.sv.setValueOrigin(ctx, v.getSlot(), OriginDefault)
176+
}
177+
168178
if u.sv.NonSystemTenant() && v.Class() == SystemOnly {
169179
// Don't try to reset system settings on a non-system tenant.
170180
continue
@@ -174,3 +184,11 @@ func (u updater) ResetRemaining(ctx context.Context) {
174184
}
175185
}
176186
}
187+
188+
// SetValueOrigin sets the origin of the value of a given setting.
189+
func (u updater) SetValueOrigin(ctx context.Context, key string, origin ValueOrigin) {
190+
d, ok := registry[key]
191+
if ok {
192+
u.sv.setValueOrigin(ctx, d.getSlot(), origin)
193+
}
194+
}

pkg/settings/values.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ type valuesContainer struct {
6161
// current context (i.e. it is a SystemOnly setting and the container is for a
6262
// tenant). Reading or writing such a setting causes panics in test builds.
6363
forbidden [numSlots]bool
64+
65+
hasValue [numSlots]uint32
6466
}
6567

6668
func (c *valuesContainer) setGenericVal(slot slotIdx, newVal interface{}) {
@@ -154,6 +156,14 @@ func (sv *Values) setInt64(ctx context.Context, slot slotIdx, newVal int64) {
154156
}
155157
}
156158

159+
func (sv *Values) setValueOrigin(ctx context.Context, slot slotIdx, origin ValueOrigin) {
160+
atomic.StoreUint32(&sv.container.hasValue[slot], uint32(origin))
161+
}
162+
163+
func (sv *Values) getValueOrigin(ctx context.Context, slot slotIdx) ValueOrigin {
164+
return ValueOrigin(atomic.LoadUint32(&sv.container.hasValue[slot]))
165+
}
166+
157167
// setDefaultOverride overrides the default value for the respective setting to
158168
// newVal.
159169
func (sv *Values) setDefaultOverride(slot slotIdx, newVal interface{}) {

0 commit comments

Comments
 (0)