156156import java .util .function .Supplier ;
157157
158158import static org .elasticsearch .TransportVersions .ERROR_TRACE_IN_TRANSPORT_HEADER ;
159+ import static org .elasticsearch .common .Strings .format ;
159160import static org .elasticsearch .core .TimeValue .timeValueHours ;
160161import static org .elasticsearch .core .TimeValue .timeValueMillis ;
161162import static org .elasticsearch .core .TimeValue .timeValueMinutes ;
@@ -563,12 +564,18 @@ protected void doClose() {
563564 * @param <T> the type of the response
564565 * @param listener the action listener to be wrapped
565566 * @param version channel version of the request
567+ * @param nodeId id of the current node
568+ * @param shardId id of the shard being searched
569+ * @param taskId id of the task being executed
566570 * @param threadPool with context where to write the new header
567571 * @return the wrapped action listener
568572 */
569573 static <T > ActionListener <T > maybeWrapListenerForStackTrace (
570574 ActionListener <T > listener ,
571575 TransportVersion version ,
576+ String nodeId ,
577+ ShardId shardId ,
578+ long taskId ,
572579 ThreadPool threadPool
573580 ) {
574581 boolean header = true ;
@@ -577,6 +584,18 @@ static <T> ActionListener<T> maybeWrapListenerForStackTrace(
577584 }
578585 if (header == false ) {
579586 return listener .delegateResponse ((l , e ) -> {
587+ org .apache .logging .log4j .util .Supplier <String > messageSupplier = () -> format (
588+ "[%s]%s: failed to execute search request for task [%d]" ,
589+ nodeId ,
590+ shardId ,
591+ taskId
592+ );
593+ // Keep this logic aligned with that of SUPPRESSED_ERROR_LOGGER in RestResponse
594+ if (ExceptionsHelper .status (e ).getStatus () < 500 || ExceptionsHelper .isNodeOrShardUnavailableTypeException (e )) {
595+ logger .debug (messageSupplier , e );
596+ } else {
597+ logger .warn (messageSupplier , e );
598+ }
580599 ExceptionsHelper .unwrapCausesAndSuppressed (e , err -> {
581600 err .setStackTrace (EMPTY_STACK_TRACE_ARRAY );
582601 return false ;
@@ -588,7 +607,14 @@ static <T> ActionListener<T> maybeWrapListenerForStackTrace(
588607 }
589608
590609 public void executeDfsPhase (ShardSearchRequest request , SearchShardTask task , ActionListener <SearchPhaseResult > listener ) {
591- listener = maybeWrapListenerForStackTrace (listener , request .getChannelVersion (), threadPool );
610+ listener = maybeWrapListenerForStackTrace (
611+ listener ,
612+ request .getChannelVersion (),
613+ clusterService .localNode ().getId (),
614+ request .shardId (),
615+ task .getId (),
616+ threadPool
617+ );
592618 final IndexShard shard = getShard (request );
593619 rewriteAndFetchShardRequest (shard , request , listener .delegateFailure ((l , rewritten ) -> {
594620 // fork the execution in the search thread pool
@@ -632,7 +658,14 @@ public void executeQueryPhase(ShardSearchRequest request, CancellableTask task,
632658 rewriteAndFetchShardRequest (
633659 shard ,
634660 request ,
635- maybeWrapListenerForStackTrace (listener , request .getChannelVersion (), threadPool ).delegateFailure ((l , orig ) -> {
661+ maybeWrapListenerForStackTrace (
662+ listener ,
663+ request .getChannelVersion (),
664+ clusterService .localNode ().getId (),
665+ request .shardId (),
666+ task .getId (),
667+ threadPool
668+ ).delegateFailure ((l , orig ) -> {
636669 // check if we can shortcut the query phase entirely.
637670 if (orig .canReturnNullResponseIfMatchNoDocs ()) {
638671 assert orig .scroll () == null ;
@@ -830,9 +863,16 @@ private SearchPhaseResult executeQueryPhase(ShardSearchRequest request, Cancella
830863 }
831864
832865 public void executeRankFeaturePhase (RankFeatureShardRequest request , SearchShardTask task , ActionListener <RankFeatureResult > listener ) {
833- listener = maybeWrapListenerForStackTrace (listener , request .getShardSearchRequest ().getChannelVersion (), threadPool );
834866 final ReaderContext readerContext = findReaderContext (request .contextId (), request );
835867 final ShardSearchRequest shardSearchRequest = readerContext .getShardSearchRequest (request .getShardSearchRequest ());
868+ listener = maybeWrapListenerForStackTrace (
869+ listener ,
870+ shardSearchRequest .getChannelVersion (),
871+ clusterService .localNode ().getId (),
872+ shardSearchRequest .shardId (),
873+ task .getId (),
874+ threadPool
875+ );
836876 final Releasable markAsUsed = readerContext .markAsUsed (getKeepAlive (shardSearchRequest ));
837877 runAsync (getExecutor (readerContext .indexShard ()), () -> {
838878 try (SearchContext searchContext = createContext (readerContext , shardSearchRequest , task , ResultsType .RANK_FEATURE , false )) {
@@ -881,8 +921,15 @@ public void executeQueryPhase(
881921 ActionListener <ScrollQuerySearchResult > listener ,
882922 TransportVersion version
883923 ) {
884- listener = maybeWrapListenerForStackTrace (listener , version , threadPool );
885924 final LegacyReaderContext readerContext = (LegacyReaderContext ) findReaderContext (request .contextId (), request );
925+ listener = maybeWrapListenerForStackTrace (
926+ listener ,
927+ version ,
928+ clusterService .localNode ().getId (),
929+ readerContext .indexShard ().shardId (),
930+ task .getId (),
931+ threadPool
932+ );
886933 final Releasable markAsUsed ;
887934 try {
888935 markAsUsed = readerContext .markAsUsed (getScrollKeepAlive (request .scroll ()));
@@ -930,9 +977,16 @@ public void executeQueryPhase(
930977 ActionListener <QuerySearchResult > listener ,
931978 TransportVersion version
932979 ) {
933- listener = maybeWrapListenerForStackTrace (listener , version , threadPool );
934980 final ReaderContext readerContext = findReaderContext (request .contextId (), request .shardSearchRequest ());
935981 final ShardSearchRequest shardSearchRequest = readerContext .getShardSearchRequest (request .shardSearchRequest ());
982+ listener = maybeWrapListenerForStackTrace (
983+ listener ,
984+ version ,
985+ clusterService .localNode ().getId (),
986+ shardSearchRequest .shardId (),
987+ task .getId (),
988+ threadPool
989+ );
936990 final Releasable markAsUsed = readerContext .markAsUsed (getKeepAlive (shardSearchRequest ));
937991 rewriteAndFetchShardRequest (readerContext .indexShard (), shardSearchRequest , listener .delegateFailure ((l , rewritten ) -> {
938992 // fork the execution in the search thread pool
0 commit comments