Skip to content

Cherry pick #82414 to 26.6: Workload scheduling: Memory reservations - #108686

Merged
robot-clickhouse-ci-2 merged 188 commits into
backport/26.6/82414from
cherrypick/26.6/82414
Jun 27, 2026
Merged

robot-clickhouse-ci-2 merged 188 commits into
backport/26.6/82414from
cherrypick/26.6/82414

Conversation

@robot-clickhouse-ci-2

Copy link
Copy Markdown
Contributor

Original pull-request #82414

Do not merge this PR manually

This pull-request is a first step of an automated backporting.
It contains changes similar to calling git cherry-pick locally.
If you intend to continue backporting the changes, then resolve all conflicts if any.
Otherwise, if you do not want to backport them, then just close this pull-request.

The check results does not matter at this step - you can safely ignore them.

Troubleshooting

If the conflicts were resolved in a wrong way

If this cherry-pick PR is completely screwed by a wrong conflicts resolution, and you want to recreate it:

  • delete the pr-cherrypick label from the PR
  • delete this branch from the repository

You also need to check the Original pull-request for pr-backports-created label, and delete if it's presented there

The PR source

The PR is created in the CI job

serxa added 30 commits June 23, 2025 12:49
serxa and others added 25 commits June 9, 2026 16:43
…kill

`AllocationLimit::allocation_to_kill` was cleared in only two places:
`approveDecrease` (when the victim's `removing_allocation` decrease propagates
up) and on subtree detach in `propagateUpdate`.

A reservation created with `reserve_memory` unset has `reserved_size == 0` and
is never admitted: it goes straight to `running_allocations` and its first
increase (driven by `syncWithMemoryTracker`) is the one that exceeds the
workload limit, so `selectAllocationToKill` picks itself as the victim and
`allocation_to_kill` is set to it. When that allocation is removed it takes the
never-admitted local path in `AllocationQueue::processActivation`, which applies
a `removing_allocation` decrease locally and does NOT propagate it up (doing so
would underflow `allocations`). `approveDecrease` therefore never runs and
`allocation_to_kill` is left dangling at the destroyed allocation. The next
over-limit increase finds `allocation_to_kill != nullptr` in `setIncrease`,
skips issuing a fresh kill, and blocks forever.

Clear `allocation_to_kill` in `setIncrease` once there is no increase request
left to satisfy (`new_increase == nullptr`), before the early return — in the
self-kill case both `increase` and `new_increase` are already `nullptr`. This is
the same class of bug as the earlier subtree-detach fix, but for the
per-allocation never-admitted-removal path. Add a deterministic regression test.

Observed as a 600s timeout (not flakiness) in
`test_scheduler_memory::test_max_memory_limit`.
CI: Integration tests (amd_asan_ubsan), SHA 43bfd58
PR: #82414

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
`PrecedenceAllocation::selectAllocationToKill` always picked the least-precedence
running child (`running_children.rbegin()`) as the victim without comparing it
against the killer's own precedence. At the least common ancestor of the killer
and the victim this let a lower-precedence pending allocation evict a
higher-precedence running one — e.g. workload `dev` (`precedence = 2`) starting a
query could kill running `prd` (`precedence = 1`) when admitting it would exceed
the workload `max_memory`. That violates the contract that a pending allocation
of lower precedence never kills a higher-precedence workload.

When this node is on the killer's path (`&killer == increase`, so `increase_child`
is the killer's branch) and the victim branch differs, enforce precedence: never
evict a strictly higher-precedence child, and for a `Pending` killer never evict
an equal-precedence one either (it is not running yet, so it must not displace an
already-admitted peer). A running allocation may still reclaim from
equal-or-lower precedence children, so the existing high-precedence eviction path
is unchanged.

This was not covered before: the unit harness in `gtest_space_shared_scheduler`
cannot build a `PrecedenceAllocation`, and the workload-level tests only exercised
the killer = highest-precedence direction or the equal-precedence
`FairAllocation` path. Add the missing negative-direction test.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
…enqueue

The constructor calls `queue.insertAllocation`, then waits for admission and
calls `throwIfNeeded`. `killAllocation` only records `kill_reason` and does not
unlink the allocation, so throwing straight from the constructor on a kill skips
`~MemoryReservation` and `removeAllocation` never runs, leaving the scheduler
with a dangling pointer to a destroyed object (the base `~ResourceAllocation`
has only debug-only checks, so in release this is a use-after-free rather than
an exception). The `fail_reason` path was already safe because `allocationFailed`
sets `removed`.

Extract the destructor's unlink-and-wait logic into `detachFromQueue` and call
it from the constructor before reporting the failure, so a throwing constructor
always removes the allocation first.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
…e-shared

`ResourceLink::operator bool` is true when either `queue` or `allocation_queue`
is set, but `MemoryReservation` requires `allocation_queue`. If the configured
memory-reservation resource name resolves to a link with only `queue` set (e.g.
a deprecated XML resource shadowing the SQL `MEMORY RESERVATION` one), the
constructor dereferenced a null `allocation_queue`. Throw `BAD_ARGUMENTS` instead.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
`ParserCreateWorkloadEntity` (which parses `<resources_and_workloads>`) only
accepts `CREATE RESOURCE` / `CREATE WORKLOAD`, so the example without `CREATE`
would fail config reload.
…eduling

# Conflicts:
#	src/Common/Scheduler/WorkloadSettings.cpp
…eduling

# Conflicts:
#	src/Common/ErrorCodes.cpp
…eduling

# Conflicts:
#	src/Common/Scheduler/Nodes/tests/gtest_time_shared_scheduler.cpp
#	src/Common/Scheduler/Nodes/tests/gtest_workload_resource_manager.cpp
#	src/Common/Scheduler/ResourceGuard.h
`ResourceGuard::Request::finish` calls `link.queue->adjustBudget`, but a request
that was failed by the scheduler is often failed precisely because its queue is
being purged/destructed, so the raw `ResourceLink::queue` may already dangle.
The master merge had switched `unlock`/`recoverAfterConstructorFailure` to always
call `finish`, causing a heap-use-after-free when a `ResourceGuard` outlives its
queue (e.g. a leaf workload turning into a non-leaf while a deferred request is in
flight) — observed in `SchedulerTimeSharedWorkloadNode.ResourceGuardException`
under ASan.

Restore the rule that `finish` and the failed path are alternatives: skip `finish`
(and the queue access it performs) for a failed request, via `consumeException`.
The budget transaction of a failed request is therefore not rolled back, so the
queue budget may be slightly off afterwards; this is acceptable as failures are
rare and the queue (with its budget) is usually gone anyway, whereas touching it
would be a use-after-free. The failed-enqueue budget rollback in
`enqueueRequestUsingBudget` is unaffected.

CI: https://s3.amazonaws.com/clickhouse-test-reports/PRs/82414/5d2e517ae2842c7ffe00dbfca555428099b5e2bb/unit_tests_asan_ubsan/unit_tests_asan_ubsan.log

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
…eduling

# Conflicts:
#	src/Common/ErrorCodes.cpp
…eduling

# Conflicts:
#	tests/integration/test_scheduler/test.py
#	tests/integration/test_scheduler_cpu/test.py
#	tests/integration/test_scheduler_cpu_preemptive/test.py
…eduling

# Conflicts:
#	src/Common/Scheduler/Nodes/tests/gtest_workload_resource_manager.cpp
Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Base max_memory_ratio on total_memory_tracker.getHardLimit() so that
max_server_memory_usage (and its derived RAM ratio) is respected, falling
back to physical memory only when no server hard limit is configured.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only! do not test disable testing on pull request labels Jun 27, 2026
@robot-clickhouse-ci-2
robot-clickhouse-ci-2 merged commit abd8c64 into backport/26.6/82414 Jun 27, 2026
@robot-clickhouse-ci-2
robot-clickhouse-ci-2 deleted the cherrypick/26.6/82414 branch June 27, 2026 10:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

do not test disable testing on pull request pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only!

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants