Search in sources :

Example 1 with AssertingAckListener

use of org.elasticsearch.discovery.zen.PublishClusterStateActionTests.AssertingAckListener in project elasticsearch by elastic.

the class ZenDiscoveryUnitTests method testPendingCSQueueIsClearedWhenClusterStatePublished.

public void testPendingCSQueueIsClearedWhenClusterStatePublished() throws Exception {
    ThreadPool threadPool = new TestThreadPool(getClass().getName());
    // randomly make minimum_master_nodes a value higher than we have nodes for, so it will force failure
    int minMasterNodes = randomBoolean() ? 3 : 1;
    Settings settings = Settings.builder().put(DISCOVERY_ZEN_MINIMUM_MASTER_NODES_SETTING.getKey(), Integer.toString(minMasterNodes)).build();
    ArrayDeque<Closeable> toClose = new ArrayDeque<>();
    try {
        final MockTransportService masterTransport = MockTransportService.createNewService(settings, Version.CURRENT, threadPool, null);
        masterTransport.start();
        DiscoveryNode masterNode = masterTransport.getLocalNode();
        toClose.addFirst(masterTransport);
        ClusterState state = ClusterStateCreationUtils.state(masterNode, null, masterNode);
        // build the zen discovery and cluster service
        ClusterService masterClusterService = createClusterService(threadPool, masterNode);
        toClose.addFirst(masterClusterService);
        state = ClusterState.builder(masterClusterService.getClusterName()).nodes(state.nodes()).build();
        setState(masterClusterService, state);
        ZenDiscovery masterZen = buildZenDiscovery(settings, masterTransport, masterClusterService, threadPool);
        toClose.addFirst(masterZen);
        masterTransport.acceptIncomingRequests();
        // inject a pending cluster state
        masterZen.pendingClusterStatesQueue().addPending(ClusterState.builder(new ClusterName("foreign")).build());
        // a new cluster state with a new discovery node (we will test if the cluster state
        // was updated by the presence of this node in NodesFaultDetection)
        ClusterState newState = ClusterState.builder(masterClusterService.state()).incrementVersion().nodes(DiscoveryNodes.builder(masterClusterService.state().nodes()).masterNodeId(masterNode.getId())).build();
        try {
            // publishing a new cluster state
            ClusterChangedEvent clusterChangedEvent = new ClusterChangedEvent("testing", newState, state);
            AssertingAckListener listener = new AssertingAckListener(newState.nodes().getSize() - 1);
            masterZen.publish(clusterChangedEvent, listener);
            listener.await(1, TimeUnit.HOURS);
            // publish was a success, check that queue as cleared
            assertThat(masterZen.pendingClusterStates(), emptyArray());
        } catch (Discovery.FailedToCommitClusterStateException e) {
            // not successful, so the pending queue should stay
            assertThat(masterZen.pendingClusterStates(), arrayWithSize(1));
            assertThat(masterZen.pendingClusterStates()[0].getClusterName().value(), equalTo("foreign"));
        }
    } finally {
        IOUtils.close(toClose);
        terminate(threadPool);
    }
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) ZenDiscovery.shouldIgnoreOrRejectNewClusterState(org.elasticsearch.discovery.zen.ZenDiscovery.shouldIgnoreOrRejectNewClusterState) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) MockTransportService(org.elasticsearch.test.transport.MockTransportService) Closeable(java.io.Closeable) ThreadPool(org.elasticsearch.threadpool.ThreadPool) TestThreadPool(org.elasticsearch.threadpool.TestThreadPool) Discovery(org.elasticsearch.discovery.Discovery) ClusterChangedEvent(org.elasticsearch.cluster.ClusterChangedEvent) TestThreadPool(org.elasticsearch.threadpool.TestThreadPool) ArrayDeque(java.util.ArrayDeque) ClusterServiceUtils.createClusterService(org.elasticsearch.test.ClusterServiceUtils.createClusterService) ClusterService(org.elasticsearch.cluster.service.ClusterService) AssertingAckListener(org.elasticsearch.discovery.zen.PublishClusterStateActionTests.AssertingAckListener) ClusterName(org.elasticsearch.cluster.ClusterName) Settings(org.elasticsearch.common.settings.Settings)

Example 2 with AssertingAckListener

use of org.elasticsearch.discovery.zen.PublishClusterStateActionTests.AssertingAckListener in project elasticsearch by elastic.

the class ZenDiscoveryUnitTests method testNodesUpdatedAfterClusterStatePublished.

public void testNodesUpdatedAfterClusterStatePublished() throws Exception {
    ThreadPool threadPool = new TestThreadPool(getClass().getName());
    // randomly make minimum_master_nodes a value higher than we have nodes for, so it will force failure
    int minMasterNodes = randomBoolean() ? 3 : 1;
    Settings settings = Settings.builder().put(DISCOVERY_ZEN_MINIMUM_MASTER_NODES_SETTING.getKey(), Integer.toString(minMasterNodes)).build();
    ArrayDeque<Closeable> toClose = new ArrayDeque<>();
    try {
        Set<DiscoveryNode> expectedFDNodes = null;
        final MockTransportService masterTransport = MockTransportService.createNewService(settings, Version.CURRENT, threadPool, null);
        masterTransport.start();
        DiscoveryNode masterNode = masterTransport.getLocalNode();
        toClose.addFirst(masterTransport);
        ClusterState state = ClusterStateCreationUtils.state(masterNode, masterNode, masterNode);
        // build the zen discovery and cluster service
        ClusterService masterClusterService = createClusterService(threadPool, masterNode);
        toClose.addFirst(masterClusterService);
        // TODO: clustername shouldn't be stored twice in cluster service, but for now, work around it
        state = ClusterState.builder(masterClusterService.getClusterName()).nodes(state.nodes()).build();
        setState(masterClusterService, state);
        ZenDiscovery masterZen = buildZenDiscovery(settings, masterTransport, masterClusterService, threadPool);
        toClose.addFirst(masterZen);
        masterTransport.acceptIncomingRequests();
        final MockTransportService otherTransport = MockTransportService.createNewService(settings, Version.CURRENT, threadPool, null);
        otherTransport.start();
        toClose.addFirst(otherTransport);
        DiscoveryNode otherNode = otherTransport.getLocalNode();
        final ClusterState otherState = ClusterState.builder(masterClusterService.getClusterName()).nodes(DiscoveryNodes.builder().add(otherNode).localNodeId(otherNode.getId())).build();
        ClusterService otherClusterService = createClusterService(threadPool, masterNode);
        toClose.addFirst(otherClusterService);
        setState(otherClusterService, otherState);
        ZenDiscovery otherZen = buildZenDiscovery(settings, otherTransport, otherClusterService, threadPool);
        toClose.addFirst(otherZen);
        otherTransport.acceptIncomingRequests();
        masterTransport.connectToNode(otherNode);
        otherTransport.connectToNode(masterNode);
        // a new cluster state with a new discovery node (we will test if the cluster state
        // was updated by the presence of this node in NodesFaultDetection)
        ClusterState newState = ClusterState.builder(masterClusterService.state()).incrementVersion().nodes(DiscoveryNodes.builder(state.nodes()).add(otherNode).masterNodeId(masterNode.getId())).build();
        try {
            // publishing a new cluster state
            ClusterChangedEvent clusterChangedEvent = new ClusterChangedEvent("testing", newState, state);
            AssertingAckListener listener = new AssertingAckListener(newState.nodes().getSize() - 1);
            expectedFDNodes = masterZen.getFaultDetectionNodes();
            masterZen.publish(clusterChangedEvent, listener);
            listener.await(1, TimeUnit.HOURS);
            // publish was a success, update expected FD nodes based on new cluster state
            expectedFDNodes = fdNodesForState(newState, masterNode);
        } catch (Discovery.FailedToCommitClusterStateException e) {
            // not successful, so expectedFDNodes above should remain what it was originally assigned
            // ensure min master nodes is the higher value, otherwise we shouldn't fail
            assertEquals(3, minMasterNodes);
        }
        assertEquals(expectedFDNodes, masterZen.getFaultDetectionNodes());
    } finally {
        IOUtils.close(toClose);
        terminate(threadPool);
    }
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) ZenDiscovery.shouldIgnoreOrRejectNewClusterState(org.elasticsearch.discovery.zen.ZenDiscovery.shouldIgnoreOrRejectNewClusterState) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) MockTransportService(org.elasticsearch.test.transport.MockTransportService) Closeable(java.io.Closeable) ThreadPool(org.elasticsearch.threadpool.ThreadPool) TestThreadPool(org.elasticsearch.threadpool.TestThreadPool) Discovery(org.elasticsearch.discovery.Discovery) ClusterChangedEvent(org.elasticsearch.cluster.ClusterChangedEvent) TestThreadPool(org.elasticsearch.threadpool.TestThreadPool) ArrayDeque(java.util.ArrayDeque) ClusterServiceUtils.createClusterService(org.elasticsearch.test.ClusterServiceUtils.createClusterService) ClusterService(org.elasticsearch.cluster.service.ClusterService) AssertingAckListener(org.elasticsearch.discovery.zen.PublishClusterStateActionTests.AssertingAckListener) Settings(org.elasticsearch.common.settings.Settings)

Aggregations

Closeable (java.io.Closeable)2 ArrayDeque (java.util.ArrayDeque)2 ClusterChangedEvent (org.elasticsearch.cluster.ClusterChangedEvent)2 ClusterState (org.elasticsearch.cluster.ClusterState)2 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)2 ClusterService (org.elasticsearch.cluster.service.ClusterService)2 Settings (org.elasticsearch.common.settings.Settings)2 Discovery (org.elasticsearch.discovery.Discovery)2 AssertingAckListener (org.elasticsearch.discovery.zen.PublishClusterStateActionTests.AssertingAckListener)2 ZenDiscovery.shouldIgnoreOrRejectNewClusterState (org.elasticsearch.discovery.zen.ZenDiscovery.shouldIgnoreOrRejectNewClusterState)2 ClusterServiceUtils.createClusterService (org.elasticsearch.test.ClusterServiceUtils.createClusterService)2 MockTransportService (org.elasticsearch.test.transport.MockTransportService)2 TestThreadPool (org.elasticsearch.threadpool.TestThreadPool)2 ThreadPool (org.elasticsearch.threadpool.ThreadPool)2 ClusterName (org.elasticsearch.cluster.ClusterName)1