use of com.hazelcast.spi.partitiongroup.MemberGroup in project hazelcast by hazelcast.
the class PartitionStateGeneratorTest method testOnlyUnassignedArrangement.
@Test
public void testOnlyUnassignedArrangement() throws Exception {
List<Member> memberList = createMembers(10, 1);
MemberGroupFactory memberGroupFactory = new SingleMemberGroupFactory();
Collection<MemberGroup> groups = memberGroupFactory.createMemberGroups(memberList);
PartitionStateGenerator generator = new PartitionStateGeneratorImpl();
PartitionReplica[][] state = generator.arrange(groups, emptyPartitionArray(100));
// unassign some partitions entirely
Collection<Integer> unassignedPartitions = new ArrayList<Integer>();
for (int i = 0; i < state.length; i++) {
if (i % 3 == 0) {
state[i] = new PartitionReplica[InternalPartition.MAX_REPLICA_COUNT];
unassignedPartitions.add(i);
}
}
// unassign only backup replicas of some partitions
for (int i = 0; i < state.length; i++) {
if (i % 10 == 0) {
Arrays.fill(state[i], 1, InternalPartition.MAX_REPLICA_COUNT, null);
}
}
InternalPartition[] partitions = toPartitionArray(state);
state = generator.arrange(groups, partitions, unassignedPartitions);
for (int pid = 0; pid < state.length; pid++) {
PartitionReplica[] addresses = state[pid];
if (unassignedPartitions.contains(pid)) {
for (PartitionReplica address : addresses) {
assertNotNull(address);
}
} else {
InternalPartition partition = partitions[pid];
for (int replicaIx = 0; replicaIx < InternalPartition.MAX_REPLICA_COUNT; replicaIx++) {
assertEquals(partition.getReplica(replicaIx), addresses[replicaIx]);
}
}
}
}
use of com.hazelcast.spi.partitiongroup.MemberGroup in project hazelcast by hazelcast.
the class MemberGroupFactoryTest method testConfigMemberGroupFactoryCreateMemberGroups.
/**
* When there is a matching {@link MemberGroupConfig} for a {@link Member}, it will be assigned to a {@link MemberGroup}.
* <p>
* In this test all members will have a matching configuration, so there will be 4 groups with 2 members each.
*/
@Test
public void testConfigMemberGroupFactoryCreateMemberGroups() {
Collection<Member> members = createMembers();
Collection<MemberGroupConfig> groupConfigs = createMemberGroupConfigs(true);
MemberGroupFactory groupFactory = new ConfigMemberGroupFactory(groupConfigs);
Collection<MemberGroup> memberGroups = groupFactory.createMemberGroups(members);
assertEquals("Member Groups: " + String.valueOf(memberGroups), 4, memberGroups.size());
for (MemberGroup memberGroup : memberGroups) {
assertEquals("Member Group: " + String.valueOf(memberGroup), 2, memberGroup.size());
}
}
use of com.hazelcast.spi.partitiongroup.MemberGroup in project hazelcast by hazelcast.
the class MemberGroupFactoryTest method testPlacementMetadataAwareMemberGroupFactoryCreateMemberGroups.
@Test
public void testPlacementMetadataAwareMemberGroupFactoryCreateMemberGroups() {
MemberGroupFactory groupFactory = new PlacementAwareMemberGroupFactory();
Collection<Member> members = createMembersWithPlacementAwareMetadata();
Collection<MemberGroup> memberGroups = groupFactory.createMemberGroups(members);
assertEquals("Member Groups: " + memberGroups, 3, memberGroups.size());
for (MemberGroup memberGroup : memberGroups) {
assertEquals("Member Group: " + memberGroup, 1, memberGroup.size());
}
}
use of com.hazelcast.spi.partitiongroup.MemberGroup in project hazelcast by hazelcast.
the class PartitionStateManager method initializePartitionAssignments.
/**
* Arranges the partitions if:
* <ul>
* <li>this instance {@link NodeExtension#isStartCompleted()}</li>
* <li>the cluster state allows migrations. See {@link ClusterState#isMigrationAllowed()}</li>
* </ul>
* This will also set the manager state to initialized (if not already) and invoke the
* {@link DefaultPartitionReplicaInterceptor} for all changed replicas which
* will cancel replica synchronizations and increase the partition state version.
*
* @param excludedMembers members which are to be excluded from the new layout
* @return if the new partition was assigned
* @throws HazelcastException if the partition state generator failed to arrange the partitions
*/
boolean initializePartitionAssignments(Set<Member> excludedMembers) {
if (!isPartitionAssignmentAllowed()) {
return false;
}
Collection<MemberGroup> memberGroups = createMemberGroups(excludedMembers);
if (memberGroups.isEmpty()) {
logger.warning("No member group is available to assign partition ownership...");
return false;
}
logger.info("Initializing cluster partition table arrangement...");
PartitionReplica[][] newState = partitionStateGenerator.arrange(memberGroups, partitions);
if (newState.length != partitionCount) {
throw new HazelcastException("Invalid partition count! " + "Expected: " + partitionCount + ", Actual: " + newState.length);
}
for (int partitionId = 0; partitionId < partitionCount; partitionId++) {
InternalPartitionImpl partition = partitions[partitionId];
PartitionReplica[] replicas = newState[partitionId];
partition.setReplicas(replicas);
}
ClusterState clusterState = node.getClusterService().getClusterState();
if (!clusterState.isMigrationAllowed()) {
// cluster state is either changed or locked, reset state back and fail.
reset();
logger.warning("Partitions can't be assigned since cluster-state= " + clusterState);
return false;
}
setInitialized();
return true;
}
use of com.hazelcast.spi.partitiongroup.MemberGroup in project hazelcast by hazelcast.
the class PartitionStateManager method createMemberGroups.
private Collection<MemberGroup> createMemberGroups(final Set<Member> excludedMembers) {
MemberSelector exclude = member -> !excludedMembers.contains(member);
final MemberSelector selector = MemberSelectors.and(DATA_MEMBER_SELECTOR, exclude);
final Collection<Member> members = node.getClusterService().getMembers(selector);
return memberGroupFactory.createMemberGroups(members);
}
Aggregations