You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by zh...@apache.org on 2021/11/21 10:58:45 UTC

[shardingsphere] branch master updated: Use props to configure cache option (#13722)

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 203594b  Use props to configure cache option (#13722)
203594b is described below

commit 203594b7626450c45b80b271e68534be98950f21
Author: Guocheng Tang <to...@qq.com>
AuthorDate: Sun Nov 21 18:57:38 2021 +0800

    Use props to configure cache option (#13722)
---
 .../engine/type/standard/AbstractSQLRouteTest.java      |  2 +-
 .../config/properties/ConfigurationPropertyKey.java     | 17 ++++++++++++++++-
 .../infra/parser/ShardingSphereSQLParserEngine.java     |  3 +--
 .../infra/parser/cache/SQLStatementCacheBuilder.java    | 16 ++++++++++------
 .../infra/parser/sql/SQLStatementParserEngine.java      | 12 ++++++------
 .../parser/sql/SQLStatementParserEngineFactory.java     |  7 ++++---
 .../parser/cache/SQLStatementCacheBuilderTest.java      |  7 +++++--
 .../engine/AbstractSQLRewriterParameterizedTest.java    |  4 ++--
 8 files changed, 45 insertions(+), 23 deletions(-)

diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/route/engine/type/standard/AbstractSQLRouteTest.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/route/engine/type/standard/AbstractSQLRouteTest.java
index 713c3e3..bd07c32 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/route/engine/type/standard/AbstractSQLRouteTest.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/route/engine/type/standard/AbstractSQLRouteTest.java
@@ -57,7 +57,7 @@ public abstract class AbstractSQLRouteTest extends AbstractRoutingEngineTest {
         SingleTableRule singleTableRule = createAllSingleTableRule(Collections.singletonList(shardingRule));
         ShardingSphereSchema schema = buildSchema();
         ConfigurationProperties props = new ConfigurationProperties(new Properties());
-        SQLStatementParserEngine sqlStatementParserEngine = new SQLStatementParserEngine("MySQL", false);
+        SQLStatementParserEngine sqlStatementParserEngine = new SQLStatementParserEngine("MySQL", props);
         ShardingSphereRuleMetaData ruleMetaData = new ShardingSphereRuleMetaData(Collections.emptyList(), Arrays.asList(shardingRule, singleTableRule));
         ShardingSphereMetaData metaData = new ShardingSphereMetaData("sharding_db", mock(ShardingSphereResource.class, RETURNS_DEEP_STUBS), ruleMetaData, schema);
         Map<String, ShardingSphereMetaData> metaDataMap = Collections.singletonMap(DefaultSchema.LOGIC_NAME, metaData);
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/properties/ConfigurationPropertyKey.java b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/properties/ConfigurationPropertyKey.java
index 8b1c86a..470859d 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/properties/ConfigurationPropertyKey.java
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/properties/ConfigurationPropertyKey.java
@@ -122,7 +122,22 @@ public enum ConfigurationPropertyKey implements TypedPropertyKey {
     /**
      * Whether enable sql federation.
      */
-    SQL_FEDERATION_ENABLED("sql-federation-enabled", String.valueOf(Boolean.FALSE), boolean.class);
+    SQL_FEDERATION_ENABLED("sql-federation-enabled", String.valueOf(Boolean.FALSE), boolean.class),
+
+    /**
+     * SQL Statement cache initial capacity.
+     */
+    SQL_CACHE_INITIAL_CAPACITY("sql-cache-initial-capacity", String.valueOf(2000), int.class),
+
+    /**
+     * SQL statement cache maximum size.
+     */
+    SQL_CACHE_MAXIMUM_SIZE("sql-cache-maximum-size", String.valueOf(65535L), long.class),
+
+    /**
+     * SQL statement cache concurrency level.
+     */
+    SQL_CACHE_CONCURRENCY_LEVEL("sql-cache-concurrency-level", String.valueOf(4), int.class);
     
     private final String key;
     
diff --git a/shardingsphere-infra/shardingsphere-infra-parser/src/main/java/org/apache/shardingsphere/infra/parser/ShardingSphereSQLParserEngine.java b/shardingsphere-infra/shardingsphere-infra-parser/src/main/java/org/apache/shardingsphere/infra/parser/ShardingSphereSQLParserEngine.java
index 7f67a27..e5b24b1 100644
--- a/shardingsphere-infra/shardingsphere-infra-parser/src/main/java/org/apache/shardingsphere/infra/parser/ShardingSphereSQLParserEngine.java
+++ b/shardingsphere-infra/shardingsphere-infra-parser/src/main/java/org/apache/shardingsphere/infra/parser/ShardingSphereSQLParserEngine.java
@@ -21,7 +21,6 @@ import com.google.common.util.concurrent.UncheckedExecutionException;
 import org.antlr.v4.runtime.misc.ParseCancellationException;
 import org.apache.shardingsphere.distsql.parser.engine.api.DistSQLStatementParserEngine;
 import org.apache.shardingsphere.infra.config.properties.ConfigurationProperties;
-import org.apache.shardingsphere.infra.config.properties.ConfigurationPropertyKey;
 import org.apache.shardingsphere.infra.parser.sql.SQLStatementParserEngine;
 import org.apache.shardingsphere.infra.parser.sql.SQLStatementParserEngineFactory;
 import org.apache.shardingsphere.sql.parser.exception.SQLParsingException;
@@ -38,7 +37,7 @@ public final class ShardingSphereSQLParserEngine {
     private final DistSQLStatementParserEngine distSQLStatementParserEngine;
     
     public ShardingSphereSQLParserEngine(final String databaseTypeName, final ConfigurationProperties props) {
-        sqlStatementParserEngine = SQLStatementParserEngineFactory.getSQLStatementParserEngine(databaseTypeName, props.getValue(ConfigurationPropertyKey.SQL_COMMENT_PARSE_ENABLED));
+        sqlStatementParserEngine = SQLStatementParserEngineFactory.getSQLStatementParserEngine(databaseTypeName, props);
         distSQLStatementParserEngine = new DistSQLStatementParserEngine();
     }
     
diff --git a/shardingsphere-infra/shardingsphere-infra-parser/src/main/java/org/apache/shardingsphere/infra/parser/cache/SQLStatementCacheBuilder.java b/shardingsphere-infra/shardingsphere-infra-parser/src/main/java/org/apache/shardingsphere/infra/parser/cache/SQLStatementCacheBuilder.java
index b28506a..96c711d 100644
--- a/shardingsphere-infra/shardingsphere-infra-parser/src/main/java/org/apache/shardingsphere/infra/parser/cache/SQLStatementCacheBuilder.java
+++ b/shardingsphere-infra/shardingsphere-infra-parser/src/main/java/org/apache/shardingsphere/infra/parser/cache/SQLStatementCacheBuilder.java
@@ -21,7 +21,8 @@ import com.google.common.cache.CacheBuilder;
 import com.google.common.cache.LoadingCache;
 import lombok.AccessLevel;
 import lombok.NoArgsConstructor;
-import org.apache.shardingsphere.sql.parser.api.CacheOption;
+import org.apache.shardingsphere.infra.config.properties.ConfigurationProperties;
+import org.apache.shardingsphere.infra.config.properties.ConfigurationPropertyKey;
 import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
 
 /**
@@ -33,13 +34,16 @@ public final class SQLStatementCacheBuilder {
     /**
      * Build SQL statement cache.
      *
-     * @param option cache option
+     * @param props configuration props
      * @param databaseType database type
-     * @param sqlCommentParseEnabled sql comment parse enabled
      * @return built SQL statement cache
      */
-    public static LoadingCache<String, SQLStatement> build(final CacheOption option, final String databaseType, final boolean sqlCommentParseEnabled) {
-        return CacheBuilder.newBuilder().softValues().initialCapacity(option.getInitialCapacity()).maximumSize(option.getMaximumSize())
-                .concurrencyLevel(option.getConcurrencyLevel()).build(new SQLStatementCacheLoader(databaseType, sqlCommentParseEnabled));
+    public static LoadingCache<String, SQLStatement> build(final ConfigurationProperties props, final String databaseType) {
+        int initialCapacity = props.getValue(ConfigurationPropertyKey.SQL_CACHE_INITIAL_CAPACITY);
+        long maximumSize = props.getValue(ConfigurationPropertyKey.SQL_CACHE_MAXIMUM_SIZE);
+        int concurrencyLevel = props.getValue(ConfigurationPropertyKey.SQL_CACHE_CONCURRENCY_LEVEL);
+        boolean sqlCommentParseEnabled = props.getValue(ConfigurationPropertyKey.SQL_COMMENT_PARSE_ENABLED);
+        return CacheBuilder.newBuilder().softValues().initialCapacity(initialCapacity).maximumSize(maximumSize)
+                .concurrencyLevel(concurrencyLevel).build(new SQLStatementCacheLoader(databaseType, sqlCommentParseEnabled));
     }
 }
diff --git a/shardingsphere-infra/shardingsphere-infra-parser/src/main/java/org/apache/shardingsphere/infra/parser/sql/SQLStatementParserEngine.java b/shardingsphere-infra/shardingsphere-infra-parser/src/main/java/org/apache/shardingsphere/infra/parser/sql/SQLStatementParserEngine.java
index 3ae5720..73b47a1 100644
--- a/shardingsphere-infra/shardingsphere-infra-parser/src/main/java/org/apache/shardingsphere/infra/parser/sql/SQLStatementParserEngine.java
+++ b/shardingsphere-infra/shardingsphere-infra-parser/src/main/java/org/apache/shardingsphere/infra/parser/sql/SQLStatementParserEngine.java
@@ -18,8 +18,9 @@
 package org.apache.shardingsphere.infra.parser.sql;
 
 import com.google.common.cache.LoadingCache;
+import org.apache.shardingsphere.infra.config.properties.ConfigurationProperties;
+import org.apache.shardingsphere.infra.config.properties.ConfigurationPropertyKey;
 import org.apache.shardingsphere.infra.parser.cache.SQLStatementCacheBuilder;
-import org.apache.shardingsphere.sql.parser.api.CacheOption;
 import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
 
 /**
@@ -30,11 +31,10 @@ public final class SQLStatementParserEngine {
     private final SQLStatementParserExecutor sqlStatementParserExecutor;
     
     private final LoadingCache<String, SQLStatement> sqlStatementCache;
-    
-    public SQLStatementParserEngine(final String databaseType, final boolean sqlCommentParseEnabled) {
-        sqlStatementParserExecutor = new SQLStatementParserExecutor(databaseType, sqlCommentParseEnabled);
-        // TODO use props to configure cache option
-        sqlStatementCache = SQLStatementCacheBuilder.build(new CacheOption(2000, 65535L, 4), databaseType, sqlCommentParseEnabled);
+
+    public SQLStatementParserEngine(final String databaseType, final ConfigurationProperties props) {
+        sqlStatementParserExecutor = new SQLStatementParserExecutor(databaseType, props.getValue(ConfigurationPropertyKey.SQL_COMMENT_PARSE_ENABLED));
+        sqlStatementCache = SQLStatementCacheBuilder.build(props, databaseType);
     }
     
     /**
diff --git a/shardingsphere-infra/shardingsphere-infra-parser/src/main/java/org/apache/shardingsphere/infra/parser/sql/SQLStatementParserEngineFactory.java b/shardingsphere-infra/shardingsphere-infra-parser/src/main/java/org/apache/shardingsphere/infra/parser/sql/SQLStatementParserEngineFactory.java
index c43e675..09c4c4e 100644
--- a/shardingsphere-infra/shardingsphere-infra-parser/src/main/java/org/apache/shardingsphere/infra/parser/sql/SQLStatementParserEngineFactory.java
+++ b/shardingsphere-infra/shardingsphere-infra-parser/src/main/java/org/apache/shardingsphere/infra/parser/sql/SQLStatementParserEngineFactory.java
@@ -19,6 +19,7 @@ package org.apache.shardingsphere.infra.parser.sql;
 
 import lombok.AccessLevel;
 import lombok.NoArgsConstructor;
+import org.apache.shardingsphere.infra.config.properties.ConfigurationProperties;
 
 import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
@@ -35,10 +36,10 @@ public final class SQLStatementParserEngineFactory {
      * Get SQL statement parser engine.
      *
      * @param databaseType name of database type
-     * @param sqlCommentParseEnabled sql comment parse enabled
+     * @param props configuration props
      * @return SQL statement parser engine
      */
-    public static SQLStatementParserEngine getSQLStatementParserEngine(final String databaseType, final boolean sqlCommentParseEnabled) {
-        return ENGINES.getOrDefault(databaseType, ENGINES.computeIfAbsent(databaseType, key -> new SQLStatementParserEngine(key, sqlCommentParseEnabled)));
+    public static SQLStatementParserEngine getSQLStatementParserEngine(final String databaseType, final ConfigurationProperties props) {
+        return ENGINES.getOrDefault(databaseType, ENGINES.computeIfAbsent(databaseType, key -> new SQLStatementParserEngine(key, props)));
     }
 }
diff --git a/shardingsphere-infra/shardingsphere-infra-parser/src/test/java/org/apache/shardingsphere/infra/parser/cache/SQLStatementCacheBuilderTest.java b/shardingsphere-infra/shardingsphere-infra-parser/src/test/java/org/apache/shardingsphere/infra/parser/cache/SQLStatementCacheBuilderTest.java
index ecb2e01..29895d5 100644
--- a/shardingsphere-infra/shardingsphere-infra-parser/src/test/java/org/apache/shardingsphere/infra/parser/cache/SQLStatementCacheBuilderTest.java
+++ b/shardingsphere-infra/shardingsphere-infra-parser/src/test/java/org/apache/shardingsphere/infra/parser/cache/SQLStatementCacheBuilderTest.java
@@ -18,9 +18,11 @@
 package org.apache.shardingsphere.infra.parser.cache;
 
 import com.google.common.cache.LoadingCache;
-import org.apache.shardingsphere.sql.parser.api.CacheOption;
+import org.apache.shardingsphere.infra.config.properties.ConfigurationProperties;
 import org.junit.Test;
 
+import java.util.Properties;
+
 import static org.hamcrest.CoreMatchers.isA;
 import static org.junit.Assert.assertThat;
 
@@ -28,6 +30,7 @@ public final class SQLStatementCacheBuilderTest {
     
     @Test
     public void assertBuild() {
-        assertThat(SQLStatementCacheBuilder.build(new CacheOption(128, 1024L, 4), "MySQL", false), isA(LoadingCache.class));
+        ConfigurationProperties props = new ConfigurationProperties(new Properties());
+        assertThat(SQLStatementCacheBuilder.build(props, "MySQL"), isA(LoadingCache.class));
     }
 }
diff --git a/shardingsphere-test/shardingsphere-rewrite-test/src/test/java/org/apache/shardingsphere/sharding/rewrite/parameterized/engine/AbstractSQLRewriterParameterizedTest.java b/shardingsphere-test/shardingsphere-rewrite-test/src/test/java/org/apache/shardingsphere/sharding/rewrite/parameterized/engine/AbstractSQLRewriterParameterizedTest.java
index 87f03df..b51dd09 100644
--- a/shardingsphere-test/shardingsphere-rewrite-test/src/test/java/org/apache/shardingsphere/sharding/rewrite/parameterized/engine/AbstractSQLRewriterParameterizedTest.java
+++ b/shardingsphere-test/shardingsphere-rewrite-test/src/test/java/org/apache/shardingsphere/sharding/rewrite/parameterized/engine/AbstractSQLRewriterParameterizedTest.java
@@ -88,9 +88,9 @@ public abstract class AbstractSQLRewriterParameterizedTest {
                 new YamlRuleConfigurationSwapperEngine().swapToRuleConfigurations(rootConfig.getRules()), DatabaseTypeRegistry.getTrunkDatabaseType(databaseType),
                 new YamlDataSourceConfigurationSwapper().swapToDataSources(rootConfig.getDataSources()), new ConfigurationProperties(new Properties())));
         mockRules(rules);
-        SQLStatementParserEngine sqlStatementParserEngine = new SQLStatementParserEngine(databaseType, false);
-        ShardingSphereSchema schema = mockSchema();
         ConfigurationProperties props = new ConfigurationProperties(rootConfig.getProps());
+        SQLStatementParserEngine sqlStatementParserEngine = new SQLStatementParserEngine(databaseType, props);
+        ShardingSphereSchema schema = mockSchema();
         ShardingSphereMetaData metaData = new ShardingSphereMetaData("sharding_db", mock(ShardingSphereResource.class), new ShardingSphereRuleMetaData(Collections.emptyList(), rules), schema);
         Map<String, ShardingSphereMetaData> metaDataMap = new HashMap<>(2, 1);
         metaDataMap.put(DefaultSchema.LOGIC_NAME, metaData);