org.apache.zookeeper.ZooKeeper.exists()方法的使用及代码示例

x33g5p2x  于2022-02-05 转载在 其他  
字(8.5k)|赞(0)|评价(0)|浏览(318)

本文整理了Java中org.apache.zookeeper.ZooKeeper.exists()方法的一些代码示例,展示了ZooKeeper.exists()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ZooKeeper.exists()方法的具体详情如下:
包路径:org.apache.zookeeper.ZooKeeper
类名称:ZooKeeper
方法名:exists

ZooKeeper.exists介绍

[英]Return the stat of the node of the given path. Return null if no such a node exists.

If the watch is non-null and the call is successful (no exception is thrown), a watch will be left on the node with the given path. The watch will be triggered by a successful operation that creates/delete the node or sets the data on the node.
[中]返回给定路径的节点的状态。如果不存在这样的节点,则返回null。
如果watch为非null且调用成功(未引发异常),则将在节点上保留一个具有给定路径的watch。创建/删除节点或在节点上设置数据的成功操作将触发监视。

代码示例

代码示例来源:origin: apache/zookeeper

/**
 * The asynchronous version of exists.
 *
 * @see #exists(String, boolean)
 */
public void exists(String path, boolean watch, StatCallback cb, Object ctx) {
  exists(path, watch ? watchManager.defaultWatcher : null, cb, ctx);
}

代码示例来源:origin: apache/zookeeper

public boolean execute() throws KeeperException, InterruptedException {
    Stat stat = zookeeper.exists(path, false);
    if (stat != null) {
      return true;
    }
    zookeeper.create(path, data, acl, flags);
    return true;
  }
});

代码示例来源:origin: knightliao/disconf

public boolean execute() throws KeeperException, InterruptedException {
    Stat stat = zookeeper.exists(path, false);
    if (stat != null) {
      return true;
    }
    zookeeper.create(path, data, acl, flags);
    return true;
  }
});

代码示例来源:origin: apache/zookeeper

private void utestExists(int port)
  throws IOException, InterruptedException, KeeperException
{
  ZooKeeper zk =
    new ZooKeeper("127.0.0.1:" + port, CONNECTION_TIMEOUT, this);
  for (int i = 0; i < 10000; i++) {
    zk.exists("/this/path/doesnt_exist!", true);
  }
  zk.close();
}

代码示例来源:origin: apache/zookeeper

/**
 * Verify the expected znodes were created and that the last znode, which
 * caused the roll-over, did not.
 */
private void checkNodes(ZooKeeper zk, int start, int count) throws Exception {
  LOG.info("Validating nodes {} thru {}", start, (start + count));
  for (int i = start; i < start + count; i++) {
    Assert.assertNotNull(zk.exists("/foo" + i, false));
    LOG.error("Exists zxid:{}", Long.toHexString(zk.exists("/foo" + i, false).getCzxid()));
  }
  Assert.assertNull(zk.exists("/foo" + (start + count), false));
}

代码示例来源:origin: apache/zookeeper

private void createNoStatVerifyResult(String newName)
    throws KeeperException, InterruptedException {
  Assert.assertNull("Node existed before created", zk.exists(newName, false));
  zk.create(newName, newName.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.CONTAINER);
  Assert.assertNotNull("Node was not created as expected",
      zk.exists(newName, false));
}

代码示例来源:origin: apache/zookeeper

private void createNoStatVerifyResult(String newName)
  throws KeeperException, InterruptedException {
 Assert.assertNull("Node existed before created", zk.exists(newName, false));
 String path = zk.create(newName, newName.getBytes(),
             Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
 Assert.assertEquals(path, newName);
 Assert.assertNotNull("Node was not created as expected",
            zk.exists(newName, false));
}

代码示例来源:origin: apache/zookeeper

private Stat createWithStatVerifyResult(String newName)
    throws KeeperException, InterruptedException {
  Assert.assertNull("Node existed before created", zk.exists(newName, false));
  Stat stat = new Stat();
  zk.create(newName, newName.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.CONTAINER, stat);
  validateCreateStat(stat, newName);
  Stat referenceStat = zk.exists(newName, false);
  Assert.assertNotNull("Node was not created as expected", referenceStat);
  Assert.assertEquals(referenceStat, stat);
  return stat;
}

代码示例来源:origin: apache/zookeeper

@Test
public void testCreateWithNullStat()
  throws IOException, KeeperException, InterruptedException {
 String name = "/foo";
 Assert.assertNull(zk.exists(name, false));
 Stat stat = null;
 // If a null Stat object is passed the create should still
 // succeed, but no Stat info will be returned.
 String path = zk.create(name, name.getBytes(),
   Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT, stat);
 Assert.assertNull(stat);
 Assert.assertNotNull(zk.exists(name, false));
}

代码示例来源:origin: apache/zookeeper

@SuppressWarnings("ConstantConditions")
@Test(timeout = 30000)
public void testCreateWithNullStat()
    throws KeeperException, InterruptedException {
  final String name = "/foo";
  Assert.assertNull(zk.exists(name, false));
  Stat stat = null;
  // If a null Stat object is passed the create should still
  // succeed, but no Stat info will be returned.
  zk.create(name, name.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.CONTAINER, stat);
  Assert.assertNull(stat);
  Assert.assertNotNull(zk.exists(name, false));
}

代码示例来源:origin: apache/zookeeper

public void verifyExistsFailure_NoNode() {
  rc = KeeperException.Code.NONODE;
  stat = null;
  zk.exists(path, false, this, toString());
  verify();
}

代码示例来源:origin: apache/zookeeper

private Stat createWithStatVerifyResult(String newName)
   throws KeeperException, InterruptedException {
 Assert.assertNull("Node existed before created", zk.exists(newName, false));
 Stat stat = new Stat();
 String path = zk.create(newName, newName.getBytes(),
             Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT, stat);
 Assert.assertEquals(path, newName);
 validateCreateStat(stat, newName);
 Stat referenceStat = zk.exists(newName, false);
 Assert.assertNotNull("Node was not created as expected", referenceStat);
 Assert.assertEquals(referenceStat, stat);
 return stat;
}

代码示例来源:origin: apache/zookeeper

public static void main(String[] args) throws Exception {
  System.out.printf("Starting\n");
  final TimeTest test = new TimeTest();
  System.out.printf("After construct\n");
  test.setUp();
  ZooKeeper zk = test.createClient();
  zk.create("/ephemeral", new byte[]{1, 2, 3},
      ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
  while (Time.currentElapsedTime() - nt0 < 100000) {
    System.out.printf("%d\t%s\n", discrepancy(),
        zk.exists("/ephemeral",
            watchCount.get() == 0 ? createWatcher() : null) != null);
    waitByYielding(500);
  }
}

代码示例来源:origin: apache/zookeeper

public void verifyMultiFailure_NoSideEffect() throws KeeperException, InterruptedException {
  List<Op> ops = Arrays.asList(
      Op.create("/multi", new byte[0],
          Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT),
      Op.delete("/nonexist1", -1));
  zk.multi(ops, this, null);
  latch_await();
  Assert.assertTrue(this.opResults.get(0) instanceof OpResult.ErrorResult);
  Assert.assertNull(zk.exists("/multi", false));
}

代码示例来源:origin: apache/zookeeper

@Test
public void testNodeCreated() throws Exception {
  String path = "/test1-created";
  zk1.exists(path, watcher);
  qu.shutdown(1);
  zk2.create(path, new byte[2], ZooDefs.Ids.OPEN_ACL_UNSAFE,
      CreateMode.PERSISTENT);
  qu.start(1);
  watcher.waitForConnected(TIMEOUT * 1000L);
  watcher.assertEvent(TIMEOUT, EventType.NodeCreated);
}

代码示例来源:origin: apache/zookeeper

@Test
public void testCreateDelete() throws Exception {
  multi(zk, Arrays.asList(
      Op.create("/multi", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT),
      Op.delete("/multi", 0)
      ));
  // '/multi' should have been deleted
  Assert.assertNull(zk.exists("/multi", null));
}

代码示例来源:origin: apache/zookeeper

@Test(timeout = 30000)
public void testSimpleDeletion()
    throws KeeperException, InterruptedException {
  zk.create("/foo", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.CONTAINER);
  zk.create("/foo/bar", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
  zk.delete("/foo/bar", -1);  // should cause "/foo" to get deleted when checkContainers() is called
  ContainerManager containerManager = new ContainerManager(serverFactory.getZooKeeperServer()
      .getZKDatabase(), serverFactory.getZooKeeperServer().firstProcessor, 1, 100);
  containerManager.checkContainers();
  Thread.sleep(1000);
  Assert.assertNull("Container should have been deleted", zk.exists("/foo", false));
}

代码示例来源:origin: apache/zookeeper

public void verifyExists() {
  new StringCB(zk).verifyCreate();
  zk.exists(path, false, this, toString());
  verify();
}

代码示例来源:origin: apache/zookeeper

@Test
public void testDeleteUpdateConflict() throws Exception {
  /* Delete of a node folowed by an update of the (now) deleted node */
  try {
    multi(zk, Arrays.asList(
      Op.create("/multi", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT),
      Op.delete("/multi", 0),
      Op.setData("/multi", "Y".getBytes(), 0)
      ));
    Assert.fail("/multi should have been deleted so setData should have failed");
  } catch (KeeperException e) {
    /* PASS */
  }
  // '/multi' should never have been created as entire op should fail
  Assert.assertNull(zk.exists("/multi", null)) ;
}

代码示例来源:origin: apache/zookeeper

@Test
public void testNodeDataChanged() throws Exception {
  String path = "/test-changed";
  zk1.create(path, new byte[1], ZooDefs.Ids.OPEN_ACL_UNSAFE,
      CreateMode.PERSISTENT);
  Stat stat1 = zk1.exists(path, watcher);
  qu.shutdown(1);
  zk2.setData(path, new byte[2], stat1.getVersion());
  qu.start(1);
  watcher.waitForConnected(TIMEOUT);
  watcher.assertEvent(TIMEOUT, EventType.NodeDataChanged);
}

相关文章