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 ;
@@ -88,6 +89,7 @@ final class RemoteClusterConnection extends AbstractComponent implements Transpo
8889 private final int maxNumRemoteConnections ;
8990 private final Predicate <DiscoveryNode > nodePredicate ;
9091 private final ThreadPool threadPool ;
92+ private volatile String proxyAddress ;
9193 private volatile List <Supplier <DiscoveryNode >> seedNodes ;
9294 private volatile boolean skipUnavailable ;
9395 private final ConnectHandler connectHandler ;
@@ -106,6 +108,13 @@ final class RemoteClusterConnection extends AbstractComponent implements Transpo
106108 RemoteClusterConnection (Settings settings , String clusterAlias , List <Supplier <DiscoveryNode >> seedNodes ,
107109 TransportService transportService , ConnectionManager connectionManager , int maxNumRemoteConnections ,
108110 Predicate <DiscoveryNode > nodePredicate ) {
111+ this (settings , clusterAlias , seedNodes , transportService , connectionManager , maxNumRemoteConnections , nodePredicate , null );
112+ }
113+
114+ RemoteClusterConnection (Settings settings , String clusterAlias , List <Supplier <DiscoveryNode >> seedNodes ,
115+ TransportService transportService , ConnectionManager connectionManager , int maxNumRemoteConnections , Predicate <DiscoveryNode >
116+ nodePredicate ,
117+ String proxyAddress ) {
109118 super (settings );
110119 this .transportService = transportService ;
111120 this .maxNumRemoteConnections = maxNumRemoteConnections ;
@@ -130,13 +139,26 @@ final class RemoteClusterConnection extends AbstractComponent implements Transpo
130139 connectionManager .addListener (this );
131140 // we register the transport service here as a listener to make sure we notify handlers on disconnect etc.
132141 connectionManager .addListener (transportService );
142+ this .proxyAddress = proxyAddress ;
143+ }
144+
145+ private static DiscoveryNode maybeAddProxyAddress (String proxyAddress , DiscoveryNode node ) {
146+ if (proxyAddress == null || proxyAddress .isEmpty ()) {
147+ return node ;
148+ } else {
149+ // resovle proxy address lazy here
150+ InetSocketAddress proxyInetAddress = RemoteClusterAware .parseSeedAddress (proxyAddress );
151+ return new DiscoveryNode (node .getName (), node .getId (), node .getEphemeralId (), node .getHostName (), node
152+ .getHostAddress (), new TransportAddress (proxyInetAddress ), node .getAttributes (), node .getRoles (), node .getVersion ());
153+ }
133154 }
134155
135156 /**
136157 * Updates the list of seed nodes for this cluster connection
137158 */
138- synchronized void updateSeedNodes (List <Supplier <DiscoveryNode >> seedNodes , ActionListener <Void > connectListener ) {
159+ synchronized void updateSeedNodes (String proxyAddress , List <Supplier <DiscoveryNode >> seedNodes , ActionListener <Void > connectListener ) {
139160 this .seedNodes = Collections .unmodifiableList (new ArrayList <>(seedNodes ));
161+ this .proxyAddress = proxyAddress ;
140162 connectHandler .connect (connectListener );
141163 }
142164
@@ -281,6 +303,7 @@ Transport.Connection getConnection(DiscoveryNode remoteClusterNode) {
281303 return new ProxyConnection (connection , remoteClusterNode );
282304 }
283305
306+
284307 static final class ProxyConnection implements Transport .Connection {
285308 private final Transport .Connection proxyConnection ;
286309 private final DiscoveryNode targetNode ;
@@ -461,7 +484,7 @@ private void collectRemoteNodes(Iterator<Supplier<DiscoveryNode>> seedNodes,
461484 try {
462485 if (seedNodes .hasNext ()) {
463486 cancellableThreads .executeIO (() -> {
464- final DiscoveryNode seedNode = seedNodes .next ().get ();
487+ final DiscoveryNode seedNode = maybeAddProxyAddress ( proxyAddress , seedNodes .next ().get () );
465488 final TransportService .HandshakeResponse handshakeResponse ;
466489 Transport .Connection connection = manager .openConnection (seedNode ,
467490 ConnectionProfile .buildSingleChannelProfile (TransportRequestOptions .Type .REG , null , null ));
@@ -476,7 +499,7 @@ private void collectRemoteNodes(Iterator<Supplier<DiscoveryNode>> seedNodes,
476499 throw ex ;
477500 }
478501
479- final DiscoveryNode handshakeNode = handshakeResponse .getDiscoveryNode ();
502+ final DiscoveryNode handshakeNode = maybeAddProxyAddress ( proxyAddress , handshakeResponse .getDiscoveryNode () );
480503 if (nodePredicate .test (handshakeNode ) && connectedNodes .size () < maxNumRemoteConnections ) {
481504 manager .connectToNode (handshakeNode , remoteProfile , transportService .connectionValidator (handshakeNode ));
482505 if (remoteClusterName .get () == null ) {
@@ -583,7 +606,8 @@ public void handleResponse(ClusterStateResponse response) {
583606 cancellableThreads .executeIO (() -> {
584607 DiscoveryNodes nodes = response .getState ().nodes ();
585608 Iterable <DiscoveryNode > nodesIter = nodes .getNodes ()::valuesIt ;
586- for (DiscoveryNode node : nodesIter ) {
609+ for (DiscoveryNode n : nodesIter ) {
610+ DiscoveryNode node = maybeAddProxyAddress (proxyAddress , n );
587611 if (nodePredicate .test (node ) && connectedNodes .size () < maxNumRemoteConnections ) {
588612 try {
589613 connectionManager .connectToNode (node , remoteProfile ,
@@ -646,7 +670,8 @@ void addConnectedNode(DiscoveryNode node) {
646670 * Get the information about remote nodes to be rendered on {@code _remote/info} requests.
647671 */
648672 public RemoteConnectionInfo getConnectionInfo () {
649- List <TransportAddress > seedNodeAddresses = seedNodes .stream ().map (node -> node .get ().getAddress ()).collect (Collectors .toList ());
673+ List <TransportAddress > seedNodeAddresses = seedNodes .stream ().map (node -> node .get ().getAddress ()).collect
674+ (Collectors .toList ());
650675 TimeValue initialConnectionTimeout = RemoteClusterService .REMOTE_INITIAL_CONNECTION_TIMEOUT_SETTING .get (settings );
651676 return new RemoteConnectionInfo (clusterAlias , seedNodeAddresses , maxNumRemoteConnections , connectedNodes .size (),
652677 initialConnectionTimeout , skipUnavailable );
0 commit comments