Skip to content

Commit d4f555f

Browse files
authored
ZOOKEEPER-4963: Add ZooKeeper::builder to replace new ZooKeeperBuilder
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 Reviewers: anmolnar Author: kezhuw Closes #2301 from kezhuw/ZOOKEEPER-4963-ZooKeeper_builder
1 parent 8b13615 commit d4f555f

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
@@ -418,6 +418,25 @@ public boolean isConnected() {
418418
}
419419
}
420420

421+
/**
422+
* Creates a builder with given connect string and session timeout.
423+
*
424+
* @param connectString
425+
* comma separated host:port pairs, each corresponding to a zk
426+
* server. e.g. "127.0.0.1:3000,127.0.0.1:3001,127.0.0.1:3002"
427+
* If the optional chroot suffix is used the example would look
428+
* like: "127.0.0.1:3000,127.0.0.1:3001,127.0.0.1:3002/app/a"
429+
* where the client would be rooted at "/app/a" and all paths
430+
* would be relative to this root - ie getting/setting/etc...
431+
* "/foo/bar" would result in operations being run on
432+
* "/app/a/foo/bar" (from the server perspective).
433+
* @param sessionTimeout
434+
* session timeout
435+
*/
436+
public static ZooKeeperBuilder builder(String connectString, Duration sessionTimeout) {
437+
return new ZooKeeperBuilder(connectString, sessionTimeout);
438+
}
439+
421440
/**
422441
* To create a ZooKeeper client object, the application needs to pass a
423442
* connection string containing a comma separated list of host:port pairs,
@@ -460,9 +479,11 @@ public boolean isConnected() {
460479
* in cases of network failure
461480
* @throws IllegalArgumentException
462481
* if an invalid chroot path is specified
482+
*
483+
* @see #builder(String, Duration) for builder style construction
463484
*/
464485
public ZooKeeper(String connectString, int sessionTimeout, Watcher watcher) throws IOException {
465-
this(new ZooKeeperBuilder(connectString, Duration.ofMillis(sessionTimeout))
486+
this(builder(connectString, Duration.ofMillis(sessionTimeout))
466487
.withDefaultWatcher(watcher)
467488
.toOptions());
468489
}
@@ -511,13 +532,15 @@ public ZooKeeper(String connectString, int sessionTimeout, Watcher watcher) thro
511532
* in cases of network failure
512533
* @throws IllegalArgumentException
513534
* if an invalid chroot path is specified
535+
*
536+
* @see #builder(String, Duration) for builder style construction
514537
*/
515538
public ZooKeeper(
516539
String connectString,
517540
int sessionTimeout,
518541
Watcher watcher,
519542
ZKClientConfig conf) throws IOException {
520-
this(new ZooKeeperBuilder(connectString, Duration.ofMillis(sessionTimeout))
543+
this(builder(connectString, Duration.ofMillis(sessionTimeout))
521544
.withDefaultWatcher(watcher)
522545
.withClientConfig(conf)
523546
.toOptions());
@@ -579,14 +602,16 @@ public ZooKeeper(
579602
* in cases of network failure
580603
* @throws IllegalArgumentException
581604
* if an invalid chroot path is specified
605+
*
606+
* @see #builder(String, Duration) for builder style construction
582607
*/
583608
public ZooKeeper(
584609
String connectString,
585610
int sessionTimeout,
586611
Watcher watcher,
587612
boolean canBeReadOnly,
588613
HostProvider aHostProvider) throws IOException {
589-
this(new ZooKeeperBuilder(connectString, Duration.ofMillis(sessionTimeout))
614+
this(builder(connectString, Duration.ofMillis(sessionTimeout))
590615
.withDefaultWatcher(watcher)
591616
.withCanBeReadOnly(canBeReadOnly)
592617
.withHostProvider(ignored -> aHostProvider)
@@ -651,6 +676,8 @@ public ZooKeeper(
651676
* in cases of network failure
652677
* @throws IllegalArgumentException
653678
* if an invalid chroot path is specified
679+
*
680+
* @see #builder(String, Duration) for builder style construction
654681
*/
655682
public ZooKeeper(
656683
String connectString,
@@ -660,7 +687,7 @@ public ZooKeeper(
660687
HostProvider hostProvider,
661688
ZKClientConfig clientConfig
662689
) throws IOException {
663-
this(new ZooKeeperBuilder(connectString, Duration.ofMillis(sessionTimeout))
690+
this(builder(connectString, Duration.ofMillis(sessionTimeout))
664691
.withDefaultWatcher(watcher)
665692
.withCanBeReadOnly(canBeReadOnly)
666693
.withHostProvider(ignored -> hostProvider)
@@ -740,13 +767,15 @@ ClientCnxn createConnection(
740767
* in cases of network failure
741768
* @throws IllegalArgumentException
742769
* if an invalid chroot path is specified
770+
*
771+
* @see #builder(String, Duration) for builder style construction
743772
*/
744773
public ZooKeeper(
745774
String connectString,
746775
int sessionTimeout,
747776
Watcher watcher,
748777
boolean canBeReadOnly) throws IOException {
749-
this(new ZooKeeperBuilder(connectString, Duration.ofMillis(sessionTimeout))
778+
this(builder(connectString, Duration.ofMillis(sessionTimeout))
750779
.withDefaultWatcher(watcher)
751780
.withCanBeReadOnly(canBeReadOnly)
752781
.toOptions());
@@ -805,14 +834,16 @@ public ZooKeeper(
805834
* in cases of network failure
806835
* @throws IllegalArgumentException
807836
* if an invalid chroot path is specified
837+
*
838+
* @see #builder(String, Duration) for builder style construction
808839
*/
809840
public ZooKeeper(
810841
String connectString,
811842
int sessionTimeout,
812843
Watcher watcher,
813844
boolean canBeReadOnly,
814845
ZKClientConfig conf) throws IOException {
815-
this(new ZooKeeperBuilder(connectString, Duration.ofMillis(sessionTimeout))
846+
this(builder(connectString, Duration.ofMillis(sessionTimeout))
816847
.withDefaultWatcher(watcher)
817848
.withCanBeReadOnly(canBeReadOnly)
818849
.withClientConfig(conf)
@@ -870,14 +901,16 @@ public ZooKeeper(
870901
* @throws IOException in cases of network failure
871902
* @throws IllegalArgumentException if an invalid chroot path is specified
872903
* @throws IllegalArgumentException for an invalid list of ZooKeeper hosts
904+
*
905+
* @see #builder(String, Duration) for builder style construction
873906
*/
874907
public ZooKeeper(
875908
String connectString,
876909
int sessionTimeout,
877910
Watcher watcher,
878911
long sessionId,
879912
byte[] sessionPasswd) throws IOException {
880-
this(new ZooKeeperBuilder(connectString, Duration.ofMillis(sessionTimeout))
913+
this(builder(connectString, Duration.ofMillis(sessionTimeout))
881914
.withDefaultWatcher(watcher)
882915
.withSession(sessionId, sessionPasswd)
883916
.toOptions());
@@ -946,6 +979,8 @@ public ZooKeeper(
946979
* use this as HostProvider to enable custom behaviour.
947980
* @throws IOException in cases of network failure
948981
* @throws IllegalArgumentException if an invalid chroot path is specified
982+
*
983+
* @see #builder(String, Duration) for builder style construction
949984
*/
950985
public ZooKeeper(
951986
String connectString,
@@ -955,7 +990,7 @@ public ZooKeeper(
955990
byte[] sessionPasswd,
956991
boolean canBeReadOnly,
957992
HostProvider aHostProvider) throws IOException {
958-
this(new ZooKeeperBuilder(connectString, Duration.ofMillis(sessionTimeout))
993+
this(builder(connectString, Duration.ofMillis(sessionTimeout))
959994
.withDefaultWatcher(watcher)
960995
.withSession(sessionId, sessionPasswd)
961996
.withCanBeReadOnly(canBeReadOnly)
@@ -1031,6 +1066,8 @@ public ZooKeeper(
10311066
* @throws IllegalArgumentException if an invalid chroot path is specified
10321067
*
10331068
* @since 3.5.5
1069+
*
1070+
* @see #builder(String, Duration) for builder style construction
10341071
*/
10351072
public ZooKeeper(
10361073
String connectString,
@@ -1041,7 +1078,7 @@ public ZooKeeper(
10411078
boolean canBeReadOnly,
10421079
HostProvider hostProvider,
10431080
ZKClientConfig clientConfig) throws IOException {
1044-
this(new ZooKeeperBuilder(connectString, Duration.ofMillis(sessionTimeout))
1081+
this(builder(connectString, Duration.ofMillis(sessionTimeout))
10451082
.withSession(sessionId, sessionPasswd)
10461083
.withDefaultWatcher(watcher)
10471084
.withCanBeReadOnly(canBeReadOnly)
@@ -1053,6 +1090,8 @@ public ZooKeeper(
10531090
/**
10541091
* Create a ZooKeeper client and establish session asynchronously.
10551092
*
1093+
* <p>This is private and export for internal usage.
1094+
*
10561095
* <p>This constructor will initiate connection to the server and return
10571096
* immediately - potentially (usually) before the session is fully established.
10581097
* The watcher from options will be notified of any changes in state. This
@@ -1180,6 +1219,8 @@ public ZooKeeper(ZooKeeperOptions options) throws IOException {
11801219
* majority in the background.
11811220
* @throws IOException in cases of network failure
11821221
* @throws IllegalArgumentException if an invalid chroot path is specified
1222+
*
1223+
* @see #builder(String, Duration) for builder style construction
11831224
*/
11841225
public ZooKeeper(
11851226
String connectString,
@@ -1188,7 +1229,7 @@ public ZooKeeper(
11881229
long sessionId,
11891230
byte[] sessionPasswd,
11901231
boolean canBeReadOnly) throws IOException {
1191-
this(new ZooKeeperBuilder(connectString, Duration.ofMillis(sessionTimeout))
1232+
this(builder(connectString, Duration.ofMillis(sessionTimeout))
11921233
.withDefaultWatcher(watcher)
11931234
.withSession(sessionId, sessionPasswd)
11941235
.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)