Search in sources :

Example 6 with GridTestMessage

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

the class GridTcpCommunicationSpiRecoverySelfTest method testBlockRead2.

/**
 * @throws Exception If failed.
 */
@Test
public void testBlockRead2() throws Exception {
    createSpis();
    try {
        final TcpCommunicationSpi spi0 = spis.get(0);
        final TcpCommunicationSpi spi1 = spis.get(1);
        final TestListener lsnr0 = (TestListener) spi0.getListener();
        final TestListener lsnr1 = (TestListener) spi1.getListener();
        final ClusterNode node0 = nodes.get(0);
        final ClusterNode node1 = nodes.get(1);
        final AtomicInteger msgId = new AtomicInteger();
        final AtomicInteger expCnt0 = new AtomicInteger();
        final AtomicInteger expCnt1 = 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);
        expCnt1.incrementAndGet();
        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(new Callable<Void>() {

                    @Override
                    public Void call() throws Exception {
                        for (int i = 0; i < 6000; i++) {
                            spi0.sendMessage(node1, new GridTestMessage(node0.id(), msgId.incrementAndGet(), 0));
                            expCnt1.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 if ses1 was closed.
                }
                // Wait when session is closed, then try to open new connection from node1.
                GridTestUtils.waitForCondition(new GridAbsPredicate() {

                    @Override
                    public boolean apply() {
                        return ses1.closeTime() != 0;
                    }
                }, awaitForSocketWriteTimeout());
                assertTrue("Failed to wait for session close", ses1.closeTime() != 0);
                for (int j = 0; j < 100; j++) {
                    spi1.sendMessage(node0, new GridTestMessage(node1.id(), msgId.incrementAndGet(), 0));
                    expCnt0.incrementAndGet();
                }
                sndFut.get();
                final int expMsgs0 = expCnt0.get();
                final int expMsgs1 = expCnt1.get();
                GridTestUtils.waitForCondition(new GridAbsPredicate() {

                    @Override
                    public boolean apply() {
                        return lsnr0.rcvCnt.get() >= expMsgs0 && lsnr1.rcvCnt.get() >= expMsgs1;
                    }
                }, 60_000);
                assertEquals(expMsgs0, lsnr0.rcvCnt.get());
                assertEquals(expMsgs1, lsnr1.rcvCnt.get());
                if (spi1.isUsePairedConnections())
                    assertTrue(waitForSessionsCount(spi1, 2));
            } 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) GridNioSession(org.apache.ignite.internal.util.nio.GridNioSession) GridAbsPredicate(org.apache.ignite.internal.util.lang.GridAbsPredicate) BindException(java.net.BindException) IgniteSpiException(org.apache.ignite.spi.IgniteSpiException) BindException(java.net.BindException) IgniteInterruptedCheckedException(org.apache.ignite.internal.IgniteInterruptedCheckedException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) GridSpiTest(org.apache.ignite.testframework.junits.spi.GridSpiTest) GridSpiAbstractTest(org.apache.ignite.testframework.junits.spi.GridSpiAbstractTest) Test(org.junit.Test)

Example 7 with GridTestMessage

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

the class GridTcpCommunicationSpiRecoverySelfTest method checkBlockListener.

/**
 * @throws Exception If failed.
 */
private void checkBlockListener() throws Exception {
    TcpCommunicationSpi spi0 = spis.get(0);
    TcpCommunicationSpi spi1 = spis.get(1);
    final TestListener lsnr0 = (TestListener) spi0.getListener();
    final TestListener lsnr1 = (TestListener) spi1.getListener();
    ClusterNode node0 = nodes.get(0);
    ClusterNode node1 = nodes.get(1);
    lsnr1.block();
    int msgId = 0;
    for (int j = 0; j < 10; j++) {
        spi0.sendMessage(node1, new GridTestMessage(node0.id(), ++msgId, 0));
        spi1.sendMessage(node0, new GridTestMessage(node1.id(), ++msgId, 0));
    }
    lsnr1.blockLatch.await();
    lsnr1.unblock();
    Thread.sleep(500);
    int errCnt = 0;
    int msgs = 0;
    while (true) {
        try {
            int id = msgId + 1;
            spi0.sendMessage(node1, new GridTestMessage(node0.id(), id, 0));
            msgId++;
            msgs++;
            if (msgs == 10)
                break;
        } catch (IgniteSpiException e) {
            errCnt++;
            if (errCnt > 10)
                fail("Failed to send message: " + e);
        }
    }
    for (int j = 0; j < 10; j++) spi1.sendMessage(node0, new GridTestMessage(node1.id(), ++msgId, 0));
    final int expMsgs = 20;
    GridTestUtils.waitForCondition(new GridAbsPredicate() {

        @Override
        public boolean apply() {
            return lsnr0.rcvCnt.get() >= expMsgs && lsnr1.rcvCnt.get() >= expMsgs;
        }
    }, awaitForSocketWriteTimeout());
    assertEquals(expMsgs, lsnr0.rcvCnt.get());
    assertEquals(expMsgs, lsnr1.rcvCnt.get());
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) GridTestMessage(org.apache.ignite.spi.communication.GridTestMessage) GridAbsPredicate(org.apache.ignite.internal.util.lang.GridAbsPredicate) IgniteSpiException(org.apache.ignite.spi.IgniteSpiException)

Example 8 with GridTestMessage

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

the class GridTcpCommunicationSpiConfigSelfTest method testSendToNonInitializedTcpCommSpi.

/**
 * Verifies that TcpCommunicationSpi starts messaging protocol only when fully initialized.
 *
 * @see <a href="https://issues.apache.org/jira/browse/IGNITE-12982">IGNITE-12982</a>
 *
 * @throws Exception If failed.
 */
@Test
@WithSystemProperty(key = "IGNITE_SKIP_CONFIGURATION_CONSISTENCY_CHECK", value = "true")
public void testSendToNonInitializedTcpCommSpi() throws Exception {
    ListeningTestLogger listeningLogger = new ListeningTestLogger(log);
    LogListener npeLsnr = LogListener.matches("NullPointerException").andMatches("InboundConnectionHandler.onMessageSent").build();
    listeningLogger.registerListener(npeLsnr);
    GridTestNode sendingNode = new GridTestNode();
    sendingNode.order(0);
    GridSpiTestContext sendingCtx = initSpiContext();
    TcpCommunicationSpi sendingSpi = initializeSpi(sendingCtx, sendingNode, listeningLogger, false);
    spisToStop.add(sendingSpi);
    sendingSpi.onContextInitialized(sendingCtx);
    GridTestNode receiverNode = new GridTestNode();
    receiverNode.order(1);
    GridSpiTestContext receiverCtx = initSpiContext();
    /*
         * This is a dirty hack to intervene into TcpCommunicationSpi#onContextInitialized0 method
         * and add a delay before injecting metrics listener into its clients (like InboundConnectionHandler).
         * The purpose of the delay is to make race between sending a message and initializing TcpCommSpi visible.
         *
         * This solution heavily depends on current code structure of onContextInitialized0 method.
         * If any modifications are made to it, this logic could break and the test starts failing.
         *
         * In that case try to rewrite the test or delete it as this race is really hard to test.
         */
    receiverCtx.metricsRegistryProducer((name) -> {
        try {
            Thread.sleep(100);
        } catch (Exception ignored) {
        // No-op.
        }
        return new MetricRegistry(name, null, null, new NullLogger());
    });
    TcpCommunicationSpi receiverSpi = initializeSpi(receiverCtx, receiverNode, listeningLogger, true);
    spisToStop.add(receiverSpi);
    receiverCtx.remoteNodes().add(sendingNode);
    sendingCtx.remoteNodes().add(receiverNode);
    IgniteInternalFuture sendFut = GridTestUtils.runAsync(() -> {
        Message msg = new GridTestMessage(sendingNode.id(), 0, 0);
        sendingSpi.sendMessage(receiverNode, msg);
    });
    IgniteInternalFuture initFut = GridTestUtils.runAsync(() -> {
        try {
            receiverSpi.onContextInitialized(receiverCtx);
        } catch (Exception ignored) {
        // No-op.
        }
    });
    assertFalse("Check test logs, NPE was found", GridTestUtils.waitForCondition(npeLsnr::check, 3_000));
    initFut.get();
    sendFut.get();
}
Also used : GridSpiTestContext(org.apache.ignite.testframework.GridSpiTestContext) GridTestMessage(org.apache.ignite.spi.communication.GridTestMessage) NullLogger(org.apache.ignite.logger.NullLogger) LogListener(org.apache.ignite.testframework.LogListener) GridTestMessage(org.apache.ignite.spi.communication.GridTestMessage) Message(org.apache.ignite.plugin.extensions.communication.Message) MetricRegistry(org.apache.ignite.internal.processors.metric.MetricRegistry) ListeningTestLogger(org.apache.ignite.testframework.ListeningTestLogger) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) GridTestNode(org.apache.ignite.testframework.GridTestNode) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridSpiTest(org.apache.ignite.testframework.junits.spi.GridSpiTest) GridAbstractTest(org.apache.ignite.testframework.junits.GridAbstractTest) Test(org.junit.Test) GridSpiAbstractConfigTest(org.apache.ignite.testframework.junits.spi.GridSpiAbstractConfigTest) WithSystemProperty(org.apache.ignite.testframework.junits.WithSystemProperty)

Example 9 with GridTestMessage

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

the class GridTcpCommunicationSpiRecoveryAckSelfTest method checkOverflow.

/**
 * @throws Exception If failed.
 */
private void checkOverflow() throws Exception {
    TcpCommunicationSpi spi0 = spis.get(0);
    TcpCommunicationSpi spi1 = spis.get(1);
    ClusterNode node0 = nodes.get(0);
    ClusterNode node1 = nodes.get(1);
    final GridNioServer<?> srv1 = ((GridNioServerWrapper) U.field(spi1, "nioSrvWrapper")).nio();
    int msgId = 0;
    // Send message to establish connection.
    spi0.sendMessage(node1, new GridTestMessage(node0.id(), ++msgId, 0));
    // Prevent node1 from send
    GridTestUtils.setFieldValue(srv1, "skipWrite", true);
    final GridNioSession ses0 = communicationSession(spi0);
    int sentMsgs = 1;
    for (int i = 0; i < 1280; i++) {
        try {
            spi0.sendMessage(node1, new GridTestMessage(node0.id(), ++msgId, 0));
            sentMsgs++;
        } catch (IgniteSpiException e) {
            log.info("Send error [err=" + e + ", sentMsgs=" + sentMsgs + ']');
            break;
        }
    }
    // Wait when session is closed because of queue overflow.
    GridTestUtils.waitForCondition(new GridAbsPredicate() {

        @Override
        public boolean apply() {
            return ses0.closeTime() != 0;
        }
    }, 5000);
    assertTrue("Failed to wait for session close", ses0.closeTime() != 0);
    GridTestUtils.setFieldValue(srv1, "skipWrite", false);
    for (int i = 0; i < 100; i++) spi0.sendMessage(node1, new GridTestMessage(node0.id(), ++msgId, 0));
    final int expMsgs = sentMsgs + 100;
    final TestListener lsnr = (TestListener) spi1.getListener();
    GridTestUtils.waitForCondition(new GridAbsPredicate() {

        @Override
        public boolean apply() {
            return lsnr.rcvCnt.get() >= expMsgs;
        }
    }, 5000);
    assertEquals(expMsgs, lsnr.rcvCnt.get());
}
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) GridNioServerWrapper(org.apache.ignite.spi.communication.tcp.internal.GridNioServerWrapper) IgniteSpiException(org.apache.ignite.spi.IgniteSpiException)

Example 10 with GridTestMessage

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

the class GridTcpCommunicationSpiRecoveryAckSelfTest 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);
            for (int j = 0; j < msgPerIter; j++) {
                spi0.sendMessage(node1, new GridTestMessage(node0.id(), ++msgId, 0));
                spi1.sendMessage(node0, new GridTestMessage(node1.id(), ++msgId, 0));
            }
            U.sleep(500);
            expMsgs += msgPerIter;
            final long totAcked0 = totAcked;
            for (TcpCommunicationSpi spi : spis) {
                GridNioServer<?> srv = ((GridNioServerWrapper) U.field(spi, "nioSrvWrapper")).nio();
                final Collection<? extends GridNioSession> sessions = GridTestUtils.getFieldValue(srv, "sessions");
                GridTestUtils.waitForCondition(new GridAbsPredicate() {

                    @Override
                    public boolean apply() {
                        return !sessions.isEmpty();
                    }
                }, 5_000);
                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());
            }
            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) GridNioServerWrapper(org.apache.ignite.spi.communication.tcp.internal.GridNioServerWrapper) GridNioRecoveryDescriptor(org.apache.ignite.internal.util.nio.GridNioRecoveryDescriptor)

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