/
blockscache.go
463 lines (393 loc) · 11.7 KB
/
blockscache.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
package bloomshipper
import (
"container/list"
"context"
"fmt"
"os"
"sync"
"time"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"go.uber.org/atomic"
"github.com/grafana/loki/v3/pkg/storage/stores/shipper/bloomshipper/config"
"github.com/grafana/loki/v3/pkg/util"
"github.com/grafana/loki/v3/pkg/util/constants"
)
const (
defaultPurgeInterval = 1 * time.Minute
// eviction reasons
reasonExpired = "expired"
reasonFull = "full"
// errors when putting entries
errAlreadyExists = "entry already exists: %s"
errTooBig = "entry exceeds hard limit: %s"
)
type Cache interface {
Put(ctx context.Context, key string, value BlockDirectory) error
PutInc(ctx context.Context, key string, value BlockDirectory) error
PutMany(ctx context.Context, keys []string, values []BlockDirectory) error
Get(ctx context.Context, key string) (BlockDirectory, bool)
Release(ctx context.Context, key string) error
Stop()
}
type blocksCacheMetrics struct {
entriesAdded prometheus.Counter
entriesEvicted *prometheus.CounterVec
entriesFetched *prometheus.CounterVec
entriesCurrent prometheus.Gauge
usageBytes prometheus.Gauge
// collecting hits/misses for every Get() is a performance killer
// instead, increment a counter and collect them in an interval
hits *atomic.Int64
misses *atomic.Int64
}
func newBlocksCacheMetrics(reg prometheus.Registerer, namespace, subsystem string) *blocksCacheMetrics {
return &blocksCacheMetrics{
entriesAdded: promauto.With(reg).NewCounter(prometheus.CounterOpts{
Namespace: namespace,
Subsystem: subsystem,
Name: "added_total",
Help: "The total number of entries added to the cache",
}),
entriesEvicted: promauto.With(reg).NewCounterVec(prometheus.CounterOpts{
Namespace: namespace,
Subsystem: subsystem,
Name: "evicted_total",
Help: "The total number of entries evicted from the cache",
}, []string{"reason"}),
entriesFetched: promauto.With(reg).NewCounterVec(prometheus.CounterOpts{
Namespace: namespace,
Subsystem: subsystem,
Name: "fetched_total",
Help: "Total number of entries fetched from cache, grouped by status",
}, []string{"status"}),
entriesCurrent: promauto.With(reg).NewGauge(prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: subsystem,
Name: "entries",
Help: "Current number of entries in the cache",
}),
usageBytes: promauto.With(reg).NewGauge(prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: subsystem,
Name: "usage_bytes",
Help: "The current size of entries managed by the cache in bytes",
}),
hits: atomic.NewInt64(0),
misses: atomic.NewInt64(0),
}
}
func (m *blocksCacheMetrics) Collect() {
m.entriesFetched.WithLabelValues("hit").Add(float64(m.hits.Swap(0)))
m.entriesFetched.WithLabelValues("miss").Add(float64(m.misses.Swap(0)))
}
// compiler check to enforce implementation of the Cache interface
var _ Cache = &BlocksCache{}
// BlocksCache is an in-memory cache that manages block directories on the filesystem.
type BlocksCache struct {
cfg config.BlocksCacheConfig
metrics *blocksCacheMetrics
logger log.Logger
lock sync.RWMutex // lock for cache entries
entries map[string]*list.Element
lru *list.List
done chan struct{}
triggerEviction chan struct{}
currSizeBytes int64
}
type Entry struct {
Key string
Value BlockDirectory
created time.Time
refCount *atomic.Int32
}
// NewFsBlocksCache returns a new file-system mapping cache for bloom blocks,
// where entries map block directories on disk.
func NewFsBlocksCache(cfg config.BlocksCacheConfig, reg prometheus.Registerer, logger log.Logger) *BlocksCache {
cache := &BlocksCache{
cfg: cfg,
logger: logger,
metrics: newBlocksCacheMetrics(reg, constants.Loki, "bloom_blocks_cache"),
entries: make(map[string]*list.Element),
lru: list.New(),
done: make(chan struct{}),
triggerEviction: make(chan struct{}, 1),
}
// Set a default interval for the ticker
// This can be overwritten to a smaller value in tests
if cfg.PurgeInterval == 0 {
cfg.PurgeInterval = defaultPurgeInterval
}
go cache.runTTLEvictJob(cfg.PurgeInterval, cfg.TTL)
go cache.runLRUevictJob()
go cache.runMetricsCollectJob(5 * time.Second)
return cache
}
// Put implements Cache.
// It stores a value with given key.
func (c *BlocksCache) Put(ctx context.Context, key string, value BlockDirectory) error {
if ctx.Err() != nil {
return ctx.Err()
}
c.lock.Lock()
defer c.lock.Unlock()
_, err := c.put(key, value)
return err
}
// PutInc implements Cache.
// It stores a value with given key and increments the ref counter on that item.
func (c *BlocksCache) PutInc(ctx context.Context, key string, value BlockDirectory) error {
if ctx.Err() != nil {
return ctx.Err()
}
c.lock.Lock()
defer c.lock.Unlock()
entry, err := c.put(key, value)
if err != nil {
return err
}
entry.refCount.Inc()
return nil
}
// PutMany implements Cache.
func (c *BlocksCache) PutMany(ctx context.Context, keys []string, values []BlockDirectory) error {
if ctx.Err() != nil {
return ctx.Err()
}
c.lock.Lock()
defer c.lock.Unlock()
var err util.MultiError
for i := range keys {
if _, e := c.put(keys[i], values[i]); e != nil {
err.Add(e)
}
}
return err.Err()
}
func (c *BlocksCache) put(key string, value BlockDirectory) (*Entry, error) {
// Blocks cache does not allow updating, so it rejects values with the same key
_, exists := c.entries[key]
if exists {
return nil, fmt.Errorf(errAlreadyExists, key)
}
entry := &Entry{
Key: key,
Value: value,
created: time.Now(),
refCount: atomic.NewInt32(0),
}
size := entry.Value.Size()
// Reject items that are larger than the hard limit
if size > int64(c.cfg.HardLimit) {
// It's safe to clean up the disk, since it does not have any references
// yet. Ideally, we avoid downloading blocks that do not fit into the cache
// upfront.
_ = c.remove(entry)
return nil, fmt.Errorf(errTooBig, key)
}
// Allow adding the new item even if the cache exceeds its soft limit.
// However, broadcast the condition that the cache should be cleaned up.
if c.currSizeBytes+size > int64(c.cfg.SoftLimit) {
level.Debug(c.logger).Log(
"msg", "adding item exceeds soft limit",
"action", "trigger soft eviction",
"curr_size_bytes", c.currSizeBytes,
"entry_size_bytes", size,
"soft_limit_bytes", c.cfg.SoftLimit,
"hard_limit_bytes", c.cfg.HardLimit,
)
select {
case c.triggerEviction <- struct{}{}:
// nothing
default:
level.Debug(c.logger).Log("msg", "eviction already in progress")
}
}
// Adding an item blocks if the cache would exceed its hard limit.
if c.currSizeBytes+size > int64(c.cfg.HardLimit) {
level.Debug(c.logger).Log(
"msg", "adding item exceeds hard limit",
"action", "evict items until space is freed",
"curr_size_bytes", c.currSizeBytes,
"entry_size_bytes", size,
"soft_limit_bytes", c.cfg.SoftLimit,
"hard_limit_bytes", c.cfg.HardLimit,
)
// TODO(chaudum): implement case
return nil, errors.New("todo: implement waiting for evictions to free up space")
}
// Cache has space to add the item.
c.entries[key] = c.lru.PushFront(entry)
c.currSizeBytes += size
c.metrics.entriesAdded.Inc()
c.metrics.entriesCurrent.Inc()
c.metrics.usageBytes.Set(float64(c.currSizeBytes))
return entry, nil
}
func (c *BlocksCache) evict(key string, element *list.Element, reason string) {
entry := element.Value.(*Entry)
err := c.remove(entry)
if err != nil {
level.Error(c.logger).Log("msg", "failed to remove entry from disk", "err", err)
return
}
c.lru.Remove(element)
delete(c.entries, key)
c.currSizeBytes -= entry.Value.Size()
c.metrics.entriesCurrent.Dec()
c.metrics.entriesEvicted.WithLabelValues(reason).Inc()
}
// Get implements Cache.
// Get returns the stored value against the given key.
func (c *BlocksCache) Get(ctx context.Context, key string) (BlockDirectory, bool) {
if ctx.Err() != nil {
return BlockDirectory{}, false
}
c.lock.Lock()
defer c.lock.Unlock()
entry := c.get(key)
if entry == nil {
return BlockDirectory{}, false
}
return entry.Value, true
}
func (c *BlocksCache) get(key string) *Entry {
element, exists := c.entries[key]
if !exists {
c.metrics.misses.Inc()
return nil
}
entry := element.Value.(*Entry)
entry.refCount.Inc()
c.lru.MoveToFront(element)
c.metrics.hits.Inc()
return entry
}
// Release decrements the ref counter on the cached item with given key.
func (c *BlocksCache) Release(ctx context.Context, key string) error {
if ctx.Err() != nil {
return ctx.Err()
}
// We can use a read lock because we only update a field on an existing entry
// and we do not modify the map of entries or the order in the LRU list.
c.lock.RLock()
defer c.lock.RUnlock()
element, exists := c.entries[key]
if !exists {
return nil
}
entry := element.Value.(*Entry)
entry.refCount.Dec()
return nil
}
// Stop implements Cache.
func (c *BlocksCache) Stop() {
c.lock.Lock()
defer c.lock.Unlock()
c.entries = make(map[string]*list.Element)
c.lru.Init()
c.metrics.entriesCurrent.Set(float64(0))
c.metrics.usageBytes.Set(float64(0))
close(c.done)
}
func (c *BlocksCache) remove(entry *Entry) error {
level.Info(c.logger).Log("msg", "remove entry from disk", "path", entry.Value.Path)
err := os.RemoveAll(entry.Value.Path)
if err != nil {
return fmt.Errorf("error removing bloom block directory from disk: %w", err)
}
return nil
}
func (c *BlocksCache) runMetricsCollectJob(interval time.Duration) {
level.Info(c.logger).Log("msg", "run metrics collect job")
ticker := time.NewTicker(interval)
defer ticker.Stop()
for {
select {
case <-c.done:
return
case <-ticker.C:
c.metrics.Collect()
}
}
}
func (c *BlocksCache) runLRUevictJob() {
level.Info(c.logger).Log("msg", "run lru evict job")
for {
select {
case <-c.done:
return
case <-c.triggerEviction:
c.evictLeastRecentlyUsedItems()
}
}
}
func (c *BlocksCache) evictLeastRecentlyUsedItems() {
c.lock.Lock()
defer c.lock.Unlock()
level.Debug(c.logger).Log(
"msg", "evict least recently used entries",
"curr_size_bytes", c.currSizeBytes,
"soft_limit_bytes", c.cfg.SoftLimit,
"hard_limit_bytes", c.cfg.HardLimit,
)
elem := c.lru.Back()
for c.currSizeBytes >= int64(c.cfg.SoftLimit) && elem != nil {
entry := elem.Value.(*Entry)
if entry.refCount.Load() == 0 {
level.Debug(c.logger).Log(
"msg", "evict least recently used entry",
"entry", entry.Key,
)
c.evict(entry.Key, elem, reasonFull)
}
elem = elem.Prev()
}
}
func (c *BlocksCache) runTTLEvictJob(interval, ttl time.Duration) {
if interval == 0 || ttl == 0 {
return
}
level.Info(c.logger).Log("msg", "run ttl evict job")
ticker := time.NewTicker(interval)
defer ticker.Stop()
for {
select {
case <-c.done:
return
case <-ticker.C:
c.evictExpiredItems(ttl)
}
}
}
// evictExpiredItems prunes items in the cache that exceeded their ttl after last access
func (c *BlocksCache) evictExpiredItems(ttl time.Duration) {
c.lock.Lock()
defer c.lock.Unlock()
level.Debug(c.logger).Log(
"msg", "evict expired entries",
"curr_size_bytes", c.currSizeBytes,
"soft_limit_bytes", c.cfg.SoftLimit,
"hard_limit_bytes", c.cfg.HardLimit,
)
for k, v := range c.entries {
entry := v.Value.(*Entry)
if time.Since(entry.created) > ttl && entry.refCount.Load() == 0 {
level.Debug(c.logger).Log(
"msg", "evict expired entry",
"entry", entry.Key,
"age", time.Since(entry.created),
"ttl", ttl,
)
c.evict(k, v, reasonExpired)
}
}
}
func (c *BlocksCache) len() (int, bool) {
c.lock.RLock()
defer c.lock.RUnlock()
return c.lru.Len(), c.lru.Len() == len(c.entries)
}