Search in sources :

Example 6 with ReadResponse

use of alluxio.grpc.ReadResponse 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 7 with ReadResponse

use of alluxio.grpc.ReadResponse in project alluxio by Alluxio.

the class BlockReadHandlerTest method checkAllReadResponses.

/**
 * Checks all the read responses.
 */
private void checkAllReadResponses(List<ReadResponse> responses, long checksumExpected) throws Exception {
    long checksumActual = 0;
    CommonUtils.waitFor("response", () -> mResponseCompleted || mError != null, WaitForOptions.defaults().setTimeoutMs(Constants.MINUTE_MS));
    for (ReadResponse readResponse : responses) {
        if (readResponse == null) {
            Assert.fail();
            break;
        }
        assertTrue(readResponse.hasChunk());
        assertTrue(readResponse.getChunk().hasData());
        ByteString buffer = readResponse.getChunk().getData();
        if (buffer != null) {
            for (byte b : buffer) {
                checksumActual += BufferUtils.byteToInt(b);
            }
        }
    }
    assertEquals(checksumExpected, checksumActual);
}
Also used : ReadResponse(alluxio.grpc.ReadResponse) ByteString(com.google.protobuf.ByteString)

Aggregations

ReadResponse (alluxio.grpc.ReadResponse)7 ReadRequest (alluxio.grpc.ReadRequest)3 IOException (java.io.IOException)3 DataBuffer (alluxio.network.protocol.databuffer.DataBuffer)2 NioDataBuffer (alluxio.network.protocol.databuffer.NioDataBuffer)2 WorkerNetAddress (alluxio.wire.WorkerNetAddress)2 ByteBuffer (java.nio.ByteBuffer)2 Before (org.junit.Before)2 AlluxioConfiguration (alluxio.conf.AlluxioConfiguration)1 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 ByteString (com.google.protobuf.ByteString)1 ClientCallStreamObserver (io.grpc.stub.ClientCallStreamObserver)1 StreamObserver (io.grpc.stub.StreamObserver)1 ArrayList (java.util.ArrayList)1 Nullable (javax.annotation.Nullable)1