Search in sources :

Example 1 with Return

use of com.twitter.util.Return in project distributedlog by twitter.

the class BookKeeperClient method deleteLedger.

public Future<Void> deleteLedger(long lid, final boolean ignoreNonExistentLedger) {
    BookKeeper bk;
    try {
        bk = get();
    } catch (IOException ioe) {
        return Future.exception(ioe);
    }
    final Promise<Void> promise = new Promise<Void>();
    bk.asyncDeleteLedger(lid, new AsyncCallback.DeleteCallback() {

        @Override
        public void deleteComplete(int rc, Object ctx) {
            if (BKException.Code.OK == rc) {
                promise.updateIfEmpty(new Return<Void>(null));
            } else if (BKException.Code.NoSuchLedgerExistsException == rc) {
                if (ignoreNonExistentLedger) {
                    promise.updateIfEmpty(new Return<Void>(null));
                } else {
                    promise.updateIfEmpty(new Throw<Void>(BKException.create(rc)));
                }
            } else {
                promise.updateIfEmpty(new Throw<Void>(BKException.create(rc)));
            }
        }
    }, null);
    return promise;
}
Also used : Return(com.twitter.util.Return) AsyncCallback(org.apache.bookkeeper.client.AsyncCallback) BookKeeper(org.apache.bookkeeper.client.BookKeeper) IOException(java.io.IOException) Promise(com.twitter.util.Promise)

Example 2 with Return

use of com.twitter.util.Return in project distributedlog by twitter.

the class ZKSessionLock method asyncTryLock.

@Override
public Future<LockWaiter> asyncTryLock(final long timeout, final TimeUnit unit) {
    final Promise<String> result = new Promise<String>();
    final boolean wait = DistributedLogConstants.LOCK_IMMEDIATE != timeout;
    if (wait) {
        asyncTryLock(wait, result);
    } else {
        // try to check locks first
        zk.getChildren(lockPath, null, new AsyncCallback.Children2Callback() {

            @Override
            public void processResult(final int rc, String path, Object ctx, final List<String> children, Stat stat) {
                lockStateExecutor.submit(lockPath, new SafeRunnable() {

                    @Override
                    public void safeRun() {
                        if (!lockState.inState(State.INIT)) {
                            result.setException(new LockStateChangedException(lockPath, lockId, State.INIT, lockState.getState()));
                            return;
                        }
                        if (KeeperException.Code.OK.intValue() != rc) {
                            result.setException(KeeperException.create(KeeperException.Code.get(rc)));
                            return;
                        }
                        FailpointUtils.checkFailPointNoThrow(FailpointUtils.FailPointName.FP_LockTryAcquire);
                        Collections.sort(children, MEMBER_COMPARATOR);
                        if (children.size() > 0) {
                            asyncParseClientID(zk, lockPath, children.get(0)).addEventListener(new FutureEventListener<Pair<String, Long>>() {

                                @Override
                                public void onSuccess(Pair<String, Long> owner) {
                                    if (!checkOrClaimLockOwner(owner, result)) {
                                        acquireFuture.updateIfEmpty(new Return<Boolean>(false));
                                    }
                                }

                                @Override
                                public void onFailure(final Throwable cause) {
                                    lockStateExecutor.submit(lockPath, new SafeRunnable() {

                                        @Override
                                        public void safeRun() {
                                            result.setException(cause);
                                        }
                                    });
                                }
                            });
                        } else {
                            asyncTryLock(wait, result);
                        }
                    }
                });
            }
        }, null);
    }
    final Promise<Boolean> waiterAcquireFuture = new Promise<Boolean>(new com.twitter.util.Function<Throwable, BoxedUnit>() {

        @Override
        public BoxedUnit apply(Throwable t) {
            acquireFuture.raise(t);
            return BoxedUnit.UNIT;
        }
    });
    return result.map(new AbstractFunction1<String, LockWaiter>() {

        @Override
        public LockWaiter apply(final String currentOwner) {
            final Exception acquireException = new OwnershipAcquireFailedException(lockPath, currentOwner);
            FutureUtils.within(acquireFuture, timeout, unit, acquireException, lockStateExecutor, lockPath).addEventListener(new FutureEventListener<Boolean>() {

                @Override
                public void onSuccess(Boolean acquired) {
                    completeOrFail(acquireException);
                }

                @Override
                public void onFailure(final Throwable acquireCause) {
                    completeOrFail(acquireException);
                }

                private void completeOrFail(final Throwable acquireCause) {
                    if (isLockHeld()) {
                        waiterAcquireFuture.setValue(true);
                    } else {
                        asyncUnlock().addEventListener(new FutureEventListener<BoxedUnit>() {

                            @Override
                            public void onSuccess(BoxedUnit value) {
                                waiterAcquireFuture.setException(acquireCause);
                            }

                            @Override
                            public void onFailure(Throwable cause) {
                                waiterAcquireFuture.setException(acquireCause);
                            }
                        });
                    }
                }
            });
            ;
            return new LockWaiter(lockId.getLeft(), currentOwner, waiterAcquireFuture);
        }
    });
}
Also used : AsyncCallback(org.apache.zookeeper.AsyncCallback) Stat(org.apache.zookeeper.data.Stat) BoxedUnit(scala.runtime.BoxedUnit) Pair(org.apache.commons.lang3.tuple.Pair) OwnershipAcquireFailedException(com.twitter.distributedlog.exceptions.OwnershipAcquireFailedException) Return(com.twitter.util.Return) SafeRunnable(org.apache.bookkeeper.util.SafeRunnable) UnexpectedException(com.twitter.distributedlog.exceptions.UnexpectedException) ZKException(com.twitter.distributedlog.exceptions.ZKException) DLInterruptedException(com.twitter.distributedlog.exceptions.DLInterruptedException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) LockingException(com.twitter.distributedlog.exceptions.LockingException) KeeperException(org.apache.zookeeper.KeeperException) TimeoutException(com.twitter.util.TimeoutException) IOException(java.io.IOException) OwnershipAcquireFailedException(com.twitter.distributedlog.exceptions.OwnershipAcquireFailedException) Promise(com.twitter.util.Promise) FutureEventListener(com.twitter.util.FutureEventListener)

Example 3 with Return

use of com.twitter.util.Return in project distributedlog by twitter.

the class Utils method zkSetData.

/**
 * Set <code>data</code> to zookeeper <code>path</code>.
 *
 * @param zk
 *          zookeeper client
 * @param path
 *          path to set data
 * @param data
 *          data to set
 * @param version
 *          version used to set data
 * @return future representing the version after this operation.
 */
public static Future<ZkVersion> zkSetData(ZooKeeper zk, String path, byte[] data, ZkVersion version) {
    final Promise<ZkVersion> promise = new Promise<ZkVersion>();
    zk.setData(path, data, version.getZnodeVersion(), new AsyncCallback.StatCallback() {

        @Override
        public void processResult(int rc, String path, Object ctx, Stat stat) {
            if (KeeperException.Code.OK.intValue() == rc) {
                promise.updateIfEmpty(new Return<ZkVersion>(new ZkVersion(stat.getVersion())));
                return;
            }
            promise.updateIfEmpty(new Throw<ZkVersion>(KeeperException.create(KeeperException.Code.get(rc))));
            return;
        }
    }, null);
    return promise;
}
Also used : Promise(com.twitter.util.Promise) Stat(org.apache.zookeeper.data.Stat) Return(com.twitter.util.Return) Throw(com.twitter.util.Throw) AsyncCallback(org.apache.zookeeper.AsyncCallback) ZkVersion(org.apache.bookkeeper.meta.ZkVersion)

Example 4 with Return

use of com.twitter.util.Return in project distributedlog by twitter.

the class BookKeeperClient method createLedger.

// Util functions
public Future<LedgerHandle> createLedger(int ensembleSize, int writeQuorumSize, int ackQuorumSize) {
    BookKeeper bk;
    try {
        bk = get();
    } catch (IOException ioe) {
        return Future.exception(ioe);
    }
    final Promise<LedgerHandle> promise = new Promise<LedgerHandle>();
    bk.asyncCreateLedger(ensembleSize, writeQuorumSize, ackQuorumSize, BookKeeper.DigestType.CRC32, passwd, new AsyncCallback.CreateCallback() {

        @Override
        public void createComplete(int rc, LedgerHandle lh, Object ctx) {
            if (BKException.Code.OK == rc) {
                promise.updateIfEmpty(new Return<LedgerHandle>(lh));
            } else {
                promise.updateIfEmpty(new Throw<LedgerHandle>(BKException.create(rc)));
            }
        }
    }, null);
    return promise;
}
Also used : Promise(com.twitter.util.Promise) Return(com.twitter.util.Return) LedgerHandle(org.apache.bookkeeper.client.LedgerHandle) Throw(com.twitter.util.Throw) AsyncCallback(org.apache.bookkeeper.client.AsyncCallback) BookKeeper(org.apache.bookkeeper.client.BookKeeper) IOException(java.io.IOException)

Aggregations

Promise (com.twitter.util.Promise)4 Return (com.twitter.util.Return)4 IOException (java.io.IOException)3 Throw (com.twitter.util.Throw)2 AsyncCallback (org.apache.bookkeeper.client.AsyncCallback)2 BookKeeper (org.apache.bookkeeper.client.BookKeeper)2 AsyncCallback (org.apache.zookeeper.AsyncCallback)2 Stat (org.apache.zookeeper.data.Stat)2 DLInterruptedException (com.twitter.distributedlog.exceptions.DLInterruptedException)1 LockingException (com.twitter.distributedlog.exceptions.LockingException)1 OwnershipAcquireFailedException (com.twitter.distributedlog.exceptions.OwnershipAcquireFailedException)1 UnexpectedException (com.twitter.distributedlog.exceptions.UnexpectedException)1 ZKException (com.twitter.distributedlog.exceptions.ZKException)1 FutureEventListener (com.twitter.util.FutureEventListener)1 TimeoutException (com.twitter.util.TimeoutException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 LedgerHandle (org.apache.bookkeeper.client.LedgerHandle)1 ZkVersion (org.apache.bookkeeper.meta.ZkVersion)1 SafeRunnable (org.apache.bookkeeper.util.SafeRunnable)1 Pair (org.apache.commons.lang3.tuple.Pair)1