This is an automated email from the ASF dual-hosted git repository.

kxiao pushed a commit to branch branch-2.0
in repository https://gitbox.apache.org/repos/asf/doris.git

commit eaa00dcf8836de811371b4c560c24a44924c02d8
Author: AlexYue <[email protected]>
AuthorDate: Mon Oct 9 11:27:49 2023 +0800

    [bugfix](policy) Forbid creating policy with same name with different 
resource name (#25025)
---
 .../java/org/apache/doris/policy/PolicyMgr.java    | 11 ++++-
 .../cold_heat_separation/policy/create.groovy      | 48 ++++++++++++++++++++++
 2 files changed, 58 insertions(+), 1 deletion(-)

diff --git a/fe/fe-core/src/main/java/org/apache/doris/policy/PolicyMgr.java 
b/fe/fe-core/src/main/java/org/apache/doris/policy/PolicyMgr.java
index 0ee44ba886d..42083ee6cd3 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/policy/PolicyMgr.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/policy/PolicyMgr.java
@@ -120,7 +120,16 @@ public class PolicyMgr implements Writable {
         Policy policy = Policy.fromCreateStmt(stmt);
         writeLock();
         try {
-            if (existPolicy(policy)) {
+            boolean storagePolicyExists = false;
+            if (PolicyTypeEnum.STORAGE == policy.getType()) {
+                // The name of the storage policy remains globally unique 
until it is renamed by user.
+                // So we could just compare the policy name to check if there 
are redundant ones.
+                // Otherwise two storage policy share one same name but with 
different resource name
+                // will not be filtered. See github #25025 for more details.
+                storagePolicyExists = getPoliciesByType(PolicyTypeEnum.STORAGE)
+                        .stream().anyMatch(p -> 
p.getPolicyName().equals(policy.getPolicyName()));
+            }
+            if (storagePolicyExists || existPolicy(policy)) {
                 if (stmt.isIfNotExists()) {
                     return;
                 }
diff --git a/regression-test/suites/cold_heat_separation/policy/create.groovy 
b/regression-test/suites/cold_heat_separation/policy/create.groovy
index f1efc22fa7f..1232dbe504f 100644
--- a/regression-test/suites/cold_heat_separation/policy/create.groovy
+++ b/regression-test/suites/cold_heat_separation/policy/create.groovy
@@ -442,4 +442,52 @@ suite("create_policy") {
         //  errCode = 2, detailMessage = storage resource doesn't exist: 
s3_resource_not_exist
         assertEquals(storage_exist.call("testPolicy_15"), false)
     }
+
+    if (!storage_exist.call("testPolicy_redundant_name")) {
+        def create_s3_resource = try_sql """
+            CREATE RESOURCE "testPolicy_redundant_name_resource"
+            PROPERTIES(
+                "type"="s3",
+                "AWS_REGION" = "bj",
+                "AWS_ENDPOINT" = "http://bj.s3.comaaaa";,
+                "AWS_ROOT_PATH" = "path/to/rootaaaa",
+                "AWS_SECRET_KEY" = "aaaa",
+                "AWS_ACCESS_KEY" = "bbba",
+                "AWS_BUCKET" = "test-bucket",
+                "s3_validity_check" = "false"
+            );
+        """
+        def create_s3_resource_1 = try_sql """
+            CREATE RESOURCE "testPolicy_redundant_name_resource_1"
+            PROPERTIES(
+                "type"="s3",
+                "AWS_REGION" = "bj",
+                "AWS_ENDPOINT" = "http://bj.s3.comaaaa";,
+                "AWS_ROOT_PATH" = "path/to/rootaaaa",
+                "AWS_SECRET_KEY" = "aaaa",
+                "AWS_ACCESS_KEY" = "bbba",
+                "AWS_BUCKET" = "test-bucket",
+                "s3_validity_check" = "false"
+            );
+        """
+        def create_succ_1 = try_sql """
+            CREATE STORAGE POLICY testPolicy_redundant_name
+            PROPERTIES(
+            "storage_resource" = "testPolicy_redundant_name_resource",
+            "cooldown_ttl" = "10086"
+            );
+        """
+        try {
+            sql """
+                CREATE STORAGE POLICY testPolicy_redundant_name
+                PROPERTIES(
+                "storage_resource" = "testPolicy_redundant_name_resource_1",
+                "cooldown_ttl" = "10086"
+                );
+            """
+        } catch (Exception e) {
+            log.info(e.getMessage())
+            assertTrue(e.getMessage().contains('already create'))
+        }
+    }
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to