use of alluxio.util.proto.ProtoMessage 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.util.proto.ProtoMessage in project alluxio by Alluxio.
the class RPCProtoMessage method createResponse.
/**
* Creates a response for a given status.
*
* @param code the status code
* @param message the user provided message
* @param e the cause of this error
* @param data the data buffer
* @return the message created
*/
public static RPCProtoMessage createResponse(Protocol.Status.Code code, String message, Throwable e, DataBuffer data) {
Protocol.Status status = Protocol.Status.newBuilder().setCode(code).setMessage(message).build();
if (e != null) {
Protocol.Exception.Builder builder = Protocol.Exception.newBuilder();
String className = e.getClass().getCanonicalName();
if (className != null) {
builder.setClassName(className);
}
if (e.getMessage() != null) {
builder.setMessage(e.getMessage());
}
status = status.toBuilder().setCause(builder.build()).build();
}
Protocol.Response response = Protocol.Response.newBuilder().setStatus(status).build();
return new RPCProtoMessage(new ProtoMessage(response), data);
}
use of alluxio.util.proto.ProtoMessage 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.util.proto.ProtoMessage 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));
}
use of alluxio.util.proto.ProtoMessage in project alluxio by Alluxio.
the class NettyPacketWriter method sendEOF.
/**
* Sends an empty packet to signify the EOF.
*/
private void sendEOF() {
final long pos;
mLock.lock();
try {
if (mEOFSent) {
return;
}
mEOFSent = true;
pos = mPosToQueue;
} finally {
mLock.unlock();
}
// Write the last packet.
Protocol.WriteRequest writeRequest = Protocol.WriteRequest.newBuilder().setId(mId).setOffset(pos).setSessionId(mSessionId).setTier(mTier).setType(mRequestType).build();
mChannel.writeAndFlush(new RPCProtoMessage(new ProtoMessage(writeRequest), null)).addListener(ChannelFutureListener.CLOSE_ON_FAILURE);
}
Aggregations