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/08/21 02:45:44 UTC
[shardingsphere] branch master updated: Migrate global rules (update in TrafficRule) to the shardingsphere-traffic-distsql module. (#20326)
This is an automated email from the ASF dual-hosted git repository.
zhangliang 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 831883021d0 Migrate global rules (update in TrafficRule) to the shardingsphere-traffic-distsql module. (#20326)
831883021d0 is described below
commit 831883021d08fac066c8336a057bf5c3c85a3536
Author: yx9o <ya...@163.com>
AuthorDate: Sun Aug 21 10:45:35 2022 +0800
Migrate global rules (update in TrafficRule) to the shardingsphere-traffic-distsql module. (#20326)
---
.../src/main/antlr4/imports/Keyword.g4 | 12 ----
.../src/main/antlr4/imports/RALStatement.g4 | 36 ----------
.../parser/autogen/KernelDistSQLStatement.g4 | 3 -
.../core/kernel/KernelDistSQLStatementVisitor.java | 69 --------------------
.../ral/UpdatableGlobalRuleRALStatement.java | 12 ++--
.../infra/distsql/update/GlobalRuleRALUpdater.java | 40 ++++++++++++
.../handler}/convert/TrafficRuleConverter.java | 4 +-
.../update/AlterTrafficRuleStatementUpdater.java | 70 ++++++++++----------
.../update/CreateTrafficRuleStatementUpdater.java | 70 ++++++++++----------
.../update/DropTrafficRuleStatementUpdater.java | 55 ++++++++--------
...phere.infra.distsql.update.GlobalRuleRALUpdater | 4 +-
.../fixture/DistSQLTrafficAlgorithmFixture.java | 2 +-
.../DistSQLTrafficLoadBalanceAlgorithmFixture.java | 2 +-
.../AlterTrafficRuleStatementUpdaterTest.java | 58 +++++++----------
.../CreateTrafficRuleStatementUpdaterTest.java | 55 ++++++----------
.../DropTrafficRuleStatementUpdaterTest.java | 47 +++++--------
...che.shardingsphere.traffic.spi.TrafficAlgorithm | 2 +-
...gsphere.traffic.spi.TrafficLoadBalanceAlgorithm | 2 +-
.../imports/traffic/{Keyword.g4 => BaseRule.g4} | 24 +++----
.../src/main/antlr4/imports/traffic/Keyword.g4 | 44 +++++++++++++
.../main/antlr4/imports/traffic/RALStatement.g4 | 46 ++++++++++++-
.../parser/autogen/TrafficDistSQLStatement.g4 | 6 +-
.../core/TrafficDistSQLStatementVisitor.java | 76 ++++++++++++++++++++++
.../distsql/parser/segment/TrafficRuleSegment.java | 3 +-
.../updatable/AlterTrafficRuleStatement.java | 8 +--
.../updatable/CreateTrafficRuleStatement.java | 8 +--
.../updatable/DropTrafficRuleStatement.java | 6 +-
.../distsql/ral/RALBackendHandlerFactory.java | 13 ++--
.../ral/UpdatableGlobalRuleRALBackendHandler.java | 55 ++++++++++++++++
...pdatableGlobalRuleRALBackendHandlerFactory.java | 50 ++++++++++++++
.../ral/impl/UpdatableRALStatementAssert.java | 6 +-
.../updatable/AlterTrafficRuleStatementAssert.java | 4 +-
.../CreateTrafficRuleStatementAssert.java | 4 +-
.../updatable/DropTrafficRuleStatementAssert.java | 2 +-
34 files changed, 518 insertions(+), 380 deletions(-)
diff --git a/shardingsphere-distsql/shardingsphere-distsql-parser/src/main/antlr4/imports/Keyword.g4 b/shardingsphere-distsql/shardingsphere-distsql-parser/src/main/antlr4/imports/Keyword.g4
index 8029cb86dfc..d0c065eadfd 100644
--- a/shardingsphere-distsql/shardingsphere-distsql-parser/src/main/antlr4/imports/Keyword.g4
+++ b/shardingsphere-distsql/shardingsphere-distsql-parser/src/main/antlr4/imports/Keyword.g4
@@ -239,10 +239,6 @@ COUNT
: C O U N T
;
-TRAFFIC
- : T R A F F I C
- ;
-
LABEL
: L A B E L
;
@@ -259,14 +255,6 @@ LABELS
: L A B E L S
;
-TRAFFIC_ALGORITHM
- : T R A F F I C UL_ A L G O R I T H M
- ;
-
-LOAD_BALANCER
- : L O A D UL_ B A L A N C E R
- ;
-
EXPORT
: E X P O R T
;
diff --git a/shardingsphere-distsql/shardingsphere-distsql-parser/src/main/antlr4/imports/RALStatement.g4 b/shardingsphere-distsql/shardingsphere-distsql-parser/src/main/antlr4/imports/RALStatement.g4
index 9d99f3320f8..534da574fdf 100644
--- a/shardingsphere-distsql/shardingsphere-distsql-parser/src/main/antlr4/imports/RALStatement.g4
+++ b/shardingsphere-distsql/shardingsphere-distsql-parser/src/main/antlr4/imports/RALStatement.g4
@@ -83,18 +83,6 @@ showModeInfo
: SHOW MODE INFO
;
-createTrafficRule
- : CREATE TRAFFIC RULE trafficRuleDefinition (COMMA trafficRuleDefinition)*
- ;
-
-alterTrafficRule
- : ALTER TRAFFIC RULE trafficRuleDefinition (COMMA trafficRuleDefinition)*
- ;
-
-dropTrafficRule
- : DROP TRAFFIC RULE ifExists? ruleName (COMMA ruleName)*
- ;
-
labelInstance
: (LABEL | RELABEL) INSTANCE instanceId WITH label (COMMA label)*
;
@@ -103,22 +91,6 @@ unlabelInstance
: UNLABEL INSTANCE instanceId (WITH label (COMMA label)*)?
;
-trafficRuleDefinition
- : ruleName LP (labelDefinition COMMA)? trafficAlgorithmDefinition (COMMA loadBalancerDefinition)? RP
- ;
-
-labelDefinition
- : LABELS LP label (COMMA label)* RP
- ;
-
-trafficAlgorithmDefinition
- : TRAFFIC_ALGORITHM LP algorithmDefinition RP
- ;
-
-loadBalancerDefinition
- : LOAD_BALANCER LP algorithmDefinition RP
- ;
-
exportDatabaseConfiguration
: EXPORT DATABASE (CONFIGURATION | CONFIG) (FROM databaseName)? (COMMA? FILE EQ filePath)?
;
@@ -255,10 +227,6 @@ concurrencyLevel
: INT
;
-ruleName
- : IDENTIFIER
- ;
-
label
: IDENTIFIER
;
@@ -267,10 +235,6 @@ intValue
: INT
;
-ifExists
- : IF EXISTS
- ;
-
prepareDistSQL
: PREPARE DISTSQL
;
diff --git a/shardingsphere-distsql/shardingsphere-distsql-parser/src/main/antlr4/org/apache/shardingsphere/distsql/parser/autogen/KernelDistSQLStatement.g4 b/shardingsphere-distsql/shardingsphere-distsql-parser/src/main/antlr4/org/apache/shardingsphere/distsql/parser/autogen/KernelDistSQLStatement.g4
index 9315d7b7f9b..8e2f7d44953 100644
--- a/shardingsphere-distsql/shardingsphere-distsql-parser/src/main/antlr4/org/apache/shardingsphere/distsql/parser/autogen/KernelDistSQLStatement.g4
+++ b/shardingsphere-distsql/shardingsphere-distsql-parser/src/main/antlr4/org/apache/shardingsphere/distsql/parser/autogen/KernelDistSQLStatement.g4
@@ -53,9 +53,6 @@ execute
| alterSQLParserRule
| showTransactionRule
| alterTransactionRule
- | dropTrafficRule
- | createTrafficRule
- | alterTrafficRule
| exportDatabaseConfiguration
| showRulesUsedResource
| importDatabaseConfiguration
diff --git a/shardingsphere-distsql/shardingsphere-distsql-parser/src/main/java/org/apache/shardingsphere/distsql/parser/core/kernel/KernelDistSQLStatementVisitor.java b/shardingsphere-distsql/shardingsphere-distsql-parser/src/main/java/org/apache/shardingsphere/distsql/parser/core/kernel/KernelDistSQLStatementVisitor.java
index 1e945d02085..39be4c97c96 100644
--- a/shardingsphere-distsql/shardingsphere-distsql-parser/src/main/java/org/apache/shardingsphere/distsql/parser/core/kernel/KernelDistSQLStatementVisitor.java
+++ b/shardingsphere-distsql/shardingsphere-distsql-parser/src/main/java/org/apache/shardingsphere/distsql/parser/core/kernel/KernelDistSQLStatementVisitor.java
@@ -20,13 +20,11 @@ package org.apache.shardingsphere.distsql.parser.core.kernel;
import org.antlr.v4.runtime.tree.ParseTree;
import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementBaseVisitor;
import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.AddResourceContext;
-import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.AlgorithmDefinitionContext;
import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.AlterDefaultSingleTableRuleContext;
import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.AlterInstanceContext;
import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.AlterMigrationProcessConfigurationContext;
import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.AlterResourceContext;
import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.AlterSQLParserRuleContext;
-import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.AlterTrafficRuleContext;
import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.AlterTransactionRuleContext;
import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.ApplyDistSQLContext;
import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.BatchSizeContext;
@@ -37,21 +35,17 @@ import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementPa
import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.CountSingleTableRuleContext;
import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.CreateDefaultSingleTableRuleContext;
import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.CreateMigrationProcessConfigurationContext;
-import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.CreateTrafficRuleContext;
import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.DatabaseNameContext;
import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.DisableInstanceContext;
import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.DiscardDistSQLContext;
import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.DropDefaultSingleTableRuleContext;
import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.DropResourceContext;
-import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.DropTrafficRuleContext;
import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.EnableInstanceContext;
import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.ExportDatabaseConfigurationContext;
import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.FromSegmentContext;
import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.ImportDatabaseConfigurationContext;
import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.InstanceIdContext;
-import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.LabelDefinitionContext;
import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.LabelInstanceContext;
-import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.LoadBalancerDefinitionContext;
import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.MigrationProcessConfigurationContext;
import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.PasswordContext;
import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.PrepareDistSQLContext;
@@ -81,7 +75,6 @@ import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementPa
import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.ShowVariableContext;
import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.SqlParserRuleDefinitionContext;
import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.StreamChannelContext;
-import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.TrafficRuleDefinitionContext;
import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.TransactionRuleDefinitionContext;
import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.UnlabelInstanceContext;
import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.WorkerThreadContext;
@@ -92,7 +85,6 @@ import org.apache.shardingsphere.distsql.parser.segment.DataSourceSegment;
import org.apache.shardingsphere.distsql.parser.segment.HostnameAndPortBasedDataSourceSegment;
import org.apache.shardingsphere.distsql.parser.segment.MigrationProcessConfigurationSegment;
import org.apache.shardingsphere.distsql.parser.segment.ReadOrWriteSegment;
-import org.apache.shardingsphere.distsql.parser.segment.TrafficRuleSegment;
import org.apache.shardingsphere.distsql.parser.segment.TransactionProviderSegment;
import org.apache.shardingsphere.distsql.parser.segment.URLBasedDataSourceSegment;
import org.apache.shardingsphere.distsql.parser.statement.ral.hint.ClearHintStatement;
@@ -111,13 +103,10 @@ import org.apache.shardingsphere.distsql.parser.statement.ral.queryable.ShowVari
import org.apache.shardingsphere.distsql.parser.statement.ral.updatable.AlterInstanceStatement;
import org.apache.shardingsphere.distsql.parser.statement.ral.updatable.AlterMigrationProcessConfigurationStatement;
import org.apache.shardingsphere.distsql.parser.statement.ral.updatable.AlterSQLParserRuleStatement;
-import org.apache.shardingsphere.distsql.parser.statement.ral.updatable.AlterTrafficRuleStatement;
import org.apache.shardingsphere.distsql.parser.statement.ral.updatable.AlterTransactionRuleStatement;
import org.apache.shardingsphere.distsql.parser.statement.ral.updatable.ApplyDistSQLStatement;
import org.apache.shardingsphere.distsql.parser.statement.ral.updatable.CreateMigrationProcessConfigurationStatement;
-import org.apache.shardingsphere.distsql.parser.statement.ral.updatable.CreateTrafficRuleStatement;
import org.apache.shardingsphere.distsql.parser.statement.ral.updatable.DiscardDistSQLStatement;
-import org.apache.shardingsphere.distsql.parser.statement.ral.updatable.DropTrafficRuleStatement;
import org.apache.shardingsphere.distsql.parser.statement.ral.updatable.ImportDatabaseConfigurationStatement;
import org.apache.shardingsphere.distsql.parser.statement.ral.updatable.LabelInstanceStatement;
import org.apache.shardingsphere.distsql.parser.statement.ral.updatable.PrepareDistSQLStatement;
@@ -144,7 +133,6 @@ import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.DatabaseS
import org.apache.shardingsphere.sql.parser.sql.common.value.identifier.IdentifierValue;
import java.util.Collection;
-import java.util.Collections;
import java.util.Properties;
import java.util.stream.Collectors;
@@ -349,12 +337,6 @@ public final class KernelDistSQLStatementVisitor extends KernelDistSQLStatementB
return visit(ctx.transactionRuleDefinition());
}
- @Override
- public ASTNode visitDropTrafficRule(final DropTrafficRuleContext ctx) {
- Collection<String> ruleNames = null == ctx.ruleName() ? null : ctx.ruleName().stream().map(this::getIdentifierValue).collect(Collectors.toSet());
- return new DropTrafficRuleStatement(null != ctx.ifExists(), ruleNames);
- }
-
@Override
public ASTNode visitTransactionRuleDefinition(final TransactionRuleDefinitionContext ctx) {
String defaultType = getIdentifierValue(ctx.defaultType());
@@ -395,57 +377,6 @@ public final class KernelDistSQLStatementVisitor extends KernelDistSQLStatementB
null == ctx.maximumSize() ? null : Long.parseLong(getIdentifierValue(ctx.maximumSize())));
}
- @Override
- public ASTNode visitCreateTrafficRule(final CreateTrafficRuleContext ctx) {
- return new CreateTrafficRuleStatement(ctx.trafficRuleDefinition().stream().map(each -> (TrafficRuleSegment) visit(each)).collect(Collectors.toList()));
- }
-
- @Override
- public ASTNode visitAlterTrafficRule(final AlterTrafficRuleContext ctx) {
- return new AlterTrafficRuleStatement(ctx.trafficRuleDefinition().stream().map(each -> (TrafficRuleSegment) visit(each)).collect(Collectors.toList()));
- }
-
- @Override
- public ASTNode visitTrafficRuleDefinition(final TrafficRuleDefinitionContext ctx) {
- AlgorithmSegment loadBalancerSegment = null;
- if (null != ctx.loadBalancerDefinition()) {
- loadBalancerSegment = (AlgorithmSegment) visit(ctx.loadBalancerDefinition().algorithmDefinition());
- }
- return new TrafficRuleSegment(getIdentifierValue(ctx.ruleName()), buildLabels(ctx.labelDefinition()),
- (AlgorithmSegment) visit(ctx.trafficAlgorithmDefinition().algorithmDefinition()), loadBalancerSegment);
- }
-
- @Override
- public ASTNode visitLoadBalancerDefinition(final LoadBalancerDefinitionContext ctx) {
- if (null == ctx) {
- return null;
- }
- return visit(ctx.algorithmDefinition());
- }
-
- @Override
- public ASTNode visitAlgorithmDefinition(final AlgorithmDefinitionContext ctx) {
- return new AlgorithmSegment(getIdentifierValue(ctx.algorithmTypeName()), buildProperties(ctx.propertiesDefinition()));
- }
-
- private Properties buildProperties(final PropertiesDefinitionContext ctx) {
- Properties result = new Properties();
- if (null == ctx) {
- return result;
- }
- for (PropertyContext each : ctx.properties().property()) {
- result.setProperty(IdentifierValue.getQuotedContent(each.key.getText()), IdentifierValue.getQuotedContent(each.value.getText()));
- }
- return result;
- }
-
- private Collection<String> buildLabels(final LabelDefinitionContext labelDefinition) {
- if (null == labelDefinition) {
- return Collections.emptyList();
- }
- return labelDefinition.label().stream().map(this::getIdentifierValue).collect(Collectors.toList());
- }
-
@Override
public ASTNode visitExportDatabaseConfiguration(final ExportDatabaseConfigurationContext ctx) {
return new ExportDatabaseConfigurationStatement(null == ctx.databaseName() ? null : (DatabaseSegment) visit(ctx.databaseName()), getIdentifierValue(ctx.filePath()));
diff --git a/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-distsql/shardingsphere-traffic-distsql-parser/src/main/antlr4/org/apache/shardingsphere/distsql/parser/autogen/TrafficDistSQLStatement.g4 b/shardingsphere-distsql/shardingsphere-distsql-statement/src/main/java/org/apache/shardingsphere/distsql/parser/statement/ral/UpdatableGlobalRuleRALStatement.java
similarity index 79%
copy from shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-distsql/shardingsphere-traffic-distsql-parser/src/main/antlr4/org/apache/shardingsphere/distsql/parser/autogen/TrafficDistSQLStatement.g4
copy to shardingsphere-distsql/shardingsphere-distsql-statement/src/main/java/org/apache/shardingsphere/distsql/parser/statement/ral/UpdatableGlobalRuleRALStatement.java
index d7524a3e37c..5c4b81dc31d 100644
--- a/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-distsql/shardingsphere-traffic-distsql-parser/src/main/antlr4/org/apache/shardingsphere/distsql/parser/autogen/TrafficDistSQLStatement.g4
+++ b/shardingsphere-distsql/shardingsphere-distsql-statement/src/main/java/org/apache/shardingsphere/distsql/parser/statement/ral/UpdatableGlobalRuleRALStatement.java
@@ -15,10 +15,10 @@
* limitations under the License.
*/
-grammar TrafficDistSQLStatement;
+package org.apache.shardingsphere.distsql.parser.statement.ral;
-import Symbol, RALStatement;
-
-execute
- : (showTrafficRules) SEMI?
- ;
+/**
+ * Updatable RAL statement for global rule.
+ */
+public abstract class UpdatableGlobalRuleRALStatement extends UpdatableRALStatement {
+}
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/distsql/update/GlobalRuleRALUpdater.java b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/distsql/update/GlobalRuleRALUpdater.java
new file mode 100644
index 00000000000..3333132b599
--- /dev/null
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/distsql/update/GlobalRuleRALUpdater.java
@@ -0,0 +1,40 @@
+/*
+ * 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.infra.distsql.update;
+
+import org.apache.shardingsphere.infra.distsql.exception.DistSQLException;
+import org.apache.shardingsphere.infra.metadata.database.rule.ShardingSphereRuleMetaData;
+import org.apache.shardingsphere.infra.util.spi.annotation.SingletonSPI;
+import org.apache.shardingsphere.infra.util.spi.type.typed.TypedSPI;
+import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
+
+/**
+ * RAL updater for global rule.
+ */
+@SingletonSPI
+public interface GlobalRuleRALUpdater extends TypedSPI {
+
+ /**
+ * Execute update.
+ *
+ * @param ruleMetaData rule meta data
+ * @param sqlStatement SQL statement
+ * @throws DistSQLException definition violation exception
+ */
+ void executeUpdate(ShardingSphereRuleMetaData ruleMetaData, SQLStatement sqlStatement) throws DistSQLException;
+}
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/common/convert/TrafficRuleConverter.java b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-distsql/shardingsphere-traffic-distsql-handler/src/main/java/org/apache/shardingsphere/traffic/distsql/handler/convert/TrafficRuleConverter.java
similarity index 95%
rename from shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/common/convert/TrafficRuleConverter.java
rename to shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-distsql/shardingsphere-traffic-distsql-handler/src/main/java/org/apache/shardingsphere/traffic/distsql/handler/convert/TrafficRuleConverter.java
index 817c2be0288..d5025b1dd5b 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/common/convert/TrafficRuleConverter.java
+++ b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-distsql/shardingsphere-traffic-distsql-handler/src/main/java/org/apache/shardingsphere/traffic/distsql/handler/convert/TrafficRuleConverter.java
@@ -15,13 +15,13 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.proxy.backend.handler.distsql.ral.common.convert;
+package org.apache.shardingsphere.traffic.distsql.handler.convert;
import org.apache.shardingsphere.distsql.parser.segment.AlgorithmSegment;
-import org.apache.shardingsphere.distsql.parser.segment.TrafficRuleSegment;
import org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration;
import org.apache.shardingsphere.traffic.api.config.TrafficRuleConfiguration;
import org.apache.shardingsphere.traffic.api.config.TrafficStrategyConfiguration;
+import org.apache.shardingsphere.traffic.distsql.parser.segment.TrafficRuleSegment;
import java.util.Collection;
import java.util.Optional;
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/AlterTrafficRuleHandler.java b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-distsql/shardingsphere-traffic-distsql-handler/src/main/java/org/apache/shardingsphere/traffic/distsql/handler/update/AlterTrafficRuleStatementUpdater.java
similarity index 69%
rename from shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/AlterTrafficRuleHandler.java
rename to shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-distsql/shardingsphere-traffic-distsql-handler/src/main/java/org/apache/shardingsphere/traffic/distsql/handler/update/AlterTrafficRuleStatementUpdater.java
index a143572c7c4..703cfd05621 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/AlterTrafficRuleHandler.java
+++ b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-distsql/shardingsphere-traffic-distsql-handler/src/main/java/org/apache/shardingsphere/traffic/distsql/handler/update/AlterTrafficRuleStatementUpdater.java
@@ -15,22 +15,21 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.proxy.backend.handler.distsql.ral.updatable;
+package org.apache.shardingsphere.traffic.distsql.handler.update;
-import org.apache.shardingsphere.distsql.parser.segment.TrafficRuleSegment;
-import org.apache.shardingsphere.distsql.parser.statement.ral.updatable.AlterTrafficRuleStatement;
import org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration;
import org.apache.shardingsphere.infra.distsql.exception.DistSQLException;
import org.apache.shardingsphere.infra.distsql.exception.rule.InvalidAlgorithmConfigurationException;
import org.apache.shardingsphere.infra.distsql.exception.rule.RequiredRuleMissedException;
+import org.apache.shardingsphere.infra.distsql.update.GlobalRuleRALUpdater;
+import org.apache.shardingsphere.infra.metadata.database.rule.ShardingSphereRuleMetaData;
import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
-import org.apache.shardingsphere.mode.manager.ContextManager;
-import org.apache.shardingsphere.mode.metadata.MetaDataContexts;
-import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
-import org.apache.shardingsphere.proxy.backend.handler.distsql.ral.UpdatableRALBackendHandler;
-import org.apache.shardingsphere.proxy.backend.handler.distsql.ral.common.convert.TrafficRuleConverter;
+import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
import org.apache.shardingsphere.traffic.api.config.TrafficRuleConfiguration;
import org.apache.shardingsphere.traffic.api.config.TrafficStrategyConfiguration;
+import org.apache.shardingsphere.traffic.distsql.handler.convert.TrafficRuleConverter;
+import org.apache.shardingsphere.traffic.distsql.parser.segment.TrafficRuleSegment;
+import org.apache.shardingsphere.traffic.distsql.parser.statement.updatable.AlterTrafficRuleStatement;
import org.apache.shardingsphere.traffic.factory.TrafficAlgorithmFactory;
import org.apache.shardingsphere.traffic.factory.TrafficLoadBalanceAlgorithmFactory;
import org.apache.shardingsphere.traffic.rule.TrafficRule;
@@ -43,41 +42,41 @@ import java.util.Map;
import java.util.stream.Collectors;
/**
- * Alter traffic rule handler.
+ * Alter traffic rule statement updater.
*/
-public final class AlterTrafficRuleHandler extends UpdatableRALBackendHandler<AlterTrafficRuleStatement> {
+public final class AlterTrafficRuleStatementUpdater implements GlobalRuleRALUpdater {
@Override
- protected void update(final ContextManager contextManager) throws DistSQLException {
- check();
- replaceNewRule();
- persistNewRuleConfigurations();
+ public void executeUpdate(final ShardingSphereRuleMetaData ruleMetaData, final SQLStatement sqlStatement) throws DistSQLException {
+ AlterTrafficRuleStatement statement = (AlterTrafficRuleStatement) sqlStatement;
+ check(ruleMetaData, statement);
+ replaceNewRule(ruleMetaData, statement);
}
- private void check() throws DistSQLException {
- checkRuleNames();
- checkAlgorithmNames();
+ private void check(final ShardingSphereRuleMetaData ruleMetaData, final AlterTrafficRuleStatement sqlStatement) throws DistSQLException {
+ checkRuleNames(ruleMetaData, sqlStatement);
+ checkAlgorithmNames(sqlStatement);
}
- private void checkRuleNames() throws DistSQLException {
- Collection<String> notExistRuleNames = getNotExistRuleNames();
+ private void checkRuleNames(final ShardingSphereRuleMetaData ruleMetaData, final AlterTrafficRuleStatement sqlStatement) throws DistSQLException {
+ Collection<String> notExistRuleNames = getNotExistRuleNames(ruleMetaData, sqlStatement);
DistSQLException.predictionThrow(notExistRuleNames.isEmpty(), () -> new RequiredRuleMissedException("Traffic", notExistRuleNames));
}
- private Collection<String> getNotExistRuleNames() {
- TrafficRule currentRule = ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData().getGlobalRuleMetaData().getSingleRule(TrafficRule.class);
+ private Collection<String> getNotExistRuleNames(final ShardingSphereRuleMetaData ruleMetaData, final AlterTrafficRuleStatement sqlStatement) {
+ TrafficRule currentRule = ruleMetaData.getSingleRule(TrafficRule.class);
Collection<String> currentRuleNames = currentRule.getStrategyRules().stream().map(TrafficStrategyRule::getName).collect(Collectors.toSet());
- return getSqlStatement().getSegments().stream().map(TrafficRuleSegment::getName).filter(each -> !currentRuleNames.contains(each)).collect(Collectors.toSet());
+ return sqlStatement.getSegments().stream().map(TrafficRuleSegment::getName).filter(each -> !currentRuleNames.contains(each)).collect(Collectors.toSet());
}
- private void checkAlgorithmNames() throws DistSQLException {
- Collection<String> invalidAlgorithmNames = getInvalidAlgorithmNames();
+ private void checkAlgorithmNames(final AlterTrafficRuleStatement sqlStatement) throws DistSQLException {
+ Collection<String> invalidAlgorithmNames = getInvalidAlgorithmNames(sqlStatement);
DistSQLException.predictionThrow(invalidAlgorithmNames.isEmpty(), () -> new InvalidAlgorithmConfigurationException("Traffic", invalidAlgorithmNames));
}
- private Collection<String> getInvalidAlgorithmNames() {
+ private Collection<String> getInvalidAlgorithmNames(final AlterTrafficRuleStatement sqlStatement) {
Collection<String> result = new LinkedList<>();
- for (TrafficRuleSegment each : getSqlStatement().getSegments()) {
+ for (TrafficRuleSegment each : sqlStatement.getSegments()) {
if (!TrafficAlgorithmFactory.contains(each.getAlgorithm().getName())) {
result.add(each.getAlgorithm().getName());
}
@@ -88,19 +87,18 @@ public final class AlterTrafficRuleHandler extends UpdatableRALBackendHandler<Al
return result;
}
- private void replaceNewRule() {
- TrafficRuleConfiguration toBeAlteredRuleConfig = createToBeAlteredRuleConfiguration();
- Collection<ShardingSphereRule> globalRules = ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData().getGlobalRuleMetaData().getRules();
+ private void replaceNewRule(final ShardingSphereRuleMetaData ruleMetaData, final AlterTrafficRuleStatement sqlStatement) {
+ TrafficRuleConfiguration toBeAlteredRuleConfig = createToBeAlteredRuleConfiguration(ruleMetaData, sqlStatement);
+ Collection<ShardingSphereRule> globalRules = ruleMetaData.getRules();
TrafficRule trafficRule = new TrafficRule(toBeAlteredRuleConfig);
globalRules.removeIf(each -> each instanceof TrafficRule);
globalRules.add(trafficRule);
}
- private TrafficRuleConfiguration createToBeAlteredRuleConfiguration() {
+ private TrafficRuleConfiguration createToBeAlteredRuleConfiguration(final ShardingSphereRuleMetaData ruleMetaData, final AlterTrafficRuleStatement sqlStatement) {
TrafficRuleConfiguration result = new TrafficRuleConfiguration();
- TrafficRuleConfiguration configFromSQLStatement = TrafficRuleConverter.convert(getSqlStatement().getSegments());
- TrafficRuleConfiguration currentConfig = ProxyContext
- .getInstance().getContextManager().getMetaDataContexts().getMetaData().getGlobalRuleMetaData().getSingleRule(TrafficRule.class).getConfiguration();
+ TrafficRuleConfiguration configFromSQLStatement = TrafficRuleConverter.convert(sqlStatement.getSegments());
+ TrafficRuleConfiguration currentConfig = ruleMetaData.getSingleRule(TrafficRule.class).getConfiguration();
result.getTrafficStrategies().addAll(createToBeAlteredStrategyConfigurations(currentConfig, configFromSQLStatement));
result.getTrafficAlgorithms().putAll(createToBeAlteredTrafficAlgorithms(currentConfig, configFromSQLStatement, getInUsedTrafficAlgorithm(result)));
result.getLoadBalancers().putAll(createToBeAlteredLoadBalancers(currentConfig, configFromSQLStatement, getInUsedLoadBalancer(result)));
@@ -143,8 +141,8 @@ public final class AlterTrafficRuleHandler extends UpdatableRALBackendHandler<Al
return result;
}
- private void persistNewRuleConfigurations() {
- MetaDataContexts metaDataContexts = ProxyContext.getInstance().getContextManager().getMetaDataContexts();
- metaDataContexts.getPersistService().getGlobalRuleService().persist(metaDataContexts.getMetaData().getGlobalRuleMetaData().getConfigurations(), true);
+ @Override
+ public String getType() {
+ return AlterTrafficRuleStatement.class.getName();
}
}
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/CreateTrafficRuleHandler.java b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-distsql/shardingsphere-traffic-distsql-handler/src/main/java/org/apache/shardingsphere/traffic/distsql/handler/update/CreateTrafficRuleStatementUpdater.java
similarity index 64%
rename from shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/CreateTrafficRuleHandler.java
rename to shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-distsql/shardingsphere-traffic-distsql-handler/src/main/java/org/apache/shardingsphere/traffic/distsql/handler/update/CreateTrafficRuleStatementUpdater.java
index f7a23ea2620..2eb2e2a1a81 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/CreateTrafficRuleHandler.java
+++ b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-distsql/shardingsphere-traffic-distsql-handler/src/main/java/org/apache/shardingsphere/traffic/distsql/handler/update/CreateTrafficRuleStatementUpdater.java
@@ -15,22 +15,21 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.proxy.backend.handler.distsql.ral.updatable;
+package org.apache.shardingsphere.traffic.distsql.handler.update;
-import org.apache.shardingsphere.distsql.parser.segment.TrafficRuleSegment;
-import org.apache.shardingsphere.distsql.parser.statement.ral.updatable.CreateTrafficRuleStatement;
import org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration;
import org.apache.shardingsphere.infra.distsql.exception.DistSQLException;
import org.apache.shardingsphere.infra.distsql.exception.rule.DuplicateRuleException;
import org.apache.shardingsphere.infra.distsql.exception.rule.InvalidAlgorithmConfigurationException;
+import org.apache.shardingsphere.infra.distsql.update.GlobalRuleRALUpdater;
+import org.apache.shardingsphere.infra.metadata.database.rule.ShardingSphereRuleMetaData;
import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
-import org.apache.shardingsphere.mode.manager.ContextManager;
-import org.apache.shardingsphere.mode.metadata.MetaDataContexts;
-import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
-import org.apache.shardingsphere.proxy.backend.handler.distsql.ral.UpdatableRALBackendHandler;
-import org.apache.shardingsphere.proxy.backend.handler.distsql.ral.common.convert.TrafficRuleConverter;
+import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
import org.apache.shardingsphere.traffic.api.config.TrafficRuleConfiguration;
import org.apache.shardingsphere.traffic.api.config.TrafficStrategyConfiguration;
+import org.apache.shardingsphere.traffic.distsql.handler.convert.TrafficRuleConverter;
+import org.apache.shardingsphere.traffic.distsql.parser.segment.TrafficRuleSegment;
+import org.apache.shardingsphere.traffic.distsql.parser.statement.updatable.CreateTrafficRuleStatement;
import org.apache.shardingsphere.traffic.factory.TrafficAlgorithmFactory;
import org.apache.shardingsphere.traffic.factory.TrafficLoadBalanceAlgorithmFactory;
import org.apache.shardingsphere.traffic.rule.TrafficRule;
@@ -43,41 +42,41 @@ import java.util.Map;
import java.util.stream.Collectors;
/**
- * Create traffic rule handler.
+ * Create traffic rule statement updater.
*/
-public final class CreateTrafficRuleHandler extends UpdatableRALBackendHandler<CreateTrafficRuleStatement> {
+public final class CreateTrafficRuleStatementUpdater implements GlobalRuleRALUpdater {
@Override
- protected void update(final ContextManager contextManager) throws DistSQLException {
- check();
- replaceNewRule();
- persistNewRuleConfigurations();
+ public void executeUpdate(final ShardingSphereRuleMetaData ruleMetaData, final SQLStatement sqlStatement) throws DistSQLException {
+ CreateTrafficRuleStatement statement = (CreateTrafficRuleStatement) sqlStatement;
+ check(ruleMetaData, statement);
+ replaceNewRule(ruleMetaData, statement);
}
- private void check() throws DistSQLException {
- checkRuleNames();
- checkAlgorithmNames();
+ private void check(final ShardingSphereRuleMetaData ruleMetaData, final CreateTrafficRuleStatement sqlStatement) throws DistSQLException {
+ checkRuleNames(ruleMetaData, sqlStatement);
+ checkAlgorithmNames(sqlStatement);
}
- private void checkRuleNames() throws DistSQLException {
- Collection<String> inUsedRuleNames = getInUsedRuleNames();
+ private void checkRuleNames(final ShardingSphereRuleMetaData ruleMetaData, final CreateTrafficRuleStatement sqlStatement) throws DistSQLException {
+ Collection<String> inUsedRuleNames = getInUsedRuleNames(ruleMetaData, sqlStatement);
DistSQLException.predictionThrow(inUsedRuleNames.isEmpty(), () -> new DuplicateRuleException("Traffic", inUsedRuleNames));
}
- private Collection<String> getInUsedRuleNames() {
- TrafficRule currentRule = ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData().getGlobalRuleMetaData().getSingleRule(TrafficRule.class);
+ private Collection<String> getInUsedRuleNames(final ShardingSphereRuleMetaData ruleMetaData, final CreateTrafficRuleStatement sqlStatement) {
+ TrafficRule currentRule = ruleMetaData.getSingleRule(TrafficRule.class);
Collection<String> currentRuleNames = currentRule.getStrategyRules().stream().map(TrafficStrategyRule::getName).collect(Collectors.toSet());
- return getSqlStatement().getSegments().stream().map(TrafficRuleSegment::getName).filter(currentRuleNames::contains).collect(Collectors.toSet());
+ return sqlStatement.getSegments().stream().map(TrafficRuleSegment::getName).filter(currentRuleNames::contains).collect(Collectors.toSet());
}
- private void checkAlgorithmNames() throws DistSQLException {
- Collection<String> invalidAlgorithmNames = getInvalidAlgorithmNames();
+ private void checkAlgorithmNames(final CreateTrafficRuleStatement sqlStatement) throws DistSQLException {
+ Collection<String> invalidAlgorithmNames = getInvalidAlgorithmNames(sqlStatement);
DistSQLException.predictionThrow(invalidAlgorithmNames.isEmpty(), () -> new InvalidAlgorithmConfigurationException("Traffic", invalidAlgorithmNames));
}
- private Collection<String> getInvalidAlgorithmNames() {
+ private Collection<String> getInvalidAlgorithmNames(final CreateTrafficRuleStatement sqlStatement) {
Collection<String> result = new LinkedList<>();
- for (TrafficRuleSegment each : getSqlStatement().getSegments()) {
+ for (TrafficRuleSegment each : sqlStatement.getSegments()) {
if (!TrafficAlgorithmFactory.contains(each.getAlgorithm().getName())) {
result.add(each.getAlgorithm().getName());
}
@@ -88,19 +87,18 @@ public final class CreateTrafficRuleHandler extends UpdatableRALBackendHandler<C
return result;
}
- private void replaceNewRule() {
- TrafficRuleConfiguration toBeAlteredRuleConfig = createToBeAlteredRuleConfiguration();
- Collection<ShardingSphereRule> globalRules = ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData().getGlobalRuleMetaData().getRules();
+ private void replaceNewRule(final ShardingSphereRuleMetaData ruleMetaData, final CreateTrafficRuleStatement sqlStatement) {
+ TrafficRuleConfiguration toBeAlteredRuleConfig = createToBeAlteredRuleConfiguration(ruleMetaData, sqlStatement);
+ Collection<ShardingSphereRule> globalRules = ruleMetaData.getRules();
TrafficRule trafficRule = new TrafficRule(toBeAlteredRuleConfig);
globalRules.removeIf(each -> each instanceof TrafficRule);
globalRules.add(trafficRule);
}
- private TrafficRuleConfiguration createToBeAlteredRuleConfiguration() {
+ private TrafficRuleConfiguration createToBeAlteredRuleConfiguration(final ShardingSphereRuleMetaData ruleMetaData, final CreateTrafficRuleStatement sqlStatement) {
TrafficRuleConfiguration result = new TrafficRuleConfiguration();
- TrafficRuleConfiguration configFromSQLStatement = TrafficRuleConverter.convert(getSqlStatement().getSegments());
- TrafficRuleConfiguration currentConfig = ProxyContext
- .getInstance().getContextManager().getMetaDataContexts().getMetaData().getGlobalRuleMetaData().getSingleRule(TrafficRule.class).getConfiguration();
+ TrafficRuleConfiguration configFromSQLStatement = TrafficRuleConverter.convert(sqlStatement.getSegments());
+ TrafficRuleConfiguration currentConfig = ruleMetaData.getSingleRule(TrafficRule.class).getConfiguration();
result.getTrafficStrategies().addAll(createToBeAlteredStrategyConfigurations(currentConfig, configFromSQLStatement));
result.getTrafficAlgorithms().putAll(createToBeAlteredTrafficAlgorithms(currentConfig, configFromSQLStatement));
result.getLoadBalancers().putAll(createToBeAlteredLoadBalancers(currentConfig, configFromSQLStatement));
@@ -125,8 +123,8 @@ public final class CreateTrafficRuleHandler extends UpdatableRALBackendHandler<C
return result;
}
- private void persistNewRuleConfigurations() {
- MetaDataContexts metaDataContexts = ProxyContext.getInstance().getContextManager().getMetaDataContexts();
- metaDataContexts.getPersistService().getGlobalRuleService().persist(metaDataContexts.getMetaData().getGlobalRuleMetaData().getConfigurations(), true);
+ @Override
+ public String getType() {
+ return CreateTrafficRuleStatement.class.getName();
}
}
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/DropTrafficRuleHandler.java b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-distsql/shardingsphere-traffic-distsql-handler/src/main/java/org/apache/shardingsphere/traffic/distsql/handler/update/DropTrafficRuleStatementUpdater.java
similarity index 66%
rename from shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/DropTrafficRuleHandler.java
rename to shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-distsql/shardingsphere-traffic-distsql-handler/src/main/java/org/apache/shardingsphere/traffic/distsql/handler/update/DropTrafficRuleStatementUpdater.java
index f5bbe032925..ceb52d6ad15 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/DropTrafficRuleHandler.java
+++ b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-distsql/shardingsphere-traffic-distsql-handler/src/main/java/org/apache/shardingsphere/traffic/distsql/handler/update/DropTrafficRuleStatementUpdater.java
@@ -15,19 +15,18 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.proxy.backend.handler.distsql.ral.updatable;
+package org.apache.shardingsphere.traffic.distsql.handler.update;
-import org.apache.shardingsphere.distsql.parser.statement.ral.updatable.DropTrafficRuleStatement;
import org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration;
import org.apache.shardingsphere.infra.distsql.exception.DistSQLException;
import org.apache.shardingsphere.infra.distsql.exception.rule.RequiredRuleMissedException;
+import org.apache.shardingsphere.infra.distsql.update.GlobalRuleRALUpdater;
+import org.apache.shardingsphere.infra.metadata.database.rule.ShardingSphereRuleMetaData;
import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
-import org.apache.shardingsphere.mode.manager.ContextManager;
-import org.apache.shardingsphere.mode.metadata.MetaDataContexts;
-import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
-import org.apache.shardingsphere.proxy.backend.handler.distsql.ral.UpdatableRALBackendHandler;
+import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
import org.apache.shardingsphere.traffic.api.config.TrafficRuleConfiguration;
import org.apache.shardingsphere.traffic.api.config.TrafficStrategyConfiguration;
+import org.apache.shardingsphere.traffic.distsql.parser.statement.updatable.DropTrafficRuleStatement;
import org.apache.shardingsphere.traffic.rule.TrafficRule;
import java.util.Collection;
@@ -36,45 +35,43 @@ import java.util.Map;
import java.util.stream.Collectors;
/**
- * Drop traffic rule statement handler.
+ * Drop traffic rule statement updater.
*/
-public final class DropTrafficRuleHandler extends UpdatableRALBackendHandler<DropTrafficRuleStatement> {
+public final class DropTrafficRuleStatementUpdater implements GlobalRuleRALUpdater {
@Override
- protected void update(final ContextManager contextManager) throws DistSQLException {
- check();
- replaceNewRule();
- persistNewRuleConfigurations();
+ public void executeUpdate(final ShardingSphereRuleMetaData ruleMetaData, final SQLStatement sqlStatement) throws DistSQLException {
+ DropTrafficRuleStatement statement = (DropTrafficRuleStatement) sqlStatement;
+ check(ruleMetaData, statement);
+ replaceNewRule(ruleMetaData, statement);
}
- private void check() throws DistSQLException {
- checkRuleNames();
+ private void check(final ShardingSphereRuleMetaData ruleMetaData, final DropTrafficRuleStatement sqlStatement) throws DistSQLException {
+ checkRuleNames(ruleMetaData, sqlStatement);
}
- private void checkRuleNames() throws DistSQLException {
- if (getSqlStatement().isIfExists()) {
+ private void checkRuleNames(final ShardingSphereRuleMetaData ruleMetaData, final DropTrafficRuleStatement sqlStatement) throws DistSQLException {
+ if (sqlStatement.isIfExists()) {
return;
}
- TrafficRule rule = ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData().getGlobalRuleMetaData().getSingleRule(TrafficRule.class);
+ TrafficRule rule = ruleMetaData.getSingleRule(TrafficRule.class);
TrafficRuleConfiguration config = rule.getConfiguration();
Collection<String> currentRuleNames = config.getTrafficStrategies().stream().map(TrafficStrategyConfiguration::getName).collect(Collectors.toSet());
- Collection<String> notExistRuleNames = getSqlStatement().getRuleNames().stream().filter(each -> !currentRuleNames.contains(each)).collect(Collectors.toSet());
+ Collection<String> notExistRuleNames = sqlStatement.getRuleNames().stream().filter(each -> !currentRuleNames.contains(each)).collect(Collectors.toSet());
DistSQLException.predictionThrow(notExistRuleNames.isEmpty(), () -> new RequiredRuleMissedException("Traffic"));
}
- private void replaceNewRule() {
- TrafficRuleConfiguration toBeAlteredRuleConfig = createToBeAlteredRuleConfiguration();
- Collection<ShardingSphereRule> globalRules = ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData().getGlobalRuleMetaData().getRules();
+ private void replaceNewRule(final ShardingSphereRuleMetaData ruleMetaData, final DropTrafficRuleStatement sqlStatement) {
+ TrafficRuleConfiguration toBeAlteredRuleConfig = createToBeAlteredRuleConfiguration(ruleMetaData, sqlStatement);
+ Collection<ShardingSphereRule> globalRules = ruleMetaData.getRules();
globalRules.removeIf(each -> each instanceof TrafficRule);
globalRules.add(new TrafficRule(toBeAlteredRuleConfig));
}
- private TrafficRuleConfiguration createToBeAlteredRuleConfiguration() {
+ private TrafficRuleConfiguration createToBeAlteredRuleConfiguration(final ShardingSphereRuleMetaData ruleMetaData, final DropTrafficRuleStatement sqlStatement) {
TrafficRuleConfiguration result = new TrafficRuleConfiguration();
- DropTrafficRuleStatement statement = getSqlStatement();
- TrafficRuleConfiguration currentConfig = ProxyContext
- .getInstance().getContextManager().getMetaDataContexts().getMetaData().getGlobalRuleMetaData().getSingleRule(TrafficRule.class).getConfiguration();
- result.getTrafficStrategies().addAll(createToBeAlteredStrategyConfigurations(currentConfig, statement));
+ TrafficRuleConfiguration currentConfig = ruleMetaData.getSingleRule(TrafficRule.class).getConfiguration();
+ result.getTrafficStrategies().addAll(createToBeAlteredStrategyConfigurations(currentConfig, sqlStatement));
result.getTrafficAlgorithms().putAll(createToBeAlteredTrafficAlgorithm(currentConfig, getInUsedTrafficAlgorithm(result)));
result.getLoadBalancers().putAll(createToBeAlteredLoadBalancers(currentConfig, getInUsedLoadBalancer(result)));
return result;
@@ -108,8 +105,8 @@ public final class DropTrafficRuleHandler extends UpdatableRALBackendHandler<Dro
return result;
}
- private void persistNewRuleConfigurations() {
- MetaDataContexts metaDataContexts = ProxyContext.getInstance().getContextManager().getMetaDataContexts();
- metaDataContexts.getPersistService().getGlobalRuleService().persist(metaDataContexts.getMetaData().getGlobalRuleMetaData().getConfigurations(), true);
+ @Override
+ public String getType() {
+ return DropTrafficRuleStatement.class.getName();
}
}
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/resources/META-INF/services/org.apache.shardingsphere.traffic.spi.TrafficLoadBalanceAlgorithm b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-distsql/shardingsphere-traffic-distsql-handler/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.distsql.update.GlobalRuleRALUpdater
similarity index 74%
copy from shardingsphere-proxy/shardingsphere-proxy-backend/src/test/resources/META-INF/services/org.apache.shardingsphere.traffic.spi.TrafficLoadBalanceAlgorithm
copy to shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-distsql/shardingsphere-traffic-distsql-handler/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.distsql.update.GlobalRuleRALUpdater
index a5d6e27292a..31cd47aa82d 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/resources/META-INF/services/org.apache.shardingsphere.traffic.spi.TrafficLoadBalanceAlgorithm
+++ b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-distsql/shardingsphere-traffic-distsql-handler/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.distsql.update.GlobalRuleRALUpdater
@@ -15,4 +15,6 @@
# limitations under the License.
#
-org.apache.shardingsphere.proxy.backend.handler.distsql.fixture.DistSQLTrafficLoadBalanceAlgorithmFixture
+org.apache.shardingsphere.traffic.distsql.handler.update.CreateTrafficRuleStatementUpdater
+org.apache.shardingsphere.traffic.distsql.handler.update.AlterTrafficRuleStatementUpdater
+org.apache.shardingsphere.traffic.distsql.handler.update.DropTrafficRuleStatementUpdater
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/fixture/DistSQLTrafficAlgorithmFixture.java b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-distsql/shardingsphere-traffic-distsql-handler/src/test/java/org/apache/shardingsphere/traffic/distsql/handler/fixture/DistSQLTrafficAlgorithmFixture.java
similarity index 94%
rename from shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/fixture/DistSQLTrafficAlgorithmFixture.java
rename to shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-distsql/shardingsphere-traffic-distsql-handler/src/test/java/org/apache/shardingsphere/traffic/distsql/handler/fixture/DistSQLTrafficAlgorithmFixture.java
index 3aa269363fc..55e90ea97a7 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/fixture/DistSQLTrafficAlgorithmFixture.java
+++ b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-distsql/shardingsphere-traffic-distsql-handler/src/test/java/org/apache/shardingsphere/traffic/distsql/handler/fixture/DistSQLTrafficAlgorithmFixture.java
@@ -15,7 +15,7 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.proxy.backend.handler.distsql.fixture;
+package org.apache.shardingsphere.traffic.distsql.handler.fixture;
import lombok.Getter;
import org.apache.shardingsphere.traffic.spi.TrafficAlgorithm;
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/fixture/DistSQLTrafficLoadBalanceAlgorithmFixture.java b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-distsql/shardingsphere-traffic-distsql-handler/src/test/java/org/apache/shardingsphere/traffic/distsql/handler/fixture/DistSQLTrafficLoadBalanceAlgorithmFixture.java
similarity index 95%
rename from shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/fixture/DistSQLTrafficLoadBalanceAlgorithmFixture.java
rename to shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-distsql/shardingsphere-traffic-distsql-handler/src/test/java/org/apache/shardingsphere/traffic/distsql/handler/fixture/DistSQLTrafficLoadBalanceAlgorithmFixture.java
index 9fa80cc2acb..48132ff23bb 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/fixture/DistSQLTrafficLoadBalanceAlgorithmFixture.java
+++ b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-distsql/shardingsphere-traffic-distsql-handler/src/test/java/org/apache/shardingsphere/traffic/distsql/handler/fixture/DistSQLTrafficLoadBalanceAlgorithmFixture.java
@@ -15,7 +15,7 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.proxy.backend.handler.distsql.fixture;
+package org.apache.shardingsphere.traffic.distsql.handler.fixture;
import lombok.Getter;
import org.apache.shardingsphere.infra.instance.metadata.InstanceMetaData;
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/AlterTrafficRuleHandlerTest.java b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-distsql/shardingsphere-traffic-distsql-handler/src/test/java/org/apache/shardingsphere/traffic/distsql/handler/update/AlterTrafficRuleStatementUpdaterTest.java
similarity index 68%
rename from shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/AlterTrafficRuleHandlerTest.java
rename to shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-distsql/shardingsphere-traffic-distsql-handler/src/test/java/org/apache/shardingsphere/traffic/distsql/handler/update/AlterTrafficRuleStatementUpdaterTest.java
index b4d90686bac..f69ad80fc75 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/AlterTrafficRuleHandlerTest.java
+++ b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-distsql/shardingsphere-traffic-distsql-handler/src/test/java/org/apache/shardingsphere/traffic/distsql/handler/update/AlterTrafficRuleStatementUpdaterTest.java
@@ -15,21 +15,17 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.proxy.backend.handler.distsql.ral.updatable;
+package org.apache.shardingsphere.traffic.distsql.handler.update;
import org.apache.shardingsphere.distsql.parser.segment.AlgorithmSegment;
-import org.apache.shardingsphere.distsql.parser.segment.TrafficRuleSegment;
-import org.apache.shardingsphere.distsql.parser.statement.ral.updatable.AlterTrafficRuleStatement;
-import org.apache.shardingsphere.distsql.parser.statement.ral.updatable.CreateTrafficRuleStatement;
import org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration;
import org.apache.shardingsphere.infra.distsql.exception.rule.InvalidAlgorithmConfigurationException;
import org.apache.shardingsphere.infra.distsql.exception.rule.RequiredRuleMissedException;
import org.apache.shardingsphere.infra.metadata.database.rule.ShardingSphereRuleMetaData;
-import org.apache.shardingsphere.mode.manager.ContextManager;
-import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
-import org.apache.shardingsphere.proxy.backend.util.ProxyContextRestorer;
import org.apache.shardingsphere.traffic.api.config.TrafficRuleConfiguration;
import org.apache.shardingsphere.traffic.api.config.TrafficStrategyConfiguration;
+import org.apache.shardingsphere.traffic.distsql.parser.segment.TrafficRuleSegment;
+import org.apache.shardingsphere.traffic.distsql.parser.statement.updatable.AlterTrafficRuleStatement;
import org.apache.shardingsphere.traffic.rule.TrafficRule;
import org.apache.shardingsphere.traffic.rule.TrafficStrategyRule;
import org.junit.Test;
@@ -43,43 +39,39 @@ import java.util.Properties;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
-import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
-public final class AlterTrafficRuleHandlerTest extends ProxyContextRestorer {
+public final class AlterTrafficRuleStatementUpdaterTest {
@Test(expected = RequiredRuleMissedException.class)
public void assertExecuteWithNotExistRuleName() throws SQLException {
- mockContextManager();
+ ShardingSphereRuleMetaData ruleMetaData = createRuleMetaData();
TrafficRuleSegment trafficRuleSegment = new TrafficRuleSegment(
"rule_name_3", Arrays.asList("olap", "order_by"), new AlgorithmSegment("DISTSQL.FIXTURE", new Properties()), new AlgorithmSegment("DISTSQL.FIXTURE", new Properties()));
- AlterTrafficRuleHandler handler = new AlterTrafficRuleHandler();
- handler.init(new AlterTrafficRuleStatement(Collections.singleton(trafficRuleSegment)), null);
- handler.execute();
+ AlterTrafficRuleStatementUpdater updater = new AlterTrafficRuleStatementUpdater();
+ updater.executeUpdate(ruleMetaData, new AlterTrafficRuleStatement(Collections.singleton(trafficRuleSegment)));
}
@Test(expected = InvalidAlgorithmConfigurationException.class)
public void assertExecuteWithInvalidAlgorithmType() throws SQLException {
- mockContextManager();
+ ShardingSphereRuleMetaData ruleMetaData = createRuleMetaData();
TrafficRuleSegment trafficRuleSegment = new TrafficRuleSegment(
"rule_name_1", Arrays.asList("olap", "order_by"), new AlgorithmSegment("invalid", new Properties()), new AlgorithmSegment("invalid", new Properties()));
- AlterTrafficRuleHandler handler = new AlterTrafficRuleHandler();
- handler.init(new AlterTrafficRuleStatement(Collections.singleton(trafficRuleSegment)), null);
- handler.execute();
+ AlterTrafficRuleStatementUpdater updater = new AlterTrafficRuleStatementUpdater();
+ updater.executeUpdate(ruleMetaData, new AlterTrafficRuleStatement(Collections.singleton(trafficRuleSegment)));
}
@Test(expected = IllegalStateException.class)
public void assertExecuteWithLoadBalancerCannotBeNull() throws SQLException {
- mockContextManager();
- TrafficRuleSegment trafficRuleSegment = new TrafficRuleSegment("input_rule_name", Arrays.asList("olap", "order_by"),
+ ShardingSphereRuleMetaData ruleMetaData = createRuleMetaData();
+ TrafficRuleSegment trafficRuleSegment = new TrafficRuleSegment("rule_name_1", Arrays.asList("olap", "order_by"),
new AlgorithmSegment("DISTSQL.FIXTURE", new Properties()), null);
- CreateTrafficRuleHandler handler = new CreateTrafficRuleHandler();
- handler.init(new CreateTrafficRuleStatement(Collections.singleton(trafficRuleSegment)), null);
+ AlterTrafficRuleStatementUpdater updater = new AlterTrafficRuleStatementUpdater();
try {
- handler.execute();
+ updater.executeUpdate(ruleMetaData, new AlterTrafficRuleStatement(Collections.singleton(trafficRuleSegment)));
} catch (final IllegalStateException ex) {
- TrafficRule currentRule = ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData().getGlobalRuleMetaData().getSingleRule(TrafficRule.class);
+ TrafficRule currentRule = ruleMetaData.getSingleRule(TrafficRule.class);
assertNotNull(currentRule);
throw ex;
}
@@ -87,15 +79,14 @@ public final class AlterTrafficRuleHandlerTest extends ProxyContextRestorer {
@Test
public void assertExecute() throws SQLException {
- ContextManager contextManager = mockContextManager();
+ ShardingSphereRuleMetaData ruleMetaData = createRuleMetaData();
TrafficRuleSegment trafficRuleSegment1 = new TrafficRuleSegment(
"rule_name_1", Arrays.asList("olap", "order_by"), new AlgorithmSegment("DISTSQL.FIXTURE", new Properties()), new AlgorithmSegment("DISTSQL.FIXTURE", new Properties()));
TrafficRuleSegment trafficRuleSegment2 = new TrafficRuleSegment(
"rule_name_2", Collections.emptyList(), new AlgorithmSegment("DISTSQL.FIXTURE", new Properties()), new AlgorithmSegment("DISTSQL.FIXTURE", new Properties()));
- AlterTrafficRuleHandler handler = new AlterTrafficRuleHandler();
- handler.init(new AlterTrafficRuleStatement(Arrays.asList(trafficRuleSegment1, trafficRuleSegment2)), null);
- handler.execute();
- TrafficRuleConfiguration alteredConfig = contextManager.getMetaDataContexts().getMetaData().getGlobalRuleMetaData().getSingleRule(TrafficRule.class).getConfiguration();
+ AlterTrafficRuleStatementUpdater updater = new AlterTrafficRuleStatementUpdater();
+ updater.executeUpdate(ruleMetaData, new AlterTrafficRuleStatement(Arrays.asList(trafficRuleSegment1, trafficRuleSegment2)));
+ TrafficRuleConfiguration alteredConfig = ruleMetaData.getSingleRule(TrafficRule.class).getConfiguration();
assertThat(alteredConfig.getTrafficStrategies().size(), is(2));
assertThat(alteredConfig.getLoadBalancers().size(), is(2));
assertThat(alteredConfig.getTrafficAlgorithms().size(), is(2));
@@ -104,18 +95,13 @@ public final class AlterTrafficRuleHandlerTest extends ProxyContextRestorer {
assertNotNull(alteredConfig.getLoadBalancers().get("rule_name_2_distsql.fixture"));
}
- private ContextManager mockContextManager() {
- ContextManager result = mock(ContextManager.class, RETURNS_DEEP_STUBS);
- TrafficRule rule = mockTrafficRule();
- when(rule.getConfiguration()).thenReturn(createTrafficRuleConfiguration());
- ShardingSphereRuleMetaData globalRuleMetaData = new ShardingSphereRuleMetaData(new LinkedList<>(Collections.singleton(rule)));
- when(result.getMetaDataContexts().getMetaData().getGlobalRuleMetaData()).thenReturn(globalRuleMetaData);
- ProxyContext.init(result);
- return result;
+ private ShardingSphereRuleMetaData createRuleMetaData() {
+ return new ShardingSphereRuleMetaData(new LinkedList<>(Collections.singleton(mockTrafficRule())));
}
private TrafficRule mockTrafficRule() {
TrafficRule result = mock(TrafficRule.class);
+ when(result.getConfiguration()).thenReturn(createTrafficRuleConfiguration());
TrafficStrategyRule strategyRule1 = mock(TrafficStrategyRule.class);
when(strategyRule1.getName()).thenReturn("rule_name_1");
TrafficStrategyRule strategyRule2 = mock(TrafficStrategyRule.class);
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/CreateTrafficRuleHandlerTest.java b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-distsql/shardingsphere-traffic-distsql-handler/src/test/java/org/apache/shardingsphere/traffic/distsql/handler/update/CreateTrafficRuleStatementUpdaterTest.java
similarity index 71%
rename from shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/CreateTrafficRuleHandlerTest.java
rename to shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-distsql/shardingsphere-traffic-distsql-handler/src/test/java/org/apache/shardingsphere/traffic/distsql/handler/update/CreateTrafficRuleStatementUpdaterTest.java
index 7fae5923502..39cf17c27f9 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/CreateTrafficRuleHandlerTest.java
+++ b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-distsql/shardingsphere-traffic-distsql-handler/src/test/java/org/apache/shardingsphere/traffic/distsql/handler/update/CreateTrafficRuleStatementUpdaterTest.java
@@ -15,20 +15,17 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.proxy.backend.handler.distsql.ral.updatable;
+package org.apache.shardingsphere.traffic.distsql.handler.update;
import org.apache.shardingsphere.distsql.parser.segment.AlgorithmSegment;
-import org.apache.shardingsphere.distsql.parser.segment.TrafficRuleSegment;
-import org.apache.shardingsphere.distsql.parser.statement.ral.updatable.CreateTrafficRuleStatement;
import org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration;
import org.apache.shardingsphere.infra.distsql.exception.rule.DuplicateRuleException;
import org.apache.shardingsphere.infra.distsql.exception.rule.InvalidAlgorithmConfigurationException;
import org.apache.shardingsphere.infra.metadata.database.rule.ShardingSphereRuleMetaData;
-import org.apache.shardingsphere.mode.manager.ContextManager;
-import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
-import org.apache.shardingsphere.proxy.backend.util.ProxyContextRestorer;
import org.apache.shardingsphere.traffic.api.config.TrafficRuleConfiguration;
import org.apache.shardingsphere.traffic.api.config.TrafficStrategyConfiguration;
+import org.apache.shardingsphere.traffic.distsql.parser.segment.TrafficRuleSegment;
+import org.apache.shardingsphere.traffic.distsql.parser.statement.updatable.CreateTrafficRuleStatement;
import org.apache.shardingsphere.traffic.rule.TrafficRule;
import org.apache.shardingsphere.traffic.rule.TrafficStrategyRule;
import org.junit.Test;
@@ -43,43 +40,39 @@ import java.util.Properties;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
-import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
-public final class CreateTrafficRuleHandlerTest extends ProxyContextRestorer {
+public final class CreateTrafficRuleStatementUpdaterTest {
@Test(expected = DuplicateRuleException.class)
public void assertExecuteWithInUsedRuleName() throws SQLException {
- mockContextManager();
+ ShardingSphereRuleMetaData ruleMetaData = createRuleMetaData();
TrafficRuleSegment trafficRuleSegment = new TrafficRuleSegment(
"rule_name_1", Arrays.asList("olap", "order_by"), new AlgorithmSegment("DISTSQL.FIXTURE", new Properties()), new AlgorithmSegment("DISTSQL.FIXTURE", new Properties()));
- CreateTrafficRuleHandler handler = new CreateTrafficRuleHandler();
- handler.init(new CreateTrafficRuleStatement(Collections.singleton(trafficRuleSegment)), null);
- handler.execute();
+ CreateTrafficRuleStatementUpdater updater = new CreateTrafficRuleStatementUpdater();
+ updater.executeUpdate(ruleMetaData, new CreateTrafficRuleStatement(Collections.singleton(trafficRuleSegment)));
}
@Test(expected = InvalidAlgorithmConfigurationException.class)
public void assertExecuteWithInvalidAlgorithmType() throws SQLException {
- mockContextManager();
+ ShardingSphereRuleMetaData ruleMetaData = createRuleMetaData();
TrafficRuleSegment trafficRuleSegment = new TrafficRuleSegment("input_rule_name", Arrays.asList("olap", "order_by"),
new AlgorithmSegment("invalid", new Properties()), new AlgorithmSegment("invalid", new Properties()));
- CreateTrafficRuleHandler handler = new CreateTrafficRuleHandler();
- handler.init(new CreateTrafficRuleStatement(Collections.singleton(trafficRuleSegment)), null);
- handler.execute();
+ CreateTrafficRuleStatementUpdater updater = new CreateTrafficRuleStatementUpdater();
+ updater.executeUpdate(ruleMetaData, new CreateTrafficRuleStatement(Collections.singleton(trafficRuleSegment)));
}
@Test(expected = IllegalStateException.class)
public void assertExecuteWithLoadBalancerCannotBeNull() throws SQLException {
- mockContextManager();
+ ShardingSphereRuleMetaData ruleMetaData = createRuleMetaData();
TrafficRuleSegment trafficRuleSegment = new TrafficRuleSegment("input_rule_name", Arrays.asList("olap", "order_by"),
new AlgorithmSegment("DISTSQL.FIXTURE", new Properties()), null);
- CreateTrafficRuleHandler handler = new CreateTrafficRuleHandler();
- handler.init(new CreateTrafficRuleStatement(Collections.singleton(trafficRuleSegment)), null);
+ CreateTrafficRuleStatementUpdater updater = new CreateTrafficRuleStatementUpdater();
try {
- handler.execute();
+ updater.executeUpdate(ruleMetaData, new CreateTrafficRuleStatement(Collections.singleton(trafficRuleSegment)));
} catch (final IllegalStateException ex) {
- TrafficRule currentRule = ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData().getGlobalRuleMetaData().getSingleRule(TrafficRule.class);
+ TrafficRule currentRule = ruleMetaData.getSingleRule(TrafficRule.class);
assertNotNull(currentRule);
throw ex;
}
@@ -87,15 +80,14 @@ public final class CreateTrafficRuleHandlerTest extends ProxyContextRestorer {
@Test
public void assertExecute() throws SQLException {
- ContextManager contextManager = mockContextManager();
+ ShardingSphereRuleMetaData ruleMetaData = createRuleMetaData();
TrafficRuleSegment trafficRuleSegment1 = new TrafficRuleSegment(
"rule_name_3", Arrays.asList("olap", "order_by"), new AlgorithmSegment("DISTSQL.FIXTURE", new Properties()), new AlgorithmSegment("DISTSQL.FIXTURE", new Properties()));
TrafficRuleSegment trafficRuleSegment2 = new TrafficRuleSegment(
"rule_name_4", Collections.emptyList(), new AlgorithmSegment("DISTSQL.FIXTURE", new Properties()), new AlgorithmSegment("DISTSQL.FIXTURE", new Properties()));
- CreateTrafficRuleHandler handler = new CreateTrafficRuleHandler();
- handler.init(new CreateTrafficRuleStatement(Arrays.asList(trafficRuleSegment1, trafficRuleSegment2)), null);
- handler.execute();
- TrafficRuleConfiguration addedConfig = contextManager.getMetaDataContexts().getMetaData().getGlobalRuleMetaData().getSingleRule(TrafficRule.class).getConfiguration();
+ CreateTrafficRuleStatementUpdater updater = new CreateTrafficRuleStatementUpdater();
+ updater.executeUpdate(ruleMetaData, new CreateTrafficRuleStatement(Arrays.asList(trafficRuleSegment1, trafficRuleSegment2)));
+ TrafficRuleConfiguration addedConfig = ruleMetaData.getSingleRule(TrafficRule.class).getConfiguration();
assertThat(addedConfig.getTrafficStrategies().size(), is(4));
assertThat(addedConfig.getLoadBalancers().size(), is(4));
assertThat(addedConfig.getTrafficAlgorithms().size(), is(4));
@@ -107,18 +99,13 @@ public final class CreateTrafficRuleHandlerTest extends ProxyContextRestorer {
assertNotNull(addedConfig.getLoadBalancers().get("rule_name_4_distsql.fixture"));
}
- private ContextManager mockContextManager() {
- ContextManager result = mock(ContextManager.class, RETURNS_DEEP_STUBS);
- TrafficRule rule = mockTrafficRule();
- when(rule.getConfiguration()).thenReturn(createTrafficRuleConfiguration());
- ShardingSphereRuleMetaData globalRuleMetaData = new ShardingSphereRuleMetaData(new LinkedList<>(Collections.singleton(rule)));
- when(result.getMetaDataContexts().getMetaData().getGlobalRuleMetaData()).thenReturn(globalRuleMetaData);
- ProxyContext.init(result);
- return result;
+ private ShardingSphereRuleMetaData createRuleMetaData() {
+ return new ShardingSphereRuleMetaData(new LinkedList<>(Collections.singleton(mockTrafficRule())));
}
private TrafficRule mockTrafficRule() {
TrafficRule result = mock(TrafficRule.class);
+ when(result.getConfiguration()).thenReturn(createTrafficRuleConfiguration());
TrafficStrategyRule strategyRule1 = mock(TrafficStrategyRule.class);
when(strategyRule1.getName()).thenReturn("rule_name_1");
TrafficStrategyRule strategyRule2 = mock(TrafficStrategyRule.class);
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/DropTrafficRuleHandlerTest.java b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-distsql/shardingsphere-traffic-distsql-handler/src/test/java/org/apache/shardingsphere/traffic/distsql/handler/update/DropTrafficRuleStatementUpdaterTest.java
similarity index 63%
rename from shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/DropTrafficRuleHandlerTest.java
rename to shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-distsql/shardingsphere-traffic-distsql-handler/src/test/java/org/apache/shardingsphere/traffic/distsql/handler/update/DropTrafficRuleStatementUpdaterTest.java
index 97512b483cb..6be63b66f74 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/DropTrafficRuleHandlerTest.java
+++ b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-distsql/shardingsphere-traffic-distsql-handler/src/test/java/org/apache/shardingsphere/traffic/distsql/handler/update/DropTrafficRuleStatementUpdaterTest.java
@@ -15,17 +15,14 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.proxy.backend.handler.distsql.ral.updatable;
+package org.apache.shardingsphere.traffic.distsql.handler.update;
-import org.apache.shardingsphere.distsql.parser.statement.ral.updatable.DropTrafficRuleStatement;
import org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration;
import org.apache.shardingsphere.infra.distsql.exception.rule.RequiredRuleMissedException;
import org.apache.shardingsphere.infra.metadata.database.rule.ShardingSphereRuleMetaData;
-import org.apache.shardingsphere.mode.manager.ContextManager;
-import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
-import org.apache.shardingsphere.proxy.backend.util.ProxyContextRestorer;
import org.apache.shardingsphere.traffic.api.config.TrafficRuleConfiguration;
import org.apache.shardingsphere.traffic.api.config.TrafficStrategyConfiguration;
+import org.apache.shardingsphere.traffic.distsql.parser.statement.updatable.DropTrafficRuleStatement;
import org.apache.shardingsphere.traffic.rule.TrafficRule;
import org.junit.Test;
@@ -39,27 +36,24 @@ import java.util.Properties;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
-import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
-public final class DropTrafficRuleHandlerTest extends ProxyContextRestorer {
+public final class DropTrafficRuleStatementUpdaterTest {
@Test(expected = RequiredRuleMissedException.class)
public void assertExecuteForNotExistedRuleWithoutIfExists() throws SQLException {
- mockContextManager();
- DropTrafficRuleHandler handler = new DropTrafficRuleHandler();
- handler.init(new DropTrafficRuleStatement(false, Collections.singleton("not_existed_rule")), null);
- handler.execute();
+ ShardingSphereRuleMetaData ruleMetaData = createRuleMetaData();
+ DropTrafficRuleStatementUpdater updater = new DropTrafficRuleStatementUpdater();
+ updater.executeUpdate(ruleMetaData, new DropTrafficRuleStatement(false, Collections.singleton("not_existed_rule")));
}
@Test
public void assertExecuteForNotExistedRuleWithIfExists() throws SQLException {
- ContextManager contextManager = mockContextManager();
- DropTrafficRuleHandler handler = new DropTrafficRuleHandler();
- handler.init(new DropTrafficRuleStatement(true, Collections.singleton("rule_name_3")), null);
- handler.execute();
- TrafficRuleConfiguration updatedConfig = contextManager.getMetaDataContexts().getMetaData().getGlobalRuleMetaData().getSingleRule(TrafficRule.class).getConfiguration();
+ ShardingSphereRuleMetaData ruleMetaData = createRuleMetaData();
+ DropTrafficRuleStatementUpdater updater = new DropTrafficRuleStatementUpdater();
+ updater.executeUpdate(ruleMetaData, new DropTrafficRuleStatement(true, Collections.singleton("rule_name_3")));
+ TrafficRuleConfiguration updatedConfig = ruleMetaData.getSingleRule(TrafficRule.class).getConfiguration();
assertThat(updatedConfig.getTrafficStrategies().size(), is(2));
assertThat(updatedConfig.getLoadBalancers().size(), is(2));
assertThat(updatedConfig.getTrafficAlgorithms().size(), is(2));
@@ -67,11 +61,10 @@ public final class DropTrafficRuleHandlerTest extends ProxyContextRestorer {
@Test
public void assertExecute() throws SQLException {
- ContextManager contextManager = mockContextManager();
- DropTrafficRuleHandler handler = new DropTrafficRuleHandler();
- handler.init(new DropTrafficRuleStatement(false, Collections.singleton("rule_name_1")), null);
- handler.execute();
- TrafficRuleConfiguration updatedConfig = contextManager.getMetaDataContexts().getMetaData().getGlobalRuleMetaData().getSingleRule(TrafficRule.class).getConfiguration();
+ ShardingSphereRuleMetaData ruleMetaData = createRuleMetaData();
+ DropTrafficRuleStatementUpdater updater = new DropTrafficRuleStatementUpdater();
+ updater.executeUpdate(ruleMetaData, new DropTrafficRuleStatement(false, Collections.singleton("rule_name_1")));
+ TrafficRuleConfiguration updatedConfig = ruleMetaData.getSingleRule(TrafficRule.class).getConfiguration();
assertThat(updatedConfig.getTrafficStrategies().size(), is(1));
assertThat(updatedConfig.getLoadBalancers().size(), is(1));
assertThat(updatedConfig.getTrafficAlgorithms().size(), is(1));
@@ -80,14 +73,10 @@ public final class DropTrafficRuleHandlerTest extends ProxyContextRestorer {
assertNotNull(updatedConfig.getLoadBalancers().get("load_balancer_2"));
}
- private ContextManager mockContextManager() {
- ContextManager result = mock(ContextManager.class, RETURNS_DEEP_STUBS);
- TrafficRule rule = mock(TrafficRule.class);
- when(rule.getConfiguration()).thenReturn(createTrafficRuleConfiguration());
- ShardingSphereRuleMetaData globalRuleMetaData = new ShardingSphereRuleMetaData(new LinkedList<>(Collections.singleton(rule)));
- when(result.getMetaDataContexts().getMetaData().getGlobalRuleMetaData()).thenReturn(globalRuleMetaData);
- ProxyContext.init(result);
- return result;
+ private ShardingSphereRuleMetaData createRuleMetaData() {
+ TrafficRule trafficRule = mock(TrafficRule.class);
+ when(trafficRule.getConfiguration()).thenReturn(createTrafficRuleConfiguration());
+ return new ShardingSphereRuleMetaData(new LinkedList<>(Collections.singleton(trafficRule)));
}
private TrafficRuleConfiguration createTrafficRuleConfiguration() {
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/resources/META-INF/services/org.apache.shardingsphere.traffic.spi.TrafficAlgorithm b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-distsql/shardingsphere-traffic-distsql-handler/src/test/resources/META-INF/services/org.apache.shardingsphere.traffic.spi.TrafficAlgorithm
similarity index 89%
rename from shardingsphere-proxy/shardingsphere-proxy-backend/src/test/resources/META-INF/services/org.apache.shardingsphere.traffic.spi.TrafficAlgorithm
rename to shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-distsql/shardingsphere-traffic-distsql-handler/src/test/resources/META-INF/services/org.apache.shardingsphere.traffic.spi.TrafficAlgorithm
index 5a739086447..d46cdca170c 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/resources/META-INF/services/org.apache.shardingsphere.traffic.spi.TrafficAlgorithm
+++ b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-distsql/shardingsphere-traffic-distsql-handler/src/test/resources/META-INF/services/org.apache.shardingsphere.traffic.spi.TrafficAlgorithm
@@ -15,4 +15,4 @@
# limitations under the License.
#
-org.apache.shardingsphere.proxy.backend.handler.distsql.fixture.DistSQLTrafficAlgorithmFixture
+org.apache.shardingsphere.traffic.distsql.handler.fixture.DistSQLTrafficAlgorithmFixture
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/resources/META-INF/services/org.apache.shardingsphere.traffic.spi.TrafficLoadBalanceAlgorithm b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-distsql/shardingsphere-traffic-distsql-handler/src/test/resources/META-INF/services/org.apache.shardingsphere.traffic.spi.TrafficLoadBalanceAlgorithm
similarity index 88%
rename from shardingsphere-proxy/shardingsphere-proxy-backend/src/test/resources/META-INF/services/org.apache.shardingsphere.traffic.spi.TrafficLoadBalanceAlgorithm
rename to shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-distsql/shardingsphere-traffic-distsql-handler/src/test/resources/META-INF/services/org.apache.shardingsphere.traffic.spi.TrafficLoadBalanceAlgorithm
index a5d6e27292a..134c6858d8b 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/resources/META-INF/services/org.apache.shardingsphere.traffic.spi.TrafficLoadBalanceAlgorithm
+++ b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-distsql/shardingsphere-traffic-distsql-handler/src/test/resources/META-INF/services/org.apache.shardingsphere.traffic.spi.TrafficLoadBalanceAlgorithm
@@ -15,4 +15,4 @@
# limitations under the License.
#
-org.apache.shardingsphere.proxy.backend.handler.distsql.fixture.DistSQLTrafficLoadBalanceAlgorithmFixture
+org.apache.shardingsphere.traffic.distsql.handler.fixture.DistSQLTrafficLoadBalanceAlgorithmFixture
diff --git a/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-distsql/shardingsphere-traffic-distsql-parser/src/main/antlr4/imports/traffic/Keyword.g4 b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-distsql/shardingsphere-traffic-distsql-parser/src/main/antlr4/imports/traffic/BaseRule.g4
similarity index 80%
copy from shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-distsql/shardingsphere-traffic-distsql-parser/src/main/antlr4/imports/traffic/Keyword.g4
copy to shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-distsql/shardingsphere-traffic-distsql-parser/src/main/antlr4/imports/traffic/BaseRule.g4
index 7f3ce62998e..90547789404 100644
--- a/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-distsql/shardingsphere-traffic-distsql-parser/src/main/antlr4/imports/traffic/Keyword.g4
+++ b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-distsql/shardingsphere-traffic-distsql-parser/src/main/antlr4/imports/traffic/BaseRule.g4
@@ -15,26 +15,18 @@
* limitations under the License.
*/
-lexer grammar Keyword;
+grammar BaseRule;
-import Alphabet;
+import Symbol, Keyword, Literals;
-WS
- : [ \t\r\n] + ->skip
+propertiesDefinition
+ : PROPERTIES LP properties? RP
;
-SHOW
- : S H O W
+properties
+ : property (COMMA property)*
;
-TRAFFIC
- : T R A F F I C
- ;
-
-RULES
- : R U L E S
- ;
-
-RULE
- : R U L E
+property
+ : key=STRING EQ value=STRING
;
diff --git a/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-distsql/shardingsphere-traffic-distsql-parser/src/main/antlr4/imports/traffic/Keyword.g4 b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-distsql/shardingsphere-traffic-distsql-parser/src/main/antlr4/imports/traffic/Keyword.g4
index 7f3ce62998e..a46575dc48f 100644
--- a/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-distsql/shardingsphere-traffic-distsql-parser/src/main/antlr4/imports/traffic/Keyword.g4
+++ b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-distsql/shardingsphere-traffic-distsql-parser/src/main/antlr4/imports/traffic/Keyword.g4
@@ -38,3 +38,47 @@ RULES
RULE
: R U L E
;
+
+CREATE
+ : C R E A T E
+ ;
+
+PROPERTIES
+ : P R O P E R T I E S
+ ;
+
+LABELS
+ : L A B E L S
+ ;
+
+TRAFFIC_ALGORITHM
+ : T R A F F I C UL_ A L G O R I T H M
+ ;
+
+TYPE
+ : T Y P E
+ ;
+
+NAME
+ : N A M E
+ ;
+
+LOAD_BALANCER
+ : L O A D UL_ B A L A N C E R
+ ;
+
+ALTER
+ : A L T E R
+ ;
+
+DROP
+ : D R O P
+ ;
+
+IF
+ : I F
+ ;
+
+EXISTS
+ : E X I S T S
+ ;
diff --git a/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-distsql/shardingsphere-traffic-distsql-parser/src/main/antlr4/imports/traffic/RALStatement.g4 b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-distsql/shardingsphere-traffic-distsql-parser/src/main/antlr4/imports/traffic/RALStatement.g4
index 6a83ea4f0de..e76201c1bce 100644
--- a/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-distsql/shardingsphere-traffic-distsql-parser/src/main/antlr4/imports/traffic/RALStatement.g4
+++ b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-distsql/shardingsphere-traffic-distsql-parser/src/main/antlr4/imports/traffic/RALStatement.g4
@@ -17,12 +17,56 @@
grammar RALStatement;
-import Keyword, Literals;
+import BaseRule;
+
+createTrafficRule
+ : CREATE TRAFFIC RULE trafficRuleDefinition (COMMA trafficRuleDefinition)*
+ ;
+
+alterTrafficRule
+ : ALTER TRAFFIC RULE trafficRuleDefinition (COMMA trafficRuleDefinition)*
+ ;
+
+dropTrafficRule
+ : DROP TRAFFIC RULE ifExists? ruleName (COMMA ruleName)*
+ ;
showTrafficRules
: SHOW TRAFFIC (RULES | RULE ruleName)
;
+trafficRuleDefinition
+ : ruleName LP (labelDefinition COMMA)? trafficAlgorithmDefinition (COMMA loadBalancerDefinition)? RP
+ ;
+
+labelDefinition
+ : LABELS LP label (COMMA label)* RP
+ ;
+
+trafficAlgorithmDefinition
+ : TRAFFIC_ALGORITHM LP algorithmDefinition RP
+ ;
+
+algorithmDefinition
+ : TYPE LP NAME EQ algorithmTypeName (COMMA propertiesDefinition)? RP
+ ;
+
+loadBalancerDefinition
+ : LOAD_BALANCER LP algorithmDefinition RP
+ ;
+
+algorithmTypeName
+ : STRING
+ ;
+
+label
+ : IDENTIFIER
+ ;
+
ruleName
: IDENTIFIER
;
+
+ifExists
+ : IF EXISTS
+ ;
diff --git a/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-distsql/shardingsphere-traffic-distsql-parser/src/main/antlr4/org/apache/shardingsphere/distsql/parser/autogen/TrafficDistSQLStatement.g4 b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-distsql/shardingsphere-traffic-distsql-parser/src/main/antlr4/org/apache/shardingsphere/distsql/parser/autogen/TrafficDistSQLStatement.g4
index d7524a3e37c..f67537baaf7 100644
--- a/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-distsql/shardingsphere-traffic-distsql-parser/src/main/antlr4/org/apache/shardingsphere/distsql/parser/autogen/TrafficDistSQLStatement.g4
+++ b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-distsql/shardingsphere-traffic-distsql-parser/src/main/antlr4/org/apache/shardingsphere/distsql/parser/autogen/TrafficDistSQLStatement.g4
@@ -20,5 +20,9 @@ grammar TrafficDistSQLStatement;
import Symbol, RALStatement;
execute
- : (showTrafficRules) SEMI?
+ : (showTrafficRules
+ | createTrafficRule
+ | alterTrafficRule
+ | dropTrafficRule
+ ) SEMI?
;
diff --git a/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-distsql/shardingsphere-traffic-distsql-parser/src/main/java/org/apache/shardingsphere/traffic/distsql/parser/core/TrafficDistSQLStatementVisitor.java b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-distsql/shardingsphere-traffic-distsql-parser/src/main/java/org/apache/shardingsphere/traffic/distsql/parser/core/TrafficDistSQLStatementVisitor.java
index bcf11128916..c97d337734b 100644
--- a/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-distsql/shardingsphere-traffic-distsql-parser/src/main/java/org/apache/shardingsphere/traffic/distsql/parser/core/TrafficDistSQLStatementVisitor.java
+++ b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-distsql/shardingsphere-traffic-distsql-parser/src/main/java/org/apache/shardingsphere/traffic/distsql/parser/core/TrafficDistSQLStatementVisitor.java
@@ -19,17 +19,93 @@ package org.apache.shardingsphere.traffic.distsql.parser.core;
import org.antlr.v4.runtime.tree.ParseTree;
import org.apache.shardingsphere.distsql.parser.autogen.TrafficDistSQLStatementBaseVisitor;
+import org.apache.shardingsphere.distsql.parser.autogen.TrafficDistSQLStatementParser.AlgorithmDefinitionContext;
+import org.apache.shardingsphere.distsql.parser.autogen.TrafficDistSQLStatementParser.AlterTrafficRuleContext;
+import org.apache.shardingsphere.distsql.parser.autogen.TrafficDistSQLStatementParser.CreateTrafficRuleContext;
+import org.apache.shardingsphere.distsql.parser.autogen.TrafficDistSQLStatementParser.DropTrafficRuleContext;
+import org.apache.shardingsphere.distsql.parser.autogen.TrafficDistSQLStatementParser.LabelDefinitionContext;
+import org.apache.shardingsphere.distsql.parser.autogen.TrafficDistSQLStatementParser.LoadBalancerDefinitionContext;
+import org.apache.shardingsphere.distsql.parser.autogen.TrafficDistSQLStatementParser.PropertiesDefinitionContext;
+import org.apache.shardingsphere.distsql.parser.autogen.TrafficDistSQLStatementParser.PropertyContext;
import org.apache.shardingsphere.distsql.parser.autogen.TrafficDistSQLStatementParser.ShowTrafficRulesContext;
+import org.apache.shardingsphere.distsql.parser.autogen.TrafficDistSQLStatementParser.TrafficRuleDefinitionContext;
+import org.apache.shardingsphere.distsql.parser.segment.AlgorithmSegment;
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.value.identifier.IdentifierValue;
+import org.apache.shardingsphere.traffic.distsql.parser.segment.TrafficRuleSegment;
import org.apache.shardingsphere.traffic.distsql.parser.statement.queryable.ShowTrafficRulesStatement;
+import org.apache.shardingsphere.traffic.distsql.parser.statement.updatable.AlterTrafficRuleStatement;
+import org.apache.shardingsphere.traffic.distsql.parser.statement.updatable.CreateTrafficRuleStatement;
+import org.apache.shardingsphere.traffic.distsql.parser.statement.updatable.DropTrafficRuleStatement;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Properties;
+import java.util.stream.Collectors;
/**
* SQL statement visitor for traffic dist SQL.
*/
public final class TrafficDistSQLStatementVisitor extends TrafficDistSQLStatementBaseVisitor<ASTNode> implements SQLVisitor {
+ @Override
+ public ASTNode visitCreateTrafficRule(final CreateTrafficRuleContext ctx) {
+ return new CreateTrafficRuleStatement(ctx.trafficRuleDefinition().stream().map(each -> (TrafficRuleSegment) visit(each)).collect(Collectors.toList()));
+ }
+
+ @Override
+ public ASTNode visitAlterTrafficRule(final AlterTrafficRuleContext ctx) {
+ return new AlterTrafficRuleStatement(ctx.trafficRuleDefinition().stream().map(each -> (TrafficRuleSegment) visit(each)).collect(Collectors.toList()));
+ }
+
+ @Override
+ public ASTNode visitTrafficRuleDefinition(final TrafficRuleDefinitionContext ctx) {
+ AlgorithmSegment loadBalancerSegment = null;
+ if (null != ctx.loadBalancerDefinition()) {
+ loadBalancerSegment = (AlgorithmSegment) visit(ctx.loadBalancerDefinition().algorithmDefinition());
+ }
+ return new TrafficRuleSegment(getIdentifierValue(ctx.ruleName()), buildLabels(ctx.labelDefinition()),
+ (AlgorithmSegment) visit(ctx.trafficAlgorithmDefinition().algorithmDefinition()), loadBalancerSegment);
+ }
+
+ private Collection<String> buildLabels(final LabelDefinitionContext labelDefinition) {
+ if (null == labelDefinition) {
+ return Collections.emptyList();
+ }
+ return labelDefinition.label().stream().map(this::getIdentifierValue).collect(Collectors.toList());
+ }
+
+ @Override
+ public ASTNode visitLoadBalancerDefinition(final LoadBalancerDefinitionContext ctx) {
+ if (null == ctx) {
+ return null;
+ }
+ return visit(ctx.algorithmDefinition());
+ }
+
+ @Override
+ public ASTNode visitAlgorithmDefinition(final AlgorithmDefinitionContext ctx) {
+ return new AlgorithmSegment(getIdentifierValue(ctx.algorithmTypeName()), buildProperties(ctx.propertiesDefinition()));
+ }
+
+ private Properties buildProperties(final PropertiesDefinitionContext ctx) {
+ Properties result = new Properties();
+ if (null == ctx) {
+ return result;
+ }
+ for (PropertyContext each : ctx.properties().property()) {
+ result.setProperty(IdentifierValue.getQuotedContent(each.key.getText()), IdentifierValue.getQuotedContent(each.value.getText()));
+ }
+ return result;
+ }
+
+ @Override
+ public ASTNode visitDropTrafficRule(final DropTrafficRuleContext ctx) {
+ Collection<String> ruleNames = null == ctx.ruleName() ? null : ctx.ruleName().stream().map(this::getIdentifierValue).collect(Collectors.toSet());
+ return new DropTrafficRuleStatement(null != ctx.ifExists(), ruleNames);
+ }
+
@Override
public ASTNode visitShowTrafficRules(final ShowTrafficRulesContext ctx) {
return new ShowTrafficRulesStatement(null == ctx.ruleName() ? null : getIdentifierValue(ctx.ruleName()));
diff --git a/shardingsphere-distsql/shardingsphere-distsql-statement/src/main/java/org/apache/shardingsphere/distsql/parser/segment/TrafficRuleSegment.java b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-distsql/shardingsphere-traffic-distsql-statement/src/main/java/org/apache/shardingsphere/traffic/distsql/parser/segment/TrafficRuleSegment.java
similarity index 90%
rename from shardingsphere-distsql/shardingsphere-distsql-statement/src/main/java/org/apache/shardingsphere/distsql/parser/segment/TrafficRuleSegment.java
rename to shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-distsql/shardingsphere-traffic-distsql-statement/src/main/java/org/apache/shardingsphere/traffic/distsql/parser/segment/TrafficRuleSegment.java
index b7f3c9c8278..d8b38561f3a 100644
--- a/shardingsphere-distsql/shardingsphere-distsql-statement/src/main/java/org/apache/shardingsphere/distsql/parser/segment/TrafficRuleSegment.java
+++ b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-distsql/shardingsphere-traffic-distsql-statement/src/main/java/org/apache/shardingsphere/traffic/distsql/parser/segment/TrafficRuleSegment.java
@@ -15,10 +15,11 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.distsql.parser.segment;
+package org.apache.shardingsphere.traffic.distsql.parser.segment;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
+import org.apache.shardingsphere.distsql.parser.segment.AlgorithmSegment;
import org.apache.shardingsphere.sql.parser.api.visitor.ASTNode;
import java.util.Collection;
diff --git a/shardingsphere-distsql/shardingsphere-distsql-statement/src/main/java/org/apache/shardingsphere/distsql/parser/statement/ral/updatable/AlterTrafficRuleStatement.java b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-distsql/shardingsphere-traffic-distsql-statement/src/main/java/org/apache/shardingsphere/traffic/distsql/parser/statement/updatable/AlterTrafficRuleStatement.java
similarity index 79%
rename from shardingsphere-distsql/shardingsphere-distsql-statement/src/main/java/org/apache/shardingsphere/distsql/parser/statement/ral/updatable/AlterTrafficRuleStatement.java
rename to shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-distsql/shardingsphere-traffic-distsql-statement/src/main/java/org/apache/shardingsphere/traffic/distsql/parser/statement/updatable/AlterTrafficRuleStatement.java
index 8d842f7ec06..b0be68db995 100644
--- a/shardingsphere-distsql/shardingsphere-distsql-statement/src/main/java/org/apache/shardingsphere/distsql/parser/statement/ral/updatable/AlterTrafficRuleStatement.java
+++ b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-distsql/shardingsphere-traffic-distsql-statement/src/main/java/org/apache/shardingsphere/traffic/distsql/parser/statement/updatable/AlterTrafficRuleStatement.java
@@ -15,12 +15,12 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.distsql.parser.statement.ral.updatable;
+package org.apache.shardingsphere.traffic.distsql.parser.statement.updatable;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
-import org.apache.shardingsphere.distsql.parser.segment.TrafficRuleSegment;
-import org.apache.shardingsphere.distsql.parser.statement.ral.UpdatableRALStatement;
+import org.apache.shardingsphere.distsql.parser.statement.ral.UpdatableGlobalRuleRALStatement;
+import org.apache.shardingsphere.traffic.distsql.parser.segment.TrafficRuleSegment;
import java.util.Collection;
@@ -29,7 +29,7 @@ import java.util.Collection;
*/
@RequiredArgsConstructor
@Getter
-public final class AlterTrafficRuleStatement extends UpdatableRALStatement {
+public final class AlterTrafficRuleStatement extends UpdatableGlobalRuleRALStatement {
private final Collection<TrafficRuleSegment> segments;
}
diff --git a/shardingsphere-distsql/shardingsphere-distsql-statement/src/main/java/org/apache/shardingsphere/distsql/parser/statement/ral/updatable/CreateTrafficRuleStatement.java b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-distsql/shardingsphere-traffic-distsql-statement/src/main/java/org/apache/shardingsphere/traffic/distsql/parser/statement/updatable/CreateTrafficRuleStatement.java
similarity index 79%
rename from shardingsphere-distsql/shardingsphere-distsql-statement/src/main/java/org/apache/shardingsphere/distsql/parser/statement/ral/updatable/CreateTrafficRuleStatement.java
rename to shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-distsql/shardingsphere-traffic-distsql-statement/src/main/java/org/apache/shardingsphere/traffic/distsql/parser/statement/updatable/CreateTrafficRuleStatement.java
index 12512501b0c..b65f88d9d5d 100644
--- a/shardingsphere-distsql/shardingsphere-distsql-statement/src/main/java/org/apache/shardingsphere/distsql/parser/statement/ral/updatable/CreateTrafficRuleStatement.java
+++ b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-distsql/shardingsphere-traffic-distsql-statement/src/main/java/org/apache/shardingsphere/traffic/distsql/parser/statement/updatable/CreateTrafficRuleStatement.java
@@ -15,12 +15,12 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.distsql.parser.statement.ral.updatable;
+package org.apache.shardingsphere.traffic.distsql.parser.statement.updatable;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
-import org.apache.shardingsphere.distsql.parser.segment.TrafficRuleSegment;
-import org.apache.shardingsphere.distsql.parser.statement.ral.UpdatableRALStatement;
+import org.apache.shardingsphere.distsql.parser.statement.ral.UpdatableGlobalRuleRALStatement;
+import org.apache.shardingsphere.traffic.distsql.parser.segment.TrafficRuleSegment;
import java.util.Collection;
@@ -29,7 +29,7 @@ import java.util.Collection;
*/
@RequiredArgsConstructor
@Getter
-public final class CreateTrafficRuleStatement extends UpdatableRALStatement {
+public final class CreateTrafficRuleStatement extends UpdatableGlobalRuleRALStatement {
private final Collection<TrafficRuleSegment> segments;
}
diff --git a/shardingsphere-distsql/shardingsphere-distsql-statement/src/main/java/org/apache/shardingsphere/distsql/parser/statement/ral/updatable/DropTrafficRuleStatement.java b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-distsql/shardingsphere-traffic-distsql-statement/src/main/java/org/apache/shardingsphere/traffic/distsql/parser/statement/updatable/DropTrafficRuleStatement.java
similarity index 85%
rename from shardingsphere-distsql/shardingsphere-distsql-statement/src/main/java/org/apache/shardingsphere/distsql/parser/statement/ral/updatable/DropTrafficRuleStatement.java
rename to shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-distsql/shardingsphere-traffic-distsql-statement/src/main/java/org/apache/shardingsphere/traffic/distsql/parser/statement/updatable/DropTrafficRuleStatement.java
index 5ff64f5f95d..9719ff177be 100644
--- a/shardingsphere-distsql/shardingsphere-distsql-statement/src/main/java/org/apache/shardingsphere/distsql/parser/statement/ral/updatable/DropTrafficRuleStatement.java
+++ b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-distsql/shardingsphere-traffic-distsql-statement/src/main/java/org/apache/shardingsphere/traffic/distsql/parser/statement/updatable/DropTrafficRuleStatement.java
@@ -15,11 +15,11 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.distsql.parser.statement.ral.updatable;
+package org.apache.shardingsphere.traffic.distsql.parser.statement.updatable;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
-import org.apache.shardingsphere.distsql.parser.statement.ral.UpdatableRALStatement;
+import org.apache.shardingsphere.distsql.parser.statement.ral.UpdatableGlobalRuleRALStatement;
import java.util.Collection;
@@ -28,7 +28,7 @@ import java.util.Collection;
*/
@RequiredArgsConstructor
@Getter
-public final class DropTrafficRuleStatement extends UpdatableRALStatement {
+public final class DropTrafficRuleStatement extends UpdatableGlobalRuleRALStatement {
private final boolean ifExists;
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/RALBackendHandlerFactory.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/RALBackendHandlerFactory.java
index 0dbac6a9967..aa73fd9dd87 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/RALBackendHandlerFactory.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/RALBackendHandlerFactory.java
@@ -22,6 +22,7 @@ import lombok.NoArgsConstructor;
import org.apache.shardingsphere.distsql.parser.statement.ral.HintRALStatement;
import org.apache.shardingsphere.distsql.parser.statement.ral.QueryableGlobalRuleRALStatement;
import org.apache.shardingsphere.distsql.parser.statement.ral.RALStatement;
+import org.apache.shardingsphere.distsql.parser.statement.ral.UpdatableGlobalRuleRALStatement;
import org.apache.shardingsphere.distsql.parser.statement.ral.queryable.ConvertYamlConfigurationStatement;
import org.apache.shardingsphere.distsql.parser.statement.ral.queryable.ExportDatabaseConfigurationStatement;
import org.apache.shardingsphere.distsql.parser.statement.ral.queryable.ShowAllVariableStatement;
@@ -36,12 +37,9 @@ import org.apache.shardingsphere.distsql.parser.statement.ral.queryable.ShowVari
import org.apache.shardingsphere.distsql.parser.statement.ral.scaling.QueryableScalingRALStatement;
import org.apache.shardingsphere.distsql.parser.statement.ral.scaling.UpdatableScalingRALStatement;
import org.apache.shardingsphere.distsql.parser.statement.ral.updatable.AlterSQLParserRuleStatement;
-import org.apache.shardingsphere.distsql.parser.statement.ral.updatable.AlterTrafficRuleStatement;
import org.apache.shardingsphere.distsql.parser.statement.ral.updatable.AlterTransactionRuleStatement;
import org.apache.shardingsphere.distsql.parser.statement.ral.updatable.ApplyDistSQLStatement;
-import org.apache.shardingsphere.distsql.parser.statement.ral.updatable.CreateTrafficRuleStatement;
import org.apache.shardingsphere.distsql.parser.statement.ral.updatable.DiscardDistSQLStatement;
-import org.apache.shardingsphere.distsql.parser.statement.ral.updatable.DropTrafficRuleStatement;
import org.apache.shardingsphere.distsql.parser.statement.ral.updatable.ImportDatabaseConfigurationStatement;
import org.apache.shardingsphere.distsql.parser.statement.ral.updatable.LabelInstanceStatement;
import org.apache.shardingsphere.distsql.parser.statement.ral.updatable.PrepareDistSQLStatement;
@@ -69,12 +67,9 @@ import org.apache.shardingsphere.proxy.backend.handler.distsql.ral.queryable.Sho
import org.apache.shardingsphere.proxy.backend.handler.distsql.ral.queryable.ShowTransactionRuleHandler;
import org.apache.shardingsphere.proxy.backend.handler.distsql.ral.queryable.ShowVariableHandler;
import org.apache.shardingsphere.proxy.backend.handler.distsql.ral.updatable.AlterSQLParserRuleHandler;
-import org.apache.shardingsphere.proxy.backend.handler.distsql.ral.updatable.AlterTrafficRuleHandler;
import org.apache.shardingsphere.proxy.backend.handler.distsql.ral.updatable.AlterTransactionRuleHandler;
import org.apache.shardingsphere.proxy.backend.handler.distsql.ral.updatable.ApplyDistSQLHandler;
-import org.apache.shardingsphere.proxy.backend.handler.distsql.ral.updatable.CreateTrafficRuleHandler;
import org.apache.shardingsphere.proxy.backend.handler.distsql.ral.updatable.DiscardDistSQLHandler;
-import org.apache.shardingsphere.proxy.backend.handler.distsql.ral.updatable.DropTrafficRuleHandler;
import org.apache.shardingsphere.proxy.backend.handler.distsql.ral.updatable.ImportDatabaseConfigurationHandler;
import org.apache.shardingsphere.proxy.backend.handler.distsql.ral.updatable.LabelInstanceHandler;
import org.apache.shardingsphere.proxy.backend.handler.distsql.ral.updatable.PrepareDistSQLHandler;
@@ -106,9 +101,6 @@ public final class RALBackendHandlerFactory {
HANDLERS.put(SetVariableStatement.class, SetVariableHandler.class);
HANDLERS.put(SetReadwriteSplittingStatusStatement.class, SetReadwriteSplittingStatusHandler.class);
HANDLERS.put(RefreshTableMetadataStatement.class, RefreshTableMetadataHandler.class);
- HANDLERS.put(CreateTrafficRuleStatement.class, CreateTrafficRuleHandler.class);
- HANDLERS.put(AlterTrafficRuleStatement.class, AlterTrafficRuleHandler.class);
- HANDLERS.put(DropTrafficRuleStatement.class, DropTrafficRuleHandler.class);
HANDLERS.put(AlterSQLParserRuleStatement.class, AlterSQLParserRuleHandler.class);
HANDLERS.put(AlterTransactionRuleStatement.class, AlterTransactionRuleHandler.class);
HANDLERS.put(PrepareDistSQLStatement.class, PrepareDistSQLHandler.class);
@@ -151,6 +143,9 @@ public final class RALBackendHandlerFactory {
if (sqlStatement instanceof QueryableGlobalRuleRALStatement) {
return QueryableGlobalRuleRALBackendHandlerFactory.newInstance((QueryableGlobalRuleRALStatement) sqlStatement);
}
+ if (sqlStatement instanceof UpdatableGlobalRuleRALStatement) {
+ return UpdatableGlobalRuleRALBackendHandlerFactory.newInstance((UpdatableGlobalRuleRALStatement) sqlStatement);
+ }
return createRALBackendHandler(sqlStatement, connectionSession);
}
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/UpdatableGlobalRuleRALBackendHandler.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/UpdatableGlobalRuleRALBackendHandler.java
new file mode 100644
index 00000000000..c141f4ee0b3
--- /dev/null
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/UpdatableGlobalRuleRALBackendHandler.java
@@ -0,0 +1,55 @@
+/*
+ * 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.handler.distsql.ral;
+
+import org.apache.shardingsphere.distsql.parser.statement.ral.RALStatement;
+import org.apache.shardingsphere.infra.distsql.update.GlobalRuleRALUpdater;
+import org.apache.shardingsphere.mode.metadata.MetaDataContexts;
+import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
+import org.apache.shardingsphere.proxy.backend.handler.ProxyBackendHandler;
+import org.apache.shardingsphere.proxy.backend.response.header.ResponseHeader;
+import org.apache.shardingsphere.proxy.backend.response.header.update.UpdateResponseHeader;
+
+import java.sql.SQLException;
+
+/**
+ * Updatable RAL backend handler for global rule.
+ */
+public final class UpdatableGlobalRuleRALBackendHandler implements ProxyBackendHandler {
+
+ private final RALStatement sqlStatement;
+
+ private final GlobalRuleRALUpdater updater;
+
+ public UpdatableGlobalRuleRALBackendHandler(final RALStatement sqlStatement, final GlobalRuleRALUpdater updater) {
+ this.sqlStatement = sqlStatement;
+ this.updater = updater;
+ }
+
+ @Override
+ public ResponseHeader execute() throws SQLException {
+ updater.executeUpdate(ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData().getGlobalRuleMetaData(), sqlStatement);
+ persistNewRuleConfigurations();
+ return new UpdateResponseHeader(sqlStatement);
+ }
+
+ private void persistNewRuleConfigurations() {
+ MetaDataContexts metaDataContexts = ProxyContext.getInstance().getContextManager().getMetaDataContexts();
+ metaDataContexts.getPersistService().getGlobalRuleService().persist(metaDataContexts.getMetaData().getGlobalRuleMetaData().getConfigurations(), true);
+ }
+}
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/UpdatableGlobalRuleRALBackendHandlerFactory.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/UpdatableGlobalRuleRALBackendHandlerFactory.java
new file mode 100644
index 00000000000..4c67041e460
--- /dev/null
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/UpdatableGlobalRuleRALBackendHandlerFactory.java
@@ -0,0 +1,50 @@
+/*
+ * 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.handler.distsql.ral;
+
+import lombok.AccessLevel;
+import lombok.NoArgsConstructor;
+import org.apache.shardingsphere.distsql.parser.statement.ral.UpdatableGlobalRuleRALStatement;
+import org.apache.shardingsphere.infra.distsql.update.GlobalRuleRALUpdater;
+import org.apache.shardingsphere.infra.util.spi.ShardingSphereServiceLoader;
+import org.apache.shardingsphere.infra.util.spi.type.typed.TypedSPIRegistry;
+import org.apache.shardingsphere.proxy.backend.handler.ProxyBackendHandler;
+
+import java.util.Properties;
+
+/**
+ * Updatable RAL backend handler factory for global rule.
+ */
+@NoArgsConstructor(access = AccessLevel.PRIVATE)
+public final class UpdatableGlobalRuleRALBackendHandlerFactory {
+
+ static {
+ ShardingSphereServiceLoader.register(GlobalRuleRALUpdater.class);
+ }
+
+ /**
+ * Create new instance of updatable RAL backend handler for global rule.
+ *
+ * @param sqlStatement updatable RAL statement for global rule
+ * @return created instance
+ */
+ public static ProxyBackendHandler newInstance(final UpdatableGlobalRuleRALStatement sqlStatement) {
+ GlobalRuleRALUpdater updater = TypedSPIRegistry.getRegisteredService(GlobalRuleRALUpdater.class, sqlStatement.getClass().getCanonicalName(), new Properties());
+ return new UpdatableGlobalRuleRALBackendHandler(sqlStatement, updater);
+ }
+}
diff --git a/shardingsphere-test/shardingsphere-parser-test/src/main/java/org/apache/shardingsphere/test/sql/parser/parameterized/asserts/statement/distsql/ral/impl/UpdatableRALStatementAssert.java b/shardingsphere-test/shardingsphere-parser-test/src/main/java/org/apache/shardingsphere/test/sql/parser/parameterized/asserts/statement/distsql/ral/impl/UpdatableRALStatementAssert.java
index 515b3349299..a8e07856cd8 100644
--- a/shardingsphere-test/shardingsphere-parser-test/src/main/java/org/apache/shardingsphere/test/sql/parser/parameterized/asserts/statement/distsql/ral/impl/UpdatableRALStatementAssert.java
+++ b/shardingsphere-test/shardingsphere-parser-test/src/main/java/org/apache/shardingsphere/test/sql/parser/parameterized/asserts/statement/distsql/ral/impl/UpdatableRALStatementAssert.java
@@ -22,9 +22,6 @@ import lombok.NoArgsConstructor;
import org.apache.shardingsphere.distsql.parser.statement.ral.UpdatableRALStatement;
import org.apache.shardingsphere.distsql.parser.statement.ral.updatable.AlterInstanceStatement;
import org.apache.shardingsphere.distsql.parser.statement.ral.updatable.AlterSQLParserRuleStatement;
-import org.apache.shardingsphere.distsql.parser.statement.ral.updatable.AlterTrafficRuleStatement;
-import org.apache.shardingsphere.distsql.parser.statement.ral.updatable.CreateTrafficRuleStatement;
-import org.apache.shardingsphere.distsql.parser.statement.ral.updatable.DropTrafficRuleStatement;
import org.apache.shardingsphere.distsql.parser.statement.ral.updatable.ImportDatabaseConfigurationStatement;
import org.apache.shardingsphere.distsql.parser.statement.ral.updatable.LabelInstanceStatement;
import org.apache.shardingsphere.distsql.parser.statement.ral.updatable.RefreshTableMetadataStatement;
@@ -52,6 +49,9 @@ import org.apache.shardingsphere.test.sql.parser.parameterized.jaxb.cases.domain
import org.apache.shardingsphere.test.sql.parser.parameterized.jaxb.cases.domain.statement.distsql.ral.RefreshTableMetadataStatementTestCase;
import org.apache.shardingsphere.test.sql.parser.parameterized.jaxb.cases.domain.statement.distsql.ral.SetVariableStatementTestCase;
import org.apache.shardingsphere.test.sql.parser.parameterized.jaxb.cases.domain.statement.distsql.ral.UnlabelInstanceStatementTestCase;
+import org.apache.shardingsphere.traffic.distsql.parser.statement.updatable.AlterTrafficRuleStatement;
+import org.apache.shardingsphere.traffic.distsql.parser.statement.updatable.CreateTrafficRuleStatement;
+import org.apache.shardingsphere.traffic.distsql.parser.statement.updatable.DropTrafficRuleStatement;
/**
* Updatable RAL statement assert.
diff --git a/shardingsphere-test/shardingsphere-parser-test/src/main/java/org/apache/shardingsphere/test/sql/parser/parameterized/asserts/statement/distsql/ral/impl/updatable/AlterTrafficRuleStatementAssert.java b/shardingsphere-test/shardingsphere-parser-test/src/main/java/org/apache/shardingsphere/test/sql/parser/parameterized/asserts/statement/distsql/ral/impl/updatable/AlterTrafficRuleStatementAssert.java
index 6304c801754..edd9e4c5e81 100644
--- a/shardingsphere-test/shardingsphere-parser-test/src/main/java/org/apache/shardingsphere/test/sql/parser/parameterized/asserts/statement/distsql/ral/impl/updatable/AlterTrafficRuleStatementAssert.java
+++ b/shardingsphere-test/shardingsphere-parser-test/src/main/java/org/apache/shardingsphere/test/sql/parser/parameterized/asserts/statement/distsql/ral/impl/updatable/AlterTrafficRuleStatementAssert.java
@@ -17,12 +17,12 @@
package org.apache.shardingsphere.test.sql.parser.parameterized.asserts.statement.distsql.ral.impl.updatable;
-import org.apache.shardingsphere.distsql.parser.segment.TrafficRuleSegment;
-import org.apache.shardingsphere.distsql.parser.statement.ral.updatable.AlterTrafficRuleStatement;
import org.apache.shardingsphere.test.sql.parser.parameterized.asserts.SQLCaseAssertContext;
import org.apache.shardingsphere.test.sql.parser.parameterized.asserts.segment.distsql.AlgorithmAssert;
import org.apache.shardingsphere.test.sql.parser.parameterized.jaxb.cases.domain.segment.impl.distsql.ral.ExpectedTrafficRule;
import org.apache.shardingsphere.test.sql.parser.parameterized.jaxb.cases.domain.statement.distsql.ral.AlterTrafficRuleStatementTestCase;
+import org.apache.shardingsphere.traffic.distsql.parser.segment.TrafficRuleSegment;
+import org.apache.shardingsphere.traffic.distsql.parser.statement.updatable.AlterTrafficRuleStatement;
import java.util.Collection;
import java.util.List;
diff --git a/shardingsphere-test/shardingsphere-parser-test/src/main/java/org/apache/shardingsphere/test/sql/parser/parameterized/asserts/statement/distsql/ral/impl/updatable/CreateTrafficRuleStatementAssert.java b/shardingsphere-test/shardingsphere-parser-test/src/main/java/org/apache/shardingsphere/test/sql/parser/parameterized/asserts/statement/distsql/ral/impl/updatable/CreateTrafficRuleStatementAssert.java
index 342c60a5291..cd0a3c024fc 100644
--- a/shardingsphere-test/shardingsphere-parser-test/src/main/java/org/apache/shardingsphere/test/sql/parser/parameterized/asserts/statement/distsql/ral/impl/updatable/CreateTrafficRuleStatementAssert.java
+++ b/shardingsphere-test/shardingsphere-parser-test/src/main/java/org/apache/shardingsphere/test/sql/parser/parameterized/asserts/statement/distsql/ral/impl/updatable/CreateTrafficRuleStatementAssert.java
@@ -17,12 +17,12 @@
package org.apache.shardingsphere.test.sql.parser.parameterized.asserts.statement.distsql.ral.impl.updatable;
-import org.apache.shardingsphere.distsql.parser.segment.TrafficRuleSegment;
-import org.apache.shardingsphere.distsql.parser.statement.ral.updatable.CreateTrafficRuleStatement;
import org.apache.shardingsphere.test.sql.parser.parameterized.asserts.SQLCaseAssertContext;
import org.apache.shardingsphere.test.sql.parser.parameterized.asserts.segment.distsql.AlgorithmAssert;
import org.apache.shardingsphere.test.sql.parser.parameterized.jaxb.cases.domain.segment.impl.distsql.ral.ExpectedTrafficRule;
import org.apache.shardingsphere.test.sql.parser.parameterized.jaxb.cases.domain.statement.distsql.ral.CreateTrafficRuleStatementTestCase;
+import org.apache.shardingsphere.traffic.distsql.parser.segment.TrafficRuleSegment;
+import org.apache.shardingsphere.traffic.distsql.parser.statement.updatable.CreateTrafficRuleStatement;
import java.util.Collection;
import java.util.List;
diff --git a/shardingsphere-test/shardingsphere-parser-test/src/main/java/org/apache/shardingsphere/test/sql/parser/parameterized/asserts/statement/distsql/ral/impl/updatable/DropTrafficRuleStatementAssert.java b/shardingsphere-test/shardingsphere-parser-test/src/main/java/org/apache/shardingsphere/test/sql/parser/parameterized/asserts/statement/distsql/ral/impl/updatable/DropTrafficRuleStatementAssert.java
index 7a75c05cf18..66ae72f8307 100644
--- a/shardingsphere-test/shardingsphere-parser-test/src/main/java/org/apache/shardingsphere/test/sql/parser/parameterized/asserts/statement/distsql/ral/impl/updatable/DropTrafficRuleStatementAssert.java
+++ b/shardingsphere-test/shardingsphere-parser-test/src/main/java/org/apache/shardingsphere/test/sql/parser/parameterized/asserts/statement/distsql/ral/impl/updatable/DropTrafficRuleStatementAssert.java
@@ -19,9 +19,9 @@ package org.apache.shardingsphere.test.sql.parser.parameterized.asserts.statemen
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
-import org.apache.shardingsphere.distsql.parser.statement.ral.updatable.DropTrafficRuleStatement;
import org.apache.shardingsphere.test.sql.parser.parameterized.asserts.SQLCaseAssertContext;
import org.apache.shardingsphere.test.sql.parser.parameterized.jaxb.cases.domain.statement.distsql.ral.DropTrafficRuleStatementTestCase;
+import org.apache.shardingsphere.traffic.distsql.parser.statement.updatable.DropTrafficRuleStatement;
import java.util.ArrayList;