Skip to content

Commit

Permalink
Remove dispatch event builder for StandaloneMetaDataManagerPersistSer…
Browse files Browse the repository at this point in the history
…vice (#34098)
  • Loading branch information
menghaoranss authored Dec 18, 2024
1 parent c9b8f99 commit d7685eb
Show file tree
Hide file tree
Showing 59 changed files with 795 additions and 417 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
import org.apache.shardingsphere.broadcast.metadata.nodepath.BroadcastRuleNodePathProvider;
import org.apache.shardingsphere.broadcast.rule.BroadcastRule;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.mode.event.dispatch.rule.alter.AlterRuleItemEvent;
import org.apache.shardingsphere.mode.event.dispatch.rule.drop.DropRuleItemEvent;
import org.apache.shardingsphere.infra.util.yaml.YamlEngine;
import org.apache.shardingsphere.mode.spi.item.AlterRuleItem;
import org.apache.shardingsphere.mode.spi.item.DropRuleItem;
import org.apache.shardingsphere.mode.spi.RuleItemConfigurationChangedProcessor;

import java.util.LinkedHashSet;
Expand All @@ -36,7 +36,7 @@ public final class BroadcastTableChangedProcessor implements RuleItemConfigurati

@SuppressWarnings("unchecked")
@Override
public BroadcastRuleConfiguration swapRuleItemConfiguration(final AlterRuleItemEvent event, final String yamlContent) {
public BroadcastRuleConfiguration swapRuleItemConfiguration(final AlterRuleItem alterRuleItem, final String yamlContent) {
return new BroadcastRuleConfiguration(YamlEngine.unmarshal(yamlContent, LinkedHashSet.class));
}

Expand All @@ -46,13 +46,13 @@ public BroadcastRuleConfiguration findRuleConfiguration(final ShardingSphereData
}

@Override
public void changeRuleItemConfiguration(final AlterRuleItemEvent event, final BroadcastRuleConfiguration currentRuleConfig, final BroadcastRuleConfiguration toBeChangedItemConfig) {
public void changeRuleItemConfiguration(final AlterRuleItem alterRuleItem, final BroadcastRuleConfiguration currentRuleConfig, final BroadcastRuleConfiguration toBeChangedItemConfig) {
currentRuleConfig.getTables().clear();
currentRuleConfig.getTables().addAll(toBeChangedItemConfig.getTables());
}

@Override
public void dropRuleItemConfiguration(final DropRuleItemEvent event, final BroadcastRuleConfiguration currentRuleConfig) {
public void dropRuleItemConfiguration(final DropRuleItem dropRuleItem, final BroadcastRuleConfiguration currentRuleConfig) {
currentRuleConfig.getTables().clear();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
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.mode.event.dispatch.rule.alter.AlterNamedRuleItemEvent;
import org.apache.shardingsphere.mode.event.dispatch.rule.drop.DropRuleItemEvent;
import org.apache.shardingsphere.mode.spi.item.AlterNamedRuleItem;
import org.apache.shardingsphere.mode.spi.item.DropRuleItem;
import org.apache.shardingsphere.mode.spi.RuleItemConfigurationChangedProcessor;
import org.junit.jupiter.api.Test;

Expand All @@ -44,8 +44,8 @@ class BroadcastTableChangedProcessorTest {

@Test
void assertSwapRuleItemConfiguration() {
AlterNamedRuleItemEvent event = mock(AlterNamedRuleItemEvent.class);
BroadcastRuleConfiguration actual = processor.swapRuleItemConfiguration(event, "- foo_tbl");
AlterNamedRuleItem alterNamedRuleItem = mock(AlterNamedRuleItem.class);
BroadcastRuleConfiguration actual = processor.swapRuleItemConfiguration(alterNamedRuleItem, "- foo_tbl");
assertThat(actual.getTables(), is(Collections.singleton("foo_tbl")));
}

Expand All @@ -65,18 +65,18 @@ private ShardingSphereDatabase mockDatabase(final BroadcastRuleConfiguration rul

@Test
void assertChangeRuleItemConfiguration() {
AlterNamedRuleItemEvent event = mock(AlterNamedRuleItemEvent.class);
AlterNamedRuleItem alterNamedRuleItem = mock(AlterNamedRuleItem.class);
BroadcastRuleConfiguration currentRuleConfig = createCurrentRuleConfiguration();
BroadcastRuleConfiguration toBeChangedItemConfig = new BroadcastRuleConfiguration(new LinkedList<>(Collections.singleton("bar_tbl")));
processor.changeRuleItemConfiguration(event, currentRuleConfig, toBeChangedItemConfig);
processor.changeRuleItemConfiguration(alterNamedRuleItem, currentRuleConfig, toBeChangedItemConfig);
assertThat(currentRuleConfig.getTables(), is(Collections.singletonList("bar_tbl")));
}

@Test
void assertDropRuleItemConfiguration() {
DropRuleItemEvent event = mock(DropRuleItemEvent.class);
DropRuleItem dropRuleItem = mock(DropRuleItem.class);
BroadcastRuleConfiguration currentRuleConfig = createCurrentRuleConfiguration();
processor.dropRuleItemConfiguration(event, currentRuleConfig);
processor.dropRuleItemConfiguration(dropRuleItem, currentRuleConfig);
assertTrue(currentRuleConfig.getTables().isEmpty());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@
import org.apache.shardingsphere.encrypt.yaml.config.rule.YamlEncryptTableRuleConfiguration;
import org.apache.shardingsphere.encrypt.yaml.swapper.rule.YamlEncryptTableRuleConfigurationSwapper;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
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.mode.spi.item.AlterRuleItem;
import org.apache.shardingsphere.mode.spi.item.DropNamedRuleItem;
import org.apache.shardingsphere.mode.spi.item.DropRuleItem;
import org.apache.shardingsphere.mode.spi.RuleItemConfigurationChangedProcessor;

import java.util.LinkedHashMap;
Expand All @@ -39,7 +39,7 @@
public final class EncryptTableChangedProcessor implements RuleItemConfigurationChangedProcessor<EncryptRuleConfiguration, EncryptTableRuleConfiguration> {

@Override
public EncryptTableRuleConfiguration swapRuleItemConfiguration(final AlterRuleItemEvent event, final String yamlContent) {
public EncryptTableRuleConfiguration swapRuleItemConfiguration(final AlterRuleItem alterRuleItem, final String yamlContent) {
return new YamlEncryptTableRuleConfigurationSwapper().swapToObject(YamlEngine.unmarshal(yamlContent, YamlEncryptTableRuleConfiguration.class));
}

Expand All @@ -50,15 +50,15 @@ public EncryptRuleConfiguration findRuleConfiguration(final ShardingSphereDataba
}

@Override
public void changeRuleItemConfiguration(final AlterRuleItemEvent event, final EncryptRuleConfiguration currentRuleConfig, final EncryptTableRuleConfiguration toBeChangedItemConfig) {
public void changeRuleItemConfiguration(final AlterRuleItem alterRuleItem, final EncryptRuleConfiguration currentRuleConfig, final EncryptTableRuleConfiguration toBeChangedItemConfig) {
// TODO refactor DistSQL to only persist config
currentRuleConfig.getTables().removeIf(each -> each.getName().equals(toBeChangedItemConfig.getName()));
currentRuleConfig.getTables().add(toBeChangedItemConfig);
}

@Override
public void dropRuleItemConfiguration(final DropRuleItemEvent event, final EncryptRuleConfiguration currentRuleConfig) {
currentRuleConfig.getTables().removeIf(each -> each.getName().equals(((DropNamedRuleItemEvent) event).getItemName()));
public void dropRuleItemConfiguration(final DropRuleItem dropRuleItem, final EncryptRuleConfiguration currentRuleConfig) {
currentRuleConfig.getTables().removeIf(each -> each.getName().equals(((DropNamedRuleItem) dropRuleItem).getItemName()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@
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.mode.spi.item.AlterNamedRuleItem;
import org.apache.shardingsphere.mode.spi.item.DropNamedRuleItem;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
Expand All @@ -53,8 +53,8 @@ class EncryptTableChangedProcessorTest {

@Test
void assertSwapRuleItemConfiguration() {
AlterNamedRuleItemEvent event = mock(AlterNamedRuleItemEvent.class);
EncryptTableRuleConfiguration actual = processor.swapRuleItemConfiguration(event, createYAMLContent());
AlterNamedRuleItem alterNamedRuleItem = mock(AlterNamedRuleItem.class);
EncryptTableRuleConfiguration actual = processor.swapRuleItemConfiguration(alterNamedRuleItem, createYAMLContent());
assertThat(actual, deepEqual(new EncryptTableRuleConfiguration("foo_tbl",
Collections.singletonList(new EncryptColumnRuleConfiguration("foo_col", new EncryptColumnItemRuleConfiguration("foo_col_cipher", "foo_algo"))))));
}
Expand Down Expand Up @@ -88,11 +88,11 @@ private ShardingSphereDatabase mockDatabase(final EncryptRuleConfiguration ruleC

@Test
void assertChangeRuleItemConfiguration() {
AlterNamedRuleItemEvent event = mock(AlterNamedRuleItemEvent.class);
AlterNamedRuleItem alterNamedRuleItem = mock(AlterNamedRuleItem.class);
EncryptRuleConfiguration currentRuleConfig = createCurrentRuleConfiguration();
EncryptTableRuleConfiguration toBeChangedItemConfig = new EncryptTableRuleConfiguration("foo_tbl",
Collections.singleton(new EncryptColumnRuleConfiguration("foo_col", new EncryptColumnItemRuleConfiguration("bar_col_cipher", "bar_algo"))));
processor.changeRuleItemConfiguration(event, currentRuleConfig, toBeChangedItemConfig);
processor.changeRuleItemConfiguration(alterNamedRuleItem, currentRuleConfig, toBeChangedItemConfig);
assertThat(currentRuleConfig.getTables().size(), is(1));
assertThat(new ArrayList<>(currentRuleConfig.getTables()).get(0).getColumns().size(), is(1));
assertThat(new ArrayList<>(new ArrayList<>(currentRuleConfig.getTables()).get(0).getColumns()).get(0).getCipher().getName(), is("bar_col_cipher"));
Expand All @@ -101,10 +101,10 @@ void assertChangeRuleItemConfiguration() {

@Test
void assertDropRuleItemConfiguration() {
DropNamedRuleItemEvent event = mock(DropNamedRuleItemEvent.class);
when(event.getItemName()).thenReturn("foo_tbl");
DropNamedRuleItem dropNamedRuleItem = mock(DropNamedRuleItem.class);
when(dropNamedRuleItem.getItemName()).thenReturn("foo_tbl");
EncryptRuleConfiguration currentRuleConfig = createCurrentRuleConfiguration();
processor.dropRuleItemConfiguration(event, currentRuleConfig);
processor.dropRuleItemConfiguration(dropNamedRuleItem, currentRuleConfig);
assertTrue(currentRuleConfig.getTables().isEmpty());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
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.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.mode.spi.item.AlterNamedRuleItem;
import org.apache.shardingsphere.mode.spi.item.DropNamedRuleItem;
import org.junit.jupiter.api.Test;

import java.util.Collections;
Expand Down Expand Up @@ -59,22 +59,22 @@ private ShardingSphereDatabase mockDatabase() {

@Test
void assertChangeRuleItemConfiguration() {
AlterNamedRuleItemEvent event = mock(AlterNamedRuleItemEvent.class);
when(event.getItemName()).thenReturn("bar_algo");
AlterNamedRuleItem alterNamedRuleItem = mock(AlterNamedRuleItem.class);
when(alterNamedRuleItem.getItemName()).thenReturn("bar_algo");
EncryptRuleConfiguration currentRuleConfig = createCurrentRuleConfiguration();
AlgorithmConfiguration toBeChangedItemConfig = new AlgorithmConfiguration("BAR_FIXTURE", new Properties());
processor.changeRuleItemConfiguration(event, currentRuleConfig, toBeChangedItemConfig);
processor.changeRuleItemConfiguration(alterNamedRuleItem, currentRuleConfig, toBeChangedItemConfig);
assertThat(currentRuleConfig.getEncryptors().size(), is(2));
assertThat(currentRuleConfig.getEncryptors().get("foo_algo").getType(), is("FOO_FIXTURE"));
assertThat(currentRuleConfig.getEncryptors().get("bar_algo").getType(), is("BAR_FIXTURE"));
}

@Test
void assertDropRuleItemConfiguration() {
DropNamedRuleItemEvent event = mock(DropNamedRuleItemEvent.class);
when(event.getItemName()).thenReturn("foo_algo");
DropNamedRuleItem dropNamedRuleItem = mock(DropNamedRuleItem.class);
when(dropNamedRuleItem.getItemName()).thenReturn("foo_algo");
EncryptRuleConfiguration currentRuleConfig = createCurrentRuleConfiguration();
processor.dropRuleItemConfiguration(event, currentRuleConfig);
processor.dropRuleItemConfiguration(dropNamedRuleItem, currentRuleConfig);
assertTrue(currentRuleConfig.getEncryptors().isEmpty());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@
import org.apache.shardingsphere.mask.rule.MaskRule;
import org.apache.shardingsphere.mask.yaml.config.rule.YamlMaskTableRuleConfiguration;
import org.apache.shardingsphere.mask.yaml.swapper.rule.YamlMaskTableRuleConfigurationSwapper;
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.mode.spi.item.AlterRuleItem;
import org.apache.shardingsphere.mode.spi.item.DropNamedRuleItem;
import org.apache.shardingsphere.mode.spi.item.DropRuleItem;
import org.apache.shardingsphere.mode.spi.RuleItemConfigurationChangedProcessor;

import java.util.LinkedHashMap;
Expand All @@ -39,7 +39,7 @@
public final class MaskTableChangedProcessor implements RuleItemConfigurationChangedProcessor<MaskRuleConfiguration, MaskTableRuleConfiguration> {

@Override
public MaskTableRuleConfiguration swapRuleItemConfiguration(final AlterRuleItemEvent event, final String yamlContent) {
public MaskTableRuleConfiguration swapRuleItemConfiguration(final AlterRuleItem alterRuleItem, final String yamlContent) {
return new YamlMaskTableRuleConfigurationSwapper().swapToObject(YamlEngine.unmarshal(yamlContent, YamlMaskTableRuleConfiguration.class));
}

Expand All @@ -49,15 +49,15 @@ public MaskRuleConfiguration findRuleConfiguration(final ShardingSphereDatabase
}

@Override
public void changeRuleItemConfiguration(final AlterRuleItemEvent event, final MaskRuleConfiguration currentRuleConfig, final MaskTableRuleConfiguration toBeChangedItemConfig) {
public void changeRuleItemConfiguration(final AlterRuleItem alterRuleItem, final MaskRuleConfiguration currentRuleConfig, final MaskTableRuleConfiguration toBeChangedItemConfig) {
// TODO refactor DistSQL to only persist config
currentRuleConfig.getTables().removeIf(each -> each.getName().equals(toBeChangedItemConfig.getName()));
currentRuleConfig.getTables().add(toBeChangedItemConfig);
}

@Override
public void dropRuleItemConfiguration(final DropRuleItemEvent event, final MaskRuleConfiguration currentRuleConfig) {
currentRuleConfig.getTables().removeIf(each -> each.getName().equals(((DropNamedRuleItemEvent) event).getItemName()));
public void dropRuleItemConfiguration(final DropRuleItem dropRuleItem, final MaskRuleConfiguration currentRuleConfig) {
currentRuleConfig.getTables().removeIf(each -> each.getName().equals(((DropNamedRuleItem) dropRuleItem).getItemName()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData;
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
import org.apache.shardingsphere.mask.config.MaskRuleConfiguration;
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.mode.spi.item.AlterNamedRuleItem;
import org.apache.shardingsphere.mode.spi.item.DropNamedRuleItem;
import org.junit.jupiter.api.Test;

import java.util.Collections;
Expand Down Expand Up @@ -59,22 +59,22 @@ private ShardingSphereDatabase mockDatabase() {

@Test
void assertChangeRuleItemConfiguration() {
AlterNamedRuleItemEvent event = mock(AlterNamedRuleItemEvent.class);
when(event.getItemName()).thenReturn("bar_algo");
AlterNamedRuleItem alterNamedRuleItem = mock(AlterNamedRuleItem.class);
when(alterNamedRuleItem.getItemName()).thenReturn("bar_algo");
MaskRuleConfiguration currentRuleConfig = createCurrentRuleConfiguration();
AlgorithmConfiguration toBeChangedItemConfig = new AlgorithmConfiguration("BAR_FIXTURE", new Properties());
processor.changeRuleItemConfiguration(event, currentRuleConfig, toBeChangedItemConfig);
processor.changeRuleItemConfiguration(alterNamedRuleItem, currentRuleConfig, toBeChangedItemConfig);
assertThat(currentRuleConfig.getMaskAlgorithms().size(), is(2));
assertThat(currentRuleConfig.getMaskAlgorithms().get("foo_algo").getType(), is("FOO_FIXTURE"));
assertThat(currentRuleConfig.getMaskAlgorithms().get("bar_algo").getType(), is("BAR_FIXTURE"));
}

@Test
void assertDropRuleItemConfiguration() {
DropNamedRuleItemEvent event = mock(DropNamedRuleItemEvent.class);
when(event.getItemName()).thenReturn("foo_algo");
DropNamedRuleItem dropNamedRuleItem = mock(DropNamedRuleItem.class);
when(dropNamedRuleItem.getItemName()).thenReturn("foo_algo");
MaskRuleConfiguration currentRuleConfig = createCurrentRuleConfiguration();
processor.dropRuleItemConfiguration(event, currentRuleConfig);
processor.dropRuleItemConfiguration(dropNamedRuleItem, currentRuleConfig);
assertTrue(currentRuleConfig.getMaskAlgorithms().isEmpty());
}

Expand Down
Loading

0 comments on commit d7685eb

Please sign in to comment.