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

wuweijie 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 9e80e85  Add ContextManagerBuilder SPI  (#11888)
9e80e85 is described below

commit 9e80e8554b8434b3579617f86347d4e16de81822
Author: Liang Zhang <[email protected]>
AuthorDate: Wed Aug 18 21:03:57 2021 +0800

    Add ContextManagerBuilder SPI  (#11888)
    
    * Add ContextManagerBuilder SPI
    
    * Refactor BootstrapInitializer
    
    * Refactor BootstrapInitializer
    
    * Refactor BootstrapInitializer
---
 .../context/ClusterContextManagerBuilder.java      |   5 +
 ...ere.infra.context.manager.ContextManagerBuilder |  18 +++
 .../governance/core/mode/ClusterModeBuilder.java   |   2 +-
 .../infra/mode/impl/memory/MemoryModeBuilder.java  |   2 +-
 .../impl/standalone/StandaloneModeBuilder.java     |   2 +-
 .../context/manager/ContextManagerBuilder.java     |   3 +-
 .../manager/impl/MemoryContextManagerBuilder.java  |   5 +
 .../impl/StandaloneContextManagerBuilder.java      |   5 +
 ...ere.infra.context.manager.ContextManagerBuilder |  19 +++
 .../org/apache/shardingsphere/proxy/Bootstrap.java |  18 +--
 .../proxy/initializer/BootstrapInitializer.java    | 114 +++++++++++++++-
 .../initializer/BootstrapInitializerFactory.java   |  52 --------
 .../impl/AbstractBootstrapInitializer.java         | 143 ---------------------
 .../impl/ClusterBootstrapInitializer.java          |  37 ------
 .../impl/MemoryBootstrapInitializer.java           |  37 ------
 .../impl/StandaloneBootstrapInitializer.java       |  37 ------
 16 files changed, 173 insertions(+), 326 deletions(-)

diff --git 
a/shardingsphere-governance/shardingsphere-governance-context/src/main/java/org/apache/shardingsphere/governance/context/ClusterContextManagerBuilder.java
 
b/shardingsphere-governance/shardingsphere-governance-context/src/main/java/org/apache/shardingsphere/governance/context/ClusterContextManagerBuilder.java
index 3cbe11a..02b11f8 100644
--- 
a/shardingsphere-governance/shardingsphere-governance-context/src/main/java/org/apache/shardingsphere/governance/context/ClusterContextManagerBuilder.java
+++ 
b/shardingsphere-governance/shardingsphere-governance-context/src/main/java/org/apache/shardingsphere/governance/context/ClusterContextManagerBuilder.java
@@ -161,4 +161,9 @@ public final class ClusterContextManagerBuilder implements 
ContextManagerBuilder
         }
         return new TransactionContexts(engines);
     }
+    
+    @Override
+    public String getType() {
+        return "Cluster";
+    }
 }
diff --git 
a/shardingsphere-governance/shardingsphere-governance-context/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.context.manager.ContextManagerBuilder
 
b/shardingsphere-governance/shardingsphere-governance-context/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.context.manager.ContextManagerBuilder
new file mode 100644
index 0000000..da4727f
--- /dev/null
+++ 
b/shardingsphere-governance/shardingsphere-governance-context/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.context.manager.ContextManagerBuilder
@@ -0,0 +1,18 @@
+#
+# 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.
+#
+
+org.apache.shardingsphere.governance.context.ClusterContextManagerBuilder
diff --git 
a/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/mode/ClusterModeBuilder.java
 
b/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/mode/ClusterModeBuilder.java
index 88c1c1b..22ac24c 100644
--- 
a/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/mode/ClusterModeBuilder.java
+++ 
b/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/mode/ClusterModeBuilder.java
@@ -50,6 +50,6 @@ public final class ClusterModeBuilder implements ModeBuilder {
     
     @Override
     public String getType() {
-        return "CLUSTER";
+        return "Cluster";
     }
 }
diff --git 
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/mode/impl/memory/MemoryModeBuilder.java
 
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/mode/impl/memory/MemoryModeBuilder.java
index bceccd5..df2fae1 100644
--- 
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/mode/impl/memory/MemoryModeBuilder.java
+++ 
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/mode/impl/memory/MemoryModeBuilder.java
@@ -33,6 +33,6 @@ public final class MemoryModeBuilder implements ModeBuilder {
     
     @Override
     public String getType() {
-        return "MEMORY";
+        return "Memory";
     }
 }
diff --git 
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/mode/impl/standalone/StandaloneModeBuilder.java
 
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/mode/impl/standalone/StandaloneModeBuilder.java
index c183e6f..7efbfd8 100644
--- 
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/mode/impl/standalone/StandaloneModeBuilder.java
+++ 
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/mode/impl/standalone/StandaloneModeBuilder.java
@@ -45,6 +45,6 @@ public final class StandaloneModeBuilder implements 
ModeBuilder {
     
     @Override
     public String getType() {
-        return "STANDALONE";
+        return "Standalone";
     }
 }
diff --git 
a/shardingsphere-infra/shardingsphere-infra-context/src/main/java/org/apache/shardingsphere/infra/context/manager/ContextManagerBuilder.java
 
b/shardingsphere-infra/shardingsphere-infra-context/src/main/java/org/apache/shardingsphere/infra/context/manager/ContextManagerBuilder.java
index 0848edf..0d13838 100644
--- 
a/shardingsphere-infra/shardingsphere-infra-context/src/main/java/org/apache/shardingsphere/infra/context/manager/ContextManagerBuilder.java
+++ 
b/shardingsphere-infra/shardingsphere-infra-context/src/main/java/org/apache/shardingsphere/infra/context/manager/ContextManagerBuilder.java
@@ -19,6 +19,7 @@ package org.apache.shardingsphere.infra.context.manager;
 
 import org.apache.shardingsphere.infra.config.RuleConfiguration;
 import org.apache.shardingsphere.infra.mode.ShardingSphereMode;
+import org.apache.shardingsphere.infra.spi.typed.TypedSPI;
 
 import javax.sql.DataSource;
 import java.sql.SQLException;
@@ -29,7 +30,7 @@ import java.util.Properties;
 /**
  * Context manager builder.
  */
-public interface ContextManagerBuilder {
+public interface ContextManagerBuilder extends TypedSPI {
     
     /**
      * Build context manager.
diff --git 
a/shardingsphere-infra/shardingsphere-infra-context/src/main/java/org/apache/shardingsphere/infra/context/manager/impl/MemoryContextManagerBuilder.java
 
b/shardingsphere-infra/shardingsphere-infra-context/src/main/java/org/apache/shardingsphere/infra/context/manager/impl/MemoryContextManagerBuilder.java
index 8d48d29..8a4a45a 100644
--- 
a/shardingsphere-infra/shardingsphere-infra-context/src/main/java/org/apache/shardingsphere/infra/context/manager/impl/MemoryContextManagerBuilder.java
+++ 
b/shardingsphere-infra/shardingsphere-infra-context/src/main/java/org/apache/shardingsphere/infra/context/manager/impl/MemoryContextManagerBuilder.java
@@ -62,4 +62,9 @@ public final class MemoryContextManagerBuilder implements 
ContextManagerBuilder
         }
         return new TransactionContexts(engines);
     }
+    
+    @Override
+    public String getType() {
+        return "Memory";
+    }
 }
diff --git 
a/shardingsphere-infra/shardingsphere-infra-context/src/main/java/org/apache/shardingsphere/infra/context/manager/impl/StandaloneContextManagerBuilder.java
 
b/shardingsphere-infra/shardingsphere-infra-context/src/main/java/org/apache/shardingsphere/infra/context/manager/impl/StandaloneContextManagerBuilder.java
index 256271f..a4ce02a 100644
--- 
a/shardingsphere-infra/shardingsphere-infra-context/src/main/java/org/apache/shardingsphere/infra/context/manager/impl/StandaloneContextManagerBuilder.java
+++ 
b/shardingsphere-infra/shardingsphere-infra-context/src/main/java/org/apache/shardingsphere/infra/context/manager/impl/StandaloneContextManagerBuilder.java
@@ -158,4 +158,9 @@ public final class StandaloneContextManagerBuilder 
implements ContextManagerBuil
         }
         return new TransactionContexts(engines);
     }
+    
+    @Override
+    public String getType() {
+        return "Standalone";
+    }
 }
diff --git 
a/shardingsphere-infra/shardingsphere-infra-context/src/test/resources/META-INF/services/org.apache.shardingsphere.infra.context.manager.ContextManagerBuilder
 
b/shardingsphere-infra/shardingsphere-infra-context/src/test/resources/META-INF/services/org.apache.shardingsphere.infra.context.manager.ContextManagerBuilder
new file mode 100644
index 0000000..c182e74
--- /dev/null
+++ 
b/shardingsphere-infra/shardingsphere-infra-context/src/test/resources/META-INF/services/org.apache.shardingsphere.infra.context.manager.ContextManagerBuilder
@@ -0,0 +1,19 @@
+#
+# 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.
+#
+
+org.apache.shardingsphere.infra.context.manager.impl.MemoryContextManagerBuilder
+org.apache.shardingsphere.infra.context.manager.impl.StandaloneContextManagerBuilder
diff --git 
a/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/java/org/apache/shardingsphere/proxy/Bootstrap.java
 
b/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/java/org/apache/shardingsphere/proxy/Bootstrap.java
index 0faeab8..4fb1b44 100644
--- 
a/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/java/org/apache/shardingsphere/proxy/Bootstrap.java
+++ 
b/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/java/org/apache/shardingsphere/proxy/Bootstrap.java
@@ -19,20 +19,18 @@ package org.apache.shardingsphere.proxy;
 
 import lombok.AccessLevel;
 import lombok.NoArgsConstructor;
+import org.apache.shardingsphere.infra.mode.ShardingSphereMode;
 import org.apache.shardingsphere.infra.mode.builder.ModeBuilderEngine;
 import org.apache.shardingsphere.infra.mode.config.ModeConfiguration;
-import 
org.apache.shardingsphere.infra.mode.config.StandalonePersistRepositoryConfiguration;
 import 
org.apache.shardingsphere.infra.yaml.config.swapper.mode.ModeConfigurationYamlSwapper;
 import org.apache.shardingsphere.proxy.arguments.BootstrapArguments;
 import org.apache.shardingsphere.proxy.config.ProxyConfigurationLoader;
 import org.apache.shardingsphere.proxy.config.YamlProxyConfiguration;
 import org.apache.shardingsphere.proxy.frontend.ShardingSphereProxy;
 import org.apache.shardingsphere.proxy.initializer.BootstrapInitializer;
-import org.apache.shardingsphere.proxy.initializer.BootstrapInitializerFactory;
 
 import java.io.IOException;
 import java.sql.SQLException;
-import java.util.Properties;
 
 /**
  * ShardingSphere-Proxy Bootstrap.
@@ -50,19 +48,13 @@ public final class Bootstrap {
     public static void main(final String[] args) throws IOException, 
SQLException {
         BootstrapArguments bootstrapArgs = new BootstrapArguments(args);
         YamlProxyConfiguration yamlConfig = 
ProxyConfigurationLoader.load(bootstrapArgs.getConfigurationPath());
-        BootstrapInitializer initializer = 
createBootstrapInitializer(yamlConfig);
-        initializer.init(yamlConfig);
+        ShardingSphereMode mode = 
ModeBuilderEngine.build(getModeConfiguration(yamlConfig));
+        new BootstrapInitializer(mode).init(yamlConfig);
         new ShardingSphereProxy().start(bootstrapArgs.getPort());
     }
     
-    private static BootstrapInitializer createBootstrapInitializer(final 
YamlProxyConfiguration yamlConfig) {
-        ModeConfiguration modeConfig = getModeConfiguration(yamlConfig);
-        return 
BootstrapInitializerFactory.newInstance(ModeBuilderEngine.build(modeConfig), 
modeConfig.isOverwrite());
-    }
-    
     private static ModeConfiguration getModeConfiguration(final 
YamlProxyConfiguration yamlConfig) {
-        return null == yamlConfig.getServerConfiguration().getMode()
-                ? new ModeConfiguration("Standalone", new 
StandalonePersistRepositoryConfiguration("Local", new Properties()), true)
-                : new 
ModeConfigurationYamlSwapper().swapToObject(yamlConfig.getServerConfiguration().getMode());
+        return null == yamlConfig.getServerConfiguration().getMode() 
+                ? new ModeConfiguration("Memory", null, true) : new 
ModeConfigurationYamlSwapper().swapToObject(yamlConfig.getServerConfiguration().getMode());
     }
 }
diff --git 
a/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/java/org/apache/shardingsphere/proxy/initializer/BootstrapInitializer.java
 
b/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/java/org/apache/shardingsphere/proxy/initializer/BootstrapInitializer.java
index 521b4ed..00cf861 100644
--- 
a/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/java/org/apache/shardingsphere/proxy/initializer/BootstrapInitializer.java
+++ 
b/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/java/org/apache/shardingsphere/proxy/initializer/BootstrapInitializer.java
@@ -17,20 +17,128 @@
 
 package org.apache.shardingsphere.proxy.initializer;
 
+import lombok.RequiredArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.shardingsphere.db.protocol.mysql.constant.MySQLServerInfo;
+import 
org.apache.shardingsphere.db.protocol.postgresql.constant.PostgreSQLServerInfo;
+import 
org.apache.shardingsphere.infra.config.datasource.DataSourceConfiguration;
+import org.apache.shardingsphere.infra.config.datasource.DataSourceParameter;
+import org.apache.shardingsphere.infra.context.manager.ContextManager;
+import org.apache.shardingsphere.infra.context.manager.ContextManagerBuilder;
+import org.apache.shardingsphere.infra.mode.ShardingSphereMode;
+import org.apache.shardingsphere.infra.mode.config.ModeConfiguration;
+import org.apache.shardingsphere.infra.spi.ShardingSphereServiceLoader;
+import org.apache.shardingsphere.infra.spi.typed.TypedSPIRegistry;
+import 
org.apache.shardingsphere.infra.yaml.config.swapper.mode.ModeConfigurationYamlSwapper;
+import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
+import org.apache.shardingsphere.proxy.config.ProxyConfiguration;
 import org.apache.shardingsphere.proxy.config.YamlProxyConfiguration;
+import 
org.apache.shardingsphere.proxy.config.util.DataSourceParameterConverter;
+import 
org.apache.shardingsphere.proxy.config.yaml.swapper.YamlProxyConfigurationSwapper;
+import org.apache.shardingsphere.proxy.database.DatabaseServerInfo;
+import org.apache.shardingsphere.scaling.core.api.ScalingWorker;
+import org.apache.shardingsphere.scaling.core.config.ScalingContext;
+import org.apache.shardingsphere.scaling.core.config.ServerConfiguration;
 
+import javax.sql.DataSource;
 import java.sql.SQLException;
+import java.util.LinkedHashMap;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Optional;
+import java.util.Properties;
 
 /**
  * Bootstrap initializer.
  */
-public interface BootstrapInitializer {
+@RequiredArgsConstructor
+@Slf4j
+public final class BootstrapInitializer {
+    
+    static {
+        ShardingSphereServiceLoader.register(ContextManagerBuilder.class);
+    }
+    
+    private final ShardingSphereMode mode;
     
     /**
      * Initialize.
-     * 
+     *
      * @param yamlConfig YAML proxy configuration
      * @throws SQLException SQL exception
      */
-    void init(YamlProxyConfiguration yamlConfig) throws SQLException;
+    public void init(final YamlProxyConfiguration yamlConfig) throws 
SQLException {
+        ProxyConfiguration proxyConfig = new 
YamlProxyConfigurationSwapper().swap(yamlConfig);
+        ModeConfiguration modeConfig = null == 
yamlConfig.getServerConfiguration().getMode()
+                ? new ModeConfiguration("Memory", null, true) : new 
ModeConfigurationYamlSwapper().swapToObject(yamlConfig.getServerConfiguration().getMode());
+        ContextManager contextManager = 
TypedSPIRegistry.getRegisteredService(ContextManagerBuilder.class, 
modeConfig.getType(), new Properties()).build(
+                mode, getDataSourcesMap(proxyConfig.getSchemaDataSources()), 
proxyConfig.getSchemaRules(), proxyConfig.getGlobalRules(), 
proxyConfig.getProps(), modeConfig.isOverwrite());
+        ProxyContext.getInstance().init(contextManager);
+        setDatabaseServerInfo();
+        initScaling(yamlConfig, modeConfig);
+    }
+    
+    // TODO add DataSourceParameter param to ContextManagerBuilder to avoid 
re-build data source
+    private Map<String, Map<String, DataSource>> getDataSourcesMap(final 
Map<String, Map<String, DataSourceParameter>> dataSourceParametersMap) {
+        Map<String, Map<String, DataSource>> result = new 
LinkedHashMap<>(dataSourceParametersMap.size(), 1);
+        for (Entry<String, Map<String, DataSourceParameter>> entry : 
dataSourceParametersMap.entrySet()) {
+            result.put(entry.getKey(), 
getDataSourceMap(DataSourceParameterConverter.getDataSourceConfigurationMap(entry.getValue())));
+        }
+        return result;
+    }
+    
+    private Map<String, DataSource> getDataSourceMap(final Map<String, 
DataSourceConfiguration> dataSourceConfigMap) {
+        Map<String, DataSource> result = new 
LinkedHashMap<>(dataSourceConfigMap.size(), 1);
+        for (Entry<String, DataSourceConfiguration> entry : 
dataSourceConfigMap.entrySet()) {
+            result.put(entry.getKey(), entry.getValue().createDataSource());
+        }
+        return result;
+    }
+    
+    private void setDatabaseServerInfo() {
+        findBackendDataSource().ifPresent(dataSourceSample -> {
+            DatabaseServerInfo databaseServerInfo = new 
DatabaseServerInfo(dataSourceSample);
+            log.info(databaseServerInfo.toString());
+            switch (databaseServerInfo.getDatabaseName()) {
+                case "MySQL":
+                    
MySQLServerInfo.setServerVersion(databaseServerInfo.getDatabaseVersion());
+                    break;
+                case "PostgreSQL":
+                    
PostgreSQLServerInfo.setServerVersion(databaseServerInfo.getDatabaseVersion());
+                    break;
+                default:
+            }
+        });
+    }
+    
+    private Optional<DataSource> findBackendDataSource() {
+        for (String each : ProxyContext.getInstance().getAllSchemaNames()) {
+            return 
ProxyContext.getInstance().getMetaData(each).getResource().getDataSources().values().stream().findFirst();
+        }
+        return Optional.empty();
+    }
+    
+    private void initScaling(final YamlProxyConfiguration yamlConfig, final 
ModeConfiguration modeConfig) {
+        Optional<ServerConfiguration> scalingConfig = 
findScalingConfiguration(yamlConfig);
+        if (!scalingConfig.isPresent()) {
+            return;
+        }
+        if ("Cluster".equals(modeConfig.getType())) {
+            scalingConfig.get().setModeConfiguration(modeConfig);
+            ScalingContext.getInstance().init(scalingConfig.get());
+            ScalingWorker.init(); 
+        } else {
+            ScalingContext.getInstance().init(scalingConfig.get());
+        }
+    }
+    
+    private Optional<ServerConfiguration> findScalingConfiguration(final 
YamlProxyConfiguration yamlConfig) {
+        if (null == yamlConfig.getServerConfiguration().getScaling()) {
+            return Optional.empty();
+        }
+        ServerConfiguration result = new ServerConfiguration();
+        
result.setBlockQueueSize(yamlConfig.getServerConfiguration().getScaling().getBlockQueueSize());
+        
result.setWorkerThread(yamlConfig.getServerConfiguration().getScaling().getWorkerThread());
+        return Optional.of(result);
+    }
 }
diff --git 
a/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/java/org/apache/shardingsphere/proxy/initializer/BootstrapInitializerFactory.java
 
b/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/java/org/apache/shardingsphere/proxy/initializer/BootstrapInitializerFactory.java
deleted file mode 100644
index 1fac766..0000000
--- 
a/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/java/org/apache/shardingsphere/proxy/initializer/BootstrapInitializerFactory.java
+++ /dev/null
@@ -1,52 +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.proxy.initializer;
-
-import lombok.AccessLevel;
-import lombok.NoArgsConstructor;
-import org.apache.shardingsphere.governance.core.mode.ClusterMode;
-import org.apache.shardingsphere.infra.mode.ShardingSphereMode;
-import org.apache.shardingsphere.infra.mode.impl.standalone.StandaloneMode;
-import 
org.apache.shardingsphere.proxy.initializer.impl.ClusterBootstrapInitializer;
-import 
org.apache.shardingsphere.proxy.initializer.impl.MemoryBootstrapInitializer;
-import 
org.apache.shardingsphere.proxy.initializer.impl.StandaloneBootstrapInitializer;
-
-/**
- * Bootstrap initializer factory.
- */
-@NoArgsConstructor(access = AccessLevel.PRIVATE)
-public final class BootstrapInitializerFactory {
-    
-    /**
-     * New instance.
-     * 
-     * @param mode ShardingSphere mode
-     * @param overwrite is overwrite to persist repository
-     * @return bootstrap initializer
-     */
-    public static BootstrapInitializer newInstance(final ShardingSphereMode 
mode, final boolean overwrite) {
-        // TODO split to pluggable SPI
-        if (mode instanceof StandaloneMode) {
-            return new StandaloneBootstrapInitializer(mode);
-        }
-        if (mode instanceof ClusterMode) {
-            return new ClusterBootstrapInitializer(mode);
-        }
-        return new MemoryBootstrapInitializer(mode);
-    }
-}
diff --git 
a/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/java/org/apache/shardingsphere/proxy/initializer/impl/AbstractBootstrapInitializer.java
 
b/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/java/org/apache/shardingsphere/proxy/initializer/impl/AbstractBootstrapInitializer.java
deleted file mode 100644
index 1894fe8..0000000
--- 
a/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/java/org/apache/shardingsphere/proxy/initializer/impl/AbstractBootstrapInitializer.java
+++ /dev/null
@@ -1,143 +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.proxy.initializer.impl;
-
-import lombok.Getter;
-import lombok.extern.slf4j.Slf4j;
-import org.apache.shardingsphere.db.protocol.mysql.constant.MySQLServerInfo;
-import 
org.apache.shardingsphere.db.protocol.postgresql.constant.PostgreSQLServerInfo;
-import 
org.apache.shardingsphere.infra.config.datasource.DataSourceConfiguration;
-import org.apache.shardingsphere.infra.config.datasource.DataSourceParameter;
-import org.apache.shardingsphere.infra.context.manager.ContextManager;
-import org.apache.shardingsphere.infra.context.manager.ContextManagerBuilder;
-import org.apache.shardingsphere.infra.mode.ShardingSphereMode;
-import org.apache.shardingsphere.infra.mode.config.ModeConfiguration;
-import org.apache.shardingsphere.infra.persist.DistMetaDataPersistService;
-import 
org.apache.shardingsphere.infra.yaml.config.swapper.mode.ModeConfigurationYamlSwapper;
-import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
-import org.apache.shardingsphere.proxy.config.ProxyConfiguration;
-import org.apache.shardingsphere.proxy.config.YamlProxyConfiguration;
-import 
org.apache.shardingsphere.proxy.config.util.DataSourceParameterConverter;
-import 
org.apache.shardingsphere.proxy.config.yaml.swapper.YamlProxyConfigurationSwapper;
-import org.apache.shardingsphere.proxy.database.DatabaseServerInfo;
-import org.apache.shardingsphere.proxy.initializer.BootstrapInitializer;
-import org.apache.shardingsphere.scaling.core.api.ScalingWorker;
-import org.apache.shardingsphere.scaling.core.config.ScalingContext;
-import org.apache.shardingsphere.scaling.core.config.ServerConfiguration;
-
-import javax.sql.DataSource;
-import java.sql.SQLException;
-import java.util.LinkedHashMap;
-import java.util.Map;
-import java.util.Map.Entry;
-import java.util.Optional;
-
-/**
- * Abstract bootstrap initializer.
- */
-@Slf4j
-public abstract class AbstractBootstrapInitializer implements 
BootstrapInitializer {
-    
-    private final ShardingSphereMode mode;
-    
-    @Getter
-    private final DistMetaDataPersistService distMetaDataPersistService;
-    
-    public AbstractBootstrapInitializer(final ShardingSphereMode mode) {
-        this.mode = mode;
-        distMetaDataPersistService = mode.getPersistRepository().isPresent() ? 
new DistMetaDataPersistService(mode.getPersistRepository().get()) : null;
-    }
-    
-    @Override
-    public final void init(final YamlProxyConfiguration yamlConfig) throws 
SQLException {
-        ProxyConfiguration proxyConfig = new 
YamlProxyConfigurationSwapper().swap(yamlConfig);
-        ModeConfiguration modeConfig = null == 
yamlConfig.getServerConfiguration().getMode()
-                ? new ModeConfiguration("Memory", null, true) : new 
ModeConfigurationYamlSwapper().swapToObject(yamlConfig.getServerConfiguration().getMode());
-        ContextManager contextManager = createContextManagerBuilder().build(
-                mode, getDataSourcesMap(proxyConfig.getSchemaDataSources()), 
proxyConfig.getSchemaRules(), proxyConfig.getGlobalRules(), 
proxyConfig.getProps(), modeConfig.isOverwrite());
-        ProxyContext.getInstance().init(contextManager);
-        setDatabaseServerInfo();
-        initScaling(yamlConfig, modeConfig);
-    }
-    
-    protected abstract ContextManagerBuilder createContextManagerBuilder();
-    
-    // TODO add DataSourceParameter param to ContextManagerBuilder to avoid 
re-build data source
-    private Map<String, Map<String, DataSource>> getDataSourcesMap(final 
Map<String, Map<String, DataSourceParameter>> dataSourceParametersMap) {
-        Map<String, Map<String, DataSource>> result = new 
LinkedHashMap<>(dataSourceParametersMap.size(), 1);
-        for (Entry<String, Map<String, DataSourceParameter>> entry : 
dataSourceParametersMap.entrySet()) {
-            result.put(entry.getKey(), 
getDataSourceMap(DataSourceParameterConverter.getDataSourceConfigurationMap(entry.getValue())));
-        }
-        return result;
-    }
-    
-    private Map<String, DataSource> getDataSourceMap(final Map<String, 
DataSourceConfiguration> dataSourceConfigMap) {
-        Map<String, DataSource> result = new 
LinkedHashMap<>(dataSourceConfigMap.size(), 1);
-        for (Entry<String, DataSourceConfiguration> entry : 
dataSourceConfigMap.entrySet()) {
-            result.put(entry.getKey(), entry.getValue().createDataSource());
-        }
-        return result;
-    }
-    
-    private void setDatabaseServerInfo() {
-        findBackendDataSource().ifPresent(dataSourceSample -> {
-            DatabaseServerInfo databaseServerInfo = new 
DatabaseServerInfo(dataSourceSample);
-            log.info(databaseServerInfo.toString());
-            switch (databaseServerInfo.getDatabaseName()) {
-                case "MySQL":
-                    
MySQLServerInfo.setServerVersion(databaseServerInfo.getDatabaseVersion());
-                    break;
-                case "PostgreSQL":
-                    
PostgreSQLServerInfo.setServerVersion(databaseServerInfo.getDatabaseVersion());
-                    break;
-                default:
-            }
-        });
-    }
-    
-    private Optional<DataSource> findBackendDataSource() {
-        for (String each : ProxyContext.getInstance().getAllSchemaNames()) {
-            return 
ProxyContext.getInstance().getMetaData(each).getResource().getDataSources().values().stream().findFirst();
-        }
-        return Optional.empty();
-    }
-    
-    private void initScaling(final YamlProxyConfiguration yamlConfig, final 
ModeConfiguration modeConfig) {
-        Optional<ServerConfiguration> scalingConfig = 
findScalingConfiguration(yamlConfig);
-        if (!scalingConfig.isPresent()) {
-            return;
-        }
-        if ("Cluster".equals(modeConfig.getType())) {
-            scalingConfig.get().setModeConfiguration(modeConfig);
-            ScalingContext.getInstance().init(scalingConfig.get());
-            ScalingWorker.init(); 
-        } else {
-            ScalingContext.getInstance().init(scalingConfig.get());
-        }
-    }
-    
-    private Optional<ServerConfiguration> findScalingConfiguration(final 
YamlProxyConfiguration yamlConfig) {
-        if (null == yamlConfig.getServerConfiguration().getScaling()) {
-            return Optional.empty();
-        }
-        ServerConfiguration result = new ServerConfiguration();
-        
result.setBlockQueueSize(yamlConfig.getServerConfiguration().getScaling().getBlockQueueSize());
-        
result.setWorkerThread(yamlConfig.getServerConfiguration().getScaling().getWorkerThread());
-        return Optional.of(result);
-    }
-}
diff --git 
a/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/java/org/apache/shardingsphere/proxy/initializer/impl/ClusterBootstrapInitializer.java
 
b/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/java/org/apache/shardingsphere/proxy/initializer/impl/ClusterBootstrapInitializer.java
deleted file mode 100644
index b02b035..0000000
--- 
a/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/java/org/apache/shardingsphere/proxy/initializer/impl/ClusterBootstrapInitializer.java
+++ /dev/null
@@ -1,37 +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.proxy.initializer.impl;
-
-import 
org.apache.shardingsphere.governance.context.ClusterContextManagerBuilder;
-import org.apache.shardingsphere.infra.context.manager.ContextManagerBuilder;
-import org.apache.shardingsphere.infra.mode.ShardingSphereMode;
-
-/**
- * Cluster bootstrap initializer.
- */
-public final class ClusterBootstrapInitializer extends 
AbstractBootstrapInitializer {
-    
-    public ClusterBootstrapInitializer(final ShardingSphereMode mode) {
-        super(mode);
-    }
-    
-    @Override
-    protected ContextManagerBuilder createContextManagerBuilder() {
-        return new ClusterContextManagerBuilder();
-    }
-}
diff --git 
a/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/java/org/apache/shardingsphere/proxy/initializer/impl/MemoryBootstrapInitializer.java
 
b/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/java/org/apache/shardingsphere/proxy/initializer/impl/MemoryBootstrapInitializer.java
deleted file mode 100644
index 268c8c3..0000000
--- 
a/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/java/org/apache/shardingsphere/proxy/initializer/impl/MemoryBootstrapInitializer.java
+++ /dev/null
@@ -1,37 +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.proxy.initializer.impl;
-
-import org.apache.shardingsphere.infra.context.manager.ContextManagerBuilder;
-import 
org.apache.shardingsphere.infra.context.manager.impl.MemoryContextManagerBuilder;
-import org.apache.shardingsphere.infra.mode.ShardingSphereMode;
-
-/**
- * Memory bootstrap initializer.
- */
-public final class MemoryBootstrapInitializer extends 
AbstractBootstrapInitializer {
-    
-    public MemoryBootstrapInitializer(final ShardingSphereMode mode) {
-        super(mode);
-    }
-    
-    @Override
-    protected ContextManagerBuilder createContextManagerBuilder() {
-        return new MemoryContextManagerBuilder();
-    }
-}
diff --git 
a/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/java/org/apache/shardingsphere/proxy/initializer/impl/StandaloneBootstrapInitializer.java
 
b/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/java/org/apache/shardingsphere/proxy/initializer/impl/StandaloneBootstrapInitializer.java
deleted file mode 100644
index ea533d7..0000000
--- 
a/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/java/org/apache/shardingsphere/proxy/initializer/impl/StandaloneBootstrapInitializer.java
+++ /dev/null
@@ -1,37 +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.proxy.initializer.impl;
-
-import org.apache.shardingsphere.infra.context.manager.ContextManagerBuilder;
-import 
org.apache.shardingsphere.infra.context.manager.impl.StandaloneContextManagerBuilder;
-import org.apache.shardingsphere.infra.mode.ShardingSphereMode;
-
-/**
- * Standalone bootstrap initializer.
- */
-public final class StandaloneBootstrapInitializer extends 
AbstractBootstrapInitializer {
-    
-    public StandaloneBootstrapInitializer(final ShardingSphereMode mode) {
-        super(mode);
-    }
-    
-    @Override
-    protected ContextManagerBuilder createContextManagerBuilder() {
-        return new StandaloneContextManagerBuilder();
-    }
-}

Reply via email to