use of org.apache.distributedlog.zk.ZKTransaction in project bookkeeper by apache.
the class SimpleLedgerAllocator method cleanupAndClose.
private void cleanupAndClose(final CompletableFuture<Void> closePromise) {
LOG.info("Closing ledger allocator on {}.", allocatePath);
final ZKTransaction txn = new ZKTransaction(zkc);
// try obtain ledger handle
tryObtain(txn, new OpListener<LedgerHandle>() {
@Override
public void onCommit(LedgerHandle r) {
// no-op
complete();
}
@Override
public void onAbort(Throwable t) {
// no-op
complete();
}
private void complete() {
closePromise.complete(null);
LOG.info("Closed ledger allocator on {}.", allocatePath);
}
}).whenComplete(new FutureEventListener<LedgerHandle>() {
@Override
public void onSuccess(LedgerHandle lh) {
// try obtain succeed
// if we could obtain the ledger handle, we have the responsibility to close it
deleteLedger(lh.getId());
// wait for deletion to be completed
List<CompletableFuture<Void>> outstandingDeletions;
synchronized (ledgerDeletions) {
outstandingDeletions = Lists.newArrayList(ledgerDeletions);
}
FutureUtils.collect(outstandingDeletions).whenComplete(new FutureEventListener<List<Void>>() {
@Override
public void onSuccess(List<Void> values) {
txn.execute();
}
@Override
public void onFailure(Throwable cause) {
LOG.debug("Fail to obtain the allocated ledger handle when closing the allocator : ", cause);
closePromise.complete(null);
}
});
}
@Override
public void onFailure(Throwable cause) {
LOG.debug("Fail to obtain the allocated ledger handle when closing the allocator : ", cause);
closePromise.complete(null);
}
});
}
use of org.apache.distributedlog.zk.ZKTransaction in project bookkeeper by apache.
the class TestLedgerAllocator method testAllocation.
/**
* {@link https://issues.apache.org/jira/browse/DL-43}.
*/
@DistributedLogAnnotations.FlakyTest
@Ignore
@Test(timeout = 60000)
public void testAllocation() throws Exception {
String allocationPath = "/allocation1";
SimpleLedgerAllocator allocator = createAllocator(allocationPath);
allocator.allocate();
ZKTransaction txn = newTxn();
LedgerHandle lh = Utils.ioResult(allocator.tryObtain(txn, NULL_LISTENER));
logger.info("Try obtaining ledger handle {}", lh.getId());
byte[] data = zkc.get().getData(allocationPath, false, null);
assertEquals((Long) lh.getId(), Long.valueOf(new String(data, UTF_8)));
txn.addOp(DefaultZKOp.of(Op.setData("/unexistedpath", "data".getBytes(UTF_8), -1), null));
try {
Utils.ioResult(txn.execute());
fail("Should fail the transaction when setting unexisted path");
} catch (ZKException ke) {
// expected
logger.info("Should fail on executing transaction when setting unexisted path", ke);
}
data = zkc.get().getData(allocationPath, false, null);
assertEquals((Long) lh.getId(), Long.valueOf(new String(data, UTF_8)));
// Create new transaction to obtain the ledger again.
txn = newTxn();
// we could obtain the ledger if it was obtained
LedgerHandle newLh = Utils.ioResult(allocator.tryObtain(txn, NULL_LISTENER));
assertEquals(lh.getId(), newLh.getId());
Utils.ioResult(txn.execute());
data = zkc.get().getData(allocationPath, false, null);
assertEquals(0, data.length);
Utils.close(allocator);
}
use of org.apache.distributedlog.zk.ZKTransaction in project bookkeeper by apache.
the class TestLedgerAllocator method testCloseAllocatorAfterConfirm.
/**
* {@link https://issues.apache.org/jira/browse/DL-26}.
*/
@DistributedLogAnnotations.FlakyTest
@Ignore
@Test(timeout = 60000)
public void testCloseAllocatorAfterConfirm() throws Exception {
String allocationPath = "/allocation2";
SimpleLedgerAllocator allocator = createAllocator(allocationPath);
allocator.allocate();
ZKTransaction txn = newTxn();
// close during obtaining ledger.
LedgerHandle lh = Utils.ioResult(allocator.tryObtain(txn, NULL_LISTENER));
Utils.ioResult(txn.execute());
Utils.close(allocator);
byte[] data = zkc.get().getData(allocationPath, false, null);
assertEquals(0, data.length);
// the ledger is not deleted.
bkc.get().openLedger(lh.getId(), BookKeeper.DigestType.CRC32, dlConf.getBKDigestPW().getBytes(UTF_8));
}
use of org.apache.distributedlog.zk.ZKTransaction in project bookkeeper by apache.
the class TestLedgerAllocator method testCloseAllocatorAfterAbort.
@Test(timeout = 60000)
public void testCloseAllocatorAfterAbort() throws Exception {
String allocationPath = "/allocation3";
SimpleLedgerAllocator allocator = createAllocator(allocationPath);
allocator.allocate();
ZKTransaction txn = newTxn();
// close during obtaining ledger.
LedgerHandle lh = Utils.ioResult(allocator.tryObtain(txn, NULL_LISTENER));
txn.addOp(DefaultZKOp.of(Op.setData("/unexistedpath", "data".getBytes(UTF_8), -1), null));
try {
Utils.ioResult(txn.execute());
fail("Should fail the transaction when setting unexisted path");
} catch (ZKException ke) {
// expected
}
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.distributedlog.zk.ZKTransaction in project bookkeeper by apache.
the class TestLedgerAllocator method testObtainMultipleLedgers.
@Test(timeout = 60000)
public void testObtainMultipleLedgers() throws Exception {
String allocationPath = "/" + runtime.getMethodName();
SimpleLedgerAllocator allocator = createAllocator(allocationPath);
int numLedgers = 10;
Set<LedgerHandle> allocatedLedgers = new HashSet<LedgerHandle>();
for (int i = 0; i < numLedgers; i++) {
allocator.allocate();
ZKTransaction txn = newTxn();
LedgerHandle lh = Utils.ioResult(allocator.tryObtain(txn, NULL_LISTENER));
Utils.ioResult(txn.execute());
allocatedLedgers.add(lh);
}
assertEquals(numLedgers, allocatedLedgers.size());
}
Aggregations