-
Notifications
You must be signed in to change notification settings - Fork 5
/
store.go
589 lines (510 loc) · 11.3 KB
/
store.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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
package drax
import (
"bytes"
"fmt"
"io"
"strings"
"sync"
"time"
"github.com/Sirupsen/logrus"
"github.com/cpuguy83/drax/api"
"github.com/cpuguy83/drax/api/client"
"github.com/cpuguy83/drax/rpc"
libkvstore "github.com/docker/libkv/store"
"github.com/hashicorp/raft"
)
var (
// ErrKeyNotFound - Key does not exist in the store
ErrKeyNotFound = libkvstore.ErrKeyNotFound
// ErrKeyModified - Key was modified during atomic operation
ErrKeyModified = libkvstore.ErrKeyModified
// ErrCallNotSupported - call is not supported
ErrCallNotSupported = libkvstore.ErrCallNotSupported
wgPool = sync.Pool{New: func() interface{} { return new(sync.WaitGroup) }}
)
type raftCluster interface {
IsLeader() bool
LeaderCh() <-chan interface{}
GetLeader() string
ShutdownCh() <-chan struct{}
Apply([]byte) error
}
type store struct {
mu sync.RWMutex
watchLock sync.RWMutex
ttlLock sync.Mutex
data *db
r raftCluster
dialer rpc.DialerFn
watches map[string]*watch
treeWatches map[string]*treeWatch
}
type watch struct {
watchers map[chan *libkvstore.KVPair]struct{}
sync.RWMutex
closed bool
timeout time.Duration
}
type treeWatch struct {
watchers map[chan []*libkvstore.KVPair]struct{}
sync.RWMutex
closed bool
timeout time.Duration
}
type ttl struct {
TTL time.Duration
CreateTime time.Time
CreateIndex uint64
}
type db struct {
KV map[string]*libkvstore.KVPair
TTLs map[string]*ttl
}
func newDB() *db {
return &db{KV: make(map[string]*libkvstore.KVPair), TTLs: make(map[string]*ttl)}
}
func (s *store) newClient() *client.Client {
leader := s.r.GetLeader()
return client.New(leader, defaultTimeout, s.dialer)
}
func newStore() *store {
return &store{data: newDB(), watches: make(map[string]*watch), treeWatches: make(map[string]*treeWatch)}
}
func (s *store) Get(key string) (*libkvstore.KVPair, error) {
if s.r.IsLeader() {
s.mu.RLock()
defer s.mu.RUnlock()
return s.get(key)
}
return s.newClient().Get(key)
}
func (s *store) get(key string) (*libkvstore.KVPair, error) {
kv, ok := s.data.KV[key]
if !ok {
return nil, ErrKeyNotFound
}
return kv, nil
}
func (s *store) Put(key string, value []byte, options *libkvstore.WriteOptions) error {
if !s.r.IsLeader() {
return s.newClient().Put(key, value, options)
}
s.mu.Lock()
defer s.mu.Unlock()
req := &api.Request{
Action: api.Put,
Key: key,
Value: value,
}
if options != nil {
req.TTL = options.TTL
}
return s.apply(req)
}
func (s *store) Delete(key string) error {
if !s.r.IsLeader() {
return s.newClient().Delete(key)
}
s.mu.Lock()
defer s.mu.Unlock()
return s.apply(&api.Request{
Action: api.Delete,
Key: key,
})
}
func (s *store) Exists(key string) (bool, error) {
if !s.r.IsLeader() {
return s.newClient().Exists(key)
}
s.mu.RLock()
defer s.mu.RUnlock()
_, ok := s.data.KV[key]
return ok, nil
}
func (s *store) List(prefix string) ([]*libkvstore.KVPair, error) {
if !s.r.IsLeader() {
return s.newClient().List(prefix)
}
s.mu.RLock()
defer s.mu.RUnlock()
var out []*libkvstore.KVPair
for k, v := range s.data.KV {
if !strings.HasPrefix(k, prefix) {
continue
}
var kv libkvstore.KVPair
kv = *v
out = append(out, &kv)
}
if len(out) == 0 {
return nil, ErrKeyNotFound
}
return out, nil
}
func (s *store) DeleteTree(dir string) error {
if !s.r.IsLeader() {
return s.newClient().DeleteTree(dir)
}
s.mu.Lock()
defer s.mu.Unlock()
return s.apply(&api.Request{
Action: api.DeleteTree,
Key: dir,
})
}
func (s *store) Watch(key string, stopCh <-chan struct{}) (<-chan *libkvstore.KVPair, error) {
if !s.r.IsLeader() {
return s.newClient().Watch(key, stopCh)
}
s.watchLock.Lock()
defer s.watchLock.Unlock()
waitStop := func(w *watch, stopCh <-chan struct{}, ch chan *libkvstore.KVPair) {
<-stopCh
w.Evict(ch)
}
if w, exists := s.watches[key]; exists {
ch := w.Subscribe()
go waitStop(w, stopCh, ch)
return ch, nil
}
w := &watch{watchers: make(map[chan *libkvstore.KVPair]struct{})}
ch := w.Subscribe()
s.watches[key] = w
go waitStop(w, stopCh, ch)
return ch, nil
}
func (s *store) WatchTree(dir string, stopCh <-chan struct{}) (<-chan []*libkvstore.KVPair, error) {
if !s.r.IsLeader() {
return s.newClient().WatchTree(dir, stopCh)
}
s.watchLock.Lock()
defer s.watchLock.Unlock()
waitStop := func(w *treeWatch, stopCh <-chan struct{}, ch chan []*libkvstore.KVPair) {
<-stopCh
w.Evict(ch)
}
if w, exists := s.treeWatches[dir]; exists {
ch := w.Subscribe()
go waitStop(w, stopCh, ch)
return ch, nil
}
w := &treeWatch{watchers: make(map[chan []*libkvstore.KVPair]struct{})}
s.treeWatches[dir] = w
ch := w.Subscribe()
go waitStop(w, stopCh, ch)
return ch, nil
}
func (s *store) NewLock(key string, options *libkvstore.LockOptions) (libkvstore.Locker, error) {
if !s.r.IsLeader() {
return s.newClient().NewLock(key, options)
}
return nil, ErrCallNotSupported
}
func (s *store) AtomicPut(key string, value []byte, previous *libkvstore.KVPair, options *libkvstore.WriteOptions) (bool, *libkvstore.KVPair, error) {
if !s.r.IsLeader() {
return s.newClient().AtomicPut(key, value, previous, options)
}
s.mu.Lock()
defer s.mu.Unlock()
kv, err := s.get(key)
if err != nil {
if previous != nil && err == ErrKeyNotFound {
return false, nil, ErrKeyModified
}
}
if previous != nil && kv.LastIndex != previous.LastIndex {
return false, nil, ErrKeyModified
}
req := &api.Request{
Action: api.Put,
Key: key,
Value: value,
}
if options != nil {
req.TTL = options.TTL
}
if err := s.apply(req); err != nil {
return false, nil, err
}
kv, err = s.get(key)
if err != nil {
return false, nil, libkvstore.ErrKeyNotFound
}
return true, kv, nil
}
func (s *store) AtomicDelete(key string, previous *libkvstore.KVPair) (bool, error) {
if !s.r.IsLeader() {
return s.newClient().AtomicDelete(key, previous)
}
s.mu.Lock()
defer s.mu.Unlock()
if previous == nil {
return false, libkvstore.ErrPreviousNotSpecified
}
kv, err := s.get(key)
if err != nil {
if err == ErrKeyModified {
return false, err
}
}
if kv.LastIndex != previous.LastIndex {
return false, ErrKeyModified
}
if err := s.apply(&api.Request{
Action: api.Delete,
Key: key,
}); err != nil {
return false, err
}
return true, nil
}
func (s *store) Close() {
return
}
func (s *store) apply(ax *api.Request) error {
buf := bytes.NewBuffer(nil)
if err := api.Encode(ax, buf); err != nil {
return err
}
return s.r.Apply(buf.Bytes())
}
func (s *store) waitLeader() {
leaderCh := s.r.LeaderCh()
logrus.Debug("store: waiting for leader")
var state raft.RaftState
for {
select {
case si := <-leaderCh:
state = si.(raft.RaftState)
case <-s.r.ShutdownCh():
return
}
if state != raft.Leader {
continue
}
logrus.Debug("store: handling leader")
s.handleLeader(leaderCh)
logrus.Debugf("store: waiting for leader")
}
}
func (s *store) handleLeader(leaderCh <-chan interface{}) {
for {
select {
case state := <-leaderCh:
if state != raft.Leader {
return
}
case <-s.r.ShutdownCh():
return
default:
}
time.Sleep(1 * time.Second)
s.ttlLock.Lock()
var keys []string
for k, ttl := range s.data.TTLs {
if ttlDue(ttl) {
keys = append(keys, k)
}
}
if len(keys) > 0 {
logrus.Debugf("reaping TTL's for %v", keys)
s.reapKeys(keys)
}
s.ttlLock.Unlock()
}
}
func (s *store) reapKeys(keys []string) {
if err := s.apply(&api.Request{
Action: reapKeys,
Args: keys,
}); err != nil {
logrus.Debugf("error reaping keys: %v", err)
}
}
func ttlDue(t *ttl) bool {
now := time.Now()
return now.After(t.CreateTime.Add(t.TTL))
}
type storeFSM store
func (s *storeFSM) Apply(l *raft.Log) interface{} {
var ax api.Request
if err := api.Decode(&ax, bytes.NewBuffer(l.Data)); err != nil {
return err
}
switch ax.Action {
case api.Delete:
delete(s.data.KV, ax.Key)
delete(s.data.TTLs, ax.Key)
s.closeWatches(ax.Key)
case api.Put:
kv := &libkvstore.KVPair{Key: ax.Key, Value: ax.Value, LastIndex: l.Index}
s.data.KV[ax.Key] = kv
if ax.TTL != 0 {
s.ttlLock.Lock()
s.data.TTLs[ax.Key] = &ttl{CreateTime: time.Now(), TTL: ax.TTL, CreateIndex: l.Index}
s.ttlLock.Unlock()
}
s.checkWatches(ax.Key, kv)
s.checkTreeWatches(ax.Key, []*libkvstore.KVPair{kv})
case api.DeleteTree:
for k := range s.data.KV {
if !strings.HasPrefix(k, ax.Key) {
continue
}
delete(s.data.KV, k)
delete(s.data.TTLs, k)
s.closeWatches(ax.Key)
}
case reapKeys:
s.mu.Lock()
for _, k := range ax.Args {
delete(s.data.KV, k)
delete(s.data.TTLs, k)
}
s.mu.Unlock()
default:
return fmt.Errorf("unknown api.Request")
}
return nil
}
func (s *storeFSM) Snapshot() (raft.FSMSnapshot, error) {
return s, nil
}
func (s *storeFSM) Restore(r io.ReadCloser) error {
defer r.Close()
s.data = newDB()
return api.Decode(s.data, r)
}
func (s *storeFSM) Persist(sink raft.SnapshotSink) error {
defer sink.Close()
s.mu.Lock()
defer s.mu.Unlock()
return api.Encode(s.data, sink)
}
func (*storeFSM) Release() {}
func (s *storeFSM) checkWatches(key string, kv *libkvstore.KVPair) {
s.watchLock.Lock()
w, exists := s.watches[key]
s.watchLock.Unlock()
if !exists {
return
}
if kv == nil {
w.Close()
return
}
w.Publish(kv)
}
func (s *storeFSM) checkTreeWatches(key string, kv []*libkvstore.KVPair) {
s.watchLock.Lock()
defer s.watchLock.Unlock()
for dir, w := range s.treeWatches {
if !strings.HasPrefix(key, dir) {
continue
}
go w.Publish(kv)
}
}
func (s *storeFSM) closeWatches(key string) {
s.watchLock.Lock()
if w, exists := s.watches[key]; exists {
w.Close()
delete(s.watches, key)
}
for dir, w := range s.treeWatches {
// key is dir
if dir == key {
delete(s.treeWatches, key)
w.Close()
continue
}
// dir is underneath key, recurrsive delete
if strings.HasPrefix(dir, key) {
delete(s.treeWatches, key)
w.Close()
}
}
s.watchLock.Unlock()
}
func (w *treeWatch) Subscribe() chan []*libkvstore.KVPair {
ch := make(chan []*libkvstore.KVPair, 1000)
w.Lock()
w.watchers[ch] = struct{}{}
w.Unlock()
return ch
}
func (w *treeWatch) Publish(kv []*libkvstore.KVPair) {
w.RLock()
defer w.RUnlock()
if len(w.watchers) == 0 {
return
}
wg := wgPool.Get().(*sync.WaitGroup)
wg.Add(len(w.watchers))
for s := range w.watchers {
go func(s chan []*libkvstore.KVPair) {
defer wg.Done()
select {
case <-time.After(w.timeout):
return
case s <- kv:
}
}(s)
}
wg.Wait()
}
func (w *treeWatch) Evict(s chan []*libkvstore.KVPair) {
w.Lock()
delete(w.watchers, s)
close(s)
w.Unlock()
}
func (w *watch) Subscribe() chan *libkvstore.KVPair {
ch := make(chan *libkvstore.KVPair, 1000)
w.Lock()
w.watchers[ch] = struct{}{}
w.Unlock()
return ch
}
func (w *watch) Publish(kv *libkvstore.KVPair) {
w.RLock()
defer w.RUnlock()
if len(w.watchers) == 0 {
return
}
wg := wgPool.Get().(*sync.WaitGroup)
wg.Add(len(w.watchers))
for s := range w.watchers {
go func(s chan *libkvstore.KVPair) {
defer wg.Done()
select {
case <-time.After(w.timeout):
return
case s <- kv:
}
}(s)
}
wg.Wait()
}
func (w *watch) Evict(s chan *libkvstore.KVPair) {
w.Lock()
delete(w.watchers, s)
close(s)
w.Unlock()
}
func (w *watch) Close() {
w.Lock()
defer w.Unlock()
for s := range w.watchers {
close(s)
delete(w.watchers, s)
}
}
func (w *treeWatch) Close() {
w.Lock()
defer w.Unlock()
for s := range w.watchers {
close(s)
delete(w.watchers, s)
}
}