use of com.hazelcast.cp.exception.NotLeaderException 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.NotLeaderException in project hazelcast by hazelcast.
the class QueryTask method handleLeaderLocalRead.
private void handleLeaderLocalRead() {
RaftState state = raftNode.state();
if (state.role() != LEADER) {
resultFuture.completeExceptionally(new NotLeaderException(raftNode.getGroupId(), raftNode.getLocalMember(), state.leader()));
return;
}
// TODO: We can reject the query, if leader is not able to reach majority of the followers
handleAnyLocalRead();
}
use of com.hazelcast.cp.exception.NotLeaderException in project hazelcast by hazelcast.
the class RaftReplicateOp method run.
@Override
public final void run() {
RaftService service = getService();
RaftNode raftNode = service.getOrInitRaftNode(groupId);
if (raftNode == null) {
if (service.isRaftGroupDestroyed(groupId)) {
sendResponse(new CPGroupDestroyedException(groupId));
} else {
sendResponse(new NotLeaderException(groupId, service.getLocalCPEndpoint(), null));
}
return;
} else if (raftNode.getStatus() == RaftNodeStatus.STEPPED_DOWN) {
sendResponse(new NotLeaderException(groupId, service.getLocalCPEndpoint(), null));
getNodeEngine().getExecutionService().execute(CP_SUBSYSTEM_EXECUTOR, () -> service.stepDownRaftNode(groupId));
return;
}
replicate(raftNode).whenCompleteAsync(this, CALLER_RUNS);
}
use of com.hazelcast.cp.exception.NotLeaderException in project hazelcast by hazelcast.
the class RaftQueryOp method run.
@Override
public final void run() {
RaftService service = getService();
RaftNode raftNode = service.getRaftNode(groupId);
if (raftNode == null) {
if (service.isRaftGroupDestroyed(groupId)) {
sendResponse(new CPGroupDestroyedException(groupId));
} else {
sendResponse(new NotLeaderException(groupId, service.getLocalCPEndpoint(), null));
}
return;
} else if (raftNode.getStatus() == RaftNodeStatus.STEPPED_DOWN) {
sendResponse(new NotLeaderException(groupId, service.getLocalCPEndpoint(), null));
getNodeEngine().getExecutionService().execute(CP_SUBSYSTEM_EXECUTOR, () -> service.stepDownRaftNode(groupId));
return;
}
if (op instanceof RaftNodeAware) {
((RaftNodeAware) op).setRaftNode(raftNode);
}
raftNode.query(op, queryPolicy).whenCompleteAsync(this, CALLER_RUNS);
}
use of com.hazelcast.cp.exception.NotLeaderException 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