未验证 提交 52aa585f 编写于 作者: L Liang Zhang 提交者: GitHub

Refactor Master-Slave Java API (#5910)

* rename MasterSlaveDataSourceConfiguration implement RuleConfiguration

* rename MasterSlaveDataSourceConfiguration.loadBalanceStrategy

* rename MasterSlaveDataSourceRuleConfiguration
上级 b6f9ddd7
......@@ -9,22 +9,22 @@ weight = 2
可配置属性:
| *名称* | *数据类型* | *说明* |
| --------------- | ------------------------------------------------ | ------------ |
| dataSources (+) | Collection\<MasterSlaveDataSourceConfiguration\> | 主从数据源列表 |
| *名称* | *数据类型* | *说明* |
| --------------- | ---------------------------------------------------- | ------------ |
| dataSources (+) | Collection\<MasterSlaveDataSourceRuleConfiguration\> | 主从数据源列表 |
## 主从数据源配置
类名称:org.apache.shardingsphere.masterslave.api.config.MasterSlaveDataSourceConfiguration
类名称:org.apache.shardingsphere.masterslave.api.config.MasterSlaveDataSourceRuleConfiguration
可配置属性:
| *名称* | *数据类型* | *说明* | *默认值* |
| ------------------------------------ | -------------------------------- | ---------------- | -------------- |
| name | String | 读写分离数据源名称 | - |
| masterDataSourceName | String | 主库数据源名称 | - |
| slaveDataSourceNames | Collection\<String\> | 从库数据源名称列表 | - |
| loadBalanceStrategyConfiguration (?) | LoadBalanceStrategyConfiguration | 从库负载均衡算法 | 轮询负载均衡算法 |
| *名称* | *数据类型* | *说明* | *默认值* |
| ----------------------- | -------------------------------- | ---------------- | -------------- |
| name | String | 读写分离数据源名称 | - |
| masterDataSourceName | String | 主库数据源名称 | - |
| slaveDataSourceNames | Collection\<String\> | 从库数据源名称列表 | - |
| loadBalanceStrategy (?) | LoadBalanceStrategyConfiguration | 从库负载均衡算法 | 轮询负载均衡算法 |
## 从库负载均衡策略配置
......
......@@ -9,22 +9,22 @@ Class name: org.apache.shardingsphere.masterslave.api.config.MasterSlaveRuleConf
Attributes:
| *Name* | *DataType* | *Description* |
| ------------------------ | ------------------------------------------------ | ------------------------------------ |
| dataSources (+) | Collection\<MasterSlaveDataSourceConfiguration\> | Data sources of master and slaves |
| *Name* | *DataType* | *Description* |
| --------------- | ---------------------------------------------------- | --------------------------------- |
| dataSources (+) | Collection\<MasterSlaveDataSourceRuleConfiguration\> | Data sources of master and slaves |
## Master Slave Data Source Configuration
Class name: org.apache.shardingsphere.masterslave.api.config.MasterSlaveDataSourceConfiguration
Class name: org.apache.shardingsphere.masterslave.api.config.MasterSlaveDataSourceRuleConfiguration
Attributes:
| *Name* | *DataType* | *Description* | *Default Value* |
| ------------------------------------ | -------------------------------- | ------------------------------------- | ---------------------------------- |
| name | String | Read-write split data source name | - |
| masterDataSourceName | String | Master database source name | - |
| slaveDataSourceNames | Collection\<String\> | Slave database source name list | - |
| loadBalanceStrategyConfiguration (?) | LoadBalanceStrategyConfiguration | Slave database load balance algorithm | Round robin load balance algorithm |
| *Name* | *DataType* | *Description* | *Default Value* |
| ----------------------- | -------------------------------- | ------------------------------------- | ---------------------------------- |
| name | String | Read-write split data source name | - |
| masterDataSourceName | String | Master database source name | - |
| slaveDataSourceNames | Collection\<String\> | Slave database source name list | - |
| loadBalanceStrategy (?) | LoadBalanceStrategyConfiguration | Slave database load balance algorithm | Round robin load balance algorithm |
## Slave Data Sources Load Balance Strategy Configuration
......
......@@ -17,7 +17,7 @@
package org.apache.shardingsphere.example.orchestration.raw.jdbc.config.local;
import org.apache.shardingsphere.masterslave.api.config.MasterSlaveDataSourceConfiguration;
import org.apache.shardingsphere.masterslave.api.config.MasterSlaveDataSourceRuleConfiguration;
import org.apache.shardingsphere.masterslave.api.config.MasterSlaveRuleConfiguration;
import org.apache.shardingsphere.example.config.ExampleConfiguration;
import org.apache.shardingsphere.example.core.api.DataSourceUtil;
......@@ -43,7 +43,7 @@ public final class LocalMasterSlaveConfiguration implements ExampleConfiguration
@Override
public DataSource getDataSource() throws SQLException {
MasterSlaveDataSourceConfiguration dataSourceConfiguration = new MasterSlaveDataSourceConfiguration(
MasterSlaveDataSourceRuleConfiguration dataSourceConfiguration = new MasterSlaveDataSourceRuleConfiguration(
"demo_ds_master_slave", "demo_ds_master", Arrays.asList("demo_ds_slave_0", "demo_ds_slave_1"));
MasterSlaveRuleConfiguration masterSlaveRuleConfig = new MasterSlaveRuleConfiguration(Collections.singleton(dataSourceConfiguration));
OrchestrationConfiguration orchestrationConfig = new OrchestrationConfiguration(centerConfigurationMap);
......
......@@ -23,7 +23,7 @@ import org.apache.shardingsphere.example.algorithm.StandardModuloShardingTableAl
import org.apache.shardingsphere.example.config.ExampleConfiguration;
import org.apache.shardingsphere.example.core.api.DataSourceUtil;
import org.apache.shardingsphere.infra.config.RuleConfiguration;
import org.apache.shardingsphere.masterslave.api.config.MasterSlaveDataSourceConfiguration;
import org.apache.shardingsphere.masterslave.api.config.MasterSlaveDataSourceRuleConfiguration;
import org.apache.shardingsphere.masterslave.api.config.MasterSlaveRuleConfiguration;
import org.apache.shardingsphere.orchestration.center.config.CenterConfiguration;
import org.apache.shardingsphere.orchestration.center.config.OrchestrationConfiguration;
......@@ -92,9 +92,9 @@ public final class LocalShardingMasterSlaveConfiguration implements ExampleConfi
}
private MasterSlaveRuleConfiguration getMasterSlaveRuleConfiguration() {
MasterSlaveDataSourceConfiguration dataSourceConfiguration1 = new MasterSlaveDataSourceConfiguration(
MasterSlaveDataSourceRuleConfiguration dataSourceConfiguration1 = new MasterSlaveDataSourceRuleConfiguration(
"ds_0", "demo_ds_master_0", Arrays.asList("demo_ds_master_0_slave_0", "demo_ds_master_0_slave_1"));
MasterSlaveDataSourceConfiguration dataSourceConfiguration2 = new MasterSlaveDataSourceConfiguration(
MasterSlaveDataSourceRuleConfiguration dataSourceConfiguration2 = new MasterSlaveDataSourceRuleConfiguration(
"ds_1", "demo_ds_master_1", Arrays.asList("demo_ds_master_1_slave_0", "demo_ds_master_1_slave_1"));
return new MasterSlaveRuleConfiguration(Arrays.asList(dataSourceConfiguration1, dataSourceConfiguration2));
}
......
......@@ -20,7 +20,7 @@ package org.apache.shardingsphere.example.shadow.table.raw.jdbc.config;
import org.apache.shardingsphere.driver.api.ShardingSphereDataSourceFactory;
import org.apache.shardingsphere.example.config.ExampleConfiguration;
import org.apache.shardingsphere.example.core.api.DataSourceUtil;
import org.apache.shardingsphere.masterslave.api.config.MasterSlaveDataSourceConfiguration;
import org.apache.shardingsphere.masterslave.api.config.MasterSlaveDataSourceRuleConfiguration;
import org.apache.shardingsphere.masterslave.api.config.MasterSlaveRuleConfiguration;
import org.apache.shardingsphere.shadow.api.config.ShadowRuleConfiguration;
......@@ -51,7 +51,7 @@ public final class MasterSlaveShadowDatabasesConfiguration implements ExampleCon
}
private MasterSlaveRuleConfiguration getMasterSlaveRuleConfiguration() {
MasterSlaveDataSourceConfiguration masterSlaveDataSourceConfiguration = new MasterSlaveDataSourceConfiguration("ds_ms", "ds_master", Collections.singletonList("ds_slave"));
return new MasterSlaveRuleConfiguration(Collections.singletonList(masterSlaveDataSourceConfiguration));
MasterSlaveDataSourceRuleConfiguration masterSlaveDataSourceRuleConfiguration = new MasterSlaveDataSourceRuleConfiguration("ds_ms", "ds_master", Collections.singletonList("ds_slave"));
return new MasterSlaveRuleConfiguration(Collections.singletonList(masterSlaveDataSourceRuleConfiguration));
}
}
......@@ -17,7 +17,7 @@
package org.apache.shardingsphere.example.sharding.raw.jdbc.config;
import org.apache.shardingsphere.masterslave.api.config.MasterSlaveDataSourceConfiguration;
import org.apache.shardingsphere.masterslave.api.config.MasterSlaveDataSourceRuleConfiguration;
import org.apache.shardingsphere.masterslave.api.config.MasterSlaveRuleConfiguration;
import org.apache.shardingsphere.example.config.ExampleConfiguration;
import org.apache.shardingsphere.example.core.api.DataSourceUtil;
......@@ -35,7 +35,7 @@ public final class MasterSlaveConfiguration implements ExampleConfiguration {
@Override
public DataSource getDataSource() throws SQLException {
MasterSlaveDataSourceConfiguration dataSourceConfiguration = new MasterSlaveDataSourceConfiguration(
MasterSlaveDataSourceRuleConfiguration dataSourceConfiguration = new MasterSlaveDataSourceRuleConfiguration(
"demo_ds_master_slave", "demo_ds_master", Arrays.asList("demo_ds_slave_0", "demo_ds_slave_1"));
MasterSlaveRuleConfiguration masterSlaveRuleConfig = new MasterSlaveRuleConfiguration(Collections.singleton(dataSourceConfiguration));
return ShardingSphereDataSourceFactory.createDataSource(createDataSourceMap(), Collections.singleton(masterSlaveRuleConfig), new Properties());
......
......@@ -21,7 +21,7 @@ import org.apache.shardingsphere.example.algorithm.StandardModuloShardingDatabas
import org.apache.shardingsphere.example.algorithm.StandardModuloShardingTableAlgorithm;
import org.apache.shardingsphere.example.config.ExampleConfiguration;
import org.apache.shardingsphere.example.core.api.DataSourceUtil;
import org.apache.shardingsphere.masterslave.api.config.MasterSlaveDataSourceConfiguration;
import org.apache.shardingsphere.masterslave.api.config.MasterSlaveDataSourceRuleConfiguration;
import org.apache.shardingsphere.masterslave.api.config.MasterSlaveRuleConfiguration;
import org.apache.shardingsphere.sharding.api.config.KeyGeneratorConfiguration;
import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration;
......@@ -86,9 +86,9 @@ public final class ShardingMasterSlaveConfigurationPrecise implements ExampleCon
}
private static MasterSlaveRuleConfiguration createMasterSlaveRuleConfiguration() {
MasterSlaveDataSourceConfiguration dataSourceConfiguration1 = new MasterSlaveDataSourceConfiguration(
MasterSlaveDataSourceRuleConfiguration dataSourceConfiguration1 = new MasterSlaveDataSourceRuleConfiguration(
"ds_0", "demo_ds_master_0", Arrays.asList("demo_ds_master_0_slave_0", "demo_ds_master_0_slave_1"));
MasterSlaveDataSourceConfiguration dataSourceConfiguration2 = new MasterSlaveDataSourceConfiguration(
MasterSlaveDataSourceRuleConfiguration dataSourceConfiguration2 = new MasterSlaveDataSourceRuleConfiguration(
"ds_1", "demo_ds_master_1", Arrays.asList("demo_ds_master_1_slave_0", "demo_ds_master_1_slave_1"));
return new MasterSlaveRuleConfiguration(Arrays.asList(dataSourceConfiguration1, dataSourceConfiguration2));
}
......
......@@ -21,7 +21,7 @@ import org.apache.shardingsphere.example.algorithm.StandardModuloShardingDatabas
import org.apache.shardingsphere.example.algorithm.StandardModuloShardingTableAlgorithm;
import org.apache.shardingsphere.example.config.ExampleConfiguration;
import org.apache.shardingsphere.example.core.api.DataSourceUtil;
import org.apache.shardingsphere.masterslave.api.config.MasterSlaveDataSourceConfiguration;
import org.apache.shardingsphere.masterslave.api.config.MasterSlaveDataSourceRuleConfiguration;
import org.apache.shardingsphere.masterslave.api.config.MasterSlaveRuleConfiguration;
import org.apache.shardingsphere.sharding.api.config.KeyGeneratorConfiguration;
import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration;
......@@ -86,9 +86,9 @@ public final class ShardingMasterSlaveConfigurationRange implements ExampleConfi
}
private static MasterSlaveRuleConfiguration createMasterSlaveRuleConfiguration() {
MasterSlaveDataSourceConfiguration dataSourceConfiguration1 = new MasterSlaveDataSourceConfiguration(
MasterSlaveDataSourceRuleConfiguration dataSourceConfiguration1 = new MasterSlaveDataSourceRuleConfiguration(
"ds_0", "demo_ds_master_0", Arrays.asList("demo_ds_master_0_slave_0", "demo_ds_master_0_slave_1"));
MasterSlaveDataSourceConfiguration dataSourceConfiguration2 = new MasterSlaveDataSourceConfiguration(
MasterSlaveDataSourceRuleConfiguration dataSourceConfiguration2 = new MasterSlaveDataSourceRuleConfiguration(
"ds_1", "demo_ds_master_1", Arrays.asList("demo_ds_master_1_slave_0", "demo_ds_master_1_slave_1"));
return new MasterSlaveRuleConfiguration(Arrays.asList(dataSourceConfiguration1, dataSourceConfiguration2));
}
......
......@@ -20,15 +20,14 @@ package org.apache.shardingsphere.masterslave.api.config;
import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import lombok.Getter;
import org.apache.shardingsphere.infra.config.RuleConfiguration;
import java.util.List;
/**
* Master-slave data source configuration.
* Master-slave data source rule configuration.
*/
@Getter
public final class MasterSlaveDataSourceConfiguration implements RuleConfiguration {
public final class MasterSlaveDataSourceRuleConfiguration {
private final String name;
......@@ -36,20 +35,20 @@ public final class MasterSlaveDataSourceConfiguration implements RuleConfigurati
private final List<String> slaveDataSourceNames;
private final LoadBalanceStrategyConfiguration loadBalanceStrategyConfiguration;
private final LoadBalanceStrategyConfiguration loadBalanceStrategy;
public MasterSlaveDataSourceConfiguration(final String name, final String masterDataSourceName, final List<String> slaveDataSourceNames) {
public MasterSlaveDataSourceRuleConfiguration(final String name, final String masterDataSourceName, final List<String> slaveDataSourceNames) {
this(name, masterDataSourceName, slaveDataSourceNames, null);
}
public MasterSlaveDataSourceConfiguration(final String name,
final String masterDataSourceName, final List<String> slaveDataSourceNames, final LoadBalanceStrategyConfiguration loadBalanceStrategyConfiguration) {
public MasterSlaveDataSourceRuleConfiguration(final String name,
final String masterDataSourceName, final List<String> slaveDataSourceNames, final LoadBalanceStrategyConfiguration loadBalanceStrategy) {
Preconditions.checkArgument(!Strings.isNullOrEmpty(name), "Name is required.");
Preconditions.checkArgument(!Strings.isNullOrEmpty(masterDataSourceName), "Master data source name is required.");
Preconditions.checkArgument(null != slaveDataSourceNames && !slaveDataSourceNames.isEmpty(), "Slave data source names are required.");
this.name = name;
this.masterDataSourceName = masterDataSourceName;
this.slaveDataSourceNames = slaveDataSourceNames;
this.loadBalanceStrategyConfiguration = loadBalanceStrategyConfiguration;
this.loadBalanceStrategy = loadBalanceStrategy;
}
}
......@@ -30,5 +30,5 @@ import java.util.Collection;
@Getter
public final class MasterSlaveRuleConfiguration implements RuleConfiguration {
private final Collection<MasterSlaveDataSourceConfiguration> dataSources;
private final Collection<MasterSlaveDataSourceRuleConfiguration> dataSources;
}
......@@ -25,44 +25,44 @@ import java.util.Collections;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
public final class MasterSlaveDataSourceConfigurationTest {
public final class MasterSlaveDataSourceRuleConfigurationTest {
@Test(expected = IllegalArgumentException.class)
public void assertConstructorWithoutName() {
new MasterSlaveDataSourceConfiguration("", "master_ds", Collections.singletonList("slave_ds"));
new MasterSlaveDataSourceRuleConfiguration("", "master_ds", Collections.singletonList("slave_ds"));
}
@Test(expected = IllegalArgumentException.class)
public void assertConstructorWithoutMasterDataSourceName() {
new MasterSlaveDataSourceConfiguration("ds", "", Collections.singletonList("slave_ds"));
new MasterSlaveDataSourceRuleConfiguration("ds", "", Collections.singletonList("slave_ds"));
}
@Test(expected = IllegalArgumentException.class)
public void assertConstructorWithoutSlaveDataSourceNames() {
new MasterSlaveDataSourceConfiguration("ds", "master_ds", null);
new MasterSlaveDataSourceRuleConfiguration("ds", "master_ds", null);
}
@Test(expected = IllegalArgumentException.class)
public void assertConstructorWithEmptySlaveDataSourceNames() {
new MasterSlaveDataSourceConfiguration("ds", "master_ds", Collections.emptyList());
new MasterSlaveDataSourceRuleConfiguration("ds", "master_ds", Collections.emptyList());
}
@Test
public void assertConstructorWithMinArguments() {
MasterSlaveDataSourceConfiguration actual = new MasterSlaveDataSourceConfiguration("ds", "master_ds", Collections.singletonList("slave_ds"));
MasterSlaveDataSourceRuleConfiguration actual = new MasterSlaveDataSourceRuleConfiguration("ds", "master_ds", Collections.singletonList("slave_ds"));
assertThat(actual.getName(), CoreMatchers.is("ds"));
assertThat(actual.getMasterDataSourceName(), CoreMatchers.is("master_ds"));
assertThat(actual.getSlaveDataSourceNames(), CoreMatchers.is(Collections.singletonList("slave_ds")));
assertNull(actual.getLoadBalanceStrategyConfiguration());
assertNull(actual.getLoadBalanceStrategy());
}
@Test
public void assertConstructorWithMaxArguments() {
MasterSlaveDataSourceConfiguration actual = new MasterSlaveDataSourceConfiguration(
MasterSlaveDataSourceRuleConfiguration actual = new MasterSlaveDataSourceRuleConfiguration(
"ds", "master_ds", Collections.singletonList("slave_ds"), new LoadBalanceStrategyConfiguration("ROUND_ROBIN"));
assertThat(actual.getName(), CoreMatchers.is("ds"));
assertThat(actual.getMasterDataSourceName(), CoreMatchers.is("master_ds"));
assertThat(actual.getSlaveDataSourceNames(), CoreMatchers.is(Collections.singletonList("slave_ds")));
assertThat(actual.getLoadBalanceStrategyConfiguration().getType(), CoreMatchers.is("ROUND_ROBIN"));
assertThat(actual.getLoadBalanceStrategy().getType(), CoreMatchers.is("ROUND_ROBIN"));
}
}
......@@ -20,7 +20,7 @@ package org.apache.shardingsphere.masterslave.rule;
import lombok.AccessLevel;
import lombok.Getter;
import org.apache.shardingsphere.masterslave.api.config.LoadBalanceStrategyConfiguration;
import org.apache.shardingsphere.masterslave.api.config.MasterSlaveDataSourceConfiguration;
import org.apache.shardingsphere.masterslave.api.config.MasterSlaveDataSourceRuleConfiguration;
import org.apache.shardingsphere.infra.spi.ShardingSphereServiceLoader;
import org.apache.shardingsphere.masterslave.spi.MasterSlaveLoadBalanceAlgorithm;
import org.apache.shardingsphere.infra.spi.type.TypedSPIRegistry;
......@@ -54,11 +54,11 @@ public final class MasterSlaveDataSourceRule {
@Getter(AccessLevel.NONE)
private final Collection<String> disabledDataSourceNames = new HashSet<>();
public MasterSlaveDataSourceRule(final MasterSlaveDataSourceConfiguration configuration) {
public MasterSlaveDataSourceRule(final MasterSlaveDataSourceRuleConfiguration configuration) {
name = configuration.getName();
masterDataSourceName = configuration.getMasterDataSourceName();
slaveDataSourceNames = configuration.getSlaveDataSourceNames();
loadBalanceAlgorithm = createLoadBalanceAlgorithm(configuration.getLoadBalanceStrategyConfiguration());
loadBalanceAlgorithm = createLoadBalanceAlgorithm(configuration.getLoadBalanceStrategy());
}
private MasterSlaveLoadBalanceAlgorithm createLoadBalanceAlgorithm(final LoadBalanceStrategyConfiguration configuration) {
......
......@@ -18,7 +18,7 @@
package org.apache.shardingsphere.masterslave.rule;
import com.google.common.base.Preconditions;
import org.apache.shardingsphere.masterslave.api.config.MasterSlaveDataSourceConfiguration;
import org.apache.shardingsphere.masterslave.api.config.MasterSlaveDataSourceRuleConfiguration;
import org.apache.shardingsphere.masterslave.api.config.MasterSlaveRuleConfiguration;
import org.apache.shardingsphere.infra.rule.event.impl.DataSourceNameDisabledEvent;
import org.apache.shardingsphere.infra.rule.DataSourceRoutedRule;
......@@ -41,7 +41,7 @@ public final class MasterSlaveRule implements DataSourceRoutedRule, StatusContai
public MasterSlaveRule(final MasterSlaveRuleConfiguration configuration) {
Preconditions.checkArgument(!configuration.getDataSources().isEmpty(), "Master-slave data source rules can not be empty.");
dataSourceRules = new HashMap<>(configuration.getDataSources().size(), 1);
for (MasterSlaveDataSourceConfiguration each : configuration.getDataSources()) {
for (MasterSlaveDataSourceRuleConfiguration each : configuration.getDataSources()) {
dataSourceRules.put(each.getName(), new MasterSlaveDataSourceRule(each));
}
}
......
......@@ -26,11 +26,11 @@ import java.util.List;
import java.util.Properties;
/**
* Master-slave data source configuration for YAML.
* Master-slave data source rule configuration for YAML.
*/
@Getter
@Setter
public final class YamlMasterSlaveDataSourceConfiguration implements YamlConfiguration {
public final class YamlMasterSlaveDataSourceRuleConfiguration implements YamlConfiguration {
private String name;
......
......@@ -32,7 +32,7 @@ import java.util.Map;
@Setter
public final class YamlMasterSlaveRuleConfiguration implements YamlRuleConfiguration {
private Map<String, YamlMasterSlaveDataSourceConfiguration> dataSources = new LinkedHashMap<>();
private Map<String, YamlMasterSlaveDataSourceRuleConfiguration> dataSources = new LinkedHashMap<>();
@Override
public Class<MasterSlaveRuleConfiguration> getRuleConfigurationType() {
......
......@@ -19,10 +19,10 @@ package org.apache.shardingsphere.masterslave.yaml.swapper;
import com.google.common.base.Strings;
import org.apache.shardingsphere.masterslave.api.config.LoadBalanceStrategyConfiguration;
import org.apache.shardingsphere.masterslave.api.config.MasterSlaveDataSourceConfiguration;
import org.apache.shardingsphere.masterslave.api.config.MasterSlaveDataSourceRuleConfiguration;
import org.apache.shardingsphere.masterslave.api.config.MasterSlaveRuleConfiguration;
import org.apache.shardingsphere.masterslave.constant.MasterSlaveOrder;
import org.apache.shardingsphere.masterslave.yaml.config.YamlMasterSlaveDataSourceConfiguration;
import org.apache.shardingsphere.masterslave.yaml.config.YamlMasterSlaveDataSourceRuleConfiguration;
import org.apache.shardingsphere.masterslave.yaml.config.YamlMasterSlaveRuleConfiguration;
import org.apache.shardingsphere.infra.yaml.swapper.YamlRuleConfigurationSwapper;
......@@ -40,35 +40,35 @@ public final class MasterSlaveRuleConfigurationYamlSwapper implements YamlRuleCo
@Override
public YamlMasterSlaveRuleConfiguration swap(final MasterSlaveRuleConfiguration data) {
YamlMasterSlaveRuleConfiguration result = new YamlMasterSlaveRuleConfiguration();
result.setDataSources(data.getDataSources().stream().collect(Collectors.toMap(MasterSlaveDataSourceConfiguration::getName, this::swap, (a, b) -> b, LinkedHashMap::new)));
result.setDataSources(data.getDataSources().stream().collect(Collectors.toMap(MasterSlaveDataSourceRuleConfiguration::getName, this::swap, (a, b) -> b, LinkedHashMap::new)));
return result;
}
private YamlMasterSlaveDataSourceConfiguration swap(final MasterSlaveDataSourceConfiguration group) {
YamlMasterSlaveDataSourceConfiguration result = new YamlMasterSlaveDataSourceConfiguration();
private YamlMasterSlaveDataSourceRuleConfiguration swap(final MasterSlaveDataSourceRuleConfiguration group) {
YamlMasterSlaveDataSourceRuleConfiguration result = new YamlMasterSlaveDataSourceRuleConfiguration();
result.setName(group.getName());
result.setMasterDataSourceName(group.getMasterDataSourceName());
result.setSlaveDataSourceNames(group.getSlaveDataSourceNames());
if (null != group.getLoadBalanceStrategyConfiguration()) {
result.setLoadBalanceAlgorithmType(group.getLoadBalanceStrategyConfiguration().getType());
if (null != group.getLoadBalanceStrategy()) {
result.setLoadBalanceAlgorithmType(group.getLoadBalanceStrategy().getType());
}
return result;
}
@Override
public MasterSlaveRuleConfiguration swap(final YamlMasterSlaveRuleConfiguration yamlConfiguration) {
Collection<MasterSlaveDataSourceConfiguration> groups = new LinkedList<>();
for (Entry<String, YamlMasterSlaveDataSourceConfiguration> entry : yamlConfiguration.getDataSources().entrySet()) {
Collection<MasterSlaveDataSourceRuleConfiguration> groups = new LinkedList<>();
for (Entry<String, YamlMasterSlaveDataSourceRuleConfiguration> entry : yamlConfiguration.getDataSources().entrySet()) {
groups.add(swap(entry.getKey(), entry.getValue()));
}
return new MasterSlaveRuleConfiguration(groups);
}
private MasterSlaveDataSourceConfiguration swap(final String name, final YamlMasterSlaveDataSourceConfiguration yamlGroup) {
return new MasterSlaveDataSourceConfiguration(name, yamlGroup.getMasterDataSourceName(), yamlGroup.getSlaveDataSourceNames(), getLoadBalanceStrategyConfiguration(yamlGroup));
private MasterSlaveDataSourceRuleConfiguration swap(final String name, final YamlMasterSlaveDataSourceRuleConfiguration yamlGroup) {
return new MasterSlaveDataSourceRuleConfiguration(name, yamlGroup.getMasterDataSourceName(), yamlGroup.getSlaveDataSourceNames(), getLoadBalanceStrategyConfiguration(yamlGroup));
}
private LoadBalanceStrategyConfiguration getLoadBalanceStrategyConfiguration(final YamlMasterSlaveDataSourceConfiguration yamlGroup) {
private LoadBalanceStrategyConfiguration getLoadBalanceStrategyConfiguration(final YamlMasterSlaveDataSourceRuleConfiguration yamlGroup) {
return Strings.isNullOrEmpty(yamlGroup.getLoadBalanceAlgorithmType()) ? null : new LoadBalanceStrategyConfiguration(yamlGroup.getLoadBalanceAlgorithmType(), yamlGroup.getProps());
}
......
......@@ -18,7 +18,7 @@
package org.apache.shardingsphere.masterslave.log;
import lombok.SneakyThrows;
import org.apache.shardingsphere.masterslave.api.config.MasterSlaveDataSourceConfiguration;
import org.apache.shardingsphere.masterslave.api.config.MasterSlaveDataSourceRuleConfiguration;
import org.apache.shardingsphere.masterslave.api.config.MasterSlaveRuleConfiguration;
import org.apache.shardingsphere.infra.log.ConfigurationLogger;
import org.junit.Before;
......@@ -75,7 +75,7 @@ public final class MasterSlaveConfigurationLoggerTest {
}
private MasterSlaveRuleConfiguration getMasterSlaveRuleConfiguration() {
return new MasterSlaveRuleConfiguration(Collections.singleton(new MasterSlaveDataSourceConfiguration("ms_ds", "master_ds", Arrays.asList("slave_ds_0", "slave_ds_1"))));
return new MasterSlaveRuleConfiguration(Collections.singleton(new MasterSlaveDataSourceRuleConfiguration("ms_ds", "master_ds", Arrays.asList("slave_ds_0", "slave_ds_1"))));
}
private void assertLogInfo(final String logContent) {
......
......@@ -18,7 +18,7 @@
package org.apache.shardingsphere.masterslave.rule;
import org.apache.shardingsphere.masterslave.api.config.LoadBalanceStrategyConfiguration;
import org.apache.shardingsphere.masterslave.api.config.MasterSlaveDataSourceConfiguration;
import org.apache.shardingsphere.masterslave.api.config.MasterSlaveDataSourceRuleConfiguration;
import org.junit.Test;
import java.util.Arrays;
......@@ -30,7 +30,7 @@ import static org.junit.Assert.assertThat;
public final class MasterSlaveDataSourceRuleTest {
private final MasterSlaveDataSourceRule masterSlaveDataSourceRule = new MasterSlaveDataSourceRule(
new MasterSlaveDataSourceConfiguration("test_ms", "master_db", Arrays.asList("slave_db_0", "slave_db_1"), new LoadBalanceStrategyConfiguration("RANDOM")));
new MasterSlaveDataSourceRuleConfiguration("test_ms", "master_db", Arrays.asList("slave_db_0", "slave_db_1"), new LoadBalanceStrategyConfiguration("RANDOM")));
@Test
public void assertGetSlaveDataSourceNamesWithoutDisabledDataSourceNames() {
......
......@@ -18,7 +18,7 @@
package org.apache.shardingsphere.masterslave.rule;
import org.apache.shardingsphere.masterslave.api.config.LoadBalanceStrategyConfiguration;
import org.apache.shardingsphere.masterslave.api.config.MasterSlaveDataSourceConfiguration;
import org.apache.shardingsphere.masterslave.api.config.MasterSlaveDataSourceRuleConfiguration;
import org.apache.shardingsphere.masterslave.api.config.MasterSlaveRuleConfiguration;
import org.junit.Test;
......@@ -50,7 +50,7 @@ public final class MasterSlaveRuleTest {
}
private MasterSlaveRule createMasterSlaveRule() {
MasterSlaveDataSourceConfiguration configuration = new MasterSlaveDataSourceConfiguration(
MasterSlaveDataSourceRuleConfiguration configuration = new MasterSlaveDataSourceRuleConfiguration(
"test_ms", "master_db", Arrays.asList("slave_db_0", "slave_db_1"), new LoadBalanceStrategyConfiguration("RANDOM"));
return new MasterSlaveRule(new MasterSlaveRuleConfiguration(Collections.singleton(configuration)));
}
......
......@@ -18,9 +18,9 @@
package org.apache.shardingsphere.masterslave.yaml.swapper;
import org.apache.shardingsphere.masterslave.api.config.LoadBalanceStrategyConfiguration;
import org.apache.shardingsphere.masterslave.api.config.MasterSlaveDataSourceConfiguration;
import org.apache.shardingsphere.masterslave.api.config.MasterSlaveDataSourceRuleConfiguration;
import org.apache.shardingsphere.masterslave.api.config.MasterSlaveRuleConfiguration;
import org.apache.shardingsphere.masterslave.yaml.config.YamlMasterSlaveDataSourceConfiguration;
import org.apache.shardingsphere.masterslave.yaml.config.YamlMasterSlaveDataSourceRuleConfiguration;
import org.apache.shardingsphere.masterslave.yaml.config.YamlMasterSlaveRuleConfiguration;
import org.junit.Test;
......@@ -35,7 +35,7 @@ public final class MasterSlaveRuleConfigurationYamlSwapperTest {
@Test
public void assertSwapToYamlWithLoadBalanceAlgorithm() {
MasterSlaveDataSourceConfiguration dataSourceConfiguration = new MasterSlaveDataSourceConfiguration(
MasterSlaveDataSourceRuleConfiguration dataSourceConfiguration = new MasterSlaveDataSourceRuleConfiguration(
"ds", "master", Collections.singletonList("slave"), new LoadBalanceStrategyConfiguration("ROUND_ROBIN"));
YamlMasterSlaveRuleConfiguration actual = new MasterSlaveRuleConfigurationYamlSwapper().swap(new MasterSlaveRuleConfiguration(Collections.singleton(dataSourceConfiguration)));
assertThat(actual.getDataSources().get("ds").getName(), is("ds"));
......@@ -46,7 +46,7 @@ public final class MasterSlaveRuleConfigurationYamlSwapperTest {
@Test
public void assertSwapToYamlWithoutLoadBalanceAlgorithm() {
MasterSlaveDataSourceConfiguration dataSourceConfiguration = new MasterSlaveDataSourceConfiguration("ds", "master", Collections.singletonList("slave"));
MasterSlaveDataSourceRuleConfiguration dataSourceConfiguration = new MasterSlaveDataSourceRuleConfiguration("ds", "master", Collections.singletonList("slave"));
YamlMasterSlaveRuleConfiguration actual = new MasterSlaveRuleConfigurationYamlSwapper().swap(new MasterSlaveRuleConfiguration(Collections.singleton(dataSourceConfiguration)));
assertThat(actual.getDataSources().get("ds").getName(), is("ds"));
assertThat(actual.getDataSources().get("ds").getMasterDataSourceName(), is("master"));
......@@ -60,7 +60,7 @@ public final class MasterSlaveRuleConfigurationYamlSwapperTest {
yamlConfiguration.getDataSources().get("master_slave_ds").setLoadBalanceAlgorithmType("RANDOM");
MasterSlaveRuleConfiguration actual = new MasterSlaveRuleConfigurationYamlSwapper().swap(yamlConfiguration);
assertMasterSlaveRuleConfiguration(actual);
assertThat(actual.getDataSources().iterator().next().getLoadBalanceStrategyConfiguration().getType(), is("RANDOM"));
assertThat(actual.getDataSources().iterator().next().getLoadBalanceStrategy().getType(), is("RANDOM"));
}
@Test
......@@ -68,12 +68,12 @@ public final class MasterSlaveRuleConfigurationYamlSwapperTest {
YamlMasterSlaveRuleConfiguration yamlConfiguration = createYamlMasterSlaveRuleConfiguration();
MasterSlaveRuleConfiguration actual = new MasterSlaveRuleConfigurationYamlSwapper().swap(yamlConfiguration);
assertMasterSlaveRuleConfiguration(actual);
assertNull(actual.getDataSources().iterator().next().getLoadBalanceStrategyConfiguration());
assertNull(actual.getDataSources().iterator().next().getLoadBalanceStrategy());
}
private YamlMasterSlaveRuleConfiguration createYamlMasterSlaveRuleConfiguration() {
YamlMasterSlaveRuleConfiguration result = new YamlMasterSlaveRuleConfiguration();
result.getDataSources().put("master_slave_ds", new YamlMasterSlaveDataSourceConfiguration());
result.getDataSources().put("master_slave_ds", new YamlMasterSlaveDataSourceRuleConfiguration());
result.getDataSources().get("master_slave_ds").setName("master_slave_ds");
result.getDataSources().get("master_slave_ds").setMasterDataSourceName("master_ds");
result.getDataSources().get("master_slave_ds").setSlaveDataSourceNames(Arrays.asList("slave_ds_0", "slave_ds_1"));
......@@ -81,7 +81,7 @@ public final class MasterSlaveRuleConfigurationYamlSwapperTest {
}
private void assertMasterSlaveRuleConfiguration(final MasterSlaveRuleConfiguration actual) {
MasterSlaveDataSourceConfiguration group = actual.getDataSources().iterator().next();
MasterSlaveDataSourceRuleConfiguration group = actual.getDataSources().iterator().next();
assertThat(group.getName(), is("master_slave_ds"));
assertThat(group.getMasterDataSourceName(), is("master_ds"));
assertThat(group.getSlaveDataSourceNames(), is(Arrays.asList("slave_ds_0", "slave_ds_1")));
......
......@@ -17,7 +17,7 @@
package org.apache.shardingsphere.masterslave.route.engine;
import org.apache.shardingsphere.masterslave.api.config.MasterSlaveDataSourceConfiguration;
import org.apache.shardingsphere.masterslave.api.config.MasterSlaveDataSourceRuleConfiguration;
import org.apache.shardingsphere.masterslave.api.config.MasterSlaveRuleConfiguration;
import org.apache.shardingsphere.masterslave.rule.MasterSlaveRule;
import org.apache.shardingsphere.masterslave.route.engine.impl.MasterVisitedManager;
......@@ -77,7 +77,7 @@ public final class MasterSlaveRouteDecoratorTest {
public void setUp() {
routeDecorator = new MasterSlaveRouteDecorator();
masterSlaveRule = new MasterSlaveRule(
new MasterSlaveRuleConfiguration(Collections.singleton(new MasterSlaveDataSourceConfiguration(DATASOURCE_NAME, MASTER_DATASOURCE, Collections.singletonList(SLAVE_DATASOURCE)))));
new MasterSlaveRuleConfiguration(Collections.singleton(new MasterSlaveDataSourceRuleConfiguration(DATASOURCE_NAME, MASTER_DATASOURCE, Collections.singletonList(SLAVE_DATASOURCE)))));
}
@After
......
......@@ -19,7 +19,7 @@ package org.apache.shardingsphere.masterslave.spring.namespace.parser;
import com.google.common.base.Splitter;
import com.google.common.base.Strings;
import org.apache.shardingsphere.masterslave.api.config.MasterSlaveDataSourceConfiguration;
import org.apache.shardingsphere.masterslave.api.config.MasterSlaveDataSourceRuleConfiguration;
import org.apache.shardingsphere.masterslave.api.config.MasterSlaveRuleConfiguration;
import org.apache.shardingsphere.masterslave.spring.namespace.tag.MasterSlaveRuleBeanDefinitionTag;
import org.springframework.beans.factory.config.BeanDefinition;
......@@ -45,14 +45,14 @@ public final class MasterSlaveRuleBeanDefinitionParser extends AbstractBeanDefin
List<Element> masterSlaveDataSourceElements = DomUtils.getChildElementsByTagName(element, MasterSlaveRuleBeanDefinitionTag.MASTER_SLAVE_DATA_SOURCE_TAG);
List<BeanDefinition> masterSlaveDataSources = new ManagedList<>(masterSlaveDataSourceElements.size());
for (Element each : masterSlaveDataSourceElements) {
masterSlaveDataSources.add(parseMasterSlaveDataSourceConfiguration(each));
masterSlaveDataSources.add(parseMasterSlaveDataSourceRuleConfiguration(each));
}
factory.addConstructorArgValue(masterSlaveDataSources);
return factory.getBeanDefinition();
}
private BeanDefinition parseMasterSlaveDataSourceConfiguration(final Element element) {
BeanDefinitionBuilder factory = BeanDefinitionBuilder.rootBeanDefinition(MasterSlaveDataSourceConfiguration.class);
private BeanDefinition parseMasterSlaveDataSourceRuleConfiguration(final Element element) {
BeanDefinitionBuilder factory = BeanDefinitionBuilder.rootBeanDefinition(MasterSlaveDataSourceRuleConfiguration.class);
factory.addConstructorArgValue(element.getAttribute(MasterSlaveRuleBeanDefinitionTag.MASTER_SLAVE_DATA_SOURCE_ID_TAG));
factory.addConstructorArgValue(element.getAttribute(MasterSlaveRuleBeanDefinitionTag.MASTER_DATA_SOURCE_NAME_TAG));
factory.addConstructorArgValue(parseSlaveDataSourcesRef(element));
......
......@@ -19,7 +19,7 @@ package org.apache.shardingsphere.driver.orchestration.internal.datasource;
import org.apache.commons.dbcp2.BasicDataSource;
import org.apache.shardingsphere.masterslave.api.config.LoadBalanceStrategyConfiguration;
import org.apache.shardingsphere.masterslave.api.config.MasterSlaveDataSourceConfiguration;
import org.apache.shardingsphere.masterslave.api.config.MasterSlaveDataSourceRuleConfiguration;
import org.apache.shardingsphere.masterslave.api.config.MasterSlaveRuleConfiguration;
import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration;
import org.apache.shardingsphere.sharding.api.config.ShardingTableRuleConfiguration;
......@@ -121,7 +121,7 @@ public final class OrchestrationShardingSphereDataSourceTest {
}
private MasterSlaveRuleConfiguration getMasterSlaveRuleConfiguration() {
MasterSlaveDataSourceConfiguration dataSourceConfiguration = new MasterSlaveDataSourceConfiguration(
MasterSlaveDataSourceRuleConfiguration dataSourceConfiguration = new MasterSlaveDataSourceRuleConfiguration(
"ds_ms", "ds_m", Collections.singletonList("ds_s"), new LoadBalanceStrategyConfiguration("ROUND_ROBIN"));
return new MasterSlaveRuleConfiguration(Collections.singleton(dataSourceConfiguration));
}
......
......@@ -20,7 +20,7 @@ package org.apache.shardingsphere.proxy.config;
import org.apache.shardingsphere.encrypt.yaml.config.YamlEncryptRuleConfiguration;
import org.apache.shardingsphere.encrypt.yaml.config.YamlEncryptorConfiguration;
import org.apache.shardingsphere.orchestration.center.yaml.config.YamlCenterRepositoryConfiguration;
import org.apache.shardingsphere.masterslave.yaml.config.YamlMasterSlaveDataSourceConfiguration;
import org.apache.shardingsphere.masterslave.yaml.config.YamlMasterSlaveDataSourceRuleConfiguration;
import org.apache.shardingsphere.masterslave.yaml.config.YamlMasterSlaveRuleConfiguration;
import org.apache.shardingsphere.sharding.yaml.config.YamlShardingRuleConfiguration;
import org.apache.shardingsphere.proxy.config.yaml.YamlDataSourceParameter;
......@@ -96,12 +96,12 @@ public final class ShardingConfigurationLoaderTest {
Optional<YamlMasterSlaveRuleConfiguration> masterSlaveRuleConfiguration = actual.getRules().stream().filter(
each -> each instanceof YamlMasterSlaveRuleConfiguration).findFirst().map(configuration -> (YamlMasterSlaveRuleConfiguration) configuration);
assertTrue(masterSlaveRuleConfiguration.isPresent());
for (YamlMasterSlaveDataSourceConfiguration each : masterSlaveRuleConfiguration.get().getDataSources().values()) {
for (YamlMasterSlaveDataSourceRuleConfiguration each : masterSlaveRuleConfiguration.get().getDataSources().values()) {
assertMasterSlaveRuleConfiguration(each);
}
}
private void assertMasterSlaveRuleConfiguration(final YamlMasterSlaveDataSourceConfiguration actual) {
private void assertMasterSlaveRuleConfiguration(final YamlMasterSlaveDataSourceRuleConfiguration actual) {
assertThat(actual.getName(), is("ms_ds"));
assertThat(actual.getMasterDataSourceName(), is("master_ds"));
assertThat(actual.getSlaveDataSourceNames().size(), is(2));
......
......@@ -18,8 +18,9 @@
package org.apache.shardingsphere.ui.servcie.impl;
import com.google.common.base.Strings;
import org.apache.shardingsphere.infra.config.RuleConfiguration;
import org.apache.shardingsphere.infra.yaml.engine.YamlEngine;
import org.apache.shardingsphere.masterslave.api.config.MasterSlaveDataSourceConfiguration;
import org.apache.shardingsphere.masterslave.api.config.MasterSlaveDataSourceRuleConfiguration;
import org.apache.shardingsphere.masterslave.api.config.MasterSlaveRuleConfiguration;
import org.apache.shardingsphere.orchestration.core.registrycenter.RegistryCenterNodeStatus;
import org.apache.shardingsphere.orchestration.core.registrycenter.instance.InstanceState;
......@@ -29,7 +30,6 @@ import org.apache.shardingsphere.ui.servcie.OrchestrationService;
import org.apache.shardingsphere.ui.servcie.RegistryCenterService;
import org.apache.shardingsphere.ui.servcie.ShardingSchemaService;
import org.apache.shardingsphere.ui.util.ConfigurationYamlConverter;
import org.apache.shardingsphere.infra.config.RuleConfiguration;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
......@@ -111,12 +111,12 @@ public final class OrchestrationServiceImpl implements OrchestrationService {
private void addSlaveDataSource(final Collection<SlaveDataSourceDTO> slaveDataSourceDTOS, final MasterSlaveRuleConfiguration masterSlaveRuleConfiguration, final String schemaName) {
Collection<String> disabledSchemaDataSourceNames = getDisabledSchemaDataSourceNames();
for (MasterSlaveDataSourceConfiguration each : masterSlaveRuleConfiguration.getDataSources()) {
for (MasterSlaveDataSourceRuleConfiguration each : masterSlaveRuleConfiguration.getDataSources()) {
slaveDataSourceDTOS.addAll(getSlaveDataSourceDTOS(schemaName, disabledSchemaDataSourceNames, each));
}
}
private Collection<SlaveDataSourceDTO> getSlaveDataSourceDTOS(final String schemaName, final Collection<String> disabledSchemaDataSourceNames, final MasterSlaveDataSourceConfiguration group) {
private Collection<SlaveDataSourceDTO> getSlaveDataSourceDTOS(final String schemaName, final Collection<String> disabledSchemaDataSourceNames, final MasterSlaveDataSourceRuleConfiguration group) {
Collection<SlaveDataSourceDTO> result = new LinkedList<>();
for (String each : group.getSlaveDataSourceNames()) {
result.add(new SlaveDataSourceDTO(schemaName, group.getMasterDataSourceName(), each, !disabledSchemaDataSourceNames.contains(schemaName + "." + each)));
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册