Example #1
0
  @Test
  public void testCacheLocalEntries() {
    int n = 2;
    String mapName = "test";

    Config config = new Config();
    final NearCacheConfig nearCacheConfig = new NearCacheConfig();
    nearCacheConfig.setCacheLocalEntries(true);
    nearCacheConfig.setInvalidateOnChange(false);
    final MapConfig mapConfig = config.getMapConfig(mapName);
    mapConfig.setNearCacheConfig(nearCacheConfig);
    HazelcastInstance instance = createHazelcastInstanceFactory(n).newInstances(config)[0];

    IMap<String, String> map = instance.getMap(mapName);

    int noOfEntries = 100;

    for (int i = 0; i < noOfEntries; i++) {
      map.put("key" + i, "value" + i);
    }

    // warm-up cache
    for (int i = 0; i < noOfEntries; i++) {
      map.get("key" + i);
    }

    NearCache nearCache = getNearCache(mapName, instance);
    assertEquals(noOfEntries, nearCache.size());
  }
Example #2
0
 private int countNotNullValuesInNearCache(String mapName, HazelcastInstance instance) {
   final NearCache nearCache = getNearCache(mapName, instance);
   final Collection<NearCache.CacheRecord> values = nearCache.getReadonlyMap().values();
   int count = 0;
   for (NearCache.CacheRecord e : values) {
     if (!NearCache.NULL_OBJECT.equals(e.getValue())) {
       count++;
     }
   }
   return count;
 }
Example #3
0
  @Test
  public void testBasicUsage() throws Exception {
    int n = 3;
    String mapName = "test";

    Config config = new Config();
    config
        .getMapConfig(mapName)
        .setNearCacheConfig(new NearCacheConfig().setInvalidateOnChange(true));
    TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(n);

    HazelcastInstance[] instances = factory.newInstances(config);
    IMap<Object, Object> map = instances[0].getMap(mapName);

    int count = 5000;
    for (int i = 0; i < count; i++) {
      map.put(i, i);
    }

    for (HazelcastInstance instance : instances) {
      IMap<Object, Object> m = instance.getMap(mapName);
      for (int i = 0; i < count; i++) {
        Assert.assertNotNull(m.get(i));
      }
    }

    for (int i = 0; i < count; i++) {
      map.put(i, i * 2);
    }

    for (HazelcastInstance instance : instances) {
      IMap<Object, Object> m = instance.getMap(mapName);
      for (int i = 0; i < count; i++) {
        Assert.assertNotNull(m.get(i));
      }
    }

    for (HazelcastInstance instance : instances) {
      NearCache nearCache = getNearCache(mapName, instance);
      int size = nearCache.size();
      assertTrue("NearCache Size: " + size, size > 0);
    }

    map.clear();
    for (HazelcastInstance instance : instances) {
      NearCache nearCache = getNearCache(mapName, instance);
      int size = nearCache.size();
      assertEquals(0, size);
    }
  }
Example #4
0
  @Test
  @Category(ProblematicTest.class)
  public void testNearCacheInvalidationByUsingMapPutAll() {
    int n = 3;
    String mapName = "test";

    Config config = new Config();
    config
        .getMapConfig(mapName)
        .setNearCacheConfig(new NearCacheConfig().setInvalidateOnChange(true));
    TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(n);

    HazelcastInstance[] instances = factory.newInstances(config);
    IMap<Object, Object> map = instances[0].getMap(mapName);

    int count = 5000;
    for (int i = 0; i < count; i++) {
      map.put(i, i);
    }

    // populate the near cache
    for (int i = 0; i < count; i++) {
      map.get(i);
    }

    final NearCache nearCache = getNearCache(mapName, instances[0]);
    assertTrue(
        nearCache.size()
            > (count / n
                - count
                    * 0.1)); // more-or-less (count / no_of_nodes) should be in the near cache now

    Map<Object, Object> invalidationMap = new HashMap<Object, Object>(count);
    for (int i = 0; i < count; i++) {
      invalidationMap.put(i, i);
    }
    map.putAll(invalidationMap); // this should invalidate the near cache

    assertTrueEventually(
        new AssertTask() {
          @Override
          public void run() {
            assertEquals("Invalidation is not working on putAll()", 0, nearCache.size());
          }
        });
  }