Search in sources :

Example 11 with CacheEntryEvent

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

the class CacheContinuousQueryFactoryFilterRandomOperationTest method waitAndCheckEvent.

/**
     * @param evtsQueues Event queue.
     * @param partCntrs Partition counters.
     * @param aff Affinity function.
     * @param key Key.
     * @param val Value.
     * @param oldVal Old value.
     * @throws Exception If failed.
     */
private void waitAndCheckEvent(List<BlockingQueue<CacheEntryEvent<?, ?>>> evtsQueues, Map<Integer, Long> partCntrs, Affinity<Object> aff, Object key, Object val, Object oldVal) throws Exception {
    if ((val == null && oldVal == null || (val != null && !isAccepted((QueryTestValue) val)))) {
        checkNoEvent(evtsQueues);
        return;
    }
    for (BlockingQueue<CacheEntryEvent<?, ?>> evtsQueue : evtsQueues) {
        CacheEntryEvent<?, ?> evt = evtsQueue.poll(5, SECONDS);
        assertNotNull("Failed to wait for event [key=" + key + ", val=" + val + ", oldVal=" + oldVal + ']', evt);
        assertEquals(key, evt.getKey());
        assertEquals(val, evt.getValue());
        assertEquals(oldVal, evt.getOldValue());
        long cntr = partCntrs.get(aff.partition(key));
        CacheQueryEntryEvent qryEntryEvt = evt.unwrap(CacheQueryEntryEvent.class);
        assertNotNull(cntr);
        assertNotNull(qryEntryEvt);
        assertEquals(cntr, qryEntryEvt.getPartitionUpdateCounter());
    }
}
Also used : CacheQueryEntryEvent(org.apache.ignite.cache.query.CacheQueryEntryEvent) CacheEntryEvent(javax.cache.event.CacheEntryEvent)

Example 12 with CacheEntryEvent

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

the class CacheContinuousQueryCounterAbstractTest method testAllEntries.

/**
     * @throws Exception If failed.
     */
public void testAllEntries() throws Exception {
    IgniteCache<Integer, Integer> cache = grid(0).cache(CACHE_NAME);
    ContinuousQuery<Integer, Integer> qry = new ContinuousQuery<>();
    final Map<Integer, List<T2<Integer, Long>>> map = new HashMap<>();
    final CountDownLatch latch = new CountDownLatch(5);
    qry.setLocalListener(new CacheEntryUpdatedListener<Integer, Integer>() {

        @Override
        public void onUpdated(Iterable<CacheEntryEvent<? extends Integer, ? extends Integer>> evts) {
            for (CacheEntryEvent<? extends Integer, ? extends Integer> e : evts) {
                synchronized (map) {
                    List<T2<Integer, Long>> vals = map.get(e.getKey());
                    if (vals == null) {
                        vals = new ArrayList<>();
                        map.put(e.getKey(), vals);
                    }
                    vals.add(new T2<>(e.getValue(), e.unwrap(CacheQueryEntryEvent.class).getPartitionUpdateCounter()));
                }
                latch.countDown();
            }
        }
    });
    try (QueryCursor<Cache.Entry<Integer, Integer>> ignored = cache.query(qry)) {
        cache.put(1, 1);
        cache.put(2, 2);
        cache.put(3, 3);
        cache.remove(2);
        cache.put(1, 10);
        assert latch.await(LATCH_TIMEOUT, MILLISECONDS);
        assertEquals(3, map.size());
        List<T2<Integer, Long>> vals = map.get(1);
        assertNotNull(vals);
        assertEquals(2, vals.size());
        assertEquals(1, (int) vals.get(0).get1());
        assertEquals(1L, (long) vals.get(0).get2());
        assertEquals(10, (int) vals.get(1).get1());
        assertEquals(2L, (long) vals.get(1).get2());
        vals = map.get(2);
        assertNotNull(vals);
        assertEquals(2, vals.size());
        assertEquals(2, (int) vals.get(0).get1());
        assertEquals(1L, (long) vals.get(0).get2());
        assertNull(vals.get(1).get1());
        vals = map.get(3);
        assertNotNull(vals);
        assertEquals(1, vals.size());
        assertEquals(3, (int) vals.get(0).get1());
        assertEquals(1L, (long) vals.get(0).get2());
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) CacheEntryEvent(javax.cache.event.CacheEntryEvent) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) ArrayList(java.util.ArrayList) List(java.util.List) T2(org.apache.ignite.internal.util.typedef.T2)

Example 13 with CacheEntryEvent

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

the class CacheContinuousQueryCounterAbstractTest method testTwoQueryListener.

/**
     * @throws Exception If failed.
     */
public void testTwoQueryListener() throws Exception {
    if (cacheMode() == LOCAL)
        return;
    final IgniteCache<Integer, Integer> cache = grid(0).cache(CACHE_NAME);
    final IgniteCache<Integer, Integer> cache1 = grid(1).cache(CACHE_NAME);
    final AtomicInteger cntr = new AtomicInteger(0);
    final AtomicInteger cntr1 = new AtomicInteger(0);
    final ContinuousQuery<Integer, Integer> qry1 = new ContinuousQuery<>();
    final ContinuousQuery<Integer, Integer> qry2 = new ContinuousQuery<>();
    final Map<Integer, List<T2<Integer, Long>>> map1 = new HashMap<>();
    final Map<Integer, List<T2<Integer, Long>>> map2 = new HashMap<>();
    qry1.setLocalListener(new CacheEntryUpdatedListener<Integer, Integer>() {

        @Override
        public void onUpdated(Iterable<CacheEntryEvent<? extends Integer, ? extends Integer>> evts) {
            for (CacheEntryEvent<? extends Integer, ? extends Integer> e : evts) {
                cntr.incrementAndGet();
                synchronized (map1) {
                    List<T2<Integer, Long>> vals = map1.get(e.getKey());
                    if (vals == null) {
                        vals = new ArrayList<>();
                        map1.put(e.getKey(), vals);
                    }
                    vals.add(new T2<>(e.getValue(), e.unwrap(CacheQueryEntryEvent.class).getPartitionUpdateCounter()));
                }
            }
        }
    });
    qry2.setLocalListener(new CacheEntryUpdatedListener<Integer, Integer>() {

        @Override
        public void onUpdated(Iterable<CacheEntryEvent<? extends Integer, ? extends Integer>> evts) {
            for (CacheEntryEvent<? extends Integer, ? extends Integer> e : evts) {
                cntr1.incrementAndGet();
                synchronized (map2) {
                    List<T2<Integer, Long>> vals = map2.get(e.getKey());
                    if (vals == null) {
                        vals = new ArrayList<>();
                        map2.put(e.getKey(), vals);
                    }
                    vals.add(new T2<>(e.getValue(), e.unwrap(CacheQueryEntryEvent.class).getPartitionUpdateCounter()));
                }
            }
        }
    });
    try (QueryCursor<Cache.Entry<Integer, Integer>> query2 = cache1.query(qry2);
        QueryCursor<Cache.Entry<Integer, Integer>> query1 = cache.query(qry1)) {
        for (int i = 0; i < gridCount(); i++) {
            IgniteCache<Object, Object> cache0 = grid(i).cache(CACHE_NAME);
            cache0.put(1, 1);
            cache0.put(2, 2);
            cache0.put(3, 3);
            cache0.remove(1);
            cache0.remove(2);
            cache0.remove(3);
            final int iter = i + 1;
            assert GridTestUtils.waitForCondition(new PA() {

                @Override
                public boolean apply() {
                    return iter * 6 * /* count operation */
                    2 == /* count continues queries*/
                    (cntr.get() + cntr1.get());
                }
            }, 5000L);
            checkEvents(map1, i);
            map1.clear();
            checkEvents(map2, i);
            map2.clear();
        }
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) CacheEntryEvent(javax.cache.event.CacheEntryEvent) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) PA(org.apache.ignite.internal.util.typedef.PA) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ArrayList(java.util.ArrayList) List(java.util.List) T2(org.apache.ignite.internal.util.typedef.T2)

Example 14 with CacheEntryEvent

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

the class CacheContinuousQueryCounterAbstractTest method testLoadCache.

/**
     * @throws Exception If failed.
     */
public void testLoadCache() throws Exception {
    IgniteCache<Integer, Integer> cache = grid(0).cache(CACHE_NAME);
    ContinuousQuery<Integer, Integer> qry = new ContinuousQuery<>();
    final Map<Integer, T2<Integer, Long>> map = new ConcurrentHashMap8<>();
    final CountDownLatch latch = new CountDownLatch(10);
    qry.setLocalListener(new CacheEntryUpdatedListener<Integer, Integer>() {

        @Override
        public void onUpdated(Iterable<CacheEntryEvent<? extends Integer, ? extends Integer>> evts) {
            for (CacheEntryEvent<? extends Integer, ? extends Integer> e : evts) {
                map.put(e.getKey(), new T2<>(e.getValue(), e.unwrap(CacheQueryEntryEvent.class).getPartitionUpdateCounter()));
                latch.countDown();
            }
        }
    });
    try (QueryCursor<Cache.Entry<Integer, Integer>> ignored = cache.query(qry)) {
        cache.loadCache(null, 0);
        assert latch.await(LATCH_TIMEOUT, MILLISECONDS) : "Count: " + latch.getCount();
        assertEquals(10, map.size());
        for (int i = 0; i < 10; i++) {
            assertEquals(i, (int) map.get(i).get1());
            assertEquals((long) 1, (long) map.get(i).get2());
        }
    }
}
Also used : ConcurrentHashMap8(org.jsr166.ConcurrentHashMap8) CountDownLatch(java.util.concurrent.CountDownLatch) CacheEntryEvent(javax.cache.event.CacheEntryEvent) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) T2(org.apache.ignite.internal.util.typedef.T2)

Example 15 with CacheEntryEvent

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

the class CacheContinuousQueryCounterAbstractTest method testRestartQuery.

/**
     * @throws Exception If failed.
     */
public void testRestartQuery() throws Exception {
    IgniteCache<Integer, Integer> cache = grid(0).cache(CACHE_NAME);
    final int keyCnt = 300;
    final int updateKey = 1;
    for (int i = 0; i < keyCnt; i++) cache.put(updateKey, i);
    for (int i = 0; i < 10; i++) {
        if (i % 2 == 0) {
            final AtomicInteger cntr = new AtomicInteger(0);
            ContinuousQuery<Integer, Integer> qry = new ContinuousQuery<>();
            final List<T2<Integer, Long>> vals = new ArrayList<>();
            qry.setLocalListener(new CacheEntryUpdatedListener<Integer, Integer>() {

                @Override
                public void onUpdated(Iterable<CacheEntryEvent<? extends Integer, ? extends Integer>> evts) {
                    for (CacheEntryEvent<? extends Integer, ? extends Integer> e : evts) {
                        synchronized (vals) {
                            cntr.incrementAndGet();
                            vals.add(new T2<>(e.getValue(), e.unwrap(CacheQueryEntryEvent.class).getPartitionUpdateCounter()));
                        }
                    }
                }
            });
            try (QueryCursor<Cache.Entry<Integer, Integer>> ignore = cache.query(qry)) {
                for (int key = 0; key < keyCnt; key++) cache.put(updateKey, cache.get(updateKey) + 1);
                assert GridTestUtils.waitForCondition(new PA() {

                    @Override
                    public boolean apply() {
                        return cntr.get() == keyCnt;
                    }
                }, 2000L);
                synchronized (vals) {
                    for (T2<Integer, Long> val : vals) assertEquals((long) val.get1() + 1, (long) val.get2());
                }
            }
        } else {
            for (int key = 0; key < keyCnt; key++) cache.put(updateKey, cache.get(updateKey) + 1);
        }
    }
}
Also used : ArrayList(java.util.ArrayList) CacheEntryEvent(javax.cache.event.CacheEntryEvent) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) PA(org.apache.ignite.internal.util.typedef.PA) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) T2(org.apache.ignite.internal.util.typedef.T2)

Aggregations

CacheEntryEvent (javax.cache.event.CacheEntryEvent)55 ContinuousQuery (org.apache.ignite.cache.query.ContinuousQuery)41 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)28 CountDownLatch (java.util.concurrent.CountDownLatch)24 ArrayList (java.util.ArrayList)18 CacheEntryListenerException (javax.cache.event.CacheEntryListenerException)14 Ignite (org.apache.ignite.Ignite)13 HashMap (java.util.HashMap)11 List (java.util.List)11 IgniteCache (org.apache.ignite.IgniteCache)8 PA (org.apache.ignite.internal.util.typedef.PA)8 QueryCursor (org.apache.ignite.cache.query.QueryCursor)7 T2 (org.apache.ignite.internal.util.typedef.T2)7 IgniteException (org.apache.ignite.IgniteException)6 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)5 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)5 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)5 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)5 ConcurrentMap (java.util.concurrent.ConcurrentMap)4 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)4