Skip to content
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 filtered index cache filtering #6955

Merged
merged 2 commits into from
Dec 4, 2023
Merged
Changes from 1 commit
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
31 changes: 20 additions & 11 deletions pkg/store/cache/filter_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,45 +14,54 @@ import (
)

type FilteredIndexCache struct {
cache IndexCache
enabledItems []string
cache IndexCache
postingsEnabled bool
seriesEnabled bool
expandedPostingsEnabled bool
}

// NewFilteredIndexCache creates a filtered index cache based on enabled items.
func NewFilteredIndexCache(cache IndexCache, enabledItems []string) *FilteredIndexCache {
return &FilteredIndexCache{
cache: cache,
enabledItems: enabledItems,
c := &FilteredIndexCache{cache: cache}
if len(enabledItems) == 0 || slices.Contains(enabledItems, CacheTypePostings) {
c.postingsEnabled = true
}
if len(enabledItems) == 0 || slices.Contains(enabledItems, CacheTypeExpandedPostings) {
c.expandedPostingsEnabled = true
}
if len(enabledItems) == 0 || slices.Contains(enabledItems, CacheTypeSeries) {
c.seriesEnabled = true
}
yeya24 marked this conversation as resolved.
Show resolved Hide resolved
return c
}

// StorePostings sets the postings identified by the ulid and label to the value v,
// if the postings already exists in the cache it is not mutated.
func (c *FilteredIndexCache) StorePostings(blockID ulid.ULID, l labels.Label, v []byte, tenant string) {
if len(c.enabledItems) == 0 || slices.Contains(c.enabledItems, CacheTypePostings) {
if c.postingsEnabled {
c.cache.StorePostings(blockID, l, v, tenant)
}
}

// FetchMultiPostings fetches multiple postings - each identified by a label -
// and returns a map containing cache hits, along with a list of missing keys.
func (c *FilteredIndexCache) FetchMultiPostings(ctx context.Context, blockID ulid.ULID, keys []labels.Label, tenant string) (hits map[labels.Label][]byte, misses []labels.Label) {
if len(c.enabledItems) == 0 || slices.Contains(c.enabledItems, CacheTypePostings) {
if c.postingsEnabled {
return c.cache.FetchMultiPostings(ctx, blockID, keys, tenant)
}
return nil, keys
}

// StoreExpandedPostings stores expanded postings for a set of label matchers.
func (c *FilteredIndexCache) StoreExpandedPostings(blockID ulid.ULID, matchers []*labels.Matcher, v []byte, tenant string) {
if len(c.enabledItems) == 0 || slices.Contains(c.enabledItems, CacheTypeExpandedPostings) {
if c.expandedPostingsEnabled {
c.cache.StoreExpandedPostings(blockID, matchers, v, tenant)
}
}

// FetchExpandedPostings fetches expanded postings and returns cached data and a boolean value representing whether it is a cache hit or not.
func (c *FilteredIndexCache) FetchExpandedPostings(ctx context.Context, blockID ulid.ULID, matchers []*labels.Matcher, tenant string) ([]byte, bool) {
if len(c.enabledItems) == 0 || slices.Contains(c.enabledItems, CacheTypeExpandedPostings) {
if c.expandedPostingsEnabled {
return c.cache.FetchExpandedPostings(ctx, blockID, matchers, tenant)
}
return nil, false
Expand All @@ -61,15 +70,15 @@ func (c *FilteredIndexCache) FetchExpandedPostings(ctx context.Context, blockID
// StoreSeries sets the series identified by the ulid and id to the value v,
// if the series already exists in the cache it is not mutated.
func (c *FilteredIndexCache) StoreSeries(blockID ulid.ULID, id storage.SeriesRef, v []byte, tenant string) {
if len(c.enabledItems) == 0 || slices.Contains(c.enabledItems, CacheTypeSeries) {
if c.seriesEnabled {
c.cache.StoreSeries(blockID, id, v, tenant)
}
}

// FetchMultiSeries fetches multiple series - each identified by ID - from the cache
// and returns a map containing cache hits, along with a list of missing IDs.
func (c *FilteredIndexCache) FetchMultiSeries(ctx context.Context, blockID ulid.ULID, ids []storage.SeriesRef, tenant string) (hits map[storage.SeriesRef][]byte, misses []storage.SeriesRef) {
if len(c.enabledItems) == 0 || slices.Contains(c.enabledItems, CacheTypeSeries) {
if c.seriesEnabled {
return c.cache.FetchMultiSeries(ctx, blockID, ids, tenant)
}
return nil, ids
Expand Down
Loading