Search in sources :

Example 6 with NotLeaderException

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

the class MembershipChangeTask 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 (!isValidGroupMemberCommitIndex()) {
            return;
        }
        Collection<RaftEndpoint> members = new LinkedHashSet<RaftEndpoint>(state.members());
        boolean memberExists = members.contains(member);
        switch(membershipChangeMode) {
            case ADD:
                if (memberExists) {
                    resultFuture.completeExceptionally(new MemberAlreadyExistsException(member));
                    return;
                }
                members.add(member);
                break;
            case REMOVE:
                if (!memberExists) {
                    resultFuture.completeExceptionally(new MemberDoesNotExistException(member));
                    return;
                }
                members.remove(member);
                break;
            default:
                resultFuture.completeExceptionally(new IllegalArgumentException("Unknown type: " + membershipChangeMode));
                return;
        }
        logger.info("New members after " + membershipChangeMode + " " + member + " -> " + members);
        new ReplicateTask(raftNode, new UpdateRaftGroupMembersCmd(members, member, membershipChangeMode), resultFuture).run();
    } catch (Throwable t) {
        logger.severe(this + " failed", t);
        RaftEndpoint leader = raftNode.getLeader();
        UUID leaderUuid = leader != null ? leader.getUuid() : null;
        resultFuture.completeExceptionally(new CPSubsystemException("Internal failure", t, leaderUuid));
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) NotLeaderException(com.hazelcast.cp.exception.NotLeaderException) MemberAlreadyExistsException(com.hazelcast.cp.internal.raft.exception.MemberAlreadyExistsException) RaftState(com.hazelcast.cp.internal.raft.impl.state.RaftState) RaftEndpoint(com.hazelcast.cp.internal.raft.impl.RaftEndpoint) CPSubsystemException(com.hazelcast.cp.exception.CPSubsystemException) MemberDoesNotExistException(com.hazelcast.cp.internal.raft.exception.MemberDoesNotExistException) UpdateRaftGroupMembersCmd(com.hazelcast.cp.internal.raft.impl.command.UpdateRaftGroupMembersCmd) UUID(java.util.UUID)

Example 7 with NotLeaderException

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

the class LocalRaftTest method when_followerAttemptsToReplicate_then_itFails.

@Test
public void when_followerAttemptsToReplicate_then_itFails() throws ExecutionException, InterruptedException {
    group = newGroup(3);
    group.start();
    RaftNodeImpl leader = group.waitUntilLeaderElected();
    RaftNodeImpl[] followers = group.getNodesExcept(leader.getLocalMember());
    try {
        followers[0].replicate(new ApplyRaftRunnable("val")).joinInternal();
        fail("NotLeaderException should have been thrown");
    } catch (NotLeaderException e) {
        ignore(e);
    }
    for (RaftNodeImpl raftNode : group.getNodes()) {
        RaftDataService service = group.getIntegration(raftNode.getLocalMember()).getService();
        assertEquals(0, service.size());
    }
}
Also used : NotLeaderException(com.hazelcast.cp.exception.NotLeaderException) ApplyRaftRunnable(com.hazelcast.cp.internal.raft.impl.dataservice.ApplyRaftRunnable) RaftDataService(com.hazelcast.cp.internal.raft.impl.dataservice.RaftDataService) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

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