use of org.apache.ignite.cache.query.ContinuousQuery in project ignite by apache.
the class CacheContinuousQueryConcurrentPartitionUpdateTest method concurrentUpdatesAndQueryStart.
/**
* @param atomicityMode Cache atomicity mode.
* @throws Exception If failed.
*/
private void concurrentUpdatesAndQueryStart(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);
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;
for (int i = 0; i < 5; i++) {
log.info("Iteration: " + i);
ContinuousQuery<Object, Object> qry = new ContinuousQuery<>();
final AtomicInteger evtCnt = new AtomicInteger();
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;
final IgniteCache<Object, Object> srvCache = srv.cache(DEFAULT_CACHE_NAME);
final AtomicBoolean stop = new AtomicBoolean();
try {
IgniteInternalFuture fut = GridTestUtils.runMultiThreadedAsync(new Callable<Void>() {
@Override
public Void call() throws Exception {
ThreadLocalRandom rnd = ThreadLocalRandom.current();
while (!stop.get()) srvCache.put(keys.get(rnd.nextInt(KEYS)), rnd.nextInt(100) - 200);
return null;
}
}, THREADS, "update");
U.sleep(1000);
cur = clientCache.query(qry);
U.sleep(1000);
stop.set(true);
fut.get();
} finally {
stop.set(true);
}
GridTestUtils.runMultiThreadedAsync(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());
cur.close();
}
}
use of org.apache.ignite.cache.query.ContinuousQuery in project ignite by apache.
the class CacheContinuousQueryOperationP2PTest method testContinuousQuery.
/**
* @param ccfg Cache configuration.
* @param isClient Client.
* @throws Exception If failed.
*/
protected void testContinuousQuery(CacheConfiguration<Object, Object> ccfg, boolean isClient) throws Exception {
ignite(0).createCache(ccfg);
ThreadLocalRandom rnd = ThreadLocalRandom.current();
QueryCursor<?> cur = null;
final Class<Factory<CacheEntryEventFilter>> evtFilterFactory = (Class<Factory<CacheEntryEventFilter>>) getExternalClassLoader().loadClass("org.apache.ignite.tests.p2p.CacheDeploymentEntryEventFilterFactory");
final CountDownLatch latch = new CountDownLatch(10);
ContinuousQuery<Integer, Integer> qry = new ContinuousQuery<>();
TestLocalListener localLsnr = new TestLocalListener() {
@Override
public void onEvent(Iterable<CacheEntryEvent<? extends Integer, ? extends Integer>> evts) throws CacheEntryListenerException {
for (CacheEntryEvent<? extends Integer, ? extends Integer> evt : evts) {
latch.countDown();
log.info("Received event: " + evt);
}
}
};
MutableCacheEntryListenerConfiguration<Integer, Integer> lsnrCfg = new MutableCacheEntryListenerConfiguration<>(new FactoryBuilder.SingletonFactory<>(localLsnr), (Factory<? extends CacheEntryEventFilter<? super Integer, ? super Integer>>) (Object) evtFilterFactory.newInstance(), true, true);
qry.setLocalListener(localLsnr);
qry.setRemoteFilterFactory((Factory<? extends CacheEntryEventFilter<Integer, Integer>>) (Object) evtFilterFactory.newInstance());
IgniteCache<Integer, Integer> cache = null;
try {
if (isClient)
cache = grid(NODES - 1).cache(ccfg.getName());
else
cache = grid(rnd.nextInt(NODES - 1)).cache(ccfg.getName());
cur = cache.query(qry);
cache.registerCacheEntryListener(lsnrCfg);
for (int i = 0; i < 10; i++) cache.put(i, i);
assertTrue(latch.await(3, TimeUnit.SECONDS));
} finally {
if (cur != null)
cur.close();
if (cache != null)
cache.deregisterCacheEntryListener(lsnrCfg);
}
}
use of org.apache.ignite.cache.query.ContinuousQuery in project ignite by apache.
the class BinaryMetadataUpdatesFlowTest method startListening.
/**
* @param idx Index.
* @param deafClient Deaf client.
* @param observedIds Observed ids.
*/
private void startListening(int idx, boolean deafClient, Set<Integer> observedIds) throws Exception {
clientMode = true;
ContinuousQuery qry = new ContinuousQuery();
qry.setLocalListener(new CQListener(observedIds));
if (deafClient) {
applyDiscoveryHook = true;
discoveryHook = new DiscoveryHook() {
@Override
public void handleDiscoveryMessage(DiscoverySpiCustomMessage msg) {
DiscoveryCustomMessage customMsg = msg == null ? null : (DiscoveryCustomMessage) IgniteUtils.field(msg, "delegate");
if (customMsg instanceof MetadataUpdateProposedMessage) {
if (((MetadataUpdateProposedMessage) customMsg).typeId() == BINARY_TYPE_ID)
GridTestUtils.setFieldValue(customMsg, "typeId", 1);
} else if (customMsg instanceof MetadataUpdateAcceptedMessage) {
if (((MetadataUpdateAcceptedMessage) customMsg).typeId() == BINARY_TYPE_ID)
GridTestUtils.setFieldValue(customMsg, "typeId", 1);
}
}
};
IgniteEx client = startGrid(idx);
client.cache(DEFAULT_CACHE_NAME).withKeepBinary().query(qry);
} else {
applyDiscoveryHook = false;
IgniteEx client = startGrid(idx);
client.cache(DEFAULT_CACHE_NAME).withKeepBinary().query(qry);
}
}
use of org.apache.ignite.cache.query.ContinuousQuery in project ignite by apache.
the class TcpDiscoveryMultiThreadedTest method _testClientContinuousQueryCoordinatorStop.
/**
* @throws Exception If failed.
*/
public void _testClientContinuousQueryCoordinatorStop() 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);
ignite(0).createCache(new CacheConfiguration<>(DEFAULT_CACHE_NAME));
final AtomicInteger startIdx = new AtomicInteger(START_NODES);
final CyclicBarrier barrier = new CyclicBarrier(JOIN_NODES + 1);
clientFlagGlobal = true;
IgniteInternalFuture<?> fut = 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);
assertTrue(ignite.configuration().isClientMode());
log.info("Started node: " + ignite.name());
IgniteCache<Object, Object> cache = ignite.getOrCreateCache(DEFAULT_CACHE_NAME);
for (int i = 0; i < 10; i++) {
ContinuousQuery<Object, Object> qry = new ContinuousQuery<>();
qry.setLocalListener(new CacheEntryUpdatedListener<Object, Object>() {
@Override
public void onUpdated(Iterable<CacheEntryEvent<?, ?>> evts) {
// No-op.
}
});
cache.query(qry);
}
return null;
}
}, JOIN_NODES, "start-thread");
barrier.await();
U.sleep(ThreadLocalRandom.current().nextInt(100, 500));
for (int i = 0; i < START_NODES - 1; i++) {
GridTestUtils.invoke(ignite(i).configuration().getDiscoverySpi(), "simulateNodeFailure");
stopGrid(i);
}
fut.get();
stopAllGrids();
}
}
use of org.apache.ignite.cache.query.ContinuousQuery in project ignite by apache.
the class SqlViewExporterSpiTest method testContinuousQuery.
/**
*/
@Test
public void testContinuousQuery() throws Exception {
IgniteCache<Integer, Integer> cache = ignite0.createCache("cache-1");
assertTrue(execute(ignite0, "SELECT * FROM SYS.CONTINUOUS_QUERIES").isEmpty());
assertTrue(execute(ignite1, "SELECT * FROM SYS.CONTINUOUS_QUERIES").isEmpty());
try (QueryCursor qry = cache.query(new ContinuousQuery<>().setInitialQuery(new ScanQuery<>()).setPageSize(100).setTimeInterval(1000).setLocalListener(evts -> {
// No-op.
}).setRemoteFilterFactory(() -> evt -> true))) {
for (int i = 0; i < 100; i++) cache.put(i, i);
checkContinuouQueryView(ignite0, true);
checkContinuouQueryView(ignite1, false);
}
assertTrue(execute(ignite0, "SELECT * FROM SYS.CONTINUOUS_QUERIES").isEmpty());
assertTrue(waitForCondition(() -> execute(ignite1, "SELECT * FROM SYS.CONTINUOUS_QUERIES").isEmpty(), getTestTimeout()));
}
Aggregations