Search in sources :

Example 16 with IgniteBiPredicate

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());
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) IgniteBiPredicate(org.apache.ignite.lang.IgniteBiPredicate) Ignite(org.apache.ignite.Ignite) UUID(java.util.UUID)

Example 17 with IgniteBiPredicate

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()));
        }
    });
}
Also used : TestJob(org.apache.storm.testing.TestJob) IgniteBiPredicate(org.apache.ignite.lang.IgniteBiPredicate) TopologyBuilder(org.apache.storm.topology.TopologyBuilder) Config(org.apache.storm.Config) StormTopology(org.apache.storm.generated.StormTopology) IgniteCache(org.apache.ignite.IgniteCache) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) MkClusterParam(org.apache.storm.testing.MkClusterParam) ILocalCluster(org.apache.storm.ILocalCluster) MockedSources(org.apache.storm.testing.MockedSources) CompleteTopologyParam(org.apache.storm.testing.CompleteTopologyParam) CacheEvent(org.apache.ignite.events.CacheEvent) UUID(java.util.UUID)

Example 18 with IgniteBiPredicate

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();
    }
}
Also used : IgniteBiPredicate(org.apache.ignite.lang.IgniteBiPredicate) Ignite(org.apache.ignite.Ignite)

Example 19 with IgniteBiPredicate

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();
    }
}
Also used : IgniteBiPredicate(org.apache.ignite.lang.IgniteBiPredicate) CountDownLatch(java.util.concurrent.CountDownLatch) GridConcurrentHashSet(org.apache.ignite.internal.util.GridConcurrentHashSet) CacheEvent(org.apache.ignite.events.CacheEvent) Ignite(org.apache.ignite.Ignite) UUID(java.util.UUID) HashMap(java.util.HashMap) Map(java.util.Map)

Example 20 with IgniteBiPredicate

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();
    }
}
Also used : IgniteBiPredicate(org.apache.ignite.lang.IgniteBiPredicate) MessageAndMetadata(kafka.message.MessageAndMetadata) CountDownLatch(java.util.concurrent.CountDownLatch) TimeoutException(java.util.concurrent.TimeoutException) CacheEvent(org.apache.ignite.events.CacheEvent) Ignite(org.apache.ignite.Ignite) UUID(java.util.UUID) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

IgniteBiPredicate (org.apache.ignite.lang.IgniteBiPredicate)23 Ignite (org.apache.ignite.Ignite)18 UUID (java.util.UUID)12 CountDownLatch (java.util.concurrent.CountDownLatch)9 CacheEvent (org.apache.ignite.events.CacheEvent)8 IgniteCache (org.apache.ignite.IgniteCache)6 IgnitePredicate (org.apache.ignite.lang.IgnitePredicate)5 Cache (javax.cache.Cache)4 IgniteException (org.apache.ignite.IgniteException)4 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 List (java.util.List)3 ScanQuery (org.apache.ignite.cache.query.ScanQuery)3 ClusterNode (org.apache.ignite.cluster.ClusterNode)3 Map (java.util.Map)2 CacheException (javax.cache.CacheException)2 CacheEntryEvent (javax.cache.event.CacheEntryEvent)2 CacheEntryEventFilter (javax.cache.event.CacheEntryEventFilter)2 IgniteClientDisconnectedException (org.apache.ignite.IgniteClientDisconnectedException)2 Ignition (org.apache.ignite.Ignition)2