Search in sources :

Example 1 with ReleasableBytesReference

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);
    }
}
Also used : BytesReference(org.opensearch.common.bytes.BytesReference) ReleasableBytesReference(org.opensearch.common.bytes.ReleasableBytesReference) ReleasableBytesReference(org.opensearch.common.bytes.ReleasableBytesReference) ByteBuf(io.netty.buffer.ByteBuf)

Example 2 with ReleasableBytesReference

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);
    }
}
Also used : ReleasableBytesReference(org.opensearch.common.bytes.ReleasableBytesReference) BytesRefIterator(org.apache.lucene.util.BytesRefIterator) ByteBuffer(java.nio.ByteBuffer) BytesRef(org.apache.lucene.util.BytesRef)

Example 3 with ReleasableBytesReference

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;
}
Also used : ReleasableBytesReference(org.opensearch.common.bytes.ReleasableBytesReference) LongArrayList(com.carrotsearch.hppc.LongArrayList) ReleasableLock(org.opensearch.common.util.concurrent.ReleasableLock) AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException) IOException(java.io.IOException)

Example 4 with ReleasableBytesReference

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();
        }
    }
}
Also used : BytesReference(org.opensearch.common.bytes.BytesReference) CompositeBytesReference(org.opensearch.common.bytes.CompositeBytesReference) ReleasableBytesReference(org.opensearch.common.bytes.ReleasableBytesReference) ReleasableBytesReference(org.opensearch.common.bytes.ReleasableBytesReference)

Example 5 with ReleasableBytesReference

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();
            }
        }
    }
}
Also used : ReleasableBytesReference(org.opensearch.common.bytes.ReleasableBytesReference)

Aggregations

ReleasableBytesReference (org.opensearch.common.bytes.ReleasableBytesReference)32 BytesReference (org.opensearch.common.bytes.BytesReference)20 BytesStreamOutput (org.opensearch.common.io.stream.BytesStreamOutput)13 ArrayList (java.util.ArrayList)11 Version (org.opensearch.Version)10 IOException (java.io.IOException)7 BytesArray (org.opensearch.common.bytes.BytesArray)7 ThreadContext (org.opensearch.common.util.concurrent.ThreadContext)6 CompositeBytesReference (org.opensearch.common.bytes.CompositeBytesReference)5 Releasable (org.opensearch.common.lease.Releasable)5 LegacyESVersion (org.opensearch.LegacyESVersion)4 CircuitBreakingException (org.opensearch.common.breaker.CircuitBreakingException)4 Collections (java.util.Collections)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