forked from kvtools/redis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
redis.go
762 lines (630 loc) · 18.9 KB
/
redis.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
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
// Package redis contains the Redis store implementation.
package redis
import (
"context"
"crypto/tls"
"errors"
"fmt"
"log"
"strings"
"time"
"github.com/kvtools/valkeyrie"
"github.com/kvtools/valkeyrie/store"
"github.com/redis/go-redis/v9"
)
// StoreName the name of the store.
const StoreName = "redis"
const (
noExpiration = time.Duration(0)
defaultLockTTL = 60 * time.Second
)
var (
// ErrMultipleEndpointsUnsupported is thrown when there are
// multiple endpoints specified for Redis.
ErrMultipleEndpointsUnsupported = errors.New("redis: does not support multiple endpoints")
// ErrAbortTryLock is thrown when a user stops trying to seek the lock
// by sending a signal to the stop chan,
// this is used to verify if the operation succeeded.
ErrAbortTryLock = errors.New("redis: lock operation aborted")
// ErrMasterSetMustBeProvided is thrown when Redis Sentinel is enabled
// and the MasterName option is undefined.
ErrMasterSetMustBeProvided = errors.New("master set name must be provided")
// ErrInvalidRoutesOptions is thrown when Redis Sentinel is enabled
// with RouteByLatency & RouteRandomly options without the ClusterClient.
ErrInvalidRoutesOptions = errors.New("RouteByLatency and RouteRandomly options are only allowed with the ClusterClient")
)
// registers Redis to Valkeyrie.
func init() {
valkeyrie.Register(StoreName, newStore)
}
// Config the Redis configuration.
type Config struct {
TLS *tls.Config
Username string
Password string
DB int
Sentinel *Sentinel
// PoolSize is the size of the connection pool per available CPU. By default, 10 connections.
// When more connections are needed, new connections will be allocated with a maximum of MaxActiveConns.
PoolSize int
// MaxActiveConns is the maximum number of connections allocated by the pool at a given time.
// When zero, there is no limit on the number of connections in the pool.
MaxActiveConns int
}
// Sentinel holds the Redis Sentinel configuration.
type Sentinel struct {
MasterName string
Username string
Password string
// ClusterClient indicates whether to use the NewFailoverClusterClient to build the client.
ClusterClient bool
// Allows routing read-only commands to the closest master or replica node.
// This option only works with NewFailoverClusterClient.
RouteByLatency bool
// Allows routing read-only commands to the random master or replica node.
// This option only works with NewFailoverClusterClient.
RouteRandomly bool
// Route all commands to replica read-only nodes.
ReplicaOnly bool
// Use replicas disconnected with master when cannot get connected replicas
// Now, this option only works in RandomReplicaAddr function.
UseDisconnectedReplicas bool
}
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 redis.UniversalClient
script *redis.Script
codec Codec
}
// New creates a new Redis client.
func New(ctx context.Context, endpoints []string, options *Config) (*Store, error) {
return NewWithCodec(ctx, endpoints, options, &RawCodec{})
}
// NewWithCodec creates a new Redis client with codec config.
func NewWithCodec(ctx context.Context, endpoints []string, options *Config, codec Codec) (*Store, error) {
client, err := newClient(endpoints, options)
if err != nil {
return nil, err
}
return makeStore(ctx, client, codec), nil
}
func newClient(endpoints []string, options *Config) (redis.UniversalClient, error) {
if options != nil && options.Sentinel != nil {
if options.Sentinel.MasterName == "" {
return nil, ErrMasterSetMustBeProvided
}
if !options.Sentinel.ClusterClient && (options.Sentinel.RouteByLatency || options.Sentinel.RouteRandomly) {
return nil, ErrInvalidRoutesOptions
}
cfg := &redis.FailoverOptions{
SentinelAddrs: endpoints,
SentinelUsername: options.Sentinel.Username,
SentinelPassword: options.Sentinel.Password,
MasterName: options.Sentinel.MasterName,
RouteByLatency: options.Sentinel.RouteByLatency,
RouteRandomly: options.Sentinel.RouteRandomly,
ReplicaOnly: options.Sentinel.ReplicaOnly,
UseDisconnectedReplicas: options.Sentinel.UseDisconnectedReplicas,
Username: options.Username,
Password: options.Password,
DB: options.DB,
DialTimeout: 5 * time.Second,
ReadTimeout: 30 * time.Second,
WriteTimeout: 30 * time.Second,
ContextTimeoutEnabled: true,
TLSConfig: options.TLS,
PoolSize: options.PoolSize,
MaxActiveConns: options.MaxActiveConns,
}
if options.Sentinel.ClusterClient {
return redis.NewFailoverClusterClient(cfg), nil
}
return redis.NewFailoverClient(cfg), nil
}
if len(endpoints) > 1 {
return nil, ErrMultipleEndpointsUnsupported
}
opt := &redis.Options{
Addr: endpoints[0],
DialTimeout: 5 * time.Second,
ReadTimeout: 30 * time.Second,
WriteTimeout: 30 * time.Second,
}
if options != nil {
opt.TLSConfig = options.TLS
opt.Username = options.Username
opt.Password = options.Password
opt.DB = options.DB
opt.PoolSize = options.PoolSize
opt.MaxActiveConns = options.MaxActiveConns
}
// TODO: use *redis.ClusterClient if we support multiple endpoints.
return redis.NewClient(opt), nil
}
func makeStore(ctx context.Context, client redis.UniversalClient, codec Codec) *Store {
// Listen to Keyspace events.
client.ConfigSet(ctx, "notify-keyspace-events", "KEA")
var c Codec = &JSONCodec{}
if codec != nil {
c = codec
}
return &Store{
client: client,
script: redis.NewScript(luaScript()),
codec: c,
}
}
// Put a value at the specified key.
func (r *Store) Put(ctx context.Context, key string, value []byte, opts *store.WriteOptions) error {
expirationAfter := noExpiration
if opts != nil && opts.TTL != 0 {
expirationAfter = opts.TTL
}
return r.setTTL(ctx, normalize(key), &store.KVPair{
Key: key,
Value: value,
LastIndex: sequenceNum(),
}, expirationAfter)
}
func (r *Store) setTTL(ctx context.Context, key string, val *store.KVPair, ttl time.Duration) error {
valStr, err := r.codec.Encode(val)
if err != nil {
return err
}
return r.client.Set(ctx, key, valStr, ttl).Err()
}
// Get a value given its key.
func (r *Store) Get(ctx context.Context, key string, _ *store.ReadOptions) (*store.KVPair, error) {
return r.get(ctx, normalize(key))
}
func (r *Store) get(ctx context.Context, key string) (*store.KVPair, error) {
reply, err := r.client.Get(ctx, key).Bytes()
if err != nil {
if errors.Is(err, redis.Nil) {
return nil, store.ErrKeyNotFound
}
return nil, err
}
val := store.KVPair{}
if err := r.codec.Decode(reply, &val); err != nil {
return nil, err
}
if val.Key == "" {
val.Key = key
}
return &val, nil
}
// Delete the value at the specified key.
func (r *Store) Delete(ctx context.Context, key string) error {
return r.client.Del(ctx, normalize(key)).Err()
}
// Exists verify if a Key exists in the store.
func (r *Store) Exists(ctx context.Context, key string, _ *store.ReadOptions) (bool, error) {
count, err := r.client.Exists(ctx, normalize(key)).Result()
return count != 0, err
}
// Watch for changes on a key.
// glitch: we use notified-then-retrieve to retrieve *store.KVPair.
// so the responses may sometimes inaccurate.
func (r *Store) Watch(ctx context.Context, key string, _ *store.ReadOptions) (<-chan *store.KVPair, error) {
watchCh := make(chan *store.KVPair)
nKey := normalize(key)
get := getter(func() (interface{}, error) {
pair, err := r.get(ctx, nKey)
if err != nil {
return nil, err
}
return pair, nil
})
push := pusher(func(v interface{}) {
if val, ok := v.(*store.KVPair); ok {
watchCh <- val
}
})
sub := newSubscribe(ctx, r.client, regexWatch(nKey, false))
go func(ctx context.Context, sub *subscribe, get getter, push pusher) {
defer func() {
close(watchCh)
_ = sub.Close()
}()
msgCh := sub.Receive(ctx)
if err := watchLoop(ctx, msgCh, get, push); err != nil {
log.Printf("watchLoop in Watch err: %v", err)
}
}(ctx, sub, get, push)
return watchCh, nil
}
// WatchTree watches for changes on child nodes under a given directory.
func (r *Store) WatchTree(ctx context.Context, directory string, _ *store.ReadOptions) (<-chan []*store.KVPair, error) {
watchCh := make(chan []*store.KVPair)
nKey := normalize(directory)
get := getter(func() (interface{}, error) {
pair, err := r.list(ctx, nKey)
if err != nil {
return nil, err
}
return pair, nil
})
push := pusher(func(v interface{}) {
if p, ok := v.([]*store.KVPair); ok {
watchCh <- p
}
})
sub := newSubscribe(ctx, r.client, regexWatch(nKey, true))
go func(ctx context.Context, sub *subscribe, get getter, push pusher) {
defer func() {
close(watchCh)
_ = sub.Close()
}()
msgCh := sub.Receive(ctx)
if err := watchLoop(ctx, msgCh, get, push); err != nil {
log.Printf("watchLoop in WatchTree err:%v\n", err)
}
}(ctx, sub, get, push)
return watchCh, nil
}
// NewLock creates a lock for a given key.
// The returned Locker is not held and must be acquired
// with `.Lock`. The Value is optional.
func (r *Store) NewLock(_ context.Context, key string, opts *store.LockOptions) (store.Locker, error) {
ttl := defaultLockTTL
var value []byte
if opts != nil {
if opts.TTL != 0 {
ttl = opts.TTL
}
if len(opts.Value) != 0 {
value = opts.Value
}
}
return &redisLock{
redis: r,
last: nil,
key: key,
value: value,
ttl: ttl,
unlockCh: make(chan struct{}),
}, nil
}
// List the content of a given prefix.
func (r *Store) List(ctx context.Context, directory string, _ *store.ReadOptions) ([]*store.KVPair, error) {
return r.list(ctx, normalize(directory))
}
func (r *Store) list(ctx context.Context, directory string) ([]*store.KVPair, error) {
regex := scanRegex(directory) // for all keyed with $directory.
allKeys, err := r.keys(ctx, regex)
if err != nil {
return nil, err
}
// TODO: need to handle when #key is too large.
return r.mget(ctx, directory, allKeys...)
}
func (r *Store) keys(ctx context.Context, regex string) ([]string, error) {
const (
startCursor = 0
endCursor = 0
defaultCount = 10
)
var allKeys []string
keys, nextCursor, err := r.client.Scan(ctx, startCursor, regex, defaultCount).Result()
if err != nil {
return nil, err
}
allKeys = append(allKeys, keys...)
for nextCursor != endCursor {
keys, nextCursor, err = r.client.Scan(ctx, nextCursor, regex, defaultCount).Result()
if err != nil {
return nil, err
}
allKeys = append(allKeys, keys...)
}
if len(allKeys) == 0 {
return nil, store.ErrKeyNotFound
}
return allKeys, nil
}
// mget values given their keys.
func (r *Store) mget(ctx context.Context, directory string, keys ...string) ([]*store.KVPair, error) {
replies, err := r.client.MGet(ctx, keys...).Result()
if err != nil {
return nil, err
}
var pairs []*store.KVPair
for i, reply := range replies {
var sreply string
if v, ok := reply.(string); ok {
sreply = v
}
if sreply == "" {
// empty reply.
continue
}
pair := &store.KVPair{}
if err := r.codec.Decode([]byte(sreply), pair); err != nil {
return nil, err
}
if pair.Key == "" {
pair.Key = keys[i]
}
if normalize(pair.Key) != directory {
pairs = append(pairs, pair)
}
}
return pairs, nil
}
// DeleteTree deletes a range of keys under a given directory.
// glitch: we list all available keys first and then delete them all
// it costs two operations on redis, so is not atomicity.
func (r *Store) DeleteTree(ctx context.Context, directory string) error {
regex := scanRegex(normalize(directory)) // for all keyed with $directory.
allKeys, err := r.keys(ctx, regex)
if err != nil {
return err
}
return r.client.Del(ctx, allKeys...).Err()
}
// AtomicPut is an atomic CAS operation on a single value.
// Pass previous = nil to create a new key.
// We introduced script on this page, so atomicity is guaranteed.
func (r *Store) AtomicPut(ctx context.Context, key string, value []byte, previous *store.KVPair, opts *store.WriteOptions) (bool, *store.KVPair, error) {
expirationAfter := noExpiration
if opts != nil && opts.TTL != 0 {
expirationAfter = opts.TTL
}
newKV := &store.KVPair{
Key: key,
Value: value,
LastIndex: sequenceNum(),
}
nKey := normalize(key)
// if previous == nil, set directly.
if previous == nil {
if err := r.setNX(ctx, nKey, newKV, expirationAfter); err != nil {
return false, nil, err
}
return true, newKV, nil
}
if err := r.cas(ctx, nKey, previous, newKV, formatSec(expirationAfter)); err != nil {
return false, nil, err
}
return true, newKV, nil
}
func (r *Store) setNX(ctx context.Context, key string, val *store.KVPair, expirationAfter time.Duration) error {
valBlob, err := r.codec.Encode(val)
if err != nil {
return err
}
if !r.client.SetNX(ctx, key, valBlob, expirationAfter).Val() {
return store.ErrKeyExists
}
return nil
}
func (r *Store) cas(ctx context.Context, key string, oldPair, newPair *store.KVPair, secInStr string) error {
newVal, err := r.codec.Encode(newPair)
if err != nil {
return err
}
oldVal, err := r.codec.Encode(oldPair)
if err != nil {
return err
}
return r.runScript(ctx, cmdCAS, key, oldVal, newVal, secInStr)
}
// AtomicDelete is an atomic delete operation on a single value
// the value will be deleted if previous matched the one stored in db.
func (r *Store) AtomicDelete(ctx context.Context, key string, previous *store.KVPair) (bool, error) {
if err := r.cad(ctx, normalize(key), previous); err != nil {
return false, err
}
return true, nil
}
func (r *Store) cad(ctx context.Context, key string, old *store.KVPair) error {
oldVal, err := r.codec.Encode(old)
if err != nil {
return err
}
return r.runScript(ctx, cmdCAD, key, oldVal)
}
// Close the store connection.
func (r *Store) Close() error {
return r.client.Close()
}
func (r *Store) runScript(ctx context.Context, args ...interface{}) error {
err := r.script.Run(ctx, r.client, nil, args...).Err()
if err != nil && strings.Contains(err.Error(), "redis: key is not found") {
return store.ErrKeyNotFound
}
if err != nil && strings.Contains(err.Error(), "redis: value has been changed") {
return store.ErrKeyModified
}
return err
}
func regexWatch(key string, withChildren bool) string {
if withChildren {
// For all database and keys with $key prefix.
return fmt.Sprintf("__keyspace*:%s*", key)
}
// For all database and keys with $key.
return fmt.Sprintf("__keyspace*:%s", key)
}
// getter defines a func type which retrieves data from remote storage.
type getter func() (interface{}, error)
// pusher defines a func type which pushes data blob into watch channel.
type pusher func(interface{})
func watchLoop(ctx context.Context, msgCh chan *redis.Message, get getter, push pusher) error {
// deliver the original data before we set up any events.
pair, err := get()
if err != nil && !errors.Is(err, store.ErrKeyNotFound) {
return err
}
if errors.Is(err, store.ErrKeyNotFound) {
pair = &store.KVPair{}
}
push(pair)
for m := range msgCh {
select {
case <-ctx.Done():
return ctx.Err()
default:
}
// retrieve and send back.
pair, err := get()
if err != nil && !errors.Is(err, store.ErrKeyNotFound) {
return err
}
// in case of watching a key that has been expired or deleted return and empty KV.
if errors.Is(err, store.ErrKeyNotFound) && (m.Payload == "expired" || m.Payload == "del") {
pair = &store.KVPair{}
}
push(pair)
}
return nil
}
type subscribe struct {
pubsub *redis.PubSub
closeCh chan struct{}
}
func newSubscribe(ctx context.Context, client redis.UniversalClient, regex string) *subscribe {
return &subscribe{
pubsub: client.PSubscribe(ctx, regex),
closeCh: make(chan struct{}),
}
}
func (s *subscribe) Close() error {
close(s.closeCh)
return s.pubsub.Close()
}
func (s *subscribe) Receive(ctx context.Context) chan *redis.Message {
msgCh := make(chan *redis.Message)
go s.receiveLoop(ctx, msgCh)
return msgCh
}
func (s *subscribe) receiveLoop(ctx context.Context, msgCh chan *redis.Message) {
defer close(msgCh)
for {
select {
case <-s.closeCh:
return
case <-ctx.Done():
return
default:
msg, err := s.pubsub.ReceiveMessage(ctx)
if err != nil {
return
}
if msg != nil {
msgCh <- msg
}
}
}
}
type redisLock struct {
redis *Store
last *store.KVPair
unlockCh chan struct{}
key string
value []byte
ttl time.Duration
}
func (l *redisLock) Lock(ctx context.Context) (<-chan struct{}, error) {
lockHeld := make(chan struct{})
success, err := l.tryLock(ctx, lockHeld)
if err != nil {
return nil, err
}
if success {
return lockHeld, nil
}
// wait for changes on the key.
watch, err := l.redis.Watch(ctx, l.key, nil)
if err != nil {
return nil, err
}
for {
select {
case <-ctx.Done():
return nil, ErrAbortTryLock
case <-watch:
success, err := l.tryLock(ctx, lockHeld)
if err != nil {
return nil, err
}
if success {
return lockHeld, nil
}
}
}
}
// tryLock return `true, nil` when it acquired and hold the lock
// and return `false, nil` when it can't lock now,
// and return `false, err` if any unexpected error happened underlying.
func (l *redisLock) tryLock(ctx context.Context, lockHeld chan struct{}) (bool, error) {
success, item, err := l.redis.AtomicPut(ctx, l.key, l.value, l.last, &store.WriteOptions{
TTL: l.ttl,
})
if success {
l.last = item
// keep holding.
go l.holdLock(ctx, lockHeld)
return true, nil
}
if errors.Is(err, store.ErrKeyNotFound) || errors.Is(err, store.ErrKeyModified) || errors.Is(err, store.ErrKeyExists) {
return false, nil
}
return false, err
}
func (l *redisLock) holdLock(ctx context.Context, lockHeld chan struct{}) {
defer close(lockHeld)
hold := func() error {
_, item, err := l.redis.AtomicPut(ctx, l.key, l.value, l.last, &store.WriteOptions{
TTL: l.ttl,
})
if err == nil {
l.last = item
}
return err
}
heartbeat := time.NewTicker(l.ttl / 3)
defer heartbeat.Stop()
for {
select {
case <-heartbeat.C:
if err := hold(); err != nil {
return
}
case <-l.unlockCh:
return
case <-ctx.Done():
return
}
}
}
func (l *redisLock) Unlock(ctx context.Context) error {
l.unlockCh <- struct{}{}
_, err := l.redis.AtomicDelete(ctx, l.key, l.last)
if err != nil {
return err
}
l.last = nil
return nil
}
func scanRegex(directory string) string {
return fmt.Sprintf("%s*", directory)
}
func normalize(key string) string {
return strings.TrimPrefix(key, "/")
}
func formatSec(dur time.Duration) string {
return fmt.Sprintf("%d", int(dur/time.Second))
}
func sequenceNum() uint64 {
// TODO: use uuid if we concerns collision probability of this number.
return uint64(time.Now().Nanosecond())
}