Search in sources :

Example 16 with CacheEntryEvent

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

the class GridServiceContinuousQueryRedeployTest method testServiceRedeploymentAfterCancel.

/**
     * @throws Exception If failed.
     */
public void testServiceRedeploymentAfterCancel() throws Exception {
    final Ignite ignite = startGrid(0);
    final IgniteCache<Object, Object> managementCache = ignite.getOrCreateCache(CACHE_NAME);
    final ContinuousQuery<Object, Object> qry = new ContinuousQuery<>();
    final List<Object> evts = Collections.synchronizedList(new ArrayList<>());
    qry.setLocalListener(new CacheEntryUpdatedListener<Object, Object>() {

        @Override
        public void onUpdated(Iterable<CacheEntryEvent<?, ?>> iterable) throws CacheEntryListenerException {
            for (CacheEntryEvent<?, ?> event : iterable) evts.add(event);
        }
    });
    int iterations = 100;
    while (iterations-- > 0) {
        QueryCursor quorumCursor = managementCache.query(qry);
        IgniteInternalFuture<?> fut1 = GridTestUtils.runAsync(new Callable<Object>() {

            @Override
            public Object call() throws Exception {
                System.out.println("Deploy " + SERVICE_NAME);
                deployService(ignite);
                return null;
            }
        });
        IgniteInternalFuture<?> fut2 = GridTestUtils.runAsync(new Callable<Object>() {

            @Override
            public Object call() throws Exception {
                System.out.println("Undeploy " + SERVICE_NAME);
                ignite.services().cancel(SERVICE_NAME);
                return null;
            }
        });
        fut1.get();
        fut2.get();
        U.sleep(100);
        assert evts.size() <= 1 : evts.size();
        ignite.services().cancel("service1");
        evts.clear();
        quorumCursor.close();
    }
}
Also used : CacheEntryListenerException(javax.cache.event.CacheEntryListenerException) CacheEntryEvent(javax.cache.event.CacheEntryEvent) CacheEntryListenerException(javax.cache.event.CacheEntryListenerException) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) Ignite(org.apache.ignite.Ignite) QueryCursor(org.apache.ignite.cache.query.QueryCursor)

Example 17 with CacheEntryEvent

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

the class GridCacheContinuousQueryAbstractSelfTest method testInitialQueryAndUpdates.

/**
     * @throws Exception If failed.
     */
public void testInitialQueryAndUpdates() throws Exception {
    IgniteCache<Integer, Integer> cache = grid(0).cache(DEFAULT_CACHE_NAME);
    ContinuousQuery<Integer, Integer> qry = new ContinuousQuery<>();
    qry.setInitialQuery(new ScanQuery<>(new P2<Integer, Integer>() {

        @Override
        public boolean apply(Integer k, Integer v) {
            return k >= 5;
        }
    }));
    final Map<Integer, Integer> map = new ConcurrentHashMap8<>();
    final CountDownLatch latch = new CountDownLatch(2);
    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();
            }
        }
    });
    for (int i = 0; i < 10; i++) cache.put(i, i);
    try (QueryCursor<Cache.Entry<Integer, Integer>> cur = cache.query(qry)) {
        List<Cache.Entry<Integer, Integer>> res = cur.getAll();
        Collections.sort(res, new Comparator<Cache.Entry<Integer, Integer>>() {

            @Override
            public int compare(Cache.Entry<Integer, Integer> e1, Cache.Entry<Integer, Integer> e2) {
                return e1.getKey().compareTo(e2.getKey());
            }
        });
        assertEquals(5, res.size());
        int exp = 5;
        for (Cache.Entry<Integer, Integer> e : res) {
            assertEquals(exp, e.getKey().intValue());
            assertEquals(exp, e.getValue().intValue());
            exp++;
        }
        cache.put(10, 10);
        cache.put(11, 11);
        assert latch.await(LATCH_TIMEOUT, MILLISECONDS) : latch.getCount();
        assertEquals(2, map.size());
        for (int i = 11; i < 12; i++) assertEquals(i, (int) map.get(i));
    }
}
Also used : P2(org.apache.ignite.internal.util.typedef.P2) 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) Cache(javax.cache.Cache) IgniteCache(org.apache.ignite.IgniteCache)

Example 18 with CacheEntryEvent

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

the class GridCacheContinuousQueryAbstractSelfTest method testLocalNodeOnly.

/**
     * @throws Exception If failed.
     */
public void testLocalNodeOnly() throws Exception {
    IgniteCache<Integer, Integer> cache = grid(0).cache(DEFAULT_CACHE_NAME);
    if (grid(0).cache(DEFAULT_CACHE_NAME).getConfiguration(CacheConfiguration.class).getCacheMode() != PARTITIONED)
        return;
    ContinuousQuery<Integer, Integer> qry = new ContinuousQuery<>();
    final Map<Integer, List<Integer>> map = new HashMap<>();
    final CountDownLatch latch = new CountDownLatch(1);
    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<Integer> vals = map.get(e.getKey());
                    if (vals == null) {
                        vals = new ArrayList<>();
                        map.put(e.getKey(), vals);
                    }
                    vals.add(e.getValue());
                }
                latch.countDown();
            }
        }
    });
    try (QueryCursor<Cache.Entry<Integer, Integer>> ignored = cache.query(qry.setLocal(true))) {
        int locKey = -1;
        int rmtKey = -1;
        int key = 0;
        while (true) {
            ClusterNode n = grid(0).affinity(DEFAULT_CACHE_NAME).mapKeyToNode(key);
            assert n != null;
            if (n.equals(grid(0).localNode()))
                locKey = key;
            else
                rmtKey = key;
            key++;
            if (locKey >= 0 && rmtKey >= 0)
                break;
        }
        cache.put(locKey, 1);
        cache.put(rmtKey, 2);
        assert latch.await(LATCH_TIMEOUT, MILLISECONDS);
        assertEquals(1, map.size());
        List<Integer> vals = map.get(locKey);
        assertNotNull(vals);
        assertEquals(1, vals.size());
        assertEquals(1, (int) vals.get(0));
    }
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) 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) List(java.util.List) ArrayList(java.util.ArrayList)

Example 19 with CacheEntryEvent

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

the class GridCacheContinuousQueryAbstractSelfTest method testEntriesByFilter.

/**
     * @throws Exception If failed.
     */
public void testEntriesByFilter() throws Exception {
    IgniteCache<Integer, Integer> cache = grid(0).cache(DEFAULT_CACHE_NAME);
    ContinuousQuery<Integer, Integer> qry = new ContinuousQuery<>();
    final Map<Integer, List<Integer>> map = new HashMap<>();
    final CountDownLatch latch = new CountDownLatch(4);
    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<Integer> vals = map.get(e.getKey());
                    if (vals == null) {
                        vals = new ArrayList<>();
                        map.put(e.getKey(), vals);
                    }
                    vals.add(e.getValue());
                }
                latch.countDown();
            }
        }
    });
    qry.setRemoteFilter(new CacheEntryEventSerializableFilter<Integer, Integer>() {

        @Override
        public boolean evaluate(CacheEntryEvent<? extends Integer, ? extends Integer> evt) {
            return evt.getKey() > 2;
        }
    });
    try (QueryCursor<Cache.Entry<Integer, Integer>> ignored = cache.query(qry)) {
        cache.put(1, 1);
        cache.put(2, 2);
        cache.put(3, 3);
        cache.put(4, 4);
        cache.remove(2);
        cache.remove(3);
        cache.put(1, 10);
        cache.put(4, 40);
        assert latch.await(LATCH_TIMEOUT, MILLISECONDS);
        assertEquals(2, map.size());
        List<Integer> vals = map.get(3);
        assertNotNull(vals);
        assertEquals(2, vals.size());
        assertEquals(3, (int) vals.get(0));
        assertNull(vals.get(1));
        vals = map.get(4);
        assertNotNull(vals);
        assertEquals(2, vals.size());
        assertEquals(4, (int) vals.get(0));
        assertEquals(40, (int) vals.get(1));
    }
}
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) List(java.util.List) ArrayList(java.util.ArrayList)

Example 20 with CacheEntryEvent

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

the class GridCacheContinuousQueryAbstractSelfTest method testEvents.

/**
     * @throws Exception If failed.
     */
public void testEvents() throws Exception {
    final AtomicInteger cnt = new AtomicInteger();
    final CountDownLatch latch = new CountDownLatch(50);
    final CountDownLatch execLatch = new CountDownLatch(cacheMode() == REPLICATED ? 1 : gridCount());
    IgnitePredicate<Event> lsnr = new IgnitePredicate<Event>() {

        @Override
        public boolean apply(Event evt) {
            assert evt instanceof CacheQueryReadEvent;
            CacheQueryReadEvent qe = (CacheQueryReadEvent) evt;
            assertEquals(CONTINUOUS.name(), qe.queryType());
            assertEquals(DEFAULT_CACHE_NAME, qe.cacheName());
            assertEquals(grid(0).localNode().id(), qe.subjectId());
            assertNull(qe.className());
            assertNull(qe.clause());
            assertNull(qe.scanQueryFilter());
            assertNotNull(qe.continuousQueryFilter());
            assertNull(qe.arguments());
            cnt.incrementAndGet();
            latch.countDown();
            return true;
        }
    };
    IgnitePredicate<Event> execLsnr = new IgnitePredicate<Event>() {

        @Override
        public boolean apply(Event evt) {
            assert evt instanceof CacheQueryExecutedEvent;
            CacheQueryExecutedEvent qe = (CacheQueryExecutedEvent) evt;
            assertEquals(CONTINUOUS.name(), qe.queryType());
            assertEquals(DEFAULT_CACHE_NAME, qe.cacheName());
            assertEquals(grid(0).localNode().id(), qe.subjectId());
            assertNull(qe.className());
            assertNull(qe.clause());
            assertNull(qe.scanQueryFilter());
            assertNotNull(qe.continuousQueryFilter());
            assertNull(qe.arguments());
            execLatch.countDown();
            return true;
        }
    };
    try {
        for (int i = 0; i < gridCount(); i++) {
            grid(i).events().localListen(lsnr, EVT_CACHE_QUERY_OBJECT_READ);
            grid(i).events().localListen(execLsnr, EVT_CACHE_QUERY_EXECUTED);
        }
        IgniteCache<Integer, Integer> cache = grid(0).cache(DEFAULT_CACHE_NAME);
        ContinuousQuery<Integer, Integer> qry = new ContinuousQuery<>();
        qry.setLocalListener(new CacheEntryUpdatedListener<Integer, Integer>() {

            @Override
            public void onUpdated(Iterable<CacheEntryEvent<? extends Integer, ? extends Integer>> evts) {
            // No-op.
            }
        });
        qry.setRemoteFilter(new CacheEntryEventSerializableFilter<Integer, Integer>() {

            @Override
            public boolean evaluate(CacheEntryEvent<? extends Integer, ? extends Integer> evt) {
                return evt.getValue() >= 50;
            }
        });
        try (QueryCursor<Cache.Entry<Integer, Integer>> ignored = cache.query(qry)) {
            for (int i = 0; i < 100; i++) cache.put(i, i);
            assert latch.await(LATCH_TIMEOUT, MILLISECONDS);
            assert execLatch.await(LATCH_TIMEOUT, MILLISECONDS);
            assertEquals(50, cnt.get());
        }
    } finally {
        for (int i = 0; i < gridCount(); i++) {
            grid(i).events().stopLocalListen(lsnr, EVT_CACHE_QUERY_OBJECT_READ);
            grid(i).events().stopLocalListen(execLsnr, EVT_CACHE_QUERY_EXECUTED);
        }
    }
}
Also used : CacheQueryReadEvent(org.apache.ignite.events.CacheQueryReadEvent) IgnitePredicate(org.apache.ignite.lang.IgnitePredicate) CountDownLatch(java.util.concurrent.CountDownLatch) CacheEntryEvent(javax.cache.event.CacheEntryEvent) CacheQueryExecutedEvent(org.apache.ignite.events.CacheQueryExecutedEvent) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CacheQueryExecutedEvent(org.apache.ignite.events.CacheQueryExecutedEvent) Event(org.apache.ignite.events.Event) CacheEntryEvent(javax.cache.event.CacheEntryEvent) CacheQueryReadEvent(org.apache.ignite.events.CacheQueryReadEvent)

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