Example usage for org.apache.zookeeper KeeperException getMessage

List of usage examples for org.apache.zookeeper KeeperException getMessage

Introduction

In this page you can find the example usage for org.apache.zookeeper KeeperException getMessage.

Prototype

@Override
    public String getMessage() 

Source Link

Usage

From source file:com.oneapm.base.tools.ZookeeperClient.java

/**
 * ?//from www  .ja v a 2  s.c  o  m
 *
 * @param path
 * @return
 */
public boolean isExists(String path) {
    try {
        Stat stat = this.zk.exists(path, false);
        return null != stat;
    } catch (KeeperException e) {
        LOG.error("??,?KeeperException! path: " + path + ", errMsg:" + e.getMessage(), e);
    } catch (InterruptedException e) {
        LOG.error("??,?InterruptedException! path: " + path + ", errMsg:" + e.getMessage(),
                e);
    }
    return false;
}

From source file:com.oneapm.base.tools.ZookeeperClient.java

/**
 * ??//from   ww w.  java  2 s  . com
 *
 * @param path
 * @param data
 * @return
 */
public boolean writeData(String path, String data) {
    try {
        Stat stat = zk.setData(path, data.getBytes(), -1);
        //            LOG.info("??, path" + path + ", stat: " + stat);
        return true;
    } catch (KeeperException e) {
        LOG.error("?, ?KeeperException! path: " + path + ", data:" + data + ", errMsg:"
                + e.getMessage(), e);
    } catch (InterruptedException e) {
        LOG.error("?, ?InterruptedException! path: " + path + ", data:" + data
                + ", errMsg:" + e.getMessage(), e);
    }

    return false;
}

From source file:com.oneapm.base.tools.ZookeeperClient.java

/**
 * ??//  w  w w  .j av a 2 s. com
 *
 * @param path
 * @return
 */
public String readData(String path) {
    String data = null;
    try {
        byte[] bt = zk.getData(path, this, null);
        if (bt != null) {
            data = new String(bt);
        }
        LOG.info("???, path:" + path + ", content:" + data);
    } catch (KeeperException e) {
        LOG.error("??,?KeeperException! path: " + path + ", errMsg:" + e.getMessage(), e);
    } catch (InterruptedException e) {
        LOG.error("??,?InterruptedException! path: " + path + ", errMsg:" + e.getMessage(),
                e);
    }
    return data;
}

From source file:com.smartitengineering.cms.spi.lock.impl.distributed.ZKLock.java

License:Open Source License

protected boolean tryRemoteLock(String lockId, final long availableMillisForRemoteLock)
        throws IllegalStateException, InterruptedException {
    final LocalLockRegistrar registrar = config.getRegistrar();
    final ZooKeeper keeper = config.getZooKeeper();
    final String node = getNode();
    if (logger.isDebugEnabled()) {
        logger.debug("Attained local lock " + lockId);
    }/* w  w  w.j a  v a2 s .  c o  m*/
    try {
        if (StringUtils.isNotBlank(lockId)) {
            keeper.create(node, org.apache.commons.codec.binary.StringUtils.getBytesUtf8(config.getNodeId()),
                    Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
            keeper.exists(node, this);
            localLockId = lockId;
            return true;
        } else {
            return false;
        }
    } catch (KeeperException ke) {
        if (ke.code() == KeeperException.Code.NODEEXISTS) {
            logger.debug("Lock alrady exists!");
            if (availableMillisForRemoteLock > 0) {
                synchronized (ZKLock.this) {
                    try {
                        keeper.exists(node, new Watcher() {

                            public void process(WatchedEvent event) {
                                if (event.getType().equals(Event.EventType.NodeDeleted)) {
                                    synchronized (ZKLock.this) {
                                        ZKLock.this.notifyAll();
                                    }
                                }
                            }
                        });
                    } catch (Exception ex) {
                        logger.error("Could not attach watcher", ex);
                    }
                    final long remoteStart = System.currentTimeMillis();
                    ZKLock.this.wait(availableMillisForRemoteLock);
                    return tryRemoteLock(lockId,
                            availableMillisForRemoteLock - (System.currentTimeMillis() - remoteStart));
                }
            } else {
                registrar.unlock(key, lockId);
                return false;
            }
        } else {
            logger.error(ke.getMessage(), ke);
            throw new IllegalStateException(ke);
        }
    } catch (Exception ex) {
        registrar.unlock(key, lockId);
        logger.error(ex.getMessage(), ex);
        throw new IllegalStateException(ex);
    }
}

From source file:com.yahoo.pulsar.discovery.service.ZookeeperCacheLoader.java

License:Apache License

/**
 * 1. creates ZooKeeper Children cache on path {@value LOADBALANCE_BROKERS_ROOT}, 2. sets watch on the path and 3.
 * maintain list of available brokers at availableActiveBrokers
 * /*from  w  w w. ja  v a  2  s  .  c o m*/
 * @throws InterruptedException
 * @throws IOException
 * 
 */
private void initializeBrokerList() throws InterruptedException, IOException {
    this.availableActiveBrokerCache = new ZooKeeperChildrenCache(getLocalZkCache(), LOADBALANCE_BROKERS_ROOT);
    this.availableActiveBrokerCache.registerListener(new ZooKeeperCacheListener<Set<String>>() {
        @Override
        public void onUpdate(String path, Set<String> data, Stat stat) {
            if (log.isDebugEnabled()) {
                log.debug("Update Received for path {}", path);
            }
            availableActiveBrokers = Lists.newArrayList(data);
        }
    });
    // initialize available broker list
    try {
        this.availableActiveBrokers = Lists.newArrayList(availableActiveBrokerCache.get());
    } catch (KeeperException e) {
        log.warn("Failed to find broker znode children under {}", LOADBALANCE_BROKERS_ROOT, e);
        throw new IOException(String.format("Failed to find broker list in zk at %s with %s ",
                LOADBALANCE_BROKERS_ROOT, e.getMessage()), e);
    }
}

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

License:Apache License

/**
 * Constructs a new MdsManager./* w  ww  .  jav a2  s  .co  m*/
 * @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 MdsManager(String zkConnectionStr, String zkRoot) throws IOException {

    this.storageRoot = "/" + zkRoot;

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

    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.MdsManager.java

License:Apache License

/**
 * Timestamped write on metadata storage.
 * @param key - the key//  www .j  a va2  s . c  o  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  av a  2  s.  c  o  m*/
 * @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/* www.  j a  va 2 s.c o  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.ZkRmds.java

License:Apache License

/**
 * Constructs a new MdsManager.//ww  w. jav a 2  s . c om
 * 
 * @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);
    }
}