Skip to content

Commit

Permalink
Merge pull request #1335 from tristaZero/dev
Browse files Browse the repository at this point in the history
Simply the configuration for orchestration
  • Loading branch information
terrymanu authored Oct 11, 2018
2 parents 9a986c8 + 65be51d commit 53485b5
Show file tree
Hide file tree
Showing 12 changed files with 55 additions and 24 deletions.
1 change: 1 addition & 0 deletions RELEASE-NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
1. [ISSUE #1289](https://github.com/sharding-sphere/sharding-sphere/issues/1289) Adjust Hint API
1. [ISSUE #1302](https://github.com/sharding-sphere/sharding-sphere/issues/1302) Refine package structure
1. [ISSUE #1305](https://github.com/sharding-sphere/sharding-sphere/issues/1305) Deprecated and remove sharding-jdbc-transaction-parent module
1. [ISSUE #1382](https://github.com/sharding-sphere/sharding-sphere/issues/1328) Remove type configuration in Orchestration module

### Bug Fixes

Expand Down
2 changes: 1 addition & 1 deletion RELEASE-NOTES_ZH.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
1. [ISSUE #1289](https://github.com/sharding-sphere/sharding-sphere/issues/1289) 调整Hint API
1. [ISSUE #1302](https://github.com/sharding-sphere/sharding-sphere/issues/1302) 调整包结构
1. [ISSUE #1305](https://github.com/sharding-sphere/sharding-sphere/issues/1305) 废弃并删除sharding-jdbc-transaction-parent模块

1. [ISSUE #1382](https://github.com/sharding-sphere/sharding-sphere/issues/1328) 去除Orchestration模块中type的配置

### 缺陷修正

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@

import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import io.shardingsphere.api.config.ShardingRuleConfiguration;
import io.shardingsphere.core.exception.ShardingException;
import io.shardingsphere.core.rule.ShardingRule;
import io.shardingsphere.orchestration.config.OrchestrationType;
import io.shardingsphere.orchestration.internal.OrchestrationFacade;
import io.shardingsphere.shardingjdbc.jdbc.core.datasource.MasterSlaveDataSource;
import io.shardingsphere.shardingjdbc.jdbc.core.datasource.ShardingDataSource;
import io.shardingsphere.shardingjdbc.orchestration.internal.datasource.OrchestrationMasterSlaveDataSource;
Expand Down Expand Up @@ -74,9 +76,42 @@ public class OrchestrationSpringBootConfiguration implements EnvironmentAware {
*/
@Bean
public DataSource dataSource() throws SQLException {
OrchestrationType type = orchestrationProperties.getType();
Preconditions.checkState(null != type, "Missing the type of datasource configuration in orchestration configuration");
return OrchestrationType.SHARDING == type ? createShardingDataSource() : createMasterSlaveDataSource();
Preconditions.checkState(isValidConfiguration(), "The orchestration configuration is invalid, please choose one from Sharding rule and Master-slave rule.");
return OrchestrationType.SHARDING == getOrchestrationType() ? createShardingDataSource() : createMasterSlaveDataSource();
}

private boolean isValidConfiguration() {
return isValidRuleConfiguration() || isValidOrchestrationConfiguration();
}

private boolean isValidRuleConfiguration() {
return (shardingProperties.getTables().isEmpty() && !Strings.isNullOrEmpty(masterSlaveProperties.getMasterDataSourceName()))
|| (!shardingProperties.getTables().isEmpty() && Strings.isNullOrEmpty(masterSlaveProperties.getMasterDataSourceName()));
}

private boolean isValidOrchestrationConfiguration() {
return !Strings.isNullOrEmpty(orchestrationProperties.getName());
}

private OrchestrationType getOrchestrationType() {
if (isValidRuleConfiguration()) {
return getOrchestrationTypeByLocal();
}
return getOrchestrationTypeByRegistry();
}

private OrchestrationType getOrchestrationTypeByLocal() {
return shardingProperties.getTables().isEmpty() ? OrchestrationType.MASTER_SLAVE : OrchestrationType.SHARDING;
}

private OrchestrationType getOrchestrationTypeByRegistry() {
OrchestrationFacade orchestrationFacade = new OrchestrationFacade(orchestrationProperties.getOrchestrationConfiguration());
ShardingRuleConfiguration shardingRuleConfiguration = orchestrationFacade.getConfigService().loadShardingRuleConfiguration();
orchestrationFacade.close();
if (null != shardingRuleConfiguration && !shardingRuleConfiguration.getTableRuleConfigs().isEmpty()) {
return OrchestrationType.SHARDING;
}
return OrchestrationType.MASTER_SLAVE;
}

private DataSource createShardingDataSource() throws SQLException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ sharding.jdbc.config.masterslave.master-data-source-name=ds_master
sharding.jdbc.config.masterslave.slave-data-source-names=ds_slave_0,ds_slave_1

sharding.jdbc.config.orchestration.name=demo_spring_boot_ds_ms
sharding.jdbc.config.orchestration.type=master_slave
sharding.jdbc.config.orchestration.overwrite=true
sharding.jdbc.config.orchestration.zookeeper.namespace=orchestration-spring-boot-master-slave-test
sharding.jdbc.config.orchestration.zookeeper.server-lists=localhost:3181
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,8 @@ sharding.jdbc.config.sharding.props.sql.show=true
sharding.jdbc.config.sharding.props.executor.size=100

sharding.jdbc.config.orchestration.name=demo_spring_boot_ds_sharding
sharding.jdbc.config.orchestration.type=sharding
sharding.jdbc.config.orchestration.overwrite=true
sharding.jdbc.config.orchestration.zookeeper.namespace=orchestration-spring-boot-sharding-test
sharding.jdbc.config.orchestration.zookeeper.server-lists=localhost:3181

sharding.jdbc.config.sharding.config-map.key1=value1
sharding.jdbc.config.sharding.config-map.key1=value1
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ private BeanDefinition getOrchestrationConfiguration(final Element element) {
factory.addConstructorArgValue(element.getAttribute(ID_ATTRIBUTE));
factory.addConstructorArgReference(element.getAttribute(ShardingDataSourceBeanDefinitionParserTag.REG_REF_TAG));
factory.addConstructorArgValue(element.getAttribute(ShardingDataSourceBeanDefinitionParserTag.OVERWRITE_TAG));
factory.addConstructorArgValue(orchestrationType);
return factory.getBeanDefinition();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package io.shardingsphere.shardingjdbc.orchestration.internal.datasource;

import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.google.common.eventbus.Subscribe;
import io.shardingsphere.api.ConfigMapContext;
import io.shardingsphere.api.config.MasterSlaveRuleConfiguration;
Expand Down Expand Up @@ -55,7 +56,7 @@ public OrchestrationMasterSlaveDataSource(final OrchestrationConfiguration orche
super(new OrchestrationFacade(orchestrationConfig));
ConfigurationService configService = getOrchestrationFacade().getConfigService();
MasterSlaveRuleConfiguration masterSlaveRuleConfig = configService.loadMasterSlaveRuleConfiguration();
Preconditions.checkNotNull(masterSlaveRuleConfig, "Missing the master-slave rule configuration on register center");
Preconditions.checkState(null != masterSlaveRuleConfig && !Strings.isNullOrEmpty(masterSlaveRuleConfig.getMasterDataSourceName()), "No available master slave rule configuration to load.");
dataSource = new MasterSlaveDataSource(
configService.loadDataSourceMap(), masterSlaveRuleConfig, configService.loadMasterSlaveConfigMap(), configService.loadMasterSlaveProperties());
initOrchestrationFacade(dataSource);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public OrchestrationShardingDataSource(final OrchestrationConfiguration orchestr
super(new OrchestrationFacade(orchestrationConfig));
ConfigurationService configService = getOrchestrationFacade().getConfigService();
ShardingRuleConfiguration shardingRuleConfig = configService.loadShardingRuleConfiguration();
Preconditions.checkNotNull(shardingRuleConfig, "Missing the sharding rule configuration on register center");
Preconditions.checkState(null != shardingRuleConfig && !shardingRuleConfig.getTableRuleConfigs().isEmpty(), "Missing the sharding rule configuration on register center");
dataSource = new ShardingDataSource(configService.loadDataSourceMap(),
new ShardingRule(shardingRuleConfig, configService.loadDataSourceMap().keySet()), configService.loadShardingConfigMap(), configService.loadShardingProperties());
initOrchestrationFacade(dataSource);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
* Orchestration configuration.
*
* @author zhangliang
* @author panjuan
*/
@RequiredArgsConstructor
@Getter
Expand All @@ -35,6 +36,4 @@ public final class OrchestrationConfiguration {
private final RegistryCenterConfiguration regCenterConfig;

private final boolean overwrite;

private final OrchestrationType type;
}
Original file line number Diff line number Diff line change
Expand Up @@ -259,13 +259,11 @@ public Map<String, Map<String, DataSourceParameter>> loadProxyDataSources() {
*/
public ShardingRuleConfiguration loadShardingRuleConfiguration() {
try {
ShardingRuleConfiguration result = ShardingConfigurationConverter.shardingRuleConfigFromYaml(regCenter.getDirectly(configNode.getFullPath(ConfigurationNode.SHARDING_RULE_NODE_PATH)));
Preconditions.checkState(null != result && !result.getTableRuleConfigs().isEmpty(), "No available sharding rule configuration to load.");
return result;
return ShardingConfigurationConverter.shardingRuleConfigFromYaml(regCenter.getDirectly(configNode.getFullPath(ConfigurationNode.SHARDING_RULE_NODE_PATH)));
// CHECKSTYLE:OFF
} catch (final Exception ex) {
// CHECKSTYLE:ON
throw new ShardingConfigurationException("No available sharding rule configuration to load.");
return new ShardingRuleConfiguration();
}
}

Expand Down Expand Up @@ -297,14 +295,12 @@ public Properties loadShardingProperties() {
*/
public MasterSlaveRuleConfiguration loadMasterSlaveRuleConfiguration() {
try {
MasterSlaveRuleConfiguration result = MasterSlaveConfigurationConverter.masterSlaveRuleConfigFromYaml(
return MasterSlaveConfigurationConverter.masterSlaveRuleConfigFromYaml(
regCenter.getDirectly(configNode.getFullPath(ConfigurationNode.MASTER_SLAVE_RULE_NODE_PATH)));
Preconditions.checkState(null != result && !Strings.isNullOrEmpty(result.getMasterDataSourceName()), "No available master slave rule configuration to load.");
return result;
// CHECKSTYLE:OFF
} catch (final Exception ex) {
// CHECKSTYLE:ON
throw new ShardingConfigurationException("No available master slave rule configuration to load.");
return new MasterSlaveRuleConfiguration();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

package io.shardingsphere.orchestration.internal.state.datasource;

import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import io.shardingsphere.api.config.MasterSlaveRuleConfiguration;
import io.shardingsphere.api.config.ShardingRuleConfiguration;
import io.shardingsphere.core.rule.DataSourceParameter;
Expand Down Expand Up @@ -100,6 +102,7 @@ public Map<String, Map<String, DataSourceParameter>> getProxyAvailableDataSource
*/
public ShardingRuleConfiguration getAvailableShardingRuleConfiguration() {
ShardingRuleConfiguration result = configService.loadShardingRuleConfiguration();
Preconditions.checkState(null != result && !result.getTableRuleConfigs().isEmpty(), "Missing the sharding rule configuration on register center");
Collection<String> disabledDataSourceNames = getDisabledDataSourceNames();
for (String each : disabledDataSourceNames) {
for (MasterSlaveRuleConfiguration masterSlaveRuleConfig : result.getMasterSlaveRuleConfigs()) {
Expand All @@ -116,6 +119,7 @@ public ShardingRuleConfiguration getAvailableShardingRuleConfiguration() {
*/
public MasterSlaveRuleConfiguration getAvailableMasterSlaveRuleConfiguration() {
MasterSlaveRuleConfiguration result = configService.loadMasterSlaveRuleConfiguration();
Preconditions.checkState(null != result && !Strings.isNullOrEmpty(result.getMasterDataSourceName()), "No available master slave rule configuration to load.");
Collection<String> disabledDataSourceNames = getDisabledDataSourceNames();
for (String each : disabledDataSourceNames) {
result.getSlaveDataSourceNames().remove(each);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

import com.google.common.base.Preconditions;
import io.shardingsphere.orchestration.config.OrchestrationConfiguration;
import io.shardingsphere.orchestration.config.OrchestrationType;
import io.shardingsphere.orchestration.reg.etcd.EtcdConfiguration;
import io.shardingsphere.orchestration.reg.zookeeper.ZookeeperConfiguration;
import lombok.Getter;
Expand All @@ -29,6 +28,7 @@
* Orchestration configuration for yaml.
*
* @author caohao
* @author panjuan
*/
@Getter
@Setter
Expand All @@ -42,15 +42,13 @@ public class YamlOrchestrationConfiguration {

private boolean overwrite;

private OrchestrationType type;

/**
* Get orchestration master-slave rule configuration from yaml.
*
* @return orchestration master-slave rule configuration from yaml
*/
public OrchestrationConfiguration getOrchestrationConfiguration() {
Preconditions.checkState(null == etcd || null == zookeeper, "Can't config both zookeeper and etcd as registry center!");
return new OrchestrationConfiguration(getName(), null != etcd ? etcd : zookeeper, overwrite, type);
return new OrchestrationConfiguration(getName(), null != etcd ? etcd : zookeeper, overwrite);
}
}

0 comments on commit 53485b5

Please sign in to comment.