You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by du...@apache.org on 2020/11/07 07:04:47 UTC

[shardingsphere] branch master updated: Static SchemaMetaDataLoader (#8063)

This is an automated email from the ASF dual-hosted git repository.

duanzhengqiang 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 ed2c62b  Static SchemaMetaDataLoader (#8063)
ed2c62b is described below

commit ed2c62b8284609a8faebf547cd19173a39b990a4
Author: Liang Zhang <te...@163.com>
AuthorDate: Sat Nov 7 15:04:26 2020 +0800

    Static SchemaMetaDataLoader (#8063)
---
 .../schema/loader/SchemaMetaDataLoader.java        | 41 ++++++++++++----------
 .../schema/loader/SchemaMetaDataLoaderTest.java    | 10 +++---
 .../context/schema/SchemaContextsBuilder.java      |  2 +-
 .../driver/executor/AbstractStatementExecutor.java |  3 +-
 .../jdbc/JDBCDatabaseCommunicationEngine.java      |  5 ++-
 5 files changed, 30 insertions(+), 31 deletions(-)

diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/schema/loader/SchemaMetaDataLoader.java b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/schema/loader/SchemaMetaDataLoader.java
index 343201b..87275b7 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/schema/loader/SchemaMetaDataLoader.java
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/schema/loader/SchemaMetaDataLoader.java
@@ -17,7 +17,8 @@
 
 package org.apache.shardingsphere.infra.metadata.schema.loader;
 
-import lombok.RequiredArgsConstructor;
+import lombok.AccessLevel;
+import lombok.NoArgsConstructor;
 import org.apache.shardingsphere.infra.config.properties.ConfigurationProperties;
 import org.apache.shardingsphere.infra.database.DefaultSchema;
 import org.apache.shardingsphere.infra.database.type.DatabaseType;
@@ -34,6 +35,7 @@ import org.apache.shardingsphere.infra.spi.ordered.OrderedSPIRegistry;
 import javax.sql.DataSource;
 import java.sql.SQLException;
 import java.util.Collection;
+import java.util.Collections;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.Map.Entry;
@@ -43,7 +45,7 @@ import java.util.TreeSet;
 /**
  * Schema meta data loader.
  */
-@RequiredArgsConstructor
+@NoArgsConstructor(access = AccessLevel.PRIVATE)
 public final class SchemaMetaDataLoader {
     
     static {
@@ -51,19 +53,19 @@ public final class SchemaMetaDataLoader {
         ShardingSphereServiceLoader.register(ShardingSphereMetaDataDecorator.class);
     }
     
-    private final Collection<ShardingSphereRule> rules;
-    
     /**
      * Load schema meta data.
      * 
      * @param databaseType database type
      * @param dataSourceMap data source map
+     * @param rules ShardingSphere rules
      * @param props configuration properties
      * @return schema meta data
      * @throws SQLException SQL exception
      */
     @SuppressWarnings({"unchecked", "rawtypes"})
-    public PhysicalSchemaMetaData load(final DatabaseType databaseType, final Map<String, DataSource> dataSourceMap, final ConfigurationProperties props) throws SQLException {
+    public static PhysicalSchemaMetaData load(final DatabaseType databaseType, final Map<String, DataSource> dataSourceMap, 
+                                       final Collection<ShardingSphereRule> rules, final ConfigurationProperties props) throws SQLException {
         Collection<String> excludedTableNames = new TreeSet<>(String.CASE_INSENSITIVE_ORDER);
         PhysicalSchemaMetaData result = new PhysicalSchemaMetaData();
         for (Entry<ShardingSphereRule, ShardingSphereMetaDataLoader> entry : OrderedSPIRegistry.getRegisteredServices(rules, ShardingSphereMetaDataLoader.class).entrySet()) {
@@ -74,7 +76,7 @@ public final class SchemaMetaDataLoader {
             }
             result.merge(schemaMetaData);
         }
-        decorate(result);
+        decorate(rules, result);
         return result;
     }
     
@@ -83,14 +85,14 @@ public final class SchemaMetaDataLoader {
      *
      * @param databaseType database type
      * @param dataSource data source
+     * @param rules ShardingSphere rules
      * @param props configuration properties
      * @return schema meta data
      * @throws SQLException SQL exception
      */
-    public PhysicalSchemaMetaData load(final DatabaseType databaseType, final DataSource dataSource, final ConfigurationProperties props) throws SQLException {
-        Map<String, DataSource> dataSourceMap = new HashMap<>(1, 1);
-        dataSourceMap.put(DefaultSchema.LOGIC_NAME, dataSource);
-        return load(databaseType, dataSourceMap, props);
+    public static PhysicalSchemaMetaData load(final DatabaseType databaseType, final DataSource dataSource, 
+                                       final Collection<ShardingSphereRule> rules, final ConfigurationProperties props) throws SQLException {
+        return load(databaseType, Collections.singletonMap(DefaultSchema.LOGIC_NAME, dataSource), rules, props);
     }
     
     /**
@@ -98,18 +100,19 @@ public final class SchemaMetaDataLoader {
      *
      * @param databaseType database type
      * @param dataSourceMap data source map
+     * @param rules ShardingSphere rules
      * @param tableName table name
      * @param props configuration properties
      * @return schema meta data
      * @throws SQLException SQL exception
      */
     @SuppressWarnings({"unchecked", "rawtypes"})
-    public Optional<PhysicalTableMetaData> load(final DatabaseType databaseType, final Map<String, DataSource> dataSourceMap,
-                                                final String tableName, final ConfigurationProperties props) throws SQLException {
+    public static Optional<PhysicalTableMetaData> load(final DatabaseType databaseType, final Map<String, DataSource> dataSourceMap,
+                                                final Collection<ShardingSphereRule> rules, final String tableName, final ConfigurationProperties props) throws SQLException {
         for (Entry<ShardingSphereRule, ShardingSphereMetaDataLoader> entry : OrderedSPIRegistry.getRegisteredServices(rules, ShardingSphereMetaDataLoader.class).entrySet()) {
             Optional<PhysicalTableMetaData> result = entry.getValue().load(databaseType, dataSourceMap, new DataNodes(rules), tableName, entry.getKey(), props);
             if (result.isPresent()) {
-                return Optional.of(decorate(tableName, result.get()));
+                return Optional.of(decorate(rules, tableName, result.get()));
             }
         }
         return Optional.empty();
@@ -120,19 +123,19 @@ public final class SchemaMetaDataLoader {
      *
      * @param databaseType database type
      * @param dataSource data source
+     * @param rules ShardingSphere rules
      * @param tableName table name
      * @param props configuration properties
      * @return schema meta data
      * @throws SQLException SQL exception
      */
-    public Optional<PhysicalTableMetaData> load(final DatabaseType databaseType, final DataSource dataSource, final String tableName, final ConfigurationProperties props) throws SQLException {
-        Map<String, DataSource> dataSourceMap = new HashMap<>(1, 1);
-        dataSourceMap.put(DefaultSchema.LOGIC_NAME, dataSource);
-        return load(databaseType, dataSourceMap, tableName, props);
+    public static Optional<PhysicalTableMetaData> load(final DatabaseType databaseType, final DataSource dataSource,
+                                                final Collection<ShardingSphereRule> rules, final String tableName, final ConfigurationProperties props) throws SQLException {
+        return load(databaseType, Collections.singletonMap(DefaultSchema.LOGIC_NAME, dataSource), rules, tableName, props);
     }
     
     @SuppressWarnings({"unchecked", "rawtypes"})
-    private void decorate(final PhysicalSchemaMetaData schemaMetaData) {
+    private static void decorate(final Collection<ShardingSphereRule> rules, final PhysicalSchemaMetaData schemaMetaData) {
         Map<String, PhysicalTableMetaData> tableMetaDataMap = new HashMap<>(schemaMetaData.getAllTableNames().size(), 1);
         Map<ShardingSphereRule, ShardingSphereMetaDataDecorator> decorators = OrderedSPIRegistry.getRegisteredServices(rules, ShardingSphereMetaDataDecorator.class);
         for (String each : schemaMetaData.getAllTableNames()) {
@@ -144,7 +147,7 @@ public final class SchemaMetaDataLoader {
     }
     
     @SuppressWarnings({"unchecked", "rawtypes"})
-    private PhysicalTableMetaData decorate(final String tableName, final PhysicalTableMetaData tableMetaData) {
+    private static PhysicalTableMetaData decorate(final Collection<ShardingSphereRule> rules, final String tableName, final PhysicalTableMetaData tableMetaData) {
         Map<ShardingSphereRule, ShardingSphereMetaDataDecorator> decorators = OrderedSPIRegistry.getRegisteredServices(rules, ShardingSphereMetaDataDecorator.class);
         PhysicalTableMetaData result = null;
         for (Entry<ShardingSphereRule, ShardingSphereMetaDataDecorator> entry : decorators.entrySet()) {
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/metadata/schema/loader/SchemaMetaDataLoaderTest.java b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/metadata/schema/loader/SchemaMetaDataLoaderTest.java
index dca4d77..74a1652 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/metadata/schema/loader/SchemaMetaDataLoaderTest.java
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/metadata/schema/loader/SchemaMetaDataLoaderTest.java
@@ -49,16 +49,14 @@ public final class SchemaMetaDataLoaderTest {
     @Mock
     private ConfigurationProperties props;
     
-    private final SchemaMetaDataLoader loader = new SchemaMetaDataLoader(Arrays.asList(new CommonFixtureRule(), new DataNodeRoutedFixtureRule()));
-    
     @Test
     public void assertSyncLoadFullDatabases() throws SQLException {
-        assertPhysicalSchemaMetaData(loader.load(databaseType, dataSource, props));
+        assertPhysicalSchemaMetaData(SchemaMetaDataLoader.load(databaseType, dataSource, Arrays.asList(new CommonFixtureRule(), new DataNodeRoutedFixtureRule()), props));
     }
     
     @Test
     public void assertAsyncLoadFullDatabases() throws SQLException {
-        assertPhysicalSchemaMetaData(loader.load(databaseType, dataSource, props));
+        assertPhysicalSchemaMetaData(SchemaMetaDataLoader.load(databaseType, dataSource, Arrays.asList(new CommonFixtureRule(), new DataNodeRoutedFixtureRule()), props));
     }
     
     private void assertPhysicalSchemaMetaData(final PhysicalSchemaMetaData actual) {
@@ -73,11 +71,11 @@ public final class SchemaMetaDataLoaderTest {
     
     @Test
     public void assertLoadWithExistedTableName() throws SQLException {
-        assertTrue(loader.load(databaseType, dataSource, "data_node_routed_table_0", props).isPresent());
+        assertTrue(SchemaMetaDataLoader.load(databaseType, dataSource, Arrays.asList(new CommonFixtureRule(), new DataNodeRoutedFixtureRule()), "data_node_routed_table_0", props).isPresent());
     }
     
     @Test
     public void assertLoadWithNotExistedTableName() throws SQLException {
-        assertFalse(loader.load(databaseType, dataSource, "invalid_table", props).isPresent());
+        assertFalse(SchemaMetaDataLoader.load(databaseType, dataSource, Arrays.asList(new CommonFixtureRule(), new DataNodeRoutedFixtureRule()), "invalid_table", props).isPresent());
     }
 }
diff --git a/shardingsphere-infra/shardingsphere-infra-context/src/main/java/org/apache/shardingsphere/infra/context/schema/SchemaContextsBuilder.java b/shardingsphere-infra/shardingsphere-infra-context/src/main/java/org/apache/shardingsphere/infra/context/schema/SchemaContextsBuilder.java
index 1cfaa3e..994d918 100644
--- a/shardingsphere-infra/shardingsphere-infra-context/src/main/java/org/apache/shardingsphere/infra/context/schema/SchemaContextsBuilder.java
+++ b/shardingsphere-infra/shardingsphere-infra-context/src/main/java/org/apache/shardingsphere/infra/context/schema/SchemaContextsBuilder.java
@@ -135,7 +135,7 @@ public final class SchemaContextsBuilder {
     private ShardingSphereSchema buildSchema(final String schemaName, final Map<String, DataSource> dataSourceMap, final Collection<ShardingSphereRule> rules) throws SQLException {
         long start = System.currentTimeMillis();
         TableAddressingMetaData tableAddressingMetaData = TableAddressingMetaDataLoader.load(databaseType, dataSourceMap, rules);
-        PhysicalSchemaMetaData physicalSchemaMetaData = new SchemaMetaDataLoader(rules).load(databaseType, dataSourceMap, props);
+        PhysicalSchemaMetaData physicalSchemaMetaData = SchemaMetaDataLoader.load(databaseType, dataSourceMap, rules, props);
         ShardingSphereSchema result = new ShardingSphereSchema(tableAddressingMetaData, physicalSchemaMetaData);
         log.info("Load meta data for schema {} finished, cost {} milliseconds.", schemaName, System.currentTimeMillis() - start);
         return result;
diff --git a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/executor/AbstractStatementExecutor.java b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/executor/AbstractStatementExecutor.java
index fe26171..a0c0118 100644
--- a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/executor/AbstractStatementExecutor.java
+++ b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/executor/AbstractStatementExecutor.java
@@ -82,10 +82,9 @@ public abstract class AbstractStatementExecutor {
         }
         Optional<MetaDataRefreshStrategy> refreshStrategy = MetaDataRefreshStrategyFactory.newInstance(sqlStatement);
         if (refreshStrategy.isPresent()) {
-            SchemaMetaDataLoader loader = new SchemaMetaDataLoader(metaData.getRuleMetaData().getRules());
             Collection<String> routeDataSourceNames = routeUnits.stream().map(RouteUnit::getDataSourceMapper).map(RouteMapper::getLogicName).collect(Collectors.toList());
             refreshStrategy.get().refreshMetaData(metaData.getSchema(), schemaContexts.getDatabaseType(), routeDataSourceNames, 
-                    sqlStatement, tableName -> loader.load(schemaContexts.getDatabaseType(), dataSourceMap, tableName, schemaContexts.getProps()));
+                    sqlStatement, tableName -> SchemaMetaDataLoader.load(schemaContexts.getDatabaseType(), dataSourceMap, metaData.getRuleMetaData().getRules(), tableName, schemaContexts.getProps()));
             notifyPersistLogicMetaData(DefaultSchema.LOGIC_NAME, metaData.getSchema().getSchemaMetaData());
         }
     }
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/JDBCDatabaseCommunicationEngine.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/JDBCDatabaseCommunicationEngine.java
index f1fc12a..c2c3271 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/JDBCDatabaseCommunicationEngine.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/JDBCDatabaseCommunicationEngine.java
@@ -111,9 +111,8 @@ public final class JDBCDatabaseCommunicationEngine implements DatabaseCommunicat
     }
     
     private Optional<PhysicalTableMetaData> loadTableMetaData(final String tableName) throws SQLException {
-        SchemaMetaDataLoader loader = new SchemaMetaDataLoader(metaData.getRuleMetaData().getRules());
-        return loader.load(
-                ProxyContext.getInstance().getSchemaContexts().getDatabaseType(), metaData.getResource().getDataSources(), tableName, ProxyContext.getInstance().getSchemaContexts().getProps());
+        return SchemaMetaDataLoader.load(ProxyContext.getInstance().getSchemaContexts().getDatabaseType(), 
+                metaData.getResource().getDataSources(), metaData.getRuleMetaData().getRules(), tableName, ProxyContext.getInstance().getSchemaContexts().getProps());
     }
     
     private BackendResponse merge(final SQLStatementContext<?> sqlStatementContext) throws SQLException {