vaibhavk1992 commented on code in PR #843: URL: https://github.com/apache/incubator-xtable/pull/843#discussion_r3572081533
########## xtable-spark-runtime/src/test/java/org/apache/xtable/spark/ITXTableSparkRuntimeBundle.java: ########## @@ -0,0 +1,297 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.xtable.spark; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assumptions.assumeTrue; + +import java.io.File; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.TimeUnit; +import java.util.stream.Stream; + +import org.apache.spark.api.java.JavaSparkContext; +import org.apache.spark.sql.Dataset; +import org.apache.spark.sql.Row; +import org.apache.spark.sql.SparkSession; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.io.TempDir; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +import org.apache.hudi.client.HoodieReadClient; + +import org.apache.xtable.GenericTable; +import org.apache.xtable.hudi.HudiTestUtil; + +/** + * Validates that the relocated {@code xtable-spark-runtime} bundle jar is self-contained by running + * an actual XTable sync via {@code $SPARK_HOME/bin/spark-submit} for one case per direction, and + * asserting the target is data-equivalent to the source. Engines (and the Avro/Parquet versions + * they require) are supplied to the submit on a flat classpath via {@code + * spark.driver.extraClassPath} from {@code target/engine-classpath.txt}, never bundled; a flat + * classpath keeps a single Avro on one loader, matching a real cluster. Skipped when {@code + * SPARK_HOME} is not set. + * + * <p>Covers Hudi, Iceberg and Delta as both source and target (one case per direction). Hudi is + * supplied via the {@code hudi-spark} bundle (its regenerated Avro model classes link on Avro 1.12, + * unlike raw hudi-common), so a {@code SPARK_HOME} matching the Hudi/Delta line (Spark 3.4 for the + * Hudi 0.x line) is required. Local-timestamp columns are excluded from the equivalence check due + * to known Hudi/Iceberg representation differences (HUDI-7088). + */ +class ITXTableSparkRuntimeBundle { + + @TempDir static java.nio.file.Path tempDir; + + private static SparkSession sparkSession; + private static JavaSparkContext jsc; + + @BeforeAll + static void setupSpark() { + sparkSession = + SparkSession.builder() + .config(HoodieReadClient.addHoodieSupport(HudiTestUtil.getSparkConf(tempDir))) + .getOrCreate(); + jsc = JavaSparkContext.fromSparkContext(sparkSession.sparkContext()); + } + + @AfterAll + static void stopSpark() { + if (jsc != null) { + jsc.close(); + } + if (sparkSession != null) { + sparkSession.stop(); + } + } + + private static Stream<Arguments> directions() { + // sourceFormat, targetFormat, hudiPartitionSpec (only used for a partitioned Hudi source) + return Stream.of( + Arguments.of("HUDI", "ICEBERG", "level:VALUE"), + Arguments.of("ICEBERG", "HUDI", null), + Arguments.of("HUDI", "DELTA", "level:VALUE"), + Arguments.of("DELTA", "HUDI", null)); + } + + @ParameterizedTest(name = "{0} -> {1}") + @MethodSource("directions") + void bundleRunsSyncViaSparkSubmit(String sourceFormat, String targetFormat, String partitionSpec) + throws Exception { + String sparkHome = System.getenv("SPARK_HOME"); + assumeTrue(sparkHome != null && !sparkHome.trim().isEmpty(), "SPARK_HOME is not set"); + + String tableName = sourceFormat.toLowerCase() + "_to_" + targetFormat.toLowerCase(); + String basePath; + String dataPath; + List<String> columns; + String orderByColumn; + try (GenericTable<?, ?> table = + GenericTable.getInstance(tableName, tempDir, sparkSession, jsc, sourceFormat, true)) { + table.insertRows(100); + basePath = table.getBasePath(); + // Targets write metadata alongside the data files; for Iceberg this is <basePath>/data. + dataPath = table.getDataPath(); + columns = table.getColumnsToSelect(); + orderByColumn = table.getOrderByColumn(); + } + + File bundleJar = findBundleJar(); + File sparkSubmit = new File(sparkHome, "bin/spark-submit"); + assertTrue(sparkSubmit.canExecute(), "spark-submit not executable at " + sparkSubmit); + + // Engines are provided on a FLAT classpath (single avro on one loader), NOT via --packages, + // which would layer them in a child classloader and break cross-boundary avro casts. + String engineClasspath = readEngineClasspath(); + + List<String> command = new ArrayList<>(); + command.add(sparkSubmit.getAbsolutePath()); + command.add("--master"); + command.add("local[2]"); + command.add("--conf"); + command.add("spark.driver.extraClassPath=" + engineClasspath); + command.add("--conf"); + command.add("spark.executor.extraClassPath=" + engineClasspath); + command.add("--class"); + command.add("org.apache.xtable.spark.XTableSparkSync"); + command.add(bundleJar.getAbsolutePath()); + command.add("--basePath"); + command.add(basePath); + command.add("--dataPath"); + command.add(dataPath); + command.add("--sourceFormat"); + command.add(sourceFormat); + command.add("--targets"); + command.add(targetFormat); + command.add("--tableName"); + command.add(tableName); + if (partitionSpec != null) { + command.add("--partitionSpec"); + command.add(partitionSpec); + } + + ProcessBuilder pb = new ProcessBuilder(command); + pb.environment().put("SPARK_LOCAL_IP", "127.0.0.1"); + pb.redirectErrorStream(true); + Process process = pb.start(); + String output = readOutput(process); Review Comment: readOutput(process) reads stdout until EOF, which only happens when the process exits. It is called before process.waitFor(10, TimeUnit.MINUTES), so if spark-submit hangs the test blocks indefinitely — the 10-minute timeout is never consulted. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
