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

[shardingsphere] branch master updated: Remove single table duplicate check and add some document to describe single table usage (#18698)

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

panjuan 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 1bb29cfd1d1 Remove single table duplicate check and add some document to describe single table usage (#18698)
1bb29cfd1d1 is described below

commit 1bb29cfd1d120ba9fcb10ba273f577f61dfc14c6
Author: Zhengqiang Duan <du...@apache.org>
AuthorDate: Wed Jun 29 17:43:59 2022 +0800

    Remove single table duplicate check and add some document to describe single table usage (#18698)
    
    * Remove single table duplicate check and add some document to describe single table usage
    
    * optimize document
---
 .../content/features/sharding/concept/table.cn.md  |  1 +
 .../content/features/sharding/concept/table.en.md  |  1 +
 .../datanode/SingleTableDataNodeLoader.java        | 13 --------
 .../datanode/SingleTableDataNodeLoaderTest.java    | 36 ++++++++++++++--------
 4 files changed, 26 insertions(+), 25 deletions(-)

diff --git a/docs/document/content/features/sharding/concept/table.cn.md b/docs/document/content/features/sharding/concept/table.cn.md
index cb4c44afca8..11bb7ba1e2b 100644
--- a/docs/document/content/features/sharding/concept/table.cn.md
+++ b/docs/document/content/features/sharding/concept/table.cn.md
@@ -60,3 +60,4 @@ SELECT i.* FROM t_order_1 o JOIN t_order_item_1 i ON o.order_id=i.order_id WHERE
 
 指所有的分片数据源中仅唯一存在的表。
 适用于数据量不大且无需分片的表。
+用户需要保证单表的唯一性,如果单表在同一个数据库的同一个 schema 下重复,ShardingSphere 只会加载第一个单表用于 SQL 路由。
diff --git a/docs/document/content/features/sharding/concept/table.en.md b/docs/document/content/features/sharding/concept/table.en.md
index e63efa0a915..c0b4fb3f5de 100644
--- a/docs/document/content/features/sharding/concept/table.en.md
+++ b/docs/document/content/features/sharding/concept/table.en.md
@@ -60,3 +60,4 @@ It can be applied to the small data volume that needs to correlate with big data
 
 It refers to only one table that exists in all sharding database sources. 
 It is suitable for little data in table without sharding.
+Users need to ensure that single table is unique. If single table is repeated under the same schema in the same database, ShardingSphere will only load the first single table for sql route.
diff --git a/shardingsphere-kernel/shardingsphere-single-table/shardingsphere-single-table-core/src/main/java/org/apache/shardingsphere/singletable/datanode/SingleTableDataNodeLoader.java b/shardingsphere-kernel/shardingsphere-single-table/shardingsphere-single-table-core/src/main/java/org/apache/shardingsphere/singletable/datanode/SingleTableDataNodeLoader.java
index 483e9baf7ec..66bc7aa00fa 100644
--- a/shardingsphere-kernel/shardingsphere-single-table/shardingsphere-single-table-core/src/main/java/org/apache/shardingsphere/singletable/datanode/SingleTableDataNodeLoader.java
+++ b/shardingsphere-kernel/shardingsphere-single-table/shardingsphere-single-table-core/src/main/java/org/apache/shardingsphere/singletable/datanode/SingleTableDataNodeLoader.java
@@ -17,7 +17,6 @@
 
 package org.apache.shardingsphere.singletable.datanode;
 
-import com.google.common.base.Preconditions;
 import lombok.AccessLevel;
 import lombok.NoArgsConstructor;
 import org.apache.shardingsphere.infra.config.exception.ShardingSphereConfigurationException;
@@ -28,7 +27,6 @@ import org.apache.shardingsphere.infra.metadata.database.schema.loader.common.Sc
 import javax.sql.DataSource;
 import java.sql.SQLException;
 import java.util.Collection;
-import java.util.HashSet;
 import java.util.LinkedHashMap;
 import java.util.LinkedHashSet;
 import java.util.LinkedList;
@@ -61,7 +59,6 @@ public final class SingleTableDataNodeLoader {
                 Collection<DataNode> existDataNodes = result.getOrDefault(each.toLowerCase(), new LinkedHashSet<>(addedDataNodes.size(), 1));
                 existDataNodes.addAll(addedDataNodes);
                 result.putIfAbsent(each.toLowerCase(), existDataNodes);
-                Preconditions.checkState(!containsDuplicateTable(existDataNodes), "Single table conflict, there are multiple tables `%s` existed.", each);
             }
         }
         return result;
@@ -86,16 +83,6 @@ public final class SingleTableDataNodeLoader {
         return result;
     }
     
-    private static boolean containsDuplicateTable(final Collection<DataNode> dataNodes) {
-        Collection<String> schemas = new HashSet<>(dataNodes.size(), 1);
-        for (DataNode each : dataNodes) {
-            if (!schemas.add(each.getSchemaName())) {
-                return true;
-            }
-        }
-        return false;
-    }
-    
     private static Map<String, Collection<String>> loadSchemaTableNames(final String databaseName, final DatabaseType databaseType, final DataSource dataSource, final String dataSourceName) {
         try {
             return SchemaTableNamesLoader.loadSchemaTableNames(databaseName, databaseType, dataSource);
diff --git a/shardingsphere-kernel/shardingsphere-single-table/shardingsphere-single-table-core/src/test/java/org/apache/shardingsphere/singletable/datanode/SingleTableDataNodeLoaderTest.java b/shardingsphere-kernel/shardingsphere-single-table/shardingsphere-single-table-core/src/test/java/org/apache/shardingsphere/singletable/datanode/SingleTableDataNodeLoaderTest.java
index ddc9ad45367..c3b10567c59 100644
--- a/shardingsphere-kernel/shardingsphere-single-table/shardingsphere-single-table-core/src/test/java/org/apache/shardingsphere/singletable/datanode/SingleTableDataNodeLoaderTest.java
+++ b/shardingsphere-kernel/shardingsphere-single-table/shardingsphere-single-table-core/src/test/java/org/apache/shardingsphere/singletable/datanode/SingleTableDataNodeLoaderTest.java
@@ -83,20 +83,32 @@ public final class SingleTableDataNodeLoaderTest {
     @Test
     public void assertLoad() {
         Collection<String> excludedTables = Arrays.asList("salary", "employee", "student");
-        Map<String, Collection<DataNode>> dataNodeMap = SingleTableDataNodeLoader.load(DefaultDatabase.LOGIC_NAME, mock(DatabaseType.class), dataSourceMap, excludedTables);
-        assertFalse(dataNodeMap.containsKey("employee"));
-        assertFalse(dataNodeMap.containsKey("salary"));
-        assertFalse(dataNodeMap.containsKey("student"));
-        assertTrue(dataNodeMap.containsKey("dept"));
-        assertTrue(dataNodeMap.containsKey("teacher"));
-        assertTrue(dataNodeMap.containsKey("class"));
-        assertThat(dataNodeMap.get("dept").iterator().next().getDataSourceName(), is("ds0"));
-        assertThat(dataNodeMap.get("teacher").iterator().next().getDataSourceName(), is("ds1"));
-        assertThat(dataNodeMap.get("class").iterator().next().getDataSourceName(), is("ds1"));
+        Map<String, Collection<DataNode>> actual = SingleTableDataNodeLoader.load(DefaultDatabase.LOGIC_NAME, mock(DatabaseType.class), dataSourceMap, excludedTables);
+        assertFalse(actual.containsKey("employee"));
+        assertFalse(actual.containsKey("salary"));
+        assertFalse(actual.containsKey("student"));
+        assertTrue(actual.containsKey("dept"));
+        assertTrue(actual.containsKey("teacher"));
+        assertTrue(actual.containsKey("class"));
+        assertThat(actual.get("dept").iterator().next().getDataSourceName(), is("ds0"));
+        assertThat(actual.get("teacher").iterator().next().getDataSourceName(), is("ds1"));
+        assertThat(actual.get("class").iterator().next().getDataSourceName(), is("ds1"));
     }
     
-    @Test(expected = IllegalStateException.class)
+    @Test
     public void assertLoadWithConflictTables() {
-        SingleTableDataNodeLoader.load(DefaultDatabase.LOGIC_NAME, mock(DatabaseType.class), dataSourceMap, Collections.emptyList());
+        Map<String, Collection<DataNode>> actual = SingleTableDataNodeLoader.load(DefaultDatabase.LOGIC_NAME, mock(DatabaseType.class), dataSourceMap, Collections.emptyList());
+        assertTrue(actual.containsKey("employee"));
+        assertTrue(actual.containsKey("salary"));
+        assertTrue(actual.containsKey("student"));
+        assertTrue(actual.containsKey("dept"));
+        assertTrue(actual.containsKey("teacher"));
+        assertTrue(actual.containsKey("class"));
+        assertThat(actual.get("employee").iterator().next().getDataSourceName(), is("ds0"));
+        assertThat(actual.get("salary").iterator().next().getDataSourceName(), is("ds0"));
+        assertThat(actual.get("student").iterator().next().getDataSourceName(), is("ds1"));
+        assertThat(actual.get("dept").iterator().next().getDataSourceName(), is("ds0"));
+        assertThat(actual.get("teacher").iterator().next().getDataSourceName(), is("ds1"));
+        assertThat(actual.get("class").iterator().next().getDataSourceName(), is("ds1"));
     }
 }