-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsettings.go
430 lines (383 loc) · 12 KB
/
settings.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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
// Copyright 2012, 2013 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package state
import (
"fmt"
"sort"
"strings"
"github.com/juju/errors"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
"gopkg.in/mgo.v2/txn"
)
// See: http://docs.mongodb.org/manual/faq/developers/#faq-dollar-sign-escaping
// for why we're using those replacements.
const (
fullWidthDot = "\uff0e"
fullWidthDollar = "\uff04"
)
var (
escapeReplacer = strings.NewReplacer(".", fullWidthDot, "$", fullWidthDollar)
unescapeReplacer = strings.NewReplacer(fullWidthDot, ".", fullWidthDollar, "$")
)
const (
ItemAdded = iota
ItemModified
ItemDeleted
)
// ItemChange represents the change of an item in a settings.
type ItemChange struct {
Type int
Key string
OldValue interface{}
NewValue interface{}
}
// String returns the item change in a readable format.
func (ic *ItemChange) String() string {
switch ic.Type {
case ItemAdded:
return fmt.Sprintf("setting added: %v = %v", ic.Key, ic.NewValue)
case ItemModified:
return fmt.Sprintf("setting modified: %v = %v (was %v)",
ic.Key, ic.NewValue, ic.OldValue)
case ItemDeleted:
return fmt.Sprintf("setting deleted: %v (was %v)", ic.Key, ic.OldValue)
}
return fmt.Sprintf("unknown setting change type %d: %v = %v (was %v)",
ic.Type, ic.Key, ic.NewValue, ic.OldValue)
}
// itemChangeSlice contains a slice of item changes in a config node.
// It implements the sort interface to sort the items changes by key.
type itemChangeSlice []ItemChange
func (ics itemChangeSlice) Len() int { return len(ics) }
func (ics itemChangeSlice) Less(i, j int) bool { return ics[i].Key < ics[j].Key }
func (ics itemChangeSlice) Swap(i, j int) { ics[i], ics[j] = ics[j], ics[i] }
// A Settings manages changes to settings as a delta in memory and merges
// them back in the database when explicitly requested.
type Settings struct {
st *State
key string
// disk holds the values in the config node before
// any keys have been changed. It is reset on Read and Write
// operations.
disk map[string]interface{}
// cache holds the current values in the config node.
// The difference between disk and core
// determines the delta to be applied when Settings.Write
// is called.
core map[string]interface{}
txnRevno int64
}
// Keys returns the current keys in alphabetical order.
func (c *Settings) Keys() []string {
keys := []string{}
for key := range c.core {
keys = append(keys, key)
}
sort.Strings(keys)
return keys
}
// Get returns the value of key and whether it was found.
func (c *Settings) Get(key string) (value interface{}, found bool) {
value, found = c.core[key]
return
}
// Map returns all keys and values of the node.
func (c *Settings) Map() map[string]interface{} {
return copyMap(c.core, nil)
}
// Set sets key to value
func (c *Settings) Set(key string, value interface{}) {
c.core[key] = value
}
// Update sets multiple key/value pairs.
func (c *Settings) Update(kv map[string]interface{}) {
for key, value := range kv {
c.core[key] = value
}
}
// Delete removes key.
func (c *Settings) Delete(key string) {
delete(c.core, key)
}
// cacheKeys returns the keys of all caches as a key=>true map.
func cacheKeys(caches ...map[string]interface{}) map[string]bool {
keys := make(map[string]bool)
for _, cache := range caches {
for key := range cache {
keys[key] = true
}
}
return keys
}
// Write writes changes made to c back onto its node. Changes are written
// as a delta applied on top of the latest version of the node, to prevent
// overwriting unrelated changes made to the node since it was last read.
func (c *Settings) Write() ([]ItemChange, error) {
changes := []ItemChange{}
updates := bson.M{}
deletions := bson.M{}
for key := range cacheKeys(c.disk, c.core) {
old, ondisk := c.disk[key]
new, incore := c.core[key]
if new == old {
continue
}
var change ItemChange
escapedKey := escapeReplacer.Replace(key)
switch {
case incore && ondisk:
change = ItemChange{ItemModified, key, old, new}
updates[escapedKey] = new
case incore && !ondisk:
change = ItemChange{ItemAdded, key, nil, new}
updates[escapedKey] = new
case ondisk && !incore:
change = ItemChange{ItemDeleted, key, old, nil}
deletions[escapedKey] = 1
default:
panic("unreachable")
}
changes = append(changes, change)
}
if len(changes) == 0 {
return []ItemChange{}, nil
}
sort.Sort(itemChangeSlice(changes))
ops := []txn.Op{{
C: settingsC,
Id: c.st.docID(c.key),
Assert: txn.DocExists,
Update: setUnsetUpdate(updates, deletions),
}}
err := c.st.runTransaction(ops)
if err == txn.ErrAborted {
return nil, errors.NotFoundf("settings")
}
if err != nil {
return nil, fmt.Errorf("cannot write settings: %v", err)
}
c.disk = copyMap(c.core, nil)
return changes, nil
}
func newSettings(st *State, key string) *Settings {
return &Settings{
st: st,
key: key,
core: make(map[string]interface{}),
}
}
// cleanSettingsMap cleans the map of version, env-uuid and _id fields and
// also unescapes keys coming out of MongoDB.
func cleanSettingsMap(in map[string]interface{}) {
delete(in, "env-uuid")
delete(in, "_id")
delete(in, "txn-revno")
delete(in, "txn-queue")
replaceKeys(in, unescapeReplacer.Replace)
}
// replaceKeys will modify the provided map in place by replacing keys with
// their replacement if they have been modified.
func replaceKeys(m map[string]interface{}, replace func(string) string) {
for key, value := range m {
if newKey := replace(key); newKey != key {
delete(m, key)
m[newKey] = value
}
}
return
}
// copyMap copies the keys and values of one map into a new one. If replace
// is non-nil, for each old key k, the new key will be replace(k).
func copyMap(in map[string]interface{}, replace func(string) string) (out map[string]interface{}) {
out = make(map[string]interface{})
for key, value := range in {
if replace != nil {
key = replace(key)
}
out[key] = value
}
return
}
// Read (re)reads the node data into c.
func (c *Settings) Read() error {
config, txnRevno, err := readSettingsDoc(c.st, c.key)
if err == mgo.ErrNotFound {
c.disk = nil
c.core = make(map[string]interface{})
return errors.NotFoundf("settings")
}
if err != nil {
return fmt.Errorf("cannot read settings: %v", err)
}
c.txnRevno = txnRevno
c.disk = config
c.core = copyMap(config, nil)
return nil
}
// readSettingsDoc reads the settings with the given
// key. It returns the settings and the current rxnRevno.
func readSettingsDoc(st *State, key string) (map[string]interface{}, int64, error) {
settings, closer := st.getRawCollection(settingsC)
defer closer()
config := map[string]interface{}{}
err := settings.FindId(st.docID(key)).One(config)
// This is required to allow loading of environ settings before the
// environment UUID migration has been applied to the settings collection.
// Without this, an agent's version cannot be read, blocking the upgrade.
if err == mgo.ErrNotFound && key == environGlobalKey {
err := settings.FindId(environGlobalKey).One(config)
if err != nil {
return nil, 0, err
}
} else if err != nil {
return nil, 0, err
}
txnRevno := config["txn-revno"].(int64)
cleanSettingsMap(config)
return config, txnRevno, nil
}
// ReadSettings returns the settings for the given key.
func (st *State) ReadSettings(key string) (*Settings, error) {
return readSettings(st, key)
}
func (st *State) ReadLeadershipSettings(serviceId string) (*Settings, error) {
return readSettings(st, LeadershipSettingsDocId(serviceId))
}
// readSettings returns the Settings for key.
func readSettings(st *State, key string) (*Settings, error) {
s := newSettings(st, key)
if err := s.Read(); err != nil {
return nil, err
}
return s, nil
}
var errSettingsExist = fmt.Errorf("cannot overwrite existing settings")
func createSettingsOp(st *State, key string, values map[string]interface{}) txn.Op {
newValues := copyMap(values, escapeReplacer.Replace)
newValues["env-uuid"] = st.EnvironUUID()
return txn.Op{
C: settingsC,
Id: st.docID(key),
Assert: txn.DocMissing,
Insert: newValues,
}
}
// createSettings writes an initial config node.
func createSettings(st *State, key string, values map[string]interface{}) (*Settings, error) {
s := newSettings(st, key)
s.core = copyMap(values, nil)
ops := []txn.Op{createSettingsOp(st, key, values)}
err := s.st.runTransaction(ops)
if err == txn.ErrAborted {
return nil, errSettingsExist
}
if err != nil {
return nil, fmt.Errorf("cannot create settings: %v", err)
}
return s, nil
}
// removeSettings removes the Settings for key.
func removeSettings(st *State, key string) error {
settings, closer := st.getCollection(settingsC)
defer closer()
err := settings.RemoveId(key)
if err == mgo.ErrNotFound {
return errors.NotFoundf("settings")
}
return nil
}
// listSettings returns all the settings with the specified key prefix.
func listSettings(st *State, keyPrefix string) (map[string]map[string]interface{}, error) {
settings, closer := st.getRawCollection(settingsC)
defer closer()
var matchingSettings []map[string]interface{}
findExpr := fmt.Sprintf("^%s.*$", st.docID(keyPrefix))
if err := settings.Find(bson.D{{"_id", bson.D{{"$regex", findExpr}}}}).All(&matchingSettings); err != nil {
return nil, err
}
result := make(map[string]map[string]interface{})
for i := range matchingSettings {
id := matchingSettings[i]["_id"].(string)
cleanSettingsMap(matchingSettings[i])
result[st.localID(id)] = matchingSettings[i]
}
return result, nil
}
// replaceSettingsOp returns a txn.Op that deletes the document's contents and
// replaces it with the supplied values, and a function that should be called on
// txn failure to determine whether this operation failed (due to a concurrent
// settings change).
func replaceSettingsOp(st *State, key string, values map[string]interface{}) (txn.Op, func() (bool, error), error) {
s, err := readSettings(st, key)
if err != nil {
return txn.Op{}, nil, err
}
deletes := bson.M{}
for k := range s.disk {
if _, found := values[k]; !found {
deletes[escapeReplacer.Replace(k)] = 1
}
}
newValues := copyMap(values, escapeReplacer.Replace)
op := s.assertUnchangedOp()
op.Update = setUnsetUpdate(bson.M(newValues), deletes)
assertFailed := func() (bool, error) {
latest, err := readSettings(st, key)
if err != nil {
return false, err
}
return latest.txnRevno != s.txnRevno, nil
}
return op, assertFailed, nil
}
func (s *Settings) assertUnchangedOp() txn.Op {
return txn.Op{
C: settingsC,
Id: s.st.docID(s.key),
Assert: bson.D{{"txn-revno", s.txnRevno}},
}
}
// setUnsetUpdate returns a bson.D for use in
// a txn.Op's Update field, containing $set and
// $unset operators if the corresponding operands
// are non-empty.
func setUnsetUpdate(set, unset bson.M) bson.D {
var update bson.D
if len(set) > 0 {
update = append(update, bson.DocElem{"$set", set})
}
if len(unset) > 0 {
update = append(update, bson.DocElem{"$unset", unset})
}
return update
}
// StateSettings is used to expose various settings APIs outside of the state package.
type StateSettings struct {
st *State
}
// NewStateSettings creates a StateSettings from state.
func NewStateSettings(st *State) *StateSettings {
return &StateSettings{st}
}
// CreateSettings exposes createSettings on state for use outside the state package.
func (s *StateSettings) CreateSettings(key string, settings map[string]interface{}) error {
_, err := createSettings(s.st, key, settings)
return err
}
// ReadSettings exposes readSettings on state for use outside the state package.
func (s *StateSettings) ReadSettings(key string) (map[string]interface{}, error) {
if settings, err := readSettings(s.st, key); err != nil {
return nil, err
} else {
return settings.Map(), nil
}
}
// RemoveSettings exposes removeSettings on state for use outside the state package.
func (s *StateSettings) RemoveSettings(key string) error {
return removeSettings(s.st, key)
}
// ListSettings exposes listSettings on state for use outside the state package.
func (s *StateSettings) ListSettings(keyPrefix string) (map[string]map[string]interface{}, error) {
return listSettings(s.st, keyPrefix)
}