This is an automated email from the ASF dual-hosted git repository.
zhangliang 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 dd3ee66539a Optimize execution prepare engine for high frequency
invocation (#35352)
dd3ee66539a is described below
commit dd3ee66539a422ea05aeb6aae6237d2c56b2297e
Author: Liang Zhang <[email protected]>
AuthorDate: Fri May 9 17:46:18 2025 +0800
Optimize execution prepare engine for high frequency invocation (#35352)
- Add @HighFrequencyInvocation annotation to relevant classes
- Improve data structure usage for better performance
- Refactor method names for consistency and clarity
---
.../executor/sql/prepare/AbstractExecutionPrepareEngine.java | 10 +++++++---
.../sql/prepare/driver/DriverExecutionPrepareEngine.java | 6 ++++--
.../jdbc/builder/PreparedStatementExecutionUnitBuilder.java | 3 +--
.../executor/sql/prepare/raw/RawExecutionPrepareEngine.java | 2 ++
4 files changed, 14 insertions(+), 7 deletions(-)
diff --git
a/infra/executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/prepare/AbstractExecutionPrepareEngine.java
b/infra/executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/prepare/AbstractExecutionPrepareEngine.java
index ce97cb8e323..81df7d15a8b 100644
---
a/infra/executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/prepare/AbstractExecutionPrepareEngine.java
+++
b/infra/executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/prepare/AbstractExecutionPrepareEngine.java
@@ -18,6 +18,7 @@
package org.apache.shardingsphere.infra.executor.sql.prepare;
import com.google.common.collect.Lists;
+import org.apache.shardingsphere.infra.annotation.HighFrequencyInvocation;
import org.apache.shardingsphere.infra.executor.kernel.model.ExecutionGroup;
import
org.apache.shardingsphere.infra.executor.kernel.model.ExecutionGroupContext;
import
org.apache.shardingsphere.infra.executor.kernel.model.ExecutionGroupReportContext;
@@ -28,6 +29,7 @@ import
org.apache.shardingsphere.infra.rule.ShardingSphereRule;
import org.apache.shardingsphere.infra.spi.type.ordered.OrderedSPILoader;
import java.sql.SQLException;
+import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;
@@ -41,6 +43,7 @@ import java.util.TreeMap;
*
* @param <T> type of input value
*/
+@HighFrequencyInvocation
public abstract class AbstractExecutionPrepareEngine<T> implements
ExecutionPrepareEngine<T> {
private final int maxConnectionsSizePerQuery;
@@ -65,8 +68,9 @@ public abstract class AbstractExecutionPrepareEngine<T>
implements ExecutionPrep
Collection<ExecutionGroup<T>> result = new LinkedList<>();
for (Entry<String, List<ExecutionUnit>> entry :
aggregateExecutionUnitGroups(executionUnits).entrySet()) {
String dataSourceName = entry.getKey();
- List<List<ExecutionUnit>> executionUnitGroups =
group(entry.getValue());
- ConnectionMode connectionMode = maxConnectionsSizePerQuery <
entry.getValue().size() ? ConnectionMode.CONNECTION_STRICTLY :
ConnectionMode.MEMORY_STRICTLY;
+ List<ExecutionUnit> groupedExecutionUnits = entry.getValue();
+ List<List<ExecutionUnit>> executionUnitGroups =
group(groupedExecutionUnits);
+ ConnectionMode connectionMode = maxConnectionsSizePerQuery <
groupedExecutionUnits.size() ? ConnectionMode.CONNECTION_STRICTLY :
ConnectionMode.MEMORY_STRICTLY;
result.addAll(group(databaseName, dataSourceName,
connectionOffsets.getOrDefault(dataSourceName, 0), executionUnitGroups,
connectionMode));
}
return decorate(routeContext, result, reportContext);
@@ -83,7 +87,7 @@ public abstract class AbstractExecutionPrepareEngine<T>
implements ExecutionPrep
private Map<String, List<ExecutionUnit>>
aggregateExecutionUnitGroups(final Collection<ExecutionUnit> executionUnits) {
Map<String, List<ExecutionUnit>> result = new TreeMap<>();
for (ExecutionUnit each : executionUnits) {
- result.computeIfAbsent(each.getDataSourceName(), unused -> new
LinkedList<>()).add(each);
+ result.computeIfAbsent(each.getDataSourceName(), unused -> new
ArrayList<>(executionUnits.size())).add(each);
}
return result;
}
diff --git
a/infra/executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/prepare/driver/DriverExecutionPrepareEngine.java
b/infra/executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/prepare/driver/DriverExecutionPrepareEngine.java
index 00b8c6ed609..87e91317860 100644
---
a/infra/executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/prepare/driver/DriverExecutionPrepareEngine.java
+++
b/infra/executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/prepare/driver/DriverExecutionPrepareEngine.java
@@ -18,6 +18,7 @@
package org.apache.shardingsphere.infra.executor.sql.prepare.driver;
import lombok.Getter;
+import org.apache.shardingsphere.infra.annotation.HighFrequencyInvocation;
import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
import org.apache.shardingsphere.infra.executor.kernel.model.ExecutionGroup;
import org.apache.shardingsphere.infra.executor.sql.context.ExecutionUnit;
@@ -41,6 +42,7 @@ import java.util.concurrent.ConcurrentHashMap;
* @param <T> type of driver execution unit
* @param <C> type of resource connection
*/
+@HighFrequencyInvocation
public final class DriverExecutionPrepareEngine<T extends
DriverExecutionUnit<?>, C> extends AbstractExecutionPrepareEngine<T> {
@SuppressWarnings("rawtypes")
@@ -68,7 +70,7 @@ public final class DriverExecutionPrepareEngine<T extends
DriverExecutionUnit<?>
this.databaseConnectionManager = databaseConnectionManager;
this.statementManager = statementManager;
this.option = option;
- sqlExecutionUnitBuilder = getCachedSqlExecutionUnitBuilder(type);
+ sqlExecutionUnitBuilder = getCachedSQLExecutionUnitBuilder(type);
this.storageUnits = storageUnits;
}
@@ -79,7 +81,7 @@ public final class DriverExecutionPrepareEngine<T extends
DriverExecutionUnit<?>
* @return sql execution unit builder
*/
@SuppressWarnings("rawtypes")
- private SQLExecutionUnitBuilder getCachedSqlExecutionUnitBuilder(final
String type) {
+ private SQLExecutionUnitBuilder getCachedSQLExecutionUnitBuilder(final
String type) {
SQLExecutionUnitBuilder result;
if (null == (result = TYPE_TO_BUILDER_MAP.get(type))) {
result = TYPE_TO_BUILDER_MAP.computeIfAbsent(type, key ->
TypedSPILoader.getService(SQLExecutionUnitBuilder.class, key));
diff --git
a/infra/executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/prepare/driver/jdbc/builder/PreparedStatementExecutionUnitBuilder.java
b/infra/executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/prepare/driver/jdbc/builder/PreparedStatementExecutionUnitBuilder.java
index 43b2f5690ba..fba360ae9c6 100644
---
a/infra/executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/prepare/driver/jdbc/builder/PreparedStatementExecutionUnitBuilder.java
+++
b/infra/executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/prepare/driver/jdbc/builder/PreparedStatementExecutionUnitBuilder.java
@@ -37,8 +37,7 @@ public final class PreparedStatementExecutionUnitBuilder
implements JDBCExecutio
@Override
public JDBCExecutionUnit build(final ExecutionUnit executionUnit, final
ExecutorJDBCStatementManager statementManager, final Connection connection,
final int connectionOffset, final
ConnectionMode connectionMode, final StatementOption option, final DatabaseType
databaseType) throws SQLException {
- PreparedStatement preparedStatement = createPreparedStatement(
- executionUnit, statementManager, connection, connectionOffset,
connectionMode, option, databaseType);
+ PreparedStatement preparedStatement =
createPreparedStatement(executionUnit, statementManager, connection,
connectionOffset, connectionMode, option, databaseType);
return new JDBCExecutionUnit(executionUnit, connectionMode,
preparedStatement);
}
diff --git
a/infra/executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/prepare/raw/RawExecutionPrepareEngine.java
b/infra/executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/prepare/raw/RawExecutionPrepareEngine.java
index dfc50aceb15..b1572ef52da 100644
---
a/infra/executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/prepare/raw/RawExecutionPrepareEngine.java
+++
b/infra/executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/prepare/raw/RawExecutionPrepareEngine.java
@@ -17,6 +17,7 @@
package org.apache.shardingsphere.infra.executor.sql.prepare.raw;
+import org.apache.shardingsphere.infra.annotation.HighFrequencyInvocation;
import org.apache.shardingsphere.infra.executor.kernel.model.ExecutionGroup;
import org.apache.shardingsphere.infra.executor.sql.context.ExecutionUnit;
import
org.apache.shardingsphere.infra.executor.sql.execute.engine.ConnectionMode;
@@ -32,6 +33,7 @@ import java.util.stream.Collectors;
/**
* Raw execution prepare engine.
*/
+@HighFrequencyInvocation
public final class RawExecutionPrepareEngine extends
AbstractExecutionPrepareEngine<RawSQLExecutionUnit> {
public RawExecutionPrepareEngine(final int maxConnectionsSizePerQuery,
final Collection<ShardingSphereRule> rules) {