Skip to content

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

[#2414] improved(core): Make the unit tests for entity storage unified in the core module (#2545)

xloya50256c89ee23
diff --git a/core/src/main/java/com/datastrato/gravitino/storage/relational/utils/ExceptionUtils.java b/core/src/main/java/com/datastrato/gravitino/storage/relational/utils/ExceptionUtils.java
index 21ac6057b6..bf37465a24 100644
--- a/core/src/main/java/com/datastrato/gravitino/storage/relational/utils/ExceptionUtils.java
+++ b/core/src/main/java/com/datastrato/gravitino/storage/relational/utils/ExceptionUtils.java
@@ -5,7 +5,7 @@
package com.datastrato.gravitino.storage.relational.utils;
import com.datastrato.gravitino.Entity;
-import com.datastrato.gravitino.EntityAlreadyExistsException;
+import com.datastrato.gravitino.exceptions.AlreadyExistsException;
import java.sql.SQLIntegrityConstraintViolationException;
public class ExceptionUtils {
@@ -19,8 +19,7 @@ public class ExceptionUtils {
// Usually throwing `SQLIntegrityConstraintViolationException` means that
// SQL violates the constraints of `primary key` and `unique key`.
// We simply think that the entity already exists at this time.
- throw new EntityAlreadyExistsException(
- String.format("%s entity: %s already exists", type.name(), entityName));
+ throw new AlreadyExistsException("%s entity: %s already exists", type.name(), entityName);
}
}
}
diff --git a/core/src/main/java/com/datastrato/gravitino/storage/relational/utils/POConverters.java b/core/src/main/java/com/datastrato/gravitino/storage/relational/utils/POConverters.java
index 29c2e69512..013dafa4a4 100644
--- a/core/src/main/java/com/datastrato/gravitino/storage/relational/utils/POConverters.java
+++ b/core/src/main/java/com/datastrato/gravitino/storage/relational/utils/POConverters.java
@@ -497,6 +497,9 @@ public class POConverters {
try {
Map<String, String> oldProperties =
JsonUtils.anyFieldMapper().readValue(oldFilesetVersionPO.getProperties(), Map.class);
+ if (oldProperties == null) {
+ return newFileset.properties() != null;
+ }
return !oldProperties.equals(newFileset.properties());
} catch (JsonProcessingException e) {
throw new RuntimeException("Failed to deserialize json object:", e);
diff --git a/core/src/test/java/com/datastrato/gravitino/catalog/TestCatalogManager.java b/core/src/test/java/com/datastrato/gravitino/catalog/TestCatalogManager.java
index c60825e86b..42d2f28e5d 100644
--- a/core/src/test/java/com/datastrato/gravitino/catalog/TestCatalogManager.java
+++ b/core/src/test/java/com/datastrato/gravitino/catalog/TestCatalogManager.java
@@ -14,8 +14,6 @@ import com.datastrato.gravitino.EntityStore;
import com.datastrato.gravitino.NameIdentifier;
import com.datastrato.gravitino.Namespace;
import com.datastrato.gravitino.StringIdentifier;
-import com.datastrato.gravitino.TestEntityStore;
-import com.datastrato.gravitino.TestEntityStore.InMemoryEntityStore;
import com.datastrato.gravitino.exceptions.CatalogAlreadyExistsException;
import com.datastrato.gravitino.exceptions.NoSuchCatalogException;
import com.datastrato.gravitino.exceptions.NoSuchMetalakeException;
@@ -23,6 +21,8 @@ import com.datastrato.gravitino.meta.AuditInfo;
import com.datastrato.gravitino.meta.BaseMetalake;
import com.datastrato.gravitino.meta.SchemaVersion;
import com.datastrato.gravitino.storage.RandomIdGenerator;
+import com.datastrato.gravitino.storage.memory.TestMemoryEntityStore;
+import com.datastrato.gravitino.storage.memory.TestMemoryEntityStore.InMemoryEntityStore;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
@@ -64,7 +64,7 @@ public class TestCatalogManager {
config = new Config(false) {};
config.set(Configs.CATALOG_LOAD_ISOLATED, false);
- entityStore = new TestEntityStore.InMemoryEntityStore();
+ entityStore = new TestMemoryEntityStore.InMemoryEntityStore();
entityStore.initialize(config);
entityStore.setSerDe(null);
diff --git a/core/src/test/java/com/datastrato/gravitino/catalog/TestCatalogOperationDispatcher.java b/core/src/test/java/com/datastrato/gravitino/catalog/TestCatalogOperationDispatcher.java
index 6d5c25c3a5..88c354dcff 100644
--- a/core/src/test/java/com/datastrato/gravitino/catalog/TestCatalogOperationDispatcher.java
+++ b/core/src/test/java/com/datastrato/gravitino/catalog/TestCatalogOperationDispatcher.java
@@ -27,7 +27,6 @@ import com.datastrato.gravitino.NameIdentifier;
import com.datastrato.gravitino.Namespace;
import com.datastrato.gravitino.StringIdentifier;
import com.datastrato.gravitino.TestColumn;
-import com.datastrato.gravitino.TestEntityStore;
import com.datastrato.gravitino.auth.AuthConstants;
import com.datastrato.gravitino.exceptions.IllegalNamespaceException;
import com.datastrato.gravitino.exceptions.NoSuchEntityException;
@@ -47,6 +46,7 @@ import com.datastrato.gravitino.rel.expressions.transforms.Transform;
import com.datastrato.gravitino.rel.types.Types;
import com.datastrato.gravitino.storage.IdGenerator;
import com.datastrato.gravitino.storage.RandomIdGenerator;
+import com.datastrato.gravitino.storage.memory.TestMemoryEntityStore;
import com.google.common.collect.ImmutableMap;
import java.io.IOException;
import java.time.Instant;
@@ -82,7 +82,7 @@ public class TestCatalogOperationDispatcher {
config = new Config(false) {};
config.set(Configs.CATALOG_LOAD_ISOLATED, false);
- entityStore = spy(new TestEntityStore.InMemoryEntityStore());
+ entityStore = spy(new TestMemoryEntityStore.InMemoryEntityStore());
entityStore.initialize(config);
entityStore.setSerDe(null);
diff --git a/core/src/test/java/com/datastrato/gravitino/metalake/TestMetalakeManager.java b/core/src/test/java/com/datastrato/gravitino/metalake/TestMetalakeManager.java
index 3e0223d133..ad86b4709f 100644
--- a/core/src/test/java/com/datastrato/gravitino/metalake/TestMetalakeManager.java
+++ b/core/src/test/java/com/datastrato/gravitino/metalake/TestMetalakeManager.java
@@ -9,11 +9,11 @@ import com.datastrato.gravitino.EntityStore;
import com.datastrato.gravitino.MetalakeChange;
import com.datastrato.gravitino.NameIdentifier;
import com.datastrato.gravitino.StringIdentifier;
-import com.datastrato.gravitino.TestEntityStore;
import com.datastrato.gravitino.exceptions.MetalakeAlreadyExistsException;
import com.datastrato.gravitino.exceptions.NoSuchMetalakeException;
import com.datastrato.gravitino.meta.BaseMetalake;
import com.datastrato.gravitino.storage.RandomIdGenerator;
+import com.datastrato.gravitino.storage.memory.TestMemoryEntityStore;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Sets;
import java.io.IOException;
@@ -36,7 +36,7 @@ public class TestMetalakeManager {
public static void setUp() {
config = new Config(false) {};
- entityStore = new TestEntityStore.InMemoryEntityStore();
+ entityStore = new TestMemoryEntityStore.InMemoryEntityStore();
entityStore.initialize(config);
entityStore.setSerDe(null);
diff --git a/core/src/test/java/com/datastrato/gravitino/storage/TestEntityStorage.java b/core/src/test/java/com/datastrato/gravitino/storage/TestEntityStorage.java
new file mode 100644
index 0000000000..7d4fbb6afe
--- /dev/null
+++ b/core/src/test/java/com/datastrato/gravitino/storage/TestEntityStorage.java
@@ -0,0 +1,1436 @@
+/*
+ * Copyright 2024 Datastrato Pvt Ltd.
+ * This software is licensed under the Apache License version 2.
+ */
+
+package com.datastrato.gravitino.storage;
+
+import static com.datastrato.gravitino.Configs.DEFAULT_ENTITY_KV_STORE;
+import static com.datastrato.gravitino.Configs.DEFAULT_ENTITY_RELATIONAL_STORE;
+import static com.datastrato.gravitino.Configs.ENTITY_KV_STORE;
+import static com.datastrato.gravitino.Configs.ENTITY_RELATIONAL_JDBC_BACKEND_DRIVER;
+import static com.datastrato.gravitino.Configs.ENTITY_RELATIONAL_JDBC_BACKEND_PASSWORD;
+import static com.datastrato.gravitino.Configs.ENTITY_RELATIONAL_JDBC_BACKEND_URL;
+import static com.datastrato.gravitino.Configs.ENTITY_RELATIONAL_JDBC_BACKEND_USER;
+import static com.datastrato.gravitino.Configs.ENTITY_RELATIONAL_STORE;
+import static com.datastrato.gravitino.Configs.ENTITY_STORE;
+import static com.datastrato.gravitino.Configs.ENTRY_KV_ROCKSDB_BACKEND_PATH;
+import static com.datastrato.gravitino.Configs.KV_DELETE_AFTER_TIME;
+import static com.datastrato.gravitino.Configs.RELATIONAL_ENTITY_STORE;
+import static com.datastrato.gravitino.Configs.STORE_TRANSACTION_MAX_SKEW_TIME;
+
+import com.datastrato.gravitino.Catalog;
+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.exceptions.AlreadyExistsException;
+import com.datastrato.gravitino.exceptions.NoSuchEntityException;
+import com.datastrato.gravitino.exceptions.NonEmptyEntityException;
+import com.datastrato.gravitino.file.Fileset;
+import com.datastrato.gravitino.meta.AuditInfo;
+import com.datastrato.gravitino.meta.BaseMetalake;
+import com.datastrato.gravitino.meta.CatalogEntity;
+import com.datastrato.gravitino.meta.FilesetEntity;
+import com.datastrato.gravitino.meta.SchemaEntity;
+import com.datastrato.gravitino.meta.SchemaVersion;
+import com.datastrato.gravitino.meta.TableEntity;
+import com.datastrato.gravitino.storage.relational.RelationalEntityStore;
+import com.datastrato.gravitino.storage.relational.session.SqlSessionFactoryHelper;
+import com.google.common.base.Preconditions;
+import java.io.File;
+import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+import java.sql.Connection;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.time.Instant;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Objects;
+import java.util.UUID;
+import org.apache.commons.io.FileUtils;
+import org.apache.commons.io.IOUtils;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.ibatis.session.SqlSession;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.MethodSource;
+import org.mockito.Mockito;
+
+public class TestEntityStorage {
+ public static final String KV_STORE_PATH =
+ "/tmp/gravitino_kv_entityStore_" + UUID.randomUUID().toString().replace("-", "");
+
+ private static final String JDBC_STORE_PATH =
+ "/tmp/gravitino_jdbc_entityStore_" + UUID.randomUUID().toString().replace("-", "");
+ private static final String DB_DIR = JDBC_STORE_PATH + "/testdb";
+
+ static Object[] storageProvider() {
+ return new Object[] {Configs.DEFAULT_ENTITY_STORE, Configs.RELATIONAL_ENTITY_STORE};
+ }
+
+ private void init(String type, Config config) {
+ Preconditions.checkArgument(StringUtils.isNotBlank(type));
+ if (type.equals(Configs.DEFAULT_ENTITY_STORE)) {
+ try {
+ FileUtils.deleteDirectory(FileUtils.getFile(KV_STORE_PATH));
+ } catch (Exception e) {
+ // Ignore
+ }
+ Mockito.when(config.get(ENTITY_STORE)).thenReturn("kv");
+ Mockito.when(config.get(ENTITY_KV_STORE)).thenReturn(DEFAULT_ENTITY_KV_STORE);
+ Mockito.when(config.get(Configs.ENTITY_SERDE)).thenReturn("proto");
+ Mockito.when(config.get(ENTRY_KV_ROCKSDB_BACKEND_PATH)).thenReturn(KV_STORE_PATH);
+
+ Assertions.assertEquals(KV_STORE_PATH, config.get(ENTRY_KV_ROCKSDB_BACKEND_PATH));
+ Mockito.when(config.get(STORE_TRANSACTION_MAX_SKEW_TIME)).thenReturn(1000L);
+ Mockito.when(config.get(KV_DELETE_AFTER_TIME)).thenReturn(20 * 60 * 1000L);
+ } else if (type.equals(Configs.RELATIONAL_ENTITY_STORE)) {
+ File dir = new File(DB_DIR);
+ if (dir.exists() || !dir.isDirectory()) {
+ dir.delete();
+ }
+ dir.mkdirs();
+ Mockito.when(config.get(ENTITY_STORE)).thenReturn(RELATIONAL_ENTITY_STORE);
+ Mockito.when(config.get(ENTITY_RELATIONAL_STORE)).thenReturn(DEFAULT_ENTITY_RELATIONAL_STORE);
+ Mockito.when(config.get(ENTITY_RELATIONAL_JDBC_BACKEND_URL))
+ .thenReturn(String.format("jdbc:h2:%s;DB_CLOSE_DELAY=-1;MODE=MYSQL", DB_DIR));
+ Mockito.when(config.get(ENTITY_RELATIONAL_JDBC_BACKEND_USER)).thenReturn("root");
+ Mockito.when(config.get(ENTITY_RELATIONAL_JDBC_BACKEND_PASSWORD)).thenReturn("123");
+ Mockito.when(config.get(ENTITY_RELATIONAL_JDBC_BACKEND_DRIVER)).thenReturn("org.h2.Driver");
+ } else {
+ throw new UnsupportedOperationException("Unsupported entity store type: " + type);
+ }
+ }
+
+ private void prepareJdbcTable() {
+ // Read the ddl sql to create table
+ String scriptPath = "h2/h2-init.sql";
+ try (SqlSession sqlSession =
+ SqlSessionFactoryHelper.getInstance().getSqlSessionFactory().openSession(true);
+ Connection connection = sqlSession.getConnection();
+ Statement statement = connection.createStatement()) {
+ StringBuilder ddlBuilder = new StringBuilder();
+ IOUtils.readLines(
+ Objects.requireNonNull(
+ this.getClass().getClassLoader().getResourceAsStream(scriptPath)),
+ StandardCharsets.UTF_8)
+ .forEach(line -> ddlBuilder.append(line).append("\n"));
+ statement.execute(ddlBuilder.toString());
+ } catch (Exception e) {
+ throw new IllegalStateException("Create tables failed", e);
+ }
+ }
+
+ private void destroy(String type) {
+ Preconditions.checkArgument(StringUtils.isNotBlank(type));
+ if (type.equals(Configs.DEFAULT_ENTITY_STORE)) {
+ try {
+ FileUtils.deleteDirectory(FileUtils.getFile(KV_STORE_PATH));
+ } catch (Exception e) {
+ // Ignore
+ }
+ } else if (type.equals(Configs.RELATIONAL_ENTITY_STORE)) {
+ dropAllTables();
+ File dir = new File(DB_DIR);
+ if (dir.exists()) {
+ dir.delete();
+ }
+ } else {
+ throw new UnsupportedOperationException("Unsupported entity store type: " + type);
+ }
+ }
+
+ private static void dropAllTables() {
+ try (SqlSession sqlSession =
+ SqlSessionFactoryHelper.getInstance().getSqlSessionFactory().openSession(true)) {
+ try (Connection connection = sqlSession.getConnection()) {
+ try (Statement statement = connection.createStatement()) {
+ String query = "SHOW TABLES";
+ List<String> tableList = new ArrayList<>();
+ try (ResultSet rs = statement.executeQuery(query)) {
+ while (rs.next()) {
+ tableList.add(rs.getString(1));
+ }
+ }
+ for (String table : tableList) {
+ statement.execute("DROP TABLE " + table);
+ }
+ }
+ }
+ } catch (SQLException e) {
+ throw new RuntimeException("Drop table failed", e);
+ }
+ }
+
+ @ParameterizedTest
+ @MethodSource("storageProvider")
+ void testRestart(String type) throws IOException {
+ Config config = Mockito.mock(Config.class);
+ init(type, config);
+ AuditInfo auditInfo =
+ AuditInfo.builder().withCreator("creator").withCreateTime(Instant.now()).build();
+
+ try (EntityStore store = EntityStoreFactory.createEntityStore(config)) {
+ store.initialize(config);
+ if (store instanceof RelationalEntityStore) {
+ prepareJdbcTable();
+ }
+
+ BaseMetalake metalake =
+ createBaseMakeLake(RandomIdGenerator.INSTANCE.nextId(), "metalake", auditInfo);
+ CatalogEntity catalog =
+ createCatalog(
+ RandomIdGenerator.INSTANCE.nextId(), Namespace.of("metalake"), "catalog", auditInfo);
+ CatalogEntity catalogCopy =
+ createCatalog(
+ RandomIdGenerator.INSTANCE.nextId(),
+ Namespace.of("metalake"),
+ "catalogCopy",
+ auditInfo);
+
+ SchemaEntity schema1 =
+ createSchemaEntity(
+ RandomIdGenerator.INSTANCE.nextId(),
+ Namespace.of("metalake", "catalog"),
+ "schema1",
+ auditInfo);
+ TableEntity table1 =
+ createTableEntity(
+ RandomIdGenerator.INSTANCE.nextId(),
+ Namespace.of("metalake", "catalog", "schema1"),
+ "table1",
+ auditInfo);
+ FilesetEntity fileset1 =
+ createFilesetEntity(
+ RandomIdGenerator.INSTANCE.nextId(),
+ Namespace.of("metalake", "catalog", "schema1"),
+ "fileset1",
+ auditInfo);
+
+ // Store all entities
+ store.put(metalake);
+ store.put(catalog);
+ store.put(catalogCopy);
+ store.put(schema1);
+ store.put(table1);
+ store.put(fileset1);
+
+ Assertions.assertDoesNotThrow(
+ () ->
+ store.get(
+ NameIdentifier.of("metalake"), Entity.EntityType.METALAKE, BaseMetalake.class));
+ Assertions.assertDoesNotThrow(
+ () ->
+ store.get(
+ NameIdentifier.of("metalake", "catalog"),
+ Entity.EntityType.CATALOG,
+ CatalogEntity.class));
+ Assertions.assertDoesNotThrow(
+ () ->
+ store.get(
+ NameIdentifier.of("metalake", "catalog", "schema1"),
+ Entity.EntityType.SCHEMA,
+ SchemaEntity.class));
+ Assertions.assertDoesNotThrow(
+ () ->
+ store.get(
+ NameIdentifier.of("metalake", "catalog", "schema1", "table1"),
+ Entity.EntityType.TABLE,
+ TableEntity.class));
+ Assertions.assertDoesNotThrow(
+ () ->
+ store.get(
+ NameIdentifier.of("metalake", "catalog", "schema1", "fileset1"),
+ Entity.EntityType.FILESET,
+ FilesetEntity.class));
+ }
+
+ // It will automatically close the store we create before, then we reopen the entity store
+ try (EntityStore store = EntityStoreFactory.createEntityStore(config)) {
+ store.initialize(config);
+
+ Assertions.assertDoesNotThrow(
+ () ->
+ store.get(
+ NameIdentifier.of("metalake"), Entity.EntityType.METALAKE, BaseMetalake.class));
+ Assertions.assertDoesNotThrow(
+ () ->
+ store.get(
+ NameIdentifier.of("metalake", "catalog"),
+ Entity.EntityType.CATALOG,
+ CatalogEntity.class));
+ Assertions.assertDoesNotThrow(
+ () ->
+ store.get(
+ NameIdentifier.of("metalake", "catalog", "schema1"),
+ Entity.EntityType.SCHEMA,
+ SchemaEntity.class));
+ Assertions.assertDoesNotThrow(
+ () ->
+ store.get(
+ NameIdentifier.of("metalake", "catalog", "schema1", "table1"),
+ Entity.EntityType.TABLE,
+ TableEntity.class));
+ Assertions.assertDoesNotThrow(
+ () ->
+ store.get(
+ NameIdentifier.of("metalake", "catalog", "schema1", "fileset1"),
+ Entity.EntityType.FILESET,
+ FilesetEntity.class));
+ destroy(type);
+ }
+ }
+
+ @ParameterizedTest
+ @MethodSource("storageProvider")
+ void testEntityUpdate(String type) throws Exception {
+ Config config = Mockito.mock(Config.class);
+ init(type, config);
+
+ AuditInfo auditInfo =
+ AuditInfo.builder().withCreator("creator").withCreateTime(Instant.now()).build();
+
+ try (EntityStore store = EntityStoreFactory.createEntityStore(config)) {
+ store.initialize(config);
+ if (store instanceof RelationalEntityStore) {
+ prepareJdbcTable();
+ }
+
+ BaseMetalake metalake =
+ createBaseMakeLake(RandomIdGenerator.INSTANCE.nextId(), "metalake", auditInfo);
+ CatalogEntity catalog =
+ createCatalog(
+ RandomIdGenerator.INSTANCE.nextId(), Namespace.of("metalake"), "catalog", auditInfo);
+ CatalogEntity catalogCopy =
+ createCatalog(
+ RandomIdGenerator.INSTANCE.nextId(),
+ Namespace.of("metalake"),
+ "catalogCopy",
+ auditInfo);
+
+ SchemaEntity schema1 =
+ createSchemaEntity(
+ RandomIdGenerator.INSTANCE.nextId(),
+ Namespace.of("metalake", "catalog"),
+ "schema1",
+ auditInfo);
+ TableEntity table1 =
+ createTableEntity(
+ RandomIdGenerator.INSTANCE.nextId(),
+ Namespace.of("metalake", "catalog", "schema1"),
+ "table1",
+ auditInfo);
+ FilesetEntity fileset1 =
+ createFilesetEntity(
+ RandomIdGenerator.INSTANCE.nextId(),
+ Namespace.of("metalake", "catalog", "schema1"),
+ "fileset1",
+ auditInfo);
+
+ SchemaEntity schema2 =
+ createSchemaEntity(
+ RandomIdGenerator.INSTANCE.nextId(),
+ Namespace.of("metalake", "catalog"),
+ "schema2",
+ auditInfo);
+ TableEntity table1InSchema2 =
+ createTableEntity(
+ RandomIdGenerator.INSTANCE.nextId(),
+ Namespace.of("metalake", "catalog", "schema2"),
+ "table1",
+ auditInfo);
+ FilesetEntity fileset1InSchema2 =
+ createFilesetEntity(
+ RandomIdGenerator.INSTANCE.nextId(),
+ Namespace.of("metalake", "catalog", "schema2"),
+ "fileset1",
+ auditInfo);
+
+ // Store all entities
+ store.put(metalake);
+ store.put(catalog);
+ store.put(catalogCopy);
+ store.put(schema1);
+ store.put(schema2);
+ store.put(table1);
+ store.put(table1InSchema2);
+ store.put(fileset1);
+ store.put(fileset1InSchema2);
+
+ validateMetalakeChanged(store, metalake);
+ validateCatalogChanged(store, catalog);
+ validateSchemaChanged(store, schema1);
+ validateTableChanged(store, table1);
+ validateFilesetChanged(store, fileset1);
+ validateDeletedTable(store);
+ validateDeletedFileset(store);
+ validateAlreadyExistEntity(store, schema2);
+ validateNotChangedEntity(store, schema2);
+
+ destroy(type);
+ }
+ }
+
+ @ParameterizedTest
+ @MethodSource("storageProvider")
+ void testEntityDelete(String type) throws IOException {
+ // TODO
+ Config config = Mockito.mock(Config.class);
+ init(type, config);
+
+ AuditInfo auditInfo =
+ AuditInfo.builder().withCreator("creator").withCreateTime(Instant.now()).build();
+
+ try (EntityStore store = EntityStoreFactory.createEntityStore(config)) {
+ store.initialize(config);
+ if (store instanceof RelationalEntityStore) {
+ prepareJdbcTable();
+ }
+
+ BaseMetalake metalake = createBaseMakeLake(1L, "metalake", auditInfo);
+ CatalogEntity catalog = createCatalog(1L, Namespace.of("metalake"), "catalog", auditInfo);
+ CatalogEntity catalogCopy =
+ createCatalog(2L, Namespace.of("metalake"), "catalogCopy", auditInfo);
+
+ SchemaEntity schema1 =
+ createSchemaEntity(1L, Namespace.of("metalake", "catalog"), "schema1", auditInfo);
+ TableEntity table1 =
+ createTableEntity(
+ 1L, Namespace.of("metalake", "catalog", "schema1"), "table1", auditInfo);
+ FilesetEntity fileset1 =
+ createFilesetEntity(
+ 1L, Namespace.of("metalake", "catalog", "schema1"), "fileset1", auditInfo);
+
+ SchemaEntity schema2 =
+ createSchemaEntity(2L, Namespace.of("metalake", "catalog"), "schema2", auditInfo);
+ TableEntity table1InSchema2 =
+ createTableEntity(
+ 2L, Namespace.of("metalake", "catalog", "schema2"), "table1", auditInfo);
+ FilesetEntity fileset1InSchema2 =
+ createFilesetEntity(
+ 2L, Namespace.of("metalake", "catalog", "schema2"), "fileset1", auditInfo);
+
+ // Store all entities
+ store.put(metalake);
+ store.put(catalog);
+ store.put(catalogCopy);
+ store.put(schema1);
+ store.put(schema2);
+ store.put(table1);
+ store.put(table1InSchema2);
+ store.put(fileset1);
+ store.put(fileset1InSchema2);
+
+ validateAllEntityExist(
+ metalake,
+ store,
+ catalog,
+ catalogCopy,
+ schema1,
+ schema2,
+ table1,
+ table1InSchema2,
+ fileset1,
+ fileset1InSchema2);
+
+ validateDeleteTable(store, schema2, table1, table1InSchema2);
+
+ validateDeleteFileset(store, schema2, fileset1, fileset1InSchema2);
+
+ validateDeleteSchema(store, schema1, table1, fileset1);
+
+ validateDeleteCatalog(
+ store, catalog, table1, schema1, table1InSchema2, schema2, fileset1, fileset1InSchema2);
+
+ validateDeleteMetalake(store, metalake, catalogCopy);
+
+ // Store all entities again
+ BaseMetalake metalakeNew =
+ createBaseMakeLake(
+ RandomIdGenerator.INSTANCE.nextId(),
+ metalake.name(),
+ (AuditInfo) metalake.auditInfo());
+ store.put(metalakeNew);
+ CatalogEntity catalogNew =
+ createCatalog(
+ RandomIdGenerator.INSTANCE.nextId(),
+ catalog.namespace(),
+ catalog.name(),
+ (AuditInfo) catalog.auditInfo());
+ store.put(catalogNew);
+ CatalogEntity catalogCopyNew =
+ createCatalog(
+ RandomIdGenerator.INSTANCE.nextId(),
+ catalogCopy.namespace(),
+ catalogCopy.name(),
+ (AuditInfo) catalogCopy.auditInfo());
+ store.put(catalogCopyNew);
+ SchemaEntity schema1New =
+ createSchemaEntity(
+ RandomIdGenerator.INSTANCE.nextId(),
+ schema1.namespace(),
+ schema1.name(),
+ schema1.auditInfo());
+ store.put(schema1New);
+ SchemaEntity schema2New =
+ createSchemaEntity(
+ RandomIdGenerator.INSTANCE.nextId(),
+ schema2.namespace(),
+ schema2.name(),
+ schema2.auditInfo());
+ store.put(schema2New);
+ TableEntity table1New =
+ createTableEntity(
+ RandomIdGenerator.INSTANCE.nextId(),
+ table1.namespace(),
+ table1.name(),
+ table1.auditInfo());
+ store.put(table1New);
+ TableEntity table1InSchema2New =
+ createTableEntity(
+ RandomIdGenerator.INSTANCE.nextId(),
+ table1InSchema2.namespace(),
+ table1InSchema2.name(),
+ table1InSchema2.auditInfo());
+ store.put(table1InSchema2New);
+ FilesetEntity fileset1New =
+ createFilesetEntity(
+ RandomIdGenerator.INSTANCE.nextId(),
+ fileset1.namespace(),
+ fileset1.name(),
+ fileset1.auditInfo());
+ store.put(fileset1New);
+ FilesetEntity fileset1InSchema2New =
+ createFilesetEntity(
+ RandomIdGenerator.INSTANCE.nextId(),
+ fileset1InSchema2.namespace(),
+ fileset1InSchema2.name(),
+ fileset1InSchema2.auditInfo());
+ store.put(fileset1InSchema2New);
+
+ validateDeleteTableCascade(store, table1New);
+
+ validateDeleteFilesetCascade(store, fileset1New);
+
+ validateDeleteSchemaCascade(store, schema1New, table1New, fileset1New);
+
+ validateDeleteCatalogCascade(store, catalogNew, schema2New);
+
+ validateDeleteMetalakeCascade(store, metalakeNew, catalogNew, schema2New);
+
+ destroy(type);
+ }
+ }
+
+ @ParameterizedTest
+ @MethodSource("storageProvider")
+ void testDeleteAndRename(String type) throws IOException {
+ Config config = Mockito.mock(Config.class);
+ init(type, config);
+ try (EntityStore store = EntityStoreFactory.createEntityStore(config)) {
+ store.initialize(config);
+ if (store instanceof RelationalEntityStore) {
+ prepareJdbcTable();
+ }
+
+ AuditInfo auditInfo =
+ AuditInfo.builder().withCreator("creator").withCreateTime(Instant.now()).build();
+
+ BaseMetalake metalake1 =
+ createBaseMakeLake(RandomIdGenerator.INSTANCE.nextId(), "metalake1", auditInfo);
+ BaseMetalake metalake2 =
+ createBaseMakeLake(RandomIdGenerator.INSTANCE.nextId(), "metalake2", auditInfo);
+ BaseMetalake metalake3 =
+ createBaseMakeLake(RandomIdGenerator.INSTANCE.nextId(), "metalake3", auditInfo);
+
+ store.put(metalake1);
+ store.put(metalake2);
+ store.put(metalake3);
+
+ store.delete(NameIdentifier.of("metalake1"), Entity.EntityType.METALAKE);
+ store.delete(NameIdentifier.of("metalake2"), Entity.EntityType.METALAKE);
+ store.delete(NameIdentifier.of("metalake3"), Entity.EntityType.METALAKE);
+
+ // Rename metalake1 --> metalake2
+ BaseMetalake metalake1New =
+ createBaseMakeLake(RandomIdGenerator.INSTANCE.nextId(), "metalake1", auditInfo);
+ store.put(metalake1New);
+ store.update(
+ NameIdentifier.of("metalake1"),
+ BaseMetalake.class,
+ Entity.EntityType.METALAKE,
+ e -> createBaseMakeLake(metalake1New.id(), "metalake2", (AuditInfo) e.auditInfo()));
+
+ // Rename metalake3 --> metalake1
+ BaseMetalake metalake3New1 =
+ createBaseMakeLake(RandomIdGenerator.INSTANCE.nextId(), "metalake3", auditInfo);
+ store.put(metalake3New1);
+ store.update(
+ NameIdentifier.of("metalake3"),
+ BaseMetalake.class,
+ Entity.EntityType.METALAKE,
+ e -> createBaseMakeLake(metalake3New1.id(), "metalake1", (AuditInfo) e.auditInfo()));
+
+ // Rename metalake3 --> metalake2
+ BaseMetalake metalake3New2 =
+ createBaseMakeLake(RandomIdGenerator.INSTANCE.nextId(), "metalake3", auditInfo);
+ store.put(metalake3New2);
+ Thread.sleep(1000);
+ store.delete(NameIdentifier.of("metalake2"), Entity.EntityType.METALAKE);
+ store.update(
+ NameIdentifier.of("metalake3"),
+ BaseMetalake.class,
+ Entity.EntityType.METALAKE,
+ e -> createBaseMakeLake(metalake3New2.id(), "metalake2", (AuditInfo) e.auditInfo()));
+
+ // Finally, only metalake2 and metalake1 are left.
+ Assertions.assertDoesNotThrow(
+ () ->
+ store.get(
+ NameIdentifier.of("metalake2"), Entity.EntityType.METALAKE, BaseMetalake.class));
+ Assertions.assertDoesNotThrow(
+ () ->
+ store.get(
+ NameIdentifier.of("metalake1"), Entity.EntityType.METALAKE, BaseMetalake.class));
+ NameIdentifier id = NameIdentifier.of("metalake3");
+ Assertions.assertThrows(
+ NoSuchEntityException.class,
+ () -> store.get(id, Entity.EntityType.METALAKE, BaseMetalake.class));
+
+ // Test catalog
+ CatalogEntity catalog1 =
+ createCatalog(
+ RandomIdGenerator.INSTANCE.nextId(),
+ Namespace.of("metalake1"),
+ "catalog1",
+ auditInfo);
+ CatalogEntity catalog2 =
+ createCatalog(
+ RandomIdGenerator.INSTANCE.nextId(),
+ Namespace.of("metalake1"),
+ "catalog2",
+ auditInfo);
+
+ store.put(catalog1);
+ store.put(catalog2);
+
+ store.delete(NameIdentifier.of("metalake1", "catalog1"), Entity.EntityType.CATALOG);
+ store.delete(NameIdentifier.of("metalake1", "catalog2"), Entity.EntityType.CATALOG);
+
+ CatalogEntity catalog1New =
+ createCatalog(
+ RandomIdGenerator.INSTANCE.nextId(),
+ catalog1.namespace(),
+ catalog1.name(),
+ (AuditInfo) catalog1.auditInfo());
+ store.put(catalog1New);
+ // Should be OK;
+ store.update(
+ catalog1New.nameIdentifier(),
+ CatalogEntity.class,
+ Entity.EntityType.CATALOG,
+ e ->
+ createCatalog(
+ catalog1New.id(),
+ Namespace.of("metalake1"),
+ "catalog2",
+ (AuditInfo) e.auditInfo()));
+
+ // Test schema
+ SchemaEntity schema1 =
+ createSchemaEntity(
+ RandomIdGenerator.INSTANCE.nextId(),
+ Namespace.of("metalake1", "catalog2"),
+ "schema1",
+ auditInfo);
+ SchemaEntity schema2 =
+ createSchemaEntity(
+ RandomIdGenerator.INSTANCE.nextId(),
+ Namespace.of("metalake1", "catalog2"),
+ "schema2",
+ auditInfo);
+
+ store.put(schema1);
+ store.put(schema2);
+
+ store.delete(NameIdentifier.of("metalake1", "catalog2", "schema1"), Entity.EntityType.SCHEMA);
+ store.delete(NameIdentifier.of("metalake1", "catalog2", "schema2"), Entity.EntityType.SCHEMA);
+
+ SchemaEntity schema1New =
+ createSchemaEntity(
+ RandomIdGenerator.INSTANCE.nextId(),
+ schema1.namespace(),
+ schema1.name(),
+ schema1.auditInfo());
+ store.put(schema1New);
+ store.update(
+ schema1New.nameIdentifier(),
+ SchemaEntity.class,
+ Entity.EntityType.SCHEMA,
+ e ->
+ createSchemaEntity(
+ schema1New.id(),
+ Namespace.of("metalake1", "catalog2"),
+ "schema2",
+ e.auditInfo()));
+
+ // Test table
+ TableEntity table1 =
+ createTableEntity(
+ RandomIdGenerator.INSTANCE.nextId(),
+ Namespace.of("metalake1", "catalog2", "schema2"),
+ "table1",
+ auditInfo);
+ TableEntity table2 =
+ createTableEntity(
+ RandomIdGenerator.INSTANCE.nextId(),
+ Namespace.of("metalake1", "catalog2", "schema2"),
+ "table2",
+ auditInfo);
+
+ store.put(table1);
+ store.put(table2);
+
+ store.delete(
+ NameIdentifier.of("metalake1", "catalog2", "schema2", "table1"), Entity.EntityType.TABLE);
+ store.delete(
+ NameIdentifier.of("metalake1", "catalog2", "schema2", "table2"), Entity.EntityType.TABLE);
+
+ TableEntity table1New =
+ createTableEntity(
+ RandomIdGenerator.INSTANCE.nextId(),
+ table1.namespace(),
+ table1.name(),
+ table1.auditInfo());
+ store.put(table1New);
+ store.update(
+ table1New.nameIdentifier(),
+ TableEntity.class,
+ Entity.EntityType.TABLE,
+ e ->
+ createTableEntity(
+ table1New.id(),
+ Namespace.of("metalake1", "catalog2", "schema2"),
+ "table2",
+ e.auditInfo()));
+
+ // Test Fileset
+ FilesetEntity fileset1 =
+ createFilesetEntity(
+ RandomIdGenerator.INSTANCE.nextId(),
+ Namespace.of("metalake1", "catalog2", "schema2"),
+ "fileset1",
+ auditInfo);
+ FilesetEntity fileset2 =
+ createFilesetEntity(
+ RandomIdGenerator.INSTANCE.nextId(),
+ Namespace.of("metalake1", "catalog2", "schema2"),
+ "fileset2",
+ auditInfo);
+
+ store.put(fileset1);
+ store.put(fileset2);
+
+ store.delete(
+ NameIdentifier.of("metalake1", "catalog2", "schema2", "fileset1"),
+ Entity.EntityType.FILESET);
+ store.delete(
+ NameIdentifier.of("metalake1", "catalog2", "schema2", "fileset2"),
+ Entity.EntityType.FILESET);
+
+ FilesetEntity fileset1New =
+ createFilesetEntity(
+ RandomIdGenerator.INSTANCE.nextId(),
+ fileset1.namespace(),
+ fileset1.name(),
+ fileset1.auditInfo());
+ store.put(fileset1New);
+ store.update(
+ fileset1New.nameIdentifier(),
+ FilesetEntity.class,
+ Entity.EntityType.FILESET,
+ e ->
+ createFilesetEntity(
+ fileset1New.id(),
+ Namespace.of("metalake1", "catalog2", "schema2"),
+ "fileset2",
+ e.auditInfo()));
+
+ destroy(type);
+ } catch (InterruptedException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ public static BaseMetalake createBaseMakeLake(Long id, String name, AuditInfo auditInfo) {
+ return BaseMetalake.builder()
+ .withId(id)
+ .withName(name)
+ .withAuditInfo(auditInfo)
+ .withComment("")
+ .withProperties(null)
+ .withVersion(SchemaVersion.V_0_1)
+ .build();
+ }
+
+ public static CatalogEntity createCatalog(
+ Long id, Namespace namespace, String name, AuditInfo auditInfo) {
+ return CatalogEntity.builder()
+ .withId(id)
+ .withName(name)
+ .withNamespace(namespace)
+ .withType(Catalog.Type.RELATIONAL)
+ .withProvider("test")
+ .withComment("")
+ .withProperties(null)
+ .withAuditInfo(auditInfo)
+ .build();
+ }
+
+ public static SchemaEntity createSchemaEntity(
+ Long id, Namespace namespace, String name, AuditInfo auditInfo) {
+ return SchemaEntity.builder()
+ .withId(id)
+ .withName(name)
+ .withNamespace(namespace)
+ .withComment("")
+ .withProperties(null)
+ .withAuditInfo(auditInfo)
+ .build();
+ }
+
+ public static TableEntity createTableEntity(
+ Long id, Namespace namespace, String name, AuditInfo auditInfo) {
+ return TableEntity.builder()
+ .withId(id)
+ .withName(name)
+ .withNamespace(namespace)
+ .withAuditInfo(auditInfo)
+ .build();
+ }
+
+ public static FilesetEntity createFilesetEntity(
+ Long id, Namespace namespace, String name, AuditInfo auditInfo) {
+ return FilesetEntity.builder()
+ .withId(id)
+ .withName(name)
+ .withNamespace(namespace)
+ .withFilesetType(Fileset.Type.MANAGED)
+ .withStorageLocation("/tmp")
+ .withComment("")
+ .withProperties(null)
+ .withAuditInfo(auditInfo)
+ .build();
+ }
+
+ private void validateDeleteFilesetCascade(EntityStore store, FilesetEntity fileset1)
+ throws IOException {
+ // Delete the fileset 'metalake.catalog.schema1.fileset1'
+ Assertions.assertTrue(store.delete(fileset1.nameIdentifier(), Entity.EntityType.FILESET, true));
+ Assertions.assertFalse(store.exists(fileset1.nameIdentifier(), Entity.EntityType.FILESET));
+ }
+
+ private void validateDeleteTableCascade(EntityStore store, TableEntity table1)
+ throws IOException {
+ // Delete the table 'metalake.catalog.schema1.table1'
+ Assertions.assertTrue(store.delete(table1.nameIdentifier(), Entity.EntityType.TABLE, true));
+ Assertions.assertFalse(store.exists(table1.nameIdentifier(), Entity.EntityType.TABLE));
+ }
+
+ private void validateDeleteFileset(
+ EntityStore store,
+ SchemaEntity schema2,
+ FilesetEntity fileset1,
+ FilesetEntity fileset1InSchema2)
+ throws IOException {
+ // Delete the fileset 'metalake.catalog.schema2.fileset1'
+ Assertions.assertTrue(
+ store.delete(fileset1InSchema2.nameIdentifier(), Entity.EntityType.FILESET));
+ Assertions.assertFalse(
+ store.exists(fileset1InSchema2.nameIdentifier(), Entity.EntityType.FILESET));
+
+ // Make sure fileset 'metalake.catalog.schema1.fileset1' still exist;
+ Assertions.assertEquals(
+ fileset1,
+ store.get(fileset1.nameIdentifier(), Entity.EntityType.FILESET, FilesetEntity.class));
+ // Make sure schema 'metalake.catalog.schema2' still exist;
+ Assertions.assertEquals(
+ schema2, store.get(schema2.nameIdentifier(), Entity.EntityType.SCHEMA, SchemaEntity.class));
+ }
+
+ private void validateDeleteMetalakeCascade(
+ EntityStore store, BaseMetalake metalake, CatalogEntity catalog, SchemaEntity schema2)
+ throws IOException {
+ Assertions.assertTrue(
+ store.delete(metalake.nameIdentifier(), Entity.EntityType.METALAKE, true));
+
+ // catalog has already deleted, so we can't delete it again and should return false
+ Assertions.assertFalse(store.exists(catalog.nameIdentifier(), Entity.EntityType.CATALOG));
+ Assertions.assertFalse(store.exists(schema2.nameIdentifier(), Entity.EntityType.SCHEMA));
+ Assertions.assertFalse(store.exists(metalake.nameIdentifier(), Entity.EntityType.METALAKE));
+ }
+
+ private void validateDeleteCatalogCascade(
+ EntityStore store, CatalogEntity catalog, SchemaEntity schema2) throws IOException {
+ Assertions.assertThrowsExactly(
+ NonEmptyEntityException.class,
+ () -> store.delete(catalog.nameIdentifier(), Entity.EntityType.CATALOG));
+ store.delete(catalog.nameIdentifier(), Entity.EntityType.CATALOG, true);
+ NameIdentifier id = catalog.nameIdentifier();
+ Assertions.assertThrowsExactly(
+ NoSuchEntityException.class,
+ () -> store.get(id, Entity.EntityType.CATALOG, CatalogEntity.class));
+
+ Assertions.assertThrowsExactly(
+ NoSuchEntityException.class,
+ () -> store.get(schema2.nameIdentifier(), Entity.EntityType.SCHEMA, SchemaEntity.class));
+ }
+
+ private void validateDeleteSchemaCascade(
+ EntityStore store, SchemaEntity schema1, TableEntity table1, FilesetEntity fileset1)
+ throws IOException {
+ TableEntity table1New =
+ createTableEntity(
+ RandomIdGenerator.INSTANCE.nextId(),
+ table1.namespace(),
+ table1.name(),
+ table1.auditInfo());
+ store.put(table1New);
+ FilesetEntity fileset1New =
+ createFilesetEntity(
+ RandomIdGenerator.INSTANCE.nextId(),
+ fileset1.namespace(),
+ fileset1.name(),
+ fileset1.auditInfo());
+ store.put(fileset1New);
+
+ Assertions.assertThrowsExactly(
+ NonEmptyEntityException.class,
+ () -> store.delete(schema1.nameIdentifier(), Entity.EntityType.SCHEMA));
+
+ Assertions.assertEquals(
+ schema1, store.get(schema1.nameIdentifier(), Entity.EntityType.SCHEMA, SchemaEntity.class));
+
+ // Test cascade delete
+ try {
+ Thread.sleep(1000);
+ } catch (InterruptedException e) {
+ throw new RuntimeException(e);
+ }
+ store.delete(schema1.nameIdentifier(), Entity.EntityType.SCHEMA, true);
+ try {
+ store.get(table1.nameIdentifier(), Entity.EntityType.TABLE, TableEntity.class);
+ } catch (Exception e) {
+ Assertions.assertTrue(e instanceof NoSuchEntityException);
+ Assertions.assertTrue(e.getMessage().contains("schema1"));
+ }
+
+ Assertions.assertThrows(
+ NoSuchEntityException.class,
+ () -> store.get(fileset1.nameIdentifier(), Entity.EntityType.FILESET, FilesetEntity.class));
+ }
+
+ private static void validateDeleteMetalake(
+ EntityStore store, BaseMetalake metalake, CatalogEntity catalogCopy) throws IOException {
+ // Now delete catalog 'catalogCopy' and metalake
+ Assertions.assertThrowsExactly(
+ NonEmptyEntityException.class,
+ () -> store.delete(metalake.nameIdentifier(), Entity.EntityType.METALAKE));
+ store.delete(catalogCopy.nameIdentifier(), Entity.EntityType.CATALOG);
+ Assertions.assertFalse(store.exists(catalogCopy.nameIdentifier(), Entity.EntityType.CATALOG));
+
+ store.delete(metalake.nameIdentifier(), Entity.EntityType.METALAKE);
+ Assertions.assertFalse(store.exists(metalake.nameIdentifier(), Entity.EntityType.METALAKE));
+ }
+
+ private static void validateDeleteCatalog(
+ EntityStore store,
+ CatalogEntity catalog,
+ TableEntity table1,
+ SchemaEntity schema1,
+ TableEntity table1InSchema2,
+ SchemaEntity schema2,
+ FilesetEntity fileset1,
+ FilesetEntity fileset1InSchema2)
+ throws IOException {
+ // Now try to delete all schemas under catalog;
+ Assertions.assertThrowsExactly(
+ NonEmptyEntityException.class,
+ () -> store.delete(catalog.nameIdentifier(), Entity.EntityType.CATALOG));
+ store.delete(table1.nameIdentifier(), Entity.EntityType.TABLE);
+ store.delete(fileset1.nameIdentifier(), Entity.EntityType.FILESET);
+ try {
+ Thread.sleep(1000);
+ } catch (InterruptedException e) {
+ throw new RuntimeException(e);
+ }
+ store.delete(schema1.nameIdentifier(), Entity.EntityType.SCHEMA);
+ store.delete(table1InSchema2.nameIdentifier(), Entity.EntityType.TABLE);
+ Assertions.assertFalse(
+ store.exists(fileset1InSchema2.nameIdentifier(), Entity.EntityType.FILESET));
+ store.delete(schema2.nameIdentifier(), Entity.EntityType.SCHEMA);
+
+ store.delete(catalog.nameIdentifier(), Entity.EntityType.CATALOG);
+ Assertions.assertFalse(store.exists(catalog.nameIdentifier(), Entity.EntityType.CATALOG));
+ }
+
+ private static void validateDeleteSchema(
+ EntityStore store, SchemaEntity schema1, TableEntity table1, FilesetEntity fileset1)
+ throws IOException {
+ // Delete the schema 'metalake.catalog.schema1' but failed, because it ha sub-entities;
+ NonEmptyEntityException exception =
+ Assertions.assertThrowsExactly(
+ NonEmptyEntityException.class,
+ () -> store.delete(schema1.nameIdentifier(), Entity.EntityType.SCHEMA));
+ Assertions.assertTrue(exception.getMessage().contains("metalake.catalog.schema1"));
+ // Make sure schema 'metalake.catalog.schema1' and table 'metalake.catalog.schema1.table1'
+ // and table 'metalake.catalog.schema1.fileset1' has not been deleted yet;
+ Assertions.assertTrue(store.exists(schema1.nameIdentifier(), Entity.EntityType.SCHEMA));
+ Assertions.assertTrue(store.exists(table1.nameIdentifier(), Entity.EntityType.TABLE));
+ Assertions.assertTrue(store.exists(fileset1.nameIdentifier(), Entity.EntityType.FILESET));
+
+ // Delete table1,fileset1 and schema1
+ Assertions.assertTrue(store.delete(table1.nameIdentifier(), Entity.EntityType.TABLE));
+ Assertions.assertTrue(store.delete(fileset1.nameIdentifier(), Entity.EntityType.FILESET));
+ Assertions.assertTrue(store.delete(schema1.nameIdentifier(), Entity.EntityType.SCHEMA));
+ // Make sure table1, fileset1 in 'metalake.catalog.schema1' can't be access;
+ Assertions.assertFalse(store.exists(table1.nameIdentifier(), Entity.EntityType.TABLE));
+ Assertions.assertFalse(store.exists(fileset1.nameIdentifier(), Entity.EntityType.FILESET));
+ Assertions.assertFalse(store.exists(schema1.nameIdentifier(), Entity.EntityType.SCHEMA));
+ // Now we re-insert table1 and schema1, and everything should be OK
+ SchemaEntity schema1New =
+ createSchemaEntity(
+ RandomIdGenerator.INSTANCE.nextId(),
+ schema1.namespace(),
+ schema1.name(),
+ schema1.auditInfo());
+ store.put(schema1New);
+ TableEntity table1New =
+ createTableEntity(
+ RandomIdGenerator.INSTANCE.nextId(),
+ table1.namespace(),
+ table1.name(),
+ table1.auditInfo());
+ store.put(table1New);
+ FilesetEntity fileset1New =
+ createFilesetEntity(
+ RandomIdGenerator.INSTANCE.nextId(),
+ fileset1.namespace(),
+ fileset1.name(),
+ fileset1.auditInfo());
+ store.put(fileset1New);
+ Assertions.assertEquals(
+ schema1New,
+ store.get(schema1.nameIdentifier(), Entity.EntityType.SCHEMA, SchemaEntity.class));
+ Assertions.assertEquals(
+ table1New, store.get(table1.nameIdentifier(), Entity.EntityType.TABLE, TableEntity.class));
+ Assertions.assertEquals(
+ fileset1New,
+ store.get(fileset1.nameIdentifier(), Entity.EntityType.FILESET, FilesetEntity.class));
+ }
+
+ private void validateDeleteTable(
+ EntityStore store, SchemaEntity schema2, TableEntity table1, TableEntity table1InSchema2)
+ throws IOException {
+ // Delete the table 'metalake.catalog.schema2.table1'
+ Assertions.assertTrue(store.delete(table1InSchema2.nameIdentifier(), Entity.EntityType.TABLE));
+ Assertions.assertFalse(store.exists(table1InSchema2.nameIdentifier(), Entity.EntityType.TABLE));
+
+ // Make sure table 'metalake.catalog.schema1.table1' still exist;
+ Assertions.assertEquals(
+ table1, store.get(table1.nameIdentifier(), Entity.EntityType.TABLE, TableEntity.class));
+ // Make sure schema 'metalake.catalog.schema2' still exist;
+ Assertions.assertEquals(
+ schema2, store.get(schema2.nameIdentifier(), Entity.EntityType.SCHEMA, SchemaEntity.class));
+ // Re-insert table1Inschema2 and everything is OK
+ TableEntity table1InSchema2New =
+ createTableEntity(
+ RandomIdGenerator.INSTANCE.nextId(),
+ table1InSchema2.namespace(),
+ table1InSchema2.name(),
+ table1InSchema2.auditInfo());
+ store.put(table1InSchema2New);
+ Assertions.assertTrue(store.exists(table1InSchema2.nameIdentifier(), Entity.EntityType.TABLE));
+ }
+
+ private static void validateAllEntityExist(
+ BaseMetalake metalake,
+ EntityStore store,
+ CatalogEntity catalog,
+ CatalogEntity catalogCopy,
+ SchemaEntity schema1,
+ SchemaEntity schema2,
+ TableEntity table1,
+ TableEntity table1InSchema2,
+ FilesetEntity fileset1,
+ FilesetEntity fileset1Inschema2)
+ throws IOException {
+ // Now try to get
+ Assertions.assertEquals(
+ metalake,
+ store.get(metalake.nameIdentifier(), Entity.EntityType.METALAKE, BaseMetalake.class));
+ Assertions.assertEquals(
+ catalog,
+ store.get(catalog.nameIdentifier(), Entity.EntityType.CATALOG, CatalogEntity.class));
+ Assertions.assertEquals(
+ catalogCopy,
+ store.get(catalogCopy.nameIdentifier(), Entity.EntityType.CATALOG, CatalogEntity.class));
+ Assertions.assertEquals(
+ schema1, store.get(schema1.nameIdentifier(), Entity.EntityType.SCHEMA, SchemaEntity.class));
+ Assertions.assertEquals(
+ schema2, store.get(schema2.nameIdentifier(), Entity.EntityType.SCHEMA, SchemaEntity.class));
+ Assertions.assertEquals(
+ table1, store.get(table1.nameIdentifier(), Entity.EntityType.TABLE, TableEntity.class));
+ Assertions.assertEquals(
+ table1InSchema2,
+ store.get(table1InSchema2.nameIdentifier(), Entity.EntityType.TABLE, TableEntity.class));
+ Assertions.assertEquals(
+ fileset1,
+ store.get(fileset1.nameIdentifier(), Entity.EntityType.FILESET, FilesetEntity.class));
+ Assertions.assertEquals(
+ fileset1Inschema2,
+ store.get(
+ fileset1Inschema2.nameIdentifier(), Entity.EntityType.FILESET, FilesetEntity.class));
+ }
+
+ private void validateDeletedFileset(EntityStore store) throws IOException {
+ store.delete(
+ NameIdentifier.of("metalakeChanged", "catalogChanged", "schema2", "fileset1"),
+ Entity.EntityType.FILESET);
+ // Update a deleted entities
+ Assertions.assertThrowsExactly(
+ NoSuchEntityException.class,
+ () ->
+ store.update(
+ NameIdentifier.of("metalakeChanged", "catalogChanged", "schema2", "fileset1"),
+ FilesetEntity.class,
+ Entity.EntityType.FILESET,
+ (e) -> e));
+ }
+
+ private void validateFilesetChanged(EntityStore store, FilesetEntity filesetEntity)
+ throws IOException {
+ // Check fileset entities
+ store.update(
+ NameIdentifier.of("metalakeChanged", "catalogChanged", "schemaChanged", "fileset1"),
+ FilesetEntity.class,
+ Entity.EntityType.FILESET,
+ e -> {
+ AuditInfo auditInfo1 =
+ AuditInfo.builder().withCreator("creator5").withCreateTime(Instant.now()).build();
+ return createFilesetEntity(
+ filesetEntity.id(),
+ Namespace.of("metalakeChanged", "catalogChanged", "schemaChanged"),
+ "filesetChanged",
+ auditInfo1);
+ });
+
+ Assertions.assertThrowsExactly(
+ NoSuchEntityException.class,
+ () ->
+ store.get(
+ NameIdentifier.of("metalakeChanged", "catalogChanged", "schema1", "fileset1"),
+ Entity.EntityType.FILESET,
+ FilesetEntity.class));
+ FilesetEntity updatedFileset =
+ store.get(
+ NameIdentifier.of(
+ "metalakeChanged", "catalogChanged", "schemaChanged", "filesetChanged"),
+ Entity.EntityType.FILESET,
+ FilesetEntity.class);
+ Assertions.assertEquals("creator5", updatedFileset.auditInfo().creator());
+
+ Assertions.assertNotNull(
+ store.get(
+ NameIdentifier.of("metalakeChanged", "catalogChanged", "schema2", "fileset1"),
+ Entity.EntityType.FILESET,
+ FilesetEntity.class));
+ }
+
+ private void validateNotChangedEntity(EntityStore store, SchemaEntity schema) throws IOException {
+ // Update operations do not contain any changes in name
+ store.update(
+ NameIdentifier.of("metalakeChanged", "catalogChanged", "schema2"),
+ SchemaEntity.class,
+ Entity.EntityType.SCHEMA,
+ e -> {
+ AuditInfo auditInfo1 =
+ AuditInfo.builder().withCreator("creator6").withCreateTime(Instant.now()).build();
+ return createSchemaEntity(
+ schema.id(),
+ Namespace.of("metalakeChanged", "catalogChanged"),
+ "schema2",
+ auditInfo1);
+ });
+ Assertions.assertEquals(
+ "creator6",
+ store
+ .get(
+ NameIdentifier.of("metalakeChanged", "catalogChanged", "schema2"),
+ Entity.EntityType.SCHEMA,
+ SchemaEntity.class)
+ .auditInfo()
+ .creator());
+ }
+
+ private void validateAlreadyExistEntity(EntityStore store, SchemaEntity schema) {
+ // The updated entities already existed, should throw exception
+ Assertions.assertThrowsExactly(
+ AlreadyExistsException.class,
+ () ->
+ store.update(
+ NameIdentifier.of("metalakeChanged", "catalogChanged", "schema2"),
+ SchemaEntity.class,
+ Entity.EntityType.SCHEMA,
+ e -> {
+ AuditInfo auditInfo1 =
+ AuditInfo.builder()
+ .withCreator("creator5")
+ .withCreateTime(Instant.now())
+ .build();
+ return createSchemaEntity(
+ schema.id(),
+ Namespace.of("metalakeChanged", "catalogChanged"),
+ "schemaChanged",
+ auditInfo1);
+ }));
+ }
+
+ private void validateMetalakeChanged(EntityStore store, BaseMetalake metalake)
+ throws IOException {
+ // Try to check an update option is what we expected
+ store.update(
+ metalake.nameIdentifier(),
+ BaseMetalake.class,
+ Entity.EntityType.METALAKE,
+ e -> {
+ AuditInfo auditInfo1 =
+ AuditInfo.builder().withCreator("creator1").withCreateTime(Instant.now()).build();
+ return createBaseMakeLake(metalake.id(), "metalakeChanged", auditInfo1);
+ });
+
+ // Check metalake entity and sub-entities are already changed.
+ BaseMetalake updatedMetalake =
+ store.get(
+ NameIdentifier.of("metalakeChanged"), Entity.EntityType.METALAKE, BaseMetalake.class);
+ Assertions.assertEquals("creator1", updatedMetalake.auditInfo().creator());
+
+ Assertions.assertThrowsExactly(
+ NoSuchEntityException.class,
+ () ->
+ store.get(
+ NameIdentifier.of("metalake"), Entity.EntityType.METALAKE, BaseMetalake.class));
+ Assertions.assertNotNull(
+ store.get(
+ NameIdentifier.of("metalakeChanged", "catalog"),
+ Entity.EntityType.CATALOG,
+ CatalogEntity.class));
+ Assertions.assertNotNull(
+ store.get(
+ NameIdentifier.of("metalakeChanged", "catalog", "schema1"),
+ Entity.EntityType.SCHEMA,
+ SchemaEntity.class));
+ Assertions.assertNotNull(
+ store.get(
+ NameIdentifier.of("metalakeChanged", "catalog", "schema1", "table1"),
+ Entity.EntityType.TABLE,
+ TableEntity.class));
+ Assertions.assertNotNull(
+ store.get(
+ NameIdentifier.of("metalakeChanged", "catalog", "schema1", "fileset1"),
+ Entity.EntityType.FILESET,
+ FilesetEntity.class));
+ Assertions.assertNotNull(
+ store.get(
+ NameIdentifier.of("metalakeChanged", "catalog", "schema2"),
+ Entity.EntityType.SCHEMA,
+ SchemaEntity.class));
+ Assertions.assertNotNull(
+ store.get(
+ NameIdentifier.of("metalakeChanged", "catalog", "schema2", "table1"),
+ Entity.EntityType.TABLE,
+ TableEntity.class));
+ Assertions.assertNotNull(
+ store.get(
+ NameIdentifier.of("metalakeChanged", "catalog", "schema2", "fileset1"),
+ Entity.EntityType.FILESET,
+ FilesetEntity.class));
+ }
+
+ private void validateCatalogChanged(EntityStore store, CatalogEntity catalog) throws IOException {
+ // Check catalog entities and sub-entities are already changed.
+ store.update(
+ NameIdentifier.of("metalakeChanged", "catalog"),
+ CatalogEntity.class,
+ Entity.EntityType.CATALOG,
+ e -> {
+ AuditInfo auditInfo1 =
+ AuditInfo.builder().withCreator("creator2").withCreateTime(Instant.now()).build();
+ return createCatalog(
+ catalog.id(), Namespace.of("metalakeChanged"), "catalogChanged", auditInfo1);
+ });
+ CatalogEntity updatedCatalog =
+ store.get(
+ NameIdentifier.of("metalakeChanged", "catalogChanged"),
+ Entity.EntityType.CATALOG,
+ CatalogEntity.class);
+ Assertions.assertEquals("creator2", updatedCatalog.auditInfo().creator());
+ Assertions.assertThrowsExactly(
+ NoSuchEntityException.class,
+ () ->
+ store.get(
+ NameIdentifier.of("metalakeChanged", "catalog"),
+ Entity.EntityType.CATALOG,
+ CatalogEntity.class));
+ Assertions.assertNotNull(
+ store.get(
+ NameIdentifier.of("metalakeChanged", "catalogChanged", "schema1"),
+ Entity.EntityType.SCHEMA,
+ SchemaEntity.class));
+ Assertions.assertNotNull(
+ store.get(
+ NameIdentifier.of("metalakeChanged", "catalogChanged", "schema1", "table1"),
+ Entity.EntityType.TABLE,
+ TableEntity.class));
+ Assertions.assertNotNull(
+ store.get(
+ NameIdentifier.of("metalakeChanged", "catalogChanged", "schema1", "fileset1"),
+ Entity.EntityType.FILESET,
+ FilesetEntity.class));
+ Assertions.assertNotNull(
+ store.get(
+ NameIdentifier.of("metalakeChanged", "catalogChanged", "schema2"),
+ Entity.EntityType.SCHEMA,
+ SchemaEntity.class));
+ Assertions.assertNotNull(
+ store.get(
+ NameIdentifier.of("metalakeChanged", "catalogChanged", "schema2", "table1"),
+ Entity.EntityType.TABLE,
+ TableEntity.class));
+ Assertions.assertNotNull(
+ store.get(
+ NameIdentifier.of("metalakeChanged", "catalogChanged", "schema2", "fileset1"),
+ Entity.EntityType.FILESET,
+ FilesetEntity.class));
+ }
+
+ private void validateSchemaChanged(EntityStore store, SchemaEntity schema) throws IOException {
+ // Check schema entities and sub-entities are already changed.
+ store.update(
+ NameIdentifier.of("metalakeChanged", "catalogChanged", "schema1"),
+ SchemaEntity.class,
+ Entity.EntityType.SCHEMA,
+ e -> {
+ AuditInfo auditInfo1 =
+ AuditInfo.builder().withCreator("creator3").withCreateTime(Instant.now()).build();
+ return createSchemaEntity(
+ schema.id(),
+ Namespace.of("metalakeChanged", "catalogChanged"),
+ "schemaChanged",
+ auditInfo1);
+ });
+
+ Assertions.assertThrowsExactly(
+ NoSuchEntityException.class,
+ () ->
+ store.get(
+ NameIdentifier.of("metalakeChanged", "catalogChanged", "schema1"),
+ Entity.EntityType.SCHEMA,
+ SchemaEntity.class));
+ SchemaEntity updatedSchema =
+ store.get(
+ NameIdentifier.of("metalakeChanged", "catalogChanged", "schemaChanged"),
+ Entity.EntityType.SCHEMA,
+ SchemaEntity.class);
+ Assertions.assertEquals("creator3", updatedSchema.auditInfo().creator());
+
+ Assertions.assertNotNull(
+ store.get(
+ NameIdentifier.of("metalakeChanged", "catalogChanged", "schemaChanged"),
+ Entity.EntityType.SCHEMA,
+ SchemaEntity.class));
+ Assertions.assertNotNull(
+ store.get(
+ NameIdentifier.of("metalakeChanged", "catalogChanged", "schemaChanged", "table1"),
+ Entity.EntityType.TABLE,
+ TableEntity.class));
+ Assertions.assertNotNull(
+ store.get(
+ NameIdentifier.of("metalakeChanged", "catalogChanged", "schemaChanged", "fileset1"),
+ Entity.EntityType.FILESET,
+ FilesetEntity.class));
+ Assertions.assertNotNull(
+ store.get(
+ NameIdentifier.of("metalakeChanged", "catalogChanged", "schema2"),
+ Entity.EntityType.SCHEMA,
+ SchemaEntity.class));
+ Assertions.assertNotNull(
+ store.get(
+ NameIdentifier.of("metalakeChanged", "catalogChanged", "schema2", "table1"),
+ Entity.EntityType.TABLE,
+ TableEntity.class));
+ Assertions.assertNotNull(
+ store.get(
+ NameIdentifier.of("metalakeChanged", "catalogChanged", "schema2", "fileset1"),
+ Entity.EntityType.FILESET,
+ FilesetEntity.class));
+ }
+
+ private void validateTableChanged(EntityStore store, TableEntity table) throws IOException {
+ // Check table entities
+ store.update(
+ NameIdentifier.of("metalakeChanged", "catalogChanged", "schemaChanged", "table1"),
+ TableEntity.class,
+ Entity.EntityType.TABLE,
+ e -> {
+ AuditInfo auditInfo1 =
+ AuditInfo.builder().withCreator("creator4").withCreateTime(Instant.now()).build();
+ return createTableEntity(
+ table.id(),
+ Namespace.of("metalakeChanged", "catalogChanged", "schemaChanged"),
+ "tableChanged",
+ auditInfo1);
+ });
+
+ Assertions.assertThrowsExactly(
+ NoSuchEntityException.class,
+ () ->
+ store.get(
+ NameIdentifier.of("metalakeChanged", "catalogChanged", "schema1", "table1"),
+ Entity.EntityType.TABLE,
+ TableEntity.class));
+ TableEntity updatedTable =
+ store.get(
+ NameIdentifier.of("metalakeChanged", "catalogChanged", "schemaChanged", "tableChanged"),
+ Entity.EntityType.TABLE,
+ TableEntity.class);
+ Assertions.assertEquals("creator4", updatedTable.auditInfo().creator());
+
+ Assertions.assertNotNull(
+ store.get(
+ NameIdentifier.of("metalakeChanged", "catalogChanged", "schema2", "table1"),
+ Entity.EntityType.TABLE,
+ TableEntity.class));
+ }
+
+ private void validateDeletedTable(EntityStore store) throws IOException {
+ store.delete(
+ NameIdentifier.of("metalakeChanged", "catalogChanged", "schema2", "table1"),
+ Entity.EntityType.TABLE);
+ // Update a deleted entities
+ Assertions.assertThrowsExactly(
+ NoSuchEntityException.class,
+ () ->
+ store.update(
+ NameIdentifier.of("metalakeChanged", "catalogChanged", "schema2", "table1"),
+ TableEntity.class,
+ Entity.EntityType.TABLE,
+ (e) -> e));
+ }
+}
diff --git a/core/src/test/java/com/datastrato/gravitino/storage/TestStorageVersion.java b/core/src/test/java/com/datastrato/gravitino/storage/TestStorageVersion.java
deleted file mode 100644
index 8f274cf505..0000000000
--- a/core/src/test/java/com/datastrato/gravitino/storage/TestStorageVersion.java
+++ /dev/null
@@ -1,21 +0,0 @@
-/*
- * Copyright 2023 Datastrato Pvt Ltd.
- * This software is licensed under the Apache License version 2.
- */
-
-package com.datastrato.gravitino.storage;
-
-import org.junit.jupiter.api.Assertions;
-import org.junit.jupiter.api.Test;
-
-class TestStorageVersion {
-
- @Test
- void testFromString() {
- StorageLayoutVersion version = StorageLayoutVersion.fromString("v1");
- Assertions.assertEquals(StorageLayoutVersion.V1, version);
-
- Assertions.assertThrowsExactly(
- StorageLayoutException.class, () -> StorageLayoutVersion.fromString("v200000.0"));
- }
-}
diff --git a/core/src/test/java/com/datastrato/gravitino/storage/kv/TestKvEntityStorage.java b/core/src/test/java/com/datastrato/gravitino/storage/kv/TestKvEntityStorage.java
index 818f6083b6..c937400a1c 100644
--- a/core/src/test/java/com/datastrato/gravitino/storage/kv/TestKvEntityStorage.java
+++ b/core/src/test/java/com/datastrato/gravitino/storage/kv/TestKvEntityStorage.java
@@ -12,10 +12,9 @@ import static com.datastrato.gravitino.Configs.ENTRY_KV_ROCKSDB_BACKEND_PATH;
import static com.datastrato.gravitino.Configs.KV_DELETE_AFTER_TIME;
import static com.datastrato.gravitino.Configs.STORE_TRANSACTION_MAX_SKEW_TIME;
-import com.datastrato.gravitino.Catalog.Type;
import com.datastrato.gravitino.Config;
import com.datastrato.gravitino.Configs;
-import com.datastrato.gravitino.Entity.EntityType;
+import com.datastrato.gravitino.Entity;
import com.datastrato.gravitino.EntityAlreadyExistsException;
import com.datastrato.gravitino.EntitySerDeFactory;
import com.datastrato.gravitino.EntityStore;
@@ -26,22 +25,16 @@ import com.datastrato.gravitino.Namespace;
import com.datastrato.gravitino.exceptions.AlreadyExistsException;
import com.datastrato.gravitino.exceptions.NoSuchEntityException;
import com.datastrato.gravitino.exceptions.NonEmptyEntityException;
-import com.datastrato.gravitino.file.Fileset;
import com.datastrato.gravitino.meta.AuditInfo;
import com.datastrato.gravitino.meta.BaseMetalake;
import com.datastrato.gravitino.meta.CatalogEntity;
-import com.datastrato.gravitino.meta.FilesetEntity;
-import com.datastrato.gravitino.meta.SchemaEntity;
-import com.datastrato.gravitino.meta.SchemaVersion;
-import com.datastrato.gravitino.meta.TableEntity;
-import com.datastrato.gravitino.storage.StorageLayoutVersion;
+import com.datastrato.gravitino.storage.TestEntityStorage;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.time.Instant;
import java.util.List;
-import java.util.UUID;
import java.util.concurrent.CompletionService;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorCompletionService;
@@ -56,868 +49,17 @@ import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
-public class TestKvEntityStorage {
- public static final String ROCKS_DB_STORE_PATH =
- "/tmp/gravitino_test_entityStore_" + UUID.randomUUID().toString().replace("-", "");
-
+public class TestKvEntityStorage extends TestEntityStorage {
@BeforeEach
@AfterEach
public void cleanEnv() {
try {
- FileUtils.deleteDirectory(FileUtils.getFile(ROCKS_DB_STORE_PATH));
+ FileUtils.deleteDirectory(FileUtils.getFile(KV_STORE_PATH));
} catch (Exception e) {
// Ignore
}
}
- public static BaseMetalake createBaseMakeLake(String name, AuditInfo auditInfo) {
- return BaseMetalake.builder()
- .withId(1L)
- .withName(name)
- .withAuditInfo(auditInfo)
- .withVersion(SchemaVersion.V_0_1)
- .build();
- }
-
- public static CatalogEntity createCatalog(Namespace namespace, String name, AuditInfo auditInfo) {
- return CatalogEntity.builder()
- .withId(1L)
- .withName(name)
- .withNamespace(namespace)
- .withType(Type.RELATIONAL)
- .withProvider("test")
- .withAuditInfo(auditInfo)
- .build();
- }
-
- public static SchemaEntity createSchemaEntity(
- Namespace namespace, String name, AuditInfo auditInfo) {
- return SchemaEntity.builder()
- .withId(1L)
- .withName(name)
- .withNamespace(namespace)
- .withAuditInfo(auditInfo)
- .build();
- }
-
- public static TableEntity createTableEntity(
- Namespace namespace, String name, AuditInfo auditInfo) {
- return TableEntity.builder()
- .withId(1L)
- .withName(name)
- .withNamespace(namespace)
- .withAuditInfo(auditInfo)
- .build();
- }
-
- public static FilesetEntity createFilesetEntity(
- Namespace namespace, String name, AuditInfo auditInfo) {
- return FilesetEntity.builder()
- .withId(1L)
- .withName(name)
- .withNamespace(namespace)
- .withFilesetType(Fileset.Type.MANAGED)
- .withStorageLocation("/tmp")
- .withAuditInfo(auditInfo)
- .build();
- }
-
- @Test
- void testRestart() throws IOException {
- Config config = Mockito.mock(Config.class);
- Mockito.when(config.get(ENTITY_STORE)).thenReturn("kv");
- Mockito.when(config.get(ENTITY_KV_STORE)).thenReturn(DEFAULT_ENTITY_KV_STORE);
- Mockito.when(config.get(Configs.ENTITY_SERDE)).thenReturn("proto");
- Mockito.when(config.get(ENTRY_KV_ROCKSDB_BACKEND_PATH)).thenReturn(ROCKS_DB_STORE_PATH);
-
- Assertions.assertEquals(ROCKS_DB_STORE_PATH, config.get(ENTRY_KV_ROCKSDB_BACKEND_PATH));
- Mockito.when(config.get(STORE_TRANSACTION_MAX_SKEW_TIME)).thenReturn(1000L);
- Mockito.when(config.get(KV_DELETE_AFTER_TIME)).thenReturn(20 * 60 * 1000L);
-
- AuditInfo auditInfo =
- AuditInfo.builder().withCreator("creator").withCreateTime(Instant.now()).build();
-
- try (EntityStore store = EntityStoreFactory.createEntityStore(config)) {
- store.initialize(config);
- Assertions.assertTrue(store instanceof KvEntityStore);
- store.setSerDe(EntitySerDeFactory.createEntitySerDe(config.get(Configs.ENTITY_SERDE)));
-
- BaseMetalake metalake = createBaseMakeLake("metalake", auditInfo);
- CatalogEntity catalog = createCatalog(Namespace.of("metalake"), "catalog", auditInfo);
- CatalogEntity catalogCopy = createCatalog(Namespace.of("metalake"), "catalogCopy", auditInfo);
-
- SchemaEntity schema1 =
- createSchemaEntity(Namespace.of("metalake", "catalog"), "schema1", auditInfo);
- TableEntity table1 =
- createTableEntity(Namespace.of("metalake", "catalog", "schema1"), "table1", auditInfo);
- FilesetEntity fileset1 =
- createFilesetEntity(
- Namespace.of("metalake", "catalog", "schema1"), "fileset1", auditInfo);
-
- // Store all entities
- store.put(metalake);
- store.put(catalog);
- store.put(catalogCopy);
- store.put(schema1);
- store.put(table1);
- store.put(fileset1);
-
- Assertions.assertDoesNotThrow(
- () -> store.get(NameIdentifier.of("metalake"), EntityType.METALAKE, BaseMetalake.class));
- Assertions.assertDoesNotThrow(
- () ->
- store.get(
- NameIdentifier.of("metalake", "catalog"),
- EntityType.CATALOG,
- CatalogEntity.class));
- Assertions.assertDoesNotThrow(
- () ->
- store.get(
- NameIdentifier.of("metalake", "catalog", "schema1"),
- EntityType.SCHEMA,
- SchemaEntity.class));
- Assertions.assertDoesNotThrow(
- () ->
- store.get(
- NameIdentifier.of("metalake", "catalog", "schema1", "table1"),
- EntityType.TABLE,
- TableEntity.class));
- Assertions.assertDoesNotThrow(
- () ->
- store.get(
- NameIdentifier.of("metalake", "catalog", "schema1", "fileset1"),
- EntityType.FILESET,
- FilesetEntity.class));
- }
-
- // It will automatically close the store we create before, then we reopen the entity store
- try (EntityStore store = EntityStoreFactory.createEntityStore(config)) {
- store.initialize(config);
- Assertions.assertTrue(store instanceof KvEntityStore);
- store.setSerDe(EntitySerDeFactory.createEntitySerDe(config.get(Configs.ENTITY_SERDE)));
-
- Assertions.assertDoesNotThrow(
- () -> store.get(NameIdentifier.of("metalake"), EntityType.METALAKE, BaseMetalake.class));
- Assertions.assertDoesNotThrow(
- () ->
- store.get(
- NameIdentifier.of("metalake", "catalog"),
- EntityType.CATALOG,
- CatalogEntity.class));
- Assertions.assertDoesNotThrow(
- () ->
- store.get(
- NameIdentifier.of("metalake", "catalog", "schema1"),
- EntityType.SCHEMA,
- SchemaEntity.class));
- Assertions.assertDoesNotThrow(
- () ->
- store.get(
- NameIdentifier.of("metalake", "catalog", "schema1", "table1"),
- EntityType.TABLE,
- TableEntity.class));
- Assertions.assertDoesNotThrow(
- () ->
- store.get(
- NameIdentifier.of("metalake", "catalog", "schema1", "fileset1"),
- EntityType.FILESET,
- FilesetEntity.class));
- }
- }
-
- @Test
- void testEntityUpdate() throws Exception {
- Config config = Mockito.mock(Config.class);
- Mockito.when(config.get(ENTITY_STORE)).thenReturn("kv");
- Mockito.when(config.get(ENTITY_KV_STORE)).thenReturn(DEFAULT_ENTITY_KV_STORE);
- Mockito.when(config.get(Configs.ENTITY_SERDE)).thenReturn("proto");
- Mockito.when(config.get(ENTRY_KV_ROCKSDB_BACKEND_PATH)).thenReturn(ROCKS_DB_STORE_PATH);
- Mockito.when(config.get(STORE_TRANSACTION_MAX_SKEW_TIME)).thenReturn(1000L);
- Mockito.when(config.get(KV_DELETE_AFTER_TIME)).thenReturn(20 * 60 * 1000L);
-
- Assertions.assertEquals(ROCKS_DB_STORE_PATH, config.get(ENTRY_KV_ROCKSDB_BACKEND_PATH));
- AuditInfo auditInfo =
- AuditInfo.builder().withCreator("creator").withCreateTime(Instant.now()).build();
-
- try (EntityStore store = EntityStoreFactory.createEntityStore(config)) {
- store.initialize(config);
- Assertions.assertTrue(store instanceof KvEntityStore);
- store.setSerDe(EntitySerDeFactory.createEntitySerDe(config.get(Configs.ENTITY_SERDE)));
-
- BaseMetalake metalake = createBaseMakeLake("metalake", auditInfo);
- CatalogEntity catalog = createCatalog(Namespace.of("metalake"), "catalog", auditInfo);
- CatalogEntity catalogCopy = createCatalog(Namespace.of("metalake"), "catalogCopy", auditInfo);
-
- SchemaEntity schema1 =
- createSchemaEntity(Namespace.of("metalake", "catalog"), "schema1", auditInfo);
- TableEntity table1 =
- createTableEntity(Namespace.of("metalake", "catalog", "schema1"), "table1", auditInfo);
- FilesetEntity fileset1 =
- createFilesetEntity(
- Namespace.of("metalake", "catalog", "schema1"), "fileset1", auditInfo);
-
- SchemaEntity schema2 =
- createSchemaEntity(Namespace.of("metalake", "catalog"), "schema2", auditInfo);
- TableEntity table1InSchema2 =
- createTableEntity(Namespace.of("metalake", "catalog", "schema2"), "table1", auditInfo);
- FilesetEntity fileset1InSchema2 =
- createFilesetEntity(
- Namespace.of("metalake", "catalog", "schema2"), "fileset1", auditInfo);
-
- // Store all entities
- store.put(metalake);
- store.put(catalog);
- store.put(catalogCopy);
- store.put(schema1);
- store.put(schema2);
- store.put(table1);
- store.put(table1InSchema2);
- store.put(fileset1);
- store.put(fileset1InSchema2);
-
- validateMetalakeChanged(store, metalake);
- validateCatalogChanged(store);
- validateSchemaChanged(store);
- validateTableChanged(store);
- validateFilesetChanged(store);
- validateDeletedTable(store);
- validateDeletedFileset(store);
- validateAlreadyExistEntity(store);
- validateNotChangedEntity(store);
- }
- }
-
- private void validateDeletedFileset(EntityStore store) throws IOException {
- store.delete(
- NameIdentifier.of("metalakeChanged", "catalogChanged", "schema2", "fileset1"),
- EntityType.FILESET);
- // Update a deleted entities
- Assertions.assertThrowsExactly(
- NoSuchEntityException.class,
- () ->
- store.update(
- NameIdentifier.of("metalakeChanged", "catalogChanged", "schema2", "fileset1"),
- FilesetEntity.class,
- EntityType.FILESET,
- (e) -> e));
- }
-
- private void validateFilesetChanged(EntityStore store) throws IOException {
- // Check fileset entities
- store.update(
- NameIdentifier.of("metalakeChanged", "catalogChanged", "schemaChanged", "fileset1"),
- FilesetEntity.class,
- EntityType.FILESET,
- e -> {
- AuditInfo auditInfo1 =
- AuditInfo.builder().withCreator("creator5").withCreateTime(Instant.now()).build();
- return createFilesetEntity(
- Namespace.of("metalakeChanged", "catalogChanged", "schemaChanged"),
- "filesetChanged",
- auditInfo1);
- });
-
- Assertions.assertThrowsExactly(
- NoSuchEntityException.class,
- () ->
- store.get(
- NameIdentifier.of("metalakeChanged", "catalogChanged", "schema1", "fileset1"),
- EntityType.FILESET,
- FilesetEntity.class));
- FilesetEntity updatedFileset =
- store.get(
- NameIdentifier.of(
- "metalakeChanged", "catalogChanged", "schemaChanged", "filesetChanged"),
- EntityType.FILESET,
- FilesetEntity.class);
- Assertions.assertEquals("creator5", updatedFileset.auditInfo().creator());
-
- Assertions.assertNotNull(
- store.get(
- NameIdentifier.of("metalakeChanged", "catalogChanged", "schema2", "fileset1"),
- EntityType.FILESET,
- FilesetEntity.class));
- }
-
- private void validateNotChangedEntity(EntityStore store) throws IOException {
- // Update operations do not contain any changes in name
- store.update(
- NameIdentifier.of("metalakeChanged", "catalogChanged", "schema2"),
- SchemaEntity.class,
- EntityType.SCHEMA,
- e -> {
- AuditInfo auditInfo1 =
- AuditInfo.builder().withCreator("creator6").withCreateTime(Instant.now()).build();
- return createSchemaEntity(
- Namespace.of("metalakeChanged", "catalogChanged"), "schema2", auditInfo1);
- });
- Assertions.assertEquals(
- "creator6",
- store
- .get(
- NameIdentifier.of("metalakeChanged", "catalogChanged", "schema2"),
- EntityType.SCHEMA,
- SchemaEntity.class)
- .auditInfo()
- .creator());
- }
-
- private void validateAlreadyExistEntity(EntityStore store) {
- // The updated entities already existed, should throw exception
- Assertions.assertThrowsExactly(
- AlreadyExistsException.class,
- () ->
- store.update(
- NameIdentifier.of("metalakeChanged", "catalogChanged", "schema2"),
- SchemaEntity.class,
- EntityType.SCHEMA,
- e -> {
- AuditInfo auditInfo1 =
- AuditInfo.builder()
- .withCreator("creator5")
- .withCreateTime(Instant.now())
- .build();
- return createSchemaEntity(
- Namespace.of("metalakeChanged", "catalogChanged"),
- "schemaChanged",
- auditInfo1);
- }));
- }
-
- private void validateMetalakeChanged(EntityStore store, BaseMetalake metalake)
- throws IOException {
- // Try to check an update option is what we expected
- store.update(
- metalake.nameIdentifier(),
- BaseMetalake.class,
- EntityType.METALAKE,
- e -> {
- AuditInfo auditInfo1 =
- AuditInfo.builder().withCreator("creator1").withCreateTime(Instant.now()).build();
- return createBaseMakeLake("metalakeChanged", auditInfo1);
- });
-
- // Check metalake entity and sub-entities are already changed.
- BaseMetalake updatedMetalake =
- store.get(NameIdentifier.of("metalakeChanged"), EntityType.METALAKE, BaseMetalake.class);
- Assertions.assertEquals("creator1", updatedMetalake.auditInfo().creator());
-
- Assertions.assertThrowsExactly(
- NoSuchEntityException.class,
- () -> store.get(NameIdentifier.of("metalake"), EntityType.METALAKE, BaseMetalake.class));
- Assertions.assertNotNull(
- store.get(
- NameIdentifier.of("metalakeChanged", "catalog"),
- EntityType.CATALOG,
- CatalogEntity.class));
- Assertions.assertNotNull(
- store.get(
- NameIdentifier.of("metalakeChanged", "catalog", "schema1"),
- EntityType.SCHEMA,
- SchemaEntity.class));
- Assertions.assertNotNull(
- store.get(
- NameIdentifier.of("metalakeChanged", "catalog", "schema1", "table1"),
- EntityType.TABLE,
- TableEntity.class));
- Assertions.assertNotNull(
- store.get(
- NameIdentifier.of("metalakeChanged", "catalog", "schema1", "fileset1"),
- EntityType.FILESET,
- FilesetEntity.class));
- Assertions.assertNotNull(
- store.get(
- NameIdentifier.of("metalakeChanged", "catalog", "schema2"),
- EntityType.SCHEMA,
- SchemaEntity.class));
- Assertions.assertNotNull(
- store.get(
- NameIdentifier.of("metalakeChanged", "catalog", "schema2", "table1"),
- EntityType.TABLE,
- TableEntity.class));
- Assertions.assertNotNull(
- store.get(
- NameIdentifier.of("metalakeChanged", "catalog", "schema2", "fileset1"),
- EntityType.FILESET,
- FilesetEntity.class));
- }
-
- private void validateCatalogChanged(EntityStore store) throws IOException {
- // Check catalog entities and sub-entities are already changed.
- store.update(
- NameIdentifier.of("metalakeChanged", "catalog"),
- CatalogEntity.class,
- EntityType.CATALOG,
- e -> {
- AuditInfo auditInfo1 =
- AuditInfo.builder().withCreator("creator2").withCreateTime(Instant.now()).build();
- return createCatalog(Namespace.of("metalakeChanged"), "catalogChanged", auditInfo1);
- });
- CatalogEntity updatedCatalog =
- store.get(
- NameIdentifier.of("metalakeChanged", "catalogChanged"),
- EntityType.CATALOG,
- CatalogEntity.class);
- Assertions.assertEquals("creator2", updatedCatalog.auditInfo().creator());
- Assertions.assertThrowsExactly(
- NoSuchEntityException.class,
- () ->
- store.get(
- NameIdentifier.of("metalakeChanged", "catalog"),
- EntityType.CATALOG,
- CatalogEntity.class));
- Assertions.assertNotNull(
- store.get(
- NameIdentifier.of("metalakeChanged", "catalogChanged", "schema1"),
- EntityType.SCHEMA,
- SchemaEntity.class));
- Assertions.assertNotNull(
- store.get(
- NameIdentifier.of("metalakeChanged", "catalogChanged", "schema1", "table1"),
- EntityType.TABLE,
- TableEntity.class));
- Assertions.assertNotNull(
- store.get(
- NameIdentifier.of("metalakeChanged", "catalogChanged", "schema1", "fileset1"),
- EntityType.FILESET,
- FilesetEntity.class));
- Assertions.assertNotNull(
- store.get(
- NameIdentifier.of("metalakeChanged", "catalogChanged", "schema2"),
- EntityType.SCHEMA,
- SchemaEntity.class));
- Assertions.assertNotNull(
- store.get(
- NameIdentifier.of("metalakeChanged", "catalogChanged", "schema2", "table1"),
- EntityType.TABLE,
- TableEntity.class));
- Assertions.assertNotNull(
- store.get(
- NameIdentifier.of("metalakeChanged", "catalogChanged", "schema2", "fileset1"),
- EntityType.FILESET,
- FilesetEntity.class));
- }
-
- private void validateSchemaChanged(EntityStore store) throws IOException {
- // Check schema entities and sub-entities are already changed.
- store.update(
- NameIdentifier.of("metalakeChanged", "catalogChanged", "schema1"),
- SchemaEntity.class,
- EntityType.SCHEMA,
- e -> {
- AuditInfo auditInfo1 =
- AuditInfo.builder().withCreator("creator3").withCreateTime(Instant.now()).build();
- return createSchemaEntity(
- Namespace.of("metalakeChanged", "catalogChanged"), "schemaChanged", auditInfo1);
- });
-
- Assertions.assertThrowsExactly(
- NoSuchEntityException.class,
- () ->
- store.get(
- NameIdentifier.of("metalakeChanged", "catalogChanged", "schema1"),
- EntityType.SCHEMA,
- SchemaEntity.class));
- SchemaEntity updatedSchema =
- store.get(
- NameIdentifier.of("metalakeChanged", "catalogChanged", "schemaChanged"),
- EntityType.SCHEMA,
- SchemaEntity.class);
- Assertions.assertEquals("creator3", updatedSchema.auditInfo().creator());
-
- Assertions.assertNotNull(
- store.get(
- NameIdentifier.of("metalakeChanged", "catalogChanged", "schemaChanged"),
- EntityType.SCHEMA,
- SchemaEntity.class));
- Assertions.assertNotNull(
- store.get(
- NameIdentifier.of("metalakeChanged", "catalogChanged", "schemaChanged", "table1"),
- EntityType.TABLE,
- TableEntity.class));
- Assertions.assertNotNull(
- store.get(
- NameIdentifier.of("metalakeChanged", "catalogChanged", "schemaChanged", "fileset1"),
- EntityType.FILESET,
- FilesetEntity.class));
- Assertions.assertNotNull(
- store.get(
- NameIdentifier.of("metalakeChanged", "catalogChanged", "schema2"),
- EntityType.SCHEMA,
- SchemaEntity.class));
- Assertions.assertNotNull(
- store.get(
- NameIdentifier.of("metalakeChanged", "catalogChanged", "schema2", "table1"),
- EntityType.TABLE,
- TableEntity.class));
- Assertions.assertNotNull(
- store.get(
- NameIdentifier.of("metalakeChanged", "catalogChanged", "schema2", "fileset1"),
- EntityType.FILESET,
- FilesetEntity.class));
- }
-
- private void validateTableChanged(EntityStore store) throws IOException {
- // Check table entities
- store.update(
- NameIdentifier.of("metalakeChanged", "catalogChanged", "schemaChanged", "table1"),
- TableEntity.class,
- EntityType.TABLE,
- e -> {
- AuditInfo auditInfo1 =
- AuditInfo.builder().withCreator("creator4").withCreateTime(Instant.now()).build();
- return createTableEntity(
- Namespace.of("metalakeChanged", "catalogChanged", "schemaChanged"),
- "tableChanged",
- auditInfo1);
- });
-
- Assertions.assertThrowsExactly(
- NoSuchEntityException.class,
- () ->
- store.get(
- NameIdentifier.of("metalakeChanged", "catalogChanged", "schema1", "table1"),
- EntityType.TABLE,
- TableEntity.class));
- TableEntity updatedTable =
- store.get(
- NameIdentifier.of("metalakeChanged", "catalogChanged", "schemaChanged", "tableChanged"),
- EntityType.TABLE,
- TableEntity.class);
- Assertions.assertEquals("creator4", updatedTable.auditInfo().creator());
-
- Assertions.assertNotNull(
- store.get(
- NameIdentifier.of("metalakeChanged", "catalogChanged", "schema2", "table1"),
- EntityType.TABLE,
- TableEntity.class));
- }
-
- private void validateDeletedTable(EntityStore store) throws IOException {
- store.delete(
- NameIdentifier.of("metalakeChanged", "catalogChanged", "schema2", "table1"),
- EntityType.TABLE);
- // Update a deleted entities
- Assertions.assertThrowsExactly(
- NoSuchEntityException.class,
- () ->
- store.update(
- NameIdentifier.of("metalakeChanged", "catalogChanged", "schema2", "table1"),
- TableEntity.class,
- EntityType.TABLE,
- (e) -> e));
- }
-
- @Test
- void testEntityDelete() throws IOException {
- // TODO
- Config config = Mockito.mock(Config.class);
- Mockito.when(config.get(ENTITY_STORE)).thenReturn("kv");
- Mockito.when(config.get(ENTITY_KV_STORE)).thenReturn(DEFAULT_ENTITY_KV_STORE);
- Mockito.when(config.get(Configs.ENTITY_SERDE)).thenReturn("proto");
- Mockito.when(config.get(ENTRY_KV_ROCKSDB_BACKEND_PATH)).thenReturn("/tmp/gravitino");
- Mockito.when(config.get(STORE_TRANSACTION_MAX_SKEW_TIME)).thenReturn(1000L);
- Mockito.when(config.get(KV_DELETE_AFTER_TIME)).thenReturn(20 * 60 * 1000L);
-
- FileUtils.deleteDirectory(FileUtils.getFile("/tmp/gravitino"));
-
- AuditInfo auditInfo =
- AuditInfo.builder().withCreator("creator").withCreateTime(Instant.now()).build();
-
- try (EntityStore store = EntityStoreFactory.createEntityStore(config)) {
- store.initialize(config);
- Assertions.assertTrue(store instanceof KvEntityStore);
- store.setSerDe(EntitySerDeFactory.createEntitySerDe(config.get(Configs.ENTITY_SERDE)));
-
- BaseMetalake metalake = createBaseMakeLake("metalake", auditInfo);
- CatalogEntity catalog = createCatalog(Namespace.of("metalake"), "catalog", auditInfo);
- CatalogEntity catalogCopy = createCatalog(Namespace.of("metalake"), "catalogCopy", auditInfo);
-
- SchemaEntity schema1 =
- createSchemaEntity(Namespace.of("metalake", "catalog"), "schema1", auditInfo);
- TableEntity table1 =
- createTableEntity(Namespace.of("metalake", "catalog", "schema1"), "table1", auditInfo);
- FilesetEntity fileset1 =
- createFilesetEntity(
- Namespace.of("metalake", "catalog", "schema1"), "fileset1", auditInfo);
-
- SchemaEntity schema2 =
- createSchemaEntity(Namespace.of("metalake", "catalog"), "schema2", auditInfo);
- TableEntity table1InSchema2 =
- createTableEntity(Namespace.of("metalake", "catalog", "schema2"), "table1", auditInfo);
- FilesetEntity fileset1InSchema2 =
- createFilesetEntity(
- Namespace.of("metalake", "catalog", "schema2"), "fileset1", auditInfo);
-
- // Store all entities
- store.put(metalake);
- store.put(catalog);
- store.put(catalogCopy);
- store.put(schema1);
- store.put(schema2);
- store.put(table1);
- store.put(table1InSchema2);
- store.put(fileset1);
- store.put(fileset1InSchema2);
-
- validateAllEntityExist(
- metalake,
- store,
- catalog,
- catalogCopy,
- schema1,
- schema2,
- table1,
- table1InSchema2,
- fileset1,
- fileset1InSchema2);
-
- validateDeleteTable(store, schema2, table1, table1InSchema2);
-
- validateDeleteFileset(store, schema2, fileset1, fileset1InSchema2);
-
- validateDeleteSchema(store, schema1, table1, fileset1);
-
- validateDeleteCatalog(
- store, catalog, table1, schema1, table1InSchema2, schema2, fileset1, fileset1InSchema2);
-
- validateDeleteMetalake(store, metalake, catalogCopy);
-
- // Store all entities again
- store.put(metalake);
- store.put(catalog);
- store.put(catalogCopy);
- store.put(schema1);
- store.put(schema2);
- store.put(table1);
- store.put(table1InSchema2);
- store.put(fileset1);
- store.put(fileset1InSchema2);
-
- validateDeleteTableCascade(store, table1);
-
- validateDeleteFilesetCascade(store, fileset1);
-
- validateDeleteSchemaCascade(store, schema1, table1, fileset1);
-
- validateDeleteCatalogCascade(store, catalog, schema2);
-
- validateDeleteMetalakeCascade(store, metalake, catalog, schema2);
- }
- }
-
- private void validateDeleteFilesetCascade(EntityStore store, FilesetEntity fileset1)
- throws IOException {
- // Delete the fileset 'metalake.catalog.schema1.fileset1'
- Assertions.assertTrue(store.delete(fileset1.nameIdentifier(), EntityType.FILESET, true));
- Assertions.assertFalse(store.exists(fileset1.nameIdentifier(), EntityType.FILESET));
- }
-
- private void validateDeleteTableCascade(EntityStore store, TableEntity table1)
- throws IOException {
- // Delete the table 'metalake.catalog.schema1.table1'
- Assertions.assertTrue(store.delete(table1.nameIdentifier(), EntityType.TABLE, true));
- Assertions.assertFalse(store.exists(table1.nameIdentifier(), EntityType.TABLE));
- }
-
- private void validateDeleteFileset(
- EntityStore store,
- SchemaEntity schema2,
- FilesetEntity fileset1,
- FilesetEntity fileset1InSchema2)
- throws IOException {
- // Delete the fileset 'metalake.catalog.schema2.fileset1'
- Assertions.assertTrue(store.delete(fileset1InSchema2.nameIdentifier(), EntityType.FILESET));
- Assertions.assertFalse(store.exists(fileset1InSchema2.nameIdentifier(), EntityType.FILESET));
-
- // Make sure fileset 'metalake.catalog.schema1.fileset1' still exist;
- Assertions.assertEquals(
- fileset1, store.get(fileset1.nameIdentifier(), EntityType.FILESET, FilesetEntity.class));
- // Make sure schema 'metalake.catalog.schema2' still exist;
- Assertions.assertEquals(
- schema2, store.get(schema2.nameIdentifier(), EntityType.SCHEMA, SchemaEntity.class));
- }
-
- private void validateDeleteMetalakeCascade(
- EntityStore store, BaseMetalake metalake, CatalogEntity catalog, SchemaEntity schema2)
- throws IOException {
- Assertions.assertTrue(store.delete(metalake.nameIdentifier(), EntityType.METALAKE, true));
-
- // catalog has already deleted, so we can't delete it again and should return false
- Assertions.assertFalse(store.delete(catalog.nameIdentifier(), EntityType.CATALOG));
- Assertions.assertFalse(store.delete(schema2.nameIdentifier(), EntityType.SCHEMA));
- Assertions.assertFalse(store.delete(metalake.nameIdentifier(), EntityType.METALAKE));
- }
-
- private void validateDeleteCatalogCascade(
- EntityStore store, CatalogEntity catalog, SchemaEntity schema2) throws IOException {
- Assertions.assertThrowsExactly(
- NonEmptyEntityException.class,
- () -> store.delete(catalog.nameIdentifier(), EntityType.CATALOG));
- store.delete(catalog.nameIdentifier(), EntityType.CATALOG, true);
- NameIdentifier id = catalog.nameIdentifier();
- Assertions.assertThrowsExactly(
- NoSuchEntityException.class, () -> store.get(id, EntityType.CATALOG, CatalogEntity.class));
-
- Assertions.assertThrowsExactly(
- NoSuchEntityException.class,
- () -> store.get(schema2.nameIdentifier(), EntityType.SCHEMA, SchemaEntity.class));
- }
-
- private void validateDeleteSchemaCascade(
- EntityStore store, SchemaEntity schema1, TableEntity table1, FilesetEntity fileset1)
- throws IOException {
- store.put(table1);
- store.put(fileset1);
-
- Assertions.assertThrowsExactly(
- NonEmptyEntityException.class,
- () -> store.delete(schema1.nameIdentifier(), EntityType.SCHEMA));
-
- Assertions.assertEquals(
- schema1, store.get(schema1.nameIdentifier(), EntityType.SCHEMA, SchemaEntity.class));
-
- // Test cascade delete
- store.delete(schema1.nameIdentifier(), EntityType.SCHEMA, true);
- try {
- store.get(table1.nameIdentifier(), EntityType.TABLE, TableEntity.class);
- } catch (Exception e) {
- Assertions.assertTrue(e instanceof NoSuchEntityException);
- Assertions.assertTrue(e.getMessage().contains("metalake.catalog.schema1"));
- }
-
- Assertions.assertThrows(
- NoSuchEntityException.class,
- () -> store.get(fileset1.nameIdentifier(), EntityType.FILESET, FilesetEntity.class));
- }
-
- private static void validateDeleteMetalake(
- EntityStore store, BaseMetalake metalake, CatalogEntity catalogCopy) throws IOException {
- // Now delete catalog 'catalogCopy' and metalake
- Assertions.assertThrowsExactly(
- NonEmptyEntityException.class,
- () -> store.delete(metalake.nameIdentifier(), EntityType.METALAKE));
- store.delete(catalogCopy.nameIdentifier(), EntityType.CATALOG);
- Assertions.assertFalse(store.exists(catalogCopy.nameIdentifier(), EntityType.CATALOG));
-
- store.delete(metalake.nameIdentifier(), EntityType.METALAKE);
- Assertions.assertFalse(store.exists(metalake.nameIdentifier(), EntityType.METALAKE));
- }
-
- private static void validateDeleteCatalog(
- EntityStore store,
- CatalogEntity catalog,
- TableEntity table1,
- SchemaEntity schema1,
- TableEntity table1InSchema2,
- SchemaEntity schema2,
- FilesetEntity fileset1,
- FilesetEntity fileset1InSchema2)
- throws IOException {
- // Now try to delete all schemas under catalog;
- Assertions.assertThrowsExactly(
- NonEmptyEntityException.class,
- () -> store.delete(catalog.nameIdentifier(), EntityType.CATALOG));
- store.delete(table1.nameIdentifier(), EntityType.TABLE);
- store.delete(fileset1.nameIdentifier(), EntityType.FILESET);
- store.delete(schema1.nameIdentifier(), EntityType.SCHEMA);
- store.delete(table1InSchema2.nameIdentifier(), EntityType.TABLE);
- store.delete(fileset1InSchema2.nameIdentifier(), EntityType.FILESET);
- store.delete(schema2.nameIdentifier(), EntityType.SCHEMA);
-
- store.delete(catalog.nameIdentifier(), EntityType.CATALOG);
- Assertions.assertFalse(store.exists(catalog.nameIdentifier(), EntityType.CATALOG));
- }
-
- private static void validateDeleteSchema(
- EntityStore store, SchemaEntity schema1, TableEntity table1, FilesetEntity fileset1)
- throws IOException {
- // Delete the schema 'metalake.catalog.schema1' but failed, because it ha sub-entities;
- NonEmptyEntityException exception =
- Assertions.assertThrowsExactly(
- NonEmptyEntityException.class,
- () -> store.delete(schema1.nameIdentifier(), EntityType.SCHEMA));
- Assertions.assertTrue(exception.getMessage().contains("metalake.catalog.schema1.table1"));
- // Make sure schema 'metalake.catalog.schema1' and table 'metalake.catalog.schema1.table1'
- // and table 'metalake.catalog.schema1.fileset1' has not been deleted yet;
- Assertions.assertTrue(store.exists(schema1.nameIdentifier(), EntityType.SCHEMA));
- Assertions.assertTrue(store.exists(table1.nameIdentifier(), EntityType.TABLE));
- Assertions.assertTrue(store.exists(fileset1.nameIdentifier(), EntityType.FILESET));
-
- // Delete table1,fileset1 and schema1
- Assertions.assertTrue(store.delete(table1.nameIdentifier(), EntityType.TABLE));
- Assertions.assertTrue(store.delete(fileset1.nameIdentifier(), EntityType.FILESET));
- Assertions.assertTrue(store.delete(schema1.nameIdentifier(), EntityType.SCHEMA));
- // Make sure table1, fileset1 in 'metalake.catalog.schema1' can't be access;
- Assertions.assertFalse(store.exists(table1.nameIdentifier(), EntityType.TABLE));
- Assertions.assertFalse(store.exists(fileset1.nameIdentifier(), EntityType.FILESET));
- Assertions.assertFalse(store.exists(schema1.nameIdentifier(), EntityType.SCHEMA));
- // Now we re-insert table1 and schema1, and everything should be OK
- store.put(schema1);
- store.put(table1);
- store.put(fileset1);
- Assertions.assertEquals(
- schema1, store.get(schema1.nameIdentifier(), EntityType.SCHEMA, SchemaEntity.class));
- Assertions.assertEquals(
- table1, store.get(table1.nameIdentifier(), EntityType.TABLE, TableEntity.class));
- Assertions.assertEquals(
- fileset1, store.get(fileset1.nameIdentifier(), EntityType.FILESET, FilesetEntity.class));
- }
-
- private void validateDeleteTable(
- EntityStore store, SchemaEntity schema2, TableEntity table1, TableEntity table1InSchema2)
- throws IOException {
- // Delete the table 'metalake.catalog.schema2.table1'
- Assertions.assertTrue(store.delete(table1InSchema2.nameIdentifier(), EntityType.TABLE));
- Assertions.assertFalse(store.exists(table1InSchema2.nameIdentifier(), EntityType.TABLE));
-
- // Make sure table 'metalake.catalog.schema1.table1' still exist;
- Assertions.assertEquals(
- table1, store.get(table1.nameIdentifier(), EntityType.TABLE, TableEntity.class));
- // Make sure schema 'metalake.catalog.schema2' still exist;
- Assertions.assertEquals(
- schema2, store.get(schema2.nameIdentifier(), EntityType.SCHEMA, SchemaEntity.class));
- // Re-insert table1Inschema2 and everything is OK
- store.put(table1InSchema2);
- Assertions.assertTrue(store.exists(table1InSchema2.nameIdentifier(), EntityType.TABLE));
- }
-
- private static void validateAllEntityExist(
- BaseMetalake metalake,
- EntityStore store,
- CatalogEntity catalog,
- CatalogEntity catalogCopy,
- SchemaEntity schema1,
- SchemaEntity schema2,
- TableEntity table1,
- TableEntity table1InSchema2,
- FilesetEntity fileset1,
- FilesetEntity fileset1Inschema2)
- throws IOException {
- // Now try to get
- Assertions.assertEquals(
- metalake, store.get(metalake.nameIdentifier(), EntityType.METALAKE, BaseMetalake.class));
- Assertions.assertEquals(
- catalog, store.get(catalog.nameIdentifier(), EntityType.CATALOG, CatalogEntity.class));
- Assertions.assertEquals(
- catalogCopy,
- store.get(catalogCopy.nameIdentifier(), EntityType.CATALOG, CatalogEntity.class));
- Assertions.assertEquals(
- schema1, store.get(schema1.nameIdentifier(), EntityType.SCHEMA, SchemaEntity.class));
- Assertions.assertEquals(
- schema2, store.get(schema2.nameIdentifier(), EntityType.SCHEMA, SchemaEntity.class));
- Assertions.assertEquals(
- table1, store.get(table1.nameIdentifier(), EntityType.TABLE, TableEntity.class));
- Assertions.assertEquals(
- table1InSchema2,
- store.get(table1InSchema2.nameIdentifier(), EntityType.TABLE, TableEntity.class));
- Assertions.assertEquals(
- fileset1, store.get(fileset1.nameIdentifier(), EntityType.FILESET, FilesetEntity.class));
- Assertions.assertEquals(
- fileset1Inschema2,
- store.get(fileset1Inschema2.nameIdentifier(), EntityType.FILESET, FilesetEntity.class));
- }
-
@Test
void testCreateKvEntityStore() throws IOException {
Config config = Mockito.mock(Config.class);
@@ -938,18 +80,19 @@ public class TestKvEntityStorage {
AuditInfo auditInfo =
AuditInfo.builder().withCreator("creator").withCreateTime(Instant.now()).build();
- BaseMetalake metalake = createBaseMakeLake("metalake", auditInfo);
- BaseMetalake metalakeCopy = createBaseMakeLake("metalakeCopy", auditInfo);
- CatalogEntity catalog = createCatalog(Namespace.of("metalake"), "catalog", auditInfo);
- CatalogEntity catalogCopy = createCatalog(Namespace.of("metalake"), "catalogCopy", auditInfo);
+ BaseMetalake metalake = createBaseMakeLake(1L, "metalake", auditInfo);
+ BaseMetalake metalakeCopy = createBaseMakeLake(2L, "metalakeCopy", auditInfo);
+ CatalogEntity catalog = createCatalog(1L, Namespace.of("metalake"), "catalog", auditInfo);
+ CatalogEntity catalogCopy =
+ createCatalog(2L, Namespace.of("metalake"), "catalogCopy", auditInfo);
CatalogEntity catalogCopyAgain =
- createCatalog(Namespace.of("metalake"), "catalogCopyAgain", auditInfo);
+ createCatalog(3L, Namespace.of("metalake"), "catalogCopyAgain", auditInfo);
// First, we try to test transactional is OK
final NameIdentifier metalakeID1 = metalake.nameIdentifier();
Assertions.assertThrows(
NoSuchEntityException.class,
- () -> store.get(metalakeID1, EntityType.METALAKE, BaseMetalake.class));
+ () -> store.get(metalakeID1, Entity.EntityType.METALAKE, BaseMetalake.class));
store.put(metalake);
store.put(catalog);
@@ -958,67 +101,71 @@ public class TestKvEntityStorage {
store.put(catalogCopyAgain);
Metalake retrievedMetalake =
- store.get(metalake.nameIdentifier(), EntityType.METALAKE, BaseMetalake.class);
+ store.get(metalake.nameIdentifier(), Entity.EntityType.METALAKE, BaseMetalake.class);
Assertions.assertEquals(metalake, retrievedMetalake);
CatalogEntity retrievedCatalog =
- store.get(catalog.nameIdentifier(), EntityType.CATALOG, CatalogEntity.class);
+ store.get(catalog.nameIdentifier(), Entity.EntityType.CATALOG, CatalogEntity.class);
Assertions.assertEquals(catalog, retrievedCatalog);
Metalake retrievedMetalakeCopy =
- store.get(metalakeCopy.nameIdentifier(), EntityType.METALAKE, BaseMetalake.class);
+ store.get(metalakeCopy.nameIdentifier(), Entity.EntityType.METALAKE, BaseMetalake.class);
Assertions.assertEquals(metalakeCopy, retrievedMetalakeCopy);
CatalogEntity retrievedCatalogCopy =
- store.get(catalogCopy.nameIdentifier(), EntityType.CATALOG, CatalogEntity.class);
+ store.get(catalogCopy.nameIdentifier(), Entity.EntityType.CATALOG, CatalogEntity.class);
Assertions.assertEquals(catalogCopy, retrievedCatalogCopy);
// Test scan and store list interface
List<CatalogEntity> catalogEntityList =
- store.list(catalog.namespace(), CatalogEntity.class, EntityType.CATALOG);
+ store.list(catalog.namespace(), CatalogEntity.class, Entity.EntityType.CATALOG);
Assertions.assertEquals(3, catalogEntityList.size());
Assertions.assertTrue(catalogEntityList.contains(catalog));
Assertions.assertTrue(catalogEntityList.contains(catalogCopy));
Assertions.assertTrue(catalogEntityList.contains(catalogCopyAgain));
Assertions.assertThrows(EntityAlreadyExistsException.class, () -> store.put(catalog, false));
- store.delete(catalog.nameIdentifier(), EntityType.CATALOG);
+ store.delete(catalog.nameIdentifier(), Entity.EntityType.CATALOG);
final NameIdentifier metalakeID2 = catalog.nameIdentifier();
Assertions.assertThrows(
NoSuchEntityException.class,
- () -> store.get(metalakeID2, EntityType.CATALOG, CatalogEntity.class));
+ () -> store.get(metalakeID2, Entity.EntityType.CATALOG, CatalogEntity.class));
Assertions.assertThrows(
EntityAlreadyExistsException.class, () -> store.put(catalogCopy, false));
- store.delete(catalogCopy.nameIdentifier(), EntityType.CATALOG);
+ store.delete(catalogCopy.nameIdentifier(), Entity.EntityType.CATALOG);
final NameIdentifier metalakeID3 = catalogCopy.nameIdentifier();
Assertions.assertThrows(
NoSuchEntityException.class,
- () -> store.get(metalakeID3, EntityType.CATALOG, CatalogEntity.class));
+ () -> store.get(metalakeID3, Entity.EntityType.CATALOG, CatalogEntity.class));
Assertions.assertThrowsExactly(
NonEmptyEntityException.class,
- () -> store.delete(metalake.nameIdentifier(), EntityType.METALAKE));
- store.delete(catalogCopyAgain.nameIdentifier(), EntityType.CATALOG);
- Assertions.assertTrue(store.delete(metalake.nameIdentifier(), EntityType.METALAKE));
+ () -> store.delete(metalake.nameIdentifier(), Entity.EntityType.METALAKE));
+ store.delete(catalogCopyAgain.nameIdentifier(), Entity.EntityType.CATALOG);
+ Assertions.assertTrue(store.delete(metalake.nameIdentifier(), Entity.EntityType.METALAKE));
final NameIdentifier metalakeID4 = metalake.nameIdentifier();
Assertions.assertThrows(
NoSuchEntityException.class,
- () -> store.get(metalakeID4, EntityType.METALAKE, BaseMetalake.class));
+ () -> store.get(metalakeID4, Entity.EntityType.METALAKE, BaseMetalake.class));
// Test update
- BaseMetalake updatedMetalake = createBaseMakeLake("updatedMetalake", auditInfo);
+ BaseMetalake updatedMetalake = createBaseMakeLake(1L, "updatedMetalake", auditInfo);
store.put(metalake);
store.update(
- metalake.nameIdentifier(), BaseMetalake.class, EntityType.METALAKE, l -> updatedMetalake);
+ metalake.nameIdentifier(),
+ BaseMetalake.class,
+ Entity.EntityType.METALAKE,
+ l -> updatedMetalake);
Assertions.assertEquals(
updatedMetalake,
- store.get(updatedMetalake.nameIdentifier(), EntityType.METALAKE, BaseMetalake.class));
+ store.get(
+ updatedMetalake.nameIdentifier(), Entity.EntityType.METALAKE, BaseMetalake.class));
final NameIdentifier metalakeID5 = metalake.nameIdentifier();
Assertions.assertThrows(
NoSuchEntityException.class,
- () -> store.get(metalakeID5, EntityType.METALAKE, BaseMetalake.class));
+ () -> store.get(metalakeID5, Entity.EntityType.METALAKE, BaseMetalake.class));
// Add new updateMetaLake.
// 'updatedMetalake2' is a new name, which will trigger id allocation
- BaseMetalake updatedMetalake2 = createBaseMakeLake("updatedMetalake2", auditInfo);
+ BaseMetalake updatedMetalake2 = createBaseMakeLake(3L, "updatedMetalake2", auditInfo);
store.put(updatedMetalake2);
}
}
@@ -1056,19 +203,20 @@ public class TestKvEntityStorage {
AuditInfo auditInfo =
AuditInfo.builder().withCreator("creator").withCreateTime(Instant.now()).build();
- BaseMetalake metalake = createBaseMakeLake("metalake", auditInfo);
- CatalogEntity catalog = createCatalog(Namespace.of("metalake"), "catalog", auditInfo);
+ BaseMetalake metalake = createBaseMakeLake(1L, "metalake", auditInfo);
+ CatalogEntity catalog = createCatalog(1L, Namespace.of("metalake"), "catalog", auditInfo);
store.put(metalake);
store.put(catalog);
Assertions.assertNotNull(
- store.get(catalog.nameIdentifier(), EntityType.CATALOG, CatalogEntity.class));
+ store.get(catalog.nameIdentifier(), Entity.EntityType.CATALOG, CatalogEntity.class));
// Delete the catalog entity, and we try to use multi-thread to delete it and make sure only
// one thread can delete it.
for (int i = 0; i < 10; i++) {
future.submit(
- () -> store.delete(NameIdentifier.of("metalake", "catalog"), EntityType.CATALOG));
+ () ->
+ store.delete(NameIdentifier.of("metalake", "catalog"), Entity.EntityType.CATALOG));
}
int totalSuccessNum = 0;
for (int i = 0; i < 10; i++) {
@@ -1107,14 +255,14 @@ public class TestKvEntityStorage {
store.update(
NameIdentifier.of("metalake", "catalog"),
CatalogEntity.class,
- EntityType.CATALOG,
+ Entity.EntityType.CATALOG,
e -> {
AuditInfo auditInfo1 =
AuditInfo.builder()
.withCreator("creator1")
.withCreateTime(Instant.now())
.build();
- return createCatalog(Namespace.of("metalake"), "catalog1", auditInfo1);
+ return createCatalog(1L, Namespace.of("metalake"), "catalog1", auditInfo1);
});
return null;
});
@@ -1139,343 +287,4 @@ public class TestKvEntityStorage {
Assertions.assertEquals(9, totalFailed);
}
}
-
- @Test
- void testStorageLayoutVersion() throws IOException {
- Config config = Mockito.mock(Config.class);
- File baseDir = new File(System.getProperty("java.io.tmpdir"));
- File file = Files.createTempDirectory(baseDir.toPath(), "test").toFile();
- file.deleteOnExit();
- Mockito.when(config.get(ENTITY_STORE)).thenReturn("kv");
- Mockito.when(config.get(ENTITY_KV_STORE)).thenReturn(DEFAULT_ENTITY_KV_STORE);
- Mockito.when(config.get(Configs.ENTITY_SERDE)).thenReturn("proto");
- Mockito.when(config.get(ENTRY_KV_ROCKSDB_BACKEND_PATH)).thenReturn(file.getAbsolutePath());
- Mockito.when(config.get(STORE_TRANSACTION_MAX_SKEW_TIME)).thenReturn(1000L);
- Mockito.when(config.get(KV_DELETE_AFTER_TIME)).thenReturn(20 * 60 * 1000L);
-
- // First time create entity store, the storage layout version should be DEFAULT_LAYOUT_VERSION
- try (EntityStore store = EntityStoreFactory.createEntityStore(config)) {
- store.initialize(config);
- Assertions.assertTrue(store instanceof KvEntityStore);
- store.setSerDe(EntitySerDeFactory.createEntitySerDe(config.get(Configs.ENTITY_SERDE)));
- KvEntityStore entityStore = (KvEntityStore) store;
- Assertions.assertEquals(StorageLayoutVersion.V1, entityStore.storageLayoutVersion);
- }
-
- // Second time create entity store, the storage layout version should be DEFAULT_LAYOUT_VERSION
- try (EntityStore store = EntityStoreFactory.createEntityStore(config)) {
- store.initialize(config);
- Assertions.assertTrue(store instanceof KvEntityStore);
- store.setSerDe(EntitySerDeFactory.createEntitySerDe(config.get(Configs.ENTITY_SERDE)));
- KvEntityStore entityStore = (KvEntityStore) store;
- Assertions.assertEquals(StorageLayoutVersion.V1, entityStore.storageLayoutVersion);
- }
- }
-
- @Test
- void testDeleteAndRename() throws IOException {
- Config config = Mockito.mock(Config.class);
- File baseDir = new File(System.getProperty("java.io.tmpdir"));
- File file = Files.createTempDirectory(baseDir.toPath(), "test").toFile();
- file.deleteOnExit();
- Mockito.when(config.get(ENTITY_STORE)).thenReturn("kv");
- Mockito.when(config.get(ENTITY_KV_STORE)).thenReturn(DEFAULT_ENTITY_KV_STORE);
- Mockito.when(config.get(Configs.ENTITY_SERDE)).thenReturn("proto");
- Mockito.when(config.get(ENTRY_KV_ROCKSDB_BACKEND_PATH)).thenReturn(file.getAbsolutePath());
- Mockito.when(config.get(STORE_TRANSACTION_MAX_SKEW_TIME)).thenReturn(1000L);
- Mockito.when(config.get(KV_DELETE_AFTER_TIME)).thenReturn(20 * 60 * 1000L);
-
- try (EntityStore store = EntityStoreFactory.createEntityStore(config)) {
- store.initialize(config);
- Assertions.assertTrue(store instanceof KvEntityStore);
- store.setSerDe(EntitySerDeFactory.createEntitySerDe(config.get(Configs.ENTITY_SERDE)));
-
- AuditInfo auditInfo =
- AuditInfo.builder().withCreator("creator").withCreateTime(Instant.now()).build();
-
- BaseMetalake metalake1 = createBaseMakeLake("metalake1", auditInfo);
- BaseMetalake metalake2 = createBaseMakeLake("metalake2", auditInfo);
- BaseMetalake metalake3 = createBaseMakeLake("metalake3", auditInfo);
-
- store.put(metalake1);
- store.put(metalake2);
- store.put(metalake3);
-
- store.delete(NameIdentifier.of("metalake1"), EntityType.METALAKE);
- store.delete(NameIdentifier.of("metalake2"), EntityType.METALAKE);
- store.delete(NameIdentifier.of("metalake3"), EntityType.METALAKE);
-
- // Rename metalake1 --> metalake2
- store.put(metalake1);
- store.update(
- NameIdentifier.of("metalake1"),
- BaseMetalake.class,
- EntityType.METALAKE,
- e -> createBaseMakeLake("metalake2", (AuditInfo) e.auditInfo()));
-
- // Rename metalake3 --> metalake1
- store.put(metalake3);
- store.update(
- NameIdentifier.of("metalake3"),
- BaseMetalake.class,
- EntityType.METALAKE,
- e -> createBaseMakeLake("metalake1", (AuditInfo) e.auditInfo()));
-
- // Rename metalake3 --> metalake2
- store.put(metalake3);
- store.delete(NameIdentifier.of("metalake2"), EntityType.METALAKE);
- store.update(
- NameIdentifier.of("metalake3"),
- BaseMetalake.class,
- EntityType.METALAKE,
- e -> createBaseMakeLake("metalake2", (AuditInfo) e.auditInfo()));
-
- // Finally, only metalake2 and metalake1 are left.
- Assertions.assertDoesNotThrow(
- () -> store.get(NameIdentifier.of("metalake2"), EntityType.METALAKE, BaseMetalake.class));
- Assertions.assertDoesNotThrow(
- () -> store.get(NameIdentifier.of("metalake1"), EntityType.METALAKE, BaseMetalake.class));
- NameIdentifier id = NameIdentifier.of("metalake3");
- Assertions.assertThrows(
- NoSuchEntityException.class,
- () -> store.get(id, EntityType.METALAKE, BaseMetalake.class));
-
- // Test catalog
- CatalogEntity catalog1 = createCatalog(Namespace.of("metalake1"), "catalog1", auditInfo);
- CatalogEntity catalog2 = createCatalog(Namespace.of("metalake1"), "catalog2", auditInfo);
-
- store.put(catalog1);
- store.put(catalog2);
-
- store.delete(NameIdentifier.of("metalake1", "catalog1"), EntityType.CATALOG);
- store.delete(NameIdentifier.of("metalake1", "catalog2"), EntityType.CATALOG);
-
- store.put(catalog1);
- // Should be OK;
- store.update(
- NameIdentifier.of("metalake1", "catalog1"),
- CatalogEntity.class,
- EntityType.CATALOG,
- e -> createCatalog(Namespace.of("metalake1"), "catalog2", (AuditInfo) e.auditInfo()));
-
- // Test schema
- SchemaEntity schema1 =
- createSchemaEntity(Namespace.of("metalake1", "catalog2"), "schema1", auditInfo);
- SchemaEntity schema2 =
- createSchemaEntity(Namespace.of("metalake1", "catalog2"), "schema2", auditInfo);
-
- store.put(schema1);
- store.put(schema2);
-
- store.delete(NameIdentifier.of("metalake1", "catalog2", "schema1"), EntityType.SCHEMA);
- store.delete(NameIdentifier.of("metalake1", "catalog2", "schema2"), EntityType.SCHEMA);
-
- store.put(schema1);
- store.update(
- NameIdentifier.of("metalake1", "catalog2", "schema1"),
- SchemaEntity.class,
- EntityType.SCHEMA,
- e -> createSchemaEntity(Namespace.of("metalake1", "catalog2"), "schema2", e.auditInfo()));
-
- // Test table
- TableEntity table1 =
- createTableEntity(Namespace.of("metalake1", "catalog2", "schema2"), "table1", auditInfo);
- TableEntity table2 =
- createTableEntity(Namespace.of("metalake1", "catalog2", "schema2"), "table2", auditInfo);
-
- store.put(table1);
- store.put(table2);
-
- store.delete(
- NameIdentifier.of("metalake1", "catalog2", "schema2", "table1"), EntityType.TABLE);
- store.delete(
- NameIdentifier.of("metalake1", "catalog2", "schema2", "table2"), EntityType.TABLE);
-
- store.put(table1);
- store.update(
- NameIdentifier.of("metalake1", "catalog2", "schema2", "table1"),
- TableEntity.class,
- EntityType.TABLE,
- e ->
- createTableEntity(
- Namespace.of("metalake1", "catalog2", "schema2"), "table2", e.auditInfo()));
-
- // Test Fileset
- FilesetEntity fileset1 =
- createFilesetEntity(
- Namespace.of("metalake1", "catalog2", "schema2"), "fileset1", auditInfo);
- FilesetEntity fileset2 =
- createFilesetEntity(
- Namespace.of("metalake1", "catalog2", "schema2"), "fileset2", auditInfo);
-
- store.put(fileset1);
- store.put(fileset2);
-
- store.delete(
- NameIdentifier.of("metalake1", "catalog2", "schema2", "fileset1"), EntityType.FILESET);
- store.delete(
- NameIdentifier.of("metalake1", "catalog2", "schema2", "fileset2"), EntityType.FILESET);
-
- store.put(fileset1);
- store.update(
- NameIdentifier.of("metalake1", "catalog2", "schema2", "fileset1"),
- FilesetEntity.class,
- EntityType.FILESET,
- e ->
- createFilesetEntity(
- Namespace.of("metalake1", "catalog2", "schema2"), "fileset2", e.auditInfo()));
- }
- }
-
- @Test
- void testRemoveWithGCCollector() throws IOException, InterruptedException {
- Config config = Mockito.mock(Config.class);
- File baseDir = new File(System.getProperty("java.io.tmpdir"));
- File file = Files.createTempDirectory(baseDir.toPath(), "test").toFile();
- file.deleteOnExit();
- Mockito.when(config.get(ENTITY_STORE)).thenReturn("kv");
- Mockito.when(config.get(ENTITY_KV_STORE)).thenReturn(DEFAULT_ENTITY_KV_STORE);
- Mockito.when(config.get(Configs.ENTITY_SERDE)).thenReturn("proto");
- Mockito.when(config.get(ENTRY_KV_ROCKSDB_BACKEND_PATH)).thenReturn(file.getAbsolutePath());
- Mockito.when(config.get(STORE_TRANSACTION_MAX_SKEW_TIME)).thenReturn(1000L);
- Mockito.when(config.get(KV_DELETE_AFTER_TIME)).thenReturn(20 * 60 * 1000L);
-
- try (EntityStore store = EntityStoreFactory.createEntityStore(config)) {
- store.initialize(config);
-
- if (!(store instanceof KvEntityStore)) {
- return;
- }
- KvEntityStore kvEntityStore = (KvEntityStore) store;
-
- store.setSerDe(EntitySerDeFactory.createEntitySerDe(config.get(Configs.ENTITY_SERDE)));
- AuditInfo auditInfo =
- AuditInfo.builder().withCreator("creator").withCreateTime(Instant.now()).build();
-
- BaseMetalake metalake1 = createBaseMakeLake("metalake1", auditInfo);
- BaseMetalake metalake2 = createBaseMakeLake("metalake2", auditInfo);
- BaseMetalake metalake3 = createBaseMakeLake("metalake3", auditInfo);
-
- store.put(metalake1);
- store.put(metalake2);
- store.put(metalake3);
-
- store.delete(NameIdentifier.of("metalake1"), EntityType.METALAKE);
- store.delete(NameIdentifier.of("metalake2"), EntityType.METALAKE);
- store.delete(NameIdentifier.of("metalake3"), EntityType.METALAKE);
-
- store.put(metalake1);
- store.put(metalake2);
- store.put(metalake3);
-
- Mockito.when(config.get(KV_DELETE_AFTER_TIME)).thenReturn(1000L);
- Thread.sleep(1500);
-
- kvEntityStore.kvGarbageCollector.collectAndClean();
-
- Assertions.assertDoesNotThrow(
- () -> store.get(NameIdentifier.of("metalake1"), EntityType.METALAKE, BaseMetalake.class));
- Assertions.assertDoesNotThrow(
- () -> store.get(NameIdentifier.of("metalake2"), EntityType.METALAKE, BaseMetalake.class));
- Assertions.assertDoesNotThrow(
- () -> store.get(NameIdentifier.of("metalake3"), EntityType.METALAKE, BaseMetalake.class));
-
- // Test catalog
- CatalogEntity catalog1 = createCatalog(Namespace.of("metalake1"), "catalog1", auditInfo);
- CatalogEntity catalog2 = createCatalog(Namespace.of("metalake1"), "catalog2", auditInfo);
-
- store.put(catalog1);
- store.put(catalog2);
-
- store.delete(NameIdentifier.of("metalake1", "catalog1"), EntityType.CATALOG);
- store.delete(NameIdentifier.of("metalake1", "catalog2"), EntityType.CATALOG);
-
- store.put(catalog1);
- store.put(catalog2);
-
- Mockito.when(config.get(KV_DELETE_AFTER_TIME)).thenReturn(1000L);
- Thread.sleep(1500);
-
- kvEntityStore.kvGarbageCollector.collectAndClean();
-
- Assertions.assertDoesNotThrow(
- () ->
- store.get(
- NameIdentifier.of("metalake1", "catalog1"),
- EntityType.CATALOG,
- CatalogEntity.class));
- Assertions.assertDoesNotThrow(
- () ->
- store.get(
- NameIdentifier.of("metalake1", "catalog2"),
- EntityType.CATALOG,
- CatalogEntity.class));
-
- // Test schema
- SchemaEntity schema1 =
- createSchemaEntity(Namespace.of("metalake1", "catalog2"), "schema1", auditInfo);
- SchemaEntity schema2 =
- createSchemaEntity(Namespace.of("metalake1", "catalog2"), "schema2", auditInfo);
-
- store.put(schema1);
- store.put(schema2);
-
- store.delete(NameIdentifier.of("metalake1", "catalog2", "schema1"), EntityType.SCHEMA);
- store.delete(NameIdentifier.of("metalake1", "catalog2", "schema2"), EntityType.SCHEMA);
-
- store.put(schema1);
- store.put(schema2);
-
- Mockito.when(config.get(KV_DELETE_AFTER_TIME)).thenReturn(1000L);
- Thread.sleep(1500);
- kvEntityStore.kvGarbageCollector.collectAndClean();
-
- Assertions.assertDoesNotThrow(
- () ->
- store.get(
- NameIdentifier.of("metalake1", "catalog2", "schema1"),
- EntityType.SCHEMA,
- SchemaEntity.class));
- Assertions.assertDoesNotThrow(
- () ->
- store.get(
- NameIdentifier.of("metalake1", "catalog2", "schema2"),
- EntityType.SCHEMA,
- SchemaEntity.class));
-
- // Test table
- TableEntity table1 =
- createTableEntity(Namespace.of("metalake1", "catalog2", "schema2"), "table1", auditInfo);
- TableEntity table2 =
- createTableEntity(Namespace.of("metalake1", "catalog2", "schema2"), "table2", auditInfo);
-
- store.put(table1);
- store.put(table2);
-
- store.delete(
- NameIdentifier.of("metalake1", "catalog2", "schema2", "table1"), EntityType.TABLE);
- store.delete(
- NameIdentifier.of("metalake1", "catalog2", "schema2", "table2"), EntityType.TABLE);
-
- store.put(table1);
- store.put(table2);
-
- Mockito.when(config.get(KV_DELETE_AFTER_TIME)).thenReturn(1000L);
- Thread.sleep(1500);
- kvEntityStore.kvGarbageCollector.collectAndClean();
-
- Assertions.assertDoesNotThrow(
- () ->
- store.get(
- NameIdentifier.of("metalake1", "catalog2", "schema2", "table1"),
- EntityType.TABLE,
- TableEntity.class));
- Assertions.assertDoesNotThrow(
- () ->
- store.get(
- NameIdentifier.of("metalake1", "catalog2", "schema2", "table2"),
- EntityType.TABLE,
- TableEntity.class));
- }
- }
}
diff --git a/core/src/test/java/com/datastrato/gravitino/storage/kv/TestKvGarbageCollector.java b/core/src/test/java/com/datastrato/gravitino/storage/kv/TestKvGarbageCollector.java
index 045b08924e..fbc29af178 100644
--- a/core/src/test/java/com/datastrato/gravitino/storage/kv/TestKvGarbageCollector.java
+++ b/core/src/test/java/com/datastrato/gravitino/storage/kv/TestKvGarbageCollector.java
@@ -19,6 +19,7 @@ import static com.datastrato.gravitino.storage.kv.TestKvEntityStorage.createTabl
import com.datastrato.gravitino.Config;
import com.datastrato.gravitino.Configs;
+import com.datastrato.gravitino.Entity;
import com.datastrato.gravitino.EntitySerDeFactory;
import com.datastrato.gravitino.EntityStore;
import com.datastrato.gravitino.EntityStoreFactory;
@@ -151,7 +152,7 @@ class TestKvGarbageCollector {
}
@Test
- void testRemoveWithGCCollector() throws IOException, InterruptedException {
+ void testRemoveWithGCCollector1() throws IOException, InterruptedException {
Config config = Mockito.mock(Config.class);
File baseDir = new File(System.getProperty("java.io.tmpdir"));
File file = Files.createTempDirectory(baseDir.toPath(), "test").toFile();
@@ -173,15 +174,16 @@ class TestKvGarbageCollector {
AuditInfo auditInfo =
AuditInfo.builder().withCreator("creator").withCreateTime(Instant.now()).build();
- BaseMetalake metalake1 = createBaseMakeLake("metalake1", auditInfo);
- CatalogEntity catalog = createCatalog(Namespace.of("metalake1"), "catalog1", auditInfo);
+ BaseMetalake metalake1 = createBaseMakeLake(1L, "metalake1", auditInfo);
+ CatalogEntity catalog = createCatalog(1L, Namespace.of("metalake1"), "catalog1", auditInfo);
SchemaEntity schemaEntity =
- createSchemaEntity(Namespace.of("metalake1", "catalog1"), "schema1", auditInfo);
+ createSchemaEntity(1L, Namespace.of("metalake1", "catalog1"), "schema1", auditInfo);
TableEntity tableEntity =
- createTableEntity(Namespace.of("metalake1", "catalog1", "schema1"), "table1", auditInfo);
+ createTableEntity(
+ 1L, Namespace.of("metalake1", "catalog1", "schema1"), "table1", auditInfo);
FilesetEntity filesetEntity =
createFilesetEntity(
- Namespace.of("metalake1", "catalog1", "schema1"), "fileset1", auditInfo);
+ 1L, Namespace.of("metalake1", "catalog1", "schema1"), "fileset1", auditInfo);
kvEntityStore.put(metalake1);
kvEntityStore.put(catalog);
@@ -234,4 +236,164 @@ class TestKvGarbageCollector {
}
}
}
+
+ @Test
+ void testRemoveWithGCCollector2() throws IOException, InterruptedException {
+ Config config = Mockito.mock(Config.class);
+ File baseDir = new File(System.getProperty("java.io.tmpdir"));
+ File file = Files.createTempDirectory(baseDir.toPath(), "test").toFile();
+ file.deleteOnExit();
+ Mockito.when(config.get(ENTITY_STORE)).thenReturn("kv");
+ Mockito.when(config.get(ENTITY_KV_STORE)).thenReturn(DEFAULT_ENTITY_KV_STORE);
+ Mockito.when(config.get(Configs.ENTITY_SERDE)).thenReturn("proto");
+ Mockito.when(config.get(ENTRY_KV_ROCKSDB_BACKEND_PATH)).thenReturn(file.getAbsolutePath());
+ Mockito.when(config.get(STORE_TRANSACTION_MAX_SKEW_TIME)).thenReturn(1000L);
+ Mockito.when(config.get(KV_DELETE_AFTER_TIME)).thenReturn(20 * 60 * 1000L);
+
+ try (EntityStore store = EntityStoreFactory.createEntityStore(config)) {
+ store.initialize(config);
+
+ if (!(store instanceof KvEntityStore)) {
+ return;
+ }
+ KvEntityStore kvEntityStore = (KvEntityStore) store;
+
+ store.setSerDe(EntitySerDeFactory.createEntitySerDe(config.get(Configs.ENTITY_SERDE)));
+ AuditInfo auditInfo =
+ AuditInfo.builder().withCreator("creator").withCreateTime(Instant.now()).build();
+
+ BaseMetalake metalake1 = createBaseMakeLake(1L, "metalake1", auditInfo);
+ BaseMetalake metalake2 = createBaseMakeLake(2L, "metalake2", auditInfo);
+ BaseMetalake metalake3 = createBaseMakeLake(3L, "metalake3", auditInfo);
+
+ store.put(metalake1);
+ store.put(metalake2);
+ store.put(metalake3);
+
+ store.delete(NameIdentifier.of("metalake1"), Entity.EntityType.METALAKE);
+ store.delete(NameIdentifier.of("metalake2"), Entity.EntityType.METALAKE);
+ store.delete(NameIdentifier.of("metalake3"), Entity.EntityType.METALAKE);
+
+ store.put(metalake1);
+ store.put(metalake2);
+ store.put(metalake3);
+
+ Mockito.when(config.get(KV_DELETE_AFTER_TIME)).thenReturn(1000L);
+ Thread.sleep(1500);
+
+ kvEntityStore.kvGarbageCollector.collectAndClean();
+
+ Assertions.assertDoesNotThrow(
+ () ->
+ store.get(
+ NameIdentifier.of("metalake1"), Entity.EntityType.METALAKE, BaseMetalake.class));
+ Assertions.assertDoesNotThrow(
+ () ->
+ store.get(
+ NameIdentifier.of("metalake2"), Entity.EntityType.METALAKE, BaseMetalake.class));
+ Assertions.assertDoesNotThrow(
+ () ->
+ store.get(
+ NameIdentifier.of("metalake3"), Entity.EntityType.METALAKE, BaseMetalake.class));
+
+ // Test catalog
+ CatalogEntity catalog1 = createCatalog(1L, Namespace.of("metalake1"), "catalog1", auditInfo);
+ CatalogEntity catalog2 = createCatalog(2L, Namespace.of("metalake1"), "catalog2", auditInfo);
+
+ store.put(catalog1);
+ store.put(catalog2);
+
+ store.delete(NameIdentifier.of("metalake1", "catalog1"), Entity.EntityType.CATALOG);
+ store.delete(NameIdentifier.of("metalake1", "catalog2"), Entity.EntityType.CATALOG);
+
+ store.put(catalog1);
+ store.put(catalog2);
+
+ Mockito.when(config.get(KV_DELETE_AFTER_TIME)).thenReturn(1000L);
+ Thread.sleep(1500);
+
+ kvEntityStore.kvGarbageCollector.collectAndClean();
+
+ Assertions.assertDoesNotThrow(
+ () ->
+ store.get(
+ NameIdentifier.of("metalake1", "catalog1"),
+ Entity.EntityType.CATALOG,
+ CatalogEntity.class));
+ Assertions.assertDoesNotThrow(
+ () ->
+ store.get(
+ NameIdentifier.of("metalake1", "catalog2"),
+ Entity.EntityType.CATALOG,
+ CatalogEntity.class));
+
+ // Test schema
+ SchemaEntity schema1 =
+ createSchemaEntity(1L, Namespace.of("metalake1", "catalog2"), "schema1", auditInfo);
+ SchemaEntity schema2 =
+ createSchemaEntity(2L, Namespace.of("metalake1", "catalog2"), "schema2", auditInfo);
+
+ store.put(schema1);
+ store.put(schema2);
+
+ store.delete(NameIdentifier.of("metalake1", "catalog2", "schema1"), Entity.EntityType.SCHEMA);
+ store.delete(NameIdentifier.of("metalake1", "catalog2", "schema2"), Entity.EntityType.SCHEMA);
+
+ store.put(schema1);
+ store.put(schema2);
+
+ Mockito.when(config.get(KV_DELETE_AFTER_TIME)).thenReturn(1000L);
+ Thread.sleep(1500);
+ kvEntityStore.kvGarbageCollector.collectAndClean();
+
+ Assertions.assertDoesNotThrow(
+ () ->
+ store.get(
+ NameIdentifier.of("metalake1", "catalog2", "schema1"),
+ Entity.EntityType.SCHEMA,
+ SchemaEntity.class));
+ Assertions.assertDoesNotThrow(
+ () ->
+ store.get(
+ NameIdentifier.of("metalake1", "catalog2", "schema2"),
+ Entity.EntityType.SCHEMA,
+ SchemaEntity.class));
+
+ // Test table
+ TableEntity table1 =
+ createTableEntity(
+ 1L, Namespace.of("metalake1", "catalog2", "schema2"), "table1", auditInfo);
+ TableEntity table2 =
+ createTableEntity(
+ 2L, Namespace.of("metalake1", "catalog2", "schema2"), "table2", auditInfo);
+
+ store.put(table1);
+ store.put(table2);
+
+ store.delete(
+ NameIdentifier.of("metalake1", "catalog2", "schema2", "table1"), Entity.EntityType.TABLE);
+ store.delete(
+ NameIdentifier.of("metalake1", "catalog2", "schema2", "table2"), Entity.EntityType.TABLE);
+
+ store.put(table1);
+ store.put(table2);
+
+ Mockito.when(config.get(KV_DELETE_AFTER_TIME)).thenReturn(1000L);
+ Thread.sleep(1500);
+ kvEntityStore.kvGarbageCollector.collectAndClean();
+
+ Assertions.assertDoesNotThrow(
+ () ->
+ store.get(
+ NameIdentifier.of("metalake1", "catalog2", "schema2", "table1"),
+ Entity.EntityType.TABLE,
+ TableEntity.class));
+ Assertions.assertDoesNotThrow(
+ () ->
+ store.get(
+ NameIdentifier.of("metalake1", "catalog2", "schema2", "table2"),
+ Entity.EntityType.TABLE,
+ TableEntity.class));
+ }
+ }
}
diff --git a/core/src/test/java/com/datastrato/gravitino/storage/kv/TestStorageVersion.java b/core/src/test/java/com/datastrato/gravitino/storage/kv/TestStorageVersion.java
new file mode 100644
index 0000000000..87a75ee77f
--- /dev/null
+++ b/core/src/test/java/com/datastrato/gravitino/storage/kv/TestStorageVersion.java
@@ -0,0 +1,71 @@
+/*
+ * Copyright 2023 Datastrato Pvt Ltd.
+ * This software is licensed under the Apache License version 2.
+ */
+
+package com.datastrato.gravitino.storage.kv;
+
+import static com.datastrato.gravitino.Configs.DEFAULT_ENTITY_KV_STORE;
+import static com.datastrato.gravitino.Configs.ENTITY_KV_STORE;
+import static com.datastrato.gravitino.Configs.ENTITY_STORE;
+import static com.datastrato.gravitino.Configs.ENTRY_KV_ROCKSDB_BACKEND_PATH;
+import static com.datastrato.gravitino.Configs.KV_DELETE_AFTER_TIME;
+import static com.datastrato.gravitino.Configs.STORE_TRANSACTION_MAX_SKEW_TIME;
+
+import com.datastrato.gravitino.Config;
+import com.datastrato.gravitino.Configs;
+import com.datastrato.gravitino.EntitySerDeFactory;
+import com.datastrato.gravitino.EntityStore;
+import com.datastrato.gravitino.EntityStoreFactory;
+import com.datastrato.gravitino.storage.StorageLayoutException;
+import com.datastrato.gravitino.storage.StorageLayoutVersion;
+import java.io.File;
+import java.io.IOException;
+import java.nio.file.Files;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+import org.mockito.Mockito;
+
+class TestStorageVersion {
+
+ @Test
+ void testFromString() {
+ StorageLayoutVersion version = StorageLayoutVersion.fromString("v1");
+ Assertions.assertEquals(StorageLayoutVersion.V1, version);
+
+ Assertions.assertThrowsExactly(
+ StorageLayoutException.class, () -> StorageLayoutVersion.fromString("v200000.0"));
+ }
+
+ @Test
+ void testStorageLayoutVersion() throws IOException {
+ Config config = Mockito.mock(Config.class);
+ File baseDir = new File(System.getProperty("java.io.tmpdir"));
+ File file = Files.createTempDirectory(baseDir.toPath(), "test").toFile();
+ file.deleteOnExit();
+ Mockito.when(config.get(ENTITY_STORE)).thenReturn("kv");
+ Mockito.when(config.get(ENTITY_KV_STORE)).thenReturn(DEFAULT_ENTITY_KV_STORE);
+ Mockito.when(config.get(Configs.ENTITY_SERDE)).thenReturn("proto");
+ Mockito.when(config.get(ENTRY_KV_ROCKSDB_BACKEND_PATH)).thenReturn(file.getAbsolutePath());
+ Mockito.when(config.get(STORE_TRANSACTION_MAX_SKEW_TIME)).thenReturn(1000L);
+ Mockito.when(config.get(KV_DELETE_AFTER_TIME)).thenReturn(20 * 60 * 1000L);
+
+ // First time create entity store, the storage layout version should be DEFAULT_LAYOUT_VERSION
+ try (EntityStore store = EntityStoreFactory.createEntityStore(config)) {
+ store.initialize(config);
+ Assertions.assertTrue(store instanceof KvEntityStore);
+ store.setSerDe(EntitySerDeFactory.createEntitySerDe(config.get(Configs.ENTITY_SERDE)));
+ KvEntityStore entityStore = (KvEntityStore) store;
+ Assertions.assertEquals(StorageLayoutVersion.V1, entityStore.storageLayoutVersion);
+ }
+
+ // Second time create entity store, the storage layout version should be DEFAULT_LAYOUT_VERSION
+ try (EntityStore store = EntityStoreFactory.createEntityStore(config)) {
+ store.initialize(config);
+ Assertions.assertTrue(store instanceof KvEntityStore);
+ store.setSerDe(EntitySerDeFactory.createEntitySerDe(config.get(Configs.ENTITY_SERDE)));
+ KvEntityStore entityStore = (KvEntityStore) store;
+ Assertions.assertEquals(StorageLayoutVersion.V1, entityStore.storageLayoutVersion);
+ }
+ }
+}
diff --git a/core/src/test/java/com/datastrato/gravitino/TestEntityStore.java b/core/src/test/java/com/datastrato/gravitino/storage/memory/TestMemoryEntityStore.java
similarity index 93%
rename from core/src/test/java/com/datastrato/gravitino/TestEntityStore.java
rename to core/src/test/java/com/datastrato/gravitino/storage/memory/TestMemoryEntityStore.java
index a6d1d46846..ce8d9237fc 100644
--- a/core/src/test/java/com/datastrato/gravitino/TestEntityStore.java
+++ b/core/src/test/java/com/datastrato/gravitino/storage/memory/TestMemoryEntityStore.java
@@ -2,9 +2,19 @@
* Copyright 2023 Datastrato Pvt Ltd.
* This software is licensed under the Apache License version 2.
*/
-package com.datastrato.gravitino;
+package com.datastrato.gravitino.storage.memory;
+import com.datastrato.gravitino.Config;
+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.EntityStore;
+import com.datastrato.gravitino.HasIdentifier;
+import com.datastrato.gravitino.Metalake;
+import com.datastrato.gravitino.NameIdentifier;
+import com.datastrato.gravitino.Namespace;
+import com.datastrato.gravitino.TestCatalog;
import com.datastrato.gravitino.exceptions.NoSuchEntityException;
import com.datastrato.gravitino.file.Fileset;
import com.datastrato.gravitino.meta.AuditInfo;
@@ -28,7 +38,7 @@ import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
-public class TestEntityStore {
+public class TestMemoryEntityStore {
public static class InMemoryEntityStore implements EntityStore {
private final Map<NameIdentifier, Entity> entityMap;
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
deleted file mode 100644
index 58430308ff..0000000000
--- a/core/src/test/java/com/datastrato/gravitino/storage/relational/TestRelationalEntityStore.java
+++ /dev/null
@@ -1,1547 +0,0 @@
-/*
- * 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 static com.datastrato.gravitino.Configs.ENTITY_RELATIONAL_JDBC_BACKEND_DRIVER;
-import static com.datastrato.gravitino.Configs.ENTITY_RELATIONAL_JDBC_BACKEND_PASSWORD;
-import static com.datastrato.gravitino.Configs.ENTITY_RELATIONAL_JDBC_BACKEND_URL;
-import static com.datastrato.gravitino.Configs.ENTITY_RELATIONAL_JDBC_BACKEND_USER;
-import static com.datastrato.gravitino.Configs.ENTITY_RELATIONAL_STORE;
-import static com.datastrato.gravitino.Configs.ENTITY_STORE;
-import static com.datastrato.gravitino.Configs.RELATIONAL_ENTITY_STORE;
-import static org.junit.jupiter.api.Assertions.assertEquals;
-import static org.junit.jupiter.api.Assertions.assertFalse;
-import static org.junit.jupiter.api.Assertions.assertNotNull;
-import static org.junit.jupiter.api.Assertions.assertThrows;
-import static org.junit.jupiter.api.Assertions.assertTrue;
-
-import com.datastrato.gravitino.Catalog;
-import com.datastrato.gravitino.Config;
-import com.datastrato.gravitino.Entity;
-import com.datastrato.gravitino.EntityAlreadyExistsException;
-import com.datastrato.gravitino.EntityStore;
-import com.datastrato.gravitino.EntityStoreFactory;
-import com.datastrato.gravitino.Namespace;
-import com.datastrato.gravitino.exceptions.NoSuchEntityException;
-import com.datastrato.gravitino.exceptions.NonEmptyEntityException;
-import com.datastrato.gravitino.file.Fileset;
-import com.datastrato.gravitino.meta.AuditInfo;
-import com.datastrato.gravitino.meta.BaseMetalake;
-import com.datastrato.gravitino.meta.CatalogEntity;
-import com.datastrato.gravitino.meta.FilesetEntity;
-import com.datastrato.gravitino.meta.SchemaEntity;
-import com.datastrato.gravitino.meta.SchemaVersion;
-import com.datastrato.gravitino.meta.TableEntity;
-import com.datastrato.gravitino.storage.relational.session.SqlSessionFactoryHelper;
-import java.io.BufferedReader;
-import java.io.File;
-import java.io.IOException;
-import java.io.InputStreamReader;
-import java.net.URL;
-import java.nio.charset.StandardCharsets;
-import java.nio.file.Files;
-import java.nio.file.Paths;
-import java.sql.Connection;
-import java.sql.ResultSet;
-import java.sql.SQLException;
-import java.sql.Statement;
-import java.time.Instant;
-import java.util.ArrayList;
-import java.util.Comparator;
-import java.util.HashMap;
-import java.util.List;
-import java.util.UUID;
-import java.util.stream.Collectors;
-import org.apache.ibatis.session.SqlSession;
-import org.junit.jupiter.api.AfterAll;
-import org.junit.jupiter.api.AfterEach;
-import org.junit.jupiter.api.BeforeAll;
-import org.junit.jupiter.api.Test;
-import org.mockito.Mockito;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-public class TestRelationalEntityStore {
- private static final Logger Logger = LoggerFactory.getLogger(TestRelationalEntityStore.class);
- private static final String MYSQL_STORE_PATH =
- "/tmp/gravitino_test_entityStore_" + UUID.randomUUID().toString().replace("-", "");
- private static final String DB_DIR = MYSQL_STORE_PATH + "/testdb";
- private static EntityStore entityStore = null;
-
- @BeforeAll
- public static void setUp() {
- File dir = new File(DB_DIR);
- if (dir.exists() || !dir.isDirectory()) {
- dir.delete();
- }
- dir.mkdirs();
-
- // Use H2 DATABASE to simulate MySQL
- Config config = Mockito.mock(Config.class);
- Mockito.when(config.get(ENTITY_STORE)).thenReturn(RELATIONAL_ENTITY_STORE);
- Mockito.when(config.get(ENTITY_RELATIONAL_STORE)).thenReturn(DEFAULT_ENTITY_RELATIONAL_STORE);
- Mockito.when(config.get(ENTITY_RELATIONAL_JDBC_BACKEND_URL))
- .thenReturn(String.format("jdbc:h2:%s;DB_CLOSE_DELAY=-1;MODE=MYSQL", DB_DIR));
- Mockito.when(config.get(ENTITY_RELATIONAL_JDBC_BACKEND_USER)).thenReturn("root");
- Mockito.when(config.get(ENTITY_RELATIONAL_JDBC_BACKEND_PASSWORD)).thenReturn("123");
- Mockito.when(config.get(ENTITY_RELATIONAL_JDBC_BACKEND_DRIVER)).thenReturn("org.h2.Driver");
- entityStore = EntityStoreFactory.createEntityStore(config);
- entityStore.initialize(config);
-
- // Read the ddl sql to create table
- String scriptPath = "h2/h2-init.sql";
- try (SqlSession sqlSession =
- SqlSessionFactoryHelper.getInstance().getSqlSessionFactory().openSession(true);
- Connection connection = sqlSession.getConnection();
- Statement statement = connection.createStatement()) {
- URL scriptUrl = ClassLoader.getSystemResource(scriptPath);
- if (scriptUrl == null) {
- throw new IllegalStateException("Cannot find init sql script:" + scriptPath);
- }
- StringBuilder ddlBuilder = new StringBuilder();
- try (InputStreamReader inputStreamReader =
- new InputStreamReader(
- Files.newInputStream(Paths.get(scriptUrl.getPath())), StandardCharsets.UTF_8);
- BufferedReader bufferedReader = new BufferedReader(inputStreamReader)) {
- String line;
- while ((line = bufferedReader.readLine()) != null) {
- ddlBuilder.append(line).append("\n");
- }
- }
- statement.execute(ddlBuilder.toString());
- } catch (Exception e) {
- throw new IllegalStateException("Create tables failed", e);
- }
- }
-
- @AfterEach
- public void destroy() {
- truncateAllTables();
- }
-
- @AfterAll
- public static void tearDown() {
- dropAllTables();
- try {
- entityStore.close();
- } catch (IOException e) {
- Logger.error("Close the entity store failed:", e);
- }
-
- File dir = new File(DB_DIR);
- if (dir.exists()) {
- dir.delete();
- }
- }
-
- @Test
- public void testMetalakePutAndGet() throws IOException {
- BaseMetalake metalake = createMetalake(1L, "test_metalake", "this is test");
- entityStore.put(metalake, false);
- BaseMetalake insertedMetalake =
- entityStore.get(metalake.nameIdentifier(), Entity.EntityType.METALAKE, BaseMetalake.class);
- assertNotNull(insertedMetalake);
- assertTrue(checkMetalakeEquals(metalake, insertedMetalake));
-
- // overwrite false
- BaseMetalake duplicateMetalake = createMetalake(1L, "test_metalake", "this is test");
- assertThrows(
- EntityAlreadyExistsException.class, () -> entityStore.put(duplicateMetalake, false));
-
- // overwrite true
- BaseMetalake overwrittenMetalake = createMetalake(1L, "test_metalake2", "this is test2");
- entityStore.put(overwrittenMetalake, true);
- BaseMetalake insertedMetalake1 =
- entityStore.get(
- overwrittenMetalake.nameIdentifier(), Entity.EntityType.METALAKE, BaseMetalake.class);
- assertEquals(
- 1,
- entityStore.list(Namespace.empty(), BaseMetalake.class, Entity.EntityType.METALAKE).size());
- assertEquals(overwrittenMetalake.name(), insertedMetalake1.name());
- assertEquals(overwrittenMetalake.comment(), insertedMetalake1.comment());
- }
-
- @Test
- public void testCatalogPutAndGet() throws IOException {
- BaseMetalake metalake = createMetalake(1L, "test_metalake", "this is test");
- entityStore.put(metalake, false);
-
- CatalogEntity catalog =
- createCatalog(
- 1L, "test_catalog", Namespace.ofCatalog(metalake.name()), "this is catalog test");
- entityStore.put(catalog, false);
-
- CatalogEntity insertedCatalog =
- entityStore.get(catalog.nameIdentifier(), Entity.EntityType.CATALOG, CatalogEntity.class);
- assertNotNull(insertedCatalog);
- assertTrue(checkCatalogEquals(catalog, insertedCatalog));
-
- // overwrite false
- CatalogEntity duplicateCatalog =
- createCatalog(
- 1L, "test_catalog", Namespace.ofCatalog(metalake.name()), "this is catalog test");
- assertThrows(
- EntityAlreadyExistsException.class, () -> entityStore.put(duplicateCatalog, false));
-
- // overwrite true
- CatalogEntity overwrittenCatalog =
- createCatalog(
- 1L, "test_catalog1", Namespace.ofCatalog(metalake.name()), "this is catalog test1");
- entityStore.put(overwrittenCatalog, true);
- CatalogEntity insertedCatalog1 =
- entityStore.get(
- overwrittenCatalog.nameIdentifier(), Entity.EntityType.CATALOG, CatalogEntity.class);
- assertEquals(
- 1,
- entityStore
- .list(overwrittenCatalog.namespace(), CatalogEntity.class, Entity.EntityType.CATALOG)
- .size());
- assertEquals(overwrittenCatalog.name(), insertedCatalog1.name());
- assertEquals(overwrittenCatalog.getComment(), insertedCatalog1.getComment());
- }
-
- @Test
- public void testSchemaPutAndGet() throws IOException {
- BaseMetalake metalake = createMetalake(1L, "test_metalake", "this is test");
- entityStore.put(metalake, false);
-
- CatalogEntity catalog =
- createCatalog(1L, "test_catalog", Namespace.ofCatalog(metalake.name()), "this is test");
- entityStore.put(catalog, false);
-
- SchemaEntity schema =
- createSchema(
- 1L,
- "test_schema",
- Namespace.ofSchema(metalake.name(), catalog.name()),
- "this is schema test");
- entityStore.put(schema, false);
-
- SchemaEntity insertedSchema =
- entityStore.get(schema.nameIdentifier(), Entity.EntityType.SCHEMA, SchemaEntity.class);
- assertNotNull(insertedSchema);
- assertTrue(checkSchemaEquals(schema, insertedSchema));
-
- // overwrite false
- SchemaEntity duplicateSchema =
- createSchema(
- 1L,
- "test_schema",
- Namespace.ofSchema(metalake.name(), catalog.name()),
- "this is schema test");
- assertThrows(EntityAlreadyExistsException.class, () -> entityStore.put(duplicateSchema, false));
-
- // overwrite true
- SchemaEntity overwrittenSchema =
- createSchema(
- 1L,
- "test_schema1",
- Namespace.ofSchema(metalake.name(), catalog.name()),
- "this is schema test1");
- entityStore.put(overwrittenSchema, true);
- SchemaEntity insertedSchema1 =
- entityStore.get(
- overwrittenSchema.nameIdentifier(), Entity.EntityType.SCHEMA, SchemaEntity.class);
-
- assertEquals(
- 1,
- entityStore
- .list(insertedSchema1.namespace(), SchemaEntity.class, Entity.EntityType.SCHEMA)
- .size());
- assertEquals(overwrittenSchema.name(), insertedSchema1.name());
- assertEquals(overwrittenSchema.comment(), insertedSchema1.comment());
- }
-
- @Test
- public void testTablePutAndGet() throws IOException {
- BaseMetalake metalake = createMetalake(1L, "test_metalake", "this is test");
- entityStore.put(metalake, false);
-
- CatalogEntity catalog =
- createCatalog(1L, "test_catalog", Namespace.ofCatalog(metalake.name()), "this is test");
- entityStore.put(catalog, false);
-
- SchemaEntity schema =
- createSchema(
- 1L,
- "test_schema",
- Namespace.ofSchema(metalake.name(), catalog.name()),
- "this is schema test");
- entityStore.put(schema, false);
-
- TableEntity table =
- createTable(
- 1L, "test_table", Namespace.ofTable(metalake.name(), catalog.name(), schema.name()));
- entityStore.put(table, false);
-
- TableEntity insertedTable =
- entityStore.get(table.nameIdentifier(), Entity.EntityType.TABLE, TableEntity.class);
- assertNotNull(insertedTable);
- assertTrue(checkTableEquals(table, insertedTable));
-
- // overwrite false
- TableEntity duplicateTable =
- createTable(
- 1L, "test_table", Namespace.ofTable(metalake.name(), catalog.name(), schema.name()));
- assertThrows(EntityAlreadyExistsException.class, () -> entityStore.put(duplicateTable, false));
-
- // overwrite true
- TableEntity overwrittenTable =
- createTable(
- 1L, "test_table1", Namespace.ofTable(metalake.name(), catalog.name(), schema.name()));
- entityStore.put(overwrittenTable, true);
- TableEntity insertedTable1 =
- entityStore.get(
- overwrittenTable.nameIdentifier(), Entity.EntityType.TABLE, TableEntity.class);
-
- assertEquals(
- 1,
- entityStore
- .list(insertedTable1.namespace(), TableEntity.class, Entity.EntityType.TABLE)
- .size());
- assertEquals(overwrittenTable.name(), insertedTable1.name());
- assertEquals(overwrittenTable.auditInfo().creator(), insertedTable1.auditInfo().creator());
- }
-
- @Test
- public void testFilesetPutAndGet() throws IOException {
- BaseMetalake metalake = createMetalake(1L, "test_metalake", "this is test");
- entityStore.put(metalake, false);
-
- CatalogEntity catalog =
- createCatalog(1L, "test_catalog", Namespace.ofCatalog(metalake.name()), "this is test");
- entityStore.put(catalog, false);
-
- SchemaEntity schema =
- createSchema(
- 1L,
- "test_schema",
- Namespace.ofSchema(metalake.name(), catalog.name()),
- "this is schema test");
- entityStore.put(schema, false);
-
- FilesetEntity filesetEntity =
- createFileset(
- 1L,
- "test_fileset",
- Namespace.ofFileset(metalake.name(), catalog.name(), schema.name()),
- "this is fileset test",
- "hdfs://localhost/test");
- entityStore.put(filesetEntity, false);
-
- FilesetEntity insertedFileset =
- entityStore.get(
- filesetEntity.nameIdentifier(), Entity.EntityType.FILESET, FilesetEntity.class);
- assertNotNull(insertedFileset);
- assertTrue(checkFilesetEquals(filesetEntity, insertedFileset));
-
- // overwrite false
- FilesetEntity duplicateFileset =
- createFileset(
- 1L,
- "test_fileset",
- Namespace.ofFileset(metalake.name(), catalog.name(), schema.name()),
- "this is fileset test",
- "hdfs://localhost/test");
- assertThrows(
- EntityAlreadyExistsException.class, () -> entityStore.put(duplicateFileset, false));
-
- // overwrite true
- FilesetEntity overwrittenFileset =
- createFileset(
- 1L,
- "test_fileset1",
- Namespace.ofFileset(metalake.name(), catalog.name(), schema.name()),
- "this is fileset test1",
- "hdfs://localhost/test1");
- entityStore.put(overwrittenFileset, true);
- FilesetEntity insertedFileset1 =
- entityStore.get(
- overwrittenFileset.nameIdentifier(), Entity.EntityType.FILESET, FilesetEntity.class);
-
- assertEquals(
- 1,
- entityStore
- .list(insertedFileset1.namespace(), FilesetEntity.class, Entity.EntityType.FILESET)
- .size());
- checkFilesetEquals(overwrittenFileset, insertedFileset1);
- }
-
- @Test
- public void testMetalakePutAndList() throws IOException {
- BaseMetalake metalake1 = createMetalake(1L, "test_metalake1", "this is test 1");
- BaseMetalake metalake2 = createMetalake(2L, "test_metalake2", "this is test 2");
- List<BaseMetalake> beforePutList =
- entityStore.list(metalake1.namespace(), BaseMetalake.class, Entity.EntityType.METALAKE);
- assertNotNull(beforePutList);
- assertEquals(0, beforePutList.size());
-
- entityStore.put(metalake1, false);
- entityStore.put(metalake2, false);
- List<BaseMetalake> metalakes =
- entityStore.list(metalake1.namespace(), BaseMetalake.class, Entity.EntityType.METALAKE)
- .stream()
- .sorted(Comparator.comparing(BaseMetalake::id))
- .collect(Collectors.toList());
- assertNotNull(metalakes);
- assertEquals(2, metalakes.size());
- assertTrue(checkMetalakeEquals(metalake1, metalakes.get(0)));
- assertTrue(checkMetalakeEquals(metalake2, metalakes.get(1)));
- }
-
- @Test
- public void testCatalogPutAndList() throws IOException {
- BaseMetalake metalake = createMetalake(1L, "test_metalake", "this is test 1");
- entityStore.put(metalake, false);
-
- CatalogEntity catalog1 =
- createCatalog(
- 1L, "test_catalog1", Namespace.ofCatalog(metalake.name()), "this is catalog 1");
- CatalogEntity catalog2 =
- createCatalog(
- 2L, "test_catalog2", Namespace.ofCatalog(metalake.name()), "this is catalog 2");
- List<CatalogEntity> beforeCatalogList =
- entityStore.list(catalog1.namespace(), CatalogEntity.class, Entity.EntityType.CATALOG);
- assertNotNull(beforeCatalogList);
- assertEquals(0, beforeCatalogList.size());
-
- entityStore.put(catalog1, false);
- entityStore.put(catalog2, false);
- List<CatalogEntity> catalogEntities =
- entityStore.list(catalog1.namespace(), CatalogEntity.class, Entity.EntityType.CATALOG)
- .stream()
- .sorted(Comparator.comparing(CatalogEntity::id))
- .collect(Collectors.toList());
- assertNotNull(catalogEntities);
- assertEquals(2, catalogEntities.size());
- assertTrue(checkCatalogEquals(catalog1, catalogEntities.get(0)));
- assertTrue(checkCatalogEquals(catalog2, catalogEntities.get(1)));
- }
-
- @Test
- public void testSchemaPutAndList() throws IOException {
- BaseMetalake metalake = createMetalake(1L, "test_metalake", "this is test 1");
- entityStore.put(metalake, false);
-
- CatalogEntity catalog =
- createCatalog(1L, "test_catalog", Namespace.ofCatalog(metalake.name()), "this is test");
- entityStore.put(catalog, false);
-
- SchemaEntity schema1 =
- createSchema(
- 1L,
- "test_schema1",
- Namespace.ofSchema(metalake.name(), catalog.name()),
- "this is schema 1");
- SchemaEntity schema2 =
- createSchema(
- 2L,
- "test_schema2",
- Namespace.ofSchema(metalake.name(), catalog.name()),
- "this is schema 2");
- List<SchemaEntity> beforeSchemaList =
- entityStore.list(schema1.namespace(), SchemaEntity.class, Entity.EntityType.SCHEMA);
- assertNotNull(beforeSchemaList);
- assertEquals(0, beforeSchemaList.size());
-
- entityStore.put(schema1, false);
- entityStore.put(schema2, false);
- List<SchemaEntity> schemaEntities =
- entityStore.list(schema1.namespace(), SchemaEntity.class, Entity.EntityType.SCHEMA).stream()
- .sorted(Comparator.comparing(SchemaEntity::id))
- .collect(Collectors.toList());
- assertNotNull(schemaEntities);
- assertEquals(2, schemaEntities.size());
- assertTrue(checkSchemaEquals(schema1, schemaEntities.get(0)));
- assertTrue(checkSchemaEquals(schema2, schemaEntities.get(1)));
- }
-
- @Test
- public void testTablePutAndList() throws IOException {
- BaseMetalake metalake = createMetalake(1L, "test_metalake", "this is test 1");
- entityStore.put(metalake, false);
-
- CatalogEntity catalog =
- createCatalog(1L, "test_catalog", Namespace.ofCatalog(metalake.name()), "this is test");
- entityStore.put(catalog, false);
-
- SchemaEntity schema =
- createSchema(
- 1L, "test_schema", Namespace.ofSchema(metalake.name(), catalog.name()), "this is test");
- entityStore.put(schema, false);
-
- TableEntity table1 =
- createTable(
- 1L, "test_table1", Namespace.ofTable(metalake.name(), catalog.name(), schema.name()));
- TableEntity table2 =
- createTable(
- 2L, "test_table2", Namespace.ofTable(metalake.name(), catalog.name(), schema.name()));
-
- List<TableEntity> beforeTableList =
- entityStore.list(table1.namespace(), TableEntity.class, Entity.EntityType.TABLE);
- assertNotNull(beforeTableList);
- assertEquals(0, beforeTableList.size());
-
- entityStore.put(table1, false);
- entityStore.put(table2, false);
- List<TableEntity> tableEntities =
- entityStore.list(table1.namespace(), TableEntity.class, Entity.EntityType.TABLE).stream()
- .sorted(Comparator.comparing(TableEntity::id))
- .collect(Collectors.toList());
- assertNotNull(tableEntities);
- assertEquals(2, tableEntities.size());
- assertTrue(checkTableEquals(table1, tableEntities.get(0)));
- assertTrue(checkTableEquals(table2, tableEntities.get(1)));
- }
-
- @Test
- public void testFilesetPutAndList() throws IOException {
- BaseMetalake metalake = createMetalake(1L, "test_metalake", "this is test 1");
- entityStore.put(metalake, false);
-
- CatalogEntity catalog =
- createCatalog(1L, "test_catalog", Namespace.ofCatalog(metalake.name()), "this is test");
- entityStore.put(catalog, false);
-
- SchemaEntity schema =
- createSchema(
- 1L, "test_schema", Namespace.ofSchema(metalake.name(), catalog.name()), "this is test");
- entityStore.put(schema, false);
-
- FilesetEntity fileset1 =
- createFileset(
- 1L,
- "test_filese1",
- Namespace.ofFileset(metalake.name(), catalog.name(), schema.name()),
- "this is fileset 1",
- "hdfs://localhost/test1");
- FilesetEntity fileset2 =
- createFileset(
- 2L,
- "test_filese2",
- Namespace.ofFileset(metalake.name(), catalog.name(), schema.name()),
- "this is fileset 2",
- "hdfs://localhost/test2");
-
- List<FilesetEntity> beforeFilesetList =
- entityStore.list(fileset1.namespace(), FilesetEntity.class, Entity.EntityType.FILESET);
- assertNotNull(beforeFilesetList);
- assertEquals(0, beforeFilesetList.size());
-
- entityStore.put(fileset1, false);
- entityStore.put(fileset2, false);
- List<FilesetEntity> filesetEntities =
- entityStore.list(fileset1.namespace(), FilesetEntity.class, Entity.EntityType.FILESET)
- .stream()
- .sorted(Comparator.comparing(FilesetEntity::id))
- .collect(Collectors.toList());
- assertNotNull(filesetEntities);
- assertEquals(2, filesetEntities.size());
- assertTrue(checkFilesetEquals(fileset1, filesetEntities.get(0)));
- assertTrue(checkFilesetEquals(fileset2, filesetEntities.get(1)));
- }
-
- @Test
- public void testMetalakePutAndDelete() throws IOException, InterruptedException {
- BaseMetalake metalake = createMetalake(1L, "test_metalake", "this is test");
- entityStore.put(metalake, false);
-
- assertNotNull(
- entityStore.get(metalake.nameIdentifier(), Entity.EntityType.METALAKE, BaseMetalake.class));
- entityStore.delete(metalake.nameIdentifier(), Entity.EntityType.METALAKE, false);
-
- assertThrows(
- NoSuchEntityException.class,
- () ->
- entityStore.get(
- metalake.nameIdentifier(), Entity.EntityType.METALAKE, BaseMetalake.class));
-
- // sleep 1s to make delete_at seconds differently
- Thread.sleep(1000);
-
- // test cascade delete
- BaseMetalake metalake1 = createMetalake(2L, "test_metalake1", "this is test");
- entityStore.put(metalake1, false);
-
- CatalogEntity subCatalog =
- createCatalog(
- 1L, "test_catalog", Namespace.ofCatalog(metalake1.name()), "test cascade deleted");
- entityStore.put(subCatalog, false);
-
- SchemaEntity subSchema =
- createSchema(
- 1L,
- "test_schema",
- Namespace.ofSchema(metalake1.name(), subCatalog.name()),
- "test cascade deleted");
- entityStore.put(subSchema, false);
-
- TableEntity subTable =
- createTable(
- 1L,
- "test_table",
- Namespace.ofTable(metalake1.name(), subCatalog.name(), subSchema.name()));
- entityStore.put(subTable, false);
-
- FilesetEntity subFileset =
- createFileset(
- 1L,
- "test_fileset",
- Namespace.ofFileset(metalake1.name(), subCatalog.name(), subSchema.name()),
- "test cascade deleted",
- "hdfs://localhost/test");
- entityStore.put(subFileset, false);
-
- // cascade is false
- assertThrows(
- NonEmptyEntityException.class,
- () -> entityStore.delete(metalake1.nameIdentifier(), Entity.EntityType.METALAKE, false));
-
- // cascade is true
- entityStore.delete(metalake1.nameIdentifier(), Entity.EntityType.METALAKE, true);
- assertFalse(entityStore.exists(metalake1.nameIdentifier(), Entity.EntityType.METALAKE));
- assertFalse(entityStore.exists(subCatalog.nameIdentifier(), Entity.EntityType.CATALOG));
- assertFalse(entityStore.exists(subSchema.nameIdentifier(), Entity.EntityType.SCHEMA));
- assertFalse(entityStore.exists(subTable.nameIdentifier(), Entity.EntityType.TABLE));
- assertFalse(entityStore.exists(subFileset.nameIdentifier(), Entity.EntityType.FILESET));
- }
-
- @Test
- public void testCatalogPutAndDelete() throws IOException, InterruptedException {
- BaseMetalake metalake = createMetalake(3L, "test_metalake", "this is test");
- entityStore.put(metalake, false);
-
- CatalogEntity catalog =
- createCatalog(2L, "test_catalog", Namespace.ofCatalog(metalake.name()), "this is test");
- entityStore.put(catalog, false);
-
- assertNotNull(
- entityStore.get(catalog.nameIdentifier(), Entity.EntityType.CATALOG, CatalogEntity.class));
- entityStore.delete(catalog.nameIdentifier(), Entity.EntityType.CATALOG, false);
-
- assertThrows(
- NoSuchEntityException.class,
- () ->
- entityStore.get(
- catalog.nameIdentifier(), Entity.EntityType.CATALOG, CatalogEntity.class));
-
- // sleep 1s to make delete_at seconds differently
- Thread.sleep(1000);
-
- // test cascade delete
- CatalogEntity catalog1 =
- createCatalog(
- 3L, "test_catalog1", Namespace.ofCatalog(metalake.name()), "test cascade deleted");
- entityStore.put(catalog1, false);
-
- SchemaEntity subSchema =
- createSchema(
- 1L,
- "test_schema",
- Namespace.ofSchema(metalake.name(), catalog1.name()),
- "test cascade deleted");
- entityStore.put(subSchema, false);
-
- TableEntity subTable =
- createTable(
- 1L,
- "test_table",
- Namespace.ofTable(metalake.name(), catalog1.name(), subSchema.name()));
- entityStore.put(subTable, false);
-
- FilesetEntity subFileset =
- createFileset(
- 1L,
- "test_fileset",
- Namespace.ofFileset(metalake.name(), catalog1.name(), subSchema.name()),
- "test cascade deleted",
- "hdfs://localhost/test");
- entityStore.put(subFileset, false);
-
- // cascade is false
- assertThrows(
- NonEmptyEntityException.class,
- () -> entityStore.delete(catalog1.nameIdentifier(), Entity.EntityType.CATALOG, false));
-
- // cascade is true
- entityStore.delete(catalog1.nameIdentifier(), Entity.EntityType.CATALOG, true);
- assertFalse(entityStore.exists(catalog1.nameIdentifier(), Entity.EntityType.CATALOG));
- assertFalse(entityStore.exists(subSchema.nameIdentifier(), Entity.EntityType.SCHEMA));
- assertFalse(entityStore.exists(subTable.nameIdentifier(), Entity.EntityType.TABLE));
- assertFalse(entityStore.exists(subFileset.nameIdentifier(), Entity.EntityType.FILESET));
- }
-
- @Test
- public void testSchemaPutAndDelete() throws IOException, InterruptedException {
- BaseMetalake metalake = createMetalake(3L, "test_metalake", "this is test");
- entityStore.put(metalake, false);
-
- CatalogEntity catalog =
- createCatalog(2L, "test_catalog", Namespace.ofCatalog(metalake.name()), "this is test");
- entityStore.put(catalog, false);
-
- SchemaEntity schema =
- createSchema(
- 2L, "test_schema", Namespace.ofSchema(metalake.name(), catalog.name()), "this is test");
- entityStore.put(schema, false);
-
- assertNotNull(
- entityStore.get(schema.nameIdentifier(), Entity.EntityType.SCHEMA, SchemaEntity.class));
- entityStore.delete(schema.nameIdentifier(), Entity.EntityType.SCHEMA, false);
-
- assertThrows(
- NoSuchEntityException.class,
- () ->
- entityStore.get(schema.nameIdentifier(), Entity.EntityType.SCHEMA, SchemaEntity.class));
-
- // sleep 1s to make delete_at seconds differently
- Thread.sleep(1000);
-
- // test cascade delete
- SchemaEntity schema1 =
- createSchema(
- 3L,
- "test_schema1",
- Namespace.ofSchema(metalake.name(), catalog.name()),
- "test cascade deleted");
- entityStore.put(schema1, false);
-
- TableEntity subTable =
- createTable(
- 1L, "test_table", Namespace.ofTable(metalake.name(), catalog.name(), schema1.name()));
- entityStore.put(subTable, false);
-
- FilesetEntity subFileset =
- createFileset(
- 1L,
- "test_fileset",
- Namespace.ofFileset(metalake.name(), catalog.name(), schema1.name()),
- "test cascade deleted",
- "hdfs://localhost/test");
- entityStore.put(subFileset, false);
-
- // cascade is false
- assertThrows(
- NonEmptyEntityException.class,
- () -> entityStore.delete(schema1.nameIdentifier(), Entity.EntityType.SCHEMA, false));
-
- // cascade is true
- entityStore.delete(schema1.nameIdentifier(), Entity.EntityType.SCHEMA, true);
- assertFalse(entityStore.exists(schema1.nameIdentifier(), Entity.EntityType.SCHEMA));
- assertFalse(entityStore.exists(subTable.nameIdentifier(), Entity.EntityType.TABLE));
- assertFalse(entityStore.exists(subFileset.nameIdentifier(), Entity.EntityType.FILESET));
- }
-
- @Test
- public void testTablePutAndDelete() throws IOException {
- BaseMetalake metalake = createMetalake(3L, "test_metalake", "this is test");
- entityStore.put(metalake, false);
-
- CatalogEntity catalog =
- createCatalog(2L, "test_catalog", Namespace.ofCatalog(metalake.name()), "this is test");
- entityStore.put(catalog, false);
-
- SchemaEntity schema =
- createSchema(
- 2L, "test_schema", Namespace.ofSchema(metalake.name(), catalog.name()), "this is test");
- entityStore.put(schema, false);
-
- TableEntity table =
- createTable(
- 2L, "test_table", Namespace.ofTable(metalake.name(), catalog.name(), schema.name()));
- entityStore.put(table, false);
-
- assertNotNull(
- entityStore.get(table.nameIdentifier(), Entity.EntityType.TABLE, TableEntity.class));
- entityStore.delete(table.nameIdentifier(), Entity.EntityType.TABLE, false);
-
- assertThrows(
- NoSuchEntityException.class,
- () -> entityStore.get(table.nameIdentifier(), Entity.EntityType.TABLE, TableEntity.class));
- }
-
- @Test
- public void testFilesetPutAndDelete() throws IOException, InterruptedException {
- BaseMetalake metalake = createMetalake(3L, "test_metalake", "this is test");
- entityStore.put(metalake, false);
-
- CatalogEntity catalog =
- createCatalog(2L, "test_catalog", Namespace.ofCatalog(metalake.name()), "this is test");
- entityStore.put(catalog, false);
-
- SchemaEntity schema =
- createSchema(
- 2L, "test_schema", Namespace.ofSchema(metalake.name(), catalog.name()), "this is test");
- entityStore.put(schema, false);
-
- FilesetEntity fileset =
- createFileset(
- 2L,
- "test_fileset",
- Namespace.ofFileset(metalake.name(), catalog.name(), schema.name()),
- "this is fileset test",
- "hdfs://localhost/test");
- entityStore.put(fileset, false);
-
- assertNotNull(
- entityStore.get(fileset.nameIdentifier(), Entity.EntityType.FILESET, FilesetEntity.class));
- entityStore.delete(fileset.nameIdentifier(), Entity.EntityType.FILESET, false);
-
- assertThrows(
- NoSuchEntityException.class,
- () ->
- entityStore.get(
- fileset.nameIdentifier(), Entity.EntityType.FILESET, FilesetEntity.class));
- }
-
- @Test
- public void testMetalakePutAndUpdate() throws IOException {
- BaseMetalake metalake = createMetalake(1L, "test_metalake", "this is test");
- entityStore.put(metalake, false);
-
- assertThrows(
- RuntimeException.class,
- () ->
- entityStore.update(
- metalake.nameIdentifier(),
- BaseMetalake.class,
- Entity.EntityType.METALAKE,
- m -> {
- BaseMetalake.Builder builder =
- BaseMetalake.builder()
- // Change the id, which is not allowed
- .withId(2L)
- .withName("test_metalake2")
- .withComment("this is test 2")
- .withProperties(new HashMap<>())
- .withAuditInfo((AuditInfo) m.auditInfo())
- .withVersion(m.getVersion());
- return builder.build();
- }));
-
- AuditInfo changedAuditInfo =
- AuditInfo.builder().withCreator("changed_creator").withCreateTime(Instant.now()).build();
- BaseMetalake updatedMetalake =
- entityStore.update(
- metalake.nameIdentifier(),
- BaseMetalake.class,
- Entity.EntityType.METALAKE,
- m -> {
- BaseMetalake.Builder builder =
- BaseMetalake.builder()
- .withId(m.id())
- .withName("test_metalake2")
- .withComment("this is test 2")
- .withProperties(new HashMap<>())
- .withAuditInfo(changedAuditInfo)
- .withVersion(m.getVersion());
- return builder.build();
- });
-
- BaseMetalake storedMetalake =
- entityStore.get(
- updatedMetalake.nameIdentifier(), Entity.EntityType.METALAKE, BaseMetalake.class);
- assertEquals(metalake.id(), storedMetalake.id());
- assertEquals("test_metalake2", updatedMetalake.name());
- assertEquals("this is test 2", updatedMetalake.comment());
- assertEquals(changedAuditInfo.creator(), updatedMetalake.auditInfo().creator());
-
- BaseMetalake metalake3 = createMetalake(3L, "test_metalake3", "this is test 3");
- entityStore.put(metalake3, false);
-
- assertThrows(
- EntityAlreadyExistsException.class,
- () ->
- entityStore.update(
- metalake3.nameIdentifier(),
- BaseMetalake.class,
- Entity.EntityType.METALAKE,
- m -> {
- BaseMetalake.Builder builder =
- BaseMetalake.builder()
- .withId(metalake3.id())
- // metalake name already exists
- .withName("test_metalake2")
- .withComment(metalake3.comment())
- .withProperties(new HashMap<>())
- .withAuditInfo((AuditInfo) m.auditInfo())
- .withVersion(m.getVersion());
- return builder.build();
- }));
- }
-
- @Test
- public void testCatalogPutAndUpdate() throws IOException {
- BaseMetalake metalake = createMetalake(1L, "test_metalake", "this is test");
- entityStore.put(metalake, false);
-
- CatalogEntity catalog =
- createCatalog(
- 1L, "test_catalog", Namespace.ofCatalog(metalake.name()), "this is catalog test");
- entityStore.put(catalog, false);
-
- assertThrows(
- RuntimeException.class,
- () ->
- entityStore.update(
- catalog.nameIdentifier(),
- CatalogEntity.class,
- Entity.EntityType.CATALOG,
- c -> {
- CatalogEntity.Builder builder =
- CatalogEntity.builder()
- // Change the id, which is not allowed
- .withId(2L)
- .withName("test_catalog2")
- .withNamespace(Namespace.ofCatalog(metalake.name()))
- .withType(Catalog.Type.RELATIONAL)
- .withProvider("test")
- .withComment("this is catalog test 2")
- .withProperties(new HashMap<>())
- .withAuditInfo((AuditInfo) c.auditInfo());
- return builder.build();
- }));
-
- AuditInfo changedAuditInfo =
- AuditInfo.builder().withCreator("changed_creator").withCreateTime(Instant.now()).build();
- CatalogEntity updatedCatalog =
- entityStore.update(
- catalog.nameIdentifier(),
- CatalogEntity.class,
- Entity.EntityType.CATALOG,
- c -> {
- CatalogEntity.Builder builder =
- CatalogEntity.builder()
- .withId(c.id())
- .withName("test_catalog2")
- .withNamespace(Namespace.ofCatalog(metalake.name()))
- .withType(Catalog.Type.RELATIONAL)
- .withProvider("test")
- .withComment("this is catalog test 2")
- .withProperties(new HashMap<>())
- .withAuditInfo(changedAuditInfo);
- return builder.build();
- });
-
- CatalogEntity storedCatalog =
- entityStore.get(
- updatedCatalog.nameIdentifier(), Entity.EntityType.CATALOG, CatalogEntity.class);
-
- assertEquals(catalog.id(), storedCatalog.id());
- assertEquals("test_catalog2", updatedCatalog.name());
- assertEquals("this is catalog test 2", updatedCatalog.getComment());
- assertEquals(changedAuditInfo.creator(), updatedCatalog.auditInfo().creator());
-
- CatalogEntity catalog3 =
- createCatalog(
- 3L, "test_catalog3", Namespace.ofCatalog(metalake.name()), "this is catalog test 3");
- entityStore.put(catalog3, false);
- assertThrows(
- EntityAlreadyExistsException.class,
- () ->
- entityStore.update(
- catalog3.nameIdentifier(),
- CatalogEntity.class,
- Entity.EntityType.CATALOG,
- c -> {
- CatalogEntity.Builder builder =
- CatalogEntity.builder()
- .withId(catalog3.id())
- // catalog name already exists
- .withName("test_catalog2")
- .withNamespace(Namespace.ofCatalog(metalake.name()))
- .withType(Catalog.Type.RELATIONAL)
- .withProvider("test")
- .withComment(catalog3.getComment())
- .withProperties(new HashMap<>())
- .withAuditInfo((AuditInfo) c.auditInfo());
- return builder.build();
- }));
- }
-
- @Test
- public void testSchemaPutAndUpdate() throws IOException {
- BaseMetalake metalake = createMetalake(1L, "test_metalake", "this is test");
- entityStore.put(metalake, false);
-
- CatalogEntity catalog =
- createCatalog(
- 1L, "test_catalog", Namespace.ofCatalog(metalake.name()), "this is catalog test");
- entityStore.put(catalog, false);
-
- SchemaEntity schema =
- createSchema(
- 1L,
- "test_schema",
- Namespace.ofSchema(metalake.name(), catalog.name()),
- "this is schema test");
- entityStore.put(schema, false);
-
- assertThrows(
- RuntimeException.class,
- () ->
- entityStore.update(
- schema.nameIdentifier(),
- SchemaEntity.class,
- Entity.EntityType.SCHEMA,
- s -> {
- SchemaEntity.Builder builder =
- SchemaEntity.builder()
- // Change the id, which is not allowed
- .withId(2L)
- .withName("test_schema2")
- .withNamespace(Namespace.ofSchema(metalake.name(), catalog.name()))
- .withComment("this is schema test 2")
- .withProperties(new HashMap<>())
- .withAuditInfo(s.auditInfo());
- return builder.build();
- }));
-
- AuditInfo changedAuditInfo =
- AuditInfo.builder().withCreator("changed_creator").withCreateTime(Instant.now()).build();
- SchemaEntity updatedSchema =
- entityStore.update(
- schema.nameIdentifier(),
- SchemaEntity.class,
- Entity.EntityType.SCHEMA,
- s -> {
- SchemaEntity.Builder builder =
- SchemaEntity.builder()
- .withId(s.id())
- .withName("test_schema2")
- .withNamespace(Namespace.ofSchema(metalake.name(), catalog.name()))
- .withComment("this is schema test 2")
- .withProperties(new HashMap<>())
- .withAuditInfo(changedAuditInfo);
- return builder.build();
- });
-
- SchemaEntity storedSchema =
- entityStore.get(
- updatedSchema.nameIdentifier(), Entity.EntityType.SCHEMA, SchemaEntity.class);
-
- assertEquals(catalog.id(), storedSchema.id());
- assertEquals("test_schema2", storedSchema.name());
- assertEquals("this is schema test 2", storedSchema.comment());
- assertEquals(changedAuditInfo.creator(), storedSchema.auditInfo().creator());
-
- SchemaEntity schema3 =
- createSchema(
- 3L,
- "test_schema3",
- Namespace.ofSchema(metalake.name(), catalog.name()),
- "this is schema test 3");
- entityStore.put(schema3, false);
-
- assertThrows(
- EntityAlreadyExistsException.class,
- () ->
- entityStore.update(
- schema3.nameIdentifier(),
- SchemaEntity.class,
- Entity.EntityType.SCHEMA,
- s -> {
- SchemaEntity.Builder builder =
- SchemaEntity.builder()
- .withId(schema3.id())
- // schema name already exists
- .withName("test_schema2")
- .withNamespace(Namespace.ofSchema(metalake.name(), catalog.name()))
- .withComment(s.comment())
- .withProperties(new HashMap<>())
- .withAuditInfo(s.auditInfo());
- return builder.build();
- }));
- }
-
- @Test
- public void testTablePutAndUpdate() throws IOException {
- BaseMetalake metalake = createMetalake(1L, "test_metalake", "this is test");
- entityStore.put(metalake, false);
-
- CatalogEntity catalog =
- createCatalog(
- 1L, "test_catalog", Namespace.ofCatalog(metalake.name()), "this is catalog test");
- entityStore.put(catalog, false);
-
- SchemaEntity schema =
- createSchema(
- 1L,
- "test_schema",
- Namespace.ofSchema(metalake.name(), catalog.name()),
- "this is schema test");
- entityStore.put(schema, false);
-
- TableEntity table =
- createTable(
- 1L, "test_table", Namespace.ofTable(metalake.name(), catalog.name(), schema.name()));
- entityStore.put(table, false);
-
- assertThrows(
- RuntimeException.class,
- () ->
- entityStore.update(
- table.nameIdentifier(),
- TableEntity.class,
- Entity.EntityType.TABLE,
- s -> {
- TableEntity.Builder builder =
- TableEntity.builder()
- // Change the id, which is not allowed
- .withId(2L)
- .withName("test_table2")
- .withNamespace(
- Namespace.ofTable(metalake.name(), catalog.name(), schema.name()))
- .withAuditInfo(s.auditInfo());
- return builder.build();
- }));
-
- AuditInfo changedAuditInfo =
- AuditInfo.builder().withCreator("changed_creator").withCreateTime(Instant.now()).build();
- TableEntity updatedTable =
- entityStore.update(
- table.nameIdentifier(),
- TableEntity.class,
- Entity.EntityType.TABLE,
- s -> {
- TableEntity.Builder builder =
- TableEntity.builder()
- .withId(s.id())
- .withName("test_table2")
- .withNamespace(
- Namespace.ofTable(metalake.name(), catalog.name(), schema.name()))
- .withAuditInfo(changedAuditInfo);
- return builder.build();
- });
-
- TableEntity storedTable =
- entityStore.get(updatedTable.nameIdentifier(), Entity.EntityType.TABLE, TableEntity.class);
-
- assertEquals(table.id(), storedTable.id());
- assertEquals("test_table2", storedTable.name());
- assertEquals(changedAuditInfo.creator(), storedTable.auditInfo().creator());
-
- TableEntity table3 =
- createTable(
- 3L, "test_table3", Namespace.ofTable(metalake.name(), catalog.name(), schema.name()));
- entityStore.put(table3, false);
-
- assertThrows(
- EntityAlreadyExistsException.class,
- () ->
- entityStore.update(
- table3.nameIdentifier(),
- TableEntity.class,
- Entity.EntityType.TABLE,
- s -> {
- TableEntity.Builder builder =
- TableEntity.builder()
- .withId(table3.id())
- // table name already exists
- .withName("test_table2")
- .withNamespace(
- Namespace.ofTable(metalake.name(), catalog.name(), schema.name()))
- .withAuditInfo(s.auditInfo());
- return builder.build();
- }));
- }
-
- @Test
- public void testFilesetPutAndUpdate() throws IOException {
- BaseMetalake metalake = createMetalake(1L, "test_metalake", "this is test");
- entityStore.put(metalake, false);
-
- CatalogEntity catalog =
- createCatalog(
- 1L, "test_catalog", Namespace.ofCatalog(metalake.name()), "this is catalog test");
- entityStore.put(catalog, false);
-
- SchemaEntity schema =
- createSchema(
- 1L,
- "test_schema",
- Namespace.ofSchema(metalake.name(), catalog.name()),
- "this is schema test");
- entityStore.put(schema, false);
-
- FilesetEntity fileset =
- createFileset(
- 1L,
- "test_fileset",
- Namespace.ofFileset(metalake.name(), catalog.name(), schema.name()),
- "this is fileset test",
- "hdfs://localhost/test");
- entityStore.put(fileset, false);
-
- assertThrows(
- RuntimeException.class,
- () ->
- entityStore.update(
- fileset.nameIdentifier(),
- FilesetEntity.class,
- Entity.EntityType.FILESET,
- f -> {
- FilesetEntity.Builder builder =
- FilesetEntity.builder()
- // Change the id, which is not allowed
- .withId(2L)
- .withName("test_fileset2")
- .withNamespace(
- Namespace.ofFileset(metalake.name(), catalog.name(), schema.name()))
- .withComment(f.comment())
- .withProperties(f.properties())
- .withStorageLocation(f.storageLocation())
- .withFilesetType(f.filesetType())
- .withAuditInfo(f.auditInfo());
- return builder.build();
- }));
-
- AuditInfo changedAuditInfo =
- AuditInfo.builder().withCreator("changed_creator").withCreateTime(Instant.now()).build();
- // update version fields and normal fields
- FilesetEntity updatedFileset =
- entityStore.update(
- fileset.nameIdentifier(),
- FilesetEntity.class,
- Entity.EntityType.FILESET,
- f -> {
- FilesetEntity.Builder builder =
- FilesetEntity.builder()
- .withId(f.id())
- .withName("test_fileset2")
- .withNamespace(
- Namespace.ofFileset(metalake.name(), catalog.name(), schema.name()))
- .withFilesetType(f.filesetType())
- .withComment("this is fileset test 2")
- .withStorageLocation("hdfs://localhost/test2")
- .withProperties(f.properties())
- .withAuditInfo(changedAuditInfo);
- return builder.build();
- });
-
- FilesetEntity storedFileset =
- entityStore.get(
- updatedFileset.nameIdentifier(), Entity.EntityType.FILESET, FilesetEntity.class);
-
- assertEquals(fileset.id(), storedFileset.id());
- assertEquals("test_fileset2", storedFileset.name());
- assertEquals(changedAuditInfo.creator(), storedFileset.auditInfo().creator());
- assertEquals("this is fileset test 2", storedFileset.comment());
- assertEquals("hdfs://localhost/test2", storedFileset.storageLocation());
-
- FilesetEntity fileset3 =
- createFileset(
- 3L,
- "test_fileset3",
- Namespace.ofFileset(metalake.name(), catalog.name(), schema.name()),
- "this is fileset test 3",
- "hdfs://localhost/test3");
- entityStore.put(fileset3, false);
-
- assertThrows(
- EntityAlreadyExistsException.class,
- () ->
- entityStore.update(
- fileset3.nameIdentifier(),
- FilesetEntity.class,
- Entity.EntityType.FILESET,
- f -> {
- FilesetEntity.Builder builder =
- FilesetEntity.builder()
- .withId(fileset3.id())
- // fileset name already exists
- .withName("test_fileset2")
- .withNamespace(
- Namespace.ofFileset(metalake.name(), catalog.name(), schema.name()))
- .withFilesetType(f.filesetType())
- .withStorageLocation(f.storageLocation())
- .withComment(f.comment())
- .withProperties(f.properties())
- .withAuditInfo(f.auditInfo());
- return builder.build();
- }));
-
- // only update normal fields
- FilesetEntity updatedFileset2 =
- entityStore.update(
- fileset3.nameIdentifier(),
- FilesetEntity.class,
- Entity.EntityType.FILESET,
- f -> {
- FilesetEntity.Builder builder =
- FilesetEntity.builder()
- .withId(f.id())
- .withName("test_fileset4")
- .withNamespace(
- Namespace.ofFileset(metalake.name(), catalog.name(), schema.name()))
- .withFilesetType(f.filesetType())
- .withComment(f.comment())
- .withStorageLocation(f.storageLocation())
- .withProperties(f.properties())
- .withAuditInfo(changedAuditInfo);
- return builder.build();
- });
-
- FilesetEntity storedFileset2 =
- entityStore.get(
- updatedFileset2.nameIdentifier(), Entity.EntityType.FILESET, FilesetEntity.class);
-
- assertEquals(fileset3.id(), storedFileset2.id());
- assertEquals("test_fileset4", storedFileset2.name());
- assertEquals(changedAuditInfo.creator(), storedFileset2.auditInfo().creator());
- assertEquals(fileset3.comment(), storedFileset2.comment());
- assertEquals(fileset3.storageLocation(), storedFileset2.storageLocation());
- }
-
- @Test
- public void testMetalakePutAndExists() throws IOException {
- BaseMetalake metalake = createMetalake(1L, "test_metalake", "this is test");
- entityStore.put(metalake, false);
- assertTrue(entityStore.exists(metalake.nameIdentifier(), Entity.EntityType.METALAKE));
- }
-
- @Test
- public void testCatalogPutAndExists() throws IOException {
- BaseMetalake metalake = createMetalake(1L, "test_metalake", "this is test");
- entityStore.put(metalake, false);
-
- CatalogEntity catalog =
- createCatalog(1L, "test_catalog", Namespace.ofCatalog(metalake.name()), "this is test");
- entityStore.put(catalog, false);
-
- assertTrue(entityStore.exists(catalog.nameIdentifier(), Entity.EntityType.CATALOG));
- }
-
- @Test
- public void testSchemaPutAndExists() throws IOException {
- BaseMetalake metalake = createMetalake(1L, "test_metalake", "this is test");
- entityStore.put(metalake, false);
-
- CatalogEntity catalog =
- createCatalog(1L, "test_catalog", Namespace.ofCatalog(metalake.name()), "this is test");
- entityStore.put(catalog, false);
-
- SchemaEntity schema =
- createSchema(
- 1L, "test_schema", Namespace.ofSchema(metalake.name(), catalog.name()), "this is test");
- entityStore.put(schema, false);
-
- assertTrue(entityStore.exists(schema.nameIdentifier(), Entity.EntityType.SCHEMA));
- }
-
- @Test
- public void testTablePutAndExists() throws IOException {
- BaseMetalake metalake = createMetalake(1L, "test_metalake", "this is test");
- entityStore.put(metalake, false);
-
- CatalogEntity catalog =
- createCatalog(1L, "test_catalog", Namespace.ofCatalog(metalake.name()), "this is test");
- entityStore.put(catalog, false);
-
- SchemaEntity schema =
- createSchema(
- 1L, "test_schema", Namespace.ofSchema(metalake.name(), catalog.name()), "this is test");
- entityStore.put(schema, false);
-
- TableEntity table =
- createTable(
- 1L, "test_table", Namespace.ofTable(metalake.name(), catalog.name(), schema.name()));
- entityStore.put(table, false);
-
- assertTrue(entityStore.exists(table.nameIdentifier(), Entity.EntityType.TABLE));
- }
-
- @Test
- public void testFilesetPutAndExists() throws IOException {
- BaseMetalake metalake = createMetalake(1L, "test_metalake", "this is test");
- entityStore.put(metalake, false);
-
- CatalogEntity catalog =
- createCatalog(1L, "test_catalog", Namespace.ofCatalog(metalake.name()), "this is test");
- entityStore.put(catalog, false);
-
- SchemaEntity schema =
- createSchema(
- 1L, "test_schema", Namespace.ofSchema(metalake.name(), catalog.name()), "this is test");
- entityStore.put(schema, false);
-
- FilesetEntity fileset =
- createFileset(
- 1L,
- "test_table",
- Namespace.ofFileset(metalake.name(), catalog.name(), schema.name()),
- "this is test",
- "hdfs://localhost/test");
- entityStore.put(fileset, false);
-
- assertTrue(entityStore.exists(fileset.nameIdentifier(), Entity.EntityType.FILESET));
- }
-
- private static BaseMetalake createMetalake(Long id, String name, String comment) {
- AuditInfo auditInfo =
- AuditInfo.builder().withCreator("creator").withCreateTime(Instant.now()).build();
- return BaseMetalake.builder()
- .withId(id)
- .withName(name)
- .withComment(comment)
- .withProperties(new HashMap<>())
- .withAuditInfo(auditInfo)
- .withVersion(SchemaVersion.V_0_1)
- .build();
- }
-
- private static boolean checkMetalakeEquals(BaseMetalake expected, BaseMetalake actual) {
- return expected.id().equals(actual.id())
- && expected.name().equals(actual.name())
- && expected.comment().equals(actual.comment())
- && expected.properties().equals(actual.properties())
- && expected.auditInfo().equals(actual.auditInfo())
- && expected.getVersion().equals(actual.getVersion());
- }
-
- private static CatalogEntity createCatalog(
- Long id, String name, Namespace namespace, String comment) {
- AuditInfo auditInfo =
- AuditInfo.builder().withCreator("creator").withCreateTime(Instant.now()).build();
- return CatalogEntity.builder()
- .withId(id)
- .withName(name)
- .withNamespace(namespace)
- .withType(Catalog.Type.RELATIONAL)
- .withProvider("test")
- .withComment(comment)
- .withProperties(new HashMap<>())
- .withAuditInfo(auditInfo)
- .build();
- }
-
- private static boolean checkCatalogEquals(CatalogEntity expected, CatalogEntity actual) {
- return expected.id().equals(actual.id())
- && expected.name().equals(actual.name())
- && expected.namespace().equals(actual.namespace())
- && expected.getComment().equals(actual.getComment())
- && expected.getType().equals(actual.getType())
- && expected.getProvider().equals(actual.getProvider())
- && expected.getProperties() != null
- && expected.getProperties().equals(actual.getProperties())
- && expected.auditInfo().equals(actual.auditInfo());
- }
-
- private static SchemaEntity createSchema(
- Long id, String name, Namespace namespace, String comment) {
- AuditInfo auditInfo =
- AuditInfo.builder().withCreator("creator").withCreateTime(Instant.now()).build();
- return SchemaEntity.builder()
- .withId(id)
- .withName(name)
- .withNamespace(namespace)
- .withComment(comment)
- .withProperties(new HashMap<>())
- .withAuditInfo(auditInfo)
- .build();
- }
-
- private static boolean checkSchemaEquals(SchemaEntity expected, SchemaEntity actual) {
- return expected.id().equals(actual.id())
- && expected.name().equals(actual.name())
- && expected.namespace().equals(actual.namespace())
- && expected.comment().equals(actual.comment())
- && expected.properties() != null
- && expected.properties().equals(actual.properties())
- && expected.auditInfo().equals(actual.auditInfo());
- }
-
- private static TableEntity createTable(Long id, String name, Namespace namespace) {
- AuditInfo auditInfo =
- AuditInfo.builder().withCreator("creator").withCreateTime(Instant.now()).build();
- return TableEntity.builder()
- .withId(id)
- .withName(name)
- .withNamespace(namespace)
- .withAuditInfo(auditInfo)
- .build();
- }
-
- private static boolean checkTableEquals(TableEntity expected, TableEntity actual) {
- return expected.id().equals(actual.id())
- && expected.name().equals(actual.name())
- && expected.namespace().equals(actual.namespace())
- && expected.auditInfo().equals(actual.auditInfo());
- }
-
- private static FilesetEntity createFileset(
- Long id, String name, Namespace namespace, String comment, String location) {
- AuditInfo auditInfo =
- AuditInfo.builder().withCreator("creator").withCreateTime(Instant.now()).build();
- return FilesetEntity.builder()
- .withId(id)
- .withName(name)
- .withNamespace(namespace)
- .withFilesetType(Fileset.Type.MANAGED)
- .withComment(comment)
- .withAuditInfo(auditInfo)
- .withStorageLocation(location)
- .withProperties(new HashMap<>())
- .build();
- }
-
- private static boolean checkFilesetEquals(FilesetEntity expected, FilesetEntity actual) {
- return expected.id().equals(actual.id())
- && expected.name().equals(actual.name())
- && expected.filesetType() == actual.filesetType()
- && expected.comment().equals(actual.comment())
- && expected.storageLocation().equals(actual.storageLocation())
- && expected.properties() != null
- && expected.properties().equals(actual.properties())
- && expected.namespace().equals(actual.namespace())
- && expected.auditInfo().equals(actual.auditInfo());
- }
-
- private static void truncateAllTables() {
- try (SqlSession sqlSession =
- SqlSessionFactoryHelper.getInstance().getSqlSessionFactory().openSession(true)) {
- try (Connection connection = sqlSession.getConnection()) {
- try (Statement statement = connection.createStatement()) {
- String query = "SHOW TABLES";
- List<String> tableList = new ArrayList<>();
- try (ResultSet rs = statement.executeQuery(query)) {
- while (rs.next()) {
- tableList.add(rs.getString(1));
- }
- }
- for (String table : tableList) {
- statement.execute("TRUNCATE TABLE " + table);
- }
- }
- }
- } catch (SQLException e) {
- throw new RuntimeException("Clear table failed", e);
- }
- }
-
- private static void dropAllTables() {
- try (SqlSession sqlSession =
- SqlSessionFactoryHelper.getInstance().getSqlSessionFactory().openSession(true)) {
- try (Connection connection = sqlSession.getConnection()) {
- try (Statement statement = connection.createStatement()) {
- String query = "SHOW TABLES";
- List<String> tableList = new ArrayList<>();
- try (ResultSet rs = statement.executeQuery(query)) {
- while (rs.next()) {
- tableList.add(rs.getString(1));
- }
- }
- for (String table : tableList) {
- statement.execute("DROP TABLE " + table);
- }
- }
- }
- } catch (SQLException e) {
- throw new RuntimeException("Drop table failed", e);
- }
- }
-}