use of com.hazelcast.cp.CPMember in project hazelcast by hazelcast.
the class TimedMemberStateFactory method createMemberState.
private void createMemberState(MemberStateImpl memberState, Collection<StatisticsAwareService> services) {
Node node = instance.node;
final Collection<Client> clients = instance.node.clientEngine.getClients();
final Set<ClientEndPointDTO> serializableClientEndPoints = createHashSet(clients.size());
for (Client client : clients) {
serializableClientEndPoints.add(new ClientEndPointDTO(client));
}
memberState.setClients(serializableClientEndPoints);
memberState.setName(instance.getName());
memberState.setUuid(node.getThisUuid());
if (instance.getConfig().getCPSubsystemConfig().getCPMemberCount() == 0) {
memberState.setCpMemberUuid(null);
} else {
CPMember localCPMember = instance.getCPSubsystem().getLocalCPMember();
memberState.setCpMemberUuid(localCPMember != null ? localCPMember.getUuid() : null);
}
Address thisAddress = node.getThisAddress();
memberState.setAddress(thisAddress.getHost() + ":" + thisAddress.getPort());
memberState.setEndpoints(node.getLocalMember().getAddressMap());
MemberPartitionStateImpl memberPartitionState = (MemberPartitionStateImpl) memberState.getMemberPartitionState();
InternalPartitionService partitionService = node.getPartitionService();
IPartition[] partitions = partitionService.getPartitions();
List<Integer> partitionList = memberPartitionState.getPartitions();
for (IPartition partition : partitions) {
if (partition.isLocal()) {
partitionList.add(partition.getPartitionId());
}
}
memberPartitionState.setMemberStateSafe(memberStateSafe);
memberState.setOperationStats(getOperationStats());
createMemState(memberState, services);
createNodeState(memberState);
createHotRestartState(memberState);
createClusterHotRestartStatus(memberState);
memberState.setClientStats(getClientAttributes(node.getClientEngine().getClientStatistics()));
}
use of com.hazelcast.cp.CPMember in project hazelcast by hazelcast.
the class CPGroupRebalanceTest method test.
@Test
public void test() throws Exception {
int groupSize = 5;
int leadershipsPerMember = 10;
int groupCount = groupSize * leadershipsPerMember;
HazelcastInstance[] instances = newInstances(groupSize, groupSize, 0);
waitUntilCPDiscoveryCompleted(instances);
Collection<CPGroupId> groupIds = new ArrayList<>(groupCount);
RaftInvocationManager invocationManager = getRaftInvocationManager(instances[0]);
for (int i = 0; i < groupCount; i++) {
RaftGroupId groupId = invocationManager.createRaftGroup("group-" + i).joinInternal();
groupIds.add(groupId);
}
HazelcastInstance metadataLeader = getLeaderInstance(instances, getMetadataGroupId(instances[0]));
Collection<CPMember> cpMembers = metadataLeader.getCPSubsystem().getCPSubsystemManagementService().getCPMembers().toCompletableFuture().get();
// Assert eventually since during the test
// a long pause can cause leadership change unexpectedly.
assertTrueEventually(() -> {
// Wait for leader election all groups
for (CPGroupId groupId : groupIds) {
waitAllForLeaderElection(instances, groupId);
}
rebalanceLeaderships(metadataLeader);
Map<CPMember, Collection<CPGroupId>> leadershipsMap = getLeadershipsMap(metadataLeader, cpMembers);
for (Entry<CPMember, Collection<CPGroupId>> entry : leadershipsMap.entrySet()) {
int count = entry.getValue().size();
assertEquals(leadershipsString(leadershipsMap), leadershipsPerMember, count);
}
});
}
use of com.hazelcast.cp.CPMember 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.CPMember 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.CPMember in project hazelcast by hazelcast.
the class CPMemberAddRemoveTest method testMetadataGroupReinitializationAfterLostMajority.
@Test
public void testMetadataGroupReinitializationAfterLostMajority() throws ExecutionException, InterruptedException {
HazelcastInstance[] instances = newInstances(3, 3, 1);
long groupIdSeed = getRaftService(instances[0]).getMetadataGroupManager().getGroupIdSeed();
RaftGroupId groupId = getRaftInvocationManager(instances[0]).createRaftGroup(CPGroup.DEFAULT_GROUP_NAME).get();
IAtomicLong long1 = instances[0].getCPSubsystem().getAtomicLong("proxy");
sleepAtLeastMillis(10);
instances[1].getLifecycleService().terminate();
instances[2].getLifecycleService().terminate();
assertClusterSizeEventually(2, instances[3]);
HazelcastInstance[] newInstances = new HazelcastInstance[3];
newInstances[0] = instances[0];
newInstances[1] = instances[3];
Config config = createConfig(3, 3);
newInstances[2] = factory.newHazelcastInstance(config);
assertClusterSizeEventually(3, newInstances);
newInstances[0].getCPSubsystem().getCPSubsystemManagementService().reset().toCompletableFuture().get();
waitUntilCPDiscoveryCompleted(newInstances);
long newGroupIdSeed = getRaftService(newInstances[0]).getMetadataGroupManager().getGroupIdSeed();
RaftGroupId newGroupId = getRaftInvocationManager(instances[0]).createRaftGroup(CPGroup.DEFAULT_GROUP_NAME).get();
assertThat(newGroupIdSeed, greaterThan(groupIdSeed));
assertThat(newGroupId.getSeed(), greaterThan(groupId.getSeed()));
try {
long1.incrementAndGet();
fail();
} catch (CPGroupDestroyedException ignored) {
}
IAtomicLong long2 = newInstances[2].getCPSubsystem().getAtomicLong("proxy");
long2.incrementAndGet();
assertTrueEventually(() -> {
CPGroupSummary group = queryRaftGroupLocally(newInstances[2], getMetadataGroupId(newInstances[2]));
assertNotNull(group);
Collection<CPMember> endpoints = group.members();
for (HazelcastInstance instance : newInstances) {
Member localMember = instance.getCluster().getLocalMember();
assertThat(new CPMemberInfo(localMember), isIn(endpoints));
}
});
}
Aggregations