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

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

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

ZooKeeper.multi介绍

[英]Executes multiple ZooKeeper operations or none of them.

On success, a list of results is returned. On failure, an exception is raised which contains partial results and error details, see KeeperException#getResults

Note: The maximum allowable size of all of the data arrays in all of the setData operations in this single request is typically 1 MB (1,048,576 bytes). This limit is specified on the server via jute.maxbuffer. Requests larger than this will cause a KeeperException to be thrown.
[中]执行多个ZooKeeper操作或不执行任何操作。
成功后,将返回结果列表。失败时,会引发包含部分结果和错误详细信息的异常,请参阅KeeperException#getResults
注意:在这个单一请求中,所有setData操作中所有数据数组的最大允许大小通常为1MB(1048576字节)。此限制是通过jute.maxbuffer在服务器上指定的。大于此值的请求将导致抛出KeeperException。

代码示例

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

public List<OpResult> commit() throws InterruptedException, KeeperException {
  return zk.multi(ops);
}

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

public void commit(MultiCallback cb, Object ctx) {
    zk.multi(ops, cb, ctx);
  }
}

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

public List<OpResult> commit() throws InterruptedException, KeeperException {
  return zk.multi(ops);
}

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

public void commit(MultiCallback cb, Object ctx) {
    zk.multi(ops, cb, ctx);
  }
}

代码示例来源:origin: twitter/distributedlog

@Override
public Future<Void> execute() {
  if (!done.compareAndSet(false, true)) {
    return result;
  }
  try {
    zkc.get().multi(zkOps, this, result);
  } catch (ZooKeeperClient.ZooKeeperConnectionException e) {
    result.setException(FutureUtils.zkException(e, ""));
  } catch (InterruptedException e) {
    result.setException(FutureUtils.zkException(e, ""));
  }
  return result;
}

代码示例来源: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

public void verifyMultiFailure_AllErrorResult() {
  List<Op> ops = Arrays.asList(
      Op.create("/multi", new byte[0],
          Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT),
      Op.delete("/nonexist1", -1),
      Op.setData("/multi", "test".getBytes(), -1));
  zk.multi(ops, this, null);
  latch_await();
  Assert.assertTrue(this.opResults.get(0) instanceof OpResult.ErrorResult);
  Assert.assertTrue(this.opResults.get(1) instanceof OpResult.ErrorResult);
  Assert.assertTrue(this.opResults.get(2) instanceof OpResult.ErrorResult);
}

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

zk.multi(opList);
  Assert.fail("multi delete should failed for not empty directory");
} catch (KeeperException.NotEmptyException e) {
zk.multi(opList);

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

private List<OpResult> multi(ZooKeeper zk, Iterable<Op> ops)
throws KeeperException, InterruptedException {
  if (useAsync) {
    final MultiResult res = new MultiResult();
    zk.multi(ops, new MultiCallback() {
      @Override
      public void processResult(int rc, String path, Object ctx,
                   List<OpResult> opResults) {
        synchronized (res) {
          res.rc = rc;
          res.results = opResults;
          res.finished = true;
          res.notifyAll();
        }
      }
    }, null);
    synchronized (res) {
      while (!res.finished) {
        res.wait();
      }
    }
    if (KeeperException.Code.OK.intValue() != res.rc) {
      KeeperException ke = KeeperException.create(KeeperException.Code.get(res.rc));
      throw ke;
    }
    return res.results;
  } else {
    return zk.multi(ops);
  }
}

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

public void verifyMulti() {
  List<Op> ops = Arrays.asList(
      Op.create("/multi", new byte[0],
          Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT),
      Op.delete("/multi", -1));
  zk.multi(ops, this, null);
  latch_await();
  Assert.assertEquals(this.rc, KeeperException.Code.OK.intValue());
  Assert.assertTrue(this.opResults.get(0) instanceof OpResult.CreateResult);
  Assert.assertTrue(this.opResults.get(1) instanceof OpResult.DeleteResult);
}

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

public void verifyMultiSequential_NoSideEffect() throws Exception{
    StringCB scb = new StringCB(zk);
    scb.verifyCreate();
    String path = scb.path + "-";
    String seqPath = path + "0000000002";
    zk.create(path, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL);
    Assert.assertNotNull(zk.exists(path + "0000000001", false));
    List<Op> ops = Arrays.asList(
        Op.create(path , new byte[0],
            Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL),
        Op.delete("/nonexist", -1));
    zk.multi(ops, this, null);
    latch_await();
    Assert.assertNull(zk.exists(seqPath, false));
    zk.create(path, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL);
    Assert.assertNotNull(zk.exists(seqPath, false));
  }
}

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

@Test(timeout = 30000)
public void testMultiWithContainerSimple()
    throws KeeperException, InterruptedException {
  Op createContainer = Op.create("/foo", new byte[0],
      ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.CONTAINER);
  zk.multi(Collections.singletonList(createContainer));
  DataTree dataTree = serverFactory.getZooKeeperServer().getZKDatabase().getDataTree();
  Assert.assertEquals(dataTree.getContainers().size(), 1);
}

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

zk[followerA].multi(Arrays.asList(
  Op.create(node1, node1.getBytes(),
      Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT),

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

if (useAsync) {
  final MultiResult res = new MultiResult();
  zk.multi(ops, new MultiCallback() {
    @Override
    public void processResult(int rc, String path, Object ctx,
    zk.multi(ops);
    Assert.fail("Shouldn't have validated in ZooKeeper client!");
  } catch (KeeperException e) {

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

watcher.waitForConnected(CONNECTION_TIMEOUT);
zk.multi(Arrays.asList(
    Op.create("/multi0", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT),
    Op.create("/multi1", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT),

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

@Test
public void ephemeralCreateMultiOpTest() throws KeeperException, InterruptedException, IOException {
  final ZooKeeper zk = createClient();
  String data = "test";
  String path = "/ephemeralcreatemultiop";
  zk.create(path, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
  QuorumZooKeeperServer server = getConnectedServer(zk.getSessionId());
  Assert.assertNotNull("unable to find server interlocutor", server);
  UpgradeableSessionTracker sessionTracker = (UpgradeableSessionTracker)server.getSessionTracker();
  Assert.assertFalse("session already global", sessionTracker.isGlobalSession(zk.getSessionId()));
  List<OpResult> multi = null;
  try {
    multi = zk.multi(Arrays.asList(
        Op.setData(path, data.getBytes(), 0),
        Op.create(path + "/e", data.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL),
        Op.create(path + "/p", data.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT),
        Op.create(path + "/q", data.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL)
    ));
  } catch (KeeperException.SessionExpiredException e) {
    // the scenario that inspired this unit test
    Assert.fail("received session expired for a session promotion in a multi-op");
  }
  Assert.assertNotNull(multi);
  Assert.assertEquals(4, multi.size());
  Assert.assertEquals(data, new String(zk.getData(path + "/e", false, null)));
  Assert.assertEquals(data, new String(zk.getData(path + "/p", false, null)));
  Assert.assertEquals(data, new String(zk.getData(path + "/q", false, null)));
  Assert.assertTrue("session not promoted", sessionTracker.isGlobalSession(zk.getSessionId()));
}

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

zk.getSessionId(),
  zk.getSessionPasswd());
zknew.multi(Arrays.asList(
  Op.create("/testSessionMovedWithMultiOp-1", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL)
));

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

@Test(timeout = 30000)
public void testMultiWithContainer()
    throws KeeperException, InterruptedException {
  Op createContainer = Op.create("/foo", new byte[0],
      ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.CONTAINER);
  Op createChild = Op.create("/foo/bar", new byte[0],
      ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
  zk.multi(Arrays.asList(createContainer, createChild));
  DataTree dataTree = serverFactory.getZooKeeperServer().getZKDatabase().getDataTree();
  Assert.assertEquals(dataTree.getContainers().size(), 1);
  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));
  createContainer = Op.create("/foo", new byte[0],
      ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.CONTAINER);
  createChild = Op.create("/foo/bar", new byte[0],
      ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
  Op deleteChild = Op.delete("/foo/bar", -1);
  zk.multi(Arrays.asList(createContainer, createChild, deleteChild));
  containerManager.checkContainers();
  Thread.sleep(1000);
  Assert.assertNull("Container should have been deleted", zk.exists("/foo", false));
}

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

if (useAsync) {
  final MultiResult res = new MultiResult();
  zk.multi(ops, new MultiCallback() {
    @Override
    public void processResult(int rc, String path, Object ctx,
} else {
  try {
    zk.multi(ops);
    Assert.fail("/multi should have been deleted so setData should have failed");
  } catch (KeeperException e) {

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

/**
 * Run multiple operations in a transactional manner. Retry before throwing exception
 */
public List<OpResult> multi(Iterable<Op> ops)
 throws KeeperException, InterruptedException {
 try (TraceScope scope = TraceUtil.createTrace("RecoverableZookeeper.multi")) {
  RetryCounter retryCounter = retryCounterFactory.create();
  Iterable<Op> multiOps = prepareZKMulti(ops);
  while (true) {
   try {
    long startTime = EnvironmentEdgeManager.currentTime();
    List<OpResult> opResults = checkZk().multi(multiOps);
    return opResults;
   } catch (KeeperException e) {
    switch (e.code()) {
     case CONNECTIONLOSS:
      retryOrThrow(retryCounter, e, "multi");
      break;
     case OPERATIONTIMEOUT:
      retryOrThrow(retryCounter, e, "multi");
      break;
     default:
      throw e;
    }
   }
   retryCounter.sleepUntilNextRetry();
  }
 }
}

相关文章