Search in sources :

Example 16 with ZKTransaction

use of com.twitter.distributedlog.zk.ZKTransaction in project distributedlog by twitter.

the class SimpleLedgerAllocator method cleanupAndClose.

private void cleanupAndClose(final Promise<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() {
            FutureUtils.setValue(closePromise, null);
            LOG.info("Closed ledger allocator on {}.", allocatePath);
        }
    }).addEventListener(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<Future<Void>> outstandingDeletions;
            synchronized (ledgerDeletions) {
                outstandingDeletions = Lists.newArrayList(ledgerDeletions);
            }
            Future.collect(outstandingDeletions).addEventListener(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);
                    FutureUtils.setValue(closePromise, null);
                }
            });
        }

        @Override
        public void onFailure(Throwable cause) {
            LOG.debug("Fail to obtain the allocated ledger handle when closing the allocator : ", cause);
            FutureUtils.setValue(closePromise, null);
        }
    });
}
Also used : OpListener(com.twitter.distributedlog.util.Transaction.OpListener) LedgerHandle(org.apache.bookkeeper.client.LedgerHandle) LinkedList(java.util.LinkedList) List(java.util.List) FutureEventListener(com.twitter.util.FutureEventListener) ZKTransaction(com.twitter.distributedlog.zk.ZKTransaction)

Aggregations

ZKTransaction (com.twitter.distributedlog.zk.ZKTransaction)16 LedgerHandle (org.apache.bookkeeper.client.LedgerHandle)13 Test (org.junit.Test)13 IOException (java.io.IOException)5 ZKException (com.twitter.distributedlog.exceptions.ZKException)4 HashSet (java.util.HashSet)3 Stat (org.apache.zookeeper.data.Stat)3 FutureEventListener (com.twitter.util.FutureEventListener)2 LedgerEntry (org.apache.bookkeeper.client.LedgerEntry)2 ZkVersion (org.apache.bookkeeper.meta.ZkVersion)2 Versioned (org.apache.bookkeeper.versioning.Versioned)2 Ignore (org.junit.Ignore)2 DistributedLogConfiguration (com.twitter.distributedlog.DistributedLogConfiguration)1 AllocationException (com.twitter.distributedlog.bk.SimpleLedgerAllocator.AllocationException)1 DLIllegalStateException (com.twitter.distributedlog.exceptions.DLIllegalStateException)1 EndOfStreamException (com.twitter.distributedlog.exceptions.EndOfStreamException)1 TransactionIdOutOfOrderException (com.twitter.distributedlog.exceptions.TransactionIdOutOfOrderException)1 UnexpectedException (com.twitter.distributedlog.exceptions.UnexpectedException)1 OpListener (com.twitter.distributedlog.util.Transaction.OpListener)1 LinkedList (java.util.LinkedList)1