Skip to content

Correct MemoryTracker with MemoryWorker - #75714

Merged
antonio2368 merged 1 commit into
masterfrom
memory-worker-correct-memory-tracker
Feb 10, 2025
Merged

antonio2368 merged 1 commit into
masterfrom
memory-worker-correct-memory-tracker

Conversation

@antonio2368

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Improvement

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

Add a config memory_worker_correct_memory_tracker to enable correction of internal memory tracker with information from different source read in the background thread periodically.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

@clickhouse-gh

clickhouse-gh Bot commented Feb 7, 2025

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [b6b6556]

@clickhouse-gh clickhouse-gh Bot added the pr-improvement Pull request with some product improvements label Feb 7, 2025
@divanik divanik added pr-must-backport Pull request should be backported intentionally. Use this label with great care! pr-must-backport-cloud labels Feb 7, 2025
@divanik

divanik commented Feb 7, 2025

Copy link
Copy Markdown
Contributor

LGTM, but since this is a critical core part of the code I think review of more experienced engineer is also required

/// resident memory can be much larger than the actual allocated memory
/// so we rather ignore the potential difference caused by allocated memory
/// before MemoryTracker initialization
if (unlikely(total_memory_tracker.get() < 0) || correct_tracker)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I doubt that it makes any difference (updating for the first run or not), but anyway this is minor

@antonio2368
antonio2368 added this pull request to the merge queue Feb 10, 2025
Merged via the queue into master with commit f9b881e Feb 10, 2025
@antonio2368
antonio2368 deleted the memory-worker-correct-memory-tracker branch February 10, 2025 08:48
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Feb 10, 2025
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-backports-created-cloud deprecated label, NOOP label Feb 10, 2025
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Feb 10, 2025
robot-ch-test-poll added a commit that referenced this pull request Feb 10, 2025
Backport #75714 to 24.11: Correct `MemoryTracker` with `MemoryWorker`
@azat azat self-assigned this Feb 10, 2025
@azat azat mentioned this pull request Feb 11, 2025
1 task
antonio2368 added a commit that referenced this pull request Feb 13, 2025
Backport #75714 to 25.1: Correct `MemoryTracker` with `MemoryWorker`
antonio2368 added a commit that referenced this pull request Feb 13, 2025
Backport #75714 to 24.12: Correct `MemoryTracker` with `MemoryWorker`
@robot-clickhouse robot-clickhouse added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Jul 2, 2025
pull Bot pushed a commit to SINHASantos/ClickHouse that referenced this pull request Sep 28, 2026
`memory_worker_correct_memory_tracker` has been `0` since it was introduced in
ClickHouse#75714, which leaves the global
`MemoryTracker` a pure counter: `MemoryWorker` refreshes only `rss` on its tick
and rewrites `amount` on its first tick and whenever the tracker goes negative.
An upward drift is therefore permanent for the lifetime of the process — the
memory it describes has already been freed, so no later deallocation can work
it off. Once the drift alone exceeds `max_server_memory_usage`, every
allocation fails, down to the zero-byte `CurrentMemoryTracker::check` at the
start of a connection, and the server rejects every query while using a
fraction of its limit.

That is not hypothetical: it accounts for 10 of the 21 hung-check failures
carrying a memory-limit message in the last 30 days of `default.checks`, and
for two open issues. In `Stress test (arm_release)` on master
`75247055068ba70941661c930840ee625a991adc` the tracker read `164.75 GiB`,
unchanged for the six minutes until the process was killed, while `current RSS`
was `7.16 GiB` against a limit of `47.64 GiB`.

The measurement the correction uses is the same quantity the hard-limit check
already enforces through its `will_be_rss > current_hard_limit` branch, so
making `amount` agree with it cannot reject an allocation that is accepted
today; it can only stop rejecting allocations that no memory justifies. The
drift stays observable: `MemoryTrackingUncorrected` keeps the uncorrected
value, so its difference from `MemoryTracking` is the drift accumulated so far.
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 pr-backports-created-cloud deprecated label, NOOP pr-improvement Pull request with some product improvements pr-must-backport Pull request should be backported intentionally. Use this label with great care! pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR 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.

7 participants