Search in sources :

Example 1 with MemberGroup

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);
}
Also used : Address(com.hazelcast.nio.Address) InetAddress(java.net.InetAddress) HashMap(java.util.HashMap) MemberImpl(com.hazelcast.instance.MemberImpl) DefaultMemberGroup(com.hazelcast.partition.membergroup.DefaultMemberGroup) MemberGroup(com.hazelcast.partition.membergroup.MemberGroup) Collection(java.util.Collection) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Example 2 with MemberGroup

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;
}
Also used : MemberGroup(com.hazelcast.partition.membergroup.MemberGroup) ClusterState(com.hazelcast.cluster.ClusterState) HazelcastException(com.hazelcast.core.HazelcastException) Address(com.hazelcast.nio.Address)

Aggregations

Address (com.hazelcast.nio.Address)2 MemberGroup (com.hazelcast.partition.membergroup.MemberGroup)2 ClusterState (com.hazelcast.cluster.ClusterState)1 HazelcastException (com.hazelcast.core.HazelcastException)1 MemberImpl (com.hazelcast.instance.MemberImpl)1 DefaultMemberGroup (com.hazelcast.partition.membergroup.DefaultMemberGroup)1 InetAddress (java.net.InetAddress)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1