Skip to content

Commit a7c1494

Browse files
craig[bot]dtmaryliag
committed
104449: sql: add default_value and origin to show cluster settings r=maryliag a=maryliag Fixes https://cockroachlabs.atlassian.net/browse/CRDB-28519 **Commit 1** 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. **Commit 2** Previously, there was no easy way to see default values for cluster settings. This commit add the column for `default_value` and `origin` to `crdb_internal.cluster_settings` and the `show cluster settings` command. Release note (sql change): Add columns `default_value` and `origin` (default, override, external-override) to the `show cluster settings` command. Co-authored-by: David Taylor <[email protected]> Co-authored-by: maryliag <[email protected]>
2 parents 1fd8581 + e64b6a4 commit a7c1494

File tree

13 files changed

+140
-24
lines changed

13 files changed

+140
-24
lines changed

pkg/ccl/logictestccl/testdata/logic_test/crdb_internal_tenant

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,10 +224,10 @@ SELECT * FROM crdb_internal.session_trace WHERE span_idx < 0
224224
----
225225
span_idx message_idx timestamp duration operation loc tag message age
226226

227-
query TTTBT colnames
227+
query TTTBTTT colnames
228228
SELECT * FROM crdb_internal.cluster_settings WHERE variable = ''
229229
----
230-
variable value type public description
230+
variable value type public description default_value origin
231231

232232
query TI colnames
233233
SELECT * FROM crdb_internal.feature_usage WHERE feature_name = ''

pkg/server/settings_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ func TestSettingsShowAll(t *testing.T) {
276276
if len(rows) < 2 {
277277
t.Fatalf("show all returned too few rows (%d)", len(rows))
278278
}
279-
const expColumns = 5
279+
const expColumns = 7
280280
if len(rows[0]) != expColumns {
281281
t.Fatalf("show all must return %d columns, found %d", expColumns, len(rows[0]))
282282
}

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: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@
1010

1111
package settings
1212

13-
import "context"
13+
import (
14+
"context"
15+
"fmt"
16+
)
1417

1518
// Setting is the interface exposing the metadata for a cluster setting.
1619
//
@@ -79,6 +82,9 @@ type NonMaskedSetting interface {
7982
// ErrorHint returns a hint message to be displayed to the user when there's
8083
// an error.
8184
ErrorHint() (bool, string)
85+
86+
// ValueOrigin returns the origin of the current value.
87+
ValueOrigin(ctx context.Context, sv *Values) ValueOrigin
8288
}
8389

8490
// Class describes the scope of a setting in multi-tenant scenarios. While all
@@ -142,3 +148,24 @@ const (
142148
// In short: "Go ahead but be careful."
143149
Public
144150
)
151+
152+
// ValueOrigin indicates the origin of the current value of a setting, e.g. if
153+
// it is coming from the in-code default or an explicit override.
154+
type ValueOrigin uint32
155+
156+
const (
157+
// OriginDefault indicates the value in use is the default value.
158+
OriginDefault ValueOrigin = iota
159+
// OriginExplicitlySet indicates the value is has been set explicitly.
160+
OriginExplicitlySet
161+
// OriginExternallySet indicates the value has been set externally, such as
162+
// via a host-cluster override for this or all tenant(s).
163+
OriginExternallySet
164+
)
165+
166+
func (v ValueOrigin) String() string {
167+
if v > OriginExternallySet {
168+
return fmt.Sprintf("invalid (%d)", v)
169+
}
170+
return [...]string{"default", "override", "external-override"}[v]
171+
}

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{}) {

pkg/sql/crdb_internal.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2054,7 +2054,9 @@ CREATE TABLE crdb_internal.cluster_settings (
20542054
value STRING NOT NULL,
20552055
type STRING NOT NULL,
20562056
public BOOL NOT NULL, -- whether the setting is documented, which implies the user can expect support.
2057-
description STRING NOT NULL
2057+
description STRING NOT NULL,
2058+
default_value STRING NOT NULL,
2059+
origin STRING NOT NULL -- the origin of the value: 'default' , 'override' or 'external-override'
20582060
)`,
20592061
populate: func(ctx context.Context, p *planner, _ catalog.DatabaseDescriptor, addRow func(...tree.Datum) error) error {
20602062
hasSqlModify, err := p.HasPrivilege(ctx, syntheticprivilege.GlobalPrivilegeObject, privilege.MODIFYSQLCLUSTERSETTING, p.User())
@@ -2093,12 +2095,19 @@ CREATE TABLE crdb_internal.cluster_settings (
20932095
strVal := setting.String(&p.ExecCfg().Settings.SV)
20942096
isPublic := setting.Visibility() == settings.Public
20952097
desc := setting.Description()
2098+
defaultVal, err := setting.DecodeToString(setting.EncodedDefault())
2099+
if err != nil {
2100+
return err
2101+
}
2102+
origin := setting.ValueOrigin(ctx, &p.ExecCfg().Settings.SV).String()
20962103
if err := addRow(
20972104
tree.NewDString(k),
20982105
tree.NewDString(strVal),
20992106
tree.NewDString(setting.Typ()),
21002107
tree.MakeDBool(tree.DBool(isPublic)),
21012108
tree.NewDString(desc),
2109+
tree.NewDString(defaultVal),
2110+
tree.NewDString(origin),
21022111
); err != nil {
21032112
return err
21042113
}

pkg/sql/delegate/show_all_cluster_settings.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,12 @@ func (d *delegator) delegateShowClusterSettingList(
7777

7878
if stmt.All {
7979
return d.parse(
80-
`SELECT variable, value, type AS setting_type, public, description
80+
`SELECT variable, value, type AS setting_type, public, description, default_value, origin
8181
FROM crdb_internal.cluster_settings`,
8282
)
8383
}
8484
return d.parse(
85-
`SELECT variable, value, type AS setting_type, description
85+
`SELECT variable, value, type AS setting_type, description, default_value, origin
8686
FROM crdb_internal.cluster_settings
8787
WHERE public IS TRUE`,
8888
)

pkg/sql/logictest/testdata/logic_test/cluster_settings

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,44 @@ WHERE variable IN ('sql.defaults.default_int_size')
7878
----
7979
sql.defaults.default_int_size 4
8080

81+
query TTTT
82+
SELECT variable, value, default_value, origin FROM [SHOW ALL CLUSTER SETTINGS]
83+
WHERE variable IN ('sql.index_recommendation.drop_unused_duration')
84+
----
85+
sql.index_recommendation.drop_unused_duration 168h0m0s 168h0m0s default
86+
87+
statement ok
88+
SET CLUSTER SETTING sql.index_recommendation.drop_unused_duration = '10s'
89+
90+
query TTTT
91+
SELECT variable, value, default_value, origin FROM [SHOW ALL CLUSTER SETTINGS]
92+
WHERE variable IN ('sql.index_recommendation.drop_unused_duration')
93+
----
94+
sql.index_recommendation.drop_unused_duration 10s 168h0m0s override
95+
96+
statement ok
97+
RESET CLUSTER SETTING sql.index_recommendation.drop_unused_duration
98+
99+
query TTTT
100+
SELECT variable, value, default_value, origin FROM [SHOW ALL CLUSTER SETTINGS]
101+
WHERE variable IN ('sql.index_recommendation.drop_unused_duration')
102+
----
103+
sql.index_recommendation.drop_unused_duration 168h0m0s 168h0m0s default
104+
105+
user host-cluster-root
106+
107+
statement ok
108+
ALTER TENANT ALL SET CLUSTER SETTING sql.index_recommendation.drop_unused_duration = '50s'
109+
110+
user root
111+
112+
onlyif config 3node-tenant-default-configs
113+
query TTTT
114+
SELECT variable, value, default_value, origin FROM [SHOW ALL CLUSTER SETTINGS]
115+
WHERE variable IN ('sql.index_recommendation.drop_unused_duration')
116+
----
117+
sql.index_recommendation.drop_unused_duration 50s 168h0m0s external-override
118+
81119
user root
82120

83121
statement ok

0 commit comments

Comments
 (0)