Search in sources :

Example 1 with DataNettyBufferV2

use of alluxio.network.protocol.databuffer.DataNettyBufferV2 in project alluxio by Alluxio.

the class DataServerUfsBlockReadHandler method getDataBuffer.

@Override
protected DataBuffer getDataBuffer(Channel channel, long offset, int len) throws IOException {
    BlockReader blockReader = ((UfsBlockReadRequestInternal) mRequest).mBlockReader;
    // This buf is released by netty.
    ByteBuf buf = channel.alloc().buffer(len, len);
    try {
        while (buf.writableBytes() > 0 && blockReader.transferTo(buf) != -1) {
        }
        return new DataNettyBufferV2(buf);
    } catch (Throwable e) {
        buf.release();
        throw e;
    }
}
Also used : BlockReader(alluxio.worker.block.io.BlockReader) DataNettyBufferV2(alluxio.network.protocol.databuffer.DataNettyBufferV2) ByteBuf(io.netty.buffer.ByteBuf)

Example 2 with DataNettyBufferV2

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

Example 3 with DataNettyBufferV2

use of alluxio.network.protocol.databuffer.DataNettyBufferV2 in project alluxio by Alluxio.

the class RPCProtoMessage method decode.

/**
   * Decodes the message from a buffer. This method increments the refcount of the bytebuf passed
   * by 1.
   *
   * @param in the buffer
   * @param prototype a message prototype used to infer the type of the message
   * @return the message decoded
   */
public static RPCProtoMessage decode(ByteBuf in, ProtoMessage.Type prototype) {
    int length = in.readInt();
    byte[] serialized = new byte[length];
    in.readBytes(serialized);
    in.retain();
    return new RPCProtoMessage(serialized, prototype, new DataNettyBufferV2(in));
}
Also used : DataNettyBufferV2(alluxio.network.protocol.databuffer.DataNettyBufferV2)

Example 4 with DataNettyBufferV2

use of alluxio.network.protocol.databuffer.DataNettyBufferV2 in project alluxio by Alluxio.

the class DataServerBlockReadHandler method getDataBuffer.

@Override
protected DataBuffer getDataBuffer(Channel channel, long offset, int len) throws IOException {
    BlockReader blockReader = ((BlockReadRequestInternal) mRequest).mBlockReader;
    Preconditions.checkArgument(blockReader.getChannel() instanceof FileChannel, "Only FileChannel is supported!");
    switch(mTransferType) {
        case MAPPED:
            ByteBuf buf = channel.alloc().buffer(len, len);
            try {
                FileChannel fileChannel = (FileChannel) blockReader.getChannel();
                Preconditions.checkState(fileChannel.position() == offset);
                while (buf.writableBytes() > 0 && buf.writeBytes(fileChannel, buf.writableBytes()) != -1) {
                }
                return new DataNettyBufferV2(buf);
            } catch (Throwable e) {
                buf.release();
                throw e;
            }
        // intend to fall through as TRANSFER is the default type.
        case TRANSFER:
        default:
            return new DataFileChannel((FileChannel) blockReader.getChannel(), offset, len);
    }
}
Also used : DataFileChannel(alluxio.network.protocol.databuffer.DataFileChannel) FileChannel(java.nio.channels.FileChannel) BlockReader(alluxio.worker.block.io.BlockReader) DataNettyBufferV2(alluxio.network.protocol.databuffer.DataNettyBufferV2) ByteBuf(io.netty.buffer.ByteBuf) DataFileChannel(alluxio.network.protocol.databuffer.DataFileChannel)

Example 5 with DataNettyBufferV2

use of alluxio.network.protocol.databuffer.DataNettyBufferV2 in project alluxio by Alluxio.

the class NettyPacketReader method readPacket.

@Override
public DataBuffer readPacket() throws IOException {
    Preconditions.checkState(!mClosed, "PacketReader is closed while reading packets.");
    ByteBuf buf = null;
    mLock.lock();
    try {
        while (true) {
            if (mDone) {
                return null;
            }
            if (mPacketReaderException != null) {
                throw new IOException(mPacketReaderException);
            }
            buf = mPackets.poll();
            // TODO(peis): Have a better criteria to resume so that we can have fewer state changes.
            if (!tooManyPacketsPending()) {
                resume();
            }
            // Queue is empty.
            if (buf == null) {
                try {
                    if (!mNotEmptyOrFailed.await(READ_TIMEOUT_MS, TimeUnit.MILLISECONDS)) {
                        throw new IOException(String.format("Timeout while reading packet from block %d @ %s.", mId, mAddress));
                    }
                } catch (InterruptedException e) {
                    throw Throwables.propagate(e);
                }
            } else {
                if (buf.readableBytes() == 0) {
                    buf.release();
                    mDone = true;
                    return null;
                }
                mPosToRead += buf.readableBytes();
                Preconditions.checkState(mPosToRead - mStart <= mBytesToRead);
                return new DataNettyBufferV2(buf);
            }
        }
    } catch (Throwable e) {
        if (buf != null) {
            buf.release();
        }
        throw e;
    } finally {
        mLock.unlock();
    }
}
Also used : DataNettyBufferV2(alluxio.network.protocol.databuffer.DataNettyBufferV2) IOException(java.io.IOException) ByteBuf(io.netty.buffer.ByteBuf)

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