use of alluxio.network.protocol.databuffer.DataBuffer 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.databuffer.DataBuffer 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.network.protocol.databuffer.DataBuffer in project alluxio by Alluxio.
the class PacketInStream method positionedRead.
@Override
public int positionedRead(long pos, byte[] b, int off, int len) throws IOException {
if (len == 0) {
return 0;
}
if (pos < 0 || pos >= mLength) {
return -1;
}
int lenCopy = len;
try (PacketReader reader = mPacketReaderFactory.create(pos, len)) {
// it is not free to create/close a PacketReader.
while (len > 0) {
DataBuffer dataBuffer = null;
try {
dataBuffer = reader.readPacket();
if (dataBuffer == null) {
break;
}
Preconditions.checkState(dataBuffer.readableBytes() <= len);
int toRead = dataBuffer.readableBytes();
dataBuffer.readBytes(b, off, toRead);
len -= toRead;
off += toRead;
} finally {
if (dataBuffer != null) {
dataBuffer.release();
}
}
}
}
if (lenCopy == len) {
return -1;
}
return lenCopy - len;
}
use of alluxio.network.protocol.databuffer.DataBuffer in project alluxio by Alluxio.
the class NettyPacketReaderTest method checkPackets.
/**
* Reads the packets from the given {@link PacketReader}.
*
* @param reader the packet reader
* @param checksumStart the start position to calculate the checksum
* @param bytesToRead bytes to read
* @return the checksum of the data read starting from checksumStart
*/
private long checkPackets(PacketReader reader, long checksumStart, long bytesToRead) throws Exception {
long pos = 0;
long checksum = 0;
while (true) {
DataBuffer packet = reader.readPacket();
if (packet == null) {
break;
}
try {
Assert.assertTrue(packet instanceof DataNettyBufferV2);
ByteBuf buf = (ByteBuf) packet.getNettyOutput();
byte[] bytes = new byte[buf.readableBytes()];
buf.readBytes(bytes);
for (int i = 0; i < bytes.length; i++) {
if (pos >= checksumStart) {
checksum += BufferUtils.byteToInt(bytes[i]);
}
pos++;
if (pos >= bytesToRead) {
return checksum;
}
}
} finally {
packet.release();
}
}
return checksum;
}
use of alluxio.network.protocol.databuffer.DataBuffer in project alluxio by Alluxio.
the class NettyRemoteBlockReaderTest method createRPCBlockReadResponse.
private RPCBlockReadResponse createRPCBlockReadResponse(RPCResponse.Status status) {
ByteBuffer byteBuffer = ByteBuffer.allocateDirect((int) LENGTH);
byteBuffer.put("alluxio".getBytes());
DataBuffer dataBuffer = new DataByteBuffer(byteBuffer, LENGTH);
return new RPCBlockReadResponse(BLOCK_ID, OFFSET, LENGTH, dataBuffer, status);
}
Aggregations