Skip to content

Commit b159f09

Browse files
committed
fix
Signed-off-by: alanprot <alanprot@gmail.com>
1 parent 49429c9 commit b159f09

File tree

2 files changed

+20
-32
lines changed

2 files changed

+20
-32
lines changed

pkg/storage/tsdb/expanded_postings_cache.go

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -197,10 +197,17 @@ func (c *BlocksPostingsForMatchersCache) fetchPostings(blockID ulid.ULID, ix tsd
197197
return c.result(promise)
198198
}
199199

200-
func (c *BlocksPostingsForMatchersCache) result(promise *cacheEntryPromise[[]storage.SeriesRef]) func(ctx context.Context) (index.Postings, error) {
200+
func (c *BlocksPostingsForMatchersCache) result(ce *cacheEntryPromise[[]storage.SeriesRef]) func(ctx context.Context) (index.Postings, error) {
201201
return func(ctx context.Context) (index.Postings, error) {
202-
ids, err := promise.result(ctx)
203-
return index.NewListPostings(ids), err
202+
select {
203+
case <-ctx.Done():
204+
return nil, ctx.Err()
205+
case <-ce.done:
206+
if ctx.Err() != nil {
207+
return nil, ctx.Err()
208+
}
209+
return index.NewListPostings(ce.v), ce.err
210+
}
204211
}
205212
}
206213

@@ -327,9 +334,12 @@ func (c *fifoCache[V]) getPromiseForKey(k string, fetch func() (V, int64, error)
327334
c.expire()
328335
}
329336

330-
// If is cached but is expired, lets try to replace the cache value
331-
if ok && loaded.(*cacheEntryPromise[V]).isExpired(c.cfg.Ttl, c.timeNow()) {
332-
if c.cachedValues.CompareAndSwap(k, loaded, r) {
337+
if ok {
338+
// If the promise is already in the cache, lets wait it to fetch the data.
339+
<-loaded.(*cacheEntryPromise[V]).done
340+
341+
// If is cached but is expired, lets try to replace the cache value.
342+
if loaded.(*cacheEntryPromise[V]).isExpired(c.cfg.Ttl, c.timeNow()) && c.cachedValues.CompareAndSwap(k, loaded, r) {
333343
r.v, r.sizeBytes, r.err = fetch()
334344
r.sizeBytes += int64(len(k))
335345
c.updateSize(loaded.(*cacheEntryPromise[V]).sizeBytes, r.sizeBytes)
@@ -404,19 +414,6 @@ type cacheEntryPromise[V any] struct {
404414
err error
405415
}
406416

407-
func (ce *cacheEntryPromise[V]) result(ctx context.Context) (V, error) {
408-
select {
409-
case <-ctx.Done():
410-
return ce.v, ctx.Err()
411-
case <-ce.done:
412-
if ctx.Err() != nil {
413-
return ce.v, ctx.Err()
414-
}
415-
416-
return ce.v, ce.err
417-
}
418-
}
419-
420417
func (ce *cacheEntryPromise[V]) isExpired(ttl time.Duration, now time.Time) bool {
421418
ts := ce.ts
422419
r := now.Sub(ts)

pkg/storage/tsdb/expanded_postings_cache_test.go

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package tsdb
22

33
import (
4-
"context"
54
"fmt"
65
"strings"
76
"sync"
@@ -54,9 +53,7 @@ func TestFifoCacheDisabled(t *testing.T) {
5453
return 1, 0, nil
5554
})
5655
require.False(t, loaded)
57-
v, err := old.result(context.Background())
58-
require.NoError(t, err)
59-
require.Equal(t, 1, v)
56+
require.Equal(t, 1, old.v)
6057
require.False(t, cache.contains("key1"))
6158
}
6259

@@ -101,17 +98,13 @@ func TestFifoCacheExpire(t *testing.T) {
10198
return 1, 8, nil
10299
})
103100
require.False(t, loaded)
104-
v, err := p.result(context.Background())
105-
require.NoError(t, err)
106-
require.Equal(t, 1, v)
101+
require.Equal(t, 1, p.v)
107102
require.True(t, cache.contains(key))
108103
p, loaded = cache.getPromiseForKey(key, func() (int, int64, error) {
109104
return 1, 0, nil
110105
})
111106
require.True(t, loaded)
112-
v, err = p.result(context.Background())
113-
require.NoError(t, err)
114-
require.Equal(t, 1, v)
107+
require.Equal(t, 1, p.v)
115108
}
116109

117110
totalCacheSize := 0
@@ -137,10 +130,8 @@ func TestFifoCacheExpire(t *testing.T) {
137130
return 2, 18, nil
138131
})
139132
require.False(t, loaded)
140-
v, err := p.result(context.Background())
141-
require.NoError(t, err)
142133
// New value
143-
require.Equal(t, 2, v)
134+
require.Equal(t, 2, p.v)
144135
// Total Size Updated
145136
require.Equal(t, originalSize+10, cache.cachedBytes)
146137
}

0 commit comments

Comments
 (0)