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

Fix excessive memory usage for FINAL (due to too much streams usage) #50429

Merged
merged 1 commit into from Jun 7, 2023

Conversation

azat
Copy link
Collaborator

@azat azat commented Jun 1, 2023

Changelog category (leave one):

  • Performance Improvement

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

Fix excessive memory usage for FINAL (due to too much streams usage)

Previously it could create MergeTreeInOrder for each mark, however this could be very suboptimal, due to each MergeTreeInOrder has some memory overhead.

Now, by collapsing all marks for one part together it is more memory effiecient.

I've tried the query from the altinity wiki 1 and it decreases memory usage twice:

SELECT * FROM repl_tbl FINAL WHERE key IN (SELECT toUInt32(number) FROM numbers(1000000) WHERE number % 50000 = 0) FORMAT Null
  • upstream: MemoryTracker: Peak memory usage (for query): 520.27 MiB.
  • patched: MemoryTracker: Peak memory usage (for query): 260.95 MiB.

And it could be not 2x and even more or less, it depends on the gaps in marks for reading (for example in my setup the memory usage increased a lot, from ~16GiB of RAM to >64GiB due to lots of marks and gaps).

Cc: @nickitat
Cc: @UnamedRus (you may want to run your queries)

Note: this pops up after a proper marks skipping for FINAL in #47801

P.S. Marked as bug fix to run tests on previous version.

@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-bugfix Pull request with bugfix, not backported by default label Jun 1, 2023
@robot-clickhouse-ci-2
Copy link
Contributor

robot-clickhouse-ci-2 commented Jun 1, 2023

This is an automated comment for commit 228ebab with description of existing statuses. It's updated for the latest CI running
The full report is available here
The overall status of the commit is 🔴 failure

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
Bugfix validate checkChecks that either a new test (functional or integration) or there some changed tests that fail with the binary built on master branch🟢 success
CI runningA meta-check that indicates the running CI. Normally, it's in success or pending state. The failed status indicates some problems with the PR🟡 pending
ClickHouse build checkBuilds ClickHouse in various configurations for use in further steps. You have to fix the builds that fail. Build logs often has enough information to fix the error, but you might have to reproduce the failure locally. The cmake options can be found in the build log, grepping for cmake. Use these options and follow the general build process🟢 success
Compatibility checkChecks that clickhouse binary runs on distributions with old libc versions. If it fails, ask a maintainer for help🟢 success
Docker image for serversThe check to build and optionally push the mentioned image to docker hub🟢 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. Integrational 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
Integration testsThe integration tests report. In parenthesis the package type is given, and in square brackets are the optional part/total tests🟢 success
Mergeable CheckChecks if all other necessary checks are successful🟢 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
Push to DockerhubThe check for building and pushing the CI related docker images to docker hub🟢 success
SQLancerFuzzing tests that detect logical bugs with SQLancer tool🟢 success
SqllogicRun clickhouse on the sqllogic test set against sqlite and checks that all statements are passed🟢 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🔴 failure
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🔴 failure

@nickitat nickitat self-assigned this Jun 1, 2023
Previously it could create MergeTreeInOrder for each mark, however this
could be very suboptimal, due to each MergeTreeInOrder has some memory
overhead.

Now, by collapsing all marks for one part together it is more memory
effiecient.

I've tried the query from the altinity wiki [1] and it decreases memory
usage twice:

    SELECT * FROM repl_tbl FINAL WHERE key IN (SELECT toUInt32(number) FROM numbers(1000000) WHERE number % 50000 = 0) FORMAT Null

- upstream: MemoryTracker: Peak memory usage (for query): 520.27 MiB.
- patched:  MemoryTracker: Peak memory usage (for query): 260.95 MiB.

  [1]: https://kb.altinity.com/engines/mergetree-table-engine-family/replacingmergetree/#multiple-keys

And it could be not 2x and even more or less, it depends on the gaps in
marks for reading (for example in my setup the memory usage increased a
lot, from ~16GiB of RAM to >64GiB due to lots of marks and gaps).

Signed-off-by: Azat Khuzhin <a.khuzhin@semrush.com>
@azat
Copy link
Collaborator Author

azat commented Jun 3, 2023

Stateless tests (release) — fail: 1, passed: 5315, skipped: 6

Stateless tests (release, wide parts enabled) — fail: 1, passed: 5313, skipped: 8

  • 02780_final_streams_data_skipping_index - related, fixed

@azat
Copy link
Collaborator Author

azat commented Jun 4, 2023

Stateless tests (release, analyzer) — fail: 1, passed: 5310, skipped: 16

Stateless tests (release, s3 storage) [2/2] — fail: 1, passed: 2662, skipped: 28

@azat azat requested a review from nickitat June 7, 2023 09:41
result_layers.emplace_back();
auto & current_layer = result_layers.emplace_back();
/// Map part_idx into index inside layer, used to merge marks from the same part into one reader
std::unordered_map<size_t, size_t> part_idx_in_layer;
Copy link
Member

Choose a reason for hiding this comment

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

shouldn't it be just ssize_t last_part_idx?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Looks like this will not be enough, since parts_ranges_queue is sorted by primary key value not by the part, plus there are modifications of this queue inside this loop.

Copy link
Member

Choose a reason for hiding this comment

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

yep, I misunderstood your approach initially.

Copy link
Member

@nickitat nickitat left a comment

Choose a reason for hiding this comment

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

nice improvement, thanks

result_layers.emplace_back();
auto & current_layer = result_layers.emplace_back();
/// Map part_idx into index inside layer, used to merge marks from the same part into one reader
std::unordered_map<size_t, size_t> part_idx_in_layer;
Copy link
Member

Choose a reason for hiding this comment

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

yep, I misunderstood your approach initially.

@nickitat nickitat merged commit 036ddcd into ClickHouse:master Jun 7, 2023
259 of 264 checks passed
@azat azat deleted the fix-final-memory-usage branch June 7, 2023 12:15
@li-zhao-hotstar
Copy link

thanks for the fix!

do we know the plan this can be released / backported? since the LTS release 23.3 contains #47801, we had observed critical memory issues with it.

@robot-ch-test-poll1 robot-ch-test-poll1 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Sep 6, 2023
Enmk pushed a commit to Altinity/ClickHouse that referenced this pull request Sep 20, 2023
…lickHouse#50429)

Previously it could create MergeTreeInOrder for each mark, however this
could be very suboptimal, due to each MergeTreeInOrder has some memory
overhead.

Now, by collapsing all marks for one part together it is more memory
effiecient.

I've tried the query from the altinity wiki [1] and it decreases memory
usage twice:

    SELECT * FROM repl_tbl FINAL WHERE key IN (SELECT toUInt32(number) FROM numbers(1000000) WHERE number % 50000 = 0) FORMAT Null

- upstream: MemoryTracker: Peak memory usage (for query): 520.27 MiB.
- patched:  MemoryTracker: Peak memory usage (for query): 260.95 MiB.

  [1]: https://kb.altinity.com/engines/mergetree-table-engine-family/replacingmergetree/#multiple-keys

And it could be not 2x and even more or less, it depends on the gaps in
marks for reading (for example in my setup the memory usage increased a
lot, from ~16GiB of RAM to >64GiB due to lots of marks and gaps).

Signed-off-by: Azat Khuzhin <a.khuzhin@semrush.com>
Enmk added a commit to Altinity/ClickHouse that referenced this pull request Sep 21, 2023
…l-memory-usage

23.3 Backport of ClickHouse#50429 - Fix excessive memory usage for FINAL (due to too much streams usage)
MyroTk pushed a commit to Altinity/ClickHouse that referenced this pull request Sep 21, 2023
…lickHouse#50429)

Previously it could create MergeTreeInOrder for each mark, however this
could be very suboptimal, due to each MergeTreeInOrder has some memory
overhead.

Now, by collapsing all marks for one part together it is more memory
effiecient.

I've tried the query from the altinity wiki [1] and it decreases memory
usage twice:

    SELECT * FROM repl_tbl FINAL WHERE key IN (SELECT toUInt32(number) FROM numbers(1000000) WHERE number % 50000 = 0) FORMAT Null

- upstream: MemoryTracker: Peak memory usage (for query): 520.27 MiB.
- patched:  MemoryTracker: Peak memory usage (for query): 260.95 MiB.

  [1]: https://kb.altinity.com/engines/mergetree-table-engine-family/replacingmergetree/#multiple-keys

And it could be not 2x and even more or less, it depends on the gaps in
marks for reading (for example in my setup the memory usage increased a
lot, from ~16GiB of RAM to >64GiB due to lots of marks and gaps).

Signed-off-by: Azat Khuzhin <a.khuzhin@semrush.com>
@alexey-milovidov
Copy link
Member

@azat This isn't a bug fix, and it shouldn't have been backported.

@alexey-milovidov alexey-milovidov removed the pr-bugfix Pull request with bugfix, not backported by default label Oct 18, 2023
@azat
Copy link
Collaborator Author

azat commented Oct 19, 2023

@alexey-milovidov I've marked it as bug fix to run tests on previous version, and AFAIK there is no automatic backports of the bug fixes anyway, so this had been done explicitly and not by me.

@azat
Copy link
Collaborator Author

azat commented Oct 19, 2023

And what was the problem with the backport? Did it create any bugs?

@alexey-milovidov
Copy link
Member

@azat excessive backports compromise stability by stealing attention from important changes.

@UnamedRus
Copy link
Contributor

AFAIK, there was a discussion and complain, about why people stuck to old versions of ClickHouse and not newer one, which is still in 1 year LTS support.

And if "newer" version of ClickHouse have regressions compared to older ones, which makes them 10-100 times slower for them, it's the answer why this (keeping to older versions) will continue to happen and people will look away from using newer releases.

And no, upgrade to latest version, which have all fixes is not an answer for them, #49410 #55643

@alexey-milovidov
Copy link
Member

We should not do these types of backports.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore v23.3-must-backport
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants