feat(emr-containers): retry a job run's failed driver under its retry policy (#9218)

A docker job run with a retryPolicyConfiguration failed at its driver's
first error, and every run showed its policy, even one that disables
retries.

- With a maxAttempts above 1, a driver that exits with an error is
  started again, "restarted automatically if it fails", until the run
  has made maxAttempts attempts, after "an exponential retry interval of
  (10 seconds, 20 seconds, 40 seconds ...) which is capped at 6 minutes"
  (EMR on EKS Development Guide, "Using job retry policies").
- Between its attempts the run is SUBMITTED and keeps its running slot,
  each new attempt raises its retryPolicyExecution's currentAttemptCount,
  and CancelJobRun cancels it at once. One Floci stopped in between waits
  out the rest of its backoff once it is read again. A driver that cannot
  start, or that Floci stopped, is not retried.
- With retries enabled, its driver pod's name has a suffix of each
  attempt's own, spark-<job id>-driver-<random-suffix>, so each attempt's
  logs go to a folder and streams of their own, under the run's one Spark
  application id.
- A maxAttempts outside 1 to 2,147,483,647 is refused with "Retry policy
  configuration's parameter value of maxAttempts is invalid", and one of
  1 shows neither the policy nor its execution: "These fields will not
  be visible when retry policy is disabled in the job".
- Floci sends no EMR on EKS events, so the docs note that there is no
  "EMR Job Run New Driver Attempt" event.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-11-22 15:44:21 -04:00
1 parent 82fdb36228
commit 39b601514a
5 files changed
+415 -38

No files matched your search

@@ -52,6 +52,8 @@ class EmrContainersServiceTest {
private final ObjectMapper mapper = new ObjectMapper();
private MutableClock clock;
private EksService eks;
// The retries' backoffs a docker service waits for, which the test ends when it chooses.
private final QueuedDelays delays = new QueuedDelays();
@BeforeEach
void setUp() {
@@ -694,7 +696,7 @@ class EmrContainersServiceTest {
when(iam.resolveCallerArn(eq(null))).thenReturn(Optional.empty());
return new EmrContainersService(stores.get(0), stores.get(1), stores.get(2), stores.get(3), stores.get(4),
mapper, new RegionResolver(REGION, ACCOUNT_ID), eks, iam, null, clock, 0, runner, executor,
"registry.internal/emr-on-eks/spark/%s", "floci-net");
"registry.internal/emr-on-eks/spark/%s", "floci-net", delays);
}
private EmrContainersService dockerService(FakeRunner runner, QueuedExecutor executor) {
@@ -988,6 +990,194 @@ class EmrContainersServiceTest {
"the same Spark application id names both");
}
private String startRetriedJobRun(EmrContainersService service, String clusterId, String retryPolicy) {
return service.startJobRun(clusterId, json("""
{"clientToken": "%s", "executionRoleArn": "%s", "releaseLabel": "%s",
"jobDriver": {"sparkSubmitJobDriver": {"entryPoint": "s3://bucket/pi.py"}},
"retryPolicyConfiguration": %s,
"configurationOverrides": {"monitoringConfiguration": {
"s3MonitoringConfiguration": {"logUri": "s3://logs"}}}}
""".formatted(UUID.randomUUID(), ROLE, RELEASE, retryPolicy))).get("id").textValue();
}
@Test
void dockerRunWhoseDriverFailsIsRetriedAfterAnExponentialBackoffUntilItsAttemptsRunOut() {
FakeRunner runner = new FakeRunner();
runner.outcome = SparkJobRunner.Outcome.exitedWith(1);
QueuedExecutor executor = new QueuedExecutor();
EmrContainersService service = dockerService(runner, executor);
String clusterId = virtualCluster(service, EKS_CLUSTER);
String id = startRetriedJobRun(service, clusterId, "{\"maxAttempts\": 3}");
executor.runAll();
JsonNode waiting = jobRun(service, clusterId, id);
assertEquals("SUBMITTED", state(waiting), "between its attempts");
assertEquals(1, waiting.at("/retryPolicyExecution/currentAttemptCount").intValue());
assertEquals(List.of(Duration.ofSeconds(10)), delays.delays);
delays.runAll();
executor.runAll();
assertEquals(2, jobRun(service, clusterId, id).at("/retryPolicyExecution/currentAttemptCount").intValue());
assertEquals(List.of(Duration.ofSeconds(10), Duration.ofSeconds(20)), delays.delays);
delays.runAll();
executor.runAll();
JsonNode run = jobRun(service, clusterId, id);
assertEquals("FAILED", state(run));
assertEquals("USER_ERROR", run.get("failureReason").textValue());
assertEquals(3, run.at("/retryPolicyExecution/currentAttemptCount").intValue());
assertEquals(3, run.at("/retryPolicyConfiguration/maxAttempts").intValue());
assertEquals(3, runner.jobs.size(), "a driver for each attempt");
assertEquals(2, delays.delays.size(), "no backoff after the last");
List<String> folders = runner.jobs.stream().map(SparkJobRunner.Job::logFolder).toList();
for (String folder : folders) {
assertTrue(folder.matches("s3://logs/" + clusterId + "/jobs/" + id
+ "/containers/spark-[0-9a-f]{32}/spark-" + id + "-driver-[0-9a-z]{5}/"), folder);
}
assertEquals(3, Set.copyOf(folders).size(), "each attempt's driver pod has a suffix of its own");
assertEquals(1, folders.stream().map(folder -> folder.split("/")[7]).distinct().count(),
"a retry restarts the driver of the same Spark application");
}
@Test
void dockerRunStoredWithoutItsApplicationIdKeepsTheOneItsFirstAttemptGets() {
List<AccountAwareStorageBackend<ObjectNode>> stores = stores();
FakeRunner runner = new FakeRunner();
runner.outcome = SparkJobRunner.Outcome.exitedWith(1);
QueuedExecutor executor = new QueuedExecutor();
EmrContainersService service = dockerService(runner, executor, stores);
String clusterId = limitedVirtualCluster(service, "{\"maxConcurrentJobRuns\": 1}");
startJobRun(service, clusterId);
String id = startRetriedJobRun(service, clusterId, "{\"maxAttempts\": 2}");
// Queued as a run stored before runs kept their application's id was.
String arn = jobRun(service, clusterId, id).get("arn").textValue();
ObjectNode stored = stores.get(1).get(arn).orElseThrow().deepCopy();
stored.remove("_sparkApplicationId");
stores.get(1).put(arn, stored);
executor.runAll();
delays.runAll();
executor.runAll();
List<SparkJobRunner.Job> attempts = runner.jobs.stream().filter(job -> job.runId().equals(id)).toList();
assertEquals(2, attempts.size());
assertEquals(attempts.get(0).logFolder().split("/")[7], attempts.get(1).logFolder().split("/")[7]);
assertEquals("FAILED", state(jobRun(service, clusterId, id)));
}
@Test
void dockerRunAnEarlierProcessLeftBetweenItsAttemptsWaitsOutItsBackoffAndRetries() {
List<AccountAwareStorageBackend<ObjectNode>> stores = stores();
FakeRunner failing = new FakeRunner();
failing.outcome = SparkJobRunner.Outcome.exitedWith(1);
QueuedExecutor earlierExecutor = new QueuedExecutor();
EmrContainersService earlier = dockerService(failing, earlierExecutor, stores);
String clusterId = virtualCluster(earlier, EKS_CLUSTER);
String id = startRetriedJobRun(earlier, clusterId, "{\"maxAttempts\": 2}");
earlierExecutor.runAll();
assertEquals(List.of(Duration.ofSeconds(10)), delays.delays);
// The earlier process stops 4 seconds into the backoff, its timer with it.
delays.forget();
clock.advance(Duration.ofSeconds(4));
FakeRunner runner = new FakeRunner();
QueuedExecutor executor = new QueuedExecutor();
EmrContainersService restarted = dockerService(runner, executor, stores);
JsonNode waiting = jobRun(restarted, clusterId, id);
assertEquals("SUBMITTED", state(waiting), "not a run left running");
assertEquals(1, waiting.at("/retryPolicyExecution/currentAttemptCount").intValue());
assertEquals(List.of(Duration.ofSeconds(6)), delays.delays, "the rest of its backoff");
delays.runAll();
executor.runAll();
JsonNode run = jobRun(restarted, clusterId, id);
assertEquals("COMPLETED", state(run));
assertEquals(2, run.at("/retryPolicyExecution/currentAttemptCount").intValue());
assertEquals(1, runner.jobs.size());
assertTrue(runner.leftoversRemoved.isEmpty());
assertEquals(failing.jobs.get(0).logFolder().split("/")[7], runner.jobs.get(0).logFolder().split("/")[7],
"the same Spark application");
}
@Test
void dockerRunWhoseRetriedDriverSucceedsCompletes() {
FakeRunner runner = new FakeRunner();
runner.outcome = SparkJobRunner.Outcome.exitedWith(1);
QueuedExecutor executor = new QueuedExecutor();
EmrContainersService service = dockerService(runner, executor);
String clusterId = virtualCluster(service, EKS_CLUSTER);
String id = startRetriedJobRun(service, clusterId, "{\"maxAttempts\": 5}");
executor.runAll();
runner.outcome = SparkJobRunner.Outcome.exitedWith(0);
delays.runAll();
executor.runAll();
JsonNode run = jobRun(service, clusterId, id);
assertEquals("COMPLETED", state(run));
assertEquals(2, run.at("/retryPolicyExecution/currentAttemptCount").intValue());
}
@Test
void dockerRunCancelledBetweenItsAttemptsIsCancelledAtOnce() {
FakeRunner runner = new FakeRunner();
runner.outcome = SparkJobRunner.Outcome.exitedWith(1);
QueuedExecutor executor = new QueuedExecutor();
EmrContainersService service = dockerService(runner, executor);
String clusterId = limitedVirtualCluster(service, "{\"maxConcurrentJobRuns\": 1}");
String id = startRetriedJobRun(service, clusterId, "{\"maxAttempts\": 2}");
String queued = startJobRun(service, clusterId).get("id").textValue();
executor.runAll();
assertEquals("PENDING", state(jobRun(service, clusterId, queued)), "the retried run keeps its slot");
service.cancelJobRun(clusterId, id);
assertEquals("CANCELLED", state(jobRun(service, clusterId, id)));
assertEquals("SUBMITTED", state(jobRun(service, clusterId, queued)), "the freed slot goes to the queue");
delays.runAll();
executor.runAll();
assertEquals("CANCELLED", state(jobRun(service, clusterId, id)), "its backoff's end starts nothing");
assertEquals(1, runner.jobs.stream().filter(job -> job.runId().equals(id)).count());
}
@Test
void retryPolicyOfOneAttemptIsNotShownAndAnInvalidOneIsRefused() {
EmrContainersService service = service(0);
String clusterId = virtualCluster(service, EKS_CLUSTER);
JsonNode disabled = jobRun(service, clusterId, startRetriedJobRun(service, clusterId, "{\"maxAttempts\": 1}"));
JsonNode enabled = jobRun(service, clusterId, startRetriedJobRun(service, clusterId, "{\"maxAttempts\": 2}"));
assertFalse(disabled.has("retryPolicyConfiguration"), "not visible when retry policy is disabled");
assertFalse(disabled.has("retryPolicyExecution"));
assertEquals(2, enabled.at("/retryPolicyConfiguration/maxAttempts").intValue());
assertEquals(1, enabled.at("/retryPolicyExecution/currentAttemptCount").intValue());
for (String invalid : List.of("0", "-1")) {
AwsException refused = assertThrows(AwsException.class,
() -> startRetriedJobRun(service, clusterId, "{\"maxAttempts\": " + invalid + "}"), invalid);
assertEquals("ValidationException", refused.getErrorCode());
assertEquals("Retry policy configuration's parameter value of maxAttempts is invalid",
refused.getMessage());
}
// Not a 32-bit integer at all, which the model's JavaInteger refuses first.
for (String invalid : List.of("2147483648", "\"2\"")) {
AwsException refused = assertThrows(AwsException.class,
() -> startRetriedJobRun(service, clusterId, "{\"maxAttempts\": " + invalid + "}"), invalid);
assertEquals("ValidationException", refused.getErrorCode());
}
}
@Test
void retryBackoffDoublesFromTenSecondsToSixMinutes() {
assertEquals(Duration.ofSeconds(10), EmrContainersService.retryDelay(1));
assertEquals(Duration.ofSeconds(20), EmrContainersService.retryDelay(2));
assertEquals(Duration.ofSeconds(40), EmrContainersService.retryDelay(3));
assertEquals(Duration.ofSeconds(320), EmrContainersService.retryDelay(6));
assertEquals(Duration.ofMinutes(6), EmrContainersService.retryDelay(7));
assertEquals(Duration.ofMinutes(6), EmrContainersService.retryDelay(Integer.MAX_VALUE));
}
@Test
void monitoringDestinationWithoutItsLogGroupOrUriIsRefusedFromATemplateAsFromARequest() {
EmrContainersService service = service(0);
@@ -1028,6 +1218,30 @@ class EmrContainersServiceTest {
assertEquals("COMPLETED", state(jobRun(service, clusterId, id)));
}
/** Waits each backoff until the test ends them all, on the test's thread. */
private static final class QueuedDelays implements EmrContainersService.Delays {
final List<Duration> delays = new ArrayList<>();
private final Deque<Runnable> tasks = new ArrayDeque<>();
@Override
public void after(Duration delay, Runnable task) {
delays.add(delay);
tasks.add(task);
}
void runAll() {
while (!tasks.isEmpty()) {
tasks.removeFirst().run();
}
}
// The backoffs waited for so far go, as a process's timer goes with it.
void forget() {
delays.clear();
tasks.clear();
}
}
/** Runs what the service hands its run threads only when the test says so, on the test's thread. */
private static final class QueuedExecutor implements Executor {
private final Deque<Runnable> tasks = new ArrayDeque<>();
@@ -19,6 +19,7 @@ class EmrContainersSparkJobsTest {
"arn:aws:emr-containers:eu-west-1:123456789012:/virtualclusters/vc1/jobruns/run1";
private static final String ROLE = "arn:aws:iam::123456789012:role/emr-job";
private static final String APPLICATION = "spark-0123456789abcdef0123456789abcdef";
private static final String DRIVER = "spark-run1-driver";
private final ObjectMapper mapper = new ObjectMapper();
@@ -65,7 +66,7 @@ class EmrContainersSparkJobsTest {
{"classification": "spark-defaults", "properties": {"spark.sql.shuffle.partitions": "4"}}]}
""");
SparkJobRunner.Job job = EmrContainersSparkJobs.job(run, null, "floci-net", APPLICATION);
SparkJobRunner.Job job = EmrContainersSparkJobs.job(run, null, "floci-net", APPLICATION, DRIVER);
assertEquals("emr-containers", job.service());
assertEquals("run1", job.runId());
@@ -101,7 +102,7 @@ class EmrContainersSparkJobsTest {
"logStreamNamePrefix": "nightly"}}}
""");
SparkJobRunner.Job job = EmrContainersSparkJobs.job(run, null, null, APPLICATION);
SparkJobRunner.Job job = EmrContainersSparkJobs.job(run, null, null, APPLICATION, DRIVER);
String driver = "vc1/jobs/run1/containers/" + APPLICATION + "/spark-run1-driver/";
assertEquals("s3://logs/emr/" + driver, job.logFolder());
@@ -118,7 +119,7 @@ class EmrContainersSparkJobsTest {
"cloudWatchMonitoringConfiguration": {"logGroupName": "jobs"}}}
""");
SparkJobRunner.Job job = EmrContainersSparkJobs.job(run, null, null, APPLICATION);
SparkJobRunner.Job job = EmrContainersSparkJobs.job(run, null, null, APPLICATION, DRIVER);
String driver = "vc1/jobs/run1/containers/" + APPLICATION + "/spark-run1-driver/";
assertEquals("s3://logs/" + driver, job.logFolder());
@@ -138,7 +139,7 @@ class EmrContainersSparkJobsTest {
"sparkSqlParameters": "--conf spark.executor.instances=2 -hivevar table=reviews"}}
""", null);
SparkJobRunner.Job job = EmrContainersSparkJobs.job(run, null, null, APPLICATION);
SparkJobRunner.Job job = EmrContainersSparkJobs.job(run, null, null, APPLICATION, DRIVER);
assertEquals(List.of("spark-sql", "--conf", "spark.executor.instances=2", "-hivevar", "table=reviews", "-f",
"s3://bucket/query.sql"), job.command());
@@ -157,7 +158,7 @@ class EmrContainersSparkJobsTest {
"properties": {"spark.kubernetes.container.image": "custom/all:1"}}]}
""");
SparkJobRunner.Job job = EmrContainersSparkJobs.job(run, null, null, APPLICATION);
SparkJobRunner.Job job = EmrContainersSparkJobs.job(run, null, null, APPLICATION, DRIVER);
assertEquals("custom/driver:1", job.customImage(), "the driver's own image wins over the job's");
assertEquals(Map.of("PYSPARK_PYTHON", "./env/bin/python"), job.environment());
@@ -166,6 +167,7 @@ class EmrContainersSparkJobsTest {
{"sparkSubmitJobDriver": {"entryPoint": "local:///app/job.py",
"sparkSubmitParameters": "--conf spark.kubernetes.container.image=custom/all:2"}}
""", null);
assertEquals("custom/all:2", EmrContainersSparkJobs.job(onlyJobImage, null, null, APPLICATION).customImage());
assertEquals("custom/all:2",
EmrContainersSparkJobs.job(onlyJobImage, null, null, APPLICATION, DRIVER).customImage());
}
}