Search in sources :

Example 21 with QueryCursor

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

the class CacheScanPartitionQueryFallbackSelfTest method testScanFallbackOnRebalancingCursor1.

/**
     * Scan should activate fallback mechanism when new nodes join topology and rebalancing happens in parallel with
     * scan query.
     *
     * @throws Exception In case of error.
     */
public void testScanFallbackOnRebalancingCursor1() throws Exception {
    cacheMode = CacheMode.PARTITIONED;
    clientMode = false;
    backups = 1;
    commSpiFactory = new TestFallbackOnRebalancingCommunicationSpiFactory();
    try {
        Ignite ignite = startGrids(GRID_CNT);
        fillCache(ignite);
        final AtomicBoolean done = new AtomicBoolean(false);
        IgniteInternalFuture fut1 = multithreadedAsync(new Callable<Object>() {

            @Override
            public Object call() throws Exception {
                for (int i = 0; i < 5; i++) {
                    startGrid(GRID_CNT + i);
                    U.sleep(500);
                }
                done.set(true);
                return null;
            }
        }, 1);
        final AtomicInteger nodeIdx = new AtomicInteger();
        IgniteInternalFuture fut2 = multithreadedAsync(new Callable<Object>() {

            @Override
            public Object call() throws Exception {
                int nodeId = nodeIdx.getAndIncrement();
                IgniteCache<Integer, Integer> cache = grid(nodeId).cache(DEFAULT_CACHE_NAME);
                int cntr = 0;
                while (!done.get()) {
                    int part = ThreadLocalRandom.current().nextInt(ignite(nodeId).affinity(DEFAULT_CACHE_NAME).partitions());
                    if (cntr++ % 100 == 0)
                        info("Running query [node=" + nodeId + ", part=" + part + ']');
                    try (QueryCursor<Cache.Entry<Integer, Integer>> cur = cache.query(new ScanQuery<Integer, Integer>(part).setPageSize(5))) {
                        doTestScanQueryCursor(cur, part);
                    }
                }
                return null;
            }
        }, GRID_CNT);
        fut1.get();
        fut2.get();
    } finally {
        stopAllGrids();
    }
}
Also used : IgniteCache(org.apache.ignite.IgniteCache) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) IgniteSpiException(org.apache.ignite.spi.IgniteSpiException) IgniteException(org.apache.ignite.IgniteException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Ignite(org.apache.ignite.Ignite) QueryCursor(org.apache.ignite.cache.query.QueryCursor) Cache(javax.cache.Cache) IgniteCache(org.apache.ignite.IgniteCache)

Example 22 with QueryCursor

use of org.apache.ignite.cache.query.QueryCursor 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 23 with QueryCursor

use of org.apache.ignite.cache.query.QueryCursor 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)

Example 24 with QueryCursor

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

the class IgniteCacheAbstractQuerySelfTest method testSimpleCustomTableName.

/**
     * JUnit.
     *
     * @throws Exception In case of error.
     */
public void testSimpleCustomTableName() throws Exception {
    final IgniteCache<Integer, Object> cache = ignite().cache(DEFAULT_CACHE_NAME);
    cache.put(10, new Type1(1, "Type1 record #1"));
    cache.put(20, new Type1(2, "Type1 record #2"));
    QueryCursor<Cache.Entry<Integer, Type1>> qry1 = cache.query(new SqlQuery<Integer, Type1>(Type1.class, "FROM Type2"));
    List<Cache.Entry<Integer, Type1>> all = qry1.getAll();
    assertEquals(2, all.size());
    QueryCursor<List<?>> qry = cache.query(new SqlFieldsQuery("SELECT name FROM Type2"));
    assertEquals(2, qry.getAll().size());
    GridTestUtils.assertThrows(log, new GridPlainCallable<Void>() {

        @Override
        public Void call() throws Exception {
            QueryCursor<Cache.Entry<Integer, Type1>> qry = cache.query(new SqlQuery<Integer, Type1>(Type1.class, "FROM Type1"));
            qry.getAll();
            return null;
        }
    }, CacheException.class, null);
}
Also used : SqlQuery(org.apache.ignite.cache.query.SqlQuery) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) CacheException(javax.cache.CacheException) IOException(java.io.IOException) BinaryObject(org.apache.ignite.binary.BinaryObject) List(java.util.List) ArrayList(java.util.ArrayList) QueryCursor(org.apache.ignite.cache.query.QueryCursor) Cache(javax.cache.Cache) IgniteCache(org.apache.ignite.IgniteCache)

Example 25 with QueryCursor

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

the class IgniteClientReconnectQueriesTest method scanQueryReconnectInProgress.

/**
     * @param setPart If {@code true} sets partition for scan query.
     * @throws Exception If failed.
     */
private void scanQueryReconnectInProgress(boolean setPart) throws Exception {
    Ignite cln = grid(serverCount());
    assertTrue(cln.cluster().localNode().isClient());
    final Ignite srv = clientRouter(cln);
    final IgniteCache<Integer, Person> clnCache = cln.getOrCreateCache(QUERY_CACHE);
    clnCache.put(1, new Person(1, "name1", "surname1"));
    clnCache.put(2, new Person(2, "name2", "surname2"));
    clnCache.put(3, new Person(3, "name3", "surname3"));
    final ScanQuery<Integer, Person> scanQry = new ScanQuery<>();
    scanQry.setPageSize(1);
    scanQry.setFilter(new IgniteBiPredicate<Integer, Person>() {

        @Override
        public boolean apply(Integer integer, Person person) {
            return true;
        }
    });
    if (setPart)
        scanQry.setPartition(1);
    blockMessage(GridCacheQueryResponse.class);
    final IgniteInternalFuture<Object> fut = GridTestUtils.runAsync(new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            try {
                QueryCursor<Cache.Entry<Integer, Person>> qryCursor = clnCache.query(scanQry);
                qryCursor.getAll();
            } catch (CacheException e) {
                checkAndWait(e);
                return true;
            }
            return false;
        }
    });
    // Check that client waiting operation.
    GridTestUtils.assertThrows(log, new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            return fut.get(200);
        }
    }, IgniteFutureTimeoutCheckedException.class, null);
    assertNotDone(fut);
    unblockMessage();
    reconnectClientNode(cln, srv, null);
    assertTrue((Boolean) fut.get(2, SECONDS));
    QueryCursor<Cache.Entry<Integer, Person>> qryCursor2 = clnCache.query(scanQry);
    List<Cache.Entry<Integer, Person>> entries = qryCursor2.getAll();
    assertEquals(setPart ? 1 : 3, entries.size());
    for (Cache.Entry<Integer, Person> entry : entries) {
        assertEquals(Integer.class, entry.getKey().getClass());
        assertEquals(Person.class, entry.getValue().getClass());
    }
}
Also used : CacheException(javax.cache.CacheException) ScanQuery(org.apache.ignite.cache.query.ScanQuery) CacheException(javax.cache.CacheException) IgniteFutureTimeoutCheckedException(org.apache.ignite.internal.IgniteFutureTimeoutCheckedException) Ignite(org.apache.ignite.Ignite) QueryCursor(org.apache.ignite.cache.query.QueryCursor) IgniteCache(org.apache.ignite.IgniteCache) Cache(javax.cache.Cache)

Aggregations

QueryCursor (org.apache.ignite.cache.query.QueryCursor)34 ContinuousQuery (org.apache.ignite.cache.query.ContinuousQuery)20 IgniteCache (org.apache.ignite.IgniteCache)16 Ignite (org.apache.ignite.Ignite)14 Cache (javax.cache.Cache)13 ArrayList (java.util.ArrayList)12 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)10 IgniteException (org.apache.ignite.IgniteException)10 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)9 CacheEntryEvent (javax.cache.event.CacheEntryEvent)8 CacheEntryListenerException (javax.cache.event.CacheEntryListenerException)7 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)6 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)6 CacheException (javax.cache.CacheException)6 CacheEntryUpdatedListener (javax.cache.event.CacheEntryUpdatedListener)6 SqlFieldsQuery (org.apache.ignite.cache.query.SqlFieldsQuery)6 SqlQuery (org.apache.ignite.cache.query.SqlQuery)6 ScanQuery (org.apache.ignite.cache.query.ScanQuery)5 PA (org.apache.ignite.internal.util.typedef.PA)5 EntryProcessorException (javax.cache.processor.EntryProcessorException)4