Search in sources :

Example 6 with IllegalReferenceCountException

use of io.netty.util.IllegalReferenceCountException in project netty by netty.

the class SslHandlerTest method newHandler.

private static ChannelHandler newHandler(final SslContext sslCtx, final Promise<Void> promise) {
    return new ChannelInitializer() {

        @Override
        protected void initChannel(final Channel ch) {
            final SslHandler sslHandler = sslCtx.newHandler(ch.alloc());
            sslHandler.setHandshakeTimeoutMillis(1000);
            ch.pipeline().addFirst(sslHandler);
            sslHandler.handshakeFuture().addListener(new FutureListener<Channel>() {

                @Override
                public void operationComplete(final Future<Channel> future) {
                    ch.pipeline().remove(sslHandler);
                    // Schedule the close so removal has time to propagate exception if any.
                    ch.eventLoop().execute(new Runnable() {

                        @Override
                        public void run() {
                            ch.close();
                        }
                    });
                }
            });
            ch.pipeline().addLast(new ChannelInboundHandlerAdapter() {

                @Override
                public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
                    if (cause instanceof CodecException) {
                        cause = cause.getCause();
                    }
                    if (cause instanceof IllegalReferenceCountException) {
                        promise.setFailure(cause);
                    }
                }

                @Override
                public void channelInactive(ChannelHandlerContext ctx) {
                    promise.trySuccess(null);
                }
            });
        }
    };
}
Also used : LocalServerChannel(io.netty.channel.local.LocalServerChannel) LocalChannel(io.netty.channel.local.LocalChannel) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) Channel(io.netty.channel.Channel) IllegalReferenceCountException(io.netty.util.IllegalReferenceCountException) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) CodecException(io.netty.handler.codec.CodecException) ChannelInitializer(io.netty.channel.ChannelInitializer) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter)

Example 7 with IllegalReferenceCountException

use of io.netty.util.IllegalReferenceCountException in project netty by netty.

the class ReferenceCountUpdater method retryRelease0.

private boolean retryRelease0(T instance, int decrement) {
    for (; ; ) {
        int rawCnt = updater().get(instance), realCnt = toLiveRealRefCnt(rawCnt, decrement);
        if (decrement == realCnt) {
            if (tryFinalRelease0(instance, rawCnt)) {
                return true;
            }
        } else if (decrement < realCnt) {
            // all changes to the raw count are 2x the "real" change
            if (updater().compareAndSet(instance, rawCnt, rawCnt - (decrement << 1))) {
                return false;
            }
        } else {
            throw new IllegalReferenceCountException(realCnt, -decrement);
        }
        // this benefits throughput under high contention
        Thread.yield();
    }
}
Also used : IllegalReferenceCountException(io.netty.util.IllegalReferenceCountException)

Example 8 with IllegalReferenceCountException

use of io.netty.util.IllegalReferenceCountException in project bookkeeper by apache.

the class BookKeeperTest method testReadEntryReleaseByteBufs.

@Test
public void testReadEntryReleaseByteBufs() throws Exception {
    ClientConfiguration confWriter = new ClientConfiguration();
    confWriter.setZkServers(zkUtil.getZooKeeperConnectString());
    int numEntries = 10;
    byte[] data = "foobar".getBytes();
    long ledgerId;
    try (BookKeeper bkc = new BookKeeper(confWriter)) {
        try (LedgerHandle lh = bkc.createLedger(digestType, "testPasswd".getBytes())) {
            ledgerId = lh.getId();
            for (int i = 0; i < numEntries; i++) {
                lh.addEntry(data);
            }
        }
    }
    // v2 protocol, using pooled buffers
    ClientConfiguration confReader1 = new ClientConfiguration().setUseV2WireProtocol(true).setNettyUsePooledBuffers(true);
    confReader1.setZkServers(zkUtil.getZooKeeperConnectString());
    try (BookKeeper bkc = new BookKeeper(confReader1)) {
        try (LedgerHandle lh = bkc.openLedger(ledgerId, digestType, "testPasswd".getBytes())) {
            assertEquals(numEntries - 1, lh.readLastConfirmed());
            for (Enumeration<LedgerEntry> readEntries = lh.readEntries(0, numEntries - 1); readEntries.hasMoreElements(); ) {
                LedgerEntry entry = readEntries.nextElement();
                try {
                    entry.data.release();
                } catch (IllegalReferenceCountException ok) {
                    fail("ByteBuf already released");
                }
            }
        }
    }
    // v2 protocol, not using pooled buffers
    ClientConfiguration confReader2 = new ClientConfiguration().setUseV2WireProtocol(true).setNettyUsePooledBuffers(false);
    confReader2.setZkServers(zkUtil.getZooKeeperConnectString());
    try (BookKeeper bkc = new BookKeeper(confReader2)) {
        try (LedgerHandle lh = bkc.openLedger(ledgerId, digestType, "testPasswd".getBytes())) {
            assertEquals(numEntries - 1, lh.readLastConfirmed());
            for (Enumeration<LedgerEntry> readEntries = lh.readEntries(0, numEntries - 1); readEntries.hasMoreElements(); ) {
                LedgerEntry entry = readEntries.nextElement();
                try {
                    entry.data.release();
                } catch (IllegalReferenceCountException e) {
                    fail("ByteBuf already released");
                }
            }
        }
    }
    // v3 protocol, not using pooled buffers
    ClientConfiguration confReader3 = new ClientConfiguration().setUseV2WireProtocol(false).setNettyUsePooledBuffers(false);
    confReader3.setZkServers(zkUtil.getZooKeeperConnectString());
    try (BookKeeper bkc = new BookKeeper(confReader3)) {
        try (LedgerHandle lh = bkc.openLedger(ledgerId, digestType, "testPasswd".getBytes())) {
            assertEquals(numEntries - 1, lh.readLastConfirmed());
            for (Enumeration<LedgerEntry> readEntries = lh.readEntries(0, numEntries - 1); readEntries.hasMoreElements(); ) {
                LedgerEntry entry = readEntries.nextElement();
                assertTrue("Can't release entry " + entry.getEntryId() + ": ref = " + entry.data.refCnt(), entry.data.release());
                try {
                    assertFalse(entry.data.release());
                    fail("ByteBuf already released");
                } catch (IllegalReferenceCountException ok) {
                }
            }
        }
    }
    // v3 protocol, using pooled buffers
    // v3 protocol from 4.5 always "wraps" buffers returned by protobuf
    ClientConfiguration confReader4 = new ClientConfiguration().setUseV2WireProtocol(false).setNettyUsePooledBuffers(true);
    confReader4.setZkServers(zkUtil.getZooKeeperConnectString());
    try (BookKeeper bkc = new BookKeeper(confReader4)) {
        try (LedgerHandle lh = bkc.openLedger(ledgerId, digestType, "testPasswd".getBytes())) {
            assertEquals(numEntries - 1, lh.readLastConfirmed());
            for (Enumeration<LedgerEntry> readEntries = lh.readEntries(0, numEntries - 1); readEntries.hasMoreElements(); ) {
                LedgerEntry entry = readEntries.nextElement();
                // ButeBufs not reference counter
                assertTrue("Can't release entry " + entry.getEntryId() + ": ref = " + entry.data.refCnt(), entry.data.release());
                try {
                    assertFalse(entry.data.release());
                    fail("ByteBuf already released");
                } catch (IllegalReferenceCountException ok) {
                }
            }
        }
    }
    // cannot read twice an entry
    ClientConfiguration confReader5 = new ClientConfiguration();
    confReader5.setZkServers(zkUtil.getZooKeeperConnectString());
    try (BookKeeper bkc = new BookKeeper(confReader5)) {
        try (LedgerHandle lh = bkc.openLedger(ledgerId, digestType, "testPasswd".getBytes())) {
            assertEquals(numEntries - 1, lh.readLastConfirmed());
            for (Enumeration<LedgerEntry> readEntries = lh.readEntries(0, numEntries - 1); readEntries.hasMoreElements(); ) {
                LedgerEntry entry = readEntries.nextElement();
                entry.getEntry();
                try {
                    entry.getEntry();
                    fail("entry data accessed twice");
                } catch (IllegalStateException ok) {
                }
                try {
                    entry.getEntryInputStream();
                    fail("entry data accessed twice");
                } catch (IllegalStateException ok) {
                }
            }
        }
    }
}
Also used : IllegalReferenceCountException(io.netty.util.IllegalReferenceCountException) ClientConfiguration(org.apache.bookkeeper.conf.ClientConfiguration) Test(org.junit.Test)

Example 9 with IllegalReferenceCountException

use of io.netty.util.IllegalReferenceCountException in project flink by apache.

the class AbstractByteBufTest method assertDuplicateFailAfterRelease.

private static void assertDuplicateFailAfterRelease(ByteBuf... bufs) {
    for (ByteBuf buf : bufs) {
        if (buf.refCnt() > 0) {
            buf.release();
        }
    }
    for (ByteBuf buf : bufs) {
        try {
            assertEquals(0, buf.refCnt());
            buf.duplicate();
            fail();
        } catch (IllegalReferenceCountException ignored) {
        // as expected
        }
    }
}
Also used : IllegalReferenceCountException(org.apache.flink.shaded.netty4.io.netty.util.IllegalReferenceCountException) ByteBuf(org.apache.flink.shaded.netty4.io.netty.buffer.ByteBuf)

Example 10 with IllegalReferenceCountException

use of io.netty.util.IllegalReferenceCountException in project flink by apache.

the class AbstractByteBufTest method testMemoryAddressAfterRelease.

@Test
public void testMemoryAddressAfterRelease() {
    ByteBuf buf = releasedBuffer();
    if (buf.hasMemoryAddress()) {
        try {
            buf.memoryAddress();
            fail();
        } catch (IllegalReferenceCountException e) {
        // expected
        }
    }
}
Also used : IllegalReferenceCountException(org.apache.flink.shaded.netty4.io.netty.util.IllegalReferenceCountException) ByteBuf(org.apache.flink.shaded.netty4.io.netty.buffer.ByteBuf) Test(org.junit.Test)

Aggregations

ByteBuf (org.apache.flink.shaded.netty4.io.netty.buffer.ByteBuf)6 IllegalReferenceCountException (org.apache.flink.shaded.netty4.io.netty.util.IllegalReferenceCountException)6 IllegalReferenceCountException (io.netty.util.IllegalReferenceCountException)5 Test (org.junit.Test)5 DrillBuf (io.netty.buffer.DrillBuf)2 Channel (io.netty.channel.Channel)1 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)1 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)1 ChannelInitializer (io.netty.channel.ChannelInitializer)1 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)1 LocalChannel (io.netty.channel.local.LocalChannel)1 LocalServerChannel (io.netty.channel.local.LocalServerChannel)1 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)1 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)1 CodecException (io.netty.handler.codec.CodecException)1 ClientConfiguration (org.apache.bookkeeper.conf.ClientConfiguration)1 BaseTest (org.apache.drill.test.BaseTest)1