Search in sources :

Example 1 with CacheEntryUpdatedListener

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

the class CacheContinuousQueryFactoryFilterRandomOperationTest method testInternalQuery.

/**
     * @throws Exception If failed.
     */
public void testInternalQuery() throws Exception {
    CacheConfiguration<Object, Object> ccfg = cacheConfiguration(REPLICATED, 1, ATOMIC, false);
    final IgniteCache<Object, Object> cache = grid(0).createCache(ccfg);
    UUID uuid = null;
    try {
        for (int i = 0; i < 10; i++) cache.put(i, i);
        final CountDownLatch latch = new CountDownLatch(5);
        CacheEntryUpdatedListener lsnr = new CacheEntryUpdatedListener() {

            @Override
            public void onUpdated(Iterable iterable) throws CacheEntryListenerException {
                for (Object evt : iterable) {
                    latch.countDown();
                    log.info("Received event: " + evt);
                }
            }
        };
        uuid = grid(0).context().cache().cache(cache.getName()).context().continuousQueries().executeInternalQuery(lsnr, new SerializableFilter(), false, true, true);
        for (int i = 10; i < 20; i++) cache.put(i, i);
        assertTrue(latch.await(3, SECONDS));
    } finally {
        if (uuid != null)
            grid(0).context().cache().cache(cache.getName()).context().continuousQueries().cancelInternalQuery(uuid);
        grid(0).destroyCache(ccfg.getName());
    }
}
Also used : CacheEntryEventSerializableFilter(org.apache.ignite.cache.CacheEntryEventSerializableFilter) CacheEntryUpdatedListener(javax.cache.event.CacheEntryUpdatedListener) UUID(java.util.UUID) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 2 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 3 with CacheEntryUpdatedListener

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

the class CacheContinuousBatchAckTest method checkBackupAcknowledgeMessage.

/**
     * @param ccfg Cache configuration.
     * @throws Exception If failed.
     */
private void checkBackupAcknowledgeMessage(CacheConfiguration<Object, Object> ccfg) throws Exception {
    QueryCursor qry = null;
    IgniteCache<Object, Object> cache = null;
    try {
        ContinuousQuery q = new ContinuousQuery();
        q.setLocalListener(new CacheEntryUpdatedListener() {

            @Override
            public void onUpdated(Iterable iterable) throws CacheEntryListenerException {
            // No-op.
            }
        });
        cache = grid(SERVER).getOrCreateCache(ccfg);
        qry = cache.query(q);
        for (int i = 0; i < 10000; i++) cache.put(i, i);
        assert !GridTestUtils.waitForCondition(new PA() {

            @Override
            public boolean apply() {
                return fail.get();
            }
        }, 1300L);
    } finally {
        if (qry != null)
            qry.close();
        if (cache != null)
            grid(SERVER).destroyCache(cache.getName());
    }
}
Also used : PA(org.apache.ignite.internal.util.typedef.PA) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) CacheEntryUpdatedListener(javax.cache.event.CacheEntryUpdatedListener) CacheEntryListenerException(javax.cache.event.CacheEntryListenerException) QueryCursor(org.apache.ignite.cache.query.QueryCursor)

Example 4 with CacheEntryUpdatedListener

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

the class GridCacheContinuousQueryMultiNodesFilteringTest method testWithNodeFilter.

/**
     * @throws Exception If failed.
     */
public void testWithNodeFilter() throws Exception {
    List<QueryCursor> qryCursors = new ArrayList<>();
    final int nodesCnt = 3;
    startGridsMultiThreaded(nodesCnt);
    awaitPartitionMapExchange();
    CacheConfiguration ccfg = cacheConfiguration(new NodeFilterByRegexp(".*(0|1)$"));
    grid(0).createCache(ccfg);
    final AtomicInteger cntr = new AtomicInteger();
    final ConcurrentMap<ClusterNode, Set<Integer>> maps = new ConcurrentHashMap<>();
    final AtomicBoolean doubleNtfFail = new AtomicBoolean(false);
    CacheEntryUpdatedListener<Integer, Integer> lsnr = new CacheEntryUpdatedListener<Integer, Integer>() {

        @Override
        public void onUpdated(Iterable<CacheEntryEvent<? extends Integer, ? extends Integer>> evts) throws CacheEntryListenerException {
            for (CacheEntryEvent<? extends Integer, ? extends Integer> e : evts) {
                cntr.incrementAndGet();
                ClusterNode node = ((Ignite) e.getSource().unwrap(Ignite.class)).cluster().localNode();
                Set<Integer> set = maps.get(node);
                if (set == null) {
                    set = new ConcurrentSkipListSet<>();
                    Set<Integer> oldVal = maps.putIfAbsent(node, set);
                    set = oldVal != null ? oldVal : set;
                }
                if (!set.add(e.getValue()))
                    doubleNtfFail.set(false);
            }
        }
    };
    for (int i = 0; i < nodesCnt; i++) {
        ContinuousQuery<Integer, Integer> qry = new ContinuousQuery<>();
        qry.setLocalListener(lsnr);
        Ignite ignite = grid(i);
        log.info("Try to start CQ on node: " + ignite.cluster().localNode().id());
        qryCursors.add(ignite.cache(ccfg.getName()).query(qry));
        log.info("CQ started on node: " + ignite.cluster().localNode().id());
    }
    client = true;
    startGrid(nodesCnt);
    awaitPartitionMapExchange();
    ContinuousQuery<Integer, Integer> qry = new ContinuousQuery<>();
    qry.setLocalListener(lsnr);
    qryCursors.add(grid(nodesCnt).cache(ccfg.getName()).query(qry));
    for (int i = 0; i <= nodesCnt; i++) {
        for (int key = 0; key < KEYS; key++) {
            int val = (i * KEYS) + key;
            grid(i).cache(ccfg.getName()).put(val, val);
        }
    }
    assertTrue(GridTestUtils.waitForCondition(new PA() {

        @Override
        public boolean apply() {
            return cntr.get() >= 2 * (nodesCnt + 1) * KEYS;
        }
    }, 5000L));
    assertFalse("Got duplicate", doubleNtfFail.get());
    for (int i = 0; i < (nodesCnt + 1) * KEYS; i++) {
        for (Map.Entry<ClusterNode, Set<Integer>> e : maps.entrySet()) assertTrue("Lost event on node: " + e.getKey().id() + ", event: " + i, e.getValue().remove(i));
    }
    for (Map.Entry<ClusterNode, Set<Integer>> e : maps.entrySet()) assertTrue("Unexpected event on node: " + e.getKey(), e.getValue().isEmpty());
    assertEquals("Not expected count of CQ", nodesCnt + 1, qryCursors.size());
    for (QueryCursor cur : qryCursors) cur.close();
}
Also used : Set(java.util.Set) ConcurrentSkipListSet(java.util.concurrent.ConcurrentSkipListSet) ArrayList(java.util.ArrayList) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) CacheEntryUpdatedListener(javax.cache.event.CacheEntryUpdatedListener) Ignite(org.apache.ignite.Ignite) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) QueryCursor(org.apache.ignite.cache.query.QueryCursor) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) ClusterNode(org.apache.ignite.cluster.ClusterNode) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) PA(org.apache.ignite.internal.util.typedef.PA) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 5 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)

Aggregations

CacheEntryUpdatedListener (javax.cache.event.CacheEntryUpdatedListener)13 ContinuousQuery (org.apache.ignite.cache.query.ContinuousQuery)10 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)7 Ignite (org.apache.ignite.Ignite)6 QueryCursor (org.apache.ignite.cache.query.QueryCursor)6 IgniteCache (org.apache.ignite.IgniteCache)5 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)5 IgniteException (org.apache.ignite.IgniteException)5 Cache (javax.cache.Cache)3 CacheEntryListenerException (javax.cache.event.CacheEntryListenerException)3 IgniteClientDisconnectedException (org.apache.ignite.IgniteClientDisconnectedException)3 IgniteClientDisconnectedCheckedException (org.apache.ignite.internal.IgniteClientDisconnectedCheckedException)3 ClusterTopologyCheckedException (org.apache.ignite.internal.cluster.ClusterTopologyCheckedException)3 IgniteSpiException (org.apache.ignite.spi.IgniteSpiException)3 ArrayList (java.util.ArrayList)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 CyclicBarrier (java.util.concurrent.CyclicBarrier)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 CacheEntryEvent (javax.cache.event.CacheEntryEvent)2 CacheEntryEventSerializableFilter (org.apache.ignite.cache.CacheEntryEventSerializableFilter)2