feat(emr): run a Spark History Server on a cluster's primary node in the docker runner mode (#8796)

With runner-mode docker, a cluster with Spark now runs a Spark History
Server on its primary node, as EMR's does ("Spark HistoryServer |
http://master-public-dns-name:18080/"), and GetOnClusterAppUIPresignedURL
opens it for real, on its own host, as the cluster's other interfaces
open.

Where the primary node runs the server, a step's Spark on the cluster's
YARN keeps its event logs where "By
default, Amazon EMR stores event logs in the /var/log/spark/apps
directory of HDFS" (spark.eventLog.dir hdfs:///var/log/spark/apps, which
the cluster's spark-defaults may replace), and names the server as
spark.yarn.historyServer.address, so an ended application's tracking URL
leads to its page there. The primary node makes that folder with the
others in its HDFS, starts the server once it is there, reading it and
listening where HDFS does, as one of the node's daemons, and is ready
once it answers. Its port is published on the host's loopback interface
as the other interfaces' are. With an ApplicationId, the server's link
lands on the application's page.

EmrAppUiNightlyIntegrationTest runs it for real: after a SparkPi step,
the ResourceManager's proxy sends the application's tracking URL on to
its page on the server's own host, the server lists it from its event
logs, and the server's own link lands on it.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-11-17 21:06:32 -04:00
1 parent a1de0ee905
commit 0475f96d85
11 files changed
+232 -44

No files matched your search

@@ -117,19 +117,20 @@ public interface ClusterStorage {
* YARN on a cluster: a ResourceManager on its primary node, and a NodeManager on each of its {@code nodes}, and on
* the primary node too when it {@code primaryWorks}, each offering its containers {@code memoryMb} of memory and
* {@code vcores} virtual cores, which run its steps' Spark executors; the folder of the nodes' image holding the
* Hadoop their daemons run from, or null for the image's own; the copies of a block its HDFS keeps; and the S3
* folder its applications' logs go in, or null. A primary node that works runs a DataNode as well.
* Hadoop their daemons run from, or null for the image's own; the copies of a block its HDFS keeps; the S3
* folder its applications' logs go in, or null; and whether its primary node runs a Spark History Server, as on a
* cluster with Spark. A primary node that works runs a DataNode as well.
*/
record Yarn(int memoryMb, int vcores, String hadoopFolder, List<Node> nodes, int replication, String logFolder,
boolean primaryWorks) {
boolean primaryWorks, boolean sparkHistory) {
Yarn(int memoryMb, int vcores) {
this(memoryMb, vcores, null, List.of(), 1, null, true);
this(memoryMb, vcores, null, List.of(), 1, null, true, false);
}
/** This YARN with its daemons run from the Hadoop in {@code folder}. */
Yarn withHadoopFolder(String folder) {
return new Yarn(memoryMb, vcores, folder, nodes, replication, logFolder, primaryWorks);
return new Yarn(memoryMb, vcores, folder, nodes, replication, logFolder, primaryWorks, sparkHistory);
}
/**
@@ -138,12 +139,17 @@ public interface ClusterStorage {
*/
Yarn onNodes(List<Node> workers, int copies, String folder) {
return new Yarn(memoryMb, vcores, hadoopFolder, List.copyOf(workers), copies, folder,
workers.stream().noneMatch(Node::core));
workers.stream().noneMatch(Node::core), sparkHistory);
}
/** This YARN with its primary node running a DataNode and a NodeManager, or not. */
Yarn withPrimaryWorking(boolean works) {
return new Yarn(memoryMb, vcores, hadoopFolder, nodes, replication, logFolder, works);
return new Yarn(memoryMb, vcores, hadoopFolder, nodes, replication, logFolder, works, sparkHistory);
}
/** This YARN with its primary node running a Spark History Server, or not. */
Yarn withSparkHistory(boolean runs) {
return new Yarn(memoryMb, vcores, hadoopFolder, nodes, replication, logFolder, primaryWorks, runs);
}
}
@@ -39,11 +39,12 @@ import java.util.regex.Pattern;
* A cluster's application user interfaces: the persistent ones "hosted off-cluster" (Amazon EMR Management Guide, "View
* persistent application user interfaces in Amazon EMR") and the on-cluster ones, both reached through presigned URLs.
* In the docker runner mode, an on-cluster UI that runs on the cluster's primary node, its ResourceManager, an
* application master through it, its MapReduce JobHistory server, or the NodeManager of a primary node that works,
* opens for real through {@link EmrAppUiProxy}, on a host of its own under {@link #UI_HOST}, as AWS's are under
* "emrappui-prod.us-east-1.amazonaws.com" (Management Guide, "View Amazon EMR application history"). Floci keeps a
* persistent application UI's record, but serves no Spark History Server, Tez UI or YARN timeline server: a link to
* one of those, and to any on-cluster UI elsewhere, opens a page of Floci's own about the cluster
* application master through it, its MapReduce JobHistory server, the NodeManager of a primary node that works, or the
* Spark History Server of a cluster with Spark, opens for real through {@link EmrAppUiProxy}, on a host of its own
* under {@link #UI_HOST}, as AWS's are under "emrappui-prod.us-east-1.amazonaws.com" (Management Guide, "View Amazon
* EMR application history"). Floci keeps a persistent application UI's record, but serves no persistent UI, Tez UI or
* YARN timeline server: a link to one of those, and to any on-cluster UI elsewhere, opens a page of Floci's own about
* the cluster
* ({@link EmrAppUiController}). Either link is valid for {@link #VALIDITY}: the page's is signed with a key of this
* process, and a proxied interface's host holds a secret this process gave it.
*/
@@ -72,6 +73,7 @@ public class EmrAppUiService {
static final String RESOURCE_MANAGER = "ResourceManager";
static final String APPLICATION_MASTER = "ApplicationMaster";
static final String JOB_HISTORY_SERVER = "JobHistoryServer";
static final String SPARK_HISTORY_SERVER = "SparkHistoryServer";
// No source gives AWS's wording for these; they are Floci's.
static final String NO_SUCH_INTERFACE = "This link opens none of the cluster's interfaces on that port.";
static final String CLUSTER_ENDED = "Cluster %s is no longer running, and its on-cluster application UIs are"
@@ -267,6 +269,7 @@ public class EmrAppUiService {
int port = switch (type) {
case RESOURCE_MANAGER, APPLICATION_MASTER -> EmrClusterStorage.RESOURCE_MANAGER_WEB_PORT;
case JOB_HISTORY_SERVER -> EmrServerlessSparkRunner.JOB_HISTORY_WEB_PORT;
case SPARK_HISTORY_SERVER -> EmrServerlessSparkRunner.SPARK_HISTORY_WEB_PORT;
default -> 0;
};
return interfaces.ports().contains(port) ? port : null;
@@ -282,6 +285,7 @@ public class EmrAppUiService {
Matcher matcher = applicationId != null ? APPLICATION_ID.matcher(applicationId) : null;
yield matcher != null && matcher.matches() ? "/jobhistory/job/job_" + matcher.group(1) : "/jobhistory";
}
case SPARK_HISTORY_SERVER -> application != null ? "/history/" + application + "/" : "/";
default -> application != null ? "/cluster/app/" + application : "/cluster";
};
}
@@ -143,6 +143,11 @@ public class EmrClusterStorage implements ClusterStorage, ContainerTeardown {
// 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";
// Set on a primary node that runs a Spark History Server, as on a cluster with Spark; its version on its web port
// says it has started.
private static final String SPARK_HISTORY_VARIABLE = "FLOCI_SPARK_HISTORY";
private static final String SPARK_HISTORY_VERSION = "http://" + BIND + ":"
+ EmrServerlessSparkRunner.SPARK_HISTORY_WEB_PORT + "/api/v1/version";
// 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;
@@ -274,10 +279,13 @@ public class EmrClusterStorage implements ClusterStorage, ContainerTeardown {
// 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.
// Hive's exec jar goes in, and, with YARN, the folder its Spark applications' event logs go in
// (EmrServerlessSparkRunner.SPARK_EVENT_LOGS). With SPARK_HISTORY_VARIABLE set, a Spark History Server starts then,
// as EMR's primary node runs one on a cluster with Spark, reading that folder, listening on its port where HDFS
// does. HDFS_READY then says the node is ready, once the metastore takes connections, the NodeManager has
// registered, and the JobHistory server and the Spark History Server answer. 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.
@@ -308,6 +316,8 @@ public class EmrClusterStorage implements ClusterStorage, ContainerTeardown {
+ "if [ -z \"$" + NODE_WORKERS_VARIABLE + "\" ] || [ -d /mnt/datanode/current ]; then "
+ "hdfs datanode & n=2; fi; "
+ "dirs=\"/tmp /user/hadoop\"; "
+ "[ -z \"$" + YARN_MEMORY_VARIABLE + "\" ] || dirs=\"$dirs "
+ EmrServerlessSparkRunner.SPARK_EVENT_LOGS + "\"; "
+ "if [ -n \"$" + HIVE_METASTORE_VARIABLE + "\" ]; then "
+ "dirs=\"$dirs /user/hive/warehouse " + HIVE_JARS + "\"; "
+ "[ -z \"$" + HIVE_METASTORE_SITE_VARIABLE + "\" ] || mapfile -t site <<< \"$"
@@ -337,6 +347,12 @@ public class EmrClusterStorage implements ClusterStorage, ContainerTeardown {
+ untilReady("curl -sf -m 5 \"" + NAMENODE_JMX + "NameNodeInfo\""
+ " | grep -q \"\\\"Safemode\\\" : \\\"\\\"\"")
+ "hdfs dfs -mkdir -p $dirs || exit 1; "
+ "if [ -n \"$" + SPARK_HISTORY_VARIABLE + "\" ]; then "
+ "SPARK_LOCAL_IP=$FLOCI_HDFS_BIND SPARK_HISTORY_OPTS=\"-Dspark.history.fs.logDirectory="
+ "hdfs://$FLOCI_HDFS_HOST:8020" + EmrServerlessSparkRunner.SPARK_EVENT_LOGS
+ " -Dspark.history.ui.port=" + EmrServerlessSparkRunner.SPARK_HISTORY_WEB_PORT + "\""
+ " /usr/lib/spark/bin/spark-class org.apache.spark.deploy.history.HistoryServer & n=$((n + 1)); "
+ "fi; "
+ "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
@@ -348,6 +364,9 @@ public class EmrClusterStorage implements ClusterStorage, ContainerTeardown {
+ " | grep -q \"\\\"activeNodes\\\":[1-9]\"")
+ untilReady("curl -sf -m 5 " + JOB_HISTORY_INFO + " | grep -q historyInfo")
+ "fi; "
+ "if [ -n \"$" + SPARK_HISTORY_VARIABLE + "\" ]; then "
+ untilReady("curl -sf -m 5 " + SPARK_HISTORY_VERSION + " > /dev/null")
+ "fi; "
+ "touch " + HDFS_READY + "; "
// Not wait -n: an emr-6.x image's bash, 4.2, does not have it.
+ "while [ \"$(jobs -rp | wc -l)\" = $n ]; do sleep 1; done; rm -f " + HDFS_READY
@@ -743,6 +762,9 @@ public class EmrClusterStorage implements ClusterStorage, ContainerTeardown {
boolean everyWorkerJoined = network != null && clusterSubnet(clusterId) != null
&& (hiveMetastore == null || !yarn.primaryWorks());
storage = storage.withYarn(host, everyWorkerJoined ? awsEnv.flociEndpoint() : flociEndpointOn(name));
if (yarn.sparkHistory()) {
storage = storage.withSparkHistoryServer(host);
}
return yarn.nodes().stream().anyMatch(Node::core) ? storage.withReplication(yarn.replication()) : storage;
}
}
@@ -1312,6 +1334,9 @@ public class EmrClusterStorage implements ClusterStorage, ContainerTeardown {
if (yarn.primaryWorks()) {
spec.withLoopbackPortBinding(NODE_MANAGER_WEB_PORT, 0);
}
if (yarn.sparkHistory()) {
spec.withLoopbackPortBinding(EmrServerlessSparkRunner.SPARK_HISTORY_WEB_PORT, 0);
}
}
if (yarn != null) {
spec.withEnv(YARN_MEMORY_VARIABLE, String.valueOf(yarn.memoryMb()))
@@ -1323,6 +1348,9 @@ public class EmrClusterStorage implements ClusterStorage, ContainerTeardown {
if (yarn.hadoopFolder() != null) {
spec.withEnv(HADOOP_FOLDER_VARIABLE, yarn.hadoopFolder());
}
if (yarn.sparkHistory()) {
spec.withEnv(SPARK_HISTORY_VARIABLE, "1");
}
}
// The metastore and the executors YARN runs reach Floci, Floci on the host by its address, Floci's own
// certificate trusted when it serves TLS.
@@ -977,8 +977,8 @@ public class EmrService implements Resettable, StoredResourceArns, ContainerTear
/**
* The web interfaces Floci serves of a cluster's primary node, where it runs YARN there, in the docker runner
* mode: its ResourceManager, its MapReduce JobHistory server, and, where the node works, its NodeManager; empty
* elsewhere.
* mode: its ResourceManager, its MapReduce JobHistory server, where the node works its NodeManager, and on a
* cluster with Spark its Spark History Server; empty elsewhere.
*/
public Optional<WebInterfaces> webInterfaces(EmrCluster cluster) {
if (!runsOnClusters() || !runsYarn(cluster)) {
@@ -986,9 +986,13 @@ public class EmrService implements Resettable, StoredResourceArns, ContainerTear
}
Set<Integer> ports = new LinkedHashSet<>(List.of(EmrClusterStorage.RESOURCE_MANAGER_WEB_PORT,
EmrServerlessSparkRunner.JOB_HISTORY_WEB_PORT));
if (yarnOf(cluster).primaryWorks()) {
ClusterStorage.Yarn yarn = yarnOf(cluster);
if (yarn.primaryWorks()) {
ports.add(EmrClusterStorage.NODE_MANAGER_WEB_PORT);
}
if (yarn.sparkHistory()) {
ports.add(EmrServerlessSparkRunner.SPARK_HISTORY_WEB_PORT);
}
return Optional.of(new WebInterfaces(masterPrivateDnsName(cluster), Set.copyOf(ports)));
}
@@ -2878,7 +2882,8 @@ public class EmrService implements Resettable, StoredResourceArns, ContainerTear
return nodeManager.withHadoopFolder(EmrReleaseImages.hadoopFolder(cluster.getReleaseLabel()))
.onNodes(nodes, (int) Math.max(1, Math.min(replication(cluster), coreNodes)),
EmrSparkSteps.containerLogFolder(cluster))
.withPrimaryWorking(cluster.getProvisionedCoreNodes() == 0 || coreNodes == 0);
.withPrimaryWorking(cluster.getProvisionedCoreNodes() == 0 || coreNodes == 0)
.withSparkHistory(EmrSparkSteps.names(cluster.getApplications(), SNAPSHOT_MAPPER, "Spark"));
}
// What a cluster asks of YARN when it is read, which may be later than now; what it asks now, if it has gone by then.
@@ -125,6 +125,13 @@ public class EmrServerlessSparkRunner implements SparkJobRunner, ContainerTeardo
/** 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;
/** A cluster's Spark History Server's port, "Spark HistoryServer | http://master-public-dns-name:18080/". */
public static final int SPARK_HISTORY_WEB_PORT = 18080;
/**
* Where a cluster's Spark applications keep their event logs, which its Spark History Server reads: "By default,
* Amazon EMR stores event logs in the /var/log/spark/apps directory of HDFS".
*/
public static final String SPARK_EVENT_LOGS = "/var/log/spark/apps";
// 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.
@@ -1625,8 +1632,10 @@ public class EmrServerlessSparkRunner implements SparkJobRunner, ContainerTeardo
// A step's Spark on its cluster's YARN: the ResourceManager on the primary node; Spark's jars from the node's image,
// which is the step's; executors and a cluster-mode driver sized for the NodeManager rather than the image's 14 GB
// ("determined by the core and task instance types" on EMR), which the cluster's spark-defaults and the step's own
// options replace; a client-mode driver reached at the container's name on the cluster's network; and the step's
// AWS environment for each executor and a cluster-mode driver, as its container has it.
// options replace; a client-mode driver reached at the container's name on the cluster's network; where the
// primary node runs a Spark History Server, as on a cluster with Spark, its event logs in the cluster's HDFS, where
// the server reads them, which the application's tracking URL leads to once it has ended; and the step's AWS
// environment for each executor and a cluster-mode driver, as its container has it.
private static List<SparkSetting> yarnSettings(Job job, List<String> executorEnvironment) {
List<SparkSetting> settings = new ArrayList<>(List.of(
new SparkSetting("spark.hadoop.yarn.resourcemanager.hostname", job.storage().resourceManager()),
@@ -1637,6 +1646,12 @@ public class EmrServerlessSparkRunner implements SparkJobRunner, ContainerTeardo
new SparkSetting("spark.driver.cores", "1"),
new SparkSetting("spark.driver.host", driverHost(job)),
new SparkSetting("spark.driver.bindAddress", "0.0.0.0")));
String history = job.storage().sparkHistoryServer();
if (history != null) {
settings.add(new SparkSetting("spark.eventLog.enabled", "true"));
settings.add(new SparkSetting("spark.eventLog.dir", "hdfs://" + SPARK_EVENT_LOGS));
settings.add(new SparkSetting("spark.yarn.historyServer.address", history + ":" + SPARK_HISTORY_WEB_PORT));
}
for (String variable : executorEnvironment) {
int equals = variable.indexOf('=');
String name = variable.substring(0, equals);
@@ -211,12 +211,13 @@ public interface SparkJobRunner {
* node, named {@code hdfsHost}, which a step's container reaches at {@code hdfsAddress} on the Docker network
* {@code hdfsNetwork}, the cluster's own, which the container joins (all three null until it runs, the network
* also on the host's own network, which steps share with it); and, on a cluster with Hive or HCatalog, the URI of
* the Hive metastore service on that node, or null; and how many copies of a block its HDFS keeps, when its core
* nodes hold it, else 0.
* the Hive metastore service on that node, or null; how many copies of a block its HDFS keeps, when its core
* nodes hold it, else 0; and the host of the Spark History Server on that node, which reads its Spark applications'
* event logs, or null where it runs none.
*/
record Storage(String home, String mnt, String hdfsHost, String hdfsAddress, String hdfsNetwork,
String hiveMetastore, String resourceManager, String flociEndpoint, int replication,
String metadataAddress) {
String metadataAddress, String sparkHistoryServer) {
/**
* The component label of an EMR cluster's HDFS container, which carries its service's labels as a driver
@@ -229,19 +230,19 @@ public interface SparkJobRunner {
/** A cluster's volumes, before its HDFS runs. */
public Storage(String home, String mnt) {
this(home, mnt, null, null, null, null, null, null, 0, null);
this(home, mnt, null, null, null, null, null, null, 0, null, null);
}
/** This storage, its HDFS's NameNode on {@code host}, at {@code address} on {@code network}. */
public Storage withHdfs(String host, String address, String network) {
return new Storage(home, mnt, host, address, network, hiveMetastore, resourceManager, flociEndpoint,
replication, metadataAddress);
replication, metadataAddress, sparkHistoryServer);
}
/** This storage, its Hive metastore service at {@code uri}. */
public Storage withHiveMetastore(String uri) {
return new Storage(home, mnt, hdfsHost, hdfsAddress, hdfsNetwork, uri, resourceManager, flociEndpoint,
replication, metadataAddress);
replication, metadataAddress, sparkHistoryServer);
}
/**
@@ -250,13 +251,21 @@ public interface SparkJobRunner {
*/
public Storage withYarn(String host, String flociEndpoint) {
return new Storage(home, mnt, hdfsHost, hdfsAddress, hdfsNetwork, hiveMetastore, host, flociEndpoint,
replication, metadataAddress);
replication, metadataAddress, sparkHistoryServer);
}
/** This storage, its HDFS keeping {@code copies} copies of a block on its core nodes. */
public Storage withReplication(int copies) {
return new Storage(home, mnt, hdfsHost, hdfsAddress, hdfsNetwork, hiveMetastore, resourceManager,
flociEndpoint, copies, metadataAddress);
flociEndpoint, copies, metadataAddress, sparkHistoryServer);
}
/**
* This storage, a Spark History Server on {@code host}, the primary node, reading its applications' event logs.
*/
public Storage withSparkHistoryServer(String host) {
return new Storage(home, mnt, hdfsHost, hdfsAddress, hdfsNetwork, hiveMetastore, resourceManager,
flociEndpoint, replication, metadataAddress, host);
}
/**
@@ -265,7 +274,7 @@ public interface SparkJobRunner {
*/
public Storage withMetadataAddress(String address) {
return new Storage(home, mnt, hdfsHost, hdfsAddress, hdfsNetwork, hiveMetastore, resourceManager,
flociEndpoint, replication, address);
flociEndpoint, replication, address, sparkHistoryServer);
}
}