Search in sources :

Example 1 with GridNioFilter

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

the class GridTcpRestProtocol method startTcpServer.

/**
 * Tries to start server with given parameters.
 *
 * @param hostAddr Host on which server should be bound.
 * @param port Port on which server should be bound.
 * @param lsnr Server message listener.
 * @param parser Server message parser.
 * @param sslCtx SSL context in case if SSL is enabled.
 * @param cfg Configuration for other parameters.
 * @return {@code True} if server successfully started, {@code false} if port is used and
 *      server was unable to start.
 */
private boolean startTcpServer(InetAddress hostAddr, int port, GridNioServerListener<GridClientMessage> lsnr, GridNioParser parser, @Nullable SSLContext sslCtx, ConnectorConfiguration cfg) {
    try {
        GridNioFilter codec = new GridNioCodecFilter(parser, log, false);
        GridNioFilter[] filters;
        if (sslCtx != null) {
            GridNioSslFilter sslFilter = new GridNioSslFilter(sslCtx, cfg.isDirectBuffer(), ByteOrder.nativeOrder(), log, ctx.metric().registry(REST_CONNECTOR_METRIC_REGISTRY_NAME));
            sslFilter.directMode(false);
            boolean auth = cfg.isSslClientAuth();
            sslFilter.wantClientAuth(auth);
            sslFilter.needClientAuth(auth);
            filters = new GridNioFilter[] { codec, sslFilter };
        } else
            filters = new GridNioFilter[] { codec };
        srv = GridNioServer.<GridClientMessage>builder().address(hostAddr).port(port).listener(lsnr).logger(log).selectorCount(cfg.getSelectorCount()).igniteInstanceName(ctx.igniteInstanceName()).serverName("tcp-rest").tcpNoDelay(cfg.isNoDelay()).directBuffer(cfg.isDirectBuffer()).byteOrder(ByteOrder.nativeOrder()).socketSendBufferSize(cfg.getSendBufferSize()).socketReceiveBufferSize(cfg.getReceiveBufferSize()).sendQueueLimit(cfg.getSendQueueLimit()).filters(filters).directMode(false).metricRegistry(ctx.metric().registry(REST_CONNECTOR_METRIC_REGISTRY_NAME)).build();
        srv.idleTimeout(cfg.getIdleTimeout());
        srv.start();
        ctx.ports().registerPort(port, IgnitePortProtocol.TCP, getClass());
        return true;
    } catch (IgniteCheckedException e) {
        if (log.isDebugEnabled())
            log.debug("Failed to start " + name() + " protocol on port " + port + ": " + e.getMessage());
        return false;
    }
}
Also used : GridNioFilter(org.apache.ignite.internal.util.nio.GridNioFilter) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridNioCodecFilter(org.apache.ignite.internal.util.nio.GridNioCodecFilter) GridNioSslFilter(org.apache.ignite.internal.util.nio.ssl.GridNioSslFilter) GridClientMessage(org.apache.ignite.internal.processors.rest.client.message.GridClientMessage)

Example 2 with GridNioFilter

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

the class ClientListenerProcessor method makeFilters.

/**
 * Make NIO server filters.
 *
 * @param cliConnCfg Client configuration.
 * @return Array of filters, suitable for the configuration.
 * @throws IgniteCheckedException if provided SslContextFactory is null.
 */
@NotNull
private GridNioFilter[] makeFilters(@NotNull ClientConnectorConfiguration cliConnCfg) throws IgniteCheckedException {
    GridNioFilter openSesFilter = new GridNioAsyncNotifyFilter(ctx.igniteInstanceName(), execSvc, log) {

        @Override
        public void onSessionOpened(GridNioSession ses) throws IgniteCheckedException {
            proceedSessionOpened(ses);
        }

        @Override
        public void onMessageReceived(GridNioSession ses, Object msg) throws IgniteCheckedException {
            ClientListenerConnectionContext connCtx = ses.meta(ClientListenerNioListener.CONN_CTX_META_KEY);
            if (connCtx != null && connCtx.parser() != null && connCtx.handler().isCancellationSupported()) {
                ClientMessage inMsg;
                int cmdType;
                long reqId;
                try {
                    inMsg = (ClientMessage) msg;
                    cmdType = connCtx.parser().decodeCommandType(inMsg);
                    reqId = connCtx.parser().decodeRequestId(inMsg);
                } catch (Exception e) {
                    U.error(log, "Failed to parse client request.", e);
                    ses.close();
                    return;
                }
                if (connCtx.handler().isCancellationCommand(cmdType)) {
                    CANCEL_COUNTER.incrementAndGet();
                    proceedMessageReceived(ses, msg);
                } else {
                    connCtx.handler().registerRequest(reqId, cmdType);
                    super.onMessageReceived(ses, msg);
                }
            } else
                super.onMessageReceived(ses, msg);
        }
    };
    GridNioFilter codecFilter = new GridNioCodecFilter(new ClientListenerNioMessageParser(log), log, true);
    if (cliConnCfg.isSslEnabled()) {
        Factory<SSLContext> sslCtxFactory = cliConnCfg.isUseIgniteSslContextFactory() ? ctx.config().getSslContextFactory() : cliConnCfg.getSslContextFactory();
        if (sslCtxFactory == null)
            throw new IgniteCheckedException("Failed to create client listener " + "(SSL is enabled but factory is null). Check the ClientConnectorConfiguration");
        GridNioSslFilter sslFilter = new GridNioSslFilter(sslCtxFactory.create(), true, ByteOrder.nativeOrder(), log, ctx.metric().registry(CLIENT_CONNECTOR_METRICS));
        sslFilter.directMode(true);
        boolean auth = cliConnCfg.isSslClientAuth();
        sslFilter.wantClientAuth(auth);
        sslFilter.needClientAuth(auth);
        return new GridNioFilter[] { openSesFilter, codecFilter, sslFilter };
    } else {
        return new GridNioFilter[] { openSesFilter, codecFilter };
    }
}
Also used : GridNioFilter(org.apache.ignite.internal.util.nio.GridNioFilter) GridNioSession(org.apache.ignite.internal.util.nio.GridNioSession) SSLContext(javax.net.ssl.SSLContext) GridNioSslFilter(org.apache.ignite.internal.util.nio.ssl.GridNioSslFilter) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) JMException(javax.management.JMException) GridNioAsyncNotifyFilter(org.apache.ignite.internal.util.nio.GridNioAsyncNotifyFilter) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridNioCodecFilter(org.apache.ignite.internal.util.nio.GridNioCodecFilter) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with GridNioFilter

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

the class GridTcpRouterImpl method startTcpServer.

/**
 * Tries to start server with given parameters.
 *
 * @param hostAddr Host on which server should be bound.
 * @param port Port on which server should be bound.
 * @param lsnr Server message listener.
 * @param parser Server message parser.
 * @param tcpNoDelay Flag indicating whether TCP_NODELAY flag should be set for accepted connections.
 * @param sslCtx SSL context in case if SSL is enabled.
 * @param wantClientAuth Whether client will be requested for authentication.
 * @param needClientAuth Whether client is required to be authenticated.
 * @return {@code True} if server successfully started, {@code false} if port is used and
 *      server was unable to start.
 */
private boolean startTcpServer(InetAddress hostAddr, int port, GridNioServerListener<GridClientMessage> lsnr, GridNioParser parser, boolean tcpNoDelay, @Nullable SSLContext sslCtx, boolean wantClientAuth, boolean needClientAuth) {
    try {
        GridNioFilter codec = new GridNioCodecFilter(parser, log, false);
        // This name is required to be unique in order to avoid collisions with
        // ThreadWorkerGroups running in the same JVM by other routers/nodes.
        String igniteInstanceName = "router-" + id;
        GridNioFilter[] filters;
        if (sslCtx != null) {
            GridNioSslFilter sslFilter = new GridNioSslFilter(sslCtx, false, ByteOrder.nativeOrder(), log, null);
            sslFilter.wantClientAuth(wantClientAuth);
            sslFilter.needClientAuth(needClientAuth);
            filters = new GridNioFilter[] { codec, sslFilter };
        } else
            filters = new GridNioFilter[] { codec };
        srv = GridNioServer.<GridClientMessage>builder().address(hostAddr).port(port).listener(lsnr).logger(log).selectorCount(Runtime.getRuntime().availableProcessors()).igniteInstanceName(igniteInstanceName).serverName("router").tcpNoDelay(tcpNoDelay).directBuffer(false).byteOrder(ByteOrder.nativeOrder()).socketSendBufferSize(0).socketReceiveBufferSize(0).sendQueueLimit(0).filters(filters).idleTimeout(cfg.getIdleTimeout()).build();
        srv.start();
        return true;
    } catch (IgniteCheckedException e) {
        if (log.isDebugEnabled())
            log.debug("Failed to start TCP router protocol on port " + port + ": " + e.getMessage());
        srv = null;
        return false;
    }
}
Also used : GridNioFilter(org.apache.ignite.internal.util.nio.GridNioFilter) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridNioCodecFilter(org.apache.ignite.internal.util.nio.GridNioCodecFilter) GridNioSslFilter(org.apache.ignite.internal.util.nio.ssl.GridNioSslFilter)

Example 4 with GridNioFilter

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

the class SqlListenerProcessor method start.

/** {@inheritDoc} */
@Override
public void start(boolean activeOnStart) throws IgniteCheckedException {
    IgniteConfiguration cfg = ctx.config();
    OdbcConfiguration odbcCfg = cfg.getOdbcConfiguration();
    if (odbcCfg != null) {
        try {
            HostAndPortRange hostPort;
            if (F.isEmpty(odbcCfg.getEndpointAddress())) {
                hostPort = new HostAndPortRange(OdbcConfiguration.DFLT_TCP_HOST, OdbcConfiguration.DFLT_TCP_PORT_FROM, OdbcConfiguration.DFLT_TCP_PORT_TO);
            } else {
                hostPort = HostAndPortRange.parse(odbcCfg.getEndpointAddress(), OdbcConfiguration.DFLT_TCP_PORT_FROM, OdbcConfiguration.DFLT_TCP_PORT_TO, "Failed to parse ODBC endpoint address");
            }
            assertParameter(odbcCfg.getThreadPoolSize() > 0, "threadPoolSize > 0");
            odbcExecSvc = new IgniteThreadPoolExecutor("odbc", cfg.getIgniteInstanceName(), odbcCfg.getThreadPoolSize(), odbcCfg.getThreadPoolSize(), 0, new LinkedBlockingQueue<Runnable>());
            InetAddress host;
            try {
                host = InetAddress.getByName(hostPort.host());
            } catch (Exception e) {
                throw new IgniteCheckedException("Failed to resolve ODBC host: " + hostPort.host(), e);
            }
            Exception lastErr = null;
            for (int port = hostPort.portFrom(); port <= hostPort.portTo(); port++) {
                try {
                    GridNioFilter[] filters = new GridNioFilter[] { new GridNioAsyncNotifyFilter(ctx.igniteInstanceName(), odbcExecSvc, log) {

                        @Override
                        public void onSessionOpened(GridNioSession ses) throws IgniteCheckedException {
                            proceedSessionOpened(ses);
                        }
                    }, new GridNioCodecFilter(new SqlListenerBufferedParser(), log, false) };
                    GridNioServer<byte[]> srv0 = GridNioServer.<byte[]>builder().address(host).port(port).listener(new SqlListenerNioListener(ctx, busyLock, odbcCfg.getMaxOpenCursors())).logger(log).selectorCount(DFLT_SELECTOR_CNT).igniteInstanceName(ctx.igniteInstanceName()).serverName("odbc").tcpNoDelay(DFLT_TCP_NODELAY).directBuffer(DFLT_TCP_DIRECT_BUF).byteOrder(ByteOrder.nativeOrder()).socketSendBufferSize(odbcCfg.getSocketSendBufferSize()).socketReceiveBufferSize(odbcCfg.getSocketReceiveBufferSize()).filters(filters).directMode(false).idleTimeout(Long.MAX_VALUE).build();
                    srv0.start();
                    srv = srv0;
                    ctx.ports().registerPort(port, IgnitePortProtocol.TCP, getClass());
                    log.info("ODBC processor has started on TCP port " + port);
                    lastErr = null;
                    break;
                } catch (Exception e) {
                    lastErr = e;
                }
            }
            assert (srv != null && lastErr == null) || (srv == null && lastErr != null);
            if (lastErr != null)
                throw new IgniteCheckedException("Failed to bind to any [host:port] from the range [" + "address=" + hostPort + ", lastErr=" + lastErr + ']');
        } catch (Exception e) {
            throw new IgniteCheckedException("Failed to start ODBC processor.", e);
        }
    }
}
Also used : GridNioFilter(org.apache.ignite.internal.util.nio.GridNioFilter) GridNioSession(org.apache.ignite.internal.util.nio.GridNioSession) HostAndPortRange(org.apache.ignite.internal.util.HostAndPortRange) IgniteThreadPoolExecutor(org.apache.ignite.thread.IgniteThreadPoolExecutor) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridNioAsyncNotifyFilter(org.apache.ignite.internal.util.nio.GridNioAsyncNotifyFilter) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) GridNioCodecFilter(org.apache.ignite.internal.util.nio.GridNioCodecFilter) OdbcConfiguration(org.apache.ignite.configuration.OdbcConfiguration) InetAddress(java.net.InetAddress)

Example 5 with GridNioFilter

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

the class TcpCommunicationSpi method resetNioServer.

/**
 * Recreates tpcSrvr socket instance.
 *
 * @return Server instance.
 * @throws IgniteCheckedException Thrown if it's not possible to create server.
 */
private GridNioServer<Message> 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.
    int lastPort = locPortRange == 0 ? locPort : locPort + locPortRange - 1;
    for (int port = locPort; port <= lastPort; port++) {
        try {
            MessageFactory msgFactory = new MessageFactory() {

                private MessageFactory impl;

                @Nullable
                @Override
                public Message create(short type) {
                    if (impl == null)
                        impl = getSpiContext().messageFactory();
                    assert impl != null;
                    return impl.create(type);
                }
            };
            GridNioMessageReaderFactory readerFactory = new GridNioMessageReaderFactory() {

                private MessageFormatter formatter;

                @Override
                public MessageReader reader(GridNioSession ses, MessageFactory msgFactory) throws IgniteCheckedException {
                    if (formatter == null)
                        formatter = getSpiContext().messageFormatter();
                    assert formatter != null;
                    ConnectionKey key = ses.meta(CONN_IDX_META);
                    return key != null ? formatter.reader(key.nodeId(), msgFactory) : null;
                }
            };
            GridNioMessageWriterFactory writerFactory = new GridNioMessageWriterFactory() {

                private MessageFormatter formatter;

                @Override
                public MessageWriter writer(GridNioSession ses) throws IgniteCheckedException {
                    if (formatter == null)
                        formatter = getSpiContext().messageFormatter();
                    assert formatter != null;
                    ConnectionKey key = ses.meta(CONN_IDX_META);
                    return key != null ? formatter.writer(key.nodeId()) : null;
                }
            };
            GridDirectParser parser = new GridDirectParser(log.getLogger(GridDirectParser.class), msgFactory, readerFactory);
            IgnitePredicate<Message> skipRecoveryPred = new IgnitePredicate<Message>() {

                @Override
                public boolean apply(Message msg) {
                    return msg instanceof RecoveryLastReceivedMessage;
                }
            };
            boolean clientMode = Boolean.TRUE.equals(ignite.configuration().isClientMode());
            IgniteBiInClosure<GridNioSession, Integer> queueSizeMonitor = !clientMode && slowClientQueueLimit > 0 ? new CI2<GridNioSession, Integer>() {

                @Override
                public void apply(GridNioSession ses, Integer qSize) {
                    checkClientQueueSize(ses, qSize);
                }
            } : null;
            GridNioFilter[] filters;
            if (isSslEnabled()) {
                GridNioSslFilter sslFilter = new GridNioSslFilter(ignite.configuration().getSslContextFactory().create(), true, ByteOrder.nativeOrder(), log);
                sslFilter.directMode(true);
                sslFilter.wantClientAuth(true);
                sslFilter.needClientAuth(true);
                filters = new GridNioFilter[] { new GridNioCodecFilter(parser, log, true), new GridConnectionBytesVerifyFilter(log), sslFilter };
            } else
                filters = new GridNioFilter[] { new GridNioCodecFilter(parser, log, true), new GridConnectionBytesVerifyFilter(log) };
            GridNioServer<Message> srvr = GridNioServer.<Message>builder().address(locHost).port(port).listener(srvLsnr).logger(log).selectorCount(selectorsCnt).igniteInstanceName(igniteInstanceName).serverName("tcp-comm").tcpNoDelay(tcpNoDelay).directBuffer(directBuf).byteOrder(ByteOrder.nativeOrder()).socketSendBufferSize(sockSndBuf).socketReceiveBufferSize(sockRcvBuf).sendQueueLimit(msgQueueLimit).directMode(true).metricsListener(metricsLsnr).writeTimeout(sockWriteTimeout).selectorSpins(selectorSpins).filters(filters).writerFactory(writerFactory).skipRecoveryPredicate(skipRecoveryPred).messageQueueSizeListener(queueSizeMonitor).readWriteSelectorsAssign(usePairedConnections).build();
            boundTcpPort = port;
            // Ack Port the TCP server was bound to.
            if (log.isInfoEnabled()) {
                log.info("Successfully bound communication NIO server to TCP port " + "[port=" + boundTcpPort + ", locHost=" + locHost + ", selectorsCnt=" + selectorsCnt + ", selectorSpins=" + srvr.selectorSpins() + ", pairedConn=" + usePairedConnections + ']');
            }
            srvr.idleTimeout(idleConnTimeout);
            return srvr;
        } catch (IgniteCheckedException e) {
            if (X.hasCause(e, SSLException.class))
                throw new IgniteSpiException("Failed to create SSL context. SSL factory: " + ignite.configuration().getSslContextFactory() + '.', e);
            lastEx = e;
            if (log.isDebugEnabled())
                log.debug("Failed to bind to local port (will try next port within range) [port=" + port + ", locHost=" + locHost + ']');
            onException("Failed to bind to local port (will try next port within range) [port=" + port + ", locHost=" + locHost + ']', e);
        }
    }
    // 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 : GridNioFilter(org.apache.ignite.internal.util.nio.GridNioFilter) GridNioSession(org.apache.ignite.internal.util.nio.GridNioSession) RecoveryLastReceivedMessage(org.apache.ignite.spi.communication.tcp.messages.RecoveryLastReceivedMessage) Message(org.apache.ignite.plugin.extensions.communication.Message) HandshakeMessage(org.apache.ignite.spi.communication.tcp.messages.HandshakeMessage) NodeIdMessage(org.apache.ignite.spi.communication.tcp.messages.NodeIdMessage) IgnitePredicate(org.apache.ignite.lang.IgnitePredicate) MessageFormatter(org.apache.ignite.plugin.extensions.communication.MessageFormatter) GridNioSslFilter(org.apache.ignite.internal.util.nio.ssl.GridNioSslFilter) SSLException(javax.net.ssl.SSLException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridDirectParser(org.apache.ignite.internal.util.nio.GridDirectParser) IgniteSpiException(org.apache.ignite.spi.IgniteSpiException) GridNioMessageWriterFactory(org.apache.ignite.internal.util.nio.GridNioMessageWriterFactory) RecoveryLastReceivedMessage(org.apache.ignite.spi.communication.tcp.messages.RecoveryLastReceivedMessage) MessageFactory(org.apache.ignite.plugin.extensions.communication.MessageFactory) GridConnectionBytesVerifyFilter(org.apache.ignite.internal.util.nio.GridConnectionBytesVerifyFilter) IpcEndpoint(org.apache.ignite.internal.util.ipc.IpcEndpoint) IpcSharedMemoryServerEndpoint(org.apache.ignite.internal.util.ipc.shmem.IpcSharedMemoryServerEndpoint) GridNioCodecFilter(org.apache.ignite.internal.util.nio.GridNioCodecFilter) GridNioMessageReaderFactory(org.apache.ignite.internal.util.nio.GridNioMessageReaderFactory)

Aggregations

IgniteCheckedException (org.apache.ignite.IgniteCheckedException)8 GridNioFilter (org.apache.ignite.internal.util.nio.GridNioFilter)8 GridNioCodecFilter (org.apache.ignite.internal.util.nio.GridNioCodecFilter)7 GridNioSession (org.apache.ignite.internal.util.nio.GridNioSession)5 GridNioSslFilter (org.apache.ignite.internal.util.nio.ssl.GridNioSslFilter)5 IgniteException (org.apache.ignite.IgniteException)3 IgniteConfiguration (org.apache.ignite.configuration.IgniteConfiguration)3 InetAddress (java.net.InetAddress)2 ByteOrder (java.nio.ByteOrder)2 SSLException (javax.net.ssl.SSLException)2 GridConnectionBytesVerifyFilter (org.apache.ignite.internal.util.nio.GridConnectionBytesVerifyFilter)2 GridDirectParser (org.apache.ignite.internal.util.nio.GridDirectParser)2 GridNioMessageReaderFactory (org.apache.ignite.internal.util.nio.GridNioMessageReaderFactory)2 GridNioMessageWriterFactory (org.apache.ignite.internal.util.nio.GridNioMessageWriterFactory)2 IgnitePredicate (org.apache.ignite.lang.IgnitePredicate)2 Message (org.apache.ignite.plugin.extensions.communication.Message)2 MessageFactory (org.apache.ignite.plugin.extensions.communication.MessageFactory)2 MessageFormatter (org.apache.ignite.plugin.extensions.communication.MessageFormatter)2 IgniteSpiException (org.apache.ignite.spi.IgniteSpiException)2 HandshakeMessage (org.apache.ignite.spi.communication.tcp.messages.HandshakeMessage)2