Repository navigation
Fix the global memory tracker going negative after a refused allocation over max_server_memory_usage - #127054
Fix the global memory tracker going negative after a refused allocation over max_server_memory_usage#127054groeneai wants to merge 2 commits into
max_server_memory_usage#127054Conversation
…acker corrections The global memory tracker charges an allocation before comparing it with the hard limit, and keeps the charge while it shrinks the caches and waits in the overcommit tracker (up to memory_usage_overcommit_max_wait_microseconds) for another query to free memory. Each tick of the memory worker replaces `rss` (always) and `amount` (every tick with memory_worker_correct_memory_tracker, the default since ClickHouse#118094) with a measurement, which cannot contain a charge that has no allocation behind it yet. When the allocation is then refused, the rollback subtracts it from counters that no longer hold it, and the tracker stays that far below the truth until the next tick, so the server limit admits allocations it should refuse. In an AST fuzzer run refused 64 GiB allocations drove the tracker to -500 GiB and the server was OOM-killed at 43 GiB RSS under a 40 GiB limit. The global tracker now counts the charges inside its over-limit branch, and updateAllocated and updateRSS add them to the measurement. The metric and the peak keep the measured value: a pending charge is added to the metric only when it is committed, and a refused one must never become a peak. During an overcommit wait the charge now also survives the ticks, so the overcommit tracker stops the query it picked, as it did with the correction off. A refusal rolls its charge back and leaves the pending count under the mutex that updateAllocated and updateRSS hold while they read the count and replace the counter, so a correction cannot bring a rolled-back charge back. The count is published with release and read with acquire, so a correction that sees a charge also sees it in the counters it replaces. A worker tick within a few instructions of the charge can still miss that allocation until the next correction; closing that needs more atomics on every global allocation. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Move the pre-existing uncorrected_amount carry comment back above the line it explains. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Internal second-model review (5 rounds, final on 3fae7eb: 0 open findings)
|
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-6:20261020-220800 |
|
cc @azat @antonio2368, could you review this and add the |
|
Workflow [PR], commit [f083d42] Summary: ✅
AI ReviewSummaryThe PR preserves pending global allocation charges across memory-worker corrections and serializes refusal rollback with those corrections, preventing the allocation-sized undercount after an overcommit wait. No actionable findings in head Missing context / blind spots
Final VerdictStatus: ✅ Approve. |
azat
left a comment
There was a problem hiding this comment.
This increases the complexity and does not fixes the issue 100%, there is alternative that allows to avoid mutex but, it is also not 100%
And this should be taken with care, since MemoryTracker can be a bottleneck
|
Agreed. The new atomic and the mutex are only on the over-limit branch of Which do you prefer?
|
Related: #118094
Related: #126176
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed the global memory tracker going negative by the size of an allocation that
max_server_memory_usagerefused after waiting for memory to be freed, whenmemory_worker_correct_memory_trackeris enabled (the default since 26.10). While it was negative, the server memory limit admitted allocations it should have refused, and the server could be killed by the OOM killer.Description
On 26.9.19.9 with
memory_worker_correct_memory_tracker = 1, a query whose 4.47 GiB allocationmax_server_memory_usagerefuses after an overcommit wait leaves the global memory tracker at about -4.2 GiB (3 of 3 runs), so until the next memory worker tick the server limit admits allocations it should refuse.The global tracker charges an allocation before checking the limit and holds the charge while it waits for memory to be freed. Each memory worker tick replaces
amountandrsswith a measurement, which cannot contain that charge, so the refusal then subtracts the allocation from counters that no longer hold it.The global tracker now counts the charges it is still deciding on, and
updateAllocatedandupdateRSSkeep them on top of the measurement (the metric and the peak keep the measured value); a refusal's rollback and a correction take one mutex. The waiting charge thus stays in the tracker, and the overcommit tracker stops the query it picked, as it does with the correction off.CI: refused 64 GiB allocations drove the tracker to -500.84 GiB, then the server was OOM-killed at 43 GiB RSS under a 40 GiB limit (AST fuzzer on #123890).
Validation: two gtests in
gtest_memory_tracker.cpprun a worker correction inside the over-limit decision, for a refused and for an admitted allocation; both fail on master by exactly the allocation size and pass with the fix. With the fix, the end-to-end repro shows no negative correction.A worker tick within a few instructions of the charge can still miss that allocation until the next correction; closing that needs more atomics on every global allocation.
Workflow [PR]
Sync PR [sync-upstream/pr/127054]