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

Moving code for cache functions to utils file #48

Merged

Conversation

pranavmishradatabricks
Copy link

  • I added CHANGELOG entry for this change.
  • Change is not relevant to the end user.

Changes

Moved code pertaining to the LRU Cache in the handler to a separate file for code clarity and to prevent conflicts upstream. This is a fast follow on https://github.com/databricks/thanos/pull/40

Verification

I used the same testing process as in the original pull request.

internal/cortex/frontend/transport/handler.go Outdated Show resolved Hide resolved
internal/cortex/frontend/transport/utils.go Outdated Show resolved Hide resolved
@hczhu-db hczhu-db requested a review from jnyi June 13, 2024 01:29
@hczhu-db
Copy link
Collaborator

hczhu-db commented Jun 13, 2024

Can you paste the command to run the new unit test?

@pranavmishradatabricks
Copy link
Author

Can you paste the command to run the new unit test?

=== RUN TestNewFailedQueryCache
--- PASS: TestNewFailedQueryCache (0.00s)
=== RUN TestUpdateFailedQueryCache
=== RUN TestUpdateFailedQueryCache/No_error_code_in_error_message
=== RUN TestUpdateFailedQueryCache/Non-cacheable_error_code
=== RUN TestUpdateFailedQueryCache/Cacheable_error_code
--- PASS: TestUpdateFailedQueryCache (0.00s)
--- PASS: TestUpdateFailedQueryCache/No_error_code_in_error_message (0.00s)
--- PASS: TestUpdateFailedQueryCache/Non-cacheable_error_code (0.00s)
--- PASS: TestUpdateFailedQueryCache/Cacheable_error_code (0.00s)
=== RUN TestQueryHitCache
=== RUN TestQueryHitCache/Cache_hit
=== RUN TestQueryHitCache/Cache_miss
--- PASS: TestQueryHitCache (0.00s)
--- PASS: TestQueryHitCache/Cache_hit (0.00s)
--- PASS: TestQueryHitCache/Cache_miss (0.00s)
PASS

internal/cortex/frontend/transport/handler.go Outdated Show resolved Hide resolved
internal/cortex/frontend/transport/handler.go Outdated Show resolved Hide resolved
internal/cortex/frontend/transport/handler.go Outdated Show resolved Hide resolved
internal/cortex/frontend/transport/handler.go Outdated Show resolved Hide resolved
internal/cortex/frontend/transport/utils/utils.go Outdated Show resolved Hide resolved
internal/cortex/frontend/transport/handler.go Outdated Show resolved Hide resolved
internal/cortex/frontend/transport/utils/utils_test.go Outdated Show resolved Hide resolved
internal/cortex/frontend/transport/utils/utils_test.go Outdated Show resolved Hide resolved
internal/cortex/frontend/transport/utils/utils_test.go Outdated Show resolved Hide resolved
internal/cortex/frontend/transport/utils/utils_test.go Outdated Show resolved Hide resolved
@pranavmishradatabricks
Copy link
Author

New testing output

=== RUN TestNewFailedQueryCache
--- PASS: TestNewFailedQueryCache (0.00s)
=== RUN TestUpdateFailedQueryCache
=== RUN TestUpdateFailedQueryCache/No_error_code_in_error_message
=== RUN TestUpdateFailedQueryCache/Non-cacheable_error_code
=== RUN TestUpdateFailedQueryCache/Cacheable_error_code
=== RUN TestUpdateFailedQueryCache/Adding_query_with_whitespace_and_ensuring_it_is_normalized
=== RUN TestUpdateFailedQueryCache/Cacheable_error_code_with_range_of_0,_ensuring_regex_parsing_is_correct,_and_updating_range_length
=== RUN TestUpdateFailedQueryCache/Successful_update_to_range_length
--- PASS: TestUpdateFailedQueryCache (0.00s)
--- PASS: TestUpdateFailedQueryCache/No_error_code_in_error_message (0.00s)
--- PASS: TestUpdateFailedQueryCache/Non-cacheable_error_code (0.00s)
--- PASS: TestUpdateFailedQueryCache/Cacheable_error_code (0.00s)
--- PASS: TestUpdateFailedQueryCache/Adding_query_with_whitespace_and_ensuring_it_is_normalized (0.00s)
--- PASS: TestUpdateFailedQueryCache/Cacheable_error_code_with_range_of_0,_ensuring_regex_parsing_is_correct,_and_updating_range_length (0.00s)
--- PASS: TestUpdateFailedQueryCache/Successful_update_to_range_length (0.00s)
=== RUN TestQueryHitCache
=== RUN TestQueryHitCache/Cache_hit
=== RUN TestQueryHitCache/Cache_miss
=== RUN TestQueryHitCache/Cache_miss_due_to_shorter_range_length
=== RUN TestQueryHitCache/Cache_hit_whitespace
=== RUN TestQueryHitCache/Cache_miss_whitespace
--- PASS: TestQueryHitCache (0.00s)
--- PASS: TestQueryHitCache/Cache_hit (0.00s)
--- PASS: TestQueryHitCache/Cache_miss (0.00s)
--- PASS: TestQueryHitCache/Cache_miss_due_to_shorter_range_length (0.00s)
--- PASS: TestQueryHitCache/Cache_hit_whitespace (0.00s)
--- PASS: TestQueryHitCache/Cache_miss_whitespace (0.00s)
PASS

internal/cortex/frontend/transport/handler.go Outdated Show resolved Hide resolved
internal/cortex/frontend/transport/handler.go Outdated Show resolved Hide resolved
internal/cortex/frontend/transport/handler.go Outdated Show resolved Hide resolved
internal/cortex/frontend/transport/handler.go Show resolved Hide resolved

// QueryHitCache checks if the lru cache is hit and returns whether to increment counter for cache hits along with appropriate message.
func queryHitCache(queryExpressionNormalized string, queryExpressionRangeLength int, lruCache *lru.Cache) (bool, string) {
if value, ok := lruCache.Get(queryExpressionNormalized); ok && value.(int) <= queryExpressionRangeLength {
Copy link
Collaborator

Choose a reason for hiding this comment

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

There is a bug here. The condition should be value.(int) >= queryExpressionRangeLength such that the cached range is longer than the current query range.
@pranavmishradatabricks Can you fix it and add a unit test case like the following one?

1. Cache failed query("xxx", range=1000) 
2. query("xxx", range=999) shouldn't be banned.

Choose a reason for hiding this comment

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

The condition should be value.(int) <= queryExpressionRangeLength since queryExpressionRangeLength is the range length of the new query and value.(int) is the range length of the old query. From my understanding, it should only hit the cache and block the query if the old query's range length <= the new query's range length (new query is longer range length).

I have a test case showing that it does not hit the cache here: Test File Line 153

Copy link
Collaborator

Choose a reason for hiding this comment

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

You are right.

internal/cortex/frontend/transport/utils/utils.go Outdated Show resolved Hide resolved
internal/cortex/frontend/transport/handler.go Show resolved Hide resolved

// QueryHitCache checks if the lru cache is hit and returns whether to increment counter for cache hits along with appropriate message.
func queryHitCache(queryExpressionNormalized string, queryExpressionRangeLength int, lruCache *lru.Cache) (bool, string) {
if value, ok := lruCache.Get(queryExpressionNormalized); ok && value.(int) <= queryExpressionRangeLength {
Copy link
Collaborator

Choose a reason for hiding this comment

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

You are right.

@pranavmishradatabricks pranavmishradatabricks merged commit 95e4da3 into databricks:db_main Jul 24, 2024
12 checks passed
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.

4 participants