use of com.hazelcast.config.MapStoreConfig in project hazelcast by hazelcast.
the class MapStoreDataLoadingContinuesWhenNodeJoins method testDataLoadedCorrectly.
@Test(timeout = 600000)
public void testDataLoadedCorrectly() throws Exception {
// create shared hazelcast config
final Config config = new XmlConfigBuilder().build();
// disable JMX to make sure lazy loading works asynchronously
config.setProperty("hazelcast.jmx", "false");
// get map config
MapConfig mapConfig = config.getMapConfig(MAP_NAME);
// create shared map store implementation
// - use slow loading (300ms per map entry)
final CountDownLatch halfOfKeysAreLoaded = new CountDownLatch(1);
final InMemoryMapStore store = new InMemoryMapStore(halfOfKeysAreLoaded, 300, false);
store.preload(PRELOAD_SIZE);
// configure map store
MapStoreConfig mapStoreConfig = new MapStoreConfig();
mapStoreConfig.setEnabled(true);
mapStoreConfig.setInitialLoadMode(InitialLoadMode.LAZY);
mapStoreConfig.setWriteDelaySeconds(WRITE_DELAY_SECONDS);
mapStoreConfig.setClassName(null);
mapStoreConfig.setImplementation(store);
mapConfig.setMapStoreConfig(mapStoreConfig);
final TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(2);
final CountDownLatch node1Started = new CountDownLatch(1);
final CountDownLatch node1FinishedLoading = new CountDownLatch(1);
// thread 1:
// start a single node and load the data
Thread thread1 = new Thread(new Runnable() {
@Override
public void run() {
HazelcastInstance instance = factory.newHazelcastInstance(config);
// try-finally to stop hazelcast instance
node1Started.countDown();
try {
// get map
// this will trigger loading the data
final IMap<String, String> map = instance.getMap(MAP_NAME);
mapSize.set(map.size());
node1FinishedLoading.countDown();
assertTrueEventually(new AssertTask() {
@Override
public void run() throws Exception {
assertEquals(PRELOAD_SIZE, map.size());
}
}, 5);
// -------------------------------------------------- {20s}
} finally {
instance.getLifecycleService().shutdown();
}
}
}, "Thread 1");
thread1.start();
// wait 10s after starting first thread
node1Started.await();
// thread 2:
// simulate a second member which joins the cluster
Thread thread2 = new Thread(new Runnable() {
@Override
public void run() {
HazelcastInstance instance = factory.newHazelcastInstance(config);
// try-finally to stop hazelcast instance
try {
// get map
instance.getMap(MAP_NAME);
final int loadTimeMillis = MS_PER_LOAD * PRELOAD_SIZE;
node1FinishedLoading.await(loadTimeMillis, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
EmptyStatement.ignore(e);
} finally {
instance.getLifecycleService().shutdown();
}
}
}, "Thread 2");
if (SIMULATE_SECOND_NODE) {
thread2.start();
}
// join threads
thread1.join();
if (SIMULATE_SECOND_NODE) {
thread2.join();
}
}
use of com.hazelcast.config.MapStoreConfig in project hazelcast by hazelcast.
the class InterceptorTest method testPutEvent_withInterceptor_withLoadAll.
@Test
public void testPutEvent_withInterceptor_withLoadAll() {
String name = randomString();
Config config = getConfig();
MapStoreConfig mapStoreConfig = new MapStoreConfig();
mapStoreConfig.setEnabled(true).setImplementation(new DummyLoader());
config.getMapConfig(name).setMapStoreConfig(mapStoreConfig);
HazelcastInstance instance1 = createHazelcastInstance(config);
IMap<Integer, String> map = instance1.getMap(name);
map.addInterceptor(new SimpleInterceptor());
final EntryAddedLatch listener = new EntryAddedLatch();
map.addEntryListener(listener, true);
Set<Integer> keys = new HashSet<Integer>();
keys.add(1);
map.loadAll(keys, false);
assertTrueEventually(new AssertTask() {
@Override
public void run() throws Exception {
assertEquals("FOO-1", listener.value.get());
}
}, 15);
}
use of com.hazelcast.config.MapStoreConfig in project hazelcast by hazelcast.
the class MapTransactionTest method testGetForUpdate_LoadsKeyFromMapLoader_whenKeyDoesNotExistsInDb.
@Test
public void testGetForUpdate_LoadsKeyFromMapLoader_whenKeyDoesNotExistsInDb() {
final String mapName = randomMapName();
final MapStoreAdapter mock = mock(MapStoreAdapter.class);
when(mock.load(anyObject())).thenReturn(null);
Config config = new Config();
MapStoreConfig storeConfig = new MapStoreConfig();
storeConfig.setEnabled(true).setImplementation(mock);
config.getMapConfig(mapName).setMapStoreConfig(storeConfig);
HazelcastInstance instance = createHazelcastInstance(config);
instance.executeTransaction(new TransactionalTask<Object>() {
@Override
public Object execute(TransactionalTaskContext context) throws TransactionException {
TransactionalMap<Object, Object> map = context.getMap(mapName);
Object value = map.getForUpdate(1);
assertNull("value should be null", value);
verify(mock, times(1)).load(anyObject());
return null;
}
});
}
use of com.hazelcast.config.MapStoreConfig in project hazelcast by hazelcast.
the class IndexIntegrationTest method loadFromStore_whenEvicted.
@Test
public void loadFromStore_whenEvicted() {
// GIVEN
String name = randomString();
String attributeName = "currency";
String currency = "dollar";
long amount = 5L;
Config config = new Config();
config.setProperty(GroupProperty.PARTITION_COUNT.getName(), "1");
MapConfig mapConfig = config.getMapConfig(name);
mapConfig.setEvictionPolicy(EvictionPolicy.LFU);
mapConfig.setMinEvictionCheckMillis(0);
// size=1 means each put/load will trigger eviction
MaxSizeConfig maxSizeConfig = new MaxSizeConfig(1, PER_PARTITION);
mapConfig.setMaxSizeConfig(maxSizeConfig);
// Dummy map loader which returns a Trade object with amount=5, currency=dollar
MapStoreConfig mapStoreConfig = mapConfig.getMapStoreConfig();
mapStoreConfig.setEnabled(true);
mapStoreConfig.setImplementation(new DummyLoader(amount, currency));
HazelcastInstance instance = createHazelcastInstance(config);
IMap<String, Trade> map = instance.getMap(name);
map.addIndex(attributeName, false);
// WHEN
// This `get` will trigger load from map-loader but since eviction kicks in, entry will get removed
// We should be able to get the value loaded from store but index should be removed
Trade trade = map.get(randomString());
map.get(randomString());
// THEN
assertEquals(1, map.size());
assertEquals(5L, (long) trade.amount);
assertEquals(currency, trade.currency);
Index index = getIndexOfAttributeForMap(instance, name, attributeName);
Set<QueryableEntry> dollars = index.getRecords(currency);
assertEquals(1, dollars.size());
}
use of com.hazelcast.config.MapStoreConfig in project hazelcast by hazelcast.
the class PostProcessingMapStoreTest method testProcessedValueCarriedToTheBackup.
@Test
public void testProcessedValueCarriedToTheBackup() {
String name = randomString();
Config config = new Config();
MapConfig mapConfig = config.getMapConfig(name);
mapConfig.setReadBackupData(true);
MapStoreConfig mapStoreConfig = new MapStoreConfig();
mapStoreConfig.setEnabled(true).setClassName(IncrementerPostProcessingMapStore.class.getName());
mapConfig.setMapStoreConfig(mapStoreConfig);
HazelcastInstance instance1 = factory.newHazelcastInstance(config);
HazelcastInstance instance2 = factory.newHazelcastInstance(config);
IMap<Integer, SampleObject> map1 = instance1.getMap(name);
IMap<Integer, SampleObject> map2 = instance2.getMap(name);
for (int i = 0; i < 100; i++) {
map1.put(i, new SampleObject(i));
}
for (int i = 0; i < 100; i++) {
SampleObject o = map1.get(i);
assertEquals(i + 1, o.version);
}
for (int i = 0; i < 100; i++) {
SampleObject o = map2.get(i);
assertEquals(i + 1, o.version);
}
}
Aggregations