Search in sources :

Example 16 with MemberGroup

use of com.hazelcast.spi.partitiongroup.MemberGroup in project hazelcast by hazelcast.

the class SPIAwareMemberGroupFactory method createInternalMemberGroups.

@Override
protected Set<MemberGroup> createInternalMemberGroups(Collection<? extends Member> allMembers) {
    Set<MemberGroup> memberGroups = createHashSet(allMembers.size());
    for (Member member : allMembers) {
        try {
            if (member.localMember()) {
                DefaultDiscoveryService defaultDiscoveryService = (DefaultDiscoveryService) discoveryService;
                // If no discovery strategy is found fail-fast
                if (!defaultDiscoveryService.getDiscoveryStrategies().iterator().hasNext()) {
                    throw new RuntimeException("Could not load any Discovery Strategy, please " + "check service definitions under META_INF.services folder. ");
                } else {
                    for (DiscoveryStrategy discoveryStrategy : defaultDiscoveryService.getDiscoveryStrategies()) {
                        PartitionGroupStrategy groupStrategy = discoveryStrategy.getPartitionGroupStrategy(allMembers);
                        if (groupStrategy == null) {
                            groupStrategy = discoveryStrategy.getPartitionGroupStrategy();
                        }
                        checkNotNull(groupStrategy);
                        for (MemberGroup group : groupStrategy.getMemberGroups()) {
                            memberGroups.add(group);
                        }
                        return memberGroups;
                    }
                }
            }
        } catch (Exception e) {
            if (e instanceof ValidationException) {
                throw new InvalidConfigurationException("Invalid configuration", e);
            } else {
                throw new RuntimeException("Failed to configure discovery strategies", e);
            }
        }
    }
    return memberGroups;
}
Also used : MemberGroup(com.hazelcast.spi.partitiongroup.MemberGroup) ValidationException(com.hazelcast.config.properties.ValidationException) PartitionGroupStrategy(com.hazelcast.spi.partitiongroup.PartitionGroupStrategy) DefaultDiscoveryService(com.hazelcast.spi.discovery.impl.DefaultDiscoveryService) Member(com.hazelcast.cluster.Member) InvalidConfigurationException(com.hazelcast.config.InvalidConfigurationException) ValidationException(com.hazelcast.config.properties.ValidationException) DiscoveryStrategy(com.hazelcast.spi.discovery.DiscoveryStrategy) InvalidConfigurationException(com.hazelcast.config.InvalidConfigurationException)

Example 17 with MemberGroup

use of com.hazelcast.spi.partitiongroup.MemberGroup in project hazelcast by hazelcast.

the class HostAwareMemberGroupFactory method createInternalMemberGroups.

@Override
protected Set<MemberGroup> createInternalMemberGroups(Collection<? extends Member> allMembers) {
    Map<String, MemberGroup> groups = createHashMap(allMembers.size());
    for (Member member : allMembers) {
        Address address = ((MemberImpl) member).getAddress();
        MemberGroup group = groups.get(address.getHost());
        if (group == null) {
            group = new DefaultMemberGroup();
            groups.put(address.getHost(), group);
        }
        group.addMember(member);
    }
    return new HashSet<MemberGroup>(groups.values());
}
Also used : MemberGroup(com.hazelcast.spi.partitiongroup.MemberGroup) Address(com.hazelcast.cluster.Address) MemberImpl(com.hazelcast.cluster.impl.MemberImpl) Member(com.hazelcast.cluster.Member) HashSet(java.util.HashSet)

Example 18 with MemberGroup

use of com.hazelcast.spi.partitiongroup.MemberGroup in project hazelcast by hazelcast.

the class PartitionStateGeneratorTest method testCustomPartitionStateGenerator.

@Test
public void testCustomPartitionStateGenerator() throws Exception {
    final MemberGroupFactory memberGroupFactory = new MemberGroupFactory() {

        public Collection<MemberGroup> createMemberGroups(Collection<? extends Member> members) {
            MemberGroup[] g = new MemberGroup[4];
            for (int i = 0; i < g.length; i++) {
                g[i] = new DefaultMemberGroup();
            }
            for (Member member : members) {
                Address address = member.getAddress();
                if (even(address.getHost().hashCode()) && even(address.getPort())) {
                    g[0].addMember(member);
                } else if (even(address.getHost().hashCode()) && !even(address.getPort())) {
                    g[1].addMember(member);
                } else if (!even(address.getHost().hashCode()) && even(address.getPort())) {
                    g[2].addMember(member);
                } else if (!even(address.getHost().hashCode()) && !even(address.getPort())) {
                    g[3].addMember(member);
                }
            }
            List<MemberGroup> list = new LinkedList<MemberGroup>();
            for (MemberGroup memberGroup : g) {
                if (memberGroup.size() > 0) {
                    list.add(memberGroup);
                }
            }
            return list;
        }

        boolean even(int k) {
            return k % 2 == 0;
        }
    };
    test(memberGroupFactory);
}
Also used : MemberGroup(com.hazelcast.spi.partitiongroup.MemberGroup) DefaultMemberGroup(com.hazelcast.internal.partition.membergroup.DefaultMemberGroup) Address(com.hazelcast.cluster.Address) InetAddress(java.net.InetAddress) Collection(java.util.Collection) DefaultMemberGroup(com.hazelcast.internal.partition.membergroup.DefaultMemberGroup) Member(com.hazelcast.cluster.Member) ConfigMemberGroupFactory(com.hazelcast.internal.partition.membergroup.ConfigMemberGroupFactory) HostAwareMemberGroupFactory(com.hazelcast.internal.partition.membergroup.HostAwareMemberGroupFactory) SingleMemberGroupFactory(com.hazelcast.internal.partition.membergroup.SingleMemberGroupFactory) MemberGroupFactory(com.hazelcast.internal.partition.membergroup.MemberGroupFactory) LinkedList(java.util.LinkedList) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 19 with MemberGroup

use of com.hazelcast.spi.partitiongroup.MemberGroup in project hazelcast by hazelcast.

the class MemberGroupFactoryTest method testHostAwareMemberGroupFactoryCreateMemberGroups.

@Test
public void testHostAwareMemberGroupFactoryCreateMemberGroups() {
    MemberGroupFactory groupFactory = new HostAwareMemberGroupFactory();
    Collection<Member> members = createMembers();
    Collection<MemberGroup> memberGroups = groupFactory.createMemberGroups(members);
    assertEquals("Member Groups: " + String.valueOf(memberGroups), 8, memberGroups.size());
    for (MemberGroup memberGroup : memberGroups) {
        assertEquals("Member Group: " + String.valueOf(memberGroup), 2, memberGroup.size());
    }
}
Also used : MemberGroup(com.hazelcast.spi.partitiongroup.MemberGroup) Member(com.hazelcast.cluster.Member) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 20 with MemberGroup

use of com.hazelcast.spi.partitiongroup.MemberGroup in project hazelcast by hazelcast.

the class MemberGroupFactoryTest method testNodeMetadataAwareMemberGroupFactoryCreateMemberGroups.

@Test
public void testNodeMetadataAwareMemberGroupFactoryCreateMemberGroups() {
    MemberGroupFactory groupFactory = new NodeAwareMemberGroupFactory();
    Collection<Member> members = createMembersWithNodeAwareMetadata();
    Collection<MemberGroup> memberGroups = groupFactory.createMemberGroups(members);
    assertEquals("Member Groups: " + String.valueOf(memberGroups), 3, memberGroups.size());
    for (MemberGroup memberGroup : memberGroups) {
        assertEquals("Member Group: " + String.valueOf(memberGroup), 1, memberGroup.size());
    }
}
Also used : MemberGroup(com.hazelcast.spi.partitiongroup.MemberGroup) Member(com.hazelcast.cluster.Member) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Aggregations

MemberGroup (com.hazelcast.spi.partitiongroup.MemberGroup)23 Member (com.hazelcast.cluster.Member)20 QuickTest (com.hazelcast.test.annotation.QuickTest)10 Test (org.junit.Test)10 DefaultMemberGroup (com.hazelcast.internal.partition.membergroup.DefaultMemberGroup)6 MemberGroupFactory (com.hazelcast.internal.partition.membergroup.MemberGroupFactory)5 HashSet (java.util.HashSet)5 PartitionReplica (com.hazelcast.internal.partition.PartitionReplica)4 Address (com.hazelcast.cluster.Address)3 MemberImpl (com.hazelcast.cluster.impl.MemberImpl)3 MemberGroupConfig (com.hazelcast.config.MemberGroupConfig)3 Node (com.hazelcast.instance.impl.Node)3 Collection (java.util.Collection)3 ClusterState (com.hazelcast.cluster.ClusterState)2 AwsConfig (com.hazelcast.config.AwsConfig)2 Config (com.hazelcast.config.Config)2 DiscoveryConfig (com.hazelcast.config.DiscoveryConfig)2 DiscoveryStrategyConfig (com.hazelcast.config.DiscoveryStrategyConfig)2 InterfacesConfig (com.hazelcast.config.InterfacesConfig)2 JoinConfig (com.hazelcast.config.JoinConfig)2