Search in sources :

Example 11 with CacheEntryEventFilter

use of javax.cache.event.CacheEntryEventFilter in project ignite by apache.

the class CacheContinuousQueryHandler method onEntryUpdate.

/**
 * @param evt Continuous query event.
 * @param notify Notify flag.
 * @param loc Listener deployed on this node.
 * @param recordIgniteEvt Record ignite event.
 */
private void onEntryUpdate(CacheContinuousQueryEvent<K, V> evt, boolean notify, boolean loc, boolean recordIgniteEvt) {
    try {
        GridCacheContext<K, V> cctx = cacheContext(ctx);
        if (cctx == null)
            return;
        CacheContinuousQueryEntry entry = evt.entry();
        IgniteClosure<CacheEntryEvent<? extends K, ? extends V>, ?> trans = getTransformer();
        if (loc) {
            if (!locOnly) {
                Collection<CacheEntryEvent<? extends K, ? extends V>> evts = handleEvent(ctx, entry);
                notifyLocalListener(evts, trans);
                if (!internal && !skipPrimaryCheck)
                    sendBackupAcknowledge(ackBuf.onAcknowledged(entry), routineId, ctx);
            } else if (!entry.isFiltered())
                notifyLocalListener(F.asList(evt), trans);
        } else {
            if (!entry.isFiltered()) {
                if (trans != null)
                    entry = transformToEntry(trans, evt);
                prepareEntry(cctx, nodeId, entry);
            }
            Object entryOrList = handleEntry(cctx, entry);
            if (entryOrList != null) {
                if (log.isDebugEnabled())
                    log.debug("Send the following event to listener: " + entryOrList);
                ctx.continuous().addNotification(nodeId, routineId, entryOrList, topic, sync, true);
            }
        }
    } catch (ClusterTopologyCheckedException ex) {
        if (log.isDebugEnabled())
            log.debug("Failed to send event notification to node, node left cluster " + "[node=" + nodeId + ", err=" + ex + ']');
    } catch (IgniteCheckedException ex) {
        U.error(ctx.log(CU.CONTINUOUS_QRY_LOG_CATEGORY), "Failed to send event notification to node: " + nodeId, ex);
    }
    if (recordIgniteEvt && notify) {
        CacheEntryEventFilter filter;
        try {
            filter = getEventFilter();
        } catch (IgniteCheckedException e) {
            if (log.isDebugEnabled()) {
                log.debug("Failed to trigger a continuous query event. " + "[routineId=" + routineId + ", cacheName=" + cacheName + ", err=" + e + "]");
            }
            return;
        }
        // noinspection unchecked
        ctx.event().record(new CacheQueryReadEvent<K, V>(ctx.discovery().localNode(), "Continuous query executed.", EVT_CACHE_QUERY_OBJECT_READ, CacheQueryType.CONTINUOUS.name(), cacheName, null, null, null, filter instanceof CacheEntryEventSerializableFilter ? (CacheEntryEventSerializableFilter) filter : null, null, nodeId, taskName(), evt.getKey(), evt.getValue(), evt.getOldValue(), null));
    }
}
Also used : CacheEntryEventSerializableFilter(org.apache.ignite.cache.CacheEntryEventSerializableFilter) CacheEntryEventFilter(javax.cache.event.CacheEntryEventFilter) CacheEntryEvent(javax.cache.event.CacheEntryEvent) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) ClusterTopologyCheckedException(org.apache.ignite.internal.cluster.ClusterTopologyCheckedException)

Example 12 with CacheEntryEventFilter

use of javax.cache.event.CacheEntryEventFilter in project ignite by apache.

the class CacheContinuousQueryHandler method sendQueryExecutedEvent.

/**
 * Fires continuous query execution event.
 * @see org.apache.ignite.events.EventType#EVT_CACHE_QUERY_EXECUTED
 */
private void sendQueryExecutedEvent() {
    GridCacheContext<K, V> cctx = cacheContext(ctx);
    CacheEntryEventFilter filter;
    try {
        filter = getEventFilter();
    } catch (IgniteCheckedException e) {
        if (log.isDebugEnabled()) {
            log.debug("Failed to trigger the continuoue query executed event. " + "[routineId=" + routineId + ", cacheName=" + cacheName + ", err=" + e + "]");
        }
        return;
    }
    if (cctx != null && cctx.events().isRecordable(EVT_CACHE_QUERY_EXECUTED)) {
        // noinspection unchecked
        ctx.event().record(new CacheQueryExecutedEvent<K, V>(ctx.discovery().localNode(), "Continuous query executed.", EVT_CACHE_QUERY_EXECUTED, CacheQueryType.CONTINUOUS.name(), cacheName, null, null, null, filter instanceof CacheEntryEventSerializableFilter ? (CacheEntryEventSerializableFilter) filter : null, null, nodeId, taskName()));
    }
}
Also used : CacheEntryEventSerializableFilter(org.apache.ignite.cache.CacheEntryEventSerializableFilter) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) CacheEntryEventFilter(javax.cache.event.CacheEntryEventFilter)

Example 13 with CacheEntryEventFilter

use of javax.cache.event.CacheEntryEventFilter in project ignite by apache.

the class CacheContinuousQueryOperationP2PTest method testMultithreadedUpdatesNodeJoin.

/**
 * @throws Exception If failed.
 */
@Test
public void testMultithreadedUpdatesNodeJoin() throws Exception {
    Ignite client = startGrid("client");
    CacheConfiguration<Object, Object> cacheCfg = cacheConfiguration(PARTITIONED, 0, ATOMIC);
    IgniteCache<Object, Object> cache = client.createCache(cacheCfg);
    int iterations = 50;
    int keysNum = 100;
    int threadsNum = Runtime.getRuntime().availableProcessors();
    CountDownLatch updatesLatch = new CountDownLatch(iterations * keysNum * threadsNum / 2);
    ContinuousQuery<Object, Object> qry = new ContinuousQuery<>();
    final Class<Factory<CacheEntryEventFilter>> evtFilterFactoryCls = (Class<Factory<CacheEntryEventFilter>>) getExternalClassLoader().loadClass("org.apache.ignite.tests.p2p.CacheDeploymentEntryEventFilterFactory");
    qry.setRemoteFilterFactory((Factory<? extends CacheEntryEventFilter<Object, Object>>) (Object) evtFilterFactoryCls.newInstance());
    qry.setLocalListener((evts) -> {
        for (CacheEntryEvent<?, ?> ignored : evts) updatesLatch.countDown();
    });
    cache.query(qry);
    for (int t = 0; t < threadsNum; t++) {
        int threadId = t;
        GridTestUtils.runAsync(() -> {
            for (int i = 0; i < iterations; i++) {
                log.info("Iteration #" + (i + 1));
                for (int k = 0; k < keysNum; k++) {
                    int key = keysNum * threadId + k;
                    cache.put(key, key);
                }
            }
        }, "cache-writer-thread-" + threadId);
    }
    startGrid(NODES);
    assertTrue("Failed to wait for all cache updates invocations. Latch: " + updatesLatch, updatesLatch.await(30, TimeUnit.SECONDS));
}
Also used : Factory(javax.cache.configuration.Factory) CountDownLatch(java.util.concurrent.CountDownLatch) CacheEntryEventFilter(javax.cache.event.CacheEntryEventFilter) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) Ignite(org.apache.ignite.Ignite) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 14 with CacheEntryEventFilter

use of javax.cache.event.CacheEntryEventFilter in project ignite by apache.

the class CacheContinuousQueryRandomOperationsTest method testFilterAndFactoryProvided.

/**
 * @throws Exception If failed.
 */
@Test
public void testFilterAndFactoryProvided() throws Exception {
    final CacheConfiguration<Object, Object> ccfg = cacheConfiguration(PARTITIONED, 1, ATOMIC, false);
    grid(0).createCache(ccfg);
    try {
        final ContinuousQuery qry = new ContinuousQuery();
        qry.setRemoteFilterFactory(new Factory<CacheEntryEventFilter>() {

            @Override
            public CacheEntryEventFilter create() {
                return null;
            }
        });
        qry.setRemoteFilter(new CacheEntryEventSerializableFilter() {

            @Override
            public boolean evaluate(CacheEntryEvent event) throws CacheEntryListenerException {
                return false;
            }
        });
        qry.setLocalListener(new CacheEntryUpdatedListener() {

            @Override
            public void onUpdated(Iterable iterable) throws CacheEntryListenerException {
            // No-op.
            }
        });
        GridTestUtils.assertThrows(log, new Callable<Object>() {

            @Override
            public Object call() throws Exception {
                return grid(0).cache(ccfg.getName()).query(qry);
            }
        }, IgniteException.class, null);
    } finally {
        grid(0).destroyCache(ccfg.getName());
    }
}
Also used : CacheEntryEventSerializableFilter(org.apache.ignite.cache.CacheEntryEventSerializableFilter) CacheEntryEventFilter(javax.cache.event.CacheEntryEventFilter) CacheEntryListenerException(javax.cache.event.CacheEntryListenerException) CacheEntryEvent(javax.cache.event.CacheEntryEvent) IgniteException(org.apache.ignite.IgniteException) CacheWriterException(javax.cache.integration.CacheWriterException) CacheEntryListenerException(javax.cache.event.CacheEntryListenerException) CacheLoaderException(javax.cache.integration.CacheLoaderException) AbstractContinuousQuery(org.apache.ignite.cache.query.AbstractContinuousQuery) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) CacheEntryUpdatedListener(javax.cache.event.CacheEntryUpdatedListener) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Aggregations

CacheEntryEventFilter (javax.cache.event.CacheEntryEventFilter)14 ContinuousQuery (org.apache.ignite.cache.query.ContinuousQuery)11 Factory (javax.cache.configuration.Factory)8 CacheEntryEvent (javax.cache.event.CacheEntryEvent)8 IgniteCache (org.apache.ignite.IgniteCache)8 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)6 Test (org.junit.Test)6 Ignite (org.apache.ignite.Ignite)5 CacheEntryEventSerializableFilter (org.apache.ignite.cache.CacheEntryEventSerializableFilter)5 Cache (javax.cache.Cache)4 CacheEntryUpdatedListener (javax.cache.event.CacheEntryUpdatedListener)4 AbstractContinuousQuery (org.apache.ignite.cache.query.AbstractContinuousQuery)4 IgniteConfiguration (org.apache.ignite.configuration.IgniteConfiguration)4 IgniteEx (org.apache.ignite.internal.IgniteEx)4 U (org.apache.ignite.internal.util.typedef.internal.U)4 Map (java.util.Map)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 IgniteException (org.apache.ignite.IgniteException)3 IgniteBiPredicate (org.apache.ignite.lang.IgniteBiPredicate)3 IgniteClosure (org.apache.ignite.lang.IgniteClosure)3