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 ;
40
39
import org .elasticsearch .common .util .CancellableThreads ;
41
40
import org .elasticsearch .common .util .concurrent .AbstractRunnable ;
42
41
import org .elasticsearch .common .util .concurrent .ThreadContext ;
42
+ import org .elasticsearch .core .internal .io .IOUtils ;
43
43
import org .elasticsearch .threadpool .ThreadPool ;
44
44
45
45
import java .io .Closeable ;
50
50
import java .util .HashSet ;
51
51
import java .util .Iterator ;
52
52
import java .util .List ;
53
- import java .util .Optional ;
54
53
import java .util .Set ;
55
54
import java .util .concurrent .ArrayBlockingQueue ;
56
55
import java .util .concurrent .BlockingQueue ;
61
60
import java .util .function .Consumer ;
62
61
import java .util .function .Function ;
63
62
import java .util .function .Predicate ;
64
- import java .util .function .Supplier ;
65
63
import java .util .stream .Collectors ;
66
64
67
65
/**
@@ -181,7 +179,7 @@ public void ensureConnected(ActionListener<Void> voidActionListener) {
181
179
182
180
private void fetchShardsInternal (ClusterSearchShardsRequest searchShardsRequest ,
183
181
final ActionListener <ClusterSearchShardsResponse > listener ) {
184
- final DiscoveryNode node = connectedNodes .get ();
182
+ final DiscoveryNode node = connectedNodes .getAny ();
185
183
transportService .sendRequest (node , ClusterSearchShardsAction .NAME , searchShardsRequest ,
186
184
new TransportResponseHandler <ClusterSearchShardsResponse >() {
187
185
@@ -217,7 +215,7 @@ void collectNodes(ActionListener<Function<String, DiscoveryNode>> listener) {
217
215
request .clear ();
218
216
request .nodes (true );
219
217
request .local (true ); // run this on the node that gets the request it's as good as any other
220
- final DiscoveryNode node = connectedNodes .get ();
218
+ final DiscoveryNode node = connectedNodes .getAny ();
221
219
transportService .sendRequest (node , ClusterStateAction .NAME , request , TransportRequestOptions .EMPTY ,
222
220
new TransportResponseHandler <ClusterStateResponse >() {
223
221
@ Override
@@ -255,40 +253,52 @@ public String executor() {
255
253
}
256
254
257
255
/**
258
- * Returns a connection to the remote cluster. This connection might be a proxy connection that redirects internally to the
259
- * given node.
256
+ * Returns a connection to the remote cluster, preferably a direct connection to the provided {@link DiscoveryNode}.
257
+ * If such node is not connected, the returned connection will be a proxy connection that redirects to it .
260
258
*/
261
259
Transport .Connection getConnection (DiscoveryNode remoteClusterNode ) {
262
- DiscoveryNode discoveryNode = connectedNodes .get ();
260
+ if (transportService .nodeConnected (remoteClusterNode )) {
261
+ return transportService .getConnection (remoteClusterNode );
262
+ }
263
+ DiscoveryNode discoveryNode = connectedNodes .getAny ();
263
264
Transport .Connection connection = transportService .getConnection (discoveryNode );
264
- return new Transport .Connection () {
265
- @ Override
266
- public DiscoveryNode getNode () {
267
- return remoteClusterNode ;
268
- }
265
+ return new ProxyConnection (connection , remoteClusterNode );
266
+ }
269
267
270
- @ Override
271
- public void sendRequest (long requestId , String action , TransportRequest request , TransportRequestOptions options )
268
+ static final class ProxyConnection implements Transport .Connection {
269
+ private final Transport .Connection proxyConnection ;
270
+ private final DiscoveryNode targetNode ;
271
+
272
+ private ProxyConnection (Transport .Connection proxyConnection , DiscoveryNode targetNode ) {
273
+ this .proxyConnection = proxyConnection ;
274
+ this .targetNode = targetNode ;
275
+ }
276
+
277
+ @ Override
278
+ public DiscoveryNode getNode () {
279
+ return targetNode ;
280
+ }
281
+
282
+ @ Override
283
+ public void sendRequest (long requestId , String action , TransportRequest request , TransportRequestOptions options )
272
284
throws IOException , TransportException {
273
- connection .sendRequest (requestId , TransportActionProxy .getProxyAction (action ),
274
- TransportActionProxy .wrapRequest (remoteClusterNode , request ), options );
275
- }
285
+ proxyConnection .sendRequest (requestId , TransportActionProxy .getProxyAction (action ),
286
+ TransportActionProxy .wrapRequest (targetNode , request ), options );
287
+ }
276
288
277
- @ Override
278
- public void close () throws IOException {
279
- assert false : "proxy connections must not be closed" ;
280
- }
289
+ @ Override
290
+ public void close () {
291
+ assert false : "proxy connections must not be closed" ;
292
+ }
281
293
282
- @ Override
283
- public Version getVersion () {
284
- return connection .getVersion ();
285
- }
286
- };
294
+ @ Override
295
+ public Version getVersion () {
296
+ return proxyConnection .getVersion ();
297
+ }
287
298
}
288
299
289
300
Transport .Connection getConnection () {
290
- DiscoveryNode discoveryNode = connectedNodes .get ();
291
- return transportService .getConnection (discoveryNode );
301
+ return transportService .getConnection (getAnyConnectedNode ());
292
302
}
293
303
294
304
@ Override
@@ -385,7 +395,7 @@ public void onFailure(Exception e) {
385
395
}
386
396
387
397
@ Override
388
- protected void doRun () throws Exception {
398
+ protected void doRun () {
389
399
ActionListener <Void > listener = ActionListener .wrap ((x ) -> {
390
400
synchronized (queue ) {
391
401
running .release ();
@@ -590,8 +600,8 @@ boolean isNodeConnected(final DiscoveryNode node) {
590
600
return connectedNodes .contains (node );
591
601
}
592
602
593
- DiscoveryNode getConnectedNode () {
594
- return connectedNodes .get ();
603
+ DiscoveryNode getAnyConnectedNode () {
604
+ return connectedNodes .getAny ();
595
605
}
596
606
597
607
void addConnectedNode (DiscoveryNode node ) {
@@ -612,7 +622,7 @@ int getNumNodesConnected() {
612
622
return connectedNodes .size ();
613
623
}
614
624
615
- private static class ConnectedNodes implements Supplier < DiscoveryNode > {
625
+ private static final class ConnectedNodes {
616
626
617
627
private final Set <DiscoveryNode > nodeSet = new HashSet <>();
618
628
private final String clusterAlias ;
@@ -623,8 +633,7 @@ private ConnectedNodes(String clusterAlias) {
623
633
this .clusterAlias = clusterAlias ;
624
634
}
625
635
626
- @ Override
627
- public synchronized DiscoveryNode get () {
636
+ public synchronized DiscoveryNode getAny () {
628
637
ensureIteratorAvailable ();
629
638
if (currentIterator .hasNext ()) {
630
639
return currentIterator .next ();
@@ -657,15 +666,6 @@ synchronized boolean contains(DiscoveryNode node) {
657
666
return nodeSet .contains (node );
658
667
}
659
668
660
- synchronized Optional <DiscoveryNode > getAny () {
661
- ensureIteratorAvailable ();
662
- if (currentIterator .hasNext ()) {
663
- return Optional .of (currentIterator .next ());
664
- } else {
665
- return Optional .empty ();
666
- }
667
- }
668
-
669
669
private synchronized void ensureIteratorAvailable () {
670
670
if (currentIterator == null ) {
671
671
currentIterator = nodeSet .iterator ();
0 commit comments