This is an automated email from the ASF dual-hosted git repository.

chengzhang 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 e0b4cf139ad Add DistSQLExecutorRequiredChecker (#29978)
e0b4cf139ad is described below

commit e0b4cf139ad323892bd472b8d94c8c8499d2dedf
Author: Liang Zhang <[email protected]>
AuthorDate: Sun Feb 4 13:10:13 2024 +0800

    Add DistSQLExecutorRequiredChecker (#29978)
    
    * Add DistSQLExecutorRequiredChecker
    
    * Add DistSQLExecutorRequiredChecker
---
 .../required/DistSQLExecutorRequiredChecker.java   | 65 ++++++++++++++++++++++
 .../type/update/DistSQLUpdateExecuteEngine.java    | 10 +---
 .../DatabaseRuleDefinitionExecuteEngine.java       | 14 +----
 .../LegacyDatabaseRuleDefinitionExecuteEngine.java | 14 +----
 4 files changed, 71 insertions(+), 32 deletions(-)

diff --git 
a/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/required/DistSQLExecutorRequiredChecker.java
 
b/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/required/DistSQLExecutorRequiredChecker.java
new file mode 100644
index 00000000000..f918afbd1f6
--- /dev/null
+++ 
b/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/required/DistSQLExecutorRequiredChecker.java
@@ -0,0 +1,65 @@
+/*
+ * 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.distsql.handler.required;
+
+import lombok.RequiredArgsConstructor;
+import 
org.apache.shardingsphere.distsql.handler.exception.rule.MissingRequiredRuleException;
+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.rule.ShardingSphereRule;
+import org.apache.shardingsphere.mode.manager.ContextManager;
+import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
+
+import java.util.Optional;
+
+/**
+ * DistSQL executor required checker.
+ */
+@RequiredArgsConstructor
+public final class DistSQLExecutorRequiredChecker {
+    
+    private final SQLStatement sqlStatement;
+    
+    private final ContextManager contextManager;
+    
+    private final String databaseName;
+    
+    private final Object executor;
+    
+    /**
+     * Check before DistSQL execute.
+     * 
+     * @param rule rule
+     */
+    public void check(final ShardingSphereRule rule) {
+        
Optional.ofNullable(executor.getClass().getAnnotation(DistSQLExecutorClusterModeRequired.class)).ifPresent(optional
 -> checkClusterMode());
+        
Optional.ofNullable(executor.getClass().getAnnotation(DistSQLExecutorCurrentRuleRequired.class)).ifPresent(optional
 -> checkCurrentRule(rule, optional));
+    }
+    
+    private void checkClusterMode() {
+        
ShardingSpherePreconditions.checkState(contextManager.getInstanceContext().isCluster(),
 () -> new UnsupportedSQLOperationException("Mode must be `Cluster`."));
+    }
+    
+    private void checkCurrentRule(final ShardingSphereRule rule, final 
DistSQLExecutorCurrentRuleRequired currentRuleRequired) {
+        if (sqlStatement instanceof DropRuleStatement && ((DropRuleStatement) 
sqlStatement).isIfExists()) {
+            return;
+        }
+        ShardingSpherePreconditions.checkNotNull(rule, () -> new 
MissingRequiredRuleException(currentRuleRequired.value(), databaseName));
+    }
+}
diff --git 
a/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/type/update/DistSQLUpdateExecuteEngine.java
 
b/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/type/update/DistSQLUpdateExecuteEngine.java
index c300f272c6a..95c0899eae5 100644
--- 
a/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/type/update/DistSQLUpdateExecuteEngine.java
+++ 
b/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/type/update/DistSQLUpdateExecuteEngine.java
@@ -19,7 +19,7 @@ package org.apache.shardingsphere.distsql.handler.type.update;
 
 import 
org.apache.shardingsphere.distsql.handler.aware.DistSQLExecutorDatabaseAware;
 import 
org.apache.shardingsphere.distsql.handler.aware.DistSQLExecutorRuleAware;
-import 
org.apache.shardingsphere.distsql.handler.required.DistSQLExecutorClusterModeRequired;
+import 
org.apache.shardingsphere.distsql.handler.required.DistSQLExecutorRequiredChecker;
 import 
org.apache.shardingsphere.distsql.handler.type.update.rdl.rule.engine.database.DatabaseRuleDefinitionExecuteEngine;
 import 
org.apache.shardingsphere.distsql.handler.type.update.rdl.rule.engine.global.GlobalRuleDefinitionExecuteEngine;
 import 
org.apache.shardingsphere.distsql.handler.type.update.rdl.rule.engine.legacy.LegacyDatabaseRuleDefinitionExecuteEngine;
@@ -116,16 +116,10 @@ public abstract class DistSQLUpdateExecuteEngine {
         if (executor instanceof DistSQLExecutorRuleAware) {
             setRule((DistSQLExecutorRuleAware) executor);
         }
-        checkBeforeUpdate(executor);
+        new DistSQLExecutorRequiredChecker(sqlStatement, contextManager, 
databaseName, executor).check(null);
         executor.executeUpdate(sqlStatement, contextManager);
     }
     
-    private void checkBeforeUpdate(final DistSQLUpdateExecutor<?> executor) {
-        if (null != 
executor.getClass().getAnnotation(DistSQLExecutorClusterModeRequired.class)) {
-            
ShardingSpherePreconditions.checkState(contextManager.getInstanceContext().isCluster(),
 () -> new UnsupportedSQLOperationException("Mode must be `Cluster`."));
-        }
-    }
-    
     @SuppressWarnings({"rawtypes", "unchecked"})
     private void setRule(final DistSQLExecutorRuleAware executor) throws 
UnsupportedSQLOperationException {
         Optional<ShardingSphereRule> rule = findRule(executor.getRuleClass());
diff --git 
a/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/type/update/rdl/rule/engine/database/DatabaseRuleDefinitionExecuteEngine.java
 
b/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/type/update/rdl/rule/engine/database/DatabaseRuleDefinitionExecuteEngine.java
index 1e75bc50842..5ca8d601244 100644
--- 
a/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/type/update/rdl/rule/engine/database/DatabaseRuleDefinitionExecuteEngine.java
+++ 
b/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/type/update/rdl/rule/engine/database/DatabaseRuleDefinitionExecuteEngine.java
@@ -18,14 +18,11 @@
 package 
org.apache.shardingsphere.distsql.handler.type.update.rdl.rule.engine.database;
 
 import lombok.RequiredArgsConstructor;
-import 
org.apache.shardingsphere.distsql.handler.exception.rule.MissingRequiredRuleException;
-import 
org.apache.shardingsphere.distsql.handler.required.DistSQLExecutorCurrentRuleRequired;
+import 
org.apache.shardingsphere.distsql.handler.required.DistSQLExecutorRequiredChecker;
 import 
org.apache.shardingsphere.distsql.handler.type.update.rdl.rule.spi.database.DatabaseRuleDefinitionExecutor;
 import 
org.apache.shardingsphere.distsql.handler.type.update.rdl.rule.spi.database.DatabaseRuleDropExecutor;
 import 
org.apache.shardingsphere.distsql.statement.rdl.rule.RuleDefinitionStatement;
-import 
org.apache.shardingsphere.distsql.statement.rdl.rule.type.DropRuleStatement;
 import org.apache.shardingsphere.infra.config.rule.RuleConfiguration;
-import 
org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions;
 import 
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
 import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
 import org.apache.shardingsphere.mode.manager.ContextManager;
@@ -65,17 +62,10 @@ public final class DatabaseRuleDefinitionExecuteEngine {
     
     @SuppressWarnings("unchecked")
     private void checkBeforeUpdate(final ShardingSphereRule rule) {
-        
Optional.ofNullable(executor.getClass().getAnnotation(DistSQLExecutorCurrentRuleRequired.class)).ifPresent(optional
 -> checkCurrentRule(rule, optional));
+        new DistSQLExecutorRequiredChecker(sqlStatement, contextManager, 
database.getName(), executor).check(rule);
         executor.checkBeforeUpdate(sqlStatement);
     }
     
-    private void checkCurrentRule(final ShardingSphereRule rule, final 
DistSQLExecutorCurrentRuleRequired currentRuleRequired) {
-        if (sqlStatement instanceof DropRuleStatement && ((DropRuleStatement) 
sqlStatement).isIfExists()) {
-            return;
-        }
-        ShardingSpherePreconditions.checkNotNull(rule, () -> new 
MissingRequiredRuleException(currentRuleRequired.value(), database.getName()));
-    }
-    
     @SuppressWarnings({"rawtypes", "unchecked"})
     private boolean getRefreshStatus() {
         return !(executor instanceof DatabaseRuleDropExecutor) || 
((DatabaseRuleDropExecutor) executor).hasAnyOneToBeDropped(sqlStatement);
diff --git 
a/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/type/update/rdl/rule/engine/legacy/LegacyDatabaseRuleDefinitionExecuteEngine.java
 
b/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/type/update/rdl/rule/engine/legacy/LegacyDatabaseRuleDefinitionExecuteEngine.java
index 9adc429429b..30d985d9591 100644
--- 
a/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/type/update/rdl/rule/engine/legacy/LegacyDatabaseRuleDefinitionExecuteEngine.java
+++ 
b/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/type/update/rdl/rule/engine/legacy/LegacyDatabaseRuleDefinitionExecuteEngine.java
@@ -18,17 +18,14 @@
 package 
org.apache.shardingsphere.distsql.handler.type.update.rdl.rule.engine.legacy;
 
 import lombok.RequiredArgsConstructor;
-import 
org.apache.shardingsphere.distsql.handler.exception.rule.MissingRequiredRuleException;
-import 
org.apache.shardingsphere.distsql.handler.required.DistSQLExecutorCurrentRuleRequired;
+import 
org.apache.shardingsphere.distsql.handler.required.DistSQLExecutorRequiredChecker;
 import 
org.apache.shardingsphere.distsql.handler.type.update.rdl.rule.spi.database.DatabaseRuleAlterExecutor;
 import 
org.apache.shardingsphere.distsql.handler.type.update.rdl.rule.spi.database.DatabaseRuleCreateExecutor;
 import 
org.apache.shardingsphere.distsql.handler.type.update.rdl.rule.spi.database.DatabaseRuleDefinitionExecutor;
 import 
org.apache.shardingsphere.distsql.handler.type.update.rdl.rule.spi.database.DatabaseRuleDropExecutor;
 import 
org.apache.shardingsphere.distsql.statement.rdl.rule.RuleDefinitionStatement;
-import 
org.apache.shardingsphere.distsql.statement.rdl.rule.type.DropRuleStatement;
 import org.apache.shardingsphere.infra.config.rule.RuleConfiguration;
 import 
org.apache.shardingsphere.infra.config.rule.decorator.RuleConfigurationDecorator;
-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;
@@ -76,17 +73,10 @@ public final class 
LegacyDatabaseRuleDefinitionExecuteEngine {
     
     @SuppressWarnings("unchecked")
     private void checkBeforeUpdate(final ShardingSphereRule rule) {
-        
Optional.ofNullable(executor.getClass().getAnnotation(DistSQLExecutorCurrentRuleRequired.class)).ifPresent(optional
 -> checkCurrentRule(rule, optional));
+        new DistSQLExecutorRequiredChecker(sqlStatement, contextManager, 
database.getName(), executor).check(rule);
         executor.checkBeforeUpdate(sqlStatement);
     }
     
-    private void checkCurrentRule(final ShardingSphereRule rule, final 
DistSQLExecutorCurrentRuleRequired currentRuleRequired) {
-        if (sqlStatement instanceof DropRuleStatement && ((DropRuleStatement) 
sqlStatement).isIfExists()) {
-            return;
-        }
-        ShardingSpherePreconditions.checkNotNull(rule, () -> new 
MissingRequiredRuleException(currentRuleRequired.value(), database.getName()));
-    }
-    
     @SuppressWarnings({"unchecked", "rawtypes"})
     private boolean getRefreshStatus(final SQLStatement sqlStatement, final 
DatabaseRuleDefinitionExecutor<?, ?> executor) {
         return !(executor instanceof DatabaseRuleDropExecutor) || 
((DatabaseRuleDropExecutor) executor).hasAnyOneToBeDropped(sqlStatement);

Reply via email to