org.apache.bookkeeper.util.ZkUtils.createFullPathOptimistic()方法的使用及代码示例

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

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

ZkUtils.createFullPathOptimistic介绍

[英]Create zookeeper path recursively and optimistically. This method can throw any of the KeeperExceptions which can be thrown by ZooKeeper#create. KeeperException.NodeExistsException will only be thrown if the full path specified by path already exists. The existence of any parent znodes is not an error condition.
[中]以递归和乐观的方式创建zookeeper路径。此方法可以抛出ZooKeeper#create可以抛出的任何KeeperException。保持异常。只有当_path_u指定的完整路径已存在时,才会引发NodeExistsException。任何父节点的存在都不是错误条件。

代码示例

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

private void initializePool() throws IOException {
  try {
    List<String> allocators;
    try {
      allocators = zkc.get().getChildren(poolPath, false);
    } catch (KeeperException.NoNodeException e) {
      logger.info("Allocator Pool {} doesn't exist. Creating it.", poolPath);
      ZkUtils.createFullPathOptimistic(zkc.get(), poolPath, new byte[0], zkc.getDefaultACL(),
          CreateMode.PERSISTENT);
      allocators = zkc.get().getChildren(poolPath, false);
    }
    if (null == allocators) {
      allocators = new ArrayList<String>();
    }
    if (allocators.size() < corePoolSize) {
      createAllocators(corePoolSize - allocators.size());
      allocators = zkc.get().getChildren(poolPath, false);
    }
    initializeAllocators(allocators);
  } catch (InterruptedException ie) {
    throw new DLInterruptedException("Interrupted when ensuring " + poolPath + " created : ", ie);
  } catch (KeeperException ke) {
    throw new IOException("Encountered zookeeper exception when initializing pool " + poolPath + " : ", ke);
  }
}

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

/**
 * Start cluster
 *
 * @throws Exception
 */
protected void startBookKeeper() throws Exception {
  zkc = MockZooKeeper.newInstance();
  for (int i = 0; i < numBookies; i++) {
    ZkUtils.createFullPathOptimistic(zkc, "/ledgers/available/192.168.1.1:" + (5000 + i), "".getBytes(), null,
        null);
  }
  zkc.create("/ledgers/LAYOUT", "1\nflat:1".getBytes(), null, null);
  bkc = new PulsarMockBookKeeper(zkc, executor.chooseThread(this));
}

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

private static void createZPathIfNotExists(final ZooKeeper zkClient, final String path) throws Exception {
  if (zkClient.exists(path, false) == null) {
    try {
      ZkUtils.createFullPathOptimistic(zkClient, path, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE,
          CreateMode.PERSISTENT);
    } catch (KeeperException.NodeExistsException e) {
      // Ignore if already exists.
    }
  }
}

代码示例来源:origin: com.yahoo.pulsar/pulsar-broker

private static void createZPathIfNotExists(final ZooKeeper zkClient, final String path) throws Exception {
  if (zkClient.exists(path, false) == null) {
    try {
      ZkUtils.createFullPathOptimistic(zkClient, path, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE,
          CreateMode.PERSISTENT);
    } catch (KeeperException.NodeExistsException e) {
      // Ignore if already exists.
    }
  }
}

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

protected void zkCreateOptimistic(String path, byte[] content) throws Exception {
  ZkUtils.createFullPathOptimistic(globalZk(), path, content, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
}

代码示例来源:origin: com.yahoo.pulsar/pulsar-broker

protected void zkCreateOptimistic(String path, byte[] content) throws Exception {
  ZkUtils.createFullPathOptimistic(globalZk(), path, content, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
}

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

private void createFailureDomainRoot(ZooKeeper zk, String path) {
  try {
    final String clusterZnodePath = Paths.get(path).getParent().toString();
    if (zk.exists(clusterZnodePath, false) != null && zk.exists(path, false) == null) {
      try {
        byte[] data = "".getBytes();
        ZkUtils.createFullPathOptimistic(zk, path, data, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        LOG.info("Successfully created failure-domain znode at {}", path);
      } catch (KeeperException.NodeExistsException e) {
        // Ok
      }
    }
  } catch (KeeperException.NodeExistsException e) {
    // Ok
  } catch (Exception e) {
    LOG.warn("Failed to create failure-domain znode {} ", path, e);
  }
}

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

private void saveQuotaToZnode(String zpath, ResourceQuota quota) throws Exception {
  ZooKeeper zk = this.localZkCache.getZooKeeper();
  if (zk.exists(zpath, false) == null) {
    try {
      ZkUtils.createFullPathOptimistic(zk, zpath, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    } catch (KeeperException.NodeExistsException e) {
    }
  }
  zk.setData(zpath, this.jsonMapper.writeValueAsBytes(quota), -1);
}

代码示例来源:origin: com.yahoo.pulsar/pulsar-broker

private void saveQuotaToZnode(String zpath, ResourceQuota quota) throws Exception {
  ZooKeeper zk = this.localZkCache.getZooKeeper();
  if (zk.exists(zpath, false) == null) {
    try {
      ZkUtils.createFullPathOptimistic(zk, zpath, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    } catch (KeeperException.NodeExistsException e) {
    }
  }
  zk.setData(zpath, this.jsonMapper.writeValueAsBytes(quota), -1);
}

代码示例来源:origin: com.yahoo.pulsar/pulsar-broker-common

private void initZK() throws PulsarServerException {
  String[] paths = new String[] { CLUSTERS_ROOT, POLICIES_ROOT };
  // initialize the zk client with values
  try {
    ZooKeeper zk = cache.getZooKeeper();
    for (String path : paths) {
      try {
        if (zk.exists(path, false) == null) {
          ZkUtils.createFullPathOptimistic(zk, path, new byte[0], Ids.OPEN_ACL_UNSAFE,
              CreateMode.PERSISTENT);
        }
      } catch (KeeperException.NodeExistsException e) {
        // Ok
      }
    }
  } catch (Exception e) {
    LOG.error(e.getMessage(), e);
    throw new PulsarServerException(e);
  }
}

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

private boolean createZnodeIfNotExist(String path, Optional<Object> value) throws KeeperException, InterruptedException {
  // create persistent node on ZooKeeper
  if (globalZk().exists(path, false) == null) {
    // create all the intermediate nodes
    try {
      ZkUtils.createFullPathOptimistic(globalZk(), path,
          value.isPresent() ? jsonMapper().writeValueAsBytes(value.get()) : null, Ids.OPEN_ACL_UNSAFE,
          CreateMode.PERSISTENT);
      return true;
    } catch (KeeperException.NodeExistsException nee) {
      if(log.isDebugEnabled()) {
        log.debug("Other broker preempted the full path [{}] already. Continue...", path);
      }
    } catch (JsonGenerationException e) {
      // ignore json error as it is empty hash
    } catch (JsonMappingException e) {
    } catch (IOException e) {
    }
  }
  return false;
}

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

private void initZK() throws PulsarServerException {
  String[] paths = new String[] { CLUSTERS_ROOT, POLICIES_ROOT };
  // initialize the zk client with values
  try {
    ZooKeeper zk = cache.getZooKeeper();
    for (String path : paths) {
      try {
        if (zk.exists(path, false) == null) {
          ZkUtils.createFullPathOptimistic(zk, path, new byte[0], Ids.OPEN_ACL_UNSAFE,
              CreateMode.PERSISTENT);
        }
      } catch (KeeperException.NodeExistsException e) {
        // Ok
      }
    }
  } catch (Exception e) {
    LOG.error(e.getMessage(), e);
    throw new PulsarServerException(e);
  }
}

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

/**
 * Acquire the underreplicated ledger lock.
 */
public static void acquireUnderreplicatedLedgerLock(ZooKeeper zkc, String zkLedgersRootPath,
  long ledgerId, List<ACL> zkAcls)
    throws KeeperException, InterruptedException {
  ZkUtils.createFullPathOptimistic(zkc, getUrLedgerLockZnode(getUrLockPath(zkLedgersRootPath), ledgerId),
      LOCK_DATA, zkAcls, CreateMode.EPHEMERAL);
}

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

private static void createZnodeOptimistic(ZooKeeper zkc, String path, String data, CreateMode mode)
      throws KeeperException, InterruptedException {
    try {
      // create node optimistically
      checkNotNull(LocalZooKeeperConnectionService.createIfAbsent(zkc, path, data, mode));
    } catch (NoNodeException e) {
      // if path contains multiple levels after the root, create the intermediate nodes first
      String[] parts = path.split("/");
      if (parts.length > 3) {
        String int_path = path.substring(0, path.lastIndexOf("/"));
        if (zkc.exists(int_path, false) == null) {
          // create the intermediate nodes
          try {
            ZkUtils.createFullPathOptimistic(zkc, int_path, new byte[0], Ids.OPEN_ACL_UNSAFE,
                CreateMode.PERSISTENT);
          } catch (KeeperException.NodeExistsException nee) {
            LOG.debug(
                "Other broker preempted the full intermediate path [{}] already. Continue for acquiring the leaf ephemeral node.",
                int_path);
          }
        }
        checkNotNull(LocalZooKeeperConnectionService.createIfAbsent(zkc, path, data, mode));
      } else {
        // If failed to create immediate child of root node, throw exception
        throw e;
      }
    }
  }
}

代码示例来源:origin: com.yahoo.pulsar/pulsar-broker

private void createNamespaceIsolationPolicyNode(String nsIsolationPolicyPath)
    throws KeeperException, InterruptedException {
  // create persistent node on ZooKeeper
  if (globalZk().exists(nsIsolationPolicyPath, false) == null) {
    // create all the intermediate nodes
    try {
      ZkUtils.createFullPathOptimistic(globalZk(), nsIsolationPolicyPath,
          jsonMapper().writeValueAsBytes(Collections.emptyMap()), Ids.OPEN_ACL_UNSAFE,
          CreateMode.PERSISTENT);
    } catch (KeeperException.NodeExistsException nee) {
      log.debug("Other broker preempted the full path [{}] already. Continue...", nsIsolationPolicyPath);
    } catch (JsonGenerationException e) {
      // ignore json error as it is empty hash
    } catch (JsonMappingException e) {
    } catch (IOException e) {
    }
  }
}

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

private void initZK() throws PulsarServerException {
  String[] paths = new String[] { MANAGED_LEDGER_ROOT, OWNER_INFO_ROOT, LOCAL_POLICIES_ROOT };
  // initialize the zk client with values
  try {
    ZooKeeper zk = cache.getZooKeeper();
    for (String path : paths) {
      if (cache.exists(path)) {
        continue;
      }
      try {
        ZkUtils.createFullPathOptimistic(zk, path, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
      } catch (KeeperException.NodeExistsException e) {
        // Ok
      }
    }
  } catch (Exception e) {
    LOG.error(e.getMessage(), e);
    throw new PulsarServerException(e);
  }
}

代码示例来源:origin: com.yahoo.pulsar/pulsar-broker

private void initZK() throws PulsarServerException {
  String[] paths = new String[] { MANAGED_LEDGER_ROOT, OWNER_INFO_ROOT, LOCAL_POLICIES_ROOT };
  // initialize the zk client with values
  try {
    ZooKeeper zk = cache.getZooKeeper();
    for (String path : paths) {
      if (cache.exists(path)) {
        continue;
      }
      try {
        ZkUtils.createFullPathOptimistic(zk, path, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
      } catch (KeeperException.NodeExistsException e) {
        // Ok
      }
    }
  } catch (Exception e) {
    LOG.error(e.getMessage(), e);
    throw new PulsarServerException(e);
  }
}

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

@Override
public void start() throws PulsarServerException {
  String brokerZnodePath = LoadManager.LOADBALANCE_BROKERS_ROOT + "/" + lookupServiceAddress;
  try {
    // When running in standalone, this error can happen when killing the "standalone" process
    // ungracefully since the ZK session will not be closed and it will take some time for ZK server
    // to prune the expired sessions after startup.
    // Since there's a single broker instance running, it's safe, in this mode, to remove the old lock
    // Delete and recreate z-node
    try {
      if (zkClient.exists(brokerZnodePath, null) != null) {
        zkClient.delete(brokerZnodePath, -1);
      }
    } catch (NoNodeException nne) {
      // Ignore if z-node was just expired
    }
    ZkUtils.createFullPathOptimistic(zkClient, brokerZnodePath, localData.getJsonBytes(),
        ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
  } catch (Exception e) {
    throw new PulsarServerException(e);
  }
}

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

private void setDynamicConfigurationToZK(String zkPath, Map<String, String> settings) throws IOException {
  byte[] settingBytes = ObjectMapperFactory.getThreadLocal().writeValueAsBytes(settings);
  try {
    if (pulsar.getLocalZkCache().exists(zkPath)) {
      pulsar.getZkClient().setData(zkPath, settingBytes, -1);
    } else {
      ZkUtils.createFullPathOptimistic(pulsar.getZkClient(), zkPath, settingBytes, Ids.OPEN_ACL_UNSAFE,
          CreateMode.PERSISTENT);
    }
  } catch (Exception e) {
    log.warn("Got exception when writing to ZooKeeper path [{}]:", zkPath, e);
  }
}

代码示例来源:origin: com.yahoo.pulsar/pulsar-broker

private void setDynamicConfigurationToZK(String zkPath, Map<String, String> settings) throws IOException {
  byte[] settingBytes = ObjectMapperFactory.getThreadLocal().writeValueAsBytes(settings);
  try {
    if (pulsar.getLocalZkCache().exists(zkPath)) {
      pulsar.getZkClient().setData(zkPath, settingBytes, -1);
    } else {
      ZkUtils.createFullPathOptimistic(pulsar.getZkClient(), zkPath, settingBytes, Ids.OPEN_ACL_UNSAFE,
          CreateMode.PERSISTENT);
    }
  } catch (Exception e) {
    log.warn("Got exception when writing to ZooKeeper path [{}]:", zkPath, e);
  }
}

相关文章