use of org.elasticsearch.common.bytes.ReleasableBytesReference in project crate by crate.
the class InboundDecoderTests method testDecode.
public void testDecode() throws IOException {
boolean isRequest = randomBoolean();
String action = "test-request";
long requestId = randomNonNegativeLong();
OutboundMessage message;
if (isRequest) {
message = new OutboundMessage.Request(new TestRequest(randomAlphaOfLength(100)), Version.CURRENT, action, requestId, false, false);
} else {
message = new OutboundMessage.Response(new TestResponse(randomAlphaOfLength(100)), Version.CURRENT, requestId, false, false);
}
final BytesReference totalBytes = message.serialize(new BytesStreamOutput());
int totalHeaderSize = TcpHeader.headerSize(Version.CURRENT) + totalBytes.getInt(TcpHeader.VARIABLE_HEADER_SIZE_POSITION);
final BytesReference messageBytes = totalBytes.slice(totalHeaderSize, totalBytes.length() - totalHeaderSize);
InboundDecoder decoder = new InboundDecoder(Version.CURRENT, PageCacheRecycler.NON_RECYCLING_INSTANCE);
final ArrayList<Object> fragments = new ArrayList<>();
final ReleasableBytesReference releasable1 = ReleasableBytesReference.wrap(totalBytes);
int bytesConsumed = decoder.decode(releasable1, fragments::add);
assertEquals(totalHeaderSize, bytesConsumed);
assertEquals(1, releasable1.refCount());
final Header header = (Header) fragments.get(0);
assertEquals(requestId, header.getRequestId());
assertEquals(Version.CURRENT, header.getVersion());
assertFalse(header.isCompressed());
assertFalse(header.isHandshake());
if (isRequest) {
assertEquals(action, header.getActionName());
assertTrue(header.isRequest());
} else {
assertTrue(header.isResponse());
}
assertFalse(header.needsToReadVariableHeader());
fragments.clear();
final BytesReference bytes2 = totalBytes.slice(bytesConsumed, totalBytes.length() - bytesConsumed);
final ReleasableBytesReference releasable2 = ReleasableBytesReference.wrap(bytes2);
int bytesConsumed2 = decoder.decode(releasable2, fragments::add);
assertEquals(totalBytes.length() - totalHeaderSize, bytesConsumed2);
final Object content = fragments.get(0);
final Object endMarker = fragments.get(1);
assertEquals(messageBytes, content);
// Ref count is incremented since the bytes are forwarded as a fragment
assertEquals(2, releasable2.refCount());
assertEquals(InboundDecoder.END_CONTENT, endMarker);
}
use of org.elasticsearch.common.bytes.ReleasableBytesReference in project crate by crate.
the class InboundPipelineTests method testEnsureBodyIsNotPrematurelyReleased.
public void testEnsureBodyIsNotPrematurelyReleased() throws IOException {
BiConsumer<TcpChannel, InboundMessage> messageHandler = (c, m) -> {
};
final StatsTracker statsTracker = new StatsTracker();
final LongSupplier millisSupplier = () -> TimeValue.nsecToMSec(System.nanoTime());
final InboundDecoder decoder = new InboundDecoder(Version.CURRENT, PageCacheRecycler.NON_RECYCLING_INSTANCE);
final Supplier<CircuitBreaker> breaker = () -> new NoopCircuitBreaker("test");
final InboundAggregator aggregator = new InboundAggregator(breaker, (Predicate<String>) action -> true);
final InboundPipeline pipeline = new InboundPipeline(statsTracker, millisSupplier, decoder, aggregator, messageHandler);
try (BytesStreamOutput streamOutput = new BytesStreamOutput()) {
String actionName = "actionName";
final Version version = Version.CURRENT;
final String value = randomAlphaOfLength(1000);
final boolean isRequest = randomBoolean();
final long requestId = randomNonNegativeLong();
OutboundMessage message;
if (isRequest) {
message = new OutboundMessage.Request(new TestRequest(value), version, actionName, requestId, false, false);
} else {
message = new OutboundMessage.Response(new TestResponse(value), version, requestId, false, false);
}
final BytesReference reference = message.serialize(streamOutput);
final int fixedHeaderSize = TcpHeader.headerSize(Version.CURRENT);
final int variableHeaderSize = reference.getInt(fixedHeaderSize - 4);
final int totalHeaderSize = fixedHeaderSize + variableHeaderSize;
final AtomicBoolean bodyReleased = new AtomicBoolean(false);
for (int i = 0; i < totalHeaderSize - 1; ++i) {
try (ReleasableBytesReference slice = ReleasableBytesReference.wrap(reference.slice(i, 1))) {
pipeline.handleBytes(new FakeTcpChannel(), slice);
}
}
final Releasable releasable = () -> bodyReleased.set(true);
final int from = totalHeaderSize - 1;
final BytesReference partHeaderPartBody = reference.slice(from, reference.length() - from - 1);
try (ReleasableBytesReference slice = new ReleasableBytesReference(partHeaderPartBody, releasable)) {
pipeline.handleBytes(new FakeTcpChannel(), slice);
}
assertFalse(bodyReleased.get());
try (ReleasableBytesReference slice = new ReleasableBytesReference(reference.slice(reference.length() - 1, 1), releasable)) {
pipeline.handleBytes(new FakeTcpChannel(), slice);
}
assertTrue(bodyReleased.get());
}
}
use of org.elasticsearch.common.bytes.ReleasableBytesReference in project crate by crate.
the class TransportDecompressorTests method testSimpleCompression.
public void testSimpleCompression() throws IOException {
try (BytesStreamOutput output = new BytesStreamOutput()) {
StreamOutput deflateStream = CompressorFactory.COMPRESSOR.streamOutput(Streams.flushOnCloseStream(output));
byte randomByte = randomByte();
deflateStream.write(randomByte);
deflateStream.close();
BytesReference bytes = output.bytes();
TransportDecompressor decompressor = new TransportDecompressor(PageCacheRecycler.NON_RECYCLING_INSTANCE);
int bytesConsumed = decompressor.decompress(bytes);
assertEquals(bytes.length(), bytesConsumed);
assertTrue(decompressor.isEOS());
ReleasableBytesReference releasableBytesReference = decompressor.pollDecompressedPage();
assertEquals(randomByte, releasableBytesReference.get(0));
releasableBytesReference.close();
}
}
use of org.elasticsearch.common.bytes.ReleasableBytesReference in project crate by crate.
the class TransportDecompressorTests method testIncrementalMultiPageCompression.
public void testIncrementalMultiPageCompression() throws IOException {
try (BytesStreamOutput output = new BytesStreamOutput()) {
StreamOutput deflateStream = CompressorFactory.COMPRESSOR.streamOutput(Streams.flushOnCloseStream(output));
for (int i = 0; i < 10000; ++i) {
deflateStream.writeInt(i);
}
deflateStream.close();
BytesReference bytes = output.bytes();
TransportDecompressor decompressor = new TransportDecompressor(PageCacheRecycler.NON_RECYCLING_INSTANCE);
int split1 = (int) (bytes.length() * 0.3);
int split2 = (int) (bytes.length() * 0.65);
BytesReference inbound1 = bytes.slice(0, split1);
BytesReference inbound2 = bytes.slice(split1, split2 - split1);
BytesReference inbound3 = bytes.slice(split2, bytes.length() - split2);
int bytesConsumed1 = decompressor.decompress(inbound1);
assertEquals(inbound1.length(), bytesConsumed1);
assertFalse(decompressor.isEOS());
int bytesConsumed2 = decompressor.decompress(inbound2);
assertEquals(inbound2.length(), bytesConsumed2);
assertFalse(decompressor.isEOS());
int bytesConsumed3 = decompressor.decompress(inbound3);
assertEquals(inbound3.length(), bytesConsumed3);
assertTrue(decompressor.isEOS());
ReleasableBytesReference reference1 = decompressor.pollDecompressedPage();
ReleasableBytesReference reference2 = decompressor.pollDecompressedPage();
ReleasableBytesReference reference3 = decompressor.pollDecompressedPage();
assertNull(decompressor.pollDecompressedPage());
CompositeBytesReference composite = new CompositeBytesReference(reference1, reference2, reference3);
assertEquals(4 * 10000, composite.length());
StreamInput streamInput = composite.streamInput();
for (int i = 0; i < 10000; ++i) {
assertEquals(i, streamInput.readInt());
}
Releasables.close(reference1, reference2, reference3);
}
}
Aggregations