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.linkedin.d2.balancer.zkfs.ZKFSDirectory.java

License:Apache License

@Override
public void getServiceNames(final Callback<List<String>> callback) {
    final ZooKeeper zk = _connection.getZooKeeper();
    final String path = ZKFSUtil.servicePath(_basePath);
    zk.getChildren(path, false, new AsyncCallback.Children2Callback() {
        @Override/* w  ww . j  a v  a 2  s.  co  m*/
        public void processResult(int rc, String path, Object ctx, List<String> children, Stat stat) {
            KeeperException.Code code = KeeperException.Code.get(rc);
            switch (code) {
            case OK:
                callback.onSuccess(children);
                break;
            case NONODE:
                callback.onSuccess(Collections.<String>emptyList());
                break;
            default:
                callback.onError(KeeperException.create(code));
                break;
            }
        }
    }, null);
}

From source file:com.linkedin.d2.balancer.zkfs.ZKFSDirectory.java

License:Apache License

@Override
public void getClusterNames(final Callback<List<String>> callback) {
    final ZooKeeper zk = _connection.getZooKeeper();
    final String path = ZKFSUtil.clusterPath(_basePath);
    zk.getChildren(path, false, new AsyncCallback.Children2Callback() {
        @Override/*from w  w  w .  j  a v a2  s .  c  om*/
        public void processResult(int rc, String path, Object ctx, List<String> children, Stat stat) {
            KeeperException.Code code = KeeperException.Code.get(rc);
            switch (code) {
            case OK:
                callback.onSuccess(children);
                break;
            case NONODE:
                callback.onSuccess(Collections.<String>emptyList());
                break;
            default:
                callback.onError(KeeperException.create(code));
                break;
            }
        }
    }, null);

}

From source file:com.linkedin.d2.discovery.stores.zk.DeltaWriteZooKeeperPermanentStore.java

License:Apache License

/**
 * Returns callback of ensurePersistentNodeExists used in ZookeeperPermanentStore's put.
 * The callback would first try getting existing node's data. If it does not exist or is
 * different from the new value, it is overwritten. Otherwise nothing happens.
 *
 * @param listenTo/*w  ww.  j a  v a2 s .  c om*/
 * @param discoveryProperties
 * @param callback
 */
@Override
protected Callback<None> getExistsCallBack(final String listenTo, final T discoveryProperties,
        final Callback<None> callback) {
    final String path = getPath(listenTo);

    final AsyncCallback.StatCallback dataCallback = new AsyncCallback.StatCallback() {
        @Override
        public void processResult(int rc, String path, Object ctx, Stat stat) {
            KeeperException.Code code = KeeperException.Code.get(rc);
            switch (code) {
            case OK:
                callback.onSuccess(None.none());
                break;
            default:
                callback.onError(KeeperException.create(code));
                break;
            }
        }
    };

    final AsyncCallback.DataCallback getDataCallback = new AsyncCallback.DataCallback() {
        @Override
        public void processResult(int rc, String path, Object context, byte[] bytes, Stat stat) {
            _log.debug("Data callback got rc {} for path {}", rc, path);
            KeeperException.Code result = KeeperException.Code.get(rc);

            switch (result) {
            case OK:
                // Get property currently in store.
                T propertiesInStore = null;
                if (bytes != null) {
                    try {
                        propertiesInStore = _serializer.fromBytes(bytes);
                    } catch (PropertySerializationException e) {
                        _log.warn("Unable to de-serialize properties for {}, overwriting", path, e);
                    }
                }

                // Compare with new property and only call setData if it is different.
                if (propertiesInStore == null || !propertiesInStore.equals(discoveryProperties)) {
                    _log.debug("Updating value for {}", path);
                    _zk.setData(path, _serializer.toBytes(discoveryProperties), -1, dataCallback, null);
                } else {
                    _log.debug("Node is up to date, skipping write for {}", path);
                    callback.onSuccess(None.none());
                }

                break;
            default:
                callback.onError(KeeperException.create(result));
                break;
            }
        }

    };

    return new Callback<None>() {
        @Override
        public void onSuccess(None none) {
            _zk.getData(path, false, getDataCallback, null);
        }

        @Override
        public void onError(Throwable e) {
            _log.debug("Exist : failed for path {}", path);
            callback.onError(e);
        }
    };
}

From source file:com.linkedin.d2.discovery.stores.zk.RetryZooKeeper.java

License:Apache License

public String createUniqueSequential(final String path, final byte[] data, final List<ACL> acl,
        final CreateMode createMode, final AsyncCallback.StringCallback cb, final Object ctx) {
    if (!createMode.isSequential()) {
        create(path, data, acl, createMode, cb, ctx);
        return path;
    }//from   w  ww  .j  a  va2  s .c  om

    final String retryPath = path + "-" + _uuid.toString() + "-";
    final RetryCallback callback = new RetryCallback() {
        @Override
        protected void retry() {
            final String parentPath = path.substring(0, path.lastIndexOf('/'));
            final StringCallback stringCallback = this;

            final ChildrenCallback childrenCallback = new ChildrenCallback() {
                @Override
                public void processResult(int ccRC, String ccPath, Object ccCtx, List<String> ccChildren) {
                    KeeperException.Code code = KeeperException.Code.get(ccRC);
                    // we don't have to handle CONNECTIONLOSS here; it would be handled by the retry version of getChildren
                    switch (code) {
                    case OK:
                        List<String> ourChildren = new ArrayList<String>();
                        for (final String child : ccChildren) {
                            if (child.contains(_uuid.toString())) {
                                ourChildren.add(child);
                            }
                        }
                        if (ourChildren.size() > 0) {
                            ChildrenInspector inspector = new ChildrenInspector(ourChildren.size());
                            for (final String ourChild : ourChildren) {
                                // user retry version of getData here
                                getData(parentPath + "/" + ourChild, false, inspector, null);
                            }
                        } else {
                            // no children belong to us found, retry create directly
                            _log.info("Retry create operation: path = " + retryPath + "data length: "
                                    + data.length);
                            zkCreate(retryPath, data, acl, createMode, stringCallback, ctx);
                        }
                        break;
                    default:
                        _log.error("Retry create aborted in getChildren. KeeperException code: " + code);
                        break;
                    }
                }

                class ChildrenInspector implements DataCallback {
                    private int _count;

                    ChildrenInspector(int count) {
                        _count = count;
                    }

                    @Override
                    public void processResult(int dcRC, String dcPath, Object dcCtx, byte[] dcData,
                            Stat dcStat) {
                        KeeperException.Code code = KeeperException.Code.get(dcRC);
                        // we don't have to handle CONNECTIONLOSS here
                        switch (code) {
                        case OK:
                            if (Arrays.equals(data, dcData)) {
                                // we find the data we wanted to create
                                // do not decrement _count
                                // retry create won't be triggered as a result
                            } else {
                                // this is not the data we wanted to create
                                _count--;
                                if (_count == 0) {
                                    // this is the last child to be inspected
                                    // all previous children do not have the data we wanted to create
                                    // trigger retry create
                                    _log.info("Retry create operation: path = " + retryPath + "data length: "
                                            + data.length);
                                    zkCreate(retryPath, data, acl, createMode, stringCallback, ctx);
                                }
                            }
                            break;
                        default:
                            _log.error("Retry create stopped in getData. KeeperException code: " + code);
                            break;
                        }
                    }
                }
            };
            // use retry version of getChildren
            getChildren(parentPath, false, childrenCallback, null);
        }

        @Override
        protected void processStringResult(int cbRC, String cbPath, Object cbCtx, String cbName) {
            cb.processResult(cbRC, cbPath, cbCtx, cbName);
        }
    };

    zkCreate(retryPath, data, acl, createMode, callback, ctx);
    return retryPath;
}

From source file:com.linkedin.d2.discovery.stores.zk.RetryZooKeeperTest.java

License:Apache License

@SuppressWarnings("unchecked")
@Test/*ww  w. ja  v  a2 s .c om*/
public void testDelete() throws NoSuchMethodException {
    final RetryZooKeeper rzkPartialMock = createMockObject(RetryZooKeeper.class.getMethod("zkDelete",
            String.class, int.class, AsyncCallback.VoidCallback.class, Object.class));

    // mock up zkDelete, which wrapper's ZooKeeper's delete
    rzkPartialMock.zkDelete((String) EasyMock.anyObject(), EasyMock.anyInt(),
            (AsyncCallback.VoidCallback) EasyMock.anyObject(), EasyMock.anyObject());

    // first try, "connection loss"
    expectDeleteCallbackWithCode(_connectionLossRC);
    // second try, "no node"
    expectDeleteCallbackWithCode(KeeperException.Code.NONODE.intValue());
    EasyMock.replay(rzkPartialMock);
    rzkPartialMock.delete(_dummyPath, _dummyVersion, _dummyVoidCallback, _dummyCtx);
    EasyMock.verify(rzkPartialMock);
}

From source file:com.linkedin.d2.discovery.stores.zk.SymlinkAwareZooKeeper.java

License:Apache License

private void getData0(final String path, final SymlinkWatcher watcher, final AsyncCallback.DataCallback cb,
        final Object ctx) {
    int index = SymlinkUtil.firstSymlinkIndex(path);
    if (index < 0) {
        _zk.getData(path, watcher, cb, ctx);
    } else {/* ww w.jav  a  2  s.  com*/
        String symlink = path.substring(0, index);
        final String remainPath = path.substring(index);
        // TODO: instead of resolving the symlink everytime it is requested, we probably can cache
        // the resolve results and rely on the watch event associated with the symlink to invalidate the cache.
        AsyncCallback.DataCallback resolveCallback = new AsyncCallback.DataCallback() {
            @Override
            public void processResult(int rc, String path, Object ctx, byte data[], Stat stat) {
                KeeperException.Code result = KeeperException.Code.get(rc);
                switch (result) {
                case OK:
                    try {
                        String realPath = _serializer.fromBytes(data);
                        getData0(realPath + remainPath, watcher, cb, ctx);
                    } catch (Exception e) {
                        if (watcher != null)
                            watcher.disable();
                        cb.processResult(KeeperException.Code.NONODE.intValue(), path, ctx, null, null);
                        LOG.warn("Exception when resolving symlink: " + path, e);
                    }
                    break;

                default:
                    if (watcher != null)
                        watcher.disable();
                    cb.processResult(rc, path, ctx, data, stat);
                    break;
                }
            }
        };
        _zk.getData(symlink, watcher, resolveCallback, ctx);
    }
}

From source file:com.linkedin.d2.discovery.stores.zk.SymlinkAwareZooKeeper.java

License:Apache License

private void exists0(final String path, final SymlinkWatcher watcher, final AsyncCallback.StatCallback cb,
        final Object ctx) {
    int index = SymlinkUtil.firstSymlinkIndex(path);
    if (index < 0) {
        _zk.exists(path, watcher, cb, ctx);
    } else {//from w w  w  .j  av  a  2 s  .c o  m
        String symlink = path.substring(0, index);
        final String remainPath = path.substring(index);
        AsyncCallback.DataCallback resolveCallback = new AsyncCallback.DataCallback() {
            @Override
            public void processResult(int rc, String p, Object c, byte data[], Stat s) {
                KeeperException.Code result = KeeperException.Code.get(rc);
                switch (result) {
                case OK:
                    try {
                        String realPath = _serializer.fromBytes(data);
                        exists0(realPath + remainPath, watcher, cb, ctx);
                    } catch (Exception e) {
                        // we don't want to disable watch here because NONODE is not an exception
                        // in exists() call, so the watch is still valid.
                        cb.processResult(KeeperException.Code.NONODE.intValue(), path, ctx, null);
                        LOG.warn("Exception when resolving symlink: " + path, e);
                    }
                    break;

                case NONODE:
                    // the intermediate symlink znode doesn't exist. we should attach an existsWatcher to this znode,
                    // otherwise we won't get notified once it is back later.
                    //
                    // Note that we don't have to do this for getData0() and getChildren0() because NONODE is considered
                    // as exception for these two operations and by definition no watcher should be attached if an exception
                    // is thrown.
                    AsyncCallback.StatCallback existsCallback = new AsyncCallback.StatCallback() {
                        @Override
                        public void processResult(int rc, String p, Object c, Stat s) {
                            KeeperException.Code code = KeeperException.Code.get(rc);
                            switch (code) {
                            case OK:
                                // the symlink znode is back, resume the original call.
                                exists0(path, watcher, cb, ctx);
                                break;

                            case NONODE:
                                // the existsWatcher has been attached. we just need to invoke the user callback in this case.
                                cb.processResult(rc, path, ctx, s);
                                break;

                            default:
                                // the call failed with some other reasons. disable the watcher and invoke the user callback.
                                if (watcher != null)
                                    watcher.disable();
                                cb.processResult(rc, path, ctx, s);
                                break;
                            }
                        }
                    };
                    _zk.exists(p, watcher, existsCallback, ctx);
                    break;

                default:
                    if (watcher != null)
                        watcher.disable();
                    cb.processResult(rc, path, ctx, s);
                    break;
                }
            }
        };
        _zk.getData(symlink, watcher, resolveCallback, ctx);
    }
}

From source file:com.linkedin.d2.discovery.stores.zk.SymlinkAwareZooKeeper.java

License:Apache License

private void getChildren0(final String path, final SymlinkWatcher watcher,
        final AsyncCallback.ChildrenCallback cb, final Object ctx) {
    int index = SymlinkUtil.firstSymlinkIndex(path);
    if (index < 0) {
        _zk.getChildren(path, watcher, cb, ctx);
    } else {//w ww.  j a v a  2s .  c  o  m
        String symlink = path.substring(0, index);
        final String remainPath = path.substring(index);
        AsyncCallback.DataCallback resolveCallback = new AsyncCallback.DataCallback() {
            @Override
            public void processResult(int rc, String path, Object ctx, byte data[], Stat stat) {
                KeeperException.Code result = KeeperException.Code.get(rc);
                switch (result) {
                case OK:
                    try {
                        String realPath = _serializer.fromBytes(data);
                        getChildren0(realPath + remainPath, watcher, cb, ctx);
                    } catch (Exception e) {
                        if (watcher != null)
                            watcher.disable();
                        cb.processResult(KeeperException.Code.NONODE.intValue(), path, ctx,
                                Collections.<String>emptyList());
                        LOG.warn("Exception when resolving symlink: " + path, e);
                    }
                    break;

                default:
                    if (watcher != null)
                        watcher.disable();
                    cb.processResult(rc, path, ctx, Collections.<String>emptyList());
                    break;
                }
            }
        };
        _zk.getData(symlink, watcher, resolveCallback, ctx);
    }
}

From source file:com.linkedin.d2.discovery.stores.zk.SymlinkAwareZooKeeper.java

License:Apache License

private void getChildren2(final String path, final SymlinkWatcher watcher,
        final AsyncCallback.Children2Callback cb, final Object ctx) {
    int index = SymlinkUtil.firstSymlinkIndex(path);
    if (index < 0) {
        _zk.getChildren(path, watcher, cb, ctx);
    } else {/*from   w  w w  .j  a v  a2s.  c o m*/
        String symlink = path.substring(0, index);
        final String remainPath = path.substring(index);
        AsyncCallback.DataCallback resolveCallback = new AsyncCallback.DataCallback() {
            @Override
            public void processResult(int rc, String path, Object ctx, byte data[], Stat stat) {
                KeeperException.Code result = KeeperException.Code.get(rc);
                switch (result) {
                case OK:
                    try {
                        String realPath = _serializer.fromBytes(data);
                        getChildren2(realPath + remainPath, watcher, cb, ctx);
                    } catch (Exception e) {
                        if (watcher != null)
                            watcher.disable();
                        cb.processResult(KeeperException.Code.NONODE.intValue(), path, ctx,
                                Collections.<String>emptyList(), null);
                        LOG.warn("Exception when resolving symlink: " + path, e);
                    }
                    break;

                default:
                    if (watcher != null)
                        watcher.disable();
                    cb.processResult(rc, path, ctx, Collections.<String>emptyList(), null);
                    break;
                }
            }
        };
        _zk.getData(symlink, watcher, resolveCallback, ctx);
    }
}

From source file:com.linkedin.d2.discovery.stores.zk.SymlinkAwareZooKeeperTest.java

License:Apache License

@Test
public void testSymlinkExists() throws InterruptedException {
    final CountDownLatch latch = new CountDownLatch(1);
    AsyncCallback.StatCallback callback = new AsyncCallback.StatCallback() {
        @Override//from  w w w  . ja  v a2 s .  c o m
        public void processResult(int rc, String path, Object ctx, Stat stat) {
            KeeperException.Code result = KeeperException.Code.get(rc);
            Assert.assertEquals(result, KeeperException.Code.OK);
            Assert.assertEquals(path, "/foo/$link");
            Assert.assertNotNull(stat);
            latch.countDown();
        }
    };
    _zkClient.getZooKeeper().exists("/foo/$link", null, callback, null);
    latch.await(30, TimeUnit.SECONDS);
}