Search in sources :

Example 6 with ContinuousQuery

use of org.apache.ignite.cache.query.ContinuousQuery in project ignite by apache.

the class CacheContinuousQueryFailoverAbstractSelfTest method testBackupQueueEvict.

/**
     * @throws Exception If failed.
     */
public void testBackupQueueEvict() throws Exception {
    startGridsMultiThreaded(2);
    client = true;
    Ignite qryClient = startGrid(2);
    CacheEventListener1 lsnr = new CacheEventListener1(false);
    ContinuousQuery<Object, Object> qry = new ContinuousQuery<>();
    qry.setLocalListener(lsnr);
    QueryCursor<?> cur = qryClient.cache(DEFAULT_CACHE_NAME).query(qry);
    assertEquals(0, backupQueue(ignite(0)).size());
    long ttl = 100;
    final ExpiryPolicy expiry = new TouchedExpiryPolicy(new Duration(MILLISECONDS, ttl));
    final IgniteCache<Object, Object> cache0 = ignite(2).cache(DEFAULT_CACHE_NAME).withExpiryPolicy(expiry);
    final List<Integer> keys = primaryKeys(ignite(1).cache(DEFAULT_CACHE_NAME), BACKUP_ACK_THRESHOLD);
    lsnr.latch = new CountDownLatch(keys.size());
    for (Integer key : keys) {
        log.info("Put: " + key);
        cache0.put(key, key);
    }
    GridTestUtils.waitForCondition(new GridAbsPredicate() {

        @Override
        public boolean apply() {
            return backupQueue(ignite(0)).isEmpty();
        }
    }, 2000);
    assertTrue("Backup queue is not cleared: " + backupQueue(ignite(0)), backupQueue(ignite(0)).size() < BACKUP_ACK_THRESHOLD);
    boolean wait = waitForCondition(new GridAbsPredicate() {

        @Override
        public boolean apply() {
            return cache0.localPeek(keys.get(0)) == null;
        }
    }, ttl + 1000);
    assertTrue("Entry evicted.", wait);
    GridTestUtils.waitForCondition(new GridAbsPredicate() {

        @Override
        public boolean apply() {
            return backupQueue(ignite(0)).isEmpty();
        }
    }, 2000);
    assertTrue("Backup queue is not cleared: " + backupQueue(ignite(0)), backupQueue(ignite(0)).size() < BACKUP_ACK_THRESHOLD);
    if (backupQueue(ignite(0)).size() != 0) {
        for (Object o : backupQueue(ignite(0))) {
            CacheContinuousQueryEntry e = (CacheContinuousQueryEntry) o;
            assertNotSame("Evicted entry added to backup queue.", -1L, e.updateCounter());
        }
    }
    cur.close();
}
Also used : GridAbsPredicate(org.apache.ignite.internal.util.lang.GridAbsPredicate) Duration(javax.cache.expiry.Duration) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) TouchedExpiryPolicy(javax.cache.expiry.TouchedExpiryPolicy) ExpiryPolicy(javax.cache.expiry.ExpiryPolicy) Ignite(org.apache.ignite.Ignite) TouchedExpiryPolicy(javax.cache.expiry.TouchedExpiryPolicy)

Example 7 with ContinuousQuery

use of org.apache.ignite.cache.query.ContinuousQuery in project ignite by apache.

the class CacheContinuousQueryFailoverAbstractSelfTest method checkBackupQueue.

/**
     * @param backups Number of backups.
     * @param updateFromClient If {@code true} executes cache update from client node.
     * @throws Exception If failed.
     */
private void checkBackupQueue(int backups, boolean updateFromClient) throws Exception {
    this.backups = atomicityMode() == CacheAtomicityMode.ATOMIC ? backups : backups < 2 ? 2 : backups;
    final int SRV_NODES = 4;
    startGridsMultiThreaded(SRV_NODES);
    client = true;
    Ignite qryClient = startGrid(SRV_NODES);
    client = false;
    IgniteCache<Object, Object> qryClientCache = qryClient.cache(DEFAULT_CACHE_NAME);
    Affinity<Object> aff = qryClient.affinity(DEFAULT_CACHE_NAME);
    CacheEventListener1 lsnr = asyncCallback() ? new CacheEventAsyncListener1(false) : new CacheEventListener1(false);
    ContinuousQuery<Object, Object> qry = new ContinuousQuery<>();
    qry.setLocalListener(lsnr);
    QueryCursor<?> cur = qryClientCache.query(qry);
    int PARTS = 10;
    Map<Object, T2<Object, Object>> updates = new HashMap<>();
    List<T3<Object, Object, Object>> expEvts = new ArrayList<>();
    for (int i = 0; i < (atomicityMode() == CacheAtomicityMode.ATOMIC ? SRV_NODES - 1 : SRV_NODES - 2); i++) {
        log.info("Stop iteration: " + i);
        TestCommunicationSpi spi = (TestCommunicationSpi) ignite(i).configuration().getCommunicationSpi();
        Ignite ignite = ignite(i);
        IgniteCache<Object, Object> cache = ignite.cache(DEFAULT_CACHE_NAME);
        List<Integer> keys = testKeys(cache, PARTS);
        CountDownLatch latch = new CountDownLatch(keys.size());
        lsnr.latch = latch;
        boolean first = true;
        for (Integer key : keys) {
            log.info("Put [node=" + ignite.name() + ", key=" + key + ", part=" + aff.partition(key) + ']');
            T2<Object, Object> t = updates.get(key);
            if (updateFromClient) {
                if (atomicityMode() == CacheAtomicityMode.TRANSACTIONAL) {
                    try (Transaction tx = qryClient.transactions().txStart()) {
                        qryClientCache.put(key, key);
                        tx.commit();
                    } catch (CacheException | ClusterTopologyException ignored) {
                        log.warning("Failed put. [Key=" + key + ", val=" + key + "]");
                        continue;
                    }
                } else
                    qryClientCache.put(key, key);
            } else {
                if (atomicityMode() == CacheAtomicityMode.TRANSACTIONAL) {
                    try (Transaction tx = ignite.transactions().txStart()) {
                        cache.put(key, key);
                        tx.commit();
                    } catch (CacheException | ClusterTopologyException ignored) {
                        log.warning("Failed put. [Key=" + key + ", val=" + key + "]");
                        continue;
                    }
                } else
                    cache.put(key, key);
            }
            if (t == null) {
                updates.put(key, new T2<>((Object) key, null));
                expEvts.add(new T3<>((Object) key, (Object) key, null));
            } else {
                updates.put(key, new T2<>((Object) key, (Object) key));
                expEvts.add(new T3<>((Object) key, (Object) key, (Object) key));
            }
            if (first) {
                spi.skipMsg = true;
                first = false;
            }
        }
        stopGrid(i);
        if (!latch.await(5, SECONDS)) {
            Set<Integer> keys0 = new HashSet<>(keys);
            keys0.removeAll(lsnr.keys);
            log.info("Missed events for keys: " + keys0);
            fail("Failed to wait for notifications [exp=" + keys.size() + ", left=" + lsnr.latch.getCount() + ']');
        }
        checkEvents(expEvts, lsnr);
    }
    for (int i = 0; i < (atomicityMode() == CacheAtomicityMode.ATOMIC ? SRV_NODES - 1 : SRV_NODES - 2); i++) {
        log.info("Start iteration: " + i);
        Ignite ignite = startGrid(i);
        IgniteCache<Object, Object> cache = ignite.cache(DEFAULT_CACHE_NAME);
        List<Integer> keys = testKeys(cache, PARTS);
        CountDownLatch latch = new CountDownLatch(keys.size());
        lsnr.latch = latch;
        for (Integer key : keys) {
            log.info("Put [node=" + ignite.name() + ", key=" + key + ", part=" + aff.partition(key) + ']');
            T2<Object, Object> t = updates.get(key);
            if (t == null) {
                updates.put(key, new T2<>((Object) key, null));
                expEvts.add(new T3<>((Object) key, (Object) key, null));
            } else {
                updates.put(key, new T2<>((Object) key, (Object) key));
                expEvts.add(new T3<>((Object) key, (Object) key, (Object) key));
            }
            if (updateFromClient)
                qryClientCache.put(key, key);
            else
                cache.put(key, key);
        }
        if (!latch.await(10, SECONDS)) {
            Set<Integer> keys0 = new HashSet<>(keys);
            keys0.removeAll(lsnr.keys);
            log.info("Missed events for keys: " + keys0);
            fail("Failed to wait for notifications [exp=" + keys.size() + ", left=" + lsnr.latch.getCount() + ']');
        }
        checkEvents(expEvts, lsnr);
    }
    cur.close();
    assertFalse("Unexpected error during test, see log for details.", err);
}
Also used : ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) CacheException(javax.cache.CacheException) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) Ignite(org.apache.ignite.Ignite) T2(org.apache.ignite.internal.util.typedef.T2) T3(org.apache.ignite.internal.util.typedef.T3) HashSet(java.util.HashSet) GridConcurrentHashSet(org.apache.ignite.internal.util.GridConcurrentHashSet) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Transaction(org.apache.ignite.transactions.Transaction) ClusterTopologyException(org.apache.ignite.cluster.ClusterTopologyException)

Example 8 with ContinuousQuery

use of org.apache.ignite.cache.query.ContinuousQuery in project ignite by apache.

the class CacheContinuousQueryFailoverAbstractSelfTest method testBackupQueueCleanupClientQuery.

/**
     * @throws Exception If failed.
     */
public void testBackupQueueCleanupClientQuery() throws Exception {
    startGridsMultiThreaded(2);
    client = true;
    Ignite qryClient = startGrid(2);
    CacheEventListener1 lsnr = new CacheEventListener1(false);
    ContinuousQuery<Object, Object> qry = new ContinuousQuery<>();
    qry.setLocalListener(lsnr);
    QueryCursor<?> cur = qryClient.cache(DEFAULT_CACHE_NAME).query(qry);
    assertEquals(0, backupQueue(ignite(1)).size());
    IgniteCache<Object, Object> cache0 = ignite(0).cache(DEFAULT_CACHE_NAME);
    List<Integer> keys = primaryKeys(cache0, BACKUP_ACK_THRESHOLD);
    CountDownLatch latch = new CountDownLatch(keys.size());
    lsnr.latch = latch;
    for (Integer key : keys) {
        log.info("Put: " + key);
        cache0.put(key, key);
    }
    GridTestUtils.waitForCondition(new GridAbsPredicate() {

        @Override
        public boolean apply() {
            return backupQueue(ignite(1)).isEmpty();
        }
    }, 2000);
    assertTrue("Backup queue is not cleared: " + backupQueue(ignite(1)), backupQueue(ignite(1)).size() < BACKUP_ACK_THRESHOLD);
    if (!latch.await(5, SECONDS))
        fail("Failed to wait for notifications [exp=" + keys.size() + ", left=" + lsnr.latch.getCount() + ']');
    keys = primaryKeys(cache0, BACKUP_ACK_THRESHOLD / 2);
    latch = new CountDownLatch(keys.size());
    lsnr.latch = latch;
    for (Integer key : keys) cache0.put(key, key);
    final long ACK_FREQ = 5000;
    GridTestUtils.waitForCondition(new GridAbsPredicate() {

        @Override
        public boolean apply() {
            return backupQueue(ignite(1)).isEmpty();
        }
    }, ACK_FREQ + 2000);
    assertTrue("Backup queue is not cleared: " + backupQueue(ignite(1)), backupQueue(ignite(1)).isEmpty());
    if (!latch.await(5, SECONDS))
        fail("Failed to wait for notifications [exp=" + keys.size() + ", left=" + lsnr.latch.getCount() + ']');
    cur.close();
    assertFalse("Unexpected error during test, see log for details.", err);
}
Also used : GridAbsPredicate(org.apache.ignite.internal.util.lang.GridAbsPredicate) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) Ignite(org.apache.ignite.Ignite)

Example 9 with ContinuousQuery

use of org.apache.ignite.cache.query.ContinuousQuery in project ignite by apache.

the class CacheContinuousQueryFailoverAbstractSelfTest method testStartStopQuery.

/**
     * @throws Exception If failed.
     */
public void testStartStopQuery() throws Exception {
    this.backups = 1;
    final int SRV_NODES = 3;
    startGridsMultiThreaded(SRV_NODES);
    client = true;
    final Ignite qryClient = startGrid(SRV_NODES);
    client = false;
    IgniteCache<Object, Object> clnCache = qryClient.cache(DEFAULT_CACHE_NAME);
    IgniteOutClosure<IgniteCache<Integer, Integer>> rndCache = new IgniteOutClosure<IgniteCache<Integer, Integer>>() {

        int cnt = 0;

        @Override
        public IgniteCache<Integer, Integer> apply() {
            ++cnt;
            return grid(cnt % SRV_NODES + 1).cache(DEFAULT_CACHE_NAME);
        }
    };
    Ignite igniteSrv = ignite(0);
    IgniteCache<Object, Object> srvCache = igniteSrv.cache(DEFAULT_CACHE_NAME);
    List<Integer> keys = testKeys(srvCache, 3);
    int keyCnt = keys.size();
    for (int j = 0; j < 50; ++j) {
        ContinuousQuery<Object, Object> qry = new ContinuousQuery<>();
        final CacheEventListener3 lsnr = asyncCallback() ? new CacheEventAsyncListener3() : new CacheEventListener3();
        qry.setLocalListener(lsnr);
        qry.setRemoteFilter(lsnr);
        int keyIter = 0;
        for (; keyIter < keyCnt / 2; keyIter++) {
            int key = keys.get(keyIter);
            rndCache.apply().put(key, key);
        }
        assert lsnr.evts.isEmpty();
        QueryCursor<Cache.Entry<Object, Object>> qryCur = clnCache.query(qry);
        Map<Object, T2<Object, Object>> updates = new HashMap<>();
        final List<T3<Object, Object, Object>> expEvts = new ArrayList<>();
        Affinity<Object> aff = affinity(srvCache);
        boolean filtered = false;
        for (; keyIter < keys.size(); keyIter++) {
            int key = keys.get(keyIter);
            int val = filtered ? 1 : 2;
            log.info("Put [key=" + key + ", val=" + val + ", part=" + aff.partition(key) + ']');
            T2<Object, Object> t = updates.get(key);
            if (t == null) {
                // Check filtered.
                if (!filtered) {
                    updates.put(key, new T2<>((Object) val, null));
                    expEvts.add(new T3<>((Object) key, (Object) val, null));
                }
            } else {
                // Check filtered.
                if (!filtered) {
                    updates.put(key, new T2<>((Object) val, (Object) t.get1()));
                    expEvts.add(new T3<>((Object) key, (Object) val, (Object) t.get1()));
                }
            }
            rndCache.apply().put(key, val);
            filtered = !filtered;
        }
        checkEvents(expEvts, lsnr, false);
        qryCur.close();
    }
}
Also used : ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) MutableEntry(javax.cache.processor.MutableEntry) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) Ignite(org.apache.ignite.Ignite) T2(org.apache.ignite.internal.util.typedef.T2) T3(org.apache.ignite.internal.util.typedef.T3) IgniteCache(org.apache.ignite.IgniteCache) IgniteOutClosure(org.apache.ignite.lang.IgniteOutClosure) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Example 10 with ContinuousQuery

use of org.apache.ignite.cache.query.ContinuousQuery in project ignite by apache.

the class CacheContinuousQueryAsyncFilterListenerTest method testNonDeadLockInListener.

/**
     * @param ccfg Cache configuration.
     * @param asyncFltr Async filter.
     * @param asyncLsnr Async listener.
     * @param jcacheApi Use JCache api for registration entry update listener.
     * @throws Exception If failed.
     */
private void testNonDeadLockInListener(CacheConfiguration ccfg, final boolean asyncFltr, boolean asyncLsnr, boolean jcacheApi) throws Exception {
    ignite(0).createCache(ccfg);
    ThreadLocalRandom rnd = ThreadLocalRandom.current();
    try {
        for (int i = 0; i < ITERATION_CNT; i++) {
            log.info("Start iteration: " + i);
            int nodeIdx = i % NODES;
            final String cacheName = ccfg.getName();
            final IgniteCache cache = grid(nodeIdx).cache(cacheName);
            final QueryTestKey key = NODES - 1 != nodeIdx ? affinityKey(cache) : new QueryTestKey(1);
            final QueryTestValue val0 = new QueryTestValue(1);
            final QueryTestValue newVal = new QueryTestValue(2);
            final CountDownLatch latch = new CountDownLatch(1);
            final CountDownLatch evtFromLsnrLatch = new CountDownLatch(1);
            IgniteBiInClosure<Ignite, CacheEntryEvent<? extends QueryTestKey, ? extends QueryTestValue>> fltrClsr = new IgniteBiInClosure<Ignite, CacheEntryEvent<? extends QueryTestKey, ? extends QueryTestValue>>() {

                @Override
                public void apply(Ignite ignite, CacheEntryEvent<? extends QueryTestKey, ? extends QueryTestValue> e) {
                    if (asyncFltr) {
                        assertFalse("Failed: " + Thread.currentThread().getName(), Thread.currentThread().getName().contains("sys-"));
                        assertTrue("Failed: " + Thread.currentThread().getName(), Thread.currentThread().getName().contains("callback-"));
                    }
                }
            };
            IgniteBiInClosure<Ignite, CacheEntryEvent<? extends QueryTestKey, ? extends QueryTestValue>> lsnrClsr = new IgniteBiInClosure<Ignite, CacheEntryEvent<? extends QueryTestKey, ? extends QueryTestValue>>() {

                @Override
                public void apply(Ignite ignite, CacheEntryEvent<? extends QueryTestKey, ? extends QueryTestValue> e) {
                    IgniteCache<Object, Object> cache0 = ignite.cache(cacheName);
                    QueryTestValue val = e.getValue();
                    if (val == null)
                        return;
                    else if (val.equals(newVal)) {
                        evtFromLsnrLatch.countDown();
                        return;
                    } else if (!val.equals(val0))
                        return;
                    Transaction tx = null;
                    try {
                        if (cache0.getConfiguration(CacheConfiguration.class).getAtomicityMode() == TRANSACTIONAL)
                            tx = ignite.transactions().txStart(PESSIMISTIC, REPEATABLE_READ);
                        assertEquals(val, val0);
                        cache0.put(key, newVal);
                        if (tx != null)
                            tx.commit();
                        latch.countDown();
                    } catch (Exception exp) {
                        log.error("Failed: ", exp);
                        throw new IgniteException(exp);
                    } finally {
                        if (tx != null)
                            tx.close();
                    }
                }
            };
            QueryCursor qry = null;
            MutableCacheEntryListenerConfiguration<QueryTestKey, QueryTestValue> lsnrCfg = null;
            CacheInvokeListener locLsnr = asyncLsnr ? new CacheInvokeListenerAsync(lsnrClsr) : new CacheInvokeListener(lsnrClsr);
            CacheEntryEventSerializableFilter<QueryTestKey, QueryTestValue> rmtFltr = asyncFltr ? new CacheTestRemoteFilterAsync(fltrClsr) : new CacheTestRemoteFilter(fltrClsr);
            if (jcacheApi) {
                lsnrCfg = new MutableCacheEntryListenerConfiguration<>(FactoryBuilder.factoryOf(locLsnr), FactoryBuilder.factoryOf(rmtFltr), true, false);
                cache.registerCacheEntryListener(lsnrCfg);
            } else {
                ContinuousQuery<QueryTestKey, QueryTestValue> conQry = new ContinuousQuery<>();
                conQry.setLocalListener(locLsnr);
                conQry.setRemoteFilterFactory(FactoryBuilder.factoryOf(rmtFltr));
                qry = cache.query(conQry);
            }
            try {
                if (rnd.nextBoolean())
                    cache.put(key, val0);
                else {
                    cache.invoke(key, new CacheEntryProcessor() {

                        @Override
                        public Object process(MutableEntry entry, Object... arguments) throws EntryProcessorException {
                            entry.setValue(val0);
                            return null;
                        }
                    });
                }
                assertTrue("Failed to waiting event.", U.await(latch, 3, SECONDS));
                assertEquals(cache.get(key), new QueryTestValue(2));
                assertTrue("Failed to waiting event from listener.", U.await(latch, 3, SECONDS));
            } finally {
                if (qry != null)
                    qry.close();
                if (lsnrCfg != null)
                    cache.deregisterCacheEntryListener(lsnrCfg);
            }
            log.info("Iteration finished: " + i);
        }
    } finally {
        ignite(0).destroyCache(ccfg.getName());
    }
}
Also used : IgniteBiInClosure(org.apache.ignite.lang.IgniteBiInClosure) CacheEntryEvent(javax.cache.event.CacheEntryEvent) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) EntryProcessorException(javax.cache.processor.EntryProcessorException) IgniteException(org.apache.ignite.IgniteException) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Ignite(org.apache.ignite.Ignite) MutableEntry(javax.cache.processor.MutableEntry) QueryCursor(org.apache.ignite.cache.query.QueryCursor) IgniteCache(org.apache.ignite.IgniteCache) CountDownLatch(java.util.concurrent.CountDownLatch) EntryProcessorException(javax.cache.processor.EntryProcessorException) IgniteException(org.apache.ignite.IgniteException) CacheEntryListenerException(javax.cache.event.CacheEntryListenerException) Transaction(org.apache.ignite.transactions.Transaction) CacheEntryProcessor(org.apache.ignite.cache.CacheEntryProcessor)

Aggregations

ContinuousQuery (org.apache.ignite.cache.query.ContinuousQuery)82 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)47 Ignite (org.apache.ignite.Ignite)42 CacheEntryEvent (javax.cache.event.CacheEntryEvent)41 CountDownLatch (java.util.concurrent.CountDownLatch)38 ArrayList (java.util.ArrayList)26 CacheEntryListenerException (javax.cache.event.CacheEntryListenerException)19 QueryCursor (org.apache.ignite.cache.query.QueryCursor)19 IgniteCache (org.apache.ignite.IgniteCache)18 IgniteException (org.apache.ignite.IgniteException)15 HashMap (java.util.HashMap)14 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)13 List (java.util.List)12 PA (org.apache.ignite.internal.util.typedef.PA)12 T2 (org.apache.ignite.internal.util.typedef.T2)12 CacheEntryUpdatedListener (javax.cache.event.CacheEntryUpdatedListener)11 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)10 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)9 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)9 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)9