1818 */
1919package org .elasticsearch .transport ;
2020
21+ import java .net .InetSocketAddress ;
2122import java .util .function .Supplier ;
2223import org .apache .logging .log4j .message .ParameterizedMessage ;
2324import org .apache .lucene .store .AlreadyClosedException ;
4243import org .elasticsearch .common .io .stream .StreamInput ;
4344import org .elasticsearch .common .settings .Settings ;
4445import org .elasticsearch .common .transport .TransportAddress ;
46+ import org .elasticsearch .common .unit .TimeValue ;
4547import org .elasticsearch .common .util .CancellableThreads ;
4648import org .elasticsearch .common .util .concurrent .AbstractRunnable ;
4749import org .elasticsearch .common .util .concurrent .ThreadContext ;
@@ -92,6 +94,7 @@ final class RemoteClusterConnection extends AbstractComponent implements Transpo
9294 private final int maxNumRemoteConnections ;
9395 private final Predicate <DiscoveryNode > nodePredicate ;
9496 private final ThreadPool threadPool ;
97+ private volatile String proxyAddress ;
9598 private volatile List <Supplier <DiscoveryNode >> seedNodes ;
9699 private volatile boolean skipUnavailable ;
97100 private final ConnectHandler connectHandler ;
@@ -110,6 +113,13 @@ final class RemoteClusterConnection extends AbstractComponent implements Transpo
110113 RemoteClusterConnection (Settings settings , String clusterAlias , List <Supplier <DiscoveryNode >> seedNodes ,
111114 TransportService transportService , ConnectionManager connectionManager , int maxNumRemoteConnections ,
112115 Predicate <DiscoveryNode > nodePredicate ) {
116+ this (settings , clusterAlias , seedNodes , transportService , connectionManager , maxNumRemoteConnections , nodePredicate , null );
117+ }
118+
119+ RemoteClusterConnection (Settings settings , String clusterAlias , List <Supplier <DiscoveryNode >> seedNodes ,
120+ TransportService transportService , ConnectionManager connectionManager , int maxNumRemoteConnections , Predicate <DiscoveryNode >
121+ nodePredicate ,
122+ String proxyAddress ) {
113123 super (settings );
114124 this .transportService = transportService ;
115125 this .maxNumRemoteConnections = maxNumRemoteConnections ;
@@ -134,13 +144,26 @@ final class RemoteClusterConnection extends AbstractComponent implements Transpo
134144 connectionManager .addListener (this );
135145 // we register the transport service here as a listener to make sure we notify handlers on disconnect etc.
136146 connectionManager .addListener (transportService );
147+ this .proxyAddress = proxyAddress ;
148+ }
149+
150+ private static DiscoveryNode maybeAddProxyAddress (String proxyAddress , DiscoveryNode node ) {
151+ if (proxyAddress == null || proxyAddress .isEmpty ()) {
152+ return node ;
153+ } else {
154+ // resovle proxy address lazy here
155+ InetSocketAddress proxyInetAddress = RemoteClusterAware .parseSeedAddress (proxyAddress );
156+ return new DiscoveryNode (node .getName (), node .getId (), node .getEphemeralId (), node .getHostName (), node
157+ .getHostAddress (), new TransportAddress (proxyInetAddress ), node .getAttributes (), node .getRoles (), node .getVersion ());
158+ }
137159 }
138160
139161 /**
140162 * Updates the list of seed nodes for this cluster connection
141163 */
142- synchronized void updateSeedNodes (List <Supplier <DiscoveryNode >> seedNodes , ActionListener <Void > connectListener ) {
164+ synchronized void updateSeedNodes (String proxyAddress , List <Supplier <DiscoveryNode >> seedNodes , ActionListener <Void > connectListener ) {
143165 this .seedNodes = Collections .unmodifiableList (new ArrayList <>(seedNodes ));
166+ this .proxyAddress = proxyAddress ;
144167 connectHandler .connect (connectListener );
145168 }
146169
@@ -285,6 +308,7 @@ Transport.Connection getConnection(DiscoveryNode remoteClusterNode) {
285308 return new ProxyConnection (connection , remoteClusterNode );
286309 }
287310
311+
288312 static final class ProxyConnection implements Transport .Connection {
289313 private final Transport .Connection proxyConnection ;
290314 private final DiscoveryNode targetNode ;
@@ -465,7 +489,7 @@ private void collectRemoteNodes(Iterator<Supplier<DiscoveryNode>> seedNodes,
465489 try {
466490 if (seedNodes .hasNext ()) {
467491 cancellableThreads .executeIO (() -> {
468- final DiscoveryNode seedNode = seedNodes .next ().get ();
492+ final DiscoveryNode seedNode = maybeAddProxyAddress ( proxyAddress , seedNodes .next ().get () );
469493 final TransportService .HandshakeResponse handshakeResponse ;
470494 Transport .Connection connection = manager .openConnection (seedNode ,
471495 ConnectionProfile .buildSingleChannelProfile (TransportRequestOptions .Type .REG , null , null ));
@@ -480,7 +504,7 @@ private void collectRemoteNodes(Iterator<Supplier<DiscoveryNode>> seedNodes,
480504 throw ex ;
481505 }
482506
483- final DiscoveryNode handshakeNode = handshakeResponse .getDiscoveryNode ();
507+ final DiscoveryNode handshakeNode = maybeAddProxyAddress ( proxyAddress , handshakeResponse .getDiscoveryNode () );
484508 if (nodePredicate .test (handshakeNode ) && connectedNodes .size () < maxNumRemoteConnections ) {
485509 manager .connectToNode (handshakeNode , remoteProfile , transportService .connectionValidator (handshakeNode ));
486510 if (remoteClusterName .get () == null ) {
@@ -587,7 +611,8 @@ public void handleResponse(ClusterStateResponse response) {
587611 cancellableThreads .executeIO (() -> {
588612 DiscoveryNodes nodes = response .getState ().nodes ();
589613 Iterable <DiscoveryNode > nodesIter = nodes .getNodes ()::valuesIt ;
590- for (DiscoveryNode node : nodesIter ) {
614+ for (DiscoveryNode n : nodesIter ) {
615+ DiscoveryNode node = maybeAddProxyAddress (proxyAddress , n );
591616 if (nodePredicate .test (node ) && connectedNodes .size () < maxNumRemoteConnections ) {
592617 try {
593618 connectionManager .connectToNode (node , remoteProfile ,
@@ -709,6 +734,14 @@ public String executor() {
709734 }
710735 }
711736
737+ RemoteConnectionInfo getLocalConnectionInfo () { // for tests
738+ List <TransportAddress > seedNodeAddresses = seedNodes .stream ().map (node -> node .get ().getAddress ()).collect
739+ (Collectors .toList ());
740+ TimeValue initialConnectionTimeout = RemoteClusterService .REMOTE_INITIAL_CONNECTION_TIMEOUT_SETTING .get (settings );
741+ return new RemoteConnectionInfo (clusterAlias , Collections .emptyList (),
742+ seedNodeAddresses , maxNumRemoteConnections , connectedNodes .size (), initialConnectionTimeout , skipUnavailable );
743+ }
744+
712745 int getNumNodesConnected () {
713746 return connectedNodes .size ();
714747 }
0 commit comments