Skip to content

Commit 74ae25d

Browse files
virajjasaniHorizonNet
authored andcommitted
HBASE-22638 ZooKeeper Utility enhancements
Signed-off-by: Jan Hentschel <jan.hentschel@ultratendency.com>
1 parent 84ee724 commit 74ae25d

File tree

6 files changed

+78
-71
lines changed

6 files changed

+78
-71
lines changed

hbase-zookeeper/src/main/java/org/apache/hadoop/hbase/zookeeper/MiniZooKeeperCluster.java

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -54,21 +54,21 @@ public class MiniZooKeeperCluster {
5454

5555
private static final int TICK_TIME = 2000;
5656
private static final int DEFAULT_CONNECTION_TIMEOUT = 30000;
57-
private int connectionTimeout;
57+
private final int connectionTimeout;
5858

5959
private boolean started;
6060

6161
/** The default port. If zero, we use a random port. */
6262
private int defaultClientPort = 0;
6363

64-
private List<NIOServerCnxnFactory> standaloneServerFactoryList;
65-
private List<ZooKeeperServer> zooKeeperServers;
66-
private List<Integer> clientPortList;
64+
private final List<NIOServerCnxnFactory> standaloneServerFactoryList;
65+
private final List<ZooKeeperServer> zooKeeperServers;
66+
private final List<Integer> clientPortList;
6767

6868
private int activeZKServerIndex;
6969
private int tickTime = 0;
7070

71-
private Configuration configuration;
71+
private final Configuration configuration;
7272

7373
public MiniZooKeeperCluster() {
7474
this(new Configuration());
@@ -96,6 +96,7 @@ public void addClientPort(int clientPort) {
9696

9797
/**
9898
* Get the list of client ports.
99+
*
99100
* @return clientPortList the client port list
100101
*/
101102
@VisibleForTesting
@@ -141,18 +142,16 @@ private int selectClientPort(int seedPort) {
141142
}
142143
}
143144
// Make sure that the port is unused.
144-
while (true) {
145+
// break when an unused port is found
146+
do {
145147
for (i = 0; i < clientPortList.size(); i++) {
146148
if (returnClientPort == clientPortList.get(i)) {
147149
// Already used. Update the port and retry.
148150
returnClientPort++;
149151
break;
150152
}
151153
}
152-
if (i == clientPortList.size()) {
153-
break; // found a unused port, exit
154-
}
155-
}
154+
} while (i != clientPortList.size());
156155
return returnClientPort;
157156
}
158157

@@ -161,7 +160,7 @@ public void setTickTime(int tickTime) {
161160
}
162161

163162
public int getBackupZooKeeperServerNum() {
164-
return zooKeeperServers.size()-1;
163+
return zooKeeperServers.size() - 1;
165164
}
166165

167166
public int getZooKeeperServerNum() {
@@ -177,7 +176,7 @@ private static void setupTestEnv() {
177176
System.setProperty("zookeeper.preAllocSize", "100");
178177
FileTxnLog.setPreallocSize(100 * 1024);
179178
// allow all 4 letter words
180-
System.setProperty("zookeeper.4lw.commands.whitelist","*");
179+
System.setProperty("zookeeper.4lw.commands.whitelist", "*");
181180
}
182181

183182
public int startup(File baseDir) throws IOException, InterruptedException {
@@ -210,7 +209,7 @@ public int startup(File baseDir, int numZooKeeperServers) throws IOException,
210209

211210
// running all the ZK servers
212211
for (int i = 0; i < numZooKeeperServers; i++) {
213-
File dir = new File(baseDir, "zookeeper_"+i).getAbsoluteFile();
212+
File dir = new File(baseDir, "zookeeper_" + i).getAbsoluteFile();
214213
createDir(dir);
215214
int tickTimeToUse;
216215
if (this.tickTime > 0) {
@@ -266,8 +265,7 @@ public int startup(File baseDir, int numZooKeeperServers) throws IOException,
266265
// We have selected a port as a client port. Update clientPortList if necessary.
267266
if (clientPortList.size() <= i) { // it is not in the list, add the port
268267
clientPortList.add(currentClientPort);
269-
}
270-
else if (clientPortList.get(i) <= 0) { // the list has invalid port, update with valid port
268+
} else if (clientPortList.get(i) <= 0) { // the list has invalid port, update with valid port
271269
clientPortList.remove(i);
272270
clientPortList.add(i, currentClientPort);
273271
}
@@ -403,13 +401,10 @@ private static boolean waitForServerDown(int port, long timeout) throws IOExcept
403401
long start = System.currentTimeMillis();
404402
while (true) {
405403
try {
406-
Socket sock = new Socket("localhost", port);
407-
try {
404+
try (Socket sock = new Socket("localhost", port)) {
408405
OutputStream outstream = sock.getOutputStream();
409406
outstream.write("stat".getBytes());
410407
outstream.flush();
411-
} finally {
412-
sock.close();
413408
}
414409
} catch (IOException e) {
415410
return true;

hbase-zookeeper/src/main/java/org/apache/hadoop/hbase/zookeeper/RecoverableZooKeeper.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,9 @@ public class RecoverableZooKeeper {
7979
// An identifier of this process in the cluster
8080
private final String identifier;
8181
private final byte[] id;
82-
private Watcher watcher;
83-
private int sessionTimeout;
84-
private String quorumServers;
82+
private final Watcher watcher;
83+
private final int sessionTimeout;
84+
private final String quorumServers;
8585

8686
public RecoverableZooKeeper(String quorumServers, int sessionTimeout,
8787
Watcher watcher, int maxRetries, int retryIntervalMillis, int maxSleepTime)

hbase-zookeeper/src/main/java/org/apache/hadoop/hbase/zookeeper/ZKAclReset.java

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,10 @@ private static void resetAcls(final ZKWatcher zkw, final String znode,
6767

6868
private static void resetAcls(final Configuration conf, boolean eraseAcls)
6969
throws Exception {
70-
ZKWatcher zkw = new ZKWatcher(conf, "ZKAclReset", null);
71-
try {
70+
try (ZKWatcher zkw = new ZKWatcher(conf, "ZKAclReset", null)) {
7271
LOG.info((eraseAcls ? "Erase" : "Set") + " HBase ACLs for " +
73-
zkw.getQuorum() + " " + zkw.getZNodePaths().baseZNode);
72+
zkw.getQuorum() + " " + zkw.getZNodePaths().baseZNode);
7473
resetAcls(zkw, zkw.getZNodePaths().baseZNode, eraseAcls);
75-
} finally {
76-
zkw.close();
7774
}
7875
}
7976

@@ -96,13 +93,20 @@ private void printUsageAndExit() {
9693
public int run(String[] args) throws Exception {
9794
boolean eraseAcls = true;
9895

99-
for (int i = 0; i < args.length; ++i) {
100-
if (args[i].equals("-help")) {
101-
printUsageAndExit();
102-
} else if (args[i].equals("-set-acls")) {
103-
eraseAcls = false;
104-
} else {
105-
printUsageAndExit();
96+
for (String arg : args) {
97+
switch (arg) {
98+
case "-help": {
99+
printUsageAndExit();
100+
break;
101+
}
102+
case "-set-acls": {
103+
eraseAcls = false;
104+
break;
105+
}
106+
default: {
107+
printUsageAndExit();
108+
break;
109+
}
106110
}
107111
}
108112

hbase-zookeeper/src/main/java/org/apache/hadoop/hbase/zookeeper/ZKClusterId.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@
3535
*/
3636
@InterfaceAudience.Private
3737
public class ZKClusterId {
38-
private ZKWatcher watcher;
39-
private Abortable abortable;
38+
private final ZKWatcher watcher;
39+
private final Abortable abortable;
4040
private String id;
4141

4242
public ZKClusterId(ZKWatcher watcher, Abortable abortable) {

hbase-zookeeper/src/main/java/org/apache/hadoop/hbase/zookeeper/ZKLeaderManager.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ public class ZKLeaderManager extends ZKListener {
4545

4646
private final Object lock = new Object();
4747
private final AtomicBoolean leaderExists = new AtomicBoolean();
48-
private String leaderZNode;
49-
private byte[] nodeId;
50-
private Stoppable candidate;
48+
private final String leaderZNode;
49+
private final byte[] nodeId;
50+
private final Stoppable candidate;
5151

5252
public ZKLeaderManager(ZKWatcher watcher, String leaderZNode,
5353
byte[] identifier, Stoppable candidate) {

hbase-zookeeper/src/main/java/org/apache/hadoop/hbase/zookeeper/ZKUtil.java

Lines changed: 40 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ private static class JaasConfiguration extends javax.security.auth.login.Configu
244244
private static final Map<String, String> BASIC_JAAS_OPTIONS = new HashMap<>();
245245
static {
246246
String jaasEnvVar = System.getenv("HBASE_JAAS_DEBUG");
247-
if (jaasEnvVar != null && "true".equalsIgnoreCase(jaasEnvVar)) {
247+
if ("true".equalsIgnoreCase(jaasEnvVar)) {
248248
BASIC_JAAS_OPTIONS.put("debug", "true");
249249
}
250250
}
@@ -351,7 +351,7 @@ public static boolean watchAndCheckExists(ZKWatcher zkw, String znode)
351351
throws KeeperException {
352352
try {
353353
Stat s = zkw.getRecoverableZooKeeper().exists(znode, zkw);
354-
boolean exists = s != null ? true : false;
354+
boolean exists = s != null;
355355
if (exists) {
356356
LOG.debug(zkw.prefix("Set watcher on existing znode=" + znode));
357357
} else {
@@ -441,8 +441,7 @@ public static List<String> listChildrenAndWatchForNewChildren(
441441
ZKWatcher zkw, String znode)
442442
throws KeeperException {
443443
try {
444-
List<String> children = zkw.getRecoverableZooKeeper().getChildren(znode, zkw);
445-
return children;
444+
return zkw.getRecoverableZooKeeper().getChildren(znode, zkw);
446445
} catch(KeeperException.NoNodeException ke) {
447446
LOG.debug(zkw.prefix("Unable to list children of znode " + znode + " " +
448447
"because node does not exist (not an error)"));
@@ -1744,9 +1743,12 @@ public static String dump(ZKWatcher zkw) {
17441743
sb.append("<<FAILED LOOKUP: " + e.getMessage() + ">>");
17451744
}
17461745
sb.append("\nBackup master addresses:");
1747-
for (String child : listChildrenNoWatch(zkw,
1748-
zkw.getZNodePaths().backupMasterAddressesZNode)) {
1749-
sb.append("\n ").append(child);
1746+
final List<String> backupMasterChildrenNoWatchList = listChildrenNoWatch(zkw,
1747+
zkw.getZNodePaths().backupMasterAddressesZNode);
1748+
if (backupMasterChildrenNoWatchList != null) {
1749+
for (String child : backupMasterChildrenNoWatchList) {
1750+
sb.append("\n ").append(child);
1751+
}
17501752
}
17511753
sb.append("\nRegion server holding hbase:meta: "
17521754
+ new MetaTableLocator().getMetaRegionLocation(zkw));
@@ -1758,8 +1760,12 @@ public static String dump(ZKWatcher zkw) {
17581760
+ new MetaTableLocator().getMetaRegionLocation(zkw, i));
17591761
}
17601762
sb.append("\nRegion servers:");
1761-
for (String child : listChildrenNoWatch(zkw, zkw.getZNodePaths().rsZNode)) {
1762-
sb.append("\n ").append(child);
1763+
final List<String> rsChildrenNoWatchList =
1764+
listChildrenNoWatch(zkw, zkw.getZNodePaths().rsZNode);
1765+
if (rsChildrenNoWatchList != null) {
1766+
for (String child : rsChildrenNoWatchList) {
1767+
sb.append("\n ").append(child);
1768+
}
17631769
}
17641770
try {
17651771
getReplicationZnodesDump(zkw, sb);
@@ -1810,31 +1816,33 @@ private static void getReplicationZnodesDump(ZKWatcher zkw, StringBuilder sb)
18101816
// do a ls -r on this znode
18111817
sb.append("\n").append(replicationZnode).append(": ");
18121818
List<String> children = ZKUtil.listChildrenNoWatch(zkw, replicationZnode);
1813-
Collections.sort(children);
1814-
for (String child : children) {
1815-
String znode = ZNodePaths.joinZNode(replicationZnode, child);
1816-
if (znode.equals(zkw.getZNodePaths().peersZNode)) {
1817-
appendPeersZnodes(zkw, znode, sb);
1818-
} else if (znode.equals(zkw.getZNodePaths().queuesZNode)) {
1819-
appendRSZnodes(zkw, znode, sb);
1820-
} else if (znode.equals(zkw.getZNodePaths().hfileRefsZNode)) {
1821-
appendHFileRefsZnodes(zkw, znode, sb);
1819+
if (children != null) {
1820+
Collections.sort(children);
1821+
for (String child : children) {
1822+
String zNode = ZNodePaths.joinZNode(replicationZnode, child);
1823+
if (zNode.equals(zkw.getZNodePaths().peersZNode)) {
1824+
appendPeersZnodes(zkw, zNode, sb);
1825+
} else if (zNode.equals(zkw.getZNodePaths().queuesZNode)) {
1826+
appendRSZnodes(zkw, zNode, sb);
1827+
} else if (zNode.equals(zkw.getZNodePaths().hfileRefsZNode)) {
1828+
appendHFileRefsZNodes(zkw, zNode, sb);
1829+
}
18221830
}
18231831
}
18241832
}
18251833

1826-
private static void appendHFileRefsZnodes(ZKWatcher zkw, String hfileRefsZnode,
1834+
private static void appendHFileRefsZNodes(ZKWatcher zkw, String hFileRefsZNode,
18271835
StringBuilder sb) throws KeeperException {
1828-
sb.append("\n").append(hfileRefsZnode).append(": ");
1829-
for (String peerIdZnode : ZKUtil.listChildrenNoWatch(zkw, hfileRefsZnode)) {
1830-
String znodeToProcess = ZNodePaths.joinZNode(hfileRefsZnode, peerIdZnode);
1831-
sb.append("\n").append(znodeToProcess).append(": ");
1832-
List<String> peerHFileRefsZnodes = ZKUtil.listChildrenNoWatch(zkw, znodeToProcess);
1833-
int size = peerHFileRefsZnodes.size();
1834-
for (int i = 0; i < size; i++) {
1835-
sb.append(peerHFileRefsZnodes.get(i));
1836-
if (i != size - 1) {
1837-
sb.append(", ");
1836+
sb.append("\n").append(hFileRefsZNode).append(": ");
1837+
final List<String> hFileRefChildrenNoWatchList =
1838+
ZKUtil.listChildrenNoWatch(zkw, hFileRefsZNode);
1839+
if (hFileRefChildrenNoWatchList != null) {
1840+
for (String peerIdZNode : hFileRefChildrenNoWatchList) {
1841+
String zNodeToProcess = ZNodePaths.joinZNode(hFileRefsZNode, peerIdZNode);
1842+
sb.append("\n").append(zNodeToProcess).append(": ");
1843+
List<String> peerHFileRefsZNodes = ZKUtil.listChildrenNoWatch(zkw, zNodeToProcess);
1844+
if (peerHFileRefsZNodes != null) {
1845+
sb.append(String.join(", ", peerHFileRefsZNodes));
18381846
}
18391847
}
18401848
}
@@ -1946,10 +1954,10 @@ private static void appendPeerState(ZKWatcher zkw, String znodeToProcess, String
19461954
* @return The array of response strings.
19471955
* @throws IOException When the socket communication fails.
19481956
*/
1949-
public static String[] getServerStats(String server, int timeout)
1957+
private static String[] getServerStats(String server, int timeout)
19501958
throws IOException {
19511959
String[] sp = server.split(":");
1952-
if (sp == null || sp.length == 0) {
1960+
if (sp.length == 0) {
19531961
return null;
19541962
}
19551963

@@ -2085,7 +2093,7 @@ public static void logZKTree(ZKWatcher zkw, String root) {
20852093
* @see #logZKTree(ZKWatcher, String)
20862094
* @throws KeeperException if an unexpected exception occurs
20872095
*/
2088-
protected static void logZKTree(ZKWatcher zkw, String root, String prefix)
2096+
private static void logZKTree(ZKWatcher zkw, String root, String prefix)
20892097
throws KeeperException {
20902098
List<String> children = ZKUtil.listChildrenNoWatch(zkw, root);
20912099

0 commit comments

Comments
 (0)