-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
settings_watcher.go
372 lines (336 loc) · 10.8 KB
/
settings_watcher.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
// Copyright 2020 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
// Package settingswatcher provides utilities to update cluster settings using
// a range feed.
package settingswatcher
import (
"context"
"github.com/cockroachdb/cockroach/pkg/clusterversion"
"github.com/cockroachdb/cockroach/pkg/keys"
"github.com/cockroachdb/cockroach/pkg/kv/kvclient/rangefeed"
"github.com/cockroachdb/cockroach/pkg/kv/kvclient/rangefeed/rangefeedbuffer"
"github.com/cockroachdb/cockroach/pkg/kv/kvclient/rangefeed/rangefeedcache"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/settings"
"github.com/cockroachdb/cockroach/pkg/settings/cluster"
"github.com/cockroachdb/cockroach/pkg/util/hlc"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/cockroach/pkg/util/protoutil"
"github.com/cockroachdb/cockroach/pkg/util/stop"
"github.com/cockroachdb/cockroach/pkg/util/syncutil"
"github.com/cockroachdb/errors"
"github.com/cockroachdb/redact"
)
// SettingsWatcher is used to watch for cluster settings changes with a
// rangefeed.
type SettingsWatcher struct {
clock *hlc.Clock
codec keys.SQLCodec
settings *cluster.Settings
f *rangefeed.Factory
stopper *stop.Stopper
dec RowDecoder
storage Storage
overridesMonitor OverridesMonitor
mu struct {
syncutil.Mutex
updater settings.Updater
values map[string]settings.EncodedValue
overrides map[string]settings.EncodedValue
}
// testingWatcherKnobs allows the client to inject testing knobs into
// the underlying rangefeedcache.Watcher.
testingWatcherKnobs *rangefeedcache.TestingKnobs
}
// Storage is used to write a snapshot of KVs out to disk for use upon restart.
type Storage interface {
SnapshotKVs(ctx context.Context, kvs []roachpb.KeyValue)
}
// New constructs a new SettingsWatcher.
func New(
clock *hlc.Clock,
codec keys.SQLCodec,
settingsToUpdate *cluster.Settings,
f *rangefeed.Factory,
stopper *stop.Stopper,
storage Storage, // optional
) *SettingsWatcher {
return &SettingsWatcher{
clock: clock,
codec: codec,
settings: settingsToUpdate,
f: f,
stopper: stopper,
dec: MakeRowDecoder(codec),
storage: storage,
}
}
// NewWithOverrides constructs a new SettingsWatcher which allows external
// overrides, discovered through an OverridesMonitor.
func NewWithOverrides(
clock *hlc.Clock,
codec keys.SQLCodec,
settingsToUpdate *cluster.Settings,
f *rangefeed.Factory,
stopper *stop.Stopper,
overridesMonitor OverridesMonitor,
storage Storage,
) *SettingsWatcher {
s := New(clock, codec, settingsToUpdate, f, stopper, storage)
s.overridesMonitor = overridesMonitor
settingsToUpdate.OverridesInformer = s
return s
}
// Start will start the SettingsWatcher. It returns after the initial settings
// have been retrieved. An error will be returned if the context is canceled or
// the stopper is stopped prior to the initial data being retrieved.
func (s *SettingsWatcher) Start(ctx context.Context) error {
settingsTablePrefix := s.codec.TablePrefix(keys.SettingsTableID)
settingsTableSpan := roachpb.Span{
Key: settingsTablePrefix,
EndKey: settingsTablePrefix.PrefixEnd(),
}
s.resetUpdater()
var initialScan = struct {
ch chan struct{}
done bool
err error
}{
ch: make(chan struct{}),
}
noteUpdate := func(update rangefeedcache.Update) {
if update.Type != rangefeedcache.CompleteUpdate {
return
}
s.mu.Lock()
defer s.mu.Unlock()
s.mu.updater.ResetRemaining(ctx)
if !initialScan.done {
initialScan.done = true
close(initialScan.ch)
}
}
s.mu.values = make(map[string]settings.EncodedValue)
if s.overridesMonitor != nil {
s.mu.overrides = make(map[string]settings.EncodedValue)
// Initialize the overrides. We want to do this before processing the
// settings table, otherwise we could see temporary transitions to the value
// in the table.
s.updateOverrides(ctx)
// Set up a worker to watch the monitor.
if err := s.stopper.RunAsyncTask(ctx, "setting-overrides", func(ctx context.Context) {
overridesCh := s.overridesMonitor.RegisterOverridesChannel()
for {
select {
case <-overridesCh:
s.updateOverrides(ctx)
case <-s.stopper.ShouldQuiesce():
return
}
}
}); err != nil {
// We are shutting down.
return err
}
}
// bufferSize configures how large of a buffer to permit for accumulated
// changes of settings between resolved timestamps. It's an arbitrary
// number thought ought to be big enough. Note that if there is no underlying
// storage, we'll never produce any events in s.handleKV() so we can use a
// bufferSize of 0.
var bufferSize int
if s.storage != nil {
bufferSize = settings.MaxSettings * 3
}
var snapshot []roachpb.KeyValue // used with storage
maybeUpdateSnapshot := func(update rangefeedcache.Update) {
// Only record the update to the buffer if we're writing to storage.
if s.storage == nil ||
// and the update has some new information to write.
(update.Type == rangefeedcache.IncrementalUpdate && len(update.Events) == 0) {
return
}
eventKVs := rangefeedbuffer.EventsToKVs(update.Events,
rangefeedbuffer.RangeFeedValueEventToKV)
switch update.Type {
case rangefeedcache.CompleteUpdate:
snapshot = eventKVs
case rangefeedcache.IncrementalUpdate:
snapshot = rangefeedbuffer.MergeKVs(snapshot, eventKVs)
}
s.storage.SnapshotKVs(ctx, snapshot)
}
c := rangefeedcache.NewWatcher(
"settings-watcher",
s.clock, s.f,
bufferSize,
[]roachpb.Span{settingsTableSpan},
false, // withPrevValue
func(ctx context.Context, kv *roachpb.RangeFeedValue) rangefeedbuffer.Event {
return s.handleKV(ctx, kv)
},
func(ctx context.Context, update rangefeedcache.Update) {
noteUpdate(update)
maybeUpdateSnapshot(update)
},
s.testingWatcherKnobs,
)
// Kick off the rangefeedcache which will retry until the stopper stops.
if err := rangefeedcache.Start(ctx, s.stopper, c, func(err error) {
if !initialScan.done {
initialScan.err = err
initialScan.done = true
close(initialScan.ch)
} else {
s.resetUpdater()
}
}); err != nil {
return err // we're shutting down
}
// Wait for the initial scan before returning.
select {
case <-initialScan.ch:
return initialScan.err
case <-s.stopper.ShouldQuiesce():
return errors.Wrap(stop.ErrUnavailable, "failed to retrieve initial cluster settings")
case <-ctx.Done():
return errors.Wrap(ctx.Err(), "failed to retrieve initial cluster settings")
}
}
func (s *SettingsWatcher) handleKV(
ctx context.Context, kv *roachpb.RangeFeedValue,
) rangefeedbuffer.Event {
name, val, tombstone, err := s.dec.DecodeRow(roachpb.KeyValue{
Key: kv.Key,
Value: kv.Value,
})
if err != nil {
log.Warningf(ctx, "failed to decode settings row %v: %v", kv.Key, err)
return nil
}
if !s.codec.ForSystemTenant() {
setting, ok := settings.Lookup(name, settings.LookupForLocalAccess, s.codec.ForSystemTenant())
if !ok {
log.Warningf(ctx, "unknown setting %s, skipping update", redact.Safe(name))
return nil
}
if setting.Class() != settings.TenantWritable {
log.Warningf(ctx, "ignoring read-only setting %s", redact.Safe(name))
return nil
}
}
s.mu.Lock()
defer s.mu.Unlock()
_, hasOverride := s.mu.overrides[name]
if tombstone {
// This event corresponds to a deletion.
delete(s.mu.values, name)
if !hasOverride {
s.setDefaultLocked(ctx, name)
}
} else {
s.mu.values[name] = val
if !hasOverride {
s.setLocked(ctx, name, val)
}
}
if s.storage != nil {
return kv
}
return nil
}
const versionSettingKey = "version"
// set the current value of a setting.
func (s *SettingsWatcher) setLocked(ctx context.Context, key string, val settings.EncodedValue) {
// The system tenant (i.e. the KV layer) does not use the SettingsWatcher
// to propagate cluster version changes (it uses the BumpClusterVersion
// RPC). However, non-system tenants (i.e. SQL pods) (asynchronously) get
// word of the new cluster version below.
if key == versionSettingKey && !s.codec.ForSystemTenant() {
var v clusterversion.ClusterVersion
if err := protoutil.Unmarshal([]byte(val.Value), &v); err != nil {
log.Warningf(ctx, "failed to set cluster version: %v", err)
} else if err := s.settings.Version.SetActiveVersion(ctx, v); err != nil {
log.Warningf(ctx, "failed to set cluster version: %v", err)
} else {
log.Infof(ctx, "set cluster version to: %v", v)
}
return
}
if err := s.mu.updater.Set(ctx, key, val); err != nil {
log.Warningf(ctx, "failed to set setting %s to %s: %v", redact.Safe(key), val.Value, err)
}
}
// setDefaultLocked sets a setting to its default value.
func (s *SettingsWatcher) setDefaultLocked(ctx context.Context, key string) {
setting, ok := settings.Lookup(key, settings.LookupForLocalAccess, s.codec.ForSystemTenant())
if !ok {
log.Warningf(ctx, "failed to find setting %s, skipping update", redact.Safe(key))
return
}
ws, ok := setting.(settings.NonMaskedSetting)
if !ok {
log.Fatalf(ctx, "expected non-masked setting, got %T", s)
}
val := settings.EncodedValue{
Value: ws.EncodedDefault(),
Type: ws.Typ(),
}
s.setLocked(ctx, key, val)
}
// updateOverrides updates the overrides map and updates any settings
// accordingly.
func (s *SettingsWatcher) updateOverrides(ctx context.Context) {
newOverrides := s.overridesMonitor.Overrides()
s.mu.Lock()
defer s.mu.Unlock()
for key, val := range newOverrides {
if key == versionSettingKey {
log.Warningf(ctx, "ignoring attempt to override %s", key)
continue
}
if oldVal, hasExisting := s.mu.overrides[key]; hasExisting && oldVal == val {
// We already have the same override in place; ignore.
continue
}
// A new override was added or an existing override has changed.
s.mu.overrides[key] = val
s.setLocked(ctx, key, val)
}
// Clean up any overrides that were removed.
for key := range s.mu.overrides {
if _, ok := newOverrides[key]; !ok {
delete(s.mu.overrides, key)
// Reset the setting to the value in the settings table (or the default
// value).
if val, ok := s.mu.values[key]; ok {
s.setLocked(ctx, key, val)
} else {
s.setDefaultLocked(ctx, key)
}
}
}
}
func (s *SettingsWatcher) resetUpdater() {
s.mu.Lock()
defer s.mu.Unlock()
s.mu.updater = s.settings.MakeUpdater()
}
// SetTestingKnobs is used by tests to set testing knobs.
func (s *SettingsWatcher) SetTestingKnobs(knobs *rangefeedcache.TestingKnobs) {
s.testingWatcherKnobs = knobs
}
// IsOverridden implements cluster.OverridesInformer.
func (s *SettingsWatcher) IsOverridden(settingName string) bool {
s.mu.Lock()
defer s.mu.Unlock()
_, exists := s.mu.overrides[settingName]
return exists
}