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:com.talis.platform.sequencing.zookeeper.ZkClock.java

License:Apache License

private void createKey(String key) throws KeeperException {
    LOG.debug(String.format("Creating new node for key %s", key));
    try {/*from www  .j  a  v a  2  s. c o  m*/
        myZooKeeper.create(key, DEFAULT_DATA, DEFAULT_ACL, CreateMode.PERSISTENT);
        myMetrics.incrementKeyCreations();
    } catch (InterruptedException e) {
        myMetrics.incrementInterruptedExceptions();
        LOG.error(String.format("Caught InterruptedException when creating key %s", key), e);
    } catch (KeeperException e) {
        if (e.code().equals(KeeperException.Code.NODEEXISTS)) {
            LOG.info(String.format(
                    "Tried to create %s, but it already exists. " + "Probably a (harmless) race condition",
                    key));
        } else {
            myMetrics.incrementKeeperExceptions();
            throw e;
        }
    }
}

From source file:com.twitter.common.zookeeper.ZooKeeperUtils.java

License:Apache License

/**
 * Returns true if the given exception indicates an error that can be resolved by retrying the
 * operation without modification./* w w w.j a v a2  s  . c  om*/
 *
 * @param e the exception to check
 * @return true if the causing operation is strictly retryable
 */
public static boolean isRetryable(KeeperException e) {
    Preconditions.checkNotNull(e);

    switch (e.code()) {
    case CONNECTIONLOSS:
    case SESSIONEXPIRED:
    case SESSIONMOVED:
    case OPERATIONTIMEOUT:
        return true;

    case RUNTIMEINCONSISTENCY:
    case DATAINCONSISTENCY:
    case MARSHALLINGERROR:
    case BADARGUMENTS:
    case NONODE:
    case NOAUTH:
    case BADVERSION:
    case NOCHILDRENFOREPHEMERALS:
    case NODEEXISTS:
    case NOTEMPTY:
    case INVALIDCALLBACK:
    case INVALIDACL:
    case AUTHFAILED:
    case UNIMPLEMENTED:

        // These two should not be encountered - they are used internally by ZK to specify ranges
    case SYSTEMERROR:
    case APIERROR:

    case OK: // This is actually an invalid ZK exception code

    default:
        return false;
    }
}

From source file:com.twitter.distributedlog.acl.ZKAccessControl.java

License:Apache License

public Future<ZKAccessControl> create(ZooKeeperClient zkc) {
    final Promise<ZKAccessControl> promise = new Promise<ZKAccessControl>();
    try {//from   w w w . ja  va2 s .  com
        zkc.get().create(zkPath, serialize(accessControlEntry), zkc.getDefaultACL(), CreateMode.PERSISTENT,
                new AsyncCallback.StringCallback() {
                    @Override
                    public void processResult(int rc, String path, Object ctx, String name) {
                        if (KeeperException.Code.OK.intValue() == rc) {
                            ZKAccessControl.this.zkVersion = 0;
                            promise.setValue(ZKAccessControl.this);
                        } else {
                            promise.setException(KeeperException.create(KeeperException.Code.get(rc)));
                        }
                    }
                }, null);
    } catch (ZooKeeperClient.ZooKeeperConnectionException e) {
        promise.setException(e);
    } catch (InterruptedException e) {
        promise.setException(e);
    } catch (IOException e) {
        promise.setException(e);
    }
    return promise;
}

From source file:com.twitter.distributedlog.acl.ZKAccessControl.java

License:Apache License

public Future<ZKAccessControl> update(ZooKeeperClient zkc) {
    final Promise<ZKAccessControl> promise = new Promise<ZKAccessControl>();
    try {/*from  w  w w. j  a  v  a 2 s  . co m*/
        zkc.get().setData(zkPath, serialize(accessControlEntry), zkVersion, new AsyncCallback.StatCallback() {
            @Override
            public void processResult(int rc, String path, Object ctx, Stat stat) {
                if (KeeperException.Code.OK.intValue() == rc) {
                    ZKAccessControl.this.zkVersion = stat.getVersion();
                    promise.setValue(ZKAccessControl.this);
                } else {
                    promise.setException(KeeperException.create(KeeperException.Code.get(rc)));
                }
            }
        }, null);
    } catch (ZooKeeperClient.ZooKeeperConnectionException e) {
        promise.setException(e);
    } catch (InterruptedException e) {
        promise.setException(e);
    } catch (IOException e) {
        promise.setException(e);
    }
    return promise;
}

From source file:com.twitter.distributedlog.acl.ZKAccessControl.java

License:Apache License

public static Future<ZKAccessControl> read(final ZooKeeperClient zkc, final String zkPath, Watcher watcher) {
    final Promise<ZKAccessControl> promise = new Promise<ZKAccessControl>();

    try {/*from  w w  w . ja  v  a  2  s. c  o  m*/
        zkc.get().getData(zkPath, watcher, new AsyncCallback.DataCallback() {
            @Override
            public void processResult(int rc, String path, Object ctx, byte[] data, Stat stat) {
                if (KeeperException.Code.OK.intValue() == rc) {
                    try {
                        AccessControlEntry ace = deserialize(zkPath, data);
                        promise.setValue(new ZKAccessControl(ace, zkPath, stat.getVersion()));
                    } catch (IOException ioe) {
                        promise.setException(ioe);
                    }
                } else {
                    promise.setException(KeeperException.create(KeeperException.Code.get(rc)));
                }
            }
        }, null);
    } catch (ZooKeeperClient.ZooKeeperConnectionException e) {
        promise.setException(e);
    } catch (InterruptedException e) {
        promise.setException(e);
    }
    return promise;
}

From source file:com.twitter.distributedlog.acl.ZKAccessControl.java

License:Apache License

public static Future<Void> delete(final ZooKeeperClient zkc, final String zkPath) {
    final Promise<Void> promise = new Promise<Void>();

    try {//from w  w  w . j  a  v a  2s  . c om
        zkc.get().delete(zkPath, -1, new AsyncCallback.VoidCallback() {
            @Override
            public void processResult(int rc, String path, Object ctx) {
                if (KeeperException.Code.OK.intValue() == rc || KeeperException.Code.NONODE.intValue() == rc) {
                    promise.setValue(null);
                } else {
                    promise.setException(KeeperException.create(KeeperException.Code.get(rc)));
                }
            }
        }, null);
    } catch (ZooKeeperClient.ZooKeeperConnectionException e) {
        promise.setException(e);
    } catch (InterruptedException e) {
        promise.setException(e);
    }
    return promise;
}

From source file:com.twitter.distributedlog.acl.ZKAccessControlManager.java

License:Apache License

private void fetchAccessControlEntries(final Promise<Void> promise) {
    try {/*from   ww  w.j av a  2s .c om*/
        zkc.get().getChildren(zkRootPath, this, new AsyncCallback.Children2Callback() {
            @Override
            public void processResult(int rc, String path, Object ctx, List<String> children, Stat stat) {
                if (KeeperException.Code.OK.intValue() != rc) {
                    promise.setException(KeeperException.create(KeeperException.Code.get(rc)));
                    return;
                }
                Set<String> streamsReceived = new HashSet<String>();
                streamsReceived.addAll(children);
                Set<String> streamsCached = streamEntries.keySet();
                Set<String> streamsRemoved = Sets.difference(streamsCached, streamsReceived).immutableCopy();
                for (String s : streamsRemoved) {
                    ZKAccessControl accessControl = streamEntries.remove(s);
                    if (null != accessControl) {
                        logger.info("Removed Access Control Entry for stream {} : {}", s,
                                accessControl.getAccessControlEntry());
                    }
                }
                if (streamsReceived.isEmpty()) {
                    promise.setValue(null);
                    return;
                }
                final AtomicInteger numPendings = new AtomicInteger(streamsReceived.size());
                final AtomicInteger numFailures = new AtomicInteger(0);
                for (String s : streamsReceived) {
                    final String streamName = s;
                    ZKAccessControl.read(zkc, zkRootPath + "/" + streamName, null)
                            .addEventListener(new FutureEventListener<ZKAccessControl>() {

                                @Override
                                public void onSuccess(ZKAccessControl accessControl) {
                                    streamEntries.put(streamName, accessControl);
                                    logger.info("Added overrided access control for stream {} : {}", streamName,
                                            accessControl.getAccessControlEntry());
                                    complete();
                                }

                                @Override
                                public void onFailure(Throwable cause) {
                                    if (cause instanceof KeeperException.NoNodeException) {
                                        streamEntries.remove(streamName);
                                    } else if (cause instanceof ZKAccessControl.CorruptedAccessControlException) {
                                        logger.warn(
                                                "Access control is corrupted for stream {} @ {}, skipped it ...",
                                                new Object[] { streamName, zkRootPath, cause });
                                        streamEntries.remove(streamName);
                                    } else {
                                        if (1 == numFailures.incrementAndGet()) {
                                            promise.setException(cause);
                                        }
                                    }
                                    complete();
                                }

                                private void complete() {
                                    if (0 == numPendings.decrementAndGet() && numFailures.get() == 0) {
                                        promise.setValue(null);
                                    }
                                }
                            });
                }
            }
        }, null);
    } catch (ZooKeeperClient.ZooKeeperConnectionException e) {
        promise.setException(e);
    } catch (InterruptedException e) {
        promise.setException(e);
    }
}

From source file:com.twitter.distributedlog.acl.ZKAccessControlManager.java

License:Apache License

private void createDefaultAccessControlEntryIfNeeded(final Promise<ZKAccessControl> promise) {
    ZooKeeper zk;// w w  w.  ja  v  a2  s .c  o  m
    try {
        zk = zkc.get();
    } catch (ZooKeeperClient.ZooKeeperConnectionException e) {
        promise.setException(e);
        return;
    } catch (InterruptedException e) {
        promise.setException(e);
        return;
    }
    ZkUtils.asyncCreateFullPathOptimistic(zk, zkRootPath, new byte[0], zkc.getDefaultACL(),
            CreateMode.PERSISTENT, new AsyncCallback.StringCallback() {
                @Override
                public void processResult(int rc, String path, Object ctx, String name) {
                    if (KeeperException.Code.OK.intValue() == rc) {
                        logger.info("Created zk path {} for default ACL.", zkRootPath);
                        fetchDefaultAccessControlEntry(promise);
                    } else {
                        promise.setException(KeeperException.create(KeeperException.Code.get(rc)));
                    }
                }
            }, null);
}

From source file:com.twitter.distributedlog.auditor.DLAuditor.java

License:Apache License

private void collectLedgersFromAllocator(final URI uri,
        final com.twitter.distributedlog.DistributedLogManagerFactory factory,
        final List<String> allocationPaths, final Set<Long> ledgers) throws IOException {
    final LinkedBlockingQueue<String> poolQueue = new LinkedBlockingQueue<String>();
    for (String allocationPath : allocationPaths) {
        String rootPath = uri.getPath() + "/" + allocationPath;
        try {//from ww w.j a v a 2 s.  com
            List<String> pools = getZooKeeperClient(factory).get().getChildren(rootPath, false);
            for (String pool : pools) {
                poolQueue.add(rootPath + "/" + pool);
            }
        } catch (KeeperException e) {
            throw new ZKException("Failed to get list of pools from " + rootPath, e);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            throw new DLInterruptedException("Interrupted on getting list of pools from " + rootPath, e);
        }
    }

    logger.info("Collecting ledgers from allocators for {} : {}", uri, poolQueue);

    executeAction(poolQueue, 10, new Action<String>() {
        @Override
        public void execute(String poolPath) throws IOException {
            try {
                collectLedgersFromPool(poolPath);
            } catch (InterruptedException e) {
                throw new DLInterruptedException(
                        "Interrupted on collecting ledgers from allocation pool " + poolPath, e);
            } catch (KeeperException e) {
                throw new ZKException("Failed to collect ledgers from allocation pool " + poolPath, e.code());
            }
        }

        private void collectLedgersFromPool(String poolPath)
                throws InterruptedException, ZooKeeperClient.ZooKeeperConnectionException, KeeperException {
            List<String> allocators = getZooKeeperClient(factory).get().getChildren(poolPath, false);
            for (String allocator : allocators) {
                String allocatorPath = poolPath + "/" + allocator;
                byte[] data = getZooKeeperClient(factory).get().getData(allocatorPath, false, new Stat());
                if (null != data && data.length > 0) {
                    try {
                        long ledgerId = DLUtils.bytes2LedgerId(data);
                        synchronized (ledgers) {
                            ledgers.add(ledgerId);
                        }
                    } catch (NumberFormatException nfe) {
                        logger.warn("Invalid ledger found in allocator path {} : ", allocatorPath, nfe);
                    }
                }
            }
        }
    });

    logger.info("Collected ledgers from allocators for {}.", uri);
}

From source file:com.twitter.distributedlog.bk.LedgerAllocatorPool.java

License:Apache License

private void createAllocators(int numAllocators) throws InterruptedException, IOException {
    final AtomicInteger numPendings = new AtomicInteger(numAllocators);
    final AtomicInteger numFailures = new AtomicInteger(0);
    final CountDownLatch latch = new CountDownLatch(1);
    AsyncCallback.StringCallback createCallback = new AsyncCallback.StringCallback() {
        @Override//from   w  ww  .j  av  a  2s . com
        public void processResult(int rc, String path, Object ctx, String name) {
            if (KeeperException.Code.OK.intValue() != rc) {
                numFailures.incrementAndGet();
                latch.countDown();
                return;
            }
            if (numPendings.decrementAndGet() == 0 && numFailures.get() == 0) {
                latch.countDown();
            }
        }
    };
    for (int i = 0; i < numAllocators; i++) {
        zkc.get().create(poolPath + "/A", new byte[0], zkc.getDefaultACL(), CreateMode.PERSISTENT_SEQUENTIAL,
                createCallback, null);
    }
    latch.await();
    if (numFailures.get() > 0) {
        throw new IOException("Failed to create " + numAllocators + " allocators.");
    }
}