use of alluxio.network.protocol.databuffer.DataNettyBufferV2 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.DataNettyBufferV2 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.DataNettyBufferV2 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.DataNettyBufferV2 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