Search in sources :

Example 61 with QueryCursor

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

the class SqlSystemViewsSelfTest method testRunningQueriesView.

/**
 * Test running queries system view.
 */
@Test
public void testRunningQueriesView() throws Exception {
    IgniteEx ignite = startGrid(0);
    IgniteCache cache = ignite.createCache(new CacheConfiguration<>(DEFAULT_CACHE_NAME).setIndexedTypes(Integer.class, String.class));
    cache.put(100, "200");
    String sql = "SELECT SQL, QUERY_ID, SCHEMA_NAME, LOCAL, START_TIME, DURATION FROM " + systemSchemaName() + ".SQL_QUERIES";
    FieldsQueryCursor notClosedFieldQryCursor = cache.query(new SqlFieldsQuery(sql).setLocal(true));
    List<?> cur = cache.query(new SqlFieldsQuery(sql).setLocal(true)).getAll();
    assertEquals(2, cur.size());
    List<?> res0 = (List<?>) cur.get(0);
    List<?> res1 = (List<?>) cur.get(1);
    Timestamp ts = (Timestamp) res0.get(4);
    Instant now = Instant.now();
    long diffInMillis = now.minusMillis(ts.getTime()).toEpochMilli();
    assertTrue(diffInMillis < 3000);
    assertEquals(sql, res0.get(0));
    assertEquals(sql, res1.get(0));
    assertTrue((Boolean) res0.get(3));
    String id0 = (String) res0.get(1);
    String id1 = (String) res1.get(1);
    assertNotEquals(id0, id1);
    String qryPrefix = ignite.localNode().id() + "_";
    String qryId1 = qryPrefix + "1";
    String qryId2 = qryPrefix + "2";
    assertTrue(id0.equals(qryId1) || id1.equals(qryId1));
    assertTrue(id0.equals(qryId2) || id1.equals(qryId2));
    assertEquals(2, cache.query(new SqlFieldsQuery(sql)).getAll().size());
    notClosedFieldQryCursor.close();
    assertEquals(1, cache.query(new SqlFieldsQuery(sql)).getAll().size());
    cache.put(100, "200");
    QueryCursor notClosedQryCursor = cache.query(new SqlQuery<>(String.class, "_key=100"));
    String expSqlQry = "SELECT \"default\".\"STRING\"._KEY, \"default\".\"STRING\"._VAL FROM " + "\"default\".\"STRING\" WHERE _key=100";
    cur = cache.query(new SqlFieldsQuery(sql)).getAll();
    assertEquals(2, cur.size());
    res0 = (List<?>) cur.get(0);
    res1 = (List<?>) cur.get(1);
    assertTrue(expSqlQry, res0.get(0).equals(expSqlQry) || res1.get(0).equals(expSqlQry));
    assertFalse((Boolean) res0.get(3));
    assertFalse((Boolean) res1.get(3));
    notClosedQryCursor.close();
    sql = "SELECT SQL, QUERY_ID FROM " + systemSchemaName() + ".SQL_QUERIES WHERE QUERY_ID='" + qryPrefix + "7'";
    assertEquals(qryPrefix + "7", ((List<?>) cache.query(new SqlFieldsQuery(sql)).getAll().get(0)).get(1));
    sql = "SELECT SQL FROM " + systemSchemaName() + ".SQL_QUERIES WHERE DURATION > 100000";
    assertTrue(cache.query(new SqlFieldsQuery(sql)).getAll().isEmpty());
    sql = "SELECT SQL FROM " + systemSchemaName() + ".SQL_QUERIES WHERE QUERY_ID='UNKNOWN'";
    assertTrue(cache.query(new SqlFieldsQuery(sql)).getAll().isEmpty());
}
Also used : FieldsQueryCursor(org.apache.ignite.cache.query.FieldsQueryCursor) Instant(java.time.Instant) IgniteCache(org.apache.ignite.IgniteCache) Timestamp(java.sql.Timestamp) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) IgniteEx(org.apache.ignite.internal.IgniteEx) Arrays.asList(java.util.Arrays.asList) List(java.util.List) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) QueryCursor(org.apache.ignite.cache.query.QueryCursor) FieldsQueryCursor(org.apache.ignite.cache.query.FieldsQueryCursor) AbstractIndexingCommonTest(org.apache.ignite.internal.processors.cache.index.AbstractIndexingCommonTest) AbstractSchemaSelfTest(org.apache.ignite.internal.processors.cache.index.AbstractSchemaSelfTest) Test(org.junit.Test)

Example 62 with QueryCursor

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

the class IgniteAbstractPageReplacementBenchmark method setUp.

/**
 * {@inheritDoc}
 */
@Override
public void setUp(BenchmarkConfiguration cfg) throws Exception {
    super.setUp(cfg);
    int progress = 0;
    final AtomicBoolean replacement = new AtomicBoolean();
    ignite().events().remoteListen(new IgniteBiPredicate<UUID, Event>() {

        @Override
        public boolean apply(UUID uuid, Event evt) {
            if (evt.type() == EVT_PAGE_REPLACEMENT_STARTED) {
                replacement.set(true);
                return false;
            }
            return true;
        }
    }, null, EVT_PAGE_REPLACEMENT_STARTED);
    int portion = 100;
    Map<Integer, TestValue> putMap = new HashMap<>(portion, 1.f);
    while (progress < 2 * replCntr) {
        putMap.clear();
        for (int i = 0; i < portion; i++) putMap.put(progress + i, new TestValue(progress + i));
        progress += portion;
        cache().putAll(putMap);
        if (progress % 1000 == 0)
            BenchmarkUtils.println("progress=" + progress);
        if (replacement.compareAndSet(true, false)) {
            if (replCntr != Integer.MAX_VALUE / 2)
                throw new Exception("Invalid expected val: " + replCntr);
            replCntr = progress;
            BenchmarkUtils.println("replCntr=" + replCntr);
        }
    }
    BenchmarkUtils.println("Benchmark cache fullfill complete. progress=" + progress + " replCntr=" + replCntr + ".");
    long backgroundScanInterval = args.getLongParameter("BACKGROUND_SCAN_INTERVAL", 0L);
    if (backgroundScanInterval != 0) {
        IgniteCache<Integer, Object> scanCache = ignite().getOrCreateCache(SCAN_CACHE_NAME);
        try (IgniteDataStreamer<Integer, Object> streamer = ignite().dataStreamer(SCAN_CACHE_NAME)) {
            for (int i = 0; i < replCntr; i++) streamer.addData(i, new TestValue(i));
        }
        BenchmarkUtils.println("Scan cache fullfill complete. Size=" + replCntr + ".");
        backgroundScanThread = new Thread(() -> {
            long iteration = 0;
            while (ignite().cluster().state().active()) {
                iteration++;
                long size = 0;
                try (QueryCursor cursor = scanCache.query(new ScanQuery())) {
                    for (Object o : cursor) size++;
                }
                BenchmarkUtils.println("Background scan iteration " + iteration + " finished, size=" + size);
                try {
                    Thread.sleep(backgroundScanInterval);
                } catch (InterruptedException e) {
                    return;
                }
            }
        });
        backgroundScanThread.start();
    }
    int cacheSize = 0;
    try (QueryCursor cursor = cache.query(new ScanQuery())) {
        for (Object o : cursor) cacheSize++;
    }
    BenchmarkUtils.println("cache size=" + cacheSize);
    range = (int) (args.getDoubleParameter("REPLACE_RATIO", 1.0) * replCntr);
}
Also used : HashMap(java.util.HashMap) ScanQuery(org.apache.ignite.cache.query.ScanQuery) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Event(org.apache.ignite.events.Event) UUID(java.util.UUID) QueryCursor(org.apache.ignite.cache.query.QueryCursor)

Example 63 with QueryCursor

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

the class IgniteCacheAbstractQuerySelfTest method testSimpleCustomTableName.

/**
 * JUnit.
 */
@Test
public void testSimpleCustomTableName() {
    CacheConfiguration<Integer, Object> cacheConf = new CacheConfiguration<Integer, Object>(cacheConfiguration()).setName(DEFAULT_CACHE_NAME).setQueryEntities(Arrays.asList(new QueryEntity(Integer.class, Type1.class), new QueryEntity(Integer.class, Type2.class)));
    final IgniteCache<Integer, Object> cache = ignite().getOrCreateCache(cacheConf);
    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) QueryEntity(org.apache.ignite.cache.QueryEntity) 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) NearCacheConfiguration(org.apache.ignite.configuration.NearCacheConfiguration) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) QueryCursor(org.apache.ignite.cache.query.QueryCursor) Cache(javax.cache.Cache) IgniteCache(org.apache.ignite.IgniteCache) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 64 with QueryCursor

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

the class ComputeUtils method getData.

/**
 * Extracts partition {@code data} from the local storage, if it's not found in local storage recovers this {@code
 * data} from a partition {@code upstream} and {@code context}. Be aware that this method should be called from
 * the node where partition is placed.
 *
 * @param ignite Ignite instance.
 * @param upstreamCacheName Name of an {@code upstream} cache.
 * @param filter Filter for {@code upstream} data.
 * @param transformerBuilder Builder of upstream transformers.
 * @param datasetCacheName Name of a partition {@code context} cache.
 * @param datasetId Dataset ID.
 * @param partDataBuilder Partition data builder.
 * @param env Learning environment.
 * @param <K> Type of a key in {@code upstream} data.
 * @param <V> Type of a value in {@code upstream} data.
 * @param <C> Type of a partition {@code context}.
 * @param <D> Type of a partition {@code data}.
 * @return Partition {@code data}.
 */
public static <K, V, C extends Serializable, D extends AutoCloseable> D getData(Ignite ignite, String upstreamCacheName, IgniteBiPredicate<K, V> filter, UpstreamTransformerBuilder transformerBuilder, String datasetCacheName, UUID datasetId, PartitionDataBuilder<K, V, C, D> partDataBuilder, LearningEnvironment env, boolean isKeepBinary) {
    PartitionDataStorage dataStorage = (PartitionDataStorage) ignite.cluster().nodeLocalMap().computeIfAbsent(String.format(DATA_STORAGE_KEY_TEMPLATE, datasetId), key -> new PartitionDataStorage());
    final int part = env.partition();
    return dataStorage.computeDataIfAbsent(part, () -> {
        IgniteCache<Integer, C> learningCtxCache = ignite.cache(datasetCacheName);
        C ctx = learningCtxCache.get(part);
        IgniteCache<K, V> upstreamCache = ignite.cache(upstreamCacheName);
        if (isKeepBinary)
            upstreamCache = upstreamCache.withKeepBinary();
        ScanQuery<K, V> qry = new ScanQuery<>();
        qry.setLocal(true);
        qry.setPartition(part);
        qry.setFilter(filter);
        UpstreamTransformer transformer = transformerBuilder.build(env);
        UpstreamTransformer transformerCp = Utils.copy(transformer);
        long cnt = computeCount(upstreamCache, qry, transformer);
        if (cnt > 0) {
            try (QueryCursor<UpstreamEntry<K, V>> cursor = upstreamCache.query(qry, e -> new UpstreamEntry<>(e.getKey(), e.getValue()))) {
                Iterator<UpstreamEntry<K, V>> it = cursor.iterator();
                Stream<UpstreamEntry> transformedStream = transformerCp.transform(Utils.asStream(it, cnt).map(x -> (UpstreamEntry) x));
                it = Utils.asStream(transformedStream.iterator()).map(x -> (UpstreamEntry<K, V>) x).iterator();
                Iterator<UpstreamEntry<K, V>> iter = new IteratorWithConcurrentModificationChecker<>(it, cnt, "Cache expected to be not modified during dataset data building [partition=" + part + ']');
                return partDataBuilder.build(env, iter, cnt, ctx);
            }
        }
        return null;
    });
}
Also used : UpstreamTransformer(org.apache.ignite.ml.dataset.UpstreamTransformer) Arrays(java.util.Arrays) DeployingContext(org.apache.ignite.ml.environment.deploy.DeployingContext) IgniteBiPredicate(org.apache.ignite.lang.IgniteBiPredicate) IgniteFunction(org.apache.ignite.ml.math.functions.IgniteFunction) Affinity(org.apache.ignite.cache.affinity.Affinity) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ConcurrentMap(java.util.concurrent.ConcurrentMap) IgniteCallable(org.apache.ignite.lang.IgniteCallable) PartitionContextBuilder(org.apache.ignite.ml.dataset.PartitionContextBuilder) LearningEnvironment(org.apache.ignite.ml.environment.LearningEnvironment) Map(java.util.Map) PartitionDataBuilder(org.apache.ignite.ml.dataset.PartitionDataBuilder) UpstreamTransformerBuilder(org.apache.ignite.ml.dataset.UpstreamTransformerBuilder) LearningEnvironmentBuilder(org.apache.ignite.ml.environment.LearningEnvironmentBuilder) UpstreamEntry(org.apache.ignite.ml.dataset.UpstreamEntry) ClusterGroup(org.apache.ignite.cluster.ClusterGroup) IgniteFuture(org.apache.ignite.lang.IgniteFuture) Iterator(java.util.Iterator) Collection(java.util.Collection) IgniteException(org.apache.ignite.IgniteException) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) UUID(java.util.UUID) Ignite(org.apache.ignite.Ignite) IgniteCache(org.apache.ignite.IgniteCache) Serializable(java.io.Serializable) LockSupport(java.util.concurrent.locks.LockSupport) Stream(java.util.stream.Stream) Ignition(org.apache.ignite.Ignition) Utils(org.apache.ignite.ml.util.Utils) QueryCursor(org.apache.ignite.cache.query.QueryCursor) BitSet(java.util.BitSet) ScanQuery(org.apache.ignite.cache.query.ScanQuery) GridPeerDeployAware(org.apache.ignite.internal.util.lang.GridPeerDeployAware) ScanQuery(org.apache.ignite.cache.query.ScanQuery) UpstreamTransformer(org.apache.ignite.ml.dataset.UpstreamTransformer) UpstreamEntry(org.apache.ignite.ml.dataset.UpstreamEntry)

Example 65 with QueryCursor

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

the class TcpDiscoveryMultiThreadedTest method testCustomEventOnJoinCoordinatorStop.

/**
 * @throws Exception If failed.
 */
@Ignore("https://issues.apache.org/jira/browse/IGNITE-10198")
@Test
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 {
                String cacheName = DEFAULT_CACHE_NAME + "-tmp";
                Ignite ignite = ignite(START_NODES - 1);
                while (!stop.get()) {
                    CacheConfiguration ccfg = new CacheConfiguration(cacheName);
                    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) Ignore(org.junit.Ignore) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) 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