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));
}
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();
}
}
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());
}
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);
}
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);
}
Aggregations