Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 12 additions & 11 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ lazy val scala213 = "2.13.16"

lazy val deltaVersion = "4.0.0"
lazy val sparkVersion = "4.0.0"
lazy val hadoopVersion = "3.4.0"

// Library versions
lazy val jacksonVersion = "2.17.0"
Expand Down Expand Up @@ -503,14 +504,14 @@ lazy val cli = (project in file("examples") / "cli")
"io.delta" % "delta-kernel-api" % deltaVersion,
"io.delta" % "delta-kernel-defaults" % deltaVersion,
"io.delta" % "delta-storage" % deltaVersion,
"org.apache.hadoop" % "hadoop-client-api" % "3.4.0",
"org.apache.hadoop" % "hadoop-client-runtime" % "3.4.0",
"org.apache.hadoop" % "hadoop-client-api" % hadoopVersion,
"org.apache.hadoop" % "hadoop-client-runtime" % hadoopVersion,
"de.vandermeer" % "asciitable" % "0.3.2",
// for s3 access
"org.fusesource.jansi" % "jansi" % "2.4.1",
"com.amazonaws" % "aws-java-sdk-core" % "1.12.728",
"org.apache.hadoop" % "hadoop-aws" % "3.4.0",
"org.apache.hadoop" % "hadoop-azure" % "3.4.0",
"org.apache.hadoop" % "hadoop-aws" % hadoopVersion,
"org.apache.hadoop" % "hadoop-azure" % hadoopVersion,
"com.google.guava" % "guava" % "31.0.1-jre",
// Test dependencies
"org.junit.jupiter" % "junit-jupiter" % "5.10.3" % Test,
Expand Down Expand Up @@ -572,7 +573,7 @@ lazy val spark = (project in file("connectors/spark"))
"org.antlr" % "antlr4-runtime" % "4.13.1",
"org.antlr" % "antlr4" % "4.13.1",
"com.google.cloud.bigdataoss" % "util-hadoop" % "3.0.2" % Provided,
"org.apache.hadoop" % "hadoop-azure" % "3.4.0" % Provided,
"org.apache.hadoop" % "hadoop-azure" % hadoopVersion % Provided,
"software.amazon.awssdk" % "auth" % "2.25.37" % Provided,
),
libraryDependencies ++= Seq(
Expand All @@ -583,8 +584,8 @@ lazy val spark = (project in file("connectors/spark"))
"org.mockito" % "mockito-inline" % "5.2.0" % Test,
"org.mockito" % "mockito-junit-jupiter" % "5.12.0" % Test,
"net.aichler" % "jupiter-interface" % JupiterKeys.jupiterVersion.value % Test,
"org.apache.hadoop" % "hadoop-client-runtime" % "3.4.0",
"org.apache.hadoop" % "hadoop-aws" % "3.4.0" % Test,
"org.apache.hadoop" % "hadoop-client-runtime" % hadoopVersion,
"org.apache.hadoop" % "hadoop-aws" % hadoopVersion % Test,
"io.delta" %% "delta-spark" % deltaVersion % Test,
),
dependencyOverrides ++= Seq(
Expand Down Expand Up @@ -622,6 +623,7 @@ lazy val spark = (project in file("connectors/spark"))

lazy val integrationTests = (project in file("integration-tests"))
.enablePlugins(CheckstylePlugin)
.dependsOn(spark)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it will be better to use the cross module depending here than configured the static io.unitycatalog:unitycatalog-spark here. https://github.com/unitycatalog/unitycatalog/pull/1119/files#diff-5634c415cd8c8504fdb973a3ed092300b43c4b8fc1e184f7249eb29a55511f91L645

Since for the later one, we need to manually publish the unitycatalog-spark into local maven repo, which will makes the integrationTests more hard to run.

With the former one, if we make any changes in the unitycatalog-spark connector, then we can simply use the command ./build/sbt integrationTests/test to run the integrationTests without publishing.

.settings(
name := s"$artifactNamePrefix-integration-tests",
commonSettings,
Expand All @@ -639,10 +641,9 @@ lazy val integrationTests = (project in file("integration-tests"))
"org.projectlombok" % "lombok" % "1.18.32" % Provided,
"org.apache.spark" %% "spark-sql" % sparkVersion % Test,
"io.delta" %% "delta-spark" % deltaVersion % Test,
"org.apache.hadoop" % "hadoop-aws" % "3.3.6" % Test,
"org.apache.hadoop" % "hadoop-azure" % "3.3.6" % Test,
"org.apache.hadoop" % "hadoop-aws" % hadoopVersion % Test,

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before this PR, we use the hadoop 3.3.6 to run the integration tests by default. while actually, I encountered a serious issue here #1092 (comment).

I think I will need a separate PR to address the cross-hadoop version issue.

"org.apache.hadoop" % "hadoop-azure" % hadoopVersion % Test,
"com.google.cloud.bigdataoss" % "gcs-connector" % "3.0.2" % Test classifier "shaded",
"io.unitycatalog" %% "unitycatalog-spark" % "0.2.0" % Test,
),
dependencyOverrides ++= Seq(
"com.fasterxml.jackson.core" % "jackson-databind" % "2.15.0",
Expand All @@ -652,7 +653,7 @@ lazy val integrationTests = (project in file("integration-tests"))
"com.fasterxml.jackson.dataformat" % "jackson-dataformat-xml" % "2.15.0",
"org.antlr" % "antlr4-runtime" % "4.13.1",
"org.antlr" % "antlr4" % "4.13.1",
"org.apache.hadoop" % "hadoop-client-api" % "3.3.6",
"org.apache.hadoop" % "hadoop-client-api" % hadoopVersion,
),
Test / javaOptions += s"-Duser.dir=${((ThisBuild / baseDirectory).value / "integration-tests").getAbsolutePath}",
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,11 @@
import org.junit.jupiter.api.BeforeAll;

public class BaseSparkTest {
// todo: parameterize such that catalogs can be specified per cloud provider if desired
private static final String ServerUrl =
System.getenv().getOrDefault("CATALOG_URI", "http://localhost:8080");
private static final String AuthToken =
System.getenv().getOrDefault("CATALOG_AUTH_TOKEN", "");
private static final String CatalogName =
System.getenv().getOrDefault("CATALOG_NAME", "unity");
protected static SparkSession spark;

@BeforeAll
public static void setup() {
spark = createSparkSessionWithCatalogs(CatalogName);
spark = createSparkSessionWithCatalogs(TestUtils.CATALOG_NAME);
}

protected static SparkSession createSparkSessionWithCatalogs(String... catalogs) {
Expand All @@ -44,8 +37,8 @@ protected static SparkSession createSparkSessionWithCatalogs(String... catalogs)
builder =
builder
.config(catalogConf, "io.unitycatalog.spark.UCSingleCatalog")
.config(catalogConf + ".uri", ServerUrl)
.config(catalogConf + ".token", AuthToken);
.config(catalogConf + ".uri", TestUtils.SERVER_URL)
.config(catalogConf + ".token", TestUtils.AUTH_TOKEN);
}
if (catalogs.length > 0) {
builder.config("spark.sql.defaultCatalog", catalogs[0]);
Expand All @@ -57,18 +50,18 @@ protected static String getBaseLocation(LocationType locationType) throws IOExce
return switch (locationType) {
// todo: add hook to clean up temp directory
case FILE -> Files.createTempDirectory("uc-test-table").toFile().getAbsolutePath();
case S3 -> System.getenv("S3_BASE_LOCATION");
case GS -> System.getenv("GS_BASE_LOCATION");
case ABFSS -> System.getenv("ABFSS_BASE_LOCATION");
case S3 -> TestUtils.S3_BASE_LOCATION;
case GS -> TestUtils.GS_BASE_LOCATION;
case ABFSS -> TestUtils.ABFSS_BASE_LOCATION;
};
}

@Getter
public enum LocationType {
FILE("file://"),
S3(System.getenv("S3_BASE_LOCATION")),
GS(System.getenv("GS_BASE_LOCATION")),
ABFSS(System.getenv("ABFSS_BASE_LOCATION"));
S3(TestUtils.S3_BASE_LOCATION),
GS(TestUtils.GS_BASE_LOCATION),
ABFSS(TestUtils.ABFSS_BASE_LOCATION);

private final String baseLocation;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
package io.unitycatalog.integrationtests;

import static io.unitycatalog.integrationtests.TestUtils.AUTH_TOKEN;
import static io.unitycatalog.integrationtests.TestUtils.CATALOG_NAME;
import static io.unitycatalog.integrationtests.TestUtils.S3_BASE_LOCATION;
import static io.unitycatalog.integrationtests.TestUtils.SERVER_URL;
import static io.unitycatalog.integrationtests.TestUtils.envAsBoolean;
import static io.unitycatalog.integrationtests.TestUtils.envAsLong;
import static io.unitycatalog.integrationtests.TestUtils.randomName;
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;

import io.unitycatalog.spark.UCSingleCatalog;
import java.util.List;
import java.util.UUID;
import org.apache.spark.api.java.function.MapPartitionsFunction;
import org.apache.spark.sql.Encoders;
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.Test;
import org.sparkproject.guava.collect.Iterators;

/**
* Typically, cloud vendor credentials (used to access cloud storage) issued by the UC server expire
* after one hour. This integration test launches a long-running read-write job whose duration
* (controlled by the 'CREDENTIAL_RENEWAL_TEST_DURATION_SECONDS' environment variable) exceeds the
* credential’s expiration time. If the job completes successfully, it verifies that the credential
* renewal mechanism works as expected, seamlessly refreshing credentials without interrupting the
* ongoing job.
*
* <p>
*
* <p><b>To run this tests: </b>
*
* <pre>
* export CATALOG_URI=...
* export CATALOG_AUTH_TOKEN=...
* export CATALOG_NAME=...
* export S3_BASE_LOCATION=...
* SBT_OPTS="-Xmx8G -XX:+UseG1GC" \
* ./build/sbt \
* "integrationTests/testOnly io.unitycatalog.integrationtests.SparkCredentialRenewalTest"
* </pre>
*/
public class SparkCredentialRenewalTest {

@openinx openinx Oct 31, 2025 •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the credential renewal long-running job. We can run this command to do the integrtation test.

env \
  CATALOG_URI="$UC_URI" \
  CATALOG_AUTH_TOKEN="$UC_TOKEN" \
  CATALOG_NAME="main" \
  S3_BASE_LOCATION="$UC_S3_LOCATION" \
  SBT_OPTS="-Xmx8G -XX:+UseG1GC" \
  "$PWD/build/sbt" "integrationTests/testOnly io.unitycatalog.integrationtests.SparkCredentialRenewalTest"

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And if we set the CREDENTIAL_RENEWAL_TEST_RENEWAL_ENABLED=false in this test, then the job will be failure.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thsi should be in the class docs as instructions on how to run this manually. not in comments.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it's good to add in class docs. Done !

private static final String PREFIX = "CREDENTIAL_RENEWAL_TEST_";

// Define the CREDENTIAL_RENEWAL_TEST_RENEWAL_ENABLED environment variable.
private static final boolean RENEW_CRED_ENABLED = envAsBoolean(PREFIX + "RENEWAL_ENABLED", true);

// Define the CREDENTIAL_RENEWAL_TEST_DURATION_SECONDS environment variable, which control how
// long will the test run. 90 minutes by default.
private static final long DURATION_SECONDS = envAsLong(PREFIX + "DURATION_SECONDS", 5600L);

// Define the schema name and table name.
private static final String SCHEMA_NAME = randomName();
private static final String SRC_TABLE =
String.format("%s.%s.src_%s", CATALOG_NAME, SCHEMA_NAME, randomName());
private static final String DST_TABLE =
String.format("%s.%s.dst_%s", CATALOG_NAME, SCHEMA_NAME, randomName());

private static SparkSession spark;

@BeforeAll
public static void beforeAll() {
String testCatalogKey = String.format("spark.sql.catalog.%s", CATALOG_NAME);
spark =
SparkSession.builder()
.appName("test-credential-renewal-in-long-running-job")
.master("local[1]") // Make it single-threaded explicitly.
.config("spark.hadoop.fs.s3.impl", "org.apache.hadoop.fs.s3a.S3AFileSystem")
.config("spark.sql.extensions", "io.delta.sql.DeltaSparkSessionExtension")
.config(
"spark.sql.catalog.spark_catalog",
"org.apache.spark.sql.delta.catalog.DeltaCatalog")
.config(testCatalogKey, UCSingleCatalog.class.getName())
.config(testCatalogKey + ".uri", SERVER_URL)
.config(testCatalogKey + ".token", AUTH_TOKEN)
.config(testCatalogKey + ".warehouse", CATALOG_NAME)
.config(testCatalogKey + ".renewCredential.enabled", String.valueOf(RENEW_CRED_ENABLED))
.getOrCreate();

sql("CREATE SCHEMA %s.%s", CATALOG_NAME, SCHEMA_NAME);
sql(
"CREATE TABLE %s (id INT) USING delta LOCATION '%s/%s' PARTITIONED BY (partition INT)",
SRC_TABLE, S3_BASE_LOCATION, UUID.randomUUID());
sql(
"CREATE TABLE %s (id INT) USING delta LOCATION '%s/%s'",
DST_TABLE, S3_BASE_LOCATION, UUID.randomUUID());
}

@AfterAll
public static void afterAll() {
sql("DROP TABLE IF EXISTS %s", SRC_TABLE);
sql("DROP TABLE IF EXISTS %s", DST_TABLE);
sql("DROP SCHEMA IF EXISTS %s.%s", CATALOG_NAME, SCHEMA_NAME);
spark.stop();
}

@Test
public void testLongRunningJob() {
// Every 100 seconds produce a row, because we don't want to produce massive small files.
long rowCount = DURATION_SECONDS / 100 + (DURATION_SECONDS % 100 == 0 ? 0 : 1);

// Generate data for the delta source table.
sql("INSERT INTO %s SELECT id, id FROM range(0, %s)", SRC_TABLE, rowCount);

// Read from the Delta source table, mapping each partition to a separate task, and finally
// write back to the destination table. With parallelism set to 1, tasks for each partition
// execute sequentially. Every partition task will sleep 100 seconds, and the accumulated
// elapsed time will finally trigger the renewal of credentials.

// The spark job should be success because we've enabled the credential renewal.
spark
.read()
.format("delta")
.table(SRC_TABLE)
.mapPartitions(
(MapPartitionsFunction<Row, Integer>)
input -> {
Thread.sleep(100 * 1000L);
return Iterators.transform(input, row -> row.getInt(0));
},
Encoders.INT())
.withColumnRenamed("value", "id")
.write()
.format("delta")
.mode("append")
.saveAsTable(DST_TABLE);

List<Row> results = sql("SELECT COUNT(*) FROM %s", DST_TABLE);
assertThat(results.size()).isEqualTo(1);
assertThat(results.get(0).getLong(0)).isEqualTo(rowCount);
}

private static List<Row> sql(String statement, Object... args) {
return spark.sql(String.format(statement, args)).collectAsList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import java.util.List;
import java.util.UUID;
import lombok.SneakyThrows;
import org.apache.commons.lang3.RandomStringUtils;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
Expand All @@ -19,7 +18,7 @@
public class SparkDeltaTableCRUDTest extends BaseSparkTest {
private static final String BASE_TABLE_NAME = "numbers";
Comment thread
openinx marked this conversation as resolved.
Resolved
private final String RUN_ID = UUID.randomUUID().toString();
private static final String SCHEMA = RandomStringUtils.randomAlphabetic(8);
private static final String SCHEMA = TestUtils.randomName();

static String getTableName(LocationType locationType) {
return format("%s_%s", BASE_TABLE_NAME, locationType.name());
Expand Down Expand Up @@ -129,7 +128,6 @@ public void update(LocationType locationType) {
String table = getTableName(locationType);
spark.sql(format(
"UPDATE %s SET as_double = as_int * 1.5 WHERE as_int = 42", table));

assertThat(getData(table))
.as("Data after UPDATE")
.isEqualTo(List.of(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import java.util.List;
import java.util.UUID;
import lombok.SneakyThrows;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.spark.SparkUnsupportedOperationException;
import org.apache.spark.sql.AnalysisException;
import org.junit.jupiter.api.AfterAll;
Expand All @@ -22,7 +21,7 @@
public class SparkParquetTableCRUDTest extends BaseSparkTest {
private static final String BASE_TABLE_NAME = "numbers";
Comment thread
openinx marked this conversation as resolved.
Resolved
private final String RUN_ID = UUID.randomUUID().toString();
private static final String SCHEMA = RandomStringUtils.randomAlphabetic(8);
private static final String SCHEMA = TestUtils.randomName();

static String getTableName(LocationType locationType) {
return format("%s_%s", BASE_TABLE_NAME, locationType.name());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package io.unitycatalog.integrationtests;

import org.apache.commons.lang3.RandomStringUtils;

public class TestUtils {
public static final String SERVER_URL = envAsString("CATALOG_URI", "http://localhost:8080");
public static final String AUTH_TOKEN = envAsString("CATALOG_AUTH_TOKEN", "");
public static final String CATALOG_NAME = envAsString("CATALOG_NAME", "unity");
public static final String S3_BASE_LOCATION = envAsString("S3_BASE_LOCATION", "s3://bucket/key");
public static final String GS_BASE_LOCATION = envAsString("GS_BASE_LOCATION", "gs://bucket/key");
public static final String ABFSS_BASE_LOCATION =
envAsString("ABFSS_BASE_LOCATION", "abfss://bucket/key");

public static String envAsString(String key, String defaultValue) {
return System.getenv().getOrDefault(key, defaultValue);
}

public static long envAsLong(String key, long defaultValue) {
String value = System.getenv().get(key);
return value == null ? defaultValue : Long.parseLong(value);
}

public static boolean envAsBoolean(String key, boolean defaultValue) {
String value = System.getenv().get(key);
return value == null ? defaultValue : Boolean.parseBoolean(value);
}

public static String randomName() {
return RandomStringUtils.secure().nextAlphabetic(8);
}
}