Search in sources :

Example 1 with OpListener

use of com.twitter.distributedlog.util.Transaction.OpListener 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

OpListener (com.twitter.distributedlog.util.Transaction.OpListener)1 ZKTransaction (com.twitter.distributedlog.zk.ZKTransaction)1 FutureEventListener (com.twitter.util.FutureEventListener)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 LedgerHandle (org.apache.bookkeeper.client.LedgerHandle)1