155155import java .util .function .Supplier ;
156156
157157import static org .elasticsearch .TransportVersions .ERROR_TRACE_IN_TRANSPORT_HEADER ;
158+ import static org .elasticsearch .common .Strings .format ;
158159import static org .elasticsearch .core .TimeValue .timeValueHours ;
159160import static org .elasticsearch .core .TimeValue .timeValueMillis ;
160161import static org .elasticsearch .core .TimeValue .timeValueMinutes ;
@@ -519,12 +520,18 @@ protected void doClose() {
519520 * @param <T> the type of the response
520521 * @param listener the action listener to be wrapped
521522 * @param version channel version of the request
523+ * @param nodeId id of the current node
524+ * @param shardId id of the shard being searched
525+ * @param taskId id of the task being executed
522526 * @param threadPool with context where to write the new header
523527 * @return the wrapped action listener
524528 */
525529 static <T > ActionListener <T > maybeWrapListenerForStackTrace (
526530 ActionListener <T > listener ,
527531 TransportVersion version ,
532+ String nodeId ,
533+ ShardId shardId ,
534+ long taskId ,
528535 ThreadPool threadPool
529536 ) {
530537 boolean header = true ;
@@ -533,6 +540,18 @@ static <T> ActionListener<T> maybeWrapListenerForStackTrace(
533540 }
534541 if (header == false ) {
535542 return listener .delegateResponse ((l , e ) -> {
543+ org .apache .logging .log4j .util .Supplier <String > messageSupplier = () -> format (
544+ "[%s]%s: failed to execute search request for task [%d]" ,
545+ nodeId ,
546+ shardId ,
547+ taskId
548+ );
549+ // Keep this logic aligned with that of SUPPRESSED_ERROR_LOGGER in RestResponse
550+ if (ExceptionsHelper .status (e ).getStatus () < 500 || ExceptionsHelper .isNodeOrShardUnavailableTypeException (e )) {
551+ logger .debug (messageSupplier , e );
552+ } else {
553+ logger .warn (messageSupplier , e );
554+ }
536555 ExceptionsHelper .unwrapCausesAndSuppressed (e , err -> {
537556 err .setStackTrace (EMPTY_STACK_TRACE_ARRAY );
538557 return false ;
@@ -544,7 +563,14 @@ static <T> ActionListener<T> maybeWrapListenerForStackTrace(
544563 }
545564
546565 public void executeDfsPhase (ShardSearchRequest request , SearchShardTask task , ActionListener <SearchPhaseResult > listener ) {
547- listener = maybeWrapListenerForStackTrace (listener , request .getChannelVersion (), threadPool );
566+ listener = maybeWrapListenerForStackTrace (
567+ listener ,
568+ request .getChannelVersion (),
569+ clusterService .localNode ().getId (),
570+ request .shardId (),
571+ task .getId (),
572+ threadPool
573+ );
548574 final IndexShard shard = getShard (request );
549575 rewriteAndFetchShardRequest (shard , request , listener .delegateFailure ((l , rewritten ) -> {
550576 // fork the execution in the search thread pool
@@ -582,7 +608,14 @@ private void loadOrExecuteQueryPhase(final ShardSearchRequest request, final Sea
582608 }
583609
584610 public void executeQueryPhase (ShardSearchRequest request , SearchShardTask task , ActionListener <SearchPhaseResult > listener ) {
585- ActionListener <SearchPhaseResult > finalListener = maybeWrapListenerForStackTrace (listener , request .getChannelVersion (), threadPool );
611+ ActionListener <SearchPhaseResult > finalListener = maybeWrapListenerForStackTrace (
612+ listener ,
613+ request .getChannelVersion (),
614+ clusterService .localNode ().getId (),
615+ request .shardId (),
616+ task .getId (),
617+ threadPool
618+ );
586619 assert request .canReturnNullResponseIfMatchNoDocs () == false || request .numberOfShards () > 1
587620 : "empty responses require more than one shard" ;
588621 final IndexShard shard = getShard (request );
@@ -775,9 +808,16 @@ private SearchPhaseResult executeQueryPhase(ShardSearchRequest request, SearchSh
775808 }
776809
777810 public void executeRankFeaturePhase (RankFeatureShardRequest request , SearchShardTask task , ActionListener <RankFeatureResult > listener ) {
778- listener = maybeWrapListenerForStackTrace (listener , request .getShardSearchRequest ().getChannelVersion (), threadPool );
779811 final ReaderContext readerContext = findReaderContext (request .contextId (), request );
780812 final ShardSearchRequest shardSearchRequest = readerContext .getShardSearchRequest (request .getShardSearchRequest ());
813+ listener = maybeWrapListenerForStackTrace (
814+ listener ,
815+ shardSearchRequest .getChannelVersion (),
816+ clusterService .localNode ().getId (),
817+ shardSearchRequest .shardId (),
818+ task .getId (),
819+ threadPool
820+ );
781821 final Releasable markAsUsed = readerContext .markAsUsed (getKeepAlive (shardSearchRequest ));
782822 runAsync (getExecutor (readerContext .indexShard ()), () -> {
783823 try (SearchContext searchContext = createContext (readerContext , shardSearchRequest , task , ResultsType .RANK_FEATURE , false )) {
@@ -822,8 +862,15 @@ public void executeQueryPhase(
822862 ActionListener <ScrollQuerySearchResult > listener ,
823863 TransportVersion version
824864 ) {
825- listener = maybeWrapListenerForStackTrace (listener , version , threadPool );
826865 final LegacyReaderContext readerContext = (LegacyReaderContext ) findReaderContext (request .contextId (), request );
866+ listener = maybeWrapListenerForStackTrace (
867+ listener ,
868+ version ,
869+ clusterService .localNode ().getId (),
870+ readerContext .indexShard ().shardId (),
871+ task .getId (),
872+ threadPool
873+ );
827874 final Releasable markAsUsed ;
828875 try {
829876 markAsUsed = readerContext .markAsUsed (getScrollKeepAlive (request .scroll ()));
@@ -864,9 +911,16 @@ public void executeQueryPhase(
864911 ActionListener <QuerySearchResult > listener ,
865912 TransportVersion version
866913 ) {
867- listener = maybeWrapListenerForStackTrace (listener , version , threadPool );
868914 final ReaderContext readerContext = findReaderContext (request .contextId (), request .shardSearchRequest ());
869915 final ShardSearchRequest shardSearchRequest = readerContext .getShardSearchRequest (request .shardSearchRequest ());
916+ listener = maybeWrapListenerForStackTrace (
917+ listener ,
918+ version ,
919+ clusterService .localNode ().getId (),
920+ shardSearchRequest .shardId (),
921+ task .getId (),
922+ threadPool
923+ );
870924 final Releasable markAsUsed = readerContext .markAsUsed (getKeepAlive (shardSearchRequest ));
871925 rewriteAndFetchShardRequest (readerContext .indexShard (), shardSearchRequest , listener .delegateFailure ((l , rewritten ) -> {
872926 // fork the execution in the search thread pool
0 commit comments