Search in sources :

Example 1 with NotLeaderException

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));
    }
}
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 NotLeaderException

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();
}
Also used : NotLeaderException(com.hazelcast.cp.exception.NotLeaderException) RaftState(com.hazelcast.cp.internal.raft.impl.state.RaftState)

Example 3 with NotLeaderException

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);
}
Also used : NotLeaderException(com.hazelcast.cp.exception.NotLeaderException) RaftService(com.hazelcast.cp.internal.RaftService) CPGroupDestroyedException(com.hazelcast.cp.exception.CPGroupDestroyedException) RaftNode(com.hazelcast.cp.internal.raft.impl.RaftNode)

Example 4 with NotLeaderException

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);
}
Also used : RaftNodeAware(com.hazelcast.cp.internal.RaftNodeAware) NotLeaderException(com.hazelcast.cp.exception.NotLeaderException) RaftService(com.hazelcast.cp.internal.RaftService) CPGroupDestroyedException(com.hazelcast.cp.exception.CPGroupDestroyedException) RaftNode(com.hazelcast.cp.internal.raft.impl.RaftNode)

Example 5 with NotLeaderException

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

NotLeaderException (com.hazelcast.cp.exception.NotLeaderException)7 RaftState (com.hazelcast.cp.internal.raft.impl.state.RaftState)4 CPGroupDestroyedException (com.hazelcast.cp.exception.CPGroupDestroyedException)2 CPSubsystemException (com.hazelcast.cp.exception.CPSubsystemException)2 CannotReplicateException (com.hazelcast.cp.exception.CannotReplicateException)2 RaftService (com.hazelcast.cp.internal.RaftService)2 RaftEndpoint (com.hazelcast.cp.internal.raft.impl.RaftEndpoint)2 RaftNode (com.hazelcast.cp.internal.raft.impl.RaftNode)2 UUID (java.util.UUID)2 RaftNodeAware (com.hazelcast.cp.internal.RaftNodeAware)1 MemberAlreadyExistsException (com.hazelcast.cp.internal.raft.exception.MemberAlreadyExistsException)1 MemberDoesNotExistException (com.hazelcast.cp.internal.raft.exception.MemberDoesNotExistException)1 UpdateRaftGroupMembersCmd (com.hazelcast.cp.internal.raft.impl.command.UpdateRaftGroupMembersCmd)1 ApplyRaftRunnable (com.hazelcast.cp.internal.raft.impl.dataservice.ApplyRaftRunnable)1 RaftDataService (com.hazelcast.cp.internal.raft.impl.dataservice.RaftDataService)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 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)1 QuickTest (com.hazelcast.test.annotation.QuickTest)1