You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by du...@apache.org on 2022/06/17 09:46:03 UTC

[shardingsphere] branch master updated: add actualDataNodes for auto tables. (#18372)

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

duanzhengqiang 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 9e2fb93cd96 add actualDataNodes for auto tables. (#18372)
9e2fb93cd96 is described below

commit 9e2fb93cd96095dda37928ebf231edc888d026e9
Author: Chuxin Chen <ch...@qq.com>
AuthorDate: Fri Jun 17 17:45:58 2022 +0800

    add actualDataNodes for auto tables. (#18372)
    
    * add actualDataNodes for auto tables.
    
    * add actualDataNodes for auto tables.
    
    * add actualDataNodes for auto tables.
    
    * add actualDataNodes for auto tables.
    
    * add actualDataNodes for auto tables.
---
 .../rule/ShardingAutoTableRuleConfiguration.java   |  3 ++
 .../shardingsphere/sharding/rule/TableRule.java    | 18 ++-----
 .../YamlShardingAutoTableRuleConfiguration.java    |  2 +
 ...eAlgorithmProviderConfigurationYamlSwapper.java |  8 +--
 .../ShardingRuleConfigurationYamlSwapper.java      |  8 +--
 ...rdingAutoTableRuleConfigurationYamlSwapper.java | 63 ++++++++++++++++++++++
 ...gAutoTableRuleConfigurationYamlSwapperTest.java | 37 ++++++++++++-
 .../infra/datanode/DataNodeUtil.java               | 21 ++++++++
 8 files changed, 136 insertions(+), 24 deletions(-)

diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-api/src/main/java/org/apache/shardingsphere/sharding/api/config/rule/ShardingAutoTableRuleConfiguration.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-api/src/main/java/org/apache/shardingsphere/sharding/api/config/rule/ShardingAutoTableRuleConfiguration.java
index 5fda87e8fb1..70bcf9ed72f 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-api/src/main/java/org/apache/shardingsphere/sharding/api/config/rule/ShardingAutoTableRuleConfiguration.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-api/src/main/java/org/apache/shardingsphere/sharding/api/config/rule/ShardingAutoTableRuleConfiguration.java
@@ -41,6 +41,9 @@ public final class ShardingAutoTableRuleConfiguration {
     
     private KeyGenerateStrategyConfiguration keyGenerateStrategy;
     
+    // TODO prevent user to config this.
+    private String actualDataNodes;
+    
     public ShardingAutoTableRuleConfiguration(final String logicTable) {
         this(logicTable, null);
     }
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/rule/TableRule.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/rule/TableRule.java
index 6a17a78c962..ce6eac888a6 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/rule/TableRule.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/rule/TableRule.java
@@ -38,7 +38,6 @@ import java.util.Collection;
 import java.util.Collections;
 import java.util.Comparator;
 import java.util.HashMap;
-import java.util.Iterator;
 import java.util.LinkedHashSet;
 import java.util.LinkedList;
 import java.util.List;
@@ -153,24 +152,15 @@ public final class TableRule {
     }
     
     private List<String> getDataNodes(final ShardingAutoTableRuleConfiguration tableRuleConfig, final ShardingAutoTableAlgorithm shardingAlgorithm, final Collection<String> dataSourceNames) {
+        if (null != tableRuleConfig.getActualDataNodes() && !tableRuleConfig.getActualDataNodes().isEmpty()) {
+            return new InlineExpressionParser(tableRuleConfig.getActualDataNodes()).splitAndEvaluate();
+        }
         if (null == tableShardingStrategyConfig) {
             return new LinkedList<>();
         }
         List<String> dataSources = Strings.isNullOrEmpty(tableRuleConfig.getActualDataSources()) ? new LinkedList<>(dataSourceNames)
                 : new InlineExpressionParser(tableRuleConfig.getActualDataSources()).splitAndEvaluate();
-        return fillDataSourceNames(shardingAlgorithm.getAutoTablesAmount(), dataSources);
-    }
-    
-    private List<String> fillDataSourceNames(final int amount, final List<String> dataSources) {
-        List<String> result = new LinkedList<>();
-        Iterator<String> iterator = dataSources.iterator();
-        for (int i = 0; i < amount; i++) {
-            if (!iterator.hasNext()) {
-                iterator = dataSources.iterator();
-            }
-            result.add(String.format("%s.%s_%s", iterator.next(), logicTable, i));
-        }
-        return result;
+        return DataNodeUtil.getFormatDataNodes(shardingAlgorithm.getAutoTablesAmount(), logicTable, dataSources);
     }
     
     private Set<String> getActualTables() {
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/yaml/config/rule/YamlShardingAutoTableRuleConfiguration.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/yaml/config/rule/YamlShardingAutoTableRuleConfiguration.java
index 14d8ac3f5f4..c12629b04c5 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/yaml/config/rule/YamlShardingAutoTableRuleConfiguration.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/yaml/config/rule/YamlShardingAutoTableRuleConfiguration.java
@@ -39,4 +39,6 @@ public final class YamlShardingAutoTableRuleConfiguration implements YamlConfigu
     private YamlShardingStrategyConfiguration shardingStrategy;
     
     private YamlKeyGenerateStrategyConfiguration keyGenerateStrategy;
+    
+    private String actualDataNodes;
 }
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/yaml/swapper/ShardingRuleAlgorithmProviderConfigurationYamlSwapper.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/yaml/swapper/ShardingRuleAlgorithmProviderConfigurationYamlSwapper.java
index 3fc52abb24e..8daae9a4817 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/yaml/swapper/ShardingRuleAlgorithmProviderConfigurationYamlSwapper.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/yaml/swapper/ShardingRuleAlgorithmProviderConfigurationYamlSwapper.java
@@ -29,6 +29,7 @@ import org.apache.shardingsphere.sharding.yaml.swapper.rule.ShardingTableRuleCon
 import org.apache.shardingsphere.sharding.yaml.swapper.strategy.KeyGenerateStrategyConfigurationYamlSwapper;
 import org.apache.shardingsphere.sharding.yaml.swapper.strategy.ShardingStrategyConfigurationYamlSwapper;
 
+import java.util.Collections;
 import java.util.Map.Entry;
 
 /**
@@ -38,8 +39,6 @@ public final class ShardingRuleAlgorithmProviderConfigurationYamlSwapper impleme
     
     private final ShardingTableRuleConfigurationYamlSwapper tableYamlSwapper = new ShardingTableRuleConfigurationYamlSwapper();
     
-    private final ShardingAutoTableRuleConfigurationYamlSwapper autoTableYamlSwapper = new ShardingAutoTableRuleConfigurationYamlSwapper();
-    
     private final ShardingStrategyConfigurationYamlSwapper shardingStrategyYamlSwapper = new ShardingStrategyConfigurationYamlSwapper();
     
     private final KeyGenerateStrategyConfigurationYamlSwapper keyGenerateStrategyYamlSwapper = new KeyGenerateStrategyConfigurationYamlSwapper();
@@ -48,7 +47,8 @@ public final class ShardingRuleAlgorithmProviderConfigurationYamlSwapper impleme
     public YamlShardingRuleConfiguration swapToYamlConfiguration(final AlgorithmProvidedShardingRuleConfiguration data) {
         YamlShardingRuleConfiguration result = new YamlShardingRuleConfiguration();
         data.getTables().forEach(each -> result.getTables().put(each.getLogicTable(), tableYamlSwapper.swapToYamlConfiguration(each)));
-        data.getAutoTables().forEach(each -> result.getAutoTables().put(each.getLogicTable(), autoTableYamlSwapper.swapToYamlConfiguration(each)));
+        data.getAutoTables().forEach(each -> result.getAutoTables().put(each.getLogicTable(), new ShardingAutoTableRuleConfigurationYamlSwapper(data.getShardingAlgorithms(), Collections.emptyMap())
+                .swapToYamlConfiguration(each)));
         result.getBindingTables().addAll(data.getBindingTableGroups());
         result.getBroadcastTables().addAll(data.getBroadcastTables());
         setYamlDefaultStrategies(data, result);
@@ -67,7 +67,7 @@ public final class ShardingRuleAlgorithmProviderConfigurationYamlSwapper impleme
         for (Entry<String, YamlShardingAutoTableRuleConfiguration> entry : yamlConfig.getAutoTables().entrySet()) {
             YamlShardingAutoTableRuleConfiguration tableRuleConfig = entry.getValue();
             tableRuleConfig.setLogicTable(entry.getKey());
-            result.getAutoTables().add(autoTableYamlSwapper.swapToObject(tableRuleConfig));
+            result.getAutoTables().add(new ShardingAutoTableRuleConfigurationYamlSwapper(Collections.emptyMap(), Collections.emptyMap()).swapToObject(tableRuleConfig));
         }
         result.getBindingTableGroups().addAll(yamlConfig.getBindingTables());
         result.getBroadcastTables().addAll(yamlConfig.getBroadcastTables());
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/yaml/swapper/ShardingRuleConfigurationYamlSwapper.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/yaml/swapper/ShardingRuleConfigurationYamlSwapper.java
index 21b173e533e..4194f3a9839 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/yaml/swapper/ShardingRuleConfigurationYamlSwapper.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/yaml/swapper/ShardingRuleConfigurationYamlSwapper.java
@@ -30,6 +30,7 @@ import org.apache.shardingsphere.sharding.yaml.swapper.rule.ShardingTableRuleCon
 import org.apache.shardingsphere.sharding.yaml.swapper.strategy.KeyGenerateStrategyConfigurationYamlSwapper;
 import org.apache.shardingsphere.sharding.yaml.swapper.strategy.ShardingStrategyConfigurationYamlSwapper;
 
+import java.util.Collections;
 import java.util.Map.Entry;
 
 /**
@@ -39,8 +40,6 @@ public final class ShardingRuleConfigurationYamlSwapper implements YamlRuleConfi
     
     private final ShardingTableRuleConfigurationYamlSwapper tableYamlSwapper = new ShardingTableRuleConfigurationYamlSwapper();
     
-    private final ShardingAutoTableRuleConfigurationYamlSwapper autoTableYamlSwapper = new ShardingAutoTableRuleConfigurationYamlSwapper();
-    
     private final ShardingStrategyConfigurationYamlSwapper shardingStrategyYamlSwapper = new ShardingStrategyConfigurationYamlSwapper();
     
     private final KeyGenerateStrategyConfigurationYamlSwapper keyGenerateStrategyYamlSwapper = new KeyGenerateStrategyConfigurationYamlSwapper();
@@ -53,7 +52,8 @@ public final class ShardingRuleConfigurationYamlSwapper implements YamlRuleConfi
     public YamlShardingRuleConfiguration swapToYamlConfiguration(final ShardingRuleConfiguration data) {
         YamlShardingRuleConfiguration result = new YamlShardingRuleConfiguration();
         data.getTables().forEach(each -> result.getTables().put(each.getLogicTable(), tableYamlSwapper.swapToYamlConfiguration(each)));
-        data.getAutoTables().forEach(each -> result.getAutoTables().put(each.getLogicTable(), autoTableYamlSwapper.swapToYamlConfiguration(each)));
+        data.getAutoTables().forEach(each -> result.getAutoTables().put(each.getLogicTable(),
+                new ShardingAutoTableRuleConfigurationYamlSwapper(Collections.emptyMap(), data.getShardingAlgorithms()).swapToYamlConfiguration(each)));
         result.getBindingTables().addAll(data.getBindingTableGroups());
         result.getBroadcastTables().addAll(data.getBroadcastTables());
         setYamlDefaultStrategies(data, result);
@@ -98,7 +98,7 @@ public final class ShardingRuleConfigurationYamlSwapper implements YamlRuleConfi
         for (Entry<String, YamlShardingAutoTableRuleConfiguration> entry : yamlConfig.getAutoTables().entrySet()) {
             YamlShardingAutoTableRuleConfiguration tableRuleConfig = entry.getValue();
             tableRuleConfig.setLogicTable(entry.getKey());
-            result.getAutoTables().add(autoTableYamlSwapper.swapToObject(tableRuleConfig));
+            result.getAutoTables().add(new ShardingAutoTableRuleConfigurationYamlSwapper(Collections.emptyMap(), Collections.emptyMap()).swapToObject(tableRuleConfig));
         }
         result.getBindingTableGroups().addAll(yamlConfig.getBindingTables());
         result.getBroadcastTables().addAll(yamlConfig.getBroadcastTables());
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/yaml/swapper/rule/ShardingAutoTableRuleConfigurationYamlSwapper.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/yaml/swapper/rule/ShardingAutoTableRuleConfigurationYamlSwapper.java
index a6a903cc064..e20ad40e04f 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/yaml/swapper/rule/ShardingAutoTableRuleConfigurationYamlSwapper.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/yaml/swapper/rule/ShardingAutoTableRuleConfigurationYamlSwapper.java
@@ -18,21 +18,41 @@
 package org.apache.shardingsphere.sharding.yaml.swapper.rule;
 
 import com.google.common.base.Preconditions;
+import lombok.RequiredArgsConstructor;
+import org.apache.shardingsphere.infra.config.algorithm.ShardingSphereAlgorithmConfiguration;
+import org.apache.shardingsphere.infra.datanode.DataNodeUtil;
+import org.apache.shardingsphere.infra.expr.InlineExpressionParser;
 import org.apache.shardingsphere.infra.yaml.config.swapper.YamlConfigurationSwapper;
 import org.apache.shardingsphere.sharding.api.config.rule.ShardingAutoTableRuleConfiguration;
+import org.apache.shardingsphere.sharding.api.sharding.ShardingAutoTableAlgorithm;
+import org.apache.shardingsphere.sharding.factory.ShardingAlgorithmFactory;
+import org.apache.shardingsphere.sharding.spi.ShardingAlgorithm;
 import org.apache.shardingsphere.sharding.yaml.config.rule.YamlShardingAutoTableRuleConfiguration;
 import org.apache.shardingsphere.sharding.yaml.swapper.strategy.KeyGenerateStrategyConfigurationYamlSwapper;
 import org.apache.shardingsphere.sharding.yaml.swapper.strategy.ShardingStrategyConfigurationYamlSwapper;
 
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+
 /**
  * Sharding auto table rule configuration YAML swapper.
  */
+@RequiredArgsConstructor
 public final class ShardingAutoTableRuleConfigurationYamlSwapper implements YamlConfigurationSwapper<YamlShardingAutoTableRuleConfiguration, ShardingAutoTableRuleConfiguration> {
     
     private final ShardingStrategyConfigurationYamlSwapper shardingStrategyYamlSwapper = new ShardingStrategyConfigurationYamlSwapper();
     
     private final KeyGenerateStrategyConfigurationYamlSwapper keyGenerateStrategyYamlSwapper = new KeyGenerateStrategyConfigurationYamlSwapper();
     
+    // TODO remove after refactoring auto table actual data node.
+    private final Map<String, ShardingAlgorithm> shardingAlgorithms;
+    
+    // TODO remove after refactoring auto table actual data node.
+    private final Map<String, ShardingSphereAlgorithmConfiguration> shardingAlgorithmConfigs;
+    
     @Override
     public YamlShardingAutoTableRuleConfiguration swapToYamlConfiguration(final ShardingAutoTableRuleConfiguration data) {
         YamlShardingAutoTableRuleConfiguration result = new YamlShardingAutoTableRuleConfiguration();
@@ -45,6 +65,11 @@ public final class ShardingAutoTableRuleConfigurationYamlSwapper implements Yaml
         if (null != data.getKeyGenerateStrategy()) {
             result.setKeyGenerateStrategy(keyGenerateStrategyYamlSwapper.swapToYamlConfiguration(data.getKeyGenerateStrategy()));
         }
+        if (null != data.getActualDataNodes() && !data.getActualDataNodes().isEmpty()) {
+            result.setActualDataNodes(data.getActualDataNodes());
+        } else if (!shardingAlgorithms.isEmpty() || !shardingAlgorithmConfigs.isEmpty()) {
+            setActualDataNodesWithAlgorithms(data, result);
+        }
         return result;
     }
     
@@ -53,6 +78,7 @@ public final class ShardingAutoTableRuleConfigurationYamlSwapper implements Yaml
         Preconditions.checkNotNull(yamlConfig.getLogicTable(), "Logic table cannot be null.");
         ShardingAutoTableRuleConfiguration result = new ShardingAutoTableRuleConfiguration(yamlConfig.getLogicTable(), yamlConfig.getActualDataSources());
         result.setActualTablePrefix(yamlConfig.getActualTablePrefix());
+        result.setActualDataNodes(yamlConfig.getActualDataNodes());
         if (null != yamlConfig.getShardingStrategy()) {
             result.setShardingStrategy(shardingStrategyYamlSwapper.swapToObject(yamlConfig.getShardingStrategy()));
         }
@@ -61,4 +87,41 @@ public final class ShardingAutoTableRuleConfigurationYamlSwapper implements Yaml
         }
         return result;
     }
+    
+    private void setActualDataNodesWithAlgorithms(final ShardingAutoTableRuleConfiguration data, final YamlShardingAutoTableRuleConfiguration result) {
+        if (null != data.getActualDataSources() && !data.getActualDataSources().isEmpty()) {
+            getShardingCountWithAlgorithms(data, shardingAlgorithms).ifPresent(shardingCount -> result.setActualDataNodes(getActualDataNodes(data, shardingCount)));
+            getShardingCountWithAlgorithmConfig(data, shardingAlgorithmConfigs).ifPresent(shardingCount -> result.setActualDataNodes(getActualDataNodes(data, shardingCount)));
+        }
+    }
+    
+    private Optional<Integer> getShardingCountWithAlgorithms(final ShardingAutoTableRuleConfiguration configuration, final Map<String, ShardingAlgorithm> shardingAlgorithms) {
+        if (null != configuration.getShardingStrategy() && shardingAlgorithms.containsKey(configuration.getShardingStrategy().getShardingAlgorithmName())) {
+            ShardingAlgorithm algorithm = shardingAlgorithms.get(configuration.getShardingStrategy().getShardingAlgorithmName());
+            if (algorithm instanceof ShardingAutoTableAlgorithm) {
+                return Optional.of(((ShardingAutoTableAlgorithm) algorithm).getAutoTablesAmount());
+            }
+        }
+        return Optional.empty();
+    }
+    
+    private Optional<Integer> getShardingCountWithAlgorithmConfig(final ShardingAutoTableRuleConfiguration configuration, final Map<String, ShardingSphereAlgorithmConfiguration> shardingAlgorithms) {
+        if (null != configuration.getShardingStrategy() && shardingAlgorithms.containsKey(configuration.getShardingStrategy().getShardingAlgorithmName())) {
+            ShardingAlgorithm algorithm = ShardingAlgorithmFactory.newInstance(shardingAlgorithms.get(configuration.getShardingStrategy().getShardingAlgorithmName()));
+            if (algorithm instanceof ShardingAutoTableAlgorithm) {
+                return Optional.of(((ShardingAutoTableAlgorithm) algorithm).getAutoTablesAmount());
+            }
+        }
+        return Optional.empty();
+    }
+    
+    private String getActualDataNodes(final ShardingAutoTableRuleConfiguration data, final int shardingCount) {
+        Collection<String> dataSourceNames = getDataSourceNames(data.getActualDataSources());
+        return String.join(",", DataNodeUtil.getFormatDataNodes(shardingCount, data.getLogicTable(), dataSourceNames));
+    }
+    
+    private Collection<String> getDataSourceNames(final String dataSources) {
+        List<String> actualDataSources = new InlineExpressionParser(dataSources).splitAndEvaluate();
+        return new HashSet<>(actualDataSources);
+    }
 }
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/swapper/rule/ShardingAutoTableRuleConfigurationYamlSwapperTest.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/swapper/rule/ShardingAutoTableRuleConfigurationYamlSwapperTest.java
index 6962c95eb46..c091a34040c 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/swapper/rule/ShardingAutoTableRuleConfigurationYamlSwapperTest.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/swapper/rule/ShardingAutoTableRuleConfigurationYamlSwapperTest.java
@@ -21,6 +21,8 @@ import org.apache.shardingsphere.infra.yaml.config.swapper.YamlConfigurationSwap
 import org.apache.shardingsphere.sharding.api.config.rule.ShardingAutoTableRuleConfiguration;
 import org.apache.shardingsphere.sharding.api.config.strategy.keygen.KeyGenerateStrategyConfiguration;
 import org.apache.shardingsphere.sharding.api.config.strategy.sharding.StandardShardingStrategyConfiguration;
+import org.apache.shardingsphere.sharding.api.sharding.ShardingAutoTableAlgorithm;
+import org.apache.shardingsphere.sharding.spi.ShardingAlgorithm;
 import org.apache.shardingsphere.sharding.yaml.config.rule.YamlShardingAutoTableRuleConfiguration;
 import org.apache.shardingsphere.sharding.yaml.config.strategy.keygen.YamlKeyGenerateStrategyConfiguration;
 import org.apache.shardingsphere.sharding.yaml.config.strategy.sharding.YamlShardingStrategyConfiguration;
@@ -35,6 +37,9 @@ import org.mockito.Mock;
 import org.mockito.junit.MockitoJUnitRunner;
 
 import java.lang.reflect.Field;
+import java.util.Collections;
+import java.util.LinkedHashMap;
+import java.util.Map;
 
 import static org.hamcrest.CoreMatchers.is;
 import static org.junit.Assert.assertNotNull;
@@ -42,6 +47,7 @@ import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertThat;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
+import static org.mockito.Mockito.withSettings;
 
 @RunWith(MockitoJUnitRunner.class)
 public final class ShardingAutoTableRuleConfigurationYamlSwapperTest {
@@ -52,7 +58,7 @@ public final class ShardingAutoTableRuleConfigurationYamlSwapperTest {
     @Mock
     private KeyGenerateStrategyConfigurationYamlSwapper keyGenerateStrategyYamlSwapper;
     
-    private final ShardingAutoTableRuleConfigurationYamlSwapper tableYamlSwapper = new ShardingAutoTableRuleConfigurationYamlSwapper();
+    private final ShardingAutoTableRuleConfigurationYamlSwapper tableYamlSwapper = new ShardingAutoTableRuleConfigurationYamlSwapper(mockAlgorithms(), Collections.emptyMap());
     
     @Before
     public void setUp() throws ReflectiveOperationException {
@@ -83,16 +89,43 @@ public final class ShardingAutoTableRuleConfigurationYamlSwapperTest {
         shardingTableRuleConfig.setActualTablePrefix("tmp_");
         shardingTableRuleConfig.setShardingStrategy(mock(StandardShardingStrategyConfiguration.class));
         shardingTableRuleConfig.setKeyGenerateStrategy(mock(KeyGenerateStrategyConfiguration.class));
+        shardingTableRuleConfig.setActualDataNodes("ds0.tbl_0");
         YamlShardingAutoTableRuleConfiguration actual = tableYamlSwapper.swapToYamlConfiguration(shardingTableRuleConfig);
         assertThat(actual.getLogicTable(), is("tbl"));
         assertThat(actual.getActualDataSources(), is("ds0,ds1"));
         assertThat(actual.getActualTablePrefix(), is("tmp_"));
+        assertThat(actual.getActualDataNodes(), is("ds0.tbl_0"));
         assertNotNull(actual.getShardingStrategy());
         assertNotNull(actual.getKeyGenerateStrategy());
     }
     
+    @Test
+    public void assertSwapToYamlAutoAddActualDataNodes() {
+        ShardingAutoTableRuleConfiguration shardingTableRuleConfig = new ShardingAutoTableRuleConfiguration("tbl", "ds0,ds1");
+        shardingTableRuleConfig.setActualTablePrefix("tmp_");
+        StandardShardingStrategyConfiguration strategyConfiguration = mock(StandardShardingStrategyConfiguration.class);
+        when(strategyConfiguration.getShardingAlgorithmName()).thenReturn("mod_2");
+        shardingTableRuleConfig.setShardingStrategy(strategyConfiguration);
+        shardingTableRuleConfig.setKeyGenerateStrategy(mock(KeyGenerateStrategyConfiguration.class));
+        YamlShardingAutoTableRuleConfiguration actual = tableYamlSwapper.swapToYamlConfiguration(shardingTableRuleConfig);
+        assertThat(actual.getLogicTable(), is("tbl"));
+        assertThat(actual.getActualDataSources(), is("ds0,ds1"));
+        assertThat(actual.getActualTablePrefix(), is("tmp_"));
+        assertThat(actual.getActualDataNodes(), is("ds0.tbl_0,ds1.tbl_1"));
+        assertNotNull(actual.getShardingStrategy());
+        assertNotNull(actual.getKeyGenerateStrategy());
+    }
+    
+    private Map<String, ShardingAlgorithm> mockAlgorithms() {
+        Map<String, ShardingAlgorithm> result = new LinkedHashMap<>();
+        ShardingAlgorithm algorithm = mock(ShardingAlgorithm.class, withSettings().extraInterfaces(ShardingAutoTableAlgorithm.class));
+        when(((ShardingAutoTableAlgorithm) algorithm).getAutoTablesAmount()).thenReturn(2);
+        result.put("mod_2", algorithm);
+        return result;
+    }
+    
     @Test(expected = NullPointerException.class)
     public void assertSwapToObjectWithoutLogicTable() {
-        new ShardingAutoTableRuleConfigurationYamlSwapper().swapToObject(new YamlShardingAutoTableRuleConfiguration());
+        new ShardingAutoTableRuleConfigurationYamlSwapper(Collections.emptyMap(), Collections.emptyMap()).swapToObject(new YamlShardingAutoTableRuleConfiguration());
     }
 }
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/datanode/DataNodeUtil.java b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/datanode/DataNodeUtil.java
index 520c39d6b20..7b9b88722b4 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/datanode/DataNodeUtil.java
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/datanode/DataNodeUtil.java
@@ -22,6 +22,7 @@ import lombok.NoArgsConstructor;
 
 import java.util.Collection;
 import java.util.Collections;
+import java.util.Iterator;
 import java.util.LinkedHashMap;
 import java.util.LinkedList;
 import java.util.List;
@@ -68,4 +69,24 @@ public final class DataNodeUtil {
         }
         return result;
     }
+    
+    /**
+     * Get format data nodes.
+     * 
+     * @param amount amount
+     * @param logicTable logic table
+     * @param dataSources data source names
+     * @return data node list
+     */
+    public static List<String> getFormatDataNodes(final int amount, final String logicTable, final Collection<String> dataSources) {
+        List<String> result = new LinkedList<>();
+        Iterator<String> iterator = dataSources.iterator();
+        for (int i = 0; i < amount; i++) {
+            if (!iterator.hasNext()) {
+                iterator = dataSources.iterator();
+            }
+            result.add(String.format("%s.%s_%s", iterator.next(), logicTable, i));
+        }
+        return result;
+    }
 }