-
Notifications
You must be signed in to change notification settings - Fork 0
/
yinstance.go
476 lines (440 loc) · 12.4 KB
/
yinstance.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
package ycache
import (
"context"
"fmt"
"math/rand"
"sort"
"strings"
"sync"
"sync/atomic"
"time"
)
var (
ErrLoadFuncEmpty = fmt.Errorf("request LoadFunc is nil")
ErrBatchLoadFuncEmpty = fmt.Errorf("request BatchLoadFunc is nil")
ErrConcurrentLoad = fmt.Errorf("concurrent load return nil")
)
type LoadFunc func(ctx context.Context, realKey string) ([]byte, error)
type BatchLoadFunc func(ctx context.Context, realKeys []string) (map[string][]byte, error)
type YInstance struct {
root string
name string
ttl int
factor int
random int
lc *loadControl
cacheList []ICache
errHandle ErrorHandleFunc
stat *YStat
}
func (yi *YInstance) Name() string {
return yi.name
}
func (yi *YInstance) Get(ctx context.Context, prefix string, realKey string, loadFn LoadFunc) (value []byte, err error) {
defer func() {
atomic.AddInt64(&yi.stat.TotalReqCount, 1)
if err != nil {
atomic.AddInt64(&yi.stat.TotalReqFailed, 1)
}
}()
cacheKey := yi.addPrefix(prefix, realKey)
for index := 0; index < len(yi.cacheList); index++ {
value, err = yi.getFromCache(ctx, index, cacheKey)
if err == nil {
for head := 0; head < index; head++ {
_ = yi.setToCache(ctx, head, cacheKey, value)
}
return value, nil
}
}
var done func()
done, value, err = yi.loadFromSource(ctx, prefix, realKey, loadFn)
if done != nil {
//when write to cache successfully, call done
defer done()
} else {
//value from channel not need to set cache
return value, err
}
if err != nil {
return nil, err
}
for index := 0; index < len(yi.cacheList); index++ {
_ = yi.setToCache(ctx, index, cacheKey, value)
}
return value, nil
}
func (yi *YInstance) BatchGet(ctx context.Context, prefix string, realKeys []string, batchLoadFn BatchLoadFunc) (kvs map[string][]byte, err error) {
defer func() {
atomic.AddInt64(&yi.stat.TotalReqCount, 1)
if err != nil {
atomic.AddInt64(&yi.stat.TotalReqFailed, 1)
}
}()
cacheKeyList := make([]string, 0)
keyMap := make(map[string]string)
for _, key := range realKeys {
cacheKey := yi.addPrefix(prefix, key)
cacheKeyList = append(cacheKeyList, cacheKey)
//record real key to request key
keyMap[cacheKey] = key
}
//data key is request key.
dataKvs := make(map[string][]byte)
for index := 0; index < len(yi.cacheList); index++ {
cacheKvs, err := yi.batchGetFromCache(ctx, index, cacheKeyList)
if err == nil {
//this level values not exist
if len(cacheKvs) == 0 {
continue
}
newCacheKvs := make(map[string][]byte)
for k, v := range cacheKvs {
newCacheKvs[k] = v
//note: transfer to request key
dataKvs[keyMap[k]] = v
}
for head := 0; head < index; head++ {
_ = yi.batchSetToCache(ctx, head, newCacheKvs)
}
if len(dataKvs) == len(realKeys) {
return dataKvs, nil
}
}
}
loadRealKeys := make([]string, 0)
for _, key := range realKeys {
if _, ok := dataKvs[key]; !ok {
loadRealKeys = append(loadRealKeys, key)
}
}
var done func()
done, loadRealKvs, err := yi.batchLoadFromSource(ctx, prefix, loadRealKeys, batchLoadFn)
if done != nil {
//when write to cache successfully, call done
defer done()
} else {
for key, value := range loadRealKvs {
dataKvs[key] = value
}
//value from channel not need to set cache
return dataKvs, err
}
if err != nil {
return nil, err
}
if len(loadRealKvs) == 0 {
return dataKvs, nil
}
loadCacheKvs := make(map[string][]byte)
for key, value := range loadRealKvs {
//add to data
dataKvs[key] = value
//to save new key
cacheKey := yi.addPrefix(prefix, key)
loadCacheKvs[cacheKey] = value
}
for index := 0; index < len(yi.cacheList); index++ {
_ = yi.batchSetToCache(ctx, index, loadCacheKvs)
}
return dataKvs, nil
}
func (yi *YInstance) Delete(ctx context.Context, prefix string, realKey string) (err error) {
defer func() {
atomic.AddInt64(&yi.stat.TotalReqCount, 1)
if err != nil {
atomic.AddInt64(&yi.stat.TotalReqFailed, 1)
}
}()
cacheKey := yi.addPrefix(prefix, realKey)
for index := len(yi.cacheList) - 1; index >= 0; index-- {
_ = yi.delToCache(ctx, index, cacheKey)
}
return nil
}
func (yi *YInstance) BatchDelete(ctx context.Context, prefix string, realKeys []string) (err error) {
defer func() {
atomic.AddInt64(&yi.stat.TotalReqCount, 1)
if err != nil {
atomic.AddInt64(&yi.stat.TotalReqFailed, 1)
}
}()
cacheKeyList := make([]string, 0)
for _, key := range realKeys {
cacheKey := yi.addPrefix(prefix, key)
cacheKeyList = append(cacheKeyList, cacheKey)
}
for index := len(yi.cacheList) - 1; index >= 0; index-- {
_ = yi.batchDelToCache(ctx, index, cacheKeyList)
}
return nil
}
type YStat struct {
TotalReqCount int64
TotalReqFailed int64
TotalLoadConcurrent int64
TotalLoadCount int64
TotalLoadFailed int64
CacheStats map[string]*CacheStat
}
type CacheStat struct {
ReqCount int64
ReqFailed int64
}
func (yi *YInstance) Stat() *YStat {
stat := &YStat{
TotalReqCount: atomic.LoadInt64(&yi.stat.TotalReqCount),
TotalReqFailed: atomic.LoadInt64(&yi.stat.TotalReqFailed),
TotalLoadConcurrent: atomic.LoadInt64(&yi.stat.TotalLoadConcurrent),
TotalLoadCount: atomic.LoadInt64(&yi.stat.TotalLoadCount),
TotalLoadFailed: atomic.LoadInt64(&yi.stat.TotalLoadFailed),
CacheStats: make(map[string]*CacheStat),
}
for name, cs := range yi.stat.CacheStats {
stat.CacheStats[name] = &CacheStat{
ReqCount: atomic.LoadInt64(&cs.ReqCount),
ReqFailed: atomic.LoadInt64(&cs.ReqFailed),
}
}
return stat
}
func (yi *YInstance) getFromCache(ctx context.Context, index int, cacheKey string) (value []byte, err error) {
cache := yi.cacheList[index]
defer func() {
atomic.AddInt64(&yi.stat.CacheStats[cache.Name()].ReqCount, 1)
if err != nil {
atomic.AddInt64(&yi.stat.CacheStats[cache.Name()].ReqFailed, 1)
yi.handleError(fmt.Sprintf("getFromCache(%s) failed", cache.Name()), err)
}
}()
return cache.Get(ctx, cacheKey)
}
func (yi *YInstance) batchGetFromCache(ctx context.Context, index int, cacheKeys []string) (kvs map[string][]byte, err error) {
cache := yi.cacheList[index]
defer func() {
atomic.AddInt64(&yi.stat.CacheStats[cache.Name()].ReqCount, 1)
if err != nil {
atomic.AddInt64(&yi.stat.CacheStats[cache.Name()].ReqFailed, 1)
yi.handleError(fmt.Sprintf("batchGetFromCache(%s) failed", cache.Name()), err)
}
}()
return cache.BatchGet(ctx, cacheKeys)
}
func (yi *YInstance) delToCache(ctx context.Context, index int, cacheKey string) (err error) {
cache := yi.cacheList[index]
defer func() {
atomic.AddInt64(&yi.stat.CacheStats[cache.Name()].ReqCount, 1)
if err != nil {
atomic.AddInt64(&yi.stat.CacheStats[cache.Name()].ReqFailed, 1)
yi.handleError(fmt.Sprintf("delToCache(%s) failed", cache.Name()), err)
}
}()
return cache.Del(ctx, cacheKey)
}
func (yi *YInstance) batchDelToCache(ctx context.Context, index int, cacheKeys []string) (err error) {
cache := yi.cacheList[index]
defer func() {
atomic.AddInt64(&yi.stat.CacheStats[cache.Name()].ReqCount, 1)
if err != nil {
atomic.AddInt64(&yi.stat.CacheStats[cache.Name()].ReqFailed, 1)
yi.handleError(fmt.Sprintf("batchDelToCache(%s) failed", cache.Name()), err)
}
}()
return cache.BatchDel(ctx, cacheKeys)
}
func (yi *YInstance) setToCache(ctx context.Context, index int, cacheKey string, value []byte) (err error) {
cache := yi.cacheList[index]
defer func() {
atomic.AddInt64(&yi.stat.CacheStats[cache.Name()].ReqCount, 1)
if err != nil {
atomic.AddInt64(&yi.stat.CacheStats[cache.Name()].ReqFailed, 1)
yi.handleError(fmt.Sprintf("setToCache(%s) failed", cache.Name()), err)
}
}()
ttl := yi.makeLevelTtl(index)
return cache.Set(ctx, cacheKey, value, ttl)
}
func (yi *YInstance) batchSetToCache(ctx context.Context, index int, cacheKvs map[string][]byte) (err error) {
cache := yi.cacheList[index]
defer func() {
atomic.AddInt64(&yi.stat.CacheStats[cache.Name()].ReqCount, 1)
if err != nil {
atomic.AddInt64(&yi.stat.CacheStats[cache.Name()].ReqFailed, 1)
yi.handleError(fmt.Sprintf("batchSetToCache(%s) failed", cache.Name()), err)
}
}()
ttl := yi.makeLevelTtl(index)
return cache.BatchSet(ctx, cacheKvs, ttl)
}
func (yi *YInstance) loadFromSource(ctx context.Context, prefix string, realKey string, loadFn LoadFunc) (done func(), value []byte, err error) {
if loadFn == nil {
return nil, nil, ErrLoadFuncEmpty
}
defer func() {
atomic.AddInt64(&yi.stat.TotalLoadConcurrent, 1)
}()
raceId := yi.addPrefix(prefix, realKey)
rh := yi.lc.get(raceId)
retVal, retErr, success := rh.request(func() (interface{}, error) {
lv, le := loadFn(ctx, realKey)
return lv, le
})
if success {
done = func() {
//add metric
atomic.AddInt64(&yi.stat.TotalLoadCount, 1)
if err != nil {
atomic.AddInt64(&yi.stat.TotalLoadFailed, 1)
yi.handleError("loadFromSource failed", err)
}
yi.lc.delete(raceId)
rh.wakeupWaiters(retVal, retErr)
}
}
if retVal != nil {
value = retVal.([]byte)
}
if retErr != nil {
err = retErr
}
return
}
func (yi *YInstance) batchLoadFromSource(ctx context.Context, prefix string, realKeys []string, batchLoadFn BatchLoadFunc) (done func(), kvs map[string][]byte, err error) {
if batchLoadFn == nil {
return nil, nil, ErrBatchLoadFuncEmpty
}
defer func() {
atomic.AddInt64(&yi.stat.TotalLoadConcurrent, 1)
}()
sort.Strings(realKeys)
raceId := yi.addPrefix(prefix, strings.Join(realKeys, ","))
rh := yi.lc.get(raceId)
retVal, retErr, success := rh.request(func() (interface{}, error) {
lv, le := batchLoadFn(ctx, realKeys)
return lv, le
})
if success {
done = func() {
//add metric
atomic.AddInt64(&yi.stat.TotalLoadCount, 1)
if err != nil {
atomic.AddInt64(&yi.stat.TotalLoadFailed, 1)
yi.handleError("batchLoadFromSource failed", err)
}
yi.lc.delete(raceId)
rh.wakeupWaiters(retVal, retErr)
}
}
if retVal != nil {
kvs = retVal.(map[string][]byte)
}
if retErr != nil {
err = retErr
}
return
}
func (yi *YInstance) addPrefix(prefix string, realKey string) string {
if prefix != "" {
return fmt.Sprintf("%s_%s_%s_%s", yi.root, yi.name, prefix, realKey)
} else {
return fmt.Sprintf("%s_%s_%s", yi.root, yi.name, realKey)
}
}
func (yi *YInstance) makeLevelTtl(index int) int {
ttl := yi.ttl * (1 + index*yi.factor)
if yi.random > 0 {
src := rand.NewSource(time.Now().UnixNano())
number := rand.New(src).Intn(yi.random)
ttl = ttl + number
}
return ttl
}
func (yi *YInstance) getBoundaryTtl() int {
boundaryTtl := yi.ttl * (1 + (len(yi.cacheList)-1)*yi.factor)
return boundaryTtl
}
func (yi *YInstance) handleError(desc string, err error) {
if yi.errHandle != nil {
yi.errHandle(fmt.Sprintf("[yinstance:%s] error:%s", yi.name, desc), err)
}
}
//loadControl for control all key request load func in concurrency condition
type loadControl struct {
mutex sync.Mutex
handlers map[string]*raceHandler
}
func (lc *loadControl) get(id string) *raceHandler {
lc.mutex.Lock()
defer lc.mutex.Unlock()
if item, ok := lc.handlers[id]; ok {
return item
}
item := &raceHandler{
result: make(chan *raceResult, 0),
}
lc.handlers[id] = item
return item
}
func (lc *loadControl) delete(id string) {
lc.mutex.Lock()
defer lc.mutex.Unlock()
if _, ok := lc.handlers[id]; ok {
delete(lc.handlers, id)
}
}
//raceHandler for handle the key in concurrent condition
type raceHandler struct {
count int64
result chan *raceResult
}
type raceResult struct {
value interface{}
err error
}
func (rh *raceHandler) request(fn func() (interface{}, error)) (value interface{}, err error, success bool) {
number := atomic.AddInt64(&rh.count, 1)
//only first request can call load func successfully
//others will wait for reply
if number == 1 {
value, err = fn()
success = true
} else {
ret := <-rh.result
if ret == nil {
err = ErrConcurrentLoad
} else {
value, err = ret.value, ret.err
}
}
return
}
func (rh *raceHandler) wakeupWaiters(value interface{}, err error) {
total := atomic.LoadInt64(&rh.count)
for total > 1 {
total--
rh.result <- &raceResult{
value: value,
err: err,
}
}
close(rh.result)
}
type InstanceOption func(yc *YCache, yi *YInstance) error
func WithInstanceOptionRandomTtl(random int) InstanceOption {
return func(yc *YCache, yi *YInstance) error {
yi.random = random
return nil
}
}
func WithInstanceOptionCacheTtl(ttl int) InstanceOption {
return func(yc *YCache, yi *YInstance) error {
yi.ttl = ttl
return nil
}
}
func WithInstanceOptionTtlFactor(factor int) InstanceOption {
return func(yc *YCache, yi *YInstance) error {
yi.factor = factor
return nil
}
}