Skip to content

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

[#2079] feat(relation-entity-store): Add the relation entity store interface (#1930)

xloyad49c809bea23
diff --git a/core/src/main/java/com/datastrato/gravitino/Configs.java b/core/src/main/java/com/datastrato/gravitino/Configs.java
index 3b3d6246f6..3903870ca1 100644
--- a/core/src/main/java/com/datastrato/gravitino/Configs.java
+++ b/core/src/main/java/com/datastrato/gravitino/Configs.java
@@ -5,17 +5,23 @@
package com.datastrato.gravitino;
import com.datastrato.gravitino.config.ConfigBuilder;
+import com.datastrato.gravitino.config.ConfigConstants;
import com.datastrato.gravitino.config.ConfigEntry;
import java.io.File;
+import org.apache.commons.lang3.StringUtils;
public interface Configs {
String DEFAULT_ENTITY_STORE = "kv";
+ String RELATIONAL_ENTITY_STORE = "relational";
String ENTITY_STORE_KEY = "gravitino.entity.store";
String DEFAULT_ENTITY_KV_STORE = "RocksDBKvBackend";
String ENTITY_KV_STORE_KEY = "gravitino.entity.store.kv";
+ String DEFAULT_ENTITY_RELATIONAL_STORE = "MySQLBackend";
+ String ENTITY_RELATIONAL_STORE_KEY = "gravitino.entity.store.relational";
+
String ENTITY_KV_ROCKSDB_BACKEND_PATH_KEY = "gravitino.entity.store.kv.rocksdbPath";
Long DEFAULT_KV_DELETE_AFTER_TIME = 604800000L; // 7 days
@@ -45,6 +51,14 @@ public interface Configs {
.stringConf()
.createWithDefault(DEFAULT_ENTITY_KV_STORE);
+ ConfigEntry<String> ENTITY_RELATIONAL_STORE =
+ new ConfigBuilder(ENTITY_RELATIONAL_STORE_KEY)
+ .doc("Detailed implementation of relational storage")
+ .version("0.5.0")
+ .stringConf()
+ .checkValue(StringUtils::isNotBlank, ConfigConstants.NOT_BLANK_ERROR_MSG)
+ .createWithDefault(DEFAULT_ENTITY_RELATIONAL_STORE);
+
ConfigEntry<String> ENTRY_KV_ROCKSDB_BACKEND_PATH =
new ConfigBuilder(ENTITY_KV_ROCKSDB_BACKEND_PATH_KEY)
.doc("Directory path of `RocksDBKvBackend`")
diff --git a/core/src/main/java/com/datastrato/gravitino/EntityStoreFactory.java b/core/src/main/java/com/datastrato/gravitino/EntityStoreFactory.java
index 8f1a036782..dfc083e7e6 100644
--- a/core/src/main/java/com/datastrato/gravitino/EntityStoreFactory.java
+++ b/core/src/main/java/com/datastrato/gravitino/EntityStoreFactory.java
@@ -5,6 +5,7 @@
package com.datastrato.gravitino;
import com.datastrato.gravitino.storage.kv.KvEntityStore;
+import com.datastrato.gravitino.storage.relational.RelationalEntityStore;
import com.google.common.collect.ImmutableMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -20,7 +21,11 @@ public class EntityStoreFactory {
// Register EntityStore's short name to its full qualified class name in the map. So that user
// doesn't need to specify the full qualified class name when creating an EntityStore.
public static final ImmutableMap<String, String> ENTITY_STORES =
- ImmutableMap.of("kv", KvEntityStore.class.getCanonicalName());
+ ImmutableMap.of(
+ Configs.DEFAULT_ENTITY_STORE,
+ KvEntityStore.class.getCanonicalName(),
+ Configs.RELATIONAL_ENTITY_STORE,
+ RelationalEntityStore.class.getCanonicalName());
// Private constructor to prevent instantiation of this factory class.
private EntityStoreFactory() {}
diff --git a/core/src/main/java/com/datastrato/gravitino/GravitinoEnv.java b/core/src/main/java/com/datastrato/gravitino/GravitinoEnv.java
index 820af076e1..9b27f561b0 100644
--- a/core/src/main/java/com/datastrato/gravitino/GravitinoEnv.java
+++ b/core/src/main/java/com/datastrato/gravitino/GravitinoEnv.java
@@ -27,8 +27,6 @@ public class GravitinoEnv {
private EntityStore entityStore;
- private EntitySerDe entitySerDe;
-
private CatalogManager catalogManager;
private CatalogOperationDispatcher catalogOperationDispatcher;
@@ -68,13 +66,9 @@ public class GravitinoEnv {
this.metricsSystem = new MetricsSystem();
metricsSystem.register(new JVMMetricsSource());
- // Initialize EntitySerDe
- this.entitySerDe = EntitySerDeFactory.createEntitySerDe(config);
-
// Initialize EntityStore
this.entityStore = EntityStoreFactory.createEntityStore(config);
entityStore.initialize(config);
- entityStore.setSerDe(entitySerDe);
// create and initialize a random id generator
this.idGenerator = new RandomIdGenerator();
@@ -103,15 +97,6 @@ public class GravitinoEnv {
return config;
}
- /**
- * Get the EntitySerDe associated with the Gravitino environment.
- *
- * @return The EntitySerDe instance.
- */
- public EntitySerDe entitySerDe() {
- return entitySerDe;
- }
-
/**
* Get the EntityStore associated with the Gravitino environment.
*
diff --git a/core/src/main/java/com/datastrato/gravitino/storage/kv/KvEntityStore.java b/core/src/main/java/com/datastrato/gravitino/storage/kv/KvEntityStore.java
index 9e9a355226..18deb7a4cc 100644
--- a/core/src/main/java/com/datastrato/gravitino/storage/kv/KvEntityStore.java
+++ b/core/src/main/java/com/datastrato/gravitino/storage/kv/KvEntityStore.java
@@ -17,6 +17,7 @@ import com.datastrato.gravitino.Entity;
import com.datastrato.gravitino.Entity.EntityType;
import com.datastrato.gravitino.EntityAlreadyExistsException;
import com.datastrato.gravitino.EntitySerDe;
+import com.datastrato.gravitino.EntitySerDeFactory;
import com.datastrato.gravitino.EntityStore;
import com.datastrato.gravitino.HasIdentifier;
import com.datastrato.gravitino.NameIdentifier;
@@ -103,6 +104,7 @@ public class KvEntityStore implements EntityStore {
kvGarbageCollector.start();
this.storageLayoutVersion = initStorageVersionInfo();
+ this.serDe = EntitySerDeFactory.createEntitySerDe(config);
}
@Override
diff --git a/core/src/main/java/com/datastrato/gravitino/storage/relational/RelationalBackend.java b/core/src/main/java/com/datastrato/gravitino/storage/relational/RelationalBackend.java
new file mode 100644
index 0000000000..8969db9f91
--- /dev/null
+++ b/core/src/main/java/com/datastrato/gravitino/storage/relational/RelationalBackend.java
@@ -0,0 +1,94 @@
+/*
+ * 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 com.datastrato.gravitino.Entity;
+import com.datastrato.gravitino.EntityAlreadyExistsException;
+import com.datastrato.gravitino.HasIdentifier;
+import com.datastrato.gravitino.NameIdentifier;
+import com.datastrato.gravitino.Namespace;
+import com.datastrato.gravitino.exceptions.NoSuchEntityException;
+import java.io.Closeable;
+import java.io.IOException;
+import java.util.List;
+import java.util.function.Function;
+
+/** Interface defining the operations for a Relation Backend. */
+public interface RelationalBackend extends Closeable {
+
+ /**
+ * Initializes the Relational Backend environment with the provided configuration.
+ *
+ * @param config The configuration for the backend.
+ */
+ void initialize(Config config);
+
+ /**
+ * Lists the entities associated with the given parent namespace and entityType
+ *
+ * @param namespace The parent namespace of these entities.
+ * @param entityType The type of these entities.
+ * @return The list of entities associated with the given parent namespace and entityType, or null
+ * if the entities does not exist.
+ * @throws NoSuchEntityException If the corresponding parent entity of these list entities cannot
+ * be found.
+ */
+ <E extends Entity & HasIdentifier> List<E> list(Namespace namespace, Entity.EntityType entityType)
+ throws NoSuchEntityException;
+
+ /**
+ * Checks the entity associated with the given identifier and entityType whether exists.
+ *
+ * @param ident The identifier of the entity.
+ * @param entityType The type of the entity.
+ * @return True, if the entity can be found, else return false.
+ */
+ boolean exists(NameIdentifier ident, Entity.EntityType entityType);
+
+ /**
+ * Stores the entity, possibly overwriting an existing entity if specified.
+ *
+ * @param e The entity which need be stored.
+ * @param overwritten If true, overwrites the existing value.
+ * @throws EntityAlreadyExistsException If the entity already exists and overwrite is false.
+ */
+ <E extends Entity & HasIdentifier> void insert(E e, boolean overwritten)
+ throws EntityAlreadyExistsException;
+
+ /**
+ * Updates the entity.
+ *
+ * @param ident The identifier of the entity which need be stored.
+ * @param entityType The type of the entity.
+ * @return The entity after updating.
+ * @throws NoSuchEntityException If the entity is not exist.
+ */
+ <E extends Entity & HasIdentifier> E update(
+ NameIdentifier ident, Entity.EntityType entityType, Function<E, E> updater)
+ throws NoSuchEntityException;
+
+ /**
+ * Retrieves the entity associated with the identifier and the entity type.
+ *
+ * @param ident The identifier of the entity.
+ * @param entityType The type of the entity.
+ * @return The entity associated with the identifier and the entity type, or null if the key does
+ * not exist.
+ * @throws IOException If an I/O exception occurs during retrieval.
+ */
+ <E extends Entity & HasIdentifier> E get(NameIdentifier ident, Entity.EntityType entityType)
+ throws IOException;
+
+ /**
+ * Deletes the entity associated with the identifier and the entity type.
+ *
+ * @param ident The identifier of the entity.
+ * @param entityType The type of the entity.
+ * @param cascade True, If you need to cascade delete entities, else false.
+ * @return True, if the entity was successfully deleted, else false.
+ */
+ boolean delete(NameIdentifier ident, Entity.EntityType entityType, boolean cascade);
+}
diff --git a/core/src/main/java/com/datastrato/gravitino/storage/relational/RelationalEntityStore.java b/core/src/main/java/com/datastrato/gravitino/storage/relational/RelationalEntityStore.java
new file mode 100644
index 0000000000..d614d7d7a1
--- /dev/null
+++ b/core/src/main/java/com/datastrato/gravitino/storage/relational/RelationalEntityStore.java
@@ -0,0 +1,119 @@
+/*
+ * Copyright 2024 Datastrato Pvt Ltd.
+ * This software is licensed under the Apache License version 2.
+ */
+package com.datastrato.gravitino.storage.relational;
+
+import static com.datastrato.gravitino.Configs.ENTITY_RELATIONAL_STORE;
+
+import com.datastrato.gravitino.Config;
+import com.datastrato.gravitino.Configs;
+import com.datastrato.gravitino.Entity;
+import com.datastrato.gravitino.EntityAlreadyExistsException;
+import com.datastrato.gravitino.EntitySerDe;
+import com.datastrato.gravitino.EntityStore;
+import com.datastrato.gravitino.HasIdentifier;
+import com.datastrato.gravitino.NameIdentifier;
+import com.datastrato.gravitino.Namespace;
+import com.datastrato.gravitino.exceptions.AlreadyExistsException;
+import com.datastrato.gravitino.exceptions.NoSuchEntityException;
+import com.datastrato.gravitino.storage.relational.mysql.MySQLBackend;
+import com.datastrato.gravitino.utils.Executable;
+import com.google.common.collect.ImmutableMap;
+import java.io.IOException;
+import java.util.List;
+import java.util.function.Function;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Relation store to store entities. This means we can store entities in a relational store. I.e.,
+ * MySQL, PostgreSQL, etc. If you want to use a different backend, you can implement the {@link
+ * RelationalBackend} interface
+ */
+public class RelationalEntityStore implements EntityStore {
+ private static final Logger LOGGER = LoggerFactory.getLogger(RelationalEntityStore.class);
+ public static final ImmutableMap<String, String> RELATIONAL_BACKENDS =
+ ImmutableMap.of(
+ Configs.DEFAULT_ENTITY_RELATIONAL_STORE, MySQLBackend.class.getCanonicalName());
+ private RelationalBackend backend;
+
+ @Override
+ public void initialize(Config config) throws RuntimeException {
+ this.backend = createRelationalEntityBackend(config);
+ }
+
+ private static RelationalBackend createRelationalEntityBackend(Config config) {
+ String backendName = config.get(ENTITY_RELATIONAL_STORE);
+ String className =
+ RELATIONAL_BACKENDS.getOrDefault(backendName, Configs.DEFAULT_ENTITY_RELATIONAL_STORE);
+
+ try {
+ RelationalBackend relationalBackend =
+ (RelationalBackend) Class.forName(className).getDeclaredConstructor().newInstance();
+ relationalBackend.initialize(config);
+ return relationalBackend;
+ } catch (Exception e) {
+ LOGGER.error(
+ "Failed to create and initialize RelationalBackend by name '{}'.", backendName, e);
+ throw new RuntimeException(
+ "Failed to create and initialize RelationalBackend by name: " + backendName, e);
+ }
+ }
+
+ @Override
+ public void setSerDe(EntitySerDe entitySerDe) {
+ throw new UnsupportedOperationException("Unsupported operation in relational entity store.");
+ }
+
+ @Override
+ public <E extends Entity & HasIdentifier> List<E> list(
+ Namespace namespace, Class<E> type, Entity.EntityType entityType) throws IOException {
+ return backend.list(namespace, entityType);
+ }
+
+ @Override
+ public boolean exists(NameIdentifier ident, Entity.EntityType entityType) throws IOException {
+ return backend.exists(ident, entityType);
+ }
+
+ @Override
+ public <E extends Entity & HasIdentifier> void put(E e, boolean overwritten)
+ throws IOException, EntityAlreadyExistsException {
+ backend.insert(e, overwritten);
+ }
+
+ @Override
+ public <E extends Entity & HasIdentifier> E update(
+ NameIdentifier ident, Class<E> type, Entity.EntityType entityType, Function<E, E> updater)
+ throws IOException, NoSuchEntityException, AlreadyExistsException {
+ return backend.update(ident, entityType, updater);
+ }
+
+ @Override
+ public <E extends Entity & HasIdentifier> E get(
+ NameIdentifier ident, Entity.EntityType entityType, Class<E> e)
+ throws NoSuchEntityException, IOException {
+ E entity = backend.get(ident, entityType);
+ if (entity == null) {
+ throw new NoSuchEntityException(ident.toString());
+ }
+ return entity;
+ }
+
+ @Override
+ public boolean delete(NameIdentifier ident, Entity.EntityType entityType, boolean cascade)
+ throws IOException {
+ return backend.delete(ident, entityType, cascade);
+ }
+
+ @Override
+ public <R, E extends Exception> R executeInTransaction(Executable<R, E> executable) {
+ throw new UnsupportedOperationException("Unsupported operation in relational entity store.");
+ }
+
+ @Override
+ public void close() throws IOException {
+ backend.close();
+ }
+}
diff --git a/core/src/main/java/com/datastrato/gravitino/storage/relational/mysql/MySQLBackend.java b/core/src/main/java/com/datastrato/gravitino/storage/relational/mysql/MySQLBackend.java
new file mode 100644
index 0000000000..4b793a02dd
--- /dev/null
+++ b/core/src/main/java/com/datastrato/gravitino/storage/relational/mysql/MySQLBackend.java
@@ -0,0 +1,67 @@
+/*
+ * Copyright 2024 Datastrato Pvt Ltd.
+ * This software is licensed under the Apache License version 2.
+ */
+package com.datastrato.gravitino.storage.relational.mysql;
+
+import com.datastrato.gravitino.Config;
+import com.datastrato.gravitino.Entity;
+import com.datastrato.gravitino.EntityAlreadyExistsException;
+import com.datastrato.gravitino.HasIdentifier;
+import com.datastrato.gravitino.NameIdentifier;
+import com.datastrato.gravitino.Namespace;
+import com.datastrato.gravitino.exceptions.NoSuchEntityException;
+import com.datastrato.gravitino.storage.relational.RelationalBackend;
+import java.io.IOException;
+import java.util.List;
+import java.util.function.Function;
+
+/**
+ * {@link MySQLBackend} is a MySQL implementation of RelationalBackend interface. If we want to use
+ * another relational implementation, We can just implement {@link RelationalBackend} interface and
+ * use it in the Gravitino.
+ */
+public class MySQLBackend implements RelationalBackend {
+
+ /** Initialize the MySQL backend instance. */
+ @Override
+ public void initialize(Config config) {}
+
+ @Override
+ public <E extends Entity & HasIdentifier> List<E> list(
+ Namespace namespace, Entity.EntityType entityType) throws NoSuchEntityException {
+ throw new UnsupportedOperationException("Unsupported operation now.");
+ }
+
+ @Override
+ public boolean exists(NameIdentifier ident, Entity.EntityType entityType) {
+ throw new UnsupportedOperationException("Unsupported operation now.");
+ }
+
+ @Override
+ public <E extends Entity & HasIdentifier> void insert(E e, boolean overwritten)
+ throws EntityAlreadyExistsException {
+ throw new UnsupportedOperationException("Unsupported operation now.");
+ }
+
+ @Override
+ public <E extends Entity & HasIdentifier> E update(
+ NameIdentifier ident, Entity.EntityType entityType, Function<E, E> updater)
+ throws NoSuchEntityException {
+ throw new UnsupportedOperationException("Unsupported operation now.");
+ }
+
+ @Override
+ public <E extends Entity & HasIdentifier> E get(
+ NameIdentifier ident, Entity.EntityType entityType) throws IOException {
+ throw new UnsupportedOperationException("Unsupported operation now.");
+ }
+
+ @Override
+ public boolean delete(NameIdentifier ident, Entity.EntityType entityType, boolean cascade) {
+ throw new UnsupportedOperationException("Unsupported operation now.");
+ }
+
+ @Override
+ public void close() throws IOException {}
+}
diff --git a/core/src/test/java/com/datastrato/gravitino/TestEntityStore.java b/core/src/test/java/com/datastrato/gravitino/TestEntityStore.java
index 4ddb8bfcab..6393854df2 100644
--- a/core/src/test/java/com/datastrato/gravitino/TestEntityStore.java
+++ b/core/src/test/java/com/datastrato/gravitino/TestEntityStore.java
@@ -45,7 +45,9 @@ public class TestEntityStore {
}
@Override
- public void initialize(Config config) throws RuntimeException {}
+ public void initialize(Config config) throws RuntimeException {
+ this.serde = Mockito.mock(EntitySerDe.class);
+ }
@Override
public void setSerDe(EntitySerDe entitySerDe) {
diff --git a/core/src/test/java/com/datastrato/gravitino/storage/relational/TestRelationalEntityStore.java b/core/src/test/java/com/datastrato/gravitino/storage/relational/TestRelationalEntityStore.java
new file mode 100644
index 0000000000..039e3bf0f3
--- /dev/null
+++ b/core/src/test/java/com/datastrato/gravitino/storage/relational/TestRelationalEntityStore.java
@@ -0,0 +1,104 @@
+/*
+ * Copyright 2024 Datastrato Pvt Ltd.
+ * This software is licensed under the Apache License version 2.
+ */
+
+package com.datastrato.gravitino.storage.relational;
+
+import static com.datastrato.gravitino.Configs.DEFAULT_ENTITY_RELATIONAL_STORE;
+
+import com.datastrato.gravitino.Config;
+import com.datastrato.gravitino.Configs;
+import com.datastrato.gravitino.Entity;
+import com.datastrato.gravitino.EntityStore;
+import com.datastrato.gravitino.EntityStoreFactory;
+import com.datastrato.gravitino.NameIdentifier;
+import com.datastrato.gravitino.Namespace;
+import com.datastrato.gravitino.meta.BaseMetalake;
+import java.io.IOException;
+import java.util.function.Function;
+import org.junit.jupiter.api.AfterAll;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
+import org.mockito.Mockito;
+
+public class TestRelationalEntityStore {
+ private static EntityStore store;
+
+ @BeforeAll
+ public static void setUp() {
+ Config config = Mockito.mock(Config.class);
+ Mockito.when(config.get(Configs.ENTITY_STORE)).thenReturn(Configs.RELATIONAL_ENTITY_STORE);
+ Mockito.when(config.get(Configs.ENTITY_RELATIONAL_STORE))
+ .thenReturn(DEFAULT_ENTITY_RELATIONAL_STORE);
+ store = EntityStoreFactory.createEntityStore(config);
+ store.initialize(config);
+ Assertions.assertTrue(store instanceof RelationalEntityStore);
+ }
+
+ @AfterAll
+ public static void teardown() throws IOException {
+ store.close();
+ store = null;
+ }
+
+ @Test
+ public void testSetSerDe() {
+ Assertions.assertThrows(UnsupportedOperationException.class, () -> store.setSerDe(null));
+ }
+
+ @Test
+ public void testExecuteInTransaction() {
+ Assertions.assertThrows(
+ UnsupportedOperationException.class, () -> store.executeInTransaction(null));
+ }
+
+ @Test
+ public void testExists() throws IOException {
+ NameIdentifier nameIdentifier = Mockito.mock(NameIdentifier.class);
+ Assertions.assertThrows(
+ UnsupportedOperationException.class,
+ () -> store.exists(nameIdentifier, Entity.EntityType.METALAKE));
+ }
+
+ @Test
+ public void testPut() throws IOException {
+ BaseMetalake metalake = Mockito.mock(BaseMetalake.class);
+ Assertions.assertThrows(UnsupportedOperationException.class, () -> store.put(metalake, false));
+ }
+
+ @Test
+ public void testGet() {
+ NameIdentifier nameIdentifier = Mockito.mock(NameIdentifier.class);
+ Assertions.assertThrows(
+ UnsupportedOperationException.class,
+ () -> store.get(nameIdentifier, Entity.EntityType.METALAKE, BaseMetalake.class));
+ }
+
+ @Test
+ public void testUpdate() {
+ NameIdentifier nameIdentifier = Mockito.mock(NameIdentifier.class);
+ Function function = Mockito.mock(Function.class);
+ Assertions.assertThrows(
+ UnsupportedOperationException.class,
+ () ->
+ store.update(nameIdentifier, BaseMetalake.class, Entity.EntityType.METALAKE, function));
+ }
+
+ @Test
+ public void testList() {
+ Namespace namespace = Mockito.mock(Namespace.class);
+ Assertions.assertThrows(
+ UnsupportedOperationException.class,
+ () -> store.list(namespace, BaseMetalake.class, Entity.EntityType.METALAKE));
+ }
+
+ @Test
+ public void testDelete() {
+ NameIdentifier nameIdentifier = Mockito.mock(NameIdentifier.class);
+ Assertions.assertThrows(
+ UnsupportedOperationException.class,
+ () -> store.delete(nameIdentifier, Entity.EntityType.METALAKE, false));
+ }
+}