This is an automated email from the ASF dual-hosted git repository.
yx9o 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 422d1f4894d Refactor UpdatableGlobalRuleRALBackendHandler (#24219)
422d1f4894d is described below
commit 422d1f4894dbe26325f04953f3611bce46a35c88
Author: Raigor <[email protected]>
AuthorDate: Fri Feb 17 18:13:27 2023 +0800
Refactor UpdatableGlobalRuleRALBackendHandler (#24219)
---
.../handler/ral/update/GlobalRuleRALUpdater.java | 26 +++-
.../update/AlterSQLParserRuleStatementUpdater.java | 32 ++---
.../AlterSQLParserRuleStatementUpdaterTest.java | 19 +--
.../update/AlterTrafficRuleStatementUpdater.java | 50 +++-----
.../update/CreateTrafficRuleStatementUpdater.java | 132 ---------------------
.../update/DropTrafficRuleStatementUpdater.java | 113 ------------------
...distsql.handler.ral.update.GlobalRuleRALUpdater | 2 -
.../AlterTrafficRuleStatementUpdaterTest.java | 62 +++-------
.../CreateTrafficRuleStatementUpdaterTest.java | 132 ---------------------
.../DropTrafficRuleStatementUpdaterTest.java | 96 ---------------
.../AlterTransactionRuleStatementUpdater.java | 37 +++---
.../AlterTransactionRuleStatementUpdaterTest.java | 53 ++-------
.../distsql/ral/RALBackendHandlerFactory.java | 3 +-
.../ral/UpdatableGlobalRuleRALBackendHandler.java | 38 +++++-
14 files changed, 129 insertions(+), 666 deletions(-)
diff --git
a/distsql/handler/src/main/java/org/apache/shardingsphere/distsql/handler/ral/update/GlobalRuleRALUpdater.java
b/distsql/handler/src/main/java/org/apache/shardingsphere/distsql/handler/ral/update/GlobalRuleRALUpdater.java
index b043b5b12c2..7d0668ceced 100644
---
a/distsql/handler/src/main/java/org/apache/shardingsphere/distsql/handler/ral/update/GlobalRuleRALUpdater.java
+++
b/distsql/handler/src/main/java/org/apache/shardingsphere/distsql/handler/ral/update/GlobalRuleRALUpdater.java
@@ -17,7 +17,7 @@
package org.apache.shardingsphere.distsql.handler.ral.update;
-import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
+import org.apache.shardingsphere.infra.config.rule.RuleConfiguration;
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;
@@ -26,13 +26,29 @@ import
org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
* RAL updater for global rule.
*/
@SingletonSPI
-public interface GlobalRuleRALUpdater extends TypedSPI {
+public interface GlobalRuleRALUpdater<T extends SQLStatement, R extends
RuleConfiguration> extends TypedSPI {
/**
- * Execute update.
+ * Check SQL statement.
*
- * @param metaData meta data
+ * @param currentRuleConfig current rule configuration
* @param sqlStatement SQL statement
*/
- void executeUpdate(ShardingSphereMetaData metaData, SQLStatement
sqlStatement);
+ void checkSQLStatement(R currentRuleConfig, T sqlStatement);
+
+ /**
+ * Build altered rule configuration.
+ *
+ * @param currentRuleConfig current rule configuration
+ * @param sqlStatement SQL statement
+ * @return built altered rule configuration
+ */
+ RuleConfiguration buildAlteredRuleConfiguration(R currentRuleConfig, T
sqlStatement);
+
+ /**
+ * Get rule configuration class.
+ *
+ * @return rule configuration class
+ */
+ Class<R> getRuleConfigurationClass();
}
diff --git
a/kernel/parser/distsql/handler/src/main/java/org/apache/shardingsphere/parser/distsql/handler/update/AlterSQLParserRuleStatementUpdater.java
b/kernel/parser/distsql/handler/src/main/java/org/apache/shardingsphere/parser/distsql/handler/update/AlterSQLParserRuleStatementUpdater.java
index de2c929c180..29a24604d2a 100644
---
a/kernel/parser/distsql/handler/src/main/java/org/apache/shardingsphere/parser/distsql/handler/update/AlterSQLParserRuleStatementUpdater.java
+++
b/kernel/parser/distsql/handler/src/main/java/org/apache/shardingsphere/parser/distsql/handler/update/AlterSQLParserRuleStatementUpdater.java
@@ -18,39 +18,28 @@
package org.apache.shardingsphere.parser.distsql.handler.update;
import
org.apache.shardingsphere.distsql.handler.ral.update.GlobalRuleRALUpdater;
-import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
-import
org.apache.shardingsphere.infra.metadata.database.rule.ShardingSphereRuleMetaData;
-import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
import org.apache.shardingsphere.parser.config.SQLParserRuleConfiguration;
import
org.apache.shardingsphere.parser.distsql.parser.segment.CacheOptionSegment;
import
org.apache.shardingsphere.parser.distsql.parser.statement.updatable.AlterSQLParserRuleStatement;
-import org.apache.shardingsphere.parser.rule.SQLParserRule;
import org.apache.shardingsphere.sql.parser.api.CacheOption;
-import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
-
-import java.util.Collection;
/**
* Alter SQL parser rule statement handler.
*/
-public final class AlterSQLParserRuleStatementUpdater implements
GlobalRuleRALUpdater {
+public final class AlterSQLParserRuleStatementUpdater implements
GlobalRuleRALUpdater<AlterSQLParserRuleStatement, SQLParserRuleConfiguration> {
@Override
- public void executeUpdate(final ShardingSphereMetaData metaData, final
SQLStatement sqlStatement) {
- SQLParserRuleConfiguration toBeAlteredRuleConfig =
createToBeAlteredRuleConfiguration(metaData.getGlobalRuleMetaData(),
sqlStatement);
- Collection<ShardingSphereRule> globalRules =
metaData.getGlobalRuleMetaData().getRules();
- globalRules.removeIf(each -> each instanceof SQLParserRule);
- globalRules.add(new SQLParserRule(toBeAlteredRuleConfig));
+ public void checkSQLStatement(final SQLParserRuleConfiguration
currentRuleConfig, final AlterSQLParserRuleStatement sqlStatement) {
}
- private SQLParserRuleConfiguration
createToBeAlteredRuleConfiguration(final ShardingSphereRuleMetaData
ruleMetaData, final SQLStatement sqlStatement) {
- AlterSQLParserRuleStatement ruleStatement =
(AlterSQLParserRuleStatement) sqlStatement;
- SQLParserRuleConfiguration currentConfig =
ruleMetaData.getSingleRule(SQLParserRule.class).getConfiguration();
- boolean sqlCommentParseEnabled = null ==
ruleStatement.getSqlCommentParseEnable() ?
currentConfig.isSqlCommentParseEnabled() :
ruleStatement.getSqlCommentParseEnable();
+ @Override
+ public SQLParserRuleConfiguration buildAlteredRuleConfiguration(final
SQLParserRuleConfiguration currentRuleConfig, final AlterSQLParserRuleStatement
sqlStatement) {
+ boolean sqlCommentParseEnabled = null ==
sqlStatement.getSqlCommentParseEnable() ?
currentRuleConfig.isSqlCommentParseEnabled() :
sqlStatement.getSqlCommentParseEnable();
CacheOption parseTreeCache =
- null == ruleStatement.getParseTreeCache() ?
currentConfig.getParseTreeCache() :
createCacheOption(currentConfig.getParseTreeCache(),
ruleStatement.getParseTreeCache());
+ null == sqlStatement.getParseTreeCache() ?
currentRuleConfig.getParseTreeCache() :
createCacheOption(currentRuleConfig.getParseTreeCache(),
sqlStatement.getParseTreeCache());
CacheOption sqlStatementCache =
- null == ruleStatement.getSqlStatementCache() ?
currentConfig.getSqlStatementCache() :
createCacheOption(currentConfig.getSqlStatementCache(),
ruleStatement.getSqlStatementCache());
+ null == sqlStatement.getSqlStatementCache() ?
currentRuleConfig.getSqlStatementCache()
+ :
createCacheOption(currentRuleConfig.getSqlStatementCache(),
sqlStatement.getSqlStatementCache());
return new SQLParserRuleConfiguration(sqlCommentParseEnabled,
parseTreeCache, sqlStatementCache);
}
@@ -60,6 +49,11 @@ public final class AlterSQLParserRuleStatementUpdater
implements GlobalRuleRALUp
return new CacheOption(initialCapacity, maximumSize);
}
+ @Override
+ public Class<SQLParserRuleConfiguration> getRuleConfigurationClass() {
+ return SQLParserRuleConfiguration.class;
+ }
+
@Override
public String getType() {
return AlterSQLParserRuleStatement.class.getName();
diff --git
a/kernel/parser/distsql/handler/src/test/java/org/apache/shardingsphere/parser/distsql/handler/update/AlterSQLParserRuleStatementUpdaterTest.java
b/kernel/parser/distsql/handler/src/test/java/org/apache/shardingsphere/parser/distsql/handler/update/AlterSQLParserRuleStatementUpdaterTest.java
index 00fc4563ab0..1148084d24a 100644
---
a/kernel/parser/distsql/handler/src/test/java/org/apache/shardingsphere/parser/distsql/handler/update/AlterSQLParserRuleStatementUpdaterTest.java
+++
b/kernel/parser/distsql/handler/src/test/java/org/apache/shardingsphere/parser/distsql/handler/update/AlterSQLParserRuleStatementUpdaterTest.java
@@ -17,19 +17,12 @@
package org.apache.shardingsphere.parser.distsql.handler.update;
-import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
-import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
-import
org.apache.shardingsphere.infra.metadata.database.rule.ShardingSphereRuleMetaData;
+import org.apache.shardingsphere.parser.config.SQLParserRuleConfiguration;
import
org.apache.shardingsphere.parser.distsql.parser.segment.CacheOptionSegment;
import
org.apache.shardingsphere.parser.distsql.parser.statement.updatable.AlterSQLParserRuleStatement;
-import org.apache.shardingsphere.parser.rule.SQLParserRule;
import
org.apache.shardingsphere.parser.rule.builder.DefaultSQLParserRuleConfigurationBuilder;
import org.junit.Test;
-import java.util.Collections;
-import java.util.LinkedList;
-import java.util.Properties;
-
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertTrue;
@@ -40,9 +33,7 @@ public final class AlterSQLParserRuleStatementUpdaterTest {
public void assertExecute() {
AlterSQLParserRuleStatementUpdater updater = new
AlterSQLParserRuleStatementUpdater();
AlterSQLParserRuleStatement sqlStatement = new
AlterSQLParserRuleStatement(true, new CacheOptionSegment(64, 512L), new
CacheOptionSegment(1000, 1000L));
- ShardingSphereMetaData metaData = createMetaData();
- updater.executeUpdate(metaData, sqlStatement);
- SQLParserRule actual =
metaData.getGlobalRuleMetaData().getSingleRule(SQLParserRule.class);
+ SQLParserRuleConfiguration actual =
updater.buildAlteredRuleConfiguration(getSQLParserRuleConfiguration(),
sqlStatement);
assertTrue(actual.isSqlCommentParseEnabled());
assertThat(actual.getSqlStatementCache().getInitialCapacity(),
is(1000));
assertThat(actual.getSqlStatementCache().getMaximumSize(), is(1000L));
@@ -50,9 +41,7 @@ public final class AlterSQLParserRuleStatementUpdaterTest {
assertThat(actual.getParseTreeCache().getMaximumSize(), is(512L));
}
- private ShardingSphereMetaData createMetaData() {
- SQLParserRule rule = new SQLParserRule(new
DefaultSQLParserRuleConfigurationBuilder().build());
- ShardingSphereRuleMetaData ruleMetaData = new
ShardingSphereRuleMetaData(new LinkedList<>(Collections.singleton(rule)));
- return new ShardingSphereMetaData(Collections.emptyMap(),
ruleMetaData, new ConfigurationProperties(new Properties()));
+ private SQLParserRuleConfiguration getSQLParserRuleConfiguration() {
+ return new DefaultSQLParserRuleConfigurationBuilder().build();
}
}
diff --git
a/kernel/traffic/distsql/handler/src/main/java/org/apache/shardingsphere/traffic/distsql/handler/update/AlterTrafficRuleStatementUpdater.java
b/kernel/traffic/distsql/handler/src/main/java/org/apache/shardingsphere/traffic/distsql/handler/update/AlterTrafficRuleStatementUpdater.java
index eabd2ee3c72..730e65c79e0 100644
---
a/kernel/traffic/distsql/handler/src/main/java/org/apache/shardingsphere/traffic/distsql/handler/update/AlterTrafficRuleStatementUpdater.java
+++
b/kernel/traffic/distsql/handler/src/main/java/org/apache/shardingsphere/traffic/distsql/handler/update/AlterTrafficRuleStatementUpdater.java
@@ -21,19 +21,13 @@ import
org.apache.shardingsphere.distsql.handler.exception.algorithm.InvalidAlgo
import
org.apache.shardingsphere.distsql.handler.exception.rule.MissingRequiredRuleException;
import
org.apache.shardingsphere.distsql.handler.ral.update.GlobalRuleRALUpdater;
import org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration;
-import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
-import
org.apache.shardingsphere.infra.metadata.database.rule.ShardingSphereRuleMetaData;
-import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
import
org.apache.shardingsphere.infra.util.exception.ShardingSpherePreconditions;
import org.apache.shardingsphere.infra.util.spi.type.typed.TypedSPILoader;
-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.rule.TrafficRule;
-import org.apache.shardingsphere.traffic.rule.TrafficStrategyRule;
import org.apache.shardingsphere.traffic.spi.TrafficAlgorithm;
import org.apache.shardingsphere.traffic.spi.TrafficLoadBalanceAlgorithm;
@@ -46,28 +40,21 @@ import java.util.stream.Collectors;
/**
* Alter traffic rule statement updater.
*/
-public final class AlterTrafficRuleStatementUpdater implements
GlobalRuleRALUpdater {
+public final class AlterTrafficRuleStatementUpdater implements
GlobalRuleRALUpdater<AlterTrafficRuleStatement, TrafficRuleConfiguration> {
@Override
- public void executeUpdate(final ShardingSphereMetaData metaData, final
SQLStatement sqlStatement) {
- AlterTrafficRuleStatement statement = (AlterTrafficRuleStatement)
sqlStatement;
- check(metaData.getGlobalRuleMetaData(), statement);
- replaceNewRule(metaData.getGlobalRuleMetaData(), statement);
- }
-
- private void check(final ShardingSphereRuleMetaData ruleMetaData, final
AlterTrafficRuleStatement sqlStatement) {
- checkRuleNames(ruleMetaData, sqlStatement);
+ public void checkSQLStatement(final TrafficRuleConfiguration
currentRuleConfig, final AlterTrafficRuleStatement sqlStatement) {
+ checkRuleNames(currentRuleConfig, sqlStatement);
checkAlgorithmNames(sqlStatement);
}
- private void checkRuleNames(final ShardingSphereRuleMetaData ruleMetaData,
final AlterTrafficRuleStatement sqlStatement) {
- Collection<String> notExistRuleNames =
getNotExistRuleNames(ruleMetaData, sqlStatement);
+ private void checkRuleNames(final TrafficRuleConfiguration
currentRuleConfig, final AlterTrafficRuleStatement sqlStatement) {
+ Collection<String> notExistRuleNames =
getNotExistRuleNames(currentRuleConfig, sqlStatement);
ShardingSpherePreconditions.checkState(notExistRuleNames.isEmpty(), ()
-> new MissingRequiredRuleException("Traffic", notExistRuleNames));
}
- 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());
+ private Collection<String> getNotExistRuleNames(final
TrafficRuleConfiguration currentRuleConfig, final AlterTrafficRuleStatement
sqlStatement) {
+ Collection<String> currentRuleNames =
currentRuleConfig.getTrafficStrategies().stream().map(TrafficStrategyConfiguration::getName).collect(Collectors.toSet());
return
sqlStatement.getSegments().stream().map(TrafficRuleSegment::getName).filter(each
-> !currentRuleNames.contains(each)).collect(Collectors.toSet());
}
@@ -89,21 +76,13 @@ public final class AlterTrafficRuleStatementUpdater
implements GlobalRuleRALUpda
return result;
}
- 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(final
ShardingSphereRuleMetaData ruleMetaData, final AlterTrafficRuleStatement
sqlStatement) {
+ @Override
+ public TrafficRuleConfiguration buildAlteredRuleConfiguration(final
TrafficRuleConfiguration currentRuleConfig, final AlterTrafficRuleStatement
sqlStatement) {
TrafficRuleConfiguration result = new TrafficRuleConfiguration();
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)));
+
result.getTrafficStrategies().addAll(createToBeAlteredStrategyConfigurations(currentRuleConfig,
configFromSQLStatement));
+
result.getTrafficAlgorithms().putAll(createToBeAlteredTrafficAlgorithms(currentRuleConfig,
configFromSQLStatement, getInUsedTrafficAlgorithm(result)));
+
result.getLoadBalancers().putAll(createToBeAlteredLoadBalancers(currentRuleConfig,
configFromSQLStatement, getInUsedLoadBalancer(result)));
return result;
}
@@ -143,6 +122,11 @@ public final class AlterTrafficRuleStatementUpdater
implements GlobalRuleRALUpda
return result;
}
+ @Override
+ public Class<TrafficRuleConfiguration> getRuleConfigurationClass() {
+ return TrafficRuleConfiguration.class;
+ }
+
@Override
public String getType() {
return AlterTrafficRuleStatement.class.getName();
diff --git
a/kernel/traffic/distsql/handler/src/main/java/org/apache/shardingsphere/traffic/distsql/handler/update/CreateTrafficRuleStatementUpdater.java
b/kernel/traffic/distsql/handler/src/main/java/org/apache/shardingsphere/traffic/distsql/handler/update/CreateTrafficRuleStatementUpdater.java
deleted file mode 100644
index 80c4a744912..00000000000
---
a/kernel/traffic/distsql/handler/src/main/java/org/apache/shardingsphere/traffic/distsql/handler/update/CreateTrafficRuleStatementUpdater.java
+++ /dev/null
@@ -1,132 +0,0 @@
-/*
- * 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.traffic.distsql.handler.update;
-
-import
org.apache.shardingsphere.distsql.handler.exception.algorithm.InvalidAlgorithmConfigurationException;
-import
org.apache.shardingsphere.distsql.handler.exception.rule.DuplicateRuleException;
-import
org.apache.shardingsphere.distsql.handler.ral.update.GlobalRuleRALUpdater;
-import org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration;
-import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
-import
org.apache.shardingsphere.infra.metadata.database.rule.ShardingSphereRuleMetaData;
-import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
-import
org.apache.shardingsphere.infra.util.exception.ShardingSpherePreconditions;
-import org.apache.shardingsphere.infra.util.spi.type.typed.TypedSPILoader;
-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.rule.TrafficRule;
-import org.apache.shardingsphere.traffic.rule.TrafficStrategyRule;
-import org.apache.shardingsphere.traffic.spi.TrafficAlgorithm;
-import org.apache.shardingsphere.traffic.spi.TrafficLoadBalanceAlgorithm;
-
-import java.util.Collection;
-import java.util.LinkedHashMap;
-import java.util.LinkedList;
-import java.util.Map;
-import java.util.stream.Collectors;
-
-/**
- * Create traffic rule statement updater.
- */
-public final class CreateTrafficRuleStatementUpdater implements
GlobalRuleRALUpdater {
-
- @Override
- public void executeUpdate(final ShardingSphereMetaData metaData, final
SQLStatement sqlStatement) {
- CreateTrafficRuleStatement statement = (CreateTrafficRuleStatement)
sqlStatement;
- check(metaData.getGlobalRuleMetaData(), statement);
- replaceNewRule(metaData.getGlobalRuleMetaData(), statement);
- }
-
- private void check(final ShardingSphereRuleMetaData ruleMetaData, final
CreateTrafficRuleStatement sqlStatement) {
- checkRuleNames(ruleMetaData, sqlStatement);
- checkAlgorithmNames(sqlStatement);
- }
-
- private void checkRuleNames(final ShardingSphereRuleMetaData ruleMetaData,
final CreateTrafficRuleStatement sqlStatement) {
- Collection<String> inUsedRuleNames = getInUsedRuleNames(ruleMetaData,
sqlStatement);
- ShardingSpherePreconditions.checkState(inUsedRuleNames.isEmpty(), ()
-> new DuplicateRuleException("Traffic", inUsedRuleNames));
- }
-
- 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
sqlStatement.getSegments().stream().map(TrafficRuleSegment::getName).filter(currentRuleNames::contains).collect(Collectors.toSet());
- }
-
- private void checkAlgorithmNames(final CreateTrafficRuleStatement
sqlStatement) {
- Collection<String> invalidAlgorithmNames =
getInvalidAlgorithmNames(sqlStatement);
-
ShardingSpherePreconditions.checkState(invalidAlgorithmNames.isEmpty(), () ->
new InvalidAlgorithmConfigurationException("Traffic", invalidAlgorithmNames));
- }
-
- private Collection<String> getInvalidAlgorithmNames(final
CreateTrafficRuleStatement sqlStatement) {
- Collection<String> result = new LinkedList<>();
- for (TrafficRuleSegment each : sqlStatement.getSegments()) {
- if (!TypedSPILoader.contains(TrafficAlgorithm.class,
each.getAlgorithm().getName())) {
- result.add(each.getAlgorithm().getName());
- }
- if (null != each.getLoadBalancer() &&
!TypedSPILoader.contains(TrafficLoadBalanceAlgorithm.class,
each.getLoadBalancer().getName())) {
- result.add(each.getLoadBalancer().getName());
- }
- }
- return result;
- }
-
- 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(final
ShardingSphereRuleMetaData ruleMetaData, final CreateTrafficRuleStatement
sqlStatement) {
- TrafficRuleConfiguration result = new TrafficRuleConfiguration();
- 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));
- return result;
- }
-
- private Collection<TrafficStrategyConfiguration>
createToBeAlteredStrategyConfigurations(final TrafficRuleConfiguration
currentConfig, final TrafficRuleConfiguration configFromSQLStatement) {
- Collection<TrafficStrategyConfiguration> result = new
LinkedList<>(currentConfig.getTrafficStrategies());
- result.addAll(configFromSQLStatement.getTrafficStrategies());
- return result;
- }
-
- private Map<String, AlgorithmConfiguration>
createToBeAlteredTrafficAlgorithms(final TrafficRuleConfiguration
currentConfig, final TrafficRuleConfiguration configFromSQLStatement) {
- Map<String, AlgorithmConfiguration> result = new
LinkedHashMap<>(currentConfig.getTrafficAlgorithms());
- result.putAll(configFromSQLStatement.getTrafficAlgorithms());
- return result;
- }
-
- private Map<String, AlgorithmConfiguration>
createToBeAlteredLoadBalancers(final TrafficRuleConfiguration currentConfig,
final TrafficRuleConfiguration configFromSQLStatement) {
- Map<String, AlgorithmConfiguration> result = new
LinkedHashMap<>(currentConfig.getLoadBalancers());
- result.putAll(configFromSQLStatement.getLoadBalancers());
- return result;
- }
-
- @Override
- public String getType() {
- return CreateTrafficRuleStatement.class.getName();
- }
-}
diff --git
a/kernel/traffic/distsql/handler/src/main/java/org/apache/shardingsphere/traffic/distsql/handler/update/DropTrafficRuleStatementUpdater.java
b/kernel/traffic/distsql/handler/src/main/java/org/apache/shardingsphere/traffic/distsql/handler/update/DropTrafficRuleStatementUpdater.java
deleted file mode 100644
index 6bb57fb510b..00000000000
---
a/kernel/traffic/distsql/handler/src/main/java/org/apache/shardingsphere/traffic/distsql/handler/update/DropTrafficRuleStatementUpdater.java
+++ /dev/null
@@ -1,113 +0,0 @@
-/*
- * 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.traffic.distsql.handler.update;
-
-import
org.apache.shardingsphere.distsql.handler.exception.rule.MissingRequiredRuleException;
-import
org.apache.shardingsphere.distsql.handler.ral.update.GlobalRuleRALUpdater;
-import org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration;
-import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
-import
org.apache.shardingsphere.infra.metadata.database.rule.ShardingSphereRuleMetaData;
-import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
-import
org.apache.shardingsphere.infra.util.exception.ShardingSpherePreconditions;
-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;
-import java.util.LinkedHashMap;
-import java.util.Map;
-import java.util.stream.Collectors;
-
-/**
- * Drop traffic rule statement updater.
- */
-public final class DropTrafficRuleStatementUpdater implements
GlobalRuleRALUpdater {
-
- @Override
- public void executeUpdate(final ShardingSphereMetaData metaData, final
SQLStatement sqlStatement) {
- DropTrafficRuleStatement statement = (DropTrafficRuleStatement)
sqlStatement;
- check(metaData.getGlobalRuleMetaData(), statement);
- replaceNewRule(metaData.getGlobalRuleMetaData(), statement);
- }
-
- private void check(final ShardingSphereRuleMetaData ruleMetaData, final
DropTrafficRuleStatement sqlStatement) {
- checkRuleNames(ruleMetaData, sqlStatement);
- }
-
- private void checkRuleNames(final ShardingSphereRuleMetaData ruleMetaData,
final DropTrafficRuleStatement sqlStatement) {
- if (sqlStatement.isIfExists()) {
- return;
- }
- 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 =
sqlStatement.getRuleNames().stream().filter(each ->
!currentRuleNames.contains(each)).collect(Collectors.toSet());
- ShardingSpherePreconditions.checkState(notExistRuleNames.isEmpty(), ()
-> new MissingRequiredRuleException("Traffic"));
- }
-
- 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(final
ShardingSphereRuleMetaData ruleMetaData, final DropTrafficRuleStatement
sqlStatement) {
- TrafficRuleConfiguration result = new TrafficRuleConfiguration();
- 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;
- }
-
- private Collection<TrafficStrategyConfiguration>
createToBeAlteredStrategyConfigurations(final TrafficRuleConfiguration
currentConfig, final DropTrafficRuleStatement statement) {
- return currentConfig.getTrafficStrategies().stream().filter(each ->
!statement.getRuleNames().contains(each.getName())).collect(Collectors.toList());
- }
-
- private Collection<String> getInUsedTrafficAlgorithm(final
TrafficRuleConfiguration config) {
- return
config.getTrafficStrategies().stream().map(TrafficStrategyConfiguration::getAlgorithmName).collect(Collectors.toSet());
- }
-
- private Map<String, AlgorithmConfiguration>
createToBeAlteredTrafficAlgorithm(final TrafficRuleConfiguration currentConfig,
final Collection<String> inUsedTrafficAlgorithm) {
- Map<String, AlgorithmConfiguration> result = new
LinkedHashMap<>(currentConfig.getTrafficAlgorithms());
- for (String each : result.keySet().stream().filter(each ->
!inUsedTrafficAlgorithm.contains(each)).collect(Collectors.toSet())) {
- result.remove(each);
- }
- return result;
- }
-
- private Collection<String> getInUsedLoadBalancer(final
TrafficRuleConfiguration config) {
- return
config.getTrafficStrategies().stream().map(TrafficStrategyConfiguration::getLoadBalancerName).collect(Collectors.toSet());
- }
-
- private Map<String, AlgorithmConfiguration>
createToBeAlteredLoadBalancers(final TrafficRuleConfiguration currentConfig,
final Collection<String> inUsedLoadBalancer) {
- Map<String, AlgorithmConfiguration> result = new
LinkedHashMap<>(currentConfig.getLoadBalancers());
- for (String each : result.keySet().stream().filter(each ->
!inUsedLoadBalancer.contains(each)).collect(Collectors.toSet())) {
- result.remove(each);
- }
- return result;
- }
-
- @Override
- public String getType() {
- return DropTrafficRuleStatement.class.getName();
- }
-}
diff --git
a/kernel/traffic/distsql/handler/src/main/resources/META-INF/services/org.apache.shardingsphere.distsql.handler.ral.update.GlobalRuleRALUpdater
b/kernel/traffic/distsql/handler/src/main/resources/META-INF/services/org.apache.shardingsphere.distsql.handler.ral.update.GlobalRuleRALUpdater
index 31cd47aa82d..7ac314a7ead 100644
---
a/kernel/traffic/distsql/handler/src/main/resources/META-INF/services/org.apache.shardingsphere.distsql.handler.ral.update.GlobalRuleRALUpdater
+++
b/kernel/traffic/distsql/handler/src/main/resources/META-INF/services/org.apache.shardingsphere.distsql.handler.ral.update.GlobalRuleRALUpdater
@@ -15,6 +15,4 @@
# limitations under the License.
#
-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/kernel/traffic/distsql/handler/src/test/java/org/apache/shardingsphere/traffic/distsql/handler/update/AlterTrafficRuleStatementUpdaterTest.java
b/kernel/traffic/distsql/handler/src/test/java/org/apache/shardingsphere/traffic/distsql/handler/update/AlterTrafficRuleStatementUpdaterTest.java
index 96341023bc3..8782d305412 100644
---
a/kernel/traffic/distsql/handler/src/test/java/org/apache/shardingsphere/traffic/distsql/handler/update/AlterTrafficRuleStatementUpdaterTest.java
+++
b/kernel/traffic/distsql/handler/src/test/java/org/apache/shardingsphere/traffic/distsql/handler/update/AlterTrafficRuleStatementUpdaterTest.java
@@ -21,98 +21,66 @@ import
org.apache.shardingsphere.distsql.handler.exception.algorithm.InvalidAlgo
import
org.apache.shardingsphere.distsql.handler.exception.rule.MissingRequiredRuleException;
import org.apache.shardingsphere.distsql.parser.segment.AlgorithmSegment;
import org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration;
-import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
-import
org.apache.shardingsphere.infra.metadata.database.rule.ShardingSphereRuleMetaData;
import org.apache.shardingsphere.test.util.PropertiesBuilder;
import org.apache.shardingsphere.test.util.PropertiesBuilder.Property;
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;
import java.util.Arrays;
import java.util.Collections;
-import java.util.LinkedList;
import java.util.Properties;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertNotNull;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
public final class AlterTrafficRuleStatementUpdaterTest {
@Test(expected = MissingRequiredRuleException.class)
public void assertExecuteWithNotExistRuleName() {
- ShardingSphereMetaData metaData = createMetaData();
TrafficRuleSegment trafficRuleSegment = new TrafficRuleSegment(
"rule_name_3", Arrays.asList("olap", "order_by"), new
AlgorithmSegment("DISTSQL.FIXTURE", new Properties()), new
AlgorithmSegment("DISTSQL.FIXTURE", new Properties()));
AlterTrafficRuleStatementUpdater updater = new
AlterTrafficRuleStatementUpdater();
- updater.executeUpdate(metaData, new
AlterTrafficRuleStatement(Collections.singleton(trafficRuleSegment)));
+ updater.checkSQLStatement(createTrafficRuleConfiguration(), new
AlterTrafficRuleStatement(Collections.singleton(trafficRuleSegment)));
}
@Test(expected = InvalidAlgorithmConfigurationException.class)
public void assertExecuteWithInvalidAlgorithmType() {
- ShardingSphereMetaData metaData = createMetaData();
TrafficRuleSegment trafficRuleSegment = new TrafficRuleSegment(
"rule_name_1", Arrays.asList("olap", "order_by"), new
AlgorithmSegment("invalid", new Properties()), new AlgorithmSegment("invalid",
new Properties()));
AlterTrafficRuleStatementUpdater updater = new
AlterTrafficRuleStatementUpdater();
- updater.executeUpdate(metaData, new
AlterTrafficRuleStatement(Collections.singleton(trafficRuleSegment)));
+ updater.checkSQLStatement(createTrafficRuleConfiguration(), new
AlterTrafficRuleStatement(Collections.singleton(trafficRuleSegment)));
}
- @Test(expected = IllegalStateException.class)
+ @Test
public void assertExecuteWithLoadBalancerCannotBeNull() {
- ShardingSphereMetaData metaData = createMetaData();
TrafficRuleSegment trafficRuleSegment = new
TrafficRuleSegment("rule_name_1", Arrays.asList("olap", "order_by"),
new AlgorithmSegment("DISTSQL.FIXTURE", new Properties()),
null);
AlterTrafficRuleStatementUpdater updater = new
AlterTrafficRuleStatementUpdater();
- try {
- updater.executeUpdate(metaData, new
AlterTrafficRuleStatement(Collections.singleton(trafficRuleSegment)));
- } catch (final IllegalStateException ex) {
- TrafficRule currentRule =
metaData.getGlobalRuleMetaData().getSingleRule(TrafficRule.class);
- assertNotNull(currentRule);
- throw ex;
- }
+ TrafficRuleConfiguration actual =
updater.buildAlteredRuleConfiguration(createTrafficRuleConfiguration(), new
AlterTrafficRuleStatement(Collections.singleton(trafficRuleSegment)));
+ assertThat(actual.getTrafficStrategies().size(), is(2));
+ assertThat(actual.getTrafficAlgorithms().size(), is(2));
+ assertThat(actual.getLoadBalancers().size(), is(1));
}
@Test
public void assertExecute() {
- ShardingSphereMetaData metaData = createMetaData();
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()));
AlterTrafficRuleStatementUpdater updater = new
AlterTrafficRuleStatementUpdater();
- updater.executeUpdate(metaData, new
AlterTrafficRuleStatement(Arrays.asList(trafficRuleSegment1,
trafficRuleSegment2)));
- TrafficRuleConfiguration alteredConfig =
metaData.getGlobalRuleMetaData().getSingleRule(TrafficRule.class).getConfiguration();
- assertThat(alteredConfig.getTrafficStrategies().size(), is(2));
- assertThat(alteredConfig.getLoadBalancers().size(), is(2));
- assertThat(alteredConfig.getTrafficAlgorithms().size(), is(2));
-
assertThat(alteredConfig.getTrafficStrategies().iterator().next().getName(),
is("rule_name_1"));
-
assertNotNull(alteredConfig.getTrafficAlgorithms().get("rule_name_1_distsql.fixture"));
-
assertNotNull(alteredConfig.getLoadBalancers().get("rule_name_2_distsql.fixture"));
- }
-
- private ShardingSphereMetaData createMetaData() {
- ShardingSphereMetaData metaData = mock(ShardingSphereMetaData.class);
- ShardingSphereRuleMetaData ruleMetaData = new
ShardingSphereRuleMetaData(new
LinkedList<>(Collections.singleton(mockTrafficRule())));
- when(metaData.getGlobalRuleMetaData()).thenReturn(ruleMetaData);
- return metaData;
- }
-
- 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);
- when(strategyRule2.getName()).thenReturn("rule_name_2");
-
when(result.getStrategyRules()).thenReturn(Arrays.asList(strategyRule1,
strategyRule2));
- return result;
+ TrafficRuleConfiguration actual =
+
updater.buildAlteredRuleConfiguration(createTrafficRuleConfiguration(), new
AlterTrafficRuleStatement(Arrays.asList(trafficRuleSegment1,
trafficRuleSegment2)));
+ assertThat(actual.getTrafficStrategies().size(), is(2));
+ assertThat(actual.getTrafficAlgorithms().size(), is(2));
+ assertThat(actual.getLoadBalancers().size(), is(2));
+ assertThat(actual.getTrafficStrategies().iterator().next().getName(),
is("rule_name_1"));
+
assertNotNull(actual.getTrafficAlgorithms().get("rule_name_1_distsql.fixture"));
+
assertNotNull(actual.getLoadBalancers().get("rule_name_2_distsql.fixture"));
}
private TrafficRuleConfiguration createTrafficRuleConfiguration() {
diff --git
a/kernel/traffic/distsql/handler/src/test/java/org/apache/shardingsphere/traffic/distsql/handler/update/CreateTrafficRuleStatementUpdaterTest.java
b/kernel/traffic/distsql/handler/src/test/java/org/apache/shardingsphere/traffic/distsql/handler/update/CreateTrafficRuleStatementUpdaterTest.java
deleted file mode 100644
index b9cdf7fc75e..00000000000
---
a/kernel/traffic/distsql/handler/src/test/java/org/apache/shardingsphere/traffic/distsql/handler/update/CreateTrafficRuleStatementUpdaterTest.java
+++ /dev/null
@@ -1,132 +0,0 @@
-/*
- * 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.traffic.distsql.handler.update;
-
-import
org.apache.shardingsphere.distsql.handler.exception.algorithm.InvalidAlgorithmConfigurationException;
-import
org.apache.shardingsphere.distsql.handler.exception.rule.DuplicateRuleException;
-import org.apache.shardingsphere.distsql.parser.segment.AlgorithmSegment;
-import org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration;
-import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
-import
org.apache.shardingsphere.infra.metadata.database.rule.ShardingSphereRuleMetaData;
-import org.apache.shardingsphere.test.util.PropertiesBuilder;
-import org.apache.shardingsphere.test.util.PropertiesBuilder.Property;
-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;
-
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Collections;
-import java.util.LinkedList;
-import java.util.Properties;
-
-import static org.hamcrest.CoreMatchers.is;
-import static org.hamcrest.MatcherAssert.assertThat;
-import static org.junit.Assert.assertNotNull;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
-
-public final class CreateTrafficRuleStatementUpdaterTest {
-
- @Test(expected = DuplicateRuleException.class)
- public void assertExecuteWithInUsedRuleName() {
- ShardingSphereMetaData metaData = createMetaData();
- TrafficRuleSegment trafficRuleSegment = new TrafficRuleSegment(
- "rule_name_1", Arrays.asList("olap", "order_by"), new
AlgorithmSegment("DISTSQL.FIXTURE", new Properties()), new
AlgorithmSegment("DISTSQL.FIXTURE", new Properties()));
- CreateTrafficRuleStatementUpdater updater = new
CreateTrafficRuleStatementUpdater();
- updater.executeUpdate(metaData, new
CreateTrafficRuleStatement(Collections.singleton(trafficRuleSegment)));
- }
-
- @Test(expected = InvalidAlgorithmConfigurationException.class)
- public void assertExecuteWithInvalidAlgorithmType() {
- ShardingSphereMetaData metaData = createMetaData();
- TrafficRuleSegment trafficRuleSegment = new
TrafficRuleSegment("input_rule_name", Arrays.asList("olap", "order_by"),
- new AlgorithmSegment("invalid", new Properties()), new
AlgorithmSegment("invalid", new Properties()));
- CreateTrafficRuleStatementUpdater updater = new
CreateTrafficRuleStatementUpdater();
- updater.executeUpdate(metaData, new
CreateTrafficRuleStatement(Collections.singleton(trafficRuleSegment)));
- }
-
- @Test(expected = IllegalStateException.class)
- public void assertExecuteWithLoadBalancerCannotBeNull() {
- ShardingSphereMetaData metaData = createMetaData();
- TrafficRuleSegment trafficRuleSegment = new
TrafficRuleSegment("input_rule_name", Arrays.asList("olap", "order_by"),
- new AlgorithmSegment("DISTSQL.FIXTURE", new Properties()),
null);
- CreateTrafficRuleStatementUpdater updater = new
CreateTrafficRuleStatementUpdater();
- try {
- updater.executeUpdate(metaData, new
CreateTrafficRuleStatement(Collections.singleton(trafficRuleSegment)));
- } catch (final IllegalStateException ex) {
- TrafficRule currentRule =
metaData.getGlobalRuleMetaData().getSingleRule(TrafficRule.class);
- assertNotNull(currentRule);
- throw ex;
- }
- }
-
- @Test
- public void assertExecute() {
- ShardingSphereMetaData metaData = createMetaData();
- 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()));
- CreateTrafficRuleStatementUpdater updater = new
CreateTrafficRuleStatementUpdater();
- updater.executeUpdate(metaData, new
CreateTrafficRuleStatement(Arrays.asList(trafficRuleSegment1,
trafficRuleSegment2)));
- TrafficRuleConfiguration addedConfig =
metaData.getGlobalRuleMetaData().getSingleRule(TrafficRule.class).getConfiguration();
- assertThat(addedConfig.getTrafficStrategies().size(), is(4));
- assertThat(addedConfig.getLoadBalancers().size(), is(4));
- assertThat(addedConfig.getTrafficAlgorithms().size(), is(4));
- assertThat(new
ArrayList<>(addedConfig.getTrafficStrategies()).get(2).getName(),
is("rule_name_3"));
- assertThat(new
ArrayList<>(addedConfig.getTrafficStrategies()).get(3).getName(),
is("rule_name_4"));
- assertNotNull(addedConfig.getTrafficAlgorithms().get("algorithm_2"));
- assertNotNull(addedConfig.getLoadBalancers().get("load_balancer_2"));
-
assertNotNull(addedConfig.getTrafficAlgorithms().get("rule_name_3_distsql.fixture"));
-
assertNotNull(addedConfig.getLoadBalancers().get("rule_name_4_distsql.fixture"));
- }
-
- private ShardingSphereMetaData createMetaData() {
- ShardingSphereMetaData metaData = mock(ShardingSphereMetaData.class);
- ShardingSphereRuleMetaData ruleMetaData = new
ShardingSphereRuleMetaData(new
LinkedList<>(Collections.singleton(mockTrafficRule())));
- when(metaData.getGlobalRuleMetaData()).thenReturn(ruleMetaData);
- return metaData;
- }
-
- 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);
- when(strategyRule2.getName()).thenReturn("rule_name_2");
-
when(result.getStrategyRules()).thenReturn(Arrays.asList(strategyRule1,
strategyRule2));
- return result;
- }
-
- private TrafficRuleConfiguration createTrafficRuleConfiguration() {
- TrafficRuleConfiguration result = new TrafficRuleConfiguration();
- result.getTrafficStrategies().add(new
TrafficStrategyConfiguration("rule_name_1", Arrays.asList("olap", "order_by"),
"algorithm_1", "load_balancer_1"));
- result.getTrafficStrategies().add(new
TrafficStrategyConfiguration("rule_name_2", Collections.singleton("oltp"),
"algorithm_2", "load_balancer_2"));
- result.getTrafficAlgorithms().put("algorithm_1", new
AlgorithmConfiguration("SQL_MATCH", PropertiesBuilder.build(new Property("sql",
"select * from t_order"))));
- result.getTrafficAlgorithms().put("algorithm_2", new
AlgorithmConfiguration("SQL_HINT", new Properties()));
- result.getLoadBalancers().put("load_balancer_1", new
AlgorithmConfiguration("RANDOM", new Properties()));
- result.getLoadBalancers().put("load_balancer_2", new
AlgorithmConfiguration("ROUND_ROBIN", new Properties()));
- return result;
- }
-}
diff --git
a/kernel/traffic/distsql/handler/src/test/java/org/apache/shardingsphere/traffic/distsql/handler/update/DropTrafficRuleStatementUpdaterTest.java
b/kernel/traffic/distsql/handler/src/test/java/org/apache/shardingsphere/traffic/distsql/handler/update/DropTrafficRuleStatementUpdaterTest.java
deleted file mode 100644
index 61762743242..00000000000
---
a/kernel/traffic/distsql/handler/src/test/java/org/apache/shardingsphere/traffic/distsql/handler/update/DropTrafficRuleStatementUpdaterTest.java
+++ /dev/null
@@ -1,96 +0,0 @@
-/*
- * 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.traffic.distsql.handler.update;
-
-import
org.apache.shardingsphere.distsql.handler.exception.rule.MissingRequiredRuleException;
-import org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration;
-import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
-import
org.apache.shardingsphere.infra.metadata.database.rule.ShardingSphereRuleMetaData;
-import org.apache.shardingsphere.test.util.PropertiesBuilder;
-import org.apache.shardingsphere.test.util.PropertiesBuilder.Property;
-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;
-
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Collections;
-import java.util.LinkedList;
-import java.util.Properties;
-
-import static org.hamcrest.CoreMatchers.is;
-import static org.hamcrest.MatcherAssert.assertThat;
-import static org.junit.Assert.assertNotNull;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
-
-public final class DropTrafficRuleStatementUpdaterTest {
-
- @Test(expected = MissingRequiredRuleException.class)
- public void assertExecuteForNotExistedRuleWithoutIfExists() {
- ShardingSphereMetaData metaData = createMetaData();
- DropTrafficRuleStatementUpdater updater = new
DropTrafficRuleStatementUpdater();
- updater.executeUpdate(metaData, new DropTrafficRuleStatement(false,
Collections.singleton("not_existed_rule")));
- }
-
- @Test
- public void assertExecuteForNotExistedRuleWithIfExists() {
- ShardingSphereMetaData metaData = createMetaData();
- DropTrafficRuleStatementUpdater updater = new
DropTrafficRuleStatementUpdater();
- updater.executeUpdate(metaData, new DropTrafficRuleStatement(true,
Collections.singleton("rule_name_3")));
- TrafficRuleConfiguration updatedConfig =
metaData.getGlobalRuleMetaData().getSingleRule(TrafficRule.class).getConfiguration();
- assertThat(updatedConfig.getTrafficStrategies().size(), is(2));
- assertThat(updatedConfig.getLoadBalancers().size(), is(2));
- assertThat(updatedConfig.getTrafficAlgorithms().size(), is(2));
- }
-
- @Test
- public void assertExecute() {
- ShardingSphereMetaData metaData = createMetaData();
- DropTrafficRuleStatementUpdater updater = new
DropTrafficRuleStatementUpdater();
- updater.executeUpdate(metaData, new DropTrafficRuleStatement(false,
Collections.singleton("rule_name_1")));
- TrafficRuleConfiguration updatedConfig =
metaData.getGlobalRuleMetaData().getSingleRule(TrafficRule.class).getConfiguration();
- assertThat(updatedConfig.getTrafficStrategies().size(), is(1));
- assertThat(updatedConfig.getLoadBalancers().size(), is(1));
- assertThat(updatedConfig.getTrafficAlgorithms().size(), is(1));
- assertThat(new
ArrayList<>(updatedConfig.getTrafficStrategies()).get(0).getName(),
is("rule_name_2"));
- assertNotNull(updatedConfig.getTrafficAlgorithms().get("algorithm_2"));
- assertNotNull(updatedConfig.getLoadBalancers().get("load_balancer_2"));
- }
-
- private ShardingSphereMetaData createMetaData() {
- TrafficRule trafficRule = mock(TrafficRule.class);
-
when(trafficRule.getConfiguration()).thenReturn(createTrafficRuleConfiguration());
- ShardingSphereMetaData metaData = mock(ShardingSphereMetaData.class);
- when(metaData.getGlobalRuleMetaData()).thenReturn(new
ShardingSphereRuleMetaData(new
LinkedList<>(Collections.singleton(trafficRule))));
- return metaData;
- }
-
- private TrafficRuleConfiguration createTrafficRuleConfiguration() {
- TrafficRuleConfiguration result = new TrafficRuleConfiguration();
- result.getTrafficStrategies().add(new
TrafficStrategyConfiguration("rule_name_1", Arrays.asList("olap", "order_by"),
"algorithm_1", "load_balancer_1"));
- result.getTrafficStrategies().add(new
TrafficStrategyConfiguration("rule_name_2", Collections.singleton("oltp"),
"algorithm_2", "load_balancer_2"));
- result.getTrafficAlgorithms().put("algorithm_1", new
AlgorithmConfiguration("SQL_MATCH", PropertiesBuilder.build(new Property("sql",
"select * from t_order"))));
- result.getTrafficAlgorithms().put("algorithm_2", new
AlgorithmConfiguration("SQL_HINT", new Properties()));
- result.getLoadBalancers().put("load_balancer_1", new
AlgorithmConfiguration("RANDOM", new Properties()));
- result.getLoadBalancers().put("load_balancer_2", new
AlgorithmConfiguration("ROUND_ROBIN", new Properties()));
- return result;
- }
-}
diff --git
a/kernel/transaction/distsql/handler/src/main/java/org/apache/shardingsphere/transaction/distsql/handler/update/AlterTransactionRuleStatementUpdater.java
b/kernel/transaction/distsql/handler/src/main/java/org/apache/shardingsphere/transaction/distsql/handler/update/AlterTransactionRuleStatementUpdater.java
index 7a10dec05cc..f19428f598e 100644
---
a/kernel/transaction/distsql/handler/src/main/java/org/apache/shardingsphere/transaction/distsql/handler/update/AlterTransactionRuleStatementUpdater.java
+++
b/kernel/transaction/distsql/handler/src/main/java/org/apache/shardingsphere/transaction/distsql/handler/update/AlterTransactionRuleStatementUpdater.java
@@ -19,15 +19,11 @@ package
org.apache.shardingsphere.transaction.distsql.handler.update;
import
org.apache.shardingsphere.distsql.handler.exception.rule.InvalidRuleConfigurationException;
import
org.apache.shardingsphere.distsql.handler.ral.update.GlobalRuleRALUpdater;
-import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
-import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
import
org.apache.shardingsphere.infra.util.exception.ShardingSpherePreconditions;
import org.apache.shardingsphere.infra.util.spi.ShardingSphereServiceLoader;
-import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
import org.apache.shardingsphere.transaction.api.TransactionType;
import
org.apache.shardingsphere.transaction.config.TransactionRuleConfiguration;
import
org.apache.shardingsphere.transaction.distsql.parser.statement.updatable.AlterTransactionRuleStatement;
-import org.apache.shardingsphere.transaction.rule.TransactionRule;
import
org.apache.shardingsphere.transaction.spi.ShardingSphereTransactionManager;
import java.util.Collection;
@@ -36,25 +32,17 @@ import java.util.Optional;
/**
* Alter transaction rule statement handler.
*/
-public final class AlterTransactionRuleStatementUpdater implements
GlobalRuleRALUpdater {
+public final class AlterTransactionRuleStatementUpdater implements
GlobalRuleRALUpdater<AlterTransactionRuleStatement,
TransactionRuleConfiguration> {
@Override
- public void executeUpdate(final ShardingSphereMetaData metaData, final
SQLStatement sqlStatement) {
- check((AlterTransactionRuleStatement) sqlStatement);
- Collection<ShardingSphereRule> globalRules =
metaData.getGlobalRuleMetaData().getRules();
- globalRules.stream().filter(each -> each instanceof
TransactionRule).forEach(each -> ((TransactionRule) each).closeStaleResource());
- globalRules.removeIf(each -> each instanceof TransactionRule);
- TransactionRuleConfiguration toBeAlteredRuleConfig =
createToBeAlteredRuleConfiguration(sqlStatement);
- globalRules.add(new TransactionRule(toBeAlteredRuleConfig,
metaData.getDatabases()));
- }
-
- private void check(final AlterTransactionRuleStatement statement) {
- checkTransactionType(statement);
- TransactionType transactionType =
TransactionType.valueOf(statement.getDefaultType().toUpperCase());
+ public void checkSQLStatement(final TransactionRuleConfiguration
currentRuleConfig, final AlterTransactionRuleStatement sqlStatement) {
+ checkTransactionType(sqlStatement);
+ TransactionType transactionType =
TransactionType.valueOf(sqlStatement.getDefaultType().toUpperCase());
if (TransactionType.LOCAL.equals(transactionType)) {
return;
}
- checkTransactionManager(statement, transactionType);
+ checkTransactionManager(sqlStatement, transactionType);
+
}
private void checkTransactionType(final AlterTransactionRuleStatement
statement) {
@@ -67,8 +55,6 @@ public final class AlterTransactionRuleStatementUpdater
implements GlobalRuleRAL
private void checkTransactionManager(final AlterTransactionRuleStatement
statement, final TransactionType transactionType) {
Collection<ShardingSphereTransactionManager> transactionManagers =
ShardingSphereServiceLoader.getServiceInstances(ShardingSphereTransactionManager.class);
- ShardingSpherePreconditions.checkState(!transactionManagers.isEmpty(),
- () -> new InvalidRuleConfigurationException("Transaction",
String.format("No transaction manager with type `%s`",
statement.getDefaultType())));
Optional<ShardingSphereTransactionManager> transactionManager =
transactionManagers.stream().filter(each ->
transactionType.equals(each.getTransactionType())).findFirst();
ShardingSpherePreconditions.checkState(transactionManager.isPresent(),
() -> new InvalidRuleConfigurationException("Transaction",
String.format("No transaction manager with type `%s`",
statement.getDefaultType())));
@@ -82,9 +68,14 @@ public final class AlterTransactionRuleStatementUpdater
implements GlobalRuleRAL
() -> new InvalidRuleConfigurationException("Transaction",
String.format("No transaction manager provider with type `%s`", providerType)));
}
- private TransactionRuleConfiguration
createToBeAlteredRuleConfiguration(final SQLStatement sqlStatement) {
- AlterTransactionRuleStatement ruleStatement =
(AlterTransactionRuleStatement) sqlStatement;
- return new
TransactionRuleConfiguration(ruleStatement.getDefaultType(),
ruleStatement.getProvider().getProviderType(),
ruleStatement.getProvider().getProps());
+ @Override
+ public TransactionRuleConfiguration buildAlteredRuleConfiguration(final
TransactionRuleConfiguration currentRuleConfig, final
AlterTransactionRuleStatement sqlStatement) {
+ return new TransactionRuleConfiguration(sqlStatement.getDefaultType(),
sqlStatement.getProvider().getProviderType(),
sqlStatement.getProvider().getProps());
+ }
+
+ @Override
+ public Class<TransactionRuleConfiguration> getRuleConfigurationClass() {
+ return TransactionRuleConfiguration.class;
}
@Override
diff --git
a/kernel/transaction/distsql/handler/src/test/java/org/apache/shardingsphere/transaction/distsql/handler/update/AlterTransactionRuleStatementUpdaterTest.java
b/kernel/transaction/distsql/handler/src/test/java/org/apache/shardingsphere/transaction/distsql/handler/update/AlterTransactionRuleStatementUpdaterTest.java
index 277e43128e9..172cd8fa34a 100644
---
a/kernel/transaction/distsql/handler/src/test/java/org/apache/shardingsphere/transaction/distsql/handler/update/AlterTransactionRuleStatementUpdaterTest.java
+++
b/kernel/transaction/distsql/handler/src/test/java/org/apache/shardingsphere/transaction/distsql/handler/update/AlterTransactionRuleStatementUpdaterTest.java
@@ -17,38 +17,26 @@
package org.apache.shardingsphere.transaction.distsql.handler.update;
-import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
-import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
-import
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
-import
org.apache.shardingsphere.infra.metadata.database.rule.ShardingSphereRuleMetaData;
import org.apache.shardingsphere.infra.util.props.PropertiesConverter;
import org.apache.shardingsphere.infra.util.spi.ShardingSphereServiceLoader;
import org.apache.shardingsphere.test.util.PropertiesBuilder;
import org.apache.shardingsphere.test.util.PropertiesBuilder.Property;
-import org.apache.shardingsphere.transaction.api.TransactionType;
import
org.apache.shardingsphere.transaction.config.TransactionRuleConfiguration;
import
org.apache.shardingsphere.transaction.distsql.handler.fixture.ShardingSphereTransactionManagerFixture;
import
org.apache.shardingsphere.transaction.distsql.parser.segment.TransactionProviderSegment;
import
org.apache.shardingsphere.transaction.distsql.parser.statement.updatable.AlterTransactionRuleStatement;
-import org.apache.shardingsphere.transaction.rule.TransactionRule;
import
org.apache.shardingsphere.transaction.spi.ShardingSphereTransactionManager;
import org.junit.Test;
import org.mockito.MockedStatic;
-import javax.sql.DataSource;
import java.util.Collections;
-import java.util.HashMap;
-import java.util.LinkedList;
-import java.util.Map;
import java.util.Properties;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
-import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
-import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.mockStatic;
-import static org.mockito.Mockito.when;
public final class AlterTransactionRuleStatementUpdaterTest {
@@ -59,15 +47,12 @@ public final class AlterTransactionRuleStatementUpdaterTest
{
shardingSphereServiceLoader.when(
() ->
ShardingSphereServiceLoader.getServiceInstances(ShardingSphereTransactionManager.class)).thenReturn(Collections.singleton(new
ShardingSphereTransactionManagerFixture()));
AlterTransactionRuleStatementUpdater updater = new
AlterTransactionRuleStatementUpdater();
- ShardingSphereMetaData metaData = createMetaData();
- updater.executeUpdate(metaData, new
AlterTransactionRuleStatement("XA",
+ TransactionRuleConfiguration actual =
updater.buildAlteredRuleConfiguration(createTransactionRuleConfiguration(), new
AlterTransactionRuleStatement("XA",
new TransactionProviderSegment("Atomikos",
PropertiesBuilder.build(new Property("host", "127.0.0.1"), new
Property("databaseName", "jbossts")))));
- TransactionRule updatedRule =
metaData.getGlobalRuleMetaData().getSingleRule(TransactionRule.class);
- assertThat(updatedRule.getDefaultType(), is(TransactionType.XA));
- assertThat(updatedRule.getProviderType(), is("Atomikos"));
- assertTrue(updatedRule.getDatabases().containsKey("foo_db"));
- assertTrue(null != updatedRule.getProps() &&
!updatedRule.getProps().isEmpty());
- String props = PropertiesConverter.convert(updatedRule.getProps());
+ assertThat(actual.getDefaultType(), is("XA"));
+ assertThat(actual.getProviderType(), is("Atomikos"));
+ assertFalse(actual.getProps().isEmpty());
+ String props = PropertiesConverter.convert(actual.getProps());
assertTrue(props.contains("host=127.0.0.1"));
assertTrue(props.contains("databaseName=jbossts"));
}
@@ -76,27 +61,13 @@ public final class AlterTransactionRuleStatementUpdaterTest
{
@Test
public void assertExecuteWithLocal() {
AlterTransactionRuleStatementUpdater updater = new
AlterTransactionRuleStatementUpdater();
- ShardingSphereMetaData metaData = createMetaData();
- updater.executeUpdate(metaData, new
AlterTransactionRuleStatement("LOCAL", new TransactionProviderSegment("", new
Properties())));
- TransactionRule updatedRule =
metaData.getGlobalRuleMetaData().getSingleRule(TransactionRule.class);
- assertThat(updatedRule.getDefaultType(), is(TransactionType.LOCAL));
- assertThat(updatedRule.getProviderType(), is(""));
- assertTrue(updatedRule.getDatabases().containsKey("foo_db"));
+ TransactionRuleConfiguration actual =
+
updater.buildAlteredRuleConfiguration(createTransactionRuleConfiguration(), new
AlterTransactionRuleStatement("LOCAL", new TransactionProviderSegment("", new
Properties())));
+ assertThat(actual.getDefaultType(), is("LOCAL"));
+ assertThat(actual.getProviderType(), is(""));
}
- private ShardingSphereMetaData createMetaData() {
- Map<String, ShardingSphereDatabase> databases = new
HashMap<>(Collections.singletonMap("foo_db", mockDatabase()));
- ShardingSphereRuleMetaData ruleMetaData = new
ShardingSphereRuleMetaData(new
LinkedList<>(Collections.singleton(createTransactionRule(databases))));
- return new ShardingSphereMetaData(databases, ruleMetaData, new
ConfigurationProperties(new Properties()));
- }
-
- private TransactionRule createTransactionRule(final Map<String,
ShardingSphereDatabase> databases) {
- return new TransactionRule(new TransactionRuleConfiguration("BASE",
null, new Properties()), databases);
- }
-
- private ShardingSphereDatabase mockDatabase() {
- ShardingSphereDatabase result = mock(ShardingSphereDatabase.class,
RETURNS_DEEP_STUBS);
-
when(result.getResourceMetaData().getDataSources()).thenReturn(Collections.singletonMap("foo_ds",
mock(DataSource.class, RETURNS_DEEP_STUBS)));
- return result;
+ private TransactionRuleConfiguration createTransactionRuleConfiguration() {
+ return new TransactionRuleConfiguration("BASE", null, new
Properties());
}
}
diff --git
a/proxy/backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/RALBackendHandlerFactory.java
b/proxy/backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/RALBackendHandlerFactory.java
index 2d602ec33fa..a760e84e8de 100644
---
a/proxy/backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/RALBackendHandlerFactory.java
+++
b/proxy/backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/RALBackendHandlerFactory.java
@@ -19,7 +19,6 @@ package
org.apache.shardingsphere.proxy.backend.handler.distsql.ral;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
-import
org.apache.shardingsphere.distsql.handler.ral.update.GlobalRuleRALUpdater;
import org.apache.shardingsphere.distsql.handler.ral.update.RALUpdater;
import org.apache.shardingsphere.distsql.parser.statement.ral.HintRALStatement;
import
org.apache.shardingsphere.distsql.parser.statement.ral.QueryableRALStatement;
@@ -86,7 +85,7 @@ public final class RALBackendHandlerFactory {
return new
UpdatableScalingRALBackendHandler((UpdatableScalingRALStatement) sqlStatement,
connectionSession);
}
if (sqlStatement instanceof UpdatableGlobalRuleRALStatement) {
- return new UpdatableGlobalRuleRALBackendHandler(sqlStatement,
TypedSPILoader.getService(GlobalRuleRALUpdater.class,
sqlStatement.getClass().getName()));
+ return new UpdatableGlobalRuleRALBackendHandler(sqlStatement);
}
return createRALBackendHandler(sqlStatement, connectionSession);
}
diff --git
a/proxy/backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/UpdatableGlobalRuleRALBackendHandler.java
b/proxy/backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/UpdatableGlobalRuleRALBackendHandler.java
index 0a4fb4177b6..3a30316d9c8 100644
---
a/proxy/backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/UpdatableGlobalRuleRALBackendHandler.java
+++
b/proxy/backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/UpdatableGlobalRuleRALBackendHandler.java
@@ -17,14 +17,20 @@
package org.apache.shardingsphere.proxy.backend.handler.distsql.ral;
+import
org.apache.shardingsphere.distsql.handler.exception.rule.MissingRequiredRuleException;
import
org.apache.shardingsphere.distsql.handler.ral.update.GlobalRuleRALUpdater;
import org.apache.shardingsphere.distsql.parser.statement.ral.RALStatement;
+import org.apache.shardingsphere.infra.config.rule.RuleConfiguration;
+import org.apache.shardingsphere.infra.util.spi.type.typed.TypedSPILoader;
import org.apache.shardingsphere.mode.manager.ContextManager;
import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
import
org.apache.shardingsphere.proxy.backend.handler.distsql.DistSQLBackendHandler;
import org.apache.shardingsphere.proxy.backend.response.header.ResponseHeader;
import
org.apache.shardingsphere.proxy.backend.response.header.update.UpdateResponseHeader;
+import java.util.Collection;
+import java.util.LinkedList;
+
/**
* Updatable RAL backend handler for global rule.
*/
@@ -32,18 +38,38 @@ public final class UpdatableGlobalRuleRALBackendHandler
implements DistSQLBacken
private final RALStatement sqlStatement;
- private final GlobalRuleRALUpdater updater;
-
- public UpdatableGlobalRuleRALBackendHandler(final RALStatement
sqlStatement, final GlobalRuleRALUpdater updater) {
+ public UpdatableGlobalRuleRALBackendHandler(final RALStatement
sqlStatement) {
this.sqlStatement = sqlStatement;
- this.updater = updater;
}
+ @SuppressWarnings({"rawtypes", "unchecked"})
@Override
public ResponseHeader execute() {
-
updater.executeUpdate(ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData(),
sqlStatement);
+ GlobalRuleRALUpdater globalRuleUpdater =
TypedSPILoader.getService(GlobalRuleRALUpdater.class,
sqlStatement.getClass().getName());
+ Class<? extends RuleConfiguration> ruleConfigClass =
globalRuleUpdater.getRuleConfigurationClass();
ContextManager contextManager =
ProxyContext.getInstance().getContextManager();
-
contextManager.getInstanceContext().getModeContextManager().alterGlobalRuleConfiguration(contextManager.getMetaDataContexts().getMetaData().getGlobalRuleMetaData().getConfigurations());
+ Collection<RuleConfiguration> ruleConfigurations =
contextManager.getMetaDataContexts().getMetaData().getGlobalRuleMetaData().getConfigurations();
+ RuleConfiguration currentRuleConfig =
findCurrentRuleConfiguration(ruleConfigurations, ruleConfigClass);
+ globalRuleUpdater.checkSQLStatement(currentRuleConfig, sqlStatement);
+
contextManager.getInstanceContext().getModeContextManager().alterGlobalRuleConfiguration(processUpdate(ruleConfigurations,
sqlStatement, globalRuleUpdater, currentRuleConfig));
return new UpdateResponseHeader(sqlStatement);
}
+
+ private RuleConfiguration findCurrentRuleConfiguration(final
Collection<RuleConfiguration> ruleConfigurations, final Class<? extends
RuleConfiguration> ruleConfigClass) {
+ for (RuleConfiguration each : ruleConfigurations) {
+ if (ruleConfigClass.isAssignableFrom(each.getClass())) {
+ return each;
+ }
+ }
+ throw new
MissingRequiredRuleException(ruleConfigClass.getSimpleName());
+ }
+
+ @SuppressWarnings({"rawtypes", "unchecked"})
+ private Collection<RuleConfiguration> processUpdate(final
Collection<RuleConfiguration> ruleConfigurations, final RALStatement
sqlStatement, final GlobalRuleRALUpdater globalRuleUpdater,
+ final
RuleConfiguration currentRuleConfig) {
+ Collection<RuleConfiguration> result = new
LinkedList<>(ruleConfigurations);
+ result.remove(currentRuleConfig);
+
result.add(globalRuleUpdater.buildAlteredRuleConfiguration(currentRuleConfig,
sqlStatement));
+ return result;
+ }
}