Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions integration/querier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,11 +297,11 @@ func TestQuerierWithBlocksStorageRunningInMicroservicesMode(t *testing.T) {
}
require.NoError(t, storeGateways.WaitSumMetrics(e2e.Equals(2), "thanos_store_index_cache_hits_total")) // this time has used the index cache

if strings.Contains(testCfg.indexCacheBackend, tsdb.IndexCacheBackendInMemory) {
if testCfg.indexCacheBackend == tsdb.IndexCacheBackendInMemory {
require.NoError(t, storeGateways.WaitSumMetrics(e2e.Equals(9), "thanos_store_index_cache_items")) // as before
require.NoError(t, storeGateways.WaitSumMetrics(e2e.Equals(9), "thanos_store_index_cache_items_added_total")) // as before
}
if strings.Contains(testCfg.indexCacheBackend, tsdb.IndexCacheBackendMemcached) {
if testCfg.indexCacheBackend == tsdb.IndexCacheBackendMemcached {
require.NoError(t, storeGateways.WaitSumMetrics(e2e.Equals(23-l0CacheHits), "thanos_memcached_operations_total")) // as before + 2 gets - cache hits
}

Expand Down
52 changes: 45 additions & 7 deletions pkg/storage/tsdb/multilevel_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,35 @@ func (m *multiLevelCache) StorePostings(blockID ulid.ULID, l labels.Label, v []b
func (m *multiLevelCache) FetchMultiPostings(ctx context.Context, blockID ulid.ULID, keys []labels.Label) (hits map[labels.Label][]byte, misses []labels.Label) {
misses = keys
hits = map[labels.Label][]byte{}
for _, c := range m.caches {
h, m := c.FetchMultiPostings(ctx, blockID, misses)
misses = m
backfillMap := map[storecache.IndexCache][]map[labels.Label][]byte{}
for i, c := range m.caches {
backfillMap[c] = []map[labels.Label][]byte{}
h, mi := c.FetchMultiPostings(ctx, blockID, misses)
misses = mi

for label, bytes := range h {
hits[label] = bytes
}

if i > 0 {
backfillMap[m.caches[i-1]] = append(backfillMap[m.caches[i-1]], h)
}

if len(misses) == 0 {
break
}
}

defer func() {
for cache, hit := range backfillMap {
for _, values := range hit {
for l, b := range values {
cache.StorePostings(blockID, l, b)
}
}
}
}()

return hits, misses
}

Expand All @@ -59,8 +76,11 @@ func (m *multiLevelCache) StoreExpandedPostings(blockID ulid.ULID, matchers []*l
}

func (m *multiLevelCache) FetchExpandedPostings(ctx context.Context, blockID ulid.ULID, matchers []*labels.Matcher) ([]byte, bool) {
for _, c := range m.caches {
for i, c := range m.caches {
if d, h := c.FetchExpandedPostings(ctx, blockID, matchers); h {
if i > 0 {
m.caches[i-1].StoreExpandedPostings(blockID, matchers, d)
}
return d, h
}
}
Expand All @@ -84,18 +104,36 @@ func (m *multiLevelCache) StoreSeries(blockID ulid.ULID, id storage.SeriesRef, v
func (m *multiLevelCache) FetchMultiSeries(ctx context.Context, blockID ulid.ULID, ids []storage.SeriesRef) (hits map[storage.SeriesRef][]byte, misses []storage.SeriesRef) {
misses = ids
hits = map[storage.SeriesRef][]byte{}
for _, c := range m.caches {
h, m := c.FetchMultiSeries(ctx, blockID, misses)
misses = m
backfillMap := map[storecache.IndexCache][]map[storage.SeriesRef][]byte{}

for i, c := range m.caches {
backfillMap[c] = []map[storage.SeriesRef][]byte{}
h, miss := c.FetchMultiSeries(ctx, blockID, misses)
misses = miss

for label, bytes := range h {
hits[label] = bytes
}

if i > 0 && len(h) > 0 {
backfillMap[m.caches[i-1]] = append(backfillMap[m.caches[i-1]], h)
}

if len(misses) == 0 {
break
}
}

defer func() {
for cache, hit := range backfillMap {
for _, values := range hit {
for m, b := range values {
cache.StoreSeries(blockID, m, b)
}
}
}
}()

return hits, misses
}

Expand Down
20 changes: 16 additions & 4 deletions pkg/storage/tsdb/multilevel_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,16 +148,20 @@ func Test_MultiLevelCache(t *testing.T) {
cache.FetchMultiPostings(ctx, bID, []labels.Label{l1, l2})
},
},
"[FetchMultiPostings] should fallback only the missing keys on l1": {
"[FetchMultiPostings] should fallback and backfill only the missing keys on l1": {
m1ExpectedCalls: map[string][][]interface{}{
"FetchMultiPostings": {{bID, []labels.Label{l1, l2}}},
"StorePostings": {{bID, l2, v}},
},
m2ExpectedCalls: map[string][][]interface{}{
"FetchMultiPostings": {{bID, []labels.Label{l2}}},
},
m1MockedCalls: map[string][]interface{}{
"FetchMultiPostings": {map[labels.Label][]byte{l1: make([]byte, 1)}, []labels.Label{l2}},
},
m2MockedCalls: map[string][]interface{}{
"FetchMultiPostings": {map[labels.Label][]byte{l2: v}, []labels.Label{}},
},
call: func(cache storecache.IndexCache) {
cache.FetchMultiPostings(ctx, bID, []labels.Label{l1, l2})
},
Expand Down Expand Up @@ -185,15 +189,19 @@ func Test_MultiLevelCache(t *testing.T) {
cache.FetchMultiSeries(ctx, bID, []storage.SeriesRef{1, 2})
},
},
"[FetchMultiSeries] should fallback only the missing keys on l1": {
"[FetchMultiSeries] should fallback and backfill only the missing keys on l1": {
m1ExpectedCalls: map[string][][]interface{}{
"FetchMultiSeries": {{bID, []storage.SeriesRef{1, 2}}},
"StoreSeries": {{bID, storage.SeriesRef(2), v}},
},
m2ExpectedCalls: map[string][][]interface{}{
"FetchMultiSeries": {{bID, []storage.SeriesRef{2}}},
},
m1MockedCalls: map[string][]interface{}{
"FetchMultiSeries": {map[storage.SeriesRef][]byte{1: make([]byte, 1)}, []storage.SeriesRef{2}},
"FetchMultiSeries": {map[storage.SeriesRef][]byte{1: v}, []storage.SeriesRef{2}},
},
m2MockedCalls: map[string][]interface{}{
"FetchMultiSeries": {map[storage.SeriesRef][]byte{2: v}, []storage.SeriesRef{2}},
},
call: func(cache storecache.IndexCache) {
cache.FetchMultiSeries(ctx, bID, []storage.SeriesRef{1, 2})
Expand All @@ -211,13 +219,17 @@ func Test_MultiLevelCache(t *testing.T) {
cache.FetchMultiSeries(ctx, bID, []storage.SeriesRef{1, 2})
},
},
"[FetchExpandedPostings] Should fallback when miss": {
"[FetchExpandedPostings] Should fallback and backfill when miss": {
m1ExpectedCalls: map[string][][]interface{}{
"StoreExpandedPostings": {{bID, []*labels.Matcher{matcher}, v}},
"FetchExpandedPostings": {{bID, []*labels.Matcher{matcher}}},
},
m2ExpectedCalls: map[string][][]interface{}{
"FetchExpandedPostings": {{bID, []*labels.Matcher{matcher}}},
},
m2MockedCalls: map[string][]interface{}{
"FetchExpandedPostings": {v, true},
},
call: func(cache storecache.IndexCache) {
cache.FetchExpandedPostings(ctx, bID, []*labels.Matcher{matcher})
},
Expand Down