Search in sources :

Example 26 with ReleasableBytesReference

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);
}
Also used : ReleasableBytesReference(org.elasticsearch.common.bytes.ReleasableBytesReference) BytesReference(org.elasticsearch.common.bytes.BytesReference) ReleasableBytesReference(org.elasticsearch.common.bytes.ReleasableBytesReference) ArrayList(java.util.ArrayList) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput)

Example 27 with ReleasableBytesReference

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());
    }
}
Also used : Tuple(io.crate.common.collections.Tuple) LongSupplier(java.util.function.LongSupplier) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) BytesArray(org.elasticsearch.common.bytes.BytesArray) CircuitBreaker(org.elasticsearch.common.breaker.CircuitBreaker) BiConsumer(java.util.function.BiConsumer) NoopCircuitBreaker(org.elasticsearch.common.breaker.NoopCircuitBreaker) ESTestCase(org.elasticsearch.test.ESTestCase) Streams(io.crate.common.io.Streams) Releasable(org.elasticsearch.common.lease.Releasable) Predicate(java.util.function.Predicate) PageCacheRecycler(org.elasticsearch.common.util.PageCacheRecycler) IOException(java.io.IOException) BytesReference(org.elasticsearch.common.bytes.BytesReference) TestCircuitBreaker(org.elasticsearch.common.breaker.TestCircuitBreaker) Objects(java.util.Objects) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) List(java.util.List) Version(org.elasticsearch.Version) CircuitBreakingException(org.elasticsearch.common.breaker.CircuitBreakingException) TimeValue(io.crate.common.unit.TimeValue) ReleasableBytesReference(org.elasticsearch.common.bytes.ReleasableBytesReference) CircuitBreaker(org.elasticsearch.common.breaker.CircuitBreaker) NoopCircuitBreaker(org.elasticsearch.common.breaker.NoopCircuitBreaker) TestCircuitBreaker(org.elasticsearch.common.breaker.TestCircuitBreaker) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput) Version(org.elasticsearch.Version) NoopCircuitBreaker(org.elasticsearch.common.breaker.NoopCircuitBreaker) BytesReference(org.elasticsearch.common.bytes.BytesReference) ReleasableBytesReference(org.elasticsearch.common.bytes.ReleasableBytesReference) ReleasableBytesReference(org.elasticsearch.common.bytes.ReleasableBytesReference) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Releasable(org.elasticsearch.common.lease.Releasable) LongSupplier(java.util.function.LongSupplier)

Example 28 with ReleasableBytesReference

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();
    }
}
Also used : ReleasableBytesReference(org.elasticsearch.common.bytes.ReleasableBytesReference) BytesReference(org.elasticsearch.common.bytes.BytesReference) CompositeBytesReference(org.elasticsearch.common.bytes.CompositeBytesReference) ReleasableBytesReference(org.elasticsearch.common.bytes.ReleasableBytesReference) StreamOutput(org.elasticsearch.common.io.stream.StreamOutput) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput)

Example 29 with ReleasableBytesReference

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);
    }
}
Also used : ReleasableBytesReference(org.elasticsearch.common.bytes.ReleasableBytesReference) BytesReference(org.elasticsearch.common.bytes.BytesReference) CompositeBytesReference(org.elasticsearch.common.bytes.CompositeBytesReference) ReleasableBytesReference(org.elasticsearch.common.bytes.ReleasableBytesReference) CompositeBytesReference(org.elasticsearch.common.bytes.CompositeBytesReference) StreamInput(org.elasticsearch.common.io.stream.StreamInput) StreamOutput(org.elasticsearch.common.io.stream.StreamOutput) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput)

Aggregations

ReleasableBytesReference (org.elasticsearch.common.bytes.ReleasableBytesReference)29 BytesReference (org.elasticsearch.common.bytes.BytesReference)16 BytesStreamOutput (org.elasticsearch.common.io.stream.BytesStreamOutput)13 ArrayList (java.util.ArrayList)11 Version (org.elasticsearch.Version)10 IOException (java.io.IOException)8 BytesArray (org.elasticsearch.common.bytes.BytesArray)7 CompositeBytesReference (org.elasticsearch.common.bytes.CompositeBytesReference)5 Releasable (org.elasticsearch.common.lease.Releasable)5 CircuitBreakingException (org.elasticsearch.common.breaker.CircuitBreakingException)4 Tuple (io.crate.common.collections.Tuple)3 Streams (io.crate.common.io.Streams)3 TimeValue (io.crate.common.unit.TimeValue)3 List (java.util.List)3 Objects (java.util.Objects)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 AtomicLong (java.util.concurrent.atomic.AtomicLong)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 BiConsumer (java.util.function.BiConsumer)3 LongSupplier (java.util.function.LongSupplier)3