Search in sources :

Example 1 with AbstractByteBufAllocator

use of io.netty.buffer.AbstractByteBufAllocator in project netty by netty.

the class ByteToMessageDecoderTest method releaseWhenMergeCumulateThrowsInExpand.

private void releaseWhenMergeCumulateThrowsInExpand(int untilFailure, boolean shouldFail) {
    ByteBuf oldCumulation = UnpooledByteBufAllocator.DEFAULT.heapBuffer(8, 8).writeZero(1);
    final WriteFailingByteBuf newCumulation = new WriteFailingByteBuf(untilFailure, 16);
    ByteBufAllocator allocator = new AbstractByteBufAllocator(false) {

        @Override
        public boolean isDirectBufferPooled() {
            return false;
        }

        @Override
        protected ByteBuf newHeapBuffer(int initialCapacity, int maxCapacity) {
            return newCumulation;
        }

        @Override
        protected ByteBuf newDirectBuffer(int initialCapacity, int maxCapacity) {
            throw new UnsupportedOperationException();
        }
    };
    ByteBuf in = Unpooled.buffer().writeZero(12);
    Throwable thrown = null;
    try {
        ByteToMessageDecoder.MERGE_CUMULATOR.cumulate(allocator, oldCumulation, in);
    } catch (Throwable t) {
        thrown = t;
    }
    assertEquals(0, in.refCnt());
    if (shouldFail) {
        assertSame(newCumulation.writeError(), thrown);
        assertEquals(1, oldCumulation.refCnt());
        oldCumulation.release();
        assertEquals(0, newCumulation.refCnt());
    } else {
        assertNull(thrown);
        assertEquals(0, oldCumulation.refCnt());
        assertEquals(1, newCumulation.refCnt());
        newCumulation.release();
    }
}
Also used : UnpooledByteBufAllocator(io.netty.buffer.UnpooledByteBufAllocator) ByteBufAllocator(io.netty.buffer.ByteBufAllocator) AbstractByteBufAllocator(io.netty.buffer.AbstractByteBufAllocator) AbstractByteBufAllocator(io.netty.buffer.AbstractByteBufAllocator) ByteBuf(io.netty.buffer.ByteBuf) CompositeByteBuf(io.netty.buffer.CompositeByteBuf) UnpooledHeapByteBuf(io.netty.buffer.UnpooledHeapByteBuf)

Example 2 with AbstractByteBufAllocator

use of io.netty.buffer.AbstractByteBufAllocator in project bookkeeper by apache.

the class BookieWriteLedgerTest method testLedgerCreateByteBufRefCnt.

@Test
@SuppressWarnings("unchecked")
public void testLedgerCreateByteBufRefCnt() throws Exception {
    final LedgerHandle lh = bkc.createLedger(5, 3, 2, digestType, ledgerPassword, null);
    final List<AbstractByteBufAllocator> allocs = Lists.newArrayList(new PooledByteBufAllocator(true), new PooledByteBufAllocator(false), new UnpooledByteBufAllocator(true), new UnpooledByteBufAllocator(false));
    int entryId = 0;
    for (AbstractByteBufAllocator alloc : allocs) {
        final ByteBuf data = alloc.buffer(10);
        data.writeBytes(("fragment0" + entryId).getBytes());
        assertEquals("ref count on ByteBuf should be 1", 1, data.refCnt());
        CompletableFuture<Integer> cf = new CompletableFuture<>();
        lh.asyncAddEntry(data, (rc, handle, eId, ctx) -> {
            CompletableFuture<Integer> future = (CompletableFuture<Integer>) ctx;
            future.complete(rc);
        }, cf);
        int rc = cf.get();
        assertEquals("rc code is OK", BKException.Code.OK, rc);
        for (int i = 0; i < 10; i++) {
            if (data.refCnt() == 0) {
                break;
            }
            // recycler runs asynchronously
            TimeUnit.MILLISECONDS.sleep(250);
        }
        assertEquals("writing entry with id " + entryId + ", ref count on ByteBuf should be 0 ", 0, data.refCnt());
        org.apache.bookkeeper.client.api.LedgerEntry e = lh.read(entryId, entryId).getEntry(entryId);
        assertEquals("entry data is correct", "fragment0" + entryId, new String(e.getEntryBytes()));
        entryId++;
    }
    bkc.deleteLedger(lh.ledgerId);
}
Also used : AbstractByteBufAllocator(io.netty.buffer.AbstractByteBufAllocator) ByteBuf(io.netty.buffer.ByteBuf) PooledByteBufAllocator(io.netty.buffer.PooledByteBufAllocator) CompletableFuture(java.util.concurrent.CompletableFuture) UnpooledByteBufAllocator(io.netty.buffer.UnpooledByteBufAllocator) Test(org.junit.Test)

Example 3 with AbstractByteBufAllocator

use of io.netty.buffer.AbstractByteBufAllocator in project bookkeeper by apache.

the class BookieWriteLedgerTest method testLedgerCreateAdvByteBufRefCnt.

@Test
@SuppressWarnings("unchecked")
public void testLedgerCreateAdvByteBufRefCnt() throws Exception {
    long ledgerId = rng.nextLong();
    ledgerId &= Long.MAX_VALUE;
    if (!baseConf.getLedgerManagerFactoryClass().equals(LongHierarchicalLedgerManagerFactory.class)) {
        // since LongHierarchicalLedgerManager supports ledgerIds of
        // decimal length upto 19 digits but other
        // LedgerManagers only upto 10 decimals
        ledgerId %= 9999999999L;
    }
    final LedgerHandle lh = bkc.createLedgerAdv(ledgerId, 5, 3, 2, digestType, ledgerPassword, null);
    final List<AbstractByteBufAllocator> allocs = Lists.newArrayList(new PooledByteBufAllocator(true), new PooledByteBufAllocator(false), new UnpooledByteBufAllocator(true), new UnpooledByteBufAllocator(false));
    long entryId = 0;
    for (AbstractByteBufAllocator alloc : allocs) {
        final ByteBuf data = alloc.buffer(10);
        data.writeBytes(("fragment0" + entryId).getBytes());
        assertEquals("ref count on ByteBuf should be 1", 1, data.refCnt());
        CompletableFuture<Integer> cf = new CompletableFuture<>();
        lh.asyncAddEntry(entryId, data, (rc, handle, eId, qwcLatency, ctx) -> {
            CompletableFuture<Integer> future = (CompletableFuture<Integer>) ctx;
            future.complete(rc);
        }, cf);
        int rc = cf.get();
        assertEquals("rc code is OK", BKException.Code.OK, rc);
        for (int i = 0; i < 10; i++) {
            if (data.refCnt() == 0) {
                break;
            }
            // recycler runs asynchronously
            TimeUnit.MILLISECONDS.sleep(250);
        }
        assertEquals("writing entry with id " + entryId + ", ref count on ByteBuf should be 0 ", 0, data.refCnt());
        org.apache.bookkeeper.client.api.LedgerEntry e = lh.read(entryId, entryId).getEntry(entryId);
        assertEquals("entry data is correct", "fragment0" + entryId, new String(e.getEntryBytes()));
        entryId++;
    }
    bkc.deleteLedger(lh.ledgerId);
}
Also used : AbstractByteBufAllocator(io.netty.buffer.AbstractByteBufAllocator) ByteBuf(io.netty.buffer.ByteBuf) PooledByteBufAllocator(io.netty.buffer.PooledByteBufAllocator) CompletableFuture(java.util.concurrent.CompletableFuture) UnpooledByteBufAllocator(io.netty.buffer.UnpooledByteBufAllocator) LongHierarchicalLedgerManagerFactory(org.apache.bookkeeper.meta.LongHierarchicalLedgerManagerFactory) Test(org.junit.Test)

Aggregations

AbstractByteBufAllocator (io.netty.buffer.AbstractByteBufAllocator)3 ByteBuf (io.netty.buffer.ByteBuf)3 UnpooledByteBufAllocator (io.netty.buffer.UnpooledByteBufAllocator)3 PooledByteBufAllocator (io.netty.buffer.PooledByteBufAllocator)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 Test (org.junit.Test)2 ByteBufAllocator (io.netty.buffer.ByteBufAllocator)1 CompositeByteBuf (io.netty.buffer.CompositeByteBuf)1 UnpooledHeapByteBuf (io.netty.buffer.UnpooledHeapByteBuf)1 LongHierarchicalLedgerManagerFactory (org.apache.bookkeeper.meta.LongHierarchicalLedgerManagerFactory)1