You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by du...@apache.org on 2023/01/11 01:17:55 UTC
[shardingsphere] branch master updated: Add Readwrite-splitting feature error code (#23478)
This is an automated email from the ASF dual-hosted git repository.
duanzhengqiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new 95bc6efaa3e Add Readwrite-splitting feature error code (#23478)
95bc6efaa3e is described below
commit 95bc6efaa3ea7a5a659ae190e83effaf70f859cf
Author: zhaojinchao <zh...@apache.org>
AuthorDate: Wed Jan 11 09:17:41 2023 +0800
Add Readwrite-splitting feature error code (#23478)
* Add Readwrite-splitting feature error code
* Update error code
* Fix checkstyle
* Add more readwrite-splitting exception error code
* Fix checkstyle
---
...ReplicaWeightReadQueryLoadBalanceAlgorithm.java | 5 ++-
...sactionWeightReadQueryLoadBalanceAlgorithm.java | 5 ++-
.../WeightReadQueryLoadBalanceAlgorithm.java | 5 ++-
...ReadwriteSplittingRuleConfigurationChecker.java | 43 +++++++++++++++-------
...MissingRequiredReadDatabaseWeightException.java | 33 +++++++++++++++++
.../checker/DataSourceNameExistedException.java | 33 +++++++++++++++++
.../checker/DuplicateDataSourceException.java | 33 +++++++++++++++++
...idWeightLoadBalancerConfigurationException.java | 33 +++++++++++++++++
.../LoadBalancerAlgorithmNotFoundException.java | 33 +++++++++++++++++
...ngRequiredAutoAwareDataSourceNameException.java | 33 +++++++++++++++++
.../MissingRequiredDataSourceNameException.java | 33 +++++++++++++++++
...issingRequiredReadDataSourceNamesException.java | 33 +++++++++++++++++
...issingRequiredWriteDataSourceNameException.java | 33 +++++++++++++++++
...lidInlineExpressionDataSourceNameException.java | 33 +++++++++++++++++
.../rule/ReadwriteSplittingRule.java | 11 ++++--
...writeSplittingRuleConfigurationCheckerTest.java | 8 ++--
16 files changed, 384 insertions(+), 23 deletions(-)
diff --git a/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/FixedReplicaWeightReadQueryLoadBalanceAlgorithm.java b/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/FixedReplicaWeightReadQueryLoadBalanceAlgorithm.java
index 6d5ace05a70..453952d61f9 100644
--- a/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/FixedReplicaWeightReadQueryLoadBalanceAlgorithm.java
+++ b/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/FixedReplicaWeightReadQueryLoadBalanceAlgorithm.java
@@ -20,7 +20,9 @@ package org.apache.shardingsphere.readwritesplitting.algorithm.loadbalance;
import com.google.common.base.Preconditions;
import lombok.Getter;
import org.apache.shardingsphere.infra.context.transaction.TransactionConnectionContext;
+import org.apache.shardingsphere.infra.util.exception.ShardingSpherePreconditions;
import org.apache.shardingsphere.readwritesplitting.exception.algorithm.InvalidReadDatabaseWeightException;
+import org.apache.shardingsphere.readwritesplitting.exception.algorithm.MissingRequiredReadDatabaseWeightException;
import org.apache.shardingsphere.readwritesplitting.spi.ReadQueryLoadBalanceAlgorithm;
import java.util.Arrays;
@@ -106,7 +108,8 @@ public final class FixedReplicaWeightReadQueryLoadBalanceAlgorithm implements Re
private double getWeightValue(final String readDataSourceName) {
Object weightObject = props.get(readDataSourceName);
- Preconditions.checkNotNull(weightObject, "Read database `%s` access weight is not configured", readDataSourceName);
+ ShardingSpherePreconditions.checkNotNull(weightObject, () -> new MissingRequiredReadDatabaseWeightException(getType(),
+ String.format("Read database `%s` access weight is not configured", readDataSourceName)));
double result;
try {
result = Double.parseDouble(weightObject.toString());
diff --git a/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/TransactionWeightReadQueryLoadBalanceAlgorithm.java b/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/TransactionWeightReadQueryLoadBalanceAlgorithm.java
index 8c6821303f5..071d315174a 100644
--- a/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/TransactionWeightReadQueryLoadBalanceAlgorithm.java
+++ b/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/TransactionWeightReadQueryLoadBalanceAlgorithm.java
@@ -20,7 +20,9 @@ package org.apache.shardingsphere.readwritesplitting.algorithm.loadbalance;
import com.google.common.base.Preconditions;
import lombok.Getter;
import org.apache.shardingsphere.infra.context.transaction.TransactionConnectionContext;
+import org.apache.shardingsphere.infra.util.exception.ShardingSpherePreconditions;
import org.apache.shardingsphere.readwritesplitting.exception.algorithm.InvalidReadDatabaseWeightException;
+import org.apache.shardingsphere.readwritesplitting.exception.algorithm.MissingRequiredReadDatabaseWeightException;
import org.apache.shardingsphere.readwritesplitting.spi.ReadQueryLoadBalanceAlgorithm;
import java.util.Arrays;
@@ -100,7 +102,8 @@ public final class TransactionWeightReadQueryLoadBalanceAlgorithm implements Rea
private double getWeightValue(final String readDataSourceName) {
Object weightObject = props.get(readDataSourceName);
- Preconditions.checkNotNull(weightObject, "Read database `%s` access weight is not configured", readDataSourceName);
+ ShardingSpherePreconditions.checkNotNull(weightObject, () -> new MissingRequiredReadDatabaseWeightException(getType(),
+ String.format("Read database `%s` access weight is not configured", readDataSourceName)));
double result;
try {
result = Double.parseDouble(weightObject.toString());
diff --git a/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/WeightReadQueryLoadBalanceAlgorithm.java b/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/WeightReadQueryLoadBalanceAlgorithm.java
index ad20cdd9081..e3add991ee2 100644
--- a/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/WeightReadQueryLoadBalanceAlgorithm.java
+++ b/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/WeightReadQueryLoadBalanceAlgorithm.java
@@ -20,7 +20,9 @@ package org.apache.shardingsphere.readwritesplitting.algorithm.loadbalance;
import com.google.common.base.Preconditions;
import lombok.Getter;
import org.apache.shardingsphere.infra.context.transaction.TransactionConnectionContext;
+import org.apache.shardingsphere.infra.util.exception.ShardingSpherePreconditions;
import org.apache.shardingsphere.readwritesplitting.exception.algorithm.InvalidReadDatabaseWeightException;
+import org.apache.shardingsphere.readwritesplitting.exception.algorithm.MissingRequiredReadDatabaseWeightException;
import org.apache.shardingsphere.readwritesplitting.spi.ReadQueryLoadBalanceAlgorithm;
import java.util.Arrays;
@@ -103,7 +105,8 @@ public final class WeightReadQueryLoadBalanceAlgorithm implements ReadQueryLoadB
private double getWeightValue(final String readDataSourceName) {
Object weightObject = props.get(readDataSourceName);
- Preconditions.checkNotNull(weightObject, "Read database `%s` access weight is not configured", readDataSourceName);
+ ShardingSpherePreconditions.checkNotNull(weightObject, () -> new MissingRequiredReadDatabaseWeightException(getType(),
+ String.format("Read database `%s` access weight is not configured", readDataSourceName)));
double result;
try {
result = Double.parseDouble(weightObject.toString());
diff --git a/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/checker/ReadwriteSplittingRuleConfigurationChecker.java b/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/checker/ReadwriteSplittingRuleConfigurationChecker.java
index 5cb5a06633e..77df65ef2a2 100644
--- a/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/checker/ReadwriteSplittingRuleConfigurationChecker.java
+++ b/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/checker/ReadwriteSplittingRuleConfigurationChecker.java
@@ -24,6 +24,7 @@ import org.apache.shardingsphere.infra.config.rule.checker.RuleConfigurationChec
import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
import org.apache.shardingsphere.infra.rule.identifier.type.DataSourceContainedRule;
import org.apache.shardingsphere.infra.rule.identifier.type.DynamicDataSourceContainedRule;
+import org.apache.shardingsphere.infra.util.exception.ShardingSpherePreconditions;
import org.apache.shardingsphere.infra.util.expr.InlineExpressionParser;
import org.apache.shardingsphere.readwritesplitting.algorithm.loadbalance.TransactionWeightReadQueryLoadBalanceAlgorithm;
import org.apache.shardingsphere.readwritesplitting.algorithm.loadbalance.WeightReadQueryLoadBalanceAlgorithm;
@@ -32,6 +33,15 @@ import org.apache.shardingsphere.readwritesplitting.api.rule.ReadwriteSplittingD
import org.apache.shardingsphere.readwritesplitting.api.strategy.DynamicReadwriteSplittingStrategyConfiguration;
import org.apache.shardingsphere.readwritesplitting.api.strategy.StaticReadwriteSplittingStrategyConfiguration;
import org.apache.shardingsphere.readwritesplitting.constant.ReadwriteSplittingOrder;
+import org.apache.shardingsphere.readwritesplitting.exception.algorithm.MissingRequiredReadDatabaseWeightException;
+import org.apache.shardingsphere.readwritesplitting.exception.checker.DataSourceNameExistedException;
+import org.apache.shardingsphere.readwritesplitting.exception.checker.MissingRequiredDataSourceNameException;
+import org.apache.shardingsphere.readwritesplitting.exception.checker.MissingRequiredAutoAwareDataSourceNameException;
+import org.apache.shardingsphere.readwritesplitting.exception.checker.MissingRequiredReadDataSourceNamesException;
+import org.apache.shardingsphere.readwritesplitting.exception.checker.MissingRequiredWriteDataSourceNameException;
+import org.apache.shardingsphere.readwritesplitting.exception.checker.DuplicateDataSourceException;
+import org.apache.shardingsphere.readwritesplitting.exception.checker.LoadBalancerAlgorithmNotFoundException;
+import org.apache.shardingsphere.readwritesplitting.exception.checker.InvalidWeightLoadBalancerConfigurationException;
import org.apache.shardingsphere.readwritesplitting.spi.ReadQueryLoadBalanceAlgorithm;
import javax.sql.DataSource;
@@ -62,17 +72,17 @@ public final class ReadwriteSplittingRuleConfigurationChecker implements RuleCon
Collection<String> addedWriteDataSourceNames = new HashSet<>();
Collection<String> addedReadDataSourceNames = new HashSet<>();
for (ReadwriteSplittingDataSourceRuleConfiguration each : configs) {
- Preconditions.checkArgument(!Strings.isNullOrEmpty(each.getName()), "Readwrite-splitting data source name is required.");
+ ShardingSpherePreconditions.checkState(!Strings.isNullOrEmpty(each.getName()), () -> new MissingRequiredDataSourceNameException(databaseName));
Preconditions.checkState(null != each.getStaticStrategy() || null != each.getDynamicStrategy(), "No available readwrite-splitting rule configuration in database `%s`.", databaseName);
Optional.ofNullable(each.getStaticStrategy()).ifPresent(optional -> checkStaticStrategy(databaseName, dataSourceMap, addedWriteDataSourceNames, addedReadDataSourceNames, optional, rules));
- Optional.ofNullable(each.getDynamicStrategy()).ifPresent(optional -> checkDynamicStrategy(rules, optional));
+ Optional.ofNullable(each.getDynamicStrategy()).ifPresent(optional -> checkDynamicStrategy(databaseName, rules, optional));
}
}
private void checkStaticStrategy(final String databaseName, final Map<String, DataSource> dataSourceMap, final Collection<String> addedWriteDataSourceNames,
final Collection<String> readDataSourceNames, final StaticReadwriteSplittingStrategyConfiguration strategyConfig, final Collection<ShardingSphereRule> rules) {
- Preconditions.checkArgument(!Strings.isNullOrEmpty(strategyConfig.getWriteDataSourceName()), "Write data source name is required.");
- Preconditions.checkArgument(!strategyConfig.getReadDataSourceNames().isEmpty(), "Read data source names are required.");
+ ShardingSpherePreconditions.checkState(!Strings.isNullOrEmpty(strategyConfig.getWriteDataSourceName()), () -> new MissingRequiredWriteDataSourceNameException(databaseName));
+ ShardingSpherePreconditions.checkState(!strategyConfig.getReadDataSourceNames().isEmpty(), () -> new MissingRequiredReadDataSourceNamesException(databaseName));
checkWriteDataSourceNames(databaseName, dataSourceMap, addedWriteDataSourceNames, strategyConfig, rules);
for (String each : readDataSourceNames) {
checkReadeDataSourceNames(databaseName, dataSourceMap, readDataSourceNames, each);
@@ -82,8 +92,10 @@ public final class ReadwriteSplittingRuleConfigurationChecker implements RuleCon
private void checkWriteDataSourceNames(final String databaseName, final Map<String, DataSource> dataSourceMap, final Collection<String> addedWriteDataSourceNames,
final StaticReadwriteSplittingStrategyConfiguration strategyConfig, final Collection<ShardingSphereRule> rules) {
for (String each : new InlineExpressionParser(strategyConfig.getWriteDataSourceName()).splitAndEvaluate()) {
- Preconditions.checkState(dataSourceMap.containsKey(each) || containsInOtherRules(each, rules), "Write data source name `%s` not in database `%s`.", each, databaseName);
- Preconditions.checkState(addedWriteDataSourceNames.add(each), "Can not config duplicate write data source `%s` in database `%s`.", each, databaseName);
+ ShardingSpherePreconditions.checkState(dataSourceMap.containsKey(each) || containsInOtherRules(each, rules), () ->
+ new DataSourceNameExistedException(String.format("Write data source name `%s` not in database `%s`.", each, databaseName)));
+ ShardingSpherePreconditions.checkState(addedWriteDataSourceNames.add(each), () ->
+ new DuplicateDataSourceException(String.format("Can not config duplicate write data source `%s` in database `%s`.", each, databaseName)));
}
}
@@ -99,13 +111,15 @@ public final class ReadwriteSplittingRuleConfigurationChecker implements RuleCon
private void checkReadeDataSourceNames(final String databaseName,
final Map<String, DataSource> dataSourceMap, final Collection<String> addedReadDataSourceNames, final String readDataSourceName) {
for (String each : new InlineExpressionParser(readDataSourceName).splitAndEvaluate()) {
- Preconditions.checkState(dataSourceMap.containsKey(each), "Read data source name `%s` not in database `%s`.", each, databaseName);
- Preconditions.checkState(addedReadDataSourceNames.add(each), "Can not config duplicate read data source `%s` in database `%s`.", each, databaseName);
+ ShardingSpherePreconditions.checkState(dataSourceMap.containsKey(each), () ->
+ new DataSourceNameExistedException(String.format("Read data source name `%s` not in database `%s`.", each, databaseName)));
+ ShardingSpherePreconditions.checkState(addedReadDataSourceNames.add(each), () ->
+ new DuplicateDataSourceException(String.format("Can not config duplicate read data source `%s` in database `%s`.", each, databaseName)));
}
}
- private void checkDynamicStrategy(final Collection<ShardingSphereRule> rules, final DynamicReadwriteSplittingStrategyConfiguration dynamicStrategy) {
- Preconditions.checkArgument(!Strings.isNullOrEmpty(dynamicStrategy.getAutoAwareDataSourceName()), "Auto aware data source name is required");
+ private void checkDynamicStrategy(final String databaseName, final Collection<ShardingSphereRule> rules, final DynamicReadwriteSplittingStrategyConfiguration dynamicStrategy) {
+ ShardingSpherePreconditions.checkState(!Strings.isNullOrEmpty(dynamicStrategy.getAutoAwareDataSourceName()), () -> new MissingRequiredAutoAwareDataSourceNameException(databaseName));
Optional<ShardingSphereRule> dynamicDataSourceStrategy = rules.stream().filter(each -> each instanceof DynamicDataSourceContainedRule).findFirst();
Preconditions.checkArgument(dynamicDataSourceStrategy.isPresent(), "Dynamic data source strategy is required");
}
@@ -117,12 +131,13 @@ public final class ReadwriteSplittingRuleConfigurationChecker implements RuleCon
continue;
}
ReadQueryLoadBalanceAlgorithm loadBalancer = loadBalancers.get(each.getLoadBalancerName());
- Preconditions.checkNotNull(loadBalancer, "Not found load balance type in database `%s`", databaseName);
+ ShardingSpherePreconditions.checkNotNull(loadBalancer, () -> new LoadBalancerAlgorithmNotFoundException(databaseName));
if (loadBalancer instanceof WeightReadQueryLoadBalanceAlgorithm || loadBalancer instanceof TransactionWeightReadQueryLoadBalanceAlgorithm) {
- Preconditions.checkState(!loadBalancer.getProps().isEmpty(), "Readwrite-splitting data source weight config are required in database `%s`", databaseName);
+ ShardingSpherePreconditions.checkState(!loadBalancer.getProps().isEmpty(), () -> new MissingRequiredReadDatabaseWeightException(loadBalancer.getType(),
+ String.format("Read data source weight config are required in database `%s`", databaseName)));
Collection<String> dataSourceNames = getDataSourceNames(each, rules);
- loadBalancer.getProps().stringPropertyNames().forEach(dataSourceName -> Preconditions.checkState(dataSourceNames.contains(dataSourceName),
- "Load Balancer datasource name config does not match datasource in database `%s`", databaseName));
+ loadBalancer.getProps().stringPropertyNames().forEach(dataSourceName -> ShardingSpherePreconditions.checkState(dataSourceNames.contains(dataSourceName),
+ () -> new InvalidWeightLoadBalancerConfigurationException(databaseName)));
}
}
}
diff --git a/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/exception/algorithm/MissingRequiredReadDatabaseWeightException.java b/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/exception/algorithm/MissingRequiredReadDatabaseWeightException.java
new file mode 100644
index 00000000000..5e09a6153dd
--- /dev/null
+++ b/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/exception/algorithm/MissingRequiredReadDatabaseWeightException.java
@@ -0,0 +1,33 @@
+/*
+ * 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.exception.algorithm;
+
+import org.apache.shardingsphere.infra.util.exception.external.sql.sqlstate.XOpenSQLState;
+import org.apache.shardingsphere.readwritesplitting.exception.ReadwriteSplittingSQLException;
+
+/**
+ * Missing required read database weight exception.
+ */
+public final class MissingRequiredReadDatabaseWeightException extends ReadwriteSplittingSQLException {
+
+ private static final long serialVersionUID = 8006957930250488016L;
+
+ public MissingRequiredReadDatabaseWeightException(final String loadBalancerType, final String reason) {
+ super(XOpenSQLState.CHECK_OPTION_VIOLATION, 81, "Load balancer algorithm `%s` initialization failed, reason is: %s.", loadBalancerType, reason);
+ }
+}
diff --git a/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/exception/checker/DataSourceNameExistedException.java b/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/exception/checker/DataSourceNameExistedException.java
new file mode 100644
index 00000000000..407f32a9fcf
--- /dev/null
+++ b/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/exception/checker/DataSourceNameExistedException.java
@@ -0,0 +1,33 @@
+/*
+ * 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.exception.checker;
+
+import org.apache.shardingsphere.infra.util.exception.external.sql.sqlstate.XOpenSQLState;
+import org.apache.shardingsphere.readwritesplitting.exception.ReadwriteSplittingSQLException;
+
+/**
+ * Data source name existed exception.
+ */
+public final class DataSourceNameExistedException extends ReadwriteSplittingSQLException {
+
+ private static final long serialVersionUID = 1284608200400804784L;
+
+ public DataSourceNameExistedException(final String reason) {
+ super(XOpenSQLState.NOT_FOUND, 94, reason);
+ }
+}
diff --git a/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/exception/checker/DuplicateDataSourceException.java b/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/exception/checker/DuplicateDataSourceException.java
new file mode 100644
index 00000000000..de3a32049f3
--- /dev/null
+++ b/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/exception/checker/DuplicateDataSourceException.java
@@ -0,0 +1,33 @@
+/*
+ * 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.exception.checker;
+
+import org.apache.shardingsphere.infra.util.exception.external.sql.sqlstate.XOpenSQLState;
+import org.apache.shardingsphere.readwritesplitting.exception.ReadwriteSplittingSQLException;
+
+/**
+ * Duplicate write data source exception.
+ */
+public final class DuplicateDataSourceException extends ReadwriteSplittingSQLException {
+
+ private static final long serialVersionUID = -58671655670347084L;
+
+ public DuplicateDataSourceException(final String reason) {
+ super(XOpenSQLState.DUPLICATE, 93, reason);
+ }
+}
diff --git a/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/exception/checker/InvalidWeightLoadBalancerConfigurationException.java b/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/exception/checker/InvalidWeightLoadBalancerConfigurationException.java
new file mode 100644
index 00000000000..bfe7d8a6685
--- /dev/null
+++ b/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/exception/checker/InvalidWeightLoadBalancerConfigurationException.java
@@ -0,0 +1,33 @@
+/*
+ * 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.exception.checker;
+
+import org.apache.shardingsphere.infra.util.exception.external.sql.sqlstate.XOpenSQLState;
+import org.apache.shardingsphere.readwritesplitting.exception.ReadwriteSplittingSQLException;
+
+/**
+ * Invalid weight load balancer configuration exception.
+ */
+public final class InvalidWeightLoadBalancerConfigurationException extends ReadwriteSplittingSQLException {
+
+ private static final long serialVersionUID = 7206356551434539781L;
+
+ public InvalidWeightLoadBalancerConfigurationException(final String databaseName) {
+ super(XOpenSQLState.CHECK_OPTION_VIOLATION, 97, "Weight load balancer datasource name config does not match data sources in database `%s`.", databaseName);
+ }
+}
diff --git a/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/exception/checker/LoadBalancerAlgorithmNotFoundException.java b/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/exception/checker/LoadBalancerAlgorithmNotFoundException.java
new file mode 100644
index 00000000000..da6b2c48e89
--- /dev/null
+++ b/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/exception/checker/LoadBalancerAlgorithmNotFoundException.java
@@ -0,0 +1,33 @@
+/*
+ * 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.exception.checker;
+
+import org.apache.shardingsphere.infra.util.exception.external.sql.sqlstate.XOpenSQLState;
+import org.apache.shardingsphere.readwritesplitting.exception.ReadwriteSplittingSQLException;
+
+/**
+ * Load balancer algorithm not found exception.
+ */
+public final class LoadBalancerAlgorithmNotFoundException extends ReadwriteSplittingSQLException {
+
+ private static final long serialVersionUID = -5517443698152561344L;
+
+ public LoadBalancerAlgorithmNotFoundException(final String databaseName) {
+ super(XOpenSQLState.NOT_FOUND, 96, "Not found load balance type in database `%s`.", databaseName);
+ }
+}
diff --git a/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/exception/checker/MissingRequiredAutoAwareDataSourceNameException.java b/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/exception/checker/MissingRequiredAutoAwareDataSourceNameException.java
new file mode 100644
index 00000000000..7298a5c4763
--- /dev/null
+++ b/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/exception/checker/MissingRequiredAutoAwareDataSourceNameException.java
@@ -0,0 +1,33 @@
+/*
+ * 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.exception.checker;
+
+import org.apache.shardingsphere.infra.util.exception.external.sql.sqlstate.XOpenSQLState;
+import org.apache.shardingsphere.readwritesplitting.exception.ReadwriteSplittingSQLException;
+
+/**
+ * Missing required auto aware data source name exception.
+ */
+public final class MissingRequiredAutoAwareDataSourceNameException extends ReadwriteSplittingSQLException {
+
+ private static final long serialVersionUID = -7921103941300062641L;
+
+ public MissingRequiredAutoAwareDataSourceNameException(final String databaseName) {
+ super(XOpenSQLState.CHECK_OPTION_VIOLATION, 95, "Auto aware data source name is required in database `%s`.", databaseName);
+ }
+}
diff --git a/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/exception/checker/MissingRequiredDataSourceNameException.java b/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/exception/checker/MissingRequiredDataSourceNameException.java
new file mode 100644
index 00000000000..9567b9667c1
--- /dev/null
+++ b/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/exception/checker/MissingRequiredDataSourceNameException.java
@@ -0,0 +1,33 @@
+/*
+ * 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.exception.checker;
+
+import org.apache.shardingsphere.infra.util.exception.external.sql.sqlstate.XOpenSQLState;
+import org.apache.shardingsphere.readwritesplitting.exception.ReadwriteSplittingSQLException;
+
+/**
+ * Missing required data source name exception.
+ */
+public final class MissingRequiredDataSourceNameException extends ReadwriteSplittingSQLException {
+
+ private static final long serialVersionUID = 8006957930250488016L;
+
+ public MissingRequiredDataSourceNameException(final String databaseName) {
+ super(XOpenSQLState.CHECK_OPTION_VIOLATION, 90, "Data source name is required in database `%s`.", databaseName);
+ }
+}
diff --git a/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/exception/checker/MissingRequiredReadDataSourceNamesException.java b/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/exception/checker/MissingRequiredReadDataSourceNamesException.java
new file mode 100644
index 00000000000..b39b3609d43
--- /dev/null
+++ b/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/exception/checker/MissingRequiredReadDataSourceNamesException.java
@@ -0,0 +1,33 @@
+/*
+ * 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.exception.checker;
+
+import org.apache.shardingsphere.infra.util.exception.external.sql.sqlstate.XOpenSQLState;
+import org.apache.shardingsphere.readwritesplitting.exception.ReadwriteSplittingSQLException;
+
+/**
+ * Missing required read data source names exception.
+ */
+public final class MissingRequiredReadDataSourceNamesException extends ReadwriteSplittingSQLException {
+
+ private static final long serialVersionUID = 3795576963060485964L;
+
+ public MissingRequiredReadDataSourceNamesException(final String databaseName) {
+ super(XOpenSQLState.CHECK_OPTION_VIOLATION, 92, "Read data source names is required in database `%s`.", databaseName);
+ }
+}
diff --git a/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/exception/checker/MissingRequiredWriteDataSourceNameException.java b/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/exception/checker/MissingRequiredWriteDataSourceNameException.java
new file mode 100644
index 00000000000..2c769bf5e7b
--- /dev/null
+++ b/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/exception/checker/MissingRequiredWriteDataSourceNameException.java
@@ -0,0 +1,33 @@
+/*
+ * 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.exception.checker;
+
+import org.apache.shardingsphere.infra.util.exception.external.sql.sqlstate.XOpenSQLState;
+import org.apache.shardingsphere.readwritesplitting.exception.ReadwriteSplittingSQLException;
+
+/**
+ * Missing required write data source name exception.
+ */
+public final class MissingRequiredWriteDataSourceNameException extends ReadwriteSplittingSQLException {
+
+ private static final long serialVersionUID = -3310589272704868388L;
+
+ public MissingRequiredWriteDataSourceNameException(final String databaseName) {
+ super(XOpenSQLState.CHECK_OPTION_VIOLATION, 91, "Write data source name is required in database `%s`.", databaseName);
+ }
+}
diff --git a/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/exception/rule/InvalidInlineExpressionDataSourceNameException.java b/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/exception/rule/InvalidInlineExpressionDataSourceNameException.java
new file mode 100644
index 00000000000..8cbe065fbf7
--- /dev/null
+++ b/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/exception/rule/InvalidInlineExpressionDataSourceNameException.java
@@ -0,0 +1,33 @@
+/*
+ * 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.exception.rule;
+
+import org.apache.shardingsphere.infra.util.exception.external.sql.sqlstate.XOpenSQLState;
+import org.apache.shardingsphere.readwritesplitting.exception.ReadwriteSplittingSQLException;
+
+/**
+ * Invalid inline expression data source name exception.
+ */
+public final class InvalidInlineExpressionDataSourceNameException extends ReadwriteSplittingSQLException {
+
+ private static final long serialVersionUID = 87659916563551964L;
+
+ public InvalidInlineExpressionDataSourceNameException(final String reason) {
+ super(XOpenSQLState.CHECK_OPTION_VIOLATION, 70, reason);
+ }
+}
diff --git a/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/rule/ReadwriteSplittingRule.java b/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/rule/ReadwriteSplittingRule.java
index 78c00624491..256dee05cfd 100644
--- a/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/rule/ReadwriteSplittingRule.java
+++ b/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/rule/ReadwriteSplittingRule.java
@@ -31,6 +31,7 @@ import org.apache.shardingsphere.infra.rule.identifier.type.StorageConnectorReus
import org.apache.shardingsphere.infra.rule.identifier.type.exportable.ExportableRule;
import org.apache.shardingsphere.infra.rule.identifier.type.exportable.constant.ExportableConstants;
import org.apache.shardingsphere.infra.rule.identifier.type.exportable.constant.ExportableItemConstants;
+import org.apache.shardingsphere.infra.util.exception.ShardingSpherePreconditions;
import org.apache.shardingsphere.infra.util.expr.InlineExpressionParser;
import org.apache.shardingsphere.infra.util.spi.type.required.RequiredSPIRegistry;
import org.apache.shardingsphere.mode.metadata.storage.StorageNodeStatus;
@@ -39,6 +40,7 @@ import org.apache.shardingsphere.readwritesplitting.api.ReadwriteSplittingRuleCo
import org.apache.shardingsphere.readwritesplitting.api.rule.ReadwriteSplittingDataSourceRuleConfiguration;
import org.apache.shardingsphere.readwritesplitting.api.strategy.DynamicReadwriteSplittingStrategyConfiguration;
import org.apache.shardingsphere.readwritesplitting.api.strategy.StaticReadwriteSplittingStrategyConfiguration;
+import org.apache.shardingsphere.readwritesplitting.exception.rule.InvalidInlineExpressionDataSourceNameException;
import org.apache.shardingsphere.readwritesplitting.spi.ReadQueryLoadBalanceAlgorithm;
import org.apache.shardingsphere.readwritesplitting.strategy.type.DynamicReadwriteSplittingStrategy;
import org.apache.shardingsphere.readwritesplitting.strategy.type.StaticReadwriteSplittingStrategy;
@@ -92,8 +94,10 @@ public final class ReadwriteSplittingRule implements DatabaseRule, DataSourceCon
List<String> inlineWriteDatasourceNames = new InlineExpressionParser(config.getStaticStrategy().getWriteDataSourceName()).splitAndEvaluate();
List<List<String>> inlineReadDatasourceNames = config.getStaticStrategy().getReadDataSourceNames().stream()
.map(each -> new InlineExpressionParser(each).splitAndEvaluate()).collect(Collectors.toList());
- Preconditions.checkArgument(inlineWriteDatasourceNames.size() == inlineReadwriteDataSourceNames.size(), "Inline expression write data source names size error");
- inlineReadDatasourceNames.forEach(each -> Preconditions.checkArgument(each.size() == inlineReadwriteDataSourceNames.size(), "Inline expression read data source names size error"));
+ ShardingSpherePreconditions.checkState(inlineWriteDatasourceNames.size() == inlineReadwriteDataSourceNames.size(),
+ () -> new InvalidInlineExpressionDataSourceNameException("Inline expression write data source names size error"));
+ inlineReadDatasourceNames.forEach(each -> ShardingSpherePreconditions.checkState(each.size() == inlineReadwriteDataSourceNames.size(),
+ () -> new InvalidInlineExpressionDataSourceNameException("Inline expression read data source names size error")));
for (int i = 0; i < inlineReadwriteDataSourceNames.size(); i++) {
ReadwriteSplittingDataSourceRuleConfiguration staticConfig = createStaticDataSourceRuleConfiguration(
config, i, inlineReadwriteDataSourceNames, inlineWriteDatasourceNames, inlineReadDatasourceNames);
@@ -116,7 +120,8 @@ public final class ReadwriteSplittingRule implements DatabaseRule, DataSourceCon
Map<String, ReadwriteSplittingDataSourceRule> result = new LinkedHashMap<>();
List<String> inlineReadwriteDataSourceNames = new InlineExpressionParser(config.getName()).splitAndEvaluate();
List<String> inlineAutoAwareDataSourceNames = new InlineExpressionParser(config.getDynamicStrategy().getAutoAwareDataSourceName()).splitAndEvaluate();
- Preconditions.checkArgument(inlineAutoAwareDataSourceNames.size() == inlineReadwriteDataSourceNames.size(), "Inline expression auto aware data source names size error");
+ ShardingSpherePreconditions.checkState(inlineAutoAwareDataSourceNames.size() == inlineReadwriteDataSourceNames.size(),
+ () -> new InvalidInlineExpressionDataSourceNameException("Inline expression auto aware data source names size error"));
for (int i = 0; i < inlineReadwriteDataSourceNames.size(); i++) {
ReadwriteSplittingDataSourceRuleConfiguration dynamicConfig = createDynamicDataSourceRuleConfiguration(config, i, inlineReadwriteDataSourceNames, inlineAutoAwareDataSourceNames);
result.put(inlineReadwriteDataSourceNames.get(i), new ReadwriteSplittingDataSourceRule(dynamicConfig, loadBalanceAlgorithm, builtRules));
diff --git a/features/readwrite-splitting/core/src/test/java/org/apache/shardingsphere/readwritesplitting/checker/ReadwriteSplittingRuleConfigurationCheckerTest.java b/features/readwrite-splitting/core/src/test/java/org/apache/shardingsphere/readwritesplitting/checker/ReadwriteSplittingRuleConfigurationCheckerTest.java
index 37d822cfaf2..b2269956443 100644
--- a/features/readwrite-splitting/core/src/test/java/org/apache/shardingsphere/readwritesplitting/checker/ReadwriteSplittingRuleConfigurationCheckerTest.java
+++ b/features/readwrite-splitting/core/src/test/java/org/apache/shardingsphere/readwritesplitting/checker/ReadwriteSplittingRuleConfigurationCheckerTest.java
@@ -26,6 +26,8 @@ import org.apache.shardingsphere.readwritesplitting.api.ReadwriteSplittingRuleCo
import org.apache.shardingsphere.readwritesplitting.api.rule.ReadwriteSplittingDataSourceRuleConfiguration;
import org.apache.shardingsphere.readwritesplitting.api.strategy.DynamicReadwriteSplittingStrategyConfiguration;
import org.apache.shardingsphere.readwritesplitting.api.strategy.StaticReadwriteSplittingStrategyConfiguration;
+import org.apache.shardingsphere.readwritesplitting.exception.checker.InvalidWeightLoadBalancerConfigurationException;
+import org.apache.shardingsphere.readwritesplitting.exception.checker.LoadBalancerAlgorithmNotFoundException;
import org.apache.shardingsphere.test.util.PropertiesBuilder;
import org.apache.shardingsphere.test.util.PropertiesBuilder.Property;
import org.junit.Test;
@@ -77,7 +79,7 @@ public final class ReadwriteSplittingRuleConfigurationCheckerTest {
}
@SuppressWarnings({"rawtypes", "unchecked"})
- @Test(expected = NullPointerException.class)
+ @Test(expected = LoadBalancerAlgorithmNotFoundException.class)
public void assertCheckWhenConfigInvalidWriteDataSource() {
ReadwriteSplittingRuleConfiguration config = mock(ReadwriteSplittingRuleConfiguration.class);
List<ReadwriteSplittingDataSourceRuleConfiguration> configurations = Arrays.asList(createDataSourceRuleConfig(
@@ -88,7 +90,7 @@ public final class ReadwriteSplittingRuleConfigurationCheckerTest {
}
@SuppressWarnings({"rawtypes", "unchecked"})
- @Test(expected = NullPointerException.class)
+ @Test(expected = LoadBalancerAlgorithmNotFoundException.class)
public void assertCheckWhenConfigInvalidReadDataSource() {
ReadwriteSplittingRuleConfiguration config = mock(ReadwriteSplittingRuleConfiguration.class);
List<ReadwriteSplittingDataSourceRuleConfiguration> configurations = Arrays.asList(createDataSourceRuleConfig(
@@ -99,7 +101,7 @@ public final class ReadwriteSplittingRuleConfigurationCheckerTest {
}
@SuppressWarnings({"rawtypes", "unchecked"})
- @Test(expected = IllegalStateException.class)
+ @Test(expected = InvalidWeightLoadBalancerConfigurationException.class)
public void assertCheckWeightLoadBalanceInvalidDataSourceName() {
ReadwriteSplittingRuleConfiguration config = mock(ReadwriteSplittingRuleConfiguration.class);
List<ReadwriteSplittingDataSourceRuleConfiguration> configs = Collections.singletonList(createDataSourceRuleConfig("write_ds_0", Arrays.asList("read_ds_0", "read_ds_1")));