Search in sources :

Example 6 with NioDataBuffer

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

the class AbstractWriteHandler method write.

/**
 * Handles write request.
 *
 * @param writeRequest the request from the client
 */
public void write(WriteRequest writeRequest) {
    if (!tryAcquireSemaphore()) {
        return;
    }
    mSerializingExecutor.execute(() -> {
        try {
            if (mContext == null) {
                LOG.debug("Received write request {}.", RpcSensitiveConfigMask.CREDENTIAL_FIELD_MASKER.maskObjects(LOG, writeRequest));
                try {
                    mContext = createRequestContext(writeRequest);
                } catch (Exception e) {
                    // abort() assumes context is initialized.
                    // Reply with the error in order to prevent clients getting stuck.
                    replyError(new Error(AlluxioStatusException.fromThrowable(e), true));
                    throw e;
                }
            } else {
                Preconditions.checkState(!mContext.isDoneUnsafe(), "invalid request after write request is completed.");
            }
            if (mContext.isDoneUnsafe() || mContext.getError() != null) {
                return;
            }
            validateWriteRequest(writeRequest);
            if (writeRequest.hasCommand()) {
                WriteRequestCommand command = writeRequest.getCommand();
                if (command.getFlush()) {
                    flush();
                } else {
                    handleCommand(command, mContext);
                }
            } else {
                Preconditions.checkState(writeRequest.hasChunk(), "write request is missing data chunk in non-command message");
                ByteString data = writeRequest.getChunk().getData();
                Preconditions.checkState(data != null && data.size() > 0, "invalid data size from write request message");
                writeData(new NioDataBuffer(data.asReadOnlyByteBuffer(), data.size()));
            }
        } catch (Exception e) {
            LogUtils.warnWithException(LOG, "Exception occurred while processing write request {}.", writeRequest, e);
            abort(new Error(AlluxioStatusException.fromThrowable(e), true));
        } finally {
            mSemaphore.release();
        }
    });
}
Also used : WriteRequestCommand(alluxio.grpc.WriteRequestCommand) ByteString(com.google.protobuf.ByteString) NioDataBuffer(alluxio.network.protocol.databuffer.NioDataBuffer) AlluxioStatusException(alluxio.exception.status.AlluxioStatusException) InvalidArgumentException(alluxio.exception.status.InvalidArgumentException) StatusRuntimeException(io.grpc.StatusRuntimeException)

Example 7 with NioDataBuffer

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

the class SharedGrpcDataReader method readChunk.

@Override
@Nullable
public DataBuffer readChunk() throws IOException {
    int index = (int) (mPosToRead / mChunkSize);
    DataBuffer chunk = mCachedDataReader.readChunk(index);
    if (chunk == null) {
        return null;
    }
    ByteBuffer bb = chunk.getReadOnlyByteBuffer();
    // Force to align to chunk size
    bb.position((int) (mPosToRead % mChunkSize));
    mPosToRead += mChunkSize - mPosToRead % mChunkSize;
    return new NioDataBuffer(bb, bb.remaining());
}
Also used : NioDataBuffer(alluxio.network.protocol.databuffer.NioDataBuffer) ByteBuffer(java.nio.ByteBuffer) NioDataBuffer(alluxio.network.protocol.databuffer.NioDataBuffer) DataBuffer(alluxio.network.protocol.databuffer.DataBuffer) Nullable(javax.annotation.Nullable)

Example 8 with NioDataBuffer

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

the class TestDataReader method readChunk.

@Override
@Nullable
public DataBuffer readChunk() {
    if (mPos >= mEnd || mPos >= mData.length) {
        return null;
    }
    int bytesToRead = (int) (Math.min(Math.min(mChunkSize, mEnd - mPos), mData.length - mPos));
    ByteBuffer buffer = ByteBuffer.wrap(mData, (int) mPos, bytesToRead);
    DataBuffer dataBuffer = new NioDataBuffer(buffer, buffer.remaining());
    mPos += dataBuffer.getLength();
    return dataBuffer;
}
Also used : NioDataBuffer(alluxio.network.protocol.databuffer.NioDataBuffer) ByteBuffer(java.nio.ByteBuffer) DataBuffer(alluxio.network.protocol.databuffer.DataBuffer) NioDataBuffer(alluxio.network.protocol.databuffer.NioDataBuffer) Nullable(javax.annotation.Nullable)

Example 9 with NioDataBuffer

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

the class GrpcDataReaderTest method checkChunks.

/**
 * Reads the chunks from the given {@link DataReader}.
 *
 * @param reader the data reader
 * @param checksumStart the start position to calculate the checksum
 * @param bytesToRead bytes to read
 * @return the checksum of the data read starting from checksumStart
 */
private long checkChunks(DataReader reader, long checksumStart, long bytesToRead) throws Exception {
    long pos = 0;
    long checksum = 0;
    while (true) {
        DataBuffer chunk = reader.readChunk();
        if (chunk == null) {
            break;
        }
        try {
            assertTrue(chunk instanceof NioDataBuffer);
            ByteBuf buf = (ByteBuf) chunk.getNettyOutput();
            byte[] bytes = new byte[buf.readableBytes()];
            buf.readBytes(bytes);
            for (int i = 0; i < bytes.length; i++) {
                if (pos >= checksumStart) {
                    checksum += BufferUtils.byteToInt(bytes[i]);
                }
                pos++;
                if (pos >= bytesToRead) {
                    return checksum;
                }
            }
        } finally {
            chunk.release();
        }
    }
    return checksum;
}
Also used : NioDataBuffer(alluxio.network.protocol.databuffer.NioDataBuffer) ByteBuf(io.netty.buffer.ByteBuf) NioDataBuffer(alluxio.network.protocol.databuffer.NioDataBuffer) DataBuffer(alluxio.network.protocol.databuffer.DataBuffer)

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