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.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-11-14 12:44:35 -04:00
1 parent 97fcbf21d0
commit f665d5f5e7
5 files changed
+286 -3

No files matched your search

@@ -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"));
}
}
@@ -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<String> 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();
@@ -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<Map<String, Application>> 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<String> 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<String> 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);