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 9888697  Drop readwrite splitting rule (#10367)
9888697 is described below

commit 98886976985abe7eae41fb6d78d435e37be73bdb
Author: Haoran Meng <[email protected]>
AuthorDate: Mon May 17 19:47:35 2021 +0800

    Drop readwrite splitting rule (#10367)
    
    * Drop readwrite splitting rule
    
    * Drop readwrite splitting rule
    
    Co-authored-by: menghaoranss <[email protected]>
---
 .../src/main/antlr4/imports/RDLStatement.g4        |   4 +-
 .../distsql/parser/autogen/DistSQLStatement.g4     |   2 +-
 .../distsql/parser/core/DistSQLVisitor.java        |  21 ++--
 .../api/DistSQLStatementParserEngineTest.java      |  10 ++
 ...va => DropReadwriteSplittingRuleStatement.java} |   4 +-
 .../text/distsql/rdl/RDLBackendHandlerFactory.java |   6 +-
 .../DropReadwriteSplittingRuleBackendHandler.java  |  23 ++--
 .../text/distsql/RDLBackendHandlerFactoryTest.java |  14 +--
 ...opReadwriteSplittingRuleBackendHandlerTest.java | 117 +++++++++++++++++++++
 9 files changed, 161 insertions(+), 40 deletions(-)

diff --git 
a/shardingsphere-distsql-parser/shardingsphere-distsql-parser-engine/src/main/antlr4/imports/RDLStatement.g4
 
b/shardingsphere-distsql-parser/shardingsphere-distsql-parser-engine/src/main/antlr4/imports/RDLStatement.g4
index e3a0ed7..b5aa46a 100644
--- 
a/shardingsphere-distsql-parser/shardingsphere-distsql-parser-engine/src/main/antlr4/imports/RDLStatement.g4
+++ 
b/shardingsphere-distsql-parser/shardingsphere-distsql-parser-engine/src/main/antlr4/imports/RDLStatement.g4
@@ -175,8 +175,8 @@ columnName
     : IDENTIFIER
     ;
 
-dropReplicaQueryRule
-    : DROP REPLICA_QUERY RULE LP IDENTIFIER (COMMA IDENTIFIER)* RP
+dropReadwriteSplittingRule
+    : DROP READWRITE_SPLITTING RULE IDENTIFIER (COMMA IDENTIFIER)*
     ;
 
 dropShardingTableRule
diff --git 
a/shardingsphere-distsql-parser/shardingsphere-distsql-parser-engine/src/main/antlr4/org/apache/shardingsphere/distsql/parser/autogen/DistSQLStatement.g4
 
b/shardingsphere-distsql-parser/shardingsphere-distsql-parser-engine/src/main/antlr4/org/apache/shardingsphere/distsql/parser/autogen/DistSQLStatement.g4
index ee94ae3..724288a 100644
--- 
a/shardingsphere-distsql-parser/shardingsphere-distsql-parser-engine/src/main/antlr4/org/apache/shardingsphere/distsql/parser/autogen/DistSQLStatement.g4
+++ 
b/shardingsphere-distsql-parser/shardingsphere-distsql-parser-engine/src/main/antlr4/org/apache/shardingsphere/distsql/parser/autogen/DistSQLStatement.g4
@@ -33,7 +33,7 @@ execute
     | dropShardingBindingTableRules
     | dropShardingBroadcastTableRules
     | alterReadwriteSplittingRule
-    | dropReplicaQueryRule
+    | dropReadwriteSplittingRule
     | showResources
     | showRule
     | showScalingJobList
diff --git 
a/shardingsphere-distsql-parser/shardingsphere-distsql-parser-engine/src/main/java/org/apache/shardingsphere/distsql/parser/core/DistSQLVisitor.java
 
b/shardingsphere-distsql-parser/shardingsphere-distsql-parser-engine/src/main/java/org/apache/shardingsphere/distsql/parser/core/DistSQLVisitor.java
index 2482444..8cf3f9d 100644
--- 
a/shardingsphere-distsql-parser/shardingsphere-distsql-parser-engine/src/main/java/org/apache/shardingsphere/distsql/parser/core/DistSQLVisitor.java
+++ 
b/shardingsphere-distsql-parser/shardingsphere-distsql-parser-engine/src/main/java/org/apache/shardingsphere/distsql/parser/core/DistSQLVisitor.java
@@ -30,7 +30,6 @@ import 
org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.C
 import 
org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.CreateShardingBroadcastTableRulesContext;
 import 
org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.CreateShardingTableRuleContext;
 import 
org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.DataSourceContext;
-import 
org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.DropReplicaQueryRuleContext;
 import 
org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.DropResourceContext;
 import 
org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.DropScalingJobContext;
 import 
org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.FunctionDefinitionContext;
@@ -66,7 +65,7 @@ import 
org.apache.shardingsphere.distsql.parser.statement.rdl.create.impl.Create
 import 
org.apache.shardingsphere.distsql.parser.statement.rdl.create.impl.CreateShardingBindingTableRulesStatement;
 import 
org.apache.shardingsphere.distsql.parser.statement.rdl.create.impl.CreateShardingBroadcastTableRulesStatement;
 import 
org.apache.shardingsphere.distsql.parser.statement.rdl.create.impl.CreateShardingTableRuleStatement;
-import 
org.apache.shardingsphere.distsql.parser.statement.rdl.drop.impl.DropReplicaQueryRuleStatement;
+import 
org.apache.shardingsphere.distsql.parser.statement.rdl.drop.impl.DropReadwriteSplittingRuleStatement;
 import 
org.apache.shardingsphere.distsql.parser.statement.rdl.drop.impl.DropResourceStatement;
 import 
org.apache.shardingsphere.distsql.parser.statement.rdl.drop.impl.DropShardingBindingTableRulesStatement;
 import 
org.apache.shardingsphere.distsql.parser.statement.rdl.drop.impl.DropShardingBroadcastTableRulesStatement;
@@ -216,15 +215,6 @@ public final class DistSQLVisitor extends 
DistSQLStatementBaseVisitor<ASTNode> {
         return new 
AlterReadwriteSplittingRuleStatement(ctx.readwriteSplittingRuleDefinition()
                 .stream().map(each -> (ReadwriteSplittingRuleSegment) 
visit(each)).collect(Collectors.toList()));
     }
-    
-    @Override
-    public ASTNode visitDropReplicaQueryRule(final DropReplicaQueryRuleContext 
ctx) {
-        DropReplicaQueryRuleStatement result = new 
DropReplicaQueryRuleStatement();
-        for (TerminalNode each : ctx.IDENTIFIER()) {
-            result.getRuleNames().add(each.getText());
-        }
-        return result;
-    }
 
     @Override
     public ASTNode visitDropShardingTableRule(final 
DistSQLStatementParser.DropShardingTableRuleContext ctx) {
@@ -285,7 +275,14 @@ public final class DistSQLVisitor extends 
DistSQLStatementBaseVisitor<ASTNode> {
     public ASTNode visitTableName(final TableNameContext ctx) {
         return new TableNameSegment(ctx.getStart().getStartIndex(), 
ctx.getStop().getStopIndex(), new IdentifierValue(ctx.getText()));
     }
-    
+
+    @Override
+    public ASTNode visitDropReadwriteSplittingRule(final 
DistSQLStatementParser.DropReadwriteSplittingRuleContext ctx) {
+        DropReadwriteSplittingRuleStatement result = new 
DropReadwriteSplittingRuleStatement();
+        
result.getRuleNames().addAll(ctx.IDENTIFIER().stream().map(TerminalNode::getText).collect(Collectors.toList()));
+        return result;
+    }
+
     @Override
     public ASTNode visitShowShardingRule(final ShowShardingRuleContext ctx) {
         if (null != ctx.schemaName()) {
diff --git 
a/shardingsphere-distsql-parser/shardingsphere-distsql-parser-engine/src/test/java/org/apache/shardingsphere/distsql/parser/api/DistSQLStatementParserEngineTest.java
 
b/shardingsphere-distsql-parser/shardingsphere-distsql-parser-engine/src/test/java/org/apache/shardingsphere/distsql/parser/api/DistSQLStatementParserEngineTest.java
index 30fefd5..89ae130 100644
--- 
a/shardingsphere-distsql-parser/shardingsphere-distsql-parser-engine/src/test/java/org/apache/shardingsphere/distsql/parser/api/DistSQLStatementParserEngineTest.java
+++ 
b/shardingsphere-distsql-parser/shardingsphere-distsql-parser-engine/src/test/java/org/apache/shardingsphere/distsql/parser/api/DistSQLStatementParserEngineTest.java
@@ -29,6 +29,7 @@ import 
org.apache.shardingsphere.distsql.parser.statement.rdl.create.impl.Create
 import 
org.apache.shardingsphere.distsql.parser.statement.rdl.create.impl.CreateShardingBindingTableRulesStatement;
 import 
org.apache.shardingsphere.distsql.parser.statement.rdl.create.impl.CreateShardingBroadcastTableRulesStatement;
 import 
org.apache.shardingsphere.distsql.parser.statement.rdl.create.impl.CreateShardingTableRuleStatement;
+import 
org.apache.shardingsphere.distsql.parser.statement.rdl.drop.impl.DropReadwriteSplittingRuleStatement;
 import 
org.apache.shardingsphere.distsql.parser.statement.rdl.drop.impl.DropResourceStatement;
 import 
org.apache.shardingsphere.distsql.parser.statement.rdl.drop.impl.DropShardingBindingTableRulesStatement;
 import 
org.apache.shardingsphere.distsql.parser.statement.rdl.drop.impl.DropShardingBroadcastTableRulesStatement;
@@ -104,6 +105,8 @@ public final class DistSQLStatementParserEngineTest {
             + "TYPE(NAME=random)"
             + ")";
 
+    private static final String RDL_DROP_READWRITE_SPLITTING_RULE = "DROP 
READWRITE_SPLITTING RULE ms_group_0,ms_group_1";
+
     private final DistSQLStatementParserEngine engine = new 
DistSQLStatementParserEngine();
     
     @Test
@@ -270,4 +273,11 @@ public final class DistSQLStatementParserEngineTest {
         SQLStatement sqlStatement = 
engine.parse(RDL_ALTER_READWRITE_SPLITTING_RULE);
         assertTrue(sqlStatement instanceof 
AlterReadwriteSplittingRuleStatement);
     }
+
+    @Test
+    public void assertParseDropReadwriteSplittingRule() {
+        SQLStatement sqlStatement = 
engine.parse(RDL_DROP_READWRITE_SPLITTING_RULE);
+        assertTrue(sqlStatement instanceof 
DropReadwriteSplittingRuleStatement);
+        assertThat(((DropReadwriteSplittingRuleStatement) 
sqlStatement).getRuleNames(), is(Arrays.asList("ms_group_0", "ms_group_1")));
+    }
 }
diff --git 
a/shardingsphere-distsql-parser/shardingsphere-distsql-parser-statement/src/main/java/org/apache/shardingsphere/distsql/parser/statement/rdl/drop/impl/DropReplicaQueryRuleStatement.java
 
b/shardingsphere-distsql-parser/shardingsphere-distsql-parser-statement/src/main/java/org/apache/shardingsphere/distsql/parser/statement/rdl/drop/impl/DropReadwriteSplittingRuleStatement.java
similarity index 90%
rename from 
shardingsphere-distsql-parser/shardingsphere-distsql-parser-statement/src/main/java/org/apache/shardingsphere/distsql/parser/statement/rdl/drop/impl/DropReplicaQueryRuleStatement.java
rename to 
shardingsphere-distsql-parser/shardingsphere-distsql-parser-statement/src/main/java/org/apache/shardingsphere/distsql/parser/statement/rdl/drop/impl/DropReadwriteSplittingRuleStatement.java
index 5b7f4b0..6854814 100644
--- 
a/shardingsphere-distsql-parser/shardingsphere-distsql-parser-statement/src/main/java/org/apache/shardingsphere/distsql/parser/statement/rdl/drop/impl/DropReplicaQueryRuleStatement.java
+++ 
b/shardingsphere-distsql-parser/shardingsphere-distsql-parser-statement/src/main/java/org/apache/shardingsphere/distsql/parser/statement/rdl/drop/impl/DropReadwriteSplittingRuleStatement.java
@@ -24,10 +24,10 @@ import java.util.Collection;
 import java.util.LinkedList;
 
 /**
- * Drop replica query rule statement.
+ * Drop readwrite splitting rule statement.
  */
 @Getter
-public final class DropReplicaQueryRuleStatement extends DropRDLStatement {
+public final class DropReadwriteSplittingRuleStatement extends 
DropRDLStatement {
     
     private final Collection<String> ruleNames = new LinkedList<>();
 }
diff --git 
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/RDLBackendHandlerFactory.java
 
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/RDLBackendHandlerFactory.java
index 683257c..a4081e5 100644
--- 
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/RDLBackendHandlerFactory.java
+++ 
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/RDLBackendHandlerFactory.java
@@ -28,7 +28,7 @@ import 
org.apache.shardingsphere.distsql.parser.statement.rdl.create.impl.Create
 import 
org.apache.shardingsphere.distsql.parser.statement.rdl.create.impl.CreateShardingBindingTableRulesStatement;
 import 
org.apache.shardingsphere.distsql.parser.statement.rdl.create.impl.CreateShardingBroadcastTableRulesStatement;
 import 
org.apache.shardingsphere.distsql.parser.statement.rdl.create.impl.CreateShardingTableRuleStatement;
-import 
org.apache.shardingsphere.distsql.parser.statement.rdl.drop.impl.DropReplicaQueryRuleStatement;
+import 
org.apache.shardingsphere.distsql.parser.statement.rdl.drop.impl.DropReadwriteSplittingRuleStatement;
 import 
org.apache.shardingsphere.distsql.parser.statement.rdl.drop.impl.DropResourceStatement;
 import 
org.apache.shardingsphere.distsql.parser.statement.rdl.drop.impl.DropShardingBindingTableRulesStatement;
 import 
org.apache.shardingsphere.distsql.parser.statement.rdl.drop.impl.DropShardingBroadcastTableRulesStatement;
@@ -127,8 +127,8 @@ public final class RDLBackendHandlerFactory {
         if (sqlStatement instanceof AlterReadwriteSplittingRuleStatement) {
             return Optional.of(new 
AlterReadwriteSplittingRuleBackendHandler((AlterReadwriteSplittingRuleStatement)
 sqlStatement, backendConnection));
         }
-        if (sqlStatement instanceof DropReplicaQueryRuleStatement) {
-            return Optional.of(new 
DropReadwriteSplittingRuleBackendHandler((DropReplicaQueryRuleStatement) 
sqlStatement, backendConnection));
+        if (sqlStatement instanceof DropReadwriteSplittingRuleStatement) {
+            return Optional.of(new 
DropReadwriteSplittingRuleBackendHandler((DropReadwriteSplittingRuleStatement) 
sqlStatement, backendConnection));
         }
         if (sqlStatement instanceof DropDatabaseStatement) {
             return Optional.of(new 
DropDatabaseBackendHandler((DropDatabaseStatement) sqlStatement, 
backendConnection));
diff --git 
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/impl/DropReadwriteSplittingRuleBackendHandler.java
 
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/impl/DropReadwriteSplittingRuleBackendHandler.java
index 9227b45..2d4ff8a 100644
--- 
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/impl/DropReadwriteSplittingRuleBackendHandler.java
+++ 
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/impl/DropReadwriteSplittingRuleBackendHandler.java
@@ -17,7 +17,7 @@
 
 package org.apache.shardingsphere.proxy.backend.text.distsql.rdl.impl;
 
-import 
org.apache.shardingsphere.distsql.parser.statement.rdl.drop.impl.DropReplicaQueryRuleStatement;
+import 
org.apache.shardingsphere.distsql.parser.statement.rdl.drop.impl.DropReadwriteSplittingRuleStatement;
 import 
org.apache.shardingsphere.governance.core.registry.listener.event.rule.RuleConfigurationsAlteredEvent;
 import org.apache.shardingsphere.infra.config.RuleConfiguration;
 import org.apache.shardingsphere.infra.eventbus.ShardingSphereEventBus;
@@ -39,16 +39,16 @@ import java.util.Optional;
 import java.util.stream.Collectors;
 
 /**
- * Drop readwrite-splitting rule backend handler.
+ * Drop readwrite splitting rule backend handler.
  */
-public final class DropReadwriteSplittingRuleBackendHandler extends 
SchemaRequiredBackendHandler<DropReplicaQueryRuleStatement> {
+public final class DropReadwriteSplittingRuleBackendHandler extends 
SchemaRequiredBackendHandler<DropReadwriteSplittingRuleStatement> {
 
-    public DropReadwriteSplittingRuleBackendHandler(final 
DropReplicaQueryRuleStatement sqlStatement, final BackendConnection 
backendConnection) {
+    public DropReadwriteSplittingRuleBackendHandler(final 
DropReadwriteSplittingRuleStatement sqlStatement, final BackendConnection 
backendConnection) {
         super(sqlStatement, backendConnection);
     }
     
     @Override
-    public ResponseHeader execute(final String schemaName, final 
DropReplicaQueryRuleStatement sqlStatement) {
+    public ResponseHeader execute(final String schemaName, final 
DropReadwriteSplittingRuleStatement sqlStatement) {
         Collection<String> ruleNames = sqlStatement.getRuleNames();
         Optional<ReadwriteSplittingRuleConfiguration> ruleConfig = 
ProxyContext.getInstance().getMetaData(schemaName).getRuleMetaData().getConfigurations().stream()
                 .filter(each -> each instanceof 
ReadwriteSplittingRuleConfiguration).map(each -> 
(ReadwriteSplittingRuleConfiguration) each).findFirst();
@@ -62,8 +62,9 @@ public final class DropReadwriteSplittingRuleBackendHandler 
extends SchemaRequir
         if (!yamlConfig.isPresent()) {
             throw new ReadwriteSplittingRuleNotExistedException();
         }
-        Collection<RuleConfiguration> rules = drop(yamlConfig.get(), 
ruleNames);
-        post(schemaName, rules);
+        drop(yamlConfig.get(), ruleNames);
+        post(schemaName, new YamlRuleConfigurationSwapperEngine()
+                
.swapToRuleConfigurations(yamlConfig.get().getDataSources().isEmpty() ? 
Collections.emptyList() : Collections.singletonList(yamlConfig.get())));
         return new UpdateResponseHeader(sqlStatement);
     }
     
@@ -75,15 +76,11 @@ public final class DropReadwriteSplittingRuleBackendHandler 
extends SchemaRequir
         }
     }
 
-    private Collection<RuleConfiguration> drop(final 
YamlReadwriteSplittingRuleConfiguration yamlConfig, final Collection<String> 
ruleNames) {
+    private void drop(final YamlReadwriteSplittingRuleConfiguration 
yamlConfig, final Collection<String> ruleNames) {
         for (String each : ruleNames) {
+            
yamlConfig.getLoadBalancers().remove(yamlConfig.getDataSources().get(each).getLoadBalancerName());
             yamlConfig.getDataSources().remove(each);
         }
-        if (yamlConfig.getDataSources().isEmpty()) {
-            return new 
YamlRuleConfigurationSwapperEngine().swapToRuleConfigurations(Collections.emptyList());
-        } else {
-            return new 
YamlRuleConfigurationSwapperEngine().swapToRuleConfigurations(Collections.singleton(yamlConfig));
-        }
     }
     
     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/RDLBackendHandlerFactoryTest.java
 
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/RDLBackendHandlerFactoryTest.java
index 9ccd3f9..a70712a 100644
--- 
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/RDLBackendHandlerFactoryTest.java
+++ 
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/RDLBackendHandlerFactoryTest.java
@@ -22,7 +22,7 @@ import 
org.apache.shardingsphere.distsql.parser.statement.rdl.alter.AlterReadwri
 import 
org.apache.shardingsphere.distsql.parser.statement.rdl.create.impl.AddResourceStatement;
 import 
org.apache.shardingsphere.distsql.parser.statement.rdl.create.impl.CreateReadwriteSplittingRuleStatement;
 import 
org.apache.shardingsphere.distsql.parser.statement.rdl.create.impl.CreateShardingTableRuleStatement;
-import 
org.apache.shardingsphere.distsql.parser.statement.rdl.drop.impl.DropReplicaQueryRuleStatement;
+import 
org.apache.shardingsphere.distsql.parser.statement.rdl.drop.impl.DropReadwriteSplittingRuleStatement;
 import 
org.apache.shardingsphere.distsql.parser.statement.rdl.drop.impl.DropResourceStatement;
 import 
org.apache.shardingsphere.distsql.parser.statement.rql.show.ShowResourcesStatement;
 import 
org.apache.shardingsphere.infra.config.properties.ConfigurationProperties;
@@ -213,23 +213,23 @@ public final class RDLBackendHandlerFactoryTest {
     }
 
     @Test(expected = ReadwriteSplittingRuleNotExistedException.class)
-    public void assertExecuteDropReplicaQueryRuleContext() throws SQLException 
{
+    public void assertExecuteDropReadwriteSplittingRuleContext() throws 
SQLException {
         BackendConnection connection = mock(BackendConnection.class);
         when(connection.getSchemaName()).thenReturn("schema");
         try {
-            RDLBackendHandlerFactory.newInstance(new MySQLDatabaseType(), 
mock(DropReplicaQueryRuleStatement.class), connection);
+            RDLBackendHandlerFactory.newInstance(new MySQLDatabaseType(), 
mock(DropReadwriteSplittingRuleStatement.class), connection);
         } catch (final SQLException ex) {
-            assertThat(ex.getMessage(), is("No Registry center to execute 
`DropReplicaQueryRuleStatement` SQL"));
+            assertThat(ex.getMessage(), is("No Registry center to execute 
`DropReadwriteSplittingRuleStatement` SQL"));
         }
         setGovernanceMetaDataContexts(true);
-        Optional<TextProtocolBackendHandler> rdlBackendHandler = 
RDLBackendHandlerFactory.newInstance(new MySQLDatabaseType(), 
mock(DropReplicaQueryRuleStatement.class), connection);
+        Optional<TextProtocolBackendHandler> rdlBackendHandler = 
RDLBackendHandlerFactory.newInstance(new MySQLDatabaseType(), 
mock(DropReadwriteSplittingRuleStatement.class), connection);
         assertTrue(rdlBackendHandler.isPresent());
         ResponseHeader response = rdlBackendHandler.get().execute();
         assertThat(response, instanceOf(UpdateResponseHeader.class));
     }
 
     @Test
-    public void assertExecuteCreateReplicaQueryRuleContext() throws 
SQLException {
+    public void assertExecuteCreateReadwriteSplittingRuleContext() throws 
SQLException {
         BackendConnection connection = mock(BackendConnection.class);
         when(connection.getSchemaName()).thenReturn("schema");
         try {
@@ -245,7 +245,7 @@ public final class RDLBackendHandlerFactoryTest {
     }
 
     @Test(expected = ReadwriteSplittingRuleNotExistedException.class)
-    public void assertExecuteAlterReplicaQueryRuleContext() throws 
SQLException {
+    public void assertExecuteAlterReadwriteSplittingRuleContext() throws 
SQLException {
         BackendConnection connection = mock(BackendConnection.class);
         when(connection.getSchemaName()).thenReturn("schema");
         try {
diff --git 
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/impl/DropReadwriteSplittingRuleBackendHandlerTest.java
 
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/impl/DropReadwriteSplittingRuleBackendHandlerTest.java
new file mode 100644
index 0000000..4d425dd
--- /dev/null
+++ 
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/impl/DropReadwriteSplittingRuleBackendHandlerTest.java
@@ -0,0 +1,117 @@
+/*
+ * 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.proxy.backend.text.distsql.rdl.impl;
+
+import com.google.common.collect.Maps;
+import 
org.apache.shardingsphere.distsql.parser.statement.rdl.drop.impl.DropReadwriteSplittingRuleStatement;
+import 
org.apache.shardingsphere.infra.config.algorithm.ShardingSphereAlgorithmConfiguration;
+import org.apache.shardingsphere.infra.context.metadata.MetaDataContexts;
+import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
+import 
org.apache.shardingsphere.infra.metadata.rule.ShardingSphereRuleMetaData;
+import org.apache.shardingsphere.infra.spi.ShardingSphereServiceLoader;
+import 
org.apache.shardingsphere.proxy.backend.communication.jdbc.connection.BackendConnection;
+import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
+import 
org.apache.shardingsphere.proxy.backend.exception.ReadwriteSplittingRuleDataSourcesNotExistedException;
+import 
org.apache.shardingsphere.proxy.backend.exception.ReadwriteSplittingRuleNotExistedException;
+import org.apache.shardingsphere.proxy.backend.response.header.ResponseHeader;
+import 
org.apache.shardingsphere.proxy.backend.response.header.update.UpdateResponseHeader;
+import 
org.apache.shardingsphere.readwritesplitting.api.ReadwriteSplittingRuleConfiguration;
+import 
org.apache.shardingsphere.readwritesplitting.api.rule.ReadwriteSplittingDataSourceRuleConfiguration;
+import 
org.apache.shardingsphere.readwritesplitting.spi.ReplicaLoadBalanceAlgorithm;
+import org.apache.shardingsphere.transaction.context.TransactionContexts;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnitRunner;
+
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.when;
+
+@RunWith(MockitoJUnitRunner.class)
+public final class DropReadwriteSplittingRuleBackendHandlerTest {
+    
+    @Mock
+    private BackendConnection backendConnection;
+    
+    @Mock
+    private DropReadwriteSplittingRuleStatement sqlStatement;
+    
+    @Mock
+    private MetaDataContexts metaDataContexts;
+    
+    @Mock
+    private TransactionContexts transactionContexts;
+    
+    @Mock
+    private ShardingSphereMetaData shardingSphereMetaData;
+    
+    @Mock
+    private ShardingSphereRuleMetaData ruleMetaData;
+
+    @Mock
+    private ReadwriteSplittingDataSourceRuleConfiguration 
readwriteSplittingDataSourceRuleConfiguration;
+
+    @Mock
+    private ShardingSphereAlgorithmConfiguration 
shardingSphereAlgorithmConfiguration;
+    
+    private DropReadwriteSplittingRuleBackendHandler handler = new 
DropReadwriteSplittingRuleBackendHandler(sqlStatement, backendConnection);
+    
+    @Before
+    public void setUp() {
+        
ShardingSphereServiceLoader.register(ReplicaLoadBalanceAlgorithm.class);
+        ProxyContext.getInstance().init(metaDataContexts, transactionContexts);
+        
when(metaDataContexts.getAllSchemaNames()).thenReturn(Collections.singletonList("test"));
+        
when(metaDataContexts.getMetaData(eq("test"))).thenReturn(shardingSphereMetaData);
+        
when(shardingSphereMetaData.getRuleMetaData()).thenReturn(ruleMetaData);
+    }
+    
+    @Test
+    public void assertExecute() {
+        
when(sqlStatement.getRuleNames()).thenReturn(Collections.singletonList("pr_ds"));
+        Map<String, ShardingSphereAlgorithmConfiguration> loadBalancers = new 
HashMap<>(1, 1);
+        loadBalancers.put("pr_ds", shardingSphereAlgorithmConfiguration);
+        when(ruleMetaData.getConfigurations()).thenReturn(Collections
+                .singletonList(new 
ReadwriteSplittingRuleConfiguration(Collections
+                        
.singleton(readwriteSplittingDataSourceRuleConfiguration), loadBalancers)));
+        
when(readwriteSplittingDataSourceRuleConfiguration.getName()).thenReturn("pr_ds");
+        
when(readwriteSplittingDataSourceRuleConfiguration.getLoadBalancerName()).thenReturn("pr_ds");
+        ResponseHeader responseHeader = handler.execute("test", sqlStatement);
+        assertNotNull(responseHeader);
+        assertTrue(responseHeader instanceof UpdateResponseHeader);
+    }
+    
+    @Test(expected = ReadwriteSplittingRuleNotExistedException.class)
+    public void assertExecuteWithNotExistReadwriteSplittingRule() {
+        
when(ruleMetaData.getConfigurations()).thenReturn(Collections.emptyList());
+        handler.execute("test", sqlStatement);
+    }
+
+    @Test(expected = 
ReadwriteSplittingRuleDataSourcesNotExistedException.class)
+    public void assertExecuteWithNoDroppedReadwriteSplittingRuleDataSources() {
+        
when(sqlStatement.getRuleNames()).thenReturn(Collections.singletonList("pr_ds"));
+        
when(ruleMetaData.getConfigurations()).thenReturn(Collections.singletonList(new 
ReadwriteSplittingRuleConfiguration(Collections.emptyList(), 
Maps.newHashMap())));
+        handler.execute("test", sqlStatement);
+    }
+}

Reply via email to