Search in sources :

Example 61 with TransportAddress

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

the class AbstractSimpleTransportTestCase method testTimeoutPerConnection.

public void testTimeoutPerConnection() throws IOException {
    assumeTrue("Works only on BSD network stacks", Constants.MAC_OS_X || Constants.FREE_BSD);
    try (ServerSocket socket = new ServerSocket()) {
        // 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(getLocalEphemeral(), 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.opensearch.cluster.node.DiscoveryNode) MockTransportService(org.opensearch.test.transport.MockTransportService) BoundTransportAddress(org.opensearch.common.transport.BoundTransportAddress) TransportAddress(org.opensearch.common.transport.TransportAddress) ServerSocket(java.net.ServerSocket)

Example 62 with TransportAddress

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

the class AbstractSimpleTransportTestCase method testHandshakeUpdatesVersion.

public void testHandshakeUpdatesVersion() throws IOException {
    assumeTrue("only tcp transport has a handshake method", serviceA.getOriginalTransport() instanceof TcpTransport);
    Version version = VersionUtils.randomVersionBetween(random(), Version.CURRENT.minimumCompatibilityVersion(), Version.CURRENT);
    try (MockTransportService service = buildService("TS_C", version, Settings.EMPTY)) {
        service.start();
        service.acceptIncomingRequests();
        TransportAddress address = service.boundAddress().publishAddress();
        DiscoveryNode node = new DiscoveryNode("TS_TPC", "TS_TPC", address, emptyMap(), emptySet(), LegacyESVersion.fromString("2.0.0"));
        ConnectionProfile.Builder builder = new ConnectionProfile.Builder();
        builder.addConnections(1, TransportRequestOptions.Type.BULK, TransportRequestOptions.Type.PING, TransportRequestOptions.Type.RECOVERY, TransportRequestOptions.Type.REG, TransportRequestOptions.Type.STATE);
        try (Transport.Connection connection = serviceA.openConnection(node, builder.build())) {
            // OpenSearch [1.0:3.0) in bwc mode should only "upgrade" to Legacy v7.10.2
            assertEquals(connection.getVersion(), version.onOrAfter(Version.V_1_0_0) && version.before(V_3_0_0) ? LegacyESVersion.V_7_10_2 : version);
        }
    }
}
Also used : DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) MockTransportService(org.opensearch.test.transport.MockTransportService) Version(org.opensearch.Version) LegacyESVersion(org.opensearch.LegacyESVersion) BoundTransportAddress(org.opensearch.common.transport.BoundTransportAddress) TransportAddress(org.opensearch.common.transport.TransportAddress) StubbableTransport(org.opensearch.test.transport.StubbableTransport)

Example 63 with TransportAddress

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

the class AbstractSimpleTransportTestCase method testHostOnMessages.

public void testHostOnMessages() throws InterruptedException {
    final CountDownLatch latch = new CountDownLatch(2);
    final AtomicReference<TransportAddress> addressA = new AtomicReference<>();
    final AtomicReference<TransportAddress> addressB = new AtomicReference<>();
    serviceB.registerRequestHandler("internal:action1", ThreadPool.Names.SAME, TestRequest::new, (request, channel, task) -> {
        addressA.set(request.remoteAddress());
        channel.sendResponse(new TestResponse((String) null));
        latch.countDown();
    });
    serviceA.sendRequest(nodeB, "internal:action1", new TestRequest(), new TransportResponseHandler<TestResponse>() {

        @Override
        public TestResponse read(StreamInput in) throws IOException {
            return new TestResponse(in);
        }

        @Override
        public void handleResponse(TestResponse response) {
            addressB.set(response.remoteAddress());
            latch.countDown();
        }

        @Override
        public void handleException(TransportException exp) {
            latch.countDown();
        }

        @Override
        public String executor() {
            return ThreadPool.Names.SAME;
        }
    });
    if (!latch.await(10, TimeUnit.SECONDS)) {
        fail("message round trip did not complete within a sensible time frame");
    }
    assertTrue(nodeA.getAddress().getAddress().equals(addressA.get().getAddress()));
    assertTrue(nodeB.getAddress().getAddress().equals(addressB.get().getAddress()));
}
Also used : BoundTransportAddress(org.opensearch.common.transport.BoundTransportAddress) TransportAddress(org.opensearch.common.transport.TransportAddress) StreamInput(org.opensearch.common.io.stream.StreamInput) AtomicReference(java.util.concurrent.atomic.AtomicReference) Matchers.hasToString(org.hamcrest.Matchers.hasToString) Matchers.containsString(org.hamcrest.Matchers.containsString) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 64 with TransportAddress

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

the class AbstractSimpleTransportTestCase method testTransportProfilesWithPortAndHost.

public void testTransportProfilesWithPortAndHost() {
    boolean doIPV6 = NetworkUtils.SUPPORTS_V6;
    List<String> hosts;
    if (doIPV6) {
        hosts = Arrays.asList("_local:ipv6_", "_local:ipv4_");
    } else {
        hosts = Arrays.asList("_local:ipv4_");
    }
    try (MockTransportService serviceC = buildService("TS_C", version0, Settings.builder().put("transport.profiles.default.bind_host", "_local:ipv4_").put("transport.profiles.some_profile.port", "8900-9000").put("transport.profiles.some_profile.bind_host", "_local:ipv4_").put("transport.profiles.some_other_profile.port", "8700-8800").putList("transport.profiles.some_other_profile.bind_host", hosts).putList("transport.profiles.some_other_profile.publish_host", "_local:ipv4_").build())) {
        serviceC.start();
        serviceC.acceptIncomingRequests();
        Map<String, BoundTransportAddress> profileBoundAddresses = serviceC.transport.profileBoundAddresses();
        assertTrue(profileBoundAddresses.containsKey("some_profile"));
        assertTrue(profileBoundAddresses.containsKey("some_other_profile"));
        assertTrue(profileBoundAddresses.get("some_profile").publishAddress().getPort() >= 8900);
        assertTrue(profileBoundAddresses.get("some_profile").publishAddress().getPort() < 9000);
        assertTrue(profileBoundAddresses.get("some_other_profile").publishAddress().getPort() >= 8700);
        assertTrue(profileBoundAddresses.get("some_other_profile").publishAddress().getPort() < 8800);
        assertTrue(profileBoundAddresses.get("some_profile").boundAddresses().length >= 1);
        if (doIPV6) {
            assertTrue(profileBoundAddresses.get("some_other_profile").boundAddresses().length >= 2);
            int ipv4 = 0;
            int ipv6 = 0;
            for (TransportAddress addr : profileBoundAddresses.get("some_other_profile").boundAddresses()) {
                if (addr.address().getAddress() instanceof Inet4Address) {
                    ipv4++;
                } else if (addr.address().getAddress() instanceof Inet6Address) {
                    ipv6++;
                } else {
                    fail("what kind of address is this: " + addr.address().getAddress());
                }
            }
            assertTrue("num ipv4 is wrong: " + ipv4, ipv4 >= 1);
            assertTrue("num ipv6 is wrong: " + ipv6, ipv6 >= 1);
        } else {
            assertTrue(profileBoundAddresses.get("some_other_profile").boundAddresses().length >= 1);
        }
        assertTrue(profileBoundAddresses.get("some_other_profile").publishAddress().address().getAddress() instanceof Inet4Address);
    }
}
Also used : Inet4Address(java.net.Inet4Address) MockTransportService(org.opensearch.test.transport.MockTransportService) BoundTransportAddress(org.opensearch.common.transport.BoundTransportAddress) TransportAddress(org.opensearch.common.transport.TransportAddress) BoundTransportAddress(org.opensearch.common.transport.BoundTransportAddress) Inet6Address(java.net.Inet6Address) Matchers.hasToString(org.hamcrest.Matchers.hasToString) Matchers.containsString(org.hamcrest.Matchers.containsString)

Example 65 with TransportAddress

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

the class AbstractSimpleTransportTestCase method testTcpHandshakeConnectionReset.

public void testTcpHandshakeConnectionReset() throws IOException, InterruptedException {
    try (ServerSocket socket = new ServerSocket()) {
        socket.bind(getLocalEphemeral(), 1);
        socket.setReuseAddress(true);
        DiscoveryNode dummy = new DiscoveryNode("TEST", new TransportAddress(socket.getInetAddress(), socket.getLocalPort()), emptyMap(), emptySet(), version0);
        Thread t = new Thread() {

            @Override
            public void run() {
                try (Socket accept = socket.accept()) {
                    if (randomBoolean()) {
                        // sometimes wait until the other side sends the message
                        accept.getInputStream().read();
                    }
                } catch (IOException e) {
                    throw new UncheckedIOException(e);
                }
            }
        };
        t.start();
        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.timeValueHours(1));
        ConnectTransportException ex = expectThrows(ConnectTransportException.class, () -> serviceA.connectToNode(dummy, builder.build()));
        assertEquals(ex.getMessage(), "[][" + dummy.getAddress() + "] general node connection failure");
        assertThat(ex.getCause().getMessage(), startsWith("handshake failed"));
        t.join();
    }
}
Also used : DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) BoundTransportAddress(org.opensearch.common.transport.BoundTransportAddress) TransportAddress(org.opensearch.common.transport.TransportAddress) ServerSocket(java.net.ServerSocket) UncheckedIOException(java.io.UncheckedIOException) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) ServerSocket(java.net.ServerSocket) Socket(java.net.Socket)

Aggregations

TransportAddress (org.opensearch.common.transport.TransportAddress)129 Settings (org.opensearch.common.settings.Settings)51 DiscoveryNode (org.opensearch.cluster.node.DiscoveryNode)43 BoundTransportAddress (org.opensearch.common.transport.BoundTransportAddress)31 Version (org.opensearch.Version)26 ArrayList (java.util.ArrayList)24 IOException (java.io.IOException)22 List (java.util.List)21 InetAddress (java.net.InetAddress)20 HashSet (java.util.HashSet)20 CountDownLatch (java.util.concurrent.CountDownLatch)20 ClusterSettings (org.opensearch.common.settings.ClusterSettings)20 ThreadPool (org.opensearch.threadpool.ThreadPool)17 Matchers.containsString (org.hamcrest.Matchers.containsString)16 HttpServerTransport (org.opensearch.http.HttpServerTransport)16 Set (java.util.Set)15 TimeUnit (java.util.concurrent.TimeUnit)15 AtomicReference (java.util.concurrent.atomic.AtomicReference)15 OpenSearchTestCase (org.opensearch.test.OpenSearchTestCase)15 MockTransportService (org.opensearch.test.transport.MockTransportService)15