Search in sources :

Example 46 with RaftEndpoint

use of com.hazelcast.cp.internal.raft.impl.RaftEndpoint 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 47 with RaftEndpoint

use of com.hazelcast.cp.internal.raft.impl.RaftEndpoint in project hazelcast by hazelcast.

the class CreateRaftGroupOp method writeData.

@Override
public void writeData(ObjectDataOutput out) throws IOException {
    out.writeString(groupName);
    out.writeInt(members.size());
    for (RaftEndpoint member : members) {
        out.writeObject(member);
    }
    out.writeLong(groupId);
}
Also used : RaftEndpoint(com.hazelcast.cp.internal.raft.impl.RaftEndpoint)

Example 48 with RaftEndpoint

use of com.hazelcast.cp.internal.raft.impl.RaftEndpoint in project hazelcast by hazelcast.

the class HazelcastRaftTestSupport method getRandomFollowerInstance.

protected static HazelcastInstance getRandomFollowerInstance(HazelcastInstance[] instances, CPGroupId groupId) {
    RaftNodeImpl[] raftNodeRef = new RaftNodeImpl[1];
    assertTrueEventually(() -> {
        for (HazelcastInstance instance : instances) {
            RaftNodeImpl raftNode = getRaftNode(instance, groupId);
            if (raftNode != null) {
                raftNodeRef[0] = raftNode;
                return;
            }
        }
        fail();
    });
    RaftNodeImpl raftNode = raftNodeRef[0];
    waitUntilLeaderElected(raftNode);
    RaftEndpoint leaderEndpoint = getLeaderMember(raftNode);
    assertNotNull(leaderEndpoint);
    for (HazelcastInstance instance : instances) {
        CPMember cpMember = instance.getCPSubsystem().getLocalCPMember();
        if (cpMember != null && !cpMember.getUuid().equals(leaderEndpoint.getUuid())) {
            return instance;
        }
    }
    throw new AssertionError();
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) RaftNodeImpl(com.hazelcast.cp.internal.raft.impl.RaftNodeImpl) RaftEndpoint(com.hazelcast.cp.internal.raft.impl.RaftEndpoint) CPMember(com.hazelcast.cp.CPMember)

Example 49 with RaftEndpoint

use of com.hazelcast.cp.internal.raft.impl.RaftEndpoint in project hazelcast by hazelcast.

the class RaftStateTest method test_initialState.

@Test
public void test_initialState() {
    assertEquals(name, state.name());
    assertEquals(groupId, state.groupId());
    assertEquals(members.size(), state.memberCount());
    assertEquals(members, state.members());
    Collection<RaftEndpoint> remoteMembers = new HashSet<RaftEndpoint>(members);
    remoteMembers.remove(localMember);
    assertEquals(remoteMembers, state.remoteMembers());
    assertEquals(0, state.term());
    assertEquals(RaftRole.FOLLOWER, state.role());
    assertNull(state.leader());
    assertEquals(0, state.commitIndex());
    assertEquals(0, state.lastApplied());
    assertEquals(3, state.majority());
    assertNull(state.votedFor());
    assertNull(state.leaderState());
    assertNull(state.candidateState());
    RaftLog log = state.log();
    assertEquals(0, log.lastLogOrSnapshotIndex());
    assertEquals(0, log.lastLogOrSnapshotTerm());
}
Also used : TestRaftEndpoint(com.hazelcast.cp.internal.raft.impl.testing.TestRaftEndpoint) RaftEndpoint(com.hazelcast.cp.internal.raft.impl.RaftEndpoint) HashSet(java.util.HashSet) RaftLog(com.hazelcast.cp.internal.raft.impl.log.RaftLog) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 50 with RaftEndpoint

use of com.hazelcast.cp.internal.raft.impl.RaftEndpoint in project hazelcast by hazelcast.

the class RaftStateTest method setup.

@Before
public void setup() {
    groupId = new TestRaftGroupId(name);
    localMember = newRaftMember(5000);
    members = new HashSet<RaftEndpoint>(asList(localMember, newRaftMember(5001), newRaftMember(5002), newRaftMember(5003), newRaftMember(5004)));
    state = newRaftState(groupId, localMember, members, 100);
}
Also used : TestRaftEndpoint(com.hazelcast.cp.internal.raft.impl.testing.TestRaftEndpoint) RaftEndpoint(com.hazelcast.cp.internal.raft.impl.RaftEndpoint) TestRaftGroupId(com.hazelcast.cp.internal.raft.impl.testing.TestRaftGroupId) Before(org.junit.Before)

Aggregations

RaftEndpoint (com.hazelcast.cp.internal.raft.impl.RaftEndpoint)57 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)14 QuickTest (com.hazelcast.test.annotation.QuickTest)14 Test (org.junit.Test)14 RaftNodeImpl (com.hazelcast.cp.internal.raft.impl.RaftNodeImpl)8 RaftState (com.hazelcast.cp.internal.raft.impl.state.RaftState)7 ArrayList (java.util.ArrayList)7 CPMember (com.hazelcast.cp.CPMember)6 RaftLog (com.hazelcast.cp.internal.raft.impl.log.RaftLog)6 UUID (java.util.UUID)6 HazelcastInstance (com.hazelcast.core.HazelcastInstance)5 CPGroupId (com.hazelcast.cp.CPGroupId)5 TestRaftEndpoint (com.hazelcast.cp.internal.raft.impl.testing.TestRaftEndpoint)5 LinkedHashSet (java.util.LinkedHashSet)4 LogEntry (com.hazelcast.cp.internal.raft.impl.log.LogEntry)3 CPSubsystemException (com.hazelcast.cp.exception.CPSubsystemException)2 NotLeaderException (com.hazelcast.cp.exception.NotLeaderException)2 CannotCreateRaftGroupException (com.hazelcast.cp.internal.exception.CannotCreateRaftGroupException)2 RaftStateStore (com.hazelcast.cp.internal.raft.impl.persistence.RaftStateStore)2 LeaderState (com.hazelcast.cp.internal.raft.impl.state.LeaderState)2