Search in sources :

Example 16 with ScanQuery

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

the class CacheScanPartitionQueryFallbackSelfTest method scanFallbackOnRebalancing.

/**
     * @param cur If {@code true} tests query cursor.
     * @throws Exception In case of error.
     */
private void scanFallbackOnRebalancing(final boolean cur) throws Exception {
    cacheMode = CacheMode.PARTITIONED;
    clientMode = false;
    backups = 2;
    commSpiFactory = new TestFallbackOnRebalancingCommunicationSpiFactory();
    syncRebalance = true;
    try {
        Ignite ignite = startGrids(GRID_CNT);
        fillCache(ignite);
        final AtomicBoolean done = new AtomicBoolean(false);
        final AtomicInteger idx = new AtomicInteger(GRID_CNT);
        IgniteInternalFuture fut1 = multithreadedAsync(new Callable<Object>() {

            @Override
            public Object call() throws Exception {
                int id = idx.getAndIncrement();
                while (!done.get()) {
                    startGrid(id);
                    Thread.sleep(3000);
                    info("Will stop grid: " + getTestIgniteInstanceName(id));
                    stopGrid(id);
                    if (done.get())
                        return null;
                    Thread.sleep(3000);
                }
                return null;
            }
        }, 2);
        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>> cur0 = cache.query(new ScanQuery<Integer, Integer>(part))) {
                        if (cur)
                            doTestScanQueryCursor(cur0, part);
                        else
                            doTestScanQuery(cur0, part);
                    }
                }
                return null;
            }
        }, GRID_CNT);
        // Test for one minute
        Thread.sleep(60 * 1000);
        done.set(true);
        fut2.get();
        fut1.get();
    } finally {
        stopAllGrids();
    }
}
Also used : IgniteCache(org.apache.ignite.IgniteCache) ScanQuery(org.apache.ignite.cache.query.ScanQuery) 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 17 with ScanQuery

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

the class CacheScanPartitionQueryFallbackSelfTest method testScanLocal.

/**
     * Scan should perform on the local node.
     *
     * @throws Exception If failed.
     */
public void testScanLocal() throws Exception {
    cacheMode = CacheMode.PARTITIONED;
    backups = 0;
    commSpiFactory = new TestLocalCommunicationSpiFactory();
    try {
        Ignite ignite = startGrids(GRID_CNT);
        IgniteCacheProxy<Integer, Integer> cache = fillCache(ignite);
        int part = anyLocalPartition(cache.context());
        QueryCursor<Cache.Entry<Integer, Integer>> qry = cache.query(new ScanQuery<Integer, Integer>().setPartition(part));
        doTestScanQuery(qry, part);
    } finally {
        stopAllGrids();
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ScanQuery(org.apache.ignite.cache.query.ScanQuery) Ignite(org.apache.ignite.Ignite)

Example 18 with ScanQuery

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

the class IgniteCacheP2pUnmarshallingQueryErrorTest method testResponseMessageOnRequestUnmarshallingFailed.

/**
     * @throws Exception If failed.
     */
public void testResponseMessageOnRequestUnmarshallingFailed() throws Exception {
    readCnt.set(Integer.MAX_VALUE);
    try {
        jcache().query(new ScanQuery<>(new IgniteBiPredicate<TestKey, String>() {

            @Override
            public boolean apply(TestKey key, String val) {
                return false;
            }

            private void readObject(ObjectInputStream is) throws IOException {
                throw new IOException();
            }

            private void writeObject(ObjectOutputStream os) throws IOException {
            // No-op.
            }
        })).getAll();
        fail();
    } catch (Exception ignored) {
    // No-op.
    }
}
Also used : ScanQuery(org.apache.ignite.cache.query.ScanQuery) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream) IOException(java.io.IOException) CacheException(javax.cache.CacheException) ObjectInputStream(java.io.ObjectInputStream)

Example 19 with ScanQuery

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

the class IgniteCachePartitionedQuerySelfTest method testScanQueryPagination.

/**
     * @throws Exception If failed.
     */
public void testScanQueryPagination() throws Exception {
    final int pageSize = 5;
    final AtomicInteger pages = new AtomicInteger(0);
    IgniteCache<Integer, Integer> cache = jcache(Integer.class, Integer.class);
    for (int i = 0; i < 50; i++) cache.put(i, i);
    CommunicationSpi spi = ignite().configuration().getCommunicationSpi();
    assert spi instanceof TestTcpCommunicationSpi;
    TestTcpCommunicationSpi commSpi = (TestTcpCommunicationSpi) spi;
    commSpi.filter = new IgniteInClosure<Message>() {

        @Override
        public void apply(Message msg) {
            if (!(msg instanceof GridIoMessage))
                return;
            Message msg0 = ((GridIoMessage) msg).message();
            if (msg0 instanceof GridCacheQueryRequest) {
                assertEquals(pageSize, ((GridCacheQueryRequest) msg0).pageSize());
                pages.incrementAndGet();
            } else if (msg0 instanceof GridCacheQueryResponse)
                assertTrue(((GridCacheQueryResponse) msg0).data().size() <= pageSize);
        }
    };
    try {
        ScanQuery<Integer, Integer> qry = new ScanQuery<Integer, Integer>();
        qry.setPageSize(pageSize);
        List<Cache.Entry<Integer, Integer>> all = cache.query(qry).getAll();
        assertTrue(pages.get() > ignite().cluster().forDataNodes(DEFAULT_CACHE_NAME).nodes().size());
        assertEquals(50, all.size());
    } finally {
        commSpi.filter = null;
    }
}
Also used : GridIoMessage(org.apache.ignite.internal.managers.communication.GridIoMessage) CommunicationSpi(org.apache.ignite.spi.communication.CommunicationSpi) TcpCommunicationSpi(org.apache.ignite.spi.communication.tcp.TcpCommunicationSpi) GridIoMessage(org.apache.ignite.internal.managers.communication.GridIoMessage) Message(org.apache.ignite.plugin.extensions.communication.Message) ScanQuery(org.apache.ignite.cache.query.ScanQuery) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) GridCacheQueryResponse(org.apache.ignite.internal.processors.cache.query.GridCacheQueryResponse) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) GridCacheQueryRequest(org.apache.ignite.internal.processors.cache.query.GridCacheQueryRequest)

Example 20 with ScanQuery

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

the class CacheAbstractQueryDetailMetricsSelfTest method testQueryMetricsEviction.

/**
     * Test metrics eviction.
     *
     * @throws Exception In case of error.
     */
public void testQueryMetricsEviction() throws Exception {
    IgniteCache<Integer, String> cache = grid(0).context().cache().jcache("A");
    // Execute several DIFFERENT queries with guaranteed DIFFERENT time of execution.
    cache.query(new SqlFieldsQuery("select * from String")).getAll();
    Thread.sleep(100);
    cache.query(new SqlFieldsQuery("select count(*) from String")).getAll();
    Thread.sleep(100);
    cache.query(new SqlFieldsQuery("select * from String limit 1")).getAll();
    Thread.sleep(100);
    cache.query(new SqlFieldsQuery("select * from String limit 2")).getAll();
    Thread.sleep(100);
    cache.query(new ScanQuery()).getAll();
    Thread.sleep(100);
    cache.query(new SqlQuery("String", "from String")).getAll();
    waitingFor(cache, "size", QRY_DETAIL_METRICS_SIZE);
    for (int i = 0; i < QRY_DETAIL_METRICS_SIZE; i++) checkMetrics(cache, QRY_DETAIL_METRICS_SIZE, i, 1, 1, 0, false);
    // Check that collected metrics contains correct items: metrics for last 3 queries.
    Collection<? extends QueryDetailMetrics> metrics = cache.queryDetailMetrics();
    String lastMetrics = "";
    for (QueryDetailMetrics m : metrics) lastMetrics += m.queryType() + " " + m.query() + ";";
    assertTrue(lastMetrics.contains("SQL_FIELDS select * from String limit 2;"));
    assertTrue(lastMetrics.contains("SCAN A;"));
    assertTrue(lastMetrics.contains("SQL from String;"));
    cache = grid(0).context().cache().jcache("B");
    cache.query(new SqlFieldsQuery("select * from String")).getAll();
    cache.query(new SqlFieldsQuery("select count(*) from String")).getAll();
    cache.query(new SqlFieldsQuery("select * from String limit 1")).getAll();
    cache.query(new SqlFieldsQuery("select * from String limit 2")).getAll();
    cache.query(new ScanQuery()).getAll();
    cache.query(new SqlQuery("String", "from String")).getAll();
    waitingFor(cache, "size", QRY_DETAIL_METRICS_SIZE);
    for (int i = 0; i < QRY_DETAIL_METRICS_SIZE; i++) checkMetrics(cache, QRY_DETAIL_METRICS_SIZE, i, 1, 1, 0, false);
    if (gridCnt > 1) {
        cache = grid(1).context().cache().jcache("A");
        cache.query(new SqlFieldsQuery("select * from String")).getAll();
        cache.query(new SqlFieldsQuery("select count(*) from String")).getAll();
        cache.query(new SqlFieldsQuery("select * from String limit 1")).getAll();
        cache.query(new SqlFieldsQuery("select * from String limit 2")).getAll();
        cache.query(new ScanQuery()).getAll();
        cache.query(new SqlQuery("String", "from String")).getAll();
        waitingFor(cache, "size", QRY_DETAIL_METRICS_SIZE);
        for (int i = 0; i < QRY_DETAIL_METRICS_SIZE; i++) checkMetrics(cache, QRY_DETAIL_METRICS_SIZE, i, 1, 1, 0, false);
        cache = grid(1).context().cache().jcache("B");
        cache.query(new SqlFieldsQuery("select * from String")).getAll();
        cache.query(new SqlFieldsQuery("select count(*) from String")).getAll();
        cache.query(new SqlFieldsQuery("select * from String limit 1")).getAll();
        cache.query(new SqlFieldsQuery("select * from String limit 2")).getAll();
        cache.query(new ScanQuery()).getAll();
        cache.query(new SqlQuery("String", "from String")).getAll();
        waitingFor(cache, "size", QRY_DETAIL_METRICS_SIZE);
        for (int i = 0; i < QRY_DETAIL_METRICS_SIZE; i++) checkMetrics(cache, QRY_DETAIL_METRICS_SIZE, i, 1, 1, 0, false);
    }
}
Also used : QueryDetailMetrics(org.apache.ignite.cache.query.QueryDetailMetrics) SqlQuery(org.apache.ignite.cache.query.SqlQuery) ScanQuery(org.apache.ignite.cache.query.ScanQuery) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery)

Aggregations

ScanQuery (org.apache.ignite.cache.query.ScanQuery)29 Ignite (org.apache.ignite.Ignite)17 Cache (javax.cache.Cache)16 IgniteCache (org.apache.ignite.IgniteCache)16 CacheException (javax.cache.CacheException)7 List (java.util.List)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 QueryCursor (org.apache.ignite.cache.query.QueryCursor)5 ArrayList (java.util.ArrayList)4 IgniteException (org.apache.ignite.IgniteException)4 BinaryObject (org.apache.ignite.binary.BinaryObject)4 IOException (java.io.IOException)3 Collection (java.util.Collection)3 Collections (java.util.Collections)3 Map (java.util.Map)3 BinaryOperator (java.util.function.BinaryOperator)3 EntryProcessorException (javax.cache.processor.EntryProcessorException)3 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)3 Ignition (org.apache.ignite.Ignition)3 Affinity (org.apache.ignite.cache.affinity.Affinity)3