/** * <P> * Create a cluster using {@code 127.0.0.1:5701} as the master. The master must be created first, and may be the only * server instance in this JVM. * </P> * * @param name Enables easy identification * @param port The only port this server can use. * @return The master or the 2nd server in the cluster */ public static HazelcastInstance makeServer(final String name, final int port) { Config hazelcastConfig = new Config(name); hazelcastConfig.getNetworkConfig().setReuseAddress(true); hazelcastConfig.getNetworkConfig().getJoin().getMulticastConfig().setEnabled(false); hazelcastConfig.getNetworkConfig().getJoin().getAwsConfig().setEnabled(false); TcpIpConfig tcpIpConfig = hazelcastConfig.getNetworkConfig().getJoin().getTcpIpConfig(); tcpIpConfig.setEnabled(true); tcpIpConfig.setMembers(Arrays.asList(MASTER_SERVER)); tcpIpConfig.setRequiredMember(MASTER_SERVER); hazelcastConfig.getNetworkConfig().setPort(port); hazelcastConfig.getNetworkConfig().setPortAutoIncrement(false); HazelcastInstance hazelcastInstance = Hazelcast.newHazelcastInstance(hazelcastConfig); LOG.debug("Created {}", hazelcastInstance); return hazelcastInstance; }
private void configureTcpIpParameters() { nwConfig.getJoin().getMulticastConfig().setEnabled(false); nwConfig.getJoin().getAwsConfig().setEnabled(false); TcpIpConfig tcpIpConfig = nwConfig.getJoin().getTcpIpConfig(); tcpIpConfig.setEnabled(true); Parameter connTimeoutParameter = parameters.get(CONNECTION_TIMEOUT); if (connTimeoutParameter != null && connTimeoutParameter.getValue() != null) { int connTimeout = Integer.parseInt(((String) (connTimeoutParameter.getValue())).trim()); tcpIpConfig.setConnectionTimeoutSeconds(connTimeout); } log.info(String.format("Mesos membership scheme TCP IP parameters configured [Connection-Timeout] %ds", tcpIpConfig.getConnectionTimeoutSeconds())); }
public HazelcastSessionDao() { log.info("Initializing Hazelcast Shiro session persistence.."); // configure Hazelcast instance final Config cfg = new Config(); cfg.setInstanceName(hcInstanceName); // group configuration cfg.setGroupConfig(new GroupConfig(HC_GROUP_NAME, HC_GROUP_PASSWORD)); // network configuration initialization final NetworkConfig netCfg = new NetworkConfig(); netCfg.setPortAutoIncrement(true); netCfg.setPort(HC_PORT); // multicast final MulticastConfig mcCfg = new MulticastConfig(); mcCfg.setEnabled(false); mcCfg.setMulticastGroup(HC_MULTICAST_GROUP); mcCfg.setMulticastPort(HC_MULTICAST_PORT); // tcp final TcpIpConfig tcpCfg = new TcpIpConfig(); tcpCfg.addMember("127.0.0.1"); tcpCfg.setEnabled(false); // network join configuration final JoinConfig joinCfg = new JoinConfig(); joinCfg.setMulticastConfig(mcCfg); joinCfg.setTcpIpConfig(tcpCfg); netCfg.setJoin(joinCfg); // ssl netCfg.setSSLConfig(new SSLConfig().setEnabled(false)); // get map map = Hazelcast.newHazelcastInstance(cfg).getMap(HC_MAP); log.info("Hazelcast Shiro session persistence initialized."); }
private void setTcpIpConfig(TcpIpConfig tcpIp) { tcpIp.setEnabled(true); if (members == null || members.isEmpty()) { for (String member : members) { if (StringUtils.isBlank(member)) { logger.warn("HAZELCAST MEMBER is empty: {}", member); continue; } tcpIp.getMembers().add(member); } } }
@Test public void testParsing() { String xmlFileName = "test-jclouds-config.xml"; InputStream xmlResource = JCloudsDiscoveryFactoryTest.class.getClassLoader().getResourceAsStream(xmlFileName); Config config = new XmlConfigBuilder(xmlResource).build(); JoinConfig joinConfig = config.getNetworkConfig().getJoin(); AwsConfig awsConfig = joinConfig.getAwsConfig(); assertFalse(awsConfig.isEnabled()); TcpIpConfig tcpIpConfig = joinConfig.getTcpIpConfig(); assertFalse(tcpIpConfig.isEnabled()); MulticastConfig multicastConfig = joinConfig.getMulticastConfig(); assertFalse(multicastConfig.isEnabled()); DiscoveryConfig discoveryConfig = joinConfig.getDiscoveryConfig(); assertTrue(discoveryConfig.isEnabled()); assertEquals(1, discoveryConfig.getDiscoveryStrategyConfigs().size()); DiscoveryStrategyConfig providerConfig = discoveryConfig.getDiscoveryStrategyConfigs().iterator().next(); assertEquals(12, providerConfig.getProperties().size()); assertEquals("aws-ec2", providerConfig.getProperties().get("provider")); assertEquals("test", providerConfig.getProperties().get("identity")); assertEquals("test", providerConfig.getProperties().get("credential")); assertEquals("zone1,zone2", providerConfig.getProperties().get("zones")); assertEquals("region1,region2", providerConfig.getProperties().get("regions")); assertEquals("zone1,zone2", providerConfig.getProperties().get("zones")); assertEquals("tag1,tag2", providerConfig.getProperties().get("tag-keys")); assertEquals("tagvalue1,tagvalue2", providerConfig.getProperties().get("tag-values")); assertEquals("group", providerConfig.getProperties().get("group")); assertEquals("5702", providerConfig.getProperties().get("hz-port")); assertEquals("myfile.json", providerConfig.getProperties().get("credentialPath")); assertEquals("myRole", providerConfig.getProperties().get("role-name")); assertEquals("http://foo/bar", providerConfig.getProperties().get("endpoint")); }
private TcpIpConfig tcpIpConfig(final com.typesafe.config.Config config) { return ConfigUtils.getConfig(config, "join-config", "tcpip") .map(tcpipConfig -> { final TcpIpConfig tcpip = new TcpIpConfig(); tcpip.setEnabled(true); ConfigUtils.getString(tcpipConfig, "required-member").ifPresent(tcpip::setRequiredMember); ConfigUtils.getStringList(tcpipConfig, "members").ifPresent(members -> { members.stream().forEach(tcpip::addMember); }); ConfigUtils.getInt(tcpipConfig, "connection-timeout-seconds").ifPresent(tcpip::setConnectionTimeoutSeconds); return tcpip; }) .orElseGet(() -> new TcpIpConfig().setEnabled(false)); }
public HazelcastSessionDao() { log.info("Initializating Hazelcast Shiro session persistence.."); // configure Hazelcast instance hcInstanceName = UUID.randomUUID().toString(); Config cfg = new Config(); cfg.setInstanceName(hcInstanceName); // group configuration cfg.setGroupConfig(new GroupConfig(HC_GROUP_NAME, HC_GROUP_PASSWORD)); // network configuration initialization NetworkConfig netCfg = new NetworkConfig(); netCfg.setPortAutoIncrement(true); netCfg.setPort(HC_PORT); // multicast MulticastConfig mcCfg = new MulticastConfig(); mcCfg.setEnabled(true); mcCfg.setMulticastGroup(HC_MULTICAST_GROUP); mcCfg.setMulticastPort(HC_MULTICAST_PORT); // tcp TcpIpConfig tcpCfg = new TcpIpConfig(); tcpCfg.setEnabled(false); // network join configuration JoinConfig joinCfg = new JoinConfig(); joinCfg.setMulticastConfig(mcCfg); joinCfg.setTcpIpConfig(tcpCfg); netCfg.setJoin(joinCfg); // ssl netCfg.setSSLConfig(new SSLConfig().setEnabled(false)); // get map map = Hazelcast.newHazelcastInstance(cfg).getMap(HC_MAP); log.info("Hazelcast Shiro session persistence initialized."); }
public void doJoin(AtomicBoolean joined) { int tryCount = 0; long joinStartTime = System.currentTimeMillis(); long maxJoinMillis = node.getGroupProperties().MAX_JOIN_SECONDS.getInteger() * 1000; while (node.isActive() && !joined.get() && (System.currentTimeMillis() - joinStartTime < maxJoinMillis)) { String msg = "Joining master " + node.getMasterAddress(); logger.log(Level.FINEST, msg); systemLogService.logJoin(msg); Address masterAddressNow = findMasterWithMulticast(); if (masterAddressNow != null && masterAddressNow.equals(node.getMasterAddress())) { tryCount--; } node.setMasterAddress(masterAddressNow); systemLogService.logJoin("Setting master " + masterAddressNow); if (node.getMasterAddress() == null || node.address.equals(node.getMasterAddress())) { TcpIpConfig tcpIpConfig = config.getNetworkConfig().getJoin().getTcpIpConfig(); if (tcpIpConfig != null && tcpIpConfig.isEnabled()) { doTCP(joined); } else { systemLogService.logJoin("Setting as master"); node.setAsMaster(); } return; } if (tryCount++ > 22) { failedJoiningToMaster(true, tryCount); } if (!node.getMasterAddress().equals(node.address)) { connectAndSendJoinRequest(node.getMasterAddress()); } else { node.setMasterAddress(null); tryCount = 0; } try { //noinspection BusyWait Thread.sleep(500L); } catch (InterruptedException ignored) { } } }
@Test @Ignore public void testTCPIPJoinWithManyNodesWith4secIntervals() throws UnknownHostException, InterruptedException { final int count = 10; System.setProperty("hazelcast.mancenter.enabled", "false"); final CountDownLatch latch = new CountDownLatch(count); for (int i = 0; i < count; i++) { final int seed = i; new Thread(new Runnable() { public void run() { try { Thread.sleep(4700 * seed + 1); final Config config = new Config(); config.getNetworkConfig().getJoin().getMulticastConfig().setEnabled(false); TcpIpConfig tcpIpConfig = config.getNetworkConfig().getJoin().getTcpIpConfig(); tcpIpConfig.setEnabled(true); int port = 12301; config.setPortAutoIncrement(false); config.setPort(port + seed); for (int i = 0; i < count; i++) { tcpIpConfig.addAddress(new Address("127.0.0.1", port + i)); } HazelcastInstance h = Hazelcast.newHazelcastInstance(config); latch.countDown(); //h.getMap("name").size(); } catch (Exception e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } } }).start(); } assertTrue(latch.await(200, TimeUnit.SECONDS)); }
@Bean public Config hazelcastConfig() { Config config = new Config(); config.addMapConfig(new MapConfig("spring-boot-admin-event-store").setInMemoryFormat(InMemoryFormat.OBJECT) .setBackupCount(1) .setEvictionPolicy(EvictionPolicy.NONE)); config.getNetworkConfig().getJoin().getMulticastConfig().setEnabled(false); TcpIpConfig tcpIpConfig = config.getNetworkConfig().getJoin().getTcpIpConfig(); tcpIpConfig.setEnabled(true); tcpIpConfig.setMembers(singletonList("127.0.0.1")); return config; }
@Test public void testBuilder() { final Config config = new JsonConfigBuilder(getResource()).build(); assertNotNull(config); // Group Config assertNotNull(config.getGroupConfig()); assertEquals("group-name", config.getGroupConfig().getName()); assertEquals("group-password", config.getGroupConfig().getPassword()); // Network Config final NetworkConfig netCfg = config.getNetworkConfig(); assertEquals(true, netCfg.isReuseAddress()); assertEquals(5900, netCfg.getPort()); assertEquals(false, netCfg.isPortAutoIncrement()); assertEquals(100, netCfg.getPortCount()); assertTrue(netCfg.getOutboundPortDefinitions().contains("10100")); assertTrue(netCfg.getOutboundPortDefinitions().contains("9000-10000")); assertFalse(netCfg.getOutboundPortDefinitions().contains("1")); assertEquals("127.0.0.1", netCfg.getPublicAddress()); // Multicast Config final MulticastConfig mcastCfg = netCfg.getJoin().getMulticastConfig(); assertEquals(false, mcastCfg.isEnabled()); assertEquals(false, mcastCfg.isLoopbackModeEnabled()); assertTrue(mcastCfg.getTrustedInterfaces().contains("eth0")); assertTrue(mcastCfg.getTrustedInterfaces().contains("eth1")); assertFalse(mcastCfg.getTrustedInterfaces().contains("lo0")); // TcpIp Config final TcpIpConfig tcpCfg = netCfg.getJoin().getTcpIpConfig(); assertEquals(false, tcpCfg.isEnabled()); assertEquals(10, tcpCfg.getConnectionTimeoutSeconds()); }
@Test public void testBuilder() { final Config config = new YamlConfigBuilder(getResource()).build(); assertNotNull(config); // Group Config assertNotNull(config.getGroupConfig()); assertEquals("group-name", config.getGroupConfig().getName()); assertEquals("group-password", config.getGroupConfig().getPassword()); // Network Config final NetworkConfig netCfg = config.getNetworkConfig(); assertEquals(true, netCfg.isReuseAddress()); assertEquals(5900, netCfg.getPort()); assertEquals(false, netCfg.isPortAutoIncrement()); assertEquals(100, netCfg.getPortCount()); assertFalse(netCfg.getOutboundPortDefinitions().isEmpty()); assertEquals(2, netCfg.getOutboundPortDefinitions().size()); assertTrue(netCfg.getOutboundPortDefinitions().contains("10100")); assertTrue(netCfg.getOutboundPortDefinitions().contains("9000-10000")); assertEquals("127.0.0.1", netCfg.getPublicAddress()); // Multicast Config final MulticastConfig mcastCfg = netCfg.getJoin().getMulticastConfig(); assertEquals(false, mcastCfg.isEnabled()); assertEquals(false, mcastCfg.isLoopbackModeEnabled()); assertFalse(mcastCfg.getTrustedInterfaces().isEmpty()); assertEquals(2, mcastCfg.getTrustedInterfaces().size()); assertTrue(mcastCfg.getTrustedInterfaces().contains("eth0")); assertTrue(mcastCfg.getTrustedInterfaces().contains("eth1")); // TcpIp Config final TcpIpConfig tcpCfg = netCfg.getJoin().getTcpIpConfig(); assertEquals(false, tcpCfg.isEnabled()); assertEquals(10, tcpCfg.getConnectionTimeoutSeconds()); assertFalse(tcpCfg.getMembers().isEmpty()); assertEquals(3, tcpCfg.getMembers().size()); assertTrue(tcpCfg.getMembers().contains("192.168.0.1")); assertTrue(tcpCfg.getMembers().contains("192.168.0.2")); assertTrue(tcpCfg.getMembers().contains("192.168.0.3")); assertEquals("127.0.0.1", tcpCfg.getRequiredMember()); // Interfaces Config final InterfacesConfig ifacesCfg = netCfg.getInterfaces(); assertEquals(false, ifacesCfg.isEnabled()); assertEquals(3, ifacesCfg.getInterfaces().size()); assertTrue(ifacesCfg.getInterfaces().contains("10.3.16.*")); assertTrue(ifacesCfg.getInterfaces().contains("10.3.10.4-18")); assertTrue(ifacesCfg.getInterfaces().contains("192.168.1.3")); }
public static void main(String[] args) throws InterruptedException { final Config cfg = new Config(); cfg.setInstanceName(UUID.randomUUID().toString()); final Properties props = new Properties(); props.put("hazelcast.rest.enabled", false); props.put("hazelcast.logging.type", "slf4j"); props.put("hazelcast.connect.all.wait.seconds", 45); props.put("hazelcast.operation.call.timeout.millis", 30000); // group configuration cfg.setGroupConfig(new GroupConfig(args[0], args[1])); // network configuration initialization final NetworkConfig netCfg = new NetworkConfig(); netCfg.setPortAutoIncrement(true); netCfg.setPort(5701); // multicast final MulticastConfig mcCfg = new MulticastConfig(); mcCfg.setEnabled(false); // tcp final TcpIpConfig tcpCfg = new TcpIpConfig(); tcpCfg.addMember("127.0.0.1"); tcpCfg.setEnabled(true); // network join configuration final JoinConfig joinCfg = new JoinConfig(); joinCfg.setMulticastConfig(mcCfg); joinCfg.setTcpIpConfig(tcpCfg); netCfg.setJoin(joinCfg); // ssl netCfg.setSSLConfig(new SSLConfig().setEnabled(false)); // creating cassandra client final CassandraClient dao = new CassandraClient(); dao.initialize(args[2]); final HazelcastMapStore mapStore = new HazelcastMapStore(User.class); mapStore.setDao(dao); // Adding mapstore final MapConfig mapCfg = cfg.getMapConfig("cassandra-map-store"); final MapStoreConfig mapStoreCfg = new MapStoreConfig(); mapStoreCfg.setImplementation(mapStore); mapStoreCfg.setWriteDelaySeconds(1); // to load all map at same time mapStoreCfg.setInitialLoadMode(MapStoreConfig.InitialLoadMode.EAGER); mapCfg.setMapStoreConfig(mapStoreCfg); cfg.addMapConfig(mapCfg); HazelcastInstance instance = Hazelcast.newHazelcastInstance(cfg); // TERM signal processing Runtime.getRuntime().addShutdownHook(new Thread(() -> { Hazelcast.shutdownAll(); })); }
public Config getConfig() { final Config cfg = new Config(); cfg.setInstanceName(instanceName); final Properties props = new Properties(); props.put("hazelcast.rest.enabled", false); props.put("hazelcast.logging.type", "slf4j"); props.put("hazelcast.connect.all.wait.seconds", 45); props.put("hazelcast.operation.call.timeout.millis", 30000); // group configuration cfg.setGroupConfig(new GroupConfig(Constants.HC_GROUP_NAME, Constants.HC_GROUP_PASSWORD)); // network configuration initialization final NetworkConfig netCfg = new NetworkConfig(); netCfg.setPortAutoIncrement(true); netCfg.setPort(Constants.HC_PORT); // multicast final MulticastConfig mcCfg = new MulticastConfig(); mcCfg.setEnabled(false); // tcp final TcpIpConfig tcpCfg = new TcpIpConfig(); tcpCfg.addMember("127.0.0.1"); tcpCfg.setEnabled(true); // network join configuration final JoinConfig joinCfg = new JoinConfig(); joinCfg.setMulticastConfig(mcCfg); joinCfg.setTcpIpConfig(tcpCfg); netCfg.setJoin(joinCfg); // ssl netCfg.setSSLConfig(new SSLConfig().setEnabled(false)); // Adding mapstore final MapConfig mapCfg = cfg.getMapConfig(storeType); final MapStoreConfig mapStoreCfg = new MapStoreConfig(); mapStoreCfg.setImplementation(store); mapStoreCfg.setWriteDelaySeconds(1); // to load all map at same time mapStoreCfg.setInitialLoadMode(MapStoreConfig.InitialLoadMode.EAGER); mapCfg.setMapStoreConfig(mapStoreCfg); cfg.addMapConfig(mapCfg); return cfg; }
public void doJoin(AtomicBoolean joined) { int tryCount = 0; long joinStartTime = Clock.currentTimeMillis(); long maxJoinMillis = node.getGroupProperties().MAX_JOIN_SECONDS.getInteger() * 1000; while (node.isActive() && !joined.get() && (Clock.currentTimeMillis() - joinStartTime < maxJoinMillis)) { String msg = "Joining to master node: " + node.getMasterAddress(); logger.log(Level.FINEST, msg); systemLogService.logJoin(msg); final Address masterAddressNow; if (targetAddress == null) { masterAddressNow = findMasterWithMulticast(); } else { // if target address is set explicitly, try to join target address first. masterAddressNow = targetAddress; targetAddress = null; } node.setMasterAddress(masterAddressNow); if (masterAddressNow != null) { systemLogService.logJoin("Setting master address to " + masterAddressNow); } if (node.getMasterAddress() == null || node.address.equals(node.getMasterAddress())) { TcpIpConfig tcpIpConfig = config.getNetworkConfig().getJoin().getTcpIpConfig(); if (tcpIpConfig != null && tcpIpConfig.isEnabled()) { doTCP(joined); } else { node.setAsMaster(); } return; } if (++tryCount > 49) { failedJoiningToMaster(true, tryCount); } if (!node.getMasterAddress().equals(node.address)) { connectAndSendJoinRequest(node.getMasterAddress()); } else { node.setMasterAddress(null); tryCount = 0; } try { //noinspection BusyWait Thread.sleep(500L); } catch (InterruptedException ignored) { } } }