Skip to content

Commit 23fa363

Browse files
authored
HBASE-28563 Closing ZooKeeper in ZKMainServer (#5869)
Signed-off-by: Duo Zhang <[email protected]> Reviewed-by: Andor Molnár <[email protected]>
1 parent 2a7aa0d commit 23fa363

File tree

1 file changed

+24
-7
lines changed

1 file changed

+24
-7
lines changed

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

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@
1717
*/
1818
package org.apache.hadoop.hbase.zookeeper;
1919

20+
import java.io.Closeable;
2021
import java.io.IOException;
2122
import org.apache.hadoop.conf.Configuration;
2223
import org.apache.hadoop.hbase.HBaseConfiguration;
2324
import org.apache.hadoop.hbase.HBaseInterfaceAudience;
25+
import org.apache.hadoop.hbase.ZooKeeperConnectionException;
2426
import org.apache.yetus.audience.InterfaceAudience;
2527
import org.apache.zookeeper.ZooKeeperMain;
2628
import org.apache.zookeeper.cli.CliException;
@@ -38,18 +40,23 @@ public String parse(final Configuration c) {
3840
}
3941

4042
/**
41-
* ZooKeeper 3.4.6 broke being able to pass commands on command line. See ZOOKEEPER-1897. This
42-
* class is a hack to restore this faclity.
43+
* ZooKeeper 3.4.6 broke being able to pass commands on command line. See ZOOKEEPER-1897,
44+
* ZOOKEEPER-4804. This class is a hack to restore this faclity.
4345
*/
44-
private static class HACK_UNTIL_ZOOKEEPER_1897_ZooKeeperMain extends ZooKeeperMain {
46+
private static class HACK_UNTIL_ZOOKEEPER_1897_ZooKeeperMain extends ZooKeeperMain
47+
implements Closeable {
4548
public HACK_UNTIL_ZOOKEEPER_1897_ZooKeeperMain(String[] args)
4649
throws IOException, InterruptedException {
4750
super(args);
4851
// Make sure we are connected before we proceed. Can take a while on some systems. If we
4952
// run the command without being connected, we get ConnectionLoss KeeperErrorConnection...
5053
// Make it 30seconds. We dont' have a config in this context and zk doesn't have
5154
// a timeout until after connection. 30000ms is default for zk.
52-
ZooKeeperHelper.ensureConnectedZooKeeper(this.zk, 30000);
55+
try {
56+
ZooKeeperHelper.ensureConnectedZooKeeper(this.zk, 30000);
57+
} catch (ZooKeeperConnectionException e) {
58+
this.zk.close();
59+
}
5360
}
5461

5562
/**
@@ -62,6 +69,15 @@ void runCmdLine() throws IOException, InterruptedException, CliException {
6269
processCmd(this.cl);
6370
System.exit(0);
6471
}
72+
73+
@Override
74+
public void close() throws IOException {
75+
try {
76+
this.zk.close();
77+
} catch (InterruptedException e) {
78+
Thread.currentThread().interrupt();
79+
}
80+
}
6581
}
6682

6783
/**
@@ -109,9 +125,10 @@ public static void main(String[] args) throws Exception {
109125
// ZOOKEEPER-1897 was committed to zookeeper-3.4.6 but elsewhere in this class we say
110126
// 3.4.6 breaks command-processing; TODO.
111127
if (hasCommandLineArguments(args)) {
112-
HACK_UNTIL_ZOOKEEPER_1897_ZooKeeperMain zkm =
113-
new HACK_UNTIL_ZOOKEEPER_1897_ZooKeeperMain(newArgs);
114-
zkm.runCmdLine();
128+
try (HACK_UNTIL_ZOOKEEPER_1897_ZooKeeperMain zkm =
129+
new HACK_UNTIL_ZOOKEEPER_1897_ZooKeeperMain(newArgs)) {
130+
zkm.runCmdLine();
131+
}
115132
} else {
116133
ZooKeeperMain.main(newArgs);
117134
}

0 commit comments

Comments
 (0)