You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by zh...@apache.org on 2022/06/29 11:02:53 UTC
[shardingsphere] branch master updated: add sharding audit (#18678)
This is an automated email from the ASF dual-hosted git repository.
zhangliang 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 d646f7a26f8 add sharding audit (#18678)
d646f7a26f8 is described below
commit d646f7a26f8bc0ae378d90d94f78fb54a405f864
Author: natehuang <na...@tencent.com>
AuthorDate: Wed Jun 29 19:02:43 2022 +0800
add sharding audit (#18678)
* add sharding audit
* Adjust the audit configuration, add the parameter allowHintDisable
* rename shardingAuditAlgorithm to shardingAuditAlgorithmName
---
.../api/config/ShardingRuleConfiguration.java | 5 ++
.../audit/ShardingAuditStrategyConfiguration.java} | 28 ++++----
.../factory/ShardingAuditAlgorithmFactory.java | 57 ++++++++++++++++
.../sharding/spi/ShardingAuditAlgorithm.java | 47 ++++++++++++++
...AlgorithmProvidedShardingRuleConfiguration.java | 6 ++
.../checker/audit/ShardingAuditChecker.java | 75 ++++++++++++++++++++++
.../sharding/constant/ShardingOrder.java | 5 ++
.../shardingsphere/sharding/rule/ShardingRule.java | 20 ++++++
.../yaml/config/YamlShardingRuleConfiguration.java | 5 ++
.../YamlShardingAuditStrategyConfiguration.java} | 24 +++----
...eAlgorithmProviderConfigurationYamlSwapper.java | 9 +++
.../ShardingRuleConfigurationYamlSwapper.java | 8 +++
...rdingAuditStrategyConfigurationYamlSwapper.java | 41 ++++++++++++
....shardingsphere.infra.executor.check.SQLChecker | 18 ++++++
14 files changed, 320 insertions(+), 28 deletions(-)
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-api/src/main/java/org/apache/shardingsphere/sharding/api/config/ShardingRuleConfiguration.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-api/src/main/java/org/apache/shardingsphere/sharding/api/config/ShardingRuleConfiguration.java
index ee7e5e460ed..8e35e213c98 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-api/src/main/java/org/apache/shardingsphere/sharding/api/config/ShardingRuleConfiguration.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-api/src/main/java/org/apache/shardingsphere/sharding/api/config/ShardingRuleConfiguration.java
@@ -25,6 +25,7 @@ import org.apache.shardingsphere.infra.config.rulealtered.OnRuleAlteredActionCon
import org.apache.shardingsphere.infra.config.scope.DatabaseRuleConfiguration;
import org.apache.shardingsphere.sharding.api.config.rule.ShardingAutoTableRuleConfiguration;
import org.apache.shardingsphere.sharding.api.config.rule.ShardingTableRuleConfiguration;
+import org.apache.shardingsphere.sharding.api.config.strategy.audit.ShardingAuditStrategyConfiguration;
import org.apache.shardingsphere.sharding.api.config.strategy.keygen.KeyGenerateStrategyConfiguration;
import org.apache.shardingsphere.sharding.api.config.strategy.sharding.ShardingStrategyConfiguration;
@@ -60,7 +61,11 @@ public final class ShardingRuleConfiguration implements DatabaseRuleConfiguratio
private Map<String, ShardingSphereAlgorithmConfiguration> keyGenerators = new LinkedHashMap<>();
+ private Map<String, ShardingSphereAlgorithmConfiguration> shardingAuditAlgorithms = new LinkedHashMap<>();
+
private String scalingName;
private Map<String, OnRuleAlteredActionConfiguration> scaling = new LinkedHashMap<>();
+
+ private Map<String, ShardingAuditStrategyConfiguration> shardingAudits = new LinkedHashMap<>();
}
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/constant/ShardingOrder.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-api/src/main/java/org/apache/shardingsphere/sharding/api/config/strategy/audit/ShardingAuditStrategyConfiguration.java
similarity index 54%
copy from shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/constant/ShardingOrder.java
copy to shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-api/src/main/java/org/apache/shardingsphere/sharding/api/config/strategy/audit/ShardingAuditStrategyConfiguration.java
index 22acab09d77..45bd1d4239e 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/constant/ShardingOrder.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-api/src/main/java/org/apache/shardingsphere/sharding/api/config/strategy/audit/ShardingAuditStrategyConfiguration.java
@@ -15,24 +15,24 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.sharding.constant;
+package org.apache.shardingsphere.sharding.api.config.strategy.audit;
-import lombok.AccessLevel;
-import lombok.NoArgsConstructor;
+import com.google.common.base.Preconditions;
+import lombok.Getter;
/**
- * Sharding order.
+ * Sharding audit strategy configuration.
*/
-@NoArgsConstructor(access = AccessLevel.PRIVATE)
-public final class ShardingOrder {
+@Getter
+public final class ShardingAuditStrategyConfiguration {
- /**
- * Sharding order.
- */
- public static final int ORDER = -10;
+ private final String shardingAuditAlgorithmName;
- /**
- * Algorithm provider sharding order.
- */
- public static final int ALGORITHM_PROVIDER_ORDER = ORDER + 1;
+ private final boolean allowHintDisable;
+
+ public ShardingAuditStrategyConfiguration(final String shardingAuditAlgorithmName, final boolean allowHintDisable) {
+ Preconditions.checkNotNull(shardingAuditAlgorithmName, "Sharding algorithm name is required.");
+ this.shardingAuditAlgorithmName = shardingAuditAlgorithmName;
+ this.allowHintDisable = allowHintDisable;
+ }
}
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-api/src/main/java/org/apache/shardingsphere/sharding/factory/ShardingAuditAlgorithmFactory.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-api/src/main/java/org/apache/shardingsphere/sharding/factory/ShardingAuditAlgorithmFactory.java
new file mode 100644
index 00000000000..3d852d81d3d
--- /dev/null
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-api/src/main/java/org/apache/shardingsphere/sharding/factory/ShardingAuditAlgorithmFactory.java
@@ -0,0 +1,57 @@
+/*
+ * 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.sharding.factory;
+
+import lombok.AccessLevel;
+import lombok.NoArgsConstructor;
+import org.apache.shardingsphere.infra.config.algorithm.ShardingSphereAlgorithmConfiguration;
+import org.apache.shardingsphere.infra.config.algorithm.ShardingSphereAlgorithmFactory;
+import org.apache.shardingsphere.sharding.spi.ShardingAuditAlgorithm;
+import org.apache.shardingsphere.spi.ShardingSphereServiceLoader;
+import org.apache.shardingsphere.spi.type.typed.TypedSPIRegistry;
+
+/**
+ * Sharding audit algorithm factory.
+ */
+@NoArgsConstructor(access = AccessLevel.PRIVATE)
+public class ShardingAuditAlgorithmFactory {
+
+ static {
+ ShardingSphereServiceLoader.register(ShardingAuditAlgorithm.class);
+ }
+
+ /**
+ * Create new instance of sharding audit check algorithm.
+ *
+ * @param shardingAuditAlgorithmConfig sharding algorithm configuration
+ * @return created instance
+ */
+ public static ShardingAuditAlgorithm newInstance(final ShardingSphereAlgorithmConfiguration shardingAuditAlgorithmConfig) {
+ return ShardingSphereAlgorithmFactory.createAlgorithm(shardingAuditAlgorithmConfig, ShardingAuditAlgorithm.class);
+ }
+
+ /**
+ * Judge whether contains sharding audit algorithm.
+ *
+ * @param shardingAuditAlgorithmType sharding audit algorithm type
+ * @return contains sharding audit algorithm or not
+ */
+ public static boolean contains(final String shardingAuditAlgorithmType) {
+ return TypedSPIRegistry.findRegisteredService(ShardingAuditAlgorithm.class, shardingAuditAlgorithmType).isPresent();
+ }
+}
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-api/src/main/java/org/apache/shardingsphere/sharding/spi/ShardingAuditAlgorithm.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-api/src/main/java/org/apache/shardingsphere/sharding/spi/ShardingAuditAlgorithm.java
new file mode 100644
index 00000000000..4afed78c067
--- /dev/null
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-api/src/main/java/org/apache/shardingsphere/sharding/spi/ShardingAuditAlgorithm.java
@@ -0,0 +1,47 @@
+/*
+ * 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.sharding.spi;
+
+import org.apache.shardingsphere.infra.check.SQLCheckResult;
+import org.apache.shardingsphere.infra.config.algorithm.ShardingSphereAlgorithm;
+import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
+import org.apache.shardingsphere.infra.metadata.user.Grantee;
+import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
+import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
+
+import java.util.List;
+import java.util.Map;
+
+/**
+ * Sharding audit algorithm.
+ */
+public interface ShardingAuditAlgorithm extends ShardingSphereAlgorithm {
+
+ /**
+ * Sharding audit algorithm SQL check.
+ *
+ * @param sqlStatement SQL statement
+ * @param parameters SQL parameters
+ * @param currentDatabase current database
+ * @param databases databases
+ * @param grantee grantee
+ * @param rule shardingSphereRule
+ * @return SQL check result
+ */
+ SQLCheckResult check(SQLStatement sqlStatement, List<Object> parameters, Grantee grantee, String currentDatabase, Map<String, ShardingSphereDatabase> databases, ShardingSphereRule rule);
+}
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/algorithm/config/AlgorithmProvidedShardingRuleConfiguration.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/algorithm/config/AlgorithmProvidedShardingRuleConfiguration.java
index 3a828abeac1..ba6906ba542 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/algorithm/config/AlgorithmProvidedShardingRuleConfiguration.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/algorithm/config/AlgorithmProvidedShardingRuleConfiguration.java
@@ -23,10 +23,12 @@ import org.apache.shardingsphere.infra.config.function.DistributedRuleConfigurat
import org.apache.shardingsphere.infra.config.scope.DatabaseRuleConfiguration;
import org.apache.shardingsphere.sharding.api.config.rule.ShardingAutoTableRuleConfiguration;
import org.apache.shardingsphere.sharding.api.config.rule.ShardingTableRuleConfiguration;
+import org.apache.shardingsphere.sharding.api.config.strategy.audit.ShardingAuditStrategyConfiguration;
import org.apache.shardingsphere.sharding.api.config.strategy.keygen.KeyGenerateStrategyConfiguration;
import org.apache.shardingsphere.sharding.api.config.strategy.sharding.ShardingStrategyConfiguration;
import org.apache.shardingsphere.sharding.spi.KeyGenerateAlgorithm;
import org.apache.shardingsphere.sharding.spi.ShardingAlgorithm;
+import org.apache.shardingsphere.sharding.spi.ShardingAuditAlgorithm;
import java.util.Collection;
import java.util.LinkedHashMap;
@@ -59,4 +61,8 @@ public final class AlgorithmProvidedShardingRuleConfiguration implements Databas
private Map<String, ShardingAlgorithm> shardingAlgorithms = new LinkedHashMap<>();
private Map<String, KeyGenerateAlgorithm> keyGenerators = new LinkedHashMap<>();
+
+ private Map<String, ShardingAuditAlgorithm> shardingAuditAlgorithms = new LinkedHashMap<>();
+
+ private Map<String, ShardingAuditStrategyConfiguration> shardingAudits = new LinkedHashMap<>();
}
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/checker/audit/ShardingAuditChecker.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/checker/audit/ShardingAuditChecker.java
new file mode 100644
index 00000000000..f1ac351996a
--- /dev/null
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/checker/audit/ShardingAuditChecker.java
@@ -0,0 +1,75 @@
+/*
+ * 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.sharding.checker.audit;
+
+import org.apache.shardingsphere.infra.check.SQLCheckResult;
+import org.apache.shardingsphere.infra.executor.check.SQLChecker;
+import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
+import org.apache.shardingsphere.infra.metadata.user.Grantee;
+import org.apache.shardingsphere.sharding.api.config.strategy.audit.ShardingAuditStrategyConfiguration;
+import org.apache.shardingsphere.sharding.constant.ShardingOrder;
+import org.apache.shardingsphere.sharding.rule.ShardingRule;
+import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
+
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.function.BiPredicate;
+
+/**
+ * Sharding audit checker.
+ */
+public final class ShardingAuditChecker implements SQLChecker<ShardingRule> {
+
+ @Override
+ public boolean check(final String databaseName, final Grantee grantee, final ShardingRule rule) {
+ return true;
+ }
+
+ @Override
+ public SQLCheckResult check(final SQLStatement sqlStatement, final List<Object> parameters, final Grantee grantee,
+ final String currentDatabase, final Map<String, ShardingSphereDatabase> databases, final ShardingRule rule) {
+ for (Entry<String, ShardingAuditStrategyConfiguration> entry : rule.getShardingAudits().entrySet()) {
+ SQLCheckResult result = rule.getShardingAuditAlgorithms().get(entry.getValue().getShardingAuditAlgorithmName()).check(sqlStatement, parameters, grantee, currentDatabase, databases, rule);
+ if (!result.isPassed()) {
+ return result;
+ }
+ }
+ return new SQLCheckResult(true, "");
+ }
+
+ @Override
+ public boolean check(final Grantee grantee, final ShardingRule rule) {
+ return true;
+ }
+
+ @Override
+ public boolean check(final Grantee grantee, final BiPredicate<Object, Object> validator, final Object cipher, final ShardingRule rule) {
+ return true;
+ }
+
+ @Override
+ public int getOrder() {
+ return ShardingOrder.AUDIT_ORDER;
+ }
+
+ @Override
+ public Class<ShardingRule> getTypeClass() {
+ return ShardingRule.class;
+ }
+}
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/constant/ShardingOrder.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/constant/ShardingOrder.java
index 22acab09d77..38fac1b81a3 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/constant/ShardingOrder.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/constant/ShardingOrder.java
@@ -35,4 +35,9 @@ public final class ShardingOrder {
* Algorithm provider sharding order.
*/
public static final int ALGORITHM_PROVIDER_ORDER = ORDER + 1;
+
+ /**
+ * Sharding audit order.
+ */
+ public static final int AUDIT_ORDER = 1000;
}
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/rule/ShardingRule.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/rule/ShardingRule.java
index 4277c9889f8..2affa1e4313 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/rule/ShardingRule.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/rule/ShardingRule.java
@@ -41,6 +41,7 @@ import org.apache.shardingsphere.sharding.algorithm.config.AlgorithmProvidedShar
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.api.config.strategy.audit.ShardingAuditStrategyConfiguration;
import org.apache.shardingsphere.sharding.api.config.strategy.keygen.KeyGenerateStrategyConfiguration;
import org.apache.shardingsphere.sharding.api.config.strategy.sharding.ComplexShardingStrategyConfiguration;
import org.apache.shardingsphere.sharding.api.config.strategy.sharding.NoneShardingStrategyConfiguration;
@@ -49,8 +50,10 @@ import org.apache.shardingsphere.sharding.api.config.strategy.sharding.StandardS
import org.apache.shardingsphere.sharding.api.sharding.ShardingAutoTableAlgorithm;
import org.apache.shardingsphere.sharding.factory.KeyGenerateAlgorithmFactory;
import org.apache.shardingsphere.sharding.factory.ShardingAlgorithmFactory;
+import org.apache.shardingsphere.sharding.factory.ShardingAuditAlgorithmFactory;
import org.apache.shardingsphere.sharding.spi.KeyGenerateAlgorithm;
import org.apache.shardingsphere.sharding.spi.ShardingAlgorithm;
+import org.apache.shardingsphere.sharding.spi.ShardingAuditAlgorithm;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.column.ColumnSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.BinaryOperationExpression;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.ExpressionSegment;
@@ -92,6 +95,8 @@ public final class ShardingRule implements DatabaseRule, DataNodeContainedRule,
private final Map<String, KeyGenerateAlgorithm> keyGenerators = new LinkedHashMap<>();
+ private final Map<String, ShardingAuditAlgorithm> shardingAuditAlgorithms = new LinkedHashMap<>();
+
private final Map<String, TableRule> tableRules = new LinkedHashMap<>();
private final Map<String, BindingTableRule> bindingTableRules = new LinkedHashMap<>();
@@ -108,11 +113,14 @@ public final class ShardingRule implements DatabaseRule, DataNodeContainedRule,
private final Map<String, Collection<DataNode>> shardingTableDataNodes;
+ private final Map<String, ShardingAuditStrategyConfiguration> shardingAudits;
+
public ShardingRule(final ShardingRuleConfiguration config, final Collection<String> dataSourceNames) {
configuration = config;
this.dataSourceNames = getDataSourceNames(config.getTables(), config.getAutoTables(), dataSourceNames);
config.getShardingAlgorithms().forEach((key, value) -> shardingAlgorithms.put(key, createShardingAlgorithm(key, value, config.getTables(), config.getAutoTables())));
config.getKeyGenerators().forEach((key, value) -> keyGenerators.put(key, KeyGenerateAlgorithmFactory.newInstance(value)));
+ config.getShardingAuditAlgorithms().forEach((key, value) -> shardingAuditAlgorithms.put(key, ShardingAuditAlgorithmFactory.newInstance(value)));
tableRules.putAll(createTableRules(config.getTables(), config.getDefaultKeyGenerateStrategy()));
tableRules.putAll(createAutoTableRules(config.getAutoTables(), config.getDefaultKeyGenerateStrategy()));
broadcastTables = createBroadcastTables(config.getBroadcastTables());
@@ -124,6 +132,7 @@ public final class ShardingRule implements DatabaseRule, DataNodeContainedRule,
: keyGenerators.get(config.getDefaultKeyGenerateStrategy().getKeyGeneratorName());
defaultShardingColumn = config.getDefaultShardingColumn();
shardingTableDataNodes = createShardingTableDataNodes(tableRules);
+ shardingAudits = createShardingAudits(config.getShardingAudits());
Preconditions.checkArgument(isValidBindingTableConfiguration(tableRules, new BindingTableCheckedConfiguration(this.dataSourceNames, shardingAlgorithms, config.getBindingTableGroups(),
broadcastTables, defaultDatabaseShardingStrategyConfig, defaultTableShardingStrategyConfig, defaultShardingColumn)),
"Invalid binding table configuration in ShardingRuleConfiguration.");
@@ -134,6 +143,7 @@ public final class ShardingRule implements DatabaseRule, DataNodeContainedRule,
this.dataSourceNames = getDataSourceNames(config.getTables(), config.getAutoTables(), dataSourceNames);
shardingAlgorithms.putAll(config.getShardingAlgorithms());
keyGenerators.putAll(config.getKeyGenerators());
+ shardingAuditAlgorithms.putAll(config.getShardingAuditAlgorithms());
tableRules.putAll(createTableRules(config.getTables(), config.getDefaultKeyGenerateStrategy()));
tableRules.putAll(createAutoTableRules(config.getAutoTables(), config.getDefaultKeyGenerateStrategy()));
broadcastTables = createBroadcastTables(config.getBroadcastTables());
@@ -145,6 +155,7 @@ public final class ShardingRule implements DatabaseRule, DataNodeContainedRule,
: keyGenerators.get(config.getDefaultKeyGenerateStrategy().getKeyGeneratorName());
defaultShardingColumn = config.getDefaultShardingColumn();
shardingTableDataNodes = createShardingTableDataNodes(tableRules);
+ shardingAudits = createShardingAudits(config.getShardingAudits());
Preconditions.checkArgument(isValidBindingTableConfiguration(tableRules, new BindingTableCheckedConfiguration(this.dataSourceNames, shardingAlgorithms, config.getBindingTableGroups(),
broadcastTables, defaultDatabaseShardingStrategyConfig, defaultTableShardingStrategyConfig, defaultShardingColumn)),
"Invalid binding table configuration in ShardingRuleConfiguration.");
@@ -158,6 +169,15 @@ public final class ShardingRule implements DatabaseRule, DataNodeContainedRule,
return result;
}
+ private Map<String, ShardingAuditStrategyConfiguration> createShardingAudits(final Map<String, ShardingAuditStrategyConfiguration> shardingAudits) {
+ if (null == shardingAudits) {
+ return new LinkedHashMap<>(0, 1);
+ }
+ Preconditions.checkArgument(shardingAudits.values().stream().allMatch(each -> shardingAuditAlgorithms.containsKey(each.getShardingAuditAlgorithmName())),
+ "Cannot find sharding audit algorithm");
+ return shardingAudits;
+ }
+
private Collection<String> getDataSourceNames(final Collection<ShardingTableRuleConfiguration> tableRuleConfigs,
final Collection<ShardingAutoTableRuleConfiguration> autoTableRuleConfigs, final Collection<String> dataSourceNames) {
if (tableRuleConfigs.isEmpty() && autoTableRuleConfigs.isEmpty()) {
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/yaml/config/YamlShardingRuleConfiguration.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/yaml/config/YamlShardingRuleConfiguration.java
index 1351c8e932d..367e79d28d4 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/yaml/config/YamlShardingRuleConfiguration.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/yaml/config/YamlShardingRuleConfiguration.java
@@ -25,6 +25,7 @@ import org.apache.shardingsphere.infra.yaml.config.pojo.rulealtered.YamlOnRuleAl
import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration;
import org.apache.shardingsphere.sharding.yaml.config.rule.YamlShardingAutoTableRuleConfiguration;
import org.apache.shardingsphere.sharding.yaml.config.rule.YamlTableRuleConfiguration;
+import org.apache.shardingsphere.sharding.yaml.config.strategy.audit.YamlShardingAuditStrategyConfiguration;
import org.apache.shardingsphere.sharding.yaml.config.strategy.keygen.YamlKeyGenerateStrategyConfiguration;
import org.apache.shardingsphere.sharding.yaml.config.strategy.sharding.YamlShardingStrategyConfiguration;
@@ -58,12 +59,16 @@ public final class YamlShardingRuleConfiguration implements YamlRuleConfiguratio
private Map<String, YamlShardingSphereAlgorithmConfiguration> keyGenerators = new LinkedHashMap<>();
+ private Map<String, YamlShardingSphereAlgorithmConfiguration> shardingAuditAlgorithms = new LinkedHashMap<>();
+
private String defaultShardingColumn;
private String scalingName;
private Map<String, YamlOnRuleAlteredActionConfiguration> scaling = new LinkedHashMap<>();
+ private Map<String, YamlShardingAuditStrategyConfiguration> shardingAudits = new LinkedHashMap<>();
+
@Override
public Class<ShardingRuleConfiguration> getRuleConfigurationType() {
return ShardingRuleConfiguration.class;
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/constant/ShardingOrder.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/yaml/config/strategy/audit/YamlShardingAuditStrategyConfiguration.java
similarity index 65%
copy from shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/constant/ShardingOrder.java
copy to shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/yaml/config/strategy/audit/YamlShardingAuditStrategyConfiguration.java
index 22acab09d77..d259b8bf42d 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/constant/ShardingOrder.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/yaml/config/strategy/audit/YamlShardingAuditStrategyConfiguration.java
@@ -15,24 +15,20 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.sharding.constant;
+package org.apache.shardingsphere.sharding.yaml.config.strategy.audit;
-import lombok.AccessLevel;
-import lombok.NoArgsConstructor;
+import lombok.Getter;
+import lombok.Setter;
+import org.apache.shardingsphere.infra.yaml.config.pojo.YamlConfiguration;
/**
- * Sharding order.
+ * Sharing audit strategy configuration for YAML.
*/
-@NoArgsConstructor(access = AccessLevel.PRIVATE)
-public final class ShardingOrder {
+@Getter
+@Setter
+public final class YamlShardingAuditStrategyConfiguration implements YamlConfiguration {
- /**
- * Sharding order.
- */
- public static final int ORDER = -10;
+ private String shardingAuditAlgorithmName;
- /**
- * Algorithm provider sharding order.
- */
- public static final int ALGORITHM_PROVIDER_ORDER = ORDER + 1;
+ private boolean allowHintDisable;
}
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/yaml/swapper/ShardingRuleAlgorithmProviderConfigurationYamlSwapper.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/yaml/swapper/ShardingRuleAlgorithmProviderConfigurationYamlSwapper.java
index 8daae9a4817..b49c2e10adc 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/yaml/swapper/ShardingRuleAlgorithmProviderConfigurationYamlSwapper.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/yaml/swapper/ShardingRuleAlgorithmProviderConfigurationYamlSwapper.java
@@ -27,6 +27,7 @@ import org.apache.shardingsphere.sharding.yaml.config.rule.YamlTableRuleConfigur
import org.apache.shardingsphere.sharding.yaml.swapper.rule.ShardingAutoTableRuleConfigurationYamlSwapper;
import org.apache.shardingsphere.sharding.yaml.swapper.rule.ShardingTableRuleConfigurationYamlSwapper;
import org.apache.shardingsphere.sharding.yaml.swapper.strategy.KeyGenerateStrategyConfigurationYamlSwapper;
+import org.apache.shardingsphere.sharding.yaml.swapper.strategy.ShardingAuditStrategyConfigurationYamlSwapper;
import org.apache.shardingsphere.sharding.yaml.swapper.strategy.ShardingStrategyConfigurationYamlSwapper;
import java.util.Collections;
@@ -43,6 +44,8 @@ public final class ShardingRuleAlgorithmProviderConfigurationYamlSwapper impleme
private final KeyGenerateStrategyConfigurationYamlSwapper keyGenerateStrategyYamlSwapper = new KeyGenerateStrategyConfigurationYamlSwapper();
+ private final ShardingAuditStrategyConfigurationYamlSwapper shardingAuditStrategyYamlSwapper = new ShardingAuditStrategyConfigurationYamlSwapper();
+
@Override
public YamlShardingRuleConfiguration swapToYamlConfiguration(final AlgorithmProvidedShardingRuleConfiguration data) {
YamlShardingRuleConfiguration result = new YamlShardingRuleConfiguration();
@@ -53,6 +56,7 @@ public final class ShardingRuleAlgorithmProviderConfigurationYamlSwapper impleme
result.getBroadcastTables().addAll(data.getBroadcastTables());
setYamlDefaultStrategies(data, result);
setYamlAlgorithms(data, result);
+ data.getShardingAudits().forEach((key, value) -> result.getShardingAudits().put(key, shardingAuditStrategyYamlSwapper.swapToYamlConfiguration(value)));
return result;
}
@@ -73,6 +77,7 @@ public final class ShardingRuleAlgorithmProviderConfigurationYamlSwapper impleme
result.getBroadcastTables().addAll(yamlConfig.getBroadcastTables());
setDefaultStrategies(yamlConfig, result);
result.setDefaultShardingColumn(yamlConfig.getDefaultShardingColumn());
+ yamlConfig.getShardingAudits().forEach((key, value) -> result.getShardingAudits().put(key, shardingAuditStrategyYamlSwapper.swapToObject(value)));
return result;
}
@@ -122,5 +127,9 @@ public final class ShardingRuleAlgorithmProviderConfigurationYamlSwapper impleme
if (null != data.getKeyGenerators()) {
data.getKeyGenerators().forEach((key, value) -> yamlConfig.getKeyGenerators().put(key, new YamlShardingSphereAlgorithmConfiguration(value.getType(), value.getProps())));
}
+ if (null != data.getShardingAuditAlgorithms()) {
+ data.getShardingAuditAlgorithms().forEach(
+ (key, value) -> yamlConfig.getShardingAuditAlgorithms().put(key, new YamlShardingSphereAlgorithmConfiguration(value.getType(), value.getProps())));
+ }
}
}
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/yaml/swapper/ShardingRuleConfigurationYamlSwapper.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/yaml/swapper/ShardingRuleConfigurationYamlSwapper.java
index 4194f3a9839..8b98bcae98e 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/yaml/swapper/ShardingRuleConfigurationYamlSwapper.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/yaml/swapper/ShardingRuleConfigurationYamlSwapper.java
@@ -28,6 +28,7 @@ import org.apache.shardingsphere.sharding.yaml.config.rule.YamlTableRuleConfigur
import org.apache.shardingsphere.sharding.yaml.swapper.rule.ShardingAutoTableRuleConfigurationYamlSwapper;
import org.apache.shardingsphere.sharding.yaml.swapper.rule.ShardingTableRuleConfigurationYamlSwapper;
import org.apache.shardingsphere.sharding.yaml.swapper.strategy.KeyGenerateStrategyConfigurationYamlSwapper;
+import org.apache.shardingsphere.sharding.yaml.swapper.strategy.ShardingAuditStrategyConfigurationYamlSwapper;
import org.apache.shardingsphere.sharding.yaml.swapper.strategy.ShardingStrategyConfigurationYamlSwapper;
import java.util.Collections;
@@ -48,6 +49,8 @@ public final class ShardingRuleConfigurationYamlSwapper implements YamlRuleConfi
private final OnRuleAlteredActionConfigurationYamlSwapper onRuleAlteredActionYamlSwapper = new OnRuleAlteredActionConfigurationYamlSwapper();
+ private final ShardingAuditStrategyConfigurationYamlSwapper shardingAuditStrategyYamlSwapper = new ShardingAuditStrategyConfigurationYamlSwapper();
+
@Override
public YamlShardingRuleConfiguration swapToYamlConfiguration(final ShardingRuleConfiguration data) {
YamlShardingRuleConfiguration result = new YamlShardingRuleConfiguration();
@@ -60,6 +63,7 @@ public final class ShardingRuleConfigurationYamlSwapper implements YamlRuleConfi
setYamlAlgorithms(data, result);
result.setDefaultShardingColumn(data.getDefaultShardingColumn());
result.setScalingName(data.getScalingName());
+ data.getShardingAudits().forEach((key, value) -> result.getShardingAudits().put(key, shardingAuditStrategyYamlSwapper.swapToYamlConfiguration(value)));
return result;
}
@@ -106,6 +110,7 @@ public final class ShardingRuleConfigurationYamlSwapper implements YamlRuleConfi
setAlgorithms(yamlConfig, result);
result.setDefaultShardingColumn(yamlConfig.getDefaultShardingColumn());
result.setScalingName(yamlConfig.getScalingName());
+ yamlConfig.getShardingAudits().forEach((key, value) -> result.getShardingAudits().put(key, shardingAuditStrategyYamlSwapper.swapToObject(value)));
return result;
}
@@ -128,6 +133,9 @@ public final class ShardingRuleConfigurationYamlSwapper implements YamlRuleConfi
if (null != yamlConfig.getKeyGenerators()) {
yamlConfig.getKeyGenerators().forEach((key, value) -> ruleConfig.getKeyGenerators().put(key, algorithmSwapper.swapToObject(value)));
}
+ if (null != yamlConfig.getShardingAuditAlgorithms()) {
+ yamlConfig.getShardingAuditAlgorithms().forEach((key, value) -> ruleConfig.getShardingAuditAlgorithms().put(key, algorithmSwapper.swapToObject(value)));
+ }
if (null != yamlConfig.getScaling()) {
yamlConfig.getScaling().forEach((key, value) -> ruleConfig.getScaling().put(key, onRuleAlteredActionYamlSwapper.swapToObject(value)));
}
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/yaml/swapper/strategy/ShardingAuditStrategyConfigurationYamlSwapper.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/yaml/swapper/strategy/ShardingAuditStrategyConfigurationYamlSwapper.java
new file mode 100644
index 00000000000..7ed1d7ac782
--- /dev/null
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/yaml/swapper/strategy/ShardingAuditStrategyConfigurationYamlSwapper.java
@@ -0,0 +1,41 @@
+/*
+ * 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.sharding.yaml.swapper.strategy;
+
+import org.apache.shardingsphere.infra.yaml.config.swapper.YamlConfigurationSwapper;
+import org.apache.shardingsphere.sharding.api.config.strategy.audit.ShardingAuditStrategyConfiguration;
+import org.apache.shardingsphere.sharding.yaml.config.strategy.audit.YamlShardingAuditStrategyConfiguration;
+
+/**
+ * Sharding audit strategy configuration YAML swapper.
+ */
+public final class ShardingAuditStrategyConfigurationYamlSwapper implements YamlConfigurationSwapper<YamlShardingAuditStrategyConfiguration, ShardingAuditStrategyConfiguration> {
+
+ @Override
+ public YamlShardingAuditStrategyConfiguration swapToYamlConfiguration(final ShardingAuditStrategyConfiguration data) {
+ YamlShardingAuditStrategyConfiguration result = new YamlShardingAuditStrategyConfiguration();
+ result.setShardingAuditAlgorithmName(data.getShardingAuditAlgorithmName());
+ result.setAllowHintDisable(data.isAllowHintDisable());
+ return result;
+ }
+
+ @Override
+ public ShardingAuditStrategyConfiguration swapToObject(final YamlShardingAuditStrategyConfiguration yamlConfig) {
+ return new ShardingAuditStrategyConfiguration(yamlConfig.getShardingAuditAlgorithmName(), yamlConfig.isAllowHintDisable());
+ }
+}
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.executor.check.SQLChecker b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.executor.check.SQLChecker
new file mode 100644
index 00000000000..b85888720a2
--- /dev/null
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.executor.check.SQLChecker
@@ -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.checker.audit.ShardingAuditChecker