8
8
"strconv"
9
9
"strings"
10
10
"sync"
11
+ "sync/atomic"
11
12
"time"
12
13
13
14
"github.com/oklog/ulid"
@@ -100,6 +101,9 @@ func (cfg *PostingsCacheConfig) RegisterFlagsWithPrefix(prefix, block string, f
100
101
type ExpandedPostingsCacheFactory struct {
101
102
seedByHash * seedByHash
102
103
cfg TSDBPostingsCacheConfig
104
+
105
+ headCachedBytes atomic.Int64
106
+ blocksCachedBytes atomic.Int64
103
107
}
104
108
105
109
func NewExpandedPostingsCacheFactory (cfg TSDBPostingsCacheConfig ) * ExpandedPostingsCacheFactory {
@@ -118,7 +122,7 @@ func NewExpandedPostingsCacheFactory(cfg TSDBPostingsCacheConfig) *ExpandedPosti
118
122
}
119
123
120
124
func (f * ExpandedPostingsCacheFactory ) NewExpandedPostingsCache (userId string , metrics * ExpandedPostingsCacheMetrics ) ExpandedPostingsCache {
121
- return newBlocksPostingsForMatchersCache (userId , f .cfg , metrics , f .seedByHash )
125
+ return newBlocksPostingsForMatchersCache (userId , f .cfg , metrics , f .seedByHash , & f . headCachedBytes , & f . blocksCachedBytes )
122
126
}
123
127
124
128
type ExpandedPostingsCache interface {
@@ -138,7 +142,7 @@ type blocksPostingsForMatchersCache struct {
138
142
seedByHash * seedByHash
139
143
}
140
144
141
- func newBlocksPostingsForMatchersCache (userId string , cfg TSDBPostingsCacheConfig , metrics * ExpandedPostingsCacheMetrics , seedByHash * seedByHash ) ExpandedPostingsCache {
145
+ func newBlocksPostingsForMatchersCache (userId string , cfg TSDBPostingsCacheConfig , metrics * ExpandedPostingsCacheMetrics , seedByHash * seedByHash , headCachedBytes , blocksCachedBytes * atomic. Int64 ) ExpandedPostingsCache {
142
146
if cfg .PostingsForMatchers == nil {
143
147
cfg .PostingsForMatchers = tsdb .PostingsForMatchers
144
148
}
@@ -148,8 +152,8 @@ func newBlocksPostingsForMatchersCache(userId string, cfg TSDBPostingsCacheConfi
148
152
}
149
153
150
154
return & blocksPostingsForMatchersCache {
151
- headCache : newFifoCache [[]storage.SeriesRef ](cfg .Head , "head" , metrics , cfg .timeNow ),
152
- blocksCache : newFifoCache [[]storage.SeriesRef ](cfg .Blocks , "block" , metrics , cfg .timeNow ),
155
+ headCache : newFifoCache [[]storage.SeriesRef ](cfg .Head , headCachedBytes , "head" , metrics , cfg .timeNow ),
156
+ blocksCache : newFifoCache [[]storage.SeriesRef ](cfg .Blocks , blocksCachedBytes , "block" , metrics , cfg .timeNow ),
153
157
postingsForMatchersFunc : cfg .PostingsForMatchers ,
154
158
timeNow : cfg .timeNow ,
155
159
metrics : metrics ,
@@ -333,17 +337,18 @@ type fifoCache[V any] struct {
333
337
// Fields from here should be locked
334
338
cachedMtx sync.RWMutex
335
339
cached * list.List
336
- cachedBytes int64
340
+ cachedBytes * atomic. Int64
337
341
}
338
342
339
- func newFifoCache [V any ](cfg PostingsCacheConfig , name string , metrics * ExpandedPostingsCacheMetrics , timeNow func () time.Time ) * fifoCache [V ] {
343
+ func newFifoCache [V any ](cfg PostingsCacheConfig , cachedBytes * atomic. Int64 , name string , metrics * ExpandedPostingsCacheMetrics , timeNow func () time.Time ) * fifoCache [V ] {
340
344
return & fifoCache [V ]{
341
345
cachedValues : new (sync.Map ),
342
346
cached : list .New (),
343
347
cfg : cfg ,
344
348
timeNow : timeNow ,
345
349
name : name ,
346
350
metrics : * metrics ,
351
+ cachedBytes : cachedBytes ,
347
352
}
348
353
}
349
354
@@ -417,7 +422,7 @@ func (c *fifoCache[V]) shouldEvictHead() (string, bool) {
417
422
return "" , false
418
423
}
419
424
420
- if c .cachedBytes > c .cfg .MaxBytes {
425
+ if c .cachedBytes . Load () > c .cfg .MaxBytes {
421
426
return "full" , true
422
427
}
423
428
@@ -437,7 +442,7 @@ func (c *fifoCache[V]) evictHead() {
437
442
c .cached .Remove (front )
438
443
oldestKey := front .Value .(string )
439
444
if oldest , loaded := c .cachedValues .LoadAndDelete (oldestKey ); loaded {
440
- c .cachedBytes -= oldest .(* cacheEntryPromise [V ]).sizeBytes
445
+ c .cachedBytes . Add ( - oldest .(* cacheEntryPromise [V ]).sizeBytes )
441
446
}
442
447
}
443
448
@@ -449,7 +454,7 @@ func (c *fifoCache[V]) created(key string, sizeBytes int64) {
449
454
c .cachedMtx .Lock ()
450
455
defer c .cachedMtx .Unlock ()
451
456
c .cached .PushBack (key )
452
- c .cachedBytes += sizeBytes
457
+ c .cachedBytes . Add ( sizeBytes )
453
458
}
454
459
455
460
func (c * fifoCache [V ]) updateSize (oldSize , newSizeBytes int64 ) {
@@ -459,7 +464,7 @@ func (c *fifoCache[V]) updateSize(oldSize, newSizeBytes int64) {
459
464
460
465
c .cachedMtx .Lock ()
461
466
defer c .cachedMtx .Unlock ()
462
- c .cachedBytes += newSizeBytes - oldSize
467
+ c .cachedBytes . Add ( newSizeBytes - oldSize )
463
468
}
464
469
465
470
type cacheEntryPromise [V any ] struct {
0 commit comments