1818import org .apache .lucene .search .TopDocs ;
1919import org .elasticsearch .ElasticsearchException ;
2020import org .elasticsearch .ExceptionsHelper ;
21+ import org .elasticsearch .TransportVersion ;
2122import org .elasticsearch .action .ActionListener ;
2223import org .elasticsearch .action .ActionRunnable ;
2324import org .elasticsearch .action .ResolvedIndices ;
@@ -537,27 +538,26 @@ protected void doClose() {
537538 *
538539 * @param <T> the type of the response
539540 * @param listener the action listener to be wrapped
540- * @param request the shard request being executed
541+ * @param version channel version of the request
542+ * @param nodeId id of the current node
543+ * @param shardId id of the shard being searched
541544 * @param threadPool with context where to write the new header
542- * @param clusterService the cluster service
543545 * @return the wrapped action listener
544546 */
545547 static <T > ActionListener <T > maybeWrapListenerForStackTrace (
546548 ActionListener <T > listener ,
547- ShardSearchRequest request ,
548- ThreadPool threadPool ,
549- ClusterService clusterService
549+ TransportVersion version ,
550+ String nodeId ,
551+ ShardId shardId ,
552+ ThreadPool threadPool
550553 ) {
551554 boolean header = true ;
552- if (request . getChannelVersion () .onOrAfter (ERROR_TRACE_IN_TRANSPORT_HEADER ) && threadPool .getThreadContext () != null ) {
555+ if (version .onOrAfter (ERROR_TRACE_IN_TRANSPORT_HEADER ) && threadPool .getThreadContext () != null ) {
553556 header = Boolean .parseBoolean (threadPool .getThreadContext ().getHeaderOrDefault ("error_trace" , "false" ));
554557 }
555558 if (header == false ) {
556559 return listener .delegateResponse ((l , e ) -> {
557- logger .debug (
558- () -> format ("[%s]%s Clearing stack trace before transport:" , clusterService .localNode ().getId (), request .shardId ()),
559- e
560- );
560+ logger .debug (() -> format ("[%s]%s: failed to execute search request" , nodeId , shardId ), e );
561561 ExceptionsHelper .unwrapCausesAndSuppressed (e , err -> {
562562 err .setStackTrace (EMPTY_STACK_TRACE_ARRAY );
563563 return false ;
@@ -569,7 +569,13 @@ static <T> ActionListener<T> maybeWrapListenerForStackTrace(
569569 }
570570
571571 public void executeDfsPhase (ShardSearchRequest request , SearchShardTask task , ActionListener <SearchPhaseResult > listener ) {
572- listener = maybeWrapListenerForStackTrace (listener , request , threadPool , clusterService );
572+ listener = maybeWrapListenerForStackTrace (
573+ listener ,
574+ request .getChannelVersion (),
575+ clusterService .localNode ().getId (),
576+ request .shardId (),
577+ threadPool
578+ );
573579 final IndexShard shard = getShard (request );
574580 rewriteAndFetchShardRequest (shard , request , listener .delegateFailure ((l , rewritten ) -> {
575581 // fork the execution in the search thread pool
@@ -613,7 +619,13 @@ public void executeQueryPhase(ShardSearchRequest request, CancellableTask task,
613619 rewriteAndFetchShardRequest (
614620 shard ,
615621 request ,
616- maybeWrapListenerForStackTrace (listener , request , threadPool , clusterService ).delegateFailure ((l , orig ) -> {
622+ maybeWrapListenerForStackTrace (
623+ listener ,
624+ request .getChannelVersion (),
625+ clusterService .localNode ().getId (),
626+ request .shardId (),
627+ threadPool
628+ ).delegateFailure ((l , orig ) -> {
617629 // check if we can shortcut the query phase entirely.
618630 if (orig .canReturnNullResponseIfMatchNoDocs ()) {
619631 assert orig .scroll () == null ;
@@ -813,7 +825,13 @@ private SearchPhaseResult executeQueryPhase(ShardSearchRequest request, Cancella
813825 public void executeRankFeaturePhase (RankFeatureShardRequest request , SearchShardTask task , ActionListener <RankFeatureResult > listener ) {
814826 final ReaderContext readerContext = findReaderContext (request .contextId (), request );
815827 final ShardSearchRequest shardSearchRequest = readerContext .getShardSearchRequest (request .getShardSearchRequest ());
816- listener = maybeWrapListenerForStackTrace (listener , shardSearchRequest , threadPool , clusterService );
828+ listener = maybeWrapListenerForStackTrace (
829+ listener ,
830+ shardSearchRequest .getChannelVersion (),
831+ clusterService .localNode ().getId (),
832+ shardSearchRequest .shardId (),
833+ threadPool
834+ );
817835 final Releasable markAsUsed = readerContext .markAsUsed (getKeepAlive (shardSearchRequest ));
818836 runAsync (getExecutor (readerContext .indexShard ()), () -> {
819837 try (SearchContext searchContext = createContext (readerContext , shardSearchRequest , task , ResultsType .RANK_FEATURE , false )) {
@@ -859,11 +877,17 @@ private QueryFetchSearchResult executeFetchPhase(ReaderContext reader, SearchCon
859877 public void executeQueryPhase (
860878 InternalScrollSearchRequest request ,
861879 SearchShardTask task ,
862- ActionListener <ScrollQuerySearchResult > listener
880+ ActionListener <ScrollQuerySearchResult > listener ,
881+ TransportVersion version
863882 ) {
864883 final LegacyReaderContext readerContext = (LegacyReaderContext ) findReaderContext (request .contextId (), request );
865- final ShardSearchRequest shardSearchRequest = readerContext .getShardSearchRequest (null );
866- listener = maybeWrapListenerForStackTrace (listener , shardSearchRequest , threadPool , clusterService );
884+ listener = maybeWrapListenerForStackTrace (
885+ listener ,
886+ version ,
887+ clusterService .localNode ().getId (),
888+ readerContext .indexShard ().shardId (),
889+ threadPool
890+ );
867891 final Releasable markAsUsed ;
868892 try {
869893 markAsUsed = readerContext .markAsUsed (getScrollKeepAlive (request .scroll ()));
@@ -873,6 +897,7 @@ public void executeQueryPhase(
873897 throw e ;
874898 }
875899 runAsync (getExecutor (readerContext .indexShard ()), () -> {
900+ final ShardSearchRequest shardSearchRequest = readerContext .getShardSearchRequest (null );
876901 try (SearchContext searchContext = createContext (readerContext , shardSearchRequest , task , ResultsType .QUERY , false );) {
877902 var opsListener = searchContext .indexShard ().getSearchOperationListener ();
878903 final long beforeQueryTime = System .nanoTime ();
@@ -904,10 +929,21 @@ public void executeQueryPhase(
904929 * It is the responsibility of the caller to ensure that the ref count is correctly decremented
905930 * when the object is no longer needed.
906931 */
907- public void executeQueryPhase (QuerySearchRequest request , SearchShardTask task , ActionListener <QuerySearchResult > listener ) {
932+ public void executeQueryPhase (
933+ QuerySearchRequest request ,
934+ SearchShardTask task ,
935+ ActionListener <QuerySearchResult > listener ,
936+ TransportVersion version
937+ ) {
908938 final ReaderContext readerContext = findReaderContext (request .contextId (), request .shardSearchRequest ());
909939 final ShardSearchRequest shardSearchRequest = readerContext .getShardSearchRequest (request .shardSearchRequest ());
910- listener = maybeWrapListenerForStackTrace (listener , shardSearchRequest , threadPool , clusterService );
940+ listener = maybeWrapListenerForStackTrace (
941+ listener ,
942+ version ,
943+ clusterService .localNode ().getId (),
944+ shardSearchRequest .shardId (),
945+ threadPool
946+ );
911947 final Releasable markAsUsed = readerContext .markAsUsed (getKeepAlive (shardSearchRequest ));
912948 rewriteAndFetchShardRequest (readerContext .indexShard (), shardSearchRequest , listener .delegateFailure ((l , rewritten ) -> {
913949 // fork the execution in the search thread pool
0 commit comments