mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(emr): run command-runner.jar commands and script-runner.jar scripts (#6747)
In the docker runner mode a step that was not a Spark step failed without running, so a cluster whose steps copy files with the AWS CLI, run a shell or Python script, or call script-runner.jar could not run its steps in Floci. Such steps now run in the same per-step container of the release's EMR Serverless image, with the same credentials, endpoint, logs and lifecycle as a Spark step, as the Release Guide's "Run commands and scripts on an Amazon EMR cluster" describes them: - command-runner.jar runs its arguments as a command, the first naming it, unless that is spark-submit (the Spark step, as before) or a tool the image does not have: hive-script, pig-script, hadoop-streaming, hadoop-lzo and s3-dist-cp still fail without running. - script-runner.jar, by name (7.13 on) or by its s3://<region>.elasticmapreduce/libs/script-runner/script-runner.jar URI, fetches its script from S3 with the image's AWS CLI and the step's credentials, then runs it with the step's other arguments. A local script named without a path runs from the working directory when a file there has that name. The image runs them as its hadoop user in /home/hadoop, with bash, Python 3, Java, curl and AWS CLI v2, which reaches Floci through AWS_ENDPOINT_URL. A spark-submit they start runs in local mode on Floci's S3 with the cluster's spark-defaults: the image's spark-defaults.conf names EMR Serverless's resource manager as its master, which a system property cannot override, so the container starts through a wrapper that copies the image's Spark configuration, drops that master, and adds local mode, Floci's settings and the cluster's spark-defaults, in a SPARK_CONF_DIR of its own, each setting escaped for the Properties file Spark reads it as. A step's own --conf still wins. A failed command reads "The step's command exited with code N.". SparkJobRunner.Job gets a command, run in place of spark-submit.
This commit is contained in:
1 parent
ac42b3064e
commit
897667f4e6
9 files changed
+336
-43
No files matched your search
+33
@@ -79,6 +79,7 @@ class EmrServiceContainerStepsTest {
|
||||
assertNull(job.imageTemplate());
|
||||
assertNull(job.dockerNetwork());
|
||||
assertEquals(List.of(REPORT, "s3://bucket/out/"), job.application());
|
||||
assertNull(job.command());
|
||||
assertEquals(Map.of("spark.executor.cores", "2"), job.sparkDefaults());
|
||||
assertEquals(Duration.ZERO, job.timeout());
|
||||
assertNull(job.logFolder());
|
||||
@@ -215,6 +216,38 @@ class EmrServiceContainerStepsTest {
|
||||
assertEquals("RUNNING", service.describeStep(cluster.getId(), cluster.getSteps().get(1).getId()).getState());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aCommandStepRunsItsCommandAndFailsWithItsExitCode() {
|
||||
EmrStep copy = sparkStep("copy", "CONTINUE");
|
||||
copy.setArgs(List.of("bash", "-c", "aws s3 cp s3://bucket/in/ s3://bucket/out/ --recursive"));
|
||||
EmrCluster cluster = service.runJobFlow(cluster("commands", true, copy), REGION);
|
||||
runner.outcome = SparkJobRunner.Outcome.exitedWith(1);
|
||||
|
||||
fixture.runStepThread();
|
||||
|
||||
SparkJobRunner.Job job = runner.jobs.get(0);
|
||||
assertEquals(copy.getArgs(), job.command());
|
||||
assertEquals(List.of(), job.application());
|
||||
EmrStep failed = service.describeStep(cluster.getId(), cluster.getSteps().get(0).getId());
|
||||
assertEquals("FAILED", failed.getState());
|
||||
assertEquals("The step's command exited with code 1.", failed.getFailureMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aScriptRunnerStepRunsItsScriptAsACommand() {
|
||||
EmrStep script = sparkStep("script", "CONTINUE");
|
||||
script.setJar("s3://us-east-1.elasticmapreduce/libs/script-runner/script-runner.jar");
|
||||
script.setArgs(List.of("s3://bucket/scripts/load.sh", "2026-10-26"));
|
||||
EmrCluster cluster = service.runJobFlow(cluster("scripts", true, script), REGION);
|
||||
|
||||
fixture.runStepThread();
|
||||
|
||||
List<String> command = runner.jobs.get(0).command();
|
||||
assertEquals(List.of("script-runner", "s3://bucket/scripts/load.sh", "2026-10-26"),
|
||||
command.subList(3, command.size()));
|
||||
assertEquals("COMPLETED", service.describeStep(cluster.getId(), cluster.getSteps().get(0).getId()).getState());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aStepFlociDoesNotRunFailsWithoutRunning() {
|
||||
EmrStep hive = sparkStep("query", "CONTINUE");
|
||||
|
||||
@@ -2,13 +2,18 @@ package io.github.hectorvent.floci.services.emr;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.github.hectorvent.floci.services.emr.model.EmrStep;
|
||||
import io.github.hectorvent.floci.services.lambda.launcher.BootstrapProcess;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
|
||||
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.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
class EmrSparkStepsTest {
|
||||
|
||||
@@ -48,6 +53,56 @@ class EmrSparkStepsTest {
|
||||
assertNull(EmrSparkSteps.application(step(null, null)));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aCommandRunnerStepThatIsNotSparkRunsItsCommand() {
|
||||
// The Release Guide's own example: download a script, make it executable, run it.
|
||||
List<String> bash = List.of("bash", "-c",
|
||||
"aws s3 cp s3://bucket/my-script.sh /home/hadoop; chmod u+x /home/hadoop/my-script.sh;"
|
||||
+ " cd /home/hadoop; ./my-script.sh");
|
||||
|
||||
assertEquals(bash, EmrSparkSteps.command(step("command-runner.jar", null, bash.toArray(String[]::new))));
|
||||
assertEquals(List.of("python3", "-c", "print(1)"),
|
||||
EmrSparkSteps.command(step("command-runner.jar", null, "python3", "-c", "print(1)")));
|
||||
assertNull(EmrSparkSteps.command(
|
||||
step("command-runner.jar", null, "spark-submit", "s3://bucket/jobs/report.py")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aScriptRunnerStepFetchesItsScriptAndRunsItWithItsArguments() {
|
||||
for (String jar : List.of("script-runner.jar",
|
||||
"s3://us-east-1.elasticmapreduce/libs/script-runner/script-runner.jar")) {
|
||||
List<String> command = EmrSparkSteps.command(step(jar, null, "s3://bucket/my-script.sh", "--day", "1"));
|
||||
|
||||
assertEquals(List.of("bash", "-c"), command.subList(0, 2));
|
||||
assertTrue(command.get(2).contains("aws s3 cp --only-show-errors"));
|
||||
assertEquals(List.of("script-runner", "s3://bucket/my-script.sh", "--day", "1"),
|
||||
command.subList(3, command.size()));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void aScriptRunnerScriptNamedWithoutAPathRunsFromTheWorkingDirectory(@TempDir Path workingDirectory)
|
||||
throws Exception {
|
||||
// Not executable yet: script-runner makes it so, as it does a script it fetched from S3.
|
||||
Files.writeString(workingDirectory.resolve("my-script.sh"), "#!/bin/sh\nprintf 'ran:%s|' \"$@\"\n");
|
||||
List<String> command = EmrSparkSteps.command(step("script-runner.jar", null, "my-script.sh", "--day", "1 2"));
|
||||
|
||||
BootstrapProcess.Result ended =
|
||||
BootstrapProcess.run(new ProcessBuilder(command).directory(workingDirectory.toFile()));
|
||||
|
||||
assertEquals(new BootstrapProcess.Result(0, "ran:--day|ran:1 2|"), ended);
|
||||
}
|
||||
|
||||
@Test
|
||||
void toolsTheImageDoesNotHaveAreNotRun() {
|
||||
for (String tool : List.of("hive-script", "pig-script", "hadoop-streaming", "hadoop-lzo", "s3-dist-cp")) {
|
||||
assertNull(EmrSparkSteps.command(step("command-runner.jar", null, tool, "--src", "s3://bucket/in/")));
|
||||
}
|
||||
assertNull(EmrSparkSteps.command(step("command-runner.jar", null)));
|
||||
assertNull(EmrSparkSteps.command(step("script-runner.jar", null)));
|
||||
assertNull(EmrSparkSteps.command(step("s3://bucket/jars/app.jar", null, "1")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void theClustersSparkDefaultsComeFromItsConfigurations() {
|
||||
String configurations = """
|
||||
|
||||
+68
-1
@@ -23,12 +23,14 @@ import org.mockito.InOrder;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.Closeable;
|
||||
import java.io.IOException;
|
||||
import java.io.StringReader;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.time.Duration;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Properties;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.zip.GZIPInputStream;
|
||||
|
||||
@@ -130,12 +132,71 @@ class EmrServerlessSparkRunnerTest {
|
||||
runner(builder).run(job(Duration.ZERO), progress);
|
||||
|
||||
verify(builder).newContainer("public.ecr.aws/emr-serverless/spark/emr-7.5.0:latest");
|
||||
verify(spec).withEntrypoint(List.of("/usr/lib/spark/bin/spark-submit"));
|
||||
verify(spec).withName(ContainerStorageHelper.dockerName(config, "emr-serverless-00f0000000000001"));
|
||||
verify(spec).withDockerNetwork(Optional.of("floci-network"));
|
||||
verify(spec).withLabels(argThat(labels ->
|
||||
"emr-serverless".equals(labels.get(ContainerStorageHelper.SERVICE_LABEL))));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aCommandRunsThroughFlocisWrapperWithTheJobsSparkSettingsInItsEnvironment() {
|
||||
ContainerBuilder builder = mock(ContainerBuilder.class);
|
||||
ContainerBuilder.Builder spec = mock(ContainerBuilder.Builder.class, RETURNS_SELF);
|
||||
when(builder.newContainer(anyString())).thenReturn(spec);
|
||||
List<String> command = List.of("bash", "-c", "aws s3 ls s3://bucket/");
|
||||
|
||||
runner(builder).run(commandJob(command, Map.of("spark.executor.cores", "4")), progress);
|
||||
|
||||
// bash -c runs Floci's wrapper with "floci" as its $0 and the command as "$@".
|
||||
verify(spec).withEntrypoint(argThat(entrypoint -> entrypoint.size() == 4
|
||||
&& entrypoint.subList(0, 2).equals(List.of("/bin/bash", "-c"))
|
||||
&& entrypoint.get(2).contains("export SPARK_CONF_DIR=") && entrypoint.get(2).endsWith("exec \"$@\"")
|
||||
&& "floci".equals(entrypoint.get(3))));
|
||||
verify(spec).withCmd(command);
|
||||
verify(spec).withEnv(eq("FLOCI_SPARK_DEFAULTS"),
|
||||
argThat(file -> file.startsWith("spark.master local[*]\n")
|
||||
&& file.endsWith("\nspark.executor.cores 4")));
|
||||
verify(spec, never()).withEntrypoint(List.of("/usr/lib/spark/bin/spark-submit"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aCommandsOwnSparkSubmitRunsLocallyOnFlocisS3WithTheJobsSparkDefaults() {
|
||||
assertEquals(String.join("\n",
|
||||
"spark.master local[*]",
|
||||
"spark.hadoop.fs.s3a.endpoint http://floci:4566",
|
||||
"spark.hadoop.fs.s3a.endpoint.region eu-west-1",
|
||||
"spark.hadoop.fs.s3a.path.style.access true",
|
||||
"spark.hadoop.fs.s3a.committer.magic.track.commits.in.memory.enabled true",
|
||||
"spark.hadoop.fs.s3.impl org.apache.hadoop.fs.s3a.S3AFileSystem",
|
||||
"spark.hadoop.fs.AbstractFileSystem.s3.impl org.apache.hadoop.fs.s3a.S3A",
|
||||
"spark.hadoop.aws.glue.endpoint http://floci:4566",
|
||||
"spark.hadoop.aws.region eu-west-1",
|
||||
"spark.sql.shuffle.partitions 8"),
|
||||
EmrServerlessSparkRunner.sparkDefaultsFile(
|
||||
commandJob(List.of("bash", "-c", "spark-submit s3://bucket/jobs/app.py"),
|
||||
Map.of("spark.sql.shuffle.partitions", "8")), ENDPOINT));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aCommandsSparkDefaultsReachSparkAsTheJobGaveThem() throws IOException {
|
||||
Map<String, String> sparkDefaults = new LinkedHashMap<>();
|
||||
sparkDefaults.put("spark.driver.extraJavaOptions", "-Dpattern=\\d+ -Dpath=C:\\tmp");
|
||||
sparkDefaults.put("spark.odd key=with:separators", "#not a comment\nnor a second line\t!");
|
||||
sparkDefaults.put("!spark.leading", "kept");
|
||||
|
||||
// Spark's Utils.getPropertiesFromFile loads spark-defaults.conf with Properties.load.
|
||||
Properties loaded = new Properties();
|
||||
loaded.load(new StringReader(EmrServerlessSparkRunner.sparkDefaultsFile(
|
||||
commandJob(List.of("bash", "-c", "spark-submit s3://bucket/jobs/app.py"), sparkDefaults), ENDPOINT)));
|
||||
|
||||
assertEquals("local[*]", loaded.getProperty("spark.master"));
|
||||
assertEquals("-Dpattern=\\d+ -Dpath=C:\\tmp", loaded.getProperty("spark.driver.extraJavaOptions"));
|
||||
assertEquals("#not a comment\nnor a second line\t!", loaded.getProperty("spark.odd key=with:separators"));
|
||||
assertEquals("kept", loaded.getProperty("!spark.leading"));
|
||||
assertEquals(ENDPOINT, loaded.getProperty("spark.hadoop.fs.s3a.endpoint"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aTemplateThatNamesNoImageFailsTheRunWithItsReason() {
|
||||
EmrServerlessSparkRunner runner = runner();
|
||||
@@ -488,6 +549,12 @@ class EmrServerlessSparkRunnerTest {
|
||||
SparkJobRunner.CloudWatchLogs cloudWatchLogs) {
|
||||
return new SparkJobRunner.Job("emr-serverless", "00f0000000000001", "123456789012", "eu-west-1", "emr-7.5.0",
|
||||
imageTemplate, "floci-network", "arn:aws:iam::123456789012:role/job", sparkDefaults, application,
|
||||
timeout, logFolder, cloudWatchLogs);
|
||||
null, timeout, logFolder, cloudWatchLogs);
|
||||
}
|
||||
|
||||
private static SparkJobRunner.Job commandJob(List<String> command, Map<String, String> sparkDefaults) {
|
||||
return new SparkJobRunner.Job("emr", "s-1A2B3C4D5E6F7", "123456789012", "eu-west-1", "emr-7.5.0", null,
|
||||
"floci-network", "arn:aws:iam::123456789012:role/job", sparkDefaults, List.of(), command,
|
||||
Duration.ZERO, null, null);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user