Merge pull request #11768 from jpountz/fix/stream_eos

Make sure messages are fully read even in case of EOS markers.
This commit is contained in:
Adrien Grand 2015-06-30 09:00:59 +02:00
commit ed561cd434
1 changed files with 76 additions and 47 deletions

View File

@ -19,6 +19,7 @@
package org.elasticsearch.transport.netty; package org.elasticsearch.transport.netty;
import org.apache.lucene.util.IOUtils;
import org.elasticsearch.Version; import org.elasticsearch.Version;
import org.elasticsearch.common.component.Lifecycle; import org.elasticsearch.common.component.Lifecycle;
import org.elasticsearch.common.compress.Compressor; import org.elasticsearch.common.compress.Compressor;
@ -90,7 +91,8 @@ public class MessageChannelHandler extends SimpleChannelUpstreamHandler {
byte status = buffer.readByte(); byte status = buffer.readByte();
Version version = Version.fromId(buffer.readInt()); Version version = Version.fromId(buffer.readInt());
StreamInput wrappedStream; StreamInput wrappedStream = null;
try {
if (TransportStatus.isCompress(status) && hasMessageBytesToRead && buffer.readable()) { if (TransportStatus.isCompress(status) && hasMessageBytesToRead && buffer.readable()) {
Compressor compressor; Compressor compressor;
try { try {
@ -113,16 +115,27 @@ public class MessageChannelHandler extends SimpleChannelUpstreamHandler {
if (TransportStatus.isRequest(status)) { if (TransportStatus.isRequest(status)) {
String action = handleRequest(ctx.getChannel(), wrappedStream, requestId, version); String action = handleRequest(ctx.getChannel(), wrappedStream, requestId, version);
if (buffer.readerIndex() != expectedIndexReader) { boolean success = false;
if (buffer.readerIndex() < expectedIndexReader) { try {
logger.warn("Message not fully read (request) for requestId [{}], action [{}], readerIndex [{}] vs expected [{}]; resetting", final int nextByte = wrappedStream.read();
requestId, action, buffer.readerIndex(), expectedIndexReader); // calling read() is useful to make sure the message is fully read, even if there is an EOS marker
} else { if (nextByte != -1) {
logger.warn("Message read past expected size (request) for requestId=[{}], action [{}], readerIndex [{}] vs expected [{}]; resetting", throw new IllegalStateException("Message not fully read (request) for requestId [" + requestId + "], action ["
requestId, action, buffer.readerIndex(), expectedIndexReader); + action + "], readerIndex [" + buffer.readerIndex() + "] vs expected [" + expectedIndexReader + "]; resetting");
} }
if (buffer.readerIndex() < expectedIndexReader) {
throw new IllegalStateException("Message is fully read (request), yet there are " + (expectedIndexReader - buffer.readerIndex()) + " remaining bytes; resetting");
}
if (buffer.readerIndex() > expectedIndexReader) {
throw new IllegalStateException("Message read past expected size (request) for requestId [" + requestId + "], action ["
+ action + "], readerIndex [" + buffer.readerIndex() + "] vs expected [" + expectedIndexReader + "]; resetting");
}
success = true;
} finally {
if (!success) {
buffer.readerIndex(expectedIndexReader); buffer.readerIndex(expectedIndexReader);
} }
}
} else { } else {
TransportResponseHandler handler = transportServiceAdapter.onResponseReceived(requestId); TransportResponseHandler handler = transportServiceAdapter.onResponseReceived(requestId);
// ignore if its null, the adapter logs it // ignore if its null, the adapter logs it
@ -136,16 +149,32 @@ public class MessageChannelHandler extends SimpleChannelUpstreamHandler {
// if its null, skip those bytes // if its null, skip those bytes
buffer.readerIndex(markedReaderIndex + size); buffer.readerIndex(markedReaderIndex + size);
} }
if (buffer.readerIndex() != expectedIndexReader) {
if (buffer.readerIndex() < expectedIndexReader) { boolean success = false;
logger.warn("Message not fully read (response) for [{}] handler {}, error [{}], resetting", requestId, handler, TransportStatus.isError(status)); try {
} else { final int nextByte = wrappedStream.read();
logger.warn("Message read past expected size (response) for [{}] handler {}, error [{}], resetting", requestId, handler, TransportStatus.isError(status)); // 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");
} }
if (buffer.readerIndex() < expectedIndexReader) {
throw new IllegalStateException("Message is fully read (response), yet there are " + (expectedIndexReader - buffer.readerIndex()) + " remaining bytes; resetting");
}
if (buffer.readerIndex() > expectedIndexReader) {
throw new IllegalStateException("Message read past expected size (response) for requestId [" + requestId + "], handler ["
+ handler + "], error [" + TransportStatus.isError(status) + "]; resetting");
}
success = true;
} finally {
if (!success) {
buffer.readerIndex(expectedIndexReader); buffer.readerIndex(expectedIndexReader);
} }
} }
wrappedStream.close(); }
} finally {
IOUtils.close(wrappedStream);
}
} }
protected void handleResponse(Channel channel, StreamInput buffer, final TransportResponseHandler handler) { protected void handleResponse(Channel channel, StreamInput buffer, final TransportResponseHandler handler) {