Skip to content

Design Refactor for the Multi-threaded Lifetime of a Query - [MOD-15430] - #9426

Closed
GuyAv46 wants to merge 28 commits into
masterfrom
guyav-design_blocked_client_usage
Closed

GuyAv46 wants to merge 28 commits into
masterfrom
guyav-design_blocked_client_usage

Conversation

@GuyAv46

@GuyAv46 GuyAv46 commented May 4, 2026

Copy link
Copy Markdown
Collaborator

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

@GuyAv46
GuyAv46 requested a review from lerman25 May 4, 2026 13:33
@GuyAv46
GuyAv46 requested a review from kei-nan May 4, 2026 13:34
@jit-ci

jit-ci Bot commented May 4, 2026 •

Copy link
Copy Markdown

🛡️ Jit Security Scan Results

CRITICAL HIGH MEDIUM

✅ No security findings were detected in this PR


Security scan by Jit

@GuyAv46
GuyAv46 requested a review from JoanFM May 4, 2026 13:44

@kei-nan kei-nan left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

some parts of the md document do not render correctly.
e.g:
"3.3 Lifetime / ownership of one cycle (no cursor)"

Also take into consideration depleters that can operate on an AREQ outside the thread worker lifetime(we are planning some optimizations in that area so a worker thread would unblock the client but depleters would continue running on the AREQ object)

I did a shallow review - don't have a lot of free time
will try and review more closely in the upcoming days.

@GuyAv46
GuyAv46 requested a review from dor-forer May 4, 2026 14:22
@JoanFM

JoanFM commented May 5, 2026

Copy link
Copy Markdown
Collaborator

Would it be possible to clarify the motivation to separate BlockedRequestCtx and BlockedClientCtx? Are their lifetimes different?

Comment thread docs/design/blocked_client_refactor.md Resolved
Comment thread docs/design/blocked_client_refactor.md Resolved
Comment thread docs/design/blocked_client_refactor.md Resolved
Comment thread docs/design/blocked_client_refactor.md Resolved
Comment thread docs/design/blocked_client_refactor.md Resolved
@JoanFM

JoanFM commented May 5, 2026

Copy link
Copy Markdown
Collaborator

This design should consider the mutex we are using for ASM to make sure we properly count the requests per keyspace version, we have also a mutex there attached to the lifetime of AREQ.

Comment thread docs/design/blocked_client_refactor.md Resolved
GuyAv46 added 3 commits May 5, 2026 17:44
…cCtx wrapper for unified refcounting. Enhance clarity on role responsibilities and streamline cross-thread interactions.
…p. Migrate existing fields and unify BlockedQueryNode and BlockedCursorNode into a single BlockedNode type. Update related APIs for consistency and clarity.
@GuyAv46 GuyAv46 changed the title Design Refactor for the Multi-threaded Lifetime of a Query Design Refactor for the Multi-threaded Lifetime of a Query - [MOD-15430] May 7, 2026
GuyAv46 added 6 commits May 7, 2026 18:09
…ion to manage per-cycle spec-lock state. Introduce `runQueryCycle` wrapper for enhanced thread safety and state management during pipeline execution. Ensure cross-thread safety and maintain existing API signatures for lock operations.
- Renamed class definitions for clarity in the flowchart.
- Enhanced descriptions of BlockedClientCtx and Cursor to reflect their roles more accurately.
- Updated the ownership and lifetime details in the per-struct table for better understanding.
- Introduced CycleOutcome enum to manage outcomes in BlockedClientCtx.
- Clarified synchronization mechanisms and access patterns for various structures.
- Consolidated changes to ensure BG threads handle cursor operations, moving them away from main thread interactions.
- Improved documentation to reflect the new design and its implications on thread safety and data access.
… and synchronization

- Updated the BlockedClientCtx diagram to clarify its contents and relationships.
- Renamed SpecLockHolder to SpecLockState and adjusted its usage to enhance understanding of state transitions.
- Improved documentation on the ownership and lifecycle of various structures, including RedisSearchCtx and ChunkReplyState.
- Clarified synchronization mechanisms for shared structures, emphasizing the importance of thread safety during BCC cycles.
- Streamlined the cursor management process, ensuring that cursor operations are safely handled within the main thread.
- Enhanced the runRequestCycle wrapper to enforce state assertions and improve safety during background processing.
- Updated the per-struct table to reflect changes in ownership and access patterns, ensuring consistency across the codebase.
…ips, clarifying ownership and refcounting rationale post-refactor.
- Updated the BlockedClientCtx (BCC) and RequestSyncCtx (RSC) descriptions for clarity on ownership and reference management.
- Changed terminology from "holds" to "owns" to better reflect the ownership semantics in the context of BCC and CURS.
- Clarified the ownership model for RSC, emphasizing single ownership and the transfer mechanism between BCC and Cursor.
- Removed unnecessary references to refcounting in RSC, highlighting the single-owner-with-transfer model.
- Enhanced the explanation of cursor management and its thread safety, focusing on the implications of the single-owner-with-transfer model.
- Adjusted the table summarizing the relationships and access patterns for various components to align with the updated descriptions.
@github-actions

This comment was marked as off-topic.

@github-actions

This comment was marked as duplicate.

@github-actions

This comment was marked as duplicate.

GuyAv46 added 2 commits May 9, 2026 10:43
Document optimization opportunity enabled by the refactor: switching from per-arg `sdsnewlen` copies to `RedisModule_HoldString` refcount bumps. With the guarantee that `AREQ_Free` now runs only on main thread, we can safely use `HoldString`/`FreeString` instead of `sdsnew`/`sdsfree`, eliminating unnecessary string copies. The conversion affects a handful of readers that currently treat `req->args[i]` as `sds`/`char*` and applies
GuyAv46 added 4 commits May 10, 2026 15:12
…Ctx retirement

Step 6 now retires `ConcurrentCmdCtx`, `CoordRequestCtx`, and `_HandleRedisCommandEx` for AREQ-shaped coord paths by moving AREQ creation to main and replacing the BG handler signature. This makes the old Step 6 helper (`ConcurrentSearchBlockClientCtx`) obsolete, so Step 7 introduces a replacement `OpBlockClientCtx` for operational paths. Steps renumbered accordingly (old 7 → 8). Added follow-up notes on slimming `SearchCmdCtx` for the non-AREQ `FT.SEARCH` coord path and deferring `CurrentThread` slot cleanup since the RSC now holds a StrongRef end-to-end.
…eep Compile on worker

Clarify that AREQ_New moves to main (eliminating the setReqLock race by binding the AREQ before BlockClient), while AREQ_Compile and prepareForExecution stay on the worker thread to preserve coord-side latency. Update error handling flow: compile errors land on areq->status and route through reply_cb (same path as today's useReplyCallback mode) rather than being returned synchronously. Document argv lifetime implications and reference the §9 HoldString follow-up for refcount-based capture.
…fecycle details, unify typedefs, and remove deprecated structures
@sonarqubecloud

Copy link
Copy Markdown

@lerman25 lerman25 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Great work.
I think that we should address RETURN-STRICT+ FAIL cursors edge case, where the main thread replies (in reply/timeout+return-strict), but the background thread is still using the cursor. We are not stating what happens on another READ/DEL.

GuyAv46 and others added 2 commits June 7, 2026 16:31
…actor

- blockedClientReqCtx carries WeakRef spec_ref, not RedisModuleCtx*
- clarify req->useReplyCallback is a bool on AREQ, not in storedReplyState
- rename BCHCtx row to blockedClientHybridCtx; clarify CoordRequestCtx scope
- refresh coordinator footgun refs to function names
- resync stale file:line anchors (cursor.c, aggregate_exec.c, rmr.c, module.c)
…RICT

Align the design with current master now that the RETURN_STRICT
mechanism is complete (through MOD-16091 cursor depletion and
MOD-16267 safe-loader GIL preempt):

- Split today's embedded RequestSyncCtx: per-request RequestSyncState
  (timedOut + abort-wake, sub-AREQs included) stays embedded; the heap
  wrapper — renamed BlockedRequestCtx (BRC) — owns the request, hosts
  the RETURN_STRICT coordination verbatim, and carries the per-cycle
  blocked-client handle.
- RETURN_STRICT protocol is an explicit non-goal: re-homed, never
  redesigned. New §4.3 documents the hosted claim/GIL-preempt/wait flow
  and the planned follow-up (main thread never waits; timeout callback
  drains a generic thread-safe result list), which brc->reply prepares
  for.
- Timed-out cursors are always dropped (never parked) on master, so
  OnFree's park condition is !timedOut && !ITERDONE && !delete_mark and
  no timeout state crosses cursor cycles; the scattered disposal
  patch-ups collapse into OnFree.
- §3.6: shard hybrid multi-cursor ownership resolved with no counting —
  StartCursors hands each sub-AREQ to its sub-cursor's BRC (duplicating
  the plan-borrowed args, severing latent container pointers) and the
  container dissolves at the end of the initial cycle, relying on the
  blocked-client reply_cb→OnFree ordering.
- Coordinator sections updated to master (sticky timeoutPolicy →
  per-cycle brc field, isCursorReadReturnStrict derived, setReqLock /
  preRequestError retired by bind-on-main, KeepBlockedClient
  continuations need no plumbing, coord cursor bound on main).
- Fixed internal inconsistencies: canonical BeginCycle sequence,
  OnFree cursor sourcing, Step 2/3 field split, stale registry-snapshot
  and ConcurrentSearchBlockClientCtx references, [sir] section links.

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

sonarqubecloud Bot commented Jul 2, 2026

Copy link
Copy Markdown

@github-actions

github-actions Bot commented Sep 1, 2026

Copy link
Copy Markdown

This pull request is stale because it has been open for 60 days with no activity.

@github-actions github-actions Bot added the stale label Sep 1, 2026
@GuyAv46 GuyAv46 closed this Sep 22, 2026
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.

4 participants