Search in sources :

Example 6 with ReleasableBytesStreamOutput

use of org.elasticsearch.common.io.stream.ReleasableBytesStreamOutput in project elasticsearch by elastic.

the class Translog method writeOperations.

/**
     * Writes all operations in the given iterable to the given output stream including the size of the array
     * use {@link #readOperations(StreamInput)} to read it back.
     */
public static void writeOperations(StreamOutput outStream, List<Operation> toWrite) throws IOException {
    final ReleasableBytesStreamOutput out = new ReleasableBytesStreamOutput(BigArrays.NON_RECYCLING_INSTANCE);
    try {
        outStream.writeInt(toWrite.size());
        final BufferedChecksumStreamOutput checksumStreamOutput = new BufferedChecksumStreamOutput(out);
        for (Operation op : toWrite) {
            out.reset();
            final long start = out.position();
            out.skip(Integer.BYTES);
            writeOperationNoSize(checksumStreamOutput, op);
            long end = out.position();
            int operationSize = (int) (out.position() - Integer.BYTES - start);
            out.seek(start);
            out.writeInt(operationSize);
            out.seek(end);
            ReleasablePagedBytesReference bytes = out.bytes();
            bytes.writeTo(outStream);
        }
    } finally {
        Releasables.close(out.bytes());
    }
}
Also used : ReleasablePagedBytesReference(org.elasticsearch.common.bytes.ReleasablePagedBytesReference) ReleasableBytesStreamOutput(org.elasticsearch.common.io.stream.ReleasableBytesStreamOutput)

Example 7 with ReleasableBytesStreamOutput

use of org.elasticsearch.common.io.stream.ReleasableBytesStreamOutput in project elasticsearch by elastic.

the class Translog method add.

/**
     * Adds an operation to the transaction log.
     *
     * @param operation the operation to add
     * @return the location of the operation in the translog
     * @throws IOException if adding the operation to the translog resulted in an I/O exception
     */
public Location add(final Operation operation) throws IOException {
    final ReleasableBytesStreamOutput out = new ReleasableBytesStreamOutput(bigArrays);
    try {
        final BufferedChecksumStreamOutput checksumStreamOutput = new BufferedChecksumStreamOutput(out);
        final long start = out.position();
        out.skip(Integer.BYTES);
        writeOperationNoSize(checksumStreamOutput, operation);
        final long end = out.position();
        final int operationSize = (int) (end - Integer.BYTES - start);
        out.seek(start);
        out.writeInt(operationSize);
        out.seek(end);
        final ReleasablePagedBytesReference bytes = out.bytes();
        try (ReleasableLock ignored = readLock.acquire()) {
            ensureOpen();
            return current.add(bytes, operation.seqNo());
        }
    } catch (final AlreadyClosedException | IOException ex) {
        try {
            closeOnTragicEvent(ex);
        } catch (final Exception inner) {
            ex.addSuppressed(inner);
        }
        throw ex;
    } catch (final Exception e) {
        try {
            closeOnTragicEvent(e);
        } catch (final Exception inner) {
            e.addSuppressed(inner);
        }
        throw new TranslogException(shardId, "Failed to write operation [" + operation + "]", e);
    } finally {
        Releasables.close(out.bytes());
    }
}
Also used : ReleasablePagedBytesReference(org.elasticsearch.common.bytes.ReleasablePagedBytesReference) ReleasableBytesStreamOutput(org.elasticsearch.common.io.stream.ReleasableBytesStreamOutput) AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException) IOException(java.io.IOException) ReleasableLock(org.elasticsearch.common.util.concurrent.ReleasableLock) AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException) IOException(java.io.IOException) EOFException(java.io.EOFException)

Example 8 with ReleasableBytesStreamOutput

use of org.elasticsearch.common.io.stream.ReleasableBytesStreamOutput in project elasticsearch by elastic.

the class TcpTransport method sendResponse.

private void sendResponse(Version nodeVersion, Channel channel, final TransportResponse response, final long requestId, final String action, TransportResponseOptions options, byte status) throws IOException {
    if (compress) {
        options = TransportResponseOptions.builder(options).withCompress(true).build();
    }
    // TODO share some code with sendRequest
    status = TransportStatus.setResponse(status);
    ReleasableBytesStreamOutput bStream = new ReleasableBytesStreamOutput(bigArrays);
    // we wrap this in a release once since if the onRequestSent callback throws an exception
    // we might release things twice and this should be prevented
    final Releasable toRelease = Releasables.releaseOnce(() -> Releasables.close(bStream.bytes()));
    boolean addedReleaseListener = false;
    StreamOutput stream = bStream;
    try {
        if (options.compress()) {
            status = TransportStatus.setCompress(status);
            stream = CompressorFactory.COMPRESSOR.streamOutput(stream);
        }
        threadPool.getThreadContext().writeTo(stream);
        stream.setVersion(nodeVersion);
        BytesReference reference = buildMessage(requestId, status, nodeVersion, response, stream, bStream);
        final TransportResponseOptions finalOptions = options;
        Runnable onRequestSent = () -> {
            // this might be called in a different thread
            try {
                toRelease.close();
            } finally {
                transportServiceAdapter.onResponseSent(requestId, action, response, finalOptions);
            }
        };
        addedReleaseListener = internalSendMessage(channel, reference, onRequestSent);
    } finally {
        try {
            IOUtils.close(stream);
        } finally {
            if (!addedReleaseListener) {
                toRelease.close();
            }
        }
    }
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) CompositeBytesReference(org.elasticsearch.common.bytes.CompositeBytesReference) AbstractRunnable(org.elasticsearch.common.util.concurrent.AbstractRunnable) AbstractLifecycleRunnable(org.elasticsearch.common.util.concurrent.AbstractLifecycleRunnable) ReleasableBytesStreamOutput(org.elasticsearch.common.io.stream.ReleasableBytesStreamOutput) Releasable(org.elasticsearch.common.lease.Releasable) StreamOutput(org.elasticsearch.common.io.stream.StreamOutput) ReleasableBytesStreamOutput(org.elasticsearch.common.io.stream.ReleasableBytesStreamOutput) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput)

Example 9 with ReleasableBytesStreamOutput

use of org.elasticsearch.common.io.stream.ReleasableBytesStreamOutput in project elasticsearch by elastic.

the class Netty4UtilsTests method getRandomizedBytesReference.

private BytesReference getRandomizedBytesReference(int length) throws IOException {
    // we know bytes stream output always creates a paged bytes reference, we use it to create randomized content
    ReleasableBytesStreamOutput out = new ReleasableBytesStreamOutput(length, bigarrays);
    for (int i = 0; i < length; i++) {
        out.writeByte((byte) random().nextInt(1 << 8));
    }
    assertEquals(out.size(), length);
    BytesReference ref = out.bytes();
    assertEquals(ref.length(), length);
    if (randomBoolean()) {
        return new BytesArray(ref.toBytesRef());
    } else if (randomBoolean()) {
        BytesRef bytesRef = ref.toBytesRef();
        return Netty4Utils.toBytesReference(Unpooled.wrappedBuffer(bytesRef.bytes, bytesRef.offset, bytesRef.length));
    } else {
        return ref;
    }
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) BytesArray(org.elasticsearch.common.bytes.BytesArray) ReleasableBytesStreamOutput(org.elasticsearch.common.io.stream.ReleasableBytesStreamOutput) BytesRef(org.apache.lucene.util.BytesRef)

Example 10 with ReleasableBytesStreamOutput

use of org.elasticsearch.common.io.stream.ReleasableBytesStreamOutput in project elasticsearch by elastic.

the class ByteBufBytesReferenceTests method newBytesReference.

@Override
protected BytesReference newBytesReference(int length) throws IOException {
    ReleasableBytesStreamOutput out = new ReleasableBytesStreamOutput(length, bigarrays);
    for (int i = 0; i < length; i++) {
        out.writeByte((byte) random().nextInt(1 << 8));
    }
    assertEquals(out.size(), length);
    BytesReference ref = out.bytes();
    assertEquals(ref.length(), length);
    BytesRef bytesRef = ref.toBytesRef();
    final ByteBuf buffer = Unpooled.wrappedBuffer(bytesRef.bytes, bytesRef.offset, bytesRef.length);
    return Netty4Utils.toBytesReference(buffer);
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) ReleasableBytesStreamOutput(org.elasticsearch.common.io.stream.ReleasableBytesStreamOutput) ByteBuf(io.netty.buffer.ByteBuf) BytesRef(org.apache.lucene.util.BytesRef)

Aggregations

ReleasableBytesStreamOutput (org.elasticsearch.common.io.stream.ReleasableBytesStreamOutput)13 BytesReference (org.elasticsearch.common.bytes.BytesReference)4 BytesStreamOutput (org.elasticsearch.common.io.stream.BytesStreamOutput)3 EOFException (java.io.EOFException)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 AlreadyClosedException (org.apache.lucene.store.AlreadyClosedException)2 BytesRef (org.apache.lucene.util.BytesRef)2 CompositeBytesReference (org.elasticsearch.common.bytes.CompositeBytesReference)2 ReleasablePagedBytesReference (org.elasticsearch.common.bytes.ReleasablePagedBytesReference)2 StreamOutput (org.elasticsearch.common.io.stream.StreamOutput)2 Releasable (org.elasticsearch.common.lease.Releasable)2 AbstractLifecycleRunnable (org.elasticsearch.common.util.concurrent.AbstractLifecycleRunnable)2 AbstractRunnable (org.elasticsearch.common.util.concurrent.AbstractRunnable)2 ReleasableLock (org.elasticsearch.common.util.concurrent.ReleasableLock)2 ByteBuf (io.netty.buffer.ByteBuf)1 List (java.util.List)1 Version (org.elasticsearch.Version)1 BytesArray (org.elasticsearch.common.bytes.BytesArray)1 ReleasableBytesReference (org.elasticsearch.common.bytes.ReleasableBytesReference)1