Example usage for io.netty.buffer ByteBuf release

List of usage examples for io.netty.buffer ByteBuf release

Introduction

In this page you can find the example usage for io.netty.buffer ByteBuf release.

Prototype

boolean release();

Source Link

Document

Decreases the reference count by 1 and deallocates this object if the reference count reaches at 0 .

Usage

From source file:alluxio.client.block.stream.PacketInStream.java

License:Apache License

@Override
public int positionedRead(long pos, byte[] b, int off, int len) throws IOException {
    if (len == 0) {
        return 0;
    }// ww  w  .  ja v a 2 s . co  m
    if (pos < 0 || pos >= mLength) {
        return -1;
    }

    int lenCopy = len;
    try (PacketReader reader = mPacketReaderFactory.create(pos, len)) {
        // We try to read len bytes instead of returning after reading one packet because
        // it is not free to create/close a PacketReader.
        while (len > 0) {
            ByteBuf buf = null;
            try {
                buf = reader.readPacket();
                if (buf == null) {
                    break;
                }
                Preconditions.checkState(buf.readableBytes() <= len);
                int toRead = buf.readableBytes();
                buf.readBytes(b, off, toRead);
                len -= toRead;
                off += toRead;
            } finally {
                if (buf != null) {
                    buf.release();
                }
            }
        }
    }
    if (lenCopy == len) {
        return -1;
    }
    return lenCopy - len;
}

From source file:alluxio.client.block.stream.PacketInStream.java

License:Apache License

/**
 * Destroys a packet./* w ww.  ja  v  a2  s  .com*/
 *
 * @param packet the packet
 */
private void destroyPacket(ByteBuf packet) {
    if (packet != null) {
        packet.release();
    }
}

From source file:alluxio.client.block.stream.UfsFallbackLocalFileDataWriter.java

License:Apache License

@Override
public void writeChunk(ByteBuf chunk) throws IOException {
    if (mIsWritingToLocal) {
        long pos = mLocalFileDataWriter.pos();
        try {//from  w w w  .  j  a v a  2s .  com
            // chunk.refcount++ to ensure chunk not garbage-collected if writeChunk fails
            chunk.retain();
            // chunk.refcount-- inside regardless of exception
            mLocalFileDataWriter.writeChunk(chunk);
            // chunk.refcount-- on success
            chunk.release();
            return;
        } catch (ResourceExhaustedException e) {
            LOG.warn("Fallback to write to UFS for block {} due to a failure of insufficient space "
                    + "on the local worker: {}", mBlockId, e.getMessage());
            mIsWritingToLocal = false;
        }
        try {
            if (pos == 0) {
                // Nothing has been written to temp block, we can cancel this failed local writer and
                // cleanup the temp block.
                mLocalFileDataWriter.cancel();
            } else {
                // Note that, we can not cancel mLocalFileDataWriter now as the cancel message may
                // arrive and clean the temp block before it is written to UFS.
                mLocalFileDataWriter.flush();
            }
            // Close the block writer. We do not close the mLocalFileDataWriter to prevent the worker
            // completes the block, commit it and remove it.
            //mLocalFileDataWriter.getWriter().close();
            mGrpcDataWriter = GrpcDataWriter.create(mContext, mWorkerNetAddress, mBlockId, mBlockSize,
                    RequestType.UFS_FALLBACK_BLOCK, mOutStreamOptions);
            // Instruct the server to write the previously transferred data from temp block to UFS only
            // when there is data already written.
            if (pos > 0) {
                mGrpcDataWriter.writeFallbackInitRequest(pos);
            }
        } catch (Exception e) {
            // chunk.refcount-- on exception
            chunk.release();
            throw new IOException("Failed to switch to writing block " + mBlockId + " to UFS", e);
        }
    }
    mGrpcDataWriter.writeChunk(chunk); // refcount-- inside to release chunk
}

From source file:alluxio.client.block.stream.UfsFallbackLocalFilePacketWriter.java

License:Apache License

@Override
public void writePacket(ByteBuf packet) throws IOException {
    if (mIsWritingToLocal) {
        long pos = mLocalFilePacketWriter.pos();
        try {//from ww w.java 2s  .  com
            // packet.refcount++ to ensure packet not garbage-collected if writePacket fails
            packet.retain();
            // packet.refcount-- inside regardless of exception
            mLocalFilePacketWriter.writePacket(packet);
            // packet.refcount-- on success
            packet.release();
            return;
        } catch (ResourceExhaustedException e) {
            LOG.warn("Fallback to write to UFS for block {} due to a failure of insufficient space "
                    + "on the local worker: {}", mBlockId, e.getMessage());
            mIsWritingToLocal = false;
        }
        try {
            if (pos == 0) {
                // Nothing has been written to temp block, we can cancel this failed local writer and
                // cleanup the temp block.
                mLocalFilePacketWriter.cancel();
            } else {
                // Note that, we can not cancel mLocalFilePacketWriter now as the cancel message may
                // arrive and clean the temp block before it is written to UFS.
                mLocalFilePacketWriter.flush();
            }
            // Close the block writer. We do not close the mLocalFilePacketWriter to prevent the worker
            // completes the block, commit it and remove it.
            //mLocalFilePacketWriter.getWriter().close();
            mNettyPacketWriter = NettyPacketWriter.create(mContext, mWorkerNetAddress, mBlockId, mBlockSize,
                    Protocol.RequestType.UFS_FALLBACK_BLOCK, mOutStreamOptions);
            // Instruct the server to write the previously transferred data from temp block to UFS only
            // when there is data already written.
            if (pos > 0) {
                mNettyPacketWriter.writeFallbackInitPacket(pos);
            }
        } catch (Exception e) {
            // packet.refcount-- on exception
            packet.release();
            throw new IOException("Failed to switch to writing block " + mBlockId + " to UFS", e);
        }
    }
    mNettyPacketWriter.writePacket(packet); // refcount-- inside to release packet
}

From source file:alluxio.worker.block.UnderFileSystemBlockReaderTest.java

License:Apache License

@Test
public void transferFullBlock() throws Exception {
    mReader = UnderFileSystemBlockReader.create(mUnderFileSystemBlockMeta, 0, false, mAlluxioBlockStore);
    ByteBuf buf = PooledByteBufAllocator.DEFAULT.buffer((int) TEST_BLOCK_SIZE * 2, (int) TEST_BLOCK_SIZE * 2);
    try {/*ww w .j  a v  a2s  .  com*/
        while (buf.writableBytes() > 0 && mReader.transferTo(buf) != -1) {
        }
        Assert.assertTrue(BufferUtils.equalIncreasingByteBuffer((int) TEST_BLOCK_SIZE, (int) TEST_BLOCK_SIZE,
                buf.nioBuffer()));
        mReader.close();
    } finally {
        buf.release();
    }
}

From source file:alluxio.worker.block.UnderFileSystemBlockReaderTest.java

License:Apache License

@Test
public void transferPartialBlock() throws Exception {
    mReader = UnderFileSystemBlockReader.create(mUnderFileSystemBlockMeta, 0, false, mAlluxioBlockStore);
    ByteBuf buf = PooledByteBufAllocator.DEFAULT.buffer((int) TEST_BLOCK_SIZE / 2, (int) TEST_BLOCK_SIZE / 2);
    try {//from w w w. ja  v  a  2  s. co  m
        while (buf.writableBytes() > 0 && mReader.transferTo(buf) != -1) {
        }
        Assert.assertTrue(BufferUtils.equalIncreasingByteBuffer((int) TEST_BLOCK_SIZE,
                (int) TEST_BLOCK_SIZE / 2, buf.nioBuffer()));
        mReader.close();
    } finally {
        buf.release();
    }
}

From source file:alluxio.worker.netty.AbstractWriteHandler.java

License:Apache License

/**
 * Releases a {@link ByteBuf}./* w  w w .j  av  a  2 s  .c  o m*/
 *
 * @param buf the netty byte buffer
 */
private static void release(ByteBuf buf) {
    if (buf != null && buf != EOF && buf != CANCEL && buf != ABORT) {
        buf.release();
    }
}

From source file:alluxio.worker.netty.DataServerBlockReadHandler.java

License:Apache License

@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:/*from w  w  w.  j  av a 2  s  .  co m*/
        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;
        }
    case TRANSFER: // intend to fall through as TRANSFER is the default type.
    default:
        return new DataFileChannel((FileChannel) blockReader.getChannel(), offset, len);
    }
}

From source file:alluxio.worker.netty.DataServerReadHandlerTest.java

License:Apache License

/**
 * Checks all the read responses.//from  w ww .  j a  v  a2  s . co  m
 */
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);
}

From source file:alluxio.worker.netty.DataServerUfsBlockReadHandler.java

License:Apache License

@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 {// w w w . ja  va  2 s.  c o m
        while (buf.writableBytes() > 0 && blockReader.transferTo(buf) != -1) {
        }
        return new DataNettyBufferV2(buf);
    } catch (Throwable e) {
        buf.release();
        throw e;
    }
}