Skip to content

Make max_bytes_before_external_sort limit depends on total query memory consumption#72598

Merged
alexey-milovidov merged 1 commit intoClickHouse:masterfrom
azat:max_bytes_before_external_sort-threads
Jan 2, 2025
Merged

Make max_bytes_before_external_sort limit depends on total query memory consumption#72598
alexey-milovidov merged 1 commit intoClickHouse:masterfrom
azat:max_bytes_before_external_sort-threads

Conversation

@azat
Copy link
Copy Markdown
Member

@azat azat commented Nov 28, 2024

Changelog category (leave one):

  • Improvement

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

Make max_bytes_before_external_sort limit depends on total query memory consumption (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

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 #71406)

P.S. maybe it will be better to mark it as Backward Incompatible Change to highlight this change in changelogs

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

robot-ch-test-poll4 commented Nov 28, 2024

This is an automated comment for commit a7e97aa 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

@alexey-milovidov
Copy link
Copy Markdown
Member

This is ok, but it will be much better to use the same approach as in Aggregator - to use the total memory usage of the query.

@alexey-milovidov alexey-milovidov self-assigned this Nov 30, 2024
@azat azat force-pushed the max_bytes_before_external_sort-threads branch from d47232f to 7eb25c1 Compare December 1, 2024 18:55
@azat azat changed the title Align max_bytes_before_external_sort with parallel sorting Make max_bytes_before_external_sort more user-friendly Dec 1, 2024
@azat
Copy link
Copy Markdown
Member Author

azat commented Dec 1, 2024

This is ok, but it will be much better to use the same approach as in Aggregator - to use the total memory usage of the query.

Done (was waiting for the #71406, which adds the helper)

@azat azat force-pushed the max_bytes_before_external_sort-threads branch from de45f52 to 9441e74 Compare December 4, 2024 11:03
@azat
Copy link
Copy Markdown
Member Author

azat commented Dec 4, 2024

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.

@azat azat force-pushed the max_bytes_before_external_sort-threads branch from 84d7f5a to 608c7ed Compare December 4, 2024 16:38
@azat azat marked this pull request as draft December 11, 2024 17:00
@alexey-milovidov
Copy link
Copy Markdown
Member

Thanks, this sounds good - let's continue.

@azat
Copy link
Copy Markdown
Member Author

azat commented Dec 25, 2024

Right now CI is unstable for external sort, I'm waiting for #73097 to be merged (hope it will help)

@azat azat changed the title Make max_bytes_before_external_sort more user-friendly Make max_bytes_before_external_sort limit depends on total query memory consumption Dec 30, 2024
@azat azat force-pushed the max_bytes_before_external_sort-threads branch from 90ec40e to f347332 Compare December 30, 2024 20:52
@azat azat marked this pull request as ready for review December 30, 2024 20:52
…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 azat force-pushed the max_bytes_before_external_sort-threads branch from f347332 to a7e97aa Compare December 31, 2024 21:20
@azat
Copy link
Copy Markdown
Member Author

azat commented Jan 2, 2025

Test failures does not looks related:

Integration tests (aarch64) [6/6] — fail: 2, passed: 552

>           raise QueryRuntimeException(
                "Client failed! Return code: {}, stderr: {}".format(
                    self.process.returncode, stderr
                ),
                self.process.returncode,
                stderr,
            )
E           helpers.client.QueryRuntimeException: Client failed! Return code: 210, stderr: Code: 210. DB::NetException: Net Exception: No route to host (172.16.5.2:9000). (NETWORK_ERROR)
>       assert node.contains_in_log(
            "because of the race with list & delete"
        ) or node_2.contains_in_log("because of the race with list & delete")
E       AssertionError: assert (False or False)
E        +  where False = <bound method ClickHouseInstance.contains_in_log of <helpers.cluster.ClickHouseInstance object at 0xffce7c2384f0>>('because of the race with list & delete')
E        +    where <bound method ClickHouseInstance.contains_in_log of <helpers.cluster.ClickHouseInstance object at 0xffce7c2384f0>> = <helpers.cluster.ClickHouseInstance object at 0xffce7c2384f0>.contains_in_log
E        +  and   False = <bound method ClickHouseInstance.contains_in_log of <helpers.cluster.ClickHouseInstance object at 0xffce7c238520>>('because of the race with list & delete')
E        +    where <bound method ClickHouseInstance.contains_in_log of <helpers.cluster.ClickHouseInstance object at 0xffce7c238520>> = <helpers.cluster.ClickHouseInstance object at 0xffce7c238520>.contains_in_log

Integration tests (tsan) [3/6] — fail: 1, passed: 558

  • test_keeper_three_nodes_two_alive/test.py::test_restart_third_node
>               raise self._exception
E               kazoo.exceptions.ConnectionLoss

@alexey-milovidov alexey-milovidov merged commit 04cae9d into ClickHouse:master Jan 2, 2025
@azat azat deleted the max_bytes_before_external_sort-threads branch January 2, 2025 14:50
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jan 2, 2025
azat added a commit to azat/ClickHouse that referenced this pull request Jan 2, 2025
Follow-up for: ClickHouse#72598
Signed-off-by: Azat Khuzhin <a.khuzhin@semrush.com>
azat added a commit to azat/ClickHouse that referenced this pull request May 24, 2025
…xternal ORDER BY

In ClickHouse#72598 the behavior of `max_bytes_before_external_sort` has been
changed, it has been switched to rely on the total memory usage of the
query instead of the size of a sorting data.

But, the problem with this approach is that you can have a lot of memory
used outside of sorting (FINAL, aggregation), and spilling sorting
blocks to disk will not help reduce memory usage obviously, but it will
make things even worse, because it will create lots of small files, that
will require a lot of memory to merge them.

So this patch reverts the behavior of `max_bytes_before_external_sort`
back, now it will rely on the sorting block size.
{"allow_general_join_planning", false, true, "Allow more general join planning algorithm when hash join algorithm is enabled."},
{"optimize_extract_common_expressions", false, false, "Introduce setting to optimize WHERE, PREWHERE, ON, HAVING and QUALIFY expressions by extracting common expressions out from disjunction of conjunctions."},
{"max_bytes_ratio_before_external_sort", 0., 0., "New setting."},
{"min_external_sort_block_bytes", 0., 100_MiB, "New setting."},
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

The default value is very bad, but, it will be removed anyway - #80777

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.

4 participants