Search in sources :

Example 1 with GridSelectorNioSessionImpl

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

the class InboundConnectionHandler method onMessage.

/**
 * {@inheritDoc}
 */
@Override
public void onMessage(final GridNioSession ses, Message msg) {
    Span span = MTC.span();
    span.addLog(() -> "Communication received");
    span.addTag(SpanTags.MESSAGE, () -> traceName(msg));
    ConnectionKey connKey = ses.meta(CONN_IDX_META);
    if (connKey == null) {
        assert ses.accepted() : ses;
        if (!connectGate.tryEnter()) {
            if (log.isDebugEnabled())
                log.debug("Close incoming connection, failed to enter gateway.");
            ses.send(new RecoveryLastReceivedMessage(NODE_STOPPING)).listen(fut -> ses.close());
            return;
        }
        try {
            onFirstMessage(ses, msg);
        } finally {
            connectGate.leave();
        }
    } else {
        if (isChannelConnIdx(connKey.connectionIndex())) {
            if (ses.meta(CHANNEL_FUT_META) == null)
                nioSrvWrapper.onChannelCreate((GridSelectorNioSessionImpl) ses, connKey, msg);
            else {
                GridFutureAdapter<Channel> fut = ses.meta(CHANNEL_FUT_META);
                GridSelectorNioSessionImpl ses0 = (GridSelectorNioSessionImpl) ses;
                ses0.closeSocketOnSessionClose(false);
                ses0.close().listen(f -> {
                    if (f.error() != null) {
                        fut.onDone(f.error());
                        return;
                    }
                    fut.onDone(ses0.key().channel());
                });
            }
            return;
        }
        Object consistentId = ses.meta(CONSISTENT_ID_META);
        assert consistentId != null;
        if (msg instanceof RecoveryLastReceivedMessage) {
            metricsLsnr.onMessageReceived(msg, consistentId);
            GridNioRecoveryDescriptor recovery = ses.outRecoveryDescriptor();
            if (recovery != null) {
                RecoveryLastReceivedMessage msg0 = (RecoveryLastReceivedMessage) msg;
                if (log.isDebugEnabled()) {
                    log.debug("Received recovery acknowledgement [rmtNode=" + connKey.nodeId() + ", connIdx=" + connKey.connectionIndex() + ", rcvCnt=" + msg0.received() + ']');
                }
                recovery.ackReceived(msg0.received());
            }
            return;
        } else {
            GridNioRecoveryDescriptor recovery = ses.inRecoveryDescriptor();
            if (recovery != null) {
                long rcvCnt = recovery.onReceived();
                if (rcvCnt % cfg.ackSendThreshold() == 0) {
                    if (log.isDebugEnabled()) {
                        log.debug("Send recovery acknowledgement [rmtNode=" + connKey.nodeId() + ", connIdx=" + connKey.connectionIndex() + ", rcvCnt=" + rcvCnt + ']');
                    }
                    ses.systemMessage(new RecoveryLastReceivedMessage(rcvCnt));
                    recovery.lastAcknowledged(rcvCnt);
                }
            } else if (connKey.dummy()) {
                assert msg instanceof NodeIdMessage : msg;
                TcpCommunicationNodeConnectionCheckFuture fut = ses.meta(SES_FUT_META);
                assert fut != null : msg;
                fut.onConnected(U.bytesToUuid(((NodeIdMessage) msg).nodeIdBytes(), 0));
                nioSrvWrapper.nio().closeFromWorkerThread(ses);
                return;
            }
        }
        metricsLsnr.onMessageReceived(msg, consistentId);
        IgniteRunnable c;
        if (cfg.messageQueueLimit() > 0) {
            GridNioMessageTracker tracker = ses.meta(TRACKER_META);
            if (tracker == null) {
                GridNioMessageTracker old = ses.addMeta(TRACKER_META, tracker = new GridNioMessageTracker(ses, cfg.messageQueueLimit()));
                assert old == null;
            }
            tracker.onMessageReceived();
            c = tracker;
        } else
            c = NOOP;
        lsnr.onMessage(connKey.nodeId(), msg, c);
    }
}
Also used : RecoveryLastReceivedMessage(org.apache.ignite.spi.communication.tcp.messages.RecoveryLastReceivedMessage) Channel(java.nio.channels.Channel) Span(org.apache.ignite.internal.processors.tracing.Span) GridNioMessageTracker(org.apache.ignite.internal.util.nio.GridNioMessageTracker) IgniteRunnable(org.apache.ignite.lang.IgniteRunnable) NodeIdMessage(org.apache.ignite.spi.communication.tcp.messages.NodeIdMessage) GridNioRecoveryDescriptor(org.apache.ignite.internal.util.nio.GridNioRecoveryDescriptor) GridSelectorNioSessionImpl(org.apache.ignite.internal.util.nio.GridSelectorNioSessionImpl)

Aggregations

Channel (java.nio.channels.Channel)1 Span (org.apache.ignite.internal.processors.tracing.Span)1 GridNioMessageTracker (org.apache.ignite.internal.util.nio.GridNioMessageTracker)1 GridNioRecoveryDescriptor (org.apache.ignite.internal.util.nio.GridNioRecoveryDescriptor)1 GridSelectorNioSessionImpl (org.apache.ignite.internal.util.nio.GridSelectorNioSessionImpl)1 IgniteRunnable (org.apache.ignite.lang.IgniteRunnable)1 NodeIdMessage (org.apache.ignite.spi.communication.tcp.messages.NodeIdMessage)1 RecoveryLastReceivedMessage (org.apache.ignite.spi.communication.tcp.messages.RecoveryLastReceivedMessage)1