Search in sources :

Example 61 with MapStoreConfig

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();
    }
}
Also used : XmlConfigBuilder(com.hazelcast.config.XmlConfigBuilder) MapConfig(com.hazelcast.config.MapConfig) Config(com.hazelcast.config.Config) MapStoreConfig(com.hazelcast.config.MapStoreConfig) MapStoreConfig(com.hazelcast.config.MapStoreConfig) CountDownLatch(java.util.concurrent.CountDownLatch) IMap(com.hazelcast.core.IMap) HazelcastInstance(com.hazelcast.core.HazelcastInstance) AssertTask(com.hazelcast.test.AssertTask) MapConfig(com.hazelcast.config.MapConfig) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) Test(org.junit.Test) SlowTest(com.hazelcast.test.annotation.SlowTest)

Example 62 with MapStoreConfig

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);
}
Also used : Config(com.hazelcast.config.Config) MapStoreConfig(com.hazelcast.config.MapStoreConfig) MapStoreConfig(com.hazelcast.config.MapStoreConfig) HazelcastInstance(com.hazelcast.core.HazelcastInstance) AssertTask(com.hazelcast.test.AssertTask) HashSet(java.util.HashSet) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 63 with MapStoreConfig

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;
        }
    });
}
Also used : TransactionalMap(com.hazelcast.core.TransactionalMap) HazelcastInstance(com.hazelcast.core.HazelcastInstance) TransactionException(com.hazelcast.transaction.TransactionException) MapStoreConfig(com.hazelcast.config.MapStoreConfig) Config(com.hazelcast.config.Config) MapStoreAdapter(com.hazelcast.core.MapStoreAdapter) TransactionalTaskContext(com.hazelcast.transaction.TransactionalTaskContext) Mockito.anyObject(org.mockito.Mockito.anyObject) MapStoreConfig(com.hazelcast.config.MapStoreConfig) QuickTest(com.hazelcast.test.annotation.QuickTest) NightlyTest(com.hazelcast.test.annotation.NightlyTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 64 with MapStoreConfig

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());
}
Also used : MaxSizeConfig(com.hazelcast.config.MaxSizeConfig) MapConfig(com.hazelcast.config.MapConfig) Config(com.hazelcast.config.Config) MapIndexConfig(com.hazelcast.config.MapIndexConfig) MapStoreConfig(com.hazelcast.config.MapStoreConfig) MapStoreConfig(com.hazelcast.config.MapStoreConfig) MaxSizeConfig(com.hazelcast.config.MaxSizeConfig) HazelcastInstance(com.hazelcast.core.HazelcastInstance) MapConfig(com.hazelcast.config.MapConfig) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 65 with MapStoreConfig

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);
    }
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) MapConfig(com.hazelcast.config.MapConfig) Config(com.hazelcast.config.Config) MapStoreConfig(com.hazelcast.config.MapStoreConfig) MapConfig(com.hazelcast.config.MapConfig) MapStoreConfig(com.hazelcast.config.MapStoreConfig) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Aggregations

MapStoreConfig (com.hazelcast.config.MapStoreConfig)76 Config (com.hazelcast.config.Config)70 MapConfig (com.hazelcast.config.MapConfig)61 HazelcastInstance (com.hazelcast.core.HazelcastInstance)51 Test (org.junit.Test)50 QuickTest (com.hazelcast.test.annotation.QuickTest)43 ParallelTest (com.hazelcast.test.annotation.ParallelTest)42 TestHazelcastInstanceFactory (com.hazelcast.test.TestHazelcastInstanceFactory)23 GroupConfig (com.hazelcast.config.GroupConfig)21 IMap (com.hazelcast.core.IMap)12 AssertTask (com.hazelcast.test.AssertTask)12 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)12 MapIndexConfig (com.hazelcast.config.MapIndexConfig)10 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)9 NightlyTest (com.hazelcast.test.annotation.NightlyTest)8 HashSet (java.util.HashSet)8 NearCacheConfig (com.hazelcast.config.NearCacheConfig)6 HashMap (java.util.HashMap)6 Map (java.util.Map)6 MapStoreAdapter (com.hazelcast.core.MapStoreAdapter)5