Skip to content

Commit 3f654e2

Browse files
Michael Figuieretolbertam
authored andcommitted
JAVA-1632: Add a withIpPrefix(String) method to CCMBridge.Builder
1 parent 89f14bc commit 3f654e2

File tree

1 file changed

+41
-23
lines changed

1 file changed

+41
-23
lines changed

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

Lines changed: 41 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
import java.util.regex.Matcher;
3535
import java.util.regex.Pattern;
3636

37-
import static com.datastax.driver.core.TestUtils.*;
37+
import static com.datastax.driver.core.TestUtils.executeNoFail;
3838

3939
public class CCMBridge implements CCMAccess {
4040

@@ -270,6 +270,8 @@ public static boolean isWindows() {
270270

271271
private final int binaryPort;
272272

273+
private final String ipPrefix;
274+
273275
private final File ccmDir;
274276

275277
private final boolean isDSE;
@@ -284,11 +286,12 @@ public static boolean isWindows() {
284286

285287
private final int[] nodes;
286288

287-
private CCMBridge(String clusterName, VersionNumber cassandraVersion, VersionNumber dseVersion,
289+
private CCMBridge(String clusterName, VersionNumber cassandraVersion, VersionNumber dseVersion, String ipPrefix,
288290
int storagePort, int thriftPort, int binaryPort, String jvmArgs, int[] nodes) {
289291
this.clusterName = clusterName;
290292
this.cassandraVersion = cassandraVersion;
291293
this.dseVersion = dseVersion;
294+
this.ipPrefix = ipPrefix;
292295
this.storagePort = storagePort;
293296
this.thriftPort = thriftPort;
294297
this.binaryPort = binaryPort;
@@ -307,9 +310,13 @@ public String getClusterName() {
307310
return clusterName;
308311
}
309312

313+
protected String ipOfNode(int n) {
314+
return ipPrefix + n;
315+
}
316+
310317
@Override
311318
public InetSocketAddress addressOfNode(int n) {
312-
return new InetSocketAddress(TestUtils.ipOfNode(n), binaryPort);
319+
return new InetSocketAddress(ipOfNode(n), binaryPort);
313320
}
314321

315322
@Override
@@ -426,7 +433,7 @@ public synchronized void start() {
426433
for (int dc = 1; dc <= nodes.length; dc++) {
427434
int nodesInDc = nodes[dc - 1];
428435
for (int i = 0; i < nodesInDc; i++) {
429-
InetSocketAddress addr = new InetSocketAddress(ipOfNode(n), binaryPort);
436+
InetSocketAddress addr = addressOfNode(n);
430437
logger.debug("Waiting for binary protocol to show up for {}", addr);
431438
TestUtils.waitUntilPortIsUp(addr);
432439
n++;
@@ -487,7 +494,7 @@ public String checkForErrors() {
487494

488495
@Override
489496
public void start(int n) {
490-
logger.debug(String.format("Starting: node %s (%s%s:%s) in %s", n, TestUtils.IP_PREFIX, n, binaryPort, this));
497+
logger.debug(String.format("Starting: node %s (%s%s:%s) in %s", n, ipPrefix, n, binaryPort, this));
491498
try {
492499
String cmd = CCM_COMMAND + " node%d start " + jvmArgs + getStartWaitArguments();
493500
if (isWindows() && this.cassandraVersion.compareTo(VersionNumber.parse("2.2.4")) >= 0) {
@@ -511,31 +518,31 @@ public void start(int n) {
511518

512519
@Override
513520
public void stop(int n) {
514-
logger.debug(String.format("Stopping: node %s (%s%s:%s) in %s", n, TestUtils.IP_PREFIX, n, binaryPort, this));
521+
logger.debug(String.format("Stopping: node %s (%s%s:%s) in %s", n, ipPrefix, n, binaryPort, this));
515522
execute(CCM_COMMAND + " node%d stop", n);
516523
}
517524

518525
@Override
519526
public void forceStop(int n) {
520-
logger.debug(String.format("Force stopping: node %s (%s%s:%s) in %s", n, TestUtils.IP_PREFIX, n, binaryPort, this));
527+
logger.debug(String.format("Force stopping: node %s (%s%s:%s) in %s", n, ipPrefix, n, binaryPort, this));
521528
execute(CCM_COMMAND + " node%d stop --not-gently", n);
522529
}
523530

524531
@Override
525532
public void pause(int n) {
526-
logger.debug(String.format("Pausing: node %s (%s%s:%s) in %s", n, TestUtils.IP_PREFIX, n, binaryPort, this));
533+
logger.debug(String.format("Pausing: node %s (%s%s:%s) in %s", n, ipPrefix, n, binaryPort, this));
527534
execute(CCM_COMMAND + " node%d pause", n);
528535
}
529536

530537
@Override
531538
public void resume(int n) {
532-
logger.debug(String.format("Resuming: node %s (%s%s:%s) in %s", n, TestUtils.IP_PREFIX, n, binaryPort, this));
539+
logger.debug(String.format("Resuming: node %s (%s%s:%s) in %s", n, ipPrefix, n, binaryPort, this));
533540
execute(CCM_COMMAND + " node%d resume", n);
534541
}
535542

536543
@Override
537544
public void remove(int n) {
538-
logger.debug(String.format("Removing: node %s (%s%s:%s) from %s", n, TestUtils.IP_PREFIX, n, binaryPort, this));
545+
logger.debug(String.format("Removing: node %s (%s%s:%s) from %s", n, ipPrefix, n, binaryPort, this));
539546
execute(CCM_COMMAND + " node%d remove", n);
540547
}
541548

@@ -546,18 +553,18 @@ public void add(int n) {
546553

547554
@Override
548555
public void add(int dc, int n) {
549-
logger.debug(String.format("Adding: node %s (%s%s:%s) to %s", n, TestUtils.IP_PREFIX, n, binaryPort, this));
550-
String thriftItf = TestUtils.ipOfNode(n) + ":" + thriftPort;
551-
String storageItf = TestUtils.ipOfNode(n) + ":" + storagePort;
552-
String binaryItf = TestUtils.ipOfNode(n) + ":" + binaryPort;
553-
String remoteLogItf = TestUtils.ipOfNode(n) + ":" + TestUtils.findAvailablePort();
556+
logger.debug(String.format("Adding: node %s (%s%s:%s) to %s", n, ipPrefix, n, binaryPort, this));
557+
String thriftItf = ipOfNode(n) + ":" + thriftPort;
558+
String storageItf = ipOfNode(n) + ":" + storagePort;
559+
String binaryItf = ipOfNode(n) + ":" + binaryPort;
560+
String remoteLogItf = ipOfNode(n) + ":" + TestUtils.findAvailablePort();
554561
execute(CCM_COMMAND + " add node%d -d dc%s -i %s%d -t %s -l %s --binary-itf %s -j %d -r %s -s -b" + (isDSE ? " --dse" : ""),
555-
n, dc, TestUtils.IP_PREFIX, n, thriftItf, storageItf, binaryItf, TestUtils.findAvailablePort(), remoteLogItf);
562+
n, dc, ipPrefix, n, thriftItf, storageItf, binaryItf, TestUtils.findAvailablePort(), remoteLogItf);
556563
}
557564

558565
@Override
559566
public void decommission(int n) {
560-
logger.debug(String.format("Decommissioning: node %s (%s%s:%s) from %s", n, TestUtils.IP_PREFIX, n, binaryPort, this));
567+
logger.debug(String.format("Decommissioning: node %s (%s%s:%s) from %s", n, ipPrefix, n, binaryPort, this));
561568
// Special case for C* 3.12+, DSE 5.1+, force decommission (see CASSANDRA-12510)
562569
String cmd = CCM_COMMAND + " node%d decommission";
563570
if (this.cassandraVersion.compareTo(VersionNumber.parse("3.12")) >= 0) {
@@ -780,6 +787,7 @@ public static class Builder {
780787
public static final String RANDOM_PORT = "__RANDOM_PORT__";
781788
private static final Pattern RANDOM_PORT_PATTERN = Pattern.compile(RANDOM_PORT);
782789

790+
private String ipPrefix = TestUtils.IP_PREFIX;
783791
int[] nodes = {1};
784792
private boolean start = true;
785793
private boolean dse = false;
@@ -797,6 +805,14 @@ private Builder() {
797805
cassandraConfiguration.put("native_transport_port", RANDOM_PORT);
798806
}
799807

808+
/**
809+
* IP Prefix to use for the address of each node. Its format has to be like {@code "127.1.1."}.
810+
*/
811+
public Builder withIpPrefix(String ipPrefix) {
812+
this.ipPrefix = ipPrefix;
813+
return this;
814+
}
815+
800816
/**
801817
* Number of hosts for each DC. Defaults to [1] (1 DC with 1 node)
802818
*/
@@ -949,7 +965,7 @@ public CCMBridge build() {
949965
cassandraConfiguration.remove("rpc_port");
950966
cassandraConfiguration.remove("thrift_prepared_statements_cache_size_mb");
951967
}
952-
final CCMBridge ccm = new CCMBridge(clusterName, cassandraVersion, dseVersion, storagePort, thriftPort, binaryPort, joinJvmArgs(), nodes);
968+
final CCMBridge ccm = new CCMBridge(clusterName, cassandraVersion, dseVersion, ipPrefix, storagePort, thriftPort, binaryPort, joinJvmArgs(), nodes);
953969

954970
Runtime.getRuntime().addShutdownHook(new Thread() {
955971
@Override
@@ -1010,7 +1026,7 @@ private String buildCreateCommand(String clusterName, boolean versionConfigured,
10101026
cassandraVersion, VersionNumber dseVersion) {
10111027
StringBuilder result = new StringBuilder(CCM_COMMAND + " create");
10121028
result.append(" ").append(clusterName);
1013-
result.append(" -i ").append(TestUtils.IP_PREFIX);
1029+
result.append(" -i ").append(ipPrefix);
10141030
result.append(" ");
10151031
if (nodes.length > 0) {
10161032
result.append(" -n ");
@@ -1052,8 +1068,8 @@ private void updateNodeConf(CCMBridge ccm) {
10521068
for (int dc = 1; dc <= nodes.length; dc++) {
10531069
int nodesInDc = nodes[dc - 1];
10541070
for (int i = 0; i < nodesInDc; i++) {
1055-
int jmxPort = findAvailablePort();
1056-
int debugPort = findAvailablePort();
1071+
int jmxPort = TestUtils.findAvailablePort();
1072+
int debugPort = TestUtils.findAvailablePort();
10571073
logger.trace("Node {} in cluster {} using JMX port {} and debug port {}", n, ccm.getClusterName(), jmxPort, debugPort);
10581074
File nodeConf = new File(ccm.getNodeDir(n), "node.conf");
10591075
File nodeConf2 = new File(ccm.getNodeDir(n), "node.conf.tmp");
@@ -1068,7 +1084,7 @@ private void updateNodeConf(CCMBridge ccm) {
10681084
if (line.startsWith("jmx_port")) {
10691085
line = String.format("jmx_port: '%s'", jmxPort);
10701086
} else if (line.startsWith("remote_debug_port")) {
1071-
line = String.format("remote_debug_port: %s:%s", TestUtils.ipOfNode(n), debugPort);
1087+
line = String.format("remote_debug_port: %s:%s", ipPrefix + n, debugPort);
10721088
}
10731089
pw.println(line);
10741090
}
@@ -1128,6 +1144,7 @@ public boolean equals(Object o) {
11281144

11291145
Builder builder = (Builder) o;
11301146

1147+
if (ipPrefix != builder.ipPrefix) return false;
11311148
if (dse != builder.dse) return false;
11321149
if (!Arrays.equals(nodes, builder.nodes)) return false;
11331150
if (version != null ? !version.equals(builder.version) : builder.version != null) return false;
@@ -1143,6 +1160,7 @@ public int hashCode() {
11431160
// do not include start as it is not relevant to the settings of the cluster.
11441161
int result = Arrays.hashCode(nodes);
11451162
result = 31 * result + (dse ? 1 : 0);
1163+
result = 31 * result + ipPrefix.hashCode();
11461164
result = 31 * result + (version != null ? version.hashCode() : 0);
11471165
result = 31 * result + createOptions.hashCode();
11481166
result = 31 * result + jvmArgs.hashCode();
@@ -1153,4 +1171,4 @@ public int hashCode() {
11531171
}
11541172
}
11551173

1156-
}
1174+
}

0 commit comments

Comments
 (0)