@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")); }
@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); } }); } }
@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()); }
@Test(timeout = 120000) public void testMapGetAll() throws InterruptedException { final Map<String, String> _map = new HashMap<String, String>(); _map.put("key1", "value1"); _map.put("key2", "value2"); _map.put("key3", "value3"); final AtomicBoolean loadAllCalled = new AtomicBoolean(false); final AtomicBoolean loadCalled = new AtomicBoolean(false); TestHazelcastInstanceFactory nodeFactory = createHazelcastInstanceFactory(2); Config config = getConfig(); MapStoreConfig mapStoreConfig = new MapStoreConfig(); mapStoreConfig.setEnabled(true); mapStoreConfig.setImplementation( new MapLoader<String, String>() { public String load(String key) { loadCalled.set(true); return _map.get(key); } public Map<String, String> loadAll(Collection<String> keys) { loadAllCalled.set(true); final HashMap<String, String> temp = new HashMap<String, String>(); for (String key : keys) { temp.put(key, _map.get(key)); } return temp; } public Set<String> loadAllKeys() { return _map.keySet(); } }); String mapName = "default"; config.getMapConfig(mapName).setMapStoreConfig(mapStoreConfig); HazelcastInstance instance1 = nodeFactory.newHazelcastInstance(config); HazelcastInstance instance2 = nodeFactory.newHazelcastInstance(config); IMap map = instance1.getMap(mapName); final HashSet<String> keys = new HashSet<String>(3); keys.add("key1"); keys.add("key3"); keys.add("key4"); final Map subMap = map.getAll(keys); assertEquals(2, subMap.size()); assertEquals("value1", subMap.get("key1")); assertEquals("value3", subMap.get("key3")); assertTrue(loadAllCalled.get()); assertFalse(loadCalled.get()); }
private Config createChunkedMapLoaderConfig( String mapName, int chunkSize, ChunkedLoader chunkedLoader) { Config cfg = getConfig(); cfg.setProperty(GroupProperty.PARTITION_COUNT, "1"); cfg.setProperty(GroupProperty.MAP_LOAD_CHUNK_SIZE, String.valueOf(chunkSize)); MapStoreConfig mapStoreConfig = new MapStoreConfig(); mapStoreConfig.setEnabled(true); mapStoreConfig.setImplementation(chunkedLoader); MapConfig mapConfig = cfg.getMapConfig(mapName); mapConfig.setMapStoreConfig(mapStoreConfig); return cfg; }
private Config createMapConfig(String mapName, SampleIndexableObjectMapLoader loader) { Config config = getConfig(); MapConfig mapConfig = config.getMapConfig(mapName); List<MapIndexConfig> indexConfigs = mapConfig.getMapIndexConfigs(); indexConfigs.add(new MapIndexConfig("name", true)); MapStoreConfig storeConfig = new MapStoreConfig(); storeConfig.setFactoryImplementation(loader); storeConfig.setEnabled(true); mapConfig.setMapStoreConfig(storeConfig); return config; }
// https://github.com/hazelcast/hazelcast/issues/1770 @Test public void test1770() throws InterruptedException { Config config = getConfig(); config.getManagementCenterConfig().setEnabled(true); config.getManagementCenterConfig().setUrl("http://127.0.0.1:8090/mancenter"); MapConfig mapConfig = config.getMapConfig("foo"); final AtomicBoolean loadAllCalled = new AtomicBoolean(); MapLoader<Object, Object> mapLoader = new MapLoader<Object, Object>() { @Override public Object load(Object key) { return null; } @Override public Map<Object, Object> loadAll(Collection keys) { loadAllCalled.set(true); return new HashMap<Object, Object>(); } @Override public Set<Object> loadAllKeys() { return new HashSet<Object>(Collections.singletonList(1)); } }; MapStoreConfig mapStoreConfig = new MapStoreConfig(); mapStoreConfig.setEnabled(true); mapStoreConfig.setImplementation(mapLoader); mapConfig.setMapStoreConfig(mapStoreConfig); HazelcastInstance hz = createHazelcastInstance(config); hz.getMap(mapConfig.getName()); assertTrueAllTheTime( new AssertTask() { @Override public void run() { assertFalse("LoadAll should not have been called", loadAllCalled.get()); } }, 10); }
@Bean public Config config() { MapConfig map = new MapConfig() .setName("test-map") .setEvictionPolicy(EvictionPolicy.LRU) .setTimeToLiveSeconds(2400); MapStoreConfig ms = new MapStoreConfig(); ms.setClassName("com.ikentoo.test.hazelcast.LoggingMapStore"); ms.setEnabled(true); ms.setWriteDelaySeconds(30); ms.setWriteCoalescing(true); map.setMapStoreConfig(ms); Config c = new Config().addMapConfig(map).setProperty("hazelcast.logging.type", "slf4j"); return c; }
@Test(timeout = 120000) public void testInitialLoadModeEager() { int size = 10000; String mapName = "default"; TestHazelcastInstanceFactory nodeFactory = createHazelcastInstanceFactory(2); Config config = getConfig(); 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); HazelcastInstance instance2 = nodeFactory.newHazelcastInstance(config); IMap map = instance1.getMap(mapName); assertSizeEventually(size, map); }
@Test(timeout = 120000) public void testInitialLoadModeEagerWhileStoppigOneNode() throws InterruptedException { 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("testInitialLoadModeEagerWhileStoppigOneNode") .setMapStoreConfig(mapStoreConfig); final HazelcastInstance instance1 = nodeFactory.newHazelcastInstance(config); final HazelcastInstance instance2 = nodeFactory.newHazelcastInstance(config); new Thread( new Runnable() { @Override public void run() { sleepSeconds(3); instance1.getLifecycleService().shutdown(); sleepSeconds(3); final IMap<Object, Object> map = instance2.getMap("testInitialLoadModeEagerWhileStoppigOneNode"); assertEquals(size, map.size()); countDownLatch.countDown(); } }) .start(); assertOpenEventually(countDownLatch); final IMap<Object, Object> map2 = instance2.getMap("testInitialLoadModeEagerWhileStoppigOneNode"); final int map2Size = map2.size(); assertEquals(size, map2Size); }
@Test(timeout = 240000) public void testMapInitialLoad() throws InterruptedException { int size = 10000; String mapName = randomMapName(); TestHazelcastInstanceFactory nodeFactory = createHazelcastInstanceFactory(3); Config config = getConfig(); MapStoreConfig mapStoreConfig = new MapStoreConfig(); mapStoreConfig.setEnabled(true); mapStoreConfig.setImplementation(new SimpleMapLoader(size, true)); MapConfig mc = config.getMapConfig(mapName); mc.setMapStoreConfig(mapStoreConfig); HazelcastInstance instance1 = nodeFactory.newHazelcastInstance(config); HazelcastInstance instance2 = nodeFactory.newHazelcastInstance(config); IMap map = instance1.getMap(mapName); // trigger initial loads by touching partitions. for (int i = 0; i < size; i++) { assertEquals(i, map.get(i)); } assertSizeEventually(size, map); for (int i = 0; i < size; i++) { assertEquals(i, map.get(i)); } assertNull(map.put(size, size)); assertEquals(size, map.remove(size)); assertNull(map.get(size)); HazelcastInstance instance3 = nodeFactory.newHazelcastInstance(config); for (int i = 0; i < size; i++) { assertEquals(i, map.get(i)); } }
@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(timeout = 120000) public void testIssue1115EnablingMapstoreMutatingValue() throws InterruptedException { Config config = getConfig(); String mapName = "testIssue1115"; MapStore mapStore = new ProcessingStore(); MapStoreConfig mapStoreConfig = new MapStoreConfig(); mapStoreConfig.setEnabled(true); mapStoreConfig.setImplementation(mapStore); config.getMapConfig(mapName).setMapStoreConfig(mapStoreConfig); TestHazelcastInstanceFactory nodeFactory = createHazelcastInstanceFactory(2); HazelcastInstance instance1 = nodeFactory.newHazelcastInstance(config); HazelcastInstance instance2 = nodeFactory.newHazelcastInstance(config); IMap<Integer, Employee> map = instance1.getMap(mapName); Random random = new Random(); // testing put with new object for (int i = 0; i < 10; i++) { Employee emp = new Employee(); emp.setAge(random.nextInt(20) + 20); map.put(i, emp); } for (int i = 0; i < 10; i++) { Employee employee = map.get(i); assertEquals(employee.getAge() * 1000, employee.getSalary(), 0); } // testing put with existing object for (int i = 0; i < 10; i++) { Employee emp = map.get(i); emp.setAge(random.nextInt(20) + 20); map.put(i, emp); } for (int i = 0; i < 10; i++) { Employee employee = map.get(i); assertEquals(employee.getAge() * 1000, employee.getSalary(), 0); } // testing put with replace for (int i = 0; i < 10; i++) { Employee emp = map.get(i); emp.setAge(random.nextInt(20) + 20); map.replace(i, emp); } for (int i = 0; i < 10; i++) { Employee employee = map.get(i); assertEquals(employee.getAge() * 1000, employee.getSalary(), 0); } // testing put with putIfAbsent for (int i = 10; i < 20; i++) { Employee emp = new Employee(); emp.setAge(random.nextInt(20) + 20); map.putIfAbsent(i, emp); } for (int i = 10; i < 20; i++) { Employee employee = map.get(i); assertEquals(employee.getAge() * 1000, employee.getSalary(), 0); } }