org.apache.zookeeper.AsyncCallback.StringCallback Java Examples

The following examples show how to use org.apache.zookeeper.AsyncCallback.StringCallback. 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: TestApi.java    From jframe with Apache License 2.0 6 votes vote down vote up
public void testCreate() {
    class CreateCallback implements StringCallback {
        @Override
        public void processResult(int rc, String path, Object ctx, String name) {
            LOG.info("code->{} path->{}", rc, path);
            switch (Code.get(rc)) {
            case CONNECTIONLOSS:
                // TODO re-create
                break;
            case OK:
                break;
            case NODEEXISTS:
                break;
            default:
                LOG.error("error code->{} path->{}", rc, path);
            }
        }

    }

    if (zk != null)
        zk.create("/test", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT, new CreateCallback(), null);
}
 
Example #2
Source File: BookKeeperJournalManager.java    From hadoop with Apache License 2.0 4 votes vote down vote up
/**
 * Pre-creating bookkeeper metadata path in zookeeper.
 */
private void prepareBookKeeperEnv() throws IOException {
  // create bookie available path in zookeeper if it doesn't exists
  final String zkAvailablePath = conf.get(BKJM_ZK_LEDGERS_AVAILABLE_PATH,
      BKJM_ZK_LEDGERS_AVAILABLE_PATH_DEFAULT);
  final CountDownLatch zkPathLatch = new CountDownLatch(1);

  final AtomicBoolean success = new AtomicBoolean(false);
  StringCallback callback = new StringCallback() {
    @Override
    public void processResult(int rc, String path, Object ctx, String name) {
      if (KeeperException.Code.OK.intValue() == rc
          || KeeperException.Code.NODEEXISTS.intValue() == rc) {
        LOG.info("Successfully created bookie available path : "
            + zkAvailablePath);
        success.set(true);
      } else {
        KeeperException.Code code = KeeperException.Code.get(rc);
        LOG.error("Error : "
                + KeeperException.create(code, path).getMessage()
                + ", failed to create bookie available path : "
                + zkAvailablePath);
      }
      zkPathLatch.countDown();
    }
  };
  ZkUtils.asyncCreateFullPathOptimistic(zkc, zkAvailablePath, new byte[0],
      Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT, callback, null);

  try {
    if (!zkPathLatch.await(zkc.getSessionTimeout(), TimeUnit.MILLISECONDS)
        || !success.get()) {
      throw new IOException("Couldn't create bookie available path :"
          + zkAvailablePath + ", timed out " + zkc.getSessionTimeout()
          + " millis");
    }
  } catch (InterruptedException e) {
    Thread.currentThread().interrupt();
    throw new IOException(
        "Interrupted when creating the bookie available path : "
            + zkAvailablePath, e);
  }
}
 
Example #3
Source File: BookKeeperJournalManager.java    From big-c with Apache License 2.0 4 votes vote down vote up
/**
 * Pre-creating bookkeeper metadata path in zookeeper.
 */
private void prepareBookKeeperEnv() throws IOException {
  // create bookie available path in zookeeper if it doesn't exists
  final String zkAvailablePath = conf.get(BKJM_ZK_LEDGERS_AVAILABLE_PATH,
      BKJM_ZK_LEDGERS_AVAILABLE_PATH_DEFAULT);
  final CountDownLatch zkPathLatch = new CountDownLatch(1);

  final AtomicBoolean success = new AtomicBoolean(false);
  StringCallback callback = new StringCallback() {
    @Override
    public void processResult(int rc, String path, Object ctx, String name) {
      if (KeeperException.Code.OK.intValue() == rc
          || KeeperException.Code.NODEEXISTS.intValue() == rc) {
        LOG.info("Successfully created bookie available path : "
            + zkAvailablePath);
        success.set(true);
      } else {
        KeeperException.Code code = KeeperException.Code.get(rc);
        LOG.error("Error : "
                + KeeperException.create(code, path).getMessage()
                + ", failed to create bookie available path : "
                + zkAvailablePath);
      }
      zkPathLatch.countDown();
    }
  };
  ZkUtils.asyncCreateFullPathOptimistic(zkc, zkAvailablePath, new byte[0],
      Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT, callback, null);

  try {
    if (!zkPathLatch.await(zkc.getSessionTimeout(), TimeUnit.MILLISECONDS)
        || !success.get()) {
      throw new IOException("Couldn't create bookie available path :"
          + zkAvailablePath + ", timed out " + zkc.getSessionTimeout()
          + " millis");
    }
  } catch (InterruptedException e) {
    Thread.currentThread().interrupt();
    throw new IOException(
        "Interrupted when creating the bookie available path : "
            + zkAvailablePath, e);
  }
}
 
Example #4
Source File: MockZooKeeper.java    From pulsar with Apache License 2.0 4 votes vote down vote up
@Override
public void create(final String path, final byte[] data, final List<ACL> acl, CreateMode createMode,
        final StringCallback cb, final Object ctx) {


    executor.execute(() -> {
        mutex.lock();

        if (stopped) {
            cb.processResult(KeeperException.Code.CONNECTIONLOSS.intValue(), path, ctx, null);
            return;
        }

        final Set<Watcher> toNotifyCreate = Sets.newHashSet();
        toNotifyCreate.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));
        }

        Optional<KeeperException.Code> failure = programmedFailure(Op.CREATE, path);
        if (failure.isPresent()) {
            mutex.unlock();
            cb.processResult(failure.get().intValue(), path, ctx, null);
        } else if (stopped) {
            mutex.unlock();
            cb.processResult(KeeperException.Code.CONNECTIONLOSS.intValue(), path, ctx, null);
        } else if (tree.containsKey(path)) {
            mutex.unlock();
            cb.processResult(KeeperException.Code.NODEEXISTS.intValue(), path, ctx, null);
        } else if (!parent.isEmpty() && !tree.containsKey(parent)) {
            mutex.unlock();
            cb.processResult(KeeperException.Code.NONODE.intValue(), path, ctx, null);
        } else {
            tree.put(path, Pair.of(data, 0));
            watchers.removeAll(path);
            mutex.unlock();
            cb.processResult(0, path, ctx, null);

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

}