You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by wu...@apache.org on 2021/02/06 09:30:55 UTC

[shardingsphere] branch master updated: Fix drop index issue for broadcast table (#9360)

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

wuweijie 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 77743c9  Fix drop index issue for broadcast table (#9360)
77743c9 is described below

commit 77743c9ea38cfc7898cd0143bfe5199f87c715ed
Author: Liang Zhang <te...@163.com>
AuthorDate: Sat Feb 6 17:30:21 2021 +0800

    Fix drop index issue for broadcast table (#9360)
    
    * For xml format
    
    * Fix drop index issue for broadcast table
    
    * Fix test case
---
 .../ShardingTableBroadcastRoutingEngine.java       |  24 ++++-
 .../ShardingTableBroadcastRoutingEngineTest.java   | 110 ++++-----------------
 .../cases/ddl/ddl-integration-test-cases.xml       |  37 ++++---
 3 files changed, 58 insertions(+), 113 deletions(-)

diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/type/broadcast/ShardingTableBroadcastRoutingEngine.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/type/broadcast/ShardingTableBroadcastRoutingEngine.java
index 7543464..9297428 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/type/broadcast/ShardingTableBroadcastRoutingEngine.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/type/broadcast/ShardingTableBroadcastRoutingEngine.java
@@ -49,13 +49,20 @@ public final class ShardingTableBroadcastRoutingEngine implements ShardingRouteE
     @Override
     public void route(final RouteContext routeContext, final ShardingRule shardingRule) {
         for (String each : getLogicTableNames()) {
-            routeContext.getRouteUnits().addAll(getAllRouteUnits(shardingRule, each));
+            if (shardingRule.getBroadcastTables().contains(each)) {
+                routeContext.getRouteUnits().addAll(getBroadcastTableRouteUnits(shardingRule, each));
+            } else {
+                routeContext.getRouteUnits().addAll(getAllRouteUnits(shardingRule, each));
+            }
         }
     }
     
     private Collection<String> getLogicTableNames() {
-        return sqlStatementContext.getSqlStatement() instanceof DropIndexStatement && !((DropIndexStatement) sqlStatementContext.getSqlStatement()).getIndexes().isEmpty()
-                ? getTableNamesFromMetaData((DropIndexStatement) sqlStatementContext.getSqlStatement()) : sqlStatementContext.getTablesContext().getTableNames();
+        Collection<String> tableNamesInSQL = sqlStatementContext.getTablesContext().getTableNames();
+        if (!tableNamesInSQL.isEmpty()) {
+            return tableNamesInSQL;
+        }
+        return sqlStatementContext.getSqlStatement() instanceof DropIndexStatement ? getTableNamesFromMetaData((DropIndexStatement) sqlStatementContext.getSqlStatement()) : Collections.emptyList();
     }
     
     private Collection<String> getTableNamesFromMetaData(final DropIndexStatement dropIndexStatement) {
@@ -77,12 +84,19 @@ public final class ShardingTableBroadcastRoutingEngine implements ShardingRouteE
         return Optional.empty();
     }
     
+    private Collection<RouteUnit> getBroadcastTableRouteUnits(final ShardingRule shardingRule, final String broadcastTableName) {
+        Collection<RouteUnit> result = new LinkedList<>();
+        for (String each : shardingRule.getDataSourceNames()) {
+            result.add(new RouteUnit(new RouteMapper(each, each), Collections.singletonList(new RouteMapper(broadcastTableName, broadcastTableName))));
+        }
+        return result;
+    }
+    
     private Collection<RouteUnit> getAllRouteUnits(final ShardingRule shardingRule, final String logicTableName) {
         Collection<RouteUnit> result = new LinkedList<>();
         TableRule tableRule = shardingRule.getTableRule(logicTableName);
         for (DataNode each : tableRule.getActualDataNodes()) {
-            RouteUnit routeUnit = new RouteUnit(new RouteMapper(each.getDataSourceName(), each.getDataSourceName()), Collections.singletonList(new RouteMapper(logicTableName, each.getTableName())));
-            result.add(routeUnit);
+            result.add(new RouteUnit(new RouteMapper(each.getDataSourceName(), each.getDataSourceName()), Collections.singletonList(new RouteMapper(logicTableName, each.getTableName()))));
         }
         return result;
     }
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/test/java/org/apache/shardingsphere/sharding/route/engine/type/broadcast/ShardingTableBroadcastRoutingEngineTest.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/test/java/org/apache/shardingsphere/sharding/route/engine/type/broadcast/ShardingTableBroadcastRoutingEngineTest.java
index c482b15..abe3757 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/test/java/org/apache/shardingsphere/sharding/route/engine/type/broadcast/ShardingTableBroadcastRoutingEngineTest.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/test/java/org/apache/shardingsphere/sharding/route/engine/type/broadcast/ShardingTableBroadcastRoutingEngineTest.java
@@ -18,30 +18,18 @@
 package org.apache.shardingsphere.sharding.route.engine.type.broadcast;
 
 import com.google.common.collect.Lists;
-import org.apache.shardingsphere.infra.binder.segment.table.TablesContext;
 import org.apache.shardingsphere.infra.binder.statement.SQLStatementContext;
 import org.apache.shardingsphere.infra.database.type.DatabaseType;
 import org.apache.shardingsphere.infra.metadata.schema.ShardingSphereSchema;
-import org.apache.shardingsphere.infra.metadata.schema.model.IndexMetaData;
-import org.apache.shardingsphere.infra.metadata.schema.model.TableMetaData;
 import org.apache.shardingsphere.infra.route.context.RouteContext;
 import org.apache.shardingsphere.infra.route.context.RouteMapper;
 import org.apache.shardingsphere.infra.route.context.RouteUnit;
 import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration;
 import org.apache.shardingsphere.sharding.api.config.rule.ShardingTableRuleConfiguration;
 import org.apache.shardingsphere.sharding.rule.ShardingRule;
-import org.apache.shardingsphere.sql.parser.sql.common.segment.ddl.index.IndexSegment;
-import org.apache.shardingsphere.sql.parser.sql.common.statement.ddl.DDLStatement;
-import org.apache.shardingsphere.sql.parser.sql.common.statement.ddl.DropIndexStatement;
-import org.apache.shardingsphere.sql.parser.sql.common.value.identifier.IdentifierValue;
-import org.apache.shardingsphere.sql.parser.sql.dialect.statement.mysql.ddl.MySQLDropIndexStatement;
-import org.apache.shardingsphere.sql.parser.sql.dialect.statement.oracle.ddl.OracleDropIndexStatement;
-import org.apache.shardingsphere.sql.parser.sql.dialect.statement.postgresql.ddl.PostgreSQLDropIndexStatement;
-import org.apache.shardingsphere.sql.parser.sql.dialect.statement.sqlserver.ddl.SQLServerDropIndexStatement;
 import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
-import org.mockito.Mock;
 import org.mockito.junit.MockitoJUnitRunner;
 
 import javax.sql.DataSource;
@@ -58,36 +46,26 @@ import static org.mockito.Mockito.when;
 @RunWith(MockitoJUnitRunner.class)
 public final class ShardingTableBroadcastRoutingEngineTest {
     
-    @Mock
-    private SQLStatementContext sqlStatementContext;
-    
-    @Mock
-    private TablesContext tablesContext;
-    
-    @Mock
-    private ShardingSphereSchema schema;
-    
-    @Mock
-    private TableMetaData tableMetaData;
-    
-    private ShardingTableBroadcastRoutingEngine tableBroadcastRoutingEngine;
+    private ShardingTableBroadcastRoutingEngine shardingTableBroadcastRoutingEngine;
     
     private ShardingRule shardingRule;
     
     @Before
     public void setUp() {
-        ShardingTableRuleConfiguration tableRuleConfig = new ShardingTableRuleConfiguration("t_order", "ds${0..1}.t_order_${0..2}");
-        ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
-        shardingRuleConfig.getTables().add(tableRuleConfig);
-        when(sqlStatementContext.getTablesContext()).thenReturn(tablesContext);
-        when(tablesContext.getTableNames()).thenReturn(Lists.newArrayList("t_order"));
-        when(schema.getAllTableNames()).thenReturn(Lists.newArrayList("t_order"));
-        when(schema.get("t_order")).thenReturn(tableMetaData);
-        Map<String, IndexMetaData> indexMetaDataMap = new HashMap<>(1, 1);
-        indexMetaDataMap.put("index_name", new IndexMetaData("index_name"));
-        when(tableMetaData.getIndexes()).thenReturn(indexMetaDataMap);
-        tableBroadcastRoutingEngine = new ShardingTableBroadcastRoutingEngine(schema, sqlStatementContext);
-        shardingRule = new ShardingRule(shardingRuleConfig, mock(DatabaseType.class), createDataSourceMap());
+        shardingTableBroadcastRoutingEngine = new ShardingTableBroadcastRoutingEngine(mock(ShardingSphereSchema.class), mockSQLStatementContext());
+        shardingRule = new ShardingRule(createShardingRuleConfiguration(), mock(DatabaseType.class), createDataSourceMap());
+    }
+    
+    private SQLStatementContext<?> mockSQLStatementContext() {
+        SQLStatementContext<?> result = mock(SQLStatementContext.class, RETURNS_DEEP_STUBS);
+        when(result.getTablesContext().getTableNames()).thenReturn(Lists.newArrayList("t_order"));
+        return result;
+    }
+    
+    private ShardingRuleConfiguration createShardingRuleConfiguration() {
+        ShardingRuleConfiguration result = new ShardingRuleConfiguration();
+        result.getTables().add(new ShardingTableRuleConfiguration("t_order", "ds${0..1}.t_order_${0..2}"));
+        return result;
     }
     
     private Map<String, DataSource> createDataSourceMap() {
@@ -99,67 +77,21 @@ public final class ShardingTableBroadcastRoutingEngineTest {
     
     @Test
     public void assertRouteForNormalDDL() {
-        DDLStatement ddlStatement = mock(DDLStatement.class);
-        when(sqlStatementContext.getSqlStatement()).thenReturn(ddlStatement);
         RouteContext routeContext = new RouteContext();
-        tableBroadcastRoutingEngine.route(routeContext, shardingRule);
+        shardingTableBroadcastRoutingEngine.route(routeContext, shardingRule);
         assertRouteContext(routeContext);
     }
     
-    @Test(expected = IllegalStateException.class)
-    public void assertRouteForNonExistMySQLDropIndex() {
-        assertRouteForNonExistDropIndex(mock(MySQLDropIndexStatement.class));
-    }
-
-    @Test(expected = IllegalStateException.class)
-    public void assertRouteForNonExistOracleDropIndex() {
-        assertRouteForNonExistDropIndex(mock(OracleDropIndexStatement.class));
-    }
-
-    @Test(expected = IllegalStateException.class)
-    public void assertRouteForNonExistPostgreSQLDropIndex() {
-        assertRouteForNonExistDropIndex(mock(PostgreSQLDropIndexStatement.class));
-    }
-
-    @Test(expected = IllegalStateException.class)
-    public void assertRouteForNonExistSQLServerDropIndex() {
-        assertRouteForNonExistDropIndex(mock(SQLServerDropIndexStatement.class));
-    }
-
-    private void assertRouteForNonExistDropIndex(final DropIndexStatement indexStatement) {
-        IndexSegment indexSegment = new IndexSegment(0, 0, new IdentifierValue("no_index"));
-        when(indexStatement.getIndexes()).thenReturn(Lists.newArrayList(indexSegment));
-        when(sqlStatementContext.getSqlStatement()).thenReturn(indexStatement);
+    @Test
+    public void assertRouteForNonExistDropIndex() {
         RouteContext routeContext = new RouteContext();
-        tableBroadcastRoutingEngine.route(routeContext, shardingRule);
+        shardingTableBroadcastRoutingEngine.route(routeContext, shardingRule);
     }
     
     @Test
-    public void assertRouteForMySQLDropIndex() {
-        assertRouteForDropIndex(mock(MySQLDropIndexStatement.class));
-    }
-
-    @Test
-    public void assertRouteForOracleDropIndex() {
-        assertRouteForDropIndex(mock(OracleDropIndexStatement.class));
-    }
-
-    @Test
-    public void assertRouteForPostgreSQLDropIndex() {
-        assertRouteForDropIndex(mock(PostgreSQLDropIndexStatement.class));
-    }
-
-    @Test
-    public void assertRouteForSQLServerDropIndex() {
-        assertRouteForDropIndex(mock(SQLServerDropIndexStatement.class));
-    }
-
-    private void assertRouteForDropIndex(final DropIndexStatement indexStatement) {
-        IndexSegment indexSegment = new IndexSegment(0, 0, new IdentifierValue("index_name"));
-        when(indexStatement.getIndexes()).thenReturn(Lists.newArrayList(indexSegment));
-        when(sqlStatementContext.getSqlStatement()).thenReturn(indexStatement);
+    public void assertRouteForDropIndex() {
         RouteContext routeContext = new RouteContext();
-        tableBroadcastRoutingEngine.route(routeContext, shardingRule);
+        shardingTableBroadcastRoutingEngine.route(routeContext, shardingRule);
         assertRouteContext(routeContext);
     }
     
diff --git a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/resources/cases/ddl/ddl-integration-test-cases.xml b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/resources/cases/ddl/ddl-integration-test-cases.xml
index 26487c1..3bb0969 100644
--- a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/resources/cases/ddl/ddl-integration-test-cases.xml
+++ b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/resources/cases/ddl/ddl-integration-test-cases.xml
@@ -63,50 +63,49 @@
             <initial-sql sql="CREATE TABLE t_order_details(id int, description varchar(10))" affected-table="t_order_details" />
         </assertion>
     </test-case>
-
+    
     <test-case sql="CREATE TABLE t_broadcast_table_for_ddl(id int PRIMARY KEY, description varchar(10))">
         <assertion expected-data-file="broadcast_create_table.xml">
             <initial-sql affected-table="t_broadcast_table_for_ddl" />
         </assertion>
     </test-case>
-
+    
     <test-case sql="CREATE INDEX t_broadcast_table_for_ddl_index ON t_broadcast_table_for_ddl (id)">
         <assertion expected-data-file="broadcast_create_index.xml">
             <initial-sql sql="CREATE TABLE t_broadcast_table_for_ddl(id int, description varchar(10))" affected-table="t_broadcast_table_for_ddl" />
         </assertion>
     </test-case>
-
+    
     <test-case sql="CREATE UNIQUE INDEX t_broadcast_table_for_ddl_index ON t_broadcast_table_for_ddl (id)">
         <assertion expected-data-file="broadcast_create_unique_index.xml">
             <initial-sql sql="CREATE TABLE t_broadcast_table_for_ddl(id int, description varchar(10))" affected-table="t_broadcast_table_for_ddl" />
         </assertion>
     </test-case>
-
+    
     <test-case sql="ALTER TABLE t_broadcast_table_for_ddl ADD name varchar(10)">
         <assertion expected-data-file="broadcast_alter_table.xml">
             <initial-sql sql="CREATE TABLE t_broadcast_table_for_ddl(id int, description varchar(10))" affected-table="t_broadcast_table_for_ddl" />
         </assertion>
     </test-case>
-
+    
     <test-case sql="DROP TABLE t_broadcast_table_for_ddl">
         <assertion expected-data-file="broadcast_drop_table.xml">
             <initial-sql sql="CREATE TABLE t_broadcast_table_for_ddl(id int, description varchar(10))" affected-table="t_broadcast_table_for_ddl" />
         </assertion>
     </test-case>
-
-    <!--TODO FIX ME! ISSUE #9314 datasource problem -->
-<!--    <test-case sql="DROP INDEX t_broadcast_table_for_ddl_index ON t_broadcast_table_for_ddl" db-types="MySQL,SQLServer">-->
-<!--        <assertion expected-data-file="broadcast_unchanged_table.xml">-->
-<!--            <initial-sql sql="CREATE TABLE t_broadcast_table_for_ddl(id int, description varchar(10));CREATE INDEX t_broadcast_table_for_ddl_index ON t_broadcast_table_for_ddl(description)" affected-table="t_broadcast_table_for_ddl" />-->
-<!--        </assertion>-->
-<!--    </test-case>-->
-
-<!--    <test-case sql="DROP INDEX t_broadcast_table_for_ddl_index" db-types="PostgreSQL,Oracle">-->
-<!--        <assertion expected-data-file="broadcast_unchanged_table.xml">-->
-<!--            <initial-sql sql="CREATE TABLE t_broadcast_table_for_ddl(id int, description varchar(10));CREATE INDEX t_broadcast_table_for_ddl_index ON t_broadcast_table_for_ddl(description)" affected-table="t_broadcast_table_for_ddl_test" />-->
-<!--        </assertion>-->
-<!--    </test-case>-->
-
+    
+    <test-case sql="DROP INDEX t_broadcast_table_for_ddl_index ON t_broadcast_table_for_ddl" db-types="MySQL,SQLServer">
+        <assertion expected-data-file="broadcast_unchanged_table.xml">
+            <initial-sql sql="CREATE TABLE t_broadcast_table_for_ddl(id int, description varchar(10));CREATE INDEX t_broadcast_table_for_ddl_index ON t_broadcast_table_for_ddl(description)" affected-table="t_broadcast_table_for_ddl" />
+        </assertion>
+    </test-case>
+    
+    <test-case sql="DROP INDEX t_broadcast_table_for_ddl_index" db-types="PostgreSQL,Oracle">
+        <assertion expected-data-file="broadcast_unchanged_table.xml">
+            <initial-sql sql="CREATE TABLE t_broadcast_table_for_ddl(id int, description varchar(10));CREATE INDEX t_broadcast_table_for_ddl_index ON t_broadcast_table_for_ddl(description)" affected-table="t_broadcast_table_for_ddl_test" />
+        </assertion>
+    </test-case>
+    
     <test-case sql="TRUNCATE TABLE t_broadcast_table_for_ddl">
         <assertion expected-data-file="broadcast_unchanged_table.xml">
             <initial-sql sql="CREATE TABLE t_broadcast_table_for_ddl(id int, description varchar(10))" affected-table="t_broadcast_table_for_ddl" />