@Test
  public void testQueryDuringAndAfterMigrationWithIndex() throws Exception {
    Config cfg = new Config();
    final HazelcastInstance h1 = nodeFactory.newHazelcastInstance(cfg);

    IMap imap = h1.getMap("employees");
    imap.addIndex("name", false);
    imap.addIndex("active", false);

    int size = 500;
    for (int i = 0; i < size; i++) {
      imap.put(String.valueOf(i), new Employee("joe" + i, i % 60, ((i & 1) == 1), (double) i));
    }

    nodeFactory.newInstances(cfg, 3);

    final IMap employees = h1.getMap("employees");
    assertTrueAllTheTime(
        new AssertTask() {
          @Override
          public void run() throws Exception {
            Collection<Employee> values =
                employees.values(new SqlPredicate("active and name LIKE 'joe15%'"));
            for (Employee employee : values) {
              assertTrue(employee.isActive() && employee.getName().startsWith("joe15"));
            }
            assertEquals(6, values.size());
          }
        },
        3);
  }
Exemple #2
0
  @Test
  public void testNearCacheStats() throws Exception {
    String mapName = "NearCacheStatsTest";
    Config config = new Config();
    config
        .getMapConfig(mapName)
        .setNearCacheConfig(new NearCacheConfig().setInvalidateOnChange(true));
    TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(2);
    HazelcastInstance[] instances = factory.newInstances(config);
    IMap<Integer, Integer> map = instances[0].getMap("NearCacheStatsTest");

    for (int i = 0; i < 1000; i++) {
      map.put(i, i);
    }
    // populate near cache
    for (int i = 0; i < 1000; i++) {
      map.get(i);
    }

    NearCacheStats stats = map.getLocalMapStats().getNearCacheStats();

    assertTrue("owned Entries", 400 < stats.getOwnedEntryCount());
    assertTrue("misses", 1000 == stats.getMisses());
    // make some hits
    for (int i = 0; i < 1000; i++) {
      map.get(i);
    }
    NearCacheStats stats2 = map.getLocalMapStats().getNearCacheStats();

    assertTrue("hits", 400 < stats2.getHits());
    assertTrue("misses", 400 < stats2.getMisses());
    assertTrue("hits+misses", 2000 == stats2.getHits() + stats2.getMisses());
  }
  @Test(timeout = MINUTE)
  public void testQueryDuringAndAfterMigration() throws Exception {
    HazelcastInstance h1 = nodeFactory.newHazelcastInstance();
    int count = 500;
    IMap imap = h1.getMap("employees");
    for (int i = 0; i < count; i++) {
      imap.put(String.valueOf(i), new Employee("joe" + i, i % 60, ((i & 1) == 1), (double) i));
    }

    nodeFactory.newInstances(new Config(), 3);

    final IMap employees = h1.getMap("employees");
    assertTrueAllTheTime(
        new AssertTask() {
          @Override
          public void run() throws Exception {
            Collection<Employee> values =
                employees.values(new SqlPredicate("active and name LIKE 'joe15%'"));
            for (Employee employee : values) {
              assertTrue(employee.isActive());
            }
            assertEquals(6, values.size());
          }
        },
        3);
  }
Exemple #4
0
  @Test
  public void testWaitingIndefinitely() throws InterruptedException {
    TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(5);
    final Config config = new Config();
    config.setProperty(GroupProperties.PROP_OPERATION_CALL_TIMEOUT_MILLIS, "2000");
    final HazelcastInstance[] instances = factory.newInstances(config);

    instances[0].getLock("testWaitingIndefinitely").lock();

    final CountDownLatch latch = new CountDownLatch(1);
    new Thread() {
      public void run() {
        try {
          // because max timeout=2000 we get timeout exception which we should not
          instances[1].getLock("testWaitingIndefinitely").lock();
          latch.countDown();
        } catch (Exception ignored) {
        }
      }
    }.start();

    // wait for enough time which is greater than max-timeout (2000)
    Thread.sleep(10000);

    instances[0].getLock("testWaitingIndefinitely").unlock();

    assertTrue(latch.await(5, TimeUnit.SECONDS));
  }
Exemple #5
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());
  }
  @Test
  public void testDataIntegrity() throws InterruptedException {
    setLoggingLog4j();
    System.out.println("nodeCount = " + nodeCount);
    System.out.println("operations = " + operations);
    System.out.println("keyCount = " + keyCount);
    Config config = new Config();
    config.getReplicatedMapConfig("test").setReplicationDelayMillis(0);
    TestHazelcastInstanceFactory factory = new TestHazelcastInstanceFactory(nodeCount);
    final HazelcastInstance[] instances = factory.newInstances(config);
    String replicatedMapName = "test";
    final List<ReplicatedMap> maps = createMapOnEachInstance(instances, replicatedMapName);
    ArrayList<Integer> keys = generateRandomIntegerList(keyCount);
    Thread[] threads = createThreads(nodeCount, maps, keys, operations);
    for (Thread thread : threads) {
      thread.start();
    }
    for (Thread thread : threads) {
      thread.join();
    }
    for (int i = 0; i < keyCount; i++) {
      final String key = "foo-" + keys.get(i);
      assertTrueEventually(
          new AssertTask() {
            @Override
            public void run() throws Exception {
              System.out.println("---------------------");
              System.out.println("key = " + key);
              printValues();
              assertValuesAreEqual();
            }

            private void printValues() throws Exception {
              for (int j = 0; j < maps.size(); j++) {
                ReplicatedMap map = maps.get(j);
                System.out.println(
                    "value["
                        + j
                        + "] = "
                        + map.get(key)
                        + " , store version : "
                        + getStore(map, key).getVersion());
              }
            }

            private void assertValuesAreEqual() {
              for (int i = 0; i < maps.size() - 1; i++) {
                ReplicatedMap map1 = maps.get(i);
                ReplicatedMap map2 = maps.get(i + 1);
                Object v1 = map1.get(key);
                Object v2 = map2.get(key);
                assertNotNull(v1);
                assertNotNull(v2);
                assertEquals(v1, v2);
              }
            }
          },
          120);
    }
  }
  @Test
  public void testWaitingIndefinitely() throws InterruptedException {
    final Config config = new Config();
    config.setProperty(GroupProperties.PROP_OPERATION_CALL_TIMEOUT_MILLIS, "3000");

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

    // need to warm-up partitions,
    // since waiting for lock backup can take up to 5 seconds
    // and that may cause OperationTimeoutException with "No response for 4000 ms" error.
    warmUpPartitions(instances);

    final String name = "testWaitingIndefinitely";
    ILock lock = instances[0].getLock(name);
    lock.lock();

    final CountDownLatch latch = new CountDownLatch(1);
    new Thread() {
      public void run() {
        try {
          // because max timeout=3000 we get timeout exception which we should not
          instances[1].getLock(name).lock();
          latch.countDown();
        } catch (Exception ignored) {
        }
      }
    }.start();

    // wait for enough time which is greater than max-timeout (3000)
    sleepSeconds(10);
    lock.unlock();

    assertTrue(latch.await(20, TimeUnit.SECONDS));
  }
  @Test
  public void testSubmitToMemberRunnable() throws InterruptedException {
    final int k = simpleTestNodeCount;
    TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(k);
    final HazelcastInstance[] instances = factory.newInstances(new Config());
    final AtomicInteger count = new AtomicInteger(0);
    final CountDownLatch latch = new CountDownLatch(k);
    final ExecutionCallback callback =
        new ExecutionCallback() {
          public void onResponse(Object response) {
            if (response == null) {
              count.incrementAndGet();
            }
            latch.countDown();
          }

          public void onFailure(Throwable t) {}
        };
    for (int i = 0; i < k; i++) {
      final HazelcastInstance instance = instances[i];
      final IExecutorService service = instance.getExecutorService("testSubmitToMemberRunnable");
      final String script =
          "if(!hazelcast.getCluster().getLocalMember().equals(member)) "
              + "hazelcast.getAtomicLong('testSubmitToMemberRunnable').incrementAndGet();";
      final HashMap map = new HashMap();
      map.put("member", instance.getCluster().getLocalMember());
      service.submitToMember(
          new ScriptRunnable(script, map), instance.getCluster().getLocalMember(), callback);
    }
    latch.await(10, TimeUnit.SECONDS);
    assertEquals(0, instances[0].getAtomicLong("testSubmitToMemberRunnable").get());
    assertEquals(k, count.get());
  }
 private HazelcastInstance[] createHazelcastInstances() {
   Config config = new Config();
   final String configName = randomString();
   config.getQueueConfig(configName).setMaxSize(100);
   TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(2);
   return factory.newInstances(config);
 }
  @Test
  public void testSubmitToAllMembersCallable() throws InterruptedException {
    final int k = simpleTestNodeCount;
    TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(k);
    final HazelcastInstance[] instances = factory.newInstances(new Config());
    final AtomicInteger count = new AtomicInteger(0);
    final CountDownLatch countDownLatch = new CountDownLatch(k * k);
    final MultiExecutionCallback callback =
        new MultiExecutionCallback() {
          public void onResponse(Member member, Object value) {
            count.incrementAndGet();
            countDownLatch.countDown();
          }

          public void onComplete(Map<Member, Object> values) {}
        };
    for (int i = 0; i < k; i++) {
      final IExecutorService service =
          instances[i].getExecutorService("testSubmitToAllMembersCallable");
      final String script =
          "hazelcast.getAtomicLong('testSubmitToAllMembersCallable').incrementAndGet();";
      service.submitToAllMembers(new ScriptCallable(script, null), callback);
    }
    countDownLatch.await(30, TimeUnit.SECONDS);
    final IAtomicLong result = instances[0].getAtomicLong("testSubmitToAllMembersCallable");
    assertEquals(k * k, result.get());
    assertEquals(k * k, count.get());
  }
  @Test
  public void testSubmitToKeyOwnerCallable() throws Exception {
    final int k = simpleTestNodeCount;
    TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(k);
    final HazelcastInstance[] instances = factory.newInstances(new Config());
    final AtomicInteger count = new AtomicInteger(0);
    final CountDownLatch latch = new CountDownLatch(k / 2);
    final ExecutionCallback callback =
        new ExecutionCallback() {
          public void onResponse(Object response) {
            if ((Boolean) response) count.incrementAndGet();
            latch.countDown();
          }

          public void onFailure(Throwable t) {}
        };
    for (int i = 0; i < k; i++) {
      final HazelcastInstance instance = instances[i];
      final IExecutorService service = instance.getExecutorService("testSubmitToKeyOwnerCallable");
      final String script = "hazelcast.getCluster().getLocalMember().equals(member)";
      final HashMap map = new HashMap();
      final Member localMember = instance.getCluster().getLocalMember();
      map.put("member", localMember);
      int key = 0;
      while (!localMember.equals(instance.getPartitionService().getPartition(++key).getOwner())) ;
      if (i % 2 == 0) {
        final Future f = service.submitToKeyOwner(new ScriptCallable(script, map), key);
        assertTrue((Boolean) f.get(5, TimeUnit.SECONDS));
      } else {
        service.submitToKeyOwner(new ScriptCallable(script, map), key, callback);
      }
    }
    assertTrue(latch.await(30, TimeUnit.SECONDS));
    assertEquals(k / 2, count.get());
  }
 @Test
 public void testPollLong() throws Exception {
   TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(2);
   final HazelcastInstance[] instances = factory.newInstances();
   final HazelcastInstance h1 = instances[0];
   final HazelcastInstance h2 = instances[1];
   final IQueue q1 = h1.getQueue("default");
   final IQueue q2 = h2.getQueue("default");
   final CountDownLatch offerLatch = new CountDownLatch(2 * 100);
   Thread.sleep(1000);
   new Thread(
           new Runnable() {
             public void run() {
               for (int i = 0; i < 100; i++) {
                 if (q1.offer("item")) {
                   offerLatch.countDown();
                 }
                 if (q2.offer("item")) {
                   offerLatch.countDown();
                 }
               }
             }
           })
       .start();
   assertOpenEventually(offerLatch);
   final ExecutorService es = Executors.newFixedThreadPool(50);
   final CountDownLatch latch = new CountDownLatch(200);
   Thread.sleep(3000);
   for (int i = 0; i < 100; i++) {
     es.execute(
         new Runnable() {
           public void run() {
             try {
               if ("item".equals(q1.poll(5, TimeUnit.SECONDS))) {
                 latch.countDown();
               }
             } catch (InterruptedException e) {
               e.printStackTrace();
             }
           }
         });
     es.execute(
         new Runnable() {
           public void run() {
             try {
               if ("item".equals(q2.poll(5, TimeUnit.SECONDS))) {
                 latch.countDown();
               }
             } catch (InterruptedException e) {
               e.printStackTrace();
             }
           }
         });
   }
   assertOpenEventually(latch);
   es.shutdown();
 }
Exemple #13
0
  @Test
  public void testTopicPublishingMember() {
    final int nodeCount = 3;
    final String randomName = "testTopicPublishingMember" + generateRandomString(5);

    TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(nodeCount);
    HazelcastInstance[] instances = factory.newInstances();

    final CountDownLatch mainLatch = new CountDownLatch(nodeCount);
    final AtomicInteger count1 = new AtomicInteger(0);
    final AtomicInteger count2 = new AtomicInteger(0);
    final AtomicInteger count3 = new AtomicInteger(0);

    for (int i = 0; i < nodeCount; i++) {
      final HazelcastInstance instance = instances[i];
      new Thread(
              new Runnable() {
                public void run() {
                  ITopic<Long> topic = instance.getTopic(randomName);
                  topic.addMessageListener(
                      new MessageListener<Long>() {
                        public void onMessage(Message<Long> message) {
                          Member publishingMember = message.getPublishingMember();
                          if (publishingMember.equals(instance.getCluster().getLocalMember()))
                            count1.incrementAndGet();
                          if (publishingMember.equals(message.getMessageObject()))
                            count2.incrementAndGet();
                          if (publishingMember.localMember()) count3.incrementAndGet();
                        }
                      });
                  mainLatch.countDown();
                }
              })
          .start();
    }
    try {
      mainLatch.await(1, TimeUnit.MINUTES);
    } catch (InterruptedException e) {
      fail();
    }

    for (int i = 0; i < nodeCount; i++) {
      HazelcastInstance instance = instances[i];
      instance.getTopic(randomName).publish(instance.getCluster().getLocalMember());
    }

    assertTrueEventually(
        new AssertTask() {
          @Override
          public void run() {
            assertEquals(nodeCount, count1.get());
            assertEquals(nodeCount * nodeCount, count2.get());
            assertEquals(nodeCount, count3.get());
          }
        });
  }
 @Test
 public void testSubmitMultipleNode() throws ExecutionException, InterruptedException {
   final int k = simpleTestNodeCount;
   TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(k);
   final HazelcastInstance[] instances = factory.newInstances(new Config());
   for (int i = 0; i < k; i++) {
     final IExecutorService service = instances[i].getExecutorService("testSubmitMultipleNode");
     final String script = "hazelcast.getAtomicLong('testSubmitMultipleNode').incrementAndGet();";
     final Future future = service.submit(new ScriptCallable(script, null));
     assertEquals((long) (i + 1), future.get());
   }
 }
Exemple #15
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);
    }
  }
Exemple #16
0
  @Test
  public void testMapRecordIdleEviction() throws InterruptedException {
    Config cfg = new Config();
    MapConfig mc = cfg.getMapConfig("testMapRecordIdleEviction");
    int maxIdleSeconds = 8;
    int size = 1000;
    final int nsize = size / 10;
    mc.setMaxIdleSeconds(maxIdleSeconds);
    TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(2);
    final HazelcastInstance[] instances = factory.newInstances(cfg);
    final IMap map = instances[0].getMap("testMapRecordIdleEviction");
    final CountDownLatch latch = new CountDownLatch(size - nsize);
    map.addEntryListener(
        new EntryAdapter() {
          public void entryEvicted(EntryEvent event) {
            latch.countDown();
          }
        },
        false);

    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(100);
                  } catch (HazelcastInstanceNotActiveException e) {
                    return;
                  } catch (InterruptedException e) {
                    return;
                  }
                }
              }
            });
    thread.start();

    for (int i = 0; i < size; i++) {
      map.put(i, i);
    }
    assertTrue(latch.await(1, TimeUnit.MINUTES));
    Assert.assertEquals(nsize, map.size());

    thread.interrupt();
    thread.join(5000);
  }
 @Test
 public void testPollNull() throws Exception {
   TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(2);
   final HazelcastInstance[] instances = factory.newInstances();
   final HazelcastInstance h1 = instances[0];
   final HazelcastInstance h2 = instances[1];
   final IQueue q1 = h1.getQueue("default");
   final IQueue q2 = h2.getQueue("default");
   for (int i = 0; i < 100; i++) {
     assertNull(q1.poll());
     assertNull(q2.poll());
   }
   assertNull(q1.poll(2, TimeUnit.SECONDS));
   assertNull(q2.poll(2, TimeUnit.SECONDS));
 }
 @Test
 public void testExecuteMultipleNode() throws InterruptedException, ExecutionException {
   final int k = simpleTestNodeCount;
   TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(k);
   final HazelcastInstance[] instances = factory.newInstances(new Config());
   for (int i = 0; i < k; i++) {
     final IExecutorService service = instances[i].getExecutorService("testExecuteMultipleNode");
     final String script = "hazelcast.getAtomicLong('count').incrementAndGet();";
     final int rand = new Random().nextInt(100);
     final Future<Integer> future = service.submit(new ScriptRunnable(script, null), rand);
     assertEquals(Integer.valueOf(rand), future.get());
   }
   final IAtomicLong count = instances[0].getAtomicLong("count");
   assertEquals(k, count.get());
 }
Exemple #19
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());
          }
        });
  }
 /** @throws Exception */
 @Test
 public void testQueueAfterShutdown2() throws Exception {
   TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(2);
   final HazelcastInstance[] instances = factory.newInstances();
   final HazelcastInstance h1 = instances[0];
   final HazelcastInstance h2 = instances[1];
   IQueue q1 = h1.getQueue("default");
   IQueue q2 = h2.getQueue("default");
   q1.offer("item");
   assertEquals(1, q1.size());
   assertEquals(1, q2.size());
   assertEquals("item", q2.take());
   assertEquals(0, q1.size());
   assertEquals(0, q2.size());
   h2.getLifecycleService().shutdown();
   assertEquals(0, q1.size());
 }
Exemple #21
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 testOffer() throws Exception {
   TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(2);
   final HazelcastInstance[] instances = factory.newInstances();
   HazelcastInstance h1 = instances[0];
   HazelcastInstance h2 = instances[1];
   final IQueue q1 = h1.getQueue("default");
   final IQueue q2 = h2.getQueue("default");
   for (int i = 0; i < 100; i++) {
     assertTrue(q1.offer("item" + i, 100, TimeUnit.SECONDS));
     assertTrue(q2.offer("item" + i, 100, TimeUnit.SECONDS));
   }
   assertEquals("item0", q1.peek());
   assertEquals("item0", q2.peek());
   for (int i = 0; i < 100; i++) {
     assertEquals("item" + i, q1.poll());
     assertEquals("item" + i, q2.poll());
   }
 }
Exemple #23
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();
    }
  }
Exemple #24
0
  @Test
  public void testMapPutTtlWithListener() throws InterruptedException {
    Config cfg = new Config();
    TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(2);
    final HazelcastInstance[] instances = factory.newInstances(cfg);
    warmUpPartitions(instances);

    final int k = 10;
    final int putCount = 10000;
    final CountDownLatch latch = new CountDownLatch(k * putCount);
    final IMap map = instances[0].getMap("testMapEvictionTtlWithListener");

    final AtomicBoolean error = new AtomicBoolean(false);
    final Set<Long> times = Collections.newSetFromMap(new ConcurrentHashMap<Long, Boolean>());

    map.addEntryListener(
        new EntryAdapter() {
          public void entryEvicted(final EntryEvent event) {
            final Long expectedEvictionTime = (Long) (event.getOldValue());
            long timeDifference = System.currentTimeMillis() - expectedEvictionTime;
            if (timeDifference > 5000) {
              error.set(true);
              times.add(timeDifference);
            }
            latch.countDown();
          }
        },
        true);

    for (int i = 0; i < k; i++) {
      final int threadId = i;
      int ttl = (int) (Math.random() * 5000 + 3000);
      for (int j = 0; j < putCount; j++) {
        final long expectedEvictionTime = ttl + System.currentTimeMillis();
        map.put(j + putCount * threadId, expectedEvictionTime, ttl, TimeUnit.MILLISECONDS);
      }
    }

    assertTrue(latch.await(1, TimeUnit.MINUTES));
    assertFalse("Some evictions took more than 3 seconds! -> " + times, error.get());
  }
  @Test
  public void testShutdown() throws Exception {
    TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(2);
    final HazelcastInstance[] instances = factory.newInstances();
    final HazelcastInstance h1 = instances[0];
    final HazelcastInstance h2 = instances[1];
    warmUpPartitions(h2, h1);

    final IQueue q1 = h1.getQueue("default");
    final IQueue q2 = h2.getQueue("default");
    for (int i = 0; i < 40; i++) {
      assertTrue(q1.offer("item" + i, 100, TimeUnit.SECONDS));
    }
    h1.getLifecycleService().shutdown();
    for (int i = 40; i < 100; i++) {
      assertTrue(q2.offer("item" + i, 100, TimeUnit.SECONDS));
    }
    for (int i = 0; i < 100; i++) {
      assertEquals("item" + i, q2.poll());
    }
  }
Exemple #26
0
  /** Testing if topic can properly listen messages and if topic has any issue after a shutdown. */
  @Test
  public void testTopicCluster() throws InterruptedException {
    String topicName = "TestMessages" + generateRandomString(5);
    Config cfg = new Config();

    TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(2);
    HazelcastInstance[] instances = factory.newInstances(cfg);
    HazelcastInstance instance1 = instances[0];
    HazelcastInstance instance2 = instances[1];

    ITopic<String> topic1 = instance1.getTopic(topicName);
    final CountDownLatch latch1 = new CountDownLatch(1);
    final String message = "Test" + randomString();

    topic1.addMessageListener(
        new MessageListener<String>() {
          public void onMessage(Message msg) {
            assertEquals(message, msg.getMessageObject());
            latch1.countDown();
          }
        });

    ITopic<String> topic2 = instance2.getTopic(topicName);
    final CountDownLatch latch2 = new CountDownLatch(2);
    topic2.addMessageListener(
        new MessageListener<String>() {
          public void onMessage(Message msg) {
            assertEquals(message, msg.getMessageObject());
            latch2.countDown();
          }
        });

    topic1.publish(message);
    assertTrue(latch1.await(5, TimeUnit.SECONDS));

    instance1.shutdown();
    topic2.publish(message);
    assertTrue(latch2.await(5, TimeUnit.SECONDS));
  }
  @Test
  public void testSubmitToMembersRunnable() throws InterruptedException {
    final int k = simpleTestNodeCount;
    TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(k);
    final HazelcastInstance[] instances = factory.newInstances(new Config());
    final AtomicInteger count = new AtomicInteger(0);
    final CountDownLatch latch = new CountDownLatch(k);
    final MultiExecutionCallback callback =
        new MultiExecutionCallback() {
          public void onResponse(Member member, Object value) {
            count.incrementAndGet();
          }

          public void onComplete(Map<Member, Object> values) {
            latch.countDown();
          }
        };
    int sum = 0;
    final Set<Member> membersSet = instances[0].getCluster().getMembers();
    final Member[] members = membersSet.toArray(new Member[membersSet.size()]);
    final Random random = new Random();
    for (int i = 0; i < k; i++) {
      final IExecutorService service =
          instances[i].getExecutorService("testSubmitToMembersRunnable");
      final String script =
          "hazelcast.getAtomicLong('testSubmitToMembersRunnable').incrementAndGet();";
      final int n = random.nextInt(k) + 1;
      sum += n;
      Member[] m = new Member[n];
      System.arraycopy(members, 0, m, 0, n);
      service.submitToMembers(new ScriptRunnable(script, null), Arrays.asList(m), callback);
    }

    assertTrue(latch.await(30, TimeUnit.SECONDS));
    final IAtomicLong result = instances[0].getAtomicLong("testSubmitToMembersRunnable");
    assertEquals(sum, result.get());
    assertEquals(sum, count.get());
  }
Exemple #28
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();
    }
  }
  @Test
  public void testSenderAndBackupTerminates_AfterInitialLoad() throws InterruptedException {
    String name = randomString();
    Config config = new Config();
    MapConfig mapConfig = config.getMapConfig(name);
    MapStoreConfig mapStoreConfig = new MapStoreConfig();
    mapStoreConfig.setEnabled(true);
    mapStoreConfig.setImplementation(new DummyMapLoader());
    mapStoreConfig.setInitialLoadMode(MapStoreConfig.InitialLoadMode.EAGER);
    mapConfig.setMapStoreConfig(mapStoreConfig);
    TestHazelcastInstanceFactory instanceFactory = createHazelcastInstanceFactory(5);
    HazelcastInstance[] instances = instanceFactory.newInstances(config);

    IMap<Object, Object> map = instances[0].getMap(name);
    map.clear();

    HazelcastInstance[] ownerAndReplicas = findOwnerAndReplicas(instances, name);
    ownerAndReplicas[0].getLifecycleService().terminate();
    ownerAndReplicas[1].getLifecycleService().terminate();

    map = ownerAndReplicas[3].getMap(name);
    map.loadAll(false);
    assertEquals(DummyMapLoader.SIZE, map.size());
  }
  @Test
  @Ignore
  public void testQueryWithIndexesWhileMigrating() throws Exception {

    HazelcastInstance h1 = nodeFactory.newHazelcastInstance();
    IMap imap = h1.getMap("employees");
    imap.addIndex("age", true);
    imap.addIndex("active", false);

    for (int i = 0; i < 500; i++) {
      imap.put("e" + i, new Employee("name" + i, i % 50, ((i & 1) == 1), (double) i));
    }
    assertEquals(500, imap.size());
    Set<Map.Entry> entries = imap.entrySet(new SqlPredicate("active=true and age>44"));
    assertEquals(30, entries.size());

    nodeFactory.newInstances(new Config(), 3);

    long startNow = Clock.currentTimeMillis();
    while ((Clock.currentTimeMillis() - startNow) < 10000) {
      entries = imap.entrySet(new SqlPredicate("active=true and age>44"));
      assertEquals(30, entries.size());
    }
  }