-
Notifications
You must be signed in to change notification settings - Fork 806
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve async processor handling enabled items, optimize code further #5686
Merged
yeya24
merged 4 commits into
cortexproject:master
from
yeya24:improve-async-multilevel-cache-filter
Nov 30, 2023
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ import ( | |
"github.com/prometheus/prometheus/storage" | ||
"github.com/thanos-io/thanos/pkg/cacheutil" | ||
storecache "github.com/thanos-io/thanos/pkg/store/cache" | ||
"golang.org/x/exp/slices" | ||
) | ||
|
||
const ( | ||
|
@@ -21,18 +22,22 @@ const ( | |
) | ||
|
||
type multiLevelCache struct { | ||
caches []storecache.IndexCache | ||
postingsCaches, seriesCaches, expandedPostingCaches []storecache.IndexCache | ||
|
||
fetchLatency *prometheus.HistogramVec | ||
backFillLatency *prometheus.HistogramVec | ||
backfillProcessor *cacheutil.AsyncOperationProcessor | ||
backfillDroppedItems *prometheus.CounterVec | ||
fetchLatency *prometheus.HistogramVec | ||
backFillLatency *prometheus.HistogramVec | ||
backfillProcessor *cacheutil.AsyncOperationProcessor | ||
backfillDroppedPostings prometheus.Counter | ||
backfillDroppedSeries prometheus.Counter | ||
backfillDroppedExpandedPostings prometheus.Counter | ||
|
||
maxBackfillItems int | ||
} | ||
|
||
func (m *multiLevelCache) StorePostings(blockID ulid.ULID, l labels.Label, v []byte, tenant string) { | ||
wg := sync.WaitGroup{} | ||
wg.Add(len(m.caches)) | ||
for _, c := range m.caches { | ||
wg.Add(len(m.postingsCaches)) | ||
for _, c := range m.postingsCaches { | ||
cache := c | ||
go func() { | ||
defer wg.Done() | ||
|
@@ -48,9 +53,9 @@ func (m *multiLevelCache) FetchMultiPostings(ctx context.Context, blockID ulid.U | |
|
||
misses = keys | ||
hits = map[labels.Label][]byte{} | ||
backfillItems := make([]map[labels.Label][]byte, len(m.caches)-1) | ||
for i, c := range m.caches { | ||
if i < len(m.caches)-1 { | ||
backfillItems := make([]map[labels.Label][]byte, len(m.postingsCaches)-1) | ||
for i, c := range m.postingsCaches { | ||
if i < len(m.postingsCaches)-1 { | ||
backfillItems[i] = map[labels.Label][]byte{} | ||
} | ||
if ctx.Err() != nil { | ||
|
@@ -76,14 +81,23 @@ func (m *multiLevelCache) FetchMultiPostings(ctx context.Context, blockID ulid.U | |
backFillTimer := prometheus.NewTimer(m.backFillLatency.WithLabelValues(cacheTypePostings)) | ||
defer backFillTimer.ObserveDuration() | ||
for i, values := range backfillItems { | ||
for lbl, b := range values { | ||
lbl := lbl | ||
b := b | ||
if err := m.backfillProcessor.EnqueueAsync(func() { | ||
m.caches[i].StorePostings(blockID, lbl, b, tenant) | ||
}); errors.Is(err, cacheutil.ErrAsyncBufferFull) { | ||
m.backfillDroppedItems.WithLabelValues(cacheTypePostings).Inc() | ||
i := i | ||
values := values | ||
if len(values) == 0 { | ||
continue | ||
} | ||
if err := m.backfillProcessor.EnqueueAsync(func() { | ||
cnt := 0 | ||
for lbl, b := range values { | ||
m.postingsCaches[i].StorePostings(blockID, lbl, b, tenant) | ||
cnt++ | ||
if cnt == m.maxBackfillItems { | ||
m.backfillDroppedPostings.Add(float64(len(values) - cnt)) | ||
return | ||
} | ||
} | ||
}); errors.Is(err, cacheutil.ErrAsyncBufferFull) { | ||
m.backfillDroppedPostings.Add(float64(len(values))) | ||
} | ||
} | ||
}() | ||
|
@@ -93,8 +107,8 @@ func (m *multiLevelCache) FetchMultiPostings(ctx context.Context, blockID ulid.U | |
|
||
func (m *multiLevelCache) StoreExpandedPostings(blockID ulid.ULID, matchers []*labels.Matcher, v []byte, tenant string) { | ||
wg := sync.WaitGroup{} | ||
wg.Add(len(m.caches)) | ||
for _, c := range m.caches { | ||
wg.Add(len(m.expandedPostingCaches)) | ||
for _, c := range m.expandedPostingCaches { | ||
cache := c | ||
go func() { | ||
defer wg.Done() | ||
|
@@ -108,17 +122,17 @@ func (m *multiLevelCache) FetchExpandedPostings(ctx context.Context, blockID uli | |
timer := prometheus.NewTimer(m.fetchLatency.WithLabelValues(cacheTypeExpandedPostings)) | ||
defer timer.ObserveDuration() | ||
|
||
for i, c := range m.caches { | ||
for i, c := range m.expandedPostingCaches { | ||
if ctx.Err() != nil { | ||
return nil, false | ||
} | ||
if d, h := c.FetchExpandedPostings(ctx, blockID, matchers, tenant); h { | ||
if i > 0 { | ||
backFillTimer := prometheus.NewTimer(m.backFillLatency.WithLabelValues(cacheTypeExpandedPostings)) | ||
if err := m.backfillProcessor.EnqueueAsync(func() { | ||
m.caches[i-1].StoreExpandedPostings(blockID, matchers, d, tenant) | ||
m.expandedPostingCaches[i-1].StoreExpandedPostings(blockID, matchers, d, tenant) | ||
}); errors.Is(err, cacheutil.ErrAsyncBufferFull) { | ||
m.backfillDroppedItems.WithLabelValues(cacheTypeExpandedPostings).Inc() | ||
m.backfillDroppedExpandedPostings.Inc() | ||
} | ||
backFillTimer.ObserveDuration() | ||
} | ||
|
@@ -131,8 +145,8 @@ func (m *multiLevelCache) FetchExpandedPostings(ctx context.Context, blockID uli | |
|
||
func (m *multiLevelCache) StoreSeries(blockID ulid.ULID, id storage.SeriesRef, v []byte, tenant string) { | ||
wg := sync.WaitGroup{} | ||
wg.Add(len(m.caches)) | ||
for _, c := range m.caches { | ||
wg.Add(len(m.seriesCaches)) | ||
for _, c := range m.seriesCaches { | ||
cache := c | ||
go func() { | ||
defer wg.Done() | ||
|
@@ -148,10 +162,10 @@ func (m *multiLevelCache) FetchMultiSeries(ctx context.Context, blockID ulid.ULI | |
|
||
misses = ids | ||
hits = map[storage.SeriesRef][]byte{} | ||
backfillItems := make([]map[storage.SeriesRef][]byte, len(m.caches)-1) | ||
backfillItems := make([]map[storage.SeriesRef][]byte, len(m.seriesCaches)-1) | ||
|
||
for i, c := range m.caches { | ||
if i < len(m.caches)-1 { | ||
for i, c := range m.seriesCaches { | ||
if i < len(m.seriesCaches)-1 { | ||
backfillItems[i] = map[storage.SeriesRef][]byte{} | ||
} | ||
if ctx.Err() != nil { | ||
|
@@ -177,29 +191,57 @@ func (m *multiLevelCache) FetchMultiSeries(ctx context.Context, blockID ulid.ULI | |
backFillTimer := prometheus.NewTimer(m.backFillLatency.WithLabelValues(cacheTypeSeries)) | ||
defer backFillTimer.ObserveDuration() | ||
for i, values := range backfillItems { | ||
for ref, b := range values { | ||
ref := ref | ||
b := b | ||
if err := m.backfillProcessor.EnqueueAsync(func() { | ||
m.caches[i].StoreSeries(blockID, ref, b, tenant) | ||
}); errors.Is(err, cacheutil.ErrAsyncBufferFull) { | ||
m.backfillDroppedItems.WithLabelValues(cacheTypeSeries).Inc() | ||
i := i | ||
values := values | ||
if len(values) == 0 { | ||
continue | ||
} | ||
if err := m.backfillProcessor.EnqueueAsync(func() { | ||
cnt := 0 | ||
for ref, b := range values { | ||
m.seriesCaches[i].StoreSeries(blockID, ref, b, tenant) | ||
cnt++ | ||
if cnt == m.maxBackfillItems { | ||
m.backfillDroppedSeries.Add(float64(len(values) - cnt)) | ||
return | ||
} | ||
} | ||
}); errors.Is(err, cacheutil.ErrAsyncBufferFull) { | ||
m.backfillDroppedSeries.Add(float64(len(values))) | ||
} | ||
} | ||
}() | ||
|
||
return hits, misses | ||
} | ||
|
||
func newMultiLevelCache(reg prometheus.Registerer, cfg MultiLevelIndexCacheConfig, c ...storecache.IndexCache) storecache.IndexCache { | ||
func filterCachesByItem(enabledItems [][]string, cachedItem string, c ...storecache.IndexCache) []storecache.IndexCache { | ||
filteredCaches := make([]storecache.IndexCache, 0, len(c)) | ||
for i := range enabledItems { | ||
if len(enabledItems[i]) == 0 || slices.Contains(enabledItems[i], cachedItem) { | ||
filteredCaches = append(filteredCaches, c[i]) | ||
} | ||
} | ||
return filteredCaches | ||
} | ||
|
||
func newMultiLevelCache(reg prometheus.Registerer, cfg MultiLevelIndexCacheConfig, enabledItems [][]string, c ...storecache.IndexCache) storecache.IndexCache { | ||
if len(c) == 1 { | ||
return c[0] | ||
if len(enabledItems[0]) == 0 { | ||
return c[0] | ||
} | ||
return storecache.NewFilteredIndexCache(c[0], enabledItems[0]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we still need this type? we cannot just use a multi level with 1 level? |
||
} | ||
|
||
backfillDroppedItems := promauto.With(reg).NewCounterVec(prometheus.CounterOpts{ | ||
Name: "cortex_store_multilevel_index_cache_backfill_dropped_items_total", | ||
Help: "Total number of items dropped due to async buffer full when backfilling multilevel cache ", | ||
}, []string{"item_type"}) | ||
return &multiLevelCache{ | ||
caches: c, | ||
backfillProcessor: cacheutil.NewAsyncOperationProcessor(cfg.MaxAsyncBufferSize, cfg.MaxAsyncConcurrency), | ||
postingsCaches: filterCachesByItem(enabledItems, cacheTypePostings, c...), | ||
seriesCaches: filterCachesByItem(enabledItems, cacheTypeSeries, c...), | ||
expandedPostingCaches: filterCachesByItem(enabledItems, cacheTypeExpandedPostings, c...), | ||
backfillProcessor: cacheutil.NewAsyncOperationProcessor(cfg.MaxAsyncBufferSize, cfg.MaxAsyncConcurrency), | ||
fetchLatency: promauto.With(reg).NewHistogramVec(prometheus.HistogramOpts{ | ||
Name: "cortex_store_multilevel_index_cache_fetch_duration_seconds", | ||
Help: "Histogram to track latency to fetch items from multi level index cache", | ||
|
@@ -210,9 +252,9 @@ func newMultiLevelCache(reg prometheus.Registerer, cfg MultiLevelIndexCacheConfi | |
Help: "Histogram to track latency to backfill items from multi level index cache", | ||
Buckets: []float64{0.01, 0.1, 0.3, 0.6, 1, 3, 6, 10, 15, 20, 25, 30, 40, 50, 60, 90}, | ||
}, []string{"item_type"}), | ||
backfillDroppedItems: promauto.With(reg).NewCounterVec(prometheus.CounterOpts{ | ||
Name: "cortex_store_multilevel_index_cache_backfill_dropped_items_total", | ||
Help: "Total number of items dropped due to async buffer full when backfilling multilevel cache ", | ||
}, []string{"item_type"}), | ||
backfillDroppedPostings: backfillDroppedItems.WithLabelValues(cacheTypePostings), | ||
backfillDroppedSeries: backfillDroppedItems.WithLabelValues(cacheTypeSeries), | ||
backfillDroppedExpandedPostings: backfillDroppedItems.WithLabelValues(cacheTypeExpandedPostings), | ||
maxBackfillItems: cfg.MaxBackfillItems, | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we put this also on the "async task"?