You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by ji...@apache.org on 2022/12/20 02:31:59 UTC

[shardingsphere] branch master updated: ConvertYamlConfigurationHandler supports auditStrategy. (#22979)

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

jianglongtao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git


The following commit(s) were added to refs/heads/master by this push:
     new 45b4ddf25fb ConvertYamlConfigurationHandler supports auditStrategy. (#22979)
45b4ddf25fb is described below

commit 45b4ddf25fbfae4a6650851fe7141f34460fa77e
Author: yx9o <ya...@163.com>
AuthorDate: Tue Dec 20 10:31:32 2022 +0800

    ConvertYamlConfigurationHandler supports auditStrategy. (#22979)
---
 .../common/constant/DistSQLScriptConstants.java    |  2 +
 .../queryable/ConvertYamlConfigurationHandler.java | 77 +++++++++++++---------
 .../ConvertYamlConfigurationHandlerTest.java       |  4 +-
 .../conf/convert/config-sharding-auto-tables.yaml  | 13 ++++
 .../expected/convert-sharding-auto-tables.yaml     |  9 ++-
 .../test/resources/expected/convert-sharding.yaml  |  6 +-
 6 files changed, 74 insertions(+), 37 deletions(-)

diff --git a/proxy/backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/common/constant/DistSQLScriptConstants.java b/proxy/backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/common/constant/DistSQLScriptConstants.java
index d859cbc01ee..e1b089f2f23 100644
--- a/proxy/backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/common/constant/DistSQLScriptConstants.java
+++ b/proxy/backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/common/constant/DistSQLScriptConstants.java
@@ -109,6 +109,8 @@ public final class DistSQLScriptConstants {
     
     public static final String KEY_GENERATOR_STRATEGY = "KEY_GENERATE_STRATEGY(COLUMN=%s, %s)";
     
+    public static final String AUDIT_STRATEGY = "AUDIT_STRATEGY(%s, ALLOW_HINT_DISABLE=%s)";
+    
     public static final String SHARDING_BINDING_TABLE_RULES = "CREATE SHARDING TABLE REFERENCE RULE";
     
     public static final String BINDING_TABLES = " %s (%s)";
diff --git a/proxy/backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/queryable/ConvertYamlConfigurationHandler.java b/proxy/backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/queryable/ConvertYamlConfigurationHandler.java
index dff36d70c2a..1471a18df4a 100644
--- a/proxy/backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/queryable/ConvertYamlConfigurationHandler.java
+++ b/proxy/backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/queryable/ConvertYamlConfigurationHandler.java
@@ -61,6 +61,7 @@ import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration;
 import org.apache.shardingsphere.sharding.api.config.rule.ShardingAutoTableRuleConfiguration;
 import org.apache.shardingsphere.sharding.api.config.rule.ShardingTableReferenceRuleConfiguration;
 import org.apache.shardingsphere.sharding.api.config.rule.ShardingTableRuleConfiguration;
+import org.apache.shardingsphere.sharding.api.config.strategy.audit.ShardingAuditStrategyConfiguration;
 import org.apache.shardingsphere.sharding.api.config.strategy.keygen.KeyGenerateStrategyConfiguration;
 import org.apache.shardingsphere.sharding.api.config.strategy.sharding.ComplexShardingStrategyConfiguration;
 import org.apache.shardingsphere.sharding.api.config.strategy.sharding.ShardingStrategyConfiguration;
@@ -222,8 +223,8 @@ public final class ConvertYamlConfigurationHandler extends QueryableRALBackendHa
         if (ruleConfig.getTables().isEmpty() && ruleConfig.getAutoTables().isEmpty()) {
             return;
         }
-        String tableRules = getTableRules(ruleConfig.getTables(), ruleConfig.getShardingAlgorithms(), ruleConfig.getKeyGenerators());
-        String autoTableRules = getAutoTableRules(ruleConfig.getAutoTables(), ruleConfig.getShardingAlgorithms(), ruleConfig.getKeyGenerators(), ruleConfig.getDefaultShardingColumn());
+        String tableRules = getTableRules(ruleConfig);
+        String autoTableRules = getAutoTableRules(ruleConfig);
         result.append(DistSQLScriptConstants.CREATE_SHARDING_TABLE).append(tableRules);
         if (!Strings.isNullOrEmpty(tableRules) && !Strings.isNullOrEmpty(autoTableRules)) {
             result.append(DistSQLScriptConstants.COMMA).append(System.lineSeparator());
@@ -231,15 +232,14 @@ public final class ConvertYamlConfigurationHandler extends QueryableRALBackendHa
         result.append(autoTableRules).append(DistSQLScriptConstants.SEMI).append(System.lineSeparator()).append(System.lineSeparator());
     }
     
-    private String getAutoTableRules(final Collection<ShardingAutoTableRuleConfiguration> autoTables, final Map<String, AlgorithmConfiguration> shardingAlgorithms,
-                                     final Map<String, AlgorithmConfiguration> keyGenerators, final String defaultShardingColumn) {
+    private String getAutoTableRules(final ShardingRuleConfiguration ruleConfig) {
         StringBuilder result = new StringBuilder();
-        if (!autoTables.isEmpty()) {
-            Iterator<ShardingAutoTableRuleConfiguration> iterator = autoTables.iterator();
+        if (!ruleConfig.getAutoTables().isEmpty()) {
+            Iterator<ShardingAutoTableRuleConfiguration> iterator = ruleConfig.getAutoTables().iterator();
             while (iterator.hasNext()) {
-                ShardingAutoTableRuleConfiguration tableRuleConfig = iterator.next();
-                result.append(String.format(DistSQLScriptConstants.SHARDING_AUTO_TABLE, tableRuleConfig.getLogicTable(), tableRuleConfig.getActualDataSources(),
-                        appendAutoTableStrategy(tableRuleConfig, shardingAlgorithms, keyGenerators, defaultShardingColumn)));
+                ShardingAutoTableRuleConfiguration autoTableRuleConfig = iterator.next();
+                result.append(String.format(DistSQLScriptConstants.SHARDING_AUTO_TABLE, autoTableRuleConfig.getLogicTable(), autoTableRuleConfig.getActualDataSources(),
+                        appendAutoTableStrategy(autoTableRuleConfig, ruleConfig)));
                 if (iterator.hasNext()) {
                     result.append(DistSQLScriptConstants.COMMA);
                 }
@@ -248,15 +248,14 @@ public final class ConvertYamlConfigurationHandler extends QueryableRALBackendHa
         return result.toString();
     }
     
-    private String getTableRules(final Collection<ShardingTableRuleConfiguration> tables, final Map<String, AlgorithmConfiguration> shardingAlgorithms,
-                                 final Map<String, AlgorithmConfiguration> keyGenerators) {
+    private String getTableRules(final ShardingRuleConfiguration ruleConfig) {
         StringBuilder result = new StringBuilder();
-        if (!tables.isEmpty()) {
-            Iterator<ShardingTableRuleConfiguration> iterator = tables.iterator();
+        if (!ruleConfig.getTables().isEmpty()) {
+            Iterator<ShardingTableRuleConfiguration> iterator = ruleConfig.getTables().iterator();
             while (iterator.hasNext()) {
                 ShardingTableRuleConfiguration tableRuleConfig = iterator.next();
                 result.append(String.format(DistSQLScriptConstants.SHARDING_TABLE, tableRuleConfig.getLogicTable(), tableRuleConfig.getActualDataNodes(),
-                        appendTableStrategy(tableRuleConfig, shardingAlgorithms, keyGenerators)));
+                        appendTableStrategy(tableRuleConfig, ruleConfig)));
                 if (iterator.hasNext()) {
                     result.append(DistSQLScriptConstants.COMMA);
                 }
@@ -265,24 +264,43 @@ public final class ConvertYamlConfigurationHandler extends QueryableRALBackendHa
         return result.toString();
     }
     
-    private String appendAutoTableStrategy(final ShardingAutoTableRuleConfiguration ruleConfig, final Map<String, AlgorithmConfiguration> shardingAlgorithms,
-                                           final Map<String, AlgorithmConfiguration> keyGenerators, final String defaultShardingColumn) {
+    private String appendAutoTableStrategy(final ShardingAutoTableRuleConfiguration autoTableRuleConfig, final ShardingRuleConfiguration ruleConfig) {
         StringBuilder result = new StringBuilder();
-        StandardShardingStrategyConfiguration strategyConfig = (StandardShardingStrategyConfiguration) ruleConfig.getShardingStrategy();
-        String shardingColumn = !Strings.isNullOrEmpty(strategyConfig.getShardingColumn()) ? strategyConfig.getShardingColumn() : defaultShardingColumn;
-        result.append(String.format(DistSQLScriptConstants.AUTO_TABLE_STRATEGY, shardingColumn, getAlgorithmType(shardingAlgorithms.get(strategyConfig.getShardingAlgorithmName()))));
-        appendKeyGenerateStrategy(keyGenerators, ruleConfig.getKeyGenerateStrategy(), result);
-        // TODO auditStrategy
+        StandardShardingStrategyConfiguration strategyConfig = (StandardShardingStrategyConfiguration) autoTableRuleConfig.getShardingStrategy();
+        String shardingColumn = !Strings.isNullOrEmpty(strategyConfig.getShardingColumn()) ? strategyConfig.getShardingColumn() : ruleConfig.getDefaultShardingColumn();
+        result.append(String.format(DistSQLScriptConstants.AUTO_TABLE_STRATEGY, shardingColumn, getAlgorithmType(ruleConfig.getShardingAlgorithms().get(strategyConfig.getShardingAlgorithmName()))));
+        appendKeyGenerateStrategy(ruleConfig.getKeyGenerators(), autoTableRuleConfig.getKeyGenerateStrategy(), result);
+        appendAuditStrategy(ruleConfig.getAuditors(), null != autoTableRuleConfig.getAuditStrategy() ? autoTableRuleConfig.getAuditStrategy() : ruleConfig.getDefaultAuditStrategy(), result);
         return result.toString();
     }
     
-    private String appendTableStrategy(final ShardingTableRuleConfiguration ruleConfig,
-                                       final Map<String, AlgorithmConfiguration> shardingAlgorithms, final Map<String, AlgorithmConfiguration> keyGenerators) {
+    private void appendAuditStrategy(final Map<String, AlgorithmConfiguration> auditors, final ShardingAuditStrategyConfiguration auditStrategy, final StringBuilder result) {
+        if (null != auditStrategy) {
+            result.append(DistSQLScriptConstants.COMMA).append(System.lineSeparator());
+            result.append(String.format(DistSQLScriptConstants.AUDIT_STRATEGY, getAlgorithmTypes(auditors, auditStrategy.getAuditorNames()), auditStrategy.isAllowHintDisable()));
+        }
+    }
+    
+    private String getAlgorithmTypes(final Map<String, AlgorithmConfiguration> auditors, final Collection<String> auditorNames) {
         StringBuilder result = new StringBuilder();
-        appendStrategy(ruleConfig.getDatabaseShardingStrategy(), DistSQLScriptConstants.DATABASE_STRATEGY, result, shardingAlgorithms);
-        appendStrategy(ruleConfig.getTableShardingStrategy(), DistSQLScriptConstants.TABLE_STRATEGY, result, shardingAlgorithms);
-        appendKeyGenerateStrategy(keyGenerators, ruleConfig.getKeyGenerateStrategy(), result);
-        // TODO auditStrategy
+        if (!auditorNames.isEmpty()) {
+            Iterator<String> iterator = auditorNames.iterator();
+            while (iterator.hasNext()) {
+                result.append(getAlgorithmType(auditors.get(iterator.next())));
+                if (iterator.hasNext()) {
+                    result.append(DistSQLScriptConstants.COMMA);
+                }
+            }
+        }
+        return result.toString();
+    }
+    
+    private String appendTableStrategy(final ShardingTableRuleConfiguration tableRuleConfig, final ShardingRuleConfiguration ruleConfig) {
+        StringBuilder result = new StringBuilder();
+        appendStrategy(tableRuleConfig.getDatabaseShardingStrategy(), DistSQLScriptConstants.DATABASE_STRATEGY, result, ruleConfig.getShardingAlgorithms());
+        appendStrategy(tableRuleConfig.getTableShardingStrategy(), DistSQLScriptConstants.TABLE_STRATEGY, result, ruleConfig.getShardingAlgorithms());
+        appendKeyGenerateStrategy(ruleConfig.getKeyGenerators(), tableRuleConfig.getKeyGenerateStrategy(), result);
+        appendAuditStrategy(ruleConfig.getAuditors(), null != tableRuleConfig.getAuditStrategy() ? tableRuleConfig.getAuditStrategy() : ruleConfig.getDefaultAuditStrategy(), result);
         return result.toString();
     }
     
@@ -426,10 +444,9 @@ public final class ConvertYamlConfigurationHandler extends QueryableRALBackendHa
     
     private String getDatabaseDiscoveryHeartbeat(final DatabaseDiscoveryHeartBeatConfiguration heartBeatConfig) {
         StringBuilder result = new StringBuilder();
-        if (null == heartBeatConfig) {
-            return result.toString();
+        if (null != heartBeatConfig) {
+            result.append(getAlgorithmProperties(heartBeatConfig.getProps()));
         }
-        result.append(getAlgorithmProperties(heartBeatConfig.getProps()));
         return result.toString();
     }
     
diff --git a/proxy/backend/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/queryable/ConvertYamlConfigurationHandlerTest.java b/proxy/backend/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/queryable/ConvertYamlConfigurationHandlerTest.java
index 633694376c6..cdbe21529d5 100644
--- a/proxy/backend/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/queryable/ConvertYamlConfigurationHandlerTest.java
+++ b/proxy/backend/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/queryable/ConvertYamlConfigurationHandlerTest.java
@@ -148,11 +148,11 @@ public final class ConvertYamlConfigurationHandlerTest {
     }
     
     private void assertParseSQL(final String actual) {
-        for (String each : Splitter.on(";").trimResults().splitToList(actual)) {
+        Splitter.on(";").trimResults().splitToList(actual).forEach(each -> {
             if (!Strings.isNullOrEmpty(each)) {
                 assertNotNull(sqlParserRule.getSQLParserEngine(new MySQLDatabaseType().getType()).parse(each, false));
             }
-        }
+        });
     }
     
     @SneakyThrows(IOException.class)
diff --git a/proxy/backend/src/test/resources/conf/convert/config-sharding-auto-tables.yaml b/proxy/backend/src/test/resources/conf/convert/config-sharding-auto-tables.yaml
index 8a0bfef2885..2c1003804a5 100644
--- a/proxy/backend/src/test/resources/conf/convert/config-sharding-auto-tables.yaml
+++ b/proxy/backend/src/test/resources/conf/convert/config-sharding-auto-tables.yaml
@@ -49,6 +49,10 @@ rules:
         keyGenerateStrategy:
           column: order_id
           keyGeneratorName: snowflake
+        auditStrategy:
+          auditorNames:
+            - sharding_key_required_auditor
+          allowHintDisable: true
       t_order_item:
         actualDataSources: ds_0,ds_1
         shardingStrategy:
@@ -73,6 +77,15 @@ rules:
         props:
           sharding-count: 4
 
+    defaultAuditStrategy:
+      auditorNames:
+        - sharding_key_required_auditor
+      allowHintDisable: true
+
     keyGenerators:
       snowflake:
         type: SNOWFLAKE
+
+    auditors:
+      sharding_key_required_auditor:
+        type: DML_SHARDING_CONDITIONS
diff --git a/proxy/backend/src/test/resources/expected/convert-sharding-auto-tables.yaml b/proxy/backend/src/test/resources/expected/convert-sharding-auto-tables.yaml
index d914c6e5475..feb604ff1a3 100644
--- a/proxy/backend/src/test/resources/expected/convert-sharding-auto-tables.yaml
+++ b/proxy/backend/src/test/resources/expected/convert-sharding-auto-tables.yaml
@@ -33,15 +33,18 @@ CREATE SHARDING TABLE RULE t_order (
 STORAGE_UNITS(ds_0,ds_1),
 SHARDING_COLUMN=order_id,
 TYPE(NAME='mod', PROPERTIES('sharding-count'='4')),
-KEY_GENERATE_STRATEGY(COLUMN=order_id, TYPE(NAME='snowflake'))
+KEY_GENERATE_STRATEGY(COLUMN=order_id, TYPE(NAME='snowflake')),
+AUDIT_STRATEGY(TYPE(NAME='dml_sharding_conditions'), ALLOW_HINT_DISABLE=true)
 ), t_order_item (
 STORAGE_UNITS(ds_0,ds_1),
 SHARDING_COLUMN=order_id,
 TYPE(NAME='mod', PROPERTIES('sharding-count'='4')),
-KEY_GENERATE_STRATEGY(COLUMN=order_item_id, TYPE(NAME='snowflake'))
+KEY_GENERATE_STRATEGY(COLUMN=order_item_id, TYPE(NAME='snowflake')),
+AUDIT_STRATEGY(TYPE(NAME='dml_sharding_conditions'), ALLOW_HINT_DISABLE=true)
 ), t_account (
 STORAGE_UNITS(ds_0,ds_1),
 SHARDING_COLUMN=account_id,
 TYPE(NAME='mod', PROPERTIES('sharding-count'='4')),
-KEY_GENERATE_STRATEGY(COLUMN=account_id, TYPE(NAME='snowflake'))
+KEY_GENERATE_STRATEGY(COLUMN=account_id, TYPE(NAME='snowflake')),
+AUDIT_STRATEGY(TYPE(NAME='dml_sharding_conditions'), ALLOW_HINT_DISABLE=true)
 );
diff --git a/proxy/backend/src/test/resources/expected/convert-sharding.yaml b/proxy/backend/src/test/resources/expected/convert-sharding.yaml
index 0a79ed11acf..31173739617 100644
--- a/proxy/backend/src/test/resources/expected/convert-sharding.yaml
+++ b/proxy/backend/src/test/resources/expected/convert-sharding.yaml
@@ -32,11 +32,13 @@ PROPERTIES('minPoolSize'='1', 'connectionTimeoutMilliseconds'='30000', 'maxLifet
 CREATE SHARDING TABLE RULE t_order (
 DATANODES('ds_${0..1}.t_order_${0..1}'),
 TABLE_STRATEGY(TYPE='complex', SHARDING_COLUMNS=order_id, user_id, SHARDING_ALGORITHM(TYPE(NAME='inline', PROPERTIES('algorithm-expression'='t_order_${order_id % 2}', 'datetime-lower'='2022-01-01 00:00:00')))),
-KEY_GENERATE_STRATEGY(COLUMN=order_id, TYPE(NAME='snowflake'))
+KEY_GENERATE_STRATEGY(COLUMN=order_id, TYPE(NAME='snowflake')),
+AUDIT_STRATEGY(TYPE(NAME='dml_sharding_conditions'), ALLOW_HINT_DISABLE=true)
 ), t_order_item (
 DATANODES('ds_${0..1}.t_order_item_${0..1}'),
 TABLE_STRATEGY(TYPE='complex', SHARDING_COLUMNS=order_id, user_id, SHARDING_ALGORITHM(TYPE(NAME='inline', PROPERTIES('algorithm-expression'='t_order_item_${order_id % 2}', 'datetime-lower'='2022-01-01 00:00:00')))),
-KEY_GENERATE_STRATEGY(COLUMN=order_item_id, TYPE(NAME='snowflake'))
+KEY_GENERATE_STRATEGY(COLUMN=order_item_id, TYPE(NAME='snowflake')),
+AUDIT_STRATEGY(TYPE(NAME='dml_sharding_conditions'), ALLOW_HINT_DISABLE=true)
 );
 
 CREATE BROADCAST TABLE RULE t_address;