-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.go
375 lines (328 loc) · 8.04 KB
/
db.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
package fleet
import (
"bytes"
"context"
"io/fs"
"os"
"path/filepath"
"runtime"
"strings"
"github.com/KarpelesLab/goupd"
bolt "go.etcd.io/bbolt"
)
// DB is a local db with data versioned & copied across all members of the fleet through the DB endpoint
// each regular DB update is pushed to everyone
// each DB entry has a nanosecond timestamp, if multiple updates of one key are done at the same time they are all kept together
// any node can ask to replay changes done to the db since any point in time, including zero
// timestamp for keys are stored in 2x int64 (second, nanosecond), as bigendian when serialized
type DbWatchCallback func(string, []byte)
func (a *Agent) initDb() {
// Open the Bolt database located in the config directory
d, err := os.UserConfigDir()
if err != nil {
panic(err)
}
d = filepath.Join(d, goupd.PROJECT_NAME)
EnsureDir(d)
a.db, err = bolt.Open(filepath.Join(d, "fleet.db"), 0600, nil)
if err != nil {
panic(err)
}
}
// simple db get for program usage
func (a *Agent) DbGet(key string) ([]byte, error) {
v, err := a.dbSimpleGet([]byte("app"), []byte(key))
return v, err
}
// simple db set for program usage
func (a *Agent) DbSet(key string, value []byte) error {
return a.feedDbSetBC([]byte("app"), []byte(key), value, DbNow())
}
// DbWatch will trigger the cb function upon updates of the given key
// Special key "*" covers all keys (can only be one callback for a key)
func (a *Agent) DbWatch(key string, cb func(string, []byte)) {
a.dbWatchLock.Lock()
defer a.dbWatchLock.Unlock()
a.dbWatch[key] = cb
}
func (a *Agent) dbWatchTrigger(key string, val []byte) {
a.dbWatchLock.RLock()
cb1, ok1 := a.dbWatch[key]
cb2, ok2 := a.dbWatch["*"]
a.dbWatchLock.RUnlock()
if ok1 {
cb1(key, val)
}
if ok2 {
cb2(key, val)
}
}
func (a *Agent) feedDbSetBC(bucket, key, val []byte, v DbStamp) error {
if err := a.feedDbSet(bucket, key, val, v); err != nil {
return err
}
a.broadcastDbRecord(context.Background(), bucket, key, val, v)
return nil
}
func (a *Agent) needDbEntry(bucket, key []byte, v DbStamp) bool {
if string(bucket) == "local" || string(bucket) == "fleet" {
// bucket "local" cannot be replicated
return false
}
// compute global key (bucket + NUL + key)
fk := append(append(bucket, 0), key...)
// check version
curV, err := a.dbSimpleGet([]byte("version"), fk)
if err != nil {
return true // yes, need
}
var curVT DbStamp
err = curVT.UnmarshalBinary(curV)
if err != nil {
return false
}
// if "v" is after our version, we need it
return v.After(curVT)
}
func (a *Agent) feedDbSet(bucket, key, val []byte, v DbStamp) error {
if string(bucket) == "local" || string(bucket) == "fleet" {
// bucket "local" cannot be replicated
return nil
}
// compute global key (bucket + NUL + key)
fk := append(append(bucket, 0), key...)
// check version
curV, err := a.dbSimpleGet([]byte("version"), fk)
if err == nil && len(curV) > 0 {
// decode curV
var curVT DbStamp
err = curVT.UnmarshalBinary(curV)
if err != nil {
return err
}
// compare with v
if !v.After(curVT) {
// no need for update, we already have the latest version
return nil
}
}
// update
err = a.db.Update(func(tx *bolt.Tx) error {
vb, err := tx.CreateBucketIfNotExists([]byte("version"))
if err != nil {
return err
}
vl, err := tx.CreateBucketIfNotExists([]byte("vlog"))
if err != nil {
return err
}
b, err := tx.CreateBucketIfNotExists(bucket)
if err != nil {
return err
}
vBin, _ := v.MarshalBinary()
err = vb.Put(fk, vBin)
if err != nil {
return err
}
// remove old entries from vlog
c := vl.Cursor()
for k, v := c.First(); k != nil; k, v = c.Next() {
if bytes.Equal(v, fk) {
vl.Delete(k) // this delete has no reason to fail, and even if it does it's not really an issue
}
}
// add to vlog
err = vl.Put(append(vBin, fk...), fk)
if err != nil {
return err
}
return b.Put(key, val)
})
go a.dbWatchTrigger(string(key), val)
return err
}
func (a *Agent) dbGetVersion(bucket, key []byte) (val []byte, stamp DbStamp, err error) {
if string(bucket) == "local" || string(bucket) == "fleet" {
// bucket "local" cannot be replicated
err = fs.ErrNotExist
return
}
err = a.db.View(func(tx *bolt.Tx) error {
b := tx.Bucket(bucket)
if b == nil {
return os.ErrNotExist
}
v := b.Get(key)
if v == nil {
return os.ErrNotExist
}
val = make([]byte, len(v))
copy(val, v)
versionBucket := tx.Bucket([]byte("version"))
fk := append(append(bucket, 0), key...)
vers := versionBucket.Get(fk)
if v == nil {
// no stamp
return nil
}
return stamp.UnmarshalBinary(vers)
})
return
}
func (a *Agent) databasePacket() *PacketDbVersions {
p := &PacketDbVersions{}
if c, err := a.NewDbCursor([]byte("version")); err == nil {
// version global key (bucket + NUL + key)
defer c.Close()
k, v := c.First()
for {
if k == nil {
break
}
k2 := make([]byte, len(k))
copy(k2, k)
k3 := bytes.SplitN(k2, []byte{0}, 2)
if len(k3) == 2 {
if string(k3[0]) == "local" || string(k3[0]) == "fleet" {
continue
}
stamp := DbStamp{}
stamp.UnmarshalBinary(v)
p.Info = append(p.Info, &PacketDbVersionsEntry{Stamp: stamp, Bucket: k3[0], Key: k3[1]})
}
k, v = c.Next()
}
}
return p
}
// internal setter
func (a *Agent) dbSimpleSet(bucket, key, val []byte) error {
return a.db.Update(func(tx *bolt.Tx) error {
b, err := tx.CreateBucketIfNotExists(bucket)
if err != nil {
return err
}
return b.Put(key, val)
})
}
// internal delete
func (a *Agent) dbSimpleDel(bucket, key []byte) error {
return a.db.Update(func(tx *bolt.Tx) error {
b := tx.Bucket(bucket)
if b == nil {
return nil
}
return b.Delete(key)
})
}
// internal getter
func (a *Agent) dbSimpleGet(bucket, key []byte) (r []byte, err error) {
err = a.db.View(func(tx *bolt.Tx) error {
b := tx.Bucket(bucket)
if b == nil {
return os.ErrNotExist
}
v := b.Get(key)
if v == nil {
return os.ErrNotExist
}
r = make([]byte, len(v))
copy(r, v)
return nil
})
return
}
func (a *Agent) dbFleetGet(keyname string) ([]byte, error) {
// for example keyname="internal_key:jwt"
data, err := a.dbSimpleGet([]byte("fleet"), []byte(keyname))
if err == nil {
return data, nil
}
// attempt to locate file named a.b for key a:b
filename := strings.ReplaceAll(keyname, ":", ".")
if strings.HasSuffix(filename, ".crt") {
// replace with .pem
filename = strings.TrimSuffix(filename, ".crt") + ".pem"
}
err = a.getFile(filename, func(v []byte) error {
data = v
return a.dbSimpleSet([]byte("fleet"), []byte(keyname), v)
})
if err == nil {
return data, nil
}
return data, err
}
func (a *Agent) dbFleetDel(keyname string) error {
// for example keyname="internal_key:jwt"
return a.dbSimpleDel([]byte("fleet"), []byte(keyname))
}
type DbCursor struct {
tx *bolt.Tx
bucket *bolt.Bucket
cursor *bolt.Cursor
pfx []byte
}
func dbCursorFinalizer(c *DbCursor) {
c.tx.Rollback()
}
func (a *Agent) NewDbCursor(bucket []byte) (*DbCursor, error) {
// create a readonly tx and a cursor
tx, err := a.db.Begin(false)
if err != nil {
return nil, err
}
r := &DbCursor{tx: tx}
runtime.SetFinalizer(r, dbCursorFinalizer)
r.bucket = tx.Bucket(bucket)
if r.bucket == nil {
tx.Rollback()
return nil, os.ErrNotExist
}
r.cursor = r.bucket.Cursor()
return r, nil
}
func (c *DbCursor) Seek(pfx []byte) ([]byte, []byte) {
c.pfx = pfx
k, v := c.cursor.Seek(pfx)
if pfx == nil {
return k, v
}
if k == nil {
// couldn't seek
return nil, nil
}
if !bytes.HasPrefix(k, pfx) {
// key not found
return nil, nil
}
return k[len(pfx):], v
}
func (c *DbCursor) First() ([]byte, []byte) {
c.pfx = nil
return c.cursor.First()
}
func (c *DbCursor) Last() ([]byte, []byte) {
c.pfx = nil
return c.cursor.Last()
}
func (c *DbCursor) Next() ([]byte, []byte) {
k, v := c.cursor.Next()
if k == nil {
return nil, nil
}
if c.pfx != nil {
if !bytes.HasPrefix(k, c.pfx) {
return nil, nil
}
return k[len(c.pfx):], v
}
return k, v
}
func (c *DbCursor) Close() error {
return c.tx.Rollback()
}
func (a *Agent) shutdownDb() {
a.db.Close()
}