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 2020/11/05 15:29:21 UTC
[shardingsphere] branch master updated: #7318,
add route switch (#8041)
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 34cbbea #7318, add route switch (#8041)
34cbbea is described below
commit 34cbbeaf709d5c0f801b7195104a4ea4d059a5d2
Author: Zhang Yonglun <zh...@apache.org>
AuthorDate: Thu Nov 5 23:28:53 2020 +0800
#7318, add route switch (#8041)
---
.../ha/api/config/rule/HADataSourceRuleConfiguration.java | 2 ++
.../org/apache/shardingsphere/ha/rule/HADataSourceRule.java | 3 +++
.../ha/yaml/config/rule/YamlHADataSourceRuleConfiguration.java | 2 ++
.../HARuleAlgorithmProviderConfigurationYamlSwapper.java | 5 +++--
.../ha/yaml/swapper/HARuleConfigurationYamlSwapper.java | 5 +++--
.../apache/shardingsphere/ha/rule/HADataSourceRuleTest.java | 10 +++++-----
.../java/org/apache/shardingsphere/ha/rule/HARuleTest.java | 2 +-
.../ha/rule/biulder/AlgorithmProvidedHARuleBuilderTest.java | 2 +-
.../shardingsphere/ha/rule/biulder/HARuleBuilderTest.java | 2 +-
.../HARuleAlgorithmProviderConfigurationYamlSwapperTest.java | 2 +-
.../ha/yaml/swapper/HARuleConfigurationYamlSwapperTest.java | 4 ++--
.../src/test/resources/yaml/ha-rule.yaml | 2 ++
.../ha/route/engine/impl/HADataSourceRouter.java | 2 +-
.../apache/shardingsphere/ha/route/engine/HASQLRouterTest.java | 2 +-
.../shardingsphere/ha/spring/boot/HASpringBootStarterTest.java | 1 +
.../src/test/resources/application-ha.properties | 1 +
.../ha/spring/namespace/parser/HARuleBeanDefinitionParser.java | 1 +
.../ha/spring/namespace/tag/HARuleBeanDefinitionTag.java | 2 ++
.../src/main/resources/META-INF/namespace/ha.xsd | 1 +
.../ha/spring/namespace/HASpringNamespaceTest.java | 2 ++
.../test/resources/META-INF/spring/ha-application-context.xml | 2 +-
shardingsphere-jdbc/shardingsphere-jdbc-core/pom.xml | 5 +++++
shardingsphere-proxy/shardingsphere-proxy-backend/pom.xml | 5 +++++
.../src/main/resources/conf/config-ha.yaml | 1 +
24 files changed, 48 insertions(+), 18 deletions(-)
diff --git a/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-api/src/main/java/org/apache/shardingsphere/ha/api/config/rule/HADataSourceRuleConfiguration.java b/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-api/src/main/java/org/apache/shardingsphere/ha/api/config/rule/HADataSourceRuleConfiguration.java
index 25a4719..b858652 100644
--- a/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-api/src/main/java/org/apache/shardingsphere/ha/api/config/rule/HADataSourceRuleConfiguration.java
+++ b/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-api/src/main/java/org/apache/shardingsphere/ha/api/config/rule/HADataSourceRuleConfiguration.java
@@ -36,4 +36,6 @@ public final class HADataSourceRuleConfiguration {
private final List<String> replicaDataSourceNames;
private final String loadBalancerName;
+
+ private final Boolean readWriteSplit;
}
diff --git a/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-common/src/main/java/org/apache/shardingsphere/ha/rule/HADataSourceRule.java b/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-common/src/main/java/org/apache/shardingsphere/ha/rule/HADataSourceRule.java
index ee09e3b..b3ce7ef 100644
--- a/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-common/src/main/java/org/apache/shardingsphere/ha/rule/HADataSourceRule.java
+++ b/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-common/src/main/java/org/apache/shardingsphere/ha/rule/HADataSourceRule.java
@@ -46,6 +46,8 @@ public final class HADataSourceRule {
private final ReplicaLoadBalanceAlgorithm loadBalancer;
+ private final Boolean readWriteSplit;
+
@Getter(AccessLevel.NONE)
private final Collection<String> disabledDataSourceNames = new HashSet<>();
@@ -55,6 +57,7 @@ public final class HADataSourceRule {
primaryDataSourceName = config.getPrimaryDataSourceName();
replicaDataSourceNames = config.getReplicaDataSourceNames();
this.loadBalancer = loadBalancer;
+ this.readWriteSplit = config.getReadWriteSplit();
}
private void checkConfiguration(final HADataSourceRuleConfiguration config) {
diff --git a/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-common/src/main/java/org/apache/shardingsphere/ha/yaml/config/rule/YamlHADataSourceRuleConfiguration.java b/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-common/src/main/java/org/apache/shardingsphere/ha/yaml/config/rule/YamlHADataSourceRuleConfiguration.java
index 9fe6d17..56155b6 100644
--- a/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-common/src/main/java/org/apache/shardingsphere/ha/yaml/config/rule/YamlHADataSourceRuleConfiguration.java
+++ b/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-common/src/main/java/org/apache/shardingsphere/ha/yaml/config/rule/YamlHADataSourceRuleConfiguration.java
@@ -40,5 +40,7 @@ public final class YamlHADataSourceRuleConfiguration implements YamlConfiguratio
private String loadBalancerName;
+ private Boolean readWriteSplit;
+
private Properties props = new Properties();
}
diff --git a/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-common/src/main/java/org/apache/shardingsphere/ha/yaml/swapper/HARuleAlgorithmProviderConfigurationYamlSwapper.java b/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-common/src/main/java/org/apache/shardingsphere/ha/yaml/swapper/HARuleAlgorithmProviderConfigurationYamlSwapper.java
index 15fb341..b139b2b 100644
--- a/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-common/src/main/java/org/apache/shardingsphere/ha/yaml/swapper/HARuleAlgorithmProviderConfigurationYamlSwapper.java
+++ b/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-common/src/main/java/org/apache/shardingsphere/ha/yaml/swapper/HARuleAlgorithmProviderConfigurationYamlSwapper.java
@@ -54,6 +54,7 @@ public final class HARuleAlgorithmProviderConfigurationYamlSwapper
result.setPrimaryDataSourceName(dataSourceRuleConfig.getPrimaryDataSourceName());
result.setReplicaDataSourceNames(dataSourceRuleConfig.getReplicaDataSourceNames());
result.setLoadBalancerName(dataSourceRuleConfig.getLoadBalancerName());
+ result.setReadWriteSplit(dataSourceRuleConfig.getReadWriteSplit());
return result;
}
@@ -69,8 +70,8 @@ public final class HARuleAlgorithmProviderConfigurationYamlSwapper
}
private HADataSourceRuleConfiguration swapToObject(final String name, final YamlHADataSourceRuleConfiguration yamlDataSourceRuleConfig) {
- return new HADataSourceRuleConfiguration(name,
- yamlDataSourceRuleConfig.getPrimaryDataSourceName(), yamlDataSourceRuleConfig.getReplicaDataSourceNames(), yamlDataSourceRuleConfig.getLoadBalancerName());
+ return new HADataSourceRuleConfiguration(name, yamlDataSourceRuleConfig.getPrimaryDataSourceName(), yamlDataSourceRuleConfig.getReplicaDataSourceNames(),
+ yamlDataSourceRuleConfig.getLoadBalancerName(), yamlDataSourceRuleConfig.getReadWriteSplit());
}
@Override
diff --git a/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-common/src/main/java/org/apache/shardingsphere/ha/yaml/swapper/HARuleConfigurationYamlSwapper.java b/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-common/src/main/java/org/apache/shardingsphere/ha/yaml/swapper/HARuleConfigurationYamlSwapper.java
index 56334b5..8421544 100644
--- a/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-common/src/main/java/org/apache/shardingsphere/ha/yaml/swapper/HARuleConfigurationYamlSwapper.java
+++ b/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-common/src/main/java/org/apache/shardingsphere/ha/yaml/swapper/HARuleConfigurationYamlSwapper.java
@@ -58,6 +58,7 @@ public final class HARuleConfigurationYamlSwapper
result.setPrimaryDataSourceName(dataSourceRuleConfig.getPrimaryDataSourceName());
result.setReplicaDataSourceNames(dataSourceRuleConfig.getReplicaDataSourceNames());
result.setLoadBalancerName(dataSourceRuleConfig.getLoadBalancerName());
+ result.setReadWriteSplit(dataSourceRuleConfig.getReadWriteSplit());
return result;
}
@@ -75,8 +76,8 @@ public final class HARuleConfigurationYamlSwapper
}
private HADataSourceRuleConfiguration swapToObject(final String name, final YamlHADataSourceRuleConfiguration yamlDataSourceRuleConfig) {
- return new HADataSourceRuleConfiguration(name,
- yamlDataSourceRuleConfig.getPrimaryDataSourceName(), yamlDataSourceRuleConfig.getReplicaDataSourceNames(), yamlDataSourceRuleConfig.getLoadBalancerName());
+ return new HADataSourceRuleConfiguration(name, yamlDataSourceRuleConfig.getPrimaryDataSourceName(), yamlDataSourceRuleConfig.getReplicaDataSourceNames(),
+ yamlDataSourceRuleConfig.getLoadBalancerName(), yamlDataSourceRuleConfig.getReadWriteSplit());
}
@Override
diff --git a/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-common/src/test/java/org/apache/shardingsphere/ha/rule/HADataSourceRuleTest.java b/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-common/src/test/java/org/apache/shardingsphere/ha/rule/HADataSourceRuleTest.java
index 5bce187..ee8cde2 100644
--- a/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-common/src/test/java/org/apache/shardingsphere/ha/rule/HADataSourceRuleTest.java
+++ b/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-common/src/test/java/org/apache/shardingsphere/ha/rule/HADataSourceRuleTest.java
@@ -34,26 +34,26 @@ import static org.junit.Assert.assertThat;
public final class HADataSourceRuleTest {
private final HADataSourceRule haDataSourceRule = new HADataSourceRule(
- new HADataSourceRuleConfiguration("test_pr", "primary_ds", Arrays.asList("replica_ds_0", "replica_ds_1"), "random"), new RandomReplicaLoadBalanceAlgorithm());
+ new HADataSourceRuleConfiguration("test_pr", "primary_ds", Arrays.asList("replica_ds_0", "replica_ds_1"), "random", true), new RandomReplicaLoadBalanceAlgorithm());
@Test(expected = IllegalArgumentException.class)
public void assertNewHADataSourceRuleWithoutName() {
- new HADataSourceRule(new HADataSourceRuleConfiguration("", "primary_ds", Collections.singletonList("replica_ds"), null), new RoundRobinReplicaLoadBalanceAlgorithm());
+ new HADataSourceRule(new HADataSourceRuleConfiguration("", "primary_ds", Collections.singletonList("replica_ds"), null, true), new RoundRobinReplicaLoadBalanceAlgorithm());
}
@Test(expected = IllegalArgumentException.class)
public void assertNewHADataSourceRuleWithoutPrimaryDataSourceName() {
- new HADataSourceRule(new HADataSourceRuleConfiguration("ds", "", Collections.singletonList("replica_ds"), null), new RoundRobinReplicaLoadBalanceAlgorithm());
+ new HADataSourceRule(new HADataSourceRuleConfiguration("ds", "", Collections.singletonList("replica_ds"), null, true), new RoundRobinReplicaLoadBalanceAlgorithm());
}
@Test(expected = IllegalArgumentException.class)
public void assertNewHADataSourceRuleWithNullReplicaDataSourceName() {
- new HADataSourceRule(new HADataSourceRuleConfiguration("ds", "primary_ds", null, null), new RoundRobinReplicaLoadBalanceAlgorithm());
+ new HADataSourceRule(new HADataSourceRuleConfiguration("ds", "primary_ds", null, null, true), new RoundRobinReplicaLoadBalanceAlgorithm());
}
@Test(expected = IllegalArgumentException.class)
public void assertNewHADataSourceRuleWithEmptyReplicaDataSourceName() {
- new HADataSourceRule(new HADataSourceRuleConfiguration("ds", "primary_ds", Collections.emptyList(), null), new RoundRobinReplicaLoadBalanceAlgorithm());
+ new HADataSourceRule(new HADataSourceRuleConfiguration("ds", "primary_ds", Collections.emptyList(), null, true), new RoundRobinReplicaLoadBalanceAlgorithm());
}
@Test
diff --git a/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-common/src/test/java/org/apache/shardingsphere/ha/rule/HARuleTest.java b/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-common/src/test/java/org/apache/shardingsphere/ha/rule/HARuleTest.java
index c0e96ac..74b9298 100644
--- a/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-common/src/test/java/org/apache/shardingsphere/ha/rule/HARuleTest.java
+++ b/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-common/src/test/java/org/apache/shardingsphere/ha/rule/HARuleTest.java
@@ -56,7 +56,7 @@ public final class HARuleTest {
private HARule createHARule() {
HADataSourceRuleConfiguration config =
- new HADataSourceRuleConfiguration("test_pr", "primary_ds", Arrays.asList("replica_ds_0", "replica_ds_1"), "random");
+ new HADataSourceRuleConfiguration("test_pr", "primary_ds", Arrays.asList("replica_ds_0", "replica_ds_1"), "random", true);
return new HARule(new HARuleConfiguration(
Collections.singleton(config), ImmutableMap.of("random", new ShardingSphereAlgorithmConfiguration("RANDOM", new Properties()))));
}
diff --git a/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-common/src/test/java/org/apache/shardingsphere/ha/rule/biulder/AlgorithmProvidedHARuleBuilderTest.java b/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-common/src/test/java/org/apache/shardingsphere/ha/rule/biulder/AlgorithmProvidedHARuleBuilderTest.java
index 8381ba9..b70c6a9 100644
--- a/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-common/src/test/java/org/apache/shardingsphere/ha/rule/biulder/AlgorithmProvidedHARuleBuilderTest.java
+++ b/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-common/src/test/java/org/apache/shardingsphere/ha/rule/biulder/AlgorithmProvidedHARuleBuilderTest.java
@@ -43,7 +43,7 @@ public final class AlgorithmProvidedHARuleBuilderTest {
public void assertBuild() {
AlgorithmProvidedHARuleConfiguration algorithmProvidedRuleConfig = mock(AlgorithmProvidedHARuleConfiguration.class);
HADataSourceRuleConfiguration ruleConfig = new HADataSourceRuleConfiguration(
- "name", "primaryDataSourceName", Collections.singletonList("name"), "loadBalancerName");
+ "name", "primaryDataSourceName", Collections.singletonList("name"), "loadBalancerName", true);
when(algorithmProvidedRuleConfig.getDataSources()).thenReturn(Collections.singletonList(ruleConfig));
ShardingSphereRuleBuilder builder = OrderedSPIRegistry.getRegisteredServices(
Collections.singletonList(algorithmProvidedRuleConfig), ShardingSphereRuleBuilder.class).get(algorithmProvidedRuleConfig);
diff --git a/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-common/src/test/java/org/apache/shardingsphere/ha/rule/biulder/HARuleBuilderTest.java b/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-common/src/test/java/org/apache/shardingsphere/ha/rule/biulder/HARuleBuilderTest.java
index 5c2ffdd..1220776 100644
--- a/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-common/src/test/java/org/apache/shardingsphere/ha/rule/biulder/HARuleBuilderTest.java
+++ b/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-common/src/test/java/org/apache/shardingsphere/ha/rule/biulder/HARuleBuilderTest.java
@@ -43,7 +43,7 @@ public final class HARuleBuilderTest {
public void assertBuild() {
HARuleConfiguration ruleConfig = mock(HARuleConfiguration.class);
HADataSourceRuleConfiguration dataSourceRuleConfig = new HADataSourceRuleConfiguration(
- "name", "primaryDataSourceName", Collections.singletonList("name"), "loadBalancerName");
+ "name", "primaryDataSourceName", Collections.singletonList("name"), "loadBalancerName", true);
when(ruleConfig.getDataSources()).thenReturn(Collections.singletonList(dataSourceRuleConfig));
ShardingSphereRuleBuilder builder = OrderedSPIRegistry.getRegisteredServices(Collections.singletonList(ruleConfig), ShardingSphereRuleBuilder.class).get(ruleConfig);
assertThat(builder.build(ruleConfig, Collections.emptyList()), instanceOf(HARule.class));
diff --git a/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-common/src/test/java/org/apache/shardingsphere/ha/yaml/swapper/HARuleAlgorithmProviderConfigurationYamlSwapperTest.java b/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-common/src/test/java/org/apache/shardingsphere/ha/yaml/swapper/HARuleAlgorithmProviderConfigurationYamlSwapperTest.java
index 78b36a0..18300a8 100644
--- a/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-common/src/test/java/org/apache/shardingsphere/ha/yaml/swapper/HARuleAlgorithmProviderConfigurationYamlSwapperTest.java
+++ b/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-common/src/test/java/org/apache/shardingsphere/ha/yaml/swapper/HARuleAlgorithmProviderConfigurationYamlSwapperTest.java
@@ -85,7 +85,7 @@ public final class HARuleAlgorithmProviderConfigurationYamlSwapperTest {
private YamlHARuleConfiguration createYamlHARuleConfiguration() {
HADataSourceRuleConfiguration ruleConfig = new HADataSourceRuleConfiguration("name", "primaryDataSourceName",
- Collections.singletonList("replicaDataSourceName"), "loadBalancerName");
+ Collections.singletonList("replicaDataSourceName"), "loadBalancerName", true);
return swapper.swapToYamlConfiguration(
new AlgorithmProvidedHARuleConfiguration(Collections.singletonList(ruleConfig), ImmutableMap.of("name", new RandomReplicaLoadBalanceAlgorithm())));
}
diff --git a/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-common/src/test/java/org/apache/shardingsphere/ha/yaml/swapper/HARuleConfigurationYamlSwapperTest.java b/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-common/src/test/java/org/apache/shardingsphere/ha/yaml/swapper/HARuleConfigurationYamlSwapperTest.java
index f011146..a0055af 100644
--- a/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-common/src/test/java/org/apache/shardingsphere/ha/yaml/swapper/HARuleConfigurationYamlSwapperTest.java
+++ b/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-common/src/test/java/org/apache/shardingsphere/ha/yaml/swapper/HARuleConfigurationYamlSwapperTest.java
@@ -50,7 +50,7 @@ public final class HARuleConfigurationYamlSwapperTest {
@Test
public void assertSwapToYamlWithLoadBalanceAlgorithm() {
HADataSourceRuleConfiguration dataSourceConfig =
- new HADataSourceRuleConfiguration("ds", "primary", Collections.singletonList("replica"), "roundRobin");
+ new HADataSourceRuleConfiguration("ds", "primary", Collections.singletonList("replica"), "roundRobin", true);
YamlHARuleConfiguration actual = getHARuleConfigurationYamlSwapper().swapToYamlConfiguration(new HARuleConfiguration(
Collections.singleton(dataSourceConfig), ImmutableMap.of("roundRobin", new ShardingSphereAlgorithmConfiguration("ROUND_ROBIN", new Properties()))));
assertThat(actual.getDataSources().get("ds").getName(), is("ds"));
@@ -61,7 +61,7 @@ public final class HARuleConfigurationYamlSwapperTest {
@Test
public void assertSwapToYamlWithoutLoadBalanceAlgorithm() {
- HADataSourceRuleConfiguration dataSourceConfig = new HADataSourceRuleConfiguration("ds", "primary", Collections.singletonList("replica"), null);
+ HADataSourceRuleConfiguration dataSourceConfig = new HADataSourceRuleConfiguration("ds", "primary", Collections.singletonList("replica"), null, true);
YamlHARuleConfiguration actual = getHARuleConfigurationYamlSwapper().swapToYamlConfiguration(
new HARuleConfiguration(Collections.singleton(dataSourceConfig), Collections.emptyMap()));
assertThat(actual.getDataSources().get("ds").getName(), is("ds"));
diff --git a/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-common/src/test/resources/yaml/ha-rule.yaml b/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-common/src/test/resources/yaml/ha-rule.yaml
index cd04b13..6c3e888 100644
--- a/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-common/src/test/resources/yaml/ha-rule.yaml
+++ b/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-common/src/test/resources/yaml/ha-rule.yaml
@@ -56,12 +56,14 @@ rules:
- primary_ds_0_replica_0
- primary_ds_0_replica_1
loadBalancerName: roundRobin
+ readWriteSplit: true
ds_1:
primaryDataSourceName: primary_ds_1
replicaDataSourceNames:
- primary_ds_1_replica_0
- primary_ds_1_replica_1
loadBalancerName: random
+ readWriteSplit: false
loadBalancers:
roundRobin:
type: ROUND_ROBIN
diff --git a/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-route/src/main/java/org/apache/shardingsphere/ha/route/engine/impl/HADataSourceRouter.java b/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-route/src/main/java/org/apache/shardingsphere/ha/route/engine/impl/HADataSourceRouter.java
index cdd94f4..737f482 100644
--- a/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-route/src/main/java/org/apache/shardingsphere/ha/route/engine/impl/HADataSourceRouter.java
+++ b/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-route/src/main/java/org/apache/shardingsphere/ha/route/engine/impl/HADataSourceRouter.java
@@ -39,7 +39,7 @@ public final class HADataSourceRouter {
* @return data source name
*/
public String route(final SQLStatement sqlStatement) {
- if (isPrimaryRoute(sqlStatement)) {
+ if (isPrimaryRoute(sqlStatement) || !rule.getReadWriteSplit()) {
PrimaryVisitedManager.setPrimaryVisited();
return rule.getPrimaryDataSourceName();
}
diff --git a/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-route/src/test/java/org/apache/shardingsphere/ha/route/engine/HASQLRouterTest.java b/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-route/src/test/java/org/apache/shardingsphere/ha/route/engine/HASQLRouterTest.java
index 797f0d7..207e9e7 100644
--- a/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-route/src/test/java/org/apache/shardingsphere/ha/route/engine/HASQLRouterTest.java
+++ b/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-route/src/test/java/org/apache/shardingsphere/ha/route/engine/HASQLRouterTest.java
@@ -80,7 +80,7 @@ public final class HASQLRouterTest {
@Before
public void setUp() {
rule = new HARule(new HARuleConfiguration(Collections.singleton(
- new HADataSourceRuleConfiguration(DATASOURCE_NAME, PRIMARY_DATASOURCE, Collections.singletonList(REPLICA_DATASOURCE), null)), Collections.emptyMap()));
+ new HADataSourceRuleConfiguration(DATASOURCE_NAME, PRIMARY_DATASOURCE, Collections.singletonList(REPLICA_DATASOURCE), null, true)), Collections.emptyMap()));
sqlRouter = (HASQLRouter) OrderedSPIRegistry.getRegisteredServices(Collections.singleton(rule), SQLRouter.class).get(rule);
}
diff --git a/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-spring/shardingsphere-ha-spring-boot-starter/src/test/java/org/apache/shardingsphere/ha/spring/boot/HASpringBootStarterTest.java b/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-spring/shardingsphere-ha-spring-boot-starter/src/test/java/org/apache/shardingsphere/ha/spring/boot/HASpringBootStarterTest.java
index 52a5b37..a4063cd 100644
--- a/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-spring/shardingsphere-ha-spring-boot-starter/src/test/java/org/apache/shardingsphere/ha/spring/boot/HASpringBootStarterTest.java
+++ b/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-spring/shardingsphere-ha-spring-boot-starter/src/test/java/org/apache/shardingsphere/ha/spring/boot/HASpringBootStarterTest.java
@@ -57,6 +57,7 @@ public class HASpringBootStarterTest {
assertThat(dataSourceRuleConfig.getName(), is("pr_ds"));
assertThat(dataSourceRuleConfig.getPrimaryDataSourceName(), is("primary_ds"));
assertThat(dataSourceRuleConfig.getLoadBalancerName(), is("random"));
+ assertTrue(dataSourceRuleConfig.getReadWriteSplit());
assertThat(dataSourceRuleConfig.getReplicaDataSourceNames().size(), is(2));
assertTrue(config.getDataSources().contains(dataSourceRuleConfig));
assertThat(config.getLoadBalanceAlgorithms().size(), is(1));
diff --git a/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-spring/shardingsphere-ha-spring-boot-starter/src/test/resources/application-ha.properties b/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-spring/shardingsphere-ha-spring-boot-starter/src/test/resources/application-ha.properties
index 95d92ba..b738b14 100644
--- a/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-spring/shardingsphere-ha-spring-boot-starter/src/test/resources/application-ha.properties
+++ b/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-spring/shardingsphere-ha-spring-boot-starter/src/test/resources/application-ha.properties
@@ -20,3 +20,4 @@ spring.shardingsphere.rules.ha.load-balancers.random.type=RANDOM
spring.shardingsphere.rules.ha.data-sources.pr_ds.primary-data-source-name=primary_ds
spring.shardingsphere.rules.ha.data-sources.pr_ds.replica-data-source-names=replica_ds_0,replica_ds_1
spring.shardingsphere.rules.ha.data-sources.pr_ds.load-balancer-name=random
+spring.shardingsphere.rules.ha.data-sources.pr_ds.read-write-split=true
diff --git a/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-spring/shardingsphere-ha-spring-namespace/src/main/java/org/apache/shardingsphere/ha/spring/namespace/parser/HARuleBeanDefinitionParser.java b/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-spring/shardingsphere-ha-spring-namespace/src/main/java/org/apache/shardingsphere/ha/spring/namespace/parser/HARuleBeanDefinitionParser.java
index 516689e..37c2429 100644
--- a/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-spring/shardingsphere-ha-spring-namespace/src/main/java/org/apache/shardingsphere/ha/spring/namespace/parser/HARuleBeanDefinitionParser.java
+++ b/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-spring/shardingsphere-ha-spring-namespace/src/main/java/org/apache/shardingsphere/ha/spring/namespace/parser/HARuleBeanDefinitionParser.java
@@ -63,6 +63,7 @@ public final class HARuleBeanDefinitionParser extends AbstractBeanDefinitionPars
factory.addConstructorArgValue(element.getAttribute(HARuleBeanDefinitionTag.PRIMARY_DATA_SOURCE_NAME_ATTRIBUTE));
factory.addConstructorArgValue(parseReplicaDataSourcesRef(element));
factory.addConstructorArgValue(element.getAttribute(HARuleBeanDefinitionTag.LOAD_BALANCE_ALGORITHM_REF_ATTRIBUTE));
+ factory.addConstructorArgValue(element.getAttribute(HARuleBeanDefinitionTag.READ_WRITE_SPLIT_ATTRIBUTE));
return factory.getBeanDefinition();
}
diff --git a/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-spring/shardingsphere-ha-spring-namespace/src/main/java/org/apache/shardingsphere/ha/spring/namespace/tag/HARuleBeanDefinitionTag.java b/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-spring/shardingsphere-ha-spring-namespace/src/main/java/org/apache/shardingsphere/ha/spring/namespace/tag/HARuleBeanDefinitionTag.java
index 8778375..080c445 100644
--- a/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-spring/shardingsphere-ha-spring-namespace/src/main/java/org/apache/shardingsphere/ha/spring/namespace/tag/HARuleBeanDefinitionTag.java
+++ b/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-spring/shardingsphere-ha-spring-namespace/src/main/java/org/apache/shardingsphere/ha/spring/namespace/tag/HARuleBeanDefinitionTag.java
@@ -37,4 +37,6 @@ public final class HARuleBeanDefinitionTag {
public static final String REPLICA_DATA_SOURCE_NAMES_ATTRIBUTE = "replica-data-source-names";
public static final String LOAD_BALANCE_ALGORITHM_REF_ATTRIBUTE = "load-balance-algorithm-ref";
+
+ public static final String READ_WRITE_SPLIT_ATTRIBUTE = "read-write-split";
}
diff --git a/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-spring/shardingsphere-ha-spring-namespace/src/main/resources/META-INF/namespace/ha.xsd b/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-spring/shardingsphere-ha-spring-namespace/src/main/resources/META-INF/namespace/ha.xsd
index 4dd95a9..b1d84f9 100644
--- a/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-spring/shardingsphere-ha-spring-namespace/src/main/resources/META-INF/namespace/ha.xsd
+++ b/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-spring/shardingsphere-ha-spring-namespace/src/main/resources/META-INF/namespace/ha.xsd
@@ -38,6 +38,7 @@
<xsd:attribute name="primary-data-source-name" type="xsd:string" use="required" />
<xsd:attribute name="replica-data-source-names" type="xsd:string" use="required" />
<xsd:attribute name="load-balance-algorithm-ref" type="xsd:string" />
+ <xsd:attribute name="read-write-split" type="xsd:boolean" />
</xsd:complexType>
</xsd:element>
diff --git a/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-spring/shardingsphere-ha-spring-namespace/src/test/java/org/apache/shardingsphere/ha/spring/namespace/HASpringNamespaceTest.java b/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-spring/shardingsphere-ha-spring-namespace/src/test/java/org/apache/shardingsphere/ha/spring/namespace/HASpringNamespaceTest.java
index bcb3fd5..04d86ad 100644
--- a/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-spring/shardingsphere-ha-spring-namespace/src/test/java/org/apache/shardingsphere/ha/spring/namespace/HASpringNamespaceTest.java
+++ b/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-spring/shardingsphere-ha-spring-namespace/src/test/java/org/apache/shardingsphere/ha/spring/namespace/HASpringNamespaceTest.java
@@ -32,6 +32,7 @@ import java.util.Map;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
@ContextConfiguration(locations = "classpath:META-INF/spring/ha-application-context.xml")
public final class HASpringNamespaceTest extends AbstractJUnit4SpringContextTests {
@@ -81,5 +82,6 @@ public final class HASpringNamespaceTest extends AbstractJUnit4SpringContextTest
assertThat(dataSourceRuleConfig.getPrimaryDataSourceName(), is("primary_ds"));
assertThat(dataSourceRuleConfig.getReplicaDataSourceNames(), is(Arrays.asList("replica_ds_0", "replica_ds_1")));
assertThat(dataSourceRuleConfig.getLoadBalancerName(), is("randomLoadbalancer"));
+ assertTrue(dataSourceRuleConfig.getReadWriteSplit());
}
}
diff --git a/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-spring/shardingsphere-ha-spring-namespace/src/test/resources/META-INF/spring/ha-application-context.xml b/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-spring/shardingsphere-ha-spring-namespace/src/test/resources/META-INF/spring/ha-application-context.xml
index 4ff001c..85368eb 100644
--- a/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-spring/shardingsphere-ha-spring-namespace/src/test/resources/META-INF/spring/ha-application-context.xml
+++ b/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-spring/shardingsphere-ha-spring-namespace/src/test/resources/META-INF/spring/ha-application-context.xml
@@ -31,6 +31,6 @@
</ha:rule>
<ha:rule id="randomRule">
- <ha:data-source-rule id="random_ds" primary-data-source-name="primary_ds" replica-data-source-names="replica_ds_0, replica_ds_1" load-balance-algorithm-ref="randomLoadbalancer" />
+ <ha:data-source-rule id="random_ds" primary-data-source-name="primary_ds" replica-data-source-names="replica_ds_0, replica_ds_1" load-balance-algorithm-ref="randomLoadbalancer" read-write-split="true"/>
</ha:rule>
</beans>
diff --git a/shardingsphere-jdbc/shardingsphere-jdbc-core/pom.xml b/shardingsphere-jdbc/shardingsphere-jdbc-core/pom.xml
index 0a32847..faddd49 100644
--- a/shardingsphere-jdbc/shardingsphere-jdbc-core/pom.xml
+++ b/shardingsphere-jdbc/shardingsphere-jdbc-core/pom.xml
@@ -74,6 +74,11 @@
</dependency>
<dependency>
<groupId>org.apache.shardingsphere</groupId>
+ <artifactId>shardingsphere-ha-route</artifactId>
+ <version>${project.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-sharding-rewrite</artifactId>
<version>${project.version}</version>
</dependency>
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/pom.xml b/shardingsphere-proxy/shardingsphere-proxy-backend/pom.xml
index 49d896a..852db6a 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/pom.xml
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/pom.xml
@@ -79,6 +79,11 @@
</dependency>
<dependency>
<groupId>org.apache.shardingsphere</groupId>
+ <artifactId>shardingsphere-ha-route</artifactId>
+ <version>${project.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-sharding-rewrite</artifactId>
<version>${project.version}</version>
</dependency>
diff --git a/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/resources/conf/config-ha.yaml b/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/resources/conf/config-ha.yaml
index 38ce034..ad87728 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/resources/conf/config-ha.yaml
+++ b/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/resources/conf/config-ha.yaml
@@ -87,3 +87,4 @@
# replicaDataSourceNames:
# - replica_ds_0
# - replica_ds_1
+# readWriteSplit: false