@Test
 public void valuesToArray() {
   HazelcastClient hClient = getHazelcastClient();
   IMap map = hClient.getMap("valuesToArray");
   assertEquals(0, map.size());
   map.put("a", "1");
   map.put("b", "2");
   map.put("c", "3");
   assertEquals(3, map.size());
   {
     final Object[] values = map.values().toArray();
     Arrays.sort(values);
     assertArrayEquals(new Object[] {"1", "2", "3"}, values);
   }
   {
     final String[] values = (String[]) map.values().toArray(new String[3]);
     Arrays.sort(values);
     assertArrayEquals(new String[] {"1", "2", "3"}, values);
   }
   {
     final String[] values = (String[]) map.values().toArray(new String[2]);
     Arrays.sort(values);
     assertArrayEquals(new String[] {"1", "2", "3"}, values);
   }
   {
     final String[] values = (String[]) map.values().toArray(new String[5]);
     Arrays.sort(values, 0, 3);
     assertArrayEquals(new String[] {"1", "2", "3", null, null}, values);
   }
 }
 @Test
 public void addIndex() {
   HazelcastClient hClient = getHazelcastClient();
   IMap map = hClient.getMap("addIndex");
   int size = 1000;
   for (int i = 0; i < size; i++) {
     map.put(String.valueOf(i), new Employee("name" + i, i, true, 0));
   }
   EntryObject e = new PredicateBuilder().getEntryObject();
   Predicate predicate = e.get("age").equal(23);
   long begin = Clock.currentTimeMillis();
   Set<Entry<Object, Object>> set = map.entrySet(predicate);
   long timeWithoutIndex = Clock.currentTimeMillis() - begin;
   assertEquals(1, set.size());
   assertEquals(size, map.size());
   map.destroy();
   map = hClient.getMap("addIndex");
   map.addIndex("age", true);
   for (int i = 0; i < size; i++) {
     map.put(String.valueOf(i), new Employee("name" + i, i, true, 0));
   }
   begin = Clock.currentTimeMillis();
   set = map.entrySet(predicate);
   long timeWithIndex = Clock.currentTimeMillis() - begin;
   assertEquals(1, set.size());
   assertEquals(size, map.size());
   assertTrue(timeWithoutIndex > 2 * timeWithIndex);
   //    	map.addIndex("age", true);
 }
 @Test
 public void tryPut() throws InterruptedException {
   HazelcastClient hClient = getHazelcastClient();
   IMap<String, String> map = hClient.getMap("tryPut");
   assertEquals(0, map.size());
   Boolean result = map.tryPut("1", "CBDEF", 1, TimeUnit.SECONDS);
   assertTrue(result);
   assertEquals(1, map.size());
 }
 @Test
 public void putWithTTL() throws InterruptedException {
   HazelcastClient hClient = getHazelcastClient();
   IMap<String, String> map = hClient.getMap("putWithTTL");
   assertEquals(0, map.size());
   map.put("1", "CBDEF", 100, TimeUnit.MILLISECONDS);
   assertEquals(1, map.size());
   Thread.sleep(200);
   assertEquals(0, map.size());
 }
 @Test
 public void putIfAbsentWithTtl() throws InterruptedException {
   HazelcastClient hClient = getHazelcastClient();
   IMap<String, String> map = hClient.getMap("putIfAbsentWithTtl");
   String result = map.put("1", "CBDEF");
   assertNull(result);
   assertNull(map.putIfAbsent("2", "C", 100, TimeUnit.MILLISECONDS));
   assertEquals(2, map.size());
   assertEquals("C", map.putIfAbsent("2", "D", 100, TimeUnit.MILLISECONDS));
   Thread.sleep(100);
   assertEquals(1, map.size());
 }
예제 #6
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());
  }
예제 #7
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);
  }
 @Test
 public void testIssue508And513() throws Exception {
   HazelcastClient client = getHazelcastClient();
   IMap<String, HashSet<byte[]>> callEventsMap = client.getMap("CALL_EVENTS");
   IMap<String, Long> metaDataMap = client.getMap("CALL_META_DATA");
   IMap<String, byte[]> callStartMap = client.getMap("CALL_START_EVENTS");
   MultiMap<String, String> calls = client.getMultiMap("CALLS");
   calls.lock("1");
   calls.unlock("1");
   byte[] bytes = new byte[10];
   HashSet<byte[]> hashSet = new HashSet<byte[]>();
   hashSet.add(bytes);
   String callId = "1";
   callEventsMap.put(callId, hashSet);
   callStartMap.put(callId, bytes);
   metaDataMap.put(callId, 10L);
   Transaction txn = client.getTransaction();
   txn.begin();
   try {
     // remove the data
     callEventsMap.remove(callId);
     // remove meta data
     metaDataMap.remove(callId);
     // remove call start
     callStartMap.remove(callId);
     calls.put(callId, callId);
     txn.commit();
   } catch (Exception e) {
     fail();
   }
   assertNull(callEventsMap.get(callId));
   assertNull(metaDataMap.get(callId));
   assertNull(callStartMap.get(callId));
   assertEquals(0, callEventsMap.size());
   assertEquals(0, metaDataMap.size());
   assertEquals(0, callStartMap.size());
 }
예제 #9
0
 /*
    github issue 585
 */
 @Test
 public void testIssue585SetWithoutTTL() 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");
   Thread.sleep(2000);
   assertEquals(0, map.size());
   h.getLifecycleService().shutdown();
 }
예제 #10
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();
    }
  }
 @Test
 public void addListenerForKey() throws InterruptedException, IOException {
   HazelcastClient hClient = getHazelcastClient();
   final IMap<String, String> map = hClient.getMap("addListenerForKey");
   map.clear();
   assertEquals(0, map.size());
   final CountDownLatch entryAddLatch = new CountDownLatch(1);
   final CountDownLatch entryUpdatedLatch = new CountDownLatch(1);
   final CountDownLatch entryRemovedLatch = new CountDownLatch(1);
   CountDownLatchEntryListener<String, String> listener =
       new CountDownLatchEntryListener<String, String>(
           entryAddLatch, entryUpdatedLatch, entryRemovedLatch);
   map.addEntryListener(listener, "hello", true);
   assertNull(map.get("hello"));
   map.put("hello", "world");
   map.put("hello", "new world");
   assertEquals("new world", map.get("hello"));
   map.remove("hello");
   assertTrue(entryAddLatch.await(10, TimeUnit.SECONDS));
   assertTrue(entryUpdatedLatch.await(10, TimeUnit.SECONDS));
   assertTrue(entryRemovedLatch.await(10, TimeUnit.SECONDS));
 }
예제 #12
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));
  }
 @Test
 public void addListenerAndMultiPut() throws InterruptedException, IOException {
   HazelcastClient hClient = getHazelcastClient();
   final IMap<String, byte[]> map = hClient.getMap("addListenerAndMultiPut");
   map.clear();
   int counter = 100;
   assertEquals(0, map.size());
   final CountDownLatch entryAddLatch = new CountDownLatch(counter);
   final CountDownLatch entryUpdatedLatch = new CountDownLatch(counter);
   final CountDownLatch entryRemovedLatch = new CountDownLatch(counter);
   CountDownLatchEntryListener<String, byte[]> listener =
       new CountDownLatchEntryListener<String, byte[]>(
           entryAddLatch, entryUpdatedLatch, entryRemovedLatch);
   map.addEntryListener(listener, true);
   assertNull(map.get("hello"));
   Map<String, byte[]> many = new HashMap<String, byte[]>();
   for (int i = 0; i < counter; i++) {
     many.put("" + i, new byte[i]);
   }
   map.putAll(many);
   assertTrue(entryAddLatch.await(10, TimeUnit.SECONDS));
   //        assertTrue(entryUpdatedLatch.await(10, TimeUnit.MILLISECONDS));
   //        assertTrue(entryRemovedLatch.await(10, TimeUnit.MILLISECONDS));
 }
 @Test
 public void getSize() {
   HazelcastClient hClient = getHazelcastClient();
   IMap map = hClient.getMap("getSize");
   assertEquals(0, map.size());
   map.put("a", "b");
   assertEquals(1, map.size());
   for (int i = 0; i < 100; i++) {
     map.put(String.valueOf(i), String.valueOf(i));
   }
   assertEquals(101, map.size());
   map.remove("a");
   assertEquals(100, map.size());
   for (int i = 0; i < 50; i++) {
     map.remove(String.valueOf(i));
   }
   assertEquals(50, map.size());
   for (int i = 50; i < 100; i++) {
     map.remove(String.valueOf(i));
   }
   assertEquals(0, map.size());
 }
예제 #15
0
 @Test
 public void testPutWithTwoMember() throws Exception {
   Config config = new Config();
   HazelcastInstance h1 = Hazelcast.newHazelcastInstance(config);
   HazelcastInstance h2 = Hazelcast.newHazelcastInstance(config);
   assertEquals(2, h1.getCluster().getMembers().size());
   assertEquals(2, h2.getCluster().getMembers().size());
   IMap imap1 = h1.getMap("default");
   IMap imap2 = h2.getMap("default");
   assertEquals(0, imap1.size());
   assertEquals(0, imap2.size());
   CMap cmap1 = getCMap(h1, "default");
   CMap cmap2 = getCMap(h2, "default");
   assertNotNull(cmap1);
   assertNotNull(cmap2);
   Object key = "1";
   Object value = "value";
   Data dKey = toData(key);
   Data dValue = toData(value);
   imap1.put(key, value, 5, TimeUnit.SECONDS);
   assertEquals(1, cmap1.mapRecords.size());
   assertEquals(1, cmap2.mapRecords.size());
   assertEquals(
       1,
       cmap1.getMapIndexService().getOwnedRecords().size()
           + cmap2.getMapIndexService().getOwnedRecords().size());
   Record record1 = cmap1.getRecord(dKey);
   Record record2 = cmap2.getRecord(dKey);
   long now = System.currentTimeMillis();
   long millisLeft1 = record1.getExpirationTime() - now;
   long millisLeft2 = record2.getExpirationTime() - now;
   assertTrue(millisLeft1 <= 5000 && millisLeft1 > 0);
   assertTrue(millisLeft2 <= 5000 && millisLeft2 > 0);
   assertTrue(record1.isActive());
   assertTrue(record2.isActive());
   assertEquals(1, record1.valueCount());
   assertEquals(1, record2.valueCount());
   assertEquals(dValue, record1.getValueData());
   assertEquals(dValue, record2.getValueData());
   imap1.set("2", "value2", 5, TimeUnit.SECONDS);
   assertEquals("value2", imap1.get("2"));
   assertEquals("value2", imap2.get("2"));
   Thread.sleep(6000);
   assertNull(imap1.get("2"));
   assertNull(imap2.get("2"));
   now = System.currentTimeMillis();
   assertFalse(record1.isValid(now));
   assertFalse(record2.isValid(now));
   Thread.sleep(23000);
   assertEquals(0, cmap1.getMapIndexService().getOwnedRecords().size());
   assertEquals(0, cmap2.getMapIndexService().getOwnedRecords().size());
   assertEquals(0, cmap1.mapRecords.size());
   assertEquals(0, cmap2.mapRecords.size());
   imap1.put(key, value, 10, TimeUnit.SECONDS);
   assertTrue(migrateKey(key, h1, h1, 0));
   assertTrue(migrateKey(key, h1, h2, 1));
   assertEquals(1, cmap1.mapRecords.size());
   assertEquals(1, cmap2.mapRecords.size());
   assertEquals(
       1,
       cmap1.getMapIndexService().getOwnedRecords().size()
           + cmap2.getMapIndexService().getOwnedRecords().size());
   record1 = cmap1.getRecord(dKey);
   record2 = cmap2.getRecord(dKey);
   now = System.currentTimeMillis();
   millisLeft1 = record1.getExpirationTime() - now;
   millisLeft2 = record2.getExpirationTime() - now;
   assertTrue(millisLeft1 <= 11000 && millisLeft1 > 0);
   assertTrue(millisLeft2 <= 11000 && millisLeft2 > 0);
   assertTrue(record1.isActive());
   assertTrue(record2.isActive());
   assertTrue(record1.isValid(now));
   assertTrue(record2.isValid(now));
   assertEquals(1, record1.valueCount());
   assertEquals(1, record2.valueCount());
   assertEquals(1, cmap1.mapRecords.size());
   assertEquals(1, cmap2.mapRecords.size());
   assertEquals(
       1,
       cmap1.getMapIndexService().getOwnedRecords().size()
           + cmap2.getMapIndexService().getOwnedRecords().size());
   assertTrue(migrateKey(key, h1, h2, 0));
   assertTrue(migrateKey(key, h1, h1, 1));
   cmap1.startCleanup(true);
   cmap2.startCleanup(true);
   assertEquals(1, cmap1.mapRecords.size());
   assertEquals(1, cmap2.mapRecords.size());
   assertEquals(1, cmap2.getMapIndexService().getOwnedRecords().size());
   assertEquals(0, cmap1.getMapIndexService().getOwnedRecords().size());
   now = System.currentTimeMillis();
   millisLeft1 = record1.getExpirationTime() - now;
   millisLeft2 = record2.getExpirationTime() - now;
   assertTrue(millisLeft1 <= 10000 && millisLeft1 > 0);
   assertTrue(millisLeft2 <= 10000 && millisLeft2 > 0);
   assertTrue(record1.isActive());
   assertTrue(record2.isActive());
   assertTrue(record1.isValid(now));
   assertTrue(record2.isValid(now));
   assertEquals(1, record1.valueCount());
   assertEquals(1, record2.valueCount());
   Thread.sleep(11000);
   now = System.currentTimeMillis();
   assertFalse(record1.isValid(now));
   assertFalse(record2.isValid(now));
   Thread.sleep(20000);
   assertEquals(0, cmap1.mapRecords.size());
   assertEquals(0, cmap2.mapRecords.size());
   assertEquals(0, cmap1.getMapIndexService().getOwnedRecords().size());
   assertEquals(0, cmap2.getMapIndexService().getOwnedRecords().size());
   imap1.put("1", "value1");
   record1 = cmap1.getRecord(dKey);
   record2 = cmap2.getRecord(dKey);
   assertEquals(1, cmap1.mapRecords.size());
   assertEquals(1, cmap2.mapRecords.size());
   assertEquals(0, cmap1.getMapIndexService().getOwnedRecords().size());
   assertEquals(1, cmap2.getMapIndexService().getOwnedRecords().size());
   now = System.currentTimeMillis();
   assertEquals(Long.MAX_VALUE, record1.getExpirationTime());
   assertEquals(Long.MAX_VALUE, record2.getExpirationTime());
   assertTrue(record1.isActive());
   assertTrue(record2.isActive());
   assertTrue(record1.isValid(now));
   assertTrue(record2.isValid(now));
   assertEquals(1, record1.valueCount());
   assertEquals(1, record2.valueCount());
   imap1.remove("1");
   assertEquals(0, cmap1.getMapIndexService().getOwnedRecords().size());
   assertEquals(0, cmap2.getMapIndexService().getOwnedRecords().size());
   Thread.sleep(20000);
   assertEquals(0, cmap1.mapRecords.size());
   assertEquals(0, cmap2.mapRecords.size());
   assertEquals(0, cmap1.mapIndexService.size());
   assertEquals(0, cmap2.mapIndexService.size());
 }