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

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

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

ZooKeeper.transaction介绍

[英]A Transaction is a thin wrapper on the #multi method which provides a builder object that can be used to construct and commit an atomic set of operations.
[中]事务是#multi方法上的一个薄包装,它提供了一个生成器对象,可用于构造和提交一组原子操作。

代码示例

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

void createLogInNamespaceSync(URI uri, String logName)
    throws InterruptedException, IOException, KeeperException {
  Transaction txn = zkc.get().transaction();

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

@Test
public void testChRootTransaction() throws Exception {
  // creating the subtree for chRoot clients.
  String chRoot = createNameSpace();
  // checking the child version using chRoot client.
  zk_chroot = createClient(this.hostPort + chRoot);
  String childPath = "/myid";
  Transaction transaction = zk_chroot.transaction();
  transaction.create(childPath, new byte[0], Ids.OPEN_ACL_UNSAFE,
      CreateMode.PERSISTENT);
  transaction.check(childPath, 0);
  transaction.setData(childPath, childPath.getBytes(), 0);
  commit(transaction);
  Assert.assertNotNull("zNode is not created under chroot:" + chRoot, zk
      .exists(chRoot + childPath, false));
  Assert.assertNotNull("zNode is not created under chroot:" + chRoot,
      zk_chroot.exists(childPath, false));
  Assert.assertNull("zNode is created directly under '/', ignored configured chroot",
          zk.exists(childPath, false));
  Assert.assertArrayEquals("zNode data not matching", childPath
      .getBytes(), zk_chroot.getData(childPath, false, null));
  transaction = zk_chroot.transaction();
  // Deleting child using chRoot client.
  transaction.delete(childPath, 1);
  commit(transaction);
  Assert.assertNull("chroot:" + chRoot + " exists after delete", zk
      .exists(chRoot + "/myid", false));
  Assert.assertNull("chroot:" + chRoot + " exists after delete",
      zk_chroot.exists("/myid", false));
}

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

Transaction transaction = zk.transaction();
transaction.setData(node1, "no way".getBytes(), -1);
transaction.create(node2, data.getBytes(),

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

@Test
public void testTransactionBuilder() throws Exception {
  List<OpResult> results = commit(zk.transaction()
      .create("/t1", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT)
      .create("/t1/child", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT)
  results = commit(zk.transaction()
      .check("/t1", 0)
      .check("/t1/child", 0)
    results = commit(zk.transaction()
        .check("/t1", 0)
        .check("/t1/child", 0)
  results = commit(zk.transaction()
      .check("/t1", 0)
      .setData("/t1", new byte[0], 0));
    results = commit(zk.transaction()
        .check("/t1", 1)
        .setData("/t1", new byte[0], 2));
  results = commit(zk.transaction()
      .check("/t1", 1)
      .check("/t1/child", 0)
  results = commit(zk.transaction()
      .delete("/t2", -1)
      .delete("/t1/child", -1));

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

private static void createLog(ZooKeeperClient zk, URI uri, String logName, String logIdentifier)
    throws Exception {
  final String logRootPath = getLogRootPath(uri, logName, logIdentifier);
  final String logSegmentsPath = logRootPath + LOGSEGMENTS_PATH;
  final String maxTxIdPath = logRootPath + MAX_TXID_PATH;
  final String lockPath = logRootPath + LOCK_PATH;
  final String readLockPath = logRootPath + READ_LOCK_PATH;
  final String versionPath = logRootPath + VERSION_PATH;
  final String allocationPath = logRootPath + ALLOCATION_PATH;
  Utils.zkCreateFullPathOptimistic(zk, logRootPath, new byte[0],
      zk.getDefaultACL(), CreateMode.PERSISTENT);
  Transaction txn = zk.get().transaction();
  txn.create(logSegmentsPath, DLUtils.serializeLogSegmentSequenceNumber(
          DistributedLogConstants.UNASSIGNED_LOGSEGMENT_SEQNO),
      zk.getDefaultACL(), CreateMode.PERSISTENT);
  txn.create(maxTxIdPath, DLUtils.serializeTransactionId(0L),
      zk.getDefaultACL(), CreateMode.PERSISTENT);
  txn.create(lockPath, DistributedLogConstants.EMPTY_BYTES,
      zk.getDefaultACL(), CreateMode.PERSISTENT);
  txn.create(readLockPath, DistributedLogConstants.EMPTY_BYTES,
      zk.getDefaultACL(), CreateMode.PERSISTENT);
  txn.create(versionPath, ZKLogMetadataForWriter.intToBytes(LAYOUT_VERSION),
      zk.getDefaultACL(), CreateMode.PERSISTENT);
  txn.create(allocationPath, DistributedLogConstants.EMPTY_BYTES,
      zk.getDefaultACL(), CreateMode.PERSISTENT);
  txn.commit();
}

代码示例来源:origin: ksprojects/zkcopy

/**
 *
 * @param zk
 *            Zookeeper server to commit transactions to.
 * @param transactionSize
 *            Number of operations to perform before commiting, <em>n.b you will
 *            have to perform you last {@link #commit()} manually </em>
 */
protected AutoCommitTransactionWrapper(ZooKeeper zk, int transactionSize) {
  super(zk);
  transaction = zk.transaction();
  this.zk = zk;
  this.transactionSize = transactionSize;
}

代码示例来源:origin: org.apache.bookkeeper/bookkeeper-server

@Override
@Deprecated
public Transaction transaction() {
  // since there is no reference about which client that the transaction could use
  // so just use ZooKeeper instance directly.
  // you'd better to use {@link #multi}.
  ZooKeeper zkHandle = zk.get();
  if (null == zkHandle) {
    return super.transaction();
  }
  return zkHandle.transaction();
}

代码示例来源:origin: org.apache.pulsar/pulsar-proxy

@Override
@Deprecated
public Transaction transaction() {
  // since there is no reference about which client that the transaction could use
  // so just use ZooKeeper instance directly.
  // you'd better to use {@link #multi}.
  ZooKeeper zkHandle = zk.get();
  if (null == zkHandle) {
    return super.transaction();
  }
  return zkHandle.transaction();
}

代码示例来源:origin: ksprojects/zkcopy

private void maybeCommitTransaction() {
  if (opsSinceCommit >= (transactionSize - 1)) {
    try {
      Writer.logger.info("Committing transaction");
      transaction.commit();
      opsSinceCommit = 0;
      transaction = zk.transaction();
    } catch (InterruptedException | KeeperException e) {
      throw new RuntimeException(e);
    }
  } else {
    opsSinceCommit++;
  }
}

代码示例来源:origin: sleberknight/zookeeper-samples

Transaction txn = zooKeeper.transaction();
for (int i = 2; i < args.length; i++) {
  String childPath = ZKPaths.makePath(parentPath, args[i]);

代码示例来源:origin: org.apache.distributedlog/distributedlog-service

Transaction tx = zk.transaction();
List<String> children = zk.getChildren(serverLoadPath, false);
HashSet<String> servers = new HashSet<String>(children);

代码示例来源:origin: org.apache.distributedlog/distributedlog-core

void createLogInNamespaceSync(URI uri, String logName)
    throws InterruptedException, IOException, KeeperException {
  Transaction txn = zkc.get().transaction();

相关文章