Skip to content

Commit 90ec94e

Browse files
committed
ZOOKEEPER-4963: Add ZooKeeper::builder to replace new ZooKeeperBuilder
`ZooKeeper::builder` should be better: 1. More exposure chance as a newly introduce method in class `ZooKeeper`. 2. No need to import or remember the newly introduced class as most `ZooKeeper` instances could be built in one chain. `ZooKeeperBuilder` is introduced in 3.10.0 so it safe to do this. Refs: ZOOKEEPER-4697
1 parent 770804b commit 90ec94e

File tree

3 files changed

+59
-26
lines changed

3 files changed

+59
-26
lines changed

zookeeper-server/src/main/java/org/apache/zookeeper/ZooKeeper.java

Lines changed: 51 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,25 @@ public boolean isConnected() {
419419
}
420420
}
421421

422+
/**
423+
* Creates a builder with given connect string and session timeout.
424+
*
425+
* @param connectString
426+
* comma separated host:port pairs, each corresponding to a zk
427+
* server. e.g. "127.0.0.1:3000,127.0.0.1:3001,127.0.0.1:3002"
428+
* If the optional chroot suffix is used the example would look
429+
* like: "127.0.0.1:3000,127.0.0.1:3001,127.0.0.1:3002/app/a"
430+
* where the client would be rooted at "/app/a" and all paths
431+
* would be relative to this root - ie getting/setting/etc...
432+
* "/foo/bar" would result in operations being run on
433+
* "/app/a/foo/bar" (from the server perspective).
434+
* @param sessionTimeout
435+
* session timeout
436+
*/
437+
public static ZooKeeperBuilder builder(String connectString, Duration sessionTimeout) {
438+
return new ZooKeeperBuilder(connectString, sessionTimeout);
439+
}
440+
422441
/**
423442
* To create a ZooKeeper client object, the application needs to pass a
424443
* connection string containing a comma separated list of host:port pairs,
@@ -461,9 +480,11 @@ public boolean isConnected() {
461480
* in cases of network failure
462481
* @throws IllegalArgumentException
463482
* if an invalid chroot path is specified
483+
*
484+
* @see #builder(String, Duration) for builder style construction
464485
*/
465486
public ZooKeeper(String connectString, int sessionTimeout, Watcher watcher) throws IOException {
466-
this(new ZooKeeperBuilder(connectString, Duration.ofMillis(sessionTimeout))
487+
this(builder(connectString, Duration.ofMillis(sessionTimeout))
467488
.withDefaultWatcher(watcher)
468489
.toOptions());
469490
}
@@ -512,13 +533,15 @@ public ZooKeeper(String connectString, int sessionTimeout, Watcher watcher) thro
512533
* in cases of network failure
513534
* @throws IllegalArgumentException
514535
* if an invalid chroot path is specified
536+
*
537+
* @see #builder(String, Duration) for builder style construction
515538
*/
516539
public ZooKeeper(
517540
String connectString,
518541
int sessionTimeout,
519542
Watcher watcher,
520543
ZKClientConfig conf) throws IOException {
521-
this(new ZooKeeperBuilder(connectString, Duration.ofMillis(sessionTimeout))
544+
this(builder(connectString, Duration.ofMillis(sessionTimeout))
522545
.withDefaultWatcher(watcher)
523546
.withClientConfig(conf)
524547
.toOptions());
@@ -580,14 +603,16 @@ public ZooKeeper(
580603
* in cases of network failure
581604
* @throws IllegalArgumentException
582605
* if an invalid chroot path is specified
606+
*
607+
* @see #builder(String, Duration) for builder style construction
583608
*/
584609
public ZooKeeper(
585610
String connectString,
586611
int sessionTimeout,
587612
Watcher watcher,
588613
boolean canBeReadOnly,
589614
HostProvider aHostProvider) throws IOException {
590-
this(new ZooKeeperBuilder(connectString, Duration.ofMillis(sessionTimeout))
615+
this(builder(connectString, Duration.ofMillis(sessionTimeout))
591616
.withDefaultWatcher(watcher)
592617
.withCanBeReadOnly(canBeReadOnly)
593618
.withHostProvider(ignored -> aHostProvider)
@@ -652,6 +677,8 @@ public ZooKeeper(
652677
* in cases of network failure
653678
* @throws IllegalArgumentException
654679
* if an invalid chroot path is specified
680+
*
681+
* @see #builder(String, Duration) for builder style construction
655682
*/
656683
public ZooKeeper(
657684
String connectString,
@@ -661,7 +688,7 @@ public ZooKeeper(
661688
HostProvider hostProvider,
662689
ZKClientConfig clientConfig
663690
) throws IOException {
664-
this(new ZooKeeperBuilder(connectString, Duration.ofMillis(sessionTimeout))
691+
this(builder(connectString, Duration.ofMillis(sessionTimeout))
665692
.withDefaultWatcher(watcher)
666693
.withCanBeReadOnly(canBeReadOnly)
667694
.withHostProvider(ignored -> hostProvider)
@@ -741,13 +768,15 @@ ClientCnxn createConnection(
741768
* in cases of network failure
742769
* @throws IllegalArgumentException
743770
* if an invalid chroot path is specified
771+
*
772+
* @see #builder(String, Duration) for builder style construction
744773
*/
745774
public ZooKeeper(
746775
String connectString,
747776
int sessionTimeout,
748777
Watcher watcher,
749778
boolean canBeReadOnly) throws IOException {
750-
this(new ZooKeeperBuilder(connectString, Duration.ofMillis(sessionTimeout))
779+
this(builder(connectString, Duration.ofMillis(sessionTimeout))
751780
.withDefaultWatcher(watcher)
752781
.withCanBeReadOnly(canBeReadOnly)
753782
.toOptions());
@@ -806,14 +835,16 @@ public ZooKeeper(
806835
* in cases of network failure
807836
* @throws IllegalArgumentException
808837
* if an invalid chroot path is specified
838+
*
839+
* @see #builder(String, Duration) for builder style construction
809840
*/
810841
public ZooKeeper(
811842
String connectString,
812843
int sessionTimeout,
813844
Watcher watcher,
814845
boolean canBeReadOnly,
815846
ZKClientConfig conf) throws IOException {
816-
this(new ZooKeeperBuilder(connectString, Duration.ofMillis(sessionTimeout))
847+
this(builder(connectString, Duration.ofMillis(sessionTimeout))
817848
.withDefaultWatcher(watcher)
818849
.withCanBeReadOnly(canBeReadOnly)
819850
.withClientConfig(conf)
@@ -871,14 +902,16 @@ public ZooKeeper(
871902
* @throws IOException in cases of network failure
872903
* @throws IllegalArgumentException if an invalid chroot path is specified
873904
* @throws IllegalArgumentException for an invalid list of ZooKeeper hosts
905+
*
906+
* @see #builder(String, Duration) for builder style construction
874907
*/
875908
public ZooKeeper(
876909
String connectString,
877910
int sessionTimeout,
878911
Watcher watcher,
879912
long sessionId,
880913
byte[] sessionPasswd) throws IOException {
881-
this(new ZooKeeperBuilder(connectString, Duration.ofMillis(sessionTimeout))
914+
this(builder(connectString, Duration.ofMillis(sessionTimeout))
882915
.withDefaultWatcher(watcher)
883916
.withSession(sessionId, sessionPasswd)
884917
.toOptions());
@@ -947,6 +980,8 @@ public ZooKeeper(
947980
* use this as HostProvider to enable custom behaviour.
948981
* @throws IOException in cases of network failure
949982
* @throws IllegalArgumentException if an invalid chroot path is specified
983+
*
984+
* @see #builder(String, Duration) for builder style construction
950985
*/
951986
public ZooKeeper(
952987
String connectString,
@@ -956,7 +991,7 @@ public ZooKeeper(
956991
byte[] sessionPasswd,
957992
boolean canBeReadOnly,
958993
HostProvider aHostProvider) throws IOException {
959-
this(new ZooKeeperBuilder(connectString, Duration.ofMillis(sessionTimeout))
994+
this(builder(connectString, Duration.ofMillis(sessionTimeout))
960995
.withDefaultWatcher(watcher)
961996
.withSession(sessionId, sessionPasswd)
962997
.withCanBeReadOnly(canBeReadOnly)
@@ -1032,6 +1067,8 @@ public ZooKeeper(
10321067
* @throws IllegalArgumentException if an invalid chroot path is specified
10331068
*
10341069
* @since 3.5.5
1070+
*
1071+
* @see #builder(String, Duration) for builder style construction
10351072
*/
10361073
public ZooKeeper(
10371074
String connectString,
@@ -1042,7 +1079,7 @@ public ZooKeeper(
10421079
boolean canBeReadOnly,
10431080
HostProvider hostProvider,
10441081
ZKClientConfig clientConfig) throws IOException {
1045-
this(new ZooKeeperBuilder(connectString, Duration.ofMillis(sessionTimeout))
1082+
this(builder(connectString, Duration.ofMillis(sessionTimeout))
10461083
.withSession(sessionId, sessionPasswd)
10471084
.withDefaultWatcher(watcher)
10481085
.withCanBeReadOnly(canBeReadOnly)
@@ -1054,6 +1091,8 @@ public ZooKeeper(
10541091
/**
10551092
* Create a ZooKeeper client and establish session asynchronously.
10561093
*
1094+
* <p>This is private and export for internal usage.
1095+
*
10571096
* <p>This constructor will initiate connection to the server and return
10581097
* immediately - potentially (usually) before the session is fully established.
10591098
* The watcher from options will be notified of any changes in state. This
@@ -1181,6 +1220,8 @@ public ZooKeeper(ZooKeeperOptions options) throws IOException {
11811220
* majority in the background.
11821221
* @throws IOException in cases of network failure
11831222
* @throws IllegalArgumentException if an invalid chroot path is specified
1223+
*
1224+
* @see #builder(String, Duration) for builder style construction
11841225
*/
11851226
public ZooKeeper(
11861227
String connectString,
@@ -1189,7 +1230,7 @@ public ZooKeeper(
11891230
long sessionId,
11901231
byte[] sessionPasswd,
11911232
boolean canBeReadOnly) throws IOException {
1192-
this(new ZooKeeperBuilder(connectString, Duration.ofMillis(sessionTimeout))
1233+
this(builder(connectString, Duration.ofMillis(sessionTimeout))
11931234
.withDefaultWatcher(watcher)
11941235
.withSession(sessionId, sessionPasswd)
11951236
.withCanBeReadOnly(canBeReadOnly)

zookeeper-server/src/main/java/org/apache/zookeeper/client/ZooKeeperBuilder.java

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -47,22 +47,11 @@ public class ZooKeeperBuilder {
4747
private ZKClientConfig clientConfig;
4848

4949
/**
50-
* Creates a builder with given connect string and session timeout.
51-
*
52-
* @param connectString
53-
* comma separated host:port pairs, each corresponding to a zk
54-
* server. e.g. "127.0.0.1:3000,127.0.0.1:3001,127.0.0.1:3002"
55-
* If the optional chroot suffix is used the example would look
56-
* like: "127.0.0.1:3000,127.0.0.1:3001,127.0.0.1:3002/app/a"
57-
* where the client would be rooted at "/app/a" and all paths
58-
* would be relative to this root - ie getting/setting/etc...
59-
* "/foo/bar" would result in operations being run on
60-
* "/app/a/foo/bar" (from the server perspective).
61-
* @param sessionTimeout
62-
* session timeout
50+
* This is private and export for internal usage. Use {@link ZooKeeper#builder(String, Duration)} instead.
6351
*/
52+
@InterfaceAudience.Private
6453
public ZooKeeperBuilder(String connectString, Duration sessionTimeout) {
65-
this.connectString = connectString;
54+
this.connectString = Objects.requireNonNull(connectString, "connect string must not be null");
6655
this.sessionTimeout = Objects.requireNonNull(sessionTimeout, "session timeout must not be null");
6756
}
6857

@@ -145,6 +134,8 @@ public ZooKeeperBuilder withClientConfig(ZKClientConfig clientConfig) {
145134
/**
146135
* Creates a {@link ZooKeeperOptions} with configured options.
147136
*
137+
* <p>This is private and export for internal usage.
138+
*
148139
* @apiNote helper to delegate existing constructors to {@link ZooKeeper#ZooKeeper(ZooKeeperOptions)}
149140
*/
150141
@InterfaceAudience.Private

zookeeper-server/src/test/java/org/apache/zookeeper/client/ZooKeeperBuilderTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.apache.zookeeper.Watcher;
3030
import org.apache.zookeeper.ZooDefs;
3131
import org.apache.zookeeper.ZooKeeper;
32+
import org.apache.zookeeper.admin.ZooKeeperAdmin;
3233
import org.apache.zookeeper.common.Time;
3334
import org.apache.zookeeper.test.ClientBase;
3435
import org.junit.jupiter.api.Test;
@@ -70,7 +71,7 @@ private void testClient(BlockingQueue<WatchedEvent> events, ZooKeeper zk) throws
7071
@Test
7172
public void testBuildClient() throws Exception {
7273
BlockingQueue<WatchedEvent> events = new LinkedBlockingQueue<>();
73-
ZooKeeper zk = new ZooKeeperBuilder(hostPort, Duration.ofMillis(1000))
74+
ZooKeeper zk = ZooKeeper.builder(hostPort, Duration.ofMillis(1000))
7475
.withDefaultWatcher(events::offer)
7576
.build();
7677
testClient(events, zk);
@@ -79,7 +80,7 @@ public void testBuildClient() throws Exception {
7980
@Test
8081
public void testBuildAdminClient() throws Exception {
8182
BlockingQueue<WatchedEvent> events = new LinkedBlockingQueue<>();
82-
ZooKeeper zk = new ZooKeeperBuilder(hostPort, Duration.ofMillis(1000))
83+
ZooKeeperAdmin zk = ZooKeeper.builder(hostPort, Duration.ofMillis(1000))
8384
.withDefaultWatcher(events::offer)
8485
.buildAdmin();
8586
testClient(events, zk);

0 commit comments

Comments
 (0)