-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
kvlog.go
370 lines (295 loc) · 8.04 KB
/
kvlog.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
package kv
import (
"bytes"
"context"
"crypto/sha1"
"encoding/binary"
"encoding/json"
"fmt"
"time"
platform "github.com/influxdata/influxdb/v2"
"github.com/influxdata/influxdb/v2/kit/platform/errors"
)
var (
kvlogBucket = []byte("keyvaluelogv1")
kvlogIndex = []byte("keyvaluelogindexv1")
// ErrKeyValueLogBoundsNotFound is returned when oplog entries cannot be located
// for the provided bounds
ErrKeyValueLogBoundsNotFound = &errors.Error{
Code: errors.ENotFound,
Msg: "oplog not found",
}
)
var _ platform.KeyValueLog = (*Service)(nil)
type keyValueLogBounds struct {
Start int64 `json:"start"`
Stop int64 `json:"stop"`
}
func newKeyValueLogBounds(now time.Time) *keyValueLogBounds {
return &keyValueLogBounds{
Start: now.UTC().UnixNano(),
Stop: now.UTC().UnixNano(),
}
}
func (b *keyValueLogBounds) update(t time.Time) {
now := t.UTC().UnixNano()
if now < b.Start {
b.Start = now
} else if b.Stop < now {
b.Stop = now
}
}
// StartTime retrieves the start value of a bounds as a time.Time
func (b *keyValueLogBounds) StartTime() time.Time {
return time.Unix(0, b.Start)
}
// StopTime retrieves the stop value of a bounds as a time.Time
func (b *keyValueLogBounds) StopTime() time.Time {
return time.Unix(0, b.Stop)
}
// Bounds returns the key boundaries for the keyvaluelog for a resourceType/resourceID pair.
func (b *keyValueLogBounds) Bounds(k []byte) ([]byte, []byte, error) {
start, err := encodeLogEntryKey(k, b.Start)
if err != nil {
return nil, nil, err
}
stop, err := encodeLogEntryKey(k, b.Stop)
if err != nil {
return nil, nil, err
}
return start, stop, nil
}
func encodeLogEntryKey(key []byte, v int64) ([]byte, error) {
prefix := encodeKeyValueIndexKey(key)
k := make([]byte, len(prefix)+8)
buf := bytes.NewBuffer(k)
_, err := buf.Write(prefix)
if err != nil {
return nil, err
}
// This needs to be big-endian so that the iteration order is preserved when scanning keys
if err := binary.Write(buf, binary.BigEndian, v); err != nil {
return nil, err
}
return buf.Bytes(), err
}
func decodeLogEntryKey(key []byte) ([]byte, time.Time, error) {
buf := bytes.NewReader(key[len(key)-8:])
var ts int64
// This needs to be big-endian so that the iteration order is preserved when scanning keys
err := binary.Read(buf, binary.BigEndian, &ts)
if err != nil {
return nil, time.Unix(0, 0), err
}
return key[:len(key)-8], time.Unix(0, ts), nil
}
func encodeKeyValueIndexKey(k []byte) []byte {
// keys produced must be fixed length to ensure that we can iterate through the keyspace without any error.
h := sha1.New()
h.Write([]byte(k))
return h.Sum(nil)
}
func (s *Service) getKeyValueLogBounds(ctx context.Context, tx Tx, key []byte) (*keyValueLogBounds, error) {
k := encodeKeyValueIndexKey(key)
b, err := tx.Bucket(kvlogIndex)
if err != nil {
return nil, err
}
v, err := b.Get(k)
if IsNotFound(err) {
return nil, ErrKeyValueLogBoundsNotFound
}
if err != nil {
return nil, err
}
bounds := &keyValueLogBounds{}
if err := json.Unmarshal(v, bounds); err != nil {
return nil, err
}
return bounds, nil
}
func (s *Service) putKeyValueLogBounds(ctx context.Context, tx Tx, key []byte, bounds *keyValueLogBounds) error {
k := encodeKeyValueIndexKey(key)
v, err := json.Marshal(bounds)
if err != nil {
return err
}
b, err := tx.Bucket(kvlogIndex)
if err != nil {
return err
}
if err := b.Put(k, v); err != nil {
return err
}
return nil
}
func (s *Service) updateKeyValueLogBounds(ctx context.Context, tx Tx, k []byte, t time.Time) error {
// retrieve the keyValue log boundaries
bounds, err := s.getKeyValueLogBounds(ctx, tx, k)
if err != nil && err != ErrKeyValueLogBoundsNotFound {
return err
}
if err == ErrKeyValueLogBoundsNotFound {
// if the bounds don't exist yet, create them
bounds = newKeyValueLogBounds(t)
}
// update the bounds to if needed
bounds.update(t)
if err := s.putKeyValueLogBounds(ctx, tx, k, bounds); err != nil {
return err
}
return nil
}
// ForEachLogEntry retrieves the keyValue log for a resource type ID combination. KeyValues may be returned in ascending and descending order.
func (s *Service) ForEachLogEntry(ctx context.Context, k []byte, opts platform.FindOptions, fn func([]byte, time.Time) error) error {
return s.kv.View(ctx, func(tx Tx) error {
return s.ForEachLogEntryTx(ctx, tx, k, opts, fn)
})
}
func (s *Service) ForEachLogEntryTx(ctx context.Context, tx Tx, k []byte, opts platform.FindOptions, fn func([]byte, time.Time) error) error {
b, err := s.getKeyValueLogBounds(ctx, tx, k)
if err != nil {
return err
}
bkt, err := tx.Bucket(kvlogBucket)
if err != nil {
return err
}
startKey, stopKey, err := b.Bounds(k)
if err != nil {
return err
}
direction := CursorAscending
if opts.Descending {
direction = CursorDescending
startKey, stopKey = stopKey, startKey
}
cur, err := bkt.ForwardCursor(startKey, WithCursorDirection(direction))
if err != nil {
return err
}
count := 0
if opts.Offset > 0 {
// Skip offset many items
for i := 0; i < opts.Offset; i++ {
k, _ := cur.Next()
if bytes.Equal(k, stopKey) {
return nil
}
}
}
for k, v := cur.Next(); k != nil; k, v = cur.Next() {
if count >= opts.Limit && opts.Limit != 0 {
break
}
_, ts, err := decodeLogEntryKey(k)
if err != nil {
return err
}
if err := fn(v, ts); err != nil {
return err
}
if bytes.Equal(k, stopKey) {
// if we've reached the stop key, there are no keys log entries left
// in the keyspace.
break
}
count++
}
return nil
}
// AddLogEntry logs an keyValue for a particular resource type ID pairing.
func (s *Service) AddLogEntry(ctx context.Context, k, v []byte, t time.Time) error {
return s.kv.Update(ctx, func(tx Tx) error {
return s.AddLogEntryTx(ctx, tx, k, v, t)
})
}
func (s *Service) AddLogEntryTx(ctx context.Context, tx Tx, k, v []byte, t time.Time) error {
if err := s.updateKeyValueLogBounds(ctx, tx, k, t); err != nil {
return err
}
if err := s.putLogEntry(ctx, tx, k, v, t); err != nil {
return err
}
return nil
}
func (s *Service) putLogEntry(ctx context.Context, tx Tx, k, v []byte, t time.Time) error {
key, err := encodeLogEntryKey(k, t.UTC().UnixNano())
if err != nil {
return err
}
b, err := tx.Bucket(kvlogBucket)
if err != nil {
return err
}
if err := b.Put(key, v); err != nil {
return err
}
return nil
}
func (s *Service) getLogEntry(ctx context.Context, tx Tx, k []byte, t time.Time) ([]byte, time.Time, error) {
key, err := encodeLogEntryKey(k, t.UTC().UnixNano())
if err != nil {
return nil, t, err
}
b, err := tx.Bucket(kvlogBucket)
if err != nil {
return nil, t, err
}
v, err := b.Get(key)
if IsNotFound(err) {
return nil, t, fmt.Errorf("log entry not found")
}
if err != nil {
return nil, t, err
}
return v, t, nil
}
// FirstLogEntry retrieves the first log entry for a key value log.
func (s *Service) FirstLogEntry(ctx context.Context, k []byte) ([]byte, time.Time, error) {
var v []byte
var t time.Time
err := s.kv.View(ctx, func(tx Tx) error {
val, ts, err := s.firstLogEntry(ctx, tx, k)
if err != nil {
return err
}
v, t = val, ts
return nil
})
if err != nil {
return nil, t, err
}
return v, t, nil
}
// LastLogEntry retrieves the first log entry for a key value log.
func (s *Service) LastLogEntry(ctx context.Context, k []byte) ([]byte, time.Time, error) {
var v []byte
var t time.Time
err := s.kv.View(ctx, func(tx Tx) error {
val, ts, err := s.lastLogEntry(ctx, tx, k)
if err != nil {
return err
}
v, t = val, ts
return nil
})
if err != nil {
return nil, t, err
}
return v, t, nil
}
func (s *Service) firstLogEntry(ctx context.Context, tx Tx, k []byte) ([]byte, time.Time, error) {
bounds, err := s.getKeyValueLogBounds(ctx, tx, k)
if err != nil {
return nil, time.Time{}, err
}
return s.getLogEntry(ctx, tx, k, bounds.StartTime())
}
func (s *Service) lastLogEntry(ctx context.Context, tx Tx, k []byte) ([]byte, time.Time, error) {
bounds, err := s.getKeyValueLogBounds(ctx, tx, k)
if err != nil {
return nil, time.Time{}, err
}
return s.getLogEntry(ctx, tx, k, bounds.StopTime())
}