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();
}
}
}
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);
}
}
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();
}
}
}
}
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;
}
}
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));
}
Aggregations