mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(emr): run s3-dist-cp steps (#7192)
In the docker runner mode an s3-dist-cp step, the Release Guide's way of
moving data "from Amazon S3 into HDFS where it can be processed by
subsequent steps", between buckets or back to S3, was FAILED without
running, and on the default TERMINATE_CLUSTER it ended its cluster with
errors. The EMR Serverless image steps run in has neither s3-dist-cp nor
the MapReduce it runs on.
Now a command-runner.jar step whose first argument is s3-dist-cp runs
Floci's own S3DistCp: a Python program, emr/s3-dist-cp.py, which the step's
command writes into the container and runs through spark-submit, copying
through Hadoop's file systems in Spark's JVM. The command wrapper gives it
what a Spark step gets: S3 paths in Floci's S3 and hdfs:// paths, and
those without a scheme, in the cluster's HDFS. It takes the options of
the Release Guide's table, --name=value or --name value, and -D
property=value:
- --src and --dest, a source folder's files keeping their folders;
- --srcPattern and --groupBy matched against the whole path; --groupBy
concatenates the files whose groups give the same name into one file
named by them, and refuses an expression with no group, as the guide
says S3DistCp does; --targetSize breaks a larger group into parts
numbered before the extension ("myfile0.gz, myfile1.gz"); without
--outputCodec a group keeps the compression its files share, named
with that codec's extension, or has none when they differ, each file
decompressed so the group is one stream;
- --outputCodec (gzip, gz, lzo, lzop, snappy, none) through Hadoop's
codecs, each source decompressed by its own extension;
- --deleteOnSuccess, deleting the files but not their folders, as AWS's
"Seven tips for using S3DistCp" says;
- --outputManifest, --previousManifest, --requirePreviousManifest and
--copyFromManifest, the manifest gzipped JSON lines of path, baseName,
srcDir and size, as that post shows them;
- --s3ServerSideEncryption, --storageClass, --disableMultipartUpload and
--multipartUploadChunkSize as S3A's settings; --numberFiles,
--startingIndex, --srcPrefixesFile and --appendToLastFile, the last
through a copy renamed over the file, as neither S3 nor the cluster's
HDFS appends in place. The copy has a name of its own and goes if the
writing fails, and the file steps aside, under a name of its own too,
until the copy has taken its place, coming back if it cannot, so a
failed append leaves it as it was. --s3Endpoint is ignored, S3 being
Floci's.
--requirePreviousManifest takes true or false, after = or as the next
argument, and alone is true, its default. A --srcPrefixesFile line ending
in / names that folder alone, its slash kept though Hadoop's Path drops
it; any other is a prefix as S3 lists one, logs/day taking in
logs/day-old/.
A bucket name with an underscore fails the step, as S3DistCp "does not
support" one, and so does a copy Hadoop fails, leaving what was copied.
A copy that would write two files to the same path, the manifest
included (a.log and a.log.gz with --outputCodec=gzip), a file over one
of its own sources, as with the same --src and --dest, or a file outside
--dest, through a .. in a manifest's baseName or in --outputManifest
(Hadoop's paths resolve it), or a file with no name, from a --groupBy
whose groups matched nothing say, fails before it writes anything. emr.md
says what is Floci's own reading: the grouped file's folder,
--numberFiles' naming, a group's compression without --outputCodec and
that refusal.
This commit is contained in:
1 parent
beef71e8eb
commit
a74a9b0ffc
7 files changed
+973
-14
No files matched your search
+20
-3
@@ -280,6 +280,23 @@ class EmrServiceContainerStepsTest {
|
||||
assertEquals(List.of(), runner.jobs);
|
||||
}
|
||||
|
||||
@Test
|
||||
void anS3DistCpStepRunsFlocisS3DistCpOnTheClustersStorage() {
|
||||
EmrStep copy = sparkStep("copy", "CONTINUE");
|
||||
copy.setArgs(new ArrayList<>(List.of("s3-dist-cp", "--src=s3://bucket/logs/", "--dest=hdfs:///output")));
|
||||
EmrCluster cluster = service.runJobFlow(cluster("copying", true, copy), REGION);
|
||||
|
||||
fixture.runStepThread();
|
||||
|
||||
assertEquals("COMPLETED", service.describeStep(cluster.getId(), cluster.getSteps().get(0).getId()).getState());
|
||||
SparkJobRunner.Job job = runner.jobs.get(0);
|
||||
assertEquals(List.of("bash", "-c"), job.command().subList(0, 2));
|
||||
assertEquals(List.of("s3-dist-cp", "--src=s3://bucket/logs/", "--dest=hdfs:///output"),
|
||||
job.command().subList(3, job.command().size()));
|
||||
assertEquals(List.of(), job.application());
|
||||
assertEquals(new SparkJobRunner.Storage(cluster.getId() + "-home", cluster.getId() + "-mnt"), job.storage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void describeStepAnswersAFailedStepsFailureDetails() {
|
||||
EmrCluster cluster = service.runJobFlow(cluster("described", true, sparkStep("breaks", "CONTINUE")), REGION);
|
||||
@@ -706,9 +723,9 @@ class EmrServiceContainerStepsTest {
|
||||
|
||||
@Test
|
||||
void aClusterWhoseStepsNoContainerRanHasNoStorage() {
|
||||
EmrStep copy = sparkStep("copy", "CONTINUE");
|
||||
copy.setArgs(new ArrayList<>(List.of("s3-dist-cp", "--src=s3://bucket/in/", "--dest=hdfs:///in/")));
|
||||
EmrCluster cluster = service.runJobFlow(cluster("unrun", true, copy), REGION);
|
||||
EmrStep query = sparkStep("query", "CONTINUE");
|
||||
query.setArgs(new ArrayList<>(List.of("hive-script", "--run-hive-script", "--args", "-f", "s3://bucket/q.q")));
|
||||
EmrCluster cluster = service.runJobFlow(cluster("unrun", true, query), REGION);
|
||||
fixture.runStepThread();
|
||||
|
||||
service.terminateJobFlows(List.of(cluster.getId()));
|
||||
|
||||
@@ -6,12 +6,15 @@ import io.github.hectorvent.floci.services.lambda.launcher.BootstrapProcess;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
@@ -93,9 +96,37 @@ class EmrSparkStepsTest {
|
||||
assertEquals(new BootstrapProcess.Result(0, "ran:--day|ran:1 2|"), ended);
|
||||
}
|
||||
|
||||
@Test
|
||||
void anS3DistCpStepWritesFlocisS3DistCpAndRunsItThroughSparkSubmit(@TempDir Path folder) throws Exception {
|
||||
// The Release Guide's own S3DistCp step.
|
||||
List<String> command = EmrSparkSteps.command(step("command-runner.jar", null, "s3-dist-cp",
|
||||
"--s3Endpoint=s3.amazonaws.com", "--src=s3://bucket/logs/j-3GYXXXXXX9IOJ/node/",
|
||||
"--dest=hdfs:///output", "--srcPattern=.*[a-zA-Z,]+"));
|
||||
// A spark-submit that shows what it was given and keeps the program, in place of the image's.
|
||||
Path sparkSubmit = folder.resolve("spark-submit");
|
||||
Files.writeString(sparkSubmit, "#!/bin/sh\nprintf '%s|' \"$@\"\ncp \"$3\" " + folder.resolve("kept.py") + "\n");
|
||||
assertTrue(sparkSubmit.toFile().setExecutable(true));
|
||||
ProcessBuilder builder = new ProcessBuilder(command);
|
||||
builder.environment().put("PATH", folder + ":" + System.getenv("PATH"));
|
||||
builder.environment().put("TMPDIR", folder.toString());
|
||||
|
||||
BootstrapProcess.Result ended = BootstrapProcess.run(builder);
|
||||
|
||||
assertEquals(List.of("bash", "-c"), command.subList(0, 2));
|
||||
assertEquals(new BootstrapProcess.Result(0, "--conf|spark.ui.enabled=false|" + folder + "/floci-s3-dist-cp.py|"
|
||||
+ "--s3Endpoint=s3.amazonaws.com|--src=s3://bucket/logs/j-3GYXXXXXX9IOJ/node/|--dest=hdfs:///output|"
|
||||
+ "--srcPattern=.*[a-zA-Z,]+|"), ended);
|
||||
String program;
|
||||
try (InputStream in = getClass().getClassLoader().getResourceAsStream(EmrSparkSteps.S3_DIST_CP_RESOURCE)) {
|
||||
program = new String(in.readAllBytes(), StandardCharsets.UTF_8);
|
||||
}
|
||||
assertEquals(program.stripTrailing(), Files.readString(folder.resolve("kept.py")).stripTrailing());
|
||||
assertFalse(program.lines().anyMatch(EmrSparkSteps.S3_DIST_CP_END::equals));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toolsTheImageDoesNotHaveAreNotRun() {
|
||||
for (String tool : List.of("hive-script", "pig-script", "hadoop-streaming", "hadoop-lzo", "s3-dist-cp")) {
|
||||
for (String tool : List.of("hive-script", "pig-script", "hadoop-streaming", "hadoop-lzo")) {
|
||||
assertNull(EmrSparkSteps.command(step("command-runner.jar", null, tool, "--src", "s3://bucket/in/")));
|
||||
}
|
||||
assertNull(EmrSparkSteps.command(step("command-runner.jar", null)));
|
||||
|
||||
@@ -0,0 +1,376 @@
|
||||
package io.github.hectorvent.floci.services.emr;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.github.hectorvent.floci.services.lambda.launcher.BootstrapProcess;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.junit.jupiter.api.Assumptions.assumeTrue;
|
||||
|
||||
/**
|
||||
* Floci's S3DistCp program, its options and what it plans to write where, run by the host's {@code python3}: the
|
||||
* planning needs no Spark, which only the copy itself runs under in a step's container.
|
||||
*/
|
||||
class S3DistCpProgramTest {
|
||||
|
||||
private static final ObjectMapper MAPPER = new ObjectMapper();
|
||||
// Loads the program, the file named first, as a module; each driver below goes on to print its answers as JSON.
|
||||
private static final String LOAD_PROGRAM = """
|
||||
import importlib.util, json, sys
|
||||
spec = importlib.util.spec_from_file_location("s3distcp", sys.argv[1])
|
||||
program = importlib.util.module_from_spec(spec)
|
||||
spec.loader.exec_module(program)
|
||||
""";
|
||||
// Prints what each case gives.
|
||||
private static final String DRIVER = LOAD_PROGRAM + """
|
||||
cases = json.loads(sys.stdin.read())
|
||||
answers = []
|
||||
for case in cases:
|
||||
try:
|
||||
options, settings = program.parse(case["argv"])
|
||||
if "files" not in case:
|
||||
answers.append({"options": options, "settings": settings})
|
||||
continue
|
||||
files = [tuple(f) for f in case["files"]]
|
||||
outputs = program.plan(files, options, case.get("extensions", {}), case.get("outputExtension"))
|
||||
program.check_targets(outputs, options["dest"], options.get("outputManifest"), lambda l: l)
|
||||
answers.append({"outputs": [[target, [f[1] for f in sources]] for target, sources in outputs]})
|
||||
except program.UsageError as e:
|
||||
answers.append({"error": str(e)})
|
||||
print(json.dumps(answers))
|
||||
""";
|
||||
// Runs replace against a file system of names whose listed renames fail, and prints what each case leaves.
|
||||
private static final String REPLACE_DRIVER = LOAD_PROGRAM + """
|
||||
class Files:
|
||||
def __init__(self, files, failing):
|
||||
self.files, self.failing = dict(files), {tuple(f) for f in failing}
|
||||
|
||||
def rename(self, src, dst):
|
||||
if (src, dst) in self.failing or src not in self.files or dst in self.files:
|
||||
return False
|
||||
self.files[dst] = self.files.pop(src)
|
||||
return True
|
||||
|
||||
def delete(self, path, recursive):
|
||||
return self.files.pop(path, None) is not None
|
||||
|
||||
answers = []
|
||||
for failing in json.loads(sys.stdin.read()):
|
||||
files = Files({"t": "old", "t.new": "new"}, failing)
|
||||
try:
|
||||
program.replace(files, "t.new", "t", "t.old")
|
||||
answers.append({"files": files.files})
|
||||
except IOError as e:
|
||||
answers.append({"files": files.files, "error": str(e)})
|
||||
print(json.dumps(answers))
|
||||
""";
|
||||
// Prints what a file's path starts with under each --srcPrefixesFile line, given the path Hadoop makes of it.
|
||||
private static final String PREFIX_DRIVER = LOAD_PROGRAM + """
|
||||
print(json.dumps([program.prefix_start(line, path) for line, path in json.loads(sys.stdin.read())]))
|
||||
""";
|
||||
private static final long MIB = 1024 * 1024;
|
||||
// The Release Guide's log layout, by day and hour.
|
||||
private static final List<List<Object>> LOGS = List.of(
|
||||
file("2017-02-22/05/a.log", 3 * MIB), file("2017-02-22/05/b.log", 3 * MIB),
|
||||
file("2017-02-22/05/c.log.gz", 5 * MIB), file("2017-02-22/06/d.log", MIB),
|
||||
file("2017-02-22/06/notes.txt", 10));
|
||||
|
||||
@TempDir
|
||||
static Path folder;
|
||||
|
||||
@BeforeAll
|
||||
static void needsPython() throws Exception {
|
||||
boolean python;
|
||||
try {
|
||||
python = BootstrapProcess.run(new ProcessBuilder("python3", "--version")).exit() == 0;
|
||||
} catch (IOException e) {
|
||||
python = false;
|
||||
}
|
||||
assumeTrue(python, "the host has no python3");
|
||||
try (InputStream in = S3DistCpProgramTest.class.getClassLoader()
|
||||
.getResourceAsStream(EmrSparkSteps.S3_DIST_CP_RESOURCE)) {
|
||||
Files.write(folder.resolve("s3-dist-cp.py"), in.readAllBytes());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void optionsTakeEitherFormAndGenericSettingsTheirOwn() throws Exception {
|
||||
JsonNode parsed = run(List.of(new Case(List.of("--src", "s3://bucket/logs", "--dest=hdfs:///output",
|
||||
"--targetSize=2", "-D", "fs.s3a.connection.maximum=8", "-Dmapreduce.job.reduces=1", "--deleteOnSuccess",
|
||||
"--requirePreviousManifest=false")))).get(0);
|
||||
|
||||
assertEquals(MAPPER.readTree("""
|
||||
{"src": "s3://bucket/logs", "dest": "hdfs:///output", "targetSize": "2", "deleteOnSuccess": true,
|
||||
"requirePreviousManifest": false}"""), parsed.path("options"));
|
||||
assertEquals(MAPPER.readTree("[[\"fs.s3a.connection.maximum\", \"8\"], [\"mapreduce.job.reduces\", \"1\"]]"),
|
||||
parsed.path("settings"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void requirePreviousManifestTakesTrueOrFalseInEitherFormAndAloneIsTrue() throws Exception {
|
||||
List<String> argv = List.of("--src=s3://bucket/logs/", "--dest=hdfs:///output");
|
||||
List<JsonNode> answers = run(List.of(
|
||||
new Case(concat(argv, "--requirePreviousManifest", "false")),
|
||||
new Case(concat(argv, "--requirePreviousManifest", "TRUE")),
|
||||
new Case(concat(argv, "--requirePreviousManifest", "--deleteOnSuccess")),
|
||||
new Case(concat(argv, "--requirePreviousManifest=maybe"))));
|
||||
|
||||
assertEquals(List.of("false", "true", "true"), answers.subList(0, 3).stream()
|
||||
.map(answer -> answer.path("options").path("requirePreviousManifest").asText()).toList());
|
||||
assertTrue(answers.get(2).path("options").path("deleteOnSuccess").asBoolean());
|
||||
assertEquals("--requirePreviousManifest takes true or false, not maybe", answers.get(3).path("error").asText());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aCopyThatWouldLoseDataFailsBeforeAnythingIsWritten() throws Exception {
|
||||
List<List<Object>> logs = new ArrayList<>(LOGS);
|
||||
logs.add(file("2017-02-22/05/c.log", 4));
|
||||
List<JsonNode> answers = run(List.of(
|
||||
new Case(List.of("--src=s3://bucket/logs/", "--dest=hdfs:///output", "--outputCodec=gzip"), logs,
|
||||
".gz"),
|
||||
new Case(List.of("--src=s3://bucket/logs/", "--dest=s3a://bucket/logs/"), LOGS),
|
||||
new Case(List.of("--src=s3://bucket/logs/", "--dest=hdfs:///output",
|
||||
"--outputManifest=2017-02-22/06/d.log"), LOGS),
|
||||
new Case(List.of("--src=s3://bucket/logs/", "--dest=s3a://bucket/logs", "--srcPattern=.*/05/[ab]\\.log",
|
||||
"--outputCodec=gzip"), LOGS, ".gz")));
|
||||
|
||||
assertEquals("2017-02-22/05/c.log and 2017-02-22/05/c.log.gz would both be written to"
|
||||
+ " hdfs:///output/2017-02-22/05/c.log.gz", answers.get(0).path("error").asText());
|
||||
assertEquals("2017-02-22/05/a.log would be written over its source s3a://bucket/logs/2017-02-22/05/a.log",
|
||||
answers.get(1).path("error").asText());
|
||||
assertEquals("2017-02-22/06/d.log and the manifest would both be written to"
|
||||
+ " hdfs:///output/2017-02-22/06/d.log", answers.get(2).path("error").asText());
|
||||
// The same folder is fine when every output has a name of its own.
|
||||
assertEquals(List.of("2017-02-22/05/a.log.gz", "2017-02-22/05/b.log.gz"), targets(answers.get(3)));
|
||||
}
|
||||
|
||||
@Test
|
||||
void anOutputThatWouldLeaveTheDestinationFailsTheCopy() throws Exception {
|
||||
// A manifest's baseName is the output's path under --dest, and Hadoop's paths resolve a "..".
|
||||
List<JsonNode> answers = run(List.of(
|
||||
new Case(List.of("--src=s3://bucket/logs/", "--dest=hdfs:///output", "--copyFromManifest",
|
||||
"--previousManifest=s3://bucket/manifest.gz"),
|
||||
List.of(file("2017-02-22/../../victim", 3))),
|
||||
new Case(List.of("--src=s3://bucket/logs/", "--dest=hdfs:///output", "--outputManifest=../manifest.gz"),
|
||||
LOGS)));
|
||||
|
||||
assertEquals(List.of("2017-02-22/../../victim leads out of --dest hdfs:///output",
|
||||
"../manifest.gz leads out of --dest hdfs:///output"),
|
||||
answers.stream().map(answer -> answer.path("error").asText()).toList());
|
||||
}
|
||||
|
||||
@Test
|
||||
void anOutputWithNoFileNameFailsTheCopy() throws Exception {
|
||||
// Hadoop's paths would write it as its folder.
|
||||
List<String> argv = List.of("--src=s3://bucket/logs/", "--dest=hdfs:///output");
|
||||
List<JsonNode> answers = run(List.of(
|
||||
new Case(concat(argv, "--groupBy=.*/(z*)[ab]\\.log"), LOGS),
|
||||
new Case(concat(argv, "--outputManifest="), LOGS)));
|
||||
|
||||
assertEquals(List.of("2017-02-22/05/a.log, 2017-02-22/05/b.log would be written to"
|
||||
+ " hdfs:///output/2017-02-22/05/, which names no file",
|
||||
"the manifest would be written to hdfs:///output/, which names no file"),
|
||||
answers.stream().map(answer -> answer.path("error").asText()).toList());
|
||||
}
|
||||
|
||||
@Test
|
||||
void optionsS3DistCpRefusesFailItBeforeAnythingIsCopied() throws Exception {
|
||||
List<JsonNode> answers = run(List.of(
|
||||
new Case(List.of("--src=s3://bucket/logs/")),
|
||||
new Case(List.of("--src=s3://amzn_s3_demo/logs/", "--dest=hdfs:///output")),
|
||||
new Case(List.of("--src=s3://bucket/logs/", "--dest=hdfs:///output", "--groupBy=.*\\.log")),
|
||||
new Case(List.of("--src=s3://bucket/logs/", "--dest=hdfs:///output", "--outputCodec=zip")),
|
||||
new Case(List.of("--src=s3://bucket/logs/", "--dest=hdfs:///output", "--targetSize=ten")),
|
||||
new Case(List.of("--src=s3://bucket/logs/", "--dest=hdfs:///output", "--deleteOnSuccess=yes")),
|
||||
new Case(List.of("--src=s3://bucket/logs/", "--dest=hdfs:///output", "--srcPatern=.*"))));
|
||||
|
||||
assertEquals(List.of("--dest is required",
|
||||
"S3DistCp does not support Amazon S3 bucket names that contain the underscore character:"
|
||||
+ " s3://amzn_s3_demo/logs/",
|
||||
"The --groupBy regular expression .*\\.log has no parenthetical group to name the concatenated"
|
||||
+ " files by",
|
||||
"--outputCodec takes gzip, gz, lzo, lzop, snappy or none, not zip",
|
||||
"--targetSize takes a whole number, not ten",
|
||||
"--deleteOnSuccess takes no value",
|
||||
"Unknown option --srcPatern"), answers.stream().map(answer -> answer.path("error").asText()).toList());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aCopyKeepsEachFilesFolderUnderTheDestinationAndASourcePatternMatchesTheWholePath() throws Exception {
|
||||
List<JsonNode> answers = run(List.of(
|
||||
new Case(List.of("--src=s3://bucket/logs/", "--dest=hdfs:///output"), LOGS),
|
||||
new Case(List.of("--src=s3://bucket/logs/", "--dest=hdfs:///output", "--srcPattern=.*\\.log"), LOGS),
|
||||
new Case(List.of("--src=s3://bucket/logs/", "--dest=hdfs:///output", "--srcPattern=\\d+"), LOGS)));
|
||||
|
||||
assertEquals(outputs("""
|
||||
[["2017-02-22/05/a.log", ["2017-02-22/05/a.log"]], ["2017-02-22/05/b.log", ["2017-02-22/05/b.log"]],
|
||||
["2017-02-22/05/c.log.gz", ["2017-02-22/05/c.log.gz"]],
|
||||
["2017-02-22/06/d.log", ["2017-02-22/06/d.log"]],
|
||||
["2017-02-22/06/notes.txt", ["2017-02-22/06/notes.txt"]]]"""), answers.get(0).path("outputs"));
|
||||
assertEquals(List.of("2017-02-22/05/a.log", "2017-02-22/05/b.log", "2017-02-22/06/d.log"),
|
||||
targets(answers.get(1)));
|
||||
assertEquals(List.of(), targets(answers.get(2)));
|
||||
}
|
||||
|
||||
@Test
|
||||
void anOutputCodecRenamesEachFileForItsExtensionAndNoneTakesTheSourcesOff() throws Exception {
|
||||
List<String> argv = List.of("--src=s3://bucket/logs/", "--dest=hdfs:///output", "--srcPattern=.*/05/.*");
|
||||
List<JsonNode> answers = run(List.of(
|
||||
new Case(argv, LOGS, ".gz"),
|
||||
new Case(argv, LOGS, "")));
|
||||
|
||||
assertEquals(List.of("2017-02-22/05/a.log.gz", "2017-02-22/05/b.log.gz", "2017-02-22/05/c.log.gz"),
|
||||
targets(answers.get(0)));
|
||||
assertEquals(List.of("2017-02-22/05/a.log", "2017-02-22/05/b.log", "2017-02-22/05/c.log"),
|
||||
targets(answers.get(1)));
|
||||
}
|
||||
|
||||
@Test
|
||||
void groupByConcatenatesTheFilesItsGroupsNameAndTargetSizeBreaksAGroupIntoNumberedParts() throws Exception {
|
||||
String groupBy = "--groupBy=.*/(\\d\\d)/.*\\.log.*";
|
||||
List<JsonNode> answers = run(List.of(
|
||||
new Case(List.of("--src=s3://bucket/logs/", "--dest=hdfs:///output", groupBy), LOGS),
|
||||
new Case(List.of("--src=s3://bucket/logs/", "--dest=hdfs:///output", groupBy, "--targetSize=6"), LOGS),
|
||||
new Case(List.of("--src=s3://bucket/logs/", "--dest=hdfs:///output", groupBy, "--targetSize=6",
|
||||
"--outputCodec=gzip"), LOGS, ".gz")));
|
||||
|
||||
assertEquals(outputs("""
|
||||
[["2017-02-22/05/05", ["2017-02-22/05/a.log", "2017-02-22/05/b.log", "2017-02-22/05/c.log.gz"]],
|
||||
["2017-02-22/06/06", ["2017-02-22/06/d.log"]]]"""), answers.get(0).path("outputs"));
|
||||
// "broken into parts as: myfile0.gz, myfile1.gz", and a group within the size stays whole.
|
||||
assertEquals(outputs("""
|
||||
[["2017-02-22/05/050", ["2017-02-22/05/a.log", "2017-02-22/05/b.log"]],
|
||||
["2017-02-22/05/051", ["2017-02-22/05/c.log.gz"]], ["2017-02-22/06/06", ["2017-02-22/06/d.log"]]]"""),
|
||||
answers.get(1).path("outputs"));
|
||||
assertEquals(List.of("2017-02-22/05/050.gz", "2017-02-22/05/051.gz", "2017-02-22/06/06.gz"),
|
||||
targets(answers.get(2)));
|
||||
}
|
||||
|
||||
@Test
|
||||
void anAppendedCopyTakesTheFilesPlaceOnlyOnceItCanAndTheOldFileComesBackOtherwise() throws Exception {
|
||||
List<JsonNode> answers = runDriver(REPLACE_DRIVER, List.of(
|
||||
List.of(),
|
||||
List.of(List.of("t.new", "t")),
|
||||
List.of(List.of("t", "t.old")),
|
||||
List.of(List.of("t.new", "t"), List.of("t.old", "t"))));
|
||||
|
||||
assertEquals(MAPPER.readTree("{\"files\": {\"t\": \"new\"}}"), answers.get(0));
|
||||
assertEquals(MAPPER.readTree("""
|
||||
{"files": {"t": "old", "t.new": "new"}, "error": "Could not rename t.new to t"}"""), answers.get(1));
|
||||
assertEquals(MAPPER.readTree("""
|
||||
{"files": {"t": "old", "t.new": "new"}, "error": "Could not move t aside to replace it"}"""),
|
||||
answers.get(2));
|
||||
// Should even the way back fail, the old content keeps the name it stepped aside to.
|
||||
assertEquals(MAPPER.readTree("""
|
||||
{"files": {"t.new": "new", "t.old": "old"},
|
||||
"error": "Could not rename t.new to t; what it held is at t.old"}"""), answers.get(3));
|
||||
}
|
||||
|
||||
@Test
|
||||
void withoutAnOutputCodecAGroupKeepsTheCompressionItsFilesShareAndAMixedOneHasNone() throws Exception {
|
||||
List<String> argv = List.of("--src=s3://bucket/logs/", "--dest=hdfs:///output");
|
||||
List<JsonNode> answers = run(List.of(
|
||||
new Case(concat(argv, "--groupBy=.*/(\\d\\d)/.*\\.log.*"), LOGS),
|
||||
new Case(concat(argv, "--groupBy=.*/(\\d\\d)/.*\\.gz"), LOGS),
|
||||
new Case(concat(argv, "--groupBy=.*/(\\d\\d)/.*(\\.gz)"), LOGS)));
|
||||
|
||||
// 05 holds a.log, b.log and c.log.gz, written uncompressed; 06 holds d.log alone.
|
||||
assertEquals(List.of("2017-02-22/05/05", "2017-02-22/06/06"), targets(answers.get(0)));
|
||||
assertEquals(List.of("2017-02-22/05/05.gz"), targets(answers.get(1)));
|
||||
// A group's name ending in the extension does not get it twice.
|
||||
assertEquals(List.of("2017-02-22/05/05.gz"), targets(answers.get(2)));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aSourcePrefixEndingInASlashNamesThatFolderAloneAndAnyOtherIsAPrefixAsS3ListsOne() throws Exception {
|
||||
// Hadoop's Path drops the line's trailing slash.
|
||||
List<JsonNode> answers = runDriver(PREFIX_DRIVER, List.of(
|
||||
List.of("s3://bucket/logs/day/", "/logs/day"),
|
||||
List.of("s3://bucket/logs/day", "/logs/day")));
|
||||
|
||||
assertEquals("/logs/day/", answers.get(0).asText());
|
||||
assertFalse("/logs/day-old/a.log".startsWith(answers.get(0).asText()));
|
||||
assertEquals("/logs/day", answers.get(1).asText());
|
||||
assertTrue("/logs/day-old/a.log".startsWith(answers.get(1).asText()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void numberFilesPutsASequentialNumberBeforeEachFileFromTheStartingIndex() throws Exception {
|
||||
List<JsonNode> answers = run(List.of(new Case(List.of("--src=s3://bucket/logs/", "--dest=hdfs:///output",
|
||||
"--srcPattern=.*\\.log", "--numberFiles", "--startingIndex=1"), LOGS)));
|
||||
|
||||
assertEquals(List.of("2017-02-22/05/1a.log", "2017-02-22/05/2b.log", "2017-02-22/06/3d.log"),
|
||||
targets(answers.get(0)));
|
||||
}
|
||||
|
||||
private static List<Object> file(String relative, long size) {
|
||||
return List.of("s3a://bucket/logs/" + relative, relative, size);
|
||||
}
|
||||
|
||||
private static List<String> concat(List<String> argv, String... more) {
|
||||
List<String> all = new ArrayList<>(argv);
|
||||
all.addAll(List.of(more));
|
||||
return all;
|
||||
}
|
||||
|
||||
private record Case(List<String> argv, List<List<Object>> files, String outputExtension) {
|
||||
Case(List<String> argv) {
|
||||
this(argv, null, null);
|
||||
}
|
||||
|
||||
Case(List<String> argv, List<List<Object>> files) {
|
||||
this(argv, files, null);
|
||||
}
|
||||
}
|
||||
|
||||
private static List<JsonNode> run(List<Case> cases) throws Exception {
|
||||
List<Object> json = cases.stream().map(c -> {
|
||||
Map<String, Object> node = new LinkedHashMap<>();
|
||||
node.put("argv", c.argv());
|
||||
if (c.files() != null) {
|
||||
node.put("files", c.files());
|
||||
node.put("extensions", Map.of("s3a://bucket/logs/2017-02-22/05/c.log.gz", ".gz"));
|
||||
node.put("outputExtension", c.outputExtension());
|
||||
}
|
||||
return (Object) node;
|
||||
}).toList();
|
||||
return runDriver(DRIVER, json);
|
||||
}
|
||||
|
||||
private static List<JsonNode> runDriver(String program, Object cases) throws Exception {
|
||||
Path driver = folder.resolve("driver.py");
|
||||
Files.writeString(driver, program);
|
||||
Path input = folder.resolve("cases.json");
|
||||
Files.writeString(input, MAPPER.writeValueAsString(cases));
|
||||
BootstrapProcess.Result ended = BootstrapProcess.run(new ProcessBuilder("python3", "-I",
|
||||
driver.toString(), folder.resolve("s3-dist-cp.py").toString()).redirectInput(input.toFile()));
|
||||
assertEquals(0, ended.exit(), ended.output());
|
||||
List<JsonNode> answers = new ArrayList<>();
|
||||
MAPPER.readTree(ended.output()).forEach(answers::add);
|
||||
return answers;
|
||||
}
|
||||
|
||||
private static JsonNode outputs(String json) throws Exception {
|
||||
return MAPPER.readTree(json);
|
||||
}
|
||||
|
||||
private static List<String> targets(JsonNode answer) {
|
||||
List<String> targets = new ArrayList<>();
|
||||
answer.path("outputs").forEach(output -> targets.add(output.get(0).asText()));
|
||||
return targets;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user