Skip to content

Commit

Permalink
Merge ShardingCacheRule into ShardingRule (apache#25520)
Browse files Browse the repository at this point in the history
* Merge sharding cache plugin into sharding modules

* Refactor CachedShardingSQLRouter

* Fix checkstyle in CachedShardingSQLRouter

* Complete ShardingRuleConfigurationYamlIT

* Update documents for sharding cache

* Complete YamlShardingRuleConfigurationSwapper

* Remove usage of shardingsphere-sharding-cache

* Update documents about optional plugins
  • Loading branch information
TeslaCN authored May 9, 2023
1 parent e55d450 commit 5ff7f29
Show file tree
Hide file tree
Showing 46 changed files with 368 additions and 614 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ ShardingSphere 默认情况下仅包含核心 SPI 的实现,在 Git Source 存
- `org.apache.shardingsphere:shardingsphere-postgresql-dialect-exception`,数据库网关的 PostgreSQL 实现
- `org.apache.shardingsphere:shardingsphere-readwrite-splitting-core`,读写分离核心
- `org.apache.shardingsphere:shardingsphere-shadow-core`,影子库核心
- `org.apache.shardingsphere:shardingsphere-sharding-cache`,分片缓存,参考未关闭的 https://github.com/apache/shardingsphere/issues/21223
- `org.apache.shardingsphere:shardingsphere-sharding-core`,数据分片核心
- `org.apache.shardingsphere:shardingsphere-single-core`,单表(所有的分片数据源中仅唯一存在的表)核心
- `org.apache.shardingsphere:shardingsphere-sql-federation-core`,联邦查询执行器核心
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ All the built-in plugins for ShardingSphere-JDBC are listed below in the form of
- `org.apache.shardingsphere:shardingsphere-postgresql-dialect-exception`, PostgreSQL implementation of database
- `org.apache.shardingsphere:shardingsphere-readwrite-splitting-core`, read-write splitting core
- `org.apache.shardingsphere:shardingsphere-shadow-core`, shadow library core
- `org.apache.shardingsphere:shardingsphere-sharding-cache`, sharding cache,refer to https://github.com/apache/shardingsphere/issues/21223
- `org.apache.shardingsphere:shardingsphere-sharding-core`, data sharding core
- `org.apache.shardingsphere:shardingsphere-single-core`, single-table (only the only table that exists in all sharded data sources) core
- `org.apache.shardingsphere:shardingsphere-sql-federation-core`, federation query executor core
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ weight = 10

```yaml
rules:
- !SHARDING_CACHE
- !SHARDING
tables:
shardingAlgorithms:
# ...
shardingCache:
allowedMaxSqlLength: 512 # 允许缓存的 SQL 长度限制
routeCache:
initialCapacity: 65536 # 缓存初始容量
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ If the above conditions are not met, the execution delay of SQL may not be signi

```yaml
rules:
- !SHARDING_CACHE
- !SHARDING
tables:
shardingAlgorithms:
# ...
shardingCache:
allowedMaxSqlLength: 512 # Allow cached SQL length limit
routeCache:
initialCapacity: 65536 # Initial capacity
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ ShardingSphere 默认情况下仅包含核心 SPI 的实现,在 Git Source 存
- `org.apache.shardingsphere:shardingsphere-proxy-frontend-postgresql`,用于 ShardingSphere-Proxy 解析与适配访问数据库的协议的 PostgreSQL 实现
- `org.apache.shardingsphere:shardingsphere-proxy-frontend-opengauss`,用于 ShardingSphere-Proxy 解析与适配访问数据库的协议的 openGauss 实现
- `org.apache.shardingsphere:shardingsphere-proxy-backend-core`, ShardingSphere Proxy 的后端核心模块
- `org.apache.shardingsphere:shardingsphere-sharding-cache`, 分片缓存,参考未关闭的 https://github.com/apache/shardingsphere/issues/21223
- `org.apache.shardingsphere:shardingsphere-standalone-mode-core`,单机模式配置信息持久化定义核心

对于核心的 `org.apache.shardingsphere:shardingsphere-jdbc-core`,其内置插件参考[ShardingSphere-JDBC 可选插件](/cn/user-manual/shardingsphere-jdbc/optional-plugins/)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ All the built-in plugins for ShardingSphere-Proxy are listed below in the form o
- `org.apache.shardingsphere:shardingsphere-proxy-frontend-postgresql`, a PostgreSQL implementation for ShardingSphere-Proxy to parse and adapt the protocol for accessing the database
- `org.apache.shardingsphere:shardingsphere-proxy-frontend-opengauss`, an openGauss implementation for ShardingSphere-Proxy to parse and adapt the protocol for accessing the database
- `org.apache.shardingsphere:shardingsphere-proxy-backend-core`, the backend core for ShardingSphere Proxy
- `org.apache.shardingsphere:shardingsphere-sharding-cache`, sharding cache, refer to https://github.com/apache/shardingsphere/issues/21223
- `org.apache.shardingsphere:shardingsphere-standalone-mode-core`, the persistence definition core of single-machine mode configuration information

For the core `org.apache.shardingsphere:shardingsphere-jdbc-core`,Its built-in plugins reference[ShardingSphere-JDBC Optional Plugins](/en/user-manual/shardingsphere-jdbc/optional-plugins/).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration;
import org.apache.shardingsphere.infra.config.rule.function.DistributedRuleConfiguration;
import org.apache.shardingsphere.infra.config.rule.scope.DatabaseRuleConfiguration;
import org.apache.shardingsphere.sharding.api.config.cache.ShardingCacheConfiguration;
import org.apache.shardingsphere.sharding.api.config.rule.ShardingAutoTableRuleConfiguration;
import org.apache.shardingsphere.sharding.api.config.rule.ShardingTableReferenceRuleConfiguration;
import org.apache.shardingsphere.sharding.api.config.rule.ShardingTableRuleConfiguration;
Expand Down Expand Up @@ -64,4 +65,6 @@ public final class ShardingRuleConfiguration implements DatabaseRuleConfiguratio
private Map<String, AlgorithmConfiguration> keyGenerators = new LinkedHashMap<>();

private Map<String, AlgorithmConfiguration> auditors = new LinkedHashMap<>();

private ShardingCacheConfiguration shardingCache;
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,21 @@
* limitations under the License.
*/

package org.apache.shardingsphere.sharding.cache.api;
package org.apache.shardingsphere.sharding.api.config.cache;

import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.ToString;
import org.apache.shardingsphere.infra.config.rule.function.EnhancedRuleConfiguration;
import org.apache.shardingsphere.infra.config.rule.scope.DatabaseRuleConfiguration;

/**
* Configuration for sharding cache rule.
* Configuration for sharding cache.
*/
@RequiredArgsConstructor
@Getter
@ToString
public final class ShardingCacheRuleConfiguration implements DatabaseRuleConfiguration, EnhancedRuleConfiguration {
public final class ShardingCacheConfiguration {

private final int allowedMaxSqlLength;

private final ShardingCacheOptions routeCache;
private final ShardingCacheOptionsConfiguration routeCache;
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* limitations under the License.
*/

package org.apache.shardingsphere.sharding.cache.api;
package org.apache.shardingsphere.sharding.api.config.cache;

import lombok.Getter;
import lombok.RequiredArgsConstructor;
Expand All @@ -27,7 +27,7 @@
@RequiredArgsConstructor
@Getter
@ToString
public final class ShardingCacheOptions {
public final class ShardingCacheOptionsConfiguration {

private final boolean softValues;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,23 @@
* limitations under the License.
*/

package org.apache.shardingsphere.sharding.cache.rule;
package org.apache.shardingsphere.sharding.cache;

import lombok.Getter;
import org.apache.shardingsphere.infra.rule.identifier.scope.DatabaseRule;
import org.apache.shardingsphere.sharding.cache.api.ShardingCacheRuleConfiguration;
import org.apache.shardingsphere.sharding.api.config.cache.ShardingCacheConfiguration;
import org.apache.shardingsphere.sharding.cache.checker.ShardingRouteCacheableChecker;
import org.apache.shardingsphere.sharding.cache.route.cache.ShardingRouteCache;
import org.apache.shardingsphere.sharding.rule.ShardingRule;
import org.apache.shardingsphere.timeservice.core.rule.TimeServiceRule;
import org.apache.shardingsphere.timeservice.core.rule.builder.DefaultTimeServiceConfigurationBuilder;

/**
* Sharding cache rule.
* <strong>EXPERIMENTAL</strong> Sharding cache.
*/
@Getter
public final class ShardingCacheRule implements DatabaseRule {
public final class ShardingCache {

private final ShardingCacheRuleConfiguration configuration;
private final ShardingCacheConfiguration configuration;

private final ShardingRule shardingRule;

Expand All @@ -41,16 +41,11 @@ public final class ShardingCacheRule implements DatabaseRule {

private final ShardingRouteCache routeCache;

public ShardingCacheRule(final ShardingCacheRuleConfiguration configuration, final ShardingRule shardingRule, final TimeServiceRule timeServiceRule) {
public ShardingCache(final ShardingCacheConfiguration configuration, final ShardingRule shardingRule) {
this.configuration = configuration;
this.shardingRule = shardingRule;
this.timeServiceRule = timeServiceRule;
timeServiceRule = new TimeServiceRule(new DefaultTimeServiceConfigurationBuilder().build());
routeCacheableChecker = new ShardingRouteCacheableChecker(this);
routeCache = new ShardingRouteCache(configuration.getRouteCache());
}

@Override
public String getType() {
return ShardingCacheRule.class.getSimpleName();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,17 @@
import com.google.common.collect.Range;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import org.apache.shardingsphere.infra.session.query.QueryContext;
import org.apache.shardingsphere.infra.binder.segment.insert.keygen.GeneratedKeyContext;
import org.apache.shardingsphere.infra.binder.statement.SQLStatementContext;
import org.apache.shardingsphere.infra.binder.statement.dml.DeleteStatementContext;
import org.apache.shardingsphere.infra.binder.statement.dml.InsertStatementContext;
import org.apache.shardingsphere.infra.binder.statement.dml.SelectStatementContext;
import org.apache.shardingsphere.infra.binder.statement.dml.UpdateStatementContext;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.sharding.cache.api.ShardingCacheOptions;
import org.apache.shardingsphere.infra.session.query.QueryContext;
import org.apache.shardingsphere.sharding.api.config.cache.ShardingCacheOptionsConfiguration;
import org.apache.shardingsphere.sharding.cache.ShardingCache;
import org.apache.shardingsphere.sharding.cache.checker.algorithm.CacheableShardingAlgorithmChecker;
import org.apache.shardingsphere.sharding.cache.rule.ShardingCacheRule;
import org.apache.shardingsphere.sharding.route.engine.condition.ShardingCondition;
import org.apache.shardingsphere.sharding.route.engine.condition.engine.InsertClauseShardingConditionEngine;
import org.apache.shardingsphere.sharding.route.engine.condition.engine.WhereClauseShardingConditionEngine;
Expand Down Expand Up @@ -67,13 +67,13 @@ public final class ShardingRouteCacheableChecker {

private final LoadingCache<Key, ShardingRouteCacheableCheckResult> checkingCache;

public ShardingRouteCacheableChecker(final ShardingCacheRule shardingCacheRule) {
shardingRule = shardingCacheRule.getShardingRule();
timeServiceRule = shardingCacheRule.getTimeServiceRule();
checkingCache = buildCache(shardingCacheRule.getConfiguration().getRouteCache());
public ShardingRouteCacheableChecker(final ShardingCache shardingCache) {
shardingRule = shardingCache.getShardingRule();
timeServiceRule = shardingCache.getTimeServiceRule();
checkingCache = buildCache(shardingCache.getConfiguration().getRouteCache());
}

private LoadingCache<Key, ShardingRouteCacheableCheckResult> buildCache(final ShardingCacheOptions cacheOptions) {
private LoadingCache<Key, ShardingRouteCacheableCheckResult> buildCache(final ShardingCacheOptionsConfiguration cacheOptions) {
Caffeine<Object, Object> result = Caffeine.newBuilder().initialCapacity(cacheOptions.getInitialCapacity()).maximumSize(cacheOptions.getMaximumSize());
if (cacheOptions.isSoftValues()) {
result.softValues();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,74 +17,86 @@

package org.apache.shardingsphere.sharding.cache.route;

import org.apache.shardingsphere.infra.session.query.QueryContext;
import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
import org.apache.shardingsphere.infra.session.connection.ConnectionContext;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.metadata.database.rule.ShardingSphereRuleMetaData;
import org.apache.shardingsphere.infra.route.SQLRouter;
import org.apache.shardingsphere.infra.route.context.RouteContext;
import org.apache.shardingsphere.infra.session.connection.ConnectionContext;
import org.apache.shardingsphere.infra.session.query.QueryContext;
import org.apache.shardingsphere.sharding.cache.ShardingCache;
import org.apache.shardingsphere.sharding.cache.checker.ShardingRouteCacheableCheckResult;
import org.apache.shardingsphere.sharding.cache.route.cache.ShardingRouteCacheKey;
import org.apache.shardingsphere.sharding.cache.route.cache.ShardingRouteCacheValue;
import org.apache.shardingsphere.sharding.cache.rule.ShardingCacheRule;
import org.apache.shardingsphere.sharding.constant.ShardingOrder;
import org.apache.shardingsphere.sharding.route.engine.ShardingSQLRouter;
import org.apache.shardingsphere.sharding.rule.ShardingRule;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
* TODO Design a cache layer interface in kernel.
* Cached sharding SQL router.
*/
public final class CachedShardingSQLRouter implements SQLRouter<ShardingCacheRule> {
public final class CachedShardingSQLRouter {

@Override
public RouteContext createRouteContext(final QueryContext queryContext, final ShardingSphereRuleMetaData globalRuleMetaData,
final ShardingSphereDatabase database, final ShardingCacheRule rule, final ConfigurationProperties props, final ConnectionContext connectionContext) {
if (queryContext.getSql().length() > rule.getConfiguration().getAllowedMaxSqlLength()) {
return new RouteContext();
/**
* Find {@link RouteContext} from cache or calculate and try caching.
*
* @param originSQLRouter origin SQL router
* @param queryContext query context
* @param globalRuleMetaData global rule meta data
* @param database database
* @param shardingCache sharding cache
* @param props configuration properties
* @param connectionContext connection context
* @return route context
*/
public Optional<RouteContext> loadRouteContext(final OriginSQLRouter originSQLRouter, final QueryContext queryContext, final ShardingSphereRuleMetaData globalRuleMetaData,
final ShardingSphereDatabase database, final ShardingCache shardingCache, final ConfigurationProperties props,
final ConnectionContext connectionContext) {
if (queryContext.getSql().length() > shardingCache.getConfiguration().getAllowedMaxSqlLength()) {
return Optional.empty();
}
ShardingRouteCacheableCheckResult cacheableCheckResult = rule.getRouteCacheableChecker().check(database, queryContext);
ShardingRouteCacheableCheckResult cacheableCheckResult = shardingCache.getRouteCacheableChecker().check(database, queryContext);
if (!cacheableCheckResult.isProbablyCacheable()) {
return new RouteContext();
return Optional.empty();
}
List<Object> shardingConditionParams = new ArrayList<>(cacheableCheckResult.getShardingConditionParameterMarkerIndexes().size());
for (int each : cacheableCheckResult.getShardingConditionParameterMarkerIndexes()) {
if (each >= queryContext.getParameters().size()) {
return new RouteContext();
return Optional.empty();
}
shardingConditionParams.add(queryContext.getParameters().get(each));
}
Optional<RouteContext> cachedRouteContext = rule.getRouteCache().get(new ShardingRouteCacheKey(queryContext.getSql(), shardingConditionParams))
Optional<RouteContext> cachedResult = shardingCache.getRouteCache().get(new ShardingRouteCacheKey(queryContext.getSql(), shardingConditionParams))
.flatMap(ShardingRouteCacheValue::getCachedRouteContext);
RouteContext result = cachedRouteContext.orElseGet(
() -> new ShardingSQLRouter().createRouteContext(queryContext, globalRuleMetaData, database, rule.getShardingRule(), props, connectionContext));
if (!cachedRouteContext.isPresent() && hitOneShardOnly(result)) {
rule.getRouteCache().put(new ShardingRouteCacheKey(queryContext.getSql(), shardingConditionParams), new ShardingRouteCacheValue(result));
RouteContext result = cachedResult.orElseGet(
() -> originSQLRouter.createRouteContext(queryContext, globalRuleMetaData, database, shardingCache.getShardingRule(), props, connectionContext));
if (!cachedResult.isPresent() && hitOneShardOnly(result)) {
shardingCache.getRouteCache().put(new ShardingRouteCacheKey(queryContext.getSql(), shardingConditionParams), new ShardingRouteCacheValue(result));
}
return result;
return Optional.of(result);
}

private boolean hitOneShardOnly(final RouteContext routeContext) {
return 1 == routeContext.getRouteUnits().size() && 1 == routeContext.getRouteUnits().iterator().next().getTableMappers().size()
&& 1 == routeContext.getOriginalDataNodes().size() && 1 == routeContext.getOriginalDataNodes().iterator().next().size();
}

@Override
public void decorateRouteContext(final RouteContext routeContext, final QueryContext queryContext, final ShardingSphereDatabase database, final ShardingCacheRule rule,
final ConfigurationProperties props, final ConnectionContext connectionContext) {
}

@Override
public int getOrder() {
return ShardingOrder.ORDER - 1;
}

@Override
public Class<ShardingCacheRule> getTypeClass() {
return ShardingCacheRule.class;
@FunctionalInterface
public interface OriginSQLRouter {

/**
* Create route context.
*
* @param queryContext query context
* @param globalRuleMetaData global rule meta data
* @param database database
* @param rule rule
* @param props configuration properties
* @param connectionContext connection context
* @return route context
*/
RouteContext createRouteContext(QueryContext queryContext, ShardingSphereRuleMetaData globalRuleMetaData, ShardingSphereDatabase database, ShardingRule rule,
ConfigurationProperties props, ConnectionContext connectionContext);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import org.apache.shardingsphere.sharding.cache.api.ShardingCacheOptions;
import org.apache.shardingsphere.sharding.api.config.cache.ShardingCacheOptionsConfiguration;

import java.util.Optional;

Expand All @@ -30,11 +30,11 @@ public final class ShardingRouteCache {

private final Cache<ShardingRouteCacheKey, ShardingRouteCacheValue> cache;

public ShardingRouteCache(final ShardingCacheOptions cacheOptions) {
public ShardingRouteCache(final ShardingCacheOptionsConfiguration cacheOptions) {
cache = buildRouteCache(cacheOptions);
}

private Cache<ShardingRouteCacheKey, ShardingRouteCacheValue> buildRouteCache(final ShardingCacheOptions cacheOptions) {
private Cache<ShardingRouteCacheKey, ShardingRouteCacheValue> buildRouteCache(final ShardingCacheOptionsConfiguration cacheOptions) {
Caffeine<Object, Object> result = Caffeine.newBuilder().initialCapacity(cacheOptions.getInitialCapacity()).maximumSize(cacheOptions.getMaximumSize());
if (cacheOptions.isSoftValues()) {
result.softValues();
Expand Down
Loading

0 comments on commit 5ff7f29

Please sign in to comment.