use of org.opensearch.common.bytes.ReleasableBytesReference in project OpenSearch by opensearch-project.
the class Netty4MessageChannelHandler method channelRead.
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
assert Transports.assertDefaultThreadContext(transport.getThreadPool().getThreadContext());
assert Transports.assertTransportThread();
assert msg instanceof ByteBuf : "Expected message type ByteBuf, found: " + msg.getClass();
final ByteBuf buffer = (ByteBuf) msg;
Netty4TcpChannel channel = ctx.channel().attr(Netty4Transport.CHANNEL_KEY).get();
final BytesReference wrapped = Netty4Utils.toBytesReference(buffer);
try (ReleasableBytesReference reference = new ReleasableBytesReference(wrapped, buffer::release)) {
pipeline.handleBytes(channel, reference);
}
}
use of org.opensearch.common.bytes.ReleasableBytesReference in project OpenSearch by opensearch-project.
the class TranslogWriter method writeAndReleaseOps.
private void writeAndReleaseOps(ReleasableBytesReference toWrite) throws IOException {
try (ReleasableBytesReference toClose = toWrite) {
assert writeLock.isHeldByCurrentThread();
ByteBuffer ioBuffer = DiskIoBufferPool.getIoBuffer();
BytesRefIterator iterator = toWrite.iterator();
BytesRef current;
while ((current = iterator.next()) != null) {
int currentBytesConsumed = 0;
while (currentBytesConsumed != current.length) {
int nBytesToWrite = Math.min(current.length - currentBytesConsumed, ioBuffer.remaining());
ioBuffer.put(current.bytes, current.offset + currentBytesConsumed, nBytesToWrite);
currentBytesConsumed += nBytesToWrite;
if (ioBuffer.hasRemaining() == false) {
ioBuffer.flip();
writeToFile(ioBuffer);
ioBuffer.clear();
}
}
}
ioBuffer.flip();
writeToFile(ioBuffer);
}
}
use of org.opensearch.common.bytes.ReleasableBytesReference in project OpenSearch by opensearch-project.
the class TranslogWriter method syncUpTo.
/**
* Syncs the translog up to at least the given offset unless already synced
*
* @return <code>true</code> if this call caused an actual sync operation
*/
final boolean syncUpTo(long offset) throws IOException {
if (lastSyncedCheckpoint.offset < offset && syncNeeded()) {
synchronized (syncLock) {
// only one sync/checkpoint should happen concurrently but we wait
if (lastSyncedCheckpoint.offset < offset && syncNeeded()) {
// double checked locking - we don't want to fsync unless we have to and now that we have
// the lock we should check again since if this code is busy we might have fsynced enough already
final Checkpoint checkpointToSync;
final LongArrayList flushedSequenceNumbers;
final ReleasableBytesReference toWrite;
try (ReleasableLock toClose = writeLock.acquire()) {
synchronized (this) {
ensureOpen();
checkpointToSync = getCheckpoint();
toWrite = pollOpsToWrite();
flushedSequenceNumbers = nonFsyncedSequenceNumbers;
nonFsyncedSequenceNumbers = new LongArrayList(64);
}
try {
// Write ops will release operations.
writeAndReleaseOps(toWrite);
} catch (final Exception ex) {
closeWithTragicEvent(ex);
throw ex;
}
}
// we can continue writing to the buffer etc.
try {
channel.force(false);
writeCheckpoint(checkpointChannel, checkpointPath, checkpointToSync);
} catch (final Exception ex) {
closeWithTragicEvent(ex);
throw ex;
}
flushedSequenceNumbers.forEach((LongProcedure) persistedSequenceNumberConsumer::accept);
assert lastSyncedCheckpoint.offset <= checkpointToSync.offset : "illegal state: " + lastSyncedCheckpoint.offset + " <= " + checkpointToSync.offset;
// write protected by syncLock
lastSyncedCheckpoint = checkpointToSync;
return true;
}
}
}
return false;
}
use of org.opensearch.common.bytes.ReleasableBytesReference in project OpenSearch by opensearch-project.
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 BytesReference content = CompositeBytesReference.of(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.opensearch.common.bytes.ReleasableBytesReference in project OpenSearch by opensearch-project.
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 = fragmentList.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();
}
}
}
}
Aggregations