mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -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
@@ -1185,9 +1185,10 @@ public class EmrService implements Resettable {
|
||||
* Why a step whose driver ended this way, by itself or before it could start, failed, or null when it
|
||||
* did not. No source gives AWS's wording for these; they are Floci's.
|
||||
*/
|
||||
private static String failureOf(SparkJobRunner.Outcome outcome) {
|
||||
private static String failureOf(SparkJobRunner.Job job, SparkJobRunner.Outcome outcome) {
|
||||
if (outcome.exitCode() != null) {
|
||||
return outcome.exitCode() == 0 ? null : "The Spark driver exited with code " + outcome.exitCode() + ".";
|
||||
String ended = job.command() != null ? "The step's command" : "The Spark driver";
|
||||
return outcome.exitCode() == 0 ? null : ended + " exited with code " + outcome.exitCode() + ".";
|
||||
}
|
||||
return "The step could not start: " + outcome.failure();
|
||||
}
|
||||
@@ -1211,11 +1212,13 @@ public class EmrService implements Resettable {
|
||||
public void start(EmrCluster cluster, EmrStep step, String region) {
|
||||
String accountId = regionResolver.getAccountId();
|
||||
List<String> application = EmrSparkSteps.application(step);
|
||||
SparkJobRunner.Job job = application == null ? null : new SparkJobRunner.Job(SERVICE, step.getId(),
|
||||
accountId, region, cluster.getReleaseLabel(), imageTemplate, dockerNetwork,
|
||||
executionRole(cluster, step, accountId),
|
||||
EmrSparkSteps.sparkDefaults(cluster.getConfigurations(), SNAPSHOT_MAPPER), application,
|
||||
Duration.ZERO, EmrSparkSteps.logFolder(cluster, step), null);
|
||||
List<String> command = application == null ? EmrSparkSteps.command(step) : null;
|
||||
SparkJobRunner.Job job = application == null && command == null ? null
|
||||
: new SparkJobRunner.Job(SERVICE, step.getId(), accountId, region, cluster.getReleaseLabel(),
|
||||
imageTemplate, dockerNetwork, executionRole(cluster, step, accountId),
|
||||
EmrSparkSteps.sparkDefaults(cluster.getConfigurations(), SNAPSHOT_MAPPER),
|
||||
application != null ? application : List.of(), command, Duration.ZERO,
|
||||
EmrSparkSteps.logFolder(cluster, step), null);
|
||||
StepLaunch launch = new StepLaunch(accountId, region, cluster.getId(), step.getId(), generation);
|
||||
executing.add(stepKey(accountId, step.getId()));
|
||||
stepExecutor.execute(() -> runStep(job, launch));
|
||||
@@ -1244,7 +1247,7 @@ public class EmrService implements Resettable {
|
||||
RequestScopes.runAs(launch.accountId(), launch.region(), () -> run(() -> driverStopped(launch)));
|
||||
return;
|
||||
}
|
||||
failure = failureOf(outcome);
|
||||
failure = failureOf(job, outcome);
|
||||
}
|
||||
RequestScopes.runAs(launch.accountId(), launch.region(), () -> run(() -> stepEnded(launch, failure)));
|
||||
}
|
||||
|
||||
@@ -14,16 +14,18 @@ import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* What a step runs in the {@code docker} runner mode, which runs Spark steps only, each through
|
||||
* {@code spark-submit} in local mode: a {@code command-runner.jar} step running {@code spark-submit}, as
|
||||
* the Management Guide's Spark steps are written, and a custom JAR step, whose main class
|
||||
* {@code spark-submit} runs with the JAR on its class path.
|
||||
* What a step runs in the {@code docker} runner mode, in a container of the release's EMR Serverless image.
|
||||
* A Spark step runs through {@code spark-submit} in local mode: a {@code command-runner.jar} step running
|
||||
* {@code spark-submit}, as the Management Guide's Spark steps are written, and a custom JAR step, whose main
|
||||
* class {@code spark-submit} runs with the JAR on its class path. Any other {@code command-runner.jar} step
|
||||
* runs its command, and a {@code script-runner.jar} step its script, as the Release Guide's "Run commands
|
||||
* and scripts on an Amazon EMR cluster" describes them.
|
||||
*/
|
||||
final class EmrSparkSteps {
|
||||
|
||||
// No source gives AWS's wording for these; they are Floci's.
|
||||
static final String NOT_RUN = "Floci runs Spark steps only: spark-submit through command-runner.jar, or a"
|
||||
+ " custom JAR.";
|
||||
static final String NOT_RUN = "Floci does not run this step: hive-script, pig-script, hadoop-streaming,"
|
||||
+ " hadoop-lzo and s3-dist-cp are not in the image steps run in.";
|
||||
static final String INTERRUPTED = "Floci stopped while the step was running.";
|
||||
|
||||
private static final String COMMAND_RUNNER = "command-runner.jar";
|
||||
@@ -35,6 +37,31 @@ final class EmrSparkSteps {
|
||||
private static final Set<String> SWITCHES = Set.of("--help", "-h", "--supervise", "--usage-error", "--verbose",
|
||||
"-v", "--version");
|
||||
private static final String SPARK_DEFAULTS = "spark-defaults";
|
||||
// The Release Guide's command-runner.jar tools that the EMR Serverless image does not have; its hadoop
|
||||
// has no HDFS to run streaming or the LZO indexer against.
|
||||
private static final Set<String> UNAVAILABLE_TOOLS = Set.of("hive-script", "pig-script", "hadoop-streaming",
|
||||
"hadoop-lzo", "s3-dist-cp");
|
||||
// script-runner.jar fetches its script from S3 with the step's credentials, the image's AWS CLI reaching
|
||||
// Floci's S3, and runs it with its arguments; a path that is not in S3 is run as it is, a bare name from
|
||||
// the working directory when a file there has it, as exec would otherwise look for it on the PATH only.
|
||||
private static final String SCRIPT_RUNNER_COMMAND = String.join("\n",
|
||||
"script=$1",
|
||||
"shift",
|
||||
"case \"$script\" in",
|
||||
" s3://*|s3n://*|s3a://*)",
|
||||
" mkdir -p /tmp/floci-script-runner",
|
||||
" local_copy=\"/tmp/floci-script-runner/${script##*/}\"",
|
||||
" aws s3 cp --only-show-errors \"s3://${script#*://}\" \"$local_copy\" || exit $?",
|
||||
" script=$local_copy",
|
||||
" ;;",
|
||||
" */*)",
|
||||
" ;;",
|
||||
" *)",
|
||||
" if [ -f \"$script\" ]; then script=\"./$script\"; fi",
|
||||
" ;;",
|
||||
"esac",
|
||||
"chmod u+x \"$script\"",
|
||||
"exec \"$script\" \"$@\"");
|
||||
|
||||
private EmrSparkSteps() {
|
||||
}
|
||||
@@ -67,6 +94,29 @@ final class EmrSparkSteps {
|
||||
return application;
|
||||
}
|
||||
|
||||
/**
|
||||
* The command a step that is not a Spark step runs, or null for one Floci does not run: a
|
||||
* {@code command-runner.jar} step's arguments, its command first, unless it names a tool the image does
|
||||
* not have; a {@code script-runner.jar} step's script, the first of its arguments, with the rest.
|
||||
*/
|
||||
static List<String> command(EmrStep step) {
|
||||
String jar = step.getJar();
|
||||
List<String> args = step.getArgs() != null ? step.getArgs() : List.of();
|
||||
if (jar == null || args.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
if (named(jar, COMMAND_RUNNER)) {
|
||||
return named(args.getFirst(), SPARK_SUBMIT) || UNAVAILABLE_TOOLS.contains(args.getFirst())
|
||||
? null : List.copyOf(args);
|
||||
}
|
||||
if (named(jar, SCRIPT_RUNNER)) {
|
||||
List<String> command = new ArrayList<>(List.of("bash", "-c", SCRIPT_RUNNER_COMMAND, "script-runner"));
|
||||
command.addAll(args);
|
||||
return command;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* The S3 folder a step's logs go to, or null when its cluster has no {@code LogUri}: "If a value is not
|
||||
* provided, logs are not created". The Management Guide's "Log locations" puts step logs at
|
||||
|
||||
+1
-1
@@ -464,7 +464,7 @@ public class EmrServerlessJobRunService implements Resettable {
|
||||
? Duration.ZERO : Duration.ofMinutes(minutes);
|
||||
SparkJobRunner.Job job = new SparkJobRunner.Job(SERVICE, run.getJobRunId(), arn.accountId(), arn.region(),
|
||||
run.getReleaseLabel(), imageTemplate, dockerNetwork, run.getExecutionRole(), sparkDefaults(app, run),
|
||||
application(run.getJobDriver().getSparkSubmit()), timeout, driverLogFolder(app, run),
|
||||
application(run.getJobDriver().getSparkSubmit()), null, timeout, driverLogFolder(app, run),
|
||||
driverCloudWatchLogs(app, run));
|
||||
executing.add(run.getJobRunId());
|
||||
long launchedIn = generation;
|
||||
|
||||
+81
-13
@@ -54,6 +54,21 @@ public class EmrServerlessSparkRunner implements SparkJobRunner, ContainerTeardo
|
||||
|
||||
private static final Logger LOG = Logger.getLogger(EmrServerlessSparkRunner.class);
|
||||
private static final String SPARK_SUBMIT = "/usr/lib/spark/bin/spark-submit";
|
||||
private static final String SPARK_DEFAULTS_VARIABLE = "FLOCI_SPARK_DEFAULTS";
|
||||
// A command runs after this sets up a Spark configuration of its own, so a spark-submit it starts runs
|
||||
// locally on Floci's S3: the image's spark-defaults.conf names EMR Serverless's resource manager as its
|
||||
// master, which a system property cannot override, so its master and that manager's lines give way to
|
||||
// local mode, Floci's settings and the job's spark-defaults, passed in SPARK_DEFAULTS_VARIABLE.
|
||||
private static final String COMMAND_WRAPPER = String.join("\n",
|
||||
"conf=/tmp/floci-spark-conf",
|
||||
"mkdir -p \"$conf\"",
|
||||
"cp -r /usr/lib/spark/conf/. \"$conf\" 2>/dev/null",
|
||||
"touch \"$conf/spark-defaults.conf\"",
|
||||
"sed -i -e '/^spark\\.master[[:space:]]/d' -e '/^spark\\.submit\\.deployMode[[:space:]]/d'"
|
||||
+ " -e '/^spark\\.submit\\.customResourceManager/d' \"$conf/spark-defaults.conf\"",
|
||||
"printf '%s\\n' \"$" + SPARK_DEFAULTS_VARIABLE + "\" >> \"$conf/spark-defaults.conf\"",
|
||||
"export SPARK_CONF_DIR=\"$conf\"",
|
||||
"exec \"$@\"");
|
||||
private static final String DEFAULT_IMAGE =
|
||||
"public.ecr.aws/emr-serverless/spark/%s:latest"; // partition-literal: ECR Public's registry host, where AWS publishes the base images; image-template overrides it
|
||||
// The AWS runtime for Apache Spark (emr-spark-8.0.0 on) publishes its images under spark4/ instead.
|
||||
@@ -125,11 +140,19 @@ public class EmrServerlessSparkRunner implements SparkJobRunner, ContainerTeardo
|
||||
// The deployment that started the driver, which removeLeftoverDrivers() goes by.
|
||||
labels.put(ContainerStorageHelper.OWNER_LABEL, ContainerStorageHelper.ownerIdentity(config));
|
||||
String endpoint = awsEnv.flociEndpoint();
|
||||
ContainerSpec spec = containerBuilder.newContainer(image(job.imageTemplate(), job.releaseLabel()))
|
||||
ContainerBuilder.Builder container = containerBuilder.newContainer(
|
||||
image(job.imageTemplate(), job.releaseLabel()))
|
||||
.withName(ContainerStorageHelper.dockerName(config, job.service() + "-" + job.runId()))
|
||||
.withEntrypoint(List.of(SPARK_SUBMIT))
|
||||
.withCmd(sparkSubmitArguments(job, endpoint))
|
||||
.withEnv(awsEnv.sdkBaselineEnv(job.region(), Optional.empty(), credentials, job.accountId()))
|
||||
.withEnv(awsEnv.sdkBaselineEnv(job.region(), Optional.empty(), credentials, job.accountId()));
|
||||
if (job.command() == null) {
|
||||
container.withEntrypoint(List.of(SPARK_SUBMIT)).withCmd(sparkSubmitArguments(job, endpoint));
|
||||
} else {
|
||||
// bash -c's script gets "floci" as its $0, and the command as its arguments, "$@".
|
||||
container.withEntrypoint(List.of("/bin/bash", "-c", COMMAND_WRAPPER, "floci"))
|
||||
.withCmd(job.command())
|
||||
.withEnv(SPARK_DEFAULTS_VARIABLE, sparkDefaultsFile(job, endpoint));
|
||||
}
|
||||
ContainerSpec spec = container
|
||||
.withDockerNetwork(Optional.ofNullable(job.dockerNetwork()))
|
||||
.withHostDockerInternalOnLinux()
|
||||
.withEmbeddedDns()
|
||||
@@ -276,19 +299,64 @@ public class EmrServerlessSparkRunner implements SparkJobRunner, ContainerTeardo
|
||||
*/
|
||||
static List<String> sparkSubmitArguments(Job job, String flociEndpoint) {
|
||||
List<String> arguments = new ArrayList<>(List.of("--master", "local[*]"));
|
||||
conf(arguments, "spark.hadoop.fs.s3a.endpoint", flociEndpoint);
|
||||
conf(arguments, "spark.hadoop.fs.s3a.endpoint.region", job.region());
|
||||
conf(arguments, "spark.hadoop.fs.s3a.path.style.access", "true");
|
||||
conf(arguments, "spark.hadoop.fs.s3a.committer.magic.track.commits.in.memory.enabled", "true");
|
||||
conf(arguments, "spark.hadoop.fs.s3.impl", "org.apache.hadoop.fs.s3a.S3AFileSystem");
|
||||
conf(arguments, "spark.hadoop.fs.AbstractFileSystem.s3.impl", "org.apache.hadoop.fs.s3a.S3A");
|
||||
conf(arguments, "spark.hadoop.aws.glue.endpoint", flociEndpoint);
|
||||
conf(arguments, "spark.hadoop.aws.region", job.region());
|
||||
job.sparkDefaults().forEach((key, value) -> conf(arguments, key, value));
|
||||
for (SparkSetting setting : sparkSettings(job, flociEndpoint)) {
|
||||
conf(arguments, setting.key(), setting.value());
|
||||
}
|
||||
arguments.addAll(job.application());
|
||||
return arguments;
|
||||
}
|
||||
|
||||
/**
|
||||
* The {@code spark-defaults.conf} lines a command's own {@code spark-submit} runs with: local mode, then
|
||||
* the settings {@link #sparkSubmitArguments} passes, a later line winning over an earlier one, and the
|
||||
* submit's own options over them all. Spark reads the file as a {@code Properties} file, so each
|
||||
* key and value is escaped for it, to reach Spark as the job gave it.
|
||||
*/
|
||||
static String sparkDefaultsFile(Job job, String flociEndpoint) {
|
||||
StringBuilder file = new StringBuilder("spark.master local[*]");
|
||||
for (SparkSetting setting : sparkSettings(job, flociEndpoint)) {
|
||||
file.append('\n').append(propertiesEscaped(setting.key(), true)).append(' ')
|
||||
.append(propertiesEscaped(String.valueOf(setting.value()), false));
|
||||
}
|
||||
return file.toString();
|
||||
}
|
||||
|
||||
// A backslash starts an escape and a line break ends a setting; in a key, whitespace, '=' and ':' end it,
|
||||
// and a leading '#' or '!' makes the line a comment.
|
||||
private static String propertiesEscaped(String text, boolean key) {
|
||||
StringBuilder escaped = new StringBuilder();
|
||||
for (char c : text.toCharArray()) {
|
||||
switch (c) {
|
||||
case '\\' -> escaped.append("\\\\");
|
||||
case '\n' -> escaped.append("\\n");
|
||||
case '\r' -> escaped.append("\\r");
|
||||
case '\t' -> escaped.append("\\t");
|
||||
case '\f' -> escaped.append("\\f");
|
||||
case ' ', '=', ':', '#', '!' -> escaped.append(key ? "\\" : "").append(c);
|
||||
default -> escaped.append(c);
|
||||
}
|
||||
}
|
||||
return escaped.toString();
|
||||
}
|
||||
|
||||
// Floci's settings, then the job's spark-defaults, in the order a later one wins.
|
||||
private static List<SparkSetting> sparkSettings(Job job, String flociEndpoint) {
|
||||
List<SparkSetting> settings = new ArrayList<>(List.of(
|
||||
new SparkSetting("spark.hadoop.fs.s3a.endpoint", flociEndpoint),
|
||||
new SparkSetting("spark.hadoop.fs.s3a.endpoint.region", job.region()),
|
||||
new SparkSetting("spark.hadoop.fs.s3a.path.style.access", "true"),
|
||||
new SparkSetting("spark.hadoop.fs.s3a.committer.magic.track.commits.in.memory.enabled", "true"),
|
||||
new SparkSetting("spark.hadoop.fs.s3.impl", "org.apache.hadoop.fs.s3a.S3AFileSystem"),
|
||||
new SparkSetting("spark.hadoop.fs.AbstractFileSystem.s3.impl", "org.apache.hadoop.fs.s3a.S3A"),
|
||||
new SparkSetting("spark.hadoop.aws.glue.endpoint", flociEndpoint),
|
||||
new SparkSetting("spark.hadoop.aws.region", job.region())));
|
||||
job.sparkDefaults().forEach((key, value) -> settings.add(new SparkSetting(key, value)));
|
||||
return settings;
|
||||
}
|
||||
|
||||
private record SparkSetting(String key, String value) {
|
||||
}
|
||||
|
||||
/**
|
||||
* {@code spark-submit} parameters given as one string, as EMR Serverless's
|
||||
* {@code sparkSubmitParameters}, split as a POSIX shell would split them: on whitespace, except inside single
|
||||
|
||||
@@ -53,14 +53,18 @@ public interface SparkJobRunner {
|
||||
* the release's public EMR Serverless image, a null network the global default); the execution role
|
||||
* whose credentials it gets; the {@code spark-defaults} properties that apply to it;
|
||||
* {@code application}, what follows Floci's own settings on {@code spark-submit}'s command line (the
|
||||
* run's options, then its application and that application's arguments); how long it may run
|
||||
* run's options, then its application and that application's arguments); or {@code command}, when not
|
||||
* null, what the container runs in place of {@code spark-submit}, where a {@code spark-submit} it starts
|
||||
* gets Floci's settings and the {@code spark-defaults} as defaults (its {@code application} is then
|
||||
* empty); how long it may run
|
||||
* ({@link Duration#ZERO} for no limit); the S3 folder its driver's {@code stdout.gz} and
|
||||
* {@code stderr.gz} go to once it ends, ending in a slash (null for none); and where in CloudWatch Logs
|
||||
* its driver's output goes while it runs (null for nowhere but Floci's log).
|
||||
*/
|
||||
record Job(String service, String runId, String accountId, String region, String releaseLabel,
|
||||
String imageTemplate, String dockerNetwork, String executionRole, Map<String, String> sparkDefaults,
|
||||
List<String> application, Duration timeout, String logFolder, CloudWatchLogs cloudWatchLogs) {
|
||||
List<String> application, List<String> command, Duration timeout, String logFolder,
|
||||
CloudWatchLogs cloudWatchLogs) {
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in new issue
Block a user