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 84245e84b38 Support using disabled data source at startup(#20738)
(#20770)
84245e84b38 is described below
commit 84245e84b38146e20e49e474069cf4da27401f9b
Author: ZhangCheng <[email protected]>
AuthorDate: Thu Sep 15 11:43:32 2022 +0800
Support using disabled data source at startup(#20738) (#20770)
* Support using disabled data source at startup
* Simplified logic
* Add force logic and databaseName
* Remove ContextLifecycleListener and add initialized logic
* Merge from master
* Fix review problems
* Rename getEnabledDataSources method
* Fix bootstap param
* Fix merge
* Fix merge
* Fix merge
* Fix code review
* Simplify logic
* Simplify logic
* Move the force parameter position
* Extract param
* Add force check logic
---
.../src/main/resources/bin/start.sh | 19 ++-
.../checker/ShardingRuleStatementCheckerTest.java | 2 +-
...AlterShardingTableRuleStatementUpdaterTest.java | 2 +-
...reateShardingTableRuleStatementUpdaterTest.java | 2 +-
.../infra/database/type/DatabaseTypeEngine.java | 27 ++--
.../infra/datasource/state/DataSourceState.java | 71 ++++++++++
.../datasource/state/DataSourceStateManager.java | 150 +++++++++++++++++++++
.../state/exception/DataSourceStateException.java} | 27 ++--
.../metadata/database/ShardingSphereDatabase.java | 6 +-
.../database/ShardingSphereDatabasesFactory.java | 5 +-
.../database/resource/ShardingSphereResource.java | 5 +-
.../database/type/DatabaseTypeEngineTest.java | 4 +-
.../database/ShardingSphereDatabaseTest.java | 8 +-
.../resource/ShardingSphereResourceTest.java | 2 +-
.../sql/context/ExecutionContextBuilderTest.java | 4 +-
.../core/datasource/ShardingSphereDataSource.java | 2 +-
.../singletable/rule/SingleTableRule.java | 6 +-
.../mode/manager/ContextManager.java | 3 +-
.../manager/ContextManagerBuilderParameter.java | 2 +
.../ContextManagerBuilderParameterTest.java | 16 +--
.../mode/manager/ContextManagerTest.java | 2 +-
.../switcher/ResourceSwitchManagerTest.java | 2 +-
.../mode/metadata/MetaDataContextsFactoryTest.java | 2 +-
.../cluster/ClusterContextManagerBuilder.java | 36 ++++-
.../ClusterContextManagerCoordinatorTest.java | 4 +-
.../StandaloneContextManagerBuilderTextTest.java | 2 +-
.../DefaultDatabaseMetadataExecutorTest.java | 2 +-
.../admin/mysql/MySQLAdminExecutorCreatorTest.java | 4 +-
.../SelectInformationSchemataExecutorTest.java | 4 +-
.../executor/SelectDatabaseExecutorTest.java | 4 +-
.../executor/SelectTableExecutorTest.java | 2 +-
.../updatable/SetVariableBackendHandlerTest.java | 2 +-
.../distsql/rql/DataSourceQueryResultSetTest.java | 2 +-
.../rql/RulesUsedResourceQueryResultSetTest.java | 4 +-
.../rql/UnusedDataSourceQueryResultSetTest.java | 2 +-
.../org/apache/shardingsphere/proxy/Bootstrap.java | 2 +-
.../proxy/arguments/BootstrapArguments.java | 13 ++
.../proxy/initializer/BootstrapInitializer.java | 9 +-
.../OpenGaussAuthenticationHandlerTest.java | 2 +-
.../PostgreSQLAuthenticationHandlerTest.java | 2 +-
40 files changed, 376 insertions(+), 89 deletions(-)
diff --git
a/shardingsphere-distribution/shardingsphere-proxy-distribution/src/main/resources/bin/start.sh
b/shardingsphere-distribution/shardingsphere-proxy-distribution/src/main/resources/bin/start.sh
index 2320b4fc322..0087b4c4c82 100644
---
a/shardingsphere-distribution/shardingsphere-proxy-distribution/src/main/resources/bin/start.sh
+++
b/shardingsphere-distribution/shardingsphere-proxy-distribution/src/main/resources/bin/start.sh
@@ -102,6 +102,7 @@ MAIN_CLASS=org.apache.shardingsphere.proxy.Bootstrap
unset -v PORT
unset -v ADDRESSES
unset -v CONF_PATH
+unset -v FORCE
print_usage() {
echo "usage:"
@@ -116,6 +117,7 @@ print_usage() {
echo " comma-separated list. The default value is '0.0.0.0'."
echo "-p Bind port, default is '3307', which could be changed in
server.yaml"
echo "-c Path to config directory of ShardingSphere-Proxy, default is
'conf'"
+ echo "-f Force start ShardingSphere-Proxy"
exit 0
}
@@ -136,8 +138,8 @@ if [ $# == 0 ]; then
CLASS_PATH=${DEPLOY_DIR}/conf:${CLASS_PATH}
fi
-if [[ $1 == -a ]] || [[ $1 == -p ]] || [[ $1 == -c ]] ; then
- while getopts ":a:p:c:" opt
+if [[ $1 == -a ]] || [[ $1 == -p ]] || [[ $1 == -c ]] || [[ $1 == -f ]] ; then
+ while getopts ":a:p:c:f" opt
do
case $opt in
a)
@@ -149,6 +151,9 @@ if [[ $1 == -a ]] || [[ $1 == -p ]] || [[ $1 == -c ]] ; then
c)
echo "The configuration path is $OPTARG"
CONF_PATH=$OPTARG;;
+ f)
+ echo "The force param is true"
+ FORCE=true;;
?)
print_usage;;
esac
@@ -173,8 +178,16 @@ if [ -z "$PORT" ]; then
PORT=-1
fi
+if [ -z "$ADDRESSES" ]; then
+ ADDRESSES="0.0.0.0"
+fi
+
+if [ -z "$FORCE" ]; then
+ FORCE=false
+fi
+
CLASS_PATH=${CONF_PATH}:${CLASS_PATH}
-MAIN_CLASS="${MAIN_CLASS} ${PORT} ${CONF_PATH} ${ADDRESSES}"
+MAIN_CLASS="${MAIN_CLASS} ${PORT} ${CONF_PATH} ${ADDRESSES} ${FORCE}"
echo "The classpath is ${CLASS_PATH}"
echo "main class ${MAIN_CLASS}"
diff --git
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-handler/src/test/java/org/apache/shardingsphere/sharding/distsql/checker/ShardingRuleStatementCheckerTest.java
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-handler/src/test/java/org/apache/shardingsphere/sharding/distsql/checker/ShardingRuleStatementCheckerTest.java
index dfc241b5c66..935f21f7356 100644
---
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-handler/src/test/java/org/apache/shardingsphere/sharding/distsql/checker/ShardingRuleStatementCheckerTest.java
+++
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-handler/src/test/java/org/apache/shardingsphere/sharding/distsql/checker/ShardingRuleStatementCheckerTest.java
@@ -69,7 +69,7 @@ public final class ShardingRuleStatementCheckerTest {
private final ShardingRuleConfiguration shardingRuleConfig =
createShardingRuleConfiguration();
- private final ShardingSphereResource shardingSphereResource = new
ShardingSphereResource(createDataSource());
+ private final ShardingSphereResource shardingSphereResource = new
ShardingSphereResource("sharding_db", createDataSource());
@Before
public void before() {
diff --git
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/AlterShardingTableRuleStatementUpdaterTest.java
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/AlterShardingTableRuleStatementUpdaterTest.java
index 3c9eb12062e..1d1072222e3 100644
---
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/AlterShardingTableRuleStatementUpdaterTest.java
+++
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/AlterShardingTableRuleStatementUpdaterTest.java
@@ -65,7 +65,7 @@ public final class AlterShardingTableRuleStatementUpdaterTest
{
private final ShardingRuleConfiguration currentRuleConfig =
createCurrentShardingRuleConfiguration();
- private final ShardingSphereResource shardingSphereResource = new
ShardingSphereResource(createDataSource());
+ private final ShardingSphereResource shardingSphereResource = new
ShardingSphereResource("sharding_db", createDataSource());
private final AlterShardingTableRuleStatementUpdater updater = new
AlterShardingTableRuleStatementUpdater();
diff --git
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/CreateShardingTableRuleStatementUpdaterTest.java
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/CreateShardingTableRuleStatementUpdaterTest.java
index 51a70790fef..8db0c1fb603 100644
---
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/CreateShardingTableRuleStatementUpdaterTest.java
+++
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/CreateShardingTableRuleStatementUpdaterTest.java
@@ -78,7 +78,7 @@ public final class
CreateShardingTableRuleStatementUpdaterTest {
private final ShardingRuleConfiguration currentRuleConfig =
createCurrentShardingRuleConfiguration();
- private final ShardingSphereResource shardingSphereResource = new
ShardingSphereResource(createDataSource());
+ private final ShardingSphereResource shardingSphereResource = new
ShardingSphereResource("sharding_db", createDataSource());
private final CreateShardingTableRuleStatementUpdater updater = new
CreateShardingTableRuleStatementUpdater();
diff --git
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/database/type/DatabaseTypeEngine.java
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/database/type/DatabaseTypeEngine.java
index 56fb2cb4ab4..0ad8db2bdbb 100644
---
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/database/type/DatabaseTypeEngine.java
+++
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/database/type/DatabaseTypeEngine.java
@@ -18,11 +18,13 @@
package org.apache.shardingsphere.infra.database.type;
import com.google.common.base.Preconditions;
+import com.google.common.base.Strings;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.shardingsphere.infra.config.database.DatabaseConfiguration;
import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
import org.apache.shardingsphere.infra.config.props.ConfigurationPropertyKey;
+import org.apache.shardingsphere.infra.datasource.state.DataSourceStateManager;
import
org.apache.shardingsphere.infra.util.exception.external.sql.type.wrapper.SQLWrapperException;
import javax.sql.DataSource;
@@ -31,7 +33,9 @@ import java.sql.SQLException;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
+import java.util.Map.Entry;
import java.util.Optional;
+import java.util.stream.Collectors;
/**
* Database type engine.
@@ -44,12 +48,13 @@ public final class DatabaseTypeEngine {
/**
* Get protocol type.
*
+ * @param databaseName database name
* @param databaseConfig database configuration
* @param props props
* @return protocol type
*/
- public static DatabaseType getProtocolType(final DatabaseConfiguration
databaseConfig, final ConfigurationProperties props) {
- return findConfiguredDatabaseType(props).orElseGet(() ->
getDatabaseType(databaseConfig.getDataSources().values()));
+ public static DatabaseType getProtocolType(final String databaseName,
final DatabaseConfiguration databaseConfig, final ConfigurationProperties
props) {
+ return findConfiguredDatabaseType(props).orElseGet(() ->
getDatabaseType(DataSourceStateManager.getInstance().getEnabledDataSources(databaseName,
databaseConfig)));
}
/**
@@ -61,12 +66,7 @@ public final class DatabaseTypeEngine {
*/
public static DatabaseType getProtocolType(final Map<String, ? extends
DatabaseConfiguration> databaseConfigs, final ConfigurationProperties props) {
Optional<DatabaseType> configuredDatabaseType =
findConfiguredDatabaseType(props);
- if (configuredDatabaseType.isPresent()) {
- return configuredDatabaseType.get();
- }
- Collection<DataSource> dataSources = databaseConfigs.values().stream()
- .filter(each ->
!each.getDataSources().isEmpty()).findFirst().map(optional ->
optional.getDataSources().values()).orElseGet(Collections::emptyList);
- return getDatabaseType(dataSources);
+ return configuredDatabaseType.orElseGet(() ->
getDatabaseType(getEnabledDataSources(databaseConfigs)));
}
/**
@@ -76,8 +76,15 @@ public final class DatabaseTypeEngine {
* @return storage type
*/
public static DatabaseType getStorageType(final Map<String, ? extends
DatabaseConfiguration> databaseConfigs) {
- return getDatabaseType(
- databaseConfigs.values().stream().filter(each ->
!each.getDataSources().isEmpty()).findFirst().map(optional ->
optional.getDataSources().values()).orElseGet(Collections::emptyList));
+ return getDatabaseType(getEnabledDataSources(databaseConfigs));
+ }
+
+ private static Collection<DataSource> getEnabledDataSources(final
Map<String, ? extends DatabaseConfiguration> databaseConfigs) {
+ Map<String, ? extends DatabaseConfiguration> databaseConfigMap =
databaseConfigs.entrySet().stream()
+ .filter(each ->
!each.getValue().getDataSources().isEmpty()).collect(Collectors.toMap(Entry::getKey,
Entry::getValue));
+ String databaseName = databaseConfigMap.isEmpty() ? "" :
databaseConfigMap.entrySet().iterator().next().getKey();
+ return Strings.isNullOrEmpty(databaseName) ? Collections.emptyList()
+ :
DataSourceStateManager.getInstance().getEnabledDataSourceMap(databaseName,
databaseConfigMap.get(databaseName).getDataSources()).values();
}
/**
diff --git
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/datasource/state/DataSourceState.java
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/datasource/state/DataSourceState.java
new file mode 100644
index 00000000000..f25a4fca4dc
--- /dev/null
+++
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/datasource/state/DataSourceState.java
@@ -0,0 +1,71 @@
+/*
+ * 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.infra.datasource.state;
+
+import com.google.common.base.Strings;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * Data source state.
+ */
+public enum DataSourceState {
+
+ DISABLED, ENABLED;
+
+ private static final Map<String, DataSourceState> DATA_SOURCE_STATES = new
HashMap<>(2, 1);
+
+ static {
+ DATA_SOURCE_STATES.put(DISABLED.name().toLowerCase(), DISABLED);
+ DATA_SOURCE_STATES.put(ENABLED.name().toLowerCase(), ENABLED);
+ }
+
+ /**
+ * Data source disable or enable.
+ *
+ * @param state data source state
+ * @return disable or enable
+ */
+ public static boolean isDisable(final String state) {
+ return DISABLED.name().equalsIgnoreCase(state);
+ }
+
+ /**
+ * Data source disable or enable.
+ *
+ * @param state data source state
+ * @return disable or enable
+ */
+ public static boolean isEnable(final String state) {
+ return ENABLED.name().equalsIgnoreCase(state);
+ }
+
+ /**
+ * Get data source state by state name.
+ *
+ * @param state data source state name
+ * @return data source state
+ */
+ public static DataSourceState getDataSourceState(final String state) {
+ if (!Strings.isNullOrEmpty(state) &&
DATA_SOURCE_STATES.containsKey(state.toLowerCase())) {
+ return DATA_SOURCE_STATES.get(state.toLowerCase());
+ }
+ throw new IllegalArgumentException("Illegal data source state: " +
state);
+ }
+}
diff --git
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/datasource/state/DataSourceStateManager.java
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/datasource/state/DataSourceStateManager.java
new file mode 100644
index 00000000000..2b1e900991d
--- /dev/null
+++
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/datasource/state/DataSourceStateManager.java
@@ -0,0 +1,150 @@
+/*
+ * 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.infra.datasource.state;
+
+import lombok.AccessLevel;
+import lombok.NoArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.shardingsphere.infra.config.database.DatabaseConfiguration;
+import
org.apache.shardingsphere.infra.datasource.state.exception.DataSourceStateException;
+
+import javax.sql.DataSource;
+import java.sql.Connection;
+import java.sql.SQLException;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.LinkedHashMap;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+
+/**
+ * Data source state manager.
+ */
+@Slf4j
+@NoArgsConstructor(access = AccessLevel.PRIVATE)
+public final class DataSourceStateManager {
+
+ private static final DataSourceStateManager INSTANCE = new
DataSourceStateManager();
+
+ private final Map<String, DataSourceState> dataSourceStates = new
ConcurrentHashMap<>();
+
+ private volatile boolean force;
+
+ private volatile boolean initialized;
+
+ /**
+ * Get data source state manager.
+ *
+ * @return data source state manager
+ */
+ public static DataSourceStateManager getInstance() {
+ return INSTANCE;
+ }
+
+ /**
+ * Set data source states when bootstrap.
+ *
+ * @param databaseName database name
+ * @param dataSources data sources
+ * @param storageDataSourceStates storage node data source state
+ * @param force whether to force start
+ */
+ public void initStates(final String databaseName, final Map<String,
DataSource> dataSources, final Map<String, DataSourceState>
storageDataSourceStates, final boolean force) {
+ this.force = force;
+ dataSources.forEach((key, value) -> {
+ initState(databaseName, storageDataSourceStates, key, value);
+ });
+ initialized = true;
+ }
+
+ private void initState(final String databaseName, final Map<String,
DataSourceState> storageDataSourceStates, final String actualDataSourceName,
final DataSource dataSource) {
+ DataSourceState storageState =
storageDataSourceStates.get(getCacheKey(databaseName, actualDataSourceName));
+ if (DataSourceState.DISABLED == storageState) {
+ dataSourceStates.put(getCacheKey(databaseName,
actualDataSourceName), storageState);
+ } else {
+ checkState(databaseName, actualDataSourceName, dataSource);
+ }
+ }
+
+ private void checkState(final String databaseName, final String
actualDataSourceName, final DataSource dataSource) {
+ try (Connection ignored = dataSource.getConnection()) {
+ dataSourceStates.put(getCacheKey(databaseName,
actualDataSourceName), DataSourceState.ENABLED);
+ } catch (final SQLException ex) {
+ if (this.force) {
+ log.error("Data source state unavailable, ignored with the -f
parameter.", ex);
+ } else {
+ throw new DataSourceStateException("DataSourceState", 1, "Data
source state unavailable.", ex);
+ }
+ }
+ }
+
+ /**
+ * Get enabled data sources.
+ *
+ * @param databaseName database name
+ * @param databaseConfig database config
+ * @return enabled data sources
+ */
+ public Collection<DataSource> getEnabledDataSources(final String
databaseName, final DatabaseConfiguration databaseConfig) {
+ return databaseConfig.getDataSources().isEmpty() ?
Collections.emptyList() : getEnabledDataSourceMap(databaseName,
databaseConfig.getDataSources()).values();
+ }
+
+ /**
+ * Get enabled data source map.
+ *
+ * @param databaseName database name
+ * @param dataSources data sources
+ * @return enabled data source map
+ */
+ public Map<String, DataSource> getEnabledDataSourceMap(final String
databaseName, final Map<String, DataSource> dataSources) {
+ if (dataSources.isEmpty() || !initialized) {
+ return dataSources;
+ }
+ Map<String, DataSource> result =
filterDisabledDataSources(databaseName, dataSources);
+ checkForceConnection(result);
+ return result;
+ }
+
+ private Map<String, DataSource> filterDisabledDataSources(final String
databaseName, final Map<String, DataSource> dataSources) {
+ Map<String, DataSource> result = new
LinkedHashMap<>(dataSources.size(), 1);
+ dataSources.forEach((key, value) -> {
+ DataSourceState dataSourceState =
dataSourceStates.get(getCacheKey(databaseName, key));
+ if (DataSourceState.DISABLED != dataSourceState) {
+ result.put(key, value);
+ }
+ });
+ return result;
+ }
+
+ private void checkForceConnection(final Map<String, DataSource>
dataSources) {
+ if (force) {
+ dataSources.entrySet().removeIf(entry -> {
+ try (Connection ignored = entry.getValue().getConnection()) {
+ return false;
+ } catch (final SQLException ex) {
+ log.error("Data source state unavailable, ignored with the
-f parameter.", ex);
+ return true;
+ }
+ });
+ }
+ }
+
+ private String getCacheKey(final String databaseName, final String
dataSourceName) {
+ return databaseName + "." + dataSourceName;
+ }
+}
diff --git
a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/metadata/database/resource/ShardingSphereResourceTest.java
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/datasource/state/exception/DataSourceStateException.java
similarity index 55%
copy from
shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/metadata/database/resource/ShardingSphereResourceTest.java
copy to
shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/datasource/state/exception/DataSourceStateException.java
index 3f3612756f7..f9caffa25a0 100644
---
a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/metadata/database/resource/ShardingSphereResourceTest.java
+++
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/datasource/state/exception/DataSourceStateException.java
@@ -15,25 +15,18 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.infra.metadata.database.resource;
+package org.apache.shardingsphere.infra.datasource.state.exception;
-import org.apache.shardingsphere.test.mock.MockedDataSource;
-import org.junit.Test;
+import
org.apache.shardingsphere.infra.util.exception.external.server.ShardingSphereServerException;
-import java.util.Collections;
-
-import static org.junit.Assert.assertTrue;
-
-public final class ShardingSphereResourceTest {
+/**
+ * Data source state exception.
+ */
+public final class DataSourceStateException extends
ShardingSphereServerException {
+
+ private static final long serialVersionUID = -8058761885303180333L;
- @SuppressWarnings("BusyWait")
- @Test
- public void assertClose() throws InterruptedException {
- MockedDataSource dataSource = new MockedDataSource();
- new ShardingSphereResource(Collections.singletonMap("foo_ds",
dataSource)).close(dataSource);
- while (!dataSource.isClosed()) {
- Thread.sleep(10L);
- }
- assertTrue(dataSource.isClosed());
+ public DataSourceStateException(final String errorCategory, final int
errorCode, final String message, final Exception cause) {
+ super(errorCategory, errorCode, message, cause);
}
}
diff --git
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/database/ShardingSphereDatabase.java
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/database/ShardingSphereDatabase.java
index c3a4a2ab9fb..8e8fbf0648b 100644
---
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/database/ShardingSphereDatabase.java
+++
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/database/ShardingSphereDatabase.java
@@ -105,13 +105,13 @@ public final class ShardingSphereDatabase {
private static ShardingSphereDatabase create(final String name, final
DatabaseType protocolType, final DatabaseConfiguration databaseConfig,
final
Collection<ShardingSphereRule> rules, final Map<String, ShardingSphereSchema>
schemas) {
- ShardingSphereResource resource =
createResource(databaseConfig.getDataSources());
+ ShardingSphereResource resource = createResource(name,
databaseConfig.getDataSources());
ShardingSphereRuleMetaData ruleMetaData = new
ShardingSphereRuleMetaData(rules);
return new ShardingSphereDatabase(name, protocolType, resource,
ruleMetaData, schemas);
}
- private static ShardingSphereResource createResource(final Map<String,
DataSource> dataSourceMap) {
- return new ShardingSphereResource(dataSourceMap);
+ private static ShardingSphereResource createResource(final String
databaseName, final Map<String, DataSource> dataSourceMap) {
+ return new ShardingSphereResource(databaseName, dataSourceMap);
}
/**
diff --git
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/database/ShardingSphereDatabasesFactory.java
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/database/ShardingSphereDatabasesFactory.java
index d2dd238b917..b58c27bf3cf 100644
---
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/database/ShardingSphereDatabasesFactory.java
+++
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/database/ShardingSphereDatabasesFactory.java
@@ -21,6 +21,7 @@ import
org.apache.shardingsphere.infra.config.database.DatabaseConfiguration;
import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
import org.apache.shardingsphere.infra.database.type.DatabaseType;
import org.apache.shardingsphere.infra.database.type.DatabaseTypeEngine;
+import org.apache.shardingsphere.infra.datasource.state.DataSourceStateManager;
import org.apache.shardingsphere.infra.instance.InstanceContext;
import java.sql.SQLException;
@@ -46,8 +47,8 @@ public final class ShardingSphereDatabasesFactory {
*/
public static ShardingSphereDatabase create(final String databaseName,
final DatabaseConfiguration databaseConfig,
final ConfigurationProperties
props, final InstanceContext instanceContext) throws SQLException {
- DatabaseType protocolType =
DatabaseTypeEngine.getProtocolType(databaseConfig, props);
- DatabaseType storageType =
DatabaseTypeEngine.getDatabaseType(databaseConfig.getDataSources().values());
+ DatabaseType protocolType =
DatabaseTypeEngine.getProtocolType(databaseName, databaseConfig, props);
+ DatabaseType storageType =
DatabaseTypeEngine.getDatabaseType(DataSourceStateManager.getInstance().getEnabledDataSources(databaseName,
databaseConfig));
return ShardingSphereDatabase.create(databaseName, protocolType,
storageType, databaseConfig, props, instanceContext);
}
diff --git
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/ShardingSphereResource.java
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/ShardingSphereResource.java
index 39ae8a29434..43598b352d5 100644
---
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/ShardingSphereResource.java
+++
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/ShardingSphereResource.java
@@ -24,6 +24,7 @@ import
org.apache.shardingsphere.infra.database.type.DatabaseType;
import org.apache.shardingsphere.infra.database.type.DatabaseTypeEngine;
import
org.apache.shardingsphere.infra.datasource.pool.destroyer.DataSourcePoolDestroyer;
import
org.apache.shardingsphere.infra.datasource.props.DataSourcePropertiesCreator;
+import org.apache.shardingsphere.infra.datasource.state.DataSourceStateManager;
import javax.sql.DataSource;
import java.util.Collection;
@@ -46,9 +47,9 @@ public final class ShardingSphereResource {
@Getter(AccessLevel.NONE)
private final Map<String, DataSourceMetaData> dataSourceMetaDataMap;
- public ShardingSphereResource(final Map<String, DataSource> dataSources) {
+ public ShardingSphereResource(final String databaseName, final Map<String,
DataSource> dataSources) {
this.dataSources = dataSources;
- databaseType = getDatabaseType(dataSources);
+ databaseType =
getDatabaseType(DataSourceStateManager.getInstance().getEnabledDataSourceMap(databaseName,
dataSources));
dataSourceMetaDataMap = createDataSourceMetaDataMap(dataSources);
}
diff --git
a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/database/type/DatabaseTypeEngineTest.java
b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/database/type/DatabaseTypeEngineTest.java
index badf7a091c5..6401c1c53b5 100644
---
a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/database/type/DatabaseTypeEngineTest.java
+++
b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/database/type/DatabaseTypeEngineTest.java
@@ -51,7 +51,7 @@ public final class DatabaseTypeEngineTest {
Properties props = new Properties();
props.setProperty(ConfigurationPropertyKey.PROXY_FRONTEND_DATABASE_PROTOCOL_TYPE.getKey(),
"MySQL");
DatabaseConfiguration databaseConfig = new
DataSourceProvidedDatabaseConfiguration(Collections.emptyMap(),
Collections.singleton(new FixtureRuleConfiguration()));
- assertThat(DatabaseTypeEngine.getProtocolType(databaseConfig, new
ConfigurationProperties(props)), instanceOf(MySQLDatabaseType.class));
+ assertThat(DatabaseTypeEngine.getProtocolType("sharding_db",
databaseConfig, new ConfigurationProperties(props)),
instanceOf(MySQLDatabaseType.class));
assertThat(DatabaseTypeEngine.getProtocolType(Collections.singletonMap("foo_db",
databaseConfig), new ConfigurationProperties(props)),
instanceOf(MySQLDatabaseType.class));
}
@@ -59,7 +59,7 @@ public final class DatabaseTypeEngineTest {
public void assertGetProtocolTypeFromDataSource() throws SQLException {
DataSource datasource =
mockDataSource(DatabaseTypeFactory.getInstance("PostgreSQL"));
DatabaseConfiguration databaseConfig = new
DataSourceProvidedDatabaseConfiguration(Collections.singletonMap("foo_ds",
datasource), Collections.singleton(new FixtureRuleConfiguration()));
- assertThat(DatabaseTypeEngine.getProtocolType(databaseConfig, new
ConfigurationProperties(new Properties())),
instanceOf(PostgreSQLDatabaseType.class));
+ assertThat(DatabaseTypeEngine.getProtocolType("sharding_db",
databaseConfig, new ConfigurationProperties(new Properties())),
instanceOf(PostgreSQLDatabaseType.class));
assertThat(DatabaseTypeEngine.getProtocolType(Collections.singletonMap("foo_db",
databaseConfig), new ConfigurationProperties(new Properties())),
instanceOf(PostgreSQLDatabaseType.class));
}
diff --git
a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/metadata/database/ShardingSphereDatabaseTest.java
b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/metadata/database/ShardingSphereDatabaseTest.java
index ab8b88816b9..2d5969d7eb0 100644
---
a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/metadata/database/ShardingSphereDatabaseTest.java
+++
b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/metadata/database/ShardingSphereDatabaseTest.java
@@ -45,28 +45,28 @@ public final class ShardingSphereDatabaseTest {
@Test
public void assertIsComplete() {
- ShardingSphereResource resource = new
ShardingSphereResource(Collections.singletonMap("ds", new MockedDataSource()));
+ ShardingSphereResource resource = new
ShardingSphereResource("sharding_db", Collections.singletonMap("ds", new
MockedDataSource()));
ShardingSphereRuleMetaData ruleMetaData = new
ShardingSphereRuleMetaData(Collections.singleton(mock(ShardingSphereRule.class)));
assertTrue(new ShardingSphereDatabase("foo_db",
mock(DatabaseType.class), resource, ruleMetaData,
Collections.emptyMap()).isComplete());
}
@Test
public void assertIsNotCompleteWithoutRule() {
- ShardingSphereResource resource = new
ShardingSphereResource(Collections.singletonMap("ds", new MockedDataSource()));
+ ShardingSphereResource resource = new
ShardingSphereResource("sharding_db", Collections.singletonMap("ds", new
MockedDataSource()));
ShardingSphereRuleMetaData ruleMetaData = new
ShardingSphereRuleMetaData(Collections.emptyList());
assertFalse(new ShardingSphereDatabase("foo_db",
mock(DatabaseType.class), resource, ruleMetaData,
Collections.emptyMap()).isComplete());
}
@Test
public void assertIsNotCompleteWithoutDataSource() {
- ShardingSphereResource resource = new
ShardingSphereResource(Collections.emptyMap());
+ ShardingSphereResource resource = new
ShardingSphereResource("sharding_db", Collections.emptyMap());
ShardingSphereRuleMetaData ruleMetaData = new
ShardingSphereRuleMetaData(Collections.singleton(mock(ShardingSphereRule.class)));
assertFalse(new ShardingSphereDatabase("foo_db",
mock(DatabaseType.class), resource, ruleMetaData,
Collections.emptyMap()).isComplete());
}
@Test
public void assertReloadRules() {
- ShardingSphereResource resource = new
ShardingSphereResource(Collections.singletonMap("ds", new MockedDataSource()));
+ ShardingSphereResource resource = new
ShardingSphereResource("sharding_db", Collections.singletonMap("ds", new
MockedDataSource()));
Collection<ShardingSphereRule> rules = new LinkedList<>();
rules.add(mock(MutableDataNodeRule.class, RETURNS_DEEP_STUBS));
rules.add(mock(DataSourceContainedRule.class, RETURNS_DEEP_STUBS));
diff --git
a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/metadata/database/resource/ShardingSphereResourceTest.java
b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/metadata/database/resource/ShardingSphereResourceTest.java
index 3f3612756f7..fc7157bcfb3 100644
---
a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/metadata/database/resource/ShardingSphereResourceTest.java
+++
b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/metadata/database/resource/ShardingSphereResourceTest.java
@@ -30,7 +30,7 @@ public final class ShardingSphereResourceTest {
@Test
public void assertClose() throws InterruptedException {
MockedDataSource dataSource = new MockedDataSource();
- new ShardingSphereResource(Collections.singletonMap("foo_ds",
dataSource)).close(dataSource);
+ new ShardingSphereResource("sharding_db",
Collections.singletonMap("foo_ds", dataSource)).close(dataSource);
while (!dataSource.isClosed()) {
Thread.sleep(10L);
}
diff --git
a/shardingsphere-infra/shardingsphere-infra-executor/src/test/java/org/apache/shardingsphere/infra/executor/sql/context/ExecutionContextBuilderTest.java
b/shardingsphere-infra/shardingsphere-infra-executor/src/test/java/org/apache/shardingsphere/infra/executor/sql/context/ExecutionContextBuilderTest.java
index 42a8a96e762..269d8404ec1 100644
---
a/shardingsphere-infra/shardingsphere-infra-executor/src/test/java/org/apache/shardingsphere/infra/executor/sql/context/ExecutionContextBuilderTest.java
+++
b/shardingsphere-infra/shardingsphere-infra-executor/src/test/java/org/apache/shardingsphere/infra/executor/sql/context/ExecutionContextBuilderTest.java
@@ -73,7 +73,7 @@ public final class ExecutionContextBuilderTest {
Map<RouteUnit, SQLRewriteUnit> sqlRewriteUnits = new HashMap<>(2, 1);
sqlRewriteUnits.put(routeUnit1, sqlRewriteUnit1);
sqlRewriteUnits.put(routeUnit2, sqlRewriteUnit2);
- ShardingSphereResource resource = new
ShardingSphereResource(Collections.emptyMap());
+ ShardingSphereResource resource = new
ShardingSphereResource("sharding_db", Collections.emptyMap());
ShardingSphereRuleMetaData ruleMetaData = new
ShardingSphereRuleMetaData(Collections.emptyList());
ShardingSphereDatabase database = new
ShardingSphereDatabase(DefaultDatabase.LOGIC_NAME, mock(DatabaseType.class),
resource, ruleMetaData, buildDatabase());
Collection<ExecutionUnit> actual =
ExecutionContextBuilder.build(database, new
RouteSQLRewriteResult(sqlRewriteUnits), mock(SQLStatementContext.class));
@@ -91,7 +91,7 @@ public final class ExecutionContextBuilderTest {
SQLRewriteUnit sqlRewriteUnit2 = new SQLRewriteUnit("sql2",
Collections.singletonList("parameter2"));
Map<RouteUnit, SQLRewriteUnit> sqlRewriteUnits = new HashMap<>(2, 1);
sqlRewriteUnits.put(routeUnit2, sqlRewriteUnit2);
- ShardingSphereResource resource = new
ShardingSphereResource(Collections.emptyMap());
+ ShardingSphereResource resource = new
ShardingSphereResource("sharding_db", Collections.emptyMap());
ShardingSphereRuleMetaData ruleMetaData = new
ShardingSphereRuleMetaData(Collections.emptyList());
ShardingSphereDatabase database = new
ShardingSphereDatabase(DefaultDatabase.LOGIC_NAME, mock(DatabaseType.class),
resource, ruleMetaData, buildDatabaseWithoutPrimaryKey());
Collection<ExecutionUnit> actual =
ExecutionContextBuilder.build(database, new
RouteSQLRewriteResult(sqlRewriteUnits), mock(SQLStatementContext.class));
diff --git
a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/datasource/ShardingSphereDataSource.java
b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/datasource/ShardingSphereDataSource.java
index 996bf3c3690..bf62bff857e 100644
---
a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/datasource/ShardingSphereDataSource.java
+++
b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/datasource/ShardingSphereDataSource.java
@@ -72,7 +72,7 @@ public final class ShardingSphereDataSource extends
AbstractDataSourceAdapter im
Collection<RuleConfiguration> databaseRuleConfigs = new
LinkedList<>(ruleConfigs);
databaseRuleConfigs.removeAll(globalRuleConfigs);
ContextManagerBuilderParameter parameter = new
ContextManagerBuilderParameter(modeConfig,
Collections.singletonMap(databaseName,
- new DataSourceProvidedDatabaseConfiguration(dataSourceMap,
databaseRuleConfigs)), globalRuleConfigs, props, Collections.emptyList(),
instanceMetaData);
+ new DataSourceProvidedDatabaseConfiguration(dataSourceMap,
databaseRuleConfigs)), globalRuleConfigs, props, Collections.emptyList(),
instanceMetaData, false);
return
ContextManagerBuilderFactory.getInstance(modeConfig).build(parameter);
}
diff --git
a/shardingsphere-kernel/shardingsphere-single-table/shardingsphere-single-table-core/src/main/java/org/apache/shardingsphere/singletable/rule/SingleTableRule.java
b/shardingsphere-kernel/shardingsphere-single-table/shardingsphere-single-table-core/src/main/java/org/apache/shardingsphere/singletable/rule/SingleTableRule.java
index d7da7b492f9..2dadda06963 100644
---
a/shardingsphere-kernel/shardingsphere-single-table/shardingsphere-single-table-core/src/main/java/org/apache/shardingsphere/singletable/rule/SingleTableRule.java
+++
b/shardingsphere-kernel/shardingsphere-single-table/shardingsphere-single-table-core/src/main/java/org/apache/shardingsphere/singletable/rule/SingleTableRule.java
@@ -21,6 +21,7 @@ import lombok.Getter;
import org.apache.shardingsphere.infra.config.rule.RuleConfiguration;
import org.apache.shardingsphere.infra.database.type.DatabaseTypeEngine;
import org.apache.shardingsphere.infra.datanode.DataNode;
+import org.apache.shardingsphere.infra.datasource.state.DataSourceStateManager;
import org.apache.shardingsphere.infra.metadata.database.schema.QualifiedTable;
import org.apache.shardingsphere.infra.route.context.RouteContext;
import org.apache.shardingsphere.infra.route.context.RouteUnit;
@@ -70,9 +71,10 @@ public final class SingleTableRule implements DatabaseRule,
DataNodeContainedRul
public SingleTableRule(final SingleTableRuleConfiguration ruleConfig,
final String databaseName, final Map<String, DataSource> dataSourceMap, final
Collection<ShardingSphereRule> builtRules) {
configuration = ruleConfig;
defaultDataSource = ruleConfig.getDefaultDataSource().orElse(null);
- Map<String, DataSource> aggregateDataSourceMap =
getAggregateDataSourceMap(dataSourceMap, builtRules);
+ Map<String, DataSource> enabledDataSources =
DataSourceStateManager.getInstance().getEnabledDataSourceMap(databaseName,
dataSourceMap);
+ Map<String, DataSource> aggregateDataSourceMap =
getAggregateDataSourceMap(enabledDataSources, builtRules);
dataSourceNames = aggregateDataSourceMap.keySet();
- singleTableDataNodes = SingleTableDataNodeLoader.load(databaseName,
DatabaseTypeEngine.getDatabaseType(dataSourceMap.values()),
aggregateDataSourceMap, getLoadedTables(builtRules));
+ singleTableDataNodes = SingleTableDataNodeLoader.load(databaseName,
DatabaseTypeEngine.getDatabaseType(enabledDataSources.values()),
aggregateDataSourceMap, getLoadedTables(builtRules));
tableNames =
singleTableDataNodes.entrySet().stream().collect(Collectors.toConcurrentMap(Entry::getKey,
entry -> entry.getValue().iterator().next().getTableName()));
}
diff --git
a/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/ContextManager.java
b/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/ContextManager.java
index 171d1175aba..042372d0728 100644
---
a/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/ContextManager.java
+++
b/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/ContextManager.java
@@ -284,7 +284,8 @@ public final class ContextManager implements AutoCloseable {
database.getResource().getDataSources().entrySet().stream().filter(entry ->
!resource.getStaleDataSources().containsKey(entry.getKey()))
.collect(Collectors.toMap(Entry::getKey,
Entry::getValue));
result.put(database.getName().toLowerCase(),
- new ShardingSphereDatabase(database.getName(),
database.getProtocolType(), new ShardingSphereResource(newDataSource),
database.getRuleMetaData(), database.getSchemas()));
+ new ShardingSphereDatabase(database.getName(),
database.getProtocolType(), new ShardingSphereResource(database.getName(),
newDataSource),
+ database.getRuleMetaData(), database.getSchemas()));
return result;
}
diff --git
a/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/ContextManagerBuilderParameter.java
b/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/ContextManagerBuilderParameter.java
index 09471e45b8e..896b0b1ec55 100644
---
a/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/ContextManagerBuilderParameter.java
+++
b/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/ContextManagerBuilderParameter.java
@@ -49,6 +49,8 @@ public final class ContextManagerBuilderParameter {
private final InstanceMetaData instanceMetaData;
+ private final boolean force;
+
/**
* Whether parameter is empty.
*
diff --git
a/shardingsphere-mode/shardingsphere-mode-core/src/test/java/org/apache/shardingsphere/mode/manager/ContextManagerBuilderParameterTest.java
b/shardingsphere-mode/shardingsphere-mode-core/src/test/java/org/apache/shardingsphere/mode/manager/ContextManagerBuilderParameterTest.java
index a3df09bc9e1..adc07863d9b 100644
---
a/shardingsphere-mode/shardingsphere-mode-core/src/test/java/org/apache/shardingsphere/mode/manager/ContextManagerBuilderParameterTest.java
+++
b/shardingsphere-mode/shardingsphere-mode-core/src/test/java/org/apache/shardingsphere/mode/manager/ContextManagerBuilderParameterTest.java
@@ -41,32 +41,32 @@ public final class ContextManagerBuilderParameterTest {
@Test
public void assertIsEmptyWithoutAllParameters() {
- assertTrue(new ContextManagerBuilderParameter(null,
Collections.emptyMap(), Collections.emptyList(), new Properties(), null,
null).isEmpty());
+ assertTrue(new ContextManagerBuilderParameter(null,
Collections.emptyMap(), Collections.emptyList(), new Properties(), null, null,
false).isEmpty());
}
@Test
public void assertIsEmptyWithDatabaseAndWithoutConfigurations() {
- assertTrue(new ContextManagerBuilderParameter(null,
mockDatabaseConfigurations(true, true), Collections.emptyList(), new
Properties(), null, null).isEmpty());
+ assertTrue(new ContextManagerBuilderParameter(null,
mockDatabaseConfigurations(true, true), Collections.emptyList(), new
Properties(), null, null, false).isEmpty());
}
@Test
public void assertIsNotEmptyWhenGlobalRuleIsNotEmpty() {
- assertFalse(new ContextManagerBuilderParameter(null,
Collections.emptyMap(), Collections.singleton(mock(RuleConfiguration.class)),
new Properties(), null, null).isEmpty());
+ assertFalse(new ContextManagerBuilderParameter(null,
Collections.emptyMap(), Collections.singleton(mock(RuleConfiguration.class)),
new Properties(), null, null, false).isEmpty());
}
@Test
public void assertIsNotEmptyWhenPropsIsNotEmpty() {
- assertFalse(new ContextManagerBuilderParameter(null,
Collections.emptyMap(), Collections.emptyList(), createProperties(), null,
null).isEmpty());
+ assertFalse(new ContextManagerBuilderParameter(null,
Collections.emptyMap(), Collections.emptyList(), createProperties(), null,
null, false).isEmpty());
}
@Test
public void assertIsEmptyWhenDataSourceIsNotEmpty() {
- assertFalse(new ContextManagerBuilderParameter(null,
mockDatabaseConfigurations(false, true), Collections.emptyList(), new
Properties(), null, null).isEmpty());
+ assertFalse(new ContextManagerBuilderParameter(null,
mockDatabaseConfigurations(false, true), Collections.emptyList(), new
Properties(), null, null, false).isEmpty());
}
@Test
public void assertIsEmptyWhenDatabaseRuleIsNotEmpty() {
- assertFalse(new ContextManagerBuilderParameter(null,
mockDatabaseConfigurations(true, false), Collections.emptyList(), new
Properties(), null, null).isEmpty());
+ assertFalse(new ContextManagerBuilderParameter(null,
mockDatabaseConfigurations(true, false), Collections.emptyList(), new
Properties(), null, null, false).isEmpty());
}
private Map<String, DatabaseConfiguration>
mockDatabaseConfigurations(final boolean isEmptyDataSources, final boolean
isEmptyRuleConfigs) {
@@ -84,7 +84,7 @@ public final class ContextManagerBuilderParameterTest {
@Test
public void assertGetDefaultModeConfiguration() {
- ContextManagerBuilderParameter parameter = new
ContextManagerBuilderParameter(null, Collections.emptyMap(),
Collections.emptyList(), new Properties(), null, null);
+ ContextManagerBuilderParameter parameter = new
ContextManagerBuilderParameter(null, Collections.emptyMap(),
Collections.emptyList(), new Properties(), null, null, false);
assertThat(parameter.getModeConfiguration().getType(),
is("Standalone"));
assertNull(parameter.getModeConfiguration().getRepository());
assertTrue(parameter.getModeConfiguration().isOverwrite());
@@ -93,7 +93,7 @@ public final class ContextManagerBuilderParameterTest {
@Test
public void assertGetModeConfiguration() {
ModeConfiguration modeConfig = new ModeConfiguration("Cluster",
mock(PersistRepositoryConfiguration.class), false);
- ContextManagerBuilderParameter parameter = new
ContextManagerBuilderParameter(modeConfig, Collections.emptyMap(),
Collections.emptyList(), new Properties(), null, null);
+ ContextManagerBuilderParameter parameter = new
ContextManagerBuilderParameter(modeConfig, Collections.emptyMap(),
Collections.emptyList(), new Properties(), null, null, false);
assertThat(parameter.getModeConfiguration().getType(), is("Cluster"));
assertNotNull(parameter.getModeConfiguration().getRepository());
assertFalse(parameter.getModeConfiguration().isOverwrite());
diff --git
a/shardingsphere-mode/shardingsphere-mode-core/src/test/java/org/apache/shardingsphere/mode/manager/ContextManagerTest.java
b/shardingsphere-mode/shardingsphere-mode-core/src/test/java/org/apache/shardingsphere/mode/manager/ContextManagerTest.java
index a2713175cea..4fddce70631 100644
---
a/shardingsphere-mode/shardingsphere-mode-core/src/test/java/org/apache/shardingsphere/mode/manager/ContextManagerTest.java
+++
b/shardingsphere-mode/shardingsphere-mode-core/src/test/java/org/apache/shardingsphere/mode/manager/ContextManagerTest.java
@@ -100,7 +100,7 @@ public final class ContextManagerTest {
@Test
public void assertGetDataSourceMap() {
- ShardingSphereResource resource = new
ShardingSphereResource(Collections.singletonMap("foo_ds", new
MockedDataSource()));
+ ShardingSphereResource resource = new
ShardingSphereResource("sharding_db", Collections.singletonMap("foo_ds", new
MockedDataSource()));
ShardingSphereDatabase database = new
ShardingSphereDatabase(DefaultDatabase.LOGIC_NAME, mock(DatabaseType.class),
resource, mock(ShardingSphereRuleMetaData.class), Collections.emptyMap());
when(metaDataContexts.getMetaData().getDatabase(DefaultDatabase.LOGIC_NAME)).thenReturn(database);
assertThat(contextManager.getDataSourceMap(DefaultDatabase.LOGIC_NAME).size(),
is(1));
diff --git
a/shardingsphere-mode/shardingsphere-mode-core/src/test/java/org/apache/shardingsphere/mode/manager/switcher/ResourceSwitchManagerTest.java
b/shardingsphere-mode/shardingsphere-mode-core/src/test/java/org/apache/shardingsphere/mode/manager/switcher/ResourceSwitchManagerTest.java
index 21edc8dc02f..12651519a9d 100644
---
a/shardingsphere-mode/shardingsphere-mode-core/src/test/java/org/apache/shardingsphere/mode/manager/switcher/ResourceSwitchManagerTest.java
+++
b/shardingsphere-mode/shardingsphere-mode-core/src/test/java/org/apache/shardingsphere/mode/manager/switcher/ResourceSwitchManagerTest.java
@@ -37,7 +37,7 @@ public final class ResourceSwitchManagerTest {
@Test
public void assertCreate() throws InterruptedException {
Map<String, DataSource> dataSourceMap = createDataSourceMap();
- SwitchingResource actual = new ResourceSwitchManager().create(new
ShardingSphereResource(dataSourceMap), createToBeChangedDataSourcePropsMap());
+ SwitchingResource actual = new ResourceSwitchManager().create(new
ShardingSphereResource("sharding_db", dataSourceMap),
createToBeChangedDataSourcePropsMap());
assertNewDataSources(actual);
actual.closeStaleDataSources();
assertStaleDataSources(dataSourceMap);
diff --git
a/shardingsphere-mode/shardingsphere-mode-core/src/test/java/org/apache/shardingsphere/mode/metadata/MetaDataContextsFactoryTest.java
b/shardingsphere-mode/shardingsphere-mode-core/src/test/java/org/apache/shardingsphere/mode/metadata/MetaDataContextsFactoryTest.java
index 537f5c6f02b..5036d41ac94 100644
---
a/shardingsphere-mode/shardingsphere-mode-core/src/test/java/org/apache/shardingsphere/mode/metadata/MetaDataContextsFactoryTest.java
+++
b/shardingsphere-mode/shardingsphere-mode-core/src/test/java/org/apache/shardingsphere/mode/metadata/MetaDataContextsFactoryTest.java
@@ -154,7 +154,7 @@ public final class MetaDataContextsFactoryTest {
private ContextManagerBuilderParameter
createContextManagerBuilderParameter() {
return new ContextManagerBuilderParameter(null,
- Collections.singletonMap("foo_db",
mock(DataSourceGeneratedDatabaseConfiguration.class)), Collections.emptyList(),
new Properties(), Collections.emptyList(), null);
+ Collections.singletonMap("foo_db",
mock(DataSourceGeneratedDatabaseConfiguration.class)), Collections.emptyList(),
new Properties(), Collections.emptyList(), null, false);
}
@After
diff --git
a/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/ClusterContextManagerBuilder.java
b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/ClusterContextManagerBuilder.java
index 8ad645f4437..92130de8835 100644
---
a/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/ClusterContextManagerBuilder.java
+++
b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/ClusterContextManagerBuilder.java
@@ -17,10 +17,15 @@
package org.apache.shardingsphere.mode.manager.cluster;
-import org.apache.shardingsphere.infra.instance.InstanceContextAware;
-import org.apache.shardingsphere.infra.util.eventbus.EventBusContext;
+import com.google.common.base.Preconditions;
+import com.google.common.base.Splitter;
+import org.apache.shardingsphere.infra.config.database.DatabaseConfiguration;
+import org.apache.shardingsphere.infra.datasource.state.DataSourceState;
+import org.apache.shardingsphere.infra.datasource.state.DataSourceStateManager;
import org.apache.shardingsphere.infra.instance.ComputeNodeInstance;
import org.apache.shardingsphere.infra.instance.InstanceContext;
+import org.apache.shardingsphere.infra.instance.InstanceContextAware;
+import org.apache.shardingsphere.infra.util.eventbus.EventBusContext;
import org.apache.shardingsphere.mode.lock.ShardingSphereLockContext;
import org.apache.shardingsphere.mode.manager.ContextManager;
import org.apache.shardingsphere.mode.manager.ContextManagerBuilder;
@@ -31,12 +36,16 @@ import
org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.worke
import org.apache.shardingsphere.mode.metadata.MetaDataContexts;
import org.apache.shardingsphere.mode.metadata.MetaDataContextsFactory;
import org.apache.shardingsphere.mode.metadata.persist.MetaDataPersistService;
+import org.apache.shardingsphere.mode.metadata.storage.StorageNodeDataSource;
import
org.apache.shardingsphere.mode.repository.cluster.ClusterPersistRepository;
import
org.apache.shardingsphere.mode.repository.cluster.ClusterPersistRepositoryConfiguration;
import
org.apache.shardingsphere.mode.repository.cluster.ClusterPersistRepositoryFactory;
import org.apache.shardingsphere.schedule.core.ScheduleContextFactory;
import java.sql.SQLException;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
/**
* Cluster context manager builder.
@@ -54,6 +63,7 @@ public final class ClusterContextManagerBuilder implements
ContextManagerBuilder
if (persistRepository instanceof InstanceContextAware) {
((InstanceContextAware)
persistRepository).setInstanceContext(instanceContext);
}
+ checkDataSourceStates(parameter.getDatabaseConfigs(), registryCenter,
parameter.isForce());
MetaDataContexts metaDataContexts =
MetaDataContextsFactory.create(persistService, parameter, instanceContext);
persistMetaData(metaDataContexts);
ContextManager result = new ContextManager(metaDataContexts,
instanceContext);
@@ -61,6 +71,28 @@ public final class ClusterContextManagerBuilder implements
ContextManagerBuilder
return result;
}
+ private void checkDataSourceStates(final Map<String,
DatabaseConfiguration> databaseConfigs, final RegistryCenter registryCenter,
final boolean force) {
+ Map<String, StorageNodeDataSource> storageNodes =
registryCenter.getStorageNodeStatusService().loadStorageNodes();
+ Map<String, DataSourceState> storageDataSourceStates =
getStorageDataSourceStates(storageNodes);
+ databaseConfigs.forEach((key, value) -> {
+ if (null != value.getDataSources()) {
+ DataSourceStateManager.getInstance().initStates(key,
value.getDataSources(), storageDataSourceStates, force);
+ }
+ });
+ }
+
+ private Map<String, DataSourceState> getStorageDataSourceStates(final
Map<String, StorageNodeDataSource> storageDataSourceStates) {
+ Map<String, DataSourceState> result = new
HashMap<>(storageDataSourceStates.size(), 1);
+ storageDataSourceStates.forEach((key, value) -> {
+ List<String> values = Splitter.on(".").splitToList(key);
+ Preconditions.checkArgument(3 == values.size(), "Illegal data
source of storage node.");
+ String databaseName = values.get(0);
+ String dataSourceName = values.get(2);
+ result.put(databaseName + "." + dataSourceName,
DataSourceState.getDataSourceState(value.getStatus()));
+ });
+ return result;
+ }
+
private void persistConfigurations(final MetaDataPersistService
persistService, final ContextManagerBuilderParameter parameter) {
if (!parameter.isEmpty()) {
persistService.persistConfigurations(parameter.getDatabaseConfigs(),
parameter.getGlobalRuleConfigs(), parameter.getProps(),
parameter.getModeConfiguration().isOverwrite());
diff --git
a/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/ClusterContextManagerCoordinatorTest.java
b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/ClusterContextManagerCoordinatorTest.java
index 03ca2d7f7a1..10d3a6eae6d 100644
---
a/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/ClusterContextManagerCoordinatorTest.java
+++
b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/ClusterContextManagerCoordinatorTest.java
@@ -142,7 +142,7 @@ public final class ClusterContextManagerCoordinatorTest {
private ContextManagerBuilderParameter
createContextManagerBuilderParameter() {
ModeConfiguration modeConfig = new ModeConfiguration("Cluster", new
ClusterPersistRepositoryConfiguration("FIXTURE", "", "", new Properties()),
false);
InstanceMetaData instanceMetaData = new
ProxyInstanceMetaData("foo_instance_id", 3307);
- return new ContextManagerBuilderParameter(modeConfig,
Collections.emptyMap(), Collections.emptyList(), new Properties(),
Collections.emptyList(), instanceMetaData);
+ return new ContextManagerBuilderParameter(modeConfig,
Collections.emptyMap(), Collections.emptyList(), new Properties(),
Collections.emptyList(), instanceMetaData, false);
}
private Map<String, ShardingSphereDatabase> createDatabases() {
@@ -392,7 +392,7 @@ public final class ClusterContextManagerCoordinatorTest {
private Map<String, DataSource> initContextManager() {
Map<String, DataSource> result = getDataSourceMap();
- ShardingSphereResource resource = new ShardingSphereResource(result);
+ ShardingSphereResource resource = new
ShardingSphereResource("sharding_db", result);
ShardingSphereDatabase database = new ShardingSphereDatabase("db", new
MySQLDatabaseType(), resource, mock(ShardingSphereRuleMetaData.class),
Collections.emptyMap());
contextManager.getMetaDataContexts().getMetaData().getDatabases().put("db",
database);
return result;
diff --git
a/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-standalone-mode/shardingsphere-standalone-mode-core/src/test/java/org/apache/shardingsphere/mode/manager/standalone/StandaloneContextManagerBuilderTextTest.java
b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-standalone-mode/shardingsphere-standalone-mode-core/src/test/java/org/apache/shardingsphere/mode/manager/standalone/StandaloneContextManagerBuilderTextTest.java
index 0ddce88f47a..b2dda058e73 100644
---
a/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-standalone-mode/shardingsphere-standalone-mode-core/src/test/java/org/apache/shardingsphere/mode/manager/standalone/StandaloneContextManagerBuilderTextTest.java
+++
b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-standalone-mode/shardingsphere-standalone-mode-core/src/test/java/org/apache/shardingsphere/mode/manager/standalone/StandaloneContextManagerBuilderTextTest.java
@@ -59,6 +59,6 @@ public final class StandaloneContextManagerBuilderTextTest {
"foo_db", new
DataSourceProvidedDatabaseConfiguration(Collections.singletonMap("foo_ds", new
MockedDataSource()), Collections.singleton(mock(RuleConfiguration.class))));
Collection<RuleConfiguration> globalRuleConfigs =
Collections.singleton(mock(RuleConfiguration.class));
InstanceMetaData instanceMetaData = new
ProxyInstanceMetaData(UUID.randomUUID().toString(), 3307);
- return new ContextManagerBuilderParameter(modeConfig, databaseConfigs,
globalRuleConfigs, new Properties(), Collections.emptyList(), instanceMetaData);
+ return new ContextManagerBuilderParameter(modeConfig, databaseConfigs,
globalRuleConfigs, new Properties(), Collections.emptyList(), instanceMetaData,
false);
}
}
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/handler/admin/executor/DefaultDatabaseMetadataExecutorTest.java
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/handler/admin/executor/DefaultDatabaseMetadataExecutorTest.java
index 5b8120c67b6..f5f3b431d4d 100644
---
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/handler/admin/executor/DefaultDatabaseMetadataExecutorTest.java
+++
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/handler/admin/executor/DefaultDatabaseMetadataExecutorTest.java
@@ -109,7 +109,7 @@ public final class DefaultDatabaseMetadataExecutorTest
extends ProxyContextResto
private ShardingSphereDatabase createDatabase(final Map<String, String>
expectedResultSetMap) throws SQLException {
return new ShardingSphereDatabase("auth_db", new MySQLDatabaseType(),
- new ShardingSphereResource(Collections.singletonMap("foo_ds",
new MockedDataSource(mockConnection(expectedResultSetMap)))),
+ new ShardingSphereResource("sharding_db",
Collections.singletonMap("foo_ds", new
MockedDataSource(mockConnection(expectedResultSetMap)))),
mock(ShardingSphereRuleMetaData.class),
Collections.emptyMap());
}
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/handler/admin/mysql/MySQLAdminExecutorCreatorTest.java
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/handler/admin/mysql/MySQLAdminExecutorCreatorTest.java
index 55ee93f15f3..0cba04bb547 100644
---
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/handler/admin/mysql/MySQLAdminExecutorCreatorTest.java
+++
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/handler/admin/mysql/MySQLAdminExecutorCreatorTest.java
@@ -258,7 +258,7 @@ public final class MySQLAdminExecutorCreatorTest extends
ProxyContextRestorer {
@Test
public void assertCreateWithOtherSelectStatementForDatabaseName() {
Map<String, ShardingSphereDatabase> result = new LinkedHashMap<>(1, 1);
- ShardingSphereResource resource = new
ShardingSphereResource(Collections.singletonMap("ds", new MockedDataSource()));
+ ShardingSphereResource resource = new
ShardingSphereResource("sharding_db", Collections.singletonMap("ds", new
MockedDataSource()));
ShardingSphereDatabase database = new ShardingSphereDatabase("db_0",
mock(DatabaseType.class), resource, mock(ShardingSphereRuleMetaData.class),
Collections.emptyMap());
result.put("db_0", database);
initProxyContext(result);
@@ -275,7 +275,7 @@ public final class MySQLAdminExecutorCreatorTest extends
ProxyContextRestorer {
@Test
public void assertCreateWithOtherSelectStatementForNullDatabaseName() {
Map<String, ShardingSphereDatabase> result = new LinkedHashMap<>(1, 1);
- ShardingSphereResource resource = new
ShardingSphereResource(Collections.singletonMap("ds_0", new
MockedDataSource()));
+ ShardingSphereResource resource = new
ShardingSphereResource("sharding_db", Collections.singletonMap("ds_0", new
MockedDataSource()));
ShardingSphereDatabase database = new ShardingSphereDatabase("db_0",
mock(DatabaseType.class), resource, mock(ShardingSphereRuleMetaData.class),
Collections.emptyMap());
result.put("db_0", database);
initProxyContext(result);
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/handler/admin/mysql/executor/information/SelectInformationSchemataExecutorTest.java
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/handler/admin/mysql/executor/information/SelectInformationSchemataExecutorTest.java
index c5c9fa184cf..a5ebc379e53 100644
---
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/handler/admin/mysql/executor/information/SelectInformationSchemataExecutorTest.java
+++
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/handler/admin/mysql/executor/information/SelectInformationSchemataExecutorTest.java
@@ -139,11 +139,11 @@ public final class SelectInformationSchemataExecutorTest
extends ProxyContextRes
}
private ShardingSphereDatabase createDatabase(final String databaseName) {
- return createDatabase(databaseName, new
ShardingSphereResource(Collections.emptyMap()));
+ return createDatabase(databaseName, new
ShardingSphereResource("sharding_db", Collections.emptyMap()));
}
private ShardingSphereDatabase createDatabase(final Map<String, String>
expectedResultSetMap) throws SQLException {
- return createDatabase("auth_db", new
ShardingSphereResource(Collections.singletonMap("foo_ds", new
MockedDataSource(mockConnection(expectedResultSetMap)))));
+ return createDatabase("auth_db", new
ShardingSphereResource("sharding_db", Collections.singletonMap("foo_ds", new
MockedDataSource(mockConnection(expectedResultSetMap)))));
}
private Connection mockConnection(final Map<String, String>
expectedResultSetMap) throws SQLException {
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/handler/admin/postgresql/executor/SelectDatabaseExecutorTest.java
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/handler/admin/postgresql/executor/SelectDatabaseExecutorTest.java
index a089f9fe8b7..3ed72e3ddce 100644
---
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/handler/admin/postgresql/executor/SelectDatabaseExecutorTest.java
+++
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/handler/admin/postgresql/executor/SelectDatabaseExecutorTest.java
@@ -96,13 +96,13 @@ public final class SelectDatabaseExecutorTest extends
ProxyContextRestorer {
private void addShardingDatabase() throws SQLException {
Map<String, ShardingSphereDatabase> databases =
ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData().getDatabases();
databases.put("sharding_db", new ShardingSphereDatabase("sharding_db",
new PostgreSQLDatabaseType(),
- new ShardingSphereResource(Collections.singletonMap("foo_ds",
new MockedDataSource(mockConnection()))),
mock(ShardingSphereRuleMetaData.class), Collections.emptyMap()));
+ new ShardingSphereResource("sharding_db",
Collections.singletonMap("foo_ds", new MockedDataSource(mockConnection()))),
mock(ShardingSphereRuleMetaData.class), Collections.emptyMap()));
}
private void addEmptyDatabase() {
Map<String, ShardingSphereDatabase> databases =
ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData().getDatabases();
databases.put("empty_db", new ShardingSphereDatabase("empty_db", new
PostgreSQLDatabaseType(),
- new ShardingSphereResource(Collections.emptyMap()), new
ShardingSphereRuleMetaData(Collections.emptyList()), Collections.emptyMap()));
+ new ShardingSphereResource("sharding_db",
Collections.emptyMap()), new
ShardingSphereRuleMetaData(Collections.emptyList()), Collections.emptyMap()));
}
private Connection mockConnection() throws SQLException {
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/handler/admin/postgresql/executor/SelectTableExecutorTest.java
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/handler/admin/postgresql/executor/SelectTableExecutorTest.java
index b62915c40a6..a7482d793b6 100644
---
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/handler/admin/postgresql/executor/SelectTableExecutorTest.java
+++
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/handler/admin/postgresql/executor/SelectTableExecutorTest.java
@@ -87,7 +87,7 @@ public final class SelectTableExecutorTest extends
ProxyContextRestorer {
}
private ShardingSphereDatabase createDatabase() throws SQLException {
- return new ShardingSphereDatabase("public", new
PostgreSQLDatabaseType(), new
ShardingSphereResource(Collections.singletonMap("foo_ds", new
MockedDataSource(mockConnection()))),
+ return new ShardingSphereDatabase("public", new
PostgreSQLDatabaseType(), new ShardingSphereResource("sharding_db",
Collections.singletonMap("foo_ds", new MockedDataSource(mockConnection()))),
new ShardingSphereRuleMetaData(Collections.emptyList()),
Collections.singletonMap("public",
new
ShardingSphereSchema(Collections.singletonMap("t_order",
mock(ShardingSphereTable.class)), Collections.emptyMap())));
}
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/SetVariableBackendHandlerTest.java
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/SetVariableBackendHandlerTest.java
index 665a1f624e8..e561c017bc9 100644
---
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/SetVariableBackendHandlerTest.java
+++
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/SetVariableBackendHandlerTest.java
@@ -76,7 +76,7 @@ public final class SetVariableBackendHandlerTest extends
ProxyContextRestorer {
Map<String, ShardingSphereDatabase> result = new HashMap<>(10, 1);
for (int i = 0; i < 10; i++) {
ShardingSphereDatabase database =
mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS);
- when(database.getResource()).thenReturn(new
ShardingSphereResource(Collections.emptyMap()));
+ when(database.getResource()).thenReturn(new
ShardingSphereResource("sharding_db", Collections.emptyMap()));
when(database.getRuleMetaData()).thenReturn(new
ShardingSphereRuleMetaData(Collections.emptyList()));
when(database.getSchema(DefaultDatabase.LOGIC_NAME)).thenReturn(mock(ShardingSphereSchema.class));
result.put(String.format(DATABASE_PATTERN, i), database);
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rql/DataSourceQueryResultSetTest.java
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rql/DataSourceQueryResultSetTest.java
index 980de9c9135..50c782e774b 100644
---
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rql/DataSourceQueryResultSetTest.java
+++
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rql/DataSourceQueryResultSetTest.java
@@ -46,7 +46,7 @@ public final class DataSourceQueryResultSetTest {
@Before
public void before() {
- ShardingSphereResource resource = new
ShardingSphereResource(Collections.singletonMap("foo_ds", createDataSource()));
+ ShardingSphereResource resource = new
ShardingSphereResource("sharding_db", Collections.singletonMap("foo_ds",
createDataSource()));
when(database.getResource()).thenReturn(resource);
}
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rql/RulesUsedResourceQueryResultSetTest.java
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rql/RulesUsedResourceQueryResultSetTest.java
index dd0c9a9e233..eb105cd7999 100644
---
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rql/RulesUsedResourceQueryResultSetTest.java
+++
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rql/RulesUsedResourceQueryResultSetTest.java
@@ -78,7 +78,7 @@ public final class RulesUsedResourceQueryResultSetTest {
ShardingSphereRuleMetaData ruleMetaData = new
ShardingSphereRuleMetaData(
Arrays.asList(mockShardingRule(),
mockReadwriteSplittingRule(), mockDatabaseDiscoveryRule(), mockEncryptRule(),
mockShadowRule()));
when(result.getRuleMetaData()).thenReturn(ruleMetaData);
- ShardingSphereResource resource = new
ShardingSphereResource(Collections.singletonMap("foo_ds", new
MockedDataSource()));
+ ShardingSphereResource resource = new
ShardingSphereResource("sharding_db", Collections.singletonMap("foo_ds", new
MockedDataSource()));
when(result.getResource()).thenReturn(resource);
return result;
}
@@ -181,7 +181,7 @@ public final class RulesUsedResourceQueryResultSetTest {
private ShardingSphereDatabase mockEmptyDatabase() {
ShardingSphereDatabase result = mock(ShardingSphereDatabase.class);
when(result.getRuleMetaData()).thenReturn(new
ShardingSphereRuleMetaData(Collections.emptyList()));
- ShardingSphereResource resource = new
ShardingSphereResource(Collections.singletonMap("empty_ds", new
MockedDataSource()));
+ ShardingSphereResource resource = new
ShardingSphereResource("sharding_db", Collections.singletonMap("empty_ds", new
MockedDataSource()));
when(result.getResource()).thenReturn(resource);
return result;
}
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rql/UnusedDataSourceQueryResultSetTest.java
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rql/UnusedDataSourceQueryResultSetTest.java
index 8f1fa46489e..972fa016869 100644
---
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rql/UnusedDataSourceQueryResultSetTest.java
+++
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rql/UnusedDataSourceQueryResultSetTest.java
@@ -57,7 +57,7 @@ public final class UnusedDataSourceQueryResultSetTest {
@Before
public void before() {
- ShardingSphereResource resource = new
ShardingSphereResource(createDataSources());
+ ShardingSphereResource resource = new
ShardingSphereResource("sharding_db", createDataSources());
ShardingSphereRuleMetaData metaData = new
ShardingSphereRuleMetaData(Collections.singleton(createShardingRule()));
when(database.getResource()).thenReturn(resource);
when(database.getRuleMetaData()).thenReturn(metaData);
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 9a54374bca0..ef272b93003 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
@@ -49,7 +49,7 @@ public final class Bootstrap {
YamlProxyConfiguration yamlConfig =
ProxyConfigurationLoader.load(bootstrapArgs.getConfigurationPath());
int port = bootstrapArgs.getPort().orElseGet(() -> new
ConfigurationProperties(yamlConfig.getServerConfiguration().getProps()).getValue(ConfigurationPropertyKey.PROXY_DEFAULT_PORT));
List<String> addresses = bootstrapArgs.getAddresses();
- new BootstrapInitializer().init(yamlConfig, port);
+ new BootstrapInitializer().init(yamlConfig, port,
bootstrapArgs.getForce());
new ShardingSphereProxy().start(port, addresses);
}
}
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/java/org/apache/shardingsphere/proxy/arguments/BootstrapArguments.java
b/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/java/org/apache/shardingsphere/proxy/arguments/BootstrapArguments.java
index 2db76610d70..ca0041e1b4b 100644
---
a/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/java/org/apache/shardingsphere/proxy/arguments/BootstrapArguments.java
+++
b/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/java/org/apache/shardingsphere/proxy/arguments/BootstrapArguments.java
@@ -74,6 +74,19 @@ public final class BootstrapArguments {
return args.length < 3 ?
Collections.singletonList(DEFAULT_BIND_ADDRESS) :
Arrays.asList(args[2].split(","));
}
+ /**
+ * Get force startup parameter.
+ *
+ * @return force parameter
+ */
+ public boolean getForce() {
+ return args.length >= 4 && parseForceParameter(args[3]);
+ }
+
+ private boolean parseForceParameter(final String forceParam) {
+ return Boolean.TRUE.toString().equalsIgnoreCase(forceParam.trim());
+ }
+
private String paddingWithSlash(final String pathArg) {
StringBuilder result = new StringBuilder(pathArg);
if (!pathArg.startsWith("/")) {
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 b17aae1cd97..44a942f19d1 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
@@ -49,21 +49,22 @@ public final class BootstrapInitializer {
*
* @param yamlConfig YAML proxy configuration
* @param port proxy port
+ * @param force force start
* @throws SQLException SQL exception
*/
- public void init(final YamlProxyConfiguration yamlConfig, final int port)
throws SQLException {
+ public void init(final YamlProxyConfiguration yamlConfig, final int port,
final boolean force) throws SQLException {
ModeConfiguration modeConfig = null ==
yamlConfig.getServerConfiguration().getMode() ? null : new
YamlModeConfigurationSwapper().swapToObject(yamlConfig.getServerConfiguration().getMode());
ProxyConfiguration proxyConfig = new
YamlProxyConfigurationSwapper().swap(yamlConfig);
- ContextManager contextManager = createContextManager(proxyConfig,
modeConfig, port);
+ ContextManager contextManager = createContextManager(proxyConfig,
modeConfig, port, force);
ProxyContext.init(contextManager);
contextManagerInitializedCallback(modeConfig, contextManager);
ShardingSphereProxyVersion.setVersion(contextManager);
}
- private ContextManager createContextManager(final ProxyConfiguration
proxyConfig, final ModeConfiguration modeConfig, final int port) throws
SQLException {
+ private ContextManager createContextManager(final ProxyConfiguration
proxyConfig, final ModeConfiguration modeConfig, final int port, final boolean
force) throws SQLException {
ContextManagerBuilderParameter parameter = new
ContextManagerBuilderParameter(modeConfig,
proxyConfig.getDatabaseConfigurations(),
proxyConfig.getGlobalConfiguration().getRules(),
proxyConfig.getGlobalConfiguration().getProperties(),
proxyConfig.getGlobalConfiguration().getLabels(),
- createInstanceMetaData(proxyConfig, port));
+ createInstanceMetaData(proxyConfig, port), force);
return
ContextManagerBuilderFactory.getInstance(modeConfig).build(parameter);
}
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-opengauss/src/test/java/org/apache/shardingsphere/proxy/frontend/opengauss/authentication/OpenGaussAuthenticationHandlerTest.java
b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-opengauss/src/test/java/org/apache/shardingsphere/proxy/frontend/opengauss/authentication/OpenGaussAuthenticationHandlerTest.java
index 38f57d95b8b..84a87435309 100644
---
a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-opengauss/src/test/java/org/apache/shardingsphere/proxy/frontend/opengauss/authentication/OpenGaussAuthenticationHandlerTest.java
+++
b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-opengauss/src/test/java/org/apache/shardingsphere/proxy/frontend/opengauss/authentication/OpenGaussAuthenticationHandlerTest.java
@@ -131,7 +131,7 @@ public final class OpenGaussAuthenticationHandlerTest
extends ProxyContextRestor
for (int i = 0; i < 10; i++) {
ShardingSphereDatabase database =
mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS);
ShardingSphereSchema schema = mock(ShardingSphereSchema.class);
- when(database.getResource()).thenReturn(new
ShardingSphereResource(Collections.emptyMap()));
+ when(database.getResource()).thenReturn(new
ShardingSphereResource("sharding_db", Collections.emptyMap()));
when(database.getRuleMetaData()).thenReturn(new
ShardingSphereRuleMetaData(Collections.emptyList()));
when(database.getSchema(DefaultDatabase.LOGIC_NAME)).thenReturn(schema);
when(schema.getTables()).thenReturn(Collections.emptyMap());
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/authentication/PostgreSQLAuthenticationHandlerTest.java
b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/authentication/PostgreSQLAuthenticationHandlerTest.java
index d6d7328e183..f424ce7fe1c 100644
---
a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/authentication/PostgreSQLAuthenticationHandlerTest.java
+++
b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/authentication/PostgreSQLAuthenticationHandlerTest.java
@@ -138,7 +138,7 @@ public final class PostgreSQLAuthenticationHandlerTest
extends ProxyContextResto
for (int i = 0; i < 10; i++) {
ShardingSphereDatabase database =
mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS);
ShardingSphereSchema schema = mock(ShardingSphereSchema.class);
- when(database.getResource()).thenReturn(new
ShardingSphereResource(Collections.emptyMap()));
+ when(database.getResource()).thenReturn(new
ShardingSphereResource("sharding_db", Collections.emptyMap()));
when(database.getRuleMetaData()).thenReturn(new
ShardingSphereRuleMetaData(Collections.emptyList()));
when(database.getSchema(DefaultDatabase.LOGIC_NAME)).thenReturn(schema);
when(schema.getTables()).thenReturn(Collections.emptyMap());