This is an automated email from the ASF dual-hosted git repository. zhangduo pushed a commit to branch branch-2.6 in repository https://gitbox.apache.org/repos/asf/hbase.git
commit 935e532e57aa62b250098d2b66c845ac4c63027c Author: Minwoo Kang <minwoo.k...@navercorp.com> AuthorDate: Wed May 8 16:34:03 2024 +0900 HBASE-28563 Closing ZooKeeper in ZKMainServer (#5869) Signed-off-by: Duo Zhang <zhang...@apache.org> Reviewed-by: Andor Molnár <an...@apache.org> (cherry picked from commit 23fa363d360202db73a0d1b517e29841a904026c) --- .../hadoop/hbase/zookeeper/ZKMainServer.java | 31 +++++++++++++++++----- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/hbase-zookeeper/src/main/java/org/apache/hadoop/hbase/zookeeper/ZKMainServer.java b/hbase-zookeeper/src/main/java/org/apache/hadoop/hbase/zookeeper/ZKMainServer.java index 623085b5923..ce849fea1a4 100644 --- a/hbase-zookeeper/src/main/java/org/apache/hadoop/hbase/zookeeper/ZKMainServer.java +++ b/hbase-zookeeper/src/main/java/org/apache/hadoop/hbase/zookeeper/ZKMainServer.java @@ -17,10 +17,12 @@ */ package org.apache.hadoop.hbase.zookeeper; +import java.io.Closeable; import java.io.IOException; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.HBaseInterfaceAudience; +import org.apache.hadoop.hbase.ZooKeeperConnectionException; import org.apache.yetus.audience.InterfaceAudience; import org.apache.zookeeper.ZooKeeperMain; import org.apache.zookeeper.cli.CliException; @@ -38,10 +40,11 @@ public class ZKMainServer { } /** - * ZooKeeper 3.4.6 broke being able to pass commands on command line. See ZOOKEEPER-1897. This - * class is a hack to restore this faclity. + * ZooKeeper 3.4.6 broke being able to pass commands on command line. See ZOOKEEPER-1897, + * ZOOKEEPER-4804. This class is a hack to restore this faclity. */ - private static class HACK_UNTIL_ZOOKEEPER_1897_ZooKeeperMain extends ZooKeeperMain { + private static class HACK_UNTIL_ZOOKEEPER_1897_ZooKeeperMain extends ZooKeeperMain + implements Closeable { public HACK_UNTIL_ZOOKEEPER_1897_ZooKeeperMain(String[] args) throws IOException, InterruptedException { super(args); @@ -49,7 +52,11 @@ public class ZKMainServer { // run the command without being connected, we get ConnectionLoss KeeperErrorConnection... // Make it 30seconds. We dont' have a config in this context and zk doesn't have // a timeout until after connection. 30000ms is default for zk. - ZooKeeperHelper.ensureConnectedZooKeeper(this.zk, 30000); + try { + ZooKeeperHelper.ensureConnectedZooKeeper(this.zk, 30000); + } catch (ZooKeeperConnectionException e) { + this.zk.close(); + } } /** @@ -62,6 +69,15 @@ public class ZKMainServer { processCmd(this.cl); System.exit(0); } + + @Override + public void close() throws IOException { + try { + this.zk.close(); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + } } /** @@ -109,9 +125,10 @@ public class ZKMainServer { // ZOOKEEPER-1897 was committed to zookeeper-3.4.6 but elsewhere in this class we say // 3.4.6 breaks command-processing; TODO. if (hasCommandLineArguments(args)) { - HACK_UNTIL_ZOOKEEPER_1897_ZooKeeperMain zkm = - new HACK_UNTIL_ZOOKEEPER_1897_ZooKeeperMain(newArgs); - zkm.runCmdLine(); + try (HACK_UNTIL_ZOOKEEPER_1897_ZooKeeperMain zkm = + new HACK_UNTIL_ZOOKEEPER_1897_ZooKeeperMain(newArgs)) { + zkm.runCmdLine(); + } } else { ZooKeeperMain.main(newArgs); }