158158import java .util .function .Supplier ;
159159
160160import static org .elasticsearch .TransportVersions .ERROR_TRACE_IN_TRANSPORT_HEADER ;
161+ import static org .elasticsearch .common .Strings .format ;
161162import static org .elasticsearch .core .TimeValue .timeValueHours ;
162163import static org .elasticsearch .core .TimeValue .timeValueMillis ;
163164import static org .elasticsearch .core .TimeValue .timeValueMinutes ;
@@ -529,12 +530,18 @@ protected void doClose() {
529530 * @param <T> the type of the response
530531 * @param listener the action listener to be wrapped
531532 * @param version channel version of the request
533+ * @param nodeId id of the current node
534+ * @param shardId id of the shard being searched
535+ * @param taskId id of the task being executed
532536 * @param threadPool with context where to write the new header
533537 * @return the wrapped action listener
534538 */
535539 static <T > ActionListener <T > maybeWrapListenerForStackTrace (
536540 ActionListener <T > listener ,
537541 TransportVersion version ,
542+ String nodeId ,
543+ ShardId shardId ,
544+ long taskId ,
538545 ThreadPool threadPool
539546 ) {
540547 boolean header = true ;
@@ -543,6 +550,18 @@ static <T> ActionListener<T> maybeWrapListenerForStackTrace(
543550 }
544551 if (header == false ) {
545552 return listener .delegateResponse ((l , e ) -> {
553+ org .apache .logging .log4j .util .Supplier <String > messageSupplier = () -> format (
554+ "[%s]%s: failed to execute search request for task [%d]" ,
555+ nodeId ,
556+ shardId ,
557+ taskId
558+ );
559+ // Keep this logic aligned with that of SUPPRESSED_ERROR_LOGGER in RestResponse
560+ if (ExceptionsHelper .status (e ).getStatus () < 500 || ExceptionsHelper .isNodeOrShardUnavailableTypeException (e )) {
561+ logger .debug (messageSupplier , e );
562+ } else {
563+ logger .warn (messageSupplier , e );
564+ }
546565 ExceptionsHelper .unwrapCausesAndSuppressed (e , err -> {
547566 err .setStackTrace (EMPTY_STACK_TRACE_ARRAY );
548567 return false ;
@@ -554,7 +573,14 @@ static <T> ActionListener<T> maybeWrapListenerForStackTrace(
554573 }
555574
556575 public void executeDfsPhase (ShardSearchRequest request , SearchShardTask task , ActionListener <SearchPhaseResult > listener ) {
557- listener = maybeWrapListenerForStackTrace (listener , request .getChannelVersion (), threadPool );
576+ listener = maybeWrapListenerForStackTrace (
577+ listener ,
578+ request .getChannelVersion (),
579+ clusterService .localNode ().getId (),
580+ request .shardId (),
581+ task .getId (),
582+ threadPool
583+ );
558584 final IndexShard shard = getShard (request );
559585 rewriteAndFetchShardRequest (shard , request , listener .delegateFailure ((l , rewritten ) -> {
560586 // fork the execution in the search thread pool
@@ -592,7 +618,14 @@ private void loadOrExecuteQueryPhase(final ShardSearchRequest request, final Sea
592618 }
593619
594620 public void executeQueryPhase (ShardSearchRequest request , SearchShardTask task , ActionListener <SearchPhaseResult > listener ) {
595- ActionListener <SearchPhaseResult > finalListener = maybeWrapListenerForStackTrace (listener , request .getChannelVersion (), threadPool );
621+ ActionListener <SearchPhaseResult > finalListener = maybeWrapListenerForStackTrace (
622+ listener ,
623+ request .getChannelVersion (),
624+ clusterService .localNode ().getId (),
625+ request .shardId (),
626+ task .getId (),
627+ threadPool
628+ );
596629 assert request .canReturnNullResponseIfMatchNoDocs () == false || request .numberOfShards () > 1
597630 : "empty responses require more than one shard" ;
598631 final IndexShard shard = getShard (request );
@@ -785,9 +818,16 @@ private SearchPhaseResult executeQueryPhase(ShardSearchRequest request, SearchSh
785818 }
786819
787820 public void executeRankFeaturePhase (RankFeatureShardRequest request , SearchShardTask task , ActionListener <RankFeatureResult > listener ) {
788- listener = maybeWrapListenerForStackTrace (listener , request .getShardSearchRequest ().getChannelVersion (), threadPool );
789821 final ReaderContext readerContext = findReaderContext (request .contextId (), request );
790822 final ShardSearchRequest shardSearchRequest = readerContext .getShardSearchRequest (request .getShardSearchRequest ());
823+ listener = maybeWrapListenerForStackTrace (
824+ listener ,
825+ shardSearchRequest .getChannelVersion (),
826+ clusterService .localNode ().getId (),
827+ shardSearchRequest .shardId (),
828+ task .getId (),
829+ threadPool
830+ );
791831 final Releasable markAsUsed = readerContext .markAsUsed (getKeepAlive (shardSearchRequest ));
792832 runAsync (getExecutor (readerContext .indexShard ()), () -> {
793833 try (SearchContext searchContext = createContext (readerContext , shardSearchRequest , task , ResultsType .RANK_FEATURE , false )) {
@@ -832,8 +872,15 @@ public void executeQueryPhase(
832872 ActionListener <ScrollQuerySearchResult > listener ,
833873 TransportVersion version
834874 ) {
835- listener = maybeWrapListenerForStackTrace (listener , version , threadPool );
836875 final LegacyReaderContext readerContext = (LegacyReaderContext ) findReaderContext (request .contextId (), request );
876+ listener = maybeWrapListenerForStackTrace (
877+ listener ,
878+ version ,
879+ clusterService .localNode ().getId (),
880+ readerContext .indexShard ().shardId (),
881+ task .getId (),
882+ threadPool
883+ );
837884 final Releasable markAsUsed ;
838885 try {
839886 markAsUsed = readerContext .markAsUsed (getScrollKeepAlive (request .scroll ()));
@@ -874,9 +921,16 @@ public void executeQueryPhase(
874921 ActionListener <QuerySearchResult > listener ,
875922 TransportVersion version
876923 ) {
877- listener = maybeWrapListenerForStackTrace (listener , version , threadPool );
878924 final ReaderContext readerContext = findReaderContext (request .contextId (), request .shardSearchRequest ());
879925 final ShardSearchRequest shardSearchRequest = readerContext .getShardSearchRequest (request .shardSearchRequest ());
926+ listener = maybeWrapListenerForStackTrace (
927+ listener ,
928+ version ,
929+ clusterService .localNode ().getId (),
930+ shardSearchRequest .shardId (),
931+ task .getId (),
932+ threadPool
933+ );
880934 final Releasable markAsUsed = readerContext .markAsUsed (getKeepAlive (shardSearchRequest ));
881935 rewriteAndFetchShardRequest (readerContext .indexShard (), shardSearchRequest , listener .delegateFailure ((l , rewritten ) -> {
882936 // fork the execution in the search thread pool
0 commit comments