private static boolean isEvictableHeapSize(final MapContainer mapContainer) { final long usedHeapSize = getUsedHeapSize(mapContainer); if (usedHeapSize == -1L) { return false; } final MaxSizeConfig maxSizeConfig = mapContainer.getMapConfig().getMaxSizeConfig(); final int maxSize = getApproximateMaxSize(maxSizeConfig.getSize()); return maxSize < (usedHeapSize / ONE_KILOBYTE / ONE_KILOBYTE); }
@Test public void testMapNativeMaxSizePolicy() { MapConfig mapConfig = config.getMapConfig("map-with-native-max-size-policy"); MaxSizeConfig maxSizeConfig = mapConfig.getMaxSizeConfig(); assertEquals( MaxSizeConfig.MaxSizePolicy.USED_NATIVE_MEMORY_PERCENTAGE, maxSizeConfig.getMaxSizePolicy()); }
private static boolean isEvictableHeapPercentage(final MapContainer mapContainer) { final long usedHeapSize = getUsedHeapSize(mapContainer); if (usedHeapSize == -1L) { return false; } final MaxSizeConfig maxSizeConfig = mapContainer.getMapConfig().getMaxSizeConfig(); final int maxSize = getApproximateMaxSize(maxSizeConfig.getSize()); final long total = Runtime.getRuntime().totalMemory(); return maxSize < (1D * ONE_HUNDRED_PERCENT * usedHeapSize / total); }
@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 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(); } }
@Test(timeout = 120000) public void testOneMemberWriteThroughWithLRU() throws Exception { final int size = 10000; TestMapStore testMapStore = new TestMapStore(size * 2, 1, 1); testMapStore.setLoadAllKeys(false); Config config = newConfig(testMapStore, 0); config.setProperty(GroupProperty.PARTITION_COUNT, "1"); MaxSizeConfig maxSizeConfig = new MaxSizeConfig(); maxSizeConfig.setSize(size); MapConfig mapConfig = config.getMapConfig("default"); mapConfig.setEvictionPolicy(EvictionPolicy.LRU); mapConfig.setMaxSizeConfig(maxSizeConfig); mapConfig.setMinEvictionCheckMillis(0); TestHazelcastInstanceFactory nodeFactory = createHazelcastInstanceFactory(3); HazelcastInstance h1 = nodeFactory.newHazelcastInstance(config); IMap map = h1.getMap("default"); final CountDownLatch countDownLatch = new CountDownLatch(size); map.addEntryListener( new EntryAdapter() { @Override public void entryEvicted(EntryEvent event) { countDownLatch.countDown(); } }, false); for (int i = 0; i < size * 2; i++) { // trigger eviction. if (i == (size * 2) - 1 || i == size) { sleepMillis(1001); } map.put(i, new Employee("joe", i, true, 100.00)); } assertEquals(testMapStore.getStore().size(), size * 2); assertOpenEventually(countDownLatch); final String msgFailure = String.format("map size: %d put count: %d", map.size(), size); assertTrue(msgFailure, map.size() > size / 2); assertTrue(msgFailure, map.size() <= size); assertEquals(testMapStore.getStore().size(), size * 2); }
private static boolean isEvictablePerPartition(final MapContainer mapContainer) { final MapService mapService = mapContainer.getMapService(); final MaxSizeConfig maxSizeConfig = mapContainer.getMapConfig().getMaxSizeConfig(); final int maxSize = getApproximateMaxSize(maxSizeConfig.getSize()); final String mapName = mapContainer.getName(); final NodeEngine nodeEngine = mapService.getNodeEngine(); final InternalPartitionService partitionService = nodeEngine.getPartitionService(); for (int i = 0; i < partitionService.getPartitionCount(); i++) { final Address owner = partitionService.getPartitionOwner(i); if (nodeEngine.getThisAddress().equals(owner)) { final PartitionContainer container = mapService.getPartitionContainer(i); if (container == null) { return false; } final int size = container.getRecordStore(mapName).size(); if (size >= maxSize) { return true; } } } return false; }
public static boolean checkEvictable(MapContainer mapContainer) { final MaxSizeConfig maxSizeConfig = mapContainer.getMapConfig().getMaxSizeConfig(); final MaxSizeConfig.MaxSizePolicy maxSizePolicy = maxSizeConfig.getMaxSizePolicy(); boolean result; switch (maxSizePolicy) { case PER_NODE: result = isEvictablePerNode(mapContainer); break; case PER_PARTITION: result = isEvictablePerPartition(mapContainer); break; case USED_HEAP_PERCENTAGE: result = isEvictableHeapPercentage(mapContainer); break; case USED_HEAP_SIZE: result = isEvictableHeapSize(mapContainer); break; default: throw new IllegalArgumentException( "Not an appropriate max size policy [" + maxSizePolicy + ']'); } return result; }
@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)); }
@Test public void testEvictionSpeedTestPerPartition() { final int k = 2; final int size = 100; final CountDownLatch latch = new CountDownLatch(k); final String mapName = "testEvictionSpeedTestPerPartition"; 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(); final AtomicInteger failCount = new AtomicInteger(0); new Thread() { final IMap map = instances[0].getMap(mapName); public void run() { try { Thread.sleep(1000); while (latch.getCount() != 0) { try { int msize = map.size(); if (msize > (size * pnum * 1.2)) { failCount.incrementAndGet(); } Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } catch (InterruptedException e) { e.printStackTrace(); } } }.start(); for (int i = 0; i < k; i++) { final IMap map = instances[i].getMap(mapName); new Thread() { public void run() { for (int j = 0; j < 100000; j++) { map.put(k + "-" + j, j); } latch.countDown(); } }.start(); } try { Assert.assertEquals(latch.await(10, TimeUnit.MINUTES), true); Assert.assertEquals(0, failCount.get()); } catch (InterruptedException e) { e.printStackTrace(); Assert.fail(e.getMessage()); } }
// current eviction check period is 1 second. // about 30.000 records can be put in one second // so the size should be adapted @Test public void testEvictionSpeedTest() { final int k = 3; final int size = 10000; final CountDownLatch latch = new CountDownLatch(k); final String mapName = "testEvictionSpeedTest"; 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_NODE); msc.setSize(size); mc.setMaxSizeConfig(msc); TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(k); final HazelcastInstance[] instances = factory.newInstances(cfg); final AtomicBoolean success = new AtomicBoolean(true); new Thread() { final IMap map = instances[0].getMap(mapName); public void run() { try { Thread.sleep(1000); while (latch.getCount() != 0) { try { int msize = map.size(); if (msize > (size * k + size * k * 10 / 100)) { success.set(false); break; } Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } catch (InterruptedException e) { e.printStackTrace(); } } }.start(); for (int i = 0; i < k; i++) { final IMap map = instances[i].getMap(mapName); new Thread() { public void run() { for (int j = 0; j < size; j++) { map.put(k + "-" + j, j); } latch.countDown(); } }.start(); } try { Assert.assertTrue(latch.await(10, TimeUnit.MINUTES)); Assert.assertTrue(success.get()); } catch (InterruptedException e) { e.printStackTrace(); Assert.fail(e.getMessage()); } }