Search in sources :

Example 26 with GridConcurrentHashSet

use of org.apache.ignite.internal.util.GridConcurrentHashSet in project ignite by apache.

the class GridMessagingSelfTest method testSendReceiveMessageWithEnumTopic.

/**
 * Tests simple message sending-receiving with enumerated topic.
 *
 * @throws Exception If error occurs.
 */
public void testSendReceiveMessageWithEnumTopic() throws Exception {
    final Collection<Object> rcvMsgs = new GridConcurrentHashSet<>();
    // to make it modifiable
    final AtomicBoolean error = new AtomicBoolean(false);
    final CountDownLatch rcvLatch = new CountDownLatch(3);
    ignite1.message().localListen(TestTopic.TOPIC_1, new P2<UUID, Object>() {

        @Override
        public boolean apply(UUID nodeId, Object msg) {
            try {
                log.info("Received new message [msg=" + msg + ", senderNodeId=" + nodeId + ", topic=" + TestTopic.TOPIC_1 + ']');
                if (!nodeId.equals(ignite1.cluster().localNode().id())) {
                    log.error("Unexpected sender node: " + nodeId);
                    error.set(true);
                    return false;
                }
                if (!MSG_1.equals(msg)) {
                    log.error("Unexpected message " + msg + " for topic: " + TestTopic.TOPIC_1);
                    error.set(true);
                    return false;
                }
                rcvMsgs.add(msg);
                return true;
            } finally {
                rcvLatch.countDown();
            }
        }
    });
    ignite1.message().localListen(TestTopic.TOPIC_2, new P2<UUID, Object>() {

        @Override
        public boolean apply(UUID nodeId, Object msg) {
            try {
                log.info("Received new message [msg=" + msg + ", senderNodeId=" + nodeId + ", topic=" + TestTopic.TOPIC_2 + ']');
                if (!nodeId.equals(ignite1.cluster().localNode().id())) {
                    log.error("Unexpected sender node: " + nodeId);
                    error.set(true);
                    return false;
                }
                if (!MSG_2.equals(msg)) {
                    log.error("Unexpected message " + msg + " for topic: " + TestTopic.TOPIC_2);
                    error.set(true);
                    return false;
                }
                rcvMsgs.add(msg);
                return true;
            } finally {
                rcvLatch.countDown();
            }
        }
    });
    ignite1.message().localListen(null, new P2<UUID, Object>() {

        @Override
        public boolean apply(UUID nodeId, Object msg) {
            try {
                log.info("Received new message [msg=" + msg + ", senderNodeId=" + nodeId + ", topic=default]");
                if (!nodeId.equals(ignite1.cluster().localNode().id())) {
                    log.error("Unexpected sender node: " + nodeId);
                    error.set(true);
                    return false;
                }
                if (!MSG_3.equals(msg)) {
                    log.error("Unexpected message " + msg + " for topic: default");
                    error.set(true);
                    return false;
                }
                rcvMsgs.add(msg);
                return true;
            } finally {
                rcvLatch.countDown();
            }
        }
    });
    ClusterGroup rNode1 = ignite1.cluster().forLocal();
    message(rNode1).send(TestTopic.TOPIC_1, MSG_1);
    message(rNode1).send(TestTopic.TOPIC_2, MSG_2);
    message(rNode1).send(null, MSG_3);
    assertTrue(rcvLatch.await(3, TimeUnit.SECONDS));
    assertFalse(error.get());
    assertTrue(rcvMsgs.contains(MSG_1));
    assertTrue(rcvMsgs.contains(MSG_2));
    assertTrue(rcvMsgs.contains(MSG_3));
}
Also used : GridConcurrentHashSet(org.apache.ignite.internal.util.GridConcurrentHashSet) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ClusterGroup(org.apache.ignite.cluster.ClusterGroup) CountDownLatch(java.util.concurrent.CountDownLatch) UUID(java.util.UUID)

Example 27 with GridConcurrentHashSet

use of org.apache.ignite.internal.util.GridConcurrentHashSet 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 28 with GridConcurrentHashSet

use of org.apache.ignite.internal.util.GridConcurrentHashSet in project ignite by apache.

the class GridEventConsumeSelfTest method testMultithreadedWithNodeRestart.

/**
 * @throws Exception If failed.
 */
public void testMultithreadedWithNodeRestart() throws Exception {
    final AtomicBoolean stop = new AtomicBoolean();
    final BlockingQueue<IgniteBiTuple<Integer, UUID>> queue = new LinkedBlockingQueue<>();
    final Collection<UUID> started = new GridConcurrentHashSet<>();
    final Collection<UUID> stopped = new GridConcurrentHashSet<>();
    final Random rnd = new Random();
    IgniteInternalFuture<?> starterFut = multithreadedAsync(new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            for (int i = 0; i < CONSUME_CNT; i++) {
                int idx = rnd.nextInt(GRID_CNT);
                try {
                    IgniteEvents evts = grid(idx).events();
                    UUID consumeId = evts.remoteListenAsync(new P2<UUID, Event>() {

                        @Override
                        public boolean apply(UUID uuid, Event evt) {
                            return true;
                        }
                    }, null, EVT_JOB_STARTED).get(3000);
                    started.add(consumeId);
                    queue.add(F.t(idx, consumeId));
                } catch (ClusterTopologyException ignored) {
                // No-op.
                }
                U.sleep(10);
            }
            stop.set(true);
            return null;
        }
    }, 8, "consume-starter");
    IgniteInternalFuture<?> stopperFut = multithreadedAsync(new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            while (!stop.get()) {
                IgniteBiTuple<Integer, UUID> t = queue.poll(1, SECONDS);
                if (t == null)
                    continue;
                int idx = t.get1();
                UUID consumeId = t.get2();
                try {
                    IgniteEvents evts = grid(idx).events();
                    evts.stopRemoteListenAsync(consumeId).get(3000);
                    stopped.add(consumeId);
                } catch (ClusterTopologyException ignored) {
                // No-op.
                }
            }
            return null;
        }
    }, 4, "consume-stopper");
    IgniteInternalFuture<?> nodeRestarterFut = multithreadedAsync(new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            while (!stop.get()) {
                startGrid("anotherGrid");
                stopGrid("anotherGrid");
            }
            return null;
        }
    }, 1, "node-restarter");
    IgniteInternalFuture<?> jobRunnerFut = multithreadedAsync(new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            while (!stop.get()) {
                int idx = rnd.nextInt(GRID_CNT);
                try {
                    grid(idx).compute().runAsync(F.noop()).get(3000);
                } catch (IgniteException ignored) {
                // Ignore all job execution related errors.
                }
            }
            return null;
        }
    }, 1, "job-runner");
    starterFut.get();
    stopperFut.get();
    nodeRestarterFut.get();
    jobRunnerFut.get();
    IgniteBiTuple<Integer, UUID> t;
    while ((t = queue.poll()) != null) {
        int idx = t.get1();
        UUID consumeId = t.get2();
        grid(idx).events().stopRemoteListenAsync(consumeId).get(3000);
        stopped.add(consumeId);
    }
    Collection<UUID> notStopped = F.lose(started, true, stopped);
    assertEquals("Not stopped IDs: " + notStopped, 0, notStopped.size());
}
Also used : IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) ClusterTopologyException(org.apache.ignite.cluster.ClusterTopologyException) IgniteException(org.apache.ignite.IgniteException) GridConcurrentHashSet(org.apache.ignite.internal.util.GridConcurrentHashSet) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Random(java.util.Random) IgniteEvents(org.apache.ignite.IgniteEvents) IgniteException(org.apache.ignite.IgniteException) DiscoveryEvent(org.apache.ignite.events.DiscoveryEvent) JobEvent(org.apache.ignite.events.JobEvent) Event(org.apache.ignite.events.Event) ClusterTopologyException(org.apache.ignite.cluster.ClusterTopologyException) UUID(java.util.UUID)

Aggregations

GridConcurrentHashSet (org.apache.ignite.internal.util.GridConcurrentHashSet)28 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)14 IOException (java.io.IOException)12 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)12 IgniteException (org.apache.ignite.IgniteException)11 Ignite (org.apache.ignite.Ignite)10 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)9 IgniteInterruptedException (org.apache.ignite.IgniteInterruptedException)8 IgniteLock (org.apache.ignite.IgniteLock)8 ExpectedException (org.junit.rules.ExpectedException)8 UUID (java.util.UUID)7 CountDownLatch (java.util.concurrent.CountDownLatch)7 Map (java.util.Map)6 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)5 List (java.util.List)5 IgniteCache (org.apache.ignite.IgniteCache)5 ClusterGroup (org.apache.ignite.cluster.ClusterGroup)5 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 FSDataInputStream (org.apache.hadoop.fs.FSDataInputStream)4