Skip to content

Refresh S3Queue bucket locks so the TTL cleanup does not remove them mid-processing - #110292

Merged
kssenii merged 22 commits into
masterfrom
s3queue-ttl-and-elapsed-log
Jul 27, 2026
Merged

kssenii merged 22 commits into
masterfrom
s3queue-ttl-and-elapsed-log

Conversation

@kssenii

@kssenii kssenii commented Jul 13, 2026 •

Copy link
Copy Markdown
Member

Linked issues

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

Fix S3Queue/AzureQueue ordered mode with persistent processing nodes: bucket locks are now refreshed during streaming, so that the TTL cleanup (persistent_processing_node_ttl_seconds) does not remove locks of a live processor. If lock ownership is nevertheless lost, it is detected and reported (a logical error and the ObjectStorageQueueBucketLockLostOwnership profile event), and streaming recovers with a fresh file iterator.

Version info

  • Merged into: 26.8.1.195 (included in 26.8 and later)
  • Backported to: 26.7.2.23, 26.6.2.125, 26.5.6.83, 26.3.17.84

…oViews by it

Change the default of `persistent_processing_node_ttl_seconds` from 6 hours
to 24 hours. Log elapsed time of the whole `streamToViews` execution and stop
starting new iterations once elapsed time reaches half of the TTL, so that
the execution finishes before processing nodes and the bucket lock could be
cleaned up as abandoned.

Co-Authored-By: Claude Fable 5 <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Jul 13, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [ff76e2c]

Summary: ❌

job_name test_name status info comment
Stateless tests (amd_llvm_coverage, old analyzer, s3 storage, DBReplicated, WasmEdge, parallel, 2/3) FAIL
01906_lc_in_bug ERROR cidb IGNORED
Server died FAIL cidb IGNORED
Logical error '!part.empty()' (STID: 2735-47bf) FAIL cidb, issue ISSUE CREATED
Stress test (arm_asan_ubsan) FAIL
Cannot start clickhouse-server FAIL cidb IGNORED
Check failed FAIL cidb IGNORED
Upgrade check (amd_release) FAIL
Error message in clickhouse-server.log (see upgrade_error_messages.txt) FAIL cidb IGNORED

AI Review

Summary

This PR adds bucket-lock refreshes and lost-ownership detection for S3Queue/AzureQueue ordered mode, but the fix is still incomplete on current HEAD. The new heartbeat only protects some ordered paths: long in-flight read/insert/commit sections can still outlive persistent_processing_node_ttl_seconds, direct SELECT ... SETTINGS commit_on_select = 1 still has no corresponding refresh, single-bucket ordered mode never refreshes its live processing node at all, and the live-setting plumbing is still only half-updated. I would not merge it in the current state.

Findings

❌ Blockers

  • [src/Storages/ObjectStorageQueue/ObjectStorageQueueSource.cpp:502-517] The new heartbeat only runs when use_buckets_for_processing is true. In ordered queues whose effective bucket count is 1 (processing_threads_num = 1 or buckets = 1), there is no bucket lock to refresh, so the only live ownership is the per-file /processing/<file> node. cleanupPersistentProcessingNodes still deletes that node by mtime, and this path never refreshes it while reader->pull(), insert, or commit is running. A second replica can then recreate the processing node and process the same file concurrently. Suggested fix: heartbeat the per-file processing node on the single-bucket ordered path too, or reject non-zero persistent_processing_node_ttl_seconds when useBucketsForProcessing() is false.
  • [src/Storages/ObjectStorageQueue/StorageObjectStorageQueue.cpp:1179-1217] [dismissed by author -- https://github.com/Refresh S3Queue bucket locks so the TTL cleanup does not remove them mid-processing #110292#discussion_r3614636119] The PR contract still says live locks are refreshed during streaming, but the refresh only happens before next() and after commit. One slow reader->pull(), MV insert, after_processing action, or Keeper retry loop can still exceed persistent_processing_node_ttl_seconds before any refresh site is reached, so the cleanup thread can delete a live bucket lock mid-file or mid-commit and another replica can reprocess the same bucket. I still consider this real because the current code contains no heartbeat inside those long-running sections. Suggested fix: move the refresh into the long-running read/insert/commit paths themselves or run a dedicated refresher while a holder is live.
  • [src/Storages/ObjectStorageQueue/ObjectStorageQueueSource.cpp:1697-1745] The supported direct-SELECT path with commit_on_select = 1 still has no equivalent refresh around postProcessor.process() or the Keeper retry loop in ObjectStorageQueueSource::commit. A slow direct commit can therefore lose a live bucket lock after all rows were read but before the file state is committed, which reopens the same duplicate-processing race on that entrypoint. Suggested fix: share the same lock-heartbeat wrapper between MV streaming commits and direct-select commits.

⚠️ Majors

  • [src/Storages/ObjectStorageQueue/ObjectStorageQueueMetadata.cpp:120-124,1242-1245,1472-1483] The cleanup enablement is still a constructor-time snapshot in cleanup_processing_files, while persistent_processing_node_ttl_seconds and use_persistent_processing_nodes are advertised as live settings. If a table starts with persistent_processing_node_ttl_seconds = 0, cleanup_processing_files stays false forever, so ALTER ... MODIFY SETTING persistent_processing_node_ttl_seconds = N never starts cleaning abandoned locks or processing nodes. In the opposite direction, once the snapshot is true, altering use_persistent_processing_nodes cannot disable that cleanup path. Suggested fix: derive the cleanup condition from the live atomics, and activate/deactivate or recreate the cleanup task when these settings change.
  • [tests/integration/test_storage_s3_queue/test_file_iterator_ttl.py:40-92] The TTL regression coverage is still too weak for the behavior this PR claims. The test only proves the frequent-next() case, where refresh runs often enough already; it never lets the real cleanup thread race one live long-running file, and it still does not cover the single-bucket/per-file-node path or the direct-SELECT commit path. Suggested fix: add a focused repro where one active file genuinely outlives persistent_processing_node_ttl_seconds without manually stealing the lock, and cover both bucketed and single-bucket ordered mode.
Final Verdict

Changes requested: the current implementation still leaves live-ownership expiry holes on supported ordered-mode paths, and the live-setting behavior around persistent_processing_node_ttl_seconds is still inconsistent.

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Jul 13, 2026
@mintlify

mintlify Bot commented Jul 13, 2026 •

Copy link
Copy Markdown
Contributor

Preview deployment for your docs. Learn more about Mintlify Previews.

Project Status Preview Updated (UTC)
ClickHouse-docs 🟢 Ready View Preview Jul 14, 2026, 2:25 AM

@watson-ch

watson-ch Bot commented Jul 13, 2026

Copy link
Copy Markdown

Backport labels have been automatically added based on https://github.com/clickhouse/clickhouse-core-incidents/issues/714

Labels added:

  • v26.2-must-backport
  • v26.4-must-backport
  • v26.5-must-backport
  • v26.6-must-backport

These labels ensure backport PRs are properly created for each affected release version. The release automation will create backport PRs as needed based on these labels.

@kssenii
kssenii marked this pull request as draft July 13, 2026 17:24
kssenii and others added 7 commits July 13, 2026 19:26
When streamToViews breaks out of the loop because elapsed time reached
half of persistent_processing_node_ttl_seconds, drop the shared file
iterator instead of keeping it cached. releaseFinishedBuckets keeps the
last non-finished bucket holder, so a cached iterator would keep holding
the bucket lock with a stale mtime - the exact situation the early stop
is meant to avoid. Dropping the iterator releases all held buckets (via
BucketHolder destructors) once the last streaming task stops using it,
and the next execution re-acquires bucket locks with a fresh iterator.

Co-Authored-By: Claude Fable 5 <[email protected]>
…user

Add a force_release_unfinished flag to releaseFinishedBuckets which allows
to release also the last non-finished bucket holder. When streamToViews
stops on reaching half of persistent_processing_node_ttl_seconds and no
other streaming task uses the shared file iterator (checked via use_count
under streaming_mutex, which prevents new copies), release all buckets
explicitly and keep the iterator cached, so the next execution reuses its
listing state and re-acquires buckets with fresh lock nodes. If other
streaming tasks still use the iterator (parallel_inserts), force release
is not safe - they can have in-flight files from the non-finished bucket -
so keep dropping the shared iterator and let the buckets be released when
the last task stops using it.

Co-Authored-By: Claude Fable 5 <[email protected]>
The previous stop condition measured each streamToViews execution
separately, but bucket locks are held by the shared file iterator, which
outlives a single execution: with parallel_inserts, a task which adopted
the iterator just before another task dropped it could keep streaming for
another half TTL on its own clock, so in the worst case the bucket lock
hold time approached the full persistent_processing_node_ttl_seconds and
cleanup could remove a lock still in use.

Base the stop condition on the iterator age instead (no bucket lock can
be held longer than the iterator which acquired it), so all streaming
tasks sharing the iterator stop within their current iteration once the
age reaches half of the TTL. The task which stops last (use_count == 1
after the shared pointer is dropped, when no new user can appear) force
releases all buckets explicitly; BucketHolder destructors remain as
a backstop in case of a race between the last two users.

Co-Authored-By: Claude Fable 5 <[email protected]>
Replace the file iterator age clock with the age of the oldest currently
held bucket lock (a stopwatch on BucketHolder started at acquisition),
which is the resource the TTL cleanup actually applies to. With no locks
held (e.g. unordered mode) the check never fires, so the iterator is not
pointlessly dropped and re-listed every half TTL, and a lock acquired
late in the iterator lifetime gets its full half-TTL of work.

Since releasing the locks resets this clock, the sole-user case no longer
needs to stop streaming at all: release the buckets under streaming_mutex
(so no other task can adopt the iterator mid-release) and continue with
the same iterator, re-acquiring locks as needed. With concurrent streaming
tasks the behavior is as before: drop the shared iterator, all tasks stop
on the shared clock, the last one releases the buckets.

Co-Authored-By: Claude Fable 5 <[email protected]>
Instead of stopping streaming and releasing bucket locks before
persistent_processing_node_ttl_seconds passes, keep the locks alive from
inside the file iterator: BucketHolder::refresh rewrites the lock node
data with a version-guarded set, updating its mtime, and the iterator
refreshes every held lock older than half of the TTL on each iterator
step. The TTL cleanup already removes nodes with a version check, so
a refreshed lock cannot be removed by a cleanup which read a stale stat,
and the TTL now only reaps locks of genuinely dead servers - its original
purpose.

This removes the whole stop/release machinery from streamToViews: the
iterator drop, the use_count checks and the force_release_unfinished flag
of releaseFinishedBuckets. Streaming is never interrupted, and lock
refresh works during long iterations too, since it happens on every
iterator step rather than between them.

Co-Authored-By: Claude Fable 5 <[email protected]>
@kssenii kssenii changed the title Bump persistent_processing_node_ttl_seconds default and bound streamToViews by it Bump persistent_processing_node_ttl_seconds default and add bucket lock refresh Jul 14, 2026
If the lock node is gone or owned by another server when refreshing,
throw LOGICAL_ERROR instead of logging a warning, failing the current
streaming execution. The holder is marked released first, so that
release() and the destructor do not remove someone else's lock node.

Co-Authored-By: Claude Fable 5 <[email protected]>
@kssenii
kssenii force-pushed the s3queue-ttl-and-elapsed-log branch from e6776c8 to 222d4e3 Compare July 14, 2026 08:55
Scanning bucket holders on every iterator step is too often. Scan them
at most once per quarter of persistent_processing_node_ttl_seconds, so
each lock is still refreshed before 3/4 of the TTL passes.

Co-Authored-By: Claude Fable 5 <[email protected]>
files_path = f"{table_name}_data"
files_to_generate = 300

# With TTL = 2 sec a bucket lock must be refreshed once it was not

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 only exercises the "many short files" case, where the iterator reaches next() often enough to call refreshExpiringBucketLocks(). The remaining failure mode is one slow reader->pull() or a long commit spanning persistent_processing_node_ttl_seconds: in that path the refresh hook is never reached, and the per-file processing node is not refreshed at all. Please add a focused repro for one active file held past the TTL, ideally with a second node trying to acquire the same bucket, so the test matches the contract this PR is trying to enforce.

@kssenii
kssenii marked this pull request as ready for review July 20, 2026 10:21
@kssenii
kssenii requested a review from bharatnc July 20, 2026 10:22
@bharatnc bharatnc self-assigned this Jul 20, 2026
A commit can take a long time (Keeper retries with backoff), during
which no thread calls `next` on the file iterator, so bucket locks
could age past the TTL and be removed as abandoned by the cleanup.
Refresh the locks right after each commit in `streamToViews`, so that
the locks which survive into the next batch enter it with most of the
TTL budget left.

Also consolidate the refresh into a single `refreshExpiringBucketLocks`
method with a single threshold (a quarter of the TTL) and drop the scan
throttle: the scan itself is an in-memory walk over a handful of
holders, and Keeper is only touched for locks that actually aged.

Co-Authored-By: Claude Fable 5 <[email protected]>
persistent_processing_node_ttl_seconds is changeable at runtime, while
the cleanup task gating flag is fixed at metadata creation. Altering
the TTL to zero on a table with the cleanup running made it treat every
processing node and bucket lock as stale and remove live ownership
state immediately.

The opposite direction stays a known pre-existing limitation: the
cleanup task is only created at startup, so a table created with a zero
TTL does not start it when the TTL is enabled later.

Addresses: #110292 (comment)

Co-Authored-By: Claude Fable 5 <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Jul 24, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 86.20% 86.20% +0.00%
Functions 92.00% 92.00% +0.00%
Branches 78.40% 78.30% -0.10%

Changed lines: Changed C/C++ lines covered: 93/156 (59.62%) · Uncovered code

Full report · Diff report

@kssenii
kssenii added this pull request to the merge queue Jul 27, 2026
Merged via the queue into master with commit 09b584c Jul 27, 2026
174 of 178 checks passed
@kssenii
kssenii deleted the s3queue-ttl-and-elapsed-log branch July 27, 2026 09:59
@robot-clickhouse robot-clickhouse added the pr-synced-to-cloud The PR is synced to the cloud repo label Jul 27, 2026
robot-clickhouse-ci-1 added a commit that referenced this pull request Jul 27, 2026
Cherry pick #110292 to 26.3: Refresh S3Queue bucket locks so the TTL cleanup does not remove them mid-processing
robot-clickhouse added a commit that referenced this pull request Jul 27, 2026
…anup does not remove them mid-processing
robot-clickhouse-ci-1 added a commit that referenced this pull request Jul 27, 2026
Cherry pick #110292 to 26.5: Refresh S3Queue bucket locks so the TTL cleanup does not remove them mid-processing
robot-clickhouse added a commit that referenced this pull request Jul 27, 2026
…anup does not remove them mid-processing
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Jul 27, 2026
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Jul 27, 2026
clickhouse-gh Bot added a commit that referenced this pull request Jul 27, 2026
Backport #110292 to 26.7: Refresh S3Queue bucket locks so the TTL cleanup does not remove them mid-processing
kssenii added a commit that referenced this pull request Jul 29, 2026
Backport #110292 to 26.5: Refresh S3Queue bucket locks so the TTL cleanup does not remove them mid-processing
kssenii added a commit that referenced this pull request Jul 29, 2026
Backport #110292 to 26.3: Refresh S3Queue bucket locks so the TTL cleanup does not remove them mid-processing
kssenii added a commit that referenced this pull request Jul 29, 2026
Backport #110292 to 26.6: Refresh S3Queue bucket locks so the TTL cleanup does not remove them mid-processing
UnamedRus pushed a commit to UnamedRus/ClickHouse that referenced this pull request Aug 12, 2026
…he TTL cleanup does not remove them mid-processing
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-bugfix Pull request with bugfix, not backported by default pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR pr-synced-to-cloud The PR is synced to the cloud repo v26.2-must-backport v26.4-must-backport v26.5-must-backport v26.6-must-backport v26.7-must-backport

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants