This is an automated email from the ASF dual-hosted git repository.

janh pushed a commit to branch branch-2.0
in repository https://gitbox.apache.org/repos/asf/hbase.git


The following commit(s) were added to refs/heads/branch-2.0 by this push:
     new 6d21045  HBASE-22638 ZooKeeper Utility enhancements
6d21045 is described below

commit 6d21045c1c28400192208f024454bd79997001bc
Author: Viraj Jasani <[email protected]>
AuthorDate: Sat Jul 13 22:20:30 2019 +0530

    HBASE-22638 ZooKeeper Utility enhancements
    
    Signed-off-by: Jan Hentschel <[email protected]>
---
 .../hbase/zookeeper/MiniZooKeeperCluster.java      | 33 ++++------
 .../hbase/zookeeper/RecoverableZooKeeper.java      |  6 +-
 .../apache/hadoop/hbase/zookeeper/ZKAclReset.java  | 28 ++++----
 .../apache/hadoop/hbase/zookeeper/ZKClusterId.java |  4 +-
 .../hadoop/hbase/zookeeper/ZKLeaderManager.java    |  6 +-
 .../org/apache/hadoop/hbase/zookeeper/ZKUtil.java  | 75 +++++++++++++---------
 6 files changed, 81 insertions(+), 71 deletions(-)

diff --git 
a/hbase-zookeeper/src/main/java/org/apache/hadoop/hbase/zookeeper/MiniZooKeeperCluster.java
 
b/hbase-zookeeper/src/main/java/org/apache/hadoop/hbase/zookeeper/MiniZooKeeperCluster.java
index 77d976b..74cb7af 100644
--- 
a/hbase-zookeeper/src/main/java/org/apache/hadoop/hbase/zookeeper/MiniZooKeeperCluster.java
+++ 
b/hbase-zookeeper/src/main/java/org/apache/hadoop/hbase/zookeeper/MiniZooKeeperCluster.java
@@ -54,21 +54,21 @@ public class MiniZooKeeperCluster {
 
   private static final int TICK_TIME = 2000;
   private static final int DEFAULT_CONNECTION_TIMEOUT = 30000;
-  private int connectionTimeout;
+  private final int connectionTimeout;
 
   private boolean started;
 
   /** The default port. If zero, we use a random port. */
   private int defaultClientPort = 0;
 
-  private List<NIOServerCnxnFactory> standaloneServerFactoryList;
-  private List<ZooKeeperServer> zooKeeperServers;
-  private List<Integer> clientPortList;
+  private final List<NIOServerCnxnFactory> standaloneServerFactoryList;
+  private final List<ZooKeeperServer> zooKeeperServers;
+  private final List<Integer> clientPortList;
 
   private int activeZKServerIndex;
   private int tickTime = 0;
 
-  private Configuration configuration;
+  private final Configuration configuration;
 
   public MiniZooKeeperCluster() {
     this(new Configuration());
@@ -96,6 +96,7 @@ public class MiniZooKeeperCluster {
 
   /**
    * Get the list of client ports.
+   *
    * @return clientPortList the client port list
    */
   @VisibleForTesting
@@ -141,7 +142,8 @@ public class MiniZooKeeperCluster {
       }
     }
     // Make sure that the port is unused.
-    while (true) {
+    // break when an unused port is found
+    do {
       for (i = 0; i < clientPortList.size(); i++) {
         if (returnClientPort == clientPortList.get(i)) {
           // Already used. Update the port and retry.
@@ -149,10 +151,7 @@ public class MiniZooKeeperCluster {
           break;
         }
       }
-      if (i == clientPortList.size()) {
-        break; // found a unused port, exit
-      }
-    }
+    } while (i != clientPortList.size());
     return returnClientPort;
   }
 
@@ -161,7 +160,7 @@ public class MiniZooKeeperCluster {
   }
 
   public int getBackupZooKeeperServerNum() {
-    return zooKeeperServers.size()-1;
+    return zooKeeperServers.size() - 1;
   }
 
   public int getZooKeeperServerNum() {
@@ -177,7 +176,7 @@ public class MiniZooKeeperCluster {
     System.setProperty("zookeeper.preAllocSize", "100");
     FileTxnLog.setPreallocSize(100 * 1024);
     // allow all 4 letter words
-    System.setProperty("zookeeper.4lw.commands.whitelist","*");
+    System.setProperty("zookeeper.4lw.commands.whitelist", "*");
   }
 
   public int startup(File baseDir) throws IOException, InterruptedException {
@@ -210,7 +209,7 @@ public class MiniZooKeeperCluster {
 
     // running all the ZK servers
     for (int i = 0; i < numZooKeeperServers; i++) {
-      File dir = new File(baseDir, "zookeeper_"+i).getAbsoluteFile();
+      File dir = new File(baseDir, "zookeeper_" + i).getAbsoluteFile();
       createDir(dir);
       int tickTimeToUse;
       if (this.tickTime > 0) {
@@ -266,8 +265,7 @@ public class MiniZooKeeperCluster {
       // We have selected a port as a client port.  Update clientPortList if 
necessary.
       if (clientPortList.size() <= i) { // it is not in the list, add the port
         clientPortList.add(currentClientPort);
-      }
-      else if (clientPortList.get(i) <= 0) { // the list has invalid port, 
update with valid port
+      } else if (clientPortList.get(i) <= 0) { // the list has invalid port, 
update with valid port
         clientPortList.remove(i);
         clientPortList.add(i, currentClientPort);
       }
@@ -403,13 +401,10 @@ public class MiniZooKeeperCluster {
     long start = System.currentTimeMillis();
     while (true) {
       try {
-        Socket sock = new Socket("localhost", port);
-        try {
+        try (Socket sock = new Socket("localhost", port)) {
           OutputStream outstream = sock.getOutputStream();
           outstream.write("stat".getBytes());
           outstream.flush();
-        } finally {
-          sock.close();
         }
       } catch (IOException e) {
         return true;
diff --git 
a/hbase-zookeeper/src/main/java/org/apache/hadoop/hbase/zookeeper/RecoverableZooKeeper.java
 
b/hbase-zookeeper/src/main/java/org/apache/hadoop/hbase/zookeeper/RecoverableZooKeeper.java
index 9343ec2..4f9920b 100644
--- 
a/hbase-zookeeper/src/main/java/org/apache/hadoop/hbase/zookeeper/RecoverableZooKeeper.java
+++ 
b/hbase-zookeeper/src/main/java/org/apache/hadoop/hbase/zookeeper/RecoverableZooKeeper.java
@@ -79,9 +79,9 @@ public class RecoverableZooKeeper {
   // An identifier of this process in the cluster
   private final String identifier;
   private final byte[] id;
-  private Watcher watcher;
-  private int sessionTimeout;
-  private String quorumServers;
+  private final Watcher watcher;
+  private final int sessionTimeout;
+  private final String quorumServers;
 
   public RecoverableZooKeeper(String quorumServers, int sessionTimeout,
       Watcher watcher, int maxRetries, int retryIntervalMillis, int 
maxSleepTime)
diff --git 
a/hbase-zookeeper/src/main/java/org/apache/hadoop/hbase/zookeeper/ZKAclReset.java
 
b/hbase-zookeeper/src/main/java/org/apache/hadoop/hbase/zookeeper/ZKAclReset.java
index 50a6f5e..2201f83 100644
--- 
a/hbase-zookeeper/src/main/java/org/apache/hadoop/hbase/zookeeper/ZKAclReset.java
+++ 
b/hbase-zookeeper/src/main/java/org/apache/hadoop/hbase/zookeeper/ZKAclReset.java
@@ -67,13 +67,10 @@ public class ZKAclReset extends Configured implements Tool {
 
   private static void resetAcls(final Configuration conf, boolean eraseAcls)
       throws Exception {
-    ZKWatcher zkw = new ZKWatcher(conf, "ZKAclReset", null);
-    try {
+    try (ZKWatcher zkw = new ZKWatcher(conf, "ZKAclReset", null)) {
       LOG.info((eraseAcls ? "Erase" : "Set") + " HBase ACLs for " +
-                zkw.getQuorum() + " " + zkw.znodePaths.baseZNode);
+              zkw.getQuorum() + " " + zkw.znodePaths.baseZNode);
       resetAcls(zkw, zkw.znodePaths.baseZNode, eraseAcls);
-    } finally {
-      zkw.close();
     }
   }
 
@@ -96,13 +93,20 @@ public class ZKAclReset extends Configured implements Tool {
   public int run(String[] args) throws Exception {
     boolean eraseAcls = true;
 
-    for (int i = 0; i < args.length; ++i) {
-      if (args[i].equals("-help")) {
-        printUsageAndExit();
-      } else if (args[i].equals("-set-acls")) {
-        eraseAcls = false;
-      } else {
-        printUsageAndExit();
+    for (String arg : args) {
+      switch (arg) {
+        case "-help": {
+          printUsageAndExit();
+          break;
+        }
+        case "-set-acls": {
+          eraseAcls = false;
+          break;
+        }
+        default: {
+          printUsageAndExit();
+          break;
+        }
       }
     }
 
diff --git 
a/hbase-zookeeper/src/main/java/org/apache/hadoop/hbase/zookeeper/ZKClusterId.java
 
b/hbase-zookeeper/src/main/java/org/apache/hadoop/hbase/zookeeper/ZKClusterId.java
index be2529b..1e378de 100644
--- 
a/hbase-zookeeper/src/main/java/org/apache/hadoop/hbase/zookeeper/ZKClusterId.java
+++ 
b/hbase-zookeeper/src/main/java/org/apache/hadoop/hbase/zookeeper/ZKClusterId.java
@@ -35,8 +35,8 @@ import org.apache.zookeeper.KeeperException;
  */
 @InterfaceAudience.Private
 public class ZKClusterId {
-  private ZKWatcher watcher;
-  private Abortable abortable;
+  private final ZKWatcher watcher;
+  private final Abortable abortable;
   private String id;
 
   public ZKClusterId(ZKWatcher watcher, Abortable abortable) {
diff --git 
a/hbase-zookeeper/src/main/java/org/apache/hadoop/hbase/zookeeper/ZKLeaderManager.java
 
b/hbase-zookeeper/src/main/java/org/apache/hadoop/hbase/zookeeper/ZKLeaderManager.java
index 5918e68..fa26c0c 100644
--- 
a/hbase-zookeeper/src/main/java/org/apache/hadoop/hbase/zookeeper/ZKLeaderManager.java
+++ 
b/hbase-zookeeper/src/main/java/org/apache/hadoop/hbase/zookeeper/ZKLeaderManager.java
@@ -45,9 +45,9 @@ public class ZKLeaderManager extends ZKListener {
 
   private final Object lock = new Object();
   private final AtomicBoolean leaderExists = new AtomicBoolean();
-  private String leaderZNode;
-  private byte[] nodeId;
-  private Stoppable candidate;
+  private final String leaderZNode;
+  private final byte[] nodeId;
+  private final Stoppable candidate;
 
   public ZKLeaderManager(ZKWatcher watcher, String leaderZNode,
                          byte[] identifier, Stoppable candidate) {
diff --git 
a/hbase-zookeeper/src/main/java/org/apache/hadoop/hbase/zookeeper/ZKUtil.java 
b/hbase-zookeeper/src/main/java/org/apache/hadoop/hbase/zookeeper/ZKUtil.java
index 7bb4752..a045dc0 100644
--- 
a/hbase-zookeeper/src/main/java/org/apache/hadoop/hbase/zookeeper/ZKUtil.java
+++ 
b/hbase-zookeeper/src/main/java/org/apache/hadoop/hbase/zookeeper/ZKUtil.java
@@ -29,6 +29,7 @@ import java.net.Socket;
 import java.nio.charset.StandardCharsets;
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.Collections;
 import java.util.Deque;
 import java.util.HashMap;
 import java.util.LinkedList;
@@ -243,7 +244,7 @@ public final class ZKUtil {
     private static final Map<String, String> BASIC_JAAS_OPTIONS = new 
HashMap<>();
     static {
       String jaasEnvVar = System.getenv("HBASE_JAAS_DEBUG");
-      if (jaasEnvVar != null && "true".equalsIgnoreCase(jaasEnvVar)) {
+      if ("true".equalsIgnoreCase(jaasEnvVar)) {
         BASIC_JAAS_OPTIONS.put("debug", "true");
       }
     }
@@ -350,7 +351,7 @@ public final class ZKUtil {
   throws KeeperException {
     try {
       Stat s = zkw.getRecoverableZooKeeper().exists(znode, zkw);
-      boolean exists = s != null ? true : false;
+      boolean exists = s != null;
       if (exists) {
         LOG.debug(zkw.prefix("Set watcher on existing znode=" + znode));
       } else {
@@ -440,8 +441,7 @@ public final class ZKUtil {
           ZKWatcher zkw, String znode)
   throws KeeperException {
     try {
-      List<String> children = zkw.getRecoverableZooKeeper().getChildren(znode, 
zkw);
-      return children;
+      return zkw.getRecoverableZooKeeper().getChildren(znode, zkw);
     } catch(KeeperException.NoNodeException ke) {
       LOG.debug(zkw.prefix("Unable to list children of znode " + znode + " " +
           "because node does not exist (not an error)"));
@@ -1393,7 +1393,7 @@ public final class ZKUtil {
         zkw.interruptedException(e);
       }
     }
-    // atleast one element should exist
+    // at least one element should exist
     if (ops.size() > 0) {
       multiOrSequential(zkw, ops, runSequentialOnMultiFailure);
     }
@@ -1723,8 +1723,12 @@ public final class ZKUtil {
         sb.append("<<FAILED LOOKUP: " + e.getMessage() + ">>");
       }
       sb.append("\nBackup master addresses:");
-      for (String child : listChildrenNoWatch(zkw, 
zkw.znodePaths.backupMasterAddressesZNode)) {
-        sb.append("\n ").append(child);
+      final List<String> backupMasterChildrenNoWatchList = 
listChildrenNoWatch(zkw,
+              zkw.znodePaths.backupMasterAddressesZNode);
+      if (backupMasterChildrenNoWatchList != null) {
+        for (String child : backupMasterChildrenNoWatchList) {
+          sb.append("\n ").append(child);
+        }
       }
       sb.append("\nRegion server holding hbase:meta: "
         + new MetaTableLocator().getMetaRegionLocation(zkw));
@@ -1736,8 +1740,12 @@ public final class ZKUtil {
                     + new MetaTableLocator().getMetaRegionLocation(zkw, i));
       }
       sb.append("\nRegion servers:");
-      for (String child : listChildrenNoWatch(zkw, zkw.znodePaths.rsZNode)) {
-        sb.append("\n ").append(child);
+      final List<String> rsChildrenNoWatchList =
+              listChildrenNoWatch(zkw, zkw.znodePaths.rsZNode);
+      if (rsChildrenNoWatchList != null) {
+        for (String child : rsChildrenNoWatchList) {
+          sb.append("\n ").append(child);
+        }
       }
       try {
         getReplicationZnodesDump(zkw, sb);
@@ -1788,30 +1796,33 @@ public final class ZKUtil {
     // do a ls -r on this znode
     sb.append("\n").append(replicationZnode).append(": ");
     List<String> children = ZKUtil.listChildrenNoWatch(zkw, replicationZnode);
-    for (String child : children) {
-      String znode = ZNodePaths.joinZNode(replicationZnode, child);
-      if (znode.equals(zkw.znodePaths.peersZNode)) {
-        appendPeersZnodes(zkw, znode, sb);
-      } else if (znode.equals(zkw.znodePaths.queuesZNode)) {
-        appendRSZnodes(zkw, znode, sb);
-      } else if (znode.equals(zkw.znodePaths.hfileRefsZNode)) {
-        appendHFileRefsZnodes(zkw, znode, sb);
+    if (children != null) {
+      Collections.sort(children);
+      for (String child : children) {
+        String zNode = ZNodePaths.joinZNode(replicationZnode, child);
+        if (zNode.equals(zkw.znodePaths.peersZNode)) {
+          appendPeersZnodes(zkw, zNode, sb);
+        } else if (zNode.equals(zkw.znodePaths.queuesZNode)) {
+          appendRSZnodes(zkw, zNode, sb);
+        } else if (zNode.equals(zkw.znodePaths.hfileRefsZNode)) {
+          appendHFileRefsZNodes(zkw, zNode, sb);
+        }
       }
     }
   }
 
-  private static void appendHFileRefsZnodes(ZKWatcher zkw, String 
hfileRefsZnode,
+  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(", ");
+    sb.append("\n").append(hFileRefsZNode).append(": ");
+    final List<String> hFileRefChildrenNoWatchList =
+            ZKUtil.listChildrenNoWatch(zkw, hFileRefsZNode);
+    if (hFileRefChildrenNoWatchList != null) {
+      for (String peerIdZNode : hFileRefChildrenNoWatchList) {
+        String zNodeToProcess = ZNodePaths.joinZNode(hFileRefsZNode, 
peerIdZNode);
+        sb.append("\n").append(zNodeToProcess).append(": ");
+        List<String> peerHFileRefsZNodes = ZKUtil.listChildrenNoWatch(zkw, 
zNodeToProcess);
+        if (peerHFileRefsZNodes != null) {
+          sb.append(String.join(", ", peerHFileRefsZNodes));
         }
       }
     }
@@ -1923,10 +1934,10 @@ public final class ZKUtil {
    * @return The array of response strings.
    * @throws IOException When the socket communication fails.
    */
-  public static String[] getServerStats(String server, int timeout)
-  throws IOException {
+  private static String[] getServerStats(String server, int timeout)
+    throws IOException {
     String[] sp = server.split(":");
-    if (sp == null || sp.length == 0) {
+    if (sp.length == 0) {
       return null;
     }
 
@@ -2062,7 +2073,7 @@ public final class ZKUtil {
    * @see #logZKTree(ZKWatcher, String)
    * @throws KeeperException if an unexpected exception occurs
    */
-  protected static void logZKTree(ZKWatcher zkw, String root, String prefix)
+  private static void logZKTree(ZKWatcher zkw, String root, String prefix)
       throws KeeperException {
     List<String> children = ZKUtil.listChildrenNoWatch(zkw, root);
 

Reply via email to