Search in sources :

Example 1 with CannotReplicateException

use of com.hazelcast.cp.exception.CannotReplicateException in project hazelcast by hazelcast.

the class ReplicateTask method run.

@Override
public void run() {
    try {
        if (!verifyRaftNodeStatus()) {
            return;
        }
        RaftState state = raftNode.state();
        if (state.role() != LEADER) {
            resultFuture.completeExceptionally(new NotLeaderException(raftNode.getGroupId(), raftNode.getLocalMember(), state.leader()));
            return;
        }
        if (!raftNode.canReplicateNewEntry(operation)) {
            resultFuture.completeExceptionally(new CannotReplicateException(raftNode.getLocalMember()));
            return;
        }
        if (logger.isFineEnabled()) {
            logger.fine("Replicating: " + operation + " in term: " + state.term());
        }
        RaftLog log = state.log();
        if (!log.checkAvailableCapacity(1)) {
            resultFuture.completeExceptionally(new IllegalStateException("Not enough capacity in RaftLog!"));
            return;
        }
        long newEntryLogIndex = log.lastLogOrSnapshotIndex() + 1;
        raftNode.registerFuture(newEntryLogIndex, resultFuture);
        log.appendEntries(new LogEntry(state.term(), newEntryLogIndex, operation));
        preApplyRaftGroupCmd(newEntryLogIndex, operation);
        raftNode.broadcastAppendRequest();
    } catch (Throwable t) {
        logger.severe(operation + " could not be replicated to leader: " + raftNode.getLocalMember(), t);
        RaftEndpoint leader = raftNode.getLeader();
        UUID leaderUuid = leader != null ? leader.getUuid() : null;
        resultFuture.completeExceptionally(new CPSubsystemException("Internal failure", t, leaderUuid));
    }
}
Also used : NotLeaderException(com.hazelcast.cp.exception.NotLeaderException) RaftState(com.hazelcast.cp.internal.raft.impl.state.RaftState) RaftEndpoint(com.hazelcast.cp.internal.raft.impl.RaftEndpoint) CannotReplicateException(com.hazelcast.cp.exception.CannotReplicateException) UUID(java.util.UUID) CPSubsystemException(com.hazelcast.cp.exception.CPSubsystemException) LogEntry(com.hazelcast.cp.internal.raft.impl.log.LogEntry) RaftLog(com.hazelcast.cp.internal.raft.impl.log.RaftLog)

Example 2 with CannotReplicateException

use of com.hazelcast.cp.exception.CannotReplicateException in project hazelcast by hazelcast.

the class LinearizableQueryTest method when_multipleQueryLimitIsReachedBeforeHeartbeatAcks_then_noNewQueryIsAccepted.

@Test(timeout = 300_000)
public void when_multipleQueryLimitIsReachedBeforeHeartbeatAcks_then_noNewQueryIsAccepted() throws Exception {
    RaftAlgorithmConfig config = new RaftAlgorithmConfig().setUncommittedEntryCountToRejectNewAppends(1);
    group = new LocalRaftGroupBuilder(5, config).setAppendNopEntryOnLeaderElection(true).build();
    group.start();
    RaftNodeImpl leader = group.waitUntilLeaderElected();
    assertTrueEventually(() -> assertThat(getCommitIndex(leader), greaterThanOrEqualTo(1L)));
    RaftNodeImpl[] followers = group.getNodesExcept(leader.getLocalMember());
    group.dropMessagesToMember(leader.getLocalMember(), followers[0].getLocalMember(), AppendRequest.class);
    group.dropMessagesToMember(leader.getLocalMember(), followers[1].getLocalMember(), AppendRequest.class);
    group.dropMessagesToMember(leader.getLocalMember(), followers[2].getLocalMember(), AppendRequest.class);
    InternalCompletableFuture queryFuture1 = leader.query(new QueryRaftRunnable(), LINEARIZABLE);
    InternalCompletableFuture queryFuture2 = leader.query(new QueryRaftRunnable(), LINEARIZABLE);
    try {
        queryFuture2.joinInternal();
        fail();
    } catch (CannotReplicateException ignored) {
    }
    group.resetAllRulesFrom(leader.getLocalMember());
    queryFuture1.get();
}
Also used : RaftAlgorithmConfig(com.hazelcast.config.cp.RaftAlgorithmConfig) InternalCompletableFuture(com.hazelcast.spi.impl.InternalCompletableFuture) QueryRaftRunnable(com.hazelcast.cp.internal.raft.impl.dataservice.QueryRaftRunnable) LocalRaftGroupBuilder(com.hazelcast.cp.internal.raft.impl.testing.LocalRaftGroup.LocalRaftGroupBuilder) CannotReplicateException(com.hazelcast.cp.exception.CannotReplicateException) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 3 with CannotReplicateException

use of com.hazelcast.cp.exception.CannotReplicateException in project hazelcast by hazelcast.

the class LocalRaftTest method when_thereAreTooManyInflightAppendedEntries_then_newAppendsAreRejected.

@Test
public void when_thereAreTooManyInflightAppendedEntries_then_newAppendsAreRejected() {
    int uncommittedEntryCount = 10;
    RaftAlgorithmConfig config = new RaftAlgorithmConfig().setUncommittedEntryCountToRejectNewAppends(uncommittedEntryCount);
    group = newGroup(2, config);
    group.start();
    RaftNodeImpl leader = group.waitUntilLeaderElected();
    RaftNodeImpl follower = group.getAnyFollowerNode();
    group.terminateNode(follower.getLocalMember());
    for (int i = 0; i < uncommittedEntryCount; i++) {
        leader.replicate(new ApplyRaftRunnable("val" + i));
    }
    try {
        leader.replicate(new ApplyRaftRunnable("valFinal")).joinInternal();
        fail();
    } catch (CannotReplicateException ignored) {
    }
}
Also used : RaftAlgorithmConfig(com.hazelcast.config.cp.RaftAlgorithmConfig) ApplyRaftRunnable(com.hazelcast.cp.internal.raft.impl.dataservice.ApplyRaftRunnable) CannotReplicateException(com.hazelcast.cp.exception.CannotReplicateException) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 4 with CannotReplicateException

use of com.hazelcast.cp.exception.CannotReplicateException in project hazelcast by hazelcast.

the class MembershipChangeTest method when_appendNopEntryOnLeaderElection_then_canMakeMemberChangeAfterNopEntryCommitted.

@Test
public void when_appendNopEntryOnLeaderElection_then_canMakeMemberChangeAfterNopEntryCommitted() {
    // https://groups.google.com/forum/#!msg/raft-dev/t4xj6dJTP6E/d2D9LrWRza8J
    group = new LocalRaftGroupBuilder(3).setAppendNopEntryOnLeaderElection(true).build();
    group.start();
    RaftNodeImpl leader = group.waitUntilLeaderElected();
    assertTrueEventually(() -> {
        // may fail until nop-entry is committed
        try {
            leader.replicateMembershipChange(leader.getLocalMember(), MembershipChangeMode.REMOVE).get();
        } catch (CannotReplicateException e) {
            fail(e.getMessage());
        }
    });
}
Also used : LocalRaftGroupBuilder(com.hazelcast.cp.internal.raft.impl.testing.LocalRaftGroup.LocalRaftGroupBuilder) CannotReplicateException(com.hazelcast.cp.exception.CannotReplicateException) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 5 with CannotReplicateException

use of com.hazelcast.cp.exception.CannotReplicateException in project hazelcast by hazelcast.

the class QueryTask method handleLinearizableRead.

private void handleLinearizableRead() {
    if (!raftNode.isLinearizableReadOptimizationEnabled()) {
        new ReplicateTask(raftNode, operation, resultFuture).run();
        return;
    }
    RaftState state = raftNode.state();
    if (state.role() != LEADER) {
        resultFuture.completeExceptionally(new NotLeaderException(raftNode.getGroupId(), raftNode.getLocalMember(), state.leader()));
        return;
    }
    if (!raftNode.canQueryLinearizable()) {
        resultFuture.completeExceptionally(new CannotReplicateException(state.leader()));
        return;
    }
    long commitIndex = state.commitIndex();
    QueryState queryState = state.leaderState().queryState();
    if (logger.isFineEnabled()) {
        logger.fine("Adding query at commit index: " + commitIndex + ", query round: " + queryState.queryRound());
    }
    if (queryState.addQuery(commitIndex, operation, resultFuture) == 1) {
        raftNode.broadcastAppendRequest();
    }
}
Also used : NotLeaderException(com.hazelcast.cp.exception.NotLeaderException) RaftState(com.hazelcast.cp.internal.raft.impl.state.RaftState) CannotReplicateException(com.hazelcast.cp.exception.CannotReplicateException) QueryState(com.hazelcast.cp.internal.raft.impl.state.QueryState)

Aggregations

CannotReplicateException (com.hazelcast.cp.exception.CannotReplicateException)7 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)5 QuickTest (com.hazelcast.test.annotation.QuickTest)5 Test (org.junit.Test)5 LocalRaftGroupBuilder (com.hazelcast.cp.internal.raft.impl.testing.LocalRaftGroup.LocalRaftGroupBuilder)3 RaftAlgorithmConfig (com.hazelcast.config.cp.RaftAlgorithmConfig)2 NotLeaderException (com.hazelcast.cp.exception.NotLeaderException)2 ApplyRaftRunnable (com.hazelcast.cp.internal.raft.impl.dataservice.ApplyRaftRunnable)2 RaftState (com.hazelcast.cp.internal.raft.impl.state.RaftState)2 InternalCompletableFuture (com.hazelcast.spi.impl.InternalCompletableFuture)2 CPSubsystemException (com.hazelcast.cp.exception.CPSubsystemException)1 DestroyRaftGroupCmd (com.hazelcast.cp.internal.raft.command.DestroyRaftGroupCmd)1 RaftEndpoint (com.hazelcast.cp.internal.raft.impl.RaftEndpoint)1 QueryRaftRunnable (com.hazelcast.cp.internal.raft.impl.dataservice.QueryRaftRunnable)1 LogEntry (com.hazelcast.cp.internal.raft.impl.log.LogEntry)1 RaftLog (com.hazelcast.cp.internal.raft.impl.log.RaftLog)1 QueryState (com.hazelcast.cp.internal.raft.impl.state.QueryState)1 UUID (java.util.UUID)1