Search in sources :

Example 51 with ContinuousQuery

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

the class CacheContinuousQueryFilterDeploymentFailedTest method testContinuousQueryFilterDeploymentFailed.

/**
 * Tests continuous query behavior in case of filter deployment obtaining failure.
 *
 * @throws Exception If failed.
 */
@Test
@SuppressWarnings({ "ThrowableNotThrown" })
public void testContinuousQueryFilterDeploymentFailed() throws Exception {
    startGrids(NODES_CNT - 1);
    IgniteEx cli = startClientGrid(NODES_CNT - 1);
    IgniteCache<Integer, Integer> cache = cli.createCache(DEFAULT_CACHE_NAME);
    ContinuousQuery<Integer, Integer> qry = new ContinuousQuery<>();
    Class<Factory<CacheEntryEventFilter<Integer, Integer>>> rmtFilterFactoryCls = (Class<Factory<CacheEntryEventFilter<Integer, Integer>>>) getExternalClassLoader().loadClass("org.apache.ignite.tests.p2p.CacheDeploymentEntryEventFilterFactory");
    qry.setRemoteFilterFactory(rmtFilterFactoryCls.newInstance());
    spi(grid(1)).blockMessages((node, msg) -> msg instanceof GridDeploymentRequest);
    assertThrowsWithCause(() -> cache.query(qry), CacheException.class);
    assertTrue(stopRoutineLatch.await(getTestTimeout(), MILLISECONDS));
    assertTrue(allGrids().stream().allMatch(g -> ((IgniteEx) g).context().systemView().view(CQ_SYS_VIEW).size() == 0));
}
Also used : Factory(javax.cache.configuration.Factory) TestRecordingCommunicationSpi.spi(org.apache.ignite.internal.TestRecordingCommunicationSpi.spi) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) GridTestUtils.assertThrowsWithCause(org.apache.ignite.testframework.GridTestUtils.assertThrowsWithCause) TestTcpDiscoverySpi(org.apache.ignite.spi.discovery.tcp.TestTcpDiscoverySpi) Ignition.allGrids(org.apache.ignite.Ignition.allGrids) IgniteEx(org.apache.ignite.internal.IgniteEx) Test(org.junit.Test) MILLISECONDS(java.util.concurrent.TimeUnit.MILLISECONDS) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) IgniteCache(org.apache.ignite.IgniteCache) CountDownLatch(java.util.concurrent.CountDownLatch) IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) CacheEntryEventFilter(javax.cache.event.CacheEntryEventFilter) DiscoveryCustomMessage(org.apache.ignite.internal.managers.discovery.DiscoveryCustomMessage) GridDeploymentRequest(org.apache.ignite.internal.managers.deployment.GridDeploymentRequest) CacheException(javax.cache.CacheException) CQ_SYS_VIEW(org.apache.ignite.internal.processors.continuous.GridContinuousProcessor.CQ_SYS_VIEW) TestRecordingCommunicationSpi(org.apache.ignite.internal.TestRecordingCommunicationSpi) StopRoutineDiscoveryMessage(org.apache.ignite.internal.processors.continuous.StopRoutineDiscoveryMessage) DiscoveryHook(org.apache.ignite.testframework.GridTestUtils.DiscoveryHook) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) IgniteEx(org.apache.ignite.internal.IgniteEx) Factory(javax.cache.configuration.Factory) GridDeploymentRequest(org.apache.ignite.internal.managers.deployment.GridDeploymentRequest) CacheEntryEventFilter(javax.cache.event.CacheEntryEventFilter) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 52 with ContinuousQuery

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

the class CacheContinuousQueryDeploymentToClientTest method testDeploymentToNewClient.

/**
 * Test starts 1 server node and 1 client node. The client node deploys
 * CQ for the cache {@link #CACHE_NAME}. After that another client node is started.
 * Expected that CQ won't be deployed to the new client, since the client doesn't
 * store any data.
 *
 * @throws Exception If failed.
 */
@Test
public void testDeploymentToNewClient() throws Exception {
    startGrid(0);
    IgniteEx client1 = startClientGrid(1);
    IgniteCache<Integer, String> cache = client1.cache(CACHE_NAME);
    AbstractContinuousQuery<Integer, String> qry = new ContinuousQuery<Integer, String>().setLocalListener(evts -> {
    // No-op.
    }).setRemoteFilterFactory((Factory<CacheEntryEventFilter<Integer, String>>) () -> evt -> true);
    cache.query(qry);
    IgniteEx client2 = startClientGrid(2);
    GridContinuousProcessor proc = client2.context().continuous();
    assertEquals(0, ((Map<?, ?>) U.field(proc, "locInfos")).size());
    assertEquals(0, ((Map<?, ?>) U.field(proc, "rmtInfos")).size());
    assertEquals(0, ((Map<?, ?>) U.field(proc, "startFuts")).size());
    assertEquals(0, ((Map<?, ?>) U.field(proc, "stopFuts")).size());
    assertEquals(0, ((Map<?, ?>) U.field(proc, "bufCheckThreads")).size());
}
Also used : Factory(javax.cache.configuration.Factory) AbstractContinuousQuery(org.apache.ignite.cache.query.AbstractContinuousQuery) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) U(org.apache.ignite.internal.util.typedef.internal.U) IgniteEx(org.apache.ignite.internal.IgniteEx) Test(org.junit.Test) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) IgniteCache(org.apache.ignite.IgniteCache) IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) CacheEntryEventFilter(javax.cache.event.CacheEntryEventFilter) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) Map(java.util.Map) GridContinuousProcessor(org.apache.ignite.internal.processors.continuous.GridContinuousProcessor) AbstractContinuousQuery(org.apache.ignite.cache.query.AbstractContinuousQuery) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) IgniteEx(org.apache.ignite.internal.IgniteEx) GridContinuousProcessor(org.apache.ignite.internal.processors.continuous.GridContinuousProcessor) CacheEntryEventFilter(javax.cache.event.CacheEntryEventFilter) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 53 with ContinuousQuery

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

the class CacheContinuousQueryDeploymentToClientTest method testDeploymentToExistingClient.

/**
 * Test starts 1 server node and 2 client nodes. The first client node deploys
 * CQ for the cache {@link #CACHE_NAME}.
 * Expected that CQ won't be deployed to the second client, since the client doesn't
 * store any data.
 *
 * @throws Exception If failed.
 */
@Test
public void testDeploymentToExistingClient() throws Exception {
    startGrid(0);
    IgniteEx client1 = startClientGrid(1);
    IgniteCache<Integer, String> cache = client1.cache(CACHE_NAME);
    IgniteEx client2 = startClientGrid(2);
    AbstractContinuousQuery<Integer, String> qry = new ContinuousQuery<Integer, String>().setLocalListener(evts -> {
    // No-op.
    }).setRemoteFilterFactory((Factory<CacheEntryEventFilter<Integer, String>>) () -> evt -> true);
    cache.query(qry);
    GridContinuousProcessor proc = client2.context().continuous();
    assertEquals(0, ((Map<?, ?>) U.field(proc, "locInfos")).size());
    assertEquals(0, ((Map<?, ?>) U.field(proc, "rmtInfos")).size());
    assertEquals(0, ((Map<?, ?>) U.field(proc, "startFuts")).size());
    assertEquals(0, ((Map<?, ?>) U.field(proc, "stopFuts")).size());
    assertEquals(0, ((Map<?, ?>) U.field(proc, "bufCheckThreads")).size());
}
Also used : Factory(javax.cache.configuration.Factory) AbstractContinuousQuery(org.apache.ignite.cache.query.AbstractContinuousQuery) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) U(org.apache.ignite.internal.util.typedef.internal.U) IgniteEx(org.apache.ignite.internal.IgniteEx) Test(org.junit.Test) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) IgniteCache(org.apache.ignite.IgniteCache) IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) CacheEntryEventFilter(javax.cache.event.CacheEntryEventFilter) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) Map(java.util.Map) GridContinuousProcessor(org.apache.ignite.internal.processors.continuous.GridContinuousProcessor) AbstractContinuousQuery(org.apache.ignite.cache.query.AbstractContinuousQuery) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) IgniteEx(org.apache.ignite.internal.IgniteEx) GridContinuousProcessor(org.apache.ignite.internal.processors.continuous.GridContinuousProcessor) CacheEntryEventFilter(javax.cache.event.CacheEntryEventFilter) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 54 with ContinuousQuery

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

the class CacheContinuousQueryOperationFromCallbackTest method doTest.

/**
 * @param ccfg Cache configuration.
 * @throws Exception If failed.
 */
@SuppressWarnings({ "TypeMayBeWeakened", "unchecked", "TooBroadScope" })
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) - KEYS);
                    boolean startTx = cache.getConfiguration(CacheConfiguration.class).getAtomicityMode() != ATOMIC && rnd.nextBoolean();
                    Transaction tx = null;
                    boolean committed = false;
                    while (!committed && !Thread.currentThread().isInterrupted()) {
                        try {
                            if (startTx)
                                tx = cache.unwrap(Ignite.class).transactions().txStart(PESSIMISTIC, REPEATABLE_READ);
                            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));
                            }
                            if (tx != null)
                                tx.commit();
                            committed = true;
                        } catch (Exception e) {
                            assertTrue(e.getCause() instanceof TransactionSerializationException);
                            assertEquals(ccfg.getAtomicityMode(), TRANSACTIONAL_SNAPSHOT);
                        } finally {
                            if (tx != null)
                                tx.close();
                        }
                    }
                }
            }
        }, threadCnt, "put-thread");
        f.get(30, TimeUnit.SECONDS);
        assert GridTestUtils.waitForCondition(new PA() {

            @Override
            public boolean apply() {
                return qryCntr.get() >= ITERATION_CNT * threadCnt * NODES;
            }
        }, getTestTimeout());
        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;
                }
            }, getTestTimeout());
            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;
                }
            }, getTestTimeout());
            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) TransactionSerializationException(org.apache.ignite.transactions.TransactionSerializationException) 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) CacheEntryListenerException(javax.cache.event.CacheEntryListenerException) EntryProcessorException(javax.cache.processor.EntryProcessorException) TransactionSerializationException(org.apache.ignite.transactions.TransactionSerializationException) PA(org.apache.ignite.internal.util.typedef.PA) Transaction(org.apache.ignite.transactions.Transaction) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Example 55 with ContinuousQuery

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

the class CacheEntryEventProbe method start.

/**
 * {@inheritDoc}
 */
@Override
public void start(BenchmarkDriver drv, BenchmarkConfiguration cfg) throws Exception {
    this.cfg = cfg;
    if (drv instanceof IgniteCacheAbstractBenchmark) {
        IgniteCacheAbstractBenchmark drv0 = (IgniteCacheAbstractBenchmark) drv;
        if (drv0.cache() != null) {
            ContinuousQuery<Integer, Integer> qry = new ContinuousQuery<>();
            final AtomicLong cnt = new AtomicLong();
            qry.setLocalListener(localListener(cnt));
            qryCur = drv0.cache().query(qry);
            buildingService = Executors.newSingleThreadExecutor();
            buildingService.execute(new Runnable() {

                @Override
                public void run() {
                    try {
                        while (!Thread.currentThread().isInterrupted()) {
                            Thread.sleep(1000);
                            long evts = cnt.getAndSet(0);
                            BenchmarkProbePoint pnt = new BenchmarkProbePoint(TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis()), new double[] { evts });
                            collectPoint(pnt);
                        }
                    } catch (InterruptedException e) {
                    // No-op.
                    }
                }
            });
            println(cfg, getClass().getSimpleName() + " probe is started.");
        }
    }
    if (qryCur == null)
        errorHelp(cfg, "Can not start " + getClass().getSimpleName() + " probe. Probably, the driver doesn't provide \"cache()\" method.");
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) BenchmarkProbePoint(org.yardstickframework.BenchmarkProbePoint)

Aggregations

ContinuousQuery (org.apache.ignite.cache.query.ContinuousQuery)123 Test (org.junit.Test)74 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)70 Ignite (org.apache.ignite.Ignite)60 CacheEntryEvent (javax.cache.event.CacheEntryEvent)58 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)53 CountDownLatch (java.util.concurrent.CountDownLatch)48 IgniteCache (org.apache.ignite.IgniteCache)43 QueryCursor (org.apache.ignite.cache.query.QueryCursor)38 ArrayList (java.util.ArrayList)32 IgniteException (org.apache.ignite.IgniteException)28 CacheEntryListenerException (javax.cache.event.CacheEntryListenerException)27 CacheEntryUpdatedListener (javax.cache.event.CacheEntryUpdatedListener)24 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)22 Cache (javax.cache.Cache)22 HashMap (java.util.HashMap)21 List (java.util.List)19 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)19 CacheException (javax.cache.CacheException)17 IgniteEx (org.apache.ignite.internal.IgniteEx)17