World's most powerful open data catalog for building a high-performance, geo-distributed and federated metadata lake.
Java ai-catalogdata-catalogdatalakefederated-querylakehousemetadatametalakemodel-catalogopendatacatalogskycomputingstratosphere
[#2851] feat(core): Add relational backend for Group Entity (#3031)
lwyangdfdd5530f616
diff --git a/core/src/main/java/com/datastrato/gravitino/storage/relational/JDBCBackend.java b/core/src/main/java/com/datastrato/gravitino/storage/relational/JDBCBackend.javaindex bcd56709e6..8329ae7e6d 100644--- a/core/src/main/java/com/datastrato/gravitino/storage/relational/JDBCBackend.java+++ b/core/src/main/java/com/datastrato/gravitino/storage/relational/JDBCBackend.java@@ -20,6 +20,7 @@ import com.datastrato.gravitino.exceptions.NoSuchEntityException;import com.datastrato.gravitino.meta.BaseMetalake;import com.datastrato.gravitino.meta.CatalogEntity;import com.datastrato.gravitino.meta.FilesetEntity;+import com.datastrato.gravitino.meta.GroupEntity;import com.datastrato.gravitino.meta.RoleEntity;import com.datastrato.gravitino.meta.SchemaEntity;import com.datastrato.gravitino.meta.TableEntity;@@ -28,6 +29,7 @@ import com.datastrato.gravitino.meta.UserEntity;import com.datastrato.gravitino.storage.relational.converters.SQLExceptionConverterFactory;import com.datastrato.gravitino.storage.relational.service.CatalogMetaService;import com.datastrato.gravitino.storage.relational.service.FilesetMetaService;+import com.datastrato.gravitino.storage.relational.service.GroupMetaService;import com.datastrato.gravitino.storage.relational.service.MetalakeMetaService;import com.datastrato.gravitino.storage.relational.service.RoleMetaService;import com.datastrato.gravitino.storage.relational.service.SchemaMetaService;@@ -109,6 +111,8 @@ public class JDBCBackend implements RelationalBackend {UserMetaService.getInstance().insertUser((UserEntity) e, overwritten);} else if (e instanceof RoleEntity) {RoleMetaService.getInstance().insertRole((RoleEntity) e, overwritten);+ } else if (e instanceof GroupEntity) {+ GroupMetaService.getInstance().insertGroup((GroupEntity) e, overwritten);} else {throw new UnsupportedEntityTypeException("Unsupported entity type: %s for insert operation", e.getClass());@@ -134,6 +138,8 @@ public class JDBCBackend implements RelationalBackend {return (E) TopicMetaService.getInstance().updateTopic(ident, updater);case USER:return (E) UserMetaService.getInstance().updateUser(ident, updater);+ case GROUP:+ return (E) GroupMetaService.getInstance().updateGroup(ident, updater);default:throw new UnsupportedEntityTypeException("Unsupported entity type: %s for update operation", entityType);@@ -158,6 +164,8 @@ public class JDBCBackend implements RelationalBackend {return (E) TopicMetaService.getInstance().getTopicByIdentifier(ident);case USER:return (E) UserMetaService.getInstance().getUserByIdentifier(ident);+ case GROUP:+ return (E) GroupMetaService.getInstance().getGroupByIdentifier(ident);default:throw new UnsupportedEntityTypeException("Unsupported entity type: %s for get operation", entityType);@@ -181,6 +189,8 @@ public class JDBCBackend implements RelationalBackend {return TopicMetaService.getInstance().deleteTopic(ident);case USER:return UserMetaService.getInstance().deleteUser(ident);+ case GROUP:+ return GroupMetaService.getInstance().deleteGroup(ident);default:throw new UnsupportedEntityTypeException("Unsupported entity type: %s for delete operation", entityType);diff --git a/core/src/main/java/com/datastrato/gravitino/storage/relational/mapper/GroupMetaMapper.java b/core/src/main/java/com/datastrato/gravitino/storage/relational/mapper/GroupMetaMapper.javanew file mode 100644index 0000000000..b488d1dbc2--- /dev/null+++ b/core/src/main/java/com/datastrato/gravitino/storage/relational/mapper/GroupMetaMapper.java@@ -0,0 +1,119 @@+/*+ * Copyright 2024 Datastrato Pvt Ltd.+ * This software is licensed under the Apache License version 2.+ */++package com.datastrato.gravitino.storage.relational.mapper;++import com.datastrato.gravitino.storage.relational.po.GroupPO;+import org.apache.ibatis.annotations.Insert;+import org.apache.ibatis.annotations.Param;+import org.apache.ibatis.annotations.Select;+import org.apache.ibatis.annotations.Update;++/**+ * A MyBatis Mapper for table meta operation SQLs.+ *+ * <p>This interface class is a specification defined by MyBatis. It requires this interface class+ * to identify the corresponding SQLs for execution. We can write SQLs in an additional XML file, or+ * write SQLs with annotations in this interface Mapper. See: <a+ * href="https://mybatis.org/mybatis-3/getting-started.html"></a>+ */+public interface GroupMetaMapper {+ String TABLE_NAME = "group_meta";++ @Select(+ "SELECT group_id as groupId FROM "+ + TABLE_NAME+ + " WHERE metalake_id = #{metalakeId} AND group_name = #{groupName}"+ + " AND deleted_at = 0")+ Long selectGroupIdBySchemaIdAndName(+ @Param("metalakeId") Long metalakeId, @Param("groupName") String name);++ @Select(+ "SELECT group_id as groupId, group_name as groupName,"+ + " metalake_id as metalakeId,"+ + " audit_info as auditInfo,"+ + " current_version as currentVersion, last_version as lastVersion,"+ + " deleted_at as deletedAt"+ + " FROM "+ + TABLE_NAME+ + " WHERE metalake_id = #{metalakeId} AND group_name = #{groupName}"+ + " AND deleted_at = 0")+ GroupPO selectGroupMetaByMetalakeIdAndName(+ @Param("metalakeId") Long metalakeId, @Param("groupName") String name);++ @Insert(+ "INSERT INTO "+ + TABLE_NAME+ + "(group_id, group_name,"+ + " metalake_id, audit_info,"+ + " current_version, last_version, deleted_at)"+ + " VALUES("+ + " #{groupMeta.groupId},"+ + " #{groupMeta.groupName},"+ + " #{groupMeta.metalakeId},"+ + " #{groupMeta.auditInfo},"+ + " #{groupMeta.currentVersion},"+ + " #{groupMeta.lastVersion},"+ + " #{groupMeta.deletedAt}"+ + " )")+ void insertGroupMeta(@Param("groupMeta") GroupPO groupPO);++ @Insert(+ "INSERT INTO "+ + TABLE_NAME+ + "(group_id, group_name,"+ + "metalake_id, audit_info,"+ + " current_version, last_version, deleted_at)"+ + " VALUES("+ + " #{groupMeta.groupId},"+ + " #{groupMeta.groupName},"+ + " #{groupMeta.metalakeId},"+ + " #{groupMeta.auditInfo},"+ + " #{groupMeta.currentVersion},"+ + " #{groupMeta.lastVersion},"+ + " #{groupMeta.deletedAt}"+ + " )"+ + " ON DUPLICATE KEY UPDATE"+ + " group_name = #{groupMeta.groupName},"+ + " metalake_id = #{groupMeta.metalakeId},"+ + " audit_info = #{groupMeta.auditInfo},"+ + " current_version = #{groupMeta.currentVersion},"+ + " last_version = #{groupMeta.lastVersion},"+ + " deleted_at = #{groupMeta.deletedAt}")+ void insertGroupMetaOnDuplicateKeyUpdate(@Param("groupMeta") GroupPO groupPO);++ @Update(+ "UPDATE "+ + TABLE_NAME+ + " SET deleted_at = UNIX_TIMESTAMP(CURRENT_TIMESTAMP(3)) * 1000.0"+ + " WHERE group_id = #{groupId} AND deleted_at = 0")+ void softDeleteGroupMetaByGroupId(@Param("groupId") Long groupId);++ @Update(+ "UPDATE "+ + TABLE_NAME+ + " SET deleted_at = UNIX_TIMESTAMP(CURRENT_TIMESTAMP(3)) * 1000.0"+ + " WHERE metalake_id = #{metalakeId} AND deleted_at = 0")+ void softDeleteGroupMetasByMetalakeId(@Param("metalakeId") Long metalakeId);++ @Update(+ "UPDATE "+ + TABLE_NAME+ + " SET group_name = #{newGroupMeta.groupName},"+ + " metalake_id = #{newGroupMeta.metalakeId},"+ + " audit_info = #{newGroupMeta.auditInfo},"+ + " current_version = #{newGroupMeta.currentVersion},"+ + " last_version = #{newGroupMeta.lastVersion},"+ + " deleted_at = #{newGroupMeta.deletedAt}"+ + " WHERE group_id = #{oldGroupMeta.groupId}"+ + " AND group_name = #{oldGroupMeta.groupName}"+ + " AND metalake_id = #{oldGroupMeta.metalakeId}"+ + " AND audit_info = #{oldGroupMeta.auditInfo}"+ + " AND current_version = #{oldGroupMeta.currentVersion}"+ + " AND last_version = #{oldGroupMeta.lastVersion}"+ + " AND deleted_at = 0")+ Integer updateGroupMeta(+ @Param("newGroupMeta") GroupPO newGroupPO, @Param("oldGroupMeta") GroupPO oldGroupPO);+}diff --git a/core/src/main/java/com/datastrato/gravitino/storage/relational/mapper/GroupRoleRelMapper.java b/core/src/main/java/com/datastrato/gravitino/storage/relational/mapper/GroupRoleRelMapper.javanew file mode 100644index 0000000000..05afc0e19b--- /dev/null+++ b/core/src/main/java/com/datastrato/gravitino/storage/relational/mapper/GroupRoleRelMapper.java@@ -0,0 +1,105 @@+/*+ * Copyright 2024 Datastrato Pvt Ltd.+ * This software is licensed under the Apache License version 2.+ */++package com.datastrato.gravitino.storage.relational.mapper;++import com.datastrato.gravitino.storage.relational.po.GroupRoleRelPO;+import java.util.List;+import org.apache.ibatis.annotations.Insert;+import org.apache.ibatis.annotations.Param;+import org.apache.ibatis.annotations.Update;++/**+ * A MyBatis Mapper for table meta operation SQLs.+ *+ * <p>This interface class is a specification defined by MyBatis. It requires this interface class+ * to identify the corresponding SQLs for execution. We can write SQLs in an additional XML file, or+ * write SQLs with annotations in this interface Mapper. See: <a+ * href="https://mybatis.org/mybatis-3/getting-started.html"></a>+ */+public interface GroupRoleRelMapper {+ String RELATION_TABLE_NAME = "group_role_rel";+ String GROUP_TABLE_NAME = "group_meta";++ @Insert({+ "<script>",+ "INSERT INTO "+ + RELATION_TABLE_NAME+ + "(group_id, role_id,"+ + " audit_info,"+ + " current_version, last_version, deleted_at)"+ + " VALUES ",+ "<foreach collection='groupRoleRels' item='item' separator=','>",+ "(#{item.groupId},"+ + " #{item.roleId},"+ + " #{item.auditInfo},"+ + " #{item.currentVersion},"+ + " #{item.lastVersion},"+ + " #{item.deletedAt})",+ "</foreach>",+ "</script>"+ })+ void batchInsertGroupRoleRel(@Param("groupRoleRels") List<GroupRoleRelPO> groupRoleRelPOS);++ @Insert({+ "<script>",+ "INSERT INTO "+ + RELATION_TABLE_NAME+ + "(group_id, role_id,"+ + " audit_info,"+ + " current_version, last_version, deleted_at)"+ + " VALUES ",+ "<foreach collection='groupRoleRels' item='item' separator=','>",+ "(#{item.groupId},"+ + " #{item.roleId},"+ + " #{item.auditInfo},"+ + " #{item.currentVersion},"+ + " #{item.lastVersion},"+ + " #{item.deletedAt})",+ "</foreach>",+ " ON DUPLICATE KEY UPDATE"+ + " group_id = VALUES(group_id),"+ + " role_id = VALUES(role_id),"+ + " audit_info = VALUES(audit_info),"+ + " current_version = VALUES(current_version),"+ + " last_version = VALUES(last_version),"+ + " deleted_at = VALUES(deleted_at)",+ "</script>"+ })+ void batchInsertGroupRoleRelOnDuplicateKeyUpdate(+ @Param("groupRoleRels") List<GroupRoleRelPO> groupRoleRelPOS);++ @Update(+ "UPDATE "+ + RELATION_TABLE_NAME+ + " SET deleted_at = UNIX_TIMESTAMP(CURRENT_TIMESTAMP(3)) * 1000.0"+ + " WHERE group_id = #{groupId} AND deleted_at = 0")+ void softDeleteGroupRoleRelByGroupId(@Param("groupId") Long groupId);++ @Update({+ "<script>",+ "UPDATE "+ + RELATION_TABLE_NAME+ + " SET deleted_at = UNIX_TIMESTAMP(CURRENT_TIMESTAMP(3)) * 1000.0"+ + " WHERE group_id = #{groupId} AND role_id in (",+ "<foreach collection='roleIds' item='roleId' separator=','>",+ "#{roleId}",+ "</foreach>",+ ") " + "AND deleted_at = 0",+ "</script>"+ })+ void softDeleteGroupRoleRelByGroupAndRoles(+ @Param("groupId") Long groupId, @Param("roleIds") List<Long> roleIds);++ @Update(+ "UPDATE "+ + RELATION_TABLE_NAME+ + " SET deleted_at = UNIX_TIMESTAMP(CURRENT_TIMESTAMP(3)) * 1000.0"+ + " WHERE group_id IN (SELECT group_id FROM "+ + GROUP_TABLE_NAME+ + " WHERE metalake_id = #{metalakeId} AND deleted_at = 0)"+ + " AND deleted_at = 0")+ void softDeleteGroupRoleRelByMetalakeId(Long metalakeId);+}diff --git a/core/src/main/java/com/datastrato/gravitino/storage/relational/mapper/RoleMetaMapper.java b/core/src/main/java/com/datastrato/gravitino/storage/relational/mapper/RoleMetaMapper.javaindex f9732d040e..91a8b8d7bd 100644--- a/core/src/main/java/com/datastrato/gravitino/storage/relational/mapper/RoleMetaMapper.java+++ b/core/src/main/java/com/datastrato/gravitino/storage/relational/mapper/RoleMetaMapper.java@@ -21,7 +21,8 @@ import org.apache.ibatis.annotations.Select;*/public interface RoleMetaMapper {String ROLE_TABLE_NAME = "role_meta";- String RELATION_TABLE_NAME = "user_role_rel";+ String USER_RELATION_TABLE_NAME = "user_role_rel";+ String GROUP_RELATION_TABLE_NAME = "group_role_rel";@Select("SELECT role_id as roleId FROM "@@ -40,12 +41,27 @@ public interface RoleMetaMapper {+ " FROM "+ ROLE_TABLE_NAME+ " ro JOIN "- + RELATION_TABLE_NAME+ + USER_RELATION_TABLE_NAME+ " re ON ro.role_id = re.role_id"+ " WHERE re.user_id = #{userId}"+ " AND ro.deleted_at = 0 AND re.deleted_at = 0")List<RolePO> listRolesByUserId(@Param("userId") Long userId);+ @Select(+ "SELECT ro.role_id as roleId, ro.role_name as roleName,"+ + " ro.metalake_id as metalakeId, ro.properties as properties,"+ + " ro.securable_object as securableObject, ro.privileges as privileges,"+ + " ro.audit_info as auditInfo, ro.current_version as currentVersion,"+ + " ro.last_version as lastVersion, ro.deleted_at as deletedAt"+ + " FROM "+ + ROLE_TABLE_NAME+ + " ro JOIN "+ + GROUP_RELATION_TABLE_NAME+ + " ge ON ro.role_id = ge.role_id"+ + " WHERE ge.group_id = #{groupId}"+ + " AND ro.deleted_at = 0 AND ge.deleted_at = 0")+ List<RolePO> listRolesByGroupId(Long groupId);+@Insert("INSERT INTO "+ ROLE_TABLE_NAMEdiff --git a/core/src/main/java/com/datastrato/gravitino/storage/relational/po/GroupPO.java b/core/src/main/java/com/datastrato/gravitino/storage/relational/po/GroupPO.javanew file mode 100644index 0000000000..33e0ab27c5--- /dev/null+++ b/core/src/main/java/com/datastrato/gravitino/storage/relational/po/GroupPO.java@@ -0,0 +1,143 @@+/*+ * Copyright 2024 Datastrato Pvt Ltd.+ * This software is licensed under the Apache License version 2.+ */+package com.datastrato.gravitino.storage.relational.po;++import com.google.common.base.Objects;+import com.google.common.base.Preconditions;++public class GroupPO {+ private Long groupId;+ private String groupName;+ private Long metalakeId;+ private String auditInfo;+ private Long currentVersion;+ private Long lastVersion;+ private Long deletedAt;++ public Long getGroupId() {+ return groupId;+ }++ public String getGroupName() {+ return groupName;+ }++ public Long getMetalakeId() {+ return metalakeId;+ }++ public String getAuditInfo() {+ return auditInfo;+ }++ public Long getCurrentVersion() {+ return currentVersion;+ }++ public Long getLastVersion() {+ return lastVersion;+ }++ public Long getDeletedAt() {+ return deletedAt;+ }++ @Override+ public boolean equals(Object o) {+ if (this == o) {+ return true;+ }+ if (!(o instanceof GroupPO)) {+ return false;+ }+ GroupPO tablePO = (GroupPO) o;+ return Objects.equal(getGroupId(), tablePO.getGroupId())+ && Objects.equal(getGroupName(), tablePO.getGroupName())+ && Objects.equal(getMetalakeId(), tablePO.getMetalakeId())+ && Objects.equal(getAuditInfo(), tablePO.getAuditInfo())+ && Objects.equal(getCurrentVersion(), tablePO.getCurrentVersion())+ && Objects.equal(getLastVersion(), tablePO.getLastVersion())+ && Objects.equal(getDeletedAt(), tablePO.getDeletedAt());+ }++ @Override+ public int hashCode() {+ return Objects.hashCode(+ getGroupId(),+ getGroupName(),+ getMetalakeId(),+ getAuditInfo(),+ getCurrentVersion(),+ getLastVersion(),+ getDeletedAt());+ }++ public static class Builder {+ private final GroupPO groupPO;++ private Builder() {+ groupPO = new GroupPO();+ }++ public Builder withGroupId(Long groupId) {+ groupPO.groupId = groupId;+ return this;+ }++ public Builder withGroupName(String groupName) {+ groupPO.groupName = groupName;+ return this;+ }++ public Builder withMetalakeId(Long metalakeId) {+ groupPO.metalakeId = metalakeId;+ return this;+ }++ public Builder withAuditInfo(String auditInfo) {+ groupPO.auditInfo = auditInfo;+ return this;+ }++ public Builder withCurrentVersion(Long currentVersion) {+ groupPO.currentVersion = currentVersion;+ return this;+ }++ public Builder withLastVersion(Long lastVersion) {+ groupPO.lastVersion = lastVersion;+ return this;+ }++ public Builder withDeletedAt(Long deletedAt) {+ groupPO.deletedAt = deletedAt;+ return this;+ }++ private void validate() {+ Preconditions.checkArgument(groupPO.groupId != null, "Group id is required");+ Preconditions.checkArgument(groupPO.groupName != null, "Group name is required");+ Preconditions.checkArgument(groupPO.metalakeId != null, "Metalake id is required");+ Preconditions.checkArgument(groupPO.auditInfo != null, "Audit info is required");+ Preconditions.checkArgument(groupPO.currentVersion != null, "Current version is required");+ Preconditions.checkArgument(groupPO.lastVersion != null, "Last version is required");+ Preconditions.checkArgument(groupPO.deletedAt != null, "Deleted at is required");+ }++ public GroupPO build() {+ validate();+ return groupPO;+ }+ }++ /**+ * Creates a new instance of {@link Builder}.+ *+ * @return The new instance.+ */+ public static Builder builder() {+ return new Builder();+ }+}diff --git a/core/src/main/java/com/datastrato/gravitino/storage/relational/po/GroupRoleRelPO.java b/core/src/main/java/com/datastrato/gravitino/storage/relational/po/GroupRoleRelPO.javanew file mode 100644index 0000000000..82ebac8a83--- /dev/null+++ b/core/src/main/java/com/datastrato/gravitino/storage/relational/po/GroupRoleRelPO.java@@ -0,0 +1,131 @@+/*+ * Copyright 2024 Datastrato Pvt Ltd.+ * This software is licensed under the Apache License version 2.+ */+package com.datastrato.gravitino.storage.relational.po;++import com.google.common.base.Objects;+import com.google.common.base.Preconditions;++public class GroupRoleRelPO {+ private Long groupId;+ private Long roleId;+ private String auditInfo;+ private Long currentVersion;+ private Long lastVersion;+ private Long deletedAt;++ public Long getGroupId() {+ return groupId;+ }++ public Long getRoleId() {+ return roleId;+ }++ public String getAuditInfo() {+ return auditInfo;+ }++ public Long getCurrentVersion() {+ return currentVersion;+ }++ public Long getLastVersion() {+ return lastVersion;+ }++ public Long getDeletedAt() {+ return deletedAt;+ }++ @Override+ public boolean equals(Object o) {+ if (this == o) {+ return true;+ }+ if (!(o instanceof GroupRoleRelPO)) {+ return false;+ }+ GroupRoleRelPO groupRoleRelPO = (GroupRoleRelPO) o;+ return Objects.equal(getGroupId(), groupRoleRelPO.getGroupId())+ && Objects.equal(getRoleId(), groupRoleRelPO.getRoleId())+ && Objects.equal(getAuditInfo(), groupRoleRelPO.getAuditInfo())+ && Objects.equal(getCurrentVersion(), groupRoleRelPO.getCurrentVersion())+ && Objects.equal(getLastVersion(), groupRoleRelPO.getLastVersion())+ && Objects.equal(getDeletedAt(), groupRoleRelPO.getDeletedAt());+ }++ @Override+ public int hashCode() {+ return Objects.hashCode(+ getGroupId(),+ getRoleId(),+ getAuditInfo(),+ getCurrentVersion(),+ getLastVersion(),+ getDeletedAt());+ }++ public static class Builder {+ private final GroupRoleRelPO groupRoleRelPO;++ private Builder() {+ groupRoleRelPO = new GroupRoleRelPO();+ }++ public Builder withGroupId(Long groupId) {+ groupRoleRelPO.groupId = groupId;+ return this;+ }++ public Builder withRoleId(Long roleId) {+ groupRoleRelPO.roleId = roleId;+ return this;+ }++ public Builder withAuditInfo(String auditInfo) {+ groupRoleRelPO.auditInfo = auditInfo;+ return this;+ }++ public Builder withCurrentVersion(Long currentVersion) {+ groupRoleRelPO.currentVersion = currentVersion;+ return this;+ }++ public Builder withLastVersion(Long lastVersion) {+ groupRoleRelPO.lastVersion = lastVersion;+ return this;+ }++ public Builder withDeletedAt(Long deletedAt) {+ groupRoleRelPO.deletedAt = deletedAt;+ return this;+ }++ private void validate() {+ Preconditions.checkArgument(groupRoleRelPO.groupId != null, "Group id is required");+ Preconditions.checkArgument(groupRoleRelPO.roleId != null, "Role id is required");+ Preconditions.checkArgument(groupRoleRelPO.auditInfo != null, "Audit info is required");+ Preconditions.checkArgument(+ groupRoleRelPO.currentVersion != null, "Current version is required");+ Preconditions.checkArgument(groupRoleRelPO.lastVersion != null, "Last version is required");+ Preconditions.checkArgument(groupRoleRelPO.deletedAt != null, "Deleted at is required");+ }++ public GroupRoleRelPO build() {+ validate();+ return groupRoleRelPO;+ }+ }++ /**+ * Creates a new instance of {@link Builder}.+ *+ * @return The new instance.+ */+ public static Builder builder() {+ return new Builder();+ }+}diff --git a/core/src/main/java/com/datastrato/gravitino/storage/relational/service/GroupMetaService.java b/core/src/main/java/com/datastrato/gravitino/storage/relational/service/GroupMetaService.javanew file mode 100644index 0000000000..d02001e9e7--- /dev/null+++ b/core/src/main/java/com/datastrato/gravitino/storage/relational/service/GroupMetaService.java@@ -0,0 +1,226 @@+/*+ * Copyright 2024 Datastrato Pvt Ltd.+ * This software is licensed under the Apache License version 2.+ */+package com.datastrato.gravitino.storage.relational.service;++import com.datastrato.gravitino.Entity;+import com.datastrato.gravitino.HasIdentifier;+import com.datastrato.gravitino.NameIdentifier;+import com.datastrato.gravitino.exceptions.NoSuchEntityException;+import com.datastrato.gravitino.meta.GroupEntity;+import com.datastrato.gravitino.storage.relational.mapper.GroupMetaMapper;+import com.datastrato.gravitino.storage.relational.mapper.GroupRoleRelMapper;+import com.datastrato.gravitino.storage.relational.po.GroupPO;+import com.datastrato.gravitino.storage.relational.po.GroupRoleRelPO;+import com.datastrato.gravitino.storage.relational.po.RolePO;+import com.datastrato.gravitino.storage.relational.utils.ExceptionUtils;+import com.datastrato.gravitino.storage.relational.utils.POConverters;+import com.datastrato.gravitino.storage.relational.utils.SessionUtils;+import com.google.common.base.Preconditions;+import com.google.common.collect.Lists;+import com.google.common.collect.Sets;+import java.util.List;+import java.util.Objects;+import java.util.Optional;+import java.util.Set;+import java.util.function.Function;++/** The service class for group metadata. It provides the basic database operations for group. */+public class GroupMetaService {+ private static final GroupMetaService INSTANCE = new GroupMetaService();++ public static GroupMetaService getInstance() {+ return INSTANCE;+ }++ private GroupMetaService() {}++ private GroupPO getGroupPOByMetalakeIdAndName(Long metalakeId, String groupName) {+ GroupPO GroupPO =+ SessionUtils.getWithoutCommit(+ GroupMetaMapper.class,+ mapper -> mapper.selectGroupMetaByMetalakeIdAndName(metalakeId, groupName));++ if (GroupPO == null) {+ throw new NoSuchEntityException(+ NoSuchEntityException.NO_SUCH_ENTITY_MESSAGE,+ Entity.EntityType.GROUP.name().toLowerCase(),+ groupName);+ }+ return GroupPO;+ }++ private Long getGroupIdByMetalakeIdAndName(Long metalakeId, String groupName) {+ Long groupId =+ SessionUtils.getWithoutCommit(+ GroupMetaMapper.class,+ mapper -> mapper.selectGroupIdBySchemaIdAndName(metalakeId, groupName));++ if (groupId == null) {+ throw new NoSuchEntityException(+ NoSuchEntityException.NO_SUCH_ENTITY_MESSAGE,+ Entity.EntityType.GROUP.name().toLowerCase(),+ groupName);+ }+ return groupId;+ }++ public GroupEntity getGroupByIdentifier(NameIdentifier identifier) {+ Preconditions.checkArgument(+ identifier != null+ && !identifier.namespace().isEmpty()+ && identifier.namespace().levels().length == 3,+ "The identifier should not be null and should have three level.");+ Long metalakeId =+ MetalakeMetaService.getInstance().getMetalakeIdByName(identifier.namespace().level(0));+ GroupPO groupPO = getGroupPOByMetalakeIdAndName(metalakeId, identifier.name());+ List<RolePO> rolePOs = RoleMetaService.getInstance().listRolesByGroupId(groupPO.getGroupId());++ return POConverters.fromGroupPO(groupPO, rolePOs, identifier.namespace());+ }++ public void insertGroup(GroupEntity GroupEntity, boolean overwritten) {+ try {+ Preconditions.checkArgument(+ GroupEntity.namespace() != null+ && !GroupEntity.namespace().isEmpty()+ && GroupEntity.namespace().levels().length == 3,+ "The identifier should not be null and should have three level.");++ Long metalakeId =+ MetalakeMetaService.getInstance().getMetalakeIdByName(GroupEntity.namespace().level(0));+ GroupPO.Builder builder = GroupPO.builder().withMetalakeId(metalakeId);+ GroupPO GroupPO = POConverters.initializeGroupPOWithVersion(GroupEntity, builder);++ List<Long> roleIds = Optional.ofNullable(GroupEntity.roleIds()).orElse(Lists.newArrayList());+ List<GroupRoleRelPO> groupRoleRelPOS =+ POConverters.initializeGroupRoleRelsPOWithVersion(GroupEntity, roleIds);++ SessionUtils.doMultipleWithCommit(+ () ->+ SessionUtils.doWithoutCommit(+ GroupMetaMapper.class,+ mapper -> {+ if (overwritten) {+ mapper.insertGroupMetaOnDuplicateKeyUpdate(GroupPO);+ } else {+ mapper.insertGroupMeta(GroupPO);+ }+ }),+ () -> {+ if (groupRoleRelPOS.isEmpty()) {+ return;+ }+ SessionUtils.doWithoutCommit(+ GroupRoleRelMapper.class,+ mapper -> {+ if (overwritten) {+ mapper.batchInsertGroupRoleRelOnDuplicateKeyUpdate(groupRoleRelPOS);+ } else {+ mapper.batchInsertGroupRoleRel(groupRoleRelPOS);+ }+ });+ });+ } catch (RuntimeException re) {+ ExceptionUtils.checkSQLException(+ re, Entity.EntityType.GROUP, GroupEntity.nameIdentifier().toString());+ throw re;+ }+ }++ public boolean deleteGroup(NameIdentifier identifier) {+ Preconditions.checkArgument(+ identifier != null+ && !identifier.namespace().isEmpty()+ && identifier.namespace().levels().length == 3,+ "The identifier should not be null and should have three level.");+ Long metalakeId =+ MetalakeMetaService.getInstance().getMetalakeIdByName(identifier.namespace().level(0));+ Long groupId = getGroupIdByMetalakeIdAndName(metalakeId, identifier.name());++ SessionUtils.doMultipleWithCommit(+ () ->+ SessionUtils.doWithoutCommit(+ GroupMetaMapper.class, mapper -> mapper.softDeleteGroupMetaByGroupId(groupId)),+ () ->+ SessionUtils.doWithoutCommit(+ GroupRoleRelMapper.class,+ mapper -> mapper.softDeleteGroupRoleRelByGroupId(groupId)));+ return true;+ }++ public <E extends Entity & HasIdentifier> GroupEntity updateGroup(+ NameIdentifier identifier, Function<E, E> updater) {+ Preconditions.checkArgument(+ identifier != null+ && !identifier.namespace().isEmpty()+ && identifier.namespace().levels().length == 3,+ "The identifier should not be null and should have three level.");++ Long metalakeId =+ MetalakeMetaService.getInstance().getMetalakeIdByName(identifier.namespace().level(0));+ GroupPO oldGroupPO = getGroupPOByMetalakeIdAndName(metalakeId, identifier.name());+ List<RolePO> rolePOs =+ RoleMetaService.getInstance().listRolesByGroupId(oldGroupPO.getGroupId());+ GroupEntity oldGroupEntity =+ POConverters.fromGroupPO(oldGroupPO, rolePOs, identifier.namespace());++ GroupEntity newEntity = (GroupEntity) updater.apply((E) oldGroupEntity);+ Preconditions.checkArgument(+ Objects.equals(oldGroupEntity.id(), newEntity.id()),+ "The updated group entity id: %s should be same with the group entity id before: %s",+ newEntity.id(),+ oldGroupEntity.id());++ Set<Long> oldRoleIds =+ oldGroupEntity.roleIds() == null+ ? Sets.newHashSet()+ : Sets.newHashSet(oldGroupEntity.roleIds());+ Set<Long> newRoleIds =+ newEntity.roleIds() == null ? Sets.newHashSet() : Sets.newHashSet(newEntity.roleIds());++ Set<Long> insertRoleIds = Sets.difference(newRoleIds, oldRoleIds);+ Set<Long> deleteRoleIds = Sets.difference(oldRoleIds, newRoleIds);++ if (insertRoleIds.isEmpty() && deleteRoleIds.isEmpty()) {+ return newEntity;+ }+ try {+ SessionUtils.doMultipleWithCommit(+ () ->+ SessionUtils.doWithoutCommit(+ GroupMetaMapper.class,+ mapper ->+ mapper.updateGroupMeta(+ POConverters.updateGroupPOWithVersion(oldGroupPO, newEntity),+ oldGroupPO)),+ () -> {+ if (insertRoleIds.isEmpty()) {+ return;+ }+ SessionUtils.doWithoutCommit(+ GroupRoleRelMapper.class,+ mapper ->+ mapper.batchInsertGroupRoleRel(+ POConverters.initializeGroupRoleRelsPOWithVersion(+ newEntity, Lists.newArrayList(insertRoleIds))));+ },+ () -> {+ if (deleteRoleIds.isEmpty()) {+ return;+ }+ SessionUtils.doWithoutCommit(+ GroupRoleRelMapper.class,+ mapper ->+ mapper.softDeleteGroupRoleRelByGroupAndRoles(+ newEntity.id(), Lists.newArrayList(deleteRoleIds)));+ });+ } catch (RuntimeException re) {+ ExceptionUtils.checkSQLException(+ re, Entity.EntityType.GROUP, newEntity.nameIdentifier().toString());+ throw re;+ }+ return newEntity;+ }+}diff --git a/core/src/main/java/com/datastrato/gravitino/storage/relational/service/MetalakeMetaService.java b/core/src/main/java/com/datastrato/gravitino/storage/relational/service/MetalakeMetaService.javaindex 066f417833..e32ad5a113 100644--- a/core/src/main/java/com/datastrato/gravitino/storage/relational/service/MetalakeMetaService.java+++ b/core/src/main/java/com/datastrato/gravitino/storage/relational/service/MetalakeMetaService.java@@ -16,6 +16,8 @@ import com.datastrato.gravitino.meta.CatalogEntity;import com.datastrato.gravitino.storage.relational.mapper.CatalogMetaMapper;import com.datastrato.gravitino.storage.relational.mapper.FilesetMetaMapper;import com.datastrato.gravitino.storage.relational.mapper.FilesetVersionMapper;+import com.datastrato.gravitino.storage.relational.mapper.GroupMetaMapper;+import com.datastrato.gravitino.storage.relational.mapper.GroupRoleRelMapper;import com.datastrato.gravitino.storage.relational.mapper.MetalakeMetaMapper;import com.datastrato.gravitino.storage.relational.mapper.SchemaMetaMapper;import com.datastrato.gravitino.storage.relational.mapper.TableMetaMapper;@@ -180,7 +182,15 @@ public class MetalakeMetaService {() ->SessionUtils.doWithoutCommit(UserMetaMapper.class,- mapper -> mapper.softDeleteUserMetasByMetalakeId(metalakeId)));+ mapper -> mapper.softDeleteUserMetasByMetalakeId(metalakeId)),+ () ->+ SessionUtils.doWithoutCommit(+ GroupRoleRelMapper.class,+ mapper -> mapper.softDeleteGroupRoleRelByMetalakeId(metalakeId)),+ () ->+ SessionUtils.doWithoutCommit(+ GroupMetaMapper.class,+ mapper -> mapper.softDeleteGroupMetasByMetalakeId(metalakeId)));} else {List<CatalogEntity> catalogEntities =CatalogMetaService.getInstance()@@ -201,7 +211,15 @@ public class MetalakeMetaService {() ->SessionUtils.doWithoutCommit(UserMetaMapper.class,- mapper -> mapper.softDeleteUserMetasByMetalakeId(metalakeId)));+ mapper -> mapper.softDeleteUserMetasByMetalakeId(metalakeId)),+ () ->+ SessionUtils.doWithoutCommit(+ GroupRoleRelMapper.class,+ mapper -> mapper.softDeleteGroupRoleRelByMetalakeId(metalakeId)),+ () ->+ SessionUtils.doWithoutCommit(+ GroupMetaMapper.class,+ mapper -> mapper.softDeleteGroupMetasByMetalakeId(metalakeId)));}}return true;diff --git a/core/src/main/java/com/datastrato/gravitino/storage/relational/service/RoleMetaService.java b/core/src/main/java/com/datastrato/gravitino/storage/relational/service/RoleMetaService.javaindex c0656628c9..5ec05d3f45 100644--- a/core/src/main/java/com/datastrato/gravitino/storage/relational/service/RoleMetaService.java+++ b/core/src/main/java/com/datastrato/gravitino/storage/relational/service/RoleMetaService.java@@ -45,6 +45,11 @@ public class RoleMetaService {RoleMetaMapper.class, mapper -> mapper.listRolesByUserId(userId));}+ public List<RolePO> listRolesByGroupId(Long groupId) {+ return SessionUtils.getWithoutCommit(+ RoleMetaMapper.class, mapper -> mapper.listRolesByGroupId(groupId));+ }+public void insertRole(RoleEntity roleEntity, boolean overwritten) {try {Preconditions.checkArgument(diff --git a/core/src/main/java/com/datastrato/gravitino/storage/relational/service/UserMetaService.java b/core/src/main/java/com/datastrato/gravitino/storage/relational/service/UserMetaService.javaindex cc1de84a19..5ebe1a7817 100644--- a/core/src/main/java/com/datastrato/gravitino/storage/relational/service/UserMetaService.java+++ b/core/src/main/java/com/datastrato/gravitino/storage/relational/service/UserMetaService.java@@ -25,7 +25,6 @@ import java.util.Objects;import java.util.Optional;import java.util.Set;import java.util.function.Function;-import java.util.stream.Collectors;/** The service class for user metadata. It provides the basic database operations for user. */public class UserMetaService {@@ -94,16 +93,7 @@ public class UserMetaService {UserPO.Builder builder = UserPO.builder().withMetalakeId(metalakeId);UserPO userPO = POConverters.initializeUserPOWithVersion(userEntity, builder);- List<Long> roleIds = userEntity.roleIds();- if (roleIds == null) {- roleIds =- Optional.ofNullable(userEntity.roleNames()).orElse(Lists.newArrayList()).stream()- .map(- roleName ->- RoleMetaService.getInstance()- .getRoleIdByMetalakeIdAndName(metalakeId, roleName))- .collect(Collectors.toList());- }+ List<Long> roleIds = Optional.ofNullable(userEntity.roleIds()).orElse(Lists.newArrayList());List<UserRoleRelPO> userRoleRelPOs =POConverters.initializeUserRoleRelsPOWithVersion(userEntity, roleIds);@@ -193,34 +183,41 @@ public class UserMetaService {if (insertRoleIds.isEmpty() && deleteRoleIds.isEmpty()) {return newEntity;}- SessionUtils.doMultipleWithCommit(- () ->++ try {+ SessionUtils.doMultipleWithCommit(+ () ->+ SessionUtils.doWithoutCommit(+ UserMetaMapper.class,+ mapper ->+ mapper.updateUserMeta(+ POConverters.updateUserPOWithVersion(oldUserPO, newEntity), oldUserPO)),+ () -> {+ if (insertRoleIds.isEmpty()) {+ return;+ }SessionUtils.doWithoutCommit(- UserMetaMapper.class,+ UserRoleRelMapper.class,mapper ->- mapper.updateUserMeta(- POConverters.updateUserPOWithVersion(oldUserPO, newEntity), oldUserPO)),- () -> {- if (insertRoleIds.isEmpty()) {- return;- }- SessionUtils.doWithoutCommit(- UserRoleRelMapper.class,- mapper ->- mapper.batchInsertUserRoleRel(- POConverters.initializeUserRoleRelsPOWithVersion(- newEntity, Lists.newArrayList(insertRoleIds))));- },- () -> {- if (deleteRoleIds.isEmpty()) {- return;- }- SessionUtils.doWithoutCommit(- UserRoleRelMapper.class,- mapper ->- mapper.softDeleteUserRoleRelByUserAndRoles(- newEntity.id(), Lists.newArrayList(deleteRoleIds)));- });+ mapper.batchInsertUserRoleRel(+ POConverters.initializeUserRoleRelsPOWithVersion(+ newEntity, Lists.newArrayList(insertRoleIds))));+ },+ () -> {+ if (deleteRoleIds.isEmpty()) {+ return;+ }+ SessionUtils.doWithoutCommit(+ UserRoleRelMapper.class,+ mapper ->+ mapper.softDeleteUserRoleRelByUserAndRoles(+ newEntity.id(), Lists.newArrayList(deleteRoleIds)));+ });+ } catch (RuntimeException re) {+ ExceptionUtils.checkSQLException(+ re, Entity.EntityType.USER, newEntity.nameIdentifier().toString());+ throw re;+ }return newEntity;}}diff --git a/core/src/main/java/com/datastrato/gravitino/storage/relational/session/SqlSessionFactoryHelper.java b/core/src/main/java/com/datastrato/gravitino/storage/relational/session/SqlSessionFactoryHelper.javaindex 485435e1e8..64526ebf35 100644--- a/core/src/main/java/com/datastrato/gravitino/storage/relational/session/SqlSessionFactoryHelper.java+++ b/core/src/main/java/com/datastrato/gravitino/storage/relational/session/SqlSessionFactoryHelper.java@@ -10,6 +10,8 @@ import com.datastrato.gravitino.Configs;import com.datastrato.gravitino.storage.relational.mapper.CatalogMetaMapper;import com.datastrato.gravitino.storage.relational.mapper.FilesetMetaMapper;import com.datastrato.gravitino.storage.relational.mapper.FilesetVersionMapper;+import com.datastrato.gravitino.storage.relational.mapper.GroupMetaMapper;+import com.datastrato.gravitino.storage.relational.mapper.GroupRoleRelMapper;import com.datastrato.gravitino.storage.relational.mapper.MetalakeMetaMapper;import com.datastrato.gravitino.storage.relational.mapper.RoleMetaMapper;import com.datastrato.gravitino.storage.relational.mapper.SchemaMetaMapper;@@ -92,6 +94,8 @@ public class SqlSessionFactoryHelper {configuration.addMapper(UserMetaMapper.class);configuration.addMapper(RoleMetaMapper.class);configuration.addMapper(UserRoleRelMapper.class);+ configuration.addMapper(GroupMetaMapper.class);+ configuration.addMapper(GroupRoleRelMapper.class);// Create the SqlSessionFactory object, it is a singleton objectif (sqlSessionFactory == null) {diff --git a/core/src/main/java/com/datastrato/gravitino/storage/relational/utils/POConverters.java b/core/src/main/java/com/datastrato/gravitino/storage/relational/utils/POConverters.javaindex 591131071b..5ab6245d58 100644--- a/core/src/main/java/com/datastrato/gravitino/storage/relational/utils/POConverters.java+++ b/core/src/main/java/com/datastrato/gravitino/storage/relational/utils/POConverters.java@@ -13,6 +13,7 @@ import com.datastrato.gravitino.meta.AuditInfo;import com.datastrato.gravitino.meta.BaseMetalake;import com.datastrato.gravitino.meta.CatalogEntity;import com.datastrato.gravitino.meta.FilesetEntity;+import com.datastrato.gravitino.meta.GroupEntity;import com.datastrato.gravitino.meta.RoleEntity;import com.datastrato.gravitino.meta.SchemaEntity;import com.datastrato.gravitino.meta.SchemaVersion;@@ -22,6 +23,8 @@ import com.datastrato.gravitino.meta.UserEntity;import com.datastrato.gravitino.storage.relational.po.CatalogPO;import com.datastrato.gravitino.storage.relational.po.FilesetPO;import com.datastrato.gravitino.storage.relational.po.FilesetVersionPO;+import com.datastrato.gravitino.storage.relational.po.GroupPO;+import com.datastrato.gravitino.storage.relational.po.GroupRoleRelPO;import com.datastrato.gravitino.storage.relational.po.MetalakePO;import com.datastrato.gravitino.storage.relational.po.RolePO;import com.datastrato.gravitino.storage.relational.po.SchemaPO;@@ -642,13 +645,14 @@ public class POConverters {/*** Update UserPO version*- * @param oldUserPO the old UserEntity object+ * @param oldUserPO the old UserPO object* @param newUser the new TableEntity object* @return UserPO object with updated version*/public static UserPO updateUserPOWithVersion(UserPO oldUserPO, UserEntity newUser) {Long lastVersion = oldUserPO.getLastVersion();- // Will set the version to the last version + 1 when having some fields need be multiple version+ // TODO: set the version to the last version + 1 when having some fields need be multiple+ // versionLong nextVersion = lastVersion;try {return UserPO.builder()@@ -678,15 +682,55 @@ public class POConverters {List<String> roleNames =rolePOs.stream().map(RolePO::getRoleName).collect(Collectors.toList());List<Long> roleIds = rolePOs.stream().map(RolePO::getRoleId).collect(Collectors.toList());- return UserEntity.builder()- .withId(userPO.getUserId())- .withName(userPO.getUserName())- .withRoleNames(roleNames.isEmpty() ? null : roleNames)- .withRoleIds(roleIds.isEmpty() ? null : roleIds)- .withNamespace(namespace)- .withAuditInfo(- JsonUtils.anyFieldMapper().readValue(userPO.getAuditInfo(), AuditInfo.class))- .build();++ UserEntity.Builder builder =+ UserEntity.builder()+ .withId(userPO.getUserId())+ .withName(userPO.getUserName())+ .withNamespace(namespace)+ .withAuditInfo(+ JsonUtils.anyFieldMapper().readValue(userPO.getAuditInfo(), AuditInfo.class));+ if (!roleNames.isEmpty()) {+ builder.withRoleNames(roleNames);+ }+ if (!roleIds.isEmpty()) {+ builder.withRoleIds(roleIds);+ }+ return builder.build();+ } catch (JsonProcessingException e) {+ throw new RuntimeException("Failed to deserialize json object:", e);+ }+ }++ /**+ * Convert {@link GroupPO} to {@link GroupEntity}+ *+ * @param groupPO GroupPO object to be converted+ * @param rolePOs list of rolePO+ * @param namespace Namespace object to be associated with the group+ * @return GroupEntity object from GroupPO object+ */+ public static GroupEntity fromGroupPO(+ GroupPO groupPO, List<RolePO> rolePOs, Namespace namespace) {+ try {+ List<String> roleNames =+ rolePOs.stream().map(RolePO::getRoleName).collect(Collectors.toList());+ List<Long> roleIds = rolePOs.stream().map(RolePO::getRoleId).collect(Collectors.toList());++ GroupEntity.Builder builder =+ GroupEntity.builder()+ .withId(groupPO.getGroupId())+ .withName(groupPO.getGroupName())+ .withNamespace(namespace)+ .withAuditInfo(+ JsonUtils.anyFieldMapper().readValue(groupPO.getAuditInfo(), AuditInfo.class));+ if (!roleNames.isEmpty()) {+ builder.withRoleNames(roleNames);+ }+ if (!roleIds.isEmpty()) {+ builder.withRoleIds(roleIds);+ }+ return builder.build();} catch (JsonProcessingException e) {throw new RuntimeException("Failed to deserialize json object:", e);}@@ -748,4 +792,83 @@ public class POConverters {throw new RuntimeException("Failed to serialize json object:", e);}}++ /**+ * Initialize GroupPO+ *+ * @param groupEntity GroupEntity object+ * @return GroupPO object with version initialized+ */+ public static GroupPO initializeGroupPOWithVersion(+ GroupEntity groupEntity, GroupPO.Builder builder) {+ try {+ return builder+ .withGroupId(groupEntity.id())+ .withGroupName(groupEntity.name())+ .withAuditInfo(JsonUtils.anyFieldMapper().writeValueAsString(groupEntity.auditInfo()))+ .withCurrentVersion(INIT_VERSION)+ .withLastVersion(INIT_VERSION)+ .withDeletedAt(DEFAULT_DELETED_AT)+ .build();+ } catch (JsonProcessingException e) {+ throw new RuntimeException("Failed to serialize json object:", e);+ }+ }++ /**+ * Update GroupPO version+ *+ * @param oldGroupPO the old GroupPO object+ * @param newGroup the new GroupEntity object+ * @return GroupPO object with updated version+ */+ public static GroupPO updateGroupPOWithVersion(GroupPO oldGroupPO, GroupEntity newGroup) {+ Long lastVersion = oldGroupPO.getLastVersion();+ // TODO: set the version to the last version + 1 when having some fields need be multiple+ // version+ Long nextVersion = lastVersion;+ try {+ return GroupPO.builder()+ .withGroupId(oldGroupPO.getGroupId())+ .withGroupName(newGroup.name())+ .withMetalakeId(oldGroupPO.getMetalakeId())+ .withAuditInfo(JsonUtils.anyFieldMapper().writeValueAsString(newGroup.auditInfo()))+ .withCurrentVersion(nextVersion)+ .withLastVersion(nextVersion)+ .withDeletedAt(DEFAULT_DELETED_AT)+ .build();+ } catch (JsonProcessingException e) {+ throw new RuntimeException("Failed to serialize json object:", e);+ }+ }++ /**+ * Initialize GroupRoleRelPO+ *+ * @param groupEntity GroupEntity object+ * @param roleIds list of role ids+ * @return GroupRoleRelPO object with version initialized+ */+ public static List<GroupRoleRelPO> initializeGroupRoleRelsPOWithVersion(+ GroupEntity groupEntity, List<Long> roleIds) {+ try {+ List<GroupRoleRelPO> groupRoleRelPOS = Lists.newArrayList();+ for (Long roleId : roleIds) {+ GroupRoleRelPO roleRelPO =+ GroupRoleRelPO.builder()+ .withGroupId(groupEntity.id())+ .withRoleId(roleId)+ .withAuditInfo(+ JsonUtils.anyFieldMapper().writeValueAsString(groupEntity.auditInfo()))+ .withCurrentVersion(INIT_VERSION)+ .withLastVersion(INIT_VERSION)+ .withDeletedAt(DEFAULT_DELETED_AT)+ .build();+ groupRoleRelPOS.add(roleRelPO);+ }+ return groupRoleRelPOS;+ } catch (JsonProcessingException e) {+ throw new RuntimeException("Failed to serialize json object:", e);+ }+ }}diff --git a/core/src/test/java/com/datastrato/gravitino/storage/TestEntityStorage.java b/core/src/test/java/com/datastrato/gravitino/storage/TestEntityStorage.javaindex 8ce48ab031..42fad98d86 100644--- a/core/src/test/java/com/datastrato/gravitino/storage/TestEntityStorage.java+++ b/core/src/test/java/com/datastrato/gravitino/storage/TestEntityStorage.java@@ -236,6 +236,8 @@ public class TestEntityStorage {auditInfo);UserEntity user1 =createUser(RandomIdGenerator.INSTANCE.nextId(), "metalake", "user1", auditInfo);+ GroupEntity group1 =+ createGroup(RandomIdGenerator.INSTANCE.nextId(), "metalake", "group1", auditInfo);// Store all entitiesstore.put(metalake);@@ -246,6 +248,7 @@ public class TestEntityStorage {store.put(fileset1);store.put(topic1);store.put(user1);+ store.put(group1);Assertions.assertDoesNotThrow(() ->@@ -288,6 +291,13 @@ public class TestEntityStorage {AuthorizationUtils.ofUser("metalake", "user1"),Entity.EntityType.USER,UserEntity.class));++ Assertions.assertDoesNotThrow(+ () ->+ store.get(+ AuthorizationUtils.ofGroup("metalake", "group1"),+ EntityType.GROUP,+ GroupEntity.class));}// It will automatically close the store we create before, then we reopen the entity store@@ -334,6 +344,12 @@ public class TestEntityStorage {AuthorizationUtils.ofUser("metalake", "user1"),Entity.EntityType.USER,UserEntity.class));+ Assertions.assertDoesNotThrow(+ () ->+ store.get(+ AuthorizationUtils.ofGroup("metalake", "group1"),+ Entity.EntityType.GROUP,+ GroupEntity.class));destroy(type);}}@@ -464,9 +480,9 @@ public class TestEntityStorage {store.put(oneUser);UserEntity anotherUser = createUser(2L, "metalake", "anotherUser", auditInfo);store.put(anotherUser);- GroupEntity oneGroup = createGroup("metalake", "oneGroup", auditInfo);+ GroupEntity oneGroup = createGroup(1L, "metalake", "oneGroup", auditInfo);store.put(oneGroup);- GroupEntity anotherGroup = createGroup("metalake", "anotherGroup", auditInfo);+ GroupEntity anotherGroup = createGroup(2L, "metalake", "anotherGroup", auditInfo);store.put(anotherGroup);RoleEntity oneRole = createRole("metalake", "oneRole", auditInfo);store.put(oneRole);@@ -533,6 +549,8 @@ public class TestEntityStorage {2L, Namespace.of("metalake", "catalog", "schema2"), "topic1", auditInfo);UserEntity user1 = createUser(1L, "metalake", "user1", auditInfo);UserEntity user2 = createUser(2L, "metalake", "user2", auditInfo);+ GroupEntity group1 = createGroup(1L, "metalake", "group1", auditInfo);+ GroupEntity group2 = createGroup(2L, "metalake", "group2", auditInfo);// Store all entitiesstore.put(metalake);@@ -548,6 +566,8 @@ public class TestEntityStorage {store.put(topic1InSchema2);store.put(user1);store.put(user2);+ store.put(group1);+ store.put(group2);validateAllEntityExist(metalake,@@ -563,10 +583,14 @@ public class TestEntityStorage {topic1,topic1InSchema2,user1,- user2);+ user2,+ group1,+ group2);validateDeleteUser(store, user1);+ validateDeleteGroup(store, group1);+validateDeleteTable(store, schema2, table1, table1InSchema2);validateDeleteFileset(store, schema2, fileset1, fileset1InSchema2);@@ -587,7 +611,7 @@ public class TestEntityStorage {topic1,topic1InSchema2);- validateDeleteMetalake(store, metalake, catalogCopy, user2);+ validateDeleteMetalake(store, metalake, catalogCopy, user2, group2);// Store all entities again// metalake@@ -675,6 +699,9 @@ public class TestEntityStorage {UserEntity userNew =createUser(RandomIdGenerator.INSTANCE.nextId(), "metalake", "userNew", auditInfo);store.put(userNew);+ GroupEntity groupNew =+ createGroup(RandomIdGenerator.INSTANCE.nextId(), "metalake", "groupNew", auditInfo);+ store.put(groupNew);validateDeleteTableCascade(store, table1New);@@ -686,7 +713,7 @@ public class TestEntityStorage {validateDeleteCatalogCascade(store, catalogNew, schema2New);- validateDeleteMetalakeCascade(store, metalakeNew, catalogNew, schema2New, userNew);+ validateDeleteMetalakeCascade(store, metalakeNew, catalogNew, schema2New, userNew, groupNew);destroy(type);}@@ -1188,31 +1215,28 @@ public class TestEntityStorage {private static UserEntity createUser(Long id, String metalake, String name, AuditInfo auditInfo) {return UserEntity.builder().withId(id)- .withNamespace(- Namespace.of(metalake, Entity.SYSTEM_CATALOG_RESERVED_NAME, Entity.USER_SCHEMA_NAME))+ .withNamespace(AuthorizationUtils.ofUserNamespace(metalake)).withName(name).withAuditInfo(auditInfo).withRoleNames(null).build();}- private static GroupEntity createGroup(String metalake, String name, AuditInfo auditInfo) {+ private static GroupEntity createGroup(+ Long id, String metalake, String name, AuditInfo auditInfo) {return GroupEntity.builder()- .withId(1L)- .withNamespace(- Namespace.of(metalake, Entity.SYSTEM_CATALOG_RESERVED_NAME, Entity.GROUP_SCHEMA_NAME))+ .withId(id)+ .withNamespace(AuthorizationUtils.ofGroupNamespace(metalake)).withName(name).withAuditInfo(auditInfo)- .withRoleNames(Lists.newArrayList())+ .withRoleNames(null).build();}private static RoleEntity createRole(String metalake, String name, AuditInfo auditInfo) {return RoleEntity.builder().withId(1L)- .withNamespace(- Namespace.of(- metalake, CatalogEntity.SYSTEM_CATALOG_RESERVED_NAME, Entity.ROLE_SCHEMA_NAME))+ .withNamespace(AuthorizationUtils.ofRoleNamespace(metalake)).withName(name).withAuditInfo(auditInfo).withSecurableObject(SecurableObjects.of("catalog"))@@ -1283,9 +1307,11 @@ public class TestEntityStorage {BaseMetalake metalake,CatalogEntity catalog,SchemaEntity schema2,- UserEntity userNew)+ UserEntity userNew,+ GroupEntity groupNew)throws IOException {Assertions.assertTrue(store.exists(userNew.nameIdentifier(), Entity.EntityType.USER));+ Assertions.assertTrue(store.exists(groupNew.nameIdentifier(), Entity.EntityType.GROUP));Assertions.assertTrue(store.delete(metalake.nameIdentifier(), Entity.EntityType.METALAKE, true));@@ -1295,6 +1321,7 @@ public class TestEntityStorage {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));}private void validateDeleteCatalogCascade(@@ -1373,10 +1400,15 @@ public class TestEntityStorage {}private static void validateDeleteMetalake(- EntityStore store, BaseMetalake metalake, CatalogEntity catalogCopy, UserEntity user2)+ EntityStore store,+ BaseMetalake metalake,+ CatalogEntity catalogCopy,+ UserEntity user2,+ GroupEntity group2)throws IOException {// Now delete catalog 'catalogCopy' and metalakeAssertions.assertTrue(store.exists(user2.nameIdentifier(), Entity.EntityType.USER));+ Assertions.assertTrue(store.exists(group2.nameIdentifier(), Entity.EntityType.GROUP));Assertions.assertThrowsExactly(NonEmptyEntityException.class,@@ -1387,6 +1419,7 @@ public class TestEntityStorage {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));}private static void validateDeleteCatalog(@@ -1508,6 +1541,16 @@ public class TestEntityStorage {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));++ GroupEntity group =+ createGroup(RandomIdGenerator.INSTANCE.nextId(), "metalake", "group1", group1.auditInfo());+ store.put(group);+ Assertions.assertTrue(store.exists(group.nameIdentifier(), EntityType.GROUP));+ }+private void validateDeleteTable(EntityStore store, SchemaEntity schema2, TableEntity table1, TableEntity table1InSchema2)throws IOException {@@ -1546,7 +1589,9 @@ public class TestEntityStorage {TopicEntity topic1,TopicEntity topic1InSchema2,UserEntity user1,- UserEntity user2)+ UserEntity user2,+ GroupEntity group1,+ GroupEntity group2)throws IOException {// Now try to getAssertions.assertEquals(@@ -1583,6 +1628,10 @@ public class TestEntityStorage {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));}private void validateDeletedFileset(EntityStore store) throws IOException {diff --git a/core/src/test/java/com/datastrato/gravitino/storage/memory/TestMemoryEntityStore.java b/core/src/test/java/com/datastrato/gravitino/storage/memory/TestMemoryEntityStore.javaindex 9ec63ed5c0..7b55ebe8a9 100644--- a/core/src/test/java/com/datastrato/gravitino/storage/memory/TestMemoryEntityStore.java+++ b/core/src/test/java/com/datastrato/gravitino/storage/memory/TestMemoryEntityStore.java@@ -21,6 +21,7 @@ import com.datastrato.gravitino.meta.AuditInfo;import com.datastrato.gravitino.meta.BaseMetalake;import com.datastrato.gravitino.meta.CatalogEntity;import com.datastrato.gravitino.meta.FilesetEntity;+import com.datastrato.gravitino.meta.GroupEntity;import com.datastrato.gravitino.meta.SchemaEntity;import com.datastrato.gravitino.meta.SchemaVersion;import com.datastrato.gravitino.meta.TableEntity;@@ -207,6 +208,15 @@ public class TestMemoryEntityStore {.withRoleNames(null).build();+ GroupEntity groupEntity =+ GroupEntity.builder()+ .withId(1L)+ .withName("group")+ .withNamespace(Namespace.of("metalake", "catalog", "db"))+ .withAuditInfo(auditInfo)+ .withRoleNames(null)+ .build();+InMemoryEntityStore store = new InMemoryEntityStore();store.initialize(Mockito.mock(Config.class));store.setSerDe(Mockito.mock(EntitySerDe.class));@@ -217,6 +227,7 @@ public class TestMemoryEntityStore {store.put(tableEntity);store.put(filesetEntity);store.put(userEntity);+ store.put(groupEntity);Metalake retrievedMetalake =store.get(metalake.nameIdentifier(), EntityType.METALAKE, BaseMetalake.class);@@ -242,6 +253,10 @@ public class TestMemoryEntityStore {store.get(userEntity.nameIdentifier(), EntityType.USER, UserEntity.class);Assertions.assertEquals(userEntity, retrievedUser);+ GroupEntity retrievedGroup =+ store.get(groupEntity.nameIdentifier(), EntityType.GROUP, GroupEntity.class);+ Assertions.assertEquals(groupEntity, retrievedGroup);+store.delete(metalake.nameIdentifier(), EntityType.METALAKE);NameIdentifier id = metalake.nameIdentifier();Assertions.assertThrows(diff --git a/core/src/test/java/com/datastrato/gravitino/storage/relational/TestJDBCBackend.java b/core/src/test/java/com/datastrato/gravitino/storage/relational/TestJDBCBackend.javaindex 569429829c..5c6c4aa2e5 100644--- a/core/src/test/java/com/datastrato/gravitino/storage/relational/TestJDBCBackend.java+++ b/core/src/test/java/com/datastrato/gravitino/storage/relational/TestJDBCBackend.java@@ -31,6 +31,7 @@ import com.datastrato.gravitino.meta.AuditInfo;import com.datastrato.gravitino.meta.BaseMetalake;import com.datastrato.gravitino.meta.CatalogEntity;import com.datastrato.gravitino.meta.FilesetEntity;+import com.datastrato.gravitino.meta.GroupEntity;import com.datastrato.gravitino.meta.RoleEntity;import com.datastrato.gravitino.meta.SchemaEntity;import com.datastrato.gravitino.meta.SchemaVersion;@@ -745,13 +746,18 @@ public class TestJDBCBackend {}public static UserEntity createUserEntity(- Long id, Namespace namespace, String name, AuditInfo auditInfo, List<String> roleNames) {+ Long id,+ Namespace namespace,+ String name,+ AuditInfo auditInfo,+ List<String> roleNames,+ List<Long> roleIds) {return UserEntity.builder().withId(id).withName(name).withNamespace(namespace).withRoleNames(roleNames)- .withRoleIds(null)+ .withRoleIds(roleIds).withAuditInfo(auditInfo).build();}@@ -768,4 +774,33 @@ public class TestJDBCBackend {.withPrivileges(Lists.newArrayList(Privileges.fromName(Privilege.Name.LOAD_CATALOG))).build();}++ public static GroupEntity createGroupEntity(+ Long id, Namespace namespace, String name, AuditInfo auditInfo) {+ return GroupEntity.builder()+ .withId(id)+ .withName(name)+ .withNamespace(namespace)+ .withRoleNames(null)+ .withRoleIds(null)+ .withAuditInfo(auditInfo)+ .build();+ }++ public static GroupEntity createGroupEntity(+ Long id,+ Namespace namespace,+ String name,+ AuditInfo auditInfo,+ List<String> roleNames,+ List<Long> roleIds) {+ return GroupEntity.builder()+ .withId(id)+ .withName(name)+ .withNamespace(namespace)+ .withRoleNames(roleNames)+ .withRoleIds(roleIds)+ .withAuditInfo(auditInfo)+ .build();+ }}diff --git a/core/src/test/java/com/datastrato/gravitino/storage/relational/service/TestGroupMetaService.java b/core/src/test/java/com/datastrato/gravitino/storage/relational/service/TestGroupMetaService.javanew file mode 100644index 0000000000..4adef8db3e--- /dev/null+++ b/core/src/test/java/com/datastrato/gravitino/storage/relational/service/TestGroupMetaService.java@@ -0,0 +1,614 @@+/*+ * Copyright 2024 Datastrato Pvt Ltd.+ * This software is licensed under the Apache License version 2.+ */++package com.datastrato.gravitino.storage.relational.service;++import com.datastrato.gravitino.authorization.AuthorizationUtils;+import com.datastrato.gravitino.exceptions.AlreadyExistsException;+import com.datastrato.gravitino.exceptions.NoSuchEntityException;+import com.datastrato.gravitino.meta.AuditInfo;+import com.datastrato.gravitino.meta.BaseMetalake;+import com.datastrato.gravitino.meta.GroupEntity;+import com.datastrato.gravitino.meta.RoleEntity;+import com.datastrato.gravitino.storage.RandomIdGenerator;+import com.datastrato.gravitino.storage.relational.TestJDBCBackend;+import com.datastrato.gravitino.storage.relational.mapper.RoleMetaMapper;+import com.datastrato.gravitino.storage.relational.po.RolePO;+import com.datastrato.gravitino.storage.relational.utils.SessionUtils;+import com.google.common.collect.Lists;+import com.google.common.collect.Sets;+import java.time.Instant;+import java.util.List;+import java.util.function.Function;+import org.junit.jupiter.api.Assertions;+import org.junit.jupiter.api.Test;++class TestGroupMetaService extends TestJDBCBackend {++ String metalakeName = "metalake";++ @Test+ void getGroupByIdentifier() {+ AuditInfo auditInfo =+ AuditInfo.builder().withCreator("creator").withCreateTime(Instant.now()).build();++ BaseMetalake metalake =+ createBaseMakeLake(RandomIdGenerator.INSTANCE.nextId(), metalakeName, auditInfo);+ backend.insert(metalake, false);++ GroupMetaService groupMetaService = GroupMetaService.getInstance();+ RoleMetaService roleMetaService = RoleMetaService.getInstance();++ // get not exist group+ Assertions.assertThrows(+ NoSuchEntityException.class,+ () ->+ groupMetaService.getGroupByIdentifier(+ AuthorizationUtils.ofGroup(metalakeName, "group1")));++ // get group+ GroupEntity group1 =+ createGroupEntity(+ RandomIdGenerator.INSTANCE.nextId(),+ AuthorizationUtils.ofGroupNamespace(metalakeName),+ "group1",+ auditInfo);+ groupMetaService.insertGroup(group1, false);+ Assertions.assertEquals(group1, groupMetaService.getGroupByIdentifier(group1.nameIdentifier()));++ // get group with roles+ RoleEntity role1 =+ createRoleEntity(+ RandomIdGenerator.INSTANCE.nextId(),+ AuthorizationUtils.ofRoleNamespace(metalakeName),+ "role1",+ auditInfo);+ RoleEntity role2 =+ createRoleEntity(+ RandomIdGenerator.INSTANCE.nextId(),+ AuthorizationUtils.ofRoleNamespace(metalakeName),+ "role2",+ auditInfo);+ roleMetaService.insertRole(role1, false);+ roleMetaService.insertRole(role2, false);+ GroupEntity group2 =+ createGroupEntity(+ RandomIdGenerator.INSTANCE.nextId(),+ AuthorizationUtils.ofGroupNamespace(metalakeName),+ "group2",+ auditInfo,+ Lists.newArrayList(role1.name(), role2.name()),+ Lists.newArrayList(role1.id(), role2.id()));+ groupMetaService.insertGroup(group2, false);+ GroupEntity actualGroup = groupMetaService.getGroupByIdentifier(group2.nameIdentifier());+ Assertions.assertEquals(group2.name(), actualGroup.name());+ Assertions.assertEquals(+ Sets.newHashSet(group2.roleNames()), Sets.newHashSet(actualGroup.roleNames()));+ }++ @Test+ void insertGroup() {+ AuditInfo auditInfo =+ AuditInfo.builder().withCreator("creator").withCreateTime(Instant.now()).build();+ BaseMetalake metalake =+ createBaseMakeLake(RandomIdGenerator.INSTANCE.nextId(), metalakeName, auditInfo);+ backend.insert(metalake, false);++ GroupMetaService groupMetaService = GroupMetaService.getInstance();+ RoleMetaService roleMetaService = RoleMetaService.getInstance();++ // insert group+ GroupEntity group1 =+ createGroupEntity(+ RandomIdGenerator.INSTANCE.nextId(),+ AuthorizationUtils.ofGroupNamespace(metalakeName),+ "group1",+ auditInfo);+ Assertions.assertThrows(+ NoSuchEntityException.class,+ () -> groupMetaService.getGroupByIdentifier(group1.nameIdentifier()));+ Assertions.assertDoesNotThrow(() -> groupMetaService.insertGroup(group1, false));+ Assertions.assertEquals(group1, groupMetaService.getGroupByIdentifier(group1.nameIdentifier()));++ // insert duplicate group+ GroupEntity group1Exist =+ createGroupEntity(+ RandomIdGenerator.INSTANCE.nextId(),+ AuthorizationUtils.ofGroupNamespace(metalakeName),+ "group1",+ auditInfo);+ Assertions.assertThrows(+ AlreadyExistsException.class, () -> groupMetaService.insertGroup(group1Exist, false));++ // insert overwrite+ GroupEntity group1Overwrite =+ createGroupEntity(+ group1.id(),+ AuthorizationUtils.ofGroupNamespace(metalakeName),+ "group1Overwrite",+ auditInfo);+ Assertions.assertDoesNotThrow(() -> groupMetaService.insertGroup(group1Overwrite, true));+ Assertions.assertEquals(+ "group1Overwrite",+ groupMetaService.getGroupByIdentifier(group1Overwrite.nameIdentifier()).name());+ Assertions.assertEquals(+ group1Overwrite, groupMetaService.getGroupByIdentifier(group1Overwrite.nameIdentifier()));++ // insert group with roles+ RoleEntity role1 =+ createRoleEntity(+ RandomIdGenerator.INSTANCE.nextId(),+ AuthorizationUtils.ofRoleNamespace(metalakeName),+ "role1",+ auditInfo);+ RoleEntity role2 =+ createRoleEntity(+ RandomIdGenerator.INSTANCE.nextId(),+ AuthorizationUtils.ofRoleNamespace(metalakeName),+ "role2",+ auditInfo);+ roleMetaService.insertRole(role1, false);+ roleMetaService.insertRole(role2, false);+ GroupEntity group2 =+ createGroupEntity(+ RandomIdGenerator.INSTANCE.nextId(),+ AuthorizationUtils.ofGroupNamespace(metalakeName),+ "group2",+ auditInfo,+ Lists.newArrayList(role1.name(), role2.name()),+ Lists.newArrayList(role1.id(), role2.id()));+ Assertions.assertDoesNotThrow(() -> groupMetaService.insertGroup(group2, false));+ GroupEntity actualGroup = groupMetaService.getGroupByIdentifier(group2.nameIdentifier());+ Assertions.assertEquals(group2.name(), actualGroup.name());+ Assertions.assertEquals(+ Sets.newHashSet(group2.roleNames()), Sets.newHashSet(actualGroup.roleNames()));++ // insert duplicate group with roles+ GroupEntity group2Exist =+ createGroupEntity(+ RandomIdGenerator.INSTANCE.nextId(),+ AuthorizationUtils.ofGroupNamespace(metalakeName),+ "group2",+ auditInfo);+ Assertions.assertThrows(+ AlreadyExistsException.class, () -> groupMetaService.insertGroup(group2Exist, false));++ // insert overwrite group with roles+ GroupEntity group2Overwrite =+ createGroupEntity(+ group1.id(),+ AuthorizationUtils.ofGroupNamespace(metalakeName),+ "group2Overwrite",+ auditInfo);+ Assertions.assertDoesNotThrow(() -> groupMetaService.insertGroup(group2Overwrite, true));+ Assertions.assertEquals(+ "group2Overwrite",+ groupMetaService.getGroupByIdentifier(group2Overwrite.nameIdentifier()).name());+ Assertions.assertEquals(+ group2Overwrite, groupMetaService.getGroupByIdentifier(group2Overwrite.nameIdentifier()));+ }++ @Test+ void deleteGroup() {+ AuditInfo auditInfo =+ AuditInfo.builder().withCreator("creator").withCreateTime(Instant.now()).build();+ BaseMetalake metalake =+ createBaseMakeLake(RandomIdGenerator.INSTANCE.nextId(), metalakeName, auditInfo);+ backend.insert(metalake, false);++ GroupMetaService groupMetaService = GroupMetaService.getInstance();+ RoleMetaService roleMetaService = RoleMetaService.getInstance();++ // delete group+ GroupEntity group1 =+ createGroupEntity(+ RandomIdGenerator.INSTANCE.nextId(),+ AuthorizationUtils.ofGroupNamespace(metalakeName),+ "group1",+ auditInfo);+ Assertions.assertThrows(+ NoSuchEntityException.class,+ () -> groupMetaService.getGroupByIdentifier(group1.nameIdentifier()));+ Assertions.assertDoesNotThrow(() -> groupMetaService.insertGroup(group1, false));+ Assertions.assertEquals(group1, groupMetaService.getGroupByIdentifier(group1.nameIdentifier()));+ Assertions.assertTrue(groupMetaService.deleteGroup(group1.nameIdentifier()));+ Assertions.assertThrows(+ NoSuchEntityException.class,+ () -> groupMetaService.getGroupByIdentifier(group1.nameIdentifier()));++ // delete group with roles+ RoleEntity role1 =+ createRoleEntity(+ RandomIdGenerator.INSTANCE.nextId(),+ AuthorizationUtils.ofRoleNamespace(metalakeName),+ "role1",+ auditInfo);+ RoleEntity role2 =+ createRoleEntity(+ RandomIdGenerator.INSTANCE.nextId(),+ AuthorizationUtils.ofRoleNamespace(metalakeName),+ "role2",+ auditInfo);+ roleMetaService.insertRole(role1, false);+ roleMetaService.insertRole(role2, false);+ GroupEntity group2 =+ createGroupEntity(+ RandomIdGenerator.INSTANCE.nextId(),+ AuthorizationUtils.ofGroupNamespace(metalakeName),+ "group2",+ auditInfo,+ Lists.newArrayList(role1.name(), role2.name()),+ Lists.newArrayList(role1.id(), role2.id()));+ groupMetaService.insertGroup(group2, false);+ List<RolePO> rolePOs =+ SessionUtils.doWithCommitAndFetchResult(+ RoleMetaMapper.class, mapper -> mapper.listRolesByGroupId(group2.id()));+ Assertions.assertEquals(2, rolePOs.size());+ GroupEntity actualGroup = groupMetaService.getGroupByIdentifier(group2.nameIdentifier());+ Assertions.assertEquals(group2.name(), actualGroup.name());+ Assertions.assertEquals(+ Sets.newHashSet(group2.roleNames()), Sets.newHashSet(actualGroup.roleNames()));++ Assertions.assertTrue(groupMetaService.deleteGroup(group2.nameIdentifier()));+ Assertions.assertThrows(+ NoSuchEntityException.class,+ () -> groupMetaService.getGroupByIdentifier(group2.nameIdentifier()));+ rolePOs =+ SessionUtils.doWithCommitAndFetchResult(+ RoleMetaMapper.class, mapper -> mapper.listRolesByGroupId(group2.id()));+ Assertions.assertEquals(0, rolePOs.size());+ }++ @Test+ void updateGroup() {+ AuditInfo auditInfo =+ AuditInfo.builder().withCreator("creator").withCreateTime(Instant.now()).build();+ BaseMetalake metalake =+ createBaseMakeLake(RandomIdGenerator.INSTANCE.nextId(), metalakeName, auditInfo);+ backend.insert(metalake, false);++ GroupMetaService groupMetaService = GroupMetaService.getInstance();+ RoleMetaService roleMetaService = RoleMetaService.getInstance();++ RoleEntity role1 =+ createRoleEntity(+ RandomIdGenerator.INSTANCE.nextId(),+ AuthorizationUtils.ofRoleNamespace(metalakeName),+ "role1",+ auditInfo);+ RoleEntity role2 =+ createRoleEntity(+ RandomIdGenerator.INSTANCE.nextId(),+ AuthorizationUtils.ofRoleNamespace(metalakeName),+ "role2",+ auditInfo);+ roleMetaService.insertRole(role1, false);+ roleMetaService.insertRole(role2, false);+ GroupEntity group1 =+ createGroupEntity(+ RandomIdGenerator.INSTANCE.nextId(),+ AuthorizationUtils.ofGroupNamespace(metalakeName),+ "group1",+ auditInfo,+ Lists.newArrayList(role1.name(), role2.name()),+ Lists.newArrayList(role1.id(), role2.id()));+ groupMetaService.insertGroup(group1, false);+ GroupEntity actualGroup = groupMetaService.getGroupByIdentifier(group1.nameIdentifier());+ Assertions.assertEquals(group1.name(), actualGroup.name());+ Assertions.assertEquals(+ Sets.newHashSet(group1.roleNames()), Sets.newHashSet(actualGroup.roleNames()));++ RoleEntity role3 =+ createRoleEntity(+ RandomIdGenerator.INSTANCE.nextId(),+ AuthorizationUtils.ofRoleNamespace(metalakeName),+ "role3",+ auditInfo);+ roleMetaService.insertRole(role3, false);++ // update group (grant)+ Function<GroupEntity, GroupEntity> grantUpdater =+ group -> {+ AuditInfo updateAuditInfo =+ AuditInfo.builder()+ .withCreator(group.auditInfo().creator())+ .withCreateTime(group.auditInfo().createTime())+ .withLastModifier("grantGroup")+ .withLastModifiedTime(Instant.now())+ .build();++ List<String> roleNames = Lists.newArrayList(group.roleNames());+ List<Long> roleIds = Lists.newArrayList(group.roleIds());+ roleNames.add(role3.name());+ roleIds.add(role3.id());++ return GroupEntity.builder()+ .withNamespace(group.namespace())+ .withId(group.id())+ .withName(group.name())+ .withRoleNames(roleNames)+ .withRoleIds(roleIds)+ .withAuditInfo(updateAuditInfo)+ .build();+ };++ Assertions.assertNotNull(groupMetaService.updateGroup(group1.nameIdentifier(), grantUpdater));+ GroupEntity grantGroup =+ GroupMetaService.getInstance().getGroupByIdentifier(group1.nameIdentifier());+ Assertions.assertEquals(group1.id(), grantGroup.id());+ Assertions.assertEquals(group1.name(), grantGroup.name());+ Assertions.assertEquals(+ Sets.newHashSet("role1", "role2", "role3"), Sets.newHashSet(grantGroup.roleNames()));+ Assertions.assertEquals(+ Sets.newHashSet(role1.id(), role2.id(), role3.id()), Sets.newHashSet(grantGroup.roleIds()));+ Assertions.assertEquals("creator", grantGroup.auditInfo().creator());+ Assertions.assertEquals("grantGroup", grantGroup.auditInfo().lastModifier());++ // update group (revoke)+ Function<GroupEntity, GroupEntity> revokeUpdater =+ group -> {+ AuditInfo updateAuditInfo =+ AuditInfo.builder()+ .withCreator(group.auditInfo().creator())+ .withCreateTime(group.auditInfo().createTime())+ .withLastModifier("revokeGroup")+ .withLastModifiedTime(Instant.now())+ .build();++ List<String> roleNames = Lists.newArrayList(group.roleNames());+ List<Long> roleIds = Lists.newArrayList(group.roleIds());+ roleIds.remove(roleNames.indexOf("role2"));+ roleNames.remove("role2");++ return GroupEntity.builder()+ .withNamespace(group.namespace())+ .withId(group.id())+ .withName(group.name())+ .withRoleNames(roleNames)+ .withRoleIds(roleIds)+ .withAuditInfo(updateAuditInfo)+ .build();+ };++ Assertions.assertNotNull(groupMetaService.updateGroup(group1.nameIdentifier(), revokeUpdater));+ GroupEntity revokeGroup =+ GroupMetaService.getInstance().getGroupByIdentifier(group1.nameIdentifier());+ Assertions.assertEquals(group1.id(), revokeGroup.id());+ Assertions.assertEquals(group1.name(), revokeGroup.name());+ Assertions.assertEquals(+ Sets.newHashSet("role1", "role3"), Sets.newHashSet(revokeGroup.roleNames()));+ Assertions.assertEquals(+ Sets.newHashSet(role1.id(), role3.id()), Sets.newHashSet(revokeGroup.roleIds()));+ Assertions.assertEquals("creator", revokeGroup.auditInfo().creator());+ Assertions.assertEquals("revokeGroup", revokeGroup.auditInfo().lastModifier());++ RoleEntity role4 =+ createRoleEntity(+ RandomIdGenerator.INSTANCE.nextId(),+ AuthorizationUtils.ofRoleNamespace(metalakeName),+ "role4",+ auditInfo);+ roleMetaService.insertRole(role4, false);++ // update group (grant & revoke)+ Function<GroupEntity, GroupEntity> grantRevokeUpdater =+ group -> {+ AuditInfo updateAuditInfo =+ AuditInfo.builder()+ .withCreator(group.auditInfo().creator())+ .withCreateTime(group.auditInfo().createTime())+ .withLastModifier("grantRevokeUser")+ .withLastModifiedTime(Instant.now())+ .build();++ List<String> roleNames = Lists.newArrayList(group.roleNames());+ List<Long> roleIds = Lists.newArrayList(group.roleIds());+ roleIds.remove(roleNames.indexOf("role3"));+ roleNames.remove("role3");+ roleIds.add(role4.id());+ roleNames.add(role4.name());++ return GroupEntity.builder()+ .withNamespace(group.namespace())+ .withId(group.id())+ .withName(group.name())+ .withRoleNames(roleNames)+ .withRoleIds(roleIds)+ .withAuditInfo(updateAuditInfo)+ .build();+ };+ Assertions.assertNotNull(+ groupMetaService.updateGroup(group1.nameIdentifier(), grantRevokeUpdater));+ GroupEntity grantRevokeGroup =+ GroupMetaService.getInstance().getGroupByIdentifier(group1.nameIdentifier());+ Assertions.assertEquals(group1.id(), grantRevokeGroup.id());+ Assertions.assertEquals(group1.name(), grantRevokeGroup.name());+ Assertions.assertEquals(+ Sets.newHashSet("role1", "role4"), Sets.newHashSet(grantRevokeGroup.roleNames()));+ Assertions.assertEquals(+ Sets.newHashSet(role1.id(), role4.id()), Sets.newHashSet(grantRevokeGroup.roleIds()));+ Assertions.assertEquals("creator", grantRevokeGroup.auditInfo().creator());+ Assertions.assertEquals("grantRevokeUser", grantRevokeGroup.auditInfo().lastModifier());++ // no update+ Function<GroupEntity, GroupEntity> noUpdater =+ group -> {+ AuditInfo updateAuditInfo =+ AuditInfo.builder()+ .withCreator(group.auditInfo().creator())+ .withCreateTime(group.auditInfo().createTime())+ .withLastModifier("noUpdateUser")+ .withLastModifiedTime(Instant.now())+ .build();++ List<String> roleNames = Lists.newArrayList(group.roleNames());+ List<Long> roleIds = Lists.newArrayList(group.roleIds());++ return GroupEntity.builder()+ .withNamespace(group.namespace())+ .withId(group.id())+ .withName(group.name())+ .withRoleNames(roleNames)+ .withRoleIds(roleIds)+ .withAuditInfo(updateAuditInfo)+ .build();+ };+ Assertions.assertNotNull(groupMetaService.updateGroup(group1.nameIdentifier(), noUpdater));+ GroupEntity noUpdaterGroup =+ GroupMetaService.getInstance().getGroupByIdentifier(group1.nameIdentifier());+ Assertions.assertEquals(group1.id(), noUpdaterGroup.id());+ Assertions.assertEquals(group1.name(), noUpdaterGroup.name());+ Assertions.assertEquals(+ Sets.newHashSet("role1", "role4"), Sets.newHashSet(noUpdaterGroup.roleNames()));+ Assertions.assertEquals(+ Sets.newHashSet(role1.id(), role4.id()), Sets.newHashSet(noUpdaterGroup.roleIds()));+ Assertions.assertEquals("creator", noUpdaterGroup.auditInfo().creator());+ Assertions.assertEquals("grantRevokeUser", noUpdaterGroup.auditInfo().lastModifier());+ }++ @Test+ void deleteMetalake() {+ AuditInfo auditInfo =+ AuditInfo.builder().withCreator("creator").withCreateTime(Instant.now()).build();+ BaseMetalake metalake =+ createBaseMakeLake(RandomIdGenerator.INSTANCE.nextId(), metalakeName, auditInfo);+ backend.insert(metalake, false);++ GroupMetaService groupMetaService = GroupMetaService.getInstance();+ RoleMetaService roleMetaService = RoleMetaService.getInstance();++ RoleEntity role1 =+ createRoleEntity(+ RandomIdGenerator.INSTANCE.nextId(),+ AuthorizationUtils.ofRoleNamespace(metalakeName),+ "role1",+ auditInfo);+ RoleEntity role2 =+ createRoleEntity(+ RandomIdGenerator.INSTANCE.nextId(),+ AuthorizationUtils.ofRoleNamespace(metalakeName),+ "role2",+ auditInfo);+ RoleEntity role3 =+ createRoleEntity(+ RandomIdGenerator.INSTANCE.nextId(),+ AuthorizationUtils.ofRoleNamespace(metalakeName),+ "role3",+ auditInfo);+ roleMetaService.insertRole(role1, false);+ roleMetaService.insertRole(role2, false);+ roleMetaService.insertRole(role3, false);+ GroupEntity group1 =+ createGroupEntity(+ RandomIdGenerator.INSTANCE.nextId(),+ AuthorizationUtils.ofGroupNamespace(metalakeName),+ "group1",+ auditInfo,+ Lists.newArrayList(role1.name(), role2.name()),+ Lists.newArrayList(role1.id(), role2.id()));+ GroupEntity group2 =+ createGroupEntity(+ RandomIdGenerator.INSTANCE.nextId(),+ AuthorizationUtils.ofGroupNamespace(metalakeName),+ "group2",+ auditInfo,+ Lists.newArrayList(role3.name()),+ Lists.newArrayList(role3.id()));+ groupMetaService.insertGroup(group1, false);+ groupMetaService.insertGroup(group2, false);++ Assertions.assertEquals(+ group1.name(), groupMetaService.getGroupByIdentifier(group1.nameIdentifier()).name());+ Assertions.assertEquals(2, roleMetaService.listRolesByGroupId(group1.id()).size());+ Assertions.assertEquals(+ group2.name(), groupMetaService.getGroupByIdentifier(group2.nameIdentifier()).name());+ Assertions.assertEquals(1, roleMetaService.listRolesByGroupId(group2.id()).size());++ // delete metalake without cascade+ Assertions.assertTrue(+ MetalakeMetaService.getInstance().deleteMetalake(metalake.nameIdentifier(), false));++ Assertions.assertThrows(+ NoSuchEntityException.class,+ () -> groupMetaService.getGroupByIdentifier(group1.nameIdentifier()));+ Assertions.assertThrows(+ NoSuchEntityException.class,+ () -> groupMetaService.getGroupByIdentifier(group2.nameIdentifier()));+ Assertions.assertEquals(0, roleMetaService.listRolesByGroupId(group1.id()).size());+ Assertions.assertEquals(0, roleMetaService.listRolesByGroupId(group2.id()).size());+ }++ @Test+ void deleteMetalakeCascade() {+ AuditInfo auditInfo =+ AuditInfo.builder().withCreator("creator").withCreateTime(Instant.now()).build();+ BaseMetalake metalake =+ createBaseMakeLake(RandomIdGenerator.INSTANCE.nextId(), metalakeName, auditInfo);+ backend.insert(metalake, false);++ GroupMetaService groupMetaService = GroupMetaService.getInstance();+ RoleMetaService roleMetaService = RoleMetaService.getInstance();++ RoleEntity role1 =+ createRoleEntity(+ RandomIdGenerator.INSTANCE.nextId(),+ AuthorizationUtils.ofRoleNamespace(metalakeName),+ "role1",+ auditInfo);+ RoleEntity role2 =+ createRoleEntity(+ RandomIdGenerator.INSTANCE.nextId(),+ AuthorizationUtils.ofRoleNamespace(metalakeName),+ "role2",+ auditInfo);+ RoleEntity role3 =+ createRoleEntity(+ RandomIdGenerator.INSTANCE.nextId(),+ AuthorizationUtils.ofRoleNamespace(metalakeName),+ "role3",+ auditInfo);+ roleMetaService.insertRole(role1, false);+ roleMetaService.insertRole(role2, false);+ roleMetaService.insertRole(role3, false);+ GroupEntity group1 =+ createGroupEntity(+ RandomIdGenerator.INSTANCE.nextId(),+ AuthorizationUtils.ofGroupNamespace(metalakeName),+ "group1",+ auditInfo,+ Lists.newArrayList(role1.name(), role2.name()),+ Lists.newArrayList(role1.id(), role2.id()));+ GroupEntity group2 =+ createGroupEntity(+ RandomIdGenerator.INSTANCE.nextId(),+ AuthorizationUtils.ofGroupNamespace(metalakeName),+ "group2",+ auditInfo,+ Lists.newArrayList(role3.name()),+ Lists.newArrayList(role3.id()));+ groupMetaService.insertGroup(group1, false);+ groupMetaService.insertGroup(group2, false);++ Assertions.assertEquals(+ group1.name(), groupMetaService.getGroupByIdentifier(group1.nameIdentifier()).name());+ Assertions.assertEquals(2, roleMetaService.listRolesByGroupId(group1.id()).size());+ Assertions.assertEquals(+ group2.name(), groupMetaService.getGroupByIdentifier(group2.nameIdentifier()).name());+ Assertions.assertEquals(1, roleMetaService.listRolesByGroupId(group2.id()).size());++ // delete metalake with cascade+ Assertions.assertTrue(+ MetalakeMetaService.getInstance().deleteMetalake(metalake.nameIdentifier(), true));++ Assertions.assertThrows(+ NoSuchEntityException.class,+ () -> groupMetaService.getGroupByIdentifier(group1.nameIdentifier()));+ Assertions.assertThrows(+ NoSuchEntityException.class,+ () -> groupMetaService.getGroupByIdentifier(group2.nameIdentifier()));+ Assertions.assertEquals(0, roleMetaService.listRolesByGroupId(group1.id()).size());+ Assertions.assertEquals(0, roleMetaService.listRolesByGroupId(group2.id()).size());+ }+}diff --git a/core/src/test/java/com/datastrato/gravitino/storage/relational/service/TestUserMetaService.java b/core/src/test/java/com/datastrato/gravitino/storage/relational/service/TestUserMetaService.javaindex 9a509e0dcc..dabd372c12 100644--- a/core/src/test/java/com/datastrato/gravitino/storage/relational/service/TestUserMetaService.java+++ b/core/src/test/java/com/datastrato/gravitino/storage/relational/service/TestUserMetaService.java@@ -78,7 +78,8 @@ class TestUserMetaService extends TestJDBCBackend {AuthorizationUtils.ofUserNamespace(metalakeName),"user2",auditInfo,- Lists.newArrayList("role1", "role2"));+ Lists.newArrayList(role1.name(), role2.name()),+ Lists.newArrayList(role1.id(), role2.id()));userMetaService.insertUser(user2, false);UserEntity actualUser = userMetaService.getUserByIdentifier(user2.nameIdentifier());Assertions.assertEquals(user2.name(), actualUser.name());@@ -155,7 +156,8 @@ class TestUserMetaService extends TestJDBCBackend {AuthorizationUtils.ofUserNamespace(metalakeName),"user2",auditInfo,- Lists.newArrayList("role1", "role2"));+ Lists.newArrayList(role1.name(), role2.name()),+ Lists.newArrayList(role1.id(), role2.id()));Assertions.assertDoesNotThrow(() -> userMetaService.insertUser(user2, false));UserEntity actualUser = userMetaService.getUserByIdentifier(user2.nameIdentifier());Assertions.assertEquals(user2.name(), actualUser.name());@@ -236,7 +238,8 @@ class TestUserMetaService extends TestJDBCBackend {AuthorizationUtils.ofUserNamespace(metalakeName),"user2",auditInfo,- Lists.newArrayList("role1", "role2"));+ Lists.newArrayList(role1.name(), role2.name()),+ Lists.newArrayList(role1.id(), role2.id()));userMetaService.insertUser(user2, false);List<RolePO> rolePOs =SessionUtils.doWithCommitAndFetchResult(@@ -288,7 +291,8 @@ class TestUserMetaService extends TestJDBCBackend {AuthorizationUtils.ofUserNamespace(metalakeName),"user1",auditInfo,- Lists.newArrayList("role1", "role2"));+ Lists.newArrayList(role1.name(), role2.name()),+ Lists.newArrayList(role1.id(), role2.id()));userMetaService.insertUser(user1, false);UserEntity actualUser = userMetaService.getUserByIdentifier(user1.nameIdentifier());Assertions.assertEquals(user1.name(), actualUser.name());@@ -500,14 +504,16 @@ class TestUserMetaService extends TestJDBCBackend {AuthorizationUtils.ofUserNamespace(metalakeName),"user1",auditInfo,- Lists.newArrayList("role1", "role2"));+ Lists.newArrayList(role1.name(), role2.name()),+ Lists.newArrayList(role1.id(), role2.id()));UserEntity user2 =createUserEntity(RandomIdGenerator.INSTANCE.nextId(),AuthorizationUtils.ofUserNamespace(metalakeName),"user2",auditInfo,- Lists.newArrayList("role3"));+ Lists.newArrayList(role3.name()),+ Lists.newArrayList(role3.id()));userMetaService.insertUser(user1, false);userMetaService.insertUser(user2, false);@@ -570,14 +576,16 @@ class TestUserMetaService extends TestJDBCBackend {AuthorizationUtils.ofUserNamespace(metalakeName),"user1",auditInfo,- Lists.newArrayList("role1", "role2"));+ Lists.newArrayList(role1.name(), role2.name()),+ Lists.newArrayList(role1.id(), role2.id()));UserEntity user2 =createUserEntity(RandomIdGenerator.INSTANCE.nextId(),AuthorizationUtils.ofUserNamespace(metalakeName),"user2",auditInfo,- Lists.newArrayList("role3"));+ Lists.newArrayList(role3.name()),+ Lists.newArrayList(role3.id()));userMetaService.insertUser(user1, false);userMetaService.insertUser(user2, false);diff --git a/core/src/test/resources/h2/schema-h2.sql b/core/src/test/resources/h2/schema-h2.sqlindex 0b2c269b9b..0fa534548d 100644--- a/core/src/test/resources/h2/schema-h2.sql+++ b/core/src/test/resources/h2/schema-h2.sql@@ -164,4 +164,29 @@ CREATE TABLE IF NOT EXISTS `user_role_rel` (PRIMARY KEY (`id`),CONSTRAINT `uk_ui_ri_del` UNIQUE (`user_id`, `role_id`, `deleted_at`),KEY `idx_rid` (`role_id`)-) ENGINE=InnoDB;\ No newline at end of file+) ENGINE=InnoDB;++CREATE TABLE IF NOT EXISTS `group_meta` (+ `group_id` BIGINT(20) UNSIGNED NOT NULL COMMENT 'group id',+ `group_name` VARCHAR(128) NOT NULL COMMENT 'group name',+ `metalake_id` BIGINT(20) UNSIGNED NOT NULL COMMENT 'metalake id',+ `audit_info` MEDIUMTEXT NOT NULL COMMENT 'group audit info',+ `current_version` INT UNSIGNED NOT NULL DEFAULT 1 COMMENT 'group current version',+ `last_version` INT UNSIGNED NOT NULL DEFAULT 1 COMMENT 'group last version',+ `deleted_at` BIGINT(20) UNSIGNED NOT NULL DEFAULT 0 COMMENT 'group deleted at',+ PRIMARY KEY (`group_id`),+ CONSTRAINT `uk_mid_gr_del` UNIQUE (`metalake_id`, `group_name`, `deleted_at`)+ ) ENGINE=InnoDB;++CREATE TABLE IF NOT EXISTS `group_role_rel` (+ `id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT 'auto increment id',+ `group_id` BIGINT(20) UNSIGNED NOT NULL COMMENT 'group id',+ `role_id` BIGINT(20) UNSIGNED NOT NULL COMMENT 'role id',+ `audit_info` MEDIUMTEXT NOT NULL COMMENT 'relation audit info',+ `current_version` INT UNSIGNED NOT NULL DEFAULT 1 COMMENT 'relation current version',+ `last_version` INT UNSIGNED NOT NULL DEFAULT 1 COMMENT 'relation last version',+ `deleted_at` BIGINT(20) UNSIGNED NOT NULL DEFAULT 0 COMMENT 'relation deleted at',+ PRIMARY KEY (`id`),+ CONSTRAINT `uk_gi_ri_del` UNIQUE (`group_id`, `role_id`, `deleted_at`),+ KEY `idx_gid` (`group_id`)+ ) ENGINE=InnoDB;\ No newline at end of filediff --git a/scripts/mysql/schema-0.5.0-mysql.sql b/scripts/mysql/schema-0.5.0-mysql.sqlindex f78942a65c..99bf52e340 100644--- a/scripts/mysql/schema-0.5.0-mysql.sql+++ b/scripts/mysql/schema-0.5.0-mysql.sql@@ -156,4 +156,29 @@ CREATE TABLE IF NOT EXISTS `user_role_rel` (PRIMARY KEY (`id`),UNIQUE KEY `uk_ui_ri_del` (`user_id`, `role_id`, `deleted_at`),KEY `idx_rid` (`role_id`)-) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin COMMENT 'user role relation';\ No newline at end of file+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin COMMENT 'user role relation';++CREATE TABLE IF NOT EXISTS `group_meta` (+ `group_id` BIGINT(20) UNSIGNED NOT NULL COMMENT 'group id',+ `group_name` VARCHAR(128) NOT NULL COMMENT 'group name',+ `metalake_id` BIGINT(20) UNSIGNED NOT NULL COMMENT 'metalake id',+ `audit_info` MEDIUMTEXT NOT NULL COMMENT 'group audit info',+ `current_version` INT UNSIGNED NOT NULL DEFAULT 1 COMMENT 'group current version',+ `last_version` INT UNSIGNED NOT NULL DEFAULT 1 COMMENT 'group last version',+ `deleted_at` BIGINT(20) UNSIGNED NOT NULL DEFAULT 0 COMMENT 'group deleted at',+ PRIMARY KEY (`group_id`),+ UNIQUE KEY `uk_mid_gr_del` (`metalake_id`, `group_name`, `deleted_at`)+ ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin COMMENT 'group metadata';++CREATE TABLE IF NOT EXISTS `group_role_rel` (+ `id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT 'auto increment id',+ `group_id` BIGINT(20) UNSIGNED NOT NULL COMMENT 'group id',+ `role_id` BIGINT(20) UNSIGNED NOT NULL COMMENT 'role id',+ `audit_info` MEDIUMTEXT NOT NULL COMMENT 'relation audit info',+ `current_version` INT UNSIGNED NOT NULL DEFAULT 1 COMMENT 'relation current version',+ `last_version` INT UNSIGNED NOT NULL DEFAULT 1 COMMENT 'relation last version',+ `deleted_at` BIGINT(20) UNSIGNED NOT NULL DEFAULT 0 COMMENT 'relation deleted at',+ PRIMARY KEY (`id`),+ UNIQUE KEY `uk_gi_ri_del` (`group_id`, `role_id`, `deleted_at`),+ KEY `idx_rid` (`group_id`)+ ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin COMMENT 'group role relation';\ No newline at end of file