mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
feat(emr-serverless): run Spark job runs in a container (#6469)
A new runner mode, floci.services.emrserverless.runner-mode=docker,
runs each Spark job run. Floci starts one container per run from the
release's EMR Serverless base image on Amazon ECR Public, and starts
spark-submit in local mode in it itself, since the image's own
entrypoint hands over to a script only EMR Serverless supplies. The
default, immediate, keeps today's behaviour.
A docker run is SCHEDULED while its image is pulled and its container
starts, RUNNING once the Spark driver runs, then SUCCESS when it exits
0 and FAILED otherwise, with the exit code in stateDetails, as the
user guide's job run states describe. CancelJobRun, or a BATCH run
passing its executionTimeoutMinutes, makes it CANCELLING until the
driver is stopped, then CANCELLED. A run left by a Floci restart is
FAILED once read.
Floci's shutdown and a state reset stop every driver, one whose image
is still being pulled as soon as its container is up, and a reset
raises a generation the runs' threads check, so none writes its run
back after the storage wipe. A driver a hard stop left running is
removed before this process starts one: driver containers carry the
deployment's owner label, as EC2's and ECS's do, and
reconcile-containers-on-startup turns the removal off, as theirs does,
for two Flocis sharing a daemon. A stop that comes after its run has
ended is dropped, and a cancel before a run starts is read from the
run's state. A driver is watched with a look at its container every
250 ms, as Batch's runner watches its jobs, so a run holds no Docker
connection while its driver runs; a run past its timeout is
CANCELLING while its driver is stopped.
spark-submit gets the entry point, its arguments and the
sparkSubmitParameters (split as a POSIX shell would), after the
spark-defaults of the application's runtimeConfiguration, or the
run's own in its applicationConfiguration, which replace the
application's whole classification ("Only the properties included in
StartJobRun is considered for this classification"); from
emr-spark-8.1.0 the two merge by property, the run's winning, as its
release notes say. S3A takes s3:// and s3a:// paths to Floci's S3 on
every release, with in-memory commit tracking, without which EMR's
committer loses every write; the Glue Data Catalog client goes to
Floci's Glue. The container gets a session for the run's execution
role and AWS_ENDPOINT_URL, as the other containers Floci launches do.
The image is public.ecr.aws/emr-serverless/spark/<release>:latest, or
spark4/ in place of spark/ for an emr-spark-* release, where ECR
Public keeps the AWS runtime for Apache Spark images. image-template
names another image, and docker-network the network.
Docker answers a stop of a container that has already exited with
HTTP 304, which ContainerLifecycleManager logged as a failed stop
after every run that ended by itself. It now counts as stopped, as a
304 on start already counts as started, and so does the runner's stop
for a cancel that finds the driver already exited.
A Hive run fails without running. Log destinations, retries and the
cancellation grace period are not emulated; the driver's output goes
to Floci's log.
Part of #6046 (REAL-2).
This commit is contained in:
1 parent
68e7b313a8
commit
edbcac33c7
12 files changed
+1359
-25
No files matched your search
@@ -1104,6 +1104,34 @@ public interface EmulatorConfig {
|
||||
/** How long a BATCH job run stays RUNNING before it succeeds; 0 = a run succeeds as soon as it starts. */
|
||||
@WithDefault("0")
|
||||
int jobRunDurationSeconds();
|
||||
|
||||
/**
|
||||
* {@code immediate} follows the job run states without running anything; {@code docker} runs
|
||||
* each Spark job run's {@code spark-submit} in a container of the release's EMR Serverless image.
|
||||
*/
|
||||
@WithDefault("immediate")
|
||||
String runnerMode();
|
||||
|
||||
Optional<String> dockerNetwork();
|
||||
|
||||
/**
|
||||
* The image a {@code docker} job run uses, with {@code %s} standing for the application's
|
||||
* release label, for example "registry.internal/emr-serverless/spark/%s:20250425". If omitted,
|
||||
* Floci uses the release's public image on Amazon ECR Public.
|
||||
*/
|
||||
Optional<String> imageTemplate();
|
||||
|
||||
/**
|
||||
* When true, Floci removes, in the {@code docker} runner mode, every EMR Serverless driver a
|
||||
* previous run of <em>this same</em> Floci left on the daemon (matched by the
|
||||
* {@code io.floci.owner} label) before its first job run starts. Turn it off when two Floci
|
||||
* instances share a daemon with the same port and no resource namespace, so one does not
|
||||
* remove the other's drivers.
|
||||
*
|
||||
* Env var: FLOCI_SERVICES_EMRSERVERLESS_RECONCILE_CONTAINERS_ON_STARTUP
|
||||
*/
|
||||
@WithDefault("true")
|
||||
boolean reconcileContainersOnStartup();
|
||||
}
|
||||
|
||||
interface Route53ResolverServiceConfig {
|
||||
|
||||
+7
@@ -525,6 +525,10 @@ public class ContainerLifecycleManager {
|
||||
} catch (NotFoundException e) {
|
||||
LOG.debugv("Container {0} not found (already removed)", containerId);
|
||||
stoppedOrMissing = true;
|
||||
} catch (NotModifiedException e) {
|
||||
// HTTP 304: the container had already exited, which is the outcome the stop wanted.
|
||||
LOG.debugv("Container {0} had already stopped", containerId);
|
||||
stoppedOrMissing = true;
|
||||
} catch (Exception e) {
|
||||
LOG.warnv("Error stopping container {0}: {1}", containerId, e.getMessage());
|
||||
}
|
||||
@@ -586,6 +590,9 @@ public class ContainerLifecycleManager {
|
||||
LOG.debugv("Container {0} not found (already removed)", containerId);
|
||||
closeLogStreamIfPresent(logStream);
|
||||
return;
|
||||
} catch (NotModifiedException e) {
|
||||
// HTTP 304: the container had already exited, which is the outcome the stop wanted.
|
||||
LOG.debugv("Container {0} had already stopped", containerId);
|
||||
} catch (Exception e) {
|
||||
stopFailure = e;
|
||||
LOG.warnv("Error stopping container {0}: {1}", containerId, e.getMessage());
|
||||
|
||||
+295
-18
@@ -12,6 +12,8 @@ import io.github.hectorvent.floci.core.common.PaginatedResult;
|
||||
import io.github.hectorvent.floci.core.common.Pagination;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.core.common.RequestContext;
|
||||
import io.github.hectorvent.floci.core.common.RequestScopes;
|
||||
import io.github.hectorvent.floci.core.common.Resettable;
|
||||
import io.github.hectorvent.floci.core.common.SmithyValidationErrors;
|
||||
import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
|
||||
import io.github.hectorvent.floci.core.storage.StorageFactory;
|
||||
@@ -21,8 +23,10 @@ import io.github.hectorvent.floci.services.emrserverless.model.JobRun;
|
||||
import io.github.hectorvent.floci.services.emrserverless.model.JobRunAttemptSummary;
|
||||
import io.github.hectorvent.floci.services.emrserverless.model.JobRunRecord;
|
||||
import io.github.hectorvent.floci.services.emrserverless.model.JobRunSummary;
|
||||
import io.github.hectorvent.floci.services.emrserverless.model.RuntimeConfiguration;
|
||||
import io.github.hectorvent.floci.services.emrserverless.model.StartJobRunRequest;
|
||||
import io.github.hectorvent.floci.services.iam.IamService;
|
||||
import jakarta.annotation.PreDestroy;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
import org.jboss.logging.Logger;
|
||||
@@ -35,35 +39,54 @@ import java.time.format.DateTimeParseException;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* EMR Serverless job runs. Nothing executes: a run follows the job run states and GetJobRun's
|
||||
* shape, and settles lazily when read. Times are kept to the millisecond, as AWS reports them. With the default {@code job-run-duration-seconds} of 0 a
|
||||
* EMR Serverless job runs. In the default {@code immediate} runner mode nothing executes: a run
|
||||
* follows the job run states and GetJobRun's shape, and settles lazily when read. Times are kept to
|
||||
* the millisecond, as AWS reports them. With the default {@code job-run-duration-seconds} of 0 a
|
||||
* BATCH run is SUCCESS as soon as it starts; a positive duration keeps it RUNNING that long, which
|
||||
* makes CancelJobRun and the execution timeout observable. A STREAMING run, which EMR Serverless
|
||||
* "expects to run for a long time", stays RUNNING until it is cancelled.
|
||||
*
|
||||
* <p>In the {@code docker} runner mode a Spark run executes: it is SCHEDULED while its container is
|
||||
* prepared, RUNNING once its Spark driver runs, and ends as the driver does, SUCCESS on exit code 0
|
||||
* and FAILED otherwise. Cancelling it, or its execution timeout, makes it CANCELLING until the driver
|
||||
* is stopped, then CANCELLED.
|
||||
*
|
||||
* <p>Every public method is synchronized: admitting a run (look up its client token, then write
|
||||
* it), settling one and cancelling one each read and then write a run, and must not interleave.
|
||||
* it), settling one and cancelling one each read and then write a run, and must not interleave. A
|
||||
* container run's changes come from the thread that runs it, under the same lock.
|
||||
*/
|
||||
@ApplicationScoped
|
||||
public class EmrServerlessJobRunService {
|
||||
public class EmrServerlessJobRunService implements Resettable {
|
||||
|
||||
private static final Logger LOG = Logger.getLogger(EmrServerlessJobRunService.class);
|
||||
|
||||
static final String STATE_SCHEDULED = "SCHEDULED";
|
||||
static final String STATE_RUNNING = "RUNNING";
|
||||
static final String STATE_SUCCESS = "SUCCESS";
|
||||
static final String STATE_FAILED = "FAILED";
|
||||
static final String STATE_CANCELLING = "CANCELLING";
|
||||
static final String STATE_CANCELLED = "CANCELLED";
|
||||
private static final String MODE_BATCH = "BATCH";
|
||||
private static final String MODE_STREAMING = "STREAMING";
|
||||
private static final String RUNNER_DOCKER = "docker";
|
||||
private static final String SPARK_DEFAULTS = "spark-defaults";
|
||||
// No source gives AWS's wording for these; they are Floci's.
|
||||
static final String HIVE_NOT_RUN = "Floci runs Spark job runs only, and this is a Hive job run.";
|
||||
static final String INTERRUPTED = "Floci stopped while the job run was in progress.";
|
||||
|
||||
private static final Set<String> TERMINAL_STATES = Set.of(STATE_SUCCESS, "FAILED", STATE_CANCELLED);
|
||||
private static final Set<String> TERMINAL_STATES = Set.of(STATE_SUCCESS, STATE_FAILED, STATE_CANCELLED);
|
||||
private static final List<String> STATES = List.of("SUBMITTED", "PENDING", "SCHEDULED", STATE_RUNNING,
|
||||
STATE_SUCCESS, "FAILED", "CANCELLING", STATE_CANCELLED, "QUEUED");
|
||||
// JobRunStateSet holds at most 8 states.
|
||||
@@ -77,7 +100,10 @@ public class EmrServerlessJobRunService {
|
||||
// onwards", and the timeout "can no longer be set to 0".
|
||||
private static final long MAX_BATCH_EXECUTION_TIMEOUT_MINUTES = 10_080;
|
||||
private static final Pattern RELEASE_LABEL = Pattern.compile("emr-(\\d{1,6})\\.(\\d{1,6})(?:\\..*)?");
|
||||
// Nothing runs, so no run fails and none is retried: every run is its own one attempt.
|
||||
// The AWS runtime for Apache Spark, emr-spark-8.0.0 on (and emr-spark-8.0-preview).
|
||||
private static final Pattern SPARK_RUNTIME_RELEASE_LABEL =
|
||||
Pattern.compile("emr-spark-(\\d{1,6})\\.(\\d{1,6})(?:[.-].*)?");
|
||||
// No run is retried: every run is its own one attempt.
|
||||
private static final int ATTEMPT = 1;
|
||||
private static final int MAX_LIST_RESULTS = 50;
|
||||
// IAMRoleArn's pattern, as the model and AWS's validation message spell it.
|
||||
@@ -93,20 +119,41 @@ public class EmrServerlessJobRunService {
|
||||
private final ObjectMapper objectMapper;
|
||||
private final int runDurationSeconds;
|
||||
private final Clock clock;
|
||||
// Null in the immediate runner mode.
|
||||
private final SparkJobRunner sparkRunner;
|
||||
private final Executor runExecutor;
|
||||
// Container runs a thread is running; one not here that has not ended was left by a restart.
|
||||
private final Set<String> executing = new HashSet<>();
|
||||
// Raised by each reset, so a run's thread from before it writes nothing back after the wipe.
|
||||
private long generation;
|
||||
|
||||
@Inject
|
||||
public EmrServerlessJobRunService(StorageFactory storageFactory, EmrServerlessService applications,
|
||||
IamService iamService, RegionResolver regionResolver,
|
||||
RequestContext requestContext, ObjectMapper objectMapper,
|
||||
EmulatorConfig config, Clock clock) {
|
||||
EmulatorConfig config, Clock clock, EmrServerlessSparkRunner sparkRunner) {
|
||||
this(storageFactory.create("emrserverless", "emr-serverless-job-runs.json", new TypeReference<>() {}),
|
||||
applications, iamService, regionResolver, requestContext, objectMapper,
|
||||
config.services().emrserverless().jobRunDurationSeconds(), clock);
|
||||
config.services().emrserverless().jobRunDurationSeconds(), clock,
|
||||
RUNNER_DOCKER.equalsIgnoreCase(config.services().emrserverless().runnerMode()) ? sparkRunner : null,
|
||||
Executors.newCachedThreadPool(Thread.ofPlatform().daemon().name("emr-serverless-run-", 0).factory()));
|
||||
// Before any run of this process starts: a driver an earlier one left belongs to a run read as FAILED.
|
||||
if (this.sparkRunner != null && config.services().emrserverless().reconcileContainersOnStartup()) {
|
||||
sparkRunner.removeLeftoverDrivers();
|
||||
}
|
||||
}
|
||||
|
||||
EmrServerlessJobRunService(AccountAwareStorageBackend<JobRunRecord> store, EmrServerlessService applications,
|
||||
IamService iamService, RegionResolver regionResolver, RequestContext requestContext,
|
||||
ObjectMapper objectMapper, int runDurationSeconds, Clock clock) {
|
||||
this(store, applications, iamService, regionResolver, requestContext, objectMapper, runDurationSeconds, clock,
|
||||
null, Runnable::run);
|
||||
}
|
||||
|
||||
EmrServerlessJobRunService(AccountAwareStorageBackend<JobRunRecord> store, EmrServerlessService applications,
|
||||
IamService iamService, RegionResolver regionResolver, RequestContext requestContext,
|
||||
ObjectMapper objectMapper, int runDurationSeconds, Clock clock,
|
||||
SparkJobRunner sparkRunner, Executor runExecutor) {
|
||||
if (runDurationSeconds < 0) {
|
||||
throw new IllegalArgumentException(
|
||||
"floci.services.emrserverless.job-run-duration-seconds must not be negative: " + runDurationSeconds);
|
||||
@@ -119,6 +166,15 @@ public class EmrServerlessJobRunService {
|
||||
this.objectMapper = objectMapper;
|
||||
this.runDurationSeconds = runDurationSeconds;
|
||||
this.clock = clock;
|
||||
this.sparkRunner = sparkRunner;
|
||||
this.runExecutor = runExecutor;
|
||||
}
|
||||
|
||||
@PreDestroy
|
||||
void shutdown() {
|
||||
if (runExecutor instanceof ExecutorService executor) {
|
||||
executor.shutdownNow();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -142,8 +198,13 @@ public class EmrServerlessJobRunService {
|
||||
if (body.getTags() != null) {
|
||||
EmrServerlessService.validateTags(body.getTags());
|
||||
}
|
||||
JobRunRecord record = applications.admitJobRun(applicationId, app -> checkBatchTimeout(app, body),
|
||||
app -> newRun(app, body, original));
|
||||
JobRunRecord record = applications.admitJobRun(applicationId, app -> checkBatchTimeout(app, body), app -> {
|
||||
JobRunRecord admitted = newRun(app, body, original);
|
||||
if (sparkRunner != null) {
|
||||
launch(admitted, app);
|
||||
}
|
||||
return admitted;
|
||||
});
|
||||
LOG.infov("Started EMR Serverless job run {0} on application {1}", record.getJobRun().getJobRunId(),
|
||||
applicationId);
|
||||
return settle(record).getJobRun();
|
||||
@@ -167,7 +228,8 @@ public class EmrServerlessJobRunService {
|
||||
throw new AwsException("ResourceNotFoundException",
|
||||
"Job run " + jobRunId + " has no attempt " + attempt, 404);
|
||||
}
|
||||
return settle(record).getJobRun();
|
||||
// A copy: a container run's thread may change the stored run while the response is written.
|
||||
return objectMapper.convertValue(settle(record).getJobRun(), JobRun.class);
|
||||
}
|
||||
|
||||
/** A run's attempts, which are its one attempt. */
|
||||
@@ -246,13 +308,25 @@ public class EmrServerlessJobRunService {
|
||||
page.nextToken());
|
||||
}
|
||||
|
||||
/** Cancels a run that has not finished. A finished run is refused, with AWS's wording. */
|
||||
/**
|
||||
* Cancels a run that has not finished. A finished run is refused, with AWS's wording. A container
|
||||
* run is CANCELLING until its driver has stopped.
|
||||
*/
|
||||
public synchronized JobRun cancelJobRun(String applicationId, String jobRunId) {
|
||||
applications.getApplication(applicationId);
|
||||
JobRunRecord record = settle(findRun(applicationId, jobRunId));
|
||||
if (isTerminal(record.getJobRun())) {
|
||||
throw new AwsException("ValidationException", "Job run is not in a cancellable state", 400);
|
||||
}
|
||||
if (record.isContainerRun()) {
|
||||
JobRun run = record.getJobRun();
|
||||
if (!STATE_CANCELLING.equals(run.getState())) {
|
||||
markCancelling(record);
|
||||
runExecutor.execute(() -> sparkRunner.stop(jobRunId));
|
||||
LOG.infov("Cancelling EMR Serverless job run {0} on application {1}", jobRunId, applicationId);
|
||||
}
|
||||
return run;
|
||||
}
|
||||
finish(record, STATE_CANCELLED, clock.instant().truncatedTo(ChronoUnit.MILLIS));
|
||||
LOG.infov("Cancelled EMR Serverless job run {0} on application {1}", jobRunId, applicationId);
|
||||
return record.getJobRun();
|
||||
@@ -322,7 +396,7 @@ public class EmrServerlessJobRunService {
|
||||
run.setUpdatedAt(now);
|
||||
run.setExecutionRole(request.getExecutionRoleArn());
|
||||
run.setExecutionIamPolicy(request.getExecutionIamPolicy());
|
||||
run.setState(STATE_RUNNING);
|
||||
run.setState(sparkRunner != null ? STATE_SCHEDULED : STATE_RUNNING);
|
||||
run.setStateDetails("");
|
||||
run.setReleaseLabel(app.getReleaseLabel());
|
||||
run.setConfigurationOverrides(request.getConfigurationOverrides());
|
||||
@@ -336,17 +410,177 @@ public class EmrServerlessJobRunService {
|
||||
run.setAttempt(ATTEMPT);
|
||||
run.setAttemptCreatedAt(now);
|
||||
run.setAttemptUpdatedAt(now);
|
||||
run.setStartedAt(now);
|
||||
if (sparkRunner == null) {
|
||||
run.setStartedAt(now);
|
||||
}
|
||||
|
||||
JobRunRecord record = new JobRunRecord();
|
||||
record.setJobRun(run);
|
||||
record.setClientToken(request.getClientToken());
|
||||
record.setRequest(original);
|
||||
record.setApplicationType(app.getType());
|
||||
record.setContainerRun(sparkRunner != null);
|
||||
store.put(jobRunId, record);
|
||||
return record;
|
||||
}
|
||||
|
||||
/** Hands a container run to a thread of its own, which runs it to its end. */
|
||||
private void launch(JobRunRecord record, Application app) {
|
||||
JobRun run = record.getJobRun();
|
||||
if (run.getJobDriver().getSparkSubmit() == null) {
|
||||
run.setStateDetails(HIVE_NOT_RUN);
|
||||
finish(record, STATE_FAILED, run.getCreatedAt());
|
||||
return;
|
||||
}
|
||||
AwsArnUtils.Arn arn = AwsArnUtils.parse(run.getArn());
|
||||
// A STREAMING run "expects to run for a long time", so only a BATCH run's timeout ends it.
|
||||
Long minutes = run.getExecutionTimeoutMinutes();
|
||||
Duration timeout = MODE_STREAMING.equals(run.getMode()) || minutes == null || minutes == 0
|
||||
? Duration.ZERO : Duration.ofMinutes(minutes);
|
||||
SparkJobRunner.Job job = new SparkJobRunner.Job(run.getJobRunId(), run.getApplicationId(), arn.accountId(),
|
||||
arn.region(), run.getReleaseLabel(), run.getExecutionRole(), run.getJobDriver().getSparkSubmit(),
|
||||
sparkDefaults(app, run), timeout);
|
||||
executing.add(run.getJobRunId());
|
||||
long launchedIn = generation;
|
||||
runExecutor.execute(() -> execute(job, launchedIn));
|
||||
}
|
||||
|
||||
private void execute(SparkJobRunner.Job job, long launchedIn) {
|
||||
SparkJobRunner.Outcome outcome;
|
||||
try {
|
||||
outcome = sparkRunner.run(job, new RunProgress(job, launchedIn));
|
||||
} catch (RuntimeException e) {
|
||||
LOG.warnv(e, "EMR Serverless job run {0} could not run", job.jobRunId());
|
||||
outcome = SparkJobRunner.Outcome.couldNotStart(e.getMessage());
|
||||
}
|
||||
SparkJobRunner.Outcome ended = outcome;
|
||||
RequestScopes.runAs(job.accountId(), job.region(), () -> ended(job.jobRunId(), ended, launchedIn));
|
||||
}
|
||||
|
||||
/**
|
||||
* A reset has begun: the runs' threads from before it write nothing more, so the storage wipe that
|
||||
* follows leaves none of their runs behind. Its teardown has already stopped their drivers.
|
||||
*/
|
||||
@Override
|
||||
public synchronized void beforeReset() {
|
||||
generation++;
|
||||
executing.clear();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clear() {
|
||||
// The runs live in storage, which the reset has already wiped.
|
||||
}
|
||||
|
||||
// Whether a run's driver may still start: a cancelled run is past it, and so is one a reset removed.
|
||||
private synchronized boolean waitingToStart(String jobRunId, long launchedIn) {
|
||||
return launchedIn == generation && store.get(jobRunId)
|
||||
.map(record -> STATE_SCHEDULED.equals(record.getJobRun().getState())).orElse(false);
|
||||
}
|
||||
|
||||
private synchronized void driverStarted(String jobRunId, long launchedIn) {
|
||||
if (launchedIn != generation) {
|
||||
return;
|
||||
}
|
||||
store.get(jobRunId).filter(record -> STATE_SCHEDULED.equals(record.getJobRun().getState()))
|
||||
.ifPresent(record -> {
|
||||
JobRun run = record.getJobRun();
|
||||
Instant now = clock.instant().truncatedTo(ChronoUnit.MILLIS);
|
||||
run.setState(STATE_RUNNING);
|
||||
run.setStartedAt(now);
|
||||
run.setUpdatedAt(now);
|
||||
run.setAttemptUpdatedAt(now);
|
||||
store.put(jobRunId, record);
|
||||
LOG.infov("EMR Serverless job run {0} is running", jobRunId);
|
||||
});
|
||||
}
|
||||
|
||||
// A run past its timeout is CANCELLING while its driver is stopped: "the job run has timed out".
|
||||
private synchronized void driverTimedOut(String jobRunId, long launchedIn) {
|
||||
if (launchedIn != generation) {
|
||||
return;
|
||||
}
|
||||
store.get(jobRunId).filter(record -> STATE_RUNNING.equals(record.getJobRun().getState()))
|
||||
.ifPresent(this::markCancelling);
|
||||
}
|
||||
|
||||
private void markCancelling(JobRunRecord record) {
|
||||
JobRun run = record.getJobRun();
|
||||
Instant now = clock.instant().truncatedTo(ChronoUnit.MILLIS);
|
||||
run.setState(STATE_CANCELLING);
|
||||
run.setUpdatedAt(now);
|
||||
run.setAttemptUpdatedAt(now);
|
||||
store.put(run.getJobRunId(), record);
|
||||
}
|
||||
|
||||
/**
|
||||
* Ends a container run as its driver did. A cancelled run, or one past its timeout ("the job run
|
||||
* has timed out" is a reason to be CANCELLING), is CANCELLED; a driver stopped otherwise was
|
||||
* stopped by Floci's own shutdown.
|
||||
*/
|
||||
private synchronized void ended(String jobRunId, SparkJobRunner.Outcome outcome, long launchedIn) {
|
||||
if (launchedIn != generation) {
|
||||
return;
|
||||
}
|
||||
executing.remove(jobRunId);
|
||||
JobRunRecord record = store.get(jobRunId).orElse(null);
|
||||
if (record == null || isTerminal(record.getJobRun())) {
|
||||
return;
|
||||
}
|
||||
JobRun run = record.getJobRun();
|
||||
Instant now = clock.instant().truncatedTo(ChronoUnit.MILLIS);
|
||||
if (STATE_CANCELLING.equals(run.getState()) || outcome.timedOut()) {
|
||||
finish(record, STATE_CANCELLED, now);
|
||||
} else if (outcome.exitCode() != null && outcome.exitCode() == 0) {
|
||||
finish(record, STATE_SUCCESS, now);
|
||||
} else {
|
||||
// No source gives AWS's wording for a driver that fails; this is Floci's.
|
||||
run.setStateDetails(outcome.stopped() ? INTERRUPTED
|
||||
: outcome.failure() != null ? "Job run could not start: " + outcome.failure()
|
||||
: "Job run failed: the Spark driver exited with code " + outcome.exitCode() + ".");
|
||||
finish(record, STATE_FAILED, now);
|
||||
}
|
||||
LOG.infov("EMR Serverless job run {0} ended {1}", jobRunId, run.getState());
|
||||
}
|
||||
|
||||
/**
|
||||
* The {@code spark-defaults} properties for a run: the application's runtime configuration, then the
|
||||
* run's own {@code applicationConfiguration}. The two merge by classification, so a run's
|
||||
* {@code spark-defaults} replaces the application's: "Only the properties included in StartJobRun is
|
||||
* considered for this classification." From emr-spark-8.1.0 they merge by property, the run's winning.
|
||||
*/
|
||||
private static Map<String, String> sparkDefaults(Application app, JobRun run) {
|
||||
Map<String, String> properties = new LinkedHashMap<>();
|
||||
if (app.getRuntimeConfiguration() != null) {
|
||||
for (RuntimeConfiguration configuration : app.getRuntimeConfiguration()) {
|
||||
addSparkDefaults(properties, configuration.getMembers());
|
||||
}
|
||||
}
|
||||
Object overrides = run.getConfigurationOverrides() != null
|
||||
? run.getConfigurationOverrides().get("applicationConfiguration") : null;
|
||||
if (overrides instanceof List<?> configurations) {
|
||||
boolean replaces = !releaseAtLeast(SPARK_RUNTIME_RELEASE_LABEL, run.getReleaseLabel(), 8, 1);
|
||||
for (Object configuration : configurations) {
|
||||
if (configuration instanceof Map<?, ?> members
|
||||
&& SPARK_DEFAULTS.equals(members.get("classification"))) {
|
||||
if (replaces) {
|
||||
properties.clear();
|
||||
replaces = false;
|
||||
}
|
||||
addSparkDefaults(properties, members);
|
||||
}
|
||||
}
|
||||
}
|
||||
return properties;
|
||||
}
|
||||
|
||||
private static void addSparkDefaults(Map<String, String> properties, Map<?, ?> configuration) {
|
||||
if (SPARK_DEFAULTS.equals(configuration.get("classification"))
|
||||
&& configuration.get("properties") instanceof Map<?, ?> values) {
|
||||
values.forEach((key, value) -> properties.put(String.valueOf(key), String.valueOf(value)));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Finishes a BATCH run that has been RUNNING for the configured duration, or cancels it at its
|
||||
* execution timeout when that comes first: "If the job run exceeds this duration, Amazon EMR
|
||||
@@ -354,6 +588,14 @@ public class EmrServerlessJobRunService {
|
||||
*/
|
||||
private JobRunRecord settle(JobRunRecord record) {
|
||||
JobRun run = record.getJobRun();
|
||||
if (record.isContainerRun()) {
|
||||
// Its thread ends it; without one, Floci stopped while it ran and nothing is left to.
|
||||
if (!isTerminal(run) && !executing.contains(run.getJobRunId())) {
|
||||
run.setStateDetails(INTERRUPTED);
|
||||
finish(record, STATE_FAILED, clock.instant().truncatedTo(ChronoUnit.MILLIS));
|
||||
}
|
||||
return record;
|
||||
}
|
||||
if (isTerminal(run) || MODE_STREAMING.equals(run.getMode())) {
|
||||
return record;
|
||||
}
|
||||
@@ -379,7 +621,9 @@ public class EmrServerlessJobRunService {
|
||||
run.setEndedAt(endedAt);
|
||||
run.setUpdatedAt(endedAt);
|
||||
run.setAttemptUpdatedAt(endedAt);
|
||||
run.setTotalExecutionDurationSeconds((int) Duration.between(run.getStartedAt(), endedAt).toSeconds());
|
||||
// A container run cancelled or failed before its driver ran never started.
|
||||
run.setTotalExecutionDurationSeconds(run.getStartedAt() != null
|
||||
? (int) Duration.between(run.getStartedAt(), endedAt).toSeconds() : 0);
|
||||
store.put(run.getJobRunId(), record);
|
||||
}
|
||||
|
||||
@@ -500,12 +744,17 @@ public class EmrServerlessJobRunService {
|
||||
}
|
||||
|
||||
private static boolean boundsBatchTimeouts(String releaseLabel) {
|
||||
Matcher matcher = RELEASE_LABEL.matcher(releaseLabel != null ? releaseLabel : "");
|
||||
return releaseAtLeast(RELEASE_LABEL, releaseLabel, 7, 11);
|
||||
}
|
||||
|
||||
// Whether releaseLabel is of the family labelPattern matches, at major.minor or later.
|
||||
private static boolean releaseAtLeast(Pattern labelPattern, String releaseLabel, int major, int minor) {
|
||||
Matcher matcher = labelPattern.matcher(releaseLabel != null ? releaseLabel : "");
|
||||
if (!matcher.matches()) {
|
||||
return false;
|
||||
}
|
||||
int major = Integer.parseInt(matcher.group(1));
|
||||
return major > 7 || (major == 7 && Integer.parseInt(matcher.group(2)) >= 11);
|
||||
int labelMajor = Integer.parseInt(matcher.group(1));
|
||||
return labelMajor > major || (labelMajor == major && Integer.parseInt(matcher.group(2)) >= minor);
|
||||
}
|
||||
|
||||
private static JobRunSummary toSummary(JobRunRecord record) {
|
||||
@@ -550,4 +799,32 @@ public class EmrServerlessJobRunService {
|
||||
summary.setAttempt(ATTEMPT);
|
||||
return summary;
|
||||
}
|
||||
|
||||
/** What the thread running a container run tells the service, and asks it, as that run. */
|
||||
private final class RunProgress implements SparkJobRunner.Progress {
|
||||
|
||||
private final SparkJobRunner.Job job;
|
||||
private final long launchedIn;
|
||||
|
||||
RunProgress(SparkJobRunner.Job job, long launchedIn) {
|
||||
this.job = job;
|
||||
this.launchedIn = launchedIn;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void started() {
|
||||
RequestScopes.runAs(job.accountId(), job.region(), () -> driverStarted(job.jobRunId(), launchedIn));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void timedOut() {
|
||||
RequestScopes.runAs(job.accountId(), job.region(), () -> driverTimedOut(job.jobRunId(), launchedIn));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean cancelled() {
|
||||
return !RequestScopes.callAs(job.accountId(), job.region(),
|
||||
() -> waitingToStart(job.jobRunId(), launchedIn));
|
||||
}
|
||||
}
|
||||
}
|
||||
+352
@@ -0,0 +1,352 @@
|
||||
package io.github.hectorvent.floci.services.emrserverless;
|
||||
|
||||
import com.github.dockerjava.api.command.InspectContainerResponse;
|
||||
import com.github.dockerjava.api.exception.NotFoundException;
|
||||
import com.github.dockerjava.api.exception.NotModifiedException;
|
||||
import com.github.dockerjava.api.model.Container;
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.core.common.ContainerTeardown;
|
||||
import io.github.hectorvent.floci.core.common.docker.ContainerBuilder;
|
||||
import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager;
|
||||
import io.github.hectorvent.floci.core.common.docker.ContainerLogStreamer;
|
||||
import io.github.hectorvent.floci.core.common.docker.ContainerSpec;
|
||||
import io.github.hectorvent.floci.core.common.docker.ContainerStorageHelper;
|
||||
import io.github.hectorvent.floci.core.common.docker.LaunchedContainerAwsEnv;
|
||||
import io.github.hectorvent.floci.services.emrserverless.model.JobDriver;
|
||||
import io.github.hectorvent.floci.services.iam.model.SessionCreds;
|
||||
import io.github.hectorvent.floci.services.lambda.launcher.LambdaExecutionRoleCredentials;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
import java.io.Closeable;
|
||||
import java.time.Clock;
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
/**
|
||||
* Runs a Spark job run's {@code spark-submit} in a container of the release's EMR Serverless image.
|
||||
* The image's own entrypoint hands over to a script EMR Serverless supplies at run time, which the
|
||||
* public image lacks, so the runner starts {@code spark-submit} itself, in local mode: the container
|
||||
* holds the driver and its executors. The container reaches Floci as the other containers Floci
|
||||
* launches do, with the run's execution role as its credentials, and S3A, which takes {@code s3://}
|
||||
* on every release, is pointed at Floci's S3.
|
||||
*/
|
||||
@ApplicationScoped
|
||||
public class EmrServerlessSparkRunner implements SparkJobRunner, ContainerTeardown {
|
||||
|
||||
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 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.
|
||||
private static final String SPARK_RUNTIME_DEFAULT_IMAGE =
|
||||
"public.ecr.aws/emr-serverless/spark4/%s:latest"; // partition-literal: ECR Public's registry host, where AWS publishes the base images; image-template overrides it
|
||||
// "The default behavior for shutdown is to have no grace period": immediate termination.
|
||||
private static final int STOP_TIMEOUT_SECONDS = 0;
|
||||
private static final String SERVICE = "emr-serverless";
|
||||
// The characters a backslash escapes inside double quotes; before any other it is kept.
|
||||
private static final String DOUBLE_QUOTE_ESCAPES = "\"\\$`\n";
|
||||
// How often a running driver is looked at, as Batch's runner looks at its jobs.
|
||||
private static final Duration POLL_INTERVAL = Duration.ofMillis(250);
|
||||
|
||||
// The runs inside run(); a stop for a run not here comes after its driver has gone, and is dropped.
|
||||
private final ConcurrentHashMap<String, Launch> launches = new ConcurrentHashMap<>();
|
||||
|
||||
private final ContainerBuilder containerBuilder;
|
||||
private final ContainerLifecycleManager lifecycleManager;
|
||||
private final ContainerLogStreamer logStreamer;
|
||||
private final LaunchedContainerAwsEnv awsEnv;
|
||||
private final LambdaExecutionRoleCredentials roleCredentials;
|
||||
private final EmulatorConfig config;
|
||||
private final Clock clock;
|
||||
private final Duration pollInterval;
|
||||
|
||||
@Inject
|
||||
public EmrServerlessSparkRunner(ContainerBuilder containerBuilder, ContainerLifecycleManager lifecycleManager,
|
||||
ContainerLogStreamer logStreamer, LaunchedContainerAwsEnv awsEnv,
|
||||
LambdaExecutionRoleCredentials roleCredentials, EmulatorConfig config,
|
||||
Clock clock) {
|
||||
this(containerBuilder, lifecycleManager, logStreamer, awsEnv, roleCredentials, config, clock, POLL_INTERVAL);
|
||||
}
|
||||
|
||||
EmrServerlessSparkRunner(ContainerBuilder containerBuilder, ContainerLifecycleManager lifecycleManager,
|
||||
ContainerLogStreamer logStreamer, LaunchedContainerAwsEnv awsEnv,
|
||||
LambdaExecutionRoleCredentials roleCredentials, EmulatorConfig config, Clock clock,
|
||||
Duration pollInterval) {
|
||||
this.containerBuilder = containerBuilder;
|
||||
this.lifecycleManager = lifecycleManager;
|
||||
this.logStreamer = logStreamer;
|
||||
this.awsEnv = awsEnv;
|
||||
this.roleCredentials = roleCredentials;
|
||||
this.config = config;
|
||||
this.clock = clock;
|
||||
this.pollInterval = pollInterval;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Outcome run(Job job, Progress progress) {
|
||||
Launch launch = new Launch();
|
||||
launches.put(job.jobRunId(), launch);
|
||||
Optional<SessionCreds> credentials = Optional.empty();
|
||||
Closeable logHandle = null;
|
||||
try {
|
||||
// A cancel before the run was in launches had nothing to stop, so it is read here.
|
||||
if (progress.cancelled()) {
|
||||
return Outcome.halted();
|
||||
}
|
||||
credentials = roleCredentials.forRole(job.executionRole(), job.accountId(), job.jobRunId());
|
||||
Map<String, String> labels = new LinkedHashMap<>(ContainerStorageHelper.resourceIdentityLabels(
|
||||
SERVICE, job.jobRunId(), job.accountId(), job.region()));
|
||||
// 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.releaseLabel()))
|
||||
.withName(ContainerStorageHelper.dockerName(config, SERVICE + "-" + job.jobRunId()))
|
||||
.withEntrypoint(List.of(SPARK_SUBMIT))
|
||||
.withCmd(sparkSubmitArguments(job, endpoint))
|
||||
.withEnv(awsEnv.sdkBaselineEnv(job.region(), Optional.empty(), credentials, job.accountId()))
|
||||
.withDockerNetwork(config.services().emrserverless().dockerNetwork())
|
||||
.withHostDockerInternalOnLinux()
|
||||
.withEmbeddedDns()
|
||||
.withLogRotation()
|
||||
.withLabels(labels)
|
||||
.build();
|
||||
String containerId = lifecycleManager.createAndStart(spec).containerId();
|
||||
launch.container.set(containerId);
|
||||
// A stop, a reset or a shutdown while the image was pulled found no container to stop.
|
||||
if (launch.stopRequested || progress.cancelled()) {
|
||||
return Outcome.halted();
|
||||
}
|
||||
logHandle = logStreamer.attachConsoleOnly(containerId, SERVICE + ":" + job.jobRunId());
|
||||
progress.started();
|
||||
Integer exitCode = awaitExit(containerId, job, progress);
|
||||
if (launch.stopRequested) {
|
||||
return Outcome.halted();
|
||||
}
|
||||
return exitCode == null ? Outcome.outOfTime() : Outcome.exitedWith(exitCode);
|
||||
} catch (InterruptedException e) {
|
||||
// Floci is shutting down: its teardown stops the driver.
|
||||
Thread.currentThread().interrupt();
|
||||
return Outcome.halted();
|
||||
} catch (RuntimeException e) {
|
||||
if (launch.stopRequested) {
|
||||
// A reset removed the driver while it was being watched.
|
||||
return Outcome.halted();
|
||||
}
|
||||
LOG.warnv(e, "EMR Serverless job run {0} could not run", job.jobRunId());
|
||||
return Outcome.couldNotStart(e.getMessage());
|
||||
} finally {
|
||||
launches.remove(job.jobRunId(), launch);
|
||||
String unclaimed = launch.container.getAndSet(null);
|
||||
if (unclaimed != null) {
|
||||
lifecycleManager.stopAndRemove(unclaimed, logHandle, STOP_TIMEOUT_SECONDS);
|
||||
} else if (logHandle != null) {
|
||||
// Floci's teardown claimed the driver, but the log stream is still this run's to close.
|
||||
lifecycleManager.closeLogStreamAfterContainerStop(logHandle);
|
||||
}
|
||||
credentials.ifPresent(session -> roleCredentials.unregister(job.accountId(), session.accessKeyId()));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stop(String jobRunId) {
|
||||
Launch launch = launches.get(jobRunId);
|
||||
if (launch == null) {
|
||||
return;
|
||||
}
|
||||
launch.stopRequested = true;
|
||||
String containerId = launch.container.get();
|
||||
if (containerId == null) {
|
||||
// Still starting: run() reads the request once its container is up.
|
||||
return;
|
||||
}
|
||||
// run() sees it stopped at its next look, and removes it.
|
||||
try {
|
||||
lifecycleManager.getDockerClient().stopContainerCmd(containerId).withTimeout(STOP_TIMEOUT_SECONDS).exec();
|
||||
} catch (NotFoundException | NotModifiedException alreadyEnded) {
|
||||
// Gone already (404), or exited and not yet removed (304).
|
||||
LOG.debugv("EMR Serverless job run {0} had ended before it was stopped", jobRunId);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Stops every run inside {@link #run}, so a reset or a shutdown orphans none: a running driver is
|
||||
* stopped and removed now, and one whose image is still being pulled as soon as its container is up.
|
||||
*/
|
||||
@Override
|
||||
public void stopManagedContainers() {
|
||||
for (Launch launch : launches.values()) {
|
||||
launch.stopRequested = true;
|
||||
String containerId = launch.container.getAndSet(null);
|
||||
if (containerId != null) {
|
||||
lifecycleManager.stopAndRemove(containerId, null, STOP_TIMEOUT_SECONDS);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes the drivers an earlier Floci process left running, which no thread follows any more: their
|
||||
* runs are {@code FAILED} once read, so nothing would ever stop them. The job run service calls this
|
||||
* before it hands a run to {@link #run}, so every driver of this deployment it finds is a leftover.
|
||||
*/
|
||||
public void removeLeftoverDrivers() {
|
||||
String owner = ContainerStorageHelper.ownerIdentity(config);
|
||||
ContainerStorageHelper.LabelAliases aliases = ContainerStorageHelper.CONTAINER_LABEL_ALIASES;
|
||||
try {
|
||||
List<Container> containers = aliases.listByLabels(
|
||||
Map.of(ContainerStorageHelper.SERVICE_LABEL, SERVICE, ContainerStorageHelper.OWNER_LABEL, owner),
|
||||
filter -> lifecycleManager.getDockerClient().listContainersCmd()
|
||||
.withShowAll(true)
|
||||
.withLabelFilter(filter)
|
||||
.exec(),
|
||||
Container::getId);
|
||||
for (Container container : containers) {
|
||||
Map<String, String> labels = container.getLabels() != null ? container.getLabels() : Map.of();
|
||||
if (aliases.consistent(container.getId(), labels)
|
||||
&& aliases.matches(labels, ContainerStorageHelper.OWNER_LABEL, owner)) {
|
||||
lifecycleManager.removeIfExists(container.getId());
|
||||
LOG.infov("Removed EMR Serverless driver {0}, left running by an earlier Floci process",
|
||||
container.getId());
|
||||
}
|
||||
}
|
||||
} catch (RuntimeException e) {
|
||||
LOG.warnv("Could not remove the EMR Serverless drivers an earlier Floci process left: {0}",
|
||||
e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
String image(String releaseLabel) {
|
||||
return config.services().emrserverless().imageTemplate()
|
||||
.orElse(releaseLabel.startsWith("emr-spark-") ? SPARK_RUNTIME_DEFAULT_IMAGE : DEFAULT_IMAGE)
|
||||
.formatted(releaseLabel);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@code spark-submit}'s arguments: local mode, S3A on Floci's S3 for {@code s3://} and {@code s3a://}
|
||||
* paths, then the run's {@code spark-defaults} and its own {@code sparkSubmitParameters}, which win
|
||||
* over what comes before them, then the entry point and its arguments. Before release 7.10.0
|
||||
* {@code s3://} is EMRFS, which ignores the endpoint setting, so it is switched to S3A. S3A writes
|
||||
* are committed through commit data the writer keeps in memory, without which EMR's committer finds
|
||||
* none and a write is silently lost.
|
||||
*/
|
||||
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));
|
||||
JobDriver.SparkSubmit sparkSubmit = job.sparkSubmit();
|
||||
arguments.addAll(splitParameters(sparkSubmit.getSparkSubmitParameters()));
|
||||
arguments.add(sparkSubmit.getEntryPoint());
|
||||
if (sparkSubmit.getEntryPointArguments() != null) {
|
||||
arguments.addAll(sparkSubmit.getEntryPointArguments());
|
||||
}
|
||||
return arguments;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@code sparkSubmitParameters} as a POSIX shell would split them: on whitespace, except inside single
|
||||
* or double quotes. Outside quotes a backslash takes the next character as it is; inside double quotes
|
||||
* only a {@code "}, {@code \}, {@code $}, {@code `} or newline, and before any other character it is
|
||||
* kept. A backslash before a newline continues the line, and both go.
|
||||
*/
|
||||
static List<String> splitParameters(String parameters) {
|
||||
List<String> words = new ArrayList<>();
|
||||
if (parameters == null) {
|
||||
return words;
|
||||
}
|
||||
StringBuilder word = new StringBuilder();
|
||||
boolean inWord = false;
|
||||
char quote = 0;
|
||||
for (int i = 0; i < parameters.length(); i++) {
|
||||
char c = parameters.charAt(i);
|
||||
if (quote == '\'') {
|
||||
if (c == '\'') {
|
||||
quote = 0;
|
||||
} else {
|
||||
word.append(c);
|
||||
}
|
||||
} else if (c == '\\' && i + 1 < parameters.length()
|
||||
&& (quote == 0 || DOUBLE_QUOTE_ESCAPES.indexOf(parameters.charAt(i + 1)) >= 0)) {
|
||||
char escaped = parameters.charAt(++i);
|
||||
if (escaped != '\n') {
|
||||
word.append(escaped);
|
||||
inWord = true;
|
||||
}
|
||||
} else if (quote == '"') {
|
||||
if (c == '"') {
|
||||
quote = 0;
|
||||
} else {
|
||||
word.append(c);
|
||||
}
|
||||
} else if (c == '\'' || c == '"') {
|
||||
quote = c;
|
||||
inWord = true;
|
||||
} else if (Character.isWhitespace(c)) {
|
||||
if (inWord) {
|
||||
words.add(word.toString());
|
||||
word.setLength(0);
|
||||
inWord = false;
|
||||
}
|
||||
} else {
|
||||
word.append(c);
|
||||
inWord = true;
|
||||
}
|
||||
}
|
||||
if (inWord) {
|
||||
words.add(word.toString());
|
||||
}
|
||||
return words;
|
||||
}
|
||||
|
||||
private static void conf(List<String> arguments, String key, String value) {
|
||||
arguments.add("--conf");
|
||||
arguments.add(key + "=" + value);
|
||||
}
|
||||
|
||||
/**
|
||||
* The driver's exit code, or null once the run's timeout has passed with the driver still running,
|
||||
* which {@code progress} hears before the driver is stopped. Each look at the container is one short
|
||||
* control-plane call, so a run holds no Docker connection while its driver runs.
|
||||
*/
|
||||
private Integer awaitExit(String containerId, Job job, Progress progress) throws InterruptedException {
|
||||
Instant deadline = job.timeout().isZero() ? null : clock.instant().plus(job.timeout());
|
||||
while (true) {
|
||||
InspectContainerResponse.ContainerState state =
|
||||
lifecycleManager.getDockerClient().inspectContainerCmd(containerId).exec().getState();
|
||||
if (!Boolean.TRUE.equals(state.getRunning())) {
|
||||
Long exitCode = state.getExitCodeLong();
|
||||
return exitCode != null ? exitCode.intValue() : -1;
|
||||
}
|
||||
if (deadline != null && !clock.instant().isBefore(deadline)) {
|
||||
LOG.infov("EMR Serverless job run {0} passed its execution timeout of {1}", job.jobRunId(),
|
||||
job.timeout());
|
||||
progress.timedOut();
|
||||
return null;
|
||||
}
|
||||
Thread.sleep(pollInterval.toMillis());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A run inside {@link #run}: whether it is to stop, and its driver's container once it has one, which
|
||||
* whoever stops the run first claims, run() itself or Floci's teardown.
|
||||
*/
|
||||
private static final class Launch {
|
||||
final AtomicReference<String> container = new AtomicReference<>();
|
||||
volatile boolean stopRequested;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
package io.github.hectorvent.floci.services.emrserverless;
|
||||
|
||||
import io.github.hectorvent.floci.services.emrserverless.model.JobDriver;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Runs a Spark job run to its end, in the {@code docker} runner mode. The job run service holds the
|
||||
* run's state and calls this off the request thread; {@link EmrServerlessSparkRunner} is the Docker
|
||||
* implementation, and a test supplies its own.
|
||||
*/
|
||||
interface SparkJobRunner {
|
||||
|
||||
/**
|
||||
* Runs {@code job} and returns how it ended, telling {@code progress} once its Spark driver runs and
|
||||
* when it passes its timeout. Blocks until then. A run {@code progress} reports as cancelled before
|
||||
* its driver runs never starts it.
|
||||
*/
|
||||
Outcome run(Job job, Progress progress);
|
||||
|
||||
/**
|
||||
* Stops the run's driver, now or as soon as it starts, so {@link #run} returns {@link Outcome#halted};
|
||||
* a run {@link #run} has already returned from is left as it is.
|
||||
*/
|
||||
void stop(String jobRunId);
|
||||
|
||||
/** What a run tells the job run service, and asks it, while it goes. */
|
||||
interface Progress {
|
||||
|
||||
/** The run's Spark driver runs. */
|
||||
void started();
|
||||
|
||||
/** The run has passed its timeout, and its driver is about to be stopped. */
|
||||
void timedOut();
|
||||
|
||||
/** Whether the run was cancelled, or is gone, so that its driver must not start. */
|
||||
boolean cancelled();
|
||||
}
|
||||
|
||||
/**
|
||||
* What a run needs: its identity, the application's release, the run's execution role, its Spark
|
||||
* submission, the {@code spark-defaults} properties that apply to it, and how long it may run
|
||||
* ({@link Duration#ZERO} for no limit).
|
||||
*/
|
||||
record Job(String jobRunId, String applicationId, String accountId, String region, String releaseLabel,
|
||||
String executionRole, JobDriver.SparkSubmit sparkSubmit, Map<String, String> sparkDefaults,
|
||||
Duration timeout) {
|
||||
}
|
||||
|
||||
/**
|
||||
* How a run ended: its driver's exit code, or that it ran out of time, was stopped, or could not
|
||||
* start ({@code failure} says why).
|
||||
*/
|
||||
record Outcome(Integer exitCode, boolean timedOut, boolean stopped, String failure) {
|
||||
|
||||
static Outcome exitedWith(int exitCode) {
|
||||
return new Outcome(exitCode, false, false, null);
|
||||
}
|
||||
|
||||
static Outcome outOfTime() {
|
||||
return new Outcome(null, true, false, null);
|
||||
}
|
||||
|
||||
static Outcome halted() {
|
||||
return new Outcome(null, false, true, null);
|
||||
}
|
||||
|
||||
static Outcome couldNotStart(String failure) {
|
||||
return new Outcome(null, false, false, failure);
|
||||
}
|
||||
}
|
||||
}
|
||||
+5
-1
@@ -6,7 +6,8 @@ import io.quarkus.runtime.annotations.RegisterForReflection;
|
||||
/**
|
||||
* A stored job run: the run as GetJobRun reports it, plus what only Floci needs. The client token
|
||||
* and the request it came with decide whether a repeated StartJobRun is a retry or a conflict; the
|
||||
* application type fills ListJobRuns' {@code type}.
|
||||
* application type fills ListJobRuns' {@code type}. A container run is one the {@code docker} runner
|
||||
* mode runs, whose state its container decides.
|
||||
*/
|
||||
@RegisterForReflection
|
||||
public class JobRunRecord {
|
||||
@@ -14,6 +15,7 @@ public class JobRunRecord {
|
||||
private String clientToken;
|
||||
private JsonNode request;
|
||||
private String applicationType;
|
||||
private boolean containerRun;
|
||||
|
||||
public JobRun getJobRun() { return jobRun; }
|
||||
public void setJobRun(JobRun jobRun) { this.jobRun = jobRun; }
|
||||
@@ -23,4 +25,6 @@ public class JobRunRecord {
|
||||
public void setRequest(JsonNode request) { this.request = request; }
|
||||
public String getApplicationType() { return applicationType; }
|
||||
public void setApplicationType(String applicationType) { this.applicationType = applicationType; }
|
||||
public boolean isContainerRun() { return containerRun; }
|
||||
public void setContainerRun(boolean containerRun) { this.containerRun = containerRun; }
|
||||
}
|
||||
Reference in new issue
Block a user