From b38c537c7f99c4304ffbd2e4e6201152aab9991d Mon Sep 17 00:00:00 2001 From: "Juan Pan(Trista)" Date: Fri, 31 Jul 2020 20:05:46 +0800 Subject: [PATCH] use ShardingSphereSQLParserEngine instead of SQLParserEngine (#6557) --- .../jdbc/core/context/RuntimeContext.java | 139 ------------------ .../ShardingSphereResultSetTest.java | 9 +- .../shardingsphere-kernel-context/pom.xml | 2 +- .../kernel/context/SchemaContextsBuilder.java | 4 +- .../context/runtime/RuntimeContext.java | 4 +- ...atabaseCommunicationEngineFactoryTest.java | 4 +- .../ShardingCTLExplainBackendHandlerTest.java | 3 +- .../MySQLCommandExecutorFactoryTest.java | 4 +- .../MySQLComStmtExecuteExecutorTest.java | 8 +- 9 files changed, 18 insertions(+), 159 deletions(-) delete mode 100644 shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/context/RuntimeContext.java diff --git a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/context/RuntimeContext.java b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/context/RuntimeContext.java deleted file mode 100644 index 3078523fde3fc..0000000000000 --- a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/context/RuntimeContext.java +++ /dev/null @@ -1,139 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.shardingsphere.driver.jdbc.core.context; - -import com.google.common.collect.ImmutableMap; -import lombok.Getter; -import lombok.Setter; -import lombok.extern.slf4j.Slf4j; -import org.apache.shardingsphere.driver.jdbc.core.datasource.metadata.CachedDatabaseMetaData; -import org.apache.shardingsphere.infra.log.ConfigurationLogger; -import org.apache.shardingsphere.sql.parser.engine.SQLParserEngine; -import org.apache.shardingsphere.sql.parser.engine.SQLParserEngineFactory; -import org.apache.shardingsphere.transaction.ShardingTransactionManagerEngine; -import org.apache.shardingsphere.infra.config.DatabaseAccessConfiguration; -import org.apache.shardingsphere.infra.config.RuleConfiguration; -import org.apache.shardingsphere.infra.config.properties.ConfigurationProperties; -import org.apache.shardingsphere.infra.config.properties.ConfigurationPropertyKey; -import org.apache.shardingsphere.infra.database.DefaultSchema; -import org.apache.shardingsphere.infra.database.type.DatabaseType; -import org.apache.shardingsphere.infra.database.type.DatabaseTypes; -import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData; -import org.apache.shardingsphere.infra.metadata.datasource.DataSourceMetas; -import org.apache.shardingsphere.infra.metadata.schema.RuleSchemaMetaData; -import org.apache.shardingsphere.infra.metadata.schema.RuleSchemaMetaDataLoader; -import org.apache.shardingsphere.infra.rule.ShardingSphereRule; -import org.apache.shardingsphere.infra.rule.ShardingSphereRulesBuilder; -import org.apache.shardingsphere.infra.executor.kernel.ExecutorKernel; - -import javax.sql.DataSource; -import java.sql.Connection; -import java.sql.DatabaseMetaData; -import java.sql.SQLException; -import java.util.Collection; -import java.util.LinkedHashMap; -import java.util.Map; -import java.util.Map.Entry; -import java.util.Properties; - -/** - * Runtime context. - */ -@Getter -@Slf4j(topic = "ShardingSphere-metadata") -public final class RuntimeContext implements AutoCloseable { - - private final Map dataSourceMap; - - private final DatabaseType databaseType; - - private final Collection configurations; - - private final Collection rules; - - private final ConfigurationProperties props; - - private final ExecutorKernel executorKernel; - - private final SQLParserEngine sqlParserEngine; - - private final CachedDatabaseMetaData cachedDatabaseMetaData; - - private final ShardingTransactionManagerEngine shardingTransactionManagerEngine; - - @Setter - private ShardingSphereMetaData metaData; - - public RuntimeContext(final Map dataSourceMap, - final DatabaseType databaseType, final Collection configurations, final Properties props) throws SQLException { - this.dataSourceMap = dataSourceMap; - this.databaseType = databaseType; - this.configurations = configurations; - rules = ShardingSphereRulesBuilder.build(configurations, dataSourceMap.keySet()); - this.props = new ConfigurationProperties(null == props ? new Properties() : props); - executorKernel = new ExecutorKernel(this.props.getValue(ConfigurationPropertyKey.EXECUTOR_SIZE)); - sqlParserEngine = SQLParserEngineFactory.getSQLParserEngine(DatabaseTypes.getTrunkDatabaseTypeName(databaseType)); - cachedDatabaseMetaData = createCachedDatabaseMetaData(dataSourceMap); - shardingTransactionManagerEngine = new ShardingTransactionManagerEngine(); - shardingTransactionManagerEngine.init(databaseType, dataSourceMap); - metaData = createMetaData(dataSourceMap, databaseType); - log(configurations, props); - } - - public RuntimeContext(final DataSource dataSource, final DatabaseType databaseType, final Collection configurations, final Properties props) throws SQLException { - this(ImmutableMap.of(DefaultSchema.LOGIC_NAME, dataSource), databaseType, configurations, props); - } - - private CachedDatabaseMetaData createCachedDatabaseMetaData(final Map dataSourceMap) throws SQLException { - try (Connection connection = dataSourceMap.values().iterator().next().getConnection()) { - return new CachedDatabaseMetaData(connection.getMetaData()); - } - } - - private ShardingSphereMetaData createMetaData(final Map dataSourceMap, final DatabaseType databaseType) throws SQLException { - long start = System.currentTimeMillis(); - DataSourceMetas dataSourceMetas = new DataSourceMetas(databaseType, getDatabaseAccessConfigurationMap(dataSourceMap)); - RuleSchemaMetaData ruleSchemaMetaData = new RuleSchemaMetaDataLoader(rules).load(getDatabaseType(), dataSourceMap, getProps(), executorKernel.getExecutorService().getExecutorService()); - ShardingSphereMetaData result = new ShardingSphereMetaData(dataSourceMetas, ruleSchemaMetaData); - log.info("Meta data load finished, cost {} milliseconds.", System.currentTimeMillis() - start); - return result; - } - - private Map getDatabaseAccessConfigurationMap(final Map dataSourceMap) throws SQLException { - Map result = new LinkedHashMap<>(dataSourceMap.size(), 1); - for (Entry entry : dataSourceMap.entrySet()) { - DataSource dataSource = entry.getValue(); - try (Connection connection = dataSource.getConnection()) { - DatabaseMetaData metaData = connection.getMetaData(); - result.put(entry.getKey(), new DatabaseAccessConfiguration(metaData.getURL(), metaData.getUserName(), null)); - } - } - return result; - } - - private void log(final Collection configurations, final Properties props) { - ConfigurationLogger.log(configurations); - ConfigurationLogger.log(props); - } - - @Override - public void close() throws Exception { - shardingTransactionManagerEngine.close(); - executorKernel.close(); - } -} diff --git a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/resultset/ShardingSphereResultSetTest.java b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/resultset/ShardingSphereResultSetTest.java index 5f92e604a5270..892404895eecc 100644 --- a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/resultset/ShardingSphereResultSetTest.java +++ b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/resultset/ShardingSphereResultSetTest.java @@ -17,11 +17,7 @@ package org.apache.shardingsphere.driver.jdbc.core.resultset; -import java.sql.Array; -import java.time.LocalDateTime; -import java.time.ZoneId; import org.apache.shardingsphere.driver.jdbc.core.connection.ShardingSphereConnection; -import org.apache.shardingsphere.driver.jdbc.core.context.RuntimeContext; import org.apache.shardingsphere.driver.jdbc.core.statement.ShardingSphereStatement; import org.apache.shardingsphere.infra.config.properties.ConfigurationProperties; import org.apache.shardingsphere.infra.executor.sql.context.ExecutionContext; @@ -37,6 +33,7 @@ import java.math.BigDecimal; import java.net.MalformedURLException; import java.net.URL; +import java.sql.Array; import java.sql.Blob; import java.sql.Clob; import java.sql.Date; @@ -46,6 +43,8 @@ import java.sql.SQLXML; import java.sql.Time; import java.sql.Timestamp; +import java.time.LocalDateTime; +import java.time.ZoneId; import java.util.Calendar; import java.util.Collections; import java.util.List; @@ -68,8 +67,6 @@ public final class ShardingSphereResultSetTest { @Before public void setUp() throws SQLException { mergeResultSet = mock(MergedResult.class); - RuntimeContext runtimeContext = mock(RuntimeContext.class); - when(runtimeContext.getProps()).thenReturn(new ConfigurationProperties(new Properties())); shardingSphereResultSet = new ShardingSphereResultSet(getResultSets(), mergeResultSet, getShardingSphereStatement(), createExecutionContext()); } diff --git a/shardingsphere-kernel/shardingsphere-kernel-context/pom.xml b/shardingsphere-kernel/shardingsphere-kernel-context/pom.xml index b2e7eaf02aa05..5b85390598375 100644 --- a/shardingsphere-kernel/shardingsphere-kernel-context/pom.xml +++ b/shardingsphere-kernel/shardingsphere-kernel-context/pom.xml @@ -41,7 +41,7 @@ org.apache.shardingsphere - shardingsphere-sql-parser-engine + shardingsphere-rdl-parser-engine ${project.version} diff --git a/shardingsphere-kernel/shardingsphere-kernel-context/src/main/java/org/apache/shardingsphere/kernel/context/SchemaContextsBuilder.java b/shardingsphere-kernel/shardingsphere-kernel-context/src/main/java/org/apache/shardingsphere/kernel/context/SchemaContextsBuilder.java index 06cede6cde070..7876b9445e725 100644 --- a/shardingsphere-kernel/shardingsphere-kernel-context/src/main/java/org/apache/shardingsphere/kernel/context/SchemaContextsBuilder.java +++ b/shardingsphere-kernel/shardingsphere-kernel-context/src/main/java/org/apache/shardingsphere/kernel/context/SchemaContextsBuilder.java @@ -37,7 +37,7 @@ import org.apache.shardingsphere.kernel.context.runtime.RuntimeContext; import org.apache.shardingsphere.kernel.context.schema.DataSourceParameter; import org.apache.shardingsphere.kernel.context.schema.ShardingSphereSchema; -import org.apache.shardingsphere.sql.parser.engine.SQLParserEngineFactory; +import org.apache.shardingsphere.rdl.parser.engine.ShardingSphereSQLParserEngineFactory; import org.apache.shardingsphere.transaction.ShardingTransactionManagerEngine; import javax.sql.DataSource; @@ -110,7 +110,7 @@ public SchemaContexts build() throws SQLException { private SchemaContext createSchemaContext(final String schemaName) throws SQLException { Map dataSources = this.dataSources.get(schemaName); RuntimeContext runtimeContext = new RuntimeContext(createCachedDatabaseMetaData(dataSources), - executorKernel, SQLParserEngineFactory.getSQLParserEngine(DatabaseTypes.getTrunkDatabaseTypeName(databaseType)), createShardingTransactionManagerEngine(dataSources)); + executorKernel, ShardingSphereSQLParserEngineFactory.getSQLParserEngine(DatabaseTypes.getTrunkDatabaseTypeName(databaseType)), createShardingTransactionManagerEngine(dataSources)); return new SchemaContext(schemaName, createShardingSphereSchema(schemaName), runtimeContext); } diff --git a/shardingsphere-kernel/shardingsphere-kernel-context/src/main/java/org/apache/shardingsphere/kernel/context/runtime/RuntimeContext.java b/shardingsphere-kernel/shardingsphere-kernel-context/src/main/java/org/apache/shardingsphere/kernel/context/runtime/RuntimeContext.java index 2a4020dd8f8d4..5e849dec1ee7a 100644 --- a/shardingsphere-kernel/shardingsphere-kernel-context/src/main/java/org/apache/shardingsphere/kernel/context/runtime/RuntimeContext.java +++ b/shardingsphere-kernel/shardingsphere-kernel-context/src/main/java/org/apache/shardingsphere/kernel/context/runtime/RuntimeContext.java @@ -20,7 +20,7 @@ import lombok.Getter; import lombok.RequiredArgsConstructor; import org.apache.shardingsphere.infra.executor.kernel.ExecutorKernel; -import org.apache.shardingsphere.sql.parser.engine.SQLParserEngine; +import org.apache.shardingsphere.rdl.parser.engine.ShardingSphereSQLParserEngine; import org.apache.shardingsphere.transaction.ShardingTransactionManagerEngine; /** @@ -34,7 +34,7 @@ public final class RuntimeContext { private final ExecutorKernel executorKernel; - private final SQLParserEngine sqlParserEngine; + private final ShardingSphereSQLParserEngine sqlParserEngine; private final ShardingTransactionManagerEngine transactionManagerEngine; } diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/DatabaseCommunicationEngineFactoryTest.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/DatabaseCommunicationEngineFactoryTest.java index 0d9c36ad2d273..2c37329324f9e 100644 --- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/DatabaseCommunicationEngineFactoryTest.java +++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/DatabaseCommunicationEngineFactoryTest.java @@ -23,7 +23,7 @@ import org.apache.shardingsphere.proxy.backend.communication.DatabaseCommunicationEngine; import org.apache.shardingsphere.proxy.backend.communication.DatabaseCommunicationEngineFactory; import org.apache.shardingsphere.proxy.backend.communication.jdbc.connection.BackendConnection; -import org.apache.shardingsphere.sql.parser.engine.SQLParserEngine; +import org.apache.shardingsphere.rdl.parser.engine.ShardingSphereSQLParserEngine; import org.apache.shardingsphere.sql.parser.sql.statement.SQLStatement; import org.junit.Before; import org.junit.Test; @@ -47,7 +47,7 @@ public void setUp() { schemaContext = mock(SchemaContext.class); ShardingSphereSchema schema = mock(ShardingSphereSchema.class); RuntimeContext runtimeContext = mock(RuntimeContext.class); - SQLParserEngine sqlParserEngine = mock(SQLParserEngine.class); + ShardingSphereSQLParserEngine sqlParserEngine = mock(ShardingSphereSQLParserEngine.class); when(sqlParserEngine.parse(anyString(), anyBoolean())).thenReturn(mock(SQLStatement.class)); when(runtimeContext.getSqlParserEngine()).thenReturn(sqlParserEngine); when(schema.getRules()).thenReturn(Collections.emptyList()); diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/sctl/explain/ShardingCTLExplainBackendHandlerTest.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/sctl/explain/ShardingCTLExplainBackendHandlerTest.java index 8194570c20ea9..109e7eb5f0df7 100644 --- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/sctl/explain/ShardingCTLExplainBackendHandlerTest.java +++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/sctl/explain/ShardingCTLExplainBackendHandlerTest.java @@ -5,6 +5,7 @@ import org.apache.shardingsphere.kernel.context.runtime.RuntimeContext; import org.apache.shardingsphere.kernel.context.schema.ShardingSphereSchema; import org.apache.shardingsphere.proxy.backend.communication.jdbc.connection.BackendConnection; +import org.apache.shardingsphere.rdl.parser.engine.ShardingSphereSQLParserEngine; import org.apache.shardingsphere.sql.parser.engine.SQLParserEngine; import org.junit.Before; import org.junit.Test; @@ -47,7 +48,7 @@ public void setUp() { } private SchemaContext createSchemaContext() { - RuntimeContext runtimeContext = new RuntimeContext(null, null, new SQLParserEngine("MySQL"), null); + RuntimeContext runtimeContext = new RuntimeContext(null, null, new ShardingSphereSQLParserEngine(new SQLParserEngine("MySQL")), null); ShardingSphereSchema schema = new ShardingSphereSchema(new MySQLDatabaseType(), Collections.emptyList(), Collections.emptyList(), Collections.singletonMap("ds0", mock(DataSource.class)), null); return new SchemaContext("c1", schema, runtimeContext); diff --git a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/command/MySQLCommandExecutorFactoryTest.java b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/command/MySQLCommandExecutorFactoryTest.java index ae5941994dd3e..b1eddcc0dd7ca 100644 --- a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/command/MySQLCommandExecutorFactoryTest.java +++ b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/command/MySQLCommandExecutorFactoryTest.java @@ -39,7 +39,7 @@ import org.apache.shardingsphere.proxy.frontend.mysql.command.query.binary.reset.MySQLComStmtResetExecutor; import org.apache.shardingsphere.proxy.frontend.mysql.command.query.text.fieldlist.MySQLComFieldListPacketExecutor; import org.apache.shardingsphere.proxy.frontend.mysql.command.query.text.query.MySQLComQueryPacketExecutor; -import org.apache.shardingsphere.sql.parser.engine.SQLParserEngine; +import org.apache.shardingsphere.rdl.parser.engine.ShardingSphereSQLParserEngine; import org.apache.shardingsphere.sql.parser.sql.statement.SQLStatement; import org.junit.Test; @@ -60,7 +60,7 @@ public void assertNewInstance() { SchemaContext schemaContext = mock(SchemaContext.class); ShardingSphereSchema schema = mock(ShardingSphereSchema.class); RuntimeContext runtimeContext = mock(RuntimeContext.class); - SQLParserEngine sqlParserEngine = mock(SQLParserEngine.class); + ShardingSphereSQLParserEngine sqlParserEngine = mock(ShardingSphereSQLParserEngine.class); when(sqlParserEngine.parse(anyString(), anyBoolean())).thenReturn(mock(SQLStatement.class)); when(runtimeContext.getSqlParserEngine()).thenReturn(sqlParserEngine); when(schema.getRules()).thenReturn(Collections.emptyList()); diff --git a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/command/query/binary/execute/MySQLComStmtExecuteExecutorTest.java b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/command/query/binary/execute/MySQLComStmtExecuteExecutorTest.java index ed50f9abc2545..cdcf0b165d784 100644 --- a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/command/query/binary/execute/MySQLComStmtExecuteExecutorTest.java +++ b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/command/query/binary/execute/MySQLComStmtExecuteExecutorTest.java @@ -27,7 +27,7 @@ import org.apache.shardingsphere.proxy.backend.response.error.ErrorResponse; import org.apache.shardingsphere.proxy.backend.response.query.QueryResponse; import org.apache.shardingsphere.proxy.backend.response.update.UpdateResponse; -import org.apache.shardingsphere.sql.parser.engine.SQLParserEngine; +import org.apache.shardingsphere.rdl.parser.engine.ShardingSphereSQLParserEngine; import org.hamcrest.Matchers; import org.junit.Test; import org.junit.runner.RunWith; @@ -57,7 +57,7 @@ public void assertIsErrorResponse() { BackendConnection backendConnection = mock(BackendConnection.class); SchemaContext schema = mock(SchemaContext.class); RuntimeContext runtimeContext = mock(RuntimeContext.class); - SQLParserEngine sqlParserEngine = mock(SQLParserEngine.class); + ShardingSphereSQLParserEngine sqlParserEngine = mock(ShardingSphereSQLParserEngine.class); when(runtimeContext.getSqlParserEngine()).thenReturn(sqlParserEngine); when(schema.getRuntimeContext()).thenReturn(runtimeContext); when(backendConnection.getSchema()).thenReturn(schema); @@ -75,7 +75,7 @@ public void assertIsUpdateResponse() { BackendConnection backendConnection = mock(BackendConnection.class); SchemaContext schema = mock(SchemaContext.class); RuntimeContext runtimeContext = mock(RuntimeContext.class); - SQLParserEngine sqlParserEngine = mock(SQLParserEngine.class); + ShardingSphereSQLParserEngine sqlParserEngine = mock(ShardingSphereSQLParserEngine.class); when(runtimeContext.getSqlParserEngine()).thenReturn(sqlParserEngine); when(schema.getRuntimeContext()).thenReturn(runtimeContext); when(backendConnection.getSchema()).thenReturn(schema); @@ -92,7 +92,7 @@ public void assertIsQuery() { BackendConnection backendConnection = mock(BackendConnection.class); SchemaContext schema = mock(SchemaContext.class); RuntimeContext runtimeContext = mock(RuntimeContext.class); - SQLParserEngine sqlParserEngine = mock(SQLParserEngine.class); + ShardingSphereSQLParserEngine sqlParserEngine = mock(ShardingSphereSQLParserEngine.class); when(runtimeContext.getSqlParserEngine()).thenReturn(sqlParserEngine); when(schema.getRuntimeContext()).thenReturn(runtimeContext); when(backendConnection.getSchema()).thenReturn(schema);