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

panjuan 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 cf8634f  Check DiscoveryType not in use before drop it (#11274)
cf8634f is described below

commit cf8634f9964c6f43747c539033245d9062f2feb8
Author: Haoran Meng <[email protected]>
AuthorDate: Mon Jul 12 13:46:34 2021 +0800

    Check DiscoveryType not in use before drop it (#11274)
---
 .../DropDatabaseDiscoveryRuleStatementUpdater.java |  9 ++++++--
 ...pDatabaseDiscoveryRuleStatementUpdaterTest.java | 25 ++++++++++++++++++++--
 2 files changed, 30 insertions(+), 4 deletions(-)

diff --git 
a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-distsql/shardingsphere-db-discovery-distsql-handler/src/main/java/org/apache/shardingsphere/dbdiscovery/distsql/handler/update/DropDatabaseDiscoveryRuleStatementUpdater.java
 
b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-distsql/shardingsphere-db-discovery-distsql-handler/src/main/java/org/apache/shardingsphere/dbdiscovery/distsql/handler/update/DropDatabaseDisco
 [...]
index 478475c..0d8fb5c 100644
--- 
a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-distsql/shardingsphere-db-discovery-distsql-handler/src/main/java/org/apache/shardingsphere/dbdiscovery/distsql/handler/update/DropDatabaseDiscoveryRuleStatementUpdater.java
+++ 
b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-distsql/shardingsphere-db-discovery-distsql-handler/src/main/java/org/apache/shardingsphere/dbdiscovery/distsql/handler/update/DropDatabaseDiscoveryRuleStatementUpdater.java
@@ -69,8 +69,13 @@ public final class DropDatabaseDiscoveryRuleStatementUpdater 
implements RuleDefi
         Optional<DatabaseDiscoveryDataSourceRuleConfiguration> 
dataSourceRuleConfig = 
currentRuleConfig.getDataSources().stream().filter(dataSource -> 
dataSource.getName().equals(ruleName)).findAny();
         Preconditions.checkState(dataSourceRuleConfig.isPresent());
         currentRuleConfig.getDataSources().remove(dataSourceRuleConfig.get());
-        // TODO Do we need to check DiscoveryType not in use before drop it? 
-        
currentRuleConfig.getDiscoveryTypes().remove(dataSourceRuleConfig.get().getDiscoveryTypeName());
+        if (isDiscoveryTypeNotInUse(currentRuleConfig, 
dataSourceRuleConfig.get().getDiscoveryTypeName())) {
+            
currentRuleConfig.getDiscoveryTypes().remove(dataSourceRuleConfig.get().getDiscoveryTypeName());
+        }
+    }
+    
+    private boolean isDiscoveryTypeNotInUse(final 
DatabaseDiscoveryRuleConfiguration currentRuleConfig, final String 
toBeDroppedDiscoveryTypeName) {
+        return !currentRuleConfig.getDataSources().stream().filter(each -> 
each.getDiscoveryTypeName().equals(toBeDroppedDiscoveryTypeName)).findAny().isPresent();
     }
     
     @Override
diff --git 
a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-distsql/shardingsphere-db-discovery-distsql-handler/src/test/java/org/apache/shardingsphere/dbdiscovery/distsql/handler/update/DropDatabaseDiscoveryRuleStatementUpdaterTest.java
 
b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-distsql/shardingsphere-db-discovery-distsql-handler/src/test/java/org/apache/shardingsphere/dbdiscovery/distsql/handler/update/DropDatabaseD
 [...]
index 5e75d6a..b5e69c4 100644
--- 
a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-distsql/shardingsphere-db-discovery-distsql-handler/src/test/java/org/apache/shardingsphere/dbdiscovery/distsql/handler/update/DropDatabaseDiscoveryRuleStatementUpdaterTest.java
+++ 
b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-distsql/shardingsphere-db-discovery-distsql-handler/src/test/java/org/apache/shardingsphere/dbdiscovery/distsql/handler/update/DropDatabaseDiscoveryRuleStatementUpdaterTest.java
@@ -26,12 +26,17 @@ import 
org.apache.shardingsphere.infra.distsql.exception.rule.RuleDefinitionViol
 import 
org.apache.shardingsphere.infra.metadata.resource.ShardingSphereResource;
 import org.junit.Test;
 
+import java.util.Arrays;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.LinkedList;
 import java.util.Map;
 import java.util.Properties;
 
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
 import static org.mockito.Mockito.mock;
 
 public final class DropDatabaseDiscoveryRuleStatementUpdaterTest {
@@ -50,8 +55,16 @@ public final class 
DropDatabaseDiscoveryRuleStatementUpdaterTest {
     
     @Test
     public void assertUpdateCurrentRuleConfiguration() {
-        updater.updateCurrentRuleConfiguration(createSQLStatement(), 
createCurrentRuleConfiguration());
-        // TODO assert current rule configuration
+        DatabaseDiscoveryRuleConfiguration databaseDiscoveryRuleConfiguration 
= createCurrentRuleConfiguration();
+        
assertTrue(updater.updateCurrentRuleConfiguration(createSQLStatement(), 
databaseDiscoveryRuleConfiguration));
+        
assertTrue(databaseDiscoveryRuleConfiguration.getDiscoveryTypes().isEmpty());
+    }
+    
+    @Test
+    public void assertUpdateCurrentRuleConfigurationWithInUsedDiscoveryType() {
+        DatabaseDiscoveryRuleConfiguration databaseDiscoveryRuleConfiguration 
= createMultipleCurrentRuleConfigurations();
+        
assertFalse(updater.updateCurrentRuleConfiguration(createSQLStatement(), 
databaseDiscoveryRuleConfiguration));
+        
assertThat(databaseDiscoveryRuleConfiguration.getDiscoveryTypes().size(), 
is(1));
     }
     
     private DropDatabaseDiscoveryRuleStatement createSQLStatement() {
@@ -64,4 +77,12 @@ public final class 
DropDatabaseDiscoveryRuleStatementUpdaterTest {
         discoveryTypes.put("pr_ds_MGR", new 
ShardingSphereAlgorithmConfiguration("pr_ds_MGR", new Properties()));
         return new DatabaseDiscoveryRuleConfiguration(new 
LinkedList<>(Collections.singleton(dataSourceRuleConfig)), discoveryTypes);
     }
+    
+    private DatabaseDiscoveryRuleConfiguration 
createMultipleCurrentRuleConfigurations() {
+        DatabaseDiscoveryDataSourceRuleConfiguration dataSourceRuleConfig = 
new DatabaseDiscoveryDataSourceRuleConfiguration("ha_group", 
Collections.emptyList(), "pr_ds_MGR");
+        Map<String, ShardingSphereAlgorithmConfiguration> discoveryTypes = new 
HashMap<>(1, 1);
+        discoveryTypes.put("pr_ds_MGR", new 
ShardingSphereAlgorithmConfiguration("pr_ds_MGR", new Properties()));
+        return new DatabaseDiscoveryRuleConfiguration(new 
LinkedList<>(Arrays.asList(dataSourceRuleConfig, 
+                new 
DatabaseDiscoveryDataSourceRuleConfiguration("ha_group_another", 
Collections.emptyList(), "pr_ds_MGR"))), discoveryTypes);
+    }
 }

Reply via email to