Search in sources :

Example 1 with NioDataBuffer

use of alluxio.network.protocol.databuffer.NioDataBuffer in project alluxio by Alluxio.

the class GrpcDataReader method readChunkInternal.

private DataBuffer readChunkInternal() throws IOException {
    Preconditions.checkState(!mClient.get().isShutdown(), "Data reader is closed while reading data chunks.");
    DataBuffer buffer = null;
    ReadResponse response = null;
    if (mStream instanceof GrpcDataMessageBlockingStream) {
        DataMessage<ReadResponse, DataBuffer> message = ((GrpcDataMessageBlockingStream<ReadRequest, ReadResponse>) mStream).receiveDataMessage(mDataTimeoutMs);
        if (message != null) {
            response = message.getMessage();
            buffer = message.getBuffer();
            if (buffer == null && response.hasChunk() && response.getChunk().hasData()) {
                // falls back to use chunk message for compatibility
                ByteBuffer byteBuffer = response.getChunk().getData().asReadOnlyByteBuffer();
                buffer = new NioDataBuffer(byteBuffer, byteBuffer.remaining());
            }
            Preconditions.checkState(buffer != null, "response should always contain chunk");
        }
    } else {
        response = mStream.receive(mDataTimeoutMs);
        if (response != null) {
            Preconditions.checkState(response.hasChunk() && response.getChunk().hasData(), "response should always contain chunk");
            ByteBuffer byteBuffer = response.getChunk().getData().asReadOnlyByteBuffer();
            buffer = new NioDataBuffer(byteBuffer, byteBuffer.remaining());
        }
    }
    if (response == null) {
        return null;
    }
    mPosToRead += buffer.readableBytes();
    try {
        mStream.send(mReadRequest.toBuilder().setOffsetReceived(mPosToRead).build());
    } catch (Exception e) {
        // nothing is done as the receipt is sent at best effort
        LOG.debug("Failed to send receipt of data to worker {} for request {}: {}.", mAddress, mReadRequest, e.getMessage());
    }
    Preconditions.checkState(mPosToRead - mReadRequest.getOffset() <= mReadRequest.getLength());
    return buffer;
}
Also used : ReadResponse(alluxio.grpc.ReadResponse) NioDataBuffer(alluxio.network.protocol.databuffer.NioDataBuffer) ByteBuffer(java.nio.ByteBuffer) IOException(java.io.IOException) NioDataBuffer(alluxio.network.protocol.databuffer.NioDataBuffer) DataBuffer(alluxio.network.protocol.databuffer.DataBuffer)

Example 2 with NioDataBuffer

use of alluxio.network.protocol.databuffer.NioDataBuffer in project alluxio by Alluxio.

the class LocalFileDataReader method readChunk.

@Override
public DataBuffer readChunk() throws IOException {
    if (mPos >= mEnd) {
        return null;
    }
    ByteBuffer buffer = mReader.read(mPos, Math.min(mChunkSize, mEnd - mPos));
    DataBuffer dataBuffer = new NioDataBuffer(buffer, buffer.remaining());
    mPos += dataBuffer.getLength();
    MetricsSystem.counter(MetricKey.CLIENT_BYTES_READ_LOCAL.getName()).inc(dataBuffer.getLength());
    MetricsSystem.meter(MetricKey.CLIENT_BYTES_READ_LOCAL_THROUGHPUT.getName()).mark(dataBuffer.getLength());
    return dataBuffer;
}
Also used : NioDataBuffer(alluxio.network.protocol.databuffer.NioDataBuffer) ByteBuffer(java.nio.ByteBuffer) NioDataBuffer(alluxio.network.protocol.databuffer.NioDataBuffer) DataBuffer(alluxio.network.protocol.databuffer.DataBuffer)

Example 3 with NioDataBuffer

use of alluxio.network.protocol.databuffer.NioDataBuffer in project alluxio by Alluxio.

the class BufferCachingGrpcDataReader method readChunk.

/**
 * Reads a chunk of data.
 *
 * @return a chunk of data
 */
@Nullable
@VisibleForTesting
protected DataBuffer readChunk() throws IOException {
    Preconditions.checkState(!mClient.get().isShutdown(), "Data reader is closed while reading data chunks.");
    DataBuffer buffer = null;
    ReadResponse response = null;
    response = mStream.receive(mDataTimeoutMs);
    if (response == null) {
        return null;
    }
    Preconditions.checkState(response.hasChunk() && response.getChunk().hasData(), "response should always contain chunk");
    ByteBuffer byteBuffer = response.getChunk().getData().asReadOnlyByteBuffer();
    buffer = new NioDataBuffer(byteBuffer, byteBuffer.remaining());
    mPosToRead += buffer.readableBytes();
    try {
        mStream.send(mReadRequest.toBuilder().setOffsetReceived(mPosToRead).build());
    } catch (Exception e) {
        // nothing is done as the receipt is sent at best effort
        LOG.debug("Failed to send receipt of data to worker {} for request {}", mAddress, mReadRequest, e);
    }
    Preconditions.checkState(mPosToRead - mReadRequest.getOffset() <= mReadRequest.getLength());
    return buffer;
}
Also used : ReadResponse(alluxio.grpc.ReadResponse) NioDataBuffer(alluxio.network.protocol.databuffer.NioDataBuffer) ByteBuffer(java.nio.ByteBuffer) IOException(java.io.IOException) NioDataBuffer(alluxio.network.protocol.databuffer.NioDataBuffer) DataBuffer(alluxio.network.protocol.databuffer.DataBuffer) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Nullable(javax.annotation.Nullable)

Example 4 with NioDataBuffer

use of alluxio.network.protocol.databuffer.NioDataBuffer in project alluxio by Alluxio.

the class BlockWorkerDataReader method readChunk.

@Override
public DataBuffer readChunk() throws IOException {
    if (mPos >= mEnd) {
        return null;
    }
    ByteBuffer buffer = mReader.read(mPos, Math.min(mChunkSize, mEnd - mPos));
    DataBuffer dataBuffer = new NioDataBuffer(buffer, buffer.remaining());
    mPos += dataBuffer.getLength();
    MetricsSystem.counter(MetricKey.WORKER_BYTES_READ_DIRECT.getName()).inc(dataBuffer.getLength());
    MetricsSystem.meter(MetricKey.WORKER_BYTES_READ_DIRECT_THROUGHPUT.getName()).mark(dataBuffer.getLength());
    return dataBuffer;
}
Also used : NioDataBuffer(alluxio.network.protocol.databuffer.NioDataBuffer) ByteBuffer(java.nio.ByteBuffer) NioDataBuffer(alluxio.network.protocol.databuffer.NioDataBuffer) DataBuffer(alluxio.network.protocol.databuffer.DataBuffer)

Example 5 with NioDataBuffer

use of alluxio.network.protocol.databuffer.NioDataBuffer in project alluxio by Alluxio.

the class TestBufferCachingGrpcDataReader method readChunk.

@Override
protected DataBuffer readChunk() {
    if (mTotalChunkNum == -1) {
        mTotalChunkNum = getTotalChunkNum();
    }
    if (mReadChunkNum >= mTotalChunkNum) {
        // finished reading
        return null;
    }
    ByteBuffer byteBuffer = null;
    if ((mReadChunkNum + 1) * mChunkSize <= mBlockSize) {
        // full block read
        byteBuffer = BufferUtils.getIncreasingByteBuffer(mReadChunkNum * mChunkSize, mChunkSize);
    } else {
        // partial block read for the last chunk
        byteBuffer = BufferUtils.getIncreasingByteBuffer(mReadChunkNum * mChunkSize, mBlockSize - mReadChunkNum * mChunkSize);
    }
    DataBuffer buffer = new NioDataBuffer(byteBuffer, byteBuffer.remaining());
    mPosToRead += buffer.readableBytes();
    mReadChunkNum++;
    return buffer;
}
Also used : NioDataBuffer(alluxio.network.protocol.databuffer.NioDataBuffer) ByteBuffer(java.nio.ByteBuffer) DataBuffer(alluxio.network.protocol.databuffer.DataBuffer) NioDataBuffer(alluxio.network.protocol.databuffer.NioDataBuffer)

Aggregations

NioDataBuffer (alluxio.network.protocol.databuffer.NioDataBuffer)9 DataBuffer (alluxio.network.protocol.databuffer.DataBuffer)8 ByteBuffer (java.nio.ByteBuffer)7 Nullable (javax.annotation.Nullable)3 ReadResponse (alluxio.grpc.ReadResponse)2 IOException (java.io.IOException)2 AlluxioStatusException (alluxio.exception.status.AlluxioStatusException)1 InvalidArgumentException (alluxio.exception.status.InvalidArgumentException)1 WriteRequestCommand (alluxio.grpc.WriteRequestCommand)1 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 ByteString (com.google.protobuf.ByteString)1 StatusRuntimeException (io.grpc.StatusRuntimeException)1 ByteBuf (io.netty.buffer.ByteBuf)1