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 2021/09/18 04:15:38 UTC
[shardingsphere] branch master updated: cache readwrite splitting
statement (#12524)
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 23dc470 cache readwrite splitting statement (#12524)
23dc470 is described below
commit 23dc470ae011e0d507aab6414c3d6fbe2a85121a
Author: Zhengqiang Duan <du...@apache.org>
AuthorDate: Sat Sep 18 12:15:07 2021 +0800
cache readwrite splitting statement (#12524)
* cache readwrite-splitting statement
* fix unit test
---
.../shardingsphere/infra/hint/HintManager.java | 9 +++++
.../shardingsphere/infra/hint/HintManagerTest.java | 9 +++++
.../statement/ShardingSpherePreparedStatement.java | 38 ++++++++++++++++++----
3 files changed, 50 insertions(+), 6 deletions(-)
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/hint/HintManager.java b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/hint/HintManager.java
index d354396..6d1e7a3 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/hint/HintManager.java
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/hint/HintManager.java
@@ -177,6 +177,15 @@ public final class HintManager implements AutoCloseable {
databaseShardingOnly = false;
}
+ /**
+ * Judge whether hint manager instantiated or not.
+ *
+ * @return whether hint manager instantiated or not
+ */
+ public static boolean isInstantiated() {
+ return null != HINT_MANAGER_HOLDER.get();
+ }
+
@Override
public void close() {
clear();
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/hint/HintManagerTest.java b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/hint/HintManagerTest.java
index d7afba36..0aa905f 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/hint/HintManagerTest.java
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/hint/HintManagerTest.java
@@ -197,4 +197,13 @@ public final class HintManagerTest {
assertTrue(HintManager.getDatabaseShardingValues("logic_table").isEmpty());
assertTrue(HintManager.getTableShardingValues("logic_table").isEmpty());
}
+
+ @Test
+ public void assertIsInstantiated() {
+ assertFalse(HintManager.isInstantiated());
+ HintManager hintManager = HintManager.getInstance();
+ assertTrue(HintManager.isInstantiated());
+ hintManager.close();
+ assertFalse(HintManager.isInstantiated());
+ }
}
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 db40832..a938646 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
@@ -36,9 +36,9 @@ 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.RuleConfiguration;
import org.apache.shardingsphere.infra.config.properties.ConfigurationPropertyKey;
import org.apache.shardingsphere.infra.context.kernel.KernelProcessor;
-import org.apache.shardingsphere.mode.metadata.MetaDataContexts;
import org.apache.shardingsphere.infra.database.type.DatabaseTypeRegistry;
import org.apache.shardingsphere.infra.exception.ShardingSphereException;
import org.apache.shardingsphere.infra.executor.check.SQLCheckEngine;
@@ -63,12 +63,15 @@ import org.apache.shardingsphere.infra.executor.sql.prepare.driver.DriverExecuti
import org.apache.shardingsphere.infra.executor.sql.prepare.driver.jdbc.JDBCDriverType;
import org.apache.shardingsphere.infra.executor.sql.prepare.driver.jdbc.StatementOption;
import org.apache.shardingsphere.infra.executor.sql.prepare.raw.RawExecutionPrepareEngine;
+import org.apache.shardingsphere.infra.hint.HintManager;
import org.apache.shardingsphere.infra.merge.MergeEngine;
import org.apache.shardingsphere.infra.merge.result.MergedResult;
import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
import org.apache.shardingsphere.infra.parser.ShardingSphereSQLParserEngine;
import org.apache.shardingsphere.infra.rule.identifier.type.DataNodeContainedRule;
import org.apache.shardingsphere.infra.rule.identifier.type.RawExecutionRule;
+import org.apache.shardingsphere.mode.metadata.MetaDataContexts;
+import org.apache.shardingsphere.readwritesplitting.api.ReadwriteSplittingRuleConfiguration;
import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
import org.apache.shardingsphere.sql.parser.sql.common.statement.dal.DALStatement;
@@ -122,6 +125,8 @@ public final class ShardingSpherePreparedStatement extends AbstractPreparedState
private final KernelProcessor kernelProcessor;
+ private final boolean statementsCacheable;
+
private ExecutionContext executionContext;
private ResultSet currentResultSet;
@@ -165,22 +170,35 @@ public final class ShardingSpherePreparedStatement extends AbstractPreparedState
federateExecutor = new FederateJDBCExecutor(connection.getSchemaName(), metaDataContexts.getOptimizeContextFactory(), metaDataContexts.getProps(), jdbcExecutor);
batchPreparedStatementExecutor = new BatchPreparedStatementExecutor(metaDataContexts, jdbcExecutor, connection.getSchemaName());
kernelProcessor = new KernelProcessor();
+ statementsCacheable = isStatementsCacheable(metaDataContexts.getMetaData(connection.getSchemaName()).getRuleMetaData().getConfigurations());
+ }
+
+ private boolean isStatementsCacheable(final Collection<RuleConfiguration> configurations) {
+ // TODO Consider cache statements with more case
+ return 1 == configurations.size() && configurations.iterator().next() instanceof ReadwriteSplittingRuleConfiguration && !HintManager.isInstantiated();
}
@Override
public ResultSet executeQuery() throws SQLException {
- ResultSet result;
try {
+ if (statementsCacheable && !statements.isEmpty()) {
+ resetParameters();
+ return statements.iterator().next().executeQuery();
+ }
clearPrevious();
executionContext = createExecutionContext();
List<QueryResult> queryResults = executeQuery0();
MergedResult mergedResult = mergeQuery(queryResults);
- result = new ShardingSphereResultSet(getResultSetsForShardingSphereResultSet(), mergedResult, this, executionContext);
+ return new ShardingSphereResultSet(getResultSetsForShardingSphereResultSet(), mergedResult, this, executionContext);
} finally {
clearBatch();
}
- currentResultSet = result;
- return result;
+ }
+
+ private void resetParameters() throws SQLException {
+ parameterSets.clear();
+ parameterSets.add(getParameters());
+ replaySetParameter();
}
private List<ResultSet> getResultSetsForShardingSphereResultSet() throws SQLException {
@@ -223,11 +241,15 @@ public final class ShardingSpherePreparedStatement extends AbstractPreparedState
@Override
public int executeUpdate() throws SQLException {
try {
+ if (statementsCacheable && !statements.isEmpty()) {
+ resetParameters();
+ return statements.iterator().next().executeUpdate();
+ }
clearPrevious();
executionContext = createExecutionContext();
if (metaDataContexts.getMetaData(connection.getSchemaName()).getRuleMetaData().getRules().stream().anyMatch(each -> each instanceof RawExecutionRule)) {
Collection<ExecuteResult> executeResults = rawExecutor.execute(createRawExecutionGroupContext(), executionContext.getLogicSQL(), new RawSQLExecutorCallback());
- accumulate(executeResults);
+ return accumulate(executeResults);
}
ExecutionGroupContext<JDBCExecutionUnit> executionGroupContext = createExecutionGroupContext();
cacheStatements(executionGroupContext.getInputGroups());
@@ -265,6 +287,10 @@ public final class ShardingSpherePreparedStatement extends AbstractPreparedState
@Override
public boolean execute() throws SQLException {
try {
+ if (statementsCacheable && !statements.isEmpty()) {
+ resetParameters();
+ return statements.iterator().next().execute();
+ }
clearPrevious();
executionContext = createExecutionContext();
if (metaDataContexts.getMetaData(connection.getSchemaName()).getRuleMetaData().getRules().stream().anyMatch(each -> each instanceof RawExecutionRule)) {