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

query-frontend: inject query cache keys for LabelValues/Cardinality requests #6849

Merged

Conversation

dimitarvdimitrov
Copy link
Contributor

@dimitarvdimitrov dimitarvdimitrov commented Dec 7, 2023

This PR makes it possible for GEM to inject its own cache key generation. This required splitting up the existing genericQueryDelegate into two interfaces

type tenantCacheTTL interface {
	// ttl returns the cache TTL for the input userID.
	ttl(userID string) time.Duration
}

and extending CacheSplitter to include LabelValues and Cardinality cache items

// CacheKeyGenerator generates cache keys. This is a useful interface for downstream
// consumers who wish to implement their own strategies.
type CacheKeyGenerator interface {
	// QueryRequest should generate a cache key based on the tenant ID and Request.
	QueryRequest(ctx context.Context, tenantID string, r Request) string

	// LabelValues should return a cache key for a label values request. The cache key does not need to contain the tenant ID.
	LabelValues(ctx context.Context, path string, values url.Values) (*GenericQueryCacheKey, error)

	// LabelValuesCardinality should return a cache key for a label values cardinality request. The cache key does not need to contain the tenant ID.
	LabelValuesCardinality(ctx context.Context, path string, values url.Values) (*GenericQueryCacheKey, error)
}

…abelValues

Signed-off-by: Dimitar Dimitrov <dimitar.dimitrov@grafana.com>
Signed-off-by: Dimitar Dimitrov <dimitar.dimitrov@grafana.com>
Signed-off-by: Dimitar Dimitrov <dimitar.dimitrov@grafana.com>
@dimitarvdimitrov dimitarvdimitrov requested a review from a team as a code owner December 7, 2023 16:14
Signed-off-by: Dimitar Dimitrov <dimitar.dimitrov@grafana.com>
Comment on lines 186 to 188
GenerateCacheKey(ctx context.Context, userID string, r Request) string
GenerateLabelValuesCacheKey(ctx context.Context, userID, path string, values url.Values) (*GenericQueryCacheKey, error)
GenerateLabelValuesCardinalityCacheKey(ctx context.Context, userID, path string, values url.Values) (*GenericQueryCacheKey, error)
Copy link
Contributor Author

@dimitarvdimitrov dimitarvdimitrov Dec 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find the Generate prefix unnecessary and repetitive. I went with it for consistency, but I would rather drop it. If reviewers agree, I can introduce new methods as LabelValuesCacheKey and LabelValuesCardinalityCacheKey and open a follow-up PR to rename GenerateCacheKey to CacheKey

Also: should we still call it CacheSplitter if it generates cache keys, and doesn't actually split anything?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find the Generate prefix unnecessary and repetitive

I don't feel strongly about it if you want to change it.

Also: should we still call it CacheSplitter if it generates cache keys, and doesn't actually split anything?

I definitely think this should change.

Signed-off-by: Dimitar Dimitrov <dimitar.dimitrov@grafana.com>
Copy link
Contributor

@56quarters 56quarters left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

Comment on lines 186 to 188
GenerateCacheKey(ctx context.Context, userID string, r Request) string
GenerateLabelValuesCacheKey(ctx context.Context, userID, path string, values url.Values) (*GenericQueryCacheKey, error)
GenerateLabelValuesCardinalityCacheKey(ctx context.Context, userID, path string, values url.Values) (*GenericQueryCacheKey, error)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find the Generate prefix unnecessary and repetitive

I don't feel strongly about it if you want to change it.

Also: should we still call it CacheSplitter if it generates cache keys, and doesn't actually split anything?

I definitely think this should change.

Signed-off-by: Dimitar Dimitrov <dimitar.dimitrov@grafana.com>
Signed-off-by: Dimitar Dimitrov <dimitar.dimitrov@grafana.com>
@dimitarvdimitrov
Copy link
Contributor Author

thank you for the review @56quarters. I pushed a few more changes

  1. Remove Generate prefix from CacheSplitter interface
  2. Rename CacheSplitter to CacheKeyGenerator
  3. Remove userID from signature of LabelValues and Cardinality methods. The tenant ID was already being taken into account in the genericQueryCache, so it was confusing to ask the key generator to account for it too

Signed-off-by: Dimitar Dimitrov <dimitar.dimitrov@grafana.com>
@dimitarvdimitrov dimitarvdimitrov merged commit efa26e7 into main Dec 8, 2023
28 checks passed
@dimitarvdimitrov dimitarvdimitrov deleted the dimitar/query-frontend/generic-query-cache-splitting branch December 8, 2023 12:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants