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