Search in sources :

Example 36 with ContinuousQuery

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

the class CacheContinuousQueryExecuteInPrimaryTest method doTestWithEventsEntries.

/**
 * @throws Exception If failed.
 */
public void doTestWithEventsEntries(CacheConfiguration<Integer, String> ccfg) throws Exception {
    try (IgniteCache<Integer, String> cache = grid(0).createCache(ccfg)) {
        ContinuousQuery<Integer, String> qry = new ContinuousQuery<>();
        final CountDownLatch latch = new CountDownLatch(16);
        final AtomicInteger cnt = new AtomicInteger(0);
        qry.setLocalListener(new CacheEntryUpdatedListener<Integer, String>() {

            @Override
            public void onUpdated(Iterable<CacheEntryEvent<? extends Integer, ? extends String>> iterable) throws CacheEntryListenerException {
                for (CacheEntryEvent<? extends Integer, ? extends String> e : iterable) {
                    cnt.incrementAndGet();
                    latch.countDown();
                }
            }
        });
        qry.setRemoteFilterFactory(FactoryBuilder.factoryOf(new CacheEntryEventSerializableFilter<Integer, String>() {

            @Override
            public boolean evaluate(CacheEntryEvent<? extends Integer, ? extends String> e) throws CacheEntryListenerException {
                return e.getKey() % 2 == 0;
            }
        }));
        // Execute query.
        executeQuery(cache, qry, ccfg.getAtomicityMode() == TRANSACTIONAL);
        assertTrue(latch.await(LATCH_TIMEOUT, MILLISECONDS));
        assertEquals(16, cnt.get());
    } finally {
        ignite(0).destroyCache(ccfg.getName());
    }
}
Also used : CacheEntryEventSerializableFilter(org.apache.ignite.cache.CacheEntryEventSerializableFilter) 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 37 with ContinuousQuery

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

the class CacheContinuousQueryFailoverAbstractSelfTest method testLeftPrimaryAndBackupNodes.

/**
 * @throws Exception If failed.
 */
public void testLeftPrimaryAndBackupNodes() throws Exception {
    if (cacheMode() == REPLICATED)
        return;
    this.backups = 1;
    final int SRV_NODES = 3;
    startGridsMultiThreaded(SRV_NODES);
    client = true;
    final Ignite qryClient = startGrid(SRV_NODES);
    client = false;
    ContinuousQuery<Object, Object> qry = new ContinuousQuery<>();
    final CacheEventListener3 lsnr = asyncCallback() ? new CacheEventAsyncListener3() : new CacheEventListener3();
    qry.setLocalListener(lsnr);
    qry.setRemoteFilter(lsnr);
    IgniteCache<Object, Object> clnCache = qryClient.cache(DEFAULT_CACHE_NAME);
    QueryCursor<Cache.Entry<Object, Object>> qryCur = clnCache.query(qry);
    Ignite igniteSrv = ignite(0);
    IgniteCache<Object, Object> srvCache = igniteSrv.cache(DEFAULT_CACHE_NAME);
    Affinity<Object> aff = affinity(srvCache);
    List<Integer> keys = testKeys(srvCache, 1);
    Collection<ClusterNode> nodes = aff.mapPartitionToPrimaryAndBackups(keys.get(0));
    Collection<UUID> ids = F.transform(nodes, new C1<ClusterNode, UUID>() {

        @Override
        public UUID apply(ClusterNode node) {
            return node.id();
        }
    });
    int keyIter = 0;
    boolean filtered = false;
    Map<Object, T2<Object, Object>> updates = new HashMap<>();
    final List<T3<Object, Object, Object>> expEvts = new ArrayList<>();
    for (; keyIter < keys.size() / 2; keyIter++) {
        int key = keys.get(keyIter);
        log.info("Put [key=" + key + ", part=" + aff.partition(key) + ", filtered=" + filtered + ']');
        T2<Object, Object> t = updates.get(key);
        Integer val = filtered ? (key % 2 == 0 ? key + 1 : key) : key * 2;
        if (t == null) {
            updates.put(key, new T2<>((Object) val, null));
            if (!filtered)
                expEvts.add(new T3<>((Object) key, (Object) val, null));
        } else {
            updates.put(key, new T2<>((Object) val, (Object) key));
            if (!filtered)
                expEvts.add(new T3<>((Object) key, (Object) val, (Object) key));
        }
        srvCache.put(key, val);
        filtered = !filtered;
    }
    checkEvents(expEvts, lsnr, false);
    List<Thread> stopThreads = new ArrayList<>(3);
    // Stop nodes which owning this partition.
    for (int i = 0; i < SRV_NODES; i++) {
        Ignite ignite = ignite(i);
        if (ids.contains(ignite.cluster().localNode().id())) {
            final int i0 = i;
            TestCommunicationSpi spi = (TestCommunicationSpi) ignite.configuration().getCommunicationSpi();
            spi.skipAllMsg = true;
            stopThreads.add(new Thread() {

                @Override
                public void run() {
                    stopGrid(i0, true);
                }
            });
        }
    }
    // Stop and join threads.
    for (Thread t : stopThreads) t.start();
    for (Thread t : stopThreads) t.join();
    assert GridTestUtils.waitForCondition(new PA() {

        @Override
        public boolean apply() {
            // (SRV_NODES + 1 client node) - 1 primary - backup nodes.
            return qryClient.cluster().nodes().size() == (SRV_NODES + 1) - 1 - /* Primary node */
            backups;
        }
    }, 5000L);
    for (; keyIter < keys.size(); keyIter++) {
        int key = keys.get(keyIter);
        log.info("Put [key=" + key + ", filtered=" + filtered + ']');
        T2<Object, Object> t = updates.get(key);
        Integer val = filtered ? (key % 2 == 0 ? key + 1 : key) : key * 2;
        if (t == null) {
            updates.put(key, new T2<>((Object) val, null));
            if (!filtered)
                expEvts.add(new T3<>((Object) key, (Object) val, null));
        } else {
            updates.put(key, new T2<>((Object) val, (Object) key));
            if (!filtered)
                expEvts.add(new T3<>((Object) key, (Object) val, (Object) key));
        }
        clnCache.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) UUID(java.util.UUID) T2(org.apache.ignite.internal.util.typedef.T2) T3(org.apache.ignite.internal.util.typedef.T3) ClusterNode(org.apache.ignite.cluster.ClusterNode) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) PA(org.apache.ignite.internal.util.typedef.PA)

Example 38 with ContinuousQuery

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

the class CacheContinuousQueryFailoverAbstractSelfTest method testMultiThreaded.

/**
 * @throws Exception If failed.
 */
public void testMultiThreaded() throws Exception {
    this.backups = 2;
    final int SRV_NODES = 3;
    startGridsMultiThreaded(SRV_NODES);
    client = true;
    Ignite qryClient = startGrid(SRV_NODES);
    final IgniteCache<Object, Object> cache = qryClient.cache(DEFAULT_CACHE_NAME);
    CacheEventListener1 lsnr = new CacheEventListener1(true);
    ContinuousQuery<Object, Object> qry = new ContinuousQuery<>();
    qry.setLocalListener(lsnr);
    QueryCursor<?> cur = cache.query(qry);
    client = false;
    final int SRV_IDX = SRV_NODES - 1;
    List<Integer> keys = primaryKeys(ignite(SRV_IDX).cache(DEFAULT_CACHE_NAME), 10);
    final int THREADS = 10;
    for (int i = 0; i < keys.size(); i++) {
        log.info("Iteration: " + i);
        Ignite srv = ignite(SRV_IDX);
        TestCommunicationSpi spi = (TestCommunicationSpi) srv.configuration().getCommunicationSpi();
        spi.sndFirstOnly = new AtomicBoolean(false);
        final Integer key = keys.get(i);
        final AtomicInteger val = new AtomicInteger();
        CountDownLatch latch = new CountDownLatch(THREADS);
        lsnr.latch = latch;
        IgniteInternalFuture<?> fut = GridTestUtils.runMultiThreadedAsync(new Callable<Object>() {

            @Override
            public Object call() throws Exception {
                Integer val0 = val.getAndIncrement();
                cache.put(key, val0);
                return null;
            }
        }, THREADS, "update-thread");
        fut.get();
        stopGrid(SRV_IDX);
        if (!latch.await(5, SECONDS))
            fail("Failed to wait for notifications [exp=" + THREADS + ", left=" + lsnr.latch.getCount() + ']');
        assertEquals(THREADS, lsnr.allEvts.size());
        Set<Integer> vals = new HashSet<>();
        boolean err = false;
        for (CacheEntryEvent<?, ?> evt : lsnr.allEvts) {
            assertEquals(key, evt.getKey());
            assertNotNull(evt.getValue());
            if (!vals.add((Integer) evt.getValue())) {
                err = true;
                log.info("Extra event: " + evt);
            }
        }
        for (int v = 0; v < THREADS; v++) {
            if (!vals.contains(v)) {
                err = true;
                log.info("Event for value not received: " + v);
            }
        }
        assertFalse("Invalid events, see log for details.", err);
        lsnr.allEvts.clear();
        startGrid(SRV_IDX);
    }
    cur.close();
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) CacheEntryListenerException(javax.cache.event.CacheEntryListenerException) IgniteSpiException(org.apache.ignite.spi.IgniteSpiException) ClusterTopologyException(org.apache.ignite.cluster.ClusterTopologyException) EntryProcessorException(javax.cache.processor.EntryProcessorException) IgniteInterruptedCheckedException(org.apache.ignite.internal.IgniteInterruptedCheckedException) CacheException(javax.cache.CacheException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Ignite(org.apache.ignite.Ignite) HashSet(java.util.HashSet) GridConcurrentHashSet(org.apache.ignite.internal.util.GridConcurrentHashSet)

Example 39 with ContinuousQuery

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

the class CacheContinuousQueryFailoverAbstractSelfTest method testBackupQueueCleanupServerQuery.

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

        @Override
        public boolean apply() {
            return backupQueue(ignite(1)).isEmpty();
        }
    }, 5000);
    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() + ']');
    cur.close();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) GridAbsPredicate(org.apache.ignite.internal.util.lang.GridAbsPredicate) Ignite(org.apache.ignite.Ignite) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 40 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)

Aggregations

ContinuousQuery (org.apache.ignite.cache.query.ContinuousQuery)87 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)50 Ignite (org.apache.ignite.Ignite)43 CacheEntryEvent (javax.cache.event.CacheEntryEvent)42 CountDownLatch (java.util.concurrent.CountDownLatch)38 ArrayList (java.util.ArrayList)27 QueryCursor (org.apache.ignite.cache.query.QueryCursor)23 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)22 CacheEntryListenerException (javax.cache.event.CacheEntryListenerException)19 IgniteCache (org.apache.ignite.IgniteCache)19 IgniteException (org.apache.ignite.IgniteException)17 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)15 HashMap (java.util.HashMap)14 CacheEntryUpdatedListener (javax.cache.event.CacheEntryUpdatedListener)14 PA (org.apache.ignite.internal.util.typedef.PA)13 T2 (org.apache.ignite.internal.util.typedef.T2)13 List (java.util.List)12 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)10 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)10 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)9