-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathbatch.go
387 lines (349 loc) · 10.1 KB
/
batch.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
package rockscache
import (
"context"
"errors"
"fmt"
"math/rand"
"runtime/debug"
"sync"
"time"
"github.com/lithammer/shortuuid"
"github.com/redis/go-redis/v9"
)
var (
errNeedFetch = errors.New("need fetch")
errNeedAsyncFetch = errors.New("need async fetch")
)
func (c *Client) luaGetBatch(ctx context.Context, keys []string, owner string) ([]interface{}, error) {
res, err := callLua(ctx, c.rdb, getBatchScript, keys, []interface{}{now(), now() + int64(c.Options.LockExpire/time.Second), owner})
debugf("luaGetBatch return: %v, %v", res, err)
if err != nil {
return nil, err
}
return res.([]interface{}), nil
}
func (c *Client) luaSetBatch(ctx context.Context, keys []string, values []string, expires []int, owner string) error {
var vals = make([]interface{}, 0, 2+len(values))
vals = append(vals, owner)
for _, v := range values {
vals = append(vals, v)
}
for _, ex := range expires {
vals = append(vals, ex)
}
_, err := callLua(ctx, c.rdb, setBatchScript, keys, vals)
return err
}
func (c *Client) fetchBatch(ctx context.Context, keys []string, idxs []int, expire time.Duration, owner string, fn func(idxs []int) (map[int]string, error)) (map[int]string, error) {
defer func() {
if r := recover(); r != nil {
debug.PrintStack()
}
}()
data, err := fn(idxs)
if err != nil {
for _, idx := range idxs {
_ = c.UnlockForUpdate(ctx, keys[idx], owner)
}
return nil, err
}
if data == nil {
// incase data is nil
data = make(map[int]string)
}
var batchKeys []string
var batchValues []string
var batchExpires []int
for _, idx := range idxs {
v := data[idx]
ex := expire - c.Options.Delay - time.Duration(rand.Float64()*c.Options.RandomExpireAdjustment*float64(expire))
if v == "" {
if c.Options.EmptyExpire == 0 { // if empty expire is 0, then delete the key
_ = c.rdb.Del(ctx, keys[idx]).Err()
if err != nil {
debugf("batch: del failed key=%s err:%s", keys[idx], err.Error())
}
continue
}
ex = c.Options.EmptyExpire
data[idx] = v // incase idx not in data
}
batchKeys = append(batchKeys, keys[idx])
batchValues = append(batchValues, v)
batchExpires = append(batchExpires, int(ex/time.Second))
}
err = c.luaSetBatch(ctx, batchKeys, batchValues, batchExpires, owner)
if err != nil {
debugf("batch: luaSetBatch failed keys=%s err:%s", keys, err.Error())
}
return data, nil
}
func (c *Client) keysIdx(keys []string) (idxs []int) {
for i := range keys {
idxs = append(idxs, i)
}
return idxs
}
type pair struct {
idx int
data string
err error
}
func (c *Client) weakFetchBatch(ctx context.Context, keys []string, expire time.Duration, fn func(idxs []int) (map[int]string, error)) (map[int]string, error) {
debugf("batch: weakFetch keys=%+v", keys)
var result = make(map[int]string)
owner := shortuuid.New()
var toGet, toFetch, toFetchAsync []int
// read from redis without sleep
rs, err := c.luaGetBatch(ctx, keys, owner)
if err != nil {
return nil, err
}
for i, v := range rs {
r := v.([]interface{})
if r[0] == nil {
if r[1] == locked {
toFetch = append(toFetch, i)
} else {
toGet = append(toGet, i)
}
continue
}
if r[1] == locked {
toFetchAsync = append(toFetchAsync, i)
// fallthrough with old data
} // else new data
result[i] = r[0].(string)
}
if len(toFetchAsync) > 0 {
go func(idxs []int) {
debugf("batch weak: async fetch keys=%+v", keys)
_, _ = c.fetchBatch(ctx, keys, idxs, expire, owner, fn)
}(toFetchAsync)
toFetchAsync = toFetchAsync[:0] // reset toFetch
}
if len(toFetch) > 0 {
// batch fetch
fetched, err := c.fetchBatch(ctx, keys, toFetch, expire, owner, fn)
if err != nil {
return nil, err
}
for _, k := range toFetch {
result[k] = fetched[k]
}
toFetch = toFetch[:0] // reset toFetch
}
if len(toGet) > 0 {
// read from redis and sleep to wait
var wg sync.WaitGroup
var ch = make(chan pair, len(toGet))
for _, idx := range toGet {
wg.Add(1)
go func(i int) {
defer wg.Done()
r, err := c.luaGet(ctx, keys[i], owner)
for err == nil && r[0] == nil && r[1].(string) != locked {
debugf("batch weak: empty result for %s locked by other, so sleep %s", keys[i], c.Options.LockSleep.String())
select {
case <-ctx.Done():
ch <- pair{idx: i, err: ctx.Err()}
return
case <-time.After(c.Options.LockSleep):
// equal to time.Sleep(c.Options.LockSleep) but can be canceled
}
r, err = c.luaGet(ctx, keys[i], owner)
}
if err != nil {
ch <- pair{idx: i, data: "", err: err}
return
}
if r[1] != locked { // normal value
ch <- pair{idx: i, data: r[0].(string), err: nil}
return
}
if r[0] == nil {
ch <- pair{idx: i, data: "", err: errNeedFetch}
return
}
ch <- pair{idx: i, data: "", err: errNeedAsyncFetch}
}(idx)
}
wg.Wait()
close(ch)
for p := range ch {
if p.err != nil {
switch p.err {
case errNeedFetch:
toFetch = append(toFetch, p.idx)
continue
case errNeedAsyncFetch:
toFetchAsync = append(toFetchAsync, p.idx)
continue
default:
}
return nil, p.err
}
result[p.idx] = p.data
}
}
if len(toFetchAsync) > 0 {
go func(idxs []int) {
debugf("batch weak: async 2 fetch keys=%+v", keys)
_, _ = c.fetchBatch(ctx, keys, idxs, expire, owner, fn)
}(toFetchAsync)
}
if len(toFetch) > 0 {
// batch fetch
fetched, err := c.fetchBatch(ctx, keys, toFetch, expire, owner, fn)
if err != nil {
return nil, err
}
for _, k := range toFetch {
result[k] = fetched[k]
}
}
return result, nil
}
func (c *Client) strongFetchBatch(ctx context.Context, keys []string, expire time.Duration, fn func(idxs []int) (map[int]string, error)) (map[int]string, error) {
debugf("batch: strongFetch keys=%+v", keys)
var result = make(map[int]string)
owner := shortuuid.New()
var toGet, toFetch []int
// read from redis without sleep
rs, err := c.luaGetBatch(ctx, keys, owner)
if err != nil {
return nil, err
}
for i, v := range rs {
r := v.([]interface{})
if r[1] == nil { // normal value
result[i] = r[0].(string)
continue
}
if r[1] != locked { // locked by other
debugf("batch: locked by other, continue idx=%d", i)
toGet = append(toGet, i)
continue
}
// locked for fetch
toFetch = append(toFetch, i)
}
if len(toFetch) > 0 {
// batch fetch
fetched, err := c.fetchBatch(ctx, keys, toFetch, expire, owner, fn)
if err != nil {
return nil, err
}
for _, k := range toFetch {
result[k] = fetched[k]
}
toFetch = toFetch[:0] // reset toFetch
}
if len(toGet) > 0 {
// read from redis and sleep to wait
var wg sync.WaitGroup
var ch = make(chan pair, len(toGet))
for _, idx := range toGet {
wg.Add(1)
go func(i int) {
defer wg.Done()
r, err := c.luaGet(ctx, keys[i], owner)
for err == nil && r[1] != nil && r[1] != locked { // locked by other
debugf("batch: locked by other, so sleep %s", c.Options.LockSleep)
select {
case <-ctx.Done():
ch <- pair{idx: i, err: ctx.Err()}
return
case <-time.After(c.Options.LockSleep):
// equal to time.Sleep(c.Options.LockSleep) but can be canceled
}
r, err = c.luaGet(ctx, keys[i], owner)
}
if err != nil {
ch <- pair{idx: i, data: "", err: err}
return
}
if r[1] != locked { // normal value
ch <- pair{idx: i, data: r[0].(string), err: nil}
return
}
// locked for update
ch <- pair{idx: i, data: "", err: errNeedFetch}
}(idx)
}
wg.Wait()
close(ch)
for p := range ch {
if p.err != nil {
if p.err == errNeedFetch {
toFetch = append(toFetch, p.idx)
continue
}
return nil, p.err
}
result[p.idx] = p.data
}
}
if len(toFetch) > 0 {
// batch fetch
fetched, err := c.fetchBatch(ctx, keys, toFetch, expire, owner, fn)
if err != nil {
return nil, err
}
for _, k := range toFetch {
result[k] = fetched[k]
}
}
return result, nil
}
// FetchBatch returns a map with values indexed by index of keys list.
// 1. the first parameter is the keys list of the data
// 2. the second parameter is the data expiration time
// 3. the third parameter is the batch data fetch function which is called when the cache does not exist
// the parameter of the batch data fetch function is the index list of those keys
// missing in cache, which can be used to form a batch query for missing data.
// the return value of the batch data fetch function is a map, with key of the
// index and value of the corresponding data in form of string
func (c *Client) FetchBatch(keys []string, expire time.Duration, fn func(idxs []int) (map[int]string, error)) (map[int]string, error) {
return c.FetchBatch2(c.Options.Context, keys, expire, fn)
}
// FetchBatch2 is same with FetchBatch, except that a user defined context.Context can be provided.
func (c *Client) FetchBatch2(ctx context.Context, keys []string, expire time.Duration, fn func(idxs []int) (map[int]string, error)) (map[int]string, error) {
if c.Options.DisableCacheRead {
return fn(c.keysIdx(keys))
} else if c.Options.StrongConsistency {
return c.strongFetchBatch(ctx, keys, expire, fn)
}
return c.weakFetchBatch(ctx, keys, expire, fn)
}
// TagAsDeletedBatch a key list, the keys in list will expire after delay time.
func (c *Client) TagAsDeletedBatch(keys []string) error {
return c.TagAsDeletedBatch2(c.Options.Context, keys)
}
// TagAsDeletedBatch2 a key list, the keys in list will expire after delay time.
func (c *Client) TagAsDeletedBatch2(ctx context.Context, keys []string) error {
if c.Options.DisableCacheDelete {
return nil
}
debugf("batch deleting: keys=%v", keys)
luaFn := func(con redis.Scripter) error {
_, err := callLua(ctx, con, deleteBatchScript, keys, []interface{}{int64(c.Options.Delay / time.Second)})
return err
}
if c.Options.WaitReplicas > 0 {
err := luaFn(c.rdb)
cmd := redis.NewCmd(ctx, "WAIT", c.Options.WaitReplicas, c.Options.WaitReplicasTimeout)
if err == nil {
err = c.rdb.Process(ctx, cmd)
}
var replicas int
if err == nil {
replicas, err = cmd.Int()
}
if err == nil && replicas < c.Options.WaitReplicas {
err = fmt.Errorf("wait replicas %d failed. result replicas: %d", c.Options.WaitReplicas, replicas)
}
return err
}
return luaFn(c.rdb)
}