You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by GitBox <gi...@apache.org> on 2020/09/09 15:32:35 UTC

[GitHub] [shardingsphere] terrymanu commented on a change in pull request #7355: Support multi replica extension

terrymanu commented on a change in pull request #7355:
URL: https://github.com/apache/shardingsphere/pull/7355#discussion_r485700059



##########
File path: shardingsphere-features/shardingsphere-replica/shardingsphere-replica-execute/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.executor.sql.raw.execute.callback.RawExecutorCallback
##########
@@ -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.replica.execute.callback.DefaultReplicaExecutorCallback

Review comment:
       Please add a blank line in the end of file

##########
File path: shardingsphere-infra/shardingsphere-infra-executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/context/ExecutionContextBuilder.java
##########
@@ -42,22 +50,77 @@
      * 
      * @param metaData meta data
      * @param sqlRewriteResult SQL rewrite result
+     * @param sqlStatementContext SQL statement context
      * @return execution contexts
      */
-    public static Collection<ExecutionUnit> build(final ShardingSphereMetaData metaData, final SQLRewriteResult sqlRewriteResult) {
-        return sqlRewriteResult instanceof GenericSQLRewriteResult ? build(metaData, (GenericSQLRewriteResult) sqlRewriteResult) : build((RouteSQLRewriteResult) sqlRewriteResult);
+    public static Collection<ExecutionUnit> build(final ShardingSphereMetaData metaData, final SQLRewriteResult sqlRewriteResult, final SQLStatementContext sqlStatementContext) {
+        return sqlRewriteResult instanceof GenericSQLRewriteResult ? build(metaData, (GenericSQLRewriteResult) sqlRewriteResult, sqlStatementContext)
+                : build(metaData, (RouteSQLRewriteResult) sqlRewriteResult);
     }
     
-    private static Collection<ExecutionUnit> build(final ShardingSphereMetaData metaData, final GenericSQLRewriteResult sqlRewriteResult) {
+    private static Collection<ExecutionUnit> build(final ShardingSphereMetaData metaData, final GenericSQLRewriteResult sqlRewriteResult, final SQLStatementContext sqlStatementContext) {
         String dataSourceName = metaData.getDataSourceMetaDatas().getAllInstanceDataSourceNames().iterator().next();
-        return Collections.singletonList(new ExecutionUnit(dataSourceName, new SQLUnit(sqlRewriteResult.getSqlRewriteUnit().getSql(), sqlRewriteResult.getSqlRewriteUnit().getParameters())));
+        return Collections.singletonList(new ExecutionUnit(dataSourceName,
+                new SQLUnit(sqlRewriteResult.getSqlRewriteUnit().getSql(), sqlRewriteResult.getSqlRewriteUnit().getParameters(), getActualTableNames(sqlStatementContext),
+                        getPrimaryKeyColumns(metaData, sqlStatementContext))));
     }
     
-    private static Collection<ExecutionUnit> build(final RouteSQLRewriteResult sqlRewriteResult) {
+    private static Collection<ExecutionUnit> build(final ShardingSphereMetaData metaData, final RouteSQLRewriteResult sqlRewriteResult) {
         Collection<ExecutionUnit> result = new LinkedHashSet<>();
         for (Entry<RouteUnit, SQLRewriteUnit> entry : sqlRewriteResult.getSqlRewriteUnits().entrySet()) {
-            result.add(new ExecutionUnit(entry.getKey().getDataSourceMapper().getActualName(), new SQLUnit(entry.getValue().getSql(), entry.getValue().getParameters())));
+            Collection<RouteMapper> tableMappers = entry.getKey().getTableMappers();
+            result.add(new ExecutionUnit(entry.getKey().getDataSourceMapper().getActualName(),
+                    new SQLUnit(entry.getValue().getSql(), entry.getValue().getParameters(), getActualTableNames(tableMappers), getPrimaryKeyColumns(metaData, tableMappers))));
         }
         return result;
     }
+    
+    private static Set<String> getActualTableNames(final SQLStatementContext sqlStatementContext) {
+        return getGenericTableNames(sqlStatementContext);
+    }
+
+    private static Set<String> getActualTableNames(final Collection<RouteMapper> tableMappers) {
+        if (null == tableMappers) {
+            return Collections.emptySet();
+        }
+        return tableMappers.stream().map(RouteMapper::getActualName).collect(Collectors.toSet());
+    }
+    
+    private static Map<String, List<String>> getPrimaryKeyColumns(final ShardingSphereMetaData metaData, final SQLStatementContext sqlStatementContext) {
+        return getPrimaryKeyColumns(metaData, getLogicTableNames(sqlStatementContext));
+    }
+    
+    private static Map<String, List<String>> getPrimaryKeyColumns(final ShardingSphereMetaData metaData, final Collection<RouteMapper> tableMappers) {
+        return getPrimaryKeyColumns(metaData, getLogicTableNames(tableMappers));
+    }
+    
+    private static Map<String, List<String>> getPrimaryKeyColumns(final ShardingSphereMetaData metaData, final Set<String> logicTableNames) {
+        Map<String, List<String>> primaryKeyColumns = new HashMap<>(logicTableNames.size());
+        for (String each: logicTableNames) {
+            primaryKeyColumns.put(each, metaData.getRuleSchemaMetaData().getSchemaMetaData().get(each).getPrimaryKeyColumns());
+        }
+        return primaryKeyColumns;

Review comment:
       Please rename `primaryKeyColumns` as `result` for return value 

##########
File path: shardingsphere-infra/shardingsphere-infra-executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/context/SQLUnit.java
##########
@@ -36,4 +40,15 @@
     private final String sql;
     
     private final List<Object> parameters;
+
+    private final Set<String> actualTables;
+    
+    private final Map<String, List<String>> primaryKeyColumns;

Review comment:
       What is the useful of the structure of `Map<String, List<String>>`?
   Can we use a object to define this structure?




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org