Search in sources :

Example 16 with T2

use of org.apache.ignite.internal.util.typedef.T2 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 17 with T2

use of org.apache.ignite.internal.util.typedef.T2 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 18 with T2

use of org.apache.ignite.internal.util.typedef.T2 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 19 with T2

use of org.apache.ignite.internal.util.typedef.T2 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)

Example 20 with T2

use of org.apache.ignite.internal.util.typedef.T2 in project ignite by apache.

the class CacheContinuousQueryOperationFromCallbackTest method doTest.

/**
     * @param ccfg Cache configuration.
     * @throws Exception If failed.
     */
protected void doTest(final CacheConfiguration ccfg, boolean fromLsnr) throws Exception {
    ignite(0).createCache(ccfg);
    List<QueryCursor<?>> qries = new ArrayList<>();
    assertEquals(0, filterCbCntr.get());
    try {
        List<Set<T2<QueryTestKey, QueryTestValue>>> rcvdEvts = new ArrayList<>(NODES);
        List<Set<T2<QueryTestKey, QueryTestValue>>> evtsFromCallbacks = new ArrayList<>(NODES);
        final AtomicInteger qryCntr = new AtomicInteger(0);
        final AtomicInteger cbCntr = new AtomicInteger(0);
        final int threadCnt = SYSTEM_POOL_SIZE * 2;
        for (int idx = 0; idx < NODES; idx++) {
            Set<T2<QueryTestKey, QueryTestValue>> evts = Collections.newSetFromMap(new ConcurrentHashMap<T2<QueryTestKey, QueryTestValue>, Boolean>());
            Set<T2<QueryTestKey, QueryTestValue>> evtsFromCb = Collections.newSetFromMap(new ConcurrentHashMap<T2<QueryTestKey, QueryTestValue>, Boolean>());
            IgniteCache<Object, Object> cache = grid(idx).getOrCreateCache(ccfg.getName());
            ContinuousQuery qry = new ContinuousQuery();
            qry.setLocalListener(new TestCacheAsyncEventListener(evts, evtsFromCb, fromLsnr ? cache : null, qryCntr, cbCntr));
            if (!fromLsnr)
                qry.setRemoteFilterFactory(FactoryBuilder.factoryOf(new CacheTestRemoteFilterAsync(ccfg.getName())));
            rcvdEvts.add(evts);
            evtsFromCallbacks.add(evtsFromCb);
            QueryCursor qryCursor = cache.query(qry);
            qries.add(qryCursor);
        }
        IgniteInternalFuture<Long> f = GridTestUtils.runMultiThreadedAsync(new Runnable() {

            @Override
            public void run() {
                ThreadLocalRandom rnd = ThreadLocalRandom.current();
                for (int i = 0; i < ITERATION_CNT; i++) {
                    IgniteCache<QueryTestKey, QueryTestValue> cache = grid(rnd.nextInt(NODES)).cache(ccfg.getName());
                    QueryTestKey key = new QueryTestKey(rnd.nextInt(KEYS));
                    boolean startTx = cache.getConfiguration(CacheConfiguration.class).getAtomicityMode() == TRANSACTIONAL && rnd.nextBoolean();
                    Transaction tx = null;
                    if (startTx)
                        tx = cache.unwrap(Ignite.class).transactions().txStart();
                    try {
                        if ((cache.get(key) == null) || rnd.nextBoolean())
                            cache.invoke(key, new IncrementTestEntryProcessor());
                        else {
                            QueryTestValue val;
                            QueryTestValue newVal;
                            do {
                                val = cache.get(key);
                                newVal = val == null ? new QueryTestValue(0) : new QueryTestValue(val.val1 + 1);
                            } while (!cache.replace(key, val, newVal));
                        }
                    } finally {
                        if (tx != null)
                            tx.commit();
                    }
                }
            }
        }, threadCnt, "put-thread");
        f.get(30, TimeUnit.SECONDS);
        assert GridTestUtils.waitForCondition(new PA() {

            @Override
            public boolean apply() {
                return qryCntr.get() >= ITERATION_CNT * threadCnt * NODES;
            }
        }, TimeUnit.MINUTES.toMillis(2));
        for (Set<T2<QueryTestKey, QueryTestValue>> set : rcvdEvts) checkEvents(set, ITERATION_CNT * threadCnt, grid(0).cache(ccfg.getName()), false);
        if (fromLsnr) {
            final int expCnt = qryCntr.get() * NODES * KEYS_FROM_CALLBACK;
            boolean res = GridTestUtils.waitForCondition(new PA() {

                @Override
                public boolean apply() {
                    return cbCntr.get() >= expCnt;
                }
            }, TimeUnit.SECONDS.toMillis(60));
            assertTrue("Failed to wait events [exp=" + expCnt + ", act=" + cbCntr.get() + "]", res);
            assertEquals(expCnt, cbCntr.get());
            for (Set<T2<QueryTestKey, QueryTestValue>> set : evtsFromCallbacks) checkEvents(set, qryCntr.get() * KEYS_FROM_CALLBACK, grid(0).cache(ccfg.getName()), true);
        } else {
            final int expInvkCnt = ITERATION_CNT * threadCnt * (ccfg.getCacheMode() != REPLICATED ? (ccfg.getBackups() + 1) : NODES - 1) * NODES;
            GridTestUtils.waitForCondition(new PA() {

                @Override
                public boolean apply() {
                    return filterCbCntr.get() >= expInvkCnt;
                }
            }, TimeUnit.SECONDS.toMillis(60));
            assertEquals(expInvkCnt, filterCbCntr.get());
            for (Set<T2<QueryTestKey, QueryTestValue>> set : evtsFromCallbacks) checkEvents(set, expInvkCnt * KEYS_FROM_CALLBACK, grid(0).cache(ccfg.getName()), true);
        }
    } finally {
        for (QueryCursor<?> qry : qries) qry.close();
        ignite(0).destroyCache(ccfg.getName());
    }
}
Also used : Set(java.util.Set) LinkedHashSet(java.util.LinkedHashSet) ArrayList(java.util.ArrayList) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Ignite(org.apache.ignite.Ignite) QueryCursor(org.apache.ignite.cache.query.QueryCursor) T2(org.apache.ignite.internal.util.typedef.T2) IgniteCache(org.apache.ignite.IgniteCache) PA(org.apache.ignite.internal.util.typedef.PA) Transaction(org.apache.ignite.transactions.Transaction) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Aggregations

T2 (org.apache.ignite.internal.util.typedef.T2)64 ArrayList (java.util.ArrayList)25 HashMap (java.util.HashMap)24 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)19 Map (java.util.Map)15 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)15 ClusterNode (org.apache.ignite.cluster.ClusterNode)14 UUID (java.util.UUID)13 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)13 Ignite (org.apache.ignite.Ignite)13 ContinuousQuery (org.apache.ignite.cache.query.ContinuousQuery)12 List (java.util.List)11 HashSet (java.util.HashSet)10 ConcurrentMap (java.util.concurrent.ConcurrentMap)10 AffinityTopologyVersion (org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion)8 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)7 CountDownLatch (java.util.concurrent.CountDownLatch)7 CacheException (javax.cache.CacheException)7 CacheEntryEvent (javax.cache.event.CacheEntryEvent)7 Set (java.util.Set)5