use of alluxio.network.protocol.RPCProtoMessage in project alluxio by Alluxio.
the class DataServerHandler method channelRead0.
@Override
public void channelRead0(final ChannelHandlerContext ctx, final RPCMessage msg) throws IOException {
LOG.debug("Enter: {}", msg);
try {
switch(msg.getType()) {
case RPC_BLOCK_READ_REQUEST:
assert msg instanceof RPCBlockReadRequest;
mBlockHandler.handleBlockReadRequest(ctx, (RPCBlockReadRequest) msg);
break;
case RPC_BLOCK_WRITE_REQUEST:
assert msg instanceof RPCBlockWriteRequest;
mBlockHandler.handleBlockWriteRequest(ctx, (RPCBlockWriteRequest) msg);
break;
case RPC_FILE_READ_REQUEST:
assert msg instanceof RPCFileReadRequest;
mUnderFileSystemHandler.handleFileReadRequest(ctx, (RPCFileReadRequest) msg);
break;
case RPC_FILE_WRITE_REQUEST:
assert msg instanceof RPCFileWriteRequest;
mUnderFileSystemHandler.handleFileWriteRequest(ctx, (RPCFileWriteRequest) msg);
break;
case RPC_UFS_BLOCK_READ_REQUEST:
assert msg instanceof RPCUnderFileSystemBlockReadRequest;
mBlockHandler.handleUnderFileSystemBlockReadRequest(ctx, (RPCUnderFileSystemBlockReadRequest) msg);
break;
case RPC_ERROR_RESPONSE:
assert msg instanceof RPCErrorResponse;
LOG.error("Received an error response from the client: " + msg.toString());
break;
case RPC_READ_REQUEST:
case RPC_WRITE_REQUEST:
case RPC_RESPONSE:
assert msg instanceof RPCProtoMessage;
ctx.fireChannelRead(msg);
break;
default:
RPCErrorResponse resp = new RPCErrorResponse(RPCResponse.Status.UNKNOWN_MESSAGE_ERROR);
ctx.writeAndFlush(resp);
// TODO(peis): Fix this. We should not throw an exception here.
throw new IllegalArgumentException("No handler implementation for " + msg.getType());
}
} catch (IllegalArgumentException | IOException e) {
LOG.warn("{}, Error={}", msg, e.getMessage());
LOG.debug("{}", msg, e);
LOG.debug("Exit (Error): {}, Error={}", msg, e.getMessage());
// Rethrow the exception to use Netty's control flow.
throw e;
}
LOG.debug("Exit (OK): {}", msg);
}
use of alluxio.network.protocol.RPCProtoMessage 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.RPCProtoMessage in project alluxio by Alluxio.
the class DataServerWriteHandler method channelRead.
@Override
public void channelRead(ChannelHandlerContext ctx, Object object) throws Exception {
if (!acceptMessage(object)) {
ctx.fireChannelRead(object);
return;
}
RPCProtoMessage msg = (RPCProtoMessage) object;
initializeRequest(msg);
// Validate msg and return error if invalid. Init variables if necessary.
String error = validateRequest(msg);
if (!error.isEmpty()) {
replyError(ctx.channel(), Protocol.Status.Code.INVALID_ARGUMENT, error, null);
return;
}
mLock.lock();
try {
DataBuffer dataBuffer = msg.getPayloadDataBuffer();
ByteBuf buf;
if (dataBuffer == null) {
buf = ctx.alloc().buffer(0, 0);
} else {
Preconditions.checkState(dataBuffer.getLength() > 0);
assert dataBuffer.getNettyOutput() instanceof ByteBuf;
buf = (ByteBuf) dataBuffer.getNettyOutput();
}
mPosToQueue += buf.readableBytes();
mPackets.offer(buf);
if (!mPacketWriterActive) {
mPacketWriterExecutor.submit(new PacketWriter(ctx));
mPacketWriterActive = true;
}
if (tooManyPacketsInFlight()) {
ctx.channel().config().setAutoRead(false);
}
} finally {
mLock.unlock();
}
}
use of alluxio.network.protocol.RPCProtoMessage in project alluxio by Alluxio.
the class NettyPacketReader method close.
@Override
public void close() {
try {
if (mDone) {
return;
}
if (!mChannel.isOpen()) {
return;
}
try {
if (!CANCEL_ENABLED) {
mChannel.close().sync();
return;
}
if (remaining() > 0) {
Protocol.ReadRequest cancelRequest = Protocol.ReadRequest.newBuilder().setId(mId).setCancel(true).setType(mRequestType).setNoCache(mNoCache).build();
mChannel.writeAndFlush(new RPCProtoMessage(new ProtoMessage(cancelRequest))).addListener(ChannelFutureListener.CLOSE_ON_FAILURE);
}
} catch (InterruptedException e) {
mChannel.close();
throw Throwables.propagate(e);
}
while (true) {
try {
DataBuffer buf = readPacket();
// A null packet indicates the end of the stream.
if (buf == null) {
return;
}
buf.release();
} catch (IOException e) {
LOG.warn("Failed to close the NettyBlockReader (block: {}, address: {}).", mId, mAddress, e);
try {
mChannel.close().sync();
} catch (InterruptedException ee) {
throw Throwables.propagate(ee);
}
return;
}
}
} finally {
if (mChannel.isOpen()) {
mChannel.pipeline().removeLast();
// Make sure "autoread" is on before realsing the channel.
resume();
}
mContext.releaseNettyChannel(mAddress, mChannel);
mClosed = true;
}
}
use of alluxio.network.protocol.RPCProtoMessage in project alluxio by Alluxio.
the class NettyPacketWriter method writePacket.
@Override
public void writePacket(final ByteBuf buf) throws IOException {
final long len;
final long offset;
mLock.lock();
try {
Preconditions.checkState(!mClosed && !mEOFSent);
Preconditions.checkArgument(buf.readableBytes() <= PACKET_SIZE);
while (true) {
if (mPacketWriteException != null) {
throw new IOException(mPacketWriteException);
}
if (!tooManyPacketsInFlight()) {
offset = mPosToQueue;
mPosToQueue += buf.readableBytes();
len = buf.readableBytes();
break;
}
try {
if (!mBufferNotFullOrFailed.await(WRITE_TIMEOUT_MS, TimeUnit.MILLISECONDS)) {
throw new IOException(String.format("Timeout to write packet to %d @ %s.", mId, mAddress));
}
} catch (InterruptedException e) {
throw Throwables.propagate(e);
}
}
} catch (Throwable e) {
buf.release();
throw e;
} finally {
mLock.unlock();
}
Protocol.WriteRequest writeRequest = Protocol.WriteRequest.newBuilder().setId(mId).setOffset(offset).setSessionId(mSessionId).setTier(mTier).setType(mRequestType).build();
DataBuffer dataBuffer = new DataNettyBufferV2(buf);
mChannel.writeAndFlush(new RPCProtoMessage(new ProtoMessage(writeRequest), dataBuffer)).addListener(new WriteListener(offset + len));
}
Aggregations