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 2022/11/21 06:37:32 UTC

[shardingsphere] branch master updated: Fix #22300 (#22310)

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 524cde7953c Fix #22300 (#22310)
524cde7953c is described below

commit 524cde7953c036c2554d5efde4041567f461c482
Author: Raigor <ra...@gmail.com>
AuthorDate: Mon Nov 21 14:37:25 2022 +0800

    Fix #22300 (#22310)
---
 .../update/DropShadowRuleStatementUpdater.java     | 35 ++++++++---------
 .../update/DropShadowRuleStatementUpdaterTest.java | 44 ++++++++++------------
 2 files changed, 35 insertions(+), 44 deletions(-)

diff --git a/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/update/DropShadowRuleStatementUpdater.java b/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/update/DropShadowRuleStatementUpdater.java
index c6004efe301..69765dc3412 100644
--- a/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/update/DropShadowRuleStatementUpdater.java
+++ b/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/update/DropShadowRuleStatementUpdater.java
@@ -20,45 +20,42 @@ package org.apache.shardingsphere.shadow.distsql.handler.update;
 import org.apache.shardingsphere.infra.distsql.exception.rule.MissingRequiredRuleException;
 import org.apache.shardingsphere.infra.distsql.update.RuleDefinitionDropUpdater;
 import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
+import org.apache.shardingsphere.infra.util.exception.ShardingSpherePreconditions;
 import org.apache.shardingsphere.shadow.api.config.ShadowRuleConfiguration;
+import org.apache.shardingsphere.shadow.api.config.datasource.ShadowDataSourceConfiguration;
 import org.apache.shardingsphere.shadow.distsql.handler.checker.ShadowRuleStatementChecker;
 import org.apache.shardingsphere.shadow.distsql.parser.statement.DropShadowRuleStatement;
 
 import java.util.Collection;
-import java.util.LinkedList;
+import java.util.stream.Collectors;
 
 /**
  * Drop shadow rule statement updater.
  */
 public final class DropShadowRuleStatementUpdater implements RuleDefinitionDropUpdater<DropShadowRuleStatement, ShadowRuleConfiguration> {
     
-    private static final String SHADOW = "shadow";
-    
     @Override
-    public void checkSQLStatement(final ShardingSphereDatabase database,
-                                  final DropShadowRuleStatement sqlStatement, final ShadowRuleConfiguration currentRuleConfig) {
+    public void checkSQLStatement(final ShardingSphereDatabase database, final DropShadowRuleStatement sqlStatement, final ShadowRuleConfiguration currentRuleConfig) {
         if (sqlStatement.isIfExists() && !isExistRuleConfig(currentRuleConfig)) {
             return;
         }
-        checkConfigurationExist(database.getName(), currentRuleConfig);
-        checkRuleNames(database.getName(), sqlStatement, currentRuleConfig);
+        checkConfigurationExisted(database.getName(), currentRuleConfig);
+        checkRuleExisted(database.getName(), sqlStatement, currentRuleConfig);
     }
     
-    private void checkConfigurationExist(final String databaseName, final ShadowRuleConfiguration currentRuleConfig) {
-        ShadowRuleStatementChecker.checkConfigurationExist(databaseName, currentRuleConfig);
+    private void checkConfigurationExisted(final String databaseName, final ShadowRuleConfiguration currentRuleConfig) {
+        ShardingSpherePreconditions.checkNotNull(currentRuleConfig, () -> new MissingRequiredRuleException("Shadow", databaseName));
     }
     
-    private void checkRuleNames(final String databaseName, final DropShadowRuleStatement sqlStatement, final ShadowRuleConfiguration currentRuleConfig) {
+    private void checkRuleExisted(final String databaseName, final DropShadowRuleStatement sqlStatement, final ShadowRuleConfiguration currentRuleConfig) {
         if (!sqlStatement.isIfExists()) {
             ShadowRuleStatementChecker.checkRulesExist(sqlStatement.getNames(), getDataSourceNames(currentRuleConfig),
-                    different -> new MissingRequiredRuleException(SHADOW, databaseName, different));
+                    notExistedRuleNames -> new MissingRequiredRuleException("Shadow", databaseName, notExistedRuleNames));
         }
     }
     
-    private Collection<String> getDataSourceNames(final ShadowRuleConfiguration shadowRuleConfiguration) {
-        Collection<String> result = new LinkedList<>();
-        shadowRuleConfiguration.getDataSources().forEach(each -> result.add(each.getName()));
-        return result;
+    private Collection<String> getDataSourceNames(final ShadowRuleConfiguration currentRuleConfig) {
+        return currentRuleConfig.getDataSources().stream().map(ShadowDataSourceConfiguration::getName).collect(Collectors.toList());
     }
     
     @Override
@@ -68,10 +65,10 @@ public final class DropShadowRuleStatementUpdater implements RuleDefinitionDropU
     
     @Override
     public boolean updateCurrentRuleConfiguration(final DropShadowRuleStatement sqlStatement, final ShadowRuleConfiguration currentRuleConfig) {
-        Collection<String> ruleNames = sqlStatement.getNames();
-        ruleNames.forEach(each -> currentRuleConfig.getDataSources().remove(each));
-        currentRuleConfig.getTables().forEach((key, value) -> value.getDataSourceNames().removeIf(ruleNames::contains));
-        return false;
+        currentRuleConfig.getDataSources().removeIf(each -> sqlStatement.getNames().contains(each.getName()));
+        currentRuleConfig.getTables().forEach((key, value) -> value.getDataSourceNames().removeIf(sqlStatement.getNames()::contains));
+        currentRuleConfig.getTables().entrySet().removeIf(entry -> entry.getValue().getDataSourceNames().isEmpty());
+        return currentRuleConfig.getDataSources().isEmpty() || currentRuleConfig.getTables().isEmpty();
     }
     
     @Override
diff --git a/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/update/DropShadowRuleStatementUpdaterTest.java b/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/update/DropShadowRuleStatementUpdaterTest.java
index 0bc49cf68e6..43320c0e54d 100644
--- a/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/update/DropShadowRuleStatementUpdaterTest.java
+++ b/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/update/DropShadowRuleStatementUpdaterTest.java
@@ -24,7 +24,6 @@ import org.apache.shardingsphere.shadow.api.config.datasource.ShadowDataSourceCo
 import org.apache.shardingsphere.shadow.api.config.table.ShadowTableConfiguration;
 import org.apache.shardingsphere.shadow.distsql.handler.update.DropShadowRuleStatementUpdater;
 import org.apache.shardingsphere.shadow.distsql.parser.statement.DropShadowRuleStatement;
-import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.mockito.Answers;
@@ -35,9 +34,8 @@ import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collections;
 
-import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
 import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
 
 @RunWith(MockitoJUnitRunner.class)
 public final class DropShadowRuleStatementUpdaterTest {
@@ -45,40 +43,32 @@ public final class DropShadowRuleStatementUpdaterTest {
     @Mock(answer = Answers.RETURNS_DEEP_STUBS)
     private ShardingSphereDatabase database;
     
-    @Mock
-    private ShadowRuleConfiguration currentConfig;
-    
-    private final DropShadowRuleStatementUpdater updater = new DropShadowRuleStatementUpdater();
-    
-    @Before
-    public void before() {
-        when(currentConfig.getDataSources()).thenReturn(Collections.singletonList(new ShadowDataSourceConfiguration("initRuleName", "ds", "ds_shadow")));
+    @Test(expected = MissingRequiredRuleException.class)
+    public void assertCheckWithNullConfiguration() {
+        new DropShadowRuleStatementUpdater().checkSQLStatement(database, createSQLStatement("anyRuleName"), null);
     }
     
     @Test(expected = MissingRequiredRuleException.class)
-    public void assertExecuteWithoutRuleNameInMetaData() {
-        updater.checkSQLStatement(database, createSQLStatement("ruleSegment"), null);
+    public void assertCheckWithRuleNotExisted() {
+        new DropShadowRuleStatementUpdater().checkSQLStatement(database, createSQLStatement("notExistedRuleName"), mock(ShadowRuleConfiguration.class));
     }
     
     @Test
-    public void assertExecuteWithIfExists() {
-        DropShadowRuleStatement sqlStatement = createSQLStatement(true, "ruleSegment");
-        updater.checkSQLStatement(database, sqlStatement, mock(ShadowRuleConfiguration.class));
+    public void assertCheckWithIfExists() {
+        new DropShadowRuleStatementUpdater().checkSQLStatement(database, createSQLStatement(true, "notExistedRuleName"), mock(ShadowRuleConfiguration.class));
     }
     
     @Test
     public void assertUpdate() {
-        DropShadowRuleStatement sqlStatement = createSQLStatement(true, "ds_0");
+        DropShadowRuleStatement sqlStatement = createSQLStatement("shadow_group");
         ShadowRuleConfiguration ruleConfig = new ShadowRuleConfiguration();
-        ruleConfig.getTables().put("t_order", new ShadowTableConfiguration(new ArrayList<>(Collections.singleton("ds_0")), Collections.emptyList()));
+        ruleConfig.getDataSources().add(createShadowDataSourceConfiguration("shadow_group"));
+        ruleConfig.getTables().put("t_order", new ShadowTableConfiguration(new ArrayList<>(Collections.singleton("shadow_group")), Collections.emptyList()));
+        DropShadowRuleStatementUpdater updater = new DropShadowRuleStatementUpdater();
         updater.checkSQLStatement(database, sqlStatement, ruleConfig);
-        updater.updateCurrentRuleConfiguration(sqlStatement, ruleConfig);
-        assertFalse(ruleConfig.getTables().containsKey("ds_0"));
-    }
-    
-    @Test
-    public void assertExecuteSuccess() {
-        updater.checkSQLStatement(database, createSQLStatement("initRuleName"), currentConfig);
+        assertTrue(updater.updateCurrentRuleConfiguration(sqlStatement, ruleConfig));
+        assertTrue(ruleConfig.getDataSources().isEmpty());
+        assertTrue(ruleConfig.getTables().isEmpty());
     }
     
     private DropShadowRuleStatement createSQLStatement(final String... ruleName) {
@@ -88,4 +78,8 @@ public final class DropShadowRuleStatementUpdaterTest {
     private DropShadowRuleStatement createSQLStatement(final boolean ifExists, final String... ruleName) {
         return new DropShadowRuleStatement(ifExists, Arrays.asList(ruleName));
     }
+    
+    private ShadowDataSourceConfiguration createShadowDataSourceConfiguration(final String ruleName) {
+        return new ShadowDataSourceConfiguration(ruleName, "production", "shadow");
+    }
 }