Skip to content

Commit eb5e2cc

Browse files
committed
Merge pull request apache#585 from datastax/ccm-minor-enhancements
More enhancements to CCM tests.
2 parents 02ede69 + 12a279d commit eb5e2cc

File tree

14 files changed

+265
-436
lines changed

14 files changed

+265
-436
lines changed

driver-core/pom.xml

Lines changed: 0 additions & 141 deletions
Original file line numberDiff line numberDiff line change
@@ -304,66 +304,6 @@
304304
</build>
305305

306306
<profiles>
307-
<profile>
308-
<id>default</id>
309-
<properties>
310-
<env>default</env>
311-
</properties>
312-
<activation>
313-
<activeByDefault>true</activeByDefault>
314-
</activation>
315-
<build>
316-
<plugins>
317-
<plugin>
318-
<groupId>org.apache.maven.plugins</groupId>
319-
<artifactId>maven-surefire-plugin</artifactId>
320-
<version>2.16</version>
321-
<configuration>
322-
<groups>unit</groups>
323-
<useFile>false</useFile>
324-
<systemPropertyVariables>
325-
<cassandra.version>${cassandra.version}</cassandra.version>
326-
<ipprefix>${ipprefix}</ipprefix>
327-
<com.datastax.driver.NEW_NODE_DELAY_SECONDS>60</com.datastax.driver.NEW_NODE_DELAY_SECONDS>
328-
</systemPropertyVariables>
329-
<classpathDependencyExcludes>
330-
<classpathDependencyExcludes>io.netty:netty-transport-native-epoll</classpathDependencyExcludes>
331-
</classpathDependencyExcludes>
332-
<!--May be needed for debugging-->
333-
<!--<forkMode>never</forkMode>-->
334-
</configuration>
335-
</plugin>
336-
</plugins>
337-
</build>
338-
</profile>
339-
340-
<profile>
341-
<id>short</id>
342-
<properties>
343-
<env>default</env>
344-
</properties>
345-
<build>
346-
<plugins>
347-
<plugin>
348-
<groupId>org.apache.maven.plugins</groupId>
349-
<artifactId>maven-surefire-plugin</artifactId>
350-
<version>2.16</version>
351-
<configuration>
352-
<groups>unit,short</groups>
353-
<useFile>false</useFile>
354-
<systemPropertyVariables>
355-
<cassandra.version>${cassandra.version}</cassandra.version>
356-
<ipprefix>${ipprefix}</ipprefix>
357-
</systemPropertyVariables>
358-
<classpathDependencyExcludes>
359-
<classpathDependencyExcludes>io.netty:netty-transport-native-epoll</classpathDependencyExcludes>
360-
</classpathDependencyExcludes>
361-
</configuration>
362-
</plugin>
363-
</plugins>
364-
</build>
365-
</profile>
366-
367307
<profile>
368308
<id>isolated</id>
369309
<properties>
@@ -388,87 +328,6 @@
388328
</plugins>
389329
</build>
390330
</profile>
391-
392-
<profile>
393-
<id>long</id>
394-
<properties>
395-
<env>default</env>
396-
</properties>
397-
<build>
398-
<plugins>
399-
<plugin>
400-
<groupId>org.apache.maven.plugins</groupId>
401-
<artifactId>maven-surefire-plugin</artifactId>
402-
<version>2.16</version>
403-
<configuration>
404-
<groups>unit,short,long</groups>
405-
<useFile>false</useFile>
406-
<systemPropertyVariables>
407-
<cassandra.version>${cassandra.version}</cassandra.version>
408-
<ipprefix>${ipprefix}</ipprefix>
409-
</systemPropertyVariables>
410-
<classpathDependencyExcludes>
411-
<classpathDependencyExcludes>io.netty:netty-transport-native-epoll</classpathDependencyExcludes>
412-
</classpathDependencyExcludes>
413-
</configuration>
414-
</plugin>
415-
</plugins>
416-
</build>
417-
</profile>
418-
419-
<profile>
420-
<id>duration</id>
421-
<properties>
422-
<env>default</env>
423-
</properties>
424-
<build>
425-
<plugins>
426-
<plugin>
427-
<groupId>org.apache.maven.plugins</groupId>
428-
<artifactId>maven-surefire-plugin</artifactId>
429-
<version>2.16</version>
430-
<configuration>
431-
<groups>unit,short,long,duration</groups>
432-
<useFile>false</useFile>
433-
<systemPropertyVariables>
434-
<cassandra.version>${cassandra.version}</cassandra.version>
435-
<ipprefix>${ipprefix}</ipprefix>
436-
</systemPropertyVariables>
437-
<classpathDependencyExcludes>
438-
<classpathDependencyExcludes>io.netty:netty-transport-native-epoll</classpathDependencyExcludes>
439-
</classpathDependencyExcludes>
440-
</configuration>
441-
</plugin>
442-
</plugins>
443-
</build>
444-
</profile>
445-
446-
<profile>
447-
<id>doc</id>
448-
<properties>
449-
<env>default</env>
450-
</properties>
451-
<build>
452-
<plugins>
453-
<plugin>
454-
<groupId>org.apache.maven.plugins</groupId>
455-
<artifactId>maven-surefire-plugin</artifactId>
456-
<version>2.16</version>
457-
<configuration>
458-
<groups>unit,doc</groups>
459-
<useFile>false</useFile>
460-
<systemPropertyVariables>
461-
<cassandra.version>${cassandra.version}</cassandra.version>
462-
<ipprefix>${ipprefix}</ipprefix>
463-
</systemPropertyVariables>
464-
<classpathDependencyExcludes>
465-
<classpathDependencyExcludes>io.netty:netty-transport-native-epoll</classpathDependencyExcludes>
466-
</classpathDependencyExcludes>
467-
</configuration>
468-
</plugin>
469-
</plugins>
470-
</build>
471-
</profile>
472331
</profiles>
473332

474333
<licenses>

driver-core/src/test/java/com/datastax/driver/core/CCMBridge.java

Lines changed: 58 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
import java.net.InetSocketAddress;
3232
import java.util.*;
3333
import java.util.concurrent.TimeUnit;
34+
import java.util.regex.Matcher;
35+
import java.util.regex.Pattern;
3436

3537
import static com.datastax.driver.core.TestUtils.executeNoFail;
3638
import static com.datastax.driver.core.TestUtils.findAvailablePort;
@@ -659,22 +661,25 @@ protected void finalize() throws Throwable {
659661
* use {@link #builder()} to get an instance
660662
*/
661663
public static class Builder {
662-
private final String clusterName = TestUtils.generateIdentifier("cluster_");
664+
665+
public static final String RANDOM_PORT = "__RANDOM_PORT__";
666+
private static final Pattern RANDOM_PORT_PATTERN = Pattern.compile(RANDOM_PORT);
667+
663668
int[] nodes = {1};
664669
private boolean start = true;
665670
private boolean isDSE = isDSE();
666671
private String version = getCassandraVersion();
667-
private Set<String> createOptions = new HashSet<String>(getInstallArguments());
672+
private Set<String> createOptions = new LinkedHashSet<String>(getInstallArguments());
668673
private Set<String> jvmArgs = new LinkedHashSet<String>();
669-
private final Map<String, Object> cassandraConfiguration = Maps.newHashMap();
670-
private final Map<String, Object> dseConfiguration = Maps.newHashMap();
671-
// -1 means random
672-
private int thriftPort = -1;
673-
private int binaryPort = -1;
674-
private int storagePort = -1;
674+
private final Map<String, Object> cassandraConfiguration = Maps.newLinkedHashMap();
675+
private final Map<String, Object> dseConfiguration = Maps.newLinkedHashMap();
675676
private Map<Integer, Workload> workloads = new HashMap<Integer, Workload>();
676677

677678
private Builder() {
679+
cassandraConfiguration.put("start_rpc", false);
680+
cassandraConfiguration.put("storage_port", RANDOM_PORT);
681+
cassandraConfiguration.put("rpc_port", RANDOM_PORT);
682+
cassandraConfiguration.put("native_transport_port", RANDOM_PORT);
678683
}
679684

680685
/**
@@ -781,17 +786,17 @@ public Builder withJvmArgs(String... jvmArgs) {
781786
}
782787

783788
public Builder withStoragePort(int port) {
784-
storagePort = port;
789+
cassandraConfiguration.put("storage_port", port);
785790
return this;
786791
}
787792

788793
public Builder withThriftPort(int port) {
789-
thriftPort = port;
794+
cassandraConfiguration.put("rpc_port", port);
790795
return this;
791796
}
792797

793798
public Builder withBinaryPort(int port) {
794-
binaryPort = port;
799+
cassandraConfiguration.put("native_transport_port", port);
795800
return this;
796801
}
797802

@@ -809,25 +814,23 @@ public Builder withWorkload(int node, Workload workload) {
809814

810815
public CCMBridge build() {
811816
// be careful NOT to alter internal state (hashCode/equals) during build!
812-
int storagePort = this.storagePort == -1 ? findAvailablePort() : this.storagePort;
813-
int thriftPort = this.thriftPort == -1 ? findAvailablePort() : this.thriftPort;
814-
int binaryPort = this.binaryPort == -1 ? findAvailablePort() : this.binaryPort;
817+
String clusterName = TestUtils.generateIdentifier("ccm_");
818+
Map<String, Object> cassandraConfiguration = randomizePorts(this.cassandraConfiguration);
819+
Map<String, Object> dseConfiguration = randomizePorts(this.dseConfiguration);
815820
VersionNumber version = VersionNumber.parse(this.version);
821+
int storagePort = Integer.parseInt(cassandraConfiguration.get("storage_port").toString());
822+
int thriftPort = Integer.parseInt(cassandraConfiguration.get("rpc_port").toString());
823+
int binaryPort = Integer.parseInt(cassandraConfiguration.get("native_transport_port").toString());
816824
final CCMBridge ccm = new CCMBridge(clusterName, isDSE, version, storagePort, thriftPort, binaryPort, joinJvmArgs());
817825
Runtime.getRuntime().addShutdownHook(new Thread() {
818826
@Override
819827
public void run() {
820828
ccm.close();
821829
}
822830
});
823-
ccm.execute(buildCreateCommand());
831+
ccm.execute(buildCreateCommand(clusterName));
824832
updateNodeConf(ccm);
825-
HashMap<String, Object> config = new HashMap<String, Object>(cassandraConfiguration);
826-
config.put("start_rpc", false);
827-
config.put("storage_port", storagePort);
828-
config.put("rpc_port", thriftPort);
829-
config.put("native_transport_port", binaryPort);
830-
ccm.updateConfig(config);
833+
ccm.updateConfig(cassandraConfiguration);
831834
if (!dseConfiguration.isEmpty())
832835
ccm.updateDSEConfig(dseConfiguration);
833836
for (Map.Entry<Integer, Workload> entry : workloads.entrySet()) {
@@ -851,12 +854,12 @@ private String joinJvmArgs() {
851854
StringBuilder allJvmArgs = new StringBuilder("");
852855
for (String jvmArg : jvmArgs) {
853856
allJvmArgs.append(" --jvm_arg=");
854-
allJvmArgs.append(jvmArg);
857+
allJvmArgs.append(randomizePorts(jvmArg));
855858
}
856859
return allJvmArgs.toString();
857860
}
858861

859-
private String buildCreateCommand() {
862+
private String buildCreateCommand(String clusterName) {
860863
StringBuilder result = new StringBuilder(CCM_COMMAND + " create");
861864
result.append(" ").append(clusterName);
862865
result.append(" -i ").append(TestUtils.IP_PREFIX);
@@ -870,7 +873,7 @@ private String buildCreateCommand() {
870873
result.append(node);
871874
}
872875
}
873-
result.append(" ").append(Joiner.on(" ").join(createOptions));
876+
result.append(" ").append(Joiner.on(" ").join(randomizePorts(createOptions)));
874877
return result.toString();
875878
}
876879

@@ -888,7 +891,7 @@ private void updateNodeConf(CCMBridge ccm) {
888891
for (int i = 0; i < nodesInDc; i++) {
889892
int jmxPort = findAvailablePort();
890893
int debugPort = findAvailablePort();
891-
logger.trace("Node {} in cluster {} using JMX port {} and debug port {}", n, clusterName, jmxPort, debugPort);
894+
logger.trace("Node {} in cluster {} using JMX port {} and debug port {}", n, ccm.getClusterName(), jmxPort, debugPort);
892895
File nodeConf = new File(ccm.getNodeDir(n), "node.conf");
893896
File nodeConf2 = new File(ccm.getNodeDir(n), "node.conf.tmp");
894897
BufferedReader br = closer.register(new BufferedReader(new FileReader(nodeConf)));
@@ -923,6 +926,36 @@ private void updateNodeConf(CCMBridge ccm) {
923926
}
924927
}
925928

929+
private Set<String> randomizePorts(Set<String> set) {
930+
Set<String> randomized = new LinkedHashSet<String>();
931+
for (String value : set) {
932+
randomized.add(randomizePorts(value));
933+
}
934+
return randomized;
935+
}
936+
937+
private Map<String, Object> randomizePorts(Map<String, Object> map) {
938+
Map<String, Object> randomized = new HashMap<String, Object>();
939+
for (Map.Entry<String, Object> entry : map.entrySet()) {
940+
Object value = entry.getValue();
941+
if (value instanceof CharSequence) {
942+
value = randomizePorts((CharSequence) value);
943+
}
944+
randomized.put(entry.getKey(), value);
945+
}
946+
return randomized;
947+
}
948+
949+
private String randomizePorts(CharSequence str) {
950+
Matcher matcher = RANDOM_PORT_PATTERN.matcher(str);
951+
StringBuffer sb = new StringBuffer();
952+
while (matcher.find()) {
953+
matcher.appendReplacement(sb, Integer.toString(TestUtils.findAvailablePort()));
954+
}
955+
matcher.appendTail(sb);
956+
return sb.toString();
957+
}
958+
926959
@Override
927960
@SuppressWarnings("SimplifiableIfStatement")
928961
public boolean equals(Object o) {
@@ -932,9 +965,6 @@ public boolean equals(Object o) {
932965
if (o == null || getClass() != o.getClass()) return false;
933966
Builder builder = (Builder) o;
934967
if (isDSE != builder.isDSE) return false;
935-
if (thriftPort != builder.thriftPort) return false;
936-
if (binaryPort != builder.binaryPort) return false;
937-
if (storagePort != builder.storagePort) return false;
938968
if (!Arrays.equals(nodes, builder.nodes)) return false;
939969
if (!createOptions.equals(builder.createOptions)) return false;
940970
if (!jvmArgs.equals(builder.jvmArgs)) return false;
@@ -955,17 +985,10 @@ public int hashCode() {
955985
result = 31 * result + cassandraConfiguration.hashCode();
956986
result = 31 * result + dseConfiguration.hashCode();
957987
result = 31 * result + workloads.hashCode();
958-
result = 31 * result + thriftPort;
959-
result = 31 * result + binaryPort;
960-
result = 31 * result + storagePort;
961988
result = 31 * result + version.hashCode();
962989
return result;
963990
}
964991

965-
@Override
966-
public String toString() {
967-
return String.format("%s (%s nodes)", clusterName, weight());
968-
}
969992
}
970993

971994
}

0 commit comments

Comments
 (0)