fix(emr-serverless): run a job's containers for its application's architecture (#8208)

An EMR Serverless run's driver and executors ran on the Docker host's own
platform whatever architecture the application named, so an ARM64
application ran amd64 images on an x86 host, and its arm64-only
dependencies with them.

- With floci.services.emrserverless.honour-architectures on, a run's
  containers run the image for its application's architecture,
  linux/amd64 for X86_64 and linux/arm64 for ARM64: the release's
  images, published for both, and a custom image, pulled for that
  platform.
- Off by default, as Lambda's honour-architectures: tools such as
  Terraform name X86_64 unless told otherwise, so on an arm64 host
  every run would move to emulation. Off, or for an application that
  names none, a run stays on the Docker host's own platform.
- DockerPlatforms.forArchitecture is the one mapping from an AWS
  architecture name, in either case, to its Docker platform. ECS task
  definitions, Lambda functions, EC2 images and the AMI build tool had
  a copy each and now use it, keeping their own special cases: EC2's
  i386, the tool's amd64 and its refusal of anything else.

A host of another architecture needs emulation to run the image.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-11-13 15:35:33 -04:00
1 parent a852078fc9
commit d3c8412977
16 files changed
+303 -38

No files matched your search

@@ -0,0 +1,24 @@
package io.floci.aws.core.common.docker;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
class DockerPlatformsTest {
@Test
void anAwsArchitectureInEitherCaseRunsOnItsDockerPlatform() {
// EMR Serverless and ECS spell them in capitals, Lambda and EC2 in lower case.
assertEquals("linux/amd64", DockerPlatforms.forArchitecture("X86_64"));
assertEquals("linux/amd64", DockerPlatforms.forArchitecture("x86_64"));
assertEquals("linux/arm64", DockerPlatforms.forArchitecture("ARM64"));
assertEquals("linux/arm64", DockerPlatforms.forArchitecture("arm64"));
}
@Test
void noArchitectureOrAnUnknownOneLeavesTheDockerHostsOwn() {
assertNull(DockerPlatforms.forArchitecture(null));
assertNull(DockerPlatforms.forArchitecture("s390x"));
}
}
@@ -0,0 +1,20 @@
package io.floci.aws.services.ecs.model;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
class RuntimePlatformTest {
@Test
void aTaskRunsItsImagesForItsCpuArchitecture() {
assertEquals("linux/amd64", new RuntimePlatform("X86_64", "LINUX").dockerPlatform());
assertEquals("linux/arm64", new RuntimePlatform("ARM64", "LINUX").dockerPlatform());
}
@Test
void aTaskThatNamesNoCpuArchitectureRunsOnTheDockerHostsOwn() {
assertNull(new RuntimePlatform(null, "LINUX").dockerPlatform());
}
}
@@ -29,6 +29,7 @@ import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;
@@ -38,8 +39,10 @@ import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.argThat;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.timeout;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@@ -315,6 +318,59 @@ class EmrServerlessJobRunServiceTest {
assertNull(runner.jobs.get(2).customImage());
}
@Test
void aRunsContainersRunForItsApplicationsArchitectureElseTheDockerHostsOwn() {
FakeRunner runner = new FakeRunner();
EmrServerlessJobRunService service = honouringService(runner);
CreateApplicationRequest graviton = applicationRequest("graviton", "emr-7.5.0", "SPARK");
graviton.setArchitecture("ARM64");
CreateApplicationRequest x86 = applicationRequest("x86", "emr-7.5.0", "SPARK");
x86.setArchitecture("X86_64");
CreateApplicationRequest hive = applicationRequest("hive-graviton", "emr-7.5.0", "HIVE");
hive.setArchitecture("ARM64");
service.startJobRun(applications.createApplication(graviton).getApplicationId(), request(null, null));
service.startJobRun(applications.createApplication(x86).getApplicationId(), request(null, null));
service.startJobRun(applicationId, request(null, null));
service.startJobRun(applications.createApplication(hive).getApplicationId(),
hiveRequest("s3://bucket/queries/report.sql", null, null));
for (int i = 0; i < 4; i++) {
runNext();
}
assertEquals("linux/arm64", runner.jobs.get(0).platform());
assertEquals("linux/amd64", runner.jobs.get(1).platform());
// An application that names none runs on the Docker host's own platform.
assertNull(runner.jobs.get(2).platform());
assertEquals("linux/arm64", runner.jobs.get(3).platform());
}
@Test
void aRunsContainersRunForItsApplicationsArchitectureOnlyWithHonourArchitecturesOn() {
for (boolean honour : List.of(true, false)) {
EmulatorConfig config = mock(EmulatorConfig.class, RETURNS_DEEP_STUBS);
when(config.services().emrserverless().runnerMode()).thenReturn("docker");
when(config.services().emrserverless().honourArchitectures()).thenReturn(honour);
EmrServerlessSparkRunner sparkRunner = mock(EmrServerlessSparkRunner.class);
when(sparkRunner.run(any(), any())).thenReturn(SparkJobRunner.Outcome.exitedWith(0));
EmrServerlessJobRunService service = new EmrServerlessJobRunService(new InMemoryStorageFactory(ACCOUNT_ID),
applications, iamService, new RegionResolver(REGION, ACCOUNT_ID), null, objectMapper, config, clock,
sparkRunner);
CreateApplicationRequest graviton = applicationRequest("graviton-" + honour, "emr-7.5.0", "SPARK");
graviton.setArchitecture("ARM64");
try {
service.startJobRun(applications.createApplication(graviton).getApplicationId(), request(null, null));
// Off, the default, an ARM64 application's run stays on the Docker host's own platform.
String platform = honour ? "linux/arm64" : null;
verify(sparkRunner, timeout(10_000))
.run(argThat(job -> Objects.equals(platform, job.platform())), any());
} finally {
service.shutdown();
}
}
}
@Test
void aSparkRunsExecutorsRunApartInTheirWorkerTypesImageWithinTheApplicationsMaximumCapacity() {
FakeRunner runner = new FakeRunner();
@@ -1085,6 +1141,12 @@ class EmrServerlessJobRunServiceTest {
null, objectMapper, 0, clock, runner, tasks::add, null, null, null, executorContainers);
}
// A service whose container runs run for their application's architecture, as with honour-architectures on.
private EmrServerlessJobRunService honouringService(SparkJobRunner runner) {
return new EmrServerlessJobRunService(store, applications, iamService, new RegionResolver(REGION, ACCOUNT_ID),
null, objectMapper, 0, clock, runner, tasks::add, null, null, null, 0, true);
}
private void runNext() {
tasks.remove(0).run();
}
@@ -564,6 +564,68 @@ class EmrServerlessSparkRunnerTest {
order.verify(lifecycle).create(any());
}
@Test
void anArm64RunsDriverAndExecutorsImageAreThoseForArm64() {
ContainerBuilder builder = mock(ContainerBuilder.class);
ContainerBuilder.Builder spec = mock(ContainerBuilder.Builder.class, RETURNS_SELF);
when(builder.newContainer(anyString())).thenReturn(spec);
EmrServerlessSparkRunner runner = runner(builder);
when(lifecycle.create(any(), eq("linux/arm64"))).thenReturn("driver");
when(lifecycle.resolveImageForLaunch(anyString(), eq(ImagePullBehavior.PREFER_CACHED), eq("linux/arm64")))
.thenAnswer(pull -> new LaunchImage(pull.getArgument(0), null));
ContainerExecStubs.completeEveryExec(docker, "driver", 7L, "", "");
runner.run(executorsJob(Map.of()).withPlatform("linux/arm64"), progress);
// The release's image is published for both architectures, so the driver's is created for arm64, and the
// executors' pulled for it.
verify(lifecycle).create(any(), eq("linux/arm64"));
verify(lifecycle, never()).create(any());
verify(lifecycle).resolveImageForLaunch("public.ecr.aws/emr-serverless/spark/emr-7.5.0:latest",
ImagePullBehavior.PREFER_CACHED, "linux/arm64");
}
@Test
void anArm64RunsCustomImageIsPulledForArm64() {
ContainerBuilder builder = mock(ContainerBuilder.class);
ContainerBuilder.Builder spec = mock(ContainerBuilder.Builder.class, RETURNS_SELF);
String custom = "123456789012.dkr.ecr.eu-west-1.amazonaws.com/spark/custom:7.5.0";
String inFlocisRegistry = "localhost:5100/123456789012/eu-west-1/spark/custom:7.5.0";
String pulled = "sha256:" + "6".repeat(64);
when(builder.newContainer(anyString())).thenReturn(spec);
when(builder.resolveImage(inFlocisRegistry)).thenReturn(inFlocisRegistry);
when(ecr.rewriteImageUri(custom)).thenReturn(inFlocisRegistry);
when(lifecycle.resolveImageForLaunch(inFlocisRegistry, ImagePullBehavior.DEFAULT, "linux/arm64"))
.thenReturn(new LaunchImage(pulled, null));
EmrServerlessSparkRunner runner = runner(builder);
when(lifecycle.create(any(), eq("linux/arm64"))).thenReturn("driver");
// The executors' worker type naming the same custom image.
runner.run(customImageJob(custom).withExecutors(new SparkJobRunner.Executors(2, custom, 0, 0, 0, null))
.withPlatform("linux/arm64"), progress);
verify(lifecycle, times(2)).resolveImageForLaunch(inFlocisRegistry, ImagePullBehavior.DEFAULT, "linux/arm64");
verify(builder).newContainer(pulled);
}
@Test
void anArm64RunsExecutorsContainerStartsForArm64() {
ContainerBuilder builder = mock(ContainerBuilder.class);
ContainerBuilder.Builder spec = mock(ContainerBuilder.Builder.class, RETURNS_SELF);
when(builder.newContainer(anyString())).thenReturn(spec);
when(lifecycle.createAndStart(any(), eq("linux/arm64")))
.thenReturn(new ContainerLifecycleManager.ContainerInfo("executor", Map.of()));
when(lifecycle.resolveContainerNetworkIp("executor", "floci-network")).thenReturn("172.18.0.9");
SparkExecutors.Containers containers = runner(builder).executorContainers(
executorsJob(Map.of()).withPlatform("linux/arm64"), "sha256:image", "floci-network", "172.18.0.4",
"secret", AWS_ENVIRONMENT, Map.of());
containers.start(1, FOUR_CORE_EXECUTOR);
verify(lifecycle).createAndStart(any(), eq("linux/arm64"));
verify(lifecycle, never()).createAndStart(any());
}
@Test
void anApplicationsImageForItsExecutorsIsPulledBeforeTheDriverStarts() {
ContainerBuilder builder = mock(ContainerBuilder.class);