You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by GitBox <gi...@apache.org> on 2022/01/11 08:00:59 UTC

[GitHub] [shardingsphere] lanchengx opened a new pull request #14685: [DistSQL] Add check for resource in set readwrite splitting read resource syntax

lanchengx opened a new pull request #14685:
URL: https://github.com/apache/shardingsphere/pull/14685


   Fixed https://github.com/apache/shardingsphere/issues/14503.
   
   Changes proposed in this pull request:
   - Add check for resource in set readwrite splitting read resource syntax
   - Fix the show readwrite splitting rules syntax to display static configuration, the disabled database is not removed
   
   ![image](https://user-images.githubusercontent.com/52209337/148903422-3fa3aafd-1158-4246-a14a-d9189ef57bc9.png)
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@shardingsphere.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [shardingsphere] codecov-commenter commented on pull request #14685: [DistSQL] Add check for resource in set readwrite splitting read resource syntax

Posted by GitBox <gi...@apache.org>.
codecov-commenter commented on pull request #14685:
URL: https://github.com/apache/shardingsphere/pull/14685#issuecomment-1010611243


   # [Codecov](https://codecov.io/gh/apache/shardingsphere/pull/14685?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation) Report
   > Merging [#14685](https://codecov.io/gh/apache/shardingsphere/pull/14685?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation) (4e34982) into [master](https://codecov.io/gh/apache/shardingsphere/commit/46a79680d1ae70638f53798433b5be4636c59ca6?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation) (46a7968) will **decrease** coverage by `0.11%`.
   > The diff coverage is `14.56%`.
   
   [![Impacted file tree graph](https://codecov.io/gh/apache/shardingsphere/pull/14685/graphs/tree.svg?width=650&height=150&src=pr&token=ZvlXpWa7so&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation)](https://codecov.io/gh/apache/shardingsphere/pull/14685?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation)
   
   ```diff
   @@             Coverage Diff              @@
   ##             master   #14685      +/-   ##
   ============================================
   - Coverage     59.71%   59.60%   -0.12%     
   - Complexity     1826     1836      +10     
   ============================================
     Files          3044     3050       +6     
     Lines         45367    45651     +284     
     Branches       7673     7724      +51     
   ============================================
   + Hits          27092    27210     +118     
   - Misses        16163    16320     +157     
   - Partials       2112     2121       +9     
   ```
   
   
   | [Impacted Files](https://codecov.io/gh/apache/shardingsphere/pull/14685?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation) | Coverage Δ | |
   |---|---|---|
   | [...litting/rule/ReadwriteSplittingDataSourceRule.java](https://codecov.io/gh/apache/shardingsphere/pull/14685/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-c2hhcmRpbmdzcGhlcmUtZmVhdHVyZXMvc2hhcmRpbmdzcGhlcmUtcmVhZHdyaXRlLXNwbGl0dGluZy9zaGFyZGluZ3NwaGVyZS1yZWFkd3JpdGUtc3BsaXR0aW5nLWNvcmUvc3JjL21haW4vamF2YS9vcmcvYXBhY2hlL3NoYXJkaW5nc3BoZXJlL3JlYWR3cml0ZXNwbGl0dGluZy9ydWxlL1JlYWR3cml0ZVNwbGl0dGluZ0RhdGFTb3VyY2VSdWxlLmphdmE=) | `58.13% <0.00%> (-9.43%)` | :arrow_down: |
   | [...eadwritesplitting/rule/ReadwriteSplittingRule.java](https://codecov.io/gh/apache/shardingsphere/pull/14685/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-c2hhcmRpbmdzcGhlcmUtZmVhdHVyZXMvc2hhcmRpbmdzcGhlcmUtcmVhZHdyaXRlLXNwbGl0dGluZy9zaGFyZGluZ3NwaGVyZS1yZWFkd3JpdGUtc3BsaXR0aW5nLWNvcmUvc3JjL21haW4vamF2YS9vcmcvYXBhY2hlL3NoYXJkaW5nc3BoZXJlL3JlYWR3cml0ZXNwbGl0dGluZy9ydWxlL1JlYWR3cml0ZVNwbGl0dGluZ1J1bGUuamF2YQ==) | `58.92% <0.00%> (-16.08%)` | :arrow_down: |
   | [...t/excutor/SetReadwriteSplittingStatusExecutor.java](https://codecov.io/gh/apache/shardingsphere/pull/14685/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-c2hhcmRpbmdzcGhlcmUtcHJveHkvc2hhcmRpbmdzcGhlcmUtcHJveHktYmFja2VuZC9zcmMvbWFpbi9qYXZhL29yZy9hcGFjaGUvc2hhcmRpbmdzcGhlcmUvcHJveHkvYmFja2VuZC90ZXh0L2Rpc3RzcWwvcmFsL2NvbW1vbi9zZXQvZXhjdXRvci9TZXRSZWFkd3JpdGVTcGxpdHRpbmdTdGF0dXNFeGVjdXRvci5qYXZh) | `0.00% <0.00%> (ø)` | |
   | [...er/query/ReadwriteSplittingRuleQueryResultSet.java](https://codecov.io/gh/apache/shardingsphere/pull/14685/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-c2hhcmRpbmdzcGhlcmUtZmVhdHVyZXMvc2hhcmRpbmdzcGhlcmUtcmVhZHdyaXRlLXNwbGl0dGluZy9zaGFyZGluZ3NwaGVyZS1yZWFkd3JpdGUtc3BsaXR0aW5nLWRpc3RzcWwvc2hhcmRpbmdzcGhlcmUtcmVhZHdyaXRlLXNwbGl0dGluZy1kaXN0c3FsLWhhbmRsZXIvc3JjL21haW4vamF2YS9vcmcvYXBhY2hlL3NoYXJkaW5nc3BoZXJlL3JlYWR3cml0ZXNwbGl0dGluZy9kaXN0c3FsL2hhbmRsZXIvcXVlcnkvUmVhZHdyaXRlU3BsaXR0aW5nUnVsZVF1ZXJ5UmVzdWx0U2V0LmphdmE=) | `87.09% <93.75%> (+3.76%)` | :arrow_up: |
   | [...r/ShardingSphereAlgorithmBeanDefinitionParser.java](https://codecov.io/gh/apache/shardingsphere/pull/14685/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-c2hhcmRpbmdzcGhlcmUtamRiYy9zaGFyZGluZ3NwaGVyZS1qZGJjLXNwcmluZy9zaGFyZGluZ3NwaGVyZS1qZGJjLXNwcmluZy1pbmZyYS9zaGFyZGluZ3NwaGVyZS1qZGJjLXNwcmluZy1uYW1lc3BhY2UtaW5mcmEvc3JjL21haW4vamF2YS9vcmcvYXBhY2hlL3NoYXJkaW5nc3BoZXJlL3NwcmluZy9uYW1lc3BhY2UvcGFyc2VyL1NoYXJkaW5nU3BoZXJlQWxnb3JpdGhtQmVhbkRlZmluaXRpb25QYXJzZXIuamF2YQ==) | `26.66% <0.00%> (-73.34%)` | :arrow_down: |
   | [...ta/pipeline/mysql/ingest/MySQLInventoryDumper.java](https://codecov.io/gh/apache/shardingsphere/pull/14685/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-c2hhcmRpbmdzcGhlcmUta2VybmVsL3NoYXJkaW5nc3BoZXJlLWRhdGEtcGlwZWxpbmUvc2hhcmRpbmdzcGhlcmUtZGF0YS1waXBlbGluZS1kaWFsZWN0L3NoYXJkaW5nc3BoZXJlLWRhdGEtcGlwZWxpbmUtbXlzcWwvc3JjL21haW4vamF2YS9vcmcvYXBhY2hlL3NoYXJkaW5nc3BoZXJlL2RhdGEvcGlwZWxpbmUvbXlzcWwvaW5nZXN0L015U1FMSW52ZW50b3J5RHVtcGVyLmphdmE=) | `75.00% <0.00%> (-16.67%)` | :arrow_down: |
   | [...y/impl/driver/jdbc/type/memory/JDBCRowsLoader.java](https://codecov.io/gh/apache/shardingsphere/pull/14685/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-c2hhcmRpbmdzcGhlcmUtaW5mcmEvc2hhcmRpbmdzcGhlcmUtaW5mcmEtZXhlY3V0b3Ivc3JjL21haW4vamF2YS9vcmcvYXBhY2hlL3NoYXJkaW5nc3BoZXJlL2luZnJhL2V4ZWN1dG9yL3NxbC9leGVjdXRlL3Jlc3VsdC9xdWVyeS9pbXBsL2RyaXZlci9qZGJjL3R5cGUvbWVtb3J5L0pEQkNSb3dzTG9hZGVyLmphdmE=) | `88.57% <0.00%> (-8.21%)` | :arrow_down: |
   | [...dingsphere/infra/instance/ComputeNodeInstance.java](https://codecov.io/gh/apache/shardingsphere/pull/14685/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-c2hhcmRpbmdzcGhlcmUtaW5mcmEvc2hhcmRpbmdzcGhlcmUtaW5mcmEtY29tbW9uL3NyYy9tYWluL2phdmEvb3JnL2FwYWNoZS9zaGFyZGluZ3NwaGVyZS9pbmZyYS9pbnN0YW5jZS9Db21wdXRlTm9kZUluc3RhbmNlLmphdmE=) | `0.00% <0.00%> (ø)` | |
   | [...ata/persist/service/ComputeNodePersistService.java](https://codecov.io/gh/apache/shardingsphere/pull/14685/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-c2hhcmRpbmdzcGhlcmUtbW9kZS9zaGFyZGluZ3NwaGVyZS1tb2RlLWNvcmUvc3JjL21haW4vamF2YS9vcmcvYXBhY2hlL3NoYXJkaW5nc3BoZXJlL21vZGUvbWV0YWRhdGEvcGVyc2lzdC9zZXJ2aWNlL0NvbXB1dGVOb2RlUGVyc2lzdFNlcnZpY2UuamF2YQ==) | `0.00% <0.00%> (ø)` | |
   | [...persist/service/impl/GlobalRulePersistService.java](https://codecov.io/gh/apache/shardingsphere/pull/14685/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-c2hhcmRpbmdzcGhlcmUtbW9kZS9zaGFyZGluZ3NwaGVyZS1tb2RlLWNvcmUvc3JjL21haW4vamF2YS9vcmcvYXBhY2hlL3NoYXJkaW5nc3BoZXJlL21vZGUvbWV0YWRhdGEvcGVyc2lzdC9zZXJ2aWNlL2ltcGwvR2xvYmFsUnVsZVBlcnNpc3RTZXJ2aWNlLmphdmE=) | `0.00% <0.00%> (ø)` | |
   | ... and [29 more](https://codecov.io/gh/apache/shardingsphere/pull/14685/diff?src=pr&el=tree-more&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation) | |
   
   ------
   
   [Continue to review full report at Codecov](https://codecov.io/gh/apache/shardingsphere/pull/14685?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation).
   > **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation)
   > `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
   > Powered by [Codecov](https://codecov.io/gh/apache/shardingsphere/pull/14685?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation). Last update [46a7968...4e34982](https://codecov.io/gh/apache/shardingsphere/pull/14685?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation).
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@shardingsphere.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [shardingsphere] RaigorJiang commented on a change in pull request #14685: [DistSQL] Add check for resource in set readwrite splitting read resource syntax

Posted by GitBox <gi...@apache.org>.
RaigorJiang commented on a change in pull request #14685:
URL: https://github.com/apache/shardingsphere/pull/14685#discussion_r782278388



##########
File path: shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/common/set/excutor/SetReadwriteSplittingStatusExecutor.java
##########
@@ -18,45 +18,164 @@
 package org.apache.shardingsphere.proxy.backend.text.distsql.ral.common.set.excutor;
 
 import lombok.AllArgsConstructor;
+import org.apache.shardingsphere.infra.distsql.constant.ExportableConstants;
 import org.apache.shardingsphere.infra.distsql.exception.DistSQLException;
 import org.apache.shardingsphere.infra.distsql.exception.resource.RequiredResourceMissedException;
 import org.apache.shardingsphere.infra.eventbus.ShardingSphereEventBus;
 import org.apache.shardingsphere.infra.exception.SchemaNotExistedException;
 import org.apache.shardingsphere.infra.rule.event.impl.DataSourceDisabledEvent;
+import org.apache.shardingsphere.infra.rule.identifier.type.ExportableRule;
+import org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.status.storage.StorageNodeStatus;
+import org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.status.storage.node.StorageStatusNode;
+import org.apache.shardingsphere.mode.metadata.persist.MetaDataPersistService;
 import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
 import org.apache.shardingsphere.proxy.backend.exception.NoDatabaseSelectedException;
 import org.apache.shardingsphere.proxy.backend.response.header.ResponseHeader;
 import org.apache.shardingsphere.proxy.backend.response.header.update.UpdateResponseHeader;
 import org.apache.shardingsphere.proxy.backend.session.ConnectionSession;
 import org.apache.shardingsphere.proxy.backend.text.distsql.ral.common.set.SetStatementExecutor;
 import org.apache.shardingsphere.readwritesplitting.distsql.parser.statement.status.SetReadwriteSplittingStatusStatement;
+import org.apache.shardingsphere.readwritesplitting.rule.ReadwriteSplittingRule;
 
+import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Collection;
 import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Optional;
+import java.util.Set;
+import java.util.stream.Collectors;
 
 /**
  * Set readwrite-splitting status executor.
  */
 @AllArgsConstructor
 public final class SetReadwriteSplittingStatusExecutor implements SetStatementExecutor {
     
+    private static final String DISABLE = "DISABLE";
+    
     private final SetReadwriteSplittingStatusStatement sqlStatement;
     
     private final ConnectionSession connectionSession;
     
     @Override
     public ResponseHeader execute() throws DistSQLException {
         String schemaName = sqlStatement.getSchema().isPresent() ? sqlStatement.getSchema().get().getIdentifier().getValue() : connectionSession.getSchemaName();
+        String resourceName = sqlStatement.getResourceName();
+        checkSchema(schemaName);
+        boolean isDisable = DISABLE.equals(sqlStatement.getStatus());
+        if (isDisable) {
+            checkDisablingIsValid(schemaName, resourceName);
+        } else {
+            checkEnablingIsValid(schemaName, resourceName);
+        }
+        ShardingSphereEventBus.getInstance().post(new DataSourceDisabledEvent(schemaName, resourceName, isDisable));
+        return new UpdateResponseHeader(sqlStatement);
+    }
+    
+    private void checkSchema(final String schemaName) {
         if (null == schemaName) {
             throw new NoDatabaseSelectedException();
         }
         if (!ProxyContext.getInstance().getAllSchemaNames().contains(schemaName)) {
             throw new SchemaNotExistedException(schemaName);
         }
-        String resourceName = sqlStatement.getResourceName();
-        Collection<String> notExistedResources = ProxyContext.getInstance().getMetaData(schemaName).getResource().getNotExistedResources(Collections.singleton(resourceName));
-        DistSQLException.predictionThrow(notExistedResources.isEmpty(), new RequiredResourceMissedException(schemaName, Collections.singleton(resourceName)));
-        ShardingSphereEventBus.getInstance().post(new DataSourceDisabledEvent(schemaName, resourceName, "DISABLE".equals(sqlStatement.getStatus())));
-        return new UpdateResponseHeader(sqlStatement);
+    }
+    
+    private void checkEnablingIsValid(final String schemaName, final String toBeEnabledResource) throws DistSQLException {
+        checkResourceExists(schemaName, toBeEnabledResource);
+        Collection<String> disabledResources = getDisabledResources(schemaName);
+        if (!disabledResources.contains(toBeEnabledResource)) {
+            throw new UnsupportedOperationException(String.format("`%s` is not disabled", toBeEnabledResource));
+        }
+    }
+    
+    private void checkDisablingIsValid(final String schemaName, final String toBeDisabledResource) throws DistSQLException {
+        checkResourceExists(schemaName, toBeDisabledResource);
+        Collection<String> disabledResources = getDisabledResources(schemaName);
+        if (disabledResources.contains(toBeDisabledResource)) {
+            throw new UnsupportedOperationException(String.format("`%s` has been disabled", toBeDisabledResource));
+        }
+        Map<String, Map<String, String>> readwriteSplittingRules = getExportedReadwriteSplittingRules(schemaName);
+        Map<String, String> primaryResources = new HashMap<>();
+        Map<String, String> replicaResources = new HashMap<>();
+        readwriteSplittingRules.entrySet().stream().filter(entry -> !entry.getValue().isEmpty())
+                .peek(entry -> addPrimaryResource(primaryResources, entry)).forEach(entry -> addReplicaResource(replicaResources, entry));
+        if (primaryResources.containsKey(toBeDisabledResource)) {
+            throw new UnsupportedOperationException(String.format("`%s` is the primary data source in the `%s` rule, cannot be disabled", 
+                    toBeDisabledResource, primaryResources.get(toBeDisabledResource)));
+        }
+        if (!replicaResources.containsKey(toBeDisabledResource)) {
+            throw new UnsupportedOperationException(String.format("`%s` is not used by any readwrite-splitting rules, cannot be disabled", toBeDisabledResource));
+        }
+        Set<String> canBeDisabledResources = getCanBeDisabledResources(replicaResources, disabledResources);
+        if (!canBeDisabledResources.contains(toBeDisabledResource)) {
+            throw new UnsupportedOperationException(String.format("`%s` is the last resource in `%s`, cannot be disabled", toBeDisabledResource, replicaResources.get(toBeDisabledResource)));

Review comment:
       last resource or last read resource?




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@shardingsphere.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [shardingsphere] RaigorJiang commented on a change in pull request #14685: [DistSQL] Add check for resource in set readwrite splitting read resource syntax

Posted by GitBox <gi...@apache.org>.
RaigorJiang commented on a change in pull request #14685:
URL: https://github.com/apache/shardingsphere/pull/14685#discussion_r782277782



##########
File path: shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/common/set/excutor/SetReadwriteSplittingStatusExecutor.java
##########
@@ -18,45 +18,164 @@
 package org.apache.shardingsphere.proxy.backend.text.distsql.ral.common.set.excutor;
 
 import lombok.AllArgsConstructor;
+import org.apache.shardingsphere.infra.distsql.constant.ExportableConstants;
 import org.apache.shardingsphere.infra.distsql.exception.DistSQLException;
 import org.apache.shardingsphere.infra.distsql.exception.resource.RequiredResourceMissedException;
 import org.apache.shardingsphere.infra.eventbus.ShardingSphereEventBus;
 import org.apache.shardingsphere.infra.exception.SchemaNotExistedException;
 import org.apache.shardingsphere.infra.rule.event.impl.DataSourceDisabledEvent;
+import org.apache.shardingsphere.infra.rule.identifier.type.ExportableRule;
+import org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.status.storage.StorageNodeStatus;
+import org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.status.storage.node.StorageStatusNode;
+import org.apache.shardingsphere.mode.metadata.persist.MetaDataPersistService;
 import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
 import org.apache.shardingsphere.proxy.backend.exception.NoDatabaseSelectedException;
 import org.apache.shardingsphere.proxy.backend.response.header.ResponseHeader;
 import org.apache.shardingsphere.proxy.backend.response.header.update.UpdateResponseHeader;
 import org.apache.shardingsphere.proxy.backend.session.ConnectionSession;
 import org.apache.shardingsphere.proxy.backend.text.distsql.ral.common.set.SetStatementExecutor;
 import org.apache.shardingsphere.readwritesplitting.distsql.parser.statement.status.SetReadwriteSplittingStatusStatement;
+import org.apache.shardingsphere.readwritesplitting.rule.ReadwriteSplittingRule;
 
+import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Collection;
 import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Optional;
+import java.util.Set;
+import java.util.stream.Collectors;
 
 /**
  * Set readwrite-splitting status executor.
  */
 @AllArgsConstructor
 public final class SetReadwriteSplittingStatusExecutor implements SetStatementExecutor {
     
+    private static final String DISABLE = "DISABLE";
+    
     private final SetReadwriteSplittingStatusStatement sqlStatement;
     
     private final ConnectionSession connectionSession;
     
     @Override
     public ResponseHeader execute() throws DistSQLException {
         String schemaName = sqlStatement.getSchema().isPresent() ? sqlStatement.getSchema().get().getIdentifier().getValue() : connectionSession.getSchemaName();
+        String resourceName = sqlStatement.getResourceName();
+        checkSchema(schemaName);
+        boolean isDisable = DISABLE.equals(sqlStatement.getStatus());
+        if (isDisable) {
+            checkDisablingIsValid(schemaName, resourceName);
+        } else {
+            checkEnablingIsValid(schemaName, resourceName);
+        }
+        ShardingSphereEventBus.getInstance().post(new DataSourceDisabledEvent(schemaName, resourceName, isDisable));
+        return new UpdateResponseHeader(sqlStatement);
+    }
+    
+    private void checkSchema(final String schemaName) {
         if (null == schemaName) {
             throw new NoDatabaseSelectedException();
         }
         if (!ProxyContext.getInstance().getAllSchemaNames().contains(schemaName)) {
             throw new SchemaNotExistedException(schemaName);
         }
-        String resourceName = sqlStatement.getResourceName();
-        Collection<String> notExistedResources = ProxyContext.getInstance().getMetaData(schemaName).getResource().getNotExistedResources(Collections.singleton(resourceName));
-        DistSQLException.predictionThrow(notExistedResources.isEmpty(), new RequiredResourceMissedException(schemaName, Collections.singleton(resourceName)));
-        ShardingSphereEventBus.getInstance().post(new DataSourceDisabledEvent(schemaName, resourceName, "DISABLE".equals(sqlStatement.getStatus())));
-        return new UpdateResponseHeader(sqlStatement);
+    }
+    
+    private void checkEnablingIsValid(final String schemaName, final String toBeEnabledResource) throws DistSQLException {
+        checkResourceExists(schemaName, toBeEnabledResource);
+        Collection<String> disabledResources = getDisabledResources(schemaName);
+        if (!disabledResources.contains(toBeEnabledResource)) {
+            throw new UnsupportedOperationException(String.format("`%s` is not disabled", toBeEnabledResource));
+        }
+    }
+    
+    private void checkDisablingIsValid(final String schemaName, final String toBeDisabledResource) throws DistSQLException {
+        checkResourceExists(schemaName, toBeDisabledResource);
+        Collection<String> disabledResources = getDisabledResources(schemaName);
+        if (disabledResources.contains(toBeDisabledResource)) {
+            throw new UnsupportedOperationException(String.format("`%s` has been disabled", toBeDisabledResource));
+        }
+        Map<String, Map<String, String>> readwriteSplittingRules = getExportedReadwriteSplittingRules(schemaName);
+        Map<String, String> primaryResources = new HashMap<>();
+        Map<String, String> replicaResources = new HashMap<>();
+        readwriteSplittingRules.entrySet().stream().filter(entry -> !entry.getValue().isEmpty())
+                .peek(entry -> addPrimaryResource(primaryResources, entry)).forEach(entry -> addReplicaResource(replicaResources, entry));
+        if (primaryResources.containsKey(toBeDisabledResource)) {
+            throw new UnsupportedOperationException(String.format("`%s` is the primary data source in the `%s` rule, cannot be disabled", 
+                    toBeDisabledResource, primaryResources.get(toBeDisabledResource)));
+        }
+        if (!replicaResources.containsKey(toBeDisabledResource)) {
+            throw new UnsupportedOperationException(String.format("`%s` is not used by any readwrite-splitting rules, cannot be disabled", toBeDisabledResource));

Review comment:
       any readwrite-splitting `rule`




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@shardingsphere.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [shardingsphere] RaigorJiang merged pull request #14685: [DistSQL] Add check for resource in set readwrite splitting read resource syntax

Posted by GitBox <gi...@apache.org>.
RaigorJiang merged pull request #14685:
URL: https://github.com/apache/shardingsphere/pull/14685


   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@shardingsphere.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org