Search in sources :

Example 1 with Throw

use of com.twitter.util.Throw 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 2 with Throw

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

the class ZKSessionLock method asyncUnlock.

Future<BoxedUnit> asyncUnlock(final Throwable cause) {
    final Promise<BoxedUnit> promise = new Promise<BoxedUnit>();
    // Use lock executor here rather than lock action, because we want this opertaion to be applied
    // whether the epoch has changed or not. The member node is EPHEMERAL_SEQUENTIAL so there's no
    // risk of an ABA problem where we delete and recreate a node and then delete it again here.
    lockStateExecutor.submit(lockPath, new SafeRunnable() {

        @Override
        public void safeRun() {
            acquireFuture.updateIfEmpty(new Throw<Boolean>(cause));
            unlockInternal(promise);
            promise.addEventListener(new OpStatsListener<BoxedUnit>(unlockStats));
        }
    });
    return promise;
}
Also used : Promise(com.twitter.util.Promise) Throw(com.twitter.util.Throw) SafeRunnable(org.apache.bookkeeper.util.SafeRunnable) BoxedUnit(scala.runtime.BoxedUnit) OpStatsListener(com.twitter.distributedlog.stats.OpStatsListener)

Example 3 with Throw

use of com.twitter.util.Throw 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)3 Throw (com.twitter.util.Throw)3 Return (com.twitter.util.Return)2 OpStatsListener (com.twitter.distributedlog.stats.OpStatsListener)1 IOException (java.io.IOException)1 AsyncCallback (org.apache.bookkeeper.client.AsyncCallback)1 BookKeeper (org.apache.bookkeeper.client.BookKeeper)1 LedgerHandle (org.apache.bookkeeper.client.LedgerHandle)1 ZkVersion (org.apache.bookkeeper.meta.ZkVersion)1 SafeRunnable (org.apache.bookkeeper.util.SafeRunnable)1 AsyncCallback (org.apache.zookeeper.AsyncCallback)1 Stat (org.apache.zookeeper.data.Stat)1 BoxedUnit (scala.runtime.BoxedUnit)1