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

sunnianjun 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 35cd5a77497 Add UnregisterStorageUnitRuleUsageChecker (#30007)
35cd5a77497 is described below

commit 35cd5a77497e979ce7bf10dbb90b604a00c56692
Author: Liang Zhang <[email protected]>
AuthorDate: Tue Feb 6 12:52:43 2024 +0800

    Add UnregisterStorageUnitRuleUsageChecker (#30007)
---
 ...gisterStorageUnitBroadcastRuleUsageChecker.java | 38 +++++++++++++++++++
 ....resource.UnregisterStorageUnitRuleUsageChecker | 18 +++++++++
 .../UnregisterStorageUnitRuleUsageChecker.java     | 44 ++++++++++++++++++++++
 ...nregisterStorageUnitSingleRuleUsageChecker.java | 38 +++++++++++++++++++
 ....resource.UnregisterStorageUnitRuleUsageChecker | 18 +++++++++
 .../resource/UnregisterStorageUnitExecutor.java    | 32 +++++++---------
 6 files changed, 169 insertions(+), 19 deletions(-)

diff --git 
a/features/broadcast/distsql/handler/src/main/java/org/apache/shardingsphere/broadcast/distsql/handler/update/UnregisterStorageUnitBroadcastRuleUsageChecker.java
 
b/features/broadcast/distsql/handler/src/main/java/org/apache/shardingsphere/broadcast/distsql/handler/update/UnregisterStorageUnitBroadcastRuleUsageChecker.java
new file mode 100644
index 00000000000..8c6b5db8818
--- /dev/null
+++ 
b/features/broadcast/distsql/handler/src/main/java/org/apache/shardingsphere/broadcast/distsql/handler/update/UnregisterStorageUnitBroadcastRuleUsageChecker.java
@@ -0,0 +1,38 @@
+/*
+ * 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.broadcast.distsql.handler.update;
+
+import org.apache.shardingsphere.broadcast.rule.BroadcastRule;
+import 
org.apache.shardingsphere.distsql.handler.type.update.rdl.resource.UnregisterStorageUnitRuleUsageChecker;
+import 
org.apache.shardingsphere.distsql.statement.rdl.resource.unit.type.UnregisterStorageUnitStatement;
+
+/**
+ * Unregister storage unit broadcast rule usage checker.
+ */
+public final class UnregisterStorageUnitBroadcastRuleUsageChecker implements 
UnregisterStorageUnitRuleUsageChecker {
+    
+    @Override
+    public boolean isIgnored(final UnregisterStorageUnitStatement 
sqlStatement) {
+        return sqlStatement.isIgnoreBroadcastTables();
+    }
+    
+    @Override
+    public Class<BroadcastRule> getRule() {
+        return BroadcastRule.class;
+    }
+}
diff --git 
a/features/broadcast/distsql/handler/src/main/resources/META-INF/services/org.apache.shardingsphere.distsql.handler.type.update.rdl.resource.UnregisterStorageUnitRuleUsageChecker
 
b/features/broadcast/distsql/handler/src/main/resources/META-INF/services/org.apache.shardingsphere.distsql.handler.type.update.rdl.resource.UnregisterStorageUnitRuleUsageChecker
new file mode 100644
index 00000000000..3c81eac0680
--- /dev/null
+++ 
b/features/broadcast/distsql/handler/src/main/resources/META-INF/services/org.apache.shardingsphere.distsql.handler.type.update.rdl.resource.UnregisterStorageUnitRuleUsageChecker
@@ -0,0 +1,18 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+org.apache.shardingsphere.broadcast.distsql.handler.update.UnregisterStorageUnitBroadcastRuleUsageChecker
diff --git 
a/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/type/update/rdl/resource/UnregisterStorageUnitRuleUsageChecker.java
 
b/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/type/update/rdl/resource/UnregisterStorageUnitRuleUsageChecker.java
new file mode 100644
index 00000000000..128ee2c0122
--- /dev/null
+++ 
b/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/type/update/rdl/resource/UnregisterStorageUnitRuleUsageChecker.java
@@ -0,0 +1,44 @@
+/*
+ * 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.type.update.rdl.resource;
+
+import 
org.apache.shardingsphere.distsql.statement.rdl.resource.unit.type.UnregisterStorageUnitStatement;
+import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
+import org.apache.shardingsphere.infra.spi.annotation.SingletonSPI;
+
+/**
+ * Unregister storage unit rule usage checker.
+ */
+@SingletonSPI
+public interface UnregisterStorageUnitRuleUsageChecker {
+    
+    /**
+     * Whether to ignore rule usage checking when unregister storage unit.
+     * 
+     * @param sqlStatement unregister storage unit statement
+     * @return ignored or not
+     */
+    boolean isIgnored(UnregisterStorageUnitStatement sqlStatement);
+    
+    /**
+     * Get rule.
+     * 
+     * @return rule
+     */
+    Class<? extends ShardingSphereRule> getRule();
+}
diff --git 
a/kernel/single/distsql/handler/src/main/java/org/apache/shardingsphere/single/distsql/handler/update/UnregisterStorageUnitSingleRuleUsageChecker.java
 
b/kernel/single/distsql/handler/src/main/java/org/apache/shardingsphere/single/distsql/handler/update/UnregisterStorageUnitSingleRuleUsageChecker.java
new file mode 100644
index 00000000000..6aed1f93571
--- /dev/null
+++ 
b/kernel/single/distsql/handler/src/main/java/org/apache/shardingsphere/single/distsql/handler/update/UnregisterStorageUnitSingleRuleUsageChecker.java
@@ -0,0 +1,38 @@
+/*
+ * 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.single.distsql.handler.update;
+
+import 
org.apache.shardingsphere.distsql.handler.type.update.rdl.resource.UnregisterStorageUnitRuleUsageChecker;
+import 
org.apache.shardingsphere.distsql.statement.rdl.resource.unit.type.UnregisterStorageUnitStatement;
+import org.apache.shardingsphere.single.rule.SingleRule;
+
+/**
+ * Unregister storage unit single rule usage checker.
+ */
+public final class UnregisterStorageUnitSingleRuleUsageChecker implements 
UnregisterStorageUnitRuleUsageChecker {
+    
+    @Override
+    public boolean isIgnored(final UnregisterStorageUnitStatement 
sqlStatement) {
+        return sqlStatement.isIgnoreSingleTables();
+    }
+    
+    @Override
+    public Class<SingleRule> getRule() {
+        return SingleRule.class;
+    }
+}
diff --git 
a/kernel/single/distsql/handler/src/main/resources/META-INF/services/org.apache.shardingsphere.distsql.handler.type.update.rdl.resource.UnregisterStorageUnitRuleUsageChecker
 
b/kernel/single/distsql/handler/src/main/resources/META-INF/services/org.apache.shardingsphere.distsql.handler.type.update.rdl.resource.UnregisterStorageUnitRuleUsageChecker
new file mode 100644
index 00000000000..65708368991
--- /dev/null
+++ 
b/kernel/single/distsql/handler/src/main/resources/META-INF/services/org.apache.shardingsphere.distsql.handler.type.update.rdl.resource.UnregisterStorageUnitRuleUsageChecker
@@ -0,0 +1,18 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+org.apache.shardingsphere.single.distsql.handler.update.UnregisterStorageUnitSingleRuleUsageChecker
diff --git 
a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rdl/resource/UnregisterStorageUnitExecutor.java
 
b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rdl/resource/UnregisterStorageUnitExecutor.java
index f47c4e34187..461d1142ec5 100644
--- 
a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rdl/resource/UnregisterStorageUnitExecutor.java
+++ 
b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rdl/resource/UnregisterStorageUnitExecutor.java
@@ -19,26 +19,25 @@ package 
org.apache.shardingsphere.proxy.backend.handler.distsql.rdl.resource;
 
 import lombok.Setter;
 import lombok.extern.slf4j.Slf4j;
-import org.apache.shardingsphere.broadcast.rule.BroadcastRule;
 import 
org.apache.shardingsphere.distsql.handler.aware.DistSQLExecutorDatabaseAware;
 import 
org.apache.shardingsphere.distsql.handler.exception.storageunit.InvalidStorageUnitsException;
 import 
org.apache.shardingsphere.distsql.handler.exception.storageunit.MissingRequiredStorageUnitsException;
 import 
org.apache.shardingsphere.distsql.handler.exception.storageunit.StorageUnitInUsedException;
 import 
org.apache.shardingsphere.distsql.handler.type.update.DistSQLUpdateExecutor;
+import 
org.apache.shardingsphere.distsql.handler.type.update.rdl.resource.UnregisterStorageUnitRuleUsageChecker;
 import 
org.apache.shardingsphere.distsql.statement.rdl.resource.unit.type.UnregisterStorageUnitStatement;
 import 
org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions;
 import 
org.apache.shardingsphere.infra.exception.core.external.server.ShardingSphereServerException;
 import 
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
 import 
org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnit;
 import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
+import org.apache.shardingsphere.infra.spi.ShardingSphereServiceLoader;
 import org.apache.shardingsphere.mode.manager.ContextManager;
-import org.apache.shardingsphere.single.rule.SingleRule;
 
 import java.sql.SQLException;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.HashSet;
-import java.util.LinkedList;
 import java.util.Map;
 import java.util.stream.Collectors;
 
@@ -75,26 +74,21 @@ public final class UnregisterStorageUnitExecutor implements 
DistSQLUpdateExecuto
         Map<String, Collection<Class<? extends ShardingSphereRule>>> 
inUsedStorageUnits = 
database.getRuleMetaData().getInUsedStorageUnitNameAndRulesMap();
         Collection<String> inUsedStorageUnitNames = 
inUsedStorageUnits.keySet();
         inUsedStorageUnitNames.retainAll(sqlStatement.getStorageUnitNames());
-        if (!inUsedStorageUnitNames.isEmpty()) {
-            Collection<Class<? extends ShardingSphereRule>> 
ignoreShardingSphereRules = getIgnoreShardingSphereRules(sqlStatement);
-            if (!ignoreShardingSphereRules.isEmpty()) {
-                checkInUsedIgnoreTables(new HashSet<>(inUsedStorageUnitNames), 
inUsedStorageUnits, ignoreShardingSphereRules);
-            } else {
-                String firstResource = 
inUsedStorageUnitNames.iterator().next();
-                throw new StorageUnitInUsedException(firstResource, 
inUsedStorageUnits.get(firstResource));
-            }
+        if (inUsedStorageUnitNames.isEmpty()) {
+            return;
+        }
+        Collection<Class<? extends ShardingSphereRule>> 
ignoreShardingSphereRules = getIgnoreShardingSphereRules(sqlStatement);
+        if (!ignoreShardingSphereRules.isEmpty()) {
+            checkInUsedIgnoreTables(new HashSet<>(inUsedStorageUnitNames), 
inUsedStorageUnits, ignoreShardingSphereRules);
+        } else {
+            String firstResource = inUsedStorageUnitNames.iterator().next();
+            throw new StorageUnitInUsedException(firstResource, 
inUsedStorageUnits.get(firstResource));
         }
     }
     
     private Collection<Class<? extends ShardingSphereRule>> 
getIgnoreShardingSphereRules(final UnregisterStorageUnitStatement sqlStatement) 
{
-        Collection<Class<? extends ShardingSphereRule>> result = new 
LinkedList<>();
-        if (sqlStatement.isIgnoreSingleTables()) {
-            result.add(SingleRule.class);
-        }
-        if (sqlStatement.isIgnoreBroadcastTables()) {
-            result.add(BroadcastRule.class);
-        }
-        return result;
+        return 
ShardingSphereServiceLoader.getServiceInstances(UnregisterStorageUnitRuleUsageChecker.class).stream()
+                .filter(each -> 
each.isIgnored(sqlStatement)).map(UnregisterStorageUnitRuleUsageChecker::getRule).collect(Collectors.toList());
     }
     
     private void checkInUsedIgnoreTables(final Collection<String> 
inUsedResourceNames, final Map<String, Collection<Class<? extends 
ShardingSphereRule>>> inUsedStorageUnits,

Reply via email to