org.apache.zookeeper.AsyncCallback.VoidCallback Java Examples

The following examples show how to use org.apache.zookeeper.AsyncCallback.VoidCallback. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MockZooKeeper.java    From pulsar with Apache License 2.0 6 votes vote down vote up
@Override
public void sync(String path, VoidCallback cb, Object ctx) {
    executor.execute(() -> {
        Optional<KeeperException.Code> failure = programmedFailure(Op.SYNC, path);
        if (failure.isPresent()) {
            cb.processResult(failure.get().intValue(), path, ctx);
            return;
        } else if (stopped) {
            cb.processResult(KeeperException.Code.ConnectionLoss, path, ctx);
            return;
        }

        cb.processResult(0, path, ctx);
    });

}
 
Example #2
Source File: MockZooKeeper.java    From pulsar with Apache License 2.0 4 votes vote down vote up
@Override
public void delete(final String path, int version, final VoidCallback cb, final Object ctx) {
    Runnable r = () -> {
        mutex.lock();
        final Set<Watcher> toNotifyDelete = Sets.newHashSet();
        toNotifyDelete.addAll(watchers.get(path));

        final Set<Watcher> toNotifyParent = Sets.newHashSet();
        final String parent = path.substring(0, path.lastIndexOf("/"));
        if (!parent.isEmpty()) {
            toNotifyParent.addAll(watchers.get(parent));
        }
        watchers.removeAll(path);

        Optional<KeeperException.Code> failure = programmedFailure(Op.DELETE, path);
        if (failure.isPresent()) {
            mutex.unlock();
            cb.processResult(failure.get().intValue(), path, ctx);
        } else if (stopped) {
            mutex.unlock();
            cb.processResult(KeeperException.Code.CONNECTIONLOSS.intValue(), path, ctx);
        } else if (!tree.containsKey(path)) {
            mutex.unlock();
            cb.processResult(KeeperException.Code.NONODE.intValue(), path, ctx);
        } else if (hasChildren(path)) {
            mutex.unlock();
            cb.processResult(KeeperException.Code.NOTEMPTY.intValue(), path, ctx);
        } else {
            if (version != -1) {
                int currentVersion = tree.get(path).getRight();
                if (version != currentVersion) {
                    mutex.unlock();
                    cb.processResult(KeeperException.Code.BADVERSION.intValue(), path, ctx);
                    return;
                }
            }

            tree.remove(path);

            mutex.unlock();
            cb.processResult(0, path, ctx);

            toNotifyDelete.forEach(watcher -> watcher
                    .process(new WatchedEvent(EventType.NodeDeleted, KeeperState.SyncConnected, path)));
            toNotifyParent.forEach(watcher -> watcher
                    .process(new WatchedEvent(EventType.NodeChildrenChanged, KeeperState.SyncConnected, parent)));
        }
    };

    try {
        executor.execute(r);
    } catch (RejectedExecutionException ree) {
        cb.processResult(KeeperException.Code.SESSIONEXPIRED.intValue(), path, ctx);
        return;
    }

}