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:at.salzburgresearch.kmt.zkconfig.ZookeeperConfiguration.java

License:Apache License

private boolean handleException(KeeperException e) {
    switch (e.code()) {
    case CONNECTIONLOSS:
    case SESSIONEXPIRED:
        this.sync.raiseBarrier();
        return true;
    }//from   w  w  w  .jav  a 2 s  . c o  m
    return false;
}

From source file:at.salzburgresearch.kmt.zkconfig.ZookeeperConfiguration.java

License:Apache License

@Override
public Object getProperty(String key) {
    try {//ww w . j  a  va 2  s .c  o  m
        return cache.get(toZookeeperPath(key));
    } catch (ExecutionException e) {
        if (e.getCause() instanceof KeeperException
                && ((KeeperException) e.getCause()).code() == KeeperException.Code.NONODE) {
            return null;
        }
        log.error("Could not load property {}: {}", key, e);
    }
    return null;
}

From source file:co.cask.tephra.zookeeper.TephraZKClientService.java

License:Apache License

private OperationFuture<String> doCreate(final String path, @Nullable final byte[] data,
        final CreateMode createMode, final boolean createParent, final List<ACL> acl,
        final boolean ignoreNodeExists) {
    final SettableOperationFuture<String> createFuture = SettableOperationFuture.create(path, eventExecutor);
    getZooKeeper().create(path, data, acl, createMode, Callbacks.STRING, createFuture);
    if (!createParent) {
        return createFuture;
    }/*from www. jav  a  2s.  c  o  m*/

    // If create parent is request, return a different future
    final SettableOperationFuture<String> result = SettableOperationFuture.create(path, eventExecutor);
    // Watch for changes in the original future
    Futures.addCallback(createFuture, new FutureCallback<String>() {
        @Override
        public void onSuccess(String path) {
            // Propagate if creation was successful
            result.set(path);
        }

        @Override
        public void onFailure(Throwable t) {
            // See if the failure can be handled
            if (updateFailureResult(t, result, path, ignoreNodeExists)) {
                return;
            }
            // Create the parent node
            String parentPath = getParent(path);
            if (parentPath.isEmpty()) {
                result.setException(t);
                return;
            }
            // Watch for parent creation complete. Parent is created with the unsafe ACL.
            Futures.addCallback(
                    doCreate(parentPath, null, CreateMode.PERSISTENT, true, ZooDefs.Ids.OPEN_ACL_UNSAFE, true),
                    new FutureCallback<String>() {
                        @Override
                        public void onSuccess(String parentPath) {
                            // Create the requested path again
                            Futures.addCallback(doCreate(path, data, createMode, false, acl, ignoreNodeExists),
                                    new FutureCallback<String>() {
                                        @Override
                                        public void onSuccess(String pathResult) {
                                            result.set(pathResult);
                                        }

                                        @Override
                                        public void onFailure(Throwable t) {
                                            // handle the failure
                                            updateFailureResult(t, result, path, ignoreNodeExists);
                                        }
                                    });
                        }

                        @Override
                        public void onFailure(Throwable t) {
                            result.setException(t);
                        }
                    });
        }

        /**
         * Updates the result future based on the given {@link Throwable}.
         * @param t Cause of the failure
         * @param result Future to be updated
         * @param path Request path for the operation
         * @return {@code true} if it is a failure, {@code false} otherwise.
         */
        private boolean updateFailureResult(Throwable t, SettableOperationFuture<String> result, String path,
                boolean ignoreNodeExists) {
            // Propagate if there is error
            if (!(t instanceof KeeperException)) {
                result.setException(t);
                return true;
            }
            KeeperException.Code code = ((KeeperException) t).code();
            // Node already exists, simply return success if it allows for ignoring node exists (for parent node creation).
            if (ignoreNodeExists && code == KeeperException.Code.NODEEXISTS) {
                // The requested path could be used because it only applies to non-sequential node
                result.set(path);
                return false;
            }
            if (code != KeeperException.Code.NONODE) {
                result.setException(t);
                return true;
            }
            return false;
        }

        /**
         * Gets the parent of the given path.
         * @param path Path for computing its parent
         * @return Parent of the given path, or empty string if the given path is the root path already.
         */
        private String getParent(String path) {
            String parentPath = path.substring(0, path.lastIndexOf('/'));
            return (parentPath.isEmpty() && !"/".equals(path)) ? "/" : parentPath;
        }
    });

    return result;
}

From source file:com.alibaba.wasp.master.OfflineCallback.java

License:Apache License

@Override
public void processResult(int rc, String path, Object ctx, String name) {
    if (rc == KeeperException.Code.NODEEXISTS.intValue()) {
        LOG.warn("Node for " + path + " already exists");
    } else if (rc != 0) {
        // This is result code. If non-zero, need to resubmit.
        LOG.warn("rc != 0 for " + path + " -- retryable connectionloss -- "
                + "FIX see http://wiki.apache.org/hadoop/ZooKeeper/FAQ#A2");
        this.counter.addAndGet(1);
        return;//from  www. j av  a  2  s. c  o  m
    }

    if (LOG.isDebugEnabled()) {
        LOG.debug("rs=" + ctx + ", server=" + destination);
    }
    // Async exists to set a watcher so we'll get triggered when
    // unassigned node changes.
    ZooKeeper zk = this.zkw.getRecoverableZooKeeper().getZooKeeper();
    zk.exists(path, this.zkw, callBack, ctx);
}

From source file:com.alibaba.wasp.zookeeper.RecoverableZooKeeper.java

License:Apache License

/**
 * delete is an idempotent operation. Retry before throwing exception. This
 * function will not throw NoNodeException if the path does not exist.
 *//*from  www .ja va2  s .  c  om*/
public void delete(String path, int version) throws InterruptedException, KeeperException {
    RetryCounter retryCounter = retryCounterFactory.create();
    boolean isRetry = false; // False for first attempt, true for all retries.
    while (true) {
        try {
            zk.delete(path, version);
            return;
        } catch (KeeperException e) {
            switch (e.code()) {
            case NONODE:
                if (isRetry) {
                    LOG.info("Node " + path + " already deleted. Assuming a " + "previous attempt succeeded.");
                    return;
                }
                LOG.warn("Node " + path + " already deleted, retry=" + isRetry);
                throw e;

            case CONNECTIONLOSS:
            case SESSIONEXPIRED:
            case OPERATIONTIMEOUT:
                retryOrThrow(retryCounter, e, "delete");
                break;

            default:
                throw e;
            }
        }
        retryCounter.sleepUntilNextRetry();
        retryCounter.useRetry();
        isRetry = true;
    }
}

From source file:com.alibaba.wasp.zookeeper.RecoverableZooKeeper.java

License:Apache License

/**
 * exists is an idempotent operation. Retry before throwing exception
 * @return A Stat instance//from  w  w  w .j  av a 2 s  .c  om
 */
public Stat exists(String path, Watcher watcher) throws KeeperException, InterruptedException {
    RetryCounter retryCounter = retryCounterFactory.create();
    while (true) {
        try {
            return zk.exists(path, watcher);
        } catch (KeeperException e) {
            switch (e.code()) {
            case CONNECTIONLOSS:
            case SESSIONEXPIRED:
            case OPERATIONTIMEOUT:
                retryOrThrow(retryCounter, e, "exists");
                break;

            default:
                throw e;
            }
        }
        retryCounter.sleepUntilNextRetry();
        retryCounter.useRetry();
    }
}

From source file:com.alibaba.wasp.zookeeper.RecoverableZooKeeper.java

License:Apache License

/**
 * exists is an idempotent operation. Retry before throwing exception
 * @return A Stat instance//from w w w  .  j a va 2 s. c o m
 */
public Stat exists(String path, boolean watch) throws KeeperException, InterruptedException {
    RetryCounter retryCounter = retryCounterFactory.create();
    while (true) {
        try {
            return zk.exists(path, watch);
        } catch (KeeperException e) {
            switch (e.code()) {
            case CONNECTIONLOSS:
            case SESSIONEXPIRED:
            case OPERATIONTIMEOUT:
                retryOrThrow(retryCounter, e, "exists");
                break;

            default:
                throw e;
            }
        }
        retryCounter.sleepUntilNextRetry();
        retryCounter.useRetry();
    }
}

From source file:com.alibaba.wasp.zookeeper.RecoverableZooKeeper.java

License:Apache License

/**
 * getChildren is an idempotent operation. Retry before throwing exception
 * @return List of children znodes/*  w w w. ja v  a 2s.  c om*/
 */
public List<String> getChildren(String path, Watcher watcher) throws KeeperException, InterruptedException {
    RetryCounter retryCounter = retryCounterFactory.create();
    while (true) {
        try {
            return zk.getChildren(path, watcher);
        } catch (KeeperException e) {
            switch (e.code()) {
            case CONNECTIONLOSS:
            case SESSIONEXPIRED:
            case OPERATIONTIMEOUT:
                retryOrThrow(retryCounter, e, "getChildren");
                break;

            default:
                throw e;
            }
        }
        retryCounter.sleepUntilNextRetry();
        retryCounter.useRetry();
    }
}

From source file:com.alibaba.wasp.zookeeper.RecoverableZooKeeper.java

License:Apache License

/**
 * getChildren is an idempotent operation. Retry before throwing exception
 * @return List of children znodes/*from   w ww .  ja v a2 s.co  m*/
 */
public List<String> getChildren(String path, boolean watch) throws KeeperException, InterruptedException {
    RetryCounter retryCounter = retryCounterFactory.create();
    while (true) {
        try {
            return zk.getChildren(path, watch);
        } catch (KeeperException e) {
            switch (e.code()) {
            case CONNECTIONLOSS:
            case SESSIONEXPIRED:
            case OPERATIONTIMEOUT:
                retryOrThrow(retryCounter, e, "getChildren");
                break;

            default:
                throw e;
            }
        }
        retryCounter.sleepUntilNextRetry();
        retryCounter.useRetry();
    }
}

From source file:com.alibaba.wasp.zookeeper.RecoverableZooKeeper.java

License:Apache License

/**
 * getData is an idempotent operation. Retry before throwing exception
 * @return Data// w ww  . j  a  va  2  s.  c  om
 */
public byte[] getData(String path, Watcher watcher, Stat stat) throws KeeperException, InterruptedException {
    RetryCounter retryCounter = retryCounterFactory.create();
    while (true) {
        try {
            byte[] revData = zk.getData(path, watcher, stat);
            return this.removeMetaData(revData);
        } catch (KeeperException e) {
            switch (e.code()) {
            case CONNECTIONLOSS:
            case SESSIONEXPIRED:
            case OPERATIONTIMEOUT:
                retryOrThrow(retryCounter, e, "getData");
                break;

            default:
                throw e;
            }
        }
        retryCounter.sleepUntilNextRetry();
        retryCounter.useRetry();
    }
}