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 f2608de22de Convert ShadowRuleConfiguration to DistSQL via SPI (#27280)
f2608de22de is described below

commit f2608de22de172f5946c63b13a32482cf58c965c
Author: yx9o <[email protected]>
AuthorDate: Wed Jul 19 15:16:29 2023 +0800

    Convert ShadowRuleConfiguration to DistSQL via SPI (#27280)
    
    * Convert ShadowRuleConfiguration to DistSQL via SPI
    
    * Update
---
 .../ShadowConvertRuleConfigurationProvider.java    | 128 +++++++++++++++++++++
 ...dler.ral.query.ConvertRuleConfigurationProvider |  18 +++
 .../ConvertYamlConfigurationExecutor.java          |  52 ---------
 3 files changed, 146 insertions(+), 52 deletions(-)

diff --git 
a/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/provider/ShadowConvertRuleConfigurationProvider.java
 
b/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/provider/ShadowConvertRuleConfigurationProvider.java
new file mode 100644
index 00000000000..0e39866ccd8
--- /dev/null
+++ 
b/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/provider/ShadowConvertRuleConfigurationProvider.java
@@ -0,0 +1,128 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.shadow.distsql.handler.provider;
+
+import 
org.apache.shardingsphere.distsql.handler.ral.constant.DistSQLScriptConstants;
+import 
org.apache.shardingsphere.distsql.handler.ral.query.ConvertRuleConfigurationProvider;
+import org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration;
+import org.apache.shardingsphere.infra.config.rule.RuleConfiguration;
+import org.apache.shardingsphere.shadow.api.config.ShadowRuleConfiguration;
+import 
org.apache.shardingsphere.shadow.api.config.datasource.ShadowDataSourceConfiguration;
+import 
org.apache.shardingsphere.shadow.api.config.table.ShadowTableConfiguration;
+
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Properties;
+import java.util.TreeMap;
+
+/**
+ * Shadow convert rule configuration provider.
+ */
+public final class ShadowConvertRuleConfigurationProvider implements 
ConvertRuleConfigurationProvider {
+    
+    @Override
+    public String convert(final RuleConfiguration ruleConfig) {
+        return getShadowDistSQL((ShadowRuleConfiguration) ruleConfig);
+    }
+    
+    private String getShadowDistSQL(final ShadowRuleConfiguration ruleConfig) {
+        if (ruleConfig.getDataSources().isEmpty()) {
+            return "";
+        }
+        StringBuilder result = new StringBuilder();
+        result.append(DistSQLScriptConstants.CREATE_SHADOW);
+        Iterator<ShadowDataSourceConfiguration> iterator = 
ruleConfig.getDataSources().iterator();
+        while (iterator.hasNext()) {
+            ShadowDataSourceConfiguration dataSourceConfig = iterator.next();
+            String shadowRuleName = dataSourceConfig.getName();
+            String shadowTables = getShadowTables(shadowRuleName, 
ruleConfig.getTables(), ruleConfig.getShadowAlgorithms());
+            result.append(String.format(DistSQLScriptConstants.SHADOW, 
shadowRuleName, dataSourceConfig.getProductionDataSourceName(), 
dataSourceConfig.getShadowDataSourceName(), shadowTables));
+            if (iterator.hasNext()) {
+                result.append(DistSQLScriptConstants.COMMA);
+            }
+        }
+        
result.append(DistSQLScriptConstants.SEMI).append(System.lineSeparator()).append(System.lineSeparator());
+        return result.toString();
+    }
+    
+    private String getShadowTables(final String shadowRuleName, final 
Map<String, ShadowTableConfiguration> ruleConfig, final Map<String, 
AlgorithmConfiguration> algorithmConfigs) {
+        StringBuilder result = new StringBuilder();
+        Iterator<Map.Entry<String, ShadowTableConfiguration>> iterator = 
ruleConfig.entrySet().iterator();
+        while (iterator.hasNext()) {
+            Map.Entry<String, ShadowTableConfiguration> shadowTableConfig = 
iterator.next();
+            if 
(shadowTableConfig.getValue().getDataSourceNames().contains(shadowRuleName)) {
+                String shadowTableTypes = 
getShadowTableTypes(shadowTableConfig.getValue().getShadowAlgorithmNames(), 
algorithmConfigs);
+                
result.append(String.format(DistSQLScriptConstants.SHADOW_TABLE, 
shadowTableConfig.getKey(), shadowTableTypes));
+            }
+            if (iterator.hasNext()) {
+                
result.append(DistSQLScriptConstants.COMMA).append(System.lineSeparator());
+            }
+        }
+        return result.toString();
+    }
+    
+    private String getShadowTableTypes(final Collection<String> 
shadowAlgorithmNames, final Map<String, AlgorithmConfiguration> 
algorithmConfigs) {
+        StringBuilder result = new StringBuilder();
+        Iterator<String> iterator = shadowAlgorithmNames.iterator();
+        while (iterator.hasNext()) {
+            
result.append(getAlgorithmType(algorithmConfigs.get(iterator.next())));
+            if (iterator.hasNext()) {
+                result.append(DistSQLScriptConstants.COMMA).append(' ');
+            }
+        }
+        return result.toString();
+    }
+    
+    private String getAlgorithmType(final AlgorithmConfiguration 
algorithmConfig) {
+        StringBuilder result = new StringBuilder();
+        if (null == algorithmConfig) {
+            return result.toString();
+        }
+        String type = algorithmConfig.getType().toLowerCase();
+        if (algorithmConfig.getProps().isEmpty()) {
+            
result.append(String.format(DistSQLScriptConstants.ALGORITHM_TYPE_WITHOUT_PROPS,
 type));
+        } else {
+            result.append(String.format(DistSQLScriptConstants.ALGORITHM_TYPE, 
type, getAlgorithmProperties(algorithmConfig.getProps())));
+        }
+        return result.toString();
+    }
+    
+    @SuppressWarnings({"unchecked", "rawtypes"})
+    private String getAlgorithmProperties(final Properties props) {
+        StringBuilder result = new StringBuilder();
+        Iterator<String> iterator = new TreeMap(props).keySet().iterator();
+        while (iterator.hasNext()) {
+            String key = iterator.next();
+            Object value = props.get(key);
+            if (null == value) {
+                continue;
+            }
+            result.append(String.format(DistSQLScriptConstants.PROPERTY, key, 
value));
+            if (iterator.hasNext()) {
+                result.append(DistSQLScriptConstants.COMMA).append(' ');
+            }
+        }
+        return result.toString();
+    }
+    
+    @Override
+    public String getType() {
+        return ShadowRuleConfiguration.class.getName();
+    }
+}
diff --git 
a/features/shadow/distsql/handler/src/main/resources/META-INF/services/org.apache.shardingsphere.distsql.handler.ral.query.ConvertRuleConfigurationProvider
 
b/features/shadow/distsql/handler/src/main/resources/META-INF/services/org.apache.shardingsphere.distsql.handler.ral.query.ConvertRuleConfigurationProvider
new file mode 100644
index 00000000000..bd07b3efc5b
--- /dev/null
+++ 
b/features/shadow/distsql/handler/src/main/resources/META-INF/services/org.apache.shardingsphere.distsql.handler.ral.query.ConvertRuleConfigurationProvider
@@ -0,0 +1,18 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+org.apache.shardingsphere.shadow.distsql.handler.provider.ShadowConvertRuleConfigurationProvider
diff --git 
a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/queryable/ConvertYamlConfigurationExecutor.java
 
b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/queryable/ConvertYamlConfigurationExecutor.java
index 8cdbb625b2f..0a7977ade98 100644
--- 
a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/queryable/ConvertYamlConfigurationExecutor.java
+++ 
b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/queryable/ConvertYamlConfigurationExecutor.java
@@ -44,9 +44,6 @@ import 
org.apache.shardingsphere.proxy.backend.config.yaml.YamlProxyDataSourceCo
 import 
org.apache.shardingsphere.proxy.backend.config.yaml.YamlProxyDatabaseConfiguration;
 import 
org.apache.shardingsphere.proxy.backend.config.yaml.swapper.YamlProxyDataSourceConfigurationSwapper;
 import org.apache.shardingsphere.proxy.backend.exception.FileIOException;
-import org.apache.shardingsphere.shadow.api.config.ShadowRuleConfiguration;
-import 
org.apache.shardingsphere.shadow.api.config.datasource.ShadowDataSourceConfiguration;
-import 
org.apache.shardingsphere.shadow.api.config.table.ShadowTableConfiguration;
 
 import java.io.File;
 import java.io.IOException;
@@ -93,8 +90,6 @@ public final class ConvertYamlConfigurationExecutor 
implements QueryableRALExecu
                 ConvertRuleConfigurationProvider convertRuleConfigProvider = 
TypedSPILoader.getService(ConvertRuleConfigurationProvider.class,
                         ((CompatibleEncryptRuleConfiguration) 
each).convertToEncryptRuleConfiguration().getClass().getName());
                 result.append(convertRuleConfigProvider.convert(each));
-            } else if (each instanceof ShadowRuleConfiguration) {
-                appendShadowDistSQL((ShadowRuleConfiguration) each, result);
             } else if (each instanceof MaskRuleConfiguration) {
                 appendMaskDistSQL((MaskRuleConfiguration) each, result);
             } else {
@@ -180,53 +175,6 @@ public final class ConvertYamlConfigurationExecutor 
implements QueryableRALExecu
         }
     }
     
-    private void appendShadowDistSQL(final ShadowRuleConfiguration ruleConfig, 
final StringBuilder stringBuilder) {
-        if (ruleConfig.getDataSources().isEmpty()) {
-            return;
-        }
-        stringBuilder.append(DistSQLScriptConstants.CREATE_SHADOW);
-        Iterator<ShadowDataSourceConfiguration> iterator = 
ruleConfig.getDataSources().iterator();
-        while (iterator.hasNext()) {
-            ShadowDataSourceConfiguration dataSourceConfig = iterator.next();
-            String shadowRuleName = dataSourceConfig.getName();
-            String shadowTables = getShadowTables(shadowRuleName, 
ruleConfig.getTables(), ruleConfig.getShadowAlgorithms());
-            stringBuilder.append(
-                    String.format(DistSQLScriptConstants.SHADOW, 
shadowRuleName, dataSourceConfig.getProductionDataSourceName(), 
dataSourceConfig.getShadowDataSourceName(), shadowTables));
-            if (iterator.hasNext()) {
-                stringBuilder.append(DistSQLScriptConstants.COMMA);
-            }
-        }
-        
stringBuilder.append(DistSQLScriptConstants.SEMI).append(System.lineSeparator()).append(System.lineSeparator());
-    }
-    
-    private String getShadowTables(final String shadowRuleName, final 
Map<String, ShadowTableConfiguration> ruleConfig, final Map<String, 
AlgorithmConfiguration> algorithmConfigs) {
-        StringBuilder result = new StringBuilder();
-        Iterator<Entry<String, ShadowTableConfiguration>> iterator = 
ruleConfig.entrySet().iterator();
-        while (iterator.hasNext()) {
-            Entry<String, ShadowTableConfiguration> shadowTableConfig = 
iterator.next();
-            if 
(shadowTableConfig.getValue().getDataSourceNames().contains(shadowRuleName)) {
-                String shadowTableTypes = 
getShadowTableTypes(shadowTableConfig.getValue().getShadowAlgorithmNames(), 
algorithmConfigs);
-                
result.append(String.format(DistSQLScriptConstants.SHADOW_TABLE, 
shadowTableConfig.getKey(), shadowTableTypes));
-            }
-            if (iterator.hasNext()) {
-                
result.append(DistSQLScriptConstants.COMMA).append(System.lineSeparator());
-            }
-        }
-        return result.toString();
-    }
-    
-    private String getShadowTableTypes(final Collection<String> 
shadowAlgorithmNames, final Map<String, AlgorithmConfiguration> 
algorithmConfigs) {
-        StringBuilder result = new StringBuilder();
-        Iterator<String> iterator = shadowAlgorithmNames.iterator();
-        while (iterator.hasNext()) {
-            
result.append(getAlgorithmType(algorithmConfigs.get(iterator.next())));
-            if (iterator.hasNext()) {
-                result.append(DistSQLScriptConstants.COMMA).append(' ');
-            }
-        }
-        return result.toString();
-    }
-    
     private void appendMaskDistSQL(final MaskRuleConfiguration ruleConfig, 
final StringBuilder stringBuilder) {
         if (ruleConfig.getTables().isEmpty()) {
             return;

Reply via email to