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

Update memory tracker periodically with cgroup memory usage #59516

Merged
merged 10 commits into from
Feb 26, 2024

Conversation

rschu1ze
Copy link
Member

@rschu1ze rschu1ze commented Feb 2, 2024

When the system runs out of memory (as determined by system-wide settings or cgroups memory thresholds), the OOM killer stops memory-intensive processes. Due to its nature, ClickHouse will often be the victim. To avoid this, the upper limit of the (global) memory tracker in ClickHouse can be configured to a value smaller than the "kill threshold", see setting max_server_memory_usage_ram_ratio.

There are some problems with this approach:

  • The maximum memory threshold of the processes's cgroup can be changed (increased, decreased) while ClickHouse runs. An earlier fix (Respect cgroup memory limit when reloading configuration #53455) ensured that new memory limits were at least considered when ClickHouse's configuration changes (a follow-up fix Consider nested cgroup memory limits for the total memory tracker #59435 resolved problems with nested cgroups). Of course, users need to make real or dummy configuration changes for this to happen and it may already be too late at this point.

  • The memory tracking in ClickHouse is not 100% precise. For example, ClickHouse may think it uses less memory than the OS thinks. In that case, there is a risk of getting OOM-killed. Or the other way round, ClickHouse produces OOMs even if there is sufficient memory.

This PR tries to address this by reading the current memory consumption as seen by cgroups periodically and running two possible actions:

  • if actual memory consumption (as read from cgroups) exceeds a soft threshold, purge jemalloc preallocated arenas. This should bring the OS's and ClickHouse's view of the memory consumption closer together.
  • if a hard threshold is exceeded, adjust (usually lower) the hard limit in the memory tracker to the exceeded threshold. This increases the chance for OOMs in ClickHouse but decreases the risk of getting OOM killed.

The read interval (setting cgroups_memory_usage_observer_wait_time) is 15 sec by default and it can be reduced to 1 sec if needed. 0 means disabled. The parameter cannot be changed after database start.

The hard and soft limits are controlled by parameters cgroup_memory_watcher_hard_limit_ratio and cgroup_memory_watcher_soft_limit_ratio (defaults: 0.95, 0.9). They each refer to the initial maximum allowed global memory consumption computed as total system memory x max_server_memory_usage_to_ram_ratio. Both parameters can be changed at runtime.

Changelog category (leave one):

  • Not for changelog (changelog entry is not required)

@robot-ch-test-poll robot-ch-test-poll added the pr-not-for-changelog This PR should not be mentioned in the changelog label Feb 2, 2024
@robot-ch-test-poll
Copy link
Contributor

robot-ch-test-poll commented Feb 2, 2024

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

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

Successful checks
Check nameDescriptionStatus
A SyncThere's no description for the check yet, please add it to tests/ci/ci_config.py:CHECK_DESCRIPTIONS✅ success
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
ClickBenchRuns [ClickBench](https://github.com/ClickHouse/ClickBench/) with instant-attach table✅ success
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 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. 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
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
SQLTestThere's no description for the check yet, please add it to tests/ci/ci_config.py:CHECK_DESCRIPTIONS✅ 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✅ 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
Check nameDescriptionStatus
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
Integration testsThe integration tests report. In parenthesis the package type is given, and in square brackets are the optional part/total tests❌ failure
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

@hanfei1991 hanfei1991 self-assigned this Feb 2, 2024
@rschu1ze rschu1ze force-pushed the cgroup-observing-memory-tracker branch from 665dbde to e292c97 Compare February 2, 2024 14:20
@rschu1ze

This comment was marked as outdated.

@rschu1ze rschu1ze force-pushed the cgroup-observing-memory-tracker branch from e292c97 to c2cd4e7 Compare February 5, 2024 15:00
@rschu1ze rschu1ze force-pushed the cgroup-observing-memory-tracker branch from c2cd4e7 to 50bdb87 Compare February 7, 2024 12:17
@rschu1ze rschu1ze changed the title *wip* Cgroup-observing memory tracker Update memory tracker periodically with cgroup memory usage Feb 7, 2024
@rschu1ze rschu1ze force-pushed the cgroup-observing-memory-tracker branch from 50bdb87 to 42ba6ca Compare February 7, 2024 22:39
@rschu1ze rschu1ze marked this pull request as ready for review February 8, 2024 11:58
@rschu1ze rschu1ze force-pushed the cgroup-observing-memory-tracker branch 2 times, most recently from 2b18995 to 3a1ba69 Compare February 8, 2024 15:20
@rschu1ze rschu1ze force-pushed the cgroup-observing-memory-tracker branch from 3a1ba69 to f6f1fd4 Compare February 8, 2024 16:06
@rschu1ze rschu1ze merged commit 048f7ec into ClickHouse:master Feb 26, 2024
231 of 238 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pr-backports-created-cloud pr-must-backport-cloud pr-not-for-changelog This PR should not be mentioned in the changelog 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.

None yet

6 participants