@Test public void hazelcastCacheWithMainHazelcastAutoConfigurationAndSeparateCacheConfig() throws IOException { String mainConfig = "org/springframework/boot/autoconfigure/hazelcast/hazelcast-specific.xml"; String cacheConfig = "org/springframework/boot/autoconfigure/cache/hazelcast-specific.xml"; AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(); EnvironmentTestUtils.addEnvironment( applicationContext, "spring.cache.type=hazelcast", "spring.cache.hazelcast.config=" + cacheConfig, "spring.hazelcast.config=" + mainConfig); applicationContext.register(DefaultCacheConfiguration.class); applicationContext.register(HazelcastAndCacheConfiguration.class); applicationContext.refresh(); this.context = applicationContext; HazelcastInstance hazelcastInstance = this.context.getBean(HazelcastInstance.class); HazelcastCacheManager cacheManager = validateCacheManager(HazelcastCacheManager.class); HazelcastInstance cacheHazelcastInstance = getHazelcastInstance(cacheManager); assertThat(cacheHazelcastInstance, not(hazelcastInstance)); // Our custom assertThat( hazelcastInstance.getConfig().getConfigurationFile(), equalTo(new ClassPathResource(mainConfig).getFile())); assertThat( cacheHazelcastInstance.getConfig().getConfigurationFile(), equalTo(new ClassPathResource(cacheConfig).getFile())); }
@Test @Category(ProblematicTest.class) public void testTCPIPJoinWithManyNodesMultipleGroups() throws UnknownHostException, InterruptedException { final int count = 20; final int groupCount = 3; final CountDownLatch latch = new CountDownLatch(count); final ConcurrentHashMap<Integer, HazelcastInstance> mapOfInstances = new ConcurrentHashMap<Integer, HazelcastInstance>(); final Random random = new Random(); final Map<String, AtomicInteger> groups = new ConcurrentHashMap<String, AtomicInteger>(); for (int i = 0; i < groupCount; i++) { groups.put("group" + i, new AtomicInteger(0)); } final ExecutorService ex = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors() * 2); for (int i = 0; i < count; i++) { final int seed = i; ex.execute( new Runnable() { public void run() { try { Thread.sleep(random.nextInt(10) * 1000); final Config config = new Config(); config.setProperty("hazelcast.wait.seconds.before.join", "5"); String name = "group" + random.nextInt(groupCount); groups.get(name).incrementAndGet(); config.getGroupConfig().setName(name); final NetworkConfig networkConfig = config.getNetworkConfig(); networkConfig.getJoin().getMulticastConfig().setEnabled(false); TcpIpConfig tcpIpConfig = networkConfig.getJoin().getTcpIpConfig(); tcpIpConfig.setEnabled(true); int port = 12301; networkConfig.setPortAutoIncrement(false); networkConfig.setPort(port + seed); for (int i = 0; i < count; i++) { tcpIpConfig.addMember("127.0.0.1:" + (port + i)); } HazelcastInstance h = Hazelcast.newHazelcastInstance(config); mapOfInstances.put(seed, h); latch.countDown(); } catch (Exception e) { e.printStackTrace(); } } }); } try { latch.await(200, TimeUnit.SECONDS); } finally { ex.shutdown(); } for (HazelcastInstance h : mapOfInstances.values()) { int clusterSize = h.getCluster().getMembers().size(); int shouldBeClusterSize = groups.get(h.getConfig().getGroupConfig().getName()).get(); assertEquals( h.getConfig().getGroupConfig().getName() + ": ", shouldBeClusterSize, clusterSize); } }
private void initHzCacheMapConfig(HazelcastInstance hz) { Map<String, MapConfig> mapConfigs = hz.getConfig().getMapConfigs(); if (mapConfigs == null) { mapConfigs = new HashMap<String, MapConfig>(); hz.getConfig().setMapConfigs(mapConfigs); } MapConfig config = new MapConfig(); config.setTimeToLiveSeconds(this.getCacheTimeoutSeconds()); config.setMaxSizeConfig(new MaxSizeConfig(this.getCacheSizeBytes(), MaxSizePolicy.PER_NODE)); mapConfigs.put(CACHE_MAP_NAME, config); }
@Test public void setMapStoreConfigImplementationTest() { String mapName = "mapStoreImpObjTest"; String xml = "<hazelcast xmlns=\"http://www.hazelcast.com/schema/config\">\n" + "<map name=\"" + mapName + "\">\n" + "<map-store enabled=\"true\">\n" + "<class-name>com.hazelcast.config.helpers.DummyMapStore</class-name>\n" + "<write-delay-seconds>5</write-delay-seconds>\n" + "</map-store>\n" + "</map>\n" + "</hazelcast>\n"; Config config = buildConfig(xml); HazelcastInstance hz = createHazelcastInstance(config); hz.getMap(mapName); MapConfig mapConfig = hz.getConfig().getMapConfig(mapName); MapStoreConfig mapStoreConfig = mapConfig.getMapStoreConfig(); Object o = mapStoreConfig.getImplementation(); assertNotNull(o); assertTrue(o instanceof DummyMapStore); }
private static ClientConfig getClientConfig(HazelcastInstance instance) { ClientConfig clientConfig = new ClientConfig(); Address address = getNode(instance).address; clientConfig.getNetworkConfig().addAddress(address.getHost() + ":" + address.getPort()); clientConfig.getGroupConfig().setName(instance.getConfig().getGroupConfig().getName()); return clientConfig; }
@Override public String name() { try { if (cluster.getInstance() != null) { HazelcastInstance instance = cluster.getInstance(); if (instance.getConfig() != null) { Config config = instance.getConfig(); if (config.getGroupConfig() != null) { return "hazelcast/" + config.getGroupConfig().getName(); } } } } catch (Exception ignore) { } return "hazelcast"; }
/** * Make a {@link MapConfig} for the given sessionMapName if one does not exist. Set Hazelcast's * maxIdleSeconds to maxInactiveIntervalInSeconds if set (not ""). Otherwise get the externally * configured maxIdleSeconds for the distributed sessions map. * * @param hazelcastInstance the {@link HazelcastInstance} to configure */ private void configureSessionMap(HazelcastInstance hazelcastInstance) { MapConfig sessionMapConfig = hazelcastInstance.getConfig().getMapConfig(sessionMapName); if (this.maxInactiveIntervalInSeconds != null) { sessionMapConfig.setMaxIdleSeconds(this.maxInactiveIntervalInSeconds); } else { this.maxInactiveIntervalInSeconds = sessionMapConfig.getMaxIdleSeconds(); } }
private void start() throws IOException { if (logPath != null) { PropertyConfigurator.configure(logPath.getAbsolutePath()); } if (!hazelcastFile.isFile()) { throw new RuntimeException("Could not find hazelcast configuration file : " + hazelcastFile); } /* * Start a lightweight http server, make sure it can only be accessed * from the machine it's running on (don't want to allow remote * shutdown). */ server = HttpServer.create(new InetSocketAddress(host, port), port); server.createContext(baseContext + HazelcastServiceCommand.stop, new ShutdownHandler()); server.createContext(baseContext + HazelcastServiceCommand.status, new StatusHandler()); server.setExecutor(null); server.start(); // Config config = new Config(); // config.setConfigurationFile(hazelcastFile); // config.setConfigurationUrl(hazelcastFile.toURI().toURL()); // hz = Hazelcast.newHazelcastInstance(config); /* * Creating a Config object (as commented out above) and setting the * file using setConfigurationFile doesn't seem to work at all. The * system property approach does. */ String path = hazelcastFile.getAbsolutePath(); logger.info("Setting configuration file " + path); System.setProperty("hazelcast.config", path); hz = Hazelcast.getDefaultInstance(); logger.info(hz.getConfig().getConfigurationFile()); logger.info(hz.getConfig().getGroupConfig().getName()); monitor = new HazelcastMonitor(); hz.addInstanceListener(monitor); if (testValues) { hz.getMap("tst").put("1", "1"); hz.getMap("tst").put("2", "2"); hz.getMap("tst").put("3", "3"); logger.info(hz.getMap("tst").getId()); logger.info(hz.getMap("tst").size()); } System.out.println("-- HAZELCAST SERVICE WRAPPER READY --"); System.out.println( String.format("To check status, request: '%s'.", url(HazelcastServiceCommand.status))); System.out.println( String.format("To shut it down, request: '%s'.", url(HazelcastServiceCommand.stop))); }
private void testJoinWithManyNodesMultipleGroups(final boolean multicast) throws InterruptedException { final int nodeCount = 10; final int groupCount = 3; final int basePort = 12301; final CountDownLatch latch = new CountDownLatch(nodeCount); final AtomicReferenceArray<HazelcastInstance> instances = new AtomicReferenceArray<HazelcastInstance>(nodeCount); final Map<String, AtomicInteger> groups = new HashMap<String, AtomicInteger>(groupCount); for (int i = 0; i < groupCount; i++) { groups.put("group-" + i, new AtomicInteger(0)); } ExecutorService ex = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors() * 2); for (int i = 0; i < nodeCount; i++) { final int portSeed = i; ex.execute( new Runnable() { public void run() { sleepRandom(1, 1000); Config config = new Config(); String name = "group-" + (int) (Math.random() * groupCount); config.getGroupConfig().setName(name); groups.get(name).incrementAndGet(); initNetworkConfig( config.getNetworkConfig(), basePort, portSeed, multicast, nodeCount); HazelcastInstance h = Hazelcast.newHazelcastInstance(config); instances.set(portSeed, h); latch.countDown(); } }); } try { latch.await(200, TimeUnit.SECONDS); } finally { ex.shutdown(); } for (int i = 0; i < nodeCount; i++) { HazelcastInstance hz = instances.get(i); assertNotNull(hz); int clusterSize = hz.getCluster().getMembers().size(); String groupName = hz.getConfig().getGroupConfig().getName(); int shouldBeClusterSize = groups.get(groupName).get(); assertEquals(groupName + ": ", shouldBeClusterSize, clusterSize); } }
@Test public void testProperties() { final Properties properties = config.getProperties(); assertNotNull(properties); assertEquals("5", properties.get(GroupProperties.PROP_MERGE_FIRST_RUN_DELAY_SECONDS)); assertEquals("5", properties.get(GroupProperties.PROP_MERGE_NEXT_RUN_DELAY_SECONDS)); final Config config2 = instance.getConfig(); final Properties properties2 = config2.getProperties(); assertNotNull(properties2); assertEquals("5", properties2.get(GroupProperties.PROP_MERGE_FIRST_RUN_DELAY_SECONDS)); assertEquals("5", properties2.get(GroupProperties.PROP_MERGE_NEXT_RUN_DELAY_SECONDS)); }
@Test public void testProperties() { Properties properties = config.getProperties(); assertNotNull(properties); assertEquals("5", properties.get(MERGE_FIRST_RUN_DELAY_SECONDS.getName())); assertEquals("5", properties.get(MERGE_NEXT_RUN_DELAY_SECONDS.getName())); Config config2 = instance.getConfig(); Properties properties2 = config2.getProperties(); assertNotNull(properties2); assertEquals("5", properties2.get(MERGE_FIRST_RUN_DELAY_SECONDS.getName())); assertEquals("5", properties2.get(MERGE_NEXT_RUN_DELAY_SECONDS.getName())); }
@Test public void hazelcastCacheWithConfig() throws IOException { load( DefaultCacheConfiguration.class, "spring.cache.type=hazelcast", "spring.cache.hazelcast.config=org/springframework/boot/autoconfigure/cache/hazelcast-specific.xml"); HazelcastInstance hazelcastInstance = this.context.getBean(HazelcastInstance.class); HazelcastCacheManager cacheManager = validateCacheManager(HazelcastCacheManager.class); HazelcastInstance actual = getHazelcastInstance(cacheManager); assertThat(actual, sameInstance(hazelcastInstance)); assertThat( actual.getConfig().getConfigurationUrl(), equalTo( new ClassPathResource( "org/springframework/boot/autoconfigure/cache/hazelcast-specific.xml") .getURL())); cacheManager.getCache("foobar"); assertThat(cacheManager.getCacheNames(), containsInAnyOrder("foobar")); assertThat(cacheManager.getCacheNames(), hasSize(1)); }
@Setup public void setup(TestContext testContext) throws Exception { this.testContext = testContext; targetInstance = testContext.getTargetInstance(); testId = testContext.getTestId(); integrityMap = targetInstance.getMap(basename + "Integrity"); stressMap = targetInstance.getMap(basename + "Stress"); integrityThreads = new MapIntegrityThread[mapIntegrityThreadCount]; value = new byte[valueSize]; Random random = new Random(); random.nextBytes(value); if (mapLoad && isMemberNode(targetInstance)) { PartitionService partitionService = targetInstance.getPartitionService(); final Set<Partition> partitionSet = partitionService.getPartitions(); for (Partition partition : partitionSet) { while (partition.getOwner() == null) { Thread.sleep(1000); } } LOGGER.info(testId + ": " + partitionSet.size() + " partitions"); Member localMember = targetInstance.getCluster().getLocalMember(); for (int i = 0; i < totalIntegrityKeys; i++) { Partition partition = partitionService.getPartition(i); if (localMember.equals(partition.getOwner())) { integrityMap.put(i, value); } } LOGGER.info( testId + ": integrityMap=" + integrityMap.getName() + " size=" + integrityMap.size()); Config config = targetInstance.getConfig(); MapConfig mapConfig = config.getMapConfig(integrityMap.getName()); LOGGER.info(testId + ": " + mapConfig); } }
@Before public void before() { config = instance.getConfig(); }