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.proofpoint.zookeeper.RetryHandler.java

License:Apache License

boolean okToContinue(int rc) {
    KeeperException.Code code = KeeperException.Code.get(rc);
    if (shouldRetry(code)) {
        try {//  w w w . j av  a2  s .  co  m
            proc.call(client.getState().ensureCreated(), this);
        } catch (Exception e) {
            log.error(e, "for call %s", proc.getClass().getName());
        }
        return false;
    }
    return true;
}

From source file:com.proofpoint.zookeeper.RetryHandler.java

License:Apache License

private boolean shouldRetry(KeeperException.Code code) {
    if ((code == KeeperException.Code.CONNECTIONLOSS) || (code == KeeperException.Code.OPERATIONTIMEOUT)) {
        try {//from  w ww.ja va 2s .com
            //noinspection ThrowableResultOfMethodCallIgnored
            if (policy.shouldRetry(KeeperException.create(code), retries++)) {
                client.getState().close();
                return true;
            } else {
                log.info("Connection lost on retries for call %s", proc.getClass().getName());
                client.errorConnectionLost();
                client.getState().close();
            }
        } catch (Exception e) {
            log.error(e, "for call %s", proc.getClass().getName());
        }
    }
    return false;
}

From source file:com.proofpoint.zookeeper.ZookeeperClient.java

License:Apache License

@Override
public void delete(final String path) throws Exception {
    preconditionNotWatched();//from  w w  w .  ja v  a  2s .  c  om

    if (inBackground) {
        RetryHandler.Call<Void> backgroundCall = new RetryHandler.Call<Void>() {
            @Override
            public Void call(ZooKeeper client, final RetryHandler retryHandler) {
                client.delete(path, dataVersion, new AsyncCallback.VoidCallback() {
                    @Override
                    public void processResult(int rc, String path, Object ctx) {
                        if (retryHandler.okToContinue(rc)) {
                            eventQueue.postEvent(new ZookeeperEvent(ZookeeperEvent.Type.DELETE, rc, path, ctx,
                                    null, null, null, null, key));
                        }
                    }
                }, context);
                return null;
            }
        };
        RetryHandler.makeAndStart(this, retryPolicy, backgroundCall);
    } else {
        RetryHandler.Call<Void> backgroundCall = new RetryHandler.Call<Void>() {
            @Override
            public Void call(ZooKeeper client, RetryHandler<Void> voidRetryHandler) throws Exception {
                client.delete(path, dataVersion);

                // watchers aren't necessarily called on local deletes
                eventQueue.postEvent(new ZookeeperEvent(ZookeeperEvent.Type.DELETE,
                        KeeperException.Code.OK.intValue(), path, context, null, null, null, null, key));
                return null;
            }
        };
        RetryHandler.makeAndStart(this, retryPolicy, backgroundCall);
    }
}

From source file:com.proofpoint.zookeeper.ZookeeperEvent.java

License:Apache License

ZookeeperEvent(Type type, int rc, String path, Object context, byte[] data, Stat stat, String name,
        List<String> children, Object key) {
    this.type = type;
    this.key = key;
    resultCode = KeeperException.Code.get(rc);
    this.children = (children != null) ? new ArrayList<String>(Collections.unmodifiableList(children)) : null;
    this.path = path;
    this.context = context;
    this.data = data;
    this.stat = stat;
    this.name = name;
}

From source file:com.proofpoint.zookeeper.ZookeeperEvent.java

License:Apache License

/**
 * Returns the callback result code
 *
 * @return result code
 */
public KeeperException.Code getResultCode() {
    return resultCode;
}

From source file:com.rapleaf.hank.coordinator.zk.ZkHost.java

License:Apache License

@Override
public HostState getState() throws IOException {
    try {/*from  www.  j  av a 2 s  .  c o m*/
        if (zk.exists(hostPath + STATUS_PATH_SEGMENT, false) == null) {
            return HostState.OFFLINE;
        }
        try {
            return HostState.valueOf(zk.getString(hostPath + STATUS_PATH_SEGMENT));
        } catch (KeeperException e) {
            if (e.code() == Code.NONODE) {
                // the node disappeared between our exists check and our get. must be
                // offline now.
                return HostState.OFFLINE;
            }
            throw (e);
        }
    } catch (Exception e) {
        throw new IOException(e);
    }
}

From source file:com.rapleaf.hank.coordinator.zk.ZkHostConfig.java

License:Apache License

@Override
public HostState getState() throws IOException {
    try {/*from  w w  w.ja va  2s  . c o  m*/
        if (zk.exists(hostPath + STATUS_PATH_SEGMENT, false) == null) {
            return HostState.OFFLINE;
        }
        try {
            return HostState.valueOf(getString(hostPath + STATUS_PATH_SEGMENT));
        } catch (KeeperException e) {
            if (e.code() == Code.NONODE) {
                // the node disappeared between our exists check and our get. must be
                // offline now.
                return HostState.OFFLINE;
            }
            throw (e);
        }
    } catch (Exception e) {
        throw new IOException(e);
    }
}

From source file:com.renren.Wario.plugin.ObserverPlugin.java

License:Apache License

@Override
public void run() {
    if (client == null) {
        return;/*from  w  ww . java 2 s  .c o m*/
    }
    int stateVersion = getNextStateVersion();

    Stat stat = new Stat();
    byte[] data = null;
    try {
        data = client.getData("/", stat);
    } catch (KeeperException e) {
        logger.error("Exception when get root ", e);
        return;
    } catch (InterruptedException e) {
        logger.error("Thread interrupted", e);
    }
    PathAndStat root = new PathAndStat("/", data, stat);
    saveQueue.offer(root);
    client.getChildren("/", false, new ChildrenCallback() {

        @Override
        public void processResult(int rc, String path, Object ctx, List<String> children) {
            synchronized (inQueue) {
                inQueueProcess.incrementAndGet();
                inQueue.decrementAndGet();
                Code cd = KeeperException.Code.get(rc);
                switch (cd) {
                case OK:
                    break;
                default:
                    logger.warn("GetChildren on path " + path + " with " + cd.toString() + " event.");
                    return;
                }
                inTrack.incrementAndGet();
                client.getData(path, false, new DataCallback() {

                    @Override
                    public void processResult(int rc, String path, Object ctx, byte[] data, Stat stat) {
                        inTrack.decrementAndGet();
                        Code cd = Code.get(rc);
                        switch (cd) {
                        case OK:
                            break;
                        default:
                            logger.warn("GetData on path " + path + " with " + cd.toString() + " event.");
                            return;
                        }
                        PathAndStat node = new PathAndStat(path, data, stat);
                        inTrackProcess.incrementAndGet();
                        saveQueue.offer(node);
                    }
                }, null);
                Iterator<String> iterator = children.iterator();
                while (iterator.hasNext()) {
                    String child = iterator.next();
                    String son = (path.endsWith("/") ? path : path + "/") + child;
                    inQueue.incrementAndGet();
                    client.getChildren(son, false, this, null);
                }
            }
        }
    }, null);

    logger.info("Init finished " + (System.currentTimeMillis() - startTime) + " ms");

    while (true) {
        if (inQueue.intValue() == 0 && inTrack.intValue() == 0 && saveQueue.isEmpty()) {
            break;
        }
        PathAndStat node = null;
        try {
            node = saveQueue.poll(10, TimeUnit.MILLISECONDS);
        } catch (InterruptedException e) {
            logger.warn("Thread interrupted.", e);
            return;
        }

        if (node == null) {
            continue;
        }
        inDBWriterProcess.incrementAndGet();
        try {
            writeToDB(node, stateVersion);
        } catch (SQLException e) {
            logger.error(node.toString());
            logger.error("Execute sql failed!", e);
        }
    }

    try {
        updatePs.close();
        insertPs.close();
        helper.close();
    } catch (SQLException e) {
        logger.error("MysqlHelper close failed! ", e);
    }

    logger.info("Run main thread " + (System.currentTimeMillis() - startTime) + " ms");
    logger.info("INQUEUE : " + inQueueProcess.intValue() + "\nINTRACK : " + inTrackProcess.intValue()
            + "\nINDBWRITER : " + inDBWriterProcess.intValue());
}

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);
    }/*from   w w  w .j  a v  a  2  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.splicemachine.ddl.ZooKeeperDDLWatchChecker.java

License:Apache License

@Override
public Collection<String> getCurrentChangeIds() throws IOException {
    try {/*from   w  w w.  ja v  a2  s.c o  m*/
        return ZkUtils.getRecoverableZooKeeper().getChildren(zkClient.changePath, changeIdWatcher);
    } catch (KeeperException ke) {
        if (ke.code().equals(KeeperException.Code.SESSIONEXPIRED)) {
            LOG.error("ZooKeeper Session expired, terminating early");
            return null;
        }
        throw new IOException(ke);
    } catch (InterruptedException e) {
        throw Exceptions.getIOException(e);
    }
}