use of com.hazelcast.partition.membergroup.MemberGroup in project hazelcast by hazelcast.
the class PartitionStateGeneratorTest method checkTestResult.
private void checkTestResult(Address[][] state, Collection<MemberGroup> groups, int partitionCount) {
Iterator<MemberGroup> iter = groups.iterator();
while (iter.hasNext()) {
if (iter.next().size() == 0) {
iter.remove();
}
}
int replicaCount = Math.min(groups.size(), InternalPartition.MAX_REPLICA_COUNT);
Map<MemberGroup, GroupPartitionState> groupPartitionStates = new HashMap<MemberGroup, GroupPartitionState>();
Set<Address> set = new HashSet<Address>();
int avgPartitionPerGroup = partitionCount / groups.size();
for (int partitionId = 0; partitionId < partitionCount; partitionId++) {
Address[] replicas = state[partitionId];
for (int i = 0; i < replicaCount; i++) {
Address owner = replicas[i];
assertNotNull(owner);
assertFalse("Duplicate owner of partition: " + partitionId, set.contains(owner));
set.add(owner);
MemberGroup group = null;
for (MemberGroup g : groups) {
if (g.hasMember(new MemberImpl(owner, VERSION, true))) {
group = g;
break;
}
}
assertNotNull(group);
GroupPartitionState groupState = groupPartitionStates.get(group);
if (groupState == null) {
groupState = new GroupPartitionState();
groupState.group = group;
groupPartitionStates.put(group, groupState);
}
groupState.groupPartitions[i].add(partitionId);
groupState.getNodePartitions(owner)[i].add(partitionId);
}
set.clear();
}
for (GroupPartitionState groupState : groupPartitionStates.values()) {
for (Map.Entry<Address, Set<Integer>[]> entry : groupState.nodePartitionsMap.entrySet()) {
Collection<Integer>[] partitions = entry.getValue();
for (int i = 0; i < replicaCount; i++) {
int avgPartitionPerNode = groupState.groupPartitions[i].size() / groupState.nodePartitionsMap.size();
int count = partitions[i].size();
isInAllowedRange(count, avgPartitionPerNode, i, entry.getKey(), groups, partitionCount);
}
}
Collection<Integer>[] partitions = groupState.groupPartitions;
for (int i = 0; i < replicaCount; i++) {
int count = partitions[i].size();
isInAllowedRange(count, avgPartitionPerGroup, i, groupState.group, groups, partitionCount);
}
}
printTable(groupPartitionStates, replicaCount);
}
use of com.hazelcast.partition.membergroup.MemberGroup in project hazelcast by hazelcast.
the class PartitionStateManager method initializePartitionAssignments.
boolean initializePartitionAssignments(Set<Address> excludedAddresses) {
if (!isPartitionAssignmentAllowed()) {
return false;
}
Collection<MemberGroup> memberGroups = createMemberGroups(excludedAddresses);
if (memberGroups.isEmpty()) {
logger.warning("No member group is available to assign partition ownership...");
return false;
}
logger.info("Initializing cluster partition table arrangement...");
Address[][] newState = partitionStateGenerator.arrange(memberGroups, partitions);
if (newState.length != partitionCount) {
throw new HazelcastException("Invalid partition count! " + "Expected: " + partitionCount + ", Actual: " + newState.length);
}
// increment state version to make fail cluster state transaction
// if it's started and not locked the state yet.
stateVersion.incrementAndGet();
ClusterState clusterState = node.getClusterService().getClusterState();
if (clusterState != ClusterState.ACTIVE) {
// cluster state is either changed or locked, decrement version back and fail.
stateVersion.decrementAndGet();
logger.warning("Partitions can't be assigned since cluster-state= " + clusterState);
return false;
}
for (int partitionId = 0; partitionId < partitionCount; partitionId++) {
InternalPartitionImpl partition = partitions[partitionId];
Address[] replicas = newState[partitionId];
partition.setReplicaAddresses(replicas);
}
setInitialized();
return true;
}
Aggregations