feat(emr): run a cluster's core and task nodes in containers of their own (#8066)

A docker cluster's YARN and HDFS ran on its primary node alone, however
many instances the cluster named. On EMR "Core nodes run the Data Node
daemon", "a core node runs YARN NodeManager daemons, Hadoop MapReduce
tasks, and Spark executors", and "Task nodes don't run the Data Node
daemon".

- Each core and task node is a container of its own on the cluster's
  network, with its data in a volume of its own, its name there the
  private DNS name ListInstances gives it. The primary node keeps a
  DataNode and a NodeManager only on a cluster created with no core
  node, which keeps them as core nodes are added, and a primary node
  whose volume holds a DataNode's data runs it in any case, so the
  blocks on it stay in reach.
- ListInstances gives each group's instances addresses of their own, so
  an instance keeps its address while another group resizes.
- How many follows the instance groups' counts or the instance fleets'
  target capacities, at most yarn-max-worker-nodes, core nodes first. A
  fleet's unit of capacity is one node. With 0 a cluster runs on its
  primary node alone.
- HDFS keeps what EMR's does: one copy of a block below four core nodes,
  two below ten, three otherwise, or the hdfs-site classification's
  dfs.replication. Steps' Spark, Hive and hadoop clients write that many.
- A resize reaches the nodes of a cluster a step has started them on, on
  another thread: a new node takes work once it has registered, and one
  no longer wanted is decommissioned, HDFS and YARN telling when it may
  go, within EMR's default hour, each node its own, then goes with its
  data. Core nodes stop at the copies HDFS keeps.
- A core node's NodeManager is decommissioned only once HDFS has moved
  its blocks, as a decommissioned NodeManager exits, which ends the
  node's container, its DataNode with it. A core node that has gone
  stays among HDFS's excluded nodes until the NameNode has it dead, 40s
  after it stopped, so the copies it held never count again.
- A core node that stops before HDFS has moved its blocks starts again
  on its data, and one a shutdown cut off while it was decommissioned
  starts again with the cluster's next step, from its volume, which
  carries its name, to be decommissioned then. A task node's volume a
  shutdown left goes.
- A decommissioning round that fails, a refresh of HDFS's or YARN's
  lists among them, is tried again on the next; a node Docker will not
  remove yet stays to be removed then, and one whose start a teardown
  passed goes. A cluster's end removes the volumes of nodes that have no
  container, and finishes even when Docker cannot list its nodes.
- The node list is read inside the cluster's lock, so a step queued
  before a resize starts the nodes asked for by then.
- Container logs come from every node, a decommissioned one's first.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-11-12 13:59:34 -04:00
1 parent ef5e1cf010
commit 7ebed877a8
21 files changed
+2148 -111

No files matched your search

@@ -121,6 +121,29 @@ class ContainerLifecycleManagerCpuLimitTest {
assertEquals(16_000_000_000L, hostConfig.getNanoCPUs());
}
@Test
void aDnsNameIsTheContainersHostNameAndItsAliasOnItsNetwork() {
EmulatorConfig config = mock(EmulatorConfig.class, RETURNS_DEEP_STUBS);
when(config.docker().resourceNamespace()).thenReturn(Optional.empty());
ContainerSpec spec = new ContainerBuilder(config, mock(DockerHostResolver.class), mock(EmbeddedDnsServer.class))
.newContainer("app:latest").withDnsName("ip-10-1-0-1.ec2.internal").build();
DockerClient dockerClient = mock(DockerClient.class);
stubDaemonCpuCount(dockerClient, EIGHT_CPU_HOST);
CreateContainerCmd createCmd = mock(CreateContainerCmd.class, RETURNS_SELF);
when(dockerClient.createContainerCmd("app:latest")).thenReturn(createCmd);
CreateContainerResponse response = mock(CreateContainerResponse.class);
when(response.getId()).thenReturn("app-id");
when(createCmd.exec()).thenReturn(response);
ImageCacheService imageCacheService = mock(ImageCacheService.class);
when(imageCacheService.ensureImageExists(any())).thenAnswer(invocation -> invocation.getArgument(0));
new ContainerLifecycleManager(dockerClient, imageCacheService, mock(ContainerDetector.class), config)
.create(spec);
verify(createCmd).withHostName("ip-10-1-0-1.ec2.internal");
verify(createCmd).withAliases("ip-10-1-0-1.ec2.internal");
}
private static HostConfig createHostConfig(Integer hostCpus,
UnaryOperator<ContainerBuilder.Builder> customizer) {
EmulatorConfig config = mock(EmulatorConfig.class, RETURNS_DEEP_STUBS);
@@ -71,6 +71,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTimeoutPreemptively;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
@@ -78,6 +79,7 @@ import static org.mockito.ArgumentMatchers.argThat;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.RETURNS_SELF;
import static org.mockito.Mockito.clearInvocations;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.inOrder;
@@ -94,6 +96,14 @@ class EmrClusterStorageTest {
private static final String HOST = "ip-10-0-0-1.ec2.internal";
private static final String READY = "test -f /tmp/floci-hdfs-ready";
private static final BooleanSupplier NOT_STOPPED = () -> false;
private static final String CORE_HOST = "ip-10-1-0-1.ec2.internal";
private static final String TASK_HOST = "ip-10-2-0-1.ec2.internal";
private static final ClusterStorage.Node CORE_NODE = new ClusterStorage.Node("1-1", CORE_HOST, true);
private static final ClusterStorage.Node TASK_NODE = new ClusterStorage.Node("2-1", TASK_HOST, false);
private static final ClusterStorage.Yarn TWO_NODES = new ClusterStorage.Yarn(4096, 4)
.onNodes(List.of(CORE_NODE, TASK_NODE), 1, "s3://logs/j-1/containers/");
private static final ClusterStorage.Yarn CORE_ONLY = new ClusterStorage.Yarn(4096, 4)
.onNodes(List.of(CORE_NODE), 1, "s3://logs/j-1/containers/");
// A cluster with Hive, whose EC2 instance profile's role is r, with no core-site or hive-site.
private static final ClusterStorage.HiveMetastore METASTORE =
new ClusterStorage.HiveMetastore("arn:aws:iam::123456789012:role/r", Map.of());
@@ -108,8 +118,10 @@ class EmrClusterStorageTest {
private final CurrentContainerNetworkResolver currentContainer = mock(CurrentContainerNetworkResolver.class);
private final S3Service s3 = mock(S3Service.class);
private final EmrClusterStorage storage = new EmrClusterStorage(lifecycle, containerBuilder, awsEnv,
roleCredentials, currentContainer, config, s3);
roleCredentials, currentContainer, config, s3, Duration.ofSeconds(120), System::nanoTime);
private final String hdfsName = ContainerStorageHelper.dockerName(config, "emr-j-1-hdfs");
private final String coreName = ContainerStorageHelper.dockerName(config, "emr-j-1-node-1-1");
private final String taskName = ContainerStorageHelper.dockerName(config, "emr-j-1-node-2-1");
@BeforeEach
void docker() {
@@ -125,6 +137,661 @@ class EmrClusterStorageTest {
ListContainersCmd running = mock(ListContainersCmd.class, RETURNS_SELF);
when(docker.listContainersCmd()).thenReturn(running);
when(running.exec()).thenReturn(List.of());
// No node's volume is there without its container, and Docker removes the volumes it is asked to.
nodeVolumes();
when(lifecycle.removeVolume(anyString())).thenReturn(true);
}
@Test
void aClusterWithCoreAndTaskNodesRunsEachInAContainerOfItsOwn() {
nodesStart(coreName, taskName);
SparkJobRunner.Storage started = storage.hdfs("j-1", "123456789012", "us-east-1", HOST, IMAGE, "floci-network",
null, () -> TWO_NODES, NOT_STOPPED);
assertEquals(1, started.replication());
verify(spec).withName(coreName);
verify(spec).withName(taskName);
verify(lifecycle).ensureVolume(eq(coreName), any());
verify(spec).withNamedVolume(coreName, "/mnt");
verify(spec).withNamedVolume(taskName, "/mnt");
// Each under the name ListInstances gives it, on the cluster's network.
verify(spec).withDnsName(CORE_HOST);
verify(spec).withDnsName(TASK_HOST);
verify(spec).withDnsName(HOST);
verify(spec).withEnv("FLOCI_NODE_HOST", CORE_HOST);
verify(spec).withEnv("FLOCI_NODE_HOST", TASK_HOST);
// Only a core node holds a DataNode, and with one the primary node holds neither.
verify(spec).withEnv("FLOCI_NODE_CORE", "1");
verify(spec).withEnv("FLOCI_NODE_WORKERS", "1");
// What the primary node has of YARN too, and Floci in reach.
verify(spec, times(3)).withEnv("FLOCI_YARN_MEMORY_MB", "4096");
verify(spec, times(3)).withEnv("FLOCI_YARN_VCORES", "4");
verify(spec, times(3)).withHostDockerInternalOnLinux();
verify(spec, times(3)).withJavaTrustStore();
verify(spec, times(2)).withEntrypoint(argThat(entrypoint -> entrypoint.get(2).contains(
"n=1; if [ -n \"$FLOCI_NODE_CORE\" ]; then hdfs datanode & n=2; fi;")));
// It is ready once the ResourceManager knows it, not when it starts.
verify(spec, times(2)).withEntrypoint(argThat(entrypoint -> entrypoint.get(2).contains(
"until curl -sf -m 5 \"http://$FLOCI_HDFS_HOST:8088/ws/v1/cluster/nodes?states=RUNNING\""
+ " | grep -q \"\\\"nodeHostName\\\":\\\"$FLOCI_NODE_HOST\\\"\"")));
verify(spec, times(2)).withEntrypoint(argThat(entrypoint -> entrypoint.get(2).contains(
"grep -q \"$FLOCI_NODE_HOST:9866\"")));
verify(spec, times(3)).withEntrypoint(argThat(entrypoint -> entrypoint.get(2).contains(
"<name>dfs.hosts.exclude</name><value>/tmp/floci-hdfs-conf/dfs.exclude</value>")));
// A node that stops is dead to the NameNode after 40s, which a core node that has gone waits for.
verify(spec, times(3)).withEntrypoint(argThat(entrypoint -> entrypoint.get(2).contains(
"<name>dfs.namenode.heartbeat.recheck-interval</name><value>5000</value>")));
verify(spec, times(3)).withEntrypoint(argThat(entrypoint -> entrypoint.get(2).contains(
"<name>yarn.resourcemanager.nodes.exclude-path</name><value>/tmp/floci-hdfs-conf/yarn.exclude</value>")));
// The primary node runs its own DataNode and NodeManager only without core nodes, but a DataNode on a volume
// holding one's data, so the blocks on it stay in reach.
verify(spec).withEntrypoint(argThat(entrypoint -> entrypoint.get(2).contains(
"hdfs namenode & n=1; if [ -z \"$FLOCI_NODE_WORKERS\" ] || [ -d /mnt/datanode/current ]; then "
+ "hdfs datanode & n=2; fi;")));
// The primary node's readiness needs a DataNode of theirs, so they start before it is awaited.
InOrder order = inOrder(lifecycle, docker);
order.verify(lifecycle, times(3)).createAndStart(any());
order.verify(docker).execCreateCmd("c-1");
order.verify(docker).execCreateCmd(coreName);
}
@Test
void aCoreNodesDataNodeRegistersUnderItsNameAndACopyCountComesFromTheCluster() {
nodesStart(coreName, taskName);
storage.hdfs("j-1", "123456789012", "us-east-1", HOST, IMAGE, "floci-network", null,
() -> new ClusterStorage.Yarn(4096, 4).onNodes(List.of(CORE_NODE, TASK_NODE), 2, null), NOT_STOPPED);
verify(spec, times(3)).withEnv("FLOCI_HDFS_REPLICATION", "2");
verify(spec, times(3)).withEntrypoint(argThat(entrypoint -> entrypoint.get(2).contains(
"<name>dfs.replication</name><value>${FLOCI_HDFS_REPLICATION:-1}</value>")
&& entrypoint.get(2).contains("datanode_name=; [ -z \"$FLOCI_NODE_HOST\" ] || datanode_name=\"<property>"
+ "<name>dfs.datanode.hostname</name><value>$FLOCI_NODE_HOST</value></property>\"")));
}
@Test
void everyNodeRunsItsDaemonsFromTheHadoopItsImageKeepsApart() {
nodesStart(coreName, taskName);
storage.hdfs("j-1", "123456789012", "us-east-1", HOST, IMAGE, "floci-network", null,
() -> TWO_NODES.withHadoopFolder("/opt"), NOT_STOPPED);
verify(spec, times(3)).withEnv("FLOCI_HADOOP_FOLDER", "/opt");
}
@Test
void aNodeRunningInTheImageIsLeftRunning() {
nodesStart(coreName, taskName);
runningNodes(node(coreName, "running", IMAGE, CORE_HOST, true), node(taskName, "running", IMAGE, TASK_HOST, false));
storage.hdfs("j-1", "123456789012", "us-east-1", HOST, IMAGE, "floci-network", null, () -> TWO_NODES,
NOT_STOPPED);
verify(spec, never()).withName(coreName);
verify(spec, never()).withName(taskName);
verify(lifecycle, never()).removeIfExistsStrict(coreName);
}
@Test
void aNodeInAnotherImageOrStoppedStartsAgainOnItsData() {
nodesStart(coreName, taskName);
runningNodes(node(coreName, "running", "sha256:older", CORE_HOST, true),
node(taskName, "exited", IMAGE, TASK_HOST, false));
storage.hdfs("j-1", "123456789012", "us-east-1", HOST, IMAGE, "floci-network", null, () -> TWO_NODES,
NOT_STOPPED);
verify(lifecycle).removeIfExistsStrict(coreName);
verify(spec).withName(coreName);
verify(spec).withName(taskName);
verify(lifecycle, never()).removeVolume(coreName);
}
@Test
void aNodeThatStopsBeforeItIsReadyStartsAgainOnceThenFailsTheStep() {
nodesStart(coreName);
when(lifecycle.probeContainer(coreName)).thenReturn(ContainerLiveness.NOT_RUNNING);
IllegalStateException failed = assertThrows(IllegalStateException.class,
() -> storage.hdfs("j-1", "123456789012", "us-east-1", HOST, IMAGE, "floci-network", null,
() -> CORE_ONLY, NOT_STOPPED));
assertEquals("A node of EMR cluster j-1 stopped before it was ready", failed.getMessage());
verify(spec, times(2)).withName(coreName);
}
@Test
void aClusterWithOnlyTaskNodesKeepsItsDataNodeAndNodeManagerOnThePrimaryNode() {
nodesStart(taskName);
SparkJobRunner.Storage started = storage.hdfs("j-1", "123456789012", "us-east-1", HOST, IMAGE, "floci-network",
null, () -> new ClusterStorage.Yarn(4096, 4).onNodes(List.of(TASK_NODE), 1, null), NOT_STOPPED);
verify(spec, never()).withEnv(eq("FLOCI_NODE_WORKERS"), any());
verify(spec, never()).withEnv(eq("FLOCI_NODE_CORE"), any());
assertEquals(0, started.replication());
}
@Test
void aPrimaryNodeThatWorksKeepsItsDataNodeAndNodeManagerBesideItsCoreNodes() {
nodesStart(coreName, taskName);
storage.hdfs("j-1", "123456789012", "us-east-1", HOST, IMAGE, "floci-network", null,
() -> TWO_NODES.withPrimaryWorking(true), NOT_STOPPED);
verify(spec).withEnv("FLOCI_NODE_CORE", "1");
verify(spec, never()).withEnv(eq("FLOCI_NODE_WORKERS"), any());
}
@Test
void aResizeBeforeAStepHasStartedTheNodesStartsNone() {
storage.resize("j-1", () -> TWO_NODES);
verify(lifecycle, never()).createAndStart(any());
}
@Test
void aNewNodeStartsOnAResizeFromWhatItsClusterSaysThen() {
nodesStart(coreName, taskName);
storage.hdfs("j-1", "123456789012", "us-east-1", HOST, IMAGE, "floci-network", null, () -> CORE_ONLY,
NOT_STOPPED);
runningNodes(node(coreName, "running", IMAGE, CORE_HOST, true));
storage.resize("j-1", () -> TWO_NODES);
verify(spec).withName(taskName);
verify(spec, times(1)).withName(coreName);
}
@Test
void aFailedResizeIsLoggedAndLeavesTheClusterAsItIs() {
nodesStart(coreName);
storage.hdfs("j-1", "123456789012", "us-east-1", HOST, IMAGE, "floci-network", null, () -> CORE_ONLY,
NOT_STOPPED);
when(docker.listContainersCmd()).thenThrow(new IllegalStateException("Docker went away"));
assertDoesNotThrow(() -> storage.resize("j-1", () -> TWO_NODES));
}
@Test
void aTaskNodeNoLongerWantedIsDecommissionedAndGoesWithItsData() throws IOException {
nodesStart(coreName, taskName);
storage.hdfs("j-1", "123456789012", "us-east-1", HOST, IMAGE, "floci-network", null, () -> TWO_NODES,
NOT_STOPPED);
runningNodes(node(coreName, "running", IMAGE, CORE_HOST, true), node(taskName, "running", IMAGE, TASK_HOST, false));
List<List<String>> commands = ContainerExecStubs.completeEveryExec(docker, hdfsName, 0L, "", "");
String taskLog = "application_1_0001/container_1_0001_01_000002/stdout";
nodeLogs(taskName, Map.of(taskLog, "from task"));
clearInvocations(lifecycle);
storage.resize("j-1", () -> CORE_ONLY);
// Told first: YARN lets a task node go alone, as it holds no HDFS.
String told = commands.get(0).get(2);
assertTrue(told.contains("printf '" + TASK_HOST + "\\n' > /tmp/floci-hdfs-conf/yarn.exclude.new"), told);
assertTrue(told.contains("printf '' > /tmp/floci-hdfs-conf/dfs.exclude.new"), told);
assertTrue(told.contains("yarn rmadmin -refreshNodes -g 3600 -server"), told);
assertTrue(told.contains("hdfs dfsadmin -refreshNodes"), told);
// A refresh that fails takes its list away, so the next round's differs and is read again.
assertTrue(told.contains("-server || { rm -f /tmp/floci-hdfs-conf/yarn.exclude; exit 1; }"), told);
assertTrue(told.contains("hdfs dfsadmin -refreshNodes || { rm -f /tmp/floci-hdfs-conf/dfs.exclude; exit 1; }"),
told);
String asked = commands.get(2).get(2);
assertTrue(asked.contains("cluster/nodes?states=DECOMMISSIONED' | grep -q '\"nodeHostName\":\"" + TASK_HOST), asked);
assertFalse(asked.contains("Decommissioned"), asked);
verify(docker).copyArchiveFromContainerCmd(taskName, "/mnt/yarn/logs/" + taskLog);
verify(lifecycle).removeIfExistsStrict(taskName);
verify(lifecycle).removeVolume(taskName);
verify(lifecycle, never()).removeIfExistsStrict(coreName);
}
@Test
void aCoreNodeGoesOnlyOnceHdfsHasMovedItsBlocksAndYarnHasLetItGo() {
// Ten seconds on each reading, so the thread sees the NameNode's dead-node interval pass.
AtomicLong clock = new AtomicLong();
EmrClusterStorage ticking = new EmrClusterStorage(lifecycle, containerBuilder, awsEnv, roleCredentials,
currentContainer, config, s3, Duration.ofSeconds(120),
() -> clock.getAndAdd(Duration.ofSeconds(10).toNanos()), Runnable::run, Duration.ZERO);
nodesStart(coreName, taskName);
ticking.hdfs("j-1", "123456789012", "us-east-1", HOST, IMAGE, "floci-network", null, () -> TWO_NODES,
NOT_STOPPED);
runningNodes(node(coreName, "running", IMAGE, CORE_HOST, true), node(taskName, "running", IMAGE, TASK_HOST, false));
// HDFS told; told again and asked, not yet done; told again and asked, done; YARN told and asked, done; told
// while the NameNode has the node live, then not.
List<List<String>> commands = ContainerExecStubs.completeEveryExec(docker, hdfsName,
List.of(0L, 0L, 1L, 0L, 0L, 0L, 0L), "", "");
clearInvocations(lifecycle);
ticking.resize("j-1", () -> new ClusterStorage.Yarn(4096, 4).onNodes(List.of(TASK_NODE), 1, null));
String excluded = "printf '" + CORE_HOST + "\\n' > /tmp/floci-hdfs-conf/dfs.exclude.new";
String told = commands.get(0).get(2);
assertTrue(told.contains(excluded), told);
assertTrue(told.contains("printf '' > /tmp/floci-hdfs-conf/yarn.exclude.new"), told);
String hdfsAsked = commands.get(2).get(2);
assertTrue(hdfsAsked.contains("grep -Eq '" + CORE_HOST + ":9866[^}]*Decommissioned'"), hdfsAsked);
String yarnTold = commands.get(5).get(2);
assertTrue(yarnTold.contains("printf '" + CORE_HOST + "\\n' > /tmp/floci-hdfs-conf/yarn.exclude.new"),
yarnTold);
String yarnAsked = commands.get(6).get(2);
assertTrue(yarnAsked.contains("DECOMMISSIONED' | grep -q '\"nodeHostName\":\"" + CORE_HOST), yarnAsked);
assertEquals(2, commands.stream().filter(command -> command.get(2).contains("Decommissioned'")).count());
assertEquals(1, commands.stream().filter(command -> command.get(2).contains("DECOMMISSIONED'")).count());
String gone = commands.get(7).get(2);
assertTrue(gone.contains(excluded), gone);
String dead = commands.get(commands.size() - 1).get(2);
assertTrue(dead.contains("printf '' > /tmp/floci-hdfs-conf/dfs.exclude.new"), dead);
verify(lifecycle).removeIfExistsStrict(coreName);
verify(lifecycle).removeVolume(coreName);
}
@Test
void aNodeThatStoppedWhileItLeftGoesWithoutWaiting() {
nodesStart(coreName, taskName);
storage.hdfs("j-1", "123456789012", "us-east-1", HOST, IMAGE, "floci-network", null, () -> TWO_NODES,
NOT_STOPPED);
runningNodes(node(coreName, "running", IMAGE, CORE_HOST, true), node(taskName, "running", IMAGE, TASK_HOST, false));
ContainerExecStubs.completeEveryExec(docker, hdfsName, 1L, "", "");
when(lifecycle.probeContainer(taskName)).thenReturn(ContainerLiveness.NOT_RUNNING);
clearInvocations(lifecycle);
assertTimeoutPreemptively(Duration.ofSeconds(20), () -> storage.resize("j-1", () -> CORE_ONLY));
verify(lifecycle).removeIfExistsStrict(taskName);
}
@Test
void aNodeNotDecommissionedInTimeGoesAnyway() {
AtomicLong clock = new AtomicLong();
EmrClusterStorage ticking = new EmrClusterStorage(lifecycle, containerBuilder, awsEnv, roleCredentials,
currentContainer, config, s3, Duration.ofSeconds(120),
() -> clock.getAndAdd(Duration.ofHours(2).toNanos()), Runnable::run, Duration.ZERO);
nodesStart(coreName, taskName);
ticking.hdfs("j-1", "123456789012", "us-east-1", HOST, IMAGE, "floci-network", null, () -> TWO_NODES,
NOT_STOPPED);
runningNodes(node(coreName, "running", IMAGE, CORE_HOST, true), node(taskName, "running", IMAGE, TASK_HOST, false));
ContainerExecStubs.completeEveryExec(docker, hdfsName, 1L, "", "");
clearInvocations(lifecycle);
assertTimeoutPreemptively(Duration.ofSeconds(20), () -> ticking.resize("j-1", () -> CORE_ONLY));
verify(lifecycle).removeIfExistsStrict(taskName);
}
@Test
void aNodeALaterResizeLetsGoHasTheWholeTimeToo() {
// A minute on each reading, so the rounds go by.
AtomicLong clock = new AtomicLong();
EmrClusterStorage ticking = new EmrClusterStorage(lifecycle, containerBuilder, awsEnv, roleCredentials,
currentContainer, config, s3, Duration.ofSeconds(120),
() -> clock.getAndAdd(Duration.ofMinutes(1).toNanos()), Runnable::run, Duration.ZERO);
nodesStart(coreName, taskName);
ticking.hdfs("j-1", "123456789012", "us-east-1", HOST, IMAGE, "floci-network", null, () -> TWO_NODES,
NOT_STOPPED);
runningNodes(node(coreName, "running", IMAGE, CORE_HOST, true), node(taskName, "running", IMAGE, TASK_HOST, false));
// Neither HDFS nor YARN lets a node go, so each goes when its time is up.
ContainerExecStubs.completeEveryExec(docker, hdfsName, 1L, "", "");
AtomicLong coreLetGo = new AtomicLong();
AtomicBoolean shrunk = new AtomicBoolean();
when(lifecycle.probeContainer(taskName)).thenAnswer(asked -> {
// Fifty-nine minutes into the task node's decommissioning, a resize lets the core node go too.
if (shrunk.compareAndSet(false, true)) {
clock.addAndGet(Duration.ofMinutes(59).toNanos());
coreLetGo.set(clock.get());
ticking.resize("j-1", () -> new ClusterStorage.Yarn(4096, 4).onNodes(List.of(), 1, null));
}
return ContainerLiveness.RUNNING;
});
AtomicLong coreGone = new AtomicLong();
doAnswer(removing -> {
coreGone.set(clock.get());
return null;
}).when(lifecycle).removeIfExistsStrict(coreName);
assertTimeoutPreemptively(Duration.ofSeconds(20), () -> ticking.resize("j-1", () -> CORE_ONLY));
assertTrue(coreGone.get() - coreLetGo.get() >= Duration.ofHours(1).toNanos(),
"gone " + Duration.ofNanos(coreGone.get() - coreLetGo.get()) + " after it was let go");
}
@Test
void aNodeDockerCannotTellTheStateOfIsAskedAgainRatherThanTakenAsGone() {
AtomicLong clock = new AtomicLong();
EmrClusterStorage ticking = new EmrClusterStorage(lifecycle, containerBuilder, awsEnv, roleCredentials,
currentContainer, config, s3, Duration.ofSeconds(120),
() -> clock.getAndAdd(Duration.ofMinutes(30).toNanos()), Runnable::run, Duration.ZERO);
nodesStart(coreName, taskName);
ticking.hdfs("j-1", "123456789012", "us-east-1", HOST, IMAGE, "floci-network", null, () -> TWO_NODES,
NOT_STOPPED);
runningNodes(node(coreName, "running", IMAGE, CORE_HOST, true), node(taskName, "running", IMAGE, TASK_HOST, false));
ContainerExecStubs.completeEveryExec(docker, hdfsName, 1L, "", "");
when(lifecycle.probeContainer(taskName)).thenReturn(ContainerLiveness.UNKNOWN);
AtomicLong taskGone = new AtomicLong();
doAnswer(removing -> {
taskGone.set(clock.get());
return null;
}).when(lifecycle).removeIfExistsStrict(taskName);
long letGo = clock.get();
assertTimeoutPreemptively(Duration.ofSeconds(20), () -> ticking.resize("j-1", () -> CORE_ONLY));
// Not on the first round, as a stopped node would: only once its time was up.
assertTrue(taskGone.get() - letGo > Duration.ofHours(1).toNanos(),
"gone " + Duration.ofNanos(taskGone.get() - letGo) + " after it was let go");
}
@Test
void aCoreNodeThatStopsBeforeHdfsMovedItsBlocksStartsAgainOnItsData() {
AtomicLong clock = new AtomicLong();
EmrClusterStorage ticking = new EmrClusterStorage(lifecycle, containerBuilder, awsEnv, roleCredentials,
currentContainer, config, s3, Duration.ofSeconds(120),
() -> clock.getAndAdd(Duration.ofMinutes(30).toNanos()), Runnable::run, Duration.ZERO);
nodesStart(coreName, taskName);
ticking.hdfs("j-1", "123456789012", "us-east-1", HOST, IMAGE, "floci-network", null, () -> TWO_NODES,
NOT_STOPPED);
runningNodes(node(coreName, "running", IMAGE, CORE_HOST, true), node(taskName, "running", IMAGE, TASK_HOST, false));
ContainerExecStubs.completeEveryExec(docker, hdfsName, 1L, "", "");
// It stops before HDFS has moved its blocks, and runs once started again.
when(lifecycle.probeContainer(coreName)).thenReturn(ContainerLiveness.NOT_RUNNING, ContainerLiveness.RUNNING);
clearInvocations(spec, lifecycle);
assertTimeoutPreemptively(Duration.ofSeconds(20),
() -> ticking.resize("j-1", () -> new ClusterStorage.Yarn(4096, 4).onNodes(List.of(TASK_NODE), 1, null)));
verify(spec).withName(coreName);
verify(spec).withNamedVolume(coreName, "/mnt");
// Its data goes only after that, here once its time was up.
InOrder order = inOrder(lifecycle);
order.verify(lifecycle).createAndStart(any());
order.verify(lifecycle).removeVolume(coreName);
}
@Test
void aRoundThatFailsIsTriedAgainOnTheNext() {
nodesStart(coreName, taskName);
storage.hdfs("j-1", "123456789012", "us-east-1", HOST, IMAGE, "floci-network", null, () -> TWO_NODES,
NOT_STOPPED);
runningNodes(node(coreName, "running", IMAGE, CORE_HOST, true), node(taskName, "running", IMAGE, TASK_HOST, false));
ContainerExecStubs.completeEveryExec(docker, hdfsName, 0L, "", "");
when(lifecycle.probeContainer(taskName)).thenThrow(new IllegalStateException("Docker went away"))
.thenReturn(ContainerLiveness.NOT_RUNNING);
clearInvocations(lifecycle);
assertTimeoutPreemptively(Duration.ofSeconds(20), () -> storage.resize("j-1", () -> CORE_ONLY));
verify(lifecycle).removeIfExistsStrict(taskName);
verify(lifecycle).removeVolume(taskName);
}
@Test
void aNodeWhoseDataDockerKeepsStaysToBeRemovedOnTheNextRound() {
nodesStart(coreName, taskName);
storage.hdfs("j-1", "123456789012", "us-east-1", HOST, IMAGE, "floci-network", null, () -> TWO_NODES,
NOT_STOPPED);
runningNodes(node(coreName, "running", IMAGE, CORE_HOST, true), node(taskName, "running", IMAGE, TASK_HOST, false));
ContainerExecStubs.completeEveryExec(docker, hdfsName, 0L, "", "");
when(lifecycle.probeContainer(taskName)).thenReturn(ContainerLiveness.NOT_RUNNING);
when(lifecycle.removeVolume(taskName)).thenReturn(false, true);
clearInvocations(lifecycle);
assertTimeoutPreemptively(Duration.ofSeconds(20), () -> storage.resize("j-1", () -> CORE_ONLY));
verify(lifecycle, times(2)).removeVolume(taskName);
// Its logs are read once, while its container is there.
verify(docker, times(1)).copyArchiveFromContainerCmd(taskName, "/mnt/yarn/logs");
}
@Test
void aCoreNodeAShutdownCutOffWhileItLeftStartsAgainOnItsDataToBeDecommissioned() {
List<Runnable> queued = new ArrayList<>();
EmrClusterStorage queuing = queuingDecommissions(queued);
nodesStart(coreName, taskName);
// Its container went with the shutdown, and only its volume says it was a core node.
nodeVolumes(nodeVolume(coreName, CORE_HOST, true));
List<List<String>> commands = ContainerExecStubs.completeEveryExec(docker, hdfsName, 0L, "", "");
queuing.hdfs("j-1", "123456789012", "us-east-1", HOST, IMAGE, "floci-network", null,
() -> new ClusterStorage.Yarn(4096, 4).onNodes(List.of(TASK_NODE), 1, null), NOT_STOPPED);
verify(spec).withName(coreName);
verify(spec).withNamedVolume(coreName, "/mnt");
verify(lifecycle, never()).removeVolume(coreName);
String told = commands.get(0).get(2);
assertTrue(told.contains("printf '" + CORE_HOST + "\\n' > /tmp/floci-hdfs-conf/dfs.exclude.new"), told);
assertEquals(1, queued.size());
}
@Test
void aTaskNodesDataAShutdownLeftGoesAsItHoldsNoBlocks() {
nodesStart(coreName);
nodeVolumes(nodeVolume(taskName, TASK_HOST, false));
storage.hdfs("j-1", "123456789012", "us-east-1", HOST, IMAGE, "floci-network", null, () -> CORE_ONLY,
NOT_STOPPED);
verify(lifecycle).removeVolume(taskName);
verify(spec, never()).withName(taskName);
}
@Test
void aNodeWhoseStartATeardownPassedGoes() {
nodesStart(coreName);
AtomicLong starts = new AtomicLong();
when(lifecycle.createAndStart(any())).thenAnswer(starting -> {
// The second is the core node's, and a shutdown sweeps while Docker creates it.
if (starts.incrementAndGet() == 2) {
storage.stopManagedContainers();
}
return new ContainerLifecycleManager.ContainerInfo("c-1", Map.of());
});
assertThrows(ClusterStorage.Stopped.class, () -> storage.hdfs("j-1", "123456789012", "us-east-1", HOST, IMAGE,
"floci-network", null, () -> CORE_ONLY, NOT_STOPPED));
// Before it was created, by the sweep, and once it was.
verify(lifecycle, times(3)).removeIfExistsStrict(coreName);
}
@Test
void aNodeWantedAgainWhileItLeavesCarriesOn() {
List<Runnable> queued = new ArrayList<>();
EmrClusterStorage queuing = queuingDecommissions(queued);
nodesStart(coreName, taskName);
queuing.hdfs("j-1", "123456789012", "us-east-1", HOST, IMAGE, "floci-network", null, () -> TWO_NODES,
NOT_STOPPED);
runningNodes(node(coreName, "running", IMAGE, CORE_HOST, true), node(taskName, "running", IMAGE, TASK_HOST, false));
List<List<String>> commands = ContainerExecStubs.completeEveryExec(docker, hdfsName, 0L, "", "");
queuing.resize("j-1", () -> CORE_ONLY);
assertEquals(1, queued.size());
clearInvocations(lifecycle);
queuing.resize("j-1", () -> TWO_NODES);
queued.get(0).run();
assertTrue(commands.get(1).get(2).contains("printf '' > /tmp/floci-hdfs-conf/yarn.exclude.new"),
commands.get(1).get(2));
verify(lifecycle, never()).removeIfExistsStrict(taskName);
verify(lifecycle, never()).removeVolume(taskName);
}
@Test
void oneThreadDecommissionsACluster() {
List<Runnable> queued = new ArrayList<>();
EmrClusterStorage queuing = queuingDecommissions(queued);
nodesStart(coreName, taskName);
queuing.hdfs("j-1", "123456789012", "us-east-1", HOST, IMAGE, "floci-network", null, () -> TWO_NODES,
NOT_STOPPED);
runningNodes(node(coreName, "running", IMAGE, CORE_HOST, true), node(taskName, "running", IMAGE, TASK_HOST, false));
ContainerExecStubs.completeEveryExec(docker, hdfsName, 0L, "", "");
queuing.resize("j-1", () -> CORE_ONLY);
queuing.resize("j-1", () -> new ClusterStorage.Yarn(4096, 4).onNodes(List.of(), 1, null));
assertEquals(1, queued.size());
}
@Test
void aClusterThatEndsWhileANodeLeavesEndsItsDecommissioning() {
List<Runnable> queued = new ArrayList<>();
EmrClusterStorage queuing = queuingDecommissions(queued);
nodesStart(coreName, taskName);
queuing.hdfs("j-1", "123456789012", "us-east-1", HOST, IMAGE, "floci-network", null, () -> TWO_NODES,
NOT_STOPPED);
runningNodes(node(coreName, "running", IMAGE, CORE_HOST, true), node(taskName, "running", IMAGE, TASK_HOST, false));
List<List<String>> commands = ContainerExecStubs.completeEveryExec(docker, hdfsName, 0L, "", "");
queuing.resize("j-1", () -> CORE_ONLY);
int told = commands.size();
queuing.remove("j-1");
queued.get(0).run();
assertEquals(told, commands.size());
}
@Test
void aClustersEndRemovesItsNodesAndTheirDataBeforeItsNetwork() {
when(lifecycle.removeNetwork(any())).thenReturn(true);
when(lifecycle.removeVolume(any())).thenReturn(true);
runningNodes(node(coreName, "running", IMAGE, CORE_HOST, true), node(taskName, "running", IMAGE, TASK_HOST, false));
assertTrue(storage.remove("j-1"));
InOrder order = inOrder(lifecycle);
order.verify(lifecycle).removeIfExistsStrict(coreName);
order.verify(lifecycle).removeVolume(coreName);
order.verify(lifecycle).removeIfExistsStrict(taskName);
order.verify(lifecycle).removeVolume(taskName);
order.verify(lifecycle).removeIfExistsStrict(hdfsName);
order.verify(lifecycle).removeNetwork(hdfsName);
}
@Test
void aNodeDockerWillNotRemoveKeepsItsClustersEndGoing() {
when(lifecycle.removeNetwork(any())).thenReturn(true);
when(lifecycle.removeVolume(any())).thenReturn(true);
runningNodes(node(coreName, "running", IMAGE, CORE_HOST, true));
doThrow(new IllegalStateException("busy")).when(lifecycle).removeIfExistsStrict(coreName);
assertFalse(storage.remove("j-1"));
}
@Test
void aClustersEndRemovesTheDataOfANodeAShutdownLeftWithoutItsContainer() {
when(lifecycle.removeNetwork(any())).thenReturn(true);
nodeVolumes(nodeVolume(coreName, CORE_HOST, true));
assertTrue(storage.remove("j-1"));
verify(lifecycle).removeVolume(coreName);
}
@Test
void aClustersEndThatCannotFindItsNodesRemovesTheRestAndIsAskedForAgain() {
when(lifecycle.removeNetwork(any())).thenReturn(true);
when(docker.listContainersCmd()).thenThrow(new IllegalStateException("Docker went away"));
assertFalse(storage.remove("j-1"));
verify(lifecycle).removeIfExistsStrict(hdfsName);
verify(lifecycle).removeNetwork(hdfsName);
verify(lifecycle).removeVolume(hdfsName);
}
@Test
void aResetRemovesTheNodesAndLeavesTheirDataForTheNextStep() {
nodesStart(coreName);
storage.hdfs("j-1", "123456789012", "us-east-1", HOST, IMAGE, "floci-network", null, () -> CORE_ONLY,
NOT_STOPPED);
clearInvocations(lifecycle);
storage.stopManagedContainers();
verify(lifecycle).removeIfExistsStrict(coreName);
verify(lifecycle, never()).removeVolume(coreName);
}
@Test
void theNodesOfAClusterThatHasEndedGoAtStartAndThoseOfOneUpStay() {
Map<String, Container> byCluster = new LinkedHashMap<>();
String otherName = ContainerStorageHelper.dockerName(config, "emr-j-2-node-1-1");
Container gone = nodeOf("j-1", coreName);
Container up = nodeOf("j-2", otherName);
byCluster.put(coreName, gone);
byCluster.put(otherName, up);
AtomicReference<Map<String, String>> filter = new AtomicReference<>();
ListContainersCmd list = mock(ListContainersCmd.class, RETURNS_SELF);
when(docker.listContainersCmd()).thenReturn(list);
when(list.withLabelFilter(any(Map.class))).thenAnswer(asked -> {
filter.set(asked.getArgument(0));
return list;
});
when(list.exec()).thenAnswer(reading -> "emr-node".equals(filter.get().get(ContainerStorageHelper.COMPONENT_LABEL))
? List.copyOf(byCluster.values()) : List.of());
ListNetworksCmd networks = mock(ListNetworksCmd.class, RETURNS_SELF);
when(docker.listNetworksCmd()).thenReturn(networks);
when(networks.exec()).thenReturn(List.of());
ListVolumesCmd volumes = mock(ListVolumesCmd.class, RETURNS_SELF);
when(docker.listVolumesCmd()).thenReturn(volumes);
when(volumes.exec()).thenReturn(mock(ListVolumesResponse.class));
storage.removeAllBut(Set.of("j-2"));
verify(lifecycle).removeIfExistsStrict(coreName);
verify(lifecycle, never()).removeIfExistsStrict(otherName);
}
@Test
void aStoppedNodesLogsComeFromTheWholeFolderOnceAndNeverItsLaunchScript() throws IOException {
runningNodes(node(taskName, "exited", IMAGE, TASK_HOST, false));
ContainerExecStubs.completeEveryExec(docker, hdfsName, 0L, "", "");
// A decommissioned node's NodeManager exits, which ends its container, and Docker refuses a command there.
when(lifecycle.probeContainer(taskName)).thenReturn(ContainerLiveness.NOT_RUNNING);
String container = "logs/application_1_0001/container_1_0001_01_000002/";
byte[] logs = tar(Map.of(container + "stdout", "from task", container + "launch_container.sh", "secret"));
CopyArchiveFromContainerCmd copy = mock(CopyArchiveFromContainerCmd.class);
when(copy.exec()).thenAnswer(reading -> new ByteArrayInputStream(logs));
when(docker.copyArchiveFromContainerCmd(taskName, "/mnt/yarn/logs")).thenReturn(copy);
storage.archiveContainerLogs("j-1", "123456789012", "us-east-1", "s3://logs/j-1/containers/");
storage.archiveContainerLogs("j-1", "123456789012", "us-east-1", "s3://logs/j-1/containers/");
verify(s3, times(1)).putObjectFromStream(eq("logs"),
eq("j-1/containers/application_1_0001/container_1_0001_01_000002/stdout.gz"), any(), anyString(), any());
verify(s3, times(1)).putObjectFromStream(anyString(), anyString(), any(), anyString(), any());
verify(docker, never()).execCreateCmd(taskName);
}
@Test
void aStoppedPrimaryNodeWithNoNodeManagerHasNoLogsAndNoFailure() {
when(lifecycle.probeContainer(hdfsName)).thenReturn(ContainerLiveness.NOT_RUNNING);
when(docker.copyArchiveFromContainerCmd(hdfsName, "/mnt/yarn/logs")).thenThrow(new NotFoundException("none"));
assertDoesNotThrow(() -> storage.archiveContainerLogs("j-1", "123456789012", "us-east-1",
"s3://logs/j-1/containers/"));
}
@Test
void nodesThatCannotBeFoundLeaveTheStepAsItEnded() {
when(lifecycle.probeContainer(hdfsName)).thenReturn(ContainerLiveness.NOT_RUNNING);
when(docker.copyArchiveFromContainerCmd(hdfsName, "/mnt/yarn/logs")).thenThrow(new NotFoundException("none"));
when(docker.listContainersCmd()).thenThrow(new IllegalStateException("Docker went away"));
assertDoesNotThrow(() -> storage.archiveContainerLogs("j-1", "123456789012", "us-east-1",
"s3://logs/j-1/containers/"));
}
@Test
void containerLogsComeFromEveryNode() throws IOException {
runningNodes(node(coreName, "running", IMAGE, CORE_HOST, true), node(taskName, "running", IMAGE, TASK_HOST, false));
// The primary node runs no NodeManager, so it lists none.
ContainerExecStubs.completeEveryExec(docker, hdfsName, 0L, "", "");
nodeLogs(coreName, Map.of("application_1_0001/container_1_0001_01_000001/stdout", "from core"));
nodeLogs(taskName, Map.of("application_1_0001/container_1_0001_01_000002/stdout", "from task"));
storage.archiveContainerLogs("j-1", "123456789012", "us-east-1", "s3://logs/j-1/containers/");
verify(s3, times(2)).putObjectFromStream(anyString(), anyString(), any(), anyString(), any());
}
@Test
@@ -181,7 +848,8 @@ class EmrClusterStorageTest {
// cluster's sharing its namespace.
verify(spec).withEntrypoint(argThat(entrypoint -> entrypoint.get(2).contains(String.join(" ",
"cid=$(sed -n \"s/^clusterID=//p\" /mnt/namenode/current/VERSION);"
+ " hdfs namenode & hdfs datanode & n=2;",
+ " hdfs namenode & n=1; if [ -z \"$FLOCI_NODE_WORKERS\" ] || [ -d /mnt/datanode/current ];"
+ " then hdfs datanode & n=2; fi;",
"until curl -sf -m 5 \"http://127.0.0.1:9870/jmx?qry=Hadoop:service=NameNode,name=NameNodeInfo\"",
"| grep -q \"\\\"ClusterId\\\" : \\\"$cid\\\"\"; do",
"[ \"$(jobs -rp | wc -l)\" = $n ] || exit 1; sleep 0.2; done;"))));
@@ -191,7 +859,7 @@ class EmrClusterStorageTest {
// exits ends it.
verify(spec).withEntrypoint(argThat(entrypoint -> entrypoint.get(2).contains(String.join(" ",
"until curl -sf -m 5 \"http://127.0.0.1:9870/jmx?qry=Hadoop:service=NameNode,name=FSNamesystemState\"",
"| grep -q \"\\\"NumLiveDataNodes\\\" : 1,\"; do",
"| grep -q \"\\\"NumLiveDataNodes\\\" : [1-9]\"; do",
"[ \"$(jobs -rp | wc -l)\" = $n ] || exit 1; sleep 0.2; done;",
"until curl -sf -m 5 \"http://127.0.0.1:9870/jmx?qry=Hadoop:service=NameNode,name=NameNodeInfo\"",
"| grep -q \"\\\"Safemode\\\" : \\\"\\\"\"; do",
@@ -275,7 +943,7 @@ class EmrClusterStorageTest {
when(awsEnv.flociEndpoint(false)).thenReturn("http://host.docker.internal:4566");
SparkJobRunner.Storage started = storage.hdfs("j-1", "123456789012", "us-east-1", HOST, IMAGE,
"floci-network", null, new ClusterStorage.Yarn(4096, 4), NOT_STOPPED);
"floci-network", null, () -> new ClusterStorage.Yarn(4096, 4), NOT_STOPPED);
// The steps' Spark submits to the ResourceManager on the primary node, its executors reaching Floci there.
assertEquals(HOST, started.resourceManager());
@@ -297,14 +965,14 @@ class EmrClusterStorageTest {
+ "<value>$FLOCI_YARN_VCORES</value></property>\""))
// Each daemon through a variable Java 8, which refuses these options, ignores.
&& Pattern.compile("JDK_JAVA_OPTIONS=\"--add-opens=java\\.base/java\\.lang=ALL-UNNAMED[^\"]*\""
+ " yarn resourcemanager & "
+ " yarn resourcemanager & n=\\$\\(\\(n \\+ 1\\)\\); if \\[ -z \"\\$FLOCI_NODE_WORKERS\" \\]; then "
+ "JDK_JAVA_OPTIONS=\"--add-opens=java\\.base/java\\.lang=ALL-UNNAMED[^\"]*\""
+ " yarn nodemanager &").matcher(entrypoint.get(2)).find()));
// Ready once the NodeManager has registered, and the node gone if either daemon exits while starting.
verify(spec).withEntrypoint(argThat(entrypoint -> entrypoint.get(2).contains(
"yarn nodemanager & n=$((n + 2)); "
"yarn nodemanager & n=$((n + 1)); fi; "
+ "until curl -sf -m 5 http://127.0.0.1:8088/ws/v1/cluster/metrics"
+ " | grep -q \"\\\"activeNodes\\\":1,\"; do "
+ " | grep -q \"\\\"activeNodes\\\":[1-9]\"; do "
+ "[ \"$(jobs -rp | wc -l)\" = $n ] || exit 1; sleep 0.2; done; fi;"
+ " touch /tmp/floci-hdfs-ready")));
}
@@ -319,7 +987,7 @@ class EmrClusterStorageTest {
when(awsEnv.flociEndpoint(false)).thenReturn("http://host.docker.internal:4566");
storage.hdfs("j-1", "123456789012", "us-east-1", HOST, IMAGE, "floci-network", null,
new ClusterStorage.Yarn(4096, 4, "/opt"), NOT_STOPPED);
() -> new ClusterStorage.Yarn(4096, 4).withHadoopFolder("/opt"), NOT_STOPPED);
verify(spec).withEnv("FLOCI_HADOOP_FOLDER", "/opt");
// Its homes and commands, before the daemons start as the image's user, who keeps the environment.
@@ -414,8 +1082,9 @@ class EmrClusterStorageTest {
String later = "application_1_0002/container_1_0002_01_000001/stdout";
List<List<String>> listings = nodeLogs(Map.of(earlier, "one"));
storage.archiveContainerLogs("j-1", "123456789012", "us-east-1", "s3://logs/j-1/containers/");
assertEquals(List.of(List.of("find", "/mnt/yarn/logs", "-mindepth", "3", "-maxdepth", "3", "-type", "f", "-printf",
"%s %P\\n")), listings);
// A primary node whose core nodes run the NodeManagers has no such folder, which is none to list and no failure.
assertEquals(List.of(List.of("bash", "-c", "[ ! -d /mnt/yarn/logs ] || exec find /mnt/yarn/logs -mindepth 3"
+ " -maxdepth 3 -type f -printf '%s %P\\n'")), listings);
nodeLogs(Map.of(earlier, "one", later, "two"));
storage.archiveContainerLogs("j-1", "123456789012", "us-east-1", "s3://logs/j-1/containers/");
@@ -1516,21 +2185,83 @@ class EmrClusterStorageTest {
verify(lifecycle, never()).disconnectFromNetwork("floci-container", "emr-j-up-hdfs");
}
// The files, by their paths in the archive, as Docker archives a folder.
// The primary node's container logs, by their path under the NodeManager's folder, as it lists and gives them. What
// it is asked to list.
private List<List<String>> nodeLogs(Map<String, String> logs) throws IOException {
return nodeLogs(hdfsName, logs);
}
private List<List<String>> nodeLogs(String node, Map<String, String> logs) throws IOException {
StringBuilder listing = new StringBuilder();
for (Map.Entry<String, String> log : logs.entrySet()) {
listing.append(log.getValue().length()).append(' ').append(log.getKey()).append('\n');
byte[] archive = tar(Map.of(log.getKey().substring(log.getKey().lastIndexOf('/') + 1), log.getValue()));
CopyArchiveFromContainerCmd copy = mock(CopyArchiveFromContainerCmd.class);
when(copy.exec()).thenAnswer(reading -> new ByteArrayInputStream(archive));
when(docker.copyArchiveFromContainerCmd(hdfsName, "/mnt/yarn/logs/" + log.getKey())).thenReturn(copy);
when(docker.copyArchiveFromContainerCmd(node, "/mnt/yarn/logs/" + log.getKey())).thenReturn(copy);
}
return ContainerExecStubs.completeEveryExec(docker, hdfsName, 0L, listing.toString(), "");
return ContainerExecStubs.completeEveryExec(docker, node, 0L, listing.toString(), "");
}
// The primary node starts and answers ready, and so does each node named.
private void nodesStart(String... names) {
when(lifecycle.probeContainer(hdfsName)).thenReturn(ContainerLiveness.NOT_RUNNING);
when(lifecycle.createAndStart(any())).thenReturn(new ContainerLifecycleManager.ContainerInfo("c-1", Map.of()));
when(lifecycle.probeContainer("c-1")).thenReturn(ContainerLiveness.RUNNING);
when(lifecycle.resolveContainerNetworkIp("c-1", hdfsName)).thenReturn("172.18.0.5");
ContainerExecStubs.completeEveryExec(docker, "c-1", 0L, "", "");
for (String name : names) {
when(lifecycle.probeContainer(name)).thenReturn(ContainerLiveness.RUNNING);
ContainerExecStubs.completeEveryExec(docker, name, 0L, "", "");
}
}
private void runningNodes(Container... nodes) {
ListContainersCmd list = mock(ListContainersCmd.class, RETURNS_SELF);
when(docker.listContainersCmd()).thenReturn(list);
when(list.exec()).thenReturn(List.of(nodes));
}
private static Container node(String name, String state, String image, String host, boolean core) {
Container container = mock(Container.class);
when(container.getNames()).thenReturn(new String[]{"/" + name});
when(container.getState()).thenReturn(state);
when(container.getImageId()).thenReturn(image);
when(container.getLabels()).thenReturn(Map.of("io.floci.emr.node-host", host, "io.floci.emr.node-core",
String.valueOf(core)));
return container;
}
// The volumes of cluster j-1's nodes Docker lists, whatever their containers.
private void nodeVolumes(InspectVolumeResponse... volumes) {
ListVolumesCmd list = mock(ListVolumesCmd.class, RETURNS_SELF);
when(docker.listVolumesCmd()).thenReturn(list);
ListVolumesResponse response = mock(ListVolumesResponse.class);
when(response.getVolumes()).thenReturn(List.of(volumes));
when(list.exec()).thenReturn(response);
}
private static InspectVolumeResponse nodeVolume(String name, String host, boolean core) {
InspectVolumeResponse volume = mock(InspectVolumeResponse.class);
when(volume.getName()).thenReturn(name);
when(volume.getLabels()).thenReturn(Map.of(ContainerStorageHelper.RESOURCE_ID_LABEL, "j-1",
"io.floci.emr.node-host", host, "io.floci.emr.node-core", String.valueOf(core)));
return volume;
}
private static Container nodeOf(String clusterId, String name) {
Container container = mock(Container.class);
when(container.getNames()).thenReturn(new String[]{"/" + name});
when(container.getLabels()).thenReturn(Map.of(ContainerStorageHelper.RESOURCE_ID_LABEL, clusterId));
return container;
}
private EmrClusterStorage queuingDecommissions(List<Runnable> queued) {
return new EmrClusterStorage(lifecycle, containerBuilder, awsEnv, roleCredentials, currentContainer, config, s3,
Duration.ofSeconds(120), System::nanoTime, queued::add, Duration.ZERO);
}
// The files, by their paths in the archive, as Docker archives a folder.
private static byte[] tar(Map<String, String> files) throws IOException {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
try (TarArchiveOutputStream out = new TarArchiveOutputStream(bytes)) {
@@ -2,6 +2,7 @@ package io.floci.aws.services.emr;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.github.dockerjava.api.exception.NotFoundException;
import io.floci.aws.config.EmulatorConfig;
import io.floci.aws.core.common.docker.ContainerExec;
import io.floci.aws.core.common.docker.ContainerLifecycleManager;
@@ -210,6 +211,45 @@ class EmrDockerRunnerDockerIntegrationTest {
driverLogs.get(0));
}
@Test
void aClustersCoreAndTaskNodesRunItsExecutorsAndAShrinkKeepsItsHdfsData() {
put("side/nodes.py", """
import socket
import sys
from pyspark.sql import SparkSession
spark = SparkSession.builder.getOrCreate()
lines = spark.sparkContext.parallelize(range(8), 8) \\
.map(lambda i: (socket.gethostname(), "line " + str(i))).cache()
lines.map(lambda line: line[1]).saveAsTextFile("hdfs:///kept")
hosts = " ".join(sorted(set(lines.map(lambda line: line[0]).collect())))
spark.createDataFrame([(hosts,)], ["value"]).coalesce(1).write.text(sys.argv[1])
""");
startCluster("[]", null, ",{\"Name\":\"core\",\"InstanceRole\":\"CORE\",\"InstanceType\":\"m5.xlarge\","
+ "\"InstanceCount\":2},{\"Name\":\"task\",\"InstanceRole\":\"TASK\",\"InstanceType\":\"m5.xlarge\","
+ "\"InstanceCount\":1}");
// Its executors run on its core and task nodes, each named as ListInstances names it, not on its primary node.
assertEquals("COMPLETED", runStep("command-runner.jar", "spark-submit", s3Uri("side/nodes.py"),
s3Uri("hosts/")));
List<String> instances = emr("ListInstances", "{\"ClusterId\":\"" + clusterId + "\"}").jsonPath()
.getList("Instances.PrivateDnsName");
for (String host : partFiles("hosts/").trim().split(" ")) {
assertTrue(instances.subList(1, instances.size()).contains(host), host + " among " + instances);
}
// A core node goes, once HDFS has moved its blocks to the one that stays, and every line is still there.
String core = emr("ListInstanceGroups", "{\"ClusterId\":\"" + clusterId + "\"}").jsonPath()
.getString("InstanceGroups.find { it.InstanceGroupType == 'CORE' }.Id");
emr("ModifyInstanceGroups", "{\"ClusterId\":\"" + clusterId + "\",\"InstanceGroups\":[{\"InstanceGroupId\":\""
+ core + "\",\"InstanceCount\":1}]}").then().statusCode(200);
String leaving = ContainerStorageHelper.dockerName(config, "emr-" + clusterId + "-node-1-2");
await("core node " + leaving + " to go").atMost(Duration.ofMinutes(5)).pollInterval(Duration.ofSeconds(1))
.until(() -> gone(leaving));
assertEquals("COMPLETED", runStep("command-runner.jar", "s3-dist-cp", "--src", "hdfs:///kept/",
"--dest", s3Uri("kept/")));
assertEquals("line 0\nline 1\nline 2\nline 3\nline 4\nline 5\nline 6\nline 7\n", partFiles("kept/"));
}
@Test
void aClustersHdfsWhoseDataNodeExitsStartsAgainOnItsDataForTheNextStep() {
startCluster("[]");
@@ -344,16 +384,31 @@ class EmrDockerRunnerDockerIntegrationTest {
/** The same, its logs under {@code logUri} when that is not null, as the cluster's {@code LogUri}. */
private void startCluster(String configurations, String logUri) {
startCluster(configurations, logUri, "");
}
/** The same, with the instance groups {@code moreGroups}, each led by a comma, after its primary node's. */
private void startCluster(String configurations, String logUri, String moreGroups) {
clusterId = emr("RunJobFlow", "{\"Name\":\"docker-runner\",\"ReleaseLabel\":\"emr-7.5.0\","
+ (logUri == null ? "" : "\"LogUri\":\"" + logUri + "\",")
+ "\"Applications\":[{\"Name\":\"Hadoop\"},{\"Name\":\"Spark\"}],"
+ "\"Configurations\":" + configurations + ","
+ "\"Instances\":{\"KeepJobFlowAliveWhenNoSteps\":true,\"InstanceGroups\":[{\"Name\":\"primary\","
+ "\"InstanceRole\":\"MASTER\",\"InstanceType\":\"m5.xlarge\",\"InstanceCount\":1}]}}")
+ "\"InstanceRole\":\"MASTER\",\"InstanceType\":\"m5.xlarge\",\"InstanceCount\":1}" + moreGroups + "]}}")
.then().statusCode(200).extract().jsonPath().getString("JobFlowId");
cleanup.register(() -> terminateClusterAndAwaitCleanup(clusterId, emrService));
}
/** Whether Docker no longer has the container named so. */
private boolean gone(String name) {
try {
lifecycle.getDockerClient().inspectContainerCmd(name).exec();
return false;
} catch (NotFoundException e) {
return true;
}
}
/** Adds one step to the cluster and returns the state it ended in. */
private String runStep(String jar, String... args) {
ObjectNode request = JSON.createObjectNode().put("JobFlowId", clusterId);
@@ -204,7 +204,7 @@ class EmrResizeIntegrationTest {
.body(primary + ".Ec2InstanceId", equalTo("i-00000000000000001"))
.body(primary + ".PrivateDnsName", equalTo("ip-10-0-0-1.ec2.internal"))
.body("Instances.findAll { it.InstanceGroupId == '" + core + "' }.PrivateIpAddress",
equalTo(List.of("10.0.0.2", "10.0.0.3", "10.0.0.4")));
equalTo(List.of("10.1.0.1", "10.1.0.2", "10.1.0.3")));
emr("DescribeCluster", "{\"ClusterId\":\"" + id + "\"}").then().statusCode(200)
.body("Cluster.MasterPublicDnsName", equalTo("ip-10-0-0-1.ec2.internal"));
emr("TerminateJobFlows", "{\"JobFlowIds\":[\"" + id + "\"]}").then().statusCode(200);
@@ -222,7 +222,30 @@ class EmrResizeIntegrationTest {
emr("ListInstances", "{\"ClusterId\":\"" + id + "\"}").then().statusCode(200)
.body("Instances.Ec2InstanceId", equalTo(List.of("i-00000000000000002", "i-00000000000000001",
"i-00000000000000003")))
.body("Instances.PrivateIpAddress", equalTo(List.of("10.0.0.2", "10.0.0.1", "10.0.0.3")));
.body("Instances.PrivateIpAddress", equalTo(List.of("10.1.0.1", "10.0.0.1", "10.2.0.1")));
emr("TerminateJobFlows", "{\"JobFlowIds\":[\"" + id + "\"]}").then().statusCode(200);
}
@Test
@Order(10)
void anInstanceKeepsItsAddressWhileAGroupListedBeforeItResizes() {
String id = emr("RunJobFlow", "{\"Name\":\"stable-addresses\",\"Instances\":{\"KeepJobFlowAliveWhenNoSteps\":true,"
+ "\"InstanceGroups\":[{\"InstanceRole\":\"MASTER\",\"InstanceType\":\"m5.xlarge\",\"InstanceCount\":1},"
+ "{\"InstanceRole\":\"CORE\",\"InstanceType\":\"m5.xlarge\",\"InstanceCount\":1},"
+ "{\"InstanceRole\":\"TASK\",\"InstanceType\":\"m5.xlarge\",\"InstanceCount\":2}]}}")
.then().statusCode(200).extract().path("JobFlowId");
String core = emr("ListInstanceGroups", "{\"ClusterId\":\"" + id + "\"}").jsonPath()
.getString("InstanceGroups.find { it.InstanceGroupType == 'CORE' }.Id");
String task = emr("ListInstanceGroups", "{\"ClusterId\":\"" + id + "\"}").jsonPath()
.getString("InstanceGroups.find { it.InstanceGroupType == 'TASK' }.Id");
emr("ModifyInstanceGroups", "{\"ClusterId\":\"" + id + "\",\"InstanceGroups\":["
+ "{\"InstanceGroupId\":\"" + core + "\",\"InstanceCount\":2}]}").then().statusCode(200);
emr("ListInstances", "{\"ClusterId\":\"" + id + "\"}").then().statusCode(200)
.body("Instances.findAll { it.InstanceGroupId == '" + task + "' }.PrivateIpAddress",
equalTo(List.of("10.2.0.1", "10.2.0.2")))
.body("Instances.findAll { it.InstanceGroupId == '" + task + "' }.PrivateDnsName",
equalTo(List.of("ip-10-2-0-1.ec2.internal", "ip-10-2-0-2.ec2.internal")));
emr("TerminateJobFlows", "{\"JobFlowIds\":[\"" + id + "\"]}").then().statusCode(200);
}
}
@@ -7,6 +7,7 @@ import io.floci.aws.core.common.AwsException;
import io.floci.aws.core.common.ContainerTeardown;
import io.floci.aws.core.common.RegionResolver;
import io.floci.aws.services.emr.model.EmrCluster;
import io.floci.aws.services.emr.model.EmrInstanceFleet;
import io.floci.aws.services.emr.model.EmrInstanceGroup;
import io.floci.aws.services.emr.model.EmrStep;
import io.floci.aws.services.emrserverless.EmrServerlessSparkRunner;
@@ -337,6 +338,208 @@ class EmrServiceContainerStepsTest {
assertEquals("COMPLETED", service.describeStep(cluster.getId(), cluster.getSteps().get(0).getId()).getState());
}
@Test
void aClustersCoreAndTaskGroupsAreItsNodesNamedAsListInstancesNamesThem() {
fixture.clusterStorage.ownNetwork = true;
EmrCluster requested = cluster("nodes", true, sparkStep("report", "CONTINUE"));
requested.setInstanceGroups(new ArrayList<>(List.of(group("CORE", 2), group("MASTER", 1), group("TASK", 1))));
service.runJobFlow(requested, REGION);
fixture.runStepThread();
ClusterStorage.Yarn yarn = fixture.clusterStorage.yarns.get(0);
assertEquals(List.of(new ClusterStorage.Node("1-1", "ip-10-1-0-1.ec2.internal", true),
new ClusterStorage.Node("1-2", "ip-10-1-0-2.ec2.internal", true),
new ClusterStorage.Node("2-1", "ip-10-2-0-1.ec2.internal", false)), yarn.nodes());
assertEquals(1, yarn.replication());
}
@Test
void aPrimaryNodeWorksUnlessItsClusterWasCreatedWithCoreNodes() {
fixture.clusterStorage.ownNetwork = true;
EmrCluster withoutCore = cluster("task-only", true, sparkStep("report", "CONTINUE"));
withoutCore.setInstanceGroups(new ArrayList<>(List.of(group("MASTER", 1), group("TASK", 1))));
EmrCluster grown = service.runJobFlow(withoutCore, REGION);
fixture.runStepThread();
EmrCluster withCore = cluster("with-core", true, sparkStep("report", "CONTINUE"));
withCore.setInstanceGroups(new ArrayList<>(List.of(group("MASTER", 1), group("CORE", 1))));
service.runJobFlow(withCore, REGION);
fixture.runStepThread();
service.addInstanceGroups(grown.getId(), List.of(group("CORE", 2)));
fixture.runStepThread();
assertTrue(fixture.clusterStorage.yarns.get(0).primaryWorks());
assertFalse(fixture.clusterStorage.yarns.get(1).primaryWorks());
// Its DataNode holds the blocks written before, which core nodes added since do not.
ClusterStorage.Yarn withCoreNodes = fixture.clusterStorage.resizes.get(0);
assertEquals(List.of(true, true, false), withCoreNodes.nodes().stream().map(ClusterStorage.Node::core).toList());
assertTrue(withCoreNodes.primaryWorks());
}
@Test
void aFleetsUnitOfCapacityIsOneNode() {
fixture.clusterStorage.ownNetwork = true;
EmrCluster requested = cluster("fleets", true, sparkStep("report", "CONTINUE"));
requested.setInstanceFleets(new ArrayList<>(List.of(fleet("MASTER", 1, 0), fleet("CORE", 1, 1),
fleet("TASK", 0, 1))));
service.runJobFlow(requested, REGION);
fixture.runStepThread();
assertEquals(List.of(new ClusterStorage.Node("1-1", "ip-10-1-0-1.ec2.internal", true),
new ClusterStorage.Node("1-2", "ip-10-1-0-2.ec2.internal", true),
new ClusterStorage.Node("2-1", "ip-10-2-0-1.ec2.internal", false)),
fixture.clusterStorage.yarns.get(0).nodes());
}
@Test
void noMoreNodesRunThanTheSettingAllowsCoreNodesFirst() {
fixture.clusterStorage.ownNetwork = true;
EmrCluster requested = cluster("capped", true, sparkStep("report", "CONTINUE"));
requested.setInstanceGroups(new ArrayList<>(List.of(group("MASTER", 1), group("TASK", 5), group("CORE", 3))));
service.runJobFlow(requested, REGION);
fixture.runStepThread();
List<ClusterStorage.Node> nodes = fixture.clusterStorage.yarns.get(0).nodes();
assertEquals(4, nodes.size());
assertEquals(List.of(true, true, true, false), nodes.stream().map(ClusterStorage.Node::core).toList());
}
@Test
void itsHdfsKeepsTheCopiesEmrKeepsForTheCoreNodesItWasCreatedWith() {
assertEquals(1, replicationOf(3, null));
assertEquals(2, replicationOf(4, null));
assertEquals(3, replicationOf(10, null));
// Never more than the nodes that hold them.
assertEquals(2, replicationOf(2, "3"));
// "To overwrite the default value, use the hdfs-site classification."
assertEquals(3, replicationOf(3, "3"));
// One it cannot use keeps EMR's default, which its log says.
assertEquals(1, replicationOf(2, "not-a-number"));
assertEquals(2, replicationOf(4, "0"));
assertEquals(2, replicationOf(4, "-1"));
}
private int replicationOf(int coreNodes, String configured) {
EmrServiceFixture own = EmrServiceFixture.docker(runner, null);
own.clusterStorage.ownNetwork = true;
EmrCluster requested = cluster("copies", true, sparkStep("report", "CONTINUE"));
requested.setInstanceGroups(new ArrayList<>(List.of(group("MASTER", 1), group("CORE", coreNodes))));
if (configured != null) {
requested.setConfigurations("[{\"Classification\":\"hdfs-site\",\"Properties\":"
+ "{\"dfs.replication\":\"" + configured + "\"}}]");
}
own.service.runJobFlow(requested, REGION);
own.runStepThread();
return own.clusterStorage.yarns.get(0).replication();
}
@Test
void itsCoreNodesAreNotShrunkBelowTheCopiesItsHdfsKeeps() {
fixture.clusterStorage.ownNetwork = true;
EmrCluster requested = cluster("floor", true, sparkStep("report", "CONTINUE"));
requested.setInstanceGroups(new ArrayList<>(List.of(group("MASTER", 1), group("CORE", 4))));
EmrCluster cluster = service.runJobFlow(requested, REGION);
fixture.runStepThread();
String core = cluster.getInstanceGroups().get(1).getId();
service.modifyInstanceGroups(cluster.getId(), REGION,
List.of(new EmrService.InstanceGroupModification(core, 1, null)));
fixture.runStepThread();
assertEquals(2, fixture.clusterStorage.resizes.get(0).nodes().size());
assertEquals(2, fixture.clusterStorage.resizes.get(0).replication());
}
@Test
void aResizeReachesTheNodesOnceAStepHasStartedThem() {
fixture.clusterStorage.ownNetwork = true;
EmrCluster requested = cluster("resize", true, sparkStep("report", "CONTINUE"));
requested.setInstanceGroups(new ArrayList<>(List.of(group("MASTER", 1), group("CORE", 1), group("TASK", 1))));
EmrCluster cluster = service.runJobFlow(requested, REGION);
fixture.runStepThread();
String task = cluster.getInstanceGroups().get(2).getId();
service.modifyInstanceGroups(cluster.getId(), REGION,
List.of(new EmrService.InstanceGroupModification(task, 3, null)));
fixture.runStepThread();
assertEquals(4, fixture.clusterStorage.resizes.get(0).nodes().size());
EmrInstanceGroup added = group("TASK", 1);
service.addInstanceGroups(cluster.getId(), List.of(added));
fixture.runStepThread();
assertEquals(4, fixture.clusterStorage.resizes.get(1).nodes().size());
assertEquals(new ClusterStorage.Node("2-3", "ip-10-2-0-3.ec2.internal", false),
fixture.clusterStorage.resizes.get(1).nodes().get(3));
}
@Test
void aResizeOfAFleetReachesItsNodesToo() {
fixture.clusterStorage.ownNetwork = true;
EmrCluster requested = cluster("fleet-resize", true, sparkStep("report", "CONTINUE"));
requested.setInstanceFleets(new ArrayList<>(List.of(fleet("MASTER", 1, 0), fleet("CORE", 1, 0))));
EmrCluster cluster = service.runJobFlow(requested, REGION);
fixture.runStepThread();
String core = cluster.getInstanceFleets().get(1).getId();
service.modifyInstanceFleet(cluster.getId(), core, 2, null);
fixture.runStepThread();
assertEquals(2, fixture.clusterStorage.resizes.get(0).nodes().size());
service.addInstanceFleet(cluster.getId(), fleet("TASK", 1, 0));
fixture.runStepThread();
assertEquals(3, fixture.clusterStorage.resizes.get(1).nodes().size());
}
@Test
void aResizeBeforeAStepHasStartedTheNodesIsLeftToThatStep() {
fixture.clusterStorage.ownNetwork = true;
EmrCluster requested = cluster("early", true);
requested.setInstanceGroups(new ArrayList<>(List.of(group("MASTER", 1), group("CORE", 1))));
EmrCluster cluster = service.runJobFlow(requested, REGION);
String core = cluster.getInstanceGroups().get(1).getId();
service.modifyInstanceGroups(cluster.getId(), REGION,
List.of(new EmrService.InstanceGroupModification(core, 2, null)));
assertEquals(0, fixture.stepThreadsWaiting());
assertEquals(List.of(), fixture.clusterStorage.resizes);
}
@Test
void aStepQueuedBeforeAResizeStartsTheNodesItAsksForByThen() {
fixture.clusterStorage.ownNetwork = true;
EmrCluster requested = cluster("queued", true, sparkStep("report", "CONTINUE"));
requested.setInstanceGroups(new ArrayList<>(List.of(group("MASTER", 1), group("CORE", 1))));
EmrCluster cluster = service.runJobFlow(requested, REGION);
String core = cluster.getInstanceGroups().get(1).getId();
// The step's thread has not run yet.
service.modifyInstanceGroups(cluster.getId(), REGION,
List.of(new EmrService.InstanceGroupModification(core, 3, null)));
fixture.runStepThread();
assertEquals(3, fixture.clusterStorage.yarns.get(0).nodes().size());
}
@Test
void aClusterThatDoesNotRunYarnFollowsNoResize() {
fixture.clusterStorage.ownNetwork = false;
EmrCluster requested = cluster("no-yarn", true, sparkStep("report", "CONTINUE"));
requested.setInstanceGroups(new ArrayList<>(List.of(group("MASTER", 1), group("TASK", 1))));
EmrCluster cluster = service.runJobFlow(requested, REGION);
fixture.runStepThread();
String task = cluster.getInstanceGroups().get(1).getId();
service.modifyInstanceGroups(cluster.getId(), REGION,
List.of(new EmrService.InstanceGroupModification(task, 2, null)));
assertEquals(0, fixture.stepThreadsWaiting());
assertEquals(List.of(), fixture.clusterStorage.resizes);
}
@Test
void aStepStartsItsClustersHdfsOnItsPrimaryNodeFromTheReleasesHiveImage() {
EmrCluster requested = cluster("hdfs", true, sparkStep("report", "CONTINUE"), sparkStep("next", "CONTINUE"));
@@ -483,7 +686,8 @@ class EmrServiceContainerStepsTest {
SparkJobRunner.Job job = runner.jobs.get(0);
assertEquals(EmrServiceFixture.RecordingReleaseImages.IMAGE, job.imageTemplate());
assertEquals(EmrServiceFixture.RecordingReleaseImages.IMAGE, fixture.clusterStorage.hdfs.get(0).get(2));
assertEquals(List.of(new ClusterStorage.Yarn(4096, 4, "/opt")), fixture.clusterStorage.yarns);
assertEquals(List.of(new ClusterStorage.Yarn(4096, 4).withHadoopFolder("/opt")),
fixture.clusterStorage.yarns);
assertEquals(hdfsStorage(cluster).withYarn("ip-10-0-0-1.ec2.internal",
EmrServiceFixture.RecordingStorage.FLOCI_ON_NETWORK), job.storage());
}
@@ -1477,6 +1681,14 @@ class EmrServiceContainerStepsTest {
return cluster;
}
private static EmrInstanceFleet fleet(String type, int onDemand, int spot) {
EmrInstanceFleet fleet = new EmrInstanceFleet();
fleet.setInstanceFleetType(type);
fleet.setTargetOnDemandCapacity(onDemand);
fleet.setTargetSpotCapacity(spot);
return fleet;
}
private static EmrInstanceGroup group(String type, int count) {
EmrInstanceGroup group = new EmrInstanceGroup();
group.setInstanceGroupType(type);
@@ -21,6 +21,7 @@ import java.util.Optional;
import java.util.Set;
import java.util.function.BooleanSupplier;
import java.util.function.Consumer;
import java.util.function.Supplier;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.mockito.ArgumentMatchers.anyString;
@@ -78,6 +79,8 @@ final class EmrServiceFixture {
Consumer<BooleanSupplier> whileStartingHdfs = stopped -> { };
final List<String> removed = new ArrayList<>();
final List<Set<String>> kept = new ArrayList<>();
// Each resize asked for: its cluster and the nodes it wants.
final List<ClusterStorage.Yarn> resizes = new ArrayList<>();
boolean refuse;
Runnable whileCreating = () -> { };
@@ -97,6 +100,11 @@ final class EmrServiceFixture {
return ownNetwork;
}
@Override
public void resize(String clusterId, Supplier<Yarn> yarn) {
resizes.add(yarn.get());
}
@Override
public void archiveContainerLogs(String clusterId, String accountId, String region, String logFolder) {
containerLogs.add(List.of(clusterId, logFolder));
@@ -104,7 +112,8 @@ final class EmrServiceFixture {
@Override
public SparkJobRunner.Storage hdfs(String clusterId, String accountId, String region, String host,
String image, String dockerNetwork, HiveMetastore hiveMetastore, Yarn yarn,
String image, String dockerNetwork, HiveMetastore hiveMetastore,
Supplier<Yarn> yarn,
BooleanSupplier stopped) {
hdfs.add(Arrays.asList(clusterId, host, image, dockerNetwork, String.valueOf(hiveMetastore != null),
hiveMetastore != null ? hiveMetastore.role() : null));
@@ -112,7 +121,7 @@ final class EmrServiceFixture {
metastores.add(hiveMetastore);
}
if (yarn != null) {
yarns.add(yarn);
yarns.add(yarn.get());
}
whileStartingHdfs.accept(stopped);
if (hdfsFailure != null) {
@@ -271,6 +280,7 @@ final class EmrServiceFixture {
when(config.services().emr().dockerNetwork()).thenReturn(Optional.empty());
when(config.services().emr().yarnNodeMemoryMb()).thenReturn(4096);
when(config.services().emr().yarnNodeVcores()).thenReturn(4);
when(config.services().emr().yarnMaxWorkerNodes()).thenReturn(4);
when(config.services().emr().reconcileContainersOnStartup()).thenReturn(reconcile);
return config;
}
@@ -617,6 +617,23 @@ class EmrServerlessSparkRunnerTest {
"s3://bucket/jobs/report.py"), arguments.subList(20, arguments.size()));
}
@Test
void aStepWritesTheCopiesOfABlockItsCoreNodesKeep() {
SparkJobRunner.Job job = stepJob(Map.of()).onStorage(STORAGE.withReplication(2));
assertTrue(EmrServerlessSparkRunner.sparkSubmitArguments(job, "http://floci:4566", List.of())
.contains("spark.hadoop.dfs.replication=2"));
assertTrue(EmrServerlessSparkRunner.hiveArguments(job, "http://floci:4566").contains("dfs.replication=2"));
}
@Test
void aStepOnAClusterWhoseHdfsIsOnItsPrimaryNodeLeavesTheCopiesOfABlockAlone() {
SparkJobRunner.Job job = stepJob(Map.of()).onStorage(STORAGE);
assertFalse(EmrServerlessSparkRunner.sparkSubmitArguments(job, "http://floci:4566", List.of()).toString()
.contains("dfs.replication"));
}
@Test
void aStepOnItsClustersYarnSubmitsToItsResourceManagerWithExecutorsSizedForItsNode() {
SparkJobRunner.Job job = stepJob(Map.of("spark.executor.memory", "2g"))
@@ -714,6 +731,34 @@ class EmrServerlessSparkRunnerTest {
assertTrue(arguments.indexOf(glue) > metastore, arguments.toString());
}
@Test
void aHadoopCommandOnAClusterWhoseCoreNodesHoldItsHdfsWritesTheirCopies() {
ContainerBuilder builder = mock(ContainerBuilder.class);
ContainerBuilder.Builder spec = mock(ContainerBuilder.Builder.class, RETURNS_SELF);
when(builder.newContainer(anyString())).thenReturn(spec);
List<String> command = List.of("bash", "-c", "hadoop fs -put data.csv /input/");
runner(builder).run(commandJob(command, Map.of(), STORAGE.withReplication(2)), progress);
// In hdfs-site.xml, as a client reads it after core-site.xml and the image's says two.
verify(spec).withEnv("FLOCI_HDFS_SITE", "<property><name>dfs.replication</name><value>2</value></property>");
verify(spec).withEntrypoint(argThat(entrypoint -> entrypoint.get(2).contains(String.join("\n",
" if [ -n \"${FLOCI_HDFS_SITE:-}\" ]; then",
" site hdfs-site.xml \"$FLOCI_HDFS_SITE\"",
" fi"))));
}
@Test
void aHadoopCommandOnAClusterWhosePrimaryNodeHoldsItsHdfsLeavesTheImagesCopies() {
ContainerBuilder builder = mock(ContainerBuilder.class);
ContainerBuilder.Builder spec = mock(ContainerBuilder.Builder.class, RETURNS_SELF);
when(builder.newContainer(anyString())).thenReturn(spec);
runner(builder).run(commandJob(List.of("bash", "-c", "true"), Map.of(), STORAGE), progress);
verify(spec, never()).withEnv(eq("FLOCI_HDFS_SITE"), anyString());
}
@Test
void aCommandOnAClusterGivesItsHadoopCommandTheClustersHdfsToo() {
ContainerBuilder builder = mock(ContainerBuilder.class);
@@ -1465,6 +1510,21 @@ class EmrServerlessSparkRunnerTest {
verify(lifecycle, never()).removeIfExists("another-deployments");
}
@Test
void aNodeOfALiveClusterIsNoDriverTheSweepRemoves() {
EmrServerlessSparkRunner runner = runner();
when(config.port()).thenReturn(4566);
Container node = mock(Container.class);
when(node.getId()).thenReturn("node");
when(node.getLabels()).thenReturn(Map.of(ContainerStorageHelper.SERVICE_LABEL, "emr",
ContainerStorageHelper.OWNER_LABEL, "4566", ContainerStorageHelper.COMPONENT_LABEL, "emr-node"));
when(docker.listContainersCmd().withShowAll(true).withLabelFilter(anyMap()).exec()).thenReturn(List.of(node));
runner.removeLeftoverDrivers("emr");
verify(lifecycle, never()).removeIfExists("node");
}
@Test
void theHdfsOfALiveClusterIsNoDriverTheSweepRemoves() {
EmrServerlessSparkRunner runner = runner();