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());
}
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);
}
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);
}
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;
});
}
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();
}
}
Aggregations