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 2023/04/24 04:17:09 UTC

[shardingsphere] branch master updated: SHOW SHADOW RULES add algorithm information return (#25279)

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 4c48747c588 SHOW SHADOW RULES add algorithm information return (#25279)
4c48747c588 is described below

commit 4c48747c58877a1c9e5370b41b95d4f305118cf5
Author: yx9o <ya...@163.com>
AuthorDate: Mon Apr 24 12:17:01 2023 +0800

    SHOW SHADOW RULES add algorithm information return (#25279)
    
    * SHOW SHADOW RULES add algorithm information return
    
    * Update
    
    * Update
    
    * Update
    
    * Update
---
 .../handler/query/ShowShadowRuleExecutor.java      | 53 +++++++---------------
 .../distsql/query/ShowShadowRuleExecutorTest.java  | 32 +++++++++----
 2 files changed, 39 insertions(+), 46 deletions(-)

diff --git a/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/query/ShowShadowRuleExecutor.java b/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/query/ShowShadowRuleExecutor.java
index 486c576a149..8d84f8df88d 100644
--- a/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/query/ShowShadowRuleExecutor.java
+++ b/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/query/ShowShadowRuleExecutor.java
@@ -18,8 +18,10 @@
 package org.apache.shardingsphere.shadow.distsql.handler.query;
 
 import org.apache.shardingsphere.distsql.handler.query.RQLExecutor;
+import org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration;
 import org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow;
 import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
+import org.apache.shardingsphere.infra.util.props.PropertiesConverter;
 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;
@@ -29,8 +31,6 @@ import org.apache.shardingsphere.shadow.rule.ShadowRule;
 import java.util.Arrays;
 import java.util.Collection;
 import java.util.Collections;
-import java.util.HashMap;
-import java.util.Iterator;
 import java.util.LinkedHashMap;
 import java.util.LinkedList;
 import java.util.Map;
@@ -42,36 +42,22 @@ import java.util.stream.Collectors;
  */
 public final class ShowShadowRuleExecutor implements RQLExecutor<ShowShadowRulesStatement> {
     
-    private static final String RULE_NAME = "rule_name";
-    
-    private static final String SOURCE_NAME = "source_name";
-    
-    private static final String SHADOW_NAME = "shadow_name";
-    
-    private static final String SHADOW_TABLE = "shadow_table";
-    
     @Override
     public Collection<LocalDataQueryResultRow> getRows(final ShardingSphereDatabase database, final ShowShadowRulesStatement sqlStatement) {
         Optional<ShadowRule> rule = database.getRuleMetaData().findSingleRule(ShadowRule.class);
-        rule.ifPresent(optional -> buildDataSourceIterator((ShadowRuleConfiguration) optional.getConfiguration(), sqlStatement));
-        Iterator<Map<String, String>> data = Collections.emptyIterator();
-        if (rule.isPresent()) {
-            data = buildDataSourceIterator((ShadowRuleConfiguration) rule.get().getConfiguration(), sqlStatement);
-        }
         Collection<LocalDataQueryResultRow> result = new LinkedList<>();
-        while (data.hasNext()) {
-            Map<String, String> row = data.next();
-            result.add(new LocalDataQueryResultRow(row.get(RULE_NAME), row.get(SOURCE_NAME), row.get(SHADOW_NAME), row.getOrDefault(SHADOW_TABLE, "")));
+        if (rule.isPresent()) {
+            result = buildData((ShadowRuleConfiguration) rule.get().getConfiguration(), sqlStatement);
         }
         return result;
     }
     
-    private Iterator<Map<String, String>> buildDataSourceIterator(final ShadowRuleConfiguration ruleConfig, final ShowShadowRulesStatement sqlStatement) {
+    private Collection<LocalDataQueryResultRow> buildData(final ShadowRuleConfiguration ruleConfig, final ShowShadowRulesStatement sqlStatement) {
         Map<String, Map<String, ShadowTableConfiguration>> dataSourceTableMap = convertToDataSourceTableMap(ruleConfig.getTables());
         Collection<ShadowDataSourceConfiguration> specifiedConfigs = isSpecified(sqlStatement)
                 ? ruleConfig.getDataSources().stream().filter(each -> each.getName().equalsIgnoreCase(sqlStatement.getRuleName())).collect(Collectors.toList())
                 : ruleConfig.getDataSources();
-        return specifiedConfigs.stream().map(each -> buildDataItem(each, dataSourceTableMap)).collect(Collectors.toList()).iterator();
+        return specifiedConfigs.stream().map(each -> buildColumnData(each, dataSourceTableMap, ruleConfig.getShadowAlgorithms())).flatMap(Collection::stream).collect(Collectors.toList());
     }
     
     private Map<String, Map<String, ShadowTableConfiguration>> convertToDataSourceTableMap(final Map<String, ShadowTableConfiguration> tables) {
@@ -85,28 +71,21 @@ public final class ShowShadowRuleExecutor implements RQLExecutor<ShowShadowRules
         return null != sqlStatement.getRuleName() && !sqlStatement.getRuleName().isEmpty();
     }
     
-    private Map<String, String> buildDataItem(final ShadowDataSourceConfiguration dataSourceConfiguration, final Map<String, Map<String, ShadowTableConfiguration>> dataSourceTableMap) {
-        Map<String, String> result = convertToDataSourceMap(dataSourceConfiguration);
-        Map<String, ShadowTableConfiguration> dataSourceTable = dataSourceTableMap.getOrDefault(result.get(RULE_NAME), Collections.emptyMap());
-        result.put(SHADOW_TABLE, convertToString(dataSourceTable.keySet()));
-        return result;
-    }
-    
-    private Map<String, String> convertToDataSourceMap(final ShadowDataSourceConfiguration dataSourceConfiguration) {
-        Map<String, String> result = new HashMap<>();
-        result.put(RULE_NAME, dataSourceConfiguration.getName());
-        result.put(SOURCE_NAME, dataSourceConfiguration.getProductionDataSourceName());
-        result.put(SHADOW_NAME, dataSourceConfiguration.getShadowDataSourceName());
+    private Collection<LocalDataQueryResultRow> buildColumnData(final ShadowDataSourceConfiguration dataSourceConfig, final Map<String, Map<String, ShadowTableConfiguration>> dataSourceTableMap,
+                                                                final Map<String, AlgorithmConfiguration> algorithmConfigs) {
+        Map<String, ShadowTableConfiguration> dataSourceTable = dataSourceTableMap.getOrDefault(dataSourceConfig.getName(), Collections.emptyMap());
+        Collection<LocalDataQueryResultRow> result = new LinkedList<>();
+        dataSourceTable.forEach((key, value) -> value.getShadowAlgorithmNames().forEach(each -> {
+            AlgorithmConfiguration algorithmConfig = algorithmConfigs.get(each);
+            result.add(new LocalDataQueryResultRow(Arrays.asList(key, dataSourceConfig.getName(), dataSourceConfig.getProductionDataSourceName(), dataSourceConfig.getShadowDataSourceName(),
+                    algorithmConfig.getType(), PropertiesConverter.convert(algorithmConfig.getProps()))));
+        }));
         return result;
     }
     
-    private String convertToString(final Collection<String> shadowTables) {
-        return null == shadowTables ? "" : String.join(",", shadowTables);
-    }
-    
     @Override
     public Collection<String> getColumnNames() {
-        return Arrays.asList(RULE_NAME, SOURCE_NAME, SHADOW_NAME, SHADOW_TABLE);
+        return Arrays.asList("shadow_table", "rule_name", "source_name", "shadow_name", "algorithm_type", "algorithm_props");
     }
     
     @Override
diff --git a/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/query/ShowShadowRuleExecutorTest.java b/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/query/ShowShadowRuleExecutorTest.java
index 95c17ec3a2a..8481814e320 100644
--- a/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/query/ShowShadowRuleExecutorTest.java
+++ b/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/query/ShowShadowRuleExecutorTest.java
@@ -18,6 +18,7 @@
 package org.apache.shardingsphere.shadow.distsql.query;
 
 import org.apache.shardingsphere.distsql.handler.query.RQLExecutor;
+import org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration;
 import org.apache.shardingsphere.infra.config.rule.RuleConfiguration;
 import org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow;
 import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
@@ -33,6 +34,7 @@ import org.junit.jupiter.api.Test;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.Iterator;
+import java.util.Properties;
 
 import static org.hamcrest.CoreMatchers.is;
 import static org.hamcrest.MatcherAssert.assertThat;
@@ -46,25 +48,36 @@ class ShowShadowRuleExecutorTest {
     void assertGetRowData() {
         RQLExecutor<ShowShadowRulesStatement> executor = new ShowShadowRuleExecutor();
         Collection<LocalDataQueryResultRow> actual = executor.getRows(mockDatabase(), mock(ShowShadowRulesStatement.class));
-        assertThat(actual.size(), is(1));
+        assertThat(actual.size(), is(2));
         Iterator<LocalDataQueryResultRow> iterator = actual.iterator();
         LocalDataQueryResultRow row = iterator.next();
-        assertThat(row.getCell(1), is("shadow_rule"));
-        assertThat(row.getCell(2), is("source"));
-        assertThat(row.getCell(3), is("shadow"));
-        assertThat(row.getCell(4), is("t_order,t_order_1"));
+        assertThat(row.getCell(1), is("t_order"));
+        assertThat(row.getCell(2), is("shadow_rule"));
+        assertThat(row.getCell(3), is("source"));
+        assertThat(row.getCell(4), is("shadow"));
+        assertThat(row.getCell(5), is("REGEX_MATCH"));
+        assertThat(row.getCell(6), is(""));
+        row = iterator.next();
+        assertThat(row.getCell(1), is("t_order_item"));
+        assertThat(row.getCell(2), is("shadow_rule"));
+        assertThat(row.getCell(3), is("source"));
+        assertThat(row.getCell(4), is("shadow"));
+        assertThat(row.getCell(5), is("REGEX_MATCH"));
+        assertThat(row.getCell(6), is(""));
     }
     
     @Test
     void assertGetColumnNames() {
         RQLExecutor<ShowShadowRulesStatement> executor = new ShowShadowRuleExecutor();
         Collection<String> columns = executor.getColumnNames();
-        assertThat(columns.size(), is(4));
+        assertThat(columns.size(), is(6));
         Iterator<String> iterator = columns.iterator();
+        assertThat(iterator.next(), is("shadow_table"));
         assertThat(iterator.next(), is("rule_name"));
         assertThat(iterator.next(), is("source_name"));
         assertThat(iterator.next(), is("shadow_name"));
-        assertThat(iterator.next(), is("shadow_table"));
+        assertThat(iterator.next(), is("algorithm_type"));
+        assertThat(iterator.next(), is("algorithm_props"));
     }
     
     private ShardingSphereDatabase mockDatabase() {
@@ -78,8 +91,9 @@ class ShowShadowRuleExecutorTest {
     private RuleConfiguration createRuleConfiguration() {
         ShadowRuleConfiguration result = new ShadowRuleConfiguration();
         result.getDataSources().add(new ShadowDataSourceConfiguration("shadow_rule", "source", "shadow"));
-        result.getTables().put("t_order", new ShadowTableConfiguration(Collections.singletonList("shadow_rule"), Collections.emptyList()));
-        result.getTables().put("t_order_1", new ShadowTableConfiguration(Collections.singletonList("shadow_rule"), Collections.emptyList()));
+        result.getShadowAlgorithms().put("user_id_select_match_algorithm", new AlgorithmConfiguration("REGEX_MATCH", new Properties()));
+        result.getTables().put("t_order", new ShadowTableConfiguration(Collections.singletonList("shadow_rule"), Collections.singletonList("user_id_select_match_algorithm")));
+        result.getTables().put("t_order_item", new ShadowTableConfiguration(Collections.singletonList("shadow_rule"), Collections.singletonList("user_id_select_match_algorithm")));
         return result;
     }
 }