use of org.apache.ignite.cache.query.ContinuousQuery in project ignite by apache.
the class CacheContinuousQueryConcurrentPartitionUpdateTest method concurrentUpdatePartition.
/**
* @param atomicityMode Cache atomicity mode.
* @throws Exception If failed.
*/
private void concurrentUpdatePartition(CacheAtomicityMode atomicityMode) throws Exception {
Ignite srv = startGrid(0);
client = true;
Ignite client = startGrid(1);
CacheConfiguration ccfg = new CacheConfiguration(DEFAULT_CACHE_NAME);
ccfg.setWriteSynchronizationMode(FULL_SYNC);
ccfg.setAtomicityMode(atomicityMode);
IgniteCache clientCache = client.createCache(ccfg);
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());
evtCnt.incrementAndGet();
}
}
});
clientCache.query(qry);
Affinity<Integer> aff = srv.affinity(DEFAULT_CACHE_NAME);
final List<Integer> keys = new ArrayList<>();
final int KEYS = 10;
for (int i = 0; i < 100_000; i++) {
if (aff.partition(i) == 0) {
keys.add(i);
if (keys.size() == KEYS)
break;
}
}
assertEquals(KEYS, keys.size());
final int THREADS = 10;
final int UPDATES = 1000;
final IgniteCache<Object, Object> srvCache = srv.cache(DEFAULT_CACHE_NAME);
for (int i = 0; i < 15; i++) {
log.info("Iteration: " + i);
GridTestUtils.runMultiThreaded(new Callable<Void>() {
@Override
public Void call() throws Exception {
ThreadLocalRandom rnd = ThreadLocalRandom.current();
for (int i = 0; i < UPDATES; i++) srvCache.put(keys.get(rnd.nextInt(KEYS)), i);
return null;
}
}, THREADS, "update");
GridTestUtils.waitForCondition(new GridAbsPredicate() {
@Override
public boolean apply() {
log.info("Events: " + evtCnt.get());
return evtCnt.get() >= THREADS * UPDATES;
}
}, 5000);
assertEquals(THREADS * UPDATES, evtCnt.get());
evtCnt.set(0);
}
}
use of org.apache.ignite.cache.query.ContinuousQuery in project ignite by apache.
the class IgniteCacheProxy method queryContinuous.
/**
* Executes continuous query.
*
* @param qry Query.
* @param loc Local flag.
* @param keepBinary Keep binary flag.
* @return Initial iteration cursor.
*/
@SuppressWarnings("unchecked")
private QueryCursor<Cache.Entry<K, V>> queryContinuous(ContinuousQuery qry, boolean loc, boolean keepBinary) {
if (qry.getInitialQuery() instanceof ContinuousQuery)
throw new IgniteException("Initial predicate for continuous query can't be an instance of another " + "continuous query. Use SCAN or SQL query for initial iteration.");
if (qry.getLocalListener() == null)
throw new IgniteException("Mandatory local listener is not set for the query: " + qry);
if (qry.getRemoteFilter() != null && qry.getRemoteFilterFactory() != null)
throw new IgniteException("Should be used either RemoterFilter or RemoteFilterFactory.");
try {
final UUID routineId = ctx.continuousQueries().executeQuery(qry.getLocalListener(), qry.getRemoteFilter(), qry.getRemoteFilterFactory(), qry.getPageSize(), qry.getTimeInterval(), qry.isAutoUnsubscribe(), loc, keepBinary, qry.isIncludeExpired());
final QueryCursor<Cache.Entry<K, V>> cur = qry.getInitialQuery() != null ? query(qry.getInitialQuery()) : null;
return new QueryCursor<Cache.Entry<K, V>>() {
@Override
public Iterator<Cache.Entry<K, V>> iterator() {
return cur != null ? cur.iterator() : new GridEmptyIterator<Cache.Entry<K, V>>();
}
@Override
public List<Cache.Entry<K, V>> getAll() {
return cur != null ? cur.getAll() : Collections.<Cache.Entry<K, V>>emptyList();
}
@Override
public void close() {
if (cur != null)
cur.close();
try {
ctx.kernalContext().continuous().stopRoutine(routineId).get();
} catch (IgniteCheckedException e) {
throw U.convertException(e);
}
}
};
} catch (IgniteCheckedException e) {
throw U.convertException(e);
}
}
use of org.apache.ignite.cache.query.ContinuousQuery in project ignite by apache.
the class IgniteCacheProxy method query.
/** {@inheritDoc} */
@SuppressWarnings("unchecked")
@Override
public <R> QueryCursor<R> query(Query<R> qry) {
A.notNull(qry, "qry");
GridCacheGateway<K, V> gate = this.gate;
CacheOperationContext prev = onEnter(gate, opCtx);
try {
ctx.checkSecurity(SecurityPermission.CACHE_READ);
validate(qry);
convertToBinary(qry);
CacheOperationContext opCtxCall = ctx.operationContextPerCall();
boolean keepBinary = opCtxCall != null && opCtxCall.isKeepBinary();
if (qry instanceof ContinuousQuery)
return (QueryCursor<R>) queryContinuous((ContinuousQuery<K, V>) qry, qry.isLocal(), keepBinary);
if (qry instanceof SqlQuery)
return (QueryCursor<R>) ctx.kernalContext().query().querySql(ctx, (SqlQuery) qry, keepBinary);
if (qry instanceof SqlFieldsQuery)
return (FieldsQueryCursor<R>) ctx.kernalContext().query().querySqlFields(ctx, (SqlFieldsQuery) qry, keepBinary);
if (qry instanceof ScanQuery)
return query((ScanQuery) qry, null, projection(qry.isLocal()));
return (QueryCursor<R>) query(qry, projection(qry.isLocal()));
} catch (Exception e) {
if (e instanceof CacheException)
throw (CacheException) e;
throw new CacheException(e);
} finally {
onLeave(gate, prev);
}
}
use of org.apache.ignite.cache.query.ContinuousQuery in project ignite by apache.
the class TcpDiscoveryMultiThreadedTest method _testCustomEventNodeRestart.
/**
* @throws Exception If failed.
*/
public void _testCustomEventNodeRestart() throws Exception {
clientFlagGlobal = false;
Ignite ignite = startGrid(0);
ignite.getOrCreateCache(new CacheConfiguration<>(DEFAULT_CACHE_NAME));
final long stopTime = System.currentTimeMillis() + 60_000;
GridTestUtils.runMultiThreaded(new IgniteInClosure<Integer>() {
@Override
public void apply(Integer idx) {
try {
while (System.currentTimeMillis() < stopTime) {
Ignite ignite = startGrid(idx + 1);
IgniteCache<Object, Object> cache = ignite.cache(DEFAULT_CACHE_NAME);
int qryCnt = ThreadLocalRandom.current().nextInt(10) + 1;
for (int i = 0; i < qryCnt; i++) {
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();
}
GridTestUtils.invoke(ignite.configuration().getDiscoverySpi(), "simulateNodeFailure");
ignite.close();
}
} catch (Exception e) {
log.error("Unexpected error: " + e, e);
throw new IgniteException(e);
}
}
}, 5, "node-restart");
}
use of org.apache.ignite.cache.query.ContinuousQuery in project ignite by apache.
the class TcpDiscoveryMultiThreadedTest method _testCustomEventOnJoinCoordinatorStop.
/**
* @throws Exception If failed.
*/
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 {
CacheConfiguration ccfg = new CacheConfiguration(DEFAULT_CACHE_NAME);
Ignite ignite = ignite(START_NODES - 1);
while (!stop.get()) {
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