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:fr.eurecom.hybris.mds.MdsManager.java

License:Apache License

/**
 * Timestamped write on metadata storage.
 * @param key - the key//from   www  .  j ava 2 s.  co  m
 * @param md - the metadata to be written
 * @param zkVersion - the znode version expected to be overwritten; -1 when the znode does not exist
 * @return boolean: true if a znode has been modified and stale old values need to be garbage-collected
 *                  false otherwise: a new znode has been created
 * @throws HybrisException
 */
public boolean tsWrite(String key, Metadata md, int zkVersion) throws HybrisException {

    String path = this.storageRoot + "/" + key;
    try {
        if (zkVersion == NONODE) {
            this.zkCli.create().forPath(path, md.serialize());
            logger.debug("ZNode {} created.", path);
            return false;
        } else {
            this.zkCli.setData().withVersion(zkVersion).forPath(path, md.serialize());
            logger.debug("ZNode {} modified.", path);
            return true;
        }
    } catch (KeeperException e) { // NONODE exception should not happen since we set a tombstone value upon deletion

        if (e.code() == KeeperException.Code.NODEEXISTS || // multiple clients tried to create
                e.code() == KeeperException.Code.BADVERSION) { // or modify the same znode concurrently

            Stat stat = new Stat();
            byte[] newValue = null;
            try {
                newValue = this.zkCli.getData().storingStatIn(stat).forPath(path);
            } catch (Exception e1) {
                throw new HybrisException(e1);
            }

            Metadata newmd = new Metadata(newValue);
            if (md.getTs().isGreater(newmd.getTs())) {
                logger.debug("Found smaller version ({}) writing {}: retrying.", newmd.getTs(), key);
                return this.tsWrite(key, md, stat.getVersion());
            } else {
                logger.warn("Found greater version ({}) writing {}: failing.", newmd.getTs(), key);
                return false; // XXX
                // throw new HybrisException("KeeperException, could not write the key.", e);
            }

        } else {
            logger.error("Could not write ZNode " + key);
            throw new HybrisException("Could not write the ZNode " + key, e);
        }

    } catch (Exception e) {
        logger.error("Could not write ZNode " + key, e);
        throw new HybrisException("Could not write ZNode " + key + ": " + e.getMessage(), e);
    }
}

From source file:fr.eurecom.hybris.mds.MdsManager.java

License:Apache License

/**
 * Timestamped read ("slow read" in ZooKeeper parlance) from metadata storage.
 * @param key the key to read/*from  w  w  w  .j a v  a2 s  .com*/
 * @param stat the Stat Zookeeper object to be written with znode details (can be null)
 * @return Metadata object
 *              or null in case the znode does not exist or there is a tombstone Metadata object
 *              (to distinguish these two cases one must use the Stat object)
 * @throws HybrisException
 */
public Metadata tsRead(String key, Stat stat) throws HybrisException {

    String path = this.storageRoot + "/" + key;
    try {
        this.zkCli.sync().forPath(path);
        byte[] rawMd = this.zkCli.getData().storingStatIn(stat).forPath(path);
        return new Metadata(rawMd);
    } catch (KeeperException e) {

        if (e.code() == KeeperException.Code.NONODE)
            return null;
        else {
            logger.error("Could not read ZNode " + path, e);
            throw new HybrisException("Could not read the ZNode " + path, e);
        }

    } catch (Exception e) {
        logger.error("Could not read ZNode " + path, e);
        throw new HybrisException("Could not read the ZNode " + path + e.getMessage(), e);
    }
}

From source file:fr.eurecom.hybris.mds.MdsManager.java

License:Apache License

/**
 * Timestamped read ("slow read" in ZooKeeper parlance) from metadata storage.
 * @param key the key to read/*  w  ww .  j  a  v a 2s  .co  m*/
 * @param stat the Stat Zookeeper object to be written with znode details (can be null)
 * @param watcher to set upon executing the getData operation
 * @return Metadata object
 *              or null in case the znode does not exist or there is a tombstone Metadata object
 *              (to distinguish these two cases one must use the Stat object)
 * @throws HybrisException
 */
public Metadata tsRead(String key, Stat stat, CuratorWatcher watcher) throws HybrisException {

    String path = this.storageRoot + "/" + key;
    try {
        this.zkCli.sync().forPath(path);
        byte[] rawMd = this.zkCli.getData().storingStatIn(stat).usingWatcher(watcher).forPath(path);
        return new Metadata(rawMd);
    } catch (KeeperException e) {

        if (e.code() == KeeperException.Code.NONODE)
            return null;
        else {
            logger.error("Could not read ZNode " + path, e);
            throw new HybrisException("Could not read the ZNode " + path, e);
        }

    } catch (Exception e) {
        logger.error("Could not read ZNode " + path, e);
        throw new HybrisException("Could not read the ZNode " + path + e.getMessage(), e);
    }
}

From source file:fr.eurecom.hybris.mds.MdsManager.java

License:Apache License

/**
 * Delete the set of orphan keys passed as argument.
 * @param orphanKeys//from   w w  w  . j a  v  a  2 s .  co m
 */
public void removeOrphanKeys(Set<String> orphanKeys) {

    for (String key : orphanKeys) {
        String znodePath = this.gcOrphansDir + "/" + key;
        try {
            this.zkCli.delete().forPath(znodePath);
        } catch (KeeperException e) {
            if (e.code() != KeeperException.Code.NONODE)
                logger.warn("Could not delete orphan ZNode " + znodePath, e);
        } catch (Exception e) {
            logger.warn("Could not delete orphan ZNode " + znodePath, e);
        }
    }
}

From source file:fr.eurecom.hybris.mds.MdsManager.java

License:Apache License

/**
 * Delete the key passed as argument from the list of keys
 * to be checked as outdated for gc.//  www .  ja va  2s  .c  o  m
 * @param staleKeys
 */
public void removeStaleKey(String staleKey) {

    String znodePath = this.gcStaleDir + "/" + staleKey;
    try {
        this.zkCli.delete().forPath(znodePath);
    } catch (KeeperException e) {
        if (e.code() != KeeperException.Code.NONODE)
            logger.warn("Could not delete orphan ZNode " + znodePath, e);
    } catch (Exception e) {
        logger.warn("Could not delete orphan ZNode " + znodePath, e);
    }
}

From source file:fr.eurecom.hybris.mds.ZkRmds.java

License:Apache License

/**
 * Constructs a new MdsManager.//from w  ww  .ja v a  2s .com
 * 
 * @param zkConnectionStr
 *            Zookeeper cluster connection string (e.g.
 *            "zksrv1.net:2181,zksrv2.net:2181")
 * @param zkRoot
 *            the Hybris metadata root folder
 * @throws IOException
 *             thrown in case of error while initializing the Zookeeper
 *             client
 */
public ZkRmds(String zkConnectionStr, String zkRoot, boolean qRead) throws IOException {

    this.storageRoot = "/" + zkRoot;

    this.gcRoot = this.storageRoot + "-gc";
    this.gcStaleDir = this.gcRoot + "/stale";
    this.gcOrphansDir = this.gcRoot + "/orphans";
    this.quorumRead = qRead;

    try {
        RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
        this.zkCli = CuratorFrameworkFactory.newClient(zkConnectionStr, retryPolicy);
        this.zkCli.getConnectionStateListenable().addListener(this);
        this.zkCli.start();

        for (String dir : new String[] { this.storageRoot, this.gcRoot, this.gcStaleDir, this.gcOrphansDir })
            try {
                this.zkCli.create().forPath(dir);
                logger.debug("Created {}.", dir);
            } catch (KeeperException e) {
                if (e.code() != KeeperException.Code.NODEEXISTS)
                    throw e;
            }

    } catch (Exception e) {
        logger.error("Could not initialize the Zookeeper client. " + e.getMessage(), e);
        throw new IOException(e);
    }
}

From source file:fr.eurecom.hybris.mds.ZkRmds.java

License:Apache License

@Override
public boolean tsWrite(String key, Metadata md, long zkVersion) throws HybrisException {

    String path = this.storageRoot + "/" + key;
    try {/*from  w  w w.  j a v a  2s .  c  o  m*/
        if (zkVersion == NONODE) {
            this.zkCli.create().forPath(path, md.serialize());
            logger.debug("ZNode {} created.", path);
            return false;
        } else {
            this.zkCli.setData().withVersion((int) zkVersion).forPath(path, md.serialize());
            logger.debug("ZNode {} modified.", path);
            return true;
        }
    } catch (KeeperException e) { // NONODE exception should not happen
        // since we set a tombstone value upon
        // deletion

        if (e.code() == KeeperException.Code.NODEEXISTS || // multiple
        // clients tried
        // to create
                e.code() == KeeperException.Code.BADVERSION) { // or modify
            // the same
            // znode
            // concurrently

            Stat stat = new Stat();
            byte[] newValue = null;
            try {
                newValue = this.zkCli.getData().storingStatIn(stat).forPath(path);
            } catch (Exception e1) {
                throw new HybrisException(e1);
            }

            Metadata newmd = new Metadata(newValue);
            if (md.getTs().isGreater(newmd.getTs())) {
                logger.debug("Found smaller version ({}) writing {}: retrying.", newmd.getTs(), key);
                return this.tsWrite(key, md, stat.getVersion());
            } else {
                logger.debug("Found greater version ({}) writing {}: overwritten.", newmd.getTs(), key);
                return false;
            }

        } else {
            logger.error("Could not write ZNode " + key);
            throw new HybrisException("Could not write the ZNode " + key, e);
        }

    } catch (Exception e) {
        logger.error("Could not write ZNode " + key, e);
        throw new HybrisException("Could not write ZNode " + key + ": " + e.getMessage(), e);
    }
}

From source file:fr.eurecom.hybris.mds.ZkRmds.java

License:Apache License

public Metadata tsRead(String key, Stat stat) throws HybrisException {

    String path = this.storageRoot + "/" + key;
    try {//ww w  . j  av a 2s . co  m
        if (quorumRead)
            this.zkCli.setData().forPath(this.storageRoot, new byte[] { (byte) 0x00 });
        else
            this.zkCli.sync().forPath(path);
        byte[] rawMd = this.zkCli.getData().storingStatIn(stat).forPath(path);
        return new Metadata(rawMd);
    } catch (KeeperException e) {

        if (e.code() == KeeperException.Code.NONODE)
            return null;
        else {
            logger.error("Could not read ZNode " + path, e);
            throw new HybrisException("Could not read the ZNode " + path, e);
        }

    } catch (Exception e) {
        logger.error("Could not read ZNode " + path, e);
        throw new HybrisException("Could not read the ZNode " + path + e.getMessage(), e);
    }
}

From source file:fr.eurecom.hybris.mds.ZkRmds.java

License:Apache License

public Metadata tsRead(String key, Stat stat, HybrisWatcher watcher) throws HybrisException {

    String path = this.storageRoot + "/" + key;
    try {//w  w w .j av a 2s  .com
        if (quorumRead)
            this.zkCli.setData().forPath(this.storageRoot, new byte[] { (byte) 0x00 });
        else
            this.zkCli.sync().forPath(path);
        byte[] rawMd = this.zkCli.getData().storingStatIn(stat).usingWatcher(watcher).forPath(path);
        return new Metadata(rawMd);
    } catch (KeeperException e) {

        if (e.code() == KeeperException.Code.NONODE)
            return null;
        else {
            logger.error("Could not read ZNode " + path, e);
            throw new HybrisException("Could not read the ZNode " + path, e);
        }

    } catch (Exception e) {
        logger.error("Could not read ZNode " + path, e);
        throw new HybrisException("Could not read the ZNode " + path + e.getMessage(), e);
    }
}

From source file:fr.eurecom.hybris.mds.ZkRmds.java

License:Apache License

public void markStaleKey(final String key) {
    new Thread(new Runnable() {
        public void run() {
            String path = gcStaleDir + "/" + key;
            try {
                // create ZNode <root>-gc/stale/<key>
                ZkRmds.this.zkCli.create().forPath(path);
                logger.debug("GcMarker: marked {} as stale", path);
            } catch (KeeperException e) {
                if (e.code() != KeeperException.Code.NODEEXISTS)
                    logger.warn("GcMarker: could not create stale node " + path, e);
            } catch (Exception e) {
                logger.warn("GcMarker: could not create stale node " + path, e);
            }/*from w  w w .  j  ava2s  .  co m*/
        }
    }).start();
}