Skip to content

Fix the S3Queue migration to buckets for an Ordered queue with at most one bucket - #130798

Merged
alexey-milovidov merged 5 commits into
ClickHouse:masterfrom
groeneai:s3queue-migrate-from-one-bucket
Nov 12, 2026
Merged

alexey-milovidov merged 5 commits into
ClickHouse:masterfrom
groeneai:s3queue-migrate-from-one-bucket

Conversation

@groeneai

@groeneai groeneai commented Nov 11, 2026 •

Copy link
Copy Markdown
Collaborator

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):

Fixed ALTER TABLE ... MODIFY SETTING buckets with s3queue_migrate_old_metadata_to_buckets = 1 on an Ordered S3Queue or AzureQueue table with at most one bucket (buckets = 1, or buckets unset with processing_threads_num = 1). Migrating such a table to more buckets made it stop ingesting new files, and migrating it to one bucket made it ingest its already processed files again after a restart.

Description

Related: #130733.

On 26.10.2.103, after SET s3queue_migrate_old_metadata_to_buckets = 1; ALTER TABLE q MODIFY SETTING buckets = 2 on an Ordered queue created with buckets = 1, the queue never ingests another file (Failed to set file processing, error: No node. (LOGICAL_ERROR)). With buckets unset and processing_threads_num = 1, ALTER ... buckets = 1 makes the queue ingest all processed files again after a restart (measured: 33 rows for 17 files).

The readers treat at most one bucket as the layout without buckets (<keeper_path>/processed), and more as one buckets/<i>/processed per bucket. ObjectStorageQueueOrderedFileMetadata::migrateToBuckets disagreed: for a buckets = 1 source it read buckets/0/processed; without a pointer it returned before creating the buckets/<i> nodes, yet the queue switched to the new count; and it migrated to the raw setting, so a target of 1 moved the pointer where a one-bucket queue does not read it, and a target of 0 created no buckets.

The migration now targets the new effective bucket count: buckets = 0 migrates to processing_threads_num buckets, and a migration to one bucket no longer touches Keeper (behaviour change). It migrates the greater of the pointers in <keeper_path>/processed and, while the table metadata in Keeper still has at most one bucket, in buckets/0/processed, where older versions put it on a migration to one bucket. The bucket nodes are created in the same Keeper transaction, so a refused migration leaves nothing behind. Migrating a partitioned buckets = 1 queue is now refused, as with buckets unset; before, it was accepted and the pointers were left unread.

Validation: 7 of the 8 cases of the new test 05620_s3queue_ordered_migrate_from_one_bucket fail on master and all pass with the fix. End to end with MinIO the queue keeps ingesting without duplicates, also right after 26.10.2.103 migrated it to one bucket.


Workflow [PR]
Sync PR [sync-upstream/pr/130798]

Version info

  • Merged into: 26.11.1.2025-master (included in 26.11 and later)

groeneai and others added 4 commits November 11, 2026 11:08
…t one bucket

The readers of the Ordered Keeper layout treat a queue with at most one
bucket as the layout without buckets (last processed file in
`<keeper_path>/processed`) and a queue with more buckets as one
`buckets/<i>/processed` per bucket (`useBucketsForProcessing`).
`ObjectStorageQueueOrderedFileMetadata::migrateToBuckets`, run by
`ALTER TABLE ... MODIFY SETTING buckets` under
`s3queue_migrate_old_metadata_to_buckets = 1`, disagreed with that:

- for a source created with `buckets = 1` it read the pointer from
  `buckets/0/processed`, which such a queue never writes;
- when it found no pointer it returned before creating the `buckets/<i>`
  nodes, while `migrateToBucketsInKeeper` still switched the queue to the
  new count. The readers then look for nodes that do not exist: a debug
  build aborts on `chassert(bucket_exists)` in `tryAcquireBucket`, a release
  build loops on `Failed to set file processing, error: No node` and never
  ingests another file (measured on 26.10.2.103);
- it migrated to the raw `buckets` value: a target of 1 moved the pointer
  to `buckets/0/processed` and removed `<keeper_path>/processed`, so after a
  restart the queue ingests every processed file again (measured on
  26.10.2.103: 32 rows for 16 files); a target of 0 created no bucket
  although the queue then uses `processing_threads_num` buckets.

Now `migrateToBucketsInKeeper` passes the new effective bucket count
(`getBucketsNum` of the new metadata), and `migrateToBuckets` does nothing
when that count is at most 1, creates the bucket nodes before looking for
the pointer, and looks for it first in `buckets/0/processed` (where older
versions put it after a migration to one bucket, and where the initiator
replica has just put it) and then in `<keeper_path>/processed`. The
"migrated concurrently by another replica" return is keyed on the source
being bucket 0's node, which is what `prev_value != 0` meant before.

A `buckets = 1` source whose `processed` node has children (partitioned
layout) is now refused with `SUPPORT_IS_DISABLED`, as an unset source
already was, instead of being switched to a layout without a pointer.

The new test is a new file rather than a 05339 extension, because ClickHouse#130733
extends 05339.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
A `buckets = 1` Ordered queue keeps its last processed file in
`<keeper_path>/processed`, but older versions migrated `buckets = 1` into
`buckets/0/processed` and the queue then kept writing the root node again.
Such a queue can therefore carry two pointers, and the root one is the
newer one as soon as a file arrived after that migration. Taking the first
pointer found copied the stale bucket-0 one into every bucket and ingested
the files between the two pointers again. Every file up to the greatest
pointer is processed, so the migration now takes the greatest `file_path`
of all pointers it finds and removes all of them in the same multi.

`buckets/0/processed` is such a legacy whole-queue pointer only while the
bucketed layout does not exist yet. After this change a server that
migrates an empty queue creates `buckets/<i>` and can then process bucket-0
files, so another server running the same `ALTER` later would find a live
bucket-0 pointer; copying it into bucket 1 would skip that bucket's files.
`<keeper_path>/buckets/1` exists exactly when some server already created
the bucketed layout, so bucket 0 is a candidate only if it did not exist
before the migration started. It is checked once, before the retry loop: a
retry after a Keeper session loss must not see our own `buckets/1` and drop
a legacy source.

If removing a pointer fails with ZNONODE, another server migrated it in the
meantime, so the lookup is repeated (the previous check of the first
response made that branch unreachable); any other failure of a remove is
reported as a concurrent modification, and an existing bucket pointer means
the migration was already done.

The test gets cases for a bucket-0 pointer of an older version, for both
pointers with either one greater, and for a live bucket-0 pointer of
another server. Those nodes are written with `INSERT INTO system.zookeeper`.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
`migrateToBuckets` used to create `buckets/<i>` with `createAncestors`
before looking for the pointer. `createAncestors` is its own Keeper
transaction, so those nodes were committed even when the migration multi
then failed: a source remove rejected as a concurrent modification, a
partitioned source whose `processed` node has children, or a Keeper error
that `StorageObjectStorageQueue::alter` retries around `alterSettings`.
The next attempt found `buckets/1`, which is how the migration tells
"another server already migrated" from "bucket 0 still holds the pointer
older versions put there for `buckets = 1`", so it dropped the legacy
bucket-0 candidate, failed with ZNODEEXISTS on `buckets/0/processed` and
returned as if migrated: the root pointer orphaned, bucket 0 stale and
bucket 1 without a pointer, so every bucket-1 file was ingested again.

The missing bucket nodes are now part of the same multi as the migrated
pointers (with nothing to migrate nothing can be lost, so they are created
directly). `buckets/1` therefore exists only after a committed migration,
and the discriminator is re-derived on every iteration from the same
`exists()` pass that decides which bucket nodes the multi creates: a
snapshot taken once could be made stale by another server that migrates an
empty queue and then processes a bucket-0 file, whose live pointer would be
copied into bucket 1. When bucket 0 is used as the legacy source the multi
contains `create buckets/1`, so a concurrent creator makes it fail with
ZNODEEXISTS on a bucket node, which now means "re-check" instead of
"already migrated".

Test case 8 refuses a `buckets = 1` source whose `processed` node has a
child (the shape of a partitioned queue's pointers) and checks that the
refusal created no `buckets/1`.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Drop a sentence that narrated the remove requests built right below it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai

Copy link
Copy Markdown
Collaborator Author
Internal second-model review (rounds 1-3): 6 fixed, 2 declined
  • ❌ Fixed: a dual pointer left by an older version's migration to one bucket (buckets/0/processed plus a newer <keeper_path>/processed) now migrates the greater pointer and removes both.
  • ❌ Fixed: bucket 0's node is taken as a legacy pointer only while buckets/1 does not exist; otherwise it is that bucket's own pointer and is not copied.
  • ❌ Fixed: the bucket nodes are created in the same Keeper transaction as the migrated pointers, so a refused or failed migration leaves no buckets/1 that would make the retry ignore the legacy pointer.
  • ❌ Fixed: a replica that creates buckets/1 while another one migrates now makes that migration re-check instead of copying a live bucket-0 pointer.
  • ❌ Declined (out of scope): a server that keeps streaming on the one-bucket layout after another server migrated writes a new <keeper_path>/processed, which its own later migration leaves in place. Such a server already ingests processed files again once the pointer is gone (a missing pointer reads as nothing processed), also on master's path for buckets unset; changing buckets requires detached materialized views on each server.
  • ⚠️ Fixed: a source node removed concurrently by another replica now triggers a re-check instead of an error.
  • ⚠️ Fixed: the test now covers the refused migration of a buckets = 1 queue whose pointer node has children.
  • ⚠️ Declined (out of scope): a second ALTER ... buckets on a queue whose effective bucket count is already above one is guarded by Refuse to migrate an Ordered S3Queue that already uses buckets #130733.
  • 💡 Noted: the header of test case 8 mentions the next attempt, which the case does not run (under a Replicated database the holder leaves a child under the pointer, so the retry stays refused).

@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. 05620_s3queue_ordered_migrate_from_one_bucket fails on every run on master (7 of its 8 cases differ from the reference; case 5 guards a layout master already migrates) and passes with the fix. End-to-end: a buckets = 1 queue migrated to 2 buckets never ingests another file (debug build: chassert(bucket_exists) abort on the next bucket lock).
b Root cause explained? Readers treat at most one bucket as the layout without buckets (<keeper_path>/processed) and more buckets as buckets/<i>/processed. migrateToBuckets read a buckets = 1 source's pointer from buckets/0/processed, returned before creating the buckets/<i> nodes when it found no pointer, and migrated to the raw setting (target 1 moved the pointer where a one-bucket queue does not read it; target 0 created no buckets).
c Fix matches root cause? Yes. The migration produces exactly the layout the readers derive from the new effective bucket count, from the greatest of the pointers a buckets = 1 queue can have; readers, guards and refusal paths are unchanged.
d Test intent preserved / new tests added? New SQL test, Keeper only, 8 cases; older-version and other-server pointers are written with INSERT INTO system.zookeeper. Reverting any single part of the fix makes its case fail (mutant builds). 05339 is unchanged and passes.
e Both directions demonstrated? Yes: test FAIL on master / OK with the fix; end-to-end with MinIO: master aborts, or ingests processed files again (all 16 after a migration to one bucket and a restart; f_016..f_019 after an upgrade from a 26.10.2.103 layout with both pointers), the fix ingests only the new file.
f Fix is general across code paths? migrateToBuckets is the only writer of this layout transition; AzureQueue shares it. A second server running the same ALTER (two servers, one Keeper) returns without changes, also after the first one already processed bucket-0 files (bucket 1 keeps no pointer, every file ingested once). A source whose processed node has children (partitioned layout) is refused with SUPPORT_IS_DISABLED and creates no bucket node.
g Fix generalizes across inputs (params/datatypes/wrappers)? Sources buckets unset / buckets = 1 with processing_threads_num 1 and 2, targets 0 / 1 / 2, with and without a pointer, a pointer in bucket 0 only, in both nodes with either one greater, and a live bucket-0 pointer of another server. No data types involved.
h Backward compatible? (maintainer-approved exception only) No setting, default or format change. Behaviour change stated in the description: a migration to an effective count of 1 no longer touches Keeper, and buckets = 0 migrates to processing_threads_num buckets. Layouts from older versions are read.
i Invariants and contracts preserved? The in-memory metadata switches only after the Keeper multi succeeds (unchanged); missing bucket nodes are created in the same multi as the migrated pointers (directly only when there is nothing to migrate), so a refused or failed attempt leaves no buckets/1; the multi is remove-every-found-pointer + create-every-bucket, so a concurrent change of a removed pointer aborts it. 50/50 randomized runs, 10/10 with a Replicated database.

Session id: cron:clickhouse-impl-self-slot-57:20261111-123600

@clickhouse-gh

clickhouse-gh Bot commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [5eee039]

Summary: ✅


AI Review

Summary

Reviewed #130798 at 5eee039f43d8922a9d6ffeabcfa36e6aeb97bb99. The change fixes migration of ordered S3Queue and AzureQueue metadata from at most one effective bucket, preserving processed pointers and creating the required bucket layout. No unresolved findings remain. The previously reported interrupted legacy migration is fixed by consulting Keeper table metadata and checking its version in the migration transaction; the corresponding bot-authored thread was resolved.

Missing context / blind spots
  • ⚠️ Latest-head targeted CI results were not yet published, and no local ClickHouse binary was available. The preceding revision, b329f5a62112, passed all 50 runs of 05620_s3queue_ordered_migrate_from_one_bucket in the targeted ASan/UBSan report. Successful latest-head CI would confirm execution of the revised interrupted-migration case.
Final Verdict

Status: ✅ Approve. No further code changes requested; latest-head CI validation remains pending.

@clickhouse-gh clickhouse-gh Bot added pr-bugfix Pull request with bugfix, not backported by default can be tested Allows running workflows for external contributors comp-s3queue S3Queue/AzureQueue engines (queue-based ingestion from object storage). labels Nov 11, 2026
@clickhouse-gh

clickhouse-gh Bot commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 5eee039f4 with master da87c619e (stripped binary size, per-symbol sizes and ThinLTO time; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Object file sizes

4 object files changed (+39.52 KiB total), 0 added.

Object file Master PR Δ
src/CMakeFiles/dbms.dir/Storages/ObjectStorageQueue/ObjectStorageQueueOrderedFileMetadata.cpp.o 572.80 KiB 612.15 KiB +39.35 KiB (+6.87%)

711 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only clickhouse-bundle) and not compared.

Slowest function optimization changes (ThinLTO)

Median per-function time ratio to the master baseline: programs/clickhouse ×0.67, programs/clickhouse-keeper ×0.64 (each binary is linked separately, on a different machine and with different build flags); deltas below are relative to it.

Binary Function Master PR Δ vs median
programs/clickhouse DB::registerFunctionConversion(DB::FunctionFactory&) 5.8 s 14.6 s +10.7 s (+275%)
programs/clickhouse _ZN6detail10static_forITk7is_enumN2DB23AttributeUnderlyingTypeEZNS1_29callOnDictionaryAttributeType… 3.4 s 7.6 s +5.3 s (+232%)
Compile time of recompiled translation units

8 translation units recompiled, 26 s compile time in total, 8 of them have a recent master baseline.

Job report

… buckets

`buckets/1` is not a completion marker: older versions of `migrateToBuckets`
created the bucket nodes outside the migration transaction, so a server
stopping between the two leaves `buckets/1` while bucket 0 still holds the
pointer of the whole one-bucket queue. Retrying the migration then skipped
that pointer and switched the queue to two buckets with nothing in bucket 1,
so every file of bucket 1 was ingested again.

The `buckets` value in `<keeper_path>/metadata` is written only after a
migration, so it is the committed state: while it has at most one bucket,
bucket 0 of a `buckets = 1` queue is the queue's legacy pointer; once it has
more, bucket 0 belongs to bucket 0 only. The metadata version is checked in
the migration transaction, so a concurrent migration makes it recheck.

Test case 7 now covers both: `buckets/1` left by an interrupted migration,
and a migration committed by another server.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

@alexey-milovidov alexey-milovidov 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.

Not entirely trivial, but ok.

@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Nov 12, 2026
@alexey-milovidov alexey-milovidov self-assigned this Nov 12, 2026
Merged via the queue into ClickHouse:master with commit f043345 Nov 12, 2026
203 checks passed
@robot-ch-test-poll robot-ch-test-poll added the pr-synced-to-cloud The PR is synced to the cloud repo label Nov 12, 2026
BorisTyshkevich pushed a commit to BorisTyshkevich/ClickHouse that referenced this pull request Nov 15, 2026
…ne_bucket in a Memory database

Stress test (amd_msan) failed with "Cannot start clickhouse-server": the
restart could not load q_peer, "Existing table metadata in ZooKeeper
differs in buckets setting. Stored in ZooKeeper: 2, local: 1
(METADATA_MISMATCH)".

The test rewrites the Keeper metadata of q_peer to "buckets":2 to simulate
a migration by another server, then runs ALTER TABLE q_peer MODIFY SETTING
buckets = 2. In between, Keeper has 2 buckets and the table's .sql has 1,
which ATTACH refuses. The stress run's global time limit killed the client
right after the rewrite, stress.py detached (not dropped) the database,
and the restart with async_load_databases = false failed on the table.

Only q_peer has such a state: run up to every statement boundary of the
test, then detached and restarted, every other table loads. So only q_peer
moves into a Memory database, {CLICKHOUSE_DATABASE_1:Identifier}, whose
table metadata a restart never loads, as 05234 does (ClickHouse#122101). USE makes
currentDatabase() and the {database} macro of keeper_path name that
database, so the queries and the reference are unchanged; the other cases
keep running in the test database, including its Replicated variant.
DROP DATABASE is the cleanup because ignore_drop_queries_probability never
ignores it.

Related: ClickHouse#130798

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors comp-s3queue S3Queue/AzureQueue engines (queue-based ingestion from object storage). pr-bugfix Pull request with bugfix, not backported by default 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.

3 participants