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.netflix.curator.framework.imps.CuratorTransactionImpl.java

License:Apache License

private List<OpResult> doOperation(AtomicBoolean firstTime) throws Exception {
    boolean localFirstTime = firstTime.getAndSet(false);
    if (!localFirstTime) {

    }/*  w  ww .  j a va 2s .  co  m*/

    List<OpResult> opResults = client.getZooKeeper().multi(transaction);
    if (opResults.size() > 0) {
        OpResult firstResult = opResults.get(0);
        if (firstResult.getType() == ZooDefs.OpCode.error) {
            OpResult.ErrorResult error = (OpResult.ErrorResult) firstResult;
            KeeperException.Code code = KeeperException.Code.get(error.getErr());
            if (code == null) {
                code = KeeperException.Code.UNIMPLEMENTED;
            }
            throw KeeperException.create(code);
        }
    }
    return opResults;
}

From source file:com.netflix.curator.framework.imps.GetDataBuilderImpl.java

License:Apache License

@Override
public void performBackgroundOperation(final OperationAndData<String> operationAndData) throws Exception {
    final TimeTrace trace = client.getZookeeperClient().startTracer("GetDataBuilderImpl-Background");
    AsyncCallback.DataCallback callback = new AsyncCallback.DataCallback() {
        @Override/*from   ww w  .  ja v  a2  s .  co m*/
        public void processResult(int rc, String path, Object ctx, byte[] data, Stat stat) {
            trace.commit();
            if (decompress && (data != null)) {
                try {
                    data = client.getCompressionProvider().decompress(path, data);
                } catch (Exception e) {
                    log.error("Decompressing for path: " + path, e);
                    rc = KeeperException.Code.DATAINCONSISTENCY.intValue();
                }
            }
            CuratorEvent event = new CuratorEventImpl(client, CuratorEventType.GET_DATA, rc, path, null, ctx,
                    stat, data, null, null, null);
            client.processBackgroundOperation(operationAndData, event);
        }
    };
    if (watching.isWatched()) {
        client.getZooKeeper().getData(operationAndData.getData(), true, callback, backgrounding.getContext());
    } else {
        client.getZooKeeper().getData(operationAndData.getData(), watching.getWatcher(), callback,
                backgrounding.getContext());
    }
}

From source file:com.netflix.curator.framework.recipes.cache.NodeCache.java

License:Apache License

private void processBackgroundResult(CuratorEvent event) throws Exception {
    switch (event.getType()) {
    case GET_DATA: {
        if (event.getResultCode() == KeeperException.Code.OK.intValue()) {
            ChildData childData = new ChildData(path, event.getStat(), event.getData());
            setNewData(childData);//from w ww  . j a va 2  s  . c  o m
        }
        break;
    }

    case EXISTS: {
        if (event.getResultCode() == KeeperException.Code.NONODE.intValue()) {
            setNewData(null);
        } else if (event.getResultCode() == KeeperException.Code.OK.intValue()) {
            if (dataIsCompressed) {
                client.getData().decompressed().usingWatcher(watcher).inBackground(backgroundCallback)
                        .forPath(path);
            } else {
                client.getData().usingWatcher(watcher).inBackground(backgroundCallback).forPath(path);
            }
        }
        break;
    }
    }
}

From source file:com.netflix.curator.framework.recipes.cache.PathChildrenCache.java

License:Apache License

private void applyNewData(String fullPath, int resultCode, Stat stat, byte[] bytes) {
    if (resultCode == KeeperException.Code.OK.intValue()) // otherwise - node must have dropped or something - we should be getting another event
    {//from  www.ja va  2  s . co m
        ChildData data = new ChildData(fullPath, stat, bytes);
        ChildData previousData = currentData.put(fullPath, data);
        if (previousData == null) // i.e. new
        {
            listenerEvents.offer(new PathChildrenCacheEvent(PathChildrenCacheEvent.Type.CHILD_ADDED, data));
        } else if (previousData.getStat().getVersion() != stat.getVersion()) {
            listenerEvents.offer(new PathChildrenCacheEvent(PathChildrenCacheEvent.Type.CHILD_UPDATED, data));
        }
    }
}

From source file:com.netflix.curator.RetryLoop.java

License:Apache License

/**
 * Utility - return true if the given exception is retry-able
 *
 * @param exception exception to check/*from   ww w .j  a v a  2 s.  c o  m*/
 * @return true/false
 */
public static boolean isRetryException(Throwable exception) {
    if (exception instanceof KeeperException) {
        KeeperException keeperException = (KeeperException) exception;
        return shouldRetry(keeperException.code().intValue());
    }
    return false;
}

From source file:com.netflix.curator.RetryLoop.java

License:Apache License

/**
 * Utility - return true if the given Zookeeper result code is retry-able
 *
 * @param rc result code//from   w  w w  . j a  v  a 2  s.  co m
 * @return true/false
 */
public static boolean shouldRetry(int rc) {
    return (rc == KeeperException.Code.CONNECTIONLOSS.intValue())
            || (rc == KeeperException.Code.OPERATIONTIMEOUT.intValue())
            || (rc == KeeperException.Code.SESSIONMOVED.intValue())
            || (rc == KeeperException.Code.SESSIONEXPIRED.intValue());
}

From source file:com.objectdriven.maven.zookeeper.sample.DataMonitor.java

public void process(WatchedEvent event) {
    String path = event.getPath();
    if (event.getType() == Event.EventType.None) {
        // We are are being told that the state of the
        // connection has changed
        switch (event.getState()) {
        case SyncConnected:
            // In this particular example we don't need to do anything
            // here - watches are automatically re-registered with
            // server and any watches triggered while the client was
            // disconnected will be delivered (in order of course)
            break;
        case Expired:
            // It's all over
            dead = true;//from w  w  w . j  a  v a 2s  .  c  o  m
            listener.closing(KeeperException.Code.SessionExpired);
            break;
        }
    } else {
        if (path != null && path.equals(znode)) {
            // Something has changed on the node, let's find out
            zk.exists(znode, true, this, null);
        }
    }
    if (chainedWatcher != null) {
        chainedWatcher.process(event);
    }
}

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

License:Apache License

private void processGetData(ZookeeperEvent event) throws Exception {
    log.debug("ChildDataCallback: [rc = %d] %s, %s", event.getResultCode().intValue(), path, event.getStat());
    if (isStarted.get() && event.getResultCode() == KeeperException.Code.OK) { // check for case where the child disappeared since we got the childrenChanged notification

        String child = event.getContext().toString();
        Stat currentStat;/*from   w w w  .  j a  v  a 2 s .c o  m*/

        synchronized (elements) {
            currentStat = elements.get(child);
            elements.put(child, event.getStat());
        }

        if (currentStat == SENTINEL) {
            notifyAdded(child, event.getData());
        } else {
            notifyUpdated(child, event.getData(), event.getStat().getVersion());
        }
    }
}

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

License:Apache License

private void processGetChildren(ZookeeperEvent event) throws Exception {
    log.debug("ChildrenListCallBack: [rc = %d], %s, %s", event.getResultCode().intValue(), event.getPath(),
            event.getChildren());/*  w  w w  .  ja v  a2  s  . com*/
    if (isStarted.get() && event.getResultCode() == KeeperException.Code.OK) {
        synchronized (elements) {
            Set<String> added = Sets
                    .newHashSet(difference(Sets.newHashSet(event.getChildren()), elements.keySet()));
            Set<String> removed = Sets
                    .newHashSet(difference(elements.keySet(), Sets.newHashSet(event.getChildren())));

            for (String child : added) {
                String fullPath = path + "/" + child;
                elements.put(child, SENTINEL); // mark this child as tentative -- notifications will be sent once we get the data

                log.debug("Getting data: %s", fullPath);
                client.usingWatcher(this).inBackground(backgroundKey).withContext(child).getData(fullPath);
            }

            // removed
            for (String child : removed) {
                // don't notify if the node was removed before we got a chance to get the data
                // For all intents and purposes, the node never existed
                if (elements.remove(child) != SENTINEL) {
                    notifyRemoved(child);
                }
            }
        }
    }
}

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

License:Apache License

T start() throws Exception {
    if (client.getState().isZombie()) {
        return null;
    }//  w w w . j av a  2  s .  c  o  m

    while (!client.getState().ensureConnected()) {
        if (!shouldRetry(KeeperException.Code.CONNECTIONLOSS)) {
            throw new KeeperException.ConnectionLossException();
        }
    }

    for (;;) {
        try {
            return proc.call(client.getState().ensureCreated(), this);
        } catch (KeeperException e) {
            if (!shouldRetry(e.code())) {
                throw e;
            }
        }
    }
}