use of com.hazelcast.cp.internal.raft.impl.RaftEndpoint in project hazelcast by hazelcast.
the class MetadataRaftGroupTest method when_metadataClusterNodeFallsFarBehind_then_itInstallsSnapshot.
@Test
public void when_metadataClusterNodeFallsFarBehind_then_itInstallsSnapshot() {
int nodeCount = 3;
int commitCountToSnapshot = 5;
Config config = createConfig(nodeCount, nodeCount);
config.getCPSubsystemConfig().getRaftAlgorithmConfig().setCommitIndexAdvanceCountToSnapshot(commitCountToSnapshot);
HazelcastInstance[] instances = new HazelcastInstance[nodeCount];
for (int i = 0; i < nodeCount; i++) {
instances[i] = factory.newHazelcastInstance(config);
}
waitUntilCPDiscoveryCompleted(instances);
waitAllForLeaderElection(instances, getMetadataGroupId(instances[0]));
RaftEndpoint leaderEndpoint = getLeaderMember(getRaftNode(instances[0], getMetadataGroupId(instances[0])));
HazelcastInstance leader = getInstance(leaderEndpoint);
HazelcastInstance follower = getRandomFollowerInstance(instances, getMetadataGroupId(instances[0]));
dropOperationsBetween(leader, follower, RaftServiceDataSerializerHook.F_ID, singletonList(APPEND_REQUEST_OP));
List<CPGroupId> groupIds = new ArrayList<>();
for (int i = 0; i < commitCountToSnapshot; i++) {
CPGroupId groupId = createNewRaftGroup(leader, "id" + i, nodeCount);
groupIds.add(groupId);
}
assertTrueEventually(() -> assertTrue(getSnapshotEntry(getRaftNode(leader, getMetadataGroupId(leader))).index() > 0));
assertTrueEventually(() -> {
for (CPGroupId groupId : groupIds) {
assertNotNull(getRaftNode(follower, groupId));
}
});
}
use of com.hazelcast.cp.internal.raft.impl.RaftEndpoint in project hazelcast by hazelcast.
the class HazelcastRaftTestSupport method getLeaderInstance.
protected static HazelcastInstance getLeaderInstance(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 && leaderEndpoint.getUuid().equals(cpMember.getUuid())) {
return instance;
}
}
throw new AssertionError();
}
use of com.hazelcast.cp.internal.raft.impl.RaftEndpoint in project hazelcast by hazelcast.
the class RaftState method updateGroupMembers.
/**
* Initializes the last applied group members with the members and logIndex.
* This method expects there's no uncommitted membership changes, committed members are the same as
* the last applied members.
*
* Leader state is updated for the members which don't exist in committed members and committed members
* those don't exist in latest applied members are removed.
*
* @param logIndex log index of membership change
* @param members latest applied members
*/
public void updateGroupMembers(long logIndex, Collection<RaftEndpoint> members) {
assert committedGroupMembers == lastGroupMembers : "Cannot update group members to: " + members + " at log index: " + logIndex + " because last group members: " + lastGroupMembers + " is different than committed group members: " + committedGroupMembers;
assert lastGroupMembers.index() < logIndex : "Cannot update group members to: " + members + " at log index: " + logIndex + " because last group members: " + lastGroupMembers + " has a bigger log index.";
RaftGroupMembers newGroupMembers = new RaftGroupMembers(logIndex, members, localEndpoint);
committedGroupMembers = lastGroupMembers;
lastGroupMembers = newGroupMembers;
if (leaderState != null) {
for (RaftEndpoint endpoint : members) {
if (!committedGroupMembers.isKnownMember(endpoint)) {
leaderState.add(endpoint, log.lastLogOrSnapshotIndex());
}
}
for (RaftEndpoint endpoint : committedGroupMembers.remoteMembers()) {
if (!members.contains(endpoint)) {
leaderState.remove(endpoint);
}
}
}
}
use of com.hazelcast.cp.internal.raft.impl.RaftEndpoint 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));
}
}
use of com.hazelcast.cp.internal.raft.impl.RaftEndpoint in project hazelcast by hazelcast.
the class CreateRaftNodeOp method readInternal.
@Override
protected void readInternal(ObjectDataInput in) throws IOException {
super.readInternal(in);
groupId = in.readObject();
int count = in.readInt();
initialMembers = new ArrayList<>(count);
for (int i = 0; i < count; i++) {
RaftEndpoint member = in.readObject();
initialMembers.add(member);
}
}
Aggregations