Search in sources :

Example 26 with CacheEntryEvent

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

the class GridCacheContinuousQueryAbstractSelfTest method testLoadCache.

/**
     * @throws Exception If failed.
     */
public void testLoadCache() throws Exception {
    IgniteCache<Integer, Integer> cache = grid(0).cache(DEFAULT_CACHE_NAME);
    ContinuousQuery<Integer, Integer> qry = new ContinuousQuery<>();
    final Map<Integer, Integer> 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(), e.getValue());
                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));
    }
}
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)

Example 27 with CacheEntryEvent

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

the class CacheContinuousQueryRandomOperationsTest 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, Map<Object, Long> evtCntrs, Affinity<Object> aff, Object key, Object val, Object oldVal) throws Exception {
    if (val == null && oldVal == null) {
        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 curPartCntr = partCntrs.get(aff.partition(key));
        Long cntr = evtCntrs.get(key);
        CacheQueryEntryEvent qryEntryEvt = evt.unwrap(CacheQueryEntryEvent.class);
        assertNotNull(cntr);
        assertNotNull(curPartCntr);
        assertNotNull(qryEntryEvt);
        assertTrue(cntr <= curPartCntr);
        assertEquals((long) cntr, qryEntryEvt.getPartitionUpdateCounter());
    }
}
Also used : CacheQueryEntryEvent(org.apache.ignite.cache.query.CacheQueryEntryEvent) CacheEntryEvent(javax.cache.event.CacheEntryEvent)

Example 28 with CacheEntryEvent

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

the class CacheContinuousQueryRandomOperationsTest method doTestContinuousQuery.

/**
     * @param ccfg Cache configuration.
     * @param deploy The place where continuous query will be started.
     * @throws Exception If failed.
     */
protected void doTestContinuousQuery(CacheConfiguration<Object, Object> ccfg, ContinuousDeploy deploy) throws Exception {
    ignite(0).createCache(ccfg);
    try {
        long seed = System.currentTimeMillis();
        Random rnd = new Random(seed);
        log.info("Random seed: " + seed);
        List<BlockingQueue<CacheEntryEvent<?, ?>>> evtsQueues = new ArrayList<>();
        Collection<QueryCursor<?>> curs = new ArrayList<>();
        if (deploy == CLIENT) {
            ContinuousQuery<Object, Object> qry = new ContinuousQuery<>();
            final BlockingQueue<CacheEntryEvent<?, ?>> evtsQueue = new ArrayBlockingQueue<>(50_000);
            qry.setLocalListener(new CacheEntryUpdatedListener<Object, Object>() {

                @Override
                public void onUpdated(Iterable<CacheEntryEvent<?, ?>> evts) {
                    for (CacheEntryEvent<?, ?> evt : evts) evtsQueue.add(evt);
                }
            });
            evtsQueues.add(evtsQueue);
            QueryCursor<?> cur = grid(getClientIndex()).cache(ccfg.getName()).query(qry);
            curs.add(cur);
        } else if (deploy == SERVER) {
            ContinuousQuery<Object, Object> qry = new ContinuousQuery<>();
            final BlockingQueue<CacheEntryEvent<?, ?>> evtsQueue = new ArrayBlockingQueue<>(50_000);
            qry.setLocalListener(new CacheEntryUpdatedListener<Object, Object>() {

                @Override
                public void onUpdated(Iterable<CacheEntryEvent<?, ?>> evts) {
                    for (CacheEntryEvent<?, ?> evt : evts) evtsQueue.add(evt);
                }
            });
            evtsQueues.add(evtsQueue);
            QueryCursor<?> cur = grid(rnd.nextInt(getServerNodeCount())).cache(ccfg.getName()).query(qry);
            curs.add(cur);
        } else {
            for (int i = 0; i <= getServerNodeCount(); i++) {
                ContinuousQuery<Object, Object> qry = new ContinuousQuery<>();
                final BlockingQueue<CacheEntryEvent<?, ?>> evtsQueue = new ArrayBlockingQueue<>(50_000);
                qry.setLocalListener(new CacheEntryUpdatedListener<Object, Object>() {

                    @Override
                    public void onUpdated(Iterable<CacheEntryEvent<?, ?>> evts) {
                        for (CacheEntryEvent<?, ?> evt : evts) evtsQueue.add(evt);
                    }
                });
                evtsQueues.add(evtsQueue);
                QueryCursor<?> cur = ignite(i).cache(ccfg.getName()).query(qry);
                curs.add(cur);
            }
        }
        ConcurrentMap<Object, Object> expData = new ConcurrentHashMap<>();
        Map<Integer, Long> partCntr = new ConcurrentHashMap<>();
        try {
            for (int i = 0; i < ITERATION_CNT; i++) {
                if (i % 20 == 0)
                    log.info("Iteration: " + i);
                for (int idx = 0; idx < getServerNodeCount(); idx++) randomUpdate(rnd, evtsQueues, expData, partCntr, grid(idx).cache(ccfg.getName()));
            }
        } finally {
            for (QueryCursor<?> cur : curs) cur.close();
        }
    } finally {
        ignite(0).destroyCache(ccfg.getName());
    }
}
Also used : CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) CacheEntryEvent(javax.cache.event.CacheEntryEvent) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) Random(java.util.Random) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) CacheEntryUpdatedListener(javax.cache.event.CacheEntryUpdatedListener) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) QueryCursor(org.apache.ignite.cache.query.QueryCursor) BlockingQueue(java.util.concurrent.BlockingQueue) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue)

Example 29 with CacheEntryEvent

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

the class CacheContinuousQueryRandomOperationsTest method batchOperation.

/**
     * @param ccfg Cache configuration.
     * @throws Exception If failed.
     */
private void batchOperation(CacheConfiguration ccfg) throws Exception {
    IgniteCache<QueryTestKey, QueryTestValue> cache = grid(getClientIndex()).createCache(ccfg);
    try {
        ContinuousQuery<QueryTestKey, QueryTestValue> qry = new ContinuousQuery<>();
        final List<CacheEntryEvent<? extends QueryTestKey, ? extends QueryTestValue>> evts = new CopyOnWriteArrayList<>();
        if (noOpFilterFactory() != null)
            qry.setRemoteFilterFactory(noOpFilterFactory());
        qry.setLocalListener(new CacheEntryUpdatedListener<QueryTestKey, QueryTestValue>() {

            @Override
            public void onUpdated(Iterable<CacheEntryEvent<? extends QueryTestKey, ? extends QueryTestValue>> events) throws CacheEntryListenerException {
                for (CacheEntryEvent<? extends QueryTestKey, ? extends QueryTestValue> e : events) evts.add(e);
            }
        });
        Map<QueryTestKey, QueryTestValue> map = new TreeMap<>();
        for (int i = 0; i < KEYS; i++) map.put(new QueryTestKey(i), new QueryTestValue(i));
        try (QueryCursor qryCur = cache.query(qry)) {
            for (int i = 0; i < ITERATION_CNT / 2; i++) {
                log.info("Start iteration: " + i);
                // Not events.
                cache.removeAll(map.keySet());
                cache.invokeAll(map.keySet(), (EntryProcessor<QueryTestKey, QueryTestValue, ? extends Object>) (Object) new EntrySetValueProcessor(null, false));
                cache.invokeAll(map.keySet(), (EntryProcessor<QueryTestKey, QueryTestValue, ? extends Object>) (Object) new EntrySetValueProcessor(true));
                // Get events.
                cache.putAll(map);
                assert GridTestUtils.waitForCondition(new PA() {

                    @Override
                    public boolean apply() {
                        return evts.size() == KEYS;
                    }
                }, 5_000);
                checkEvents(evts, CREATED);
                evts.clear();
                // Not events.
                cache.invokeAll(map.keySet(), (EntryProcessor<QueryTestKey, QueryTestValue, ? extends Object>) (Object) new EntrySetValueProcessor(true));
                U.sleep(100);
                assertEquals(0, evts.size());
                // Get events.
                cache.invokeAll(map.keySet(), (EntryProcessor<QueryTestKey, QueryTestValue, ? extends Object>) (Object) new EntrySetValueProcessor(null, false));
                // Not events.
                cache.removeAll(map.keySet());
                cache.removeAll(map.keySet());
                assert GridTestUtils.waitForCondition(new PA() {

                    @Override
                    public boolean apply() {
                        return evts.size() == KEYS;
                    }
                }, 5_000);
                checkEvents(evts, REMOVED);
                evts.clear();
                log.info("Finish iteration: " + i);
            }
        }
    } finally {
        grid(getClientIndex()).destroyCache(ccfg.getName());
    }
}
Also used : CacheEntryListenerException(javax.cache.event.CacheEntryListenerException) TreeMap(java.util.TreeMap) CacheEntryEvent(javax.cache.event.CacheEntryEvent) PA(org.apache.ignite.internal.util.typedef.PA) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) QueryCursor(org.apache.ignite.cache.query.QueryCursor) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList)

Example 30 with CacheEntryEvent

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

the class CacheContinuousQueryRandomOperationsTest method singleOperation.

/**
     * @param ccfg Cache configuration.
     * @throws Exception If failed.
     */
private void singleOperation(CacheConfiguration ccfg) throws Exception {
    IgniteCache<QueryTestKey, QueryTestValue> cache = grid(getClientIndex()).createCache(ccfg);
    try {
        ContinuousQuery<QueryTestKey, QueryTestValue> qry = new ContinuousQuery<>();
        final List<CacheEntryEvent<? extends QueryTestKey, ? extends QueryTestValue>> evts = new CopyOnWriteArrayList<>();
        if (noOpFilterFactory() != null)
            qry.setRemoteFilterFactory(noOpFilterFactory());
        qry.setLocalListener(new CacheEntryUpdatedListener<QueryTestKey, QueryTestValue>() {

            @Override
            public void onUpdated(Iterable<CacheEntryEvent<? extends QueryTestKey, ? extends QueryTestValue>> events) throws CacheEntryListenerException {
                for (CacheEntryEvent<? extends QueryTestKey, ? extends QueryTestValue> e : events) evts.add(e);
            }
        });
        QueryTestKey key = new QueryTestKey(1);
        try (QueryCursor qryCur = cache.query(qry)) {
            for (int i = 0; i < ITERATION_CNT; i++) {
                log.info("Start iteration: " + i);
                // Not events.
                cache.invoke(key, (EntryProcessor<QueryTestKey, QueryTestValue, ? extends Object>) (Object) new EntrySetValueProcessor(true));
                // Get events.
                cache.put(key, new QueryTestValue(1));
                cache.remove(key);
                // Not events.
                cache.invoke(key, (EntryProcessor<QueryTestKey, QueryTestValue, ? extends Object>) (Object) new EntrySetValueProcessor(null, false));
                cache.invoke(key, (EntryProcessor<QueryTestKey, QueryTestValue, ? extends Object>) (Object) new EntrySetValueProcessor(null, false));
                cache.invoke(key, (EntryProcessor<QueryTestKey, QueryTestValue, ? extends Object>) (Object) new EntrySetValueProcessor(true));
                cache.remove(key);
                // Get events.
                cache.put(key, new QueryTestValue(2));
                // Not events.
                cache.invoke(key, (EntryProcessor<QueryTestKey, QueryTestValue, ? extends Object>) (Object) new EntrySetValueProcessor(true));
                // Get events.
                cache.invoke(key, (EntryProcessor<QueryTestKey, QueryTestValue, ? extends Object>) (Object) new EntrySetValueProcessor(null, false));
                // Not events.
                cache.remove(key);
                // Get events.
                cache.put(key, new QueryTestValue(3));
                cache.put(key, new QueryTestValue(4));
                // Not events.
                cache.invoke(key, (EntryProcessor<QueryTestKey, QueryTestValue, ? extends Object>) (Object) new EntrySetValueProcessor(true));
                cache.putIfAbsent(key, new QueryTestValue(5));
                cache.putIfAbsent(key, new QueryTestValue(5));
                cache.putIfAbsent(key, new QueryTestValue(5));
                cache.invoke(key, (EntryProcessor<QueryTestKey, QueryTestValue, ? extends Object>) (Object) new EntrySetValueProcessor(true));
                cache.remove(key, new QueryTestValue(5));
                // Get events.
                cache.remove(key, new QueryTestValue(4));
                cache.putIfAbsent(key, new QueryTestValue(5));
                // Not events.
                cache.replace(key, new QueryTestValue(3), new QueryTestValue(2));
                cache.replace(key, new QueryTestValue(3), new QueryTestValue(2));
                cache.replace(key, new QueryTestValue(3), new QueryTestValue(2));
                // Get events.
                cache.replace(key, new QueryTestValue(5), new QueryTestValue(6));
                assert GridTestUtils.waitForCondition(new PA() {

                    @Override
                    public boolean apply() {
                        return evts.size() == 9;
                    }
                }, 5_000);
                checkSingleEvent(evts.get(0), CREATED, new QueryTestValue(1), null);
                checkSingleEvent(evts.get(1), REMOVED, null, new QueryTestValue(1));
                checkSingleEvent(evts.get(2), CREATED, new QueryTestValue(2), null);
                checkSingleEvent(evts.get(3), REMOVED, null, new QueryTestValue(2));
                checkSingleEvent(evts.get(4), CREATED, new QueryTestValue(3), null);
                checkSingleEvent(evts.get(5), EventType.UPDATED, new QueryTestValue(4), new QueryTestValue(3));
                checkSingleEvent(evts.get(6), REMOVED, null, new QueryTestValue(4));
                checkSingleEvent(evts.get(7), CREATED, new QueryTestValue(5), null);
                checkSingleEvent(evts.get(8), EventType.UPDATED, new QueryTestValue(6), new QueryTestValue(5));
                evts.clear();
                cache.remove(key);
                cache.remove(key);
                assert GridTestUtils.waitForCondition(new PA() {

                    @Override
                    public boolean apply() {
                        return evts.size() == 1;
                    }
                }, 5_000);
                evts.clear();
                log.info("Finish iteration: " + i);
            }
        }
    } finally {
        grid(getClientIndex()).destroyCache(ccfg.getName());
    }
}
Also used : CacheEntryListenerException(javax.cache.event.CacheEntryListenerException) CacheEntryEvent(javax.cache.event.CacheEntryEvent) PA(org.apache.ignite.internal.util.typedef.PA) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) QueryCursor(org.apache.ignite.cache.query.QueryCursor) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList)

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