diff --git a/docs/services/emr.md b/docs/services/emr.md index 45f3427561..e08f457e30 100644 --- a/docs/services/emr.md +++ b/docs/services/emr.md @@ -72,6 +72,18 @@ them returns. adding that it "is idempotent but asynchronous". On an earlier release a `RUNNING` step answers `FAILED` with "Step is not in a cancellable state: RUNNING". +The limits the API reference and the Management Guide give steps are checked, in each operation's own error code, in +Floci's words, as no source gives AWS's: a `StepConcurrencyLevel` from 1 to 256 ("You can specify a minimum of 1 step +and a maximum of 256 steps"), which `RunJobFlow` refuses with `ValidationException` and `ModifyCluster` with +`InvalidRequestException`; at most 256 steps `PENDING` or `RUNNING` ("only 256 steps can be RUNNING or PENDING at any +given time", a `CANCEL_PENDING` one counted as running), which `RunJobFlow` and `AddJobFlowSteps` refuse with +`ValidationException`; no step with an `ActionOnFailure` other than `CONTINUE` added while the level is above 1 ("The +step is not submitted and the action fails with a message that the ActionOnFailure setting is not valid"), which +`AddJobFlowSteps` refuses with `ValidationException`, a step naming none taking `TERMINATE_CLUSTER`, as it is +submitted, and so refused too; and `CancelSteps` "Available only +in Amazon EMR versions 4.8.0 and later, excluding version 5.0.0", with at most 256 steps a request, refused otherwise +with `InvalidRequestException`. + In the `docker` runner mode a cancelled `RUNNING` step is `CANCEL_PENDING` while its driver stops, and keeps its place among the running steps until the driver is gone: then it is `CANCELLED`, and the next `PENDING` step starts. That is Floci's reading of the Management Guide's "Canceling a running step does not allow a pending step to start diff --git a/src/main/java/io/floci/aws/services/emr/EmrClusterLifecycle.java b/src/main/java/io/floci/aws/services/emr/EmrClusterLifecycle.java index 4f531196b4..e492e55184 100644 --- a/src/main/java/io/floci/aws/services/emr/EmrClusterLifecycle.java +++ b/src/main/java/io/floci/aws/services/emr/EmrClusterLifecycle.java @@ -37,6 +37,8 @@ final class EmrClusterLifecycle { static final String TERMINATING = "TERMINATING"; static final String TERMINATED = "TERMINATED"; static final String PENDING = "PENDING"; + // The ActionOnFailure of a step that names none. + static final String DEFAULT_ACTION_ON_FAILURE = "TERMINATE_CLUSTER"; static final String COMPLETED = "COMPLETED"; static final String CANCEL_PENDING = "CANCEL_PENDING"; static final String CANCELLED = "CANCELLED"; @@ -127,7 +129,7 @@ final class EmrClusterLifecycle { step.setState(PENDING); step.setCreationDateTime(now); if (step.getActionOnFailure() == null) { - step.setActionOnFailure("TERMINATE_CLUSTER"); + step.setActionOnFailure(DEFAULT_ACTION_ON_FAILURE); } events.queue(events.stepStatusChange(cluster, step, PENDING), region); } diff --git a/src/main/java/io/floci/aws/services/emr/EmrService.java b/src/main/java/io/floci/aws/services/emr/EmrService.java index cefc403e3a..1c7ff024de 100644 --- a/src/main/java/io/floci/aws/services/emr/EmrService.java +++ b/src/main/java/io/floci/aws/services/emr/EmrService.java @@ -72,6 +72,8 @@ import java.util.function.Consumer; import java.util.function.Function; import java.util.function.Predicate; import java.util.function.Supplier; +import java.util.regex.Matcher; +import java.util.regex.Pattern; /** * EMR management-plane business logic. Clusters and steps go through the states of @@ -182,6 +184,15 @@ public class EmrService implements Resettable, StoredResourceArns, ContainerTear private static final Set SCALING_STRATEGIES = Set.of("DEFAULT", "ADVANCED"); private static final long MIN_IDLE_TIMEOUT_SECONDS = 60; private static final long MAX_IDLE_TIMEOUT_SECONDS = 604_800; + // StepConcurrencyLevel: "You can specify a minimum of 1 step and a maximum of 256 steps" (ModifyCluster), "The + // maximum value is 256" (RunJobFlow). And "only 256 steps can be RUNNING or PENDING at any given time" (Management + // Guide), and "A maximum of 256 steps are allowed in each CancelSteps request". No source gives AWS's answer to + // any of them, so their words are Floci's. + private static final int MAX_STEP_CONCURRENCY = 256; + private static final int MAX_ACTIVE_STEPS = 256; + private static final int MAX_CANCELLED_STEPS = 256; + // A release label's major, minor and patch versions. + private static final Pattern RELEASE_VERSION = Pattern.compile("emr-(\\d{1,6})\\.(\\d{1,6})\\.(\\d{1,6})"); private static final int MAX_PORT = 65_535; // The AWS default: block public access enabled in every Region, with port 22 as an exception. private static final String DEFAULT_BLOCK_PUBLIC_ACCESS = @@ -407,6 +418,14 @@ public class EmrService implements Resettable, StoredResourceArns, ContainerTear } // A security configuration is created first and named here; no source gives AWS's answer for one that is // not there, so the code is RunJobFlow's own for its input and the wording that of the other calls. + if (outOfStepConcurrencyRange(cluster.getStepConcurrencyLevel())) { + throw new AwsException("ValidationException", + stepConcurrencyOutOfRange(cluster.getStepConcurrencyLevel()), 400); + } + // Each of its steps is PENDING as it starts. + if (cluster.getSteps().size() > MAX_ACTIVE_STEPS) { + throw new AwsException("ValidationException", tooManyActiveSteps(cluster.getSteps().size()), 400); + } if (cluster.getSecurityConfiguration() != null && secConfigStore.get(cluster.getSecurityConfiguration()).isEmpty()) { throw new AwsException("ValidationException", @@ -527,6 +546,9 @@ public class EmrService implements Resettable, StoredResourceArns, ContainerTear return call(() -> { EmrCluster cluster = requireCluster(id); rejectShutDownCluster(cluster, EmrService::jobFlowShutDown); + if (stepConcurrencyLevel != null && outOfStepConcurrencyRange(stepConcurrencyLevel)) { + throw invalid(stepConcurrencyOutOfRange(stepConcurrencyLevel)); + } if (stepConcurrencyLevel != null) { cluster.setStepConcurrencyLevel(stepConcurrencyLevel); lifecycle.concurrencyChanged(cluster, now(), regionOf(cluster)); @@ -547,6 +569,27 @@ public class EmrService implements Resettable, StoredResourceArns, ContainerTear if (isShutDown(cluster)) { throw jobFlowShutDownValidation(); } + // "If a cluster's StepConcurrencyLevel is greater than 1, do not use AddJobFlowSteps to submit a step with + // this parameter set to CANCEL_AND_WAIT or TERMINATE_CLUSTER. The step is not submitted and the action + // fails with a message that the ActionOnFailure setting is not valid", and the guide: "You cannot add a + // step with an ActionOnFailure other than CONTINUE while the step concurrency level of the cluster is + // greater than 1". A step that names none takes TERMINATE_CLUSTER, as it is submitted, so it is refused + // too. + if (cluster.getStepConcurrencyLevel() > 1) { + for (EmrStep step : steps) { + String action = step.getActionOnFailure() != null ? step.getActionOnFailure() + : EmrClusterLifecycle.DEFAULT_ACTION_ON_FAILURE; + if (!"CONTINUE".equals(action)) { + throw new AwsException("ValidationException", "The ActionOnFailure setting " + action + + " is not valid for a step added to a cluster whose StepConcurrencyLevel is greater" + + " than 1: only CONTINUE is.", 400); + } + } + } + long active = cluster.getSteps().stream().filter(EmrService::activeStep).count() + steps.size(); + if (active > MAX_ACTIVE_STEPS) { + throw new AwsException("ValidationException", tooManyActiveSteps(active), 400); + } steps.forEach(step -> step.setId("s-" + randomId(13))); cluster.getSteps().addAll(steps); lifecycle.submit(cluster, steps, now(), regionOf(cluster)); @@ -588,6 +631,15 @@ public class EmrService implements Resettable, StoredResourceArns, ContainerTear public List cancelSteps(String clusterId, List stepIds) { return call(() -> { EmrCluster cluster = requireCluster(clusterId); + // CancelSteps is "Available only in Amazon EMR versions 4.8.0 and later, excluding version 5.0.0". + if (!cancelsSteps(cluster.getReleaseLabel())) { + throw invalid("CancelSteps is available only in Amazon EMR versions 4.8.0 and later, excluding" + + " version 5.0.0, and cluster " + clusterId + " runs " + cluster.getReleaseLabel() + "."); + } + if (stepIds.size() > MAX_CANCELLED_STEPS) { + throw invalid("A CancelSteps request can cancel at most " + MAX_CANCELLED_STEPS + " steps, and this" + + " one names " + stepIds.size() + "."); + } List result = new ArrayList<>(); for (String stepId : stepIds) { EmrStep step = stepOf(cluster, stepId).orElse(null); @@ -1186,6 +1238,40 @@ public class EmrService implements Resettable, StoredResourceArns, ContainerTear return new AwsException("InvalidRequestException", message, 400); } + private static boolean outOfStepConcurrencyRange(int level) { + return level < 1 || level > MAX_STEP_CONCURRENCY; + } + + private static String stepConcurrencyOutOfRange(int level) { + return "The StepConcurrencyLevel " + level + " is not valid: it must be from 1 to " + MAX_STEP_CONCURRENCY + + "."; + } + + private static String tooManyActiveSteps(long active) { + return "Only " + MAX_ACTIVE_STEPS + " steps can be PENDING or RUNNING at any given time, and this would make " + + active + "."; + } + + // A step PENDING or RUNNING, one whose cancel is pending among them, as it still runs. + private static boolean activeStep(EmrStep step) { + return EmrClusterLifecycle.PENDING.equals(step.getState()) + || EmrClusterLifecycle.RUNNING.equals(step.getState()) + || EmrClusterLifecycle.CANCEL_PENDING.equals(step.getState()); + } + + // Whether a cluster of releaseLabel takes CancelSteps: from 4.8.0 on but 5.0.0; a label not of the emr-x.y.z form + // is taken as current. + static boolean cancelsSteps(String releaseLabel) { + Matcher version = RELEASE_VERSION.matcher(releaseLabel == null ? "" : releaseLabel); + if (!version.matches()) { + return true; + } + int major = Integer.parseInt(version.group(1)); + int minor = Integer.parseInt(version.group(2)); + int patch = Integer.parseInt(version.group(3)); + return (major > 4 || major == 4 && minor >= 8) && !(major == 5 && minor == 0 && patch == 0); + } + // Why a cluster gets no instance group or fleet of the other kind: "The configuration option you choose applies to // all nodes, it applies for the lifetime of the cluster, and instance fleets and instance groups cannot coexist in // a cluster." diff --git a/src/test/java/io/floci/aws/services/emr/EmrServiceTest.java b/src/test/java/io/floci/aws/services/emr/EmrServiceTest.java index 5ee99ca3c4..081f08044c 100644 --- a/src/test/java/io/floci/aws/services/emr/EmrServiceTest.java +++ b/src/test/java/io/floci/aws/services/emr/EmrServiceTest.java @@ -16,6 +16,8 @@ import org.junit.jupiter.api.function.Executable; import java.time.Duration; import java.time.Instant; +import java.util.ArrayList; +import java.util.Collections; import java.util.List; import java.util.Map; import java.util.concurrent.CountDownLatch; @@ -159,6 +161,105 @@ class EmrServiceTest { assertEquals("WAITING", service.describeCluster(id).getState()); } + @Test + void aStepConcurrencyLevelBelowOneOrAbove256IsRefused() { + EmrService service = new EmrServiceFixture(0, 60, 0).service; + EmrCluster wide = cluster("wide", true); + wide.setStepConcurrencyLevel(257); + + // "You can specify a minimum of 1 step and a maximum of 256 steps", in each operation's own code. + AwsException created = assertThrows(AwsException.class, () -> service.runJobFlow(wide, REGION)); + assertEquals("ValidationException", created.getErrorCode()); + assertEquals("The StepConcurrencyLevel 257 is not valid: it must be from 1 to 256.", created.getMessage()); + String id = service.runJobFlow(cluster("narrow", true), REGION).getId(); + AwsException modified = assertThrows(AwsException.class, () -> service.modifyCluster(id, 0)); + assertEquals("InvalidRequestException", modified.getErrorCode()); + assertEquals("The StepConcurrencyLevel 0 is not valid: it must be from 1 to 256.", modified.getMessage()); + assertEquals(1, service.describeCluster(id).getStepConcurrencyLevel()); + assertEquals(256, service.modifyCluster(id, 256)); + } + + @Test + void onlyContinueMayBeAddedToAClusterThatRunsStepsInParallel() { + EmrService service = new EmrServiceFixture(0, 60, 0).service; + EmrCluster parallel = cluster("parallel", true); + parallel.setStepConcurrencyLevel(2); + String id = service.runJobFlow(parallel, REGION).getId(); + EmrStep terminating = step("terminating"); + terminating.setActionOnFailure("TERMINATE_CLUSTER"); + + // "The step is not submitted and the action fails with a message that the ActionOnFailure setting is not + // valid." + AwsException refused = assertThrows(AwsException.class, + () -> service.addJobFlowSteps(id, List.of(step("fine"), terminating))); + assertEquals("ValidationException", refused.getErrorCode()); + assertEquals("The ActionOnFailure setting TERMINATE_CLUSTER is not valid for a step added to a cluster whose" + + " StepConcurrencyLevel is greater than 1: only CONTINUE is.", refused.getMessage()); + assertEquals(List.of(), service.listSteps(id, null, null)); + // One that names none takes TERMINATE_CLUSTER, so it is refused too. + EmrStep unnamed = step("unnamed"); + unnamed.setActionOnFailure(null); + assertThrows(AwsException.class, () -> service.addJobFlowSteps(id, List.of(unnamed))); + assertEquals(List.of(), service.listSteps(id, null, null)); + // At a level of 1 both are fine. + service.modifyCluster(id, 1); + assertEquals(2, service.addJobFlowSteps(id, List.of(terminating, unnamed)).size()); + assertEquals("TERMINATE_CLUSTER", service.describeStep(id, unnamed.getId()).getActionOnFailure()); + } + + @Test + void onlyTwoHundredFiftySixStepsMayBePendingOrRunningAtOnce() { + EmrService service = new EmrServiceFixture(0, 3600, 0).service; + List many = new ArrayList<>(); + for (int n = 0; n < 257; n++) { + many.add(step("step-" + n)); + } + EmrCluster crowded = cluster("crowded", true); + crowded.getSteps().addAll(many); + + AwsException created = assertThrows(AwsException.class, () -> service.runJobFlow(crowded, REGION)); + assertEquals("ValidationException", created.getErrorCode()); + assertEquals("Only 256 steps can be PENDING or RUNNING at any given time, and this would make 257.", + created.getMessage()); + EmrCluster full = cluster("full", true); + full.getSteps().addAll(many.subList(0, 256)); + String id = service.runJobFlow(full, REGION).getId(); + AwsException added = assertThrows(AwsException.class, + () -> service.addJobFlowSteps(id, List.of(step("one-more")))); + assertEquals("Only 256 steps can be PENDING or RUNNING at any given time, and this would make 257.", + added.getMessage()); + // One ended makes room again. + service.cancelSteps(id, List.of(service.listSteps(id, List.of("PENDING"), null).get(0).getId())); + assertEquals(1, service.addJobFlowSteps(id, List.of(step("one-more"))).size()); + } + + @Test + void cancelStepsTakesReleasesFrom480ButNot500AndAtMost256Steps() { + EmrService service = new EmrServiceFixture(0, 60, 0).service; + EmrCluster old = cluster("old", true); + old.setReleaseLabel("emr-4.7.2"); + String oldId = service.runJobFlow(old, REGION).getId(); + EmrCluster first = cluster("first", true); + first.setReleaseLabel("emr-5.0.0"); + String firstId = service.runJobFlow(first, REGION).getId(); + EmrCluster patched = cluster("patched", true); + patched.setReleaseLabel("emr-5.0.3"); + String patchedId = service.runJobFlow(patched, REGION).getId(); + + // "Available only in Amazon EMR versions 4.8.0 and later, excluding version 5.0.0." + AwsException older = assertThrows(AwsException.class, () -> service.cancelSteps(oldId, List.of("s-1"))); + assertEquals("InvalidRequestException", older.getErrorCode()); + assertEquals("CancelSteps is available only in Amazon EMR versions 4.8.0 and later, excluding version 5.0.0," + + " and cluster " + oldId + " runs emr-4.7.2.", older.getMessage()); + assertThrows(AwsException.class, () -> service.cancelSteps(firstId, List.of("s-1"))); + assertEquals(1, service.cancelSteps(patchedId, List.of("s-1")).size()); + // "A maximum of 256 steps are allowed in each CancelSteps request." + List ids = Collections.nCopies(257, "s-1"); + AwsException tooMany = assertThrows(AwsException.class, () -> service.cancelSteps(patchedId, ids)); + assertEquals("A CancelSteps request can cancel at most 256 steps, and this one names 257.", + tooMany.getMessage()); + } + @Test void stepsAddedWhileAClusterStartsRunOnceItIsReady() { EmrServiceFixture fixture = new EmrServiceFixture(10, 60, 0);