use of org.apache.ignite.cache.query.ContinuousQuery in project ignite by apache.
the class CacheContinuousQueryExample method main.
/**
* Executes example.
*
* @param args Command line arguments, none required.
* @throws Exception If example execution failed.
*/
public static void main(String[] args) throws Exception {
try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
System.out.println();
System.out.println(">>> Cache continuous query example started.");
// Auto-close cache at the end of the example.
try (IgniteCache<Integer, String> cache = ignite.getOrCreateCache(CACHE_NAME)) {
int keyCnt = 20;
// These entries will be queried by initial predicate.
for (int i = 0; i < keyCnt; i++) cache.put(i, Integer.toString(i));
// Create new continuous query.
ContinuousQuery<Integer, String> qry = new ContinuousQuery<>();
qry.setInitialQuery(new ScanQuery<>(new IgniteBiPredicate<Integer, String>() {
@Override
public boolean apply(Integer key, String val) {
return key > 10;
}
}));
// Callback that is called locally when update notifications are received.
qry.setLocalListener(new CacheEntryUpdatedListener<Integer, String>() {
@Override
public void onUpdated(Iterable<CacheEntryEvent<? extends Integer, ? extends String>> evts) {
for (CacheEntryEvent<? extends Integer, ? extends String> e : evts) System.out.println("Updated entry [key=" + e.getKey() + ", val=" + e.getValue() + ']');
}
});
// This filter will be evaluated remotely on all nodes.
// Entry that pass this filter will be sent to the caller.
qry.setRemoteFilterFactory(new Factory<CacheEntryEventFilter<Integer, String>>() {
@Override
public CacheEntryEventFilter<Integer, String> create() {
return new CacheEntryEventFilter<Integer, String>() {
@Override
public boolean evaluate(CacheEntryEvent<? extends Integer, ? extends String> e) {
return e.getKey() > 10;
}
};
}
});
// Execute query.
try (QueryCursor<Cache.Entry<Integer, String>> cur = cache.query(qry)) {
// Iterate through existing data.
for (Cache.Entry<Integer, String> e : cur) System.out.println("Queried existing entry [key=" + e.getKey() + ", val=" + e.getValue() + ']');
// Add a few more keys and watch more query notifications.
for (int i = keyCnt; i < keyCnt + 10; i++) cache.put(i, Integer.toString(i));
// Wait for a while while callback is notified about remaining puts.
Thread.sleep(2000);
}
} finally {
// Distributed cache could be removed from cluster only by #destroyCache() call.
ignite.destroyCache(CACHE_NAME);
}
}
}
use of org.apache.ignite.cache.query.ContinuousQuery in project ignite by apache.
the class BinaryMetadataRegistrationInsideEntryProcessorTest method testContinuousQueryAndBinaryObjectBuilder.
/**
* Continuously execute multiple EntryProcessors with having continuous queries in parallel.
* This used to lead to several deadlocks.
*
* @throws Exception If failed.
*/
@Test
public void testContinuousQueryAndBinaryObjectBuilder() throws Exception {
startGrids(3).cluster().active(true);
grid(0).createCache(new CacheConfiguration<>().setName(CACHE_NAME).setAtomicityMode(ATOMIC).setBackups(2).setCacheMode(PARTITIONED).setWriteSynchronizationMode(FULL_SYNC).setPartitionLossPolicy(READ_WRITE_SAFE));
IgniteEx client1 = startClientGrid(getConfiguration().setIgniteInstanceName("client1"));
IgniteEx client2 = startClientGrid(getConfiguration().setIgniteInstanceName("client2"));
AtomicBoolean stop = new AtomicBoolean();
AtomicInteger keyCntr = new AtomicInteger();
AtomicInteger binaryTypeCntr = new AtomicInteger();
/**
*/
class MyEntryProcessor implements CacheEntryProcessor<Object, Object, Object> {
/**
* Cached int value retrieved from {@code binaryTypeCntr} variable.
*/
private int i;
/**
*/
public MyEntryProcessor(int i) {
this.i = i;
}
/**
*/
@IgniteInstanceResource
Ignite ignite;
/**
* {@inheritDoc}
*/
@Override
public Object process(MutableEntry<Object, Object> entry, Object... arguments) throws EntryProcessorException {
BinaryObjectBuilder builder = ignite.binary().builder("my_type");
builder.setField("new_field" + i, i);
entry.setValue(builder.build());
return null;
}
}
IgniteInternalFuture fut1 = GridTestUtils.runMultiThreadedAsync(() -> {
IgniteCache<Object, Object> cache = client1.cache(CACHE_NAME).withKeepBinary();
while (!stop.get()) {
Integer key = keyCntr.getAndIncrement();
cache.put(key, key);
cache.invoke(key, new MyEntryProcessor(binaryTypeCntr.get()));
binaryTypeCntr.incrementAndGet();
}
}, 8, "writer-thread");
IgniteInternalFuture fut2 = GridTestUtils.runAsync(() -> {
IgniteCache<Object, Object> cache = client2.cache(CACHE_NAME).withKeepBinary();
while (!stop.get()) {
ContinuousQuery<Object, Object> qry = new ContinuousQuery<>();
qry.setInitialQuery(new ScanQuery<>((key, val) -> true));
qry.setLocalListener(evts -> {
});
// noinspection EmptyTryBlock
try (QueryCursor<Cache.Entry<Object, Object>> cursor = cache.query(qry)) {
// No-op.
}
}
});
doSleep(10_000);
stop.set(true);
fut1.get(10, TimeUnit.SECONDS);
fut2.get(10, TimeUnit.SECONDS);
}
use of org.apache.ignite.cache.query.ContinuousQuery in project ignite by apache.
the class IgniteClientReconnectContinuousProcessorTest method testCacheContinuousQueryReconnectNewServer.
/**
* @throws Exception If failed.
*/
@Test
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 IgniteSlowClientDetectionSelfTest method testSlowClient.
/**
* @throws Exception If failed.
*/
@Test
public void testSlowClient() throws Exception {
final IgniteEx slowClient = grid(nodeCount() - 1);
final ClusterNode slowClientNode = slowClient.localNode();
final CountDownLatch evtSegmentedLatch = new CountDownLatch(1);
slowClient.events().localListen(new IgnitePredicate<Event>() {
@Override
public boolean apply(Event evt) {
assertEquals("Unexpected event: " + evt, evt.type(), EventType.EVT_NODE_SEGMENTED);
DiscoveryEvent evt0 = (DiscoveryEvent) evt;
assertEquals(slowClientNode, evt0.eventNode());
assertEquals(5L, evt0.topologyVersion());
evtSegmentedLatch.countDown();
return false;
}
}, EventType.EVT_NODE_SEGMENTED);
final CountDownLatch evtFailedLatch = new CountDownLatch(nodeCount() - 1);
for (int i = 0; i < nodeCount() - 1; i++) {
grid(i).events().localListen(new IgnitePredicate<Event>() {
@Override
public boolean apply(Event evt) {
assertEquals("Unexpected event: " + evt, evt.type(), EventType.EVT_NODE_FAILED);
DiscoveryEvent evt0 = (DiscoveryEvent) evt;
assertEquals(slowClientNode, evt0.eventNode());
assertEquals(6L, evt0.topologyVersion());
assertEquals(4, evt0.topologyNodes().size());
evtFailedLatch.countDown();
return false;
}
}, EventType.EVT_NODE_FAILED);
}
assertTrue(slowClient.cluster().localNode().isClient());
IgniteCache<Object, Object> cache = slowClient.getOrCreateCache(PARTITIONED);
IgniteEx client0 = grid(nodeCount() - 2);
assertTrue(client0.cluster().localNode().isClient());
IgniteCache<Object, Object> cache0 = client0.getOrCreateCache(PARTITIONED);
cache.query(new ContinuousQuery<>().setLocalListener(new Listener()));
for (int i = 0; i < 100; i++) cache0.put(0, i);
GridIoManager ioMgr = slowClient.context().io();
TcpCommunicationSpi commSpi = (TcpCommunicationSpi) ((Object[]) U.field(ioMgr, "spis"))[0];
GridNioServer nioSrvr = ((GridNioServerWrapper) GridTestUtils.getFieldValue(commSpi, "nioSrvWrapper")).nio();
GridTestUtils.setFieldValue(nioSrvr, "skipRead", true);
// Initiate messages for client.
for (int i = 0; i < 100; i++) cache0.put(0, new byte[10 * 1024]);
boolean wait = GridTestUtils.waitForCondition(new GridAbsPredicate() {
@Override
public boolean apply() {
return Ignition.state(slowClient.name()) == IgniteState.STOPPED_ON_SEGMENTATION;
}
}, getTestTimeout());
assertTrue(wait);
assertTrue("Failed to wait for client failed event", evtFailedLatch.await(5000, MILLISECONDS));
assertTrue("Failed to wait for client segmented event", evtSegmentedLatch.await(5000, MILLISECONDS));
}
use of org.apache.ignite.cache.query.ContinuousQuery in project ignite by apache.
the class CacheEntryListenersTest method testListenersWithRemoteFilter.
/**
* Test continuous queries and JCache entry listeners with remote filters.
*/
@Test
@SuppressWarnings("deprecation")
public void testListenersWithRemoteFilter() throws Exception {
try (IgniteClient client = startClient(0, 1, 2)) {
ClientCache<Integer, Integer> cache = client.getOrCreateCache("testListenersWithRmtFilter");
CacheEntryEventSerializableFilter<Integer, Integer> rmtFilter = evt -> (evt.getKey() & 1) == 0;
ContinuousQueryListener<Integer, Integer> lsnr1 = new ContinuousQueryListener<>();
ContinuousQueryListener<Integer, Integer> lsnr2 = new ContinuousQueryListener<>();
cache.query(new ContinuousQuery<Integer, Integer>().setLocalListener(lsnr1).setRemoteFilterFactory(() -> rmtFilter));
cache.query(new ContinuousQuery<Integer, Integer>().setLocalListener(lsnr2).setRemoteFilter(rmtFilter));
JCacheEntryListener<Integer, Integer> lsnr3 = new JCacheEntryListener<>();
cache.registerCacheEntryListener(new MutableCacheEntryListenerConfiguration<>(() -> lsnr3, () -> rmtFilter, true, false));
for (int i = 0; i < 10; i++) cache.put(i, i);
Map<EventType, Map<Integer, Integer>> expRes = F.asMap(EventType.CREATED, IntStream.range(0, 5).boxed().collect(Collectors.toMap(i -> i * 2, i -> i * 2)));
assertEquals(expRes, aggregateListenerEvents(lsnr1, 5));
assertEquals(expRes, aggregateListenerEvents(lsnr2, 5));
assertEquals(expRes, aggregateListenerEvents(lsnr3, 5));
for (int i = 0; i < 10; i++) cache.put(i, -i);
expRes = F.asMap(EventType.UPDATED, IntStream.range(0, 5).boxed().collect(Collectors.toMap(i -> i * 2, i -> -i * 2)));
assertEquals(expRes, aggregateListenerEvents(lsnr1, 5));
assertEquals(expRes, aggregateListenerEvents(lsnr2, 5));
assertEquals(expRes, aggregateListenerEvents(lsnr3, 5));
for (int i = 0; i < 10; i++) cache.remove(i);
expRes = F.asMap(EventType.REMOVED, IntStream.range(0, 5).boxed().collect(Collectors.toMap(i -> i * 2, i -> -i * 2)));
assertEquals(expRes, aggregateListenerEvents(lsnr1, 5));
assertEquals(expRes, aggregateListenerEvents(lsnr2, 5));
assertEquals(expRes, aggregateListenerEvents(lsnr3, 5));
assertTrue(lsnr1.isQueueEmpty());
assertTrue(lsnr2.isQueueEmpty());
assertTrue(lsnr3.isQueueEmpty());
}
}
Aggregations