20
20
21
21
import org .apache .logging .log4j .message .ParameterizedMessage ;
22
22
import org .apache .lucene .store .AlreadyClosedException ;
23
- import org .elasticsearch .core .internal .io .IOUtils ;
24
23
import org .apache .lucene .util .SetOnce ;
25
24
import org .elasticsearch .Version ;
26
25
import org .elasticsearch .action .ActionListener ;
43
42
import org .elasticsearch .common .util .CancellableThreads ;
44
43
import org .elasticsearch .common .util .concurrent .AbstractRunnable ;
45
44
import org .elasticsearch .common .util .concurrent .ThreadContext ;
45
+ import org .elasticsearch .core .internal .io .IOUtils ;
46
46
import org .elasticsearch .threadpool .ThreadPool ;
47
47
48
48
import java .io .Closeable ;
64
64
import java .util .function .Consumer ;
65
65
import java .util .function .Function ;
66
66
import java .util .function .Predicate ;
67
- import java .util .function .Supplier ;
68
67
import java .util .stream .Collectors ;
69
68
70
69
/**
@@ -184,7 +183,7 @@ public void ensureConnected(ActionListener<Void> voidActionListener) {
184
183
185
184
private void fetchShardsInternal (ClusterSearchShardsRequest searchShardsRequest ,
186
185
final ActionListener <ClusterSearchShardsResponse > listener ) {
187
- final DiscoveryNode node = connectedNodes .get ();
186
+ final DiscoveryNode node = connectedNodes .getAny ();
188
187
transportService .sendRequest (node , ClusterSearchShardsAction .NAME , searchShardsRequest ,
189
188
new TransportResponseHandler <ClusterSearchShardsResponse >() {
190
189
@@ -220,7 +219,7 @@ void collectNodes(ActionListener<Function<String, DiscoveryNode>> listener) {
220
219
request .clear ();
221
220
request .nodes (true );
222
221
request .local (true ); // run this on the node that gets the request it's as good as any other
223
- final DiscoveryNode node = connectedNodes .get ();
222
+ final DiscoveryNode node = connectedNodes .getAny ();
224
223
transportService .sendRequest (node , ClusterStateAction .NAME , request , TransportRequestOptions .EMPTY ,
225
224
new TransportResponseHandler <ClusterStateResponse >() {
226
225
@ Override
@@ -258,40 +257,52 @@ public String executor() {
258
257
}
259
258
260
259
/**
261
- * Returns a connection to the remote cluster. This connection might be a proxy connection that redirects internally to the
262
- * given node.
260
+ * Returns a connection to the remote cluster, preferably a direct connection to the provided {@link DiscoveryNode}.
261
+ * If such node is not connected, the returned connection will be a proxy connection that redirects to it .
263
262
*/
264
263
Transport .Connection getConnection (DiscoveryNode remoteClusterNode ) {
265
- DiscoveryNode discoveryNode = connectedNodes .get ();
264
+ if (transportService .nodeConnected (remoteClusterNode )) {
265
+ return transportService .getConnection (remoteClusterNode );
266
+ }
267
+ DiscoveryNode discoveryNode = connectedNodes .getAny ();
266
268
Transport .Connection connection = transportService .getConnection (discoveryNode );
267
- return new Transport .Connection () {
268
- @ Override
269
- public DiscoveryNode getNode () {
270
- return remoteClusterNode ;
271
- }
269
+ return new ProxyConnection (connection , remoteClusterNode );
270
+ }
272
271
273
- @ Override
274
- public void sendRequest (long requestId , String action , TransportRequest request , TransportRequestOptions options )
272
+ static final class ProxyConnection implements Transport .Connection {
273
+ private final Transport .Connection proxyConnection ;
274
+ private final DiscoveryNode targetNode ;
275
+
276
+ private ProxyConnection (Transport .Connection proxyConnection , DiscoveryNode targetNode ) {
277
+ this .proxyConnection = proxyConnection ;
278
+ this .targetNode = targetNode ;
279
+ }
280
+
281
+ @ Override
282
+ public DiscoveryNode getNode () {
283
+ return targetNode ;
284
+ }
285
+
286
+ @ Override
287
+ public void sendRequest (long requestId , String action , TransportRequest request , TransportRequestOptions options )
275
288
throws IOException , TransportException {
276
- connection .sendRequest (requestId , TransportActionProxy .getProxyAction (action ),
277
- TransportActionProxy .wrapRequest (remoteClusterNode , request ), options );
278
- }
289
+ proxyConnection .sendRequest (requestId , TransportActionProxy .getProxyAction (action ),
290
+ TransportActionProxy .wrapRequest (targetNode , request ), options );
291
+ }
279
292
280
- @ Override
281
- public void close () throws IOException {
282
- assert false : "proxy connections must not be closed" ;
283
- }
293
+ @ Override
294
+ public void close () {
295
+ assert false : "proxy connections must not be closed" ;
296
+ }
284
297
285
- @ Override
286
- public Version getVersion () {
287
- return connection .getVersion ();
288
- }
289
- };
298
+ @ Override
299
+ public Version getVersion () {
300
+ return proxyConnection .getVersion ();
301
+ }
290
302
}
291
303
292
304
Transport .Connection getConnection () {
293
- DiscoveryNode discoveryNode = connectedNodes .get ();
294
- return transportService .getConnection (discoveryNode );
305
+ return transportService .getConnection (getAnyConnectedNode ());
295
306
}
296
307
297
308
@ Override
@@ -388,7 +399,7 @@ public void onFailure(Exception e) {
388
399
}
389
400
390
401
@ Override
391
- protected void doRun () throws Exception {
402
+ protected void doRun () {
392
403
ActionListener <Void > listener = ActionListener .wrap ((x ) -> {
393
404
synchronized (queue ) {
394
405
running .release ();
@@ -593,8 +604,8 @@ boolean isNodeConnected(final DiscoveryNode node) {
593
604
return connectedNodes .contains (node );
594
605
}
595
606
596
- DiscoveryNode getConnectedNode () {
597
- return connectedNodes .get ();
607
+ DiscoveryNode getAnyConnectedNode () {
608
+ return connectedNodes .getAny ();
598
609
}
599
610
600
611
void addConnectedNode (DiscoveryNode node ) {
@@ -605,7 +616,7 @@ void addConnectedNode(DiscoveryNode node) {
605
616
* Fetches connection info for this connection
606
617
*/
607
618
public void getConnectionInfo (ActionListener <RemoteConnectionInfo > listener ) {
608
- final Optional <DiscoveryNode > anyNode = connectedNodes .getAny ();
619
+ final Optional <DiscoveryNode > anyNode = connectedNodes .getAnyConnectedNode ();
609
620
if (anyNode .isPresent () == false ) {
610
621
// not connected we return immediately
611
622
RemoteConnectionInfo remoteConnectionStats = new RemoteConnectionInfo (clusterAlias ,
@@ -668,7 +679,7 @@ int getNumNodesConnected() {
668
679
return connectedNodes .size ();
669
680
}
670
681
671
- private static class ConnectedNodes implements Supplier < DiscoveryNode > {
682
+ private static final class ConnectedNodes {
672
683
673
684
private final Set <DiscoveryNode > nodeSet = new HashSet <>();
674
685
private final String clusterAlias ;
@@ -679,8 +690,7 @@ private ConnectedNodes(String clusterAlias) {
679
690
this .clusterAlias = clusterAlias ;
680
691
}
681
692
682
- @ Override
683
- public synchronized DiscoveryNode get () {
693
+ public synchronized DiscoveryNode getAny () {
684
694
ensureIteratorAvailable ();
685
695
if (currentIterator .hasNext ()) {
686
696
return currentIterator .next ();
@@ -713,7 +723,7 @@ synchronized boolean contains(DiscoveryNode node) {
713
723
return nodeSet .contains (node );
714
724
}
715
725
716
- synchronized Optional <DiscoveryNode > getAny () {
726
+ synchronized Optional <DiscoveryNode > getAnyConnectedNode () {
717
727
ensureIteratorAvailable ();
718
728
if (currentIterator .hasNext ()) {
719
729
return Optional .of (currentIterator .next ());
0 commit comments