You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by zh...@apache.org on 2022/01/18 02:55:05 UTC

[shardingsphere] branch master updated: Add DistSQL syntax `SHOW SHARDING SCALING RULES`. (#14842)

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

zhonghongsheng 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 1ea2392  Add DistSQL syntax `SHOW SHARDING SCALING RULES`. (#14842)
1ea2392 is described below

commit 1ea2392eb4057a52a30d9f0975311d3e7eb6ad0a
Author: Raigor <ra...@gmail.com>
AuthorDate: Tue Jan 18 10:54:07 2022 +0800

    Add DistSQL syntax `SHOW SHARDING SCALING RULES`. (#14842)
---
 .../query/ShardingScalingRulesQueryResultSet.java  | 85 ++++++++++++++++++++++
 ...dingsphere.infra.distsql.query.DistSQLResultSet |  1 +
 .../src/main/antlr4/imports/scaling/Keyword.g4     |  8 ++
 .../scaling/RQLStatement.g4}                       | 26 ++-----
 .../distsql/parser/autogen/ScalingStatement.g4     |  3 +-
 .../parser/core/ScalingSQLStatementVisitor.java    | 18 ++++-
 .../ShowShardingScalingRulesStatement.java}        | 31 +++-----
 7 files changed, 132 insertions(+), 40 deletions(-)

diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-handler/src/main/java/org/apache/shardingsphere/scaling/distsql/handler/query/ShardingScalingRulesQueryResultSet.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-handler/src/main/java/org/apache/shardingsphere/scaling/distsql/handler/query/ShardingScalingRulesQueryResultSet.java
new file mode 100644
index 0000000..74a8541
--- /dev/null
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-handler/src/main/java/org/apache/shardingsphere/scaling/distsql/handler/query/ShardingScalingRulesQueryResultSet.java
@@ -0,0 +1,85 @@
+/*
+ * 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.scaling.distsql.handler.query;
+
+import com.google.gson.Gson;
+import org.apache.shardingsphere.infra.config.rulealtered.OnRuleAlteredActionConfiguration;
+import org.apache.shardingsphere.infra.distsql.query.DistSQLResultSet;
+import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
+import org.apache.shardingsphere.scaling.distsql.statement.ShowShardingScalingRulesStatement;
+import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration;
+import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
+
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.LinkedList;
+import java.util.Map.Entry;
+import java.util.Optional;
+
+/**
+ * Result set for show sharding scaling rules.
+ */
+public final class ShardingScalingRulesQueryResultSet implements DistSQLResultSet {
+    
+    private Iterator<Entry<String, OnRuleAlteredActionConfiguration>> data;
+    
+    @Override
+    public void init(final ShardingSphereMetaData metaData, final SQLStatement sqlStatement) {
+        Optional<ShardingRuleConfiguration> ruleConfig = metaData.getRuleMetaData().getConfigurations()
+                .stream().filter(each -> each instanceof ShardingRuleConfiguration).map(each -> (ShardingRuleConfiguration) each).findAny();
+        data = ruleConfig.map(ShardingRuleConfiguration::getScaling).orElse(Collections.emptyMap()).entrySet().iterator();
+    }
+    
+    @Override
+    public Collection<String> getColumnNames() {
+        return Arrays.asList("name", "input", "output", "stream_channel", "completion_detector", "data_consistency_checker");
+    }
+    
+    @Override
+    public boolean next() {
+        return data.hasNext();
+    }
+    
+    @Override
+    public Collection<Object> getRowData() {
+        return buildRowData(data.next());
+    }
+    
+    private Collection<Object> buildRowData(final Entry<String, OnRuleAlteredActionConfiguration> data) {
+        Collection<Object> result = new LinkedList<>();
+        result.add(data.getKey());
+        OnRuleAlteredActionConfiguration shardingScalingRule = data.getValue();
+        result.add(null == shardingScalingRule ? "" : getString(shardingScalingRule.getInput()));
+        result.add(null == shardingScalingRule ? "" : getString(shardingScalingRule.getOutput()));
+        result.add(null == shardingScalingRule ? "" : getString(shardingScalingRule.getStreamChannel()));
+        result.add(null == shardingScalingRule ? "" : getString(shardingScalingRule.getCompletionDetector()));
+        result.add(null == shardingScalingRule ? "" : getString(shardingScalingRule.getDataConsistencyChecker()));
+        return result;
+    }
+    
+    private String getString(final Object obj) {
+        return null == obj ? "" : new Gson().toJson(obj);
+    }
+    
+    @Override
+    public String getType() {
+        return ShowShardingScalingRulesStatement.class.getCanonicalName();
+    }
+}
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-handler/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.distsql.query.DistSQLResultSet b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-handler/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.distsql.query.DistSQLResultSet
index fa6322f..bffcfaa 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-handler/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.distsql.query.DistSQLResultSet
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-handler/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.distsql.query.DistSQLResultSet
@@ -22,6 +22,7 @@ org.apache.shardingsphere.sharding.distsql.handler.query.ShardingAlgorithmQueryR
 org.apache.shardingsphere.sharding.distsql.handler.query.ShardingTableNodesQueryResultSet
 org.apache.shardingsphere.sharding.distsql.handler.query.ShardingKeyGeneratorsQueryResultSet
 org.apache.shardingsphere.sharding.distsql.handler.query.DefaultShardingStrategyQueryResultSet
+org.apache.shardingsphere.scaling.distsql.handler.query.ShardingScalingRulesQueryResultSet
 org.apache.shardingsphere.scaling.distsql.handler.CheckScalingQueryResultSet
 org.apache.shardingsphere.scaling.distsql.handler.ShowScalingListQueryResultSet
 org.apache.shardingsphere.scaling.distsql.handler.ShowScalingJobStatusQueryResultSet
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-parser/src/main/antlr4/imports/scaling/Keyword.g4 b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-parser/src/main/antlr4/imports/scaling/Keyword.g4
index 26283b6..bdf36a1 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-parser/src/main/antlr4/imports/scaling/Keyword.g4
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-parser/src/main/antlr4/imports/scaling/Keyword.g4
@@ -71,6 +71,14 @@ RULE
     :  R U L E
     ;
 
+RULES
+    :  R U L E S
+    ;
+
+FROM
+    : F R O M
+    ;
+
 JOB
     : J O B
     ;
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-parser/src/main/antlr4/scaling/org/apache/shardingsphere/distsql/parser/autogen/ScalingStatement.g4 b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-parser/src/main/antlr4/imports/scaling/RQLStatement.g4
similarity index 64%
copy from shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-parser/src/main/antlr4/scaling/org/apache/shardingsphere/distsql/parser/autogen/ScalingStatement.g4
copy to shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-parser/src/main/antlr4/imports/scaling/RQLStatement.g4
index b9faac4..8ff99e9 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-parser/src/main/antlr4/scaling/org/apache/shardingsphere/distsql/parser/autogen/ScalingStatement.g4
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-parser/src/main/antlr4/imports/scaling/RQLStatement.g4
@@ -15,24 +15,14 @@
  * limitations under the License.
  */
 
-grammar ScalingStatement;
+grammar RQLStatement;
 
-import Symbol, RALStatement, RDLStatement;
+import Keyword, Literals;
 
-execute
-    : (showScalingList
-    | showScalingStatus
-    | startScaling
-    | stopScaling
-    | dropScaling
-    | resetScaling
-    | checkScaling
-    | showScalingCheckAlgorithms
-    | stopScalingSourceWriting
-    | applyScaling
-    | createShardingScalingRule
-    | dropShardingScalingRule
-    | enableShardingScalingRule
-    | disableShardingScalingRule
-    ) SEMI?
+showShardingScalingRules
+    : SHOW SHARDING SCALING RULES (FROM schemaName)?
+    ;
+
+schemaName
+    : IDENTIFIER
     ;
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-parser/src/main/antlr4/scaling/org/apache/shardingsphere/distsql/parser/autogen/ScalingStatement.g4 b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-parser/src/main/antlr4/scaling/org/apache/shardingsphere/distsql/parser/autogen/ScalingStatement.g4
index b9faac4..fdad25d 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-parser/src/main/antlr4/scaling/org/apache/shardingsphere/distsql/parser/autogen/ScalingStatement.g4
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-parser/src/main/antlr4/scaling/org/apache/shardingsphere/distsql/parser/autogen/ScalingStatement.g4
@@ -17,7 +17,7 @@
 
 grammar ScalingStatement;
 
-import Symbol, RALStatement, RDLStatement;
+import Symbol, RALStatement, RDLStatement, RQLStatement;
 
 execute
     : (showScalingList
@@ -30,6 +30,7 @@ execute
     | showScalingCheckAlgorithms
     | stopScalingSourceWriting
     | applyScaling
+    | showShardingScalingRules
     | createShardingScalingRule
     | dropShardingScalingRule
     | enableShardingScalingRule
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-parser/src/main/java/org/apache/shardingsphere/scaling/distsql/parser/core/ScalingSQLStatementVisitor.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-parser/src/main/java/org/apache/shardingsphere/scaling/distsql/parser/core/ScalingSQLStatementVisitor.java
index 657531a..5d385d5 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-parser/src/main/java/org/apache/shardingsphere/scaling/distsql/parser/core/ScalingSQLStatementVisitor.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-parser/src/main/java/org/apache/shardingsphere/scaling/distsql/parser/core/ScalingSQLStatementVisitor.java
@@ -20,8 +20,8 @@ package org.apache.shardingsphere.scaling.distsql.parser.core;
 import org.apache.shardingsphere.distsql.parser.autogen.ScalingStatementBaseVisitor;
 import org.apache.shardingsphere.distsql.parser.autogen.ScalingStatementParser;
 import org.apache.shardingsphere.distsql.parser.autogen.ScalingStatementParser.AlgorithmDefinitionContext;
-import org.apache.shardingsphere.distsql.parser.autogen.ScalingStatementParser.CheckScalingContext;
 import org.apache.shardingsphere.distsql.parser.autogen.ScalingStatementParser.ApplyScalingContext;
+import org.apache.shardingsphere.distsql.parser.autogen.ScalingStatementParser.CheckScalingContext;
 import org.apache.shardingsphere.distsql.parser.autogen.ScalingStatementParser.CompleteAutoDefinitionContext;
 import org.apache.shardingsphere.distsql.parser.autogen.ScalingStatementParser.CompletionDetectorContext;
 import org.apache.shardingsphere.distsql.parser.autogen.ScalingStatementParser.CreateShardingScalingRuleContext;
@@ -37,16 +37,18 @@ import org.apache.shardingsphere.distsql.parser.autogen.ScalingStatementParser.O
 import org.apache.shardingsphere.distsql.parser.autogen.ScalingStatementParser.RateLimiterContext;
 import org.apache.shardingsphere.distsql.parser.autogen.ScalingStatementParser.ResetScalingContext;
 import org.apache.shardingsphere.distsql.parser.autogen.ScalingStatementParser.ScalingRuleDefinitionContext;
+import org.apache.shardingsphere.distsql.parser.autogen.ScalingStatementParser.SchemaNameContext;
 import org.apache.shardingsphere.distsql.parser.autogen.ScalingStatementParser.ShowScalingCheckAlgorithmsContext;
 import org.apache.shardingsphere.distsql.parser.autogen.ScalingStatementParser.ShowScalingListContext;
 import org.apache.shardingsphere.distsql.parser.autogen.ScalingStatementParser.ShowScalingStatusContext;
+import org.apache.shardingsphere.distsql.parser.autogen.ScalingStatementParser.ShowShardingScalingRulesContext;
 import org.apache.shardingsphere.distsql.parser.autogen.ScalingStatementParser.StartScalingContext;
 import org.apache.shardingsphere.distsql.parser.autogen.ScalingStatementParser.StopScalingContext;
 import org.apache.shardingsphere.distsql.parser.autogen.ScalingStatementParser.StopScalingSourceWritingContext;
 import org.apache.shardingsphere.distsql.parser.autogen.ScalingStatementParser.StreamChannelContext;
 import org.apache.shardingsphere.distsql.parser.segment.AlgorithmSegment;
-import org.apache.shardingsphere.scaling.distsql.statement.CheckScalingStatement;
 import org.apache.shardingsphere.scaling.distsql.statement.ApplyScalingStatement;
+import org.apache.shardingsphere.scaling.distsql.statement.CheckScalingStatement;
 import org.apache.shardingsphere.scaling.distsql.statement.CreateShardingScalingRuleStatement;
 import org.apache.shardingsphere.scaling.distsql.statement.DisableShardingScalingRuleStatement;
 import org.apache.shardingsphere.scaling.distsql.statement.DropScalingStatement;
@@ -56,6 +58,7 @@ import org.apache.shardingsphere.scaling.distsql.statement.ResetScalingStatement
 import org.apache.shardingsphere.scaling.distsql.statement.ShowScalingCheckAlgorithmsStatement;
 import org.apache.shardingsphere.scaling.distsql.statement.ShowScalingListStatement;
 import org.apache.shardingsphere.scaling.distsql.statement.ShowScalingStatusStatement;
+import org.apache.shardingsphere.scaling.distsql.statement.ShowShardingScalingRulesStatement;
 import org.apache.shardingsphere.scaling.distsql.statement.StartScalingStatement;
 import org.apache.shardingsphere.scaling.distsql.statement.StopScalingSourceWritingStatement;
 import org.apache.shardingsphere.scaling.distsql.statement.StopScalingStatement;
@@ -63,6 +66,7 @@ import org.apache.shardingsphere.scaling.distsql.statement.segment.InputOrOutput
 import org.apache.shardingsphere.scaling.distsql.statement.segment.ShardingScalingRuleConfigurationSegment;
 import org.apache.shardingsphere.sql.parser.api.visitor.ASTNode;
 import org.apache.shardingsphere.sql.parser.api.visitor.SQLVisitor;
+import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.SchemaSegment;
 import org.apache.shardingsphere.sql.parser.sql.common.value.identifier.IdentifierValue;
 
 import java.util.Properties;
@@ -228,6 +232,11 @@ public final class ScalingSQLStatementVisitor extends ScalingStatementBaseVisito
     }
     
     @Override
+    public ASTNode visitShowShardingScalingRules(final ShowShardingScalingRulesContext ctx) {
+        return new ShowShardingScalingRulesStatement(null == ctx.schemaName() ? null : (SchemaSegment) visit(ctx.schemaName()));
+    }
+    
+    @Override
     public ASTNode visitAlgorithmDefinition(final AlgorithmDefinitionContext ctx) {
         return new AlgorithmSegment(ctx.algorithmName().getText(), getAlgorithmProperties(ctx));
     }
@@ -242,4 +251,9 @@ public final class ScalingSQLStatementVisitor extends ScalingStatementBaseVisito
         }
         return result;
     }
+    
+    @Override
+    public ASTNode visitSchemaName(final SchemaNameContext ctx) {
+        return new SchemaSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), new IdentifierValue(ctx.getText()));
+    }
 }
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-parser/src/main/antlr4/scaling/org/apache/shardingsphere/distsql/parser/autogen/ScalingStatement.g4 b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-statement/src/main/java/org/apache/shardingsphere/scaling/distsql/statement/ShowShardingScalingRulesStatement.java
similarity index 62%
copy from shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-parser/src/main/antlr4/scaling/org/apache/shardingsphere/distsql/parser/autogen/ScalingStatement.g4
copy to shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-statement/src/main/java/org/apache/shardingsphere/scaling/distsql/statement/ShowShardingScalingRulesStatement.java
index b9faac4..fa2c2f4 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-parser/src/main/antlr4/scaling/org/apache/shardingsphere/distsql/parser/autogen/ScalingStatement.g4
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-statement/src/main/java/org/apache/shardingsphere/scaling/distsql/statement/ShowShardingScalingRulesStatement.java
@@ -15,24 +15,17 @@
  * limitations under the License.
  */
 
-grammar ScalingStatement;
+package org.apache.shardingsphere.scaling.distsql.statement;
 
-import Symbol, RALStatement, RDLStatement;
+import org.apache.shardingsphere.distsql.parser.statement.rql.show.ShowRulesStatement;
+import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.SchemaSegment;
 
-execute
-    : (showScalingList
-    | showScalingStatus
-    | startScaling
-    | stopScaling
-    | dropScaling
-    | resetScaling
-    | checkScaling
-    | showScalingCheckAlgorithms
-    | stopScalingSourceWriting
-    | applyScaling
-    | createShardingScalingRule
-    | dropShardingScalingRule
-    | enableShardingScalingRule
-    | disableShardingScalingRule
-    ) SEMI?
-    ;
+/**
+ * Show sharding scaling rules statement.
+ */
+public final class ShowShardingScalingRulesStatement extends ShowRulesStatement {
+    
+    public ShowShardingScalingRulesStatement(final SchemaSegment schema) {
+        super(schema);
+    }
+}