You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@zookeeper.apache.org by "kezhuw (via GitHub)" <gi...@apache.org> on 2023/05/31 10:14:26 UTC
[GitHub] [zookeeper] kezhuw commented on a diff in pull request #2001: ZOOKEEPER-4697: Add Builder to construct ZooKeeper and its derivations
kezhuw commented on code in PR #2001:
URL: https://github.com/apache/zookeeper/pull/2001#discussion_r1211468968
##########
zookeeper-server/src/main/java/org/apache/zookeeper/ZooKeeper.java:
##########
@@ -1025,20 +1028,71 @@ public ZooKeeper(
boolean canBeReadOnly,
HostProvider hostProvider,
ZKClientConfig clientConfig) throws IOException {
- LOG.info(
- "Initiating client connection, connectString={} "
- + "sessionTimeout={} watcher={} sessionId=0x{} sessionPasswd={}",
- connectString,
- sessionTimeout,
- watcher,
- Long.toHexString(sessionId),
- (sessionPasswd == null ? "<null>" : "<hidden>"));
+ this(new ZooKeeperBuilder(connectString, sessionTimeout)
+ .withSession(sessionId, sessionPasswd)
+ .withDefaultWatcher(watcher)
+ .withCanBeReadOnly(canBeReadOnly)
+ .withHostProvider(ignored -> hostProvider)
+ .withClientConfig(clientConfig)
+ .toOptions());
+ }
+ /**
+ * Create a ZooKeeper client and establish session asynchronously.
+ *
+ * <p>This constructor will initiate connection to the server and return
+ * immediately - potentially (usually) before the session is fully established.
+ * The watcher from options will be notified of any changes in state. This
+ * notification can come at any point before or after the constructor call
+ * has returned.
+ *
+ * <p>The instantiated ZooKeeper client object will pick an arbitrary server
+ * from the connect string and attempt to connect to it. If establishment of
+ * the connection fails, another server in the connect string will be tried
+ * (the order is non-deterministic, as we random shuffle the list), until a
+ * connection is established. The client will continue attempts until the
+ * session is explicitly closed (or the session is expired by the server).
+ *
+ * @param options options for ZooKeeper client
+ * @throws IOException in cases of IO failure
+ */
+ public ZooKeeper(ZooKeeperOptions options) throws IOException {
+ String connectString = options.getConnectString();
+ int sessionTimeout = options.getSessionTimeout();
+ long sessionId = options.getSessionId();
+ byte[] sessionPasswd = sessionId == 0 ? new byte[16] : options.getSessionPasswd();
+ Watcher watcher = options.getDefaultWatcher();
+ boolean canBeReadOnly = options.isCanBeReadOnly();
+
+ if (sessionId == 0) {
+ LOG.info(
+ "Initiating client connection, connectString={} sessionTimeout={} watcher={}",
+ connectString,
+ sessionTimeout,
+ watcher);
+ } else {
+ LOG.info(
+ "Initiating client connection, connectString={} "
+ + "sessionTimeout={} watcher={} sessionId=0x{} sessionPasswd={}",
+ connectString,
+ sessionTimeout,
+ watcher,
+ Long.toHexString(sessionId),
+ (sessionPasswd == null ? "<null>" : "<hidden>"));
+ }
+
+ ZKClientConfig clientConfig = options.getClientConfig();
this.clientConfig = clientConfig != null ? clientConfig : new ZKClientConfig();
ConnectStringParser connectStringParser = new ConnectStringParser(connectString);
+ HostProvider hostProvider;
+ if (options.getHostProvider() != null) {
+ hostProvider = options.getHostProvider().apply(connectStringParser.getServerAddresses());
+ } else {
+ hostProvider = new StaticHostProvider(connectStringParser.getServerAddresses());
Review Comment:
This is false negative. `ConnectStringParser.serverAddresses` is never `null` so does `connectStringParser.getServerAddresses()`.
@sonatype-lift ignore
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@zookeeper.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org