Search in sources :

Example 6 with GridNioSession

use of org.apache.ignite.internal.util.nio.GridNioSession in project ignite by apache.

the class GridTcpCommunicationSpiRecoverySelfTest method testBlockRead2.

/**
     * @throws Exception If failed.
     */
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));
        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 is 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());
            } 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) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Example 7 with GridNioSession

use of org.apache.ignite.internal.util.nio.GridNioSession in project ignite by apache.

the class GridTcpCommunicationSpiRecoveryAckSelfTest method checkOverflow.

/**
     * @throws Exception If failed.
     */
@SuppressWarnings("unchecked")
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 = U.field(spi1, "nioSrvr");
    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 < 150; 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) IgniteSpiException(org.apache.ignite.spi.IgniteSpiException) GridNioServer(org.apache.ignite.internal.util.nio.GridNioServer)

Example 8 with GridNioSession

use of org.apache.ignite.internal.util.nio.GridNioSession 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.
     */
@SuppressWarnings("unchecked")
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));
            }
            expMsgs += msgPerIter;
            final long totAcked0 = totAcked;
            for (TcpCommunicationSpi spi : spis) {
                GridNioServer srv = U.field(spi, "nioSrvr");
                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());
            }
            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) GridNioServer(org.apache.ignite.internal.util.nio.GridNioServer) GridNioRecoveryDescriptor(org.apache.ignite.internal.util.nio.GridNioRecoveryDescriptor)

Example 9 with GridNioSession

use of org.apache.ignite.internal.util.nio.GridNioSession in project ignite by apache.

the class TcpRestParserSelfTest method testIncorrectPackets.

/**
     * @throws Exception If failed.
     */
public void testIncorrectPackets() throws Exception {
    final GridNioSession ses = new MockNioSession();
    final GridTcpRestParser parser = new GridTcpRestParser(false);
    final byte[] opaque = new byte[] { 0x01, 0x02, 0x03, (byte) 0xFF };
    final String key = "key";
    final String val = "value";
    GridTestUtils.assertThrows(log(), new Callable<Object>() {

        @Nullable
        @Override
        public Object call() throws Exception {
            parser.decode(ses, rawPacket((byte) 0x01, (byte) 0x01, opaque, key.getBytes(), val.getBytes(), EXTRAS));
            return null;
        }
    }, IOException.class, null);
    GridTestUtils.assertThrows(log(), new Callable<Object>() {

        @Nullable
        @Override
        public Object call() throws Exception {
            parser.decode(ses, rawPacket(MEMCACHE_REQ_FLAG, (byte) 0x01, opaque, key.getBytes(), val.getBytes(), null));
            return null;
        }
    }, IOException.class, null);
    GridTestUtils.assertThrows(log(), new Callable<Object>() {

        @Nullable
        @Override
        public Object call() throws Exception {
            ByteBuffer fake = ByteBuffer.allocate(21);
            fake.put(IGNITE_REQ_FLAG);
            fake.put(U.intToBytes(-5));
            fake.put(U.longToBytes(0));
            fake.put(U.longToBytes(0));
            fake.flip();
            parser.decode(ses, fake);
            return null;
        }
    }, IOException.class, null);
}
Also used : GridNioSession(org.apache.ignite.internal.util.nio.GridNioSession) ByteBuffer(java.nio.ByteBuffer) Nullable(org.jetbrains.annotations.Nullable) IOException(java.io.IOException)

Example 10 with GridNioSession

use of org.apache.ignite.internal.util.nio.GridNioSession in project ignite by apache.

the class TcpRestParserSelfTest method testParseClientHandshake.

/**
     * Tests correct parsing of client handshake packets.
     *
     * @throws Exception If failed.
     */
public void testParseClientHandshake() throws Exception {
    for (int splitPos = 1; splitPos < 5; splitPos++) {
        log.info("Checking split position: " + splitPos);
        ByteBuffer tmp = clientHandshakePacket();
        ByteBuffer[] split = split(tmp, splitPos);
        GridNioSession ses = new MockNioSession();
        ses.addMeta(MARSHALLER.ordinal(), new GridClientOptimizedMarshaller());
        GridTcpRestParser parser = new GridTcpRestParser(false);
        Collection<GridClientMessage> lst = new ArrayList<>(1);
        for (ByteBuffer buf : split) {
            GridClientMessage r;
            while (buf.hasRemaining() && (r = parser.decode(ses, buf)) != null) lst.add(r);
            assertTrue("Parser has left unparsed bytes.", buf.remaining() == 0);
        }
        assertEquals(1, lst.size());
        GridClientHandshakeRequest req = (GridClientHandshakeRequest) F.first(lst);
        assertNotNull(req);
        assertEquals(U.bytesToShort(new byte[] { 5, 0 }, 0), req.version());
    }
}
Also used : GridClientHandshakeRequest(org.apache.ignite.internal.processors.rest.client.message.GridClientHandshakeRequest) GridNioSession(org.apache.ignite.internal.util.nio.GridNioSession) GridClientOptimizedMarshaller(org.apache.ignite.internal.client.marshaller.optimized.GridClientOptimizedMarshaller) ArrayList(java.util.ArrayList) ByteBuffer(java.nio.ByteBuffer) GridClientMessage(org.apache.ignite.internal.processors.rest.client.message.GridClientMessage)

Aggregations

GridNioSession (org.apache.ignite.internal.util.nio.GridNioSession)26 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)11 GridAbsPredicate (org.apache.ignite.internal.util.lang.GridAbsPredicate)11 ByteBuffer (java.nio.ByteBuffer)8 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)8 ClusterNode (org.apache.ignite.cluster.ClusterNode)8 GridNioServer (org.apache.ignite.internal.util.nio.GridNioServer)8 GridTestMessage (org.apache.ignite.spi.communication.GridTestMessage)8 IgniteException (org.apache.ignite.IgniteException)7 IgniteSpiException (org.apache.ignite.spi.IgniteSpiException)7 Nullable (org.jetbrains.annotations.Nullable)5 GridClientOptimizedMarshaller (org.apache.ignite.internal.client.marshaller.optimized.GridClientOptimizedMarshaller)4 GridClientMessage (org.apache.ignite.internal.processors.rest.client.message.GridClientMessage)4 IpcEndpoint (org.apache.ignite.internal.util.ipc.IpcEndpoint)4 IpcSharedMemoryServerEndpoint (org.apache.ignite.internal.util.ipc.shmem.IpcSharedMemoryServerEndpoint)4 GridNioRecoveryDescriptor (org.apache.ignite.internal.util.nio.GridNioRecoveryDescriptor)4 IOException (java.io.IOException)3 BindException (java.net.BindException)3 ArrayList (java.util.ArrayList)3 Collection (java.util.Collection)3