Search in sources :

Example 56 with MockTransportService

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

the class GlobalCheckpointSyncIT method testBackgroundGlobalCheckpointSync.

/*
     * This test swallows the post-operation global checkpoint syncs, and then restores the ability to send these requests at the end of the
     * test so that a background sync can fire and sync the global checkpoint.
     */
public void testBackgroundGlobalCheckpointSync() throws Exception {
    runGlobalCheckpointSyncTest(TimeValue.timeValueSeconds(randomIntBetween(1, 3)), (indexName, client) -> {
        // prevent global checkpoint syncs between all nodes
        final DiscoveryNodes nodes = client.admin().cluster().prepareState().get().getState().getNodes();
        for (final DiscoveryNode node : nodes) {
            for (final DiscoveryNode other : nodes) {
                if (node == other) {
                    continue;
                }
                final MockTransportService senderTransportService = (MockTransportService) internalCluster().getInstance(TransportService.class, node.getName());
                final MockTransportService receiverTransportService = (MockTransportService) internalCluster().getInstance(TransportService.class, other.getName());
                senderTransportService.addSendBehavior(receiverTransportService, (connection, requestId, action, request, options) -> {
                    if ("indices:admin/seq_no/global_checkpoint_sync[r]".equals(action)) {
                        throw new IllegalStateException("blocking indices:admin/seq_no/global_checkpoint_sync[r]");
                    } else {
                        connection.sendRequest(requestId, action, request, options);
                    }
                });
            }
        }
    }, (indexName, client) -> {
        // restore global checkpoint syncs between all nodes
        final DiscoveryNodes nodes = client.admin().cluster().prepareState().get().getState().getNodes();
        for (final DiscoveryNode node : nodes) {
            for (final DiscoveryNode other : nodes) {
                if (node == other) {
                    continue;
                }
                final MockTransportService senderTransportService = (MockTransportService) internalCluster().getInstance(TransportService.class, node.getName());
                final MockTransportService receiverTransportService = (MockTransportService) internalCluster().getInstance(TransportService.class, other.getName());
                senderTransportService.clearOutboundRules(receiverTransportService);
            }
        }
    });
}
Also used : DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) MockTransportService(org.elasticsearch.test.transport.MockTransportService) MockTransportService(org.elasticsearch.test.transport.MockTransportService) TransportService(org.elasticsearch.transport.TransportService) DiscoveryNodes(org.elasticsearch.cluster.node.DiscoveryNodes)

Example 57 with MockTransportService

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

the class InternalTestCluster method reset.

private synchronized void reset(boolean wipeData) throws IOException {
    // clear all rules for mock transport services
    for (NodeAndClient nodeAndClient : nodes.values()) {
        TransportService transportService = nodeAndClient.node.injector().getInstance(TransportService.class);
        if (transportService instanceof MockTransportService) {
            final MockTransportService mockTransportService = (MockTransportService) transportService;
            mockTransportService.clearAllRules();
        }
    }
    randomlyResetClients();
    final int newSize = sharedNodesSeeds.length;
    if (nextNodeId.get() == newSize && nodes.size() == newSize) {
        if (wipeData) {
            wipePendingDataDirectories();
        }
        logger.debug("Cluster hasn't changed - moving out - nodes: [{}] nextNodeId: [{}] numSharedNodes: [{}]", nodes.keySet(), nextNodeId.get(), newSize);
        return;
    }
    logger.debug("Cluster is NOT consistent - restarting shared nodes - nodes: [{}] nextNodeId: [{}] numSharedNodes: [{}]", nodes.keySet(), nextNodeId.get(), newSize);
    // trash all nodes with id >= sharedNodesSeeds.length - they are non shared
    final List<NodeAndClient> toClose = new ArrayList<>();
    for (NodeAndClient nodeAndClient : nodes.values()) {
        if (nodeAndClient.nodeAndClientId() >= sharedNodesSeeds.length) {
            logger.debug("Close Node [{}] not shared", nodeAndClient.name);
            toClose.add(nodeAndClient);
        }
    }
    stopNodesAndClients(toClose);
    // clean up what the nodes left that is unused
    if (wipeData) {
        wipePendingDataDirectories();
    }
    assertTrue("expected at least one master-eligible node left in " + nodes, nodes.isEmpty() || nodes.values().stream().anyMatch(NodeAndClient::isMasterEligible));
    final int prevNodeCount = nodes.size();
    // start any missing node
    assert newSize == numSharedDedicatedMasterNodes + numSharedDataNodes + numSharedCoordOnlyNodes;
    // we want to start nodes in one go
    final List<NodeAndClient> toStartAndPublish = new ArrayList<>();
    final Runnable onTransportServiceStarted = () -> rebuildUnicastHostFiles(toStartAndPublish);
    final List<Settings> settings = new ArrayList<>();
    for (int i = 0; i < numSharedDedicatedMasterNodes; i++) {
        final Settings.Builder extraSettings = Settings.builder();
        extraSettings.put(Node.NODE_MASTER_SETTING.getKey(), true);
        extraSettings.put(Node.NODE_DATA_SETTING.getKey(), false);
        settings.add(getNodeSettings(i, sharedNodesSeeds[i], extraSettings.build()));
    }
    for (int i = numSharedDedicatedMasterNodes; i < numSharedDedicatedMasterNodes + numSharedDataNodes; i++) {
        final Settings.Builder extraSettings = Settings.builder();
        if (numSharedDedicatedMasterNodes > 0) {
            // if we don't have dedicated master nodes, keep things default
            extraSettings.put(Node.NODE_MASTER_SETTING.getKey(), false).build();
            extraSettings.put(Node.NODE_DATA_SETTING.getKey(), true).build();
        }
        settings.add(getNodeSettings(i, sharedNodesSeeds[i], extraSettings.build()));
    }
    for (int i = numSharedDedicatedMasterNodes + numSharedDataNodes; i < numSharedDedicatedMasterNodes + numSharedDataNodes + numSharedCoordOnlyNodes; i++) {
        final Builder extraSettings = Settings.builder().put(Node.NODE_MASTER_SETTING.getKey(), false).put(Node.NODE_DATA_SETTING.getKey(), false);
        settings.add(getNodeSettings(i, sharedNodesSeeds[i], extraSettings.build()));
    }
    int autoBootstrapMasterNodeIndex = -1;
    final List<String> masterNodeNames = settings.stream().filter(Node.NODE_MASTER_SETTING::get).map(Node.NODE_NAME_SETTING::get).collect(Collectors.toList());
    if (prevNodeCount == 0 && autoManageMasterNodes) {
        if (numSharedDedicatedMasterNodes > 0) {
            autoBootstrapMasterNodeIndex = RandomNumbers.randomIntBetween(random, 0, numSharedDedicatedMasterNodes - 1);
        } else if (numSharedDataNodes > 0) {
            autoBootstrapMasterNodeIndex = RandomNumbers.randomIntBetween(random, 0, numSharedDataNodes - 1);
        }
    }
    final List<Settings> updatedSettings = bootstrapMasterNodeWithSpecifiedIndex(settings);
    for (int i = 0; i < numSharedDedicatedMasterNodes + numSharedDataNodes + numSharedCoordOnlyNodes; i++) {
        Settings nodeSettings = updatedSettings.get(i);
        if (i == autoBootstrapMasterNodeIndex) {
            nodeSettings = Settings.builder().putList(INITIAL_MASTER_NODES_SETTING.getKey(), masterNodeNames).put(nodeSettings).build();
        }
        final NodeAndClient nodeAndClient = buildNode(i, nodeSettings, true, onTransportServiceStarted);
        toStartAndPublish.add(nodeAndClient);
    }
    startAndPublishNodesAndClients(toStartAndPublish);
    nextNodeId.set(newSize);
    assert size() == newSize;
    if (autoManageMasterNodes && newSize > 0) {
        validateClusterFormed();
    }
    logger.debug("Cluster is consistent again - nodes: [{}] nextNodeId: [{}] numSharedNodes: [{}]", nodes.keySet(), nextNodeId.get(), newSize);
}
Also used : MockTransportService(org.elasticsearch.test.transport.MockTransportService) Builder(org.elasticsearch.common.settings.Settings.Builder) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) Node(org.elasticsearch.node.Node) MockNode(org.elasticsearch.node.MockNode) ArrayList(java.util.ArrayList) Builder(org.elasticsearch.common.settings.Settings.Builder) TransportService(org.elasticsearch.transport.TransportService) MockTransportService(org.elasticsearch.test.transport.MockTransportService) RecoverySettings(org.elasticsearch.indices.recovery.RecoverySettings) TransportSettings(org.elasticsearch.transport.TransportSettings) DiskThresholdSettings(org.elasticsearch.cluster.routing.allocation.DiskThresholdSettings) Settings(org.elasticsearch.common.settings.Settings)

Example 58 with MockTransportService

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

the class VotingConfigurationIT method testElectsNodeNotInVotingConfiguration.

public void testElectsNodeNotInVotingConfiguration() throws Exception {
    internalCluster().setBootstrapMasterNodeIndex(0);
    final List<String> nodeNames = internalCluster().startNodes(4);
    // a 4-node cluster settles on a 3-node configuration; we then prevent the nodes in the configuration from winning an election
    // by failing at the pre-voting stage, so that the extra node must be elected instead when the master shuts down. This extra node
    // should then add itself into the voting configuration.
    assertFalse(internalCluster().client().admin().cluster().prepareHealth().setWaitForNodes("4").setWaitForEvents(Priority.LANGUID).get().isTimedOut());
    String excludedNodeName = null;
    final ClusterState clusterState = internalCluster().client().admin().cluster().prepareState().clear().setNodes(true).setMetadata(true).get().getState();
    final Set<String> votingConfiguration = clusterState.getLastCommittedConfiguration().getNodeIds();
    assertThat(votingConfiguration, hasSize(3));
    assertThat(clusterState.nodes().getSize(), equalTo(4));
    assertThat(votingConfiguration, hasItem(clusterState.nodes().getMasterNodeId()));
    for (DiscoveryNode discoveryNode : clusterState.nodes()) {
        if (votingConfiguration.contains(discoveryNode.getId()) == false) {
            assertThat(excludedNodeName, nullValue());
            excludedNodeName = discoveryNode.getName();
        }
    }
    for (final String sender : nodeNames) {
        if (sender.equals(excludedNodeName)) {
            continue;
        }
        final MockTransportService senderTransportService = (MockTransportService) internalCluster().getInstance(TransportService.class, sender);
        for (final String receiver : nodeNames) {
            senderTransportService.addSendBehavior(internalCluster().getInstance(TransportService.class, receiver), (connection, requestId, action, request, options) -> {
                if (action.equals(PreVoteCollector.REQUEST_PRE_VOTE_ACTION_NAME)) {
                    throw new ElasticsearchException("rejected");
                }
                connection.sendRequest(requestId, action, request, options);
            });
        }
    }
    internalCluster().stopCurrentMasterNode();
    assertFalse(internalCluster().client().admin().cluster().prepareHealth().setWaitForNodes("3").setWaitForEvents(Priority.LANGUID).get().isTimedOut());
    final ClusterState newClusterState = internalCluster().client().admin().cluster().prepareState().clear().setNodes(true).setMetadata(true).get().getState();
    assertThat(newClusterState.nodes().getMasterNode().getName(), equalTo(excludedNodeName));
    assertThat(newClusterState.getLastCommittedConfiguration().getNodeIds(), hasItem(newClusterState.nodes().getMasterNodeId()));
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) MockTransportService(org.elasticsearch.test.transport.MockTransportService) MockTransportService(org.elasticsearch.test.transport.MockTransportService) TransportService(org.elasticsearch.transport.TransportService) ElasticsearchException(org.elasticsearch.ElasticsearchException)

Example 59 with MockTransportService

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

the class TransportServiceHandshakeTests method startServices.

private NetworkHandle startServices(String nodeNameAndId, Settings settings, Version version) {
    var allSettings = Settings.builder().put(TransportSettings.PORT.getKey(), ESTestCase.getPortRange()).put(settings).build();
    var transport = new Netty4Transport(allSettings, // handle the real world scenario instead of a faked one.
    Version.CURRENT, threadPool, new NetworkService(Collections.emptyList()), PageCacheRecycler.NON_RECYCLING_INSTANCE, new NamedWriteableRegistry(Collections.emptyList()), new NoneCircuitBreakerService(), new NettyBootstrap(), new AlwaysOKAuthentication(userName -> User.CRATE_USER), new SslContextProvider(settings));
    TransportService transportService = new MockTransportService(allSettings, transport, threadPool, (boundAddress) -> new DiscoveryNode(nodeNameAndId, nodeNameAndId, boundAddress.publishAddress(), emptyMap(), emptySet(), version), null);
    transportService.start();
    transportService.acceptIncomingRequests();
    transportServices.add(transportService);
    return new NetworkHandle(transportService, transportService.getLocalNode());
}
Also used : NamedWriteableRegistry(org.elasticsearch.common.io.stream.NamedWriteableRegistry) BeforeClass(org.junit.BeforeClass) ArrayList(java.util.ArrayList) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) NetworkService(org.elasticsearch.common.network.NetworkService) Settings(org.elasticsearch.common.settings.Settings) NoneCircuitBreakerService(org.elasticsearch.indices.breaker.NoneCircuitBreakerService) NamedWriteableRegistry(org.elasticsearch.common.io.stream.NamedWriteableRegistry) After(org.junit.After) NettyBootstrap(io.crate.netty.NettyBootstrap) ThreadPool(org.elasticsearch.threadpool.ThreadPool) ESTestCase(org.elasticsearch.test.ESTestCase) MockTransportService(org.elasticsearch.test.transport.MockTransportService) Collections.emptyMap(java.util.Collections.emptyMap) TestThreadPool(org.elasticsearch.threadpool.TestThreadPool) AfterClass(org.junit.AfterClass) Collections.emptySet(java.util.Collections.emptySet) Netty4Transport(org.elasticsearch.transport.netty4.Netty4Transport) User(io.crate.user.User) PlainActionFuture(org.elasticsearch.action.support.PlainActionFuture) IOException(java.io.IOException) PageCacheRecycler(org.elasticsearch.common.util.PageCacheRecycler) Test(org.junit.Test) VersionUtils(org.elasticsearch.test.VersionUtils) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) Version(org.elasticsearch.Version) SslContextProvider(io.crate.protocols.ssl.SslContextProvider) AlwaysOKAuthentication(io.crate.auth.AlwaysOKAuthentication) Matchers.containsString(org.hamcrest.Matchers.containsString) Collections(java.util.Collections) ActionListener(org.elasticsearch.action.ActionListener) AlwaysOKAuthentication(io.crate.auth.AlwaysOKAuthentication) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) MockTransportService(org.elasticsearch.test.transport.MockTransportService) Netty4Transport(org.elasticsearch.transport.netty4.Netty4Transport) MockTransportService(org.elasticsearch.test.transport.MockTransportService) NetworkService(org.elasticsearch.common.network.NetworkService) SslContextProvider(io.crate.protocols.ssl.SslContextProvider) NoneCircuitBreakerService(org.elasticsearch.indices.breaker.NoneCircuitBreakerService) NettyBootstrap(io.crate.netty.NettyBootstrap)

Aggregations

MockTransportService (org.elasticsearch.test.transport.MockTransportService)59 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)32 TransportService (org.elasticsearch.transport.TransportService)30 Settings (org.elasticsearch.common.settings.Settings)24 IOException (java.io.IOException)23 CountDownLatch (java.util.concurrent.CountDownLatch)21 ArrayList (java.util.ArrayList)13 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)13 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)12 TransportRequest (org.elasticsearch.transport.TransportRequest)11 TransportRequestOptions (org.elasticsearch.transport.TransportRequestOptions)11 NamedWriteableRegistry (org.elasticsearch.common.io.stream.NamedWriteableRegistry)10 NetworkService (org.elasticsearch.common.network.NetworkService)10 TestThreadPool (org.elasticsearch.threadpool.TestThreadPool)10 ClusterState (org.elasticsearch.cluster.ClusterState)9 NoneCircuitBreakerService (org.elasticsearch.indices.breaker.NoneCircuitBreakerService)9 ThreadPool (org.elasticsearch.threadpool.ThreadPool)9 Test (org.junit.Test)9 List (java.util.List)8 Collections (java.util.Collections)7