Fix FlightOutboundHandler clearing caller's ThreadContext - #21167
Conversation
Signed-off-by: Hailong Cui <ihailong@amazon.com>
PR Reviewer Guide 🔍(Review updated until commit b0dcda6)Here are some key observations to aid the review process:
|
PR Code Suggestions ✨Latest suggestions up to b0dcda6
Previous suggestionsSuggestions up to commit cb38601
|
|
@rishabhmaurya would you mind to help to review this fix? |
|
❌ Gradle check result for cb38601: FAILURE Please examine the workflow log, locate, and copy-paste the failure(s) below, then iterate to green. Is the failure a flaky test unrelated to your change? |
Signed-off-by: Hailong Cui <ihailong@amazon.com>
|
Persistent review updated to latest commit b0dcda6 |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #21167 +/- ##
============================================
+ Coverage 73.10% 73.23% +0.12%
- Complexity 73213 73255 +42
============================================
Files 5968 5968
Lines 334539 334539
Branches 48174 48170 -4
============================================
+ Hits 244572 244997 +425
+ Misses 70421 69943 -478
- Partials 19546 19599 +53 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
Breaking change brought in by PR https://github.com/opensearch-project/OpenSearch/pull/21039/changes#diff-e13f61d0a17c4541e43c1ade6f9b452696340e76d728970322fa3b85dd9b5aaa, Not related to my change. |
|
logically it makes sense. I will check the code in details later in the day. |
@rishabhmaurya thanks. My case is we are building streaming experience for chat and sending back tool call result to user in the middle of whole chat flow, so the flow is tool A called and send back toolA's result and continue reasoning and call other tools until we got a final answer. To achieve sending tool result back we leverage FlightOutBoundHandler and we still want to keep the thread context as it contains information which need to tool call. |
|
@Hailong-am thanks for the explanation, makes a lot of sense. Usually in search case, the thread executes an action and rest of the logic is handled by action listener once response is received. I'm taking a look now |
|
@Hailong-am are you restoring the preserved context once response is received so that response handler has the context? From the initial looks of it, it doesn't seems like that's the case. |
This comment was marked as duplicate.
This comment was marked as duplicate.
|
@rishabhmaurya Yes, the context is restored on the executor thread before the response is processed. Here's the flow:
This matters because The reason we need this explicitly is that Old Flow (
|
|
@Hailong-am LGTM, thanks for fixing it. One minor comment on the test. |
1842b84
into
opensearch-project:main
…-project#21167) * fix threadcontext been clear when using streaming for transport --------- Signed-off-by: Hailong Cui <ihailong@amazon.com>
…-project#21167) * fix threadcontext been clear when using streaming for transport --------- Signed-off-by: Hailong Cui <ihailong@amazon.com> Signed-off-by: Aparajita Pandey <aparajita31pandey@gmail.com>
…-project#21167) * fix threadcontext been clear when using streaming for transport --------- Signed-off-by: Hailong Cui <ihailong@amazon.com>
…-project#21167) * fix threadcontext been clear when using streaming for transport --------- Signed-off-by: Hailong Cui <ihailong@amazon.com>
Description
FlightOutboundHandlerusesstashContext()to capture the thread context before dispatching work to a plainExecutorService. However,stashContext()clears the calling thread's context as a side effect. Since the context is only restored on the executor thread, the caller loses itsThreadContext(security headers, request metadata, etc.) after callingsendResponseBatch(),completeStream(), orsendErrorResponse().This replaces the manual
stashContext()/restore()pattern withThreadContext.preserveContext(), which is the idiomatic OpenSearch mechanism for cross-thread context propagation. It:newStoredContext(false)without clearing the callerChanges
FlightOutboundHandler.java:stashContext()withpreserveContext()wrapping the executor lambdas insendResponseBatch(),completeStream(), andsendErrorResponse()StoredContextfield fromBatchTaskrecordstoredContext.restore()calls fromprocessBatchTask(),processCompleteTask(),processErrorTask()FlightOutboundHandlerTests.java(new):ThreadContextis preserved after each method callpreserveContext()FlightOutboundHandlerContextPropagationTests.java(new):FlightTransportandStreamTransportServiceFlightTransportChannelTests.java:BatchTaskconstructor call to match new signature (removedstoredContextparameter)Related Issues
Resolves #21166
#19403
Check List
--signoffBy submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
For more information on following Developer Certification of Origin and signing off your commits, please check here.