use of com.hazelcast.cp.internal.raft.impl.RaftNodeImpl 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.internal.raft.impl.RaftNodeImpl in project hazelcast by hazelcast.
the class CPMemberAddRemoveTest method testNodesBecomeAP_whenMoreThanInitialRaftMembers_areStartedConcurrently.
@Test
public void testNodesBecomeAP_whenMoreThanInitialRaftMembers_areStartedConcurrently() {
Config config = createConfig(4, 3);
Collection<Future<HazelcastInstance>> futures = new ArrayList<>();
int nodeCount = 8;
for (int i = 0; i < nodeCount; i++) {
Future<HazelcastInstance> future = spawn(() -> factory.newHazelcastInstance(config));
futures.add(future);
}
Collection<HazelcastInstance> instances = returnWithDeadline(futures, ASSERT_TRUE_EVENTUALLY_TIMEOUT, TimeUnit.SECONDS);
assertClusterSizeEventually(nodeCount, instances);
HazelcastInstance[] instancesArray = instances.toArray(new HazelcastInstance[0]);
waitUntilCPDiscoveryCompleted(instancesArray);
assertTrueEventually(() -> {
int cpCount = 0;
int metadataCount = 0;
long commitIndex = getCommitIndex(getLeaderNode(instancesArray, getMetadataGroupId(instancesArray[0])));
for (HazelcastInstance instance : instances) {
assertTrue(instance.getLifecycleService().isRunning());
if (instance.getCPSubsystem().getLocalCPMember() != null) {
cpCount++;
}
RaftNodeImpl raftNode = getRaftNode(instance, getMetadataGroupId(instance));
if (raftNode != null) {
assertEquals(commitIndex, getCommitIndex(raftNode));
if (queryRaftGroupLocally(instance, getMetadataGroupId(instance)) != null) {
metadataCount++;
}
}
}
assertEquals(4, cpCount);
assertEquals(3, metadataCount);
});
}
use of com.hazelcast.cp.internal.raft.impl.RaftNodeImpl 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.internal.raft.impl.RaftNodeImpl in project hazelcast by hazelcast.
the class CountDownLatchAdvancedTest method testNewRaftGroupMemberSchedulesTimeoutsWithSnapshot.
@Test
public void testNewRaftGroupMemberSchedulesTimeoutsWithSnapshot() throws ExecutionException, InterruptedException {
latch.trySetCount(1);
spawn(() -> {
try {
latch.await(10, MINUTES);
} catch (InterruptedException e) {
e.printStackTrace();
}
});
CPGroupId groupId = getGroupId(latch);
assertTrueEventually(() -> {
HazelcastInstance leader = getLeaderInstance(instances, groupId);
CountDownLatchService service = getNodeEngineImpl(leader).getService(CountDownLatchService.SERVICE_NAME);
ResourceRegistry registry = service.getRegistryOrNull(groupId);
assertFalse(registry.getWaitTimeouts().isEmpty());
});
for (int i = 0; i < LOG_ENTRY_COUNT_TO_SNAPSHOT; i++) {
latch.trySetCount(1);
}
assertTrueEventually(() -> {
for (HazelcastInstance instance : instances) {
RaftNodeImpl raftNode = getRaftNode(instance, groupId);
assertNotNull(raftNode);
LogEntry snapshotEntry = getSnapshotEntry(raftNode);
assertTrue(snapshotEntry.index() > 0);
List<RestoreSnapshotOp> ops = (List<RestoreSnapshotOp>) snapshotEntry.operation();
for (RestoreSnapshotOp op : ops) {
if (op.getServiceName().equals(CountDownLatchService.SERVICE_NAME)) {
ResourceRegistry registry = (ResourceRegistry) op.getSnapshot();
assertFalse(registry.getWaitTimeouts().isEmpty());
return;
}
}
fail();
}
});
instances[1].shutdown();
HazelcastInstance newInstance = factory.newHazelcastInstance(createConfig(groupSize, groupSize));
newInstance.getCPSubsystem().getCPSubsystemManagementService().promoteToCPMember().toCompletableFuture().get();
assertTrueEventually(() -> {
CountDownLatchService service = getNodeEngineImpl(newInstance).getService(CountDownLatchService.SERVICE_NAME);
CountDownLatchRegistry registry = service.getRegistryOrNull(groupId);
assertNotNull(registry);
assertFalse(registry.getWaitTimeouts().isEmpty());
Assert.assertEquals(1, registry.getRemainingCount(objectName));
});
}
use of com.hazelcast.cp.internal.raft.impl.RaftNodeImpl in project hazelcast by hazelcast.
the class FencedLockAdvancedTest method testNewRaftGroupMemberSchedulesTimeoutsWithSnapshot.
@Test
public void testNewRaftGroupMemberSchedulesTimeoutsWithSnapshot() throws ExecutionException, InterruptedException {
long fence = this.lock.lockAndGetFence();
assertTrue(fence > 0);
spawn(() -> {
lock.tryLock(10, MINUTES);
});
CPGroupId groupId = this.lock.getGroupId();
assertTrueEventually(() -> {
HazelcastInstance leader = getLeaderInstance(instances, groupId);
LockService service = getNodeEngineImpl(leader).getService(LockService.SERVICE_NAME);
ResourceRegistry registry = service.getRegistryOrNull(groupId);
assertNotNull(registry);
assertFalse(registry.getWaitTimeouts().isEmpty());
});
spawn(() -> {
for (int i = 0; i < LOG_ENTRY_COUNT_TO_SNAPSHOT; i++) {
lock.isLocked();
}
});
assertTrueEventually(() -> {
for (HazelcastInstance instance : instances) {
RaftNodeImpl raftNode = getRaftNode(instance, groupId);
assertNotNull(raftNode);
LogEntry snapshotEntry = getSnapshotEntry(raftNode);
assertTrue(snapshotEntry.index() > 0);
List<RestoreSnapshotOp> ops = (List<RestoreSnapshotOp>) snapshotEntry.operation();
for (RestoreSnapshotOp op : ops) {
if (op.getServiceName().equals(LockService.SERVICE_NAME)) {
ResourceRegistry registry = (ResourceRegistry) op.getSnapshot();
assertFalse(registry.getWaitTimeouts().isEmpty());
return;
}
}
fail();
}
});
HazelcastInstance instanceToShutdown = (instances[0] == proxyInstance) ? instances[1] : instances[0];
instanceToShutdown.shutdown();
HazelcastInstance newInstance = factory.newHazelcastInstance(createConfig(groupSize, groupSize));
newInstance.getCPSubsystem().getCPSubsystemManagementService().promoteToCPMember().toCompletableFuture().get();
assertTrueEventually(() -> {
RaftNodeImpl raftNode = getRaftNode(newInstance, groupId);
assertNotNull(raftNode);
assertTrue(getSnapshotEntry(raftNode).index() > 0);
LockService service = getNodeEngineImpl(newInstance).getService(LockService.SERVICE_NAME);
LockRegistry registry = service.getRegistryOrNull(groupId);
assertNotNull(registry);
assertFalse(registry.getWaitTimeouts().isEmpty());
LockOwnershipState ownership = registry.getLockOwnershipState(objectName);
assertTrue(ownership.isLocked());
assertTrue(ownership.getLockCount() > 0);
assertEquals(fence, ownership.getFence());
});
}
Aggregations