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 2020/12/09 08:02:56 UTC
[shardingsphere] branch master updated: Move logSQL to
KernelProcessor (#8545)
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 0bf45cf Move logSQL to KernelProcessor (#8545)
0bf45cf is described below
commit 0bf45cfda37c9e0e73ef9fcdf1ae15941db3af2a
Author: Liang Zhang <te...@163.com>
AuthorDate: Wed Dec 9 16:02:33 2020 +0800
Move logSQL to KernelProcessor (#8545)
* Refactor ShardingSphereStatement and ShardingSpherePreparedStatement
* Move logSQL to KernelProcessor
---
.../infra/context/kernel/KernelProcessor.java | 16 ++++++++++---
.../statement/ShardingSpherePreparedStatement.java | 28 ++++++----------------
.../core/statement/ShardingSphereStatement.java | 20 ++++++----------
.../communication/DatabaseCommunicationEngine.java | 13 ++--------
4 files changed, 29 insertions(+), 48 deletions(-)
diff --git a/shardingsphere-infra/shardingsphere-infra-context/src/main/java/org/apache/shardingsphere/infra/context/kernel/KernelProcessor.java b/shardingsphere-infra/shardingsphere-infra-context/src/main/java/org/apache/shardingsphere/infra/context/kernel/KernelProcessor.java
index cd4a828..059d3ea 100644
--- a/shardingsphere-infra/shardingsphere-infra-context/src/main/java/org/apache/shardingsphere/infra/context/kernel/KernelProcessor.java
+++ b/shardingsphere-infra/shardingsphere-infra-context/src/main/java/org/apache/shardingsphere/infra/context/kernel/KernelProcessor.java
@@ -17,18 +17,20 @@
package org.apache.shardingsphere.infra.context.kernel;
+import org.apache.shardingsphere.infra.binder.LogicSQL;
import org.apache.shardingsphere.infra.binder.statement.SQLStatementContext;
import org.apache.shardingsphere.infra.config.properties.ConfigurationProperties;
+import org.apache.shardingsphere.infra.config.properties.ConfigurationPropertyKey;
import org.apache.shardingsphere.infra.executor.sql.context.ExecutionContext;
import org.apache.shardingsphere.infra.executor.sql.context.ExecutionContextBuilder;
import org.apache.shardingsphere.infra.executor.sql.context.ExecutionUnit;
+import org.apache.shardingsphere.infra.executor.sql.log.SQLLogger;
+import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
import org.apache.shardingsphere.infra.rewrite.SQLRewriteEntry;
import org.apache.shardingsphere.infra.rewrite.engine.result.SQLRewriteResult;
import org.apache.shardingsphere.infra.route.context.RouteContext;
import org.apache.shardingsphere.infra.route.engine.SQLRouteEngine;
import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
-import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
-import org.apache.shardingsphere.infra.binder.LogicSQL;
import java.util.Collection;
@@ -53,6 +55,14 @@ public final class KernelProcessor {
SQLRewriteEntry rewriteEntry = new SQLRewriteEntry(metaData.getSchema(), props, rules);
SQLRewriteResult rewriteResult = rewriteEntry.rewrite(logicSQL.getSql(), logicSQL.getParameters(), sqlStatementContext, routeContext);
Collection<ExecutionUnit> executionUnits = ExecutionContextBuilder.build(metaData, rewriteResult, sqlStatementContext);
- return new ExecutionContext(sqlStatementContext, executionUnits, routeContext);
+ ExecutionContext result = new ExecutionContext(sqlStatementContext, executionUnits, routeContext);
+ logSQL(logicSQL, props, result);
+ return result;
+ }
+
+ private void logSQL(final LogicSQL logicSQL, final ConfigurationProperties props, final ExecutionContext executionContext) {
+ if (props.<Boolean>getValue(ConfigurationPropertyKey.SQL_SHOW)) {
+ SQLLogger.logSQL(logicSQL, props.<Boolean>getValue(ConfigurationPropertyKey.SQL_SIMPLE), executionContext);
+ }
}
}
diff --git a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShardingSpherePreparedStatement.java b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShardingSpherePreparedStatement.java
index c8df8ed..a8b922d 100644
--- a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShardingSpherePreparedStatement.java
+++ b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShardingSpherePreparedStatement.java
@@ -54,7 +54,6 @@ import org.apache.shardingsphere.infra.executor.sql.execute.result.ExecuteResult
import org.apache.shardingsphere.infra.executor.sql.execute.result.query.QueryResult;
import org.apache.shardingsphere.infra.executor.sql.execute.result.query.impl.driver.jdbc.type.stream.JDBCStreamQueryResult;
import org.apache.shardingsphere.infra.executor.sql.execute.result.update.UpdateResult;
-import org.apache.shardingsphere.infra.executor.sql.log.SQLLogger;
import org.apache.shardingsphere.infra.executor.sql.prepare.driver.DriverExecutionPrepareEngine;
import org.apache.shardingsphere.infra.executor.sql.prepare.driver.jdbc.JDBCDriverType;
import org.apache.shardingsphere.infra.executor.sql.prepare.driver.jdbc.StatementOption;
@@ -179,7 +178,6 @@ public final class ShardingSpherePreparedStatement extends AbstractPreparedState
}
Collection<ExecutionGroup<JDBCExecutionUnit>> executionGroups = createExecutionGroups();
cacheStatements(executionGroups);
- reply();
return driverJDBCExecutor.executeQuery(executionGroups, new PreparedStatementExecuteQueryCallback(metaDataContexts.getDatabaseType(), SQLExecutorExceptionHandler.isExceptionThrown()));
}
@@ -194,7 +192,6 @@ public final class ShardingSpherePreparedStatement extends AbstractPreparedState
}
Collection<ExecutionGroup<JDBCExecutionUnit>> executionGroups = createExecutionGroups();
cacheStatements(executionGroups);
- reply();
return driverJDBCExecutor.executeUpdate(
executionGroups, executionContext.getSqlStatementContext(), executionContext.getRouteContext().getRouteUnits(), createExecuteUpdateCallback());
} finally {
@@ -233,7 +230,6 @@ public final class ShardingSpherePreparedStatement extends AbstractPreparedState
}
Collection<ExecutionGroup<JDBCExecutionUnit>> executionGroups = createExecutionGroups();
cacheStatements(executionGroups);
- reply();
return driverJDBCExecutor.execute(
executionGroups, executionContext.getSqlStatementContext().getSqlStatement(), executionContext.getRouteContext().getRouteUnits(), createExecuteCallback());
} finally {
@@ -308,7 +304,6 @@ public final class ShardingSpherePreparedStatement extends AbstractPreparedState
LogicSQL logicSQL = createLogicSQL();
ExecutionContext result = kernelProcessor.generateExecutionContext(logicSQL, metaDataContexts.getDefaultMetaData(), metaDataContexts.getProps());
findGeneratedKey(result).ifPresent(generatedKey -> generatedValues.addAll(generatedKey.getGeneratedValues()));
- logSQL(logicSQL, result);
return result;
}
@@ -325,28 +320,25 @@ public final class ShardingSpherePreparedStatement extends AbstractPreparedState
return mergeEngine.merge(queryResults, executionContext.getSqlStatementContext());
}
- private void reply() {
- setParametersForStatements();
- replayMethodForStatements();
- }
-
private void cacheStatements(final Collection<ExecutionGroup<JDBCExecutionUnit>> executionGroups) {
for (ExecutionGroup<JDBCExecutionUnit> each : executionGroups) {
statements.addAll(each.getInputs().stream().map(jdbcExecutionUnit -> (PreparedStatement) jdbcExecutionUnit.getStorageResource()).collect(Collectors.toList()));
parameterSets.addAll(each.getInputs().stream().map(input -> input.getExecutionUnit().getSqlUnit().getParameters()).collect(Collectors.toList()));
}
+ replay();
+ }
+
+ private void replay() {
+ replaySetParameter();
+ statements.forEach(this::replayMethodsInvocation);
}
- private void setParametersForStatements() {
+ private void replaySetParameter() {
for (int i = 0; i < statements.size(); i++) {
replaySetParameter(statements.get(i), parameterSets.get(i));
}
}
- private void replayMethodForStatements() {
- statements.forEach(this::replayMethodsInvocation);
- }
-
private void clearPrevious() throws SQLException {
clearStatements();
parameterSets.clear();
@@ -357,12 +349,6 @@ public final class ShardingSpherePreparedStatement extends AbstractPreparedState
? ((InsertStatementContext) executionContext.getSqlStatementContext()).getGeneratedKeyContext() : Optional.empty();
}
- private void logSQL(final LogicSQL logicSQL, final ExecutionContext executionContext) {
- if (metaDataContexts.getProps().<Boolean>getValue(ConfigurationPropertyKey.SQL_SHOW)) {
- SQLLogger.logSQL(logicSQL, metaDataContexts.getProps().<Boolean>getValue(ConfigurationPropertyKey.SQL_SIMPLE), executionContext);
- }
- }
-
@Override
public ResultSet getGeneratedKeys() throws SQLException {
Optional<GeneratedKeyContext> generatedKey = findGeneratedKey(executionContext);
diff --git a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShardingSphereStatement.java b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShardingSphereStatement.java
index 7bae993..f23b224 100644
--- a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShardingSphereStatement.java
+++ b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShardingSphereStatement.java
@@ -34,7 +34,6 @@ import org.apache.shardingsphere.infra.binder.segment.insert.keygen.GeneratedKey
import org.apache.shardingsphere.infra.binder.statement.SQLStatementContext;
import org.apache.shardingsphere.infra.binder.statement.dml.InsertStatementContext;
import org.apache.shardingsphere.infra.binder.statement.dml.SelectStatementContext;
-import org.apache.shardingsphere.infra.config.properties.ConfigurationProperties;
import org.apache.shardingsphere.infra.config.properties.ConfigurationPropertyKey;
import org.apache.shardingsphere.infra.context.kernel.KernelProcessor;
import org.apache.shardingsphere.infra.context.metadata.MetaDataContexts;
@@ -54,7 +53,6 @@ import org.apache.shardingsphere.infra.executor.sql.execute.result.ExecuteResult
import org.apache.shardingsphere.infra.executor.sql.execute.result.query.QueryResult;
import org.apache.shardingsphere.infra.executor.sql.execute.result.query.impl.driver.jdbc.type.stream.JDBCStreamQueryResult;
import org.apache.shardingsphere.infra.executor.sql.execute.result.update.UpdateResult;
-import org.apache.shardingsphere.infra.executor.sql.log.SQLLogger;
import org.apache.shardingsphere.infra.executor.sql.prepare.driver.DriverExecutionPrepareEngine;
import org.apache.shardingsphere.infra.executor.sql.prepare.driver.jdbc.JDBCDriverType;
import org.apache.shardingsphere.infra.executor.sql.prepare.driver.jdbc.StatementOption;
@@ -338,9 +336,7 @@ public final class ShardingSphereStatement extends AbstractStatementAdapter {
private ExecutionContext createExecutionContext(final String sql) throws SQLException {
clearStatements();
LogicSQL logicSQL = createLogicSQL(sql);
- ExecutionContext result = kernelProcessor.generateExecutionContext(logicSQL, metaDataContexts.getDefaultMetaData(), metaDataContexts.getProps());
- logSQL(logicSQL, metaDataContexts.getProps(), result);
- return result;
+ return kernelProcessor.generateExecutionContext(logicSQL, metaDataContexts.getDefaultMetaData(), metaDataContexts.getProps());
}
private void clearStatements() throws SQLException {
@@ -350,16 +346,10 @@ public final class ShardingSphereStatement extends AbstractStatementAdapter {
statements.clear();
}
- private void logSQL(final LogicSQL logicSQL, final ConfigurationProperties props, final ExecutionContext executionContext) {
- if (props.<Boolean>getValue(ConfigurationPropertyKey.SQL_SHOW)) {
- SQLLogger.logSQL(logicSQL, props.<Boolean>getValue(ConfigurationPropertyKey.SQL_SIMPLE), executionContext);
- }
- }
-
private LogicSQL createLogicSQL(final String sql) {
ShardingSphereSchema schema = metaDataContexts.getDefaultMetaData().getSchema();
- ShardingSphereSQLParserEngine sqlStatementParserEngine = new ShardingSphereSQLParserEngine(DatabaseTypeRegistry.getTrunkDatabaseTypeName(metaDataContexts.getDatabaseType()));
- SQLStatement sqlStatement = sqlStatementParserEngine.parse(sql, false);
+ ShardingSphereSQLParserEngine sqlParserEngine = new ShardingSphereSQLParserEngine(DatabaseTypeRegistry.getTrunkDatabaseTypeName(metaDataContexts.getDatabaseType()));
+ SQLStatement sqlStatement = sqlParserEngine.parse(sql, false);
SQLStatementContext<?> sqlStatementContext = SQLStatementContextFactory.newInstance(schema, Collections.emptyList(), sqlStatement);
return new LogicSQL(sqlStatementContext, sql, Collections.emptyList());
}
@@ -381,6 +371,10 @@ public final class ShardingSphereStatement extends AbstractStatementAdapter {
for (ExecutionGroup<JDBCExecutionUnit> each : executionGroups) {
statements.addAll(each.getInputs().stream().map(JDBCExecutionUnit::getStorageResource).collect(Collectors.toList()));
}
+ replay();
+ }
+
+ private void replay() {
statements.forEach(this::replayMethodsInvocation);
}
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/DatabaseCommunicationEngine.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/DatabaseCommunicationEngine.java
index bca6496..8116b46 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/DatabaseCommunicationEngine.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/DatabaseCommunicationEngine.java
@@ -29,7 +29,6 @@ import org.apache.shardingsphere.infra.executor.sql.context.ExecutionContext;
import org.apache.shardingsphere.infra.executor.sql.execute.result.ExecuteResult;
import org.apache.shardingsphere.infra.executor.sql.execute.result.query.QueryResult;
import org.apache.shardingsphere.infra.executor.sql.execute.result.update.UpdateResult;
-import org.apache.shardingsphere.infra.executor.sql.log.SQLLogger;
import org.apache.shardingsphere.infra.executor.sql.prepare.driver.jdbc.JDBCDriverType;
import org.apache.shardingsphere.infra.lock.LockContext;
import org.apache.shardingsphere.infra.merge.MergeEngine;
@@ -96,18 +95,10 @@ public final class DatabaseCommunicationEngine {
* @throws SQLException SQL exception
*/
public ResponseHeader execute() throws SQLException {
- ExecutionContext executionContext = kernelProcessor.generateExecutionContext(logicSQL, metaData, ProxyContext.getInstance().getMetaDataContexts().getProps());
- logSQL(executionContext);
- return doExecute(executionContext);
+ return execute(kernelProcessor.generateExecutionContext(logicSQL, metaData, ProxyContext.getInstance().getMetaDataContexts().getProps()));
}
- private void logSQL(final ExecutionContext executionContext) {
- if (ProxyContext.getInstance().getMetaDataContexts().getProps().<Boolean>getValue(ConfigurationPropertyKey.SQL_SHOW)) {
- SQLLogger.logSQL(logicSQL, ProxyContext.getInstance().getMetaDataContexts().getProps().<Boolean>getValue(ConfigurationPropertyKey.SQL_SIMPLE), executionContext);
- }
- }
-
- private ResponseHeader doExecute(final ExecutionContext executionContext) throws SQLException {
+ private ResponseHeader execute(final ExecutionContext executionContext) throws SQLException {
if (executionContext.getExecutionUnits().isEmpty()) {
return new UpdateResponseHeader(executionContext.getSqlStatementContext().getSqlStatement());
}