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

Support to set max cache size for per query in local cache #37859

Merged
merged 28 commits into from
Jun 10, 2022

Conversation

KinderRiven
Copy link
Contributor

@KinderRiven KinderRiven commented Jun 5, 2022

Changelog category (leave one):

  • Improvement

Changelog entry (a user-readable short description of the changes that goes to CHANGELOG.md):

Description
Limiting the maximum cache usage per query can effectively prevent cache pool contamination.
Related Issues

Improvement
In the figure below, we perform several small queries (SSB's Q1.1), and after the third execution we run a large query (SSB's Q2.1, which scans almost the entire table). Running large query without limiting the maximum cache usage would pollute the entire cache pool, so continuing to run Q1.1 would require re-reading data from remote S3, causing performance fluctuations.

image

Use and Configuration
The user can set the maximum cache usage per query with set max_query_cache_size before running a query.

image

The user can set the maximum cache usage per query with set skip_download_if_exceeds_query_cache to decide what method to take for subsequent reads when the query cache overflows. If it is true, it will not continue to download the cache and read directly from the remote fs. If it is false, it will be replaced in the cache downloaded before the query according to the LRU strategy.

image

Implement
Each query, when executed, requests a temporary context that holds a cached access record (LRU queue) for that query. When the query does not reach the maximum cache usage, it replaces the cache from the main cache queue in the normal way (cold data is selected from all cache blocks according to the LRU algorithm). However, when the query reaches maximum cache usage, it replaces cache blocks downloaded by the query.

Information about CI checks: https://clickhouse.com/docs/en/development/continuous-integration/

@robot-ch-test-poll robot-ch-test-poll added the pr-improvement Pull request with some product improvements label Jun 5, 2022
@KinderRiven KinderRiven changed the title Support to configure maximum cache size per query Support to set max cache size for query in local cache Jun 5, 2022
@KinderRiven KinderRiven changed the title Support to set max cache size for query in local cache Support to set max cache size for per query in local cache Jun 5, 2022
@kssenii kssenii self-assigned this Jun 5, 2022
@kssenii kssenii added the can be tested Allows running workflows for external contributors label Jun 5, 2022
src/Common/FileCache.cpp Outdated Show resolved Hide resolved
src/Common/FileCache.cpp Outdated Show resolved Hide resolved
src/Common/FileCache.cpp Outdated Show resolved Hide resolved
src/Common/FileCache.cpp Outdated Show resolved Hide resolved
src/Common/FileCache.cpp Outdated Show resolved Hide resolved
src/Common/FileCache.cpp Outdated Show resolved Hide resolved
src/Common/FileCache.cpp Outdated Show resolved Hide resolved
src/Common/FileCache.cpp Outdated Show resolved Hide resolved
@KinderRiven KinderRiven requested a review from kssenii June 6, 2022 04:52
@kssenii
Copy link
Member

kssenii commented Jun 6, 2022

clang-tidy failed:

src/CMakeFiles/clickhouse_common_io.dir/Common/FileCache.cpp.o.d -o src/CMakeFiles/clickhouse_common_io.dir/Common/FileCache.cpp.o -c ../src/Common/FileCache.cpp
Jun 06 10:35:14 /build/build_docker/../src/Common/FileCache.cpp:35:132: error: redundant boolean literal in ternary expression result [readability-simplify-boolean-expr,-warnings-as-errors]
Jun 06 10:35:14     return (!CurrentThread::isInitialized() || !CurrentThread::get().getQueryContext() || CurrentThread::getQueryId().size == 0) ? false : true;
Jun 06 10:35:14            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
Jun 06 10:35:14            !(!CurrentThread::isInitialized() || !CurrentThread::get().getQueryContext() || CurrentThread::getQueryId().size == 0)
Jun 06 10:35:14 /build/build_docker/../src/Common/FileCache.cpp:581:13: error: 'auto cell' can be declared as 'auto *cell' [readability-qualified-auto,-warnings-as-errors]
Jun 06 10:35:14             auto cell = getCell(iter->key, iter->offset, cache_lock);
Jun 06 10:35:14             ^~~~~
Jun 06 10:35:14             auto *
Jun 06 10:35:14 182175 warnings generated.

src/Common/FileCache.cpp Outdated Show resolved Hide resolved
src/Common/FileCache.h Show resolved Hide resolved
src/Common/FileCache.cpp Outdated Show resolved Hide resolved
src/Common/FileCache.cpp Outdated Show resolved Hide resolved
src/Common/FileCache.cpp Outdated Show resolved Hide resolved
src/Common/FileCache.h Outdated Show resolved Hide resolved
src/Common/FileSegment.cpp Outdated Show resolved Hide resolved
@KinderRiven KinderRiven requested a review from kssenii June 7, 2022 18:07
src/Common/FileSegment.cpp Outdated Show resolved Hide resolved
src/Common/FileCache.cpp Outdated Show resolved Hide resolved
src/Common/FileCache.cpp Outdated Show resolved Hide resolved
@KinderRiven KinderRiven requested a review from kssenii June 8, 2022 09:41
src/Common/FileCache.h Show resolved Hide resolved
src/Common/FileCache.h Outdated Show resolved Hide resolved
src/Common/FileCache.cpp Outdated Show resolved Hide resolved
src/Common/FileCache.cpp Outdated Show resolved Hide resolved
src/Common/FileCache.h Outdated Show resolved Hide resolved
@KinderRiven KinderRiven requested a review from kssenii June 8, 2022 12:47
src/Common/FileCache.cpp Show resolved Hide resolved
src/Common/FileCache.cpp Show resolved Hide resolved
@kssenii kssenii merged commit 580a30c into ClickHouse:master Jun 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
can be tested Allows running workflows for external contributors pr-improvement Pull request with some product improvements
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants