/** * 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. * * @param zkw zk reference * @param znode path of node * @throws KeeperException if unexpected zookeeper exception */ public static void createWithParents(ZKWatcher zkw, String znode) throws KeeperException { createWithParents(zkw, znode, new byte[0]); }
@Override public void addWAL(ServerName serverName, String queueId, String fileName) throws ReplicationException { try { ZKUtil.createWithParents(zookeeper, getFileNode(serverName, queueId, fileName)); } catch (KeeperException e) { throw new ReplicationException("Failed to add wal to queue (serverName=" + serverName + ", queueId=" + queueId + ", fileName=" + fileName + ")", e); } }
@Override public void setLastSequenceIds(String peerId, Map<String, Long> lastSeqIds) throws ReplicationException { try { // No need CAS and retry here, because it'll call setLastSequenceIds() for disabled peers // only, so no conflict happen. List<ZKUtilOp> listOfOps = new ArrayList<>(); for (Entry<String, Long> lastSeqEntry : lastSeqIds.entrySet()) { String path = getSerialReplicationRegionPeerNode(lastSeqEntry.getKey(), peerId); ZKUtil.createWithParents(zookeeper, path); listOfOps.add(ZKUtilOp.setData(path, ZKUtil.positionToByteArray(lastSeqEntry.getValue()))); } if (!listOfOps.isEmpty()) { ZKUtil.multiOrSequential(zookeeper, listOfOps, true); } } catch (KeeperException e) { throw new ReplicationException("Failed to set last sequence ids, peerId=" + peerId + ", size of lastSeqIds=" + lastSeqIds.size(), e); } }
/** * Set data into node creating node if it doesn't yet exist. * Does not set watch. * * @param zkw zk reference * @param znode path of node * @param data data to set for node * @throws KeeperException if a ZooKeeper operation fails */ public static void createSetData(final ZKWatcher zkw, final String znode, final byte [] data) throws KeeperException { if (checkExists(zkw, znode) == -1) { ZKUtil.createWithParents(zkw, znode, data); } else { ZKUtil.setData(zkw, znode, data); } }
@Override public void addPeerToHFileRefs(String peerId) throws ReplicationException { String peerNode = getHFileRefsPeerNode(peerId); try { if (ZKUtil.checkExists(zookeeper, peerNode) == -1) { LOG.info("Adding peer {} to hfile reference queue.", peerId); ZKUtil.createWithParents(zookeeper, peerNode); } } catch (KeeperException e) { throw new ReplicationException("Failed to add peer " + peerId + " to hfile reference queue.", e); } }
public void start() throws KeeperException { watcher.registerListener(this); ZKUtil.createWithParents(watcher, labelZnode); ZKUtil.createWithParents(watcher, userAuthsZnode); byte[] data = ZKUtil.getDataAndWatch(watcher, labelZnode); if (data != null && data.length > 0) { refreshVisibilityLabelsCache(data); } data = ZKUtil.getDataAndWatch(watcher, userAuthsZnode); if (data != null && data.length > 0) { refreshUserAuthsCache(data); } }
private void addLastSeqIdsToOps(String queueId, Map<String, Long> lastSeqIds, List<ZKUtilOp> listOfOps) throws KeeperException, ReplicationException { String peerId = new ReplicationQueueInfo(queueId).getPeerId(); for (Entry<String, Long> lastSeqEntry : lastSeqIds.entrySet()) { String path = getSerialReplicationRegionPeerNode(lastSeqEntry.getKey(), peerId); Pair<Long, Integer> p = getLastSequenceIdWithVersion(lastSeqEntry.getKey(), peerId); byte[] data = ZKUtil.positionToByteArray(lastSeqEntry.getValue()); if (p.getSecond() < 0) { // ZNode does not exist. ZKUtil.createWithParents(zookeeper, path.substring(0, path.lastIndexOf(ZNodePaths.ZNODE_PATH_SEPARATOR))); listOfOps.add(ZKUtilOp.createAndFailSilent(path, data)); continue; } // Perform CAS in a specific version v0 (HBASE-20138) int v0 = p.getSecond(); long lastPushedSeqId = p.getFirst(); if (lastSeqEntry.getValue() <= lastPushedSeqId) { continue; } listOfOps.add(ZKUtilOp.setData(path, data, v0)); } }
/*** * Write a table's access controls to the permissions mirror in zookeeper * @param entry * @param permsData */ public void writeToZookeeper(byte[] entry, byte[] permsData) { String entryName = Bytes.toString(entry); String zkNode = ZNodePaths.joinZNode(watcher.getZNodePaths().baseZNode, ACL_NODE); zkNode = ZNodePaths.joinZNode(zkNode, entryName); try { ZKUtil.createWithParents(watcher, zkNode); ZKUtil.updateExistingNodeData(watcher, zkNode, permsData, -1); } catch (KeeperException e) { LOG.error("Failed updating permissions for entry '" + entryName + "'", e); watcher.abort("Failed writing node "+zkNode+" to zookeeper", e); } }
public void start() throws KeeperException { watcher.registerListener(this); // make sure the base node exists ZKUtil.createWithParents(watcher, keysParentZNode); if (ZKUtil.watchAndCheckExists(watcher, keysParentZNode)) { List<ZKUtil.NodeAndData> nodes = ZKUtil.getChildDataAndWatchForNewChildren(watcher, keysParentZNode); refreshNodes(nodes); } }
public void start() { try { watcher.registerListener(this); String parent = ZKUtil.getParent(leaderZNode); if (ZKUtil.checkExists(watcher, parent) < 0) { ZKUtil.createWithParents(watcher, parent); } } catch (KeeperException ke) { watcher.abort("Unhandled zk exception when starting", ke); candidate.stop("Unhandled zk exception starting up: "+ke.getMessage()); } }
@Before public void setUp() throws Exception { Configuration conf = testUtil.getConfiguration(); conf.set(HConstants.MASTER_PORT, "0"); conf.setInt(HConstants.ZK_SESSION_TIMEOUT, 2000); testUtil.startMiniZKCluster(); ZKWatcher watcher = testUtil.getZooKeeperWatcher(); ZKUtil.createWithParents(watcher, watcher.getZNodePaths().masterAddressZNode, Bytes.toBytes("fake:123")); master = new HMaster(conf); rpcClient = RpcClientFactory.createClient(conf, HConstants.CLUSTER_ID_DEFAULT); }
private static String initPeerClusterState(String baseZKNode) throws IOException, KeeperException { // Add a dummy region server and set up the cluster id Configuration testConf = new Configuration(conf); testConf.set(HConstants.ZOOKEEPER_ZNODE_PARENT, baseZKNode); ZKWatcher zkw1 = new ZKWatcher(testConf, "test1", null); String fakeRs = ZNodePaths.joinZNode(zkw1.getZNodePaths().rsZNode, "hostname1.example.org:1234"); ZKUtil.createWithParents(zkw1, fakeRs); ZKClusterId.setClusterId(zkw1, new ClusterId()); return ZKConfig.getZooKeeperClusterKey(testConf); }
@Override protected Pair<Long, Integer> getLastSequenceIdWithVersion(String encodedRegionName, String peerId) throws KeeperException { Pair<Long, Integer> oldPair = super.getLastSequenceIdWithVersion(encodedRegionName, peerId); if (getLastSeqIdOpIndex < 100) { // Let the ZNode version increase. String path = getSerialReplicationRegionPeerNode(encodedRegionName, peerId); ZKUtil.createWithParents(zookeeper, path); ZKUtil.setData(zookeeper, path, ZKUtil.positionToByteArray(100L)); } getLastSeqIdOpIndex++; return oldPair; } };
private void createBaseZNodes() throws ZooKeeperConnectionException { try { // Create all the necessary "directories" of znodes ZKUtil.createWithParents(this, znodePaths.baseZNode); ZKUtil.createAndFailSilent(this, znodePaths.rsZNode); ZKUtil.createAndFailSilent(this, znodePaths.drainingZNode); ZKUtil.createAndFailSilent(this, znodePaths.tableZNode); ZKUtil.createAndFailSilent(this, znodePaths.splitLogZNode); ZKUtil.createAndFailSilent(this, znodePaths.backupMasterAddressesZNode); ZKUtil.createAndFailSilent(this, znodePaths.tableLockZNode); ZKUtil.createAndFailSilent(this, znodePaths.masterMaintZNode); } catch (KeeperException e) { throw new ZooKeeperConnectionException( prefix("Unexpected KeeperException creating base node"), e); } }
@Override public void addPeer(String peerId, ReplicationPeerConfig peerConfig, boolean enabled, SyncReplicationState syncReplicationState) throws ReplicationException { List<ZKUtilOp> multiOps = Arrays.asList( ZKUtilOp.createAndFailSilent(getPeerNode(peerId), ReplicationPeerConfigUtil.toByteArray(peerConfig)), ZKUtilOp.createAndFailSilent(getPeerStateNode(peerId), enabled ? ENABLED_ZNODE_BYTES : DISABLED_ZNODE_BYTES), ZKUtilOp.createAndFailSilent(getSyncReplicationStateNode(peerId), SyncReplicationState.toByteArray(syncReplicationState)), ZKUtilOp.createAndFailSilent(getNewSyncReplicationStateNode(peerId), NONE_STATE_ZNODE_BYTES)); try { ZKUtil.createWithParents(zookeeper, peersZNode); ZKUtil.multiOrSequential(zookeeper, multiOps, false); } catch (KeeperException e) { throw new ReplicationException( "Could not add peer with id=" + peerId + ", peerConfig=>" + peerConfig + ", state=" + (enabled ? "ENABLED" : "DISABLED") + ", syncReplicationState=" + syncReplicationState, e); } }
/** * Create a znode with data */ @Test public void testCreateWithParents() throws KeeperException, InterruptedException { byte[] expectedData = new byte[] { 1, 2, 3 }; ZKUtil.createWithParents(ZKW, "/l1/l2/l3/l4/testCreateWithParents", expectedData); byte[] data = ZKUtil.getData(ZKW, "/l1/l2/l3/l4/testCreateWithParents"); assertTrue(Bytes.equals(expectedData, data)); ZKUtil.deleteNodeRecursively(ZKW, "/l1"); ZKUtil.createWithParents(ZKW, "/testCreateWithParents", expectedData); data = ZKUtil.getData(ZKW, "/testCreateWithParents"); assertTrue(Bytes.equals(expectedData, data)); ZKUtil.deleteNodeRecursively(ZKW, "/testCreateWithParents"); }
/** * Finally, we check the ACLs of a node outside of the /hbase hierarchy and * verify that its ACL is simply 'hbase:Perms.ALL'. */ @Test public void testOutsideHBaseNodeACL() throws Exception { if (!secureZKAvailable) { return; } ZKUtil.createWithParents(zkw, "/testACLNode"); List<ACL> acls = zkw.getRecoverableZooKeeper().getZooKeeper() .getACL("/testACLNode", new Stat()); assertEquals(1, acls.size()); assertEquals("sasl", acls.get(0).getId().getScheme()); assertEquals("hbase", acls.get(0).getId().getId()); assertEquals(ZooDefs.Perms.ALL, acls.get(0).getPerms()); }
@BeforeClass public static void setUpBeforeClass() throws Exception { utility = new HBaseTestingUtility(); utility.startMiniZKCluster(); conf = utility.getConfiguration(); ZKWatcher zk = HBaseTestingUtility.getZooKeeperWatcher(utility); ZKUtil.createWithParents(zk, zk.getZNodePaths().rsZNode); }
@Test public void testSetDataWithVersion() throws Exception { ZKUtil.createWithParents(ZKW, "/s1/s2/s3"); int v0 = getZNodeDataVersion("/s1/s2/s3"); assertEquals(0, v0); ZKUtil.setData(ZKW, "/s1/s2/s3", Bytes.toBytes(12L)); int v1 = getZNodeDataVersion("/s1/s2/s3"); assertEquals(1, v1); ZKUtil.multiOrSequential(ZKW, ImmutableList.of(ZKUtilOp.setData("/s1/s2/s3", Bytes.toBytes(13L), v1)), false); int v2 = getZNodeDataVersion("/s1/s2/s3"); assertEquals(2, v2); }
/** * Setup the config for the cluster */ @BeforeClass public static void setupCluster() throws Exception { setupConf(UTIL.getConfiguration()); UTIL.startMiniZKCluster(); CONNECTION = (ClusterConnection)ConnectionFactory.createConnection(UTIL.getConfiguration()); archivingClient = new ZKTableArchiveClient(UTIL.getConfiguration(), CONNECTION); // make hfile archiving node so we can archive files ZKWatcher watcher = UTIL.getZooKeeperWatcher(); String archivingZNode = ZKTableArchiveClient.getArchiveZNode(UTIL.getConfiguration(), watcher); ZKUtil.createWithParents(watcher, archivingZNode); rss = mock(RegionServerServices.class); }