use of com.hazelcast.cp.CPMember in project hazelcast by hazelcast.
the class CPMemberAddRemoveTest method testRaftMemberIsRemovedForGracefulShutdown.
@Test
public void testRaftMemberIsRemovedForGracefulShutdown() throws ExecutionException, InterruptedException {
HazelcastInstance[] instances = newInstances(3, 3, 0);
CPMember shutdownCPMember = instances[0].getCPSubsystem().getLocalCPMember();
instances[0].getLifecycleService().shutdown();
RaftInvocationManager invocationManager = getRaftInvocationManager(instances[1]);
CPGroupId metadataGroupId = getMetadataGroupId(instances[1]);
MembershipChangeSchedule schedule = invocationManager.<MembershipChangeSchedule>query(metadataGroupId, new GetMembershipChangeScheduleOp(), LINEARIZABLE).get();
assertNull(schedule);
CPGroup group = invocationManager.<CPGroup>invoke(metadataGroupId, new GetRaftGroupOp(metadataGroupId)).join();
assertEquals(2, group.members().size());
for (CPMember member : group.members()) {
assertNotEquals(shutdownCPMember, member);
}
}
use of com.hazelcast.cp.CPMember in project hazelcast by hazelcast.
the class CPMemberAddRemoveTest method when_newCPMemberIsAddedToTheMetadataGroupAfterRestartAndSnapshot_newMemberInstallsSnapshot.
@Test
public void when_newCPMemberIsAddedToTheMetadataGroupAfterRestartAndSnapshot_newMemberInstallsSnapshot() throws ExecutionException, InterruptedException {
int nodeCount = 3;
int commitIndexAdvanceCountToSnapshot = 50;
Config config = createConfig(nodeCount, nodeCount);
config.getCPSubsystemConfig().getRaftAlgorithmConfig().setCommitIndexAdvanceCountToSnapshot(commitIndexAdvanceCountToSnapshot);
HazelcastInstance[] instances = new HazelcastInstance[nodeCount];
for (int i = 0; i < nodeCount; i++) {
instances[i] = factory.newHazelcastInstance(config);
}
assertClusterSizeEventually(nodeCount, instances);
waitUntilCPDiscoveryCompleted(instances);
RaftGroupId initialMetadataGroupId = getMetadataGroupId(instances[0]);
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());
for (int i = 0; i < commitIndexAdvanceCountToSnapshot; i++) {
getRaftInvocationManager(instances[0]).invoke(getMetadataGroupId(instances[0]), new GetActiveCPMembersOp()).get();
}
assertTrueEventually(() -> {
assertTrue(getSnapshotEntry(getRaftNode(instances[0], newMetadataGroupId)).index() >= commitIndexAdvanceCountToSnapshot);
assertTrue(getSnapshotEntry(getRaftNode(newInstance1, newMetadataGroupId)).index() >= commitIndexAdvanceCountToSnapshot);
assertTrue(getSnapshotEntry(getRaftNode(newInstance2, newMetadataGroupId)).index() >= commitIndexAdvanceCountToSnapshot);
});
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, newMetadataGroupId));
for (HazelcastInstance instance : Arrays.asList(newInstance1, newInstance2, newInstance3)) {
assertEquals(newMetadataGroupId.getSeed(), getMetadataGroupId(instance).getSeed());
RaftNodeImpl raftNode = getRaftNode(instance, newMetadataGroupId);
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.CPMember in project hazelcast by hazelcast.
the class CPMemberAddRemoveTest method testExpandRaftGroup.
@Test
public void testExpandRaftGroup() throws ExecutionException, InterruptedException, TimeoutException {
HazelcastInstance[] instances = newInstances(3, 3, 1);
instances[0].shutdown();
instances[3].getCPSubsystem().getCPSubsystemManagementService().promoteToCPMember().toCompletableFuture().get(30, TimeUnit.SECONDS);
CPGroupId metadataGroupId = getMetadataGroupId(instances[1]);
CPGroup group = instances[1].getCPSubsystem().getCPSubsystemManagementService().getCPGroup(METADATA_CP_GROUP_NAME).toCompletableFuture().get();
assertEquals(3, group.members().size());
Collection<CPMember> members = group.members();
assertTrue(members.contains(instances[3].getCPSubsystem().getLocalCPMember()));
assertTrueEventually(() -> assertNotNull(getRaftNode(instances[3], metadataGroupId)));
}
use of com.hazelcast.cp.CPMember in project hazelcast by hazelcast.
the class CPMemberAddRemoveTest method testExpandMultipleRaftGroupsMultipleTimes.
@Test
public void testExpandMultipleRaftGroupsMultipleTimes() throws ExecutionException, InterruptedException, TimeoutException {
HazelcastInstance[] instances = newInstances(5, 5, 2);
CPGroupId metadataGroupId = getMetadataGroupId(instances[0]);
CPSubsystemManagementService managementService = instances[6].getCPSubsystem().getCPSubsystemManagementService();
String groupName = "group1";
instances[0].getCPSubsystem().getAtomicLong("long1@" + groupName).set(5);
CPGroup otherGroup = managementService.getCPGroup(groupName).toCompletableFuture().get();
CPGroupId groupId = otherGroup.id();
waitAllForLeaderElection(Arrays.copyOf(instances, 5), groupId);
CPMember[] otherGroupMembers = otherGroup.members().toArray(new CPMember[0]);
List<Address> shutdownAddresses = asList(otherGroupMembers[0].getAddress(), otherGroupMembers[1].getAddress());
instances[5].getCPSubsystem().getCPSubsystemManagementService().promoteToCPMember().toCompletableFuture().get(30, TimeUnit.SECONDS);
HazelcastInstance[] runningInstances = new HazelcastInstance[instances.length - shutdownAddresses.size()];
for (int i = 0, j = 0; i < instances.length; i++) {
HazelcastInstance instance = instances[i];
if (shutdownAddresses.contains(getAddress(instance))) {
instance.shutdown();
} else {
runningInstances[j++] = instance;
}
}
CPGroup metadataGroup = managementService.getCPGroup(metadataGroupId.getName()).toCompletableFuture().get();
otherGroup = managementService.getCPGroup(groupName).toCompletableFuture().get();
assertEquals(4, metadataGroup.members().size());
assertEquals(4, otherGroup.members().size());
assertTrueEventually(() -> {
assertNotNull(getRaftNode(instances[5], metadataGroupId));
assertNotNull(getRaftNode(instances[5], groupId));
});
instances[6].getCPSubsystem().getCPSubsystemManagementService().promoteToCPMember().toCompletableFuture().get(30, TimeUnit.SECONDS);
metadataGroup = managementService.getCPGroup(metadataGroupId.getName()).toCompletableFuture().get();
otherGroup = managementService.getCPGroup(groupName).toCompletableFuture().get();
assertEquals(5, metadataGroup.members().size());
assertEquals(5, otherGroup.members().size());
CPGroup metadataGroupRef = metadataGroup;
CPGroup otherGroupRef = otherGroup;
assertTrueEventually(() -> {
long commitIndex = getCommitIndex(getLeaderNode(runningInstances, metadataGroupId));
assertNotNull(getRaftNode(instances[6], groupId));
for (HazelcastInstance instance : asList(instances[5], instances[6])) {
RaftNodeImpl raftNode = getRaftNode(instance, metadataGroupId);
assertNotNull(raftNode);
assertEquals(commitIndex, getCommitIndex(raftNode));
CPGroup g1 = queryRaftGroupLocally(instance, metadataGroupId);
CPGroup g2 = queryRaftGroupLocally(instance, otherGroupRef.id());
assertNotNull(g1);
assertNotNull(g2);
assertArrayEquals(metadataGroupRef.members().toArray(new CPMember[0]), g1.members().toArray(new CPMember[0]));
assertArrayEquals(otherGroupRef.members().toArray(new CPMember[0]), g2.members().toArray(new CPMember[0]));
}
});
}
use of com.hazelcast.cp.CPMember in project hazelcast by hazelcast.
the class CPMemberAddRemoveTest method when_snapshotIsTakenWhileRemovingCPLeader_newMemberInstallsSnapshot.
@Test
public void when_snapshotIsTakenWhileRemovingCPLeader_newMemberInstallsSnapshot() throws Exception {
int nodeCount = 3;
int commitIndexAdvanceCountToSnapshot = 50;
Config config = createConfig(nodeCount, nodeCount);
config.getCPSubsystemConfig().getRaftAlgorithmConfig().setCommitIndexAdvanceCountToSnapshot(commitIndexAdvanceCountToSnapshot);
HazelcastInstance[] instances = new HazelcastInstance[nodeCount];
for (int i = 0; i < nodeCount; i++) {
instances[i] = factory.newHazelcastInstance(config);
}
assertClusterSizeEventually(nodeCount, instances);
waitUntilCPDiscoveryCompleted(instances);
HazelcastInstance leaderInstance = getLeaderInstance(instances, getMetadataGroupId(instances[0]));
// Specifically, RemoveCPMemberOp will be in snapshot but CompleteRaftGroupMembershipChangesOp will not.
for (int i = 0; i < commitIndexAdvanceCountToSnapshot - 5; i++) {
getRaftInvocationManager(leaderInstance).invoke(getMetadataGroupId(instances[0]), new GetActiveCPMembersOp()).get();
}
// This will add 3 entries, RemoveCPMemberOp, ChangeRaftGroupMembersCmd and CompleteRaftGroupMembershipChangesOp.
// RemoveCPMemberOp will be in snapshot but CompleteRaftGroupMembershipChangesOp will not be included.
leaderInstance.shutdown();
HazelcastInstance newInstance = factory.newHazelcastInstance(config);
newInstance.getCPSubsystem().getCPSubsystemManagementService().promoteToCPMember().toCompletableFuture().join();
List<CPMember> cpMembers = new ArrayList<>(newInstance.getCPSubsystem().getCPSubsystemManagementService().getCPMembers().toCompletableFuture().join());
assertTrueEventually(() -> {
RaftService service = getRaftService(newInstance);
List<CPMemberInfo> activeMembers = new ArrayList<>(service.getMetadataGroupManager().getActiveMembers());
assertEquals(cpMembers, activeMembers);
});
}
Aggregations