Search in sources :

Example 16 with DataBuffer

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

the class RPCBlockReadResponse method decode.

/**
   * Decodes the input {@link ByteBuf} into a {@link RPCBlockReadResponse} object and returns it.
   *
   * @param in the input {@link ByteBuf}
   * @return The decoded RPCBlockReadResponse object
   */
public static RPCBlockReadResponse decode(ByteBuf in) {
    long blockId = in.readLong();
    long offset = in.readLong();
    long length = in.readLong();
    short status = in.readShort();
    DataBuffer data = null;
    if (length > 0) {
        // use DataNettyBuffer instead of DataByteBuffer to avoid copying
        data = new DataNettyBuffer(in, (int) length);
    }
    return new RPCBlockReadResponse(blockId, offset, length, data, Status.fromShort(status));
}
Also used : DataNettyBuffer(alluxio.network.protocol.databuffer.DataNettyBuffer) DataBuffer(alluxio.network.protocol.databuffer.DataBuffer)

Example 17 with DataBuffer

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

the class RPCFileReadResponse method decode.

/**
   * Decodes the input {@link ByteBuf} into a {@link RPCFileReadResponse} object and returns it.
   *
   * @param in the input {@link ByteBuf}
   * @return The decoded RPCFileReadResponse object
   */
public static RPCFileReadResponse decode(ByteBuf in) {
    long tempUfsFileId = in.readLong();
    long offset = in.readLong();
    long length = in.readLong();
    short status = in.readShort();
    DataBuffer data = null;
    if (length > 0) {
        data = new DataNettyBuffer(in, (int) length);
    }
    return new RPCFileReadResponse(tempUfsFileId, offset, length, data, Status.fromShort(status));
}
Also used : DataNettyBuffer(alluxio.network.protocol.databuffer.DataNettyBuffer) DataBuffer(alluxio.network.protocol.databuffer.DataBuffer)

Example 18 with DataBuffer

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

the class RPCMessageEncoder method encode.

@Override
protected void encode(ChannelHandlerContext ctx, RPCMessage in, List<Object> out) throws Exception {
    RPCRequest.Type type = in.getType();
    long bodyBytes = 0;
    DataBuffer payload = null;
    if (in.hasPayload()) {
        payload = in.getPayloadDataBuffer();
        bodyBytes = payload.getLength();
    }
    int lengthBytes = Longs.BYTES;
    int typeBytes = type.getEncodedLength();
    int messageBytes = in.getEncodedLength();
    int headerBytes = lengthBytes + typeBytes + messageBytes;
    long frameBytes = headerBytes + bodyBytes;
    // Write the header info into a buffer.
    // The format is: [frame length][message type][message][(optional) data]
    ByteBuf buffer = ctx.alloc().buffer();
    buffer.writeLong(frameBytes);
    type.encode(buffer);
    in.encode(buffer);
    // Output the header buffer.
    out.add(buffer);
    if (payload != null && bodyBytes > 0) {
        Object output = payload.getNettyOutput();
        Preconditions.checkArgument(output instanceof ByteBuf || output instanceof FileRegion, "The payload must be a ByteBuf or a FileRegion.");
        out.add(output);
    }
}
Also used : FileRegion(io.netty.channel.FileRegion) ByteBuf(io.netty.buffer.ByteBuf) DataBuffer(alluxio.network.protocol.databuffer.DataBuffer)

Example 19 with DataBuffer

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

the class DataServerReadHandlerTest method checkAllReadResponses.

/**
   * Checks all the read responses.
   */
protected void checkAllReadResponses(EmbeddedChannel channel, long checksumExpected) {
    boolean eof = false;
    long checksumActual = 0;
    while (!eof) {
        Object readResponse = waitForOneResponse(channel);
        if (readResponse == null) {
            Assert.fail();
            break;
        }
        DataBuffer buffer = checkReadResponse(readResponse, Protocol.Status.Code.OK);
        eof = buffer == null;
        if (buffer != null) {
            if (buffer instanceof DataNettyBufferV2) {
                ByteBuf buf = (ByteBuf) buffer.getNettyOutput();
                while (buf.readableBytes() > 0) {
                    checksumActual += BufferUtils.byteToInt(buf.readByte());
                }
                buf.release();
            } else {
                Assert.assertTrue(buffer instanceof DataFileChannel);
                ByteBuffer buf = buffer.getReadOnlyByteBuffer();
                byte[] array = new byte[buf.remaining()];
                buf.get(array);
                for (int i = 0; i < array.length; i++) {
                    checksumActual += BufferUtils.byteToInt(array[i]);
                }
            }
        }
    }
    Assert.assertEquals(checksumExpected, checksumActual);
    Assert.assertTrue(eof);
}
Also used : DataNettyBufferV2(alluxio.network.protocol.databuffer.DataNettyBufferV2) ByteBuf(io.netty.buffer.ByteBuf) ByteBuffer(java.nio.ByteBuffer) DataBuffer(alluxio.network.protocol.databuffer.DataBuffer) DataFileChannel(alluxio.network.protocol.databuffer.DataFileChannel)

Example 20 with DataBuffer

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

the class DataServerUFSFileWriteHandlerTest method buildWriteRequest.

@Override
protected RPCProtoMessage buildWriteRequest(long offset, int len) {
    Protocol.WriteRequest writeRequest = Protocol.WriteRequest.newBuilder().setId(1L).setOffset(offset).setType(Protocol.RequestType.UFS_FILE).build();
    DataBuffer buffer = null;
    if (len > 0) {
        ByteBuf buf = PooledByteBufAllocator.DEFAULT.buffer(len);
        for (int i = 0; i < len; i++) {
            byte value = (byte) (mRandom.nextInt() % Byte.MAX_VALUE);
            buf.writeByte(value);
            mChecksum += BufferUtils.byteToInt(value);
        }
        buffer = new DataNettyBufferV2(buf);
    }
    return new RPCProtoMessage(new ProtoMessage(writeRequest), buffer);
}
Also used : RPCProtoMessage(alluxio.network.protocol.RPCProtoMessage) ProtoMessage(alluxio.util.proto.ProtoMessage) RPCProtoMessage(alluxio.network.protocol.RPCProtoMessage) DataNettyBufferV2(alluxio.network.protocol.databuffer.DataNettyBufferV2) Protocol(alluxio.proto.dataserver.Protocol) ByteBuf(io.netty.buffer.ByteBuf) DataBuffer(alluxio.network.protocol.databuffer.DataBuffer)

Aggregations

DataBuffer (alluxio.network.protocol.databuffer.DataBuffer)21 IOException (java.io.IOException)7 RPCProtoMessage (alluxio.network.protocol.RPCProtoMessage)6 DataByteBuffer (alluxio.network.protocol.databuffer.DataByteBuffer)6 ByteBuf (io.netty.buffer.ByteBuf)6 DataNettyBufferV2 (alluxio.network.protocol.databuffer.DataNettyBufferV2)5 Protocol (alluxio.proto.dataserver.Protocol)5 ProtoMessage (alluxio.util.proto.ProtoMessage)5 ChannelFuture (io.netty.channel.ChannelFuture)5 ByteBuffer (java.nio.ByteBuffer)5 RPCBlockReadResponse (alluxio.network.protocol.RPCBlockReadResponse)4 BlockDoesNotExistException (alluxio.exception.BlockDoesNotExistException)3 Test (org.junit.Test)3 DataNettyBuffer (alluxio.network.protocol.databuffer.DataNettyBuffer)2 BlockReader (alluxio.worker.block.io.BlockReader)2 RPCBlockWriteResponse (alluxio.network.protocol.RPCBlockWriteResponse)1 RPCFileReadResponse (alluxio.network.protocol.RPCFileReadResponse)1 RPCFileWriteResponse (alluxio.network.protocol.RPCFileWriteResponse)1 RPCResponse (alluxio.network.protocol.RPCResponse)1 DataFileChannel (alluxio.network.protocol.databuffer.DataFileChannel)1