use of org.apache.ignite.cache.query.ContinuousQuery in project ignite by apache.
the class IgniteCacheConfigVariationsFullApiTest method testContinuousQuery.
/**
* @throws Exception If failed.
*/
public void testContinuousQuery() throws Exception {
runInAllDataModes(new TestRunnable() {
@Override
public void run() throws Exception {
final AtomicInteger updCnt = new AtomicInteger();
ContinuousQuery<Object, Object> qry = new ContinuousQuery<>();
qry.setInitialQuery(new ScanQuery<>(new IgniteBiPredicate<Object, Object>() {
@Override
public boolean apply(Object key, Object val) {
return valueOf(key) >= 3;
}
}));
qry.setLocalListener(new CacheEntryUpdatedListener<Object, Object>() {
@Override
public void onUpdated(Iterable<CacheEntryEvent<? extends Object, ? extends Object>> evts) throws CacheEntryListenerException {
for (CacheEntryEvent<? extends Object, ? extends Object> evt : evts) {
int v = valueOf(evt.getKey());
// Check filter.
assertTrue("v=" + v, v >= 10 && v < 15);
updCnt.incrementAndGet();
}
}
});
qry.setRemoteFilter(new TestCacheEntryEventSerializableFilter());
IgniteCache<Object, Object> cache = jcache();
for (int i = 0; i < 10; i++) cache.put(key(i), value(i));
try (QueryCursor<Cache.Entry<Object, Object>> cur = cache.query(qry)) {
int cnt = 0;
for (Cache.Entry<Object, Object> e : cur) {
cnt++;
int val = valueOf(e.getKey());
assertTrue("v=" + val, val >= 3);
}
assertEquals(7, cnt);
for (int i = 10; i < 20; i++) cache.put(key(i), value(i));
GridTestUtils.waitForCondition(new GridAbsPredicateX() {
@Override
public boolean applyx() throws IgniteCheckedException {
return updCnt.get() == 5;
}
}, 30_000);
}
}
});
}
use of org.apache.ignite.cache.query.ContinuousQuery in project ignite by apache.
the class CacheContinuousBatchAckTest method checkBackupAcknowledgeMessage.
/**
* @param ccfg Cache configuration.
* @throws Exception If failed.
*/
private void checkBackupAcknowledgeMessage(CacheConfiguration<Object, Object> ccfg) throws Exception {
QueryCursor qry = null;
IgniteCache<Object, Object> cache = null;
try {
ContinuousQuery q = new ContinuousQuery();
q.setLocalListener(new CacheEntryUpdatedListener() {
@Override
public void onUpdated(Iterable iterable) throws CacheEntryListenerException {
// No-op.
}
});
cache = grid(SERVER).getOrCreateCache(ccfg);
qry = cache.query(q);
for (int i = 0; i < 10000; i++) cache.put(i, i);
assert !GridTestUtils.waitForCondition(new PA() {
@Override
public boolean apply() {
return fail.get();
}
}, 1300L);
} finally {
if (qry != null)
qry.close();
if (cache != null)
grid(SERVER).destroyCache(cache.getName());
}
}
use of org.apache.ignite.cache.query.ContinuousQuery in project ignite by apache.
the class IgniteClientReconnectContinuousProcessorTest method testCacheContinuousQueryReconnectNewServer.
/**
* @throws Exception If failed.
*/
public void testCacheContinuousQueryReconnectNewServer() throws Exception {
Ignite client = grid(serverCount());
assertTrue(client.cluster().localNode().isClient());
IgniteCache<Object, Object> clientCache = client.getOrCreateCache(new CacheConfiguration<>(DEFAULT_CACHE_NAME));
CacheEventListener lsnr = new CacheEventListener();
ContinuousQuery<Object, Object> qry = new ContinuousQuery<>();
qry.setAutoUnsubscribe(true);
qry.setLocalListener(lsnr);
QueryCursor<?> cur = clientCache.query(qry);
continuousQueryReconnect(client, clientCache, lsnr);
// Check new server registers listener for reconnected client.
try (Ignite newSrv = startGrid(serverCount() + 1)) {
awaitPartitionMapExchange();
lsnr.latch = new CountDownLatch(10);
IgniteCache<Object, Object> newSrvCache = newSrv.cache(DEFAULT_CACHE_NAME);
for (Integer key : primaryKeys(newSrvCache, 10)) newSrvCache.put(key, key);
assertTrue(lsnr.latch.await(5000, MILLISECONDS));
}
cur.close();
// Check new server does not register listener for closed query.
try (Ignite newSrv = startGrid(serverCount() + 1)) {
awaitPartitionMapExchange();
lsnr.latch = new CountDownLatch(5);
IgniteCache<Object, Object> newSrvCache = newSrv.cache(DEFAULT_CACHE_NAME);
for (Integer key : primaryKeys(newSrvCache, 5)) newSrvCache.put(key, key);
assertFalse(lsnr.latch.await(3000, MILLISECONDS));
}
}
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 GridServiceContinuousQueryRedeployTest method testServiceRedeploymentAfterCancel.
/**
* @throws Exception If failed.
*/
public void testServiceRedeploymentAfterCancel() throws Exception {
final Ignite ignite = startGrid(0);
final IgniteCache<Object, Object> managementCache = ignite.getOrCreateCache(CACHE_NAME);
final ContinuousQuery<Object, Object> qry = new ContinuousQuery<>();
final List<Object> evts = Collections.synchronizedList(new ArrayList<>());
qry.setLocalListener(new CacheEntryUpdatedListener<Object, Object>() {
@Override
public void onUpdated(Iterable<CacheEntryEvent<?, ?>> iterable) throws CacheEntryListenerException {
for (CacheEntryEvent<?, ?> event : iterable) evts.add(event);
}
});
int iterations = 100;
while (iterations-- > 0) {
QueryCursor quorumCursor = managementCache.query(qry);
IgniteInternalFuture<?> fut1 = GridTestUtils.runAsync(new Callable<Object>() {
@Override
public Object call() throws Exception {
System.out.println("Deploy " + SERVICE_NAME);
deployService(ignite);
return null;
}
});
IgniteInternalFuture<?> fut2 = GridTestUtils.runAsync(new Callable<Object>() {
@Override
public Object call() throws Exception {
System.out.println("Undeploy " + SERVICE_NAME);
ignite.services().cancel(SERVICE_NAME);
return null;
}
});
fut1.get();
fut2.get();
U.sleep(100);
assert evts.size() <= 1 : evts.size();
ignite.services().cancel("service1");
evts.clear();
quorumCursor.close();
}
}
Aggregations