Example usage for org.apache.zookeeper KeeperException code

List of usage examples for org.apache.zookeeper KeeperException code

Introduction

In this page you can find the example usage for org.apache.zookeeper KeeperException code.

Prototype

Code code

To view the source code for org.apache.zookeeper KeeperException code.

Click Source Link

Usage

From source file:org.apache.distributedlog.util.FutureUtils.java

License:Apache License

/**
 * Convert the <i>throwable</i> to zookeeper related exceptions.
 *
 * @param throwable cause/*from   w w  w .  j  a  v a 2 s .c  om*/
 * @param path zookeeper path
 * @return zookeeper related exceptions
 */
public static Throwable zkException(Throwable throwable, String path) {
    if (throwable instanceof KeeperException) {
        return new ZKException("Encountered zookeeper exception on " + path, (KeeperException) throwable);
    } else if (throwable instanceof ZooKeeperClient.ZooKeeperConnectionException) {
        return new ZKException("Encountered zookeeper connection loss on " + path,
                KeeperException.Code.CONNECTIONLOSS);
    } else if (throwable instanceof InterruptedException) {
        return new DLInterruptedException("Interrupted on operating " + path, throwable);
    } else {
        return new UnexpectedException("Encountered unexpected exception on operatiing " + path, throwable);
    }
}

From source file:org.apache.distributedlog.util.Utils.java

License:Apache License

/**
 * Asynchronously create zookeeper path recursively and optimistically.
 *
 * @param zkc Zookeeper client//from   ww  w .  ja  v  a2  s . c o m
 * @param pathToCreate  Zookeeper full path
 * @param data Zookeeper data
 * @param acl Acl of the zk path
 * @param createMode Create mode of zk path
 */
public static CompletableFuture<Void> zkAsyncCreateFullPathOptimisticAndSetData(final ZooKeeperClient zkc,
        final String pathToCreate, final byte[] data, final List<ACL> acl, final CreateMode createMode) {
    final CompletableFuture<Void> result = new CompletableFuture<Void>();

    try {
        zkc.get().setData(pathToCreate, data, -1, new AsyncCallback.StatCallback() {
            @Override
            public void processResult(int rc, String path, Object ctx, Stat stat) {
                if (rc != KeeperException.Code.NONODE.intValue()) {
                    handleKeeperExceptionCode(rc, path, result);
                    return;
                }

                Optional<String> parentPathShouldNotCreate = Optional.absent();
                zkAsyncCreateFullPathOptimisticRecursive(zkc, pathToCreate, parentPathShouldNotCreate, data,
                        acl, createMode, new AsyncCallback.StringCallback() {
                            @Override
                            public void processResult(int rc, String path, Object ctx, String name) {
                                handleKeeperExceptionCode(rc, path, result);
                            }
                        }, result);
            }
        }, result);
    } catch (Exception exc) {
        result.completeExceptionally(exc);
    }

    return result;
}

From source file:org.apache.distributedlog.util.Utils.java

License:Apache License

private static void handleKeeperExceptionCode(int rc, String pathOrMessage, CompletableFuture<Void> result) {
    if (KeeperException.Code.OK.intValue() == rc) {
        result.complete(null);//from  w w  w.  j  a  va  2 s  .  com
    } else if (DistributedLogConstants.ZK_CONNECTION_EXCEPTION_RESULT_CODE == rc) {
        result.completeExceptionally(new ZooKeeperClient.ZooKeeperConnectionException(pathOrMessage));
    } else if (DistributedLogConstants.DL_INTERRUPTED_EXCEPTION_RESULT_CODE == rc) {
        result.completeExceptionally(new DLInterruptedException(pathOrMessage));
    } else {
        result.completeExceptionally(KeeperException.create(KeeperException.Code.get(rc), pathOrMessage));
    }
}

From source file:org.apache.distributedlog.util.Utils.java

License:Apache License

/**
 * Retrieve data from zookeeper <code>path</code>.
 *
 * @param path//w ww  .j av a  2  s.  c  o  m
 *          zookeeper path to retrieve data
 * @param watch
 *          whether to watch the path
 * @return future representing the versioned value. null version or null value means path doesn't exist.
 */
public static CompletableFuture<Versioned<byte[]>> zkGetData(ZooKeeper zk, String path, boolean watch) {
    final CompletableFuture<Versioned<byte[]>> promise = new CompletableFuture<Versioned<byte[]>>();
    zk.getData(path, watch, new AsyncCallback.DataCallback() {
        @Override
        public void processResult(int rc, String path, Object ctx, byte[] data, Stat stat) {
            if (KeeperException.Code.OK.intValue() == rc) {
                if (null == stat) {
                    promise.complete(new Versioned<byte[]>(null, null));
                } else {
                    promise.complete(new Versioned<byte[]>(data, new LongVersion(stat.getVersion())));
                }
            } else if (KeeperException.Code.NONODE.intValue() == rc) {
                promise.complete(new Versioned<byte[]>(null, null));
            } else {
                promise.completeExceptionally(KeeperException.create(KeeperException.Code.get(rc)));
            }
        }
    }, null);
    return promise;
}

From source file:org.apache.distributedlog.util.Utils.java

License:Apache License

/**
 * Set <code>data</code> to zookeeper <code>path</code>.
 *
 * @param zk//w  w w.j a va2 s. c  o  m
 *          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 CompletableFuture<LongVersion> zkSetData(ZooKeeper zk, String path, byte[] data,
        LongVersion version) {
    final CompletableFuture<LongVersion> promise = new CompletableFuture<LongVersion>();
    zk.setData(path, data, (int) version.getLongVersion(), new AsyncCallback.StatCallback() {
        @Override
        public void processResult(int rc, String path, Object ctx, Stat stat) {
            if (KeeperException.Code.OK.intValue() == rc) {
                promise.complete(new LongVersion(stat.getVersion()));
                return;
            }
            promise.completeExceptionally(KeeperException.create(KeeperException.Code.get(rc)));
            return;
        }
    }, null);
    return promise;
}

From source file:org.apache.distributedlog.util.Utils.java

License:Apache License

/**
 * Delete the given <i>path</i> from zookeeper.
 *
 * @param zk/*w ww . jav  a2s. co m*/
 *          zookeeper client
 * @param path
 *          path to delete
 * @param version
 *          version used to set data
 * @return future representing the version after this operation.
 */
public static CompletableFuture<Void> zkDelete(ZooKeeper zk, String path, LongVersion version) {
    final CompletableFuture<Void> promise = new CompletableFuture<Void>();
    zk.delete(path, (int) version.getLongVersion(), new AsyncCallback.VoidCallback() {
        @Override
        public void processResult(int rc, String path, Object ctx) {
            if (KeeperException.Code.OK.intValue() == rc) {
                promise.complete(null);
                return;
            }
            promise.completeExceptionally(KeeperException.create(KeeperException.Code.get(rc)));
            return;
        }
    }, null);
    return promise;
}

From source file:org.apache.distributedlog.util.Utils.java

License:Apache License

/**
 * Delete the given <i>path</i> from zookeeper.
 *
 * @param zkc//w  w  w.  ja  va 2  s.co m
 *          zookeeper client
 * @param path
 *          path to delete
 * @param version
 *          version used to set data
 * @return future representing if the delete is successful. Return true if the node is deleted,
 * false if the node doesn't exist, otherwise future will throw exception
 *
 */
public static CompletableFuture<Boolean> zkDeleteIfNotExist(ZooKeeperClient zkc, String path,
        LongVersion version) {
    ZooKeeper zk;
    try {
        zk = zkc.get();
    } catch (ZooKeeperClient.ZooKeeperConnectionException e) {
        return FutureUtils.exception(zkException(e, path));
    } catch (InterruptedException e) {
        return FutureUtils.exception(zkException(e, path));
    }
    final CompletableFuture<Boolean> promise = new CompletableFuture<Boolean>();
    zk.delete(path, (int) version.getLongVersion(), new AsyncCallback.VoidCallback() {
        @Override
        public void processResult(int rc, String path, Object ctx) {
            if (KeeperException.Code.OK.intValue() == rc) {
                promise.complete(true);
            } else if (KeeperException.Code.NONODE.intValue() == rc) {
                promise.complete(false);
            } else {
                promise.completeExceptionally(KeeperException.create(KeeperException.Code.get(rc)));
            }
        }
    }, null);
    return promise;
}

From source file:org.apache.distributedlog.zk.ZKTransaction.java

License:Apache License

@Override
public void processResult(int rc, String path, Object ctx, List<OpResult> results) {
    if (KeeperException.Code.OK.intValue() == rc) { // transaction succeed
        for (int i = 0; i < ops.size(); i++) {
            ops.get(i).commitOpResult(results.get(i));
        }/*from  www  . j  ava  2  s .c  om*/
        FutureUtils.complete(result, null);
    } else {
        KeeperException ke = KeeperException.create(KeeperException.Code.get(rc));
        for (int i = 0; i < ops.size(); i++) {
            ops.get(i).abortOpResult(ke, null != results ? results.get(i) : null);
        }
        FutureUtils.completeExceptionally(result, ke);
    }
}

From source file:org.apache.distributedlog.zk.ZKVersionedSetOp.java

License:Apache License

@Override
protected void abortOpResult(Throwable t, @Nullable OpResult opResult) {
    Throwable cause;// w  w  w  .  j  a v a  2  s.c  o m
    if (null == opResult) {
        cause = t;
    } else {
        assert (opResult instanceof OpResult.ErrorResult);
        OpResult.ErrorResult errorResult = (OpResult.ErrorResult) opResult;
        if (KeeperException.Code.OK.intValue() == errorResult.getErr()) {
            cause = t;
        } else {
            cause = KeeperException.create(KeeperException.Code.get(errorResult.getErr()));
        }
    }
    if (null != listener) {
        listener.onAbort(cause);
    }
}