Search in sources :

Example 16 with ByteBuf

use of io.netty.buffer.ByteBuf in project hbase by apache.

the class FanOutOneBlockAsyncDFSOutputHelper method requestWriteBlock.

private static void requestWriteBlock(Channel channel, Enum<?> storageType, OpWriteBlockProto.Builder writeBlockProtoBuilder) throws IOException {
    OpWriteBlockProto proto = STORAGE_TYPE_SETTER.set(writeBlockProtoBuilder, storageType).build();
    int protoLen = proto.getSerializedSize();
    ByteBuf buffer = channel.alloc().buffer(3 + CodedOutputStream.computeRawVarint32Size(protoLen) + protoLen);
    buffer.writeShort(DataTransferProtocol.DATA_TRANSFER_VERSION);
    buffer.writeByte(Op.WRITE_BLOCK.code);
    proto.writeDelimitedTo(new ByteBufOutputStream(buffer));
    channel.writeAndFlush(buffer);
}
Also used : ByteBufOutputStream(io.netty.buffer.ByteBufOutputStream) OpWriteBlockProto(org.apache.hadoop.hdfs.protocol.proto.DataTransferProtos.OpWriteBlockProto) ByteBuf(io.netty.buffer.ByteBuf)

Example 17 with ByteBuf

use of io.netty.buffer.ByteBuf in project hbase by apache.

the class FanOutOneBlockAsyncDFSOutput method flushBuffer.

private Promise<Void> flushBuffer(ByteBuf dataBuf, long nextPacketOffsetInBlock, boolean syncBlock) {
    int dataLen = dataBuf.readableBytes();
    int chunkLen = summer.getBytesPerChecksum();
    int trailingPartialChunkLen = dataLen % chunkLen;
    int numChecks = dataLen / chunkLen + (trailingPartialChunkLen != 0 ? 1 : 0);
    int checksumLen = numChecks * summer.getChecksumSize();
    ByteBuf checksumBuf = alloc.directBuffer(checksumLen);
    summer.calculateChunkedSums(dataBuf.nioBuffer(), checksumBuf.nioBuffer(0, checksumLen));
    checksumBuf.writerIndex(checksumLen);
    PacketHeader header = new PacketHeader(4 + checksumLen + dataLen, nextPacketOffsetInBlock, nextPacketSeqno, false, dataLen, syncBlock);
    int headerLen = header.getSerializedSize();
    ByteBuf headerBuf = alloc.buffer(headerLen);
    header.putInBuffer(headerBuf.nioBuffer(0, headerLen));
    headerBuf.writerIndex(headerLen);
    long ackedLength = nextPacketOffsetInBlock + dataLen;
    Promise<Void> promise = eventLoop.<Void>newPromise().addListener(future -> {
        if (future.isSuccess()) {
            locatedBlock.getBlock().setNumBytes(ackedLength);
        }
    });
    waitingAckQueue.addLast(new Callback(promise, ackedLength, datanodeList));
    for (Channel ch : datanodeList) {
        ch.write(headerBuf.duplicate().retain());
        ch.write(checksumBuf.duplicate().retain());
        ch.writeAndFlush(dataBuf.duplicate().retain());
    }
    checksumBuf.release();
    headerBuf.release();
    dataBuf.release();
    nextPacketSeqno++;
    return promise;
}
Also used : Channel(io.netty.channel.Channel) PacketHeader(org.apache.hadoop.hdfs.protocol.datatransfer.PacketHeader) ByteBuf(io.netty.buffer.ByteBuf)

Example 18 with ByteBuf

use of io.netty.buffer.ByteBuf in project hbase by apache.

the class SaslWrapHandler method flush.

@Override
public void flush(ChannelHandlerContext ctx) throws Exception {
    if (queue.isEmpty()) {
        return;
    }
    ByteBuf buf = null;
    try {
        ChannelPromise promise = ctx.newPromise();
        int readableBytes = queue.readableBytes();
        buf = queue.remove(readableBytes, promise);
        byte[] bytes = new byte[readableBytes];
        buf.readBytes(bytes);
        byte[] wrapperBytes = saslClient.wrap(bytes, 0, bytes.length);
        ChannelPromise lenPromise = ctx.newPromise();
        ctx.write(ctx.alloc().buffer(4).writeInt(wrapperBytes.length), lenPromise);
        ChannelPromise contentPromise = ctx.newPromise();
        ctx.write(Unpooled.wrappedBuffer(wrapperBytes), contentPromise);
        PromiseCombiner combiner = new PromiseCombiner();
        combiner.addAll(lenPromise, contentPromise);
        combiner.finish(promise);
        ctx.flush();
    } finally {
        if (buf != null) {
            ReferenceCountUtil.safeRelease(buf);
        }
    }
}
Also used : PromiseCombiner(io.netty.util.concurrent.PromiseCombiner) ChannelPromise(io.netty.channel.ChannelPromise) ByteBuf(io.netty.buffer.ByteBuf)

Example 19 with ByteBuf

use of io.netty.buffer.ByteBuf in project hive by apache.

the class TestKryoMessageCodec method testAutoRegistration.

@Test
public void testAutoRegistration() throws Exception {
    KryoMessageCodec codec = new KryoMessageCodec(0, TestMessage.class);
    ByteBuf buf = newBuffer();
    codec.encode(null, new TestMessage(), buf);
    List<Object> out = Lists.newArrayList();
    codec.decode(null, buf, out);
    assertEquals(1, out.size());
    assertTrue(out.get(0) instanceof TestMessage);
}
Also used : ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.Test)

Example 20 with ByteBuf

use of io.netty.buffer.ByteBuf in project hive by apache.

the class TestKryoMessageCodec method testNegativeMessageSize.

@Test
public void testNegativeMessageSize() throws Exception {
    KryoMessageCodec codec = new KryoMessageCodec(1024);
    ByteBuf buf = newBuffer();
    buf.writeInt(-1);
    try {
        List<Object> out = Lists.newArrayList();
        codec.decode(null, buf, out);
        fail("Should have failed to decode message with negative size.");
    } catch (IllegalArgumentException e) {
        assertTrue(e.getMessage().indexOf("must be positive") > 0);
    }
}
Also used : ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.Test)

Aggregations

ByteBuf (io.netty.buffer.ByteBuf)1517 Test (org.junit.Test)663 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)160 IOException (java.io.IOException)97 CompositeByteBuf (io.netty.buffer.CompositeByteBuf)81 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)80 Test (org.testng.annotations.Test)68 InetSocketAddress (java.net.InetSocketAddress)59 Channel (io.netty.channel.Channel)57 ChannelFuture (io.netty.channel.ChannelFuture)56 ArrayList (java.util.ArrayList)53 Map (java.util.Map)44 ChannelPromise (io.netty.channel.ChannelPromise)41 AtomicReference (java.util.concurrent.atomic.AtomicReference)36 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)34 CountDownLatch (java.util.concurrent.CountDownLatch)34 HashMap (java.util.HashMap)33 RecyclableDuplicateByteBuf (io.netty.buffer.RecyclableDuplicateByteBuf)32 EventLoopGroup (io.netty.channel.EventLoopGroup)32 List (java.util.List)32