Search in sources :

Example 16 with ReleasableBytesReference

use of org.elasticsearch.common.bytes.ReleasableBytesReference in project crate by crate.

the class InboundAggregator method finishAggregation.

public InboundMessage finishAggregation() throws IOException {
    ensureOpen();
    final ReleasableBytesReference releasableContent;
    if (isFirstContent()) {
        releasableContent = ReleasableBytesReference.wrap(BytesArray.EMPTY);
    } else if (contentAggregation == null) {
        releasableContent = firstContent;
    } else {
        final ReleasableBytesReference[] references = contentAggregation.toArray(new ReleasableBytesReference[0]);
        final CompositeBytesReference content = new CompositeBytesReference(references);
        releasableContent = new ReleasableBytesReference(content, () -> Releasables.close(references));
    }
    final BreakerControl breakerControl = new BreakerControl(circuitBreaker);
    final InboundMessage aggregated = new InboundMessage(currentHeader, releasableContent, breakerControl);
    boolean success = false;
    try {
        if (aggregated.getHeader().needsToReadVariableHeader()) {
            aggregated.getHeader().finishParsingHeader(aggregated.openOrGetStreamInput());
            if (aggregated.getHeader().isRequest()) {
                initializeRequestState();
            }
        }
        if (isShortCircuited() == false) {
            checkBreaker(aggregated.getHeader(), aggregated.getContentLength(), breakerControl);
        }
        if (isShortCircuited()) {
            aggregated.close();
            success = true;
            return new InboundMessage(aggregated.getHeader(), aggregationException);
        } else {
            success = true;
            return aggregated;
        }
    } finally {
        resetCurrentAggregation();
        if (success == false) {
            aggregated.close();
        }
    }
}
Also used : ReleasableBytesReference(org.elasticsearch.common.bytes.ReleasableBytesReference) CompositeBytesReference(org.elasticsearch.common.bytes.CompositeBytesReference)

Example 17 with ReleasableBytesReference

use of org.elasticsearch.common.bytes.ReleasableBytesReference in project crate by crate.

the class InboundPipeline method getPendingBytes.

private ReleasableBytesReference getPendingBytes() {
    if (pending.size() == 1) {
        return pending.peekFirst().retain();
    } else {
        final ReleasableBytesReference[] bytesReferences = new ReleasableBytesReference[pending.size()];
        int index = 0;
        for (ReleasableBytesReference pendingReference : pending) {
            bytesReferences[index] = pendingReference.retain();
            ++index;
        }
        final Releasable releasable = () -> Releasables.closeWhileHandlingException(bytesReferences);
        return new ReleasableBytesReference(new CompositeBytesReference(bytesReferences), releasable);
    }
}
Also used : ReleasableBytesReference(org.elasticsearch.common.bytes.ReleasableBytesReference) CompositeBytesReference(org.elasticsearch.common.bytes.CompositeBytesReference) Releasable(org.elasticsearch.common.lease.Releasable)

Example 18 with ReleasableBytesReference

use of org.elasticsearch.common.bytes.ReleasableBytesReference in project crate by crate.

the class InboundPipeline method doHandleBytes.

public void doHandleBytes(TcpChannel channel, ReleasableBytesReference reference) throws IOException {
    channel.getChannelStats().markAccessed(relativeTimeInMillis.getAsLong());
    statsTracker.markBytesRead(reference.length());
    pending.add(reference.retain());
    final ArrayList<Object> fragments = FRAGMENT_LIST.get();
    boolean continueHandling = true;
    while (continueHandling && isClosed == false) {
        boolean continueDecoding = true;
        while (continueDecoding && pending.isEmpty() == false) {
            try (ReleasableBytesReference toDecode = getPendingBytes()) {
                final int bytesDecoded = decoder.decode(toDecode, fragments::add);
                if (bytesDecoded != 0) {
                    releasePendingBytes(bytesDecoded);
                    if (fragments.isEmpty() == false && endOfMessage(fragments.get(fragments.size() - 1))) {
                        continueDecoding = false;
                    }
                } else {
                    continueDecoding = false;
                }
            }
        }
        if (fragments.isEmpty()) {
            continueHandling = false;
        } else {
            try {
                forwardFragments(channel, fragments);
            } finally {
                for (Object fragment : fragments) {
                    if (fragment instanceof ReleasableBytesReference) {
                        ((ReleasableBytesReference) fragment).close();
                    }
                }
                fragments.clear();
            }
        }
    }
}
Also used : ReleasableBytesReference(org.elasticsearch.common.bytes.ReleasableBytesReference)

Example 19 with ReleasableBytesReference

use of org.elasticsearch.common.bytes.ReleasableBytesReference in project crate by crate.

the class InboundDecoder method internalDecode.

public int internalDecode(ReleasableBytesReference reference, Consumer<Object> fragmentConsumer) throws IOException {
    if (isOnHeader()) {
        int messageLength = TcpTransport.readMessageLength(reference);
        if (messageLength == -1) {
            return 0;
        } else if (messageLength == 0) {
            fragmentConsumer.accept(PING);
            return 6;
        } else {
            int headerBytesToRead = headerBytesToRead(reference);
            if (headerBytesToRead == 0) {
                return 0;
            } else {
                totalNetworkSize = messageLength + TcpHeader.BYTES_REQUIRED_FOR_MESSAGE_SIZE;
                Header header = readHeader(messageLength, reference);
                bytesConsumed += headerBytesToRead;
                if (header.isCompressed()) {
                    decompressor = new TransportDecompressor(recycler);
                }
                fragmentConsumer.accept(header);
                if (isDone()) {
                    finishMessage(fragmentConsumer);
                }
                return headerBytesToRead;
            }
        }
    } else {
        // There are a minimum number of bytes required to start decompression
        if (decompressor != null && decompressor.canDecompress(reference.length()) == false) {
            return 0;
        }
        int bytesToConsume = Math.min(reference.length(), totalNetworkSize - bytesConsumed);
        bytesConsumed += bytesToConsume;
        ReleasableBytesReference retainedContent;
        if (isDone()) {
            retainedContent = reference.retainedSlice(0, bytesToConsume);
        } else {
            retainedContent = reference.retain();
        }
        if (decompressor != null) {
            decompress(retainedContent);
            ReleasableBytesReference decompressed;
            while ((decompressed = decompressor.pollDecompressedPage()) != null) {
                fragmentConsumer.accept(decompressed);
            }
        } else {
            fragmentConsumer.accept(retainedContent);
        }
        if (isDone()) {
            finishMessage(fragmentConsumer);
        }
        return bytesToConsume;
    }
}
Also used : ReleasableBytesReference(org.elasticsearch.common.bytes.ReleasableBytesReference)

Example 20 with ReleasableBytesReference

use of org.elasticsearch.common.bytes.ReleasableBytesReference in project crate by crate.

the class InboundAggregatorTests method testInboundUnknownAction.

public void testInboundUnknownAction() throws IOException {
    long requestId = randomNonNegativeLong();
    Header header = new Header(randomInt(), requestId, TransportStatus.setRequest((byte) 0), Version.CURRENT);
    header.bwcNeedsToReadVariableHeader = false;
    header.actionName = unknownAction;
    // Initiate Message
    aggregator.headerReceived(header);
    BytesArray bytes = new BytesArray(randomByteArrayOfLength(10));
    final ReleasableBytesReference content = ReleasableBytesReference.wrap(bytes);
    aggregator.aggregate(content);
    content.close();
    assertEquals(0, content.refCount());
    // Signal EOS
    InboundMessage aggregated = aggregator.finishAggregation();
    assertThat(aggregated, notNullValue());
    assertTrue(aggregated.isShortCircuit());
    assertThat(aggregated.getException(), instanceOf(ActionNotFoundTransportException.class));
}
Also used : ReleasableBytesReference(org.elasticsearch.common.bytes.ReleasableBytesReference) BytesArray(org.elasticsearch.common.bytes.BytesArray)

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