Search in sources :

Example 1 with DiscoveryNode

use of com.hazelcast.spi.discovery.DiscoveryNode in project hazelcast by hazelcast.

the class DiscoveryAddressProvider method loadAddresses.

@Override
public Collection<InetSocketAddress> loadAddresses() {
    Iterable<DiscoveryNode> discoveredNodes = checkNotNull(discoveryService.discoverNodes(), "Discovered nodes cannot be null!");
    Collection<InetSocketAddress> possibleMembers = new ArrayList<InetSocketAddress>();
    for (DiscoveryNode discoveryNode : discoveredNodes) {
        Address discoveredAddress = discoveryNode.getPrivateAddress();
        try {
            possibleMembers.add(discoveredAddress.getInetSocketAddress());
        } catch (UnknownHostException e) {
            logger.warning("Unresolvable host exception", e);
        }
    }
    return possibleMembers;
}
Also used : DiscoveryNode(com.hazelcast.spi.discovery.DiscoveryNode) Address(com.hazelcast.nio.Address) InetSocketAddress(java.net.InetSocketAddress) UnknownHostException(java.net.UnknownHostException) InetSocketAddress(java.net.InetSocketAddress) ArrayList(java.util.ArrayList)

Example 2 with DiscoveryNode

use of com.hazelcast.spi.discovery.DiscoveryNode in project hazelcast by hazelcast.

the class ClientDiscoverySpiTest method testNodeStartup.

@Test
public void testNodeStartup() {
    Config config = new Config();
    config.setProperty("hazelcast.discovery.enabled", "true");
    config.getNetworkConfig().setPort(50001);
    InterfacesConfig interfaces = config.getNetworkConfig().getInterfaces();
    interfaces.clear();
    interfaces.setEnabled(true);
    interfaces.addInterface("127.0.0.1");
    List<DiscoveryNode> discoveryNodes = new CopyOnWriteArrayList<DiscoveryNode>();
    DiscoveryStrategyFactory factory = new CollectingDiscoveryStrategyFactory(discoveryNodes);
    JoinConfig join = config.getNetworkConfig().getJoin();
    join.getTcpIpConfig().setEnabled(false);
    join.getMulticastConfig().setEnabled(false);
    DiscoveryConfig discoveryConfig = join.getDiscoveryConfig();
    discoveryConfig.getDiscoveryStrategyConfigs().clear();
    DiscoveryStrategyConfig strategyConfig = new DiscoveryStrategyConfig(factory, Collections.<String, Comparable>emptyMap());
    discoveryConfig.addDiscoveryStrategyConfig(strategyConfig);
    final HazelcastInstance hazelcastInstance1 = Hazelcast.newHazelcastInstance(config);
    final HazelcastInstance hazelcastInstance2 = Hazelcast.newHazelcastInstance(config);
    final HazelcastInstance hazelcastInstance3 = Hazelcast.newHazelcastInstance(config);
    try {
        ClientConfig clientConfig = new ClientConfig();
        clientConfig.setProperty("hazelcast.discovery.enabled", "true");
        discoveryConfig = clientConfig.getNetworkConfig().getDiscoveryConfig();
        discoveryConfig.getDiscoveryStrategyConfigs().clear();
        strategyConfig = new DiscoveryStrategyConfig(factory, Collections.<String, Comparable>emptyMap());
        discoveryConfig.addDiscoveryStrategyConfig(strategyConfig);
        final HazelcastInstance client = HazelcastClient.newHazelcastClient(clientConfig);
        assertNotNull(hazelcastInstance1);
        assertNotNull(hazelcastInstance2);
        assertNotNull(hazelcastInstance3);
        assertNotNull(client);
        assertTrueEventually(new AssertTask() {

            @Override
            public void run() throws Exception {
                assertEquals(3, hazelcastInstance1.getCluster().getMembers().size());
                assertEquals(3, hazelcastInstance2.getCluster().getMembers().size());
                assertEquals(3, hazelcastInstance3.getCluster().getMembers().size());
                assertEquals(3, client.getCluster().getMembers().size());
            }
        });
    } finally {
        HazelcastClient.shutdownAll();
        Hazelcast.shutdownAll();
    }
}
Also used : InterfacesConfig(com.hazelcast.config.InterfacesConfig) DiscoveryNode(com.hazelcast.spi.discovery.DiscoveryNode) SimpleDiscoveryNode(com.hazelcast.spi.discovery.SimpleDiscoveryNode) DiscoveryStrategyConfig(com.hazelcast.config.DiscoveryStrategyConfig) AwsConfig(com.hazelcast.config.AwsConfig) ClientNetworkConfig(com.hazelcast.client.config.ClientNetworkConfig) DiscoveryStrategyConfig(com.hazelcast.config.DiscoveryStrategyConfig) JoinConfig(com.hazelcast.config.JoinConfig) ClientConfig(com.hazelcast.client.config.ClientConfig) Config(com.hazelcast.config.Config) DiscoveryConfig(com.hazelcast.config.DiscoveryConfig) InterfacesConfig(com.hazelcast.config.InterfacesConfig) DiscoveryStrategyFactory(com.hazelcast.spi.discovery.DiscoveryStrategyFactory) HazelcastInstance(com.hazelcast.core.HazelcastInstance) JoinConfig(com.hazelcast.config.JoinConfig) AssertTask(com.hazelcast.test.AssertTask) DiscoveryConfig(com.hazelcast.config.DiscoveryConfig) ClientConfig(com.hazelcast.client.config.ClientConfig) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 3 with DiscoveryNode

use of com.hazelcast.spi.discovery.DiscoveryNode in project hazelcast by hazelcast.

the class ClientDiscoverySpiTest method testDiscoveryServiceLifecycleMethodsCalledWhenClientAndServerStartAndShutdown.

@Test
public void testDiscoveryServiceLifecycleMethodsCalledWhenClientAndServerStartAndShutdown() {
    //Given
    Config config = new Config();
    config.setProperty("hazelcast.discovery.enabled", "true");
    config.getNetworkConfig().setPort(50001);
    InterfacesConfig interfaces = config.getNetworkConfig().getInterfaces();
    interfaces.clear();
    interfaces.setEnabled(true);
    interfaces.addInterface("127.0.0.1");
    //Both server and client are using the same LifecycleDiscoveryStrategyFactory so latch count is set to 2.
    CountDownLatch startLatch = new CountDownLatch(2);
    CountDownLatch stopLatch = new CountDownLatch(2);
    List<DiscoveryNode> discoveryNodes = new CopyOnWriteArrayList<DiscoveryNode>();
    DiscoveryStrategyFactory factory = new LifecycleDiscoveryStrategyFactory(startLatch, stopLatch, discoveryNodes);
    JoinConfig join = config.getNetworkConfig().getJoin();
    join.getTcpIpConfig().setEnabled(false);
    join.getMulticastConfig().setEnabled(false);
    DiscoveryConfig discoveryConfig = join.getDiscoveryConfig();
    discoveryConfig.getDiscoveryStrategyConfigs().clear();
    DiscoveryStrategyConfig strategyConfig = new DiscoveryStrategyConfig(factory, Collections.<String, Comparable>emptyMap());
    discoveryConfig.addDiscoveryStrategyConfig(strategyConfig);
    final HazelcastInstance hazelcastInstance = Hazelcast.newHazelcastInstance(config);
    ClientConfig clientConfig = new ClientConfig();
    clientConfig.setProperty("hazelcast.discovery.enabled", "true");
    discoveryConfig = clientConfig.getNetworkConfig().getDiscoveryConfig();
    discoveryConfig.getDiscoveryStrategyConfigs().clear();
    strategyConfig = new DiscoveryStrategyConfig(factory, Collections.<String, Comparable>emptyMap());
    discoveryConfig.addDiscoveryStrategyConfig(strategyConfig);
    final HazelcastInstance client = HazelcastClient.newHazelcastClient(clientConfig);
    assertNotNull(hazelcastInstance);
    assertNotNull(client);
    //When
    HazelcastClient.shutdownAll();
    Hazelcast.shutdownAll();
    //Then
    assertOpenEventually(startLatch);
    assertOpenEventually(stopLatch);
}
Also used : InterfacesConfig(com.hazelcast.config.InterfacesConfig) DiscoveryNode(com.hazelcast.spi.discovery.DiscoveryNode) SimpleDiscoveryNode(com.hazelcast.spi.discovery.SimpleDiscoveryNode) DiscoveryStrategyConfig(com.hazelcast.config.DiscoveryStrategyConfig) AwsConfig(com.hazelcast.config.AwsConfig) ClientNetworkConfig(com.hazelcast.client.config.ClientNetworkConfig) DiscoveryStrategyConfig(com.hazelcast.config.DiscoveryStrategyConfig) JoinConfig(com.hazelcast.config.JoinConfig) ClientConfig(com.hazelcast.client.config.ClientConfig) Config(com.hazelcast.config.Config) DiscoveryConfig(com.hazelcast.config.DiscoveryConfig) InterfacesConfig(com.hazelcast.config.InterfacesConfig) CountDownLatch(java.util.concurrent.CountDownLatch) DiscoveryStrategyFactory(com.hazelcast.spi.discovery.DiscoveryStrategyFactory) HazelcastInstance(com.hazelcast.core.HazelcastInstance) JoinConfig(com.hazelcast.config.JoinConfig) DiscoveryConfig(com.hazelcast.config.DiscoveryConfig) ClientConfig(com.hazelcast.client.config.ClientConfig) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 4 with DiscoveryNode

use of com.hazelcast.spi.discovery.DiscoveryNode in project hazelcast by hazelcast.

the class MulticastDiscoveryStrategy method discoverNodes.

@Override
public Iterable<DiscoveryNode> discoverNodes() {
    DiscoveryNode discoveryNode;
    MulticastMemberInfo multicastMemberInfo = multicastDiscoveryReceiver.receive();
    if (multicastMemberInfo == null) {
        return null;
    }
    ArrayList<DiscoveryNode> arrayList = new ArrayList<DiscoveryNode>();
    try {
        discoveryNode = new SimpleDiscoveryNode(new Address(multicastMemberInfo.getHost(), multicastMemberInfo.getPort()));
        arrayList.add(discoveryNode);
    } catch (UnknownHostException e) {
        logger.finest(e.getMessage());
    }
    return arrayList;
}
Also used : MulticastMemberInfo(com.hazelcast.spi.discovery.multicast.impl.MulticastMemberInfo) SimpleDiscoveryNode(com.hazelcast.spi.discovery.SimpleDiscoveryNode) DiscoveryNode(com.hazelcast.spi.discovery.DiscoveryNode) Address(com.hazelcast.nio.Address) InetSocketAddress(java.net.InetSocketAddress) InetAddress(java.net.InetAddress) UnknownHostException(java.net.UnknownHostException) ArrayList(java.util.ArrayList) SimpleDiscoveryNode(com.hazelcast.spi.discovery.SimpleDiscoveryNode)

Example 5 with DiscoveryNode

use of com.hazelcast.spi.discovery.DiscoveryNode in project hazelcast by hazelcast.

the class DiscoveryJoiner method getPossibleAddresses.

@Override
protected Collection<Address> getPossibleAddresses() {
    Iterable<DiscoveryNode> discoveredNodes = checkNotNull(discoveryService.discoverNodes(), "Discovered nodes cannot be null!");
    MemberImpl localMember = node.nodeEngine.getLocalMember();
    Address localAddress = localMember.getAddress();
    Collection<Address> possibleMembers = new ArrayList<Address>();
    for (DiscoveryNode discoveryNode : discoveredNodes) {
        Address discoveredAddress = usePublicAddress ? discoveryNode.getPublicAddress() : discoveryNode.getPrivateAddress();
        if (localAddress.equals(discoveredAddress)) {
            continue;
        }
        possibleMembers.add(discoveredAddress);
    }
    return possibleMembers;
}
Also used : DiscoveryNode(com.hazelcast.spi.discovery.DiscoveryNode) Address(com.hazelcast.nio.Address) MemberImpl(com.hazelcast.instance.MemberImpl) ArrayList(java.util.ArrayList)

Aggregations

DiscoveryNode (com.hazelcast.spi.discovery.DiscoveryNode)7 Address (com.hazelcast.nio.Address)5 SimpleDiscoveryNode (com.hazelcast.spi.discovery.SimpleDiscoveryNode)4 ArrayList (java.util.ArrayList)3 ClientConfig (com.hazelcast.client.config.ClientConfig)2 ClientNetworkConfig (com.hazelcast.client.config.ClientNetworkConfig)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 HazelcastInstance (com.hazelcast.core.HazelcastInstance)2 DiscoveryStrategyFactory (com.hazelcast.spi.discovery.DiscoveryStrategyFactory)2 QuickTest (com.hazelcast.test.annotation.QuickTest)2 InetSocketAddress (java.net.InetSocketAddress)2 UnknownHostException (java.net.UnknownHostException)2 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)2 Test (org.junit.Test)2 MemberImpl (com.hazelcast.instance.MemberImpl)1