Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ZOOKEEPER-1871: Add an option to zkCli to wait for connection before executing commands #1621

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
import java.util.Map;
import java.util.Map.Entry;
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 org.apache.yetus.audience.InterfaceAudience;
Expand Down Expand Up @@ -89,6 +92,7 @@ public class ZooKeeperMain {

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

public boolean getPrintWatches() {
return printWatches;
Expand Down Expand Up @@ -146,6 +150,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 @@ -208,6 +219,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 @@ -289,6 +302,17 @@ protected void connectToZK(String newHost) throws InterruptedException, IOExcept
System.setProperty(ZKClientConfig.SECURE_CLIENT, "true");
System.out.println("Secure connection is enabled");
}
if (cl.getOption("waitforconnection") != null) {
connectLatch = new CountDownLatch(1);
}
int timeout = Integer.parseInt(cl.getOption("timeout"));
zk = new ZooKeeperAdmin(host, timeout, new MyWatcher(),readOnly);
if (connectLatch != null) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please add more clarifications about the difference between waitforconnection and timeout in the GitHub description?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have given some clarification on waitforconnection and timeout options in GitHub description. Thanks

if (!connectLatch.await(timeout, TimeUnit.MILLISECONDS)) {
zk.close();
throw new IOException(KeeperException.create(KeeperException.Code.CONNECTIONLOSS));
}
}

ZKClientConfig clientConfig = null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import org.apache.zookeeper.data.Id;
import org.apache.zookeeper.data.Stat;
import org.apache.zookeeper.test.ClientBase;
import org.apache.zookeeper.PortAssignment;
import org.junit.Test;

/**
Expand Down Expand Up @@ -685,4 +686,25 @@ public void testSyncCommand() throws Exception {
runCommandExpect(cmd, expected);
}

@Test
public void testWaitForConnection() throws Exception {
// get a wrong port number
int invalid_port = PortAssignment.unique();
long timeout = 3000L; // millisecond
String[] args1 = {"-server", "localhost:" + invalid_port, "-timeout",
Long.toString(timeout), "-waitforconnection", "ls", "/"};
long start_time = System.currentTimeMillis();
// try to connect to a non-existing server so as to wait until wait_timeout
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

using CamelCase naming style(e.g.: startTime) in the UT

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Used CamelCase naming style. Thanks @maoling for pointing out

try{
ZooKeeperMain zkMain = new ZooKeeperMain(args1);
fail("IOException was expected");
}catch (IOException e) {
// do nothing
}
long end_time = System.currentTimeMillis();
assertTrue("ZooKeeeperMain does not wait until the specified timeout",
end_time - start_time >= timeout);

}

}