Skip to content

Commit

Permalink
Refactor DistSQLExecutorRequiredChecker (#29993)
Browse files Browse the repository at this point in the history
* Refactor DistSQLUpdateExecuteEngine

* Refactor DistSQLExecutorRequiredChecker
  • Loading branch information
terrymanu authored Feb 4, 2024
1 parent f4f6c16 commit 00dbf3a
Show file tree
Hide file tree
Showing 28 changed files with 53 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
/**
* Drop broadcast table rule executor.
*/
@DistSQLExecutorCurrentRuleRequired("Broadcast")
@DistSQLExecutorCurrentRuleRequired(BroadcastRule.class)
@Setter
public final class DropBroadcastTableRuleExecutor implements DatabaseRuleDropExecutor<DropBroadcastTableRuleStatement, BroadcastRule, BroadcastRuleConfiguration> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
/**
* Alter encrypt rule executor.
*/
@DistSQLExecutorCurrentRuleRequired("Encrypt")
@DistSQLExecutorCurrentRuleRequired(EncryptRule.class)
@Setter
public final class AlterEncryptRuleExecutor implements DatabaseRuleAlterExecutor<AlterEncryptRuleStatement, EncryptRule, EncryptRuleConfiguration> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
/**
* Drop encrypt rule executor.
*/
@DistSQLExecutorCurrentRuleRequired("Encrypt")
@DistSQLExecutorCurrentRuleRequired(EncryptRule.class)
@Setter
public final class DropEncryptRuleExecutor implements DatabaseRuleDropExecutor<DropEncryptRuleStatement, EncryptRule, EncryptRuleConfiguration> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
/**
* Alter mask rule executor.
*/
@DistSQLExecutorCurrentRuleRequired("Mask")
@DistSQLExecutorCurrentRuleRequired(MaskRule.class)
@Setter
public final class AlterMaskRuleExecutor implements DatabaseRuleAlterExecutor<AlterMaskRuleStatement, MaskRule, MaskRuleConfiguration> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
/**
* Drop mask rule statement executor.
*/
@DistSQLExecutorCurrentRuleRequired("Mask")
@DistSQLExecutorCurrentRuleRequired(MaskRule.class)
@Setter
public final class DropMaskRuleExecutor implements DatabaseRuleDropExecutor<DropMaskRuleStatement, MaskRule, MaskRuleConfiguration> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
/**
* Alter readwrite-splitting rule executor.
*/
@DistSQLExecutorCurrentRuleRequired("Readwrite-splitting")
@DistSQLExecutorCurrentRuleRequired(ReadwriteSplittingRule.class)
@Setter
public final class AlterReadwriteSplittingRuleExecutor implements DatabaseRuleAlterExecutor<AlterReadwriteSplittingRuleStatement, ReadwriteSplittingRule, ReadwriteSplittingRuleConfiguration> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
/**
* Drop readwrite-splitting rule executor.
*/
@DistSQLExecutorCurrentRuleRequired("Readwrite-splitting")
@DistSQLExecutorCurrentRuleRequired(ReadwriteSplittingRule.class)
@Setter
public final class DropReadwriteSplittingRuleExecutor implements DatabaseRuleDropExecutor<DropReadwriteSplittingRuleStatement, ReadwriteSplittingRule, ReadwriteSplittingRuleConfiguration> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
/**
* Alter default shadow algorithm executor.
*/
@DistSQLExecutorCurrentRuleRequired("Shadow")
@DistSQLExecutorCurrentRuleRequired(ShadowRule.class)
@Setter
public final class AlterDefaultShadowAlgorithmExecutor implements DatabaseRuleAlterExecutor<AlterDefaultShadowAlgorithmStatement, ShadowRule, ShadowRuleConfiguration> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
/**
* Alter shadow rule executor.
*/
@DistSQLExecutorCurrentRuleRequired("Shadow")
@DistSQLExecutorCurrentRuleRequired(ShadowRule.class)
@Setter
public final class AlterShadowRuleExecutor implements DatabaseRuleAlterExecutor<AlterShadowRuleStatement, ShadowRule, ShadowRuleConfiguration> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
/**
* Drop default shadow algorithm executor.
*/
@DistSQLExecutorCurrentRuleRequired("Shadow")
@DistSQLExecutorCurrentRuleRequired(ShadowRule.class)
@Setter
public final class DropDefaultShadowAlgorithmExecutor implements DatabaseRuleDropExecutor<DropDefaultShadowAlgorithmStatement, ShadowRule, ShadowRuleConfiguration> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
/**
* Drop shadow algorithm executor.
*/
@DistSQLExecutorCurrentRuleRequired("Shadow")
@DistSQLExecutorCurrentRuleRequired(ShadowRule.class)
@Setter
public final class DropShadowAlgorithmExecutor implements DatabaseRuleDropExecutor<DropShadowAlgorithmStatement, ShadowRule, ShadowRuleConfiguration> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
/**
* Drop shadow rule executor.
*/
@DistSQLExecutorCurrentRuleRequired("Shadow")
@DistSQLExecutorCurrentRuleRequired(ShadowRule.class)
@Setter
public final class DropShadowRuleExecutor implements DatabaseRuleDropExecutor<DropShadowRuleStatement, ShadowRule, ShadowRuleConfiguration> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
/**
* Alter default sharding strategy executor.
*/
@DistSQLExecutorCurrentRuleRequired("Sharding")
@DistSQLExecutorCurrentRuleRequired(ShardingRule.class)
@Setter
public final class AlterDefaultShardingStrategyExecutor implements DatabaseRuleAlterExecutor<AlterDefaultShardingStrategyStatement, ShardingRule, ShardingRuleConfiguration> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
/**
* Alter sharding table reference rule executor.
*/
@DistSQLExecutorCurrentRuleRequired("Sharding")
@DistSQLExecutorCurrentRuleRequired(ShardingRule.class)
@Setter
public final class AlterShardingTableReferenceRuleExecutor implements DatabaseRuleAlterExecutor<AlterShardingTableReferenceRuleStatement, ShardingRule, ShardingRuleConfiguration> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
/**
* Alter sharding table rule executor.
*/
@DistSQLExecutorCurrentRuleRequired("Sharding")
@DistSQLExecutorCurrentRuleRequired(ShardingRule.class)
@Setter
public final class AlterShardingTableRuleExecutor implements DatabaseRuleAlterExecutor<AlterShardingTableRuleStatement, ShardingRule, ShardingRuleConfiguration> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
/**
* Create sharding table reference rule executor.
*/
@DistSQLExecutorCurrentRuleRequired("Sharding")
@DistSQLExecutorCurrentRuleRequired(ShardingRule.class)
@Setter
public final class CreateShardingTableReferenceRuleExecutor implements DatabaseRuleCreateExecutor<CreateShardingTableReferenceRuleStatement, ShardingRule, ShardingRuleConfiguration> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
/**
* Drop default sharding strategy executor.
*/
@DistSQLExecutorCurrentRuleRequired("Sharding")
@DistSQLExecutorCurrentRuleRequired(ShardingRule.class)
@Setter
public final class DropDefaultShardingStrategyExecutor implements DatabaseRuleDropExecutor<DropDefaultShardingStrategyStatement, ShardingRule, ShardingRuleConfiguration> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
/**
* Drop sharding algorithm executor.
*/
@DistSQLExecutorCurrentRuleRequired("Sharding")
@DistSQLExecutorCurrentRuleRequired(ShardingRule.class)
@Setter
public final class DropShardingAlgorithmExecutor implements DatabaseRuleDropExecutor<DropShardingAlgorithmStatement, ShardingRule, ShardingRuleConfiguration> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
/**
* Drop sharding auditor statement executor.
*/
@DistSQLExecutorCurrentRuleRequired("Sharding auditor")
@DistSQLExecutorCurrentRuleRequired(ShardingRule.class)
@Setter
public final class DropShardingAuditorExecutor implements DatabaseRuleDropExecutor<DropShardingAuditorStatement, ShardingRule, ShardingRuleConfiguration> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
/**
* Drop sharding key generator executor.
*/
@DistSQLExecutorCurrentRuleRequired("Key generator")
@DistSQLExecutorCurrentRuleRequired(ShardingRule.class)
@Setter
public final class DropShardingKeyGeneratorExecutor implements DatabaseRuleDropExecutor<DropShardingKeyGeneratorStatement, ShardingRule, ShardingRuleConfiguration> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
/**
* Drop sharding table reference executor.
*/
@DistSQLExecutorCurrentRuleRequired("Sharding")
@DistSQLExecutorCurrentRuleRequired(ShardingRule.class)
@Setter
public final class DropShardingTableReferenceExecutor implements DatabaseRuleDropExecutor<DropShardingTableReferenceRuleStatement, ShardingRule, ShardingRuleConfiguration> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
/**
* Drop sharding table rule executor.
*/
@DistSQLExecutorCurrentRuleRequired("Sharding")
@DistSQLExecutorCurrentRuleRequired(ShardingRule.class)
@Setter
public final class DropShardingTableRuleExecutor implements DatabaseRuleDropExecutor<DropShardingTableRuleStatement, ShardingRule, ShardingRuleConfiguration> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

package org.apache.shardingsphere.distsql.handler.required;

import org.apache.shardingsphere.infra.rule.ShardingSphereRule;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
Expand All @@ -30,9 +32,9 @@
public @interface DistSQLExecutorCurrentRuleRequired {

/**
* Get rule type.
* Get rule class.
*
* @return rule type
* @return rule class
*/
String value();
Class<? extends ShardingSphereRule> value();
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.apache.shardingsphere.distsql.statement.rdl.rule.type.DropRuleStatement;
import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions;
import org.apache.shardingsphere.infra.exception.core.external.sql.type.generic.UnsupportedSQLOperationException;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
import org.apache.shardingsphere.mode.manager.ContextManager;
import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
Expand All @@ -41,22 +42,29 @@ public final class DistSQLExecutorRequiredChecker {
*
* @param sqlStatement SQL statement
* @param contextManager context manager
* @param databaseName database name
* @param rule rule
* @param database database
*/
public void check(final SQLStatement sqlStatement, final ContextManager contextManager, final String databaseName, final ShardingSphereRule rule) {
public void check(final SQLStatement sqlStatement, final ContextManager contextManager, final ShardingSphereDatabase database) {
Optional.ofNullable(executor.getClass().getAnnotation(DistSQLExecutorClusterModeRequired.class)).ifPresent(optional -> checkClusterMode(contextManager));
Optional.ofNullable(executor.getClass().getAnnotation(DistSQLExecutorCurrentRuleRequired.class)).ifPresent(optional -> checkCurrentRule(sqlStatement, databaseName, rule, optional));
Optional.ofNullable(executor.getClass().getAnnotation(DistSQLExecutorCurrentRuleRequired.class)).ifPresent(optional -> checkCurrentRule(sqlStatement, contextManager, database, optional));
}

private void checkClusterMode(final ContextManager contextManager) {
ShardingSpherePreconditions.checkState(contextManager.getInstanceContext().isCluster(), () -> new UnsupportedSQLOperationException("Mode must be `Cluster`."));
}

private void checkCurrentRule(final SQLStatement sqlStatement, final String databaseName, final ShardingSphereRule rule, final DistSQLExecutorCurrentRuleRequired currentRuleRequired) {
private void checkCurrentRule(final SQLStatement sqlStatement, final ContextManager contextManager, final ShardingSphereDatabase database,
final DistSQLExecutorCurrentRuleRequired currentRuleRequired) {
if (sqlStatement instanceof DropRuleStatement && ((DropRuleStatement) sqlStatement).isIfExists()) {
return;
}
ShardingSpherePreconditions.checkNotNull(rule, () -> new MissingRequiredRuleException(currentRuleRequired.value(), databaseName));
Optional<? extends ShardingSphereRule> rule = findRule(contextManager, database, currentRuleRequired.value());
String ruleType = currentRuleRequired.value().getSimpleName().substring(0, currentRuleRequired.value().getSimpleName().indexOf("Rule"));
ShardingSpherePreconditions.checkState(rule.isPresent(), () -> null == database ? new MissingRequiredRuleException(ruleType) : new MissingRequiredRuleException(ruleType, database.getName()));
}

private Optional<? extends ShardingSphereRule> findRule(final ContextManager contextManager, final ShardingSphereDatabase database, final Class<? extends ShardingSphereRule> ruleClass) {
Optional<? extends ShardingSphereRule> globalRule = contextManager.getMetaDataContexts().getMetaData().getGlobalRuleMetaData().findSingleRule(ruleClass);
return null == database || globalRule.isPresent() ? globalRule : database.getRuleMetaData().findSingleRule(ruleClass);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,10 @@ private void executeRuleDefinitionUpdate() {
@SuppressWarnings("rawtypes")
private void executeDatabaseRuleDefinitionUpdate(final DatabaseRuleDefinitionExecutor databaseExecutor) {
if (isNormalRuleUpdater()) {
new DatabaseRuleDefinitionExecuteEngine(
(RuleDefinitionStatement) sqlStatement, contextManager, getDatabase(databaseName), databaseExecutor).executeUpdate();
new DatabaseRuleDefinitionExecuteEngine((RuleDefinitionStatement) sqlStatement, contextManager, getDatabase(databaseName), databaseExecutor).executeUpdate();
} else {
// TODO Remove when metadata structure adjustment completed. #25485
new LegacyDatabaseRuleDefinitionExecuteEngine(
(RuleDefinitionStatement) sqlStatement, contextManager, getDatabase(databaseName), databaseExecutor).executeUpdate();
new LegacyDatabaseRuleDefinitionExecuteEngine((RuleDefinitionStatement) sqlStatement, contextManager, getDatabase(databaseName), databaseExecutor).executeUpdate();
}
}

Expand All @@ -106,8 +104,9 @@ private boolean isNormalRuleUpdater() {
@SuppressWarnings({"rawtypes", "unchecked"})
private void executeNormalUpdate() throws SQLException {
DistSQLUpdateExecutor executor = TypedSPILoader.getService(DistSQLUpdateExecutor.class, sqlStatement.getClass());
new DistSQLExecutorAwareSetter(executor).set(contextManager, null == databaseName ? null : getDatabase(databaseName), null);
new DistSQLExecutorRequiredChecker(executor).check(sqlStatement, contextManager, databaseName, null);
ShardingSphereDatabase database = null == databaseName ? null : getDatabase(databaseName);
new DistSQLExecutorAwareSetter(executor).set(contextManager, database, null);
new DistSQLExecutorRequiredChecker(executor).check(sqlStatement, contextManager, database);
executor.executeUpdate(sqlStatement, contextManager);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public void executeUpdate() {
executor.setDatabase(database);
Optional<ShardingSphereRule> rule = database.getRuleMetaData().findSingleRule(executor.getRuleClass());
executor.setRule(rule.orElse(null));
checkBeforeUpdate(rule.orElse(null));
checkBeforeUpdate();
RuleConfiguration currentRuleConfig = rule.map(ShardingSphereRule::getConfiguration).orElse(null);
if (getRefreshStatus()) {
contextManager.getMetaDataContexts().getPersistService().getMetaDataVersionPersistService()
Expand All @@ -61,8 +61,8 @@ public void executeUpdate() {
}

@SuppressWarnings("unchecked")
private void checkBeforeUpdate(final ShardingSphereRule rule) {
new DistSQLExecutorRequiredChecker(executor).check(sqlStatement, contextManager, database.getName(), rule);
private void checkBeforeUpdate() {
new DistSQLExecutorRequiredChecker(executor).check(sqlStatement, contextManager, database);
executor.checkBeforeUpdate(sqlStatement);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,16 @@ public void executeUpdate() {
executor.setDatabase(database);
Optional<ShardingSphereRule> rule = database.getRuleMetaData().findSingleRule(executor.getRuleClass());
executor.setRule(rule.orElse(null));
checkBeforeUpdate(rule.orElse(null));
checkBeforeUpdate();
RuleConfiguration currentRuleConfig = rule.map(ShardingSphereRule::getConfiguration).orElse(null);
if (getRefreshStatus(sqlStatement, executor)) {
contextManager.getInstanceContext().getModeContextManager().alterRuleConfiguration(database.getName(), processSQLStatement(database, sqlStatement, executor, currentRuleConfig));
}
}

@SuppressWarnings("unchecked")
private void checkBeforeUpdate(final ShardingSphereRule rule) {
new DistSQLExecutorRequiredChecker(executor).check(sqlStatement, contextManager, database.getName(), rule);
private void checkBeforeUpdate() {
new DistSQLExecutorRequiredChecker(executor).check(sqlStatement, contextManager, database);
executor.checkBeforeUpdate(sqlStatement);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
/**
* Unload single table statement executor.
*/
@DistSQLExecutorCurrentRuleRequired("Single")
@DistSQLExecutorCurrentRuleRequired(SingleRule.class)
@Setter
public final class UnloadSingleTableExecutor implements DatabaseRuleAlterExecutor<UnloadSingleTableStatement, SingleRule, SingleRuleConfiguration> {

Expand Down

0 comments on commit 00dbf3a

Please sign in to comment.