Search in sources :

Example 1 with DiscoveryConfig

use of com.hazelcast.config.DiscoveryConfig 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 2 with DiscoveryConfig

use of com.hazelcast.config.DiscoveryConfig in project hazelcast by hazelcast.

the class ClientDiscoverySpiTest method testNodeFilter_from_xml.

@Test
public void testNodeFilter_from_xml() throws Exception {
    String xmlFileName = "hazelcast-client-discovery-spi-test.xml";
    InputStream xmlResource = ClientDiscoverySpiTest.class.getClassLoader().getResourceAsStream(xmlFileName);
    ClientConfig clientConfig = new XmlClientConfigBuilder(xmlResource).build();
    ClientNetworkConfig networkConfig = clientConfig.getNetworkConfig();
    DiscoveryConfig discoveryConfig = networkConfig.getDiscoveryConfig();
    DiscoveryServiceProvider provider = new DefaultDiscoveryServiceProvider();
    DiscoveryService discoveryService = provider.newDiscoveryService(buildDiscoveryServiceSettings(discoveryConfig));
    discoveryService.start();
    discoveryService.discoverNodes();
    discoveryService.destroy();
    Field nodeFilterField = DefaultDiscoveryService.class.getDeclaredField("nodeFilter");
    nodeFilterField.setAccessible(true);
    TestNodeFilter nodeFilter = (TestNodeFilter) nodeFilterField.get(discoveryService);
    assertEquals(4, nodeFilter.getNodes().size());
}
Also used : DiscoveryServiceProvider(com.hazelcast.spi.discovery.integration.DiscoveryServiceProvider) DefaultDiscoveryServiceProvider(com.hazelcast.spi.discovery.impl.DefaultDiscoveryServiceProvider) Field(java.lang.reflect.Field) DefaultDiscoveryServiceProvider(com.hazelcast.spi.discovery.impl.DefaultDiscoveryServiceProvider) XmlClientConfigBuilder(com.hazelcast.client.config.XmlClientConfigBuilder) InputStream(java.io.InputStream) ClientConfig(com.hazelcast.client.config.ClientConfig) DiscoveryConfig(com.hazelcast.config.DiscoveryConfig) DiscoveryService(com.hazelcast.spi.discovery.integration.DiscoveryService) DefaultDiscoveryService(com.hazelcast.spi.discovery.impl.DefaultDiscoveryService) ClientNetworkConfig(com.hazelcast.client.config.ClientNetworkConfig) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 3 with DiscoveryConfig

use of com.hazelcast.config.DiscoveryConfig in project hazelcast by hazelcast.

the class ClientDiscoverySpiTest method test_discovery_address_translator_with_public_ip.

@Test
public void test_discovery_address_translator_with_public_ip() throws Exception {
    String xmlFileName = "hazelcast-client-discovery-spi-test.xml";
    InputStream xmlResource = ClientDiscoverySpiTest.class.getClassLoader().getResourceAsStream(xmlFileName);
    ClientConfig clientConfig = new XmlClientConfigBuilder(xmlResource).build();
    ClientNetworkConfig networkConfig = clientConfig.getNetworkConfig();
    DiscoveryConfig discoveryConfig = networkConfig.getDiscoveryConfig();
    DiscoveryServiceProvider provider = new DefaultDiscoveryServiceProvider();
    DiscoveryService discoveryService = provider.newDiscoveryService(buildDiscoveryServiceSettings(discoveryConfig));
    AddressTranslator translator = new DiscoveryAddressTranslator(discoveryService, true);
    Address publicAddress = new Address("127.0.0.1", 50001);
    Address privateAddress = new Address("127.0.0.1", 1);
    // Enforce refresh of the internal mapping
    assertEquals(publicAddress, translator.translate(privateAddress));
}
Also used : Address(com.hazelcast.nio.Address) InputStream(java.io.InputStream) DiscoveryServiceProvider(com.hazelcast.spi.discovery.integration.DiscoveryServiceProvider) DefaultDiscoveryServiceProvider(com.hazelcast.spi.discovery.impl.DefaultDiscoveryServiceProvider) AddressTranslator(com.hazelcast.client.connection.AddressTranslator) DefaultDiscoveryServiceProvider(com.hazelcast.spi.discovery.impl.DefaultDiscoveryServiceProvider) XmlClientConfigBuilder(com.hazelcast.client.config.XmlClientConfigBuilder) ClientConfig(com.hazelcast.client.config.ClientConfig) DiscoveryConfig(com.hazelcast.config.DiscoveryConfig) DiscoveryService(com.hazelcast.spi.discovery.integration.DiscoveryService) DefaultDiscoveryService(com.hazelcast.spi.discovery.impl.DefaultDiscoveryService) ClientNetworkConfig(com.hazelcast.client.config.ClientNetworkConfig) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 4 with DiscoveryConfig

use of com.hazelcast.config.DiscoveryConfig 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 5 with DiscoveryConfig

use of com.hazelcast.config.DiscoveryConfig in project hazelcast by hazelcast.

the class Node method createDiscoveryService.

private DiscoveryService createDiscoveryService(Config config) {
    JoinConfig joinConfig = config.getNetworkConfig().getJoin();
    DiscoveryConfig discoveryConfig = joinConfig.getDiscoveryConfig().getAsReadOnly();
    DiscoveryServiceProvider factory = discoveryConfig.getDiscoveryServiceProvider();
    if (factory == null) {
        factory = new DefaultDiscoveryServiceProvider();
    }
    ILogger logger = getLogger(DiscoveryService.class);
    DiscoveryServiceSettings settings = new DiscoveryServiceSettings().setConfigClassLoader(configClassLoader).setLogger(logger).setDiscoveryMode(DiscoveryMode.Member).setDiscoveryConfig(discoveryConfig).setDiscoveryNode(new SimpleDiscoveryNode(localMember.getAddress(), localMember.getAttributes()));
    return factory.newDiscoveryService(settings);
}
Also used : DiscoveryServiceProvider(com.hazelcast.spi.discovery.integration.DiscoveryServiceProvider) DefaultDiscoveryServiceProvider(com.hazelcast.spi.discovery.impl.DefaultDiscoveryServiceProvider) DefaultDiscoveryServiceProvider(com.hazelcast.spi.discovery.impl.DefaultDiscoveryServiceProvider) JoinConfig(com.hazelcast.config.JoinConfig) SimpleDiscoveryNode(com.hazelcast.spi.discovery.SimpleDiscoveryNode) ILogger(com.hazelcast.logging.ILogger) DiscoveryConfig(com.hazelcast.config.DiscoveryConfig) DiscoveryServiceSettings(com.hazelcast.spi.discovery.integration.DiscoveryServiceSettings)

Aggregations

DiscoveryConfig (com.hazelcast.config.DiscoveryConfig)15 QuickTest (com.hazelcast.test.annotation.QuickTest)10 Test (org.junit.Test)10 DiscoveryStrategyConfig (com.hazelcast.config.DiscoveryStrategyConfig)9 ClientNetworkConfig (com.hazelcast.client.config.ClientNetworkConfig)7 AwsConfig (com.hazelcast.config.AwsConfig)7 InputStream (java.io.InputStream)7 ClientConfig (com.hazelcast.client.config.ClientConfig)6 JoinConfig (com.hazelcast.config.JoinConfig)6 DefaultDiscoveryServiceProvider (com.hazelcast.spi.discovery.impl.DefaultDiscoveryServiceProvider)6 DiscoveryServiceProvider (com.hazelcast.spi.discovery.integration.DiscoveryServiceProvider)6 Config (com.hazelcast.config.Config)5 InterfacesConfig (com.hazelcast.config.InterfacesConfig)5 DiscoveryService (com.hazelcast.spi.discovery.integration.DiscoveryService)5 XmlClientConfigBuilder (com.hazelcast.client.config.XmlClientConfigBuilder)4 TcpIpConfig (com.hazelcast.config.TcpIpConfig)4 DefaultDiscoveryService (com.hazelcast.spi.discovery.impl.DefaultDiscoveryService)4 MulticastConfig (com.hazelcast.config.MulticastConfig)3 XmlConfigBuilder (com.hazelcast.config.XmlConfigBuilder)3 Address (com.hazelcast.nio.Address)3