Repository navigation
RFC: Add a server setting to allow any allocation to throw MEMORY_LIMIT_EXCEEDED - #105265
Conversation
|
Workflow [PR], commit [917c934] Summary: ❌
AI ReviewSummaryThis PR introduces Findings
Tests
Final Verdict
|
| AllocationTrace CurrentMemoryTracker::allocThrow(Int64 size) | ||
| { | ||
| const bool enforce_memory_limit = static_cast<UInt64>(size) >= min_allocation_size_to_throw_on_memory_limit.load(std::memory_order_relaxed); | ||
| return allocImpl(size, enforce_memory_limit); |
There was a problem hiding this comment.
allocThrow decides enforce_memory_limit per allocation size, but allocImpl batches untracked bytes and only applies that flag when the batch is flushed. This means a large allocation (>= threshold) can be accumulated and then flushed under enforce_memory_limit = false if the allocation that crosses untracked_memory_limit is small, so the large allocation is effectively treated as non-throwing.
Concrete example with threshold 1 MiB: allocate 1.5 MiB (enforce=true, no flush yet), then several 0.9 MiB allocations; when flush is triggered by a 0.9 MiB allocation, the whole batch is flushed with enforce=false.
Can we flush pending untracked memory when the enforcement mode changes (similar to how blocker-level changes are handled), so threshold semantics are preserved?
There was a problem hiding this comment.
Will add a note that it is recommended to set value of new setting >= max_untracked_memory
There was a problem hiding this comment.
The documentation note does not fix the runtime contract here. CurrentMemoryTracker::allocImpl still batches current_thread->untracked_memory and applies enforce_memory_limit from only the allocation that crosses untracked_memory_limit at flush time (memory_tracker->allocImpl(current_untracked_memory, enforce_memory_limit)).
So when min_allocation_size_to_throw_on_memory_limit is below max_untracked_memory, a large allocation can still be accounted in a later flush with enforce_memory_limit = false.
Concrete trace (default max_untracked_memory = 4 MiB, threshold 2 MiB):
- allocate
3 MiB(enforce_memory_limit = true, no flush), - allocate
1.5 MiB(enforce_memory_limit = false, flush happens), - flush executes
memory_tracker->allocImpl(4.5 MiB, false)and bypasses the intended size-gated enforcement.
Please flush pending untracked bytes when enforcement mode changes (or keep separate untracked buckets by mode), and add a regression test that uses a threshold below max_untracked_memory.
There was a problem hiding this comment.
I re-checked the current head, and this is still live in CurrentMemoryTracker::allocImpl: pending current_thread->untracked_memory is flushed with the latest enforce_memory_limit value only (memory_tracker->allocImpl(current_untracked_memory, enforce_memory_limit)), so mixed-size batches can bypass the configured threshold.
Concrete trace with default max_untracked_memory = 4 MiB, min_allocation_size_to_throw_on_memory_limit = 2 MiB:
- allocate
3 MiBvia throwingoperator new(enforce_memory_limit = true), no flush; - allocate
1.5 MiB(enforce_memory_limit = false), now flush fires; - flush applies
allocImpl(4.5 MiB, false)and the3 MiBchunk is effectively treated as non-throwing.
So the setting contract is still violated for thresholds below max_untracked_memory. This needs either a flush when enforcement mode changes, or separate untracked buckets per enforcement mode.
Add a server setting (`min_allocation_size_to_throw_on_memory_limit`) to allow any allocation to throw `MEMORY_LIMIT_EXCEEDED`, this should help with preventing OOMs Right now, any `new` cannot throw, this is due exception safety issues, and only explicitly marked can throw `MEMORY_LIMIT_EXCEEDED`. But this can lead to real OOMs, i.e. when kernel will kill clickhouse-server, even though we are trying hard to track memory properly. Add a server setting `min_allocation_size_to_throw_on_memory_limit` (UInt64 bytes, default 0). When non-zero, the throwing `operator new` variants are allowed to raise `MEMORY_LIMIT_EXCEEDED`. _The assumption is that code with big enough allocations does not exception safety issues._ Note, places that allows (Allocator)/never allows (malloc) `MEMORY_LIMIT_EXCEEDED` before are not affected.
04b23f7 to
a85155d
Compare
nickitat
left a comment
There was a problem hiding this comment.
Since we assume it will only be used in non-default configurations (CI or debugging OOM kills), should we really bother with this minimal limit? It seems like in such circumstances the more often it could be triggered the better.
|
I would like to have an option to increase it, since it is unclear which value is generally "safe" (only one thing is clear it should be bigger then max_untracked_memory to avoid mixing) |
LLVM Coverage Report
Changed lines: 92.72% (140/151) | lost baseline coverage: 9 line(s) · Uncovered code |
cd00255
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Add a server setting (
min_allocation_size_to_throw_on_memory_limit) to allow any allocation to throwMEMORY_LIMIT_EXCEEDED, this should help with preventing OOMsRight now, any
newcannot throw, this is due exception safety issues, and only explicitly marked can throwMEMORY_LIMIT_EXCEEDED.But this can lead to real OOMs, i.e. when kernel will kill clickhouse-server, even though we are trying hard to track memory properly.
Add a server setting
min_allocation_size_to_throw_on_memory_limit(UInt64 bytes, default 0). When non-zero, the throwingoperator newvariants are allowed to raiseMEMORY_LIMIT_EXCEEDED. The assumption is that code with big enough allocations does not exception safety issues.Note, places that allows (Allocator)/never allows (malloc)
MEMORY_LIMIT_EXCEEDEDbefore are not affected.Version info
26.6.1.204