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));
}
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));
}
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);
}
}
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);
}
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);
}
Aggregations