88 "strconv"
99 "strings"
1010 "sync"
11+ "sync/atomic"
1112 "time"
1213
1314 "github.com/oklog/ulid"
@@ -100,6 +101,9 @@ func (cfg *PostingsCacheConfig) RegisterFlagsWithPrefix(prefix, block string, f
100101type ExpandedPostingsCacheFactory struct {
101102 seedByHash * seedByHash
102103 cfg TSDBPostingsCacheConfig
104+
105+ headCachedBytes atomic.Int64
106+ blocksCachedBytes atomic.Int64
103107}
104108
105109func NewExpandedPostingsCacheFactory (cfg TSDBPostingsCacheConfig ) * ExpandedPostingsCacheFactory {
@@ -118,7 +122,7 @@ func NewExpandedPostingsCacheFactory(cfg TSDBPostingsCacheConfig) *ExpandedPosti
118122}
119123
120124func (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 )
122126}
123127
124128type ExpandedPostingsCache interface {
@@ -138,7 +142,7 @@ type blocksPostingsForMatchersCache struct {
138142 seedByHash * seedByHash
139143}
140144
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 {
142146 if cfg .PostingsForMatchers == nil {
143147 cfg .PostingsForMatchers = tsdb .PostingsForMatchers
144148 }
@@ -148,8 +152,8 @@ func newBlocksPostingsForMatchersCache(userId string, cfg TSDBPostingsCacheConfi
148152 }
149153
150154 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 ),
153157 postingsForMatchersFunc : cfg .PostingsForMatchers ,
154158 timeNow : cfg .timeNow ,
155159 metrics : metrics ,
@@ -333,17 +337,18 @@ type fifoCache[V any] struct {
333337 // Fields from here should be locked
334338 cachedMtx sync.RWMutex
335339 cached * list.List
336- cachedBytes int64
340+ cachedBytes * atomic. Int64
337341}
338342
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 ] {
340344 return & fifoCache [V ]{
341345 cachedValues : new (sync.Map ),
342346 cached : list .New (),
343347 cfg : cfg ,
344348 timeNow : timeNow ,
345349 name : name ,
346350 metrics : * metrics ,
351+ cachedBytes : cachedBytes ,
347352 }
348353}
349354
@@ -417,7 +422,7 @@ func (c *fifoCache[V]) shouldEvictHead() (string, bool) {
417422 return "" , false
418423 }
419424
420- if c .cachedBytes > c .cfg .MaxBytes {
425+ if c .cachedBytes . Load () > c .cfg .MaxBytes {
421426 return "full" , true
422427 }
423428
@@ -437,7 +442,7 @@ func (c *fifoCache[V]) evictHead() {
437442 c .cached .Remove (front )
438443 oldestKey := front .Value .(string )
439444 if oldest , loaded := c .cachedValues .LoadAndDelete (oldestKey ); loaded {
440- c .cachedBytes -= oldest .(* cacheEntryPromise [V ]).sizeBytes
445+ c .cachedBytes . Add ( - oldest .(* cacheEntryPromise [V ]).sizeBytes )
441446 }
442447}
443448
@@ -449,7 +454,7 @@ func (c *fifoCache[V]) created(key string, sizeBytes int64) {
449454 c .cachedMtx .Lock ()
450455 defer c .cachedMtx .Unlock ()
451456 c .cached .PushBack (key )
452- c .cachedBytes += sizeBytes
457+ c .cachedBytes . Add ( sizeBytes )
453458}
454459
455460func (c * fifoCache [V ]) updateSize (oldSize , newSizeBytes int64 ) {
@@ -459,7 +464,7 @@ func (c *fifoCache[V]) updateSize(oldSize, newSizeBytes int64) {
459464
460465 c .cachedMtx .Lock ()
461466 defer c .cachedMtx .Unlock ()
462- c .cachedBytes += newSizeBytes - oldSize
467+ c .cachedBytes . Add ( newSizeBytes - oldSize )
463468}
464469
465470type cacheEntryPromise [V any ] struct {
0 commit comments