You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by ji...@apache.org on 2022/05/26 09:51:59 UTC
[shardingsphere] branch master updated: Refactor ExportableConstant (#17838)
This is an automated email from the ASF dual-hosted git repository.
jianglongtao 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 8442124985c Refactor ExportableConstant (#17838)
8442124985c is described below
commit 8442124985cab30ef10bcbfa6229fe984d06df4a
Author: lanchengx <52...@users.noreply.github.com>
AuthorDate: Thu May 26 17:51:50 2022 +0800
Refactor ExportableConstant (#17838)
* Refactor `ExportableConstant`.
* Refactor `ExportableConstant`.
* Refactor `ExportableConstant`.
* Rename constant name.
* Rename.
* Compile error.
---
.../dbdiscovery/rule/DatabaseDiscoveryRule.java | 2 +-
.../rule/DatabaseDiscoveryRuleTest.java | 2 +-
.../query/DatabaseDiscoveryRuleQueryResultSet.java | 4 +-
.../DropDatabaseDiscoveryRuleStatementUpdater.java | 15 +++--
.../DatabaseDiscoveryRuleExportableFixture.java | 42 ++++++++++++++
.../ReadwriteSplittingRuleExportableFixture.java | 59 +++++++++++++++++++
.../DatabaseDiscoveryRuleQueryResultSetTest.java | 9 +--
...pDatabaseDiscoveryRuleStatementUpdaterTest.java | 12 ++++
.../rule/ReadwriteSplittingDataSourceRule.java | 21 -------
.../rule/ReadwriteSplittingRule.java | 35 +++---------
.../ReadwriteSplittingRuleQueryResultSet.java | 14 +++--
...lterReadwriteSplittingRuleStatementUpdater.java | 6 +-
...eateReadwriteSplittingRuleStatementUpdater.java | 9 ++-
.../DatabaseDiscoveryRuleExportableFixture.java | 42 ++++++++++++++
.../ReadwriteSplittingRuleExportableFixture.java | 66 ++++++++++++++++++++++
.../ReadwriteSplittingRuleQueryResultSetTest.java | 23 ++------
...ReadwriteSplittingRuleStatementUpdaterTest.java | 9 +++
...ReadwriteSplittingRuleStatementUpdaterTest.java | 34 +++++++++++
.../distsql/constant/ExportableConstants.java | 16 ++----
...Constants.java => ExportableItemConstants.java} | 16 +-----
.../singletable/rule/SingleTableRule.java | 2 +-
.../queryable/CountInstanceRulesHandler.java | 2 +-
...ShowReadwriteSplittingReadResourcesHandler.java | 28 ++++-----
.../SetReadwriteSplittingStatusHandler.java | 21 +++----
.../rql/rule/SchemaRulesCountResultSet.java | 2 +-
.../common/queryable/CountInstanceRulesTest.java | 2 +-
.../distsql/rql/SchemaRulesCountResultSetTest.java | 2 +-
27 files changed, 344 insertions(+), 151 deletions(-)
diff --git a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/main/java/org/apache/shardingsphere/dbdiscovery/rule/DatabaseDiscoveryRule.java b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/main/java/org/apache/shardingsphere/dbdiscovery/rule/DatabaseDiscoveryRule.java
index ec3b20412b3..ed6deb33e2c 100644
--- a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/main/java/org/apache/shardingsphere/dbdiscovery/rule/DatabaseDiscoveryRule.java
+++ b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/main/java/org/apache/shardingsphere/dbdiscovery/rule/DatabaseDiscoveryRule.java
@@ -176,7 +176,7 @@ public final class DatabaseDiscoveryRule implements SchemaRule, DataSourceContai
@Override
public Map<String, Supplier<Object>> getExportedMethods() {
- return Collections.singletonMap(ExportableConstants.EXPORTABLE_KEY_PRIMARY_DATA_SOURCE, this::exportPrimaryDataSourceMap);
+ return Collections.singletonMap(ExportableConstants.EXPORT_DB_DISCOVERY_PRIMARY_DATA_SOURCES, this::exportPrimaryDataSourceMap);
}
private Map<String, String> exportPrimaryDataSourceMap() {
diff --git a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/test/java/org/apache/shardingsphere/dbdiscovery/rule/DatabaseDiscoveryRuleTest.java b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/test/java/org/apache/shardingsphere/dbdiscovery/rule/DatabaseDiscoveryRuleTest.java
index 4c67979c157..a5873459c50 100644
--- a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/test/java/org/apache/shardingsphere/dbdiscovery/rule/DatabaseDiscoveryRuleTest.java
+++ b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/test/java/org/apache/shardingsphere/dbdiscovery/rule/DatabaseDiscoveryRuleTest.java
@@ -77,7 +77,7 @@ public final class DatabaseDiscoveryRuleTest {
@Test
public void assertGetExportedMethods() {
DatabaseDiscoveryRule databaseDiscoveryRule = createRule();
- assertThat(databaseDiscoveryRule.getExportedMethods().get(ExportableConstants.EXPORTABLE_KEY_PRIMARY_DATA_SOURCE).get(), is(Collections.singletonMap("test_pr", "primary")));
+ assertThat(databaseDiscoveryRule.getExportedMethods().get(ExportableConstants.EXPORT_DB_DISCOVERY_PRIMARY_DATA_SOURCES).get(), is(Collections.singletonMap("test_pr", "primary")));
}
private DatabaseDiscoveryRule createRule() {
diff --git a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-distsql/shardingsphere-db-discovery-distsql-handler/src/main/java/org/apache/shardingsphere/dbdiscovery/distsql/handler/query/DatabaseDiscoveryRuleQueryResultSet.java b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-distsql/shardingsphere-db-discovery-distsql-handler/src/main/java/org/apache/shardingsphere/dbdiscovery/distsql/handler/query/DatabaseDiscoveryRuleQuer [...]
index 25ef6a6e332..0fabcbbd7f5 100644
--- a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-distsql/shardingsphere-db-discovery-distsql-handler/src/main/java/org/apache/shardingsphere/dbdiscovery/distsql/handler/query/DatabaseDiscoveryRuleQueryResultSet.java
+++ b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-distsql/shardingsphere-db-discovery-distsql-handler/src/main/java/org/apache/shardingsphere/dbdiscovery/distsql/handler/query/DatabaseDiscoveryRuleQueryResultSet.java
@@ -72,9 +72,9 @@ public final class DatabaseDiscoveryRuleQueryResultSet implements DistSQLResultS
discoveryHeartbeats = ruleConfig.map(DatabaseDiscoveryRuleConfiguration::getDiscoveryHeartbeats).orElseGet(Collections::emptyMap);
Optional<ExportableRule> exportableRule = database.getRuleMetaData().getRules()
.stream().filter(each -> each instanceof ExportableRule)
- .filter(each -> ((ExportableRule) each).containExportableKey(Collections.singleton(ExportableConstants.EXPORTABLE_KEY_PRIMARY_DATA_SOURCE)))
+ .filter(each -> ((ExportableRule) each).containExportableKey(Collections.singleton(ExportableConstants.EXPORT_DB_DISCOVERY_PRIMARY_DATA_SOURCES)))
.map(each -> (ExportableRule) each).findAny();
- primaryDataSources = (Map<String, String>) (exportableRule.map(optional -> optional.export(ExportableConstants.EXPORTABLE_KEY_PRIMARY_DATA_SOURCE).orElseGet(Collections::emptyMap))
+ primaryDataSources = (Map<String, String>) (exportableRule.map(optional -> optional.export(ExportableConstants.EXPORT_DB_DISCOVERY_PRIMARY_DATA_SOURCES).orElseGet(Collections::emptyMap))
.orElseGet(Collections::emptyMap));
}
diff --git a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-distsql/shardingsphere-db-discovery-distsql-handler/src/main/java/org/apache/shardingsphere/dbdiscovery/distsql/handler/update/DropDatabaseDiscoveryRuleStatementUpdater.java b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-distsql/shardingsphere-db-discovery-distsql-handler/src/main/java/org/apache/shardingsphere/dbdiscovery/distsql/handler/update/DropDatabaseDisco [...]
index 6ce7160eee4..fbf64f68068 100644
--- a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-distsql/shardingsphere-db-discovery-distsql-handler/src/main/java/org/apache/shardingsphere/dbdiscovery/distsql/handler/update/DropDatabaseDiscoveryRuleStatementUpdater.java
+++ b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-distsql/shardingsphere-db-discovery-distsql-handler/src/main/java/org/apache/shardingsphere/dbdiscovery/distsql/handler/update/DropDatabaseDiscoveryRuleStatementUpdater.java
@@ -21,6 +21,7 @@ import org.apache.shardingsphere.dbdiscovery.api.config.DatabaseDiscoveryRuleCon
import org.apache.shardingsphere.dbdiscovery.api.config.rule.DatabaseDiscoveryDataSourceRuleConfiguration;
import org.apache.shardingsphere.dbdiscovery.distsql.parser.statement.DropDatabaseDiscoveryRuleStatement;
import org.apache.shardingsphere.infra.distsql.constant.ExportableConstants;
+import org.apache.shardingsphere.infra.distsql.constant.ExportableItemConstants;
import org.apache.shardingsphere.infra.distsql.exception.DistSQLException;
import org.apache.shardingsphere.infra.distsql.exception.rule.RequiredRuleMissedException;
import org.apache.shardingsphere.infra.distsql.exception.rule.RuleInUsedException;
@@ -28,8 +29,10 @@ import org.apache.shardingsphere.infra.distsql.update.RuleDefinitionDropUpdater;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.rule.identifier.type.ExportableRule;
+import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
+import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
@@ -65,10 +68,14 @@ public final class DropDatabaseDiscoveryRuleStatementUpdater implements RuleDefi
}
private void checkIsInUse(final String databaseName, final DropDatabaseDiscoveryRuleStatement sqlStatement, final ShardingSphereDatabase database) throws DistSQLException {
- Collection<String> rulesInUse = database.getRuleMetaData().findRules(ExportableRule.class).stream()
- .filter(each -> each.containExportableKey(Collections.singletonList(ExportableConstants.EXPORTABLE_KEY_AUTO_AWARE_DATA_SOURCE_NAME)))
- .map(each -> each.export(ExportableConstants.EXPORTABLE_KEY_AUTO_AWARE_DATA_SOURCE_NAME)).filter(Optional::isPresent)
- .map(each -> (Collection<String>) each.get()).flatMap(Collection::stream).collect(Collectors.toSet());
+ Optional<ExportableRule> exportableRule = database.getRuleMetaData().findRules(ExportableRule.class).stream()
+ .filter(each -> each.containExportableKey(Collections.singletonList(ExportableConstants.EXPORT_DYNAMIC_READWRITE_SPLITTING_RULE))).findFirst();
+ Collection<String> rulesInUse = new ArrayList<>();
+ exportableRule.ifPresent(op -> {
+ Map<String, Map<String, String>> readwriteRuleMap = op.export(ExportableConstants.EXPORT_DYNAMIC_READWRITE_SPLITTING_RULE)
+ .map(each -> (Map<String, Map<String, String>>) each).orElse(Collections.emptyMap());
+ readwriteRuleMap.values().stream().map(each -> each.get(ExportableItemConstants.AUTO_AWARE_DATA_SOURCE_NAME)).forEach(rulesInUse::add);
+ });
Collection<String> invalid = sqlStatement.getRuleNames().stream().filter(rulesInUse::contains).collect(Collectors.toList());
DistSQLException.predictionThrow(invalid.isEmpty(), () -> new RuleInUsedException(RULE_TYPE, databaseName, invalid));
}
diff --git a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-distsql/shardingsphere-db-discovery-distsql-handler/src/test/java/org/apache/shardingsphere/dbdiscovery/distsql/handler/fixture/DatabaseDiscoveryRuleExportableFixture.java b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-distsql/shardingsphere-db-discovery-distsql-handler/src/test/java/org/apache/shardingsphere/dbdiscovery/distsql/handler/fixture/DatabaseDiscoveryR [...]
new file mode 100644
index 00000000000..89bd7d366ad
--- /dev/null
+++ b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-distsql/shardingsphere-db-discovery-distsql-handler/src/test/java/org/apache/shardingsphere/dbdiscovery/distsql/handler/fixture/DatabaseDiscoveryRuleExportableFixture.java
@@ -0,0 +1,42 @@
+/*
+ * 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.dbdiscovery.distsql.handler.fixture;
+
+import org.apache.shardingsphere.infra.distsql.constant.ExportableConstants;
+import org.apache.shardingsphere.infra.rule.identifier.type.ExportableRule;
+
+import java.util.Collections;
+import java.util.Map;
+import java.util.function.Supplier;
+
+public final class DatabaseDiscoveryRuleExportableFixture implements ExportableRule {
+
+ @Override
+ public String getType() {
+ return null;
+ }
+
+ @Override
+ public Map<String, Supplier<Object>> getExportedMethods() {
+ return Collections.singletonMap(ExportableConstants.EXPORT_DB_DISCOVERY_PRIMARY_DATA_SOURCES, this::exportedMethod);
+ }
+
+ private Map<String, String> exportedMethod() {
+ return Collections.singletonMap("ms_group", "ds_0");
+ }
+}
diff --git a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-distsql/shardingsphere-db-discovery-distsql-handler/src/test/java/org/apache/shardingsphere/dbdiscovery/distsql/handler/fixture/ReadwriteSplittingRuleExportableFixture.java b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-distsql/shardingsphere-db-discovery-distsql-handler/src/test/java/org/apache/shardingsphere/dbdiscovery/distsql/handler/fixture/ReadwriteSplittin [...]
new file mode 100644
index 00000000000..79fc282b778
--- /dev/null
+++ b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-distsql/shardingsphere-db-discovery-distsql-handler/src/test/java/org/apache/shardingsphere/dbdiscovery/distsql/handler/fixture/ReadwriteSplittingRuleExportableFixture.java
@@ -0,0 +1,59 @@
+/*
+ * 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.dbdiscovery.distsql.handler.fixture;
+
+import org.apache.shardingsphere.infra.distsql.constant.ExportableConstants;
+import org.apache.shardingsphere.infra.distsql.constant.ExportableItemConstants;
+import org.apache.shardingsphere.infra.rule.identifier.type.ExportableRule;
+
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.LinkedHashMap;
+import java.util.Map;
+import java.util.function.Supplier;
+
+public final class ReadwriteSplittingRuleExportableFixture implements ExportableRule {
+
+ @Override
+ public String getType() {
+ return null;
+ }
+
+ @Override
+ public Map<String, Supplier<Object>> getExportedMethods() {
+ Map<String, Supplier<Object>> result = new HashMap<>(4, 1);
+ result.put(ExportableConstants.EXPORT_DYNAMIC_READWRITE_SPLITTING_RULE, this::exportDynamicDataSources);
+ result.put(ExportableConstants.EXPORT_STATIC_READWRITE_SPLITTING_RULE, this::exportStaticDataSources);
+ return result;
+ }
+
+ private Map<String, Map<String, String>> exportDynamicDataSources() {
+ Map<String, String> dynamicRule = new LinkedHashMap<>(3, 1);
+ dynamicRule.put(ExportableItemConstants.AUTO_AWARE_DATA_SOURCE_NAME, "ha_group");
+ dynamicRule.put(ExportableItemConstants.PRIMARY_DATA_SOURCE_NAME, "ds_0");
+ dynamicRule.put(ExportableItemConstants.REPLICA_DATA_SOURCE_NAMES, "ds_1,ds_2");
+ return Collections.singletonMap("dynamic_rule_1", dynamicRule);
+ }
+
+ private Map<String, Map<String, String>> exportStaticDataSources() {
+ Map<String, String> staticRule = new LinkedHashMap<>(2, 1);
+ staticRule.put(ExportableItemConstants.PRIMARY_DATA_SOURCE_NAME, "ds_0");
+ staticRule.put(ExportableItemConstants.REPLICA_DATA_SOURCE_NAMES, "ds_1");
+ return Collections.singletonMap("static_rule_1", staticRule);
+ }
+}
diff --git a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-distsql/shardingsphere-db-discovery-distsql-handler/src/test/java/org/apache/shardingsphere/dbdiscovery/distsql/handler/query/DatabaseDiscoveryRuleQueryResultSetTest.java b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-distsql/shardingsphere-db-discovery-distsql-handler/src/test/java/org/apache/shardingsphere/dbdiscovery/distsql/handler/query/DatabaseDiscoveryRule [...]
index 759be7b6987..2a741194739 100644
--- a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-distsql/shardingsphere-db-discovery-distsql-handler/src/test/java/org/apache/shardingsphere/dbdiscovery/distsql/handler/query/DatabaseDiscoveryRuleQueryResultSetTest.java
+++ b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-distsql/shardingsphere-db-discovery-distsql-handler/src/test/java/org/apache/shardingsphere/dbdiscovery/distsql/handler/query/DatabaseDiscoveryRuleQueryResultSetTest.java
@@ -20,12 +20,12 @@ package org.apache.shardingsphere.dbdiscovery.distsql.handler.query;
import org.apache.shardingsphere.dbdiscovery.api.config.DatabaseDiscoveryRuleConfiguration;
import org.apache.shardingsphere.dbdiscovery.api.config.rule.DatabaseDiscoveryDataSourceRuleConfiguration;
import org.apache.shardingsphere.dbdiscovery.api.config.rule.DatabaseDiscoveryHeartBeatConfiguration;
+import org.apache.shardingsphere.dbdiscovery.distsql.handler.fixture.DatabaseDiscoveryRuleExportableFixture;
import org.apache.shardingsphere.dbdiscovery.distsql.parser.statement.ShowDatabaseDiscoveryRulesStatement;
import org.apache.shardingsphere.infra.config.RuleConfiguration;
import org.apache.shardingsphere.infra.config.algorithm.ShardingSphereAlgorithmConfiguration;
import org.apache.shardingsphere.infra.distsql.query.DistSQLResultSet;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
-import org.apache.shardingsphere.infra.rule.identifier.type.ExportableRule;
import org.junit.Test;
import java.util.ArrayList;
@@ -39,7 +39,6 @@ import java.util.Properties;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
-import static org.mockito.ArgumentMatchers.anyCollection;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@@ -50,9 +49,7 @@ public final class DatabaseDiscoveryRuleQueryResultSetTest {
public void assertGetRowData() {
ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS);
when(database.getRuleMetaData().getConfigurations()).thenReturn(Collections.singleton(createRuleConfiguration()));
- ExportableRule exportableRule = mock(ExportableRule.class);
- when(exportableRule.export(anyCollection())).thenReturn(Collections.emptyMap());
- when(database.getRuleMetaData().getRules()).thenReturn(Collections.singleton(exportableRule));
+ when(database.getRuleMetaData().getRules()).thenReturn(Collections.singleton(new DatabaseDiscoveryRuleExportableFixture()));
DistSQLResultSet resultSet = new DatabaseDiscoveryRuleQueryResultSet();
resultSet.init(database, mock(ShowDatabaseDiscoveryRulesStatement.class));
Collection<String> columnNames = resultSet.getColumnNames();
@@ -62,7 +59,7 @@ public final class DatabaseDiscoveryRuleQueryResultSetTest {
assertThat(actual.size(), is(5));
assertThat(actual.get(0), is("ms_group"));
assertThat(actual.get(1), is("ds_0,ds_1"));
- assertThat(actual.get(2), is(""));
+ assertThat(actual.get(2), is("ds_0"));
assertThat(actual.get(3).toString(), is("{name=type_test, type=MySQL.MGR, props={}}"));
assertThat(actual.get(4).toString(), is("{name=heartbeat_test, props={}}"));
}
diff --git a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-distsql/shardingsphere-db-discovery-distsql-handler/src/test/java/org/apache/shardingsphere/dbdiscovery/distsql/handler/update/DropDatabaseDiscoveryRuleStatementUpdaterTest.java b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-distsql/shardingsphere-db-discovery-distsql-handler/src/test/java/org/apache/shardingsphere/dbdiscovery/distsql/handler/update/DropDatabaseD [...]
index 2bdc5591bad..81d2ec24144 100644
--- a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-distsql/shardingsphere-db-discovery-distsql-handler/src/test/java/org/apache/shardingsphere/dbdiscovery/distsql/handler/update/DropDatabaseDiscoveryRuleStatementUpdaterTest.java
+++ b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-distsql/shardingsphere-db-discovery-distsql-handler/src/test/java/org/apache/shardingsphere/dbdiscovery/distsql/handler/update/DropDatabaseDiscoveryRuleStatementUpdaterTest.java
@@ -19,12 +19,15 @@ package org.apache.shardingsphere.dbdiscovery.distsql.handler.update;
import org.apache.shardingsphere.dbdiscovery.api.config.DatabaseDiscoveryRuleConfiguration;
import org.apache.shardingsphere.dbdiscovery.api.config.rule.DatabaseDiscoveryDataSourceRuleConfiguration;
+import org.apache.shardingsphere.dbdiscovery.distsql.handler.fixture.ReadwriteSplittingRuleExportableFixture;
import org.apache.shardingsphere.dbdiscovery.distsql.parser.statement.DropDatabaseDiscoveryRuleStatement;
import org.apache.shardingsphere.infra.config.algorithm.ShardingSphereAlgorithmConfiguration;
import org.apache.shardingsphere.infra.distsql.exception.DistSQLException;
import org.apache.shardingsphere.infra.distsql.exception.rule.RequiredRuleMissedException;
+import org.apache.shardingsphere.infra.distsql.exception.rule.RuleInUsedException;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.metadata.database.rule.ShardingSphereRuleMetaData;
+import org.apache.shardingsphere.infra.rule.identifier.type.ExportableRule;
import org.junit.Before;
import org.junit.Test;
@@ -64,6 +67,15 @@ public final class DropDatabaseDiscoveryRuleStatementUpdaterTest {
updater.checkSQLStatement(database, createSQLStatement(), new DatabaseDiscoveryRuleConfiguration(Collections.emptyList(), Collections.emptyMap(), Collections.emptyMap()));
}
+ @Test(expected = RuleInUsedException.class)
+ public void assertCheckSQLStatementWithRuleInUsed() throws DistSQLException {
+ when(database.getRuleMetaData()).thenReturn(mock(ShardingSphereRuleMetaData.class, RETURNS_DEEP_STUBS));
+ when(database.getRuleMetaData().findRules(ExportableRule.class)).thenReturn(Collections.singleton(new ReadwriteSplittingRuleExportableFixture()));
+ DatabaseDiscoveryDataSourceRuleConfiguration configuration = new DatabaseDiscoveryDataSourceRuleConfiguration("ha_group", null, null, null);
+ updater.checkSQLStatement(database, createSQLStatement(),
+ new DatabaseDiscoveryRuleConfiguration(Collections.singletonList(configuration), Collections.emptyMap(), Collections.emptyMap()));
+ }
+
@Test
public void assertUpdateCurrentRuleConfiguration() {
DatabaseDiscoveryRuleConfiguration databaseDiscoveryRuleConfig = createCurrentRuleConfiguration();
diff --git a/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/main/java/org/apache/shardingsphere/readwritesplitting/rule/ReadwriteSplittingDataSourceRule.java b/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/main/java/org/apache/shardingsphere/readwritesplitting/rule/ReadwriteSplittingDataSourceRule.java
index 92cf32b66a0..8b631893230 100644
--- a/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/main/java/org/apache/shardingsphere/readwritesplitting/rule/ReadwriteSplittingDataSourceRule.java
+++ b/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/main/java/org/apache/shardingsphere/readwritesplitting/rule/ReadwriteSplittingDataSourceRule.java
@@ -21,7 +21,6 @@ import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import lombok.AccessLevel;
import lombok.Getter;
-import org.apache.shardingsphere.infra.distsql.constant.ExportableConstants;
import org.apache.shardingsphere.readwritesplitting.api.rule.ReadwriteSplittingDataSourceRuleConfiguration;
import org.apache.shardingsphere.readwritesplitting.spi.ReadQueryLoadBalanceAlgorithm;
import org.apache.shardingsphere.readwritesplitting.strategy.ReadwriteSplittingStrategy;
@@ -29,10 +28,8 @@ import org.apache.shardingsphere.readwritesplitting.strategy.ReadwriteSplittingS
import java.util.Collection;
import java.util.HashSet;
-import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
-import java.util.Map;
import java.util.stream.Collectors;
/**
@@ -89,24 +86,6 @@ public final class ReadwriteSplittingDataSourceRule {
}
}
- /**
- * Get data sources.
- *
- * @param removeDisabled whether to remove the disabled resource
- * @return data sources
- */
- public Map<String, String> getDataSources(final boolean removeDisabled) {
- Map<String, String> result = new LinkedHashMap<>(2, 1);
- result.put(ExportableConstants.PRIMARY_DATA_SOURCE_NAME, readwriteSplittingStrategy.getWriteDataSource());
- List<String> readDataSourceNames = readwriteSplittingStrategy.getReadDataSources();
- if (removeDisabled && !disabledDataSourceNames.isEmpty()) {
- readDataSourceNames = new LinkedList<>(readDataSourceNames);
- readDataSourceNames.removeIf(disabledDataSourceNames::contains);
- }
- result.put(ExportableConstants.REPLICA_DATA_SOURCE_NAMES, String.join(",", readDataSourceNames));
- return result;
- }
-
/**
* Get enabled replica data sources.
*
diff --git a/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/main/java/org/apache/shardingsphere/readwritesplitting/rule/ReadwriteSplittingRule.java b/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/main/java/org/apache/shardingsphere/readwritesplitting/rule/ReadwriteSplittingRule.java
index e243c969499..0f1242aa39e 100644
--- a/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/main/java/org/apache/shardingsphere/readwritesplitting/rule/ReadwriteSplittingRule.java
+++ b/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/main/java/org/apache/shardingsphere/readwritesplitting/rule/ReadwriteSplittingRule.java
@@ -20,6 +20,7 @@ package org.apache.shardingsphere.readwritesplitting.rule;
import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import org.apache.shardingsphere.infra.distsql.constant.ExportableConstants;
+import org.apache.shardingsphere.infra.distsql.constant.ExportableItemConstants;
import org.apache.shardingsphere.infra.rule.event.DataSourceStatusChangedEvent;
import org.apache.shardingsphere.infra.rule.identifier.scope.SchemaRule;
import org.apache.shardingsphere.infra.rule.identifier.type.DataSourceContainedRule;
@@ -41,7 +42,6 @@ import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.function.Supplier;
-import java.util.stream.Collectors;
/**
* Readwrite-splitting rule.
@@ -118,11 +118,9 @@ public final class ReadwriteSplittingRule implements SchemaRule, DataSourceConta
@Override
public Map<String, Supplier<Object>> getExportedMethods() {
- Map<String, Supplier<Object>> result = new HashMap<>(4, 1);
- result.put(ExportableConstants.EXPORTABLE_KEY_AUTO_AWARE_DATA_SOURCE, this::exportDynamicDataSources);
- result.put(ExportableConstants.EXPORTABLE_KEY_AUTO_AWARE_DATA_SOURCE_NAME, this::exportAutoAwareDataSourceNames);
- result.put(ExportableConstants.EXPORTABLE_KEY_ENABLED_DATA_SOURCE, this::exportStaticDataSources);
- result.put(ExportableConstants.EXPORTABLE_KEY_DATA_SOURCE, this::exportAllDataSources);
+ Map<String, Supplier<Object>> result = new HashMap<>(2, 1);
+ result.put(ExportableConstants.EXPORT_DYNAMIC_READWRITE_SPLITTING_RULE, this::exportDynamicDataSources);
+ result.put(ExportableConstants.EXPORT_STATIC_READWRITE_SPLITTING_RULE, this::exportStaticDataSources);
return result;
}
@@ -131,43 +129,28 @@ public final class ReadwriteSplittingRule implements SchemaRule, DataSourceConta
for (ReadwriteSplittingDataSourceRule each : dataSourceRules.values()) {
if (each.getReadwriteSplittingStrategy() instanceof DynamicReadwriteSplittingStrategy) {
Map<String, String> exportedDataSources = new LinkedHashMap<>(2, 1);
- exportedDataSources.put(ExportableConstants.PRIMARY_DATA_SOURCE_NAME, each.getWriteDataSource());
- exportedDataSources.put(ExportableConstants.REPLICA_DATA_SOURCE_NAMES, String.join(",", each.getReadDataSourceNames()));
+ exportedDataSources.put(ExportableItemConstants.AUTO_AWARE_DATA_SOURCE_NAME, ((DynamicReadwriteSplittingStrategy) each.getReadwriteSplittingStrategy()).getAutoAwareDataSourceName());
+ exportedDataSources.put(ExportableItemConstants.PRIMARY_DATA_SOURCE_NAME, each.getWriteDataSource());
+ exportedDataSources.put(ExportableItemConstants.REPLICA_DATA_SOURCE_NAMES, String.join(",", each.getReadwriteSplittingStrategy().getReadDataSources()));
result.put(each.getName(), exportedDataSources);
}
}
return result;
}
- private Collection<String> exportAutoAwareDataSourceNames() {
- return dataSourceRules.values().stream().filter(each -> each.getReadwriteSplittingStrategy() instanceof DynamicReadwriteSplittingStrategy)
- .map(each -> ((DynamicReadwriteSplittingStrategy) each.getReadwriteSplittingStrategy()).getAutoAwareDataSourceName()).collect(Collectors.toList());
- }
-
private Map<String, Map<String, String>> exportStaticDataSources() {
Map<String, Map<String, String>> result = new LinkedHashMap<>(dataSourceRules.size(), 1);
for (ReadwriteSplittingDataSourceRule each : dataSourceRules.values()) {
if (each.getReadwriteSplittingStrategy() instanceof StaticReadwriteSplittingStrategy) {
Map<String, String> exportedDataSources = new LinkedHashMap<>(2, 1);
- exportedDataSources.put(ExportableConstants.PRIMARY_DATA_SOURCE_NAME, each.getWriteDataSource());
- exportedDataSources.put(ExportableConstants.REPLICA_DATA_SOURCE_NAMES, String.join(",", each.getEnabledReplicaDataSources()));
+ exportedDataSources.put(ExportableItemConstants.PRIMARY_DATA_SOURCE_NAME, each.getWriteDataSource());
+ exportedDataSources.put(ExportableItemConstants.REPLICA_DATA_SOURCE_NAMES, String.join(",", each.getReadwriteSplittingStrategy().getReadDataSources()));
result.put(each.getName(), exportedDataSources);
}
}
return result;
}
- private Map<String, Map<String, String>> exportAllDataSources() {
- Map<String, Map<String, String>> result = new LinkedHashMap<>(dataSourceRules.size(), 1);
- for (ReadwriteSplittingDataSourceRule each : dataSourceRules.values()) {
- Map<String, String> exportedDataSources = new LinkedHashMap<>(2, 1);
- exportedDataSources.put(ExportableConstants.PRIMARY_DATA_SOURCE_NAME, each.getWriteDataSource());
- exportedDataSources.put(ExportableConstants.REPLICA_DATA_SOURCE_NAMES, String.join(",", each.getReadDataSourceNames()));
- result.put(each.getName(), exportedDataSources);
- }
- return result;
- }
-
@Override
public String getType() {
return ReadwriteSplittingRule.class.getSimpleName();
diff --git a/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-distsql/shardingsphere-readwrite-splitting-distsql-handler/src/main/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/query/ReadwriteSplittingRuleQueryResultSet.java b/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-distsql/shardingsphere-readwrite-splitting-distsql-handler/src/main/java/org/apache/shardingsphere/readwrite [...]
index 20db0a27107..04e0d4b182a 100644
--- a/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-distsql/shardingsphere-readwrite-splitting-distsql-handler/src/main/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/query/ReadwriteSplittingRuleQueryResultSet.java
+++ b/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-distsql/shardingsphere-readwrite-splitting-distsql-handler/src/main/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/query/ReadwriteSplittingRuleQueryResultSet.java
@@ -19,6 +19,7 @@ package org.apache.shardingsphere.readwritesplitting.distsql.handler.query;
import org.apache.shardingsphere.infra.config.algorithm.ShardingSphereAlgorithmConfiguration;
import org.apache.shardingsphere.infra.distsql.constant.ExportableConstants;
+import org.apache.shardingsphere.infra.distsql.constant.ExportableItemConstants;
import org.apache.shardingsphere.infra.distsql.query.DistSQLResultSet;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.properties.PropertiesConverter;
@@ -60,15 +61,16 @@ public final class ReadwriteSplittingRuleQueryResultSet implements DistSQLResult
Optional<ExportableRule> exportableRule = getExportableRule(database);
exportableRule.ifPresent(optional -> {
Map<String, Object> exportable = exportableRule.get()
- .export(Arrays.asList(ExportableConstants.EXPORTABLE_KEY_AUTO_AWARE_DATA_SOURCE, ExportableConstants.EXPORTABLE_KEY_ENABLED_DATA_SOURCE));
- exportableAutoAwareDataSource = (Map<String, Map<String, String>>) exportable.getOrDefault(ExportableConstants.EXPORTABLE_KEY_AUTO_AWARE_DATA_SOURCE, Collections.emptyMap());
- exportableDataSourceMap = (Map<String, Map<String, String>>) exportable.getOrDefault(ExportableConstants.EXPORTABLE_KEY_ENABLED_DATA_SOURCE, Collections.emptyMap());
+ .export(Arrays.asList(ExportableConstants.EXPORT_DYNAMIC_READWRITE_SPLITTING_RULE, ExportableConstants.EXPORT_STATIC_READWRITE_SPLITTING_RULE));
+ exportableAutoAwareDataSource = (Map<String, Map<String, String>>) exportable.getOrDefault(ExportableConstants.EXPORT_DYNAMIC_READWRITE_SPLITTING_RULE, Collections.emptyMap());
+ exportableDataSourceMap = (Map<String, Map<String, String>>) exportable.getOrDefault(ExportableConstants.EXPORT_STATIC_READWRITE_SPLITTING_RULE, Collections.emptyMap());
});
}
private Optional<ExportableRule> getExportableRule(final ShardingSphereDatabase database) {
return database.getRuleMetaData().findRules(ExportableRule.class).stream()
- .filter(each -> each.containExportableKey(Arrays.asList(ExportableConstants.EXPORTABLE_KEY_AUTO_AWARE_DATA_SOURCE, ExportableConstants.EXPORTABLE_KEY_ENABLED_DATA_SOURCE))).findAny();
+ .filter(each -> each.containExportableKey(Arrays.asList(ExportableConstants.EXPORT_DYNAMIC_READWRITE_SPLITTING_RULE, ExportableConstants.EXPORT_STATIC_READWRITE_SPLITTING_RULE)))
+ .findAny();
}
private Collection<Collection<Object>> buildData(final ReadwriteSplittingRuleConfiguration ruleConfig) {
@@ -99,14 +101,14 @@ public final class ReadwriteSplittingRuleQueryResultSet implements DistSQLResult
private String getWriteDataSourceName(final ReadwriteSplittingDataSourceRuleConfiguration dataSourceRuleConfig, final Map<String, String> exportDataSources) {
if (null != exportDataSources) {
- return exportDataSources.get(ExportableConstants.PRIMARY_DATA_SOURCE_NAME);
+ return exportDataSources.get(ExportableItemConstants.PRIMARY_DATA_SOURCE_NAME);
}
return dataSourceRuleConfig.getWriteDataSourceName().orElse("");
}
private String getReadDataSourceNames(final ReadwriteSplittingDataSourceRuleConfiguration dataSourceRuleConfig, final Map<String, String> exportDataSources) {
if (null != exportDataSources) {
- return exportDataSources.get(ExportableConstants.REPLICA_DATA_SOURCE_NAMES);
+ return exportDataSources.get(ExportableItemConstants.REPLICA_DATA_SOURCE_NAMES);
}
return dataSourceRuleConfig.getReadDataSourceNames().orElse("");
}
diff --git a/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-distsql/shardingsphere-readwrite-splitting-distsql-handler/src/main/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/update/AlterReadwriteSplittingRuleStatementUpdater.java b/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-distsql/shardingsphere-readwrite-splitting-distsql-handler/src/main/java/org/apache/shardingsphere/r [...]
index d35da921831..395d7205632 100644
--- a/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-distsql/shardingsphere-readwrite-splitting-distsql-handler/src/main/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/update/AlterReadwriteSplittingRuleStatementUpdater.java
+++ b/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-distsql/shardingsphere-readwrite-splitting-distsql-handler/src/main/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/update/AlterReadwriteSplittingRuleStatementUpdater.java
@@ -108,10 +108,10 @@ public final class AlterReadwriteSplittingRuleStatementUpdater implements RuleDe
private Collection<String> getLogicResources(final ShardingSphereDatabase database) {
Collection<String> result = new LinkedHashSet<>();
Optional<ExportableRule> exportableRule = database.getRuleMetaData().findRules(ExportableRule.class).stream()
- .filter(each -> each.containExportableKey(Collections.singletonList(ExportableConstants.EXPORTABLE_KEY_PRIMARY_DATA_SOURCE))).findAny();
+ .filter(each -> each.containExportableKey(Collections.singletonList(ExportableConstants.EXPORT_DB_DISCOVERY_PRIMARY_DATA_SOURCES))).findAny();
exportableRule.ifPresent(optional -> {
- Map<String, Object> exportData = optional.export(Collections.singletonList(ExportableConstants.EXPORTABLE_KEY_PRIMARY_DATA_SOURCE));
- Set<String> logicResources = ((Map<String, String>) exportData.getOrDefault(ExportableConstants.EXPORTABLE_KEY_PRIMARY_DATA_SOURCE, Collections.emptyMap())).keySet();
+ Map<String, Object> exportData = optional.export(Collections.singletonList(ExportableConstants.EXPORT_DB_DISCOVERY_PRIMARY_DATA_SOURCES));
+ Set<String> logicResources = ((Map<String, String>) exportData.getOrDefault(ExportableConstants.EXPORT_DB_DISCOVERY_PRIMARY_DATA_SOURCES, Collections.emptyMap())).keySet();
result.addAll(logicResources);
});
return result;
diff --git a/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-distsql/shardingsphere-readwrite-splitting-distsql-handler/src/main/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/update/CreateReadwriteSplittingRuleStatementUpdater.java b/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-distsql/shardingsphere-readwrite-splitting-distsql-handler/src/main/java/org/apache/shardingsphere/ [...]
index e63b86d99a5..8766c6906d3 100644
--- a/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-distsql/shardingsphere-readwrite-splitting-distsql-handler/src/main/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/update/CreateReadwriteSplittingRuleStatementUpdater.java
+++ b/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-distsql/shardingsphere-readwrite-splitting-distsql-handler/src/main/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/update/CreateReadwriteSplittingRuleStatementUpdater.java
@@ -42,7 +42,6 @@ import java.util.LinkedList;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
-import java.util.Set;
import java.util.stream.Collectors;
/**
@@ -92,7 +91,7 @@ public final class CreateReadwriteSplittingRuleStatementUpdater implements RuleD
Collection<String> notExistResources = database.getResource().getNotExistedResources(requireResources);
DistSQLException.predictionThrow(notExistResources.isEmpty(), () -> new RequiredResourceMissedException(databaseName, notExistResources));
Collection<String> logicResources = getLogicResources(database);
- Set<String> notExistLogicResources = requireDiscoverableResources.stream().filter(each -> !logicResources.contains(each)).collect(Collectors.toSet());
+ Collection<String> notExistLogicResources = requireDiscoverableResources.stream().filter(each -> !logicResources.contains(each)).collect(Collectors.toSet());
DistSQLException.predictionThrow(notExistLogicResources.isEmpty(), () -> new RequiredResourceMissedException(databaseName, notExistLogicResources));
}
@@ -100,10 +99,10 @@ public final class CreateReadwriteSplittingRuleStatementUpdater implements RuleD
private Collection<String> getLogicResources(final ShardingSphereDatabase database) {
Collection<String> result = new LinkedHashSet<>();
Optional<ExportableRule> exportableRule = database.getRuleMetaData().findRules(ExportableRule.class).stream()
- .filter(each -> each.containExportableKey(Collections.singletonList(ExportableConstants.EXPORTABLE_KEY_PRIMARY_DATA_SOURCE))).findAny();
+ .filter(each -> each.containExportableKey(Collections.singletonList(ExportableConstants.EXPORT_DB_DISCOVERY_PRIMARY_DATA_SOURCES))).findAny();
exportableRule.ifPresent(optional -> {
- Map<String, Object> exportData = optional.export(Collections.singletonList(ExportableConstants.EXPORTABLE_KEY_PRIMARY_DATA_SOURCE));
- Set<String> logicResources = ((Map<String, String>) exportData.getOrDefault(ExportableConstants.EXPORTABLE_KEY_PRIMARY_DATA_SOURCE, Collections.emptyMap())).keySet();
+ Map<String, Object> exportData = optional.export(Collections.singletonList(ExportableConstants.EXPORT_DB_DISCOVERY_PRIMARY_DATA_SOURCES));
+ Collection<String> logicResources = ((Map<String, String>) exportData.getOrDefault(ExportableConstants.EXPORT_DB_DISCOVERY_PRIMARY_DATA_SOURCES, Collections.emptyMap())).keySet();
result.addAll(logicResources);
});
return result;
diff --git a/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-distsql/shardingsphere-readwrite-splitting-distsql-handler/src/test/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/fixture/DatabaseDiscoveryRuleExportableFixture.java b/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-distsql/shardingsphere-readwrite-splitting-distsql-handler/src/test/java/org/apache/shardingsphere/readw [...]
new file mode 100644
index 00000000000..2da2ae5fbff
--- /dev/null
+++ b/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-distsql/shardingsphere-readwrite-splitting-distsql-handler/src/test/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/fixture/DatabaseDiscoveryRuleExportableFixture.java
@@ -0,0 +1,42 @@
+/*
+ * 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.readwritesplitting.distsql.handler.fixture;
+
+import org.apache.shardingsphere.infra.distsql.constant.ExportableConstants;
+import org.apache.shardingsphere.infra.rule.identifier.type.ExportableRule;
+
+import java.util.Collections;
+import java.util.Map;
+import java.util.function.Supplier;
+
+public final class DatabaseDiscoveryRuleExportableFixture implements ExportableRule {
+
+ @Override
+ public String getType() {
+ return null;
+ }
+
+ @Override
+ public Map<String, Supplier<Object>> getExportedMethods() {
+ return Collections.singletonMap(ExportableConstants.EXPORT_DB_DISCOVERY_PRIMARY_DATA_SOURCES, this::exportedMethod);
+ }
+
+ private Map<String, String> exportedMethod() {
+ return Collections.singletonMap("ms_group", "ds_0");
+ }
+}
diff --git a/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-distsql/shardingsphere-readwrite-splitting-distsql-handler/src/test/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/fixture/ReadwriteSplittingRuleExportableFixture.java b/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-distsql/shardingsphere-readwrite-splitting-distsql-handler/src/test/java/org/apache/shardingsphere/read [...]
new file mode 100644
index 00000000000..8ab68b898b8
--- /dev/null
+++ b/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-distsql/shardingsphere-readwrite-splitting-distsql-handler/src/test/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/fixture/ReadwriteSplittingRuleExportableFixture.java
@@ -0,0 +1,66 @@
+/*
+ * 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.readwritesplitting.distsql.handler.fixture;
+
+import org.apache.shardingsphere.infra.distsql.constant.ExportableConstants;
+import org.apache.shardingsphere.infra.distsql.constant.ExportableItemConstants;
+import org.apache.shardingsphere.infra.rule.identifier.type.ExportableRule;
+
+import java.util.HashMap;
+import java.util.LinkedHashMap;
+import java.util.Map;
+import java.util.function.Supplier;
+
+public final class ReadwriteSplittingRuleExportableFixture implements ExportableRule {
+
+ @Override
+ public String getType() {
+ return null;
+ }
+
+ @Override
+ public Map<String, Supplier<Object>> getExportedMethods() {
+ Map<String, Supplier<Object>> result = new HashMap<>(4, 1);
+ result.put(ExportableConstants.EXPORT_DYNAMIC_READWRITE_SPLITTING_RULE, this::exportDynamicDataSources);
+ result.put(ExportableConstants.EXPORT_STATIC_READWRITE_SPLITTING_RULE, this::exportStaticDataSources);
+ return result;
+ }
+
+ private Map<String, Map<String, String>> exportDynamicDataSources() {
+ Map<String, Map<String, String>> result = new HashMap<>(1, 1);
+ result.put("readwrite_ds", getAutoAwareDataSources());
+ return result;
+ }
+
+ private Map<String, String> getAutoAwareDataSources() {
+ Map<String, String> result = new HashMap<>(3, 1);
+ result.put(ExportableItemConstants.AUTO_AWARE_DATA_SOURCE_NAME, "ha_group");
+ result.put(ExportableItemConstants.PRIMARY_DATA_SOURCE_NAME, "write_ds");
+ result.put(ExportableItemConstants.REPLICA_DATA_SOURCE_NAMES, "read_ds_0,read_ds_1");
+ return result;
+ }
+
+ private Map<String, Map<String, String>> exportStaticDataSources() {
+ Map<String, Map<String, String>> result = new LinkedHashMap<>();
+ Map<String, String> staticRule = new LinkedHashMap<>(2, 1);
+ staticRule.put(ExportableItemConstants.PRIMARY_DATA_SOURCE_NAME, "ds_0");
+ staticRule.put(ExportableItemConstants.REPLICA_DATA_SOURCE_NAMES, "ds_1");
+ result.put("static_rule_1", staticRule);
+ return result;
+ }
+}
diff --git a/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-distsql/shardingsphere-readwrite-splitting-distsql-handler/src/test/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/query/ReadwriteSplittingRuleQueryResultSetTest.java b/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-distsql/shardingsphere-readwrite-splitting-distsql-handler/src/test/java/org/apache/shardingsphere/readw [...]
index 3f2e4bdb21e..9d0d831e7ae 100644
--- a/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-distsql/shardingsphere-readwrite-splitting-distsql-handler/src/test/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/query/ReadwriteSplittingRuleQueryResultSetTest.java
+++ b/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-distsql/shardingsphere-readwrite-splitting-distsql-handler/src/test/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/query/ReadwriteSplittingRuleQueryResultSetTest.java
@@ -19,18 +19,16 @@ package org.apache.shardingsphere.readwritesplitting.distsql.handler.query;
import org.apache.shardingsphere.infra.config.RuleConfiguration;
import org.apache.shardingsphere.infra.config.algorithm.ShardingSphereAlgorithmConfiguration;
-import org.apache.shardingsphere.infra.distsql.constant.ExportableConstants;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.rule.identifier.type.ExportableRule;
import org.apache.shardingsphere.readwritesplitting.api.ReadwriteSplittingRuleConfiguration;
import org.apache.shardingsphere.readwritesplitting.api.rule.ReadwriteSplittingDataSourceRuleConfiguration;
+import org.apache.shardingsphere.readwritesplitting.distsql.handler.fixture.ReadwriteSplittingRuleExportableFixture;
import org.apache.shardingsphere.readwritesplitting.distsql.parser.statement.ShowReadwriteSplittingRulesStatement;
import org.junit.Test;
import java.util.Collection;
import java.util.Collections;
-import java.util.HashMap;
-import java.util.Map;
import java.util.Properties;
import static org.hamcrest.CoreMatchers.is;
@@ -101,15 +99,11 @@ public final class ReadwriteSplittingRuleQueryResultSetTest {
@Test
public void assertGetRowDataWithAutoAwareDataSource() {
- ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS);
- ExportableRule exportableRule = mock(ExportableRule.class);
- when(exportableRule.containExportableKey(anyCollection())).thenReturn(true);
- when(database.getRuleMetaData().findRules(any())).thenReturn(Collections.singletonList(exportableRule));
- when(exportableRule.export(anyCollection())).thenReturn(
- Collections.singletonMap(ExportableConstants.EXPORTABLE_KEY_AUTO_AWARE_DATA_SOURCE, Collections.singletonMap("readwrite_ds", getAutoAwareDataSources())));
- when(database.getRuleMetaData().findRuleConfigurations(any())).thenReturn(Collections.singleton(createRuleConfigurationWithAutoAwareDataSource()));
+ ShardingSphereDatabase metaData = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS);
+ when(metaData.getRuleMetaData().findRules(ExportableRule.class)).thenReturn(Collections.singletonList(new ReadwriteSplittingRuleExportableFixture()));
+ when(metaData.getRuleMetaData().findRuleConfigurations(any())).thenReturn(Collections.singleton(createRuleConfigurationWithAutoAwareDataSource()));
ReadwriteSplittingRuleQueryResultSet resultSet = new ReadwriteSplittingRuleQueryResultSet();
- resultSet.init(database, mock(ShowReadwriteSplittingRulesStatement.class));
+ resultSet.init(metaData, mock(ShowReadwriteSplittingRulesStatement.class));
Collection<Object> actual = resultSet.getRowData();
assertThat(actual.size(), is(6));
assertTrue(actual.contains("readwrite_ds"));
@@ -124,11 +118,4 @@ public final class ReadwriteSplittingRuleQueryResultSetTest {
ReadwriteSplittingDataSourceRuleConfiguration dataSourceRuleConfig = new ReadwriteSplittingDataSourceRuleConfiguration("readwrite_ds", "Dynamic", props, "");
return new ReadwriteSplittingRuleConfiguration(Collections.singleton(dataSourceRuleConfig), null);
}
-
- private Map<String, String> getAutoAwareDataSources() {
- Map<String, String> result = new HashMap<>(2, 1);
- result.put(ExportableConstants.PRIMARY_DATA_SOURCE_NAME, "write_ds");
- result.put(ExportableConstants.REPLICA_DATA_SOURCE_NAMES, "read_ds_0,read_ds_1");
- return result;
- }
}
diff --git a/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-distsql/shardingsphere-readwrite-splitting-distsql-handler/src/test/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/update/AlterReadwriteSplittingRuleStatementUpdaterTest.java b/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-distsql/shardingsphere-readwrite-splitting-distsql-handler/src/test/java/org/apache/shardingsphe [...]
index 5d56f1dd28e..b695b9d7f56 100644
--- a/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-distsql/shardingsphere-readwrite-splitting-distsql-handler/src/test/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/update/AlterReadwriteSplittingRuleStatementUpdaterTest.java
+++ b/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-distsql/shardingsphere-readwrite-splitting-distsql-handler/src/test/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/update/AlterReadwriteSplittingRuleStatementUpdaterTest.java
@@ -23,8 +23,10 @@ import org.apache.shardingsphere.infra.distsql.exception.rule.InvalidAlgorithmCo
import org.apache.shardingsphere.infra.distsql.exception.rule.RequiredRuleMissedException;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.metadata.database.resource.ShardingSphereResource;
+import org.apache.shardingsphere.infra.rule.identifier.type.ExportableRule;
import org.apache.shardingsphere.readwritesplitting.api.ReadwriteSplittingRuleConfiguration;
import org.apache.shardingsphere.readwritesplitting.api.rule.ReadwriteSplittingDataSourceRuleConfiguration;
+import org.apache.shardingsphere.readwritesplitting.distsql.handler.fixture.DatabaseDiscoveryRuleExportableFixture;
import org.apache.shardingsphere.readwritesplitting.distsql.parser.segment.ReadwriteSplittingRuleSegment;
import org.apache.shardingsphere.readwritesplitting.distsql.parser.statement.AlterReadwriteSplittingRuleStatement;
import org.junit.Before;
@@ -74,6 +76,13 @@ public final class AlterReadwriteSplittingRuleStatementUpdaterTest {
updater.checkSQLStatement(database, createSQLStatement("TEST"), createCurrentRuleConfiguration());
}
+ @Test(expected = RequiredResourceMissedException.class)
+ public void assertCheckSQLStatementWithoutExistedAutoAwareResources() throws DistSQLException {
+ when(database.getRuleMetaData().findRules(ExportableRule.class)).thenReturn(Collections.singleton(new DatabaseDiscoveryRuleExportableFixture()));
+ ReadwriteSplittingRuleSegment ruleSegment = new ReadwriteSplittingRuleSegment("readwrite_ds", "ha_group", "TEST", new Properties());
+ updater.checkSQLStatement(database, new AlterReadwriteSplittingRuleStatement(Collections.singleton(ruleSegment)), createCurrentRuleConfiguration());
+ }
+
@Test(expected = InvalidAlgorithmConfigurationException.class)
public void assertCheckSQLStatementWithoutToBeAlteredLoadBalancers() throws DistSQLException {
when(database.getRuleMetaData().findRules(any())).thenReturn(Collections.emptyList());
diff --git a/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-distsql/shardingsphere-readwrite-splitting-distsql-handler/src/test/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/update/CreateReadwriteSplittingRuleStatementUpdaterTest.java b/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-distsql/shardingsphere-readwrite-splitting-distsql-handler/src/test/java/org/apache/shardingsph [...]
index fed03b35a8f..fbaa69efff4 100644
--- a/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-distsql/shardingsphere-readwrite-splitting-distsql-handler/src/test/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/update/CreateReadwriteSplittingRuleStatementUpdaterTest.java
+++ b/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-distsql/shardingsphere-readwrite-splitting-distsql-handler/src/test/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/update/CreateReadwriteSplittingRuleStatementUpdaterTest.java
@@ -24,25 +24,36 @@ import org.apache.shardingsphere.infra.distsql.exception.rule.InvalidAlgorithmCo
import org.apache.shardingsphere.infra.distsql.exception.rule.InvalidRuleConfigurationException;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.metadata.database.resource.ShardingSphereResource;
+import org.apache.shardingsphere.infra.rule.identifier.type.ExportableRule;
import org.apache.shardingsphere.readwritesplitting.api.ReadwriteSplittingRuleConfiguration;
import org.apache.shardingsphere.readwritesplitting.api.rule.ReadwriteSplittingDataSourceRuleConfiguration;
+import org.apache.shardingsphere.readwritesplitting.distsql.handler.fixture.DatabaseDiscoveryRuleExportableFixture;
import org.apache.shardingsphere.readwritesplitting.distsql.parser.segment.ReadwriteSplittingRuleSegment;
import org.apache.shardingsphere.readwritesplitting.distsql.parser.statement.CreateReadwriteSplittingRuleStatement;
+import org.apache.shardingsphere.readwritesplitting.factory.ReplicaLoadBalanceAlgorithmFactory;
+import org.apache.shardingsphere.readwritesplitting.spi.ReadQueryLoadBalanceAlgorithm;
+import org.apache.shardingsphere.spi.ShardingSphereServiceLoader;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Answers;
import org.mockito.Mock;
+import org.mockito.MockedStatic;
import org.mockito.junit.MockitoJUnitRunner;
+import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
+import java.util.HashMap;
import java.util.LinkedList;
import java.util.Properties;
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.mockStatic;
import static org.mockito.Mockito.when;
@RunWith(MockitoJUnitRunner.class)
@@ -82,12 +93,35 @@ public final class CreateReadwriteSplittingRuleStatementUpdaterTest {
updater.checkSQLStatement(database, createSQLStatement("TEST"), null);
}
+ @Test(expected = RequiredResourceMissedException.class)
+ public void assertCheckSQLStatementWithoutExistedAutoAwareResources() throws DistSQLException {
+ when(database.getRuleMetaData().findRules(ExportableRule.class)).thenReturn(Collections.singleton(new DatabaseDiscoveryRuleExportableFixture()));
+ ReadwriteSplittingRuleSegment ruleSegment = new ReadwriteSplittingRuleSegment("dynamic_rule", "ha_group", "TEST", new Properties());
+ updater.checkSQLStatement(database, new CreateReadwriteSplittingRuleStatement(Collections.singleton(ruleSegment)), null);
+ }
+
@Test(expected = InvalidAlgorithmConfigurationException.class)
public void assertCheckSQLStatementWithoutToBeCreatedLoadBalancers() throws DistSQLException {
when(database.getRuleMetaData().findRules(any())).thenReturn(Collections.emptyList());
updater.checkSQLStatement(database, createSQLStatement("INVALID_TYPE"), null);
}
+ @Test
+ public void assertUpdateSuccess() throws DistSQLException {
+ when(database.getRuleMetaData().findRules(ExportableRule.class)).thenReturn(Collections.singleton(new DatabaseDiscoveryRuleExportableFixture()));
+ MockedStatic<ReplicaLoadBalanceAlgorithmFactory> mockedFactory = mockStatic(ReplicaLoadBalanceAlgorithmFactory.class);
+ mockedFactory.when(() -> ReplicaLoadBalanceAlgorithmFactory.contains("TEST")).thenReturn(true);
+ ReadwriteSplittingRuleSegment dynamicSegment = new ReadwriteSplittingRuleSegment("dynamic_rule", "ms_group", "TEST", new Properties());
+ ReadwriteSplittingRuleSegment staticSegment = new ReadwriteSplittingRuleSegment("static_rule", "write_ds_0", Arrays.asList("read_ds_0", "read_ds_1"), "TEST", new Properties());
+ ShardingSphereServiceLoader.register(ReadQueryLoadBalanceAlgorithm.class);
+ CreateReadwriteSplittingRuleStatement statement = new CreateReadwriteSplittingRuleStatement(Arrays.asList(dynamicSegment, staticSegment));
+ updater.checkSQLStatement(database, statement, null);
+ ReadwriteSplittingRuleConfiguration toBeCreatedRuleConfig = updater.buildToBeCreatedRuleConfiguration(statement);
+ ReadwriteSplittingRuleConfiguration currentRuleConfig = new ReadwriteSplittingRuleConfiguration(new ArrayList<>(), new HashMap<>());
+ updater.updateCurrentRuleConfiguration(currentRuleConfig, toBeCreatedRuleConfig);
+ assertThat(currentRuleConfig.getDataSources().size(), is(2));
+ }
+
private CreateReadwriteSplittingRuleStatement createSQLStatement(final String loadBalancerName) {
ReadwriteSplittingRuleSegment ruleSegment = new ReadwriteSplittingRuleSegment("readwrite_ds", "write_ds", Arrays.asList("read_ds_0", "read_ds_1"), loadBalancerName, new Properties());
return new CreateReadwriteSplittingRuleStatement(Collections.singleton(ruleSegment));
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/distsql/constant/ExportableConstants.java b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/distsql/constant/ExportableConstants.java
index f40845d027a..174462e3d38 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/distsql/constant/ExportableConstants.java
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/distsql/constant/ExportableConstants.java
@@ -26,19 +26,11 @@ import lombok.NoArgsConstructor;
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class ExportableConstants {
- public static final String EXPORTABLE_KEY_PRIMARY_DATA_SOURCE = "primary_data_source";
+ public static final String EXPORT_DB_DISCOVERY_PRIMARY_DATA_SOURCES = "primary_data_source";
- public static final String EXPORTABLE_KEY_AUTO_AWARE_DATA_SOURCE = "auto_aware_data_source";
+ public static final String EXPORT_DYNAMIC_READWRITE_SPLITTING_RULE = "dynamic_readwrite_splitting_rules";
- public static final String EXPORTABLE_KEY_AUTO_AWARE_DATA_SOURCE_NAME = "auto_aware_data_source_name";
+ public static final String EXPORT_STATIC_READWRITE_SPLITTING_RULE = "static_readwrite_splitting_rules";
- public static final String EXPORTABLE_KEY_ENABLED_DATA_SOURCE = "enabled_data_source";
-
- public static final String EXPORTABLE_KEY_DATA_SOURCE = "data_source";
-
- public static final String EXPORTABLE_KEY_SINGLE_TABLES = "single_tables";
-
- public static final String PRIMARY_DATA_SOURCE_NAME = "primary_data_source_name";
-
- public static final String REPLICA_DATA_SOURCE_NAMES = "replica_data_source_names";
+ public static final String EXPORT_SINGLE_TABLES = "single_tables";
}
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/distsql/constant/ExportableConstants.java b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/distsql/constant/ExportableItemConstants.java
similarity index 64%
copy from shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/distsql/constant/ExportableConstants.java
copy to shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/distsql/constant/ExportableItemConstants.java
index f40845d027a..60a02aa64f2 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/distsql/constant/ExportableConstants.java
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/distsql/constant/ExportableItemConstants.java
@@ -21,22 +21,12 @@ import lombok.AccessLevel;
import lombok.NoArgsConstructor;
/**
- * Exportable constants.
+ * Exportable item constants.
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
-public final class ExportableConstants {
+public class ExportableItemConstants {
- public static final String EXPORTABLE_KEY_PRIMARY_DATA_SOURCE = "primary_data_source";
-
- public static final String EXPORTABLE_KEY_AUTO_AWARE_DATA_SOURCE = "auto_aware_data_source";
-
- public static final String EXPORTABLE_KEY_AUTO_AWARE_DATA_SOURCE_NAME = "auto_aware_data_source_name";
-
- public static final String EXPORTABLE_KEY_ENABLED_DATA_SOURCE = "enabled_data_source";
-
- public static final String EXPORTABLE_KEY_DATA_SOURCE = "data_source";
-
- public static final String EXPORTABLE_KEY_SINGLE_TABLES = "single_tables";
+ public static final String AUTO_AWARE_DATA_SOURCE_NAME = "auto_aware_data_source_name";
public static final String PRIMARY_DATA_SOURCE_NAME = "primary_data_source_name";
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 75440cfb7f9..6db030ff5ae 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
@@ -269,6 +269,6 @@ public final class SingleTableRule implements SchemaRule, DataNodeContainedRule,
@Override
public Map<String, Supplier<Object>> getExportedMethods() {
- return Collections.singletonMap(ExportableConstants.EXPORTABLE_KEY_SINGLE_TABLES, tableNames::keySet);
+ return Collections.singletonMap(ExportableConstants.EXPORT_SINGLE_TABLES, tableNames::keySet);
}
}
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/common/queryable/CountInstanceRulesHandler.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/common/queryable/CountInstanceRulesHandler.java
index add45831802..39942feea4f 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/common/queryable/CountInstanceRulesHandler.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/common/queryable/CountInstanceRulesHandler.java
@@ -91,7 +91,7 @@ public final class CountInstanceRulesHandler extends QueryableRALBackendHandler<
}
private void addSingleTableData(final Map<String, List<Object>> dataMap, final Collection<SingleTableRule> rules) {
- Optional<Integer> count = rules.stream().map(each -> (Collection) each.export(ExportableConstants.EXPORTABLE_KEY_SINGLE_TABLES).orElse(Collections.emptyMap()))
+ Optional<Integer> count = rules.stream().map(each -> (Collection) each.export(ExportableConstants.EXPORT_SINGLE_TABLES).orElse(Collections.emptyMap()))
.map(Collection::size).reduce(Integer::sum);
dataMap.compute(SINGLE_TABLE, (key, value) -> buildRow(value, SINGLE_TABLE, count.orElse(DEFAULT_COUNT)));
}
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/common/queryable/ShowReadwriteSplittingReadResourcesHandler.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/common/queryable/ShowReadwriteSplittingReadResourcesHandler.java
index bbaffeed9c9..423316a8f76 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/common/queryable/ShowReadwriteSplittingReadResourcesHandler.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/common/queryable/ShowReadwriteSplittingReadResourcesHandler.java
@@ -19,16 +19,17 @@ package org.apache.shardingsphere.proxy.backend.text.distsql.ral.common.queryabl
import com.google.common.base.Strings;
import org.apache.shardingsphere.infra.distsql.constant.ExportableConstants;
+import org.apache.shardingsphere.infra.distsql.constant.ExportableItemConstants;
import org.apache.shardingsphere.infra.exception.DatabaseNotExistedException;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.metadata.database.schema.QualifiedDatabase;
import org.apache.shardingsphere.infra.rule.identifier.type.ExportableRule;
-import org.apache.shardingsphere.mode.metadata.storage.StorageNodeDataSource;
-import org.apache.shardingsphere.mode.metadata.storage.StorageNodeStatus;
import org.apache.shardingsphere.mode.manager.ContextManager;
import org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.status.storage.service.StorageNodeStatusService;
import org.apache.shardingsphere.mode.metadata.MetaDataContexts;
import org.apache.shardingsphere.mode.metadata.persist.MetaDataPersistService;
+import org.apache.shardingsphere.mode.metadata.storage.StorageNodeDataSource;
+import org.apache.shardingsphere.mode.metadata.storage.StorageNodeStatus;
import org.apache.shardingsphere.mode.repository.cluster.ClusterPersistRepository;
import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
import org.apache.shardingsphere.proxy.backend.exception.NoDatabaseSelectedException;
@@ -41,7 +42,6 @@ import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
-import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
@@ -94,21 +94,13 @@ public final class ShowReadwriteSplittingReadResourcesHandler extends QueryableR
}
private Collection<String> getAllReadResources(final ShardingSphereDatabase database) {
- Collection<String> result = new LinkedHashSet<>();
- Map<String, Map<String, String>> readResourceData = getReadResourceData(database);
- readResourceData.forEach((key, value) -> {
- String resources = value.getOrDefault(ExportableConstants.REPLICA_DATA_SOURCE_NAMES, "");
- result.addAll(deconstructString(resources));
- });
- return result;
- }
-
- private Map<String, Map<String, String>> getReadResourceData(final ShardingSphereDatabase database) {
- return database.getRuleMetaData().getRules().stream().filter(each -> each instanceof ExportableRule)
- .map(each -> ((ExportableRule) each).export(ExportableConstants.EXPORTABLE_KEY_DATA_SOURCE))
- .map(each -> (Map<String, Map<String, String>>) each.orElse(Collections.emptyMap()))
- .map(Map::entrySet).flatMap(Collection::stream).filter(entry -> !entry.getValue().isEmpty())
- .collect(Collectors.toMap(Entry::getKey, Entry::getValue, (v1, v2) -> v2, LinkedHashMap::new));
+ Collection<String> exportKeys = Arrays.asList(ExportableConstants.EXPORT_STATIC_READWRITE_SPLITTING_RULE, ExportableConstants.EXPORT_DYNAMIC_READWRITE_SPLITTING_RULE);
+ Map<String, Object> exportMap = database.getRuleMetaData().getRules().stream().filter(each -> each instanceof ExportableRule).map(each -> (ExportableRule) each)
+ .filter(each -> each.containExportableKey(exportKeys)).findFirst().map(each -> each.export(exportKeys)).orElse(Collections.emptyMap());
+ Map<String, Map<String, String>> allReadwriteRuleMap = exportMap.values().stream().map(each -> ((Map<String, Map<String, String>>) each).entrySet())
+ .flatMap(Collection::stream).collect(Collectors.toMap(Entry::getKey, Entry::getValue, (v1, v2) -> v2, LinkedHashMap::new));
+ return allReadwriteRuleMap.values().stream().map(each -> each.getOrDefault(ExportableItemConstants.REPLICA_DATA_SOURCE_NAMES, ""))
+ .map(this::deconstructString).flatMap(Collection::stream).collect(Collectors.toCollection(LinkedList::new));
}
private Map<String, StorageNodeDataSource> getPersistentReadResources(final String databaseName, final MetaDataPersistService persistService) {
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/common/updatable/SetReadwriteSplittingStatusHandler.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/common/updatable/SetReadwriteSplittingStatusHandler.java
index 4751b465a0a..a83405dcef2 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/common/updatable/SetReadwriteSplittingStatusHandler.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/common/updatable/SetReadwriteSplittingStatusHandler.java
@@ -20,17 +20,18 @@ package org.apache.shardingsphere.proxy.backend.text.distsql.ral.common.updatabl
import com.google.common.base.Splitter;
import com.google.common.base.Strings;
import org.apache.shardingsphere.infra.distsql.constant.ExportableConstants;
+import org.apache.shardingsphere.infra.distsql.constant.ExportableItemConstants;
import org.apache.shardingsphere.infra.distsql.exception.DistSQLException;
import org.apache.shardingsphere.infra.distsql.exception.resource.RequiredResourceMissedException;
import org.apache.shardingsphere.infra.eventbus.ShardingSphereEventBus;
import org.apache.shardingsphere.infra.exception.DatabaseNotExistedException;
import org.apache.shardingsphere.infra.metadata.database.schema.QualifiedDatabase;
-import org.apache.shardingsphere.mode.metadata.storage.StorageNodeDataSource;
-import org.apache.shardingsphere.mode.metadata.storage.StorageNodeRole;
-import org.apache.shardingsphere.mode.metadata.storage.StorageNodeStatus;
import org.apache.shardingsphere.mode.manager.ContextManager;
import org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.status.storage.service.StorageNodeStatusService;
import org.apache.shardingsphere.mode.metadata.persist.MetaDataPersistService;
+import org.apache.shardingsphere.mode.metadata.storage.StorageNodeDataSource;
+import org.apache.shardingsphere.mode.metadata.storage.StorageNodeRole;
+import org.apache.shardingsphere.mode.metadata.storage.StorageNodeStatus;
import org.apache.shardingsphere.mode.metadata.storage.event.DataSourceDisabledEvent;
import org.apache.shardingsphere.mode.repository.cluster.ClusterPersistRepository;
import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
@@ -199,11 +200,11 @@ public final class SetReadwriteSplittingStatusHandler extends UpdatableRALBacken
private Map<String, Map<String, String>> getExportedReadwriteSplittingRules(final ContextManager contextManager, final String databaseName) {
Map<String, Map<String, String>> result = new HashMap<>();
contextManager.getMetaDataContexts().getDatabase(databaseName).getRuleMetaData().findRules(ReadwriteSplittingRule.class).stream().findAny()
- .filter(each -> each.containExportableKey(Arrays.asList(ExportableConstants.EXPORTABLE_KEY_AUTO_AWARE_DATA_SOURCE, ExportableConstants.EXPORTABLE_KEY_ENABLED_DATA_SOURCE)))
- .map(each -> each.export(Arrays.asList(ExportableConstants.EXPORTABLE_KEY_AUTO_AWARE_DATA_SOURCE, ExportableConstants.EXPORTABLE_KEY_ENABLED_DATA_SOURCE)))
+ .filter(each -> each.containExportableKey(Arrays.asList(ExportableConstants.EXPORT_DYNAMIC_READWRITE_SPLITTING_RULE, ExportableConstants.EXPORT_STATIC_READWRITE_SPLITTING_RULE)))
+ .map(each -> each.export(Arrays.asList(ExportableConstants.EXPORT_DYNAMIC_READWRITE_SPLITTING_RULE, ExportableConstants.EXPORT_STATIC_READWRITE_SPLITTING_RULE)))
.ifPresent(optional -> {
- result.putAll((Map) optional.getOrDefault(ExportableConstants.EXPORTABLE_KEY_AUTO_AWARE_DATA_SOURCE, Collections.emptyMap()));
- result.putAll((Map) optional.getOrDefault(ExportableConstants.EXPORTABLE_KEY_ENABLED_DATA_SOURCE, Collections.emptyMap()));
+ result.putAll((Map) optional.getOrDefault(ExportableConstants.EXPORT_DYNAMIC_READWRITE_SPLITTING_RULE, Collections.emptyMap()));
+ result.putAll((Map) optional.getOrDefault(ExportableConstants.EXPORT_STATIC_READWRITE_SPLITTING_RULE, Collections.emptyMap()));
});
return result;
}
@@ -214,9 +215,9 @@ public final class SetReadwriteSplittingStatusHandler extends UpdatableRALBacken
.filter(entry -> entry.getValue() <= 1).map(Entry::getKey).collect(Collectors.toSet());
}
- private void addReplicaResource(final Map<String, String> replicaResources, final Entry<String, Map<String, String>> entry) {
- entry.getValue().entrySet().stream().filter(entry1 -> ExportableConstants.REPLICA_DATA_SOURCE_NAMES.equals(entry1.getKey()))
- .map(entry1 -> Arrays.asList(entry1.getValue().split(","))).flatMap(Collection::stream).forEach(each -> put(replicaResources, each, entry.getKey()));
+ private void addReplicaResource(final Map<String, String> replicaResources, final Entry<String, Map<String, String>> readwriteSplittingRule) {
+ readwriteSplittingRule.getValue().entrySet().stream().filter(entry -> ExportableItemConstants.REPLICA_DATA_SOURCE_NAMES.equals(entry.getKey()))
+ .map(entry -> Arrays.asList(entry.getValue().split(","))).flatMap(Collection::stream).forEach(each -> put(replicaResources, each, readwriteSplittingRule.getKey()));
}
private void put(final Map<String, String> map, final String key, final String value) {
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rql/rule/SchemaRulesCountResultSet.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rql/rule/SchemaRulesCountResultSet.java
index ad02a75f665..86fe3fd272e 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rql/rule/SchemaRulesCountResultSet.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rql/rule/SchemaRulesCountResultSet.java
@@ -82,7 +82,7 @@ public final class SchemaRulesCountResultSet implements DistSQLResultSet {
}
private void addSingleTableData(final Map<String, Collection<Object>> dataMap, final Collection<SingleTableRule> rules) {
- Optional<Integer> count = rules.stream().map(each -> (Collection) each.export(ExportableConstants.EXPORTABLE_KEY_SINGLE_TABLES).orElse(Collections.emptyMap()))
+ Optional<Integer> count = rules.stream().map(each -> (Collection) each.export(ExportableConstants.EXPORT_SINGLE_TABLES).orElse(Collections.emptyMap()))
.map(Collection::size).reduce(Integer::sum);
dataMap.compute(SINGLE_TABLE, (key, value) -> buildRow(value, SINGLE_TABLE, count.orElse(DEFAULT_COUNT)));
}
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/common/queryable/CountInstanceRulesTest.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/common/queryable/CountInstanceRulesTest.java
index a50388c5a28..d41bf01c25c 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/common/queryable/CountInstanceRulesTest.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/common/queryable/CountInstanceRulesTest.java
@@ -88,7 +88,7 @@ public final class CountInstanceRulesTest extends ProxyContextRestorer {
private SingleTableRule mockSingleTableRule() {
SingleTableRule result = mock(SingleTableRule.class);
- when(result.export(ExportableConstants.EXPORTABLE_KEY_SINGLE_TABLES)).thenReturn(java.util.Optional.of(Arrays.asList("single_table_1", "single_table_2")));
+ when(result.export(ExportableConstants.EXPORT_SINGLE_TABLES)).thenReturn(java.util.Optional.of(Arrays.asList("single_table_1", "single_table_2")));
return result;
}
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/rql/SchemaRulesCountResultSetTest.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/rql/SchemaRulesCountResultSetTest.java
index dcb92cdc87b..669c93427aa 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/rql/SchemaRulesCountResultSetTest.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/rql/SchemaRulesCountResultSetTest.java
@@ -74,7 +74,7 @@ public final class SchemaRulesCountResultSetTest {
private SingleTableRule mockSingleTableRule() {
SingleTableRule result = mock(SingleTableRule.class);
- when(result.export(ExportableConstants.EXPORTABLE_KEY_SINGLE_TABLES)).thenReturn(java.util.Optional.of(Arrays.asList("single_table_1", "single_table_2")));
+ when(result.export(ExportableConstants.EXPORT_SINGLE_TABLES)).thenReturn(java.util.Optional.of(Arrays.asList("single_table_1", "single_table_2")));
return result;
}