@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);
  }
  @Test(timeout = 300000)
  public void testTwoMemberWriteThrough2() throws Exception {
    int items = 1000;
    TestMapStore testMapStore = new TestMapStore(items, 0, 0);
    Config config = newConfig(testMapStore, 0);
    TestHazelcastInstanceFactory nodeFactory = createHazelcastInstanceFactory(3);
    HazelcastInstance h1 = nodeFactory.newHazelcastInstance(config);
    HazelcastInstance h2 = nodeFactory.newHazelcastInstance(config);

    IMap map1 = h1.getMap("default");
    IMap map2 = h2.getMap("default");

    for (int i = 0; i < items; i++) {
      map1.put(i, "value" + i);
    }

    assertTrue(
        "store operations could not be done wisely ",
        testMapStore.latchStore.await(30, TimeUnit.SECONDS));
    assertEquals(items, testMapStore.getStore().size());
    assertEquals(items, map1.size());
    assertEquals(items, map2.size());

    testMapStore.assertAwait(10);
    // N put-load N put-store call and 1 loadAllKeys
    assertEquals(items * 2 + 1, testMapStore.callCount.get());
  }
  @Test
  public void testRemovedEntry_shouldNotBeReached_afterMigration() throws Exception {
    String mapName = randomMapName();
    TestHazelcastInstanceFactory factory = new TestHazelcastInstanceFactory(2);
    MapStoreTest.SimpleMapStore<Integer, Integer> store =
        new MapStoreTest.SimpleMapStore<Integer, Integer>();
    store.store.put(1, 0);

    Config config = createConfig(mapName, store);
    HazelcastInstance node1 = factory.newHazelcastInstance(config);

    IMap<Integer, Integer> map = node1.getMap(mapName);

    map.put(1, 1);
    map.delete(1);

    HazelcastInstance node2 = factory.newHazelcastInstance(config);

    map = node2.getMap(mapName);

    Integer value = map.get(1);
    factory.shutdownAll();

    assertNull(value);
  }
Exemplo n.º 4
0
 @Test
 public void testWANClusteringActivePassive() throws Exception {
   Config c1 = new Config();
   Config c2 = new Config();
   c1.getGroupConfig().setName("newyork");
   c1.addWanReplicationConfig(
       new WanReplicationConfig()
           .setName("my-wan")
           .addTargetClusterConfig(
               new WanTargetClusterConfig().addEndpoint("127.0.0.1:5702").setGroupName("london")));
   c1.getMapConfig("default")
       .setWanReplicationRef(
           new WanReplicationRef().setName("my-wan").setMergePolicy(PassThroughMergePolicy.NAME));
   c2.getGroupConfig().setName("london");
   c2.getMapConfig("default")
       .setWanReplicationRef(
           new WanReplicationRef().setName("my-wan").setMergePolicy(PassThroughMergePolicy.NAME));
   HazelcastInstance h10 = Hazelcast.newHazelcastInstance(c1);
   HazelcastInstance h20 = Hazelcast.newHazelcastInstance(c2);
   int size = 1000;
   MergeLatch mergeLatch2 = new MergeLatch(size);
   getConcurrentMapManager(h20).addWanMergeListener(mergeLatch2);
   for (int i = 0; i < size; i++) {
     h10.getMap("default").put(i, "value" + i);
   }
   assertTrue("Latch state: " + mergeLatch2, mergeLatch2.await(60, TimeUnit.SECONDS));
   Thread.sleep(1000);
   assertEquals(size, mergeLatch2.totalOperations());
   assertEquals(size, h10.getMap("default").size());
   assertEquals(size, h20.getMap("default").size());
   for (int i = 0; i < size; i++) {
     assertEquals("value" + i, h20.getMap("default").get(i));
   }
 }
Exemplo n.º 5
0
 @Test
 public void testNearCacheEvictionByUsingMapClear() throws InterruptedException {
   final Config cfg = new Config();
   final String mapName = "testNearCacheEviction";
   final NearCacheConfig nearCacheConfig = new NearCacheConfig();
   nearCacheConfig.setInvalidateOnChange(true);
   cfg.getMapConfig(mapName).setNearCacheConfig(nearCacheConfig);
   final TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(2);
   final HazelcastInstance hazelcastInstance1 = factory.newHazelcastInstance(cfg);
   final HazelcastInstance hazelcastInstance2 = factory.newHazelcastInstance(cfg);
   final IMap map1 = hazelcastInstance1.getMap(mapName);
   final IMap map2 = hazelcastInstance2.getMap(mapName);
   final int size = 10;
   // populate map.
   for (int i = 0; i < size; i++) {
     map1.put(i, i);
   }
   // populate near cache
   for (int i = 0; i < size; i++) {
     map1.get(i);
     map2.get(i);
   }
   // clear map should trigger near cache eviction.
   map1.clear();
   for (int i = 0; i < size; i++) {
     assertNull(map1.get(i));
   }
 }
Exemplo n.º 6
0
  // ignored due to: https://github.com/hazelcast/hazelcast/issues/5035
  @Ignore
  @Test
  public void testMapLoaderLoadUpdatingIndex() throws Exception {
    final int nodeCount = 3;
    String mapName = randomString();
    SampleIndexableObjectMapLoader loader = new SampleIndexableObjectMapLoader();

    Config config = createMapConfig(mapName, loader);

    NodeBuilder nodeBuilder = new NodeBuilder(nodeCount, config).build();
    HazelcastInstance node = nodeBuilder.getRandomNode();

    IMap<Integer, SampleIndexableObject> map = node.getMap(mapName);
    for (int i = 0; i < 10; i++) {
      map.put(i, new SampleIndexableObject("My-" + i, i));
    }

    final SqlPredicate predicate = new SqlPredicate("name='My-5'");
    assertPredicateResultCorrect(map, predicate);

    map.destroy();
    loader.preloadValues = true;

    node = nodeBuilder.getRandomNode();
    map = node.getMap(mapName);

    assertLoadAllKeysCount(loader, 1);
    assertPredicateResultCorrect(map, predicate);
  }
  @Test
  public void testHigherBatchSize_shouldNotCauseAnyInvalidation_onClient() throws Exception {
    Config config = getConfig();
    configureBatching(config, true, Integer.MAX_VALUE, Integer.MAX_VALUE);
    HazelcastInstance server = factory.newHazelcastInstance(config);

    factory.newHazelcastInstance(config);

    ClientConfig clientConfig = newClientConfig(mapName);

    HazelcastInstance client = factory.newHazelcastClient(clientConfig);

    IMap<Integer, Integer> serverMap = server.getMap(mapName);
    IMap<Integer, Integer> clientMap = client.getMap(mapName);

    int size = 1000;

    // fill serverMap
    for (int i = 0; i < size; i++) {
      serverMap.put(i, i);
    }

    // fill near-cache on client
    for (int i = 0; i < size; i++) {
      clientMap.get(i);
    }

    // generate invalidation data.
    for (int i = 0; i < size; i++) {
      serverMap.put(i, i);
    }

    assertNearCacheSizeEventually(clientMap, size);
  }
  @Test
  public void testBatchInvalidationRemovesEntries() throws Exception {
    Config config = getConfig();
    configureBatching(config, true, 10, 1);
    HazelcastInstance server = factory.newHazelcastInstance(config);
    factory.newHazelcastInstance(config);

    ClientConfig clientConfig = newClientConfig(mapName);

    HazelcastInstance client = factory.newHazelcastClient(clientConfig);

    IMap<Integer, Integer> serverMap = server.getMap(mapName);
    IMap<Integer, Integer> clientMap = client.getMap(mapName);

    int size = 1000;

    // fill serverMap
    for (int i = 0; i < size; i++) {
      serverMap.put(i, i);
    }

    // fill near-cache on client
    for (int i = 0; i < size; i++) {
      clientMap.get(i);
    }

    // generate invalidation data.
    for (int i = 0; i < size; i++) {
      serverMap.put(i, i);
    }

    assertNearCacheSizeEventually(clientMap, 0);
  }
  @Test
  public void testMapEvictAll_shouldClearNearCaches_onOwnerAndBackupNodes() throws Exception {
    Config config = getConfig();
    configureBatching(config, true, 10, 1);
    HazelcastInstance server = factory.newHazelcastInstance(config);

    factory.newHazelcastInstance(config);

    ClientConfig clientConfig = newClientConfig(mapName);

    HazelcastInstance client = factory.newHazelcastClient(clientConfig);

    IMap<Integer, Integer> serverMap = server.getMap(mapName);
    IMap<Integer, Integer> clientMap = client.getMap(mapName);

    int size = 1000;

    // fill serverMap
    for (int i = 0; i < size; i++) {
      serverMap.put(i, i);
    }

    // fill near-cache on client
    for (int i = 0; i < size; i++) {
      clientMap.get(i);
    }

    serverMap.evictAll();

    assertNearCacheSizeEventually(clientMap, 0);
  }
Exemplo n.º 10
0
  @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);
  }
Exemplo n.º 11
0
  @Test
  public void equalsReadLocalBackup() {
    TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(2);

    Config config = new Config();
    config.addMapConfig(
        new MapConfig("objectMap")
            .setInMemoryFormat(InMemoryFormat.OBJECT)
            .setReadBackupData(true));

    HazelcastInstance hz1 = factory.newHazelcastInstance(config);
    HazelcastInstance hz2 = factory.newHazelcastInstance(config);

    Pair pair = new Pair("a", "1");

    IMap<String, Pair> objectMap1 = hz1.getMap("objectMap");
    IMap<String, Pair> objectMap2 = hz2.getMap("objectMap");

    objectMap1.put("1", pair);
    Pair v1 = objectMap1.get("1");
    Pair v2 = objectMap1.get("1");

    Pair rv1 = objectMap2.get("1");
    Pair rv2 = objectMap2.get("1");
    assertNotSame(pair, v1);
    assertNotSame(pair, v2);
    assertNotSame(v1, v2);

    assertNotSame(pair, rv1);
    assertNotSame(pair, rv2);
    assertNotSame(rv1, rv2);

    assertTrue(objectMap2.containsValue(v1));
  }
Exemplo n.º 12
0
  @Test
  public void testIndexDoesNotReturnStaleResultsAfterSplit() {
    String mapName = randomMapName();
    Config config = newConfig(LatestUpdateMapMergePolicy.class.getName(), mapName);
    HazelcastInstance h1 = Hazelcast.newHazelcastInstance(config);
    HazelcastInstance h2 = Hazelcast.newHazelcastInstance(config);

    TestMembershipListener membershipListener = new TestMembershipListener(1);
    h2.getCluster().addMembershipListener(membershipListener);
    TestLifecycleListener lifecycleListener = new TestLifecycleListener(1);
    h2.getLifecycleService().addLifecycleListener(lifecycleListener);

    RealtimeCall call = new RealtimeCall();
    String key = generateKeyOwnedBy(h1);
    call.setId(key);
    call.setClusterUUID(key);

    IMap<PartitionAwareKey<String, String>, RealtimeCall> map1 = h1.getMap(mapName);
    IMap<PartitionAwareKey<String, String>, RealtimeCall> map2 = h2.getMap(mapName);

    map1.put(call.getAffinityKey(), call);

    sleepMillis(1);
    assertNotNull("entry should be in map2 before split", map2.get(call.getAffinityKey()));

    closeConnectionBetween(h1, h2);

    assertOpenEventually(membershipListener.latch);
    assertClusterSizeEventually(1, h1);
    assertClusterSizeEventually(1, h2);

    map1 = h1.getMap(mapName);
    map1.remove(call.getAffinityKey());

    sleepMillis(1);
    map2 = h2.getMap(mapName);
    assertNotNull("entry should be in map2 in split", map2.get(call.getAffinityKey()));

    assertOpenEventually(lifecycleListener.latch);
    assertClusterSizeEventually(2, h1);
    assertClusterSizeEventually(2, h2);

    map1 = h1.getMap(mapName);
    assertNotNull("entry should be in map1", map1.get(call.getAffinityKey()));

    map1.remove(call.getAffinityKey());
    assertNull("map1 should be null", map1.get(call.getAffinityKey()));
    assertNull("map2 should be null", map2.get(call.getAffinityKey()));

    for (int i = 0; i < 100; i++) {
      Collection<RealtimeCall> calls = map1.values(Predicates.equal("id", call.getId()));
      System.out.println("Map 1 query by uuid: " + calls.size());
      assert calls.size() == 0;
      calls = map2.values(Predicates.equal("id", call.getId()));
      System.out.println("Map 2 query by uuid: " + calls.size());
      assert calls.size() == 0;
      sleepMillis(5);
    }
  }
  /* github issue #183 */
  @Test
  public void testKeyBasedListeners() throws InterruptedException {
    try {
      Config config = new Config();
      HazelcastInstance instance = Hazelcast.newHazelcastInstance(config);
      IMap<String, String> map = instance.getMap("map");
      map.put("key1", "value1");
      map.put("key2", "value2");
      map.put("key3", "value3");

      ClientConfig clientConfig = new ClientConfig();
      HazelcastInstance client = HazelcastClient.newHazelcastClient(clientConfig);

      final AtomicInteger count = new AtomicInteger(0);
      IMap<String, String> clientMap = client.getMap("map");

      clientMap.addEntryListener(
          new EntryListener<String, String>() {
            public void entryAdded(EntryEvent<String, String> entryEvent) {
              count.incrementAndGet();
            }

            public void entryRemoved(EntryEvent<String, String> entryEvent) {}

            public void entryUpdated(EntryEvent<String, String> entryEvent) {
              count.incrementAndGet();
            }

            public void entryEvicted(EntryEvent<String, String> entryEvent) {}
          },
          "key1",
          true);

      clientMap.addEntryListener(
          new EntryListener<String, String>() {
            public void entryAdded(EntryEvent<String, String> entryEvent) {
              count.incrementAndGet();
            }

            public void entryRemoved(EntryEvent<String, String> entryEvent) {}

            public void entryUpdated(EntryEvent<String, String> entryEvent) {
              System.out.println("event map");
              count.incrementAndGet();
            }

            public void entryEvicted(EntryEvent<String, String> entryEvent) {}
          },
          "key2",
          true);

      map.put("key1", "new-value1");
      Thread.sleep(100);
      Assert.assertEquals(count.get(), 1);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
  }
Exemplo n.º 14
0
  private void start() throws IOException {

    if (logPath != null) {
      PropertyConfigurator.configure(logPath.getAbsolutePath());
    }

    if (!hazelcastFile.isFile()) {
      throw new RuntimeException("Could not find hazelcast configuration file : " + hazelcastFile);
    }

    /*
     * Start a lightweight http server, make sure it can only be accessed
     * from the machine it's running on (don't want to allow remote
     * shutdown).
     */
    server = HttpServer.create(new InetSocketAddress(host, port), port);
    server.createContext(baseContext + HazelcastServiceCommand.stop, new ShutdownHandler());
    server.createContext(baseContext + HazelcastServiceCommand.status, new StatusHandler());
    server.setExecutor(null);
    server.start();

    // Config config = new Config();
    // config.setConfigurationFile(hazelcastFile);
    // config.setConfigurationUrl(hazelcastFile.toURI().toURL());
    // hz = Hazelcast.newHazelcastInstance(config);

    /*
     * Creating a Config object (as commented out above) and setting the
     * file using setConfigurationFile doesn't seem to work at all. The
     * system property approach does.
     */
    String path = hazelcastFile.getAbsolutePath();
    logger.info("Setting configuration file " + path);
    System.setProperty("hazelcast.config", path);

    hz = Hazelcast.getDefaultInstance();

    logger.info(hz.getConfig().getConfigurationFile());
    logger.info(hz.getConfig().getGroupConfig().getName());

    monitor = new HazelcastMonitor();
    hz.addInstanceListener(monitor);

    if (testValues) {
      hz.getMap("tst").put("1", "1");
      hz.getMap("tst").put("2", "2");
      hz.getMap("tst").put("3", "3");

      logger.info(hz.getMap("tst").getId());
      logger.info(hz.getMap("tst").size());
    }

    System.out.println("-- HAZELCAST SERVICE WRAPPER READY --");
    System.out.println(
        String.format("To check status, request: '%s'.", url(HazelcastServiceCommand.status)));
    System.out.println(
        String.format("To shut it down, request: '%s'.", url(HazelcastServiceCommand.stop)));
  }
Exemplo n.º 15
0
  public static void main(String[] args) throws Exception {
    HazelcastInstance hz = Hazelcast.newHazelcastInstance();
    Person person = new Person("Joe");

    IMap<String, String> hzSimpleMap = hz.getMap("simple");
    hzSimpleMap.set("key1", "value1");

    IMap<String, Person> hzObjectMap = hz.getMap("object");
    hzObjectMap.set("key1", person);
  }
  @Test
  public void testEntity() {
    final HazelcastInstance hz = getHazelcastInstance(sf);
    assertNotNull(hz);
    final int count = 100;
    final int childCount = 3;
    insertDummyEntities(count, childCount);
    List<DummyEntity> list = new ArrayList<DummyEntity>(count);
    Session session = sf.openSession();
    try {
      for (int i = 0; i < count; i++) {
        DummyEntity e = (DummyEntity) session.get(DummyEntity.class, (long) i);
        session.evict(e);
        list.add(e);
      }
    } finally {
      session.close();
    }
    session = sf.openSession();
    Transaction tx = session.beginTransaction();
    try {
      for (DummyEntity dummy : list) {
        dummy.setDate(new Date());
        session.update(dummy);
      }
      tx.commit();
    } catch (Exception e) {
      tx.rollback();
      e.printStackTrace();
    } finally {
      session.close();
    }

    Statistics stats = sf.getStatistics();
    Map<?, ?> cache = hz.getMap(DummyEntity.class.getName());
    Map<?, ?> propCache = hz.getMap(DummyProperty.class.getName());
    Map<?, ?> propCollCache = hz.getMap(DummyEntity.class.getName() + ".properties");
    assertEquals((childCount + 1) * count, stats.getEntityInsertCount());
    // twice put of entity and properties (on load and update) and once put of collection
    // TODO: fix next assertion ->
    //        assertEquals((childCount + 1) * count * 2, stats.getSecondLevelCachePutCount());
    assertEquals(childCount * count, stats.getEntityLoadCount());
    assertEquals(count, stats.getSecondLevelCacheHitCount());
    // collection cache miss
    assertEquals(count, stats.getSecondLevelCacheMissCount());
    assertEquals(count, cache.size());
    assertEquals(count * childCount, propCache.size());
    assertEquals(count, propCollCache.size());
    sf.getCache().evictEntityRegion(DummyEntity.class);
    sf.getCache().evictEntityRegion(DummyProperty.class);
    assertEquals(0, cache.size());
    assertEquals(0, propCache.size());
    stats.logSummary();
  }
Exemplo n.º 17
0
 @Test
 public void testNearCacheEvictionByUsingMapTTLEviction() throws InterruptedException {
   final int instanceCount = 3;
   final int ttl = 1;
   final int size = 1000;
   final Config cfg = new Config();
   final String mapName = "_testNearCacheEvictionByUsingMapTTLEviction_";
   final NearCacheConfig nearCacheConfig = new NearCacheConfig();
   nearCacheConfig.setInvalidateOnChange(true);
   nearCacheConfig.setInMemoryFormat(InMemoryFormat.OBJECT);
   cfg.getMapConfig(mapName).setNearCacheConfig(nearCacheConfig);
   final MapConfig mapConfig = cfg.getMapConfig(mapName);
   mapConfig.setTimeToLiveSeconds(ttl);
   final TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(instanceCount);
   final HazelcastInstance instance1 = factory.newHazelcastInstance(cfg);
   final HazelcastInstance instance2 = factory.newHazelcastInstance(cfg);
   final HazelcastInstance instance3 = factory.newHazelcastInstance(cfg);
   final IMap map1 = instance1.getMap(mapName);
   final IMap map2 = instance2.getMap(mapName);
   final IMap map3 = instance3.getMap(mapName);
   // observe eviction
   final CountDownLatch latch = new CountDownLatch(size);
   map1.addEntryListener(
       new EntryAdapter() {
         public void entryEvicted(EntryEvent event) {
           latch.countDown();
         }
       },
       false);
   // populate map
   for (int i = 0; i < size; i++) {
     // populate.
     map1.put(i, i);
     // bring near caches. -- here is a time window
     // that "i" already evicted. so a "get" brings
     // a NULL object to the near cache.
     map1.get(i);
     map2.get(i);
     map3.get(i);
   }
   // wait operations to complete
   assertOpenEventually(latch);
   // check map size after eviction.
   assertEquals(0, map1.size());
   assertEquals(0, map2.size());
   assertEquals(0, map3.size());
   // near cache sizes should be zero after eviction.
   assertEquals(0, countNotNullValuesInNearCache(mapName, instance1));
   assertEquals(0, countNotNullValuesInNearCache(mapName, instance2));
   assertEquals(0, countNotNullValuesInNearCache(mapName, instance3));
 }
Exemplo n.º 18
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);
    }
  }
Exemplo n.º 19
0
  @Test(timeout = 120000)
  public void testGetAllKeys() throws Exception {
    TestEventBasedMapStore testMapStore = new TestEventBasedMapStore();
    Map store = testMapStore.getStore();
    Set keys = new HashSet();
    int size = 1000;
    for (int i = 0; i < size; i++) {
      store.put(i, "value" + i);
      keys.add(i);
    }
    Config config = newConfig(testMapStore, 2);

    TestHazelcastInstanceFactory nodeFactory = createHazelcastInstanceFactory(3);
    HazelcastInstance h1 = nodeFactory.newHazelcastInstance(config);
    HazelcastInstance h2 = nodeFactory.newHazelcastInstance(config);

    IMap map1 = h1.getMap("default");
    IMap map2 = h2.getMap("default");

    // checkIfMapLoaded("default", h1);
    // checkIfMapLoaded("default", h2);

    assertEquals("value1", map1.get(1));
    assertEquals("value1", map2.get(1));

    assertEquals(1000, map1.size());
    assertEquals(1000, map2.size());

    HazelcastInstance h3 = nodeFactory.newHazelcastInstance(config);
    IMap map3 = h3.getMap("default");
    // checkIfMapLoaded("default", h3);

    assertEquals("value1", map1.get(1));
    assertEquals("value1", map2.get(1));
    assertEquals("value1", map3.get(1));

    assertEquals(1000, map1.size());
    assertEquals(1000, map2.size());
    assertEquals(1000, map3.size());

    h3.shutdown();

    assertEquals("value1", map1.get(1));
    assertEquals("value1", map2.get(1));

    assertEquals(1000, map1.size());
    assertEquals(1000, map2.size());
  }
Exemplo n.º 20
0
  @Test
  public void testOneQuorumsFailsOneQuorumSuccessForDifferentMaps() throws Exception {
    TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(3);
    String fourNodeQuorum = randomString();
    QuorumConfig fourNodeQuorumConfig = new QuorumConfig(fourNodeQuorum, true);
    fourNodeQuorumConfig.setQuorumFunctionImplementation(
        new QuorumFunction() {
          @Override
          public boolean apply(Collection<Member> members) {
            return members.size() == 4;
          }
        });

    String threeNodeQuorum = randomString();
    QuorumConfig threeNodeQuorumConfig = new QuorumConfig(threeNodeQuorum, true);
    threeNodeQuorumConfig.setQuorumFunctionImplementation(
        new QuorumFunction() {
          @Override
          public boolean apply(Collection<Member> members) {
            return members.size() == 3;
          }
        });

    MapConfig fourNodeMapConfig = new MapConfig("fourNode");
    fourNodeMapConfig.setQuorumName(fourNodeQuorum);

    MapConfig threeNodeMapConfig = new MapConfig("threeNode");
    threeNodeMapConfig.setQuorumName(threeNodeQuorum);

    Config config = new Config();
    config.addQuorumConfig(threeNodeQuorumConfig);
    config.addQuorumConfig(fourNodeQuorumConfig);
    config.addMapConfig(fourNodeMapConfig);
    config.addMapConfig(threeNodeMapConfig);

    HazelcastInstance h1 = factory.newHazelcastInstance(config);
    HazelcastInstance h2 = factory.newHazelcastInstance(config);
    HazelcastInstance h3 = factory.newHazelcastInstance(config);

    IMap<Object, Object> fourNode = h1.getMap("fourNode");
    IMap<Object, Object> threeNode = h1.getMap("threeNode");
    threeNode.put(generateKeyOwnedBy(h1), "bar");
    try {
      fourNode.put(generateKeyOwnedBy(h1), "bar");
      fail();
    } catch (Exception e) {
    }
  }
Exemplo n.º 21
0
  @Test(timeout = 120000)
  public void testInitialLoadModeEagerMultipleThread() {
    final String mapName = "default";
    final int instanceCount = 2;
    final int size = 10000;
    final TestHazelcastInstanceFactory nodeFactory = createHazelcastInstanceFactory(instanceCount);
    final CountDownLatch countDownLatch = new CountDownLatch(instanceCount - 1);
    final Config config = getConfig();
    GroupConfig groupConfig = new GroupConfig("testEager");
    config.setGroupConfig(groupConfig);
    MapStoreConfig mapStoreConfig = new MapStoreConfig();
    mapStoreConfig.setEnabled(true);
    mapStoreConfig.setImplementation(new SimpleMapLoader(size, true));
    mapStoreConfig.setInitialLoadMode(MapStoreConfig.InitialLoadMode.EAGER);
    config.getMapConfig(mapName).setMapStoreConfig(mapStoreConfig);

    HazelcastInstance instance1 = nodeFactory.newHazelcastInstance(config);
    Runnable runnable =
        new Runnable() {
          public void run() {
            HazelcastInstance instance2 = nodeFactory.newHazelcastInstance(config);
            final IMap<Object, Object> map = instance2.getMap(mapName);
            assertEquals(size, map.size());
            countDownLatch.countDown();
          }
        };
    new Thread(runnable).start();

    assertOpenEventually(countDownLatch, 120);
    IMap map = instance1.getMap(mapName);
    assertEquals(size, map.size());
  }
Exemplo n.º 22
0
  @Test(timeout = 120000)
  public void testMapStoreWriteRemoveOrder() {
    final String mapName = randomMapName("testMapStoreWriteDeleteOrder");
    final int numIterations = 10;
    final int writeDelaySeconds = 3;
    // create map store implementation
    final RecordingMapStore store = new RecordingMapStore(0, 1);
    // create hazelcast config
    final Config config = newConfig(mapName, store, writeDelaySeconds);
    // start hazelcast instance
    final HazelcastInstance hzInstance = createHazelcastInstance(config);
    // loop over num iterations
    final IMap<String, String> map = hzInstance.getMap(mapName);
    for (int k = 0; k < numIterations; k++) {
      String key = String.valueOf(k + 10); // 2 digits for sorting in output
      String value = "v:" + key;
      // add entry
      map.put(key, value);
      // sleep 300ms
      sleepMillis(1);
      // remove entry
      map.remove(key);
    }
    // wait for store to finish
    store.awaitStores();
    // wait for remove to finish
    store.awaitRemoves();

    assertEquals(0, store.getStore().size());
  }
Exemplo n.º 23
0
  @Test(timeout = 120000)
  public void testMapStoreNotCalledFromEntryProcessorBackup() throws Exception {
    final String mapName = "testMapStoreNotCalledFromEntryProcessorBackup_" + randomString();
    final int instanceCount = 2;
    Config config = getConfig();
    // Configure map with one backup and dummy map store
    MapConfig mapConfig = config.getMapConfig(mapName);
    mapConfig.setBackupCount(1);
    MapStoreConfig mapStoreConfig = new MapStoreConfig();
    MapStoreWithStoreCount mapStore = new MapStoreWithStoreCount(1, 120);
    mapStoreConfig.setImplementation(mapStore);
    mapConfig.setMapStoreConfig(mapStoreConfig);

    TestHazelcastInstanceFactory nodeFactory = createHazelcastInstanceFactory(instanceCount);
    HazelcastInstance instance1 = nodeFactory.newHazelcastInstance(config);
    HazelcastInstance instance2 = nodeFactory.newHazelcastInstance(config);

    final IMap<String, String> map = instance1.getMap(mapName);
    final String key = "key";
    final String value = "value";
    // executeOnKey
    map.executeOnKey(key, new ValueSetterEntryProcessor(value));
    mapStore.awaitStores();

    assertEquals(value, map.get(key));
    assertEquals(1, mapStore.getCount());
  }
Exemplo n.º 24
0
  @Test(timeout = 120000)
  public void testIssue1142ExceptionWhenLoadAllReturnsNull() {
    Config config = getConfig();
    String mapname = "testIssue1142ExceptionWhenLoadAllReturnsNull";
    MapStoreConfig mapStoreConfig = new MapStoreConfig();
    mapStoreConfig.setImplementation(
        new MapStoreAdapter<String, String>() {
          @Override
          public Set<String> loadAllKeys() {
            Set keys = new HashSet();
            keys.add("key");
            return keys;
          }

          public Map loadAll(Collection keys) {
            return null;
          }
        });
    config.getMapConfig(mapname).setMapStoreConfig(mapStoreConfig);
    TestHazelcastInstanceFactory nodeFactory = createHazelcastInstanceFactory(2);
    HazelcastInstance instance = nodeFactory.newHazelcastInstance(config);
    HazelcastInstance instance2 = nodeFactory.newHazelcastInstance(config);
    final IMap map = instance.getMap(mapname);
    for (int i = 0; i < 300; i++) {
      map.put(i, i);
    }
    assertEquals(300, map.size());
  }
Exemplo n.º 25
0
  @Test(timeout = 120000)
  public void testIssue991EvictedNullIssue() throws InterruptedException {
    MapStoreConfig mapStoreConfig = new MapStoreConfig();
    mapStoreConfig.setEnabled(true);
    mapStoreConfig.setImplementation(
        new MapLoader<String, String>() {
          @Override
          public String load(String key) {
            return null;
          }

          @Override
          public Map<String, String> loadAll(Collection<String> keys) {
            return null;
          }

          @Override
          public Set<String> loadAllKeys() {
            return null;
          }
        });
    Config config = getConfig();
    config.getMapConfig("testIssue991EvictedNullIssue").setMapStoreConfig(mapStoreConfig);
    HazelcastInstance hc = createHazelcastInstance(config);
    IMap<Object, Object> map = hc.getMap("testIssue991EvictedNullIssue");
    map.get("key");
    assertNull(map.get("key"));
    map.put("key", "value");
    Thread.sleep(2000);
    assertEquals("value", map.get("key"));
  }
Exemplo n.º 26
0
  @Test(timeout = 120000)
  public void testOneMemberFlush() throws Exception {
    TestMapStore testMapStore = new TestMapStore(1, 1, 1);
    testMapStore.setLoadAllKeys(false);
    int size = 100;
    Config config = newConfig(testMapStore, 200);
    TestHazelcastInstanceFactory nodeFactory = createHazelcastInstanceFactory(3);
    HazelcastInstance h1 = nodeFactory.newHazelcastInstance(config);
    IMap map = h1.getMap("default");
    assertEquals(0, map.size());
    for (int i = 0; i < size; i++) {
      map.put(i, i);
    }
    assertEquals(size, map.size());
    assertEquals(0, testMapStore.getStore().size());
    assertEquals(size, map.getLocalMapStats().getDirtyEntryCount());
    map.flush();
    assertEquals(size, testMapStore.getStore().size());
    assertEquals(0, map.getLocalMapStats().getDirtyEntryCount());
    assertEquals(size, map.size());

    for (int i = 0; i < size / 2; i++) {
      map.remove(i);
    }
    assertEquals(size / 2, map.size());
    assertEquals(size, testMapStore.getStore().size());
    map.flush();
    assertEquals(size / 2, testMapStore.getStore().size());
    assertEquals(size / 2, map.size());
  }
Exemplo n.º 27
0
 @Test(timeout = 120000)
 public void testIssue583MapReplaceShouldTriggerMapStore() {
   final ConcurrentMap<String, Long> store = new ConcurrentHashMap<String, Long>();
   final MapStore<String, Long> myMapStore = new SimpleMapStore<String, Long>(store);
   Config config = getConfig();
   config
       .getMapConfig("myMap")
       .setMapStoreConfig(new MapStoreConfig().setImplementation(myMapStore));
   TestHazelcastInstanceFactory nodeFactory = createHazelcastInstanceFactory(3);
   HazelcastInstance hc = nodeFactory.newHazelcastInstance(config);
   IMap<String, Long> myMap = hc.getMap("myMap");
   myMap.put("one", 1L);
   assertEquals(1L, myMap.get("one").longValue());
   assertEquals(1L, store.get("one").longValue());
   myMap.putIfAbsent("two", 2L);
   assertEquals(2L, myMap.get("two").longValue());
   assertEquals(2L, store.get("two").longValue());
   myMap.putIfAbsent("one", 5L);
   assertEquals(1L, myMap.get("one").longValue());
   assertEquals(1L, store.get("one").longValue());
   myMap.replace("one", 1L, 111L);
   assertEquals(111L, myMap.get("one").longValue());
   assertEquals(111L, store.get("one").longValue());
   myMap.replace("one", 1L);
   assertEquals(1L, myMap.get("one").longValue());
   assertEquals(1L, store.get("one").longValue());
 }
Exemplo n.º 28
0
  @Test(timeout = 120000)
  public void testSlowStore() throws Exception {
    final TestMapStore store = new WaitingOnFirstTestMapStore();
    Config config = getConfig();
    MapStoreConfig mapStoreConfig = new MapStoreConfig();
    mapStoreConfig.setEnabled(true);
    mapStoreConfig.setWriteDelaySeconds(1);
    mapStoreConfig.setImplementation(store);
    config.getMapConfig("default").setMapStoreConfig(mapStoreConfig);
    HazelcastInstance h1 = createHazelcastInstance(config);
    final IMap<Integer, Integer> map = h1.getMap("testSlowStore");
    int count = 1000;
    for (int i = 0; i < count; i++) {
      map.put(i, 1);
    }
    Thread.sleep(2000); // sleep for scheduling following puts to a different second
    for (int i = 0; i < count; i++) {
      map.put(i, 2);
    }
    for (int i = 0; i < count; i++) {
      final int index = i;
      assertTrueEventually(
          new AssertTask() {
            @Override
            public void run() throws Exception {
              final Integer valueInMap = map.get(index);
              final Integer valueInStore = (Integer) store.getStore().get(index);

              assertEquals(valueInMap, valueInStore);
            }
          });
    }
  }
Exemplo n.º 29
0
  @Test
  public void setMapStoreConfigImplementationTest() {
    String mapName = "mapStoreImpObjTest";
    String xml =
        "<hazelcast xmlns=\"http://www.hazelcast.com/schema/config\">\n"
            + "<map name=\""
            + mapName
            + "\">\n"
            + "<map-store enabled=\"true\">\n"
            + "<class-name>com.hazelcast.config.helpers.DummyMapStore</class-name>\n"
            + "<write-delay-seconds>5</write-delay-seconds>\n"
            + "</map-store>\n"
            + "</map>\n"
            + "</hazelcast>\n";

    Config config = buildConfig(xml);
    HazelcastInstance hz = createHazelcastInstance(config);
    hz.getMap(mapName);

    MapConfig mapConfig = hz.getConfig().getMapConfig(mapName);
    MapStoreConfig mapStoreConfig = mapConfig.getMapStoreConfig();
    Object o = mapStoreConfig.getImplementation();

    assertNotNull(o);
    assertTrue(o instanceof DummyMapStore);
  }
Exemplo n.º 30
0
  @Test
  public void testHeartbeatResumedEvent() throws InterruptedException {
    hazelcastFactory.newHazelcastInstance();
    HazelcastInstance client = hazelcastFactory.newHazelcastClient(getClientConfig());
    final HazelcastInstance instance2 = hazelcastFactory.newHazelcastInstance();

    // make sure client is connected to instance2
    String keyOwnedByInstance2 = generateKeyOwnedBy(instance2);
    IMap<String, String> map = client.getMap(randomString());
    map.put(keyOwnedByInstance2, randomString());

    HazelcastClientInstanceImpl clientImpl = getHazelcastClientInstanceImpl(client);
    ClientConnectionManager connectionManager = clientImpl.getConnectionManager();
    final CountDownLatch countDownLatch = new CountDownLatch(1);
    connectionManager.addConnectionHeartbeatListener(
        new ConnectionHeartbeatListener() {
          @Override
          public void heartbeatResumed(Connection connection) {
            assertEquals(
                instance2.getCluster().getLocalMember().getAddress(), connection.getEndPoint());
            countDownLatch.countDown();
          }

          @Override
          public void heartbeatStopped(Connection connection) {}
        });

    blockMessagesFromInstance(instance2, client);
    sleepMillis(HEARTBEAT_TIMEOUT_MILLIS * 2);
    unblockMessagesFromInstance(instance2, client);

    assertOpenEventually(countDownLatch);
  }