Skip to content

Commit 881d0bf

Browse files
committed
Add server name to remote info API (#53634)
This commit adds the configured server_name to the proxy mode info so that it can be exposed in the remote info API.
1 parent c3d2417 commit 881d0bf

File tree

5 files changed

+44
-15
lines changed

5 files changed

+44
-15
lines changed

client/rest-high-level/src/main/java/org/elasticsearch/client/cluster/ProxyModeInfo.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,17 @@
2424
public class ProxyModeInfo implements RemoteConnectionInfo.ModeInfo {
2525
static final String NAME = "proxy";
2626
static final String ADDRESS = "address";
27+
static final String SERVER_NAME = "server_name";
2728
static final String NUM_SOCKETS_CONNECTED = "num_sockets_connected";
2829
static final String MAX_SOCKET_CONNECTIONS = "max_socket_connections";
2930
private final String address;
31+
private final String serverName;
3032
private final int maxSocketConnections;
3133
private final int numSocketsConnected;
3234

33-
ProxyModeInfo(String address, int maxSocketConnections, int numSocketsConnected) {
35+
ProxyModeInfo(String address, String serverName, int maxSocketConnections, int numSocketsConnected) {
3436
this.address = address;
37+
this.serverName = serverName;
3538
this.maxSocketConnections = maxSocketConnections;
3639
this.numSocketsConnected = numSocketsConnected;
3740
}
@@ -50,6 +53,10 @@ public String getAddress() {
5053
return address;
5154
}
5255

56+
public String getServerName() {
57+
return serverName;
58+
}
59+
5360
public int getMaxSocketConnections() {
5461
return maxSocketConnections;
5562
}
@@ -65,11 +72,12 @@ public boolean equals(Object o) {
6572
ProxyModeInfo otherProxy = (ProxyModeInfo) o;
6673
return maxSocketConnections == otherProxy.maxSocketConnections &&
6774
numSocketsConnected == otherProxy.numSocketsConnected &&
68-
Objects.equals(address, otherProxy.address);
75+
Objects.equals(address, otherProxy.address) &&
76+
Objects.equals(serverName, otherProxy.serverName);
6977
}
7078

7179
@Override
7280
public int hashCode() {
73-
return Objects.hash(address, maxSocketConnections, numSocketsConnected);
81+
return Objects.hash(address, serverName, maxSocketConnections, numSocketsConnected);
7482
}
7583
}

client/rest-high-level/src/main/java/org/elasticsearch/client/cluster/RemoteConnectionInfo.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ public final class RemoteConnectionInfo {
4848
String mode = (String) args[1];
4949
ModeInfo modeInfo;
5050
if (mode.equals(ProxyModeInfo.NAME)) {
51-
modeInfo = new ProxyModeInfo((String) args[4], (int) args[5], (int) args[6]);
51+
modeInfo = new ProxyModeInfo((String) args[4], (String) args[5], (int) args[6], (int) args[7]);
5252
} else if (mode.equals(SniffModeInfo.NAME)) {
53-
modeInfo = new SniffModeInfo((List<String>) args[7], (int) args[8], (int) args[9]);
53+
modeInfo = new SniffModeInfo((List<String>) args[8], (int) args[9], (int) args[10]);
5454
} else {
5555
throw new IllegalArgumentException("mode cannot be " + mode);
5656
}
@@ -67,6 +67,7 @@ public final class RemoteConnectionInfo {
6767
PARSER.declareBoolean(constructorArg(), new ParseField(SKIP_UNAVAILABLE));
6868

6969
PARSER.declareString(optionalConstructorArg(), new ParseField(ProxyModeInfo.ADDRESS));
70+
PARSER.declareString(optionalConstructorArg(), new ParseField(ProxyModeInfo.SERVER_NAME));
7071
PARSER.declareInt(optionalConstructorArg(), new ParseField(ProxyModeInfo.MAX_SOCKET_CONNECTIONS));
7172
PARSER.declareInt(optionalConstructorArg(), new ParseField(ProxyModeInfo.NUM_SOCKETS_CONNECTED));
7273

client/rest-high-level/src/test/java/org/elasticsearch/client/cluster/RemoteInfoResponseTests.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ protected void assertInstances(org.elasticsearch.action.admin.cluster.remote.Rem
8383
ProxyConnectionStrategy.ProxyModeInfo serverModeInfo =
8484
(ProxyConnectionStrategy.ProxyModeInfo) serverRemoteInfo.getModeInfo();
8585
assertThat(clientModeInfo.getAddress(), equalTo(serverModeInfo.getAddress()));
86+
assertThat(clientModeInfo.getServerName(), equalTo(serverModeInfo.getServerName()));
8687
assertThat(clientModeInfo.getMaxSocketConnections(), equalTo(serverModeInfo.getMaxSocketConnections()));
8788
assertThat(clientModeInfo.getNumSocketsConnected(), equalTo(serverModeInfo.getNumSocketsConnected()));
8889
} else {
@@ -95,9 +96,10 @@ private static RemoteConnectionInfo createRandomRemoteConnectionInfo() {
9596
RemoteConnectionInfo.ModeInfo modeInfo;
9697
if (randomBoolean()) {
9798
String address = randomAlphaOfLength(8);
99+
String serverName = randomAlphaOfLength(8);
98100
int maxSocketConnections = randomInt(5);
99101
int numSocketsConnected = randomInt(5);
100-
modeInfo = new ProxyConnectionStrategy.ProxyModeInfo(address, maxSocketConnections, numSocketsConnected);
102+
modeInfo = new ProxyConnectionStrategy.ProxyModeInfo(address, serverName, maxSocketConnections, numSocketsConnected);
101103
} else {
102104
List<String> seedNodes = randomList(randomInt(8), () -> randomAlphaOfLength(8));
103105
int maxConnectionsPerCluster = randomInt(5);

server/src/main/java/org/elasticsearch/transport/ProxyConnectionStrategy.java

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ protected void connectImpl(ActionListener<Void> listener) {
172172

173173
@Override
174174
public RemoteConnectionInfo.ModeInfo getModeInfo() {
175-
return new ProxyModeInfo(configuredAddress, maxNumConnections, connectionManager.size());
175+
return new ProxyModeInfo(configuredAddress, configuredServerName, maxNumConnections, connectionManager.size());
176176
}
177177

178178
private void performProxyConnectionProcess(ActionListener<Void> listener) {
@@ -255,24 +255,32 @@ private static TransportAddress resolveAddress(String address) {
255255
public static class ProxyModeInfo implements RemoteConnectionInfo.ModeInfo {
256256

257257
private final String address;
258+
private final String serverName;
258259
private final int maxSocketConnections;
259260
private final int numSocketsConnected;
260261

261-
public ProxyModeInfo(String address, int maxSocketConnections, int numSocketsConnected) {
262+
public ProxyModeInfo(String address, String serverName, int maxSocketConnections, int numSocketsConnected) {
262263
this.address = address;
264+
this.serverName = serverName;
263265
this.maxSocketConnections = maxSocketConnections;
264266
this.numSocketsConnected = numSocketsConnected;
265267
}
266268

267269
private ProxyModeInfo(StreamInput input) throws IOException {
268270
address = input.readString();
271+
if (input.getVersion().onOrAfter(Version.V_7_7_0)) {
272+
serverName = input.readString();
273+
} else {
274+
serverName = null;
275+
}
269276
maxSocketConnections = input.readVInt();
270277
numSocketsConnected = input.readVInt();
271278
}
272279

273280
@Override
274281
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
275282
builder.field("address", address);
283+
builder.field("server_name", serverName);
276284
builder.field("num_sockets_connected", numSocketsConnected);
277285
builder.field("max_socket_connections", maxSocketConnections);
278286
return builder;
@@ -281,6 +289,9 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
281289
@Override
282290
public void writeTo(StreamOutput out) throws IOException {
283291
out.writeString(address);
292+
if (out.getVersion().onOrAfter(Version.V_7_7_0)) {
293+
out.writeString(serverName);
294+
}
284295
out.writeVInt(maxSocketConnections);
285296
out.writeVInt(numSocketsConnected);
286297
}
@@ -299,6 +310,10 @@ public String getAddress() {
299310
return address;
300311
}
301312

313+
public String getServerName() {
314+
return serverName;
315+
}
316+
302317
public int getMaxSocketConnections() {
303318
return maxSocketConnections;
304319
}
@@ -319,12 +334,13 @@ public boolean equals(Object o) {
319334
ProxyModeInfo otherProxy = (ProxyModeInfo) o;
320335
return maxSocketConnections == otherProxy.maxSocketConnections &&
321336
numSocketsConnected == otherProxy.numSocketsConnected &&
322-
Objects.equals(address, otherProxy.address);
337+
Objects.equals(address, otherProxy.address) &&
338+
Objects.equals(serverName, otherProxy.serverName);
323339
}
324340

325341
@Override
326342
public int hashCode() {
327-
return Objects.hash(address, maxSocketConnections, numSocketsConnected);
343+
return Objects.hash(address, serverName, maxSocketConnections, numSocketsConnected);
328344
}
329345
}
330346
}

server/src/test/java/org/elasticsearch/transport/RemoteClusterConnectionTests.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,7 @@ public void testGetConnectionInfo() throws Exception {
348348

349349
public void testRemoteConnectionInfo() throws IOException {
350350
List<String> remoteAddresses = Collections.singletonList("seed:1");
351+
String serverName = "the_server_name";
351352

352353
RemoteConnectionInfo.ModeInfo modeInfo1;
353354
RemoteConnectionInfo.ModeInfo modeInfo2;
@@ -356,8 +357,8 @@ public void testRemoteConnectionInfo() throws IOException {
356357
modeInfo1 = new SniffConnectionStrategy.SniffModeInfo(remoteAddresses, 4, 4);
357358
modeInfo2 = new SniffConnectionStrategy.SniffModeInfo(remoteAddresses, 4, 3);
358359
} else {
359-
modeInfo1 = new ProxyConnectionStrategy.ProxyModeInfo(remoteAddresses.get(0), 18, 18);
360-
modeInfo2 = new ProxyConnectionStrategy.ProxyModeInfo(remoteAddresses.get(0), 18, 17);
360+
modeInfo1 = new ProxyConnectionStrategy.ProxyModeInfo(remoteAddresses.get(0), serverName, 18, 18);
361+
modeInfo2 = new ProxyConnectionStrategy.ProxyModeInfo(remoteAddresses.get(0), serverName,18, 17);
361362
}
362363

363364
RemoteConnectionInfo stats =
@@ -425,14 +426,15 @@ public void testRemoteConnectionInfoBwComp() throws IOException {
425426

426427
public void testRenderConnectionInfoXContent() throws IOException {
427428
List<String> remoteAddresses = Arrays.asList("seed:1", "seed:2");
429+
String serverName = "the_server_name";
428430

429431
RemoteConnectionInfo.ModeInfo modeInfo;
430432

431433
boolean sniff = randomBoolean();
432434
if (sniff) {
433435
modeInfo = new SniffConnectionStrategy.SniffModeInfo(remoteAddresses, 3, 2);
434436
} else {
435-
modeInfo = new ProxyConnectionStrategy.ProxyModeInfo(remoteAddresses.get(0), 18, 16);
437+
modeInfo = new ProxyConnectionStrategy.ProxyModeInfo(remoteAddresses.get(0), serverName,18, 16);
436438
}
437439

438440
RemoteConnectionInfo stats = new RemoteConnectionInfo("test_cluster", modeInfo, TimeValue.timeValueMinutes(30), true);
@@ -448,8 +450,8 @@ public void testRenderConnectionInfoXContent() throws IOException {
448450
"\"skip_unavailable\":true}}", Strings.toString(builder));
449451
} else {
450452
assertEquals("{\"test_cluster\":{\"connected\":true,\"mode\":\"proxy\",\"address\":\"seed:1\"," +
451-
"\"num_sockets_connected\":16,\"max_socket_connections\":18,\"initial_connect_timeout\":\"30m\"," +
452-
"\"skip_unavailable\":true}}", Strings.toString(builder));
453+
"\"server_name\":\"the_server_name\",\"num_sockets_connected\":16,\"max_socket_connections\":18,"
454+
+"\"initial_connect_timeout\":\"30m\",\"skip_unavailable\":true}}", Strings.toString(builder));
453455
}
454456
}
455457

0 commit comments

Comments
 (0)