Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor ReadwriteSplittingRuleConfigurationChecker #30490

Merged
merged 3 commits into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.rule.scope.DatabaseRule;

import javax.sql.DataSource;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.stream.Collectors;

Expand All @@ -40,10 +42,9 @@ public void check(final ShardingSphereDatabase database, final RuleConfiguration

@Override
public DatabaseRule build(final ShardingSphereDatabase database, final RuleConfiguration ruleConfig, final InstanceContext instanceContext) {
return new BroadcastRule((BroadcastRuleConfiguration) ruleConfig, database.getName(),
database.getResourceMetaData().getStorageUnits().entrySet().stream()
.collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getDataSource(), (oldValue, currentValue) -> oldValue, LinkedHashMap::new)),
database.getRuleMetaData().getRules());
Map<String, DataSource> dataSources = database.getResourceMetaData().getStorageUnits().entrySet().stream()
.collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getDataSource(), (oldValue, currentValue) -> oldValue, LinkedHashMap::new));
return new BroadcastRule((BroadcastRuleConfiguration) ruleConfig, database.getName(), dataSources, database.getRuleMetaData().getRules());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,16 @@ public final class ReadwriteSplittingRuleConfigurationChecker implements RuleCon
public void check(final String databaseName, final ReadwriteSplittingRuleConfiguration config, final Map<String, DataSource> dataSourceMap, final Collection<ShardingSphereRule> builtRules) {
Collection<ReadwriteSplittingDataSourceRuleConfiguration> configs = config.getDataSources();
checkDataSources(databaseName, configs, dataSourceMap, builtRules);
checkLoadBalancerDataSourceName(databaseName, configs, getLoadBalancer(config));
checkLoadBalancer(databaseName, configs, getLoadBalancer(config));
}

private void checkDataSources(final String databaseName,
final Collection<ReadwriteSplittingDataSourceRuleConfiguration> configs, final Map<String, DataSource> dataSourceMap, final Collection<ShardingSphereRule> rules) {
private void checkDataSources(final String databaseName, final Collection<ReadwriteSplittingDataSourceRuleConfiguration> configs,
final Map<String, DataSource> dataSourceMap, final Collection<ShardingSphereRule> builtRules) {
Collection<String> addedWriteDataSourceNames = new HashSet<>();
Collection<String> addedReadDataSourceNames = new HashSet<>();
for (ReadwriteSplittingDataSourceRuleConfiguration each : configs) {
ShardingSpherePreconditions.checkState(!Strings.isNullOrEmpty(each.getName()), () -> new MissingRequiredDataSourceNameException(databaseName));
checkDataSources(databaseName, dataSourceMap, each, addedWriteDataSourceNames, addedReadDataSourceNames, rules);
checkDataSources(databaseName, dataSourceMap, each, addedWriteDataSourceNames, addedReadDataSourceNames, builtRules);
}
}

Expand Down Expand Up @@ -108,24 +108,26 @@ private void checkReadeDataSourceNames(final String databaseName, final Map<Stri
}
}

private void checkLoadBalancerDataSourceName(final String databaseName, final Collection<ReadwriteSplittingDataSourceRuleConfiguration> configs,
final Map<String, LoadBalanceAlgorithm> loadBalancers) {
private void checkLoadBalancer(final String databaseName, final Collection<ReadwriteSplittingDataSourceRuleConfiguration> configs, final Map<String, LoadBalanceAlgorithm> loadBalancers) {
for (ReadwriteSplittingDataSourceRuleConfiguration each : configs) {
if (Strings.isNullOrEmpty(each.getLoadBalancerName())) {
continue;
}
LoadBalanceAlgorithm loadBalancer = loadBalancers.get(each.getLoadBalancerName());
ShardingSpherePreconditions.checkNotNull(loadBalancer, () -> new LoadBalancerAlgorithmNotFoundException(databaseName));
if (loadBalancer instanceof WeightLoadBalanceAlgorithm) {
ShardingSpherePreconditions.checkState(!((WeightLoadBalanceAlgorithm) loadBalancer).getAvailableTargetNames().isEmpty(),
() -> new MissingRequiredReadDatabaseWeightException(loadBalancer.getType(), String.format("Read data source weight config are required in database `%s`", databaseName)));
Collection<String> dataSourceNames = each.getReadDataSourceNames();
((WeightLoadBalanceAlgorithm) loadBalancer).getAvailableTargetNames().forEach(dataSourceName -> ShardingSpherePreconditions.checkState(dataSourceNames.contains(dataSourceName),
() -> new InvalidWeightLoadBalancerConfigurationException(databaseName)));
checkWeightLoadBalancer(databaseName, each, (WeightLoadBalanceAlgorithm) loadBalancer);
}
}
}

private void checkWeightLoadBalancer(final String databaseName, final ReadwriteSplittingDataSourceRuleConfiguration sourceRuleConfig, final WeightLoadBalanceAlgorithm loadBalancer) {
ShardingSpherePreconditions.checkState(!loadBalancer.getAvailableTargetNames().isEmpty(),
() -> new MissingRequiredReadDatabaseWeightException(loadBalancer.getType(), String.format("Read data source weight config are required in database `%s`", databaseName)));
loadBalancer.getAvailableTargetNames().forEach(each -> ShardingSpherePreconditions.checkState(sourceRuleConfig.getReadDataSourceNames().contains(each),
() -> new InvalidWeightLoadBalancerConfigurationException(databaseName)));
}

private Map<String, LoadBalanceAlgorithm> getLoadBalancer(final ReadwriteSplittingRuleConfiguration config) {
Map<String, LoadBalanceAlgorithm> result = new LinkedHashMap<>(config.getLoadBalancers().size(), 1F);
config.getLoadBalancers().forEach((key, value) -> result.put(key, TypedSPILoader.getService(LoadBalanceAlgorithm.class, value.getType(), value.getProps())));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public interface LoadBalanceAlgorithm extends ShardingSphereAlgorithm {
/**
* Get available target name.
*
* @param groupName group name, which uniquely identifies a set of availableTargetNames
* @param groupName group name, which uniquely identifies available target names
* @param availableTargetNames available data sources or instance id
* @return available target name
*/
Expand Down