Repository navigation
[MOD-13954] Shard Level FT.SEARCH/AGGREGATE - new mechanism for fail - #8335
Conversation
Automated performance analysis summaryThis comment was automatically generated given there is performance data available. In summary:
You can check a comparison in detail via the grafana link ## DetailsTests with No Significant Changes (3 tests)Tests with No Significant Changes
|
Automated performance analysis summaryThis comment was automatically generated given there is performance data available. In summary:
You can check a comparison in detail via the grafana link Performance Improvements - Comparison between master and omerl-search-shard-new-timeout-fail.Time Period from a month ago. (environment used: oss-standalone)
Performance Regressions and Issues - Comparison between master and omerl-search-shard-new-timeout-fail.Time Period from a month ago. (environment used: oss-standalone)
Tests with No Significant Changes (32 tests)Tests with No Significant Changes
|
6deebad to
07a0aa5
Compare
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## master #8335 +/- ##
========================================
Coverage 82.48% 82.49%
========================================
Files 415 415
Lines 59254 59361 +107
Branches 17310 17313 +3
========================================
+ Hits 48877 48968 +91
- Misses 10196 10212 +16
Partials 181 181
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
e3e60cb to
601952a
Compare
| #define AREQ_RP(req) AREQ_QueryProcessingCtx(req)->endProc | ||
|
|
||
| #ifdef __cplusplus | ||
| #undef _Atomic |
There was a problem hiding this comment.
remove (or undef RS_Atomic)
| RS_Atomic(bool) timedOut; | ||
| // Reply ownership state, coordinates reply between main and background thread | ||
| // Uses ReplyState enum: NOT_REPLIED -> REPLYING -> REPLIED transitions | ||
| RS_Atomic(int) replyState; |
There was a problem hiding this comment.
Consider using atomic uint8_t
| bool skipTimeoutChecks; | ||
| // Reference count for shared ownership between QueryNode (timeout callback) and background thread. | ||
| // Initialized to 1. Free when reaches 0. | ||
| uint32_t refcount; |
There was a problem hiding this comment.
We don't expect too many referees, right?
Consier using a u8
| return !HasSortBy(req) && !HasGroupBy(req) && !IsCount(req); | ||
| } | ||
|
|
||
| static bool shouldCheckInPipelineTimeout(AREQ *req) { |
There was a problem hiding this comment.
Let's mark this function as only safe for the main thread, or avoid using RunInThread() (and check if the request flag QEXEC_F_RUN_IN_BACKGROUND is on instead)
There was a problem hiding this comment.
Added a comment since QEXEC_F_RUN_IN_BACKGROUND is net set at that point
bc0c119 to
c2c7cef
Compare
| // Reply state for coordinating replies between main thread (timeout callback) and background thread | ||
| // Transitions: NOT_REPLIED -> REPLYING -> REPLIED | ||
| typedef enum { | ||
| typedef enum : uint8_t { |
| } ReplyState; | ||
| typedef struct AREQ { |
d9ba57c to
9cdb253
Compare
|
|
❌ The last analysis has failed. |
| // Background thread owns reply - wait for it to finish if still in progress | ||
| while (atomic_load_explicit(&req->replyState, memory_order_acquire) == ReplyState_Replying) { | ||
| // Busy wait until background thread transitions to REPLIED | ||
| } |
There was a problem hiding this comment.
Busy spin on main thread without yield
Medium Severity
The QueryTimeoutFailCallback runs on the main Redis thread and contains a tight busy-wait loop (while with atomic_load) when the background thread is in the ReplyState_Replying state. This spin loop with no yield, sleep, or pause instruction blocks the main thread and wastes CPU cycles. Although the replying duration is typically short, any delay in the background thread (e.g., slow I/O during reply serialization) would stall the entire Redis event loop.
| #define RS_Atomic(T) std::atomic<T> | ||
| extern "C" { | ||
| #else | ||
| #define RS_Atomic(T) _Atomic(T) |
There was a problem hiding this comment.
RS_Atomic macro not undefined in C mode
Low Severity
The RS_Atomic(T) macro is defined in both C (_Atomic(T)) and C++ (std::atomic<T>) paths, but the #undef RS_Atomic at the end of the header is only inside the #ifdef __cplusplus block. In C mode, the macro leaks to all translation units that include aggregate.h, polluting the global macro namespace. A reviewer also flagged this issue.
Additional Locations (1)
|
|
||
| #ifdef __cplusplus | ||
| #undef RS_Atomic | ||
|
|
There was a problem hiding this comment.
RS_Atomic macro not undefined in C path
Low Severity
The RS_Atomic macro is defined for both C (_Atomic(T)) and C++ (std::atomic<T>) paths, but #undef RS_Atomic only appears inside the #ifdef __cplusplus block. In C compilation units, the macro leaks out of this header, potentially polluting the macro namespace. A reviewer already flagged this ("remove (or undef RS_Atomic)").





Description
This PR implements proper timeout handling for shard-level queries when running in "Run in Threads" mode with TIMEOUT_POLICY FAIL. Previously, when a query timed out on a shard, the timeout was only detected via clock checks during query execution. This PR adds block-client timeout mechanism that allows the main thread to signal timeouts to background worker threads, ensuring consistent timeout behavior across coordinator and shard queries.
Timeout Coordination
Introduce atomic coordination between the main thread (timeout callback) and background worker thread:
New atomic flags in AREQ:
timedOut: Signals that a timeout occurred (set by timeout callback, read by worker)replyState: 3-state enum coordinating reply ownership between threads (prevents double replies)skipTimeoutChecks: Disables clock-based timeout checks when using callback-based timeoutsTimeout callback (QueryTimeoutFailCallback):
timedOutflag to signal the worker threadreplyStateto claim reply ownershipWorker thread coordination:
timedOutbefore starting pipeline executionreplyStateto claim reply ownership before replyingSkip clock-based timeout checks:
skipTimeoutChecksAREQ Reply State and Reference Counting
Three-State Reply Coordination:
Replaced the original boolean
replyingfield with a 3-state atomic enum for clearer semantics:This fixes an ambiguity where
replying=falsecould mean either "not started" or "finished".Added helper functions for better readability:
AREQ_TryClaimReply()- Atomically tries to claim reply ownership (CAS from NotReplied → Replying)AREQ_MarkReplied()- Marks reply as complete (sets state to Replied)AREQ_TryReplyWithError()- Combines claim + reply + mark in one callAREQ Reference Counting:
Added reference counting to AREQ to handle shared ownership between
blockedClientReqCtx(background thread) andBlockedQueryNode(timeout callback):uint32_t refcountfield to AREQ struct (initialized to 1)AREQ_IncrRef()- Increment reference countAREQ_DecrRef()- Decrement reference count, free when reaches 0AREQ_Free()static/internal - only called byAREQ_DecrRefAREQ_Freecalls toAREQ_DecrRefReference counting flow:
refcount=1(owned by blockedClientReqCtx)BlockQueryClientWithTimeoutcallsAREQ_IncrRef→refcount=2AREQ_DecrRef→refcount=1FreeQueryNodecallsAREQ_DecrRef→refcount=0→ AREQ freedBlockedQueryNode Cleanup Callback:
Added
freePrivDatacallback toBlockedQueryNodeto decouple it from assumingprivdatais an AREQ:BlockedQueryNode_FreePrivDatacallback typedeffreePrivDatafield toBlockedQueryNodestructBlockedQueries_AddQuery()to accept the callbackThis includes all the changes we made. Would you like any modifications to this description?
Mark if applicable
Release Notes
If a release note is required (bug fix / new feature / enhancement), describe the user impact of this PR in the title.
Note
High Risk
Touches core query execution, timeout behavior, and cross-thread reply/lifetime coordination using new atomics and refcounting; bugs here could cause double replies, hangs, or memory safety issues under load.
Overview
Shard-level
FT.SEARCH/FT.AGGREGATErunning withWORKERS > 0andON_TIMEOUT FAILnow uses a Redis blocked-client timeout callback (main thread) to deterministically fail timed-out queries, instead of relying solely on in-query clock polling.To prevent double replies and use-after-free across main/background threads, the PR adds atomic reply ownership (
ReplyState), an atomictimedOutsignal, and refcountedAREQlifetime management; it also propagates a newskipTimeoutChecksflag through pipelines/iterators/trie/suffix expansion to disable clock-based timeout checks when the callback mechanism is active. Tests and debug tooling are updated to enforce/cover the new timeout-policy constraints and shard timeout behavior.Written by Cursor Bugbot for commit 9cdb253. This will update automatically on new commits. Configure here.