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

Consider nested cgroup memory limits for the total memory tracker #59435

Merged

Conversation

rschu1ze
Copy link
Member

@rschu1ze rschu1ze commented Jan 31, 2024

Review note: Contains commits

  • 92a00d3 - Cosmetics.
  • ad0c0d8 - Cosmetics but also fixes the edge case that cgroup memory limit "0" was incorrectly interpreted as "no limit" but it really means "zero" memory.
  • 3243ea0 Kills cgroups v1 supprt for simplicity. V1 has a different hierarchy model which would have made the next commit more complex. Also, v1 is dead since over half a decade.
  • e997168 See the changelog message
  • EDIT: 80a0e05 Resurrects cgroups v1. They are still enabled in many Linux distributions in GCP, AWS and Azure :-(
  • ad13a57 Improves the detection of v2 features a bit

Changelog category (leave one):

  • Improvement

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

At startup and whenever config files are changed, ClickHouse updates the hard memory limits of its total memory tracker. These limits are computed based on various server settings and cgroups limits (on Linux). Previously, setting /sys/fs/cgroup/memory.max (for cgroups v2) was hard-coded. As a result, cgroup v2 memory limits configured for nested groups (hierarchies), e.g. /sys/fs/cgroup/my/nested/group/memory.max were ignored. This is now fixed. The behavior of v1 memory limits remains unchanged.

@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-improvement Pull request with some product improvements label Jan 31, 2024
@robot-clickhouse-ci-1
Copy link
Contributor

robot-clickhouse-ci-1 commented Jan 31, 2024

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

@nickitat nickitat self-assigned this Jan 31, 2024
base/base/getMemoryAmount.cpp Show resolved Hide resolved
base/base/getMemoryAmount.cpp Outdated Show resolved Hide resolved
This fixes the edge case that cgroup memory limit "0" is no longer
interpreted as "no limit" but as actual zero available memory, see (*).
The cgroup default setting "max" continues to be interpreted as "no
limit".

(*) https://docs.kernel.org/admin-guide/cgroup-v2.html
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.

it is not particularly relevant to this pr, but in general seems like there are quite a few places when any logging might be useful

base/base/getMemoryAmount.cpp Show resolved Hide resolved
@robot-ch-test-poll4 robot-ch-test-poll4 merged commit 3af96c6 into ClickHouse:master Feb 6, 2024
267 checks passed
@Stanislav-Vlasov
Copy link

I have a problem, I think, related to this change. I'm running the Clickhouse docker image version 24.2.2.71 (official build) on Ubuntu 22.04 (cgroup v2 enabled by default, no cgroup v1) with error in log. This error did not occur when I ran the previous version 24.1 of Clickhouse.

Application: Disabling cgroup memory observer because of an error during initialization: Code: 107. DB::Exception: Cannot find cgroups v1 or v2 current memory file. (FILE_DOESNT_EXIST), Stack trace (when copying this message, always include the lines below):

  1. DB::Exception::Exception(DB::Exception::MessageMasked&&, int, bool) @ 0x000000000cf5af1b
  2. DB::Exception::Exception<>(int, FormatStringHelperImpl<>) @ 0x00000000078cc583
  3. DB::CgroupsMemoryUsageObserver::CgroupsMemoryUsageObserver(std::chrono::duration<long long, std::ratio<1l, 1l>>) @ 0x000000000d11da73
  4. DB::Server::main(std::vector<String, std::allocator> const&) @ 0x000000000d0cfedd
  5. Poco::Util::Application::run() @ 0x00000000158c9606
  6. DB::Server::run() @ 0x000000000d0c0551
  7. Poco::Util::ServerApplication::run(int, char**) @ 0x00000000158d2479
  8. mainEntryClickHouseServer(int, char**) @ 0x000000000d0bd60a
  9. main @ 0x00000000078c8e18
  10. ? @ 0x00007f69f6e6d083
  11. _start @ 0x00000000060c686e
    (version 24.2.2.71 (official build))

Additional information about my HOST system

  1. uname -a
    Linux 6.2.0-37-generic 22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Thu Nov 2 18:01:13 UTC 2 x86_64 x86_64 x86_64 GNU/Linux

  2. docker -v
    Docker version 24.0.2, build cb74dfc

Additional information from the inside of running container

  1. cat /proc/1/cgroup
    0::/

  2. ls /sys/fs/cgroup
    cgroup.controllers
    cgroup.events
    cgroup.freeze
    cgroup.kill
    cgroup.max.depth
    cgroup.max.descendants
    cgroup.pressure
    cgroup.procs
    cgroup.stat
    cgroup.subtree_control
    cgroup.threads
    cgroup.type
    cpu.idle
    cpu.max
    cpu.max.burst
    cpu.pressure
    cpuset.cpus
    cpuset.cpus.effective
    cpuset.cpus.partition
    cpuset.mems
    cpuset.mems.effective
    cpu.stat
    cpu.uclamp.max
    cpu.uclamp.min
    cpu.weight
    cpu.weight.nice
    hugetlb.1GB.current
    hugetlb.1GB.events
    hugetlb.1GB.events.local
    hugetlb.1GB.max
    hugetlb.1GB.numa_stat
    hugetlb.1GB.rsvd.current
    hugetlb.1GB.rsvd.max
    hugetlb.2MB.current
    hugetlb.2MB.events
    hugetlb.2MB.events.local
    hugetlb.2MB.max
    hugetlb.2MB.numa_stat
    hugetlb.2MB.rsvd.current
    hugetlb.2MB.rsvd.max
    io.bfq.weight
    io.max
    io.pressure
    io.prio.class
    io.stat
    io.weight
    memory.current
    memory.events
    memory.events.local
    memory.high
    memory.low
    memory.max
    memory.min
    memory.numa_stat
    memory.oom.group
    memory.peak
    memory.pressure
    memory.reclaim
    memory.stat
    memory.swap.current
    memory.swap.events
    memory.swap.high
    memory.swap.max
    memory.zswap.current
    memory.zswap.max
    misc.current
    misc.events
    misc.max
    pids.current
    pids.events
    pids.max
    pids.peak
    rdma.current
    rdma.max

@Stanislav-Vlasov
Copy link

As you can see, there are no nested cgroups in /sys/fs/cgroup

Due to problem of "Disabling cgroup memory observer", the available memory is incorrectly determined. Clickhouse believes that all HOST memory is available without respect of cgroup limit. This causes container memory overruns and triggers OOM killer.

@rschu1ze
Copy link
Member Author

@Stanislav-Vlasov First, can you please open a GitHub issue for that? That would be a better place for discussion than the original PR.

As a workaround, you could set a sensible value for server setting max_server_memory_usage (docs). The error just means that ClickHouse will not react to memory limit changes done via cgroups.

Exception Cannot find cgroups v1 or v2 current memory file indicates that during database startup, neither a cgroups v1 nor v2 file was found. It would be interesting to debug this, perhaps you have a repro, e.g. the steps you use to invoke docker?

For completeness, I tested the latest official Docker image (v24.3, Ubuntu 22.04 LTS) and it worked for me, i.e. no errors.

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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants