Skip to content

Commit

Permalink
Merge pull request #1343 from tristaZero/dev
Browse files Browse the repository at this point in the history
Fix the bug of getting NULL Pointer exception when disable datasource from registry.
  • Loading branch information
terrymanu authored Oct 15, 2018
2 parents 713b326 + 25764e3 commit 3086266
Show file tree
Hide file tree
Showing 12 changed files with 198 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
* @author panjuan
*/
@Getter
public final class MasterSlaveRule {
public class MasterSlaveRule {

private final String name;

Expand All @@ -42,6 +42,8 @@ public final class MasterSlaveRule {

private final MasterSlaveLoadBalanceAlgorithm loadBalanceAlgorithm;

private final MasterSlaveRuleConfiguration masterSlaveRuleConfiguration;

public MasterSlaveRule(final MasterSlaveRuleConfiguration config) {
Preconditions.checkNotNull(config.getName(), "Master-slave rule name cannot be null.");
Preconditions.checkNotNull(config.getMasterDataSourceName(), "Master data source name cannot be null.");
Expand All @@ -51,6 +53,7 @@ public MasterSlaveRule(final MasterSlaveRuleConfiguration config) {
masterDataSourceName = config.getMasterDataSourceName();
slaveDataSourceNames = config.getSlaveDataSourceNames();
loadBalanceAlgorithm = null == config.getLoadBalanceAlgorithm() ? MasterSlaveLoadBalanceAlgorithmType.getDefaultAlgorithmType().getAlgorithm() : config.getLoadBalanceAlgorithm();
masterSlaveRuleConfiguration = config;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
* @author panjuan
*/
@Getter
public final class ShardingRule {
public class ShardingRule {

private final ShardingRuleConfiguration shardingRuleConfig;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ private Map<String, DataSource> getDataSourceMap(final String shardingDataSource
private ShardingRule getShardingRule(final String shardingDataSourceName) {
OrchestrationSpringShardingDataSource shardingDataSource = applicationContext.getBean(shardingDataSourceName, OrchestrationSpringShardingDataSource.class);
ShardingDataSource dataSource = (ShardingDataSource) FieldValueUtil.getFieldValue(shardingDataSource, "dataSource", true);
Object shardingContext = FieldValueUtil.getFieldValue(dataSource, "shardingContext", true);
Object shardingContext = FieldValueUtil.getFieldValue(dataSource, "shardingContext", false);
return (ShardingRule) FieldValueUtil.getFieldValue(shardingContext, "shardingRule");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,9 @@ public void assertPropsDataSource() {
Map<String, Object> configMap = new HashMap<>();
configMap.put("key1", "value1");
assertThat(ConfigMapContext.getInstance().getShardingConfig(), is(configMap));
Object shardingContext = FieldValueUtil.getFieldValue(dataSource, "shardingContext", true);
Object shardingContext = FieldValueUtil.getFieldValue(dataSource, "shardingContext", false);
assertTrue((boolean) FieldValueUtil.getFieldValue(shardingContext, "showSQL"));
ShardingProperties shardingProperties = (ShardingProperties) FieldValueUtil.getFieldValue(dataSource, "shardingProperties", true);
ShardingProperties shardingProperties = (ShardingProperties) FieldValueUtil.getFieldValue(dataSource, "shardingProperties", false);
boolean showSql = shardingProperties.getValue(ShardingPropertiesConstant.SQL_SHOW);
assertTrue(showSql);
int executorSize = shardingProperties.getValue(ShardingPropertiesConstant.EXECUTOR_SIZE);
Expand All @@ -159,7 +159,7 @@ public void assertShardingDataSourceType() {
public void assertDefaultActualDataNodes() {
OrchestrationSpringShardingDataSource multiTableRulesDataSource = applicationContext.getBean("multiTableRulesDataSourceOrchestration", OrchestrationSpringShardingDataSource.class);
ShardingDataSource dataSource = (ShardingDataSource) FieldValueUtil.getFieldValue(multiTableRulesDataSource, "dataSource", true);
Object shardingContext = FieldValueUtil.getFieldValue(dataSource, "shardingContext", true);
Object shardingContext = FieldValueUtil.getFieldValue(dataSource, "shardingContext", false);
ShardingRule shardingRule = (ShardingRule) FieldValueUtil.getFieldValue(shardingContext, "shardingRule");
assertThat(shardingRule.getTableRules().size(), is(2));
Iterator<TableRule> tableRules = shardingRule.getTableRules().iterator();
Expand All @@ -183,7 +183,7 @@ private Map<String, DataSource> getDataSourceMap(final String shardingDataSource
private ShardingRule getShardingRule(final String shardingDataSourceName) {
OrchestrationSpringShardingDataSource shardingDataSource = applicationContext.getBean(shardingDataSourceName, OrchestrationSpringShardingDataSource.class);
ShardingDataSource dataSource = (ShardingDataSource) FieldValueUtil.getFieldValue(shardingDataSource, "dataSource", true);
Object shardingContext = FieldValueUtil.getFieldValue(dataSource, "shardingContext", true);
Object shardingContext = FieldValueUtil.getFieldValue(dataSource, "shardingContext", false);
return (ShardingRule) FieldValueUtil.getFieldValue(shardingContext, "shardingRule");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@

import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;

/**
Expand Down Expand Up @@ -59,14 +57,6 @@ public AbstractOrchestrationDataSource(final OrchestrationFacade orchestrationFa
ShardingEventBusInstance.getInstance().register(this);
}

protected final Map<String, DataSource> getAvailableDataSourceMap(final Collection<String> disabledDataSourceNames) {
Map<String, DataSource> result = new LinkedHashMap<>(dataSourceMap);
for (String each : disabledDataSourceNames) {
result.remove(each);
}
return result;
}

/**
/**
* Renew circuit breaker dataSource names.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,18 @@
import com.google.common.eventbus.Subscribe;
import io.shardingsphere.api.ConfigMapContext;
import io.shardingsphere.api.config.MasterSlaveRuleConfiguration;
import io.shardingsphere.core.constant.properties.ShardingProperties;
import io.shardingsphere.core.rule.MasterSlaveRule;
import io.shardingsphere.orchestration.config.OrchestrationConfiguration;
import io.shardingsphere.orchestration.internal.OrchestrationFacade;
import io.shardingsphere.orchestration.internal.config.ConfigurationService;
import io.shardingsphere.orchestration.internal.event.config.MasterSlaveConfigurationEventBusEvent;
import io.shardingsphere.orchestration.internal.event.state.DisabledStateEventBusEvent;
import io.shardingsphere.shardingjdbc.jdbc.core.datasource.MasterSlaveDataSource;
import io.shardingsphere.shardingjdbc.orchestration.internal.circuit.datasource.CircuitBreakerDataSource;
import io.shardingsphere.orchestration.internal.rule.OrchestrationMasterSlaveRule;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.LinkedHashMap;
import java.util.Map;

/**
* Orchestration master-slave datasource.
Expand All @@ -48,7 +46,8 @@ public class OrchestrationMasterSlaveDataSource extends AbstractOrchestrationDat

public OrchestrationMasterSlaveDataSource(final MasterSlaveDataSource masterSlaveDataSource, final OrchestrationConfiguration orchestrationConfig) throws SQLException {
super(new OrchestrationFacade(orchestrationConfig), masterSlaveDataSource.getDataSourceMap());
this.dataSource = masterSlaveDataSource;
dataSource = new MasterSlaveDataSource(masterSlaveDataSource.getDataSourceMap(), new OrchestrationMasterSlaveRule(masterSlaveDataSource.getMasterSlaveRule().getMasterSlaveRuleConfiguration()),
ConfigMapContext.getInstance().getMasterSlaveConfig(), masterSlaveDataSource.getShardingProperties());
initOrchestrationFacade(dataSource);
}

Expand All @@ -57,8 +56,8 @@ public OrchestrationMasterSlaveDataSource(final OrchestrationConfiguration orche
ConfigurationService configService = getOrchestrationFacade().getConfigService();
MasterSlaveRuleConfiguration masterSlaveRuleConfig = configService.loadMasterSlaveRuleConfiguration();
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());
dataSource = new MasterSlaveDataSource(configService.loadDataSourceMap(), new OrchestrationMasterSlaveRule(masterSlaveRuleConfig), configService.loadMasterSlaveConfigMap(),
new ShardingProperties(configService.loadMasterSlaveProperties()));
initOrchestrationFacade(dataSource);
}

Expand Down Expand Up @@ -96,16 +95,4 @@ public void renew(final MasterSlaveConfigurationEventBusEvent masterSlaveEvent)
dataSource = new MasterSlaveDataSource(
masterSlaveEvent.getDataSourceMap(), masterSlaveEvent.getMasterSlaveRuleConfig(), ConfigMapContext.getInstance().getMasterSlaveConfig(), masterSlaveEvent.getProps());
}

/**
* Renew disable dataSource names.
*
* @param disabledStateEventBusEvent jdbc disabled event bus event
* @throws SQLException sql exception
*/
@Subscribe
public void renew(final DisabledStateEventBusEvent disabledStateEventBusEvent) throws SQLException {
Map<String, DataSource> newDataSourceMap = getAvailableDataSourceMap(disabledStateEventBusEvent.getDisabledDataSourceNames());
dataSource = new MasterSlaveDataSource(newDataSourceMap, dataSource.getMasterSlaveRule(), new LinkedHashMap<String, Object>(), dataSource.getShardingProperties());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,17 @@
import com.google.common.eventbus.Subscribe;
import io.shardingsphere.api.ConfigMapContext;
import io.shardingsphere.api.config.ShardingRuleConfiguration;
import io.shardingsphere.core.rule.ShardingRule;
import io.shardingsphere.orchestration.config.OrchestrationConfiguration;
import io.shardingsphere.orchestration.internal.OrchestrationFacade;
import io.shardingsphere.orchestration.internal.config.ConfigurationService;
import io.shardingsphere.orchestration.internal.event.config.ShardingConfigurationEventBusEvent;
import io.shardingsphere.orchestration.internal.event.state.DisabledStateEventBusEvent;
import io.shardingsphere.shardingjdbc.jdbc.core.datasource.ShardingDataSource;
import io.shardingsphere.shardingjdbc.orchestration.internal.circuit.datasource.CircuitBreakerDataSource;
import io.shardingsphere.orchestration.internal.rule.OrchestrationShardingRule;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.LinkedHashMap;
import java.util.Map;

/**
* Orchestration sharding datasource.
Expand All @@ -47,7 +44,8 @@ public class OrchestrationShardingDataSource extends AbstractOrchestrationDataSo

public OrchestrationShardingDataSource(final ShardingDataSource shardingDataSource, final OrchestrationConfiguration orchestrationConfig) throws SQLException {
super(new OrchestrationFacade(orchestrationConfig), shardingDataSource.getDataSourceMap());
this.dataSource = shardingDataSource;
dataSource = new ShardingDataSource(shardingDataSource.getDataSourceMap(), new OrchestrationShardingRule(shardingDataSource.getShardingContext().getShardingRule().getShardingRuleConfig(),
shardingDataSource.getDataSourceMap().keySet()), ConfigMapContext.getInstance().getShardingConfig(), shardingDataSource.getShardingProperties().getProps());
initOrchestrationFacade(dataSource);
}

Expand All @@ -57,7 +55,7 @@ public OrchestrationShardingDataSource(final OrchestrationConfiguration orchestr
ShardingRuleConfiguration shardingRuleConfig = configService.loadShardingRuleConfiguration();
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());
new OrchestrationShardingRule(shardingRuleConfig, configService.loadDataSourceMap().keySet()), configService.loadShardingConfigMap(), configService.loadShardingProperties());
initOrchestrationFacade(dataSource);
}

Expand Down Expand Up @@ -90,17 +88,4 @@ public final void close() {
public void renew(final ShardingConfigurationEventBusEvent shardingEvent) throws SQLException {
dataSource = new ShardingDataSource(shardingEvent.getDataSourceMap(), shardingEvent.getShardingRule(), new LinkedHashMap<String, Object>(), shardingEvent.getProps());
}

/**
* Renew disable dataSource names.
*
* @param disabledStateEventBusEvent jdbc disabled event bus event
* @throws SQLException SQL exception
*/
@Subscribe
public void renew(final DisabledStateEventBusEvent disabledStateEventBusEvent) throws SQLException {
Map<String, DataSource> newDataSourceMap = getAvailableDataSourceMap(disabledStateEventBusEvent.getDisabledDataSourceNames());
dataSource = new ShardingDataSource(newDataSourceMap, dataSource.getShardingContext(), dataSource.getShardingProperties());
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright 2016-2018 shardingsphere.io.
* <p>
* Licensed 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.
* </p>
*/

package io.shardingsphere.orchestration.internal.rule;

import com.google.common.eventbus.Subscribe;
import io.shardingsphere.api.config.MasterSlaveRuleConfiguration;
import io.shardingsphere.core.rule.MasterSlaveRule;
import io.shardingsphere.orchestration.internal.event.state.DisabledStateEventBusEvent;

import java.util.Collection;
import java.util.LinkedList;

/**
* Orchestration master slave rule.
*
* @author panjuan
*/
public final class OrchestrationMasterSlaveRule extends MasterSlaveRule {

private final Collection<String> disabledDataSourceNames = new LinkedList<>();

public OrchestrationMasterSlaveRule(final MasterSlaveRuleConfiguration config) {
super(config);
}

/**
* Get slave data source names.
*
* @return available slave data source name
*/
@Override
public Collection<String> getSlaveDataSourceNames() {
Collection<String> result = new LinkedList<>(super.getSlaveDataSourceNames());
for (String each : disabledDataSourceNames) {
result.remove(each);
}
return result;
}

/**
* Renew disable dataSource names.
*
* @param disabledStateEventBusEvent jdbc disabled event bus event
*/
@Subscribe
public void renew(final DisabledStateEventBusEvent disabledStateEventBusEvent) {
disabledDataSourceNames.clear();
disabledDataSourceNames.addAll(disabledStateEventBusEvent.getDisabledDataSourceNames());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright 2016-2018 shardingsphere.io.
* <p>
* Licensed 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.
* </p>
*/

package io.shardingsphere.orchestration.internal.rule;

import com.google.common.base.Function;
import com.google.common.collect.Collections2;
import io.shardingsphere.api.config.MasterSlaveRuleConfiguration;
import io.shardingsphere.api.config.ShardingRuleConfiguration;
import io.shardingsphere.core.rule.MasterSlaveRule;
import io.shardingsphere.core.rule.ShardingRule;

import java.util.Collection;
import java.util.LinkedList;

/**
* Orchestration sharding rule.
*
* @author panjuan
*/
public final class OrchestrationShardingRule extends ShardingRule {

private final Collection<OrchestrationMasterSlaveRule> masterSlaveRules = new LinkedList<>();

public OrchestrationShardingRule(final ShardingRuleConfiguration shardingRuleConfig, final Collection<String> dataSourceNames) {
super(shardingRuleConfig, dataSourceNames);
initMasterSlaveRules(shardingRuleConfig);
}

private void initMasterSlaveRules(final ShardingRuleConfiguration shardingRuleConfig) {
for (MasterSlaveRuleConfiguration each : shardingRuleConfig.getMasterSlaveRuleConfigs()) {
masterSlaveRules.add(new OrchestrationMasterSlaveRule(each));
}
}

@Override
public Collection<MasterSlaveRule> getMasterSlaveRules() {
return Collections2.transform(masterSlaveRules, new Function<OrchestrationMasterSlaveRule, MasterSlaveRule>() {

@Override
public MasterSlaveRule apply(final OrchestrationMasterSlaveRule input) {
return input;
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ private static void startWithRegistryCenter(
orchestrationFacade.init(getYamlServerConfiguration(serverConfig), getSchemaDataSourceMap(ruleConfigs), getRuleConfiguration(ruleConfigs));
}
GlobalRegistry.getInstance().init(orchestrationFacade.getConfigService().loadYamlServerConfiguration(),
orchestrationFacade.getConfigService().loadProxyDataSources(), orchestrationFacade.getConfigService().loadProxyConfiguration());
orchestrationFacade.getConfigService().loadProxyDataSources(), orchestrationFacade.getConfigService().loadProxyConfiguration(), true);
initOpenTracing();
new ShardingProxy().start(port);
}
Expand Down
Loading

0 comments on commit 3086266

Please sign in to comment.