Skip to content

Commit 68df5e2

Browse files
committed
Add builder to construct InstanceSpec
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 68df5e2

File tree

5 files changed

+156
-8
lines changed

5 files changed

+156
-8
lines changed

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: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package org.apache.curator.test;
2+
3+
import java.io.File;
4+
import java.util.Map;
5+
6+
/**
7+
* Builder to construct {@link InstanceSpec}.
8+
*/
9+
public class InstanceSpecBuilder {
10+
private File dataDirectory;
11+
private boolean deleteDataDirectoryOnClose = true;
12+
private int port = -1;
13+
private int electionPort = -1;
14+
private int quorumPort = -1;
15+
private int serverId = -1;
16+
private int tickTime = -1;
17+
private int maxClientCnxns = -1;
18+
private Map<String, String> customProperties = null;
19+
private String hostname = null;
20+
21+
/**
22+
* Data directory to store data of this server instance. It will be deleted upon sever close.
23+
*
24+
* @see #withDataDirectory(File, boolean) also
25+
*/
26+
public InstanceSpecBuilder withDataDirectory(File dataDirectory) {
27+
this.dataDirectory = dataDirectory;
28+
return this;
29+
}
30+
31+
public InstanceSpecBuilder withDataDirectory(File dataDirectory, boolean deleteDataDirectoryOnClose) {
32+
this.dataDirectory = dataDirectory;
33+
this.deleteDataDirectoryOnClose = deleteDataDirectoryOnClose;
34+
return this;
35+
}
36+
37+
public InstanceSpecBuilder withPort(int port) {
38+
this.port = port;
39+
return this;
40+
}
41+
42+
public InstanceSpecBuilder withElectionPort(int electionPort) {
43+
this.electionPort = electionPort;
44+
return this;
45+
}
46+
47+
public InstanceSpecBuilder withQuorumPort(int quorumPort) {
48+
this.quorumPort = quorumPort;
49+
return this;
50+
}
51+
52+
public InstanceSpecBuilder withServerId(int serverId) {
53+
this.serverId = serverId;
54+
return this;
55+
}
56+
57+
public InstanceSpecBuilder withTickTime(int tickTime) {
58+
this.tickTime = tickTime;
59+
return this;
60+
}
61+
62+
public InstanceSpecBuilder withMaxClientCnxns(int maxClientCnxns) {
63+
this.maxClientCnxns = maxClientCnxns;
64+
return this;
65+
}
66+
67+
public InstanceSpecBuilder withCustomProperties(Map<String, String> customProperties) {
68+
this.customProperties = customProperties;
69+
return this;
70+
}
71+
72+
public InstanceSpecBuilder withHostname(String hostname) {
73+
this.hostname = hostname;
74+
return this;
75+
}
76+
77+
@SuppressWarnings("unchecked")
78+
public InstanceSpec build() {
79+
return new InstanceSpec(
80+
dataDirectory,
81+
port,
82+
electionPort,
83+
quorumPort,
84+
deleteDataDirectoryOnClose,
85+
serverId,
86+
tickTime,
87+
maxClientCnxns,
88+
(Map) customProperties,
89+
hostname,
90+
true);
91+
}
92+
}

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)