Search in sources :

Example 6 with CacheEntryUpdatedListener

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

the class IgniteCacheConfigVariationsFullApiTest method testContinuousQuery.

/**
 * @throws Exception If failed.
 */
public void testContinuousQuery() throws Exception {
    runInAllDataModes(new TestRunnable() {

        @Override
        public void run() throws Exception {
            final AtomicInteger updCnt = new AtomicInteger();
            ContinuousQuery<Object, Object> qry = new ContinuousQuery<>();
            qry.setInitialQuery(new ScanQuery<>(new IgniteBiPredicate<Object, Object>() {

                @Override
                public boolean apply(Object key, Object val) {
                    return valueOf(key) >= 3;
                }
            }));
            qry.setLocalListener(new CacheEntryUpdatedListener<Object, Object>() {

                @Override
                public void onUpdated(Iterable<CacheEntryEvent<? extends Object, ? extends Object>> evts) throws CacheEntryListenerException {
                    for (CacheEntryEvent<? extends Object, ? extends Object> evt : evts) {
                        int v = valueOf(evt.getKey());
                        // Check filter.
                        assertTrue("v=" + v, v >= 10 && v < 15);
                        updCnt.incrementAndGet();
                    }
                }
            });
            qry.setRemoteFilter(new TestCacheEntryEventSerializableFilter());
            IgniteCache<Object, Object> cache = jcache();
            for (int i = 0; i < 10; i++) cache.put(key(i), value(i));
            try (QueryCursor<Cache.Entry<Object, Object>> cur = cache.query(qry)) {
                int cnt = 0;
                for (Cache.Entry<Object, Object> e : cur) {
                    cnt++;
                    int val = valueOf(e.getKey());
                    assertTrue("v=" + val, val >= 3);
                }
                assertEquals(7, cnt);
                for (int i = 10; i < 20; i++) cache.put(key(i), value(i));
                GridTestUtils.waitForCondition(new GridAbsPredicateX() {

                    @Override
                    public boolean applyx() throws IgniteCheckedException {
                        return updCnt.get() == 5;
                    }
                }, 30_000);
            }
        }
    });
}
Also used : IgniteCache(org.apache.ignite.IgniteCache) ScanQuery(org.apache.ignite.cache.query.ScanQuery) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) CacheEntryListenerException(javax.cache.event.CacheEntryListenerException) EntryProcessorException(javax.cache.processor.EntryProcessorException) CacheException(javax.cache.CacheException) MutableEntry(javax.cache.processor.MutableEntry) CacheEntry(org.apache.ignite.cache.CacheEntry) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CacheEntryUpdatedListener(javax.cache.event.CacheEntryUpdatedListener) GridAbsPredicateX(org.apache.ignite.internal.util.lang.GridAbsPredicateX) QueryCursor(org.apache.ignite.cache.query.QueryCursor) Cache(javax.cache.Cache) IgniteCache(org.apache.ignite.IgniteCache)

Example 7 with CacheEntryUpdatedListener

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

the class TcpDiscoveryMultiThreadedTest method _testClientContinuousQueryCoordinatorStop.

/**
 * @throws Exception If failed.
 */
public void _testClientContinuousQueryCoordinatorStop() throws Exception {
    for (int k = 0; k < 10; k++) {
        log.info("Iteration: " + k);
        clientFlagGlobal = false;
        final int START_NODES = 5;
        final int JOIN_NODES = 5;
        startGrids(START_NODES);
        ignite(0).createCache(new CacheConfiguration<>(DEFAULT_CACHE_NAME));
        final AtomicInteger startIdx = new AtomicInteger(START_NODES);
        final CyclicBarrier barrier = new CyclicBarrier(JOIN_NODES + 1);
        clientFlagGlobal = true;
        IgniteInternalFuture<?> fut = GridTestUtils.runMultiThreadedAsync(new Callable<Object>() {

            @Override
            public Object call() throws Exception {
                int idx = startIdx.getAndIncrement();
                Thread.currentThread().setName("start-thread-" + idx);
                barrier.await();
                Ignite ignite = startGrid(idx);
                assertTrue(ignite.configuration().isClientMode());
                log.info("Started node: " + ignite.name());
                IgniteCache<Object, Object> cache = ignite.getOrCreateCache(DEFAULT_CACHE_NAME);
                for (int i = 0; i < 10; i++) {
                    ContinuousQuery<Object, Object> qry = new ContinuousQuery<>();
                    qry.setLocalListener(new CacheEntryUpdatedListener<Object, Object>() {

                        @Override
                        public void onUpdated(Iterable<CacheEntryEvent<?, ?>> evts) {
                        // No-op.
                        }
                    });
                    cache.query(qry);
                }
                return null;
            }
        }, JOIN_NODES, "start-thread");
        barrier.await();
        U.sleep(ThreadLocalRandom.current().nextInt(100, 500));
        for (int i = 0; i < START_NODES - 1; i++) {
            GridTestUtils.invoke(ignite(i).configuration().getDiscoverySpi(), "simulateNodeFailure");
            stopGrid(i);
        }
        fut.get();
        stopAllGrids();
    }
}
Also used : IgniteCache(org.apache.ignite.IgniteCache) IgniteSpiException(org.apache.ignite.spi.IgniteSpiException) IgniteClientDisconnectedException(org.apache.ignite.IgniteClientDisconnectedException) IgniteClientDisconnectedCheckedException(org.apache.ignite.internal.IgniteClientDisconnectedCheckedException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) ClusterTopologyCheckedException(org.apache.ignite.internal.cluster.ClusterTopologyCheckedException) CyclicBarrier(java.util.concurrent.CyclicBarrier) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CacheEntryUpdatedListener(javax.cache.event.CacheEntryUpdatedListener) Ignite(org.apache.ignite.Ignite)

Example 8 with CacheEntryUpdatedListener

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

the class IgniteCacheGroupsTest method registerListener.

/**
 * @param node Node.
 * @param cacheName Cache name.
 * @return Received events counter.
 */
private AtomicInteger registerListener(Ignite node, String cacheName) {
    ContinuousQuery qry = new ContinuousQuery();
    final AtomicInteger cntr = new AtomicInteger();
    qry.setLocalListener(new CacheEntryUpdatedListener() {

        @Override
        public void onUpdated(Iterable iterable) {
            for (Object evt : iterable) cntr.incrementAndGet();
        }
    });
    node.cache(cacheName).query(qry);
    return cntr;
}
Also used : ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CacheEntryUpdatedListener(javax.cache.event.CacheEntryUpdatedListener) BinaryObject(org.apache.ignite.binary.BinaryObject)

Example 9 with CacheEntryUpdatedListener

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

the class IgniteCacheGroupsTest method continuousQuery.

/**
 * @param cacheMode Cache mode.
 * @param atomicityMode Cache atomicity mode.
 * @throws Exception If failed.
 */
private void continuousQuery(CacheMode cacheMode, CacheAtomicityMode atomicityMode) throws Exception {
    final int keys = 10_000;
    Integer[] data1 = generateData(keys);
    Integer[] data2 = generateData(keys);
    boolean loc = cacheMode == LOCAL;
    if (loc)
        startGrid(0);
    else
        startGridsMultiThreaded(4);
    Ignite srv0 = ignite(0);
    srv0.createCache(cacheConfiguration(GROUP1, CACHE1, cacheMode, atomicityMode, 2, false));
    srv0.createCache(cacheConfiguration(GROUP1, CACHE2, cacheMode, atomicityMode, 2, false));
    final AtomicInteger cntr1 = new AtomicInteger();
    final AtomicInteger cntr2 = new AtomicInteger();
    CacheEntryUpdatedListener lsnr1 = new CacheEntryUpdatedListener<Integer, Integer>() {

        @Override
        public void onUpdated(Iterable<CacheEntryEvent<? extends Integer, ? extends Integer>> evts) {
            for (CacheEntryEvent<? extends Integer, ? extends Integer> ignored : evts) cntr1.incrementAndGet();
        }
    };
    CacheEntryUpdatedListener lsnr2 = new CacheEntryUpdatedListener<Integer, Integer>() {

        @Override
        public void onUpdated(Iterable<CacheEntryEvent<? extends Integer, ? extends Integer>> evts) {
            for (CacheEntryEvent<? extends Integer, ? extends Integer> ignored : evts) cntr2.incrementAndGet();
        }
    };
    QueryCursor qry1 = ignite(loc ? 0 : 2).cache(CACHE1).query(new ContinuousQuery<>().setLocalListener(lsnr1));
    QueryCursor qry2 = ignite(loc ? 0 : 3).cache(CACHE2).query(new ContinuousQuery<>().setLocalListener(lsnr2));
    if (atomicityMode == TRANSACTIONAL) {
        Ignite ignite = ignite(loc ? 0 : 1);
        IgniteCache<Integer, Integer> cache1 = ignite.cache(CACHE1);
        IgniteCache<Integer, Integer> cache2 = ignite.cache(CACHE2);
        try (Transaction tx = ignite.transactions().txStart()) {
            for (int i = 0; i < keys; i++) {
                cache1.put(i, data1[i]);
                cache2.put(i, data2[i]);
            }
            tx.commit();
        }
    } else {
        int ldrs = 4;
        List<Callable<?>> cls = new ArrayList<>(ldrs * 2);
        for (int i = 0; i < ldrs; i++) {
            cls.add(putOperation(loc ? 0 : 1, ldrs, i, CACHE1, data1));
            cls.add(putOperation(loc ? 0 : 2, ldrs, i, CACHE2, data2));
        }
        GridTestUtils.runMultiThreaded(cls, "loaders");
    }
    GridTestUtils.waitForCondition(new PA() {

        @Override
        public boolean apply() {
            return cntr1.get() == keys && cntr2.get() == keys;
        }
    }, 2000);
    assertEquals(cntr1.get(), keys);
    assertEquals(cntr2.get(), keys);
    qry1.close();
    Map<Integer, Integer> map = generateDataMap(10);
    srv0.cache(CACHE1).putAll(map);
    srv0.cache(CACHE2).putAll(map);
    GridTestUtils.waitForCondition(new PA() {

        @Override
        public boolean apply() {
            return cntr2.get() == keys + 10;
        }
    }, 2000);
    assertEquals(keys + 10, cntr2.get());
    assertEquals(keys, cntr1.get());
    qry2.close();
}
Also used : ArrayList(java.util.ArrayList) GridPlainCallable(org.apache.ignite.internal.util.lang.GridPlainCallable) Callable(java.util.concurrent.Callable) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) PA(org.apache.ignite.internal.util.typedef.PA) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) Transaction(org.apache.ignite.transactions.Transaction) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CacheEntryUpdatedListener(javax.cache.event.CacheEntryUpdatedListener) Ignite(org.apache.ignite.Ignite) QueryCursor(org.apache.ignite.cache.query.QueryCursor)

Example 10 with CacheEntryUpdatedListener

use of javax.cache.event.CacheEntryUpdatedListener 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) {
    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)
            throw new IgniteException("Mandatory local listener is not set 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)
            throw new IgniteException("Mandatory local transformed event listener is not set 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());
        final QueryCursor<Cache.Entry<K, V>> cur = qry.getInitialQuery() != null ? query(qry.getInitialQuery()) : null;
        return new QueryCursor<Cache.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);
                }
            }
        };
    } 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) 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) EventListener(org.apache.ignite.cache.query.ContinuousQueryWithTransformer.EventListener) UUID(java.util.UUID) QueryCursor(org.apache.ignite.cache.query.QueryCursor) FieldsQueryCursor(org.apache.ignite.cache.query.FieldsQueryCursor) Cache(javax.cache.Cache) IgniteCache(org.apache.ignite.IgniteCache)

Aggregations

CacheEntryUpdatedListener (javax.cache.event.CacheEntryUpdatedListener)16 ContinuousQuery (org.apache.ignite.cache.query.ContinuousQuery)13 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)9 QueryCursor (org.apache.ignite.cache.query.QueryCursor)8 Ignite (org.apache.ignite.Ignite)7 IgniteCache (org.apache.ignite.IgniteCache)6 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)6 IgniteException (org.apache.ignite.IgniteException)6 Cache (javax.cache.Cache)4 ArrayList (java.util.ArrayList)3 CacheEntryListenerException (javax.cache.event.CacheEntryListenerException)3 IgniteClientDisconnectedException (org.apache.ignite.IgniteClientDisconnectedException)3 CacheEntryEventSerializableFilter (org.apache.ignite.cache.CacheEntryEventSerializableFilter)3 AbstractContinuousQuery (org.apache.ignite.cache.query.AbstractContinuousQuery)3 ClusterNode (org.apache.ignite.cluster.ClusterNode)3 IgniteClientDisconnectedCheckedException (org.apache.ignite.internal.IgniteClientDisconnectedCheckedException)3 ClusterTopologyCheckedException (org.apache.ignite.internal.cluster.ClusterTopologyCheckedException)3 PA (org.apache.ignite.internal.util.typedef.PA)3 UUID (java.util.UUID)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2