Search in sources :

Example 21 with CacheEntryEvent

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

the class GridCacheContinuousQueryAbstractSelfTest method testNodeJoinWithoutCache.

/**
     * @throws Exception If failed.
     */
@SuppressWarnings("TryFinallyCanBeTryWithResources")
public void testNodeJoinWithoutCache() throws Exception {
    IgniteCache<Integer, Integer> cache = grid(0).cache(DEFAULT_CACHE_NAME);
    ContinuousQuery<Integer, Integer> qry = new ContinuousQuery<>();
    final CountDownLatch latch = new CountDownLatch(1);
    qry.setLocalListener(new CacheEntryUpdatedListener<Integer, Integer>() {

        @Override
        public void onUpdated(Iterable<CacheEntryEvent<? extends Integer, ? extends Integer>> evts) {
            latch.countDown();
        }
    });
    QueryCursor<Cache.Entry<Integer, Integer>> cur = cache.query(qry);
    try {
        try (Ignite ignite = startGrid(NO_CACHE_IGNITE_INSTANCE_NAME)) {
            log.info("Started node without cache: " + ignite);
        }
        cache.put(1, 1);
        assertTrue(latch.await(5000, MILLISECONDS));
    } finally {
        cur.close();
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) Ignite(org.apache.ignite.Ignite) CountDownLatch(java.util.concurrent.CountDownLatch) CacheEntryEvent(javax.cache.event.CacheEntryEvent)

Example 22 with CacheEntryEvent

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

the class GridCacheContinuousQueryReplicatedTxOneNodeTest method doTestOneNode.

/**
     * @throws Exception If failed.
     */
private void doTestOneNode(boolean loc) throws Exception {
    try {
        IgniteCache<String, Integer> cache = startGrid(0).cache(DEFAULT_CACHE_NAME);
        ContinuousQuery<String, Integer> qry = new ContinuousQuery<>();
        final AtomicInteger cnt = new AtomicInteger();
        final CountDownLatch latch = new CountDownLatch(10);
        for (int i = 0; i < 10; i++) cache.put("key" + i, i);
        cache.clear();
        qry.setLocalListener(new CacheEntryUpdatedListener<String, Integer>() {

            @Override
            public void onUpdated(Iterable<CacheEntryEvent<? extends String, ? extends Integer>> evts) throws CacheEntryListenerException {
                for (CacheEntryEvent<? extends String, ? extends Integer> evt : evts) {
                    cnt.incrementAndGet();
                    latch.countDown();
                }
            }
        });
        cache.query(qry.setLocal(loc));
        for (int i = 0; i < 10; i++) cache.put("key" + i, i);
        assert latch.await(5000, TimeUnit.MILLISECONDS);
        assertEquals(10, cnt.get());
    } finally {
        stopAllGrids();
    }
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) CacheEntryListenerException(javax.cache.event.CacheEntryListenerException) CacheEntryEvent(javax.cache.event.CacheEntryEvent) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Example 23 with CacheEntryEvent

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

the class GridCacheContinuousQueryAbstractSelfTest method testBuffering.

/**
     * @throws Exception If failed.
     */
public void testBuffering() throws Exception {
    if (grid(0).cache(DEFAULT_CACHE_NAME).getConfiguration(CacheConfiguration.class).getCacheMode() != PARTITIONED)
        return;
    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(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<Integer> vals = map.get(e.getKey());
                    if (vals == null) {
                        vals = new ArrayList<>();
                        map.put(e.getKey(), vals);
                    }
                    vals.add(e.getValue());
                }
                latch.countDown();
            }
        }
    });
    qry.setPageSize(5);
    try (QueryCursor<Cache.Entry<Integer, Integer>> ignored = cache.query(qry)) {
        ClusterNode node = F.first(grid(0).cluster().forRemotes().nodes());
        Collection<Integer> keys = new HashSet<>();
        int key = 0;
        while (true) {
            ClusterNode n = grid(0).affinity(DEFAULT_CACHE_NAME).mapKeyToNode(key);
            assert n != null;
            if (n.equals(node))
                keys.add(key);
            key++;
            if (keys.size() == 6)
                break;
        }
        Iterator<Integer> it = keys.iterator();
        for (int i = 0; i < 4; i++) cache.put(it.next(), 0);
        assert !latch.await(2, SECONDS);
        for (int i = 0; i < 2; i++) cache.put(it.next(), 0);
        assert latch.await(LATCH_TIMEOUT, MILLISECONDS);
        assertEquals(5, map.size());
        it = keys.iterator();
        for (int i = 0; i < 5; i++) {
            Integer k = it.next();
            List<Integer> vals = map.get(k);
            assertNotNull(vals);
            assertEquals(1, vals.size());
            assertEquals(0, (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) HashSet(java.util.HashSet)

Example 24 with CacheEntryEvent

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

the class GridCacheContinuousQueryAbstractSelfTest method testTimeInterval.

/**
     * @throws Exception If failed.
     */
public void testTimeInterval() throws Exception {
    IgniteCache<Integer, Integer> cache = grid(0).cache(DEFAULT_CACHE_NAME);
    if (cache.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(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<Integer> vals = map.get(e.getKey());
                    if (vals == null) {
                        vals = new ArrayList<>();
                        map.put(e.getKey(), vals);
                    }
                    vals.add(e.getValue());
                }
                latch.countDown();
            }
        }
    });
    qry.setPageSize(10);
    qry.setTimeInterval(3000);
    try (QueryCursor<Cache.Entry<Integer, Integer>> ignored = cache.query(qry)) {
        ClusterNode node = F.first(grid(0).cluster().forRemotes().nodes());
        Collection<Integer> keys = new HashSet<>();
        int key = 0;
        while (true) {
            ClusterNode n = grid(0).affinity(DEFAULT_CACHE_NAME).mapKeyToNode(key);
            assert n != null;
            if (n.equals(node))
                keys.add(key);
            key++;
            if (keys.size() == 5)
                break;
        }
        for (Integer k : keys) cache.put(k, 0);
        assert !latch.await(2, SECONDS);
        assert latch.await(1000 + LATCH_TIMEOUT, MILLISECONDS);
        assertEquals(5, map.size());
        Iterator<Integer> it = keys.iterator();
        for (int i = 0; i < 5; i++) {
            Integer k = it.next();
            List<Integer> vals = map.get(k);
            assertNotNull(vals);
            assertEquals(1, vals.size());
            assertEquals(0, (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) HashSet(java.util.HashSet)

Example 25 with CacheEntryEvent

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

the class GridCacheContinuousQueryAbstractSelfTest method testExpired.

/**
     * @throws Exception If failed.
     */
public void testExpired() throws Exception {
    IgniteCache<Object, Object> cache = grid(0).cache(DEFAULT_CACHE_NAME).withExpiryPolicy(new CreatedExpiryPolicy(new Duration(MILLISECONDS, 1000)));
    final Map<Object, Object> map = new ConcurrentHashMap8<>();
    final CountDownLatch latch = new CountDownLatch(2);
    ContinuousQuery<Object, Object> qry = new ContinuousQuery<>();
    qry.setIncludeExpired(true);
    qry.setLocalListener(new CacheEntryUpdatedListener<Object, Object>() {

        @Override
        public void onUpdated(Iterable<CacheEntryEvent<?, ?>> evts) {
            for (CacheEntryEvent<?, ?> e : evts) {
                if (e.getEventType() == EventType.EXPIRED) {
                    assertNull(e.getValue());
                    map.put(e.getKey(), e.getOldValue());
                    latch.countDown();
                }
            }
        }
    });
    try (QueryCursor<Cache.Entry<Object, Object>> ignored = cache.query(qry)) {
        cache.put(1, 1);
        cache.put(2, 2);
        // Wait for expiration.
        Thread.sleep(2000);
        assert latch.await(LATCH_TIMEOUT, MILLISECONDS);
        assertEquals(2, map.size());
        assertEquals(1, (int) map.get(1));
        assertEquals(2, (int) map.get(2));
    }
}
Also used : ConcurrentHashMap8(org.jsr166.ConcurrentHashMap8) Duration(javax.cache.expiry.Duration) CreatedExpiryPolicy(javax.cache.expiry.CreatedExpiryPolicy) CountDownLatch(java.util.concurrent.CountDownLatch) CacheEntryEvent(javax.cache.event.CacheEntryEvent) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery)

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