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:com.linkedin.parseq.zk.client.ZKClientImpl.java

License:Apache License

/**
 * {@inheritDoc}/*from  w w w .  j a  v  a2  s  .  com*/
 */
@Override
public WatchableTask<Optional<Stat>> exists(String path) {
    return new WatchableTask<Optional<Stat>>("zkExists: " + path) {
        @Override
        protected Promise run(Context context) throws Throwable {
            SettablePromise<Optional<Stat>> promise = Promises.settable();
            _zkClient.exists(path, _watcher, (int rc, String p, Object ctx, Stat stat) -> {
                KeeperException.Code code = KeeperException.Code.get(rc);
                switch (code) {
                case OK:
                    promise.done(Optional.of(stat));
                    break;
                case NONODE:
                    promise.done(Optional.empty());
                    break;
                default:
                    promise.fail(KeeperException.create(code, p));
                }
            }, null);
            return promise;
        }
    };
}

From source file:com.linkedin.parseq.zk.client.ZKClientImpl.java

License:Apache License

/**
 * {@inheritDoc}/*from w  w  w . j  av a 2 s .com*/
 */
@Override
public Task<Void> delete(String path, int version) {
    return Task.async("zkDelete: " + path, () -> {
        SettablePromise<Void> promise = Promises.settable();
        _zkClient.delete(path, version, (int rc, String p, Object ctx) -> {
            KeeperException.Code code = KeeperException.Code.get(rc);
            switch (code) {
            case OK:
                promise.done(null);
                break;
            default:
                promise.fail(KeeperException.create(code, p));
            }
        }, null);
        return promise;
    });
}

From source file:com.linkedin.parseq.zk.client.ZKUtil.java

License:Apache License

/**
 * Decides whether the given {@link Throwable throwable} is recoverable or not.
 * Recoverable exceptions are defined as such:
 * http://wiki.apache.org/hadoop/ZooKeeper/ErrorHandling
 *
 * @param t the {@link Throwable}//from  w  w w . ja  v  a 2 s  .c  o  m
 * @return  {@code true} if {@code t} is recoverable and {@code false} otherwise.
 */
public static boolean isRecoverable(Throwable t) {
    if (t instanceof KeeperException) {
        KeeperException ex = (KeeperException) t;
        if (ex.code() == KeeperException.Code.CONNECTIONLOSS
                || ex.code() == KeeperException.Code.OPERATIONTIMEOUT) {
            return true;
        }
    }
    return false;
}

From source file:com.martin.zkedit.utils.ZooKeeperUtil.java

License:Open Source License

private void createIfDoesntExist(String path, byte[] data, boolean force, ZooKeeper zooKeeper)
        throws InterruptedException, KeeperException {
    try {//from  w ww  . j a v  a2  s.  c  o m
        zooKeeper.create(path, data, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    } catch (KeeperException ke) {
        //Explicit Overwrite
        if (KeeperException.Code.NODEEXISTS.equals(ke.code())) {
            if (force) {
                zooKeeper.delete(path, -1);
                zooKeeper.create(path, data, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
            }
        } else {
            throw ke;
        }
    }
}

From source file:com.navercorp.client.Client.java

License:Apache License

private static Result keeperExceptionResult(KeeperException e) {
    switch (e.code()) {
    case NONODE:/*from w w w.j a  v  a2 s. co m*/
        return new Result("", e.getMessage(), ZK_NO_NODE);
    case NODEEXISTS:
        return new Result("", e.getMessage(), ZK_NODE_EXISTS);
    case NOTEMPTY:
        return new Result("", e.getMessage(), ZK_NOT_EMPTY_NODE);
    case NOCHILDRENFOREPHEMERALS:
        return new Result("", e.getMessage(), ZK_NO_CHILDREN_FOR_EPHEMERALS);
    case BADVERSION:
        return new Result("", e.getMessage(), ZK_BAD_NODE_VERSION);
    default:
        return new Result("", e.getMessage(), ZK_UNEXPECTED_ERROR);
    }
}

From source file:com.navercorp.pinpoint.collector.cluster.zookeeper.DefaultZookeeperClient.java

License:Apache License

@Override
public void createPath(String path, boolean createEndNode)
        throws PinpointZookeeperException, InterruptedException {
    checkState();//from  www .  j a  v a2  s  .c  om

    int pos = 1;
    do {
        pos = path.indexOf('/', pos + 1);

        if (pos == -1) {
            pos = path.length();
        }

        ZooKeeper zookeeper = this.zookeeper;
        try {
            if (pos == path.length()) {
                if (!createEndNode) {
                    return;
                }
            }

            String subPath = path.substring(0, pos);
            if (zookeeper.exists(subPath, false) != null) {
                continue;
            }

            zookeeper.create(subPath, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        } catch (KeeperException exception) {
            if (exception.code() != Code.NODEEXISTS) {
                handleException(exception);
            }
        }

    } while (pos < path.length());
}

From source file:com.navercorp.pinpoint.collector.cluster.zookeeper.DefaultZookeeperClient.java

License:Apache License

@Override
public void delete(String path) throws PinpointZookeeperException, InterruptedException {
    checkState();//  w  w w.j av a 2  s .  c o  m

    ZooKeeper zookeeper = this.zookeeper;
    try {
        zookeeper.delete(path, -1);
    } catch (KeeperException exception) {
        if (exception.code() != Code.NONODE) {
            handleException(exception);
        }
    }
}

From source file:com.navercorp.pinpoint.collector.cluster.zookeeper.DefaultZookeeperClient.java

License:Apache License

@Override
public boolean exists(String path) throws PinpointZookeeperException, InterruptedException {
    checkState();/*from w w w. ja v a 2s  .c o m*/

    ZooKeeper zookeeper = this.zookeeper;
    try {
        Stat stat = zookeeper.exists(path, false);
        if (stat == null) {
            return false;
        }
    } catch (KeeperException exception) {
        if (exception.code() != Code.NODEEXISTS) {
            handleException(exception);
        }
    }
    return true;
}

From source file:com.navercorp.pinpoint.collector.cluster.zookeeper.DefaultZookeeperClient.java

License:Apache License

@Override
public List<String> getChildrenNode(String path, boolean watch)
        throws PinpointZookeeperException, InterruptedException {
    checkState();/*from  w w w .j  a  v a  2  s .  c om*/

    try {
        List<String> childNodeList = zookeeper.getChildren(path, watch, null);

        logger.info("ChildNode List = {}", childNodeList);
        return childNodeList;
    } catch (KeeperException exception) {
        if (exception.code() != Code.NONODE) {
            handleException(exception);
        }
    }

    return Collections.emptyList();
}

From source file:com.navercorp.pinpoint.collector.cluster.zookeeper.ZookeeperClient.java

License:Apache License

public void createPath(String path, boolean createEndNode)
        throws PinpointZookeeperException, InterruptedException {
    checkState();/*from   w ww  .j  a  v a  2  s  .c om*/

    int pos = 1;
    do {
        pos = path.indexOf('/', pos + 1);

        if (pos == -1) {
            pos = path.length();
        }

        ZooKeeper zookeeper = this.zookeeper;
        try {
            if (pos == path.length()) {
                if (!createEndNode) {
                    return;
                }
            }

            String subPath = path.substring(0, pos);
            if (zookeeper.exists(subPath, false) != null) {
                continue;
            }

            zookeeper.create(subPath, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        } catch (KeeperException exception) {
            if (exception.code() != Code.NODEEXISTS) {
                handleException(exception);
            }
        }

    } while (pos < path.length());
}