Search in sources :

Example 36 with LedgerHandle

use of org.apache.bookkeeper.client.LedgerHandle in project distributedlog by twitter.

the class TestLedgerAllocator method testCloseAllocatorDuringObtaining.

@Test(timeout = 60000)
public void testCloseAllocatorDuringObtaining() throws Exception {
    String allocationPath = "/allocation2";
    SimpleLedgerAllocator allocator = createAllocator(allocationPath);
    allocator.allocate();
    ZKTransaction txn = newTxn();
    // close during obtaining ledger.
    LedgerHandle lh = FutureUtils.result(allocator.tryObtain(txn, NULL_LISTENER));
    Utils.close(allocator);
    byte[] data = zkc.get().getData(allocationPath, false, null);
    assertEquals((Long) lh.getId(), Long.valueOf(new String(data, UTF_8)));
    // the ledger is not deleted
    bkc.get().openLedger(lh.getId(), BookKeeper.DigestType.CRC32, dlConf.getBKDigestPW().getBytes(UTF_8));
}
Also used : LedgerHandle(org.apache.bookkeeper.client.LedgerHandle) ZKTransaction(com.twitter.distributedlog.zk.ZKTransaction) Test(org.junit.Test)

Example 37 with LedgerHandle

use of org.apache.bookkeeper.client.LedgerHandle in project distributedlog by twitter.

the class TestLedgerHandleCache method testOpenLedgerWhenZkClosed.

@Test(timeout = 60000, expected = BKException.ZKException.class)
public void testOpenLedgerWhenZkClosed() throws Exception {
    ZooKeeperClient newZkc = TestZooKeeperClientBuilder.newBuilder().name("zkc-openledger-when-zk-closed").zkServers(zkServers).build();
    BookKeeperClient newBkc = BookKeeperClientBuilder.newBuilder().name("bkc-openledger-when-zk-closed").zkc(newZkc).ledgersPath(ledgersPath).dlConfig(conf).build();
    try {
        LedgerHandle lh = newBkc.get().createLedger(BookKeeper.DigestType.CRC32, "zkcClosed".getBytes(UTF_8));
        lh.close();
        newZkc.close();
        LedgerHandleCache cache = LedgerHandleCache.newBuilder().bkc(newBkc).conf(conf).build();
        // open ledger after zkc closed
        cache.openLedger(new LogSegmentMetadata.LogSegmentMetadataBuilder("", 2, lh.getId(), 1).setLogSegmentSequenceNo(lh.getId()).build(), false);
    } finally {
        newBkc.close();
    }
}
Also used : LedgerHandle(org.apache.bookkeeper.client.LedgerHandle) Test(org.junit.Test)

Example 38 with LedgerHandle

use of org.apache.bookkeeper.client.LedgerHandle in project distributedlog by twitter.

the class TestLedgerHandleCache method testOpenAndCloseLedger.

@Test(timeout = 60000)
public void testOpenAndCloseLedger() throws Exception {
    LedgerHandle lh = bkc.get().createLedger(1, 1, 1, BookKeeper.DigestType.CRC32, conf.getBKDigestPW().getBytes(UTF_8));
    LedgerHandleCache cache = LedgerHandleCache.newBuilder().bkc(bkc).conf(conf).build();
    LogSegmentMetadata segment = new LogSegmentMetadata.LogSegmentMetadataBuilder("/data", LogSegmentMetadata.LogSegmentMetadataVersion.VERSION_V5_SEQUENCE_ID, lh.getId(), 0L).build();
    LedgerDescriptor desc1 = cache.openLedger(segment, false);
    assertTrue(cache.handlesMap.containsKey(desc1));
    LedgerHandleCache.RefCountedLedgerHandle refLh = cache.handlesMap.get(desc1);
    assertEquals(1, refLh.getRefCount());
    cache.openLedger(segment, false);
    assertTrue(cache.handlesMap.containsKey(desc1));
    assertEquals(2, refLh.getRefCount());
    // close the ledger
    cache.closeLedger(desc1);
    assertTrue(cache.handlesMap.containsKey(desc1));
    assertEquals(1, refLh.getRefCount());
    cache.closeLedger(desc1);
    assertFalse(cache.handlesMap.containsKey(desc1));
    assertEquals(0, refLh.getRefCount());
}
Also used : LedgerHandle(org.apache.bookkeeper.client.LedgerHandle) Test(org.junit.Test)

Example 39 with LedgerHandle

use of org.apache.bookkeeper.client.LedgerHandle in project pulsar by yahoo.

the class ManagedCursorImpl method asyncCloseCursorLedger.

void asyncCloseCursorLedger(final AsyncCallbacks.CloseCallback callback, final Object ctx) {
    LedgerHandle lh = cursorLedger;
    ledger.mbean.startCursorLedgerCloseOp();
    log.info("[{}] [{}] Closing metadata ledger {}", ledger.getName(), name, lh.getId());
    lh.asyncClose(new CloseCallback() {

        @Override
        public void closeComplete(int rc, LedgerHandle lh, Object ctx) {
            ledger.mbean.endCursorLedgerCloseOp();
            if (rc == BKException.Code.OK) {
                callback.closeComplete(ctx);
            } else {
                callback.closeFailed(new ManagedLedgerException(BKException.getMessage(rc)), ctx);
            }
        }
    }, ctx);
}
Also used : ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) LedgerHandle(org.apache.bookkeeper.client.LedgerHandle) CloseCallback(org.apache.bookkeeper.client.AsyncCallback.CloseCallback)

Example 40 with LedgerHandle

use of org.apache.bookkeeper.client.LedgerHandle in project pulsar by yahoo.

the class ManagedLedgerImpl method asyncClose.

@Override
public synchronized void asyncClose(final CloseCallback callback, final Object ctx) {
    State state = STATE_UPDATER.get(this);
    if (state == State.Fenced) {
        factory.close(this);
        callback.closeFailed(new ManagedLedgerFencedException(), ctx);
        return;
    } else if (state == State.Closed) {
        if (log.isDebugEnabled()) {
            log.debug("[{}] Ignoring request to close a closed managed ledger", name);
        }
        callback.closeComplete(ctx);
        return;
    }
    log.info("[{}] Closing managed ledger", name);
    factory.close(this);
    STATE_UPDATER.set(this, State.Closed);
    LedgerHandle lh = currentLedger;
    if (log.isDebugEnabled()) {
        log.debug("[{}] Closing current writing ledger {}", name, lh.getId());
    }
    mbean.startDataLedgerCloseOp();
    lh.asyncClose((rc, lh1, ctx1) -> {
        if (log.isDebugEnabled()) {
            log.debug("[{}] Close complete for ledger {}: rc = {}", name, lh.getId(), rc);
        }
        mbean.endDataLedgerCloseOp();
        if (rc != BKException.Code.OK) {
            callback.closeFailed(new ManagedLedgerException(BKException.getMessage(rc)), ctx);
            return;
        }
        // Close all cursors in parallel
        List<CompletableFuture<Void>> futures = Lists.newArrayList();
        for (ManagedCursor cursor : cursors) {
            Futures.CloseFuture closeFuture = new Futures.CloseFuture();
            cursor.asyncClose(closeFuture, null);
            futures.add(closeFuture);
        }
        Futures.waitForAll(futures).thenRun(() -> {
            callback.closeComplete(ctx);
        }).exceptionally(exception -> {
            callback.closeFailed(new ManagedLedgerException(exception), ctx);
            return null;
        });
    }, null);
}
Also used : ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) CompletableFuture(java.util.concurrent.CompletableFuture) Futures(org.apache.bookkeeper.mledger.util.Futures) LedgerHandle(org.apache.bookkeeper.client.LedgerHandle) ManagedLedgerFencedException(org.apache.bookkeeper.mledger.ManagedLedgerException.ManagedLedgerFencedException) ManagedCursor(org.apache.bookkeeper.mledger.ManagedCursor)

Aggregations

LedgerHandle (org.apache.bookkeeper.client.LedgerHandle)51 Test (org.junit.Test)19 ZKTransaction (com.twitter.distributedlog.zk.ZKTransaction)13 LedgerEntry (org.apache.bookkeeper.client.LedgerEntry)12 ManagedLedgerException (org.apache.bookkeeper.mledger.ManagedLedgerException)12 CountDownLatch (java.util.concurrent.CountDownLatch)10 IOException (java.io.IOException)9 Entry (org.apache.bookkeeper.mledger.Entry)8 BKException (org.apache.bookkeeper.client.BKException)7 ReadEntriesCallback (org.apache.bookkeeper.mledger.AsyncCallbacks.ReadEntriesCallback)7 Test (org.testng.annotations.Test)7 List (java.util.List)6 BookKeeper (org.apache.bookkeeper.client.BookKeeper)6 ZKDistributedLock (com.twitter.distributedlog.lock.ZKDistributedLock)5 Future (com.twitter.util.Future)5 ArrayList (java.util.ArrayList)5 lombok.val (lombok.val)5 ZKException (com.twitter.distributedlog.exceptions.ZKException)4 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)3 Lists (com.google.common.collect.Lists)3