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));
}
}
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();
}
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) {
}
}
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());
}
});
}
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();
}
}
Aggregations