Search in sources :

Example 1 with Node

use of org.opensearch.client.Node in project OpenSearch by opensearch-project.

the class OpenSearchNodesSniffer method readHosts.

static List<Node> readHosts(HttpEntity entity, Scheme scheme, JsonFactory jsonFactory) throws IOException {
    try (InputStream inputStream = entity.getContent()) {
        JsonParser parser = jsonFactory.createParser(inputStream);
        if (parser.nextToken() != JsonToken.START_OBJECT) {
            throw new IOException("expected data to start with an object");
        }
        List<Node> nodes = new ArrayList<>();
        while (parser.nextToken() != JsonToken.END_OBJECT) {
            if (parser.getCurrentToken() == JsonToken.START_OBJECT) {
                if ("nodes".equals(parser.getCurrentName())) {
                    while (parser.nextToken() != JsonToken.END_OBJECT) {
                        JsonToken token = parser.nextToken();
                        assert token == JsonToken.START_OBJECT;
                        String nodeId = parser.getCurrentName();
                        Node node = readNode(nodeId, parser, scheme);
                        if (node != null) {
                            nodes.add(node);
                        }
                    }
                } else {
                    parser.skipChildren();
                }
            }
        }
        return nodes;
    }
}
Also used : InputStream(java.io.InputStream) Node(org.opensearch.client.Node) ArrayList(java.util.ArrayList) JsonToken(com.fasterxml.jackson.core.JsonToken) IOException(java.io.IOException) JsonParser(com.fasterxml.jackson.core.JsonParser)

Example 2 with Node

use of org.opensearch.client.Node in project OpenSearch by opensearch-project.

the class OpenSearchNodesSniffer method readNode.

private static Node readNode(String nodeId, JsonParser parser, Scheme scheme) throws IOException {
    HttpHost publishedHost = null;
    /*
         * We sniff the bound hosts so we can look up the node based on any
         * address on which it is listening. This is useful in OpenSearch's
         * test framework where we sometimes publish ipv6 addresses but the
         * tests contact the node on ipv4.
         */
    Set<HttpHost> boundHosts = new HashSet<>();
    String name = null;
    String version = null;
    /*
         * Multi-valued attributes come with key = `real_key.index` and we
         * unflip them after reading them because we can't rely on the order
         * that they arive.
         */
    final Map<String, String> protoAttributes = new HashMap<String, String>();
    boolean sawRoles = false;
    final Set<String> roles = new TreeSet<>();
    String fieldName = null;
    while (parser.nextToken() != JsonToken.END_OBJECT) {
        if (parser.getCurrentToken() == JsonToken.FIELD_NAME) {
            fieldName = parser.getCurrentName();
        } else if (parser.getCurrentToken() == JsonToken.START_OBJECT) {
            if ("http".equals(fieldName)) {
                while (parser.nextToken() != JsonToken.END_OBJECT) {
                    if (parser.getCurrentToken() == JsonToken.VALUE_STRING && "publish_address".equals(parser.getCurrentName())) {
                        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());
                            boundHosts.add(new HttpHost(boundAddressAsURI.getHost(), boundAddressAsURI.getPort(), boundAddressAsURI.getScheme()));
                        }
                    } else if (parser.getCurrentToken() == JsonToken.START_OBJECT) {
                        parser.skipChildren();
                    }
                }
            } else if ("attributes".equals(fieldName)) {
                while (parser.nextToken() != JsonToken.END_OBJECT) {
                    if (parser.getCurrentToken() == JsonToken.VALUE_STRING) {
                        String oldValue = protoAttributes.put(parser.getCurrentName(), parser.getValueAsString());
                        if (oldValue != null) {
                            throw new IOException("repeated attribute key [" + parser.getCurrentName() + "]");
                        }
                    } else {
                        parser.skipChildren();
                    }
                }
            } else {
                parser.skipChildren();
            }
        } else if (parser.currentToken() == JsonToken.START_ARRAY) {
            if ("roles".equals(fieldName)) {
                sawRoles = true;
                while (parser.nextToken() != JsonToken.END_ARRAY) {
                    roles.add(parser.getText());
                }
            } else {
                parser.skipChildren();
            }
        } else if (parser.currentToken().isScalarValue()) {
            if ("version".equals(fieldName)) {
                version = parser.getText();
            } else if ("name".equals(fieldName)) {
                name = parser.getText();
            }
        }
    }
    // http section is not present if http is not enabled on the node, ignore such nodes
    if (publishedHost == null) {
        logger.debug("skipping node [" + nodeId + "] with http disabled");
        return null;
    }
    Map<String, List<String>> realAttributes = new HashMap<>(protoAttributes.size());
    List<String> keys = new ArrayList<>(protoAttributes.keySet());
    for (String key : keys) {
        if (key.endsWith(".0")) {
            String realKey = key.substring(0, key.length() - 2);
            List<String> values = new ArrayList<>();
            int i = 0;
            while (true) {
                String value = protoAttributes.remove(realKey + "." + i);
                if (value == null) {
                    break;
                }
                values.add(value);
                i++;
            }
            realAttributes.put(realKey, unmodifiableList(values));
        }
    }
    for (Map.Entry<String, String> entry : protoAttributes.entrySet()) {
        realAttributes.put(entry.getKey(), singletonList(entry.getValue()));
    }
    if (version.startsWith("2.")) {
        /*
             * 2.x doesn't send roles, instead we try to read them from
             * attributes.
             */
        boolean clientAttribute = v2RoleAttributeValue(realAttributes, "client", false);
        Boolean masterAttribute = v2RoleAttributeValue(realAttributes, "master", null);
        Boolean dataAttribute = v2RoleAttributeValue(realAttributes, "data", null);
        if ((masterAttribute == null && false == clientAttribute) || masterAttribute) {
            roles.add("master");
        }
        if ((dataAttribute == null && false == clientAttribute) || dataAttribute) {
            roles.add("data");
        }
    } else {
        assert sawRoles : "didn't see roles for [" + nodeId + "]";
    }
    assert boundHosts.contains(publishedHost) : "[" + nodeId + "] doesn't make sense! publishedHost should be in boundHosts";
    logger.trace("adding node [" + nodeId + "]");
    return new Node(publishedHost, boundHosts, name, version, new Roles(roles), unmodifiableMap(realAttributes));
}
Also used : HashMap(java.util.HashMap) Node(org.opensearch.client.Node) ArrayList(java.util.ArrayList) Roles(org.opensearch.client.Node.Roles) IOException(java.io.IOException) URI(java.net.URI) HttpHost(org.apache.http.HttpHost) TreeSet(java.util.TreeSet) Collections.unmodifiableList(java.util.Collections.unmodifiableList) ArrayList(java.util.ArrayList) Collections.singletonList(java.util.Collections.singletonList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) Collections.unmodifiableMap(java.util.Collections.unmodifiableMap) HashSet(java.util.HashSet)

Example 3 with Node

use of org.opensearch.client.Node in project OpenSearch by opensearch-project.

the class OpenSearchNodesSnifferTests method testSniffNodes.

public void testSniffNodes() throws IOException {
    HttpHost httpHost = new HttpHost(httpServer.getAddress().getHostString(), httpServer.getAddress().getPort());
    try (RestClient restClient = RestClient.builder(httpHost).build()) {
        OpenSearchNodesSniffer sniffer = new OpenSearchNodesSniffer(restClient, sniffRequestTimeout, scheme);
        try {
            List<Node> sniffedNodes = sniffer.sniff();
            if (sniffResponse.isFailure) {
                fail("sniffNodes should have failed");
            }
            assertEquals(sniffResponse.result, sniffedNodes);
        } catch (ResponseException e) {
            Response response = e.getResponse();
            if (sniffResponse.isFailure) {
                final String errorPrefix = "method [GET], host [" + httpHost + "], URI [/_nodes/http?timeout=" + sniffRequestTimeout + "ms], status line [HTTP/1.1";
                assertThat(e.getMessage(), startsWith(errorPrefix));
                assertThat(e.getMessage(), containsString(Integer.toString(sniffResponse.nodesInfoResponseCode)));
                assertThat(response.getHost(), equalTo(httpHost));
                assertThat(response.getStatusLine().getStatusCode(), equalTo(sniffResponse.nodesInfoResponseCode));
                assertThat(response.getRequestLine().toString(), equalTo("GET /_nodes/http?timeout=" + sniffRequestTimeout + "ms HTTP/1.1"));
            } else {
                fail("sniffNodes should have succeeded: " + response.getStatusLine());
            }
        }
    }
}
Also used : Response(org.opensearch.client.Response) ResponseException(org.opensearch.client.ResponseException) HttpHost(org.apache.http.HttpHost) Node(org.opensearch.client.Node) RestClient(org.opensearch.client.RestClient) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString)

Example 4 with Node

use of org.opensearch.client.Node in project OpenSearch by opensearch-project.

the class OpenSearchNodesSnifferParseTests method node.

private Node node(int port, String name, String version, Set<String> roles) {
    HttpHost host = new HttpHost("127.0.0.1", port);
    Set<HttpHost> boundHosts = new HashSet<>(2);
    boundHosts.add(host);
    boundHosts.add(new HttpHost("[::1]", port));
    Map<String, List<String>> attributes = new HashMap<>();
    attributes.put("dummy", singletonList("everyone_has_me"));
    attributes.put("number", singletonList(name.substring(1)));
    attributes.put("array", Arrays.asList(name.substring(0, 1), name.substring(1)));
    return new Node(host, boundHosts, name, version, new Roles(new TreeSet<>(roles)), attributes);
}
Also used : HashMap(java.util.HashMap) HttpHost(org.apache.http.HttpHost) TreeSet(java.util.TreeSet) Node(org.opensearch.client.Node) Collections.singletonList(java.util.Collections.singletonList) List(java.util.List) Roles(org.opensearch.client.Node.Roles) HashSet(java.util.HashSet)

Example 5 with Node

use of org.opensearch.client.Node in project OpenSearch by opensearch-project.

the class OpenSearchNodesSnifferParseTests method testParsingPublishAddressWithPreES7Format.

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 = OpenSearchNodesSniffer.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());
}
Also used : HttpEntity(org.apache.http.HttpEntity) InputStream(java.io.InputStream) Node(org.opensearch.client.Node) JsonFactory(com.fasterxml.jackson.core.JsonFactory) InputStreamEntity(org.apache.http.entity.InputStreamEntity)

Aggregations

Node (org.opensearch.client.Node)20 HttpHost (org.apache.http.HttpHost)9 IOException (java.io.IOException)7 ArrayList (java.util.ArrayList)7 List (java.util.List)7 Response (org.opensearch.client.Response)5 RestClient (org.opensearch.client.RestClient)5 JsonFactory (com.fasterxml.jackson.core.JsonFactory)4 InputStream (java.io.InputStream)4 Collections.singletonList (java.util.Collections.singletonList)4 HashMap (java.util.HashMap)4 HashSet (java.util.HashSet)3 TreeSet (java.util.TreeSet)3 HttpEntity (org.apache.http.HttpEntity)3 InputStreamEntity (org.apache.http.entity.InputStreamEntity)3 NodeSelector (org.opensearch.client.NodeSelector)3 Request (org.opensearch.client.Request)3 Collections.emptyList (java.util.Collections.emptyList)2 Map (java.util.Map)2 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)2