Example usage for io.netty.buffer ByteBuf refCnt

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

Introduction

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

Prototype

int refCnt();

Source Link

Document

Returns the reference count of this object.

Usage

From source file:eu.stratosphere.runtime.io.network.netty.InboundEnvelopeDecoderTest.java

License:Apache License

@Test
public void testBufferAvailabilityRegistrationBufferPoolDestroyedSkipBytes() throws Exception {
    final EmbeddedChannel ch = new EmbeddedChannel(new OutboundEnvelopeEncoder(),
            new InboundEnvelopeDecoder(this.bufferProviderBroker));

    when(this.bufferProviderBroker.getBufferProvider(anyJobId(), anyChannelId()))
            .thenReturn(this.bufferProvider);

    when(this.bufferProvider.requestBuffer(anyInt())).thenReturn(null);

    when(this.bufferProvider
            .registerBufferAvailabilityListener(Matchers.<BufferAvailabilityListener>anyObject()))
                    .thenReturn(BufferAvailabilityRegistration.FAILED_BUFFER_POOL_DESTROYED);

    // --------------------------------------------------------------------

    Envelope[] envelopes = new Envelope[] { nextEnvelope(true), nextEnvelope(), nextEnvelope() };
    Envelope[] expectedEnvelopes = new Envelope[] { envelopes[1], envelopes[2] };

    ByteBuf buf = encode(ch, envelopes);

    int bufferSize = envelopes[0].getBuffer().size();

    // --------------------------------------------------------------------
    // 1) skip in current buffer only
    // --------------------------------------------------------------------
    {/*from   ww  w.j av a  2 s.c  o m*/
        // skip last bytes in current buffer
        ByteBuf[] slices = slice(buf, OutboundEnvelopeEncoder.HEADER_SIZE + bufferSize);

        int refCount = slices[0].refCnt();
        decodeAndVerify(ch, slices[0]);
        Assert.assertEquals(refCount - 1, slices[0].refCnt());

        refCount = slices[1].refCnt();
        decodeAndVerify(ch, slices[1], expectedEnvelopes);
        Assert.assertEquals(refCount - 1, slices[1].refCnt());
    }

    {
        // skip bytes in current buffer, leave last 16 bytes from next envelope
        ByteBuf[] slices = slice(buf, OutboundEnvelopeEncoder.HEADER_SIZE + bufferSize + 16);

        int refCount = slices[0].refCnt();
        decodeAndVerify(ch, slices[0]);
        Assert.assertEquals(refCount - 1, slices[0].refCnt());

        refCount = slices[1].refCnt();
        decodeAndVerify(ch, slices[1], expectedEnvelopes);
        Assert.assertEquals(refCount - 1, slices[1].refCnt());
    }

    {
        // skip bytes in current buffer, then continue with full envelope from same buffer
        ByteBuf[] slices = slice(buf,
                OutboundEnvelopeEncoder.HEADER_SIZE + bufferSize + OutboundEnvelopeEncoder.HEADER_SIZE);

        int refCount = slices[0].refCnt();
        decodeAndVerify(ch, slices[0], expectedEnvelopes[0]);
        Assert.assertEquals(refCount - 1, slices[0].refCnt());

        refCount = slices[1].refCnt();
        decodeAndVerify(ch, slices[1], expectedEnvelopes[1]);
        Assert.assertEquals(refCount - 1, slices[1].refCnt());
    }

    // --------------------------------------------------------------------
    // 2) skip in current and next buffer
    // --------------------------------------------------------------------

    {
        // skip bytes in current buffer, then continue to skip last 32 bytes in next buffer
        ByteBuf[] slices = slice(buf, OutboundEnvelopeEncoder.HEADER_SIZE + bufferSize - 32);

        int refCount = slices[0].refCnt();
        decodeAndVerify(ch, slices[0]);
        Assert.assertEquals(refCount - 1, slices[0].refCnt());

        refCount = slices[1].refCnt();
        decodeAndVerify(ch, slices[1], expectedEnvelopes);
        Assert.assertEquals(refCount - 1, slices[1].refCnt());
    }

    {
        // skip bytes in current buffer, then continue to skip in next two buffers
        ByteBuf[] slices = slice(buf, OutboundEnvelopeEncoder.HEADER_SIZE + bufferSize - 32, 16);

        int refCount = slices[0].refCnt();
        decodeAndVerify(ch, slices[0]);
        Assert.assertEquals(refCount - 1, slices[0].refCnt());

        refCount = slices[1].refCnt();
        decodeAndVerify(ch, slices[1]);
        Assert.assertEquals(refCount - 1, slices[1].refCnt());

        refCount = slices[2].refCnt();
        decodeAndVerify(ch, slices[2], expectedEnvelopes);
        Assert.assertEquals(refCount - 1, slices[2].refCnt());
    }

    // ref count should be 1, because slices shared the ref count
    Assert.assertEquals(1, buf.refCnt());
}

From source file:eu.stratosphere.runtime.io.network.netty.InboundEnvelopeDecoderTest.java

License:Apache License

@Test
public void testEncodeDecode() throws Exception {
    final EmbeddedChannel ch = new EmbeddedChannel(new OutboundEnvelopeEncoder(),
            new InboundEnvelopeDecoder(this.bufferProviderBroker));

    when(this.bufferProviderBroker.getBufferProvider(anyJobId(), anyChannelId()))
            .thenReturn(this.bufferProvider);

    when(this.bufferProvider.requestBuffer(anyInt())).thenAnswer(new Answer<Object>() {
        @Override//  ww  w.  java2 s .c  o  m
        public Object answer(InvocationOnMock invocation) throws Throwable {
            // fulfill the buffer request
            return allocBuffer((Integer) invocation.getArguments()[0]);
        }
    });

    // --------------------------------------------------------------------

    Envelope[] envelopes = new Envelope[] { nextEnvelope(0), nextEnvelope(2), nextEnvelope(32768),
            nextEnvelope(3782, new TestEvent1(34872527)),
            nextEnvelope(88, new TestEvent1(8749653), new TestEvent1(365345)),
            nextEnvelope(0, new TestEvent2(34563456), new TestEvent1(598432), new TestEvent2(976293845)),
            nextEnvelope(23) };

    ByteBuf buf = encode(ch, envelopes);

    // 1. complete ByteBuf as input
    int refCount = buf.retain().refCnt();

    decodeAndVerify(ch, buf, envelopes);
    Assert.assertEquals(refCount - 1, buf.refCnt());

    // 2. random slices
    buf.readerIndex(0);
    ByteBuf[] slices = randomSlices(buf);

    ch.writeInbound(slices);

    for (ByteBuf slice : slices) {
        Assert.assertEquals(1, slice.refCnt());
    }

    decodeAndVerify(ch, envelopes);

    buf.release();
}

From source file:eu.stratosphere.runtime.io.network.netty.InboundEnvelopeDecoderTest.java

License:Apache License

@Test
public void testEncodeDecodeRandomEnvelopes() throws Exception {
    final InboundEnvelopeDecoder decoder = new InboundEnvelopeDecoder(this.bufferProviderBroker);
    final EmbeddedChannel ch = new EmbeddedChannel(new OutboundEnvelopeEncoder(), decoder);

    when(this.bufferProviderBroker.getBufferProvider(anyJobId(), anyChannelId()))
            .thenReturn(this.bufferProvider);

    when(this.bufferProvider.requestBuffer(anyInt())).thenAnswer(new Answer<Object>() {
        @Override//from  www .  jav a2  s.c  o  m
        public Object answer(InvocationOnMock invocation) throws Throwable {
            // fulfill the buffer request with the requested size
            return allocBuffer((Integer) invocation.getArguments()[0]);
        }
    });

    Random randomAnswerSource = new Random(RANDOM_SEED);

    RandomBufferRequestAnswer randomBufferRequestAnswer = new RandomBufferRequestAnswer(randomAnswerSource);

    RandomBufferAvailabilityRegistrationAnswer randomBufferAvailabilityRegistrationAnswer = new RandomBufferAvailabilityRegistrationAnswer(
            randomAnswerSource, randomBufferRequestAnswer);

    when(this.bufferProvider.requestBuffer(anyInt())).thenAnswer(randomBufferRequestAnswer);

    when(this.bufferProvider
            .registerBufferAvailabilityListener(Matchers.<BufferAvailabilityListener>anyObject()))
                    .thenAnswer(randomBufferAvailabilityRegistrationAnswer);

    // --------------------------------------------------------------------

    Envelope[] envelopes = nextRandomEnvelopes(1024);

    ByteBuf buf = encode(ch, envelopes);

    ByteBuf[] slices = randomSlices(buf);

    for (ByteBuf slice : slices) {
        int refCount = slice.refCnt();
        ch.writeInbound(slice);

        // registered BufferAvailabilityListener => call bufferAvailable(buffer)
        while (randomBufferAvailabilityRegistrationAnswer.isRegistered()) {
            randomBufferAvailabilityRegistrationAnswer.unregister();

            Assert.assertFalse(ch.config().isAutoRead());
            Assert.assertEquals(refCount + 1, slice.refCnt());

            // return a buffer of max size => decoder needs to limit buffer size
            decoder.bufferAvailable(allocBuffer(MAX_BUFFER_SIZE));
            ch.runPendingTasks();
        }

        Assert.assertEquals(refCount - 1, slice.refCnt());
        Assert.assertTrue(ch.config().isAutoRead());
    }

    Envelope[] expected = randomBufferAvailabilityRegistrationAnswer.removeSkippedEnvelopes(envelopes);

    decodeAndVerify(ch, expected);

    Assert.assertEquals(1, buf.refCnt());

    buf.release();
}

From source file:io.grpc.alts.internal.AltsTsiFrameProtectorTest.java

License:Apache License

@Test
public void parserHeader_EmptyUnprotectNoRetain() throws GeneralSecurityException {
    ByteBufAllocator alloc = ByteBufAllocator.DEFAULT;
    List<Object> out = new ArrayList<>();
    FakeChannelCrypter crypter = new FakeChannelCrypter();
    AltsTsiFrameProtector.Unprotector unprotector = new AltsTsiFrameProtector.Unprotector(crypter, alloc);
    ByteBuf emptyBuf = getDirectBuffer(0, ref);
    unprotector.unprotect(emptyBuf, out, alloc);

    assertThat(emptyBuf.refCnt()).isEqualTo(1);

    unprotector.destroy();/*  w w  w  .  j a va  2s  . c om*/
}

From source file:io.grpc.alts.internal.AltsTsiFrameProtectorTest.java

License:Apache License

@Test
public void parseFrame_twoFramesNoFragment() throws GeneralSecurityException {
    int payloadBytes = 1536;
    int payloadBytes1 = 1024;
    int payloadBytes2 = payloadBytes - payloadBytes1;
    ByteBufAllocator alloc = ByteBufAllocator.DEFAULT;
    List<Object> out = new ArrayList<>();
    FakeChannelCrypter crypter = new FakeChannelCrypter();
    AltsTsiFrameProtector.Unprotector unprotector = new AltsTsiFrameProtector.Unprotector(crypter, alloc);

    ByteBuf plain = getRandom(payloadBytes, ref);
    ByteBuf outFrame = getDirectBuffer(
            2 * (AltsTsiFrameProtector.getHeaderBytes() + FakeChannelCrypter.getTagBytes()) + payloadBytes,
            ref);/*from  www. j  a  v a 2 s  . co m*/

    outFrame.writeIntLE(
            AltsTsiFrameProtector.getHeaderTypeFieldBytes() + payloadBytes1 + FakeChannelCrypter.getTagBytes());
    outFrame.writeIntLE(6);
    List<ByteBuf> framePlain1 = Collections.singletonList(plain.readSlice(payloadBytes1));
    ByteBuf frameOut1 = writeSlice(outFrame, payloadBytes1 + FakeChannelCrypter.getTagBytes());

    outFrame.writeIntLE(
            AltsTsiFrameProtector.getHeaderTypeFieldBytes() + payloadBytes2 + FakeChannelCrypter.getTagBytes());
    outFrame.writeIntLE(6);
    List<ByteBuf> framePlain2 = Collections.singletonList(plain);
    ByteBuf frameOut2 = writeSlice(outFrame, payloadBytes2 + FakeChannelCrypter.getTagBytes());

    crypter.encrypt(frameOut1, framePlain1);
    crypter.encrypt(frameOut2, framePlain2);
    plain.readerIndex(0);

    unprotector.unprotect(outFrame, out, alloc);
    assertThat(out.size()).isEqualTo(1);
    ByteBuf out1 = ref((ByteBuf) out.get(0));
    assertThat(out1).isEqualTo(plain);
    assertThat(outFrame.refCnt()).isEqualTo(1);
    assertThat(outFrame.readableBytes()).isEqualTo(0);

    unprotector.destroy();
}

From source file:io.grpc.alts.internal.AltsTsiFrameProtectorTest.java

License:Apache License

@Test
public void parseFrame_twoFramesNoFragment_Leftover() throws GeneralSecurityException {
    int payloadBytes = 1536;
    int payloadBytes1 = 1024;
    int payloadBytes2 = payloadBytes - payloadBytes1;
    ByteBufAllocator alloc = ByteBufAllocator.DEFAULT;
    List<Object> out = new ArrayList<>();
    FakeChannelCrypter crypter = new FakeChannelCrypter();
    AltsTsiFrameProtector.Unprotector unprotector = new AltsTsiFrameProtector.Unprotector(crypter, alloc);

    ByteBuf plain = getRandom(payloadBytes, ref);
    ByteBuf protectedBuf = getDirectBuffer(
            2 * (AltsTsiFrameProtector.getHeaderBytes() + FakeChannelCrypter.getTagBytes()) + payloadBytes
                    + AltsTsiFrameProtector.getHeaderBytes(),
            ref);//  w  w w . ja v  a  2 s.co  m

    protectedBuf.writeIntLE(
            AltsTsiFrameProtector.getHeaderTypeFieldBytes() + payloadBytes1 + FakeChannelCrypter.getTagBytes());
    protectedBuf.writeIntLE(6);
    List<ByteBuf> framePlain1 = Collections.singletonList(plain.readSlice(payloadBytes1));
    ByteBuf frameOut1 = writeSlice(protectedBuf, payloadBytes1 + FakeChannelCrypter.getTagBytes());

    protectedBuf.writeIntLE(
            AltsTsiFrameProtector.getHeaderTypeFieldBytes() + payloadBytes2 + FakeChannelCrypter.getTagBytes());
    protectedBuf.writeIntLE(6);
    List<ByteBuf> framePlain2 = Collections.singletonList(plain);
    ByteBuf frameOut2 = writeSlice(protectedBuf, payloadBytes2 + FakeChannelCrypter.getTagBytes());
    // This is an invalid header length field, make sure it triggers an error
    // when the remainder of the header is given.
    protectedBuf.writeIntLE((byte) -1);

    crypter.encrypt(frameOut1, framePlain1);
    crypter.encrypt(frameOut2, framePlain2);
    plain.readerIndex(0);

    unprotector.unprotect(protectedBuf, out, alloc);
    assertThat(out.size()).isEqualTo(1);
    ByteBuf out1 = ref((ByteBuf) out.get(0));
    assertThat(out1).isEqualTo(plain);

    // The protectedBuf is buffered inside the unprotector.
    assertThat(protectedBuf.readableBytes()).isEqualTo(0);
    assertThat(protectedBuf.refCnt()).isEqualTo(2);

    protectedBuf.writeIntLE(6);
    try {
        unprotector.unprotect(protectedBuf, out, alloc);
        fail("Exception expected");
    } catch (IllegalArgumentException ex) {
        assertThat(ex).hasMessageThat().contains("Invalid header field: frame size too small");
    }

    unprotector.destroy();

    // Make sure that unprotector does not hold onto buffered ByteBuf instance after destroy.
    assertThat(protectedBuf.refCnt()).isEqualTo(1);

    // Make sure that destroying twice does not throw.
    unprotector.destroy();
}

From source file:io.grpc.alts.internal.AltsTsiFrameProtectorTest.java

License:Apache License

@Test
public void parseFrame_twoFramesFragmentSecond() throws GeneralSecurityException {
    int payloadBytes = 1536;
    int payloadBytes1 = 1024;
    int payloadBytes2 = payloadBytes - payloadBytes1;
    ByteBufAllocator alloc = ByteBufAllocator.DEFAULT;
    List<Object> out = new ArrayList<>();
    FakeChannelCrypter crypter = new FakeChannelCrypter();
    AltsTsiFrameProtector.Unprotector unprotector = new AltsTsiFrameProtector.Unprotector(crypter, alloc);

    ByteBuf plain = getRandom(payloadBytes, ref);
    ByteBuf protectedBuf = getDirectBuffer(
            2 * (AltsTsiFrameProtector.getHeaderBytes() + FakeChannelCrypter.getTagBytes()) + payloadBytes
                    + AltsTsiFrameProtector.getHeaderBytes(),
            ref);//www  .ja v  a2 s .co  m

    protectedBuf.writeIntLE(
            AltsTsiFrameProtector.getHeaderTypeFieldBytes() + payloadBytes1 + FakeChannelCrypter.getTagBytes());
    protectedBuf.writeIntLE(6);
    List<ByteBuf> framePlain1 = Collections.singletonList(plain.readSlice(payloadBytes1));
    ByteBuf frameOut1 = writeSlice(protectedBuf, payloadBytes1 + FakeChannelCrypter.getTagBytes());

    protectedBuf.writeIntLE(
            AltsTsiFrameProtector.getHeaderTypeFieldBytes() + payloadBytes2 + FakeChannelCrypter.getTagBytes());
    protectedBuf.writeIntLE(6);
    List<ByteBuf> framePlain2 = Collections.singletonList(plain);
    ByteBuf frameOut2 = writeSlice(protectedBuf, payloadBytes2 + FakeChannelCrypter.getTagBytes());

    crypter.encrypt(frameOut1, framePlain1);
    crypter.encrypt(frameOut2, framePlain2);
    plain.readerIndex(0);

    unprotector
            .unprotect(
                    protectedBuf.readSlice(payloadBytes + AltsTsiFrameProtector.getHeaderBytes()
                            + FakeChannelCrypter.getTagBytes() + AltsTsiFrameProtector.getHeaderBytes()),
                    out, alloc);
    assertThat(out.size()).isEqualTo(1);
    ByteBuf out1 = ref((ByteBuf) out.get(0));
    assertThat(out1).isEqualTo(plain.readSlice(payloadBytes1));
    assertThat(protectedBuf.refCnt()).isEqualTo(2);

    unprotector.unprotect(protectedBuf, out, alloc);
    assertThat(out.size()).isEqualTo(2);
    ByteBuf out2 = ref((ByteBuf) out.get(1));
    assertThat(out2).isEqualTo(plain);
    assertThat(protectedBuf.refCnt()).isEqualTo(1);

    unprotector.destroy();
}

From source file:io.hekate.network.netty.NettyMessageTest.java

License:Apache License

@Test
public void testPreview() throws Exception {
    ByteBuf buf = Unpooled.buffer();

    buf.writeByte(10);/*from ww w .  j  av a 2  s .  c o  m*/
    buf.writeBoolean(true);
    buf.writeInt(100);
    buf.writeLong(1000);
    buf.writeDouble(1.01);

    NettyMessage msg = new NettyMessage(buf, new Codec<Object>() {
        @Override
        public boolean isStateful() {
            return false;
        }

        @Override
        public Class<Object> baseType() {
            return Object.class;
        }

        @Override
        public Object decode(DataReader in) throws IOException {
            throw new AssertionError("Should not be called.");
        }

        @Override
        public void encode(Object obj, DataWriter out) throws IOException {
            throw new AssertionError("Should not be called.");
        }
    });

    assertEquals((byte) 10, (byte) msg.preview(DataInput::readByte));
    assertEquals(0, buf.readerIndex());

    assertTrue(msg.previewBoolean(m -> {
        m.skipBytes(1);

        return m.readBoolean();
    }));
    assertEquals(0, buf.readerIndex());

    assertEquals(100, msg.previewInt(m -> {
        m.skipBytes(2);

        return m.readInt();
    }));
    assertEquals(0, buf.readerIndex());

    assertEquals(1000, msg.previewLong(m -> {
        m.skipBytes(6);

        return m.readLong();
    }));
    assertEquals(0, buf.readerIndex());

    assertEquals(1.01, msg.previewDouble(m -> {
        m.skipBytes(14);

        return m.readDouble();
    }), 1000);
    assertEquals(0, buf.readerIndex());

    assertEquals(1, buf.refCnt());

    msg.release();

    assertEquals(0, buf.refCnt());
}

From source file:io.hekate.network.netty.NettyMessageTest.java

License:Apache License

@Test
public void testHandleAsync() throws Exception {
    ByteBuf buf = Unpooled.buffer();

    buf.writeByte(1);//from w ww.j a v  a 2  s .c  om

    NettyMessage msg = new NettyMessage(buf, new Codec<Object>() {
        @Override
        public boolean isStateful() {
            return false;
        }

        @Override
        public Class<Object> baseType() {
            return Object.class;
        }

        @Override
        public Object decode(DataReader in) throws IOException {
            return in.readByte();
        }

        @Override
        public void encode(Object obj, DataWriter out) throws IOException {
            throw new AssertionError("Should not be called.");
        }
    });

    Object val;

    ExecutorService thread = Executors.newSingleThreadExecutor();

    try {
        Exchanger<Object> ref = new Exchanger<>();

        msg.handleAsync(thread, m -> {
            try {
                ref.exchange(m.decode());
            } catch (InterruptedException | IOException e) {
                // No-op.
            }
        });

        val = ref.exchange(null);
    } finally {
        thread.shutdownNow();

        thread.awaitTermination(AWAIT_TIMEOUT, TimeUnit.SECONDS);
    }

    assertEquals((byte) 1, val);
    assertEquals(1, buf.refCnt());
}

From source file:io.lettuce.core.protocol.CommandHandler.java

License:Apache License

/**
 * @see io.netty.channel.ChannelInboundHandlerAdapter#channelRead(io.netty.channel.ChannelHandlerContext, java.lang.Object)
 *//* w ww . jav  a 2  s.  co m*/
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {

    ByteBuf input = (ByteBuf) msg;

    if (!input.isReadable() || input.refCnt() == 0) {
        logger.warn("{} Input not readable {}, {}", logPrefix(), input.isReadable(), input.refCnt());
        return;
    }

    if (debugEnabled) {
        logger.debug("{} Received: {} bytes, {} commands in the stack", logPrefix(), input.readableBytes(),
                stack.size());
    }

    try {
        if (buffer.refCnt() < 1) {
            logger.warn("{} Ignoring received data for closed or abandoned connection", logPrefix());
            return;
        }

        if (debugEnabled && ctx.channel() != channel) {
            logger.debug("{} Ignoring data for a non-registered channel {}", logPrefix(), ctx.channel());
            return;
        }

        if (traceEnabled) {
            logger.trace("{} Buffer: {}", logPrefix(), input.toString(Charset.defaultCharset()).trim());
        }

        buffer.writeBytes(input);

        decode(ctx, buffer);
    } finally {
        input.release();
    }
}