Search in sources :

Example 11 with TransportAddress

use of org.opensearch.common.transport.TransportAddress in project OpenSearch by opensearch-project.

the class SeedHostsResolver method resolveHostsLists.

/**
 * Resolves a list of hosts to a list of transport addresses. Each host is resolved into a transport address (or a collection of
 * addresses if the number of ports is greater than one). 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 transportService the transport service
 * @param resolveTimeout   the timeout before returning from hostname lookups
 * @return a list of resolved transport addresses
 */
public static List<TransportAddress> resolveHostsLists(final CancellableThreads cancellableThreads, final ExecutorService executorService, final Logger logger, final List<String> hosts, final TransportService transportService, final TimeValue resolveTimeout) {
    Objects.requireNonNull(executorService);
    Objects.requireNonNull(logger);
    Objects.requireNonNull(hosts);
    Objects.requireNonNull(transportService);
    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)).collect(Collectors.toList());
    final SetOnce<List<Future<TransportAddress[]>>> futures = new SetOnce<>();
    try {
        cancellableThreads.execute(() -> futures.set(executorService.invokeAll(callables, resolveTimeout.nanos(), TimeUnit.NANOSECONDS)));
    } catch (CancellableThreads.ExecutionCancelledException e) {
        return Collections.emptyList();
    }
    final List<TransportAddress> transportAddresses = 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.get()) {
        assert future.isDone();
        final String hostname = it.next();
        if (!future.isCancelled()) {
            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) {
                        transportAddresses.add(address);
                    }
                }
            } catch (final ExecutionException e) {
                assert e.getCause() != null;
                final String message = "failed to resolve host [" + hostname + "]";
                logger.warn(message, e.getCause());
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            // ignore
            }
        } else {
            logger.warn("timed out after [{}] resolving host [{}]", resolveTimeout, hostname);
        }
    }
    return Collections.unmodifiableList(transportAddresses);
}
Also used : CancellableThreads(org.opensearch.common.util.CancellableThreads) Arrays(java.util.Arrays) AbstractRunnable(org.opensearch.common.util.concurrent.AbstractRunnable) ThreadPool(org.opensearch.threadpool.ThreadPool) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Callable(java.util.concurrent.Callable) OpenSearchExecutors(org.opensearch.common.util.concurrent.OpenSearchExecutors) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Future(java.util.concurrent.Future) ThreadFactory(java.util.concurrent.ThreadFactory) ExecutorService(java.util.concurrent.ExecutorService) ConfiguredHostsResolver(org.opensearch.discovery.PeerFinder.ConfiguredHostsResolver) Setting(org.opensearch.common.settings.Setting) TimeValue(org.opensearch.common.unit.TimeValue) SetOnce(org.apache.lucene.util.SetOnce) Iterator(java.util.Iterator) Set(java.util.Set) Settings(org.opensearch.common.settings.Settings) TransportService(org.opensearch.transport.TransportService) Collectors(java.util.stream.Collectors) TransportAddress(org.opensearch.common.transport.TransportAddress) Objects(java.util.Objects) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) AbstractLifecycleComponent(org.opensearch.common.component.AbstractLifecycleComponent) List(java.util.List) Logger(org.apache.logging.log4j.Logger) LogManager(org.apache.logging.log4j.LogManager) Collections(java.util.Collections) CancellableThreads(org.opensearch.common.util.CancellableThreads) SetOnce(org.apache.lucene.util.SetOnce) TransportAddress(org.opensearch.common.transport.TransportAddress) ArrayList(java.util.ArrayList) Callable(java.util.concurrent.Callable) ArrayList(java.util.ArrayList) List(java.util.List) ExecutionException(java.util.concurrent.ExecutionException) HashSet(java.util.HashSet)

Example 12 with TransportAddress

use of org.opensearch.common.transport.TransportAddress in project OpenSearch by opensearch-project.

the class AbstractHttpServerTransport method bindServer.

protected void bindServer() {
    // Bind and start to accept incoming connections.
    InetAddress[] hostAddresses;
    try {
        hostAddresses = networkService.resolveBindHostAddresses(bindHosts);
    } catch (IOException e) {
        throw new BindHttpException("Failed to resolve host [" + Arrays.toString(bindHosts) + "]", e);
    }
    List<TransportAddress> boundAddresses = new ArrayList<>(hostAddresses.length);
    for (InetAddress address : hostAddresses) {
        boundAddresses.add(bindAddress(address));
    }
    final InetAddress publishInetAddress;
    try {
        publishInetAddress = networkService.resolvePublishHostAddresses(publishHosts);
    } catch (Exception e) {
        throw new BindTransportException("Failed to resolve publish address", e);
    }
    final int publishPort = resolvePublishPort(settings, boundAddresses, publishInetAddress);
    TransportAddress publishAddress = new TransportAddress(new InetSocketAddress(publishInetAddress, publishPort));
    this.boundAddress = new BoundTransportAddress(boundAddresses.toArray(new TransportAddress[0]), publishAddress);
    logger.info("{}", boundAddress);
}
Also used : BoundTransportAddress(org.opensearch.common.transport.BoundTransportAddress) TransportAddress(org.opensearch.common.transport.TransportAddress) InetSocketAddress(java.net.InetSocketAddress) ArrayList(java.util.ArrayList) BoundTransportAddress(org.opensearch.common.transport.BoundTransportAddress) BindTransportException(org.opensearch.transport.BindTransportException) IOException(java.io.IOException) InetAddress(java.net.InetAddress) CancelledKeyException(java.nio.channels.CancelledKeyException) IOException(java.io.IOException) BindTransportException(org.opensearch.transport.BindTransportException)

Example 13 with TransportAddress

use of org.opensearch.common.transport.TransportAddress in project OpenSearch by opensearch-project.

the class AbstractHttpServerTransport method bindAddress.

private TransportAddress bindAddress(final InetAddress hostAddress) {
    final AtomicReference<Exception> lastException = new AtomicReference<>();
    final AtomicReference<InetSocketAddress> boundSocket = new AtomicReference<>();
    boolean success = port.iterate(portNumber -> {
        try {
            synchronized (httpServerChannels) {
                HttpServerChannel httpServerChannel = bind(new InetSocketAddress(hostAddress, portNumber));
                httpServerChannels.add(httpServerChannel);
                boundSocket.set(httpServerChannel.getLocalAddress());
            }
        } catch (Exception e) {
            lastException.set(e);
            return false;
        }
        return true;
    });
    if (!success) {
        throw new BindHttpException("Failed to bind to " + NetworkAddress.format(hostAddress, port), lastException.get());
    }
    if (logger.isDebugEnabled()) {
        logger.debug("Bound http to address {{}}", NetworkAddress.format(boundSocket.get()));
    }
    return new TransportAddress(boundSocket.get());
}
Also used : InetSocketAddress(java.net.InetSocketAddress) BoundTransportAddress(org.opensearch.common.transport.BoundTransportAddress) TransportAddress(org.opensearch.common.transport.TransportAddress) AtomicReference(java.util.concurrent.atomic.AtomicReference) CancelledKeyException(java.nio.channels.CancelledKeyException) IOException(java.io.IOException) BindTransportException(org.opensearch.transport.BindTransportException)

Example 14 with TransportAddress

use of org.opensearch.common.transport.TransportAddress in project OpenSearch by opensearch-project.

the class InboundHandler method handleRequest.

private <T extends TransportRequest> void handleRequest(TcpChannel channel, Header header, InboundMessage message) throws IOException {
    final String action = header.getActionName();
    final long requestId = header.getRequestId();
    final Version version = header.getVersion();
    if (header.isHandshake()) {
        messageListener.onRequestReceived(requestId, action);
        // Cannot short circuit handshakes
        assert message.isShortCircuit() == false;
        final StreamInput stream = namedWriteableStream(message.openOrGetStreamInput());
        assertRemoteVersion(stream, header.getVersion());
        final TransportChannel transportChannel = new TcpTransportChannel(outboundHandler, channel, action, requestId, version, header.getFeatures(), header.isCompressed(), header.isHandshake(), message.takeBreakerReleaseControl());
        try {
            handshaker.handleHandshake(transportChannel, requestId, stream);
        } catch (Exception e) {
            if (Version.CURRENT.isCompatible(header.getVersion())) {
                sendErrorResponse(action, transportChannel, e);
            } else {
                logger.warn(new ParameterizedMessage("could not send error response to handshake received on [{}] using wire format version [{}], closing channel", channel, header.getVersion()), e);
                channel.close();
            }
        }
    } else {
        final TransportChannel transportChannel = new TcpTransportChannel(outboundHandler, channel, action, requestId, version, header.getFeatures(), header.isCompressed(), header.isHandshake(), message.takeBreakerReleaseControl());
        try {
            messageListener.onRequestReceived(requestId, action);
            if (message.isShortCircuit()) {
                sendErrorResponse(action, transportChannel, message.getException());
            } else {
                final StreamInput stream = namedWriteableStream(message.openOrGetStreamInput());
                assertRemoteVersion(stream, header.getVersion());
                final RequestHandlerRegistry<T> reg = requestHandlers.getHandler(action);
                assert reg != null;
                final T request = newRequest(requestId, action, stream, reg);
                request.remoteAddress(new TransportAddress(channel.getRemoteAddress()));
                checkStreamIsFullyConsumed(requestId, action, stream);
                final String executor = reg.getExecutor();
                if (ThreadPool.Names.SAME.equals(executor)) {
                    try {
                        reg.processMessageReceived(request, transportChannel);
                    } catch (Exception e) {
                        sendErrorResponse(reg.getAction(), transportChannel, e);
                    }
                } else {
                    threadPool.executor(executor).execute(new RequestHandler<>(reg, request, transportChannel));
                }
            }
        } catch (Exception e) {
            sendErrorResponse(action, transportChannel, e);
        }
    }
}
Also used : Version(org.opensearch.Version) TransportAddress(org.opensearch.common.transport.TransportAddress) StreamInput(org.opensearch.common.io.stream.StreamInput) ByteBufferStreamInput(org.opensearch.common.io.stream.ByteBufferStreamInput) NamedWriteableAwareStreamInput(org.opensearch.common.io.stream.NamedWriteableAwareStreamInput) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) IOException(java.io.IOException) EOFException(java.io.EOFException)

Example 15 with TransportAddress

use of org.opensearch.common.transport.TransportAddress in project OpenSearch by opensearch-project.

the class InboundHandler method handleResponse.

private <T extends TransportResponse> void handleResponse(final long requestId, InetSocketAddress remoteAddress, final StreamInput stream, final TransportResponseHandler<T> handler) {
    final T response;
    try {
        response = handler.read(stream);
        response.remoteAddress(new TransportAddress(remoteAddress));
        checkStreamIsFullyConsumed(requestId, handler, stream, false);
    } catch (Exception e) {
        final Exception serializationException = new TransportSerializationException("Failed to deserialize response from handler [" + handler + "]", e);
        logger.warn(new ParameterizedMessage("Failed to deserialize response from [{}]", remoteAddress), serializationException);
        handleException(handler, serializationException);
        return;
    }
    final String executor = handler.executor();
    if (ThreadPool.Names.SAME.equals(executor)) {
        doHandleResponse(handler, response);
    } else {
        threadPool.executor(executor).execute(() -> doHandleResponse(handler, response));
    }
}
Also used : TransportAddress(org.opensearch.common.transport.TransportAddress) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) IOException(java.io.IOException) EOFException(java.io.EOFException)

Aggregations

TransportAddress (org.opensearch.common.transport.TransportAddress)175 DiscoveryNode (org.opensearch.cluster.node.DiscoveryNode)61 Settings (org.opensearch.common.settings.Settings)57 IOException (java.io.IOException)32 BoundTransportAddress (org.opensearch.common.transport.BoundTransportAddress)31 ArrayList (java.util.ArrayList)28 Version (org.opensearch.Version)28 HashSet (java.util.HashSet)25 List (java.util.List)25 InetAddress (java.net.InetAddress)24 CountDownLatch (java.util.concurrent.CountDownLatch)21 ThreadPool (org.opensearch.threadpool.ThreadPool)21 ClusterSettings (org.opensearch.common.settings.ClusterSettings)20 ClusterName (org.opensearch.cluster.ClusterName)17 Set (java.util.Set)16 TimeUnit (java.util.concurrent.TimeUnit)16 PlainActionFuture (org.opensearch.action.support.PlainActionFuture)16 HttpServerTransport (org.opensearch.http.HttpServerTransport)16 InetSocketAddress (java.net.InetSocketAddress)15 OpenSearchTestCase (org.opensearch.test.OpenSearchTestCase)15