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 2023/01/04 08:15:32 UTC
[shardingsphere] branch master updated: Add `ShadowRuleConfigurationImportChecker` (#23322)
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 b270e4d7f1f Add `ShadowRuleConfigurationImportChecker` (#23322)
b270e4d7f1f is described below
commit b270e4d7f1fd767e07534469476e7d02f40193be
Author: Zichao <57...@users.noreply.github.com>
AuthorDate: Wed Jan 4 21:15:26 2023 +1300
Add `ShadowRuleConfigurationImportChecker` (#23322)
* Add `ShadowRuleConfigurationImportChecker` for `ImportDatabaseConfigurationHandler`
* Add `ShadowRuleConfigurationImportChecker` for `ImportDatabaseConfigurationHandler`
* Add `ShadowRuleConfigurationImportChecker` for `ImportDatabaseConfigurationHandler`
* Add `ShadowRuleConfigurationImportChecker` for `ImportDatabaseConfigurationHandler`
---
.../ShadowRuleConfigurationImportChecker.java | 95 ++++++++++++++++++++++
.../ImportDatabaseConfigurationHandler.java | 9 +-
2 files changed, 101 insertions(+), 3 deletions(-)
diff --git a/proxy/backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/common/checker/ShadowRuleConfigurationImportChecker.java b/proxy/backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/common/checker/ShadowRuleConfigurationImportChecker.java
new file mode 100644
index 00000000000..22d0259d754
--- /dev/null
+++ b/proxy/backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/common/checker/ShadowRuleConfigurationImportChecker.java
@@ -0,0 +1,95 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.proxy.backend.handler.distsql.ral.common.checker;
+
+import org.apache.shardingsphere.distsql.handler.exception.algorithm.InvalidAlgorithmConfigurationException;
+import org.apache.shardingsphere.distsql.handler.exception.rule.DuplicateRuleException;
+import org.apache.shardingsphere.distsql.handler.exception.storageunit.MissingRequiredStorageUnitsException;
+import org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration;
+import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
+import org.apache.shardingsphere.infra.rule.identifier.type.DataSourceContainedRule;
+import org.apache.shardingsphere.infra.util.exception.ShardingSpherePreconditions;
+import org.apache.shardingsphere.infra.util.spi.type.typed.TypedSPIRegistry;
+import org.apache.shardingsphere.shadow.api.config.ShadowRuleConfiguration;
+import org.apache.shardingsphere.shadow.spi.ShadowAlgorithm;
+
+import java.util.Collection;
+import java.util.LinkedHashSet;
+import java.util.Map;
+import java.util.stream.Collectors;
+
+/**
+ * Shadow rule configuration import checker.
+ */
+public final class ShadowRuleConfigurationImportChecker {
+
+ /**
+ * Check shadow rule configuration.
+ *
+ * @param database database
+ * @param currentRuleConfig current rule configuration
+ */
+ public void check(final ShardingSphereDatabase database, final ShadowRuleConfiguration currentRuleConfig) {
+ if (null == database || null == currentRuleConfig) {
+ return;
+ }
+ String databaseName = database.getName();
+ checkDataSources(databaseName, database, currentRuleConfig);
+ checkTables(currentRuleConfig, databaseName);
+ checkShadowAlgorithms(currentRuleConfig);
+ }
+
+ private void checkDataSources(final String databaseName, final ShardingSphereDatabase database, final ShadowRuleConfiguration currentRuleConfig) {
+ Collection<String> requiredResource = getRequiredResources(currentRuleConfig);
+ Collection<String> notExistedResources = database.getResourceMetaData().getNotExistedResources(requiredResource);
+ Collection<String> logicResources = getLogicDataSources(database);
+ notExistedResources.removeIf(logicResources::contains);
+ ShardingSpherePreconditions.checkState(notExistedResources.isEmpty(), () -> new MissingRequiredStorageUnitsException(databaseName, notExistedResources));
+ }
+
+ private Collection<String> getRequiredResources(final ShadowRuleConfiguration currentRuleConfig) {
+ Collection<String> result = new LinkedHashSet<>();
+ currentRuleConfig.getDataSources().forEach(each -> {
+ if (null != each.getShadowDataSourceName()) {
+ result.add(each.getShadowDataSourceName());
+ }
+ if (null != each.getProductionDataSourceName()) {
+ result.add(each.getProductionDataSourceName());
+ }
+ });
+ return result;
+ }
+
+ private Collection<String> getLogicDataSources(final ShardingSphereDatabase database) {
+ return database.getRuleMetaData().getRules().stream().filter(each -> each instanceof DataSourceContainedRule)
+ .map(each -> ((DataSourceContainedRule) each).getDataSourceMapper().keySet()).flatMap(Collection::stream).collect(Collectors.toCollection(LinkedHashSet::new));
+ }
+
+ private void checkTables(final ShadowRuleConfiguration currentRuleConfig, final String databaseName) {
+ Collection<String> tableNames = currentRuleConfig.getTables().keySet();
+ Collection<String> duplicatedTables = tableNames.stream().collect(Collectors.groupingBy(each -> each, Collectors.counting())).entrySet().stream()
+ .filter(each -> each.getValue() > 1).map(Map.Entry::getKey).collect(Collectors.toSet());
+ ShardingSpherePreconditions.checkState(duplicatedTables.isEmpty(), () -> new DuplicateRuleException("SHADOW", databaseName, duplicatedTables));
+ }
+
+ private void checkShadowAlgorithms(final ShadowRuleConfiguration currentRuleConfig) {
+ Collection<String> notExistedAlgorithms = currentRuleConfig.getShadowAlgorithms().values().stream().map(AlgorithmConfiguration::getType)
+ .filter(each -> !TypedSPIRegistry.findRegisteredService(ShadowAlgorithm.class, each).isPresent()).collect(Collectors.toList());
+ ShardingSpherePreconditions.checkState(notExistedAlgorithms.isEmpty(), () -> new InvalidAlgorithmConfigurationException("Shadow algorithms", notExistedAlgorithms));
+ }
+}
diff --git a/proxy/backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/ImportDatabaseConfigurationHandler.java b/proxy/backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/ImportDatabaseConfigurationHandler.java
index 11f918a3d8c..52db838fb22 100644
--- a/proxy/backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/ImportDatabaseConfigurationHandler.java
+++ b/proxy/backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/ImportDatabaseConfigurationHandler.java
@@ -22,6 +22,8 @@ import com.zaxxer.hikari.HikariDataSource;
import org.apache.shardingsphere.dbdiscovery.api.config.DatabaseDiscoveryRuleConfiguration;
import org.apache.shardingsphere.dbdiscovery.yaml.config.YamlDatabaseDiscoveryRuleConfiguration;
import org.apache.shardingsphere.dbdiscovery.yaml.swapper.YamlDatabaseDiscoveryRuleConfigurationSwapper;
+import org.apache.shardingsphere.distsql.handler.exception.DistSQLException;
+import org.apache.shardingsphere.distsql.handler.exception.storageunit.InvalidStorageUnitsException;
import org.apache.shardingsphere.distsql.handler.validate.DataSourcePropertiesValidateHandler;
import org.apache.shardingsphere.distsql.parser.statement.ral.updatable.ImportDatabaseConfigurationStatement;
import org.apache.shardingsphere.encrypt.api.config.EncryptRuleConfiguration;
@@ -30,8 +32,6 @@ import org.apache.shardingsphere.encrypt.yaml.swapper.YamlEncryptRuleConfigurati
import org.apache.shardingsphere.infra.config.rule.RuleConfiguration;
import org.apache.shardingsphere.infra.datasource.props.DataSourceProperties;
import org.apache.shardingsphere.infra.datasource.props.DataSourcePropertiesCreator;
-import org.apache.shardingsphere.distsql.handler.exception.DistSQLException;
-import org.apache.shardingsphere.distsql.handler.exception.storageunit.InvalidStorageUnitsException;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.util.yaml.YamlEngine;
import org.apache.shardingsphere.infra.yaml.config.pojo.rule.YamlRuleConfiguration;
@@ -48,6 +48,7 @@ import org.apache.shardingsphere.proxy.backend.exception.FileIOException;
import org.apache.shardingsphere.proxy.backend.handler.distsql.ral.UpdatableRALBackendHandler;
import org.apache.shardingsphere.proxy.backend.handler.distsql.ral.common.checker.DatabaseDiscoveryRuleConfigurationImportChecker;
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.yaml.config.YamlReadwriteSplittingRuleConfiguration;
@@ -80,6 +81,8 @@ public final class ImportDatabaseConfigurationHandler extends UpdatableRALBacken
private final DatabaseDiscoveryRuleConfigurationImportChecker databaseDiscoveryRuleConfigurationImportChecker = new DatabaseDiscoveryRuleConfigurationImportChecker();
+ private final ShadowRuleConfigurationImportChecker shadowRuleConfigurationImportChecker = new ShadowRuleConfigurationImportChecker();
+
private final YamlProxyDataSourceConfigurationSwapper dataSourceConfigSwapper = new YamlProxyDataSourceConfigurationSwapper();
private final DataSourcePropertiesValidateHandler validateHandler = new DataSourcePropertiesValidateHandler();
@@ -160,7 +163,7 @@ public final class ImportDatabaseConfigurationHandler extends UpdatableRALBacken
ruleConfigs.add(encryptRuleConfig);
} else if (each instanceof YamlShadowRuleConfiguration) {
ShadowRuleConfiguration shadowRuleConfig = new YamlShadowRuleConfigurationSwapper().swapToObject((YamlShadowRuleConfiguration) each);
- // TODO check
+ shadowRuleConfigurationImportChecker.check(database, shadowRuleConfig);
ruleConfigs.add(shadowRuleConfig);
} else if (each instanceof YamlMaskRuleConfiguration) {
MaskRuleConfiguration maskRuleConfig = new YamlMaskRuleConfigurationSwapper().swapToObject((YamlMaskRuleConfiguration) each);