Skip to content

Commit

Permalink
ZOOKEEPER-1871: Add an option to zkCli to wait for connection before …
Browse files Browse the repository at this point in the history
…executing commands

-waitforconnection option will make zk client wait for -timeout time to connect to zk server.  timeout time is 30ms by default but can be specified explicitly for a session using -timeout option in command line.

Author: Mukti <[email protected]>

Reviewers: maoling <[email protected]>, Mohammad Arshad <[email protected]>

Closes apache#1626 from MuktiKrishnan/ZOOKEEPER-1871-master and squashes the following commits:

2947514 [Mukti] ZOOKEEPER-1871: Removed statement which was re-creating zookeeper admin in ZooKeeperMain.java and added documentation for waitforconnection in zookeeperCLI.md and
c475d46 [Mukti] ZOOKEEPER-1871: Add an option to zkCli to wailt for connection before executing commands
  • Loading branch information
MuktiKrishnan authored and RokLenarcic committed Sep 3, 2022
1 parent 0e3986a commit d0669e4
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
2 changes: 2 additions & 0 deletions zookeeper-docs/src/main/resources/markdown/zookeeperCLI.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ Enter into the ZooKeeper-cli
bin/zkCli.sh
# connect to the remote host with timeout:3s
bin/zkCli.sh -timeout 3000 -server remoteIP:2181
# connect to the remote host with -waitforconnection option to wait for connection success before executing commands
bin/zkCli.sh -waitforconnection -timeout 3000 -server remoteIP:2181
# connect with a custom client configuration properties file
bin/zkCli.sh -client-configuration /path/to/client.properties
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Stream;
Expand Down Expand Up @@ -68,6 +70,7 @@ public class ZooKeeperMain {

protected ZooKeeper zk;
protected String host = "";
private CountDownLatch connectLatch = null;

public boolean getPrintWatches() {
return printWatches;
Expand Down Expand Up @@ -106,6 +109,13 @@ public void process(WatchedEvent event) {
ZooKeeperMain.printMessage("WATCHER::");
ZooKeeperMain.printMessage(event.toString());
}
if (connectLatch != null) {
// connection success
if (event.getType() == Event.EventType.None
&& event.getState() == Event.KeeperState.SyncConnected) {
connectLatch.countDown();
}
}
}

}
Expand Down Expand Up @@ -168,6 +178,8 @@ public boolean parseOptions(String[] args) {
options.put("readonly", "true");
} else if (opt.equals("-client-configuration")) {
options.put("client-configuration", it.next());
} else if (opt.equals("-waitforconnection")) {
options.put("waitforconnection", "true");
}
} catch (NoSuchElementException e) {
System.err.println("Error: no argument found for option " + opt);
Expand Down Expand Up @@ -261,7 +273,19 @@ protected void connectToZK(String newHost) throws InterruptedException, IOExcept
}
}

zk = new ZooKeeperAdmin(host, Integer.parseInt(cl.getOption("timeout")), new MyWatcher(), readOnly, clientConfig);
if (cl.getOption("waitforconnection") != null) {
connectLatch = new CountDownLatch(1);
}

int timeout = Integer.parseInt(cl.getOption("timeout"));
zk = new ZooKeeperAdmin(host, timeout, new MyWatcher(), readOnly, clientConfig);
if (connectLatch != null) {
if (!connectLatch.await(timeout, TimeUnit.MILLISECONDS)) {
zk.close();
throw new IOException(KeeperException.create(KeeperException.Code.CONNECTIONLOSS));
}
}

}

public static void main(String[] args) throws IOException, InterruptedException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -774,4 +774,25 @@ private void assertClientAuthInfo(List<String> expected, String actual) {
});
}

@Test
public void testWaitForConnection() throws Exception {
// get a wrong port number
int invalidPort = PortAssignment.unique();
long timeout = 3000L; // millisecond
String[] args1 = {"-server", "localhost:" + invalidPort, "-timeout",
Long.toString(timeout), "-waitforconnection", "ls", "/"};
long startTime = System.currentTimeMillis();
// try to connect to a non-existing server so as to wait until wait_timeout
try {
ZooKeeperMain zkMain = new ZooKeeperMain(args1);
fail("IOException was expected");
} catch (IOException e) {
// do nothing
}
long endTime = System.currentTimeMillis();
assertTrue(endTime - startTime >= timeout,
"ZooKeeeperMain does not wait until the specified timeout");

}

}

0 comments on commit d0669e4

Please sign in to comment.