Skip to content

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

[#8064] improvement(policy): simplify the definition of policy (#8065)

mchades8484e78330f3
diff --git a/api/src/main/java/org/apache/gravitino/policy/Policy.java b/api/src/main/java/org/apache/gravitino/policy/Policy.java
index 05faabdbb7..330d36d097 100644
--- a/api/src/main/java/org/apache/gravitino/policy/Policy.java
+++ b/api/src/main/java/org/apache/gravitino/policy/Policy.java
@@ -19,9 +19,7 @@
package org.apache.gravitino.policy;
import com.google.common.base.Preconditions;
-import com.google.common.collect.ImmutableSet;
import java.util.Optional;
-import java.util.Set;
import org.apache.commons.lang3.StringUtils;
import org.apache.gravitino.Auditable;
import org.apache.gravitino.MetadataObject;
@@ -34,16 +32,6 @@ import org.apache.gravitino.annotation.Evolving;
@Evolving
public interface Policy extends Auditable {
- /** The set of metadata object types that the policy can be applied to. */
- Set<MetadataObject.Type> SUPPORTS_ALL_OBJECT_TYPES =
- ImmutableSet.of(
- MetadataObject.Type.CATALOG,
- MetadataObject.Type.SCHEMA,
- MetadataObject.Type.FILESET,
- MetadataObject.Type.TABLE,
- MetadataObject.Type.TOPIC,
- MetadataObject.Type.MODEL);
-
/**
* The prefix for built-in policy types. All built-in policy types should start with this prefix.
*/
@@ -52,35 +40,20 @@ public interface Policy extends Auditable {
/** The built-in policy types. Predefined policy types that are provided by the system. */
enum BuiltInType {
// todo: add built-in policies, such as:
- // DATA_COMPACTION(BUILT_IN_TYPE_PREFIX + "data_compaction", true, true,
- // SUPPORTS_ALL_OBJECT_TYPES
- // PolicyContent.DataCompactionContent.class)
+ // DATA_COMPACTION(BUILT_IN_TYPE_PREFIX + "data_compaction",
+ // PolicyContent.DataCompactionContent.class)
/**
- * Custom policy type. "custom" is a dummy type for custom policies, all non-built-in types are
- * custom types.
+ * Custom policy type. "custom" is a fixed string that indicates the policy is a non-built-in
+ * type.
*/
- CUSTOM("custom", null, null, null, PolicyContents.CustomContent.class);
+ CUSTOM("custom", PolicyContents.CustomContent.class);
private final String policyType;
- private final Boolean exclusive;
- private final Boolean inheritable;
- private final ImmutableSet<MetadataObject.Type> supportedObjectTypes;
private final Class<? extends PolicyContent> contentClass;
- BuiltInType(
- String policyType,
- Boolean exclusive,
- Boolean inheritable,
- Set<MetadataObject.Type> supportedObjectTypes,
- Class<? extends PolicyContent> contentClass) {
+ BuiltInType(String policyType, Class<? extends PolicyContent> contentClass) {
this.policyType = policyType;
- this.exclusive = exclusive;
- this.inheritable = inheritable;
- this.supportedObjectTypes =
- supportedObjectTypes == null
- ? ImmutableSet.of()
- : ImmutableSet.copyOf(supportedObjectTypes);
this.contentClass = contentClass;
}
@@ -103,8 +76,10 @@ public interface Policy extends Auditable {
String.format("Unknown built-in policy type: %s", policyType));
}
- // If the policy type is not a built-in type, it is a custom type.
- return CUSTOM;
+ throw new IllegalArgumentException(
+ String.format(
+ "Unknown policy type: %s, it should start with '%s' or be 'custom'",
+ policyType, BUILT_IN_TYPE_PREFIX));
}
/**
@@ -116,33 +91,6 @@ public interface Policy extends Auditable {
return policyType;
}
- /**
- * Check if the policy is exclusive.
- *
- * @return true if the policy is exclusive, false otherwise
- */
- public Boolean exclusive() {
- return exclusive;
- }
-
- /**
- * Check if the policy is inheritable.
- *
- * @return true if the policy is inheritable, false otherwise
- */
- public Boolean inheritable() {
- return inheritable;
- }
-
- /**
- * Get the set of metadata object types that the policy can be associated with.
- *
- * @return the set of metadata object types that the policy can be associated with
- */
- public Set<MetadataObject.Type> supportedObjectTypes() {
- return supportedObjectTypes;
- }
-
/**
* Get the content class of the policy.
*
@@ -181,31 +129,6 @@ public interface Policy extends Auditable {
*/
boolean enabled();
- /**
- * Whether the policy is exclusive or not. If the policy is exclusive, only one of the same type
- * policy can be associated with the same object. If the policy is not exclusive, multiple
- * policies of the same type can be associated with the same object.
- *
- * @return True if the policy is exclusive, false otherwise.
- */
- boolean exclusive();
-
- /**
- * Whether the policy is inheritable or not. If the policy is inheritable, it can be inherited by
- * child objects. If the policy is not inheritable, it can only be associated with the metadata
- * object itself.
- *
- * @return True if the policy is inheritable, false otherwise.
- */
- boolean inheritable();
-
- /**
- * Get the set of the metadata object types that the policy can be associated with.
- *
- * @return The set of the metadata object types that the policy can be associated with.
- */
- Set<MetadataObject.Type> supportedObjectTypes();
-
/**
* Get the content of the policy.
*
diff --git a/api/src/main/java/org/apache/gravitino/policy/PolicyContent.java b/api/src/main/java/org/apache/gravitino/policy/PolicyContent.java
index 6f1302687b..2c8d3f641e 100644
--- a/api/src/main/java/org/apache/gravitino/policy/PolicyContent.java
+++ b/api/src/main/java/org/apache/gravitino/policy/PolicyContent.java
@@ -18,11 +18,18 @@
*/
package org.apache.gravitino.policy;
+import com.google.common.base.Preconditions;
import java.util.Map;
+import java.util.Set;
+import org.apache.commons.collections4.CollectionUtils;
+import org.apache.gravitino.MetadataObject;
/** The interface of the content of the policy. */
public interface PolicyContent {
+ /** @return the set of metadata object types that the policy can be applied to */
+ Set<MetadataObject.Type> supportedObjectTypes();
+
/** @return The additional properties of the policy. */
Map<String, String> properties();
@@ -31,5 +38,8 @@ public interface PolicyContent {
*
* @throws IllegalArgumentException if the content is invalid.
*/
- void validate() throws IllegalArgumentException;
+ default void validate() throws IllegalArgumentException {
+ Preconditions.checkArgument(
+ CollectionUtils.isNotEmpty(supportedObjectTypes()), "supportedObjectTypes cannot be empty");
+ }
}
diff --git a/api/src/main/java/org/apache/gravitino/policy/PolicyContents.java b/api/src/main/java/org/apache/gravitino/policy/PolicyContents.java
index f95a186db4..27b9caa636 100644
--- a/api/src/main/java/org/apache/gravitino/policy/PolicyContents.java
+++ b/api/src/main/java/org/apache/gravitino/policy/PolicyContents.java
@@ -18,8 +18,11 @@
*/
package org.apache.gravitino.policy;
+import com.google.common.collect.ImmutableSet;
import java.util.Map;
import java.util.Objects;
+import java.util.Set;
+import org.apache.gravitino.MetadataObject;
/** Utility class for creating instances of {@link PolicyContent}. */
public class PolicyContents {
@@ -28,11 +31,15 @@ public class PolicyContents {
* Creates a custom policy content with the given rules and properties.
*
* @param rules The custom rules of the policy.
+ * @param supportedObjectTypes The set of metadata object types that the policy can be applied to.
* @param properties The additional properties of the policy.
* @return A new instance of {@link PolicyContent} with the specified rules and properties.
*/
- public static PolicyContent custom(Map<String, Object> rules, Map<String, String> properties) {
- return new CustomContent(rules, properties);
+ public static PolicyContent custom(
+ Map<String, Object> rules,
+ Set<MetadataObject.Type> supportedObjectTypes,
+ Map<String, String> properties) {
+ return new CustomContent(rules, supportedObjectTypes, properties);
}
private PolicyContents() {}
@@ -43,21 +50,31 @@ public class PolicyContents {
*/
public static class CustomContent implements PolicyContent {
private final Map<String, Object> customRules;
+ private final Set<MetadataObject.Type> supportedObjectTypes;
private final Map<String, String> properties;
/** Default constructor for Jackson deserialization only. */
private CustomContent() {
- this(null, null);
+ this(null, null, null);
}
/**
* Constructor for CustomContent.
*
* @param customRules the custom rules of the policy
+ * @param supportedObjectTypes the set of metadata object types that the policy can be applied
+ * to
* @param properties the additional properties of the policy
*/
- private CustomContent(Map<String, Object> customRules, Map<String, String> properties) {
+ private CustomContent(
+ Map<String, Object> customRules,
+ Set<MetadataObject.Type> supportedObjectTypes,
+ Map<String, String> properties) {
this.customRules = customRules;
+ this.supportedObjectTypes =
+ supportedObjectTypes == null
+ ? ImmutableSet.of()
+ : ImmutableSet.copyOf(supportedObjectTypes);
this.properties = properties;
}
@@ -71,13 +88,13 @@ public class PolicyContents {
}
@Override
- public Map<String, String> properties() {
- return properties;
+ public Set<MetadataObject.Type> supportedObjectTypes() {
+ return supportedObjectTypes;
}
@Override
- public void validate() throws IllegalArgumentException {
- // nothing to validate for custom content
+ public Map<String, String> properties() {
+ return properties;
}
@Override
@@ -85,17 +102,25 @@ public class PolicyContents {
if (!(o instanceof CustomContent)) return false;
CustomContent that = (CustomContent) o;
return Objects.equals(customRules, that.customRules)
- && Objects.equals(properties, that.properties);
+ && Objects.equals(properties, that.properties)
+ && Objects.equals(supportedObjectTypes, that.supportedObjectTypes);
}
@Override
public int hashCode() {
- return Objects.hash(customRules, properties);
+ return Objects.hash(customRules, properties, supportedObjectTypes);
}
@Override
public String toString() {
- return "CustomContent{" + "customRules=" + customRules + ", properties=" + properties + '}';
+ return "CustomContent{"
+ + "customRules="
+ + customRules
+ + ", properties="
+ + properties
+ + ", supportedObjectTypes="
+ + supportedObjectTypes
+ + '}';
}
}
}
diff --git a/api/src/main/java/org/apache/gravitino/policy/PolicyOperations.java b/api/src/main/java/org/apache/gravitino/policy/PolicyOperations.java
index 440c4860e4..1bbd8062ac 100644
--- a/api/src/main/java/org/apache/gravitino/policy/PolicyOperations.java
+++ b/api/src/main/java/org/apache/gravitino/policy/PolicyOperations.java
@@ -18,8 +18,6 @@
*/
package org.apache.gravitino.policy;
-import java.util.Set;
-import org.apache.gravitino.MetadataObject;
import org.apache.gravitino.annotation.Evolving;
import org.apache.gravitino.exceptions.NoSuchMetalakeException;
import org.apache.gravitino.exceptions.NoSuchPolicyException;
@@ -59,8 +57,7 @@ public interface PolicyOperations {
Policy getPolicy(String name) throws NoSuchPolicyException;
/**
- * Create a built-in type policy under a metalake. The exclusive, inheritable, and supported
- * object types parameters attributes are determined by the type of the policy.
+ * Create a policy under a metalake.
*
* @param name The name of the policy.
* @param type The type of the policy.
@@ -74,38 +71,6 @@ public interface PolicyOperations {
String name, String type, String comment, boolean enabled, PolicyContent content)
throws PolicyAlreadyExistsException;
- /**
- * Create a new policy under a metalake.
- *
- * @param name The name of the policy.
- * @param type The type of the policy.
- * @param comment The comment of the policy.
- * @param enabled Whether the policy is enabled or not.
- * @param exclusive Whether the policy is exclusive or not. If the policy is exclusive, only one
- * of the same type policy can be associated with the same object, and the same type of policy
- * on a metadata object will override the one inherited from the parent object. If the policy
- * is not exclusive, multiple policies of the same type can be associated with the same
- * object.
- * @param inheritable Whether the policy is inheritable or not. If the policy is inheritable, it
- * will be inherited automatically by child objects. If the policy is not inheritable, it can
- * only be associated with the metadata object itself.
- * @param supportedObjectTypes The set of the metadata object types that the policy can be
- * associated with
- * @param content The content of the policy.
- * @return The created policy.
- * @throws PolicyAlreadyExistsException If the policy already exists.
- */
- Policy createPolicy(
- String name,
- String type,
- String comment,
- boolean enabled,
- boolean exclusive,
- boolean inheritable,
- Set<MetadataObject.Type> supportedObjectTypes,
- PolicyContent content)
- throws PolicyAlreadyExistsException;
-
/**
* Enable a policy under a metalake. If the policy is already enabled, this method does nothing.
*
diff --git a/common/src/main/java/org/apache/gravitino/dto/policy/PolicyContentDTO.java b/common/src/main/java/org/apache/gravitino/dto/policy/PolicyContentDTO.java
index 505881430a..2de0cbeadd 100644
--- a/common/src/main/java/org/apache/gravitino/dto/policy/PolicyContentDTO.java
+++ b/common/src/main/java/org/apache/gravitino/dto/policy/PolicyContentDTO.java
@@ -20,10 +20,12 @@ package org.apache.gravitino.dto.policy;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.Map;
+import java.util.Set;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.EqualsAndHashCode;
import lombok.ToString;
+import org.apache.gravitino.MetadataObject;
import org.apache.gravitino.policy.PolicyContent;
/** Represents a Policy Content Data Transfer Object (DTO). */
@@ -42,6 +44,9 @@ public interface PolicyContentDTO extends PolicyContent {
@JsonProperty("properties")
private Map<String, String> properties;
+ @JsonProperty("supportedObjectTypes")
+ private Set<MetadataObject.Type> supportedObjectTypes;
+
// Default constructor for Jackson deserialization only.
private CustomContentDTO() {}
@@ -55,13 +60,13 @@ public interface PolicyContentDTO extends PolicyContent {
}
@Override
- public Map<String, String> properties() {
- return properties;
+ public Set<MetadataObject.Type> supportedObjectTypes() {
+ return supportedObjectTypes;
}
@Override
- public void validate() throws IllegalArgumentException {
- // no validation needed for custom content
+ public Map<String, String> properties() {
+ return properties;
}
}
}
diff --git a/common/src/main/java/org/apache/gravitino/dto/policy/PolicyDTO.java b/common/src/main/java/org/apache/gravitino/dto/policy/PolicyDTO.java
index 520950d681..a7ceae5292 100644
--- a/common/src/main/java/org/apache/gravitino/dto/policy/PolicyDTO.java
+++ b/common/src/main/java/org/apache/gravitino/dto/policy/PolicyDTO.java
@@ -22,14 +22,10 @@ import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.google.common.base.Preconditions;
-import java.util.Collections;
import java.util.Objects;
import java.util.Optional;
-import java.util.Set;
import lombok.ToString;
-import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
-import org.apache.gravitino.MetadataObject;
import org.apache.gravitino.dto.AuditDTO;
import org.apache.gravitino.policy.Policy;
@@ -49,15 +45,6 @@ public class PolicyDTO implements Policy {
@JsonProperty("enabled")
private boolean enabled;
- @JsonProperty("exclusive")
- private boolean exclusive;
-
- @JsonProperty("inheritable")
- private boolean inheritable;
-
- @JsonProperty("supportedObjectTypes")
- private Set<MetadataObject.Type> supportedObjectTypes = Collections.emptySet();
-
@JsonProperty("content")
@JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
@@ -84,28 +71,16 @@ public class PolicyDTO implements Policy {
if (!(o instanceof PolicyDTO)) return false;
PolicyDTO policyDTO = (PolicyDTO) o;
return enabled == policyDTO.enabled
- && exclusive == policyDTO.exclusive
- && inheritable == policyDTO.inheritable
&& Objects.equals(name, policyDTO.name)
&& Objects.equals(comment, policyDTO.comment)
&& Objects.equals(policyType, policyDTO.policyType)
- && Objects.equals(supportedObjectTypes, policyDTO.supportedObjectTypes)
&& Objects.equals(content, policyDTO.content)
&& Objects.equals(audit, policyDTO.audit);
}
@Override
public int hashCode() {
- return Objects.hash(
- name,
- comment,
- policyType,
- enabled,
- exclusive,
- inheritable,
- supportedObjectTypes,
- content,
- audit);
+ return Objects.hash(name, comment, policyType, enabled, content, audit);
}
/** @return a new builder for constructing a PolicyDTO. */
@@ -133,21 +108,6 @@ public class PolicyDTO implements Policy {
return enabled;
}
- @Override
- public boolean exclusive() {
- return exclusive;
- }
-
- @Override
- public boolean inheritable() {
- return inheritable;
- }
-
- @Override
- public Set<MetadataObject.Type> supportedObjectTypes() {
- return supportedObjectTypes;
- }
-
@Override
public PolicyContentDTO content() {
return content;
@@ -215,39 +175,6 @@ public class PolicyDTO implements Policy {
return this;
}
- /**
- * Sets whether the policy is exclusive or not.
- *
- * @param exclusive Whether the policy is exclusive.
- * @return The builder instance.
- */
- public Builder withExclusive(boolean exclusive) {
- policyDTO.exclusive = exclusive;
- return this;
- }
-
- /**
- * Sets whether the policy is inheritable or not.
- *
- * @param inheritable Whether the policy is inheritable.
- * @return The builder instance.
- */
- public Builder withInheritable(boolean inheritable) {
- policyDTO.inheritable = inheritable;
- return this;
- }
-
- /**
- * Sets the set of supported metadata object types for the policy.
- *
- * @param supportedObjectTypes The set of supported metadata object types.
- * @return The builder instance.
- */
- public Builder withSupportedObjectTypes(Set<MetadataObject.Type> supportedObjectTypes) {
- policyDTO.supportedObjectTypes = supportedObjectTypes;
- return this;
- }
-
/**
* Sets the content of the policy.
*
@@ -288,9 +215,6 @@ public class PolicyDTO implements Policy {
Preconditions.checkArgument(
StringUtils.isNotBlank(policyDTO.policyType), "policy type cannot be empty");
Preconditions.checkArgument(policyDTO.content != null, "policy content cannot be null");
- Preconditions.checkArgument(
- CollectionUtils.isNotEmpty(policyDTO.supportedObjectTypes),
- "supported objectTypes cannot be empty");
policyDTO.content.validate();
return policyDTO;
}
diff --git a/common/src/main/java/org/apache/gravitino/dto/requests/PolicyCreateRequest.java b/common/src/main/java/org/apache/gravitino/dto/requests/PolicyCreateRequest.java
index 9b57050326..ba1f06ff30 100644
--- a/common/src/main/java/org/apache/gravitino/dto/requests/PolicyCreateRequest.java
+++ b/common/src/main/java/org/apache/gravitino/dto/requests/PolicyCreateRequest.java
@@ -22,15 +22,12 @@ import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.google.common.base.Preconditions;
-import java.util.Set;
import javax.annotation.Nullable;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.ToString;
import org.apache.commons.lang3.StringUtils;
-import org.apache.gravitino.MetadataObject;
import org.apache.gravitino.dto.policy.PolicyContentDTO;
-import org.apache.gravitino.policy.Policy;
import org.apache.gravitino.rest.RESTRequest;
/** Represents a request to create a policy. */
@@ -65,15 +62,6 @@ public class PolicyCreateRequest implements RESTRequest {
})
private final PolicyContentDTO policyContent;
- @JsonProperty("exclusive")
- private final Boolean exclusive;
-
- @JsonProperty("inheritable")
- private final Boolean inheritable;
-
- @JsonProperty("supportedObjectTypes")
- private final Set<MetadataObject.Type> supportedObjectTypes;
-
/**
* Creates a new PolicyCreateRequest.
*
@@ -81,33 +69,20 @@ public class PolicyCreateRequest implements RESTRequest {
* @param comment The comment of the policy.
* @param type The type of the policy.
* @param enabled Whether the policy is enabled.
- * @param exclusive Whether the policy is exclusive.
- * @param inheritable Whether the policy is inheritable.
- * @param supportedObjectTypes The set of metadata object types that the policy can be applied to.
* @param content The content of the policy.
*/
public PolicyCreateRequest(
- String name,
- String type,
- String comment,
- boolean enabled,
- boolean exclusive,
- boolean inheritable,
- Set<MetadataObject.Type> supportedObjectTypes,
- PolicyContentDTO content) {
+ String name, String type, String comment, boolean enabled, PolicyContentDTO content) {
this.name = name;
this.policyType = type;
this.comment = comment;
this.enabled = enabled;
- this.exclusive = exclusive;
- this.inheritable = inheritable;
- this.supportedObjectTypes = supportedObjectTypes;
this.policyContent = content;
}
/** This is the constructor that is used by Jackson deserializer */
private PolicyCreateRequest() {
- this(null, null, null, true, false, false, null, null);
+ this(null, null, null, true, null);
}
/**
@@ -121,41 +96,6 @@ public class PolicyCreateRequest implements RESTRequest {
StringUtils.isNotBlank(name), "\"name\" is required and cannot be empty");
Preconditions.checkArgument(
StringUtils.isNotBlank(policyType), "\"policyType\" is required and cannot be empty");
-
- Policy.BuiltInType builtInType = Policy.BuiltInType.fromPolicyType(policyType);
- if (builtInType != Policy.BuiltInType.CUSTOM) {
- if (exclusive != null) {
- Preconditions.checkArgument(
- exclusive.equals(builtInType.exclusive()),
- String.format(
- "Exclusive flag for built-in policy type '%s' must be %s",
- builtInType, builtInType.exclusive()));
- }
-
- if (inheritable != null) {
- Preconditions.checkArgument(
- inheritable.equals(builtInType.inheritable()),
- String.format(
- "Inheritable flag for built-in policy type '%s' must be %s",
- builtInType, builtInType.inheritable()));
- }
-
- if (supportedObjectTypes != null) {
- Preconditions.checkArgument(
- builtInType.supportedObjectTypes().equals(supportedObjectTypes),
- String.format(
- "Supported object types for built-in policy type '%s' must be %s",
- builtInType, builtInType.supportedObjectTypes()));
- }
- } else {
- Preconditions.checkArgument(
- exclusive != null, "\"exclusive\" is required for custom policy type");
- Preconditions.checkArgument(
- inheritable != null, "\"inheritable\" is required for custom policy type");
- Preconditions.checkArgument(
- supportedObjectTypes != null,
- "\"supportedObjectTypes\" is required for custom policy type");
- }
Preconditions.checkArgument(
policyContent != null, "\"content\" is required and cannot be null");
policyContent.validate();
diff --git a/common/src/main/java/org/apache/gravitino/dto/util/DTOConverters.java b/common/src/main/java/org/apache/gravitino/dto/util/DTOConverters.java
index 020523bf44..9ef4026431 100644
--- a/common/src/main/java/org/apache/gravitino/dto/util/DTOConverters.java
+++ b/common/src/main/java/org/apache/gravitino/dto/util/DTOConverters.java
@@ -58,7 +58,6 @@ import org.apache.gravitino.dto.messaging.TopicDTO;
import org.apache.gravitino.dto.model.ModelDTO;
import org.apache.gravitino.dto.model.ModelVersionDTO;
import org.apache.gravitino.dto.policy.PolicyContentDTO;
-import org.apache.gravitino.dto.policy.PolicyDTO;
import org.apache.gravitino.dto.rel.ColumnDTO;
import org.apache.gravitino.dto.rel.DistributionDTO;
import org.apache.gravitino.dto.rel.SortOrderDTO;
@@ -94,7 +93,6 @@ import org.apache.gravitino.job.SparkJobTemplate;
import org.apache.gravitino.messaging.Topic;
import org.apache.gravitino.model.Model;
import org.apache.gravitino.model.ModelVersion;
-import org.apache.gravitino.policy.Policy;
import org.apache.gravitino.policy.PolicyContent;
import org.apache.gravitino.policy.PolicyContents;
import org.apache.gravitino.rel.Column;
@@ -536,31 +534,6 @@ public class DTOConverters {
return builder.build();
}
- /**
- * Converts a Policy to a PolicyDTO.
- *
- * @param policy The policy to be converted.
- * @param inherited The inherited flag.
- * @return The policy DTO.
- */
- public static PolicyDTO toDTO(Policy policy, Optional<Boolean> inherited) {
- PolicyDTO.Builder builder =
- PolicyDTO.builder()
- .withName(policy.name())
- .withComment(policy.comment())
- .withPolicyType(policy.policyType())
- .withEnabled(policy.enabled())
- .withExclusive(policy.exclusive())
- .withInheritable(policy.inheritable())
- .withSupportedObjectTypes(policy.supportedObjectTypes())
- .withContent(toDTO(policy.content()))
- .withInherited(inherited)
- .withAudit(toDTO(policy.auditInfo()))
- .withInherited(inherited);
-
- return builder.build();
- }
-
/**
* Converts a PolicyContent to a PolicyContentDTO.
*
@@ -580,6 +553,7 @@ public class DTOConverters {
PolicyContents.CustomContent customContent = (PolicyContents.CustomContent) policyContent;
return PolicyContentDTO.CustomContentDTO.builder()
.withCustomRules(customContent.customRules())
+ .withSupportedObjectTypes(customContent.supportedObjectTypes())
.withProperties(customContent.properties())
.build();
}
@@ -1259,7 +1233,10 @@ public class DTOConverters {
if (policyContentDTO instanceof PolicyContentDTO.CustomContentDTO) {
PolicyContentDTO.CustomContentDTO customContentDTO =
(PolicyContentDTO.CustomContentDTO) policyContentDTO;
- return PolicyContents.custom(customContentDTO.customRules(), customContentDTO.properties());
+ return PolicyContents.custom(
+ customContentDTO.customRules(),
+ customContentDTO.supportedObjectTypes(),
+ customContentDTO.properties());
}
throw new IllegalArgumentException(
diff --git a/common/src/test/java/org/apache/gravitino/dto/policy/TestPolicyDTO.java b/common/src/test/java/org/apache/gravitino/dto/policy/TestPolicyDTO.java
index ebb173409e..f1c2aeb7a1 100644
--- a/common/src/test/java/org/apache/gravitino/dto/policy/TestPolicyDTO.java
+++ b/common/src/test/java/org/apache/gravitino/dto/policy/TestPolicyDTO.java
@@ -18,12 +18,12 @@
*/
package org.apache.gravitino.dto.policy;
-import static org.apache.gravitino.policy.Policy.SUPPORTS_ALL_OBJECT_TYPES;
-
import com.fasterxml.jackson.core.JsonProcessingException;
import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.ImmutableSet;
import java.time.Instant;
import java.util.Optional;
+import org.apache.gravitino.MetadataObject;
import org.apache.gravitino.dto.AuditDTO;
import org.apache.gravitino.json.JsonUtils;
import org.junit.jupiter.api.Assertions;
@@ -37,6 +37,8 @@ public class TestPolicyDTO {
PolicyContentDTO.CustomContentDTO customContent =
PolicyContentDTO.CustomContentDTO.builder()
.withCustomRules(ImmutableMap.of("key1", "value1"))
+ .withSupportedObjectTypes(
+ ImmutableSet.of(MetadataObject.Type.CATALOG, MetadataObject.Type.TABLE))
.withProperties(ImmutableMap.of("prop1", "value1"))
.build();
@@ -46,10 +48,6 @@ public class TestPolicyDTO {
.withComment("policy comment")
.withPolicyType("my_compaction")
.withEnabled(true)
- .withInheritable(true)
- .withExclusive(true)
- .withInheritable(true)
- .withSupportedObjectTypes(SUPPORTS_ALL_OBJECT_TYPES)
.withContent(customContent)
.withAudit(audit)
.build();
@@ -62,9 +60,6 @@ public class TestPolicyDTO {
Assertions.assertEquals("policy comment", deserPolicyDTO.comment());
Assertions.assertEquals("my_compaction", deserPolicyDTO.policyType());
Assertions.assertTrue(deserPolicyDTO.enabled());
- Assertions.assertTrue(deserPolicyDTO.inheritable());
- Assertions.assertTrue(deserPolicyDTO.exclusive());
- Assertions.assertEquals(SUPPORTS_ALL_OBJECT_TYPES, deserPolicyDTO.supportedObjectTypes());
Assertions.assertEquals(customContent, deserPolicyDTO.content());
Assertions.assertEquals(audit, deserPolicyDTO.auditInfo());
@@ -74,7 +69,6 @@ public class TestPolicyDTO {
.withName("policy_test")
.withComment("policy comment")
.withPolicyType("my_compaction")
- .withSupportedObjectTypes(SUPPORTS_ALL_OBJECT_TYPES)
.withContent(customContent)
.withAudit(audit)
.withInherited(Optional.empty())
@@ -90,7 +84,6 @@ public class TestPolicyDTO {
.withName("policy_test")
.withComment("policy comment")
.withPolicyType("my_compaction")
- .withSupportedObjectTypes(SUPPORTS_ALL_OBJECT_TYPES)
.withContent(customContent)
.withAudit(audit)
.withInherited(Optional.of(false))
@@ -105,7 +98,6 @@ public class TestPolicyDTO {
.withName("policy_test")
.withComment("policy comment")
.withPolicyType("my_compaction")
- .withSupportedObjectTypes(SUPPORTS_ALL_OBJECT_TYPES)
.withContent(customContent)
.withAudit(audit)
.withInherited(Optional.of(true))
diff --git a/common/src/test/java/org/apache/gravitino/dto/requests/TestPolicyCreateRequest.java b/common/src/test/java/org/apache/gravitino/dto/requests/TestPolicyCreateRequest.java
index 1551719833..81b33ca3de 100644
--- a/common/src/test/java/org/apache/gravitino/dto/requests/TestPolicyCreateRequest.java
+++ b/common/src/test/java/org/apache/gravitino/dto/requests/TestPolicyCreateRequest.java
@@ -20,9 +20,10 @@ package org.apache.gravitino.dto.requests;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.ImmutableSet;
+import org.apache.gravitino.MetadataObject;
import org.apache.gravitino.dto.policy.PolicyContentDTO;
import org.apache.gravitino.json.JsonUtils;
-import org.apache.gravitino.policy.Policy;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
@@ -33,18 +34,12 @@ public class TestPolicyCreateRequest {
PolicyContentDTO content =
PolicyContentDTO.CustomContentDTO.builder()
.withCustomRules(ImmutableMap.of("rule1", "value1"))
+ .withSupportedObjectTypes(
+ ImmutableSet.of(MetadataObject.Type.FILESET, MetadataObject.Type.TABLE))
.withProperties(null)
.build();
PolicyCreateRequest request =
- new PolicyCreateRequest(
- "policy_test",
- "test_type",
- "policy comment",
- true,
- true,
- true,
- Policy.SUPPORTS_ALL_OBJECT_TYPES,
- content);
+ new PolicyCreateRequest("policy_test", "test_type", "policy comment", true, content);
String serJson = JsonUtils.objectMapper().writeValueAsString(request);
PolicyCreateRequest deserRequest =
JsonUtils.objectMapper().readValue(serJson, PolicyCreateRequest.class);
@@ -53,10 +48,28 @@ public class TestPolicyCreateRequest {
Assertions.assertEquals("policy comment", deserRequest.getComment());
Assertions.assertEquals("test_type", deserRequest.getPolicyType());
Assertions.assertTrue(deserRequest.getEnabled());
- Assertions.assertTrue(deserRequest.getExclusive());
- Assertions.assertTrue(deserRequest.getInheritable());
- Assertions.assertEquals(
- Policy.SUPPORTS_ALL_OBJECT_TYPES, deserRequest.getSupportedObjectTypes());
Assertions.assertEquals(content, deserRequest.getPolicyContent());
+
+ // test null supported object types in content
+ PolicyContentDTO contentWithNullTypes =
+ PolicyContentDTO.CustomContentDTO.builder()
+ .withCustomRules(ImmutableMap.of("rule2", "value2"))
+ .withSupportedObjectTypes(null)
+ .withProperties(null)
+ .build();
+ request =
+ new PolicyCreateRequest(
+ "policy_test_null_types", "test_type", "policy comment", true, contentWithNullTypes);
+ serJson = JsonUtils.objectMapper().writeValueAsString(request);
+ deserRequest = JsonUtils.objectMapper().readValue(serJson, PolicyCreateRequest.class);
+ Assertions.assertEquals(request, deserRequest);
+ Assertions.assertEquals("policy_test_null_types", deserRequest.getName());
+ Assertions.assertEquals("policy comment", deserRequest.getComment());
+ Assertions.assertEquals("test_type", deserRequest.getPolicyType());
+ Assertions.assertTrue(deserRequest.getEnabled());
+ Assertions.assertEquals(contentWithNullTypes, deserRequest.getPolicyContent());
+
+ Exception e = Assertions.assertThrows(IllegalArgumentException.class, request::validate);
+ Assertions.assertEquals("supportedObjectTypes cannot be empty", e.getMessage());
}
}
diff --git a/core/src/main/java/org/apache/gravitino/meta/PolicyEntity.java b/core/src/main/java/org/apache/gravitino/meta/PolicyEntity.java
index dbfedc5627..e93b8edd11 100644
--- a/core/src/main/java/org/apache/gravitino/meta/PolicyEntity.java
+++ b/core/src/main/java/org/apache/gravitino/meta/PolicyEntity.java
@@ -24,7 +24,6 @@ import java.util.Collections;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
-import java.util.Set;
import lombok.ToString;
import org.apache.commons.lang3.StringUtils;
import org.apache.gravitino.Audit;
@@ -32,32 +31,24 @@ import org.apache.gravitino.Auditable;
import org.apache.gravitino.Entity;
import org.apache.gravitino.Field;
import org.apache.gravitino.HasIdentifier;
-import org.apache.gravitino.MetadataObject;
import org.apache.gravitino.Namespace;
import org.apache.gravitino.policy.Policy;
import org.apache.gravitino.policy.PolicyContent;
import org.apache.gravitino.policy.PolicyContents;
@ToString
-public class PolicyEntity implements Policy, Entity, Auditable, HasIdentifier {
+public class PolicyEntity implements Entity, Auditable, HasIdentifier {
public static final Field ID =
Field.required("id", Long.class, "The unique id of the policy entity.");
public static final Field NAME =
Field.required("name", String.class, "The name of the policy entity.");
public static final Field POLICY_TYPE =
- Field.required("policyType", String.class, "The type of the policy entity.");
+ Field.required("policyType", Policy.BuiltInType.class, "The type of the policy entity.");
public static final Field COMMENT =
Field.optional("comment", String.class, "The comment of the policy entity.");
public static final Field ENABLED =
Field.required("enabled", Boolean.class, "The policy entity is enabled.");
- public static final Field EXCLUSIVE =
- Field.required("exclusive", Boolean.class, "The policy entity is exclusive.");
- public static final Field INHERITABLE =
- Field.required("inheritable", Boolean.class, "The policy entity is inheritable.");
- public static final Field SUPPORTED_OBJECT_TYPES =
- Field.required(
- "supportedObjectTypes", Set.class, "The supported object types of the policy entity.");
public static final Field CONTENT =
Field.required("content", PolicyContent.class, "The content of the policy entity.");
public static final Field AUDIT_INFO =
@@ -70,12 +61,9 @@ public class PolicyEntity implements Policy, Entity, Auditable, HasIdentifier {
private Long id;
private String name;
private Namespace namespace;
- private String policyType;
+ private Policy.BuiltInType policyType;
private String comment;
private boolean enabled;
- private boolean exclusive;
- private boolean inheritable;
- private Set<MetadataObject.Type> supportedObjectTypes;
private PolicyContent content;
private AuditInfo auditInfo;
@@ -89,9 +77,6 @@ public class PolicyEntity implements Policy, Entity, Auditable, HasIdentifier {
fields.put(POLICY_TYPE, policyType);
fields.put(COMMENT, comment);
fields.put(ENABLED, enabled);
- fields.put(EXCLUSIVE, exclusive);
- fields.put(INHERITABLE, inheritable);
- fields.put(SUPPORTED_OBJECT_TYPES, supportedObjectTypes);
fields.put(CONTENT, content);
fields.put(AUDIT_INFO, auditInfo);
@@ -123,42 +108,22 @@ public class PolicyEntity implements Policy, Entity, Auditable, HasIdentifier {
return auditInfo;
}
- @Override
- public String policyType() {
+ public Policy.BuiltInType policyType() {
return policyType;
}
- @Override
public String comment() {
return comment;
}
- @Override
public boolean enabled() {
return enabled;
}
- @Override
- public boolean exclusive() {
- return exclusive;
- }
-
- @Override
- public boolean inheritable() {
- return inheritable;
- }
-
- @Override
- public Set<MetadataObject.Type> supportedObjectTypes() {
- return supportedObjectTypes;
- }
-
- @Override
public PolicyContent content() {
return content;
}
- @Override
public Optional<Boolean> inherited() {
return Optional.empty();
}
@@ -175,80 +140,40 @@ public class PolicyEntity implements Policy, Entity, Auditable, HasIdentifier {
if (!(o instanceof PolicyEntity)) return false;
PolicyEntity that = (PolicyEntity) o;
return enabled == that.enabled
- && exclusive == that.exclusive
- && inheritable == that.inheritable
&& Objects.equals(id, that.id)
&& Objects.equals(name, that.name)
&& Objects.equals(namespace, that.namespace)
&& Objects.equals(policyType, that.policyType)
&& Objects.equals(comment, that.comment)
- && Objects.equals(supportedObjectTypes, that.supportedObjectTypes)
&& Objects.equals(content, that.content)
&& Objects.equals(auditInfo, that.auditInfo);
}
@Override
public int hashCode() {
- return Objects.hash(
- id,
- name,
- namespace,
- policyType,
- comment,
- enabled,
- exclusive,
- inheritable,
- supportedObjectTypes,
- content,
- auditInfo);
+ return Objects.hash(id, name, namespace, policyType, comment, enabled, content, auditInfo);
}
private void validatePolicy() {
Preconditions.checkArgument(StringUtils.isNotBlank(name()), "Policy name cannot be blank");
- Preconditions.checkArgument(
- StringUtils.isNotBlank(policyType()), "Policy type cannot be blank");
+
Preconditions.checkArgument(content() != null, "Policy content cannot be null");
- Preconditions.checkArgument(
- supportedObjectTypes() != null && !supportedObjectTypes().isEmpty(),
- "Policy must support at least one metadata object type");
- BuiltInType builtInType = BuiltInType.fromPolicyType(policyType());
Preconditions.checkArgument(
- builtInType != BuiltInType.CUSTOM || content() instanceof PolicyContents.CustomContent,
+ policyType != Policy.BuiltInType.CUSTOM
+ || content() instanceof PolicyContents.CustomContent,
"Expected CustomContent for custom policy type, but got %s",
content().getClass().getName());
- if (builtInType != BuiltInType.CUSTOM) {
- Preconditions.checkArgument(
- exclusive() == builtInType.exclusive(),
- "Expected exclusive value %s for built-in policy type %s, but got %s",
- builtInType.exclusive(),
- policyType(),
- exclusive());
- Preconditions.checkArgument(
- inheritable() == builtInType.inheritable(),
- "Expected inheritable value %s for built-in policy type %s, but got %s",
- builtInType.inheritable(),
- policyType(),
- inheritable());
- Preconditions.checkArgument(
- supportedObjectTypes().equals(builtInType.supportedObjectTypes()),
- "Expected supported object types %s for built-in policy type %s, but got %s",
- builtInType.supportedObjectTypes(),
- policyType(),
- supportedObjectTypes());
- }
+ content().validate();
}
public static class Builder {
private Long id;
private String name;
private Namespace namespace;
- private String policyType;
+ private Policy.BuiltInType policyType;
private String comment;
private boolean enabled = true;
- private boolean exclusive;
- private boolean inheritable;
- private Set<MetadataObject.Type> supportedObjectTypes;
private PolicyContent content;
private AuditInfo auditInfo;
@@ -267,7 +192,7 @@ public class PolicyEntity implements Policy, Entity, Auditable, HasIdentifier {
return this;
}
- public Builder withPolicyType(String policyType) {
+ public Builder withPolicyType(Policy.BuiltInType policyType) {
this.policyType = policyType;
return this;
}
@@ -282,21 +207,6 @@ public class PolicyEntity implements Policy, Entity, Auditable, HasIdentifier {
return this;
}
- public Builder withExclusive(boolean exclusive) {
- this.exclusive = exclusive;
- return this;
- }
-
- public Builder withInheritable(boolean inheritable) {
- this.inheritable = inheritable;
- return this;
- }
-
- public Builder withSupportedObjectTypes(Set<MetadataObject.Type> supportedObjectTypes) {
- this.supportedObjectTypes = supportedObjectTypes;
- return this;
- }
-
public Builder withContent(PolicyContent content) {
this.content = content;
return this;
@@ -315,9 +225,6 @@ public class PolicyEntity implements Policy, Entity, Auditable, HasIdentifier {
policyEntity.policyType = policyType;
policyEntity.comment = comment;
policyEntity.enabled = enabled;
- policyEntity.exclusive = exclusive;
- policyEntity.inheritable = inheritable;
- policyEntity.supportedObjectTypes = supportedObjectTypes;
policyEntity.content = content;
policyEntity.auditInfo = auditInfo;
policyEntity.validate();
diff --git a/core/src/main/java/org/apache/gravitino/policy/PolicyDispatcher.java b/core/src/main/java/org/apache/gravitino/policy/PolicyDispatcher.java
index 571dc25e09..9ac1dac7d1 100644
--- a/core/src/main/java/org/apache/gravitino/policy/PolicyDispatcher.java
+++ b/core/src/main/java/org/apache/gravitino/policy/PolicyDispatcher.java
@@ -20,11 +20,11 @@
package org.apache.gravitino.policy;
import java.util.Arrays;
-import java.util.Set;
import org.apache.gravitino.MetadataObject;
import org.apache.gravitino.annotation.Evolving;
import org.apache.gravitino.exceptions.NoSuchPolicyException;
import org.apache.gravitino.exceptions.PolicyAlreadyExistsException;
+import org.apache.gravitino.meta.PolicyEntity;
/**
* The interface provides functionalities for managing policies within a metalake. It includes a
@@ -48,7 +48,7 @@ public interface PolicyDispatcher {
* @param metalake the name of the metalake
* @return The list of policies.
*/
- Policy[] listPolicyInfos(String metalake);
+ PolicyEntity[] listPolicyInfos(String metalake);
/**
* Get a policy by its name under a metalake.
@@ -58,7 +58,7 @@ public interface PolicyDispatcher {
* @return The policy.
* @throws NoSuchPolicyException If the policy does not exist.
*/
- Policy getPolicy(String metalake, String policyName) throws NoSuchPolicyException;
+ PolicyEntity getPolicy(String metalake, String policyName) throws NoSuchPolicyException;
/**
* Create a built-in type policy under a metalake. The exclusive, inheritable, and supported
@@ -73,52 +73,13 @@ public interface PolicyDispatcher {
* @return The created policy
* @throws PolicyAlreadyExistsException If the policy already exists
*/
- default Policy createPolicy(
+ PolicyEntity createPolicy(
String metalake,
String name,
- String type,
+ Policy.BuiltInType type,
String comment,
boolean enabled,
PolicyContent content)
- throws PolicyAlreadyExistsException {
- Policy.BuiltInType builtInType = Policy.BuiltInType.fromPolicyType(type);
- return createPolicy(
- metalake,
- name,
- type,
- comment,
- enabled,
- builtInType.exclusive(),
- builtInType.inheritable(),
- builtInType.supportedObjectTypes(),
- content);
- }
-
- /**
- * Create a new type policy under a metalake.
- *
- * @param metalake the name of the metalake
- * @param policyName the name of the policy
- * @param type the type of the policy
- * @param comment the comment of the policy
- * @param enabled whether the policy is enabled or not
- * @param exclusive whether the policy is exclusive or not
- * @param inheritable whether the policy is inheritable or not
- * @param supportedObjectTypes the set of supported object types for the policy
- * @param content the content of the policy
- * @return The created policy.
- * @throws PolicyAlreadyExistsException If the policy already exists.
- */
- Policy createPolicy(
- String metalake,
- String policyName,
- String type,
- String comment,
- boolean enabled,
- boolean exclusive,
- boolean inheritable,
- Set<MetadataObject.Type> supportedObjectTypes,
- PolicyContent content)
throws PolicyAlreadyExistsException;
/**
@@ -129,7 +90,7 @@ public interface PolicyDispatcher {
* @param changes the changes to apply to the policy
* @return The updated policy.
*/
- Policy alterPolicy(String metalake, String policyName, PolicyChange... changes);
+ PolicyEntity alterPolicy(String metalake, String policyName, PolicyChange... changes);
/**
* Enable an existing policy under a metalake.
@@ -174,7 +135,7 @@ public interface PolicyDispatcher {
*/
default String[] listPoliciesForMetadataObject(String metalake, MetadataObject metadataObject) {
return Arrays.stream(listPolicyInfosForMetadataObject(metalake, metadataObject))
- .map(Policy::name)
+ .map(PolicyEntity::name)
.toArray(String[]::new);
}
@@ -185,7 +146,7 @@ public interface PolicyDispatcher {
* @param metadataObject the metadata object for which associated policies
* @return The array of policies associated with the specified metadata object.
*/
- Policy[] listPolicyInfosForMetadataObject(String metalake, MetadataObject metadataObject);
+ PolicyEntity[] listPolicyInfosForMetadataObject(String metalake, MetadataObject metadataObject);
/**
* Associate policies to a metadata object under a metalake.
@@ -210,6 +171,6 @@ public interface PolicyDispatcher {
* @param policyName the name of the policy to retrieve
* @return The policy associated with the metadata object.
*/
- Policy getPolicyForMetadataObject(
+ PolicyEntity getPolicyForMetadataObject(
String metalake, MetadataObject metadataObject, String policyName);
}
diff --git a/core/src/main/java/org/apache/gravitino/policy/PolicyManager.java b/core/src/main/java/org/apache/gravitino/policy/PolicyManager.java
index cf67a10aa9..9fb1878f81 100644
--- a/core/src/main/java/org/apache/gravitino/policy/PolicyManager.java
+++ b/core/src/main/java/org/apache/gravitino/policy/PolicyManager.java
@@ -75,11 +75,11 @@ public class PolicyManager implements PolicyDispatcher {
@Override
public String[] listPolicies(String metalake) {
- return Arrays.stream(listPolicyInfos(metalake)).map(Policy::name).toArray(String[]::new);
+ return Arrays.stream(listPolicyInfos(metalake)).map(PolicyEntity::name).toArray(String[]::new);
}
@Override
- public Policy[] listPolicyInfos(String metalake) {
+ public PolicyEntity[] listPolicyInfos(String metalake) {
NameIdentifier metalakeIdent = NameIdentifierUtil.ofMetalake(metalake);
checkMetalake(metalakeIdent, entityStore);
return TreeLockUtils.doWithTreeLock(
@@ -90,7 +90,7 @@ public class PolicyManager implements PolicyDispatcher {
return entityStore
.list(
NamespaceUtil.ofPolicy(metalake), PolicyEntity.class, Entity.EntityType.POLICY)
- .toArray(new Policy[0]);
+ .toArray(new PolicyEntity[0]);
} catch (IOException ioe) {
LOG.error("Failed to list policies under metalake {}", metalake, ioe);
throw new RuntimeException(ioe);
@@ -99,7 +99,7 @@ public class PolicyManager implements PolicyDispatcher {
}
@Override
- public Policy getPolicy(String metalake, String policyName) throws NoSuchPolicyException {
+ public PolicyEntity getPolicy(String metalake, String policyName) throws NoSuchPolicyException {
checkMetalake(NameIdentifier.of(metalake), entityStore);
return TreeLockUtils.doWithTreeLock(
NameIdentifierUtil.ofPolicy(metalake, policyName),
@@ -121,15 +121,12 @@ public class PolicyManager implements PolicyDispatcher {
}
@Override
- public Policy createPolicy(
+ public PolicyEntity createPolicy(
String metalake,
String policyName,
- String type,
+ Policy.BuiltInType type,
String comment,
boolean enabled,
- boolean exclusive,
- boolean inheritable,
- Set<MetadataObject.Type> supportedObjectTypes,
PolicyContent content)
throws PolicyAlreadyExistsException {
NameIdentifier metalakeIdent = NameIdentifierUtil.ofMetalake(metalake);
@@ -146,9 +143,6 @@ public class PolicyManager implements PolicyDispatcher {
.withComment(comment)
.withPolicyType(type)
.withEnabled(enabled)
- .withExclusive(exclusive)
- .withInheritable(inheritable)
- .withSupportedObjectTypes(supportedObjectTypes)
.withContent(content)
.withAuditInfo(
AuditInfo.builder()
@@ -171,7 +165,7 @@ public class PolicyManager implements PolicyDispatcher {
}
@Override
- public Policy alterPolicy(String metalake, String policyName, PolicyChange... changes) {
+ public PolicyEntity alterPolicy(String metalake, String policyName, PolicyChange... changes) {
NameIdentifier metalakeIdent = NameIdentifierUtil.ofMetalake(metalake);
checkMetalake(metalakeIdent, entityStore);
return TreeLockUtils.doWithTreeLock(
@@ -259,15 +253,12 @@ public class PolicyManager implements PolicyDispatcher {
}
@Override
- public Policy[] listPolicyInfosForMetadataObject(String metalake, MetadataObject metadataObject) {
+ public PolicyEntity[] listPolicyInfosForMetadataObject(
+ String metalake, MetadataObject metadataObject) {
NameIdentifier entityIdent = MetadataObjectUtil.toEntityIdent(metalake, metadataObject);
Entity.EntityType entityType = MetadataObjectUtil.toEntityType(metadataObject);
MetadataObjectUtil.checkMetadataObject(metalake, metadataObject);
- Preconditions.checkArgument(
- Policy.SUPPORTS_ALL_OBJECT_TYPES.contains(metadataObject.type()),
- "Cannot list policies for unsupported metadata object type %s",
- metadataObject.type());
checkMetalake(NameIdentifier.of(metalake), entityStore);
return TreeLockUtils.doWithTreeLock(
@@ -303,7 +294,9 @@ public class PolicyManager implements PolicyDispatcher {
String[] policiesToAdd,
String[] policiesToRemove) {
Preconditions.checkArgument(
- Policy.SUPPORTS_ALL_OBJECT_TYPES.contains(metadataObject.type()),
+ !metadataObject.type().equals(MetadataObject.Type.METALAKE)
+ && !metadataObject.type().equals(MetadataObject.Type.ROLE)
+ && !metadataObject.type().equals(MetadataObject.Type.COLUMN),
"Cannot associate policies for unsupported metadata object type %s",
metadataObject.type());
@@ -371,7 +364,7 @@ public class PolicyManager implements PolicyDispatcher {
}
@Override
- public Policy getPolicyForMetadataObject(
+ public PolicyEntity getPolicyForMetadataObject(
String metalake, MetadataObject metadataObject, String policyName) {
NameIdentifier entityIdent = MetadataObjectUtil.toEntityIdent(metalake, metadataObject);
Entity.EntityType entityType = MetadataObjectUtil.toEntityType(metadataObject);
@@ -451,9 +444,6 @@ public class PolicyManager implements PolicyDispatcher {
.withComment(policyEntity.comment())
.withPolicyType(policyEntity.policyType())
.withEnabled(policyEntity.enabled())
- .withExclusive(policyEntity.exclusive())
- .withInheritable(policyEntity.inheritable())
- .withSupportedObjectTypes(policyEntity.supportedObjectTypes())
.withContent(policyEntity.content())
.withAuditInfo(
AuditInfo.builder()
@@ -491,11 +481,24 @@ public class PolicyManager implements PolicyDispatcher {
newComment = ((PolicyChange.UpdatePolicyComment) change).getNewComment();
} else if (change instanceof PolicyChange.UpdateContent) {
PolicyChange.UpdateContent updateContent = (PolicyChange.UpdateContent) change;
+ Policy.BuiltInType policyType =
+ Policy.BuiltInType.fromPolicyType(updateContent.getPolicyType());
Preconditions.checkArgument(
- policyEntity.policyType().equalsIgnoreCase(updateContent.getPolicyType()),
+ policyEntity.policyType() == policyType,
"Policy type mismatch: expected %s but got %s",
policyEntity.policyType(),
updateContent.getPolicyType());
+
+ if (policyType != Policy.BuiltInType.CUSTOM) {
+ // cannot change the supported object types for built-in policies
+ Preconditions.checkArgument(
+ Sets.difference(
+ policyEntity.content().supportedObjectTypes(),
+ updateContent.getContent().supportedObjectTypes())
+ .isEmpty(),
+ "Policy content type mismatch: expected %s but got %s");
+ }
+
newContent = updateContent.getContent();
} else {
throw new IllegalArgumentException("Unsupported policy change: " + change);
diff --git a/core/src/main/java/org/apache/gravitino/storage/relational/mapper/PolicyMetaMapper.java b/core/src/main/java/org/apache/gravitino/storage/relational/mapper/PolicyMetaMapper.java
index 9bbfa345c4..a3493be788 100644
--- a/core/src/main/java/org/apache/gravitino/storage/relational/mapper/PolicyMetaMapper.java
+++ b/core/src/main/java/org/apache/gravitino/storage/relational/mapper/PolicyMetaMapper.java
@@ -60,9 +60,6 @@ public interface PolicyMetaMapper {
@Result(property = "policyName", column = "policy_name"),
@Result(property = "policyType", column = "policy_type"),
@Result(property = "metalakeId", column = "metalake_id"),
- @Result(property = "inheritable", column = "inheritable"),
- @Result(property = "exclusive", column = "exclusive"),
- @Result(property = "supportedObjectTypes", column = "supported_object_types"),
@Result(property = "auditInfo", column = "audit_info"),
@Result(property = "currentVersion", column = "current_version"),
@Result(property = "lastVersion", column = "last_version"),
@@ -95,9 +92,6 @@ public interface PolicyMetaMapper {
@Result(property = "policyName", column = "policy_name"),
@Result(property = "policyType", column = "policy_type"),
@Result(property = "metalakeId", column = "metalake_id"),
- @Result(property = "inheritable", column = "inheritable"),
- @Result(property = "exclusive", column = "exclusive"),
- @Result(property = "supportedObjectTypes", column = "supported_object_types"),
@Result(property = "auditInfo", column = "audit_info"),
@Result(property = "currentVersion", column = "current_version"),
@Result(property = "lastVersion", column = "last_version"),
diff --git a/core/src/main/java/org/apache/gravitino/storage/relational/mapper/PolicyMetadataObjectRelMapper.java b/core/src/main/java/org/apache/gravitino/storage/relational/mapper/PolicyMetadataObjectRelMapper.java
index 08e5a9fa22..43806a352d 100644
--- a/core/src/main/java/org/apache/gravitino/storage/relational/mapper/PolicyMetadataObjectRelMapper.java
+++ b/core/src/main/java/org/apache/gravitino/storage/relational/mapper/PolicyMetadataObjectRelMapper.java
@@ -37,9 +37,6 @@ public interface PolicyMetadataObjectRelMapper {
@Result(property = "policyName", column = "policy_name"),
@Result(property = "policyType", column = "policy_type"),
@Result(property = "metalakeId", column = "metalake_id"),
- @Result(property = "inheritable", column = "inheritable"),
- @Result(property = "exclusive", column = "exclusive"),
- @Result(property = "supportedObjectTypes", column = "supported_object_types"),
@Result(property = "auditInfo", column = "audit_info"),
@Result(property = "currentVersion", column = "current_version"),
@Result(property = "lastVersion", column = "last_version"),
@@ -65,9 +62,6 @@ public interface PolicyMetadataObjectRelMapper {
@Result(property = "policyName", column = "policy_name"),
@Result(property = "policyType", column = "policy_type"),
@Result(property = "metalakeId", column = "metalake_id"),
- @Result(property = "inheritable", column = "inheritable"),
- @Result(property = "exclusive", column = "exclusive"),
- @Result(property = "supportedObjectTypes", column = "supported_object_types"),
@Result(property = "auditInfo", column = "audit_info"),
@Result(property = "currentVersion", column = "current_version"),
@Result(property = "lastVersion", column = "last_version"),
diff --git a/core/src/main/java/org/apache/gravitino/storage/relational/mapper/provider/base/PolicyMetaBaseSQLProvider.java b/core/src/main/java/org/apache/gravitino/storage/relational/mapper/provider/base/PolicyMetaBaseSQLProvider.java
index 65f64a05df..e6a42bd939 100644
--- a/core/src/main/java/org/apache/gravitino/storage/relational/mapper/provider/base/PolicyMetaBaseSQLProvider.java
+++ b/core/src/main/java/org/apache/gravitino/storage/relational/mapper/provider/base/PolicyMetaBaseSQLProvider.java
@@ -29,8 +29,8 @@ import org.apache.ibatis.annotations.Param;
public class PolicyMetaBaseSQLProvider {
public String listPolicyPOsByMetalake(@Param("metalakeName") String metalakeName) {
- return "SELECT pm.policy_id, pm.policy_name, pm.policy_type, pm.metalake_id, pm.inheritable,"
- + " pm.exclusive, pm.supported_object_types, pm.audit_info, pm.current_version, pm.last_version,"
+ return "SELECT pm.policy_id, pm.policy_name, pm.policy_type, pm.metalake_id,"
+ + " pm.audit_info, pm.current_version, pm.last_version,"
+ " pm.deleted_at, pvi.id, pvi.metalake_id as version_metalake_id, pvi.policy_id as version_policy_id,"
+ " pvi.version, pvi.policy_comment, pvi.enabled, pvi.content, pvi.deleted_at as version_deleted_at"
+ " FROM "
@@ -50,8 +50,8 @@ public class PolicyMetaBaseSQLProvider {
public String listPolicyPOsByMetalakeAndPolicyNames(
@Param("metalakeName") String metalakeName, @Param("policyNames") List<String> policyNames) {
return "<script>"
- + "SELECT pm.policy_id, pm.policy_name, pm.policy_type, pm.metalake_id, pm.inheritable,"
- + " pm.exclusive, pm.supported_object_types, pm.audit_info, pm.current_version, pm.last_version,"
+ + "SELECT pm.policy_id, pm.policy_name, pm.policy_type, pm.metalake_id,"
+ + " pm.audit_info, pm.current_version, pm.last_version,"
+ " pm.deleted_at, pvi.id, pvi.metalake_id as version_metalake_id, pvi.policy_id as version_policy_id,"
+ " pvi.version, pvi.policy_comment, pvi.enabled, pvi.content, pvi.deleted_at as version_deleted_at"
+ " FROM "
@@ -76,19 +76,15 @@ public class PolicyMetaBaseSQLProvider {
public String insertPolicyMetaOnDuplicateKeyUpdate(@Param("policyMeta") PolicyPO policyPO) {
return "INSERT INTO "
+ POLICY_META_TABLE_NAME
- + " (policy_id, policy_name, policy_type, metalake_id, inheritable, exclusive,"
- + " supported_object_types, audit_info, current_version, last_version, deleted_at)"
+ + " (policy_id, policy_name, policy_type, metalake_id,"
+ + " audit_info, current_version, last_version, deleted_at)"
+ " VALUES (#{policyMeta.policyId}, #{policyMeta.policyName}, #{policyMeta.policyType},"
- + " #{policyMeta.metalakeId}, #{policyMeta.inheritable}, #{policyMeta.exclusive},"
- + " #{policyMeta.supportedObjectTypes}, #{policyMeta.auditInfo}, #{policyMeta.currentVersion},"
+ + " #{policyMeta.metalakeId}, #{policyMeta.auditInfo}, #{policyMeta.currentVersion},"
+ " #{policyMeta.lastVersion}, #{policyMeta.deletedAt})"
+ " ON DUPLICATE KEY UPDATE"
+ " policy_name = #{policyMeta.policyName},"
+ " policy_type = #{policyMeta.policyType},"
+ " metalake_id = #{policyMeta.metalakeId},"
- + " inheritable = #{policyMeta.inheritable},"
- + " exclusive = #{policyMeta.exclusive},"
- + " supported_object_types = #{policyMeta.supportedObjectTypes},"
+ " audit_info = #{policyMeta.auditInfo},"
+ " current_version = #{policyMeta.currentVersion},"
+ " last_version = #{policyMeta.lastVersion},"
@@ -98,18 +94,17 @@ public class PolicyMetaBaseSQLProvider {
public String insertPolicyMeta(@Param("policyMeta") PolicyPO policyPO) {
return "INSERT INTO "
+ POLICY_META_TABLE_NAME
- + " (policy_id, policy_name, policy_type, metalake_id, inheritable, exclusive,"
- + " supported_object_types, audit_info, current_version, last_version, deleted_at)"
+ + " (policy_id, policy_name, policy_type, metalake_id,"
+ + " audit_info, current_version, last_version, deleted_at)"
+ " VALUES (#{policyMeta.policyId}, #{policyMeta.policyName}, #{policyMeta.policyType},"
- + " #{policyMeta.metalakeId}, #{policyMeta.inheritable}, #{policyMeta.exclusive},"
- + " #{policyMeta.supportedObjectTypes}, #{policyMeta.auditInfo}, #{policyMeta.currentVersion},"
+ + " #{policyMeta.metalakeId}, #{policyMeta.auditInfo}, #{policyMeta.currentVersion},"
+ " #{policyMeta.lastVersion}, #{policyMeta.deletedAt})";
}
public String selectPolicyMetaByMetalakeAndName(
@Param("metalakeName") String metalakeName, @Param("policyName") String policyName) {
- return "SELECT pm.policy_id, pm.policy_name, pm.policy_type, pm.metalake_id, pm.inheritable,"
- + " pm.exclusive, pm.supported_object_types, pm.audit_info, pm.current_version, pm.last_version,"
+ return "SELECT pm.policy_id, pm.policy_name, pm.policy_type, pm.metalake_id,"
+ + " pm.audit_info, pm.current_version, pm.last_version,"
+ " pm.deleted_at, pvi.id, pvi.metalake_id as version_metalake_id, pvi.policy_id as version_policy_id,"
+ " pvi.version, pvi.policy_comment, pvi.enabled, pvi.content, pvi.deleted_at as version_deleted_at"
+ " FROM "
@@ -135,9 +130,6 @@ public class PolicyMetaBaseSQLProvider {
+ " SET policy_name = #{newPolicyMeta.policyName},"
+ " policy_type = #{newPolicyMeta.policyType},"
+ " metalake_id = #{newPolicyMeta.metalakeId},"
- + " inheritable = #{newPolicyMeta.inheritable},"
- + " exclusive = #{newPolicyMeta.exclusive},"
- + " supported_object_types = #{newPolicyMeta.supportedObjectTypes},"
+ " audit_info = #{newPolicyMeta.auditInfo},"
+ " current_version = #{newPolicyMeta.currentVersion},"
+ " last_version = #{newPolicyMeta.lastVersion},"
@@ -146,9 +138,6 @@ public class PolicyMetaBaseSQLProvider {
+ " AND policy_name = #{oldPolicyMeta.policyName}"
+ " AND policy_type = #{oldPolicyMeta.policyType}"
+ " AND metalake_id = #{oldPolicyMeta.metalakeId}"
- + " AND inheritable = #{oldPolicyMeta.inheritable}"
- + " AND exclusive = #{oldPolicyMeta.exclusive}"
- + " AND supported_object_types = #{oldPolicyMeta.supportedObjectTypes}"
+ " AND audit_info = #{oldPolicyMeta.auditInfo}"
+ " AND current_version = #{oldPolicyMeta.currentVersion}"
+ " AND last_version = #{oldPolicyMeta.lastVersion}"
diff --git a/core/src/main/java/org/apache/gravitino/storage/relational/mapper/provider/base/PolicyMetadataObjectRelBaseSQLProvider.java b/core/src/main/java/org/apache/gravitino/storage/relational/mapper/provider/base/PolicyMetadataObjectRelBaseSQLProvider.java
index 29150fe024..5aa4f680b9 100644
--- a/core/src/main/java/org/apache/gravitino/storage/relational/mapper/provider/base/PolicyMetadataObjectRelBaseSQLProvider.java
+++ b/core/src/main/java/org/apache/gravitino/storage/relational/mapper/provider/base/PolicyMetadataObjectRelBaseSQLProvider.java
@@ -37,8 +37,8 @@ public class PolicyMetadataObjectRelBaseSQLProvider {
public String listPolicyPOsByMetadataObjectIdAndType(
@Param("metadataObjectId") Long metadataObjectId,
@Param("metadataObjectType") String metadataObjectType) {
- return "SELECT pm.policy_id, pm.policy_name, pm.policy_type, pm.metalake_id, pm.inheritable,"
- + " pm.exclusive, pm.supported_object_types, pm.audit_info, pm.current_version, pm.last_version,"
+ return "SELECT pm.policy_id, pm.policy_name, pm.policy_type, pm.metalake_id,"
+ + " pm.audit_info, pm.current_version, pm.last_version,"
+ " pm.deleted_at, pvi.id, pvi.metalake_id as version_metalake_id, pvi.policy_id as version_policy_id,"
+ " pvi.version, pvi.policy_comment, pvi.enabled, pvi.content, pvi.deleted_at as version_deleted_at"
+ " FROM "
@@ -58,8 +58,8 @@ public class PolicyMetadataObjectRelBaseSQLProvider {
@Param("metadataObjectId") Long metadataObjectId,
@Param("metadataObjectType") String metadataObjectType,
@Param("policyName") String policyName) {
- return "SELECT pm.policy_id, pm.policy_name, pm.policy_type, pm.metalake_id, pm.inheritable,"
- + " pm.exclusive, pm.supported_object_types, pm.audit_info, pm.current_version, pm.last_version,"
+ return "SELECT pm.policy_id, pm.policy_name, pm.policy_type, pm.metalake_id,"
+ + " pm.audit_info, pm.current_version, pm.last_version,"
+ " pm.deleted_at, pvi.id, pvi.metalake_id as version_metalake_id, pvi.policy_id as version_policy_id,"
+ " pvi.version, pvi.policy_comment, pvi.enabled, pvi.content, pvi.deleted_at as version_deleted_at"
+ " FROM "
diff --git a/core/src/main/java/org/apache/gravitino/storage/relational/mapper/provider/postgresql/PolicyMetaPostgreSQLProvider.java b/core/src/main/java/org/apache/gravitino/storage/relational/mapper/provider/postgresql/PolicyMetaPostgreSQLProvider.java
index 8a2274af9f..7829690a4a 100644
--- a/core/src/main/java/org/apache/gravitino/storage/relational/mapper/provider/postgresql/PolicyMetaPostgreSQLProvider.java
+++ b/core/src/main/java/org/apache/gravitino/storage/relational/mapper/provider/postgresql/PolicyMetaPostgreSQLProvider.java
@@ -58,16 +58,13 @@ public class PolicyMetaPostgreSQLProvider extends PolicyMetaBaseSQLProvider {
public String insertPolicyMetaOnDuplicateKeyUpdate(PolicyPO policyPO) {
return "INSERT INTO "
+ META_TABLE_NAME
- + " (policy_id, policy_name, policy_type, metalake_id, inheritable, exclusive,"
- + " supported_object_types, audit_info, current_version, last_version, deleted_at)"
+ + " (policy_id, policy_name, policy_type, metalake_id,"
+ + " audit_info, current_version, last_version, deleted_at)"
+ " VALUES ("
+ " #{policyPO.policyId},"
+ " #{policyPO.policyName},"
+ " #{policyPO.policyType},"
+ " #{policyPO.metalakeId},"
- + " #{policyPO.inheritable},"
- + " #{policyPO.exclusive},"
- + " #{policyPO.supportedObjectTypes},"
+ " #{policyPO.auditInfo},"
+ " #{policyPO.currentVersion},"
+ " #{policyPO.lastVersion},"
@@ -76,9 +73,6 @@ public class PolicyMetaPostgreSQLProvider extends PolicyMetaBaseSQLProvider {
+ " policy_name = #{policyPO.policyName},"
+ " policy_type = #{policyPO.policyType},"
+ " metalake_id = #{policyPO.metalakeId},"
- + " inheritable = #{policyPO.inheritable},"
- + " exclusive = #{policyPO.exclusive},"
- + " supported_object_types = #{policyPO.supportedObjectTypes},"
+ " audit_info = #{policyPO.auditInfo},"
+ " current_version = #{policyPO.currentVersion},"
+ " last_version = #{policyPO.lastVersion}"
diff --git a/core/src/main/java/org/apache/gravitino/storage/relational/po/PolicyPO.java b/core/src/main/java/org/apache/gravitino/storage/relational/po/PolicyPO.java
index 1241ed1636..6e2cf0dc81 100644
--- a/core/src/main/java/org/apache/gravitino/storage/relational/po/PolicyPO.java
+++ b/core/src/main/java/org/apache/gravitino/storage/relational/po/PolicyPO.java
@@ -28,9 +28,6 @@ public class PolicyPO {
private String policyName;
private String policyType;
private Long metalakeId;
- private boolean inheritable;
- private boolean exclusive;
- private String supportedObjectTypes;
private String auditInfo;
private Long currentVersion;
private Long lastVersion;
@@ -54,9 +51,6 @@ public class PolicyPO {
&& Objects.equal(policyName, policyPO.policyName)
&& Objects.equal(policyType, policyPO.policyType)
&& Objects.equal(metalakeId, policyPO.metalakeId)
- && Objects.equal(inheritable, policyPO.inheritable)
- && Objects.equal(exclusive, policyPO.exclusive)
- && Objects.equal(supportedObjectTypes, policyPO.supportedObjectTypes)
&& Objects.equal(auditInfo, policyPO.auditInfo)
&& Objects.equal(currentVersion, policyPO.currentVersion)
&& Objects.equal(lastVersion, policyPO.lastVersion)
@@ -71,9 +65,6 @@ public class PolicyPO {
policyName,
policyType,
metalakeId,
- inheritable,
- exclusive,
- supportedObjectTypes,
auditInfo,
currentVersion,
lastVersion,
@@ -86,9 +77,6 @@ public class PolicyPO {
private String policyName;
private String policyType;
private Long metalakeId;
- private boolean inheritable;
- private boolean exclusive;
- private String supportedObjectTypes;
private String auditInfo;
private Long currentVersion;
private Long lastVersion;
@@ -115,21 +103,6 @@ public class PolicyPO {
return this;
}
- public Builder withInheritable(boolean inheritable) {
- this.inheritable = inheritable;
- return this;
- }
-
- public Builder withExclusive(boolean exclusive) {
- this.exclusive = exclusive;
- return this;
- }
-
- public Builder withSupportedObjectTypes(String supportedObjectTypes) {
- this.supportedObjectTypes = supportedObjectTypes;
- return this;
- }
-
public Builder withAuditInfo(String auditInfo) {
this.auditInfo = auditInfo;
return this;
@@ -167,9 +140,6 @@ public class PolicyPO {
policyPO.policyName = policyName;
policyPO.metalakeId = metalakeId;
policyPO.policyType = policyType;
- policyPO.inheritable = inheritable;
- policyPO.exclusive = exclusive;
- policyPO.supportedObjectTypes = supportedObjectTypes;
policyPO.auditInfo = auditInfo;
policyPO.currentVersion = currentVersion;
policyPO.lastVersion = lastVersion;
@@ -183,8 +153,6 @@ public class PolicyPO {
Preconditions.checkArgument(policyName != null, "Policy name is required");
Preconditions.checkArgument(metalakeId != null, "Metalake id is required");
Preconditions.checkArgument(policyType != null, "Policy type is required");
- Preconditions.checkArgument(
- supportedObjectTypes != null, "Supported object types is required");
Preconditions.checkArgument(currentVersion != null, "Current version is required");
Preconditions.checkArgument(lastVersion != null, "Last version is required");
Preconditions.checkArgument(deletedAt != null, "Deleted at is required");
diff --git a/core/src/main/java/org/apache/gravitino/storage/relational/service/PolicyMetaService.java b/core/src/main/java/org/apache/gravitino/storage/relational/service/PolicyMetaService.java
index f3c1b4d877..add500f5b1 100644
--- a/core/src/main/java/org/apache/gravitino/storage/relational/service/PolicyMetaService.java
+++ b/core/src/main/java/org/apache/gravitino/storage/relational/service/PolicyMetaService.java
@@ -305,19 +305,6 @@ public class PolicyMetaService {
? Collections.emptyList()
: getPolicyPOsByMetalakeAndNames(metalake, policyNamesToAdd);
- // Check if the policies to add all support the metadata object type.
- policyPOsToAdd.forEach(
- policyPO -> {
- PolicyEntity policy =
- POConverters.fromPolicyPO(policyPO, NamespaceUtil.ofPolicy(metalake));
- if (!policy.supportedObjectTypes().contains(metadataObject.type())) {
- throw new IllegalArgumentException(
- String.format(
- "Cannot associate policies for unsupported metadata object type %s, expected: %s.",
- objectType, policy.supportedObjectTypes()));
- }
- });
-
// Fetch all the policies need to remove from the metadata object.
List<String> policyNamesToRemove =
Arrays.stream(policiesToRemove).map(NameIdentifier::name).collect(Collectors.toList());
diff --git a/core/src/main/java/org/apache/gravitino/storage/relational/utils/POConverters.java b/core/src/main/java/org/apache/gravitino/storage/relational/utils/POConverters.java
index 28d0388745..1376fc39a5 100644
--- a/core/src/main/java/org/apache/gravitino/storage/relational/utils/POConverters.java
+++ b/core/src/main/java/org/apache/gravitino/storage/relational/utils/POConverters.java
@@ -20,15 +20,12 @@
package org.apache.gravitino.storage.relational.utils;
import com.fasterxml.jackson.core.JsonProcessingException;
-import com.fasterxml.jackson.core.type.TypeReference;
import com.google.common.collect.Lists;
import java.time.Instant;
import java.util.Collections;
-import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
-import java.util.Set;
import java.util.stream.Collectors;
import org.apache.commons.lang3.StringUtils;
import org.apache.gravitino.Catalog;
@@ -691,9 +688,7 @@ public class POConverters {
try {
PolicyContent oldContent =
JsonUtils.anyFieldMapper()
- .readValue(
- oldPolicyVersionPO.getContent(),
- Policy.BuiltInType.fromPolicyType(newPolicy.policyType()).contentClass());
+ .readValue(oldPolicyVersionPO.getContent(), newPolicy.policyType().contentClass());
if (oldContent == null) {
return newPolicy.content() != null;
}
@@ -730,18 +725,8 @@ public class POConverters {
return PolicyPO.builder()
.withPolicyId(newPolicy.id())
.withPolicyName(newPolicy.name())
- .withPolicyType(newPolicy.policyType())
+ .withPolicyType(newPolicy.policyType().name())
.withMetalakeId(oldPolicyPO.getMetalakeId())
- .withInheritable(newPolicy.inheritable())
- .withExclusive(newPolicy.exclusive())
- .withSupportedObjectTypes(
- JsonUtils.anyFieldMapper()
- .writeValueAsString(
- // Sort the supported object types to ensure consistent ordering
- newPolicy.supportedObjectTypes().stream()
- .map(Enum::name)
- .sorted()
- .collect(Collectors.toCollection(LinkedHashSet::new))))
.withAuditInfo(JsonUtils.anyFieldMapper().writeValueAsString(newPolicy.auditInfo()))
.withCurrentVersion(currentVersion)
.withLastVersion(lastVersion)
@@ -1388,25 +1373,17 @@ public class POConverters {
public static PolicyEntity fromPolicyPO(PolicyPO policyPO, Namespace namespace) {
try {
+ Policy.BuiltInType policyType = Policy.BuiltInType.fromPolicyType(policyPO.getPolicyType());
return PolicyEntity.builder()
.withId(policyPO.getPolicyId())
.withName(policyPO.getPolicyName())
.withNamespace(namespace)
- .withPolicyType(policyPO.getPolicyType())
+ .withPolicyType(policyType)
.withComment(policyPO.getPolicyVersionPO().getPolicyComment())
.withEnabled(policyPO.getPolicyVersionPO().isEnabled())
- .withExclusive(policyPO.isExclusive())
- .withInheritable(policyPO.isInheritable())
- .withSupportedObjectTypes(
- JsonUtils.anyFieldMapper()
- .readValue(
- policyPO.getSupportedObjectTypes(),
- new TypeReference<Set<MetadataObject.Type>>() {}))
.withContent(
JsonUtils.anyFieldMapper()
- .readValue(
- policyPO.getPolicyVersionPO().getContent(),
- Policy.BuiltInType.fromPolicyType(policyPO.getPolicyType()).contentClass()))
+ .readValue(policyPO.getPolicyVersionPO().getContent(), policyType.contentClass()))
.withAuditInfo(
JsonUtils.anyFieldMapper().readValue(policyPO.getAuditInfo(), AuditInfo.class))
.build();
@@ -1432,17 +1409,7 @@ public class POConverters {
return builder
.withPolicyId(policyEntity.id())
.withPolicyName(policyEntity.name())
- .withPolicyType(policyEntity.policyType())
- .withInheritable(policyEntity.inheritable())
- .withExclusive(policyEntity.exclusive())
- .withSupportedObjectTypes(
- JsonUtils.anyFieldMapper()
- .writeValueAsString(
- // Sort the supported object types to ensure consistent ordering
- policyEntity.supportedObjectTypes().stream()
- .map(Enum::name)
- .sorted()
- .collect(Collectors.toCollection(LinkedHashSet::new))))
+ .withPolicyType(policyEntity.policyType().name())
.withAuditInfo(JsonUtils.anyFieldMapper().writeValueAsString(policyEntity.auditInfo()))
.withCurrentVersion(INIT_VERSION)
.withLastVersion(INIT_VERSION)
diff --git a/core/src/test/java/org/apache/gravitino/meta/TestPolicyEntity.java b/core/src/test/java/org/apache/gravitino/meta/TestPolicyEntity.java
index dfbd33a85e..94fd8a86cb 100644
--- a/core/src/test/java/org/apache/gravitino/meta/TestPolicyEntity.java
+++ b/core/src/test/java/org/apache/gravitino/meta/TestPolicyEntity.java
@@ -18,12 +18,13 @@
*/
package org.apache.gravitino.meta;
-import static org.apache.gravitino.policy.Policy.SUPPORTS_ALL_OBJECT_TYPES;
-
import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.ImmutableSet;
import java.time.Instant;
import java.util.Map;
+import org.apache.gravitino.MetadataObject;
import org.apache.gravitino.Namespace;
+import org.apache.gravitino.policy.Policy;
import org.apache.gravitino.policy.PolicyContent;
import org.apache.gravitino.policy.PolicyContents;
import org.junit.jupiter.api.Assertions;
@@ -39,18 +40,19 @@ public class TestPolicyEntity {
ImmutableMap<String, Object> contentFields = ImmutableMap.of("target_file_size_bytes", 1000);
Namespace namespace = Namespace.of("m1", "c1", "s1");
- PolicyContent content = PolicyContents.custom(contentFields, properties);
+ PolicyContent content =
+ PolicyContents.custom(
+ contentFields,
+ ImmutableSet.of(MetadataObject.Type.MODEL, MetadataObject.Type.TOPIC),
+ properties);
PolicyEntity policyEntity =
PolicyEntity.builder()
.withId(1L)
.withName("test")
.withNamespace(namespace)
.withComment("test comment")
- .withPolicyType("my_compaction")
+ .withPolicyType(Policy.BuiltInType.CUSTOM)
.withEnabled(false)
- .withExclusive(true)
- .withInheritable(true)
- .withSupportedObjectTypes(SUPPORTS_ALL_OBJECT_TYPES)
.withContent(content)
.withAuditInfo(auditInfo)
.build();
@@ -59,11 +61,8 @@ public class TestPolicyEntity {
Assertions.assertEquals("test", policyEntity.name());
Assertions.assertEquals(namespace, policyEntity.namespace());
Assertions.assertEquals("test comment", policyEntity.comment());
- Assertions.assertEquals("my_compaction", policyEntity.policyType());
+ Assertions.assertEquals(Policy.BuiltInType.CUSTOM, policyEntity.policyType());
Assertions.assertFalse(policyEntity.enabled());
- Assertions.assertTrue(policyEntity.exclusive());
- Assertions.assertTrue(policyEntity.inheritable());
- Assertions.assertEquals(SUPPORTS_ALL_OBJECT_TYPES, policyEntity.supportedObjectTypes());
Assertions.assertEquals(content, policyEntity.content());
Assertions.assertEquals(auditInfo, policyEntity.auditInfo());
@@ -72,11 +71,8 @@ public class TestPolicyEntity {
.withId(1L)
.withName("test")
.withNamespace(namespace)
- .withPolicyType("my_compaction")
+ .withPolicyType(Policy.BuiltInType.CUSTOM)
.withEnabled(false)
- .withExclusive(true)
- .withInheritable(true)
- .withSupportedObjectTypes(SUPPORTS_ALL_OBJECT_TYPES)
.withContent(content)
.withAuditInfo(auditInfo)
.build();
@@ -104,7 +100,7 @@ public class TestPolicyEntity {
.withId(1L)
.withName("test")
.withNamespace(Namespace.of("m1", "c1", "s1"))
- .withPolicyType("my_compaction")
+ .withPolicyType(Policy.BuiltInType.CUSTOM)
.withAuditInfo(
AuditInfo.builder().withCreator("test").withCreateTime(Instant.now()).build())
.build());
@@ -116,8 +112,7 @@ public class TestPolicyEntity {
.withId(1L)
.withName("test")
.withNamespace(Namespace.of("m1", "c1", "s1"))
- .withPolicyType("my_compaction")
- .withSupportedObjectTypes(SUPPORTS_ALL_OBJECT_TYPES)
+ .withPolicyType(Policy.BuiltInType.CUSTOM)
.withAuditInfo(
AuditInfo.builder().withCreator("test").withCreateTime(Instant.now()).build())
.build());
diff --git a/core/src/test/java/org/apache/gravitino/policy/TestPolicyManager.java b/core/src/test/java/org/apache/gravitino/policy/TestPolicyManager.java
index dc92e25415..aafe2adc5f 100644
--- a/core/src/test/java/org/apache/gravitino/policy/TestPolicyManager.java
+++ b/core/src/test/java/org/apache/gravitino/policy/TestPolicyManager.java
@@ -33,8 +33,6 @@ import static org.apache.gravitino.Configs.TREE_LOCK_CLEAN_INTERVAL;
import static org.apache.gravitino.Configs.TREE_LOCK_MAX_NODE_IN_MEMORY;
import static org.apache.gravitino.Configs.TREE_LOCK_MIN_NODE_IN_MEMORY;
import static org.apache.gravitino.Configs.VERSION_RETENTION_COUNT;
-import static org.apache.gravitino.policy.Policy.BUILT_IN_TYPE_PREFIX;
-import static org.apache.gravitino.policy.Policy.SUPPORTS_ALL_OBJECT_TYPES;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@@ -74,6 +72,7 @@ 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.PolicyEntity;
import org.apache.gravitino.meta.SchemaEntity;
import org.apache.gravitino.meta.SchemaVersion;
import org.apache.gravitino.meta.TableEntity;
@@ -102,6 +101,9 @@ public class TestPolicyManager {
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 Set<MetadataObject.Type> SUPPORTS_OBJECT_TYPES =
+ ImmutableSet.of(
+ MetadataObject.Type.CATALOG, MetadataObject.Type.SCHEMA, MetadataObject.Type.TABLE);
private static PolicyManager policyManager;
@@ -222,20 +224,17 @@ public class TestPolicyManager {
public void testCreateAndGetPolicy() {
String policyName = "policy_" + UUID.randomUUID().toString().replace("-", "");
Map<String, Object> customRules = ImmutableMap.of("rule1", 1, "rule2", "value2");
- PolicyContent content = PolicyContents.custom(customRules, null);
- Policy policy = createCustomPolicy(METALAKE, policyName, content);
+ PolicyContent content = PolicyContents.custom(customRules, SUPPORTS_OBJECT_TYPES, null);
+ PolicyEntity policy = createCustomPolicy(METALAKE, policyName, content);
Assertions.assertEquals(policyName, policy.name());
- Assertions.assertEquals("test", policy.policyType());
+ Assertions.assertEquals(Policy.BuiltInType.CUSTOM, policy.policyType());
Assertions.assertNull(policy.comment());
Assertions.assertTrue(policy.enabled());
- Assertions.assertTrue(policy.exclusive());
- Assertions.assertTrue(policy.inheritable());
- Assertions.assertEquals(SUPPORTS_ALL_OBJECT_TYPES, policy.supportedObjectTypes());
Assertions.assertNotNull(policy.content());
Assertions.assertEquals(content, policy.content());
- Policy policy1 = policyManager.getPolicy(METALAKE, policyName);
+ PolicyEntity policy1 = policyManager.getPolicy(METALAKE, policyName);
Assertions.assertEquals(policy, policy1);
// Create a policy in non-existent metalake
@@ -245,21 +244,6 @@ public class TestPolicyManager {
() -> createCustomPolicy("non_existent_metalake", policyName, content));
Assertions.assertEquals("Metalake non_existent_metalake does not exist", e.getMessage());
- // create a policy with non-existent built-in type
- e =
- Assertions.assertThrows(
- IllegalArgumentException.class,
- () ->
- policyManager.createPolicy(
- METALAKE,
- policyName,
- BUILT_IN_TYPE_PREFIX + "abc",
- null,
- true,
- PolicyContents.custom(null, null)));
- Assertions.assertEquals(
- "Unknown built-in policy type: " + BUILT_IN_TYPE_PREFIX + "abc", e.getMessage());
-
// Create an existent policy
e =
Assertions.assertThrows(
@@ -287,7 +271,7 @@ public class TestPolicyManager {
String policyName2 = "policy2" + UUID.randomUUID().toString().replace("-", "");
String policyName3 = "policy3" + UUID.randomUUID().toString().replace("-", "");
Map<String, Object> customRules = ImmutableMap.of("rule1", 1, "rule2", "value2");
- PolicyContent content = PolicyContents.custom(customRules, null);
+ PolicyContent content = PolicyContents.custom(customRules, SUPPORTS_OBJECT_TYPES, null);
Set<String> expectedPolicyNames = ImmutableSet.of(policyName1, policyName2, policyName3);
expectedPolicyNames.forEach(policyName -> createCustomPolicy(METALAKE, policyName, content));
@@ -308,55 +292,53 @@ public class TestPolicyManager {
public void testAlterPolicy() {
String policyName = "policy1" + UUID.randomUUID().toString().replace("-", "");
Map<String, Object> customRules = ImmutableMap.of("rule1", 1, "rule2", "value2");
- PolicyContent content = PolicyContents.custom(customRules, null);
+ PolicyContent content = PolicyContents.custom(customRules, SUPPORTS_OBJECT_TYPES, null);
createCustomPolicy(METALAKE, policyName, content);
// test rename policy
String newName = "new_policy1" + UUID.randomUUID().toString().replace("-", "");
PolicyChange rename = PolicyChange.rename(newName);
- Policy renamedPolicy = policyManager.alterPolicy(METALAKE, policyName, rename);
+ PolicyEntity renamedPolicy = policyManager.alterPolicy(METALAKE, policyName, rename);
Assertions.assertEquals(newName, renamedPolicy.name());
- Assertions.assertEquals("test", renamedPolicy.policyType());
+ Assertions.assertEquals(Policy.BuiltInType.CUSTOM, renamedPolicy.policyType());
Assertions.assertNull(renamedPolicy.comment());
Assertions.assertTrue(renamedPolicy.enabled());
- Assertions.assertTrue(renamedPolicy.exclusive());
- Assertions.assertTrue(renamedPolicy.inheritable());
- Assertions.assertEquals(SUPPORTS_ALL_OBJECT_TYPES, renamedPolicy.supportedObjectTypes());
Assertions.assertEquals(content, renamedPolicy.content());
// test change comment
PolicyChange commentChange = PolicyChange.updateComment("new comment");
- Policy changedCommentPolicy =
+ PolicyEntity changedCommentPolicy =
policyManager.alterPolicy(METALAKE, renamedPolicy.name(), commentChange);
Assertions.assertEquals("new comment", changedCommentPolicy.comment());
// test update content
Map<String, Object> newCustomRules = ImmutableMap.of("rule3", 1, "rule4", "value2");
- PolicyContent newContent = PolicyContents.custom(newCustomRules, null);
- PolicyChange contentChange = PolicyChange.updateContent("test", newContent);
- Policy updatedContentPolicy =
+ PolicyContent newContent = PolicyContents.custom(newCustomRules, SUPPORTS_OBJECT_TYPES, null);
+ PolicyChange contentChange = PolicyChange.updateContent("custom", newContent);
+ PolicyEntity updatedContentPolicy =
policyManager.alterPolicy(METALAKE, changedCommentPolicy.name(), contentChange);
Assertions.assertEquals(newContent, updatedContentPolicy.content());
- // test policy type mismatch
- PolicyChange typeChange = PolicyChange.updateContent("mismatch_type", newContent);
+ // test wrong policy type
+ PolicyChange typeChange = PolicyChange.updateContent("wrong_type", newContent);
Exception e =
Assertions.assertThrows(
IllegalArgumentException.class,
() -> policyManager.alterPolicy(METALAKE, updatedContentPolicy.name(), typeChange));
Assertions.assertEquals(
- "Policy type mismatch: expected test but got mismatch_type", e.getMessage());
+ "Unknown policy type: wrong_type, it should start with 'system_' or be 'custom'",
+ e.getMessage());
// test disable policy
Assertions.assertDoesNotThrow(
() -> policyManager.disablePolicy(METALAKE, renamedPolicy.name()));
- Policy disabledPolicy = policyManager.getPolicy(METALAKE, renamedPolicy.name());
+ PolicyEntity disabledPolicy = policyManager.getPolicy(METALAKE, renamedPolicy.name());
Assertions.assertFalse(disabledPolicy.enabled());
// test enable policy
Assertions.assertDoesNotThrow(() -> policyManager.enablePolicy(METALAKE, renamedPolicy.name()));
- Policy enabledPolicy = policyManager.getPolicy(METALAKE, renamedPolicy.name());
+ PolicyEntity enabledPolicy = policyManager.getPolicy(METALAKE, renamedPolicy.name());
Assertions.assertTrue(enabledPolicy.enabled());
}
@@ -364,7 +346,7 @@ public class TestPolicyManager {
public void testDeletePolicy() {
String policyName = "policy1" + UUID.randomUUID().toString().replace("-", "");
Map<String, Object> customRules = ImmutableMap.of("rule1", 1, "rule2", "value2");
- PolicyContent content = PolicyContents.custom(customRules, null);
+ PolicyContent content = PolicyContents.custom(customRules, SUPPORTS_OBJECT_TYPES, null);
createCustomPolicy(METALAKE, policyName, content);
// delete the policy
@@ -387,7 +369,7 @@ public class TestPolicyManager {
@Test
public void testAssociatePoliciesForMetadataObject() {
Map<String, Object> customRules = ImmutableMap.of("rule1", 1, "rule2", "value2");
- PolicyContent content = PolicyContents.custom(customRules, null);
+ PolicyContent content = PolicyContents.custom(customRules, SUPPORTS_OBJECT_TYPES, null);
String policyName1 = "policy1" + UUID.randomUUID().toString().replace("-", "");
createCustomPolicy(METALAKE, policyName1, content);
String policyName2 = "policy2" + UUID.randomUUID().toString().replace("-", "");
@@ -474,16 +456,6 @@ public class TestPolicyManager {
e2.getMessage().contains("Cannot associate policies for unsupported metadata object type"),
"Actual message: " + e2.getMessage());
- MetadataObject columnObject =
- NameIdentifierUtil.toMetadataObject(
- NameIdentifierUtil.ofColumn(METALAKE, CATALOG, SCHEMA, TABLE, COLUMN),
- Entity.EntityType.COLUMN);
- e2 =
- Assertions.assertThrows(
- IllegalArgumentException.class,
- () ->
- policyManager.associatePoliciesForMetadataObject(
- METALAKE, columnObject, policiesToAdd, null));
Assertions.assertTrue(
e2.getMessage().contains("Cannot associate policies for unsupported metadata object type"),
"Actual message: " + e2.getMessage());
@@ -527,7 +499,7 @@ public class TestPolicyManager {
@Test
public void testListMetadataObjectsForPolicy() {
Map<String, Object> customRules = ImmutableMap.of("rule1", 1, "rule2", "value2");
- PolicyContent content = PolicyContents.custom(customRules, null);
+ PolicyContent content = PolicyContents.custom(customRules, SUPPORTS_OBJECT_TYPES, null);
String policyName1 = "policy1" + UUID.randomUUID().toString().replace("-", "");
createCustomPolicy(METALAKE, policyName1, content);
String policyName2 = "policy2" + UUID.randomUUID().toString().replace("-", "");
@@ -582,13 +554,13 @@ public class TestPolicyManager {
@Test
public void testListPoliciesForMetadataObject() {
Map<String, Object> customRules = ImmutableMap.of("rule1", 1, "rule2", "value2");
- PolicyContent content = PolicyContents.custom(customRules, null);
+ PolicyContent content = PolicyContents.custom(customRules, SUPPORTS_OBJECT_TYPES, null);
String policyName1 = "policy1" + UUID.randomUUID().toString().replace("-", "");
- Policy policy1 = createCustomPolicy(METALAKE, policyName1, content);
+ PolicyEntity policy1 = createCustomPolicy(METALAKE, policyName1, content);
String policyName2 = "policy2" + UUID.randomUUID().toString().replace("-", "");
- Policy policy2 = createCustomPolicy(METALAKE, policyName2, content);
+ PolicyEntity policy2 = createCustomPolicy(METALAKE, policyName2, content);
String policyName3 = "policy3" + UUID.randomUUID().toString().replace("-", "");
- Policy policy3 = createCustomPolicy(METALAKE, policyName3, content);
+ PolicyEntity policy3 = createCustomPolicy(METALAKE, policyName3, content);
MetadataObject catalogObject =
NameIdentifierUtil.toMetadataObject(
@@ -615,7 +587,8 @@ public class TestPolicyManager {
Assertions.assertEquals(
ImmutableSet.of(policyName1, policyName2, policyName3), ImmutableSet.copyOf(policies));
- Policy[] policiesInfo = policyManager.listPolicyInfosForMetadataObject(METALAKE, catalogObject);
+ PolicyEntity[] policiesInfo =
+ policyManager.listPolicyInfosForMetadataObject(METALAKE, catalogObject);
Assertions.assertEquals(3, policiesInfo.length);
Assertions.assertEquals(
ImmutableSet.of(policy1, policy2, policy3), ImmutableSet.copyOf(policiesInfo));
@@ -625,7 +598,8 @@ public class TestPolicyManager {
Assertions.assertEquals(
ImmutableSet.of(policyName1, policyName2), ImmutableSet.copyOf(policies1));
- Policy[] policiesInfo1 = policyManager.listPolicyInfosForMetadataObject(METALAKE, schemaObject);
+ PolicyEntity[] policiesInfo1 =
+ policyManager.listPolicyInfosForMetadataObject(METALAKE, schemaObject);
Assertions.assertEquals(2, policiesInfo1.length);
Assertions.assertEquals(ImmutableSet.of(policy1, policy2), ImmutableSet.copyOf(policiesInfo1));
@@ -633,7 +607,8 @@ public class TestPolicyManager {
Assertions.assertEquals(1, policies2.length);
Assertions.assertEquals(ImmutableSet.of(policyName1), ImmutableSet.copyOf(policies2));
- Policy[] policiesInfo2 = policyManager.listPolicyInfosForMetadataObject(METALAKE, tableObject);
+ PolicyEntity[] policiesInfo2 =
+ policyManager.listPolicyInfosForMetadataObject(METALAKE, tableObject);
Assertions.assertEquals(1, policiesInfo2.length);
Assertions.assertEquals(ImmutableSet.of(policy1), ImmutableSet.copyOf(policiesInfo2));
@@ -654,13 +629,13 @@ public class TestPolicyManager {
@Test
public void testGetPolicyForMetadataObject() {
Map<String, Object> customRules = ImmutableMap.of("rule1", 1, "rule2", "value2");
- PolicyContent content = PolicyContents.custom(customRules, null);
+ PolicyContent content = PolicyContents.custom(customRules, SUPPORTS_OBJECT_TYPES, null);
String policyName1 = "policy1" + UUID.randomUUID().toString().replace("-", "");
- Policy policy1 = createCustomPolicy(METALAKE, policyName1, content);
+ PolicyEntity policy1 = createCustomPolicy(METALAKE, policyName1, content);
String policyName2 = "policy2" + UUID.randomUUID().toString().replace("-", "");
- Policy policy2 = createCustomPolicy(METALAKE, policyName2, content);
+ PolicyEntity policy2 = createCustomPolicy(METALAKE, policyName2, content);
String policyName3 = "policy3" + UUID.randomUUID().toString().replace("-", "");
- Policy policy3 = createCustomPolicy(METALAKE, policyName3, content);
+ PolicyEntity policy3 = createCustomPolicy(METALAKE, policyName3, content);
MetadataObject catalogObject =
NameIdentifierUtil.toMetadataObject(
@@ -679,21 +654,23 @@ public class TestPolicyManager {
policyManager.associatePoliciesForMetadataObject(
METALAKE, tableObject, new String[] {policyName1}, null);
- Policy result = policyManager.getPolicyForMetadataObject(METALAKE, catalogObject, policyName1);
+ PolicyEntity result =
+ policyManager.getPolicyForMetadataObject(METALAKE, catalogObject, policyName1);
Assertions.assertEquals(policy1, result);
- Policy result1 = policyManager.getPolicyForMetadataObject(METALAKE, schemaObject, policyName1);
+ PolicyEntity result1 =
+ policyManager.getPolicyForMetadataObject(METALAKE, schemaObject, policyName1);
Assertions.assertEquals(policy1, result1);
- Policy result2 =
+ PolicyEntity result2 =
policyManager.getPolicyForMetadataObject(METALAKE, schemaObject, policy2.name());
Assertions.assertEquals(policy2, result2);
- Policy result3 =
+ PolicyEntity result3 =
policyManager.getPolicyForMetadataObject(METALAKE, catalogObject, policy3.name());
Assertions.assertEquals(policy3, result3);
- Policy result4 =
+ PolicyEntity result4 =
policyManager.getPolicyForMetadataObject(METALAKE, tableObject, policy1.name());
Assertions.assertEquals(policy1, result4);
@@ -737,17 +714,9 @@ public class TestPolicyManager {
e3.getMessage().contains("Failed to get policy for metadata object " + nonExistentObject));
}
- private Policy createCustomPolicy(
+ private PolicyEntity createCustomPolicy(
String metalakeName, String policyName, PolicyContent policyContent) {
return policyManager.createPolicy(
- metalakeName,
- policyName,
- "test",
- null,
- true,
- true,
- true,
- SUPPORTS_ALL_OBJECT_TYPES,
- policyContent);
+ metalakeName, policyName, Policy.BuiltInType.CUSTOM, null, true, policyContent);
}
}
diff --git a/core/src/test/java/org/apache/gravitino/storage/relational/TestJDBCBackend.java b/core/src/test/java/org/apache/gravitino/storage/relational/TestJDBCBackend.java
index 4b00d377a1..a552598709 100644
--- a/core/src/test/java/org/apache/gravitino/storage/relational/TestJDBCBackend.java
+++ b/core/src/test/java/org/apache/gravitino/storage/relational/TestJDBCBackend.java
@@ -30,7 +30,6 @@ import static org.apache.gravitino.Configs.ENTITY_STORE;
import static org.apache.gravitino.Configs.RELATIONAL_ENTITY_STORE;
import static org.apache.gravitino.SupportsRelationOperations.Type.OWNER_REL;
import static org.apache.gravitino.file.Fileset.LOCATION_NAME_UNKNOWN;
-import static org.apache.gravitino.policy.Policy.SUPPORTS_ALL_OBJECT_TYPES;
import static org.junit.Assert.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
@@ -39,6 +38,7 @@ import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;
import java.io.File;
import java.io.IOException;
@@ -55,12 +55,14 @@ import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
+import java.util.Set;
import java.util.UUID;
import org.apache.gravitino.Catalog;
import org.apache.gravitino.Config;
import org.apache.gravitino.Configs;
import org.apache.gravitino.Entity;
import org.apache.gravitino.EntityAlreadyExistsException;
+import org.apache.gravitino.MetadataObject;
import org.apache.gravitino.NameIdentifier;
import org.apache.gravitino.Namespace;
import org.apache.gravitino.authorization.AuthorizationUtils;
@@ -83,6 +85,7 @@ 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.policy.Policy;
import org.apache.gravitino.policy.PolicyContents;
import org.apache.gravitino.storage.RandomIdGenerator;
import org.apache.gravitino.storage.relational.mapper.CatalogMetaMapper;
@@ -108,6 +111,15 @@ import org.mockito.Mockito;
// Avoid re-executing tests in subclasses
@EnabledIf("isDirectlyRunningThisClass")
public class TestJDBCBackend {
+ private static final Set<MetadataObject.Type> SUPPORTED_OBJECT_TYPES =
+ ImmutableSet.of(
+ MetadataObject.Type.CATALOG,
+ MetadataObject.Type.SCHEMA,
+ MetadataObject.Type.TABLE,
+ MetadataObject.Type.FILESET,
+ MetadataObject.Type.MODEL,
+ MetadataObject.Type.TOPIC);
+
private final String JDBC_STORE_PATH =
"/tmp/gravitino_jdbc_entityStore_" + UUID.randomUUID().toString().replace("-", "");
private final String DB_DIR = JDBC_STORE_PATH + "/testdb";
@@ -615,9 +627,6 @@ public class TestJDBCBackend {
.withPolicyType(policy.policyType())
.withComment(policy.comment())
.withEnabled(!policy.enabled())
- .withExclusive(policy.exclusive())
- .withInheritable(policy.inheritable())
- .withSupportedObjectTypes(policy.supportedObjectTypes())
.withContent(policy.content())
.withAuditInfo(auditInfo)
.build();
@@ -744,9 +753,6 @@ public class TestJDBCBackend {
.withPolicyType(anotherPolicy.policyType())
.withComment(anotherPolicy.comment())
.withEnabled(!anotherPolicy.enabled())
- .withExclusive(anotherPolicy.exclusive())
- .withInheritable(anotherPolicy.inheritable())
- .withSupportedObjectTypes(anotherPolicy.supportedObjectTypes())
.withContent(anotherPolicy.content())
.withAuditInfo(auditInfo)
.build();
@@ -760,9 +766,6 @@ public class TestJDBCBackend {
.withPolicyType(anotherPolicy.policyType())
.withComment("v3")
.withEnabled(anotherPolicyV2.enabled())
- .withExclusive(anotherPolicy.exclusive())
- .withInheritable(anotherPolicy.inheritable())
- .withSupportedObjectTypes(SUPPORTS_ALL_OBJECT_TYPES)
.withContent(anotherPolicy.content())
.withAuditInfo(auditInfo)
.build();
@@ -1558,13 +1561,11 @@ public class TestJDBCBackend {
.withId(id)
.withNamespace(ns)
.withName(name)
- .withPolicyType("test")
+ .withPolicyType(Policy.BuiltInType.CUSTOM)
.withComment("")
.withEnabled(true)
- .withExclusive(true)
- .withInheritable(true)
- .withSupportedObjectTypes(SUPPORTS_ALL_OBJECT_TYPES)
- .withContent(PolicyContents.custom(ImmutableMap.of("filed1", 123), null))
+ .withContent(
+ PolicyContents.custom(ImmutableMap.of("filed1", 123), SUPPORTED_OBJECT_TYPES, null))
.withAuditInfo(auditInfo)
.build();
}
diff --git a/core/src/test/java/org/apache/gravitino/storage/relational/service/TestPolicyMetaService.java b/core/src/test/java/org/apache/gravitino/storage/relational/service/TestPolicyMetaService.java
index 56e694004d..4c906fc148 100644
--- a/core/src/test/java/org/apache/gravitino/storage/relational/service/TestPolicyMetaService.java
+++ b/core/src/test/java/org/apache/gravitino/storage/relational/service/TestPolicyMetaService.java
@@ -45,6 +45,7 @@ import org.apache.gravitino.meta.PolicyEntity;
import org.apache.gravitino.meta.SchemaEntity;
import org.apache.gravitino.meta.TableEntity;
import org.apache.gravitino.meta.TopicEntity;
+import org.apache.gravitino.policy.Policy;
import org.apache.gravitino.policy.PolicyContent;
import org.apache.gravitino.policy.PolicyContents;
import org.apache.gravitino.storage.RandomIdGenerator;
@@ -62,8 +63,6 @@ public class TestPolicyMetaService extends TestJDBCBackend {
private final AuditInfo auditInfo =
AuditInfo.builder().withCreator("creator").withCreateTime(Instant.now()).build();
- private final PolicyContent content = PolicyContents.custom(ImmutableMap.of("filed1", 123), null);
-
private final Set<MetadataObject.Type> supportedObjectTypes =
ImmutableSet.of(
MetadataObject.Type.CATALOG,
@@ -72,6 +71,8 @@ public class TestPolicyMetaService extends TestJDBCBackend {
MetadataObject.Type.FILESET,
MetadataObject.Type.MODEL,
MetadataObject.Type.TOPIC);
+ private final PolicyContent content =
+ PolicyContents.custom(ImmutableMap.of("filed1", 123), supportedObjectTypes, null);
@Test
public void testInsertAndGetPolicyByIdentifier() throws IOException {
@@ -96,10 +97,9 @@ public class TestPolicyMetaService extends TestJDBCBackend {
.withName("policy1")
.withNamespace(NamespaceUtil.ofPolicy(metalakeName))
.withComment("comment")
- .withPolicyType("test")
+ .withPolicyType(Policy.BuiltInType.CUSTOM)
.withContent(content)
.withEnabled(true)
- .withSupportedObjectTypes(supportedObjectTypes)
.withAuditInfo(auditInfo)
.build();
policyMetaService.insertPolicy(policyEntity, false);
@@ -114,10 +114,9 @@ public class TestPolicyMetaService extends TestJDBCBackend {
PolicyEntity.builder()
.withId(RandomIdGenerator.INSTANCE.nextId())
.withName("policy2")
- .withPolicyType("test")
+ .withPolicyType(Policy.BuiltInType.CUSTOM)
.withNamespace(NamespaceUtil.ofPolicy(metalakeName))
.withAuditInfo(auditInfo)
- .withSupportedObjectTypes(supportedObjectTypes)
.withContent(content)
.build();
@@ -128,7 +127,6 @@ public class TestPolicyMetaService extends TestJDBCBackend {
Assertions.assertEquals(PolicyEntity1, resultPolicyEntity1);
Assertions.assertTrue(resultPolicyEntity1.enabled());
Assertions.assertNull(resultPolicyEntity1.comment());
- Assertions.assertEquals(supportedObjectTypes, resultPolicyEntity1.supportedObjectTypes());
Assertions.assertNull(resultPolicyEntity1.content().properties());
// Test insert with overwrite.
@@ -138,8 +136,7 @@ public class TestPolicyMetaService extends TestJDBCBackend {
.withName("policy3")
.withNamespace(NamespaceUtil.ofPolicy(metalakeName))
.withComment("comment")
- .withPolicyType("test")
- .withSupportedObjectTypes(supportedObjectTypes)
+ .withPolicyType(Policy.BuiltInType.CUSTOM)
.withContent(content)
.withAuditInfo(auditInfo)
.build();
@@ -168,9 +165,8 @@ public class TestPolicyMetaService extends TestJDBCBackend {
.withName("policy1")
.withNamespace(NamespaceUtil.ofPolicy(metalakeName))
.withComment("comment")
- .withPolicyType("test")
+ .withPolicyType(Policy.BuiltInType.CUSTOM)
.withContent(content)
- .withSupportedObjectTypes(supportedObjectTypes)
.withAuditInfo(auditInfo)
.build();
policyMetaService.insertPolicy(policyEntity1, false);
@@ -181,9 +177,8 @@ public class TestPolicyMetaService extends TestJDBCBackend {
.withName("policy2")
.withNamespace(NamespaceUtil.ofPolicy(metalakeName))
.withComment("comment")
- .withPolicyType("test")
+ .withPolicyType(Policy.BuiltInType.CUSTOM)
.withContent(content)
- .withSupportedObjectTypes(supportedObjectTypes)
.withAuditInfo(auditInfo)
.build();
policyMetaService.insertPolicy(policyEntity2, false);
@@ -208,8 +203,7 @@ public class TestPolicyMetaService extends TestJDBCBackend {
.withName("policy1")
.withNamespace(NamespaceUtil.ofPolicy(metalakeName))
.withComment("comment")
- .withPolicyType("test")
- .withSupportedObjectTypes(supportedObjectTypes)
+ .withPolicyType(Policy.BuiltInType.CUSTOM)
.withContent(content)
.withAuditInfo(auditInfo)
.build();
@@ -232,8 +226,7 @@ public class TestPolicyMetaService extends TestJDBCBackend {
.withName("policy1")
.withNamespace(NamespaceUtil.ofPolicy(metalakeName))
.withComment("comment1")
- .withPolicyType("test")
- .withSupportedObjectTypes(supportedObjectTypes)
+ .withPolicyType(Policy.BuiltInType.CUSTOM)
.withContent(content)
.withAuditInfo(auditInfo)
.build();
@@ -254,8 +247,7 @@ public class TestPolicyMetaService extends TestJDBCBackend {
.withName("policy1")
.withNamespace(NamespaceUtil.ofPolicy(metalakeName))
.withComment("comment1")
- .withPolicyType("test")
- .withSupportedObjectTypes(supportedObjectTypes)
+ .withPolicyType(Policy.BuiltInType.CUSTOM)
.withContent(content)
.withAuditInfo(auditInfo)
.build();
@@ -294,8 +286,7 @@ public class TestPolicyMetaService extends TestJDBCBackend {
.withName("policy1")
.withNamespace(NamespaceUtil.ofPolicy(metalakeName))
.withComment("comment")
- .withPolicyType("test")
- .withSupportedObjectTypes(supportedObjectTypes)
+ .withPolicyType(Policy.BuiltInType.CUSTOM)
.withContent(content)
.withAuditInfo(auditInfo)
.build();
@@ -330,8 +321,7 @@ public class TestPolicyMetaService extends TestJDBCBackend {
.withName("policy1")
.withNamespace(NamespaceUtil.ofPolicy(metalakeName))
.withComment("comment")
- .withPolicyType("test")
- .withSupportedObjectTypes(supportedObjectTypes)
+ .withPolicyType(Policy.BuiltInType.CUSTOM)
.withContent(content)
.withAuditInfo(auditInfo)
.build();
@@ -356,8 +346,7 @@ public class TestPolicyMetaService extends TestJDBCBackend {
.withName("policy2")
.withNamespace(NamespaceUtil.ofPolicy(metalakeName + "1"))
.withComment("comment")
- .withPolicyType("test")
- .withSupportedObjectTypes(supportedObjectTypes)
+ .withPolicyType(Policy.BuiltInType.CUSTOM)
.withContent(content)
.withAuditInfo(auditInfo)
.build();
@@ -407,9 +396,8 @@ public class TestPolicyMetaService extends TestJDBCBackend {
.withName("policy1")
.withNamespace(NamespaceUtil.ofPolicy(metalakeName))
.withComment("comment")
- .withPolicyType("test")
+ .withPolicyType(Policy.BuiltInType.CUSTOM)
.withContent(content)
- .withSupportedObjectTypes(supportedObjectTypes)
.withAuditInfo(auditInfo)
.build();
policyMetaService.insertPolicy(policyEntity1, false);
@@ -420,9 +408,8 @@ public class TestPolicyMetaService extends TestJDBCBackend {
.withName("policy2")
.withNamespace(NamespaceUtil.ofPolicy(metalakeName))
.withComment("comment")
- .withPolicyType("test")
+ .withPolicyType(Policy.BuiltInType.CUSTOM)
.withContent(content)
- .withSupportedObjectTypes(supportedObjectTypes)
.withAuditInfo(auditInfo)
.build();
policyMetaService.insertPolicy(policyEntity2, false);
@@ -433,9 +420,8 @@ public class TestPolicyMetaService extends TestJDBCBackend {
.withName("policy3")
.withNamespace(NamespaceUtil.ofPolicy(metalakeName))
.withComment("comment")
- .withPolicyType("test")
+ .withPolicyType(Policy.BuiltInType.CUSTOM)
.withContent(content)
- .withSupportedObjectTypes(supportedObjectTypes)
.withAuditInfo(auditInfo)
.build();
policyMetaService.insertPolicy(policyEntity3, false);
@@ -739,9 +725,8 @@ public class TestPolicyMetaService extends TestJDBCBackend {
.withName("policy1")
.withNamespace(NamespaceUtil.ofPolicy(metalakeName))
.withComment("comment")
- .withPolicyType("test")
+ .withPolicyType(Policy.BuiltInType.CUSTOM)
.withContent(content)
- .withSupportedObjectTypes(supportedObjectTypes)
.withAuditInfo(auditInfo)
.build();
policyMetaService.insertPolicy(policyEntity1, false);
diff --git a/core/src/test/java/org/apache/gravitino/storage/relational/utils/TestPOConverters.java b/core/src/test/java/org/apache/gravitino/storage/relational/utils/TestPOConverters.java
index 908096236e..31211eab23 100644
--- a/core/src/test/java/org/apache/gravitino/storage/relational/utils/TestPOConverters.java
+++ b/core/src/test/java/org/apache/gravitino/storage/relational/utils/TestPOConverters.java
@@ -20,13 +20,13 @@
package org.apache.gravitino.storage.relational.utils;
import static org.apache.gravitino.file.Fileset.LOCATION_NAME_UNKNOWN;
-import static org.apache.gravitino.policy.Policy.SUPPORTS_ALL_OBJECT_TYPES;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;
import java.time.Instant;
import java.time.LocalDateTime;
@@ -37,7 +37,6 @@ import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
-import java.util.Set;
import java.util.stream.Collectors;
import org.apache.gravitino.Catalog;
import org.apache.gravitino.Entity;
@@ -61,6 +60,7 @@ import org.apache.gravitino.meta.StatisticEntity;
import org.apache.gravitino.meta.TableEntity;
import org.apache.gravitino.meta.TagEntity;
import org.apache.gravitino.meta.TopicEntity;
+import org.apache.gravitino.policy.Policy;
import org.apache.gravitino.policy.PolicyContent;
import org.apache.gravitino.policy.PolicyContents;
import org.apache.gravitino.rel.expressions.Expression;
@@ -780,24 +780,21 @@ public class TestPOConverters {
@Test
public void testFromPolicyPO() throws JsonProcessingException {
- PolicyContent content = PolicyContents.custom(null, null);
+ ImmutableSet<MetadataObject.Type> supportedObjectTypes =
+ ImmutableSet.of(MetadataObject.Type.TABLE, MetadataObject.Type.SCHEMA);
+ PolicyContent content = PolicyContents.custom(null, supportedObjectTypes, null);
PolicyVersionPO policyVersionPO =
createPolicyVersionPO(1L, 1L, 1L, "test comment", true, content);
- PolicyPO policyPO =
- createPolicyPO(
- 1L, "test", "my_type", 1L, true, true, SUPPORTS_ALL_OBJECT_TYPES, policyVersionPO);
+ PolicyPO policyPO = createPolicyPO(1L, "test", "custom", 1L, policyVersionPO);
PolicyEntity expectedPolicy =
createPolicy(
1L,
"test",
NamespaceUtil.ofPolicy("test_metalake"),
- "my_type",
+ Policy.BuiltInType.CUSTOM,
"test comment",
true,
- true,
- true,
- SUPPORTS_ALL_OBJECT_TYPES,
content);
PolicyEntity convertedPolicy =
@@ -810,9 +807,6 @@ public class TestPOConverters {
assertEquals(expectedPolicy.policyType(), convertedPolicy.policyType());
assertEquals(expectedPolicy.comment(), convertedPolicy.comment());
assertEquals(expectedPolicy.enabled(), convertedPolicy.enabled());
- assertEquals(expectedPolicy.inheritable(), convertedPolicy.inheritable());
- assertEquals(expectedPolicy.exclusive(), convertedPolicy.exclusive());
- assertEquals(expectedPolicy.supportedObjectTypes(), convertedPolicy.supportedObjectTypes());
assertEquals(expectedPolicy.content(), convertedPolicy.content());
}
@@ -1589,12 +1583,9 @@ public class TestPOConverters {
Long id,
String name,
Namespace namespace,
- String type,
+ Policy.BuiltInType type,
String comment,
boolean enabled,
- boolean exclusive,
- boolean inheritable,
- Set<MetadataObject.Type> supportedObjectTypes,
PolicyContent content) {
AuditInfo auditInfo =
AuditInfo.builder().withCreator("creator").withCreateTime(FIX_INSTANT).build();
@@ -1605,23 +1596,13 @@ public class TestPOConverters {
.withPolicyType(type)
.withComment(comment)
.withEnabled(enabled)
- .withExclusive(exclusive)
- .withInheritable(inheritable)
- .withSupportedObjectTypes(supportedObjectTypes)
.withContent(content)
.withAuditInfo(auditInfo)
.build();
}
private static PolicyPO createPolicyPO(
- Long id,
- String name,
- String policyType,
- Long metalakeId,
- boolean inheritable,
- boolean exclusive,
- Set<MetadataObject.Type> supportedObjectTypes,
- PolicyVersionPO policyVersionPO)
+ Long id, String name, String policyType, Long metalakeId, PolicyVersionPO policyVersionPO)
throws JsonProcessingException {
AuditInfo auditInfo =
AuditInfo.builder().withCreator("creator").withCreateTime(FIX_INSTANT).build();
@@ -1630,10 +1611,6 @@ public class TestPOConverters {
.withPolicyName(name)
.withPolicyType(policyType)
.withMetalakeId(metalakeId)
- .withInheritable(inheritable)
- .withExclusive(exclusive)
- .withSupportedObjectTypes(
- JsonUtils.anyFieldMapper().writeValueAsString(supportedObjectTypes))
.withAuditInfo(JsonUtils.anyFieldMapper().writeValueAsString(auditInfo))
.withCurrentVersion(1L)
.withLastVersion(1L)
diff --git a/scripts/h2/schema-1.0.0-h2.sql b/scripts/h2/schema-1.0.0-h2.sql
index d159e5de8d..438c80c1a1 100644
--- a/scripts/h2/schema-1.0.0-h2.sql
+++ b/scripts/h2/schema-1.0.0-h2.sql
@@ -344,9 +344,6 @@ CREATE TABLE IF NOT EXISTS `policy_meta` (
`policy_name` VARCHAR(128) NOT NULL COMMENT 'policy name',
`policy_type` VARCHAR(64) NOT NULL COMMENT 'policy type',
`metalake_id` BIGINT(20) UNSIGNED NOT NULL COMMENT 'metalake id',
- `inheritable` TINYINT(1) NOT NULL COMMENT 'whether the policy is inheritable, 0 is not inheritable, 1 is inheritable',
- `exclusive` TINYINT(1) NOT NULL COMMENT 'whether the policy is exclusive, 0 is not exclusive, 1 is exclusive',
- `supported_object_types` TEXT NOT NULL COMMENT 'supported object types',
`audit_info` MEDIUMTEXT NOT NULL COMMENT 'policy audit info',
`current_version` INT UNSIGNED NOT NULL DEFAULT 1 COMMENT 'policy current version',
`last_version` INT UNSIGNED NOT NULL DEFAULT 1 COMMENT 'policy last version',
diff --git a/scripts/h2/upgrade-0.9.0-to-1.0.0-h2.sql b/scripts/h2/upgrade-0.9.0-to-1.0.0-h2.sql
index 071cde1146..8ef4d825cf 100644
--- a/scripts/h2/upgrade-0.9.0-to-1.0.0-h2.sql
+++ b/scripts/h2/upgrade-0.9.0-to-1.0.0-h2.sql
@@ -22,9 +22,6 @@ CREATE TABLE IF NOT EXISTS `policy_meta` (
`policy_name` VARCHAR(128) NOT NULL COMMENT 'policy name',
`policy_type` VARCHAR(64) NOT NULL COMMENT 'policy type',
`metalake_id` BIGINT(20) UNSIGNED NOT NULL COMMENT 'metalake id',
- `inheritable` TINYINT(1) NOT NULL COMMENT 'whether the policy is inheritable, 0 is not inheritable, 1 is inheritable',
- `exclusive` TINYINT(1) NOT NULL COMMENT 'whether the policy is exclusive, 0 is not exclusive, 1 is exclusive',
- `supported_object_types` TEXT NOT NULL COMMENT 'supported object types',
`audit_info` MEDIUMTEXT NOT NULL COMMENT 'policy audit info',
`current_version` INT UNSIGNED NOT NULL DEFAULT 1 COMMENT 'policy current version',
`last_version` INT UNSIGNED NOT NULL DEFAULT 1 COMMENT 'policy last version',
diff --git a/scripts/mysql/schema-1.0.0-mysql.sql b/scripts/mysql/schema-1.0.0-mysql.sql
index 06fa8c33b7..4fb9235a55 100644
--- a/scripts/mysql/schema-1.0.0-mysql.sql
+++ b/scripts/mysql/schema-1.0.0-mysql.sql
@@ -335,9 +335,6 @@ CREATE TABLE IF NOT EXISTS `policy_meta` (
`policy_name` VARCHAR(128) NOT NULL COMMENT 'policy name',
`policy_type` VARCHAR(64) NOT NULL COMMENT 'policy type',
`metalake_id` BIGINT(20) UNSIGNED NOT NULL COMMENT 'metalake id',
- `inheritable` TINYINT(1) NOT NULL COMMENT 'whether the policy is inheritable, 0 is not inheritable, 1 is inheritable',
- `exclusive` TINYINT(1) NOT NULL COMMENT 'whether the policy is exclusive, 0 is not exclusive, 1 is exclusive',
- `supported_object_types` TEXT NOT NULL COMMENT 'supported object types',
`audit_info` MEDIUMTEXT NOT NULL COMMENT 'policy audit info',
`current_version` INT UNSIGNED NOT NULL DEFAULT 1 COMMENT 'policy current version',
`last_version` INT UNSIGNED NOT NULL DEFAULT 1 COMMENT 'policy last version',
diff --git a/scripts/mysql/upgrade-0.9.0-to-1.0.0-mysql.sql b/scripts/mysql/upgrade-0.9.0-to-1.0.0-mysql.sql
index c44c94a41d..bab851396f 100644
--- a/scripts/mysql/upgrade-0.9.0-to-1.0.0-mysql.sql
+++ b/scripts/mysql/upgrade-0.9.0-to-1.0.0-mysql.sql
@@ -22,9 +22,6 @@ CREATE TABLE IF NOT EXISTS `policy_meta` (
`policy_name` VARCHAR(128) NOT NULL COMMENT 'policy name',
`policy_type` VARCHAR(64) NOT NULL COMMENT 'policy type',
`metalake_id` BIGINT(20) UNSIGNED NOT NULL COMMENT 'metalake id',
- `inheritable` TINYINT(1) NOT NULL COMMENT 'whether the policy is inheritable, 0 is not inheritable, 1 is inheritable',
- `exclusive` TINYINT(1) NOT NULL COMMENT 'whether the policy is exclusive, 0 is not exclusive, 1 is exclusive',
- `supported_object_types` TEXT NOT NULL COMMENT 'supported object types',
`audit_info` MEDIUMTEXT NOT NULL COMMENT 'policy audit info',
`current_version` INT UNSIGNED NOT NULL DEFAULT 1 COMMENT 'policy current version',
`last_version` INT UNSIGNED NOT NULL DEFAULT 1 COMMENT 'policy last version',
diff --git a/scripts/postgresql/schema-1.0.0-postgresql.sql b/scripts/postgresql/schema-1.0.0-postgresql.sql
index a815248673..a5e0d7cafd 100644
--- a/scripts/postgresql/schema-1.0.0-postgresql.sql
+++ b/scripts/postgresql/schema-1.0.0-postgresql.sql
@@ -594,9 +594,6 @@ CREATE TABLE IF NOT EXISTS policy_meta (
policy_name VARCHAR(128) NOT NULL,
policy_type VARCHAR(64) NOT NULL,
metalake_id BIGINT NOT NULL,
- inheritable SMALLINT NOT NULL,
- exclusive SMALLINT NOT NULL,
- supported_object_types TEXT NOT NULL,
audit_info TEXT NOT NULL,
current_version INT NOT NULL DEFAULT 1,
last_version INT NOT NULL DEFAULT 1,
@@ -610,9 +607,6 @@ COMMENT ON COLUMN policy_meta.policy_id IS 'policy id';
COMMENT ON COLUMN policy_meta.policy_name IS 'policy name';
COMMENT ON COLUMN policy_meta.policy_type IS 'policy type';
COMMENT ON COLUMN policy_meta.metalake_id IS 'metalake id';
-COMMENT ON COLUMN policy_meta.inheritable IS 'whether the policy is inheritable, 0 is not inheritable, 1 is inheritable';
-COMMENT ON COLUMN policy_meta.exclusive IS 'whether the policy is exclusive, 0 is not exclusive, 1 is exclusive';
-COMMENT ON COLUMN policy_meta.supported_object_types IS 'supported object types';
COMMENT ON COLUMN policy_meta.audit_info IS 'policy audit info';
COMMENT ON COLUMN policy_meta.current_version IS 'policy current version';
COMMENT ON COLUMN policy_meta.last_version IS 'policy last version';
diff --git a/scripts/postgresql/upgrade-0.9.0-to-1.0.0-postgresql.sql b/scripts/postgresql/upgrade-0.9.0-to-1.0.0-postgresql.sql
index 22c07c15a0..a2ff394b61 100644
--- a/scripts/postgresql/upgrade-0.9.0-to-1.0.0-postgresql.sql
+++ b/scripts/postgresql/upgrade-0.9.0-to-1.0.0-postgresql.sql
@@ -22,9 +22,6 @@ CREATE TABLE IF NOT EXISTS policy_meta (
policy_name VARCHAR(128) NOT NULL,
policy_type VARCHAR(64) NOT NULL,
metalake_id BIGINT NOT NULL,
- inheritable SMALLINT NOT NULL,
- exclusive SMALLINT NOT NULL,
- supported_object_types TEXT NOT NULL,
audit_info TEXT NOT NULL,
current_version INT NOT NULL DEFAULT 1,
last_version INT NOT NULL DEFAULT 1,
@@ -38,9 +35,6 @@ COMMENT ON COLUMN policy_meta.policy_id IS 'policy id';
COMMENT ON COLUMN policy_meta.policy_name IS 'policy name';
COMMENT ON COLUMN policy_meta.policy_type IS 'policy type';
COMMENT ON COLUMN policy_meta.metalake_id IS 'metalake id';
-COMMENT ON COLUMN policy_meta.inheritable IS 'whether the policy is inheritable, 0 is not inheritable, 1 is inheritable';
-COMMENT ON COLUMN policy_meta.exclusive IS 'whether the policy is exclusive, 0 is not exclusive, 1 is exclusive';
-COMMENT ON COLUMN policy_meta.supported_object_types IS 'supported object types';
COMMENT ON COLUMN policy_meta.audit_info IS 'policy audit info';
COMMENT ON COLUMN policy_meta.current_version IS 'policy current version';
COMMENT ON COLUMN policy_meta.last_version IS 'policy last version';
diff --git a/server/src/main/java/org/apache/gravitino/server/web/rest/PolicyOperations.java b/server/src/main/java/org/apache/gravitino/server/web/rest/PolicyOperations.java
index 93ec30eb32..b1afee8c3d 100644
--- a/server/src/main/java/org/apache/gravitino/server/web/rest/PolicyOperations.java
+++ b/server/src/main/java/org/apache/gravitino/server/web/rest/PolicyOperations.java
@@ -23,6 +23,7 @@ import static org.apache.gravitino.dto.util.DTOConverters.fromDTO;
import com.codahale.metrics.annotation.ResponseMetered;
import com.codahale.metrics.annotation.Timed;
import java.util.Arrays;
+import java.util.Locale;
import java.util.Optional;
import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
@@ -49,6 +50,7 @@ import org.apache.gravitino.dto.responses.NameListResponse;
import org.apache.gravitino.dto.responses.PolicyListResponse;
import org.apache.gravitino.dto.responses.PolicyResponse;
import org.apache.gravitino.dto.util.DTOConverters;
+import org.apache.gravitino.meta.PolicyEntity;
import org.apache.gravitino.metrics.MetricNames;
import org.apache.gravitino.policy.Policy;
import org.apache.gravitino.policy.PolicyChange;
@@ -86,10 +88,10 @@ public class PolicyOperations {
httpRequest,
() -> {
if (verbose) {
- Policy[] policies = policyDispatcher.listPolicyInfos(metalake);
+ PolicyEntity[] policies = policyDispatcher.listPolicyInfos(metalake);
PolicyDTO[] policyDTOs =
Arrays.stream(policies)
- .map(p -> DTOConverters.toDTO(p, Optional.empty()))
+ .map(p -> toDTO(p, Optional.empty()))
.toArray(PolicyDTO[]::new);
LOG.info("List {} policies info under metalake: {}", policyDTOs.length, metalake);
@@ -121,29 +123,17 @@ public class PolicyOperations {
httpRequest,
() -> {
request.validate();
- Policy policy =
- Policy.BuiltInType.fromPolicyType(request.getPolicyType())
- == Policy.BuiltInType.CUSTOM
- ? policyDispatcher.createPolicy(
- metalake,
- request.getName(),
- request.getPolicyType(),
- request.getComment(),
- request.getEnabled(),
- request.getExclusive(),
- request.getInheritable(),
- request.getSupportedObjectTypes(),
- fromDTO(request.getPolicyContent()))
- : policyDispatcher.createPolicy(
- metalake,
- request.getName(),
- request.getPolicyType(),
- request.getComment(),
- request.getEnabled(),
- fromDTO(request.getPolicyContent()));
+ PolicyEntity policy =
+ policyDispatcher.createPolicy(
+ metalake,
+ request.getName(),
+ Policy.BuiltInType.fromPolicyType(request.getPolicyType()),
+ request.getComment(),
+ request.getEnabled(),
+ fromDTO(request.getPolicyContent()));
LOG.info("Created policy: {} under metalake: {}", policy.name(), metalake);
- return Utils.ok(new PolicyResponse(DTOConverters.toDTO(policy, Optional.empty())));
+ return Utils.ok(new PolicyResponse(toDTO(policy, Optional.empty())));
});
} catch (Exception e) {
return ExceptionHandlers.handlePolicyException(
@@ -164,9 +154,9 @@ public class PolicyOperations {
return Utils.doAs(
httpRequest,
() -> {
- Policy policy = policyDispatcher.getPolicy(metalake, name);
+ PolicyEntity policy = policyDispatcher.getPolicy(metalake, name);
LOG.info("Get policy: {} under metalake: {}", name, metalake);
- return Utils.ok(new PolicyResponse(DTOConverters.toDTO(policy, Optional.empty())));
+ return Utils.ok(new PolicyResponse(toDTO(policy, Optional.empty())));
});
} catch (Exception e) {
return ExceptionHandlers.handlePolicyException(OperationType.GET, name, metalake, e);
@@ -194,10 +184,10 @@ public class PolicyOperations {
request.getUpdates().stream()
.map(PolicyUpdateRequest::policyChange)
.toArray(PolicyChange[]::new);
- Policy policy = policyDispatcher.alterPolicy(metalake, name, changes);
+ PolicyEntity policy = policyDispatcher.alterPolicy(metalake, name, changes);
LOG.info("Altered policy: {} under metalake: {}", name, metalake);
- return Utils.ok(new PolicyResponse(DTOConverters.toDTO(policy, Optional.empty())));
+ return Utils.ok(new PolicyResponse(toDTO(policy, Optional.empty())));
});
} catch (Exception e) {
return ExceptionHandlers.handlePolicyException(OperationType.ALTER, name, metalake, e);
@@ -264,4 +254,19 @@ public class PolicyOperations {
return ExceptionHandlers.handlePolicyException(OperationType.DELETE, name, metalake, e);
}
}
+
+ private PolicyDTO toDTO(PolicyEntity policy, Optional<Boolean> inherited) {
+ PolicyDTO.Builder builder =
+ PolicyDTO.builder()
+ .withName(policy.name())
+ .withComment(policy.comment())
+ .withPolicyType(policy.policyType().name().toLowerCase(Locale.ROOT))
+ .withEnabled(policy.enabled())
+ .withContent(DTOConverters.toDTO(policy.content()))
+ .withInherited(inherited)
+ .withAudit(DTOConverters.toDTO(policy.auditInfo()))
+ .withInherited(inherited);
+
+ return builder.build();
+ }
}
diff --git a/server/src/test/java/org/apache/gravitino/server/web/rest/TestPolicyOperations.java b/server/src/test/java/org/apache/gravitino/server/web/rest/TestPolicyOperations.java
index 917a86afc6..243fa80bd3 100644
--- a/server/src/test/java/org/apache/gravitino/server/web/rest/TestPolicyOperations.java
+++ b/server/src/test/java/org/apache/gravitino/server/web/rest/TestPolicyOperations.java
@@ -19,7 +19,6 @@
package org.apache.gravitino.server.web.rest;
import static org.apache.gravitino.dto.util.DTOConverters.toDTO;
-import static org.apache.gravitino.policy.Policy.SUPPORTS_ALL_OBJECT_TYPES;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.Mockito.doNothing;
@@ -28,6 +27,7 @@ import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;
import java.io.IOException;
import java.time.Instant;
@@ -37,6 +37,7 @@ import javax.ws.rs.client.Entity;
import javax.ws.rs.core.Application;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
+import org.apache.gravitino.MetadataObject;
import org.apache.gravitino.dto.requests.PolicyCreateRequest;
import org.apache.gravitino.dto.requests.PolicySetRequest;
import org.apache.gravitino.dto.requests.PolicyUpdateRequest;
@@ -185,16 +186,14 @@ public class TestPolicyOperations extends JerseyTest {
@Test
public void testListPolicyInfos() {
ImmutableMap<String, Object> contentFields = ImmutableMap.of("target_file_size_bytes", 1000);
- PolicyContent content = PolicyContents.custom(contentFields, null);
+ PolicyContent content =
+ PolicyContents.custom(contentFields, ImmutableSet.of(MetadataObject.Type.TABLE), null);
PolicyEntity policy1 =
PolicyEntity.builder()
.withId(1L)
.withName("policy1")
- .withPolicyType("my_compaction")
+ .withPolicyType(Policy.BuiltInType.CUSTOM)
.withEnabled(false)
- .withExclusive(true)
- .withInheritable(true)
- .withSupportedObjectTypes(SUPPORTS_ALL_OBJECT_TYPES)
.withContent(content)
.withAuditInfo(testAuditInfo1)
.build();
@@ -203,16 +202,13 @@ public class TestPolicyOperations extends JerseyTest {
PolicyEntity.builder()
.withId(1L)
.withName("policy2")
- .withPolicyType("my_compaction")
+ .withPolicyType(Policy.BuiltInType.CUSTOM)
.withEnabled(false)
- .withExclusive(true)
- .withInheritable(true)
- .withSupportedObjectTypes(SUPPORTS_ALL_OBJECT_TYPES)
.withContent(content)
.withAuditInfo(testAuditInfo1)
.build();
- Policy[] policies = new Policy[] {policy1, policy2};
+ PolicyEntity[] policies = new PolicyEntity[] {policy1, policy2};
when(policyManager.listPolicyInfos(metalake)).thenReturn(policies);
Response resp =
@@ -238,7 +234,7 @@ public class TestPolicyOperations extends JerseyTest {
Assertions.assertEquals(Optional.empty(), policyListResp.getPolicies()[1].inherited());
// Test return empty array
- when(policyManager.listPolicyInfos(metalake)).thenReturn(new Policy[0]);
+ when(policyManager.listPolicyInfos(metalake)).thenReturn(new PolicyEntity[0]);
Response resp2 =
target(policyPath(metalake))
.queryParam("details", true)
@@ -256,41 +252,23 @@ public class TestPolicyOperations extends JerseyTest {
@Test
public void testCreatePolicy() {
ImmutableMap<String, Object> contentFields = ImmutableMap.of("target_file_size_bytes", 1000);
- PolicyContent content = PolicyContents.custom(contentFields, null);
+ PolicyContent content =
+ PolicyContents.custom(contentFields, ImmutableSet.of(MetadataObject.Type.TABLE), null);
PolicyEntity policy1 =
PolicyEntity.builder()
.withId(1L)
.withName("policy1")
- .withPolicyType("my_compaction")
+ .withPolicyType(Policy.BuiltInType.CUSTOM)
.withEnabled(false)
- .withExclusive(true)
- .withInheritable(true)
- .withSupportedObjectTypes(SUPPORTS_ALL_OBJECT_TYPES)
.withContent(content)
.withAuditInfo(testAuditInfo1)
.build();
when(policyManager.createPolicy(
- metalake,
- "policy1",
- "my_compaction",
- null,
- false,
- true,
- true,
- SUPPORTS_ALL_OBJECT_TYPES,
- content))
+ metalake, "policy1", Policy.BuiltInType.CUSTOM, null, false, content))
.thenReturn(policy1);
PolicyCreateRequest request =
- new PolicyCreateRequest(
- "policy1",
- "my_compaction",
- null,
- false,
- true,
- true,
- SUPPORTS_ALL_OBJECT_TYPES,
- toDTO(content));
+ new PolicyCreateRequest("policy1", "custom", null, false, toDTO(content));
Response resp =
target(policyPath(metalake))
.request(MediaType.APPLICATION_JSON_TYPE)
@@ -311,8 +289,7 @@ public class TestPolicyOperations extends JerseyTest {
// Test throw PolicyAlreadyExistsException
doThrow(new PolicyAlreadyExistsException("mock error"))
.when(policyManager)
- .createPolicy(
- any(), any(), any(), any(), anyBoolean(), anyBoolean(), anyBoolean(), any(), any());
+ .createPolicy(any(), any(), any(), any(), anyBoolean(), any());
Response resp1 =
target(policyPath(metalake))
.request(MediaType.APPLICATION_JSON_TYPE)
@@ -329,8 +306,7 @@ public class TestPolicyOperations extends JerseyTest {
// Test throw RuntimeException
doThrow(new RuntimeException("mock error"))
.when(policyManager)
- .createPolicy(
- any(), any(), any(), any(), anyBoolean(), anyBoolean(), anyBoolean(), any(), any());
+ .createPolicy(any(), any(), any(), any(), anyBoolean(), any());
Response resp2 =
target(policyPath(metalake))
@@ -349,16 +325,14 @@ public class TestPolicyOperations extends JerseyTest {
@Test
public void testGetPolicy() {
ImmutableMap<String, Object> contentFields = ImmutableMap.of("target_file_size_bytes", 1000);
- PolicyContent content = PolicyContents.custom(contentFields, null);
+ PolicyContent content =
+ PolicyContents.custom(contentFields, ImmutableSet.of(MetadataObject.Type.TABLE), null);
PolicyEntity policy1 =
PolicyEntity.builder()
.withId(1L)
.withName("policy1")
- .withPolicyType("my_compaction")
+ .withPolicyType(Policy.BuiltInType.CUSTOM)
.withEnabled(false)
- .withExclusive(true)
- .withInheritable(true)
- .withSupportedObjectTypes(SUPPORTS_ALL_OBJECT_TYPES)
.withContent(content)
.withAuditInfo(testAuditInfo1)
.build();
@@ -421,17 +395,15 @@ public class TestPolicyOperations extends JerseyTest {
@Test
public void testAlterPolicy() {
ImmutableMap<String, Object> contentFields = ImmutableMap.of("target_file_size_bytes", 1000);
- PolicyContent content = PolicyContents.custom(contentFields, null);
+ PolicyContent content =
+ PolicyContents.custom(contentFields, ImmutableSet.of(MetadataObject.Type.TABLE), null);
PolicyEntity newPolicy =
PolicyEntity.builder()
.withId(1L)
.withName("new_policy1")
- .withPolicyType("my_compaction")
+ .withPolicyType(Policy.BuiltInType.CUSTOM)
.withComment("new policy1 comment")
.withEnabled(false)
- .withExclusive(true)
- .withInheritable(true)
- .withSupportedObjectTypes(SUPPORTS_ALL_OBJECT_TYPES)
.withContent(content)
.withAuditInfo(testAuditInfo1)
.build();
@@ -440,7 +412,7 @@ public class TestPolicyOperations extends JerseyTest {
new PolicyChange[] {
PolicyChange.rename("new_policy1"),
PolicyChange.updateComment("new policy1 comment"),
- PolicyChange.updateContent("my_compaction", content)
+ PolicyChange.updateContent("custom", content)
};
when(policyManager.alterPolicy(metalake, "policy1", changes)).thenReturn(newPolicy);
@@ -449,7 +421,7 @@ public class TestPolicyOperations extends JerseyTest {
new PolicyUpdateRequest[] {
new PolicyUpdateRequest.RenamePolicyRequest("new_policy1"),
new PolicyUpdateRequest.UpdatePolicyCommentRequest("new policy1 comment"),
- new PolicyUpdateRequest.UpdatePolicyContentRequest("my_compaction", toDTO(content))
+ new PolicyUpdateRequest.UpdatePolicyContentRequest("custom", toDTO(content))
};
PolicyUpdatesRequest request = new PolicyUpdatesRequest(Lists.newArrayList(requests));
Response resp =