use of org.apache.ignite.cache.query.QueryCursor in project ignite by apache.
the class IgniteCacheClusterReadOnlyModeSelfTest method testScanQueryAllowed.
/**
*/
@Test
public void testScanQueryAllowed() {
performAction(cache -> {
try (QueryCursor qry = cache.query(new ScanQuery<>())) {
for (Object o : qry.getAll()) {
IgniteBiTuple<Integer, Integer> tuple = (IgniteBiTuple<Integer, Integer>) o;
assertEquals(o.toString(), kvMap.get(tuple.getKey()), tuple.getValue());
}
}
});
}
use of org.apache.ignite.cache.query.QueryCursor in project ignite by apache.
the class CacheContinuousQueryBufferLimitTest method testPendingSendToClientOnLimitReached.
/**
* @throws Exception If fails.
*/
@Test
public void testPendingSendToClientOnLimitReached() throws Exception {
AtomicInteger keys = new AtomicInteger();
AtomicReference<String> err = new AtomicReference<>();
IgniteEx srv = startGrids(2);
IgniteEx clnt = startClientGrid();
IgniteCache<Integer, Integer> cache = clnt.cache(DEFAULT_CACHE_NAME);
CacheEntryEventSerializableFilter<Integer, Integer> filter = evt -> evt.getKey() % 2 == 0;
ContinuousQuery<Integer, Integer> cq = new ContinuousQuery<>();
cq.setRemoteFilterFactory(FactoryBuilder.factoryOf(filter));
cq.setLocalListener((events) -> events.forEach(e -> {
if (!filter.evaluate(e))
err.compareAndSet(null, "Key must be filtered [e=" + e + ']');
}));
cq.setLocal(false);
spi(srv).blockMessages((nodeId, msg) -> (cachePutOperationRequestMessage(msg) && msgCntr.getAndIncrement() == 7) || msg instanceof CacheContinuousQueryBatchAck);
IgniteInternalFuture<?> loadFut = null;
try (QueryCursor<?> qry = cache.query(cq)) {
awaitPartitionMapExchange();
loadFut = GridTestUtils.runMultiThreadedAsync(() -> {
while (!Thread.currentThread().isInterrupted()) cache.put(keys.incrementAndGet(), 0);
}, 6, "cq-put-");
// Entries are checked by CacheEntryUpdatedListener which has been set to CQ. Check that all
// entries greater than pending limit filtered correctly (entries are sent to client on buffer overflow).
boolean await = waitForCondition(() -> keys.get() > OVERFLOW_KEYS_COUNT, 30_000);
assertTrue("Number of keys to put must reach the limit [keys=" + keys.get() + ", limit=" + OVERFLOW_KEYS_COUNT + ']', await);
} finally {
TestRecordingCommunicationSpi.stopBlockAll();
if (loadFut != null)
loadFut.cancel();
}
if (err.get() != null)
throw new Exception(err.get());
}
use of org.apache.ignite.cache.query.QueryCursor in project ignite by apache.
the class CacheContinuousQueryBufferLimitTest method doTestContinuousQueryPendingBufferLimit.
/**
* @param locBlockPred Block predicate on local node to emulate message delivery issues.
* @param pendingLimit Test limit of pending entries.
* @throws Exception If fails.
*/
private void doTestContinuousQueryPendingBufferLimit(IgniteBiPredicate<ClusterNode, Message> locBlockPred, int pendingLimit) throws Exception {
AtomicInteger keys = new AtomicInteger();
IgniteEx locIgnite = startGrid(0);
IgniteEx rmtIgnite = startGrid(1);
IgniteCache<Integer, Integer> cache = locIgnite.cache(DEFAULT_CACHE_NAME);
CacheConfiguration<Integer, Integer> ccfg = cache.getConfiguration(CacheConfiguration.class);
for (int i = 0; i < TOTAL_KEYS; i++) cache.put(i, i);
assertEquals(PARTS, ccfg.getAffinity().partitions());
GridAtomicLong lastAcked = new GridAtomicLong();
ContinuousQuery<Integer, Integer> cq = new ContinuousQuery<>();
cq.setRemoteFilterFactory(FactoryBuilder.factoryOf(RMT_FILTER));
cq.setLocalListener((events) -> events.forEach(e -> lastAcked.setIfGreater(((CacheQueryEntryEvent<?, ?>) e).getPartitionUpdateCounter())));
cq.setLocal(false);
IgniteInternalFuture<?> updFut = null;
try (QueryCursor<?> qry = locIgnite.cache(DEFAULT_CACHE_NAME).query(cq)) {
awaitPartitionMapExchange();
// Partition Id, Update Counter, Continuous Entry.
ConcurrentMap<Long, CacheContinuousQueryEntry> pending = getContinuousQueryPendingBuffer(rmtIgnite, CU.cacheId(DEFAULT_CACHE_NAME), 0);
spi(locIgnite).blockMessages(locBlockPred);
updFut = GridTestUtils.runMultiThreadedAsync(() -> {
while (keys.get() <= OVERFLOW_KEYS_COUNT) cache.put(keys.incrementAndGet(), 0);
}, 3, "cq-put-");
assertNotNull("Partition remote buffers must be inited", pending);
log.warning("Waiting for pending buffer being overflowed within " + OVERFLOW_KEYS_COUNT + " number of keys.");
boolean await = waitForCondition(() -> pending.size() > pendingLimit, () -> keys.get() <= OVERFLOW_KEYS_COUNT);
assertFalse("Pending buffer exceeded the limit despite entries have been acked " + "[lastAcked=" + lastAcked + ", pending=" + S.compact(pending.keySet(), i -> i + 1) + ']', await);
} finally {
spi(locIgnite).stopBlock();
if (updFut != null)
updFut.cancel();
}
}
use of org.apache.ignite.cache.query.QueryCursor in project ignite by apache.
the class CacheContinuousQueryOrderingEventTest method doOrderingTest.
/**
* @param ccfg Cache configuration.
* @param async Async filter.
* @throws Exception If failed.
*/
protected void doOrderingTest(final CacheConfiguration ccfg, final boolean async) throws Exception {
ignite(0).createCache(ccfg);
List<QueryCursor<?>> qries = new ArrayList<>();
try {
List<BlockingQueue<CacheEntryEvent<QueryTestKey, QueryTestValue>>> rcvdEvts = new ArrayList<>(LISTENER_CNT * NODES);
final AtomicInteger qryCntr = new AtomicInteger(0);
final int threadCnt = 20;
for (int idx = 0; idx < NODES; idx++) {
for (int i = 0; i < LISTENER_CNT; i++) {
BlockingQueue<CacheEntryEvent<QueryTestKey, QueryTestValue>> queue = new ArrayBlockingQueue<>(ITERATION_CNT * threadCnt);
ContinuousQuery qry = new ContinuousQuery();
if (async) {
qry.setLocalListener(new TestCacheAsyncEventListener(queue, qryCntr));
qry.setRemoteFilterFactory(FactoryBuilder.factoryOf(new CacheTestRemoteFilterAsync(ccfg.getName())));
} else {
qry.setLocalListener(new TestCacheEventListener(queue, qryCntr));
qry.setRemoteFilterFactory(FactoryBuilder.factoryOf(new CacheTestRemoteFilter(ccfg.getName())));
}
rcvdEvts.add(queue);
IgniteCache<Object, Object> cache = grid(idx).cache(ccfg.getName());
QueryCursor qryCursor = cache.query(qry);
qries.add(qryCursor);
}
}
IgniteInternalFuture<Long> f = GridTestUtils.runMultiThreadedAsync(new Runnable() {
@Override
public void run() {
ThreadLocalRandom rnd = ThreadLocalRandom.current();
for (int i = 0; i < ITERATION_CNT; i++) {
IgniteCache<QueryTestKey, QueryTestValue> cache = grid(rnd.nextInt(NODES)).cache(ccfg.getName());
QueryTestKey key = new QueryTestKey(rnd.nextInt(KEYS));
boolean startTx = atomicityMode(cache) != ATOMIC && rnd.nextBoolean();
Transaction tx = null;
boolean committed = false;
while (!committed && !Thread.currentThread().isInterrupted()) {
try {
if (startTx)
tx = cache.unwrap(Ignite.class).transactions().txStart(PESSIMISTIC, REPEATABLE_READ);
if ((cache.get(key) == null) || rnd.nextBoolean()) {
cache.invoke(key, new CacheEntryProcessor<QueryTestKey, QueryTestValue, Object>() {
@Override
public Object process(MutableEntry<QueryTestKey, QueryTestValue> entry, Object... arguments) throws EntryProcessorException {
if (entry.exists())
entry.setValue(new QueryTestValue(entry.getValue().val1 + 1));
else
entry.setValue(new QueryTestValue(0));
return null;
}
});
} else {
QueryTestValue val;
QueryTestValue newVal;
do {
val = cache.get(key);
newVal = val == null ? new QueryTestValue(0) : new QueryTestValue(val.val1 + 1);
} while (!cache.replace(key, val, newVal));
}
if (tx != null)
tx.commit();
committed = true;
} catch (CacheException e) {
assertTrue(e.getCause() instanceof TransactionSerializationException);
assertEquals(atomicityMode(cache), TRANSACTIONAL_SNAPSHOT);
} finally {
if (tx != null)
tx.close();
}
}
}
}
}, threadCnt, "put-thread");
f.get(15, TimeUnit.SECONDS);
GridTestUtils.waitForCondition(new PA() {
@Override
public boolean apply() {
return qryCntr.get() >= ITERATION_CNT * threadCnt * LISTENER_CNT * NODES;
}
}, 1000L);
for (BlockingQueue<CacheEntryEvent<QueryTestKey, QueryTestValue>> queue : rcvdEvts) checkEvents(queue, ITERATION_CNT * threadCnt);
assertFalse("Ordering invocations of filter broken.", fail);
} finally {
for (QueryCursor<?> qry : qries) qry.close();
ignite(0).destroyCache(ccfg.getName());
}
}
use of org.apache.ignite.cache.query.QueryCursor in project ignite by apache.
the class CacheContinuousQueryConcurrentPartitionUpdateTest method startListener.
/**
* @param cache Cache.
* @return Event counter.
*/
private T2<AtomicInteger, QueryCursor> startListener(IgniteCache<Object, Object> cache) {
final AtomicInteger evtCnt = new AtomicInteger();
ContinuousQuery<Object, Object> qry = new ContinuousQuery<>();
qry.setLocalListener(new CacheEntryUpdatedListener<Object, Object>() {
@Override
public void onUpdated(Iterable<CacheEntryEvent<?, ?>> evts) {
for (CacheEntryEvent evt : evts) {
assertNotNull(evt.getKey());
assertNotNull(evt.getValue());
if ((Integer) evt.getValue() >= 0)
evtCnt.incrementAndGet();
}
}
});
QueryCursor cur = cache.query(qry);
return new T2<>(evtCnt, cur);
}
Aggregations