Skip to content

Sync private - #104518

Merged
scanhex12 merged 17 commits into
ClickHouse:masterfrom
scanhex12:vedernikov/expose-merge-utils-and-storage-objstorage-bg-hooks
May 12, 2026
Merged

scanhex12 merged 17 commits into
ClickHouse:masterfrom
scanhex12:vedernikov/expose-merge-utils-and-storage-objstorage-bg-hooks

Conversation

@scanhex12

@scanhex12 scanhex12 commented May 10, 2026 •

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Not for changelog (changelog entry is not required)

Version info

  • Merged into: 26.5.1.548

scanhex12 added 2 commits May 10, 2026 13:03
Move five helpers that wrap `IPartsCollector` and the merge selector
out of the anonymous namespace in `MergeTreeDataMergerMutator.cpp` and
declare them in the header so other table engines can reuse them
without going through `MergeTreeDataMergerMutator`:

  - `canMergeAllParts`
  - `getBestPartitionToOptimizeEntire`
  - `grabAllPossibleRanges` (returns just `PartsRanges` for callers
    that don't need the per-partition statistics)
  - `chooseMergesFrom`
  - `grabAllPartsInsidePartition`

Add `calculateStatisticsForPartitions` for callers that obtain ranges
from a source other than `IPartsCollector` (which fills statistics
itself).

The internal callers in `MergeTreeDataMergerMutator` keep using the
full-fat `CollectedPartsRanges` flavour, which is renamed to
`collectAllPossibleRanges` to free up the public name.

Also let the per-storage backoff bias propagate into
`BackgroundJobsAssignee::postpone` via the existing virtual
`IBackgroundOperation::getBiasBackoffSeconds` (defaults to 0, so this
is a no-op for current `MergeTreeData` users).

No behaviour change for `MergeTree` family.
Make `StorageObjectStorage` an `IBackgroundOperation` so engines
backed by object storage can opt into the same scheduling
infrastructure that `MergeTree` uses.

The wiring is opt-in: by default
`StorageObjectStorageConfiguration::isBackgroundExecutable` returns
false, `startup` / `shutdown` skip the assignee, and any attempt to
schedule a job through a configuration that hasn't overridden
`scheduleDataProcessingJob` fails with `NOT_IMPLEMENTED`. Existing
`StorageS3`, `StorageAzure`, `StorageHDFS`, etc. observe no behaviour
change.

Subclasses opt in by overriding:

  - `isBackgroundExecutable` (return true)
  - `scheduleDataProcessingJob`
  - optionally `finishAllBackgroundJobs` and `getBiasBackoffSeconds`

`scheduleDataMovingJob` is a hard `NOT_IMPLEMENTED` override; object
storage doesn't have parts to move between disks.

Also expose `getObjectStorage` / `getObjectStorageConfiguration` on
`StorageObjectStorage` so configuration-side background workers can
reach the underlying storage and configuration without friend
declarations.
@clickhouse-gh

clickhouse-gh Bot commented May 10, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [df93162]

Summary: ❌

job_name test_name status info comment
Unit tests (asan_ubsan, function_prop_fuzzer) FAIL
FunctionsStress.stress FAIL cidb
AllTests FAIL cidb
Unit tests (tsan, function_prop_fuzzer) FAIL
FunctionsStress.stress FAIL cidb
AllTests FAIL cidb

AI Review

Summary

This PR extracts MergeTree merge-selection helpers for reuse and wires background scheduling hooks into ObjectStorage / Iceberg flows. Current HEAD still has three correctness issues (rename synchronization, Int64→Int32 truncation in Iceberg mutation byte accounting, and unsigned underflow in pool-capacity guard), so the PR is not ready to merge yet.

Findings
❌ Blockers
  • [src/Storages/ObjectStorage/StorageObjectStorage.h:230; src/Storages/ObjectStorage/StorageObjectStorage.cpp] StorageObjectStorage still has no renameInMemory override to propagate renamed StorageID into cached background_operations_assignee / storage_id; background cleanup in BackgroundJobsAssignee::finish is still keyed by stale id after RENAME TABLE.
    • Suggested fix: implement StorageObjectStorage::renameInMemory calling IStorage::renameInMemory(new_table_id), background_operations_assignee.updateStorageID(new_table_id), and storage_id = new_table_id.
  • [src/Storages/ObjectStorage/DataLakes/Iceberg/Mutations.cpp:269,338] Both write paths still assign total_bytes via static_cast<Int32>(...) despite DeleteFileWriteResult::total_bytes being Int64, so files above 2 GiB are still truncated/overflowed in metadata accounting.
    • Suggested fix: keep Int64 end-to-end and assign directly (or cast to Int64).
  • [src/Storages/MergeTree/MergeTreeDataMergerMutator.cpp:631] The guard still computes max_tasks_count - occupied with both values as size_t; if occupied >= max_tasks_count, subtraction underflows and can bypass the overload check.
    • Suggested fix: avoid unsigned subtraction here (explicit occupied >= max_tasks_count check first, or saturating free-slot computation).
ClickHouse Rules
Item Status Notes
Deletion logging ✅
Serialization versioning ➖
Core-area scrutiny ⚠️ Remaining correctness issues in core storage/merge paths
No test removal ✅
Experimental gate ➖
No magic constants ✅
Backward compatibility ⚠️ Rename path keeps stale cached storage id without sync
SettingsChangesHistory.cpp ➖
PR metadata quality ✅
Safe rollout ⚠️ Merge blocked until blockers are fixed
Compilation time ✅
No large/binary files ✅
Performance & Safety
  • Unsigned underflow in merge-pool capacity guard can schedule optimize-entire-partition work under overload conditions.
Final Verdict
  • Status: ⚠️ Request changes
  • Minimum required actions: fix the three blockers above (renameInMemory synchronization in StorageObjectStorage, Int64 byte accounting in Iceberg Mutations, and underflow-safe free-slot guard in MergeTreeDataMergerMutator).

@clickhouse-gh clickhouse-gh Bot added the pr-not-for-changelog This PR should not be mentioned in the changelog label May 10, 2026

std::shared_ptr<DataLake::ICatalog> catalog;
StorageID storage_id;
BackgroundJobsAssignee background_operations_assignee;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

BackgroundJobsAssignee caches StorageID and uses it in finish to remove queued tasks by storage id. With this new assignee on StorageObjectStorage, table rename now needs the same renameInMemory handling as MergeTreeData (background_operations_assignee.updateStorageID(new_table_id)).

Without it, after RENAME TABLE the assignee keeps the old id and shutdown/destruction may fail to remove queued tasks for the renamed table.

Please add a renameInMemory override in StorageObjectStorage that calls IStorage::renameInMemory(new_table_id) and then updates background_operations_assignee.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is still reproducible on the current HEAD (78fa1bc9).

StorageObjectStorage now owns background_operations_assignee, but the class still has no renameInMemory override. BackgroundJobsAssignee caches StorageID from construction and uses it in finish for removeTasksCorrespondingToStorage(...).

After RENAME TABLE, IStorage::renameInMemory updates only the base IStorage id, so the assignee keeps the old id and shutdown can leave queued background tasks behind. Please add StorageObjectStorage::renameInMemory that calls IStorage::renameInMemory(new_table_id) and background_operations_assignee.updateStorageID(new_table_id) (and keep storage_id member consistent as well, since it is used in this class).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This still looks unresolved on the current head.

StorageObjectStorage now owns background_operations_assignee, but there is still no renameInMemory override in either StorageObjectStorage.h or StorageObjectStorage.cpp. BackgroundJobsAssignee caches StorageID and uses it in finish for removeTasksCorrespondingToStorage(...), so after RENAME TABLE it can keep removing tasks for the old id.

Please add StorageObjectStorage::renameInMemory that calls IStorage::renameInMemory(new_table_id), background_operations_assignee.updateStorageID(new_table_id), and updates the class storage_id member too (it is used in this class, e.g. in executeCommand/alter paths).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I re-checked this on the current PR head and it is still unresolved: StorageObjectStorage has background_operations_assignee and storage_id, but there is still no renameInMemory override.

After RENAME TABLE, IStorage::renameInMemory updates only the base StorageID; the assignee still keeps the old id used by BackgroundJobsAssignee::finish (removeTasksCorrespondingToStorage(...)). That can leave queued background tasks tied to the old table id.

Please add StorageObjectStorage::renameInMemory that calls IStorage::renameInMemory(new_table_id) and then updates both background_operations_assignee (updateStorageID(new_table_id)) and the class storage_id member.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Re-checking current HEAD: this is still unresolved.

StorageObjectStorage owns background_operations_assignee, but there is still no renameInMemory override updating the cached storage id (background_operations_assignee.updateStorageID(new_table_id)). After RENAME TABLE, finish can still look up jobs by the old id and miss waiting/cleanup for the new id.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Still live on current HEAD (7e8c5200). StorageObjectStorage now owns background_operations_assignee, but there is still no StorageObjectStorage::renameInMemory override that forwards the new id into BackgroundJobsAssignee::updateStorageID.

BackgroundJobsAssignee::finish removes queued tasks by its cached storage_id, so after RENAME TABLE we can leave stale tasks under the old id.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Still unresolved on current HEAD (e774411f).

StorageObjectStorage still has no renameInMemory override, so background_operations_assignee keeps the old StorageID after RENAME TABLE.

BackgroundJobsAssignee::finish removes queued jobs by its cached storage_id, so cleanup can miss jobs that are now associated with the renamed table id. Please add StorageObjectStorage::renameInMemory and update both background_operations_assignee (updateStorageID) and the class storage_id member.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Re-checking current code: this is still unresolved.

StorageObjectStorage still has no renameInMemory override, so after RENAME TABLE the cached id inside background_operations_assignee is not synchronized. BackgroundJobsAssignee::finish removes tasks by cached storage id, so cleanup/wait can target the old id.

Please add StorageObjectStorage::renameInMemory that calls IStorage::renameInMemory(new_table_id), then updates both background_operations_assignee via updateStorageID(new_table_id) and the class member storage_id.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is still a real correctness issue on the current head.

StorageObjectStorage now owns background_operations_assignee and a cached storage_id, but the class still does not override renameInMemory. After RENAME TABLE, background_operations_assignee keeps the old StorageID, so BackgroundJobsAssignee::finish removes tasks for the old name and can leave tasks for the renamed table behind.

Please add StorageObjectStorage::renameInMemory and propagate the new ID to both cached fields, e.g. update storage_id and call background_operations_assignee.updateStorageID(new_table_id).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Re-checking current HEAD (df93162a): this is still unresolved.

StorageObjectStorage has background_operations_assignee and cached storage_id, but there is still no renameInMemory override to propagate table renames to those cached IDs. After RENAME TABLE, IStorage::renameInMemory updates only the base StorageID, while BackgroundJobsAssignee::finish still removes tasks by its cached storage_id.

Please add StorageObjectStorage::renameInMemory that calls IStorage::renameInMemory(new_table_id), then updates both:

  • background_operations_assignee.updateStorageID(new_table_id)
  • storage_id = new_table_id


std::shared_ptr<DataLake::ICatalog> catalog;
StorageID storage_id;
BackgroundJobsAssignee background_operations_assignee;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

BackgroundJobsAssignee caches StorageID and uses it in finish to remove queued tasks by storage id. With this new assignee on StorageObjectStorage, table rename now needs the same renameInMemory handling as MergeTreeData (background_operations_assignee.updateStorageID(new_table_id)).

Without it, after RENAME TABLE the assignee keeps the old id and shutdown/destruction may fail to remove queued tasks for the renamed table.

Please add a renameInMemory override in StorageObjectStorage that calls IStorage::renameInMemory(new_table_id) and then updates background_operations_assignee.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This still looks unresolved on the current head.

StorageObjectStorage now owns background_operations_assignee, but there is still no renameInMemory override in either StorageObjectStorage.h or StorageObjectStorage.cpp. BackgroundJobsAssignee caches StorageID and uses it in finish for removeTasksCorrespondingToStorage(...), so after RENAME TABLE it can keep removing tasks for the old id.

Please add StorageObjectStorage::renameInMemory that calls IStorage::renameInMemory(new_table_id), background_operations_assignee.updateStorageID(new_table_id), and updates the class storage_id member too (it is used in this class, e.g. in executeCommand/alter paths).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Still unresolved on current code.

The BackgroundJobsAssignee path added in this PR still lacks the renameInMemory synchronization that MergeTree has. StorageObjectStorage needs to update assignee storage id on rename to keep finish/job lookup consistent after RENAME TABLE.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Still live on current HEAD.

StorageObjectStorage gained BackgroundJobsAssignee, but rename synchronization is still missing: there is no renameInMemory override that updates the assignee's cached id.

Please mirror the MergeTree pattern (IStorage::renameInMemory(new_table_id) + background_operations_assignee.updateStorageID(new_table_id) + updating local storage_id).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Re-checking current HEAD: this is still live for the same reason — no StorageObjectStorage::renameInMemory override updates background_operations_assignee/storage_id on rename. The assignee keeps a stale storage id after RENAME TABLE.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Re-checking current code: this is still unresolved for the same reason. StorageObjectStorage has background scheduling hooks, but no renameInMemory synchronization with BackgroundJobsAssignee::updateStorageID.

Please add StorageObjectStorage::renameInMemory to keep the assignee storage id in sync after rename.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Re-checking current code: this is still live for the same reason.

StorageObjectStorage gained background scheduling hooks but still does not synchronize rename state with BackgroundJobsAssignee (updateStorageID) in renameInMemory. The cached id can diverge after RENAME TABLE.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Still live on current code for the same reason: StorageObjectStorage gained background job scheduling but does not override renameInMemory to propagate the new StorageID into BackgroundJobsAssignee.

Without this sync, finish may keep operating on stale table id after RENAME TABLE.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Re-checking current head: this thread is still live for the same reason.

StorageObjectStorage has no renameInMemory override, so rename does not propagate the new StorageID to background_operations_assignee (or to the class-local storage_id). That leaves background-task cleanup keyed by stale id after RENAME TABLE.

@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you all sign our Contributor License Agreement before we can accept your contribution.
1 out of 2 committers have signed the CLA.

✅ scanhex12
❌ Ubuntu


Ubuntu seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account.
You have signed the CLA already but the status is still pending? Let us recheck it.

Int32 total_rows;
Int32 total_bytes;
Int64 total_rows;
Int64 total_bytes;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

DeleteFileWriteResult::total_bytes is now Int64, but the write paths still assign through static_cast<Int32> (delete_data_result[...].total_bytes and update_data_result[...].total_bytes).

That still truncates file sizes above 2 GiB and records incorrect Iceberg snapshot statistics. Please remove the narrowing cast and store full-size values in both assignments.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is still live in current HEAD: both write paths still assign total_bytes through static_cast<Int32>:

  • delete_data_result[...].total_bytes = static_cast<Int32>(delete_bytes);
  • update_data_result[...].total_bytes = static_cast<Int32>(update_bytes);

DeleteFileWriteResult::total_bytes is now Int64, so these casts still truncate values above 2 GiB.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Re-checked on current HEAD: both narrowing assignments are still present:

  • delete_data_result[partition_key].total_bytes = static_cast<Int32>(delete_bytes);
  • update_data_result[partition_key].total_bytes = static_cast<Int32>(update_bytes);

DeleteFileWriteResult::total_bytes is Int64, so values above 2 GiB are still truncated in metadata accounting.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Still unresolved. Both assignments in Mutations.cpp still narrow to Int32:

  • delete_data_result[partition_key].total_bytes = static_cast<Int32>(delete_bytes);
  • update_data_result[partition_key].total_bytes = static_cast<Int32>(update_bytes);

DeleteFileWriteResult::total_bytes is Int64, so sizes above 2 GiB are still truncated in metadata accounting.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Still unresolved on current HEAD: DeleteFileWriteResult::total_bytes is Int64, but both write paths still narrow through static_cast<Int32>:

  • delete_data_result[partition_key].total_bytes = static_cast<Int32>(delete_bytes);
  • update_data_result[partition_key].total_bytes = static_cast<Int32>(update_bytes);

This truncates values above 2^31-1 and can emit incorrect Iceberg metadata. These assignments should keep Int64 end-to-end.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Still unresolved on current HEAD (e774411f): both assignments still narrow Int64 sizes to Int32.

  • delete_data_result[partition_key].total_bytes = static_cast<Int32>(delete_bytes);
  • update_data_result[partition_key].total_bytes = static_cast<Int32>(update_bytes);

DeleteFileWriteResult::total_bytes is Int64, so values above 2^31 - 1 are still truncated in Iceberg metadata accounting. These should be assigned without narrowing.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Still unresolved on current code.

DeleteFileWriteResult::total_bytes is Int64, but both assignments in Mutations.cpp still narrow via static_cast<Int32>:

  • delete_data_result[partition_key].total_bytes = static_cast<Int32>(delete_bytes);
  • update_data_result[partition_key].total_bytes = static_cast<Int32>(update_bytes);

This truncates sizes above 2^31 - 1 in Iceberg metadata accounting. Please assign delete_bytes / update_bytes directly as Int64.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is still live on current code: both write paths narrow byte counts to Int32 even though DeleteFileWriteResult::total_bytes is Int64.

  • delete_data_result[partition_key].total_bytes = static_cast<Int32>(delete_bytes);
  • update_data_result[partition_key].total_bytes = static_cast<Int32>(update_bytes);

For files larger than 2 GiB, this truncates/overflows metadata and produces incorrect total_bytes. Assign delete_bytes / update_bytes directly (or cast to Int64) instead of Int32.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Re-checking current HEAD (df93162a): still unresolved.

Both paths still narrow Int64 byte counters to Int32:

  • delete_data_result[partition_key].total_bytes = static_cast<Int32>(delete_bytes);
  • update_data_result[partition_key].total_bytes = static_cast<Int32>(update_bytes);

DeleteFileWriteResult::total_bytes is Int64, so files above 2 GiB still truncate/overflow metadata accounting. Please assign as Int64 end-to-end (direct assignment or static_cast<Int64>).


size_t occupied = CurrentMetrics::values[CurrentMetrics::BackgroundMergesAndMutationsPoolTask].load(std::memory_order_relaxed);
size_t max_tasks_count = context->getMergeMutateExecutor()->getMaxTasksCount();
if (occupied > 1 && max_tasks_count - occupied < (*settings)[MergeTreeSetting::number_of_free_entries_in_pool_to_execute_optimize_entire_partition])

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

occupied/max_tasks_count were changed from signed to size_t, and this expression can now underflow:

max_tasks_count - occupied

When occupied > max_tasks_count (possible with transient metric skew), subtraction wraps to a huge unsigned value and the guard is bypassed exactly when the pool is already overloaded.

Please keep these as signed integers (as before), or guard the subtraction explicitly (occupied >= max_tasks_count first).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Still unresolved on current code: occupied/max_tasks_count are size_t, and the condition still subtracts unsigned values:

max_tasks_count - occupied

When occupied >= max_tasks_count, this wraps and can bypass the "not enough idle threads" guard exactly in overload conditions. Please add an explicit occupied >= max_tasks_count check before subtraction (or use signed arithmetic as before).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Still unresolved. In getBestPartitionToOptimizeEntire, both occupied and max_tasks_count are size_t, and the guard still does unsigned subtraction:

max_tasks_count - occupied < number_of_free_entries_in_pool_to_execute_optimize_entire_partition

If occupied >= max_tasks_count, subtraction wraps and can bypass the overload guard. Please add an explicit occupied >= max_tasks_count check (or use signed arithmetic as before) before subtracting.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Still live: both occupied and max_tasks_count are size_t, and the guard still uses max_tasks_count - occupied.

When occupied > max_tasks_count (possible with races or dynamic pool-size changes), unsigned subtraction underflows and the condition becomes false, so optimize-entire-partition can run even with no free worker slots. Please compute free slots in a non-underflowing way (for example, clamp to zero before comparing).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Still live on current HEAD (e774411f).

In getBestPartitionToOptimizeEntire, both occupied and max_tasks_count are size_t, but the guard still computes max_tasks_count - occupied.

When occupied >= max_tasks_count, unsigned subtraction underflows and the "not enough idle threads" check can be bypassed. Please compute free slots in a non-underflowing way (for example, explicit occupied >= max_tasks_count check before subtraction).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Still unresolved on current code.

In getBestPartitionToOptimizeEntire, occupied and max_tasks_count are size_t, and the guard still computes max_tasks_count - occupied.

When occupied >= max_tasks_count, unsigned subtraction underflows and can bypass the idle-thread check exactly in overload conditions. Please guard explicitly (occupied >= max_tasks_count) before subtraction, or compute free slots with saturation to zero.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This guard is still unsafe after switching to size_t:

max_tasks_count - occupied < number_of_free_entries_in_pool_to_execute_optimize_entire_partition

If occupied >= max_tasks_count (possible during transient races or after pool-size reconfiguration), unsigned subtraction underflows and the condition becomes false, so the code may continue as if free slots exist.

Please avoid subtraction on unsigned values here, for example by checking occupied >= max_tasks_count first, or by comparing as occupied + required_free_entries > max_tasks_count with overflow-safe logic.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Re-checking current HEAD (df93162a): this guard is still unsafe.

occupied and max_tasks_count are size_t, and the condition still computes:

max_tasks_count - occupied < number_of_free_entries_in_pool_to_execute_optimize_entire_partition

If occupied >= max_tasks_count, unsigned subtraction underflows and the overload guard can be bypassed. Please avoid subtracting unsigned values here (for example, explicit occupied >= max_tasks_count check before subtraction, or compute free slots with saturation to zero).

@scanhex12 scanhex12 self-assigned this May 11, 2026
@scanhex12
scanhex12 enabled auto-merge May 11, 2026 15:27
@clickhouse-gh

clickhouse-gh Bot commented May 11, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.10% 84.10% +0.00%
Functions 91.10% 91.10% +0.00%
Branches 76.50% 76.50% +0.00%

Changed lines: 69.90% (267/382) | lost baseline coverage: 4 line(s) · Uncovered code

Full report · Diff report

@scanhex12
scanhex12 added this pull request to the merge queue May 12, 2026
Merged via the queue into ClickHouse:master with commit 1a7ea50 May 12, 2026
162 of 165 checks passed
@scanhex12
scanhex12 deleted the vedernikov/expose-merge-utils-and-storage-objstorage-bg-hooks branch May 12, 2026 08:14
@robot-clickhouse robot-clickhouse added the pr-synced-to-cloud The PR is synced to the cloud repo label May 12, 2026
return ranges_by_partitions;
}

String getBestPartitionToOptimizeEntire(

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 remember here #94104 we discussed and concluded that changes to the merge mutator were not needed, at least from the MergeTree side. What changed?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-not-for-changelog This PR should not be mentioned in the changelog 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.

4 participants