Search in sources :

Example 26 with IgniteClient

use of org.apache.ignite.client.IgniteClient in project ignite by apache.

the class CacheEntryListenersTest method testContinuousQueriesWithConcurrentCompute.

/**
 */
@Test
public void testContinuousQueriesWithConcurrentCompute() throws Exception {
    try (IgniteClient client = startClient(0, 1, 2)) {
        int threadsCnt = 20;
        int iterations = 50;
        Set<UUID> allNodesIds = new HashSet<>(F.nodeIds(grid(0).cluster().nodes()));
        AtomicInteger threadIdxs = new AtomicInteger();
        GridTestUtils.runMultiThreaded(() -> {
            int threadIdx = threadIdxs.incrementAndGet();
            ClientCache<Integer, Integer> cache = client.getOrCreateCache("testCQwithCompute" + threadIdx);
            try {
                for (int i = 0; i < iterations; i++) {
                    ContinuousQueryListener<Integer, Integer> lsnr = new ContinuousQueryListener<>();
                    QueryCursor<?> cur = cache.query(new ContinuousQuery<Integer, Integer>().setLocalListener(lsnr));
                    cache.put(i, i);
                    Future<T2<UUID, Set<UUID>>> fut = client.compute().executeAsync2(TestTask.class.getName(), null);
                    assertEquals(allNodesIds, fut.get().get2());
                    lsnr.assertNextCacheEvent(EventType.CREATED, i, i);
                    assertTrue(lsnr.isQueueEmpty());
                    cur.close();
                }
            } catch (Exception e) {
                log.error("Failure: ", e);
                fail();
            }
        }, threadsCnt, "run-task-async");
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) IgniteClient(org.apache.ignite.client.IgniteClient) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) UUID(java.util.UUID) T2(org.apache.ignite.internal.util.typedef.T2) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 27 with IgniteClient

use of org.apache.ignite.client.IgniteClient in project ignite by apache.

the class CacheEntryListenersTest method testJCacheListenersExpiredEntries.

/**
 * Test JCache entry listeners with expired entries.
 */
@Test
public void testJCacheListenersExpiredEntries() throws Exception {
    try (IgniteClient client = startClient(0, 1, 2)) {
        ClientCache<Integer, Integer> cache = client.getOrCreateCache("testJCacheListenersWithExp");
        JCacheEntryListener<Integer, Integer> lsnr = new JCacheEntryListener<>();
        cache.registerCacheEntryListener(new MutableCacheEntryListenerConfiguration<>(() -> lsnr, null, true, false));
        cache = cache.withExpirePolicy(new CreatedExpiryPolicy(new Duration(TimeUnit.MILLISECONDS, 1)));
        for (int i = 0; i < 100; i++) cache.put(i, i);
        int cntCreated = 0;
        int cntExpired = 0;
        for (int i = 0; i < 200; i++) {
            // There should be two events for each cache entry.
            CacheEntryEvent<?, ?> evt = lsnr.poll();
            if (evt.getEventType() == EventType.CREATED)
                cntCreated++;
            else if (evt.getEventType() == EventType.EXPIRED)
                cntExpired++;
            else
                fail("Unexpected event type: " + evt.getEventType());
        }
        assertEquals(100, cntCreated);
        assertEquals(100, cntExpired);
        assertTrue(lsnr.isQueueEmpty());
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgniteClient(org.apache.ignite.client.IgniteClient) Duration(javax.cache.expiry.Duration) CreatedExpiryPolicy(javax.cache.expiry.CreatedExpiryPolicy) Test(org.junit.Test)

Example 28 with IgniteClient

use of org.apache.ignite.client.IgniteClient in project ignite by apache.

the class CacheEntryListenersTest method testListenersWithRemoteFilter.

/**
 * Test continuous queries and JCache entry listeners with remote filters.
 */
@Test
@SuppressWarnings("deprecation")
public void testListenersWithRemoteFilter() throws Exception {
    try (IgniteClient client = startClient(0, 1, 2)) {
        ClientCache<Integer, Integer> cache = client.getOrCreateCache("testListenersWithRmtFilter");
        CacheEntryEventSerializableFilter<Integer, Integer> rmtFilter = evt -> (evt.getKey() & 1) == 0;
        ContinuousQueryListener<Integer, Integer> lsnr1 = new ContinuousQueryListener<>();
        ContinuousQueryListener<Integer, Integer> lsnr2 = new ContinuousQueryListener<>();
        cache.query(new ContinuousQuery<Integer, Integer>().setLocalListener(lsnr1).setRemoteFilterFactory(() -> rmtFilter));
        cache.query(new ContinuousQuery<Integer, Integer>().setLocalListener(lsnr2).setRemoteFilter(rmtFilter));
        JCacheEntryListener<Integer, Integer> lsnr3 = new JCacheEntryListener<>();
        cache.registerCacheEntryListener(new MutableCacheEntryListenerConfiguration<>(() -> lsnr3, () -> rmtFilter, true, false));
        for (int i = 0; i < 10; i++) cache.put(i, i);
        Map<EventType, Map<Integer, Integer>> expRes = F.asMap(EventType.CREATED, IntStream.range(0, 5).boxed().collect(Collectors.toMap(i -> i * 2, i -> i * 2)));
        assertEquals(expRes, aggregateListenerEvents(lsnr1, 5));
        assertEquals(expRes, aggregateListenerEvents(lsnr2, 5));
        assertEquals(expRes, aggregateListenerEvents(lsnr3, 5));
        for (int i = 0; i < 10; i++) cache.put(i, -i);
        expRes = F.asMap(EventType.UPDATED, IntStream.range(0, 5).boxed().collect(Collectors.toMap(i -> i * 2, i -> -i * 2)));
        assertEquals(expRes, aggregateListenerEvents(lsnr1, 5));
        assertEquals(expRes, aggregateListenerEvents(lsnr2, 5));
        assertEquals(expRes, aggregateListenerEvents(lsnr3, 5));
        for (int i = 0; i < 10; i++) cache.remove(i);
        expRes = F.asMap(EventType.REMOVED, IntStream.range(0, 5).boxed().collect(Collectors.toMap(i -> i * 2, i -> -i * 2)));
        assertEquals(expRes, aggregateListenerEvents(lsnr1, 5));
        assertEquals(expRes, aggregateListenerEvents(lsnr2, 5));
        assertEquals(expRes, aggregateListenerEvents(lsnr3, 5));
        assertTrue(lsnr1.isQueueEmpty());
        assertTrue(lsnr2.isQueueEmpty());
        assertTrue(lsnr3.isQueueEmpty());
    }
}
Also used : IntStream(java.util.stream.IntStream) ClientDisconnectListener(org.apache.ignite.client.ClientDisconnectListener) Person(org.apache.ignite.client.Person) GridTestUtils.waitForCondition(org.apache.ignite.testframework.GridTestUtils.waitForCondition) CacheEntryEventSerializableFilter(org.apache.ignite.cache.CacheEntryEventSerializableFilter) U(org.apache.ignite.internal.util.typedef.internal.U) HashMap(java.util.HashMap) CacheEntryExpiredListener(javax.cache.event.CacheEntryExpiredListener) ClientCache(org.apache.ignite.client.ClientCache) HashSet(java.util.HashSet) Future(java.util.concurrent.Future) CacheEntryListenerConfiguration(javax.cache.configuration.CacheEntryListenerConfiguration) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgniteClient(org.apache.ignite.client.IgniteClient) Map(java.util.Map) Cache(javax.cache.Cache) CacheEntryCreatedListener(javax.cache.event.CacheEntryCreatedListener) CacheEntryUpdatedListener(javax.cache.event.CacheEntryUpdatedListener) Duration(javax.cache.expiry.Duration) MutableCacheEntryListenerConfiguration(javax.cache.configuration.MutableCacheEntryListenerConfiguration) F(org.apache.ignite.internal.util.typedef.F) EventType(javax.cache.event.EventType) CreatedExpiryPolicy(javax.cache.expiry.CreatedExpiryPolicy) GridTestUtils.assertThrowsWithCause(org.apache.ignite.testframework.GridTestUtils.assertThrowsWithCause) EnumMap(java.util.EnumMap) FactoryBuilder(javax.cache.configuration.FactoryBuilder) Set(java.util.Set) BlockingQueue(java.util.concurrent.BlockingQueue) Test(org.junit.Test) UUID(java.util.UUID) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Collectors(java.util.stream.Collectors) IgniteCache(org.apache.ignite.IgniteCache) T2(org.apache.ignite.internal.util.typedef.T2) GridTestUtils(org.apache.ignite.testframework.GridTestUtils) TimeUnit(java.util.concurrent.TimeUnit) IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) CacheEntryEvent(javax.cache.event.CacheEntryEvent) CacheEntryRemovedListener(javax.cache.event.CacheEntryRemovedListener) ThinClientConfiguration(org.apache.ignite.configuration.ThinClientConfiguration) QueryCursor(org.apache.ignite.cache.query.QueryCursor) ScanQuery(org.apache.ignite.cache.query.ScanQuery) ClientConnectorConfiguration(org.apache.ignite.configuration.ClientConnectorConfiguration) EventType(javax.cache.event.EventType) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgniteClient(org.apache.ignite.client.IgniteClient) HashMap(java.util.HashMap) Map(java.util.Map) EnumMap(java.util.EnumMap) Test(org.junit.Test)

Example 29 with IgniteClient

use of org.apache.ignite.client.IgniteClient in project ignite by apache.

the class CacheEntryListenersTest method testContinuousQueriesWithIncludeExpired.

/**
 * Test continuous queries with include expired parameter.
 */
@Test
public void testContinuousQueriesWithIncludeExpired() throws Exception {
    try (IgniteClient client = startClient(0, 1, 2)) {
        ClientCache<Integer, Integer> cache = client.getOrCreateCache("testCQWithInclExp");
        ContinuousQueryListener<Integer, Integer> lsnr1 = new ContinuousQueryListener<>();
        ContinuousQueryListener<Integer, Integer> lsnr2 = new ContinuousQueryListener<>();
        ContinuousQuery<Integer, Integer> qry1 = new ContinuousQuery<Integer, Integer>().setLocalListener(lsnr1);
        ContinuousQuery<Integer, Integer> qry2 = new ContinuousQuery<Integer, Integer>().setLocalListener(lsnr2);
        qry1.setIncludeExpired(false);
        qry2.setIncludeExpired(true);
        cache.query(qry1);
        cache.query(qry2);
        cache = cache.withExpirePolicy(new CreatedExpiryPolicy(new Duration(TimeUnit.MILLISECONDS, 1)));
        for (int i = 0; i < 100; i++) cache.put(i, i);
        int cntCreated = 0;
        int cntExpired = 0;
        for (int i = 0; i < 100; i++) assertEquals(EventType.CREATED, lsnr1.poll().getEventType());
        for (int i = 0; i < 200; i++) {
            // There should be two events for each cache entry.
            CacheEntryEvent<?, ?> evt = lsnr2.poll();
            if (evt.getEventType() == EventType.CREATED)
                cntCreated++;
            else if (evt.getEventType() == EventType.EXPIRED)
                cntExpired++;
            else
                fail("Unexpected event type: " + evt.getEventType());
        }
        assertEquals(100, cntCreated);
        assertEquals(100, cntExpired);
        assertTrue(lsnr1.isQueueEmpty());
        assertTrue(lsnr2.isQueueEmpty());
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgniteClient(org.apache.ignite.client.IgniteClient) Duration(javax.cache.expiry.Duration) CreatedExpiryPolicy(javax.cache.expiry.CreatedExpiryPolicy) Test(org.junit.Test)

Example 30 with IgniteClient

use of org.apache.ignite.client.IgniteClient in project ignite by apache.

the class CacheEntryListenersTest method testRegisterDeregisterListener.

/**
 */
@Test
@SuppressWarnings("ThrowableNotThrown")
public void testRegisterDeregisterListener() throws Exception {
    try (IgniteClient client = startClient(0, 1, 2)) {
        String cacheName = "registerListener";
        ClientCache<Integer, Integer> cache0 = client.getOrCreateCache(cacheName);
        CacheEntryListenerConfiguration<Integer, Integer> lsnrCfg = new MutableCacheEntryListenerConfiguration<>(JCacheEntryListener::new, null, true, false);
        cache0.registerCacheEntryListener(lsnrCfg);
        ClientCache<Integer, Integer> cache1 = client.getOrCreateCache(cacheName + '2');
        // Can register the same listener on another cache.
        cache1.registerCacheEntryListener(lsnrCfg);
        ClientCache<Integer, Integer> cache2 = client.cache(cacheName);
        // Can't register the same listener on the same cache.
        assertThrowsWithCause(() -> cache2.registerCacheEntryListener(lsnrCfg), IllegalStateException.class);
        ClientCache<Integer, Integer> cache3 = client.cache(cacheName);
        cache3.deregisterCacheEntryListener(lsnrCfg);
        // Can register the listener after deregisteration.
        cache2.registerCacheEntryListener(lsnrCfg);
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgniteClient(org.apache.ignite.client.IgniteClient) MutableCacheEntryListenerConfiguration(javax.cache.configuration.MutableCacheEntryListenerConfiguration) Test(org.junit.Test)

Aggregations

IgniteClient (org.apache.ignite.client.IgniteClient)106 Test (org.junit.Test)76 ClientConfiguration (org.apache.ignite.configuration.ClientConfiguration)43 ThinClientConfiguration (org.apache.ignite.configuration.ThinClientConfiguration)26 UUID (java.util.UUID)21 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)15 Ignite (org.apache.ignite.Ignite)14 IgniteConfiguration (org.apache.ignite.configuration.IgniteConfiguration)14 HashSet (java.util.HashSet)13 T2 (org.apache.ignite.internal.util.typedef.T2)13 Set (java.util.Set)12 CountDownLatch (java.util.concurrent.CountDownLatch)12 ContinuousQuery (org.apache.ignite.cache.query.ContinuousQuery)12 List (java.util.List)10 BinaryObject (org.apache.ignite.binary.BinaryObject)10 GridTestUtils (org.apache.ignite.testframework.GridTestUtils)10 GridTestUtils.assertThrowsWithCause (org.apache.ignite.testframework.GridTestUtils.assertThrowsWithCause)10 Map (java.util.Map)9 Duration (javax.cache.expiry.Duration)9 ClientCacheConfiguration (org.apache.ignite.client.ClientCacheConfiguration)9