Skip to content

Commit

Permalink
Refactor GlobalRuleBuilder from database map to database collection (#…
Browse files Browse the repository at this point in the history
…33886)

* Refactor GlobalRuleBuilder from database map to database collection

* Refactor GlobalRuleBuilder from database map to database collection

* Refactor GlobalRuleBuilder from database map to database collection

* Refactor GlobalRuleBuilder from database map to database collection

* Refactor GlobalRuleBuilder from database map to database collection

* Refactor GlobalRuleBuilder from database map to database collection

* Refactor GlobalRuleBuilder from database map to database collection

* Refactor GlobalRuleBuilder from database map to database collection
  • Loading branch information
terrymanu authored Dec 3, 2024
1 parent 79352e8 commit 676abda
Show file tree
Hide file tree
Showing 37 changed files with 109 additions and 113 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public ShardingSphereDatabase getDatabase(final String databaseName) {
public void addDatabase(final String databaseName, final DatabaseType protocolType, final ConfigurationProperties props) {
ShardingSphereDatabase database = ShardingSphereDatabase.create(databaseName, protocolType, props);
databases.put(database.getName(), database);
globalRuleMetaData.getRules().forEach(each -> ((GlobalRule) each).refresh(databases, GlobalRuleChangedType.DATABASE_CHANGED));
globalRuleMetaData.getRules().forEach(each -> ((GlobalRule) each).refresh(databases.values(), GlobalRuleChangedType.DATABASE_CHANGED));
}

/**
Expand All @@ -122,7 +122,7 @@ public void dropDatabase(final String databaseName) {

@SneakyThrows(Exception.class)
private void cleanResources(final ShardingSphereDatabase database) {
globalRuleMetaData.getRules().forEach(each -> ((GlobalRule) each).refresh(databases, GlobalRuleChangedType.DATABASE_CHANGED));
globalRuleMetaData.getRules().forEach(each -> ((GlobalRule) each).refresh(databases.values(), GlobalRuleChangedType.DATABASE_CHANGED));
for (ShardingSphereRule each : database.getRuleMetaData().getRules()) {
if (each instanceof AutoCloseable) {
((AutoCloseable) each).close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import org.apache.shardingsphere.infra.rule.scope.GlobalRule;
import org.apache.shardingsphere.infra.spi.annotation.SingletonSPI;

import java.util.Map;
import java.util.Collection;

/**
* Global rule builder.
Expand All @@ -42,5 +42,5 @@ public interface GlobalRuleBuilder<T extends RuleConfiguration> extends RuleBuil
* @param props props
* @return global rule
*/
GlobalRule build(T ruleConfig, Map<String, ShardingSphereDatabase> databases, ConfigurationProperties props);
GlobalRule build(T ruleConfig, Collection<ShardingSphereDatabase> databases, ConfigurationProperties props);
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public static Collection<ShardingSphereRule> buildRules(final Collection<RuleCon
final Map<String, ShardingSphereDatabase> databases, final ConfigurationProperties props) {
Collection<ShardingSphereRule> result = new LinkedList<>();
for (Entry<RuleConfiguration, GlobalRuleBuilder> entry : getRuleBuilderMap(globalRuleConfigs).entrySet()) {
result.add(entry.getValue().build(entry.getKey(), databases, props));
result.add(entry.getValue().build(entry.getKey(), databases.values(), props));
}
return result;
}
Expand Down Expand Up @@ -93,6 +93,6 @@ private static Collection<GlobalRuleBuilder> getMissedDefaultRuleBuilders(final
@SuppressWarnings("unchecked")
public static Collection<ShardingSphereRule> buildSingleRules(final RuleConfiguration globalRuleConfig, final Map<String, ShardingSphereDatabase> databases, final ConfigurationProperties props) {
return OrderedSPILoader.getServices(GlobalRuleBuilder.class, Collections.singleton(globalRuleConfig)).entrySet()
.stream().map(each -> each.getValue().build(each.getKey(), databases, props)).collect(Collectors.toList());
.stream().map(each -> each.getValue().build(each.getKey(), databases.values(), props)).collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.rule.ShardingSphereRule;

import java.util.Map;
import java.util.Collection;

/**
* Global rule.
Expand All @@ -33,7 +33,7 @@ public interface GlobalRule extends ShardingSphereRule {
* @param databases changed databases
* @param changedType changed type
*/
default void refresh(final Map<String, ShardingSphereDatabase> databases, GlobalRuleChangedType changedType) {
default void refresh(final Collection<ShardingSphereDatabase> databases, GlobalRuleChangedType changedType) {
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,7 @@ void assertAddDatabase() {
Map<String, ShardingSphereDatabase> databases = new HashMap<>(Collections.singletonMap("foo_db", database));
ShardingSphereMetaData metaData = new ShardingSphereMetaData(databases, mock(ResourceMetaData.class), new RuleMetaData(Collections.singleton(globalRule)), configProps);
metaData.addDatabase("foo_db", databaseType, configProps);
assertThat(metaData.getDatabases(), is(databases));
verify(globalRule).refresh(databases, GlobalRuleChangedType.DATABASE_CHANGED);
assertThat(metaData.getDatabase("foo_db"), is(database));
}

@Test
Expand All @@ -91,7 +90,7 @@ void assertDropDatabase() {
assertTrue(metaData.getDatabases().isEmpty());
Awaitility.await().pollDelay(10L, TimeUnit.MILLISECONDS).until(dataSource::isClosed);
assertTrue(dataSource.isClosed());
verify(globalRule).refresh(metaData.getDatabases(), GlobalRuleChangedType.DATABASE_CHANGED);
verify(globalRule).refresh(metaData.getAllDatabases(), GlobalRuleChangedType.DATABASE_CHANGED);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.rule.builder.global.GlobalRuleBuilder;

import java.util.Map;
import java.util.Collection;

public final class FixtureGlobalRuleBuilder implements GlobalRuleBuilder<FixtureGlobalRuleConfiguration> {

@Override
public FixtureGlobalRule build(final FixtureGlobalRuleConfiguration ruleConfig, final Map<String, ShardingSphereDatabase> databases, final ConfigurationProperties props) {
public FixtureGlobalRule build(final FixtureGlobalRuleConfiguration ruleConfig, final Collection<ShardingSphereDatabase> databases, final ConfigurationProperties props) {
return new FixtureGlobalRule();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ void setUp() throws SQLException {
when(connection.getContextManager().getMetaDataContexts().getMetaData().getGlobalRuleMetaData()).thenReturn(
new RuleMetaData(Arrays.asList(
new SQLParserRule(new DefaultSQLParserRuleConfigurationBuilder().build()),
new SQLFederationRule(new DefaultSQLFederationRuleConfigurationBuilder().build(), Collections.emptyMap()))));
new SQLFederationRule(new DefaultSQLFederationRuleConfigurationBuilder().build(), Collections.emptyList()))));
when(connection.getContextManager().getMetaDataContexts().getMetaData().getProps()).thenReturn(new ConfigurationProperties(new Properties()));
when(connection.getContextManager().getMetaDataContexts().getMetaData().getDatabase(
connection.getCurrentDatabaseName()).getProtocolType()).thenReturn(TypedSPILoader.getService(DatabaseType.class, "MySQL"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ void assertSetQueryTimeout() throws SQLException {
private ShardingSphereStatement mockShardingSphereStatement(final Statement... statements) {
ShardingSphereConnection connection = mock(ShardingSphereConnection.class, RETURNS_DEEP_STUBS);
RuleMetaData globalRuleMetaData = new RuleMetaData(Arrays.asList(
new SQLFederationRule(new DefaultSQLFederationRuleConfigurationBuilder().build(), Collections.emptyMap()),
new SQLFederationRule(new DefaultSQLFederationRuleConfigurationBuilder().build(), Collections.emptyList()),
new SQLParserRule(new DefaultSQLParserRuleConfigurationBuilder().build())));
when(connection.getContextManager().getMetaDataContexts().getMetaData().getGlobalRuleMetaData()).thenReturn(globalRuleMetaData);
when(connection.getContextManager().getMetaDataContexts().getMetaData().getProps()).thenReturn(new ConfigurationProperties(new Properties()));
Expand All @@ -257,7 +257,7 @@ private ShardingSphereStatement mockShardingSphereStatementWithNeedAccumulate(fi
DatabaseType databaseType = TypedSPILoader.getService(DatabaseType.class, "FIXTURE");
when(connection.getContextManager().getMetaDataContexts().getMetaData().getDatabase("db").getProtocolType()).thenReturn(databaseType);
when(connection.getContextManager().getMetaDataContexts().getMetaData().getGlobalRuleMetaData()).thenReturn(new RuleMetaData(Arrays.asList(
new SQLFederationRule(new DefaultSQLFederationRuleConfigurationBuilder().build(), Collections.emptyMap()),
new SQLFederationRule(new DefaultSQLFederationRuleConfigurationBuilder().build(), Collections.emptyList()),
new SQLParserRule(new DefaultSQLParserRuleConfigurationBuilder().build()))));
when(connection.getContextManager().getMetaDataContexts().getMetaData().getProps()).thenReturn(new ConfigurationProperties(new Properties()));
ShardingSphereStatement result = new ShardingSphereStatement(connection, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,6 @@ private ContextManager mockContextManager(final DataSource dataSource) {
}

private TransactionRule mockTransactionRule() {
return new TransactionRule(new TransactionRuleConfiguration(TransactionType.LOCAL.name(), "", new Properties()), Collections.emptyMap());
return new TransactionRule(new TransactionRuleConfiguration(TransactionType.LOCAL.name(), "", new Properties()), Collections.emptyList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ void setUp() throws SQLException {
when(connection.getCurrentDatabaseName()).thenReturn(DefaultDatabase.LOGIC_NAME);
when(connection.getContextManager().getMetaDataContexts().getMetaData().getGlobalRuleMetaData()).thenReturn(new RuleMetaData(Arrays.asList(
new SQLParserRule(new DefaultSQLParserRuleConfigurationBuilder().build()),
new SQLFederationRule(new DefaultSQLFederationRuleConfigurationBuilder().build(), Collections.emptyMap()))));
new SQLFederationRule(new DefaultSQLFederationRuleConfigurationBuilder().build(), Collections.emptyList()))));
when(connection.getContextManager().getMetaDataContexts().getMetaData().getDatabase(
connection.getCurrentDatabaseName()).getProtocolType()).thenReturn(TypedSPILoader.getService(DatabaseType.class, "MySQL"));
when(connection.getContextManager().getMetaDataContexts().getMetaData().getProps()).thenReturn(new ConfigurationProperties(new Properties()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ void setUp() {
when(connection.getContextManager().getMetaDataContexts().getMetaData().getDatabase("db").getProtocolType()).thenReturn(databaseType);
when(connection.getContextManager().getMetaDataContexts().getMetaData().getGlobalRuleMetaData()).thenReturn(new RuleMetaData(
Arrays.asList(
new SQLFederationRule(new DefaultSQLFederationRuleConfigurationBuilder().build(), Collections.emptyMap()),
new SQLFederationRule(new DefaultSQLFederationRuleConfigurationBuilder().build(), Collections.emptyList()),
new SQLParserRule(new DefaultSQLParserRuleConfigurationBuilder().build()))));
when(connection.getContextManager().getMetaDataContexts().getMetaData().getProps()).thenReturn(new ConfigurationProperties(new Properties()));
shardingSphereStatement = new ShardingSphereStatement(connection, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.rule.builder.global.GlobalRuleBuilder;

import java.util.Map;
import java.util.Collection;

/**
* Authority rule builder.
*/
public final class AuthorityRuleBuilder implements GlobalRuleBuilder<AuthorityRuleConfiguration> {

@Override
public AuthorityRule build(final AuthorityRuleConfiguration ruleConfig, final Map<String, ShardingSphereDatabase> databases, final ConfigurationProperties props) {
public AuthorityRule build(final AuthorityRuleConfiguration ruleConfig, final Collection<ShardingSphereDatabase> databases, final ConfigurationProperties props) {
return new AuthorityRule(ruleConfig);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.rule.builder.global.GlobalRuleBuilder;

import java.util.Map;
import java.util.Collection;

/**
* Global clock rule builder.
*/
public final class GlobalClockRuleBuilder implements GlobalRuleBuilder<GlobalClockRuleConfiguration> {

@Override
public GlobalClockRule build(final GlobalClockRuleConfiguration ruleConfig, final Map<String, ShardingSphereDatabase> databases, final ConfigurationProperties props) {
public GlobalClockRule build(final GlobalClockRuleConfiguration ruleConfig, final Collection<ShardingSphereDatabase> databases, final ConfigurationProperties props) {
return new GlobalClockRule(ruleConfig);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,6 @@ class GlobalClockRuleBuilderTest {
void assertBuild() {
GlobalClockRuleConfiguration ruleConfig = new GlobalClockRuleConfiguration("FIXTURE", "FIXTURE", false, new Properties());
Map<GlobalRuleConfiguration, GlobalRuleBuilder> builders = OrderedSPILoader.getServices(GlobalRuleBuilder.class, Collections.singleton(ruleConfig));
assertThat(builders.get(ruleConfig).build(ruleConfig, Collections.emptyMap(), null), instanceOf(GlobalClockRule.class));
assertThat(builders.get(ruleConfig).build(ruleConfig, Collections.emptyList(), null), instanceOf(GlobalClockRule.class));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import org.apache.shardingsphere.logging.logger.ShardingSphereLogger;
import org.apache.shardingsphere.logging.rule.LoggingRule;

import java.util.Map;
import java.util.Collection;
import java.util.Optional;
import java.util.Properties;

Expand All @@ -36,7 +36,7 @@
public final class LoggingRuleBuilder implements GlobalRuleBuilder<LoggingRuleConfiguration> {

@Override
public LoggingRule build(final LoggingRuleConfiguration ruleConfig, final Map<String, ShardingSphereDatabase> databases, final ConfigurationProperties props) {
public LoggingRule build(final LoggingRuleConfiguration ruleConfig, final Collection<ShardingSphereDatabase> databases, final ConfigurationProperties props) {
syncLoggingRuleConfiguration(ruleConfig, props);
return new LoggingRule(ruleConfig);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ void assertBuildWhenSyncPropertiesToRule() {
ConfigurationProperties props = new ConfigurationProperties(PropertiesBuilder.build(
new Property(LoggingConstants.SQL_SHOW, Boolean.TRUE.toString()), new Property(LoggingConstants.SQL_SIMPLE, Boolean.TRUE.toString())));
LoggingRuleBuilder ruleBuilder = (LoggingRuleBuilder) OrderedSPILoader.getServices(GlobalRuleBuilder.class, Collections.singleton(ruleConfig)).get(ruleConfig);
LoggingRule actual = ruleBuilder.build(ruleConfig, Collections.emptyMap(), props);
LoggingRule actual = ruleBuilder.build(ruleConfig, Collections.emptyList(), props);
assertThat(actual.getConfiguration(), is(ruleConfig));
assertThat(logger.getProps().getProperty(LoggingConstants.SQL_LOG_ENABLE), is(Boolean.TRUE.toString()));
assertThat(logger.getProps().getProperty(LoggingConstants.SQL_LOG_SIMPLE), is(Boolean.TRUE.toString()));
Expand All @@ -58,7 +58,7 @@ void assertBuildWhenSyncRuleToProperties() {
LoggingRuleConfiguration ruleConfig = new LoggingRuleConfiguration(Collections.singleton(logger), Collections.emptyList());
ConfigurationProperties props = new ConfigurationProperties(new Properties());
LoggingRuleBuilder ruleBuilder = (LoggingRuleBuilder) OrderedSPILoader.getServices(GlobalRuleBuilder.class, Collections.singleton(ruleConfig)).get(ruleConfig);
LoggingRule actual = ruleBuilder.build(ruleConfig, Collections.emptyMap(), props);
LoggingRule actual = ruleBuilder.build(ruleConfig, Collections.emptyList(), props);
assertThat(actual.getConfiguration(), is(ruleConfig));
assertThat(props.getProps().getProperty(LoggingConstants.SQL_SHOW), is(Boolean.TRUE.toString()));
assertThat(props.getProps().getProperty(LoggingConstants.SQL_SIMPLE), is(Boolean.TRUE.toString()));
Expand All @@ -70,7 +70,7 @@ void assertBuildWhenSyncNothing() {
LoggingRuleConfiguration ruleConfig = new LoggingRuleConfiguration(Collections.singleton(logger), Collections.emptyList());
ConfigurationProperties props = new ConfigurationProperties(new Properties());
LoggingRuleBuilder ruleBuilder = (LoggingRuleBuilder) OrderedSPILoader.getServices(GlobalRuleBuilder.class, Collections.singleton(ruleConfig)).get(ruleConfig);
LoggingRule actual = ruleBuilder.build(ruleConfig, Collections.emptyMap(), props);
LoggingRule actual = ruleBuilder.build(ruleConfig, Collections.emptyList(), props);
assertThat(actual.getConfiguration(), is(ruleConfig));
assertFalse(logger.getProps().containsKey(LoggingConstants.SQL_LOG_ENABLE));
assertFalse(logger.getProps().containsKey(LoggingConstants.SQL_LOG_SIMPLE));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import org.apache.shardingsphere.sqlfederation.optimizer.context.OptimizerContext;
import org.apache.shardingsphere.sqlfederation.optimizer.context.OptimizerContextFactory;

import java.util.Map;
import java.util.Collection;
import java.util.concurrent.atomic.AtomicReference;

/**
Expand All @@ -38,13 +38,13 @@ public final class SQLFederationRule implements GlobalRule {

private final AtomicReference<OptimizerContext> optimizerContext;

public SQLFederationRule(final SQLFederationRuleConfiguration ruleConfig, final Map<String, ShardingSphereDatabase> databases) {
public SQLFederationRule(final SQLFederationRuleConfiguration ruleConfig, final Collection<ShardingSphereDatabase> databases) {
configuration = ruleConfig;
optimizerContext = new AtomicReference<>(OptimizerContextFactory.create(databases));
}

@Override
public void refresh(final Map<String, ShardingSphereDatabase> databases, final GlobalRuleChangedType changedType) {
public void refresh(final Collection<ShardingSphereDatabase> databases, final GlobalRuleChangedType changedType) {
optimizerContext.set(OptimizerContextFactory.create(databases));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@
import org.apache.shardingsphere.sqlfederation.constant.SQLFederationOrder;
import org.apache.shardingsphere.sqlfederation.rule.SQLFederationRule;

import java.util.Map;
import java.util.Collection;

/**
* SQL federation rule builder.
*/
public final class SQLFederationRuleBuilder implements GlobalRuleBuilder<SQLFederationRuleConfiguration> {

@Override
public GlobalRule build(final SQLFederationRuleConfiguration ruleConfig, final Map<String, ShardingSphereDatabase> databases, final ConfigurationProperties props) {
public GlobalRule build(final SQLFederationRuleConfiguration ruleConfig, final Collection<ShardingSphereDatabase> databases, final ConfigurationProperties props) {
return new SQLFederationRule(ruleConfig, databases);
}

Expand Down
Loading

0 comments on commit 676abda

Please sign in to comment.