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:io.reign.zk.ResilientZkClient.java

License:Apache License

/**
 * /*from   w  ww  .j a  v a2  s .co m*/
 * @param errorCode
 * @return true if exception can be classified as a ZK session error
 */
private boolean isZooKeeperSessionError(KeeperException.Code errorCode) {
    return (errorCode == KeeperException.Code.CONNECTIONLOSS
            || errorCode == KeeperException.Code.RUNTIMEINCONSISTENCY
            || errorCode == KeeperException.Code.SESSIONEXPIRED || errorCode == KeeperException.Code.SYSTEMERROR
            || errorCode == KeeperException.Code.SESSIONMOVED);
}

From source file:io.s4.ft.BookKeeperStateStorage.java

License:Open Source License

/**
 * ZooKeeper data callback./* w  w w  .ja  v a 2 s.  co  m*/
 * 
 * @param rc
 * @param path
 * @param ctx
 * @param data
 * @param stat
 */
public void processResult(int rc, String path, Object ctx, byte data[], Stat stat) {
    FetchCtx fctx = (FetchCtx) ctx;

    if (KeeperException.Code.get(rc) == KeeperException.Code.OK) {
        /*
         * Open ledger for reading.
         */
        ByteBuffer ledgerIdBB = ByteBuffer.wrap(data);
        bk.asyncOpenLedger(ledgerIdBB.getLong(), DigestType.CRC32, "flaviowashere".getBytes(),
                (AsyncCallback.OpenCallback) this, (Object) fctx);
    } else {
        logger.error("Failed to open ledger for reading: " + rc);
    }
}

From source file:io.s4.ft.BookKeeperStateStorage.java

License:Open Source License

/**
 * ZooKeeper get children callback./*from  w  ww . j  a  va2s .  c  o  m*/
 * 
 * @param rc
 * @param path
 * @param ctx
 * @param children
 */
public void processResult(int rc, String path, Object ctx, List<String> children) {
    FetchKeysCtx fkCtx = (FetchKeysCtx) ctx;

    if (KeeperException.Code.get(rc) == KeeperException.Code.OK) {
        /*
         * Add keys to set.
         */
        Set<SafeKeeperId> set = new HashSet<SafeKeeperId>();
        for (String s : children) {
            fkCtx.keys.add(new SafeKeeperId(s));
        }
        fkCtx.keys = set;
    } else {
        logger.error("Failed to get keys from ZooKeeper: " + rc);
    }

    if (fkCtx.sb != null) {
        fkCtx.sb.cross();
    }
}

From source file:io.tilt.minka.spectator.NodeCacheable.java

License:Apache License

private Collection<CuratorTransactionResult> createZNode(final CreateMode mode, final String path,
        final byte[] bytes) throws Exception {
    try {//from ww w  .j a v a  2  s  . co  m
        logger.info("{}: ({}) Changing path: {}", getClass().getSimpleName(), getLogId(), path);
        return getClient().inTransaction().setData().forPath(path, bytes).and().commit();
    } catch (KeeperException ke) {
        if (ke.code() == KeeperException.Code.NONODE) {
            logger.info("{}: ({}) Creating parent path for first time: {} because: {}",
                    getClass().getSimpleName(), getLogId(), path, ke.getMessage());
            return getClient().inTransaction().create().withMode(mode).forPath(path, bytes).and().commit();
        } else {
            logger.error("{}: ({}) Unexpected creating node: {}", getClass().getSimpleName(), getLogId(), path);
            throw ke;
        }
    }
}

From source file:io.vertx.config.zookeeper.ZookeeperConfigStore.java

License:Apache License

private void retrieve(CuratorEvent event, Handler<AsyncResult<Buffer>> completionHandler) {
    KeeperException.Code code = KeeperException.Code.get(event.getResultCode());
    if (code == KeeperException.Code.OK) {
        completionHandler.handle(Future.succeededFuture(Buffer.buffer(event.getData())));
    } else if (code == KeeperException.Code.NONODE) {
        completionHandler.handle(Future.succeededFuture(Buffer.buffer("{}")));
    } else {/*w w w .  ja  v a  2  s  .  com*/
        completionHandler.handle(Future.failedFuture(KeeperException.create(code, path)));
    }
}

From source file:io.vertx.config.zookeeper.ZookeeperConfigStoreTest.java

License:Apache License

private void dataWrittenCallback(Handler<AsyncResult<Void>> handler, Context context, CuratorEvent event) {
    context.runOnContext((x) -> {// w  ww .j  ava2  s .c o m
        if (event.getResultCode() == 0) {
            handler.handle(Future.succeededFuture());
        } else {
            handler.handle(Future
                    .failedFuture(KeeperException.create(KeeperException.Code.get(event.getResultCode()))));
        }
    });
}

From source file:jp.queuelinker.system.net.ZookeeperConnection.java

License:Apache License

/**
 * @param path/*from  w ww  .  j a  va2s  .  c o  m*/
 * @return
 * @throws InterruptedException
 * @throws KeeperException
 */
public int getAndIncrementAtomicInt(final String path) throws InterruptedException, KeeperException {
    Stat stat = new Stat();
    while (true) {
        byte[] data;
        try {
            data = super.getData(path, false, stat);
        } catch (KeeperException e) {
            throw e;
        }
        assert (data.length == 4);
        final int oldValue = ByteConverter.getIntValue(data);
        data = ByteConverter.getByteArrayOfInt(oldValue + 1);
        try {
            super.setData(path, data, stat.getVersion());
        } catch (KeeperException e) {
            if (e.code() == Code.BADVERSION) {
                continue;
            } else {
                throw e;
            }
        }
        return oldValue;
    }
}

From source file:net.sf.katta.zk.ZKClient.java

License:Apache License

/**
 * Deletes a given path. For recursive deletes use
 * {@link #deleteRecursive(String)}.//  w  ww  .java2s. c  o  m
 * 
 * @param path
 * @return
 * @throws KattaException
 */
public boolean delete(final String path) throws KattaException {
    ensureZkRunning();
    try {
        if (_zk != null) {
            _zk.delete(path, -1);
        }
        return true;
    } catch (final KeeperException e) {
        String message = "unable to delete path: " + path;
        if (KeeperException.Code.NOTEMPTY == e.code()) {
            message += " Path has sub nodes.";
        }
        throw new KattaException(message, e);
    } catch (final Exception e) {
        throw new KattaException("unable to delete path:" + path, e);
    }
}

From source file:net.sf.katta.zk.ZKClient.java

License:Apache License

/**
 * Deletes a path and all children recursively.
 * /*w w w.  j a  v  a  2  s .  c o m*/
 * @param path
 *          The path to delete.
 * @return true if successful.
 * @throws KattaException
 */
public boolean deleteRecursive(final String path) throws KattaException {
    ensureZkRunning();
    try {
        final List<String> children = _zk.getChildren(path, false);
        for (final String child : children) {
            String subPath = path + (path.endsWith("/") ? "" : _conf.getSeparator()) + child;
            if (!deleteRecursive(subPath)) {
                return false;
            }
        }
    } catch (final KeeperException e) {
        // do nothing since we simply do not have children here.
    } catch (final InterruptedException e) {
        throw new KattaException("retrieving children was interruppted", e);
    }

    if (path.equals("/") || path.equals("/zookeeper") || path.startsWith("/zookeeper/")) {
        // Special case when root path = /. Can't delete these.
        return true;
    }
    try {
        _zk.delete(path, -1);
    } catch (final KeeperException e) {
        if (e.code() != KeeperException.Code.NONODE) {
            throw new KattaException("unable to delete:" + path, e);
        }
    } catch (final Exception e) {
        throw new KattaException("unable to delete:" + path, e);
    }
    return true;
}

From source file:org.apache.accumulo.core.zookeeper.ZooUtil.java

License:Apache License

/**
 * This method will delete a node and all its children from zookeeper
 * /*from   ww w. java  2s  .c  o m*/
 * @param zPath
 *          the path to delete
 */
public static void recursiveDelete(ZooKeeper zk, String zPath, int version, NodeMissingPolicy policy)
        throws KeeperException, InterruptedException {
    if (policy.equals(NodeMissingPolicy.CREATE))
        throw new IllegalArgumentException(policy.name() + " is invalid for this operation");
    try {
        for (String child : zk.getChildren(zPath, false))
            recursiveDelete(zk, zPath + "/" + child, NodeMissingPolicy.SKIP);

        Stat stat;
        if ((stat = zk.exists(zPath, null)) != null)
            zk.delete(zPath, stat.getVersion());
    } catch (KeeperException e) {
        if (policy.equals(NodeMissingPolicy.SKIP) && e.code().equals(KeeperException.Code.NONODE))
            return;
        throw e;
    }
}