Search in sources :

Example 56 with MapStoreConfig

use of com.hazelcast.config.MapStoreConfig in project hazelcast by hazelcast.

the class MapStoreTest method testIssue583MapReplaceShouldTriggerMapStore.

@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());
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) MapConfig(com.hazelcast.config.MapConfig) MapStoreConfig(com.hazelcast.config.MapStoreConfig) Config(com.hazelcast.config.Config) GroupConfig(com.hazelcast.config.GroupConfig) AtomicLong(java.util.concurrent.atomic.AtomicLong) MapStoreConfig(com.hazelcast.config.MapStoreConfig) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 57 with MapStoreConfig

use of com.hazelcast.config.MapStoreConfig in project hazelcast by hazelcast.

the class MapLoaderTest method createMapConfig.

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;
}
Also used : MapIndexConfig(com.hazelcast.config.MapIndexConfig) MapConfig(com.hazelcast.config.MapConfig) Config(com.hazelcast.config.Config) MapIndexConfig(com.hazelcast.config.MapIndexConfig) MapStoreConfig(com.hazelcast.config.MapStoreConfig) MapConfig(com.hazelcast.config.MapConfig) MapStoreConfig(com.hazelcast.config.MapStoreConfig)

Example 58 with MapStoreConfig

use of com.hazelcast.config.MapStoreConfig in project hazelcast by hazelcast.

the class MapLoaderTest method testSenderAndBackupTerminates_AfterInitialLoad.

@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();
    assertClusterSizeEventually(3, ownerAndReplicas[3]);
    map = ownerAndReplicas[3].getMap(name);
    map.loadAll(false);
    assertEquals(DummyMapLoader.DEFAULT_SIZE, map.size());
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) MapConfig(com.hazelcast.config.MapConfig) Config(com.hazelcast.config.Config) MapIndexConfig(com.hazelcast.config.MapIndexConfig) MapStoreConfig(com.hazelcast.config.MapStoreConfig) MapConfig(com.hazelcast.config.MapConfig) MapStoreConfig(com.hazelcast.config.MapStoreConfig) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 59 with MapStoreConfig

use of com.hazelcast.config.MapStoreConfig in project hazelcast by hazelcast.

the class MapLoaderTest method test1770.

// 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);
}
Also used : HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) 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) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HazelcastInstance(com.hazelcast.core.HazelcastInstance) MapLoader(com.hazelcast.core.MapLoader) Collection(java.util.Collection) AssertTask(com.hazelcast.test.AssertTask) MapConfig(com.hazelcast.config.MapConfig) HashSet(java.util.HashSet) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 60 with MapStoreConfig

use of com.hazelcast.config.MapStoreConfig in project hazelcast by hazelcast.

the class MapStoreDataLoadingContinuesWhenNodeJoins method testNoDeadLockDuringJoin.

@Test(timeout = 600000)
public void testNoDeadLockDuringJoin() 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, MS_PER_LOAD, 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 hcInstance = factory.newHazelcastInstance(config);
            // try-finally to stop hazelcast instance
            node1Started.countDown();
            try {
                // get map
                // this will trigger loading the data
                IMap<String, String> map = hcInstance.getMap(MAP_NAME);
                map.size();
                node1FinishedLoading.countDown();
            } finally {
                thread1Shutdown.set(System.currentTimeMillis());
                hcInstance.getLifecycleService().shutdown();
            }
        }
    }, "Thread 1");
    thread1.start();
    node1Started.await();
    // thread 2:
    // simulate a second member which joins the cluster
    Thread thread2 = new Thread(new Runnable() {

        @Override
        public void run() {
            HazelcastInstance hcInstance = factory.newHazelcastInstance(config);
            try {
                hcInstance.getMap(MAP_NAME);
                final int loadTimeMillis = MS_PER_LOAD * PRELOAD_SIZE;
                node1FinishedLoading.await(loadTimeMillis, TimeUnit.MILLISECONDS);
            } catch (InterruptedException e) {
                EmptyStatement.ignore(e);
            } finally {
                thread2Shutdown.set(System.currentTimeMillis());
                hcInstance.getLifecycleService().shutdown();
            }
        }
    }, "Thread 2");
    thread2.start();
    // join threads
    thread1.join();
    thread2.join();
    // assert correct shutdown order
    if (thread1Shutdown.get() > thread2Shutdown.get()) {
        fail("Thread 2 was shutdown before thread 1.");
    }
}
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) MapConfig(com.hazelcast.config.MapConfig) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) Test(org.junit.Test) SlowTest(com.hazelcast.test.annotation.SlowTest)

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