/
CcmBridge.java
637 lines (553 loc) · 22.2 KB
/
CcmBridge.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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
/*
* 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 com.datastax.oss.driver.api.testinfra.ccm;
import com.datastax.oss.driver.api.core.Version;
import com.datastax.oss.driver.shaded.guava.common.base.Joiner;
import com.datastax.oss.driver.shaded.guava.common.io.Resources;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;
import org.apache.commons.exec.CommandLine;
import org.apache.commons.exec.DefaultExecutor;
import org.apache.commons.exec.ExecuteStreamHandler;
import org.apache.commons.exec.ExecuteWatchdog;
import org.apache.commons.exec.Executor;
import org.apache.commons.exec.LogOutputStream;
import org.apache.commons.exec.PumpStreamHandler;
import org.assertj.core.util.Lists;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class CcmBridge implements AutoCloseable {
private static final Logger LOG = LoggerFactory.getLogger(CcmBridge.class);
public static final Version VERSION =
Objects.requireNonNull(Version.parse(System.getProperty("ccm.version", "4.0.0")));
public static final String INSTALL_DIRECTORY = System.getProperty("ccm.directory");
public static final String BRANCH = System.getProperty("ccm.branch");
public static final Boolean DSE_ENABLEMENT = Boolean.getBoolean("ccm.dse");
public static final String CLUSTER_NAME = "ccm_1";
public static final String DEFAULT_CLIENT_TRUSTSTORE_PASSWORD = "fakePasswordForTests";
public static final String DEFAULT_CLIENT_TRUSTSTORE_PATH = "/client.truststore";
public static final File DEFAULT_CLIENT_TRUSTSTORE_FILE =
createTempStore(DEFAULT_CLIENT_TRUSTSTORE_PATH);
public static final String DEFAULT_CLIENT_KEYSTORE_PASSWORD = "fakePasswordForTests";
public static final String DEFAULT_CLIENT_KEYSTORE_PATH = "/client.keystore";
public static final File DEFAULT_CLIENT_KEYSTORE_FILE =
createTempStore(DEFAULT_CLIENT_KEYSTORE_PATH);
// Contains the same keypair as the client keystore, but in format usable by OpenSSL
public static final File DEFAULT_CLIENT_PRIVATE_KEY_FILE = createTempStore("/client.key");
public static final File DEFAULT_CLIENT_CERT_CHAIN_FILE = createTempStore("/client.crt");
public static final String DEFAULT_SERVER_TRUSTSTORE_PASSWORD = "fakePasswordForTests";
public static final String DEFAULT_SERVER_TRUSTSTORE_PATH = "/server.truststore";
private static final File DEFAULT_SERVER_TRUSTSTORE_FILE =
createTempStore(DEFAULT_SERVER_TRUSTSTORE_PATH);
public static final String DEFAULT_SERVER_KEYSTORE_PASSWORD = "fakePasswordForTests";
public static final String DEFAULT_SERVER_KEYSTORE_PATH = "/server.keystore";
private static final File DEFAULT_SERVER_KEYSTORE_FILE =
createTempStore(DEFAULT_SERVER_KEYSTORE_PATH);
// A separate keystore where the certificate has a CN of localhost, used for hostname
// validation testing.
public static final String DEFAULT_SERVER_LOCALHOST_KEYSTORE_PATH = "/server_localhost.keystore";
private static final File DEFAULT_SERVER_LOCALHOST_KEYSTORE_FILE =
createTempStore(DEFAULT_SERVER_LOCALHOST_KEYSTORE_PATH);
// major DSE versions
private static final Version V6_0_0 = Version.parse("6.0.0");
private static final Version V5_1_0 = Version.parse("5.1.0");
private static final Version V5_0_0 = Version.parse("5.0.0");
// mapped C* versions from DSE versions
private static final Version V4_0_0 = Version.parse("4.0.0");
private static final Version V3_10 = Version.parse("3.10");
private static final Version V3_0_15 = Version.parse("3.0.15");
private static final Version V2_1_19 = Version.parse("2.1.19");
static {
if (DSE_ENABLEMENT) {
LOG.info("CCM Bridge configured with DSE version {}", VERSION);
} else {
LOG.info("CCM Bridge configured with Apache Cassandra version {}", VERSION);
}
}
private final int[] nodes;
private final Path configDirectory;
private final AtomicBoolean started = new AtomicBoolean();
private final AtomicBoolean created = new AtomicBoolean();
private final String ipPrefix;
private final Map<String, Object> cassandraConfiguration;
private final Map<String, Object> dseConfiguration;
private final List<String> rawDseYaml;
private final List<String> createOptions;
private final List<String> dseWorkloads;
private final String jvmArgs;
private CcmBridge(
Path configDirectory,
int[] nodes,
String ipPrefix,
Map<String, Object> cassandraConfiguration,
Map<String, Object> dseConfiguration,
List<String> dseConfigurationRawYaml,
List<String> createOptions,
Collection<String> jvmArgs,
List<String> dseWorkloads) {
this.configDirectory = configDirectory;
if (nodes.length == 1) {
// Hack to ensure that the default DC is always called 'dc1': pass a list ('-nX:0') even if
// there is only one DC (with '-nX', CCM configures `SimpleSnitch`, which hard-codes the name
// to 'datacenter1')
this.nodes = new int[] {nodes[0], 0};
} else {
this.nodes = nodes;
}
this.ipPrefix = ipPrefix;
this.cassandraConfiguration = cassandraConfiguration;
this.dseConfiguration = dseConfiguration;
this.rawDseYaml = dseConfigurationRawYaml;
this.createOptions = createOptions;
StringBuilder allJvmArgs = new StringBuilder("");
String quote = isWindows() ? "\"" : "";
for (String jvmArg : jvmArgs) {
// Windows requires jvm arguments to be quoted, while *nix requires unquoted.
allJvmArgs.append(" ");
allJvmArgs.append(quote);
allJvmArgs.append("--jvm_arg=");
allJvmArgs.append(jvmArg);
allJvmArgs.append(quote);
}
this.jvmArgs = allJvmArgs.toString();
this.dseWorkloads = dseWorkloads;
}
// Copied from Netty's PlatformDependent to avoid the dependency on Netty
private static boolean isWindows() {
return System.getProperty("os.name", "").toLowerCase(Locale.US).contains("win");
}
public Optional<Version> getDseVersion() {
return DSE_ENABLEMENT ? Optional.of(VERSION) : Optional.empty();
}
public Version getCassandraVersion() {
if (!DSE_ENABLEMENT) {
return VERSION;
} else {
Version stableVersion = VERSION.nextStable();
if (stableVersion.compareTo(V6_0_0) >= 0) {
return V4_0_0;
} else if (stableVersion.compareTo(V5_1_0) >= 0) {
return V3_10;
} else if (stableVersion.compareTo(V5_0_0) >= 0) {
return V3_0_15;
} else {
return V2_1_19;
}
}
}
private String getCcmVersionString(Version version) {
// for 4.0 pre-releases, the CCM version string needs to be "4.0-alpha1" or "4.0-alpha2"
// Version.toString() always adds a patch value, even if it's not specified when parsing.
if (version.getMajor() == 4
&& version.getMinor() == 0
&& version.getPatch() == 0
&& version.getPreReleaseLabels() != null) {
// truncate the patch version from the Version string
StringBuilder sb = new StringBuilder();
sb.append(version.getMajor()).append('.').append(version.getMinor());
for (String preReleaseString : version.getPreReleaseLabels()) {
sb.append('-').append(preReleaseString);
}
return sb.toString();
}
return version.toString();
}
public void create() {
if (created.compareAndSet(false, true)) {
if (INSTALL_DIRECTORY != null) {
createOptions.add("--install-dir=" + new File(INSTALL_DIRECTORY).getAbsolutePath());
} else if (BRANCH != null) {
createOptions.add("-v git:" + BRANCH.trim().replaceAll("\"", ""));
} else {
createOptions.add("-v " + getCcmVersionString(VERSION));
}
if (DSE_ENABLEMENT) {
createOptions.add("--dse");
}
execute(
"create",
CLUSTER_NAME,
"-i",
ipPrefix,
"-n",
Arrays.stream(nodes).mapToObj(n -> "" + n).collect(Collectors.joining(":")),
createOptions.stream().collect(Collectors.joining(" ")));
Version cassandraVersion = getCassandraVersion();
for (Map.Entry<String, Object> conf : cassandraConfiguration.entrySet()) {
String originalKey = conf.getKey();
Object originalValue = conf.getValue();
execute(
"updateconf",
String.join(
":",
getConfigKey(originalKey, originalValue, cassandraVersion),
getConfigValue(originalKey, originalValue, cassandraVersion)));
}
// If we're dealing with anything more recent than 2.2 explicitly enable UDF... but run it
// through our conversion process to make
// sure more recent versions don't have a problem.
if (cassandraVersion.compareTo(Version.V2_2_0) >= 0) {
String originalKey = "enable_user_defined_functions";
Object originalValue = "true";
execute(
"updateconf",
String.join(
":",
getConfigKey(originalKey, originalValue, cassandraVersion),
getConfigValue(originalKey, originalValue, cassandraVersion)));
}
// Note that we aren't performing any substitution on DSE key/value props (at least for now)
if (DSE_ENABLEMENT) {
for (Map.Entry<String, Object> conf : dseConfiguration.entrySet()) {
execute("updatedseconf", String.format("%s:%s", conf.getKey(), conf.getValue()));
}
for (String yaml : rawDseYaml) {
executeUnsanitized("updatedseconf", "-y", yaml);
}
if (!dseWorkloads.isEmpty()) {
execute("setworkload", String.join(",", dseWorkloads));
}
}
}
}
public void nodetool(int node, String... args) {
execute(String.format("node%d nodetool %s", node, Joiner.on(" ").join(args)));
}
public void dsetool(int node, String... args) {
execute(String.format("node%d dsetool %s", node, Joiner.on(" ").join(args)));
}
public void reloadCore(int node, String keyspace, String table, boolean reindex) {
dsetool(node, "reload_core", keyspace + "." + table, "reindex=" + reindex);
}
public void start() {
if (started.compareAndSet(false, true)) {
List<String> cmdAndArgs = Lists.newArrayList("start", jvmArgs, "--wait-for-binary-proto");
overrideJvmVersionForDseWorkloads()
.ifPresent(jvmVersion -> cmdAndArgs.add(String.format("--jvm_version=%d", jvmVersion)));
try {
execute(cmdAndArgs.toArray(new String[0]));
} catch (RuntimeException re) {
// if something went wrong starting CCM, see if we can also dump the error
executeCheckLogError();
throw re;
}
}
}
public void stop() {
if (started.compareAndSet(true, false)) {
execute("stop");
}
}
public void remove() {
execute("remove");
}
public void pause(int n) {
execute("node" + n, "pause");
}
public void resume(int n) {
execute("node" + n, "resume");
}
public void start(int n) {
List<String> cmdAndArgs = Lists.newArrayList("node" + n, "start");
overrideJvmVersionForDseWorkloads()
.ifPresent(jvmVersion -> cmdAndArgs.add(String.format("--jvm_version=%d", jvmVersion)));
execute(cmdAndArgs.toArray(new String[0]));
}
public void stop(int n) {
execute("node" + n, "stop");
}
public void add(int n, String dc) {
if (getDseVersion().isPresent()) {
execute("add", "-i", ipPrefix + n, "-d", dc, "node" + n, "--dse");
} else {
execute("add", "-i", ipPrefix + n, "-d", dc, "node" + n);
}
start(n);
}
public void decommission(int n) {
nodetool(n, "decommission");
}
synchronized void execute(String... args) {
String command =
"ccm "
+ String.join(" ", args)
+ " --config-dir="
+ configDirectory.toFile().getAbsolutePath();
execute(CommandLine.parse(command));
}
synchronized void executeUnsanitized(String... args) {
String command = "ccm ";
CommandLine cli = CommandLine.parse(command);
for (String arg : args) {
cli.addArgument(arg, false);
}
cli.addArgument("--config-dir=" + configDirectory.toFile().getAbsolutePath());
execute(cli);
}
private void execute(CommandLine cli) {
execute(cli, false);
}
private void executeCheckLogError() {
String command = "ccm checklogerror --config-dir=" + configDirectory.toFile().getAbsolutePath();
// force all logs to be error logs
execute(CommandLine.parse(command), true);
}
private void execute(CommandLine cli, boolean forceErrorLogging) {
if (forceErrorLogging) {
LOG.error("Executing: " + cli);
} else {
LOG.debug("Executing: " + cli);
}
ExecuteWatchdog watchDog = new ExecuteWatchdog(TimeUnit.MINUTES.toMillis(10));
try (LogOutputStream outStream =
new LogOutputStream() {
@Override
protected void processLine(String line, int logLevel) {
if (forceErrorLogging) {
LOG.error("ccmout> {}", line);
} else {
LOG.debug("ccmout> {}", line);
}
}
};
LogOutputStream errStream =
new LogOutputStream() {
@Override
protected void processLine(String line, int logLevel) {
LOG.error("ccmerr> {}", line);
}
}) {
Executor executor = new DefaultExecutor();
ExecuteStreamHandler streamHandler = new PumpStreamHandler(outStream, errStream);
executor.setStreamHandler(streamHandler);
executor.setWatchdog(watchDog);
int retValue = executor.execute(cli);
if (retValue != 0) {
LOG.error("Non-zero exit code ({}) returned from executing ccm command: {}", retValue, cli);
}
} catch (IOException ex) {
if (watchDog.killedProcess()) {
throw new RuntimeException("The command '" + cli + "' was killed after 10 minutes");
} else {
throw new RuntimeException("The command '" + cli + "' failed to execute", ex);
}
}
}
@Override
public void close() {
remove();
}
/**
* Extracts a keystore from the classpath into a temporary file.
*
* <p>This is needed as the keystore could be part of a built test jar used by other projects, and
* they need to be extracted to a file system so cassandra may use them.
*
* @param storePath Path in classpath where the keystore exists.
* @return The generated File.
*/
private static File createTempStore(String storePath) {
File f = null;
try (OutputStream os = new FileOutputStream(f = File.createTempFile("server", ".store"))) {
f.deleteOnExit();
Resources.copy(CcmBridge.class.getResource(storePath), os);
} catch (IOException e) {
LOG.warn("Failure to write keystore, SSL-enabled servers may fail to start.", e);
}
return f;
}
/**
* Get the current JVM major version (1.8.0_372 -> 8, 11.0.19 -> 11)
*
* @return major version of current JVM
*/
private static int getCurrentJvmMajorVersion() {
String version = System.getProperty("java.version");
if (version.startsWith("1.")) {
version = version.substring(2, 3);
} else {
int dot = version.indexOf(".");
if (dot != -1) {
version = version.substring(0, dot);
}
}
return Integer.parseInt(version);
}
private Optional<Integer> overrideJvmVersionForDseWorkloads() {
if (getCurrentJvmMajorVersion() <= 8) {
return Optional.empty();
}
if (!DSE_ENABLEMENT || !getDseVersion().isPresent()) {
return Optional.empty();
}
if (getDseVersion().get().compareTo(Version.parse("6.8.19")) < 0) {
return Optional.empty();
}
if (dseWorkloads.contains("graph")) {
return Optional.of(8);
}
return Optional.empty();
}
private static String IN_MS_STR = "_in_ms";
private static int IN_MS_STR_LENGTH = IN_MS_STR.length();
private static String ENABLE_STR = "enable_";
private static int ENABLE_STR_LENGTH = ENABLE_STR.length();
private static String IN_KB_STR = "_in_kb";
private static int IN_KB_STR_LENGTH = IN_KB_STR.length();
@SuppressWarnings("unused")
private String getConfigKey(String originalKey, Object originalValue, Version cassandraVersion) {
// At least for now we won't support substitutions on nested keys. This requires an extra
// traversal of the string
// but we'll live with that for now
if (originalKey.contains(".")) return originalKey;
if (cassandraVersion.compareTo(Version.V4_1_0) < 0) return originalKey;
if (originalKey.endsWith(IN_MS_STR))
return originalKey.substring(0, originalKey.length() - IN_MS_STR_LENGTH);
if (originalKey.startsWith(ENABLE_STR))
return originalKey.substring(ENABLE_STR_LENGTH) + "_enabled";
if (originalKey.endsWith(IN_KB_STR))
return originalKey.substring(0, originalKey.length() - IN_KB_STR_LENGTH);
return originalKey;
}
private String getConfigValue(
String originalKey, Object originalValue, Version cassandraVersion) {
String originalValueStr = originalValue.toString();
if (cassandraVersion.compareTo(Version.V4_1_0) < 0) return originalValueStr;
if (originalKey.endsWith(IN_MS_STR)) return originalValueStr + "ms";
if (originalKey.endsWith(IN_KB_STR)) return originalValueStr + "KiB";
return originalValueStr;
}
public static Builder builder() {
return new Builder();
}
public static class Builder {
private int[] nodes = {1};
private final Map<String, Object> cassandraConfiguration = new LinkedHashMap<>();
private final Map<String, Object> dseConfiguration = new LinkedHashMap<>();
private final List<String> dseRawYaml = new ArrayList<>();
private final List<String> jvmArgs = new ArrayList<>();
private String ipPrefix = "127.0.0.";
private final List<String> createOptions = new ArrayList<>();
private final List<String> dseWorkloads = new ArrayList<>();
private final Path configDirectory;
private Builder() {
try {
this.configDirectory = Files.createTempDirectory("ccm");
// mark the ccm temp directories for deletion when the JVM exits
this.configDirectory.toFile().deleteOnExit();
} catch (IOException e) {
// change to unchecked for now.
throw new RuntimeException(e);
}
// disable auto_snapshot by default to reduce disk usage when destroying schema.
withCassandraConfiguration("auto_snapshot", "false");
}
public Builder withCassandraConfiguration(String key, Object value) {
cassandraConfiguration.put(key, value);
return this;
}
public Builder withDseConfiguration(String key, Object value) {
dseConfiguration.put(key, value);
return this;
}
public Builder withDseConfiguration(String rawYaml) {
dseRawYaml.add(rawYaml);
return this;
}
public Builder withJvmArgs(String... jvmArgs) {
Collections.addAll(this.jvmArgs, jvmArgs);
return this;
}
public Builder withNodes(int... nodes) {
this.nodes = nodes;
return this;
}
public Builder withIpPrefix(String ipPrefix) {
this.ipPrefix = ipPrefix;
return this;
}
/** Adds an option to the {@code ccm create} command. */
public Builder withCreateOption(String option) {
this.createOptions.add(option);
return this;
}
/** Enables SSL encryption. */
public Builder withSsl() {
cassandraConfiguration.put("client_encryption_options.enabled", "true");
cassandraConfiguration.put("client_encryption_options.optional", "false");
cassandraConfiguration.put(
"client_encryption_options.keystore", DEFAULT_SERVER_KEYSTORE_FILE.getAbsolutePath());
cassandraConfiguration.put(
"client_encryption_options.keystore_password", DEFAULT_SERVER_KEYSTORE_PASSWORD);
return this;
}
public Builder withSslLocalhostCn() {
cassandraConfiguration.put("client_encryption_options.enabled", "true");
cassandraConfiguration.put("client_encryption_options.optional", "false");
cassandraConfiguration.put(
"client_encryption_options.keystore",
DEFAULT_SERVER_LOCALHOST_KEYSTORE_FILE.getAbsolutePath());
cassandraConfiguration.put(
"client_encryption_options.keystore_password", DEFAULT_SERVER_KEYSTORE_PASSWORD);
return this;
}
/** Enables client authentication. This also enables encryption ({@link #withSsl()}. */
public Builder withSslAuth() {
withSsl();
cassandraConfiguration.put("client_encryption_options.require_client_auth", "true");
cassandraConfiguration.put(
"client_encryption_options.truststore", DEFAULT_SERVER_TRUSTSTORE_FILE.getAbsolutePath());
cassandraConfiguration.put(
"client_encryption_options.truststore_password", DEFAULT_SERVER_TRUSTSTORE_PASSWORD);
return this;
}
public Builder withDseWorkloads(String... workloads) {
this.dseWorkloads.addAll(Arrays.asList(workloads));
return this;
}
public CcmBridge build() {
return new CcmBridge(
configDirectory,
nodes,
ipPrefix,
cassandraConfiguration,
dseConfiguration,
dseRawYaml,
createOptions,
jvmArgs,
dseWorkloads);
}
}
}