Skip to content

Commit 3bc3ea1

Browse files
authored
Add builder to construct InstanceSpec (#1275)
It is hard for us to know what does `new InstanceSpec(null, -1, -1, -1, true, -1, -1, -1)` means now. Closes #1222.
1 parent 0b549b7 commit 3bc3ea1

5 files changed

Lines changed: 175 additions & 8 deletions

File tree

curator-test/src/main/java/org/apache/curator/test/InstanceSpec.java

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,15 @@ public static void reset() {
7070
nextServerId.set(1);
7171
}
7272

73+
/**
74+
* Create a builder to construct {@link InstanceSpec}.
75+
*/
76+
public static InstanceSpecBuilder builder() {
77+
return new InstanceSpecBuilder();
78+
}
79+
7380
public static InstanceSpec newInstanceSpec() {
74-
return new InstanceSpec(null, -1, -1, -1, true, -1, -1, -1);
81+
return builder().build();
7582
}
7683

7784
public static int getRandomPort() {
@@ -94,13 +101,16 @@ public static int getRandomPort() {
94101
}
95102

96103
/**
104+
* @deprecated Use {@link #builder()} instead.
105+
*
97106
* @param dataDirectory where to store data/logs/etc.
98107
* @param port the port to listen on - each server in the ensemble must use a unique port
99108
* @param electionPort the electionPort to listen on - each server in the ensemble must use a unique electionPort
100109
* @param quorumPort the quorumPort to listen on - each server in the ensemble must use a unique quorumPort
101110
* @param deleteDataDirectoryOnClose if true, the data directory will be deleted when {@link TestingCluster#close()} is called
102111
* @param serverId the server ID for the instance
103112
*/
113+
@Deprecated
104114
public InstanceSpec(
105115
File dataDirectory,
106116
int port,
@@ -112,6 +122,8 @@ public InstanceSpec(
112122
}
113123

114124
/**
125+
* @deprecated Use {@link #builder()} instead.
126+
*
115127
* @param dataDirectory where to store data/logs/etc.
116128
* @param port the port to listen on - each server in the ensemble must use a unique port
117129
* @param electionPort the electionPort to listen on - each server in the ensemble must use a unique electionPort
@@ -121,6 +133,7 @@ public InstanceSpec(
121133
* @param tickTime tickTime. Set -1 to used fault server configuration
122134
* @param maxClientCnxns max number of client connections from the same IP. Set -1 to use default server configuration
123135
*/
136+
@Deprecated
124137
public InstanceSpec(
125138
File dataDirectory,
126139
int port,
@@ -144,6 +157,8 @@ public InstanceSpec(
144157
}
145158

146159
/**
160+
* @deprecated Use {@link #builder()} instead.
161+
*
147162
* @param dataDirectory where to store data/logs/etc.
148163
* @param port the port to listen on - each server in the ensemble must use a unique port
149164
* @param electionPort the electionPort to listen on - each server in the ensemble must use a unique electionPort
@@ -154,6 +169,7 @@ public InstanceSpec(
154169
* @param maxClientCnxns max number of client connections from the same IP. Set -1 to use default server configuration
155170
* @param customProperties other properties to be passed to the server
156171
*/
172+
@Deprecated
157173
public InstanceSpec(
158174
File dataDirectory,
159175
int port,
@@ -178,6 +194,8 @@ public InstanceSpec(
178194
}
179195

180196
/**
197+
* @deprecated Use {@link #builder()} instead.
198+
*
181199
* @param dataDirectory where to store data/logs/etc.
182200
* @param port the port to listen on - each server in the ensemble must use a unique port
183201
* @param electionPort the electionPort to listen on - each server in the ensemble must use a unique electionPort
@@ -189,6 +207,7 @@ public InstanceSpec(
189207
* @param customProperties other properties to be passed to the server
190208
* @param hostname Hostname or IP if the cluster is intending to be bounded into external interfaces
191209
*/
210+
@Deprecated
192211
public InstanceSpec(
193212
File dataDirectory,
194213
int port,
@@ -200,6 +219,32 @@ public InstanceSpec(
200219
int maxClientCnxns,
201220
Map<String, Object> customProperties,
202221
String hostname) {
222+
this(
223+
dataDirectory,
224+
port,
225+
electionPort,
226+
quorumPort,
227+
deleteDataDirectoryOnClose,
228+
serverId,
229+
tickTime,
230+
maxClientCnxns,
231+
customProperties != null ? enforceStringMap(customProperties) : null,
232+
hostname,
233+
false);
234+
}
235+
236+
InstanceSpec(
237+
File dataDirectory,
238+
int port,
239+
int electionPort,
240+
int quorumPort,
241+
boolean deleteDataDirectoryOnClose,
242+
int serverId,
243+
int tickTime,
244+
int maxClientCnxns,
245+
Map<String, Object> customProperties,
246+
String hostname,
247+
boolean ignored) {
203248
this.dataDirectory = (dataDirectory != null) ? dataDirectory : DirectoryUtils.createTempDirectory();
204249
this.port = (port >= 0) ? port : getRandomPort();
205250
this.electionPort = (electionPort >= 0) ? electionPort : getRandomPort();
@@ -208,9 +253,8 @@ public InstanceSpec(
208253
this.serverId = (serverId >= 0) ? serverId : nextServerId.getAndIncrement();
209254
this.tickTime = (tickTime > 0 ? tickTime : -1); // -1 to set default value
210255
this.maxClientCnxns = (maxClientCnxns >= 0 ? maxClientCnxns : -1); // -1 to set default value
211-
this.customProperties = customProperties != null
212-
? Collections.unmodifiableMap(enforceStringMap(customProperties))
213-
: Collections.emptyMap();
256+
this.customProperties =
257+
customProperties != null ? Collections.unmodifiableMap(customProperties) : Collections.emptyMap();
214258
this.hostname = hostname == null ? localhost : hostname;
215259
}
216260

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.apache.curator.test;
21+
22+
import java.io.File;
23+
import java.util.Map;
24+
25+
/**
26+
* Builder to construct {@link InstanceSpec}.
27+
*/
28+
public class InstanceSpecBuilder {
29+
private File dataDirectory;
30+
private boolean deleteDataDirectoryOnClose = true;
31+
private int port = -1;
32+
private int electionPort = -1;
33+
private int quorumPort = -1;
34+
private int serverId = -1;
35+
private int tickTime = -1;
36+
private int maxClientCnxns = -1;
37+
private Map<String, String> customProperties = null;
38+
private String hostname = null;
39+
40+
/**
41+
* Data directory to store data of this server instance. It will be deleted upon sever close.
42+
*
43+
* @see #withDataDirectory(File, boolean) also
44+
*/
45+
public InstanceSpecBuilder withDataDirectory(File dataDirectory) {
46+
this.dataDirectory = dataDirectory;
47+
return this;
48+
}
49+
50+
public InstanceSpecBuilder withDataDirectory(File dataDirectory, boolean deleteDataDirectoryOnClose) {
51+
this.dataDirectory = dataDirectory;
52+
this.deleteDataDirectoryOnClose = deleteDataDirectoryOnClose;
53+
return this;
54+
}
55+
56+
public InstanceSpecBuilder withPort(int port) {
57+
this.port = port;
58+
return this;
59+
}
60+
61+
public InstanceSpecBuilder withElectionPort(int electionPort) {
62+
this.electionPort = electionPort;
63+
return this;
64+
}
65+
66+
public InstanceSpecBuilder withQuorumPort(int quorumPort) {
67+
this.quorumPort = quorumPort;
68+
return this;
69+
}
70+
71+
public InstanceSpecBuilder withServerId(int serverId) {
72+
this.serverId = serverId;
73+
return this;
74+
}
75+
76+
public InstanceSpecBuilder withTickTime(int tickTime) {
77+
this.tickTime = tickTime;
78+
return this;
79+
}
80+
81+
public InstanceSpecBuilder withMaxClientCnxns(int maxClientCnxns) {
82+
this.maxClientCnxns = maxClientCnxns;
83+
return this;
84+
}
85+
86+
public InstanceSpecBuilder withCustomProperties(Map<String, String> customProperties) {
87+
this.customProperties = customProperties;
88+
return this;
89+
}
90+
91+
public InstanceSpecBuilder withHostname(String hostname) {
92+
this.hostname = hostname;
93+
return this;
94+
}
95+
96+
@SuppressWarnings("unchecked")
97+
public InstanceSpec build() {
98+
return new InstanceSpec(
99+
dataDirectory,
100+
port,
101+
electionPort,
102+
quorumPort,
103+
deleteDataDirectoryOnClose,
104+
serverId,
105+
tickTime,
106+
maxClientCnxns,
107+
(Map) customProperties,
108+
hostname,
109+
true);
110+
}
111+
}

curator-test/src/main/java/org/apache/curator/test/TestingServer.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,12 @@ public TestingServer(int port, File tempDirectory) throws Exception {
9494
* @throws Exception errors
9595
*/
9696
public TestingServer(int port, File tempDirectory, boolean start) throws Exception {
97-
this(new InstanceSpec(tempDirectory, Math.max(0, port), -1, -1, true, -1), start);
97+
this(
98+
InstanceSpec.builder()
99+
.withPort(Math.max(0, port))
100+
.withDataDirectory(tempDirectory)
101+
.build(),
102+
start);
98103
}
99104

100105
/**

curator-test/src/test/java/org/apache/curator/test/TestQuorumConfigBuilder.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,14 @@ public class TestQuorumConfigBuilder {
3131

3232
@Test
3333
public void testCustomProperties() throws Exception {
34-
Map<String, Object> customProperties = new HashMap<String, Object>();
34+
Map<String, String> customProperties = new HashMap();
3535
customProperties.put("authProvider.1", "org.apache.zookeeper.server.auth.SASLAuthenticationProvider");
3636
customProperties.put("kerberos.removeHostFromPrincipal", "true");
3737
customProperties.put("kerberos.removeRealmFromPrincipal", "true");
38-
InstanceSpec spec = new InstanceSpec(null, -1, -1, -1, true, 1, -1, -1, customProperties);
38+
InstanceSpec spec = InstanceSpec.builder()
39+
.withServerId(1)
40+
.withCustomProperties(customProperties)
41+
.build();
3942
TestingServer server = new TestingServer(spec, true);
4043
try {
4144
assertEquals(

curator-test/src/test/java/org/apache/curator/test/TestTestingServer.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,11 @@ public void setCustomTickTimeTest() throws Exception {
4141
} else {
4242
customTickMs = 100;
4343
}
44-
final InstanceSpec spec = new InstanceSpec(zkTmpDir, -1, -1, -1, true, -1, customTickMs, -1);
44+
final InstanceSpec spec = InstanceSpec.builder()
45+
.withDataDirectory(zkTmpDir)
46+
.withTickTime(customTickMs)
47+
.build();
48+
;
4549
final int zkTickTime;
4650
try (TestingServer testingServer = new TestingServer(spec, true)) {
4751
TestingZooKeeperMain main = (TestingZooKeeperMain)

0 commit comments

Comments
 (0)