Skip to content

Fix memory tracking drift from background schedule pool and executor - #84946

Merged
azat merged 1 commit into
ClickHouse:masterfrom
azat:memory-tracking-threads-drift
Aug 2, 2025
Merged

azat merged 1 commit into
ClickHouse:masterfrom
azat:memory-tracking-threads-drift

Conversation

@azat

@azat azat commented Aug 2, 2025 •

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

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

Fix memory tracking drift from background schedule pool and executor

It has been reported (and I also noticed it few months ago) that it is possible to see negative MemoryTracking metric, while this should never be possible after bunch of fixes. It is not big, ~100MiB per 1 hour, but it still annoying.

But, huge thread pools, that reserve all threads at start may lead to this, since each of them will have a drift up to 4MiB (it is not depends on the max_untracked_memory, since we do not apply it for all threads at start).

And indeed I've tested this and with
background_schedule_pool_size=10000, and I see that MemoryTracking can go down, while after this patch, it is almost constant.

And I hope this is the final bits of memory tracking drift!

Note: workaround to mitigate this drift is to decrease background pool sizes, i.e. background_schedule_pool_size=10, but this is not recommended for production usage

screenshots

memory-tracking-before-docker-vanilla

memory-tracking-before-docker-vanilla

memory-tracking-before-patch-10k-1h

memory-tracking-before-patch-10k-1h

memory-tracking-after-patch-10k-2h

memory-tracking-after-patch-10k-2h

Fixes: #82036
Cc: @filimonov

It has been reported (and I also noticed it few months ago) that it is
possible to see negative MemoryTracking metric, while this should never
be possible after bunch of fixes. It is not big, ~100MiB per 1 hour, but
it still annoying.

But, huge thread pools, that reserve all threads at start may lead to
this, since each of them will have a drift up to 4MiB (it is not depends
on the `max_untracked_memory`, since we do not apply it for all
threads at start).

And indeed I've tested this and with
background_schedule_pool_size=10000, and I see that MemoryTracking can
goes down, while after this patch it is almost constant.

**And I hope this is the final bits of memory tracking drift!**

Fixes: ClickHouse#82036
@azat
azat requested review from al13n321 and antonio2368 August 2, 2025 13:36
@clickhouse-gh

clickhouse-gh Bot commented Aug 2, 2025 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [cc1606d]

Summary: ❌

job_name test_name status info comment
Stateless tests (amd_binary, ParallelReplicas, s3 storage, parallel) failure
Logical error thrown (see clickhouse-server.log or logical_errors.txt) FAIL
Stateless tests (amd_tsan, s3 storage, parallel) failure
00385_storage_file_and_clickhouse-local_app_long FAIL
Finish Workflow failure
python3 ./ci/jobs/scripts/workflow_hooks/new_tests_check.py failure

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Aug 2, 2025
@alexey-milovidov alexey-milovidov self-assigned this Aug 2, 2025
@azat
azat enabled auto-merge August 2, 2025 18:39
@azat
azat added this pull request to the merge queue Aug 2, 2025
Merged via the queue into ClickHouse:master with commit 1627b35 Aug 2, 2025
@azat
azat deleted the memory-tracking-threads-drift branch August 2, 2025 18:56
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label Aug 2, 2025
@azat azat added the pr-must-backport Pull request should be backported intentionally. Use this label with great care! label Aug 2, 2025
@azat

azat commented Aug 2, 2025

Copy link
Copy Markdown
Member Author

I am going to backport this change

robot-clickhouse added a commit that referenced this pull request Aug 2, 2025
robot-clickhouse added a commit that referenced this pull request Aug 2, 2025
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added pr-backports-created-cloud deprecated label, NOOP pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR labels Aug 2, 2025
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Aug 2, 2025
clickhouse-gh Bot added a commit that referenced this pull request Aug 2, 2025
Backport #84946 to 25.7: Fix memory tracking drift from background schedule pool and executor

@antonio2368 antonio2368 left a comment

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.

Good catch but I'm trying to understand this completely.
From what I got, we could leave unflushed untracked_memory and because at the end of BackgroundSchedulePool task we mostly deallocate, it will often go negative. But either way we would leave untracked memory for unspecified amount of time if the thread is idle.
Why don't we do it inside ThreadPool also?
And why isn't it connected to max_untracked_memory?

@azat

azat commented Aug 4, 2025

Copy link
Copy Markdown
Member Author

From what I got, we could leave unflushed untracked_memory and because at the end of BackgroundSchedulePool task we mostly deallocate, it will often go negative.

Not exactly, I saw ~1KiB allocated (positive untracked_memory) after execution of job finished in BackgroundSchedulePool

But either way we would leave untracked memory for unspecified amount of time if the thread is idle.

Yes, and this problem is bigger if you have bigger pools, since right now random threads will be used to execute job.

Why don't we do it inside ThreadPool also?

We ThreadStatus only in ThreadPool during execution, so it is not a problem there because w/o ThreadStatus it uses global tracker. The problem pops up for i.e. BackgroundSchedulePool because they preallocate all threads at once.

And why isn't it connected to max_untracked_memory?

Because we apply it only on a query level, this is an open question should be do this globally that I'm considering, but we should patches for memory tracking simpler IMHO to avoid possible regressions (or if any revert only relevant part).

alexey-milovidov added a commit that referenced this pull request Aug 4, 2025
Backport #84946 to 25.6: Fix memory tracking drift from background schedule pool and executor
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-bugfix Pull request with bugfix, not backported by default 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.

Memory Tracking Issues

6 participants