Skip to content

Automatic GROUP/ORDER BY to disk based on the memory usage#71406

Merged
Michicosun merged 17 commits intoClickHouse:masterfrom
azat:automatic-external-aggregation
Nov 29, 2024
Merged

Automatic GROUP/ORDER BY to disk based on the memory usage#71406
Michicosun merged 17 commits intoClickHouse:masterfrom
azat:automatic-external-aggregation

Conversation

@azat
Copy link
Copy Markdown
Member

@azat azat commented Nov 3, 2024

Changelog category (leave one):

  • Improvement

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

Automatic GROUP BY/ORDER BY to disk based on the server/user memory usage. Controlled with max_bytes_ratio_before_external_group_by/max_bytes_ratio_before_external_sort query settings.

Fixes: #69286

@robot-ch-test-poll1 robot-ch-test-poll1 added the pr-improvement Pull request with some product improvements label Nov 3, 2024
@robot-ch-test-poll2
Copy link
Copy Markdown
Contributor

robot-ch-test-poll2 commented Nov 3, 2024

This is an automated comment for commit dad435a with description of existing statuses. It's updated for the latest CI running

❌ Click here to open a full report in a separate page

Check nameDescriptionStatus
Integration testsThe integration tests report. In parenthesis the package type is given, and in square brackets are the optional part/total tests❌ failure
Successful checks
Check nameDescriptionStatus
AST fuzzerRuns randomly generated queries to catch program errors. The build type is optionally given in parenthesis. If it fails, ask a maintainer for help✅ success
BuildsThere's no description for the check yet, please add it to tests/ci/ci_config.py:CHECK_DESCRIPTIONS✅ success
ClickBenchRuns [ClickBench](https://github.com/ClickHouse/ClickBench/) with instant-attach table✅ success
Compatibility checkChecks that clickhouse binary runs on distributions with old libc versions. If it fails, ask a maintainer for help✅ success
Docker keeper imageThe check to build and optionally push the mentioned image to docker hub✅ success
Docker server imageThe check to build and optionally push the mentioned image to docker hub✅ success
Docs checkBuilds and tests the documentation✅ success
Fast testNormally this is the first check that is ran for a PR. It builds ClickHouse and runs most of stateless functional tests, omitting some. If it fails, further checks are not started until it is fixed. Look at the report to see which tests fail, then reproduce the failure locally as described here✅ success
Flaky testsChecks if new added or modified tests are flaky by running them repeatedly, in parallel, with more randomization. Functional tests are run 100 times with address sanitizer, and additional randomization of thread scheduling. Integration tests are run up to 10 times. If at least once a new test has failed, or was too long, this check will be red. We don't allow flaky tests, read the doc✅ success
Install packagesChecks that the built packages are installable in a clear environment✅ success
Performance ComparisonMeasure changes in query performance. The performance test report is described in detail here. In square brackets are the optional part/total tests✅ success
Stateful testsRuns stateful functional tests for ClickHouse binaries built in various configurations -- release, debug, with sanitizers, etc✅ success
Stateless testsRuns stateless functional tests for ClickHouse binaries built in various configurations -- release, debug, with sanitizers, etc✅ success
Stress testRuns stateless functional tests concurrently from several clients to detect concurrency-related errors✅ success
Style checkRuns a set of checks to keep the code style clean. If some of tests failed, see the related log from the report✅ success
Unit testsRuns the unit tests for different release types✅ success
Upgrade checkRuns stress tests on server version from last release and then tries to upgrade it to the version from the PR. It checks if the new server can successfully startup without any errors, crashes or sanitizer asserts✅ success

@azat azat force-pushed the automatic-external-aggregation branch 2 times, most recently from d392357 to 8b3ce12 Compare November 4, 2024 10:03
@Michicosun Michicosun self-assigned this Nov 4, 2024
@azat
Copy link
Copy Markdown
Member Author

azat commented Nov 4, 2024

@azat azat force-pushed the automatic-external-aggregation branch from b72e705 to 2c52da9 Compare November 4, 2024 12:48
@azat
Copy link
Copy Markdown
Member Author

azat commented Nov 4, 2024

Stateless tests (ubsan) [2/2] — Server died, fail: 17, passed: 388, skipped: 5

/var/log/clickhouse-server/clickhouse-server.err.log:2024.11.04 09:31:07.829197 [ 1264 ] {} <Fatal> : Logical error: 'Part all_61_61_0 from table test_j2lmd2kq.alter_table1 (b42bc2ca-2251-436a-990e-3104aa0976cb) remains in ZooKeeper after DROP_RANGE all_0_61_999999999_999999999'.

@azat azat changed the title Automatic GROUP BY to disk (max_bytes_ratio_before_external_group_by) Automatic GROUP/ORDER BY to disk Nov 5, 2024
@azat
Copy link
Copy Markdown
Member Author

azat commented Nov 5, 2024

Conflicting files
src/Core/SettingsChangesHistory.cpp

Fixed and also added separate setting for ORDER BY.

@azat azat force-pushed the automatic-external-aggregation branch 2 times, most recently from 19cdf22 to 2df43d4 Compare November 5, 2024 10:39
@azat azat force-pushed the automatic-external-aggregation branch from 2df43d4 to a429980 Compare November 6, 2024 05:28
@azat azat marked this pull request as draft November 6, 2024 09:38
@azat azat force-pushed the automatic-external-aggregation branch from a429980 to b33be86 Compare November 6, 2024 10:03
@azat azat marked this pull request as ready for review November 6, 2024 10:03
@azat azat force-pushed the automatic-external-aggregation branch 2 times, most recently from edd5c4f to a773d3d Compare November 7, 2024 07:22
@azat
Copy link
Copy Markdown
Member Author

azat commented Nov 7, 2024

Wow, green CI, haven't seen this for awhile

Copy link
Copy Markdown
Member

@Michicosun Michicosun left a comment

Choose a reason for hiding this comment

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

I think we can merge per query setting(max_bytes_ratio_before_external_group_by), because it is a sugar extension for currently used max_bytes_before_external_group_by.

Or use the method suggested in the issue and compute the memory limit for the query once before it starts as ratio_setting * remaining_memory_amount and use it similarly to max_bytes_before_external_group_by. In this case, to compute remaining_memory_amount, we need to find the memory tracker in the hierarchy with the most strict hard limit and check its occupancy.

@azat
Copy link
Copy Markdown
Member Author

azat commented Nov 25, 2024

I think we can merge per query setting(max_bytes_ratio_before_external_group_by), because it is a sugar extension for currently used max_bytes_before_external_group_by.

How do you think max_bytes_ratio_before_external_group_by can be merged with max_bytes_before_external_group_by?

I agree that it is kind of configuration sugar, but not completely.

The difference is that this ratio is supported all the time, not only at the query start (like described in #69286, and that 10% of cases that is not covered by that proposal is that are covered by this patches), and I do find it useful, imagine a situation when the user spawned 100 queries at one point in time, that will eventually requires more RAM that the node has.

Or use the method suggested in the issue and compute the memory limit for the query once before it starts as ratio_setting * remaining_memory_amount and use it similarly to max_bytes_before_external_group_by. In this case, to compute remaining_memory_amount, we need to find the memory tracker in the hierarchy with the most strict hard limit and check its occupancy.

Again, I'm not really like this idea, because it does allow to cover the case with query peaks.

And I actually looked through more advanced functionality that had been described in #41887, but I'm not sure that the complexity described there worth it, with taking into respect that this patches covers 99% cases.

P.S. I also followed the same convention as some other settings, like:

  • remerge_sort_lowered_memory_bytes_ratio
  • memory_overcommit_ratio_*
  • max_server_memory_usage_to_ram_ratio

@azat azat force-pushed the automatic-external-aggregation branch from a773d3d to 9534a35 Compare November 25, 2024 17:45
@Michicosun
Copy link
Copy Markdown
Member

How do you think max_bytes_ratio_before_external_group_by can be merged with max_bytes_before_external_group_by?

I meant to merge the changes with this setting (max_bytes_ratio_before_external_group_by).

I agree that it is kind of configuration sugar, but not completely.

If you set max_bytes_before_external_group_by = max_bytes_ratio_before_external_group_by * memory_limit then it will have the same logic you wrote for max_bytes_ratio_before_external_group_by.

But I agree that it is more convenient than the previous one.

And I actually looked through more advanced functionality that had been described in #41887, but I'm not sure that the complexity described there worth it, with taking into respect that this patches covers 99% cases.

Right now you implemented 2 new types of settings:

  1. max_bytes_ratio_* - this is by complexity and solving problems is the same as max_bytes_before_*
  2. *_for_server - this adds new behaviour. If server using more memory than this setting forces to spill data to disk of every running query.

Obviously 1-st can't solve #69286 because it has old logic.

Second on the other hand affects all queries at the same time - this is not per-query level setting. For example if limit is 0.7 and current memory consumption is 0.69 and you started new query it will be spilled to disk right after start, because total memory consumption will become > 0.7.

In proposed solution it is important to recalculate limit from free memory related to the current user. So why your solution handles 99% of cases?

@azat
Copy link
Copy Markdown
Member Author

azat commented Nov 25, 2024

Thanks, I guess now I understand your point and it does make sense, let's verify if I understood your correctly:

  • leave only max_bytes_ratio_before_external_group_by (no server settings)
  • use hard memory limit of the server for this ratio
  • use strictest hard memory limit for this ratio

I meant to merge the changes with this setting (max_bytes_ratio_before_external_group_by).

But it is still unclear what you are suggesting to merge

In proposed solution it is important to recalculate limit from free memory related to the current user. So why your solution handles 99% of cases?

I haven't thought about this case, my point was that #41887 looks too complex to me (with an extra hook), and I think that it is better to try dumb implementation first (like this patch, but using per-user memory limits)

@azat azat marked this pull request as draft November 26, 2024 21:00
@azat azat changed the title Automatic GROUP/ORDER BY to disk Automatic GROUP/ORDER BY to disk based on the server memory usage Nov 27, 2024
@azat azat changed the title Automatic GROUP/ORDER BY to disk based on the server memory usage Automatic GROUP/ORDER BY to disk based on the memory usage Nov 27, 2024
azat added 14 commits November 28, 2024 16:01
Signed-off-by: Azat Khuzhin <a.khuzhin@semrush.com>
Signed-off-by: Azat Khuzhin <a.khuzhin@semrush.com>
Signed-off-by: Azat Khuzhin <a.khuzhin@semrush.com>
Signed-off-by: Azat Khuzhin <a.khuzhin@semrush.com>
…r_server under TSan

The problem is that TSan has some memory overhead which increases
process RSS to 10Gi in the middle of the query and it will fail.
Another option is to avoid syncing with RSS too frequently, but I doubt
that it is significant to run this test under TSan

v1: increase memory to 20Gi
    Fixes: https://s3.amazonaws.com/clickhouse-test-reports/71406/b33be86dee3c7616e9193c339837b9e250810557/integration_tests__tsan__[6_6].html
v2: instead just disable under TSan
    Fixes: https://s3.amazonaws.com/clickhouse-test-reports/71406/df68cf7362d825c3d83991fd74c391a49e73a8b1/integration_tests__tsan__[6_6].html

Signed-off-by: Azat Khuzhin <a.khuzhin@semrush.com>
Signed-off-by: Azat Khuzhin <a.khuzhin@semrush.com>
…storage check

Signed-off-by: Azat Khuzhin <a.khuzhin@semrush.com>
…::mergeOnBlock()

Signed-off-by: Azat Khuzhin <a.khuzhin@semrush.com>
…eded()

Signed-off-by: Azat Khuzhin <a.khuzhin@semrush.com>
Signed-off-by: Azat Khuzhin <a.khuzhin@semrush.com>
- remove max_bytes_ratio_before_external_{order,group_by}_for_server
- change the way max_bytes_ratio_before_external_{order,group_by} works

Note, that it is not enough to transform ratio to bytes in
executeQuery(), since in this case it will not work for merges and
internal queries, plus, you have to reset them for Distributed engine
and update it for Merge/View/...

This patch also introduce some helpers (see MemoryTrackerUtils) and
adjust Aggregator::Params constructor to accept Settings object instead
of tons of arguments.

Signed-off-by: Azat Khuzhin <a.khuzhin@semrush.com>
Signed-off-by: Azat Khuzhin <a.khuzhin@semrush.com>
Signed-off-by: Azat Khuzhin <a.khuzhin@semrush.com>
…it is not configured

Signed-off-by: Azat Khuzhin <a.khuzhin@semrush.com>
@azat azat force-pushed the automatic-external-aggregation branch from af822de to dad435a Compare November 28, 2024 15:16
@azat
Copy link
Copy Markdown
Member Author

azat commented Nov 28, 2024

I've rebased on top of upstream (to fix conflicts in settings changes), but the changes had been done on top for easier incremental review

@Michicosun
Copy link
Copy Markdown
Member

test_storage_rabbitmq #71049

@Michicosun Michicosun added this pull request to the merge queue Nov 29, 2024
Merged via the queue into ClickHouse:master with commit b81ee27 Nov 29, 2024
@azat azat deleted the automatic-external-aggregation branch November 29, 2024 12:56
@robot-clickhouse robot-clickhouse added the pr-synced-to-cloud The PR is synced to the cloud repo label Nov 29, 2024
baibaichen added a commit to Kyligence/gluten that referenced this pull request Nov 30, 2024
baibaichen added a commit to Kyligence/gluten that referenced this pull request Nov 30, 2024
baibaichen added a commit to apache/gluten that referenced this pull request Nov 30, 2024
* [GLUTEN-1632][CH]Daily Update Clickhouse Version (20241130)

* Fix Build due to ClickHouse/ClickHouse#71406

* Fix build due to ClickHouse/ClickHouse#72460

---------

Co-authored-by: kyligence-git <gluten@kyligence.io>
Co-authored-by: Chang Chen <baibaichen@gmail.com>
azat added a commit to azat/ClickHouse that referenced this pull request Dec 30, 2024
…ry consumption

Make max_bytes_before_external_sort more user-friendly, previously it
was number of bytes in the sorting block for one sorting thread, now it
has the same meaning as max_bytes_before_external_group_by - it is total
limit for the whole query memory for all threads.

Also one more setting added to control on disk block size -
min_external_sort_block_bytes

But, after this change the files on disk can be very small (few
kilobytes) and to avoid this, I've added separate setting to control
on-disk block size - min_external_sort_block_bytes, to avoid too many
files.

Note, that max_bytes_ratio_before_external_sort already based on the
memory consumption not the sorting block size (added in ClickHouse#71406)

Signed-off-by: Azat Khuzhin <a.khuzhin@semrush.com>
azat added a commit to azat/ClickHouse that referenced this pull request Dec 31, 2024
…ry consumption

Make max_bytes_before_external_sort more user-friendly, previously it
was number of bytes in the sorting block for one sorting thread, now it
has the same meaning as max_bytes_before_external_group_by - it is total
limit for the whole query memory for all threads.

Also one more setting added to control on disk block size -
min_external_sort_block_bytes

But, after this change the files on disk can be very small (few
kilobytes) and to avoid this, I've added separate setting to control
on-disk block size - min_external_sort_block_bytes, to avoid too many
files.

Note, that max_bytes_ratio_before_external_sort already based on the
memory consumption not the sorting block size (added in ClickHouse#71406)

Signed-off-by: Azat Khuzhin <a.khuzhin@semrush.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-improvement Pull request with some product improvements pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Adaptive thresholds for spilling to disk

6 participants