use of com.hazelcast.cp.internal.raft.impl.RaftNodeImpl in project hazelcast by hazelcast.
the class CPMemberAddRemoveTest method when_newCPMemberIsAddedToTheMetadataGroupAfterRestart_newMemberCommitsMetadataGroupLogEntries.
@Test
public void when_newCPMemberIsAddedToTheMetadataGroupAfterRestart_newMemberCommitsMetadataGroupLogEntries() throws ExecutionException, InterruptedException {
int nodeCount = 3;
Config config = createConfig(nodeCount, nodeCount);
HazelcastInstance[] instances = new HazelcastInstance[nodeCount];
for (int i = 0; i < nodeCount; i++) {
instances[i] = factory.newHazelcastInstance(config);
}
RaftGroupId initialMetadataGroupId = getMetadataGroupId(instances[0]);
assertClusterSizeEventually(nodeCount, instances);
waitUntilCPDiscoveryCompleted(instances);
instances[0].getCPSubsystem().getCPSubsystemManagementService().getCPGroup(METADATA_CP_GROUP_NAME).toCompletableFuture().get();
instances[1].getLifecycleService().terminate();
instances[2].getLifecycleService().terminate();
HazelcastInstance newInstance1 = factory.newHazelcastInstance(config);
HazelcastInstance newInstance2 = factory.newHazelcastInstance(config);
assertClusterSizeEventually(3, instances[0], newInstance1, newInstance2);
instances[0].getCPSubsystem().getCPSubsystemManagementService().reset().toCompletableFuture().get();
RaftGroupId newMetadataGroupId = getRaftService(instances[0]).getMetadataGroupId();
assertTrue(newMetadataGroupId.getSeed() > initialMetadataGroupId.getSeed());
assertEquals(newMetadataGroupId.getSeed(), getRaftService(newInstance1).getMetadataGroupId().getSeed());
assertEquals(newMetadataGroupId.getSeed(), getRaftService(newInstance2).getMetadataGroupId().getSeed());
instances[0].getCPSubsystem().getAtomicLong("long@group1").set(1);
instances[0].getCPSubsystem().getAtomicLong("long@group2").set(2);
instances[0].shutdown();
HazelcastInstance newInstance3 = factory.newHazelcastInstance(config);
newInstance3.getCPSubsystem().getCPSubsystemManagementService().promoteToCPMember().toCompletableFuture().get();
CPGroup metadataGroup = newInstance1.getCPSubsystem().getCPSubsystemManagementService().getCPGroup(METADATA_CP_GROUP_NAME).toCompletableFuture().get();
CPGroup group1 = newInstance1.getCPSubsystem().getCPSubsystemManagementService().getCPGroup("group1").toCompletableFuture().get();
CPGroup group2 = newInstance1.getCPSubsystem().getCPSubsystemManagementService().getCPGroup("group2").toCompletableFuture().get();
List<CPMember> cpMembers = new ArrayList<>(newInstance1.getCPSubsystem().getCPSubsystemManagementService().getCPMembers().toCompletableFuture().get());
HazelcastInstance[] newInstances = new HazelcastInstance[] { newInstance1, newInstance2, newInstance3 };
assertTrueEventually(() -> {
long commitIndex = getCommitIndex(getLeaderNode(newInstances, getMetadataGroupId(newInstance1)));
for (HazelcastInstance instance : Arrays.asList(newInstance1, newInstance2, newInstance3)) {
assertEquals(newMetadataGroupId.getSeed(), getMetadataGroupId(instance).getSeed());
RaftNodeImpl raftNode = getRaftNode(instance, getMetadataGroupId(instance));
assertNotNull(raftNode);
assertEquals(commitIndex, getCommitIndex(raftNode));
CPGroup m = queryRaftGroupLocally(instance, metadataGroup.id());
CPGroup g1 = queryRaftGroupLocally(instance, group1.id());
CPGroup g2 = queryRaftGroupLocally(instance, group2.id());
assertNotNull(m);
assertNotNull(g1);
assertNotNull(g2);
assertArrayEquals(metadataGroup.members().toArray(new CPMember[0]), m.members().toArray(new CPMember[0]));
assertArrayEquals(group1.members().toArray(new CPMember[0]), g1.members().toArray(new CPMember[0]));
assertArrayEquals(group2.members().toArray(new CPMember[0]), g2.members().toArray(new CPMember[0]));
List<CPMemberInfo> activeMembers = new ArrayList<>(getRaftService(instance).getMetadataGroupManager().getActiveMembers());
assertEquals(cpMembers, activeMembers);
}
});
}
use of com.hazelcast.cp.internal.raft.impl.RaftNodeImpl in project hazelcast by hazelcast.
the class CPMemberAddRemoveTest method testRemoveMemberFromMajorityLostRaftGroup.
@Test
public void testRemoveMemberFromMajorityLostRaftGroup() throws ExecutionException, InterruptedException {
HazelcastInstance[] instances = newInstances(3, 3, 0);
waitAllForLeaderElection(instances, getMetadataGroupId(instances[0]));
CPGroupId groupId = getRaftInvocationManager(instances[0]).createRaftGroup("test", 2).get();
getRaftInvocationManager(instances[0]).invoke(groupId, new DummyOp()).get();
RaftNodeImpl groupLeaderRaftNode = getLeaderNode(instances, groupId);
CPGroup group = instances[0].getCPSubsystem().getCPSubsystemManagementService().getCPGroup(groupId.getName()).toCompletableFuture().get();
CPMember[] groupMembers = group.members().toArray(new CPMember[0]);
CPMember crashedMember = groupMembers[0].getUuid().equals(groupLeaderRaftNode.getLocalMember().getUuid()) ? groupMembers[1] : groupMembers[0];
HazelcastInstance runningInstance = (getAddress(instances[0])).equals(crashedMember.getAddress()) ? instances[1] : instances[0];
RaftInvocationManager invocationManager = getRaftInvocationManager(runningInstance);
factory.getInstance(crashedMember.getAddress()).getLifecycleService().terminate();
// from now on, "test" group lost the majority
// we triggered removal of the crashed member but we won't be able to commit to the "test" group
CompletableFuture<Void> f = runningInstance.getCPSubsystem().getCPSubsystemManagementService().removeCPMember(crashedMember.getUuid()).toCompletableFuture();
// wait until RaftCleanupHandler kicks in and appends ApplyRaftGroupMembersCmd to the leader of the "test" group
assertTrueEventually(() -> assertTrue(getLastLogOrSnapshotEntry(groupLeaderRaftNode).operation() instanceof UpdateRaftGroupMembersCmd));
// force-destroy the raft group.
// Now, the pending membership change in the "test" group will fail and we will fix it in the metadata group.
runningInstance.getCPSubsystem().getCPSubsystemManagementService().forceDestroyCPGroup(groupId.getName()).toCompletableFuture().get();
f.get();
MembershipChangeSchedule schedule = invocationManager.<MembershipChangeSchedule>query(getMetadataGroupId(runningInstance), new GetMembershipChangeScheduleOp(), LINEARIZABLE).get();
assertNull(schedule);
}
use of com.hazelcast.cp.internal.raft.impl.RaftNodeImpl in project hazelcast by hazelcast.
the class MetadataRaftGroupTest method when_raftGroupIsCreated_onNonMetadataMembers_thenLeaderShouldBeElected.
@Test
public void when_raftGroupIsCreated_onNonMetadataMembers_thenLeaderShouldBeElected() throws ExecutionException, InterruptedException {
int metadataGroupSize = 3;
int otherRaftGroupSize = 2;
instances = newInstances(metadataGroupSize + otherRaftGroupSize, metadataGroupSize, 0);
HazelcastInstance leaderInstance = getLeaderInstance(instances, getMetadataGroupId(instances[0]));
RaftService raftService = getRaftService(leaderInstance);
Collection<CPMemberInfo> allEndpoints = raftService.getMetadataGroupManager().getActiveMembers();
assertTrueEventually(() -> assertNotNull(raftService.getMetadataGroupManager().getGroup(getMetadataGroupId(leaderInstance))));
CPGroup metadataGroup = raftService.getMetadataGroupManager().getGroup(getMetadataGroupId(leaderInstance));
Collection<CPMemberInfo> endpoints = new HashSet<>(otherRaftGroupSize);
for (CPMemberInfo endpoint : allEndpoints) {
if (!metadataGroup.members().contains(endpoint)) {
endpoints.add(endpoint);
}
}
assertEquals(otherRaftGroupSize, endpoints.size());
List<RaftEndpoint> groupEndpoints = new ArrayList<>();
for (CPMemberInfo member : endpoints) {
groupEndpoints.add(member.toRaftEndpoint());
}
RaftOp op = new CreateRaftGroupOp("test", groupEndpoints, RandomPicker.getInt(Integer.MAX_VALUE));
InternalCompletableFuture<CPGroupSummary> f = raftService.getInvocationManager().invoke(getMetadataGroupId(leaderInstance), op);
f.whenCompleteAsync((group, t) -> {
if (t == null) {
raftService.getInvocationManager().triggerRaftNodeCreation(group);
}
});
CPGroupId groupId = f.get().id();
for (HazelcastInstance instance : instances) {
if (endpoints.contains(instance.getCPSubsystem().getLocalCPMember())) {
assertTrueEventually(() -> {
RaftNodeImpl raftNode = getRaftNode(instance, groupId);
assertNotNull(raftNode);
assertNotNull("Leader is null on " + raftNode, getLeaderMember(raftNode));
});
}
}
}
Aggregations