8
8
9
9
package org .elasticsearch .action .admin .cluster .stats ;
10
10
11
- import org .apache .logging .log4j .Level ;
12
11
import org .apache .logging .log4j .LogManager ;
13
12
import org .apache .logging .log4j .Logger ;
14
- import org .elasticsearch .ExceptionsHelper ;
13
+ import org .elasticsearch .ElasticsearchException ;
14
+ import org .elasticsearch .action .ActionFuture ;
15
15
import org .elasticsearch .action .ActionListener ;
16
16
import org .elasticsearch .action .ActionType ;
17
17
import org .elasticsearch .action .FailedNodeException ;
18
- import org .elasticsearch .action .search .RemoteClusterActionListener ;
19
18
import org .elasticsearch .action .support .ActionFilters ;
20
19
import org .elasticsearch .action .support .PlainActionFuture ;
21
20
import org .elasticsearch .cluster .ClusterSnapshotStats ;
43
42
import java .util .HashMap ;
44
43
import java .util .List ;
45
44
import java .util .Map ;
46
- import java .util .concurrent .ExecutionException ;
47
45
import java .util .function .BiFunction ;
48
46
import java .util .function .BooleanSupplier ;
49
47
@@ -89,6 +87,14 @@ public TransportClusterStatsAction(
89
87
this .settings = settings ;
90
88
}
91
89
90
+ private ActionFuture <Map <String , RemoteClusterStatsResponse >> remoteFuture ;
91
+
92
+ @ Override
93
+ protected void doExecute (Task task , ClusterStatsRequest request , ActionListener <ClusterStatsResponse > listener ) {
94
+ remoteFuture = getStatsFromRemotes (request );
95
+ super .doExecute (task , request , listener );
96
+ }
97
+
92
98
@ Override
93
99
protected void newResponseAsync (
94
100
final Task task ,
@@ -111,8 +117,7 @@ protected void newResponseAsync(
111
117
clusterService .threadPool ().absoluteTimeInMillis ()
112
118
);
113
119
114
- // TODO: this should not be happening here but leaving it here for now until we figure out proper
115
- // threading/async model for this
120
+ // This will wait until remotes are done if it didn't happen yet
116
121
var remoteClusterStats = getRemoteClusterStats (request );
117
122
118
123
final ListenableFuture <MappingStats > mappingStatsStep = new ListenableFuture <>();
@@ -178,7 +183,7 @@ private Map<String, ClusterStatsResponse.RemoteClusterStats> getRemoteClusterSta
178
183
return null ;
179
184
}
180
185
Map <String , ClusterStatsResponse .RemoteClusterStats > remoteClustersStats = new HashMap <>();
181
- Map <String , RemoteClusterStatsResponse > remoteData = getStatsFromRemotes ( request );
186
+ Map <String , RemoteClusterStatsResponse > remoteData = resolveRemoteClusterStats ( );
182
187
183
188
for (String clusterAlias : remoteClusterService .getRegisteredRemoteClusterNames ()) {
184
189
RemoteClusterConnection remoteConnection = remoteClusterService .getRemoteClusterConnection (clusterAlias );
@@ -196,16 +201,27 @@ private Map<String, ClusterStatsResponse.RemoteClusterStats> getRemoteClusterSta
196
201
return remoteClustersStats ;
197
202
}
198
203
199
- private Map <String , RemoteClusterStatsResponse > getStatsFromRemotes (ClusterStatsRequest request ) {
200
- // TODO: make correct pool
201
- final var remoteClientResponseExecutor = transportService .getThreadPool ().executor (ThreadPool .Names .MANAGEMENT );
202
- if (request .doRemotes () == false ) {
204
+ private Map <String , RemoteClusterStatsResponse > resolveRemoteClusterStats () {
205
+ try {
206
+ return remoteFuture .actionGet ();
207
+ } catch (ElasticsearchException e ) {
208
+ logger .warn ("Failed to get remote cluster stats" , e );
203
209
return Map .of ();
204
210
}
211
+ }
212
+
213
+ private ActionFuture <Map <String , RemoteClusterStatsResponse >> getStatsFromRemotes (ClusterStatsRequest request ) {
214
+ if (request .doRemotes () == false ) {
215
+ // this will never be used since getRemoteClusterStats has the same check
216
+ return null ;
217
+ }
218
+
219
+ // TODO: make correct pool
220
+ final var remoteClientResponseExecutor = transportService .getThreadPool ().executor (ThreadPool .Names .MANAGEMENT );
205
221
var remotes = remoteClusterService .getRegisteredRemoteClusterNames ();
206
222
207
- var remotesListener = new PlainActionFuture <Map <String , RemoteClusterStatsResponse >>();
208
- var groupListener = new RemoteClusterActionListener <>(remotes .size (), remotesListener );
223
+ var remotesFuture = new PlainActionFuture <Map <String , RemoteClusterStatsResponse >>();
224
+ var groupListener = new RemoteClusterActionListener <>(remotes .size (), remotesFuture );
209
225
210
226
for (String clusterAlias : remotes ) {
211
227
ClusterStatsRequest remoteRequest = request .subRequest ();
@@ -222,13 +238,6 @@ private Map<String, RemoteClusterStatsResponse> getStatsFromRemotes(ClusterStats
222
238
223
239
}
224
240
225
- try {
226
- // TODO: how do we report errors?
227
- return remotesListener .get ();
228
- } catch (InterruptedException | ExecutionException e ) {
229
- logger .log (Level .ERROR , "Failed to get remote cluster stats: " , ExceptionsHelper .unwrapCause (e ));
230
- return Map .of ();
231
- }
241
+ return remotesFuture ;
232
242
}
233
-
234
243
}
0 commit comments