Search in sources :

Example 91 with TransportAddress

use of org.elasticsearch.common.transport.TransportAddress in project elasticsearch by elastic.

the class ESIntegTestCase method buildExternalCluster.

private ExternalTestCluster buildExternalCluster(String clusterAddresses) throws IOException {
    String[] stringAddresses = clusterAddresses.split(",");
    TransportAddress[] transportAddresses = new TransportAddress[stringAddresses.length];
    int i = 0;
    for (String stringAddress : stringAddresses) {
        URL url = new URL("http://" + stringAddress);
        InetAddress inetAddress = InetAddress.getByName(url.getHost());
        transportAddresses[i++] = new TransportAddress(new InetSocketAddress(inetAddress, url.getPort()));
    }
    return new ExternalTestCluster(createTempDir(), externalClusterClientSettings(), transportClientPlugins(), transportAddresses);
}
Also used : TransportAddress(org.elasticsearch.common.transport.TransportAddress) InetSocketAddress(java.net.InetSocketAddress) InetAddress(java.net.InetAddress) URL(java.net.URL)

Example 92 with TransportAddress

use of org.elasticsearch.common.transport.TransportAddress in project elasticsearch by elastic.

the class ExceptionSerializationTests method testActionTransportException.

public void testActionTransportException() throws IOException {
    TransportAddress transportAddress = buildNewFakeTransportAddress();
    ActionTransportException ex = serialize(new ActionTransportException("name?", transportAddress, "ACTION BABY!", "message?", null));
    assertEquals("ACTION BABY!", ex.action());
    assertEquals(transportAddress, ex.address());
    assertEquals("[name?][" + transportAddress.toString() + "][ACTION BABY!] message?", ex.getMessage());
}
Also used : TransportAddress(org.elasticsearch.common.transport.TransportAddress) ActionTransportException(org.elasticsearch.transport.ActionTransportException)

Example 93 with TransportAddress

use of org.elasticsearch.common.transport.TransportAddress in project elasticsearch by elastic.

the class ExceptionSerializationTests method testConnectTransportException.

public void testConnectTransportException() throws IOException {
    TransportAddress transportAddress = buildNewFakeTransportAddress();
    DiscoveryNode node = new DiscoveryNode("thenode", transportAddress, emptyMap(), emptySet(), Version.CURRENT);
    ConnectTransportException ex = serialize(new ConnectTransportException(node, "msg", "action", null));
    assertEquals("[][" + transportAddress.toString() + "][action] msg", ex.getMessage());
    assertEquals(node, ex.node());
    assertEquals("action", ex.action());
    assertNull(ex.getCause());
    ex = serialize(new ConnectTransportException(node, "msg", "action", new NullPointerException()));
    assertEquals("[][" + transportAddress + "][action] msg", ex.getMessage());
    assertEquals(node, ex.node());
    assertEquals("action", ex.action());
    assertTrue(ex.getCause() instanceof NullPointerException);
}
Also used : DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) ConnectTransportException(org.elasticsearch.transport.ConnectTransportException) TransportAddress(org.elasticsearch.common.transport.TransportAddress)

Example 94 with TransportAddress

use of org.elasticsearch.common.transport.TransportAddress in project elasticsearch by elastic.

the class DiscoveryNodeTests method testDiscoveryNodeIsCreatedWithHostFromInetAddress.

public void testDiscoveryNodeIsCreatedWithHostFromInetAddress() throws Exception {
    InetAddress inetAddress = randomBoolean() ? InetAddress.getByName("192.0.2.1") : InetAddress.getByAddress("name1", new byte[] { (byte) 192, (byte) 168, (byte) 0, (byte) 1 });
    TransportAddress transportAddress = new TransportAddress(inetAddress, randomIntBetween(0, 65535));
    DiscoveryNode node = new DiscoveryNode("name1", "id1", transportAddress, emptyMap(), emptySet(), Version.CURRENT);
    assertEquals(transportAddress.address().getHostString(), node.getHostName());
    assertEquals(transportAddress.getAddress(), node.getHostAddress());
}
Also used : TransportAddress(org.elasticsearch.common.transport.TransportAddress) InetAddress(java.net.InetAddress)

Example 95 with TransportAddress

use of org.elasticsearch.common.transport.TransportAddress in project elasticsearch by elastic.

the class GceUnicastHostsProvider method buildDynamicNodes.

/**
     * We build the list of Nodes from GCE Management API
     * Information can be cached using `cloud.gce.refresh_interval` property if needed.
     */
@Override
public List<DiscoveryNode> buildDynamicNodes() {
    // We check that needed properties have been set
    if (this.project == null || this.project.isEmpty() || this.zones == null || this.zones.isEmpty()) {
        throw new IllegalArgumentException("one or more gce discovery settings are missing. " + "Check elasticsearch.yml file. Should have [" + GceInstancesService.PROJECT_SETTING.getKey() + "] and [" + GceInstancesService.ZONE_SETTING.getKey() + "].");
    }
    if (refreshInterval.millis() != 0) {
        if (cachedDiscoNodes != null && (refreshInterval.millis() < 0 || (System.currentTimeMillis() - lastRefresh) < refreshInterval.millis())) {
            if (logger.isTraceEnabled())
                logger.trace("using cache to retrieve node list");
            return cachedDiscoNodes;
        }
        lastRefresh = System.currentTimeMillis();
    }
    logger.debug("start building nodes list using GCE API");
    cachedDiscoNodes = new ArrayList<>();
    String ipAddress = null;
    try {
        InetAddress inetAddress = networkService.resolvePublishHostAddresses(null);
        if (inetAddress != null) {
            ipAddress = NetworkAddress.format(inetAddress);
        }
    } catch (IOException e) {
    // We can't find the publish host address... Hmmm. Too bad :-(
    // We won't simply filter it
    }
    try {
        Collection<Instance> instances = gceInstancesService.instances();
        if (instances == null) {
            logger.trace("no instance found for project [{}], zones [{}].", this.project, this.zones);
            return cachedDiscoNodes;
        }
        for (Instance instance : instances) {
            String name = instance.getName();
            String type = instance.getMachineType();
            String status = instance.getStatus();
            logger.trace("gce instance {} with status {} found.", name, status);
            // See https://github.com/elastic/elasticsearch-cloud-gce/issues/3
            if (Status.TERMINATED.equals(status)) {
                logger.debug("node {} is TERMINATED. Ignoring", name);
                continue;
            }
            // see if we need to filter by tag
            boolean filterByTag = false;
            if (tags.isEmpty() == false) {
                logger.trace("start filtering instance {} with tags {}.", name, tags);
                if (instance.getTags() == null || instance.getTags().isEmpty() || instance.getTags().getItems() == null || instance.getTags().getItems().isEmpty()) {
                    // If this instance have no tag, we filter it
                    logger.trace("no tags for this instance but we asked for tags. {} won't be part of the cluster.", name);
                    filterByTag = true;
                } else {
                    // check that all tags listed are there on the instance
                    logger.trace("comparing instance tags {} with tags filter {}.", instance.getTags().getItems(), tags);
                    for (String tag : tags) {
                        boolean found = false;
                        for (String instancetag : instance.getTags().getItems()) {
                            if (instancetag.equals(tag)) {
                                found = true;
                                break;
                            }
                        }
                        if (!found) {
                            filterByTag = true;
                            break;
                        }
                    }
                }
            }
            if (filterByTag) {
                logger.trace("filtering out instance {} based tags {}, not part of {}", name, tags, instance.getTags() == null || instance.getTags().getItems() == null ? "" : instance.getTags());
                continue;
            } else {
                logger.trace("instance {} with tags {} is added to discovery", name, tags);
            }
            String ip_public = null;
            String ip_private = null;
            List<NetworkInterface> interfaces = instance.getNetworkInterfaces();
            for (NetworkInterface networkInterface : interfaces) {
                if (ip_public == null) {
                    // Trying to get Public IP Address (For future use)
                    if (networkInterface.getAccessConfigs() != null) {
                        for (AccessConfig accessConfig : networkInterface.getAccessConfigs()) {
                            if (Strings.hasText(accessConfig.getNatIP())) {
                                ip_public = accessConfig.getNatIP();
                                break;
                            }
                        }
                    }
                }
                if (ip_private == null) {
                    ip_private = networkInterface.getNetworkIP();
                }
                // If we have both public and private, we can stop here
                if (ip_private != null && ip_public != null)
                    break;
            }
            try {
                if (ip_private.equals(ipAddress)) {
                    // We found the current node.
                    // We can ignore it in the list of DiscoveryNode
                    logger.trace("current node found. Ignoring {} - {}", name, ip_private);
                } else {
                    String address = ip_private;
                    // Test if we have es_port metadata defined here
                    if (instance.getMetadata() != null && instance.getMetadata().containsKey("es_port")) {
                        Object es_port = instance.getMetadata().get("es_port");
                        logger.trace("es_port is defined with {}", es_port);
                        if (es_port instanceof String) {
                            address = address.concat(":").concat((String) es_port);
                        } else {
                            // Ignoring other values
                            logger.trace("es_port is instance of {}. Ignoring...", es_port.getClass().getName());
                        }
                    }
                    // ip_private is a single IP Address. We need to build a TransportAddress from it
                    // If user has set `es_port` metadata, we don't need to ping all ports
                    // we only limit to 1 addresses, makes no sense to ping 100 ports
                    TransportAddress[] addresses = transportService.addressesFromString(address, 1);
                    for (TransportAddress transportAddress : addresses) {
                        logger.trace("adding {}, type {}, address {}, transport_address {}, status {}", name, type, ip_private, transportAddress, status);
                        cachedDiscoNodes.add(new DiscoveryNode("#cloud-" + name + "-" + 0, transportAddress, emptyMap(), emptySet(), Version.CURRENT.minimumCompatibilityVersion()));
                    }
                }
            } catch (Exception e) {
                final String finalIpPrivate = ip_private;
                logger.warn((Supplier<?>) () -> new ParameterizedMessage("failed to add {}, address {}", name, finalIpPrivate), e);
            }
        }
    } catch (Exception e) {
        logger.warn("exception caught during discovery", e);
    }
    logger.debug("{} node(s) added", cachedDiscoNodes.size());
    logger.debug("using dynamic discovery nodes {}", cachedDiscoNodes);
    return cachedDiscoNodes;
}
Also used : DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) Instance(com.google.api.services.compute.model.Instance) TransportAddress(org.elasticsearch.common.transport.TransportAddress) NetworkInterface(com.google.api.services.compute.model.NetworkInterface) IOException(java.io.IOException) AccessConfig(com.google.api.services.compute.model.AccessConfig) IOException(java.io.IOException) Supplier(org.apache.logging.log4j.util.Supplier) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) InetAddress(java.net.InetAddress)

Aggregations

TransportAddress (org.elasticsearch.common.transport.TransportAddress)129 Settings (org.elasticsearch.common.settings.Settings)46 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)45 BoundTransportAddress (org.elasticsearch.common.transport.BoundTransportAddress)33 ArrayList (java.util.ArrayList)31 IOException (java.io.IOException)30 InetSocketAddress (java.net.InetSocketAddress)28 InetAddress (java.net.InetAddress)22 PreBuiltTransportClient (org.elasticsearch.transport.client.PreBuiltTransportClient)20 HashSet (java.util.HashSet)16 UnknownHostException (java.net.UnknownHostException)15 TransportClient (org.elasticsearch.client.transport.TransportClient)15 TransportService (org.elasticsearch.transport.TransportService)14 NamedWriteableRegistry (org.elasticsearch.common.io.stream.NamedWriteableRegistry)13 AtomicReference (java.util.concurrent.atomic.AtomicReference)12 ClusterState (org.elasticsearch.cluster.ClusterState)12 NetworkService (org.elasticsearch.common.network.NetworkService)12 ThreadPool (org.elasticsearch.threadpool.ThreadPool)12 HashMap (java.util.HashMap)11 List (java.util.List)11