Search in sources :

Example 1 with ZKTransaction

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);
        }
    });
}
Also used : OpListener(org.apache.distributedlog.util.Transaction.OpListener) LedgerHandle(org.apache.bookkeeper.client.LedgerHandle) LinkedList(java.util.LinkedList) List(java.util.List) FutureEventListener(org.apache.bookkeeper.common.concurrent.FutureEventListener) ZKTransaction(org.apache.distributedlog.zk.ZKTransaction)

Example 2 with ZKTransaction

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);
}
Also used : ZKException(org.apache.distributedlog.exceptions.ZKException) LedgerHandle(org.apache.bookkeeper.client.LedgerHandle) ZKTransaction(org.apache.distributedlog.zk.ZKTransaction) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 3 with ZKTransaction

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));
}
Also used : LedgerHandle(org.apache.bookkeeper.client.LedgerHandle) ZKTransaction(org.apache.distributedlog.zk.ZKTransaction) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 4 with ZKTransaction

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));
}
Also used : ZKException(org.apache.distributedlog.exceptions.ZKException) LedgerHandle(org.apache.bookkeeper.client.LedgerHandle) ZKTransaction(org.apache.distributedlog.zk.ZKTransaction) Test(org.junit.Test)

Example 5 with ZKTransaction

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());
}
Also used : LedgerHandle(org.apache.bookkeeper.client.LedgerHandle) ZKTransaction(org.apache.distributedlog.zk.ZKTransaction) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

ZKTransaction (org.apache.distributedlog.zk.ZKTransaction)14 Test (org.junit.Test)13 LedgerHandle (org.apache.bookkeeper.client.LedgerHandle)12 ZKException (org.apache.distributedlog.exceptions.ZKException)4 IOException (java.io.IOException)3 HashSet (java.util.HashSet)3 Stat (org.apache.zookeeper.data.Stat)3 LedgerEntry (org.apache.bookkeeper.client.LedgerEntry)2 LongVersion (org.apache.bookkeeper.versioning.LongVersion)2 Versioned (org.apache.bookkeeper.versioning.Versioned)2 Ignore (org.junit.Ignore)2 LinkedList (java.util.LinkedList)1 List (java.util.List)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 BKException (org.apache.bookkeeper.client.BKException)1 FutureEventListener (org.apache.bookkeeper.common.concurrent.FutureEventListener)1 DistributedLogConfiguration (org.apache.distributedlog.DistributedLogConfiguration)1 AllocationException (org.apache.distributedlog.bk.SimpleLedgerAllocator.AllocationException)1 OpListener (org.apache.distributedlog.util.Transaction.OpListener)1