Search in sources :

Example 16 with TransportAddress

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

the class TransportClientNodesService method addTransportAddresses.

public TransportClientNodesService addTransportAddresses(TransportAddress... transportAddresses) {
    synchronized (mutex) {
        if (closed) {
            throw new IllegalStateException("transport client is closed, can't add an address");
        }
        List<TransportAddress> filtered = new ArrayList<>(transportAddresses.length);
        for (TransportAddress transportAddress : transportAddresses) {
            boolean found = false;
            for (DiscoveryNode otherNode : listedNodes) {
                if (otherNode.getAddress().equals(transportAddress)) {
                    found = true;
                    logger.debug("address [{}] already exists with [{}], ignoring...", transportAddress, otherNode);
                    break;
                }
            }
            if (!found) {
                filtered.add(transportAddress);
            }
        }
        if (filtered.isEmpty()) {
            return this;
        }
        List<DiscoveryNode> builder = new ArrayList<>();
        builder.addAll(listedNodes());
        for (TransportAddress transportAddress : filtered) {
            DiscoveryNode node = new DiscoveryNode("#transport#-" + tempNodeIdGenerator.incrementAndGet(), transportAddress, Collections.emptyMap(), Collections.emptySet(), minCompatibilityVersion);
            logger.debug("adding address [{}]", node);
            builder.add(node);
        }
        listedNodes = Collections.unmodifiableList(builder);
        nodesSampler.sample();
    }
    return this;
}
Also used : DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) TransportAddress(org.elasticsearch.common.transport.TransportAddress) ArrayList(java.util.ArrayList)

Example 17 with TransportAddress

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

the class UnicastZenPing method sendPings.

protected void sendPings(final TimeValue timeout, final PingingRound pingingRound) {
    final UnicastPingRequest pingRequest = new UnicastPingRequest();
    pingRequest.id = pingingRound.id();
    pingRequest.timeout = timeout;
    DiscoveryNodes discoNodes = contextProvider.nodes();
    pingRequest.pingResponse = createPingResponse(discoNodes);
    Set<DiscoveryNode> nodesFromResponses = temporalResponses.stream().map(pingResponse -> {
        assert clusterName.equals(pingResponse.clusterName()) : "got a ping request from a different cluster. expected " + clusterName + " got " + pingResponse.clusterName();
        return pingResponse.node();
    }).collect(Collectors.toSet());
    // dedup by address
    final Map<TransportAddress, DiscoveryNode> uniqueNodesByAddress = Stream.concat(pingingRound.getSeedNodes().stream(), nodesFromResponses.stream()).collect(Collectors.toMap(DiscoveryNode::getAddress, Function.identity(), (n1, n2) -> n1));
    // resolve what we can via the latest cluster state
    final Set<DiscoveryNode> nodesToPing = uniqueNodesByAddress.values().stream().map(node -> {
        DiscoveryNode foundNode = discoNodes.findByAddress(node.getAddress());
        if (foundNode == null) {
            return node;
        } else {
            return foundNode;
        }
    }).collect(Collectors.toSet());
    nodesToPing.forEach(node -> sendPingRequestToNode(node, timeout, pingingRound, pingRequest));
}
Also used : StreamOutput(org.elasticsearch.common.io.stream.StreamOutput) Arrays(java.util.Arrays) TransportRequest(org.elasticsearch.transport.TransportRequest) Releasables(org.elasticsearch.common.lease.Releasables) Property(org.elasticsearch.common.settings.Setting.Property) ConcurrentCollections(org.elasticsearch.common.util.concurrent.ConcurrentCollections) AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException) ConcurrentCollections.newConcurrentMap(org.elasticsearch.common.util.concurrent.ConcurrentCollections.newConcurrentMap) ConnectTransportException(org.elasticsearch.transport.ConnectTransportException) Future(java.util.concurrent.Future) Settings(org.elasticsearch.common.settings.Settings) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Locale(java.util.Locale) PingResponse.readPingResponse(org.elasticsearch.discovery.zen.ZenPing.PingResponse.readPingResponse) Map(java.util.Map) ThreadPool(org.elasticsearch.threadpool.ThreadPool) ClusterName(org.elasticsearch.cluster.ClusterName) CollectionUtils(org.elasticsearch.common.util.CollectionUtils) ThreadFactory(java.util.concurrent.ThreadFactory) Releasable(org.elasticsearch.common.lease.Releasable) Setting(org.elasticsearch.common.settings.Setting) Collections.emptyList(java.util.Collections.emptyList) Set(java.util.Set) KeyedLock(org.elasticsearch.common.util.concurrent.KeyedLock) TransportRequestHandler(org.elasticsearch.transport.TransportRequestHandler) ObjectCursor(com.carrotsearch.hppc.cursors.ObjectCursor) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) AbstractRunnable(org.elasticsearch.common.util.concurrent.AbstractRunnable) RemoteTransportException(org.elasticsearch.transport.RemoteTransportException) List(java.util.List) Logger(org.apache.logging.log4j.Logger) Version(org.elasticsearch.Version) Stream(java.util.stream.Stream) TransportAddress(org.elasticsearch.common.transport.TransportAddress) Supplier(org.apache.logging.log4j.util.Supplier) TransportResponseHandler(org.elasticsearch.transport.TransportResponseHandler) TransportRequestOptions(org.elasticsearch.transport.TransportRequestOptions) Queue(java.util.Queue) TransportException(org.elasticsearch.transport.TransportException) TransportChannel(org.elasticsearch.transport.TransportChannel) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) Callable(java.util.concurrent.Callable) EsThreadPoolExecutor(org.elasticsearch.common.util.concurrent.EsThreadPoolExecutor) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) Function(java.util.function.Function) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) TimeValue(org.elasticsearch.common.unit.TimeValue) TransportResponse(org.elasticsearch.transport.TransportResponse) TransportService(org.elasticsearch.transport.TransportService) ExecutorService(java.util.concurrent.ExecutorService) ConnectionProfile(org.elasticsearch.transport.ConnectionProfile) Collections.emptyMap(java.util.Collections.emptyMap) DiscoveryNodes(org.elasticsearch.cluster.node.DiscoveryNodes) EsExecutors(org.elasticsearch.common.util.concurrent.EsExecutors) AbstractComponent(org.elasticsearch.common.component.AbstractComponent) Iterator(java.util.Iterator) Collections.emptySet(java.util.Collections.emptySet) IOUtils(org.apache.lucene.util.IOUtils) IOException(java.io.IOException) Connection(org.elasticsearch.transport.Transport.Connection) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) StreamInput(org.elasticsearch.common.io.stream.StreamInput) NodeNotConnectedException(org.elasticsearch.transport.NodeNotConnectedException) Collections(java.util.Collections) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) TransportAddress(org.elasticsearch.common.transport.TransportAddress) DiscoveryNodes(org.elasticsearch.cluster.node.DiscoveryNodes)

Example 18 with TransportAddress

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

the class UnicastZenPing method resolveHostsLists.

/**
     * Resolves a list of hosts to a list of discovery nodes. Each host is resolved into a transport address (or a collection of addresses
     * if the number of ports is greater than one) and the transport addresses are used to created discovery nodes. Host lookups are done
     * in parallel using specified executor service up to the specified resolve timeout.
     *
     * @param executorService  the executor service used to parallelize hostname lookups
     * @param logger           logger used for logging messages regarding hostname lookups
     * @param hosts            the hosts to resolve
     * @param limitPortCounts  the number of ports to resolve (should be 1 for non-local transport)
     * @param transportService the transport service
     * @param nodeId_prefix    a prefix to use for node ids
     * @param resolveTimeout   the timeout before returning from hostname lookups
     * @return a list of discovery nodes with resolved transport addresses
     */
public static List<DiscoveryNode> resolveHostsLists(final ExecutorService executorService, final Logger logger, final List<String> hosts, final int limitPortCounts, final TransportService transportService, final String nodeId_prefix, final TimeValue resolveTimeout) throws InterruptedException {
    Objects.requireNonNull(executorService);
    Objects.requireNonNull(logger);
    Objects.requireNonNull(hosts);
    Objects.requireNonNull(transportService);
    Objects.requireNonNull(nodeId_prefix);
    Objects.requireNonNull(resolveTimeout);
    if (resolveTimeout.nanos() < 0) {
        throw new IllegalArgumentException("resolve timeout must be non-negative but was [" + resolveTimeout + "]");
    }
    // create tasks to submit to the executor service; we will wait up to resolveTimeout for these tasks to complete
    final List<Callable<TransportAddress[]>> callables = hosts.stream().map(hn -> (Callable<TransportAddress[]>) () -> transportService.addressesFromString(hn, limitPortCounts)).collect(Collectors.toList());
    final List<Future<TransportAddress[]>> futures = executorService.invokeAll(callables, resolveTimeout.nanos(), TimeUnit.NANOSECONDS);
    final List<DiscoveryNode> discoveryNodes = new ArrayList<>();
    final Set<TransportAddress> localAddresses = new HashSet<>();
    localAddresses.add(transportService.boundAddress().publishAddress());
    localAddresses.addAll(Arrays.asList(transportService.boundAddress().boundAddresses()));
    // ExecutorService#invokeAll guarantees that the futures are returned in the iteration order of the tasks so we can associate the
    // hostname with the corresponding task by iterating together
    final Iterator<String> it = hosts.iterator();
    for (final Future<TransportAddress[]> future : futures) {
        final String hostname = it.next();
        if (!future.isCancelled()) {
            assert future.isDone();
            try {
                final TransportAddress[] addresses = future.get();
                logger.trace("resolved host [{}] to {}", hostname, addresses);
                for (int addressId = 0; addressId < addresses.length; addressId++) {
                    final TransportAddress address = addresses[addressId];
                    // no point in pinging ourselves
                    if (localAddresses.contains(address) == false) {
                        discoveryNodes.add(new DiscoveryNode(nodeId_prefix + hostname + "_" + addressId + "#", address, emptyMap(), emptySet(), Version.CURRENT.minimumCompatibilityVersion()));
                    }
                }
            } catch (final ExecutionException e) {
                assert e.getCause() != null;
                final String message = "failed to resolve host [" + hostname + "]";
                logger.warn(message, e.getCause());
            }
        } else {
            logger.warn("timed out after [{}] resolving host [{}]", resolveTimeout, hostname);
        }
    }
    return discoveryNodes;
}
Also used : StreamOutput(org.elasticsearch.common.io.stream.StreamOutput) Arrays(java.util.Arrays) TransportRequest(org.elasticsearch.transport.TransportRequest) Releasables(org.elasticsearch.common.lease.Releasables) Property(org.elasticsearch.common.settings.Setting.Property) ConcurrentCollections(org.elasticsearch.common.util.concurrent.ConcurrentCollections) AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException) ConcurrentCollections.newConcurrentMap(org.elasticsearch.common.util.concurrent.ConcurrentCollections.newConcurrentMap) ConnectTransportException(org.elasticsearch.transport.ConnectTransportException) Future(java.util.concurrent.Future) Settings(org.elasticsearch.common.settings.Settings) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Locale(java.util.Locale) PingResponse.readPingResponse(org.elasticsearch.discovery.zen.ZenPing.PingResponse.readPingResponse) Map(java.util.Map) ThreadPool(org.elasticsearch.threadpool.ThreadPool) ClusterName(org.elasticsearch.cluster.ClusterName) CollectionUtils(org.elasticsearch.common.util.CollectionUtils) ThreadFactory(java.util.concurrent.ThreadFactory) Releasable(org.elasticsearch.common.lease.Releasable) Setting(org.elasticsearch.common.settings.Setting) Collections.emptyList(java.util.Collections.emptyList) Set(java.util.Set) KeyedLock(org.elasticsearch.common.util.concurrent.KeyedLock) TransportRequestHandler(org.elasticsearch.transport.TransportRequestHandler) ObjectCursor(com.carrotsearch.hppc.cursors.ObjectCursor) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) AbstractRunnable(org.elasticsearch.common.util.concurrent.AbstractRunnable) RemoteTransportException(org.elasticsearch.transport.RemoteTransportException) List(java.util.List) Logger(org.apache.logging.log4j.Logger) Version(org.elasticsearch.Version) Stream(java.util.stream.Stream) TransportAddress(org.elasticsearch.common.transport.TransportAddress) Supplier(org.apache.logging.log4j.util.Supplier) TransportResponseHandler(org.elasticsearch.transport.TransportResponseHandler) TransportRequestOptions(org.elasticsearch.transport.TransportRequestOptions) Queue(java.util.Queue) TransportException(org.elasticsearch.transport.TransportException) TransportChannel(org.elasticsearch.transport.TransportChannel) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) Callable(java.util.concurrent.Callable) EsThreadPoolExecutor(org.elasticsearch.common.util.concurrent.EsThreadPoolExecutor) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) Function(java.util.function.Function) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) TimeValue(org.elasticsearch.common.unit.TimeValue) TransportResponse(org.elasticsearch.transport.TransportResponse) TransportService(org.elasticsearch.transport.TransportService) ExecutorService(java.util.concurrent.ExecutorService) ConnectionProfile(org.elasticsearch.transport.ConnectionProfile) Collections.emptyMap(java.util.Collections.emptyMap) DiscoveryNodes(org.elasticsearch.cluster.node.DiscoveryNodes) EsExecutors(org.elasticsearch.common.util.concurrent.EsExecutors) AbstractComponent(org.elasticsearch.common.component.AbstractComponent) Iterator(java.util.Iterator) Collections.emptySet(java.util.Collections.emptySet) IOUtils(org.apache.lucene.util.IOUtils) IOException(java.io.IOException) Connection(org.elasticsearch.transport.Transport.Connection) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) StreamInput(org.elasticsearch.common.io.stream.StreamInput) NodeNotConnectedException(org.elasticsearch.transport.NodeNotConnectedException) Collections(java.util.Collections) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) TransportAddress(org.elasticsearch.common.transport.TransportAddress) ArrayList(java.util.ArrayList) Callable(java.util.concurrent.Callable) Future(java.util.concurrent.Future) ExecutionException(java.util.concurrent.ExecutionException) HashSet(java.util.HashSet)

Example 19 with TransportAddress

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

the class TransportClientNodesServiceTests method checkRemoveAddress.

private void checkRemoveAddress(boolean sniff) {
    Object[] extraSettings = { TransportClient.CLIENT_TRANSPORT_SNIFF.getKey(), sniff };
    try (TestIteration iteration = new TestIteration(extraSettings)) {
        final TransportClientNodesService service = iteration.transportClientNodesService;
        assertEquals(iteration.listNodesCount + iteration.sniffNodesCount, service.connectedNodes().size());
        final TransportAddress addressToRemove = randomFrom(iteration.listNodeAddresses);
        service.removeTransportAddress(addressToRemove);
        assertThat(service.connectedNodes(), everyItem(not(new CustomMatcher<DiscoveryNode>("removed address") {

            @Override
            public boolean matches(Object item) {
                return item instanceof DiscoveryNode && ((DiscoveryNode) item).getAddress().equals(addressToRemove);
            }
        })));
        assertEquals(iteration.listNodesCount + iteration.sniffNodesCount - 1, service.connectedNodes().size());
    }
}
Also used : DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) TransportAddress(org.elasticsearch.common.transport.TransportAddress)

Example 20 with TransportAddress

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

the class TransportClientRetryIT method testRetry.

public void testRetry() throws IOException, ExecutionException, InterruptedException {
    Iterable<TransportService> instances = internalCluster().getInstances(TransportService.class);
    TransportAddress[] addresses = new TransportAddress[internalCluster().size()];
    int i = 0;
    for (TransportService instance : instances) {
        addresses[i++] = instance.boundAddress().publishAddress();
    }
    Settings.Builder builder = Settings.builder().put("client.transport.nodes_sampler_interval", "1s").put("node.name", "transport_client_retry_test").put(ClusterName.CLUSTER_NAME_SETTING.getKey(), internalCluster().getClusterName()).put(Environment.PATH_HOME_SETTING.getKey(), createTempDir());
    try (TransportClient client = new MockTransportClient(builder.build())) {
        client.addTransportAddresses(addresses);
        assertEquals(client.connectedNodes().size(), internalCluster().size());
        int size = cluster().size();
        //kill all nodes one by one, leaving a single master/data node at the end of the loop
        for (int j = 1; j < size; j++) {
            internalCluster().stopRandomNode(input -> true);
            ClusterStateRequest clusterStateRequest = Requests.clusterStateRequest().local(true);
            ClusterState clusterState;
            //use both variants of execute method: with and without listener
            if (randomBoolean()) {
                clusterState = client.admin().cluster().state(clusterStateRequest).get().getState();
            } else {
                PlainListenableActionFuture<ClusterStateResponse> future = new PlainListenableActionFuture<>(client.threadPool());
                client.admin().cluster().state(clusterStateRequest, future);
                clusterState = future.get().getState();
            }
            assertThat(clusterState.nodes().getSize(), greaterThanOrEqualTo(size - j));
            assertThat(client.connectedNodes().size(), greaterThanOrEqualTo(size - j));
        }
    }
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) MockTransportClient(org.elasticsearch.transport.MockTransportClient) TransportAddress(org.elasticsearch.common.transport.TransportAddress) ClusterStateResponse(org.elasticsearch.action.admin.cluster.state.ClusterStateResponse) ClusterStateRequest(org.elasticsearch.action.admin.cluster.state.ClusterStateRequest) MockTransportClient(org.elasticsearch.transport.MockTransportClient) TransportService(org.elasticsearch.transport.TransportService) PlainListenableActionFuture(org.elasticsearch.action.support.PlainListenableActionFuture) Settings(org.elasticsearch.common.settings.Settings)

Aggregations

TransportAddress (org.elasticsearch.common.transport.TransportAddress)89 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)32 Settings (org.elasticsearch.common.settings.Settings)29 ArrayList (java.util.ArrayList)24 BoundTransportAddress (org.elasticsearch.common.transport.BoundTransportAddress)24 IOException (java.io.IOException)21 InetSocketAddress (java.net.InetSocketAddress)20 InetAddress (java.net.InetAddress)19 UnknownHostException (java.net.UnknownHostException)12 NamedWriteableRegistry (org.elasticsearch.common.io.stream.NamedWriteableRegistry)12 TransportService (org.elasticsearch.transport.TransportService)12 HashSet (java.util.HashSet)11 TransportClient (org.elasticsearch.client.transport.TransportClient)11 NetworkService (org.elasticsearch.common.network.NetworkService)11 MockTransportService (org.elasticsearch.test.transport.MockTransportService)11 NoneCircuitBreakerService (org.elasticsearch.indices.breaker.NoneCircuitBreakerService)10 HashMap (java.util.HashMap)9 List (java.util.List)9 Logger (org.apache.logging.log4j.Logger)9 MockTcpTransport (org.elasticsearch.transport.MockTcpTransport)9