Search in sources :

Example 1 with GridNioSession

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

the class TcpRestParserSelfTest method testMixedParsing.

/**
     * @throws Exception If failed.
     */
public void testMixedParsing() throws Exception {
    GridNioSession ses1 = new MockNioSession();
    GridNioSession ses2 = new MockNioSession();
    ses1.addMeta(MARSHALLER.ordinal(), new GridClientOptimizedMarshaller());
    ses2.addMeta(MARSHALLER.ordinal(), new GridClientOptimizedMarshaller());
    GridTcpRestParser parser = new GridTcpRestParser(false);
    GridClientCacheRequest req = new GridClientCacheRequest(CAS);
    req.key("key");
    String val = "value";
    req.value(val);
    req.value2(val);
    req.clientId(UUID.randomUUID());
    byte[] opaque = new byte[] { 0x01, 0x02, 0x03, (byte) 0xFF };
    String key = "key";
    ByteBuffer raw1 = rawPacket(MEMCACHE_REQ_FLAG, (byte) 0x01, opaque, key.getBytes(), val.getBytes(), EXTRAS);
    ByteBuffer raw2 = clientRequestPacket(req);
    raw1.mark();
    raw2.mark();
    int splits = Math.min(raw1.remaining(), raw2.remaining());
    for (int i = 1; i < splits; i++) {
        ByteBuffer[] packet1 = split(raw1, i);
        ByteBuffer[] packet2 = split(raw2, i);
        GridClientMessage msg = parser.decode(ses1, packet1[0]);
        assertNull(msg);
        msg = parser.decode(ses2, packet2[0]);
        assertNull(msg);
        msg = parser.decode(ses1, packet1[1]);
        assertTrue(msg instanceof GridMemcachedMessage);
        assertEquals(key, ((GridMemcachedMessage) msg).key());
        assertEquals(val, ((GridMemcachedMessage) msg).value());
        msg = parser.decode(ses2, packet2[1]);
        assertTrue(msg instanceof GridClientCacheRequest);
        assertEquals(val, ((GridClientCacheRequest) msg).value());
        assertEquals(val, ((GridClientCacheRequest) msg).value2());
        raw1.reset();
        raw2.reset();
    }
}
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 2 with GridNioSession

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

the class TcpRestParserSelfTest method testSimplePacketParsing.

/**
     * @throws Exception If failed.
     */
public void testSimplePacketParsing() throws Exception {
    GridNioSession ses = new MockNioSession();
    GridTcpRestParser parser = new GridTcpRestParser(false);
    byte hdr = MEMCACHE_REQ_FLAG;
    byte[] opCodes = { 0x01, 0x02, 0x03 };
    byte[] opaque = new byte[] { 0x01, 0x02, 0x03, (byte) 0xFF };
    String key = "key";
    String val = "value";
    for (byte opCode : opCodes) {
        ByteBuffer raw = rawPacket(hdr, opCode, opaque, key.getBytes(), val.getBytes(), EXTRAS);
        GridClientMessage msg = parser.decode(ses, raw);
        assertTrue(msg instanceof GridMemcachedMessage);
        GridMemcachedMessage packet = (GridMemcachedMessage) msg;
        assertEquals("Parser leaved unparsed bytes", 0, raw.remaining());
        assertEquals("Invalid opcode", opCode, packet.operationCode());
        assertEquals("Invalid key", key, packet.key());
        assertEquals("Invalid value", val, packet.value());
    }
}
Also used : GridNioSession(org.apache.ignite.internal.util.nio.GridNioSession) ByteBuffer(java.nio.ByteBuffer) GridClientMessage(org.apache.ignite.internal.processors.rest.client.message.GridClientMessage)

Example 3 with GridNioSession

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

the class TcpRestParserSelfTest method testParseContinuousSplit.

/**
     * @throws Exception If failed.
     */
public void testParseContinuousSplit() throws Exception {
    ByteBuffer tmp = ByteBuffer.allocate(10 * 1024);
    GridClientCacheRequest req = new GridClientCacheRequest(CAS);
    req.key("key");
    req.value(1);
    req.value2(2);
    req.clientId(UUID.randomUUID());
    for (int i = 0; i < 5; i++) tmp.put(clientRequestPacket(req));
    tmp.flip();
    for (int splitPos = 0; splitPos < tmp.remaining(); splitPos++) {
        ByteBuffer[] split = split(tmp, splitPos);
        tmp.flip();
        GridNioSession ses = new MockNioSession();
        ses.addMeta(MARSHALLER.ordinal(), new GridClientOptimizedMarshaller());
        GridTcpRestParser parser = new GridTcpRestParser(false);
        Collection<GridClientCacheRequest> lst = new ArrayList<>(5);
        for (ByteBuffer buf : split) {
            GridClientCacheRequest r;
            while (buf.hasRemaining() && (r = (GridClientCacheRequest) parser.decode(ses, buf)) != null) lst.add(r);
            assertTrue("Parser has left unparsed bytes.", buf.remaining() == 0);
        }
        assertEquals(5, lst.size());
        for (GridClientCacheRequest res : lst) {
            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) ArrayList(java.util.ArrayList) ByteBuffer(java.nio.ByteBuffer) GridClientCacheRequest(org.apache.ignite.internal.processors.rest.client.message.GridClientCacheRequest)

Example 4 with GridNioSession

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

the class HadoopExternalCommunication method createTcpClient.

/**
     * Establish TCP connection to remote hadoop process and returns client.
     *
     * @param desc Process descriptor.
     * @return Client.
     * @throws IgniteCheckedException If failed.
     */
protected HadoopCommunicationClient createTcpClient(HadoopProcessDescriptor desc) throws IgniteCheckedException {
    String addr = desc.address();
    int port = desc.tcpPort();
    if (log.isDebugEnabled())
        log.debug("Trying to connect to remote process [locProcDesc=" + locProcDesc + ", desc=" + desc + ']');
    boolean conn = false;
    HadoopTcpNioCommunicationClient client = null;
    IgniteCheckedException errs = null;
    int connectAttempts = 1;
    long connTimeout0 = connTimeout;
    int attempt = 1;
    while (!conn) {
        // Reconnection on handshake timeout.
        try {
            SocketChannel ch = SocketChannel.open();
            ch.configureBlocking(true);
            ch.socket().setTcpNoDelay(tcpNoDelay);
            ch.socket().setKeepAlive(true);
            if (sockRcvBuf > 0)
                ch.socket().setReceiveBufferSize(sockRcvBuf);
            if (sockSndBuf > 0)
                ch.socket().setSendBufferSize(sockSndBuf);
            ch.socket().connect(new InetSocketAddress(addr, port), (int) connTimeout);
            HandshakeFinish fin = new HandshakeFinish();
            GridNioSession ses = nioSrvr.createSession(ch, F.asMap(HANDSHAKE_FINISH_META, fin)).get();
            client = new HadoopTcpNioCommunicationClient(ses);
            if (log.isDebugEnabled())
                log.debug("Waiting for handshake finish for client: " + client);
            fin.await(connTimeout0);
            conn = true;
        } catch (HadoopHandshakeTimeoutException e) {
            if (client != null) {
                client.forceClose();
                client = null;
            }
            if (log.isDebugEnabled())
                log.debug("Handshake timedout (will retry with increased timeout) [timeout=" + connTimeout0 + ", desc=" + desc + ", port=" + port + ", err=" + e + ']');
            if (attempt == reconCnt || connTimeout0 > maxConnTimeout) {
                if (log.isDebugEnabled())
                    log.debug("Handshake timed out (will stop attempts to perform the handshake) " + "[timeout=" + connTimeout0 + ", maxConnTimeout=" + maxConnTimeout + ", attempt=" + attempt + ", reconCnt=" + reconCnt + ", err=" + e.getMessage() + ", addr=" + addr + ']');
                if (errs == null)
                    errs = new IgniteCheckedException("Failed to connect to remote Hadoop process " + "(is process still running?) [desc=" + desc + ", addrs=" + addr + ']');
                errs.addSuppressed(e);
                break;
            } else {
                attempt++;
                connTimeout0 *= 2;
            // Continue loop.
            }
        } catch (Exception e) {
            if (client != null) {
                client.forceClose();
                client = null;
            }
            if (log.isDebugEnabled())
                log.debug("Client creation failed [addr=" + addr + ", port=" + port + ", err=" + e + ']');
            if (X.hasCause(e, SocketTimeoutException.class))
                LT.warn(log, "Connect timed out (consider increasing 'connTimeout' " + "configuration property) [addr=" + addr + ", port=" + port + ']');
            if (errs == null)
                errs = new IgniteCheckedException("Failed to connect to remote Hadoop process (is process still running?) " + "[desc=" + desc + ", addrs=" + addr + ']');
            errs.addSuppressed(e);
            // Reconnect for the second time, if connection is not established.
            if (connectAttempts < 2 && (e instanceof ConnectException || X.hasCause(e, ConnectException.class))) {
                connectAttempts++;
                continue;
            }
            break;
        }
    }
    if (client == null) {
        assert errs != null;
        if (X.hasCause(errs, ConnectException.class))
            LT.warn(log, "Failed to connect to a remote Hadoop process (is process still running?). " + "Make sure operating system firewall is disabled on local and remote host) " + "[addrs=" + addr + ", port=" + port + ']');
        throw errs;
    }
    if (log.isDebugEnabled())
        log.debug("Created client: " + client);
    return client;
}
Also used : SocketChannel(java.nio.channels.SocketChannel) GridNioSession(org.apache.ignite.internal.util.nio.GridNioSession) InetSocketAddress(java.net.InetSocketAddress) 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) IgniteException(org.apache.ignite.IgniteException) SocketTimeoutException(java.net.SocketTimeoutException) ConnectException(java.net.ConnectException) IpcOutOfSystemResourcesException(org.apache.ignite.internal.util.ipc.shmem.IpcOutOfSystemResourcesException) IOException(java.io.IOException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) SocketTimeoutException(java.net.SocketTimeoutException) ConnectException(java.net.ConnectException)

Example 5 with GridNioSession

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

the class GridTcpCommunicationSpiMultithreadedSelfTest method testFlowSend.

/**
     * @throws Exception If failed.
     */
public void testFlowSend() throws Exception {
    reject = true;
    final CyclicBarrier barrier = new CyclicBarrier(THREAD_CNT);
    final Random rnd = new Random();
    final ClusterNode from = randomNode(rnd);
    ClusterNode tmp;
    do {
        tmp = randomNode(rnd);
    } while (tmp.id().equals(from.id()));
    final ClusterNode to = tmp;
    final int iterationCnt = 1000;
    final AtomicInteger threadId = new AtomicInteger();
    final int interval = 50;
    IgniteInternalFuture<?> fut = multithreadedAsync(new Runnable() {

        /** {@inheritDoc} */
        @Override
        public void run() {
            try {
                // Only first thread will print messages.
                int id = threadId.getAndIncrement();
                for (int i = 0; i < iterationCnt; i++) {
                    if (id == 0 && (i % 50) == 0)
                        info(">>> Running iteration " + i);
                    try {
                        for (ClusterNode node : nodes) {
                            Message msg = new GridTestMessage(from.id(), msgId.getAndIncrement(), 0);
                            spis.get(from.id()).sendMessage(node, msg);
                        }
                    } catch (IgniteException e) {
                        log.warning(">>> Oops, unable to send message (safe to ignore).", e);
                    }
                    barrier.await();
                }
            } catch (InterruptedException ignored) {
                Thread.currentThread().interrupt();
            } catch (BrokenBarrierException e) {
                info("Wait on barrier failed: " + e);
                Thread.currentThread().interrupt();
            }
        }
    }, THREAD_CNT, "message-sender");
    final AtomicBoolean run = new AtomicBoolean(true);
    IgniteInternalFuture<?> fut2 = multithreadedAsync(new Runnable() {

        @Override
        public void run() {
            try {
                while (run.get() && !Thread.currentThread().isInterrupted()) {
                    U.sleep(interval * 3 / 2);
                    ((TcpCommunicationSpi) spis.get(from.id())).onNodeLeft(to.id());
                }
            } catch (IgniteInterruptedCheckedException ignored) {
                Thread.currentThread().interrupt();
            }
        }
    }, 1);
    fut.get();
    run.set(false);
    fut2.get();
    // Wait when all messages are acknowledged to do not break next tests' logic.
    for (CommunicationSpi<Message> spi : spis.values()) {
        GridNioServer srv = U.field(spi, "nioSrvr");
        Collection<? extends GridNioSession> sessions = GridTestUtils.getFieldValue(srv, "sessions");
        for (GridNioSession ses : sessions) {
            final GridNioRecoveryDescriptor snd = ses.outRecoveryDescriptor();
            if (snd != null) {
                GridTestUtils.waitForCondition(new GridAbsPredicate() {

                    @Override
                    public boolean apply() {
                        return snd.messagesRequests().isEmpty();
                    }
                }, 10_000);
                assertEquals("Unexpected messages: " + snd.messagesRequests(), 0, snd.messagesRequests().size());
            }
        }
    }
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) GridTestMessage(org.apache.ignite.spi.communication.GridTestMessage) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) GridTestMessage(org.apache.ignite.spi.communication.GridTestMessage) Message(org.apache.ignite.plugin.extensions.communication.Message) GridNioSession(org.apache.ignite.internal.util.nio.GridNioSession) GridAbsPredicate(org.apache.ignite.internal.util.lang.GridAbsPredicate) GridNioServer(org.apache.ignite.internal.util.nio.GridNioServer) CyclicBarrier(java.util.concurrent.CyclicBarrier) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IgniteInterruptedCheckedException(org.apache.ignite.internal.IgniteInterruptedCheckedException) Random(java.util.Random) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgniteException(org.apache.ignite.IgniteException) IgniteRunnable(org.apache.ignite.lang.IgniteRunnable) GridNioRecoveryDescriptor(org.apache.ignite.internal.util.nio.GridNioRecoveryDescriptor)

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