mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(emr-serverless): run a Spark job's executors in their own containers (#8168)
EMR Serverless gives a Spark job's driver and each of its executors a worker of their own; a docker job run ran them all in one container, in Spark's local mode. With executor-containers above 0, the driver's container now runs Spark's standalone master beside the driver, and each executor runs in a container of its own, a standalone worker offering one executor's cores and memory. Floci follows the driver through its UI: an executor's size from its settings, and how many executors dynamic allocation wants from its metrics. It starts executor containers, or removes idle ones, to match: at most executor-containers at once, and none that would take the application past its maximumCapacity. They share a secret per run, run the Executor worker type's image, and go with the driver. executor-containers is 0 by default: then, or on the host's network, the executors run in the driver's container, as before.
This commit is contained in:
1 parent
9f9b116f73
commit
438e01a225
13 files changed
+1582
-57
No files matched your search
@@ -57,7 +57,7 @@ import static org.junit.jupiter.api.Assumptions.assumeTrue;
|
||||
/**
|
||||
* EMR steps and EMR Serverless job runs through Floci's Docker runner for real: a cluster in the {@code docker}
|
||||
* runner mode runs each step, and an application each job run, in a container of the release's EMR Serverless image,
|
||||
* which reaches Floci's S3 and Glue from there. The cached {@code emr-7.5.0} image keeps a local run from pulling;
|
||||
* which reaches Floci's S3 and Glue from there, a job run's executors each in a container of their own, as the profile sets. The cached {@code emr-7.5.0} image keeps a local run from pulling;
|
||||
* CI pulls it (3.5 GB) the first time.
|
||||
*/
|
||||
@QuarkusTest
|
||||
@@ -392,6 +392,38 @@ class EmrDockerRunnerDockerIntegrationTest {
|
||||
assertEquals("from the archive environment\n", partFiles("venv-out/"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void anEmrServerlessRunsExecutorsRunInContainersOfTheirOwnThatGoWithIt() {
|
||||
put("side/hosts.py", """
|
||||
import socket, sys, time
|
||||
from pyspark.sql import SparkSession
|
||||
spark = SparkSession.builder.getOrCreate()
|
||||
hosts = spark.sparkContext.parallelize(range(24), 24) \\
|
||||
.map(lambda i: (time.sleep(0.5), socket.gethostname())[1]).distinct().collect()
|
||||
where = "with the driver" if socket.gethostname() in hosts else "apart from the driver"
|
||||
spark.createDataFrame([("%d executors, %s" % (len(hosts), where),)], ["value"]) \\
|
||||
.coalesce(1).write.text(sys.argv[1])
|
||||
""");
|
||||
String applicationId = createSparkApplication(REGION, "emr-7.5.0");
|
||||
cleanup.register(() -> deleteApplication(REGION, applicationId));
|
||||
ObjectNode run = JSON.createObjectNode().put("clientToken", UUID.randomUUID().toString())
|
||||
.put("executionRoleArn", "arn:aws:iam::000000000000:role/emr-serverless-job");
|
||||
ObjectNode sparkSubmit = run.putObject("jobDriver").putObject("sparkSubmit")
|
||||
.put("entryPoint", s3Uri("side/hosts.py"))
|
||||
.put("sparkSubmitParameters", "--conf spark.executor.cores=2 --conf spark.executor.memory=1g");
|
||||
sparkSubmit.putArray("entryPointArguments").add(s3Uri("hosts-out/"));
|
||||
String jobRunId = startJobRun(REGION, applicationId, run.toString());
|
||||
|
||||
Response ended = awaitJobRun(REGION, applicationId, jobRunId);
|
||||
|
||||
assertEquals("SUCCESS", ended.jsonPath().getString("jobRun.state"));
|
||||
// Each executor has a worker of its own, as on EMR Serverless, up to the profile's two.
|
||||
assertEquals("2 executors, apart from the driver\n", partFiles("hosts-out/"));
|
||||
await().atMost(Duration.ofSeconds(30)).until(() -> lifecycle.getDockerClient().listContainersCmd()
|
||||
.withShowAll(true).withLabelFilter(Map.of(ContainerStorageHelper.RESOURCE_ID_LABEL, jobRunId))
|
||||
.exec().isEmpty());
|
||||
}
|
||||
|
||||
/**
|
||||
* A stand-in for the guide's packed virtual environment that needs no package index: its {@code bin/python}
|
||||
* runs the image's Python with the archive's own {@code lib} on the path, where the module the job imports is.
|
||||
|
||||
+69
-1
@@ -13,6 +13,7 @@ import io.floci.aws.services.emrserverless.model.JobDriver;
|
||||
import io.floci.aws.services.emrserverless.model.JobRun;
|
||||
import io.floci.aws.services.emrserverless.model.JobRunAttemptSummary;
|
||||
import io.floci.aws.services.emrserverless.model.JobRunRecord;
|
||||
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;
|
||||
@@ -295,7 +296,7 @@ class EmrServerlessJobRunServiceTest {
|
||||
String driverImage = "123456789012.dkr.ecr.us-east-1.amazonaws.com/spark/driver:7.5.0";
|
||||
CreateApplicationRequest wholeApplication = applicationRequest("custom", "emr-7.5.0", "SPARK");
|
||||
wholeApplication.setImageConfiguration(EmrServerlessServiceTest.image(applicationImage));
|
||||
// Local mode runs the executors in the driver's container, so an Executor image is not used.
|
||||
// The executors' worker type's image is theirs, not the driver's.
|
||||
wholeApplication.setWorkerTypeSpecifications(
|
||||
Map.of("Executor", EmrServerlessServiceTest.workerType(driverImage)));
|
||||
CreateApplicationRequest perWorkerType = applicationRequest("per-worker-type", "emr-7.5.0", "SPARK");
|
||||
@@ -314,6 +315,67 @@ class EmrServerlessJobRunServiceTest {
|
||||
assertNull(runner.jobs.get(2).customImage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aSparkRunsExecutorsRunApartInTheirWorkerTypesImageWithinTheApplicationsMaximumCapacity() {
|
||||
FakeRunner runner = new FakeRunner();
|
||||
EmrServerlessJobRunService service = dockerService(runner, 2);
|
||||
String applicationImage = "123456789012.dkr.ecr.us-east-1.amazonaws.com/spark/custom:7.5.0";
|
||||
String executorImage = "123456789012.dkr.ecr.us-east-1.amazonaws.com/spark/executor:7.5.0";
|
||||
CreateApplicationRequest capped = applicationRequest("capped", "emr-7.5.0", "SPARK");
|
||||
capped.setImageConfiguration(EmrServerlessServiceTest.image(applicationImage));
|
||||
capped.setWorkerTypeSpecifications(Map.of("Executor", EmrServerlessServiceTest.workerType(executorImage)));
|
||||
MaximumCapacity capacity = new MaximumCapacity();
|
||||
capacity.setCpu("12 vCPU");
|
||||
capacity.setMemory("64 GB");
|
||||
capacity.setDisk("200 GB");
|
||||
capped.setMaximumCapacity(capacity);
|
||||
CreateApplicationRequest whole = applicationRequest("whole", "emr-7.5.0", "SPARK");
|
||||
whole.setImageConfiguration(EmrServerlessServiceTest.image(applicationImage));
|
||||
String hive = applications.createApplication(applicationRequest("hive", "emr-7.5.0", "HIVE"))
|
||||
.getApplicationId();
|
||||
|
||||
Application cappedApplication = applications.createApplication(capped);
|
||||
Application wholeApplication = applications.createApplication(whole);
|
||||
service.startJobRun(cappedApplication.getApplicationId(), request(null, null));
|
||||
service.startJobRun(wholeApplication.getApplicationId(), request(null, null));
|
||||
service.startJobRun(applicationId, request(null, null));
|
||||
service.startJobRun(hive, hiveRequest("s3://bucket/queries/report.sql", null, null));
|
||||
for (int i = 0; i < 4; i++) {
|
||||
runNext();
|
||||
}
|
||||
|
||||
// Each within its application's capacity, which that application's runs share.
|
||||
assertEquals(new SparkJobRunner.Executors(2, executorImage, 12, 64, 200, cappedApplication.getArn()),
|
||||
runner.jobs.get(0).executors());
|
||||
// The application's image applies to every worker type; without one, the release's.
|
||||
assertEquals(new SparkJobRunner.Executors(2, applicationImage, 0, 0, 0, wholeApplication.getArn()),
|
||||
runner.jobs.get(1).executors());
|
||||
assertEquals(new SparkJobRunner.Executors(2, null, 0, 0, 0,
|
||||
applications.getApplication(applicationId).getArn()), runner.jobs.get(2).executors());
|
||||
// Tez runs a Hive run's tasks in its driver's container.
|
||||
assertNull(runner.jobs.get(3).executors());
|
||||
}
|
||||
|
||||
@Test
|
||||
void withoutExecutorContainersARunsExecutorsRunInItsDriversContainer() {
|
||||
FakeRunner runner = new FakeRunner();
|
||||
EmrServerlessJobRunService service = dockerService(runner, 0);
|
||||
|
||||
service.startJobRun(applicationId, request(null, null));
|
||||
runNext();
|
||||
|
||||
assertNull(runner.jobs.get(0).executors());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aMaximumCapacitysAmountIsTheNumberItStartsWith() {
|
||||
assertEquals(400, EmrServerlessJobRunService.capacityAmount("400 vCPU"));
|
||||
assertEquals(1024, EmrServerlessJobRunService.capacityAmount("1024GB"));
|
||||
assertEquals(0, EmrServerlessJobRunService.capacityAmount(null));
|
||||
assertEquals(0, EmrServerlessJobRunService.capacityAmount("GB"));
|
||||
assertEquals(Long.MAX_VALUE, EmrServerlessJobRunService.capacityAmount("9".repeat(30)));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aHiveRunGetsTheHiveDriverWorkerTypesCustomImageElseTheApplicationsElseTheReleases() {
|
||||
FakeRunner runner = new FakeRunner();
|
||||
@@ -1017,6 +1079,12 @@ class EmrServerlessJobRunServiceTest {
|
||||
null, objectMapper, 0, clock, runner, tasks::add);
|
||||
}
|
||||
|
||||
// A service whose Spark runs have up to executorContainers executors in containers of their own.
|
||||
private EmrServerlessJobRunService dockerService(SparkJobRunner runner, int executorContainers) {
|
||||
return new EmrServerlessJobRunService(store, applications, iamService, new RegionResolver(REGION, ACCOUNT_ID),
|
||||
null, objectMapper, 0, clock, runner, tasks::add, null, null, null, executorContainers);
|
||||
}
|
||||
|
||||
private void runNext() {
|
||||
tasks.remove(0).run();
|
||||
}
|
||||
|
||||
+245
-10
@@ -4,6 +4,7 @@ import com.github.dockerjava.api.DockerClient;
|
||||
import com.github.dockerjava.api.async.ResultCallback;
|
||||
import com.github.dockerjava.api.command.CopyArchiveToContainerCmd;
|
||||
import com.github.dockerjava.api.exception.InternalServerErrorException;
|
||||
import com.github.dockerjava.api.exception.NotFoundException;
|
||||
import com.github.dockerjava.api.model.Container;
|
||||
import com.github.dockerjava.api.model.Frame;
|
||||
import com.github.dockerjava.api.model.StreamType;
|
||||
@@ -11,6 +12,7 @@ import io.floci.aws.config.EmulatorConfig;
|
||||
import io.floci.aws.config.EmulatorConfig.EcsServiceConfig.ImagePullBehavior;
|
||||
import io.floci.aws.core.common.AwsException;
|
||||
import io.floci.aws.core.common.docker.ContainerBuilder;
|
||||
import io.floci.aws.core.common.docker.ContainerExecStubs;
|
||||
import io.floci.aws.core.common.docker.ContainerLifecycleManager;
|
||||
import io.floci.aws.core.common.docker.ContainerLogStreamer;
|
||||
import io.floci.aws.core.common.docker.ContainerStorageHelper;
|
||||
@@ -60,7 +62,10 @@ import static io.floci.aws.services.emrserverless.SparkJobRunner.Engine.SPARK;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotSame;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertSame;
|
||||
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.anyInt;
|
||||
@@ -68,6 +73,7 @@ import static org.mockito.ArgumentMatchers.anyMap;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.ArgumentMatchers.argThat;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.ArgumentMatchers.isNull;
|
||||
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
|
||||
import static org.mockito.Mockito.RETURNS_SELF;
|
||||
import static org.mockito.Mockito.atLeastOnce;
|
||||
@@ -366,6 +372,220 @@ class EmrServerlessSparkRunnerTest {
|
||||
verify(lifecycle, never()).connectToNetwork(anyString(), anyString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aSparkRunsExecutorsRunApartOnlyOnANetworkOfContainers() {
|
||||
SparkJobRunner.Job apart = executorsJob(Map.of());
|
||||
|
||||
assertTrue(EmrServerlessSparkRunner.executorsApart(apart, "floci-network"));
|
||||
assertTrue(EmrServerlessSparkRunner.executorsApart(apart, null));
|
||||
// On the host's network, or in another container's namespace, its ports would meet another run's.
|
||||
assertFalse(EmrServerlessSparkRunner.executorsApart(apart, "host"));
|
||||
assertFalse(EmrServerlessSparkRunner.executorsApart(apart, "container:floci"));
|
||||
// A job without them, a Hive run and a step keep their executors where they were.
|
||||
assertFalse(EmrServerlessSparkRunner.executorsApart(job(Duration.ZERO), "floci-network"));
|
||||
assertFalse(EmrServerlessSparkRunner.executorsApart(hiveJob(List.of()).withExecutors(EXECUTORS),
|
||||
"floci-network"));
|
||||
assertFalse(EmrServerlessSparkRunner.executorsApart(stepJob(Map.of()).withExecutors(EXECUTORS),
|
||||
"floci-network"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aRunWhoseExecutorsRunApartTakesItsMasterFromItsContainerAndHasItsUiOn() {
|
||||
List<String> arguments = EmrServerlessSparkRunner.sparkSubmitArguments(
|
||||
executorsJob(Map.of("spark.ui.enabled", "false")), ENDPOINT);
|
||||
|
||||
assertFalse(arguments.contains("--master"));
|
||||
// Floci's comes before the job's own, which wins.
|
||||
int on = arguments.indexOf("spark.ui.enabled=true");
|
||||
assertTrue(on > 0 && arguments.indexOf("spark.ui.enabled=false") > on);
|
||||
// Its kill links, which any container on the network reaches, the driver's container turns off.
|
||||
assertFalse(arguments.contains("spark.ui.killEnabled=false"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void whatTheDriversContainerForcesComesAfterTheRunsOwnOptionsAndBeforeItsPrimaryResource() {
|
||||
List<String> arguments = EmrServerlessSparkRunner.sparkSubmitArguments(job(null,
|
||||
List.of("--conf", "spark.ui.killEnabled=true", "--conf=spark.authenticate.secret=mine", "--master",
|
||||
"local[2]", "s3://bucket/jobs/app.jar", "--conf", "for-the-app"),
|
||||
Map.of("spark.ui.killEnabled", "true"), Duration.ZERO).withExecutors(EXECUTORS), ENDPOINT);
|
||||
|
||||
// The driver's container gives its master, secret and kill links after the options its count covers,
|
||||
// the run's own settings and options among them, and before the run's primary resource.
|
||||
int options = SparkExecutors.optionCount(arguments);
|
||||
assertEquals("s3://bucket/jobs/app.jar", arguments.get(options));
|
||||
assertTrue(arguments.subList(0, options).containsAll(List.of("spark.ui.killEnabled=true",
|
||||
"--conf=spark.authenticate.secret=mine", "--master", "local[2]")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void theDriverOfARunWhoseExecutorsRunApartStartsTheMasterWithTheRunsSecretThenFlociAsksItsUi() {
|
||||
ContainerBuilder builder = mock(ContainerBuilder.class);
|
||||
ContainerBuilder.Builder spec = mock(ContainerBuilder.Builder.class, RETURNS_SELF);
|
||||
when(builder.newContainer(anyString())).thenReturn(spec);
|
||||
EmrServerlessSparkRunner runner = runner(builder);
|
||||
List<List<String>> asked = ContainerExecStubs.completeEveryExec(docker, "driver", 7L, "", "");
|
||||
|
||||
runner.run(executorsJob(Map.of()), progress);
|
||||
|
||||
// spark-submit, then how many of its arguments are options, which the wrapper puts its own after.
|
||||
ArgumentCaptor<List<String>> cmd = ArgumentCaptor.captor();
|
||||
verify(spec).withCmd(cmd.capture());
|
||||
verify(spec).withEntrypoint(List.of("/bin/bash", "-c", SparkExecutors.DRIVER_WRAPPER, "floci",
|
||||
"/usr/lib/spark/bin/spark-submit", String.valueOf(SparkExecutors.optionCount(cmd.getValue()))));
|
||||
assertEquals("s3://bucket/jobs/app.jar", cmd.getValue().get(SparkExecutors.optionCount(cmd.getValue())));
|
||||
verify(spec).withEnv(eq(SparkExecutors.SECRET_VARIABLE),
|
||||
argThat((String secret) -> secret.matches("[0-9a-f]{64}")));
|
||||
assertFalse(cmd.getValue().contains("--master"));
|
||||
assertEquals(List.of("curl", "-sf", "-m", "2", "http://127.0.0.1:4040/api/v1/applications"),
|
||||
asked.getFirst());
|
||||
verify(lifecycle, never()).createAndStart(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aRunsExecutorsContainersGoWithItsDriver() {
|
||||
ContainerBuilder builder = mock(ContainerBuilder.class);
|
||||
ContainerBuilder.Builder spec = mock(ContainerBuilder.Builder.class, RETURNS_SELF);
|
||||
when(builder.newContainer(anyString())).thenReturn(spec);
|
||||
when(lifecycle.createAndStart(any())).thenReturn(new ContainerLifecycleManager.ContainerInfo("executor",
|
||||
Map.of()));
|
||||
EmrServerlessSparkRunner runner = runner(builder);
|
||||
// A driver whose settings leave dynamic allocation off, so its two executor instances start at once.
|
||||
ContainerExecStubs.completeEveryExec(docker, "driver", 0L, "[{\"id\": \"app-1\"}]", "");
|
||||
|
||||
assertEquals(0, runner.run(executorsJob(Map.of()), progress).exitCode());
|
||||
|
||||
verify(lifecycle, times(2)).createAndStart(any());
|
||||
verify(lifecycle, times(2)).stopAndRemove("executor", null, 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
void onTheHostsNetworkARunsExecutorsRunInItsDriversContainer() {
|
||||
ContainerBuilder builder = mock(ContainerBuilder.class);
|
||||
ContainerBuilder.Builder spec = mock(ContainerBuilder.Builder.class, RETURNS_SELF);
|
||||
when(builder.newContainer(anyString())).thenReturn(spec);
|
||||
when(builder.resolveDockerNetwork(Optional.of("floci-network"))).thenReturn(Optional.of("host"));
|
||||
|
||||
runner(builder).run(executorsJob(Map.of()), progress);
|
||||
|
||||
verify(spec).withEntrypoint(List.of("/usr/lib/spark/bin/spark-submit"));
|
||||
verify(spec).withCmd(argThat((List<String> cmd) -> List.of("--master", "local[*]").equals(cmd.subList(0, 2))));
|
||||
verify(spec, never()).withEnv(eq(SparkExecutors.SECRET_VARIABLE), anyString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void anExecutorsContainerIsAWorkerOnTheDriversNetworkLabelledAsTheRunsAndGoesWhenRemoved() {
|
||||
ContainerBuilder builder = mock(ContainerBuilder.class);
|
||||
ContainerBuilder.Builder spec = mock(ContainerBuilder.Builder.class, RETURNS_SELF);
|
||||
when(builder.newContainer(anyString())).thenReturn(spec);
|
||||
when(lifecycle.createAndStart(any())).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()),
|
||||
"sha256:image", "floci-network", "172.18.0.4", "secret", AWS_ENVIRONMENT,
|
||||
Map.of(ContainerStorageHelper.SERVICE_LABEL, "emr-serverless"));
|
||||
|
||||
SparkExecutors.Worker worker = containers.start(2, FOUR_CORE_EXECUTOR);
|
||||
containers.remove(worker.containerId());
|
||||
|
||||
assertEquals(new SparkExecutors.Worker("executor", "172.18.0.9"), worker);
|
||||
verify(builder).newContainer("sha256:image");
|
||||
verify(spec).withName(ContainerStorageHelper.dockerName(config, "emr-serverless-00f0000000000001-executor-2"));
|
||||
verify(spec).withEntrypoint(List.of("/bin/bash", "-c", SparkExecutors.WORKER_SCRIPT));
|
||||
// The driver's AWS environment, its master and secret, and the executor's cores and memory.
|
||||
verify(spec).withEnv(AWS_ENVIRONMENT);
|
||||
verify(spec).withEnv(SparkExecutors.SECRET_VARIABLE, "secret");
|
||||
verify(spec).withEnv(SparkExecutors.MASTER_VARIABLE, "172.18.0.4");
|
||||
verify(spec).withEnv(SparkExecutors.CORES_VARIABLE, "4");
|
||||
verify(spec).withEnv(SparkExecutors.MEMORY_VARIABLE, "14336");
|
||||
verify(spec).withDockerNetwork(Optional.of("floci-network"));
|
||||
verify(spec).withJavaTrustStore();
|
||||
// Found by a reset and the leftover sweep as the run's driver is.
|
||||
verify(spec).withLabels(argThat(labels -> "emr-serverless".equals(labels.get(ContainerStorageHelper.SERVICE_LABEL))
|
||||
&& SparkExecutors.COMPONENT.equals(labels.get(ContainerStorageHelper.COMPONENT_LABEL))));
|
||||
verify(lifecycle).stopAndRemove("executor", null, 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
void anExecutorsContainerWhoseAddressCannotBeFoundGoesBeforeItIsTheRunsToRemove() {
|
||||
ContainerBuilder builder = mock(ContainerBuilder.class);
|
||||
ContainerBuilder.Builder spec = mock(ContainerBuilder.Builder.class, RETURNS_SELF);
|
||||
when(builder.newContainer(anyString())).thenReturn(spec);
|
||||
when(lifecycle.createAndStart(any())).thenReturn(new ContainerLifecycleManager.ContainerInfo("executor",
|
||||
Map.of()));
|
||||
when(lifecycle.resolveContainerNetworkIp("executor", "floci-network"))
|
||||
.thenThrow(new IllegalStateException("no address"));
|
||||
SparkExecutors.Containers containers = runner(builder).executorContainers(executorsJob(Map.of()),
|
||||
"sha256:image", "floci-network", "172.18.0.4", "secret", AWS_ENVIRONMENT, Map.of());
|
||||
|
||||
assertThrows(IllegalStateException.class, () -> containers.start(1, FOUR_CORE_EXECUTOR));
|
||||
|
||||
verify(lifecycle).stopAndRemove("executor", null, 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
void aDriverThatCannotBeAskedAnswersNothing() {
|
||||
EmrServerlessSparkRunner runner = runner(mock(ContainerBuilder.class));
|
||||
when(docker.execCreateCmd("driver")).thenThrow(new NotFoundException("gone"));
|
||||
|
||||
assertNull(runner.driver("driver").get(SparkExecutors.DRIVER_UI_PORT, "/api/v1/applications"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void theRunsOfAnApplicationShareOneCapacityWhichGoesWithTheLast() {
|
||||
EmrServerlessSparkRunner runner = runner();
|
||||
SparkExecutors.Capacity first = runner.joinCapacity("arn:aws:emr-serverless:eu-west-1:123456789012:/app/a");
|
||||
SparkExecutors.Capacity second = runner.joinCapacity("arn:aws:emr-serverless:eu-west-1:123456789012:/app/a");
|
||||
|
||||
assertSame(first, second);
|
||||
assertNotSame(first, runner.joinCapacity("arn:aws:emr-serverless:eu-west-1:123456789012:/app/b"));
|
||||
// A run of no application shares it with none.
|
||||
assertNotSame(runner.joinCapacity(null), runner.joinCapacity(null));
|
||||
runner.leaveCapacity("arn:aws:emr-serverless:eu-west-1:123456789012:/app/a", first);
|
||||
assertSame(first, runner.joinCapacity("arn:aws:emr-serverless:eu-west-1:123456789012:/app/a"));
|
||||
runner.leaveCapacity("arn:aws:emr-serverless:eu-west-1:123456789012:/app/a", first);
|
||||
runner.leaveCapacity("arn:aws:emr-serverless:eu-west-1:123456789012:/app/a", first);
|
||||
assertNotSame(first, runner.joinCapacity("arn:aws:emr-serverless:eu-west-1:123456789012:/app/a"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void theReleasesImageForARunsExecutorsIsPulledWhenMissingBeforeTheDriverStarts() {
|
||||
ContainerBuilder builder = mock(ContainerBuilder.class);
|
||||
ContainerBuilder.Builder spec = mock(ContainerBuilder.Builder.class, RETURNS_SELF);
|
||||
when(builder.newContainer(anyString())).thenReturn(spec);
|
||||
EmrServerlessSparkRunner runner = runner(builder);
|
||||
ContainerExecStubs.completeEveryExec(docker, "driver", 7L, "", "");
|
||||
|
||||
runner.run(executorsJob(Map.of()), progress);
|
||||
|
||||
// Not while the run waits for its executors, where a pull would hold up a cancel and the run's timeout.
|
||||
InOrder order = inOrder(lifecycle);
|
||||
order.verify(lifecycle).resolveImageForLaunch("public.ecr.aws/emr-serverless/spark/emr-7.5.0:latest",
|
||||
ImagePullBehavior.PREFER_CACHED, null);
|
||||
order.verify(lifecycle).create(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void anApplicationsImageForItsExecutorsIsPulledBeforeTheDriverStarts() {
|
||||
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/executor:7.5.0";
|
||||
String inFlocisRegistry = "localhost:5100/123456789012/eu-west-1/spark/executor:7.5.0";
|
||||
when(builder.newContainer(anyString())).thenReturn(spec);
|
||||
when(builder.resolveImage(inFlocisRegistry)).thenReturn(inFlocisRegistry);
|
||||
when(ecr.rewriteImageUri(custom)).thenReturn(inFlocisRegistry);
|
||||
when(lifecycle.resolveImageForLaunch(inFlocisRegistry, ImagePullBehavior.DEFAULT, null))
|
||||
.thenReturn(new LaunchImage("sha256:" + "5".repeat(64), null));
|
||||
EmrServerlessSparkRunner runner = runner(builder);
|
||||
ContainerExecStubs.completeEveryExec(docker, "driver", 7L, "", "");
|
||||
|
||||
runner.run(executorsJob(Map.of()).withExecutors(new SparkJobRunner.Executors(2, custom, 0, 0, 0, null)),
|
||||
progress);
|
||||
|
||||
InOrder order = inOrder(lifecycle);
|
||||
order.verify(lifecycle).resolveImageForLaunch(inFlocisRegistry, ImagePullBehavior.DEFAULT, null);
|
||||
order.verify(lifecycle).create(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void anImageInAnEcrRepositoryRunsFromFlocisRegistryPulledForEveryRun() {
|
||||
ContainerBuilder builder = mock(ContainerBuilder.class);
|
||||
@@ -1755,6 +1975,9 @@ class EmrServerlessSparkRunnerTest {
|
||||
when(docker.inspectContainerCmd("driver").exec().getState().getRunning()).thenReturn(false);
|
||||
when(docker.inspectContainerCmd("driver").exec().getState().getExitCodeLong()).thenReturn(0L);
|
||||
when(awsEnv.flociEndpoint()).thenReturn(ENDPOINT);
|
||||
// An image pulled only when missing is there, by its own name.
|
||||
when(lifecycle.resolveImageForLaunch(anyString(), eq(ImagePullBehavior.PREFER_CACHED), isNull()))
|
||||
.thenAnswer(pull -> new LaunchImage(pull.getArgument(0), null));
|
||||
return new EmrServerlessSparkRunner(builder, lifecycle, logStreamer, awsEnv,
|
||||
mock(LambdaExecutionRoleCredentials.class), s3, ecr, config, new MutableClock(), Duration.ZERO);
|
||||
}
|
||||
@@ -1853,13 +2076,24 @@ class EmrServerlessSparkRunnerTest {
|
||||
return new SparkJobRunner.Job("emr-serverless", "00f0000000000001", "123456789012", "eu-west-1", "emr-7.5.0",
|
||||
SPARK, imageTemplate, null, "floci-network", "arn:aws:iam::123456789012:role/job", sparkDefaults,
|
||||
application, null, timeout, logFolder, cloudWatchLogs, Map.of(), List.of(), null, Map.of(), Map.of(),
|
||||
Map.of(), Map.of(), Map.of());
|
||||
Map.of(), Map.of(), Map.of(), null);
|
||||
}
|
||||
|
||||
private static final SparkJobRunner.Executors EXECUTORS = new SparkJobRunner.Executors(2, null, 0, 0, 0, null);
|
||||
// A 4-core 14G executor's size, whose worker and the driver's hold 4 vCPUs, 16 GB and 20 GB of disk.
|
||||
private static final SparkExecutors.Size FOUR_CORE_EXECUTOR = new SparkExecutors.Size(4, 14336,
|
||||
new SparkExecutors.Amount(4, 16, 20), new SparkExecutors.Amount(4, 16, 20));
|
||||
|
||||
// An EMR Serverless Spark run whose executors run in containers of their own.
|
||||
private static SparkJobRunner.Job executorsJob(Map<String, String> sparkDefaults) {
|
||||
return job(null, List.of("s3://bucket/jobs/app.jar"), sparkDefaults, Duration.ZERO).withExecutors(EXECUTORS);
|
||||
}
|
||||
|
||||
private static SparkJobRunner.Job hiveJob(List<String> application) {
|
||||
return new SparkJobRunner.Job("emr-serverless", "00f0000000000002", "123456789012", "eu-west-1", "emr-7.5.0",
|
||||
HIVE, null, null, "floci-network", "arn:aws:iam::123456789012:role/job", Map.of(), application, null,
|
||||
Duration.ZERO, null, null, Map.of(), List.of(), null, Map.of(), Map.of(), Map.of(), Map.of(), Map.of());
|
||||
Duration.ZERO, null, null, Map.of(), List.of(), null, Map.of(), Map.of(), Map.of(), Map.of(), Map.of(),
|
||||
null);
|
||||
}
|
||||
|
||||
// An EMR Hive step: hive-script's --args, then application, on cluster j-1's storage.
|
||||
@@ -1869,21 +2103,21 @@ class EmrServerlessSparkRunnerTest {
|
||||
List.of("hive-script", "--run-hive-script", "--args"), Duration.ZERO, null, null, Map.of(), List.of(),
|
||||
new SparkJobRunner.Storage("j-1-home", "j-1-mnt").withHdfs("ip-10-0-0-1.ec2.internal", "172.18.0.5",
|
||||
"j-1-hdfs").withHiveMetastore("thrift://ip-10-0-0-1.ec2.internal:9083"),
|
||||
Map.of(), Map.of(), Map.of(), Map.of(), Map.of());
|
||||
Map.of(), Map.of(), Map.of(), Map.of(), Map.of(), null);
|
||||
}
|
||||
|
||||
private static SparkJobRunner.Job customImageJob(String customImage) {
|
||||
return new SparkJobRunner.Job("emr-serverless", "00f0000000000001", "123456789012", "eu-west-1", "emr-7.5.0",
|
||||
SPARK, null, customImage, "floci-network", "arn:aws:iam::123456789012:role/job", Map.of(),
|
||||
List.of("s3://bucket/jobs/app.jar"), null, Duration.ZERO, null, null, Map.of(), List.of(), null,
|
||||
Map.of(), Map.of(), Map.of(), Map.of(), Map.of());
|
||||
Map.of(), Map.of(), Map.of(), Map.of(), Map.of(), null);
|
||||
}
|
||||
|
||||
private static SparkJobRunner.Job archiveJob(List<String> archives) {
|
||||
return new SparkJobRunner.Job("emr-serverless", "00f0000000000001", "123456789012", "eu-west-1", "emr-7.5.0",
|
||||
SPARK, null, null, "floci-network", "arn:aws:iam::123456789012:role/job", Map.of(),
|
||||
List.of("s3://bucket/jobs/app.jar"), null, Duration.ZERO, null, null, Map.of(), archives, null,
|
||||
Map.of(), Map.of(), Map.of(), Map.of(), Map.of());
|
||||
Map.of(), Map.of(), Map.of(), Map.of(), Map.of(), null);
|
||||
}
|
||||
|
||||
private static SparkJobRunner.Job commandJob(List<String> command, Map<String, String> sparkDefaults) {
|
||||
@@ -1895,7 +2129,7 @@ class EmrServerlessSparkRunnerTest {
|
||||
return new SparkJobRunner.Job("emr", "s-1A2B3C4D5E6F7", "123456789012", "eu-west-1", "emr-7.5.0", SPARK, null,
|
||||
null, "floci-network", "arn:aws:iam::123456789012:role/job", sparkDefaults, List.of(), command,
|
||||
Duration.ZERO, null, null, Map.of("PYSPARK_PYTHON", "/usr/bin/python3.11"), List.of(),
|
||||
storage, Map.of(), Map.of(), Map.of(), Map.of(), Map.of());
|
||||
storage, Map.of(), Map.of(), Map.of(), Map.of(), Map.of(), null);
|
||||
}
|
||||
|
||||
// A step on a cluster with core-site and spark-log4j2 properties: a command, or a Spark step for a null one.
|
||||
@@ -1904,7 +2138,7 @@ class EmrServerlessSparkRunnerTest {
|
||||
return new SparkJobRunner.Job("emr", "s-1A2B3C4D5E6F7", "123456789012", "eu-west-1", "emr-7.5.0", SPARK, null,
|
||||
null, "floci-network", "arn:aws:iam::123456789012:role/job", Map.of(),
|
||||
command == null ? List.of("s3://bucket/jobs/report.py") : List.of(), command, Duration.ZERO, null,
|
||||
null, Map.of(), List.of(), STORAGE, coreSite, sparkLog4j2, Map.of(), Map.of(), Map.of());
|
||||
null, Map.of(), List.of(), STORAGE, coreSite, sparkLog4j2, Map.of(), Map.of(), Map.of(), null);
|
||||
}
|
||||
|
||||
// A step on a cluster with mapred-site, hadoop-log4j and hadoop-env: a command, or a custom JAR for a null one.
|
||||
@@ -1914,7 +2148,8 @@ class EmrServerlessSparkRunnerTest {
|
||||
return new SparkJobRunner.Job("emr", "s-1A2B3C4D5E6F7", "123456789012", "eu-west-1", "emr-7.5.0", SPARK, null,
|
||||
null, "floci-network", "arn:aws:iam::123456789012:role/job", Map.of(),
|
||||
command == null ? List.of("s3://bucket/jobs/report.jar") : List.of(), command, Duration.ZERO, null,
|
||||
null, Map.of(), List.of(), STORAGE, Map.of(), Map.of(), mapredSite, hadoopLog4j, hadoopEnvironment);
|
||||
null, Map.of(), List.of(), STORAGE, Map.of(), Map.of(), mapredSite, hadoopLog4j, hadoopEnvironment,
|
||||
null);
|
||||
}
|
||||
|
||||
// A Spark step on a cluster, which has storage.
|
||||
@@ -1923,13 +2158,13 @@ class EmrServerlessSparkRunnerTest {
|
||||
job.imageTemplate(), job.customImage(), job.dockerNetwork(), job.executionRole(), job.sparkDefaults(),
|
||||
job.application(), job.command(), job.timeout(), job.logFolder(), job.cloudWatchLogs(),
|
||||
job.environment(), job.archives(), job.storage(), job.coreSite(), job.sparkLog4j2(), job.mapredSite(),
|
||||
job.hadoopLog4j(), job.hadoopEnvironment());
|
||||
job.hadoopLog4j(), job.hadoopEnvironment(), job.executors());
|
||||
}
|
||||
|
||||
private static SparkJobRunner.Job stepJob(Map<String, String> sparkDefaults) {
|
||||
return new SparkJobRunner.Job("emr", "s-1A2B3C4D5E6F7", "123456789012", "eu-west-1", "emr-7.5.0", SPARK, null,
|
||||
null, "floci-network", "arn:aws:iam::123456789012:role/job", sparkDefaults,
|
||||
List.of("s3://bucket/jobs/report.py"), null, Duration.ZERO, null, null, Map.of(), List.of(), STORAGE,
|
||||
Map.of(), Map.of(), Map.of(), Map.of(), Map.of());
|
||||
Map.of(), Map.of(), Map.of(), Map.of(), Map.of(), null);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,420 @@
|
||||
package io.floci.aws.services.emrserverless;
|
||||
|
||||
import io.floci.aws.services.emrserverless.SparkExecutors.Amount;
|
||||
import io.floci.aws.services.emrserverless.SparkExecutors.Capacity;
|
||||
import io.floci.aws.services.emrserverless.SparkExecutors.Size;
|
||||
import io.floci.aws.services.emrserverless.SparkExecutors.Worker;
|
||||
import io.floci.aws.services.emrserverless.SparkJobRunner.Executors;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
/** An EMR Serverless Spark run's executors in containers of their own, as its driver wants them. */
|
||||
class SparkExecutorsTest {
|
||||
|
||||
// The image's own spark-defaults: 4 cores and 14G for the driver and each executor, dynamic allocation on.
|
||||
private static final Map<String, String> IMAGE_DEFAULTS = Map.of(
|
||||
"spark.executor.cores", "4", "spark.executor.memory", "14G",
|
||||
"spark.driver.cores", "4", "spark.driver.memory", "14G",
|
||||
"spark.dynamicAllocation.enabled", "true", "spark.executor.instances", "3");
|
||||
private static final Executors TWO = new Executors(2, null, 0, 0, 0, null);
|
||||
// The worker of a 4-core 14G executor, or driver: 14 GB and its 10% overhead rounded up, and a 20 GB disk.
|
||||
private static final Amount FOUR_CORES = new Amount(4, 16, 20);
|
||||
|
||||
// Real time, as the driver is looked at by it.
|
||||
private final AtomicLong nanos = new AtomicLong(-5_000_000_000L);
|
||||
private final FakeDriver driver = new FakeDriver();
|
||||
private final FakeContainers containers = new FakeContainers();
|
||||
private final Capacity capacity = new Capacity();
|
||||
|
||||
@Test
|
||||
void aWorkerOffersItsExecutorsCoresAndHeapAndHoldsItsMemoryWithOverheadAndItsDisk() {
|
||||
Size size = SparkExecutors.sizeOf(IMAGE_DEFAULTS);
|
||||
|
||||
assertEquals(4, size.cores());
|
||||
assertEquals(14 * 1024, size.memoryMib());
|
||||
// 14 GB and 10% of it, rounded up to a whole GB: the 16 GB worker the user guide pairs with 4 vCPUs.
|
||||
assertEquals(FOUR_CORES, size.executor());
|
||||
assertEquals(FOUR_CORES, size.driver());
|
||||
// The overhead is at least 384 MB, or what the job sets; a bare number is MiB; a disk as the job sets it.
|
||||
Size small = SparkExecutors.sizeOf(Map.of("spark.executor.cores", "1", "spark.executor.memory", "2048",
|
||||
"spark.emr-serverless.executor.disk", "50G"));
|
||||
assertEquals(1, small.cores());
|
||||
assertEquals(2048, small.memoryMib());
|
||||
assertEquals(new Amount(1, 3, 50), small.executor());
|
||||
assertEquals(2, SparkExecutors.sizeOf(Map.of("spark.executor.memory", "1g",
|
||||
"spark.executor.memoryOverhead", "1g")).executor().memoryGb());
|
||||
// Spark's own defaults when the job sets nothing: one core, 1 GB.
|
||||
Size spark = SparkExecutors.sizeOf(Map.of());
|
||||
assertEquals(1, spark.cores());
|
||||
assertEquals(1024, spark.memoryMib());
|
||||
assertEquals(1, spark.driver().vcpus());
|
||||
}
|
||||
|
||||
@Test
|
||||
void emrServerlesssOverheadFactorCountsUnlessTheRolesOwnIsSet() {
|
||||
// "spark.emr-serverless.memoryOverheadFactor | Sets the memory overhead to add to the driver and executor
|
||||
// container memory." 14 GB and half of it are 21 GB.
|
||||
Map<String, String> settings = new HashMap<>(IMAGE_DEFAULTS);
|
||||
settings.put("spark.emr-serverless.memoryOverheadFactor", "0.5");
|
||||
assertEquals(21, SparkExecutors.sizeOf(settings).executor().memoryGb());
|
||||
assertEquals(21, SparkExecutors.sizeOf(settings).driver().memoryGb());
|
||||
|
||||
settings.put("spark.executor.memoryOverheadFactor", "0.25");
|
||||
settings.put("spark.driver.memoryOverhead", "2g");
|
||||
assertEquals(18, SparkExecutors.sizeOf(settings).executor().memoryGb());
|
||||
assertEquals(16, SparkExecutors.sizeOf(settings).driver().memoryGb());
|
||||
}
|
||||
|
||||
@Test
|
||||
void anApplicationsCapacityTakesAWorkerOnlyWhileItsLimitsHaveRoomForIt() {
|
||||
// Beside the driver's 4 vCPUs, 16 GB and 20 GB of disk.
|
||||
assertEquals(2, taken(new Executors(9, null, 12, 0, 0, null)));
|
||||
assertEquals(1, taken(new Executors(9, null, 0, 40, 0, null)));
|
||||
assertEquals(3, taken(new Executors(9, null, 0, 0, 80, null)));
|
||||
assertEquals(0, taken(new Executors(9, null, 2, 0, 0, null)));
|
||||
assertEquals(9, taken(new Executors(9, null, 0, 0, 0, null)));
|
||||
}
|
||||
|
||||
@Test
|
||||
void theRunsOfAnApplicationShareItsMaximumCapacity() {
|
||||
// Its capacity "is cumulative across all workers at any given point in time": 12 vCPUs, for two runs whose
|
||||
// drivers and executors take 4 each.
|
||||
Executors twelve = new Executors(9, null, 12, 0, 0, "application");
|
||||
driver.settings(IMAGE_DEFAULTS);
|
||||
driver.target(3);
|
||||
SparkExecutors first = new SparkExecutors(twelve, driver, containers, capacity, nanos::get);
|
||||
first.follow();
|
||||
FakeContainers others = new FakeContainers();
|
||||
SparkExecutors second = new SparkExecutors(twelve, driver, others, capacity, nanos::get);
|
||||
second.follow();
|
||||
|
||||
assertEquals(List.of(1, 2), containers.started);
|
||||
// Its driver runs already, so it is held, beyond the capacity, and none of its executors starts.
|
||||
assertEquals(List.of(), others.started);
|
||||
assertEquals(new Amount(16, 64, 80), capacity.held());
|
||||
|
||||
// Once the first run has ended, the second has room for two.
|
||||
first.removeAll();
|
||||
advance(SparkExecutors.LOOK_INTERVAL);
|
||||
second.follow();
|
||||
assertEquals(List.of(1, 2), others.started);
|
||||
assertEquals(new Amount(12, 48, 60), capacity.held());
|
||||
second.removeAll();
|
||||
assertEquals(Amount.NONE, capacity.held());
|
||||
}
|
||||
|
||||
@Test
|
||||
void nothingStartsUntilTheDriversUiAnswers() {
|
||||
SparkExecutors executors = new SparkExecutors(TWO, driver, containers, capacity, nanos::get);
|
||||
|
||||
executors.follow();
|
||||
|
||||
assertEquals(List.of(), containers.started);
|
||||
}
|
||||
|
||||
@Test
|
||||
void asManyExecutorsStartAsDynamicAllocationWantsUpToTheRunsLimit() {
|
||||
driver.settings(IMAGE_DEFAULTS);
|
||||
driver.target(3);
|
||||
SparkExecutors executors = new SparkExecutors(TWO, driver, containers, capacity, nanos::get);
|
||||
|
||||
executors.follow();
|
||||
|
||||
assertEquals(List.of(1, 2), containers.started);
|
||||
assertEquals(new Size(4, 14 * 1024, FOUR_CORES, FOUR_CORES), containers.sizes.getFirst());
|
||||
// Still wanted, none more.
|
||||
advance(SparkExecutors.LOOK_INTERVAL);
|
||||
executors.follow();
|
||||
assertEquals(List.of(1, 2), containers.started);
|
||||
assertEquals(2, executors.workers().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void noExecutorStartsThatWouldTakeTheApplicationPastItsMaximumCapacity() {
|
||||
driver.settings(IMAGE_DEFAULTS);
|
||||
driver.target(5);
|
||||
SparkExecutors executors = new SparkExecutors(new Executors(9, null, 12, 0, 0, null), driver,
|
||||
containers, capacity, nanos::get);
|
||||
|
||||
executors.follow();
|
||||
|
||||
// 4 vCPUs for the driver's worker and 4 for each executor's, of 12.
|
||||
assertEquals(List.of(1, 2), containers.started);
|
||||
}
|
||||
|
||||
@Test
|
||||
void anIdleExecutorAboveWhatSparkWantsGoesAndABusyOneStays() {
|
||||
driver.settings(IMAGE_DEFAULTS);
|
||||
driver.target(2);
|
||||
SparkExecutors executors = new SparkExecutors(TWO, driver, containers, capacity, nanos::get);
|
||||
executors.follow();
|
||||
|
||||
driver.target(1);
|
||||
driver.coresUsed(Map.of("10.0.0.1", 4, "10.0.0.2", 0));
|
||||
advance(SparkExecutors.LOOK_INTERVAL);
|
||||
executors.follow();
|
||||
|
||||
assertEquals(List.of("executor-2"), containers.removed);
|
||||
assertEquals(List.of(new Worker("executor-1", "10.0.0.1")), executors.workers());
|
||||
// Its worker's share of the capacity goes with it: the driver's and one executor's are left.
|
||||
assertEquals(new Amount(8, 32, 40), capacity.held());
|
||||
// Busy, it stays above the target until it is idle.
|
||||
driver.target(0);
|
||||
advance(SparkExecutors.LOOK_INTERVAL);
|
||||
executors.follow();
|
||||
assertEquals(List.of("executor-2"), containers.removed);
|
||||
}
|
||||
|
||||
@Test
|
||||
void anExecutorWhoseContainerEndedIsStartedAgainWhileSparkStillWantsIt() {
|
||||
driver.settings(IMAGE_DEFAULTS);
|
||||
driver.target(2);
|
||||
SparkExecutors executors = new SparkExecutors(TWO, driver, containers, capacity, nanos::get);
|
||||
executors.follow();
|
||||
|
||||
containers.ended.add("executor-1");
|
||||
advance(SparkExecutors.LOOK_INTERVAL);
|
||||
executors.follow();
|
||||
|
||||
assertEquals(List.of(1, 2, 3), containers.started);
|
||||
assertEquals(List.of("executor-2", "executor-3"),
|
||||
executors.workers().stream().map(Worker::containerId).toList());
|
||||
// The container that ended goes from the daemon too, and its worker's share of the capacity with it.
|
||||
assertEquals(List.of("executor-1"), containers.removed);
|
||||
assertEquals(new Amount(12, 48, 60), capacity.held());
|
||||
}
|
||||
|
||||
@Test
|
||||
void anEndedExecutorsContainerDockerWillNotRemoveYetIsRemovedWithTheRun() {
|
||||
driver.settings(IMAGE_DEFAULTS);
|
||||
driver.target(1);
|
||||
SparkExecutors executors = new SparkExecutors(TWO, driver, containers, capacity, nanos::get);
|
||||
executors.follow();
|
||||
containers.ended.add("executor-1");
|
||||
containers.refused.add("executor-1");
|
||||
advance(SparkExecutors.LOOK_INTERVAL);
|
||||
executors.follow();
|
||||
|
||||
containers.refused.clear();
|
||||
executors.removeAll();
|
||||
|
||||
assertEquals(List.of("executor-2", "executor-1"), containers.removed);
|
||||
}
|
||||
|
||||
@Test
|
||||
void anExecutorWhoseContainerCouldNotStartGivesItsShareBackAndIsStartedAtTheNextLook() {
|
||||
driver.settings(IMAGE_DEFAULTS);
|
||||
driver.target(1);
|
||||
containers.failNextStart = true;
|
||||
SparkExecutors executors = new SparkExecutors(TWO, driver, containers, capacity, nanos::get);
|
||||
|
||||
executors.follow();
|
||||
assertTrue(executors.workers().isEmpty());
|
||||
assertEquals(FOUR_CORES, capacity.held());
|
||||
|
||||
advance(SparkExecutors.LOOK_INTERVAL);
|
||||
executors.follow();
|
||||
assertEquals(1, executors.workers().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void withoutDynamicAllocationItsExecutorInstancesStart() {
|
||||
Map<String, String> fixed = new HashMap<>(IMAGE_DEFAULTS);
|
||||
fixed.put("spark.dynamicAllocation.enabled", "false");
|
||||
fixed.put("spark.executor.instances", "1");
|
||||
driver.settings(fixed);
|
||||
SparkExecutors executors = new SparkExecutors(TWO, driver, containers, capacity, nanos::get);
|
||||
|
||||
executors.follow();
|
||||
|
||||
assertEquals(List.of(1), containers.started);
|
||||
}
|
||||
|
||||
@Test
|
||||
void aDriverWhoseUiNeverAnswersGetsOneExecutorThatFitsAnySettings() {
|
||||
driver.masterHasApplication = true;
|
||||
// Its sizes unknown, the one executor counts for nothing against a capacity smaller than what it offers.
|
||||
SparkExecutors executors = new SparkExecutors(new Executors(2, null, 4, 8, 20, null), driver, containers,
|
||||
capacity, nanos::get);
|
||||
executors.follow();
|
||||
advance(SparkExecutors.UI_SILENCE.minus(Duration.ofSeconds(1)));
|
||||
executors.follow();
|
||||
assertEquals(List.of(), containers.started);
|
||||
|
||||
advance(SparkExecutors.LOOK_INTERVAL);
|
||||
executors.follow();
|
||||
|
||||
assertEquals(List.of(1), containers.started);
|
||||
assertEquals(SparkExecutors.BLIND_SIZE, containers.sizes.getFirst());
|
||||
assertEquals(Amount.NONE, capacity.held());
|
||||
}
|
||||
|
||||
@Test
|
||||
void theDriverIsLookedAtOnceALookInterval() {
|
||||
driver.settings(IMAGE_DEFAULTS);
|
||||
driver.target(1);
|
||||
SparkExecutors executors = new SparkExecutors(TWO, driver, containers, capacity, nanos::get);
|
||||
executors.follow();
|
||||
int asked = driver.asked;
|
||||
|
||||
executors.follow();
|
||||
|
||||
assertEquals(asked, driver.asked);
|
||||
}
|
||||
|
||||
@Test
|
||||
void everyExecutorGoesWithTheRun() {
|
||||
driver.settings(IMAGE_DEFAULTS);
|
||||
driver.target(2);
|
||||
SparkExecutors executors = new SparkExecutors(TWO, driver, containers, capacity, nanos::get);
|
||||
executors.follow();
|
||||
|
||||
executors.removeAll();
|
||||
|
||||
assertEquals(List.of("executor-1", "executor-2"), containers.removed);
|
||||
assertTrue(executors.workers().isEmpty());
|
||||
assertEquals(Amount.NONE, capacity.held());
|
||||
}
|
||||
|
||||
@Test
|
||||
void sizesReadAsSparkReadsThem() {
|
||||
assertEquals(14 * 1024, SparkExecutors.mebibytes("14G", 0));
|
||||
assertEquals(14 * 1024, SparkExecutors.mebibytes("14gb", 0));
|
||||
assertEquals(512, SparkExecutors.mebibytes("512m", 0));
|
||||
assertEquals(2048, SparkExecutors.mebibytes("2048", 0));
|
||||
assertEquals(7, SparkExecutors.mebibytes("not a size", 7));
|
||||
assertEquals(20, SparkExecutors.gigabytes(null));
|
||||
assertEquals(200, SparkExecutors.gigabytes("200G"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void sparkSubmitsOptionsEndAtItsPrimaryResource() {
|
||||
assertEquals(4, SparkExecutors.optionCount(List.of("--conf", "a=1", "--verbose", "--conf=b=2",
|
||||
"s3://bucket/app.py", "--conf", "c=3")));
|
||||
assertEquals(6, SparkExecutors.optionCount(List.of("-c", "a=1", "--name", "-x", "--supervise", "-v",
|
||||
"app.jar")));
|
||||
assertEquals(0, SparkExecutors.optionCount(List.of("app.jar", "--conf", "a=1")));
|
||||
assertEquals(2, SparkExecutors.optionCount(List.of("--conf", "a=1")));
|
||||
assertEquals(1, SparkExecutors.optionCount(List.of("--conf")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void theDriversContainerStartsTheMasterBeforeSparkSubmitAndAWorkerOffersOneExecutor() {
|
||||
// The master's address, the run's secret and the UI's kill links off come after the run's own options, which
|
||||
// cannot replace them, and before its primary resource.
|
||||
assertTrue(SparkExecutors.DRIVER_WRAPPER.contains("submit=$1; options=$2; shift 2\nexec \"$submit\""
|
||||
+ " \"${@:1:$options}\" --master \"spark://$address:7077\" --conf \"spark.driver.host=$address\""
|
||||
+ " --conf \"spark.authenticate.secret=$FLOCI_SPARK_SECRET\" --conf spark.ui.killEnabled=false"
|
||||
+ " \"${@:$((options + 1))}\""));
|
||||
assertTrue(SparkExecutors.DRIVER_WRAPPER.contains("SPARK_MASTER_OPTS=\"-Dspark.authenticate.secret="
|
||||
+ "$FLOCI_SPARK_SECRET -Dspark.master.rest.enabled=false -Dspark.ui.killEnabled=false\""));
|
||||
assertTrue(SparkExecutors.WORKER_SCRIPT.contains("--cores \"$FLOCI_EXECUTOR_CORES\" --memory"
|
||||
+ " \"${FLOCI_EXECUTOR_MEMORY_MB}m\" --work-dir /tmp/floci-spark-work --host \"$address\""
|
||||
+ " \"spark://$FLOCI_SPARK_MASTER:7077\""));
|
||||
}
|
||||
|
||||
private void advance(Duration duration) {
|
||||
nanos.addAndGet(duration.toNanos());
|
||||
}
|
||||
|
||||
// How many 4-core 14G executors' workers a capacity holding such a driver's takes, of nine asked for.
|
||||
private static int taken(Executors limits) {
|
||||
Capacity capacity = new Capacity();
|
||||
capacity.hold(FOUR_CORES);
|
||||
int taken = 0;
|
||||
while (taken < 9 && capacity.take(FOUR_CORES, limits)) {
|
||||
taken++;
|
||||
}
|
||||
return taken;
|
||||
}
|
||||
|
||||
/** A driver whose UI and master answer as a test sets them. */
|
||||
private static final class FakeDriver implements SparkExecutors.Driver {
|
||||
private String environment;
|
||||
private String metrics;
|
||||
private String master = "{\"workers\": [], \"activeapps\": []}";
|
||||
private boolean masterHasApplication;
|
||||
private int asked;
|
||||
|
||||
void settings(Map<String, String> settings) {
|
||||
StringBuilder pairs = new StringBuilder();
|
||||
settings.forEach((key, value) -> pairs.append(pairs.isEmpty() ? "" : ",")
|
||||
.append("[\"").append(key).append("\",\"").append(value).append("\"]"));
|
||||
environment = "{\"sparkProperties\": [" + pairs + "]}";
|
||||
}
|
||||
|
||||
void target(int executors) {
|
||||
metrics = "{\"gauges\": {\"app-1.driver.ExecutorAllocationManager.executors.numberTargetExecutors\":"
|
||||
+ " {\"value\": " + executors + "}}}";
|
||||
}
|
||||
|
||||
void coresUsed(Map<String, Integer> byHost) {
|
||||
StringBuilder workers = new StringBuilder();
|
||||
byHost.forEach((host, cores) -> workers.append(workers.isEmpty() ? "" : ",")
|
||||
.append("{\"host\": \"").append(host).append("\", \"coresused\": ").append(cores).append("}"));
|
||||
master = "{\"workers\": [" + workers + "], \"activeapps\": [{\"id\": \"app-1\"}]}";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String get(int port, String path) {
|
||||
asked++;
|
||||
if (port == SparkExecutors.MASTER_UI_PORT) {
|
||||
return masterHasApplication ? "{\"workers\": [], \"activeapps\": [{\"id\": \"app-1\"}]}" : master;
|
||||
}
|
||||
if (environment == null) {
|
||||
return null;
|
||||
}
|
||||
return switch (path) {
|
||||
case "/api/v1/applications" -> "[{\"id\": \"app-1\"}]";
|
||||
case "/api/v1/applications/app-1/environment" -> environment;
|
||||
case "/metrics/json/" -> metrics;
|
||||
default -> null;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/** Executors' containers, each at 10.0.0.n for the nth, which run until a test ends them. */
|
||||
private static final class FakeContainers implements SparkExecutors.Containers {
|
||||
private final List<Integer> started = new ArrayList<>();
|
||||
private final List<Size> sizes = new ArrayList<>();
|
||||
private final List<String> removed = new ArrayList<>();
|
||||
private final Set<String> ended = new HashSet<>();
|
||||
private final Set<String> refused = new HashSet<>();
|
||||
private boolean failNextStart;
|
||||
|
||||
@Override
|
||||
public Worker start(int index, Size size) {
|
||||
if (failNextStart) {
|
||||
failNextStart = false;
|
||||
throw new IllegalStateException("Docker refused to start it");
|
||||
}
|
||||
started.add(index);
|
||||
sizes.add(size);
|
||||
return new Worker("executor-" + index, "10.0.0." + index);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean running(String containerId) {
|
||||
return !ended.contains(containerId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove(String containerId) {
|
||||
if (refused.contains(containerId)) {
|
||||
throw new IllegalStateException("Docker refused to remove it");
|
||||
}
|
||||
removed.add(containerId);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -19,15 +19,16 @@ import java.util.Map;
|
||||
* program the link-local IMDS endpoint into a cluster's container, for the node and for its pods.
|
||||
* {@code EksImdsDockerIntegrationTest} relies on them.</li>
|
||||
* <li>{@code floci.services.emr.runner-mode=docker} and {@code floci.services.emrserverless.runner-mode=docker}
|
||||
* run a cluster's steps and an EMR Serverless application's job runs in containers of the release's image.
|
||||
* {@code EmrDockerRunnerDockerIntegrationTest} relies on them.</li>
|
||||
* run a cluster's steps and an EMR Serverless application's job runs in containers of the release's image, and
|
||||
* {@code floci.services.emrserverless.executor-containers=2} a Spark job run's executors each in a container of
|
||||
* its own. {@code EmrDockerRunnerDockerIntegrationTest} relies on them.</li>
|
||||
* </ul>
|
||||
*
|
||||
* <p>Each override is inert for the other classes. An InfluxDB container only starts when a DB
|
||||
* instance is created, which the IMDS test never does. The IMDS settings only act when a cluster's
|
||||
* container starts, and the test defaults keep EKS mocked, so only {@code EksImdsDockerIntegrationTest},
|
||||
* which configures the endpoint on its own container, reaches them. The metadata server it starts
|
||||
* stays up for the Timestream test, on a port that test does not use. The EMR runner modes only act
|
||||
* stays up for the Timestream test, on a port that test does not use. The EMR settings only act
|
||||
* when a cluster runs a step or an application runs a job, which neither of the other classes does.
|
||||
*
|
||||
* <p>The ELBv2 data-plane classes stay on {@link RealElbV2DataPlaneProfile}: one of them,
|
||||
@@ -45,6 +46,7 @@ public class ContainerDataPlaneProfile implements QuarkusTestProfile {
|
||||
"floci.services.eks.imds", "true",
|
||||
"floci.services.eks.imds-pod-network", "true",
|
||||
"floci.services.emr.runner-mode", "docker",
|
||||
"floci.services.emrserverless.runner-mode", "docker");
|
||||
"floci.services.emrserverless.runner-mode", "docker",
|
||||
"floci.services.emrserverless.executor-containers", "2");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user