Search in sources :

Example 1 with Header

use of org.apache.geode.internal.tcp.MsgReader.Header in project geode by apache.

the class Connection method readAck.

/**
   * @param msToWait number of milliseconds to wait for an ack. If 0 then wait forever.
   * @param msInterval interval between checks
   * @throws SocketTimeoutException if msToWait expires.
   * @throws ConnectionException if ack is not received (fixes bug 34312)
   */
public void readAck(final int msToWait, final long msInterval, final DirectReplyProcessor processor) throws SocketTimeoutException, ConnectionException {
    if (isSocketClosed()) {
        throw new ConnectionException(LocalizedStrings.Connection_CONNECTION_IS_CLOSED.toLocalizedString());
    }
    synchronized (this.stateLock) {
        this.connectionState = STATE_READING_ACK;
    }
    boolean origSocketInUse = this.socketInUse;
    this.socketInUse = true;
    MsgReader msgReader = null;
    DMStats stats = owner.getConduit().stats;
    final Version version = getRemoteVersion();
    try {
        if (useNIO()) {
            msgReader = new NIOMsgReader(this, version);
        } else {
            msgReader = new OioMsgReader(this, version);
        }
        Header header = msgReader.readHeader();
        ReplyMessage msg;
        int len;
        if (header.getNioMessageType() == NORMAL_MSG_TYPE) {
            msg = (ReplyMessage) msgReader.readMessage(header);
            len = header.getNioMessageLength();
        } else {
            MsgDestreamer destreamer = obtainMsgDestreamer(header.getNioMessageId(), version);
            while (header.getNioMessageType() == CHUNKED_MSG_TYPE) {
                msgReader.readChunk(header, destreamer);
                header = msgReader.readHeader();
            }
            msgReader.readChunk(header, destreamer);
            msg = (ReplyMessage) destreamer.getMessage();
            releaseMsgDestreamer(header.getNioMessageId(), destreamer);
            len = destreamer.size();
        }
        // I'd really just like to call dispatchMessage here. However,
        // that call goes through a bunch of checks that knock about
        // 10% of the performance. Since this direct-ack stuff is all
        // about performance, we'll skip those checks. Skipping them
        // should be legit, because we just sent a message so we know
        // the member is already in our view, etc.
        DistributionManager dm = (DistributionManager) owner.getDM();
        msg.setBytesRead(len);
        msg.setSender(remoteAddr);
        stats.incReceivedMessages(1L);
        stats.incReceivedBytes(msg.getBytesRead());
        stats.incMessageChannelTime(msg.resetTimestamp());
        msg.process(dm, processor);
    // dispatchMessage(msg, len, false);
    } catch (MemberShunnedException e) {
    // do nothing
    } catch (SocketTimeoutException timeout) {
        throw timeout;
    } catch (IOException e) {
        final String err = LocalizedStrings.Connection_ACK_READ_IO_EXCEPTION_FOR_0.toLocalizedString(this);
        if (!isSocketClosed()) {
            if (logger.isDebugEnabled() && !isIgnorableIOException(e)) {
                logger.debug(err, e);
            }
        }
        try {
            requestClose(err + ": " + e);
        } catch (Exception ex) {
        }
        throw new ConnectionException(LocalizedStrings.Connection_UNABLE_TO_READ_DIRECT_ACK_BECAUSE_0.toLocalizedString(e));
    } catch (ConnectionException e) {
        this.owner.getConduit().getCancelCriterion().checkCancelInProgress(e);
        throw e;
    } catch (Exception e) {
        this.owner.getConduit().getCancelCriterion().checkCancelInProgress(e);
        if (!isSocketClosed()) {
            logger.fatal(LocalizedMessage.create(LocalizedStrings.Connection_ACK_READ_EXCEPTION), e);
        }
        try {
            requestClose(LocalizedStrings.Connection_ACK_READ_EXCEPTION_0.toLocalizedString(e));
        } catch (Exception ex) {
        }
        throw new ConnectionException(LocalizedStrings.Connection_UNABLE_TO_READ_DIRECT_ACK_BECAUSE_0.toLocalizedString(e));
    } finally {
        stats.incProcessedMessages(1L);
        accessed();
        this.socketInUse = origSocketInUse;
        if (this.ackTimedOut) {
            logger.info(LocalizedMessage.create(LocalizedStrings.Connection_FINISHED_WAITING_FOR_REPLY_FROM_0, new Object[] { getRemoteAddress() }));
            this.ackTimedOut = false;
        }
        if (msgReader != null) {
            msgReader.close();
        }
    }
    synchronized (stateLock) {
        this.connectionState = STATE_RECEIVED_ACK;
    }
}
Also used : DMStats(org.apache.geode.distributed.internal.DMStats) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) ReplyMessage(org.apache.geode.distributed.internal.ReplyMessage) DistributedSystemDisconnectedException(org.apache.geode.distributed.DistributedSystemDisconnectedException) CancelException(org.apache.geode.CancelException) CancelledKeyException(java.nio.channels.CancelledKeyException) InterruptedIOException(java.io.InterruptedIOException) SocketException(java.net.SocketException) CacheClosedException(org.apache.geode.cache.CacheClosedException) SocketTimeoutException(java.net.SocketTimeoutException) ConnectException(java.net.ConnectException) ClosedChannelException(java.nio.channels.ClosedChannelException) IOException(java.io.IOException) ReplyException(org.apache.geode.distributed.internal.ReplyException) ClosedSelectorException(java.nio.channels.ClosedSelectorException) SocketTimeoutException(java.net.SocketTimeoutException) Header(org.apache.geode.internal.tcp.MsgReader.Header) Version(org.apache.geode.internal.Version) DistributionManager(org.apache.geode.distributed.internal.DistributionManager)

Aggregations

IOException (java.io.IOException)1 InterruptedIOException (java.io.InterruptedIOException)1 ConnectException (java.net.ConnectException)1 SocketException (java.net.SocketException)1 SocketTimeoutException (java.net.SocketTimeoutException)1 CancelledKeyException (java.nio.channels.CancelledKeyException)1 ClosedChannelException (java.nio.channels.ClosedChannelException)1 ClosedSelectorException (java.nio.channels.ClosedSelectorException)1 CancelException (org.apache.geode.CancelException)1 CacheClosedException (org.apache.geode.cache.CacheClosedException)1 DistributedSystemDisconnectedException (org.apache.geode.distributed.DistributedSystemDisconnectedException)1 DMStats (org.apache.geode.distributed.internal.DMStats)1 DistributionManager (org.apache.geode.distributed.internal.DistributionManager)1 ReplyException (org.apache.geode.distributed.internal.ReplyException)1 ReplyMessage (org.apache.geode.distributed.internal.ReplyMessage)1 Version (org.apache.geode.internal.Version)1 Header (org.apache.geode.internal.tcp.MsgReader.Header)1