diff --git a/src/main/java/io/floci/aws/services/emr/EmrClusterStorage.java b/src/main/java/io/floci/aws/services/emr/EmrClusterStorage.java index 5d6d1c90fb..0e1dfd92c9 100644 --- a/src/main/java/io/floci/aws/services/emr/EmrClusterStorage.java +++ b/src/main/java/io/floci/aws/services/emr/EmrClusterStorage.java @@ -144,6 +144,7 @@ public class EmrClusterStorage implements ClusterStorage, ContainerTeardown { "address=$FLOCI_HDFS_BIND; [ \"$address\" != 0.0.0.0 ] || address=$(hostname -i | awk '{print $1}')", "echo \"$address $FLOCI_HDFS_HOST\" >> /etc/hosts || exit", "chown hadoop:hadoop /mnt || exit", + EmrServerlessSparkRunner.HIVE_ONE_SDK, "mkdir -p " + HDFS_CONF + " || exit", "printf '%s\\n' ''" + " \"fs.defaultFShdfs://$FLOCI_HDFS_HOST:8020\"" diff --git a/src/main/java/io/floci/aws/services/emrserverless/EmrServerlessSparkRunner.java b/src/main/java/io/floci/aws/services/emrserverless/EmrServerlessSparkRunner.java index cdc543f042..534b6af268 100644 --- a/src/main/java/io/floci/aws/services/emrserverless/EmrServerlessSparkRunner.java +++ b/src/main/java/io/floci/aws/services/emrserverless/EmrServerlessSparkRunner.java @@ -260,6 +260,19 @@ public class EmrServerlessSparkRunner implements SparkJobRunner, ContainerTeardo * path, apart from the one its other steps' Hadoop reads; Hive runs with it where an image has it. */ public static final String HIVE_HADOOP_CONF = "/etc/hadoop/conf.hive"; + private static final String HIVE_LIB = "/usr/lib/hive/lib"; + /** + * Leaves Hive one AWS SDK v2, before Hive starts, as root. An {@code emr-6.x} Hive image's lib folder has the stock + * SDK's modules, ahead on Hive's class path of the SDK inside Tez's plug-in, EMR's own build and the only one with + * an S3 client. S3A, which {@code s3://} is here, then builds the plug-in's S3 client on the stock {@code sdk-core}, + * which on {@code emr-6.15.0} fails with {@code NoSuchFieldError: AWS_S3_DISABLE_EXPRESS_SESSION_AUTH}. The modules + * the plug-in also holds go, by name, which is all but CloudWatch's two. + */ + public static final String HIVE_ONE_SDK = "for core in " + HIVE_LIB + "/sdk-core-2.*.jar; do" + + " [ -e /usr/lib/tez/tez-emr-serverless.jar ] && for module in annotations apache-client auth aws-core" + + " aws-json-protocol aws-query-protocol endpoints-spi http-client-spi json-utils metrics-spi" + + " netty-nio-client profiles protocol-core regions sdk-core third-party-jackson-core utils;" + + " do rm -f \"" + HIVE_LIB + "/$module-${core##*/sdk-core-}\"; done; done"; private static final String HIVE_SCRATCHDIR = "hive.exec.scratchdir"; // The user every EMR Serverless image runs as: a custom one sets "the USER back to hadoop:hadoop". private static final String IMAGE_USER = "hadoop"; @@ -281,6 +294,7 @@ public class EmrServerlessSparkRunner implements SparkJobRunner, ContainerTeardo + HIVE_SCRATCH_VARIABLE + "\" || exit", "fi", "if [ -d " + HIVE_HADOOP_CONF + " ]; then export HADOOP_CONF_DIR=" + HIVE_HADOOP_CONF + "; fi", + HIVE_ONE_SDK, "export HOME=~" + IMAGE_USER, "cd \"$HOME\" || exit", // The image's user has no other group, as Docker starts it. diff --git a/src/test/java/io/floci/aws/services/emr/EmrClusterStorageTest.java b/src/test/java/io/floci/aws/services/emr/EmrClusterStorageTest.java index 2aa9888ce4..78cab4b082 100644 --- a/src/test/java/io/floci/aws/services/emr/EmrClusterStorageTest.java +++ b/src/test/java/io/floci/aws/services/emr/EmrClusterStorageTest.java @@ -28,6 +28,7 @@ import io.floci.aws.core.common.docker.ContainerSpec; import io.floci.aws.core.common.docker.ContainerStorageHelper; import io.floci.aws.core.common.docker.CurrentContainerNetworkResolver; import io.floci.aws.core.common.docker.LaunchedContainerAwsEnv; +import io.floci.aws.services.emrserverless.EmrServerlessSparkRunner; import io.floci.aws.services.emrserverless.SparkJobRunner; import io.floci.aws.services.iam.model.SessionCreds; import io.floci.aws.services.lambda.launcher.ImageCacheService; @@ -546,6 +547,11 @@ class EmrClusterStorageTest { + "[ \"$(jobs -rp | wc -l)\" = $n ] || exit 1; sleep 0.2; done; " + "fi; if [ -n \"$FLOCI_YARN_MEMORY_MB\" ]; then"))); + // A table's location in S3 is the metastore's to look at, with S3A, so it too leaves Hive one SDK first, as + // root, which owns Hive's lib folder. + verify(spec).withEntrypoint(argThat(entrypoint -> entrypoint.get(2).contains( + "chown hadoop:hadoop /mnt || exit\n" + EmrServerlessSparkRunner.HIVE_ONE_SDK + "\n"))); + // The session goes with the container. storage.remove("j-1"); verify(roleCredentials).unregister("123456789012", "ASIAMETASTORE"); diff --git a/src/test/java/io/floci/aws/services/emrserverless/EmrServerlessSparkRunnerTest.java b/src/test/java/io/floci/aws/services/emrserverless/EmrServerlessSparkRunnerTest.java index 87b694c879..9c4de5ee59 100644 --- a/src/test/java/io/floci/aws/services/emrserverless/EmrServerlessSparkRunnerTest.java +++ b/src/test/java/io/floci/aws/services/emrserverless/EmrServerlessSparkRunnerTest.java @@ -49,6 +49,7 @@ import java.util.Optional; import java.util.Properties; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicLong; +import java.util.stream.Stream; import java.util.zip.GZIPInputStream; import java.util.zip.GZIPOutputStream; import java.util.zip.ZipEntry; @@ -215,6 +216,8 @@ class EmrServerlessSparkRunnerTest { verify(spec).withEntrypoint(argThat((List entrypoint) -> entrypoint.size() == 5 && List.of("/bin/bash", "-c").equals(entrypoint.subList(0, 2)) && entrypoint.get(2).contains("setpriv --reuid=hadoop --regid=hadoop --clear-groups \"$@\"") + // While it is still root, which owns Hive's lib folder. + && entrypoint.get(2).contains(EmrServerlessSparkRunner.HIVE_ONE_SDK + "\nexport HOME=~hadoop\n") && List.of("floci", "/usr/bin/hive").equals(entrypoint.subList(3, 5)))); verify(spec).withCmd(argThat((List cmd) -> "--hiveconf".equals(cmd.getFirst()) && List.of("-f", "s3://bucket/queries/report.sql").equals(cmd.subList(cmd.size() - 2, cmd.size())))); @@ -1062,6 +1065,56 @@ class EmrServerlessSparkRunnerTest { assertFalse(Files.exists(folder.resolve("ran"))); } + @Test + void anEmr6HiveImageKeepsOneSdkWithCloudWatchsModulesAndWhatIsNotTheSdk(@TempDir Path folder) throws Exception { + List kept = List.of("auth-2.19.0.jar", "cloudwatch-2.20.160.jar", + "cloudwatch-metric-publisher-2.20.160.jar", "guava-31.1-jre.jar", "hive-exec-3.1.3-amzn-8.jar", + "my-library-2.20.160.jar"); + List jars = new ArrayList<>(kept); + for (String module : List.of("annotations", "apache-client", "auth", "aws-core", "aws-json-protocol", + "aws-query-protocol", "endpoints-spi", "http-client-spi", "json-utils", "metrics-spi", + "netty-nio-client", "profiles", "protocol-core", "regions", "sdk-core", "third-party-jackson-core", + "utils")) { + jars.add(module + "-2.20.160.jar"); + } + + assertEquals(kept, hiveLibAfterOneSdk(folder, true, jars.toArray(String[]::new))); + } + + @Test + void anImageWithoutStockSdkModulesKeepsEveryJar(@TempDir Path folder) throws Exception { + List jars = List.of("commons-math-2.2.jar", "guava-31.1-jre.jar", "hive-exec-3.1.3-amzn-14.jar"); + + assertEquals(jars, hiveLibAfterOneSdk(folder, true, jars.toArray(String[]::new))); + } + + @Test + void anImageWithoutTezsPluginKeepsItsOnlySdk(@TempDir Path folder) throws Exception { + List jars = List.of("aws-core-2.20.160.jar", "sdk-core-2.20.160.jar"); + + assertEquals(jars, hiveLibAfterOneSdk(folder, false, jars.toArray(String[]::new))); + } + + /** What is left of a Hive lib folder holding {@code jars} once the shell has run, by name. */ + private static List hiveLibAfterOneSdk(Path folder, boolean plugin, String... jars) throws Exception { + Path lib = Files.createDirectories(folder.resolve("hive/lib")); + Path tez = Files.createDirectories(folder.resolve("tez")); + for (String jar : jars) { + Files.createFile(lib.resolve(jar)); + } + if (plugin) { + Files.createFile(tez.resolve("tez-emr-serverless.jar")); + } + String script = EmrServerlessSparkRunner.HIVE_ONE_SDK.replace("/usr/lib/hive/lib", lib.toString()) + .replace("/usr/lib/tez", tez.toString()); + + BootstrapProcess.run(new ProcessBuilder("bash", "-c", script).directory(folder.toFile())); + + try (Stream left = Files.list(lib)) { + return left.map(path -> path.getFileName().toString()).sorted().toList(); + } + } + @Test void log4j2LinesReachLog4jAsTheClusterGaveThem() throws IOException { Map sparkLog4j2 = new LinkedHashMap<>();