Search in sources :

Example 6 with GridNioServer

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

the class HadoopExternalCommunication method resetNioServer.

/**
 * Recreates tpcSrvr socket instance.
 *
 * @return Server instance.
 * @throws IgniteCheckedException Thrown if it's not possible to create server.
 */
private GridNioServer<HadoopMessage> resetNioServer() throws IgniteCheckedException {
    if (boundTcpPort >= 0)
        throw new IgniteCheckedException("Tcp NIO server was already created on port " + boundTcpPort);
    IgniteCheckedException lastEx = null;
    // If configured TCP port is busy, find first available in range.
    for (int port = locPort; port < locPort + locPortRange; port++) {
        try {
            GridNioServer<HadoopMessage> srvr = GridNioServer.<HadoopMessage>builder().address(locHost).port(port).listener(srvLsnr).logger(log.getLogger(GridNioServer.class)).selectorCount(selectorsCnt).igniteInstanceName(igniteInstanceName).serverName("hadoop").tcpNoDelay(tcpNoDelay).directBuffer(directBuf).byteOrder(ByteOrder.nativeOrder()).socketSendBufferSize(sockSndBuf).socketReceiveBufferSize(sockRcvBuf).sendQueueLimit(msgQueueLimit).directMode(false).filters(filters()).build();
            boundTcpPort = port;
            // Ack Port the TCP server was bound to.
            if (log.isInfoEnabled())
                log.info("Successfully bound to TCP port [port=" + boundTcpPort + ", locHost=" + locHost + ']');
            return srvr;
        } catch (IgniteCheckedException e) {
            lastEx = e;
            if (log.isDebugEnabled())
                log.debug("Failed to bind to local port (will try next port within range) [port=" + port + ", locHost=" + locHost + ']');
        }
    }
    // If free port wasn't found.
    throw new IgniteCheckedException("Failed to bind to any port within range [startPort=" + locPort + ", portRange=" + locPortRange + ", locHost=" + locHost + ']', lastEx);
}
Also used : IgniteCheckedException(org.apache.ignite.IgniteCheckedException) HadoopMessage(org.apache.ignite.internal.processors.hadoop.message.HadoopMessage) 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) GridNioServer(org.apache.ignite.internal.util.nio.GridNioServer)

Example 7 with GridNioServer

use of org.apache.ignite.internal.util.nio.GridNioServer 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)

Example 8 with GridNioServer

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

the class ClientReconnectContinuousQueryTest method skipRead.

/**
 * @param igniteClient Ignite client.
 * @param skip Skip.
 */
private void skipRead(IgniteEx igniteClient, boolean skip) {
    GridIoManager ioMgr = igniteClient.context().io();
    TcpCommunicationSpi commSpi = (TcpCommunicationSpi) ((Object[]) U.field(ioMgr, "spis"))[0];
    GridNioServer nioSrvr = U.field(commSpi, "nioSrvr");
    GridTestUtils.setFieldValue(nioSrvr, "skipRead", skip);
}
Also used : GridIoManager(org.apache.ignite.internal.managers.communication.GridIoManager) TcpCommunicationSpi(org.apache.ignite.spi.communication.tcp.TcpCommunicationSpi) GridNioServer(org.apache.ignite.internal.util.nio.GridNioServer)

Example 9 with GridNioServer

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

the class GridTcpCommunicationSpiConcurrentConnectSelfTest method concurrentConnect.

/**
 * @param threads Number of threads.
 * @param msgPerThread Messages per thread.
 * @param iters Number of iterations.
 * @param sleep If {@code true} sleeps random time before starts send messages.
 * @param load Run load threads flag.
 * @throws Exception If failed.
 */
private void concurrentConnect(final int threads, final int msgPerThread, final int iters, final boolean sleep, boolean load) throws Exception {
    log.info("Concurrent connect [threads=" + threads + ", msgPerThread=" + msgPerThread + ", iters=" + iters + ", load=" + load + ", sleep=" + sleep + ']');
    final AtomicBoolean stop = new AtomicBoolean();
    IgniteInternalFuture<?> loadFut = null;
    if (load) {
        loadFut = GridTestUtils.runMultiThreadedAsync(new Callable<Long>() {

            @Override
            public Long call() throws Exception {
                long dummyRes = 0;
                List<String> list = new ArrayList<>();
                while (!stop.get()) {
                    for (int i = 0; i < 100; i++) {
                        String str = new String(new byte[i]);
                        list.add(str);
                        dummyRes += str.hashCode();
                    }
                    if (list.size() > 1000_000) {
                        list = new ArrayList<>();
                        System.gc();
                    }
                }
                return dummyRes;
            }
        }, 2, "test-load");
    }
    try {
        for (int i = 0; i < iters; i++) {
            log.info("Iteration: " + i);
            final AtomicInteger msgId = new AtomicInteger();
            final int expMsgs = threads * msgPerThread;
            CountDownLatch latch = new CountDownLatch(expMsgs);
            MessageListener lsnr = new MessageListener(latch);
            createSpis(lsnr);
            final AtomicInteger idx = new AtomicInteger();
            try {
                final Callable<Void> c = new Callable<Void>() {

                    @Override
                    public Void call() throws Exception {
                        int idx0 = idx.getAndIncrement();
                        Thread.currentThread().setName("Test thread [idx=" + idx0 + ", grid=" + (idx0 % 2) + ']');
                        CommunicationSpi<Message> spi = spis.get(idx0 % 2);
                        ClusterNode srcNode = nodes.get(idx0 % 2);
                        ClusterNode dstNode = nodes.get((idx0 + 1) % 2);
                        if (sleep) {
                            ThreadLocalRandom rnd = ThreadLocalRandom.current();
                            long millis = rnd.nextLong(10);
                            if (millis > 0)
                                Thread.sleep(millis);
                        }
                        for (int i = 0; i < msgPerThread; i++) spi.sendMessage(dstNode, new GridTestMessage(srcNode.id(), msgId.incrementAndGet(), 0));
                        return null;
                    }
                };
                List<Thread> threadsList = new ArrayList<>();
                final AtomicBoolean fail = new AtomicBoolean();
                final AtomicLong tId = new AtomicLong();
                for (int t = 0; t < threads; t++) {
                    Thread t0 = new Thread(new Runnable() {

                        @Override
                        public void run() {
                            try {
                                c.call();
                            } catch (Throwable e) {
                                log.error("Unexpected error: " + e, e);
                                fail.set(true);
                            }
                        }
                    }) {

                        @Override
                        public long getId() {
                            // Override getId to use all connections.
                            return tId.getAndIncrement();
                        }
                    };
                    threadsList.add(t0);
                    t0.start();
                }
                for (Thread t0 : threadsList) t0.join();
                assertTrue(latch.await(10, TimeUnit.SECONDS));
                for (CommunicationSpi spi : spis) {
                    ConcurrentMap<UUID, GridCommunicationClient> clients = U.field(spi, "clients");
                    assertEquals(1, clients.size());
                    final GridNioServer srv = U.field(spi, "nioSrvr");
                    final int conns = pairedConnections ? 2 : 1;
                    GridTestUtils.waitForCondition(new GridAbsPredicate() {

                        @Override
                        public boolean apply() {
                            Collection sessions = U.field(srv, "sessions");
                            return sessions.size() == conns * connectionsPerNode;
                        }
                    }, 5000);
                    Collection sessions = U.field(srv, "sessions");
                    assertEquals(conns * connectionsPerNode, sessions.size());
                }
                assertEquals(expMsgs, lsnr.cntr.get());
            } finally {
                stopSpis();
            }
        }
    } finally {
        stop.set(true);
        if (loadFut != null)
            loadFut.get();
    }
}
Also used : GridTestMessage(org.apache.ignite.spi.communication.GridTestMessage) Message(org.apache.ignite.plugin.extensions.communication.Message) ArrayList(java.util.ArrayList) Callable(java.util.concurrent.Callable) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) UUID(java.util.UUID) ClusterNode(org.apache.ignite.cluster.ClusterNode) GridTestMessage(org.apache.ignite.spi.communication.GridTestMessage) CommunicationSpi(org.apache.ignite.spi.communication.CommunicationSpi) GridAbsPredicate(org.apache.ignite.internal.util.lang.GridAbsPredicate) CountDownLatch(java.util.concurrent.CountDownLatch) GridCommunicationClient(org.apache.ignite.internal.util.nio.GridCommunicationClient) GridNioServer(org.apache.ignite.internal.util.nio.GridNioServer) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicLong(java.util.concurrent.atomic.AtomicLong) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgniteRunnable(org.apache.ignite.lang.IgniteRunnable) Collection(java.util.Collection)

Example 10 with GridNioServer

use of org.apache.ignite.internal.util.nio.GridNioServer 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)

Aggregations

GridNioServer (org.apache.ignite.internal.util.nio.GridNioServer)14 GridAbsPredicate (org.apache.ignite.internal.util.lang.GridAbsPredicate)11 ClusterNode (org.apache.ignite.cluster.ClusterNode)8 GridNioSession (org.apache.ignite.internal.util.nio.GridNioSession)8 GridTestMessage (org.apache.ignite.spi.communication.GridTestMessage)6 Collection (java.util.Collection)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 IgniteException (org.apache.ignite.IgniteException)3 GridNioRecoveryDescriptor (org.apache.ignite.internal.util.nio.GridNioRecoveryDescriptor)3 TcpCommunicationSpi (org.apache.ignite.spi.communication.tcp.TcpCommunicationSpi)3 ArrayList (java.util.ArrayList)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 Ignite (org.apache.ignite.Ignite)2 GridIoManager (org.apache.ignite.internal.managers.communication.GridIoManager)2 CI1 (org.apache.ignite.internal.util.typedef.CI1)2 IgniteRunnable (org.apache.ignite.lang.IgniteRunnable)2 Message (org.apache.ignite.plugin.extensions.communication.Message)2 IgniteSpiException (org.apache.ignite.spi.IgniteSpiException)2