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:org.apache.accumulo.fate.zookeeper.ZooCache.java

License:Apache License

/**
 * Gets the children of the given node. A watch is established by this call.
 *
 * @param zPath/*w ww  .ja v a 2 s  . c  om*/
 *          path of node
 * @return children list, or null if node has no children or does not exist
 */
public synchronized List<String> getChildren(final String zPath) {

    ZooRunnable<List<String>> zr = new ZooRunnable<List<String>>() {

        @Override
        public List<String> run() throws KeeperException, InterruptedException {
            try {
                cacheReadLock.lock();
                if (childrenCache.containsKey(zPath)) {
                    return childrenCache.get(zPath);
                }
            } finally {
                cacheReadLock.unlock();
            }

            cacheWriteLock.lock();
            try {
                if (childrenCache.containsKey(zPath)) {
                    return childrenCache.get(zPath);
                }

                final ZooKeeper zooKeeper = getZooKeeper();

                List<String> children = zooKeeper.getChildren(zPath, watcher);
                childrenCache.put(zPath, children);
                return children;
            } catch (KeeperException ke) {
                if (ke.code() != Code.NONODE) {
                    throw ke;
                }
            } finally {
                cacheWriteLock.unlock();
            }
            return null;
        }

    };

    List<String> children = zr.retry();

    if (children == null) {
        return null;
    }
    return Collections.unmodifiableList(children);
}

From source file:org.apache.accumulo.fate.zookeeper.ZooReader.java

License:Apache License

@Override
public byte[] getData(String zPath, boolean watch, Stat stat) throws KeeperException, InterruptedException {
    final Retry retry = getRetryFactory().create();
    while (true) {
        try {/*from ww w  .jav  a 2  s. c  o m*/
            return getZooKeeper().getData(zPath, watch, stat);
        } catch (KeeperException e) {
            final Code code = e.code();
            if (code == Code.CONNECTIONLOSS || code == Code.OPERATIONTIMEOUT || code == Code.SESSIONEXPIRED) {
                retryOrThrow(retry, e);
            } else {
                throw e;
            }
        }

        retry.waitForNextAttempt();
    }
}

From source file:org.apache.accumulo.fate.zookeeper.ZooReader.java

License:Apache License

@Override
public byte[] getData(String zPath, Watcher watcher, Stat stat) throws KeeperException, InterruptedException {
    final Retry retry = getRetryFactory().create();
    while (true) {
        try {//from   ww w.  j av a 2  s  .c o m
            return getZooKeeper().getData(zPath, watcher, stat);
        } catch (KeeperException e) {
            final Code code = e.code();
            if (code == Code.CONNECTIONLOSS || code == Code.OPERATIONTIMEOUT || code == Code.SESSIONEXPIRED) {
                retryOrThrow(retry, e);
            } else {
                throw e;
            }
        }

        retry.waitForNextAttempt();
    }
}

From source file:org.apache.accumulo.fate.zookeeper.ZooReader.java

License:Apache License

@Override
public Stat getStatus(String zPath) throws KeeperException, InterruptedException {
    final Retry retry = getRetryFactory().create();
    while (true) {
        try {/*from  ww  w  .  j  a v a  2  s  .co m*/
            return getZooKeeper().exists(zPath, false);
        } catch (KeeperException e) {
            final Code code = e.code();
            if (code == Code.CONNECTIONLOSS || code == Code.OPERATIONTIMEOUT || code == Code.SESSIONEXPIRED) {
                retryOrThrow(retry, e);
            } else {
                throw e;
            }
        }

        retry.waitForNextAttempt();
    }
}

From source file:org.apache.accumulo.fate.zookeeper.ZooReader.java

License:Apache License

@Override
public Stat getStatus(String zPath, Watcher watcher) throws KeeperException, InterruptedException {
    final Retry retry = getRetryFactory().create();
    while (true) {
        try {//from   ww  w  .j  ava2  s.  c  o  m
            return getZooKeeper().exists(zPath, watcher);
        } catch (KeeperException e) {
            final Code code = e.code();
            if (code == Code.CONNECTIONLOSS || code == Code.OPERATIONTIMEOUT || code == Code.SESSIONEXPIRED) {
                retryOrThrow(retry, e);
            } else {
                throw e;
            }
        }

        retry.waitForNextAttempt();
    }
}

From source file:org.apache.accumulo.fate.zookeeper.ZooReader.java

License:Apache License

@Override
public List<String> getChildren(String zPath) throws KeeperException, InterruptedException {
    final Retry retry = getRetryFactory().create();
    while (true) {
        try {//  w  w w  .java 2  s  .  c om
            return getZooKeeper().getChildren(zPath, false);
        } catch (KeeperException e) {
            final Code code = e.code();
            if (code == Code.CONNECTIONLOSS || code == Code.OPERATIONTIMEOUT || code == Code.SESSIONEXPIRED) {
                retryOrThrow(retry, e);
            } else {
                throw e;
            }
        }

        retry.waitForNextAttempt();
    }
}

From source file:org.apache.accumulo.fate.zookeeper.ZooReader.java

License:Apache License

@Override
public List<String> getChildren(String zPath, Watcher watcher) throws KeeperException, InterruptedException {
    final Retry retry = getRetryFactory().create();
    while (true) {
        try {// w w  w  .  j av  a  2  s .c o  m
            return getZooKeeper().getChildren(zPath, watcher);
        } catch (KeeperException e) {
            final Code code = e.code();
            if (code == Code.CONNECTIONLOSS || code == Code.OPERATIONTIMEOUT || code == Code.SESSIONEXPIRED) {
                retryOrThrow(retry, e);
            } else {
                throw e;
            }
        }

        retry.waitForNextAttempt();
    }
}

From source file:org.apache.accumulo.fate.zookeeper.ZooReader.java

License:Apache License

@Override
public boolean exists(String zPath) throws KeeperException, InterruptedException {
    final Retry retry = getRetryFactory().create();
    while (true) {
        try {//  ww w . ja  v  a2s . c o m
            return getZooKeeper().exists(zPath, false) != null;
        } catch (KeeperException e) {
            final Code code = e.code();
            if (code == Code.CONNECTIONLOSS || code == Code.OPERATIONTIMEOUT || code == Code.SESSIONEXPIRED) {
                retryOrThrow(retry, e);
            } else {
                throw e;
            }
        }

        retry.waitForNextAttempt();
    }
}

From source file:org.apache.accumulo.fate.zookeeper.ZooReader.java

License:Apache License

@Override
public boolean exists(String zPath, Watcher watcher) throws KeeperException, InterruptedException {
    final Retry retry = getRetryFactory().create();
    while (true) {
        try {//ww  w.j  a v  a 2s . c o  m
            return getZooKeeper().exists(zPath, watcher) != null;
        } catch (KeeperException e) {
            final Code code = e.code();
            if (code == Code.CONNECTIONLOSS || code == Code.OPERATIONTIMEOUT || code == Code.SESSIONEXPIRED) {
                retryOrThrow(retry, e);
            } else {
                throw e;
            }
        }

        retry.waitForNextAttempt();
    }
}

From source file:org.apache.accumulo.fate.zookeeper.ZooReader.java

License:Apache License

@Override
public void sync(final String path) throws KeeperException, InterruptedException {
    final AtomicInteger rc = new AtomicInteger();
    final CountDownLatch waiter = new CountDownLatch(1);
    getZooKeeper().sync(path, new VoidCallback() {
        @Override/*from  ww  w  .j a v a 2 s  .co  m*/
        public void processResult(int code, String arg1, Object arg2) {
            rc.set(code);
            waiter.countDown();
        }
    }, null);
    waiter.await();
    Code code = Code.get(rc.get());
    if (code != KeeperException.Code.OK) {
        throw KeeperException.create(code);
    }
}