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 ShadowHintDataSourceMappingsFinder #33550

Merged
merged 3 commits into from
Nov 4, 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 @@ -34,6 +34,10 @@ public final class ShadowCondition {

private final ShadowColumnCondition columnCondition;

public ShadowCondition() {
this("", ShadowOperationType.HINT_MATCH, null);
}

public ShadowCondition(final String tableName, final ShadowOperationType operationType) {
this(tableName, operationType, null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import org.apache.shardingsphere.shadow.route.finder.dml.ShadowInsertStatementDataSourceMappingsFinder;
import org.apache.shardingsphere.shadow.route.finder.dml.ShadowSelectStatementDataSourceMappingsFinder;
import org.apache.shardingsphere.shadow.route.finder.dml.ShadowUpdateStatementDataSourceMappingsFinder;
import org.apache.shardingsphere.shadow.route.finder.other.ShadowHintDataSourceMappingsFinder;
import org.apache.shardingsphere.shadow.route.finder.hint.ShadowHintDataSourceMappingsFinder;

/**
* Shadow data source mappings finder factory.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,7 @@ public Map<String, String> find(final ShadowRule rule) {
private boolean isMatchDefaultAlgorithm(final ShadowRule rule) {
Optional<ShadowAlgorithm> defaultAlgorithm = rule.getDefaultShadowAlgorithm();
if (defaultAlgorithm.isPresent() && defaultAlgorithm.get() instanceof HintShadowAlgorithm<?>) {
ShadowCondition shadowCondition = new ShadowCondition("", ShadowOperationType.HINT_MATCH);
return HintShadowAlgorithmDeterminer.isShadow((HintShadowAlgorithm<Comparable<?>>) defaultAlgorithm.get(), shadowCondition, rule, isShadow);
return HintShadowAlgorithmDeterminer.isShadow((HintShadowAlgorithm<Comparable<?>>) defaultAlgorithm.get(), new ShadowCondition(), rule, isShadow);
}
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,14 @@
* limitations under the License.
*/

package org.apache.shardingsphere.shadow.route.finder.other;
package org.apache.shardingsphere.shadow.route.finder.hint;

import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.infra.hint.HintValueContext;
import org.apache.shardingsphere.shadow.condition.ShadowCondition;
import org.apache.shardingsphere.shadow.route.determiner.HintShadowAlgorithmDeterminer;
import org.apache.shardingsphere.shadow.route.finder.ShadowDataSourceMappingsFinder;
import org.apache.shardingsphere.shadow.rule.ShadowRule;
import org.apache.shardingsphere.shadow.spi.ShadowOperationType;

import java.util.Collections;
import java.util.Map;
Expand All @@ -38,12 +37,9 @@ public final class ShadowHintDataSourceMappingsFinder implements ShadowDataSourc

@Override
public Map<String, String> find(final ShadowRule rule) {
return hintValueContext.isShadow() && isMatchAnyHintShadowAlgorithms(rule, new ShadowCondition("", ShadowOperationType.HINT_MATCH))
ShadowCondition shadowCondition = new ShadowCondition();
return rule.getAllHintShadowAlgorithms().stream().anyMatch(each -> HintShadowAlgorithmDeterminer.isShadow(each, shadowCondition, rule, hintValueContext.isShadow()))
? rule.getAllShadowDataSourceMappings()
: Collections.emptyMap();
}

private boolean isMatchAnyHintShadowAlgorithms(final ShadowRule rule, final ShadowCondition shadowCondition) {
return rule.getAllHintShadowAlgorithms().stream().anyMatch(each -> HintShadowAlgorithmDeterminer.isShadow(each, shadowCondition, rule, hintValueContext.isShadow()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* limitations under the License.
*/

package org.apache.shardingsphere.shadow.route.finder.other;
package org.apache.shardingsphere.shadow.route.finder.hint;

import org.apache.shardingsphere.infra.algorithm.core.config.AlgorithmConfiguration;
import org.apache.shardingsphere.infra.hint.HintValueContext;
Expand All @@ -25,7 +25,6 @@
import org.apache.shardingsphere.shadow.rule.ShadowRule;
import org.apache.shardingsphere.test.util.PropertiesBuilder;
import org.apache.shardingsphere.test.util.PropertiesBuilder.Property;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.util.Collections;
Expand All @@ -36,29 +35,23 @@

class ShadowHintDataSourceMappingsFinderTest {

private ShadowHintDataSourceMappingsFinder finder;

@BeforeEach
void init() {
finder = new ShadowHintDataSourceMappingsFinder(createHintValueContext());
}

private HintValueContext createHintValueContext() {
HintValueContext result = new HintValueContext();
result.setShadow(true);
return result;
@Test
void assertFindWithShadowHint() {
HintValueContext hintValueContext = new HintValueContext();
hintValueContext.setShadow(true);
assertThat(new ShadowHintDataSourceMappingsFinder(hintValueContext).find(new ShadowRule(createRuleConfiguration())), is(Collections.singletonMap("prod_ds", "shadow_ds")));
}

@Test
void assertRoute() {
Map<String, String> shadowDataSourceMappings = finder.find(new ShadowRule(createShadowRuleConfiguration()));
assertThat(shadowDataSourceMappings, is(Collections.singletonMap("ds", "ds_shadow")));
void assertFindWithNotShadowHint() {
HintValueContext hintValueContext = new HintValueContext();
assertThat(new ShadowHintDataSourceMappingsFinder(hintValueContext).find(new ShadowRule(createRuleConfiguration())), is(Collections.emptyMap()));
}

private ShadowRuleConfiguration createShadowRuleConfiguration() {
private ShadowRuleConfiguration createRuleConfiguration() {
ShadowRuleConfiguration result = new ShadowRuleConfiguration();
result.setDataSources(Collections.singleton(new ShadowDataSourceConfiguration("shadow-data-source", "ds", "ds_shadow")));
result.setTables(Collections.singletonMap("t_order", new ShadowTableConfiguration(Collections.singleton("shadow-data-source"), Collections.singleton("sql-hint-algorithm"))));
result.setDataSources(Collections.singleton(new ShadowDataSourceConfiguration("foo_ds", "prod_ds", "shadow_ds")));
result.setTables(Collections.singletonMap("foo_tbl", new ShadowTableConfiguration(Collections.singleton("foo_ds"), Collections.singleton("sql-hint-algorithm"))));
result.setShadowAlgorithms(createShadowAlgorithms());
return result;
}
Expand Down