forked from apache/gravitino
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[apache#3856] feat(core): Support embedded JDBC storage backend (apac…
…he#3922) ### What changes were proposed in this pull request? Add support for an embedded JDBC storage backend. ### Why are the changes needed? We plan to replace RocksDB KV backend with an embedded JDBC backend as the default storage backend. Fix: apache#3856 ### Does this PR introduce _any_ user-facing change? N/A ### How was this patch tested? Existing UT
- Loading branch information
Showing
23 changed files
with
496 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
core/src/main/java/com/datastrato/gravitino/storage/relational/JDBCDatabase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/* | ||
* Copyright 2024 Datastrato Pvt Ltd. | ||
* This software is licensed under the Apache License version 2. | ||
*/ | ||
package com.datastrato.gravitino.storage.relational; | ||
|
||
import com.datastrato.gravitino.Config; | ||
import java.io.Closeable; | ||
|
||
public interface JDBCDatabase extends Closeable { | ||
|
||
/** | ||
* Initializes the Relational database environment with the provided configuration. | ||
* | ||
* @param config The configuration for the database backend. | ||
* @throws RuntimeException | ||
*/ | ||
void initialize(Config config); | ||
} |
102 changes: 102 additions & 0 deletions
102
core/src/main/java/com/datastrato/gravitino/storage/relational/database/H2Database.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/* | ||
* Copyright 2024 Datastrato Pvt Ltd. | ||
* This software is licensed under the Apache License version 2. | ||
*/ | ||
package com.datastrato.gravitino.storage.relational.database; | ||
|
||
import com.datastrato.gravitino.Config; | ||
import com.datastrato.gravitino.Configs; | ||
import com.datastrato.gravitino.storage.relational.JDBCDatabase; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.charset.StandardCharsets; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.sql.Connection; | ||
import java.sql.DriverManager; | ||
import java.sql.Statement; | ||
import org.apache.commons.io.FileUtils; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class H2Database implements JDBCDatabase { | ||
private static final Logger LOG = LoggerFactory.getLogger(H2Database.class); | ||
private String h2ConnectionUri; | ||
private String username; | ||
private String password; | ||
|
||
@Override | ||
public void initialize(Config config) { | ||
this.h2ConnectionUri = startH2Database(config); | ||
} | ||
|
||
public String startH2Database(Config config) { | ||
String gravitinoHome = System.getenv("GRAVITINO_HOME"); | ||
String storagePath = getStoragePath(config); | ||
String originalJDBCUrl = config.get(Configs.ENTITY_RELATIONAL_JDBC_BACKEND_URL); | ||
this.username = config.get(Configs.ENTITY_RELATIONAL_JDBC_BACKEND_USER); | ||
this.password = config.get(Configs.ENTITY_RELATIONAL_JDBC_BACKEND_PASSWORD); | ||
|
||
String connectionUrl = constructH2URI(originalJDBCUrl, storagePath); | ||
|
||
try (Connection connection = DriverManager.getConnection(connectionUrl, username, password); | ||
Statement statement = connection.createStatement()) { | ||
String sqlContent = | ||
FileUtils.readFileToString( | ||
new File(gravitinoHome + "/scripts/h2/schema-h2.sql"), StandardCharsets.UTF_8); | ||
|
||
statement.execute(sqlContent); | ||
} catch (Exception e) { | ||
LOG.error("Failed to create table for H2 database.", e); | ||
throw new RuntimeException("Failed to create table for H2 database.", e); | ||
} | ||
|
||
config.set(Configs.ENTITY_RELATIONAL_JDBC_BACKEND_URL, connectionUrl); | ||
|
||
return connectionUrl; | ||
} | ||
|
||
private static String constructH2URI(String originURI, String storagePath) { | ||
if (!originURI.contains(":file:")) { | ||
originURI = "jdbc:h2:file:" + storagePath; | ||
} | ||
|
||
if (!originURI.contains("DB_CLOSE_DELAY")) { | ||
originURI = originURI + ";DB_CLOSE_DELAY=-1"; | ||
} | ||
|
||
if (!originURI.contains("MODE")) { | ||
originURI = originURI + ";MODE=MYSQL"; | ||
} | ||
|
||
return originURI; | ||
} | ||
|
||
private static String getStoragePath(Config config) { | ||
String dbPath = config.get(Configs.ENTITY_RELATIONAL_JDBC_BACKEND_PATH); | ||
if (StringUtils.isBlank(dbPath)) { | ||
return Configs.DEFAULT_RELATIONAL_JDBC_BACKEND_PATH; | ||
} | ||
|
||
Path path = Paths.get(dbPath); | ||
// Relative Path | ||
if (!path.isAbsolute()) { | ||
path = Paths.get(System.getenv("GRAVITINO_HOME"), dbPath); | ||
return path.toString(); | ||
} | ||
|
||
return dbPath; | ||
} | ||
|
||
@Override | ||
public void close() throws IOException { | ||
try (Connection connection = DriverManager.getConnection(h2ConnectionUri, username, password); | ||
Statement statement = connection.createStatement()) { | ||
statement.execute("SHUTDOWN"); | ||
} catch (Exception e) { | ||
LOG.error("Failed to shutdown H2 database.", e); | ||
throw new RuntimeException("Failed to shutdown H2 database.", e); | ||
} | ||
} | ||
} |
Oops, something went wrong.