Skip to content

World's most powerful open data catalog for building a high-performance, geo-distributed and federated metadata lake.

[MINOR] test: Avoid H2 async writer failures in embedded tests (#14981)

Qi Yu0f78b64fa791
diff --git a/integration-test-common/src/test/java/org/apache/gravitino/integration/test/MiniGravitino.java b/integration-test-common/src/test/java/org/apache/gravitino/integration/test/MiniGravitino.java
index 841785548b..b0345c486e 100644
--- a/integration-test-common/src/test/java/org/apache/gravitino/integration/test/MiniGravitino.java
+++ b/integration-test-common/src/test/java/org/apache/gravitino/integration/test/MiniGravitino.java
@@ -57,6 +57,7 @@ import org.apache.gravitino.rest.RESTUtils;
import org.apache.gravitino.server.GravitinoServer;
import org.apache.gravitino.server.ServerConfig;
import org.apache.gravitino.server.web.JettyServerConfig;
+import org.apache.gravitino.storage.relational.database.H2Database;
import org.junit.platform.commons.util.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -277,6 +278,36 @@ public class MiniGravitino {
return customConfigs;
}
+ // Customize the config file
+ @VisibleForTesting
+ void customizeConfigFile(String configTempFileName, String configFileName) throws IOException {
+ Map<String, String> configMap = new HashMap<>();
+ configMap.put(
+ GravitinoServer.WEBSERVER_CONF_PREFIX + JettyServerConfig.WEBSERVER_HTTP_PORT.getKey(),
+ String.valueOf(RESTUtils.findAvailablePort(2000, 3000)));
+
+ configMap.putAll(getIcebergRestServiceConfigs());
+ configMap.putAll(getLanceRestServiceConfigs(configMap));
+ configMap.putAll(context.customConfig);
+
+ Properties configProperties = serverConfig.loadPropertiesFromFile(new File(configTempFileName));
+ configProperties.putAll(configMap);
+ // Match server startup: load JVM properties first, then apply the generated file's overrides.
+ ServerConfig config = new ServerConfig();
+ config.loadFromProperties(configProperties);
+ String jdbcUrl = config.get(Configs.ENTITY_RELATIONAL_JDBC_BACKEND_URL);
+ if (Configs.DEFAULT_RELATIONAL_JDBC_BACKEND_URL.equals(jdbcUrl)) {
+ // H2 2.2.224 can panic when its asynchronous chunk writer checks free-space state while
+ // another thread removes unused chunks (h2database/h2database#4048). Synchronous writes
+ // avoid that pipeline in the default embedded test database. Preserve explicit JDBC URLs
+ // so tests can still select other backends or exercise different H2 settings.
+ configProperties.setProperty(
+ Configs.ENTITY_RELATIONAL_JDBC_BACKEND_URL_KEY,
+ H2Database.getConnectionUrl(config) + ";WRITE_DELAY=0");
+ }
+ ITUtils.overwriteConfigFile(configFileName, configProperties);
+ }
+
private Map<String, String> getLanceRestServiceConfigs(Map<String, String> configMap)
throws IOException {
if (context.ignoreLanceAuxRestService) {
@@ -329,19 +360,4 @@ public class MiniGravitino {
}
}
}
-
- // Customize the config file
- private void customizeConfigFile(String configTempFileName, String configFileName)
- throws IOException {
- Map<String, String> configMap = new HashMap<>();
- configMap.put(
- GravitinoServer.WEBSERVER_CONF_PREFIX + JettyServerConfig.WEBSERVER_HTTP_PORT.getKey(),
- String.valueOf(RESTUtils.findAvailablePort(2000, 3000)));
-
- configMap.putAll(getIcebergRestServiceConfigs());
- configMap.putAll(getLanceRestServiceConfigs(configMap));
- configMap.putAll(context.customConfig);
-
- ITUtils.rewriteConfigFile(configTempFileName, configFileName, configMap);
- }
}
diff --git a/integration-test-common/src/test/java/org/apache/gravitino/integration/test/TestMiniGravitino.java b/integration-test-common/src/test/java/org/apache/gravitino/integration/test/TestMiniGravitino.java
index 40b915c78d..00f71a50a9 100644
--- a/integration-test-common/src/test/java/org/apache/gravitino/integration/test/TestMiniGravitino.java
+++ b/integration-test-common/src/test/java/org/apache/gravitino/integration/test/TestMiniGravitino.java
@@ -22,27 +22,177 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.ResultSet;
+import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
+import java.util.Map;
+import java.util.Properties;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;
+import javax.annotation.Nullable;
+import org.apache.gravitino.Configs;
import org.apache.gravitino.client.RESTClient;
+import org.apache.gravitino.integration.test.util.ITUtils;
+import org.apache.gravitino.server.ServerConfig;
+import org.apache.gravitino.storage.relational.database.H2Database;
import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.function.Executable;
import org.junit.jupiter.api.io.TempDir;
+import org.junit.jupiter.api.parallel.ResourceLock;
+import org.junit.jupiter.api.parallel.Resources;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.NullSource;
+import org.junit.jupiter.params.provider.ValueSource;
+@ResourceLock(Resources.SYSTEM_PROPERTIES)
class TestMiniGravitino {
@TempDir private Path mockConfDir;
+ @ParameterizedTest
+ @NullSource
+ @ValueSource(strings = "jdbc:h2")
+ void testDefaultH2UsesSynchronousWrites(@Nullable String templateUrl) throws Exception {
+ Properties properties = rewriteConfig(templateUrl, Collections.emptyMap());
+ ServerConfig config = new ServerConfig();
+ config.loadFromProperties(properties);
+ String jdbcUrl = H2Database.getConnectionUrl(config);
+ assertTrue(jdbcUrl.startsWith("jdbc:h2:file:" + mockConfDir.resolve("metadata") + ";"));
+ assertTrue(Class.forName("org.h2.mvstore.RandomAccessStore").desiredAssertionStatus());
+
+ try (Connection connection = DriverManager.getConnection(jdbcUrl, "sa", "");
+ Statement statement = connection.createStatement()) {
+ try (ResultSet settings =
+ statement.executeQuery(
+ "SELECT SETTING_VALUE FROM INFORMATION_SCHEMA.SETTINGS WHERE SETTING_NAME = 'WRITE_DELAY'")) {
+ assertTrue(settings.next());
+ do {
+ assertEquals("0", settings.getString(1));
+ } while (settings.next());
+ } finally {
+ statement.execute("SHUTDOWN");
+ }
+ }
+ }
+
+ @ParameterizedTest
+ @ValueSource(
+ strings = {
+ "jdbc:h2:file:/tmp/custom-metadata",
+ "jdbc:h2:file:/tmp/custom-metadata;WRITE_DELAY=100",
+ "jdbc:mysql://localhost/gravitino",
+ "jdbc:postgresql://localhost/gravitino"
+ })
+ void testPreservesExplicitJdbcUrls(String jdbcUrl) throws Exception {
+ Properties properties =
+ rewriteConfig("jdbc:h2", Map.of(Configs.ENTITY_RELATIONAL_JDBC_BACKEND_URL_KEY, jdbcUrl));
+ assertEquals(jdbcUrl, properties.getProperty(Configs.ENTITY_RELATIONAL_JDBC_BACKEND_URL_KEY));
+ }
+
+ @Test
+ void testPreservesJdbcUrlFromTemplate() throws Exception {
+ String jdbcUrl = "jdbc:h2:file:/tmp/template-metadata;WRITE_DELAY=100";
+ Properties properties = rewriteConfig(jdbcUrl, Collections.emptyMap());
+ assertEquals(jdbcUrl, properties.getProperty(Configs.ENTITY_RELATIONAL_JDBC_BACKEND_URL_KEY));
+ }
+
+ @ParameterizedTest
+ @ValueSource(
+ strings = {
+ "jdbc:h2:file:/tmp/jvm-metadata;WRITE_DELAY=100",
+ "jdbc:mysql://localhost/gravitino",
+ "jdbc:postgresql://localhost/gravitino"
+ })
+ void testPreservesJdbcUrlFromJvm(String jdbcUrl) throws Throwable {
+ withSystemProperty(
+ Configs.ENTITY_RELATIONAL_JDBC_BACKEND_URL_KEY,
+ jdbcUrl,
+ () -> {
+ Properties properties = rewriteConfig(null, Collections.emptyMap());
+ ServerConfig config = new ServerConfig();
+ config.loadFromProperties(properties);
+ assertEquals(jdbcUrl, config.get(Configs.ENTITY_RELATIONAL_JDBC_BACKEND_URL));
+ });
+ }
+
+ @ParameterizedTest
+ @ValueSource(strings = {"jvm-metadata", "caf\u00e9", "\u5143\u6570\u636e"})
+ void testUsesStoragePathFromJvm(String directory) throws Throwable {
+ String storagePath = mockConfDir.resolve(directory).toString();
+ withSystemProperty(
+ Configs.ENTITY_RELATIONAL_JDBC_BACKEND_PATH.getKey(),
+ storagePath,
+ () -> {
+ Properties properties = rewriteConfig("jdbc:h2", null, Collections.emptyMap());
+ // Startup may write the configuration twice more when disabling auxiliary services.
+ for (int rewrite = 0; rewrite < 3; rewrite++) {
+ ServerConfig config = new ServerConfig();
+ config.loadFromProperties(properties);
+ String jdbcUrl = H2Database.getConnectionUrl(config);
+ assertTrue(jdbcUrl.startsWith("jdbc:h2:file:" + storagePath + ";"), jdbcUrl);
+ assertTrue(jdbcUrl.contains(";WRITE_DELAY=0"));
+ if (rewrite < 2) {
+ Path configFile = mockConfDir.resolve("gravitino.conf");
+ ITUtils.overwriteConfigFile(configFile.toString(), properties);
+ properties = config.loadPropertiesFromFile(configFile.toFile());
+ }
+ }
+ });
+ }
+
+ @Test
+ void testTemplateOverridesJvmJdbcUrl() throws Throwable {
+ withSystemProperty(
+ Configs.ENTITY_RELATIONAL_JDBC_BACKEND_URL_KEY,
+ "jdbc:postgresql://localhost/gravitino",
+ () -> {
+ Properties properties = rewriteConfig("jdbc:h2", Collections.emptyMap());
+ ServerConfig config = new ServerConfig();
+ config.loadFromProperties(properties);
+ String jdbcUrl = H2Database.getConnectionUrl(config);
+ assertTrue(jdbcUrl.startsWith("jdbc:h2:file:" + mockConfDir.resolve("metadata") + ";"));
+ assertTrue(jdbcUrl.contains(";WRITE_DELAY=0"));
+ });
+ }
+
+ @ParameterizedTest
+ @ValueSource(booleans = {false, true})
+ void testFileStoragePathOverridesJvm(boolean useOverride) throws Throwable {
+ String templatePath = mockConfDir.resolve("caf\u00e9").toString();
+ String storagePath =
+ useOverride ? mockConfDir.resolve("\u5143\u6570\u636e").toString() : templatePath;
+ withSystemProperty(
+ Configs.ENTITY_RELATIONAL_JDBC_BACKEND_PATH.getKey(),
+ mockConfDir.resolve("jvm-metadata").toString(),
+ () -> {
+ Map<String, String> overrides =
+ useOverride
+ ? Map.of(Configs.ENTITY_RELATIONAL_JDBC_BACKEND_PATH.getKey(), storagePath)
+ : Collections.emptyMap();
+ Properties properties = rewriteConfig("jdbc:h2", templatePath, overrides);
+ ServerConfig config = new ServerConfig();
+ config.loadFromProperties(properties);
+ String jdbcUrl = H2Database.getConnectionUrl(config);
+ assertTrue(jdbcUrl.startsWith("jdbc:h2:file:" + storagePath + ";"), jdbcUrl);
+ assertTrue(jdbcUrl.contains(";WRITE_DELAY=0"));
+ });
+ }
+
@Test
void testContextLaunchesTheOpenSourceServerByDefault() {
// Existing callers pass no launcher and must keep getting the open source server.
@@ -97,6 +247,53 @@ class TestMiniGravitino {
assertFalse(Files.exists(mockConfDir));
}
+ private Properties rewriteConfig(@Nullable String templateUrl, Map<String, String> overrides)
+ throws IOException {
+ return rewriteConfig(templateUrl, mockConfDir.resolve("metadata").toString(), overrides);
+ }
+
+ private Properties rewriteConfig(
+ @Nullable String templateUrl, @Nullable String storagePath, Map<String, String> overrides)
+ throws IOException {
+ Properties template = new Properties();
+ if (storagePath != null) {
+ template.setProperty(Configs.ENTITY_RELATIONAL_JDBC_BACKEND_PATH.getKey(), storagePath);
+ }
+ if (templateUrl != null) {
+ template.setProperty(Configs.ENTITY_RELATIONAL_JDBC_BACKEND_URL_KEY, templateUrl);
+ }
+ Path templateFile = mockConfDir.resolve("gravitino.conf.template");
+ try (OutputStream output = Files.newOutputStream(templateFile)) {
+ template.store(output, null);
+ }
+ Path configFile = mockConfDir.resolve("gravitino.conf");
+ MiniGravitino miniGravitino =
+ new MiniGravitino(
+ new MiniGravitinoContext(overrides, true, true),
+ mockConfDir.toFile(),
+ mock(ExecutorService.class),
+ null);
+ miniGravitino.customizeConfigFile(templateFile.toString(), configFile.toString());
+ Properties properties = new Properties();
+ try (InputStream input = Files.newInputStream(configFile)) {
+ properties.load(input);
+ }
+ return properties;
+ }
+
+ private void withSystemProperty(String key, String value, Executable test) throws Throwable {
+ String previous = System.setProperty(key, value);
+ try {
+ test.execute();
+ } finally {
+ if (previous == null) {
+ System.clearProperty(key);
+ } else {
+ System.setProperty(key, previous);
+ }
+ }
+ }
+
private MiniGravitino createMiniGravitino(ExecutorService executor, RESTClient restClient)
throws IOException {
Files.writeString(mockConfDir.resolve("gravitino.conf"), "test");
diff --git a/integration-test-common/src/test/java/org/apache/gravitino/integration/test/util/ITUtils.java b/integration-test-common/src/test/java/org/apache/gravitino/integration/test/util/ITUtils.java
index 67829c3bbb..3c1a49ec4f 100644
--- a/integration-test-common/src/test/java/org/apache/gravitino/integration/test/util/ITUtils.java
+++ b/integration-test-common/src/test/java/org/apache/gravitino/integration/test/util/ITUtils.java
@@ -26,7 +26,6 @@ import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
-import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
@@ -70,30 +69,43 @@ public class ITUtils {
return path.split(File.separator);
}
+ /**
+ * Loads a properties template, applies overrides, and writes the effective configuration.
+ *
+ * @param configTempFileName The template properties file.
+ * @param configFileName The destination properties file.
+ * @param configMap The configuration overrides.
+ * @throws IOException If reading or writing the configuration fails.
+ */
public static void rewriteConfigFile(
String configTempFileName, String configFileName, Map<String, String> configMap)
throws IOException {
Properties props = new Properties();
- try (InputStream inputStream = Files.newInputStream(Paths.get(configTempFileName));
- OutputStream outputStream = Files.newOutputStream(Paths.get(configFileName))) {
+ try (InputStream inputStream = Files.newInputStream(Paths.get(configTempFileName))) {
props.load(inputStream);
- props.putAll(configMap);
- for (String key : props.stringPropertyNames()) {
- String value = props.getProperty(key);
- // Use customized write functions to avoid escaping `:` into `\:`.
- outputStream.write((key + " = " + value + "\n").getBytes(StandardCharsets.UTF_8));
- }
}
+ props.putAll(configMap);
+ overwriteConfigFile(configFileName, props);
}
+ /**
+ * Writes effective string properties, including inherited defaults, in the format read by {@link
+ * Properties#load(InputStream)}.
+ *
+ * @param configFileName The destination properties file.
+ * @param props The properties to write.
+ * @throws IOException If writing the configuration fails.
+ */
public static void overwriteConfigFile(String configFileName, Properties props)
throws IOException {
+ Properties propertiesToWrite = new Properties();
+ for (String key : props.stringPropertyNames()) {
+ propertiesToWrite.setProperty(key, props.getProperty(key));
+ }
try (OutputStream outputStream = Files.newOutputStream(Paths.get(configFileName))) {
- for (String key : props.stringPropertyNames()) {
- String value = props.getProperty(key);
- // Use customized write functions to avoid escaping `:` into `\:`.
- outputStream.write((key + " = " + value + "\n").getBytes(StandardCharsets.UTF_8));
- }
+ // Pair the writer with Properties.load(InputStream) used by server startup. Escaping also
+ // preserves Unicode paths and literal backslashes through subsequent configuration rewrites.
+ propertiesToWrite.store(outputStream, null);
}
}
diff --git a/integration-test-common/src/test/java/org/apache/gravitino/integration/test/util/TestITUtils.java b/integration-test-common/src/test/java/org/apache/gravitino/integration/test/util/TestITUtils.java
index 6744a9109b..aac0330a78 100644
--- a/integration-test-common/src/test/java/org/apache/gravitino/integration/test/util/TestITUtils.java
+++ b/integration-test-common/src/test/java/org/apache/gravitino/integration/test/util/TestITUtils.java
@@ -19,12 +19,20 @@
package org.apache.gravitino.integration.test.util;
import java.io.IOException;
+import java.io.OutputStream;
import java.net.ServerSocket;
+import java.nio.file.Files;
+import java.nio.file.Path;
import java.util.Map;
+import java.util.Properties;
+import org.apache.gravitino.server.ServerConfig;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
public class TestITUtils {
+ @TempDir private Path tempDir;
+
@Test
public void testIsCiEnvironmentByCI() {
Assertions.assertTrue(ITUtils.isCiEnvironment(Map.of("CI", "true")));
@@ -46,6 +54,55 @@ public class TestITUtils {
Assertions.assertFalse(ITUtils.isCiEnvironment(Map.of("CI", "false")));
}
+ @Test
+ void testOverwriteConfigFileRoundTripsProperties() throws IOException {
+ Properties expected = configProperties();
+ Path configFile = tempDir.resolve("gravitino.conf");
+ ServerConfig config = new ServerConfig(false);
+ Properties properties = expected;
+ for (int rewrite = 0; rewrite < 3; rewrite++) {
+ ITUtils.overwriteConfigFile(configFile.toString(), properties);
+ properties = config.loadPropertiesFromFile(configFile.toFile());
+ Assertions.assertEquals(expected, properties);
+ }
+ }
+
+ @Test
+ void testRewriteConfigFileRoundTripsProperties() throws IOException {
+ Properties template = configProperties();
+ Path templateFile = tempDir.resolve("gravitino.conf.template");
+ try (OutputStream output = Files.newOutputStream(templateFile)) {
+ template.store(output, null);
+ }
+ Map<String, String> overrides =
+ Map.of("jdbc.url", "jdbc:h2:file:/tmp/\u5143\u6570\u636e", "added", "caf\u00e9");
+ Properties expected = configProperties();
+ expected.putAll(overrides);
+ Path configFile = tempDir.resolve("gravitino.conf");
+
+ ITUtils.rewriteConfigFile(templateFile.toString(), configFile.toString(), overrides);
+
+ Properties properties = new ServerConfig(false).loadPropertiesFromFile(configFile.toFile());
+ Assertions.assertEquals(expected, properties);
+ }
+
+ @Test
+ void testOverwriteConfigFilePreservesInheritedDefaults() throws IOException {
+ Properties defaults = new Properties();
+ defaults.setProperty("inherited", "caf\u00e9");
+ defaults.setProperty("overridden", "default");
+ Properties properties = new Properties(defaults);
+ properties.setProperty("overridden", "explicit");
+ Path configFile = tempDir.resolve("gravitino.conf");
+
+ ITUtils.overwriteConfigFile(configFile.toString(), properties);
+
+ Properties loaded = new ServerConfig(false).loadPropertiesFromFile(configFile.toFile());
+ Assertions.assertEquals("caf\u00e9", loaded.getProperty("inherited"));
+ Assertions.assertEquals("explicit", loaded.getProperty("overridden"));
+ Assertions.assertEquals(2, loaded.size());
+ }
+
@Test
void testCheckServerPortIsFreeAcceptsAnUnusedPort() throws IOException {
int port;
@@ -73,4 +130,13 @@ public class TestITUtils {
"the message has to name the port so the cause is actionable: " + e.getMessage());
}
}
+
+ private Properties configProperties() {
+ Properties properties = new Properties();
+ properties.setProperty("jdbc.url", "jdbc:h2:file:/tmp/caf\u00e9;WRITE_DELAY=0");
+ properties.setProperty("windows.path", "C:\\caf\u00e9\\metadata\\u0041");
+ properties.setProperty(" key:=#!", " leading\tline1\nline2\rline3\\u0041");
+ properties.setProperty("\u5143\u6570\u636e", "\uD83D\uDE80");
+ return properties;
+ }
}