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.bookie.storage.ldb.LocationsIndexRebuildTest.java

License:Apache License

@Test
public void test() throws Exception {
    File tmpDir = File.createTempFile("bkTest", ".dir");
    tmpDir.delete();//from   w w  w .  ja v  a 2 s.co  m
    tmpDir.mkdir();
    File curDir = Bookie.getCurrentDirectory(tmpDir);
    Bookie.checkDirectoryStructure(curDir);

    System.out.println(tmpDir);

    ServerConfiguration conf = TestBKConfiguration.newServerConfiguration();
    conf.setLedgerDirNames(new String[] { tmpDir.toString() });
    conf.setLedgerStorageClass(DbLedgerStorage.class.getName());
    LedgerDirsManager ledgerDirsManager = new LedgerDirsManager(conf, conf.getLedgerDirs(),
            new DiskChecker(conf.getDiskUsageThreshold(), conf.getDiskUsageWarnThreshold()));

    DbLedgerStorage ledgerStorage = new DbLedgerStorage();
    ledgerStorage.initialize(conf, null, ledgerDirsManager, ledgerDirsManager, null, checkpointSource,
            checkpointer, NullStatsLogger.INSTANCE, UnpooledByteBufAllocator.DEFAULT);

    // Insert some ledger & entries in the storage
    for (long ledgerId = 0; ledgerId < 5; ledgerId++) {
        ledgerStorage.setMasterKey(ledgerId, ("ledger-" + ledgerId).getBytes());
        ledgerStorage.setFenced(ledgerId);

        for (long entryId = 0; entryId < 100; entryId++) {
            ByteBuf entry = Unpooled.buffer(128);
            entry.writeLong(ledgerId);
            entry.writeLong(entryId);
            entry.writeBytes(("entry-" + entryId).getBytes());

            ledgerStorage.addEntry(entry);
        }
    }

    ledgerStorage.flush();
    ledgerStorage.shutdown();

    // Rebuild index through the tool
    BookieShell shell = new BookieShell();
    shell.setConf(conf);
    int res = shell.run(new String[] { "rebuild-db-ledger-locations-index" });

    Assert.assertEquals(0, res);

    // Verify that db index has the same entries
    ledgerStorage = new DbLedgerStorage();
    ledgerStorage.initialize(conf, null, ledgerDirsManager, ledgerDirsManager, null, checkpointSource,
            checkpointer, NullStatsLogger.INSTANCE, UnpooledByteBufAllocator.DEFAULT);

    Set<Long> ledgers = Sets.newTreeSet(ledgerStorage.getActiveLedgersInRange(0, Long.MAX_VALUE));
    Assert.assertEquals(Sets.newTreeSet(Lists.newArrayList(0L, 1L, 2L, 3L, 4L)), ledgers);

    for (long ledgerId = 0; ledgerId < 5; ledgerId++) {
        Assert.assertEquals(true, ledgerStorage.isFenced(ledgerId));
        Assert.assertEquals("ledger-" + ledgerId, new String(ledgerStorage.readMasterKey(ledgerId)));

        ByteBuf lastEntry = ledgerStorage.getLastEntry(ledgerId);
        assertEquals(ledgerId, lastEntry.readLong());
        long lastEntryId = lastEntry.readLong();
        assertEquals(99, lastEntryId);

        for (long entryId = 0; entryId < 100; entryId++) {
            ByteBuf entry = Unpooled.buffer(1024);
            entry.writeLong(ledgerId);
            entry.writeLong(entryId);
            entry.writeBytes(("entry-" + entryId).getBytes());

            ByteBuf result = ledgerStorage.getEntry(ledgerId, entryId);
            Assert.assertEquals(entry, result);
        }
    }

    ledgerStorage.shutdown();
    FileUtils.forceDelete(tmpDir);
}

From source file:org.apache.bookkeeper.bookie.storage.ldb.ReadCacheTest.java

License:Apache License

@Test
public void simple() {
    ReadCache cache = new ReadCache(UnpooledByteBufAllocator.DEFAULT, 10 * 1024);

    assertEquals(0, cache.count());//from   w  w  w.j  a  v a2  s.c  o  m
    assertEquals(0, cache.size());

    ByteBuf entry = Unpooled.wrappedBuffer(new byte[1024]);
    cache.put(1, 0, entry);

    assertEquals(1, cache.count());
    assertEquals(1024, cache.size());

    assertEquals(entry, cache.get(1, 0));
    assertNull(cache.get(1, 1));

    for (int i = 1; i < 10; i++) {
        cache.put(1, i, entry);
    }

    assertEquals(10, cache.count());
    assertEquals(10 * 1024, cache.size());

    cache.put(1, 10, entry);

    // First half of entries will have been evicted
    for (int i = 0; i < 5; i++) {
        assertNull(cache.get(1, i));
    }

    for (int i = 5; i < 11; i++) {
        assertEquals(entry, cache.get(1, i));
    }

    cache.close();
}

From source file:org.apache.bookkeeper.bookie.storage.ldb.ReadCacheTest.java

License:Apache License

@Test
public void emptyCache() {
    ReadCache cache = new ReadCache(UnpooledByteBufAllocator.DEFAULT, 10 * 1024);

    assertEquals(0, cache.count());/*from ww  w  .j ava  2 s  .  co m*/
    assertEquals(0, cache.size());
    assertEquals(null, cache.get(0, 0));

    cache.close();
}

From source file:org.apache.bookkeeper.bookie.storage.ldb.ReadCacheTest.java

License:Apache License

@Test
public void multipleSegments() {
    // Test with multiple smaller segments
    ReadCache cache = new ReadCache(UnpooledByteBufAllocator.DEFAULT, 10 * 1024, 2 * 1024);

    assertEquals(0, cache.count());/*from  w  w w  .java 2s. c  om*/
    assertEquals(0, cache.size());

    for (int i = 0; i < 10; i++) {
        ByteBuf entry = Unpooled.wrappedBuffer(new byte[1024]);
        entry.setInt(0, i);
        cache.put(1, i, entry);
    }

    for (int i = 0; i < 10; i++) {
        ByteBuf res = cache.get(1, i);
        assertEquals(1, res.refCnt());

        assertEquals(1024, res.readableBytes());
        assertEquals(i, res.getInt(0));
    }

    assertEquals(10, cache.count());
    assertEquals(10 * 1024, cache.size());

    // Putting one more entry, should trigger the 1st segment rollover
    ByteBuf entry = Unpooled.wrappedBuffer(new byte[1024]);
    cache.put(2, 0, entry);

    assertEquals(9, cache.count());
    assertEquals(9 * 1024, cache.size());

    cache.close();
}

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

License:Apache License

/**
 * Allow to extend BookKeeper for mocking in unit tests.
 *///w ww .  j  a  va 2  s  .  c o m
@VisibleForTesting
BookKeeper() {
    conf = new ClientConfiguration();
    internalConf = ClientInternalConf.fromConfig(conf);
    statsLogger = NullStatsLogger.INSTANCE;
    clientStats = BookKeeperClientStats.newInstance(statsLogger);
    scheduler = null;
    requestTimer = null;
    metadataDriver = null;
    placementPolicy = null;
    ownTimer = false;
    mainWorkerPool = null;
    ledgerManagerFactory = null;
    ledgerManager = null;
    ledgerIdGenerator = null;
    featureProvider = null;
    eventLoopGroup = null;
    bookieWatcher = null;
    bookieInfoScheduler = null;
    bookieClient = null;
    allocator = UnpooledByteBufAllocator.DEFAULT;
}

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

License:Apache License

public static ByteBuf generatePacket(long ledgerId, long entryId, long lastAddConfirmed, long length,
        byte[] data, int offset, int len) throws GeneralSecurityException {
    DigestManager dm = DigestManager.instantiate(ledgerId, new byte[2], DigestType.CRC32,
            UnpooledByteBufAllocator.DEFAULT, true);
    return ByteBufList.coalesce(dm.computeDigestAndPackageForSending(entryId, lastAddConfirmed, length,
            Unpooled.wrappedBuffer(data, offset, len)));
}

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

License:Apache License

@Before
public void setup() throws Exception {
    deferredBookieForceLedgerResponses = new ConcurrentHashMap<>();
    suspendedBookiesForForceLedgerAcks = Collections.synchronizedSet(new HashSet<>());
    mockLedgerMetadataRegistry = new ConcurrentHashMap<>();
    mockLedgerData = new ConcurrentHashMap<>();
    mockNextLedgerId = new AtomicLong(1);
    fencedLedgers = new ConcurrentSkipListSet<>();
    scheduler = OrderedScheduler.newSchedulerBuilder().numThreads(4).name("bk-test").build();
    executor = OrderedExecutor.newBuilder().build();
    bookieWatcher = mock(BookieWatcher.class);

    bookieClient = mock(BookieClient.class);
    ledgerManager = mock(LedgerManager.class);
    ledgerIdGenerator = mock(LedgerIdGenerator.class);

    bk = mock(BookKeeper.class);

    failedBookies = new ArrayList<>();
    availableBookies = new HashSet<>();

    when(bk.getCloseLock()).thenReturn(new ReentrantReadWriteLock());
    when(bk.isClosed()).thenReturn(false);
    when(bk.getBookieWatcher()).thenReturn(bookieWatcher);
    when(bk.getMainWorkerPool()).thenReturn(executor);
    when(bk.getBookieClient()).thenReturn(bookieClient);
    when(bk.getScheduler()).thenReturn(scheduler);

    setBookKeeperConfig(new ClientConfiguration());
    when(bk.getStatsLogger()).thenReturn(NullStatsLogger.INSTANCE);
    BookKeeperClientStats clientStats = BookKeeperClientStats.newInstance(NullStatsLogger.INSTANCE);
    ClientContext clientCtx = new ClientContext() {
        @Override//from   w ww  .  j a  v a  2  s.  c  om
        public ClientInternalConf getConf() {
            return ClientInternalConf.fromConfig(bk.getConf());
        }

        @Override
        public LedgerManager getLedgerManager() {
            return ledgerManager;
        }

        @Override
        public BookieWatcher getBookieWatcher() {
            return bookieWatcher;
        }

        @Override
        public EnsemblePlacementPolicy getPlacementPolicy() {
            return null;
        }

        @Override
        public BookieClient getBookieClient() {
            return bookieClient;
        }

        @Override
        public OrderedExecutor getMainWorkerPool() {
            return scheduler;
        }

        @Override
        public OrderedScheduler getScheduler() {
            return scheduler;
        }

        @Override
        public BookKeeperClientStats getClientStats() {
            return clientStats;
        }

        @Override
        public boolean isClientClosed() {
            return bk.isClosed();
        }

        @Override
        public ByteBufAllocator getByteBufAllocator() {
            return UnpooledByteBufAllocator.DEFAULT;
        }
    };
    when(bk.getClientCtx()).thenReturn(clientCtx);
    when(bk.getLedgerManager()).thenReturn(ledgerManager);
    when(bk.getLedgerIdGenerator()).thenReturn(ledgerIdGenerator);
    when(bk.getReturnRc(anyInt())).thenAnswer(invocationOnMock -> invocationOnMock.getArgument(0));
    when(bookieClient.isWritable(any(), anyLong())).thenReturn(true);

    setupLedgerIdGenerator();
    setupCreateLedgerMetadata();
    setupReadLedgerMetadata();
    setupWriteLedgerMetadata();
    setupRemoveLedgerMetadata();
    setupRegisterLedgerMetadataListener();
    setupBookieWatcherForNewEnsemble();
    setupBookieWatcherForEnsembleChange();
    setupBookieClientReadEntry();
    setupBookieClientAddEntry();
    setupBookieClientForceLedger();
}

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

License:Apache License

private DigestManager getDigestType(long ledgerId) throws GeneralSecurityException {
    LedgerMetadata metadata = mockLedgerMetadataRegistry.get(ledgerId);
    return DigestManager.instantiate(ledgerId, metadata.getPassword(),
            org.apache.bookkeeper.client.BookKeeper.DigestType
                    .toProtoDigestType(org.apache.bookkeeper.client.BookKeeper.DigestType
                            .fromApiDigestType(metadata.getDigestType())),
            UnpooledByteBufAllocator.DEFAULT, false);
}

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

License:Apache License

static MockClientContext create() throws Exception {
    ClientConfiguration conf = new ClientConfiguration();
    OrderedScheduler scheduler = OrderedScheduler.newSchedulerBuilder().name("mock-executor").numThreads(1)
            .build();/*from  www.j a  v a2s  .  c o m*/
    MockRegistrationClient regClient = new MockRegistrationClient();
    EnsemblePlacementPolicy placementPolicy = new DefaultEnsemblePlacementPolicy();
    BookieWatcherImpl bookieWatcherImpl = new BookieWatcherImpl(conf, placementPolicy, regClient,
            NullStatsLogger.INSTANCE);
    bookieWatcherImpl.initialBlockingBookieRead();

    return new MockClientContext().setConf(ClientInternalConf.fromConfig(conf))
            .setLedgerManager(new MockLedgerManager()).setBookieWatcher(bookieWatcherImpl)
            .setPlacementPolicy(placementPolicy).setRegistrationClient(regClient)
            .setBookieClient(new MockBookieClient(scheduler))
            .setByteBufAllocator(UnpooledByteBufAllocator.DEFAULT).setMainWorkerPool(scheduler)
            .setScheduler(scheduler).setClientStats(BookKeeperClientStats.newInstance(NullStatsLogger.INSTANCE))
            .setIsClientClosed(() -> false);
}

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

License:Apache License

@Before
public void setup() throws Exception {
    // stats/*  w  w w.j  a  va 2  s.  c o m*/
    clientStats = BookKeeperClientStats.newInstance(testStatsProvider.getStatsLogger(""));
    // policy
    ClientConfiguration conf = new ClientConfiguration();
    conf.setFirstSpeculativeReadLACTimeout(100);
    conf.setMaxSpeculativeReadLACTimeout(200);
    conf.setSpeculativeReadLACTimeoutBackoffMultiplier(2);

    internalConf = ClientInternalConf.fromConfig(conf);

    // metadata
    ArrayList<BookieSocketAddress> ensemble = new ArrayList<>(3);
    for (int i = 0; i < 3; i++) {
        ensemble.add(new BookieSocketAddress("127.0.0.1", 3181 + i));
    }
    this.ledgerMetadata = LedgerMetadataBuilder.create().withEnsembleSize(3).withWriteQuorumSize(2)
            .withAckQuorumSize(2).withPassword(new byte[0]).withDigestType(DigestType.CRC32.toApiDigestType())
            .newEnsembleEntry(0L, ensemble).build();
    this.distributionSchedule = new RoundRobinDistributionSchedule(3, 2, 3);
    // schedulers
    this.scheduler = Executors.newSingleThreadScheduledExecutor();
    this.orderedScheduler = OrderedScheduler.newSchedulerBuilder().name("test-ordered-scheduler").numThreads(1)
            .build();

    this.mockBookieClient = mock(BookieClient.class);
    this.mockPlacementPolicy = mock(EnsemblePlacementPolicy.class);
    this.mockClientCtx = mock(ClientContext.class);
    when(mockClientCtx.getBookieClient()).thenReturn(mockBookieClient);
    when(mockClientCtx.getPlacementPolicy()).thenReturn(mockPlacementPolicy);
    when(mockClientCtx.getConf()).thenReturn(internalConf);
    when(mockClientCtx.getScheduler()).thenReturn(orderedScheduler);
    when(mockClientCtx.getMainWorkerPool()).thenReturn(orderedScheduler);
    when(mockClientCtx.getClientStats()).thenReturn(clientStats);
    this.mockLh = mock(LedgerHandle.class);

    when(mockLh.getId()).thenReturn(LEDGERID);
    when(mockLh.getCurrentEnsemble()).thenReturn(ensemble);
    when(mockLh.getLedgerMetadata()).thenReturn(ledgerMetadata);
    when(mockLh.getDistributionSchedule()).thenReturn(distributionSchedule);
    digestManager = new DummyDigestManager(LEDGERID, false, UnpooledByteBufAllocator.DEFAULT);
    when(mockLh.getDigestManager()).thenReturn(digestManager);
}