Search in sources :

Example 16 with ClusterChangedEvent

use of org.elasticsearch.cluster.ClusterChangedEvent in project elasticsearch by elastic.

the class GatewayMetaStateTests method generateEvent.

ClusterChangedEvent generateEvent(boolean initializing, boolean versionChanged, boolean masterEligible) {
    //ridiculous settings to make sure we don't run into uninitialized because fo default
    AllocationService strategy = createAllocationService(Settings.builder().put("cluster.routing.allocation.node_concurrent_recoveries", 100).put(ClusterRebalanceAllocationDecider.CLUSTER_ROUTING_ALLOCATION_ALLOW_REBALANCE_SETTING.getKey(), "always").put("cluster.routing.allocation.cluster_concurrent_rebalance", 100).put("cluster.routing.allocation.node_initial_primaries_recoveries", 100).build());
    ClusterState newClusterState, previousClusterState;
    MetaData metaDataOldClusterState = MetaData.builder().put(IndexMetaData.builder("test").settings(settings(Version.CURRENT)).numberOfShards(5).numberOfReplicas(2)).build();
    RoutingTable routingTableOldClusterState = RoutingTable.builder().addAsNew(metaDataOldClusterState.index("test")).build();
    // assign all shards
    ClusterState init = ClusterState.builder(org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metaData(metaDataOldClusterState).routingTable(routingTableOldClusterState).nodes(generateDiscoveryNodes(masterEligible)).build();
    // new cluster state will have initializing shards on node 1
    RoutingTable routingTableNewClusterState = strategy.reroute(init, "reroute").routingTable();
    if (initializing == false) {
        // pretend all initialized, nothing happened
        ClusterState temp = ClusterState.builder(init).routingTable(routingTableNewClusterState).metaData(metaDataOldClusterState).build();
        routingTableNewClusterState = strategy.applyStartedShards(temp, temp.getRoutingNodes().shardsWithState(INITIALIZING)).routingTable();
        routingTableOldClusterState = routingTableNewClusterState;
    } else {
    // nothing to do, we have one routing table with unassigned and one with initializing
    }
    // create new meta data either with version changed or not
    MetaData metaDataNewClusterState = MetaData.builder().put(init.metaData().index("test"), versionChanged).build();
    // create the cluster states with meta data and routing tables as computed before
    previousClusterState = ClusterState.builder(init).metaData(metaDataOldClusterState).routingTable(routingTableOldClusterState).nodes(generateDiscoveryNodes(masterEligible)).build();
    newClusterState = ClusterState.builder(previousClusterState).routingTable(routingTableNewClusterState).metaData(metaDataNewClusterState).version(previousClusterState.getVersion() + 1).build();
    ClusterChangedEvent event = new ClusterChangedEvent("test", newClusterState, previousClusterState);
    assertThat(event.state().version(), equalTo(event.previousState().version() + 1));
    return event;
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) RoutingTable(org.elasticsearch.cluster.routing.RoutingTable) MetaData(org.elasticsearch.cluster.metadata.MetaData) TestCustomMetaData(org.elasticsearch.test.TestCustomMetaData) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) ClusterChangedEvent(org.elasticsearch.cluster.ClusterChangedEvent) AllocationService(org.elasticsearch.cluster.routing.allocation.AllocationService)

Example 17 with ClusterChangedEvent

use of org.elasticsearch.cluster.ClusterChangedEvent in project elasticsearch by elastic.

the class GatewayMetaStateTests method testVersionChangeIsAlwaysWritten.

public void testVersionChangeIsAlwaysWritten() throws Exception {
    // test that version changes are always written
    boolean initializing = randomBoolean();
    boolean versionChanged = true;
    boolean stateInMemory = randomBoolean();
    boolean masterEligible = randomBoolean();
    boolean expectMetaData = true;
    ClusterChangedEvent event = generateEvent(initializing, versionChanged, masterEligible);
    assertState(event, stateInMemory, expectMetaData);
}
Also used : ClusterChangedEvent(org.elasticsearch.cluster.ClusterChangedEvent)

Example 18 with ClusterChangedEvent

use of org.elasticsearch.cluster.ClusterChangedEvent in project elasticsearch by elastic.

the class PublishClusterStateActionTests method testSerializationFailureDuringDiffPublishing.

public void testSerializationFailureDuringDiffPublishing() throws Exception {
    MockNode nodeA = createMockNode("nodeA", Settings.EMPTY, new ClusterStateListener() {

        @Override
        public void clusterChanged(ClusterChangedEvent event) {
            fail("Shouldn't send cluster state to myself");
        }
    }).setAsMaster();
    MockNode nodeB = createMockNode("nodeB");
    // Initial cluster state with both states - the second node still shouldn't get
    // diff even though it's present in the previous cluster state
    DiscoveryNodes discoveryNodes = DiscoveryNodes.builder(nodeA.nodes()).add(nodeB.discoveryNode).build();
    ClusterState previousClusterState = ClusterState.builder(CLUSTER_NAME).nodes(discoveryNodes).build();
    ClusterState clusterState = ClusterState.builder(previousClusterState).incrementVersion().build();
    publishStateAndWait(nodeA.action, clusterState, previousClusterState);
    assertSameStateFromFull(nodeB.clusterState, clusterState);
    // cluster state update - add block
    previousClusterState = clusterState;
    clusterState = ClusterState.builder(clusterState).blocks(ClusterBlocks.builder().addGlobalBlock(MetaData.CLUSTER_READ_ONLY_BLOCK)).incrementVersion().build();
    ClusterState unserializableClusterState = new ClusterState(clusterState.version(), clusterState.stateUUID(), clusterState) {

        @Override
        public Diff<ClusterState> diff(ClusterState previousState) {
            return new Diff<ClusterState>() {

                @Override
                public ClusterState apply(ClusterState part) {
                    fail("this diff shouldn't be applied");
                    return part;
                }

                @Override
                public void writeTo(StreamOutput out) throws IOException {
                    throw new IOException("Simulated failure of diff serialization");
                }
            };
        }
    };
    try {
        publishStateAndWait(nodeA.action, unserializableClusterState, previousClusterState);
        fail("cluster state published despite of diff errors");
    } catch (Discovery.FailedToCommitClusterStateException e) {
        assertThat(e.getCause(), notNullValue());
        assertThat(e.getCause().getMessage(), containsString("failed to serialize"));
    }
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) Diff(org.elasticsearch.cluster.Diff) Discovery(org.elasticsearch.discovery.Discovery) ClusterChangedEvent(org.elasticsearch.cluster.ClusterChangedEvent) IOException(java.io.IOException) StreamOutput(org.elasticsearch.common.io.stream.StreamOutput) DiscoveryNodes(org.elasticsearch.cluster.node.DiscoveryNodes) ClusterStateListener(org.elasticsearch.cluster.ClusterStateListener)

Example 19 with ClusterChangedEvent

use of org.elasticsearch.cluster.ClusterChangedEvent in project elasticsearch by elastic.

the class PublishClusterStateActionTests method testSimultaneousClusterStatePublishing.

/**
     * Test not waiting on publishing works correctly (i.e., publishing times out)
     */
public void testSimultaneousClusterStatePublishing() throws Exception {
    int numberOfNodes = randomIntBetween(2, 10);
    int numberOfIterations = scaledRandomIntBetween(5, 50);
    Settings settings = Settings.builder().put(DiscoverySettings.PUBLISH_DIFF_ENABLE_SETTING.getKey(), randomBoolean()).build();
    MockNode master = createMockNode("node0", settings, new ClusterStateListener() {

        @Override
        public void clusterChanged(ClusterChangedEvent event) {
            assertProperMetaDataForVersion(event.state().metaData(), event.state().version());
        }
    }).setAsMaster();
    DiscoveryNodes.Builder discoveryNodesBuilder = DiscoveryNodes.builder(master.nodes());
    for (int i = 1; i < numberOfNodes; i++) {
        final String name = "node" + i;
        final MockNode node = createMockNode(name, settings, new ClusterStateListener() {

            @Override
            public void clusterChanged(ClusterChangedEvent event) {
                assertProperMetaDataForVersion(event.state().metaData(), event.state().version());
            }
        });
        discoveryNodesBuilder.add(node.discoveryNode);
    }
    AssertingAckListener[] listeners = new AssertingAckListener[numberOfIterations];
    DiscoveryNodes discoveryNodes = discoveryNodesBuilder.build();
    MetaData metaData = MetaData.EMPTY_META_DATA;
    ClusterState clusterState = ClusterState.builder(CLUSTER_NAME).metaData(metaData).build();
    ClusterState previousState;
    for (int i = 0; i < numberOfIterations; i++) {
        previousState = clusterState;
        metaData = buildMetaDataForVersion(metaData, i + 1);
        clusterState = ClusterState.builder(clusterState).incrementVersion().metaData(metaData).nodes(discoveryNodes).build();
        listeners[i] = publishState(master.action, clusterState, previousState);
    }
    for (int i = 0; i < numberOfIterations; i++) {
        listeners[i].await(1, TimeUnit.SECONDS);
    }
    // set the master cs
    master.clusterState = clusterState;
    for (MockNode node : nodes.values()) {
        assertSameState(node.clusterState, clusterState);
        assertThat(node.clusterState.nodes().getLocalNode(), equalTo(node.discoveryNode));
    }
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) ClusterChangedEvent(org.elasticsearch.cluster.ClusterChangedEvent) Matchers.hasToString(org.hamcrest.Matchers.hasToString) Matchers.containsString(org.hamcrest.Matchers.containsString) ClusterStateListener(org.elasticsearch.cluster.ClusterStateListener) MetaData(org.elasticsearch.cluster.metadata.MetaData) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) Settings(org.elasticsearch.common.settings.Settings) DiscoverySettings(org.elasticsearch.discovery.DiscoverySettings) ClusterSettings(org.elasticsearch.common.settings.ClusterSettings) DiscoveryNodes(org.elasticsearch.cluster.node.DiscoveryNodes)

Example 20 with ClusterChangedEvent

use of org.elasticsearch.cluster.ClusterChangedEvent 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

ClusterChangedEvent (org.elasticsearch.cluster.ClusterChangedEvent)27 ClusterState (org.elasticsearch.cluster.ClusterState)20 IndexMetaData (org.elasticsearch.cluster.metadata.IndexMetaData)8 MetaData (org.elasticsearch.cluster.metadata.MetaData)8 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)7 DiscoveryNodes (org.elasticsearch.cluster.node.DiscoveryNodes)7 Settings (org.elasticsearch.common.settings.Settings)7 Discovery (org.elasticsearch.discovery.Discovery)5 Matchers.containsString (org.hamcrest.Matchers.containsString)5 CountDownLatch (java.util.concurrent.CountDownLatch)4 ClusterStateListener (org.elasticsearch.cluster.ClusterStateListener)4 ClusterSettings (org.elasticsearch.common.settings.ClusterSettings)4 TimeValue (org.elasticsearch.common.unit.TimeValue)4 DiscoverySettings (org.elasticsearch.discovery.DiscoverySettings)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 ParameterizedMessage (org.apache.logging.log4j.message.ParameterizedMessage)3 ClusterStateUpdateTask (org.elasticsearch.cluster.ClusterStateUpdateTask)3 RoutingTable (org.elasticsearch.cluster.routing.RoutingTable)3 AllocationService (org.elasticsearch.cluster.routing.allocation.AllocationService)3 ShardId (org.elasticsearch.index.shard.ShardId)3