Search in sources :

Example 11 with DataBuffer

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

Example 12 with DataBuffer

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));
}
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 13 with DataBuffer

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

Example 14 with DataBuffer

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

Example 15 with DataBuffer

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);
}
Also used : DataByteBuffer(alluxio.network.protocol.databuffer.DataByteBuffer) RPCBlockReadResponse(alluxio.network.protocol.RPCBlockReadResponse) ByteBuffer(java.nio.ByteBuffer) DataByteBuffer(alluxio.network.protocol.databuffer.DataByteBuffer) DataBuffer(alluxio.network.protocol.databuffer.DataBuffer)

Aggregations

DataBuffer (alluxio.network.protocol.databuffer.DataBuffer)21 IOException (java.io.IOException)7 RPCProtoMessage (alluxio.network.protocol.RPCProtoMessage)6 DataByteBuffer (alluxio.network.protocol.databuffer.DataByteBuffer)6 ByteBuf (io.netty.buffer.ByteBuf)6 DataNettyBufferV2 (alluxio.network.protocol.databuffer.DataNettyBufferV2)5 Protocol (alluxio.proto.dataserver.Protocol)5 ProtoMessage (alluxio.util.proto.ProtoMessage)5 ChannelFuture (io.netty.channel.ChannelFuture)5 ByteBuffer (java.nio.ByteBuffer)5 RPCBlockReadResponse (alluxio.network.protocol.RPCBlockReadResponse)4 BlockDoesNotExistException (alluxio.exception.BlockDoesNotExistException)3 Test (org.junit.Test)3 DataNettyBuffer (alluxio.network.protocol.databuffer.DataNettyBuffer)2 BlockReader (alluxio.worker.block.io.BlockReader)2 RPCBlockWriteResponse (alluxio.network.protocol.RPCBlockWriteResponse)1 RPCFileReadResponse (alluxio.network.protocol.RPCFileReadResponse)1 RPCFileWriteResponse (alluxio.network.protocol.RPCFileWriteResponse)1 RPCResponse (alluxio.network.protocol.RPCResponse)1 DataFileChannel (alluxio.network.protocol.databuffer.DataFileChannel)1