@@ -77,8 +77,6 @@ final class CanMatchPreFilterSearchPhase {
7777 private final MinAndMax <?>[] minAndMaxes ;
7878 private int numPossibleMatches ;
7979 private final CoordinatorRewriteContextProvider coordinatorRewriteContextProvider ;
80- private final SearchResponseMetrics searchResponseMetrics ;
81- private long phaseStartTimeInNanos ;
8280
8381 private CanMatchPreFilterSearchPhase (
8482 Logger logger ,
@@ -126,7 +124,6 @@ private CanMatchPreFilterSearchPhase(
126124 shardItIndexMap .put (naturalOrder [j ], j );
127125 }
128126 this .shardItIndexMap = shardItIndexMap ;
129- this .searchResponseMetrics = searchResponseMetrics ;
130127 }
131128
132129 public static SubscribableListener <List <SearchShardIterator >> execute (
@@ -148,6 +145,23 @@ public static SubscribableListener<List<SearchShardIterator>> execute(
148145 return SubscribableListener .newSucceeded (List .of ());
149146 }
150147 final SubscribableListener <List <SearchShardIterator >> listener = new SubscribableListener <>();
148+ long phaseStartTimeInNanos = System .nanoTime ();
149+
150+ listener .addListener (new ActionListener <>() {
151+ @ Override
152+ public void onResponse (List <SearchShardIterator > shardsIts ) {
153+ // searchResponseMetrics is null for node can-match requests
154+ if (searchResponseMetrics != null ) {
155+ searchResponseMetrics .recordSearchPhaseDuration ("can_match" , System .nanoTime () - phaseStartTimeInNanos );
156+ }
157+ }
158+
159+ @ Override
160+ public void onFailure (Exception e ) {
161+ // do not record duration of failed phases
162+ }
163+ });
164+
151165 // Note that the search is failed when this task is rejected by the executor
152166 executor .execute (new AbstractRunnable () {
153167 @ Override
@@ -191,7 +205,6 @@ private static boolean assertSearchCoordinationThread() {
191205 private void runCoordinatorRewritePhase () {
192206 // TODO: the index filter (i.e, `_index:patten`) should be prefiltered on the coordinator
193207 assert assertSearchCoordinationThread ();
194- phaseStartTimeInNanos = System .nanoTime ();
195208 final List <SearchShardIterator > matchedShardLevelRequests = new ArrayList <>();
196209 for (SearchShardIterator searchShardIterator : shardsIts ) {
197210 final CanMatchNodeRequest canMatchNodeRequest = new CanMatchNodeRequest (
@@ -231,10 +244,6 @@ private void runCoordinatorRewritePhase() {
231244 checkNoMissingShards (matchedShardLevelRequests );
232245 new Round (matchedShardLevelRequests ).run ();
233246 }
234- // this could be null in the case where this phase is running in a remote cluster
235- if (searchResponseMetrics != null ) {
236- searchResponseMetrics .recordSearchPhaseDuration ("can_match" , System .nanoTime () - phaseStartTimeInNanos );
237- }
238247 }
239248
240249 private void consumeResult (boolean canMatch , ShardSearchRequest request ) {
0 commit comments