Search in sources :

Example 11 with BoundTransportAddress

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

the class BootstrapChecksTests method testEnforceLimitsWhenBoundToNonLocalAddress.

public void testEnforceLimitsWhenBoundToNonLocalAddress() {
    final List<TransportAddress> transportAddresses = new ArrayList<>();
    final TransportAddress nonLocalTransportAddress = buildNewFakeTransportAddress();
    transportAddresses.add(nonLocalTransportAddress);
    for (int i = 0; i < randomIntBetween(0, 7); i++) {
        final TransportAddress randomTransportAddress = randomBoolean() ? buildNewFakeTransportAddress() : new TransportAddress(InetAddress.getLoopbackAddress(), i);
        transportAddresses.add(randomTransportAddress);
    }
    final TransportAddress publishAddress = randomBoolean() ? buildNewFakeTransportAddress() : new TransportAddress(InetAddress.getLoopbackAddress(), 0);
    final BoundTransportAddress boundTransportAddress = mock(BoundTransportAddress.class);
    Collections.shuffle(transportAddresses, random());
    when(boundTransportAddress.boundAddresses()).thenReturn(transportAddresses.toArray(new TransportAddress[0]));
    when(boundTransportAddress.publishAddress()).thenReturn(publishAddress);
    assertTrue(BootstrapChecks.enforceLimits(boundTransportAddress));
}
Also used : BoundTransportAddress(org.elasticsearch.common.transport.BoundTransportAddress) TransportAddress(org.elasticsearch.common.transport.TransportAddress) ArrayList(java.util.ArrayList) BoundTransportAddress(org.elasticsearch.common.transport.BoundTransportAddress)

Example 12 with BoundTransportAddress

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

the class BootstrapChecksTests method testNonProductionMode.

public void testNonProductionMode() throws NodeValidationException {
    // nothing should happen since we are in non-production mode
    final List<TransportAddress> transportAddresses = new ArrayList<>();
    for (int i = 0; i < randomIntBetween(1, 8); i++) {
        TransportAddress localTransportAddress = new TransportAddress(InetAddress.getLoopbackAddress(), i);
        transportAddresses.add(localTransportAddress);
    }
    TransportAddress publishAddress = new TransportAddress(InetAddress.getLoopbackAddress(), 0);
    BoundTransportAddress boundTransportAddress = mock(BoundTransportAddress.class);
    when(boundTransportAddress.boundAddresses()).thenReturn(transportAddresses.toArray(new TransportAddress[0]));
    when(boundTransportAddress.publishAddress()).thenReturn(publishAddress);
    BootstrapChecks.check(Settings.EMPTY, boundTransportAddress, Collections.emptyList());
}
Also used : BoundTransportAddress(org.elasticsearch.common.transport.BoundTransportAddress) TransportAddress(org.elasticsearch.common.transport.TransportAddress) ArrayList(java.util.ArrayList) BoundTransportAddress(org.elasticsearch.common.transport.BoundTransportAddress)

Example 13 with BoundTransportAddress

use of org.elasticsearch.common.transport.BoundTransportAddress 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 14 with BoundTransportAddress

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

the class Node method writePortsFile.

/** Writes a file to the logs dir containing the ports for the given transport type */
private void writePortsFile(String type, BoundTransportAddress boundAddress) {
    Path tmpPortsFile = environment.logsFile().resolve(type + ".ports.tmp");
    try (BufferedWriter writer = Files.newBufferedWriter(tmpPortsFile, Charset.forName("UTF-8"))) {
        for (TransportAddress address : boundAddress.boundAddresses()) {
            InetAddress inetAddress = InetAddress.getByName(address.getAddress());
            if (inetAddress instanceof Inet6Address && inetAddress.isLinkLocalAddress()) {
                // no link local, just causes problems
                continue;
            }
            writer.write(NetworkAddress.format(new InetSocketAddress(inetAddress, address.getPort())) + "\n");
        }
    } catch (IOException e) {
        throw new RuntimeException("Failed to write ports file", e);
    }
    Path portsFile = environment.logsFile().resolve(type + ".ports");
    try {
        Files.move(tmpPortsFile, portsFile, StandardCopyOption.ATOMIC_MOVE);
    } catch (IOException e) {
        throw new RuntimeException("Failed to rename ports file", e);
    }
}
Also used : Path(java.nio.file.Path) BoundTransportAddress(org.elasticsearch.common.transport.BoundTransportAddress) TransportAddress(org.elasticsearch.common.transport.TransportAddress) InetSocketAddress(java.net.InetSocketAddress) Inet6Address(java.net.Inet6Address) IOException(java.io.IOException) InetAddress(java.net.InetAddress) BufferedWriter(java.io.BufferedWriter)

Example 15 with BoundTransportAddress

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

the class Netty4TransportMultiPortIntegrationIT method testThatInfosAreExposed.

@Network
public void testThatInfosAreExposed() throws Exception {
    NodesInfoResponse response = client().admin().cluster().prepareNodesInfo().clear().setTransport(true).get();
    for (NodeInfo nodeInfo : response.getNodes()) {
        assertThat(nodeInfo.getTransport().getProfileAddresses().keySet(), hasSize(1));
        assertThat(nodeInfo.getTransport().getProfileAddresses(), hasKey("client1"));
        BoundTransportAddress boundTransportAddress = nodeInfo.getTransport().getProfileAddresses().get("client1");
        for (TransportAddress transportAddress : boundTransportAddress.boundAddresses()) {
            assertThat(transportAddress, instanceOf(TransportAddress.class));
        }
        // bound addresses
        for (TransportAddress transportAddress : boundTransportAddress.boundAddresses()) {
            assertThat(transportAddress, instanceOf(TransportAddress.class));
            assertThat(transportAddress.address().getPort(), is(allOf(greaterThanOrEqualTo(randomPort), lessThanOrEqualTo(randomPort + 10))));
        }
        // publish address
        assertThat(boundTransportAddress.publishAddress(), instanceOf(TransportAddress.class));
        TransportAddress publishAddress = boundTransportAddress.publishAddress();
        assertThat(NetworkAddress.format(publishAddress.address().getAddress()), is("127.0.0.7"));
        assertThat(publishAddress.address().getPort(), is(4321));
    }
}
Also used : NodesInfoResponse(org.elasticsearch.action.admin.cluster.node.info.NodesInfoResponse) NodeInfo(org.elasticsearch.action.admin.cluster.node.info.NodeInfo) BoundTransportAddress(org.elasticsearch.common.transport.BoundTransportAddress) TransportAddress(org.elasticsearch.common.transport.TransportAddress) BoundTransportAddress(org.elasticsearch.common.transport.BoundTransportAddress) Network(org.elasticsearch.test.junit.annotations.Network)

Aggregations

BoundTransportAddress (org.elasticsearch.common.transport.BoundTransportAddress)22 TransportAddress (org.elasticsearch.common.transport.TransportAddress)16 IOException (java.io.IOException)8 ArrayList (java.util.ArrayList)8 InetAddress (java.net.InetAddress)7 InetSocketAddress (java.net.InetSocketAddress)7 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)7 NamedWriteableRegistry (org.elasticsearch.common.io.stream.NamedWriteableRegistry)6 NetworkService (org.elasticsearch.common.network.NetworkService)6 Settings (org.elasticsearch.common.settings.Settings)6 NoneCircuitBreakerService (org.elasticsearch.indices.breaker.NoneCircuitBreakerService)6 MockTransportService (org.elasticsearch.test.transport.MockTransportService)6 MockTcpTransport (org.elasticsearch.transport.MockTcpTransport)6 UnknownHostException (java.net.UnknownHostException)5 Logger (org.apache.logging.log4j.Logger)5 Transport (org.elasticsearch.transport.Transport)5 TransportService (org.elasticsearch.transport.TransportService)5 HashMap (java.util.HashMap)3 HashSet (java.util.HashSet)3 List (java.util.List)3