Search in sources :

Example 1 with CPSubsystemException

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

the class AbstractAtomicRegisterSnapshotTest method test_snapshot.

@Test
public void test_snapshot() throws Exception {
    T initialValue = setAndGetInitialValue();
    // force snapshot
    for (int i = 0; i < SNAPSHOT_THRESHOLD; i++) {
        T v = readValue();
        assertEquals(initialValue, v);
    }
    // shutdown the last instance
    instances[instances.length - 1].shutdown();
    HazelcastInstance instance = factory.newHazelcastInstance(createConfig(3, 3));
    instance.getCPSubsystem().getCPSubsystemManagementService().promoteToCPMember().toCompletableFuture().get();
    // Read from local CP member, which should install snapshot after promotion.
    assertTrueEventually(() -> {
        InternalCompletableFuture<Object> future = queryLocally(instance);
        try {
            T value = getValue(future);
            assertEquals(initialValue, value);
        } catch (CPSubsystemException e) {
            // Raft node may not be created yet...
            throw new AssertionError(e);
        }
    });
    assertTrueAllTheTime(() -> {
        InternalCompletableFuture<Object> future = queryLocally(instance);
        T value = getValue(future);
        assertEquals(initialValue, value);
    }, 5);
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) CPSubsystemException(com.hazelcast.cp.exception.CPSubsystemException) Test(org.junit.Test)

Example 2 with CPSubsystemException

use of com.hazelcast.cp.exception.CPSubsystemException 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 3 with CPSubsystemException

use of com.hazelcast.cp.exception.CPSubsystemException 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)

Aggregations

CPSubsystemException (com.hazelcast.cp.exception.CPSubsystemException)3 NotLeaderException (com.hazelcast.cp.exception.NotLeaderException)2 RaftEndpoint (com.hazelcast.cp.internal.raft.impl.RaftEndpoint)2 RaftState (com.hazelcast.cp.internal.raft.impl.state.RaftState)2 UUID (java.util.UUID)2 HazelcastInstance (com.hazelcast.core.HazelcastInstance)1 CannotReplicateException (com.hazelcast.cp.exception.CannotReplicateException)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 LogEntry (com.hazelcast.cp.internal.raft.impl.log.LogEntry)1 RaftLog (com.hazelcast.cp.internal.raft.impl.log.RaftLog)1 LinkedHashSet (java.util.LinkedHashSet)1 Test (org.junit.Test)1