@@ -43,7 +43,7 @@ type BlocksPostingsForMatchersCache struct {
4343 postingsForMatchersFunc func (ctx context.Context , ix tsdb.IndexReader , ms ... * labels.Matcher ) (index.Postings , error )
4444 timeNow func () time.Time
4545
46- metrics * ExpandedPostingsCacheMetrics
46+ metrics ExpandedPostingsCacheMetrics
4747}
4848
4949var (
@@ -66,8 +66,8 @@ type ExpandedPostingsCacheMetrics struct {
6666 NonCacheableQueries * prometheus.CounterVec
6767}
6868
69- func NewPostingCacheMetrics (r prometheus.Registerer ) * ExpandedPostingsCacheMetrics {
70- return & ExpandedPostingsCacheMetrics {
69+ func NewPostingCacheMetrics (r prometheus.Registerer ) ExpandedPostingsCacheMetrics {
70+ return ExpandedPostingsCacheMetrics {
7171 CacheRequests : promauto .With (r ).NewCounterVec (prometheus.CounterOpts {
7272 Name : "expanded_postings_cache_requests_total" ,
7373 Help : "Total number of requests to the cache." ,
@@ -87,11 +87,15 @@ func NewPostingCacheMetrics(r prometheus.Registerer) *ExpandedPostingsCacheMetri
8787 }
8888}
8989
90- func NewBlocksPostingsForMatchersCache (metrics * ExpandedPostingsCacheMetrics , headExpandedPostingsCacheSize uint64 , blockExpandedPostingsCacheSize uint64 ) ExpandedPostingsCache {
90+ func NewBlocksPostingsForMatchersCache (metrics ExpandedPostingsCacheMetrics , headExpandedPostingsCacheSize uint64 , blockExpandedPostingsCacheSize uint64 , seedSize int64 ) * BlocksPostingsForMatchersCache {
91+ if seedSize <= 0 {
92+ seedSize = seedArraySize
93+ }
94+
9195 return & BlocksPostingsForMatchersCache {
9296 headCache : newFifoCache [[]storage.SeriesRef ]("head" , metrics , time .Now , headExpandedPostingsCacheSize ),
9397 blocksCache : newFifoCache [[]storage.SeriesRef ]("block" , metrics , time .Now , blockExpandedPostingsCacheSize ),
94- headSeedByMetricName : make ([]int , seedArraySize ),
98+ headSeedByMetricName : make ([]int , seedSize ),
9599 strippedLock : make ([]sync.RWMutex , numOfSeedsStripes ),
96100 postingsForMatchersFunc : tsdb .PostingsForMatchers ,
97101 timeNow : time .Now ,
@@ -129,7 +133,7 @@ func (c *BlocksPostingsForMatchersCache) ExpireSeries(metric labels.Labels) {
129133
130134 h := MemHashString (metricName )
131135 i := h % uint64 (len (c .headSeedByMetricName ))
132- l := h % uint64 (len (c .strippedLock ))
136+ l := i % uint64 (len (c .strippedLock ))
133137 c .strippedLock [l ].Lock ()
134138 defer c .strippedLock [l ].Unlock ()
135139 c .headSeedByMetricName [i ]++
@@ -200,7 +204,7 @@ func (c *BlocksPostingsForMatchersCache) result(ce *cacheEntryPromise[[]storage.
200204func (c * BlocksPostingsForMatchersCache ) getSeedForMetricName (metricName string ) string {
201205 h := MemHashString (metricName )
202206 i := h % uint64 (len (c .headSeedByMetricName ))
203- l := h % uint64 (len (c .strippedLock ))
207+ l := i % uint64 (len (c .strippedLock ))
204208 c .strippedLock [l ].RLock ()
205209 defer c .strippedLock [l ].RUnlock ()
206210 return strconv .Itoa (c .headSeedByMetricName [i ])
@@ -276,13 +280,13 @@ type fifoCache[V any] struct {
276280 cachedBytes int64
277281}
278282
279- func newFifoCache [V any ](name string , metrics * ExpandedPostingsCacheMetrics , timeNow func () time.Time , maxBytes uint64 ) * fifoCache [V ] {
283+ func newFifoCache [V any ](name string , metrics ExpandedPostingsCacheMetrics , timeNow func () time.Time , maxBytes uint64 ) * fifoCache [V ] {
280284 return & fifoCache [V ]{
281285 cachedValues : new (sync.Map ),
282286 cached : list .New (),
283287 timeNow : timeNow ,
284288 name : name ,
285- metrics : * metrics ,
289+ metrics : metrics ,
286290 ttl : 10 * time .Minute ,
287291 maxBytes : int64 (maxBytes ),
288292 }
0 commit comments