Skip to content

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

[#9635] fix(core): fix tag association problem (#9638)

Mini Yuefec2f85ec6f
diff --git a/core/src/main/java/org/apache/gravitino/storage/relational/RelationalEntityStore.java b/core/src/main/java/org/apache/gravitino/storage/relational/RelationalEntityStore.java
index aea88e9a9e..b4e8230e36 100644
--- a/core/src/main/java/org/apache/gravitino/storage/relational/RelationalEntityStore.java
+++ b/core/src/main/java/org/apache/gravitino/storage/relational/RelationalEntityStore.java
@@ -253,6 +253,7 @@ public class RelationalEntityStore implements EntityStore, SupportsRelationOpera
boolean override)
throws IOException {
cache.invalidate(srcIdentifier, srcType, relType);
+ cache.invalidate(dstIdentifier, dstType, relType);
backend.insertRelation(relType, srcIdentifier, srcType, dstIdentifier, dstType, override);
}
@@ -264,7 +265,21 @@ public class RelationalEntityStore implements EntityStore, SupportsRelationOpera
NameIdentifier[] destEntitiesToAdd,
NameIdentifier[] destEntitiesToRemove)
throws IOException, NoSuchEntityException, EntityAlreadyExistsException {
+
+ // We need to clear the cache of the source entity and all destination entities being added or
+ // removed. This ensures that any subsequent reads will fetch the updated relations from the
+ // backend. For example, if we are adding a tag to table, we need to invalidate the cache for
+ // that table and the tag being added or removed. Otherwise, we might return stale data if we
+ // list all tags for that table or all tables for that tag.
cache.invalidate(srcEntityIdent, srcEntityType, relType);
+ for (NameIdentifier destToAdd : destEntitiesToAdd) {
+ cache.invalidate(destToAdd, srcEntityType, relType);
+ }
+
+ for (NameIdentifier destToRemove : destEntitiesToRemove) {
+ cache.invalidate(destToRemove, srcEntityType, relType);
+ }
+
return backend.updateEntityRelations(
relType, srcEntityIdent, srcEntityType, destEntitiesToAdd, destEntitiesToRemove);
}
diff --git a/core/src/test/java/org/apache/gravitino/storage/AbstractEntityStorageTest.java b/core/src/test/java/org/apache/gravitino/storage/AbstractEntityStorageTest.java
new file mode 100644
index 0000000000..f91d98c23a
--- /dev/null
+++ b/core/src/test/java/org/apache/gravitino/storage/AbstractEntityStorageTest.java
@@ -0,0 +1,1499 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.gravitino.storage;
+
+import static org.apache.gravitino.Configs.DEFAULT_ENTITY_RELATIONAL_STORE;
+import static org.apache.gravitino.Configs.ENTITY_RELATIONAL_JDBC_BACKEND_DRIVER;
+import static org.apache.gravitino.Configs.ENTITY_RELATIONAL_JDBC_BACKEND_MAX_CONNECTIONS;
+import static org.apache.gravitino.Configs.ENTITY_RELATIONAL_JDBC_BACKEND_PASSWORD;
+import static org.apache.gravitino.Configs.ENTITY_RELATIONAL_JDBC_BACKEND_PATH;
+import static org.apache.gravitino.Configs.ENTITY_RELATIONAL_JDBC_BACKEND_URL;
+import static org.apache.gravitino.Configs.ENTITY_RELATIONAL_JDBC_BACKEND_USER;
+import static org.apache.gravitino.Configs.ENTITY_RELATIONAL_JDBC_BACKEND_WAIT_MILLISECONDS;
+import static org.apache.gravitino.Configs.ENTITY_RELATIONAL_STORE;
+import static org.apache.gravitino.Configs.ENTITY_STORE;
+import static org.apache.gravitino.Configs.RELATIONAL_ENTITY_STORE;
+import static org.apache.gravitino.Configs.STORE_DELETE_AFTER_TIME;
+import static org.apache.gravitino.Configs.VERSION_RETENTION_COUNT;
+import static org.apache.gravitino.file.Fileset.LOCATION_NAME_UNKNOWN;
+
+import com.google.common.base.Preconditions;
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.Lists;
+import java.io.File;
+import java.io.IOException;
+import java.lang.reflect.Field;
+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.Collections;
+import java.util.List;
+import java.util.Map;
+import java.util.UUID;
+import org.apache.commons.io.FileUtils;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.commons.lang3.reflect.FieldUtils;
+import org.apache.commons.lang3.tuple.Pair;
+import org.apache.gravitino.Catalog;
+import org.apache.gravitino.Config;
+import org.apache.gravitino.Configs;
+import org.apache.gravitino.Entity;
+import org.apache.gravitino.Entity.EntityType;
+import org.apache.gravitino.EntityAlreadyExistsException;
+import org.apache.gravitino.EntityStore;
+import org.apache.gravitino.EntityStoreFactory;
+import org.apache.gravitino.GravitinoEnv;
+import org.apache.gravitino.NameIdentifier;
+import org.apache.gravitino.Namespace;
+import org.apache.gravitino.authorization.AuthorizationUtils;
+import org.apache.gravitino.authorization.Privileges;
+import org.apache.gravitino.authorization.SecurableObject;
+import org.apache.gravitino.authorization.SecurableObjects;
+import org.apache.gravitino.exceptions.NoSuchEntityException;
+import org.apache.gravitino.exceptions.NonEmptyEntityException;
+import org.apache.gravitino.file.Fileset;
+import org.apache.gravitino.integration.test.container.ContainerSuite;
+import org.apache.gravitino.integration.test.util.BaseIT;
+import org.apache.gravitino.meta.AuditInfo;
+import org.apache.gravitino.meta.BaseMetalake;
+import org.apache.gravitino.meta.CatalogEntity;
+import org.apache.gravitino.meta.ColumnEntity;
+import org.apache.gravitino.meta.FilesetEntity;
+import org.apache.gravitino.meta.GroupEntity;
+import org.apache.gravitino.meta.ModelEntity;
+import org.apache.gravitino.meta.ModelVersionEntity;
+import org.apache.gravitino.meta.RoleEntity;
+import org.apache.gravitino.meta.SchemaEntity;
+import org.apache.gravitino.meta.SchemaVersion;
+import org.apache.gravitino.meta.TableEntity;
+import org.apache.gravitino.meta.TopicEntity;
+import org.apache.gravitino.meta.UserEntity;
+import org.apache.gravitino.rel.types.Type;
+import org.apache.gravitino.storage.relational.RelationalBackend;
+import org.apache.gravitino.storage.relational.RelationalEntityStore;
+import org.apache.gravitino.storage.relational.RelationalGarbageCollector;
+import org.apache.gravitino.storage.relational.converters.H2ExceptionConverter;
+import org.apache.gravitino.storage.relational.converters.MySQLExceptionConverter;
+import org.apache.gravitino.storage.relational.converters.PostgreSQLExceptionConverter;
+import org.apache.gravitino.storage.relational.converters.SQLExceptionConverterFactory;
+import org.apache.gravitino.storage.relational.session.SqlSessionFactoryHelper;
+import org.apache.ibatis.session.SqlSession;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.Assertions;
+import org.mockito.Mockito;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+abstract class AbstractEntityStorageTest {
+ protected static final Logger LOG = LoggerFactory.getLogger(AbstractEntityStorageTest.class);
+
+ protected static final String JDBC_STORE_PATH =
+ "/tmp/gravitino_jdbc_entityStore_" + UUID.randomUUID().toString().replace("-", "");
+ protected static final String DB_DIR = JDBC_STORE_PATH + "/testdb";
+ protected static final String H2_FILE = DB_DIR + ".mv.db";
+
+ static Object[][] storageProvider() {
+ return new Object[][] {
+ {"h2", true},
+ {"h2", false},
+ {"mysql", true},
+ {"mysql", false},
+ {"postgresql", true},
+ {"postgresql", false}
+ };
+ }
+
+ @AfterEach
+ void closeSuit() throws IOException {
+ // todo: refactor TestEntityStorage to extend TestJDBCBackend, otherwise, each test will start
+ // and stop the container suite.
+ ContainerSuite.getInstance().close();
+ }
+
+ protected void init(String type, Config config) throws IllegalAccessException {
+ Preconditions.checkArgument(StringUtils.isNotBlank(type));
+ 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_PATH)).thenReturn(DB_DIR);
+ Mockito.when(config.get(ENTITY_RELATIONAL_JDBC_BACKEND_MAX_CONNECTIONS)).thenReturn(100);
+ Mockito.when(config.get(ENTITY_RELATIONAL_JDBC_BACKEND_WAIT_MILLISECONDS)).thenReturn(1000L);
+ Mockito.when(config.get(STORE_DELETE_AFTER_TIME)).thenReturn(20 * 60 * 1000L);
+ Mockito.when(config.get(VERSION_RETENTION_COUNT)).thenReturn(1L);
+ // Fix cache config for test
+ Mockito.when(config.get(Configs.CACHE_ENABLED)).thenReturn(true);
+ Mockito.when(config.get(Configs.CACHE_MAX_ENTRIES)).thenReturn(10_000);
+ Mockito.when(config.get(Configs.CACHE_EXPIRATION_TIME)).thenReturn(3_600_000L);
+ Mockito.when(config.get(Configs.CACHE_WEIGHER_ENABLED)).thenReturn(true);
+ Mockito.when(config.get(Configs.CACHE_STATS_ENABLED)).thenReturn(false);
+ Mockito.when(config.get(Configs.CACHE_IMPLEMENTATION)).thenReturn("caffeine");
+ Mockito.when(config.get(Configs.CACHE_LOCK_SEGMENTS)).thenReturn(16);
+
+ FieldUtils.writeField(GravitinoEnv.getInstance(), "config", config, true);
+
+ BaseIT baseIT = new BaseIT();
+
+ try {
+ if (type.equalsIgnoreCase("h2")) {
+ // The following properties are used to create the JDBC connection; they are just for test,
+ // in the real world, they will be set automatically by the configuration file if you set
+ // ENTITY_RELATIONAL_STORE as EMBEDDED_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("gravitino");
+ Mockito.when(config.get(ENTITY_RELATIONAL_JDBC_BACKEND_PASSWORD)).thenReturn("gravitino");
+ Mockito.when(config.get(ENTITY_RELATIONAL_JDBC_BACKEND_DRIVER)).thenReturn("org.h2.Driver");
+ Mockito.when(config.get(ENTITY_RELATIONAL_JDBC_BACKEND_MAX_CONNECTIONS)).thenReturn(100);
+ Mockito.when(config.get(ENTITY_RELATIONAL_JDBC_BACKEND_WAIT_MILLISECONDS))
+ .thenReturn(1000L);
+
+ FieldUtils.writeStaticField(
+ SQLExceptionConverterFactory.class, "converter", new H2ExceptionConverter(), true);
+
+ } else if (type.equalsIgnoreCase("mysql")) {
+ String mysqlJdbcUrl = baseIT.startAndInitMySQLBackend();
+ Mockito.when(config.get(ENTITY_RELATIONAL_JDBC_BACKEND_URL)).thenReturn(mysqlJdbcUrl);
+ Mockito.when(config.get(ENTITY_RELATIONAL_JDBC_BACKEND_USER)).thenReturn("root");
+ Mockito.when(config.get(ENTITY_RELATIONAL_JDBC_BACKEND_PASSWORD)).thenReturn("root");
+ Mockito.when(config.get(ENTITY_RELATIONAL_JDBC_BACKEND_DRIVER))
+ .thenReturn("com.mysql.cj.jdbc.Driver");
+ Mockito.when(config.get(ENTITY_RELATIONAL_JDBC_BACKEND_MAX_CONNECTIONS)).thenReturn(100);
+ Mockito.when(config.get(ENTITY_RELATIONAL_JDBC_BACKEND_WAIT_MILLISECONDS))
+ .thenReturn(1000L);
+
+ FieldUtils.writeStaticField(
+ SQLExceptionConverterFactory.class, "converter", new MySQLExceptionConverter(), true);
+
+ } else if (type.equalsIgnoreCase("postgresql")) {
+ String postgreSQLJdbcUrl = baseIT.startAndInitPGBackend();
+ Mockito.when(config.get(ENTITY_RELATIONAL_JDBC_BACKEND_URL)).thenReturn(postgreSQLJdbcUrl);
+ Mockito.when(config.get(ENTITY_RELATIONAL_JDBC_BACKEND_USER)).thenReturn("root");
+ Mockito.when(config.get(ENTITY_RELATIONAL_JDBC_BACKEND_PASSWORD)).thenReturn("root");
+ Mockito.when(config.get(ENTITY_RELATIONAL_JDBC_BACKEND_DRIVER))
+ .thenReturn("org.postgresql.Driver");
+
+ FieldUtils.writeStaticField(
+ SQLExceptionConverterFactory.class,
+ "converter",
+ new PostgreSQLExceptionConverter(),
+ true);
+
+ RelationalEntityStore store =
+ (RelationalEntityStore) EntityStoreFactory.createEntityStore(config);
+ store.initialize(config);
+ Field f = FieldUtils.getField(RelationalEntityStore.class, "backend", true);
+ RelationalBackend backend = (RelationalBackend) f.get(store);
+ RelationalGarbageCollector garbageCollector =
+ new RelationalGarbageCollector(backend, config);
+ garbageCollector.collectAndClean();
+
+ } else {
+ throw new UnsupportedOperationException("Unsupported entity store type: " + type);
+ }
+ } catch (Exception e) {
+ LOG.error("Failed to init entity store", e);
+ throw new RuntimeException(e);
+ }
+ }
+
+ protected void destroy(String type) {
+ Preconditions.checkArgument(StringUtils.isNotBlank(type));
+ if (type.equalsIgnoreCase("h2") || type.equalsIgnoreCase("mysql")) {
+ dropAllTables();
+ File dir = new File(DB_DIR);
+ if (dir.exists()) {
+ dir.delete();
+ }
+
+ FileUtils.deleteQuietly(new File(H2_FILE));
+ } else if (type.equalsIgnoreCase("postgresql")) {
+ // Do nothing
+ } else {
+ throw new UnsupportedOperationException("Unsupported entity store type: " + type);
+ }
+ }
+
+ protected 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);
+ }
+ }
+
+ protected 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();
+ }
+
+ protected 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();
+ }
+
+ protected 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();
+ }
+
+ protected static ColumnEntity createColumnEntity(
+ Long id, String name, int position, Type dataType, AuditInfo auditInfo) {
+ return ColumnEntity.builder()
+ .withId(id)
+ .withName(name)
+ .withPosition(position)
+ .withComment("")
+ .withDataType(dataType)
+ .withNullable(true)
+ .withAutoIncrement(false)
+ .withAuditInfo(auditInfo)
+ .build();
+ }
+
+ protected static TableEntity createTableEntity(
+ Long id, Namespace namespace, String name, AuditInfo auditInfo) {
+ return createTableEntityWithColumns(id, namespace, name, auditInfo, Collections.emptyList());
+ }
+
+ protected static TableEntity createTableEntityWithColumns(
+ Long id, Namespace namespace, String name, AuditInfo auditInfo, List<ColumnEntity> columns) {
+ return TableEntity.builder()
+ .withId(id)
+ .withName(name)
+ .withNamespace(namespace)
+ .withAuditInfo(auditInfo)
+ .withColumns(columns)
+ .build();
+ }
+
+ protected static FilesetEntity createFilesetEntity(
+ Long id, Namespace namespace, String name, AuditInfo auditInfo) {
+ return FilesetEntity.builder()
+ .withId(id)
+ .withName(name)
+ .withNamespace(namespace)
+ .withFilesetType(Fileset.Type.MANAGED)
+ .withStorageLocations(ImmutableMap.of(LOCATION_NAME_UNKNOWN, "/tmp"))
+ .withComment("")
+ .withProperties(null)
+ .withAuditInfo(auditInfo)
+ .build();
+ }
+
+ protected static TopicEntity createTopicEntity(
+ Long id, Namespace namespace, String name, AuditInfo auditInfo) {
+ return TopicEntity.builder()
+ .withId(id)
+ .withName(name)
+ .withNamespace(namespace)
+ .withComment("test comment")
+ .withProperties(ImmutableMap.of("key", "value"))
+ .withAuditInfo(auditInfo)
+ .build();
+ }
+
+ protected static UserEntity createUser(
+ Long id, String metalake, String name, AuditInfo auditInfo) {
+ return UserEntity.builder()
+ .withId(id)
+ .withNamespace(AuthorizationUtils.ofUserNamespace(metalake))
+ .withName(name)
+ .withAuditInfo(auditInfo)
+ .withRoleNames(null)
+ .build();
+ }
+
+ protected static GroupEntity createGroup(
+ Long id, String metalake, String name, AuditInfo auditInfo) {
+ return GroupEntity.builder()
+ .withId(id)
+ .withNamespace(AuthorizationUtils.ofGroupNamespace(metalake))
+ .withName(name)
+ .withAuditInfo(auditInfo)
+ .withRoleNames(null)
+ .build();
+ }
+
+ protected static RoleEntity createRole(
+ Long id, String metalake, String name, AuditInfo auditInfo) {
+ SecurableObject securableObject =
+ SecurableObjects.ofCatalog("catalog", Lists.newArrayList(Privileges.UseCatalog.allow()));
+
+ return RoleEntity.builder()
+ .withId(id)
+ .withNamespace(AuthorizationUtils.ofRoleNamespace(metalake))
+ .withName(name)
+ .withAuditInfo(auditInfo)
+ .withSecurableObjects(Lists.newArrayList(securableObject))
+ .withProperties(null)
+ .build();
+ }
+
+ protected void validateDeleteTopicCascade(EntityStore store, TopicEntity topic1)
+ throws IOException {
+ // Delete the topic 'metalake.catalog.schema1.topic1'
+ Assertions.assertTrue(store.delete(topic1.nameIdentifier(), Entity.EntityType.TOPIC));
+ Assertions.assertFalse(store.exists(topic1.nameIdentifier(), Entity.EntityType.TOPIC));
+ // Delete again should return false
+ Assertions.assertFalse(store.delete(topic1.nameIdentifier(), Entity.EntityType.TOPIC));
+ }
+
+ protected void validateDeleteModelCascade(EntityStore store, ModelEntity model1)
+ throws IOException {
+ // Delete the topic 'metalake.catalog.schema1.topic1'
+ Assertions.assertTrue(store.delete(model1.nameIdentifier(), EntityType.MODEL));
+ Assertions.assertFalse(store.exists(model1.nameIdentifier(), EntityType.MODEL));
+ // Delete again should return false
+ Assertions.assertFalse(store.delete(model1.nameIdentifier(), EntityType.MODEL));
+ }
+
+ protected 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));
+ // Delete again should return false
+ Assertions.assertFalse(
+ store.delete(fileset1.nameIdentifier(), Entity.EntityType.FILESET, true));
+ }
+
+ protected 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));
+ // Delete again should return false
+ Assertions.assertFalse(store.delete(table1.nameIdentifier(), Entity.EntityType.TABLE, true));
+ validateDeletedColumns(table1.id(), table1.type());
+ }
+
+ protected 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));
+ // Delete again should return false
+ Assertions.assertFalse(
+ store.delete(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));
+ }
+
+ protected void validateDeleteTopic(
+ EntityStore store, SchemaEntity schema2, TopicEntity topic1, TopicEntity topic1InSchema2)
+ throws IOException {
+ // Delete the topic 'metalake.catalog.schema2.topic1'
+ Assertions.assertTrue(store.delete(topic1InSchema2.nameIdentifier(), Entity.EntityType.TOPIC));
+ Assertions.assertFalse(store.exists(topic1InSchema2.nameIdentifier(), Entity.EntityType.TOPIC));
+ // Delete again should return false
+ Assertions.assertFalse(store.delete(topic1InSchema2.nameIdentifier(), Entity.EntityType.TOPIC));
+
+ // Make sure topic 'metalake.catalog.schema1.topic1' still exist;
+ Assertions.assertEquals(
+ topic1, store.get(topic1.nameIdentifier(), Entity.EntityType.TOPIC, TopicEntity.class));
+ // Make sure schema 'metalake.catalog.schema2' still exist;
+ Assertions.assertEquals(
+ schema2, store.get(schema2.nameIdentifier(), Entity.EntityType.SCHEMA, SchemaEntity.class));
+ }
+
+ protected void validateDeleteMetalakeCascade(
+ EntityStore store,
+ BaseMetalake metalake,
+ CatalogEntity catalog,
+ SchemaEntity schema2,
+ UserEntity userNew,
+ GroupEntity groupNew,
+ RoleEntity roleNew)
+ throws IOException {
+ Assertions.assertTrue(store.exists(userNew.nameIdentifier(), Entity.EntityType.USER));
+ Assertions.assertTrue(store.exists(groupNew.nameIdentifier(), Entity.EntityType.GROUP));
+ Assertions.assertTrue(store.exists(roleNew.nameIdentifier(), Entity.EntityType.ROLE));
+
+ 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));
+ Assertions.assertFalse(store.exists(userNew.nameIdentifier(), Entity.EntityType.USER));
+ Assertions.assertFalse(store.exists(groupNew.nameIdentifier(), EntityType.GROUP));
+ Assertions.assertFalse(store.exists(roleNew.nameIdentifier(), EntityType.ROLE));
+ validateDeletedColumns(metalake.id(), metalake.type());
+
+ // Delete again should return false
+ Assertions.assertFalse(
+ store.delete(metalake.nameIdentifier(), Entity.EntityType.METALAKE, true));
+ }
+
+ protected 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));
+ validateDeletedColumns(catalog.id(), catalog.type());
+
+ Assertions.assertThrowsExactly(
+ NoSuchEntityException.class,
+ () -> store.get(schema2.nameIdentifier(), Entity.EntityType.SCHEMA, SchemaEntity.class));
+ // Delete again should return false
+ Assertions.assertFalse(store.delete(catalog.nameIdentifier(), Entity.EntityType.CATALOG, true));
+ }
+
+ protected void validateDeleteSchemaCascade(
+ EntityStore store,
+ SchemaEntity schema1,
+ TableEntity table1,
+ FilesetEntity fileset1,
+ TopicEntity topic1,
+ ModelEntity model1)
+ throws IOException {
+ TableEntity table1New =
+ createTableEntityWithColumns(
+ RandomIdGenerator.INSTANCE.nextId(),
+ table1.namespace(),
+ table1.name(),
+ table1.auditInfo(),
+ table1.columns());
+ store.put(table1New);
+ FilesetEntity fileset1New =
+ createFilesetEntity(
+ RandomIdGenerator.INSTANCE.nextId(),
+ fileset1.namespace(),
+ fileset1.name(),
+ fileset1.auditInfo());
+ store.put(fileset1New);
+ TopicEntity topic1New =
+ createTopicEntity(
+ RandomIdGenerator.INSTANCE.nextId(),
+ topic1.namespace(),
+ topic1.name(),
+ topic1.auditInfo());
+ store.put(topic1New);
+
+ ModelEntity model1New =
+ createModelEntity(
+ RandomIdGenerator.INSTANCE.nextId(),
+ model1.namespace(),
+ model1.name(),
+ model1.comment(),
+ model1.latestVersion(),
+ model1.properties(),
+ model1.auditInfo());
+ store.put(model1New);
+
+ 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"));
+ }
+
+ validateDeletedColumns(schema1.id(), schema1.type());
+
+ // Delete again should return false
+ Assertions.assertFalse(store.delete(schema1.nameIdentifier(), Entity.EntityType.SCHEMA, true));
+
+ Assertions.assertThrows(
+ NoSuchEntityException.class,
+ () -> store.get(fileset1.nameIdentifier(), Entity.EntityType.FILESET, FilesetEntity.class));
+
+ Assertions.assertThrows(
+ NoSuchEntityException.class,
+ () -> store.get(topic1.nameIdentifier(), Entity.EntityType.TOPIC, TopicEntity.class));
+
+ Assertions.assertThrows(
+ NoSuchEntityException.class,
+ () -> store.get(model1.nameIdentifier(), Entity.EntityType.MODEL, ModelEntity.class));
+ }
+
+ protected void validateDeleteMetalake(
+ EntityStore store,
+ BaseMetalake metalake,
+ CatalogEntity catalogCopy,
+ UserEntity user2,
+ GroupEntity group2,
+ RoleEntity role2)
+ throws IOException {
+ // Now delete catalog 'catalogCopy' and metalake
+ Assertions.assertTrue(store.exists(user2.nameIdentifier(), Entity.EntityType.USER));
+ Assertions.assertTrue(store.exists(group2.nameIdentifier(), Entity.EntityType.GROUP));
+ Assertions.assertTrue(store.exists(role2.nameIdentifier(), Entity.EntityType.ROLE));
+
+ 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));
+ Assertions.assertFalse(store.exists(user2.nameIdentifier(), Entity.EntityType.USER));
+ Assertions.assertFalse(store.exists(group2.nameIdentifier(), Entity.EntityType.GROUP));
+ Assertions.assertFalse(store.exists(role2.nameIdentifier(), Entity.EntityType.ROLE));
+ // Delete again should return false
+ Assertions.assertFalse(store.delete(metalake.nameIdentifier(), Entity.EntityType.METALAKE));
+ }
+
+ protected void validateDeleteCatalog(
+ EntityStore store,
+ CatalogEntity catalog,
+ TableEntity table1,
+ SchemaEntity schema1,
+ TableEntity table1InSchema2,
+ SchemaEntity schema2,
+ FilesetEntity fileset1,
+ FilesetEntity fileset1InSchema2,
+ TopicEntity topic1,
+ TopicEntity topic1InSchema2,
+ ModelEntity model1,
+ ModelEntity model1InSchema2)
+ 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);
+ validateDeletedColumns(table1.id(), table1.type());
+ store.delete(fileset1.nameIdentifier(), Entity.EntityType.FILESET);
+ store.delete(topic1.nameIdentifier(), Entity.EntityType.TOPIC);
+ store.delete(model1.nameIdentifier(), Entity.EntityType.MODEL);
+
+ 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);
+ validateDeletedColumns(table1InSchema2.id(), table1InSchema2.type());
+ Assertions.assertFalse(
+ store.exists(fileset1InSchema2.nameIdentifier(), Entity.EntityType.FILESET));
+ Assertions.assertFalse(store.exists(topic1InSchema2.nameIdentifier(), Entity.EntityType.TOPIC));
+ Assertions.assertFalse(store.exists(model1InSchema2.nameIdentifier(), Entity.EntityType.MODEL));
+ store.delete(schema2.nameIdentifier(), Entity.EntityType.SCHEMA);
+
+ store.delete(catalog.nameIdentifier(), Entity.EntityType.CATALOG);
+ Assertions.assertFalse(store.exists(catalog.nameIdentifier(), Entity.EntityType.CATALOG));
+ // Delete again should return false
+ Assertions.assertFalse(store.delete(catalog.nameIdentifier(), Entity.EntityType.CATALOG));
+ }
+
+ protected void validateDeleteSchema(
+ EntityStore store,
+ SchemaEntity schema1,
+ TableEntity table1,
+ FilesetEntity fileset1,
+ TopicEntity topic1,
+ ModelEntity model1,
+ ModelVersionEntity modelVersion1)
+ 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', table 'metalake.catalog.schema1.table1',
+ // table 'metalake.catalog.schema1.fileset1' and table 'metalake.catalog.schema1.topic1'
+ // 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));
+ Assertions.assertTrue(store.exists(topic1.nameIdentifier(), Entity.EntityType.TOPIC));
+ Assertions.assertTrue(store.exists(model1.nameIdentifier(), Entity.EntityType.MODEL));
+ Assertions.assertTrue(
+ store.exists(modelVersion1.nameIdentifier(), Entity.EntityType.MODEL_VERSION));
+
+ // Delete table1,fileset1 and schema1
+ Assertions.assertTrue(store.delete(table1.nameIdentifier(), Entity.EntityType.TABLE));
+ validateDeletedColumns(table1.id(), table1.type());
+ Assertions.assertTrue(store.delete(fileset1.nameIdentifier(), Entity.EntityType.FILESET));
+ Assertions.assertTrue(store.delete(topic1.nameIdentifier(), Entity.EntityType.TOPIC));
+ Assertions.assertTrue(store.delete(model1.nameIdentifier(), Entity.EntityType.MODEL));
+ 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(topic1.nameIdentifier(), Entity.EntityType.TOPIC));
+ Assertions.assertFalse(store.exists(model1.nameIdentifier(), Entity.EntityType.MODEL));
+ Assertions.assertFalse(
+ store.exists(modelVersion1.nameIdentifier(), Entity.EntityType.MODEL_VERSION));
+ Assertions.assertFalse(store.exists(schema1.nameIdentifier(), Entity.EntityType.SCHEMA));
+
+ // Delete again should return false
+ Assertions.assertFalse(store.delete(table1.nameIdentifier(), Entity.EntityType.TABLE));
+ Assertions.assertFalse(store.delete(fileset1.nameIdentifier(), Entity.EntityType.FILESET));
+ Assertions.assertFalse(store.delete(topic1.nameIdentifier(), Entity.EntityType.TOPIC));
+ Assertions.assertFalse(store.delete(model1.nameIdentifier(), Entity.EntityType.MODEL));
+ Assertions.assertFalse(store.delete(schema1.nameIdentifier(), Entity.EntityType.SCHEMA));
+
+ // Now we re-insert schema1, table1, fileset1 and topic1, 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);
+ TopicEntity topic1New =
+ createTopicEntity(
+ RandomIdGenerator.INSTANCE.nextId(),
+ topic1.namespace(),
+ topic1.name(),
+ topic1.auditInfo());
+ store.put(topic1New);
+
+ ModelEntity model1New =
+ createModelEntity(
+ RandomIdGenerator.INSTANCE.nextId(),
+ model1.namespace(),
+ model1.name(),
+ model1.comment(),
+ model1.latestVersion(),
+ model1.properties(),
+ model1.auditInfo());
+ store.put(model1New);
+
+ 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));
+ Assertions.assertEquals(
+ topic1New, store.get(topic1.nameIdentifier(), Entity.EntityType.TOPIC, TopicEntity.class));
+ Assertions.assertEquals(
+ model1New, store.get(model1.nameIdentifier(), Entity.EntityType.MODEL, ModelEntity.class));
+ }
+
+ protected void validateDeleteUser(EntityStore store, UserEntity user1) throws IOException {
+ Assertions.assertTrue(store.exists(user1.nameIdentifier(), Entity.EntityType.USER));
+ Assertions.assertTrue(store.delete(user1.nameIdentifier(), Entity.EntityType.USER));
+ Assertions.assertFalse(store.exists(user1.nameIdentifier(), Entity.EntityType.USER));
+ // delete again should return false
+ Assertions.assertFalse(store.delete(user1.nameIdentifier(), Entity.EntityType.USER));
+
+ UserEntity user =
+ createUser(RandomIdGenerator.INSTANCE.nextId(), "metalake", "user1", user1.auditInfo());
+ store.put(user);
+ Assertions.assertTrue(store.exists(user.nameIdentifier(), Entity.EntityType.USER));
+ }
+
+ protected void validateDeleteGroup(EntityStore store, GroupEntity group1) throws IOException {
+ Assertions.assertTrue(store.delete(group1.nameIdentifier(), EntityType.GROUP));
+ Assertions.assertFalse(store.exists(group1.nameIdentifier(), Entity.EntityType.GROUP));
+ // delete again should return false
+ Assertions.assertFalse(store.delete(group1.nameIdentifier(), Entity.EntityType.GROUP));
+
+ GroupEntity group =
+ createGroup(RandomIdGenerator.INSTANCE.nextId(), "metalake", "group1", group1.auditInfo());
+ store.put(group);
+ Assertions.assertTrue(store.exists(group.nameIdentifier(), EntityType.GROUP));
+ }
+
+ protected void validateDeleteRole(EntityStore store, RoleEntity role1) throws IOException {
+ Assertions.assertTrue(store.delete(role1.nameIdentifier(), EntityType.ROLE));
+ Assertions.assertFalse(store.exists(role1.nameIdentifier(), Entity.EntityType.ROLE));
+ // delete again should return false
+ Assertions.assertFalse(store.delete(role1.nameIdentifier(), Entity.EntityType.ROLE));
+
+ RoleEntity role =
+ createRole(RandomIdGenerator.INSTANCE.nextId(), "metalake", "role1", role1.auditInfo());
+ store.put(role);
+ Assertions.assertTrue(store.exists(role.nameIdentifier(), EntityType.ROLE));
+ }
+
+ protected 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));
+ // delete again should return false
+ Assertions.assertFalse(store.delete(table1InSchema2.nameIdentifier(), Entity.EntityType.TABLE));
+
+ // Make sure all columns are deleted
+ validateDeletedColumns(table1InSchema2.id(), table1InSchema2.type());
+
+ // 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 =
+ createTableEntityWithColumns(
+ RandomIdGenerator.INSTANCE.nextId(),
+ table1InSchema2.namespace(),
+ table1InSchema2.name(),
+ table1InSchema2.auditInfo(),
+ table1InSchema2.columns());
+ store.put(table1InSchema2New);
+ Assertions.assertTrue(store.exists(table1InSchema2.nameIdentifier(), Entity.EntityType.TABLE));
+ }
+
+ protected void validateDeleteModel(
+ EntityStore store,
+ SchemaEntity schema2,
+ ModelEntity model1,
+ ModelVersionEntity modelVersion1,
+ ModelEntity model1InSchema2,
+ ModelVersionEntity modelVersion1InSchema2)
+ throws IOException {
+ Assertions.assertTrue(store.delete(model1InSchema2.nameIdentifier(), Entity.EntityType.MODEL));
+ Assertions.assertFalse(store.exists(model1InSchema2.nameIdentifier(), Entity.EntityType.MODEL));
+ // delete again should return false
+ Assertions.assertFalse(store.delete(model1InSchema2.nameIdentifier(), Entity.EntityType.MODEL));
+
+ Assertions.assertFalse(
+ store.exists(modelVersion1InSchema2.nameIdentifier(), EntityType.MODEL_VERSION));
+ Assertions.assertFalse(
+ store.delete(modelVersion1InSchema2.nameIdentifier(), EntityType.MODEL_VERSION));
+
+ ModelEntity model1Copy =
+ ModelEntity.builder()
+ .withId(model1.id())
+ .withNamespace(model1.namespace())
+ .withName(model1.name())
+ .withComment(model1.comment())
+ .withLatestVersion(model1.latestVersion() + 1)
+ .withProperties(model1.properties())
+ .withAuditInfo(model1.auditInfo())
+ .build();
+
+ Assertions.assertEquals(
+ model1Copy, store.get(model1.nameIdentifier(), Entity.EntityType.MODEL, ModelEntity.class));
+
+ Assertions.assertEquals(
+ modelVersion1,
+ store.get(
+ modelVersion1.nameIdentifier(), EntityType.MODEL_VERSION, ModelVersionEntity.class));
+
+ // Make sure schema 'metalake.catalog.schema2' still exist;
+ Assertions.assertEquals(
+ schema2, store.get(schema2.nameIdentifier(), Entity.EntityType.SCHEMA, SchemaEntity.class));
+ }
+
+ protected static void validateAllEntityExist(
+ BaseMetalake metalake,
+ EntityStore store,
+ CatalogEntity catalog,
+ CatalogEntity catalogCopy,
+ SchemaEntity schema1,
+ SchemaEntity schema2,
+ TableEntity table1,
+ TableEntity table1InSchema2,
+ FilesetEntity fileset1,
+ FilesetEntity fileset1InSchema2,
+ TopicEntity topic1,
+ TopicEntity topic1InSchema2,
+ ModelEntity model1,
+ ModelVersionEntity modelVersion1,
+ ModelEntity model1InSchema2,
+ ModelVersionEntity modelVersion1InSchema2,
+ UserEntity user1,
+ UserEntity user2,
+ GroupEntity group1,
+ GroupEntity group2,
+ RoleEntity role1,
+ RoleEntity role2)
+ 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));
+ Assertions.assertEquals(
+ topic1, store.get(topic1.nameIdentifier(), Entity.EntityType.TOPIC, TopicEntity.class));
+ Assertions.assertEquals(
+ topic1InSchema2,
+ store.get(topic1InSchema2.nameIdentifier(), Entity.EntityType.TOPIC, TopicEntity.class));
+
+ ModelEntity model1Copy =
+ ModelEntity.builder()
+ .withId(model1.id())
+ .withNamespace(model1.namespace())
+ .withName(model1.name())
+ .withComment(model1.comment())
+ .withLatestVersion(model1.latestVersion() + 1)
+ .withProperties(model1.properties())
+ .withAuditInfo(model1.auditInfo())
+ .build();
+
+ Assertions.assertEquals(
+ model1Copy, store.get(model1.nameIdentifier(), Entity.EntityType.MODEL, ModelEntity.class));
+ Assertions.assertEquals(
+ modelVersion1,
+ store.get(
+ modelVersion1.nameIdentifier(), EntityType.MODEL_VERSION, ModelVersionEntity.class));
+
+ ModelEntity model1InSchema2Copy =
+ ModelEntity.builder()
+ .withId(model1InSchema2.id())
+ .withNamespace(model1InSchema2.namespace())
+ .withName(model1InSchema2.name())
+ .withComment(model1InSchema2.comment())
+ .withLatestVersion(model1InSchema2.latestVersion() + 1)
+ .withProperties(model1InSchema2.properties())
+ .withAuditInfo(model1InSchema2.auditInfo())
+ .build();
+
+ Assertions.assertEquals(
+ model1InSchema2Copy,
+ store.get(model1InSchema2.nameIdentifier(), EntityType.MODEL, ModelEntity.class));
+ Assertions.assertEquals(
+ modelVersion1InSchema2,
+ store.get(
+ modelVersion1InSchema2.nameIdentifier(),
+ EntityType.MODEL_VERSION,
+ ModelVersionEntity.class));
+
+ Assertions.assertEquals(
+ user1, store.get(user1.nameIdentifier(), Entity.EntityType.USER, UserEntity.class));
+ Assertions.assertEquals(
+ user2, store.get(user2.nameIdentifier(), Entity.EntityType.USER, UserEntity.class));
+ Assertions.assertEquals(
+ group1, store.get(group1.nameIdentifier(), Entity.EntityType.GROUP, GroupEntity.class));
+ Assertions.assertEquals(
+ group2, store.get(group2.nameIdentifier(), Entity.EntityType.GROUP, GroupEntity.class));
+ Assertions.assertEquals(
+ role1, store.get(role1.nameIdentifier(), Entity.EntityType.ROLE, RoleEntity.class));
+ Assertions.assertEquals(
+ role2, store.get(role2.nameIdentifier(), Entity.EntityType.ROLE, RoleEntity.class));
+ }
+
+ protected 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));
+ }
+
+ protected 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));
+ }
+
+ protected void validateDeletedTopic(EntityStore store) throws IOException {
+ store.delete(
+ NameIdentifier.of("metalakeChanged", "catalogChanged", "schema2", "topic1"),
+ Entity.EntityType.TOPIC);
+ // Update a deleted entities
+ Assertions.assertThrowsExactly(
+ NoSuchEntityException.class,
+ () ->
+ store.update(
+ NameIdentifier.of("metalakeChanged", "catalogChanged", "schema2", "topic1"),
+ TopicEntity.class,
+ Entity.EntityType.TOPIC,
+ (e) -> e));
+ }
+
+ protected void validateTopicChanged(EntityStore store, TopicEntity topicEntity)
+ throws IOException {
+ // Check topic entities
+ store.update(
+ NameIdentifier.of("metalakeChanged", "catalogChanged", "schemaChanged", "topic1"),
+ TopicEntity.class,
+ Entity.EntityType.TOPIC,
+ e -> {
+ AuditInfo auditInfo1 =
+ AuditInfo.builder().withCreator("creator6").withCreateTime(Instant.now()).build();
+ return createTopicEntity(
+ topicEntity.id(),
+ Namespace.of("metalakeChanged", "catalogChanged", "schemaChanged"),
+ "topicChanged",
+ auditInfo1);
+ });
+
+ Assertions.assertThrowsExactly(
+ NoSuchEntityException.class,
+ () ->
+ store.get(
+ NameIdentifier.of("metalakeChanged", "catalogChanged", "schema1", "topic1"),
+ Entity.EntityType.TOPIC,
+ TopicEntity.class));
+ TopicEntity updatedTopic =
+ store.get(
+ NameIdentifier.of("metalakeChanged", "catalogChanged", "schemaChanged", "topicChanged"),
+ Entity.EntityType.TOPIC,
+ TopicEntity.class);
+ Assertions.assertEquals("creator6", updatedTopic.auditInfo().creator());
+
+ Assertions.assertNotNull(
+ store.get(
+ NameIdentifier.of("metalakeChanged", "catalogChanged", "schema2", "topic1"),
+ Entity.EntityType.TOPIC,
+ TopicEntity.class));
+ }
+
+ protected 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());
+ }
+
+ protected void validateAlreadyExistEntity(EntityStore store, SchemaEntity schema) {
+ // The updated entities already existed, should throw exception
+ Assertions.assertThrowsExactly(
+ EntityAlreadyExistsException.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);
+ }));
+ }
+
+ protected 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));
+ }
+
+ protected 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));
+ }
+
+ protected 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));
+ }
+
+ protected 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));
+ }
+
+ protected 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));
+ }
+
+ protected List<Pair<Long, Pair<Long, Long>>> listAllColumnWithEntityId(
+ Long entityId, Entity.EntityType entityType) {
+ String queryTemp =
+ "SELECT column_id, table_version, deleted_at FROM "
+ + "table_column_version_info WHERE %s = %d";
+ String query;
+ switch (entityType) {
+ case TABLE:
+ query = String.format(queryTemp, "table_id", entityId);
+ break;
+ case SCHEMA:
+ query = String.format(queryTemp, "schema_id", entityId);
+ break;
+ case CATALOG:
+ query = String.format(queryTemp, "catalog_id", entityId);
+ break;
+ case METALAKE:
+ query = String.format(queryTemp, "metalake_id", entityId);
+ break;
+ default:
+ throw new IllegalArgumentException("Unsupported entity type: " + entityType);
+ }
+
+ List<Pair<Long, Pair<Long, Long>>> results = Lists.newArrayList();
+ try (SqlSession sqlSession =
+ SqlSessionFactoryHelper.getInstance().getSqlSessionFactory().openSession(true)) {
+ Connection connection = sqlSession.getConnection();
+ Statement statement = connection.createStatement();
+
+ ResultSet rs = statement.executeQuery(query);
+ while (rs.next()) {
+ results.add(
+ Pair.of(
+ rs.getLong("column_id"),
+ Pair.of(rs.getLong("table_version"), rs.getLong("deleted_at"))));
+ }
+ } catch (SQLException e) {
+ throw new RuntimeException(e);
+ }
+
+ return results;
+ }
+
+ protected void validateDeletedColumns(Long entityId, Entity.EntityType entityType) {
+ List<Pair<Long, Pair<Long, Long>>> deleteResult =
+ listAllColumnWithEntityId(entityId, entityType);
+ deleteResult.forEach(p -> Assertions.assertTrue(p.getRight().getRight() > 0));
+ }
+
+ protected UserEntity createUserEntity(
+ Long id, Namespace namespace, String name, AuditInfo auditInfo) {
+ return UserEntity.builder()
+ .withId(id)
+ .withName(name)
+ .withNamespace(namespace)
+ .withRoleNames(null)
+ .withRoleIds(null)
+ .withAuditInfo(auditInfo)
+ .build();
+ }
+
+ protected RoleEntity createRoleEntity(
+ Long id, Namespace namespace, String name, AuditInfo auditInfo, String catalogName) {
+ SecurableObject securableObject =
+ SecurableObjects.ofCatalog(catalogName, Lists.newArrayList(Privileges.UseCatalog.allow()));
+
+ return RoleEntity.builder()
+ .withId(id)
+ .withName(name)
+ .withNamespace(namespace)
+ .withProperties(null)
+ .withAuditInfo(auditInfo)
+ .withSecurableObjects(Lists.newArrayList(securableObject))
+ .build();
+ }
+
+ protected ModelEntity createModelEntity(
+ Long id,
+ Namespace namespace,
+ String name,
+ String comment,
+ Integer latestVersion,
+ Map<String, String> properties,
+ AuditInfo auditInfo) {
+ return ModelEntity.builder()
+ .withId(id)
+ .withName(name)
+ .withNamespace(namespace)
+ .withComment(comment)
+ .withLatestVersion(latestVersion)
+ .withProperties(properties)
+ .withAuditInfo(auditInfo)
+ .build();
+ }
+
+ protected ModelVersionEntity createModelVersionEntity(
+ NameIdentifier modelId,
+ Integer version,
+ Map<String, String> modelUris,
+ List<String> aliases,
+ String comment,
+ Map<String, String> properties,
+ AuditInfo auditInfo) {
+ return ModelVersionEntity.builder()
+ .withModelIdentifier(modelId)
+ .withVersion(version)
+ .withUris(modelUris)
+ .withAliases(aliases)
+ .withComment(comment)
+ .withProperties(properties)
+ .withAuditInfo(auditInfo)
+ .build();
+ }
+}
diff --git a/core/src/test/java/org/apache/gravitino/storage/TestEntityStorage.java b/core/src/test/java/org/apache/gravitino/storage/TestEntityStorage.java
index 54846ad241..92b17a8b9f 100644
--- a/core/src/test/java/org/apache/gravitino/storage/TestEntityStorage.java
+++ b/core/src/test/java/org/apache/gravitino/storage/TestEntityStorage.java
@@ -19,264 +19,46 @@
package org.apache.gravitino.storage;
-import static org.apache.gravitino.Configs.DEFAULT_ENTITY_RELATIONAL_STORE;
-import static org.apache.gravitino.Configs.ENTITY_RELATIONAL_JDBC_BACKEND_DRIVER;
-import static org.apache.gravitino.Configs.ENTITY_RELATIONAL_JDBC_BACKEND_MAX_CONNECTIONS;
-import static org.apache.gravitino.Configs.ENTITY_RELATIONAL_JDBC_BACKEND_PASSWORD;
-import static org.apache.gravitino.Configs.ENTITY_RELATIONAL_JDBC_BACKEND_PATH;
-import static org.apache.gravitino.Configs.ENTITY_RELATIONAL_JDBC_BACKEND_URL;
-import static org.apache.gravitino.Configs.ENTITY_RELATIONAL_JDBC_BACKEND_USER;
-import static org.apache.gravitino.Configs.ENTITY_RELATIONAL_JDBC_BACKEND_WAIT_MILLISECONDS;
-import static org.apache.gravitino.Configs.ENTITY_RELATIONAL_STORE;
-import static org.apache.gravitino.Configs.ENTITY_STORE;
-import static org.apache.gravitino.Configs.RELATIONAL_ENTITY_STORE;
-import static org.apache.gravitino.Configs.STORE_DELETE_AFTER_TIME;
-import static org.apache.gravitino.Configs.VERSION_RETENTION_COUNT;
-import static org.apache.gravitino.file.Fileset.LOCATION_NAME_UNKNOWN;
-
-import com.github.benmanes.caffeine.cache.Cache;
-import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
-import java.io.File;
import java.io.IOException;
-import java.lang.reflect.Field;
-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.Collections;
-import java.util.List;
-import java.util.Map;
-import java.util.UUID;
-import java.util.stream.Collectors;
-import org.apache.commons.io.FileUtils;
-import org.apache.commons.lang3.StringUtils;
-import org.apache.commons.lang3.reflect.FieldUtils;
-import org.apache.commons.lang3.tuple.Pair;
-import org.apache.gravitino.Catalog;
import org.apache.gravitino.Config;
import org.apache.gravitino.Configs;
import org.apache.gravitino.Entity;
-import org.apache.gravitino.Entity.EntityType;
-import org.apache.gravitino.EntityAlreadyExistsException;
import org.apache.gravitino.EntityStore;
import org.apache.gravitino.EntityStoreFactory;
-import org.apache.gravitino.GravitinoEnv;
import org.apache.gravitino.NameIdentifier;
import org.apache.gravitino.Namespace;
-import org.apache.gravitino.SupportsRelationOperations;
import org.apache.gravitino.authorization.AuthorizationUtils;
-import org.apache.gravitino.authorization.Privilege.Condition;
-import org.apache.gravitino.authorization.Privileges;
-import org.apache.gravitino.authorization.Role;
-import org.apache.gravitino.authorization.SecurableObject;
-import org.apache.gravitino.authorization.SecurableObjects;
-import org.apache.gravitino.cache.CaffeineEntityCache;
-import org.apache.gravitino.cache.EntityCacheKey;
-import org.apache.gravitino.cache.EntityCacheRelationKey;
-import org.apache.gravitino.cache.ReverseIndexCache;
import org.apache.gravitino.exceptions.NoSuchEntityException;
-import org.apache.gravitino.exceptions.NonEmptyEntityException;
-import org.apache.gravitino.file.Fileset;
-import org.apache.gravitino.integration.test.container.ContainerSuite;
-import org.apache.gravitino.integration.test.util.BaseIT;
import org.apache.gravitino.meta.AuditInfo;
import org.apache.gravitino.meta.BaseMetalake;
import org.apache.gravitino.meta.CatalogEntity;
import org.apache.gravitino.meta.ColumnEntity;
import org.apache.gravitino.meta.FilesetEntity;
-import org.apache.gravitino.meta.GenericEntity;
import org.apache.gravitino.meta.GroupEntity;
import org.apache.gravitino.meta.ModelEntity;
import org.apache.gravitino.meta.ModelVersionEntity;
import org.apache.gravitino.meta.RoleEntity;
import org.apache.gravitino.meta.SchemaEntity;
-import org.apache.gravitino.meta.SchemaVersion;
import org.apache.gravitino.meta.TableEntity;
-import org.apache.gravitino.meta.TagEntity;
import org.apache.gravitino.meta.TopicEntity;
import org.apache.gravitino.meta.UserEntity;
import org.apache.gravitino.model.ModelVersion;
-import org.apache.gravitino.rel.types.Type;
import org.apache.gravitino.rel.types.Types;
-import org.apache.gravitino.storage.relational.RelationalBackend;
-import org.apache.gravitino.storage.relational.RelationalEntityStore;
-import org.apache.gravitino.storage.relational.RelationalGarbageCollector;
-import org.apache.gravitino.storage.relational.converters.H2ExceptionConverter;
-import org.apache.gravitino.storage.relational.converters.MySQLExceptionConverter;
-import org.apache.gravitino.storage.relational.converters.PostgreSQLExceptionConverter;
-import org.apache.gravitino.storage.relational.converters.SQLExceptionConverterFactory;
-import org.apache.gravitino.storage.relational.session.SqlSessionFactoryHelper;
-import org.apache.gravitino.utils.NameIdentifierUtil;
-import org.apache.gravitino.utils.NamespaceUtil;
-import org.apache.ibatis.session.SqlSession;
-import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import org.mockito.Mockito;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
+// Note: Do not add more tests in this class as it's already getting too large. If needed, please
+// just extend AbstractEntityStoreTest and add a new test class. More, please refer to
+// `TestEntityStoreForLance` or `TestEntityStoreRelationCache`.
@Tag("gravitino-docker-test")
-public class TestEntityStorage {
- private static final Logger LOG = LoggerFactory.getLogger(TestEntityStorage.class);
-
- 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";
- private static final String H2_FILE = DB_DIR + ".mv.db";
-
- static Object[][] storageProvider() {
- return new Object[][] {
- {"h2", true},
- {"h2", false},
- {"mysql", true},
- {"mysql", false},
- {"postgresql", true},
- {"postgresql", false}
- };
- }
-
- @AfterEach
- void closeSuit() throws IOException {
- // todo: refactor TestEntityStorage to extend TestJDBCBackend, otherwise, each test will start
- // and stop the container suite.
- ContainerSuite.getInstance().close();
- }
-
- private void init(String type, Config config) throws IllegalAccessException {
- Preconditions.checkArgument(StringUtils.isNotBlank(type));
- 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_PATH)).thenReturn(DB_DIR);
- Mockito.when(config.get(ENTITY_RELATIONAL_JDBC_BACKEND_MAX_CONNECTIONS)).thenReturn(100);
- Mockito.when(config.get(ENTITY_RELATIONAL_JDBC_BACKEND_WAIT_MILLISECONDS)).thenReturn(1000L);
- Mockito.when(config.get(STORE_DELETE_AFTER_TIME)).thenReturn(20 * 60 * 1000L);
- Mockito.when(config.get(VERSION_RETENTION_COUNT)).thenReturn(1L);
- // Fix cache config for test
- Mockito.when(config.get(Configs.CACHE_MAX_ENTRIES)).thenReturn(10_000);
- Mockito.when(config.get(Configs.CACHE_EXPIRATION_TIME)).thenReturn(3_600_000L);
- Mockito.when(config.get(Configs.CACHE_WEIGHER_ENABLED)).thenReturn(true);
- Mockito.when(config.get(Configs.CACHE_STATS_ENABLED)).thenReturn(false);
- Mockito.when(config.get(Configs.CACHE_IMPLEMENTATION)).thenReturn("caffeine");
- Mockito.when(config.get(Configs.CACHE_LOCK_SEGMENTS)).thenReturn(16);
-
- FieldUtils.writeField(GravitinoEnv.getInstance(), "config", config, true);
-
- BaseIT baseIT = new BaseIT();
-
- try {
- if (type.equalsIgnoreCase("h2")) {
- // The following properties are used to create the JDBC connection; they are just for test,
- // in the real world, they will be set automatically by the configuration file if you set
- // ENTITY_RELATIONAL_STORE as EMBEDDED_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("gravitino");
- Mockito.when(config.get(ENTITY_RELATIONAL_JDBC_BACKEND_PASSWORD)).thenReturn("gravitino");
- Mockito.when(config.get(ENTITY_RELATIONAL_JDBC_BACKEND_DRIVER)).thenReturn("org.h2.Driver");
- Mockito.when(config.get(ENTITY_RELATIONAL_JDBC_BACKEND_MAX_CONNECTIONS)).thenReturn(100);
- Mockito.when(config.get(ENTITY_RELATIONAL_JDBC_BACKEND_WAIT_MILLISECONDS))
- .thenReturn(1000L);
-
- FieldUtils.writeStaticField(
- SQLExceptionConverterFactory.class, "converter", new H2ExceptionConverter(), true);
-
- } else if (type.equalsIgnoreCase("mysql")) {
- String mysqlJdbcUrl = baseIT.startAndInitMySQLBackend();
- Mockito.when(config.get(ENTITY_RELATIONAL_JDBC_BACKEND_URL)).thenReturn(mysqlJdbcUrl);
- Mockito.when(config.get(ENTITY_RELATIONAL_JDBC_BACKEND_USER)).thenReturn("root");
- Mockito.when(config.get(ENTITY_RELATIONAL_JDBC_BACKEND_PASSWORD)).thenReturn("root");
- Mockito.when(config.get(ENTITY_RELATIONAL_JDBC_BACKEND_DRIVER))
- .thenReturn("com.mysql.cj.jdbc.Driver");
- Mockito.when(config.get(ENTITY_RELATIONAL_JDBC_BACKEND_MAX_CONNECTIONS)).thenReturn(100);
- Mockito.when(config.get(ENTITY_RELATIONAL_JDBC_BACKEND_WAIT_MILLISECONDS))
- .thenReturn(1000L);
-
- FieldUtils.writeStaticField(
- SQLExceptionConverterFactory.class, "converter", new MySQLExceptionConverter(), true);
-
- } else if (type.equalsIgnoreCase("postgresql")) {
- String postgreSQLJdbcUrl = baseIT.startAndInitPGBackend();
- Mockito.when(config.get(ENTITY_RELATIONAL_JDBC_BACKEND_URL)).thenReturn(postgreSQLJdbcUrl);
- Mockito.when(config.get(ENTITY_RELATIONAL_JDBC_BACKEND_USER)).thenReturn("root");
- Mockito.when(config.get(ENTITY_RELATIONAL_JDBC_BACKEND_PASSWORD)).thenReturn("root");
- Mockito.when(config.get(ENTITY_RELATIONAL_JDBC_BACKEND_DRIVER))
- .thenReturn("org.postgresql.Driver");
-
- FieldUtils.writeStaticField(
- SQLExceptionConverterFactory.class,
- "converter",
- new PostgreSQLExceptionConverter(),
- true);
-
- RelationalEntityStore store =
- (RelationalEntityStore) EntityStoreFactory.createEntityStore(config);
- store.initialize(config);
- Field f = FieldUtils.getField(RelationalEntityStore.class, "backend", true);
- RelationalBackend backend = (RelationalBackend) f.get(store);
- RelationalGarbageCollector garbageCollector =
- new RelationalGarbageCollector(backend, config);
- garbageCollector.collectAndClean();
-
- } else {
- throw new UnsupportedOperationException("Unsupported entity store type: " + type);
- }
- } catch (Exception e) {
- LOG.error("Failed to init entity store", e);
- throw new RuntimeException(e);
- }
- }
-
- private void destroy(String type) {
- Preconditions.checkArgument(StringUtils.isNotBlank(type));
- if (type.equalsIgnoreCase("h2") || type.equalsIgnoreCase("mysql")) {
- dropAllTables();
- File dir = new File(DB_DIR);
- if (dir.exists()) {
- dir.delete();
- }
-
- FileUtils.deleteQuietly(new File(H2_FILE));
- } else if (type.equalsIgnoreCase("postgresql")) {
- // Do nothing
- } 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);
- }
- }
+public class TestEntityStorage extends AbstractEntityStorageTest {
@ParameterizedTest
@MethodSource("storageProvider")
@@ -352,7 +134,6 @@ public class TestEntityStorage {
RoleEntity role1 =
createRole(RandomIdGenerator.INSTANCE.nextId(), "metalake", "role1", auditInfo);
- // Store all entities
store.put(metalake);
store.put(catalog);
store.put(catalogCopy);
@@ -416,13 +197,13 @@ public class TestEntityStorage {
() ->
store.get(
NameIdentifier.of("metalake", "catalog", "schema1", "model1", "alias1"),
- EntityType.MODEL_VERSION,
+ Entity.EntityType.MODEL_VERSION,
ModelVersionEntity.class));
Assertions.assertDoesNotThrow(
() ->
store.get(
NameIdentifier.of("metalake", "catalog", "schema1", "model1", "alias2"),
- EntityType.MODEL_VERSION,
+ Entity.EntityType.MODEL_VERSION,
ModelVersionEntity.class));
Assertions.assertDoesNotThrow(
@@ -436,18 +217,17 @@ public class TestEntityStorage {
() ->
store.get(
AuthorizationUtils.ofGroup("metalake", "group1"),
- EntityType.GROUP,
+ Entity.EntityType.GROUP,
GroupEntity.class));
Assertions.assertDoesNotThrow(
() ->
store.get(
AuthorizationUtils.ofRole("metalake", "role1"),
- EntityType.ROLE,
+ Entity.EntityType.ROLE,
RoleEntity.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);
@@ -501,13 +281,13 @@ public class TestEntityStorage {
() ->
store.get(
NameIdentifier.of("metalake", "catalog", "schema1", "model1", "alias1"),
- EntityType.MODEL_VERSION,
+ Entity.EntityType.MODEL_VERSION,
ModelVersionEntity.class));
Assertions.assertDoesNotThrow(
() ->
store.get(
NameIdentifier.of("metalake", "catalog", "schema1", "model1", "alias2"),
- EntityType.MODEL_VERSION,
+ Entity.EntityType.MODEL_VERSION,
ModelVersionEntity.class));
Assertions.assertDoesNotThrow(
@@ -532,117 +312,12 @@ public class TestEntityStorage {
}
}
- @ParameterizedTest
- @MethodSource("storageProvider")
- void testEntityUpdate(String type, boolean enableCache) throws Exception {
- Config config = Mockito.mock(Config.class);
- Mockito.when(config.get(Configs.CACHE_ENABLED)).thenReturn(enableCache);
-
- init(type, config);
- AuditInfo auditInfo =
- AuditInfo.builder().withCreator("creator").withCreateTime(Instant.now()).build();
-
- try (EntityStore store = EntityStoreFactory.createEntityStore(config)) {
- store.initialize(config);
-
- 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);
- TopicEntity topic1 =
- createTopicEntity(
- RandomIdGenerator.INSTANCE.nextId(),
- Namespace.of("metalake", "catalog", "schema1"),
- "topic1",
- 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);
- TopicEntity topic1InSchema2 =
- createTopicEntity(
- RandomIdGenerator.INSTANCE.nextId(),
- Namespace.of("metalake", "catalog", "schema2"),
- "topic1",
- 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);
- store.put(topic1);
- store.put(topic1InSchema2);
-
- validateMetalakeChanged(store, metalake);
- validateCatalogChanged(store, catalog);
- validateSchemaChanged(store, schema1);
- validateTableChanged(store, table1);
- validateFilesetChanged(store, fileset1);
- validateTopicChanged(store, topic1);
- validateDeletedTable(store);
- validateDeletedFileset(store);
- validateDeletedTopic(store);
- validateAlreadyExistEntity(store, schema2);
- validateNotChangedEntity(store, schema2);
-
- destroy(type);
- }
- }
-
@ParameterizedTest
@MethodSource("storageProvider")
public void testAuthorizationEntityDelete(String type, boolean enableCache)
throws IOException, IllegalAccessException {
Config config = Mockito.mock(Config.class);
Mockito.when(config.get(Configs.CACHE_ENABLED)).thenReturn(enableCache);
-
init(type, config);
AuditInfo auditInfo =
@@ -692,7 +367,6 @@ public class TestEntityStorage {
throws IOException, IllegalAccessException {
Config config = Mockito.mock(Config.class);
Mockito.when(config.get(Configs.CACHE_ENABLED)).thenReturn(enableCache);
-
init(type, config);
AuditInfo auditInfo =
@@ -793,7 +467,6 @@ public class TestEntityStorage {
RoleEntity role1 = createRole(1L, "metalake", "role1", auditInfo);
RoleEntity role2 = createRole(2L, "metalake", "role2", auditInfo);
- // Store all entities
store.put(metalake);
store.put(catalog);
store.put(catalogCopy);
@@ -841,22 +514,14 @@ public class TestEntityStorage {
role2);
validateDeleteUser(store, user1);
-
validateDeleteGroup(store, group1);
-
validateDeleteRole(store, role1);
-
validateDeleteTable(store, schema2, table1, table1InSchema2);
-
validateDeleteFileset(store, schema2, fileset1, fileset1InSchema2);
-
validateDeleteTopic(store, schema2, topic1, topic1InSchema2);
-
validateDeleteModel(
store, schema2, model1, modelVersion1, model1InSchema2, modelVersion1InSchema2);
-
validateDeleteSchema(store, schema1, table1, fileset1, topic1, model1, modelVersion1);
-
validateDeleteCatalog(
store,
catalog,
@@ -870,16 +535,13 @@ public class TestEntityStorage {
topic1InSchema2,
model1,
model1InSchema2);
-
validateDeleteMetalake(store, metalake, catalogCopy, user2, group2, role2);
- // Store all entities again
- // metalake
BaseMetalake metalakeNew =
createBaseMakeLake(
RandomIdGenerator.INSTANCE.nextId(), metalake.name(), metalake.auditInfo());
store.put(metalakeNew);
- // catalog
+
CatalogEntity catalogNew =
createCatalog(
RandomIdGenerator.INSTANCE.nextId(),
@@ -894,7 +556,7 @@ public class TestEntityStorage {
catalogCopy.name(),
(AuditInfo) catalogCopy.auditInfo());
store.put(catalogCopyNew);
- // schema
+
SchemaEntity schema1New =
createSchemaEntity(
RandomIdGenerator.INSTANCE.nextId(),
@@ -909,7 +571,7 @@ public class TestEntityStorage {
schema2.name(),
schema2.auditInfo());
store.put(schema2New);
- // table
+
TableEntity table1New =
createTableEntityWithColumns(
RandomIdGenerator.INSTANCE.nextId(),
@@ -926,7 +588,7 @@ public class TestEntityStorage {
table1InSchema2.auditInfo(),
table1InSchema2.columns());
store.put(table1InSchema2New);
- // fileset
+
FilesetEntity fileset1New =
createFilesetEntity(
RandomIdGenerator.INSTANCE.nextId(),
@@ -941,7 +603,7 @@ public class TestEntityStorage {
fileset1InSchema2.name(),
fileset1InSchema2.auditInfo());
store.put(fileset1InSchema2New);
- // topic
+
TopicEntity topic1New =
createTopicEntity(
RandomIdGenerator.INSTANCE.nextId(),
@@ -957,7 +619,6 @@ public class TestEntityStorage {
topic1InSchema2.auditInfo());
store.put(topic1InSchema2New);
- // model
ModelEntity model1New =
createModelEntity(
RandomIdGenerator.INSTANCE.nextId(),
@@ -980,17 +641,11 @@ public class TestEntityStorage {
store.put(roleNew);
validateDeleteTableCascade(store, table1New);
-
validateDeleteFilesetCascade(store, fileset1New);
-
validateDeleteTopicCascade(store, topic1New);
-
validateDeleteModelCascade(store, model1New);
-
validateDeleteSchemaCascade(store, schema1New, table1New, fileset1New, topic1New, model1New);
-
validateDeleteCatalogCascade(store, catalogNew, schema2New);
-
validateDeleteMetalakeCascade(
store, metalakeNew, catalogNew, schema2New, userNew, groupNew, roleNew);
@@ -1000,167 +655,102 @@ public class TestEntityStorage {
@ParameterizedTest
@MethodSource("storageProvider")
- void testSameNameUnderANameSpace(String type, boolean enableCache)
- throws IOException, IllegalAccessException {
+ void testEntityUpdate(String type, boolean enableCache) throws Exception, IllegalAccessException {
Config config = Mockito.mock(Config.class);
Mockito.when(config.get(Configs.CACHE_ENABLED)).thenReturn(enableCache);
-
init(type, config);
+ AuditInfo auditInfo =
+ AuditInfo.builder().withCreator("creator").withCreateTime(Instant.now()).build();
+
try (EntityStore store = EntityStoreFactory.createEntityStore(config)) {
store.initialize(config);
- AuditInfo auditInfo =
- AuditInfo.builder().withCreator("creator").withCreateTime(Instant.now()).build();
-
- BaseMetalake metalake1 =
- createBaseMakeLake(RandomIdGenerator.INSTANCE.nextId(), "metalake1", auditInfo);
- CatalogEntity catalog1 =
+ 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("metalake1"),
- "catalog1",
+ Namespace.of("metalake"),
+ "catalogCopy",
auditInfo);
+
SchemaEntity schema1 =
createSchemaEntity(
RandomIdGenerator.INSTANCE.nextId(),
- Namespace.of("metalake1", "catalog1"),
+ Namespace.of("metalake", "catalog"),
"schema1",
auditInfo);
-
- Namespace namespace = Namespace.of("metalake1", "catalog1", "schema1");
TableEntity table1 =
- createTableEntity(RandomIdGenerator.INSTANCE.nextId(), namespace, "sameName", auditInfo);
-
- FilesetEntity filesetEntity1 =
+ createTableEntity(
+ RandomIdGenerator.INSTANCE.nextId(),
+ Namespace.of("metalake", "catalog", "schema1"),
+ "table1",
+ auditInfo);
+ FilesetEntity fileset1 =
createFilesetEntity(
- RandomIdGenerator.INSTANCE.nextId(), namespace, "sameName", auditInfo);
-
- TopicEntity topicEntity1 =
- createTopicEntity(RandomIdGenerator.INSTANCE.nextId(), namespace, "sameName", auditInfo);
+ RandomIdGenerator.INSTANCE.nextId(),
+ Namespace.of("metalake", "catalog", "schema1"),
+ "fileset1",
+ auditInfo);
+ TopicEntity topic1 =
+ createTopicEntity(
+ RandomIdGenerator.INSTANCE.nextId(),
+ Namespace.of("metalake", "catalog", "schema1"),
+ "topic1",
+ auditInfo);
- ModelEntity model1 =
- createModelEntity(
+ SchemaEntity schema2 =
+ createSchemaEntity(
RandomIdGenerator.INSTANCE.nextId(),
- namespace,
- "sameName",
- "model1",
- 1,
- null,
+ 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);
+ TopicEntity topic1InSchema2 =
+ createTopicEntity(
+ RandomIdGenerator.INSTANCE.nextId(),
+ Namespace.of("metalake", "catalog", "schema2"),
+ "topic1",
auditInfo);
- store.put(metalake1);
- store.put(catalog1);
+ store.put(metalake);
+ store.put(catalog);
+ store.put(catalogCopy);
store.put(schema1);
store.put(table1);
- store.put(filesetEntity1);
- store.put(topicEntity1);
- store.put(model1);
+ store.put(fileset1);
+ store.put(topic1);
+ store.put(schema2);
+ store.put(table1InSchema2);
+ store.put(fileset1InSchema2);
+ store.put(topic1InSchema2);
- NameIdentifier identifier = NameIdentifier.of("metalake1", "catalog1", "schema1", "sameName");
-
- TableEntity loadedTableEntity =
- store.get(identifier, Entity.EntityType.TABLE, TableEntity.class);
- Assertions.assertEquals(table1.id(), loadedTableEntity.id());
- FilesetEntity loadedFilesetEntity =
- store.get(identifier, Entity.EntityType.FILESET, FilesetEntity.class);
- Assertions.assertEquals(filesetEntity1.id(), loadedFilesetEntity.id());
- TopicEntity loadedTopicEntity =
- store.get(identifier, Entity.EntityType.TOPIC, TopicEntity.class);
- Assertions.assertEquals(topicEntity1.id(), loadedTopicEntity.id());
- ModelEntity loadedModelEntity =
- store.get(identifier, Entity.EntityType.MODEL, ModelEntity.class);
- Assertions.assertEquals(model1.id(), loadedModelEntity.id());
-
- // Remove table will not affect another
- Assertions.assertTrue(store.delete(identifier, Entity.EntityType.TABLE));
- Assertions.assertNotNull(
- store.get(identifier, Entity.EntityType.FILESET, FilesetEntity.class));
- Assertions.assertNotNull(store.get(identifier, Entity.EntityType.TOPIC, TopicEntity.class));
- Assertions.assertNotNull(store.get(identifier, Entity.EntityType.MODEL, ModelEntity.class));
-
- // JDBC use id as the primary key, so we need to change the id of table1 if we want to store
- // it again
- table1 =
- createTableEntity(RandomIdGenerator.INSTANCE.nextId(), namespace, "sameName", auditInfo);
- store.put(table1);
-
- // Remove fileset will not affect another
- store.delete(identifier, Entity.EntityType.FILESET);
- Assertions.assertNotNull(store.get(identifier, Entity.EntityType.TABLE, TableEntity.class));
- Assertions.assertNotNull(store.get(identifier, Entity.EntityType.TOPIC, TopicEntity.class));
- Assertions.assertNotNull(store.get(identifier, Entity.EntityType.MODEL, ModelEntity.class));
-
- filesetEntity1 =
- createFilesetEntity(
- RandomIdGenerator.INSTANCE.nextId(), namespace, "sameName", auditInfo);
- store.put(filesetEntity1);
-
- // Remove topic will not affect another
- store.delete(identifier, Entity.EntityType.TOPIC);
- Assertions.assertNotNull(store.get(identifier, Entity.EntityType.TABLE, TableEntity.class));
- Assertions.assertNotNull(
- store.get(identifier, Entity.EntityType.FILESET, FilesetEntity.class));
- Assertions.assertNotNull(store.get(identifier, Entity.EntityType.MODEL, ModelEntity.class));
-
- topicEntity1 =
- createTopicEntity(RandomIdGenerator.INSTANCE.nextId(), namespace, "sameName", auditInfo);
- store.put(topicEntity1);
-
- // Rename table will not affect another
- long table1Id = table1.id();
- store.update(
- identifier,
- TableEntity.class,
- Entity.EntityType.TABLE,
- e -> createTableEntity(table1Id, namespace, "sameNameChanged", e.auditInfo()));
-
- NameIdentifier changedNameIdentifier =
- NameIdentifier.of("metalake1", "catalog1", "schema1", "sameNameChanged");
- Assertions.assertNotNull(
- store.get(changedNameIdentifier, Entity.EntityType.TABLE, TableEntity.class));
- Assertions.assertNotNull(
- store.get(identifier, Entity.EntityType.FILESET, FilesetEntity.class));
- Assertions.assertNotNull(store.get(identifier, Entity.EntityType.TOPIC, TopicEntity.class));
- Assertions.assertNotNull(store.get(identifier, Entity.EntityType.MODEL, ModelEntity.class));
-
- table1 =
- createTableEntity(RandomIdGenerator.INSTANCE.nextId(), namespace, "sameName", auditInfo);
- store.put(table1);
-
- // Rename fileset will not affect another
- long filesetId = filesetEntity1.id();
- store.update(
- identifier,
- FilesetEntity.class,
- Entity.EntityType.FILESET,
- e -> createFilesetEntity(filesetId, namespace, "sameNameChanged", e.auditInfo()));
-
- Assertions.assertNotNull(store.get(identifier, Entity.EntityType.TABLE, TableEntity.class));
- Assertions.assertNotNull(
- store.get(changedNameIdentifier, Entity.EntityType.FILESET, FilesetEntity.class));
- Assertions.assertNotNull(store.get(identifier, Entity.EntityType.TOPIC, TopicEntity.class));
- Assertions.assertNotNull(store.get(identifier, Entity.EntityType.MODEL, ModelEntity.class));
-
- filesetEntity1 =
- createFilesetEntity(
- RandomIdGenerator.INSTANCE.nextId(), namespace, "sameName", auditInfo);
- store.put(filesetEntity1);
-
- // Rename topic will not affect another
- long topicId = topicEntity1.id();
- store.update(
- identifier,
- TopicEntity.class,
- Entity.EntityType.TOPIC,
- e -> createTopicEntity(topicId, namespace, "sameNameChanged", e.auditInfo()));
-
- Assertions.assertNotNull(store.get(identifier, Entity.EntityType.TABLE, TableEntity.class));
- Assertions.assertNotNull(
- store.get(identifier, Entity.EntityType.FILESET, FilesetEntity.class));
- Assertions.assertNotNull(
- store.get(changedNameIdentifier, Entity.EntityType.TOPIC, TopicEntity.class));
- Assertions.assertNotNull(store.get(identifier, Entity.EntityType.MODEL, ModelEntity.class));
+ validateMetalakeChanged(store, metalake);
+ validateCatalogChanged(store, catalog);
+ validateSchemaChanged(store, schema1);
+ validateTableChanged(store, table1);
+ validateFilesetChanged(store, fileset1);
+ validateTopicChanged(store, topic1);
+ validateDeletedTable(store);
+ validateDeletedFileset(store);
+ validateDeletedTopic(store);
+ validateAlreadyExistEntity(store, schema2);
+ validateNotChangedEntity(store, schema2);
destroy(type);
}
@@ -1172,7 +762,6 @@ public class TestEntityStorage {
throws IOException, IllegalAccessException {
Config config = Mockito.mock(Config.class);
Mockito.when(config.get(Configs.CACHE_ENABLED)).thenReturn(enableCache);
-
init(type, config);
try (EntityStore store = EntityStoreFactory.createEntityStore(config)) {
store.initialize(config);
@@ -1195,7 +784,6 @@ public class TestEntityStorage {
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);
@@ -1205,7 +793,6 @@ public class TestEntityStorage {
Entity.EntityType.METALAKE,
e -> createBaseMakeLake(metalake1New.id(), "metalake2", e.auditInfo()));
- // Rename metalake3 --> metalake1
BaseMetalake metalake3New1 =
createBaseMakeLake(RandomIdGenerator.INSTANCE.nextId(), "metalake3", auditInfo);
store.put(metalake3New1);
@@ -1215,7 +802,6 @@ public class TestEntityStorage {
Entity.EntityType.METALAKE,
e -> createBaseMakeLake(metalake3New1.id(), "metalake1", e.auditInfo()));
- // Rename metalake3 --> metalake2
BaseMetalake metalake3New2 =
createBaseMakeLake(RandomIdGenerator.INSTANCE.nextId(), "metalake3", auditInfo);
store.put(metalake3New2);
@@ -1227,7 +813,6 @@ public class TestEntityStorage {
Entity.EntityType.METALAKE,
e -> createBaseMakeLake(metalake3New2.id(), "metalake2", e.auditInfo()));
- // Finally, only metalake2 and metalake1 are left.
Assertions.assertDoesNotThrow(
() ->
store.get(
@@ -1241,7 +826,6 @@ public class TestEntityStorage {
NoSuchEntityException.class,
() -> store.get(id, Entity.EntityType.METALAKE, BaseMetalake.class));
- // Test catalog
CatalogEntity catalog1 =
createCatalog(
RandomIdGenerator.INSTANCE.nextId(),
@@ -1268,7 +852,6 @@ public class TestEntityStorage {
catalog1.name(),
(AuditInfo) catalog1.auditInfo());
store.put(catalog1New);
- // Should be OK;
store.update(
catalog1New.nameIdentifier(),
CatalogEntity.class,
@@ -1280,7 +863,6 @@ public class TestEntityStorage {
"catalog2",
(AuditInfo) e.auditInfo()));
- // Test schema
SchemaEntity schema1 =
createSchemaEntity(
RandomIdGenerator.INSTANCE.nextId(),
@@ -1318,7 +900,6 @@ public class TestEntityStorage {
"schema2",
e.auditInfo()));
- // Test table
TableEntity table1 =
createTableEntity(
RandomIdGenerator.INSTANCE.nextId(),
@@ -1358,7 +939,6 @@ public class TestEntityStorage {
"table2",
e.auditInfo()));
- // Test Fileset
FilesetEntity fileset1 =
createFilesetEntity(
RandomIdGenerator.INSTANCE.nextId(),
@@ -1400,7 +980,6 @@ public class TestEntityStorage {
"fileset2",
e.auditInfo()));
- // Test topic
TopicEntity topic1 =
createTopicEntity(
RandomIdGenerator.INSTANCE.nextId(),
@@ -1446,1880 +1025,164 @@ public class TestEntityStorage {
}
}
- 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 ColumnEntity createColumnEntity(
- Long id, String name, int position, Type dataType, AuditInfo auditInfo) {
- return ColumnEntity.builder()
- .withId(id)
- .withName(name)
- .withPosition(position)
- .withComment("")
- .withDataType(dataType)
- .withNullable(true)
- .withAutoIncrement(false)
- .withAuditInfo(auditInfo)
- .build();
- }
-
- public static TableEntity createTableEntity(
- Long id, Namespace namespace, String name, AuditInfo auditInfo) {
- return createTableEntityWithColumns(id, namespace, name, auditInfo, Collections.emptyList());
- }
-
- public static TableEntity createTableEntityWithColumns(
- Long id, Namespace namespace, String name, AuditInfo auditInfo, List<ColumnEntity> columns) {
- return TableEntity.builder()
- .withId(id)
- .withName(name)
- .withNamespace(namespace)
- .withAuditInfo(auditInfo)
- .withColumns(columns)
- .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)
- .withStorageLocations(ImmutableMap.of(LOCATION_NAME_UNKNOWN, "/tmp"))
- .withComment("")
- .withProperties(null)
- .withAuditInfo(auditInfo)
- .build();
- }
-
- public static TopicEntity createTopicEntity(
- Long id, Namespace namespace, String name, AuditInfo auditInfo) {
- return TopicEntity.builder()
- .withId(id)
- .withName(name)
- .withNamespace(namespace)
- .withComment("test comment")
- .withProperties(ImmutableMap.of("key", "value"))
- .withAuditInfo(auditInfo)
- .build();
- }
-
- private static UserEntity createUser(Long id, String metalake, String name, AuditInfo auditInfo) {
- return UserEntity.builder()
- .withId(id)
- .withNamespace(AuthorizationUtils.ofUserNamespace(metalake))
- .withName(name)
- .withAuditInfo(auditInfo)
- .withRoleNames(null)
- .build();
- }
-
- private static GroupEntity createGroup(
- Long id, String metalake, String name, AuditInfo auditInfo) {
- return GroupEntity.builder()
- .withId(id)
- .withNamespace(AuthorizationUtils.ofGroupNamespace(metalake))
- .withName(name)
- .withAuditInfo(auditInfo)
- .withRoleNames(null)
- .build();
- }
-
- private static RoleEntity createRole(Long id, String metalake, String name, AuditInfo auditInfo) {
- SecurableObject securableObject =
- SecurableObjects.ofCatalog("catalog", Lists.newArrayList(Privileges.UseCatalog.allow()));
-
- return RoleEntity.builder()
- .withId(id)
- .withNamespace(AuthorizationUtils.ofRoleNamespace(metalake))
- .withName(name)
- .withAuditInfo(auditInfo)
- .withSecurableObjects(Lists.newArrayList(securableObject))
- .withProperties(null)
- .build();
- }
-
- private void validateDeleteTopicCascade(EntityStore store, TopicEntity topic1)
- throws IOException {
- // Delete the topic 'metalake.catalog.schema1.topic1'
- Assertions.assertTrue(store.delete(topic1.nameIdentifier(), Entity.EntityType.TOPIC));
- Assertions.assertFalse(store.exists(topic1.nameIdentifier(), Entity.EntityType.TOPIC));
- // Delete again should return false
- Assertions.assertFalse(store.delete(topic1.nameIdentifier(), Entity.EntityType.TOPIC));
- }
-
- private void validateDeleteModelCascade(EntityStore store, ModelEntity model1)
- throws IOException {
- // Delete the topic 'metalake.catalog.schema1.topic1'
- Assertions.assertTrue(store.delete(model1.nameIdentifier(), EntityType.MODEL));
- Assertions.assertFalse(store.exists(model1.nameIdentifier(), EntityType.MODEL));
- // Delete again should return false
- Assertions.assertFalse(store.delete(model1.nameIdentifier(), EntityType.MODEL));
- }
-
- 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));
- // Delete again should return false
- Assertions.assertFalse(
- store.delete(fileset1.nameIdentifier(), Entity.EntityType.FILESET, true));
- }
-
- 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));
- // Delete again should return false
- Assertions.assertFalse(store.delete(table1.nameIdentifier(), Entity.EntityType.TABLE, true));
- validateDeletedColumns(table1.id(), table1.type());
- }
-
- 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));
- // Delete again should return false
- Assertions.assertFalse(
- store.delete(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 validateDeleteTopic(
- EntityStore store, SchemaEntity schema2, TopicEntity topic1, TopicEntity topic1InSchema2)
- throws IOException {
- // Delete the topic 'metalake.catalog.schema2.topic1'
- Assertions.assertTrue(store.delete(topic1InSchema2.nameIdentifier(), Entity.EntityType.TOPIC));
- Assertions.assertFalse(store.exists(topic1InSchema2.nameIdentifier(), Entity.EntityType.TOPIC));
- // Delete again should return false
- Assertions.assertFalse(store.delete(topic1InSchema2.nameIdentifier(), Entity.EntityType.TOPIC));
-
- // Make sure topic 'metalake.catalog.schema1.topic1' still exist;
- Assertions.assertEquals(
- topic1, store.get(topic1.nameIdentifier(), Entity.EntityType.TOPIC, TopicEntity.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,
- UserEntity userNew,
- GroupEntity groupNew,
- RoleEntity roleNew)
- throws IOException {
- Assertions.assertTrue(store.exists(userNew.nameIdentifier(), Entity.EntityType.USER));
- Assertions.assertTrue(store.exists(groupNew.nameIdentifier(), Entity.EntityType.GROUP));
- Assertions.assertTrue(store.exists(roleNew.nameIdentifier(), Entity.EntityType.ROLE));
-
- 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));
- Assertions.assertFalse(store.exists(userNew.nameIdentifier(), Entity.EntityType.USER));
- Assertions.assertFalse(store.exists(groupNew.nameIdentifier(), EntityType.GROUP));
- Assertions.assertFalse(store.exists(roleNew.nameIdentifier(), EntityType.ROLE));
- validateDeletedColumns(metalake.id(), metalake.type());
-
- // Delete again should return false
- Assertions.assertFalse(
- store.delete(metalake.nameIdentifier(), Entity.EntityType.METALAKE, true));
- }
-
- 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));
- validateDeletedColumns(catalog.id(), catalog.type());
-
- Assertions.assertThrowsExactly(
- NoSuchEntityException.class,
- () -> store.get(schema2.nameIdentifier(), Entity.EntityType.SCHEMA, SchemaEntity.class));
- // Delete again should return false
- Assertions.assertFalse(store.delete(catalog.nameIdentifier(), Entity.EntityType.CATALOG, true));
- }
-
- private void validateDeleteSchemaCascade(
- EntityStore store,
- SchemaEntity schema1,
- TableEntity table1,
- FilesetEntity fileset1,
- TopicEntity topic1,
- ModelEntity model1)
- throws IOException {
- TableEntity table1New =
- createTableEntityWithColumns(
- RandomIdGenerator.INSTANCE.nextId(),
- table1.namespace(),
- table1.name(),
- table1.auditInfo(),
- table1.columns());
- store.put(table1New);
- FilesetEntity fileset1New =
- createFilesetEntity(
- RandomIdGenerator.INSTANCE.nextId(),
- fileset1.namespace(),
- fileset1.name(),
- fileset1.auditInfo());
- store.put(fileset1New);
- TopicEntity topic1New =
- createTopicEntity(
- RandomIdGenerator.INSTANCE.nextId(),
- topic1.namespace(),
- topic1.name(),
- topic1.auditInfo());
- store.put(topic1New);
-
- ModelEntity model1New =
- createModelEntity(
- RandomIdGenerator.INSTANCE.nextId(),
- model1.namespace(),
- model1.name(),
- model1.comment(),
- model1.latestVersion(),
- model1.properties(),
- model1.auditInfo());
- store.put(model1New);
-
- 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"));
- }
-
- validateDeletedColumns(schema1.id(), schema1.type());
-
- // Delete again should return false
- Assertions.assertFalse(store.delete(schema1.nameIdentifier(), Entity.EntityType.SCHEMA, true));
-
- Assertions.assertThrows(
- NoSuchEntityException.class,
- () -> store.get(fileset1.nameIdentifier(), Entity.EntityType.FILESET, FilesetEntity.class));
-
- Assertions.assertThrows(
- NoSuchEntityException.class,
- () -> store.get(topic1.nameIdentifier(), Entity.EntityType.TOPIC, TopicEntity.class));
-
- Assertions.assertThrows(
- NoSuchEntityException.class,
- () -> store.get(model1.nameIdentifier(), Entity.EntityType.MODEL, ModelEntity.class));
- }
-
- private void validateDeleteMetalake(
- EntityStore store,
- BaseMetalake metalake,
- CatalogEntity catalogCopy,
- UserEntity user2,
- GroupEntity group2,
- RoleEntity role2)
- throws IOException {
- // Now delete catalog 'catalogCopy' and metalake
- Assertions.assertTrue(store.exists(user2.nameIdentifier(), Entity.EntityType.USER));
- Assertions.assertTrue(store.exists(group2.nameIdentifier(), Entity.EntityType.GROUP));
- Assertions.assertTrue(store.exists(role2.nameIdentifier(), Entity.EntityType.ROLE));
-
- 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));
- Assertions.assertFalse(store.exists(user2.nameIdentifier(), Entity.EntityType.USER));
- Assertions.assertFalse(store.exists(group2.nameIdentifier(), Entity.EntityType.GROUP));
- Assertions.assertFalse(store.exists(role2.nameIdentifier(), Entity.EntityType.ROLE));
- // Delete again should return false
- Assertions.assertFalse(store.delete(metalake.nameIdentifier(), Entity.EntityType.METALAKE));
- }
-
- private void validateDeleteCatalog(
- EntityStore store,
- CatalogEntity catalog,
- TableEntity table1,
- SchemaEntity schema1,
- TableEntity table1InSchema2,
- SchemaEntity schema2,
- FilesetEntity fileset1,
- FilesetEntity fileset1InSchema2,
- TopicEntity topic1,
- TopicEntity topic1InSchema2,
- ModelEntity model1,
- ModelEntity model1InSchema2)
- 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);
- validateDeletedColumns(table1.id(), table1.type());
- store.delete(fileset1.nameIdentifier(), Entity.EntityType.FILESET);
- store.delete(topic1.nameIdentifier(), Entity.EntityType.TOPIC);
- store.delete(model1.nameIdentifier(), Entity.EntityType.MODEL);
-
- 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);
- validateDeletedColumns(table1InSchema2.id(), table1InSchema2.type());
- Assertions.assertFalse(
- store.exists(fileset1InSchema2.nameIdentifier(), Entity.EntityType.FILESET));
- Assertions.assertFalse(store.exists(topic1InSchema2.nameIdentifier(), Entity.EntityType.TOPIC));
- Assertions.assertFalse(store.exists(model1InSchema2.nameIdentifier(), Entity.EntityType.MODEL));
- store.delete(schema2.nameIdentifier(), Entity.EntityType.SCHEMA);
-
- store.delete(catalog.nameIdentifier(), Entity.EntityType.CATALOG);
- Assertions.assertFalse(store.exists(catalog.nameIdentifier(), Entity.EntityType.CATALOG));
- // Delete again should return false
- Assertions.assertFalse(store.delete(catalog.nameIdentifier(), Entity.EntityType.CATALOG));
- }
-
- private void validateDeleteSchema(
- EntityStore store,
- SchemaEntity schema1,
- TableEntity table1,
- FilesetEntity fileset1,
- TopicEntity topic1,
- ModelEntity model1,
- ModelVersionEntity modelVersion1)
- 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', table 'metalake.catalog.schema1.table1',
- // table 'metalake.catalog.schema1.fileset1' and table 'metalake.catalog.schema1.topic1'
- // 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));
- Assertions.assertTrue(store.exists(topic1.nameIdentifier(), Entity.EntityType.TOPIC));
- Assertions.assertTrue(store.exists(model1.nameIdentifier(), Entity.EntityType.MODEL));
- Assertions.assertTrue(
- store.exists(modelVersion1.nameIdentifier(), Entity.EntityType.MODEL_VERSION));
-
- // Delete table1,fileset1 and schema1
- Assertions.assertTrue(store.delete(table1.nameIdentifier(), Entity.EntityType.TABLE));
- validateDeletedColumns(table1.id(), table1.type());
- Assertions.assertTrue(store.delete(fileset1.nameIdentifier(), Entity.EntityType.FILESET));
- Assertions.assertTrue(store.delete(topic1.nameIdentifier(), Entity.EntityType.TOPIC));
- Assertions.assertTrue(store.delete(model1.nameIdentifier(), Entity.EntityType.MODEL));
- 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(topic1.nameIdentifier(), Entity.EntityType.TOPIC));
- Assertions.assertFalse(store.exists(model1.nameIdentifier(), Entity.EntityType.MODEL));
- Assertions.assertFalse(
- store.exists(modelVersion1.nameIdentifier(), Entity.EntityType.MODEL_VERSION));
- Assertions.assertFalse(store.exists(schema1.nameIdentifier(), Entity.EntityType.SCHEMA));
-
- // Delete again should return false
- Assertions.assertFalse(store.delete(table1.nameIdentifier(), Entity.EntityType.TABLE));
- Assertions.assertFalse(store.delete(fileset1.nameIdentifier(), Entity.EntityType.FILESET));
- Assertions.assertFalse(store.delete(topic1.nameIdentifier(), Entity.EntityType.TOPIC));
- Assertions.assertFalse(store.delete(model1.nameIdentifier(), Entity.EntityType.MODEL));
- Assertions.assertFalse(store.delete(schema1.nameIdentifier(), Entity.EntityType.SCHEMA));
-
- // Now we re-insert schema1, table1, fileset1 and topic1, 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);
- TopicEntity topic1New =
- createTopicEntity(
- RandomIdGenerator.INSTANCE.nextId(),
- topic1.namespace(),
- topic1.name(),
- topic1.auditInfo());
- store.put(topic1New);
-
- ModelEntity model1New =
- createModelEntity(
- RandomIdGenerator.INSTANCE.nextId(),
- model1.namespace(),
- model1.name(),
- model1.comment(),
- model1.latestVersion(),
- model1.properties(),
- model1.auditInfo());
- store.put(model1New);
-
- 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));
- Assertions.assertEquals(
- topic1New, store.get(topic1.nameIdentifier(), Entity.EntityType.TOPIC, TopicEntity.class));
- Assertions.assertEquals(
- model1New, store.get(model1.nameIdentifier(), Entity.EntityType.MODEL, ModelEntity.class));
- }
-
- private void validateDeleteUser(EntityStore store, UserEntity user1) throws IOException {
- Assertions.assertTrue(store.exists(user1.nameIdentifier(), Entity.EntityType.USER));
- Assertions.assertTrue(store.delete(user1.nameIdentifier(), Entity.EntityType.USER));
- Assertions.assertFalse(store.exists(user1.nameIdentifier(), Entity.EntityType.USER));
- // delete again should return false
- Assertions.assertFalse(store.delete(user1.nameIdentifier(), Entity.EntityType.USER));
-
- UserEntity user =
- createUser(RandomIdGenerator.INSTANCE.nextId(), "metalake", "user1", user1.auditInfo());
- store.put(user);
- Assertions.assertTrue(store.exists(user.nameIdentifier(), Entity.EntityType.USER));
- }
-
- private void validateDeleteGroup(EntityStore store, GroupEntity group1) throws IOException {
- Assertions.assertTrue(store.delete(group1.nameIdentifier(), EntityType.GROUP));
- Assertions.assertFalse(store.exists(group1.nameIdentifier(), Entity.EntityType.GROUP));
- // delete again should return false
- Assertions.assertFalse(store.delete(group1.nameIdentifier(), Entity.EntityType.GROUP));
-
- GroupEntity group =
- createGroup(RandomIdGenerator.INSTANCE.nextId(), "metalake", "group1", group1.auditInfo());
- store.put(group);
- Assertions.assertTrue(store.exists(group.nameIdentifier(), EntityType.GROUP));
- }
-
- private void validateDeleteRole(EntityStore store, RoleEntity role1) throws IOException {
- Assertions.assertTrue(store.delete(role1.nameIdentifier(), EntityType.ROLE));
- Assertions.assertFalse(store.exists(role1.nameIdentifier(), Entity.EntityType.ROLE));
- // delete again should return false
- Assertions.assertFalse(store.delete(role1.nameIdentifier(), Entity.EntityType.ROLE));
-
- RoleEntity role =
- createRole(RandomIdGenerator.INSTANCE.nextId(), "metalake", "role1", role1.auditInfo());
- store.put(role);
- Assertions.assertTrue(store.exists(role.nameIdentifier(), EntityType.ROLE));
- }
-
- 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));
- // delete again should return false
- Assertions.assertFalse(store.delete(table1InSchema2.nameIdentifier(), Entity.EntityType.TABLE));
-
- // Make sure all columns are deleted
- validateDeletedColumns(table1InSchema2.id(), table1InSchema2.type());
-
- // 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 =
- createTableEntityWithColumns(
- RandomIdGenerator.INSTANCE.nextId(),
- table1InSchema2.namespace(),
- table1InSchema2.name(),
- table1InSchema2.auditInfo(),
- table1InSchema2.columns());
- store.put(table1InSchema2New);
- Assertions.assertTrue(store.exists(table1InSchema2.nameIdentifier(), Entity.EntityType.TABLE));
- }
-
- private void validateDeleteModel(
- EntityStore store,
- SchemaEntity schema2,
- ModelEntity model1,
- ModelVersionEntity modelVersion1,
- ModelEntity model1InSchema2,
- ModelVersionEntity modelVersion1InSchema2)
- throws IOException {
- Assertions.assertTrue(store.delete(model1InSchema2.nameIdentifier(), Entity.EntityType.MODEL));
- Assertions.assertFalse(store.exists(model1InSchema2.nameIdentifier(), Entity.EntityType.MODEL));
- // delete again should return false
- Assertions.assertFalse(store.delete(model1InSchema2.nameIdentifier(), Entity.EntityType.MODEL));
-
- Assertions.assertFalse(
- store.exists(modelVersion1InSchema2.nameIdentifier(), EntityType.MODEL_VERSION));
- Assertions.assertFalse(
- store.delete(modelVersion1InSchema2.nameIdentifier(), EntityType.MODEL_VERSION));
-
- ModelEntity model1Copy =
- ModelEntity.builder()
- .withId(model1.id())
- .withNamespace(model1.namespace())
- .withName(model1.name())
- .withComment(model1.comment())
- .withLatestVersion(model1.latestVersion() + 1)
- .withProperties(model1.properties())
- .withAuditInfo(model1.auditInfo())
- .build();
-
- Assertions.assertEquals(
- model1Copy, store.get(model1.nameIdentifier(), Entity.EntityType.MODEL, ModelEntity.class));
-
- Assertions.assertEquals(
- modelVersion1,
- store.get(
- modelVersion1.nameIdentifier(), EntityType.MODEL_VERSION, ModelVersionEntity.class));
-
- // Make sure schema 'metalake.catalog.schema2' still exist;
- Assertions.assertEquals(
- schema2, store.get(schema2.nameIdentifier(), Entity.EntityType.SCHEMA, SchemaEntity.class));
- }
-
- private static void validateAllEntityExist(
- BaseMetalake metalake,
- EntityStore store,
- CatalogEntity catalog,
- CatalogEntity catalogCopy,
- SchemaEntity schema1,
- SchemaEntity schema2,
- TableEntity table1,
- TableEntity table1InSchema2,
- FilesetEntity fileset1,
- FilesetEntity fileset1InSchema2,
- TopicEntity topic1,
- TopicEntity topic1InSchema2,
- ModelEntity model1,
- ModelVersionEntity modelVersion1,
- ModelEntity model1InSchema2,
- ModelVersionEntity modelVersion1InSchema2,
- UserEntity user1,
- UserEntity user2,
- GroupEntity group1,
- GroupEntity group2,
- RoleEntity role1,
- RoleEntity role2)
- 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));
- Assertions.assertEquals(
- topic1, store.get(topic1.nameIdentifier(), Entity.EntityType.TOPIC, TopicEntity.class));
- Assertions.assertEquals(
- topic1InSchema2,
- store.get(topic1InSchema2.nameIdentifier(), Entity.EntityType.TOPIC, TopicEntity.class));
-
- ModelEntity model1Copy =
- ModelEntity.builder()
- .withId(model1.id())
- .withNamespace(model1.namespace())
- .withName(model1.name())
- .withComment(model1.comment())
- .withLatestVersion(model1.latestVersion() + 1)
- .withProperties(model1.properties())
- .withAuditInfo(model1.auditInfo())
- .build();
-
- Assertions.assertEquals(
- model1Copy, store.get(model1.nameIdentifier(), Entity.EntityType.MODEL, ModelEntity.class));
- Assertions.assertEquals(
- modelVersion1,
- store.get(
- modelVersion1.nameIdentifier(), EntityType.MODEL_VERSION, ModelVersionEntity.class));
-
- ModelEntity model1InSchema2Copy =
- ModelEntity.builder()
- .withId(model1InSchema2.id())
- .withNamespace(model1InSchema2.namespace())
- .withName(model1InSchema2.name())
- .withComment(model1InSchema2.comment())
- .withLatestVersion(model1InSchema2.latestVersion() + 1)
- .withProperties(model1InSchema2.properties())
- .withAuditInfo(model1InSchema2.auditInfo())
- .build();
-
- Assertions.assertEquals(
- model1InSchema2Copy,
- store.get(model1InSchema2.nameIdentifier(), EntityType.MODEL, ModelEntity.class));
- Assertions.assertEquals(
- modelVersion1InSchema2,
- store.get(
- modelVersion1InSchema2.nameIdentifier(),
- EntityType.MODEL_VERSION,
- ModelVersionEntity.class));
-
- Assertions.assertEquals(
- user1, store.get(user1.nameIdentifier(), Entity.EntityType.USER, UserEntity.class));
- Assertions.assertEquals(
- user2, store.get(user2.nameIdentifier(), Entity.EntityType.USER, UserEntity.class));
- Assertions.assertEquals(
- group1, store.get(group1.nameIdentifier(), Entity.EntityType.GROUP, GroupEntity.class));
- Assertions.assertEquals(
- group2, store.get(group2.nameIdentifier(), Entity.EntityType.GROUP, GroupEntity.class));
- Assertions.assertEquals(
- role1, store.get(role1.nameIdentifier(), Entity.EntityType.ROLE, RoleEntity.class));
- Assertions.assertEquals(
- role2, store.get(role2.nameIdentifier(), Entity.EntityType.ROLE, RoleEntity.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 validateDeletedTopic(EntityStore store) throws IOException {
- store.delete(
- NameIdentifier.of("metalakeChanged", "catalogChanged", "schema2", "topic1"),
- Entity.EntityType.TOPIC);
- // Update a deleted entities
- Assertions.assertThrowsExactly(
- NoSuchEntityException.class,
- () ->
- store.update(
- NameIdentifier.of("metalakeChanged", "catalogChanged", "schema2", "topic1"),
- TopicEntity.class,
- Entity.EntityType.TOPIC,
- (e) -> e));
- }
-
- private void validateTopicChanged(EntityStore store, TopicEntity topicEntity) throws IOException {
- // Check topic entities
- store.update(
- NameIdentifier.of("metalakeChanged", "catalogChanged", "schemaChanged", "topic1"),
- TopicEntity.class,
- Entity.EntityType.TOPIC,
- e -> {
- AuditInfo auditInfo1 =
- AuditInfo.builder().withCreator("creator6").withCreateTime(Instant.now()).build();
- return createTopicEntity(
- topicEntity.id(),
- Namespace.of("metalakeChanged", "catalogChanged", "schemaChanged"),
- "topicChanged",
- auditInfo1);
- });
-
- Assertions.assertThrowsExactly(
- NoSuchEntityException.class,
- () ->
- store.get(
- NameIdentifier.of("metalakeChanged", "catalogChanged", "schema1", "topic1"),
- Entity.EntityType.TOPIC,
- TopicEntity.class));
- TopicEntity updatedTopic =
- store.get(
- NameIdentifier.of("metalakeChanged", "catalogChanged", "schemaChanged", "topicChanged"),
- Entity.EntityType.TOPIC,
- TopicEntity.class);
- Assertions.assertEquals("creator6", updatedTopic.auditInfo().creator());
-
- Assertions.assertNotNull(
- store.get(
- NameIdentifier.of("metalakeChanged", "catalogChanged", "schema2", "topic1"),
- Entity.EntityType.TOPIC,
- TopicEntity.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(
- EntityAlreadyExistsException.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));
- }
-
- private List<Pair<Long, Pair<Long, Long>>> listAllColumnWithEntityId(
- Long entityId, Entity.EntityType entityType) {
- String queryTemp =
- "SELECT column_id, table_version, deleted_at FROM "
- + "table_column_version_info WHERE %s = %d";
- String query;
- switch (entityType) {
- case TABLE:
- query = String.format(queryTemp, "table_id", entityId);
- break;
- case SCHEMA:
- query = String.format(queryTemp, "schema_id", entityId);
- break;
- case CATALOG:
- query = String.format(queryTemp, "catalog_id", entityId);
- break;
- case METALAKE:
- query = String.format(queryTemp, "metalake_id", entityId);
- break;
- default:
- throw new IllegalArgumentException("Unsupported entity type: " + entityType);
- }
-
- List<Pair<Long, Pair<Long, Long>>> results = Lists.newArrayList();
- try (SqlSession sqlSession =
- SqlSessionFactoryHelper.getInstance().getSqlSessionFactory().openSession(true)) {
- Connection connection = sqlSession.getConnection();
- Statement statement = connection.createStatement();
-
- ResultSet rs = statement.executeQuery(query);
- while (rs.next()) {
- results.add(
- Pair.of(
- rs.getLong("column_id"),
- Pair.of(rs.getLong("table_version"), rs.getLong("deleted_at"))));
- }
- } catch (SQLException e) {
- throw new RuntimeException(e);
- }
-
- return results;
- }
-
- private void validateDeletedColumns(Long entityId, Entity.EntityType entityType) {
- List<Pair<Long, Pair<Long, Long>>> deleteResult =
- listAllColumnWithEntityId(entityId, entityType);
- deleteResult.forEach(p -> Assertions.assertTrue(p.getRight().getRight() > 0));
- }
-
@ParameterizedTest
@MethodSource("storageProvider")
- void testInvalidRelationCache(String type, boolean enableCache) throws Exception {
- // This class will test entity cache specifically for relational stores
- if (!enableCache) {
- return;
- }
-
+ void testSameNameUnderANameSpace(String type, boolean enableCache)
+ throws IOException, IllegalAccessException {
Config config = Mockito.mock(Config.class);
Mockito.when(config.get(Configs.CACHE_ENABLED)).thenReturn(enableCache);
-
init(type, config);
-
- AuditInfo auditInfo =
- AuditInfo.builder().withCreator("creator").withCreateTime(Instant.now()).build();
-
try (EntityStore store = EntityStoreFactory.createEntityStore(config)) {
store.initialize(config);
- BaseMetalake metalake =
- createBaseMakeLake(RandomIdGenerator.INSTANCE.nextId(), "metalake", auditInfo);
- store.put(metalake, false);
+ AuditInfo auditInfo =
+ AuditInfo.builder().withCreator("creator").withCreateTime(Instant.now()).build();
- CatalogEntity catalog =
+ BaseMetalake metalake1 =
+ createBaseMakeLake(RandomIdGenerator.INSTANCE.nextId(), "metalake1", auditInfo);
+ CatalogEntity catalog1 =
createCatalog(
RandomIdGenerator.INSTANCE.nextId(),
- NamespaceUtil.ofCatalog("metalake"),
- "catalog",
+ Namespace.of("metalake1"),
+ "catalog1",
auditInfo);
- store.put(catalog, false);
-
- // Insert a role
- RoleEntity role =
- createRoleEntity(
- RandomIdGenerator.INSTANCE.nextId(),
- AuthorizationUtils.ofRoleNamespace("metalake"),
- "role",
- auditInfo,
- "catalog");
- store.put(role, false);
-
- // Get a role
- Role oldRole = store.get(role.nameIdentifier(), Entity.EntityType.ROLE, RoleEntity.class);
-
- // Rename the catalog that the role is associated with
- CatalogEntity updatedCatalog =
- CatalogEntity.builder()
- .withId(catalog.id())
- .withNamespace(catalog.namespace())
- .withName("newCatalogName")
- .withAuditInfo(auditInfo)
- .withComment(catalog.getComment())
- .withProperties(catalog.getProperties())
- .withType(catalog.getType())
- .withProvider(catalog.getProvider())
- .build();
- store.update(
- catalog.nameIdentifier(),
- CatalogEntity.class,
- Entity.EntityType.CATALOG,
- e -> updatedCatalog);
-
- // Now try to get the role again, it should reflect the updated catalog name
- Role newRow = store.get(role.nameIdentifier(), Entity.EntityType.ROLE, RoleEntity.class);
- Assertions.assertNotEquals(oldRole, newRow);
- Assertions.assertNotEquals(oldRole.securableObjects(), newRow.securableObjects());
- List<SecurableObject> securableObjects = newRow.securableObjects();
- Assertions.assertEquals(1, securableObjects.size());
- Assertions.assertEquals("newCatalogName", securableObjects.get(0).name());
-
- // Now try to create a schema and a fileset under the updated catalog
- SchemaEntity schema =
+ SchemaEntity schema1 =
createSchemaEntity(
RandomIdGenerator.INSTANCE.nextId(),
- Namespace.of("metalake", "newCatalogName"),
- "schema",
+ Namespace.of("metalake1", "catalog1"),
+ "schema1",
auditInfo);
- store.put(schema, false);
- FilesetEntity fileset =
+ Namespace namespace = Namespace.of("metalake1", "catalog1", "schema1");
+ TableEntity table1 =
+ createTableEntity(RandomIdGenerator.INSTANCE.nextId(), namespace, "sameName", auditInfo);
+
+ FilesetEntity filesetEntity1 =
createFilesetEntity(
+ RandomIdGenerator.INSTANCE.nextId(), namespace, "sameName", auditInfo);
+
+ TopicEntity topicEntity1 =
+ createTopicEntity(RandomIdGenerator.INSTANCE.nextId(), namespace, "sameName", auditInfo);
+
+ ModelEntity model1 =
+ createModelEntity(
RandomIdGenerator.INSTANCE.nextId(),
- Namespace.of("metalake", "newCatalogName", "schema"),
- "fileset",
- auditInfo);
- store.put(fileset, false);
-
- // Now try to create two roles: one can read_fileset, another can write_fileset
- SecurableObject catalogObject =
- SecurableObjects.ofCatalog("newCatalogName", Lists.newArrayList());
- SecurableObject schemaObject =
- SecurableObjects.ofSchema(catalogObject, "schema", Lists.newArrayList());
- SecurableObject securableFileset01 =
- SecurableObjects.ofFileset(
- schemaObject, "fileset", Lists.newArrayList(Privileges.ReadFileset.allow()));
- SecurableObject securableFileset02 =
- SecurableObjects.ofFileset(
- schemaObject, "fileset", Lists.newArrayList(Privileges.WriteFileset.allow()));
-
- RoleEntity readRole =
- RoleEntity.builder()
- .withId(RandomIdGenerator.INSTANCE.nextId())
- .withName("roleReadFileset")
- .withNamespace(AuthorizationUtils.ofRoleNamespace("metalake"))
- .withProperties(null)
- .withAuditInfo(auditInfo)
- .withSecurableObjects(Lists.newArrayList(securableFileset01))
- .build();
- store.put(readRole, false);
-
- RoleEntity writeRole =
- RoleEntity.builder()
- .withId(RandomIdGenerator.INSTANCE.nextId())
- .withName("roleWriteFileset")
- .withNamespace(AuthorizationUtils.ofRoleNamespace("metalake"))
- .withProperties(null)
- .withAuditInfo(auditInfo)
- .withSecurableObjects(Lists.newArrayList(securableFileset02))
- .build();
- store.put(writeRole, false);
-
- // Load the two roles and verify their securable objects
- Role loadedReadRole =
- store.get(readRole.nameIdentifier(), Entity.EntityType.ROLE, RoleEntity.class);
- Role loadedWriteRole =
- store.get(writeRole.nameIdentifier(), Entity.EntityType.ROLE, RoleEntity.class);
- Assertions.assertEquals(1, loadedReadRole.securableObjects().size());
- Assertions.assertEquals(
- "newCatalogName.schema.fileset", loadedReadRole.securableObjects().get(0).fullName());
- Assertions.assertEquals(1, loadedWriteRole.securableObjects().size());
- Assertions.assertEquals(
- "newCatalogName.schema.fileset", loadedReadRole.securableObjects().get(0).fullName());
- Assertions.assertEquals(
- Condition.ALLOW,
- loadedReadRole.securableObjects().get(0).privileges().get(0).condition());
- Assertions.assertEquals(
- Condition.ALLOW,
- loadedWriteRole.securableObjects().get(0).privileges().get(0).condition());
-
- // Now try to drop and then recreate the role
- store.delete(readRole.nameIdentifier(), Entity.EntityType.ROLE);
-
- ReverseIndexCache reverseIndexCache =
- ((CaffeineEntityCache) ((RelationalEntityStore) store).getCache()).getReverseIndex();
- List<EntityCacheKey> reverseIndexValue =
- reverseIndexCache.get(
- NameIdentifier.of("metalake", "newCatalogName", "schema", "fileset"),
- Entity.EntityType.FILESET);
- // As read role is deleted, the reverse index cache should not have it anymore.
- Assertions.assertEquals(1, reverseIndexValue.size());
- Assertions.assertEquals(writeRole.nameIdentifier(), reverseIndexValue.get(0).identifier());
-
- store.put(readRole, true);
- store.get(readRole.nameIdentifier(), Entity.EntityType.ROLE, RoleEntity.class);
- reverseIndexValue =
- reverseIndexCache.get(
- NameIdentifier.of("metalake", "newCatalogName", "schema", "fileset"),
- Entity.EntityType.FILESET);
- // As read role is recreated, the reverse index cache should have it again.
- Assertions.assertEquals(2, reverseIndexValue.size());
- List<NameIdentifier> ids =
- reverseIndexValue.stream().map(EntityCacheKey::identifier).collect(Collectors.toList());
- Assertions.assertTrue(ids.contains(readRole.nameIdentifier()));
- Assertions.assertTrue(ids.contains(writeRole.nameIdentifier()));
-
- // Drop role1 and role2
- store.delete(readRole.nameIdentifier(), Entity.EntityType.ROLE);
- store.delete(writeRole.nameIdentifier(), Entity.EntityType.ROLE);
-
- reverseIndexValue =
- reverseIndexCache.get(
- NameIdentifier.of("metalake", "newCatalogName", "schema", "fileset"),
- Entity.EntityType.FILESET);
- // As both roles are deleted, the reverse index cache should not have them anymore.
- Assertions.assertNull(reverseIndexValue);
-
- store.put(readRole, true);
- store.put(writeRole, true);
- store.get(readRole.nameIdentifier(), Entity.EntityType.ROLE, RoleEntity.class);
- store.get(writeRole.nameIdentifier(), Entity.EntityType.ROLE, RoleEntity.class);
-
- // first try to rename the fileset to fileset_new
- store.update(
- fileset.nameIdentifier(),
- FilesetEntity.class,
- Entity.EntityType.FILESET,
- e ->
- createFilesetEntity(
- fileset.id(), fileset.namespace(), "fileset_new", fileset.auditInfo()));
-
- // try to load the two roles again, the securable objects should reflect the updated fileset
- // name
- loadedReadRole =
- store.get(readRole.nameIdentifier(), Entity.EntityType.ROLE, RoleEntity.class);
- loadedWriteRole =
- store.get(writeRole.nameIdentifier(), Entity.EntityType.ROLE, RoleEntity.class);
-
- Assertions.assertEquals(1, loadedReadRole.securableObjects().size());
- Assertions.assertEquals(
- "newCatalogName.schema.fileset_new", loadedReadRole.securableObjects().get(0).fullName());
- Assertions.assertEquals(1, loadedWriteRole.securableObjects().size());
- Assertions.assertEquals(
- "newCatalogName.schema.fileset_new", loadedReadRole.securableObjects().get(0).fullName());
- Assertions.assertEquals(
- Condition.ALLOW,
- loadedReadRole.securableObjects().get(0).privileges().get(0).condition());
- Assertions.assertEquals(
- Condition.ALLOW,
- loadedWriteRole.securableObjects().get(0).privileges().get(0).condition());
-
- // Now try to rename schema to schema_new
- store.update(
- NameIdentifier.of("metalake", "newCatalogName", "schema"),
- SchemaEntity.class,
- Entity.EntityType.SCHEMA,
- e ->
- createSchemaEntity(
- schema.id(),
- Namespace.of("metalake", "newCatalogName"),
- "schema_new",
- schema.auditInfo()));
- // try to load the two roles again, the securable objects should reflect the updated schema
- loadedReadRole =
- store.get(readRole.nameIdentifier(), Entity.EntityType.ROLE, RoleEntity.class);
- loadedWriteRole =
- store.get(writeRole.nameIdentifier(), Entity.EntityType.ROLE, RoleEntity.class);
- Assertions.assertEquals(1, loadedReadRole.securableObjects().size());
- Assertions.assertEquals(
- "newCatalogName.schema_new.fileset_new",
- loadedReadRole.securableObjects().get(0).fullName());
- Assertions.assertEquals(1, loadedWriteRole.securableObjects().size());
- Assertions.assertEquals(
- "newCatalogName.schema_new.fileset_new",
- loadedReadRole.securableObjects().get(0).fullName());
-
- // now create a user1 and assign the readRole to the user
- UserEntity user1 =
- createUserEntity(
- RandomIdGenerator.INSTANCE.nextId(),
- AuthorizationUtils.ofUserNamespace("metalake"),
- "user1",
+ namespace,
+ "sameName",
+ "model1",
+ 1,
+ null,
auditInfo);
- store.put(user1, false);
-
- // Now try to drop the fileset
- store.delete(
- NameIdentifier.of("metalake", "newCatalogName", "schema_new", "fileset_new"),
- Entity.EntityType.FILESET);
- Assertions.assertFalse(store.exists(fileset.nameIdentifier(), Entity.EntityType.FILESET));
- // Now try to load the two roles again, the securable objects should be empty
- loadedReadRole =
- store.get(readRole.nameIdentifier(), Entity.EntityType.ROLE, RoleEntity.class);
- loadedWriteRole =
- store.get(writeRole.nameIdentifier(), Entity.EntityType.ROLE, RoleEntity.class);
+ store.put(metalake1);
+ store.put(catalog1);
+ store.put(schema1);
+ store.put(table1);
+ store.put(filesetEntity1);
+ store.put(topicEntity1);
+ store.put(model1);
- Assertions.assertEquals(0, loadedReadRole.securableObjects().size());
- Assertions.assertEquals(0, loadedWriteRole.securableObjects().size());
+ NameIdentifier identifier = NameIdentifier.of("metalake1", "catalog1", "schema1", "sameName");
- destroy(type);
- }
- }
+ TableEntity loadedTableEntity =
+ store.get(identifier, Entity.EntityType.TABLE, TableEntity.class);
+ Assertions.assertEquals(table1.id(), loadedTableEntity.id());
+ FilesetEntity loadedFilesetEntity =
+ store.get(identifier, Entity.EntityType.FILESET, FilesetEntity.class);
+ Assertions.assertEquals(filesetEntity1.id(), loadedFilesetEntity.id());
+ TopicEntity loadedTopicEntity =
+ store.get(identifier, Entity.EntityType.TOPIC, TopicEntity.class);
+ Assertions.assertEquals(topicEntity1.id(), loadedTopicEntity.id());
+ ModelEntity loadedModelEntity =
+ store.get(identifier, Entity.EntityType.MODEL, ModelEntity.class);
+ Assertions.assertEquals(model1.id(), loadedModelEntity.id());
- @ParameterizedTest
- @MethodSource("storageProvider")
- void testTagRelationCache(String type, boolean enableCache) throws Exception {
- if (!enableCache) {
- return;
- }
+ Assertions.assertTrue(store.delete(identifier, Entity.EntityType.TABLE));
+ Assertions.assertNotNull(
+ store.get(identifier, Entity.EntityType.FILESET, FilesetEntity.class));
+ Assertions.assertNotNull(store.get(identifier, Entity.EntityType.TOPIC, TopicEntity.class));
+ Assertions.assertNotNull(store.get(identifier, Entity.EntityType.MODEL, ModelEntity.class));
- Config config = Mockito.mock(Config.class);
- Mockito.when(config.get(Configs.CACHE_ENABLED)).thenReturn(enableCache);
+ table1 =
+ createTableEntity(RandomIdGenerator.INSTANCE.nextId(), namespace, "sameName", auditInfo);
+ store.put(table1);
- init(type, config);
+ store.delete(identifier, Entity.EntityType.FILESET);
+ Assertions.assertNotNull(store.get(identifier, Entity.EntityType.TABLE, TableEntity.class));
+ Assertions.assertNotNull(store.get(identifier, Entity.EntityType.TOPIC, TopicEntity.class));
+ Assertions.assertNotNull(store.get(identifier, Entity.EntityType.MODEL, ModelEntity.class));
- AuditInfo auditInfo =
- AuditInfo.builder().withCreator("creator").withCreateTime(Instant.now()).build();
+ filesetEntity1 =
+ createFilesetEntity(
+ RandomIdGenerator.INSTANCE.nextId(), namespace, "sameName", auditInfo);
+ store.put(filesetEntity1);
- try (EntityStore store = EntityStoreFactory.createEntityStore(config)) {
- store.initialize(config);
+ store.delete(identifier, Entity.EntityType.TOPIC);
+ Assertions.assertNotNull(store.get(identifier, Entity.EntityType.TABLE, TableEntity.class));
+ Assertions.assertNotNull(
+ store.get(identifier, Entity.EntityType.FILESET, FilesetEntity.class));
+ Assertions.assertNotNull(store.get(identifier, Entity.EntityType.MODEL, ModelEntity.class));
- BaseMetalake metalake =
- createBaseMakeLake(RandomIdGenerator.INSTANCE.nextId(), "metalake", auditInfo);
- store.put(metalake, false);
-
- Namespace namespace = NameIdentifierUtil.ofTag("metalake", "tag1").namespace();
- TagEntity tag1 =
- TagEntity.builder()
- .withId(RandomIdGenerator.INSTANCE.nextId())
- .withNamespace(namespace)
- .withName("tag1")
- .withAuditInfo(auditInfo)
- .withProperties(Collections.emptyMap())
- .build();
- CatalogEntity catalog =
- createCatalog(
- RandomIdGenerator.INSTANCE.nextId(),
- NamespaceUtil.ofCatalog("metalake"),
- "catalog",
- auditInfo);
+ topicEntity1 =
+ createTopicEntity(RandomIdGenerator.INSTANCE.nextId(), namespace, "sameName", auditInfo);
+ store.put(topicEntity1);
- store.put(catalog, false);
- store.put(tag1, false);
-
- SupportsRelationOperations relationOperations = (SupportsRelationOperations) store;
-
- relationOperations.updateEntityRelations(
- SupportsRelationOperations.Type.TAG_METADATA_OBJECT_REL,
- catalog.nameIdentifier(),
- EntityType.CATALOG,
- new NameIdentifier[] {tag1.nameIdentifier()},
- new NameIdentifier[] {});
-
- // Now try to load the relation
- List<TagEntity> tags =
- relationOperations.listEntitiesByRelation(
- SupportsRelationOperations.Type.TAG_METADATA_OBJECT_REL,
- catalog.nameIdentifier(),
- EntityType.CATALOG,
- true);
- Assertions.assertEquals(1, tags.size());
- Assertions.assertEquals(tag1, tags.get(0));
-
- // Check whether tags exists in entity store cache
- RelationalEntityStore relationalEntityStore = (RelationalEntityStore) store;
- CaffeineEntityCache caffeineEntityCache =
- (CaffeineEntityCache) relationalEntityStore.getCache();
- Cache<EntityCacheRelationKey, List<Entity>> cache = caffeineEntityCache.getCacheData();
-
- List<Entity> cachedTags =
- cache.get(
- EntityCacheRelationKey.of(
- catalog.nameIdentifier(),
- EntityType.CATALOG,
- SupportsRelationOperations.Type.TAG_METADATA_OBJECT_REL),
- k -> null);
-
- // Check cached tags is correct
- Assertions.assertNotNull(cachedTags);
- Assertions.assertEquals(1, cachedTags.size());
- Assertions.assertEquals(tag1, cachedTags.get(0));
-
- List<GenericEntity> genericEntities =
- relationOperations.listEntitiesByRelation(
- SupportsRelationOperations.Type.TAG_METADATA_OBJECT_REL,
- tag1.nameIdentifier(),
- EntityType.TAG,
- true);
- Assertions.assertEquals(1, genericEntities.size());
- Assertions.assertEquals(catalog.id(), genericEntities.get(0).id());
- Assertions.assertEquals(catalog.name(), genericEntities.get(0).name());
-
- // Now we are going to alter the catalog
- CatalogEntity updatedCatalog =
- CatalogEntity.builder()
- .withId(catalog.id())
- .withNamespace(catalog.namespace())
- .withName("newCatalogName")
- .withAuditInfo(auditInfo)
- .withComment(catalog.getComment())
- .withProperties(catalog.getProperties())
- .withType(catalog.getType())
- .withProvider(catalog.getProvider())
- .build();
+ long table1Id = table1.id();
store.update(
- catalog.nameIdentifier(),
- CatalogEntity.class,
- Entity.EntityType.CATALOG,
- e -> updatedCatalog);
- // Now try to load the relation again from cache, it should be empty.
- List<Entity> cachedTagsAfterCatalogUpdate =
- cache.get(
- EntityCacheRelationKey.of(
- catalog.nameIdentifier(),
- EntityType.CATALOG,
- SupportsRelationOperations.Type.TAG_METADATA_OBJECT_REL),
- k -> null);
- Assertions.assertNull(cachedTagsAfterCatalogUpdate);
-
- List<Entity> cachedTagsByTagAfterCatalogUpdate =
- cache.get(
- EntityCacheRelationKey.of(
- tag1.nameIdentifier(),
- EntityType.TAG,
- SupportsRelationOperations.Type.TAG_METADATA_OBJECT_REL),
- k -> null);
- Assertions.assertNull(cachedTagsByTagAfterCatalogUpdate);
-
- // Load tags again, it should repopulate the cache
- List<TagEntity> tagsAfterCatalogUpdate =
- relationOperations.listEntitiesByRelation(
- SupportsRelationOperations.Type.TAG_METADATA_OBJECT_REL,
- updatedCatalog.nameIdentifier(),
- EntityType.CATALOG,
- true);
- Assertions.assertEquals(1, tagsAfterCatalogUpdate.size());
- Assertions.assertEquals(tag1, tagsAfterCatalogUpdate.get(0));
-
- List<Entity> cachedTagsAfterReload =
- cache.get(
- EntityCacheRelationKey.of(
- updatedCatalog.nameIdentifier(),
- EntityType.CATALOG,
- SupportsRelationOperations.Type.TAG_METADATA_OBJECT_REL),
- k -> null);
- Assertions.assertNotNull(cachedTagsAfterReload);
- Assertions.assertEquals(1, cachedTagsAfterReload.size());
- Assertions.assertEquals(tag1, cachedTagsAfterReload.get(0));
-
- List<GenericEntity> genericEntitiesAfterCatalogUpdate =
- relationOperations.listEntitiesByRelation(
- SupportsRelationOperations.Type.TAG_METADATA_OBJECT_REL,
- tag1.nameIdentifier(),
- EntityType.TAG,
- true);
- Assertions.assertEquals(1, genericEntitiesAfterCatalogUpdate.size());
- Assertions.assertEquals(catalog.id(), genericEntitiesAfterCatalogUpdate.get(0).id());
- Assertions.assertEquals(
- updatedCatalog.name(), genericEntitiesAfterCatalogUpdate.get(0).name());
-
- List<Entity> cachedTagsByTagAfterReload =
- cache.get(
- EntityCacheRelationKey.of(
- tag1.nameIdentifier(),
- EntityType.TAG,
- SupportsRelationOperations.Type.TAG_METADATA_OBJECT_REL),
- k -> null);
- Assertions.assertNotNull(cachedTagsByTagAfterReload);
- Assertions.assertEquals(1, cachedTagsByTagAfterReload.size());
- Assertions.assertEquals(
- updatedCatalog.id(), ((GenericEntity) cachedTagsByTagAfterReload.get(0)).id());
- Assertions.assertEquals(
- updatedCatalog.name(), ((GenericEntity) cachedTagsByTagAfterReload.get(0)).name());
-
- // Now try to alter the tag: rename tag1 -> tagChanged.
- TagEntity updatedTag1 =
- TagEntity.builder()
- .withId(tag1.id())
- .withNamespace(tag1.namespace())
- .withName("tagChanged")
- .withAuditInfo(auditInfo)
- .withProperties(tag1.properties())
- .build();
- store.update(tag1.nameIdentifier(), TagEntity.class, Entity.EntityType.TAG, e -> updatedTag1);
-
- // Now try to load the relation again from cache, it should be empty.
- List<Entity> cachedTagsAfterTagUpdate =
- cache.get(
- EntityCacheRelationKey.of(
- updatedCatalog.nameIdentifier(),
- EntityType.CATALOG,
- SupportsRelationOperations.Type.TAG_METADATA_OBJECT_REL),
- k -> null);
- Assertions.assertNull(cachedTagsAfterTagUpdate);
-
- List<Entity> cachedEntitiesAfterTagUpdate =
- cache.get(
- EntityCacheRelationKey.of(
- tag1.nameIdentifier(),
- EntityType.TAG,
- SupportsRelationOperations.Type.TAG_METADATA_OBJECT_REL),
- k -> null);
- Assertions.assertNull(cachedEntitiesAfterTagUpdate);
-
- List<TagEntity> tagsAfterTagUpdate =
- relationOperations.listEntitiesByRelation(
- SupportsRelationOperations.Type.TAG_METADATA_OBJECT_REL,
- updatedCatalog.nameIdentifier(),
- EntityType.CATALOG,
- true);
- Assertions.assertEquals(1, tagsAfterTagUpdate.size());
- Assertions.assertEquals(updatedTag1, tagsAfterTagUpdate.get(0));
-
- List<Entity> cachedTagsAfterTagReload =
- cache.get(
- EntityCacheRelationKey.of(
- updatedCatalog.nameIdentifier(),
- EntityType.CATALOG,
- SupportsRelationOperations.Type.TAG_METADATA_OBJECT_REL),
- k -> null);
- Assertions.assertNotNull(cachedTagsAfterTagReload);
- Assertions.assertEquals(1, cachedTagsAfterTagReload.size());
- Assertions.assertEquals(updatedTag1, cachedTagsAfterTagReload.get(0));
-
- List<GenericEntity> genericEntitiesAfterTagUpdate =
- relationOperations.listEntitiesByRelation(
- SupportsRelationOperations.Type.TAG_METADATA_OBJECT_REL,
- updatedTag1.nameIdentifier(),
- EntityType.TAG,
- true);
- Assertions.assertEquals(1, genericEntitiesAfterTagUpdate.size());
- Assertions.assertEquals(catalog.id(), genericEntitiesAfterTagUpdate.get(0).id());
- Assertions.assertEquals(updatedCatalog.name(), genericEntitiesAfterTagUpdate.get(0).name());
- List<Entity> cachedTagsByTagAfterTagReload =
- cache.get(
- EntityCacheRelationKey.of(
- updatedTag1.nameIdentifier(),
- EntityType.TAG,
- SupportsRelationOperations.Type.TAG_METADATA_OBJECT_REL),
- k -> null);
- Assertions.assertNotNull(cachedTagsByTagAfterTagReload);
- Assertions.assertEquals(1, cachedTagsByTagAfterTagReload.size());
- Assertions.assertEquals(
- updatedCatalog.id(), ((GenericEntity) cachedTagsByTagAfterTagReload.get(0)).id());
- Assertions.assertEquals(
- updatedCatalog.name(), ((GenericEntity) cachedTagsByTagAfterTagReload.get(0)).name());
- }
- }
+ identifier,
+ TableEntity.class,
+ Entity.EntityType.TABLE,
+ e -> createTableEntity(table1Id, namespace, "sameNameChanged", e.auditInfo()));
- @ParameterizedTest
- @MethodSource("storageProvider")
- void testLanceTableCreateAndUpdate(String type, boolean enableCache)
- throws IllegalAccessException {
- Config config = Mockito.mock(Config.class);
- Mockito.when(config.get(Configs.CACHE_ENABLED)).thenReturn(enableCache);
+ NameIdentifier changedNameIdentifier =
+ NameIdentifier.of("metalake1", "catalog1", "schema1", "sameNameChanged");
+ Assertions.assertNotNull(
+ store.get(changedNameIdentifier, Entity.EntityType.TABLE, TableEntity.class));
+ Assertions.assertNotNull(
+ store.get(identifier, Entity.EntityType.FILESET, FilesetEntity.class));
+ Assertions.assertNotNull(store.get(identifier, Entity.EntityType.TOPIC, TopicEntity.class));
+ Assertions.assertNotNull(store.get(identifier, Entity.EntityType.MODEL, ModelEntity.class));
- init(type, config);
- SqlSessionFactoryHelper.getInstance().init(GravitinoEnv.getInstance().config());
+ table1 =
+ createTableEntity(RandomIdGenerator.INSTANCE.nextId(), namespace, "sameName", auditInfo);
+ store.put(table1);
- AuditInfo auditInfo =
- AuditInfo.builder().withCreator("creator").withCreateTime(Instant.now()).build();
+ long filesetId = filesetEntity1.id();
+ store.update(
+ identifier,
+ FilesetEntity.class,
+ Entity.EntityType.FILESET,
+ e -> createFilesetEntity(filesetId, namespace, "sameNameChanged", e.auditInfo()));
- try (EntityStore store = EntityStoreFactory.createEntityStore(config)) {
- store.initialize(config);
+ Assertions.assertNotNull(store.get(identifier, Entity.EntityType.TABLE, TableEntity.class));
+ Assertions.assertNotNull(
+ store.get(changedNameIdentifier, Entity.EntityType.FILESET, FilesetEntity.class));
+ Assertions.assertNotNull(store.get(identifier, Entity.EntityType.TOPIC, TopicEntity.class));
+ Assertions.assertNotNull(store.get(identifier, Entity.EntityType.MODEL, ModelEntity.class));
- BaseMetalake metalake =
- createBaseMakeLake(RandomIdGenerator.INSTANCE.nextId(), "metalake", auditInfo);
- store.put(metalake, false);
-
- CatalogEntity catalogEntity =
- CatalogEntity.builder()
- .withId(RandomIdGenerator.INSTANCE.nextId())
- .withName("catalog")
- .withNamespace(NamespaceUtil.ofCatalog("metalake"))
- .withType(Catalog.Type.RELATIONAL)
- .withProvider("generic-lakehouse")
- .withComment("This is a generic-lakehouse")
- .withProperties(ImmutableMap.of())
- .withAuditInfo(auditInfo)
- .build();
-
- store.put(catalogEntity, false);
-
- SchemaEntity schemaEntity =
- SchemaEntity.builder()
- .withId(RandomIdGenerator.INSTANCE.nextId())
- .withName("schema")
- .withNamespace(NamespaceUtil.ofSchema("metalake", "catalog"))
- .withComment("This is a schema for generic-lakehouse")
- .withProperties(ImmutableMap.of())
- .withAuditInfo(auditInfo)
- .build();
- store.put(schemaEntity, false);
-
- long column1Id = RandomIdGenerator.INSTANCE.nextId();
- TableEntity table =
- TableEntity.builder()
- .withId(RandomIdGenerator.INSTANCE.nextId())
- .withNamespace(NamespaceUtil.ofTable("metalake", "catalog", "schema"))
- .withName("table")
- .withAuditInfo(auditInfo)
- .withColumns(
- Lists.newArrayList(
- ColumnEntity.builder()
- .withId(column1Id)
- .withName("column1")
- .withDataType(Types.StringType.get())
- .withComment("test column")
- .withPosition(1)
- .withAuditInfo(auditInfo)
- .build()))
- .withComment("This is a lance table")
- .withProperties(ImmutableMap.of("location", "/tmp/test", "format", "lance"))
- .build();
- store.put(table, false);
- TableEntity fetchedTable =
- store.get(table.nameIdentifier(), Entity.EntityType.TABLE, TableEntity.class);
-
- // check table properties
- Assertions.assertEquals("/tmp/test", fetchedTable.properties().get("location"));
- Assertions.assertEquals("lance", fetchedTable.properties().get("format"));
- Assertions.assertEquals("This is a lance table", fetchedTable.comment());
- Assertions.assertEquals(1, fetchedTable.columns().size());
- Assertions.assertEquals("column1", fetchedTable.columns().get(0).name());
-
- // Now try to update the table
- TableEntity updatedTable =
- TableEntity.builder()
- .withId(table.id())
- .withNamespace(table.namespace())
- .withName(table.name())
- .withAuditInfo(auditInfo)
- .withColumns(
- Lists.newArrayList(
- ColumnEntity.builder()
- .withId(column1Id)
- .withName("column1")
- .withDataType(Types.StringType.get())
- .withComment("updated test column")
- .withPosition(1)
- .withAuditInfo(auditInfo)
- .build(),
- ColumnEntity.builder()
- .withId(RandomIdGenerator.INSTANCE.nextId())
- .withName("column2")
- .withDataType(Types.IntegerType.get())
- .withComment("new column")
- .withPosition(2)
- .withAuditInfo(auditInfo)
- .build()))
- .withComment("This is an updated lance table")
- .withProperties(ImmutableMap.of("location", "/tmp/updated_test", "format", "lance"))
- .build();
+ filesetEntity1 =
+ createFilesetEntity(
+ RandomIdGenerator.INSTANCE.nextId(), namespace, "sameName", auditInfo);
+ store.put(filesetEntity1);
+ long topicId = topicEntity1.id();
store.update(
- table.nameIdentifier(), TableEntity.class, Entity.EntityType.TABLE, e -> updatedTable);
- TableEntity fetchedUpdatedTable =
- store.get(table.nameIdentifier(), Entity.EntityType.TABLE, TableEntity.class);
-
- // check updated table properties
- Assertions.assertEquals(
- "/tmp/updated_test", fetchedUpdatedTable.properties().get("location"));
- Assertions.assertEquals("lance", fetchedUpdatedTable.properties().get("format"));
- Assertions.assertEquals("This is an updated lance table", fetchedUpdatedTable.comment());
- Assertions.assertEquals(2, fetchedUpdatedTable.columns().size());
- for (ColumnEntity column : fetchedUpdatedTable.columns()) {
- if (column.name().equals("column1")) {
- Assertions.assertEquals("updated test column", column.comment());
- }
- }
-
- Assertions.assertTrue(
- fetchedUpdatedTable.columns().stream().anyMatch(c -> c.name().equals("column2")));
-
- // Test drop the table
- Assertions.assertTrue(store.delete(table.nameIdentifier(), Entity.EntityType.TABLE));
- Assertions.assertFalse(store.exists(table.nameIdentifier(), Entity.EntityType.TABLE));
+ identifier,
+ TopicEntity.class,
+ Entity.EntityType.TOPIC,
+ e -> createTopicEntity(topicId, namespace, "sameNameChanged", e.auditInfo()));
+
+ Assertions.assertNotNull(store.get(identifier, Entity.EntityType.TABLE, TableEntity.class));
+ Assertions.assertNotNull(
+ store.get(identifier, Entity.EntityType.FILESET, FilesetEntity.class));
+ Assertions.assertNotNull(
+ store.get(changedNameIdentifier, Entity.EntityType.TOPIC, TopicEntity.class));
+ Assertions.assertNotNull(store.get(identifier, Entity.EntityType.MODEL, ModelEntity.class));
destroy(type);
- } catch (IOException e) {
- throw new RuntimeException(e);
}
}
-
- private UserEntity createUserEntity(
- Long id, Namespace namespace, String name, AuditInfo auditInfo) {
- return UserEntity.builder()
- .withId(id)
- .withName(name)
- .withNamespace(namespace)
- .withRoleNames(null)
- .withRoleIds(null)
- .withAuditInfo(auditInfo)
- .build();
- }
-
- private RoleEntity createRoleEntity(
- Long id, Namespace namespace, String name, AuditInfo auditInfo, String catalogName) {
- SecurableObject securableObject =
- SecurableObjects.ofCatalog(catalogName, Lists.newArrayList(Privileges.UseCatalog.allow()));
-
- return RoleEntity.builder()
- .withId(id)
- .withName(name)
- .withNamespace(namespace)
- .withProperties(null)
- .withAuditInfo(auditInfo)
- .withSecurableObjects(Lists.newArrayList(securableObject))
- .build();
- }
-
- private ModelEntity createModelEntity(
- Long id,
- Namespace namespace,
- String name,
- String comment,
- Integer latestVersion,
- Map<String, String> properties,
- AuditInfo auditInfo) {
- return ModelEntity.builder()
- .withId(id)
- .withName(name)
- .withNamespace(namespace)
- .withComment(comment)
- .withLatestVersion(latestVersion)
- .withProperties(properties)
- .withAuditInfo(auditInfo)
- .build();
- }
-
- private ModelVersionEntity createModelVersionEntity(
- NameIdentifier modelId,
- Integer version,
- Map<String, String> modelUris,
- List<String> aliases,
- String comment,
- Map<String, String> properties,
- AuditInfo auditInfo) {
- return ModelVersionEntity.builder()
- .withModelIdentifier(modelId)
- .withVersion(version)
- .withUris(modelUris)
- .withAliases(aliases)
- .withComment(comment)
- .withProperties(properties)
- .withAuditInfo(auditInfo)
- .build();
- }
+ // Note: Do not add more tests in this class as it's already getting too large. If needed, please
+ // just extend AbstractEntityStoreTest and add a new test class. More, please refer to
+ // `TestEntityStoreForLance` or `TestEntityStoreRelationCache`.
}
diff --git a/core/src/test/java/org/apache/gravitino/storage/TestEntityStorageForLance.java b/core/src/test/java/org/apache/gravitino/storage/TestEntityStorageForLance.java
new file mode 100644
index 0000000000..83d15ef0b5
--- /dev/null
+++ b/core/src/test/java/org/apache/gravitino/storage/TestEntityStorageForLance.java
@@ -0,0 +1,177 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.gravitino.storage;
+
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.Lists;
+import java.io.IOException;
+import java.time.Instant;
+import org.apache.gravitino.Catalog;
+import org.apache.gravitino.Config;
+import org.apache.gravitino.Configs;
+import org.apache.gravitino.Entity;
+import org.apache.gravitino.EntityStore;
+import org.apache.gravitino.EntityStoreFactory;
+import org.apache.gravitino.meta.AuditInfo;
+import org.apache.gravitino.meta.BaseMetalake;
+import org.apache.gravitino.meta.CatalogEntity;
+import org.apache.gravitino.meta.ColumnEntity;
+import org.apache.gravitino.meta.SchemaEntity;
+import org.apache.gravitino.meta.TableEntity;
+import org.apache.gravitino.rel.types.Types;
+import org.apache.gravitino.utils.NamespaceUtil;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Tag;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.MethodSource;
+import org.mockito.Mockito;
+
+@Tag("gravitino-docker-test")
+public class TestEntityStorageForLance extends AbstractEntityStorageTest {
+
+ @ParameterizedTest
+ @MethodSource("storageProvider")
+ void testLanceTableCreateAndUpdate(String type, boolean enableCache)
+ throws IllegalAccessException {
+ Config config = Mockito.mock(Config.class);
+ Mockito.when(config.get(Configs.CACHE_ENABLED)).thenReturn(enableCache);
+ init(type, config);
+
+ AuditInfo auditInfo =
+ AuditInfo.builder().withCreator("creator").withCreateTime(Instant.now()).build();
+
+ try (EntityStore store = EntityStoreFactory.createEntityStore(config)) {
+ store.initialize(config);
+
+ BaseMetalake metalake =
+ createBaseMakeLake(RandomIdGenerator.INSTANCE.nextId(), "metalake", auditInfo);
+ store.put(metalake, false);
+
+ CatalogEntity catalogEntity =
+ CatalogEntity.builder()
+ .withId(RandomIdGenerator.INSTANCE.nextId())
+ .withName("catalog")
+ .withNamespace(NamespaceUtil.ofCatalog("metalake"))
+ .withType(Catalog.Type.RELATIONAL)
+ .withProvider("generic-lakehouse")
+ .withComment("This is a generic-lakehouse")
+ .withProperties(ImmutableMap.of())
+ .withAuditInfo(auditInfo)
+ .build();
+
+ store.put(catalogEntity, false);
+
+ SchemaEntity schemaEntity =
+ SchemaEntity.builder()
+ .withId(RandomIdGenerator.INSTANCE.nextId())
+ .withName("schema")
+ .withNamespace(NamespaceUtil.ofSchema("metalake", "catalog"))
+ .withComment("This is a schema for generic-lakehouse")
+ .withProperties(ImmutableMap.of())
+ .withAuditInfo(auditInfo)
+ .build();
+ store.put(schemaEntity, false);
+
+ long column1Id = RandomIdGenerator.INSTANCE.nextId();
+ TableEntity table =
+ TableEntity.builder()
+ .withId(RandomIdGenerator.INSTANCE.nextId())
+ .withNamespace(NamespaceUtil.ofTable("metalake", "catalog", "schema"))
+ .withName("table")
+ .withAuditInfo(auditInfo)
+ .withColumns(
+ Lists.newArrayList(
+ ColumnEntity.builder()
+ .withId(column1Id)
+ .withName("column1")
+ .withDataType(Types.StringType.get())
+ .withComment("test column")
+ .withPosition(1)
+ .withAuditInfo(auditInfo)
+ .build()))
+ .withComment("This is a lance table")
+ .withProperties(ImmutableMap.of("location", "/tmp/test", "format", "lance"))
+ .build();
+ store.put(table, false);
+ TableEntity fetchedTable =
+ store.get(table.nameIdentifier(), Entity.EntityType.TABLE, TableEntity.class);
+
+ Assertions.assertEquals("/tmp/test", fetchedTable.properties().get("location"));
+ Assertions.assertEquals("lance", fetchedTable.properties().get("format"));
+ Assertions.assertEquals("This is a lance table", fetchedTable.comment());
+ Assertions.assertEquals(1, fetchedTable.columns().size());
+ Assertions.assertEquals("column1", fetchedTable.columns().get(0).name());
+
+ TableEntity updatedTable =
+ TableEntity.builder()
+ .withId(table.id())
+ .withNamespace(table.namespace())
+ .withName(table.name())
+ .withAuditInfo(auditInfo)
+ .withColumns(
+ Lists.newArrayList(
+ ColumnEntity.builder()
+ .withId(column1Id)
+ .withName("column1")
+ .withDataType(Types.StringType.get())
+ .withComment("updated test column")
+ .withPosition(1)
+ .withAuditInfo(auditInfo)
+ .build(),
+ ColumnEntity.builder()
+ .withId(RandomIdGenerator.INSTANCE.nextId())
+ .withName("column2")
+ .withDataType(Types.IntegerType.get())
+ .withComment("new column")
+ .withPosition(2)
+ .withAuditInfo(auditInfo)
+ .build()))
+ .withComment("This is an updated lance table")
+ .withProperties(ImmutableMap.of("location", "/tmp/updated_test", "format", "lance"))
+ .build();
+
+ store.update(
+ table.nameIdentifier(), TableEntity.class, Entity.EntityType.TABLE, e -> updatedTable);
+ TableEntity fetchedUpdatedTable =
+ store.get(table.nameIdentifier(), Entity.EntityType.TABLE, TableEntity.class);
+
+ Assertions.assertEquals(
+ "/tmp/updated_test", fetchedUpdatedTable.properties().get("location"));
+ Assertions.assertEquals("lance", fetchedUpdatedTable.properties().get("format"));
+ Assertions.assertEquals("This is an updated lance table", fetchedUpdatedTable.comment());
+ Assertions.assertEquals(2, fetchedUpdatedTable.columns().size());
+ for (ColumnEntity column : fetchedUpdatedTable.columns()) {
+ if (column.name().equals("column1")) {
+ Assertions.assertEquals("updated test column", column.comment());
+ }
+ }
+
+ Assertions.assertTrue(
+ fetchedUpdatedTable.columns().stream().anyMatch(c -> c.name().equals("column2")));
+
+ Assertions.assertTrue(store.delete(table.nameIdentifier(), Entity.EntityType.TABLE));
+ Assertions.assertFalse(store.exists(table.nameIdentifier(), Entity.EntityType.TABLE));
+
+ destroy(type);
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ }
+}
diff --git a/core/src/test/java/org/apache/gravitino/storage/TestEntityStorageRelationCache.java b/core/src/test/java/org/apache/gravitino/storage/TestEntityStorageRelationCache.java
new file mode 100644
index 0000000000..b645d8126a
--- /dev/null
+++ b/core/src/test/java/org/apache/gravitino/storage/TestEntityStorageRelationCache.java
@@ -0,0 +1,776 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.gravitino.storage;
+
+import com.github.benmanes.caffeine.cache.Cache;
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.Lists;
+import java.time.Instant;
+import java.util.Collections;
+import java.util.List;
+import java.util.stream.Collectors;
+import org.apache.gravitino.Config;
+import org.apache.gravitino.Configs;
+import org.apache.gravitino.Entity;
+import org.apache.gravitino.EntityStore;
+import org.apache.gravitino.EntityStoreFactory;
+import org.apache.gravitino.MetadataObject;
+import org.apache.gravitino.NameIdentifier;
+import org.apache.gravitino.Namespace;
+import org.apache.gravitino.SupportsRelationOperations;
+import org.apache.gravitino.authorization.AuthorizationUtils;
+import org.apache.gravitino.authorization.Privilege.Condition;
+import org.apache.gravitino.authorization.Privileges;
+import org.apache.gravitino.authorization.Role;
+import org.apache.gravitino.authorization.SecurableObject;
+import org.apache.gravitino.authorization.SecurableObjects;
+import org.apache.gravitino.cache.CaffeineEntityCache;
+import org.apache.gravitino.cache.EntityCacheKey;
+import org.apache.gravitino.cache.EntityCacheRelationKey;
+import org.apache.gravitino.cache.ReverseIndexCache;
+import org.apache.gravitino.meta.AuditInfo;
+import org.apache.gravitino.meta.BaseMetalake;
+import org.apache.gravitino.meta.CatalogEntity;
+import org.apache.gravitino.meta.FilesetEntity;
+import org.apache.gravitino.meta.GenericEntity;
+import org.apache.gravitino.meta.PolicyEntity;
+import org.apache.gravitino.meta.RoleEntity;
+import org.apache.gravitino.meta.SchemaEntity;
+import org.apache.gravitino.meta.TagEntity;
+import org.apache.gravitino.meta.UserEntity;
+import org.apache.gravitino.policy.Policy;
+import org.apache.gravitino.policy.PolicyContents;
+import org.apache.gravitino.storage.relational.RelationalEntityStore;
+import org.apache.gravitino.utils.NameIdentifierUtil;
+import org.apache.gravitino.utils.NamespaceUtil;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Tag;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.MethodSource;
+import org.mockito.Mockito;
+
+@Tag("gravitino-docker-test")
+public class TestEntityStorageRelationCache extends AbstractEntityStorageTest {
+
+ @ParameterizedTest
+ @MethodSource("storageProvider")
+ void testInvalidRelationCache(String type, boolean enableCache) throws Exception {
+ Config config = Mockito.mock(Config.class);
+ Mockito.when(config.get(Configs.CACHE_ENABLED)).thenReturn(enableCache);
+ init(type, config);
+
+ AuditInfo auditInfo =
+ AuditInfo.builder().withCreator("creator").withCreateTime(Instant.now()).build();
+
+ try (EntityStore store = EntityStoreFactory.createEntityStore(config)) {
+ store.initialize(config);
+
+ BaseMetalake metalake =
+ createBaseMakeLake(RandomIdGenerator.INSTANCE.nextId(), "metalake", auditInfo);
+ store.put(metalake, false);
+
+ CatalogEntity catalog =
+ createCatalog(
+ RandomIdGenerator.INSTANCE.nextId(),
+ NamespaceUtil.ofCatalog("metalake"),
+ "catalog",
+ auditInfo);
+ store.put(catalog, false);
+
+ RoleEntity role =
+ createRoleEntity(
+ RandomIdGenerator.INSTANCE.nextId(),
+ AuthorizationUtils.ofRoleNamespace("metalake"),
+ "role",
+ auditInfo,
+ "catalog");
+ store.put(role, false);
+
+ Role oldRole = store.get(role.nameIdentifier(), Entity.EntityType.ROLE, RoleEntity.class);
+
+ CatalogEntity updatedCatalog =
+ CatalogEntity.builder()
+ .withId(catalog.id())
+ .withNamespace(catalog.namespace())
+ .withName("newCatalogName")
+ .withAuditInfo(auditInfo)
+ .withComment(catalog.getComment())
+ .withProperties(catalog.getProperties())
+ .withType(catalog.getType())
+ .withProvider(catalog.getProvider())
+ .build();
+ store.update(
+ catalog.nameIdentifier(),
+ CatalogEntity.class,
+ Entity.EntityType.CATALOG,
+ e -> updatedCatalog);
+
+ Role newRow = store.get(role.nameIdentifier(), Entity.EntityType.ROLE, RoleEntity.class);
+ Assertions.assertNotEquals(oldRole, newRow);
+ Assertions.assertNotEquals(oldRole.securableObjects(), newRow.securableObjects());
+ List<SecurableObject> securableObjects = newRow.securableObjects();
+ Assertions.assertEquals(1, securableObjects.size());
+ Assertions.assertEquals("newCatalogName", securableObjects.get(0).name());
+
+ SchemaEntity schema =
+ createSchemaEntity(
+ RandomIdGenerator.INSTANCE.nextId(),
+ Namespace.of("metalake", "newCatalogName"),
+ "schema",
+ auditInfo);
+
+ store.put(schema, false);
+ FilesetEntity fileset =
+ createFilesetEntity(
+ RandomIdGenerator.INSTANCE.nextId(),
+ Namespace.of("metalake", "newCatalogName", "schema"),
+ "fileset",
+ auditInfo);
+ store.put(fileset, false);
+
+ SecurableObject catalogObject =
+ SecurableObjects.ofCatalog("newCatalogName", Lists.newArrayList());
+ SecurableObject schemaObject =
+ SecurableObjects.ofSchema(catalogObject, "schema", Lists.newArrayList());
+ SecurableObject securableFileset01 =
+ SecurableObjects.ofFileset(
+ schemaObject, "fileset", Lists.newArrayList(Privileges.ReadFileset.allow()));
+ SecurableObject securableFileset02 =
+ SecurableObjects.ofFileset(
+ schemaObject, "fileset", Lists.newArrayList(Privileges.WriteFileset.allow()));
+
+ RoleEntity readRole =
+ RoleEntity.builder()
+ .withId(RandomIdGenerator.INSTANCE.nextId())
+ .withName("roleReadFileset")
+ .withNamespace(AuthorizationUtils.ofRoleNamespace("metalake"))
+ .withProperties(null)
+ .withAuditInfo(auditInfo)
+ .withSecurableObjects(Lists.newArrayList(securableFileset01))
+ .build();
+ store.put(readRole, false);
+
+ RoleEntity writeRole =
+ RoleEntity.builder()
+ .withId(RandomIdGenerator.INSTANCE.nextId())
+ .withName("roleWriteFileset")
+ .withNamespace(AuthorizationUtils.ofRoleNamespace("metalake"))
+ .withProperties(null)
+ .withAuditInfo(auditInfo)
+ .withSecurableObjects(Lists.newArrayList(securableFileset02))
+ .build();
+ store.put(writeRole, false);
+
+ Role loadedReadRole =
+ store.get(readRole.nameIdentifier(), Entity.EntityType.ROLE, RoleEntity.class);
+ Role loadedWriteRole =
+ store.get(writeRole.nameIdentifier(), Entity.EntityType.ROLE, RoleEntity.class);
+ Assertions.assertEquals(1, loadedReadRole.securableObjects().size());
+ Assertions.assertEquals(
+ "newCatalogName.schema.fileset", loadedReadRole.securableObjects().get(0).fullName());
+ Assertions.assertEquals(1, loadedWriteRole.securableObjects().size());
+ Assertions.assertEquals(
+ "newCatalogName.schema.fileset", loadedReadRole.securableObjects().get(0).fullName());
+ Assertions.assertEquals(
+ Condition.ALLOW,
+ loadedReadRole.securableObjects().get(0).privileges().get(0).condition());
+ Assertions.assertEquals(
+ Condition.ALLOW,
+ loadedWriteRole.securableObjects().get(0).privileges().get(0).condition());
+
+ store.delete(readRole.nameIdentifier(), Entity.EntityType.ROLE);
+
+ ReverseIndexCache reverseIndexCache =
+ ((CaffeineEntityCache) ((RelationalEntityStore) store).getCache()).getReverseIndex();
+ List<EntityCacheKey> reverseIndexValue =
+ reverseIndexCache.get(
+ NameIdentifier.of("metalake", "newCatalogName", "schema", "fileset"),
+ Entity.EntityType.FILESET);
+ Assertions.assertEquals(1, reverseIndexValue.size());
+ Assertions.assertEquals(writeRole.nameIdentifier(), reverseIndexValue.get(0).identifier());
+
+ store.put(readRole, true);
+ store.get(readRole.nameIdentifier(), Entity.EntityType.ROLE, RoleEntity.class);
+ reverseIndexValue =
+ reverseIndexCache.get(
+ NameIdentifier.of("metalake", "newCatalogName", "schema", "fileset"),
+ Entity.EntityType.FILESET);
+ Assertions.assertEquals(2, reverseIndexValue.size());
+ List<NameIdentifier> ids =
+ reverseIndexValue.stream().map(EntityCacheKey::identifier).collect(Collectors.toList());
+ Assertions.assertTrue(ids.contains(readRole.nameIdentifier()));
+ Assertions.assertTrue(ids.contains(writeRole.nameIdentifier()));
+
+ store.delete(readRole.nameIdentifier(), Entity.EntityType.ROLE);
+ store.delete(writeRole.nameIdentifier(), Entity.EntityType.ROLE);
+
+ reverseIndexValue =
+ reverseIndexCache.get(
+ NameIdentifier.of("metalake", "newCatalogName", "schema", "fileset"),
+ Entity.EntityType.FILESET);
+ Assertions.assertNull(reverseIndexValue);
+
+ store.put(readRole, true);
+ store.put(writeRole, true);
+ store.get(readRole.nameIdentifier(), Entity.EntityType.ROLE, RoleEntity.class);
+ store.get(writeRole.nameIdentifier(), Entity.EntityType.ROLE, RoleEntity.class);
+
+ store.update(
+ fileset.nameIdentifier(),
+ FilesetEntity.class,
+ Entity.EntityType.FILESET,
+ e ->
+ createFilesetEntity(
+ fileset.id(), fileset.namespace(), "fileset_new", fileset.auditInfo()));
+
+ loadedReadRole =
+ store.get(readRole.nameIdentifier(), Entity.EntityType.ROLE, RoleEntity.class);
+ loadedWriteRole =
+ store.get(writeRole.nameIdentifier(), Entity.EntityType.ROLE, RoleEntity.class);
+
+ Assertions.assertEquals(1, loadedReadRole.securableObjects().size());
+ Assertions.assertEquals(
+ "newCatalogName.schema.fileset_new", loadedReadRole.securableObjects().get(0).fullName());
+ Assertions.assertEquals(1, loadedWriteRole.securableObjects().size());
+ Assertions.assertEquals(
+ "newCatalogName.schema.fileset_new", loadedReadRole.securableObjects().get(0).fullName());
+ Assertions.assertEquals(
+ Condition.ALLOW,
+ loadedReadRole.securableObjects().get(0).privileges().get(0).condition());
+ Assertions.assertEquals(
+ Condition.ALLOW,
+ loadedWriteRole.securableObjects().get(0).privileges().get(0).condition());
+
+ store.update(
+ NameIdentifier.of("metalake", "newCatalogName", "schema"),
+ SchemaEntity.class,
+ Entity.EntityType.SCHEMA,
+ e ->
+ createSchemaEntity(
+ schema.id(),
+ Namespace.of("metalake", "newCatalogName"),
+ "schema_new",
+ schema.auditInfo()));
+ loadedReadRole =
+ store.get(readRole.nameIdentifier(), Entity.EntityType.ROLE, RoleEntity.class);
+ loadedWriteRole =
+ store.get(writeRole.nameIdentifier(), Entity.EntityType.ROLE, RoleEntity.class);
+ Assertions.assertEquals(1, loadedReadRole.securableObjects().size());
+ Assertions.assertEquals(
+ "newCatalogName.schema_new.fileset_new",
+ loadedReadRole.securableObjects().get(0).fullName());
+ Assertions.assertEquals(1, loadedWriteRole.securableObjects().size());
+ Assertions.assertEquals(
+ "newCatalogName.schema_new.fileset_new",
+ loadedReadRole.securableObjects().get(0).fullName());
+
+ UserEntity user1 =
+ createUserEntity(
+ RandomIdGenerator.INSTANCE.nextId(),
+ AuthorizationUtils.ofUserNamespace("metalake"),
+ "user1",
+ auditInfo);
+ store.put(user1, false);
+
+ store.delete(
+ NameIdentifier.of("metalake", "newCatalogName", "schema_new", "fileset_new"),
+ Entity.EntityType.FILESET);
+ Assertions.assertFalse(store.exists(fileset.nameIdentifier(), Entity.EntityType.FILESET));
+
+ loadedReadRole =
+ store.get(readRole.nameIdentifier(), Entity.EntityType.ROLE, RoleEntity.class);
+ loadedWriteRole =
+ store.get(writeRole.nameIdentifier(), Entity.EntityType.ROLE, RoleEntity.class);
+
+ Assertions.assertEquals(0, loadedReadRole.securableObjects().size());
+ Assertions.assertEquals(0, loadedWriteRole.securableObjects().size());
+
+ destroy(type);
+ }
+ }
+
+ @ParameterizedTest
+ @MethodSource("storageProvider")
+ void testTagRelationCache(String type, boolean enableCache) throws Exception {
+ Config config = Mockito.mock(Config.class);
+ Mockito.when(config.get(Configs.CACHE_ENABLED)).thenReturn(enableCache);
+ init(type, config);
+
+ AuditInfo auditInfo =
+ AuditInfo.builder().withCreator("creator").withCreateTime(Instant.now()).build();
+
+ try (EntityStore store = EntityStoreFactory.createEntityStore(config)) {
+ store.initialize(config);
+
+ BaseMetalake metalake =
+ createBaseMakeLake(RandomIdGenerator.INSTANCE.nextId(), "metalake", auditInfo);
+ store.put(metalake, false);
+
+ Namespace namespace = NameIdentifierUtil.ofTag("metalake", "tag1").namespace();
+ TagEntity tag1 =
+ TagEntity.builder()
+ .withId(RandomIdGenerator.INSTANCE.nextId())
+ .withNamespace(namespace)
+ .withName("tag1")
+ .withAuditInfo(auditInfo)
+ .withProperties(Collections.emptyMap())
+ .build();
+ CatalogEntity catalog =
+ createCatalog(
+ RandomIdGenerator.INSTANCE.nextId(),
+ NamespaceUtil.ofCatalog("metalake"),
+ "catalog",
+ auditInfo);
+
+ store.put(catalog, false);
+ store.put(tag1, false);
+
+ SupportsRelationOperations relationOperations = (SupportsRelationOperations) store;
+
+ relationOperations.updateEntityRelations(
+ SupportsRelationOperations.Type.TAG_METADATA_OBJECT_REL,
+ catalog.nameIdentifier(),
+ Entity.EntityType.CATALOG,
+ new NameIdentifier[] {tag1.nameIdentifier()},
+ new NameIdentifier[] {});
+
+ List<TagEntity> tags =
+ relationOperations.listEntitiesByRelation(
+ SupportsRelationOperations.Type.TAG_METADATA_OBJECT_REL,
+ catalog.nameIdentifier(),
+ Entity.EntityType.CATALOG,
+ true);
+ Assertions.assertEquals(1, tags.size());
+ Assertions.assertEquals(tag1, tags.get(0));
+
+ RelationalEntityStore relationalEntityStore = (RelationalEntityStore) store;
+ CaffeineEntityCache caffeineEntityCache =
+ (CaffeineEntityCache) relationalEntityStore.getCache();
+ Cache<EntityCacheRelationKey, List<Entity>> cache = caffeineEntityCache.getCacheData();
+
+ List<Entity> cachedTags =
+ cache.get(
+ EntityCacheRelationKey.of(
+ catalog.nameIdentifier(),
+ Entity.EntityType.CATALOG,
+ SupportsRelationOperations.Type.TAG_METADATA_OBJECT_REL),
+ k -> null);
+
+ Assertions.assertNotNull(cachedTags);
+ Assertions.assertEquals(1, cachedTags.size());
+ Assertions.assertEquals(tag1, cachedTags.get(0));
+
+ List<GenericEntity> genericEntities =
+ relationOperations.listEntitiesByRelation(
+ SupportsRelationOperations.Type.TAG_METADATA_OBJECT_REL,
+ tag1.nameIdentifier(),
+ Entity.EntityType.TAG,
+ true);
+ Assertions.assertEquals(1, genericEntities.size());
+ Assertions.assertEquals(catalog.id(), genericEntities.get(0).id());
+ Assertions.assertEquals(catalog.name(), genericEntities.get(0).name());
+
+ CatalogEntity updatedCatalog =
+ CatalogEntity.builder()
+ .withId(catalog.id())
+ .withNamespace(catalog.namespace())
+ .withName("newCatalogName")
+ .withAuditInfo(auditInfo)
+ .withComment(catalog.getComment())
+ .withProperties(catalog.getProperties())
+ .withType(catalog.getType())
+ .withProvider(catalog.getProvider())
+ .build();
+ store.update(
+ catalog.nameIdentifier(),
+ CatalogEntity.class,
+ Entity.EntityType.CATALOG,
+ e -> updatedCatalog);
+ List<Entity> cachedTagsAfterCatalogUpdate =
+ cache.get(
+ EntityCacheRelationKey.of(
+ catalog.nameIdentifier(),
+ Entity.EntityType.CATALOG,
+ SupportsRelationOperations.Type.TAG_METADATA_OBJECT_REL),
+ k -> null);
+ Assertions.assertNull(cachedTagsAfterCatalogUpdate);
+
+ List<Entity> cachedTagsByTagAfterCatalogUpdate =
+ cache.get(
+ EntityCacheRelationKey.of(
+ tag1.nameIdentifier(),
+ Entity.EntityType.TAG,
+ SupportsRelationOperations.Type.TAG_METADATA_OBJECT_REL),
+ k -> null);
+ Assertions.assertNull(cachedTagsByTagAfterCatalogUpdate);
+
+ List<TagEntity> tagsAfterCatalogUpdate =
+ relationOperations.listEntitiesByRelation(
+ SupportsRelationOperations.Type.TAG_METADATA_OBJECT_REL,
+ updatedCatalog.nameIdentifier(),
+ Entity.EntityType.CATALOG,
+ true);
+ Assertions.assertEquals(1, tagsAfterCatalogUpdate.size());
+ Assertions.assertEquals(tag1, tagsAfterCatalogUpdate.get(0));
+
+ List<Entity> cachedTagsAfterReload =
+ cache.get(
+ EntityCacheRelationKey.of(
+ updatedCatalog.nameIdentifier(),
+ Entity.EntityType.CATALOG,
+ SupportsRelationOperations.Type.TAG_METADATA_OBJECT_REL),
+ k -> null);
+ Assertions.assertNotNull(cachedTagsAfterReload);
+ Assertions.assertEquals(1, cachedTagsAfterReload.size());
+ Assertions.assertEquals(tag1, cachedTagsAfterReload.get(0));
+
+ List<GenericEntity> genericEntitiesAfterCatalogUpdate =
+ relationOperations.listEntitiesByRelation(
+ SupportsRelationOperations.Type.TAG_METADATA_OBJECT_REL,
+ tag1.nameIdentifier(),
+ Entity.EntityType.TAG,
+ true);
+ Assertions.assertEquals(1, genericEntitiesAfterCatalogUpdate.size());
+ Assertions.assertEquals(updatedCatalog.id(), genericEntitiesAfterCatalogUpdate.get(0).id());
+ Assertions.assertEquals(
+ updatedCatalog.name(), genericEntitiesAfterCatalogUpdate.get(0).name());
+
+ List<Entity> cachedTagsByTagAfterReload =
+ cache.get(
+ EntityCacheRelationKey.of(
+ tag1.nameIdentifier(),
+ Entity.EntityType.TAG,
+ SupportsRelationOperations.Type.TAG_METADATA_OBJECT_REL),
+ k -> null);
+ Assertions.assertNotNull(cachedTagsByTagAfterReload);
+ Assertions.assertEquals(1, cachedTagsByTagAfterReload.size());
+ Assertions.assertEquals(
+ updatedCatalog.id(), ((GenericEntity) cachedTagsByTagAfterReload.get(0)).id());
+ Assertions.assertEquals(
+ updatedCatalog.name(), ((GenericEntity) cachedTagsByTagAfterReload.get(0)).name());
+
+ TagEntity updatedTag1 =
+ TagEntity.builder()
+ .withId(tag1.id())
+ .withNamespace(tag1.namespace())
+ .withName("tagChanged")
+ .withAuditInfo(auditInfo)
+ .withProperties(tag1.properties())
+ .build();
+ store.update(tag1.nameIdentifier(), TagEntity.class, Entity.EntityType.TAG, e -> updatedTag1);
+
+ List<Entity> cachedTagsAfterTagUpdate =
+ cache.get(
+ EntityCacheRelationKey.of(
+ updatedCatalog.nameIdentifier(),
+ Entity.EntityType.CATALOG,
+ SupportsRelationOperations.Type.TAG_METADATA_OBJECT_REL),
+ k -> null);
+ Assertions.assertNull(cachedTagsAfterTagUpdate);
+
+ List<Entity> cachedEntitiesAfterTagUpdate =
+ cache.get(
+ EntityCacheRelationKey.of(
+ tag1.nameIdentifier(),
+ Entity.EntityType.TAG,
+ SupportsRelationOperations.Type.TAG_METADATA_OBJECT_REL),
+ k -> null);
+ Assertions.assertNull(cachedEntitiesAfterTagUpdate);
+
+ List<TagEntity> tagsAfterTagUpdate =
+ relationOperations.listEntitiesByRelation(
+ SupportsRelationOperations.Type.TAG_METADATA_OBJECT_REL,
+ updatedCatalog.nameIdentifier(),
+ Entity.EntityType.CATALOG,
+ true);
+ Assertions.assertEquals(1, tagsAfterTagUpdate.size());
+ Assertions.assertEquals(updatedTag1, tagsAfterTagUpdate.get(0));
+
+ List<Entity> cachedTagsAfterTagReload =
+ cache.get(
+ EntityCacheRelationKey.of(
+ updatedCatalog.nameIdentifier(),
+ Entity.EntityType.CATALOG,
+ SupportsRelationOperations.Type.TAG_METADATA_OBJECT_REL),
+ k -> null);
+ Assertions.assertNotNull(cachedTagsAfterTagReload);
+ Assertions.assertEquals(1, cachedTagsAfterTagReload.size());
+ Assertions.assertEquals(updatedTag1, cachedTagsAfterTagReload.get(0));
+
+ List<GenericEntity> genericEntitiesAfterTagUpdate =
+ relationOperations.listEntitiesByRelation(
+ SupportsRelationOperations.Type.TAG_METADATA_OBJECT_REL,
+ updatedTag1.nameIdentifier(),
+ Entity.EntityType.TAG,
+ true);
+ Assertions.assertEquals(1, genericEntitiesAfterTagUpdate.size());
+ Assertions.assertEquals(catalog.id(), genericEntitiesAfterTagUpdate.get(0).id());
+ Assertions.assertEquals(updatedCatalog.name(), genericEntitiesAfterTagUpdate.get(0).name());
+ List<Entity> cachedTagsByTagAfterTagReload =
+ cache.get(
+ EntityCacheRelationKey.of(
+ updatedTag1.nameIdentifier(),
+ Entity.EntityType.TAG,
+ SupportsRelationOperations.Type.TAG_METADATA_OBJECT_REL),
+ k -> null);
+ Assertions.assertNotNull(cachedTagsByTagAfterTagReload);
+ Assertions.assertEquals(1, cachedTagsByTagAfterTagReload.size());
+ Assertions.assertEquals(
+ updatedCatalog.id(), ((GenericEntity) cachedTagsByTagAfterTagReload.get(0)).id());
+ Assertions.assertEquals(
+ updatedCatalog.name(), ((GenericEntity) cachedTagsByTagAfterTagReload.get(0)).name());
+ destroy(type);
+ }
+ }
+
+ @ParameterizedTest
+ @MethodSource("storageProvider")
+ void testTagRelationMultipleBindings(String type, boolean enableCache) throws Exception {
+ Config config = Mockito.mock(Config.class);
+ Mockito.when(config.get(Configs.CACHE_ENABLED)).thenReturn(enableCache);
+ init(type, config);
+
+ AuditInfo auditInfo =
+ AuditInfo.builder().withCreator("creator").withCreateTime(Instant.now()).build();
+
+ try (EntityStore store = EntityStoreFactory.createEntityStore(config)) {
+ store.initialize(config);
+
+ BaseMetalake metalake =
+ createBaseMakeLake(RandomIdGenerator.INSTANCE.nextId(), "metalake", auditInfo);
+ store.put(metalake, false);
+
+ Namespace tagNamespace = NameIdentifierUtil.ofTag("metalake", "tag1").namespace();
+ TagEntity tag1 =
+ TagEntity.builder()
+ .withId(RandomIdGenerator.INSTANCE.nextId())
+ .withNamespace(tagNamespace)
+ .withName("tag1")
+ .withAuditInfo(auditInfo)
+ .withProperties(Collections.emptyMap())
+ .build();
+
+ CatalogEntity catalog1 =
+ createCatalog(
+ RandomIdGenerator.INSTANCE.nextId(),
+ NamespaceUtil.ofCatalog("metalake"),
+ "catalog1",
+ auditInfo);
+
+ store.put(tag1, false);
+ store.put(catalog1, false);
+
+ SupportsRelationOperations relationOperations = (SupportsRelationOperations) store;
+
+ relationOperations.updateEntityRelations(
+ SupportsRelationOperations.Type.TAG_METADATA_OBJECT_REL,
+ catalog1.nameIdentifier(),
+ Entity.EntityType.CATALOG,
+ new NameIdentifier[] {tag1.nameIdentifier()},
+ new NameIdentifier[] {});
+
+ List<TagEntity> tagsForCatalog1 =
+ relationOperations.listEntitiesByRelation(
+ SupportsRelationOperations.Type.TAG_METADATA_OBJECT_REL,
+ catalog1.nameIdentifier(),
+ Entity.EntityType.CATALOG,
+ true);
+ Assertions.assertEquals(1, tagsForCatalog1.size());
+ Assertions.assertEquals(tag1, tagsForCatalog1.get(0));
+
+ List<GenericEntity> entitiesForTag1 =
+ relationOperations.listEntitiesByRelation(
+ SupportsRelationOperations.Type.TAG_METADATA_OBJECT_REL,
+ tag1.nameIdentifier(),
+ Entity.EntityType.TAG,
+ true);
+ Assertions.assertEquals(1, entitiesForTag1.size());
+ Assertions.assertEquals(catalog1.id(), entitiesForTag1.get(0).id());
+ Assertions.assertEquals(catalog1.name(), entitiesForTag1.get(0).name());
+
+ CatalogEntity catalog2 =
+ createCatalog(
+ RandomIdGenerator.INSTANCE.nextId(),
+ NamespaceUtil.ofCatalog("metalake"),
+ "catalog2",
+ auditInfo);
+ store.put(catalog2, false);
+
+ relationOperations.updateEntityRelations(
+ SupportsRelationOperations.Type.TAG_METADATA_OBJECT_REL,
+ catalog2.nameIdentifier(),
+ Entity.EntityType.CATALOG,
+ new NameIdentifier[] {tag1.nameIdentifier()},
+ new NameIdentifier[] {});
+
+ tagsForCatalog1 =
+ relationOperations.listEntitiesByRelation(
+ SupportsRelationOperations.Type.TAG_METADATA_OBJECT_REL,
+ catalog1.nameIdentifier(),
+ Entity.EntityType.CATALOG,
+ true);
+ Assertions.assertEquals(1, tagsForCatalog1.size());
+ Assertions.assertEquals(tag1, tagsForCatalog1.get(0));
+
+ List<TagEntity> tagsForCatalog2 =
+ relationOperations.listEntitiesByRelation(
+ SupportsRelationOperations.Type.TAG_METADATA_OBJECT_REL,
+ catalog2.nameIdentifier(),
+ Entity.EntityType.CATALOG,
+ true);
+ Assertions.assertEquals(1, tagsForCatalog2.size());
+ Assertions.assertEquals(tag1, tagsForCatalog2.get(0));
+
+ entitiesForTag1 =
+ relationOperations.listEntitiesByRelation(
+ SupportsRelationOperations.Type.TAG_METADATA_OBJECT_REL,
+ tag1.nameIdentifier(),
+ Entity.EntityType.TAG,
+ true);
+ Assertions.assertEquals(2, entitiesForTag1.size());
+ List<Long> entityIds =
+ entitiesForTag1.stream().map(GenericEntity::id).collect(Collectors.toList());
+ List<String> entityNames =
+ entitiesForTag1.stream().map(GenericEntity::name).collect(Collectors.toList());
+ Assertions.assertTrue(entityIds.contains(catalog1.id()));
+ Assertions.assertTrue(entityIds.contains(catalog2.id()));
+ Assertions.assertTrue(entityNames.contains(catalog1.name()));
+ Assertions.assertTrue(entityNames.contains(catalog2.name()));
+ destroy(type);
+ }
+ }
+
+ @ParameterizedTest
+ @MethodSource("storageProvider")
+ void testPolicyRelationMultipleBindings(String type, boolean enableCache) throws Exception {
+ Config config = Mockito.mock(Config.class);
+ Mockito.when(config.get(Configs.CACHE_ENABLED)).thenReturn(enableCache);
+ init(type, config);
+
+ AuditInfo auditInfo =
+ AuditInfo.builder().withCreator("creator").withCreateTime(Instant.now()).build();
+
+ try (EntityStore store = EntityStoreFactory.createEntityStore(config)) {
+ store.initialize(config);
+
+ BaseMetalake metalake =
+ createBaseMakeLake(RandomIdGenerator.INSTANCE.nextId(), "metalake", auditInfo);
+ store.put(metalake, false);
+
+ Namespace policyNamespace = NameIdentifierUtil.ofPolicy("metalake", "policy1").namespace();
+ PolicyEntity policy1 =
+ PolicyEntity.builder()
+ .withId(RandomIdGenerator.INSTANCE.nextId())
+ .withNamespace(policyNamespace)
+ .withName("policy1")
+ .withPolicyType(Policy.BuiltInType.CUSTOM)
+ .withContent(
+ PolicyContents.custom(
+ ImmutableMap.of("rule", "allow-all"),
+ Collections.singleton(MetadataObject.Type.CATALOG),
+ Collections.emptyMap()))
+ .withAuditInfo(auditInfo)
+ .build();
+
+ CatalogEntity catalog1 =
+ createCatalog(
+ RandomIdGenerator.INSTANCE.nextId(),
+ NamespaceUtil.ofCatalog("metalake"),
+ "catalog1",
+ auditInfo);
+
+ store.put(policy1, false);
+ store.put(catalog1, false);
+
+ SupportsRelationOperations relationOperations = (SupportsRelationOperations) store;
+
+ relationOperations.updateEntityRelations(
+ SupportsRelationOperations.Type.POLICY_METADATA_OBJECT_REL,
+ catalog1.nameIdentifier(),
+ Entity.EntityType.CATALOG,
+ new NameIdentifier[] {policy1.nameIdentifier()},
+ new NameIdentifier[] {});
+
+ List<PolicyEntity> policiesForCatalog1 =
+ relationOperations.listEntitiesByRelation(
+ SupportsRelationOperations.Type.POLICY_METADATA_OBJECT_REL,
+ catalog1.nameIdentifier(),
+ Entity.EntityType.CATALOG,
+ true);
+ Assertions.assertEquals(1, policiesForCatalog1.size());
+ Assertions.assertEquals(policy1, policiesForCatalog1.get(0));
+
+ List<GenericEntity> entitiesForPolicy1 =
+ relationOperations.listEntitiesByRelation(
+ SupportsRelationOperations.Type.POLICY_METADATA_OBJECT_REL,
+ policy1.nameIdentifier(),
+ Entity.EntityType.POLICY,
+ true);
+ Assertions.assertEquals(1, entitiesForPolicy1.size());
+ Assertions.assertEquals(catalog1.id(), entitiesForPolicy1.get(0).id());
+
+ CatalogEntity catalog2 =
+ createCatalog(
+ RandomIdGenerator.INSTANCE.nextId(),
+ NamespaceUtil.ofCatalog("metalake"),
+ "catalog2",
+ auditInfo);
+ store.put(catalog2, false);
+
+ relationOperations.updateEntityRelations(
+ SupportsRelationOperations.Type.POLICY_METADATA_OBJECT_REL,
+ catalog2.nameIdentifier(),
+ Entity.EntityType.CATALOG,
+ new NameIdentifier[] {policy1.nameIdentifier()},
+ new NameIdentifier[] {});
+
+ policiesForCatalog1 =
+ relationOperations.listEntitiesByRelation(
+ SupportsRelationOperations.Type.POLICY_METADATA_OBJECT_REL,
+ catalog1.nameIdentifier(),
+ Entity.EntityType.CATALOG,
+ true);
+ Assertions.assertEquals(1, policiesForCatalog1.size());
+ Assertions.assertEquals(policy1, policiesForCatalog1.get(0));
+
+ List<PolicyEntity> policiesForCatalog2 =
+ relationOperations.listEntitiesByRelation(
+ SupportsRelationOperations.Type.POLICY_METADATA_OBJECT_REL,
+ catalog2.nameIdentifier(),
+ Entity.EntityType.CATALOG,
+ true);
+ Assertions.assertEquals(1, policiesForCatalog2.size());
+ Assertions.assertEquals(policy1, policiesForCatalog2.get(0));
+
+ entitiesForPolicy1 =
+ relationOperations.listEntitiesByRelation(
+ SupportsRelationOperations.Type.POLICY_METADATA_OBJECT_REL,
+ policy1.nameIdentifier(),
+ Entity.EntityType.POLICY,
+ true);
+ Assertions.assertEquals(2, entitiesForPolicy1.size());
+ List<Long> entityIds =
+ entitiesForPolicy1.stream().map(GenericEntity::id).collect(Collectors.toList());
+ Assertions.assertTrue(entityIds.contains(catalog1.id()));
+ Assertions.assertTrue(entityIds.contains(catalog2.id()));
+ destroy(type);
+ }
+ }
+}