Search in sources :

Example 26 with MockTransportService

use of org.elasticsearch.test.transport.MockTransportService in project elasticsearch by elastic.

the class RemoteClusterConnectionTests method testDiscoverSingleNodeWithIncompatibleSeed.

public void testDiscoverSingleNodeWithIncompatibleSeed() throws Exception {
    List<DiscoveryNode> knownNodes = new CopyOnWriteArrayList<>();
    try (MockTransportService seedTransport = startTransport("seed_node", knownNodes, Version.CURRENT);
        MockTransportService incomaptibleTransport = startTransport("incompat_seed_node", knownNodes, Version.fromString("2.0.0"));
        MockTransportService discoverableTransport = startTransport("discoverable_node", knownNodes, Version.CURRENT)) {
        DiscoveryNode seedNode = seedTransport.getLocalDiscoNode();
        DiscoveryNode discoverableNode = discoverableTransport.getLocalDiscoNode();
        DiscoveryNode incompatibleSeedNode = incomaptibleTransport.getLocalDiscoNode();
        knownNodes.add(seedTransport.getLocalDiscoNode());
        knownNodes.add(discoverableTransport.getLocalDiscoNode());
        knownNodes.add(incomaptibleTransport.getLocalDiscoNode());
        Collections.shuffle(knownNodes, random());
        List<DiscoveryNode> seedNodes = Arrays.asList(incompatibleSeedNode, seedNode);
        Collections.shuffle(seedNodes, random());
        try (MockTransportService service = MockTransportService.createNewService(Settings.EMPTY, Version.CURRENT, threadPool, null)) {
            service.start();
            service.acceptIncomingRequests();
            try (RemoteClusterConnection connection = new RemoteClusterConnection(Settings.EMPTY, "test-cluster", seedNodes, service, Integer.MAX_VALUE, n -> true)) {
                updateSeedNodes(connection, seedNodes);
                assertTrue(service.nodeConnected(seedNode));
                assertTrue(service.nodeConnected(discoverableNode));
                assertFalse(service.nodeConnected(incompatibleSeedNode));
                assertTrue(connection.assertNoRunningConnections());
            }
        }
    }
}
Also used : DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) MockTransportService(org.elasticsearch.test.transport.MockTransportService) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList)

Example 27 with MockTransportService

use of org.elasticsearch.test.transport.MockTransportService in project elasticsearch by elastic.

the class RemoteClusterConnectionTests method startTransport.

public static MockTransportService startTransport(String id, List<DiscoveryNode> knownNodes, Version version, ThreadPool threadPool) {
    boolean success = false;
    MockTransportService newService = MockTransportService.createNewService(Settings.EMPTY, version, threadPool, null);
    try {
        newService.registerRequestHandler(ClusterSearchShardsAction.NAME, ClusterSearchShardsRequest::new, ThreadPool.Names.SAME, (request, channel) -> {
            channel.sendResponse(new ClusterSearchShardsResponse(new ClusterSearchShardsGroup[0], knownNodes.toArray(new DiscoveryNode[0]), Collections.emptyMap()));
        });
        newService.registerRequestHandler(ClusterStateAction.NAME, ClusterStateRequest::new, ThreadPool.Names.SAME, (request, channel) -> {
            DiscoveryNodes.Builder builder = DiscoveryNodes.builder();
            for (DiscoveryNode node : knownNodes) {
                builder.add(node);
            }
            ClusterState build = ClusterState.builder(ClusterName.DEFAULT).nodes(builder.build()).build();
            channel.sendResponse(new ClusterStateResponse(ClusterName.DEFAULT, build, 0L));
        });
        newService.start();
        newService.acceptIncomingRequests();
        success = true;
        return newService;
    } finally {
        if (success == false) {
            newService.close();
        }
    }
}
Also used : ClusterSearchShardsResponse(org.elasticsearch.action.admin.cluster.shards.ClusterSearchShardsResponse) ClusterState(org.elasticsearch.cluster.ClusterState) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) MockTransportService(org.elasticsearch.test.transport.MockTransportService) ClusterStateResponse(org.elasticsearch.action.admin.cluster.state.ClusterStateResponse) ClusterStateRequest(org.elasticsearch.action.admin.cluster.state.ClusterStateRequest) ClusterSearchShardsRequest(org.elasticsearch.action.admin.cluster.shards.ClusterSearchShardsRequest) ClusterSearchShardsGroup(org.elasticsearch.action.admin.cluster.shards.ClusterSearchShardsGroup) DiscoveryNodes(org.elasticsearch.cluster.node.DiscoveryNodes)

Example 28 with MockTransportService

use of org.elasticsearch.test.transport.MockTransportService in project elasticsearch by elastic.

the class RemoteClusterConnectionTests method testFetchShards.

public void testFetchShards() throws Exception {
    List<DiscoveryNode> knownNodes = new CopyOnWriteArrayList<>();
    try (MockTransportService seedTransport = startTransport("seed_node", knownNodes, Version.CURRENT);
        MockTransportService discoverableTransport = startTransport("discoverable_node", knownNodes, Version.CURRENT)) {
        DiscoveryNode seedNode = seedTransport.getLocalDiscoNode();
        knownNodes.add(seedTransport.getLocalDiscoNode());
        knownNodes.add(discoverableTransport.getLocalDiscoNode());
        Collections.shuffle(knownNodes, random());
        try (MockTransportService service = MockTransportService.createNewService(Settings.EMPTY, Version.CURRENT, threadPool, null)) {
            service.start();
            service.acceptIncomingRequests();
            try (RemoteClusterConnection connection = new RemoteClusterConnection(Settings.EMPTY, "test-cluster", Arrays.asList(seedNode), service, Integer.MAX_VALUE, n -> true)) {
                if (randomBoolean()) {
                    updateSeedNodes(connection, Arrays.asList(seedNode));
                }
                SearchRequest request = new SearchRequest("test-index");
                CountDownLatch responseLatch = new CountDownLatch(1);
                AtomicReference<ClusterSearchShardsResponse> reference = new AtomicReference<>();
                AtomicReference<Exception> failReference = new AtomicReference<>();
                ActionListener<ClusterSearchShardsResponse> shardsListener = ActionListener.wrap(x -> {
                    reference.set(x);
                    responseLatch.countDown();
                }, x -> {
                    failReference.set(x);
                    responseLatch.countDown();
                });
                connection.fetchSearchShards(request, Arrays.asList("test-index"), shardsListener);
                responseLatch.await();
                assertNull(failReference.get());
                assertNotNull(reference.get());
                ClusterSearchShardsResponse clusterSearchShardsResponse = reference.get();
                assertEquals(knownNodes, Arrays.asList(clusterSearchShardsResponse.getNodes()));
                assertTrue(connection.assertNoRunningConnections());
            }
        }
    }
}
Also used : ClusterSearchShardsResponse(org.elasticsearch.action.admin.cluster.shards.ClusterSearchShardsResponse) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) MockTransportService(org.elasticsearch.test.transport.MockTransportService) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException) AlreadyConnectedException(java.nio.channels.AlreadyConnectedException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) IOException(java.io.IOException) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) UnknownHostException(java.net.UnknownHostException) UncheckedIOException(java.io.UncheckedIOException) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList)

Example 29 with MockTransportService

use of org.elasticsearch.test.transport.MockTransportService in project elasticsearch by elastic.

the class RemoteClusterConnectionTests method testFilterDiscoveredNodes.

public void testFilterDiscoveredNodes() throws Exception {
    List<DiscoveryNode> knownNodes = new CopyOnWriteArrayList<>();
    try (MockTransportService seedTransport = startTransport("seed_node", knownNodes, Version.CURRENT);
        MockTransportService discoverableTransport = startTransport("discoverable_node", knownNodes, Version.CURRENT)) {
        DiscoveryNode seedNode = seedTransport.getLocalDiscoNode();
        DiscoveryNode discoverableNode = discoverableTransport.getLocalDiscoNode();
        knownNodes.add(seedTransport.getLocalDiscoNode());
        knownNodes.add(discoverableTransport.getLocalDiscoNode());
        DiscoveryNode rejectedNode = randomBoolean() ? seedNode : discoverableNode;
        Collections.shuffle(knownNodes, random());
        try (MockTransportService service = MockTransportService.createNewService(Settings.EMPTY, Version.CURRENT, threadPool, null)) {
            service.start();
            service.acceptIncomingRequests();
            try (RemoteClusterConnection connection = new RemoteClusterConnection(Settings.EMPTY, "test-cluster", Arrays.asList(seedNode), service, Integer.MAX_VALUE, n -> n.equals(rejectedNode) == false)) {
                updateSeedNodes(connection, Arrays.asList(seedNode));
                if (rejectedNode.equals(seedNode)) {
                    assertFalse(service.nodeConnected(seedNode));
                    assertTrue(service.nodeConnected(discoverableNode));
                } else {
                    assertTrue(service.nodeConnected(seedNode));
                    assertFalse(service.nodeConnected(discoverableNode));
                }
                assertTrue(connection.assertNoRunningConnections());
            }
        }
    }
}
Also used : DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) MockTransportService(org.elasticsearch.test.transport.MockTransportService) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList)

Example 30 with MockTransportService

use of org.elasticsearch.test.transport.MockTransportService in project elasticsearch by elastic.

the class RemoteClusterConnectionTests method testConnectWithIncompatibleTransports.

public void testConnectWithIncompatibleTransports() throws Exception {
    List<DiscoveryNode> knownNodes = new CopyOnWriteArrayList<>();
    try (MockTransportService seedTransport = startTransport("seed_node", knownNodes, Version.fromString("2.0.0"))) {
        DiscoveryNode seedNode = seedTransport.getLocalDiscoNode();
        knownNodes.add(seedTransport.getLocalDiscoNode());
        try (MockTransportService service = MockTransportService.createNewService(Settings.EMPTY, Version.CURRENT, threadPool, null)) {
            service.start();
            service.acceptIncomingRequests();
            try (RemoteClusterConnection connection = new RemoteClusterConnection(Settings.EMPTY, "test-cluster", Arrays.asList(seedNode), service, Integer.MAX_VALUE, n -> true)) {
                expectThrows(Exception.class, () -> updateSeedNodes(connection, Arrays.asList(seedNode)));
                assertFalse(service.nodeConnected(seedNode));
                assertTrue(connection.assertNoRunningConnections());
            }
        }
    }
}
Also used : DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) MockTransportService(org.elasticsearch.test.transport.MockTransportService) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList)

Aggregations

MockTransportService (org.elasticsearch.test.transport.MockTransportService)42 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)25 IOException (java.io.IOException)19 Settings (org.elasticsearch.common.settings.Settings)17 CountDownLatch (java.util.concurrent.CountDownLatch)16 TransportService (org.elasticsearch.transport.TransportService)15 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)12 ArrayList (java.util.ArrayList)10 TransportRequest (org.elasticsearch.transport.TransportRequest)10 TransportRequestOptions (org.elasticsearch.transport.TransportRequestOptions)10 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)9 NamedWriteableRegistry (org.elasticsearch.common.io.stream.NamedWriteableRegistry)9 NetworkService (org.elasticsearch.common.network.NetworkService)9 NoneCircuitBreakerService (org.elasticsearch.indices.breaker.NoneCircuitBreakerService)8 TestThreadPool (org.elasticsearch.threadpool.TestThreadPool)8 ThreadPool (org.elasticsearch.threadpool.ThreadPool)8 TransportAddress (org.elasticsearch.common.transport.TransportAddress)7 InetSocketAddress (java.net.InetSocketAddress)6 AtomicReference (java.util.concurrent.atomic.AtomicReference)6 UncheckedIOException (java.io.UncheckedIOException)5