From f665d5f5e7fc91b4020fcf702c2bd99cef2d7479 Mon Sep 17 00:00:00 2001 From: Priamo Germosen <6233424+pgermosen@users.noreply.github.com> Date: Sat, 14 Nov 2026 12:44:35 -0400 Subject: [PATCH] feat(emr-serverless): update a started application's capacity and images (#8316) UpdateApplication now takes a STARTED application's maximumCapacity and custom images (imageConfiguration, workerTypeSpecifications), as the User Guide's "Updating Running Applications" describes: "You can update key application configurations such as maximum capacity, and custom image settings - without stopping and restarting the application." Every other setting still needs a CREATED or STOPPED application, as the API reference's "An application has to be in a stopped or created state in order to be updated" says, and an image change on an application with initial capacity is refused: "Custom image updates on running applications are not supported on applications with pre-initialized capacity configured." A run already going keeps the image and maximum capacity it was launched with, and a new run, or a new attempt of one, takes the application's current ones, as each run already reads them when it is launched. --- docs/services/emr-serverless.md | 13 +++ .../emrserverless/EmrServerlessService.java | 42 ++++++- .../EmrServerlessIntegrationTest.java | 66 +++++++++++ .../EmrServerlessJobRunServiceTest.java | 60 ++++++++++ .../EmrServerlessServiceTest.java | 108 ++++++++++++++++++ 5 files changed, 286 insertions(+), 3 deletions(-) diff --git a/docs/services/emr-serverless.md b/docs/services/emr-serverless.md index bb0f50d0ab..611d6ebe27 100644 --- a/docs/services/emr-serverless.md +++ b/docs/services/emr-serverless.md @@ -58,6 +58,19 @@ another. `interactiveConfiguration`, `identityCenterConfiguration` and `jobLevelCostAllocationConfiguration` among them, and `GetApplication` returns each as it was written. +`UpdateApplication` changes any setting of a `CREATED` or `STOPPED` application, and of a `STARTED` one its +`maximumCapacity` and its custom images, `imageConfiguration` and `workerTypeSpecifications`, as the user guide's +"Updating Running Applications" describes. For every other setting the API reference's "An application has to be in +a stopped or created state in order to be updated" holds: a `STARTED` application's update naming one, even beside +those or with its current value, is refused with `ValidationException`, "Application must be in a stopped or created +state in order to be updated.", and so is an image change on an application with `initialCapacity` ("Custom image +updates on running applications are not supported on applications with pre-initialized capacity configured"). A run +already going keeps the image and the maximum capacity it was launched with, and a new run, or a new attempt of a +run, takes the application's current ones: "New workloads submitted after the update automatically will use the new +image, while existing workloads continue with the original image". On a release up to 7.13 whose +`applicationLevelDigestResolution` is left true, AWS keeps the image it resolved at the application's start until the +application restarts; Floci resolves each run's image (see the custom images limitation) and runs the new one. + `DeleteApplication` leaves the application in `TERMINATED` state. `GetApplication` continues to report that state so lifecycle waiters can observe it, while an unfiltered `ListApplications` omits the terminated application. A terminated application cannot be started or stopped again. diff --git a/src/main/java/io/floci/aws/services/emrserverless/EmrServerlessService.java b/src/main/java/io/floci/aws/services/emrserverless/EmrServerlessService.java index d23749d7b4..3f7cc5a7ee 100644 --- a/src/main/java/io/floci/aws/services/emrserverless/EmrServerlessService.java +++ b/src/main/java/io/floci/aws/services/emrserverless/EmrServerlessService.java @@ -44,6 +44,20 @@ public class EmrServerlessService { // The EMR Serverless user guide's tagging limitations: at most 50 user-created tags per resource // (the model's TagMap max of 200 only bounds a single request), and no "aws:" prefix, in any case, // on a key or a value. TagKey and TagValue share one character set in the API model. + private static final String NOT_STOPPED_FOR_UPDATE = + "Application must be in a stopped or created state in order to be updated."; + // The settings an update may change only on a CREATED or STOPPED application: all but its maximum capacity and + // its custom images, which a STARTED one takes too (checkStartedUpdate). + private static final List> STOPPED_ONLY_SETTINGS = List.of( + UpdateApplicationRequest::getReleaseLabel, UpdateApplicationRequest::getInitialCapacity, + UpdateApplicationRequest::getAutoStartConfiguration, UpdateApplicationRequest::getAutoStopConfiguration, + UpdateApplicationRequest::getNetworkConfiguration, UpdateApplicationRequest::getArchitecture, + UpdateApplicationRequest::getMonitoringConfiguration, UpdateApplicationRequest::getRuntimeConfiguration, + UpdateApplicationRequest::getSchedulerConfiguration, + UpdateApplicationRequest::getDiskEncryptionConfiguration, + UpdateApplicationRequest::getInteractiveConfiguration, + UpdateApplicationRequest::getIdentityCenterConfiguration, + UpdateApplicationRequest::getJobLevelCostAllocationConfiguration); private static final int MAX_TAGS = 50; private static final String RESERVED_PREFIX = "aws:"; private static final Pattern TAG_TEXT = Pattern.compile("[A-Za-z0-9 /_.:=+@-]*"); @@ -176,10 +190,12 @@ public class EmrServerlessService { public synchronized Application updateApplication(String applicationId, UpdateApplicationRequest request) { validateImages(request.getImageConfiguration(), request.getWorkerTypeSpecifications(), false); Application app = getApplication(applicationId); - + String state = app.getState(); - if (!"CREATED".equals(state) && !"STOPPED".equals(state)) { - throw new AwsException("ValidationException", "Application must be in a stopped or created state in order to be updated.", 400); + if ("STARTED".equals(state)) { + checkStartedUpdate(app, request); + } else if (!"CREATED".equals(state) && !"STOPPED".equals(state)) { + throw new AwsException("ValidationException", NOT_STOPPED_FOR_UPDATE, 400); } if (request.getReleaseLabel() != null) { @@ -241,6 +257,26 @@ public class EmrServerlessService { return app; } + /** + * What an update may change on a STARTED application, from AWS's "Updating Running Applications": its maximum + * capacity, "for a STARTED application to modify the scaling boundary", and its custom images, "without stopping + * it", though "Custom image updates on running applications are not supported on applications with + * pre-initialized capacity configured". Any other setting still needs the application "in a stopped or created + * state". A run already going keeps the image and maximum capacity it was launched with, as "existing workloads + * continue with the original image" and "The new settings only apply to workers on new workloads"; a new run, or a + * new attempt of one, takes the application's current ones. + */ + private static void checkStartedUpdate(Application app, UpdateApplicationRequest request) { + if (STOPPED_ONLY_SETTINGS.stream().anyMatch(setting -> setting.apply(request) != null)) { + throw new AwsException("ValidationException", NOT_STOPPED_FOR_UPDATE, 400); + } + boolean images = request.getImageConfiguration() != null || request.getWorkerTypeSpecifications() != null; + if (images && app.getInitialCapacity() != null && !app.getInitialCapacity().isEmpty()) { + throw new AwsException("ValidationException", "Custom image updates on running applications are not " + + "supported on applications with pre-initialized capacity configured.", 400); + } + } + public synchronized void deleteApplication(String applicationId) { Application app = getApplication(applicationId); String state = app.getState(); diff --git a/src/test/java/io/floci/aws/services/emrserverless/EmrServerlessIntegrationTest.java b/src/test/java/io/floci/aws/services/emrserverless/EmrServerlessIntegrationTest.java index 17813951c8..0b2a2b32d0 100644 --- a/src/test/java/io/floci/aws/services/emrserverless/EmrServerlessIntegrationTest.java +++ b/src/test/java/io/floci/aws/services/emrserverless/EmrServerlessIntegrationTest.java @@ -456,4 +456,70 @@ public class EmrServerlessIntegrationTest { .body("message", equalTo("Image 000000000000.dkr.ecr.eu-west-1.amazonaws.com/spark/custom:7.5.0 is in " + "an Amazon ECR repository in eu-west-1; it must be in the application's Region, us-east-1.")); } + + @Test + @Order(13) + void updateApplicationChangesAStartedApplicationsMaximumCapacityAndImageButNotItsOtherSettings() { + String started = givenReq() + .body(""" + { + "releaseLabel": "emr-7.5.0", + "type": "SPARK", + "clientToken": "%s", + "imageConfiguration": { + "imageUri": "000000000000.dkr.ecr.us-east-1.amazonaws.com/spark/custom:7.5.0" + }, + "maximumCapacity": {"cpu": "400 vCPU", "memory": "1024 GB"} + } + """.formatted(UUID.randomUUID())) + .when() + .post("/applications") + .then() + .statusCode(200) + .extract().path("applicationId"); + givenReq().when().post("/applications/" + started + "/start").then().statusCode(200); + + givenReq() + .body(""" + { + "clientToken": "%s", + "imageConfiguration": { + "imageUri": "000000000000.dkr.ecr.us-east-1.amazonaws.com/spark/custom:7.5.1" + }, + "maximumCapacity": {"cpu": "100 vCPU", "memory": "512 GB"} + } + """.formatted(UUID.randomUUID())) + .when() + .patch("/applications/" + started) + .then() + .statusCode(200); + givenReq() + .when() + .get("/applications/" + started) + .then() + .statusCode(200) + .body("application.state", equalTo("STARTED")) + .body("application.maximumCapacity.cpu", equalTo("100 vCPU")) + .body("application.imageConfiguration.imageUri", + equalTo("000000000000.dkr.ecr.us-east-1.amazonaws.com/spark/custom:7.5.1")); + + givenReq() + .body(""" + {"clientToken": "%s", "releaseLabel": "emr-7.10.0", "maximumCapacity": {"cpu": "200 vCPU"}} + """.formatted(UUID.randomUUID())) + .when() + .patch("/applications/" + started) + .then() + .statusCode(400) + .body("__type", equalTo("ValidationException")) + .body("message", equalTo("Application must be in a stopped or created state in order to be updated.")); + + givenReq() + .when() + .get("/applications/" + started) + .then() + .statusCode(200) + .body("application.releaseLabel", equalTo("emr-7.5.0")) + .body("application.maximumCapacity.cpu", equalTo("100 vCPU")); + } } diff --git a/src/test/java/io/floci/aws/services/emrserverless/EmrServerlessJobRunServiceTest.java b/src/test/java/io/floci/aws/services/emrserverless/EmrServerlessJobRunServiceTest.java index 475e21a118..95df35c3ca 100644 --- a/src/test/java/io/floci/aws/services/emrserverless/EmrServerlessJobRunServiceTest.java +++ b/src/test/java/io/floci/aws/services/emrserverless/EmrServerlessJobRunServiceTest.java @@ -18,6 +18,7 @@ import io.floci.aws.services.emrserverless.model.MaximumCapacity; import io.floci.aws.services.emrserverless.model.MonitoringConfiguration; import io.floci.aws.services.emrserverless.model.RuntimeConfiguration; import io.floci.aws.services.emrserverless.model.StartJobRunRequest; +import io.floci.aws.services.emrserverless.model.UpdateApplicationRequest; import io.floci.aws.services.iam.IamService; import io.floci.aws.testing.InMemoryStorageFactory; import io.floci.aws.testing.MutableClock; @@ -581,6 +582,65 @@ class EmrServerlessJobRunServiceTest { assertNull(runner.jobs.get(3).executors()); } + @Test + void aRunGoingKeepsWhatItWasLaunchedWithAndTheNextTakesItsStartedApplicationsNewImageAndMaximumCapacity() { + FakeRunner runner = new FakeRunner(); + EmrServerlessJobRunService service = dockerService(runner, 2); + String firstImage = "123456789012.dkr.ecr.us-east-1.amazonaws.com/spark/custom:7.5.0"; + String nextImage = "123456789012.dkr.ecr.us-east-1.amazonaws.com/spark/custom:7.5.1"; + CreateApplicationRequest create = applicationRequest("updated-while-started", "emr-7.5.0", "SPARK"); + create.setImageConfiguration(EmrServerlessServiceTest.image(firstImage)); + create.setMaximumCapacity(EmrServerlessServiceTest.capacity("400 vCPU")); + Application app = applications.createApplication(create); + UpdateApplicationRequest update = new UpdateApplicationRequest(); + update.setImageConfiguration(EmrServerlessServiceTest.image(nextImage)); + update.setMaximumCapacity(EmrServerlessServiceTest.capacity("100 vCPU")); + List statesUpdated = new ArrayList<>(); + runner.whileRunning = () -> statesUpdated.add( + applications.updateApplication(app.getApplicationId(), update).getState()); + + service.startJobRun(app.getApplicationId(), request(null, null)); + runNext(); + runner.whileRunning = () -> { }; + service.startJobRun(app.getApplicationId(), request(null, null)); + runNext(); + + // The first run started the application, which took the update while that run went on. + assertEquals(List.of("STARTED"), statesUpdated); + assertEquals(firstImage, runner.jobs.get(0).customImage()); + assertEquals(new SparkJobRunner.Executors(2, firstImage, 400, 0, 0, app.getArn()), + runner.jobs.get(0).executors()); + assertEquals(nextImage, runner.jobs.get(1).customImage()); + assertEquals(new SparkJobRunner.Executors(2, nextImage, 100, 0, 0, app.getArn()), + runner.jobs.get(1).executors()); + } + + @Test + void aRunsNextAttemptTakesTheImageItsStartedApplicationWasUpdatedTo() { + FakeRunner runner = new FakeRunner(); + runner.outcome = SparkJobRunner.Outcome.exitedWith(1); + EmrServerlessJobRunService service = dockerService(runner); + String firstImage = "123456789012.dkr.ecr.us-east-1.amazonaws.com/spark/custom:7.5.0"; + String nextImage = "123456789012.dkr.ecr.us-east-1.amazonaws.com/spark/custom:7.5.1"; + CreateApplicationRequest create = applicationRequest("retried-after-update", "emr-7.5.0", "SPARK"); + create.setImageConfiguration(EmrServerlessServiceTest.image(firstImage)); + String id = applications.createApplication(create).getApplicationId(); + UpdateApplicationRequest update = new UpdateApplicationRequest(); + update.setImageConfiguration(EmrServerlessServiceTest.image(nextImage)); + runner.whileRunning = () -> applications.updateApplication(id, update); + StartJobRunRequest resilient = request(null, null); + resilient.setRetryPolicy(Map.of("maxAttempts", 2)); + + service.startJobRun(id, resilient); + runNext(); + runner.whileRunning = () -> { }; + runNext(); + + // "New job attempts will pick new images". + assertEquals(firstImage, runner.jobs.get(0).customImage()); + assertEquals(nextImage, runner.jobs.get(1).customImage()); + } + @Test void withoutExecutorContainersARunsExecutorsRunInItsDriversContainer() { FakeRunner runner = new FakeRunner(); diff --git a/src/test/java/io/floci/aws/services/emrserverless/EmrServerlessServiceTest.java b/src/test/java/io/floci/aws/services/emrserverless/EmrServerlessServiceTest.java index 926feb1581..174eeddcde 100644 --- a/src/test/java/io/floci/aws/services/emrserverless/EmrServerlessServiceTest.java +++ b/src/test/java/io/floci/aws/services/emrserverless/EmrServerlessServiceTest.java @@ -8,6 +8,8 @@ import io.floci.aws.core.storage.PersistentStorage; import io.floci.aws.services.emrserverless.model.Application; import io.floci.aws.services.emrserverless.model.CreateApplicationRequest; import io.floci.aws.services.emrserverless.model.ImageConfiguration; +import io.floci.aws.services.emrserverless.model.InitialCapacityConfig; +import io.floci.aws.services.emrserverless.model.MaximumCapacity; import io.floci.aws.services.emrserverless.model.UpdateApplicationRequest; import io.floci.aws.services.emrserverless.model.WorkerTypeSpecification; import io.floci.aws.testing.InMemoryStorageFactory; @@ -16,12 +18,15 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.io.TempDir; import java.io.IOException; +import java.lang.reflect.Method; import java.nio.file.Files; import java.nio.file.Path; import java.time.Duration; import java.time.Instant; +import java.util.ArrayList; import java.util.List; import java.util.Map; +import java.util.Set; import java.util.UUID; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -36,6 +41,10 @@ class EmrServerlessServiceTest { private static final String ACCOUNT_ID = "000000000000"; private static final TypeReference> APPLICATIONS = new TypeReference<>() {}; private static final String CUSTOM_IMAGE = "123456789012.dkr.ecr.us-east-1.amazonaws.com/spark/custom:7.5.0"; + private static final String OTHER_IMAGE = "123456789012.dkr.ecr.us-east-1.amazonaws.com/spark/custom:7.5.1"; + // The settings a STARTED application's update may name, besides an idempotency token and the path's application. + private static final Set STARTED_UPDATE_MEMBERS = Set.of("setApplicationId", "setClientToken", + "setMaximumCapacity", "setImageConfiguration", "setWorkerTypeSpecifications"); @TempDir Path tempDir; @@ -177,6 +186,105 @@ class EmrServerlessServiceTest { assertEquals(CUSTOM_IMAGE, unchanged.getImageConfiguration().getImageUri()); } + @Test + void aStartedApplicationTakesANewMaximumCapacityAndCustomImagesButNoOtherSetting() throws Exception { + EmrServerlessService service = service(); + CreateApplicationRequest create = createRequest(); + create.setImageConfiguration(image(CUSTOM_IMAGE)); + create.setMaximumCapacity(capacity("400 vCPU")); + // An empty initial capacity pre-initializes nothing. + create.setInitialCapacity(Map.of()); + String applicationId = service.createApplication(create).getApplicationId(); + service.startApplication(applicationId); + UpdateApplicationRequest running = new UpdateApplicationRequest(); + running.setClientToken(UUID.randomUUID().toString()); + running.setMaximumCapacity(capacity("100 vCPU")); + running.setImageConfiguration(image(OTHER_IMAGE)); + running.setWorkerTypeSpecifications(Map.of("Driver", workerType(OTHER_IMAGE))); + + Application updated = service.updateApplication(applicationId, running); + + assertEquals("STARTED", updated.getState()); + assertEquals("100 vCPU", updated.getMaximumCapacity().getCpu()); + assertEquals(OTHER_IMAGE, updated.getImageConfiguration().getImageUri()); + assertEquals(OTHER_IMAGE, + updated.getWorkerTypeSpecifications().get("Driver").getImageConfiguration().getImageUri()); + // Every other setting waits for the application to stop, even beside a new maximum capacity. + List refused = new ArrayList<>(); + for (Method setter : UpdateApplicationRequest.class.getMethods()) { + if (!setter.getName().startsWith("set") || STARTED_UPDATE_MEMBERS.contains(setter.getName())) { + continue; + } + UpdateApplicationRequest other = new UpdateApplicationRequest(); + other.setMaximumCapacity(capacity("200 vCPU")); + setter.invoke(other, someValue(setter.getParameterTypes()[0])); + AwsException refusal = assertThrows(AwsException.class, + () -> service.updateApplication(applicationId, other), setter.getName()); + assertEquals("ValidationException", refusal.getErrorCode()); + assertEquals("Application must be in a stopped or created state in order to be updated.", + refusal.getMessage()); + refused.add(setter.getName()); + } + assertEquals(13, refused.size(), refused::toString); + Application unchanged = service.getApplication(applicationId); + assertEquals("100 vCPU", unchanged.getMaximumCapacity().getCpu()); + assertEquals("emr-7.5.0", unchanged.getReleaseLabel()); + } + + @Test + void aStartedApplicationWithInitialCapacityTakesANewMaximumCapacityButNoNewImages() { + EmrServerlessService service = service(); + CreateApplicationRequest create = createRequest(); + create.setImageConfiguration(image(CUSTOM_IMAGE)); + InitialCapacityConfig drivers = new InitialCapacityConfig(); + drivers.setWorkerCount(1L); + create.setInitialCapacity(Map.of("Driver", drivers)); + String applicationId = service.createApplication(create).getApplicationId(); + service.startApplication(applicationId); + UpdateApplicationRequest newImage = new UpdateApplicationRequest(); + newImage.setImageConfiguration(image(OTHER_IMAGE)); + UpdateApplicationRequest newDriverImage = new UpdateApplicationRequest(); + newDriverImage.setWorkerTypeSpecifications(Map.of("Driver", workerType(OTHER_IMAGE))); + UpdateApplicationRequest newCapacity = new UpdateApplicationRequest(); + newCapacity.setMaximumCapacity(capacity("100 vCPU")); + + AwsException refused = assertThrows(AwsException.class, + () -> service.updateApplication(applicationId, newImage)); + assertThrows(AwsException.class, () -> service.updateApplication(applicationId, newDriverImage)); + Application updated = service.updateApplication(applicationId, newCapacity); + + assertEquals("ValidationException", refused.getErrorCode()); + assertEquals("Custom image updates on running applications are not supported on applications with " + + "pre-initialized capacity configured.", refused.getMessage()); + assertEquals(CUSTOM_IMAGE, updated.getImageConfiguration().getImageUri()); + assertNull(updated.getWorkerTypeSpecifications()); + assertEquals("100 vCPU", updated.getMaximumCapacity().getCpu()); + // Stopped, it takes one. + service.stopApplication(applicationId); + assertEquals(OTHER_IMAGE, + service.updateApplication(applicationId, newImage).getImageConfiguration().getImageUri()); + } + + // A value of the type an update's setting takes, set rather than left out. + private static Object someValue(Class type) throws ReflectiveOperationException { + if (type == String.class) { + return "x"; + } + if (type == Map.class) { + return Map.of(); + } + if (type == List.class) { + return List.of(); + } + return type.getDeclaredConstructor().newInstance(); + } + + static MaximumCapacity capacity(String cpu) { + MaximumCapacity capacity = new MaximumCapacity(); + capacity.setCpu(cpu); + return capacity; + } + private static EmrServerlessService service() { EmulatorConfig config = mock(EmulatorConfig.class); when(config.defaultAccountId()).thenReturn(ACCOUNT_ID);