Repository navigation
Reapply "Add UntrackedMemory asynchronous metric" - #106386
Conversation
|
Workflow [PR], commit [29839e5] Summary: ❌
AI ReviewSummaryThis PR reintroduces the Findings
Final Verdict |
|
|
||
| -- Value is an Int64 sum; can be negative if threads have net-freed since last flush, | ||
| -- but the metric must exist and be queryable as a number. | ||
| SELECT isFinite(value) FROM system.asynchronous_metrics WHERE metric = 'UntrackedMemory'; |
There was a problem hiding this comment.
The coverage here only proves that UntrackedMemory exists and is finite. It would still pass if UntrackedMemoryRegistry::sum always returned 0, if counters were not removed on thread teardown, or if MEMORY_LIMIT_EXCEEDED exceptions stopped including the registry value. Since the PR contract is specifically that the metric reports the sum of per-thread untracked counters and that this value is surfaced in the exception text, please add focused coverage for those semantics, for example a small unit test that creates multiple UntrackedMemoryCounters with positive and negative deltas and verifies sum before and after one counter is destroyed, plus a minimal memory-limit exception check for the new message field.
| { | ||
| Int64 current_untracked_memory = current_thread->untracked_memory; | ||
| current_thread->untracked_memory = 0; | ||
| current_thread->untracked_memory.store(0); |
There was a problem hiding this comment.
The new exception field can under-report the current thread's saved untracked bytes on the path that needs it most. This stores the registered counter as 0 before memory_tracker->allocImpl formats MEMORY_LIMIT_EXCEEDED; if that call throws, UntrackedMemoryRegistry::sum runs while the current thread's previous buffered value is neither tracked (the allocation was reverted) nor registered, and it is restored only in the catch below. As a result, Untracked memory across all threads can omit this thread's saved buffer.
Please keep the saved current-thread amount visible to the registry during the throwing path, or pass it into the exception formatting, and cover this with a focused MEMORY_LIMIT_EXCEEDED message test.
There was a problem hiding this comment.
I don't think this will make any difference in reality, but just to eliminate one source of imprecision, I've fixed this.
There was a problem hiding this comment.
This is still present at src/Common/CurrentMemoryTracker.cpp:82: the counter is stored as 0 before memory_tracker->allocImpl reaches the MEMORY_LIMIT_EXCEEDED formatting path at src/Common/MemoryTracker.cpp:452. If new_untracked_memory == previous + size and the allocation is rejected, the previous buffered bytes are restored only in the catch at src/Common/CurrentMemoryTracker.cpp:90, after the exception text has already called UntrackedMemoryRegistry::sum.
| prev_memory_tracker_parent = thread_tracker->getParent(); | ||
|
|
||
| current_thread->untracked_memory = 0; | ||
| current_thread->untracked_memory.store(0); |
There was a problem hiding this comment.
UntrackedMemory can still omit this thread while a MemoryTrackerSwitcher is active. At entry we move the previous counter value into prev_untracked_memory and publish 0 in the registered counter, but those saved bytes are neither flushed to the old parent nor visible to UntrackedMemoryRegistry::sum until the destructor restores them. Any async metric sample or MEMORY_LIMIT_EXCEEDED message inside the switcher scope can under-report by up to this thread's max_untracked_memory, per switched thread.
Please either flush the old counter before switching parents, or keep the saved value in a registered counter that sum includes.
There was a problem hiding this comment.
Again, marginal value at best, but we can simplify the code and rule out one source of imprecision, so I've applied a "fix".
There was a problem hiding this comment.
This still holds in current head: src/Common/MemoryTrackerSwitcher.cpp:16 saves the old value and src/Common/MemoryTrackerSwitcher.cpp:20 publishes 0; the saved bytes are not restored until src/Common/MemoryTrackerSwitcher.cpp:36. Any UntrackedMemoryRegistry::sum during the switched scope still omits the pre-switch buffer.
|
build broken here: #106102 |
…rg::alter PR ClickHouse#106102 (merged 2026-06-05 17:43 UTC) refactored Iceberg::alter to declare last_version and compression_method outside an if/else that assigns them in both branches. Clang-tidy cppcoreguidelines-init-variables flags the bare declarations and Build (arm_tidy) is built with -warnings-as-errors, so the build fails for every PR whose CI ran on master after this commit. CIDB shows 30+ unrelated PRs hitting this in the last 2 days (e.g. ClickHouse#89360, ClickHouse#103540, ClickHouse#106120, ClickHouse#106386, ClickHouse#106404, ClickHouse#106522, ClickHouse#105102, ClickHouse#106590). Initialize both variables to safe defaults at declaration. They are unconditionally overwritten in both if and else branches before use, so behavior is unchanged. The new defaults are only relevant if a future code path skips both assignments, in which case last_version=0 and compression_method=CompressionMethod::None are sane no-op values (the same defaults the old structured-binding form would produce through default-construction of the destructured aggregate). Co-Authored-By: Claude Opus 4.8 <[email protected]> (cherry picked from commit d72cac5)
…rg::alter PR ClickHouse#106102 (merged 2026-06-05 17:43 UTC) refactored Iceberg::alter to declare last_version and compression_method outside an if/else that assigns them in both branches. Clang-tidy cppcoreguidelines-init-variables flags the bare declarations and Build (arm_tidy) is built with -warnings-as-errors, so the build fails for every PR whose CI ran on master after this commit. CIDB shows 30+ unrelated PRs hitting this in the last 2 days (e.g. ClickHouse#89360, ClickHouse#103540, ClickHouse#106120, ClickHouse#106386, ClickHouse#106404, ClickHouse#106522, ClickHouse#105102, ClickHouse#106590). Initialize both variables to safe defaults at declaration. They are unconditionally overwritten in both if and else branches before use, so behavior is unchanged. The new defaults are only relevant if a future code path skips both assignments, in which case last_version=0 and compression_method=CompressionMethod::None are sane no-op values (the same defaults the old structured-binding form would produce through default-construction of the destructured aggregate). Co-Authored-By: Claude Opus 4.8 <[email protected]> (cherry picked from commit d72cac5)
…rg::alter PR ClickHouse#106102 (merged 2026-06-05 17:43 UTC) refactored Iceberg::alter to declare last_version and compression_method outside an if/else that assigns them in both branches. Clang-tidy cppcoreguidelines-init-variables flags the bare declarations and Build (arm_tidy) is built with -warnings-as-errors, so the build fails for every PR whose CI ran on master after this commit. CIDB shows 30+ unrelated PRs hitting this in the last 2 days (e.g. ClickHouse#89360, ClickHouse#103540, ClickHouse#106120, ClickHouse#106386, ClickHouse#106404, ClickHouse#106522, ClickHouse#105102, ClickHouse#106590). Initialize both variables to safe defaults at declaration. They are unconditionally overwritten in both if and else branches before use, so behavior is unchanged. The new defaults are only relevant if a future code path skips both assignments, in which case last_version=0 and compression_method=CompressionMethod::None are sane no-op values (the same defaults the old structured-binding form would produce through default-construction of the destructured aggregate). Co-Authored-By: Claude Opus 4.8 <[email protected]> (cherry picked from commit d72cac5)
…rg::alter PR ClickHouse#106102 (merged 2026-06-05 17:43 UTC) refactored Iceberg::alter to declare last_version and compression_method outside an if/else that assigns them in both branches. Clang-tidy cppcoreguidelines-init-variables flags the bare declarations and Build (arm_tidy) is built with -warnings-as-errors, so the build fails for every PR whose CI ran on master after this commit. CIDB shows 30+ unrelated PRs hitting this in the last 2 days (e.g. ClickHouse#89360, ClickHouse#103540, ClickHouse#106120, ClickHouse#106386, ClickHouse#106404, ClickHouse#106522, ClickHouse#105102, ClickHouse#106590). Initialize both variables to safe defaults at declaration. They are unconditionally overwritten in both if and else branches before use, so behavior is unchanged. The new defaults are only relevant if a future code path skips both assignments, in which case last_version=0 and compression_method=CompressionMethod::None are sane no-op values (the same defaults the old structured-binding form would produce through default-construction of the destructured aggregate). Co-Authored-By: Claude Opus 4.8 <[email protected]> (cherry picked from commit d72cac5)
…rg::alter PR ClickHouse#106102 (merged 2026-06-05 17:43 UTC) refactored Iceberg::alter to declare last_version and compression_method outside an if/else that assigns them in both branches. Clang-tidy cppcoreguidelines-init-variables flags the bare declarations and Build (arm_tidy) is built with -warnings-as-errors, so the build fails for every PR whose CI ran on master after this commit. CIDB shows 30+ unrelated PRs hitting this in the last 2 days (e.g. ClickHouse#89360, ClickHouse#103540, ClickHouse#106120, ClickHouse#106386, ClickHouse#106404, ClickHouse#106522, ClickHouse#105102, ClickHouse#106590). Initialize both variables to safe defaults at declaration. They are unconditionally overwritten in both if and else branches before use, so behavior is unchanged. The new defaults are only relevant if a future code path skips both assignments, in which case last_version=0 and compression_method=CompressionMethod::None are sane no-op values (the same defaults the old structured-binding form would produce through default-construction of the destructured aggregate). Co-Authored-By: Claude Opus 4.8 <[email protected]> (cherry picked from commit d72cac5)
…rg::alter PR ClickHouse#106102 (merged 2026-06-05 17:43 UTC) refactored Iceberg::alter to declare last_version and compression_method outside an if/else that assigns them in both branches. Clang-tidy cppcoreguidelines-init-variables flags the bare declarations and Build (arm_tidy) is built with -warnings-as-errors, so the build fails for every PR whose CI ran on master after this commit. CIDB shows 30+ unrelated PRs hitting this in the last 2 days (e.g. ClickHouse#89360, ClickHouse#103540, ClickHouse#106120, ClickHouse#106386, ClickHouse#106404, ClickHouse#106522, ClickHouse#105102, ClickHouse#106590). Initialize both variables to safe defaults at declaration. They are unconditionally overwritten in both if and else branches before use, so behavior is unchanged. The new defaults are only relevant if a future code path skips both assignments, in which case last_version=0 and compression_method=CompressionMethod::None are sane no-op values (the same defaults the old structured-binding form would produce through default-construction of the destructured aggregate). Co-Authored-By: Claude Opus 4.8 <[email protected]> (cherry picked from commit d72cac5)
…rg::alter PR ClickHouse#106102 (merged 2026-06-05 17:43 UTC) refactored Iceberg::alter to declare last_version and compression_method outside an if/else that assigns them in both branches. Clang-tidy cppcoreguidelines-init-variables flags the bare declarations and Build (arm_tidy) is built with -warnings-as-errors, so the build fails for every PR whose CI ran on master after this commit. CIDB shows 30+ unrelated PRs hitting this in the last 2 days (e.g. ClickHouse#89360, ClickHouse#103540, ClickHouse#106120, ClickHouse#106386, ClickHouse#106404, ClickHouse#106522, ClickHouse#105102, ClickHouse#106590). Initialize both variables to safe defaults at declaration. They are unconditionally overwritten in both if and else branches before use, so behavior is unchanged. The new defaults are only relevant if a future code path skips both assignments, in which case last_version=0 and compression_method=CompressionMethod::None are sane no-op values (the same defaults the old structured-binding form would produce through default-construction of the destructured aggregate). Co-Authored-By: Claude Opus 4.8 <[email protected]> (cherry picked from commit d72cac5)
# Conflicts: # src/Common/CurrentMemoryTracker.cpp # src/Common/ThreadStatus.cpp
| std::ignore = memory_tracker->free(-current_untracked_memory, /*_sample_probability=*/ 0.0); | ||
| std::ignore = memory_tracker->free(-new_untracked_memory, /*_sample_probability=*/ 0.0); | ||
|
|
||
| current_thread->untracked_memory.store(0); |
There was a problem hiding this comment.
Why you moved it after allocImpl?
This is wrong, we cannot do this because allocImpl may do new allocations, and we will loose them.
There was a problem hiding this comment.
allocImplmay do new allocations, and we will loose them
You are right. I've changed store(0) to add(-previous_untracked_memory).
Why you moved it after allocImpl?
If we throw MEMORY_LIMIT_EXCEEDED, we want the message to have a slightly more correct untracked memory amount. So the current thread's untracked memory counter should be previous_untracked_memory (which is the amount of untracked memory before allocation) and not 0.
There was a problem hiding this comment.
we want the message to have a slightly more correct untracked memory amount
This is good catch, but it is not correct, look:
untracked_memory=1000 # limit 1001
alloc(32)
untracked_memory=1032 # prev = 1000
untracked_memory=1000
allocImpl() # recursive allocation
untracked_memory=1000
alloc(64)
untracked_memory=1064 # prev = 1000
untracked_memory=1000
allocImpl() # no recursive allocation
untracked_memory-=1000 # 0
untracked_memory-=1000 # -1000
The rule of thumb is that "reset untracked_memory" before calling MemoryTracker
Memory tracker bugs hard to find, so let's just convert one counter to another and leave other parts as-is for now.
| Int64 prev_untracked_memory = 0; | ||
| VariableContext prev_untracked_memory_blocker_level = VariableContext::Max; | ||
| /// The per-CPU contribution accounts for the same un-flushed bytes as untracked_memory, | ||
| /// so it must be saved/restored together with it; otherwise allocations in the switched | ||
| /// scope fold into the previous parent's contribution. | ||
| PerCPUMemoryThreadState prev_per_cpu; |
There was a problem hiding this comment.
CurrentMemoryTracker::allocImpl computes the per-allocation sampling decision from the cached fields in ThreadStatus via getEffectiveSampleProbability, not by walking the tracker chain every time. MemoryTrackerSwitcher changes the thread tracker's parent, and different parents can resolve to different memory_profiler_* settings, so the switched scope has to re-resolve that cache on entry and restore it on exit or it will keep sampling with the old parent's policy. The removed untracked_memory / per-CPU saved state is a separate change: MemoryTracker::setParent already flushes that state before rewiring the parent, so the switcher no longer needs to preserve it manually.
There was a problem hiding this comment.
We flush untracked memory in both ctor (via setParent) and dtor (explicitly) of MemoryTrackerSwitcher.
There was a problem hiding this comment.
Let's not do this change here and dot it separately, it will change the behavior slightly:
- it won't restore prev_untracked_memory_blocker_level
- it will do more flushes, right now we do not flush on first setParent
- though prev_per_cpu looks redundant here (after removal of "prev_untracked_memory"), but anyway let's do it separately
|
BTW looks like (at least in CI) we do not have a lot of untracked memory, even w/o #106055, only 99-125MiBs Details |
azat
left a comment
There was a problem hiding this comment.
Looks good now, but please take a look at this as well:
- Changelog entry does not looks user friendly (and even devs friendly)
- Do we need to join thread pool's in client? Looks like we do not (so as in format, keeper-client, zookeeper-dump-tree, zookeeper-remove-by-list, extract-from-config, keeper-utils) - Ok to do separately (since initially it was done in a separate PR at least)
Co-authored-by: Azat Khuzhin <[email protected]>
| catch (...) | ||
| { | ||
| current_thread->untracked_memory += previous_untracked_memory; | ||
| current_thread->untracked_memory.add(previous_untracked_memory); |
There was a problem hiding this comment.
current_thread->untracked_memory is restored here after a failed flush, but the matching untracked_memory_blocker_level is not. That matters because the throwing path in MemoryTracker::allocImpl explicitly expects recursive allocations from Exception::ctor/std::string under MemoryTrackerBlockerInThread(VariableContext::Global) (src/Common/MemoryTracker.cpp:455), and those recursive CurrentMemoryTracker::allocImpl calls update the thread's blocker level to Global.
After this catch rethrows, the restored pre-existing bytes are still buffered, but the next flushUntrackedMemory will use that stale Global blocker and bypass the query/user/process trackers entirely, charging those bytes only to total_memory_tracker. Please restore the saved blocker context together with the saved buffer (keeping the internal exception-construction bytes separate), or flush the internal Global bytes before restoring the caller's state.
There was a problem hiding this comment.
Good finding, but not related to this PR, will be fixed separately once this will be merged. Interesting maybe this was the cause of #88483, though AFAIR there were a lot of them, while this situation is rare.
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 108/118 (91.53%) · Uncovered code |
|
This appears to be broken: It's reporting -1.9MB untracked memory. |
This is OK, it can be from For 1.9MiB it is ~60 threads that has it's uncommitted freed memory near to the threshold |
|
I didn't realize we could have uncommitted free memory. Makes sense |
…rg::alter PR ClickHouse#106102 (merged 2026-06-05 17:43 UTC) refactored Iceberg::alter to declare last_version and compression_method outside an if/else that assigns them in both branches. Clang-tidy cppcoreguidelines-init-variables flags the bare declarations and Build (arm_tidy) is built with -warnings-as-errors, so the build fails for every PR whose CI ran on master after this commit. CIDB shows 30+ unrelated PRs hitting this in the last 2 days (e.g. ClickHouse#89360, ClickHouse#103540, ClickHouse#106120, ClickHouse#106386, ClickHouse#106404, ClickHouse#106522, ClickHouse#105102, ClickHouse#106590). Initialize both variables to safe defaults at declaration. They are unconditionally overwritten in both if and else branches before use, so behavior is unchanged. The new defaults are only relevant if a future code path skips both assignments, in which case last_version=0 and compression_method=CompressionMethod::None are sane no-op values (the same defaults the old structured-binding form would produce through default-construction of the destructured aggregate). Co-Authored-By: Claude Opus 4.8 <[email protected]> (cherry picked from commit d72cac5)
…rg::alter PR ClickHouse#106102 (merged 2026-06-05 17:43 UTC) refactored Iceberg::alter to declare last_version and compression_method outside an if/else that assigns them in both branches. Clang-tidy cppcoreguidelines-init-variables flags the bare declarations and Build (arm_tidy) is built with -warnings-as-errors, so the build fails for every PR whose CI ran on master after this commit. CIDB shows 30+ unrelated PRs hitting this in the last 2 days (e.g. ClickHouse#89360, ClickHouse#103540, ClickHouse#106120, ClickHouse#106386, ClickHouse#106404, ClickHouse#106522, ClickHouse#105102, ClickHouse#106590). Initialize both variables to safe defaults at declaration. They are unconditionally overwritten in both if and else branches before use, so behavior is unchanged. The new defaults are only relevant if a future code path skips both assignments, in which case last_version=0 and compression_method=CompressionMethod::None are sane no-op values (the same defaults the old structured-binding form would produce through default-construction of the destructured aggregate). Co-Authored-By: Claude Opus 4.8 <[email protected]> (cherry picked from commit d72cac5)
…rg::alter PR ClickHouse#106102 (merged 2026-06-05 17:43 UTC) refactored Iceberg::alter to declare last_version and compression_method outside an if/else that assigns them in both branches. Clang-tidy cppcoreguidelines-init-variables flags the bare declarations and Build (arm_tidy) is built with -warnings-as-errors, so the build fails for every PR whose CI ran on master after this commit. CIDB shows 30+ unrelated PRs hitting this in the last 2 days (e.g. ClickHouse#89360, ClickHouse#103540, ClickHouse#106120, ClickHouse#106386, ClickHouse#106404, ClickHouse#106522, ClickHouse#105102, ClickHouse#106590). Initialize both variables to safe defaults at declaration. They are unconditionally overwritten in both if and else branches before use, so behavior is unchanged. The new defaults are only relevant if a future code path skips both assignments, in which case last_version=0 and compression_method=CompressionMethod::None are sane no-op values (the same defaults the old structured-binding form would produce through default-construction of the destructured aggregate). Co-Authored-By: Claude Opus 4.8 <[email protected]> (cherry picked from commit d72cac5)
…-race Reapply "Add `UntrackedMemory` asynchronous metric"
Reapply #104690 after #106365.
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Adds a new asynchronous metric UntrackedMemory (visible in
system.asynchronous_metrics) that reports memory already allocated by threads but not yet accounted in the global memory tracking counter. Each thread accumulates small allocations locally and reports them in bulk. This helps explain discrepancies betweenMemoryTrackingand the process's actual memory usage.MEMORY_LIMIT_EXCEEDEDerror messages now also include the amount of untracked memory, making it easier to understand why a query or the server hit its memory limit.