Search in sources :

Example 96 with TransportAddress

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

the class AbstractSimpleTransportTestCase method testTimeoutPerConnection.

public void testTimeoutPerConnection() throws IOException {
    assumeTrue("Works only on BSD network stacks and apparently windows", Constants.MAC_OS_X || Constants.FREE_BSD || Constants.WINDOWS);
    try (ServerSocket socket = new MockServerSocket()) {
        // note - this test uses backlog=1 which is implementation specific ie. it might not work on some TCP/IP stacks
        // on linux (at least newer ones) the listen(addr, backlog=1) should just ignore new connections if the queue is full which
        // means that once we received an ACK from the client we just drop the packet on the floor (which is what we want) and we run
        // into a connection timeout quickly. Yet other implementations can for instance can terminate the connection within the 3 way
        // handshake which I haven't tested yet.
        socket.bind(new InetSocketAddress(InetAddress.getLocalHost(), 0), 1);
        socket.setReuseAddress(true);
        DiscoveryNode first = new DiscoveryNode("TEST", new TransportAddress(socket.getInetAddress(), socket.getLocalPort()), emptyMap(), emptySet(), version0);
        DiscoveryNode second = new DiscoveryNode("TEST", new TransportAddress(socket.getInetAddress(), socket.getLocalPort()), emptyMap(), emptySet(), version0);
        ConnectionProfile.Builder builder = new ConnectionProfile.Builder();
        builder.addConnections(1, TransportRequestOptions.Type.BULK, TransportRequestOptions.Type.PING, TransportRequestOptions.Type.RECOVERY, TransportRequestOptions.Type.REG, TransportRequestOptions.Type.STATE);
        // connection with one connection and a large timeout -- should consume the one spot in the backlog queue
        try (TransportService service = buildService("TS_TPC", Version.CURRENT, null, Settings.EMPTY, true, false)) {
            IOUtils.close(service.openConnection(first, builder.build()));
            builder.setConnectTimeout(TimeValue.timeValueMillis(1));
            final ConnectionProfile profile = builder.build();
            // now with the 1ms timeout we got and test that is it's applied
            long startTime = System.nanoTime();
            ConnectTransportException ex = expectThrows(ConnectTransportException.class, () -> service.openConnection(second, profile));
            final long now = System.nanoTime();
            final long timeTaken = TimeValue.nsecToMSec(now - startTime);
            assertTrue("test didn't timeout quick enough, time taken: [" + timeTaken + "]", timeTaken < TimeValue.timeValueSeconds(5).millis());
            assertEquals(ex.getMessage(), "[][" + second.getAddress() + "] connect_timeout[1ms]");
        }
    }
}
Also used : DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) MockTransportService(org.elasticsearch.test.transport.MockTransportService) InetSocketAddress(java.net.InetSocketAddress) TransportAddress(org.elasticsearch.common.transport.TransportAddress) MockServerSocket(org.elasticsearch.mocksocket.MockServerSocket) ServerSocket(java.net.ServerSocket) MockServerSocket(org.elasticsearch.mocksocket.MockServerSocket)

Example 97 with TransportAddress

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

the class AbstractSimpleTransportTestCase method testTcpHandshakeTimeout.

public void testTcpHandshakeTimeout() throws IOException {
    try (ServerSocket socket = new MockServerSocket()) {
        socket.bind(new InetSocketAddress(InetAddress.getLocalHost(), 0), 1);
        socket.setReuseAddress(true);
        DiscoveryNode dummy = new DiscoveryNode("TEST", new TransportAddress(socket.getInetAddress(), socket.getLocalPort()), emptyMap(), emptySet(), version0);
        ConnectionProfile.Builder builder = new ConnectionProfile.Builder();
        builder.addConnections(1, TransportRequestOptions.Type.BULK, TransportRequestOptions.Type.PING, TransportRequestOptions.Type.RECOVERY, TransportRequestOptions.Type.REG, TransportRequestOptions.Type.STATE);
        builder.setHandshakeTimeout(TimeValue.timeValueMillis(1));
        ConnectTransportException ex = expectThrows(ConnectTransportException.class, () -> serviceA.connectToNode(dummy, builder.build()));
        assertEquals("[][" + dummy.getAddress() + "] handshake_timeout[1ms]", ex.getMessage());
    }
}
Also used : DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) InetSocketAddress(java.net.InetSocketAddress) TransportAddress(org.elasticsearch.common.transport.TransportAddress) MockServerSocket(org.elasticsearch.mocksocket.MockServerSocket) ServerSocket(java.net.ServerSocket) MockServerSocket(org.elasticsearch.mocksocket.MockServerSocket)

Example 98 with TransportAddress

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

the class MockTransportService method extractTransportAddresses.

public static TransportAddress[] extractTransportAddresses(TransportService transportService) {
    HashSet<TransportAddress> transportAddresses = new HashSet<>();
    BoundTransportAddress boundTransportAddress = transportService.boundAddress();
    transportAddresses.addAll(Arrays.asList(boundTransportAddress.boundAddresses()));
    transportAddresses.add(boundTransportAddress.publishAddress());
    return transportAddresses.toArray(new TransportAddress[transportAddresses.size()]);
}
Also used : TransportAddress(org.elasticsearch.common.transport.TransportAddress) BoundTransportAddress(org.elasticsearch.common.transport.BoundTransportAddress) BoundTransportAddress(org.elasticsearch.common.transport.BoundTransportAddress) HashSet(java.util.HashSet)

Example 99 with TransportAddress

use of org.elasticsearch.common.transport.TransportAddress in project flink by apache.

the class ElasticsearchUtils method convertInetSocketAddresses.

/**
	 * Utility method to convert a {@link List} of {@link InetSocketAddress} to Elasticsearch {@link TransportAddress}.
	 *
	 * @param inetSocketAddresses The list of {@link InetSocketAddress} to convert.
	 */
public static List<TransportAddress> convertInetSocketAddresses(List<InetSocketAddress> inetSocketAddresses) {
    if (inetSocketAddresses == null) {
        return null;
    } else {
        List<TransportAddress> converted;
        converted = new ArrayList<>(inetSocketAddresses.size());
        for (InetSocketAddress address : inetSocketAddresses) {
            converted.add(new InetSocketTransportAddress(address));
        }
        return converted;
    }
}
Also used : TransportAddress(org.elasticsearch.common.transport.TransportAddress) InetSocketTransportAddress(org.elasticsearch.common.transport.InetSocketTransportAddress) InetSocketAddress(java.net.InetSocketAddress) InetSocketTransportAddress(org.elasticsearch.common.transport.InetSocketTransportAddress)

Example 100 with TransportAddress

use of org.elasticsearch.common.transport.TransportAddress in project flink by apache.

the class Elasticsearch2ApiCallBridge method createClient.

@Override
public Client createClient(Map<String, String> clientConfig) {
    Settings settings = Settings.settingsBuilder().put(clientConfig).build();
    TransportClient transportClient = TransportClient.builder().settings(settings).build();
    for (TransportAddress address : ElasticsearchUtils.convertInetSocketAddresses(transportAddresses)) {
        transportClient.addTransportAddress(address);
    }
    // verify that we actually are connected to a cluster
    if (transportClient.connectedNodes().isEmpty()) {
        throw new RuntimeException("Elasticsearch client is not connected to any Elasticsearch nodes!");
    }
    if (LOG.isInfoEnabled()) {
        LOG.info("Created Elasticsearch TransportClient with connected nodes {}", transportClient.connectedNodes());
    }
    return transportClient;
}
Also used : TransportClient(org.elasticsearch.client.transport.TransportClient) TransportAddress(org.elasticsearch.common.transport.TransportAddress) Settings(org.elasticsearch.common.settings.Settings)

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