Search in sources :

Example 1 with CacheEntryEventSerializableFilter

use of org.apache.ignite.cache.CacheEntryEventSerializableFilter in project camel by apache.

the class IgniteCacheContinuousQueryTest method createRegistry.

@Override
protected JndiRegistry createRegistry() throws Exception {
    JndiRegistry answer = super.createRegistry();
    ScanQuery<Integer, Person> scanQuery1 = new ScanQuery<>(new IgniteBiPredicate<Integer, Person>() {

        private static final long serialVersionUID = 1L;

        @Override
        public boolean apply(Integer key, Person person) {
            return person.getId() > 50;
        }
    });
    CacheEntryEventSerializableFilter<Integer, Person> remoteFilter = new CacheEntryEventSerializableFilter<Integer, IgniteCacheContinuousQueryTest.Person>() {

        private static final long serialVersionUID = 5624973479995548199L;

        @Override
        public boolean evaluate(CacheEntryEvent<? extends Integer, ? extends Person> event) throws CacheEntryListenerException {
            return event.getValue().getId() > 150;
        }
    };
    answer.bind("query1", scanQuery1);
    answer.bind("remoteFilter1", remoteFilter);
    return answer;
}
Also used : JndiRegistry(org.apache.camel.impl.JndiRegistry) CacheEntryEventSerializableFilter(org.apache.ignite.cache.CacheEntryEventSerializableFilter) ScanQuery(org.apache.ignite.cache.query.ScanQuery) CacheEntryEvent(javax.cache.event.CacheEntryEvent)

Example 2 with CacheEntryEventSerializableFilter

use of org.apache.ignite.cache.CacheEntryEventSerializableFilter 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 3 with CacheEntryEventSerializableFilter

use of org.apache.ignite.cache.CacheEntryEventSerializableFilter in project ignite by apache.

the class ContinuousQueryReassignmentTest method testContinuousQueryWithRemoteFilterNotCalledOnReassignment.

/**
 * @throws Exception If failed.
 */
@Test
public void testContinuousQueryWithRemoteFilterNotCalledOnReassignment() throws Exception {
    Ignite lsnrNode = startGrid(1);
    Ignite victim = startGrid(2);
    awaitPartitionMapExchange();
    CacheConfiguration<Integer, String> cacheCfg = new CacheConfiguration<>("cache");
    cacheCfg.setBackups(1);
    IgniteCache<Integer, String> cache = lsnrNode.getOrCreateCache(cacheCfg);
    AtomicInteger updCntr = new AtomicInteger();
    CacheEntryEventSerializableFilter<Integer, String> filter = (e) -> e.getKey() % 2 == 0;
    listenToUpdates(cache, false, updCntr, filter);
    int updates = 1000;
    for (int i = 0; i < updates; i++) cache.put(i, Integer.toString(i));
    assertTrue("Failed to wait for continuous query updates. Exp: " + updates + "; actual: " + updCntr.get(), waitForCondition(() -> updCntr.get() == updates / 2, 10000));
    victim.close();
    assertFalse("Continuous query is called on reassignment.", waitForCondition(() -> updCntr.get() > updates / 2, 2000));
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CacheEntryEvent(javax.cache.event.CacheEntryEvent) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) FactoryBuilder(javax.cache.configuration.FactoryBuilder) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) GridTestUtils.waitForCondition(org.apache.ignite.testframework.GridTestUtils.waitForCondition) CacheEntryEventSerializableFilter(org.apache.ignite.cache.CacheEntryEventSerializableFilter) Test(org.junit.Test) Ignite(org.apache.ignite.Ignite) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) IgniteCache(org.apache.ignite.IgniteCache) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Ignite(org.apache.ignite.Ignite) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 4 with CacheEntryEventSerializableFilter

use of org.apache.ignite.cache.CacheEntryEventSerializableFilter in project ignite by apache.

the class CacheContinuousQueryExecuteInPrimaryTest method doTestWithEventsEntries.

/**
 * @throws Exception If failed.
 */
public void doTestWithEventsEntries(CacheConfiguration<Integer, String> ccfg) throws Exception {
    try (IgniteCache<Integer, String> cache = grid(0).createCache(ccfg)) {
        ContinuousQuery<Integer, String> qry = new ContinuousQuery<>();
        final CountDownLatch latch = new CountDownLatch(16);
        final AtomicInteger cnt = new AtomicInteger(0);
        qry.setLocalListener(new CacheEntryUpdatedListener<Integer, String>() {

            @Override
            public void onUpdated(Iterable<CacheEntryEvent<? extends Integer, ? extends String>> iterable) throws CacheEntryListenerException {
                for (CacheEntryEvent<? extends Integer, ? extends String> e : iterable) {
                    cnt.incrementAndGet();
                    latch.countDown();
                }
            }
        });
        qry.setRemoteFilterFactory(FactoryBuilder.factoryOf(new CacheEntryEventSerializableFilter<Integer, String>() {

            @Override
            public boolean evaluate(CacheEntryEvent<? extends Integer, ? extends String> e) throws CacheEntryListenerException {
                return e.getKey() % 2 == 0;
            }
        }));
        // Execute query.
        executeQuery(cache, qry, ccfg.getAtomicityMode() != ATOMIC);
        assertTrue(latch.await(LATCH_TIMEOUT, MILLISECONDS));
        assertEquals(16, cnt.get());
    } finally {
        ignite(0).destroyCache(ccfg.getName());
    }
}
Also used : CacheEntryEventSerializableFilter(org.apache.ignite.cache.CacheEntryEventSerializableFilter) CountDownLatch(java.util.concurrent.CountDownLatch) CacheEntryListenerException(javax.cache.event.CacheEntryListenerException) CacheEntryEvent(javax.cache.event.CacheEntryEvent) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Example 5 with CacheEntryEventSerializableFilter

use of org.apache.ignite.cache.CacheEntryEventSerializableFilter in project ignite by apache.

the class IgniteCacheProxyImpl method queryContinuous.

/**
 * Executes continuous query.
 *
 * @param qry Query.
 * @param loc Local flag.
 * @param keepBinary Keep binary flag.
 * @return Initial iteration cursor.
 */
@SuppressWarnings("unchecked")
private QueryCursor<Cache.Entry<K, V>> queryContinuous(AbstractContinuousQuery qry, boolean loc, boolean keepBinary) {
    GridCacheContext<K, V> ctx = getContextSafe();
    assert qry instanceof ContinuousQuery || qry instanceof ContinuousQueryWithTransformer;
    if (qry.getInitialQuery() instanceof ContinuousQuery || qry.getInitialQuery() instanceof ContinuousQueryWithTransformer) {
        throw new IgniteException("Initial predicate for continuous query can't be an instance of another " + "continuous query. Use SCAN or SQL query for initial iteration.");
    }
    CacheEntryUpdatedListener locLsnr = null;
    EventListener locTransLsnr = null;
    CacheEntryEventSerializableFilter rmtFilter = null;
    Factory<? extends IgniteClosure> rmtTransFactory = null;
    if (qry instanceof ContinuousQuery) {
        ContinuousQuery<K, V> qry0 = (ContinuousQuery<K, V>) qry;
        if (qry0.getLocalListener() == null && qry0.getRemoteFilterFactory() == null && qry0.getRemoteFilter() == null) {
            throw new IgniteException("LocalListener, RemoterFilter " + "or RemoteFilterFactory must be specified for the query: " + qry);
        }
        if (qry0.getRemoteFilter() != null && qry0.getRemoteFilterFactory() != null)
            throw new IgniteException("Should be used either RemoterFilter or RemoteFilterFactory.");
        locLsnr = qry0.getLocalListener();
        rmtFilter = qry0.getRemoteFilter();
    } else {
        ContinuousQueryWithTransformer<K, V, ?> qry0 = (ContinuousQueryWithTransformer<K, V, ?>) qry;
        if (qry0.getLocalListener() == null && qry0.getRemoteFilterFactory() == null) {
            throw new IgniteException("LocalListener " + "or RemoteFilterFactory must be specified for the query: " + qry);
        }
        if (qry0.getRemoteTransformerFactory() == null)
            throw new IgniteException("Mandatory RemoteTransformerFactory is not set for the query: " + qry);
        Collection<ClusterNode> nodes = context().grid().cluster().nodes();
        for (ClusterNode node : nodes) {
            if (node.version().compareTo(CONT_QRY_WITH_TRANSFORMER_SINCE) < 0) {
                throw new IgniteException("Can't start ContinuousQueryWithTransformer, " + "because some nodes in cluster doesn't support this feature: " + node);
            }
        }
        locTransLsnr = qry0.getLocalListener();
        rmtTransFactory = qry0.getRemoteTransformerFactory();
    }
    try {
        final UUID routineId = ctx.continuousQueries().executeQuery(locLsnr, locTransLsnr, rmtFilter, qry.getRemoteFilterFactory(), rmtTransFactory, qry.getPageSize(), qry.getTimeInterval(), qry.isAutoUnsubscribe(), loc, keepBinary, qry.isIncludeExpired());
        try {
            final QueryCursor<Cache.Entry<K, V>> cur = qry.getInitialQuery() != null ? query(qry.getInitialQuery()) : null;
            return new QueryCursorEx<Entry<K, V>>() {

                @Override
                public Iterator<Cache.Entry<K, V>> iterator() {
                    return cur != null ? cur.iterator() : new GridEmptyIterator<Cache.Entry<K, V>>();
                }

                @Override
                public List<Cache.Entry<K, V>> getAll() {
                    return cur != null ? cur.getAll() : Collections.<Cache.Entry<K, V>>emptyList();
                }

                @Override
                public void close() {
                    if (cur != null)
                        cur.close();
                    try {
                        ctx.kernalContext().continuous().stopRoutine(routineId).get();
                    } catch (IgniteCheckedException e) {
                        throw U.convertException(e);
                    }
                }

                @Override
                public void getAll(Consumer<Entry<K, V>> c) {
                // No-op.
                }

                @Override
                public List<GridQueryFieldMetadata> fieldsMeta() {
                    // noinspection rawtypes
                    return cur instanceof QueryCursorEx ? ((QueryCursorEx) cur).fieldsMeta() : null;
                }
            };
        } catch (Throwable t) {
            // Initial query failed: stop the routine.
            ctx.kernalContext().continuous().stopRoutine(routineId).get();
            throw t;
        }
    } catch (IgniteCheckedException e) {
        throw U.convertException(e);
    }
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) ContinuousQueryWithTransformer(org.apache.ignite.cache.query.ContinuousQueryWithTransformer) CacheEntryEventSerializableFilter(org.apache.ignite.cache.CacheEntryEventSerializableFilter) GridQueryFieldMetadata(org.apache.ignite.internal.processors.query.GridQueryFieldMetadata) CacheEntry(org.apache.ignite.cache.CacheEntry) AbstractContinuousQuery(org.apache.ignite.cache.query.AbstractContinuousQuery) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) CacheEntryUpdatedListener(javax.cache.event.CacheEntryUpdatedListener) IgniteException(org.apache.ignite.IgniteException) QueryCursorEx(org.apache.ignite.internal.processors.cache.query.QueryCursorEx) EventListener(org.apache.ignite.cache.query.ContinuousQueryWithTransformer.EventListener) UUID(java.util.UUID) Cache(javax.cache.Cache) IgniteCache(org.apache.ignite.IgniteCache)

Aggregations

CacheEntryEventSerializableFilter (org.apache.ignite.cache.CacheEntryEventSerializableFilter)16 CacheEntryEvent (javax.cache.event.CacheEntryEvent)12 Test (org.junit.Test)10 ContinuousQuery (org.apache.ignite.cache.query.ContinuousQuery)9 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)8 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)7 Ignite (org.apache.ignite.Ignite)6 IgniteCache (org.apache.ignite.IgniteCache)6 UUID (java.util.UUID)5 CacheEntryListenerException (javax.cache.event.CacheEntryListenerException)5 ContinuousQueryWithTransformer (org.apache.ignite.cache.query.ContinuousQueryWithTransformer)5 FactoryBuilder (javax.cache.configuration.FactoryBuilder)4 CacheEntryEventFilter (javax.cache.event.CacheEntryEventFilter)4 CacheEntryUpdatedListener (javax.cache.event.CacheEntryUpdatedListener)4 IgniteException (org.apache.ignite.IgniteException)4 IgniteClosure (org.apache.ignite.lang.IgniteClosure)4 GridTestUtils.waitForCondition (org.apache.ignite.testframework.GridTestUtils.waitForCondition)4 CountDownLatch (java.util.concurrent.CountDownLatch)3 Cache (javax.cache.Cache)3 MutableCacheEntryListenerConfiguration (javax.cache.configuration.MutableCacheEntryListenerConfiguration)3