Skip to content

Fixes for graceful shutdown - #2759

Merged
mkmkme merged 15 commits into
antalya-26.8from
fix/swarm-shutdown-network-error-reschedule
Nov 20, 2026
Merged

mkmkme merged 15 commits into
antalya-26.8from
fix/swarm-shutdown-network-error-reschedule

Conversation

@ianton-ru

Copy link
Copy Markdown

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 to CHANGELOG.md):

Fixes for graceful shutdown

Documentation entry for user-facing changes

Initial implementation (#1014) catch in initiator only ATTEMPT_TO_READ_AFTER_EOF error during node shutdown, NETWORK_ERROR is added. Solved #1521.
Fixed potential race in turning swarm mode on/off.

CI/CD Options

Exclude tests:

  • Fast test
  • Integration Tests
  • Stateless tests
  • Stateful tests
  • Unit tests
  • Performance tests
  • Aarch64 tests
  • All with ASAN
  • All with TSAN
  • All with MSAN
  • All with UBSAN
  • All with Coverage
  • All Regression
  • Disable CI Cache

Regression jobs to run:

  • Fast suites (mostly <1h)
  • Aggregate Functions (2h)
  • Alter (1.5h)
  • Benchmark (30m)
  • CAS (content-addressed storage; Antalya only)
  • ClickHouse Keeper (1h)
  • Iceberg (2h)
  • LDAP (1h)
  • OAuth (5m)
  • Parquet (1.5h)
  • RBAC (1.5h)
  • SSL Server (1h)
  • S3 (2h)
  • S3 Export (2h)
  • Swarms (30m)
  • Tiered Storage (2h)

ianton-ru and others added 5 commits November 3, 2026 10:53
Graceful swarm shutdown (SYSTEM STOP SWARM MODE) could still fail the
initiator query with NETWORK_ERROR when a drained worker was killed,
because the socket sometimes closes via RST instead of FIN (e.g. when
the killed process still had unread bytes queued). Only
ATTEMPT_TO_READ_AFTER_EOF was tolerated and converted to a
ConnectionLost packet for task rescheduling; NETWORK_ERROR was
rethrown and failed the whole query.

Treat NETWORK_ERROR the same as ATTEMPT_TO_READ_AFTER_EOF (still
gated on no data received yet and skip_unavailable_shards) in both
the async read path (RemoteQueryExecutorReadContext::Task::run) and
the synchronous read path (RemoteQueryExecutor::read), which
previously had no such tolerance at all.

Related: #1521
Related: #1014
Co-authored-by: Cursor <[email protected]>
Serialize a worker's swarm-mode check and next-task callback with SYSTEM STOP SWARM MODE. A worker previously could observe that the mode was enabled, then receive a task after the command returned and be killed before processing it.

Add a failpoint-backed integration test that stops a worker between its pre-admission pause and the task-admission lock, and verifies it sent no task requests.

Co-authored-by: Cursor <[email protected]>
Stopping swarm mode was blocked by the retry sleep and metadata resolution because the admission mutex stayed held after the task callback returned.

Co-authored-by: Cursor <[email protected]>
A header block has zero rows, so `rows() > 0` is data already received from the replica. The retry-wait test now lives with the object-storage swarm tests.

Co-authored-by: Cursor <[email protected]>
@ianton-ru

Copy link
Copy Markdown
Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 3, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-04T12:09:02.859303Z 5b76e85 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@ianton-ru ianton-ru added antalya port-antalya PRs to be ported to all new Antalya releases antalya-26.8 labels Nov 3, 2026
@github-actions

github-actions Bot commented Nov 3, 2026 •

Copy link
Copy Markdown

Workflow [PR], commit [3344390]

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 442b6d972a

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +1021 to +1024
{
std::lock_guard lock(swarm_mode_mutex);
swarm_mode_enabled = false;
CurrentMetrics::set(CurrentMetrics::IsSwarmModeEnabled, 0);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Cancel active queries before waiting for swarm admission

During shutdown, an object-storage worker may hold swarm_mode_mutex while callback() waits for the initiator's task response; the callback performs blocking network I/O in TCPHandler.cpp:830-834. This new lock acquisition therefore prevents shutdown() from reaching the later process_list.killAllQueries() call that would cancel and unblock that worker, delaying shutdown until the network receive timeout (300 seconds by default, potentially longer if configured). Reorder shutdown so active queries are canceled before waiting for task-admission callbacks, or avoid taking this blocking admission barrier on the shutdown path.

Useful? React with 👍 / 👎.

Comment on lines +2547 to +2554
auto swarm_mode_lock = getContext()->lockSwarmMode();
if (!getContext()->isSwarmModeEnabled())
{
LOG_DEBUG(getLogger("StorageObjectStorageSource"), "STOP SWARM MODE called, stop getting new tasks");
return nullptr;
}

auto task = callback();
task = callback();

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Allow concurrent task admissions while swarm mode is enabled

For concurrent s3Cluster/object-storage cluster queries on one worker, this mutex belongs to the server-wide shared Context, and it is held across the blocking task request/response callback. Consequently, every task admission from every query is now serialized behind a single network round trip, whereas previously only callbacks within one TCP connection were serialized by that connection's callback_mutex. This can severely reduce throughput for concurrent distributed reads; use a shared admission mechanism (for example, shared locking or an active-admission counter) that permits concurrent callbacks while still letting SYSTEM STOP SWARM MODE establish an exclusive barrier.

Useful? React with 👍 / 👎.

ianton-ru and others added 7 commits November 4, 2026 10:20
…ssion lock

The task-response read holds the admission mutex, so shutdown could wait until `receive_timeout`. The read now polls and returns when shutdown asks it to stop.

Co-authored-by: Cursor <[email protected]>
An exclusive `swarm_mode_mutex` made one slow initiator block task admission for every other query. Admissions now take a shared lock, while `stopSwarmMode`, `startSwarmMode`, and shutdown still take it exclusively.

Co-authored-by: Cursor <[email protected]>
Shutdown threw `ABORTED` while the worker waited for the next task, which failed the initiator query and left that response unread. The callback now returns before sending when shutdown has started, and releases the lock before reading a request that was already sent.

Co-authored-by: Cursor <[email protected]>
A sibling thread of the same query can hold `callback_mutex` while reading a task response. Releasing the shared admission lock first lets `stopSwarmMode` return without waiting out `receive_timeout`.

Co-authored-by: Cursor <[email protected]>
`createCopy` shares the flag, and shutdown never clears it. A later test in the same process otherwise sees admission as already interrupted.

Co-authored-by: Cursor <[email protected]>
A callback waiting on the connection mutex, and the response read, no longer keep `stopSwarmMode` behind `receive_timeout`. The shutdown interrupt and the thread-local unlock are unnecessary.

Co-authored-by: Cursor <[email protected]>
The pause is in `takeNextObject` before the next task request. The admission lock is taken later, in the read-task callback.

Co-authored-by: Cursor <[email protected]>
@ianton-ru

Copy link
Copy Markdown
Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep them coming!

Reviewed commit: 5b76e85d69

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@mkmkme

mkmkme commented Nov 12, 2026

Copy link
Copy Markdown
Collaborator

🤖 [Posted by an LLM agent on behalf of @mkmkme]

AI audit note: This review comment was generated by AI (Claude Fable 5.1).

Audit update for PR #2759 (graceful shutdown: NETWORK_ERROR reschedule + swarm admission lock):

Confirmed defects:

Medium: Task request is sent after the swarm admission lock is released
    Impact: After `SYSTEM STOP SWARM MODE` (or `stopSwarmMode` during server shutdown) returns, a worker can still send one `ReadTaskRequest` per in-flight callback and get one more file assigned. A kill after stop + drain wait can then interrupt that fresh task, which is the scenario `test_graceful_shutdown` guards against.
    Anchor: `src/Server/TCPHandler.cpp` / `setClusterFunctionReadTaskCallback` lambda
    Trigger: callback passes `isSwarmModeEnabled()` under the shared lock; the `if (query_state->query_context)` block ends and drops `admission`; `stopSwarmMode` takes the exclusive lock and returns; `sendReadTaskRequest()` then runs.
    Why defect: 2ef2bdda05e ("Fix call in both if branches") moved `sendReadTaskRequest()` out of the block that owns `admission`, so the check and the send are no longer atomic with respect to stop. The comments in `Context.h` ("holds it only across the enabled check and `sendReadTaskRequest`"), `Context.cpp` ("no request is sent after `stopSwarmMode` returns") and the gtest `StopBeforeSendDoesNotRequestTask` (`++sends` under the lock) all describe the previous order, not the shipped one. The integration test only exercises the unlocked recheck in `takeNextObject`, so it passes with the defect present.
    Fix direction (short): declare `std::shared_lock<SharedMutex> admission;` before the `if`, assign it inside, call `sendReadTaskRequest()` and then `admission.unlock()` before `receiveClusterFunctionReadTaskResponse`.
    Regression test direction (short): pause between the enabled check and the send (a failpoint placed after `lockSwarmMode` in `TCPHandler`), run `SYSTEM STOP SWARM MODE`, resume, and assert `ReadTaskRequestsSent` does not grow.

Coverage summary:

Scope reviewed: all 9 changed files; `TCPHandler` callback -> `ReadTaskIterator` -> `Context` swarm lock; stop/start/shutdown writers; `RemoteQueryExecutor` sync and async EOF/RST paths -> `processPacket` `ConnectionLost` -> `rescheduleTasksFromReplica`; failpoint, gtests, integration test.
Categories failed: admission-vs-stop ordering (above).
Categories passed: lock ordering (`callback_mutex` -> `swarm_mode_mutex` only, no inverse path; `SharedMutex` is writer-preferring as claimed); shared-lock hold time (no network I/O under it, both earlier Codex P1s are resolved); shutdown re-entrancy and destructor path; sync/async `RemoteQueryExecutor` parity (`got_data_from_replica` is set together with `replica_has_processed_data`; `Connection::receivePacket` disconnects on any exception; `finish` returns early on `finished`); exception safety; failpoint semantics; gtest global-state restoration.
Assumptions/limits: static review only, nothing built or run locally. CI failures on the PR (`test_database_backup`, `test_export_partition_iceberg`, `test_custom_s3_disk_remote_url_allow_hosts`) are container-start timeouts unrelated to this change.

Releasing the lock before the send let `stopSwarmMode` return and still see one more task request from an in-flight callback. The response read stays outside the lock.

Co-authored-by: Cursor <[email protected]>
@blau-ai blau-ai mentioned this pull request Nov 18, 2026
9 of 30 tasks
@DimensionWieldr

Copy link
Copy Markdown
Collaborator

No CI failures caused by this PR.

AI audit returned no more defects.

Bugfix; added tests are sufficient.

LGTM

@DimensionWieldr DimensionWieldr added the verified Approved for release label Nov 18, 2026
@mkmkme
mkmkme merged commit 3ad8520 into antalya-26.8 Nov 20, 2026
309 of 317 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

antalya antalya-26.8 antalya-26.8.39.20001 port-antalya PRs to be ported to all new Antalya releases verified Approved for release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants