Search in sources :

Example 1 with NotCompressedException

use of org.elasticsearch.common.compress.NotCompressedException in project elasticsearch by elastic.

the class TcpTransport method messageReceived.

/**
     * This method handles the message receive part for both request and responses
     */
public final void messageReceived(BytesReference reference, Channel channel, String profileName, InetSocketAddress remoteAddress, int messageLengthBytes) throws IOException {
    final int totalMessageSize = messageLengthBytes + TcpHeader.MARKER_BYTES_SIZE + TcpHeader.MESSAGE_LENGTH_SIZE;
    transportServiceAdapter.addBytesReceived(totalMessageSize);
    // we have additional bytes to read, outside of the header
    boolean hasMessageBytesToRead = (totalMessageSize - TcpHeader.HEADER_SIZE) > 0;
    StreamInput streamIn = reference.streamInput();
    boolean success = false;
    try (ThreadContext.StoredContext tCtx = threadPool.getThreadContext().stashContext()) {
        long requestId = streamIn.readLong();
        byte status = streamIn.readByte();
        Version version = Version.fromId(streamIn.readInt());
        if (TransportStatus.isCompress(status) && hasMessageBytesToRead && streamIn.available() > 0) {
            Compressor compressor;
            try {
                final int bytesConsumed = TcpHeader.REQUEST_ID_SIZE + TcpHeader.STATUS_SIZE + TcpHeader.VERSION_ID_SIZE;
                compressor = CompressorFactory.compressor(reference.slice(bytesConsumed, reference.length() - bytesConsumed));
            } catch (NotCompressedException ex) {
                int maxToRead = Math.min(reference.length(), 10);
                StringBuilder sb = new StringBuilder("stream marked as compressed, but no compressor found, first [").append(maxToRead).append("] content bytes out of [").append(reference.length()).append("] readable bytes with message size [").append(messageLengthBytes).append("] ").append("] are [");
                for (int i = 0; i < maxToRead; i++) {
                    sb.append(reference.get(i)).append(",");
                }
                sb.append("]");
                throw new IllegalStateException(sb.toString());
            }
            streamIn = compressor.streamInput(streamIn);
        }
        if (version.isCompatible(getCurrentVersion()) == false) {
            throw new IllegalStateException("Received message from unsupported version: [" + version + "] minimal compatible version is: [" + getCurrentVersion().minimumCompatibilityVersion() + "]");
        }
        streamIn = new NamedWriteableAwareStreamInput(streamIn, namedWriteableRegistry);
        streamIn.setVersion(version);
        threadPool.getThreadContext().readHeaders(streamIn);
        if (TransportStatus.isRequest(status)) {
            handleRequest(channel, profileName, streamIn, requestId, messageLengthBytes, version, remoteAddress, status);
        } else {
            final TransportResponseHandler<?> handler;
            if (TransportStatus.isHandshake(status)) {
                handler = pendingHandshakes.remove(requestId);
            } else {
                TransportResponseHandler theHandler = transportServiceAdapter.onResponseReceived(requestId);
                if (theHandler == null && TransportStatus.isError(status)) {
                    handler = pendingHandshakes.remove(requestId);
                } else {
                    handler = theHandler;
                }
            }
            // ignore if its null, the adapter logs it
            if (handler != null) {
                if (TransportStatus.isError(status)) {
                    handlerResponseError(streamIn, handler);
                } else {
                    handleResponse(remoteAddress, streamIn, handler);
                }
                // Check the entire message has been read
                final int nextByte = streamIn.read();
                // calling read() is useful to make sure the message is fully read, even if there is an EOS marker
                if (nextByte != -1) {
                    throw new IllegalStateException("Message not fully read (response) for requestId [" + requestId + "], handler [" + handler + "], error [" + TransportStatus.isError(status) + "]; resetting");
                }
            }
        }
        success = true;
    } finally {
        if (success) {
            IOUtils.close(streamIn);
        } else {
            IOUtils.closeWhileHandlingException(streamIn);
        }
    }
}
Also used : ThreadContext(org.elasticsearch.common.util.concurrent.ThreadContext) Compressor(org.elasticsearch.common.compress.Compressor) NotCompressedException(org.elasticsearch.common.compress.NotCompressedException) Version(org.elasticsearch.Version) NamedWriteableAwareStreamInput(org.elasticsearch.common.io.stream.NamedWriteableAwareStreamInput) StreamInput(org.elasticsearch.common.io.stream.StreamInput) NamedWriteableAwareStreamInput(org.elasticsearch.common.io.stream.NamedWriteableAwareStreamInput)

Aggregations

Version (org.elasticsearch.Version)1 Compressor (org.elasticsearch.common.compress.Compressor)1 NotCompressedException (org.elasticsearch.common.compress.NotCompressedException)1 NamedWriteableAwareStreamInput (org.elasticsearch.common.io.stream.NamedWriteableAwareStreamInput)1 StreamInput (org.elasticsearch.common.io.stream.StreamInput)1 ThreadContext (org.elasticsearch.common.util.concurrent.ThreadContext)1