Search in sources :

Example 16 with GridTestMessage

use of org.apache.ignite.spi.communication.GridTestMessage in project ignite by apache.

the class IgniteTcpCommunicationRecoveryAckClosureSelfTest method checkAck.

/**
 * @param ackCnt Recovery acknowledgement count.
 * @param idleTimeout Idle connection timeout.
 * @param msgPerIter Messages per iteration.
 * @throws Exception If failed.
 */
private void checkAck(int ackCnt, int idleTimeout, int msgPerIter) throws Exception {
    createSpis(ackCnt, idleTimeout, TcpCommunicationSpi.DFLT_MSG_QUEUE_LIMIT);
    try {
        TcpCommunicationSpi spi0 = spis.get(0);
        TcpCommunicationSpi spi1 = spis.get(1);
        ClusterNode node0 = nodes.get(0);
        ClusterNode node1 = nodes.get(1);
        int msgId = 0;
        int expMsgs = 0;
        long totAcked = 0;
        for (int i = 0; i < 5; i++) {
            info("Iteration: " + i);
            final AtomicInteger ackMsgs = new AtomicInteger(0);
            IgniteInClosure<IgniteException> ackC = new CI1<IgniteException>() {

                @Override
                public void apply(IgniteException o) {
                    assert o == null;
                    ackMsgs.incrementAndGet();
                }
            };
            for (int j = 0; j < msgPerIter; j++) {
                spi0.sendMessage(node1, new GridTestMessage(node0.id(), ++msgId, 0), ackC);
                spi1.sendMessage(node0, new GridTestMessage(node1.id(), ++msgId, 0), ackC);
                if (j == 0) {
                    final TestListener lsnr0 = (TestListener) spi0.getListener();
                    final TestListener lsnr1 = (TestListener) spi1.getListener();
                    GridTestUtils.waitForCondition(new GridAbsPredicate() {

                        @Override
                        public boolean apply() {
                            return lsnr0.rcvCnt.get() >= 1 && lsnr1.rcvCnt.get() >= 1;
                        }
                    }, 1000);
                }
            }
            expMsgs += msgPerIter;
            final long totAcked0 = totAcked;
            for (TcpCommunicationSpi spi : spis) {
                GridNioServer<?> srv = ((GridNioServerWrapper) U.field(spi, "nioSrvWrapper")).nio();
                Collection<? extends GridNioSession> sessions = GridTestUtils.getFieldValue(srv, "sessions");
                assertFalse(sessions.isEmpty());
                boolean found = false;
                for (GridNioSession ses : sessions) {
                    final GridNioRecoveryDescriptor recoveryDesc = ses.outRecoveryDescriptor();
                    if (recoveryDesc != null) {
                        found = true;
                        GridTestUtils.waitForCondition(new GridAbsPredicate() {

                            @Override
                            public boolean apply() {
                                long acked = GridTestUtils.getFieldValue(recoveryDesc, "acked");
                                return acked > totAcked0;
                            }
                        }, 5000);
                        GridTestUtils.waitForCondition(new GridAbsPredicate() {

                            @Override
                            public boolean apply() {
                                return recoveryDesc.messagesRequests().isEmpty();
                            }
                        }, 10_000);
                        assertEquals("Unexpected messages: " + recoveryDesc.messagesRequests(), 0, recoveryDesc.messagesRequests().size());
                        break;
                    }
                }
                assertTrue(found);
            }
            final int expMsgs0 = expMsgs;
            for (TcpCommunicationSpi spi : spis) {
                final TestListener lsnr = (TestListener) spi.getListener();
                GridTestUtils.waitForCondition(new GridAbsPredicate() {

                    @Override
                    public boolean apply() {
                        return lsnr.rcvCnt.get() >= expMsgs0;
                    }
                }, 5000);
                assertEquals(expMsgs, lsnr.rcvCnt.get());
            }
            assertEquals(msgPerIter * 2, ackMsgs.get());
            totAcked += msgPerIter;
        }
    } finally {
        stopSpis();
    }
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) GridTestMessage(org.apache.ignite.spi.communication.GridTestMessage) GridNioSession(org.apache.ignite.internal.util.nio.GridNioSession) GridAbsPredicate(org.apache.ignite.internal.util.lang.GridAbsPredicate) CI1(org.apache.ignite.internal.util.typedef.CI1) GridNioServerWrapper(org.apache.ignite.spi.communication.tcp.internal.GridNioServerWrapper) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgniteException(org.apache.ignite.IgniteException) GridNioRecoveryDescriptor(org.apache.ignite.internal.util.nio.GridNioRecoveryDescriptor)

Example 17 with GridTestMessage

use of org.apache.ignite.spi.communication.GridTestMessage in project ignite by apache.

the class GridTcpCommunicationSpiMultithreadedSelfTest method testPassThroughPerformance.

/**
 * @throws Exception If failed.
 */
@Test
public void testPassThroughPerformance() throws Exception {
    reject = true;
    info(">>> Starting pass through performance test. <<<");
    assertEquals("Invalid listener count", getSpiCount(), lsnrs.size());
    final AtomicInteger cntr = new AtomicInteger();
    final int msgCnt = 5000;
    long start = System.currentTimeMillis();
    IgniteInternalFuture<?> fut = multithreadedAsync(() -> {
        try {
            ClusterNode from = nodes.get(0);
            ClusterNode to = nodes.get(1);
            CommunicationSpi<Message> spi = spis.get(from.id());
            while (cntr.getAndIncrement() < msgCnt) {
                GridTestMessage msg = new GridTestMessage(from.id(), msgId.getAndIncrement(), 0);
                msg.payload(new byte[10 * 1024]);
                spi.sendMessage(to, msg);
            }
        } catch (IgniteException e) {
            fail("Unable to send message: " + e.getMessage());
        }
    }, 5, "message-sender");
    fut.get();
    info(">>> Sent all messages in " + (System.currentTimeMillis() - start) + " milliseconds");
    assertEquals("Invalid count of messages was sent", msgCnt, msgId.get());
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) GridTestMessage(org.apache.ignite.spi.communication.GridTestMessage) GridTestMessage(org.apache.ignite.spi.communication.GridTestMessage) Message(org.apache.ignite.plugin.extensions.communication.Message) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgniteException(org.apache.ignite.IgniteException) GridSpiAbstractTest(org.apache.ignite.testframework.junits.spi.GridSpiAbstractTest) Test(org.junit.Test)

Example 18 with GridTestMessage

use of org.apache.ignite.spi.communication.GridTestMessage in project ignite by apache.

the class GridTcpCommunicationSpiMultithreadedSelfTest method beforeTestsStarted.

/**
 * {@inheritDoc}
 */
@Override
protected void beforeTestsStarted() throws Exception {
    spis.clear();
    nodes.clear();
    spiRsrcs.clear();
    lsnrs.clear();
    Map<ClusterNode, GridSpiTestContext> ctxs = new HashMap<>();
    timeoutProcessor = new GridTimeoutProcessor(new GridTestKernalContext(log));
    timeoutProcessor.start();
    timeoutProcessor.onKernalStart(true);
    for (int i = 0; i < getSpiCount(); i++) {
        CommunicationSpi<Message> spi = newCommunicationSpi();
        GridTestUtils.setFieldValue(spi, IgniteSpiAdapter.class, "igniteInstanceName", "grid-" + i);
        IgniteTestResources rsrcs = new IgniteTestResources();
        GridTestNode node = new GridTestNode(rsrcs.getNodeId());
        node.order(i);
        GridSpiTestContext ctx = initSpiContext();
        MessageFactoryProvider testMsgFactory = factory -> factory.register(GridTestMessage.DIRECT_TYPE, GridTestMessage::new);
        ctx.messageFactory(new IgniteMessageFactoryImpl(new MessageFactory[] { new GridIoMessageFactory(), testMsgFactory }));
        ctx.timeoutProcessor(timeoutProcessor);
        ctx.setLocalNode(node);
        info(">>> Initialized context: nodeId=" + ctx.localNode().id());
        spiRsrcs.add(rsrcs);
        rsrcs.inject(spi);
        MessageListener lsnr = new MessageListener(rsrcs.getNodeId());
        spi.setListener(lsnr);
        lsnrs.put(rsrcs.getNodeId(), lsnr);
        info("Lsnrs: " + lsnrs);
        spi.spiStart(getTestIgniteInstanceName() + (i + 1));
        nodes.add(node);
        node.setAttributes(spi.getNodeAttributes());
        node.setAttribute(ATTR_MACS, F.concat(U.allLocalMACs(), ", "));
        spis.put(rsrcs.getNodeId(), spi);
        spi.onContextInitialized(ctx);
        ctxs.put(node, ctx);
    }
    // For each context set remote nodes.
    for (Entry<ClusterNode, GridSpiTestContext> e : ctxs.entrySet()) {
        for (ClusterNode n : nodes) {
            if (!n.equals(e.getKey()))
                e.getValue().remoteNodes().add(n);
        }
    }
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) GridNioSession(org.apache.ignite.internal.util.nio.GridNioSession) CommunicationListener(org.apache.ignite.spi.communication.CommunicationListener) IgniteTestResources(org.apache.ignite.testframework.junits.IgniteTestResources) ATTR_MACS(org.apache.ignite.internal.IgniteNodeAttributes.ATTR_MACS) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) U(org.apache.ignite.internal.util.typedef.internal.U) HashMap(java.util.HashMap) Random(java.util.Random) GridTimeoutProcessor(org.apache.ignite.internal.processors.timeout.GridTimeoutProcessor) Deque(java.util.Deque) MessageFactoryProvider(org.apache.ignite.plugin.extensions.communication.MessageFactoryProvider) ArrayList(java.util.ArrayList) ConcurrentMap(java.util.concurrent.ConcurrentMap) ClusterNode(org.apache.ignite.cluster.ClusterNode) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) GridTestMessage(org.apache.ignite.spi.communication.GridTestMessage) Map(java.util.Map) IgniteMessageFactoryImpl(org.apache.ignite.internal.managers.communication.IgniteMessageFactoryImpl) IgniteInterruptedCheckedException(org.apache.ignite.internal.IgniteInterruptedCheckedException) GridNioServerWrapper(org.apache.ignite.spi.communication.tcp.internal.GridNioServerWrapper) MessageFactory(org.apache.ignite.plugin.extensions.communication.MessageFactory) F(org.apache.ignite.internal.util.typedef.F) CyclicBarrier(java.util.concurrent.CyclicBarrier) GridNioServer(org.apache.ignite.internal.util.nio.GridNioServer) Collection(java.util.Collection) IgniteException(org.apache.ignite.IgniteException) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) IgniteRunnable(org.apache.ignite.lang.IgniteRunnable) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) GridSpiAbstractTest(org.apache.ignite.testframework.junits.spi.GridSpiAbstractTest) Test(org.junit.Test) UUID(java.util.UUID) ConcurrentLinkedDeque(java.util.concurrent.ConcurrentLinkedDeque) GridTestNode(org.apache.ignite.testframework.GridTestNode) GridTestUtils(org.apache.ignite.testframework.GridTestUtils) AtomicLong(java.util.concurrent.atomic.AtomicLong) CommunicationSpi(org.apache.ignite.spi.communication.CommunicationSpi) GridTestKernalContext(org.apache.ignite.testframework.junits.GridTestKernalContext) List(java.util.List) IgniteSpiAdapter(org.apache.ignite.spi.IgniteSpiAdapter) GridSpiTestContext(org.apache.ignite.testframework.GridSpiTestContext) Entry(java.util.Map.Entry) GridIoMessageFactory(org.apache.ignite.internal.managers.communication.GridIoMessageFactory) Message(org.apache.ignite.plugin.extensions.communication.Message) GridCommunicationClient(org.apache.ignite.internal.util.nio.GridCommunicationClient) GridNioRecoveryDescriptor(org.apache.ignite.internal.util.nio.GridNioRecoveryDescriptor) GridSpiTestContext(org.apache.ignite.testframework.GridSpiTestContext) GridTestMessage(org.apache.ignite.spi.communication.GridTestMessage) GridTimeoutProcessor(org.apache.ignite.internal.processors.timeout.GridTimeoutProcessor) GridTestMessage(org.apache.ignite.spi.communication.GridTestMessage) Message(org.apache.ignite.plugin.extensions.communication.Message) MessageFactory(org.apache.ignite.plugin.extensions.communication.MessageFactory) GridIoMessageFactory(org.apache.ignite.internal.managers.communication.GridIoMessageFactory) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) GridTestKernalContext(org.apache.ignite.testframework.junits.GridTestKernalContext) GridTestNode(org.apache.ignite.testframework.GridTestNode) IgniteMessageFactoryImpl(org.apache.ignite.internal.managers.communication.IgniteMessageFactoryImpl) IgniteTestResources(org.apache.ignite.testframework.junits.IgniteTestResources) MessageFactoryProvider(org.apache.ignite.plugin.extensions.communication.MessageFactoryProvider) GridIoMessageFactory(org.apache.ignite.internal.managers.communication.GridIoMessageFactory)

Example 19 with GridTestMessage

use of org.apache.ignite.spi.communication.GridTestMessage in project ignite by apache.

the class GridTcpCommunicationSpiMultithreadedSelfTest method testSendToRandomNodesMultithreaded.

/**
 * @throws Exception If failed.
 */
@Test
public void testSendToRandomNodesMultithreaded() throws Exception {
    info(">>> Starting send to random nodes multithreaded test. <<<");
    reject = false;
    assertEquals("Invalid listener count", getSpiCount(), lsnrs.size());
    final ConcurrentMap<UUID, Deque<GridTestMessage>> msgs = new ConcurrentHashMap<>();
    final int iterationCnt = 5000;
    long start = System.currentTimeMillis();
    IgniteInternalFuture<?> fut = multithreadedAsync(new Runnable() {

        /**
         * Randomizer.
         */
        private Random rnd = new Random();

        @Override
        public void run() {
            try {
                for (int i = 0; i < iterationCnt; i++) {
                    ClusterNode from = randomNode(rnd);
                    ClusterNode to = randomNode(rnd);
                    GridTestMessage msg = new GridTestMessage(from.id(), msgId.getAndIncrement(), 0);
                    spis.get(from.id()).sendMessage(to, msg);
                    Deque<GridTestMessage> queue = msgs.get(to.id());
                    if (queue == null) {
                        Deque<GridTestMessage> old = msgs.putIfAbsent(to.id(), queue = new ConcurrentLinkedDeque<>());
                        if (old != null)
                            queue = old;
                    }
                    queue.offer(msg);
                }
            } catch (IgniteException e) {
                log().error("Unable to send message.", e);
                fail("Unable to send message: " + e.getMessage());
            }
        }
    }, getSpiCount() * 3, "message-sender");
    fut.get();
    info(">>> Sent all messages in " + (System.currentTimeMillis() - start) + " milliseconds");
    assertEquals("Invalid count of messages was sent", iterationCnt * getSpiCount() * 3, msgId.get());
    U.sleep(IDLE_CONN_TIMEOUT * 2);
    // Now validate all sent and received messages.
    for (Entry<UUID, Deque<GridTestMessage>> e : msgs.entrySet()) {
        UUID to = e.getKey();
        Deque<GridTestMessage> sent = e.getValue();
        MessageListener lsnr = lsnrs.get(to);
        Deque<GridTestMessage> rcvd = lsnr.receivedMsgs();
        info(">>> Node " + to + " received " + lsnr.remoteMessageCount() + " remote messages of " + rcvd.size() + " total");
        for (int i = 0; i < 3 && sent.size() != rcvd.size(); i++) {
            info("Check failed for node [node=" + to + ", sent=" + sent.size() + ", rcvd=" + rcvd.size() + ']');
            U.sleep(2000);
        }
        assertEquals("Sent and received messages count mismatch.", sent.size(), rcvd.size());
        assertTrue("Listener did not receive some messages: " + lsnr, rcvd.containsAll(sent));
        assertTrue("Listener received extra messages: " + lsnr, sent.containsAll(rcvd));
    }
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) GridTestMessage(org.apache.ignite.spi.communication.GridTestMessage) Deque(java.util.Deque) ConcurrentLinkedDeque(java.util.concurrent.ConcurrentLinkedDeque) Random(java.util.Random) IgniteException(org.apache.ignite.IgniteException) IgniteRunnable(org.apache.ignite.lang.IgniteRunnable) UUID(java.util.UUID) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) GridSpiAbstractTest(org.apache.ignite.testframework.junits.spi.GridSpiAbstractTest) Test(org.junit.Test)

Example 20 with GridTestMessage

use of org.apache.ignite.spi.communication.GridTestMessage in project ignite by apache.

the class GridTcpCommunicationSpiRecoverySelfTest method testBlockRead1.

/**
 * @throws Exception If failed.
 */
@Test
public void testBlockRead1() throws Exception {
    createSpis();
    try {
        final TcpCommunicationSpi spi0 = spis.get(0);
        final TcpCommunicationSpi spi1 = spis.get(1);
        final TestListener lsnr1 = (TestListener) spi1.getListener();
        final ClusterNode node0 = nodes.get(0);
        final ClusterNode node1 = nodes.get(1);
        final AtomicInteger msgId = new AtomicInteger();
        // Send message to establish connection.
        spi0.sendMessage(node1, new GridTestMessage(node0.id(), msgId.incrementAndGet(), 0));
        GridTestUtils.waitForCondition(new GridAbsPredicate() {

            @Override
            public boolean apply() {
                return lsnr1.rcvCnt.get() >= 1;
            }
        }, 1000);
        final AtomicInteger sentCnt = new AtomicInteger(1);
        int errCnt = 0;
        for (int i = 0; i < ITERS; i++) {
            log.info("Iteration: " + i);
            try {
                final GridNioSession ses0 = communicationSession(spi0, false);
                final GridNioSession ses1 = communicationSession(spi1, true);
                ses1.pauseReads().get();
                IgniteInternalFuture<?> sndFut = GridTestUtils.runAsync(() -> {
                    for (int i1 = 0; i1 < 6000; i1++) {
                        spi0.sendMessage(node1, new GridTestMessage(node0.id(), msgId.incrementAndGet(), 0));
                        sentCnt.incrementAndGet();
                    }
                    return null;
                });
                // Wait when session is closed because of write timeout.
                GridTestUtils.waitForCondition(new GridAbsPredicate() {

                    @Override
                    public boolean apply() {
                        return ses0.closeTime() != 0;
                    }
                }, awaitForSocketWriteTimeout());
                assertTrue("Failed to wait for session close", ses0.closeTime() != 0);
                try {
                    ses1.resumeReads().get();
                } catch (IgniteCheckedException ignore) {
                // Can fail is ses1 was closed.
                }
                for (int j = 0; j < 100; j++) {
                    spi0.sendMessage(node1, new GridTestMessage(node0.id(), msgId.incrementAndGet(), 0));
                    sentCnt.incrementAndGet();
                }
                sndFut.get();
                final int expMsgs = sentCnt.get();
                GridTestUtils.waitForCondition(new GridAbsPredicate() {

                    @Override
                    public boolean apply() {
                        return lsnr1.rcvCnt.get() >= expMsgs;
                    }
                }, 60_000);
                assertEquals(expMsgs, lsnr1.rcvCnt.get());
                assertTrue(waitForSessionsCount(spi1, 1));
            } catch (IgniteCheckedException e) {
                if (e.hasCause(BindException.class)) {
                    errCnt++;
                    if (errCnt > 3) {
                        log.warning("Got exception > 3 times, test fails.");
                        throw e;
                    }
                    if (i < ITERS - 1) {
                        info("Got exception caused by BindException, will retry after delay: " + e);
                        U.sleep(10_000);
                    } else
                        info("Got exception caused by BindException, will ignore: " + e);
                } else {
                    log.warning("Unexpected exception: " + e, e);
                    throw e;
                }
            }
        }
    } finally {
        stopSpis();
    }
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) GridTestMessage(org.apache.ignite.spi.communication.GridTestMessage) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridNioSession(org.apache.ignite.internal.util.nio.GridNioSession) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) GridAbsPredicate(org.apache.ignite.internal.util.lang.GridAbsPredicate) BindException(java.net.BindException) GridSpiTest(org.apache.ignite.testframework.junits.spi.GridSpiTest) GridSpiAbstractTest(org.apache.ignite.testframework.junits.spi.GridSpiAbstractTest) Test(org.junit.Test)

Aggregations

GridTestMessage (org.apache.ignite.spi.communication.GridTestMessage)22 ClusterNode (org.apache.ignite.cluster.ClusterNode)17 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)12 Test (org.junit.Test)12 GridAbsPredicate (org.apache.ignite.internal.util.lang.GridAbsPredicate)10 GridNioSession (org.apache.ignite.internal.util.nio.GridNioSession)10 IgniteException (org.apache.ignite.IgniteException)9 GridSpiAbstractTest (org.apache.ignite.testframework.junits.spi.GridSpiAbstractTest)9 Message (org.apache.ignite.plugin.extensions.communication.Message)8 GridNioServerWrapper (org.apache.ignite.spi.communication.tcp.internal.GridNioServerWrapper)8 IgniteRunnable (org.apache.ignite.lang.IgniteRunnable)7 GridSpiTestContext (org.apache.ignite.testframework.GridSpiTestContext)7 GridTestNode (org.apache.ignite.testframework.GridTestNode)7 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)6 GridIoMessageFactory (org.apache.ignite.internal.managers.communication.GridIoMessageFactory)6 IgniteMessageFactoryImpl (org.apache.ignite.internal.managers.communication.IgniteMessageFactoryImpl)6 MessageFactory (org.apache.ignite.plugin.extensions.communication.MessageFactory)6 MessageFactoryProvider (org.apache.ignite.plugin.extensions.communication.MessageFactoryProvider)6 IgniteTestResources (org.apache.ignite.testframework.junits.IgniteTestResources)6 GridSpiTest (org.apache.ignite.testframework.junits.spi.GridSpiTest)6