-
Notifications
You must be signed in to change notification settings - Fork 1
/
consul.go
583 lines (479 loc) · 14.7 KB
/
consul.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
// Package consul contains the Consul store implementation.
package consul
import (
"context"
"crypto/tls"
"errors"
"net/http"
"strings"
"time"
"github.com/hashicorp/consul/api"
"github.com/kvtools/valkeyrie"
"github.com/kvtools/valkeyrie/store"
)
// StoreName the name of the store.
const StoreName = "consul"
const (
// DefaultWatchWaitTime is how long we block for at a time to check if the watched key has changed.
// This affects the minimum time it takes to cancel a watch.
DefaultWatchWaitTime = 15 * time.Second
// RenewSessionRetryMax is the number of time we should try to renew the session before giving up and throwing an error.
RenewSessionRetryMax = 5
// MaxSessionDestroyAttempts is the maximum times we will try
// to explicitly destroy the session attached to a lock after
// the connectivity to the store has been lost.
MaxSessionDestroyAttempts = 5
// defaultLockTTL is the default ttl for the consul lock.
defaultLockTTL = 20 * time.Second
)
var (
// ErrMultipleEndpointsUnsupported is thrown when there are multiple endpoints specified for Consul.
ErrMultipleEndpointsUnsupported = errors.New("consul does not support multiple endpoints")
// ErrSessionRenew is thrown when the session can't be renewed because the Consul version does not support sessions.
ErrSessionRenew = errors.New("cannot set or renew session for ttl, unable to operate on sessions")
)
// registers Consul to Valkeyrie.
func init() {
valkeyrie.Register(StoreName, newStore)
}
// Config the Consul configuration.
type Config struct {
TLS *tls.Config
ConnectionTimeout time.Duration
Token string
Namespace string
}
func newStore(ctx context.Context, endpoints []string, options valkeyrie.Config) (store.Store, error) {
cfg, ok := options.(*Config)
if !ok && options != nil {
return nil, &store.InvalidConfigurationError{Store: StoreName, Config: options}
}
return New(ctx, endpoints, cfg)
}
// Store implements the store.Store interface.
type Store struct {
client *api.Client
}
// New creates a new Consul client.
func New(_ context.Context, endpoints []string, options *Config) (*Store, error) {
if len(endpoints) > 1 {
return nil, ErrMultipleEndpointsUnsupported
}
config := createConfig(endpoints, options)
// Creates a new client.
client, err := api.NewClient(config)
if err != nil {
return nil, err
}
return &Store{client: client}, nil
}
// Get the value at "key".
// Returns the last modified index to use in conjunction to CAS calls.
func (s *Store) Get(_ context.Context, key string, opts *store.ReadOptions) (*store.KVPair, error) {
options := &api.QueryOptions{
AllowStale: false,
RequireConsistent: true,
}
// Get options.
if opts != nil {
options.RequireConsistent = opts.Consistent
}
pair, meta, err := s.client.KV().Get(normalize(key), options)
if err != nil {
return nil, err
}
// If pair is nil then the key does not exist.
if pair == nil {
return nil, store.ErrKeyNotFound
}
return &store.KVPair{Key: pair.Key, Value: pair.Value, LastIndex: meta.LastIndex}, nil
}
// Put a value at "key".
func (s *Store) Put(_ context.Context, key string, value []byte, opts *store.WriteOptions) error {
p := &api.KVPair{
Key: normalize(key),
Value: value,
Flags: api.LockFlagValue,
}
if opts != nil && opts.TTL > 0 {
// Create or renew a session holding a TTL.
// Operations on sessions are not deterministic: creating or renewing a session can fail.
for retry := 1; retry <= RenewSessionRetryMax; retry++ {
err := s.renewSession(p, opts.TTL)
if err == nil {
break
}
if retry == RenewSessionRetryMax {
return ErrSessionRenew
}
}
}
_, err := s.client.KV().Put(p, nil)
return err
}
// Delete a value at "key".
func (s *Store) Delete(ctx context.Context, key string) error {
if _, err := s.Get(ctx, key, nil); err != nil {
return err
}
_, err := s.client.KV().Delete(normalize(key), nil)
return err
}
// Exists checks that the key exists inside the store.
func (s *Store) Exists(ctx context.Context, key string, opts *store.ReadOptions) (bool, error) {
_, err := s.Get(ctx, key, opts)
if err != nil {
if errors.Is(err, store.ErrKeyNotFound) {
return false, nil
}
return false, err
}
return true, nil
}
// List child nodes of a given directory.
func (s *Store) List(_ context.Context, directory string, opts *store.ReadOptions) ([]*store.KVPair, error) {
options := &api.QueryOptions{
AllowStale: false,
RequireConsistent: true,
}
if opts != nil && !opts.Consistent {
options.AllowStale = true
options.RequireConsistent = false
}
pairs, _, err := s.client.KV().List(normalize(directory), options)
if err != nil {
return nil, err
}
if len(pairs) == 0 {
return nil, store.ErrKeyNotFound
}
var kv []*store.KVPair
for _, pair := range pairs {
if pair.Key == directory {
continue
}
kv = append(kv, &store.KVPair{
Key: pair.Key,
Value: pair.Value,
LastIndex: pair.ModifyIndex,
})
}
return kv, nil
}
// DeleteTree deletes a range of keys under a given directory.
func (s *Store) DeleteTree(ctx context.Context, directory string) error {
if _, err := s.List(ctx, directory, nil); err != nil {
return err
}
_, err := s.client.KV().DeleteTree(normalize(directory), nil)
return err
}
// Watch for changes on a "key".
// It returns a channel that will receive changes or pass on errors.
// Upon creation, the current value will first be sent to the channel.
// Providing a non-nil stopCh can be used to stop watching.
func (s *Store) Watch(ctx context.Context, key string, _ *store.ReadOptions) (<-chan *store.KVPair, error) {
kv := s.client.KV()
watchCh := make(chan *store.KVPair)
go func() {
defer close(watchCh)
// Use a wait time in order to check if we should quit from time to time.
opts := &api.QueryOptions{WaitTime: DefaultWatchWaitTime}
for {
// Check if we should quit.
select {
case <-ctx.Done():
return
default:
}
// Get the key.
pair, meta, err := kv.Get(key, opts)
if err != nil {
return
}
// If LastIndex didn't change then it means `Get` returned
// because of the WaitTime and the key didn't change.
if opts.WaitIndex == meta.LastIndex {
continue
}
opts.WaitIndex = meta.LastIndex
// Return the value to the channel.
if pair != nil {
watchCh <- &store.KVPair{
Key: pair.Key,
Value: pair.Value,
LastIndex: pair.ModifyIndex,
}
}
}
}()
return watchCh, nil
}
// WatchTree watches for changes on a "directory".
// It returns a channel that will receive changes or pass on errors.
// Upon creating a watch, the current children values will be sent to the channel.
// Providing a non-nil stopCh can be used to stop watching.
func (s *Store) WatchTree(ctx context.Context, directory string, _ *store.ReadOptions) (<-chan []*store.KVPair, error) {
kv := s.client.KV()
watchCh := make(chan []*store.KVPair)
go func() {
defer close(watchCh)
// Use a wait time in order to check if we should quit from time to time.
opts := &api.QueryOptions{WaitTime: DefaultWatchWaitTime}
for {
// Check if we should quit.
select {
case <-ctx.Done():
return
default:
}
// Get all the children.
pairs, meta, err := kv.List(directory, opts)
if err != nil {
return
}
// If LastIndex didn't change then it means `Get` returned
// because of the WaitTime and the child keys didn't change.
if opts.WaitIndex == meta.LastIndex {
continue
}
opts.WaitIndex = meta.LastIndex
// Return children KV pairs to the channel.
var kvPairs []*store.KVPair
for _, pair := range pairs {
if pair.Key == directory {
continue
}
kvPairs = append(kvPairs, &store.KVPair{
Key: pair.Key,
Value: pair.Value,
LastIndex: pair.ModifyIndex,
})
}
watchCh <- kvPairs
}
}()
return watchCh, nil
}
// NewLock returns a handle to a lock struct which can be used to provide mutual exclusion on a key.
func (s *Store) NewLock(ctx context.Context, key string, opts *store.LockOptions) (store.Locker, error) {
ttl := defaultLockTTL
lockOpts := &api.LockOptions{
Key: normalize(key),
}
if opts != nil {
// Set optional TTL on Lock.
if opts.TTL != 0 {
ttl = opts.TTL
}
// Set optional value on Lock.
if opts.Value != nil {
lockOpts.Value = opts.Value
}
}
entry := &api.SessionEntry{
Behavior: api.SessionBehaviorRelease, // Release the lock when the session expires.
TTL: (ttl / 2).String(), // Consul multiplies the TTL by 2x.
LockDelay: 1 * time.Millisecond, // Virtually disable lock delay.
}
q := (&api.WriteOptions{}).WithContext(ctx)
// Create the key session.
session, _, err := s.client.Session().Create(entry, q)
if err != nil {
return nil, err
}
lock := &consulLock{}
// Place the session and renew chan on lock.
lockOpts.Session = session
if opts != nil {
lock.renewCh = opts.RenewLock
}
l, err := s.client.LockOpts(lockOpts)
if err != nil {
return nil, err
}
// Renew the session ttl lock periodically.
if opts != nil {
s.renewLockSession(entry.TTL, session, opts.RenewLock, q)
}
lock.lock = l
return lock, nil
}
// renewLockSession is used to renew a session Lock,
// it takes a stopRenew chan which is used to explicitly stop the session renew process.
// The renewal routine never stops until a signal is sent to this channel.
// If deleting the session fails because the connection to the store is lost,
// it keeps trying to delete the session periodically until it can contact the store,
// this ensures that the lock is not maintained indefinitely which ensures liveness
// over safety for the lock when the store becomes unavailable.
func (s *Store) renewLockSession(initialTTL string, id string, stopRenew chan struct{}, q *api.WriteOptions) {
ttl, err := time.ParseDuration(initialTTL)
if err != nil {
return
}
sessionDestroyAttempts := 0
go func() {
for {
select {
case <-time.After(ttl / 2):
entry, _, err := s.client.Session().Renew(id, q)
if err != nil {
// If an error occurs,
// continue until the session gets destroyed explicitly or the session ttl times out.
continue
}
if entry == nil {
return
}
// Handle the server updating the TTL.
ttl, _ = time.ParseDuration(entry.TTL)
case <-stopRenew:
// Attempt a session destroy.
_, err := s.client.Session().Destroy(id, q)
if err == nil {
return
}
// We cannot destroy the session because the store is unavailable,
// wait for the session renew period.
// Give up after 'MaxSessionDestroyAttempts'.
sessionDestroyAttempts++
if sessionDestroyAttempts >= MaxSessionDestroyAttempts {
return
}
time.Sleep(ttl / 2)
}
}
}()
}
// AtomicPut puts a value at "key" if the key has not been modified in the meantime,
// throws an error if this is the case.
func (s *Store) AtomicPut(ctx context.Context, key string, value []byte, previous *store.KVPair, _ *store.WriteOptions) (bool, *store.KVPair, error) {
p := &api.KVPair{Key: normalize(key), Value: value, Flags: api.LockFlagValue}
// Consul interprets ModifyIndex = 0 as new key.
p.ModifyIndex = 0
if previous != nil {
p.ModifyIndex = previous.LastIndex
}
ok, _, err := s.client.KV().CAS(p, nil)
if err != nil {
return false, nil, err
}
if !ok {
if previous == nil {
return false, nil, store.ErrKeyExists
}
return false, nil, store.ErrKeyModified
}
pair, err := s.Get(ctx, key, nil)
if err != nil {
return false, nil, err
}
return true, pair, nil
}
// AtomicDelete deletes a value at "key" if the key has not been modified in the meantime,
// throws an error if this is the case.
func (s *Store) AtomicDelete(ctx context.Context, key string, previous *store.KVPair) (bool, error) {
if previous == nil {
return false, store.ErrPreviousNotSpecified
}
p := &api.KVPair{Key: normalize(key), ModifyIndex: previous.LastIndex, Flags: api.LockFlagValue}
// Extra Get operation to check on the key.
_, err := s.Get(ctx, key, nil)
if errors.Is(err, store.ErrKeyNotFound) {
return false, err
}
if work, _, err := s.client.KV().DeleteCAS(p, nil); err != nil {
return false, err
} else if !work {
return false, store.ErrKeyModified
}
return true, nil
}
// Close closes the client connection.
func (s *Store) Close() error { return nil }
func (s *Store) renewSession(pair *api.KVPair, ttl time.Duration) error {
// Check if there is any previous session with an active TTL.
session, err := s.getActiveSession(pair.Key)
if err != nil {
return err
}
if session == "" {
entry := &api.SessionEntry{
Behavior: api.SessionBehaviorDelete, // Delete the key when the session expires.
TTL: (ttl / 2).String(), // Consul multiplies the TTL by 2x.
LockDelay: 1 * time.Millisecond, // Virtually disable lock delay.
}
// Create the key session.
session, _, err = s.client.Session().Create(entry, nil)
if err != nil {
return err
}
lockOpts := &api.LockOptions{
Key: pair.Key,
Session: session,
}
// Lock and ignore if lock is held.
// It's just a placeholder for the ephemeral behavior.
lock, _ := s.client.LockOpts(lockOpts)
if lock != nil {
_, _ = lock.Lock(nil)
}
}
_, _, err = s.client.Session().Renew(session, nil)
return err
}
// getActiveSession checks if the key already has a session attached.
func (s *Store) getActiveSession(key string) (string, error) {
pair, _, err := s.client.KV().Get(key, nil)
if err != nil {
return "", err
}
if pair == nil || pair.Session == "" {
return "", nil
}
return pair.Session, nil
}
func createConfig(endpoints []string, options *Config) *api.Config {
config := api.DefaultConfig()
config.HttpClient = http.DefaultClient
config.Address = endpoints[0]
if options != nil {
if options.TLS != nil {
config.HttpClient.Transport = &http.Transport{
TLSClientConfig: options.TLS,
}
config.Scheme = "https"
}
if options.ConnectionTimeout != 0 {
config.WaitTime = options.ConnectionTimeout
}
if options.Token != "" {
config.Token = options.Token
}
if options.Namespace != "" {
config.Namespace = options.Namespace
}
}
return config
}
// normalize the key for usage in Consul.
func normalize(key string) string {
return strings.TrimPrefix(key, "/")
}
type consulLock struct {
lock *api.Lock
renewCh chan struct{}
}
// Lock attempts to acquire the lock and blocks while doing so.
// It returns a channel that is closed if our lock is lost or if an error occurs.
func (l *consulLock) Lock(ctx context.Context) (<-chan struct{}, error) {
return l.lock.Lock(ctx.Done())
}
// Unlock the "key".
// Calling unlock while not holding the lock will throw an error.
func (l *consulLock) Unlock(_ context.Context) error {
if l.renewCh != nil {
close(l.renewCh)
}
return l.lock.Unlock()
}