private void doShutdown(boolean force) { long start = Clock.currentTimeMillis(); logger.finest("** we are being asked to shutdown when active = " + String.valueOf(active)); if (!force && isActive()) { final int maxWaitSeconds = groupProperties.GRACEFUL_SHUTDOWN_MAX_WAIT.getInteger(); if (!partitionService.prepareToSafeShutdown(maxWaitSeconds, TimeUnit.SECONDS)) { logger.warning( "Graceful shutdown could not be completed in " + maxWaitSeconds + " seconds!"); } } if (isActive()) { if (!force) { clusterService.sendShutdownMessage(); } // set the joined=false first so that // threads do not process unnecessary // events, such as remove address joined.set(false); setActive(false); setMasterAddress(null); try { Runtime.getRuntime().removeShutdownHook(shutdownHookThread); } catch (Throwable ignored) { } if (managementCenterService != null) { managementCenterService.shutdown(); } logger.finest("Shutting down client command service"); clientEngine.shutdown(); logger.finest("Shutting down node engine"); nodeEngine.shutdown(); if (multicastService != null) { logger.finest("Shutting down multicast service"); multicastService.stop(); } logger.finest("Shutting down connection manager"); connectionManager.shutdown(); textCommandService.stop(); masterAddress = null; if (securityContext != null) { securityContext.destroy(); } initializer.destroy(); serializationService.destroy(); int numThreads = threadGroup.activeCount(); Thread[] threads = new Thread[numThreads * 2]; numThreads = threadGroup.enumerate(threads, false); for (int i = 0; i < numThreads; i++) { Thread thread = threads[i]; if (thread.isAlive()) { logger.finest("Shutting down thread " + thread.getName()); thread.interrupt(); } } failedConnections.clear(); systemLogService.shutdown(); logger.info( "Hazelcast Shutdown is completed in " + (Clock.currentTimeMillis() - start) + " ms."); } }
@Test public void testMapRecordIdleEvictionOnMigration() throws InterruptedException { Config cfg = new Config(); final String name = "testMapRecordIdleEvictionOnMigration"; MapConfig mc = cfg.getMapConfig(name); int maxIdleSeconds = 10; int size = 100; final int nsize = size / 5; mc.setMaxIdleSeconds(maxIdleSeconds); TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(3); HazelcastInstance instance1 = factory.newHazelcastInstance(cfg); final IMap map = instance1.getMap(name); final CountDownLatch latch = new CountDownLatch(size - nsize); map.addEntryListener( new EntryAdapter() { public void entryEvicted(EntryEvent event) { latch.countDown(); } }, false); for (int i = 0; i < size; i++) { map.put(i, i); } final Thread thread = new Thread( new Runnable() { public void run() { while (!Thread.currentThread().isInterrupted()) { try { for (int i = 0; i < nsize; i++) { map.get(i); } Thread.sleep(1000); } catch (HazelcastInstanceNotActiveException e) { return; } catch (InterruptedException e) { return; } } } }); thread.start(); HazelcastInstance instance2 = factory.newHazelcastInstance(cfg); HazelcastInstance instance3 = factory.newHazelcastInstance(cfg); assertTrue(latch.await(1, TimeUnit.MINUTES)); Assert.assertEquals(nsize, map.size()); thread.interrupt(); thread.join(5000); }
@Test public void testEvictionLFU() { try { final int k = 1; final int size = 10000; final String mapName = "testEvictionLFU"; Config cfg = new Config(); MapConfig mc = cfg.getMapConfig(mapName); mc.setEvictionPolicy(MapConfig.EvictionPolicy.LFU); mc.setEvictionPercentage(20); MaxSizeConfig msc = new MaxSizeConfig(); msc.setMaxSizePolicy(MaxSizeConfig.MaxSizePolicy.PER_NODE); msc.setSize(size); mc.setMaxSizeConfig(msc); TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(k); final HazelcastInstance[] instances = factory.newInstances(cfg); IMap<Object, Object> map = instances[0].getMap(mapName); for (int i = 0; i < size / 2; i++) { map.put(i, i); map.get(i); } Thread.sleep(1000); for (int i = size / 2; i < size; i++) { map.put(i, i); } Thread.sleep(3000); Assert.assertFalse("No eviction!?!?!?", map.size() == size); boolean isFrequentlyUsedEvicted = false; for (int i = 0; i < size / 2; i++) { if (map.get(i) == null) { isFrequentlyUsedEvicted = true; break; } } Assert.assertFalse(isFrequentlyUsedEvicted); instances[0].getLifecycleService().shutdown(); } catch (InterruptedException e) { e.printStackTrace(); } }
/** * Test for the issue 477. Updates should also update the TTL * * @throws Exception */ @Test public void testMapPutWithTTL() throws Exception { int n = 1; TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(n); IMap<Integer, String> map = factory.newHazelcastInstance(null).getMap("testMapPutWithTTL"); map.put(1, "value0", 100, TimeUnit.MILLISECONDS); assertEquals(true, map.containsKey(1)); Thread.sleep(2500); assertEquals(false, map.containsKey(1)); map.put(1, "value1", 10, TimeUnit.SECONDS); assertEquals(true, map.containsKey(1)); Thread.sleep(5000); assertEquals(true, map.containsKey(1)); map.put(1, "value2", 10, TimeUnit.SECONDS); Thread.sleep(5000); assertEquals(true, map.containsKey(1)); map.put(1, "value3", 10, TimeUnit.SECONDS); assertEquals(true, map.containsKey(1)); }
@Test public void testEvictionLRU() { final int k = 2; final int size = 10000; try { final String mapName = "testEvictionLRU"; Config cfg = new Config(); MapConfig mc = cfg.getMapConfig(mapName); mc.setEvictionPolicy(MapConfig.EvictionPolicy.LRU); mc.setEvictionPercentage(10); MaxSizeConfig msc = new MaxSizeConfig(); msc.setMaxSizePolicy(MaxSizeConfig.MaxSizePolicy.PER_NODE); msc.setSize(size); mc.setMaxSizeConfig(msc); TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(k); final HazelcastInstance[] instances = factory.newInstances(cfg); IMap<Object, Object> map = instances[0].getMap(mapName); Thread.sleep(1000); for (int i = size / 2; i < size; i++) { map.put(i, i); } Thread.sleep(2000); for (int i = 0; i < size / 2; i++) { map.put(i, i); } Thread.sleep(1000); int recentlyUsedEvicted = 0; for (int i = 0; i < size / 2; i++) { if (map.get(i) == null) { recentlyUsedEvicted++; } } Assert.assertTrue(recentlyUsedEvicted == 0); } catch (InterruptedException e) { e.printStackTrace(); } }
public void start() { logger.finest( "We are asked to start and completelyShutdown is " + String.valueOf(completelyShutdown)); if (completelyShutdown) return; nodeEngine.start(); connectionManager.start(); if (config.getNetworkConfig().getJoin().getMulticastConfig().isEnabled()) { final Thread multicastServiceThread = new Thread( hazelcastInstance.threadGroup, multicastService, getThreadNamePrefix("MulticastThread")); multicastServiceThread.start(); } setActive(true); if (!completelyShutdown) { logger.finest("Adding ShutdownHook"); Runtime.getRuntime().addShutdownHook(shutdownHookThread); } logger.finest("finished starting threads, calling join"); join(); int clusterSize = clusterService.getSize(); if (config.getNetworkConfig().isPortAutoIncrement() && address.getPort() >= config.getNetworkConfig().getPort() + clusterSize) { StringBuilder sb = new StringBuilder("Config seed port is "); sb.append(config.getNetworkConfig().getPort()); sb.append(" and cluster size is "); sb.append(clusterSize); sb.append(". Some of the ports seem occupied!"); logger.warning(sb.toString()); } try { managementCenterService = new ManagementCenterService(hazelcastInstance); } catch (Exception e) { logger.warning("ManagementCenterService could not be constructed!", e); } initializer.afterInitialize(this); }
/** * Test for issue 614 * * @throws InterruptedException */ @Test public void testContainsKeyShouldDelayEviction() throws InterruptedException { Config cfg = new Config(); String mapname = "testContainsKeyShouldDelayEviction"; cfg.getMapConfig(mapname).setMaxIdleSeconds(3); TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(1); HazelcastInstance instance = factory.newHazelcastInstance(cfg); IMap<Object, Object> map = instance.getMap(mapname); map.put(1, 1); for (int i = 0; i < 20; i++) { assertTrue(map.containsKey(1)); Thread.sleep(500); } }
/* github issue 585 */ @Test public void testIssue585ZeroTTLShouldPreventEvictionWithSet() throws InterruptedException { Config config = new Config(); config.getGroupConfig().setName("testIssue585ZeroTTLShouldPreventEvictionWithSet"); NearCacheConfig nearCacheConfig = new NearCacheConfig(); config.getMapConfig("default").setNearCacheConfig(nearCacheConfig); int n = 1; TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(n); HazelcastInstance h = factory.newHazelcastInstance(config); IMap<String, String> map = h.getMap("testIssue585ZeroTTLShouldPreventEvictionWithSet"); map.set("key", "value", 1, TimeUnit.SECONDS); map.set("key", "value2", 0, TimeUnit.SECONDS); Thread.sleep(2000); assertEquals("value2", map.get("key")); h.getLifecycleService().shutdown(); }
/* github issue 304 */ @Test public void testIssue304EvictionDespitePut() throws InterruptedException { Config c = new Config(); c.getGroupConfig().setName("testIssue304EvictionDespitePut"); final HashMap<String, MapConfig> mapConfigs = new HashMap<String, MapConfig>(); final MapConfig value = new MapConfig(); value.setMaxIdleSeconds(3); mapConfigs.put("default", value); c.setMapConfigs(mapConfigs); final Properties properties = new Properties(); properties.setProperty("hazelcast.map.cleanup.delay.seconds", "1"); // we need faster cleanups c.setProperties(properties); int n = 1; TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(n); final HazelcastInstance hazelcastInstance = factory.newHazelcastInstance(c); IMap<String, Long> map = hazelcastInstance.getMap("testIssue304EvictionDespitePutMap"); final AtomicInteger evictCount = new AtomicInteger(0); map.addEntryListener( new EntryListener<String, Long>() { public void entryAdded(EntryEvent<String, Long> event) {} public void entryRemoved(EntryEvent<String, Long> event) {} public void entryUpdated(EntryEvent<String, Long> event) {} public void entryEvicted(EntryEvent<String, Long> event) { evictCount.incrementAndGet(); } }, true); String key = "key"; for (int i = 0; i < 5; i++) { map.put(key, System.currentTimeMillis()); Thread.sleep(1000); } assertEquals(evictCount.get(), 0); assertNotNull(map.get(key)); hazelcastInstance.getLifecycleService().shutdown(); }
/** * Test for the issue 537. Eviction event is fired for an object already removed * * @throws Exception */ @Test public void testEvictionAfterRemove() throws InterruptedException { Config cfg = new Config(); TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(1); HazelcastInstance instance = factory.newHazelcastInstance(cfg); IMap<Object, Object> map = instance.getMap("map"); final AtomicInteger count = new AtomicInteger(0); map.addEntryListener( new EntryAdapter<Object, Object>() { @Override public void entryEvicted(EntryEvent<Object, Object> event) { count.incrementAndGet(); } }, true); map.put(1, 1, 1, TimeUnit.SECONDS); map.put(2, 2, 1, TimeUnit.SECONDS); map.remove(1); Thread.sleep(2000); assertEquals(1, count.get()); }
@Test public void testEvictionLFU2() { try { final int k = 2; final int size = 10000; final String mapName = "testEvictionLFU2"; Config cfg = new Config(); MapConfig mc = cfg.getMapConfig(mapName); mc.setEvictionPolicy(MapConfig.EvictionPolicy.LFU); mc.setEvictionPercentage(90); MaxSizeConfig msc = new MaxSizeConfig(); msc.setMaxSizePolicy(MaxSizeConfig.MaxSizePolicy.PER_NODE); msc.setSize(size); mc.setMaxSizeConfig(msc); TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(k); final HazelcastInstance[] instances = factory.newInstances(cfg); IMap<Object, Object> map = instances[0].getMap(mapName); for (int i = 0; i < size; i++) { map.put(i, i); } for (int i = 0; i < 3; i++) { for (int j = 0; j < 100; j++) { assertNotNull(map.get(j)); } for (int j = size - 100; j < size; j++) { assertNotNull(map.get(j)); } Thread.sleep(1000); } } catch (Exception e) { e.printStackTrace(); } }
@Test public void testEvictionPerPartition() throws InterruptedException { final int k = 2; final int size = 10; final String mapName = "testEvictionPerPartition"; Config cfg = new Config(); final MapConfig mc = cfg.getMapConfig(mapName); mc.setEvictionPolicy(MapConfig.EvictionPolicy.LRU); mc.setEvictionPercentage(25); final MaxSizeConfig msc = new MaxSizeConfig(); msc.setMaxSizePolicy(MaxSizeConfig.MaxSizePolicy.PER_PARTITION); msc.setSize(size); mc.setMaxSizeConfig(msc); TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(k); final HazelcastInstance[] instances = factory.newInstances(cfg); final int pnum = instances[0].getPartitionService().getPartitions().size(); int insertCount = size * pnum * 2; Map map = instances[0].getMap(mapName); for (int i = 0; i < insertCount; i++) { map.put(i, i); } Thread.sleep(2000); assertTrue(map.size() < size * pnum * (100 - mc.getEvictionPercentage()) / 100); }
@Test public void testMapWideEviction() throws InterruptedException { int size = 10000; Config cfg = new Config(); MapConfig mc = cfg.getMapConfig("testMapWideEviction"); mc.setEvictionPolicy(MapConfig.EvictionPolicy.LRU); mc.setEvictionPercentage(25); MaxSizeConfig msc = new MaxSizeConfig(); msc.setMaxSizePolicy(MaxSizeConfig.MaxSizePolicy.PER_NODE); msc.setSize(size); mc.setMaxSizeConfig(msc); int n = 3; TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(n); final HazelcastInstance[] instances = factory.newInstances(cfg); IMap map = instances[0].getMap("testMapWideEviction"); for (int i = 0; i < size; i++) { map.put(i, i); } Thread.sleep(1200); assertTrue(map.size() <= (size * n * (100 - mc.getEvictionPercentage()) / 100)); }