Skip to content

Commit

Permalink
Refactor RuleDefinitionException (#30718)
Browse files Browse the repository at this point in the history
* Refactor DuplicateRuleException

* Refactor MissingRequiredRuleException

* Refactor InUsedRuleException

* Refactor EmptyRuleException

* Refactor MissingRequiredSingleTableException

* Refactor DuplicateStorageUnitException

* Refactor DuplicateStorageUnitException
  • Loading branch information
terrymanu authored Mar 31, 2024
1 parent 3451db8 commit 152da00
Show file tree
Hide file tree
Showing 20 changed files with 63 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ SQL 错误码以标准的 SQL State,Vendor Code 和详细错误信息提供,
| HY000 | 10005 | Column '%s' in %s is ambiguous. |
| 42S02 | 10006 | Unknown column '%s' in '%s'. |
| 42S02 | 10007 | Table or view \`%s\` does not exist. |
| 42S02 | 10008 | Missing required table '%s' in storage unit '%s'. |
| 42000 | 10010 | Rule does not exist. |
| 42S02 | 10020 | Schema \`%s\` does not exist. |
| 42S02 | 10021 | Single table \`%s\` does not exist. |
Expand All @@ -31,14 +32,14 @@ SQL 错误码以标准的 SQL State,Vendor Code 和详细错误信息提供,
| 44000 | 10102 | Storage units '%s' do not exist in database '%s'. |
| 44000 | 10103 | Storage unit '%s' still used by '%s'. |
| 42S01 | 10104 | Duplicate storage unit names '%s'. |
| 08000 | 10105 | Storage units can not connect, error messages are: %s. |
| 0A000 | 10106 | Can not alter connection info in storage units: '%s'. |
| 08000 | 10110 | Storage units can not connect, error messages are: %s. |
| 0A000 | 10111 | Can not alter connection info in storage units: '%s'. |
| 44000 | 10120 | Invalid storage unit status, error message is: %s. |
| 44000 | 10200 | Invalid '%s' rule '%s', error message is: %s |
| 44000 | 10201 | %s rules '%s' do not exist in database '%s'. |
| 44000 | 10202 | %s rules '%s' in database '%s' are still in used. |
| 44000 | 10205 | Duplicate %s rule names '%s' in database '%s'. |
| 44000 | 10210 | There is no rule in database '%s'. |
| 42S02 | 10201 | There is no rule in database '%s'. |
| 42S02 | 10202 | %s rules '%s' do not exist in database '%s'. |
| 44000 | 10203 | %s rules '%s' in database '%s' are still in used. |
| 42S01 | 10204 | Duplicate %s rule names '%s' in database '%s'. |
| 44000 | 10300 | Algorithm '%s.'%s' initialization failed, reason is: %s. |
| 44000 | 10301 | '%s' algorithm on %s is required. |
| 42S02 | 10302 | '%s' algorithm '%s' on %s is unregistered. |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ SQL error codes provide by standard `SQL State`, `Vendor Code` and `Reason`, whi
| HY000 | 10005 | Column '%s' in %s is ambiguous. |
| 42S02 | 10006 | Unknown column '%s' in '%s'. |
| 42S02 | 10007 | Table or view \`%s\` does not exist. |
| 42S02 | 10008 | Missing required table '%s' in storage unit '%s'. |
| 42000 | 10010 | Rule does not exist. |
| 42S02 | 10020 | Schema \`%s\` does not exist. |
| 42S02 | 10021 | Single table \`%s\` does not exist. |
Expand All @@ -31,13 +32,15 @@ SQL error codes provide by standard `SQL State`, `Vendor Code` and `Reason`, whi
| 44000 | 10102 | Storage units '%s' do not exist in database '%s'. |
| 44000 | 10103 | Storage unit '%s' still used by '%s'. |
| 42S01 | 10104 | Duplicate storage unit names '%s'. |
| 08000 | 10105 | Storage units can not connect, error messages are: %s. |
| 0A000 | 10106 | Can not alter connection info in storage units: '%s'. |
| 08000 | 10110 | Storage units can not connect, error messages are: %s. |
| 0A000 | 10111 | Can not alter connection info in storage units: '%s'. |
| 44000 | 10120 | Invalid storage unit status, error message is: %s. |
| 44000 | 10200 | Invalid '%s' rule '%s', error message is: %s |
| 44000 | 10201 | %s rules '%s' do not exist in database '%s'. |
| 44000 | 10202 | %s rules '%s' in database '%s' are still in used. |
| 44000 | 10205 | Duplicate %s rule names '%s' in database '%s'. |
| 44000 | 10200 | Invalid '%s' rule '%s', error message is: %s |
| 42S02 | 10201 | There is no rule in database '%s'. |
| 42S02 | 10202 | %s rules '%s' do not exist in database '%s'. |
| 44000 | 10203 | %s rules '%s' in database '%s' are still in used. |
| 42S01 | 10204 | Duplicate %s rule names '%s' in database '%s'. |
| 44000 | 10210 | There is no rule in database '%s'. |
| 44000 | 10300 | Algorithm '%s.'%s' initialization failed, reason is: %s. |
| 44000 | 10301 | '%s' algorithm on %s is required. |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import lombok.Setter;
import org.apache.shardingsphere.distsql.handler.engine.update.rdl.rule.spi.database.DatabaseRuleDropExecutor;
import org.apache.shardingsphere.infra.exception.rule.MissingRequiredRuleException;
import org.apache.shardingsphere.infra.exception.rule.RuleInUsedException;
import org.apache.shardingsphere.infra.exception.rule.InUsedRuleException;
import org.apache.shardingsphere.distsql.handler.required.DistSQLExecutorCurrentRuleRequired;
import org.apache.shardingsphere.infra.algorithm.core.config.AlgorithmConfiguration;
import org.apache.shardingsphere.infra.datanode.DataNode;
Expand Down Expand Up @@ -72,7 +72,7 @@ private void checkToBeDroppedRuleNames(final DropReadwriteSplittingRuleStatement
private void checkToBeDroppedInUsed(final DropReadwriteSplittingRuleStatement sqlStatement) {
Collection<String> resourceBeUsed = getInUsedResources();
Collection<String> ruleInUsed = sqlStatement.getNames().stream().filter(resourceBeUsed::contains).collect(Collectors.toSet());
ShardingSpherePreconditions.checkState(ruleInUsed.isEmpty(), () -> new RuleInUsedException("Readwrite-splitting", database.getName(), ruleInUsed));
ShardingSpherePreconditions.checkState(ruleInUsed.isEmpty(), () -> new InUsedRuleException("Readwrite-splitting", database.getName(), ruleInUsed));
}

private Collection<String> getInUsedResources() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import org.apache.shardingsphere.infra.datanode.DataNode;
import org.apache.shardingsphere.infra.exception.core.external.sql.type.kernel.category.RuleDefinitionException;
import org.apache.shardingsphere.infra.exception.rule.MissingRequiredRuleException;
import org.apache.shardingsphere.infra.exception.rule.RuleInUsedException;
import org.apache.shardingsphere.infra.exception.rule.InUsedRuleException;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.rule.attribute.RuleAttributes;
import org.apache.shardingsphere.infra.rule.attribute.datanode.DataNodeRuleAttribute;
Expand Down Expand Up @@ -88,7 +88,7 @@ void assertCheckSQLStatementWithInUsed() throws RuleDefinitionException {
executor.setDatabase(database);
when(rule.getConfiguration()).thenReturn(createCurrentRuleConfiguration());
executor.setRule(rule);
assertThrows(RuleInUsedException.class, () -> executor.checkBeforeUpdate(createSQLStatement()));
assertThrows(InUsedRuleException.class, () -> executor.checkBeforeUpdate(createSQLStatement()));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import lombok.Setter;
import org.apache.shardingsphere.distsql.handler.engine.update.rdl.rule.spi.database.DatabaseRuleDropExecutor;
import org.apache.shardingsphere.infra.exception.rule.MissingRequiredRuleException;
import org.apache.shardingsphere.infra.exception.rule.RuleInUsedException;
import org.apache.shardingsphere.infra.exception.rule.InUsedRuleException;
import org.apache.shardingsphere.distsql.handler.required.DistSQLExecutorCurrentRuleRequired;
import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
Expand Down Expand Up @@ -79,7 +79,7 @@ private void checkBindingTables(final DropShardingTableRuleStatement sqlStatemen
Collection<String> bindingTables = getBindingTables();
Collection<String> usedTableNames = getToBeDroppedShardingTableNames(sqlStatement).stream().filter(bindingTables::contains).collect(Collectors.toList());
if (!usedTableNames.isEmpty()) {
throw new RuleInUsedException("Sharding", database.getName(), usedTableNames, "sharding table reference");
throw new InUsedRuleException("Sharding", database.getName(), usedTableNames, "sharding table reference");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import com.google.common.base.Splitter;
import org.apache.shardingsphere.infra.exception.core.external.sql.type.kernel.category.RuleDefinitionException;
import org.apache.shardingsphere.infra.exception.rule.MissingRequiredRuleException;
import org.apache.shardingsphere.infra.exception.rule.RuleInUsedException;
import org.apache.shardingsphere.infra.exception.rule.InUsedRuleException;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration;
import org.apache.shardingsphere.sharding.api.config.rule.ShardingAutoTableRuleConfiguration;
Expand Down Expand Up @@ -83,7 +83,7 @@ void assertCheckSQLStatementWithBindingTableRule() throws RuleDefinitionExceptio
ShardingRule rule = mock(ShardingRule.class);
when(rule.getConfiguration()).thenReturn(createCurrentRuleConfiguration());
executor.setRule(rule);
assertThrows(RuleInUsedException.class, () -> executor.checkBeforeUpdate(createSQLStatement("t_order_item")));
assertThrows(InUsedRuleException.class, () -> executor.checkBeforeUpdate(createSQLStatement("t_order_item")));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ public final class DuplicateRuleException extends RuleDefinitionException {
private static final long serialVersionUID = -1738699538105858939L;

public DuplicateRuleException(final String ruleType, final String databaseName, final Collection<String> ruleNames) {
super(XOpenSQLState.CHECK_OPTION_VIOLATION, 5, String.format("Duplicate %s rule names '%s' in database '%s'.", ruleType, ruleNames, databaseName));
super(XOpenSQLState.DUPLICATE, 4, String.format("Duplicate %s rule names '%s' in database '%s'.", ruleType, ruleNames, databaseName));
}

public DuplicateRuleException(final String ruleType, final Collection<String> ruleNames) {
super(XOpenSQLState.CHECK_OPTION_VIOLATION, 5, String.format("Duplicate %s rule names '%s'.", ruleType, ruleNames));
super(XOpenSQLState.DUPLICATE, 4, String.format("Duplicate %s rule names '%s'.", ruleType, ruleNames));
}

public DuplicateRuleException(final String type, final String databaseName) {
super(XOpenSQLState.CHECK_OPTION_VIOLATION, 5, String.format("Duplicate '%s' in database '%s'.", type, databaseName));
super(XOpenSQLState.DUPLICATE, 4, String.format("Duplicate '%s' in database '%s'.", type, databaseName));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@
import org.apache.shardingsphere.infra.exception.core.external.sql.type.kernel.category.RuleDefinitionException;

/**
* Rule does not exist exception.
* Empty rule unit exception.
*/
public final class RuleNotExistedException extends RuleDefinitionException {
public final class EmptyRuleException extends RuleDefinitionException {

private static final long serialVersionUID = -4150905802300104824L;

public RuleNotExistedException(final String databaseName) {
super(XOpenSQLState.SYNTAX_ERROR, 10, "There is no rule in database '%s'.", databaseName);
public EmptyRuleException(final String databaseName) {
super(XOpenSQLState.NOT_FOUND, 1, "There is no rule in database '%s'.", databaseName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,17 @@
import java.util.Collection;

/**
* Rule in used exception.
* In used rule exception.
*/
public final class RuleInUsedException extends RuleDefinitionException {
public final class InUsedRuleException extends RuleDefinitionException {

private static final long serialVersionUID = 3308787279125477660L;

public RuleInUsedException(final String ruleType, final String databaseName, final Collection<String> ruleNames) {
super(XOpenSQLState.CHECK_OPTION_VIOLATION, 2, "%s rules '%s' in database '%s' are still in used.", ruleType, ruleNames, databaseName);
public InUsedRuleException(final String ruleType, final String databaseName, final Collection<String> ruleNames) {
super(XOpenSQLState.CHECK_OPTION_VIOLATION, 3, "%s rules '%s' in database '%s' are still in used.", ruleType, ruleNames, databaseName);
}

public RuleInUsedException(final String ruleType, final String databaseName, final Collection<String> ruleNames, final String usingType) {
super(XOpenSQLState.CHECK_OPTION_VIOLATION, 2, "%s rules '%s' in database '%s' are still in used by %s.", ruleType, ruleNames, databaseName, usingType);
public InUsedRuleException(final String ruleType, final String databaseName, final Collection<String> ruleNames, final String usingType) {
super(XOpenSQLState.CHECK_OPTION_VIOLATION, 3, "%s rules '%s' in database '%s' are still in used by %s.", ruleType, ruleNames, databaseName, usingType);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,22 @@ public final class MissingRequiredRuleException extends RuleDefinitionException
private static final long serialVersionUID = -8464574460917965546L;

public MissingRequiredRuleException(final String ruleType) {
super(XOpenSQLState.CHECK_OPTION_VIOLATION, 1, "%s rule does not exist.", ruleType);
super(XOpenSQLState.NOT_FOUND, 2, "%s rule does not exist.", ruleType);
}

public MissingRequiredRuleException(final String ruleType, final String databaseName) {
super(XOpenSQLState.CHECK_OPTION_VIOLATION, 1, "%s rule does not exist in database '%s'.", ruleType, databaseName);
super(XOpenSQLState.NOT_FOUND, 2, "%s rule does not exist in database '%s'.", ruleType, databaseName);
}

public MissingRequiredRuleException(final String ruleType, final Collection<String> ruleNames) {
super(XOpenSQLState.CHECK_OPTION_VIOLATION, 1, "%s rules '%s' do not exist.", ruleType, ruleNames);
super(XOpenSQLState.NOT_FOUND, 2, "%s rules '%s' do not exist.", ruleType, ruleNames);
}

public MissingRequiredRuleException(final String ruleType, final String databaseName, final String ruleName) {
super(XOpenSQLState.CHECK_OPTION_VIOLATION, 1, "%s rule '%s' do not exist in database '%s'.", ruleType, ruleName, databaseName);
super(XOpenSQLState.NOT_FOUND, 2, "%s rule '%s' do not exist in database '%s'.", ruleType, ruleName, databaseName);
}

public MissingRequiredRuleException(final String ruleType, final String databaseName, final Collection<String> ruleNames) {
super(XOpenSQLState.CHECK_OPTION_VIOLATION, 1, "%s rules '%s' do not exist in database '%s'.", ruleType, ruleNames, databaseName);
super(XOpenSQLState.NOT_FOUND, 1, "%s rules '%s' do not exist in database '%s'.", ruleType, ruleNames, databaseName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@ public final class AlterStorageUnitConnectionInfoException extends ResourceDefin
private static final long serialVersionUID = 525999625052706626L;

public AlterStorageUnitConnectionInfoException(final Collection<String> storageUnitNames) {
super(XOpenSQLState.FEATURE_NOT_SUPPORTED, 6, "Can not alter connection info in storage units: '%s'.", String.join(", ", storageUnitNames));
super(XOpenSQLState.FEATURE_NOT_SUPPORTED, 11, "Can not alter connection info in storage units: '%s'.", String.join(", ", storageUnitNames));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@
/**
* Duplicate storage unit exception.
*/
public final class DuplicateStorageUnitExceptionDefinition extends ResourceDefinitionException {
public final class DuplicateStorageUnitException extends ResourceDefinitionException {

private static final long serialVersionUID = 2103793827572264148L;

public DuplicateStorageUnitExceptionDefinition(final Collection<String> storageUnitNames) {
public DuplicateStorageUnitException(final Collection<String> storageUnitNames) {
super(XOpenSQLState.DUPLICATE, 4, "Duplicate storage unit names '%s'.", String.join(", ", storageUnitNames));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public final class StorageUnitsConnectException extends ResourceDefinitionExcept
private static final long serialVersionUID = 1824912697040264268L;

public StorageUnitsConnectException(final Map<String, Exception> causes) {
super(XOpenSQLState.CONNECTION_EXCEPTION, 5, "Storage units can not connect, error messages are: %s.", causes.entrySet().stream().map(entry -> String.format(
super(XOpenSQLState.CONNECTION_EXCEPTION, 10, "Storage units can not connect, error messages are: %s.", causes.entrySet().stream().map(entry -> String.format(
"Storage unit name: '%s', error message is: %s", entry.getKey(), entry.getValue().getMessage())).collect(Collectors.joining(System.lineSeparator())));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions;
import org.apache.shardingsphere.infra.exception.core.external.ShardingSphereExternalException;
import org.apache.shardingsphere.infra.exception.storageunit.AlterStorageUnitConnectionInfoException;
import org.apache.shardingsphere.infra.exception.storageunit.DuplicateStorageUnitExceptionDefinition;
import org.apache.shardingsphere.infra.exception.storageunit.DuplicateStorageUnitException;
import org.apache.shardingsphere.infra.exception.storageunit.StorageUnitsOperateException;
import org.apache.shardingsphere.infra.exception.storageunit.MissingRequiredStorageUnitsException;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
Expand Down Expand Up @@ -80,7 +80,7 @@ private void checkBefore(final AlterStorageUnitStatement sqlStatement) {

private void checkDuplicatedStorageUnitNames(final Collection<String> storageUnitNames) {
Collection<String> duplicatedStorageUnitNames = storageUnitNames.stream().filter(each -> storageUnitNames.stream().filter(each::equals).count() > 1).collect(Collectors.toList());
ShardingSpherePreconditions.checkState(duplicatedStorageUnitNames.isEmpty(), () -> new DuplicateStorageUnitExceptionDefinition(duplicatedStorageUnitNames));
ShardingSpherePreconditions.checkState(duplicatedStorageUnitNames.isEmpty(), () -> new DuplicateStorageUnitException(duplicatedStorageUnitNames));
}

private void checkStorageUnitNameExisted(final Collection<String> storageUnitNames) {
Expand Down
Loading

0 comments on commit 152da00

Please sign in to comment.