org.apache.hadoop.hbase.zookeeper.ZKUtil.createWithParents()方法的使用及代码示例

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

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

ZKUtil.createWithParents介绍

[英]Creates the specified node and all parent nodes required for it to exist. No watches are set and no errors are thrown if the node already exists. The nodes created are persistent and open access.
[中]创建指定节点及其存在所需的所有父节点。如果节点已经存在,则不会设置任何监视,也不会引发任何错误。创建的节点是持久的、开放访问的。

代码示例

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

  1. /**
  2. * Creates the specified node and all parent nodes required for it to exist.
  3. *
  4. * No watches are set and no errors are thrown if the node already exists.
  5. *
  6. * The nodes created are persistent and open access.
  7. *
  8. * @param zkw zk reference
  9. * @param znode path of node
  10. * @throws KeeperException if unexpected zookeeper exception
  11. */
  12. public static void createWithParents(ZKWatcher zkw, String znode)
  13. throws KeeperException {
  14. createWithParents(zkw, znode, new byte[0]);
  15. }

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

  1. @Override
  2. public void addWAL(ServerName serverName, String queueId, String fileName)
  3. throws ReplicationException {
  4. try {
  5. ZKUtil.createWithParents(zookeeper, getFileNode(serverName, queueId, fileName));
  6. } catch (KeeperException e) {
  7. throw new ReplicationException("Failed to add wal to queue (serverName=" + serverName
  8. + ", queueId=" + queueId + ", fileName=" + fileName + ")", e);
  9. }
  10. }

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

  1. @Override
  2. public void setLastSequenceIds(String peerId, Map<String, Long> lastSeqIds)
  3. throws ReplicationException {
  4. try {
  5. // No need CAS and retry here, because it'll call setLastSequenceIds() for disabled peers
  6. // only, so no conflict happen.
  7. List<ZKUtilOp> listOfOps = new ArrayList<>();
  8. for (Entry<String, Long> lastSeqEntry : lastSeqIds.entrySet()) {
  9. String path = getSerialReplicationRegionPeerNode(lastSeqEntry.getKey(), peerId);
  10. ZKUtil.createWithParents(zookeeper, path);
  11. listOfOps.add(ZKUtilOp.setData(path, ZKUtil.positionToByteArray(lastSeqEntry.getValue())));
  12. }
  13. if (!listOfOps.isEmpty()) {
  14. ZKUtil.multiOrSequential(zookeeper, listOfOps, true);
  15. }
  16. } catch (KeeperException e) {
  17. throw new ReplicationException("Failed to set last sequence ids, peerId=" + peerId
  18. + ", size of lastSeqIds=" + lastSeqIds.size(), e);
  19. }
  20. }

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

  1. /**
  2. * Set data into node creating node if it doesn't yet exist.
  3. * Does not set watch.
  4. *
  5. * @param zkw zk reference
  6. * @param znode path of node
  7. * @param data data to set for node
  8. * @throws KeeperException if a ZooKeeper operation fails
  9. */
  10. public static void createSetData(final ZKWatcher zkw, final String znode, final byte [] data)
  11. throws KeeperException {
  12. if (checkExists(zkw, znode) == -1) {
  13. ZKUtil.createWithParents(zkw, znode, data);
  14. } else {
  15. ZKUtil.setData(zkw, znode, data);
  16. }
  17. }

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

  1. @Override
  2. public void addPeerToHFileRefs(String peerId) throws ReplicationException {
  3. String peerNode = getHFileRefsPeerNode(peerId);
  4. try {
  5. if (ZKUtil.checkExists(zookeeper, peerNode) == -1) {
  6. LOG.info("Adding peer {} to hfile reference queue.", peerId);
  7. ZKUtil.createWithParents(zookeeper, peerNode);
  8. }
  9. } catch (KeeperException e) {
  10. throw new ReplicationException("Failed to add peer " + peerId + " to hfile reference queue.",
  11. e);
  12. }
  13. }

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

  1. public void start() throws KeeperException {
  2. watcher.registerListener(this);
  3. ZKUtil.createWithParents(watcher, labelZnode);
  4. ZKUtil.createWithParents(watcher, userAuthsZnode);
  5. byte[] data = ZKUtil.getDataAndWatch(watcher, labelZnode);
  6. if (data != null && data.length > 0) {
  7. refreshVisibilityLabelsCache(data);
  8. }
  9. data = ZKUtil.getDataAndWatch(watcher, userAuthsZnode);
  10. if (data != null && data.length > 0) {
  11. refreshUserAuthsCache(data);
  12. }
  13. }

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

  1. private void addLastSeqIdsToOps(String queueId, Map<String, Long> lastSeqIds,
  2. List<ZKUtilOp> listOfOps) throws KeeperException, ReplicationException {
  3. String peerId = new ReplicationQueueInfo(queueId).getPeerId();
  4. for (Entry<String, Long> lastSeqEntry : lastSeqIds.entrySet()) {
  5. String path = getSerialReplicationRegionPeerNode(lastSeqEntry.getKey(), peerId);
  6. Pair<Long, Integer> p = getLastSequenceIdWithVersion(lastSeqEntry.getKey(), peerId);
  7. byte[] data = ZKUtil.positionToByteArray(lastSeqEntry.getValue());
  8. if (p.getSecond() < 0) { // ZNode does not exist.
  9. ZKUtil.createWithParents(zookeeper,
  10. path.substring(0, path.lastIndexOf(ZNodePaths.ZNODE_PATH_SEPARATOR)));
  11. listOfOps.add(ZKUtilOp.createAndFailSilent(path, data));
  12. continue;
  13. }
  14. // Perform CAS in a specific version v0 (HBASE-20138)
  15. int v0 = p.getSecond();
  16. long lastPushedSeqId = p.getFirst();
  17. if (lastSeqEntry.getValue() <= lastPushedSeqId) {
  18. continue;
  19. }
  20. listOfOps.add(ZKUtilOp.setData(path, data, v0));
  21. }
  22. }

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

  1. /***
  2. * Write a table's access controls to the permissions mirror in zookeeper
  3. * @param entry
  4. * @param permsData
  5. */
  6. public void writeToZookeeper(byte[] entry, byte[] permsData) {
  7. String entryName = Bytes.toString(entry);
  8. String zkNode = ZNodePaths.joinZNode(watcher.getZNodePaths().baseZNode, ACL_NODE);
  9. zkNode = ZNodePaths.joinZNode(zkNode, entryName);
  10. try {
  11. ZKUtil.createWithParents(watcher, zkNode);
  12. ZKUtil.updateExistingNodeData(watcher, zkNode, permsData, -1);
  13. } catch (KeeperException e) {
  14. LOG.error("Failed updating permissions for entry '" +
  15. entryName + "'", e);
  16. watcher.abort("Failed writing node "+zkNode+" to zookeeper", e);
  17. }
  18. }

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

  1. public void start() throws KeeperException {
  2. watcher.registerListener(this);
  3. // make sure the base node exists
  4. ZKUtil.createWithParents(watcher, keysParentZNode);
  5. if (ZKUtil.watchAndCheckExists(watcher, keysParentZNode)) {
  6. List<ZKUtil.NodeAndData> nodes =
  7. ZKUtil.getChildDataAndWatchForNewChildren(watcher, keysParentZNode);
  8. refreshNodes(nodes);
  9. }
  10. }

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

  1. public void start() {
  2. try {
  3. watcher.registerListener(this);
  4. String parent = ZKUtil.getParent(leaderZNode);
  5. if (ZKUtil.checkExists(watcher, parent) < 0) {
  6. ZKUtil.createWithParents(watcher, parent);
  7. }
  8. } catch (KeeperException ke) {
  9. watcher.abort("Unhandled zk exception when starting", ke);
  10. candidate.stop("Unhandled zk exception starting up: "+ke.getMessage());
  11. }
  12. }

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

  1. @Before
  2. public void setUp() throws Exception {
  3. Configuration conf = testUtil.getConfiguration();
  4. conf.set(HConstants.MASTER_PORT, "0");
  5. conf.setInt(HConstants.ZK_SESSION_TIMEOUT, 2000);
  6. testUtil.startMiniZKCluster();
  7. ZKWatcher watcher = testUtil.getZooKeeperWatcher();
  8. ZKUtil.createWithParents(watcher, watcher.getZNodePaths().masterAddressZNode,
  9. Bytes.toBytes("fake:123"));
  10. master = new HMaster(conf);
  11. rpcClient = RpcClientFactory.createClient(conf, HConstants.CLUSTER_ID_DEFAULT);
  12. }

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

  1. private static String initPeerClusterState(String baseZKNode)
  2. throws IOException, KeeperException {
  3. // Add a dummy region server and set up the cluster id
  4. Configuration testConf = new Configuration(conf);
  5. testConf.set(HConstants.ZOOKEEPER_ZNODE_PARENT, baseZKNode);
  6. ZKWatcher zkw1 = new ZKWatcher(testConf, "test1", null);
  7. String fakeRs = ZNodePaths.joinZNode(zkw1.getZNodePaths().rsZNode,
  8. "hostname1.example.org:1234");
  9. ZKUtil.createWithParents(zkw1, fakeRs);
  10. ZKClusterId.setClusterId(zkw1, new ClusterId());
  11. return ZKConfig.getZooKeeperClusterKey(testConf);
  12. }

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

  1. @Override
  2. protected Pair<Long, Integer> getLastSequenceIdWithVersion(String encodedRegionName,
  3. String peerId) throws KeeperException {
  4. Pair<Long, Integer> oldPair = super.getLastSequenceIdWithVersion(encodedRegionName, peerId);
  5. if (getLastSeqIdOpIndex < 100) {
  6. // Let the ZNode version increase.
  7. String path = getSerialReplicationRegionPeerNode(encodedRegionName, peerId);
  8. ZKUtil.createWithParents(zookeeper, path);
  9. ZKUtil.setData(zookeeper, path, ZKUtil.positionToByteArray(100L));
  10. }
  11. getLastSeqIdOpIndex++;
  12. return oldPair;
  13. }
  14. };

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

  1. private void createBaseZNodes() throws ZooKeeperConnectionException {
  2. try {
  3. // Create all the necessary "directories" of znodes
  4. ZKUtil.createWithParents(this, znodePaths.baseZNode);
  5. ZKUtil.createAndFailSilent(this, znodePaths.rsZNode);
  6. ZKUtil.createAndFailSilent(this, znodePaths.drainingZNode);
  7. ZKUtil.createAndFailSilent(this, znodePaths.tableZNode);
  8. ZKUtil.createAndFailSilent(this, znodePaths.splitLogZNode);
  9. ZKUtil.createAndFailSilent(this, znodePaths.backupMasterAddressesZNode);
  10. ZKUtil.createAndFailSilent(this, znodePaths.tableLockZNode);
  11. ZKUtil.createAndFailSilent(this, znodePaths.masterMaintZNode);
  12. } catch (KeeperException e) {
  13. throw new ZooKeeperConnectionException(
  14. prefix("Unexpected KeeperException creating base node"), e);
  15. }
  16. }

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

  1. @Override
  2. public void addPeer(String peerId, ReplicationPeerConfig peerConfig, boolean enabled,
  3. SyncReplicationState syncReplicationState) throws ReplicationException {
  4. List<ZKUtilOp> multiOps = Arrays.asList(
  5. ZKUtilOp.createAndFailSilent(getPeerNode(peerId),
  6. ReplicationPeerConfigUtil.toByteArray(peerConfig)),
  7. ZKUtilOp.createAndFailSilent(getPeerStateNode(peerId),
  8. enabled ? ENABLED_ZNODE_BYTES : DISABLED_ZNODE_BYTES),
  9. ZKUtilOp.createAndFailSilent(getSyncReplicationStateNode(peerId),
  10. SyncReplicationState.toByteArray(syncReplicationState)),
  11. ZKUtilOp.createAndFailSilent(getNewSyncReplicationStateNode(peerId), NONE_STATE_ZNODE_BYTES));
  12. try {
  13. ZKUtil.createWithParents(zookeeper, peersZNode);
  14. ZKUtil.multiOrSequential(zookeeper, multiOps, false);
  15. } catch (KeeperException e) {
  16. throw new ReplicationException(
  17. "Could not add peer with id=" + peerId + ", peerConfig=>" + peerConfig + ", state=" +
  18. (enabled ? "ENABLED" : "DISABLED") + ", syncReplicationState=" + syncReplicationState,
  19. e);
  20. }
  21. }

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

  1. /**
  2. * Create a znode with data
  3. */
  4. @Test
  5. public void testCreateWithParents() throws KeeperException, InterruptedException {
  6. byte[] expectedData = new byte[] { 1, 2, 3 };
  7. ZKUtil.createWithParents(ZKW, "/l1/l2/l3/l4/testCreateWithParents", expectedData);
  8. byte[] data = ZKUtil.getData(ZKW, "/l1/l2/l3/l4/testCreateWithParents");
  9. assertTrue(Bytes.equals(expectedData, data));
  10. ZKUtil.deleteNodeRecursively(ZKW, "/l1");
  11. ZKUtil.createWithParents(ZKW, "/testCreateWithParents", expectedData);
  12. data = ZKUtil.getData(ZKW, "/testCreateWithParents");
  13. assertTrue(Bytes.equals(expectedData, data));
  14. ZKUtil.deleteNodeRecursively(ZKW, "/testCreateWithParents");
  15. }

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

  1. /**
  2. * Finally, we check the ACLs of a node outside of the /hbase hierarchy and
  3. * verify that its ACL is simply 'hbase:Perms.ALL'.
  4. */
  5. @Test
  6. public void testOutsideHBaseNodeACL() throws Exception {
  7. if (!secureZKAvailable) {
  8. return;
  9. }
  10. ZKUtil.createWithParents(zkw, "/testACLNode");
  11. List<ACL> acls = zkw.getRecoverableZooKeeper().getZooKeeper()
  12. .getACL("/testACLNode", new Stat());
  13. assertEquals(1, acls.size());
  14. assertEquals("sasl", acls.get(0).getId().getScheme());
  15. assertEquals("hbase", acls.get(0).getId().getId());
  16. assertEquals(ZooDefs.Perms.ALL, acls.get(0).getPerms());
  17. }

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

  1. @BeforeClass
  2. public static void setUpBeforeClass() throws Exception {
  3. utility = new HBaseTestingUtility();
  4. utility.startMiniZKCluster();
  5. conf = utility.getConfiguration();
  6. ZKWatcher zk = HBaseTestingUtility.getZooKeeperWatcher(utility);
  7. ZKUtil.createWithParents(zk, zk.getZNodePaths().rsZNode);
  8. }

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

  1. @Test
  2. public void testSetDataWithVersion() throws Exception {
  3. ZKUtil.createWithParents(ZKW, "/s1/s2/s3");
  4. int v0 = getZNodeDataVersion("/s1/s2/s3");
  5. assertEquals(0, v0);
  6. ZKUtil.setData(ZKW, "/s1/s2/s3", Bytes.toBytes(12L));
  7. int v1 = getZNodeDataVersion("/s1/s2/s3");
  8. assertEquals(1, v1);
  9. ZKUtil.multiOrSequential(ZKW,
  10. ImmutableList.of(ZKUtilOp.setData("/s1/s2/s3", Bytes.toBytes(13L), v1)), false);
  11. int v2 = getZNodeDataVersion("/s1/s2/s3");
  12. assertEquals(2, v2);
  13. }

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

  1. /**
  2. * Setup the config for the cluster
  3. */
  4. @BeforeClass
  5. public static void setupCluster() throws Exception {
  6. setupConf(UTIL.getConfiguration());
  7. UTIL.startMiniZKCluster();
  8. CONNECTION = (ClusterConnection)ConnectionFactory.createConnection(UTIL.getConfiguration());
  9. archivingClient = new ZKTableArchiveClient(UTIL.getConfiguration(), CONNECTION);
  10. // make hfile archiving node so we can archive files
  11. ZKWatcher watcher = UTIL.getZooKeeperWatcher();
  12. String archivingZNode = ZKTableArchiveClient.getArchiveZNode(UTIL.getConfiguration(), watcher);
  13. ZKUtil.createWithParents(watcher, archivingZNode);
  14. rss = mock(RegionServerServices.class);
  15. }

相关文章