Search in sources :

Example 1 with BindTransportException

use of org.elasticsearch.transport.BindTransportException in project elasticsearch by elastic.

the class Netty4HttpServerTransport method createBoundHttpAddress.

private BoundTransportAddress createBoundHttpAddress() {
    // 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);
    final InetSocketAddress publishAddress = new InetSocketAddress(publishInetAddress, publishPort);
    return new BoundTransportAddress(boundAddresses.toArray(new TransportAddress[0]), new TransportAddress(publishAddress));
}
Also used : TransportAddress(org.elasticsearch.common.transport.TransportAddress) BoundTransportAddress(org.elasticsearch.common.transport.BoundTransportAddress) InetSocketAddress(java.net.InetSocketAddress) ArrayList(java.util.ArrayList) BoundTransportAddress(org.elasticsearch.common.transport.BoundTransportAddress) BindTransportException(org.elasticsearch.transport.BindTransportException) IOException(java.io.IOException) BindHttpException(org.elasticsearch.http.BindHttpException) InetAddress(java.net.InetAddress) BindHttpException(org.elasticsearch.http.BindHttpException) BindTransportException(org.elasticsearch.transport.BindTransportException) ReadTimeoutException(io.netty.handler.timeout.ReadTimeoutException) IOException(java.io.IOException)

Example 2 with BindTransportException

use of org.elasticsearch.transport.BindTransportException in project elasticsearch by elastic.

the class SimpleNetty4TransportTests method testBindUnavailableAddress.

public void testBindUnavailableAddress() {
    // this is on a lower level since it needs access to the TransportService before it's started
    int port = serviceA.boundAddress().publishAddress().getPort();
    Settings settings = Settings.builder().put(Node.NODE_NAME_SETTING.getKey(), "foobar").put(TransportService.TRACE_LOG_INCLUDE_SETTING.getKey(), "").put(TransportService.TRACE_LOG_EXCLUDE_SETTING.getKey(), "NOTHING").put("transport.tcp.port", port).build();
    ClusterSettings clusterSettings = new ClusterSettings(settings, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS);
    BindTransportException bindTransportException = expectThrows(BindTransportException.class, () -> {
        MockTransportService transportService = nettyFromThreadPool(settings, threadPool, Version.CURRENT, clusterSettings, true);
        try {
            transportService.start();
        } finally {
            transportService.stop();
            transportService.close();
        }
    });
    assertEquals("Failed to bind to [" + port + "]", bindTransportException.getMessage());
}
Also used : ClusterSettings(org.elasticsearch.common.settings.ClusterSettings) MockTransportService(org.elasticsearch.test.transport.MockTransportService) BindTransportException(org.elasticsearch.transport.BindTransportException) Settings(org.elasticsearch.common.settings.Settings) ClusterSettings(org.elasticsearch.common.settings.ClusterSettings) TransportSettings(org.elasticsearch.transport.TransportSettings)

Example 3 with BindTransportException

use of org.elasticsearch.transport.BindTransportException in project elasticsearch-jetty by sonian.

the class JettyHttpServerTransport method doStart.

@Override
protected void doStart() throws ElasticsearchException {
    PortsRange portsRange = new PortsRange(port);
    final AtomicReference<Exception> lastException = new AtomicReference<Exception>();
    Log.setLog(loggerWrapper);
    portsRange.iterate(new PortsRange.PortCallback() {

        @Override
        public boolean onPortNumber(int portNumber) {
            try {
                Server server = null;
                XmlConfiguration lastXmlConfiguration = null;
                Object[] objs = new Object[jettyConfig.length];
                Map<String, String> esProperties = jettySettings(bindHost, portNumber);
                for (int i = 0; i < jettyConfig.length; i++) {
                    String configFile = jettyConfig[i];
                    URL config = environment.resolveConfig(configFile);
                    XmlConfiguration xmlConfiguration = new XmlConfiguration(config);
                    // in the later configurations
                    if (lastXmlConfiguration != null) {
                        xmlConfiguration.getIdMap().putAll(lastXmlConfiguration.getIdMap());
                    } else {
                        xmlConfiguration.getIdMap().put("ESServerTransport", JettyHttpServerTransport.this);
                        xmlConfiguration.getIdMap().put("ESClient", client);
                    }
                    // Inject elasticsearch properties
                    xmlConfiguration.getProperties().putAll(esProperties);
                    objs[i] = xmlConfiguration.configure();
                    lastXmlConfiguration = xmlConfiguration;
                }
                // Find jetty Server with id  jettyConfigServerId
                Object serverObject = lastXmlConfiguration.getIdMap().get(jettyConfigServerId);
                if (serverObject != null) {
                    if (serverObject instanceof Server) {
                        server = (Server) serverObject;
                    }
                } else {
                    // For compatibility - if it's not available, find first available jetty Server
                    for (Object obj : objs) {
                        if (obj instanceof Server) {
                            server = (Server) obj;
                            break;
                        }
                    }
                }
                if (server == null) {
                    logger.error("Cannot find server with id [{}] in configuration files [{}]", jettyConfigServerId, jettyConfig);
                    lastException.set(new ElasticsearchException("Cannot find server with id " + jettyConfigServerId));
                    return true;
                }
                // Keep it for now for backward compatibility with previous versions of jetty.xml
                server.setAttribute(TRANSPORT_ATTRIBUTE, JettyHttpServerTransport.this);
                // Start all lifecycle objects configured by xml configurations
                for (Object obj : objs) {
                    if (obj instanceof LifeCycle) {
                        LifeCycle lifeCycle = (LifeCycle) obj;
                        if (!lifeCycle.isRunning()) {
                            lifeCycle.start();
                        }
                    }
                }
                jettyServer = server;
                lastException.set(null);
            } catch (BindException e) {
                lastException.set(e);
                return false;
            } catch (Exception e) {
                logger.error("Jetty Startup Failed ", e);
                lastException.set(e);
                return true;
            }
            return true;
        }
    });
    if (lastException.get() != null) {
        throw new BindHttpException("Failed to bind to [" + port + "]", lastException.get());
    }
    InetSocketAddress jettyBoundAddress = findFirstInetConnector(jettyServer);
    if (jettyBoundAddress != null) {
        InetSocketAddress publishAddress;
        try {
            publishAddress = new InetSocketAddress(networkService.resolvePublishHostAddress(publishHost), jettyBoundAddress.getPort());
        } catch (Exception e) {
            throw new BindTransportException("Failed to resolve publish address", e);
        }
        this.boundAddress = new BoundTransportAddress(new InetSocketTransportAddress(jettyBoundAddress), new InetSocketTransportAddress(publishAddress));
    } else {
        throw new BindHttpException("Failed to find a jetty connector with Inet transport");
    }
}
Also used : LifeCycle(org.eclipse.jetty.util.component.LifeCycle) Server(org.eclipse.jetty.server.Server) AtomicReference(java.util.concurrent.atomic.AtomicReference) XmlConfiguration(org.eclipse.jetty.xml.XmlConfiguration) ElasticsearchException(org.elasticsearch.ElasticsearchException) PortsRange(org.elasticsearch.common.transport.PortsRange) InetSocketTransportAddress(org.elasticsearch.common.transport.InetSocketTransportAddress) ElasticsearchException(org.elasticsearch.ElasticsearchException) BindTransportException(org.elasticsearch.transport.BindTransportException) BoundTransportAddress(org.elasticsearch.common.transport.BoundTransportAddress) BindTransportException(org.elasticsearch.transport.BindTransportException) Map(java.util.Map)

Example 4 with BindTransportException

use of org.elasticsearch.transport.BindTransportException in project crate by crate.

the class PostgresNetty method resolveBindAddress.

private BoundTransportAddress resolveBindAddress() {
    // Bind and start to accept incoming connections.
    try {
        InetAddress[] hostAddresses = networkService.resolveBindHostAddresses(bindHosts);
        for (InetAddress address : hostAddresses) {
            if (address instanceof Inet4Address || address instanceof Inet6Address) {
                boundAddresses.add(bindAddress(address));
            }
        }
    } catch (IOException e) {
        throw new BindPostgresException("Failed to resolve binding network host", e);
    }
    final InetAddress publishInetAddress;
    try {
        publishInetAddress = networkService.resolvePublishHostAddresses(publishHosts);
    } catch (Exception e) {
        throw new BindTransportException("Failed to resolve publish address", e);
    }
    final int publishPort = resolvePublishPort(boundAddresses, publishInetAddress);
    final InetSocketAddress publishAddress = new InetSocketAddress(publishInetAddress, publishPort);
    return new BoundTransportAddress(boundAddresses.toArray(new TransportAddress[0]), new TransportAddress(publishAddress));
}
Also used : Inet4Address(java.net.Inet4Address) InetSocketAddress(java.net.InetSocketAddress) BoundTransportAddress(org.elasticsearch.common.transport.BoundTransportAddress) TransportAddress(org.elasticsearch.common.transport.TransportAddress) BoundTransportAddress(org.elasticsearch.common.transport.BoundTransportAddress) BindTransportException(org.elasticsearch.transport.BindTransportException) Inet6Address(java.net.Inet6Address) IOException(java.io.IOException) InetAddress(java.net.InetAddress) BindTransportException(org.elasticsearch.transport.BindTransportException) BindHttpException(org.elasticsearch.http.BindHttpException) IOException(java.io.IOException)

Example 5 with BindTransportException

use of org.elasticsearch.transport.BindTransportException in project crate by crate.

the class PostgresNettyPublishPortTest method testPublishAddressOverride.

@Test
public void testPublishAddressOverride() {
    // Check override for network.publish_host
    Settings settingsWithCustomPublish = Settings.builder().put("network.publish_host", "cantbindtothis").build();
    NetworkService networkService = new NetworkService(Collections.emptyList());
    StubUserManager userManager = new StubUserManager();
    PostgresNetty psql = new PostgresNetty(settingsWithCustomPublish, mock(SQLOperations.class), userManager, networkService, new AlwaysOKAuthentication(userName -> User.CRATE_USER), new NettyBootstrap(), mock(SslContextProvider.class));
    try {
        psql.doStart();
        fail("Should have failed due to custom hostname");
    } catch (BindTransportException e) {
        // that's what we want
        assertThat(e.getCause(), instanceOf(UnknownHostException.class));
    } finally {
        psql.doStop();
        psql.close();
    }
}
Also used : PostgresNetty.resolvePublishPort(io.crate.protocols.postgres.PostgresNetty.resolvePublishPort) User(io.crate.user.User) BindTransportException(org.elasticsearch.transport.BindTransportException) Test(org.junit.Test) BindHttpException(org.elasticsearch.http.BindHttpException) UnknownHostException(java.net.UnknownHostException) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) NetworkService(org.elasticsearch.common.network.NetworkService) InetAddress.getByName(java.net.InetAddress.getByName) Settings(org.elasticsearch.common.settings.Settings) TransportAddress(org.elasticsearch.common.transport.TransportAddress) Arrays.asList(java.util.Arrays.asList) NettyBootstrap(io.crate.netty.NettyBootstrap) Matchers.equalTo(org.hamcrest.Matchers.equalTo) SslContextProvider(io.crate.protocols.ssl.SslContextProvider) ESTestCase(org.elasticsearch.test.ESTestCase) AlwaysOKAuthentication(io.crate.auth.AlwaysOKAuthentication) SQLOperations(io.crate.action.sql.SQLOperations) StubUserManager(io.crate.user.StubUserManager) Collections(java.util.Collections) Mockito.mock(org.mockito.Mockito.mock) AlwaysOKAuthentication(io.crate.auth.AlwaysOKAuthentication) NetworkService(org.elasticsearch.common.network.NetworkService) BindTransportException(org.elasticsearch.transport.BindTransportException) SslContextProvider(io.crate.protocols.ssl.SslContextProvider) SQLOperations(io.crate.action.sql.SQLOperations) Settings(org.elasticsearch.common.settings.Settings) StubUserManager(io.crate.user.StubUserManager) NettyBootstrap(io.crate.netty.NettyBootstrap) Test(org.junit.Test)

Aggregations

BindTransportException (org.elasticsearch.transport.BindTransportException)6 BoundTransportAddress (org.elasticsearch.common.transport.BoundTransportAddress)4 TransportAddress (org.elasticsearch.common.transport.TransportAddress)4 BindHttpException (org.elasticsearch.http.BindHttpException)4 IOException (java.io.IOException)3 InetAddress (java.net.InetAddress)3 InetSocketAddress (java.net.InetSocketAddress)3 ArrayList (java.util.ArrayList)2 Settings (org.elasticsearch.common.settings.Settings)2 SQLOperations (io.crate.action.sql.SQLOperations)1 AlwaysOKAuthentication (io.crate.auth.AlwaysOKAuthentication)1 NettyBootstrap (io.crate.netty.NettyBootstrap)1 PostgresNetty.resolvePublishPort (io.crate.protocols.postgres.PostgresNetty.resolvePublishPort)1 SslContextProvider (io.crate.protocols.ssl.SslContextProvider)1 StubUserManager (io.crate.user.StubUserManager)1 User (io.crate.user.User)1 ReadTimeoutException (io.netty.handler.timeout.ReadTimeoutException)1 Inet4Address (java.net.Inet4Address)1 Inet6Address (java.net.Inet6Address)1 InetAddress.getByName (java.net.InetAddress.getByName)1