6868 * The fan out and collect algorithm is traditionally used as the initial phase which can either be a query execution or collection of
6969 * distributed frequencies
7070 */
71- abstract class AbstractSearchAsyncAction <Result extends SearchPhaseResult > extends SearchPhase implements SearchPhaseContext {
71+ abstract class AbstractSearchAsyncAction <Result extends SearchPhaseResult > extends SearchPhase {
7272 private static final float DEFAULT_INDEX_BOOST = 1.0f ;
7373 private final Logger logger ;
7474 private final NamedWriteableRegistry namedWriteableRegistry ;
@@ -106,7 +106,8 @@ abstract class AbstractSearchAsyncAction<Result extends SearchPhaseResult> exten
106106 private final boolean throttleConcurrentRequests ;
107107 private final AtomicBoolean requestCancelled = new AtomicBoolean ();
108108
109- private final List <Releasable > releasables = new ArrayList <>();
109+ // protected for tests
110+ protected final List <Releasable > releasables = new ArrayList <>();
110111
111112 AbstractSearchAsyncAction (
112113 String name ,
@@ -194,7 +195,9 @@ protected void notifyListShards(
194195 );
195196 }
196197
197- @ Override
198+ /**
199+ * Registers a {@link Releasable} that will be closed when the search request finishes or fails.
200+ */
198201 public void addReleasable (Releasable releasable ) {
199202 releasables .add (releasable );
200203 }
@@ -333,8 +336,12 @@ protected abstract void executePhaseOnShard(
333336 SearchActionListener <Result > listener
334337 );
335338
336- @ Override
337- public final void executeNextPhase (SearchPhase currentPhase , Supplier <SearchPhase > nextPhaseSupplier ) {
339+ /**
340+ * Processes the phase transition from on phase to another. This method handles all errors that happen during the initial run execution
341+ * of the next phase. If there are no successful operations in the context when this method is executed the search is aborted and
342+ * a response is returned to the user indicating that all shards have failed.
343+ */
344+ protected void executeNextPhase (SearchPhase currentPhase , Supplier <SearchPhase > nextPhaseSupplier ) {
338345 /* This is the main search phase transition where we move to the next phase. If all shards
339346 * failed or if there was a failure and partial results are not allowed, then we immediately
340347 * fail. Otherwise we continue to the next phase.
@@ -470,8 +477,7 @@ protected void onShardGroupFailure(int shardIndex, SearchShardTarget shardTarget
470477 * @param shardTarget the shard target for this failure
471478 * @param e the failure reason
472479 */
473- @ Override
474- public final void onShardFailure (final int shardIndex , SearchShardTarget shardTarget , Exception e ) {
480+ void onShardFailure (final int shardIndex , SearchShardTarget shardTarget , Exception e ) {
475481 if (TransportActions .isShardNotAvailableException (e )) {
476482 // Groups shard not available exceptions under a generic exception that returns a SERVICE_UNAVAILABLE(503)
477483 // temporary error.
@@ -568,32 +574,45 @@ private void successfulShardExecution(SearchShardIterator shardsIt) {
568574 }
569575 }
570576
571- @ Override
577+ /**
578+ * Returns the total number of shards to the current search across all indices
579+ */
572580 public final int getNumShards () {
573581 return results .getNumShards ();
574582 }
575583
576- @ Override
584+ /**
585+ * Returns a logger for this context to prevent each individual phase to create their own logger.
586+ */
577587 public final Logger getLogger () {
578588 return logger ;
579589 }
580590
581- @ Override
591+ /**
592+ * Returns the currently executing search task
593+ */
582594 public final SearchTask getTask () {
583595 return task ;
584596 }
585597
586- @ Override
598+ /**
599+ * Returns the currently executing search request
600+ */
587601 public final SearchRequest getRequest () {
588602 return request ;
589603 }
590604
591- @ Override
605+ /**
606+ * Returns the targeted {@link OriginalIndices} for the provided {@code shardIndex}.
607+ */
592608 public OriginalIndices getOriginalIndices (int shardIndex ) {
593609 return shardIterators [shardIndex ].getOriginalIndices ();
594610 }
595611
596- @ Override
612+ /**
613+ * Checks if the given context id is part of the point in time of this search (if exists).
614+ * We should not release search contexts that belong to the point in time during or after searches.
615+ */
597616 public boolean isPartOfPointInTime (ShardSearchContextId contextId ) {
598617 final PointInTimeBuilder pointInTimeBuilder = request .pointInTimeBuilder ();
599618 if (pointInTimeBuilder != null ) {
@@ -630,7 +649,12 @@ boolean buildPointInTimeFromSearchResults() {
630649 return false ;
631650 }
632651
633- @ Override
652+ /**
653+ * Builds and sends the final search response back to the user.
654+ *
655+ * @param internalSearchResponse the internal search response
656+ * @param queryResults the results of the query phase
657+ */
634658 public void sendSearchResponse (SearchResponseSections internalSearchResponse , AtomicArray <SearchPhaseResult > queryResults ) {
635659 ShardSearchFailure [] failures = buildShardFailures ();
636660 Boolean allowPartialResults = request .allowPartialSearchResults ();
@@ -655,8 +679,14 @@ public void sendSearchResponse(SearchResponseSections internalSearchResponse, At
655679 }
656680 }
657681
658- @ Override
659- public final void onPhaseFailure (SearchPhase phase , String msg , Throwable cause ) {
682+ /**
683+ * This method will communicate a fatal phase failure back to the user. In contrast to a shard failure
684+ * will this method immediately fail the search request and return the failure to the issuer of the request
685+ * @param phase the phase that failed
686+ * @param msg an optional message
687+ * @param cause the cause of the phase failure
688+ */
689+ public void onPhaseFailure (SearchPhase phase , String msg , Throwable cause ) {
660690 raisePhaseFailure (new SearchPhaseExecutionException (phase .getName (), msg , cause , buildShardFailures ()));
661691 }
662692
@@ -683,6 +713,19 @@ private void raisePhaseFailure(SearchPhaseExecutionException exception) {
683713 listener .onFailure (exception );
684714 }
685715
716+ /**
717+ * Releases a search context with the given context ID on the node the given connection is connected to.
718+ * @see org.elasticsearch.search.query.QuerySearchResult#getContextId()
719+ * @see org.elasticsearch.search.fetch.FetchSearchResult#getContextId()
720+ *
721+ */
722+ void sendReleaseSearchContext (ShardSearchContextId contextId , Transport .Connection connection , OriginalIndices originalIndices ) {
723+ assert isPartOfPointInTime (contextId ) == false : "Must not release point in time context [" + contextId + "]" ;
724+ if (connection != null ) {
725+ searchTransportService .sendFreeContext (connection , contextId , originalIndices );
726+ }
727+ }
728+
686729 /**
687730 * Executed once all shard results have been received and processed
688731 * @see #onShardFailure(int, SearchShardTarget, Exception)
@@ -692,23 +735,29 @@ final void onPhaseDone() { // as a tribute to @kimchy aka. finishHim()
692735 executeNextPhase (this , this ::getNextPhase );
693736 }
694737
695- @ Override
738+ /**
739+ * Returns a connection to the node if connected otherwise and {@link org.elasticsearch.transport.ConnectTransportException} will be
740+ * thrown.
741+ */
696742 public final Transport .Connection getConnection (String clusterAlias , String nodeId ) {
697743 return nodeIdToConnection .apply (clusterAlias , nodeId );
698744 }
699745
700- @ Override
701- public final SearchTransportService getSearchTransport () {
746+ /**
747+ * Returns the {@link SearchTransportService} to send shard request to other nodes
748+ */
749+ public SearchTransportService getSearchTransport () {
702750 return searchTransportService ;
703751 }
704752
705- @ Override
706753 public final void execute (Runnable command ) {
707754 executor .execute (command );
708755 }
709756
710- @ Override
711- public final void onFailure (Exception e ) {
757+ /**
758+ * Notifies the top-level listener of the provided exception
759+ */
760+ public void onFailure (Exception e ) {
712761 listener .onFailure (e );
713762 }
714763
0 commit comments