Search in sources :

Example 21 with VotingConfiguration

use of org.elasticsearch.cluster.coordination.CoordinationMetadata.VotingConfiguration in project crate by crate.

the class CoordinationStateTests method testHandleCommitWithBadLastAcceptedTerm.

public void testHandleCommitWithBadLastAcceptedTerm() {
    VotingConfiguration initialConfig = new VotingConfiguration(Collections.singleton(node1.getId()));
    ClusterState state1 = clusterState(0L, 0L, node1, initialConfig, initialConfig, 42L);
    cs1.setInitialState(state1);
    StartJoinRequest startJoinRequest1 = new StartJoinRequest(node1, randomLongBetween(1, 5));
    Join v1 = cs1.handleStartJoin(startJoinRequest1);
    assertTrue(cs1.handleJoin(v1));
    assertTrue(cs1.electionWon());
    assertThat(expectThrows(CoordinationStateRejectedException.class, () -> cs1.handleCommit(new ApplyCommitRequest(node1, startJoinRequest1.getTerm(), 2L))).getMessage(), containsString("does not match last accepted term"));
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) VotingConfiguration(org.elasticsearch.cluster.coordination.CoordinationMetadata.VotingConfiguration)

Example 22 with VotingConfiguration

use of org.elasticsearch.cluster.coordination.CoordinationMetadata.VotingConfiguration in project crate by crate.

the class CoordinationStateTests method testHandleClientValue.

public void testHandleClientValue() {
    VotingConfiguration initialConfig = new VotingConfiguration(Collections.singleton(node1.getId()));
    ClusterState state1 = clusterState(0L, 0L, node1, initialConfig, initialConfig, 42L);
    cs1.setInitialState(state1);
    StartJoinRequest startJoinRequest1 = new StartJoinRequest(node1, randomLongBetween(1, 5));
    Join v1 = cs1.handleStartJoin(startJoinRequest1);
    Join v2 = cs2.handleStartJoin(startJoinRequest1);
    assertTrue(cs1.handleJoin(v1));
    assertTrue(cs1.electionWon());
    assertTrue(cs1.handleJoin(v2));
    VotingConfiguration newConfig = new VotingConfiguration(Collections.singleton(node2.getId()));
    ClusterState state2 = clusterState(startJoinRequest1.getTerm(), 2L, node1, initialConfig, newConfig, 42L);
    PublishRequest publishRequest = cs1.handleClientValue(state2);
    assertThat(publishRequest.getAcceptedState(), equalTo(state2));
    assertThat(cs1.getLastPublishedVersion(), equalTo(state2.version()));
    // check that another join does not mess with lastPublishedVersion
    Join v3 = cs3.handleStartJoin(startJoinRequest1);
    assertTrue(cs1.handleJoin(v3));
    assertThat(cs1.getLastPublishedVersion(), equalTo(state2.version()));
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) VotingConfiguration(org.elasticsearch.cluster.coordination.CoordinationMetadata.VotingConfiguration)

Example 23 with VotingConfiguration

use of org.elasticsearch.cluster.coordination.CoordinationMetadata.VotingConfiguration in project crate by crate.

the class CoordinationStateTests method testHandlePublishRequest.

public void testHandlePublishRequest() {
    VotingConfiguration initialConfig = new VotingConfiguration(Collections.singleton(node1.getId()));
    ClusterState state1 = clusterState(0L, 0L, node1, initialConfig, initialConfig, 42L);
    cs1.setInitialState(state1);
    StartJoinRequest startJoinRequest1 = new StartJoinRequest(node1, randomLongBetween(1, 5));
    Join v1 = cs1.handleStartJoin(startJoinRequest1);
    if (randomBoolean()) {
        assertTrue(cs1.handleJoin(v1));
        assertTrue(cs1.electionWon());
    }
    ClusterState state2 = clusterState(startJoinRequest1.getTerm(), randomLongBetween(1, 10), node1, initialConfig, initialConfig, 13L);
    PublishResponse publishResponse = cs1.handlePublishRequest(new PublishRequest(state2));
    assertThat(publishResponse.getTerm(), equalTo(state2.term()));
    assertThat(publishResponse.getVersion(), equalTo(state2.version()));
    assertThat(cs1.getLastAcceptedState(), equalTo(state2));
    assertThat(value(cs1.getLastAcceptedState()), equalTo(13L));
    ClusterState state3 = clusterState(startJoinRequest1.getTerm(), randomLongBetween(state2.getVersion() + 1, 20), node1, initialConfig, initialConfig, 13L);
    cs1.handlePublishRequest(new PublishRequest(state3));
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) VotingConfiguration(org.elasticsearch.cluster.coordination.CoordinationMetadata.VotingConfiguration)

Example 24 with VotingConfiguration

use of org.elasticsearch.cluster.coordination.CoordinationMetadata.VotingConfiguration in project crate by crate.

the class PublicationTests method testClusterStatePublishingWithFaultyNodeBeforeCommit.

public void testClusterStatePublishingWithFaultyNodeBeforeCommit() throws InterruptedException {
    VotingConfiguration singleNodeConfig = new VotingConfiguration(Set.of(n1.getId()));
    initializeCluster(singleNodeConfig);
    AssertingAckListener ackListener = new AssertingAckListener(nodes.size());
    DiscoveryNodes discoveryNodes = DiscoveryNodes.builder().add(n1).add(n2).add(n3).localNodeId(n1.getId()).build();
    // number of publish actions + initial faulty nodes injection
    AtomicInteger remainingActions = new AtomicInteger(4);
    int injectFaultAt = randomInt(remainingActions.get() - 1);
    logger.info("Injecting fault at: {}", injectFaultAt);
    Set<DiscoveryNode> initialFaultyNodes = remainingActions.decrementAndGet() == injectFaultAt ? Collections.singleton(n2) : Collections.emptySet();
    MockPublication publication = node1.publish(CoordinationStateTests.clusterState(1L, 2L, discoveryNodes, singleNodeConfig, singleNodeConfig, 42L), ackListener, initialFaultyNodes);
    publication.pendingPublications.entrySet().stream().collect(shuffle()).forEach(e -> {
        if (remainingActions.decrementAndGet() == injectFaultAt) {
            publication.onFaultyNode(n2);
        }
        if (e.getKey().equals(n2) == false || randomBoolean()) {
            PublishResponse publishResponse = nodeResolver.apply(e.getKey()).coordinationState.handlePublishRequest(publication.publishRequest);
            e.getValue().onResponse(new PublishWithJoinResponse(publishResponse, Optional.empty()));
        }
    });
    publication.pendingCommits.entrySet().stream().collect(shuffle()).forEach(e -> {
        nodeResolver.apply(e.getKey()).coordinationState.handleCommit(publication.applyCommit);
        e.getValue().onResponse(TransportResponse.Empty.INSTANCE);
    });
    assertTrue(publication.completed);
    assertTrue(publication.committed);
    // has no influence
    publication.onFaultyNode(randomFrom(n1, n3));
    List<Tuple<DiscoveryNode, Throwable>> errors = ackListener.awaitErrors(0L, TimeUnit.SECONDS);
    assertThat(errors.size(), equalTo(1));
    assertThat(errors.get(0).v1(), equalTo(n2));
    assertThat(errors.get(0).v2().getMessage(), containsString("faulty node"));
}
Also used : DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DiscoveryNodes(org.elasticsearch.cluster.node.DiscoveryNodes) Tuple(io.crate.common.collections.Tuple) VotingConfiguration(org.elasticsearch.cluster.coordination.CoordinationMetadata.VotingConfiguration)

Example 25 with VotingConfiguration

use of org.elasticsearch.cluster.coordination.CoordinationMetadata.VotingConfiguration in project crate by crate.

the class PublicationTests method testClusterStatePublishingWithFaultyNodeAfterCommit.

public void testClusterStatePublishingWithFaultyNodeAfterCommit() throws InterruptedException {
    VotingConfiguration singleNodeConfig = new VotingConfiguration(Set.of(n1.getId()));
    initializeCluster(singleNodeConfig);
    AssertingAckListener ackListener = new AssertingAckListener(nodes.size());
    DiscoveryNodes discoveryNodes = DiscoveryNodes.builder().add(n1).add(n2).add(n3).localNodeId(n1.getId()).build();
    boolean publicationDidNotMakeItToNode2 = randomBoolean();
    AtomicInteger remainingActions = new AtomicInteger(publicationDidNotMakeItToNode2 ? 2 : 3);
    int injectFaultAt = randomInt(remainingActions.get() - 1);
    logger.info("Injecting fault at: {}, publicationDidNotMakeItToNode2: {}", injectFaultAt, publicationDidNotMakeItToNode2);
    MockPublication publication = node1.publish(CoordinationStateTests.clusterState(1L, 2L, discoveryNodes, singleNodeConfig, singleNodeConfig, 42L), ackListener, Collections.emptySet());
    publication.pendingPublications.entrySet().stream().collect(shuffle()).forEach(e -> {
        if (e.getKey().equals(n2) == false || publicationDidNotMakeItToNode2 == false) {
            PublishResponse publishResponse = nodeResolver.apply(e.getKey()).coordinationState.handlePublishRequest(publication.publishRequest);
            e.getValue().onResponse(new PublishWithJoinResponse(publishResponse, Optional.empty()));
        }
    });
    publication.pendingCommits.entrySet().stream().collect(shuffle()).forEach(e -> {
        if (e.getKey().equals(n2)) {
            // we must fail node before committing for the node, otherwise failing the node is ignored
            publication.onFaultyNode(n2);
        }
        if (remainingActions.decrementAndGet() == injectFaultAt) {
            publication.onFaultyNode(n2);
        }
        if (e.getKey().equals(n2) == false || randomBoolean()) {
            nodeResolver.apply(e.getKey()).coordinationState.handleCommit(publication.applyCommit);
            e.getValue().onResponse(TransportResponse.Empty.INSTANCE);
        }
    });
    // we need to complete publication by failing the node
    if (publicationDidNotMakeItToNode2 && remainingActions.get() > injectFaultAt) {
        publication.onFaultyNode(n2);
    }
    assertTrue(publication.completed);
    assertTrue(publication.committed);
    // has no influence
    publication.onFaultyNode(randomFrom(n1, n3));
    List<Tuple<DiscoveryNode, Throwable>> errors = ackListener.awaitErrors(0L, TimeUnit.SECONDS);
    assertThat(errors.size(), equalTo(1));
    assertThat(errors.get(0).v1(), equalTo(n2));
    assertThat(errors.get(0).v2().getMessage(), containsString("faulty node"));
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DiscoveryNodes(org.elasticsearch.cluster.node.DiscoveryNodes) Tuple(io.crate.common.collections.Tuple) VotingConfiguration(org.elasticsearch.cluster.coordination.CoordinationMetadata.VotingConfiguration)

Aggregations

VotingConfiguration (org.elasticsearch.cluster.coordination.CoordinationMetadata.VotingConfiguration)66 ClusterState (org.elasticsearch.cluster.ClusterState)40 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)19 DiscoveryNodes (org.elasticsearch.cluster.node.DiscoveryNodes)8 Set (java.util.Set)6 HashSet (java.util.HashSet)5 Settings (org.elasticsearch.common.settings.Settings)5 Tuple (io.crate.common.collections.Tuple)4 Collections (java.util.Collections)4 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)4 Collectors (java.util.stream.Collectors)4 ClusterSettings (org.elasticsearch.common.settings.ClusterSettings)4 TimeValue (io.crate.common.unit.TimeValue)3 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 Optional (java.util.Optional)3 TimeUnit (java.util.concurrent.TimeUnit)3 Stream (java.util.stream.Stream)3 ESTestCase (org.elasticsearch.test.ESTestCase)3