private List<String> getAllPeersFromHFileRefsQueue0() throws KeeperException { List<String> children = ZKUtil.listChildrenNoWatch(zookeeper, hfileRefsZNode); return children != null ? children : Collections.emptyList(); }
private List<ServerName> getListOfReplicators0() throws KeeperException { List<String> children = ZKUtil.listChildrenNoWatch(zookeeper, queuesZNode); if (children == null) { children = Collections.emptyList(); } return children.stream().map(ServerName::parseServerName).collect(toList()); }
@Override public List<String> listPeerIds() throws ReplicationException { try { List<String> children = ZKUtil.listChildrenNoWatch(zookeeper, peersZNode); return children != null ? children : Collections.emptyList(); } catch (KeeperException e) { throw new ReplicationException("Cannot get the list of peers", e); } }
private List<String> getReplicableHFiles0(String peerId) throws KeeperException { List<String> children = ZKUtil.listChildrenNoWatch(this.zookeeper, getHFileRefsPeerNode(peerId)); return children != null ? children : Collections.emptyList(); }
/** * Get the znodes corresponding to the meta replicas from ZK * @return list of znodes * @throws KeeperException if a ZooKeeper operation fails */ public List<String> getMetaReplicaNodes() throws KeeperException { List<String> childrenOfBaseNode = ZKUtil.listChildrenNoWatch(this, znodePaths.baseZNode); List<String> metaReplicaNodes = new ArrayList<>(2); if (childrenOfBaseNode != null) { String pattern = conf.get("zookeeper.znode.metaserver","meta-region-server"); for (String child : childrenOfBaseNode) { if (child.startsWith(pattern)) { metaReplicaNodes.add(child); } } } return metaReplicaNodes; }
private List<String> getWALsInQueue0(ServerName serverName, String queueId) throws KeeperException { List<String> children = ZKUtil.listChildrenNoWatch(zookeeper, getQueueNode(serverName, queueId)); return children != null ? children : Collections.emptyList(); }
private List<String> getAllQueues0(ServerName serverName) throws KeeperException { List<String> children = ZKUtil.listChildrenNoWatch(zookeeper, getRsNode(serverName)); return children != null ? children : Collections.emptyList(); }
private List<String> getRegionServersInZK(final ZKWatcher zkw) throws KeeperException { return ZKUtil.listChildrenNoWatch(zkw, zkw.getZNodePaths().rsZNode); }
private static void appendHFileRefsZnodes(ZKWatcher zkw, String hfileRefsZnode, StringBuilder sb) throws KeeperException { sb.append("\n").append(hfileRefsZnode).append(": "); for (String peerIdZnode : ZKUtil.listChildrenNoWatch(zkw, hfileRefsZnode)) { String znodeToProcess = ZNodePaths.joinZNode(hfileRefsZnode, peerIdZnode); sb.append("\n").append(znodeToProcess).append(": "); List<String> peerHFileRefsZnodes = ZKUtil.listChildrenNoWatch(zkw, znodeToProcess); int size = peerHFileRefsZnodes.size(); for (int i = 0; i < size; i++) { sb.append(peerHFileRefsZnodes.get(i)); if (i != size - 1) { sb.append(", "); } } } }
/** * This function calculates how many splitters this RS should create based on expected average * tasks per RS and the hard limit upper bound(maxConcurrentTasks) set by configuration. <br> * At any given time, a RS allows spawn MIN(Expected Tasks/RS, Hard Upper Bound) * @param numTasks total number of split tasks available * @return number of tasks this RS can grab */ private int getNumExpectedTasksPerRS(int numTasks) { // at lease one RS(itself) available int availableRSs = 1; try { List<String> regionServers = ZKUtil.listChildrenNoWatch(watcher, watcher.getZNodePaths().rsZNode); availableRSs = Math.max(availableRSs, (regionServers == null) ? 0 : regionServers.size()); } catch (KeeperException e) { // do nothing LOG.debug("getAvailableRegionServers got ZooKeeper exception", e); } int expectedTasksPerRS = (numTasks / availableRSs) + ((numTasks % availableRSs == 0) ? 0 : 1); return Math.max(1, expectedTasksPerRS); // at least be one }
/** * Helper method to print the current state of the ZK tree. * @see #logZKTree(String) * @throws KeeperException if an unexpected exception occurs */ protected void logZKTree(String root, String prefix) throws KeeperException { List<String> children = ZKUtil.listChildrenNoWatch(watcher, root); if (children == null) return; for (String child : children) { LOG.debug(prefix + child); String node = ZNodePaths.joinZNode(root.equals("/") ? "" : root, child); logZKTree(node, prefix + "---"); } }
@Override public int remainingTasksInCoordination() { int count = 0; try { List<String> tasks = ZKUtil.listChildrenNoWatch(watcher, watcher.getZNodePaths().splitLogZNode); if (tasks != null) { int listSize = tasks.size(); for (int i = 0; i < listSize; i++) { if (!ZKSplitLog.isRescanNode(tasks.get(i))) { count++; } } } } catch (KeeperException ke) { LOG.warn("Failed to check remaining tasks", ke); count = -1; } return count; }
/** * Helper method to print the current state of the ZK tree. * @see #logZKTree(ZKWatcher, String) * @throws KeeperException if an unexpected exception occurs */ protected static void logZKTree(ZKWatcher zkw, String root, String prefix) throws KeeperException { List<String> children = ZKUtil.listChildrenNoWatch(zkw, root); if (children == null) { return; } for (String child : children) { LOG.debug(prefix + child); String node = ZNodePaths.joinZNode(root.equals("/") ? "" : root, child); logZKTree(zkw, node, prefix + "---"); } }
private List<ServerName> getOnlineRS() throws IOException { if (masterServices != null) { return masterServices.getServerManager().getOnlineServersList(); } LOG.debug("Reading online RS from zookeeper"); List<ServerName> servers = new LinkedList<>(); try { for (String el : ZKUtil.listChildrenNoWatch(watcher, watcher.getZNodePaths().rsZNode)) { servers.add(ServerName.parseServerName(el)); } } catch (KeeperException e) { throw new IOException("Failed to retrieve server list from zookeeper", e); } return servers; }
private static void appendPeersZnodes(ZKWatcher zkw, String peersZnode, StringBuilder sb) throws KeeperException { int pblen = ProtobufUtil.lengthOfPBMagic(); sb.append("\n").append(peersZnode).append(": "); for (String peerIdZnode : ZKUtil.listChildrenNoWatch(zkw, peersZnode)) { String znodeToProcess = ZNodePaths.joinZNode(peersZnode, peerIdZnode); byte[] data; try { data = ZKUtil.getData(zkw, znodeToProcess); } catch (InterruptedException e) { zkw.interruptedException(e); return; } // parse the data of the above peer znode. try { ReplicationProtos.ReplicationPeer.Builder builder = ReplicationProtos.ReplicationPeer.newBuilder(); ProtobufUtil.mergeFrom(builder, data, pblen, data.length - pblen); String clusterKey = builder.getClusterkey(); sb.append("\n").append(znodeToProcess).append(": ").append(clusterKey); // add the peer-state. appendPeerState(zkw, znodeToProcess, sb); } catch (IOException ipbe) { LOG.warn("Got Exception while parsing peer: " + znodeToProcess, ipbe); } } }
private static void appendPeerState(ZKWatcher zkw, String znodeToProcess, StringBuilder sb) throws KeeperException, InvalidProtocolBufferException { String peerState = zkw.getConfiguration().get("zookeeper.znode.replication.peers.state", "peer-state"); int pblen = ProtobufUtil.lengthOfPBMagic(); for (String child : ZKUtil.listChildrenNoWatch(zkw, znodeToProcess)) { if (!child.equals(peerState)) { continue; } String peerStateZnode = ZNodePaths.joinZNode(znodeToProcess, child); sb.append("\n").append(peerStateZnode).append(": "); byte[] peerStateData; try { peerStateData = ZKUtil.getData(zkw, peerStateZnode); ReplicationProtos.ReplicationState.Builder builder = ReplicationProtos.ReplicationState.newBuilder(); ProtobufUtil.mergeFrom(builder, peerStateData, pblen, peerStateData.length - pblen); sb.append(builder.getState().name()); } catch (IOException ipbe) { LOG.warn("Got Exception while parsing peer: " + znodeToProcess, ipbe); } catch (InterruptedException e) { zkw.interruptedException(e); return; } } }
private static void resetAcls(final ZKWatcher zkw, final String znode, final boolean eraseAcls) throws Exception { List<String> children = ZKUtil.listChildrenNoWatch(zkw, znode); if (children != null) { for (String child: children) { resetAcls(zkw, ZNodePaths.joinZNode(znode, child), eraseAcls); } } ZooKeeper zk = zkw.getRecoverableZooKeeper().getZooKeeper(); if (eraseAcls) { LOG.info(" - erase ACLs for " + znode); zk.setACL(znode, ZooDefs.Ids.OPEN_ACL_UNSAFE, -1); } else { LOG.info(" - set ACLs for " + znode); zk.setACL(znode, ZKUtil.createACL(zkw, znode, true), -1); } }
/** * Get a list of all the other region servers in this cluster and set a watch * @return a list of server nanes */ private List<String> getRegisteredRegionServers(boolean watch) { List<String> result = null; try { if (watch) { result = ZKUtil.listChildrenAndWatchThem(this.zookeeper, this.zookeeper.getZNodePaths().rsZNode); } else { result = ZKUtil.listChildrenNoWatch(this.zookeeper, this.zookeeper.getZNodePaths().rsZNode); } } catch (KeeperException e) { this.abortable.abort("Get list of registered region servers", e); } return result; } }
@Test(expected = KeeperException.SystemErrorException.class) public void testInterruptedDuringAction() throws ZooKeeperConnectionException, IOException, KeeperException, InterruptedException { final RecoverableZooKeeper recoverableZk = Mockito.mock(RecoverableZooKeeper.class); ZKWatcher zkw = new ZKWatcher(HBaseConfiguration.create(), "unittest", null) { @Override public RecoverableZooKeeper getRecoverableZooKeeper() { return recoverableZk; } }; Mockito.doThrow(new InterruptedException()).when(recoverableZk) .getChildren(zkw.getZNodePaths().baseZNode, null); ZKUtil.listChildrenNoWatch(zkw, zkw.getZNodePaths().baseZNode); } }
@Test public void testRegionServerHostnameReportedToMaster() throws Exception { TEST_UTIL.getConfiguration().setBoolean(HRegionServer.RS_HOSTNAME_DISABLE_MASTER_REVERSEDNS_KEY, true); StartMiniClusterOption option = StartMiniClusterOption.builder() .numMasters(NUM_MASTERS).numRegionServers(NUM_RS).numDataNodes(NUM_RS).build(); TEST_UTIL.startMiniCluster(option); boolean tablesOnMaster = LoadBalancer.isTablesOnMaster(TEST_UTIL.getConfiguration()); int expectedRS = NUM_RS + (tablesOnMaster? 1: 0); try (ZKWatcher zkw = TEST_UTIL.getZooKeeperWatcher()) { List<String> servers = ZKUtil.listChildrenNoWatch(zkw, zkw.getZNodePaths().rsZNode); assertEquals(expectedRS, servers.size()); } } }