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 ShardingAuditorChangedProcessor #33489

Merged
merged 2 commits into from
Oct 31, 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 @@ -18,42 +18,30 @@
package org.apache.shardingsphere.sharding.rule.changed;

import org.apache.shardingsphere.infra.algorithm.core.config.AlgorithmConfiguration;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.mode.event.dispatch.rule.alter.AlterNamedRuleItemEvent;
import org.apache.shardingsphere.mode.event.dispatch.rule.alter.AlterRuleItemEvent;
import org.apache.shardingsphere.mode.event.dispatch.rule.drop.DropNamedRuleItemEvent;
import org.apache.shardingsphere.mode.event.dispatch.rule.drop.DropRuleItemEvent;
import org.apache.shardingsphere.infra.util.yaml.YamlEngine;
import org.apache.shardingsphere.infra.algorithm.core.yaml.YamlAlgorithmConfiguration;
import org.apache.shardingsphere.infra.algorithm.core.yaml.YamlAlgorithmConfigurationSwapper;
import org.apache.shardingsphere.mode.spi.RuleItemConfigurationChangedProcessor;
import org.apache.shardingsphere.mode.processor.AlgorithmChangedProcessor;
import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration;
import org.apache.shardingsphere.sharding.metadata.nodepath.ShardingRuleNodePathProvider;
import org.apache.shardingsphere.sharding.rule.ShardingRule;

import java.util.Map;

/**
* Sharding auditor changed processor.
*/
public final class ShardingAuditorChangedProcessor implements RuleItemConfigurationChangedProcessor<ShardingRuleConfiguration, AlgorithmConfiguration> {
public final class ShardingAuditorChangedProcessor extends AlgorithmChangedProcessor<ShardingRuleConfiguration> {

@Override
public AlgorithmConfiguration swapRuleItemConfiguration(final AlterRuleItemEvent event, final String yamlContent) {
return new YamlAlgorithmConfigurationSwapper().swapToObject(YamlEngine.unmarshal(yamlContent, YamlAlgorithmConfiguration.class));
}

@Override
public ShardingRuleConfiguration findRuleConfiguration(final ShardingSphereDatabase database) {
return database.getRuleMetaData().findSingleRule(ShardingRule.class).map(ShardingRule::getConfiguration).orElseGet(ShardingRuleConfiguration::new);
public ShardingAuditorChangedProcessor() {
super(ShardingRule.class);
}

@Override
public void changeRuleItemConfiguration(final AlterRuleItemEvent event, final ShardingRuleConfiguration currentRuleConfig, final AlgorithmConfiguration toBeChangedItemConfig) {
currentRuleConfig.getAuditors().put(((AlterNamedRuleItemEvent) event).getItemName(), toBeChangedItemConfig);
protected ShardingRuleConfiguration createEmptyRuleConfiguration() {
return new ShardingRuleConfiguration();
}

@Override
public void dropRuleItemConfiguration(final DropRuleItemEvent event, final ShardingRuleConfiguration currentRuleConfig) {
currentRuleConfig.getAuditors().remove(((DropNamedRuleItemEvent) event).getItemName());
protected Map<String, AlgorithmConfiguration> getAlgorithmConfigurations(final ShardingRuleConfiguration currentRuleConfig) {
return currentRuleConfig.getAuditors();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,16 @@
package org.apache.shardingsphere.sharding.rule.changed;

import org.apache.shardingsphere.infra.algorithm.core.config.AlgorithmConfiguration;
import org.apache.shardingsphere.infra.algorithm.core.yaml.YamlAlgorithmConfiguration;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData;
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
import org.apache.shardingsphere.infra.util.yaml.YamlEngine;
import org.apache.shardingsphere.mode.event.dispatch.rule.alter.AlterNamedRuleItemEvent;
import org.apache.shardingsphere.mode.event.dispatch.rule.drop.DropNamedRuleItemEvent;
import org.apache.shardingsphere.mode.spi.RuleItemConfigurationChangedProcessor;
import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration;
import org.apache.shardingsphere.sharding.rule.ShardingRule;
import org.junit.jupiter.api.Test;

import java.util.Collections;
import java.util.HashMap;
import java.util.Properties;

import static org.apache.shardingsphere.test.matcher.ShardingSphereAssertionMatchers.deepEqual;
Expand All @@ -48,47 +44,38 @@ class ShardingAuditorChangedProcessorTest {
RuleItemConfigurationChangedProcessor.class, "sharding.auditors");

@Test
void assertSwapRuleItemConfiguration() {
AlgorithmConfiguration actual = processor.swapRuleItemConfiguration(new AlterNamedRuleItemEvent("", "foo_tbl", "", "", ""), createYAMLContent());
assertThat(actual, deepEqual(new AlgorithmConfiguration("foo_algo", new Properties())));
void assertFindRuleConfigurationWhenAbsent() {
assertThat(processor.findRuleConfiguration(mockDatabase()), deepEqual(new ShardingRuleConfiguration()));
}

private String createYAMLContent() {
YamlAlgorithmConfiguration yamlConfig = new YamlAlgorithmConfiguration();
yamlConfig.setType("foo_algo");
return YamlEngine.marshal(yamlConfig);
}

@Test
void assertFindRuleConfiguration() {
ShardingRuleConfiguration ruleConfig = mock(ShardingRuleConfiguration.class);
assertThat(processor.findRuleConfiguration(mockDatabase(ruleConfig)), is(ruleConfig));
}

private ShardingSphereDatabase mockDatabase(final ShardingRuleConfiguration ruleConfig) {
ShardingRule rule = mock(ShardingRule.class);
when(rule.getConfiguration()).thenReturn(ruleConfig);
private ShardingSphereDatabase mockDatabase() {
ShardingSphereDatabase result = mock(ShardingSphereDatabase.class);
when(result.getRuleMetaData()).thenReturn(new RuleMetaData(Collections.singleton(rule)));
when(result.getRuleMetaData()).thenReturn(new RuleMetaData(Collections.emptyList()));
return result;
}

@Test
void assertChangeRuleItemConfiguration() {
ShardingRuleConfiguration currentRuleConfig = new ShardingRuleConfiguration();
currentRuleConfig.setAuditors(new HashMap<>(Collections.singletonMap("foo_algo", mock(AlgorithmConfiguration.class))));
AlgorithmConfiguration toBeChangedItemConfig = new AlgorithmConfiguration("FIXTURE", new Properties());
processor.changeRuleItemConfiguration(
new AlterNamedRuleItemEvent("foo_db", "foo_algo", "", "", ""), currentRuleConfig, toBeChangedItemConfig);
assertThat(currentRuleConfig.getAuditors().size(), is(1));
assertThat(currentRuleConfig.getAuditors().get("foo_algo").getType(), is("FIXTURE"));
AlterNamedRuleItemEvent event = new AlterNamedRuleItemEvent("", "bar_algo", "", "", "");
ShardingRuleConfiguration currentRuleConfig = createCurrentRuleConfiguration();
AlgorithmConfiguration toBeChangedItemConfig = new AlgorithmConfiguration("BAR_FIXTURE", new Properties());
processor.changeRuleItemConfiguration(event, currentRuleConfig, toBeChangedItemConfig);
assertThat(currentRuleConfig.getAuditors().size(), is(2));
assertThat(currentRuleConfig.getAuditors().get("foo_algo").getType(), is("FOO_FIXTURE"));
assertThat(currentRuleConfig.getAuditors().get("bar_algo").getType(), is("BAR_FIXTURE"));
}

@Test
void assertDropRuleItemConfiguration() {
ShardingRuleConfiguration currentRuleConfig = new ShardingRuleConfiguration();
currentRuleConfig.setAuditors(new HashMap<>(Collections.singletonMap("foo_algo", mock(AlgorithmConfiguration.class))));
processor.dropRuleItemConfiguration(new DropNamedRuleItemEvent("", "foo_algo", ""), currentRuleConfig);
DropNamedRuleItemEvent event = new DropNamedRuleItemEvent("", "foo_algo", "");
ShardingRuleConfiguration currentRuleConfig = createCurrentRuleConfiguration();
processor.dropRuleItemConfiguration(event, currentRuleConfig);
assertTrue(currentRuleConfig.getAuditors().isEmpty());
}

private ShardingRuleConfiguration createCurrentRuleConfiguration() {
ShardingRuleConfiguration result = new ShardingRuleConfiguration();
result.getAuditors().put("foo_algo", new AlgorithmConfiguration("FOO_FIXTURE", new Properties()));
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ class AlgorithmChangedProcessorTest {

@Test
void assertSwapRuleItemConfiguration() {
AlgorithmConfiguration actual = processor.swapRuleItemConfiguration(new AlterNamedRuleItemEvent("", "foo", "", "", ""), createYAMLContent());
AlterNamedRuleItemEvent event = new AlterNamedRuleItemEvent("", "foo", "", "", "");
AlgorithmConfiguration actual = processor.swapRuleItemConfiguration(event, createYAMLContent());
assertThat(actual, deepEqual(new AlgorithmConfiguration("foo_algo", new Properties())));
}

Expand Down