Add --data-dir option to run task (#50342)

This commit adds a special run.datadir system property that may be
passed to `./gradlew run` which sets the root data directory used by the
task. While normally overriding the data path is not allowed for test
clusters, it is useful when experimenting with the run task.

closes #50338
This commit is contained in:
Ryan Ernst 2019-12-19 12:47:53 -08:00 committed by Ryan Ernst
parent 689df1f28f
commit a0746dbfec
3 changed files with 42 additions and 1 deletions

View File

@ -437,6 +437,14 @@ class Run extends DefaultTask {
public void setDebug(boolean enabled) {
project.project(':distribution').run.debug = enabled
}
@Option(
option = "data-dir",
description = "Override the base data directory used by the testcluster"
)
public void setDataDir(String dataDirStr) {
project.project(':distribution').run.dataDir = dataDirStr
}
}
task run(type: Run) {

View File

@ -133,7 +133,6 @@ public class ElasticsearchNode implements TestClusterConfiguration {
private final Path confPathRepo;
private final Path configFile;
private final Path confPathData;
private final Path confPathLogs;
private final Path transportPortFile;
private final Path httpPortsFile;
@ -150,6 +149,7 @@ public class ElasticsearchNode implements TestClusterConfiguration {
private boolean isWorkingDirConfigured = false;
private String httpPort = "0";
private String transportPort = "0";
private Path confPathData;
ElasticsearchNode(String path, String name, Project project, ReaperService reaper, File workingDirBase) {
this.path = path;
@ -1305,6 +1305,10 @@ public class ElasticsearchNode implements TestClusterConfiguration {
this.transportPort = transportPort;
}
void setDataPath(Path dataPath) {
this.confPathData = dataPath;
}
@Internal
Path getEsStdoutFile() {
return esStdoutFile;

View File

@ -10,9 +10,12 @@ import java.io.BufferedReader;
import java.io.Closeable;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
public class RunTask extends DefaultTestClustersTask {
@ -22,6 +25,8 @@ public class RunTask extends DefaultTestClustersTask {
private Boolean debug = false;
private Path dataDir = null;
@Option(
option = "debug-jvm",
description = "Enable debugging configuration, to allow attaching a debugger to elasticsearch."
@ -35,6 +40,19 @@ public class RunTask extends DefaultTestClustersTask {
return debug;
}
@Option(
option = "data-dir",
description = "Override the base data directory used by the testcluster"
)
public void setDataDir(String dataDirStr) {
dataDir = Paths.get(dataDirStr).toAbsolutePath();
}
@Input
public String getDataDir() {
return dataDir.toString();
}
@Override
public void beforeStart() {
int debugPort = 5005;
@ -46,6 +64,14 @@ public class RunTask extends DefaultTestClustersTask {
entry -> entry.getKey().toString().substring(CUSTOM_SETTINGS_PREFIX.length()),
entry -> entry.getValue().toString()
));
boolean singleNode = getClusters().stream().flatMap(c -> c.getNodes().stream()).count() == 1;
final Function<ElasticsearchNode, Path> getDataPath;
if (singleNode) {
getDataPath = n -> dataDir;
} else {
getDataPath = n -> dataDir.resolve(n.getName());
}
for (ElasticsearchCluster cluster : getClusters()) {
cluster.getFirstNode().setHttpPort(String.valueOf(httpPort));
httpPort++;
@ -53,6 +79,9 @@ public class RunTask extends DefaultTestClustersTask {
transportPort++;
for (ElasticsearchNode node : cluster.getNodes()) {
additionalSettings.forEach(node::setting);
if (dataDir != null) {
node.setDataPath(getDataPath.apply(node));
}
if (debug) {
logger.lifecycle(
"Running elasticsearch in debug mode, {} suspending until connected on debugPort {}",