Example usage for io.netty.buffer UnpooledByteBufAllocator DEFAULT

List of usage examples for io.netty.buffer UnpooledByteBufAllocator DEFAULT

Introduction

In this page you can find the example usage for io.netty.buffer UnpooledByteBufAllocator DEFAULT.

Prototype

UnpooledByteBufAllocator DEFAULT

To view the source code for io.netty.buffer UnpooledByteBufAllocator DEFAULT.

Click Source Link

Document

Default instance which uses leak-detection for direct buffers.

Usage

From source file:org.apache.bookkeeper.client.TestGetBookieInfoTimeout.java

License:Apache License

@Test
public void testGetBookieInfoTimeout() throws Exception {

    // connect to the bookies and create a ledger
    LedgerHandle writelh = bkc.createLedger(3, 3, digestType, "testPasswd".getBytes());
    String tmp = "Foobar";
    final int numEntries = 10;
    for (int i = 0; i < numEntries; i++) {
        writelh.addEntry(tmp.getBytes());
    }/*  w  w w  .j av  a  2 s.co  m*/

    // set timeout for getBookieInfo to be 2 secs and cause one of the bookies to go to sleep for 3X that time
    ClientConfiguration cConf = new ClientConfiguration();
    cConf.setGetBookieInfoTimeout(2);

    final BookieSocketAddress bookieToSleep = writelh.getLedgerMetadata().getEnsembleAt(0).get(0);
    int sleeptime = cConf.getBookieInfoTimeout() * 3;
    CountDownLatch latch = sleepBookie(bookieToSleep, sleeptime);
    latch.await();

    // try to get bookie info from the sleeping bookie. It should fail with timeout error
    BookieSocketAddress addr = new BookieSocketAddress(bookieToSleep.getSocketAddress().getHostString(),
            bookieToSleep.getPort());
    BookieClient bc = new BookieClientImpl(cConf, eventLoopGroup, UnpooledByteBufAllocator.DEFAULT, executor,
            scheduler, NullStatsLogger.INSTANCE);
    long flags = BookkeeperProtocol.GetBookieInfoRequest.Flags.FREE_DISK_SPACE_VALUE
            | BookkeeperProtocol.GetBookieInfoRequest.Flags.TOTAL_DISK_CAPACITY_VALUE;

    class CallbackObj {
        int rc;
        long requested;
        @SuppressWarnings("unused")
        long freeDiskSpace, totalDiskCapacity;
        CountDownLatch latch = new CountDownLatch(1);

        CallbackObj(long requested) {
            this.requested = requested;
            this.rc = 0;
            this.freeDiskSpace = 0L;
            this.totalDiskCapacity = 0L;
        }
    }
    CallbackObj obj = new CallbackObj(flags);
    bc.getBookieInfo(addr, flags, new GetBookieInfoCallback() {
        @Override
        public void getBookieInfoComplete(int rc, BookieInfo bInfo, Object ctx) {
            CallbackObj obj = (CallbackObj) ctx;
            obj.rc = rc;
            if (rc == Code.OK) {
                if ((obj.requested
                        & BookkeeperProtocol.GetBookieInfoRequest.Flags.FREE_DISK_SPACE_VALUE) != 0) {
                    obj.freeDiskSpace = bInfo.getFreeDiskSpace();
                }
                if ((obj.requested
                        & BookkeeperProtocol.GetBookieInfoRequest.Flags.TOTAL_DISK_CAPACITY_VALUE) != 0) {
                    obj.totalDiskCapacity = bInfo.getTotalDiskSpace();
                }
            }
            obj.latch.countDown();
        }

    }, obj);
    obj.latch.await();
    LOG.debug("Return code: " + obj.rc);
    assertTrue("GetBookieInfo failed with unexpected error code: " + obj.rc, obj.rc == Code.TimeoutException);
}

From source file:org.apache.bookkeeper.common.allocator.impl.ByteBufAllocatorBuilderTest.java

License:Apache License

@Test
public void testOomWithFallback() {
    ByteBufAllocator baseAlloc = mock(ByteBufAllocator.class);
    when(baseAlloc.directBuffer(anyInt(), anyInt())).thenThrow(outOfDirectMemException);

    AtomicReference<OutOfMemoryError> receivedException = new AtomicReference<>();

    ByteBufAllocator alloc = ByteBufAllocatorBuilder.create().pooledAllocator(baseAlloc)
            .unpooledAllocator(UnpooledByteBufAllocator.DEFAULT)
            .outOfMemoryPolicy(OutOfMemoryPolicy.FallbackToHeap).outOfMemoryListener((e) -> {
                receivedException.set(e);
            }).build();/*ww  w .  ja v  a  2s . co  m*/

    // Should not throw exception
    ByteBuf buf = alloc.buffer();
    assertEquals(UnpooledByteBufAllocator.DEFAULT, buf.alloc());

    // No notification should have been triggered
    assertEquals(null, receivedException.get());
}

From source file:org.apache.bookkeeper.common.allocator.impl.ByteBufAllocatorBuilderTest.java

License:Apache License

@Test
public void testUnpooled() {
    ByteBufAllocator alloc = ByteBufAllocatorBuilder.create().poolingPolicy(PoolingPolicy.UnpooledHeap).build();

    ByteBuf buf = alloc.buffer();//w w w .j  av a2  s  . c o  m
    assertEquals(UnpooledByteBufAllocator.DEFAULT, buf.alloc());
    assertTrue(buf.hasArray());

    ByteBuf buf2 = alloc.directBuffer();
    assertEquals(UnpooledByteBufAllocator.DEFAULT, buf2.alloc());
    assertFalse(buf2.hasArray());
}

From source file:org.apache.bookkeeper.common.allocator.impl.ByteBufAllocatorImpl.java

License:Apache License

ByteBufAllocatorImpl(ByteBufAllocator pooledAllocator, ByteBufAllocator unpooledAllocator,
        PoolingPolicy poolingPolicy, int poolingConcurrency, OutOfMemoryPolicy outOfMemoryPolicy,
        Consumer<OutOfMemoryError> outOfMemoryListener, LeakDetectionPolicy leakDetectionPolicy) {
    super(poolingPolicy == PoolingPolicy.PooledDirect /* preferDirect */);

    this.poolingPolicy = poolingPolicy;
    this.outOfMemoryPolicy = outOfMemoryPolicy;
    if (outOfMemoryListener == null) {
        this.outOfMemoryListener = (v) -> {
            log.error("Unable to allocate memory", v);
        };/*  w ww  .j  a v  a 2 s.  c o m*/
    } else {
        this.outOfMemoryListener = outOfMemoryListener;
    }

    if (poolingPolicy == PoolingPolicy.PooledDirect) {
        if (pooledAllocator == null) {
            if (poolingConcurrency == PooledByteBufAllocator.defaultNumDirectArena()) {
                // If all the parameters are the same as in the default Netty pool,
                // just reuse the static instance as the underlying allocator.
                this.pooledAllocator = PooledByteBufAllocator.DEFAULT;
            } else {
                this.pooledAllocator = new PooledByteBufAllocator(true /* preferDirect */,
                        poolingConcurrency /* nHeapArena */, poolingConcurrency /* nDirectArena */,
                        PooledByteBufAllocator.defaultPageSize(), PooledByteBufAllocator.defaultMaxOrder(),
                        PooledByteBufAllocator.defaultTinyCacheSize(),
                        PooledByteBufAllocator.defaultSmallCacheSize(),
                        PooledByteBufAllocator.defaultNormalCacheSize(),
                        PooledByteBufAllocator.defaultUseCacheForAllThreads());
            }
        } else {
            this.pooledAllocator = pooledAllocator;
        }
    } else {
        this.pooledAllocator = null;
    }

    this.unpooledAllocator = (unpooledAllocator != null) ? unpooledAllocator : UnpooledByteBufAllocator.DEFAULT;

    // The setting is static in Netty, so it will actually affect all
    // allocators
    switch (leakDetectionPolicy) {
    case Disabled:
        if (log.isDebugEnabled()) {
            log.debug("Disable Netty allocator leak detector");
        }
        ResourceLeakDetector.setLevel(Level.DISABLED);
        break;

    case Simple:
        log.info("Setting Netty allocator leak detector to Simple");
        ResourceLeakDetector.setLevel(Level.SIMPLE);
        break;

    case Advanced:
        log.info("Setting Netty allocator leak detector to Advanced");
        ResourceLeakDetector.setLevel(Level.ADVANCED);
        break;

    case Paranoid:
        log.info("Setting Netty allocator leak detector to Paranoid");
        ResourceLeakDetector.setLevel(Level.PARANOID);
        break;
    }
}

From source file:org.apache.bookkeeper.proto.BookieBackpressureTest.java

License:Apache License

private void mockJournal(Bookie bookie, long getDelay, long addDelay, long flushDelay) throws Exception {
    if (getDelay <= 0 && addDelay <= 0 && flushDelay <= 0) {
        return;//  w w  w  .  ja v  a  2  s .  co m
    }

    List<Journal> journals = getJournals(bookie);
    for (int i = 0; i < journals.size(); i++) {
        Journal mock = spy(journals.get(i));
        when(mock.getBufferedChannelBuilder()).thenReturn((FileChannel fc, int capacity) -> {
            SlowBufferedChannel sbc = new SlowBufferedChannel(UnpooledByteBufAllocator.DEFAULT, fc, capacity);
            sbc.setAddDelay(addDelay);
            sbc.setGetDelay(getDelay);
            sbc.setFlushDelay(flushDelay);
            return sbc;
        });

        journals.set(i, mock);
    }
}

From source file:org.apache.bookkeeper.proto.BookieProtoEncodingTest.java

License:Apache License

@Test
public void testV3ResponseDecoderNoFallback() throws Exception {
    AddResponse v2Resp = AddResponse.create(BookieProtocol.CURRENT_PROTOCOL_VERSION, BookieProtocol.EOK, 1L,
            2L);//w w  w  .j  a  v a  2s. c  o m

    BookkeeperProtocol.Response v3Resp = BookkeeperProtocol.Response.newBuilder()
            .setHeader(BKPacketHeader.newBuilder().setVersion(ProtocolVersion.VERSION_THREE).setTxnId(1L)
                    .setOperation(OperationType.ADD_ENTRY).build())
            .setStatus(StatusCode.EOK).setAddResponse(BookkeeperProtocol.AddResponse.newBuilder()
                    .setStatus(StatusCode.EOK).setLedgerId(1L).setEntryId(2L).build())
            .build();

    List<Object> outList = Lists.newArrayList();
    ChannelHandlerContext ctx = mock(ChannelHandlerContext.class);
    when(ctx.fireChannelRead(any())).thenAnswer((iom) -> {
        outList.add(iom.getArgument(0));
        return null;
    });

    ResponseEnDeCoderPreV3 v2Encoder = new ResponseEnDeCoderPreV3(registry);
    ResponseEnDecoderV3 v3Encoder = new ResponseEnDecoderV3(registry);

    ResponseDecoder v3Decoder = new ResponseDecoder(registry, false);
    try {
        v3Decoder.channelRead(ctx, v2Encoder.encode(v2Resp, UnpooledByteBufAllocator.DEFAULT));
        fail("V3 response decoder should fail on decoding v2 response");
    } catch (InvalidProtocolBufferException e) {
        // expected
    }
    assertEquals(0, outList.size());

    v3Decoder.channelRead(ctx, v3Encoder.encode(v3Resp, UnpooledByteBufAllocator.DEFAULT));
    assertEquals(1, outList.size());
}

From source file:org.apache.bookkeeper.proto.BookieProtoEncodingTest.java

License:Apache License

@Test(expected = IllegalStateException.class)
public void testV2RequestDecoderThrowExceptionOnUnknownRequests() throws Exception {
    RequestEnDeCoderPreV3 v2ReqEncoder = new RequestEnDeCoderPreV3(registry);
    RequestEnDecoderV3 v3ReqEncoder = new RequestEnDecoderV3(registry);

    BookkeeperProtocol.Request v3Req = BookkeeperProtocol.Request.newBuilder()
            .setHeader(BKPacketHeader.newBuilder().setVersion(ProtocolVersion.VERSION_THREE).setTxnId(1L)
                    .setOperation(OperationType.ADD_ENTRY).build())
            .setAddRequest(BookkeeperProtocol.AddRequest.newBuilder().setLedgerId(1L).setEntryId(2L)
                    .setMasterKey(ByteString.copyFrom("", UTF_8)).setFlag(Flag.RECOVERY_ADD)
                    .setBody(ByteString.copyFrom("test", UTF_8)))
            .build();/*w  ww . j  a  va 2s .  c o m*/

    v2ReqEncoder.decode((ByteBuf) v3ReqEncoder.encode(v3Req, UnpooledByteBufAllocator.DEFAULT));
}

From source file:org.apache.bookkeeper.proto.MockBookieClient.java

License:Apache License

public void seedEntries(BookieSocketAddress bookie, long ledgerId, long entryId, long lac) throws Exception {
    DigestManager digestManager = DigestManager.instantiate(ledgerId, new byte[0], DigestType.CRC32C,
            UnpooledByteBufAllocator.DEFAULT, false);
    ByteBuf entry = ByteBufList/*from w  w  w . ja v  a2 s  . c o  m*/
            .coalesce(digestManager.computeDigestAndPackageForSending(entryId, lac, 0, Unpooled.buffer(10)));

    LedgerData ledger = getBookieData(bookie).computeIfAbsent(ledgerId, LedgerData::new);
    ledger.addEntry(entryId, entry);
}

From source file:org.apache.bookkeeper.proto.PerChannelBookieClient.java

License:Apache License

public PerChannelBookieClient(ClientConfiguration conf, OrderedExecutor executor, EventLoopGroup eventLoopGroup,
        BookieSocketAddress addr, StatsLogger parentStatsLogger, ClientAuthProvider.Factory authProviderFactory,
        ExtensionRegistry extRegistry, PerChannelBookieClientPool pcbcPool) throws SecurityException {
    this(conf, executor, eventLoopGroup, UnpooledByteBufAllocator.DEFAULT, addr, NullStatsLogger.INSTANCE,
            authProviderFactory, extRegistry, pcbcPool, null);
}

From source file:org.apache.bookkeeper.proto.TestBookieRequestProcessor.java

License:Apache License

@Test
public void testConstructLongPollThreads() throws Exception {
    // long poll threads == read threads
    ServerConfiguration conf = new ServerConfiguration();
    try (BookieRequestProcessor processor = new BookieRequestProcessor(conf, mock(Bookie.class),
            NullStatsLogger.INSTANCE, null, UnpooledByteBufAllocator.DEFAULT)) {
        assertSame(processor.getReadThreadPool(), processor.getLongPollThreadPool());
    }/*  ww  w  .ja  v a  2s .c om*/

    // force create long poll threads if there is no read threads
    conf = new ServerConfiguration();
    conf.setNumReadWorkerThreads(0);
    try (BookieRequestProcessor processor = new BookieRequestProcessor(conf, mock(Bookie.class),
            NullStatsLogger.INSTANCE, null, UnpooledByteBufAllocator.DEFAULT)) {
        assertNull(processor.getReadThreadPool());
        assertNotNull(processor.getLongPollThreadPool());
    }

    // long poll threads and no read threads
    conf = new ServerConfiguration();
    conf.setNumReadWorkerThreads(2);
    conf.setNumLongPollWorkerThreads(2);
    try (BookieRequestProcessor processor = new BookieRequestProcessor(conf, mock(Bookie.class),
            NullStatsLogger.INSTANCE, null, UnpooledByteBufAllocator.DEFAULT)) {
        assertNotNull(processor.getReadThreadPool());
        assertNotNull(processor.getLongPollThreadPool());
        assertNotSame(processor.getReadThreadPool(), processor.getLongPollThreadPool());
    }
}