Search in sources :

Example 1 with OpListener

use of org.apache.distributedlog.util.Transaction.OpListener 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 OpListener

use of org.apache.distributedlog.util.Transaction.OpListener in project bookkeeper by apache.

the class ZKLogSegmentMetadataStore method deleteLogSegment.

@Override
public void deleteLogSegment(Transaction<Object> txn, final LogSegmentMetadata segment, final OpListener<Void> listener) {
    Op deleteOp = Op.delete(segment.getZkPath(), -1);
    logger.info("Delete segment : {}", segment);
    txn.addOp(DefaultZKOp.of(deleteOp, new OpListener<Void>() {

        @Override
        public void onCommit(Void r) {
            if (null != listener) {
                listener.onCommit(r);
            }
        }

        @Override
        public void onAbort(Throwable t) {
            logger.info("Aborted transaction on deleting segment {}", segment);
            KeeperException.Code kc;
            if (t instanceof KeeperException) {
                kc = ((KeeperException) t).code();
            } else if (t instanceof ZKException) {
                kc = ((ZKException) t).getKeeperExceptionCode();
            } else {
                abortListener(t);
                return;
            }
            if (KeeperException.Code.NONODE == kc) {
                abortListener(new LogSegmentNotFoundException(segment.getZkPath()));
                return;
            }
            abortListener(t);
        }

        private void abortListener(Throwable t) {
            if (null != listener) {
                listener.onAbort(t);
            }
        }
    }));
}
Also used : ZKOp(org.apache.distributedlog.zk.ZKOp) Op(org.apache.zookeeper.Op) DefaultZKOp(org.apache.distributedlog.zk.DefaultZKOp) ZKVersionedSetOp(org.apache.distributedlog.zk.ZKVersionedSetOp) ZKException(org.apache.distributedlog.exceptions.ZKException) OpListener(org.apache.distributedlog.util.Transaction.OpListener) KeeperException(org.apache.zookeeper.KeeperException) LogSegmentNotFoundException(org.apache.distributedlog.exceptions.LogSegmentNotFoundException)

Aggregations

OpListener (org.apache.distributedlog.util.Transaction.OpListener)2 LinkedList (java.util.LinkedList)1 List (java.util.List)1 LedgerHandle (org.apache.bookkeeper.client.LedgerHandle)1 FutureEventListener (org.apache.bookkeeper.common.concurrent.FutureEventListener)1 LogSegmentNotFoundException (org.apache.distributedlog.exceptions.LogSegmentNotFoundException)1 ZKException (org.apache.distributedlog.exceptions.ZKException)1 DefaultZKOp (org.apache.distributedlog.zk.DefaultZKOp)1 ZKOp (org.apache.distributedlog.zk.ZKOp)1 ZKTransaction (org.apache.distributedlog.zk.ZKTransaction)1 ZKVersionedSetOp (org.apache.distributedlog.zk.ZKVersionedSetOp)1 KeeperException (org.apache.zookeeper.KeeperException)1 Op (org.apache.zookeeper.Op)1