Skip to content

Commit

Permalink
improve performance for proxy by caching instances (apache#9746)
Browse files Browse the repository at this point in the history
  • Loading branch information
tuohai666 authored Mar 19, 2021
1 parent 801c823 commit b1e8607
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class SQLCheckEngine {

private static final Collection<SQLChecker> AUDITORS = OrderedSPIRegistry.getRegisteredServices(SQLChecker.class);

static {
ShardingSphereServiceLoader.register(SQLChecker.class);
}
Expand All @@ -47,8 +49,7 @@ public final class SQLCheckEngine {
* @param auth auth
*/
public static void check(final SQLStatement sqlStatement, final List<Object> parameters, final ShardingSphereMetaData metaData, final Authentication auth) {
Collection<SQLChecker> auditors = OrderedSPIRegistry.getRegisteredServices(SQLChecker.class);
for (SQLChecker each : auditors) {
for (SQLChecker each : AUDITORS) {
SQLCheckResult auditResult = each.check(sqlStatement, parameters, metaData, auth);
if (!auditResult.isPassed()) {
throw new SQLCheckException(each.getSQLCheckType(), auditResult.getErrorMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;

/**
* Abstract execution prepare engine.
Expand All @@ -47,14 +48,17 @@ public abstract class AbstractExecutionPrepareEngine<T> implements ExecutionPrep
ShardingSphereServiceLoader.register(ExecutionPrepareDecorator.class);
}

@SuppressWarnings("rawtypes")
private static final Map<Collection<ShardingSphereRule>, Map<ShardingSphereRule, ExecutionPrepareDecorator>> RULES_TO_DECORATORS_MAP = new ConcurrentHashMap<>(32, 1);

private final int maxConnectionsSizePerQuery;

@SuppressWarnings("rawtypes")
private final Map<ShardingSphereRule, ExecutionPrepareDecorator> decorators;

protected AbstractExecutionPrepareEngine(final int maxConnectionsSizePerQuery, final Collection<ShardingSphereRule> rules) {
this.maxConnectionsSizePerQuery = maxConnectionsSizePerQuery;
decorators = OrderedSPIRegistry.getRegisteredServices(rules, ExecutionPrepareDecorator.class);
decorators = RULES_TO_DECORATORS_MAP.computeIfAbsent(rules, key -> OrderedSPIRegistry.getRegisteredServices(key, ExecutionPrepareDecorator.class));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;

/**
* Driver execution prepare engine.
Expand All @@ -41,6 +43,9 @@
*/
public final class DriverExecutionPrepareEngine<T extends DriverExecutionUnit<?>, C> extends AbstractExecutionPrepareEngine<T> {

@SuppressWarnings("rawtypes")
private static final Map<String, SQLExecutionUnitBuilder> TYPE_TO_BUILDER_MAP = new ConcurrentHashMap<>(8, 1);

private final ExecutorDriverManager<C, ?, ?> executorDriverManager;

private final StorageResourceOption option;
Expand All @@ -57,7 +62,7 @@ public DriverExecutionPrepareEngine(final String type, final int maxConnectionsS
super(maxConnectionsSizePerQuery, rules);
this.executorDriverManager = executorDriverManager;
this.option = option;
sqlExecutionUnitBuilder = TypedSPIRegistry.getRegisteredService(SQLExecutionUnitBuilder.class, type, new Properties());
sqlExecutionUnitBuilder = TYPE_TO_BUILDER_MAP.computeIfAbsent(type, key -> TypedSPIRegistry.getRegisteredService(SQLExecutionUnitBuilder.class, key, new Properties()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;

/**
* Merge engine.
Expand All @@ -49,6 +50,9 @@ public final class MergeEngine {
ShardingSphereServiceLoader.register(ResultProcessEngine.class);
}

@SuppressWarnings("rawtypes")
private static final Map<Collection<ShardingSphereRule>, Map<ShardingSphereRule, ResultProcessEngine>> RULES_TO_ENGINES_MAP = new ConcurrentHashMap<>(32, 1);

private final DatabaseType databaseType;

private final ShardingSphereSchema schema;
Expand All @@ -62,7 +66,7 @@ public MergeEngine(final DatabaseType databaseType, final ShardingSphereSchema s
this.databaseType = databaseType;
this.schema = schema;
this.props = props;
engines = OrderedSPIRegistry.getRegisteredServices(rules, ResultProcessEngine.class);
engines = RULES_TO_ENGINES_MAP.computeIfAbsent(rules, key -> OrderedSPIRegistry.getRegisteredServices(key, ResultProcessEngine.class));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
* SQL rewrite entry.
Expand All @@ -43,6 +44,9 @@ public final class SQLRewriteEntry {
ShardingSphereServiceLoader.register(SQLRewriteContextDecorator.class);
}

@SuppressWarnings("rawtypes")
private static final Map<Collection<ShardingSphereRule>, Map<ShardingSphereRule, SQLRewriteContextDecorator>> RULES_TO_DECORATORS_MAP = new ConcurrentHashMap<>(32, 1);

private final ShardingSphereSchema schema;

private final ConfigurationProperties props;
Expand All @@ -53,7 +57,7 @@ public final class SQLRewriteEntry {
public SQLRewriteEntry(final ShardingSphereSchema schema, final ConfigurationProperties props, final Collection<ShardingSphereRule> rules) {
this.schema = schema;
this.props = props;
decorators = OrderedSPIRegistry.getRegisteredServices(rules, SQLRewriteContextDecorator.class);
decorators = RULES_TO_DECORATORS_MAP.computeIfAbsent(rules, key -> OrderedSPIRegistry.getRegisteredServices(key, SQLRewriteContextDecorator.class));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import java.util.Collections;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;

/**
* Partial SQL route executor.
Expand All @@ -43,14 +44,17 @@ public final class PartialSQLRouteExecutor implements SQLRouteExecutor {
ShardingSphereServiceLoader.register(SQLRouter.class);
}

@SuppressWarnings("rawtypes")
private static final Map<Collection<ShardingSphereRule>, Map<ShardingSphereRule, SQLRouter>> RULES_TO_ROUTERS_MAP = new ConcurrentHashMap<>(32, 1);

private final ConfigurationProperties props;

@SuppressWarnings("rawtypes")
private final Map<ShardingSphereRule, SQLRouter> routers;

public PartialSQLRouteExecutor(final Collection<ShardingSphereRule> rules, final ConfigurationProperties props) {
this.props = props;
routers = OrderedSPIRegistry.getRegisteredServices(rules, SQLRouter.class);
routers = RULES_TO_ROUTERS_MAP.computeIfAbsent(rules, key -> OrderedSPIRegistry.getRegisteredServices(key, SQLRouter.class));
}

@Override
Expand Down

0 comments on commit b1e8607

Please sign in to comment.