Skip to content

Commit 9a1bde2

Browse files
authored
Support es7 node http publish_address format (#49279) (#49843)
Add parsing support to node http publish_address format cname/ip:port.
1 parent 9bf6072 commit 9a1bde2

File tree

4 files changed

+97
-3
lines changed

4 files changed

+97
-3
lines changed

client/sniffer/src/main/java/org/elasticsearch/client/sniff/ElasticsearchNodesSniffer.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,9 +164,21 @@ private static Node readNode(String nodeId, JsonParser parser, Scheme scheme) th
164164
if ("http".equals(fieldName)) {
165165
while (parser.nextToken() != JsonToken.END_OBJECT) {
166166
if (parser.getCurrentToken() == JsonToken.VALUE_STRING && "publish_address".equals(parser.getCurrentName())) {
167-
URI publishAddressAsURI = URI.create(scheme + "://" + parser.getValueAsString());
168-
publishedHost = new HttpHost(publishAddressAsURI.getHost(), publishAddressAsURI.getPort(),
169-
publishAddressAsURI.getScheme());
167+
String address = parser.getValueAsString();
168+
String host;
169+
URI publishAddressAsURI;
170+
171+
// ES7 cname/ip:port format
172+
if(address.contains("/")) {
173+
String[] cnameAndURI = address.split("/", 2);
174+
publishAddressAsURI = URI.create(scheme + "://" + cnameAndURI[1]);
175+
host = cnameAndURI[0];
176+
}
177+
else {
178+
publishAddressAsURI = URI.create(scheme + "://" + address);
179+
host = publishAddressAsURI.getHost();
180+
}
181+
publishedHost = new HttpHost(host, publishAddressAsURI.getPort(), publishAddressAsURI.getScheme());
170182
} else if (parser.currentToken() == JsonToken.START_ARRAY && "bound_address".equals(parser.getCurrentName())) {
171183
while (parser.nextToken() != JsonToken.END_ARRAY) {
172184
URI boundAddressAsURI = URI.create(scheme + "://" + parser.getValueAsString());

client/sniffer/src/test/java/org/elasticsearch/client/sniff/ElasticsearchNodesSnifferParseTests.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,28 @@ public void test6x() throws IOException {
107107
node(9207, "c2", "6.0.0", false, false, true));
108108
}
109109

110+
public void testParsingPublishAddressWithPreES7Format() throws IOException {
111+
InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("es6_nodes_publication_address_format.json");
112+
113+
HttpEntity entity = new InputStreamEntity(in, ContentType.APPLICATION_JSON);
114+
List<Node> nodes = ElasticsearchNodesSniffer.readHosts(entity, Scheme.HTTP, new JsonFactory());
115+
116+
assertEquals("127.0.0.1", nodes.get(0).getHost().getHostName());
117+
assertEquals(9200, nodes.get(0).getHost().getPort());
118+
assertEquals("http", nodes.get(0).getHost().getSchemeName());
119+
}
120+
121+
public void testParsingPublishAddressWithES7Format() throws IOException {
122+
InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("es7_nodes_publication_address_format.json");
123+
124+
HttpEntity entity = new InputStreamEntity(in, ContentType.APPLICATION_JSON);
125+
List<Node> nodes = ElasticsearchNodesSniffer.readHosts(entity, Scheme.HTTP, new JsonFactory());
126+
127+
assertEquals("elastic.test", nodes.get(0).getHost().getHostName());
128+
assertEquals(9200, nodes.get(0).getHost().getPort());
129+
assertEquals("http", nodes.get(0).getHost().getSchemeName());
130+
}
131+
110132
private Node node(int port, String name, String version, boolean master, boolean data, boolean ingest) {
111133
HttpHost host = new HttpHost("127.0.0.1", port);
112134
Set<HttpHost> boundHosts = new HashSet<>(2);
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"_nodes": {
3+
"total": 8,
4+
"successful": 8,
5+
"failed": 0
6+
},
7+
"cluster_name": "elasticsearch",
8+
"nodes": {
9+
"ikXK_skVTfWkhONhldnbkw": {
10+
"name": "m1",
11+
"transport_address": "127.0.0.1:9300",
12+
"host": "127.0.0.1",
13+
"ip": "127.0.0.1",
14+
"version": "6.0.0",
15+
"build_hash": "8f0685b",
16+
"roles": [
17+
"master",
18+
"ingest"
19+
],
20+
"attributes": { },
21+
"http": {
22+
"bound_address": [
23+
"127.0.0.1:9200"
24+
],
25+
"publish_address": "127.0.0.1:9200",
26+
"max_content_length_in_bytes": 104857600
27+
}
28+
}
29+
}
30+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"_nodes": {
3+
"total": 8,
4+
"successful": 8,
5+
"failed": 0
6+
},
7+
"cluster_name": "elasticsearch",
8+
"nodes": {
9+
"ikXK_skVTfWkhONhldnbkw": {
10+
"name": "m1",
11+
"transport_address": "127.0.0.1:9300",
12+
"host": "127.0.0.1",
13+
"ip": "127.0.0.1",
14+
"version": "6.0.0",
15+
"build_hash": "8f0685b",
16+
"roles": [
17+
"master",
18+
"ingest"
19+
],
20+
"attributes": { },
21+
"http": {
22+
"bound_address": [
23+
"elastic.test:9200"
24+
],
25+
"publish_address": "elastic.test/127.0.0.1:9200",
26+
"max_content_length_in_bytes": 104857600
27+
}
28+
}
29+
}
30+
}

0 commit comments

Comments
 (0)