From 3d0d54f9f2ef2693471b72049b8db969663ff379 Mon Sep 17 00:00:00 2001 From: zhangliang Date: Tue, 3 Dec 2024 15:29:55 +0800 Subject: [PATCH 1/2] Fix sonar issues --- .../time/MySQLTimestamp2BinlogProtocolValueTest.java | 6 +++--- .../time/MySQLTimestampBinlogProtocolValueTest.java | 2 +- .../mysql/payload/MySQLPacketPayloadTest.java | 12 ++++++------ .../SeataATShardingSphereTransactionManagerTest.java | 8 ++++---- .../backend/config/ProxyConfigurationLoaderTest.java | 12 ++++++------ .../swapper/YamlProxyConfigurationSwapperTest.java | 2 +- .../e2e/env/container/atomic/DockerITContainer.java | 2 +- .../ShardingSphereMultiProxyClusterContainer.java | 2 +- .../engine/scenario/EncryptSQLRewriterIT.java | 2 +- .../engine/scenario/ShardingSQLRewriterIT.java | 2 +- .../test/natived/jdbc/databases/MySQLTest.java | 2 +- .../test/natived/proxy/databases/MySQLTest.java | 4 ++-- 12 files changed, 28 insertions(+), 28 deletions(-) diff --git a/db-protocol/mysql/src/test/java/org/apache/shardingsphere/db/protocol/mysql/packet/binlog/row/column/value/time/MySQLTimestamp2BinlogProtocolValueTest.java b/db-protocol/mysql/src/test/java/org/apache/shardingsphere/db/protocol/mysql/packet/binlog/row/column/value/time/MySQLTimestamp2BinlogProtocolValueTest.java index 219849627de42..58f8457415a35 100644 --- a/db-protocol/mysql/src/test/java/org/apache/shardingsphere/db/protocol/mysql/packet/binlog/row/column/value/time/MySQLTimestamp2BinlogProtocolValueTest.java +++ b/db-protocol/mysql/src/test/java/org/apache/shardingsphere/db/protocol/mysql/packet/binlog/row/column/value/time/MySQLTimestamp2BinlogProtocolValueTest.java @@ -52,7 +52,7 @@ void setUp() { @Test void assertReadWithoutFraction() { - int currentSeconds = Long.valueOf(System.currentTimeMillis() / 1000L).intValue(); + int currentSeconds = (int) (System.currentTimeMillis() / 1000L); when(byteBuf.readInt()).thenReturn(currentSeconds); assertThat(new MySQLTimestamp2BinlogProtocolValue().read(columnDef, payload), is(new Timestamp(currentSeconds * 1000L))); } @@ -61,8 +61,8 @@ void assertReadWithoutFraction() { void assertReadWithFraction() { columnDef.setColumnMeta(1); long currentTimeMillis = 1678795614082L; - int currentSeconds = Long.valueOf(currentTimeMillis / 1000L).intValue(); - int currentMilliseconds = Long.valueOf(currentTimeMillis % 100L).intValue(); + int currentSeconds = (int) (System.currentTimeMillis() / 1000L); + int currentMilliseconds = (int) (currentTimeMillis % 100L); when(payload.readInt1()).thenReturn(currentMilliseconds); when(byteBuf.readInt()).thenReturn(currentSeconds); assertThat("currentTimeMillis:" + currentTimeMillis, new MySQLTimestamp2BinlogProtocolValue().read(columnDef, payload), is(new Timestamp(currentSeconds * 1000L + currentMilliseconds * 10L))); diff --git a/db-protocol/mysql/src/test/java/org/apache/shardingsphere/db/protocol/mysql/packet/binlog/row/column/value/time/MySQLTimestampBinlogProtocolValueTest.java b/db-protocol/mysql/src/test/java/org/apache/shardingsphere/db/protocol/mysql/packet/binlog/row/column/value/time/MySQLTimestampBinlogProtocolValueTest.java index 6f128c8d291ab..37b6737e2711a 100644 --- a/db-protocol/mysql/src/test/java/org/apache/shardingsphere/db/protocol/mysql/packet/binlog/row/column/value/time/MySQLTimestampBinlogProtocolValueTest.java +++ b/db-protocol/mysql/src/test/java/org/apache/shardingsphere/db/protocol/mysql/packet/binlog/row/column/value/time/MySQLTimestampBinlogProtocolValueTest.java @@ -42,7 +42,7 @@ class MySQLTimestampBinlogProtocolValueTest { @Test void assertRead() { - int currentSeconds = Long.valueOf(System.currentTimeMillis() / 1000L).intValue(); + int currentSeconds = (int) (System.currentTimeMillis() / 1000L); when(payload.readInt4()).thenReturn(currentSeconds); assertThat(new MySQLTimestampBinlogProtocolValue().read(columnDef, payload), is(DateTimeFormatterFactory.getStandardFormatter().format(new Timestamp(currentSeconds * 1000L).toLocalDateTime()))); diff --git a/db-protocol/mysql/src/test/java/org/apache/shardingsphere/db/protocol/mysql/payload/MySQLPacketPayloadTest.java b/db-protocol/mysql/src/test/java/org/apache/shardingsphere/db/protocol/mysql/payload/MySQLPacketPayloadTest.java index 753f0a531a4d9..346c67767b7c5 100644 --- a/db-protocol/mysql/src/test/java/org/apache/shardingsphere/db/protocol/mysql/payload/MySQLPacketPayloadTest.java +++ b/db-protocol/mysql/src/test/java/org/apache/shardingsphere/db/protocol/mysql/payload/MySQLPacketPayloadTest.java @@ -151,23 +151,23 @@ void assertWriteIntLenencWithOneByte() { @Test void assertWriteIntLenencWithTwoBytes() { - new MySQLPacketPayload(byteBuf, StandardCharsets.UTF_8).writeIntLenenc(Double.valueOf(Math.pow(2D, 16D)).longValue() - 1L); + new MySQLPacketPayload(byteBuf, StandardCharsets.UTF_8).writeIntLenenc((long) (Math.pow(2D, 16D)) - 1L); verify(byteBuf).writeByte(0xfc); - verify(byteBuf).writeShortLE(Double.valueOf(Math.pow(2D, 16D)).intValue() - 1); + verify(byteBuf).writeShortLE((int) (Math.pow(2D, 16D)) - 1); } @Test void assertWriteIntLenencWithThreeBytes() { - new MySQLPacketPayload(byteBuf, StandardCharsets.UTF_8).writeIntLenenc(Double.valueOf(Math.pow(2D, 24D)).longValue() - 1L); + new MySQLPacketPayload(byteBuf, StandardCharsets.UTF_8).writeIntLenenc((long) (Math.pow(2D, 24D)) - 1L); verify(byteBuf).writeByte(0xfd); - verify(byteBuf).writeMediumLE(Double.valueOf(Math.pow(2D, 24D)).intValue() - 1); + verify(byteBuf).writeMediumLE((int) (Math.pow(2D, 24D)) - 1); } @Test void assertWriteIntLenencWithFourBytes() { - new MySQLPacketPayload(byteBuf, StandardCharsets.UTF_8).writeIntLenenc(Double.valueOf(Math.pow(2D, 25D)).longValue() - 1L); + new MySQLPacketPayload(byteBuf, StandardCharsets.UTF_8).writeIntLenenc((long) (Math.pow(2D, 25D)) - 1L); verify(byteBuf).writeByte(0xfe); - verify(byteBuf).writeLongLE(Double.valueOf(Math.pow(2D, 25D)).intValue() - 1L); + verify(byteBuf).writeLongLE((int) (Math.pow(2D, 25D)) - 1L); } @Test diff --git a/kernel/transaction/type/base/seata-at/src/test/java/org/apache/shardingsphere/transaction/base/seata/at/SeataATShardingSphereTransactionManagerTest.java b/kernel/transaction/type/base/seata-at/src/test/java/org/apache/shardingsphere/transaction/base/seata/at/SeataATShardingSphereTransactionManagerTest.java index 0d3f9a921ff96..ae9f350790507 100644 --- a/kernel/transaction/type/base/seata-at/src/test/java/org/apache/shardingsphere/transaction/base/seata/at/SeataATShardingSphereTransactionManagerTest.java +++ b/kernel/transaction/type/base/seata-at/src/test/java/org/apache/shardingsphere/transaction/base/seata/at/SeataATShardingSphereTransactionManagerTest.java @@ -163,11 +163,11 @@ void assertRollbackWithoutBegin() { } private void assertResult(final Class requestClass, final Class responseClass) { - assertTrue(requestQueue.stream().anyMatch(each -> each instanceof RegisterTMRequest)); - assertTrue(requestQueue.stream().anyMatch(each -> each instanceof RegisterRMRequest)); + assertTrue(requestQueue.stream().anyMatch(RegisterTMRequest.class::isInstance)); + assertTrue(requestQueue.stream().anyMatch(RegisterRMRequest.class::isInstance)); assertTrue(requestQueue.stream().anyMatch(each -> requestClass.equals(each.getClass()))); - assertTrue(responseQueue.stream().anyMatch(each -> each instanceof RegisterTMResponse)); - assertTrue(responseQueue.stream().anyMatch(each -> each instanceof RegisterRMResponse)); + assertTrue(responseQueue.stream().anyMatch(RegisterTMResponse.class::isInstance)); + assertTrue(responseQueue.stream().anyMatch(RegisterRMResponse.class::isInstance)); assertTrue(responseQueue.stream().anyMatch(each -> responseClass.equals(each.getClass()))); while (!requestQueue.isEmpty()) { Object requestPackage = requestQueue.poll(); diff --git a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/config/ProxyConfigurationLoaderTest.java b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/config/ProxyConfigurationLoaderTest.java index 3f912b1fa8842..300b9fdca2d77 100644 --- a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/config/ProxyConfigurationLoaderTest.java +++ b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/config/ProxyConfigurationLoaderTest.java @@ -73,11 +73,11 @@ private void assertShardingRuleConfiguration(final YamlProxyDatabaseConfiguratio assertDataSourceConfiguration(actual.getDataSources().get("ds_0"), "jdbc:mysql://127.0.0.1:3306/ds_0"); assertDataSourceConfiguration(actual.getDataSources().get("ds_1"), "jdbc:mysql://127.0.0.1:3306/ds_1"); Optional shardingRuleConfig = actual.getRules().stream() - .filter(each -> each instanceof YamlShardingRuleConfiguration).findFirst().map(each -> (YamlShardingRuleConfiguration) each); + .filter(YamlShardingRuleConfiguration.class::isInstance).findFirst().map(YamlShardingRuleConfiguration.class::cast); assertTrue(shardingRuleConfig.isPresent()); assertShardingRuleConfiguration(shardingRuleConfig.get()); assertFalse( - actual.getRules().stream().filter(each -> each instanceof YamlEncryptRuleConfiguration).findFirst().map(each -> (YamlEncryptRuleConfiguration) each).isPresent()); + actual.getRules().stream().filter(YamlEncryptRuleConfiguration.class::isInstance).findFirst().map(YamlEncryptRuleConfiguration.class::cast).isPresent()); } private void assertShardingRuleConfiguration(final YamlShardingRuleConfiguration actual) { @@ -96,11 +96,11 @@ private void assertReadwriteSplittingRuleConfiguration(final YamlProxyDatabaseCo assertDataSourceConfiguration(actual.getDataSources().get("write_ds"), "jdbc:mysql://127.0.0.1:3306/write_ds"); assertDataSourceConfiguration(actual.getDataSources().get("read_ds_0"), "jdbc:mysql://127.0.0.1:3306/read_ds_0"); assertDataSourceConfiguration(actual.getDataSources().get("read_ds_1"), "jdbc:mysql://127.0.0.1:3306/read_ds_1"); - assertFalse(actual.getRules().stream().filter(each -> each instanceof YamlShardingRuleConfiguration).findFirst().map(each -> (YamlShardingRuleConfiguration) each).isPresent()); + assertFalse(actual.getRules().stream().filter(YamlShardingRuleConfiguration.class::isInstance).findFirst().map(YamlShardingRuleConfiguration.class::cast).isPresent()); assertFalse( actual.getRules().stream().filter(each -> each instanceof YamlEncryptRuleConfiguration).findFirst().map(each -> (YamlEncryptRuleConfiguration) each).isPresent()); Optional ruleConfig = actual.getRules().stream() - .filter(each -> each instanceof YamlReadwriteSplittingRuleConfiguration).findFirst().map(each -> (YamlReadwriteSplittingRuleConfiguration) each); + .filter(YamlReadwriteSplittingRuleConfiguration.class::isInstance).findFirst().map(YamlReadwriteSplittingRuleConfiguration.class::cast); assertTrue(ruleConfig.isPresent()); for (YamlReadwriteSplittingDataSourceGroupRuleConfiguration each : ruleConfig.get().getDataSourceGroups().values()) { assertReadwriteSplittingRuleConfiguration(each); @@ -117,9 +117,9 @@ private void assertEncryptRuleConfiguration(final YamlProxyDatabaseConfiguration assertThat(actual.getDataSources().size(), is(1)); assertDataSourceConfiguration(actual.getDataSources().get("ds_0"), "jdbc:mysql://127.0.0.1:3306/encrypt_ds"); assertFalse(actual.getRules().stream() - .filter(each -> each instanceof YamlShardingRuleConfiguration).findFirst().map(each -> (YamlShardingRuleConfiguration) each).isPresent()); + .filter(YamlShardingRuleConfiguration.class::isInstance).findFirst().map(YamlShardingRuleConfiguration.class::cast).isPresent()); Optional encryptRuleConfig = actual.getRules().stream() - .filter(each -> each instanceof YamlEncryptRuleConfiguration).findFirst().map(each -> (YamlEncryptRuleConfiguration) each); + .filter(YamlEncryptRuleConfiguration.class::isInstance).findFirst().map(YamlEncryptRuleConfiguration.class::cast); assertTrue(encryptRuleConfig.isPresent()); assertEncryptRuleConfiguration(encryptRuleConfig.get()); } diff --git a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/config/yaml/swapper/YamlProxyConfigurationSwapperTest.java b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/config/yaml/swapper/YamlProxyConfigurationSwapperTest.java index 678790a3eab64..d5f323838d8a3 100644 --- a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/config/yaml/swapper/YamlProxyConfigurationSwapperTest.java +++ b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/config/yaml/swapper/YamlProxyConfigurationSwapperTest.java @@ -97,7 +97,7 @@ private void assertAuthorityRuleConfiguration(final ProxyConfiguration proxyConf } private Optional findAuthorityRuleConfiguration(final Collection globalRuleConfigs) { - return globalRuleConfigs.stream().filter(each -> each instanceof AuthorityRuleConfiguration).findFirst().map(each -> (AuthorityRuleConfiguration) each); + return globalRuleConfigs.stream().filter(AuthorityRuleConfiguration.class::isInstance).findFirst().map(AuthorityRuleConfiguration.class::cast); } private void assertProxyConfigurationProps(final ProxyConfiguration proxyConfig) { diff --git a/test/e2e/env/src/test/java/org/apache/shardingsphere/test/e2e/env/container/atomic/DockerITContainer.java b/test/e2e/env/src/test/java/org/apache/shardingsphere/test/e2e/env/container/atomic/DockerITContainer.java index caf74c366ae81..8f99a40294398 100644 --- a/test/e2e/env/src/test/java/org/apache/shardingsphere/test/e2e/env/container/atomic/DockerITContainer.java +++ b/test/e2e/env/src/test/java/org/apache/shardingsphere/test/e2e/env/container/atomic/DockerITContainer.java @@ -53,7 +53,7 @@ public void start() { } private void startDependencies() { - Collection dependencies = getDependencies().stream().filter(each -> each instanceof DockerITContainer).map(each -> (DockerITContainer) each).collect(Collectors.toList()); + Collection dependencies = getDependencies().stream().filter(DockerITContainer.class::isInstance).map(DockerITContainer.class::cast).collect(Collectors.toList()); dependencies.stream().filter(each -> !each.isCreated()).forEach(GenericContainer::start); dependencies.stream() .filter(each -> { diff --git a/test/e2e/env/src/test/java/org/apache/shardingsphere/test/e2e/env/container/atomic/adapter/impl/ShardingSphereMultiProxyClusterContainer.java b/test/e2e/env/src/test/java/org/apache/shardingsphere/test/e2e/env/container/atomic/adapter/impl/ShardingSphereMultiProxyClusterContainer.java index 3bd4be35ebeeb..bb41ba9e7571f 100644 --- a/test/e2e/env/src/test/java/org/apache/shardingsphere/test/e2e/env/container/atomic/adapter/impl/ShardingSphereMultiProxyClusterContainer.java +++ b/test/e2e/env/src/test/java/org/apache/shardingsphere/test/e2e/env/container/atomic/adapter/impl/ShardingSphereMultiProxyClusterContainer.java @@ -79,7 +79,7 @@ public void start() { @Override public Collection getContainers() { - return proxyClusterContainers.stream().map(each -> (ITContainer) each).collect(Collectors.toList()); + return proxyClusterContainers.stream().map(ITContainer.class::cast).collect(Collectors.toList()); } private static class RandomDataSourceAdapter implements DataSource { diff --git a/test/it/rewriter/src/test/java/org/apache/shardingsphere/test/it/rewrite/engine/scenario/EncryptSQLRewriterIT.java b/test/it/rewriter/src/test/java/org/apache/shardingsphere/test/it/rewrite/engine/scenario/EncryptSQLRewriterIT.java index b2d127dce06f9..1e314b7a83a70 100644 --- a/test/it/rewriter/src/test/java/org/apache/shardingsphere/test/it/rewrite/engine/scenario/EncryptSQLRewriterIT.java +++ b/test/it/rewriter/src/test/java/org/apache/shardingsphere/test/it/rewrite/engine/scenario/EncryptSQLRewriterIT.java @@ -100,7 +100,7 @@ protected Collection mockSchemas(final String schemaName) @Override protected void mockRules(final Collection rules, final String schemaName, final SQLStatement sqlStatement) { - Optional singleRule = rules.stream().filter(each -> each instanceof SingleRule).map(each -> (SingleRule) each).findFirst(); + Optional singleRule = rules.stream().filter(SingleRule.class::isInstance).map(SingleRule.class::cast).findFirst(); if (singleRule.isPresent() && !(sqlStatement instanceof CreateTableStatement)) { singleRule.get().getAttributes().getAttribute(MutableDataNodeRuleAttribute.class).put("encrypt_ds", schemaName, "t_account"); singleRule.get().getAttributes().getAttribute(MutableDataNodeRuleAttribute.class).put("encrypt_ds", schemaName, "t_account_bak"); diff --git a/test/it/rewriter/src/test/java/org/apache/shardingsphere/test/it/rewrite/engine/scenario/ShardingSQLRewriterIT.java b/test/it/rewriter/src/test/java/org/apache/shardingsphere/test/it/rewrite/engine/scenario/ShardingSQLRewriterIT.java index 95d2ba30b5409..0c939beff9665 100644 --- a/test/it/rewriter/src/test/java/org/apache/shardingsphere/test/it/rewrite/engine/scenario/ShardingSQLRewriterIT.java +++ b/test/it/rewriter/src/test/java/org/apache/shardingsphere/test/it/rewrite/engine/scenario/ShardingSQLRewriterIT.java @@ -56,7 +56,7 @@ protected YamlRootConfiguration createRootConfiguration(final SQLRewriteEngineTe @Override protected void mockRules(final Collection rules, final String schemaName, final SQLStatement sqlStatement) { - Optional singleRule = rules.stream().filter(each -> each instanceof SingleRule).map(each -> (SingleRule) each).findFirst(); + Optional singleRule = rules.stream().filter(SingleRule.class::isInstance).map(SingleRule.class::cast).findFirst(); if (singleRule.isPresent() && !(sqlStatement instanceof CreateTableStatement)) { singleRule.get().getAttributes().getAttribute(MutableDataNodeRuleAttribute.class).put("db", schemaName, "t_single"); singleRule.get().getAttributes().getAttribute(MutableDataNodeRuleAttribute.class).put("db", schemaName, "t_single_extend"); diff --git a/test/native/src/test/java/org/apache/shardingsphere/test/natived/jdbc/databases/MySQLTest.java b/test/native/src/test/java/org/apache/shardingsphere/test/natived/jdbc/databases/MySQLTest.java index 33c9912e81788..876c2659d2fd9 100644 --- a/test/native/src/test/java/org/apache/shardingsphere/test/natived/jdbc/databases/MySQLTest.java +++ b/test/native/src/test/java/org/apache/shardingsphere/test/natived/jdbc/databases/MySQLTest.java @@ -111,7 +111,7 @@ private Connection openConnection() throws SQLException { @SuppressWarnings({"SqlDialectInspection", "SqlNoDataSourceInspection"}) private DataSource createDataSource() throws SQLException { - Awaitility.await().atMost(Duration.ofMinutes(1L)).ignoreExceptionsMatching(e -> e instanceof CommunicationsException).until(() -> { + Awaitility.await().atMost(Duration.ofMinutes(1L)).ignoreExceptionsMatching(CommunicationsException.class::isInstance).until(() -> { openConnection().close(); return true; }); diff --git a/test/native/src/test/java/org/apache/shardingsphere/test/natived/proxy/databases/MySQLTest.java b/test/native/src/test/java/org/apache/shardingsphere/test/natived/proxy/databases/MySQLTest.java index f5a767f76db62..ee9c694297296 100644 --- a/test/native/src/test/java/org/apache/shardingsphere/test/natived/proxy/databases/MySQLTest.java +++ b/test/native/src/test/java/org/apache/shardingsphere/test/natived/proxy/databases/MySQLTest.java @@ -56,7 +56,7 @@ class MySQLTest { @BeforeAll static void beforeAll() throws SQLException { - Awaitility.await().atMost(Duration.ofSeconds(30L)).ignoreExceptionsMatching(e -> e instanceof CommunicationsException).until(() -> { + Awaitility.await().atMost(Duration.ofSeconds(30L)).ignoreExceptionsMatching(CommunicationsException.class::isInstance).until(() -> { openConnection("root", "yourStrongPassword123!", "jdbc:mysql://127.0.0.1:" + MYSQL_CONTAINER.getMappedPort(3306)) .close(); return true; @@ -70,7 +70,7 @@ static void beforeAll() throws SQLException { } String absolutePath = Paths.get("src/test/resources/test-native/yaml/proxy/databases/mysql").toAbsolutePath().toString(); proxyTestingServer = new ProxyTestingServer(absolutePath); - Awaitility.await().atMost(Duration.ofSeconds(30L)).ignoreExceptionsMatching(e -> e instanceof CommunicationsException).until(() -> { + Awaitility.await().atMost(Duration.ofSeconds(30L)).ignoreExceptionsMatching(CommunicationsException.class::isInstance).until(() -> { openConnection("root", "root", "jdbc:mysql://127.0.0.1:" + proxyTestingServer.getProxyPort()).close(); return true; }); From 167f2fb73ceff8458afa1787a4926c9890e7a978 Mon Sep 17 00:00:00 2001 From: zhangliang Date: Tue, 3 Dec 2024 15:57:58 +0800 Subject: [PATCH 2/2] 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() {