/
updater.go
137 lines (119 loc) · 3.43 KB
/
updater.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
// Copyright 2017 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 settings
import (
"strconv"
"time"
"github.com/cockroachdb/errors"
)
// EncodeDuration encodes a duration in the format parseRaw expects.
func EncodeDuration(d time.Duration) string {
return d.String()
}
// EncodeBool encodes a bool in the format parseRaw expects.
func EncodeBool(b bool) string {
return strconv.FormatBool(b)
}
// EncodeInt encodes an int in the format parseRaw expects.
func EncodeInt(i int64) string {
return strconv.FormatInt(i, 10)
}
// EncodeFloat encodes a bool in the format parseRaw expects.
func EncodeFloat(f float64) string {
return strconv.FormatFloat(f, 'G', -1, 64)
}
type updater struct {
sv *Values
m map[string]struct{}
}
// 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 Updater interface {
Set(k, rawValue, valType string) error
ResetRemaining()
}
// A NoopUpdater ignores all updates.
type NoopUpdater struct{}
// Set implements Updater. It is a no-op.
func (u NoopUpdater) Set(_, _, _ string) error { return nil }
// ResetRemaining implements Updater. It is a no-op.
func (u NoopUpdater) ResetRemaining() {}
// NewUpdater makes an Updater.
func NewUpdater(sv *Values) Updater {
return updater{
m: make(map[string]struct{}, len(registry)),
sv: sv,
}
}
// Set attempts to parse and update a setting and notes that it was updated.
func (u updater) Set(key, rawValue string, vt string) error {
d, ok := registry[key]
if !ok {
if _, ok := retiredSettings[key]; ok {
return nil
}
// Likely a new setting this old node doesn't know about.
return errors.Errorf("unknown setting '%s'", key)
}
u.m[key] = struct{}{}
if expected := d.Typ(); vt != expected {
return errors.Errorf("setting '%s' defined as type %s, not %s", key, expected, vt)
}
switch setting := d.(type) {
case *StringSetting:
return setting.set(u.sv, rawValue)
case *BoolSetting:
b, err := strconv.ParseBool(rawValue)
if err != nil {
return err
}
setting.set(u.sv, b)
return nil
case numericSetting: // includes *EnumSetting
i, err := strconv.Atoi(rawValue)
if err != nil {
return err
}
return setting.set(u.sv, int64(i))
case *FloatSetting:
f, err := strconv.ParseFloat(rawValue, 64)
if err != nil {
return err
}
return setting.set(u.sv, f)
case *DurationSetting:
d, err := time.ParseDuration(rawValue)
if err != nil {
return err
}
return setting.set(u.sv, d)
case *DurationSettingWithExplicitUnit:
d, err := time.ParseDuration(rawValue)
if err != nil {
return err
}
return setting.set(u.sv, d)
case *StateMachineSetting:
return setting.set(u.sv, []byte(rawValue))
}
return nil
}
// ResetRemaining sets all settings not updated by the updater to their default values.
func (u updater) ResetRemaining() {
for k, v := range registry {
if _, ok := u.m[k]; !ok {
v.setToDefault(u.sv)
}
}
}