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 0bf875a  Fixes sharding rules being overwrote in the registry center 
(#10155)
0bf875a is described below

commit 0bf875a589b5360154e8b14f8f7f66ef0ecb6bae
Author: Haoran Meng <[email protected]>
AuthorDate: Thu Apr 22 17:42:31 2021 +0800

    Fixes sharding rules being overwrote in the registry center (#10155)
---
 .../CreateShardingTableRuleBackendHandler.java     | 26 +++++++++++++++-------
 .../CreateShardingTableRuleBackendHandlerTest.java |  6 ++---
 2 files changed, 21 insertions(+), 11 deletions(-)

diff --git 
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/impl/CreateShardingTableRuleBackendHandler.java
 
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/impl/CreateShardingTableRuleBackendHandler.java
index 003c96f..a5253f8 100644
--- 
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/impl/CreateShardingTableRuleBackendHandler.java
+++ 
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/impl/CreateShardingTableRuleBackendHandler.java
@@ -31,7 +31,6 @@ import 
org.apache.shardingsphere.proxy.backend.response.header.update.UpdateResp
 import 
org.apache.shardingsphere.proxy.backend.text.SchemaRequiredBackendHandler;
 import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration;
 import 
org.apache.shardingsphere.sharding.converter.ShardingRuleStatementConverter;
-import 
org.apache.shardingsphere.sharding.yaml.config.YamlShardingRuleConfiguration;
 
 import java.util.ArrayList;
 import java.util.Collection;
@@ -53,9 +52,16 @@ public final class CreateShardingTableRuleBackendHandler 
extends SchemaRequiredB
     @Override
     public ResponseHeader execute(final String schemaName, final 
CreateShardingTableRuleStatement sqlStatement) {
         check(schemaName, sqlStatement);
-        YamlShardingRuleConfiguration config = 
ShardingRuleStatementConverter.convert(sqlStatement);
-        Collection<RuleConfiguration> rules = new 
YamlRuleConfigurationSwapperEngine().swapToRuleConfigurations(Collections.singleton(config));
-        post(schemaName, rules);
+        ShardingRuleConfiguration shardingRuleConfiguration = 
(ShardingRuleConfiguration) new YamlRuleConfigurationSwapperEngine()
+                
.swapToRuleConfigurations(Collections.singleton(ShardingRuleStatementConverter.convert(sqlStatement))).iterator().next();
+        Optional<ShardingRuleConfiguration> existShardingRuleConfiguration = 
getShardingRuleConfiguration(schemaName);
+        if (existShardingRuleConfiguration.isPresent()) {
+            
existShardingRuleConfiguration.get().getAutoTables().addAll(shardingRuleConfiguration.getAutoTables());
+            
existShardingRuleConfiguration.get().getShardingAlgorithms().putAll(shardingRuleConfiguration.getShardingAlgorithms());
+        } else {
+            
ProxyContext.getInstance().getMetaData(schemaName).getRuleMetaData().getConfigurations().add(shardingRuleConfiguration);
+        }
+        post(schemaName, 
ProxyContext.getInstance().getMetaData(schemaName).getRuleMetaData().getConfigurations());
         return new UpdateResponseHeader(sqlStatement);
     }
     
@@ -76,12 +82,16 @@ public final class CreateShardingTableRuleBackendHandler 
extends SchemaRequiredB
     }
     
     private Collection<String> getLogicTables(final String schemaName) {
-        Optional<ShardingRuleConfiguration> shardingRuleConfig = 
ProxyContext.getInstance().getMetaData(schemaName).getRuleMetaData().getConfigurations().stream()
-                .filter(each -> each instanceof 
ShardingRuleConfiguration).map(each -> (ShardingRuleConfiguration) 
each).findFirst();
-        if (!shardingRuleConfig.isPresent()) {
+        Optional<ShardingRuleConfiguration> shardingRuleConfiguration = 
getShardingRuleConfiguration(schemaName);
+        if (!shardingRuleConfiguration.isPresent()) {
             return Collections.EMPTY_LIST;
         }
-        return shardingRuleConfig.get().getTables().stream().map(each -> 
each.getLogicTable()).collect(Collectors.toList());
+        return shardingRuleConfiguration.get().getTables().stream().map(each 
-> each.getLogicTable()).collect(Collectors.toList());
+    }
+    
+    private Optional<ShardingRuleConfiguration> 
getShardingRuleConfiguration(final String schemaName) {
+        return 
ProxyContext.getInstance().getMetaData(schemaName).getRuleMetaData().getConfigurations().stream()
+                .filter(each -> each instanceof 
ShardingRuleConfiguration).map(each -> (ShardingRuleConfiguration) 
each).findFirst();
     }
     
     private void post(final String schemaName, final 
Collection<RuleConfiguration> rules) {
diff --git 
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/impl/CreateShardingTableRuleBackendHandlerTest.java
 
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/impl/CreateShardingTableRuleBackendHandlerTest.java
index a9f5834..f750c60 100644
--- 
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/impl/CreateShardingTableRuleBackendHandlerTest.java
+++ 
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/impl/CreateShardingTableRuleBackendHandlerTest.java
@@ -37,9 +37,9 @@ import org.junit.runner.RunWith;
 import org.mockito.Mock;
 import org.mockito.junit.MockitoJUnitRunner;
 
+import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
-import java.util.Collections;
 
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
@@ -88,7 +88,7 @@ public final class CreateShardingTableRuleBackendHandlerTest {
     public void assertExecuteWithDuplicateTablesInRDL() {
         TableRuleSegment tableRuleSegment = new TableRuleSegment();
         tableRuleSegment.setLogicTable("t_order");
-        
when(ruleMetaData.getConfigurations()).thenReturn(Collections.EMPTY_LIST);
+        when(ruleMetaData.getConfigurations()).thenReturn(new ArrayList<>());
         
when(sqlStatement.getTables()).thenReturn(Arrays.asList(tableRuleSegment, 
tableRuleSegment));
         handler.execute("test", sqlStatement);
     }
@@ -105,6 +105,6 @@ public final class 
CreateShardingTableRuleBackendHandlerTest {
     private Collection<RuleConfiguration> buildShardingConfigurations() {
         ShardingRuleConfiguration configuration = new 
ShardingRuleConfiguration();
         configuration.getTables().add(new 
ShardingTableRuleConfiguration("t_order"));
-        return Arrays.asList(configuration);
+        return new ArrayList<>(Arrays.asList(configuration));
     }
 }

Reply via email to