-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreader.go
153 lines (131 loc) · 3.3 KB
/
reader.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
package repli
import (
"context"
"time"
"github.com/redis/go-redis/v9"
log "github.com/sirupsen/logrus"
)
type ReadCommand struct {
Command string
Key string
TTL *redis.DurationCmd
Bytes *redis.StringCmd
}
type Reader struct {
readBatchSize int
readBatchLatency int
minTTL int
C chan *ReadCommand
reader *redis.Client
}
func NewReader(config *CommonConfig, readBatchSize, readBatchLatency int, minTTL int) *Reader {
return &Reader{
readBatchSize: readBatchSize,
readBatchLatency: readBatchLatency,
minTTL: minTTL,
C: make(chan *ReadCommand),
reader: config.Reader(),
}
}
func (r *Reader) Close() {
close(r.C)
r.reader.Close()
}
func (r *Reader) TTL(key string) {
r.C <- &ReadCommand{
Command: "TTL",
Key: key,
}
}
func (r *Reader) Dump(key string) {
r.C <- &ReadCommand{
Command: "DUMP",
Key: key,
}
}
func (r *Reader) Run(l *log.Entry, writer *Writer, metrics *Metrics) {
ctx := context.Background()
var commands []*ReadCommand
for {
select {
case cmd := <-r.C:
commands = append(commands, cmd)
if len(commands) < r.readBatchSize {
continue
}
case <-time.After(time.Millisecond * time.Duration(r.readBatchLatency)):
l.Debug("read batch timeout")
}
if len(commands) > 0 {
_, err := r.reader.Pipelined(ctx, func(pipe redis.Pipeliner) error {
for _, cmd := range commands {
switch cmd.Command {
case "TTL":
cmd.TTL = pipe.TTL(ctx, cmd.Key)
case "DUMP":
cmd.Bytes = pipe.Dump(ctx, cmd.Key)
cmd.TTL = pipe.PTTL(ctx, cmd.Key)
}
}
return nil
})
if err != nil && err.Error() != "redis: nil" {
l.WithFields(log.Fields{
"error": err,
}).Warn("failed to read full batch")
}
for _, cmd := range commands {
metrics.Queried()
switch cmd.Command {
case "TTL":
if cmd.TTL.Err() != nil {
l.WithFields(log.Fields{
"key": cmd.Key,
"error": cmd.TTL.Err(),
}).Error("failed to get TTL")
} else {
if cmd.TTL.Val() < 0 {
// Very unlikely since we have received _expire_ event
l.WithFields(log.Fields{
"key": cmd.Key,
}).Debug("never-expired key ignored")
continue
}
if cmd.TTL.Val().Seconds() < float64(r.minTTL) {
// No need to replicate expiring key
l.WithFields(log.Fields{
"key": cmd.Key,
}).Debug("expiring key ignored")
continue
}
ttl := time.Second * time.Duration(cmd.TTL.Val().Seconds()-(float64(r.minTTL-1)))
writer.Expire(cmd.Key, ttl)
}
case "DUMP":
dumped, err := cmd.Bytes.Bytes()
if err != nil && err.Error() == "redis: nil" {
l.WithFields(log.Fields{
"key": cmd.Key,
}).Debug("key already gone")
continue
}
if cmd.Bytes.Err() != nil || cmd.TTL.Err() != nil {
l.WithFields(log.Fields{
"key": cmd.Key,
"dumpCommandError": cmd.Bytes.Err(),
"pttlCommandError": cmd.TTL.Err(),
}).Error("failed to dump key")
} else {
ttl := time.Millisecond * time.Duration(cmd.TTL.Val().Milliseconds())
writer.Restore(cmd.Key, ttl, dumped)
}
}
}
commands = nil
}
if len(commands) == 0 {
cmd := <-r.C
commands = append(commands, cmd)
}
}
}