Example #1
0
  @Test
  public void testMapRecordEviction() throws InterruptedException {
    int size = 100000;
    Config cfg = new Config();
    MapConfig mc = cfg.getMapConfig("testMapRecordEviction");
    mc.setTimeToLiveSeconds(1);
    final CountDownLatch latch = new CountDownLatch(size);
    mc.addEntryListenerConfig(
        new EntryListenerConfig()
            .setImplementation(
                new EntryAdapter() {
                  public void entryEvicted(EntryEvent event) {
                    latch.countDown();
                  }
                })
            .setLocal(true));

    TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(2);
    final HazelcastInstance[] instances = factory.newInstances(cfg);

    IMap map = instances[0].getMap("testMapRecordEviction");
    for (int i = 0; i < size; i++) {
      map.put(i, i);
    }
    assertTrue(latch.await(5, TimeUnit.MINUTES));
    assertEquals(0, map.size());
  }
Example #2
0
  @Test
  public void testZeroResetsTTL() throws InterruptedException {
    Config cfg = new Config();
    MapConfig mc = cfg.getMapConfig("testZeroResetsTTL");
    int ttl = 5;
    mc.setTimeToLiveSeconds(ttl);
    TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(1);
    HazelcastInstance instance = factory.newHazelcastInstance(cfg);
    IMap<Object, Object> map = instance.getMap("testZeroResetsTTL");
    final CountDownLatch latch = new CountDownLatch(1);
    map.addEntryListener(
        new EntryAdapter<Object, Object>() {
          public void entryEvicted(EntryEvent event) {
            latch.countDown();
          }
        },
        false);

    map.put(1, 1);
    map.put(2, 2);
    map.put(1, 2, 0, TimeUnit.SECONDS);
    latch.await(10, TimeUnit.SECONDS);
    assertNull(map.get(2));
    assertEquals(2, map.get(1));
  }
Example #3
0
  @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);
  }
Example #4
0
  @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();
    }
  }
Example #5
0
  /*
     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();
  }
Example #6
0
  @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();
    }
  }
Example #7
0
  @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();
    }
  }
Example #8
0
 @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);
 }
Example #9
0
  @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));
  }
Example #10
0
  @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());
    }
  }
Example #11
0
  // 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());
    }
  }
 @Test
 public void testMapConfig() {
   assertNotNull(config);
   assertEquals(6, config.getMapConfigs().size());
   MapConfig testMapConfig = config.getMapConfig("testMap");
   assertNotNull(testMapConfig);
   assertEquals("testMap", testMapConfig.getName());
   assertEquals(2, testMapConfig.getBackupCount());
   assertEquals(MapConfig.EvictionPolicy.NONE, testMapConfig.getEvictionPolicy());
   assertEquals(Integer.MAX_VALUE, testMapConfig.getMaxSizeConfig().getSize());
   assertEquals(30, testMapConfig.getEvictionPercentage());
   assertEquals(0, testMapConfig.getTimeToLiveSeconds());
   assertEquals("PUT_IF_ABSENT", testMapConfig.getMergePolicy());
   assertTrue(testMapConfig.isReadBackupData());
   assertEquals(2, testMapConfig.getMapIndexConfigs().size());
   for (MapIndexConfig index : testMapConfig.getMapIndexConfigs()) {
     if ("name".equals(index.getAttribute())) {
       assertFalse(index.isOrdered());
     } else if ("age".equals(index.getAttribute())) {
       assertTrue(index.isOrdered());
     } else {
       fail("unknown index!");
     }
   }
   // Test that the testMapConfig has a mapStoreConfig and it is correct
   MapStoreConfig testMapStoreConfig = testMapConfig.getMapStoreConfig();
   assertNotNull(testMapStoreConfig);
   assertEquals("com.hazelcast.spring.DummyStore", testMapStoreConfig.getClassName());
   assertTrue(testMapStoreConfig.isEnabled());
   assertEquals(0, testMapStoreConfig.getWriteDelaySeconds());
   assertEquals(MapStoreConfig.InitialLoadMode.EAGER, testMapStoreConfig.getInitialLoadMode());
   // Test that the testMapConfig has a nearCacheConfig and it is correct
   NearCacheConfig testNearCacheConfig = testMapConfig.getNearCacheConfig();
   assertNotNull(testNearCacheConfig);
   assertEquals(0, testNearCacheConfig.getTimeToLiveSeconds());
   assertEquals(60, testNearCacheConfig.getMaxIdleSeconds());
   assertEquals("LRU", testNearCacheConfig.getEvictionPolicy());
   assertEquals(5000, testNearCacheConfig.getMaxSize());
   assertTrue(testNearCacheConfig.isInvalidateOnChange());
   // Test that the testMapConfig2's mapStoreConfig implementation
   MapConfig testMapConfig2 = config.getMapConfig("testMap2");
   assertNotNull(testMapConfig2.getMapStoreConfig().getImplementation());
   assertEquals(dummyMapStore, testMapConfig2.getMapStoreConfig().getImplementation());
   assertEquals(
       MapStoreConfig.InitialLoadMode.LAZY,
       testMapConfig2.getMapStoreConfig().getInitialLoadMode());
   assertEquals("testWan", testMapConfig2.getWanReplicationRef().getName());
   //        assertEquals("hz.ADD_NEW_ENTRY",
   // testMapConfig2.getWanReplicationRef().getMergePolicy());
   assertEquals(1000, testMapConfig2.getMaxSizeConfig().getSize());
   assertEquals(
       MaxSizeConfig.MaxSizePolicy.PER_NODE, testMapConfig2.getMaxSizeConfig().getMaxSizePolicy());
   assertEquals(2, testMapConfig2.getEntryListenerConfigs().size());
   for (EntryListenerConfig listener : testMapConfig2.getEntryListenerConfigs()) {
     if (listener.getClassName() != null) {
       assertNull(listener.getImplementation());
       assertTrue(listener.isIncludeValue());
       assertFalse(listener.isLocal());
     } else {
       assertNotNull(listener.getImplementation());
       assertEquals(entryListener, listener.getImplementation());
       assertTrue(listener.isLocal());
       assertTrue(listener.isIncludeValue());
     }
   }
   MapConfig simpleMapConfig = config.getMapConfig("simpleMap");
   assertNotNull(simpleMapConfig);
   assertEquals("simpleMap", simpleMapConfig.getName());
   assertEquals(3, simpleMapConfig.getBackupCount());
   assertEquals(1, simpleMapConfig.getAsyncBackupCount());
   assertEquals(MapConfig.EvictionPolicy.LRU, simpleMapConfig.getEvictionPolicy());
   assertEquals(10, simpleMapConfig.getMaxSizeConfig().getSize());
   assertEquals(50, simpleMapConfig.getEvictionPercentage());
   assertEquals(1, simpleMapConfig.getTimeToLiveSeconds());
   assertEquals("LATEST_UPDATE", simpleMapConfig.getMergePolicy());
   // Test that the simpleMapConfig does NOT have a mapStoreConfig
   assertNull(simpleMapConfig.getMapStoreConfig());
   // Test that the simpleMapConfig does NOT have a nearCacheConfig
   assertNull(simpleMapConfig.getNearCacheConfig());
   MapConfig testMapConfig3 = config.getMapConfig("testMap3");
   assertEquals(
       "com.hazelcast.spring.DummyStoreFactory",
       testMapConfig3.getMapStoreConfig().getFactoryClassName());
   assertFalse(testMapConfig3.getMapStoreConfig().getProperties().isEmpty());
   assertEquals(testMapConfig3.getMapStoreConfig().getProperty("dummy.property"), "value");
   MapConfig testMapConfig4 = config.getMapConfig("testMap4");
   assertEquals(
       dummyMapStoreFactory, testMapConfig4.getMapStoreConfig().getFactoryImplementation());
 }