-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
InstanceSpec.java
281 lines (252 loc) · 11.1 KB
/
InstanceSpec.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.curator.test;
import com.google.common.io.Files;
import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.UnknownHostException;
import java.util.Collections;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
/**
* Abstracts one of the servers in the ensemble
*/
public class InstanceSpec
{
private static final AtomicInteger nextServerId = new AtomicInteger(1);
private static final String localhost;
static
{
String address = "localhost";
try
{
// This is a workaround for people using OS X Lion. On Lion when a process tries to connect to a link-local
// address it takes 5 seconds to establish the connection for some reason. So instead of using 'localhost'
// which could return the link-local address randomly, we'll manually resolve it and look for an address to
// return that isn't link-local. If for some reason we can't find an address that isn't link-local then
// we'll fall back to the default lof just looking up 'localhost'.
for ( InetAddress a : InetAddress.getAllByName("localhost") )
{
if ( !a.isLinkLocalAddress() )
{
address = a.getHostAddress();
break;
}
}
}
catch ( UnknownHostException e )
{
// Something went wrong, just default to the existing approach of using 'localhost'.
}
localhost = address;
}
private final File dataDirectory;
private final int port;
private final int electionPort;
private final int quorumPort;
private final boolean deleteDataDirectoryOnClose;
private final int serverId;
private final int tickTime;
private final int maxClientCnxns;
private final Map<String,Object> customProperties;
private final String hostname;
public static void reset() {
nextServerId.set(1);
}
public static InstanceSpec newInstanceSpec()
{
return new InstanceSpec(null, -1, -1, -1, true, -1, -1, -1);
}
public static int getRandomPort()
{
ServerSocket server = null;
try
{
server = new ServerSocket(0);
server.setReuseAddress(true);
return server.getLocalPort();
}
catch ( IOException e )
{
throw new Error(e);
}
finally
{
if ( server != null )
{
try
{
server.close();
}
catch ( IOException ignore )
{
// ignore
}
}
}
}
/**
* @param dataDirectory where to store data/logs/etc.
* @param port the port to listen on - each server in the ensemble must use a unique port
* @param electionPort the electionPort to listen on - each server in the ensemble must use a unique electionPort
* @param quorumPort the quorumPort to listen on - each server in the ensemble must use a unique quorumPort
* @param deleteDataDirectoryOnClose if true, the data directory will be deleted when {@link TestingCluster#close()} is called
* @param serverId the server ID for the instance
*/
public InstanceSpec(File dataDirectory, int port, int electionPort, int quorumPort, boolean deleteDataDirectoryOnClose, int serverId)
{
this(dataDirectory, port, electionPort, quorumPort, deleteDataDirectoryOnClose, serverId, -1, -1, null, null);
}
/**
* @param dataDirectory where to store data/logs/etc.
* @param port the port to listen on - each server in the ensemble must use a unique port
* @param electionPort the electionPort to listen on - each server in the ensemble must use a unique electionPort
* @param quorumPort the quorumPort to listen on - each server in the ensemble must use a unique quorumPort
* @param deleteDataDirectoryOnClose if true, the data directory will be deleted when {@link TestingCluster#close()} is called
* @param serverId the server ID for the instance
* @param tickTime tickTime. Set -1 to used fault server configuration
* @param maxClientCnxns max number of client connections from the same IP. Set -1 to use default server configuration
*/
public InstanceSpec(File dataDirectory, int port, int electionPort, int quorumPort, boolean deleteDataDirectoryOnClose, int serverId, int tickTime, int maxClientCnxns) {
this(dataDirectory, port, electionPort, quorumPort, deleteDataDirectoryOnClose, serverId, tickTime, maxClientCnxns, null, null);
}
/**
* @param dataDirectory where to store data/logs/etc.
* @param port the port to listen on - each server in the ensemble must use a unique port
* @param electionPort the electionPort to listen on - each server in the ensemble must use a unique electionPort
* @param quorumPort the quorumPort to listen on - each server in the ensemble must use a unique quorumPort
* @param deleteDataDirectoryOnClose if true, the data directory will be deleted when {@link TestingCluster#close()} is called
* @param serverId the server ID for the instance
* @param tickTime tickTime. Set -1 to used fault server configuration
* @param maxClientCnxns max number of client connections from the same IP. Set -1 to use default server configuration
* @param customProperties other properties to be passed to the server
*/
public InstanceSpec(File dataDirectory, int port, int electionPort, int quorumPort, boolean deleteDataDirectoryOnClose, int serverId, int tickTime, int maxClientCnxns, Map<String,Object> customProperties)
{
this(dataDirectory, port, electionPort, quorumPort, deleteDataDirectoryOnClose, serverId, tickTime, maxClientCnxns, customProperties, null);
}
/**
* @param dataDirectory where to store data/logs/etc.
* @param port the port to listen on - each server in the ensemble must use a unique port
* @param electionPort the electionPort to listen on - each server in the ensemble must use a unique electionPort
* @param quorumPort the quorumPort to listen on - each server in the ensemble must use a unique quorumPort
* @param deleteDataDirectoryOnClose if true, the data directory will be deleted when {@link TestingCluster#close()} is called
* @param serverId the server ID for the instance
* @param tickTime tickTime. Set -1 to used fault server configuration
* @param maxClientCnxns max number of client connections from the same IP. Set -1 to use default server configuration
* @param customProperties other properties to be passed to the server
* @param hostname Hostname or IP if the cluster is intending to be bounded into external interfaces
*/
public InstanceSpec(File dataDirectory, int port, int electionPort, int quorumPort, boolean deleteDataDirectoryOnClose, int serverId, int tickTime, int maxClientCnxns, Map<String,Object> customProperties,String hostname)
{
this.dataDirectory = (dataDirectory != null) ? dataDirectory : Files.createTempDir();
this.port = (port >= 0) ? port : getRandomPort();
this.electionPort = (electionPort >= 0) ? electionPort : getRandomPort();
this.quorumPort = (quorumPort >= 0) ? quorumPort : getRandomPort();
this.deleteDataDirectoryOnClose = deleteDataDirectoryOnClose;
this.serverId = (serverId >= 0) ? serverId : nextServerId.getAndIncrement();
this.tickTime = (tickTime > 0 ? tickTime : -1); // -1 to set default value
this.maxClientCnxns = (maxClientCnxns >= 0 ? maxClientCnxns : -1); // -1 to set default value
this.customProperties = customProperties != null ? Collections.<String,Object>unmodifiableMap(customProperties) : Collections.<String,Object>emptyMap();
this.hostname = hostname == null ? localhost : hostname;
}
public int getServerId()
{
return serverId;
}
public File getDataDirectory()
{
return dataDirectory;
}
public int getPort()
{
return port;
}
public int getElectionPort()
{
return electionPort;
}
public int getQuorumPort()
{
return quorumPort;
}
/**
* @deprecated use {@link TestingServer#getConnectString()} or {@link TestingCluster#getConnectString()} instead
*/
@Deprecated
public String getConnectString()
{
return hostname + ":" + port;
}
public int getTickTime()
{
return tickTime;
}
public int getMaxClientCnxns()
{
return maxClientCnxns;
}
public boolean deleteDataDirectoryOnClose()
{
return deleteDataDirectoryOnClose;
}
public Map<String, Object> getCustomProperties() {
return customProperties;
}
public String getHostname() {
return hostname;
}
@Override
public String toString()
{
return "InstanceSpec{" +
"dataDirectory=" + dataDirectory +
", port=" + port +
", electionPort=" + electionPort +
", quorumPort=" + quorumPort +
", deleteDataDirectoryOnClose=" + deleteDataDirectoryOnClose +
", serverId=" + serverId +
", tickTime=" + tickTime +
", maxClientCnxns=" + maxClientCnxns +
", customProperties=" + customProperties +
", hostname=" + hostname +
"} " + super.toString();
}
@Override
public boolean equals(Object o)
{
if ( this == o )
{
return true;
}
if ( o == null || getClass() != o.getClass() )
{
return false;
}
InstanceSpec that = (InstanceSpec)o;
return hostname.equals(that.getHostname()) && port == that.port;
}
@Override
public int hashCode()
{
return hostname.hashCode() + port;
}
}