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 1b187e5880d Add ImportRuleConfigurationProvider (#30392)
1b187e5880d is described below
commit 1b187e5880df27838af029a504c7b583a8017961
Author: yx9o <[email protected]>
AuthorDate: Tue Mar 5 15:43:23 2024 +0800
Add ImportRuleConfigurationProvider (#30392)
* Add ImportRuleConfigurationProvider
* Update
* Update
---
.../ShardingImportRuleConfigurationProvider.java | 49 +++++++++++++-------
...le.spi.database.ImportRuleConfigurationProvider | 18 ++++++++
...hardingImportRuleConfigurationProviderTest.java | 13 +++---
.../database/ImportRuleConfigurationProvider.java | 53 ++++++++++++++++++++++
.../YamlDatabaseConfigurationImportExecutor.java | 42 +++++++----------
5 files changed, 128 insertions(+), 47 deletions(-)
diff --git
a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/common/checker/ShardingRuleConfigurationImportChecker.java
b/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/provider/ShardingImportRuleConfigurationProvider.java
similarity index 75%
rename from
proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/common/checker/ShardingRuleConfigurationImportChecker.java
rename to
features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/provider/ShardingImportRuleConfigurationProvider.java
index 49fa8fbfa12..6c6c0341229 100644
---
a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/common/checker/ShardingRuleConfigurationImportChecker.java
+++
b/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/provider/ShardingImportRuleConfigurationProvider.java
@@ -15,52 +15,64 @@
* limitations under the License.
*/
-package
org.apache.shardingsphere.proxy.backend.handler.distsql.ral.common.checker;
+package org.apache.shardingsphere.sharding.distsql.handler.provider;
+import
org.apache.shardingsphere.distsql.handler.engine.update.ral.rule.spi.database.ImportRuleConfigurationProvider;
import
org.apache.shardingsphere.distsql.handler.exception.rule.DuplicateRuleException;
import
org.apache.shardingsphere.distsql.handler.exception.storageunit.MissingRequiredStorageUnitsException;
import
org.apache.shardingsphere.infra.algorithm.core.config.AlgorithmConfiguration;
+import
org.apache.shardingsphere.infra.algorithm.keygen.core.KeyGenerateAlgorithm;
+import org.apache.shardingsphere.infra.config.rule.RuleConfiguration;
import org.apache.shardingsphere.infra.datanode.DataNode;
import
org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions;
import org.apache.shardingsphere.infra.expr.core.InlineExpressionParserFactory;
+import org.apache.shardingsphere.infra.instance.InstanceContext;
import
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
+import org.apache.shardingsphere.infra.rule.identifier.scope.DatabaseRule;
import
org.apache.shardingsphere.infra.rule.identifier.type.datasource.DataSourceMapperContainedRule;
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
-import
org.apache.shardingsphere.infra.algorithm.keygen.core.KeyGenerateAlgorithm;
import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration;
import
org.apache.shardingsphere.sharding.api.config.rule.ShardingAutoTableRuleConfiguration;
import
org.apache.shardingsphere.sharding.api.config.rule.ShardingTableRuleConfiguration;
+import org.apache.shardingsphere.sharding.rule.ShardingRule;
import org.apache.shardingsphere.sharding.spi.ShardingAlgorithm;
+import javax.sql.DataSource;
import java.util.Collection;
import java.util.HashSet;
+import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.LinkedList;
+import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
/**
- * Sharding rule configuration import checker.
+ * Sharding import rule configuration provider.
*/
-public final class ShardingRuleConfigurationImportChecker {
+public final class ShardingImportRuleConfigurationProvider implements
ImportRuleConfigurationProvider {
- /**
- * Check sharding rule configuration.
- *
- * @param database database
- * @param currentRuleConfig current rule configuration
- */
- public void check(final ShardingSphereDatabase database, final
ShardingRuleConfiguration currentRuleConfig) {
- if (null == database || null == currentRuleConfig) {
+ @Override
+ public void check(final ShardingSphereDatabase database, final
RuleConfiguration ruleConfig) {
+ if (null == database || null == ruleConfig) {
return;
}
+ ShardingRuleConfiguration shardingRuleConfig =
(ShardingRuleConfiguration) ruleConfig;
String databaseName = database.getName();
- checkLogicTables(databaseName, currentRuleConfig);
- checkResources(databaseName, database, currentRuleConfig);
-
checkShardingAlgorithms(currentRuleConfig.getShardingAlgorithms().values());
-
checkKeyGeneratorAlgorithms(currentRuleConfig.getKeyGenerators().values());
+ checkLogicTables(databaseName, shardingRuleConfig);
+ checkResources(databaseName, database, shardingRuleConfig);
+
checkShardingAlgorithms(shardingRuleConfig.getShardingAlgorithms().values());
+
checkKeyGeneratorAlgorithms(shardingRuleConfig.getKeyGenerators().values());
+ }
+
+ @Override
+ public DatabaseRule build(final ShardingSphereDatabase database, final
RuleConfiguration ruleConfig, final InstanceContext instanceContext) {
+ ShardingRuleConfiguration shardingRuleConfig =
(ShardingRuleConfiguration) ruleConfig;
+ Map<String, DataSource> dataSources =
database.getResourceMetaData().getStorageUnits().entrySet().stream()
+ .collect(Collectors.toMap(Entry::getKey, storageUnit ->
storageUnit.getValue().getDataSource(), (oldValue, currentValue) -> oldValue,
LinkedHashMap::new));
+ return new ShardingRule(shardingRuleConfig, dataSources,
instanceContext);
}
private void checkLogicTables(final String databaseName, final
ShardingRuleConfiguration currentRuleConfig) {
@@ -111,4 +123,9 @@ public final class ShardingRuleConfigurationImportChecker {
private void checkKeyGeneratorAlgorithms(final
Collection<AlgorithmConfiguration> algorithmConfigs) {
algorithmConfigs.stream().filter(Objects::nonNull).forEach(each ->
TypedSPILoader.checkService(KeyGenerateAlgorithm.class, each.getType(),
each.getProps()));
}
+
+ @Override
+ public Class<? extends RuleConfiguration> getType() {
+ return ShardingRuleConfiguration.class;
+ }
}
diff --git
a/features/sharding/distsql/handler/src/main/resources/META-INF/services/org.apache.shardingsphere.distsql.handler.engine.update.ral.rule.spi.database.ImportRuleConfigurationProvider
b/features/sharding/distsql/handler/src/main/resources/META-INF/services/org.apache.shardingsphere.distsql.handler.engine.update.ral.rule.spi.database.ImportRuleConfigurationProvider
new file mode 100644
index 00000000000..896e8984273
--- /dev/null
+++
b/features/sharding/distsql/handler/src/main/resources/META-INF/services/org.apache.shardingsphere.distsql.handler.engine.update.ral.rule.spi.database.ImportRuleConfigurationProvider
@@ -0,0 +1,18 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+org.apache.shardingsphere.sharding.distsql.handler.provider.ShardingImportRuleConfigurationProvider
diff --git
a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/common/checker/ShardingRuleConfigurationImportCheckerTest.java
b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/provider/ShardingImportRuleConfigurationProviderTest.java
similarity index 86%
rename from
proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/common/checker/ShardingRuleConfigurationImportCheckerTest.java
rename to
features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/provider/ShardingImportRuleConfigurationProviderTest.java
index d33cc2dfd3e..2a7daf22128 100644
---
a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/common/checker/ShardingRuleConfigurationImportCheckerTest.java
+++
b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/provider/ShardingImportRuleConfigurationProviderTest.java
@@ -15,7 +15,7 @@
* limitations under the License.
*/
-package
org.apache.shardingsphere.proxy.backend.handler.distsql.ral.common.checker;
+package org.apache.shardingsphere.sharding.distsql.provider;
import
org.apache.shardingsphere.distsql.handler.exception.rule.DuplicateRuleException;
import
org.apache.shardingsphere.distsql.handler.exception.storageunit.MissingRequiredStorageUnitsException;
@@ -25,6 +25,7 @@ import
org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData;
import
org.apache.shardingsphere.infra.spi.exception.ServiceProviderNotFoundException;
import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration;
import
org.apache.shardingsphere.sharding.api.config.rule.ShardingTableRuleConfiguration;
+import
org.apache.shardingsphere.sharding.distsql.handler.provider.ShardingImportRuleConfigurationProvider;
import org.junit.jupiter.api.Test;
import java.util.Collection;
@@ -37,28 +38,28 @@ import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
-class ShardingRuleConfigurationImportCheckerTest {
+class ShardingImportRuleConfigurationProviderTest {
@Test
void assertCheckLogicTables() {
- assertThrows(DuplicateRuleException.class, () -> new
ShardingRuleConfigurationImportChecker().check(mock(ShardingSphereDatabase.class),
createDuplicatedTablesRuleConfiguration()));
+ assertThrows(DuplicateRuleException.class, () -> new
ShardingImportRuleConfigurationProvider().check(mock(ShardingSphereDatabase.class),
createDuplicatedTablesRuleConfiguration()));
}
@Test
void assertCheckDataSources() {
ShardingRuleConfiguration currentRuleConfig = new
ShardingRuleConfiguration();
currentRuleConfig.getTables().add(new
ShardingTableRuleConfiguration("t_order", "ds_${0..2}.t_order_${0..2}"));
- assertThrows(MissingRequiredStorageUnitsException.class, () -> new
ShardingRuleConfigurationImportChecker().check(mockDatabaseWithDataSource(),
currentRuleConfig));
+ assertThrows(MissingRequiredStorageUnitsException.class, () -> new
ShardingImportRuleConfigurationProvider().check(mockDatabaseWithDataSource(),
currentRuleConfig));
}
@Test
void assertCheckKeyGenerators() {
- assertThrows(ServiceProviderNotFoundException.class, () -> new
ShardingRuleConfigurationImportChecker().check(mockDatabase(),
createInvalidKeyGeneratorRuleConfiguration()));
+ assertThrows(ServiceProviderNotFoundException.class, () -> new
ShardingImportRuleConfigurationProvider().check(mockDatabase(),
createInvalidKeyGeneratorRuleConfiguration()));
}
@Test
void assertCheckShardingAlgorithms() {
- assertThrows(ServiceProviderNotFoundException.class, () -> new
ShardingRuleConfigurationImportChecker().check(mockDatabase(),
createInvalidShardingAlgorithmRuleConfiguration()));
+ assertThrows(ServiceProviderNotFoundException.class, () -> new
ShardingImportRuleConfigurationProvider().check(mockDatabase(),
createInvalidShardingAlgorithmRuleConfiguration()));
}
private ShardingRuleConfiguration
createDuplicatedTablesRuleConfiguration() {
diff --git
a/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/engine/update/ral/rule/spi/database/ImportRuleConfigurationProvider.java
b/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/engine/update/ral/rule/spi/database/ImportRuleConfigurationProvider.java
new file mode 100644
index 00000000000..c4859c2938a
--- /dev/null
+++
b/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/engine/update/ral/rule/spi/database/ImportRuleConfigurationProvider.java
@@ -0,0 +1,53 @@
+/*
+ * 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.distsql.handler.engine.update.ral.rule.spi.database;
+
+import org.apache.shardingsphere.infra.config.rule.RuleConfiguration;
+import org.apache.shardingsphere.infra.instance.InstanceContext;
+import
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
+import org.apache.shardingsphere.infra.rule.identifier.scope.DatabaseRule;
+import org.apache.shardingsphere.infra.spi.annotation.SingletonSPI;
+import org.apache.shardingsphere.infra.spi.type.typed.TypedSPI;
+
+/**
+ * Import rule configuration provider.
+ */
+@SingletonSPI
+public interface ImportRuleConfigurationProvider extends TypedSPI {
+
+ /**
+ * Check rule configuration.
+ *
+ * @param database database
+ * @param ruleConfig rule configuration
+ */
+ void check(ShardingSphereDatabase database, RuleConfiguration ruleConfig);
+
+ /**
+ * Build database rule.
+ *
+ * @param database database
+ * @param ruleConfig rule configuration
+ * @param instanceContext instance context
+ * @return built database rule
+ */
+ DatabaseRule build(ShardingSphereDatabase database, RuleConfiguration
ruleConfig, InstanceContext instanceContext);
+
+ @Override
+ Class<? extends RuleConfiguration> getType();
+}
diff --git
a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/util/YamlDatabaseConfigurationImportExecutor.java
b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/util/YamlDatabaseConfigurationImportExecutor.java
index 0c24b855bcd..aca52d5b604 100644
---
a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/util/YamlDatabaseConfigurationImportExecutor.java
+++
b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/util/YamlDatabaseConfigurationImportExecutor.java
@@ -19,6 +19,7 @@ package org.apache.shardingsphere.proxy.backend.util;
import
org.apache.shardingsphere.broadcast.api.config.BroadcastRuleConfiguration;
import org.apache.shardingsphere.broadcast.rule.BroadcastRule;
+import
org.apache.shardingsphere.distsql.handler.engine.update.ral.rule.spi.database.ImportRuleConfigurationProvider;
import
org.apache.shardingsphere.distsql.handler.exception.datasource.MissingRequiredDataSourcesException;
import
org.apache.shardingsphere.distsql.handler.exception.storageunit.InvalidStorageUnitsException;
import
org.apache.shardingsphere.distsql.handler.validate.DataSourcePoolPropertiesValidator;
@@ -41,6 +42,7 @@ import
org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUn
import
org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnitNodeMapCreator;
import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
import org.apache.shardingsphere.infra.spi.type.ordered.OrderedSPILoader;
+import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
import
org.apache.shardingsphere.infra.yaml.config.pojo.rule.YamlRuleConfiguration;
import
org.apache.shardingsphere.infra.yaml.config.swapper.rule.YamlRuleConfigurationSwapper;
import org.apache.shardingsphere.mask.api.config.MaskRuleConfiguration;
@@ -55,17 +57,14 @@ import
org.apache.shardingsphere.proxy.backend.handler.distsql.ral.common.checke
import
org.apache.shardingsphere.proxy.backend.handler.distsql.ral.common.checker.MaskRuleConfigurationImportChecker;
import
org.apache.shardingsphere.proxy.backend.handler.distsql.ral.common.checker.ReadwriteSplittingRuleConfigurationImportChecker;
import
org.apache.shardingsphere.proxy.backend.handler.distsql.ral.common.checker.ShadowRuleConfigurationImportChecker;
-import
org.apache.shardingsphere.proxy.backend.handler.distsql.ral.common.checker.ShardingRuleConfigurationImportChecker;
import
org.apache.shardingsphere.readwritesplitting.api.ReadwriteSplittingRuleConfiguration;
import
org.apache.shardingsphere.readwritesplitting.rule.ReadwriteSplittingRule;
import org.apache.shardingsphere.shadow.api.config.ShadowRuleConfiguration;
import org.apache.shardingsphere.shadow.rule.ShadowRule;
import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration;
-import org.apache.shardingsphere.sharding.rule.ShardingRule;
import org.apache.shardingsphere.single.api.config.SingleRuleConfiguration;
import org.apache.shardingsphere.single.rule.SingleRule;
-import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.Collection;
import java.util.Collections;
@@ -82,8 +81,6 @@ import java.util.stream.Collectors;
*/
public final class YamlDatabaseConfigurationImportExecutor {
- private final ShardingRuleConfigurationImportChecker
shardingRuleConfigImportChecker = new ShardingRuleConfigurationImportChecker();
-
private final ReadwriteSplittingRuleConfigurationImportChecker
readwriteSplittingRuleConfigImportChecker = new
ReadwriteSplittingRuleConfigurationImportChecker();
private final EncryptRuleConfigurationImportChecker
encryptRuleConfigImportChecker = new EncryptRuleConfigurationImportChecker();
@@ -154,18 +151,6 @@ public final class YamlDatabaseConfigurationImportExecutor
{
}
}
- @SuppressWarnings({"rawtypes", "unchecked"})
- private Map<Integer, Collection<RuleConfiguration>>
swapToRuleConfigs(final Collection<YamlRuleConfiguration> yamlRuleConfigs) {
- Map<Integer, Collection<RuleConfiguration>> result = new
TreeMap<>(Comparator.reverseOrder());
- for (YamlRuleConfiguration each : yamlRuleConfigs) {
- YamlRuleConfigurationSwapper swapper =
OrderedSPILoader.getServicesByClass(YamlRuleConfigurationSwapper.class,
Collections.singleton(each.getRuleConfigurationType()))
- .get(each.getRuleConfigurationType());
- result.computeIfAbsent(swapper.getOrder(), key -> new
LinkedList<>());
- result.get(swapper.getOrder()).add((RuleConfiguration)
swapper.swapToObject(each));
- }
- return result;
- }
-
private void addRules(final String databaseName, final
Collection<YamlRuleConfiguration> yamlRuleConfigs) {
if (null == yamlRuleConfigs || yamlRuleConfigs.isEmpty()) {
return;
@@ -182,8 +167,12 @@ public final class YamlDatabaseConfigurationImportExecutor
{
if (null == ruleConfig) {
return;
}
+ InstanceContext instanceContext =
ProxyContext.getInstance().getContextManager().getInstanceContext();
if (ruleConfig instanceof ShardingRuleConfiguration) {
- ruleConfigs.forEach(each ->
addShardingRuleConfiguration((ShardingRuleConfiguration) each, allRuleConfigs,
database));
+ ImportRuleConfigurationProvider importRuleConfigurationProvider =
TypedSPILoader.getService(ImportRuleConfigurationProvider.class,
ruleConfig.getClass());
+ importRuleConfigurationProvider.check(database, ruleConfig);
+ allRuleConfigs.add(ruleConfig);
+
database.getRuleMetaData().getRules().add(importRuleConfigurationProvider.build(database,
ruleConfig, instanceContext));
} else if (ruleConfig instanceof ReadwriteSplittingRuleConfiguration) {
ruleConfigs.forEach(each ->
addReadwriteSplittingRuleConfiguration((ReadwriteSplittingRuleConfiguration)
each, allRuleConfigs, database));
} else if (ruleConfig instanceof EncryptRuleConfiguration) {
@@ -199,13 +188,16 @@ public final class
YamlDatabaseConfigurationImportExecutor {
}
}
- private void addShardingRuleConfiguration(final ShardingRuleConfiguration
shardingRuleConfig, final Collection<RuleConfiguration> allRuleConfigs, final
ShardingSphereDatabase database) {
- InstanceContext instanceContext =
ProxyContext.getInstance().getContextManager().getInstanceContext();
- shardingRuleConfigImportChecker.check(database, shardingRuleConfig);
- allRuleConfigs.add(shardingRuleConfig);
- Map<String, DataSource> dataSources =
database.getResourceMetaData().getStorageUnits().entrySet().stream()
- .collect(Collectors.toMap(Map.Entry::getKey, storageUnit ->
storageUnit.getValue().getDataSource(), (oldValue, currentValue) -> oldValue,
LinkedHashMap::new));
- database.getRuleMetaData().getRules().add(new
ShardingRule(shardingRuleConfig, dataSources, instanceContext));
+ @SuppressWarnings({"rawtypes", "unchecked"})
+ private Map<Integer, Collection<RuleConfiguration>>
swapToRuleConfigs(final Collection<YamlRuleConfiguration> yamlRuleConfigs) {
+ Map<Integer, Collection<RuleConfiguration>> result = new
TreeMap<>(Comparator.reverseOrder());
+ for (YamlRuleConfiguration each : yamlRuleConfigs) {
+ YamlRuleConfigurationSwapper swapper =
OrderedSPILoader.getServicesByClass(YamlRuleConfigurationSwapper.class,
Collections.singleton(each.getRuleConfigurationType()))
+ .get(each.getRuleConfigurationType());
+ result.computeIfAbsent(swapper.getOrder(), key -> new
LinkedList<>());
+ result.get(swapper.getOrder()).add((RuleConfiguration)
swapper.swapToObject(each));
+ }
+ return result;
}
private void addReadwriteSplittingRuleConfiguration(final
ReadwriteSplittingRuleConfiguration readwriteSplittingRuleConfig,