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.presence.PresenceService.java

License:Apache License

/**
 * Used to flag that a service node is dead, the presence node should be removed, and should not be checked again.
 * /*from   www  .  j av a  2  s  .  c o  m*/
 * Used internally to remove connected clients once ping(s) fail.
 * 
 * @param clusterId
 * @param serviceId
 * @param nodeId
 */
public void dead(String clusterId, String serviceId, String nodeId) {
    String nodePath = getPathScheme().joinTokens(clusterId, serviceId, nodeId);
    String path = getPathScheme().getAbsolutePath(PathType.PRESENCE, nodePath);
    try {
        getZkClient().delete(path, -1);
        announcementMap.remove(nodePath);

        getContext().getObserverManager().removeAllByOwnerId(nodeId);
    } catch (KeeperException e) {
        if (e.code() == Code.NONODE) {
            logger.debug("Node does not exist:  path={}", path);
        } else {
            logger.warn("Error trying to remove node:  " + e + ":  path=" + path, e);
        }
    } catch (InterruptedException e) {
        logger.warn("hide():  error trying to remove node:  " + e, e);
    }

}

From source file:io.reign.presence.PresenceService.java

License:Apache License

void doHide(String path, Announcement announcement) {
    long currentTimestamp = System.currentTimeMillis();
    try {// w ww  . j  a v  a2 s. c  o m

        // delete presence path
        getZkClient().delete(path, -1);

        // set last updated with some randomizer to spread out
        // requests
        announcement.setLastUpdated(currentTimestamp + (int) ((Math.random() * 5000f)));
    } catch (KeeperException e) {
        if (e.code() == Code.NONODE) {
            logger.debug("Node does not exist:  path={}", path);
        } else {
            logger.warn("Error trying to remove node:  " + e + ":  path=" + path, e);
        }
    } catch (InterruptedException e) {
        logger.warn("hide():  error trying to remove node:  " + e, e);
    }
}

From source file:io.reign.util.ZkClientUtil.java

License:Apache License

/**
 * /* www. ja  va 2s.co  m*/
 * @param zkClient
 * @param pathScheme
 * @param path
 * @param leafData
 * @param aclList
 * @param createMode
 * @param leafDataVersion
 * @param statRef
 *            to return Stat on update of data nodes
 * @return
 * @throws KeeperException
 */
public String updatePath(final ZkClient zkClient, final PathScheme pathScheme, final String path,
        final byte[] leafData, final List<ACL> aclList, final CreateMode createMode, int leafDataVersion,
        AtomicReference<Stat> statRef) throws KeeperException {

    /***** if there is leaf data, try updating first to save on ZK ops *****/
    if (!createMode.isSequential()) {
        try {
            if (statRef != null) {
                statRef.set(zkClient.setData(path, leafData, leafDataVersion));
            } else {
                zkClient.setData(path, leafData, leafDataVersion);
            }
            return path;
        } catch (KeeperException e) {
            if (e.code() == KeeperException.Code.NONODE) {
                if (logger.isDebugEnabled()) {
                    logger.debug(
                            "updatePath():  path does not exist for data update:  " + e + ":  path=" + path);
                }
            } else {
                logger.error("updatePath():  " + e, e);
                throw e;
            }
        } catch (InterruptedException e) {
            logger.warn("Interrupted in updatePath():  " + e, e);

        } // try/catch
    } // if

    /***** try to build path without building parents to save on calls to ZK *****/
    try {

        String pathCreated = zkClient.create(path, leafData, aclList, createMode);

        if (logger.isDebugEnabled()) {
            logger.debug("Created path directly:  pathCreated={}", pathCreated);
        }

        return pathCreated;
    } catch (KeeperException e) {
        if (e.code() == KeeperException.Code.NODEEXISTS) {
            if (logger.isDebugEnabled()) {
                logger.debug("Path already exists:  " + e + ": path=" + path);
            }

            return path;
        } else if (e.code() == KeeperException.Code.NONODE) {
            if (logger.isDebugEnabled()) {
                logger.debug("Parent path does not exist:  " + e + ":  path=" + path);
            }
        } else {
            logger.error("Error while building path:  " + e + "; path=" + path, e);
            throw e;
        } // if

    } catch (InterruptedException e) {
        logger.error(e + "", e);
        logger.warn("Interrupted in updatePath():  " + e, e);
    } // try/catch

    /***** build path by building parent nodes first *****/
    String[] tokens = pathScheme.tokenizePath(path);

    String pathCreated = "";
    String pathToCreate = null;
    for (int i = 0; i < tokens.length; i++) {
        String token = tokens[i];
        if ("".equals(token)) {
            // we should never get here, and if we are getting here, log as
            // error
            logger.warn("updatePath():  token is empty string!:  path='{}'; i={}; pathCreated='{}'",
                    new Object[] { path, i, pathCreated });

        } else {
            try {
                pathToCreate = pathCreated + "/" + token;

                if (logger.isDebugEnabled()) {
                    logger.debug("Creating node:  pathToCreate={}; token={}", pathToCreate, token);
                }

                // default to persistent mode until leaf node, then we use
                // the preferred create mode of caller
                CreateMode currentCreateMode = CreateMode.PERSISTENT;
                byte[] nodeData = null;
                if (i == tokens.length - 1) {
                    currentCreateMode = createMode;
                    nodeData = leafData;
                }

                pathCreated = zkClient.create(pathToCreate, nodeData, aclList, currentCreateMode);

            } catch (KeeperException e) {
                if (e.code() == KeeperException.Code.NODEEXISTS) {
                    if (logger.isDebugEnabled()) {
                        logger.debug("Path already exists:  path={}", pathToCreate);
                    }

                    pathCreated = pathToCreate;
                } else {
                    logger.error("Error while building path:  " + e + ":  path=" + pathToCreate, e);
                    throw e;
                } // if

            } catch (InterruptedException e) {
                logger.error(e + "", e);
                logger.warn("Interrupted in updatePath():  " + e, e);
            } // try/catch
        } // if
    } // for

    if (logger.isDebugEnabled()) {
        logger.debug("Created path by building parent nodes:  pathCreated={}", pathCreated);
    }
    return pathCreated;

}

From source file:io.reign.zk.ResilientZkClient.java

License:Apache License

/**
 * Re-establish any existing ZooKeeper watchers after reconnection.
 */// ww  w. j a v a 2  s .c o m
void restoreWatches() {
    for (String path : dataWatchesMap.keySet()) {
        logger.info("Restoring data watch(s):  path={}", path);
        for (Watcher watcher : dataWatchesMap.get(path)) {
            try {
                this.exists(path, watcher);
            } catch (KeeperException e) {
                if (e.code() != Code.NONODE) {
                    logger.warn("Error while restoring watch:  " + e + ":  path=" + path, e);
                }
            } catch (Exception e) {
                logger.warn("Error while restoring watch:  " + e + ":  path=" + path, e);
            } // try
        } // for
    } // for

    for (String path : childWatchesMap.keySet()) {
        logger.info("Restoring child watch(s):  path={}", path);
        for (Watcher watcher : childWatchesMap.get(path)) {
            try {
                this.getChildren(path, watcher);
            } catch (KeeperException e) {
                if (e.code() != Code.NONODE) {
                    logger.warn("Error while restoring watch:  " + e + ":  path=" + path, e);
                }
            } catch (Exception e) {
                logger.warn("Error while restoring watch:  " + e + ":  path=" + path, e);
            } // try
        } // for
    }
}

From source file:io.reign.zk.ResilientZkClient.java

License:Apache License

@Override
public List<String> getChildren(final String path, final boolean watch, final Stat stat)
        throws KeeperException, InterruptedException {

    if (watch) {/*from w ww.j a va2 s.c  om*/
        trackChildWatch(path, this);
    }

    ZooKeeperAction<List<String>> zkAction = new ZooKeeperAction<List<String>>(backoffStrategyFactory.get()) {
        @Override
        public List<String> doPerform() throws KeeperException, InterruptedException {
            try {
                return zooKeeper.getChildren(path, watch, stat);
            } catch (KeeperException e) {
                if (e.code() != Code.NONODE) {
                    throw e;
                }
                return Collections.EMPTY_LIST;
            }

        }
    };

    return zkAction.perform();
}

From source file:io.reign.zk.ResilientZkClient.java

License:Apache License

public List<String> getChildren(final String path, final Watcher watcher, final Stat stat)
        throws KeeperException, InterruptedException {

    if (watcher != null) {
        trackChildWatch(path, watcher);//from  w w  w  .  j a v  a  2  s  .c  o  m
    }

    ZooKeeperAction<List<String>> zkAction = new ZooKeeperAction<List<String>>(backoffStrategyFactory.get()) {
        @Override
        public List<String> doPerform() throws KeeperException, InterruptedException {
            try {
                return zooKeeper.getChildren(path, watcher, stat);
            } catch (KeeperException e) {
                if (e.code() != Code.NONODE) {
                    throw e;
                }
                return Collections.EMPTY_LIST;
            }
        }
    };

    return zkAction.perform();
}

From source file:io.reign.zk.ResilientZkClient.java

License:Apache License

@Override
public List<String> getChildren(final String path, final Watcher watcher)
        throws KeeperException, InterruptedException {

    if (watcher != null) {
        trackChildWatch(path, watcher);//  ww w.  j av  a 2  s  .  c  om
    }

    ZooKeeperAction<List<String>> zkAction = new ZooKeeperAction<List<String>>(backoffStrategyFactory.get()) {
        @Override
        public List<String> doPerform() throws KeeperException, InterruptedException {
            try {
                return zooKeeper.getChildren(path, watcher);
            } catch (KeeperException e) {
                if (e.code() != Code.NONODE) {
                    throw e;
                }
                return Collections.EMPTY_LIST;
            }

        }
    };

    return zkAction.perform();
}

From source file:io.reign.zk.ResilientZkClient.java

License:Apache License

/**
 * /*from   w w  w.j  a va  2 s.  com*/
 * @param path
 * @param watch
 * @return
 * @throws KeeperException
 * @throws InterruptedException
 */
@Override
public List<String> getChildren(final String path, final boolean watch)
        throws KeeperException, InterruptedException {

    if (watch) {
        trackChildWatch(path, this);
    }

    ZooKeeperAction<List<String>> zkAction = new ZooKeeperAction<List<String>>(backoffStrategyFactory.get()) {

        @Override
        public List<String> doPerform() throws KeeperException, InterruptedException {
            try {
                return zooKeeper.getChildren(path, watch);
            } catch (KeeperException e) {
                if (e.code() != Code.NONODE) {
                    throw e;
                }
                return Collections.EMPTY_LIST;
            }
        }

    };
    return zkAction.perform();

}

From source file:io.reign.zk.ResilientZkClient.java

License:Apache License

/**
 * // w ww  .  ja va2 s . c  o m
 * @param path
 * @param version
 * @throws InterruptedException
 * @throws KeeperException
 */
@Override
public void delete(final String path, final int version) throws InterruptedException, KeeperException {

    VoidZooKeeperAction zkAction = new VoidZooKeeperAction(backoffStrategyFactory.get()) {

        @Override
        public void doPerform() throws KeeperException, InterruptedException {
            try {
                zooKeeper.delete(path, version);
            } catch (KeeperException e) {
                if (e.code() != Code.NONODE) {
                    throw e;
                }
            }
        }

    };
    zkAction.perform();

}

From source file:io.reign.zk.ResilientZkClient.java

License:Apache License

/**
 * // ww  w .  ja v a2 s  . com
 * @param backoffStrategy
 * @param e
 * @throws KeeperException
 */
void handleKeeperException(BackoffStrategy backoffStrategy, KeeperException e) throws KeeperException {
    if (shutdown) {
        throw e;
    }

    if (!backoffStrategy.hasNext()) {
        // just throw exception if in fail fast mode
        throw e;
    } else if (this.isZooKeeperSessionError(e.code())) {
        // if it is a ZK session error, await connection renewal
        awaitConnectionInitialization(backoffStrategyFactory.get());
    } else {
        throw e;
    } // if
}