diff --git a/docs/services/emr-serverless.md b/docs/services/emr-serverless.md index 0e669f170d..5e1ce509cd 100644 --- a/docs/services/emr-serverless.md +++ b/docs/services/emr-serverless.md @@ -14,6 +14,7 @@ EMR Serverless is an AWS service that provides serverless data analytics environ | `floci.services.emrserverless.image-template` | Image for a `docker` job run whose application names no custom image, `%s` standing for the release label | `public.ecr.aws/emr-serverless/spark/%s:latest`, or `spark4/` in place of `spark/` for an `emr-spark-*` release | | `floci.services.emrserverless.hive-image-template` | Image for a `docker` Hive job run whose application names no custom image, `%s` standing for the release label | `public.ecr.aws/emr-serverless/hive/%s:latest` | | `floci.services.emrserverless.executor-containers` | How many executors a `docker` Spark job run may have at once, each in a container of its own (see **Executors** under [Running Spark job runs](#running-spark-job-runs)); `0`, the default, runs a run's executors in its driver's container, in Spark's local mode | `0` | +| `floci.services.emrserverless.honour-architectures` | Run a `docker` job run's driver and executors for its application's `architecture` (see **Architecture** under [Running Spark job runs](#running-spark-job-runs)); `false` runs them on the Docker host's own platform | `false` | | `floci.services.emrserverless.reconcile-containers-on-startup` | Remove the drivers and executors a previous run of this Floci left on the Docker daemon before the first job run starts; turn it off when two Floci instances share a daemon with the same port and no resource namespace | `true` | ## Endpoints @@ -228,6 +229,15 @@ worker of its own on EMR Serverless (see **Executors** below). wording. An update that leaves `imageUri` out removes that image configuration. Without a custom image, `image-template` names another image than the release's, such as a mirror or a dated tag of it: `registry.internal/emr-serverless/spark/%s:20250425`. +* **Architecture**: a run's containers, its driver's and its executors', run on the Docker host's own platform by + default. With `honour-architectures` on (`FLOCI_SERVICES_EMRSERVERLESS_HONOUR_ARCHITECTURES=true`) they run the + image for its application's `architecture`, `linux/amd64` for `X86_64` and `linux/arm64` for `ARM64`, the + release's images being published for both and a custom image pulled for that platform; an application that names + none still runs on the host's own, where on EMR Serverless "x86_64 is the default option". It is off by default, + as Lambda's is, because tools such as Terraform name `X86_64` unless told otherwise, which on an arm64 host would + move every run to emulation. A host of another architecture runs the image under emulation, which Docker Desktop + has and a Linux host gets from QEMU's `binfmt_misc` handlers; without it the driver cannot start + ("exec format error") and the run fails. Floci does not fall back to the host's platform when the setting is on. ## Running Hive job runs diff --git a/src/main/java/io/floci/aws/config/EmulatorConfig.java b/src/main/java/io/floci/aws/config/EmulatorConfig.java index 9ac24938ad..fe90a827d0 100644 --- a/src/main/java/io/floci/aws/config/EmulatorConfig.java +++ b/src/main/java/io/floci/aws/config/EmulatorConfig.java @@ -1190,6 +1190,18 @@ public interface EmulatorConfig { @WithDefault("0") int executorContainers(); + /** + * When true, a {@code docker} job run's driver and executors run for its application's architecture, + * {@code X86_64} on linux/amd64 and {@code ARM64} on linux/arm64; when false, the default, or for an + * application that names none, on the Docker host's own platform. Off by default, as Lambda's + * {@code honour-architectures}: tools such as Terraform name {@code X86_64} unless told otherwise, and a + * foreign platform needs the Docker host to emulate it (Docker Desktop, or {@code binfmt_misc} with QEMU). + * + * Env var: FLOCI_SERVICES_EMRSERVERLESS_HONOUR_ARCHITECTURES + */ + @WithDefault("false") + boolean honourArchitectures(); + /** * When true, Floci removes, in the {@code docker} runner mode, every EMR Serverless driver a * previous run of this same Floci left on the daemon (matched by the diff --git a/src/main/java/io/floci/aws/core/common/docker/ContainerLifecycleManager.java b/src/main/java/io/floci/aws/core/common/docker/ContainerLifecycleManager.java index 0bd461e39e..78947d3461 100644 --- a/src/main/java/io/floci/aws/core/common/docker/ContainerLifecycleManager.java +++ b/src/main/java/io/floci/aws/core/common/docker/ContainerLifecycleManager.java @@ -141,6 +141,15 @@ public class ContainerLifecycleManager { return createAndStart(spec, false); } + /** + * Creates and starts a container for a specific Docker platform. + * + * @param platform Docker platform, such as {@code linux/arm64}, or null for the daemon's own + */ + public ContainerInfo createAndStart(ContainerSpec spec, String platform) { + return createAndStart(spec, platform, false); + } + /** Starts a disposable container, reclaiming its anonymous volumes if startup fails. */ public ContainerInfo createAndStartEphemeral(ContainerSpec spec) { return createAndStart(spec, true); diff --git a/src/main/java/io/floci/aws/core/common/docker/DockerPlatforms.java b/src/main/java/io/floci/aws/core/common/docker/DockerPlatforms.java new file mode 100644 index 0000000000..5ba286228b --- /dev/null +++ b/src/main/java/io/floci/aws/core/common/docker/DockerPlatforms.java @@ -0,0 +1,28 @@ +package io.floci.aws.core.common.docker; + +import java.util.Locale; + +/** + * The Docker platforms AWS's architecture names stand for, as a service's resources name them: the one mapping + * ECS task definitions, Lambda functions, EC2 images (and the AMI build tool) and EMR Serverless applications share. + */ +public final class DockerPlatforms { + + private DockerPlatforms() { + } + + /** + * The Docker platform an AWS architecture name runs on, {@code x86_64} or {@code arm64} in whatever case a service + * spells them, or null for none or one Docker has no platform for, which leaves the Docker host's own. + */ + public static String forArchitecture(String architecture) { + if (architecture == null) { + return null; + } + return switch (architecture.toLowerCase(Locale.ROOT)) { + case "x86_64" -> "linux/amd64"; + case "arm64" -> "linux/arm64"; + default -> null; + }; + } +} diff --git a/src/main/java/io/floci/aws/services/ec2/AmiImageResolver.java b/src/main/java/io/floci/aws/services/ec2/AmiImageResolver.java index 5a44ebf292..4b945e2102 100644 --- a/src/main/java/io/floci/aws/services/ec2/AmiImageResolver.java +++ b/src/main/java/io/floci/aws/services/ec2/AmiImageResolver.java @@ -1,6 +1,7 @@ package io.floci.aws.services.ec2; import io.floci.aws.core.common.AwsException; +import io.floci.aws.core.common.docker.DockerPlatforms; import jakarta.enterprise.context.ApplicationScoped; import jakarta.inject.Inject; import org.jboss.logging.Logger; @@ -60,12 +61,8 @@ public class AmiImageResolver { dockerPlatform(image.architecture)); } + // EC2's i386, which Docker runs as linux/386, beside the two architectures every service names. private static String dockerPlatform(String architecture) { - return switch (architecture) { - case "arm64" -> "linux/arm64"; - case "x86_64" -> "linux/amd64"; - case "i386" -> "linux/386"; - default -> null; - }; + return "i386".equals(architecture) ? "linux/386" : DockerPlatforms.forArchitecture(architecture); } } diff --git a/src/main/java/io/floci/aws/services/ecs/model/RuntimePlatform.java b/src/main/java/io/floci/aws/services/ecs/model/RuntimePlatform.java index d7eda34483..fc127baf3b 100644 --- a/src/main/java/io/floci/aws/services/ecs/model/RuntimePlatform.java +++ b/src/main/java/io/floci/aws/services/ecs/model/RuntimePlatform.java @@ -1,5 +1,6 @@ package io.floci.aws.services.ecs.model; +import io.floci.aws.core.common.docker.DockerPlatforms; import io.quarkus.runtime.annotations.RegisterForReflection; /** @@ -20,13 +21,6 @@ public record RuntimePlatform(String cpuArchitecture, String operatingSystemFami * architecture is named and the task runs on the Docker host's own. */ public String dockerPlatform() { - if (cpuArchitecture == null) { - return null; - } - return switch (cpuArchitecture) { - case "X86_64" -> "linux/amd64"; - case "ARM64" -> "linux/arm64"; - default -> null; - }; + return DockerPlatforms.forArchitecture(cpuArchitecture); } } diff --git a/src/main/java/io/floci/aws/services/emrserverless/EmrServerlessJobRunService.java b/src/main/java/io/floci/aws/services/emrserverless/EmrServerlessJobRunService.java index 3af29dfd00..477af09c96 100644 --- a/src/main/java/io/floci/aws/services/emrserverless/EmrServerlessJobRunService.java +++ b/src/main/java/io/floci/aws/services/emrserverless/EmrServerlessJobRunService.java @@ -15,6 +15,7 @@ import io.floci.aws.core.common.RequestContext; import io.floci.aws.core.common.RequestScopes; import io.floci.aws.core.common.Resettable; import io.floci.aws.core.common.SmithyValidationErrors; +import io.floci.aws.core.common.docker.DockerPlatforms; import io.floci.aws.core.storage.AccountAwareStorageBackend; import io.floci.aws.core.storage.StorageFactory; import io.floci.aws.services.emrserverless.model.Application; @@ -165,6 +166,8 @@ public class EmrServerlessJobRunService implements Resettable { private final String dockerNetwork; // How many executor containers a Spark run may have at once: executor-containers, 0 for Spark's local mode. private final int executorContainers; + // Whether a container run runs for its application's architecture: honour-architectures, off by default. + private final boolean honourArchitectures; // Container runs a thread is running; one not here that has not ended was left by a restart. private final Set executing = new HashSet<>(); // Raised by each reset, so a run's thread from before it writes nothing back after the wipe. @@ -183,7 +186,8 @@ public class EmrServerlessJobRunService implements Resettable { config.services().emrserverless().imageTemplate().orElse(null), config.services().emrserverless().hiveImageTemplate().orElse(null), config.services().emrserverless().dockerNetwork().orElse(null), - config.services().emrserverless().executorContainers()); + config.services().emrserverless().executorContainers(), + config.services().emrserverless().honourArchitectures()); // Before any run of this process starts: a driver an earlier one left belongs to a run read as FAILED. if (this.sparkRunner != null && config.services().emrserverless().reconcileContainersOnStartup()) { sparkRunner.removeLeftoverDrivers(SERVICE); @@ -220,6 +224,16 @@ public class EmrServerlessJobRunService implements Resettable { SparkJobRunner sparkRunner, Executor runExecutor, String imageTemplate, String hiveImageTemplate, String dockerNetwork, int executorContainers) { + this(store, applications, iamService, regionResolver, requestContext, objectMapper, runDurationSeconds, clock, + sparkRunner, runExecutor, imageTemplate, hiveImageTemplate, dockerNetwork, executorContainers, false); + } + + EmrServerlessJobRunService(AccountAwareStorageBackend store, EmrServerlessService applications, + IamService iamService, RegionResolver regionResolver, RequestContext requestContext, + ObjectMapper objectMapper, int runDurationSeconds, Clock clock, + SparkJobRunner sparkRunner, Executor runExecutor, + String imageTemplate, String hiveImageTemplate, String dockerNetwork, + int executorContainers, boolean honourArchitectures) { if (runDurationSeconds < 0) { throw new IllegalArgumentException( "floci.services.emrserverless.job-run-duration-seconds must not be negative: " + runDurationSeconds); @@ -238,6 +252,7 @@ public class EmrServerlessJobRunService implements Resettable { this.hiveImageTemplate = hiveImageTemplate; this.dockerNetwork = dockerNetwork; this.executorContainers = executorContainers; + this.honourArchitectures = honourArchitectures; } @PreDestroy @@ -557,9 +572,13 @@ public class EmrServerlessJobRunService implements Resettable { archives(sparkProperties), null, Map.of(), Map.of(), Map.of(), Map.of(), Map.of(), executors(app, executorContainers)); } + // With honour-architectures on, its containers run for the application's architecture, "x86_64 is the default + // option" on EMR Serverless; with it off, or where the application names none, for the Docker host's own. + SparkJobRunner.Job onPlatform = job.withPlatform( + honourArchitectures ? DockerPlatforms.forArchitecture(app.getArchitecture()) : null); executing.add(run.getJobRunId()); long launchedIn = generation; - runExecutor.execute(() -> execute(job, launchedIn)); + runExecutor.execute(() -> execute(onPlatform, launchedIn)); } private void execute(SparkJobRunner.Job job, long launchedIn) { diff --git a/src/main/java/io/floci/aws/services/emrserverless/EmrServerlessSparkRunner.java b/src/main/java/io/floci/aws/services/emrserverless/EmrServerlessSparkRunner.java index 30ecb37a25..57458bf053 100644 --- a/src/main/java/io/floci/aws/services/emrserverless/EmrServerlessSparkRunner.java +++ b/src/main/java/io/floci/aws/services/emrserverless/EmrServerlessSparkRunner.java @@ -510,7 +510,9 @@ public class EmrServerlessSparkRunner implements SparkJobRunner, ContainerTeardo .withLabels(labels) .withJavaTrustStore() .build(); - String containerId = lifecycleManager.create(spec); + // For the application's architecture, or the Docker host's own where it names none. + String containerId = job.platform() == null ? lifecycleManager.create(spec) + : lifecycleManager.create(spec, job.platform()); launch.started(containerId); // A cluster's HDFS is on a network of its own, which only its steps join; on YARN the executors there reach // the driver at the name its container has on it. @@ -809,10 +811,10 @@ public class EmrServerlessSparkRunner implements SparkJobRunner, ContainerTeardo private String executorImage(Job job) { if (job.executors().image() == null) { return lifecycleManager.resolveImageForLaunch(image(job.imageTemplate(), job.releaseLabel(), Engine.SPARK), - ImagePullBehavior.PREFER_CACHED, null).imageId(); + ImagePullBehavior.PREFER_CACHED, job.platform()).imageId(); } String image = containerBuilder.resolveImage(ecrRegistryManager.rewriteImageUri(job.executors().image())); - return lifecycleManager.resolveImageForLaunch(image, ImagePullBehavior.DEFAULT, null).imageId(); + return lifecycleManager.resolveImageForLaunch(image, ImagePullBehavior.DEFAULT, job.platform()).imageId(); } /** @@ -845,7 +847,8 @@ public class EmrServerlessSparkRunner implements SparkJobRunner, ContainerTeardo .withLabels(labels) .withJavaTrustStore() .build(); - String containerId = lifecycleManager.createAndStart(spec).containerId(); + String containerId = (job.platform() == null ? lifecycleManager.createAndStart(spec) + : lifecycleManager.createAndStart(spec, job.platform())).containerId(); try { return new SparkExecutors.Worker(containerId, lifecycleManager.resolveContainerNetworkIp(containerId, network)); @@ -877,14 +880,15 @@ public class EmrServerlessSparkRunner implements SparkJobRunner, ContainerTeardo * image. A custom image's ECR URI leads to Floci's own registry, as a Lambda function's or an ECS task's * does, and the image is pulled for every run, as an ECS task's is, so a tag pushed again is what the next * run gets ("New workloads submitted after the update automatically will use the new image"). The driver - * starts from the image id that pull resolved, which a later push cannot move. + * starts from the image id that pull resolved, which a later push cannot move. Either is pulled for the run's + * platform, its application's architecture. */ private String launchImage(Job job) { if (job.customImage() == null) { return image(job.imageTemplate(), job.releaseLabel(), job.engine()); } String image = containerBuilder.resolveImage(ecrRegistryManager.rewriteImageUri(job.customImage())); - return lifecycleManager.resolveImageForLaunch(image, ImagePullBehavior.DEFAULT, null).imageId(); + return lifecycleManager.resolveImageForLaunch(image, ImagePullBehavior.DEFAULT, job.platform()).imageId(); } /** diff --git a/src/main/java/io/floci/aws/services/emrserverless/SparkJobRunner.java b/src/main/java/io/floci/aws/services/emrserverless/SparkJobRunner.java index 68bf648179..d0926157ff 100644 --- a/src/main/java/io/floci/aws/services/emrserverless/SparkJobRunner.java +++ b/src/main/java/io/floci/aws/services/emrserverless/SparkJobRunner.java @@ -80,8 +80,10 @@ public interface SparkJobRunner { * {@code log4j2.properties} lines its Spark gets after those of its configuration, which they replace where * a key is the same (empty for none); on a cluster, the {@code mapred-site.xml} properties its * MapReduce gets, the {@code log4j.properties} lines and the {@code hadoop-env.sh} exports its {@code hadoop} - * command gets (each empty for none); and, for an EMR Serverless Spark run, where its executors run, in - * containers of their own (null for Spark's local mode, in the driver's container). + * command gets (each empty for none); for an EMR Serverless Spark run, where its executors run, in + * containers of their own (null for Spark's local mode, in the driver's container); and the Docker platform + * its containers run, as its application's architecture names it with {@code honour-architectures} on, + * {@code linux/amd64} or {@code linux/arm64} (null for the Docker host's own). */ record Job(String service, String runId, String accountId, String region, String releaseLabel, Engine engine, String imageTemplate, String customImage, String dockerNetwork, String executionRole, @@ -89,21 +91,36 @@ public interface SparkJobRunner { String logFolder, CloudWatchLogs cloudWatchLogs, Map environment, List archives, Storage storage, Map coreSite, Map sparkLog4j2, Map mapredSite, Map hadoopLog4j, - Map hadoopEnvironment, Executors executors) { + Map hadoopEnvironment, Executors executors, String platform) { + + /** A job on the Docker host's own platform. */ + public Job(String service, String runId, String accountId, String region, String releaseLabel, Engine engine, + String imageTemplate, String customImage, String dockerNetwork, String executionRole, + Map sparkDefaults, List application, List command, + Duration timeout, String logFolder, CloudWatchLogs cloudWatchLogs, + Map environment, List archives, Storage storage, + Map coreSite, Map sparkLog4j2, Map mapredSite, + Map hadoopLog4j, Map hadoopEnvironment, Executors executors) { + this(service, runId, accountId, region, releaseLabel, engine, imageTemplate, customImage, dockerNetwork, + executionRole, sparkDefaults, application, command, timeout, logFolder, cloudWatchLogs, + environment, archives, storage, coreSite, sparkLog4j2, mapredSite, hadoopLog4j, + hadoopEnvironment, executors, null); + } /** This job, on {@code storage}. */ public Job onStorage(Storage storage) { return new Job(service, runId, accountId, region, releaseLabel, engine, imageTemplate, customImage, dockerNetwork, executionRole, sparkDefaults, application, command, timeout, logFolder, cloudWatchLogs, environment, archives, storage, coreSite, sparkLog4j2, mapredSite, hadoopLog4j, - hadoopEnvironment, executors); + hadoopEnvironment, executors, platform); } /** This job, run in {@code image}. */ public Job inImage(String image) { return new Job(service, runId, accountId, region, releaseLabel, engine, image, customImage, dockerNetwork, executionRole, sparkDefaults, application, command, timeout, logFolder, cloudWatchLogs, environment, - archives, storage, coreSite, sparkLog4j2, mapredSite, hadoopLog4j, hadoopEnvironment, executors); + archives, storage, coreSite, sparkLog4j2, mapredSite, hadoopLog4j, hadoopEnvironment, executors, + platform); } /** This job, its executors where {@code executors} has them. */ @@ -111,7 +128,15 @@ public interface SparkJobRunner { return new Job(service, runId, accountId, region, releaseLabel, engine, imageTemplate, customImage, dockerNetwork, executionRole, sparkDefaults, application, command, timeout, logFolder, cloudWatchLogs, environment, archives, storage, coreSite, sparkLog4j2, mapredSite, hadoopLog4j, - hadoopEnvironment, executors); + hadoopEnvironment, executors, platform); + } + + /** This job, its containers run for {@code platform} (null for the Docker host's own). */ + public Job withPlatform(String platform) { + return new Job(service, runId, accountId, region, releaseLabel, engine, imageTemplate, customImage, + dockerNetwork, executionRole, sparkDefaults, application, command, timeout, logFolder, + cloudWatchLogs, environment, archives, storage, coreSite, sparkLog4j2, mapredSite, hadoopLog4j, + hadoopEnvironment, executors, platform); } } diff --git a/src/main/java/io/floci/aws/services/lambda/launcher/ContainerLauncher.java b/src/main/java/io/floci/aws/services/lambda/launcher/ContainerLauncher.java index 0d9b70ef7b..2267b61767 100644 --- a/src/main/java/io/floci/aws/services/lambda/launcher/ContainerLauncher.java +++ b/src/main/java/io/floci/aws/services/lambda/launcher/ContainerLauncher.java @@ -13,6 +13,7 @@ import io.floci.aws.core.common.docker.ContainerLogStreamer; import io.floci.aws.core.common.docker.ContainerSpec; import io.floci.aws.core.common.docker.ContainerStorageHelper; import io.floci.aws.core.common.docker.DockerHostResolver; +import io.floci.aws.core.common.docker.DockerPlatforms; import io.floci.aws.core.common.docker.LaunchedContainerAwsEnv; import io.floci.aws.core.common.docker.RetryingTarCopier; import io.floci.aws.services.ec2.Ec2Service; @@ -568,11 +569,7 @@ public class ContainerLauncher implements LambdaRuntimeLauncher { if (architectures.size() != 1) { return Optional.empty(); } - return switch (architectures.getFirst()) { - case "arm64" -> Optional.of("linux/arm64"); - case "x86_64" -> Optional.of("linux/amd64"); - default -> Optional.empty(); - }; + return Optional.ofNullable(DockerPlatforms.forArchitecture(architectures.getFirst())); } private Optional requestedDockerPlatform(LambdaFunction fn, LambdaDockerFlags dockerFlags) { diff --git a/src/main/java/io/floci/aws/tools/ami/AmiImageTool.java b/src/main/java/io/floci/aws/tools/ami/AmiImageTool.java index 3e4399cad7..8e5eadd00e 100644 --- a/src/main/java/io/floci/aws/tools/ami/AmiImageTool.java +++ b/src/main/java/io/floci/aws/tools/ami/AmiImageTool.java @@ -3,6 +3,7 @@ package io.floci.aws.tools.ami; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; +import io.floci.aws.core.common.docker.DockerPlatforms; import java.io.IOException; import java.io.InputStream; @@ -264,11 +265,11 @@ public final class AmiImageTool { } private static String platform(String architecture) { - return switch (architecture) { - case "arm64" -> "linux/arm64"; - case "amd64", "x86_64" -> "linux/amd64"; - default -> throw new IllegalArgumentException("Unsupported Docker platform architecture: " + architecture); - }; + String platform = DockerPlatforms.forArchitecture(awsArchitecture(architecture)); + if (platform == null) { + throw new IllegalArgumentException("Unsupported Docker platform architecture: " + architecture); + } + return platform; } private static void required(String value, String field) { diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 8028970ee9..7ddd1dc228 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -1051,6 +1051,7 @@ floci: # image-template: "registry.internal/emr-serverless/spark/%s:20250425" # %s = the release label # hive-image-template: "registry.internal/emr-serverless/hive/%s:20250425" # for Hive job runs executor-containers: 0 # a docker Spark run's executors at once, each in a container; 0 = local mode + honour-architectures: false # true = run a docker job run for its application's architecture # Remove the drivers and executors a previous run of this deployment left on the daemon before # the first job run starts. Scoped to this deployment's own containers. reconcile-containers-on-startup: true diff --git a/src/test/java/io/floci/aws/core/common/docker/DockerPlatformsTest.java b/src/test/java/io/floci/aws/core/common/docker/DockerPlatformsTest.java new file mode 100644 index 0000000000..6cfb6be321 --- /dev/null +++ b/src/test/java/io/floci/aws/core/common/docker/DockerPlatformsTest.java @@ -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")); + } +} diff --git a/src/test/java/io/floci/aws/services/ecs/model/RuntimePlatformTest.java b/src/test/java/io/floci/aws/services/ecs/model/RuntimePlatformTest.java new file mode 100644 index 0000000000..dc68e5f289 --- /dev/null +++ b/src/test/java/io/floci/aws/services/ecs/model/RuntimePlatformTest.java @@ -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()); + } +} 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 5f64bdb566..2a6ecf6cf0 100644 --- a/src/test/java/io/floci/aws/services/emrserverless/EmrServerlessJobRunServiceTest.java +++ b/src/test/java/io/floci/aws/services/emrserverless/EmrServerlessJobRunServiceTest.java @@ -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(); } diff --git a/src/test/java/io/floci/aws/services/emrserverless/EmrServerlessSparkRunnerTest.java b/src/test/java/io/floci/aws/services/emrserverless/EmrServerlessSparkRunnerTest.java index 1e77686eb1..3899c51fb9 100644 --- a/src/test/java/io/floci/aws/services/emrserverless/EmrServerlessSparkRunnerTest.java +++ b/src/test/java/io/floci/aws/services/emrserverless/EmrServerlessSparkRunnerTest.java @@ -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);