This repository has been archived by the owner on Aug 23, 2023. It is now read-only.
/
keycache.go
99 lines (88 loc) · 1.99 KB
/
keycache.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
package keycache
import (
"sync"
"time"
schema "github.com/grafana/metrictank/schema"
)
// KeyCache tracks for all orgs, which keys have been seen, and when was the last time
type KeyCache struct {
clearIdx int // which shard should be cleared next?
clearInterval time.Duration
sync.RWMutex
caches map[uint32]*Cache
}
// NewKeyCache creates a new KeyCache
// each clearInterval, all shards will be wiped
// (one at a time, spread out over the interval)
func NewKeyCache(clearInterval time.Duration) *KeyCache {
k := &KeyCache{
clearInterval: clearInterval / 256,
caches: make(map[uint32]*Cache),
}
go k.clear()
return k
}
// Touch marks the key as seen and returns whether it was seen before
// callers should assure that t >= ref and t-ref <= 42 hours
func (k *KeyCache) Touch(key schema.MKey) bool {
k.RLock()
cache, ok := k.caches[key.Org]
k.RUnlock()
// most likely this branch won't execute
if !ok {
k.Lock()
// check again in case another routine has just added it
cache, ok = k.caches[key.Org]
if !ok {
cache = NewCache()
k.caches[key.Org] = cache
}
k.Unlock()
}
return cache.Touch(key.Key)
}
// Len returns the size across all orgs
func (k *KeyCache) Len() int {
var sum int
k.RLock()
caches := make([]*Cache, 0, len(k.caches))
for _, c := range k.caches {
caches = append(caches, c)
}
k.RUnlock()
for _, c := range caches {
sum += c.Len()
}
return sum
}
// clear makes sure each org's cache is periodically cleared
func (k *KeyCache) clear() {
tick := time.NewTicker(k.clearInterval)
for range tick.C {
type target struct {
org uint32
cache *Cache
}
k.RLock()
targets := make([]target, 0, len(k.caches))
for org, c := range k.caches {
targets = append(targets, target{
org,
c,
})
}
k.RUnlock()
for _, t := range targets {
size := t.cache.Clear(k.clearIdx)
if size == 0 {
k.Lock()
delete(k.caches, t.org)
k.Unlock()
}
}
k.clearIdx += 1
if k.clearIdx == 256 {
k.clearIdx = 0
}
}
}