1212import org .elasticsearch .action .ActionListener ;
1313import org .elasticsearch .action .ResolvedIndices ;
1414import org .elasticsearch .client .internal .Client ;
15- import org .elasticsearch .client .internal .RemoteClusterClient ;
1615import org .elasticsearch .cluster .metadata .DataStream ;
1716import org .elasticsearch .cluster .metadata .IndexMetadata ;
1817import org .elasticsearch .cluster .routing .allocation .DataTier ;
1918import org .elasticsearch .common .Strings ;
20- import org .elasticsearch .common .TriConsumer ;
2119import org .elasticsearch .common .collect .Iterators ;
2220import org .elasticsearch .common .io .stream .NamedWriteableRegistry ;
2321import org .elasticsearch .common .regex .Regex ;
2422import org .elasticsearch .common .settings .Settings ;
2523import org .elasticsearch .common .util .concurrent .CountDown ;
26- import org .elasticsearch .common .util .concurrent .ThreadContext ;
2724import org .elasticsearch .core .Nullable ;
2825import org .elasticsearch .index .Index ;
2926import org .elasticsearch .index .IndexSettings ;
3936import org .elasticsearch .search .aggregations .support .ValuesSourceRegistry ;
4037import org .elasticsearch .search .builder .PointInTimeBuilder ;
4138import org .elasticsearch .transport .RemoteClusterAware ;
42- import org .elasticsearch .transport .RemoteClusterService ;
4339import org .elasticsearch .xcontent .XContentParser ;
4440import org .elasticsearch .xcontent .XContentParserConfiguration ;
4541
5955import java .util .stream .Collectors ;
6056
6157import static org .elasticsearch .search .SearchService .DEFAULT_ALLOW_PARTIAL_SEARCH_RESULTS ;
62- import static org .elasticsearch .threadpool .ThreadPool .Names .SEARCH_COORDINATION ;
6358
6459/**
6560 * Context object used to rewrite {@link QueryBuilder} instances into simplified version.
@@ -81,9 +76,7 @@ public class QueryRewriteContext {
8176 protected final Client client ;
8277 protected final LongSupplier nowInMillis ;
8378 private final List <BiConsumer <Client , ActionListener <?>>> asyncActions = new ArrayList <>();
84- private final Map <String , List <TriConsumer <RemoteClusterClient , ThreadContext , ActionListener <?>>>> remoteAsyncActions =
85- new HashMap <>();
86- private final Map <QueryRewriteAsyncAction <?>, List <Consumer <?>>> uniqueRewriteActions = new HashMap <>();
79+ private final Map <QueryRewriteAsyncAction <?, ?>, List <Consumer <?>>> uniqueAsyncActions = new HashMap <>();
8780 protected boolean allowUnmappedFields ;
8881 protected boolean mapUnmappedFieldAsString ;
8982 protected Predicate <String > allowedFields ;
@@ -467,22 +460,11 @@ public void registerAsyncAction(BiConsumer<Client, ActionListener<?>> asyncActio
467460 asyncActions .add (asyncAction );
468461 }
469462
470- public void registerRemoteAsyncAction (
471- String clusterAlias ,
472- TriConsumer <RemoteClusterClient , ThreadContext , ActionListener <?>> asyncAction
473- ) {
474- List <TriConsumer <RemoteClusterClient , ThreadContext , ActionListener <?>>> asyncActions = remoteAsyncActions .computeIfAbsent (
475- clusterAlias ,
476- k -> new ArrayList <>()
477- );
478- asyncActions .add (asyncAction );
479- }
480-
481463 /**
482464 * Returns <code>true</code> if there are any registered async actions.
483465 */
484466 public boolean hasAsyncActions () {
485- return asyncActions .isEmpty () == false || uniqueRewriteActions . isEmpty () == false || remoteAsyncActions .isEmpty () == false ;
467+ return asyncActions .isEmpty () == false || uniqueAsyncActions .isEmpty () == false ;
486468 }
487469
488470 /**
@@ -493,10 +475,7 @@ public void executeAsyncActions(ActionListener<Void> listener) {
493475 if (hasAsyncActions () == false ) {
494476 listener .onResponse (null );
495477 } else {
496- int actionCount = asyncActions .size () + uniqueRewriteActions .size ();
497- for (var actionList : remoteAsyncActions .values ()) {
498- actionCount += actionList .size ();
499- }
478+ final int actionCount = asyncActions .size () + uniqueAsyncActions .size ();
500479
501480 CountDown countDown = new CountDown (actionCount );
502481 ActionListener <?> internalListener = new ActionListener <>() {
@@ -522,27 +501,10 @@ public void onFailure(Exception e) {
522501 action .accept (client , internalListener );
523502 }
524503
525- var copyUniqueRewriteActions = new HashMap <>(uniqueRewriteActions );
526- uniqueRewriteActions .clear ();
527- for (var entry : copyUniqueRewriteActions .keySet ()) {
528- entry .execute (client , internalListener , copyUniqueRewriteActions .get (entry ));
529- }
530-
531- var remoteAsyncActionsCopy = new HashMap <>(remoteAsyncActions );
532- remoteAsyncActions .clear ();
533- for (var entry : remoteAsyncActionsCopy .entrySet ()) {
534- String clusterAlias = entry .getKey ();
535- List <TriConsumer <RemoteClusterClient , ThreadContext , ActionListener <?>>> remoteTriConsumers = entry .getValue ();
536-
537- RemoteClusterClient remoteClient = client .getRemoteClusterClient (
538- clusterAlias ,
539- client .threadPool ().executor (SEARCH_COORDINATION ),
540- RemoteClusterService .DisconnectedStrategy .RECONNECT_UNLESS_SKIP_UNAVAILABLE
541- );
542- ThreadContext threadContext = client .threadPool ().getThreadContext ();
543- for (var action : remoteTriConsumers ) {
544- action .apply (remoteClient , threadContext , internalListener );
545- }
504+ var copyUniqueAsyncActions = new HashMap <>(uniqueAsyncActions );
505+ uniqueAsyncActions .clear ();
506+ for (var entry : copyUniqueAsyncActions .keySet ()) {
507+ entry .execute (client , internalListener , copyUniqueAsyncActions .get (entry ));
546508 }
547509 }
548510 }
@@ -733,7 +695,10 @@ public void setTrackTimeRangeFilterFrom(boolean trackTimeRangeFilterFrom) {
733695 * After the async action is executed, all consumers associated with it will be executed and receive as argument
734696 * the result of the async action.
735697 */
736- public <T > void registerUniqueAsyncAction (QueryRewriteAsyncAction <T > action , Consumer <T > consumer ) {
737- uniqueRewriteActions .computeIfAbsent (action , k -> new ArrayList <>()).add (consumer );
698+ public <T , U extends QueryRewriteAsyncAction <T , U >> void registerUniqueAsyncAction (
699+ QueryRewriteAsyncAction <T , U > action ,
700+ Consumer <T > consumer
701+ ) {
702+ uniqueAsyncActions .computeIfAbsent (action , k -> new ArrayList <>()).add (consumer );
738703 }
739704}
0 commit comments