use of org.apache.ignite.lang.IgniteBiPredicate in project ignite by apache.
the class GridClosureProcessorRemoteTest method testAnonymousUnicastRequest.
/**
* @throws Exception Thrown in case of failure.
*/
public void testAnonymousUnicastRequest() throws Exception {
Ignite g = grid(0);
assert g.cluster().nodes().size() == NODES_CNT;
execCntr.set(0);
ClusterNode rmt = F.first(g.cluster().forRemotes().nodes());
final ClusterNode loc = g.cluster().localNode();
compute(g.cluster().forNode(rmt)).run(new CARemote() {
@Override
public void apply() {
message(grid(1).cluster().forNode(loc)).localListen(null, new IgniteBiPredicate<UUID, String>() {
@Override
public boolean apply(UUID uuid, String s) {
log.info("Received test message [nodeId: " + uuid + ", s=" + s + ']');
ignite.countDownLatch("messagesPending", 1, false, true).countDown();
execCntr.incrementAndGet();
return false;
}
});
}
});
message(g.cluster().forNode(rmt)).send(null, "TESTING...");
assertTrue(g.countDownLatch("messagesPending", 1, false, true).await(2000));
assertEquals(0, execCntr.get());
}
use of org.apache.ignite.lang.IgniteBiPredicate in project ignite by apache.
the class StormIgniteStreamerSelfTest method testStormStreamerIgniteBolt.
/**
* Tests for the streamer bolt. Ignite started in bolt based on what is specified in the configuration file.
*
* @throws TimeoutException
* @throws InterruptedException
*/
public void testStormStreamerIgniteBolt() throws TimeoutException, InterruptedException {
final StormStreamer<String, String> stormStreamer = new StormStreamer<>();
stormStreamer.setAutoFlushFrequency(10L);
stormStreamer.setAllowOverwrite(true);
stormStreamer.setCacheName(TEST_CACHE);
stormStreamer.setIgniteTupleField(TestStormSpout.IGNITE_TUPLE_FIELD);
stormStreamer.setIgniteConfigFile(GRID_CONF_FILE);
Config daemonConf = new Config();
daemonConf.put(Config.STORM_LOCAL_MODE_ZMQ, false);
MkClusterParam mkClusterParam = new MkClusterParam();
mkClusterParam.setDaemonConf(daemonConf);
mkClusterParam.setSupervisors(4);
final CountDownLatch latch = new CountDownLatch(TestStormSpout.CNT);
IgniteBiPredicate<UUID, CacheEvent> putLsnr = new IgniteBiPredicate<UUID, CacheEvent>() {
@Override
public boolean apply(UUID uuid, CacheEvent evt) {
assert evt != null;
latch.countDown();
return true;
}
};
final UUID putLsnrId = ignite.events(ignite.cluster().forCacheNodes(TEST_CACHE)).remoteListen(putLsnr, null, EVT_CACHE_OBJECT_PUT);
Testing.withSimulatedTimeLocalCluster(mkClusterParam, new TestJob() {
@Override
public void run(ILocalCluster cluster) throws IOException, InterruptedException {
// Creates a test topology.
TopologyBuilder builder = new TopologyBuilder();
TestStormSpout testStormSpout = new TestStormSpout();
builder.setSpout("test-spout", testStormSpout);
builder.setBolt("ignite-bolt", stormStreamer, STORM_EXECUTORS).shuffleGrouping("test-spout");
StormTopology topology = builder.createTopology();
// Prepares a mock data for the spout.
MockedSources mockedSources = new MockedSources();
mockedSources.addMockData("test-spout", getMockData());
// Prepares the config.
Config conf = new Config();
conf.setMessageTimeoutSecs(10);
IgniteCache<Integer, String> cache = ignite.cache(TEST_CACHE);
CompleteTopologyParam completeTopologyParam = new CompleteTopologyParam();
completeTopologyParam.setTimeoutMs(10000);
completeTopologyParam.setMockedSources(mockedSources);
completeTopologyParam.setStormConf(conf);
// Checks the cache doesn't contain any entries yet.
assertEquals(0, cache.size(CachePeekMode.PRIMARY));
Testing.completeTopology(cluster, topology, completeTopologyParam);
// Checks events successfully processed in 20 seconds.
assertTrue(latch.await(10, TimeUnit.SECONDS));
ignite.events(ignite.cluster().forCacheNodes(TEST_CACHE)).stopRemoteListen(putLsnrId);
// Validates all entries are in the cache.
assertEquals(TestStormSpout.CNT, cache.size(CachePeekMode.PRIMARY));
for (Map.Entry<Integer, String> entry : TestStormSpout.getKeyValMap().entrySet()) assertEquals(entry.getValue(), cache.get(entry.getKey()));
}
});
}
use of org.apache.ignite.lang.IgniteBiPredicate in project ignite by apache.
the class IgniteQueryDedicatedPoolTest method testScanQueryUsesDedicatedThreadPool.
/**
* Tests that Scan queries are executed in dedicated pool
* @throws Exception If failed.
*/
public void testScanQueryUsesDedicatedThreadPool() throws Exception {
try (Ignite client = startGrid("client")) {
IgniteCache<Integer, Integer> cache = client.cache(CACHE_NAME);
cache.put(0, 0);
QueryCursor<Cache.Entry<Object, Object>> cursor = cache.query(new ScanQuery<>(new IgniteBiPredicate<Object, Object>() {
@Override
public boolean apply(Object o, Object o2) {
return F.eq(GridIoManager.currentPolicy(), GridIoPolicy.QUERY_POOL);
}
}));
assertEquals(1, cursor.getAll().size());
cursor.close();
}
}
use of org.apache.ignite.lang.IgniteBiPredicate in project ignite by apache.
the class SocketStreamerSelfTest method test.
/**
* @param converter Converter.
* @param r Runnable..
*/
private void test(@Nullable SocketMessageConverter<Message> converter, @Nullable byte[] delim, Runnable r, boolean oneMessagePerTuple) throws Exception {
SocketStreamer<Message, Integer, String> sockStmr = null;
Ignite ignite = grid(0);
IgniteCache<Integer, String> cache = ignite.cache(DEFAULT_CACHE_NAME);
cache.clear();
try (IgniteDataStreamer<Integer, String> stmr = ignite.dataStreamer(DEFAULT_CACHE_NAME)) {
stmr.allowOverwrite(true);
stmr.autoFlushFrequency(10);
sockStmr = new SocketStreamer<>();
sockStmr.setIgnite(ignite);
sockStmr.setStreamer(stmr);
sockStmr.setPort(port);
sockStmr.setDelimiter(delim);
if (oneMessagePerTuple) {
sockStmr.setSingleTupleExtractor(new StreamSingleTupleExtractor<Message, Integer, String>() {
@Override
public Map.Entry<Integer, String> extract(Message msg) {
return new IgniteBiTuple<>(msg.key, msg.val);
}
});
} else {
sockStmr.setMultipleTupleExtractor(new StreamMultipleTupleExtractor<Message, Integer, String>() {
@Override
public Map<Integer, String> extract(Message msg) {
Map<Integer, String> answer = new HashMap<>();
for (int value : msg.values) {
answer.put(value, Integer.toString(value));
}
return answer;
}
});
}
if (converter != null)
sockStmr.setConverter(converter);
final CountDownLatch latch = new CountDownLatch(CNT);
final GridConcurrentHashSet<CacheEvent> evts = new GridConcurrentHashSet<>();
IgniteBiPredicate<UUID, CacheEvent> locLsnr = new IgniteBiPredicate<UUID, CacheEvent>() {
@Override
public boolean apply(UUID uuid, CacheEvent evt) {
evts.add(evt);
latch.countDown();
return true;
}
};
ignite.events(ignite.cluster().forCacheNodes(DEFAULT_CACHE_NAME)).remoteListen(locLsnr, null, EVT_CACHE_OBJECT_PUT);
sockStmr.start();
r.run();
latch.await();
for (int i = 0; i < CNT; i++) {
Object val = cache.get(i);
String exp = Integer.toString(i);
if (!exp.equals(val))
log.error("Unexpected cache value [key=" + i + ", exp=" + exp + ", val=" + val + ", evts=" + evts + ']');
assertEquals(exp, val);
}
assertEquals(CNT, cache.size(CachePeekMode.PRIMARY));
} finally {
if (sockStmr != null)
sockStmr.stop();
}
}
use of org.apache.ignite.lang.IgniteBiPredicate in project ignite by apache.
the class KafkaIgniteStreamerSelfTest method consumerStream.
/**
* Consumes Kafka stream via Ignite.
*
* @param topic Topic name.
* @param keyValMap Expected key value map.
* @throws TimeoutException If timed out.
* @throws InterruptedException If interrupted.
*/
private void consumerStream(String topic, Map<String, String> keyValMap) throws TimeoutException, InterruptedException {
KafkaStreamer<String, String> kafkaStmr = null;
Ignite ignite = grid();
try (IgniteDataStreamer<String, String> stmr = ignite.dataStreamer(DEFAULT_CACHE_NAME)) {
stmr.allowOverwrite(true);
stmr.autoFlushFrequency(10);
// Configure Kafka streamer.
kafkaStmr = new KafkaStreamer<>();
// Get the cache.
IgniteCache<String, String> cache = ignite.cache(DEFAULT_CACHE_NAME);
// Set Ignite instance.
kafkaStmr.setIgnite(ignite);
// Set data streamer instance.
kafkaStmr.setStreamer(stmr);
// Set the topic.
kafkaStmr.setTopic(topic);
// Set the number of threads.
kafkaStmr.setThreads(4);
// Set the consumer configuration.
kafkaStmr.setConsumerConfig(createDefaultConsumerConfig(embeddedBroker.getZookeeperAddress(), "groupX"));
kafkaStmr.setMultipleTupleExtractor(new StreamMultipleTupleExtractor<MessageAndMetadata<byte[], byte[]>, String, String>() {
@Override
public Map<String, String> extract(MessageAndMetadata<byte[], byte[]> msg) {
Map<String, String> entries = new HashMap<>();
try {
String key = new String(msg.key());
String val = new String(msg.message());
// Convert the message into number of cache entries with same key or dynamic key from actual message.
// For now using key as cache entry key and value as cache entry value - for test purpose.
entries.put(key, val);
} catch (Exception ex) {
fail("Unexpected error." + ex);
}
return entries;
}
});
// Start kafka streamer.
kafkaStmr.start();
final CountDownLatch latch = new CountDownLatch(CNT);
IgniteBiPredicate<UUID, CacheEvent> locLsnr = new IgniteBiPredicate<UUID, CacheEvent>() {
@Override
public boolean apply(UUID uuid, CacheEvent evt) {
latch.countDown();
return true;
}
};
ignite.events(ignite.cluster().forCacheNodes(DEFAULT_CACHE_NAME)).remoteListen(locLsnr, null, EVT_CACHE_OBJECT_PUT);
// Checks all events successfully processed in 10 seconds.
assertTrue(latch.await(10, TimeUnit.SECONDS));
for (Map.Entry<String, String> entry : keyValMap.entrySet()) assertEquals(entry.getValue(), cache.get(entry.getKey()));
} finally {
if (kafkaStmr != null)
kafkaStmr.stop();
}
}
Aggregations