Skip to content

Commit

Permalink
Support es7 node http publish_address format (#49279)
Browse files Browse the repository at this point in the history
Add parsing support to node http publish_address format cname/ip:port.
  • Loading branch information
zacharymorn authored and jbaiera committed Dec 4, 2019
1 parent 72dd49d commit bbbdf94
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,21 @@ private static Node readNode(String nodeId, JsonParser parser, Scheme scheme) th
if ("http".equals(fieldName)) {
while (parser.nextToken() != JsonToken.END_OBJECT) {
if (parser.getCurrentToken() == JsonToken.VALUE_STRING && "publish_address".equals(parser.getCurrentName())) {
URI publishAddressAsURI = URI.create(scheme + "://" + parser.getValueAsString());
publishedHost = new HttpHost(publishAddressAsURI.getHost(), publishAddressAsURI.getPort(),
publishAddressAsURI.getScheme());
String address = parser.getValueAsString();
String host;
URI publishAddressAsURI;

// ES7 cname/ip:port format
if(address.contains("/")) {
String[] cnameAndURI = address.split("/", 2);
publishAddressAsURI = URI.create(scheme + "://" + cnameAndURI[1]);
host = cnameAndURI[0];
}
else {
publishAddressAsURI = URI.create(scheme + "://" + address);
host = publishAddressAsURI.getHost();
}
publishedHost = new HttpHost(host, publishAddressAsURI.getPort(), publishAddressAsURI.getScheme());
} else if (parser.currentToken() == JsonToken.START_ARRAY && "bound_address".equals(parser.getCurrentName())) {
while (parser.nextToken() != JsonToken.END_ARRAY) {
URI boundAddressAsURI = URI.create(scheme + "://" + parser.getValueAsString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,28 @@ public void test6x() throws IOException {
node(9207, "c2", "6.0.0", false, false, true));
}

public void testParsingPublishAddressWithPreES7Format() throws IOException {
InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("es6_nodes_publication_address_format.json");

HttpEntity entity = new InputStreamEntity(in, ContentType.APPLICATION_JSON);
List<Node> nodes = ElasticsearchNodesSniffer.readHosts(entity, Scheme.HTTP, new JsonFactory());

assertEquals("127.0.0.1", nodes.get(0).getHost().getHostName());
assertEquals(9200, nodes.get(0).getHost().getPort());
assertEquals("http", nodes.get(0).getHost().getSchemeName());
}

public void testParsingPublishAddressWithES7Format() throws IOException {
InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("es7_nodes_publication_address_format.json");

HttpEntity entity = new InputStreamEntity(in, ContentType.APPLICATION_JSON);
List<Node> nodes = ElasticsearchNodesSniffer.readHosts(entity, Scheme.HTTP, new JsonFactory());

assertEquals("elastic.test", nodes.get(0).getHost().getHostName());
assertEquals(9200, nodes.get(0).getHost().getPort());
assertEquals("http", nodes.get(0).getHost().getSchemeName());
}

private Node node(int port, String name, String version, boolean master, boolean data, boolean ingest) {
HttpHost host = new HttpHost("127.0.0.1", port);
Set<HttpHost> boundHosts = new HashSet<>(2);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"_nodes": {
"total": 8,
"successful": 8,
"failed": 0
},
"cluster_name": "elasticsearch",
"nodes": {
"ikXK_skVTfWkhONhldnbkw": {
"name": "m1",
"transport_address": "127.0.0.1:9300",
"host": "127.0.0.1",
"ip": "127.0.0.1",
"version": "6.0.0",
"build_hash": "8f0685b",
"roles": [
"master",
"ingest"
],
"attributes": { },
"http": {
"bound_address": [
"127.0.0.1:9200"
],
"publish_address": "127.0.0.1:9200",
"max_content_length_in_bytes": 104857600
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"_nodes": {
"total": 8,
"successful": 8,
"failed": 0
},
"cluster_name": "elasticsearch",
"nodes": {
"ikXK_skVTfWkhONhldnbkw": {
"name": "m1",
"transport_address": "127.0.0.1:9300",
"host": "127.0.0.1",
"ip": "127.0.0.1",
"version": "6.0.0",
"build_hash": "8f0685b",
"roles": [
"master",
"ingest"
],
"attributes": { },
"http": {
"bound_address": [
"elastic.test:9200"
],
"publish_address": "elastic.test/127.0.0.1:9200",
"max_content_length_in_bytes": 104857600
}
}
}
}

0 comments on commit bbbdf94

Please sign in to comment.