From 167f2fb73ceff8458afa1787a4926c9890e7a978 Mon Sep 17 00:00:00 2001 From: zhangliang Date: Tue, 3 Dec 2024 15:57:58 +0800 Subject: [PATCH] Refactor GlobalRulesBuilder --- .../infra/rule/builder/global/GlobalRulesBuilder.java | 4 ++-- .../infra/rule/builder/global/GlobalRulesBuilderTest.java | 2 +- .../mode/metadata/MetaDataContextManager.java | 2 +- .../mode/metadata/MetaDataContextsFactory.java | 8 ++++---- .../mode/metadata/MetaDataContextsFactoryTest.java | 2 +- 5 files changed, 9 insertions(+), 9 deletions(-) diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/rule/builder/global/GlobalRulesBuilder.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/rule/builder/global/GlobalRulesBuilder.java index 3786f91c4d751..e8d8a230027af 100644 --- a/infra/common/src/main/java/org/apache/shardingsphere/infra/rule/builder/global/GlobalRulesBuilder.java +++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/rule/builder/global/GlobalRulesBuilder.java @@ -49,10 +49,10 @@ public final class GlobalRulesBuilder { */ @SuppressWarnings({"unchecked", "rawtypes"}) public static Collection buildRules(final Collection globalRuleConfigs, - final Map databases, final ConfigurationProperties props) { + final Collection databases, final ConfigurationProperties props) { Collection result = new LinkedList<>(); for (Entry entry : getRuleBuilderMap(globalRuleConfigs).entrySet()) { - result.add(entry.getValue().build(entry.getKey(), databases.values(), props)); + result.add(entry.getValue().build(entry.getKey(), databases, props)); } return result; } diff --git a/infra/common/src/test/java/org/apache/shardingsphere/infra/rule/builder/global/GlobalRulesBuilderTest.java b/infra/common/src/test/java/org/apache/shardingsphere/infra/rule/builder/global/GlobalRulesBuilderTest.java index 7825f71a7b7ce..1474b5356f05a 100644 --- a/infra/common/src/test/java/org/apache/shardingsphere/infra/rule/builder/global/GlobalRulesBuilderTest.java +++ b/infra/common/src/test/java/org/apache/shardingsphere/infra/rule/builder/global/GlobalRulesBuilderTest.java @@ -40,7 +40,7 @@ class GlobalRulesBuilderTest { @Test void assertBuildRules() { Collection shardingSphereRules = GlobalRulesBuilder.buildRules( - Collections.singletonList(new FixtureGlobalRuleConfiguration()), Collections.singletonMap("foo_db", buildDatabase()), mock(ConfigurationProperties.class)); + Collections.singletonList(new FixtureGlobalRuleConfiguration()), Collections.singleton(buildDatabase()), mock(ConfigurationProperties.class)); assertThat(shardingSphereRules.size(), is(1)); assertThat(shardingSphereRules.iterator().next(), instanceOf(FixtureGlobalRule.class)); } diff --git a/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/MetaDataContextManager.java b/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/MetaDataContextManager.java index 4771fd5f1cadc..0d76a5bec88cf 100644 --- a/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/MetaDataContextManager.java +++ b/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/MetaDataContextManager.java @@ -154,7 +154,7 @@ private MetaDataContexts createMetaDataContexts(final ShardingSphereDatabase dat .createChangedDatabases(database.getName(), false, switchingResource, ruleConfigs, metaDataContexts.get(), metaDataPersistService, computeNodeInstanceContext); ConfigurationProperties props = new ConfigurationProperties(metaDataPersistService.getPropsService().load()); RuleMetaData changedGlobalMetaData = new RuleMetaData( - GlobalRulesBuilder.buildRules(ruleConfigPersistDecorateEngine.restore(metaDataPersistService.getGlobalRuleService().load()), changedDatabases, props)); + GlobalRulesBuilder.buildRules(ruleConfigPersistDecorateEngine.restore(metaDataPersistService.getGlobalRuleService().load()), changedDatabases.values(), props)); MetaDataContexts result = MetaDataContextsFactory.create(metaDataPersistService, new ShardingSphereMetaData(changedDatabases, metaDataContexts.get().getMetaData().getGlobalResourceMetaData(), changedGlobalMetaData, props)); switchingResource.closeStaleDataSources(); diff --git a/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/MetaDataContextsFactory.java b/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/MetaDataContextsFactory.java index 6318ad6ec6a2f..5a24febf0289a 100644 --- a/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/MetaDataContextsFactory.java +++ b/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/MetaDataContextsFactory.java @@ -135,7 +135,7 @@ private static MetaDataContexts newMetaDataContexts(final MetaDataPersistService final ConfigurationProperties props) { // TODO load global data sources from persist service ResourceMetaData globalResourceMetaData = new ResourceMetaData(param.getGlobalDataSources()); - RuleMetaData globalRuleMetaData = new RuleMetaData(GlobalRulesBuilder.buildRules(globalRuleConfigs, databases, props)); + RuleMetaData globalRuleMetaData = new RuleMetaData(GlobalRulesBuilder.buildRules(globalRuleConfigs, databases.values(), props)); ShardingSphereMetaData shardingSphereMetaData = new ShardingSphereMetaData(databases, globalResourceMetaData, globalRuleMetaData, props); ShardingSphereStatistics shardingSphereStatistics = initStatistics(persistService, shardingSphereMetaData); return new MetaDataContexts(shardingSphereMetaData, shardingSphereStatistics); @@ -219,7 +219,7 @@ private static void restoreRules(final MetaDataContexts metaDataContexts, final metaDataContexts.getMetaData().getGlobalRuleMetaData().getRules().removeIf(eachRule -> each.getRuleType() == eachRule.getClass()); RuleConfiguration restoredRuleConfig = each.restore(rule.getConfiguration()); ShardingSphereRule rebuiltRule = GlobalRulesBuilder.buildRules( - Collections.singleton(restoredRuleConfig), metaDataContexts.getMetaData().getDatabases(), metaDataContexts.getMetaData().getProps()).iterator().next(); + Collections.singleton(restoredRuleConfig), metaDataContexts.getMetaData().getAllDatabases(), metaDataContexts.getMetaData().getProps()).iterator().next(); metaDataContexts.getMetaData().getGlobalRuleMetaData().getRules().add(rebuiltRule); } } @@ -271,7 +271,7 @@ public static MetaDataContexts createBySwitchResource(final String databaseName, createChangedDatabases(databaseName, internalLoadMetaData, switchingResource, null, originalMetaDataContexts, metaDataPersistService, computeNodeInstanceContext); ConfigurationProperties props = originalMetaDataContexts.getMetaData().getProps(); RuleMetaData changedGlobalMetaData = new RuleMetaData( - GlobalRulesBuilder.buildRules(originalMetaDataContexts.getMetaData().getGlobalRuleMetaData().getConfigurations(), changedDatabases, props)); + GlobalRulesBuilder.buildRules(originalMetaDataContexts.getMetaData().getGlobalRuleMetaData().getConfigurations(), changedDatabases.values(), props)); return create(metaDataPersistService, new ShardingSphereMetaData(changedDatabases, originalMetaDataContexts.getMetaData().getGlobalResourceMetaData(), changedGlobalMetaData, props)); } @@ -294,7 +294,7 @@ public static MetaDataContexts createByAlterRule(final String databaseName, fina createChangedDatabases(databaseName, internalLoadMetaData, null, ruleConfigs, originalMetaDataContexts, metaDataPersistService, computeNodeInstanceContext); ConfigurationProperties props = originalMetaDataContexts.getMetaData().getProps(); RuleMetaData changedGlobalMetaData = new RuleMetaData( - GlobalRulesBuilder.buildRules(originalMetaDataContexts.getMetaData().getGlobalRuleMetaData().getConfigurations(), changedDatabases, props)); + GlobalRulesBuilder.buildRules(originalMetaDataContexts.getMetaData().getGlobalRuleMetaData().getConfigurations(), changedDatabases.values(), props)); return create(metaDataPersistService, new ShardingSphereMetaData(changedDatabases, originalMetaDataContexts.getMetaData().getGlobalResourceMetaData(), changedGlobalMetaData, props)); } diff --git a/mode/core/src/test/java/org/apache/shardingsphere/mode/metadata/MetaDataContextsFactoryTest.java b/mode/core/src/test/java/org/apache/shardingsphere/mode/metadata/MetaDataContextsFactoryTest.java index 23ce6e4f6795a..3691e7a3d5aec 100644 --- a/mode/core/src/test/java/org/apache/shardingsphere/mode/metadata/MetaDataContextsFactoryTest.java +++ b/mode/core/src/test/java/org/apache/shardingsphere/mode/metadata/MetaDataContextsFactoryTest.java @@ -88,7 +88,7 @@ void setUp() throws SQLException { when(database.getProtocolType()).thenReturn(TypedSPILoader.getService(DatabaseType.class, "FIXTURE")); when(database.getRuleMetaData().getRules()).thenReturn(Collections.emptyList()); when(ExternalMetaDataFactory.create(anyMap(), any(), any())).thenReturn(new HashMap<>(Collections.singletonMap("foo_db", database))); - when(GlobalRulesBuilder.buildRules(anyCollection(), anyMap(), any(ConfigurationProperties.class))).thenReturn(Collections.singleton(new MockedRule())); + when(GlobalRulesBuilder.buildRules(anyCollection(), anyCollection(), any(ConfigurationProperties.class))).thenReturn(Collections.singleton(new MockedRule())); } private DatabaseRulePersistService mockDatabaseRulePersistService() {