@@ -59,8 +59,8 @@ protected void doExecute(Task task, FieldCapabilitiesRequest request, final Acti
59
59
// retrieve the initial timestamp in case the action is a cross cluster search
60
60
long nowInMillis = request .nowInMillis () == null ? System .currentTimeMillis () : request .nowInMillis ();
61
61
final ClusterState clusterState = clusterService .state ();
62
- final Map <String , OriginalIndices > remoteClusterIndices = remoteClusterService . groupIndices ( request . indicesOptions (),
63
- request .indices ());
62
+ final Map <String , OriginalIndices > remoteClusterIndices =
63
+ remoteClusterService . groupIndices ( request . indicesOptions (), request .indices ());
64
64
final OriginalIndices localIndices = remoteClusterIndices .remove (RemoteClusterAware .LOCAL_CLUSTER_GROUP_KEY );
65
65
final String [] concreteIndices ;
66
66
if (localIndices == null ) {
@@ -70,62 +70,81 @@ protected void doExecute(Task task, FieldCapabilitiesRequest request, final Acti
70
70
concreteIndices = indexNameExpressionResolver .concreteIndexNames (clusterState , localIndices );
71
71
}
72
72
final int totalNumRequest = concreteIndices .length + remoteClusterIndices .size ();
73
- final CountDown completionCounter = new CountDown (totalNumRequest );
74
- final List <FieldCapabilitiesIndexResponse > indexResponses = Collections .synchronizedList (new ArrayList <>());
75
- final Runnable onResponse = () -> {
76
- if (completionCounter .countDown ()) {
77
- if (request .isMergeResults ()) {
78
- listener .onResponse (merge (indexResponses , request .includeUnmapped ()));
79
- } else {
80
- listener .onResponse (new FieldCapabilitiesResponse (indexResponses ));
81
- }
82
- }
83
- };
84
73
if (totalNumRequest == 0 ) {
85
74
listener .onResponse (new FieldCapabilitiesResponse (new String [0 ], Collections .emptyMap ()));
86
- } else {
87
- ActionListener <FieldCapabilitiesIndexResponse > innerListener = new ActionListener <FieldCapabilitiesIndexResponse >() {
88
- @ Override
89
- public void onResponse (FieldCapabilitiesIndexResponse result ) {
90
- if (result .canMatch ()) {
91
- indexResponses .add (result );
75
+ return ;
76
+ }
77
+
78
+ final CountDown completionCounter = new CountDown (totalNumRequest );
79
+ final List <FieldCapabilitiesIndexResponse > indexResponses = Collections .synchronizedList (new ArrayList <>());
80
+ final ActionListener <List <FieldCapabilitiesIndexResponse >> countDownListener = new ActionListener <>() {
81
+ @ Override
82
+ public void onResponse (List <FieldCapabilitiesIndexResponse > results ) {
83
+ for (FieldCapabilitiesIndexResponse res : results ) {
84
+ if (res .canMatch ()) {
85
+ indexResponses .add (res );
92
86
}
93
- onResponse .run ();
94
87
}
88
+ countDown ();
89
+ }
95
90
96
- @ Override
97
- public void onFailure (Exception e ) {
98
- // TODO we should somehow inform the user that we failed
99
- onResponse .run ();
100
- }
101
- };
102
- for (String index : concreteIndices ) {
103
- client .executeLocally (TransportFieldCapabilitiesIndexAction .TYPE , new FieldCapabilitiesIndexRequest (request .fields (),
104
- index , localIndices , request .indexFilter (), nowInMillis , request .runtimeFields ()), innerListener );
91
+ @ Override
92
+ public void onFailure (Exception e ) {
93
+ // TODO we should somehow inform the user that we failed
94
+ countDown ();
105
95
}
106
96
107
- // this is the cross cluster part of this API - we force the other cluster to not merge the results but instead
108
- // send us back all individual index results.
109
- for (Map .Entry <String , OriginalIndices > remoteIndices : remoteClusterIndices .entrySet ()) {
110
- String clusterAlias = remoteIndices .getKey ();
111
- OriginalIndices originalIndices = remoteIndices .getValue ();
112
- Client remoteClusterClient = remoteClusterService .getRemoteClusterClient (threadPool , clusterAlias );
113
- FieldCapabilitiesRequest remoteRequest = new FieldCapabilitiesRequest ();
114
- remoteRequest .setMergeResults (false ); // we need to merge on this node
115
- remoteRequest .indicesOptions (originalIndices .indicesOptions ());
116
- remoteRequest .indices (originalIndices .indices ());
117
- remoteRequest .fields (request .fields ());
118
- remoteRequest .runtimeFields (request .runtimeFields ());
119
- remoteRequest .indexFilter (request .indexFilter ());
120
- remoteRequest .nowInMillis (nowInMillis );
121
- remoteClusterClient .fieldCaps (remoteRequest , ActionListener .wrap (response -> {
122
- for (FieldCapabilitiesIndexResponse res : response .getIndexResponses ()) {
123
- indexResponses .add (new FieldCapabilitiesIndexResponse (RemoteClusterAware .
124
- buildRemoteIndexName (clusterAlias , res .getIndexName ()), res .get (), res .canMatch ()));
97
+ private void countDown () {
98
+ if (completionCounter .countDown ()) {
99
+ if (request .isMergeResults ()) {
100
+ listener .onResponse (merge (indexResponses , request .includeUnmapped ()));
101
+ } else {
102
+ listener .onResponse (new FieldCapabilitiesResponse (indexResponses ));
125
103
}
126
- onResponse .run ();
127
- }, failure -> onResponse .run ()));
104
+ }
128
105
}
106
+ };
107
+
108
+ for (String index : concreteIndices ) {
109
+ client .executeLocally (TransportFieldCapabilitiesIndexAction .TYPE ,
110
+ new FieldCapabilitiesIndexRequest (
111
+ request .fields (),
112
+ index ,
113
+ localIndices ,
114
+ request .indexFilter (),
115
+ nowInMillis , request .runtimeFields ()
116
+ ),
117
+ ActionListener .wrap (
118
+ response -> countDownListener .onResponse (Collections .singletonList (response )),
119
+ countDownListener ::onFailure
120
+ )
121
+ );
122
+ }
123
+
124
+ // this is the cross cluster part of this API - we force the other cluster to not merge the results but instead
125
+ // send us back all individual index results.
126
+ for (Map .Entry <String , OriginalIndices > remoteIndices : remoteClusterIndices .entrySet ()) {
127
+ String clusterAlias = remoteIndices .getKey ();
128
+ OriginalIndices originalIndices = remoteIndices .getValue ();
129
+ Client remoteClusterClient = remoteClusterService .getRemoteClusterClient (threadPool , clusterAlias );
130
+ FieldCapabilitiesRequest remoteRequest = new FieldCapabilitiesRequest ();
131
+ remoteRequest .setMergeResults (false ); // we need to merge on this node
132
+ remoteRequest .indicesOptions (originalIndices .indicesOptions ());
133
+ remoteRequest .indices (originalIndices .indices ());
134
+ remoteRequest .fields (request .fields ());
135
+ remoteRequest .runtimeFields (request .runtimeFields ());
136
+ remoteRequest .indexFilter (request .indexFilter ());
137
+ remoteRequest .nowInMillis (nowInMillis );
138
+ remoteClusterClient .fieldCaps (remoteRequest ,
139
+ ActionListener .wrap (response -> {
140
+ List <FieldCapabilitiesIndexResponse > remotes = new ArrayList <>();
141
+ for (FieldCapabilitiesIndexResponse resp : response .getIndexResponses ()) {
142
+ remotes .add (new FieldCapabilitiesIndexResponse (
143
+ RemoteClusterAware .buildRemoteIndexName (clusterAlias , resp .getIndexName ()),
144
+ resp .get (), resp .canMatch ()));
145
+ }
146
+ countDownListener .onResponse (remotes );
147
+ }, countDownListener ::onFailure ));
129
148
}
130
149
}
131
150
0 commit comments