Search in sources :

Example 71 with ContinuousQuery

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

the class CacheContinuousQueryConcurrentPartitionUpdateTest method concurrentUpdatePartition.

/**
     * @param atomicityMode Cache atomicity mode.
     * @throws Exception If failed.
     */
private void concurrentUpdatePartition(CacheAtomicityMode atomicityMode) throws Exception {
    Ignite srv = startGrid(0);
    client = true;
    Ignite client = startGrid(1);
    CacheConfiguration ccfg = new CacheConfiguration(DEFAULT_CACHE_NAME);
    ccfg.setWriteSynchronizationMode(FULL_SYNC);
    ccfg.setAtomicityMode(atomicityMode);
    IgniteCache clientCache = client.createCache(ccfg);
    final AtomicInteger evtCnt = new AtomicInteger();
    ContinuousQuery<Object, Object> qry = new ContinuousQuery<>();
    qry.setLocalListener(new CacheEntryUpdatedListener<Object, Object>() {

        @Override
        public void onUpdated(Iterable<CacheEntryEvent<?, ?>> evts) {
            for (CacheEntryEvent evt : evts) {
                assertNotNull(evt.getKey());
                assertNotNull(evt.getValue());
                evtCnt.incrementAndGet();
            }
        }
    });
    clientCache.query(qry);
    Affinity<Integer> aff = srv.affinity(DEFAULT_CACHE_NAME);
    final List<Integer> keys = new ArrayList<>();
    final int KEYS = 10;
    for (int i = 0; i < 100_000; i++) {
        if (aff.partition(i) == 0) {
            keys.add(i);
            if (keys.size() == KEYS)
                break;
        }
    }
    assertEquals(KEYS, keys.size());
    final int THREADS = 10;
    final int UPDATES = 1000;
    final IgniteCache<Object, Object> srvCache = srv.cache(DEFAULT_CACHE_NAME);
    for (int i = 0; i < 15; i++) {
        log.info("Iteration: " + i);
        GridTestUtils.runMultiThreaded(new Callable<Void>() {

            @Override
            public Void call() throws Exception {
                ThreadLocalRandom rnd = ThreadLocalRandom.current();
                for (int i = 0; i < UPDATES; i++) srvCache.put(keys.get(rnd.nextInt(KEYS)), i);
                return null;
            }
        }, THREADS, "update");
        GridTestUtils.waitForCondition(new GridAbsPredicate() {

            @Override
            public boolean apply() {
                log.info("Events: " + evtCnt.get());
                return evtCnt.get() >= THREADS * UPDATES;
            }
        }, 5000);
        assertEquals(THREADS * UPDATES, evtCnt.get());
        evtCnt.set(0);
    }
}
Also used : GridAbsPredicate(org.apache.ignite.internal.util.lang.GridAbsPredicate) IgniteCache(org.apache.ignite.IgniteCache) ArrayList(java.util.ArrayList) CacheEntryEvent(javax.cache.event.CacheEntryEvent) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Ignite(org.apache.ignite.Ignite) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration)

Example 72 with ContinuousQuery

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

the class IgniteCacheProxy method queryContinuous.

/**
     * Executes continuous query.
     *
     * @param qry Query.
     * @param loc Local flag.
     * @param keepBinary Keep binary flag.
     * @return Initial iteration cursor.
     */
@SuppressWarnings("unchecked")
private QueryCursor<Cache.Entry<K, V>> queryContinuous(ContinuousQuery qry, boolean loc, boolean keepBinary) {
    if (qry.getInitialQuery() instanceof ContinuousQuery)
        throw new IgniteException("Initial predicate for continuous query can't be an instance of another " + "continuous query. Use SCAN or SQL query for initial iteration.");
    if (qry.getLocalListener() == null)
        throw new IgniteException("Mandatory local listener is not set for the query: " + qry);
    if (qry.getRemoteFilter() != null && qry.getRemoteFilterFactory() != null)
        throw new IgniteException("Should be used either RemoterFilter or RemoteFilterFactory.");
    try {
        final UUID routineId = ctx.continuousQueries().executeQuery(qry.getLocalListener(), qry.getRemoteFilter(), qry.getRemoteFilterFactory(), qry.getPageSize(), qry.getTimeInterval(), qry.isAutoUnsubscribe(), loc, keepBinary, qry.isIncludeExpired());
        final QueryCursor<Cache.Entry<K, V>> cur = qry.getInitialQuery() != null ? query(qry.getInitialQuery()) : null;
        return new QueryCursor<Cache.Entry<K, V>>() {

            @Override
            public Iterator<Cache.Entry<K, V>> iterator() {
                return cur != null ? cur.iterator() : new GridEmptyIterator<Cache.Entry<K, V>>();
            }

            @Override
            public List<Cache.Entry<K, V>> getAll() {
                return cur != null ? cur.getAll() : Collections.<Cache.Entry<K, V>>emptyList();
            }

            @Override
            public void close() {
                if (cur != null)
                    cur.close();
                try {
                    ctx.kernalContext().continuous().stopRoutine(routineId).get();
                } catch (IgniteCheckedException e) {
                    throw U.convertException(e);
                }
            }
        };
    } catch (IgniteCheckedException e) {
        throw U.convertException(e);
    }
}
Also used : CacheEntry(org.apache.ignite.cache.CacheEntry) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) UUID(java.util.UUID) QueryCursor(org.apache.ignite.cache.query.QueryCursor) FieldsQueryCursor(org.apache.ignite.cache.query.FieldsQueryCursor) Cache(javax.cache.Cache) IgniteCache(org.apache.ignite.IgniteCache)

Example 73 with ContinuousQuery

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

the class IgniteCacheProxy method query.

/** {@inheritDoc} */
@SuppressWarnings("unchecked")
@Override
public <R> QueryCursor<R> query(Query<R> qry) {
    A.notNull(qry, "qry");
    GridCacheGateway<K, V> gate = this.gate;
    CacheOperationContext prev = onEnter(gate, opCtx);
    try {
        ctx.checkSecurity(SecurityPermission.CACHE_READ);
        validate(qry);
        convertToBinary(qry);
        CacheOperationContext opCtxCall = ctx.operationContextPerCall();
        boolean keepBinary = opCtxCall != null && opCtxCall.isKeepBinary();
        if (qry instanceof ContinuousQuery)
            return (QueryCursor<R>) queryContinuous((ContinuousQuery<K, V>) qry, qry.isLocal(), keepBinary);
        if (qry instanceof SqlQuery)
            return (QueryCursor<R>) ctx.kernalContext().query().querySql(ctx, (SqlQuery) qry, keepBinary);
        if (qry instanceof SqlFieldsQuery)
            return (FieldsQueryCursor<R>) ctx.kernalContext().query().querySqlFields(ctx, (SqlFieldsQuery) qry, keepBinary);
        if (qry instanceof ScanQuery)
            return query((ScanQuery) qry, null, projection(qry.isLocal()));
        return (QueryCursor<R>) query(qry, projection(qry.isLocal()));
    } catch (Exception e) {
        if (e instanceof CacheException)
            throw (CacheException) e;
        throw new CacheException(e);
    } finally {
        onLeave(gate, prev);
    }
}
Also used : SqlQuery(org.apache.ignite.cache.query.SqlQuery) CacheException(javax.cache.CacheException) ScanQuery(org.apache.ignite.cache.query.ScanQuery) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) EntryProcessorException(javax.cache.processor.EntryProcessorException) CacheException(javax.cache.CacheException) NoSuchElementException(java.util.NoSuchElementException) IOException(java.io.IOException) GridClosureException(org.apache.ignite.internal.util.lang.GridClosureException) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) QueryCursor(org.apache.ignite.cache.query.QueryCursor) FieldsQueryCursor(org.apache.ignite.cache.query.FieldsQueryCursor)

Example 74 with ContinuousQuery

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

the class TcpDiscoveryMultiThreadedTest method _testCustomEventNodeRestart.

/**
 * @throws Exception If failed.
 */
public void _testCustomEventNodeRestart() throws Exception {
    clientFlagGlobal = false;
    Ignite ignite = startGrid(0);
    ignite.getOrCreateCache(new CacheConfiguration<>(DEFAULT_CACHE_NAME));
    final long stopTime = System.currentTimeMillis() + 60_000;
    GridTestUtils.runMultiThreaded(new IgniteInClosure<Integer>() {

        @Override
        public void apply(Integer idx) {
            try {
                while (System.currentTimeMillis() < stopTime) {
                    Ignite ignite = startGrid(idx + 1);
                    IgniteCache<Object, Object> cache = ignite.cache(DEFAULT_CACHE_NAME);
                    int qryCnt = ThreadLocalRandom.current().nextInt(10) + 1;
                    for (int i = 0; i < qryCnt; i++) {
                        ContinuousQuery<Object, Object> qry = new ContinuousQuery<>();
                        qry.setLocalListener(new CacheEntryUpdatedListener<Object, Object>() {

                            @Override
                            public void onUpdated(Iterable<CacheEntryEvent<?, ?>> evts) {
                            // No-op.
                            }
                        });
                        QueryCursor<Cache.Entry<Object, Object>> cur = cache.query(qry);
                        cur.close();
                    }
                    GridTestUtils.invoke(ignite.configuration().getDiscoverySpi(), "simulateNodeFailure");
                    ignite.close();
                }
            } catch (Exception e) {
                log.error("Unexpected error: " + e, e);
                throw new IgniteException(e);
            }
        }
    }, 5, "node-restart");
}
Also used : IgniteCache(org.apache.ignite.IgniteCache) IgniteSpiException(org.apache.ignite.spi.IgniteSpiException) IgniteClientDisconnectedException(org.apache.ignite.IgniteClientDisconnectedException) IgniteClientDisconnectedCheckedException(org.apache.ignite.internal.IgniteClientDisconnectedCheckedException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) ClusterTopologyCheckedException(org.apache.ignite.internal.cluster.ClusterTopologyCheckedException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) CacheEntryUpdatedListener(javax.cache.event.CacheEntryUpdatedListener) IgniteException(org.apache.ignite.IgniteException) Ignite(org.apache.ignite.Ignite) QueryCursor(org.apache.ignite.cache.query.QueryCursor) Cache(javax.cache.Cache) IgniteCache(org.apache.ignite.IgniteCache)

Example 75 with ContinuousQuery

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

the class TcpDiscoveryMultiThreadedTest method _testCustomEventOnJoinCoordinatorStop.

/**
 * @throws Exception If failed.
 */
public void _testCustomEventOnJoinCoordinatorStop() throws Exception {
    for (int k = 0; k < 10; k++) {
        log.info("Iteration: " + k);
        clientFlagGlobal = false;
        final int START_NODES = 5;
        final int JOIN_NODES = 5;
        startGrids(START_NODES);
        final AtomicInteger startIdx = new AtomicInteger(START_NODES);
        final AtomicBoolean stop = new AtomicBoolean();
        IgniteInternalFuture<?> fut1 = GridTestUtils.runAsync(new Callable<Void>() {

            @Override
            public Void call() throws Exception {
                CacheConfiguration ccfg = new CacheConfiguration(DEFAULT_CACHE_NAME);
                Ignite ignite = ignite(START_NODES - 1);
                while (!stop.get()) {
                    ignite.createCache(ccfg);
                    ignite.destroyCache(ccfg.getName());
                }
                return null;
            }
        });
        try {
            final CyclicBarrier barrier = new CyclicBarrier(JOIN_NODES + 1);
            IgniteInternalFuture<?> fut2 = GridTestUtils.runMultiThreadedAsync(new Callable<Object>() {

                @Override
                public Object call() throws Exception {
                    int idx = startIdx.getAndIncrement();
                    Thread.currentThread().setName("start-thread-" + idx);
                    barrier.await();
                    Ignite ignite = startGrid(idx);
                    assertFalse(ignite.configuration().isClientMode());
                    log.info("Started node: " + ignite.name());
                    IgniteCache<Object, Object> cache = ignite.getOrCreateCache(DEFAULT_CACHE_NAME);
                    ContinuousQuery<Object, Object> qry = new ContinuousQuery<>();
                    qry.setLocalListener(new CacheEntryUpdatedListener<Object, Object>() {

                        @Override
                        public void onUpdated(Iterable<CacheEntryEvent<?, ?>> evts) {
                        // No-op.
                        }
                    });
                    QueryCursor<Cache.Entry<Object, Object>> cur = cache.query(qry);
                    cur.close();
                    return null;
                }
            }, JOIN_NODES, "start-thread");
            barrier.await();
            U.sleep(ThreadLocalRandom.current().nextInt(10, 100));
            for (int i = 0; i < START_NODES - 1; i++) {
                GridTestUtils.invoke(ignite(i).configuration().getDiscoverySpi(), "simulateNodeFailure");
                stopGrid(i);
            }
            stop.set(true);
            fut1.get();
            fut2.get();
        } finally {
            stop.set(true);
            fut1.get();
        }
        stopAllGrids();
    }
}
Also used : IgniteCache(org.apache.ignite.IgniteCache) IgniteSpiException(org.apache.ignite.spi.IgniteSpiException) IgniteClientDisconnectedException(org.apache.ignite.IgniteClientDisconnectedException) IgniteClientDisconnectedCheckedException(org.apache.ignite.internal.IgniteClientDisconnectedCheckedException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) ClusterTopologyCheckedException(org.apache.ignite.internal.cluster.ClusterTopologyCheckedException) CyclicBarrier(java.util.concurrent.CyclicBarrier) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CacheEntryUpdatedListener(javax.cache.event.CacheEntryUpdatedListener) Ignite(org.apache.ignite.Ignite) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) QueryCursor(org.apache.ignite.cache.query.QueryCursor) Cache(javax.cache.Cache) IgniteCache(org.apache.ignite.IgniteCache)

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