/
JGroupsConfigBuilder.java
323 lines (273 loc) · 11.9 KB
/
JGroupsConfigBuilder.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/*
* JBoss, Home of Professional Open Source
* Copyright 2009 Red Hat Inc. and/or its affiliates and other
* contributors as indicated by the @author tags. All rights reserved.
* See the copyright.txt in the distribution for a full listing of
* individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.infinispan.test.fwk;
import org.infinispan.util.LegacyKeySupportSystemProperties;
import org.jgroups.conf.ConfiguratorFactory;
import org.jgroups.conf.ProtocolConfiguration;
import org.jgroups.conf.ProtocolStackConfigurator;
import org.jgroups.conf.XmlConfigurator;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import static org.infinispan.test.fwk.JGroupsConfigBuilder.ProtocolType.*;
import static org.infinispan.util.Immutables.immutableMapCopy;
/**
* This class owns the logic of associating network resources(i.e. ports) with threads, in order to make sure that there
* won't be any clashes between multiple clusters running in parallel on same host. Used for parallel test suite.
*
* @author Mircea.Markus@jboss.com
* @author Galder Zamarreño
*/
public class JGroupsConfigBuilder {
public static final String JGROUPS_STACK;
// Load the XML just once
private static final ProtocolStackConfigurator tcpConfigurator = loadTcp();
private static final ProtocolStackConfigurator udpConfigurator = loadUdp();
public static final int TCP_PORT_RANGE_PER_THREAD = 100;
private static final ThreadLocal<Integer> threadTcpStartPort = new ThreadLocal<Integer>() {
private final AtomicInteger uniqueAddr = new AtomicInteger(7900);
@Override
protected Integer initialValue() {
return uniqueAddr.getAndAdd(TCP_PORT_RANGE_PER_THREAD);
}
};
/**
* Holds unique mcast_addr for each thread used for JGroups channel construction.
*/
private static final ThreadLocal<String> threadMcastIP = new ThreadLocal<String>() {
private final AtomicInteger uniqueAddr = new AtomicInteger(11);
@Override
protected String initialValue() {
return "228.10.10." + uniqueAddr.getAndIncrement();
}
};
/**
* Holds unique mcast_port for each thread used for JGroups channel construction.
*/
private static final ThreadLocal<Integer> threadMcastPort = new ThreadLocal<Integer>() {
private final AtomicInteger uniquePort = new AtomicInteger(45589);
@Override
protected Integer initialValue() {
return uniquePort.getAndIncrement();
}
};
static {
JGROUPS_STACK = LegacyKeySupportSystemProperties.getProperty("infinispan.test.jgroups.protocol", "protocol.stack", "tcp");
System.out.println("Transport protocol stack used = " + JGROUPS_STACK);
}
public static String getJGroupsConfig(String fullTestName, TransportFlags flags) {
if (JGROUPS_STACK.equalsIgnoreCase("tcp")) return getTcpConfig(fullTestName, flags);
if (JGROUPS_STACK.equalsIgnoreCase("udp")) return getUdpConfig(fullTestName, flags);
throw new IllegalStateException("Unknown protocol stack : " + JGROUPS_STACK);
}
public static String getTcpConfig(String fullTestName, TransportFlags flags) {
// With the XML already parsed, make a safe copy of the
// protocol stack configurator and use that accordingly.
JGroupsProtocolCfg jgroupsCfg =
getJGroupsProtocolCfg(tcpConfigurator.getProtocolStack());
if (!flags.withFD())
removeFailureDetectionTcp(jgroupsCfg);
if (!flags.isSiteIndexSpecified()) {
removeRela2(jgroupsCfg);
} else {
ProtocolConfiguration protocol = jgroupsCfg.getProtocol(RELAY2);
protocol.getProperties().put("site", flags.siteName());
if (flags.relayConfig() != null) //if not specified, use default
protocol.getProperties().put("config", flags.relayConfig());
}
if (!flags.withMerge())
removeMerge(jgroupsCfg);
if (jgroupsCfg.containsProtocol(TEST_PING)) {
replaceTcpStartPort(jgroupsCfg, flags);
if (fullTestName == null)
return jgroupsCfg.toString(); // IDE run of test
else
return getTestPingDiscovery(fullTestName, jgroupsCfg); // Cmd line test run
} else {
return replaceMCastAddressAndPort(jgroupsCfg);
}
}
private static void removeMerge(JGroupsProtocolCfg jgroupsCfg) {
jgroupsCfg.removeProtocol(MERGE2);
}
public static String getUdpConfig(String fullTestName, TransportFlags flags) {
JGroupsProtocolCfg jgroupsCfg = getJGroupsProtocolCfg(udpConfigurator.getProtocolStack());
if (!flags.withFD())
removeFailureDetectionUdp(jgroupsCfg);
if (!flags.withMerge())
removeMerge(jgroupsCfg);
if (!flags.isSiteIndexSpecified()) {
removeRela2(jgroupsCfg);
}
if (jgroupsCfg.containsProtocol(TEST_PING)) {
if (fullTestName != null)
return getTestPingDiscovery(fullTestName, jgroupsCfg); // Cmd line test run
}
return replaceMCastAddressAndPort(jgroupsCfg);
}
/**
* Remove all failure detection related
* protocols from the given JGroups TCP stack.
*/
private static void removeFailureDetectionTcp(JGroupsProtocolCfg jgroupsCfg) {
jgroupsCfg.removeProtocol(FD)
.removeProtocol(VERIFY_SUSPECT);
}
private static void removeRela2(JGroupsProtocolCfg jgroupsCfg) {
jgroupsCfg.removeProtocol(RELAY2);
}
private static String getTestPingDiscovery(String fullTestName, JGroupsProtocolCfg jgroupsCfg) {
ProtocolType type = TEST_PING;
Map<String, String> props = jgroupsCfg.getProtocol(type).getProperties();
props.put("testName", fullTestName);
return replaceProperties(jgroupsCfg, props, type);
}
private static void removeFailureDetectionUdp(JGroupsProtocolCfg jgroupsCfg) {
jgroupsCfg.removeProtocol(FD_SOCK).removeProtocol(FD_ALL);
}
private static String replaceMCastAddressAndPort(JGroupsProtocolCfg jgroupsCfg) {
Map<String, String> props = jgroupsCfg.getProtocol(UDP).getProperties();
props.put("mcast_addr", threadMcastIP.get());
props.put("mcast_port", threadMcastPort.get().toString());
return replaceProperties(jgroupsCfg, props, UDP);
}
private static String replaceTcpStartPort(JGroupsProtocolCfg jgroupsCfg, TransportFlags transportFlags) {
Map<String, String> props = jgroupsCfg.getProtocol(TCP).getProperties();
Integer startPort = threadTcpStartPort.get();
int portRange = TCP_PORT_RANGE_PER_THREAD;
if (transportFlags.isSiteIndexSpecified()) {
portRange = 10;
int maxIndex = TCP_PORT_RANGE_PER_THREAD / portRange - 1;
if (transportFlags.siteIndex() > maxIndex) {
throw new IllegalStateException("Currently we only support " + (maxIndex + 1) + " sites!");
}
startPort += transportFlags.siteIndex() * portRange;
}
props.put("bind_port", startPort.toString());
// In JGroups, the port_range is inclusive
props.put("port_range", String.valueOf(portRange - 1));
return replaceProperties(jgroupsCfg, props, TCP);
}
private static String replaceProperties(
JGroupsProtocolCfg cfg, Map<String, String> newProps, ProtocolType type) {
ProtocolConfiguration protocol = cfg.getProtocol(type);
ProtocolConfiguration newProtocol =
new ProtocolConfiguration(protocol.getProtocolName(), newProps);
cfg.replaceProtocol(type, newProtocol);
return cfg.toString();
}
private static ProtocolStackConfigurator loadTcp() {
try {
return ConfiguratorFactory.getStackConfigurator("stacks/tcp.xml");
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private static ProtocolStackConfigurator loadUdp() {
try {
return ConfiguratorFactory.getStackConfigurator("stacks/udp.xml");
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private static JGroupsProtocolCfg getJGroupsProtocolCfg(List<ProtocolConfiguration> baseStack) {
JGroupsXmxlConfigurator configurator = new JGroupsXmxlConfigurator(baseStack);
List<ProtocolConfiguration> protoStack = configurator.getProtocolStack();
Map<ProtocolType, ProtocolConfiguration> protoMap = new HashMap<ProtocolType, ProtocolConfiguration>(protoStack.size());
for (ProtocolConfiguration cfg : protoStack)
protoMap.put(getProtocolType(cfg.getProtocolName()), cfg);
return new JGroupsProtocolCfg(protoMap, configurator);
}
private static ProtocolType getProtocolType(String name) {
int dotIndex = name.lastIndexOf(".");
return ProtocolType.valueOf(
dotIndex == -1 ? name : name.substring(dotIndex + 1, name.length()));
}
static class JGroupsXmxlConfigurator extends XmlConfigurator {
protected JGroupsXmxlConfigurator(List<ProtocolConfiguration> protocols) {
super(copy(protocols));
}
static List<ProtocolConfiguration> copy(List<ProtocolConfiguration> protocols) {
// Make a safe copy of the protocol stack to avoid concurrent modification issues
List<ProtocolConfiguration> copy =
new ArrayList<ProtocolConfiguration>(protocols.size());
for (ProtocolConfiguration p : protocols)
copy.add(new ProtocolConfiguration(
p.getProtocolName(), immutableMapCopy(p.getProperties())));
return copy;
}
}
static class JGroupsProtocolCfg {
final Map<ProtocolType, ProtocolConfiguration> protoMap;
final ProtocolStackConfigurator configurator;
JGroupsProtocolCfg(Map<ProtocolType, ProtocolConfiguration> protoMap,
ProtocolStackConfigurator configurator) {
this.protoMap = protoMap;
this.configurator = configurator;
}
JGroupsProtocolCfg addProtocol(ProtocolType type, ProtocolConfiguration cfg, int position) {
protoMap.put(type, cfg);
configurator.getProtocolStack().add(position, cfg);
return this;
}
JGroupsProtocolCfg removeProtocol(ProtocolType type) {
// Update the stack and map
configurator.getProtocolStack().remove(protoMap.remove(type));
return this;
}
ProtocolConfiguration getProtocol(ProtocolType type) {
return protoMap.get(type);
}
boolean containsProtocol(ProtocolType type) {
return getProtocol(type) != null;
}
JGroupsProtocolCfg replaceProtocol(ProtocolType type, ProtocolConfiguration newCfg) {
ProtocolConfiguration oldCfg = protoMap.get(type);
int position = configurator.getProtocolStack().indexOf(oldCfg);
// Remove protocol and put new configuration in same position
return removeProtocol(type).addProtocol(type, newCfg, position);
}
@Override
public String toString() {
return configurator.getProtocolStackString();
}
}
enum ProtocolType {
TCP, UDP,
MPING, PING, TCPPING, TEST_PING,
MERGE2,
FD_SOCK, FD, VERIFY_SUSPECT, FD_ALL,
BARRIER,
NAKACK, UNICAST,
NAKACK2, UNICAST2,
RSVP,
STABLE,
GMS,
UFC, MFC, FC,
FRAG2,
STREAMING_STATE_TRANSFER,
RELAY2;
}
}