Search in sources :

Example 1 with WriteAdvHandle

use of org.apache.bookkeeper.client.api.WriteAdvHandle in project bookkeeper by apache.

the class BookieWriteLedgerTest method testLedgerCreateAdvWithLedgerIdInLoop.

/**
 * In a loop create/write/delete the ledger with same ledgerId through
 * the functionality of Advanced Ledger which accepts ledgerId as input.
 *
 * @throws Exception
 */
@Test
public void testLedgerCreateAdvWithLedgerIdInLoop() throws Exception {
    int ledgerCount = 40;
    long maxId = 9999999999L;
    if (baseConf.getLedgerManagerFactoryClass().equals(LongHierarchicalLedgerManagerFactory.class)) {
        // since LongHierarchicalLedgerManager supports ledgerIds of decimal length upto 19 digits but other
        // LedgerManagers only upto 10 decimals
        maxId = Long.MAX_VALUE;
    }
    // generate a stream of ledger ids
    rng.longs(ledgerCount, 0, maxId).mapToObj(ledgerId -> {
        // create a ledger for each ledger id
        LOG.info("Creating adv ledger with id {}", ledgerId);
        return bkc.newCreateLedgerOp().withEnsembleSize(1).withWriteQuorumSize(1).withAckQuorumSize(1).withDigestType(org.apache.bookkeeper.client.api.DigestType.CRC32).withPassword(ledgerPassword).makeAdv().withLedgerId(ledgerId).execute().thenApply(writer -> {
            // Add entries to ledger when created
            LOG.info("Writing stream of {} entries to {}", numEntriesToWrite, ledgerId);
            List<ByteBuf> entries = rng.ints(numEntriesToWrite, 0, maxInt).mapToObj(i -> {
                ByteBuf entry = Unpooled.buffer(4);
                entry.retain();
                entry.writeInt(i);
                return entry;
            }).collect(Collectors.toList());
            CompletableFuture<?> lastRequest = null;
            int i = 0;
            for (ByteBuf entry : entries) {
                long entryId = i++;
                LOG.info("Writing {}:{} as {}", ledgerId, entryId, entry.slice().readInt());
                lastRequest = writer.writeAsync(entryId, entry);
            }
            lastRequest.join();
            return Pair.of(writer, entries);
        });
    }).parallel().map(// wait for all creations and adds in parallel
    CompletableFuture::join).forEach(e -> {
        // check that each set of adds succeeded
        try {
            WriteAdvHandle handle = e.getLeft();
            List<ByteBuf> entries = e.getRight();
            // Read and verify
            LOG.info("Read entries for ledger: {}", handle.getId());
            readEntries(handle, entries);
            entries.forEach(ByteBuf::release);
            handle.close();
            bkc.deleteLedger(handle.getId());
        } catch (InterruptedException ie) {
            Thread.currentThread().interrupt();
            Assert.fail("Test interrupted");
        } catch (Exception ex) {
            LOG.info("Readback failed with exception", ex);
            Assert.fail("Readback failed " + ex.getMessage());
        }
    });
}
Also used : BKLedgerClosedException(org.apache.bookkeeper.client.BKException.BKLedgerClosedException) Whitebox(org.powermock.reflect.Whitebox) BookKeeperClusterTestCase(org.apache.bookkeeper.test.BookKeeperClusterTestCase) ADD_OP_UR(org.apache.bookkeeper.client.BookKeeperClientStats.ADD_OP_UR) Enumeration(java.util.Enumeration) LoggerFactory(org.slf4j.LoggerFactory) READ_OP_DM(org.apache.bookkeeper.client.BookKeeperClientStats.READ_OP_DM) HashMap(java.util.HashMap) Random(java.util.Random) CompletableFuture(java.util.concurrent.CompletableFuture) CLIENT_SCOPE(org.apache.bookkeeper.client.BookKeeperClientStats.CLIENT_SCOPE) ByteBuffer(java.nio.ByteBuffer) Unpooled(io.netty.buffer.Unpooled) ArrayList(java.util.ArrayList) ByteBuf(io.netty.buffer.ByteBuf) Pair(org.apache.commons.lang3.tuple.Pair) Map(java.util.Map) ADD_OP(org.apache.bookkeeper.client.BookKeeperClientStats.ADD_OP) Assert.fail(org.junit.Assert.fail) Bookie(org.apache.bookkeeper.bookie.Bookie) BookieException(org.apache.bookkeeper.bookie.BookieException) Before(org.junit.Before) AddCallback(org.apache.bookkeeper.client.AsyncCallback.AddCallback) DigestType(org.apache.bookkeeper.client.BookKeeper.DigestType) Logger(org.slf4j.Logger) KeeperException(org.apache.zookeeper.KeeperException) LongHierarchicalLedgerManagerFactory(org.apache.bookkeeper.meta.LongHierarchicalLedgerManagerFactory) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) Test(org.junit.Test) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) ServerConfiguration(org.apache.bookkeeper.conf.ServerConfiguration) ExecutionException(java.util.concurrent.ExecutionException) ReadHandle(org.apache.bookkeeper.client.api.ReadHandle) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) BookieSocketAddress(org.apache.bookkeeper.net.BookieSocketAddress) WriteAdvHandle(org.apache.bookkeeper.client.api.WriteAdvHandle) Assert(org.junit.Assert) LedgerEntries(org.apache.bookkeeper.client.api.LedgerEntries) Assert.assertEquals(org.junit.Assert.assertEquals) WriteAdvHandle(org.apache.bookkeeper.client.api.WriteAdvHandle) CompletableFuture(java.util.concurrent.CompletableFuture) ArrayList(java.util.ArrayList) List(java.util.List) ByteBuf(io.netty.buffer.ByteBuf) BKLedgerClosedException(org.apache.bookkeeper.client.BKException.BKLedgerClosedException) BookieException(org.apache.bookkeeper.bookie.BookieException) KeeperException(org.apache.zookeeper.KeeperException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Aggregations

ByteBuf (io.netty.buffer.ByteBuf)1 Unpooled (io.netty.buffer.Unpooled)1 IOException (java.io.IOException)1 ByteBuffer (java.nio.ByteBuffer)1 ArrayList (java.util.ArrayList)1 Enumeration (java.util.Enumeration)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Random (java.util.Random)1 UUID (java.util.UUID)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 ExecutionException (java.util.concurrent.ExecutionException)1 Collectors (java.util.stream.Collectors)1 Bookie (org.apache.bookkeeper.bookie.Bookie)1 BookieException (org.apache.bookkeeper.bookie.BookieException)1 AddCallback (org.apache.bookkeeper.client.AsyncCallback.AddCallback)1 BKLedgerClosedException (org.apache.bookkeeper.client.BKException.BKLedgerClosedException)1 DigestType (org.apache.bookkeeper.client.BookKeeper.DigestType)1