|
25 | 25 | import org.elasticsearch.tasks.Task; |
26 | 26 | import org.elasticsearch.threadpool.ThreadPool; |
27 | 27 | import org.elasticsearch.transport.RemoteClusterAware; |
| 28 | +import org.elasticsearch.transport.RemoteClusterService; |
28 | 29 | import org.elasticsearch.transport.Transport; |
29 | 30 | import org.elasticsearch.transport.TransportChannel; |
30 | 31 | import org.elasticsearch.transport.TransportRequest; |
@@ -72,12 +73,14 @@ public class EnrichPolicyResolver { |
72 | 73 | private final IndexResolver indexResolver; |
73 | 74 | private final TransportService transportService; |
74 | 75 | private final ThreadPool threadPool; |
| 76 | + private final RemoteClusterService remoteClusterService; |
75 | 77 |
|
76 | 78 | public EnrichPolicyResolver(ClusterService clusterService, TransportService transportService, IndexResolver indexResolver) { |
77 | 79 | this.clusterService = clusterService; |
78 | 80 | this.transportService = transportService; |
79 | 81 | this.indexResolver = indexResolver; |
80 | 82 | this.threadPool = transportService.getThreadPool(); |
| 83 | + this.remoteClusterService = transportService.getRemoteClusterService(); |
81 | 84 | transportService.registerRequestHandler( |
82 | 85 | RESOLVE_ACTION_NAME, |
83 | 86 | threadPool.executor(ThreadPool.Names.SEARCH), |
@@ -257,22 +260,21 @@ private void lookupPolicies( |
257 | 260 | // remote clusters |
258 | 261 | if (remotePolicies.isEmpty() == false) { |
259 | 262 | for (String cluster : remoteClusters) { |
260 | | - final Transport.Connection connection; |
261 | | - try { |
262 | | - connection = getRemoteConnection(cluster); |
263 | | - } catch (Exception e) { |
264 | | - refs.acquire().onFailure(e); |
265 | | - return; |
266 | | - } |
267 | | - transportService.sendRequest( |
268 | | - connection, |
269 | | - RESOLVE_ACTION_NAME, |
270 | | - new LookupRequest(cluster, remotePolicies), |
271 | | - TransportRequestOptions.EMPTY, |
272 | | - new ActionListenerResponseHandler<>( |
273 | | - refs.acquire(resp -> lookupResponses.put(cluster, resp)), |
274 | | - LookupResponse::new, |
275 | | - threadPool.executor(ThreadPool.Names.SEARCH) |
| 263 | + ActionListener<LookupResponse> lookupListener = refs.acquire(resp -> lookupResponses.put(cluster, resp)); |
| 264 | + getRemoteConnection( |
| 265 | + cluster, |
| 266 | + lookupListener.delegateFailureAndWrap( |
| 267 | + (delegate, connection) -> transportService.sendRequest( |
| 268 | + connection, |
| 269 | + RESOLVE_ACTION_NAME, |
| 270 | + new LookupRequest(cluster, remotePolicies), |
| 271 | + TransportRequestOptions.EMPTY, |
| 272 | + new ActionListenerResponseHandler<>( |
| 273 | + delegate, |
| 274 | + LookupResponse::new, |
| 275 | + threadPool.executor(ThreadPool.Names.SEARCH) |
| 276 | + ) |
| 277 | + ) |
276 | 278 | ) |
277 | 279 | ); |
278 | 280 | } |
@@ -389,13 +391,16 @@ protected Map<String, EnrichPolicy> availablePolicies() { |
389 | 391 | return metadata == null ? Map.of() : metadata.getPolicies(); |
390 | 392 | } |
391 | 393 |
|
392 | | - protected Transport.Connection getRemoteConnection(String cluster) { |
393 | | - return transportService.getRemoteClusterService().getConnection(cluster); |
| 394 | + protected void getRemoteConnection(String cluster, ActionListener<Transport.Connection> listener) { |
| 395 | + remoteClusterService.maybeEnsureConnectedAndGetConnection( |
| 396 | + cluster, |
| 397 | + remoteClusterService.isSkipUnavailable(cluster) == false, |
| 398 | + listener |
| 399 | + ); |
394 | 400 | } |
395 | 401 |
|
396 | 402 | public Map<String, List<String>> groupIndicesPerCluster(String[] indices) { |
397 | | - return transportService.getRemoteClusterService() |
398 | | - .groupIndices(SearchRequest.DEFAULT_INDICES_OPTIONS, indices) |
| 403 | + return remoteClusterService.groupIndices(SearchRequest.DEFAULT_INDICES_OPTIONS, indices) |
399 | 404 | .entrySet() |
400 | 405 | .stream() |
401 | 406 | .collect(Collectors.toMap(Map.Entry::getKey, e -> Arrays.asList(e.getValue().indices()))); |
|
0 commit comments