use of alluxio.network.protocol.databuffer.DataNettyBufferV2 in project alluxio by Alluxio.
the class DataServerUfsBlockReadHandler method getDataBuffer.
@Override
protected DataBuffer getDataBuffer(Channel channel, long offset, int len) throws IOException {
BlockReader blockReader = ((UfsBlockReadRequestInternal) mRequest).mBlockReader;
// This buf is released by netty.
ByteBuf buf = channel.alloc().buffer(len, len);
try {
while (buf.writableBytes() > 0 && blockReader.transferTo(buf) != -1) {
}
return new DataNettyBufferV2(buf);
} catch (Throwable e) {
buf.release();
throw e;
}
}
use of alluxio.network.protocol.databuffer.DataNettyBufferV2 in project alluxio by Alluxio.
the class DataServerBlockWriteHandlerTest method buildWriteRequest.
@Override
protected RPCProtoMessage buildWriteRequest(long offset, int len) {
Protocol.WriteRequest writeRequest = Protocol.WriteRequest.newBuilder().setId(1L).setOffset(offset).setSessionId(1L).setType(Protocol.RequestType.ALLUXIO_BLOCK).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);
}
use of alluxio.network.protocol.databuffer.DataNettyBufferV2 in project alluxio by Alluxio.
the class RPCProtoMessage method decode.
/**
* Decodes the message from a buffer. This method increments the refcount of the bytebuf passed
* by 1.
*
* @param in the buffer
* @param prototype a message prototype used to infer the type of the message
* @return the message decoded
*/
public static RPCProtoMessage decode(ByteBuf in, ProtoMessage.Type prototype) {
int length = in.readInt();
byte[] serialized = new byte[length];
in.readBytes(serialized);
in.retain();
return new RPCProtoMessage(serialized, prototype, new DataNettyBufferV2(in));
}
use of alluxio.network.protocol.databuffer.DataNettyBufferV2 in project alluxio by Alluxio.
the class DataServerBlockReadHandler method getDataBuffer.
@Override
protected DataBuffer getDataBuffer(Channel channel, long offset, int len) throws IOException {
BlockReader blockReader = ((BlockReadRequestInternal) mRequest).mBlockReader;
Preconditions.checkArgument(blockReader.getChannel() instanceof FileChannel, "Only FileChannel is supported!");
switch(mTransferType) {
case MAPPED:
ByteBuf buf = channel.alloc().buffer(len, len);
try {
FileChannel fileChannel = (FileChannel) blockReader.getChannel();
Preconditions.checkState(fileChannel.position() == offset);
while (buf.writableBytes() > 0 && buf.writeBytes(fileChannel, buf.writableBytes()) != -1) {
}
return new DataNettyBufferV2(buf);
} catch (Throwable e) {
buf.release();
throw e;
}
// intend to fall through as TRANSFER is the default type.
case TRANSFER:
default:
return new DataFileChannel((FileChannel) blockReader.getChannel(), offset, len);
}
}
use of alluxio.network.protocol.databuffer.DataNettyBufferV2 in project alluxio by Alluxio.
the class NettyPacketReader method readPacket.
@Override
public DataBuffer readPacket() throws IOException {
Preconditions.checkState(!mClosed, "PacketReader is closed while reading packets.");
ByteBuf buf = null;
mLock.lock();
try {
while (true) {
if (mDone) {
return null;
}
if (mPacketReaderException != null) {
throw new IOException(mPacketReaderException);
}
buf = mPackets.poll();
// TODO(peis): Have a better criteria to resume so that we can have fewer state changes.
if (!tooManyPacketsPending()) {
resume();
}
// Queue is empty.
if (buf == null) {
try {
if (!mNotEmptyOrFailed.await(READ_TIMEOUT_MS, TimeUnit.MILLISECONDS)) {
throw new IOException(String.format("Timeout while reading packet from block %d @ %s.", mId, mAddress));
}
} catch (InterruptedException e) {
throw Throwables.propagate(e);
}
} else {
if (buf.readableBytes() == 0) {
buf.release();
mDone = true;
return null;
}
mPosToRead += buf.readableBytes();
Preconditions.checkState(mPosToRead - mStart <= mBytesToRead);
return new DataNettyBufferV2(buf);
}
}
} catch (Throwable e) {
if (buf != null) {
buf.release();
}
throw e;
} finally {
mLock.unlock();
}
}
Aggregations