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