Search in sources :

Example 11 with IllegalReferenceCountException

use of org.apache.flink.shaded.netty4.io.netty.util.IllegalReferenceCountException in project Discord4J by Discord4J.

the class FallbackReaderStrategy method read.

@Override
public Mono<Object> read(Mono<ByteBuf> content, Class<Object> responseType) {
    if (ErrorResponse.class.isAssignableFrom(responseType)) {
        return content.handle((buf, sink) -> {
            try {
                ErrorResponse response = new ErrorResponse();
                response.getFields().put("body", asString(buf));
                sink.next(response);
            } catch (IllegalReferenceCountException e) {
                sink.complete();
            }
        });
    }
    return content.handle((buf, sink) -> {
        try {
            sink.next(asString(buf));
        } catch (IllegalReferenceCountException e) {
            sink.complete();
        }
    });
}
Also used : IllegalReferenceCountException(io.netty.util.IllegalReferenceCountException) ErrorResponse(discord4j.rest.json.response.ErrorResponse)

Example 12 with IllegalReferenceCountException

use of org.apache.flink.shaded.netty4.io.netty.util.IllegalReferenceCountException in project bookkeeper by apache.

the class BookKeeperTest method testReadEntryReleaseByteBufs.

@SuppressWarnings("deprecation")
@Test
public void testReadEntryReleaseByteBufs() throws Exception {
    ClientConfiguration confWriter = new ClientConfiguration();
    confWriter.setMetadataServiceUri(zkUtil.getMetadataServiceUri());
    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).setMetadataServiceUri(zkUtil.getMetadataServiceUri());
    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.setMetadataServiceUri(zkUtil.getMetadataServiceUri());
    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).setMetadataServiceUri(zkUtil.getMetadataServiceUri());
    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).setMetadataServiceUri(zkUtil.getMetadataServiceUri());
    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.setMetadataServiceUri(zkUtil.getMetadataServiceUri());
    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)

Aggregations

IllegalReferenceCountException (io.netty.util.IllegalReferenceCountException)6 ByteBuf (org.apache.flink.shaded.netty4.io.netty.buffer.ByteBuf)6 IllegalReferenceCountException (org.apache.flink.shaded.netty4.io.netty.util.IllegalReferenceCountException)6 Test (org.junit.Test)5 DrillBuf (io.netty.buffer.DrillBuf)2 ErrorResponse (discord4j.rest.json.response.ErrorResponse)1 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