Search in sources :

Example 6 with ICache

use of com.hazelcast.cache.ICache in project hazelcast by hazelcast.

the class SourcesTest method remoteCacheWithUnknownValueClass.

@Test
public void remoteCacheWithUnknownValueClass() throws Exception {
    // Given
    URL jarResource = Thread.currentThread().getContextClassLoader().getResource("deployment/sample-pojo-1.0-car.jar");
    assertNotNull("jar not found", jarResource);
    ClassLoader cl = new URLClassLoader(new URL[] { jarResource });
    Class<?> personClz = cl.loadClass("com.sample.pojo.car.Car");
    Object person = personClz.getConstructor(String.class, String.class).newInstance("make", "model");
    ICache<String, Object> cache = remoteHz.getCacheManager().getCache(srcName);
    // the class of the value is unknown to the remote IMDG member, it will be only known to Jet
    cache.put("key", person);
    // When
    BatchSource<Entry<String, Object>> source = Sources.remoteCache(srcName, clientConfig);
    // Then
    p.readFrom(source).map(en -> en.getValue().toString()).writeTo(sink);
    JobConfig jobConfig = new JobConfig();
    jobConfig.addJar(jarResource);
    hz().getJet().newJob(p, jobConfig).join();
    List<Object> expected = singletonList(person.toString());
    List<Object> actual = new ArrayList<>(sinkList);
    assertEquals(expected, actual);
}
Also used : Socket(java.net.Socket) BeforeClass(org.junit.BeforeClass) Util.uncheckRun(com.hazelcast.jet.impl.util.Util.uncheckRun) IntStream.range(java.util.stream.IntStream.range) QuickTest(com.hazelcast.test.annotation.QuickTest) URL(java.net.URL) HazelcastInstanceFactory(com.hazelcast.instance.impl.HazelcastInstanceFactory) SourceProcessors.readMapP(com.hazelcast.jet.core.processor.SourceProcessors.readMapP) TruePredicate.truePredicate(com.hazelcast.query.impl.predicates.TruePredicate.truePredicate) CacheSimpleConfig(com.hazelcast.config.CacheSimpleConfig) ArrayList(java.util.ArrayList) Collections.singletonList(java.util.Collections.singletonList) ServerSocket(java.net.ServerSocket) URLClassLoader(java.net.URLClassLoader) Util.entry(com.hazelcast.jet.Util.entry) START_FROM_CURRENT(com.hazelcast.jet.pipeline.JournalInitialPosition.START_FROM_CURRENT) ClientConfig(com.hazelcast.client.config.ClientConfig) Projections(com.hazelcast.projection.Projections) JobStatus(com.hazelcast.jet.core.JobStatus) Job(com.hazelcast.jet.Job) PrintWriter(java.io.PrintWriter) Config(com.hazelcast.config.Config) HazelcastInstance(com.hazelcast.core.HazelcastInstance) AfterClass(org.junit.AfterClass) UTF_8(java.nio.charset.StandardCharsets.UTF_8) Assert.assertNotNull(org.junit.Assert.assertNotNull) JobConfig(com.hazelcast.jet.config.JobConfig) Test(org.junit.Test) Projections.singleAttribute(com.hazelcast.projection.Projections.singleAttribute) Category(org.junit.experimental.categories.Category) File(java.io.File) Collectors.joining(java.util.stream.Collectors.joining) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) Ignore(org.junit.Ignore) ICache(com.hazelcast.cache.ICache) Entry(java.util.Map.Entry) Assert.assertEquals(org.junit.Assert.assertEquals) IMap(com.hazelcast.map.IMap) ArrayList(java.util.ArrayList) URL(java.net.URL) JobConfig(com.hazelcast.jet.config.JobConfig) Entry(java.util.Map.Entry) URLClassLoader(java.net.URLClassLoader) URLClassLoader(java.net.URLClassLoader) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 7 with ICache

use of com.hazelcast.cache.ICache in project hazelcast by hazelcast.

the class Sources_withEventJournalTest method remoteCacheJournal_withUnknownValueClass.

@Test
public void remoteCacheJournal_withUnknownValueClass() throws Exception {
    // Given
    URL jarResource = Thread.currentThread().getContextClassLoader().getResource("deployment/sample-pojo-1.0-car.jar");
    assertNotNull("jar not found", jarResource);
    ClassLoader cl = new URLClassLoader(new URL[] { jarResource });
    Class<?> carClz = cl.loadClass("com.sample.pojo.car.Car");
    Object car = carClz.getConstructor(String.class, String.class).newInstance("make", "model");
    String cacheName = JOURNALED_CACHE_PREFIX + randomName();
    ICache<String, Object> cache = remoteHz.getCacheManager().getCache(cacheName);
    // the class of the value is unknown to the remote IMDG member, it will be only known to Jet
    cache.put("key", car);
    // When
    StreamSource<Entry<Object, Object>> source = Sources.remoteCacheJournal(cacheName, clientConfig, START_FROM_OLDEST);
    // Then
    p.readFrom(source).withoutTimestamps().map(en -> en.getValue().toString()).writeTo(sink);
    JobConfig jobConfig = new JobConfig();
    jobConfig.addJar(jarResource);
    Job job = hz().getJet().newJob(p, jobConfig);
    List<Object> expected = singletonList(car.toString());
    assertTrueEventually(() -> assertEquals(expected, new ArrayList<>(sinkList)), 10);
    job.cancel();
}
Also used : BeforeClass(org.junit.BeforeClass) IntStream.range(java.util.stream.IntStream.range) URL(java.net.URL) HazelcastInstanceFactory(com.hazelcast.instance.impl.HazelcastInstanceFactory) CacheSimpleConfig(com.hazelcast.config.CacheSimpleConfig) ArrayList(java.util.ArrayList) Collections.singletonList(java.util.Collections.singletonList) URLClassLoader(java.net.URLClassLoader) EventJournalCacheEvent(com.hazelcast.cache.EventJournalCacheEvent) Util.entry(com.hazelcast.jet.Util.entry) ClientConfig(com.hazelcast.client.config.ClientConfig) Job(com.hazelcast.jet.Job) IList(com.hazelcast.collection.IList) PredicateEx(com.hazelcast.function.PredicateEx) Config(com.hazelcast.config.Config) HazelcastInstance(com.hazelcast.core.HazelcastInstance) AfterClass(org.junit.AfterClass) Assert.assertNotNull(org.junit.Assert.assertNotNull) JobConfig(com.hazelcast.jet.config.JobConfig) Test(org.junit.Test) SlowTest(com.hazelcast.test.annotation.SlowTest) START_FROM_OLDEST(com.hazelcast.jet.pipeline.JournalInitialPosition.START_FROM_OLDEST) Category(org.junit.experimental.categories.Category) Collectors.joining(java.util.stream.Collectors.joining) List(java.util.List) Functions.entryValue(com.hazelcast.function.Functions.entryValue) Collectors.toList(java.util.stream.Collectors.toList) Stream(java.util.stream.Stream) EventJournalMapEvent(com.hazelcast.map.EventJournalMapEvent) ICache(com.hazelcast.cache.ICache) Entry(java.util.Map.Entry) Util.mapPutEvents(com.hazelcast.jet.Util.mapPutEvents) Assert.assertEquals(org.junit.Assert.assertEquals) IMap(com.hazelcast.map.IMap) ArrayList(java.util.ArrayList) URL(java.net.URL) JobConfig(com.hazelcast.jet.config.JobConfig) Entry(java.util.Map.Entry) URLClassLoader(java.net.URLClassLoader) URLClassLoader(java.net.URLClassLoader) Job(com.hazelcast.jet.Job) Test(org.junit.Test) SlowTest(com.hazelcast.test.annotation.SlowTest)

Example 8 with ICache

use of com.hazelcast.cache.ICache in project hazelcast by hazelcast.

the class HazelcastConnectorTest method when_streamCache_withFilterAndProjection.

@Test
public void when_streamCache_withFilterAndProjection() {
    DAG dag = new DAG();
    Vertex source = dag.newVertex("source", SourceProcessors.<Integer, Integer, Integer>streamCacheP(streamSourceName, event -> !event.getKey().equals(0), EventJournalCacheEvent::getKey, START_FROM_OLDEST, eventTimePolicy(i -> i, limitingLag(0), 1, 0, 10_000)));
    Vertex sink = dag.newVertex("sink", writeListP(streamSinkName));
    dag.edge(between(source, sink));
    Job job = instance().getJet().newJob(dag);
    ICache<Integer, Integer> sourceCache = instance().getCacheManager().getCache(streamSourceName);
    range(0, ENTRY_COUNT).forEach(i -> sourceCache.put(i, i));
    assertSizeEventually(ENTRY_COUNT - 1, instance().getList(streamSinkName));
    assertFalse(instance().getList(streamSinkName).contains(0));
    assertTrue(instance().getList(streamSinkName).contains(1));
    job.cancel();
}
Also used : ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) IntStream.range(java.util.stream.IntStream.range) QuickTest(com.hazelcast.test.annotation.QuickTest) SourceProcessors.readMapP(com.hazelcast.jet.core.processor.SourceProcessors.readMapP) SourceProcessors.readListP(com.hazelcast.jet.core.processor.SourceProcessors.readListP) SourceProcessors.streamCacheP(com.hazelcast.jet.core.processor.SourceProcessors.streamCacheP) EventJournalCacheEvent(com.hazelcast.cache.EventJournalCacheEvent) TruePredicate(com.hazelcast.query.impl.predicates.TruePredicate) Map(java.util.Map) SinkProcessors.writeCacheP(com.hazelcast.jet.core.processor.SinkProcessors.writeCacheP) DAG(com.hazelcast.jet.core.DAG) ICacheManager(com.hazelcast.core.ICacheManager) Projections(com.hazelcast.projection.Projections) SourceProcessors.readCacheP(com.hazelcast.jet.core.processor.SourceProcessors.readCacheP) SimpleTestInClusterSupport(com.hazelcast.jet.SimpleTestInClusterSupport) EventTimePolicy.noEventTime(com.hazelcast.jet.core.EventTimePolicy.noEventTime) START_FROM_OLDEST(com.hazelcast.jet.pipeline.JournalInitialPosition.START_FROM_OLDEST) Category(org.junit.experimental.categories.Category) Collectors.joining(java.util.stream.Collectors.joining) List(java.util.List) NearCacheConfig(com.hazelcast.config.NearCacheConfig) EventJournalMapEvent(com.hazelcast.map.EventJournalMapEvent) Assert.assertFalse(org.junit.Assert.assertFalse) SinkProcessors(com.hazelcast.jet.core.processor.SinkProcessors) Entry(java.util.Map.Entry) SinkProcessors.writeMapP(com.hazelcast.jet.core.processor.SinkProcessors.writeMapP) IntStream(java.util.stream.IntStream) NearCachedMapProxyImpl(com.hazelcast.map.impl.proxy.NearCachedMapProxyImpl) BeforeClass(org.junit.BeforeClass) SourceProcessors(com.hazelcast.jet.core.processor.SourceProcessors) TestProcessors(com.hazelcast.jet.core.TestProcessors) Util.entry(com.hazelcast.jet.Util.entry) Job(com.hazelcast.jet.Job) Before(org.junit.Before) IList(com.hazelcast.collection.IList) Config(com.hazelcast.config.Config) SourceProcessors.streamMapP(com.hazelcast.jet.core.processor.SourceProcessors.streamMapP) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) WatermarkPolicy.limitingLag(com.hazelcast.jet.core.WatermarkPolicy.limitingLag) Vertex(com.hazelcast.jet.core.Vertex) Collectors.toList(java.util.stream.Collectors.toList) Predicates(com.hazelcast.query.Predicates) ICache(com.hazelcast.cache.ICache) EventTimePolicy.eventTimePolicy(com.hazelcast.jet.core.EventTimePolicy.eventTimePolicy) Util.mapPutEvents(com.hazelcast.jet.Util.mapPutEvents) Assert.assertEquals(org.junit.Assert.assertEquals) IMap(com.hazelcast.map.IMap) Edge.between(com.hazelcast.jet.core.Edge.between) SinkProcessors.writeListP(com.hazelcast.jet.core.processor.SinkProcessors.writeListP) Vertex(com.hazelcast.jet.core.Vertex) DAG(com.hazelcast.jet.core.DAG) Job(com.hazelcast.jet.Job) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 9 with ICache

use of com.hazelcast.cache.ICache in project hazelcast by hazelcast.

the class CacheExpirationManagerTest method expiration_task_starts_on_new_node_after_migration_when_there_is_expirable_entry.

@Test
public void expiration_task_starts_on_new_node_after_migration_when_there_is_expirable_entry() {
    Config config = getConfig();
    config.setProperty(taskPeriodSecondsPropName(), "1");
    TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory();
    final HazelcastInstance node1 = factory.newHazelcastInstance(config);
    CacheManager cacheManager = createCacheManager(node1);
    Cache cache = cacheManager.createCache("test", new CacheConfig());
    ((ICache) cache).put(1, 1, new HazelcastExpiryPolicy(ONE_HOUR, ONE_HOUR, ONE_HOUR));
    final HazelcastInstance node2 = factory.newHazelcastInstance(config);
    node1.shutdown();
    assertTrueEventually(new AssertTask() {

        @Override
        public void run() {
            assertTrue("There should be one ClearExpiredRecordsTask started", hasClearExpiredRecordsTaskStarted(node2));
        }
    });
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) Config(com.hazelcast.config.Config) CacheConfig(com.hazelcast.config.CacheConfig) ICache(com.hazelcast.cache.ICache) CacheManager(javax.cache.CacheManager) AssertTask(com.hazelcast.test.AssertTask) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) CacheConfig(com.hazelcast.config.CacheConfig) HazelcastExpiryPolicy(com.hazelcast.cache.HazelcastExpiryPolicy) Cache(javax.cache.Cache) ICache(com.hazelcast.cache.ICache) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 10 with ICache

use of com.hazelcast.cache.ICache in project hazelcast by hazelcast.

the class TimedMemberStateTest method testOnlyCachesWithStatsEnabled_areReportedInTimedMemberState.

@Test
public void testOnlyCachesWithStatsEnabled_areReportedInTimedMemberState() {
    // create 100 caches with stats enabled
    for (int i = 0; i < 100; i++) {
        hz.getCacheManager().getCache(CACHE_WITH_STATS_PREFIX + i);
    }
    // create 50 caches with stats disabled
    for (int i = 0; i < 50; i++) {
        ICache cacheWithoutStats = hz.getCacheManager().getCache(CACHE_WITHOUT_STATS_PREFIX + i);
        // explicitly request local stats -> this registers an empty stats object in CacheService
        cacheWithoutStats.getLocalCacheStatistics();
    }
    MemberStateImpl memberState = createState().getMemberState();
    for (int i = 0; i < 100; i++) {
        assertContains(memberState.getCachesWithStats(), getDistributedObjectName(CACHE_WITH_STATS_PREFIX + i));
        assertNotContains(memberState.getCachesWithStats(), getDistributedObjectName(CACHE_WITHOUT_STATS_PREFIX + i));
    }
}
Also used : MemberStateImpl(com.hazelcast.internal.monitor.impl.MemberStateImpl) ICache(com.hazelcast.cache.ICache) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Aggregations

ICache (com.hazelcast.cache.ICache)20 Test (org.junit.Test)16 QuickTest (com.hazelcast.test.annotation.QuickTest)15 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)13 HazelcastInstance (com.hazelcast.core.HazelcastInstance)11 CacheManager (javax.cache.CacheManager)9 CacheConfig (com.hazelcast.config.CacheConfig)7 Config (com.hazelcast.config.Config)6 CachingProvider (javax.cache.spi.CachingProvider)6 CacheTestSupport.createClientCachingProvider (com.hazelcast.cache.CacheTestSupport.createClientCachingProvider)5 CacheTestSupport.createServerCachingProvider (com.hazelcast.cache.CacheTestSupport.createServerCachingProvider)5 HazelcastExpiryPolicy (com.hazelcast.cache.HazelcastExpiryPolicy)5 HazelcastServerCachingProvider (com.hazelcast.cache.impl.HazelcastServerCachingProvider)4 CacheSimpleConfig (com.hazelcast.config.CacheSimpleConfig)4 ClientConfig (com.hazelcast.client.config.ClientConfig)3 IList (com.hazelcast.collection.IList)3 Job (com.hazelcast.jet.Job)3 Util.entry (com.hazelcast.jet.Util.entry)3 JobConfig (com.hazelcast.jet.config.JobConfig)3 IMap (com.hazelcast.map.IMap)3