Search in sources :

Example 56 with QueryCursor

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

the class IgniteCacheQueryCacheDestroySelfTest method runQuery.

/**
 * @throws Exception If failed.
 */
private void runQuery() throws Exception {
    ScanQuery<String, String> scanQuery = new ScanQuery<String, String>().setLocal(true).setFilter(new IgniteBiPredicate<String, String>() {

        @Override
        public boolean apply(String key, String p) {
            return key != null && key.isEmpty();
        }
    });
    Ignite ignite = ignite(ThreadLocalRandom.current().nextInt(GRID_CNT));
    IgniteCache<String, String> example = ignite.cache(CACHE_NAME);
    for (int partition : ignite.affinity(CACHE_NAME).primaryPartitions(ignite.cluster().localNode())) {
        scanQuery.setPartition(partition);
        try (QueryCursor cursor = example.query(scanQuery)) {
            for (Object p : cursor) {
                String value = (String) ((Cache.Entry) p).getValue();
                assertNotNull(value);
            }
        }
    }
}
Also used : ScanQuery(org.apache.ignite.cache.query.ScanQuery) Ignite(org.apache.ignite.Ignite) QueryCursor(org.apache.ignite.cache.query.QueryCursor) IgniteCache(org.apache.ignite.IgniteCache) Cache(javax.cache.Cache)

Example 57 with QueryCursor

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

the class LoadTest method testMultithreading.

/**
 * Test thin client in multi-thread environment.
 */
@Test
public void testMultithreading() throws Exception {
    final int THREAD_CNT = 8;
    final int ITERATION_CNT = 20;
    final int BATCH_SIZE = 1000;
    final int PAGE_CNT = 3;
    IgniteConfiguration srvCfg = Config.getServerConfiguration();
    // No peer class loading from thin clients: we need the server to know about this class to deserialize
    // ScanQuery filter.
    srvCfg.setBinaryConfiguration(new BinaryConfiguration().setTypeConfigurations(Arrays.asList(new BinaryTypeConfiguration(getClass().getName()), new BinaryTypeConfiguration(SerializedLambda.class.getName()))));
    try (Ignite ignored = Ignition.start(srvCfg);
        IgniteClient client = Ignition.startClient(new ClientConfiguration().setAddresses(Config.SERVER))) {
        ClientCache<Integer, String> cache = client.createCache("testMultithreading");
        AtomicInteger cnt = new AtomicInteger(1);
        AtomicReference<Throwable> error = new AtomicReference<>();
        Runnable assertion = () -> {
            try {
                int rangeStart = cnt.getAndAdd(BATCH_SIZE);
                int rangeEnd = rangeStart + BATCH_SIZE;
                Map<Integer, String> data = IntStream.range(rangeStart, rangeEnd).boxed().collect(Collectors.toMap(i -> i, i -> String.format("String %s", i)));
                cache.putAll(data);
                Query<Cache.Entry<Integer, String>> qry = new ScanQuery<Integer, String>().setPageSize(data.size() / PAGE_CNT).setFilter((i, s) -> i >= rangeStart && i < rangeEnd);
                try (QueryCursor<Cache.Entry<Integer, String>> cur = cache.query(qry)) {
                    List<Cache.Entry<Integer, String>> res = cur.getAll();
                    assertEquals("Unexpected number of entries", data.size(), res.size());
                    Map<Integer, String> act = res.stream().collect(Collectors.toMap(Cache.Entry::getKey, Cache.Entry::getValue));
                    assertEquals("Unexpected entries", data, act);
                }
            } catch (Throwable ex) {
                error.set(ex);
            }
        };
        CountDownLatch complete = new CountDownLatch(THREAD_CNT);
        Runnable manyAssertions = () -> {
            for (int i = 0; i < ITERATION_CNT && error.get() == null; i++) assertion.run();
            complete.countDown();
        };
        ExecutorService threadPool = Executors.newFixedThreadPool(THREAD_CNT);
        IntStream.range(0, THREAD_CNT).forEach(t -> threadPool.submit(manyAssertions));
        assertTrue("Timeout", complete.await(180, TimeUnit.SECONDS));
        String errMsg = error.get() == null ? "" : error.get().getMessage();
        assertNull(errMsg, error.get());
    }
}
Also used : IntStream(java.util.stream.IntStream) Arrays(java.util.Arrays) AtomicReference(java.util.concurrent.atomic.AtomicReference) BinaryConfiguration(org.apache.ignite.configuration.BinaryConfiguration) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SerializedLambda(java.lang.invoke.SerializedLambda) Map(java.util.Map) Cache(javax.cache.Cache) Timeout(org.junit.rules.Timeout) ExecutorService(java.util.concurrent.ExecutorService) Query(org.apache.ignite.cache.query.Query) BinaryTypeConfiguration(org.apache.ignite.binary.BinaryTypeConfiguration) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Ignite(org.apache.ignite.Ignite) Collectors(java.util.stream.Collectors) Executors(java.util.concurrent.Executors) GridTestUtils(org.apache.ignite.testframework.GridTestUtils) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) Ignition(org.apache.ignite.Ignition) Rule(org.junit.Rule) Assert.assertNull(org.junit.Assert.assertNull) ClientConfiguration(org.apache.ignite.configuration.ClientConfiguration) QueryCursor(org.apache.ignite.cache.query.QueryCursor) ScanQuery(org.apache.ignite.cache.query.ScanQuery) Assert.assertEquals(org.junit.Assert.assertEquals) Query(org.apache.ignite.cache.query.Query) ScanQuery(org.apache.ignite.cache.query.ScanQuery) Ignite(org.apache.ignite.Ignite) List(java.util.List) QueryCursor(org.apache.ignite.cache.query.QueryCursor) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) BinaryConfiguration(org.apache.ignite.configuration.BinaryConfiguration) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ExecutorService(java.util.concurrent.ExecutorService) BinaryTypeConfiguration(org.apache.ignite.binary.BinaryTypeConfiguration) Map(java.util.Map) ClientConfiguration(org.apache.ignite.configuration.ClientConfiguration) Cache(javax.cache.Cache) Test(org.junit.Test)

Example 58 with QueryCursor

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

the class IgniteCacheContinuousQueryBackupQueueTest method testBackupQueueAutoUnsubscribeFalse.

/**
 * @throws Exception If failed.
 */
@Test
public void testBackupQueueAutoUnsubscribeFalse() throws Exception {
    try (Ignite client = startClientGrid(GRID_COUNT)) {
        awaitPartitionMapExchange();
        List<QueryCursor> qryCursors = new ArrayList<>();
        for (int i = 0; i < QUERY_COUNT; i++) {
            ContinuousQuery<Object, Object> qry = new ContinuousQuery<>();
            qry.setLocalListener(new CacheEventListener());
            qry.setRemoteFilterFactory(new AlwaysFalseFilterFactory());
            qry.setAutoUnsubscribe(false);
            qryCursors.add(client.cache(CACHE_NAME).query(qry));
        }
        for (int i = 0; i < KEYS_COUNT; i++) {
            log.info("Put key: " + i);
            grid(i % GRID_COUNT).cache(CACHE_NAME).put(i, new byte[1024 * 50]);
        }
        int size = backupQueueSize();
        assertTrue(size > 0);
        assertTrue(size <= BACKUP_ACK_THRESHOLD * QUERY_COUNT * /* partition count */
        1024);
        stopGrid(GRID_COUNT);
        awaitPartitionMapExchange();
        for (int i = 0; i < KEYS_COUNT; i++) {
            log.info("Put key: " + i);
            grid(i % GRID_COUNT).cache(CACHE_NAME).put(i, new byte[1024 * 50]);
        }
        size = backupQueueSize();
        assertEquals(-1, size);
    }
}
Also used : ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) ArrayList(java.util.ArrayList) Ignite(org.apache.ignite.Ignite) QueryCursor(org.apache.ignite.cache.query.QueryCursor) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 59 with QueryCursor

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

the class IgniteCacheContinuousQueryBackupQueueTest method testManyQueryBackupQueue.

/**
 * @throws Exception If failed.
 */
@Test
public void testManyQueryBackupQueue() throws Exception {
    List<QueryCursor> qryCursors = new ArrayList<>();
    for (int i = 0; i < QUERY_COUNT; i++) {
        ContinuousQuery<Object, Object> qry = new ContinuousQuery<>();
        qry.setLocalListener(new CacheEventListener());
        qry.setRemoteFilterFactory(new AlwaysFalseFilterFactory());
        qryCursors.add(grid(0).cache(CACHE_NAME).query(qry));
    }
    for (int i = 0; i < KEYS_COUNT; i++) {
        log.info("Put key: " + i);
        for (int j = 0; j < 150; j++) grid(ThreadLocalRandom.current().nextInt(GRID_COUNT)).cache(CACHE_NAME).put(i, new byte[1024 * 50]);
    }
    int size = backupQueueSize();
    // Backup queues total size should not exceed one entry per query per partition. This is because
    // {@link CacheContinuousQueryEventBuffer} is optimized to store filtered events and
    // used in this test {@link AlwaysFalseFilterFactory} always declines updates.
    // Zero total size is possible when backup queue is cleaned by timeout.
    assertTrue(size <= QUERY_COUNT * /* partition count */
    1024);
    for (QueryCursor qry : qryCursors) qry.close();
}
Also used : ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) ArrayList(java.util.ArrayList) QueryCursor(org.apache.ignite.cache.query.QueryCursor) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 60 with QueryCursor

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

the class FunctionalQueryTest method testQueries.

/**
 * Tested API:
 * <ul>
 * <li>{@link ClientCache#query(Query)}</li>
 * </ul>
 */
@Test
public void testQueries() throws Exception {
    IgniteConfiguration srvCfg = Config.getServerConfiguration();
    // No peer class loading from thin clients: we need the server to know about this class to deserialize
    // ScanQuery filter.
    srvCfg.setBinaryConfiguration(new BinaryConfiguration().setTypeConfigurations(Arrays.asList(new BinaryTypeConfiguration(getClass().getName()), new BinaryTypeConfiguration(SerializedLambda.class.getName()))));
    try (Ignite ignored = Ignition.start(srvCfg);
        IgniteClient client = Ignition.startClient(getClientConfiguration())) {
        ClientCache<Integer, Person> cache = client.getOrCreateCache(Config.DEFAULT_CACHE_NAME);
        Map<Integer, Person> data = IntStream.rangeClosed(1, 100).boxed().collect(Collectors.toMap(i -> i, i -> new Person(i, String.format("Person %s", i))));
        cache.putAll(data);
        int minId = data.size() / 2 + 1;
        int pageSize = (data.size() - minId) / 3;
        // expected query result size
        int expSize = data.size() - minId + 1;
        // Expected result
        Map<Integer, Person> exp = data.entrySet().stream().filter(e -> e.getKey() >= minId).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
        // Scan and SQL queries
        Collection<Query<Cache.Entry<Integer, Person>>> queries = Arrays.asList(new ScanQuery<Integer, Person>((i, p) -> p.getId() >= minId).setPageSize(pageSize), new SqlQuery<Integer, Person>(Person.class, "id >= ?").setArgs(minId).setPageSize(pageSize));
        for (Query<Cache.Entry<Integer, Person>> qry : queries) {
            try (QueryCursor<Cache.Entry<Integer, Person>> cur = cache.query(qry)) {
                List<Cache.Entry<Integer, Person>> res = cur.getAll();
                assertEquals(String.format("Unexpected number of rows from %s", qry.getClass().getSimpleName()), expSize, res.size());
                Map<Integer, Person> act = res.stream().collect(Collectors.toMap(Cache.Entry::getKey, Cache.Entry::getValue));
                assertEquals(String.format("unexpected rows from %s", qry.getClass().getSimpleName()), exp, act);
            }
        }
        checkSqlFieldsQuery(cache, minId, pageSize, expSize, exp, true);
        checkSqlFieldsQuery(cache, minId, pageSize, expSize, exp, false);
    }
}
Also used : IntStream(java.util.stream.IntStream) Arrays(java.util.Arrays) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) BinaryConfiguration(org.apache.ignite.configuration.BinaryConfiguration) SerializedLambda(java.lang.invoke.SerializedLambda) Map(java.util.Map) SqlQuery(org.apache.ignite.cache.query.SqlQuery) Cache(javax.cache.Cache) Timeout(org.junit.rules.Timeout) Query(org.apache.ignite.cache.query.Query) BinaryTypeConfiguration(org.apache.ignite.binary.BinaryTypeConfiguration) Assert.assertNotNull(org.junit.Assert.assertNotNull) Collection(java.util.Collection) Test(org.junit.Test) Ignite(org.apache.ignite.Ignite) Field(java.lang.reflect.Field) Collectors(java.util.stream.Collectors) GridTestUtils(org.apache.ignite.testframework.GridTestUtils) List(java.util.List) IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) Ignition(org.apache.ignite.Ignition) Rule(org.junit.Rule) ClientConfiguration(org.apache.ignite.configuration.ClientConfiguration) QueryCursor(org.apache.ignite.cache.query.QueryCursor) ScanQuery(org.apache.ignite.cache.query.ScanQuery) Assert.assertEquals(org.junit.Assert.assertEquals) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) SqlQuery(org.apache.ignite.cache.query.SqlQuery) Query(org.apache.ignite.cache.query.Query) ScanQuery(org.apache.ignite.cache.query.ScanQuery) ScanQuery(org.apache.ignite.cache.query.ScanQuery) IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) BinaryConfiguration(org.apache.ignite.configuration.BinaryConfiguration) BinaryTypeConfiguration(org.apache.ignite.binary.BinaryTypeConfiguration) Ignite(org.apache.ignite.Ignite) Map(java.util.Map) Cache(javax.cache.Cache) Test(org.junit.Test)

Aggregations

QueryCursor (org.apache.ignite.cache.query.QueryCursor)72 IgniteCache (org.apache.ignite.IgniteCache)44 Ignite (org.apache.ignite.Ignite)38 ContinuousQuery (org.apache.ignite.cache.query.ContinuousQuery)36 Cache (javax.cache.Cache)33 Test (org.junit.Test)29 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)28 ScanQuery (org.apache.ignite.cache.query.ScanQuery)26 ArrayList (java.util.ArrayList)24 IgniteException (org.apache.ignite.IgniteException)22 List (java.util.List)19 SqlFieldsQuery (org.apache.ignite.cache.query.SqlFieldsQuery)19 Map (java.util.Map)17 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)17 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)17 CacheException (javax.cache.CacheException)16 GridTestUtils (org.apache.ignite.testframework.GridTestUtils)16 Collection (java.util.Collection)15 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)15 Transaction (org.apache.ignite.transactions.Transaction)15