Search in sources :

Example 6 with ByteBufList

use of org.apache.bookkeeper.util.ByteBufList in project bookkeeper by apache.

the class TestPendingReadLacOp method testPendingReadLacOpMissingExplicitLAC.

@Test
public void testPendingReadLacOpMissingExplicitLAC() throws Exception {
    LedgerHandle lh = bkc.createLedger(3, 3, 2, BookKeeper.DigestType.CRC32, pwd);
    lh.append(data);
    lh.append(data);
    lh.append(data);
    final CompletableFuture<Long> result = new CompletableFuture<>();
    PendingReadLacOp pro = new PendingReadLacOp(lh, (rc, lac) -> result.complete(lac)) {

        @Override
        public void initiate() {
            for (int i = 0; i < lh.metadata.currentEnsemble.size(); i++) {
                final int index = i;
                ByteBufList buffer = lh.getDigestManager().computeDigestAndPackageForSending(2, 1, data.length, Unpooled.wrappedBuffer(data));
                bkc.scheduler.schedule(() -> {
                    readLacComplete(0, lh.getId(), null, Unpooled.copiedBuffer(buffer.toArray()), index);
                }, 0, TimeUnit.SECONDS);
                lh.bk.getBookieClient().readLac(lh.metadata.currentEnsemble.get(i), lh.ledgerId, this, i);
            }
        }
    };
    pro.initiate();
    assertEquals(1, result.get().longValue());
}
Also used : ByteBufList(org.apache.bookkeeper.util.ByteBufList) CompletableFuture(java.util.concurrent.CompletableFuture) Test(org.junit.Test)

Example 7 with ByteBufList

use of org.apache.bookkeeper.util.ByteBufList in project bookkeeper by apache.

the class ParallelLedgerRecoveryTest method testRecoveryOnEntryGap.

@Test
public void testRecoveryOnEntryGap() throws Exception {
    byte[] passwd = "recovery-on-entry-gap".getBytes(UTF_8);
    LedgerHandle lh = bkc.createLedger(1, 1, 1, DigestType.CRC32, passwd);
    for (int i = 0; i < 10; i++) {
        lh.addEntry(("recovery-on-entry-gap-" + i).getBytes(UTF_8));
    }
    // simulate ledger writer failure on concurrent writes causing gaps
    long entryId = 14;
    long lac = 8;
    byte[] data = "recovery-on-entry-gap-gap".getBytes(UTF_8);
    ByteBufList toSend = lh.macManager.computeDigestAndPackageForSending(entryId, lac, lh.getLength() + 100, Unpooled.wrappedBuffer(data, 0, data.length));
    final CountDownLatch addLatch = new CountDownLatch(1);
    final AtomicBoolean addSuccess = new AtomicBoolean(false);
    LOG.info("Add entry {} with lac = {}", entryId, lac);
    lh.bk.getBookieClient().addEntry(lh.metadata.currentEnsemble.get(0), lh.getId(), lh.ledgerKey, entryId, toSend, new WriteCallback() {

        @Override
        public void writeComplete(int rc, long ledgerId, long entryId, BookieSocketAddress addr, Object ctx) {
            addSuccess.set(BKException.Code.OK == rc);
            addLatch.countDown();
        }
    }, 0, BookieProtocol.FLAG_NONE);
    addLatch.await();
    assertTrue("add entry 14 should succeed", addSuccess.get());
    ClientConfiguration newConf = new ClientConfiguration();
    newConf.addConfiguration(baseClientConf);
    newConf.setEnableParallelRecoveryRead(true);
    newConf.setRecoveryReadBatchSize(10);
    BookKeeper newBk = new BookKeeper(newConf);
    final LedgerHandle recoverLh = newBk.openLedgerNoRecovery(lh.getId(), DigestType.CRC32, passwd);
    assertEquals("wrong lac found", 8L, recoverLh.getLastAddConfirmed());
    final CountDownLatch recoverLatch = new CountDownLatch(1);
    final AtomicLong newLac = new AtomicLong(-1);
    final AtomicBoolean isMetadataClosed = new AtomicBoolean(false);
    final AtomicInteger numSuccessCalls = new AtomicInteger(0);
    final AtomicInteger numFailureCalls = new AtomicInteger(0);
    recoverLh.recover(new GenericCallback<Void>() {

        @Override
        public void operationComplete(int rc, Void result) {
            if (BKException.Code.OK == rc) {
                newLac.set(recoverLh.getLastAddConfirmed());
                isMetadataClosed.set(recoverLh.getLedgerMetadata().isClosed());
                numSuccessCalls.incrementAndGet();
            } else {
                numFailureCalls.incrementAndGet();
            }
            recoverLatch.countDown();
        }
    });
    recoverLatch.await();
    assertEquals("wrong lac found", 9L, newLac.get());
    assertTrue("metadata isn't closed after recovery", isMetadataClosed.get());
    Thread.sleep(5000);
    assertEquals("recovery callback should be triggered only once", 1, numSuccessCalls.get());
    assertEquals("recovery callback should be triggered only once", 0, numFailureCalls.get());
}
Also used : ByteBufList(org.apache.bookkeeper.util.ByteBufList) WriteCallback(org.apache.bookkeeper.proto.BookkeeperInternalCallbacks.WriteCallback) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicLong(java.util.concurrent.atomic.AtomicLong) BookieSocketAddress(org.apache.bookkeeper.net.BookieSocketAddress) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ClientConfiguration(org.apache.bookkeeper.conf.ClientConfiguration) Test(org.junit.Test)

Aggregations

ByteBufList (org.apache.bookkeeper.util.ByteBufList)7 BookieSocketAddress (org.apache.bookkeeper.net.BookieSocketAddress)4 Test (org.junit.Test)4 CompletableFuture (java.util.concurrent.CompletableFuture)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 AtomicLong (java.util.concurrent.atomic.AtomicLong)2 ClientConfiguration (org.apache.bookkeeper.conf.ClientConfiguration)2 WriteCallback (org.apache.bookkeeper.proto.BookkeeperInternalCallbacks.WriteCallback)2 CountDownLatch (java.util.concurrent.CountDownLatch)1 ReadCallback (org.apache.bookkeeper.client.AsyncCallback.ReadCallback)1 BKDigestMatchException (org.apache.bookkeeper.client.BKException.BKDigestMatchException)1 BookieClient (org.apache.bookkeeper.proto.BookieClient)1 BookkeeperInternalCallbacks (org.apache.bookkeeper.proto.BookkeeperInternalCallbacks)1 ArgumentMatchers.anyLong (org.mockito.ArgumentMatchers.anyLong)1 Benchmark (org.openjdk.jmh.annotations.Benchmark)1