Skip to content

[MOD-18502] Move coordinator AGGREGATE FAIL reply serialization off the main thread - #11552

Merged
lerman25 merged 6 commits into
8.10from
omerl-fail-bg-coord-aggregate
Sep 28, 2026
Merged

lerman25 merged 6 commits into
8.10from
omerl-fail-bg-coord-aggregate

Conversation

@lerman25

@lerman25 lerman25 commented Sep 24, 2026 •

Copy link
Copy Markdown
Collaborator

Describe the changes in the pull request

Coordinator AGGREGATE with ON_TIMEOUT FAIL serializes replies on the main thread. Move serialization to workers through the existing direct reply path used by RETURN, preserving full-chunk collection so late errors still replace the entire reply.

Keep the blocked-client deadline active during encoding. Finalize coordinator cursors during free-data cleanup: pause successful nonterminal cursors and free timed-out, disconnected, or exhausted cursors. This includes PROFILE, initial WITHCURSOR requests, and cursor READ. Background error paths skip accounting when they observe that the request has timed out, across coordinator, shard, and standalone paths. This is best-effort suppression; it does not guarantee exactly-once accounting.

Follows #11534 and #11541. Rebase to master is deferred until #11543 merges.

Validation:

  • Debug build with assertions passed.
  • test_blocked_client_timeout: 67 passed / 9 skipped standalone; 100 passed / 13 skipped in a three-shard cluster. No failures.
  • Coverage includes timeout accounting when the worker observes the timeout flag, reply parity, late errors, cursor cleanup, and timeout before/during/after encoding. It does not establish exactly-once accounting for all interleavings.

Known limitations accepted in this PR

  • Error accounting is best effort. A worker can pass the timeout-state check before the blocked-client timeout callback runs. Both paths can then count an error; if the worker also has a timeout error, one command can increment timeout_error twice. Metrics for a background reply may remain counted even when Redis discards that reply. We accept this without adding atomic accounting ownership or additional synchronization. This accounting limitation does not imply a duplicate client reply or a server crash.
  • Standalone/shard cursor cleanup after disconnect remains unchanged. An initial cursor whose reply is discarded after disconnect can remain allocated until MAXIDLE cleanup. A disconnected cursor READ may consume an undelivered chunk; resuming a known cursor ID can therefore skip that chunk. We accept this to avoid expanding shared disconnect handling. Coordinator disconnect cleanup remains in place.
  • Background work can continue after the client timeout. A worker may serialize a reply that Redis subsequently discards. Avoiding all such work is outside this change; no additional timeout checks or synchronization are introduced solely to prevent wasted encoding.

Which additional issues this PR fixes

MOD-18502

Main objects this PR modified

  • Coordinator AGGREGATE and cursor dispatch.
  • Shared AREQ cursor finalization and coordinator request cleanup.
  • Debug encoding sync points and blocked-client timeout tests.

Mark if applicable

  • This PR introduces API changes
  • This PR introduces serialization changes

Release Notes

  • This PR requires release notes
  • This PR does not require release notes

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 24, 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-09-28T08:17:15.618399Z ed1e496 New commits
ℹ️ 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.

@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: f9ed77dd38

ℹ️ 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".

((AREQ *)req)->useReplyCallback = ctx->useReplyCallback;
AREQ *areq = (AREQ *)req;
areq->useReplyCallback = ctx->useReplyCallback;
areq->encodeReplyInBackground = ctx->timeoutPolicy == TimeoutPolicy_Fail;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Count each coordinator cursor timeout only once

When a coordinator FT.CURSOR READ using ON_TIMEOUT FAIL reaches its blocked-client deadline after the worker has taken the cursor, DistAggregateTimeoutFailCallback increments timeout_error and marks the AREQ timed out, then this new direct-encoding path lets handleSendChunkError observe the same timeout and increment the metric again. The updated tests explicitly changed these cases to expect error_count=2, so one timed-out command now overreports the coordinator error metric; suppress worker-side accounting once the timeout callback has replied, or retain single-owner accounting.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Fixed in 3a5e349. handleSendChunkError skips worker-side accounting when encodeReplyInBackground && AREQ_TimedOut(req). Restored +1 expectations for coordinator, shard, and standalone timeouts. The debug build and timeout suites pass (67 standalone, 100 cluster).

Base automatically changed from omerl-fail-bg-shard-search-agg-8.10 to 8.10 September 24, 2026 15:54

@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: 3a5e3497bc

ℹ️ 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 +1893 to +1895
if (req->encodeReplyInBackground && !AREQ_TimedOut(req) &&
!(req->stateflags & QEXEC_S_ITERDONE)) {
Cursor_Pause(cursor);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Free standalone cursors when the blocked client disconnects

When a standalone/shard ON_TIMEOUT FAIL request with WITHCURSOR disconnects before receiving the worker-buffered reply, FreeQueryNode/FreeCursorNode discards the RedisModuleCtx, so the request is never marked timed out or disconnected. This condition therefore pauses a successful nonterminal cursor even though its reply was discarded, leaking an initial cursor until idle GC and allowing a known cursor ID to resume after silently consuming an undelivered chunk. The coordinator avoids this by checking RedisModule_BlockedClientDisconnected in DistCoordReqFreePrivData; the standalone cleanup needs equivalent disconnect propagation before finalization.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

We accept retaining standalone/shard cursors until MAXIDLE after a disconnect to keep this PR small. An interrupted cursor READ may also have consumed an undelivered chunk. We will keep the existing coordinator cleanup without adding shared disconnect plumbing in this PR.

Comment on lines +1986 to +1987
r->useReplyCallback = !r->encodeReplyInBackground;
blockClientCtx.replyCallback = r->useReplyCallback ? QueryReplyCallback : NULL;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Suppress early-error accounting after a FAIL timeout

With this callback-free FAIL selection, a deadline can fire after the worker's initial timeout check but before an early failure such as index promotion or execution-plan preparation; the timeout callback counts TIMED_OUT, then AREQ_ReplyOrStoreError or CoordRequestCtx_ReplyOrStoreError directly counts the discarded worker error as well. The fresh evidence beyond the previously reported serialization case is that the new guard exists only in handleSendChunkError, while these early-error paths now bypass it because useReplyCallback is false. Gate their direct reply/accounting on the request timeout state as well.

Useful? React with 👍 / 👎.

@lerman25
lerman25 force-pushed the omerl-fail-bg-coord-aggregate branch from 3a5e349 to a2c0393 Compare September 24, 2026 21:28
@github-actions github-actions Bot added size:L and removed size:M labels Sep 24, 2026

@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: 452717284f

ℹ️ 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".

}
#endif
// The timeout callback already counted errors for discarded background replies.
const bool countError = !(req->encodeReplyInBackground && AREQ_TimedOut(req));

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Make timeout error accounting a single atomic claim

The new guard still has a TOCTOU window: for any background ON_TIMEOUT FAIL reply, the worker can read AREQ_TimedOut(req) == false here, then the blocked-client timeout callback can set the flag and increment timeout_error, after which the worker also increments it at lines 562 or 568. Thus the single-count guarantee remains timing-dependent; the fresh evidence beyond the prior comment is that the fix only added an atomic load rather than an atomic ownership claim or synchronization covering the load and increment. Use a single atomic claim shared by the timeout callback and worker-side accounting.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

We accept this limitation. The timeout-state guard provides best-effort suppression, not exactly-once accounting: a worker that passes the check before the timeout callback runs can still count an error for a discarded reply, including a second timeout count. We are okay with those metric increments and will not add atomic accounting ownership or additional synchronization in this PR. This is an accounting limitation, not a duplicate client reply or server-crash issue. The PR description now documents this and the other accepted limitations.

@lerman25
lerman25 requested a review from GuyAv46 September 25, 2026 08:54
GuyAv46
GuyAv46 previously approved these changes Sep 27, 2026
@lerman25
lerman25 added this pull request to the merge queue Sep 27, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Sep 27, 2026
GuyAv46
GuyAv46 previously approved these changes Sep 27, 2026
@lerman25
lerman25 added this pull request to the merge queue Sep 27, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Sep 27, 2026
GuyAv46
GuyAv46 previously approved these changes Sep 28, 2026

@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: fe47aac34d

ℹ️ 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".

while (!profileShouldStopCollectingReplies(req)) {
MRReply_Free(rpnet->current.root);
} while (getNextReply(rpnet) != RS_RESULT_EOF);
if (getNextReply(rpnet) == RS_RESULT_EOF) {

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 Wake profile reply waits when the FAIL deadline fires

When a coordinator FT.PROFILE AGGREGATE reaches this getNextReply call while a shard's final profile reply is still pending, a concurrent FAIL timeout only sets AREQ_TimedOut; it does not call RequestSyncCtx_WakeAbortChannel. Since MRIterator_NextWithTimeout was given no deadline and its condition variable is not signaled by changing the atomic flag, the new loop cannot re-evaluate profileShouldStopCollectingReplies until that shard eventually responds. A stalled shard can therefore retain the request and a coordinator worker long past the configured deadline, and enough such requests can exhaust the coordinator pool; wake the registered abort channel from the FAIL timeout callback.

AGENTS.md reference: AGENTS.md:L268-L269

Useful? React with 👍 / 👎.

Comment thread src/module.c
if (policy == TimeoutPolicy_Fail || policy == TimeoutPolicy_ReturnStrict) {
handlerCtx.bcCtx.reply_callback = DistAggregateReplyCallback;
bool useReplyCallback = policy == TimeoutPolicy_ReturnStrict;
handlerCtx.bcCtx.reply_callback = useReplyCallback ? DistAggregateReplyCallback : NULL;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Skip warning accounting for discarded FAIL profiles

For coordinator FT.PROFILE AGGREGATE under FAIL, selecting direct worker serialization here lets a reply discarded by the timeout callback continue through trackWarnings_Resp2 or _replyWarnings, which increments timeout_warning even though the client received a timeout error instead of that warning. The changed test explicitly permits this scheduling-dependent extra metric, so repeated timed-out profiles make INFO warning telemetry inaccurate; suppress warning accounting once a background reply observes AREQ_TimedOut, just as the error paths now do.

Useful? React with 👍 / 👎.

if (req->encodeReplyInBackground) {
// Pause after a row is encoded; signaling disarms the hook for later rows.
SyncPoint_Wait(SYNC_POINT_DURING_BACKGROUND_REPLY_ENCODE);
SyncPoint_Wait(IsCoordinator(req) ? SYNC_POINT_DURING_COORD_BACKGROUND_REPLY_ENCODE

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 Stop encoding rows after coordinator FAIL cancellation

When a coordinator FAIL deadline or client cancellation occurs after result collection has entered serialization, the new direct path continues calling serializeResult for every remaining row without checking AREQ_TimedOut; the flag is only consulted for accounting and eventual cursor cleanup. A query with a very large reply can therefore keep a coordinator worker busy and grow a reply buffer that Redis will discard, long after the caller received its timeout, allowing repeated abandoned queries to exhaust worker capacity or memory. Check cancellation between encoded rows and destroy the remaining buffered results instead of finishing the discarded reply.

AGENTS.md reference: AGENTS.md:L268-L269

Useful? React with 👍 / 👎.

@sonarqubecloud

Copy link
Copy Markdown

Quality Gate Failed Quality Gate failed

Failed conditions
3.9% Duplication on New Code (required ≤ 3%)

See analysis details on SonarQube Cloud

@lerman25
lerman25 added this pull request to the merge queue Sep 28, 2026
Merged via the queue into 8.10 with commit 58e0e44 Sep 28, 2026
25 of 26 checks passed
@lerman25
lerman25 deleted the omerl-fail-bg-coord-aggregate branch September 28, 2026 10:36
@redisearch-backport-pull-request

redisearch-backport-pull-request Bot commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor

🤖 Auto-backport summary

Target Status Backport PR
8.6-rse error Missing coordinator timeout infrastructure in src/module.c and src/coord/dist_aggregate.c: this branch has no coordinator AGGREGATE blocked-client FAIL callback or Cursors_PeekTimeoutInfo lookup, and no CoordRequestCtx timeout-policy field/setter. The incoming conflict includes entire coordinator timeout/reply callbacks and cursor dispatch from earlier changes, beyond this PR. Porting that prerequisite infrastructure requires a separate lifecycle and timeout design decision; accepting it as conflict context would import unrelated changes. Cherry-pick aborted.
8.8 error Non-mechanical timeout-policy conflict in src/coord/coord_request_ctx.c and src/module.c: this branch has no CoordRequestCtx.timeoutPolicy or CoordRequestCtx_SetTimeoutPolicy, which the source change relies on to select background FAIL encoding and disconnect cleanup. Its coordinator cursor dispatch supports the FAIL timer only and predates the source RETURN_STRICT cursor callbacks. A faithful adaptation requires deciding which earlier policy-capture and cursor-lifecycle changes to bring across; merely accepting the incoming hunks introduces unavailable APIs. Cherry-pick aborted; prerequisite selection needs maintainer review.
8.8-rse clean #11589

(PR #11552, 58e0e440b — generated by the auto-backport workflow.)

Workflow run · Retry unfinished targets with /backport <branches>.

@lerman25

Copy link
Copy Markdown
Collaborator Author

Manual follow-up to the auto-backport summary:

lerman25 added a commit that referenced this pull request Nov 25, 2026
…he main thread

Port the FAIL path from OSS 8.10 pull request #11552 to master.
lerman25 added a commit that referenced this pull request Nov 25, 2026
…he main thread

Stop collecting shard profiles as soon as a worker reply is cancelled. Port coordinator AGGREGATE reply parity checks for results, profiles, and cursor chunks.

Port the FAIL path from OSS 8.10 pull request #11552.
lerman25 added a commit that referenced this pull request Nov 25, 2026
…he main thread

Stop collecting shard profiles when a worker reply is cancelled. Exercise coordinator AGGREGATE parity for results, profiles, and cursor chunks.

Port the FAIL path from OSS 8.10 pull request #11552.
lerman25 added a commit that referenced this pull request Nov 29, 2026
…he main thread

Stop collecting shard profiles when a worker reply is cancelled. Exercise coordinator AGGREGATE parity for results, profiles, and cursor chunks.

Port the FAIL path from OSS 8.10 pull request #11552.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants