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 ShadowRule #33520

Merged
merged 4 commits into from
Nov 3, 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 @@ -31,9 +31,9 @@ public interface HintShadowAlgorithm<T extends Comparable<?>> extends ShadowAlgo
/**
* Is need shadow.
*
* @param relatedShadowTables related shadow tables
* @param shadowTableNames shadow table names
* @param hintShadowValue hint value of shadow
* @return is need shadow or not
*/
boolean isShadow(Collection<String> relatedShadowTables, PreciseHintShadowValue<T> hintShadowValue);
boolean isShadow(Collection<String> shadowTableNames, PreciseHintShadowValue<T> hintShadowValue);
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,12 @@ private Map<String, String> getTableAliasAndNameMappings(final Collection<Simple

@Override
public Map<String, String> find(final ShadowRule rule) {
Collection<String> relatedShadowTables = rule.getRelatedShadowTables(tableAliasAndNameMappings.values());
if (relatedShadowTables.isEmpty() && isMatchDefaultAlgorithm(rule)) {
Collection<String> shadowTables = rule.filterShadowTables(tableAliasAndNameMappings.values());
if (shadowTables.isEmpty() && isMatchDefaultAlgorithm(rule)) {
return rule.getAllShadowDataSourceMappings();
}
Map<String, String> result = findBySQLHints(rule, relatedShadowTables);
return result.isEmpty() ? findByShadowColumn(rule, relatedShadowTables) : result;
Map<String, String> result = findBySQLHints(rule, shadowTables);
return result.isEmpty() ? findByShadowColumn(rule, shadowTables) : result;
}

@SuppressWarnings("unchecked")
Expand All @@ -93,15 +93,15 @@ private Map<String, String> findBySQLHints(final ShadowRule rule, final Collecti
Map<String, String> result = new LinkedHashMap<>();
for (String each : relatedShadowTables) {
if (isContainsShadowInSQLHints(rule, each, new ShadowDetermineCondition(each, operationType))) {
result.putAll(rule.getRelatedShadowDataSourceMappings(each));
result.putAll(rule.getShadowDataSourceMappings(each));
return result;
}
}
return result;
}

private boolean isContainsShadowInSQLHints(final ShadowRule rule, final String tableName, final ShadowDetermineCondition shadowCondition) {
for (HintShadowAlgorithm<Comparable<?>> each : rule.getRelatedHintShadowAlgorithms(tableName)) {
for (HintShadowAlgorithm<Comparable<?>> each : rule.getHintShadowAlgorithms(tableName)) {
if (HintShadowAlgorithmDeterminer.isShadow(each, shadowCondition, rule, isShadow)) {
return true;
}
Expand All @@ -111,9 +111,9 @@ private boolean isContainsShadowInSQLHints(final ShadowRule rule, final String t

private Map<String, String> findByShadowColumn(final ShadowRule rule, final Collection<String> relatedShadowTables) {
for (String each : relatedShadowTables) {
Collection<String> relatedShadowColumnNames = rule.getRelatedShadowColumnNames(operationType, each);
if (!relatedShadowColumnNames.isEmpty() && isMatchAnyColumnShadowAlgorithms(rule, each, relatedShadowColumnNames)) {
return rule.getRelatedShadowDataSourceMappings(each);
Collection<String> shadowColumnNames = rule.getShadowColumnNames(operationType, each);
if (!shadowColumnNames.isEmpty() && isMatchAnyColumnShadowAlgorithms(rule, each, shadowColumnNames)) {
return rule.getShadowDataSourceMappings(each);
}
}
return Collections.emptyMap();
Expand All @@ -129,7 +129,7 @@ private boolean isMatchAnyColumnShadowAlgorithms(final ShadowRule rule, final St
}

private boolean isMatchAnyColumnShadowAlgorithms(final ShadowRule rule, final String shadowTable, final String shadowColumn) {
Collection<ColumnShadowAlgorithm<Comparable<?>>> columnShadowAlgorithms = rule.getRelatedColumnShadowAlgorithms(operationType, shadowTable, shadowColumn);
Collection<ColumnShadowAlgorithm<Comparable<?>>> columnShadowAlgorithms = rule.getShadowAlgorithms(operationType, shadowTable, shadowColumn);
if (columnShadowAlgorithms.isEmpty()) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,19 @@
/**
* Databases shadow rule.
*/
@Getter
public final class ShadowRule implements DatabaseRule {

@Getter
private final ShadowRuleConfiguration configuration;

private final Collection<String> hintShadowAlgorithmNames = new LinkedList<>();
private final Map<String, ShadowDataSourceRule> shadowDataSourceRules = new LinkedHashMap<>();

private final Map<String, ShadowDataSourceRule> shadowDataSourceMappings = new LinkedHashMap<>();
private final Map<String, ShadowTableRule> shadowTableRules = new LinkedHashMap<>();

@Getter
private final Map<String, ShadowAlgorithm> shadowAlgorithms = new LinkedHashMap<>();

private final Map<String, ShadowTableRule> shadowTableRules = new LinkedHashMap<>();
private final Collection<String> hintShadowAlgorithmNames = new LinkedList<>();

private final ShadowAlgorithm defaultShadowAlgorithm;

Expand All @@ -63,21 +64,21 @@ public final class ShadowRule implements DatabaseRule {

public ShadowRule(final ShadowRuleConfiguration ruleConfig) {
configuration = ruleConfig;
initShadowDataSourceMappings(ruleConfig.getDataSources());
initShadowAlgorithmConfigurations(ruleConfig.getShadowAlgorithms());
initDataSourceRules(ruleConfig.getDataSources());
initShadowAlgorithms(ruleConfig.getShadowAlgorithms());
defaultShadowAlgorithm = shadowAlgorithms.get(ruleConfig.getDefaultShadowAlgorithmName());
if (defaultShadowAlgorithm instanceof HintShadowAlgorithm<?>) {
hintShadowAlgorithmNames.add(ruleConfig.getDefaultShadowAlgorithmName());
}
initShadowTableRules(ruleConfig.getTables());
attributes = new RuleAttributes(new ShadowDataSourceMapperRuleAttribute(shadowDataSourceMappings));
initTableRules(ruleConfig.getTables());
attributes = new RuleAttributes(new ShadowDataSourceMapperRuleAttribute(shadowDataSourceRules));
}

private void initShadowDataSourceMappings(final Collection<ShadowDataSourceConfiguration> dataSources) {
dataSources.forEach(each -> shadowDataSourceMappings.put(each.getName(), new ShadowDataSourceRule(each.getProductionDataSourceName(), each.getShadowDataSourceName())));
private void initDataSourceRules(final Collection<ShadowDataSourceConfiguration> dataSources) {
dataSources.forEach(each -> shadowDataSourceRules.put(each.getName(), new ShadowDataSourceRule(each.getProductionDataSourceName(), each.getShadowDataSourceName())));
}

private void initShadowAlgorithmConfigurations(final Map<String, AlgorithmConfiguration> shadowAlgorithmConfigs) {
private void initShadowAlgorithms(final Map<String, AlgorithmConfiguration> shadowAlgorithmConfigs) {
shadowAlgorithmConfigs.forEach((key, value) -> {
ShadowAlgorithm algorithm = TypedSPILoader.getService(ShadowAlgorithm.class, value.getType(), value.getProps());
if (algorithm instanceof HintShadowAlgorithm<?>) {
Expand All @@ -87,7 +88,7 @@ private void initShadowAlgorithmConfigurations(final Map<String, AlgorithmConfig
});
}

private void initShadowTableRules(final Map<String, ShadowTableConfiguration> tables) {
private void initTableRules(final Map<String, ShadowTableConfiguration> tables) {
tables.forEach((key, value) -> shadowTableRules.put(key, new ShadowTableRule(key, value.getDataSourceNames(), value.getShadowAlgorithmNames(), shadowAlgorithms)));
}

Expand All @@ -102,13 +103,13 @@ public Optional<ShadowAlgorithm> getDefaultShadowAlgorithm() {
}

/**
* Get related shadow tables.
* Filter shadow tables.
*
* @param tableNames table names
* @return related shadow tables
* @param tableNames to be filtered table names
* @return filtered shadow tables
*/
@HighFrequencyInvocation
public Collection<String> getRelatedShadowTables(final Collection<String> tableNames) {
public Collection<String> filterShadowTables(final Collection<String> tableNames) {
Collection<String> result = new LinkedList<>();
for (String each : tableNames) {
if (shadowTableRules.containsKey(each)) {
Expand Down Expand Up @@ -144,14 +145,14 @@ public Collection<HintShadowAlgorithm<Comparable<?>>> getAllHintShadowAlgorithms
}

/**
* Get related hint shadow algorithms by table name.
* Get hint shadow algorithms by table name.
*
* @param tableName table name
* @return hint shadow algorithms
*/
@HighFrequencyInvocation
@SuppressWarnings("unchecked")
public Collection<HintShadowAlgorithm<Comparable<?>>> getRelatedHintShadowAlgorithms(final String tableName) {
public Collection<HintShadowAlgorithm<Comparable<?>>> getHintShadowAlgorithms(final String tableName) {
Collection<HintShadowAlgorithm<Comparable<?>>> result = new LinkedList<>();
Collection<String> hintShadowAlgorithmNames = shadowTableRules.get(tableName).getHintShadowAlgorithmNames();
for (String each : hintShadowAlgorithmNames) {
Expand All @@ -161,38 +162,36 @@ public Collection<HintShadowAlgorithm<Comparable<?>>> getRelatedHintShadowAlgori
}

/**
* Get related column shadow algorithms by table name.
* Get shadow algorithms.
*
* @param shadowOperationType shadow operation type
* @param tableName table name
* @param shadowColumn shadow column
* @return column shadow algorithms
* @param shadowColumnName shadow column name
* @return shadow algorithms
*/
@HighFrequencyInvocation
@SuppressWarnings("unchecked")
public Collection<ColumnShadowAlgorithm<Comparable<?>>> getRelatedColumnShadowAlgorithms(final ShadowOperationType shadowOperationType, final String tableName, final String shadowColumn) {
public Collection<ColumnShadowAlgorithm<Comparable<?>>> getShadowAlgorithms(final ShadowOperationType shadowOperationType, final String tableName, final String shadowColumnName) {
Collection<ColumnShadowAlgorithm<Comparable<?>>> result = new LinkedList<>();
Map<ShadowOperationType, Collection<ShadowAlgorithmNameRule>> columnShadowAlgorithmNames = shadowTableRules.get(tableName).getColumnShadowAlgorithmNames();
for (ShadowAlgorithmNameRule each : columnShadowAlgorithmNames.getOrDefault(shadowOperationType, Collections.emptyList())) {
if (shadowColumn.equals(each.getShadowColumnName())) {
for (ShadowAlgorithmNameRule each : shadowTableRules.get(tableName).getColumnShadowAlgorithmNames().getOrDefault(shadowOperationType, Collections.emptyList())) {
if (shadowColumnName.equals(each.getShadowColumnName())) {
result.add((ColumnShadowAlgorithm<Comparable<?>>) shadowAlgorithms.get(each.getShadowAlgorithmName()));
}
}
return result;
}

/**
* Get related shadow column names.
* Get shadow column names.
*
* @param shadowOperationType shadow operation type
* @param tableName table name
* @return related shadow column names
* @return got shadow column names
*/
@HighFrequencyInvocation
public Collection<String> getRelatedShadowColumnNames(final ShadowOperationType shadowOperationType, final String tableName) {
public Collection<String> getShadowColumnNames(final ShadowOperationType shadowOperationType, final String tableName) {
Collection<String> result = new LinkedList<>();
Map<ShadowOperationType, Collection<ShadowAlgorithmNameRule>> columnShadowAlgorithmNames = shadowTableRules.get(tableName).getColumnShadowAlgorithmNames();
for (ShadowAlgorithmNameRule each : columnShadowAlgorithmNames.getOrDefault(shadowOperationType, Collections.emptyList())) {
for (ShadowAlgorithmNameRule each : shadowTableRules.get(tableName).getColumnShadowAlgorithmNames().getOrDefault(shadowOperationType, Collections.emptyList())) {
result.add(each.getShadowColumnName());
}
return result;
Expand All @@ -202,13 +201,13 @@ public Collection<String> getRelatedShadowColumnNames(final ShadowOperationType
* Get shadow data source mappings.
*
* @param tableName table name
* @return shadow data source rules
* @return shadow data source mappings
*/
@HighFrequencyInvocation
public Map<String, String> getRelatedShadowDataSourceMappings(final String tableName) {
Map<String, String> result = new LinkedHashMap<>();
public Map<String, String> getShadowDataSourceMappings(final String tableName) {
Map<String, String> result = new LinkedHashMap<>(shadowDataSourceRules.size(), 1F);
for (String each : shadowTableRules.get(tableName).getShadowDataSources()) {
ShadowDataSourceRule dataSourceRule = shadowDataSourceMappings.get(each);
ShadowDataSourceRule dataSourceRule = shadowDataSourceRules.get(each);
result.put(dataSourceRule.getProductionDataSource(), dataSourceRule.getShadowDataSource());
}
return result;
Expand All @@ -221,23 +220,23 @@ public Map<String, String> getRelatedShadowDataSourceMappings(final String table
*/
@HighFrequencyInvocation
public Map<String, String> getAllShadowDataSourceMappings() {
Map<String, String> result = new LinkedHashMap<>();
for (Entry<String, ShadowDataSourceRule> entry : shadowDataSourceMappings.entrySet()) {
ShadowDataSourceRule rule = entry.getValue();
result.put(rule.getProductionDataSource(), rule.getShadowDataSource());
Map<String, String> result = new LinkedHashMap<>(shadowDataSourceRules.size(), 1F);
for (Entry<String, ShadowDataSourceRule> entry : shadowDataSourceRules.entrySet()) {
ShadowDataSourceRule dataSourceRule = entry.getValue();
result.put(dataSourceRule.getProductionDataSource(), dataSourceRule.getShadowDataSource());
}
return result;
}

/**
* Find production data source name.
*
* @param actualDataSourceName actual data source name
* @param logicDataSourceName logic data source name
* @return found production data source name
*/
@HighFrequencyInvocation
public Optional<String> findProductionDataSourceName(final String actualDataSourceName) {
ShadowDataSourceRule dataSourceRule = shadowDataSourceMappings.get(actualDataSourceName);
public Optional<String> findProductionDataSourceName(final String logicDataSourceName) {
ShadowDataSourceRule dataSourceRule = shadowDataSourceRules.get(logicDataSourceName);
return null == dataSourceRule ? Optional.empty() : Optional.of(dataSourceRule.getProductionDataSource());
}
}
Loading