-
Notifications
You must be signed in to change notification settings - Fork 374
/
policy.go
385 lines (350 loc) · 9.12 KB
/
policy.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
/*
* Copyright 2019 Dgraph Labs, Inc. and Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ristretto
import (
"container/list"
"math"
"sync"
"time"
"github.com/dgraph-io/ristretto/z"
)
const (
// lfuSample is the number of items to sample when looking at eviction
// candidates. 5 seems to be the most optimal number [citation needed].
lfuSample = 5
)
// policy is the interface encapsulating eviction/admission behavior.
//
// TODO: remove this interface and just rename defaultPolicy to policy, as we
// are probably only going to use/implement/maintain one policy.
type policy interface {
ringConsumer
// Add attempts to Add the key-cost pair to the Policy. It returns a slice
// of evicted keys and a bool denoting whether or not the key-cost pair
// was added. If it returns true, the key should be stored in cache.
Add(uint64, int64, int64) ([]*item, bool)
// Has returns true if the key exists in the Policy.
Has(uint64) bool
// Del deletes the key from the Policy.
Del(uint64)
// Cap returns the available capacity.
Cap() int64
// Close stops all goroutines and closes all channels.
Close()
// Update updates the cost value for the key.
Update(uint64, int64)
// Cost returns the cost value of a key or -1 if missing.
Cost(uint64) int64
// Optionally, set stats object to track how policy is performing.
CollectMetrics(*Metrics)
// Clear zeroes out all counters and clears hashmaps.
Clear()
}
func newPolicy(numCounters, maxCost int64) policy {
return newDefaultPolicy(numCounters, maxCost)
}
type defaultPolicy struct {
sync.Mutex
admit *tinyLFU
evict *sampledLFU
itemsCh chan []uint64
stop chan struct{}
metrics *Metrics
times *list.List
}
func newDefaultPolicy(numCounters, maxCost int64) *defaultPolicy {
p := &defaultPolicy{
admit: newTinyLFU(numCounters),
evict: newSampledLFU(maxCost),
itemsCh: make(chan []uint64, 3),
stop: make(chan struct{}),
times: list.New(),
}
go p.processItems()
return p
}
func (p *defaultPolicy) CollectMetrics(metrics *Metrics) {
p.metrics = metrics
p.evict.metrics = metrics
}
type policyPair struct {
key uint64
cost int64
}
func (p *defaultPolicy) processItems() {
for {
select {
case items := <-p.itemsCh:
p.Lock()
p.admit.Push(items)
p.Unlock()
case <-p.stop:
return
}
}
}
func (p *defaultPolicy) Push(keys []uint64) bool {
if len(keys) == 0 {
return true
}
select {
case p.itemsCh <- keys:
p.metrics.add(keepGets, keys[0], uint64(len(keys)))
return true
default:
p.metrics.add(dropGets, keys[0], uint64(len(keys)))
return false
}
}
func (p *defaultPolicy) Add(key uint64, cost, ttl int64) ([]*item, bool) {
p.Lock()
defer p.Unlock()
// can't add an item bigger than entire cache
if cost > p.evict.maxCost {
return nil, false
}
// we don't need to go any further if the item is already in the cache
if has := p.evict.updateIfHas(key, cost); has {
return nil, true
}
// if we got this far, this key doesn't exist in the cache
//
// calculate the remaining room in the cache (usually bytes)
room := p.evict.roomLeft(cost)
if room >= 0 {
// there's enough room in the cache to store the new item without
// overflowing, so we can do that now and stop here
p.evict.add(key, cost)
if ttl != -1 {
p.times.PushFront([2]uint64{key, uint64(ttl)})
}
return nil, true
}
// as items are evicted they will be appended to victims
victims := make([]*item, 0)
// delete expired items before doing any evictions, we may not even need to
for e := p.times.Back(); e != nil; {
i := e.Value.([2]uint64)
if i[1] > uint64(time.Now().Unix()) {
break
}
next := e.Prev()
p.times.Remove(e)
e = next
p.evict.del(i[0])
victims = append(victims, &item{
key: i[0],
// TODO: better way of getting cost?
cost: p.evict.keys[i[0]],
})
}
if misc := p.evict.roomLeft(cost); misc >= 0 {
p.evict.add(key, cost)
if ttl != -1 {
p.times.PushFront([2]uint64{key, uint64(ttl)})
}
return victims, true
}
// incHits is the hit count for the incoming item
incHits := p.admit.Estimate(key)
// sample is the eviction candidate pool to be filled via random sampling
//
// TODO: perhaps we should use a min heap here. Right now our time
// complexity is N for finding the min. Min heap should bring it down to
// O(lg N).
sample := make([]*policyPair, 0, lfuSample)
// delete victims until there's enough space or a minKey is found that has
// more hits than incoming item.
for ; room < 0; room = p.evict.roomLeft(cost) {
// fill up empty slots in sample
sample = p.evict.fillSample(sample)
// find minimally used item in sample
minKey, minHits, minId, minCost := uint64(0), int64(math.MaxInt64), 0, int64(0)
for i, pair := range sample {
// look up hit count for sample key
if hits := p.admit.Estimate(pair.key); hits < minHits {
minKey, minHits, minId, minCost = pair.key, hits, i, pair.cost
}
}
// if the incoming item isn't worth keeping in the policy, reject.
if incHits < minHits {
p.metrics.add(rejectSets, key, 1)
return victims, false
}
// delete the victim from metadata
p.evict.del(minKey)
// delete the victim from sample
sample[minId] = sample[len(sample)-1]
sample = sample[:len(sample)-1]
// store victim in evicted victims slice
victims = append(victims, &item{key: minKey, cost: minCost})
}
p.evict.add(key, cost)
if ttl != -1 {
p.times.PushFront([2]uint64{key, uint64(ttl)})
}
return victims, true
}
func (p *defaultPolicy) Has(key uint64) bool {
p.Lock()
_, exists := p.evict.keys[key]
p.Unlock()
return exists
}
func (p *defaultPolicy) Del(key uint64) {
p.Lock()
p.evict.del(key)
p.Unlock()
}
func (p *defaultPolicy) Cap() int64 {
p.Lock()
capacity := int64(p.evict.maxCost - p.evict.used)
p.Unlock()
return capacity
}
func (p *defaultPolicy) Update(key uint64, cost int64) {
p.Lock()
p.evict.updateIfHas(key, cost)
p.Unlock()
}
func (p *defaultPolicy) Cost(key uint64) int64 {
p.Lock()
if cost, found := p.evict.keys[key]; found {
p.Unlock()
return cost
}
p.Unlock()
return -1
}
func (p *defaultPolicy) Clear() {
p.Lock()
p.admit.clear()
p.evict.clear()
p.Unlock()
}
func (p *defaultPolicy) Close() {
// block until p.processItems goroutine is returned
p.stop <- struct{}{}
close(p.stop)
close(p.itemsCh)
}
// sampledLFU is an eviction helper storing key-cost pairs.
type sampledLFU struct {
keys map[uint64]int64
maxCost int64
used int64
metrics *Metrics
}
func newSampledLFU(maxCost int64) *sampledLFU {
return &sampledLFU{
keys: make(map[uint64]int64),
maxCost: maxCost,
}
}
func (p *sampledLFU) roomLeft(cost int64) int64 {
return p.maxCost - (p.used + cost)
}
func (p *sampledLFU) fillSample(in []*policyPair) []*policyPair {
if len(in) >= lfuSample {
return in
}
for key, cost := range p.keys {
in = append(in, &policyPair{key, cost})
if len(in) >= lfuSample {
return in
}
}
return in
}
func (p *sampledLFU) del(key uint64) {
cost, ok := p.keys[key]
if !ok {
return
}
p.used -= cost
delete(p.keys, key)
}
func (p *sampledLFU) add(key uint64, cost int64) {
p.keys[key] = cost
p.used += cost
}
func (p *sampledLFU) updateIfHas(key uint64, cost int64) bool {
if keyCost, found := p.keys[key]; found {
// update the cost of an existing key, but don't worry about evicting,
// evictions will be handled the next time a new item is added
p.metrics.add(keyUpdate, key, 1)
p.used += cost - keyCost
p.keys[key] = cost
return true
}
return false
}
func (p *sampledLFU) clear() {
p.used = 0
p.keys = make(map[uint64]int64)
}
// tinyLFU is an admission helper that keeps track of access frequency using
// tiny (4-bit) counters in the form of a count-min sketch.
// tinyLFU is NOT thread safe.
type tinyLFU struct {
freq *cmSketch
door *z.Bloom
incrs int64
resetAt int64
}
func newTinyLFU(numCounters int64) *tinyLFU {
return &tinyLFU{
freq: newCmSketch(numCounters),
door: z.NewBloomFilter(float64(numCounters), 0.01),
resetAt: numCounters,
}
}
func (p *tinyLFU) Push(keys []uint64) {
for _, key := range keys {
p.Increment(key)
}
}
func (p *tinyLFU) Estimate(key uint64) int64 {
hits := p.freq.Estimate(key)
if p.door.Has(key) {
hits += 1
}
return hits
}
func (p *tinyLFU) Increment(key uint64) {
// flip doorkeeper bit if not already
if added := p.door.AddIfNotHas(key); !added {
// increment count-min counter if doorkeeper bit is already set.
p.freq.Increment(key)
}
p.incrs++
if p.incrs >= p.resetAt {
p.reset()
}
}
func (p *tinyLFU) reset() {
// Zero out incrs.
p.incrs = 0
// clears doorkeeper bits
p.door.Clear()
// halves count-min counters
p.freq.Reset()
}
func (p *tinyLFU) clear() {
p.incrs = 0
p.door.Clear()
p.freq.Clear()
}