18
18
*/
19
19
package org .elasticsearch .transport ;
20
20
21
+ import java .util .function .Supplier ;
21
22
import org .elasticsearch .Version ;
22
23
import org .elasticsearch .cluster .metadata .ClusterNameExpressionResolver ;
23
24
import org .elasticsearch .cluster .node .DiscoveryNode ;
@@ -48,9 +49,20 @@ public abstract class RemoteClusterAware extends AbstractComponent {
48
49
/**
49
50
* A list of initial seed nodes to discover eligible nodes from the remote cluster
50
51
*/
51
- public static final Setting .AffixSetting <List <InetSocketAddress >> REMOTE_CLUSTERS_SEEDS = Setting .affixKeySetting ("search.remote." ,
52
- "seeds" , (key ) -> Setting .listSetting (key , Collections .emptyList (), RemoteClusterAware ::parseSeedAddress ,
53
- Setting .Property .NodeScope , Setting .Property .Dynamic ));
52
+ public static final Setting .AffixSetting <List <String >> REMOTE_CLUSTERS_SEEDS = Setting .affixKeySetting (
53
+ "search.remote." ,
54
+ "seeds" ,
55
+ key -> Setting .listSetting (
56
+ key , Collections .emptyList (),
57
+ s -> {
58
+ // validate seed address
59
+ parsePort (s );
60
+ return s ;
61
+ },
62
+ Setting .Property .NodeScope ,
63
+ Setting .Property .Dynamic
64
+ )
65
+ );
54
66
public static final char REMOTE_CLUSTER_INDEX_SEPARATOR = ':' ;
55
67
public static final String LOCAL_CLUSTER_GROUP_KEY = "" ;
56
68
@@ -65,18 +77,20 @@ protected RemoteClusterAware(Settings settings) {
65
77
this .clusterNameResolver = new ClusterNameExpressionResolver (settings );
66
78
}
67
79
68
- protected static Map <String , List <DiscoveryNode >> buildRemoteClustersSeeds (Settings settings ) {
69
- Stream <Setting <List <InetSocketAddress >>> allConcreteSettings = REMOTE_CLUSTERS_SEEDS .getAllConcreteSettings (settings );
80
+ protected static Map <String , List <Supplier < DiscoveryNode > >> buildRemoteClustersSeeds (Settings settings ) {
81
+ Stream <Setting <List <String >>> allConcreteSettings = REMOTE_CLUSTERS_SEEDS .getAllConcreteSettings (settings );
70
82
return allConcreteSettings .collect (
71
83
Collectors .toMap (REMOTE_CLUSTERS_SEEDS ::getNamespace , concreteSetting -> {
72
84
String clusterName = REMOTE_CLUSTERS_SEEDS .getNamespace (concreteSetting );
73
- List <DiscoveryNode > nodes = new ArrayList <>();
74
- for (InetSocketAddress address : concreteSetting .get (settings )) {
75
- TransportAddress transportAddress = new TransportAddress (address );
76
- DiscoveryNode node = new DiscoveryNode (clusterName + "#" + transportAddress .toString (),
77
- transportAddress ,
78
- Version .CURRENT .minimumCompatibilityVersion ());
79
- nodes .add (node );
85
+ List <String > addresses = concreteSetting .get (settings );
86
+ List <Supplier <DiscoveryNode >> nodes = new ArrayList <>(addresses .size ());
87
+ for (String address : addresses ) {
88
+ nodes .add (() -> {
89
+ TransportAddress transportAddress = new TransportAddress (RemoteClusterAware .parseSeedAddress (address ));
90
+ return new DiscoveryNode (clusterName + "#" + transportAddress .toString (),
91
+ transportAddress ,
92
+ Version .CURRENT .minimumCompatibilityVersion ());
93
+ });
80
94
}
81
95
return nodes ;
82
96
}));
@@ -128,7 +142,7 @@ public Map<String, List<String>> groupClusterIndices(String[] requestIndices, Pr
128
142
* Subclasses must implement this to receive information about updated cluster aliases. If the given address list is
129
143
* empty the cluster alias is unregistered and should be removed.
130
144
*/
131
- protected abstract void updateRemoteCluster (String clusterAlias , List <InetSocketAddress > addresses );
145
+ protected abstract void updateRemoteCluster (String clusterAlias , List <String > addresses );
132
146
133
147
/**
134
148
* Registers this instance to listen to updates on the cluster settings.
@@ -138,27 +152,35 @@ public void listenForUpdates(ClusterSettings clusterSettings) {
138
152
(namespace , value ) -> {});
139
153
}
140
154
141
- private static InetSocketAddress parseSeedAddress (String remoteHost ) {
142
- int portSeparator = remoteHost .lastIndexOf (':' ); // in case we have a IPv6 address ie. [::1]:9300
143
- if (portSeparator == -1 || portSeparator == remoteHost .length ()) {
144
- throw new IllegalArgumentException ("remote hosts need to be configured as [host:port], found [" + remoteHost + "] instead" );
145
- }
146
- String host = remoteHost .substring (0 , portSeparator );
155
+ protected static InetSocketAddress parseSeedAddress (String remoteHost ) {
156
+ String host = remoteHost .substring (0 , indexOfPortSeparator (remoteHost ));
147
157
InetAddress hostAddress ;
148
158
try {
149
159
hostAddress = InetAddress .getByName (host );
150
160
} catch (UnknownHostException e ) {
151
161
throw new IllegalArgumentException ("unknown host [" + host + "]" , e );
152
162
}
163
+ return new InetSocketAddress (hostAddress , parsePort (remoteHost ));
164
+ }
165
+
166
+ private static int parsePort (String remoteHost ) {
153
167
try {
154
- int port = Integer .valueOf (remoteHost .substring (portSeparator + 1 ));
168
+ int port = Integer .valueOf (remoteHost .substring (indexOfPortSeparator ( remoteHost ) + 1 ));
155
169
if (port <= 0 ) {
156
170
throw new IllegalArgumentException ("port number must be > 0 but was: [" + port + "]" );
157
171
}
158
- return new InetSocketAddress ( hostAddress , port ) ;
172
+ return port ;
159
173
} catch (NumberFormatException e ) {
160
- throw new IllegalArgumentException ("port must be a number" , e );
174
+ throw new IllegalArgumentException ("failed to parse port" , e );
175
+ }
176
+ }
177
+
178
+ private static int indexOfPortSeparator (String remoteHost ) {
179
+ int portSeparator = remoteHost .lastIndexOf (':' ); // in case we have a IPv6 address ie. [::1]:9300
180
+ if (portSeparator == -1 || portSeparator == remoteHost .length ()) {
181
+ throw new IllegalArgumentException ("remote hosts need to be configured as [host:port], found [" + remoteHost + "] instead" );
161
182
}
183
+ return portSeparator ;
162
184
}
163
185
164
186
public static String buildRemoteIndexName (String clusterAlias , String indexName ) {
0 commit comments