Elffuss BraidHub

kikocis/java-driver public

Merge pull request #564 from datastax/ccm-dse-support

Add support for using DataStax Enterprise with CCMBridge.
Changesete85d9dc258583b371524990292833bac90c45250fd45912be99a5d32f7db1469
AuthorOlivier Michallat <olim7t@users.noreply.github.com> · 2016-01-04 17:46 +01:00
CommitterOlivier Michallat <olim7t@users.noreply.github.com> · 2016-01-04 17:46 +01:00
Parents4569034a55a1, 6650dc23785e

Browse the files at this changeset

5 files changed

modified driver-core/src/test/java/com/datastax/driver/core/CCMBridge.java +179 −14
@@ -51,9 +51,11 @@
5151
5252 public static final String IP_PREFIX;
5353
54- static final String CASSANDRA_VERSION;
54+ private static final String CASSANDRA_VERSION;
5555
56- static final String CASSANDRA_INSTALL_ARGS;
56+ private static final String CASSANDRA_INSTALL_ARGS;
57+
58+ private static final boolean IS_DSE;
5759
5860 public static final String DEFAULT_CLIENT_TRUSTSTORE_PASSWORD = "cassandra1sfun";
5961 public static final String DEFAULT_CLIENT_TRUSTSTORE_PATH = "/client.truststore";
@@ -85,6 +87,49 @@
8587 */
8688 private static final Map<String, String> ENVIRONMENT_MAP;
8789
90+ /**
91+ * A mapping of full DSE versions to their C* counterpart. This is not meant to be comprehensive. Used by
92+ * {@link #getCassandraVersion()}. If C* version cannot be derived, the method makes a 'best guess'.
93+ */
94+ private static final Map<String, String> dseToCassandraVersions = ImmutableMap.<String, String>builder()
95+ .put("5.0", "3.0")
96+ .put("4.8.3", "2.1.11")
97+ .put("4.8.2", "2.1.11")
98+ .put("4.8.1", "2.1.11")
99+ .put("4.8", "2.1.9")
100+ .put("4.7.6", "2.1.11")
101+ .put("4.7.5", "2.1.11")
102+ .put("4.7.4", "2.1.11")
103+ .put("4.7.3", "2.1.8")
104+ .put("4.7.2", "2.1.8")
105+ .put("4.7.1", "2.1.5")
106+ .put("4.6.11", "2.0.16")
107+ .put("4.6.10", "2.0.16")
108+ .put("4.6.9", "2.0.16")
109+ .put("4.6.8", "2.0.16")
110+ .put("4.6.7", "2.0.14")
111+ .put("4.6.6", "2.0.14")
112+ .put("4.6.5", "2.0.14")
113+ .put("4.6.4", "2.0.14")
114+ .put("4.6.3", "2.0.12")
115+ .put("4.6.2", "2.0.12")
116+ .put("4.6.1", "2.0.12")
117+ .put("4.6", "2.0.11")
118+ .put("4.5.9", "2.0.16")
119+ .put("4.5.8", "2.0.14")
120+ .put("4.5.7", "2.0.12")
121+ .put("4.5.6", "2.0.12")
122+ .put("4.5.5", "2.0.12")
123+ .put("4.5.4", "2.0.11")
124+ .put("4.5.3", "2.0.11")
125+ .put("4.5.2", "2.0.10")
126+ .put("4.5.1", "2.0.8")
127+ .put("4.5", "2.0.8")
128+ .put("4.0", "2.0")
129+ .put("3.2", "1.2")
130+ .put("3.1", "1.2")
131+ .build();
132+
88133 /**
89134 * The command to use to launch CCM
90135 */
@@ -94,14 +139,30 @@
94139 CASSANDRA_VERSION = System.getProperty("cassandra.version");
95140 String installDirectory = System.getProperty("cassandra.directory");
96141 String branch = System.getProperty("cassandra.branch");
142+
143+ String dseProperty = System.getProperty("dse");
144+ // If -Ddse, if the value is empty interpret it as enabled,
145+ // otherwise if there is a value, parse as boolean.
146+ IS_DSE = dseProperty != null && (dseProperty.isEmpty() || Boolean.parseBoolean(dseProperty));
147+
148+ StringBuilder installArgs = new StringBuilder();
97149 if (installDirectory != null && !installDirectory.trim().isEmpty()) {
98- CASSANDRA_INSTALL_ARGS = "--install-dir=" + new File(installDirectory).getAbsolutePath();
150+ installArgs.append("--install-dir=");
151+ installArgs.append(new File(installDirectory).getAbsolutePath());
99152 } else if (branch != null && !branch.trim().isEmpty()) {
100- CASSANDRA_INSTALL_ARGS = "-v git:" + branch;
153+ installArgs.append("-v git:");
154+ installArgs.append(branch.trim().replaceAll("\"", ""));
101155 } else {
102- CASSANDRA_INSTALL_ARGS = "-v " + CASSANDRA_VERSION;
156+ installArgs.append("-v");
157+ installArgs.append(CASSANDRA_VERSION);
103158 }
104159
160+ if (IS_DSE) {
161+ installArgs.append(" --dse");
162+ }
163+
164+ CASSANDRA_INSTALL_ARGS = installArgs.toString();
165+
105166 String ip_prefix = System.getProperty("ipprefix");
106167 if (ip_prefix == null || ip_prefix.isEmpty()) {
107168 ip_prefix = "127.0.1.";
@@ -132,6 +193,14 @@
132193 envMap.put("JAVA_HOME", ccmJavaHome);
133194 }
134195 ENVIRONMENT_MAP = ImmutableMap.copyOf(envMap);
196+
197+ if (CCMBridge.isDSE()) {
198+ logger.info("Tests requiring CCM will use DSE version {} (C* {}, install arguments: {})",
199+ CCMBridge.getDSEVersion(), CCMBridge.getCassandraVersion(), CCMBridge.getInstallArguments());
200+ } else {
201+ logger.info("Tests requiring CCM will use Cassandra version {} (install arguments: {})",
202+ CCMBridge.getCassandraVersion(), CCMBridge.getInstallArguments());
203+ }
135204 }
136205
137206 /**
@@ -147,8 +216,68 @@
147216
148217 private final File ccmDir;
149218
150- private CCMBridge() {
219+ private final boolean isDSE;
220+
221+ private CCMBridge(boolean isDSE) {
151222 this.ccmDir = Files.createTempDir();
223+ this.isDSE = isDSE;
224+ }
225+
226+ /**
227+ * @return The configured cassandra version. If -Ddse=true was used, this value is derived from the
228+ * DSE version provided. If the DSE version can't be derived the following logic is used:
229+ * <ol>
230+ * <li>If <= 3.X, use C* 1.2</li>
231+ * <li>If 4.X, use 2.1 for >= 4.7, 2.0 otherwise.</li>
232+ * <li>Otherwise 3.0</li>
233+ * </ol>
234+ */
235+ public static String getCassandraVersion() {
236+ if (isDSE()) {
237+ String cassandraVersion = dseToCassandraVersions.get(CASSANDRA_VERSION);
238+ if (cassandraVersion != null) {
239+ return cassandraVersion;
240+ } else if (CASSANDRA_VERSION.startsWith("3.") || CASSANDRA_VERSION.compareTo("3") <= 0) {
241+ return "1.2";
242+ } else if (CASSANDRA_VERSION.startsWith("4.")) {
243+ if (CASSANDRA_VERSION.compareTo("4.7") >= 0) {
244+ return "2.1";
245+ } else {
246+ return "2.0";
247+ }
248+ } else {
249+ // Fallback on 3.0 by default.
250+ return "3.0";
251+ }
252+
253+ } else {
254+ return CASSANDRA_VERSION;
255+ }
256+ }
257+
258+ /**
259+ * @return The configured DSE version if '-Ddse=true' specified, otherwise null.
260+ */
261+ public static String getDSEVersion() {
262+ if (isDSE()) {
263+ return CASSANDRA_VERSION;
264+ } else {
265+ return null;
266+ }
267+ }
268+
269+ /**
270+ * @return Whether or not DSE was configured via '-Ddse=true'.
271+ */
272+ public static boolean isDSE() {
273+ return IS_DSE;
274+ }
275+
276+ /**
277+ * @return The install arguments to pass to CCM when creating the cluster.
278+ */
279+ public static String getInstallArguments() {
280+ return CASSANDRA_INSTALL_ARGS;
152281 }
153282
154283 /**
@@ -216,9 +345,14 @@
216345 execute(CCM_COMMAND + " node%d start --wait-other-notice --wait-for-binary-proto", n);
217346 }
218347
219- public void start(int n, String jvmArg) {
220- logger.info("Starting: " + IP_PREFIX + n + " with " + jvmArg);
221- execute(CCM_COMMAND + " node%d start --wait-other-notice --wait-for-binary-proto --jvm_arg=%s", n, jvmArg);
348+ public void start(int n, String... jvmArg) {
349+ StringBuilder jvmArgs = new StringBuilder("");
350+ for (int i = 0; i < jvmArg.length; i++) {
351+ jvmArgs.append(" --jvm_arg=");
352+ jvmArgs.append(jvmArg[i]);
353+ }
354+ logger.info("Starting: " + IP_PREFIX + n + " with " + jvmArgs);
355+ execute(CCM_COMMAND + " node%d start --wait-other-notice --wait-for-binary-proto %s", n, jvmArgs.toString());
222356 }
223357
224358 public void stop(int n) {
@@ -257,9 +391,9 @@
257391
258392 public void bootstrapNode(int n, String dc) {
259393 if (dc == null)
260- execute(CCM_COMMAND + " add node%d -i %s%d -j %d -r %d -b -s", n, IP_PREFIX, n, 7000 + 100 * n, 8000 + 100 * n);
394+ execute(CCM_COMMAND + " add node%d -i %s%d -j %d -r %d -b -s" + (isDSE ? " --dse" : ""), n, IP_PREFIX, n, 7000 + 100 * n, 8000 + 100 * n);
261395 else
262- execute(CCM_COMMAND + " add node%d -i %s%d -j %d -b -d %s -s", n, IP_PREFIX, n, 7000 + 100 * n, dc);
396+ execute(CCM_COMMAND + " add node%d -i %s%d -j %d -b -d %s -s" + (isDSE ? " --dse" : ""), n, IP_PREFIX, n, 7000 + 100 * n, dc);
263397 execute(CCM_COMMAND + " node%d start --wait-other-notice --wait-for-binary-proto", n);
264398 }
265399
@@ -268,7 +402,7 @@
268402 String storageItf = IP_PREFIX + n + ":" + storagePort;
269403 String binaryItf = IP_PREFIX + n + ":" + binaryPort;
270404 String remoteLogItf = IP_PREFIX + n + ":" + remoteDebugPort;
271- execute(CCM_COMMAND + " add node%d -i %s%d -b -t %s -l %s --binary-itf %s -j %d -r %s -s",
405+ execute(CCM_COMMAND + " add node%d -i %s%d -b -t %s -l %s --binary-itf %s -j %d -r %s -s" + (isDSE ? " --dse" : ""),
272406 n, IP_PREFIX, n, thriftItf, storageItf, binaryItf, jmxPort, remoteLogItf);
273407 execute(CCM_COMMAND + " node%d start --wait-other-notice --wait-for-binary-proto", n);
274408 }
@@ -285,6 +419,18 @@
285419 execute(CCM_COMMAND + " updateconf " + confStr);
286420 }
287421
422+ public void updateDSEConfig(Map<String, String> configs) {
423+ StringBuilder confStr = new StringBuilder();
424+ for (Map.Entry<String, String> entry : configs.entrySet()) {
425+ confStr.append(entry.getKey() + ":" + entry.getValue() + " ");
426+ }
427+ execute(CCM_COMMAND + " updatedseconf " + confStr);
428+ }
429+
430+ public void setWorkload(int node, String workload) {
431+ execute(CCM_COMMAND + " node%d setworkload %s", node, workload);
432+ }
433+
288434 private void execute(String command, Object... args) {
289435
290436 String fullCommand = String.format(command, args) + " --config-dir=" + ccmDir;
@@ -618,10 +764,14 @@
618764 private final String clusterName;
619765 private Integer[] nodes = {1};
620766 private boolean start = true;
767+ private boolean isDSE = IS_DSE;
621768 private String cassandraInstallArgs = CASSANDRA_INSTALL_ARGS;
622769 private String[] startOptions = new String[0];
770+
623771 private Map<String, String> cassandraConfiguration = Maps.newHashMap();
624772
773+ private Map<String, String> dseConfiguration = Maps.newHashMap();
774+
625775
626776 Builder(String clusterName) {
627777 this.clusterName = clusterName;
@@ -665,6 +815,13 @@
665815 */
666816 public Builder withCassandraVersion(String cassandraVersion) {
667817 this.cassandraInstallArgs = "-v " + cassandraVersion;
818+ this.isDSE = false;
819+ return this;
820+ }
821+
822+ public Builder withDSEVersion(String dseVersion) {
823+ this.cassandraInstallArgs = "-v " + dseVersion + " --dse";
824+ this.isDSE = true;
668825 return this;
669826 }
670827
@@ -684,10 +841,18 @@
684841 return this;
685842 }
686843
844+ public Builder withDSEConfiguration(String key, String value) {
845+ this.dseConfiguration.put(key, value);
846+ return this;
847+ }
848+
687849 public CCMBridge build() {
688- CCMBridge ccm = new CCMBridge();
850+ CCMBridge ccm = new CCMBridge(isDSE);
689851 ccm.execute(buildCreateCommand());
690- ccm.updateConfig(cassandraConfiguration);
852+ if (!cassandraConfiguration.isEmpty())
853+ ccm.updateConfig(cassandraConfiguration);
854+ if (!dseConfiguration.isEmpty())
855+ ccm.updateDSEConfig(dseConfiguration);
691856 if (start)
692857 ccm.start();
693858 return ccm;
modified driver-core/src/test/java/com/datastax/driver/core/TestListener.java +47 −10
@@ -16,6 +16,7 @@
1616 package com.datastax.driver.core;
1717
1818 import com.datastax.driver.core.utils.CassandraVersion;
19+import com.datastax.driver.core.utils.DseVersion;
1920 import org.testng.*;
2021 import org.testng.internal.ConstructorOrMethod;
2122
@@ -27,10 +28,6 @@
2728 private long start_time = System.nanoTime();
2829 private int test_index = 0;
2930
30- static {
31- System.out.println("[CCMBridge] Using Cassandra version: " + CCMBridge.CASSANDRA_VERSION + " (install arguments: " + CCMBridge.CASSANDRA_INSTALL_ARGS + ")");
32- }
33-
3431 @Override
3532 public void onTestFailure(ITestResult tr) {
3633 long elapsedTime = TimeUnit.NANOSECONDS.toSeconds((System.nanoTime() - start_time));
@@ -90,15 +87,27 @@
9087 ConstructorOrMethod constructorOrMethod = testNgMethod.getConstructorOrMethod();
9188
9289 Class<?> clazz = testNgMethod.getInstance().getClass();
93- if (clazz != null && clazz.isAnnotationPresent(CassandraVersion.class)) {
94- CassandraVersion cassandraVersion = clazz.getAnnotation(CassandraVersion.class);
95- TestUtils.versionCheck(cassandraVersion.major(), cassandraVersion.minor(), cassandraVersion.description());
90+ if (clazz != null) {
91+ if (clazz.isAnnotationPresent(CassandraVersion.class)) {
92+ CassandraVersion cassandraVersion = clazz.getAnnotation(CassandraVersion.class);
93+ cassandraVersionCheck(cassandraVersion);
94+ }
95+ if (clazz.isAnnotationPresent(DseVersion.class)) {
96+ DseVersion dseVersion = clazz.getAnnotation(DseVersion.class);
97+ dseVersionCheck(dseVersion);
98+ }
9699 }
97100
98101 Method method = constructorOrMethod.getMethod();
99- if (method != null && method.isAnnotationPresent(CassandraVersion.class)) {
100- CassandraVersion cassandraVersion = method.getAnnotation(CassandraVersion.class);
101- TestUtils.versionCheck(cassandraVersion.major(), cassandraVersion.minor(), cassandraVersion.description());
102+ if (method != null) {
103+ if (method.isAnnotationPresent(CassandraVersion.class)) {
104+ CassandraVersion cassandraVersion = method.getAnnotation(CassandraVersion.class);
105+ cassandraVersionCheck(cassandraVersion);
106+ }
107+ if (method.isAnnotationPresent(DseVersion.class)) {
108+ DseVersion dseVersion = method.getAnnotation(DseVersion.class);
109+ dseVersionCheck(dseVersion);
110+ }
102111 }
103112 }
104113
@@ -106,4 +115,32 @@
106115 public void afterInvocation(IInvokedMethod testMethod, ITestResult testResult) {
107116 // Do nothing
108117 }
118+
119+ private static void cassandraVersionCheck(CassandraVersion version) {
120+ versionCheck(CCMBridge.getCassandraVersion(), version.major(), version.minor(), version.description());
121+ }
122+
123+ private static void dseVersionCheck(DseVersion version) {
124+ if (CCMBridge.isDSE()) {
125+ versionCheck(CCMBridge.getDSEVersion(), version.major(), version.minor(), version.description());
126+ } else {
127+ throw new SkipException("Skipping test because not configured for DataStax Enterprise cluster.");
128+ }
129+ }
130+
131+ private static void versionCheck(String version, double majorCheck, int minorCheck, String skipString) {
132+ if (version == null) {
133+ throw new SkipException("Skipping test because provided version is null");
134+ } else {
135+ String[] versionArray = version.split("\\.|-");
136+ double major = Double.parseDouble(versionArray[0] + "." + versionArray[1]);
137+ // If there is no minor version, assume latest version of whatever was provided.
138+ int minor = versionArray.length >= 3 ? Integer.parseInt(versionArray[2]) : Integer.MAX_VALUE;
139+
140+ if (major < majorCheck || (major == majorCheck && minor < minorCheck)) {
141+ throw new SkipException("Version >= " + majorCheck + "." + minorCheck + " required. " +
142+ "Description: " + skipString);
143+ }
144+ }
145+ }
109146 }
modified driver-core/src/test/java/com/datastax/driver/core/TestUtils.java +0 −11
@@ -24,7 +24,6 @@
2424 import org.scassandra.ScassandraFactory;
2525 import org.slf4j.Logger;
2626 import org.slf4j.LoggerFactory;
27-import org.testng.SkipException;
2827
2928 import java.io.IOException;
3029 import java.math.BigDecimal;
@@ -392,16 +391,6 @@
392391 return testForDown ? !host.isUp() : host.isUp();
393392 }
394393
395- public static void versionCheck(double majorCheck, int minorCheck, String skipString) {
396- String version = System.getProperty("cassandra.version");
397- String[] versionArray = version.split("\\.|-");
398- double major = Double.parseDouble(versionArray[0] + "." + versionArray[1]);
399- int minor = Integer.parseInt(versionArray[2]);
400-
401- if (major < majorCheck || (major == majorCheck && minor < minorCheck)) {
402- throw new SkipException("Version >= " + majorCheck + "." + minorCheck + " required. Description: " + skipString);
403- }
404- }
405394
406395 public static Host findOrWaitForHost(Cluster cluster, int node, long duration, TimeUnit unit) {
407396 return findOrWaitForHost(cluster, CCMBridge.ipOfNode(node), duration, unit);
modified driver-core/src/test/java/com/datastax/driver/core/utils/CassandraVersion.java +4 −4
@@ -19,7 +19,7 @@
1919 import java.lang.annotation.RetentionPolicy;
2020
2121 /**
22- * <p>Annotation for a Class or Method that defines a Cassandra Version requirement. If the cassandra verison in used
22+ * <p>Annotation for a Class or Method that defines a Cassandra Version requirement. If the cassandra version in use
2323 * does not meet the version requirement, the test is skipped.</p>
2424 *
2525 * @see {@link com.datastax.driver.core.TestListener#beforeInvocation(org.testng.IInvokedMethod, org.testng.ITestResult)} for usage.
@@ -29,15 +29,15 @@
2929 /**
3030 * @return The major version required to execute this test, i.e. "2.0"
3131 */
32- public double major();
32+ double major();
3333
3434 /**
3535 * @return The minor version required to execute this test, i.e. "0"
3636 */
37- public int minor() default 0;
37+ int minor() default 0;
3838
3939 /**
4040 * @return The description returned if this version requirement is not met.
4141 */
42- public String description() default "Does not meet minimum version requirement.";
42+ String description() default "Does not meet minimum version requirement.";
4343 }
added driver-core/src/test/java/com/datastax/driver/core/utils/DseVersion.java +43 −0
@@ -0,0 +1,43 @@
1+/*
2+ * Copyright (C) 2012-2015 DataStax Inc.
3+ *
4+ * Licensed under the Apache License, Version 2.0 (the "License");
5+ * you may not use this file except in compliance with the License.
6+ * You may obtain a copy of the License at
7+ *
8+ * http://www.apache.org/licenses/LICENSE-2.0
9+ *
10+ * Unless required by applicable law or agreed to in writing, software
11+ * distributed under the License is distributed on an "AS IS" BASIS,
12+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ * See the License for the specific language governing permissions and
14+ * limitations under the License.
15+ */
16+package com.datastax.driver.core.utils;
17+
18+import java.lang.annotation.Retention;
19+import java.lang.annotation.RetentionPolicy;
20+
21+/**
22+ * <p>Annotation for a Class or Method that defines a DataStax Enterprise Version requirement.
23+ * If the version in use does not meet the version requirement or DSE is not used, the test is skipped.</p>
24+ *
25+ * @see {@link com.datastax.driver.core.TestListener#beforeInvocation(org.testng.IInvokedMethod, org.testng.ITestResult)} for usage.
26+ */
27+@Retention(RetentionPolicy.RUNTIME)
28+public @interface DseVersion {
29+ /**
30+ * @return The major version required to execute this test, i.e. "4.8"
31+ */
32+ double major() default 0.0;
33+
34+ /**
35+ * @return The minor version required to execute this test, i.e. "3"
36+ */
37+ int minor() default 0;
38+
39+ /**
40+ * @return The description returned if this version requirement is not met.
41+ */
42+ String description() default "Does not meet minimum version requirement.";
43+}