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

@@ -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 <em>this same</em> Floci left on the daemon (matched by the
@@ -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);
@@ -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;
};
}
}
@@ -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);
}
}
@@ -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);
}
}
@@ -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<String> 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<JobRunRecord> 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) {
@@ -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();
}
/**
@@ -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<String, String> environment,
List<String> archives, Storage storage, Map<String, String> coreSite,
Map<String, String> sparkLog4j2, Map<String, String> mapredSite, Map<String, String> hadoopLog4j,
Map<String, String> hadoopEnvironment, Executors executors) {
Map<String, String> 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<String, String> sparkDefaults, List<String> application, List<String> command,
Duration timeout, String logFolder, CloudWatchLogs cloudWatchLogs,
Map<String, String> environment, List<String> archives, Storage storage,
Map<String, String> coreSite, Map<String, String> sparkLog4j2, Map<String, String> mapredSite,
Map<String, String> hadoopLog4j, Map<String, String> 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);
}
}
@@ -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<String> requestedDockerPlatform(LambdaFunction fn, LambdaDockerFlags dockerFlags) {
@@ -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) {