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

panjuan 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 f9edb9e0231 Move pipeline dedicated code to pipeline module to keep 
YamlShardingSphereDataSourceFactory clear (#30155)
f9edb9e0231 is described below

commit f9edb9e0231925c19ae1693bed3f4e319adfc5b2
Author: Liang Zhang <[email protected]>
AuthorDate: Sat Feb 17 18:23:52 2024 +0800

    Move pipeline dedicated code to pipeline module to keep 
YamlShardingSphereDataSourceFactory clear (#30155)
---
 .../yaml/YamlShardingSphereDataSourceFactory.java  | 27 -------------------
 .../ShardingSpherePipelineDataSourceCreator.java   | 30 ++++++++++++++++++++--
 .../pipeline/core/util/PipelineContextUtils.java   | 24 ++++++++++++-----
 3 files changed, 45 insertions(+), 36 deletions(-)

diff --git 
a/jdbc/core/src/main/java/org/apache/shardingsphere/driver/api/yaml/YamlShardingSphereDataSourceFactory.java
 
b/jdbc/core/src/main/java/org/apache/shardingsphere/driver/api/yaml/YamlShardingSphereDataSourceFactory.java
index 7da615d2c03..ffa8508598b 100644
--- 
a/jdbc/core/src/main/java/org/apache/shardingsphere/driver/api/yaml/YamlShardingSphereDataSourceFactory.java
+++ 
b/jdbc/core/src/main/java/org/apache/shardingsphere/driver/api/yaml/YamlShardingSphereDataSourceFactory.java
@@ -24,9 +24,7 @@ import 
org.apache.shardingsphere.driver.api.ShardingSphereDataSourceFactory;
 import org.apache.shardingsphere.infra.config.mode.ModeConfiguration;
 import org.apache.shardingsphere.infra.config.rule.RuleConfiguration;
 import org.apache.shardingsphere.infra.database.core.DefaultDatabase;
-import 
org.apache.shardingsphere.infra.datasource.pool.destroyer.DataSourcePoolDestroyer;
 import org.apache.shardingsphere.infra.util.yaml.YamlEngine;
-import org.apache.shardingsphere.infra.yaml.config.pojo.YamlRootConfiguration;
 import 
org.apache.shardingsphere.infra.yaml.config.pojo.rule.YamlGlobalRuleConfiguration;
 import 
org.apache.shardingsphere.infra.yaml.config.swapper.mode.YamlModeConfigurationSwapper;
 import 
org.apache.shardingsphere.infra.yaml.config.swapper.resource.YamlDataSourceConfigurationSwapper;
@@ -51,31 +49,6 @@ public final class YamlShardingSphereDataSourceFactory {
     
     private static final YamlDataSourceConfigurationSwapper 
DATA_SOURCE_SWAPPER = new YamlDataSourceConfigurationSwapper();
     
-    /**
-     * Create ShardingSphere data source without cache.
-     * 
-     * @param rootConfig rule configurations
-     * @return ShardingSphere data source
-     * @throws SQLException SQL exception
-     */
-    public static DataSource createDataSourceWithoutCache(final 
YamlRootConfiguration rootConfig) throws SQLException {
-        Map<String, DataSource> dataSourceMap = 
DATA_SOURCE_SWAPPER.swapToDataSources(rootConfig.getDataSources(), false);
-        try {
-            return createDataSource(dataSourceMap, rootConfig);
-            // CHECKSTYLE:OFF
-        } catch (final SQLException | RuntimeException ex) {
-            // CHECKSTYLE:ON
-            
dataSourceMap.values().stream().map(DataSourcePoolDestroyer::new).forEach(DataSourcePoolDestroyer::asyncDestroy);
-            throw ex;
-        }
-    }
-    
-    private static DataSource createDataSource(final Map<String, DataSource> 
dataSourceMap, final YamlRootConfiguration rootConfig) throws SQLException {
-        ModeConfiguration modeConfig = null == rootConfig.getMode() ? null : 
new YamlModeConfigurationSwapper().swapToObject(rootConfig.getMode());
-        Collection<RuleConfiguration> ruleConfigs = 
RULE_CONFIG_SWAPPER.swapToRuleConfigurations(rootConfig.getRules());
-        return 
ShardingSphereDataSourceFactory.createDataSource(rootConfig.getDatabaseName(), 
modeConfig, dataSourceMap, ruleConfigs, rootConfig.getProps());
-    }
-    
     /**
      * Create ShardingSphere data source.
      * 
diff --git 
a/jdbc/core/src/main/java/org/apache/shardingsphere/driver/data/pipeline/datasource/creator/ShardingSpherePipelineDataSourceCreator.java
 
b/jdbc/core/src/main/java/org/apache/shardingsphere/driver/data/pipeline/datasource/creator/ShardingSpherePipelineDataSourceCreator.java
index 48036c99efe..ebb552b94bc 100644
--- 
a/jdbc/core/src/main/java/org/apache/shardingsphere/driver/data/pipeline/datasource/creator/ShardingSpherePipelineDataSourceCreator.java
+++ 
b/jdbc/core/src/main/java/org/apache/shardingsphere/driver/data/pipeline/datasource/creator/ShardingSpherePipelineDataSourceCreator.java
@@ -20,14 +20,20 @@ package 
org.apache.shardingsphere.driver.data.pipeline.datasource.creator;
 import 
org.apache.shardingsphere.authority.yaml.config.YamlAuthorityRuleConfiguration;
 import 
org.apache.shardingsphere.data.pipeline.api.type.ShardingSpherePipelineDataSourceConfiguration;
 import org.apache.shardingsphere.data.pipeline.spi.PipelineDataSourceCreator;
-import 
org.apache.shardingsphere.driver.api.yaml.YamlShardingSphereDataSourceFactory;
+import org.apache.shardingsphere.driver.api.ShardingSphereDataSourceFactory;
+import org.apache.shardingsphere.infra.config.mode.ModeConfiguration;
 import org.apache.shardingsphere.infra.config.props.ConfigurationPropertyKey;
 import 
org.apache.shardingsphere.infra.config.props.temporary.TemporaryConfigurationPropertyKey;
+import org.apache.shardingsphere.infra.config.rule.RuleConfiguration;
+import 
org.apache.shardingsphere.infra.datasource.pool.destroyer.DataSourcePoolDestroyer;
 import org.apache.shardingsphere.infra.util.yaml.YamlEngine;
 import org.apache.shardingsphere.infra.yaml.config.pojo.YamlRootConfiguration;
 import 
org.apache.shardingsphere.infra.yaml.config.pojo.algorithm.YamlAlgorithmConfiguration;
 import 
org.apache.shardingsphere.infra.yaml.config.pojo.mode.YamlModeConfiguration;
 import 
org.apache.shardingsphere.infra.yaml.config.pojo.mode.YamlPersistRepositoryConfiguration;
+import 
org.apache.shardingsphere.infra.yaml.config.swapper.mode.YamlModeConfigurationSwapper;
+import 
org.apache.shardingsphere.infra.yaml.config.swapper.resource.YamlDataSourceConfigurationSwapper;
+import 
org.apache.shardingsphere.infra.yaml.config.swapper.rule.YamlRuleConfigurationSwapperEngine;
 import 
org.apache.shardingsphere.mode.repository.standalone.jdbc.props.JDBCRepositoryPropertyKey;
 import 
org.apache.shardingsphere.sharding.yaml.config.YamlShardingRuleConfiguration;
 import 
org.apache.shardingsphere.sharding.yaml.swapper.ShardingRuleConfigurationConverter;
@@ -36,7 +42,9 @@ import 
org.apache.shardingsphere.single.yaml.config.pojo.YamlSingleRuleConfigura
 
 import javax.sql.DataSource;
 import java.sql.SQLException;
+import java.util.Collection;
 import java.util.Collections;
+import java.util.Map;
 import java.util.Optional;
 import java.util.Properties;
 import java.util.concurrent.atomic.AtomicInteger;
@@ -61,7 +69,7 @@ public final class ShardingSpherePipelineDataSourceCreator 
implements PipelineDa
             removeAuditStrategy(yamlShardingRuleConfig.get());
         }
         rootConfig.setMode(createStandaloneModeConfiguration());
-        return 
YamlShardingSphereDataSourceFactory.createDataSourceWithoutCache(rootConfig);
+        return createDataSourceWithoutCache(rootConfig);
     }
     
     private void removeAuthorityRule(final YamlRootConfiguration rootConfig) {
@@ -117,6 +125,24 @@ public final class ShardingSpherePipelineDataSourceCreator 
implements PipelineDa
         return result;
     }
     
+    private DataSource createDataSourceWithoutCache(final 
YamlRootConfiguration rootConfig) throws SQLException {
+        Map<String, DataSource> dataSourceMap = new 
YamlDataSourceConfigurationSwapper().swapToDataSources(rootConfig.getDataSources(),
 false);
+        try {
+            return createDataSource(dataSourceMap, rootConfig);
+            // CHECKSTYLE:OFF
+        } catch (final SQLException | RuntimeException ex) {
+            // CHECKSTYLE:ON
+            
dataSourceMap.values().stream().map(DataSourcePoolDestroyer::new).forEach(DataSourcePoolDestroyer::asyncDestroy);
+            throw ex;
+        }
+    }
+    
+    private DataSource createDataSource(final Map<String, DataSource> 
dataSourceMap, final YamlRootConfiguration rootConfig) throws SQLException {
+        ModeConfiguration modeConfig = null == rootConfig.getMode() ? null : 
new YamlModeConfigurationSwapper().swapToObject(rootConfig.getMode());
+        Collection<RuleConfiguration> ruleConfigs = new 
YamlRuleConfigurationSwapperEngine().swapToRuleConfigurations(rootConfig.getRules());
+        return 
ShardingSphereDataSourceFactory.createDataSource(rootConfig.getDatabaseName(), 
modeConfig, dataSourceMap, ruleConfigs, rootConfig.getProps());
+    }
+    
     @Override
     public String getType() {
         return ShardingSpherePipelineDataSourceConfiguration.TYPE;
diff --git 
a/test/it/pipeline/src/test/java/org/apache/shardingsphere/test/it/data/pipeline/core/util/PipelineContextUtils.java
 
b/test/it/pipeline/src/test/java/org/apache/shardingsphere/test/it/data/pipeline/core/util/PipelineContextUtils.java
index 62439b8d0eb..e4310509aca 100644
--- 
a/test/it/pipeline/src/test/java/org/apache/shardingsphere/test/it/data/pipeline/core/util/PipelineContextUtils.java
+++ 
b/test/it/pipeline/src/test/java/org/apache/shardingsphere/test/it/data/pipeline/core/util/PipelineContextUtils.java
@@ -45,9 +45,10 @@ import 
org.apache.shardingsphere.data.pipeline.scenario.migration.config.Migrati
 import 
org.apache.shardingsphere.data.pipeline.scenario.migration.config.MigrationTaskConfiguration;
 import 
org.apache.shardingsphere.data.pipeline.scenario.migration.context.MigrationJobItemContext;
 import 
org.apache.shardingsphere.data.pipeline.scenario.migration.ingest.dumper.MigrationIncrementalDumperContextCreator;
-import 
org.apache.shardingsphere.driver.api.yaml.YamlShardingSphereDataSourceFactory;
+import org.apache.shardingsphere.driver.api.ShardingSphereDataSourceFactory;
 import 
org.apache.shardingsphere.driver.jdbc.core.datasource.ShardingSphereDataSource;
 import org.apache.shardingsphere.infra.config.mode.ModeConfiguration;
+import org.apache.shardingsphere.infra.config.rule.RuleConfiguration;
 import 
org.apache.shardingsphere.infra.database.core.metadata.database.DialectDatabaseMetaData;
 import org.apache.shardingsphere.infra.database.core.type.DatabaseTypeRegistry;
 import org.apache.shardingsphere.infra.datanode.DataNode;
@@ -57,6 +58,8 @@ import 
org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSp
 import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
 import org.apache.shardingsphere.infra.yaml.config.pojo.YamlRootConfiguration;
 import 
org.apache.shardingsphere.infra.yaml.config.swapper.mode.YamlModeConfigurationSwapper;
+import 
org.apache.shardingsphere.infra.yaml.config.swapper.resource.YamlDataSourceConfigurationSwapper;
+import 
org.apache.shardingsphere.infra.yaml.config.swapper.rule.YamlRuleConfigurationSwapperEngine;
 import org.apache.shardingsphere.metadata.persist.MetaDataBasedPersistService;
 import org.apache.shardingsphere.metadata.persist.MetaDataPersistService;
 import org.apache.shardingsphere.metadata.persist.NewMetaDataPersistService;
@@ -68,6 +71,8 @@ import 
org.apache.shardingsphere.test.it.data.pipeline.core.fixture.EmbedTesting
 import org.apache.shardingsphere.test.util.ConfigurationFileUtils;
 import org.mockito.internal.configuration.plugins.Plugins;
 
+import javax.sql.DataSource;
+import java.sql.SQLException;
 import java.sql.Types;
 import java.util.Arrays;
 import java.util.Collection;
@@ -90,7 +95,6 @@ public final class PipelineContextUtils {
     /**
      * Mock mode configuration and context manager.
      */
-    @SneakyThrows
     public static void mockModeConfigAndContextManager() {
         EmbedTestingServer.start();
         PipelineContextKey contextKey = getContextKey();
@@ -101,8 +105,7 @@ public final class PipelineContextUtils {
                 
ConfigurationFileUtils.readFileAndIgnoreComments("config_sharding_sphere_jdbc_source.yaml"));
         YamlRootConfiguration rootConfig = (YamlRootConfiguration) 
pipelineDataSourceConfig.getDataSourceConfiguration();
         ModeConfiguration modeConfig = new 
YamlModeConfigurationSwapper().swapToObject(rootConfig.getMode());
-        ShardingSphereDataSource dataSource = (ShardingSphereDataSource) 
YamlShardingSphereDataSourceFactory.createDataSourceWithoutCache(rootConfig);
-        ContextManager contextManager = getContextManager(dataSource);
+        ContextManager contextManager = getContextManager(rootConfig);
         ClusterPersistRepository persistRepository = 
getClusterPersistRepository((ClusterPersistRepositoryConfiguration) 
modeConfig.getRepository());
         MetaDataBasedPersistService persistService = 
"Cluster".equals(modeConfig.getType()) ? new 
NewMetaDataPersistService(persistRepository) : new 
MetaDataPersistService(persistRepository);
         MetaDataContexts metaDataContexts = 
renewMetaDataContexts(contextManager.getMetaDataContexts(), persistService);
@@ -110,9 +113,16 @@ public final class PipelineContextUtils {
         PipelineContextManager.putContext(contextKey, pipelineContext);
     }
     
-    @SneakyThrows(ReflectiveOperationException.class)
-    private static ContextManager getContextManager(final 
ShardingSphereDataSource dataSource) {
-        return (ContextManager) 
Plugins.getMemberAccessor().get(ShardingSphereDataSource.class.getDeclaredField("contextManager"),
 dataSource);
+    @SneakyThrows({ReflectiveOperationException.class, SQLException.class})
+    private static ContextManager getContextManager(final 
YamlRootConfiguration rootConfig) {
+        return (ContextManager) 
Plugins.getMemberAccessor().get(ShardingSphereDataSource.class.getDeclaredField("contextManager"),
 createDataSource(rootConfig));
+    }
+    
+    private static DataSource createDataSource(final YamlRootConfiguration 
rootConfig) throws SQLException {
+        Map<String, DataSource> dataSourceMap = new 
YamlDataSourceConfigurationSwapper().swapToDataSources(rootConfig.getDataSources(),
 false);
+        ModeConfiguration modeConfig = new 
YamlModeConfigurationSwapper().swapToObject(rootConfig.getMode());
+        Collection<RuleConfiguration> ruleConfigs = new 
YamlRuleConfigurationSwapperEngine().swapToRuleConfigurations(rootConfig.getRules());
+        return 
ShardingSphereDataSourceFactory.createDataSource(rootConfig.getDatabaseName(), 
modeConfig, dataSourceMap, ruleConfigs, rootConfig.getProps());
     }
     
     private static ClusterPersistRepository getClusterPersistRepository(final 
ClusterPersistRepositoryConfiguration repositoryConfig) {

Reply via email to