mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(emr): run Pig steps on Apache Pig, an opt-in download added to the cluster's image (#8636)
No AWS image carries Pig, so a Pig step failed without running. With floci.services.emr.pig.enabled on (off by default), a docker cluster whose Applications name Pig gets Apache Pig 0.18.0 in its image: downloaded to a temporary file from the first of floci.services.emr.pig.urls that gives it whole with the checksum floci.services.emr.pig.sha256 names (a URL that fails, stalls for a minute or gives another checksum passes to the next), then streamed into /usr/lib/pig as EmrReleaseImages makes the image, with Tez from the release's Hive image and a /usr/bin/pig that EMR's own pig-script runs. Pig runs on the image's Tez, its bundled Tez and protobuf 3 left out, on the cluster's YARN or in local mode, with exectype=tez, a working directory without a scheme, which S3A takes, and no task reports and one try for counters from the MapReduce JobHistory server the cluster does not run, then the cluster's tez-site and pig-properties, in a pig.properties of the step's own; pig-log4j and pig-env reach Pig too. A command step's new tez-site.xml shares Hive's Tez-on-YARN settings. Without the setting, a Pig step on a cluster that installs Pig still fails without running, its message naming the setting; on one that does not, EMR's pig-script says "Pig isn't installed." as on EMR.
This commit is contained in:
1 parent
d2bca16023
commit
30f8a9985e
17 files changed
+1355
-109
No files matched your search
@@ -2250,6 +2250,9 @@ public interface EmulatorConfig {
|
||||
@WithDefault("4")
|
||||
int yarnMaxWorkerNodes();
|
||||
|
||||
/** Apache Pig for a {@code docker} cluster's Pig steps, which no AWS image carries; off unless enabled. */
|
||||
EmrPigConfig pig();
|
||||
|
||||
/**
|
||||
* When true, Floci removes, in the {@code docker} runner mode, every EMR step driver and bootstrap
|
||||
* container a previous run of <em>this same</em> Floci left on the daemon (matched by the
|
||||
@@ -2263,6 +2266,37 @@ public interface EmulatorConfig {
|
||||
boolean reconcileContainersOnStartup();
|
||||
}
|
||||
|
||||
interface EmrPigConfig {
|
||||
/**
|
||||
* When true, a {@code docker} cluster whose {@code Applications} name Pig has Apache Pig in the image its steps
|
||||
* run in, downloaded from {@link #urls()} when Floci makes that image, and its Pig steps run it. Off by default,
|
||||
* as the download is Apache's, not AWS's, and about 470 MB.
|
||||
*
|
||||
* Env var: FLOCI_SERVICES_EMR_PIG_ENABLED
|
||||
*/
|
||||
@WithDefault("false")
|
||||
boolean enabled();
|
||||
|
||||
/**
|
||||
* Where Floci downloads Apache Pig's binary distribution from, the first that answers: Apache's download CDN,
|
||||
* which serves current releases, then its archive, which keeps every release.
|
||||
*
|
||||
* Env var: FLOCI_SERVICES_EMR_PIG_URLS
|
||||
*/
|
||||
@WithDefault("https://dlcdn.apache.org/pig/pig-0.18.0/pig-0.18.0.tar.gz,"
|
||||
+ "https://archive.apache.org/dist/pig/pig-0.18.0/pig-0.18.0.tar.gz")
|
||||
List<String> urls();
|
||||
|
||||
/**
|
||||
* The SHA-256 of the distribution {@link #urls()} name; one downloaded with another fails the step, and no
|
||||
* image is made of it.
|
||||
*
|
||||
* Env var: FLOCI_SERVICES_EMR_PIG_SHA256
|
||||
*/
|
||||
@WithDefault("6845adb936a3c3bcc71451765953cae4103e410ba2f6c5a47cc9becade0a434a")
|
||||
String sha256();
|
||||
}
|
||||
|
||||
interface WafV2ServiceConfig {
|
||||
@WithDefault("true")
|
||||
boolean enabled();
|
||||
|
||||
@@ -23,6 +23,10 @@ import org.jboss.logging.Logger;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.io.UncheckedIOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.LinkedHashMap;
|
||||
@@ -43,7 +47,9 @@ import java.util.concurrent.ConcurrentHashMap;
|
||||
* kept for the release's next cluster; the one a newer pull makes replaces those of its kind made before
|
||||
* ({@link #KIND_LABEL}). A release of the AWS runtime for Apache Spark has no Hive image, and another
|
||||
* release's HDFS and YARN do not run on its newer Hadoop, so its image gets the whole of that release's Hadoop, in a
|
||||
* folder apart, which only its primary node's daemons run from.
|
||||
* folder apart, which only its primary node's daemons run from. For a cluster with Pig, which no AWS image carries,
|
||||
* the image gets Tez, as Pig's scripts run on it, and Apache Pig, downloaded as it is made ({@link PigDistribution}),
|
||||
* whose checksum its name holds.
|
||||
*/
|
||||
@ApplicationScoped
|
||||
public class EmrReleaseImages implements ReleaseImages {
|
||||
@@ -82,6 +88,12 @@ public class EmrReleaseImages implements ReleaseImages {
|
||||
new Copy("/usr/bin/s3-dist-cp", "/usr/bin"),
|
||||
Copy.to("/etc/hadoop/conf", EmrServerlessSparkRunner.HIVE_HADOOP_CONF),
|
||||
new Copy(GOODIES, EMR_SHARE, HIVE_GOODIES));
|
||||
// What a cluster with Pig and without Hive adds to those Hadoop services: Tez, which Pig runs its scripts on, with
|
||||
// its configuration and the goodies its links lead to, kept apart as for Hive.
|
||||
static final List<Copy> TEZ_COPIES = List.of(
|
||||
new Copy("/usr/lib/tez", "/usr/lib"),
|
||||
new Copy("/etc/tez", "/etc"),
|
||||
new Copy(GOODIES, EMR_SHARE, HIVE_GOODIES));
|
||||
private static final String HADOOP_APART = "/opt";
|
||||
// What it adds for a release of the AWS runtime for Apache Spark: another release's Hadoop, whole and kept apart.
|
||||
static final List<Copy> HADOOP_APART_COPIES = List.of(
|
||||
@@ -102,14 +114,17 @@ public class EmrReleaseImages implements ReleaseImages {
|
||||
private final ContainerBuilder containerBuilder;
|
||||
private final ImageCacheService imageCacheService;
|
||||
private final EmulatorConfig config;
|
||||
private final PigDistribution pigDistribution;
|
||||
|
||||
@Inject
|
||||
public EmrReleaseImages(ContainerLifecycleManager lifecycleManager, ContainerBuilder containerBuilder,
|
||||
ImageCacheService imageCacheService, EmulatorConfig config) {
|
||||
ImageCacheService imageCacheService, EmulatorConfig config,
|
||||
PigDistribution pigDistribution) {
|
||||
this.lifecycleManager = lifecycleManager;
|
||||
this.containerBuilder = containerBuilder;
|
||||
this.imageCacheService = imageCacheService;
|
||||
this.config = config;
|
||||
this.pigDistribution = pigDistribution;
|
||||
}
|
||||
|
||||
/** The folder of a release's image holding the Hadoop its primary node's daemons run from, or null for its own. */
|
||||
@@ -124,25 +139,28 @@ public class EmrReleaseImages implements ReleaseImages {
|
||||
* it to a registry for an image only this daemon has.
|
||||
*/
|
||||
@Override
|
||||
public String image(String releaseLabel, String sparkImage, String hiveImage, boolean withHive) {
|
||||
List<Copy> copies = hadoopFolder(releaseLabel) != null ? HADOOP_APART_COPIES
|
||||
: withHive ? COPIES : HADOOP_COPIES;
|
||||
public String image(String releaseLabel, String sparkImage, String hiveImage, boolean withHive,
|
||||
boolean withPig) {
|
||||
List<Copy> copies = copies(releaseLabel, withHive, withPig);
|
||||
PigDistribution pig = withPig && hadoopFolder(releaseLabel) == null ? pigDistribution : null;
|
||||
// What is copied, and the Pig written after it, by its checksum.
|
||||
String content = copies + (pig != null ? "\n" + pig.identity() : "");
|
||||
String spark = containerBuilder.resolveImage(sparkImage);
|
||||
String hive = containerBuilder.resolveImage(hiveImage);
|
||||
String repository = ContainerStorageHelper.dockerName(config, COMPONENT);
|
||||
String kind = Digests.sha256Hex(releaseLabel + "\n" + spark + "\n" + hive + "\n" + copies).substring(0, 12);
|
||||
String kind = Digests.sha256Hex(releaseLabel + "\n" + spark + "\n" + hive + "\n" + content).substring(0, 12);
|
||||
synchronized (making.computeIfAbsent(repository + ":" + kind, name -> new Object())) {
|
||||
String sparkId = imageCacheService.resolveForLaunch(spark, ImagePullBehavior.PREFER_CACHED, null)
|
||||
.imageId();
|
||||
String hiveId = imageCacheService.resolveForLaunch(hive, ImagePullBehavior.PREFER_CACHED, null).imageId();
|
||||
String tag = releaseLabel + "-"
|
||||
+ Digests.sha256Hex(sparkId + "\n" + hiveId + "\n" + copies).substring(0, 12);
|
||||
+ Digests.sha256Hex(sparkId + "\n" + hiveId + "\n" + content).substring(0, 12);
|
||||
DockerClient docker = lifecycleManager.getDockerClient();
|
||||
try {
|
||||
return hold(docker.inspectImageCmd(repository + ":" + tag).exec().getId());
|
||||
} catch (NotFoundException e) {
|
||||
LOG.infov("Making {0}:{1} for EMR clusters {2}, from {3} and {4}", repository, tag,
|
||||
withHive ? "with Hive" : "without Hive", spark, hive);
|
||||
(withHive ? "with Hive" : "without Hive") + (pig != null ? ", with Pig" : ""), spark, hive);
|
||||
}
|
||||
String user = docker.inspectImageCmd(sparkId).exec().getConfig().getUser();
|
||||
Map<String, String> labels = new LinkedHashMap<>();
|
||||
@@ -150,6 +168,13 @@ public class EmrReleaseImages implements ReleaseImages {
|
||||
labels.put(ContainerStorageHelper.COMPONENT_LABEL, COMPONENT);
|
||||
labels.put(ContainerStorageHelper.OWNER_LABEL, ContainerStorageHelper.ownerIdentity(config));
|
||||
labels.put(KIND_LABEL, kind);
|
||||
// Downloaded and checked before any container is made, and read again by a copy Docker makes retry.
|
||||
Path pigArchive;
|
||||
try {
|
||||
pigArchive = pig != null ? pig.download() : null;
|
||||
} catch (IOException e) {
|
||||
throw new UncheckedIOException(e.getMessage(), e);
|
||||
}
|
||||
String made;
|
||||
List<String> containers = new ArrayList<>();
|
||||
try {
|
||||
@@ -161,6 +186,12 @@ public class EmrReleaseImages implements ReleaseImages {
|
||||
RetryingTarCopier.copyStreamed(docker, target, copy.into(), copy.from(),
|
||||
out -> copy(docker.copyArchiveFromContainerCmd(source, copy.from()).exec(), out, copy));
|
||||
}
|
||||
if (pigArchive != null) {
|
||||
RetryingTarCopier.copyStreamed(docker, target, PigDistribution.FOLDER, PigDistribution.HOME,
|
||||
out -> PigDistribution.write(pigArchive, out));
|
||||
RetryingTarCopier.copyBytes(docker, target, PigDistribution.COMMAND_FOLDER, PigDistribution.NAME,
|
||||
PigDistribution.COMMAND.getBytes(StandardCharsets.UTF_8), 0755);
|
||||
}
|
||||
made = lifecycleManager.commit(target, repository, tag,
|
||||
user == null || user.isBlank() ? "root" : user, labels);
|
||||
} finally {
|
||||
@@ -172,12 +203,37 @@ public class EmrReleaseImages implements ReleaseImages {
|
||||
repository, tag, e.getMessage());
|
||||
}
|
||||
}
|
||||
if (pigArchive != null) {
|
||||
try {
|
||||
Files.deleteIfExists(pigArchive);
|
||||
} catch (IOException e) {
|
||||
LOG.warnv("Could not remove {0}, the Apache Pig downloaded for {1}:{2}: {3}", pigArchive,
|
||||
repository, tag, e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
removeReplaced(docker, labels, made, repository + ":" + tag);
|
||||
return hold(made);
|
||||
}
|
||||
}
|
||||
|
||||
// Another release's Hadoop for an emr-spark-* release, which has neither Hive nor Pig; Hive, Tez and those Hadoop
|
||||
// services for a cluster with Hive; Tez and those services for one with Pig alone; those services alone otherwise.
|
||||
static List<Copy> copies(String releaseLabel, boolean withHive, boolean withPig) {
|
||||
if (hadoopFolder(releaseLabel) != null) {
|
||||
return HADOOP_APART_COPIES;
|
||||
}
|
||||
if (withHive) {
|
||||
return COPIES;
|
||||
}
|
||||
if (withPig) {
|
||||
List<Copy> copies = new ArrayList<>(HADOOP_COPIES);
|
||||
copies.addAll(TEZ_COPIES);
|
||||
return List.copyOf(copies);
|
||||
}
|
||||
return HADOOP_COPIES;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void released(String imageId) {
|
||||
held.computeIfPresent(imageId, (id, holders) -> holders > 1 ? holders - 1 : null);
|
||||
|
||||
@@ -149,6 +149,8 @@ public class EmrService implements Resettable, StoredResourceArns, ContainerTear
|
||||
private final String imageTemplate;
|
||||
private final String hiveImageTemplate;
|
||||
private final String dockerNetwork;
|
||||
// Whether a cluster whose Applications name Pig gets Apache Pig in its image (floci.services.emr.pig.enabled).
|
||||
private final boolean pigEnabled;
|
||||
// What the NodeManager of a cluster that runs YARN offers its containers.
|
||||
private final ClusterStorage.Yarn nodeManager;
|
||||
private final int maxWorkerNodes;
|
||||
@@ -290,6 +292,7 @@ public class EmrService implements Resettable, StoredResourceArns, ContainerTear
|
||||
this.imageTemplate = config.services().emr().imageTemplate().orElse(null);
|
||||
this.hiveImageTemplate = config.services().emr().hiveImageTemplate().orElse(null);
|
||||
this.dockerNetwork = config.services().emr().dockerNetwork().orElse(null);
|
||||
this.pigEnabled = config.services().emr().pig().enabled();
|
||||
this.nodeManager = new ClusterStorage.Yarn(config.services().emr().yarnNodeMemoryMb(),
|
||||
config.services().emr().yarnNodeVcores());
|
||||
this.maxWorkerNodes = config.services().emr().yarnMaxWorkerNodes();
|
||||
@@ -1683,8 +1686,12 @@ public class EmrService implements Resettable, StoredResourceArns, ContainerTear
|
||||
EmrSparkSteps.HiveCommand hive = EmrSparkSteps.hive(step, cluster.getReleaseLabel(),
|
||||
cluster.getApplications(), cluster.getConfigurations(), SNAPSHOT_MAPPER);
|
||||
boolean yarn = runsYarn(cluster);
|
||||
boolean pig = withPig(cluster);
|
||||
boolean pigMissing = !pig && EmrSparkSteps.installsPig(cluster.getReleaseLabel(), cluster.getApplications(),
|
||||
SNAPSHOT_MAPPER);
|
||||
List<String> application = hive == null ? EmrSparkSteps.application(step, yarn) : null;
|
||||
List<String> command = hive == null && application == null ? EmrSparkSteps.command(step) : null;
|
||||
List<String> command = hive == null && application == null ? EmrSparkSteps.command(step, pigMissing)
|
||||
: null;
|
||||
SparkJobRunner.Storage storage = null;
|
||||
if ((hive != null || application != null || command != null) && clusterStorage != null) {
|
||||
storage = clusterStorage.volumes(cluster.getId());
|
||||
@@ -1730,7 +1737,7 @@ public class EmrService implements Resettable, StoredResourceArns, ContainerTear
|
||||
EmrSparkSteps.sparkDefaults(cluster.getConfigurations(), SNAPSHOT_MAPPER),
|
||||
application != null ? application : List.of(), command, Duration.ZERO,
|
||||
EmrSparkSteps.logFolder(cluster, step), null,
|
||||
EmrSparkSteps.environment(step, cluster.getConfigurations(), SNAPSHOT_MAPPER), List.of(),
|
||||
stepEnvironment(step, cluster, command != null && pig), List.of(),
|
||||
storage, EmrSparkSteps.coreSite(cluster.getConfigurations(), SNAPSHOT_MAPPER),
|
||||
EmrSparkSteps.sparkLog4j2(cluster.getConfigurations(), SNAPSHOT_MAPPER),
|
||||
EmrSparkSteps.mapredSite(cluster.getConfigurations(), SNAPSHOT_MAPPER),
|
||||
@@ -1895,13 +1902,30 @@ public class EmrService implements Resettable, StoredResourceArns, ContainerTear
|
||||
}
|
||||
boolean withHive = EmrSparkSteps.installsHive(cluster.getReleaseLabel(), cluster.getApplications(),
|
||||
SNAPSHOT_MAPPER);
|
||||
if (!withHive && !runsYarn(cluster)) {
|
||||
boolean withPig = withPig(cluster);
|
||||
if (!withHive && !withPig && !runsYarn(cluster)) {
|
||||
return null;
|
||||
}
|
||||
String release = cluster.getReleaseLabel();
|
||||
String spark = EmrServerlessSparkRunner.image(imageTemplate, release);
|
||||
String hive = hdfsImage(release);
|
||||
return () -> releaseImages.image(release, spark, hive, withHive);
|
||||
return () -> releaseImages.image(release, spark, hive, withHive, withPig);
|
||||
}
|
||||
|
||||
// Whether a cluster's image has Floci's Pig: it installs Pig, and floci.services.emr.pig.enabled adds it.
|
||||
private boolean withPig(EmrCluster cluster) {
|
||||
return pigEnabled && EmrSparkSteps.installsPig(cluster.getReleaseLabel(), cluster.getApplications(),
|
||||
SNAPSHOT_MAPPER);
|
||||
}
|
||||
|
||||
// The variables a step that is not a Hive step runs with: its cluster's spark-env exports and its own Properties
|
||||
// (see EmrSparkSteps.environment), and for a command on a cluster with Floci's Pig, its Pig configuration.
|
||||
private static Map<String, String> stepEnvironment(EmrStep step, EmrCluster cluster, boolean pig) {
|
||||
Map<String, String> variables = EmrSparkSteps.environment(step, cluster.getConfigurations(), SNAPSHOT_MAPPER);
|
||||
if (pig) {
|
||||
variables.putAll(EmrSparkSteps.pigEnvironment(cluster.getConfigurations(), SNAPSHOT_MAPPER));
|
||||
}
|
||||
return variables;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -27,12 +27,14 @@ import java.util.Set;
|
||||
* {@code hadoop-streaming} or {@code hadoop-lzo} one through the image's {@code hadoop}, with MapReduce in local
|
||||
* mode; an {@code s3-dist-cp} one runs Floci's own S3DistCp. On a cluster with Hive, whose image holds Hive too, a
|
||||
* {@code command-runner.jar} step running {@code hive-script}, as the AWS CLI writes a Hive step, or {@code hive}
|
||||
* runs Hive.
|
||||
* runs Hive. One running {@code pig-script}, as the AWS CLI writes a Pig step, runs EMR's, which runs the
|
||||
* {@code pig} Floci adds to a cluster with Pig, with Pig's configuration of its own ({@link #pigEnvironment}).
|
||||
*/
|
||||
final class EmrSparkSteps {
|
||||
|
||||
// No source gives AWS's wording for these; they are Floci's.
|
||||
static final String NOT_RUN = "Floci does not run this step: pig-script is not in the image steps run in.";
|
||||
static final String NOT_RUN = "Floci does not run this step: Pig is not in the image steps run in."
|
||||
+ " floci.services.emr.pig.enabled adds Apache Pig to it.";
|
||||
static final String INTERRUPTED = "Floci stopped while the step was running.";
|
||||
|
||||
private static final String COMMAND_RUNNER = "command-runner.jar";
|
||||
@@ -65,8 +67,34 @@ final class EmrSparkSteps {
|
||||
private static final String SPARK_HADOOP_PREFIX = "spark.hadoop.";
|
||||
// spark-submit gives the JVM it runs an application's main class in the Java options of this variable.
|
||||
private static final String SPARK_SUBMIT_OPTS = "SPARK_SUBMIT_OPTS";
|
||||
// The Release Guide's command-runner.jar tools that the EMR Serverless images do not have.
|
||||
private static final Set<String> UNAVAILABLE_TOOLS = Set.of("pig-script");
|
||||
// The AWS CLI's Pig step on a release is command-runner.jar running "pig-script --run-pig-script --args" and the
|
||||
// step's arguments; the image has EMR's pig-script, which runs pig with them, or says "Pig isn't installed.", as
|
||||
// the image has no pig unless Floci adds it.
|
||||
private static final String PIG_SCRIPT = "pig-script";
|
||||
private static final String PIG = "pig";
|
||||
// The Release Guide's Pig classifications: pig-properties will "Change values in Pig's pig.properties file",
|
||||
// pig-log4j "Change values in Pig's log4j.properties file", and pig-env "Change values in the Pig environment",
|
||||
// through its export classification. Pig 0.16.0 and later "use Apache Tez", the Release Guide's Pig considerations
|
||||
// say: "You can explicitly set exectype=mapreduce in the pig-properties configuration classification to use
|
||||
// MapReduce." A cluster's tez-site is Tez's, which Pig takes as it does a Hadoop property, ahead of pig-properties.
|
||||
private static final String PIG_PROPERTIES = "pig-properties";
|
||||
private static final String PIG_LOG4J = "pig-log4j";
|
||||
private static final String PIG_ENV = "pig-env";
|
||||
private static final String TEZ_SITE = "tez-site";
|
||||
private static final String PIG_EXECTYPE = "exectype";
|
||||
private static final String PIG_TEZ = "tez";
|
||||
// Pig's setting for the log4j.properties it reads, which then is the one the cluster's pig-log4j changes.
|
||||
private static final String PIG_LOG4J_CONF = "log4jconf";
|
||||
// Pig sets the file system of each of a script's inputs to the job's working directory, the default file system's
|
||||
// home of the user it runs as, which S3A, Floci's s3://, refuses when it names another file system. Named without
|
||||
// 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";
|
||||
@@ -78,7 +106,7 @@ final class EmrSparkSteps {
|
||||
// hive-site, which will "Change values in Hive's hive-site.xml"; and hive-env's exports, which will "Change
|
||||
// values in the Hive environment".
|
||||
private static final String HIVE_SITE = "hive-site";
|
||||
private static final List<String> HIVE_CLASSIFICATIONS = List.of(CORE_SITE, "tez-site", HIVE_SITE);
|
||||
private static final List<String> HIVE_CLASSIFICATIONS = List.of(CORE_SITE, TEZ_SITE, HIVE_SITE);
|
||||
private static final String HIVE_ENV = "hive-env";
|
||||
// EMR's MapReduce jars are in /usr/lib/hadoop-mapreduce, as the image's own hadoop-streaming script has it,
|
||||
// which the image does not have; its streaming jar is in /usr/lib/hadoop.
|
||||
@@ -145,11 +173,12 @@ final class EmrSparkSteps {
|
||||
|
||||
/**
|
||||
* The command a step that is not a Spark step runs, or null for one Floci does not run: a
|
||||
* {@code command-runner.jar} step's arguments, its command first, unless it names a tool the image does
|
||||
* not have; Floci's S3DistCp with the arguments of an {@code s3-dist-cp} one; a {@code script-runner.jar}
|
||||
* step's script, the first of its arguments, with the rest.
|
||||
* {@code command-runner.jar} step's arguments, its command first, unless it runs {@code pig-script} on a cluster
|
||||
* that installs Pig, which Floci has not added ({@code pigMissing}), where EMR's would run it; on one that does not,
|
||||
* EMR's {@code pig-script} runs and finds no {@code pig}, as it does there. Floci's S3DistCp with the arguments of an
|
||||
* {@code s3-dist-cp} one; a {@code script-runner.jar} step's script, the first of its arguments, with the rest.
|
||||
*/
|
||||
static List<String> command(EmrStep step) {
|
||||
static List<String> command(EmrStep step, boolean pigMissing) {
|
||||
String jar = step.getJar();
|
||||
List<String> args = step.getArgs() != null ? step.getArgs() : List.of();
|
||||
if (jar == null || args.isEmpty()) {
|
||||
@@ -161,7 +190,7 @@ final class EmrSparkSteps {
|
||||
command.addAll(args.subList(1, args.size()));
|
||||
return command;
|
||||
}
|
||||
return named(args.getFirst(), SPARK_SUBMIT) || UNAVAILABLE_TOOLS.contains(args.getFirst())
|
||||
return named(args.getFirst(), SPARK_SUBMIT) || pigMissing && named(args.getFirst(), PIG_SCRIPT)
|
||||
? null : List.copyOf(args);
|
||||
}
|
||||
if (named(jar, SCRIPT_RUNNER)) {
|
||||
@@ -234,18 +263,68 @@ final class EmrSparkSteps {
|
||||
* Spark."
|
||||
*/
|
||||
static boolean installsHive(String releaseLabel, String applications, ObjectMapper objectMapper) {
|
||||
return installs(releaseLabel, applications, objectMapper, HIVE, HCATALOG);
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether a cluster installs Pig: its {@code Applications} name it, unless its release is of the AWS runtime for
|
||||
* Apache Spark, whose release notes say "Pig and Oozie are not included".
|
||||
*/
|
||||
static boolean installsPig(String releaseLabel, String applications, ObjectMapper objectMapper) {
|
||||
return installs(releaseLabel, applications, objectMapper, PIG);
|
||||
}
|
||||
|
||||
// Whether a cluster's Applications name any of names, in any case, unless its release is of the AWS runtime for
|
||||
// Apache Spark, which installs neither Hive, HCatalog nor Pig.
|
||||
private static boolean installs(String releaseLabel, String applications, ObjectMapper objectMapper,
|
||||
String... names) {
|
||||
if (releaseLabel != null && EmrServerlessSparkRunner.sparkRuntime(releaseLabel)) {
|
||||
return false;
|
||||
}
|
||||
for (JsonNode application : stored(applications, objectMapper)) {
|
||||
String name = application.path("Name").asText();
|
||||
if (HIVE.equalsIgnoreCase(name) || HCATALOG.equalsIgnoreCase(name)) {
|
||||
return true;
|
||||
for (String installed : names) {
|
||||
if (installed.equalsIgnoreCase(name)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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,
|
||||
* 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}.
|
||||
*/
|
||||
static Map<String, String> pigEnvironment(String configurations, ObjectMapper objectMapper) {
|
||||
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");
|
||||
}
|
||||
properties.putAll(properties(configurations, objectMapper, TEZ_SITE));
|
||||
properties.putAll(properties(configurations, objectMapper, PIG_PROPERTIES));
|
||||
Map<String, String> variables = new LinkedHashMap<>();
|
||||
variables.put(EmrServerlessSparkRunner.PIG_PROPERTIES_VARIABLE,
|
||||
EmrServerlessSparkRunner.propertiesLines(properties));
|
||||
if (!log4j.isEmpty()) {
|
||||
variables.put(EmrServerlessSparkRunner.PIG_LOG4J_VARIABLE, EmrServerlessSparkRunner.propertiesLines(log4j));
|
||||
}
|
||||
Map<String, String> exports = exports(configurations, objectMapper, PIG_ENV);
|
||||
if (!exports.isEmpty()) {
|
||||
variables.put(EmrServerlessSparkRunner.PIG_ENV_VARIABLE, EmrServerlessSparkRunner.exportLines(exports));
|
||||
}
|
||||
return variables;
|
||||
}
|
||||
|
||||
/**
|
||||
* The S3 folder its YARN applications' logs go to, or null when a cluster has no {@code LogUri}: the Management
|
||||
* Guide's {@code /cluster-id/containers}, "Application container logs. The logs for each YARN application are
|
||||
|
||||
@@ -0,0 +1,214 @@
|
||||
package io.floci.aws.services.emr;
|
||||
|
||||
import io.floci.aws.config.EmulatorConfig;
|
||||
import io.floci.aws.core.common.docker.RetryingTarCopier;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
|
||||
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
|
||||
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URI;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.security.DigestInputStream;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.time.Duration;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HexFormat;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.zip.GZIPInputStream;
|
||||
|
||||
/**
|
||||
* Apache Pig for the image of a cluster that installs it, when {@code floci.services.emr.pig.enabled} is on: no AWS
|
||||
* image carries Pig, so Floci downloads Apache's binary distribution from the first of its URLs that gives it with its
|
||||
* SHA-256, and writes what Pig runs from as the archive of {@link #HOME}, EMR's folder for it. EMR 7.x
|
||||
* releases ship Pig 0.17.0, whose Apache build is "compatible with Hadoop 2.5+ releases"; Floci's default is 0.18.0,
|
||||
* whose {@code pig-0.18.0.tar.gz} "is compatible with Hadoop 3, Spark 3, Hive 3 and HBase 2", the Hadoop the images
|
||||
* hold. Pig runs on the image's Tez, as EMR's runs on EMR's: the Tez 0.10.3 its distribution bundles needs protobuf 3,
|
||||
* which it bundles too, and the image's Tez 0.10.2 protobuf 2.5, so the two would meet on one class path. Both of its own
|
||||
* stay out, and its {@code pig-env.sh} puts the image's Tez on Pig's class path ({@link #ENVIRONMENT}).
|
||||
*/
|
||||
@ApplicationScoped
|
||||
public class PigDistribution {
|
||||
|
||||
/** The folder Pig goes in, under its own name. */
|
||||
static final String FOLDER = "/usr/lib";
|
||||
static final String NAME = "pig";
|
||||
static final String HOME = FOLDER + "/" + NAME;
|
||||
/** The folder of the {@code pig} a step runs, as EMR's {@code pig-script} does the one it finds. */
|
||||
static final String COMMAND_FOLDER = "/usr/bin";
|
||||
/**
|
||||
* That {@code pig}, which runs Pig's launcher by the launcher's own path: the launcher takes Pig's folder from the
|
||||
* path it was run by, so a link would lead it to {@code /usr} instead.
|
||||
*/
|
||||
static final String COMMAND = "#!/bin/bash\nexec " + HOME + "/bin/" + NAME + " \"$@\"\n";
|
||||
// What Pig runs from: its launcher, its configuration, its jars and its licence. The docs, the sources, the tests,
|
||||
// the jars of Pig on Spark, which EMR does not run, the Hadoop it bundles for a machine without one, and its Tez and
|
||||
// the protobuf that Tez needs stay out, as does a pig-env.sh, Floci writing its own.
|
||||
private static final Set<String> KEPT_FOLDERS = Set.of("bin", "conf", "lib");
|
||||
private static final List<String> LEFT_OUT = List.of("lib/spark/", "lib/hadoop2-runtime/", "lib/hadoop3-runtime/",
|
||||
"lib/h3/tez-", "lib/protobuf-java-", "conf/pig-env.sh");
|
||||
/**
|
||||
* Pig's {@code conf/pig-env.sh}, which its launcher reads before it builds its class path: the image's Tez, with
|
||||
* its protobuf 2.5, then the class path a cluster's {@code pig-env} sets, whose exports come first.
|
||||
*/
|
||||
static final String ENVIRONMENT = "export PIG_CLASSPATH=\"/usr/lib/tez/*:/usr/lib/tez/lib/*"
|
||||
+ "${PIG_CLASSPATH:+:$PIG_CLASSPATH}\"\n";
|
||||
private static final String ENVIRONMENT_FILE = NAME + "/conf/pig-env.sh";
|
||||
private static final Set<String> KEPT_FILES = Set.of("LICENSE.txt", "NOTICE.txt");
|
||||
private static final Duration CONNECT_TIMEOUT = Duration.ofSeconds(30);
|
||||
// A download that sends nothing for this long has stalled, and the next URL is tried.
|
||||
private static final Duration READ_TIMEOUT = Duration.ofSeconds(60);
|
||||
private static final int BUFFER_BYTES = 64 * 1024;
|
||||
|
||||
private final List<URI> urls;
|
||||
private final String sha256;
|
||||
private final Duration connectTimeout;
|
||||
private final Duration readTimeout;
|
||||
private final Path downloads;
|
||||
|
||||
@Inject
|
||||
public PigDistribution(EmulatorConfig config) {
|
||||
this(config.services().emr().pig().urls(), config.services().emr().pig().sha256(), CONNECT_TIMEOUT,
|
||||
READ_TIMEOUT, Path.of(System.getProperty("java.io.tmpdir")));
|
||||
}
|
||||
|
||||
PigDistribution(List<String> urls, String sha256, Duration connectTimeout, Duration readTimeout,
|
||||
Path downloads) {
|
||||
this.urls = urls.stream().map(String::trim).filter(url -> !url.isEmpty()).map(URI::create).toList();
|
||||
this.sha256 = sha256.trim();
|
||||
this.connectTimeout = connectTimeout;
|
||||
this.readTimeout = readTimeout;
|
||||
this.downloads = downloads;
|
||||
}
|
||||
|
||||
/** What the image's name says it holds: the distribution, by its checksum. */
|
||||
String identity() {
|
||||
return "pig " + sha256.toLowerCase();
|
||||
}
|
||||
|
||||
/**
|
||||
* Downloads the distribution to a temporary file, which the caller deletes, from the first URL that gives it whole
|
||||
* with its SHA-256: one that cannot be reached, answers otherwise, stalls for {@link #READ_TIMEOUT} or gives another
|
||||
* checksum passes to the next, so nothing goes into an image before it has been checked.
|
||||
*/
|
||||
Path download() throws IOException {
|
||||
List<String> failures = new ArrayList<>();
|
||||
for (URI url : urls) {
|
||||
Path file = Files.createTempFile(downloads, "floci-pig-", ".tar.gz");
|
||||
try {
|
||||
fetch(url, file);
|
||||
return file;
|
||||
} catch (IOException e) {
|
||||
Files.deleteIfExists(file);
|
||||
failures.add(url + ": " + e.getMessage());
|
||||
}
|
||||
}
|
||||
throw new IOException("Could not download Apache Pig: " + (failures.isEmpty() ? "no URL is set"
|
||||
: String.join("; ", failures)));
|
||||
}
|
||||
|
||||
private void fetch(URI url, Path file) throws IOException {
|
||||
MessageDigest digest = sha256Digest();
|
||||
HttpURLConnection connection = (HttpURLConnection) url.toURL().openConnection();
|
||||
connection.setConnectTimeout((int) connectTimeout.toMillis());
|
||||
connection.setReadTimeout((int) readTimeout.toMillis());
|
||||
try {
|
||||
int status = connection.getResponseCode();
|
||||
if (status != HttpURLConnection.HTTP_OK) {
|
||||
throw new IOException("answered " + status);
|
||||
}
|
||||
try (InputStream in = new DigestInputStream(connection.getInputStream(), digest);
|
||||
OutputStream out = Files.newOutputStream(file)) {
|
||||
in.transferTo(out);
|
||||
}
|
||||
} finally {
|
||||
connection.disconnect();
|
||||
}
|
||||
String found = HexFormat.of().formatHex(digest.digest());
|
||||
if (!found.equalsIgnoreCase(sha256)) {
|
||||
throw new IOException("has the SHA-256 " + found + ", not the " + sha256
|
||||
+ " floci.services.emr.pig.sha256 names");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes the folders and files Pig runs from, of the gzipped archive {@code download}, to {@code out} as an archive
|
||||
* of {@link #FOLDER}, under {@link #NAME} in place of the distribution's own folder, owned by root, and
|
||||
* {@link #ENVIRONMENT} as its {@code pig-env.sh}.
|
||||
*/
|
||||
static void write(Path download, OutputStream out) throws IOException {
|
||||
try (InputStream raw = Files.newInputStream(download);
|
||||
GZIPInputStream gzip = new GZIPInputStream(raw, BUFFER_BYTES);
|
||||
TarArchiveInputStream in = new TarArchiveInputStream(gzip);
|
||||
TarArchiveOutputStream tar = RetryingTarCopier.newTarStream(out)) {
|
||||
TarArchiveEntry entry;
|
||||
while ((entry = in.getNextEntry()) != null) {
|
||||
String path = kept(entry.getName());
|
||||
if (path == null || entry.isLink() && kept(entry.getLinkName()) == null) {
|
||||
continue;
|
||||
}
|
||||
entry.setName(path);
|
||||
if (entry.isLink()) {
|
||||
entry.setLinkName(kept(entry.getLinkName()));
|
||||
}
|
||||
entry.setIds(0, 0);
|
||||
entry.setNames("root", "root");
|
||||
tar.putArchiveEntry(entry);
|
||||
in.transferTo(tar);
|
||||
tar.closeArchiveEntry();
|
||||
}
|
||||
TarArchiveEntry environment = new TarArchiveEntry(ENVIRONMENT_FILE);
|
||||
byte[] lines = ENVIRONMENT.getBytes(StandardCharsets.UTF_8);
|
||||
environment.setSize(lines.length);
|
||||
environment.setMode(0644);
|
||||
environment.setIds(0, 0);
|
||||
environment.setNames("root", "root");
|
||||
tar.putArchiveEntry(environment);
|
||||
tar.write(lines);
|
||||
tar.closeArchiveEntry();
|
||||
}
|
||||
}
|
||||
|
||||
private static MessageDigest sha256Digest() {
|
||||
try {
|
||||
return MessageDigest.getInstance("SHA-256");
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
throw new IllegalStateException(e);
|
||||
}
|
||||
}
|
||||
|
||||
// A path of the distribution, its own folder first, under NAME, or null for one Pig does not run from, or that
|
||||
// leaves the folder.
|
||||
static String kept(String path) {
|
||||
if (path == null || path.startsWith("/")) {
|
||||
return null;
|
||||
}
|
||||
int slash = path.indexOf('/');
|
||||
if (slash < 0) {
|
||||
return null;
|
||||
}
|
||||
String rest = path.substring(slash + 1);
|
||||
for (String segment : rest.split("/")) {
|
||||
if (segment.equals("..")) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
if (rest.isEmpty()) {
|
||||
return NAME + "/";
|
||||
}
|
||||
int folderEnd = rest.indexOf('/');
|
||||
boolean kept = folderEnd < 0
|
||||
? KEPT_FILES.contains(rest) || rest.endsWith(".jar")
|
||||
: KEPT_FOLDERS.contains(rest.substring(0, folderEnd)) && LEFT_OUT.stream().noneMatch(rest::startsWith);
|
||||
return kept ? NAME + "/" + rest : null;
|
||||
}
|
||||
}
|
||||
@@ -1,20 +1,20 @@
|
||||
package io.floci.aws.services.emr;
|
||||
|
||||
/**
|
||||
* The images a cluster runs in, in the EMR {@code docker} runner mode: two per release, its EMR Serverless image with
|
||||
* its EMR Serverless Hive image's HDFS, MapReduce and YARN added, and for a cluster with Hive its Hive too, as an EMR
|
||||
* cluster's nodes hold every application it installs; a release of the AWS runtime for Apache Spark, which has no Hive
|
||||
* image, has one, with the Hadoop of another release's kept apart. {@link EmrReleaseImages} is the Docker implementation,
|
||||
* and a test supplies its own.
|
||||
* The images a cluster runs in, in the EMR {@code docker} runner mode: its release's EMR Serverless image with its EMR
|
||||
* Serverless Hive image's HDFS, MapReduce and YARN added, and for a cluster with Hive its Hive too, for one with Pig
|
||||
* Tez and Apache Pig, as an EMR cluster's nodes hold every application it installs. A release of the AWS runtime for
|
||||
* Apache Spark, which has no Hive image, has one, with the Hadoop of another release's kept apart.
|
||||
* {@link EmrReleaseImages} is the Docker implementation, and a test supplies its own.
|
||||
*/
|
||||
public interface ReleaseImages {
|
||||
|
||||
/**
|
||||
* The id of the image of {@code releaseLabel} made of {@code sparkImage} and {@code hiveImage}, with Hive or with
|
||||
* Hadoop's services alone, made first when the Docker daemon does not have it, which may take as long as pulling
|
||||
* the two; a failure is thrown.
|
||||
* Hadoop's services alone, and with Pig or without, made first when the Docker daemon does not have it, which may
|
||||
* take as long as pulling the two, and downloading Pig; a failure is thrown.
|
||||
*/
|
||||
String image(String releaseLabel, String sparkImage, String hiveImage, boolean withHive);
|
||||
String image(String releaseLabel, String sparkImage, String hiveImage, boolean withHive, boolean withPig);
|
||||
|
||||
/**
|
||||
* The step or the bootstrap that {@link #image} gave {@code imageId} to has ended, so a newer image of its kind may
|
||||
|
||||
@@ -101,6 +101,8 @@ public class EmrServerlessSparkRunner implements SparkJobRunner, ContainerTeardo
|
||||
// Where a step on its cluster's YARN finds the ResourceManager, as a yarn-site.xml property, a Hadoop client reading
|
||||
// yarn-site.xml after core-site.xml.
|
||||
private static final String YARN_SITE_VARIABLE = "FLOCI_YARN_SITE";
|
||||
// How a command's Tez, Pig's among them, runs its DAGs, as tez-site.xml properties (see tezSite).
|
||||
private static final String TEZ_SITE_VARIABLE = "FLOCI_TEZ_SITE";
|
||||
// The folder of a step's image holding the Hadoop its cluster's nodes run from, which its hadoop command runs from
|
||||
// on YARN, as the MapReduce home the local job runner uses has no YARN client to speak of.
|
||||
private static final String HADOOP_ROOT_VARIABLE = "FLOCI_HADOOP_ROOT";
|
||||
@@ -219,18 +221,19 @@ public class EmrServerlessSparkRunner implements SparkJobRunner, ContainerTeardo
|
||||
"fi",
|
||||
"export SPARK_CONF_DIR=\"$conf\"");
|
||||
// On a cluster, the hadoop command, and MapReduce in a step's spark-submit, get its HDFS and Floci's S3 too,
|
||||
// through a copy of its Hadoop configuration (the image's, or the folder a spark-env export names), links
|
||||
// followed, whose core-site.xml takes in the original's through Hadoop's XInclude, whatever its XML, by a name
|
||||
// relative to the folder, which Spark ships to YARN's containers on a cluster that runs it, then
|
||||
// HADOOP_SITE_VARIABLE's properties, which win (see hadoopSite), and whose mapred-site.xml runs MapReduce in local
|
||||
// mode the same way (see mapredSite). The cluster's hadoop-log4j lines follow the log4j.properties, on a line of
|
||||
// their own, a later key winning, and its hadoop-env exports start the hadoop-env.sh the hadoop command reads,
|
||||
// ahead of the image's own lines, which add to HADOOP_CLASSPATH rather than set it, so a HADOOP_CLASSPATH the
|
||||
// cluster sets keeps the LZO codec the image's core-site.xml names. The hadoop script runs only once its HDFS,
|
||||
// MapReduce and YARN homes name folders that are there, and the image has /usr/lib/hadoop alone, without
|
||||
// MapReduce, so the MapReduce home is Floci's (see MAPRED_HOME). The image's hdfs command cannot run, so hdfs dfs
|
||||
// runs as hadoop fs, and its hadoop-streaming names a jar it does not have, so the one it has runs. A step's
|
||||
// command can run sudo, as on EMR, which the image does not have, so it finds Floci's first (see SUDO).
|
||||
// through a copy of its Hadoop configuration (the image's, or the folder a spark-env export names), links followed,
|
||||
// whose core-site.xml takes in the original's through Hadoop's XInclude, whatever its XML, by a name relative to
|
||||
// the folder, which Spark ships to YARN's containers on a cluster that runs it, then HADOOP_SITE_VARIABLE's
|
||||
// properties, which win (see hadoopSite), and whose mapred-site.xml runs MapReduce in local mode the same way (see
|
||||
// mapredSite), as a command's tez-site.xml does Tez (see tezSite). The cluster's hadoop-log4j lines follow the
|
||||
// log4j.properties, on a line of their own, a later key winning, and its hadoop-env exports start the hadoop-env.sh
|
||||
// the hadoop command reads, ahead of the image's own lines, which add to HADOOP_CLASSPATH rather than set it, so a
|
||||
// HADOOP_CLASSPATH the cluster sets keeps the LZO codec the image's core-site.xml names. The hadoop script runs
|
||||
// only once its HDFS, MapReduce and YARN homes name folders that are there, and the image has /usr/lib/hadoop
|
||||
// alone, without MapReduce, so the MapReduce home is Floci's (see MAPRED_HOME). The image's hdfs command cannot
|
||||
// run, so hdfs dfs runs as hadoop fs, and its hadoop-streaming names a jar it does not have, so the one it has
|
||||
// runs. A step's command can run sudo, as on EMR, which the image does not have, so it finds Floci's first (see
|
||||
// SUDO).
|
||||
private static final List<String> HADOOP_CONF_SETUP = List.of(
|
||||
"if [ -n \"${" + HADOOP_SITE_VARIABLE + ":-}\" ]; then",
|
||||
" hadoop_conf=/tmp/floci-hadoop-conf source_conf=\"${HADOOP_CONF_DIR:-/etc/hadoop/conf}\"",
|
||||
@@ -253,6 +256,9 @@ public class EmrServerlessSparkRunner implements SparkJobRunner, ContainerTeardo
|
||||
" if [ -n \"${" + YARN_SITE_VARIABLE + ":-}\" ]; then",
|
||||
" site yarn-site.xml \"$" + YARN_SITE_VARIABLE + "\"",
|
||||
" fi",
|
||||
" if [ -n \"${" + TEZ_SITE_VARIABLE + ":-}\" ]; then",
|
||||
" site tez-site.xml \"$" + TEZ_SITE_VARIABLE + "\"",
|
||||
" fi",
|
||||
" if [ ! -s \"$hadoop_conf/log4j.properties\" ]; then",
|
||||
" printf '%s\\n' '" + String.join("' '", HADOOP_CONSOLE_LOG) + "' > \"$hadoop_conf/log4j.properties\"",
|
||||
" fi",
|
||||
@@ -294,6 +300,38 @@ public class EmrServerlessSparkRunner implements SparkJobRunner, ContainerTeardo
|
||||
" chmod +x /tmp/floci-bin/hadoop /tmp/floci-bin/hadoop-streaming",
|
||||
" fi",
|
||||
"fi");
|
||||
/**
|
||||
* The lines a step's command gets after those of Pig's own {@code pig.properties}, a later key winning, in a Pig
|
||||
* configuration of the container's own ({@link #PIG_CONF}), which {@code PIG_CONF_DIR} names; set for a cluster
|
||||
* with Pig.
|
||||
*/
|
||||
public static final String PIG_PROPERTIES_VARIABLE = "FLOCI_PIG_PROPERTIES";
|
||||
/** The lines that follow those of that configuration's {@code log4j.properties}, its template's where it has none. */
|
||||
public static final String PIG_LOG4J_VARIABLE = "FLOCI_PIG_LOG4J";
|
||||
/** The lines that start that configuration's {@code pig-env.sh}, which Pig's launcher reads. */
|
||||
public static final String PIG_ENV_VARIABLE = "FLOCI_PIG_ENV";
|
||||
/** Where a step's command has its Pig configuration. */
|
||||
public static final String PIG_CONF = "/tmp/floci-pig-conf";
|
||||
// On a cluster with Pig, a command's pig reads a configuration of its own: a copy of the one PIG_CONF_DIR names, or
|
||||
// Pig's, links followed, whose pig.properties, log4j.properties and pig-env.sh get the cluster's lines.
|
||||
private static final List<String> PIG_CONF_SETUP = List.of(
|
||||
"if [ -n \"${" + PIG_PROPERTIES_VARIABLE + ":-}\" ]; then",
|
||||
" mkdir -p " + PIG_CONF,
|
||||
" cp -rL \"${PIG_CONF_DIR:-/usr/lib/pig/conf}/.\" " + PIG_CONF + " 2>/dev/null",
|
||||
" printf '\\n%s\\n' \"$" + PIG_PROPERTIES_VARIABLE + "\" >> " + PIG_CONF + "/pig.properties",
|
||||
" if [ -n \"${" + PIG_LOG4J_VARIABLE + ":-}\" ]; then",
|
||||
" if [ ! -f " + PIG_CONF + "/log4j.properties ]; then",
|
||||
" cp " + PIG_CONF + "/log4j.properties.template " + PIG_CONF + "/log4j.properties 2>/dev/null",
|
||||
" fi",
|
||||
" printf '\\n%s\\n' \"$" + PIG_LOG4J_VARIABLE + "\" >> " + PIG_CONF + "/log4j.properties",
|
||||
" fi",
|
||||
" if [ -n \"${" + PIG_ENV_VARIABLE + ":-}\" ]; then",
|
||||
" { printf '%s\\n' \"$" + PIG_ENV_VARIABLE + "\"; cat " + PIG_CONF + "/pig-env.sh 2>/dev/null; }"
|
||||
+ " > " + PIG_CONF + "/floci-pig-env.sh",
|
||||
" mv " + PIG_CONF + "/floci-pig-env.sh " + PIG_CONF + "/pig-env.sh",
|
||||
" fi",
|
||||
" export PIG_CONF_DIR=" + PIG_CONF,
|
||||
"fi");
|
||||
// A step on a cluster, or one whose job has log4j2.properties lines, runs spark-submit after this.
|
||||
private static final String SPARK_CONF_WRAPPER = wrapper(SPARK_CONF_COPY, HADOOP_CONF_SETUP);
|
||||
// A command runs after this sets up a Spark configuration of its own, so a spark-submit it starts runs
|
||||
@@ -305,7 +343,7 @@ public class EmrServerlessSparkRunner implements SparkJobRunner, ContainerTeardo
|
||||
"sed -i -e '/^spark\\.master[[:space:]]/d' -e '/^spark\\.submit\\.deployMode[[:space:]]/d'"
|
||||
+ " -e '/^spark\\.submit\\.customResourceManager/d' \"$conf/spark-defaults.conf\"",
|
||||
"printf '\\n%s\\n' \"$" + SPARK_DEFAULTS_VARIABLE + "\" >> \"$conf/spark-defaults.conf\""),
|
||||
HADOOP_CONF_SETUP);
|
||||
HADOOP_CONF_SETUP, PIG_CONF_SETUP);
|
||||
// The archives a driver needs in its working directory are put in this folder of its container, each in a
|
||||
// numbered folder of its own, which the copy creates.
|
||||
private static final String ARCHIVES_FOLDER = "/tmp/floci-archives";
|
||||
@@ -342,6 +380,12 @@ public class EmrServerlessSparkRunner implements SparkJobRunner, ContainerTeardo
|
||||
private static final List<SparkSetting> HIVE_SETTINGS = List.of(
|
||||
new SparkSetting("tez.local.mode", "true"),
|
||||
new SparkSetting("tez.am.client.am.port-range", "40000-40100"));
|
||||
// A command's Tez where there is no YARN: in local mode, in the JVM that starts it, which has Tez's jars already,
|
||||
// its tasks' output read where it is written, as Pig's own Tez local mode has it.
|
||||
private static final List<SparkSetting> TEZ_LOCAL_SETTINGS = List.of(
|
||||
new SparkSetting("tez.local.mode", "true"),
|
||||
new SparkSetting("tez.ignore.lib.uris", "true"),
|
||||
new SparkSetting("tez.runtime.optimize.local.fetch", "true"));
|
||||
// On a cluster, Hive's metastore is the cluster's own, as "Hive records metastore information in a MySQL database
|
||||
// on the primary node's file system": the metastore service on its primary node (see EmrClusterStorage), in place
|
||||
// of the Glue Data Catalog client the image's hive-site names, or, where that does not run, an embedded Derby
|
||||
@@ -558,6 +602,9 @@ public class EmrServerlessSparkRunner implements SparkJobRunner, ContainerTeardo
|
||||
.withEnv(YARN_SITE_VARIABLE, siteProperty("yarn.resourcemanager.hostname",
|
||||
job.storage().resourceManager()));
|
||||
}
|
||||
if (job.command() != null) {
|
||||
container.withEnv(TEZ_SITE_VARIABLE, tezSite(job, executorEnvironment));
|
||||
}
|
||||
if (job.storage() != null && job.storage().replication() > 0) {
|
||||
container.withEnv(HDFS_SITE_VARIABLE, siteProperty("dfs.replication",
|
||||
String.valueOf(job.storage().replication())));
|
||||
@@ -1225,19 +1272,51 @@ public class EmrServerlessSparkRunner implements SparkJobRunner, ContainerTeardo
|
||||
new SparkSetting("mapreduce.reduce.memory.mb", "768"));
|
||||
}
|
||||
|
||||
// Hive's Tez on the cluster's YARN. The image's tez-site.xml names EMR Serverless's own plugins, whose stock
|
||||
// counterparts are in the same jars, named here as Tez's own defaults are; its port ranges and addresses are
|
||||
// placeholders EMR Serverless fills, its compression needs a native library the image lacks, and a task's two
|
||||
// virtual cores never match the one YARN's default scheduler grants, so Tez would give each container back. An
|
||||
// application master says where it is by ${env.NM_HOST}, which is left for its own node to fill in: the settings
|
||||
// are read on the step's side first, and $NM_HOST or an env of the step's would be filled in there.
|
||||
/**
|
||||
* The {@code tez-site.xml} properties a step's command gets, so that Tez, which Pig runs a script on by default,
|
||||
* runs its DAGs on its cluster's YARN, as Hive's does there, or, where there is no YARN to run them on, in the JVM
|
||||
* that starts them, without the jars a YARN container is given.
|
||||
*/
|
||||
static String tezSite(Job job, List<String> executorEnvironment) {
|
||||
List<SparkSetting> settings = new ArrayList<>();
|
||||
if (onYarn(job)) {
|
||||
settings.addAll(tezOnYarnSettings(job, executorEnvironment, List.of()));
|
||||
settings.add(new SparkSetting("tez.task.resource.memory.mb", "768"));
|
||||
} else {
|
||||
settings.addAll(TEZ_LOCAL_SETTINGS);
|
||||
}
|
||||
List<String> properties = new ArrayList<>();
|
||||
settings.forEach(setting -> properties.add(siteProperty(setting.key(), setting.value())));
|
||||
return String.join("\n", properties);
|
||||
}
|
||||
|
||||
// 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.
|
||||
private static List<SparkSetting> hiveOnYarnSettings(Job job, List<String> executorEnvironment) {
|
||||
String root = hadoopRoot(job);
|
||||
List<String> classPath = new ArrayList<>(List.of("$PWD/*", root + "/hive/lib/hive-exec.jar", root + "/tez/*",
|
||||
root + "/tez/lib/*"));
|
||||
classPath.addAll(hadoopLayout(root));
|
||||
String environment = containerEnvironment(executorEnvironment, "HIVE_HOME=" + root + "/hive",
|
||||
"TEZ_HOME=" + root + "/tez");
|
||||
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"));
|
||||
return settings;
|
||||
}
|
||||
|
||||
// Tez on the cluster's YARN, its containers given classPath, then Tez's and Hadoop's jars, and variables, then
|
||||
// TEZ_HOME. The image's tez-site.xml names EMR Serverless's own plugins, whose stock counterparts are in the same
|
||||
// jars, named here as Tez's own defaults are; its port ranges and addresses are placeholders EMR Serverless fills,
|
||||
// and its compression needs a native library the image lacks. An application master says where it is by
|
||||
// ${env.NM_HOST}, which is left for its own node to fill in: the settings are read on the step's side first, and
|
||||
// $NM_HOST or an env of the step's would be filled in there.
|
||||
private static List<SparkSetting> tezOnYarnSettings(Job job, List<String> executorEnvironment,
|
||||
List<String> classPath, String... variables) {
|
||||
String root = hadoopRoot(job);
|
||||
List<String> containerClassPath = new ArrayList<>(List.of("$PWD/*"));
|
||||
containerClassPath.addAll(classPath);
|
||||
containerClassPath.addAll(List.of(root + "/tez/*", root + "/tez/lib/*"));
|
||||
containerClassPath.addAll(hadoopLayout(root));
|
||||
List<String> containerVariables = new ArrayList<>(List.of(variables));
|
||||
containerVariables.add("TEZ_HOME=" + root + "/tez");
|
||||
String environment = containerEnvironment(executorEnvironment, containerVariables.toArray(String[]::new));
|
||||
return List.of(
|
||||
new SparkSetting("yarn.resourcemanager.hostname", job.storage().resourceManager()),
|
||||
new SparkSetting("tez.local.mode", "false"),
|
||||
@@ -1258,12 +1337,10 @@ public class EmrServerlessSparkRunner implements SparkJobRunner, ContainerTeardo
|
||||
new SparkSetting("tez.shuffle.bind-host", "0.0.0.0"),
|
||||
new SparkSetting("tez.lib.uris", "file://" + root + "/tez/,file://" + root + "/tez/lib/"),
|
||||
new SparkSetting("tez.use.cluster.hadoop-libs", "false"),
|
||||
new SparkSetting("tez.cluster.additional.classpath.prefix", String.join(":", classPath)),
|
||||
new SparkSetting("tez.cluster.additional.classpath.prefix", String.join(":", containerClassPath)),
|
||||
new SparkSetting("tez.am.launch.env", environment),
|
||||
new SparkSetting("tez.task.launch.env", environment),
|
||||
new SparkSetting("tez.am.resource.memory.mb", "1024"),
|
||||
new SparkSetting("hive.tez.container.size", "768"),
|
||||
new SparkSetting("hive.tez.cpu.vcores", "1"),
|
||||
new SparkSetting("tez.runtime.intermediate-output.should-compress", "false"),
|
||||
new SparkSetting("tez.runtime.intermediate-input.is-compressed", "false"));
|
||||
}
|
||||
@@ -1307,8 +1384,16 @@ public class EmrServerlessSparkRunner implements SparkJobRunner, ContainerTeardo
|
||||
* is left out, as its line would run what it holds.
|
||||
*/
|
||||
static String hadoopEnvFile(Job job) {
|
||||
return exportLines(job.hadoopEnvironment());
|
||||
}
|
||||
|
||||
/**
|
||||
* An export of each of {@code variables}, as given, in single quotes, with no shell expansion. A name a shell cannot
|
||||
* export is left out, as its line would run what it holds.
|
||||
*/
|
||||
public static String exportLines(Map<String, String> variables) {
|
||||
List<String> lines = new ArrayList<>();
|
||||
job.hadoopEnvironment().forEach((name, value) -> {
|
||||
variables.forEach((name, value) -> {
|
||||
if (SHELL_NAME.matcher(name).matches()) {
|
||||
lines.add("export " + name + "='" + value.replace("'", "'\\''") + "'");
|
||||
}
|
||||
@@ -1316,8 +1401,8 @@ public class EmrServerlessSparkRunner implements SparkJobRunner, ContainerTeardo
|
||||
return String.join("\n", lines);
|
||||
}
|
||||
|
||||
// Each property as a line Properties.load reads back as it is.
|
||||
private static String propertiesLines(Map<String, String> properties) {
|
||||
/** Each property as a line {@code Properties.load} reads back as it is. */
|
||||
public static String propertiesLines(Map<String, String> properties) {
|
||||
List<String> lines = new ArrayList<>();
|
||||
properties.forEach((key, value) ->
|
||||
lines.add(propertiesEscaped(key, true) + " = " + propertiesEscaped(value, false)));
|
||||
|
||||
Reference in new issue
Block a user