Search in sources :

Example 26 with GridMessageListener

use of org.apache.ignite.internal.managers.communication.GridMessageListener in project ignite by apache.

the class TxDeadlockDetectionMessageMarshallingTest method testMessageUnmarshallWithoutCacheContext.

/**
 * @throws Exception If failed.
 */
@Test
public void testMessageUnmarshallWithoutCacheContext() throws Exception {
    try {
        Ignite ignite = startGrid(0);
        CacheConfiguration<Integer, Integer> ccfg = new CacheConfiguration<>(DEFAULT_CACHE_NAME);
        IgniteCache<Integer, Integer> cache = ignite.getOrCreateCache(ccfg);
        Ignite client = startClientGrid(1);
        final GridCacheSharedContext<Object, Object> clientCtx = ((IgniteKernal) client).context().cache().context();
        final CountDownLatch latch = new CountDownLatch(1);
        final AtomicBoolean res = new AtomicBoolean();
        clientCtx.gridIO().addMessageListener(TOPIC, new GridMessageListener() {

            @Override
            public void onMessage(UUID nodeId, Object msg, byte plc) {
                if (msg instanceof TxLocksResponse) {
                    try {
                        ((TxLocksResponse) msg).finishUnmarshal(clientCtx, clientCtx.deploy().globalLoader());
                        res.set(true);
                    } catch (Exception e) {
                        log.error("Message unmarshal failed", e);
                    } finally {
                        latch.countDown();
                    }
                }
            }
        });
        GridCacheContext cctx = ((IgniteCacheProxy) cache).context();
        KeyCacheObject key = cctx.toCacheKeyObject(1);
        TxLocksResponse msg = new TxLocksResponse();
        msg.addKey(cctx.txKey(key));
        msg.prepareMarshal(cctx.shared());
        ((IgniteKernal) ignite).context().cache().context().gridIO().sendToCustomTopic(((IgniteKernal) client).localNode(), TOPIC, msg, GridIoPolicy.PUBLIC_POOL);
        boolean await = latch.await(1, TimeUnit.SECONDS);
        assertTrue(await && res.get());
    } finally {
        stopAllGrids();
    }
}
Also used : IgniteKernal(org.apache.ignite.internal.IgniteKernal) GridCacheContext(org.apache.ignite.internal.processors.cache.GridCacheContext) GridMessageListener(org.apache.ignite.internal.managers.communication.GridMessageListener) IgniteCacheProxy(org.apache.ignite.internal.processors.cache.IgniteCacheProxy) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Ignite(org.apache.ignite.Ignite) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) UUID(java.util.UUID) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 27 with GridMessageListener

use of org.apache.ignite.internal.managers.communication.GridMessageListener in project ignite by apache.

the class GridCachePartitionedGetSelfTest method prepare.

/**
 * Puts value to primary node and registers listener
 * that sets {@link #received} flag to {@code true}
 * if {@link GridNearGetRequest} was received on primary node.
 *
 * @throws Exception If failed.
 */
private void prepare() throws Exception {
    for (int i = 0; i < GRID_CNT; i++) {
        Ignite g = grid(i);
        if (grid(i).affinity(DEFAULT_CACHE_NAME).isPrimary(grid(i).localNode(), KEY)) {
            info("Primary node: " + g.cluster().localNode().id());
            // Put value.
            g.cache(DEFAULT_CACHE_NAME).put(KEY, VAL);
            // Register listener.
            ((IgniteKernal) g).context().io().addMessageListener(TOPIC_CACHE, new GridMessageListener() {

                @Override
                public void onMessage(UUID nodeId, Object msg, byte plc) {
                    info("Received message from node [nodeId=" + nodeId + ", msg=" + msg + ']');
                    if (msg instanceof GridNearSingleGetRequest) {
                        info("Setting flag: " + System.identityHashCode(received));
                        received.set(true);
                    }
                }
            });
            break;
        }
    }
}
Also used : IgniteKernal(org.apache.ignite.internal.IgniteKernal) GridNearSingleGetRequest(org.apache.ignite.internal.processors.cache.distributed.near.GridNearSingleGetRequest) GridMessageListener(org.apache.ignite.internal.managers.communication.GridMessageListener) Ignite(org.apache.ignite.Ignite) UUID(java.util.UUID)

Example 28 with GridMessageListener

use of org.apache.ignite.internal.managers.communication.GridMessageListener in project ignite by apache.

the class GridIoManagerBenchmark0 method testVariableLoad.

/**
 * @throws Exception If failed.
 */
@Test
public void testVariableLoad() throws Exception {
    final IgniteKernal sndKernal = (IgniteKernal) grid(0);
    final IgniteKernal rcvKernal = (IgniteKernal) grid(1);
    final ClusterNode sndNode = sndKernal.localNode();
    final ClusterNode rcvNode = rcvKernal.localNode();
    final GridIoManager snd = sndKernal.context().io();
    final GridIoManager rcv = rcvKernal.context().io();
    info("Senders: " + THREADS);
    info("Messages: " + CONCUR_MSGS);
    final Semaphore sem = new Semaphore(CONCUR_MSGS);
    final LongAdder msgCntr = new LongAdder();
    final String topic = "test-topic";
    final Map<IgniteUuid, CountDownLatch> latches = new ConcurrentHashMap<>();
    rcv.addMessageListener(topic, new GridMessageListener() {

        @Override
        public void onMessage(UUID nodeId, Object msg, byte plc) {
            try {
                rcv.sendToCustomTopic(sndNode, topic, (Message) msg, PUBLIC_POOL);
            } catch (IgniteCheckedException e) {
                error("Failed to send message.", e);
            }
        }
    });
    snd.addMessageListener(topic, new GridMessageListener() {

        @Override
        public void onMessage(UUID nodeId, Object msg, byte plc) {
            msgCntr.increment();
            sem.release();
            CountDownLatch latch = latches.get(((GridTestMessage) msg).id());
            if (latch != null)
                latch.countDown();
        }
    });
    final AtomicBoolean finish = new AtomicBoolean();
    final AtomicReference<CountDownLatch> latchRef = new AtomicReference<>();
    IgniteInternalFuture<?> f = GridTestUtils.runMultiThreadedAsync(new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            while (!finish.get()) {
                CountDownLatch latch = latchRef.get();
                if (latch != null)
                    U.await(latch);
                IgniteUuid msgId = IgniteUuid.randomUuid();
                sem.acquire();
                snd.sendToCustomTopic(rcvNode, topic, new GridTestMessage(msgId, (String) null), PUBLIC_POOL);
            }
            return null;
        }
    }, THREADS, "send-thread");
    IgniteInternalFuture<?> f1 = GridTestUtils.runMultiThreadedAsync(new Callable<Object>() {

        private long ts = System.currentTimeMillis();

        @Override
        public Object call() throws Exception {
            try {
                while (!finish.get()) {
                    info(U.nl() + ">>>" + U.nl() + ">>> High load." + U.nl() + ">>>");
                    U.sleep(15 * 1000);
                    reportNumbers();
                    info(U.nl() + ">>>" + U.nl() + ">>> Low load." + U.nl() + ">>>");
                    CountDownLatch latch = new CountDownLatch(1);
                    try {
                        // Here will be a pause.
                        latchRef.set(latch);
                        U.sleep(7 * 1000);
                        reportNumbers();
                    } finally {
                        latch.countDown();
                    }
                }
            } catch (IgniteCheckedException e) {
                X.println("Message send failed", e);
            }
            return null;
        }

        /**
         */
        void reportNumbers() {
            long newTs = System.currentTimeMillis();
            long qrys = msgCntr.sumThenReset();
            long time = newTs - ts;
            X.println("Communication benchmark [qps=" + qrys * 1000 / time + ", executed=" + qrys + ", time=" + time + ']');
            ts = newTs;
        }
    }, 1, "load-dispatcher");
    IgniteInternalFuture<?> f2 = GridTestUtils.runMultiThreadedAsync(new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            while (!finish.get()) {
                U.sleep(1000);
                IgniteUuid msgId = IgniteUuid.randomUuid();
                CountDownLatch latch = new CountDownLatch(1);
                latches.put(msgId, latch);
                snd.sendToCustomTopic(rcvNode, topic, new GridTestMessage(msgId, (String) null), PUBLIC_POOL);
                long start = System.currentTimeMillis();
                latch.await();
                info("Response time: " + (System.currentTimeMillis() - start));
            }
            return null;
        }
    }, THREADS, "low-loader");
    Thread.sleep(TEST_TIMEOUT);
    finish.set(true);
    sem.release(CONCUR_MSGS * 2);
    f.get();
    f1.get();
    f2.get();
}
Also used : Message(org.apache.ignite.plugin.extensions.communication.Message) GridMessageListener(org.apache.ignite.internal.managers.communication.GridMessageListener) Semaphore(java.util.concurrent.Semaphore) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteUuid(org.apache.ignite.lang.IgniteUuid) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) UUID(java.util.UUID) ClusterNode(org.apache.ignite.cluster.ClusterNode) IgniteKernal(org.apache.ignite.internal.IgniteKernal) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) LongAdder(java.util.concurrent.atomic.LongAdder) GridIoManager(org.apache.ignite.internal.managers.communication.GridIoManager) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 29 with GridMessageListener

use of org.apache.ignite.internal.managers.communication.GridMessageListener in project ignite by apache.

the class GridIoManagerBenchmark0 method testThroughput.

/**
 * @throws Exception If failed.
 */
@Test
public void testThroughput() throws Exception {
    final IgniteKernal sndKernal = (IgniteKernal) grid(0);
    final IgniteKernal rcvKernal = (IgniteKernal) grid(1);
    final ClusterNode sndNode = sndKernal.localNode();
    final ClusterNode rcvNode = rcvKernal.localNode();
    final GridIoManager snd = sndKernal.context().io();
    final GridIoManager rcv = rcvKernal.context().io();
    info("Senders: " + THREADS);
    info("Messages: " + CONCUR_MSGS);
    final Semaphore sem = new Semaphore(CONCUR_MSGS);
    final LongAdder msgCntr = new LongAdder();
    final String topic = "test-topic";
    rcv.addMessageListener(topic, new GridMessageListener() {

        @Override
        public void onMessage(UUID nodeId, Object msg, byte plc) {
            try {
                rcv.sendToCustomTopic(sndNode, topic, (Message) msg, PUBLIC_POOL);
            } catch (IgniteCheckedException e) {
                error("Failed to send message.", e);
            }
        }
    });
    snd.addMessageListener(topic, new GridMessageListener() {

        @Override
        public void onMessage(UUID nodeId, Object msg, byte plc) {
            msgCntr.increment();
            sem.release();
        }
    });
    Timer t = new Timer("results-reporter");
    t.schedule(new TimerTask() {

        private long ts = System.currentTimeMillis();

        @Override
        public void run() {
            long newTs = System.currentTimeMillis();
            long qrys = msgCntr.sumThenReset();
            long time = newTs - ts;
            X.println("Communication benchmark [qps=" + qrys * 1000 / time + ", executed=" + qrys + ", time=" + time + ']');
            ts = newTs;
        }
    }, 10000, 10000);
    final AtomicBoolean finish = new AtomicBoolean();
    IgniteInternalFuture<?> f = GridTestUtils.runMultiThreadedAsync(new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            try {
                IgniteUuid msgId = IgniteUuid.randomUuid();
                while (!finish.get()) {
                    sem.acquire();
                    snd.sendToCustomTopic(rcvNode, topic, new GridTestMessage(msgId, (String) null), PUBLIC_POOL);
                }
            } catch (IgniteCheckedException e) {
                X.println("Message send failed", e);
            } catch (InterruptedException ignored) {
            // No-op.
            }
            return null;
        }
    }, THREADS, "send-thread");
    Thread.sleep(TEST_TIMEOUT);
    finish.set(true);
    sem.release(CONCUR_MSGS * 2);
    t.cancel();
    f.get();
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) IgniteKernal(org.apache.ignite.internal.IgniteKernal) Message(org.apache.ignite.plugin.extensions.communication.Message) GridMessageListener(org.apache.ignite.internal.managers.communication.GridMessageListener) Semaphore(java.util.concurrent.Semaphore) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) LongAdder(java.util.concurrent.atomic.LongAdder) Timer(java.util.Timer) TimerTask(java.util.TimerTask) GridIoManager(org.apache.ignite.internal.managers.communication.GridIoManager) IgniteUuid(org.apache.ignite.lang.IgniteUuid) UUID(java.util.UUID) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 30 with GridMessageListener

use of org.apache.ignite.internal.managers.communication.GridMessageListener in project ignite by apache.

the class GridCacheMessageSelfTest method doSend.

/**
 * @throws Exception If failed.
 */
private void doSend() throws Exception {
    GridIoManager mgr0 = grid(0).context().io();
    GridIoManager mgr1 = grid(1).context().io();
    String topic = "test-topic";
    final CountDownLatch latch = new CountDownLatch(SAMPLE_CNT);
    mgr1.addMessageListener(topic, new GridMessageListener() {

        @Override
        public void onMessage(UUID nodeId, Object msg, byte plc) {
            try {
                latch.countDown();
                Collection<TestMessage1> messages = ((TestMessage) msg).entries();
                assertEquals(10, messages.size());
                int cnt = 0;
                for (TestMessage1 msg1 : messages) {
                    assertTrue(msg1.body().contains(TEST_BODY));
                    int i = Integer.parseInt(msg1.body().substring(TEST_BODY.length() + 1));
                    assertEquals(cnt, i);
                    TestMessage2 msg2 = (TestMessage2) msg1.message();
                    assertEquals(TEST_BODY + "_" + i + "_2", msg2.body());
                    assertEquals(grid(0).localNode().id(), msg2.nodeId());
                    assertEquals(i, msg2.id());
                    GridTestMessage msg3 = (GridTestMessage) msg2.message();
                    assertEquals(cnt, msg3.getMsgId());
                    assertEquals(grid(1).localNode().id(), msg3.getSourceNodeId());
                    cnt++;
                }
            } catch (Exception e) {
                fail("Exception " + e.getMessage());
            }
        }
    });
    TestMessage msg = new TestMessage();
    for (int i = 0; i < 10; i++) {
        TestMessage2 mes1 = new TestMessage2();
        mes1.init(new GridTestMessage(grid(1).localNode().id(), i, 0), grid(0).localNode().id(), i, TEST_BODY + "_" + i + "_2");
        TestMessage1 mes2 = new TestMessage1();
        mes2.init(mes1, TEST_BODY + "_" + i);
        msg.add(mes2);
    }
    mgr0.sendToCustomTopic(grid(1).localNode(), topic, msg, GridIoPolicy.PUBLIC_POOL);
    assert latch.await(3, SECONDS);
}
Also used : GridMessageListener(org.apache.ignite.internal.managers.communication.GridMessageListener) CountDownLatch(java.util.concurrent.CountDownLatch) GridIoManager(org.apache.ignite.internal.managers.communication.GridIoManager) Collection(java.util.Collection) GridDirectCollection(org.apache.ignite.internal.GridDirectCollection) UUID(java.util.UUID)

Aggregations

UUID (java.util.UUID)30 GridMessageListener (org.apache.ignite.internal.managers.communication.GridMessageListener)30 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)15 ClusterNode (org.apache.ignite.cluster.ClusterNode)12 DiscoveryEvent (org.apache.ignite.events.DiscoveryEvent)11 Event (org.apache.ignite.events.Event)11 GridLocalEventListener (org.apache.ignite.internal.managers.eventstorage.GridLocalEventListener)11 GridIoManager (org.apache.ignite.internal.managers.communication.GridIoManager)7 Test (org.junit.Test)7 CountDownLatch (java.util.concurrent.CountDownLatch)6 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)6 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)5 Collection (java.util.Collection)4 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)4 IgniteKernal (org.apache.ignite.internal.IgniteKernal)4 Message (org.apache.ignite.plugin.extensions.communication.Message)4 ArrayList (java.util.ArrayList)3 Map (java.util.Map)3 IgniteException (org.apache.ignite.IgniteException)3 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)3