mirror of
https://github.com/floci-io/floci.git
synced 2026-12-05 19:18:01 -05:00
feat(emr): write the node files AWS documents on a cluster's nodes (#8473)
An EMR node keeps files about itself that bootstrap actions read to tell which node they run on. AWS documents two fields of them: the EMR Management Guide's bootstrap action reads isMaster from /mnt/var/lib/info/instance.json, and AWS's Knowledge Center reads instanceRole from /mnt/var/lib/instance-controller/extraInstanceData.json with jq, comparing it, lowercased, with master, core and task. Floci now writes those two fields, and nothing else: isMaster true and instanceRole MASTER in the bootstrap actions' container and each step's, which run as on the primary node, and in the primary node's own, and false with CORE or TASK in each core or task node's, the roles as the API's InstanceRoleType names them. They are written as the image's hadoop user, whose /mnt is, so Hive's metastore can still write under /mnt/var/lib, and moved into place whole, as steps share /mnt. The bootstrap container's copies go before it is kept as the cluster's image. Files that cannot be written fail the bootstrap, the step or the node.
This commit is contained in:
1 parent
f491a77dba
commit
f71476037a
7 files changed
+164
-19
No files matched your search
@@ -60,6 +60,7 @@ public class EmrBootstrapRunner implements BootstrapRunner, ContainerTeardown {
|
||||
private static final int STOP_TIMEOUT_SECONDS = 0;
|
||||
// No source gives a bootstrap action a time limit: it runs until it ends, or its cluster does.
|
||||
private static final int NO_TIMEOUT = Integer.MAX_VALUE;
|
||||
private static final int NODE_INFO_TIMEOUT_SECONDS = 60;
|
||||
// What each action runs as root, with the sudo a step has too (see EmrServerlessSparkRunner.SUDO), whose options
|
||||
// the action, already root, has no need of: $1 is its script, and the rest that script's arguments. The JVMs it
|
||||
// starts trust Floci's CA once the container has its trust store.
|
||||
@@ -79,8 +80,11 @@ public class EmrBootstrapRunner implements BootstrapRunner, ContainerTeardown {
|
||||
"exit $code");
|
||||
// Run once every action has: what only the actions needed goes before the image is kept, the trust store
|
||||
// included, which each step's container gets afresh.
|
||||
// The primary node's files, which say which node it is, go too, so a node started from the image writes its own.
|
||||
private static final String CLEANUP_COMMAND = "rm -f /tmp/floci-bin/sudo /tmp/floci-bootstrap-action-* "
|
||||
+ ContainerCaBundle.JAVA_TRUST_STORE_PATH + "; rmdir /tmp/floci-bin";
|
||||
+ ContainerCaBundle.JAVA_TRUST_STORE_PATH + " " + EmrServerlessSparkRunner.INSTANCE_FILE + " "
|
||||
+ EmrServerlessSparkRunner.EXTRA_INSTANCE_DATA_FILE + "; rmdir /tmp/floci-bin;"
|
||||
+ " rmdir /mnt/var/lib/info /mnt/var/lib/instance-controller 2>/dev/null";
|
||||
|
||||
// The bootstraps inside run(); a stop for one not here comes after its container has gone, and is dropped.
|
||||
private final ContainerLaunches launches = new ContainerLaunches();
|
||||
@@ -144,6 +148,8 @@ public class EmrBootstrapRunner implements BootstrapRunner, ContainerTeardown {
|
||||
if (launch.stopRequested() || cancelled.getAsBoolean()) {
|
||||
return Outcome.halted();
|
||||
}
|
||||
// The actions run on the primary node, whose files say so, as the Management Guide's own action reads them.
|
||||
writeNodeInfo(containerId);
|
||||
List<String> env = awsEnv.sdkBaselineEnv(bootstrap.region(), Optional.empty(), credentials,
|
||||
bootstrap.accountId());
|
||||
for (int number = 1; number <= bootstrap.actions().size(); number++) {
|
||||
@@ -176,6 +182,17 @@ public class EmrBootstrapRunner implements BootstrapRunner, ContainerTeardown {
|
||||
}
|
||||
}
|
||||
|
||||
private void writeNodeInfo(String containerId) {
|
||||
ContainerExec.Result written = ContainerExec.run(lifecycleManager.getDockerClient(), containerId,
|
||||
new String[] {"env", EmrServerlessSparkRunner.INSTANCE_ROLE_VARIABLE + "=MASTER", "bash", "-c",
|
||||
EmrServerlessSparkRunner.NODE_INFO}, NODE_INFO_TIMEOUT_SECONDS);
|
||||
if (written.exitCode() != 0) {
|
||||
String why = written.stderr().strip();
|
||||
throw new IllegalStateException("Could not write the primary node's "
|
||||
+ EmrServerlessSparkRunner.INSTANCE_FILE + (why.isEmpty() ? "" : ": " + why));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs action {@code number} in the container, its output going to Floci's log and to the action's log
|
||||
* folder; null when it succeeded, or how it failed.
|
||||
|
||||
@@ -168,10 +168,12 @@ public class EmrClusterStorage implements ClusterStorage, ContainerTeardown {
|
||||
+ " HADOOP_HDFS_HOME=$h/hadoop-hdfs HADOOP_MAPRED_HOME=$h/hadoop-mapreduce"
|
||||
+ " HADOOP_YARN_HOME=$h/hadoop-yarn PATH=$h/hadoop-hdfs/bin:$h/hadoop-yarn/bin:$h/hadoop/bin:$PATH",
|
||||
"fi");
|
||||
// What every node of a cluster writes before its daemons start: the Hadoop configuration, its name among the
|
||||
// nodes in HDFS's when it is given one, and the empty files listing the nodes being decommissioned.
|
||||
// What every node of a cluster writes before its daemons start: the files that say which node it is, the Hadoop
|
||||
// configuration, its name among the nodes in HDFS's when it is given one, and the empty files listing the nodes
|
||||
// being decommissioned.
|
||||
private static final String NODE_SETUP = String.join("\n",
|
||||
"chown hadoop:hadoop /mnt || exit",
|
||||
EmrServerlessSparkRunner.NODE_INFO,
|
||||
EmrServerlessSparkRunner.HIVE_ONE_SDK,
|
||||
"mkdir -p " + HDFS_CONF + " || exit",
|
||||
"printf '%s\\n' '<configuration>'"
|
||||
@@ -832,6 +834,7 @@ public class EmrClusterStorage implements ClusterStorage, ContainerTeardown {
|
||||
.withName(name)
|
||||
.withUser("root")
|
||||
.withEntrypoint(List.of("/bin/bash", "-c", NODE_SCRIPT))
|
||||
.withEnv(EmrServerlessSparkRunner.INSTANCE_ROLE_VARIABLE, core ? "CORE" : "TASK")
|
||||
.withEnv("FLOCI_HDFS_HOST", request.host())
|
||||
.withEnv("FLOCI_HDFS_BIND", "0.0.0.0")
|
||||
.withEnv(NODE_HOST_VARIABLE, host)
|
||||
@@ -1247,6 +1250,7 @@ public class EmrClusterStorage implements ClusterStorage, ContainerTeardown {
|
||||
.withName(name)
|
||||
.withUser("root")
|
||||
.withEntrypoint(List.of("/bin/bash", "-c", HDFS_SCRIPT))
|
||||
.withEnv(EmrServerlessSparkRunner.INSTANCE_ROLE_VARIABLE, "MASTER")
|
||||
.withEnv("FLOCI_HDFS_HOST", host)
|
||||
.withEnv("FLOCI_HDFS_BIND", shared ? LOOPBACK : "0.0.0.0")
|
||||
.withEnv("HADOOP_CONF_DIR", HDFS_CONF)
|
||||
|
||||
@@ -177,6 +177,35 @@ public class EmrServerlessSparkRunner implements SparkJobRunner, ContainerTeardo
|
||||
"exec env \"$@\"");
|
||||
/** Writes {@link #SUDO} to /tmp/floci-bin/sudo, a folder that is there already. */
|
||||
public static final String SUDO_FILE = String.join("\n", "cat > /tmp/floci-bin/sudo <<'SUDO'", SUDO, "SUDO");
|
||||
/** The variable naming the instance role of an EMR cluster's container, MASTER, CORE or TASK (see NODE_INFO). */
|
||||
public static final String INSTANCE_ROLE_VARIABLE = "FLOCI_INSTANCE_ROLE";
|
||||
/** The file an EMR node's {@code isMaster} is in, which the EMR Management Guide's bootstrap action reads. */
|
||||
public static final String INSTANCE_FILE = "/mnt/var/lib/info/instance.json";
|
||||
/** The file an EMR node's {@code instanceRole} is in, which AWS's Knowledge Center reads with jq. */
|
||||
public static final String EXTRA_INSTANCE_DATA_FILE = "/mnt/var/lib/instance-controller/extraInstanceData.json";
|
||||
// Run as the image's user with the node's role as $1: each file written whole, then moved in place, so a step
|
||||
// starting beside another, which shares its /mnt, reads it whole too.
|
||||
private static final String NODE_INFO_FILES = String.join("\n",
|
||||
"w() { t=$(mktemp \"$1.XXXXXX\") && printf \"%s\\n\" \"$2\" > \"$t\" && chmod 644 \"$t\""
|
||||
+ " && mv -f \"$t\" \"$1\"; }",
|
||||
"m=false; [ \"$1\" != MASTER ] || m=true",
|
||||
"mkdir -p /mnt/var/lib/info /mnt/var/lib/instance-controller"
|
||||
+ " && w " + INSTANCE_FILE + " \"{\\\"isMaster\\\": $m}\""
|
||||
+ " && w " + EXTRA_INSTANCE_DATA_FILE + " \"{\\\"instanceRole\\\": \\\"$1\\\"}\"");
|
||||
/**
|
||||
* Where {@link #INSTANCE_ROLE_VARIABLE} names a role, writes what AWS documents of the files an EMR node keeps
|
||||
* about itself, and only that: {@link #INSTANCE_FILE}'s {@code isMaster}, as the EMR Management Guide's bootstrap
|
||||
* action reads it ("grep isMaster /mnt/var/lib/info/instance.json | grep false" on a node that is not the primary),
|
||||
* and {@link #EXTRA_INSTANCE_DATA_FILE}'s {@code instanceRole}, which AWS's Knowledge Center reads with jq and
|
||||
* compares, lowercased, with master, core and task, given as the API's {@code InstanceRoleType} names it. Written
|
||||
* as the image's user, whose /mnt is, so a folder made there is that user's too, as Hive's metastore, which writes
|
||||
* under /mnt/var/lib, runs as that user.
|
||||
*/
|
||||
public static final String NODE_INFO = String.join("\n",
|
||||
"if [ -n \"${" + INSTANCE_ROLE_VARIABLE + ":-}\" ]; then",
|
||||
" as=; [ \"$(id -u)\" != 0 ] || as=\"setpriv --reuid=hadoop --regid=hadoop --clear-groups\"",
|
||||
" $as bash -c '" + NODE_INFO_FILES + "' floci \"$" + INSTANCE_ROLE_VARIABLE + "\" || exit",
|
||||
"fi");
|
||||
// A Spark configuration of the container's own: a copy of the folder a spark-env export of SPARK_CONF_DIR names,
|
||||
// else of the image's, links followed, whose log4j2.properties ends with the job's lines, passed in
|
||||
// SPARK_LOG4J2_VARIABLE, a later line winning. What is added to a file starts on a line of its own, as the
|
||||
@@ -377,6 +406,7 @@ public class EmrServerlessSparkRunner implements SparkJobRunner, ContainerTeardo
|
||||
" chmod 755 /tmp/floci-bin/sudo",
|
||||
" export PATH=\"/tmp/floci-bin:$PATH\"",
|
||||
"fi",
|
||||
NODE_INFO,
|
||||
HIVE_ONE_SDK,
|
||||
"export HOME=~" + IMAGE_USER,
|
||||
"cd \"$HOME\" || exit",
|
||||
@@ -510,8 +540,10 @@ public class EmrServerlessSparkRunner implements SparkJobRunner, ContainerTeardo
|
||||
container.withEnv(SPARK_LOG4J2_VARIABLE, sparkLog4j2File(job));
|
||||
}
|
||||
if (job.storage() != null) {
|
||||
// A step runs on its cluster's primary node, whose files say so.
|
||||
container.withNamedVolume(job.storage().home(), "/home/hadoop")
|
||||
.withNamedVolume(job.storage().mnt(), "/mnt");
|
||||
.withNamedVolume(job.storage().mnt(), "/mnt")
|
||||
.withEnv(INSTANCE_ROLE_VARIABLE, "MASTER");
|
||||
// The primary node's name leads to its HDFS, as the cluster's DNS resolves it.
|
||||
if (job.storage().hdfsHost() != null) {
|
||||
container.withExtraHost(job.storage().hdfsHost(), job.storage().hdfsAddress());
|
||||
@@ -1300,7 +1332,7 @@ public class EmrServerlessSparkRunner implements SparkJobRunner, ContainerTeardo
|
||||
// A wrapper's script: the lines of each part, in order, then what it runs.
|
||||
@SafeVarargs
|
||||
private static String wrapper(List<String>... parts) {
|
||||
List<String> lines = new ArrayList<>();
|
||||
List<String> lines = new ArrayList<>(List.of(NODE_INFO));
|
||||
for (List<String> part : parts) {
|
||||
lines.addAll(part);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user