Skip to content

Commit

Permalink
Implement error handling for ZooKeeperConnectionException in constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
Minwoo Kang committed May 5, 2024
1 parent 9581764 commit a9686ae
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ private ZooKeeperHelper() {
* timeout to wait on connection establishment.
*/
public static ZooKeeper getConnectedZooKeeper(String connectString, int sessionTimeoutMs)
throws IOException, InterruptedException {
throws IOException {
ZooKeeper zookeeper = new ZooKeeper(connectString, sessionTimeoutMs, e -> {
});
return ensureConnectedZooKeeper(zookeeper, sessionTimeoutMs);
Expand All @@ -52,7 +52,7 @@ public static ZooKeeper getConnectedZooKeeper(String connectString, int sessionT
* @param timeout Time to wait on established Connection
*/
public static ZooKeeper ensureConnectedZooKeeper(ZooKeeper zookeeper, int timeout)
throws ZooKeeperConnectionException, InterruptedException {
throws ZooKeeperConnectionException {
if (zookeeper.getState().isConnected()) {
return zookeeper;
}
Expand All @@ -61,7 +61,6 @@ public static ZooKeeper ensureConnectedZooKeeper(ZooKeeper zookeeper, int timeou
while (!zookeeper.getState().isConnected()) {
Threads.sleep(1);
if (stopWatch.elapsed(TimeUnit.MILLISECONDS) > timeout) {
zookeeper.close();
throw new ZooKeeperConnectionException("Failed connect after waiting "
+ stopWatch.elapsed(TimeUnit.MILLISECONDS) + "ms (zk session timeout); " + zookeeper);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
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;
Expand Down Expand Up @@ -51,7 +52,11 @@ public HACK_UNTIL_ZOOKEEPER_1897_ZooKeeperMain(String[] args)
// 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();
}
}

/**
Expand Down

0 comments on commit a9686ae

Please sign in to comment.