Search in sources :

Example 11 with GridNioSession

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

the class TcpRestParserSelfTest method testCustomMessages.

/**
     * @throws Exception If failed.
     */
public void testCustomMessages() throws Exception {
    GridClientCacheRequest req = new GridClientCacheRequest(CAS);
    req.key("key");
    req.value(1);
    req.value2(2);
    req.clientId(UUID.randomUUID());
    ByteBuffer raw = clientRequestPacket(req);
    GridNioSession ses = new MockNioSession();
    ses.addMeta(MARSHALLER.ordinal(), new GridClientOptimizedMarshaller());
    GridTcpRestParser parser = new GridTcpRestParser(false);
    GridClientMessage msg = parser.decode(ses, raw);
    assertNotNull(msg);
    assertEquals("Parser leaved unparsed bytes", 0, raw.remaining());
    assertTrue(msg instanceof GridClientCacheRequest);
    GridClientCacheRequest res = (GridClientCacheRequest) msg;
    assertEquals("Invalid operation", req.operation(), res.operation());
    assertEquals("Invalid clientId", req.clientId(), res.clientId());
    assertEquals("Invalid key", req.key(), res.key());
    assertEquals("Invalid value 1", req.value(), res.value());
    assertEquals("Invalid value 2", req.value2(), res.value2());
}
Also used : GridNioSession(org.apache.ignite.internal.util.nio.GridNioSession) GridClientOptimizedMarshaller(org.apache.ignite.internal.client.marshaller.optimized.GridClientOptimizedMarshaller) GridClientCacheRequest(org.apache.ignite.internal.processors.rest.client.message.GridClientCacheRequest) ByteBuffer(java.nio.ByteBuffer) GridClientMessage(org.apache.ignite.internal.processors.rest.client.message.GridClientMessage)

Example 12 with GridNioSession

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

the class GridTcpCommunicationSpiRecoveryAckSelfTest method communicationSession.

/**
     * @param spi SPI.
     * @return Session.
     * @throws Exception If failed.
     */
@SuppressWarnings("unchecked")
private GridNioSession communicationSession(TcpCommunicationSpi spi) throws Exception {
    final GridNioServer srv = U.field(spi, "nioSrvr");
    GridTestUtils.waitForCondition(new GridAbsPredicate() {

        @Override
        public boolean apply() {
            Collection<? extends GridNioSession> sessions = GridTestUtils.getFieldValue(srv, "sessions");
            return !sessions.isEmpty();
        }
    }, 5000);
    Collection<? extends GridNioSession> sessions = GridTestUtils.getFieldValue(srv, "sessions");
    assertEquals(1, sessions.size());
    return sessions.iterator().next();
}
Also used : GridNioSession(org.apache.ignite.internal.util.nio.GridNioSession) GridAbsPredicate(org.apache.ignite.internal.util.lang.GridAbsPredicate) Collection(java.util.Collection) GridNioServer(org.apache.ignite.internal.util.nio.GridNioServer)

Example 13 with GridNioSession

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

the class GridTcpCommunicationSpiRecoverySelfTest method testBlockRead1.

/**
     * @throws Exception If failed.
     */
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));
        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(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));
                            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());
            } 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 14 with GridNioSession

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

the class GridTcpCommunicationSpiRecoverySelfTest method communicationSession.

/**
     * @param spi SPI.
     * @param in {@code True} if need find inbound session.
     * @return Session.
     * @throws Exception If failed.
     */
@SuppressWarnings("unchecked")
private GridNioSession communicationSession(TcpCommunicationSpi spi, boolean in) throws Exception {
    final GridNioServer srv = U.field(spi, "nioSrvr");
    GridTestUtils.waitForCondition(new GridAbsPredicate() {

        @Override
        public boolean apply() {
            Collection<? extends GridNioSession> sessions = GridTestUtils.getFieldValue(srv, "sessions");
            return !sessions.isEmpty();
        }
    }, awaitForSocketWriteTimeout());
    Collection<? extends GridNioSession> sessions = GridTestUtils.getFieldValue(srv, "sessions");
    for (GridNioSession ses : sessions) {
        if (in == ses.accepted())
            return ses;
    }
    fail("Failed to find session");
    return null;
}
Also used : GridNioSession(org.apache.ignite.internal.util.nio.GridNioSession) GridAbsPredicate(org.apache.ignite.internal.util.lang.GridAbsPredicate) Collection(java.util.Collection) GridNioServer(org.apache.ignite.internal.util.nio.GridNioServer)

Example 15 with GridNioSession

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

the class HadoopExternalCommunication method createShmemClient.

/**
     * @param desc Process descriptor.
     * @param port Port.
     * @return Client.
     * @throws IgniteCheckedException If failed.
     */
@Nullable
protected HadoopCommunicationClient createShmemClient(HadoopProcessDescriptor desc, int port) throws IgniteCheckedException {
    int attempt = 1;
    int connectAttempts = 1;
    long connTimeout0 = connTimeout;
    while (true) {
        IpcEndpoint clientEndpoint;
        try {
            clientEndpoint = new IpcSharedMemoryClientEndpoint(port, (int) connTimeout, log);
        } catch (IgniteCheckedException e) {
            // Reconnect for the second time, if connection is not established.
            if (connectAttempts < 2 && X.hasCause(e, ConnectException.class)) {
                connectAttempts++;
                continue;
            }
            throw e;
        }
        HadoopCommunicationClient client = null;
        try {
            ShmemWorker worker = new ShmemWorker(clientEndpoint, false);
            shmemWorkers.add(worker);
            GridNioSession ses = worker.session();
            HandshakeFinish fin = new HandshakeFinish();
            // We are in lock, it is safe to get session and attach
            ses.addMeta(HANDSHAKE_FINISH_META, fin);
            client = new HadoopTcpNioCommunicationClient(ses);
            new IgniteThread(worker).start();
            fin.await(connTimeout0);
        } catch (HadoopHandshakeTimeoutException e) {
            if (log.isDebugEnabled())
                log.debug("Handshake timed out (will retry with increased timeout) [timeout=" + connTimeout0 + ", err=" + e.getMessage() + ", client=" + client + ']');
            if (client != null)
                client.forceClose();
            if (attempt == reconCnt || connTimeout0 > maxConnTimeout) {
                if (log.isDebugEnabled())
                    log.debug("Handshake timedout (will stop attempts to perform the handshake) " + "[timeout=" + connTimeout0 + ", maxConnTimeout=" + maxConnTimeout + ", attempt=" + attempt + ", reconCnt=" + reconCnt + ", err=" + e.getMessage() + ", client=" + client + ']');
                throw e;
            } else {
                attempt++;
                connTimeout0 *= 2;
                continue;
            }
        } catch (RuntimeException | Error e) {
            if (log.isDebugEnabled())
                log.debug("Caught exception (will close client) [err=" + e.getMessage() + ", client=" + client + ']');
            if (client != null)
                client.forceClose();
            throw e;
        }
        return client;
    }
}
Also used : IpcEndpoint(org.apache.ignite.internal.util.ipc.IpcEndpoint) GridNioSession(org.apache.ignite.internal.util.nio.GridNioSession) IpcSharedMemoryClientEndpoint(org.apache.ignite.internal.util.ipc.shmem.IpcSharedMemoryClientEndpoint) IpcEndpoint(org.apache.ignite.internal.util.ipc.IpcEndpoint) IpcSharedMemoryClientEndpoint(org.apache.ignite.internal.util.ipc.shmem.IpcSharedMemoryClientEndpoint) IpcSharedMemoryServerEndpoint(org.apache.ignite.internal.util.ipc.shmem.IpcSharedMemoryServerEndpoint) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteThread(org.apache.ignite.thread.IgniteThread) Nullable(org.jetbrains.annotations.Nullable)

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