Search in sources :

Example 6 with DataNettyBufferV2

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));
}
Also used : ProtoMessage(alluxio.util.proto.ProtoMessage) RPCProtoMessage(alluxio.network.protocol.RPCProtoMessage) RPCProtoMessage(alluxio.network.protocol.RPCProtoMessage) DataNettyBufferV2(alluxio.network.protocol.databuffer.DataNettyBufferV2) IOException(java.io.IOException) Protocol(alluxio.proto.dataserver.Protocol) DataBuffer(alluxio.network.protocol.databuffer.DataBuffer)

Example 7 with DataNettyBufferV2

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;
}
Also used : DataNettyBufferV2(alluxio.network.protocol.databuffer.DataNettyBufferV2) ByteBuf(io.netty.buffer.ByteBuf) DataBuffer(alluxio.network.protocol.databuffer.DataBuffer)

Example 8 with DataNettyBufferV2

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);
}
Also used : DataNettyBufferV2(alluxio.network.protocol.databuffer.DataNettyBufferV2) ByteBuf(io.netty.buffer.ByteBuf) ByteBuffer(java.nio.ByteBuffer) DataBuffer(alluxio.network.protocol.databuffer.DataBuffer) DataFileChannel(alluxio.network.protocol.databuffer.DataFileChannel)

Example 9 with DataNettyBufferV2

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);
}
Also used : RPCProtoMessage(alluxio.network.protocol.RPCProtoMessage) ProtoMessage(alluxio.util.proto.ProtoMessage) RPCProtoMessage(alluxio.network.protocol.RPCProtoMessage) DataNettyBufferV2(alluxio.network.protocol.databuffer.DataNettyBufferV2) Protocol(alluxio.proto.dataserver.Protocol) ByteBuf(io.netty.buffer.ByteBuf) DataBuffer(alluxio.network.protocol.databuffer.DataBuffer)

Aggregations

DataNettyBufferV2 (alluxio.network.protocol.databuffer.DataNettyBufferV2)9 ByteBuf (io.netty.buffer.ByteBuf)7 DataBuffer (alluxio.network.protocol.databuffer.DataBuffer)5 RPCProtoMessage (alluxio.network.protocol.RPCProtoMessage)3 Protocol (alluxio.proto.dataserver.Protocol)3 ProtoMessage (alluxio.util.proto.ProtoMessage)3 DataFileChannel (alluxio.network.protocol.databuffer.DataFileChannel)2 BlockReader (alluxio.worker.block.io.BlockReader)2 IOException (java.io.IOException)2 ByteBuffer (java.nio.ByteBuffer)1 FileChannel (java.nio.channels.FileChannel)1