feat(emr): run a MapReduce JobHistory server on a cluster's primary node (#8662)

EMR's primary node runs one (a release's mapred-site classification
"Restarts Hadoop MapReduce-HistoryServer"); Floci's did not, so a
client asking about a MapReduce job whose application master had ended,
for its counters or task reports, waited about 30 seconds a call on
0.0.0.0:10020 and got nothing. Pig asks after each of its MapReduce
jobs, so #8636 turned its task reports off and gave its client one try.

A cluster that runs YARN now starts `mapred historyserver` beside the
ResourceManager, on Hadoop's own ports and where HDFS listens, from the
jobs' history in the cluster's HDFS, and its primary node is ready once
the server answers. A step's MapReduce settings name it, and so do a
Hive step's, for a query run on MapReduce; an emr-spark-* node runs it
from the Hadoop it keeps apart. The node scripts' readiness waits share
one helper. Pig's
workaround goes: its MapReduce jobs' counters come from the server, as
on EMR.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-11-16 18:09:21 -04:00
1 parent 5ea7186549
commit 6e30baf46c
9 files changed
+127 -81

No files matched your search

@@ -134,6 +134,9 @@ public class EmrClusterStorage implements ClusterStorage, ContainerTeardown {
// The ResourceManager's cluster metrics on its web port, which say when the NodeManager has registered.
private static final String RESOURCE_MANAGER_METRICS = "http://" + BIND + ":8088/ws/v1/cluster/metrics";
private static final String RESOURCE_MANAGER_NODES = "http://" + BIND + ":8088/ws/v1/cluster/nodes?states=";
// The MapReduce JobHistory server's information on its web port, which says it has started.
private static final String JOB_HISTORY_INFO = "http://" + BIND + ":"
+ EmrServerlessSparkRunner.JOB_HISTORY_WEB_PORT + "/ws/v1/history/info";
// Where a NodeManager keeps its containers' logs, on the node's volume, as <application>/<container>/<file>.
private static final String YARN_LOGS = "/mnt/yarn/logs";
private static final int LOG_LISTING_SECONDS = 30;
@@ -166,7 +169,8 @@ public class EmrClusterStorage implements ClusterStorage, ContainerTeardown {
" h=$" + HADOOP_FOLDER_VARIABLE,
" export HADOOP_HOME=$h/hadoop HADOOP_COMMON_HOME=$h/hadoop HADOOP_LIBEXEC_DIR=$h/hadoop/libexec"
+ " 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",
+ " HADOOP_YARN_HOME=$h/hadoop-yarn"
+ " PATH=$h/hadoop-hdfs/bin:$h/hadoop-yarn/bin:$h/hadoop-mapreduce/bin:$h/hadoop/bin:$PATH",
"fi");
// 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
@@ -227,6 +231,13 @@ public class EmrClusterStorage implements ClusterStorage, ContainerTeardown {
"yarn.scheduler.capacity.root.default.maximum-capacity", "100",
"yarn.scheduler.capacity.maximum-am-resource-percent", "0.5"))
+ " '</configuration>' > " + HDFS_CONF + "/capacity-scheduler.xml || exit",
" printf '%s\\n' '<configuration>'" + yarnProperties(List.of(
"mapreduce.jobhistory.address", "$FLOCI_HDFS_HOST:" + EmrServerlessSparkRunner.JOB_HISTORY_PORT,
"mapreduce.jobhistory.webapp.address",
"$FLOCI_HDFS_HOST:" + EmrServerlessSparkRunner.JOB_HISTORY_WEB_PORT,
"mapreduce.jobhistory.admin.address", "$FLOCI_HDFS_HOST:10033",
"mapreduce.jobhistory.bind-host", "$FLOCI_HDFS_BIND"))
+ " '</configuration>' > " + HDFS_CONF + "/mapred-site.xml || exit",
"fi",
"printf '%s\\n' 'log4j.rootLogger=WARN,console' 'log4j.appender.console=org.apache.log4j.ConsoleAppender'"
+ " 'log4j.appender.console.layout=org.apache.log4j.PatternLayout'"
@@ -239,26 +250,28 @@ public class EmrClusterStorage implements ClusterStorage, ContainerTeardown {
// and a DataNode with their data in it, the NameNode formatted the first time, with one copy of each block and
// without permissions, listening on FLOCI_HDFS_BIND: every address of the container, on the cluster's own network,
// or, in a namespace the steps share, the host's or a container's, only its loopback, which its name leads to, so
// no other interface serves files anyone could change; in a container's, the hosts file the name goes in is the
// one the namespace's containers share, the steps' too. The other daemons start with them, as none needs the HDFS
// to start. With HIVE_METASTORE_VARIABLE set, Hive's metastore service does, with the Hive image's Hadoop
// no other interface serves files anyone could change; in a container's, the hosts file the name goes in is the one
// the namespace's containers share, the steps' too. The other daemons start with them, as none needs the HDFS to
// start. With HIVE_METASTORE_VARIABLE set, Hive's metastore service does, with the Hive image's Hadoop
// configuration, which the node's image keeps at HIVE_HADOOP_CONF, as Hive steps run, its settings after it: "Hive
// records metastore information in a MySQL database on the primary node's file system", kept as a Derby database
// in /mnt, which only the service opens, so any number of steps share it, its log in /tmp, and its warehouse in
// the HDFS; the cluster's core-site and hive-site, last, replace any of them, as on EMR's primary node. With
// records metastore information in a MySQL database on the primary node's file system", kept as a Derby database in
// /mnt, which only the service opens, so any number of steps share it, its log in /tmp, and its warehouse in the
// HDFS; the cluster's core-site and hive-site, last, replace any of them, as on EMR's primary node. With
// YARN_MEMORY_VARIABLE set, YARN does, as "the primary node runs the YARN ResourceManager service": a
// ResourceManager and, as on a cluster of one node, a NodeManager, which offers that much memory and
// YARN_VCORES_VARIABLE's cores to the containers it runs the steps' Spark executors in, its local and log folders
// in /mnt, Java 17 opening to both the packages they reach into. Where the cluster's core nodes hold them
// (NODE_WORKERS_VARIABLE), the node runs no DataNode or NodeManager, but a DataNode on a volume that holds one's
// data, so the blocks it kept stay in reach. Once the NameNode on its port is this container's own, its ClusterId
// the one its format gave it, rather than another cluster's in that namespace, the DataNode has joined and the
// NameNode has left safe mode, which on a restart waits for the blocks the volume holds, the folders EMR's HDFS has
// for steps, /tmp and the hadoop user's home, are made, and the metastore's warehouse and HIVE_JARS, which Hive's
// exec jar goes in. HDFS_READY then says the node is ready, once the metastore takes connections and the
// NodeManager has registered. A daemon that exits, before or after, the metastore among them, ends the container,
// its readiness gone, so the next step starts it again on its data. With HADOOP_FOLDER_VARIABLE set, every daemon
// runs from the Hadoop in that folder.
// in /mnt, and the MapReduce JobHistory server EMR runs too (a release's mapred-site "Restarts Hadoop
// MapReduce-HistoryServer"), which a client asks about a MapReduce job whose application master has ended, the
// jobs' history in the HDFS; Java 17 opening to each the packages they reach into. Where the cluster's core nodes
// hold them (NODE_WORKERS_VARIABLE), the node runs no DataNode or NodeManager, but a DataNode on a volume that
// holds one's data, so the blocks it kept stay in reach. Once the NameNode on its port is this container's own, its
// ClusterId the one its format gave it, rather than another cluster's in that namespace, the DataNode has joined
// and the NameNode has left safe mode, which on a restart waits for the blocks the volume holds, the folders EMR's
// HDFS has for steps, /tmp and the hadoop user's home, are made, and the metastore's warehouse and HIVE_JARS, which
// Hive's exec jar goes in. HDFS_READY then says the node is ready, once the metastore takes connections, the
// NodeManager has registered and the JobHistory server answers. A daemon that exits, before or after, the metastore
// among them, ends the container, its readiness gone, so the next step starts it again on its data. With
// HADOOP_FOLDER_VARIABLE set, every daemon runs from the Hadoop in that folder.
// Where a node's daemons listen: FLOCI_HDFS_BIND, or, with SUBNET_VARIABLE set, the node's own address in that subnet,
// its cluster's network, alone, so another network it joins does not reach its HDFS or its YARN; written to
// BIND_FILE. Hive 3's metastore has no setting for the address it listens on, so it takes every one.
@@ -309,28 +322,25 @@ public class EmrClusterStorage implements ClusterStorage, ContainerTeardown {
+ "JDK_JAVA_OPTIONS=\"" + yarnOpens() + "\" yarn resourcemanager & n=$((n + 1)); "
+ "if [ -z \"$" + NODE_WORKERS_VARIABLE + "\" ]; then "
+ "JDK_JAVA_OPTIONS=\"" + yarnOpens() + "\" yarn nodemanager & n=$((n + 1)); fi; "
+ "JDK_JAVA_OPTIONS=\"" + yarnOpens() + "\" mapred historyserver & n=$((n + 1)); "
+ "fi; "
+ "until curl -sf -m 5 \"" + NAMENODE_JMX + "NameNodeInfo\""
+ " | grep -q \"\\\"ClusterId\\\" : \\\"$cid\\\"\"; do "
+ "[ \"$(jobs -rp | wc -l)\" = $n ] || exit 1; sleep 0.2; done; "
+ "until curl -sf -m 5 \"" + NAMENODE_JMX + "FSNamesystemState\""
+ " | grep -q \"\\\"NumLiveDataNodes\\\" : [1-9]\"; do "
+ "[ \"$(jobs -rp | wc -l)\" = $n ] || exit 1; sleep 0.2; done; "
+ "until curl -sf -m 5 \"" + NAMENODE_JMX + "NameNodeInfo\""
+ " | grep -q \"\\\"Safemode\\\" : \\\"\\\"\"; do "
+ "[ \"$(jobs -rp | wc -l)\" = $n ] || exit 1; sleep 0.2; done; "
+ untilReady("curl -sf -m 5 \"" + NAMENODE_JMX + "NameNodeInfo\""
+ " | grep -q \"\\\"ClusterId\\\" : \\\"$cid\\\"\"")
+ untilReady("curl -sf -m 5 \"" + NAMENODE_JMX + "FSNamesystemState\""
+ " | grep -q \"\\\"NumLiveDataNodes\\\" : [1-9]\"")
+ untilReady("curl -sf -m 5 \"" + NAMENODE_JMX + "NameNodeInfo\""
+ " | grep -q \"\\\"Safemode\\\" : \\\"\\\"\"")
+ "hdfs dfs -mkdir -p $dirs || exit 1; "
+ "if [ -n \"$" + HIVE_METASTORE_VARIABLE + "\" ]; then "
+ "jar=$(ls /usr/lib/hive/lib/hive-exec-[0-9]*.jar | head -1); "
+ "hdfs dfs -put -f \"$jar\" " + HIVE_JARS
+ "/$(basename \"$jar\" .jar)-$(sha256sum \"$jar\" | cut -d\" \" -f1).jar || exit 1; "
+ "until (exec 3<>/dev/tcp/127.0.0.1/" + HIVE_METASTORE_PORT + ") 2>/dev/null; do "
+ "[ \"$(jobs -rp | wc -l)\" = $n ] || exit 1; sleep 0.2; done; "
+ untilReady("(exec 3<>/dev/tcp/127.0.0.1/" + HIVE_METASTORE_PORT + ") 2>/dev/null")
+ "fi; "
+ "if [ -n \"$" + YARN_MEMORY_VARIABLE + "\" ]; then "
+ "until curl -sf -m 5 " + RESOURCE_MANAGER_METRICS
+ " | grep -q \"\\\"activeNodes\\\":[1-9]\"; do "
+ "[ \"$(jobs -rp | wc -l)\" = $n ] || exit 1; sleep 0.2; done; "
+ untilReady("curl -sf -m 5 " + RESOURCE_MANAGER_METRICS
+ " | grep -q \"\\\"activeNodes\\\":[1-9]\"")
+ untilReady("curl -sf -m 5 " + JOB_HISTORY_INFO + " | grep -q historyInfo")
+ "fi; "
+ "touch " + HDFS_READY + "; "
// Not wait -n: an emr-6.x image's bash, 4.2, does not have it.
@@ -346,13 +356,12 @@ public class EmrClusterStorage implements ClusterStorage, ContainerTeardown {
"exec setpriv --reuid=hadoop --regid=hadoop --clear-groups bash -c '"
+ "n=1; if [ -n \"$" + NODE_CORE_VARIABLE + "\" ]; then hdfs datanode & n=2; fi; "
+ "JDK_JAVA_OPTIONS=\"" + yarnOpens() + "\" yarn nodemanager & "
+ "until curl -sf -m 5 \"http://$FLOCI_HDFS_HOST:8088/ws/v1/cluster/nodes?states=RUNNING\""
+ " | grep -q \"\\\"nodeHostName\\\":\\\"$" + NODE_HOST_VARIABLE + "\\\"\"; do "
+ "[ \"$(jobs -rp | wc -l)\" = $n ] || exit 1; sleep 0.2; done; "
+ untilReady("curl -sf -m 5 \"http://$FLOCI_HDFS_HOST:8088/ws/v1/cluster/nodes?states=RUNNING\""
+ " | grep -q \"\\\"nodeHostName\\\":\\\"$" + NODE_HOST_VARIABLE + "\\\"\"")
+ "if [ -n \"$" + NODE_CORE_VARIABLE + "\" ]; then "
+ "until curl -sf -m 5 \"http://$FLOCI_HDFS_HOST:9870/jmx?qry=Hadoop:service=NameNode,"
+ "name=NameNodeInfo\" | grep -q \"$" + NODE_HOST_VARIABLE + ":9866\"; do "
+ "[ \"$(jobs -rp | wc -l)\" = $n ] || exit 1; sleep 0.2; done; fi; "
+ untilReady("curl -sf -m 5 \"http://$FLOCI_HDFS_HOST:9870/jmx?qry=Hadoop:service=NameNode,"
+ "name=NameNodeInfo\" | grep -q \"$" + NODE_HOST_VARIABLE + ":9866\"")
+ "fi; "
+ "touch " + HDFS_READY + "; "
+ "while [ \"$(jobs -rp | wc -l)\" = $n ]; do sleep 1; done; rm -f " + HDFS_READY
+ "; kill $(jobs -rp) 2>/dev/null; exit 1'");
@@ -1216,6 +1225,11 @@ public class EmrClusterStorage implements ClusterStorage, ContainerTeardown {
return properties.toString();
}
// A node script's wait until condition holds, polled; one of its n daemons exiting meanwhile ends the node.
private static String untilReady(String condition) {
return "until " + condition + "; do [ \"$(jobs -rp | wc -l)\" = $n ] || exit 1; sleep 0.2; done; ";
}
// The JVM options that open to Hadoop's classes the packages its daemons reach into.
private static String yarnOpens() {
return EmrServerlessSparkRunner.yarnJavaOptions();
@@ -90,11 +90,6 @@ final class EmrSparkSteps {
// one, it is the same folder in HDFS, and Hadoop fills in the user.
private static final String WORKING_DIR = "mapreduce.job.working.dir";
private static final String USER_HOME = "/user/${user.name}";
// A cluster runs no MapReduce JobHistory server, which a job client asks for a job once its application master has
// gone, as Pig does for each MapReduce job's task reports and counters: Pig asks for no task reports, and its
// client asks the server once rather than three times, each attempt waiting about ten seconds to connect.
private static final String PIG_NO_TASK_REPORT = "pig.stats.notaskreport";
private static final String CLIENT_RETRIES = "yarn.app.mapreduce.client.max-retries";
// The AWS CLI's Hive step on a release is command-runner.jar running "hive-script --run-hive-script --args"
// and the step's arguments, the ones the image's hive-script runs hive with; a step can also run hive itself.
private static final String HIVE_SCRIPT = "hive-script";
@@ -295,8 +290,7 @@ final class EmrSparkSteps {
/**
* The variables a step's command gets on a cluster with Floci's Pig, which make its {@code pig} read a Pig
* configuration of its own: {@code pig.properties} lines that run Pig on Tez, as EMR's does, from a working
* directory S3A takes, with no wait on a JobHistory server the cluster does not run, then the cluster's
* {@code tez-site} and {@code pig-properties}, a later one winning, and,
* directory S3A takes, then the cluster's {@code tez-site} and {@code pig-properties}, a later one winning, and,
* with a {@code pig-log4j}, Pig's setting to read the {@code log4j.properties} that gets its lines;
* {@code pig-env}'s exports start its {@code pig-env.sh}.
*/
@@ -304,8 +298,6 @@ final class EmrSparkSteps {
Map<String, String> properties = new LinkedHashMap<>();
properties.put(PIG_EXECTYPE, PIG_TEZ);
properties.put(WORKING_DIR, USER_HOME);
properties.put(PIG_NO_TASK_REPORT, "true");
properties.put(CLIENT_RETRIES, "1");
Map<String, String> log4j = properties(configurations, objectMapper, PIG_LOG4J);
if (!log4j.isEmpty()) {
properties.put(PIG_LOG4J_CONF, EmrServerlessSparkRunner.PIG_CONF + "/log4j.properties");
@@ -122,6 +122,9 @@ public class EmrServerlessSparkRunner implements SparkJobRunner, ContainerTeardo
// The port of a cluster's NameNode, HDFS's own default, which hdfs:// paths and those without a scheme
// ("hdfs:// (or no prefix)") reach through the cluster's primary node.
private static final int HDFS_PORT = 8020;
/** The ports of a cluster's MapReduce JobHistory server on its primary node, Hadoop's own defaults. */
public static final int JOB_HISTORY_PORT = 10020;
public static final int JOB_HISTORY_WEB_PORT = 19888;
// Spark's default checkpoint manager goes through Hadoop's FileContext, which refuses an hdfs:// path without a
// host, though "hdfs:///path-to-data" and "/path-to-data" "would resolve to the same location in HDFS", as the
// Management Guide puts it; so a streaming query keeps its checkpoints through the file system instead.
@@ -1254,7 +1257,7 @@ public class EmrServerlessSparkRunner implements SparkJobRunner, ContainerTeardo
// A MapReduce job on the cluster's YARN: its application master and tasks find Hadoop in the nodes' layout, which
// yarn.application.classpath's default does not name, and S3A's AWS SDK, reach Floci with the step's AWS environment,
// and are sized for a NodeManager as Spark's executors are. The image's core-site.xml names the LZO codec, so its
// jars come too.
// jars come too. Its client asks the cluster's JobHistory server about it once its application master has ended.
private static List<SparkSetting> yarnMapReduceSettings(Job job, List<String> executorEnvironment) {
String root = hadoopRoot(job);
List<String> classPath = new ArrayList<>(hadoopLayout(root));
@@ -1269,7 +1272,20 @@ public class EmrServerlessSparkRunner implements SparkJobRunner, ContainerTeardo
new SparkSetting("mapreduce.reduce.env", environment),
new SparkSetting("yarn.app.mapreduce.am.resource.mb", "1024"),
new SparkSetting("mapreduce.map.memory.mb", "768"),
new SparkSetting("mapreduce.reduce.memory.mb", "768"));
new SparkSetting("mapreduce.reduce.memory.mb", "768"),
jobHistory(job), jobHistoryWeb(job));
}
// The cluster's MapReduce JobHistory server on its primary node, which a MapReduce job's client asks about it once
// its application master has ended, and its web pages.
private static SparkSetting jobHistory(Job job) {
return new SparkSetting("mapreduce.jobhistory.address",
job.storage().resourceManager() + ":" + JOB_HISTORY_PORT);
}
private static SparkSetting jobHistoryWeb(Job job) {
return new SparkSetting("mapreduce.jobhistory.webapp.address",
job.storage().resourceManager() + ":" + JOB_HISTORY_WEB_PORT);
}
/**
@@ -1291,13 +1307,16 @@ public class EmrServerlessSparkRunner implements SparkJobRunner, ContainerTeardo
}
// Hive's Tez on the cluster's YARN, sized as its tasks are by Hive's own settings: a task's two virtual cores never
// match the one YARN's default scheduler grants, so Tez would give each container back.
// match the one YARN's default scheduler grants, so Tez would give each container back. A query that runs MapReduce
// instead, its hive.execution.engine set to mr, asks the cluster's JobHistory server about its jobs.
private static List<SparkSetting> hiveOnYarnSettings(Job job, List<String> executorEnvironment) {
String root = hadoopRoot(job);
List<SparkSetting> settings = new ArrayList<>(tezOnYarnSettings(job, executorEnvironment,
List.of(root + "/hive/lib/hive-exec.jar"), "HIVE_HOME=" + root + "/hive"));
settings.add(new SparkSetting("hive.tez.container.size", "768"));
settings.add(new SparkSetting("hive.tez.cpu.vcores", "1"));
settings.add(jobHistory(job));
settings.add(jobHistoryWeb(job));
return settings;
}