From 10dc2ddfd4497f2eafee892bbba5e0fe5b737f08 Mon Sep 17 00:00:00 2001 From: Zhengqiang Duan Date: Wed, 18 Oct 2023 18:28:58 +0800 Subject: [PATCH] Refactor SingleRule to pass protocolType for heterogeneous database scenario single table load (#28795) * Refactor SingleRule to pass protocolType for heterogeneous database scenario single table load * fix unit test * Revert oracle pom dependency --- .../rule/builder/BroadcastRuleBuilder.java | 5 +-- .../builder/BroadcastRuleBuilderTest.java | 3 +- .../builder/CompatibleEncryptRuleBuilder.java | 3 +- .../rule/builder/EncryptRuleBuilder.java | 3 +- .../rule/builder/EncryptRuleBuilderTest.java | 3 +- .../mask/rule/builder/MaskRuleBuilder.java | 3 +- .../rule/builder/MaskRuleBuilderTest.java | 3 +- .../ReadwriteSplittingRuleBuilder.java | 3 +- .../ReadwriteSplittingRuleBuilderTest.java | 3 +- .../rule/builder/ShadowRuleBuilder.java | 3 +- .../rule/builder/ShadowRuleBuilderTest.java | 3 +- .../rule/builder/ShardingRuleBuilder.java | 5 +-- .../ShardingRoutingEngineFixtureBuilder.java | 3 +- .../rule/builder/ShardingRuleBuilderTest.java | 7 +++-- .../database/ShardingSphereDatabase.java | 2 +- .../builder/database/DatabaseRuleBuilder.java | 4 ++- .../database/DatabaseRulesBuilder.java | 13 +++++--- .../database/DatabaseRulesBuilderTest.java | 5 +-- .../fixture/FixtureDatabaseRuleBuilder.java | 3 +- .../factory/InternalMetaDataFactory.java | 7 +++-- .../factory/NewInternalMetaDataFactory.java | 7 +++-- .../datanode/SingleTableDataNodeLoader.java | 29 +++++++++-------- .../SingleRuleConfigurationDecorator.java | 4 +-- .../single/rule/SingleRule.java | 26 ++++++++-------- .../rule/builder/SingleRuleBuilder.java | 5 +-- .../SingleTableDataNodeLoaderTest.java | 1 + .../single/route/SingleSQLRouterTest.java | 13 ++++---- ...ingleDatabaseBroadcastRouteEngineTest.java | 3 +- .../engine/SingleStandardRouteEngineTest.java | 8 +++-- .../single/rule/SingleRuleTest.java | 31 ++++++++++--------- .../rule/builder/SingleRuleBuilderTest.java | 7 +++-- .../ShowUnloadedSingleTableExecutor.java | 2 +- .../LoadSingleTableStatementUpdater.java | 4 ++- .../context/ConfigurationContextManager.java | 4 +-- .../mode/fixture/ModeRuleBuilderFixture.java | 3 +- ...mlDatabaseConfigurationImportExecutor.java | 2 +- .../shardingsphere/proxy/Bootstrap.java | 6 ++-- .../test/it/rewrite/engine/SQLRewriterIT.java | 6 ++-- 38 files changed, 140 insertions(+), 105 deletions(-) diff --git a/features/broadcast/core/src/main/java/org/apache/shardingsphere/broadcast/rule/builder/BroadcastRuleBuilder.java b/features/broadcast/core/src/main/java/org/apache/shardingsphere/broadcast/rule/builder/BroadcastRuleBuilder.java index c3cb032add418..1d1365af371c2 100644 --- a/features/broadcast/core/src/main/java/org/apache/shardingsphere/broadcast/rule/builder/BroadcastRuleBuilder.java +++ b/features/broadcast/core/src/main/java/org/apache/shardingsphere/broadcast/rule/builder/BroadcastRuleBuilder.java @@ -20,6 +20,7 @@ import org.apache.shardingsphere.broadcast.api.config.BroadcastRuleConfiguration; import org.apache.shardingsphere.broadcast.constant.BroadcastOrder; import org.apache.shardingsphere.broadcast.rule.BroadcastRule; +import org.apache.shardingsphere.infra.database.core.type.DatabaseType; import org.apache.shardingsphere.infra.instance.InstanceContext; import org.apache.shardingsphere.infra.rule.ShardingSphereRule; import org.apache.shardingsphere.infra.rule.builder.database.DatabaseRuleBuilder; @@ -34,8 +35,8 @@ public final class BroadcastRuleBuilder implements DatabaseRuleBuilder { @Override - public BroadcastRule build(final BroadcastRuleConfiguration config, final String databaseName, final Map dataSources, - final Collection builtRules, final InstanceContext instanceContext) { + public BroadcastRule build(final BroadcastRuleConfiguration config, final String databaseName, final DatabaseType protocolType, + final Map dataSources, final Collection builtRules, final InstanceContext instanceContext) { return new BroadcastRule(config, databaseName, dataSources); } diff --git a/features/broadcast/core/src/test/java/org/apache/shardingsphere/broadcast/rule/builder/BroadcastRuleBuilderTest.java b/features/broadcast/core/src/test/java/org/apache/shardingsphere/broadcast/rule/builder/BroadcastRuleBuilderTest.java index cc6d18d1ab425..e0c579de2500d 100644 --- a/features/broadcast/core/src/test/java/org/apache/shardingsphere/broadcast/rule/builder/BroadcastRuleBuilderTest.java +++ b/features/broadcast/core/src/test/java/org/apache/shardingsphere/broadcast/rule/builder/BroadcastRuleBuilderTest.java @@ -22,6 +22,7 @@ import org.apache.shardingsphere.infra.instance.InstanceContext; import org.apache.shardingsphere.infra.rule.builder.database.DatabaseRuleBuilder; import org.apache.shardingsphere.infra.spi.type.ordered.OrderedSPILoader; +import org.apache.shardingsphere.test.fixture.database.MockedDatabaseType; import org.junit.jupiter.api.Test; import java.util.Collections; @@ -37,6 +38,6 @@ class BroadcastRuleBuilderTest { void assertBuild() { BroadcastRuleConfiguration ruleConfig = mock(BroadcastRuleConfiguration.class); DatabaseRuleBuilder builder = OrderedSPILoader.getServices(DatabaseRuleBuilder.class, Collections.singleton(ruleConfig)).get(ruleConfig); - assertThat(builder.build(ruleConfig, "", Collections.emptyMap(), Collections.emptyList(), mock(InstanceContext.class)), instanceOf(BroadcastRule.class)); + assertThat(builder.build(ruleConfig, "", new MockedDatabaseType(), Collections.emptyMap(), Collections.emptyList(), mock(InstanceContext.class)), instanceOf(BroadcastRule.class)); } } diff --git a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rule/builder/CompatibleEncryptRuleBuilder.java b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rule/builder/CompatibleEncryptRuleBuilder.java index 57bfc21ccefe7..20a2ad62e2818 100644 --- a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rule/builder/CompatibleEncryptRuleBuilder.java +++ b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rule/builder/CompatibleEncryptRuleBuilder.java @@ -20,6 +20,7 @@ import org.apache.shardingsphere.encrypt.api.config.CompatibleEncryptRuleConfiguration; import org.apache.shardingsphere.encrypt.constant.EncryptOrder; import org.apache.shardingsphere.encrypt.rule.EncryptRule; +import org.apache.shardingsphere.infra.database.core.type.DatabaseType; import org.apache.shardingsphere.infra.instance.InstanceContext; import org.apache.shardingsphere.infra.rule.ShardingSphereRule; import org.apache.shardingsphere.infra.rule.builder.database.DatabaseRuleBuilder; @@ -37,7 +38,7 @@ public final class CompatibleEncryptRuleBuilder implements DatabaseRuleBuilder { @Override - public EncryptRule build(final CompatibleEncryptRuleConfiguration config, final String databaseName, + public EncryptRule build(final CompatibleEncryptRuleConfiguration config, final String databaseName, final DatabaseType protocolType, final Map dataSources, final Collection builtRules, final InstanceContext instanceContext) { return new EncryptRule(databaseName, config); } diff --git a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rule/builder/EncryptRuleBuilder.java b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rule/builder/EncryptRuleBuilder.java index 34b8d7622bb69..d65d87539e867 100644 --- a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rule/builder/EncryptRuleBuilder.java +++ b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rule/builder/EncryptRuleBuilder.java @@ -20,6 +20,7 @@ import org.apache.shardingsphere.encrypt.api.config.EncryptRuleConfiguration; import org.apache.shardingsphere.encrypt.constant.EncryptOrder; import org.apache.shardingsphere.encrypt.rule.EncryptRule; +import org.apache.shardingsphere.infra.database.core.type.DatabaseType; import org.apache.shardingsphere.infra.instance.InstanceContext; import org.apache.shardingsphere.infra.rule.ShardingSphereRule; import org.apache.shardingsphere.infra.rule.builder.database.DatabaseRuleBuilder; @@ -34,7 +35,7 @@ public final class EncryptRuleBuilder implements DatabaseRuleBuilder { @Override - public EncryptRule build(final EncryptRuleConfiguration config, final String databaseName, + public EncryptRule build(final EncryptRuleConfiguration config, final String databaseName, final DatabaseType protocolType, final Map dataSources, final Collection builtRules, final InstanceContext instanceContext) { return new EncryptRule(databaseName, config); } diff --git a/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/rule/builder/EncryptRuleBuilderTest.java b/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/rule/builder/EncryptRuleBuilderTest.java index e002840e9a402..ad27b325c8b97 100644 --- a/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/rule/builder/EncryptRuleBuilderTest.java +++ b/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/rule/builder/EncryptRuleBuilderTest.java @@ -22,6 +22,7 @@ import org.apache.shardingsphere.infra.instance.InstanceContext; import org.apache.shardingsphere.infra.rule.builder.database.DatabaseRuleBuilder; import org.apache.shardingsphere.infra.spi.type.ordered.OrderedSPILoader; +import org.apache.shardingsphere.test.fixture.database.MockedDatabaseType; import org.junit.jupiter.api.Test; import java.util.Collections; @@ -37,6 +38,6 @@ class EncryptRuleBuilderTest { void assertBuild() { EncryptRuleConfiguration ruleConfig = mock(EncryptRuleConfiguration.class); DatabaseRuleBuilder builder = OrderedSPILoader.getServices(DatabaseRuleBuilder.class, Collections.singleton(ruleConfig)).get(ruleConfig); - assertThat(builder.build(ruleConfig, "", Collections.emptyMap(), Collections.emptyList(), mock(InstanceContext.class)), instanceOf(EncryptRule.class)); + assertThat(builder.build(ruleConfig, "", new MockedDatabaseType(), Collections.emptyMap(), Collections.emptyList(), mock(InstanceContext.class)), instanceOf(EncryptRule.class)); } } diff --git a/features/mask/core/src/main/java/org/apache/shardingsphere/mask/rule/builder/MaskRuleBuilder.java b/features/mask/core/src/main/java/org/apache/shardingsphere/mask/rule/builder/MaskRuleBuilder.java index afa30544624d5..6b5c00d9b288b 100644 --- a/features/mask/core/src/main/java/org/apache/shardingsphere/mask/rule/builder/MaskRuleBuilder.java +++ b/features/mask/core/src/main/java/org/apache/shardingsphere/mask/rule/builder/MaskRuleBuilder.java @@ -17,6 +17,7 @@ package org.apache.shardingsphere.mask.rule.builder; +import org.apache.shardingsphere.infra.database.core.type.DatabaseType; import org.apache.shardingsphere.infra.instance.InstanceContext; import org.apache.shardingsphere.infra.rule.ShardingSphereRule; import org.apache.shardingsphere.infra.rule.builder.database.DatabaseRuleBuilder; @@ -34,7 +35,7 @@ public final class MaskRuleBuilder implements DatabaseRuleBuilder { @Override - public MaskRule build(final MaskRuleConfiguration config, final String databaseName, + public MaskRule build(final MaskRuleConfiguration config, final String databaseName, final DatabaseType protocolType, final Map dataSources, final Collection builtRules, final InstanceContext instanceContext) { return new MaskRule(config); } diff --git a/features/mask/core/src/test/java/org/apache/shardingsphere/mask/rule/builder/MaskRuleBuilderTest.java b/features/mask/core/src/test/java/org/apache/shardingsphere/mask/rule/builder/MaskRuleBuilderTest.java index efb3efc7c36af..997e3b46024c6 100644 --- a/features/mask/core/src/test/java/org/apache/shardingsphere/mask/rule/builder/MaskRuleBuilderTest.java +++ b/features/mask/core/src/test/java/org/apache/shardingsphere/mask/rule/builder/MaskRuleBuilderTest.java @@ -17,6 +17,7 @@ package org.apache.shardingsphere.mask.rule.builder; +import org.apache.shardingsphere.infra.database.mysql.type.MySQLDatabaseType; import org.apache.shardingsphere.infra.instance.InstanceContext; import org.apache.shardingsphere.infra.rule.builder.database.DatabaseRuleBuilder; import org.apache.shardingsphere.infra.spi.type.ordered.OrderedSPILoader; @@ -37,6 +38,6 @@ class MaskRuleBuilderTest { void assertBuild() { MaskRuleConfiguration ruleConfig = mock(MaskRuleConfiguration.class); DatabaseRuleBuilder builder = OrderedSPILoader.getServices(DatabaseRuleBuilder.class, Collections.singleton(ruleConfig)).get(ruleConfig); - assertThat(builder.build(ruleConfig, "", Collections.emptyMap(), Collections.emptyList(), mock(InstanceContext.class)), instanceOf(MaskRule.class)); + assertThat(builder.build(ruleConfig, "", new MySQLDatabaseType(), Collections.emptyMap(), Collections.emptyList(), mock(InstanceContext.class)), instanceOf(MaskRule.class)); } } diff --git a/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/rule/builder/ReadwriteSplittingRuleBuilder.java b/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/rule/builder/ReadwriteSplittingRuleBuilder.java index ba9ebde69890a..be4c696272f75 100644 --- a/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/rule/builder/ReadwriteSplittingRuleBuilder.java +++ b/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/rule/builder/ReadwriteSplittingRuleBuilder.java @@ -17,6 +17,7 @@ package org.apache.shardingsphere.readwritesplitting.rule.builder; +import org.apache.shardingsphere.infra.database.core.type.DatabaseType; import org.apache.shardingsphere.infra.instance.InstanceContext; import org.apache.shardingsphere.infra.rule.ShardingSphereRule; import org.apache.shardingsphere.infra.rule.builder.database.DatabaseRuleBuilder; @@ -34,7 +35,7 @@ public final class ReadwriteSplittingRuleBuilder implements DatabaseRuleBuilder { @Override - public ReadwriteSplittingRule build(final ReadwriteSplittingRuleConfiguration config, final String databaseName, + public ReadwriteSplittingRule build(final ReadwriteSplittingRuleConfiguration config, final String databaseName, final DatabaseType protocolType, final Map dataSources, final Collection builtRules, final InstanceContext instanceContext) { return new ReadwriteSplittingRule(databaseName, config, instanceContext); } diff --git a/features/readwrite-splitting/core/src/test/java/org/apache/shardingsphere/readwritesplitting/rule/builder/ReadwriteSplittingRuleBuilderTest.java b/features/readwrite-splitting/core/src/test/java/org/apache/shardingsphere/readwritesplitting/rule/builder/ReadwriteSplittingRuleBuilderTest.java index 8aa3af15ada46..e0b79221ec6d5 100644 --- a/features/readwrite-splitting/core/src/test/java/org/apache/shardingsphere/readwritesplitting/rule/builder/ReadwriteSplittingRuleBuilderTest.java +++ b/features/readwrite-splitting/core/src/test/java/org/apache/shardingsphere/readwritesplitting/rule/builder/ReadwriteSplittingRuleBuilderTest.java @@ -23,6 +23,7 @@ import org.apache.shardingsphere.readwritesplitting.api.ReadwriteSplittingRuleConfiguration; import org.apache.shardingsphere.readwritesplitting.api.rule.ReadwriteSplittingDataSourceRuleConfiguration; import org.apache.shardingsphere.readwritesplitting.rule.ReadwriteSplittingRule; +import org.apache.shardingsphere.test.fixture.database.MockedDatabaseType; import org.junit.jupiter.api.Test; import java.util.Collections; @@ -39,6 +40,6 @@ void assertBuild() { ReadwriteSplittingRuleConfiguration ruleConfig = new ReadwriteSplittingRuleConfiguration(Collections.singleton( new ReadwriteSplittingDataSourceRuleConfiguration("name", "writeDataSourceName", Collections.singletonList("readDataSourceName"), "loadBalancerName")), Collections.emptyMap()); DatabaseRuleBuilder builder = OrderedSPILoader.getServices(DatabaseRuleBuilder.class, Collections.singleton(ruleConfig)).get(ruleConfig); - assertThat(builder.build(ruleConfig, "", Collections.emptyMap(), Collections.emptyList(), mock(InstanceContext.class)), instanceOf(ReadwriteSplittingRule.class)); + assertThat(builder.build(ruleConfig, "", new MockedDatabaseType(), Collections.emptyMap(), Collections.emptyList(), mock(InstanceContext.class)), instanceOf(ReadwriteSplittingRule.class)); } } diff --git a/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/rule/builder/ShadowRuleBuilder.java b/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/rule/builder/ShadowRuleBuilder.java index 7735cbf76faa1..e68462d69efe2 100644 --- a/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/rule/builder/ShadowRuleBuilder.java +++ b/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/rule/builder/ShadowRuleBuilder.java @@ -17,6 +17,7 @@ package org.apache.shardingsphere.shadow.rule.builder; +import org.apache.shardingsphere.infra.database.core.type.DatabaseType; import org.apache.shardingsphere.infra.instance.InstanceContext; import org.apache.shardingsphere.infra.rule.ShardingSphereRule; import org.apache.shardingsphere.infra.rule.builder.database.DatabaseRuleBuilder; @@ -34,7 +35,7 @@ public final class ShadowRuleBuilder implements DatabaseRuleBuilder { @Override - public ShadowRule build(final ShadowRuleConfiguration config, final String databaseName, + public ShadowRule build(final ShadowRuleConfiguration config, final String databaseName, final DatabaseType protocolType, final Map dataSources, final Collection builtRules, final InstanceContext instanceContext) { return new ShadowRule(config); } diff --git a/features/shadow/core/src/test/java/org/apache/shardingsphere/shadow/rule/builder/ShadowRuleBuilderTest.java b/features/shadow/core/src/test/java/org/apache/shardingsphere/shadow/rule/builder/ShadowRuleBuilderTest.java index 0f424f71d07d7..6a58968a095e3 100644 --- a/features/shadow/core/src/test/java/org/apache/shardingsphere/shadow/rule/builder/ShadowRuleBuilderTest.java +++ b/features/shadow/core/src/test/java/org/apache/shardingsphere/shadow/rule/builder/ShadowRuleBuilderTest.java @@ -17,6 +17,7 @@ package org.apache.shardingsphere.shadow.rule.builder; +import org.apache.shardingsphere.infra.database.mysql.type.MySQLDatabaseType; import org.apache.shardingsphere.infra.instance.InstanceContext; import org.apache.shardingsphere.infra.rule.builder.database.DatabaseRuleBuilder; import org.apache.shardingsphere.infra.spi.type.ordered.OrderedSPILoader; @@ -37,6 +38,6 @@ class ShadowRuleBuilderTest { void assertBuild() { ShadowRuleConfiguration ruleConfig = new ShadowRuleConfiguration(); DatabaseRuleBuilder builder = OrderedSPILoader.getServices(DatabaseRuleBuilder.class, Collections.singleton(ruleConfig)).get(ruleConfig); - assertThat(builder.build(ruleConfig, "", Collections.emptyMap(), Collections.emptyList(), mock(InstanceContext.class)), instanceOf(ShadowRule.class)); + assertThat(builder.build(ruleConfig, "", new MySQLDatabaseType(), Collections.emptyMap(), Collections.emptyList(), mock(InstanceContext.class)), instanceOf(ShadowRule.class)); } } diff --git a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/rule/builder/ShardingRuleBuilder.java b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/rule/builder/ShardingRuleBuilder.java index d0ea8dab04fe4..a239e23b194ef 100644 --- a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/rule/builder/ShardingRuleBuilder.java +++ b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/rule/builder/ShardingRuleBuilder.java @@ -17,10 +17,11 @@ package org.apache.shardingsphere.sharding.rule.builder; +import org.apache.shardingsphere.infra.database.core.type.DatabaseType; +import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions; import org.apache.shardingsphere.infra.instance.InstanceContext; import org.apache.shardingsphere.infra.rule.ShardingSphereRule; import org.apache.shardingsphere.infra.rule.builder.database.DatabaseRuleBuilder; -import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions; import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration; import org.apache.shardingsphere.sharding.constant.ShardingOrder; import org.apache.shardingsphere.sharding.exception.metadata.MissingRequiredShardingConfigurationException; @@ -36,7 +37,7 @@ public final class ShardingRuleBuilder implements DatabaseRuleBuilder { @Override - public ShardingRule build(final ShardingRuleConfiguration config, final String databaseName, + public ShardingRule build(final ShardingRuleConfiguration config, final String databaseName, final DatabaseType protocolType, final Map dataSources, final Collection builtRules, final InstanceContext instanceContext) { ShardingSpherePreconditions.checkState(null != dataSources && !dataSources.isEmpty(), () -> new MissingRequiredShardingConfigurationException("Data source", databaseName)); return new ShardingRule(config, dataSources.keySet(), instanceContext); diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/fixture/ShardingRoutingEngineFixtureBuilder.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/fixture/ShardingRoutingEngineFixtureBuilder.java index c07736b4aab3e..78baf5241e342 100644 --- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/fixture/ShardingRoutingEngineFixtureBuilder.java +++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/fixture/ShardingRoutingEngineFixtureBuilder.java @@ -23,6 +23,7 @@ import org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext; import org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration; import org.apache.shardingsphere.infra.database.core.DefaultDatabase; +import org.apache.shardingsphere.infra.database.h2.type.H2DatabaseType; import org.apache.shardingsphere.infra.instance.InstanceContext; import org.apache.shardingsphere.infra.rule.ShardingSphereRule; import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration; @@ -291,7 +292,7 @@ private static Collection createDataSourceNames() { */ public static SingleRule createSingleRule(final Collection rules) { Map dataSourceMap = createDataSourceMap(); - SingleRule result = new SingleRule(new SingleRuleConfiguration(), DefaultDatabase.LOGIC_NAME, dataSourceMap, rules); + SingleRule result = new SingleRule(new SingleRuleConfiguration(), DefaultDatabase.LOGIC_NAME, new H2DatabaseType(), dataSourceMap, rules); result.put(dataSourceMap.keySet().iterator().next(), DefaultDatabase.LOGIC_NAME, "t_category"); return result; } diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/rule/builder/ShardingRuleBuilderTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/rule/builder/ShardingRuleBuilderTest.java index 37e7b5cfd9d3d..e1c80628f160d 100644 --- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/rule/builder/ShardingRuleBuilderTest.java +++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/rule/builder/ShardingRuleBuilderTest.java @@ -17,6 +17,7 @@ package org.apache.shardingsphere.sharding.rule.builder; +import org.apache.shardingsphere.infra.database.mysql.type.MySQLDatabaseType; import org.apache.shardingsphere.infra.instance.InstanceContext; import org.apache.shardingsphere.infra.rule.builder.database.DatabaseRuleBuilder; import org.apache.shardingsphere.infra.spi.type.ordered.OrderedSPILoader; @@ -51,7 +52,7 @@ void setUp() { @SuppressWarnings("unchecked") @Test void assertBuild() { - assertThat(builder.build(ruleConfig, "sharding_db", + assertThat(builder.build(ruleConfig, "sharding_db", new MySQLDatabaseType(), Collections.singletonMap("name", mock(DataSource.class, RETURNS_DEEP_STUBS)), Collections.emptyList(), mock(InstanceContext.class)), instanceOf(ShardingRule.class)); } @@ -59,13 +60,13 @@ void assertBuild() { @Test void assertBuildWithNullDataSourceMap() { assertThrows(MissingRequiredShardingConfigurationException.class, - () -> builder.build(ruleConfig, "sharding_db", null, Collections.emptyList(), mock(InstanceContext.class))); + () -> builder.build(ruleConfig, "sharding_db", new MySQLDatabaseType(), null, Collections.emptyList(), mock(InstanceContext.class))); } @SuppressWarnings("unchecked") @Test void assertBuildWithEmptyDataSourceMap() { assertThrows(MissingRequiredShardingConfigurationException.class, - () -> builder.build(ruleConfig, "sharding_db", Collections.emptyMap(), Collections.emptyList(), mock(InstanceContext.class))); + () -> builder.build(ruleConfig, "sharding_db", new MySQLDatabaseType(), Collections.emptyMap(), Collections.emptyList(), mock(InstanceContext.class))); } } diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/ShardingSphereDatabase.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/ShardingSphereDatabase.java index 88a63a1861a97..afde6bc5e4905 100644 --- a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/ShardingSphereDatabase.java +++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/ShardingSphereDatabase.java @@ -88,7 +88,7 @@ public ShardingSphereDatabase(final String name, final DatabaseType protocolType */ public static ShardingSphereDatabase create(final String name, final DatabaseType protocolType, final Map storageTypes, final DatabaseConfiguration databaseConfig, final ConfigurationProperties props, final InstanceContext instanceContext) throws SQLException { - Collection databaseRules = DatabaseRulesBuilder.build(name, databaseConfig, instanceContext); + Collection databaseRules = DatabaseRulesBuilder.build(name, protocolType, databaseConfig, instanceContext); Map schemas = new ConcurrentHashMap<>(GenericSchemaBuilder .build(new GenericSchemaBuilderMaterial(protocolType, storageTypes, DataSourceStateManager.getInstance().getEnabledDataSources(name, databaseConfig), databaseRules, props, new DatabaseTypeRegistry(protocolType).getDefaultSchemaName(name)))); diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/rule/builder/database/DatabaseRuleBuilder.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/rule/builder/database/DatabaseRuleBuilder.java index 1e3ab5be6ec39..26feb2688e2a9 100644 --- a/infra/common/src/main/java/org/apache/shardingsphere/infra/rule/builder/database/DatabaseRuleBuilder.java +++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/rule/builder/database/DatabaseRuleBuilder.java @@ -18,6 +18,7 @@ package org.apache.shardingsphere.infra.rule.builder.database; import org.apache.shardingsphere.infra.config.rule.RuleConfiguration; +import org.apache.shardingsphere.infra.database.core.type.DatabaseType; import org.apache.shardingsphere.infra.instance.InstanceContext; import org.apache.shardingsphere.infra.rule.ShardingSphereRule; import org.apache.shardingsphere.infra.rule.builder.RuleBuilder; @@ -41,10 +42,11 @@ public interface DatabaseRuleBuilder extends RuleBu * * @param config rule configuration * @param databaseName database name + * @param protocolType protocol type * @param dataSources data sources * @param builtRules built rules * @param instanceContext instance context * @return built database rule */ - DatabaseRule build(T config, String databaseName, Map dataSources, Collection builtRules, InstanceContext instanceContext); + DatabaseRule build(T config, String databaseName, DatabaseType protocolType, Map dataSources, Collection builtRules, InstanceContext instanceContext); } diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/rule/builder/database/DatabaseRulesBuilder.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/rule/builder/database/DatabaseRulesBuilder.java index 18c8ca2dd4fe1..e1dce396d8ad4 100644 --- a/infra/common/src/main/java/org/apache/shardingsphere/infra/rule/builder/database/DatabaseRulesBuilder.java +++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/rule/builder/database/DatabaseRulesBuilder.java @@ -24,6 +24,7 @@ import org.apache.shardingsphere.infra.config.rule.checker.RuleConfigurationChecker; import org.apache.shardingsphere.infra.config.rule.function.DistributedRuleConfiguration; import org.apache.shardingsphere.infra.config.rule.function.EnhancedRuleConfiguration; +import org.apache.shardingsphere.infra.database.core.type.DatabaseType; import org.apache.shardingsphere.infra.instance.InstanceContext; import org.apache.shardingsphere.infra.rule.ShardingSphereRule; import org.apache.shardingsphere.infra.spi.type.ordered.OrderedSPILoader; @@ -49,12 +50,13 @@ public final class DatabaseRulesBuilder { * Build database rules. * * @param databaseName database name + * @param protocolType protocol type * @param databaseConfig database configuration * @param instanceContext instance context * @return built rules */ @SuppressWarnings({"unchecked", "rawtypes"}) - public static Collection build(final String databaseName, final DatabaseConfiguration databaseConfig, final InstanceContext instanceContext) { + public static Collection build(final String databaseName, final DatabaseType protocolType, final DatabaseConfiguration databaseConfig, final InstanceContext instanceContext) { Collection result = new LinkedList<>(); for (Entry entry : getRuleBuilderMap(databaseConfig).entrySet()) { RuleConfigurationChecker configChecker = OrderedSPILoader.getServicesByClass( @@ -63,7 +65,7 @@ public static Collection build(final String databaseName, fi if (null != configChecker) { configChecker.check(databaseName, entry.getKey(), dataSources, result); } - result.add(entry.getValue().build(entry.getKey(), databaseName, dataSources, result, instanceContext)); + result.add(entry.getValue().build(entry.getKey(), databaseName, protocolType, dataSources, result, instanceContext)); } return result; } @@ -72,6 +74,7 @@ public static Collection build(final String databaseName, fi * Build database rules. * * @param databaseName database name + * @param protocolType protocol type * @param dataSources data sources * @param rules rules * @param ruleConfig rule configuration @@ -79,8 +82,8 @@ public static Collection build(final String databaseName, fi * @return built rules */ @SuppressWarnings({"unchecked", "rawtypes"}) - public static Collection build(final String databaseName, final Map dataSources, final Collection rules, - final RuleConfiguration ruleConfig, final InstanceContext instanceContext) { + public static Collection build(final String databaseName, final DatabaseType protocolType, final Map dataSources, + final Collection rules, final RuleConfiguration ruleConfig, final InstanceContext instanceContext) { Collection result = new LinkedList<>(); for (Entry entry : OrderedSPILoader.getServices(DatabaseRuleBuilder.class, Collections.singletonList(ruleConfig), Comparator.reverseOrder()).entrySet()) { @@ -89,7 +92,7 @@ public static Collection build(final String databaseName, fi if (null != configChecker) { configChecker.check(databaseName, entry.getKey(), dataSources, rules); } - result.add(entry.getValue().build(entry.getKey(), databaseName, dataSources, rules, instanceContext)); + result.add(entry.getValue().build(entry.getKey(), databaseName, protocolType, dataSources, rules, instanceContext)); } return result; } diff --git a/infra/common/src/test/java/org/apache/shardingsphere/infra/rule/builder/database/DatabaseRulesBuilderTest.java b/infra/common/src/test/java/org/apache/shardingsphere/infra/rule/builder/database/DatabaseRulesBuilderTest.java index 39d501814807b..96e03c96e2a7a 100644 --- a/infra/common/src/test/java/org/apache/shardingsphere/infra/rule/builder/database/DatabaseRulesBuilderTest.java +++ b/infra/common/src/test/java/org/apache/shardingsphere/infra/rule/builder/database/DatabaseRulesBuilderTest.java @@ -18,6 +18,7 @@ package org.apache.shardingsphere.infra.rule.builder.database; import org.apache.shardingsphere.infra.config.database.impl.DataSourceProvidedDatabaseConfiguration; +import org.apache.shardingsphere.infra.database.mysql.type.MySQLDatabaseType; import org.apache.shardingsphere.infra.fixture.FixtureRule; import org.apache.shardingsphere.infra.fixture.FixtureRuleConfiguration; import org.apache.shardingsphere.infra.instance.InstanceContext; @@ -36,8 +37,8 @@ class DatabaseRulesBuilderTest { @Test void assertBuild() { - Iterator actual = DatabaseRulesBuilder.build( - "foo_db", new DataSourceProvidedDatabaseConfiguration(Collections.emptyMap(), Collections.singleton(new FixtureRuleConfiguration())), mock(InstanceContext.class)).iterator(); + Iterator actual = DatabaseRulesBuilder.build("foo_db", new MySQLDatabaseType(), + new DataSourceProvidedDatabaseConfiguration(Collections.emptyMap(), Collections.singleton(new FixtureRuleConfiguration())), mock(InstanceContext.class)).iterator(); assertThat(actual.next(), instanceOf(FixtureRule.class)); assertFalse(actual.hasNext()); } diff --git a/infra/common/src/test/java/org/apache/shardingsphere/infra/rule/builder/fixture/FixtureDatabaseRuleBuilder.java b/infra/common/src/test/java/org/apache/shardingsphere/infra/rule/builder/fixture/FixtureDatabaseRuleBuilder.java index 5ba9d3fad2d70..c93695deeef0b 100644 --- a/infra/common/src/test/java/org/apache/shardingsphere/infra/rule/builder/fixture/FixtureDatabaseRuleBuilder.java +++ b/infra/common/src/test/java/org/apache/shardingsphere/infra/rule/builder/fixture/FixtureDatabaseRuleBuilder.java @@ -17,6 +17,7 @@ package org.apache.shardingsphere.infra.rule.builder.fixture; +import org.apache.shardingsphere.infra.database.core.type.DatabaseType; import org.apache.shardingsphere.infra.fixture.FixtureRule; import org.apache.shardingsphere.infra.instance.InstanceContext; import org.apache.shardingsphere.infra.rule.ShardingSphereRule; @@ -29,7 +30,7 @@ public final class FixtureDatabaseRuleBuilder implements DatabaseRuleBuilder { @Override - public FixtureRule build(final FixtureDatabaseRuleConfiguration config, final String databaseName, + public FixtureRule build(final FixtureDatabaseRuleConfiguration config, final String databaseName, final DatabaseType protocolType, final Map dataSources, final Collection builtRules, final InstanceContext instanceContext) { return new FixtureRule(); } diff --git a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/factory/InternalMetaDataFactory.java b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/factory/InternalMetaDataFactory.java index 58a8f3e780e2c..45cc2e6b93362 100644 --- a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/factory/InternalMetaDataFactory.java +++ b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/factory/InternalMetaDataFactory.java @@ -21,8 +21,8 @@ import lombok.NoArgsConstructor; import org.apache.shardingsphere.infra.config.database.DatabaseConfiguration; import org.apache.shardingsphere.infra.config.props.ConfigurationProperties; -import org.apache.shardingsphere.infra.database.core.type.DatabaseType; import org.apache.shardingsphere.infra.database.DatabaseTypeEngine; +import org.apache.shardingsphere.infra.database.core.type.DatabaseType; import org.apache.shardingsphere.infra.instance.InstanceContext; import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; import org.apache.shardingsphere.infra.rule.builder.database.DatabaseRulesBuilder; @@ -50,8 +50,9 @@ public final class InternalMetaDataFactory { */ public static ShardingSphereDatabase create(final String databaseName, final MetaDataBasedPersistService persistService, final DatabaseConfiguration databaseConfig, final ConfigurationProperties props, final InstanceContext instanceContext) { - return ShardingSphereDatabase.create(databaseName, DatabaseTypeEngine.getProtocolType(databaseName, databaseConfig, props), databaseConfig, - DatabaseRulesBuilder.build(databaseName, databaseConfig, instanceContext), persistService.getDatabaseMetaDataService().loadSchemas(databaseName)); + DatabaseType protocolType = DatabaseTypeEngine.getProtocolType(databaseName, databaseConfig, props); + return ShardingSphereDatabase.create(databaseName, protocolType, databaseConfig, DatabaseRulesBuilder.build(databaseName, protocolType, databaseConfig, instanceContext), + persistService.getDatabaseMetaDataService().loadSchemas(databaseName)); } /** diff --git a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/factory/NewInternalMetaDataFactory.java b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/factory/NewInternalMetaDataFactory.java index 34c43929adc69..f64a9ae9e669f 100644 --- a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/factory/NewInternalMetaDataFactory.java +++ b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/factory/NewInternalMetaDataFactory.java @@ -21,8 +21,8 @@ import lombok.NoArgsConstructor; import org.apache.shardingsphere.infra.config.database.DatabaseConfiguration; import org.apache.shardingsphere.infra.config.props.ConfigurationProperties; -import org.apache.shardingsphere.infra.database.core.type.DatabaseType; import org.apache.shardingsphere.infra.database.DatabaseTypeEngine; +import org.apache.shardingsphere.infra.database.core.type.DatabaseType; import org.apache.shardingsphere.infra.instance.InstanceContext; import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; import org.apache.shardingsphere.infra.rule.builder.database.DatabaseRulesBuilder; @@ -51,8 +51,9 @@ public final class NewInternalMetaDataFactory { */ public static ShardingSphereDatabase create(final String databaseName, final NewMetaDataPersistService persistService, final DatabaseConfiguration databaseConfig, final ConfigurationProperties props, final InstanceContext instanceContext) { - return ShardingSphereDatabase.create(databaseName, DatabaseTypeEngine.getProtocolType(databaseName, databaseConfig, props), databaseConfig, - DatabaseRulesBuilder.build(databaseName, databaseConfig, instanceContext), persistService.getDatabaseMetaDataService().loadSchemas(databaseName)); + DatabaseType protocolType = DatabaseTypeEngine.getProtocolType(databaseName, databaseConfig, props); + return ShardingSphereDatabase.create(databaseName, protocolType, databaseConfig, DatabaseRulesBuilder.build(databaseName, protocolType, databaseConfig, instanceContext), + persistService.getDatabaseMetaDataService().loadSchemas(databaseName)); } /** diff --git a/kernel/single/core/src/main/java/org/apache/shardingsphere/single/datanode/SingleTableDataNodeLoader.java b/kernel/single/core/src/main/java/org/apache/shardingsphere/single/datanode/SingleTableDataNodeLoader.java index bb5d3ac96c5bc..2265e0bf91c4c 100644 --- a/kernel/single/core/src/main/java/org/apache/shardingsphere/single/datanode/SingleTableDataNodeLoader.java +++ b/kernel/single/core/src/main/java/org/apache/shardingsphere/single/datanode/SingleTableDataNodeLoader.java @@ -19,6 +19,7 @@ import lombok.AccessLevel; import lombok.NoArgsConstructor; +import org.apache.shardingsphere.infra.database.DatabaseTypeEngine; import org.apache.shardingsphere.infra.database.core.type.DatabaseType; import org.apache.shardingsphere.infra.datanode.DataNode; import org.apache.shardingsphere.infra.database.core.metadata.data.loader.type.SchemaMetaDataLoader; @@ -48,25 +49,25 @@ public final class SingleTableDataNodeLoader { * Load single table data nodes. * * @param databaseName database name - * @param databaseType database type + * @param protocolType protocol type * @param dataSourceMap data source map * @param builtRules built rules * @param configuredTables configured tables * @return single table data node map */ - public static Map> load(final String databaseName, final DatabaseType databaseType, final Map dataSourceMap, + public static Map> load(final String databaseName, final DatabaseType protocolType, final Map dataSourceMap, final Collection builtRules, final Collection configuredTables) { Collection featureRequiredSingleTables = SingleTableLoadUtils.getFeatureRequiredSingleTables(builtRules); if (configuredTables.isEmpty() && featureRequiredSingleTables.isEmpty()) { return new LinkedHashMap<>(); } Collection excludedTables = SingleTableLoadUtils.getExcludedTables(builtRules); - Map> actualDataNodes = load(databaseName, databaseType, dataSourceMap, excludedTables); + Map> actualDataNodes = load(databaseName, dataSourceMap, excludedTables); Collection splitTables = SingleTableLoadUtils.splitTableLines(configuredTables); if (splitTables.contains(SingleTableConstants.ALL_TABLES) || splitTables.contains(SingleTableConstants.ALL_SCHEMA_TABLES)) { return actualDataNodes; } - Map>> configuredTableMap = getConfiguredTableMap(databaseName, databaseType, splitTables); + Map>> configuredTableMap = getConfiguredTableMap(databaseName, protocolType, splitTables); return loadSpecifiedDataNodes(actualDataNodes, featureRequiredSingleTables, configuredTableMap); } @@ -74,16 +75,14 @@ public static Map> load(final String databaseName, * Load single table data nodes. * * @param databaseName database name - * @param databaseType database type * @param dataSourceMap data source map * @param excludedTables excluded tables * @return single table data node map */ - public static Map> load(final String databaseName, final DatabaseType databaseType, - final Map dataSourceMap, final Collection excludedTables) { + public static Map> load(final String databaseName, final Map dataSourceMap, final Collection excludedTables) { Map> result = new ConcurrentHashMap<>(); for (Entry entry : dataSourceMap.entrySet()) { - Map> dataNodeMap = load(databaseName, databaseType, entry.getKey(), entry.getValue(), excludedTables); + Map> dataNodeMap = load(databaseName, DatabaseTypeEngine.getStorageType(entry.getValue()), entry.getKey(), entry.getValue(), excludedTables); for (Entry> each : dataNodeMap.entrySet()) { Collection addedDataNodes = each.getValue(); Collection existDataNodes = result.getOrDefault(each.getKey().toLowerCase(), new LinkedHashSet<>(addedDataNodes.size(), 1F)); @@ -94,9 +93,9 @@ public static Map> load(final String databaseName, return result; } - private static Map> load(final String databaseName, final DatabaseType databaseType, final String dataSourceName, + private static Map> load(final String databaseName, final DatabaseType storageType, final String dataSourceName, final DataSource dataSource, final Collection excludedTables) { - Map> schemaTableNames = loadSchemaTableNames(databaseName, databaseType, dataSource, dataSourceName); + Map> schemaTableNames = loadSchemaTableNames(databaseName, storageType, dataSource, dataSourceName); Map> result = new LinkedHashMap<>(); for (Entry> entry : schemaTableNames.entrySet()) { for (String each : entry.getValue()) { @@ -155,12 +154,12 @@ private static Collection getSingleDataNodeCollection(final DataNode d return result; } - private static Map>> getConfiguredTableMap(final String databaseName, final DatabaseType databaseType, final Collection configuredTables) { + private static Map>> getConfiguredTableMap(final String databaseName, final DatabaseType protocolType, final Collection configuredTables) { if (configuredTables.isEmpty()) { return Collections.emptyMap(); } Map>> result = new LinkedHashMap<>(); - Collection dataNodes = SingleTableLoadUtils.convertToDataNodes(databaseName, databaseType, configuredTables); + Collection dataNodes = SingleTableLoadUtils.convertToDataNodes(databaseName, protocolType, configuredTables); for (DataNode each : dataNodes) { Map> schemaTables = result.getOrDefault(each.getDataSourceName(), new LinkedHashMap<>()); Collection tables = schemaTables.getOrDefault(each.getSchemaName(), new LinkedList<>()); @@ -175,15 +174,15 @@ private static Map>> getConfiguredTableMa * Load schema table names. * * @param databaseName database name - * @param databaseType database type + * @param storageType storage type * @param dataSource data source * @param dataSourceName data source name * @return schema table names * @throws SingleTablesLoadingException Single tables loading exception */ - public static Map> loadSchemaTableNames(final String databaseName, final DatabaseType databaseType, final DataSource dataSource, final String dataSourceName) { + public static Map> loadSchemaTableNames(final String databaseName, final DatabaseType storageType, final DataSource dataSource, final String dataSourceName) { try { - return SchemaMetaDataLoader.loadSchemaTableNames(databaseName, databaseType, dataSource); + return SchemaMetaDataLoader.loadSchemaTableNames(databaseName, storageType, dataSource); } catch (final SQLException ex) { throw new SingleTablesLoadingException(databaseName, dataSourceName, ex); } diff --git a/kernel/single/core/src/main/java/org/apache/shardingsphere/single/decorator/SingleRuleConfigurationDecorator.java b/kernel/single/core/src/main/java/org/apache/shardingsphere/single/decorator/SingleRuleConfigurationDecorator.java index 3f427d2d94bea..f7a9fb05a9971 100644 --- a/kernel/single/core/src/main/java/org/apache/shardingsphere/single/decorator/SingleRuleConfigurationDecorator.java +++ b/kernel/single/core/src/main/java/org/apache/shardingsphere/single/decorator/SingleRuleConfigurationDecorator.java @@ -22,9 +22,9 @@ import org.apache.shardingsphere.infra.database.core.type.DatabaseType; import org.apache.shardingsphere.infra.database.core.type.DatabaseTypeRegistry; import org.apache.shardingsphere.infra.datanode.DataNode; -import org.apache.shardingsphere.infra.state.datasource.DataSourceStateManager; import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions; import org.apache.shardingsphere.infra.rule.ShardingSphereRule; +import org.apache.shardingsphere.infra.state.datasource.DataSourceStateManager; import org.apache.shardingsphere.single.api.config.SingleRuleConfiguration; import org.apache.shardingsphere.single.api.constant.SingleTableConstants; import org.apache.shardingsphere.single.datanode.SingleTableDataNodeLoader; @@ -65,7 +65,7 @@ private Collection decorateTables(final String databaseName, final Map aggregatedDataSources = SingleTableLoadUtils.getAggregatedDataSourceMap(enabledDataSources, builtRules); DatabaseType databaseType = enabledDataSources.isEmpty() ? DatabaseTypeEngine.getDefaultStorageType() : DatabaseTypeEngine.getStorageType(enabledDataSources.values().iterator().next()); Collection excludedTables = SingleTableLoadUtils.getExcludedTables(builtRules); - Map> actualDataNodes = SingleTableDataNodeLoader.load(databaseName, databaseType, aggregatedDataSources, excludedTables); + Map> actualDataNodes = SingleTableDataNodeLoader.load(databaseName, aggregatedDataSources, excludedTables); Collection configuredDataNodes = SingleTableLoadUtils.convertToDataNodes(databaseName, databaseType, splitTables); checkRuleConfiguration(databaseName, aggregatedDataSources, excludedTables, configuredDataNodes); boolean isSchemaSupportedDatabaseType = new DatabaseTypeRegistry(databaseType).getDialectDatabaseMetaData().getDefaultSchema().isPresent(); diff --git a/kernel/single/core/src/main/java/org/apache/shardingsphere/single/rule/SingleRule.java b/kernel/single/core/src/main/java/org/apache/shardingsphere/single/rule/SingleRule.java index 871e2ce917274..443884a30f5a1 100644 --- a/kernel/single/core/src/main/java/org/apache/shardingsphere/single/rule/SingleRule.java +++ b/kernel/single/core/src/main/java/org/apache/shardingsphere/single/rule/SingleRule.java @@ -21,11 +21,9 @@ import org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext; import org.apache.shardingsphere.infra.binder.context.type.IndexAvailable; import org.apache.shardingsphere.infra.config.rule.RuleConfiguration; -import org.apache.shardingsphere.infra.database.DatabaseTypeEngine; import org.apache.shardingsphere.infra.database.core.type.DatabaseType; import org.apache.shardingsphere.infra.database.core.type.DatabaseTypeRegistry; import org.apache.shardingsphere.infra.datanode.DataNode; -import org.apache.shardingsphere.infra.state.datasource.DataSourceStateManager; import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; import org.apache.shardingsphere.infra.metadata.database.schema.QualifiedTable; import org.apache.shardingsphere.infra.metadata.database.schema.util.IndexMetaDataUtils; @@ -37,6 +35,7 @@ import org.apache.shardingsphere.infra.rule.identifier.type.TableNamesMapper; import org.apache.shardingsphere.infra.rule.identifier.type.exportable.ExportableRule; import org.apache.shardingsphere.infra.rule.identifier.type.exportable.constant.ExportableConstants; +import org.apache.shardingsphere.infra.state.datasource.DataSourceStateManager; import org.apache.shardingsphere.single.api.config.SingleRuleConfiguration; import org.apache.shardingsphere.single.datanode.SingleTableDataNodeLoader; import org.apache.shardingsphere.single.util.SingleTableLoadUtils; @@ -71,16 +70,17 @@ public final class SingleRule implements DatabaseRule, DataNodeContainedRule, Ta private final TableNamesMapper tableNamesMapper = new TableNamesMapper(); - private final DatabaseType databaseType; + private final DatabaseType protocolType; - public SingleRule(final SingleRuleConfiguration ruleConfig, final String databaseName, final Map dataSourceMap, final Collection builtRules) { + public SingleRule(final SingleRuleConfiguration ruleConfig, final String databaseName, final DatabaseType protocolType, final Map dataSourceMap, + final Collection builtRules) { configuration = ruleConfig; defaultDataSource = ruleConfig.getDefaultDataSource().orElse(null); Map enabledDataSources = DataSourceStateManager.getInstance().getEnabledDataSources(databaseName, dataSourceMap); Map aggregateDataSourceMap = SingleTableLoadUtils.getAggregatedDataSourceMap(enabledDataSources, builtRules); dataSourceNames = aggregateDataSourceMap.keySet(); - databaseType = enabledDataSources.isEmpty() ? DatabaseTypeEngine.getDefaultStorageType() : DatabaseTypeEngine.getStorageType(enabledDataSources.values().iterator().next()); - singleTableDataNodes = SingleTableDataNodeLoader.load(databaseName, databaseType, aggregateDataSourceMap, builtRules, configuration.getTables()); + this.protocolType = protocolType; + singleTableDataNodes = SingleTableDataNodeLoader.load(databaseName, protocolType, aggregateDataSourceMap, builtRules, configuration.getTables()); singleTableDataNodes.forEach((key, value) -> tableNamesMapper.put(value.iterator().next().getTableName())); } @@ -172,9 +172,9 @@ private boolean containsDataNode(final QualifiedTable qualifiedTable, final Coll * @return qualified tables */ public Collection getQualifiedTables(final SQLStatementContext sqlStatementContext, final ShardingSphereDatabase database) { - Collection result = getQualifiedTables(database, databaseType, sqlStatementContext.getTablesContext().getSimpleTableSegments()); + Collection result = getQualifiedTables(database, protocolType, sqlStatementContext.getTablesContext().getSimpleTableSegments()); if (result.isEmpty() && sqlStatementContext instanceof IndexAvailable) { - result = IndexMetaDataUtils.getTableNames(database, databaseType, ((IndexAvailable) sqlStatementContext).getIndexes()); + result = IndexMetaDataUtils.getTableNames(database, protocolType, ((IndexAvailable) sqlStatementContext).getIndexes()); } return result; } @@ -203,11 +203,11 @@ public void put(final String dataSourceName, final String schemaName, final Stri private void addTableConfiguration(final String dataSourceName, final String schemaName, final String tableName) { Collection splitTables = SingleTableLoadUtils.splitTableLines(configuration.getTables()); - if (splitTables.contains(SingleTableLoadUtils.getAllTablesNodeStr(databaseType)) - || splitTables.contains(SingleTableLoadUtils.getAllTablesNodeStrFromDataSource(databaseType, dataSourceName, schemaName))) { + if (splitTables.contains(SingleTableLoadUtils.getAllTablesNodeStr(protocolType)) + || splitTables.contains(SingleTableLoadUtils.getAllTablesNodeStrFromDataSource(protocolType, dataSourceName, schemaName))) { return; } - String dataNodeString = SingleTableLoadUtils.getDataNodeString(databaseType, dataSourceName, schemaName, tableName); + String dataNodeString = SingleTableLoadUtils.getDataNodeString(protocolType, dataSourceName, schemaName, tableName); if (!configuration.getTables().contains(dataNodeString)) { configuration.getTables().add(dataNodeString); } @@ -229,7 +229,7 @@ public void remove(final Collection schemaNames, final String tableName) DataNode each = iterator.next(); if (schemaNames.contains(each.getSchemaName().toLowerCase())) { iterator.remove(); - configuration.getTables().remove(SingleTableLoadUtils.getDataNodeString(databaseType, each.getDataSourceName(), each.getSchemaName(), tableName)); + configuration.getTables().remove(SingleTableLoadUtils.getDataNodeString(protocolType, each.getDataSourceName(), each.getSchemaName(), tableName)); } } if (dataNodes.isEmpty()) { @@ -252,7 +252,7 @@ public Optional findTableDataNode(final String schemaName, final Strin @Override public ShardingSphereRule reloadRule(final RuleConfiguration config, final String databaseName, final Map dataSourceMap, final Collection builtRules) { - return new SingleRule((SingleRuleConfiguration) config, databaseName, dataSourceMap, builtRules); + return new SingleRule((SingleRuleConfiguration) config, databaseName, protocolType, dataSourceMap, builtRules); } @Override diff --git a/kernel/single/core/src/main/java/org/apache/shardingsphere/single/rule/builder/SingleRuleBuilder.java b/kernel/single/core/src/main/java/org/apache/shardingsphere/single/rule/builder/SingleRuleBuilder.java index 5aae278564662..6090589521789 100644 --- a/kernel/single/core/src/main/java/org/apache/shardingsphere/single/rule/builder/SingleRuleBuilder.java +++ b/kernel/single/core/src/main/java/org/apache/shardingsphere/single/rule/builder/SingleRuleBuilder.java @@ -17,6 +17,7 @@ package org.apache.shardingsphere.single.rule.builder; +import org.apache.shardingsphere.infra.database.core.type.DatabaseType; import org.apache.shardingsphere.infra.instance.InstanceContext; import org.apache.shardingsphere.infra.rule.ShardingSphereRule; import org.apache.shardingsphere.infra.rule.builder.database.DatabaseRuleBuilder; @@ -34,9 +35,9 @@ public final class SingleRuleBuilder implements DatabaseRuleBuilder { @Override - public SingleRule build(final SingleRuleConfiguration config, final String databaseName, + public SingleRule build(final SingleRuleConfiguration config, final String databaseName, final DatabaseType protocolType, final Map dataSources, final Collection builtRules, final InstanceContext instanceContext) { - return new SingleRule(config, databaseName, dataSources, builtRules); + return new SingleRule(config, databaseName, protocolType, dataSources, builtRules); } @Override diff --git a/kernel/single/core/src/test/java/org/apache/shardingsphere/single/datanode/SingleTableDataNodeLoaderTest.java b/kernel/single/core/src/test/java/org/apache/shardingsphere/single/datanode/SingleTableDataNodeLoaderTest.java index b516d60238ed3..3cd144e60ab02 100644 --- a/kernel/single/core/src/test/java/org/apache/shardingsphere/single/datanode/SingleTableDataNodeLoaderTest.java +++ b/kernel/single/core/src/test/java/org/apache/shardingsphere/single/datanode/SingleTableDataNodeLoaderTest.java @@ -78,6 +78,7 @@ private DataSource mockDataSource(final String dataSourceName, final List mockQualifiedTables() { @Test void assertRouteWithoutSingleRule() throws SQLException { SingleStandardRouteEngine engine = new SingleStandardRouteEngine(mockQualifiedTables(), new MySQLCreateTableStatement(false)); - SingleRule singleRule = new SingleRule(new SingleRuleConfiguration(), DefaultDatabase.LOGIC_NAME, createDataSourceMap(), Collections.emptyList()); + SingleRule singleRule = new SingleRule(new SingleRuleConfiguration(), DefaultDatabase.LOGIC_NAME, new MySQLDatabaseType(), createDataSourceMap(), Collections.emptyList()); RouteContext routeContext = new RouteContext(); engine.route(routeContext, singleRule); List routeUnits = new ArrayList<>(routeContext.getRouteUnits()); @@ -106,7 +107,8 @@ void assertRouteWithoutSingleRule() throws SQLException { @Test void assertRouteWithDefaultSingleRule() throws SQLException { SingleStandardRouteEngine engine = new SingleStandardRouteEngine(mockQualifiedTables(), new MySQLCreateTableStatement(false)); - SingleRule singleRule = new SingleRule(new SingleRuleConfiguration(Collections.emptyList(), "ds_0"), DefaultDatabase.LOGIC_NAME, createDataSourceMap(), Collections.emptyList()); + SingleRule singleRule = + new SingleRule(new SingleRuleConfiguration(Collections.emptyList(), "ds_0"), DefaultDatabase.LOGIC_NAME, new MySQLDatabaseType(), createDataSourceMap(), Collections.emptyList()); RouteContext routeContext = new RouteContext(); engine.route(routeContext, singleRule); List routeUnits = new ArrayList<>(routeContext.getRouteUnits()); diff --git a/kernel/single/core/src/test/java/org/apache/shardingsphere/single/rule/SingleRuleTest.java b/kernel/single/core/src/test/java/org/apache/shardingsphere/single/rule/SingleRuleTest.java index 7bb44fbce6617..1b107b9067b38 100644 --- a/kernel/single/core/src/test/java/org/apache/shardingsphere/single/rule/SingleRuleTest.java +++ b/kernel/single/core/src/test/java/org/apache/shardingsphere/single/rule/SingleRuleTest.java @@ -18,6 +18,7 @@ package org.apache.shardingsphere.single.rule; import org.apache.shardingsphere.infra.database.core.DefaultDatabase; +import org.apache.shardingsphere.infra.database.h2.type.H2DatabaseType; import org.apache.shardingsphere.infra.datanode.DataNode; import org.apache.shardingsphere.infra.metadata.database.schema.QualifiedTable; import org.apache.shardingsphere.infra.route.context.RouteContext; @@ -102,7 +103,7 @@ void assertGetSingleTableDataNodes() { TableContainedRule tableContainedRule = mock(TableContainedRule.class, RETURNS_DEEP_STUBS); when(tableContainedRule.getDistributedTableMapper().getTableNames()).thenReturn(Collections.singletonList("t_order")); when(tableContainedRule.getActualTableMapper().getTableNames()).thenReturn(Arrays.asList("t_order_0", "t_order_1")); - SingleRule singleRule = new SingleRule(ruleConfig, DefaultDatabase.LOGIC_NAME, dataSourceMap, Collections.singleton(tableContainedRule)); + SingleRule singleRule = new SingleRule(ruleConfig, DefaultDatabase.LOGIC_NAME, new H2DatabaseType(), dataSourceMap, Collections.singleton(tableContainedRule)); Map> actual = singleRule.getSingleTableDataNodes(); assertThat(actual.size(), is(2)); assertTrue(actual.containsKey("employee")); @@ -114,7 +115,7 @@ void assertGetSingleTableDataNodesWithUpperCase() { TableContainedRule tableContainedRule = mock(TableContainedRule.class, RETURNS_DEEP_STUBS); when(tableContainedRule.getDistributedTableMapper().getTableNames()).thenReturn(Collections.singletonList("T_ORDER")); when(tableContainedRule.getActualTableMapper().getTableNames()).thenReturn(Arrays.asList("T_ORDER_0", "T_ORDER_1")); - SingleRule singleRule = new SingleRule(ruleConfig, DefaultDatabase.LOGIC_NAME, dataSourceMap, Collections.singleton(tableContainedRule)); + SingleRule singleRule = new SingleRule(ruleConfig, DefaultDatabase.LOGIC_NAME, new H2DatabaseType(), dataSourceMap, Collections.singleton(tableContainedRule)); Map> actual = singleRule.getSingleTableDataNodes(); assertThat(actual.size(), is(2)); assertTrue(actual.containsKey("employee")); @@ -124,7 +125,7 @@ void assertGetSingleTableDataNodesWithUpperCase() { @Test void assertFindSingleTableDataNode() { DataNodeContainedRule dataNodeContainedRule = mock(DataNodeContainedRule.class); - SingleRule singleRule = new SingleRule(ruleConfig, DefaultDatabase.LOGIC_NAME, dataSourceMap, Collections.singleton(dataNodeContainedRule)); + SingleRule singleRule = new SingleRule(ruleConfig, DefaultDatabase.LOGIC_NAME, new H2DatabaseType(), dataSourceMap, Collections.singleton(dataNodeContainedRule)); Optional actual = singleRule.findTableDataNode(DefaultDatabase.LOGIC_NAME, "employee"); assertTrue(actual.isPresent()); assertThat(actual.get().getDataSourceName(), is("foo_ds")); @@ -134,7 +135,7 @@ void assertFindSingleTableDataNode() { @Test void assertFindSingleTableDataNodeWithUpperCase() { DataNodeContainedRule dataNodeContainedRule = mock(DataNodeContainedRule.class); - SingleRule singleRule = new SingleRule(ruleConfig, DefaultDatabase.LOGIC_NAME, dataSourceMap, Collections.singleton(dataNodeContainedRule)); + SingleRule singleRule = new SingleRule(ruleConfig, DefaultDatabase.LOGIC_NAME, new H2DatabaseType(), dataSourceMap, Collections.singleton(dataNodeContainedRule)); Optional actual = singleRule.findTableDataNode(DefaultDatabase.LOGIC_NAME, "EMPLOYEE"); assertTrue(actual.isPresent()); assertThat(actual.get().getDataSourceName(), is("foo_ds")); @@ -151,7 +152,7 @@ void assertIsAllTablesInSameDataSource() { RouteContext routeContext = new RouteContext(); routeContext.putRouteUnit(dataSourceMapper, tableMappers); DataNodeContainedRule dataNodeContainedRule = mock(DataNodeContainedRule.class); - SingleRule singleRule = new SingleRule(ruleConfig, DefaultDatabase.LOGIC_NAME, dataSourceMap, Collections.singleton(dataNodeContainedRule)); + SingleRule singleRule = new SingleRule(ruleConfig, DefaultDatabase.LOGIC_NAME, new H2DatabaseType(), dataSourceMap, Collections.singleton(dataNodeContainedRule)); assertTrue(singleRule.isAllTablesInSameComputeNode(routeContext.getOriginalDataNodes().stream().flatMap(Collection::stream).collect(Collectors.toList()), singleTables)); } @@ -160,14 +161,14 @@ void assertAssignNewDataSourceName() { DataNodeContainedRule dataNodeContainedRule = mock(DataNodeContainedRule.class); SingleRuleConfiguration singleRuleConfig = new SingleRuleConfiguration(); singleRuleConfig.setDefaultDataSource("foo_ds"); - SingleRule singleRule = new SingleRule(singleRuleConfig, DefaultDatabase.LOGIC_NAME, dataSourceMap, Collections.singleton(dataNodeContainedRule)); + SingleRule singleRule = new SingleRule(singleRuleConfig, DefaultDatabase.LOGIC_NAME, new H2DatabaseType(), dataSourceMap, Collections.singleton(dataNodeContainedRule)); assertThat(singleRule.assignNewDataSourceName(), is("foo_ds")); } @Test void assertGetSingleTables() { DataNodeContainedRule dataNodeContainedRule = mock(DataNodeContainedRule.class); - SingleRule singleRule = new SingleRule(ruleConfig, DefaultDatabase.LOGIC_NAME, dataSourceMap, Collections.singleton(dataNodeContainedRule)); + SingleRule singleRule = new SingleRule(ruleConfig, DefaultDatabase.LOGIC_NAME, new H2DatabaseType(), dataSourceMap, Collections.singleton(dataNodeContainedRule)); Collection tableNames = new LinkedList<>(); tableNames.add(new QualifiedTable(DefaultDatabase.LOGIC_NAME, "employee")); assertThat(singleRule.getSingleTables(tableNames).iterator().next().getSchemaName(), is(DefaultDatabase.LOGIC_NAME)); @@ -177,7 +178,7 @@ void assertGetSingleTables() { @Test void assertPut() { DataNodeContainedRule dataNodeContainedRule = mock(DataNodeContainedRule.class); - SingleRule singleRule = new SingleRule(ruleConfig, DefaultDatabase.LOGIC_NAME, dataSourceMap, Collections.singleton(dataNodeContainedRule)); + SingleRule singleRule = new SingleRule(ruleConfig, DefaultDatabase.LOGIC_NAME, new H2DatabaseType(), dataSourceMap, Collections.singleton(dataNodeContainedRule)); String tableName = "teacher"; String dataSourceName = "foo_ds"; singleRule.put(dataSourceName, DefaultDatabase.LOGIC_NAME, tableName); @@ -195,7 +196,7 @@ void assertPut() { @Test void assertRemove() { DataNodeContainedRule dataNodeContainedRule = mock(DataNodeContainedRule.class); - SingleRule singleRule = new SingleRule(ruleConfig, DefaultDatabase.LOGIC_NAME, dataSourceMap, Collections.singleton(dataNodeContainedRule)); + SingleRule singleRule = new SingleRule(ruleConfig, DefaultDatabase.LOGIC_NAME, new H2DatabaseType(), dataSourceMap, Collections.singleton(dataNodeContainedRule)); String tableName = "employee"; singleRule.remove(DefaultDatabase.LOGIC_NAME, tableName); Collection tableNames = new LinkedList<>(); @@ -209,7 +210,7 @@ void assertRemove() { @Test void assertGetAllDataNodes() { DataNodeContainedRule dataNodeContainedRule = mock(DataNodeContainedRule.class); - SingleRule singleRule = new SingleRule(ruleConfig, DefaultDatabase.LOGIC_NAME, dataSourceMap, Collections.singleton(dataNodeContainedRule)); + SingleRule singleRule = new SingleRule(ruleConfig, DefaultDatabase.LOGIC_NAME, new H2DatabaseType(), dataSourceMap, Collections.singleton(dataNodeContainedRule)); assertTrue(singleRule.getAllDataNodes().containsKey("employee")); assertTrue(singleRule.getAllDataNodes().containsKey("student")); assertTrue(singleRule.getAllDataNodes().containsKey("t_order_0")); @@ -219,7 +220,7 @@ void assertGetAllDataNodes() { @Test void assertGetDataNodesByTableName() { DataNodeContainedRule dataNodeContainedRule = mock(DataNodeContainedRule.class); - SingleRule singleRule = new SingleRule(ruleConfig, DefaultDatabase.LOGIC_NAME, dataSourceMap, Collections.singleton(dataNodeContainedRule)); + SingleRule singleRule = new SingleRule(ruleConfig, DefaultDatabase.LOGIC_NAME, new H2DatabaseType(), dataSourceMap, Collections.singleton(dataNodeContainedRule)); Collection actual = singleRule.getDataNodesByTableName("EMPLOYEE"); assertThat(actual.size(), is(1)); DataNode dataNode = actual.iterator().next(); @@ -230,7 +231,7 @@ void assertGetDataNodesByTableName() { @Test void assertFindFirstActualTable() { DataNodeContainedRule dataNodeContainedRule = mock(DataNodeContainedRule.class); - SingleRule singleRule = new SingleRule(ruleConfig, DefaultDatabase.LOGIC_NAME, dataSourceMap, Collections.singleton(dataNodeContainedRule)); + SingleRule singleRule = new SingleRule(ruleConfig, DefaultDatabase.LOGIC_NAME, new H2DatabaseType(), dataSourceMap, Collections.singleton(dataNodeContainedRule)); String logicTable = "employee"; assertFalse(singleRule.findFirstActualTable(logicTable).isPresent()); } @@ -238,14 +239,14 @@ void assertFindFirstActualTable() { @Test void assertIsNeedAccumulate() { DataNodeContainedRule dataNodeContainedRule = mock(DataNodeContainedRule.class); - SingleRule singleRule = new SingleRule(ruleConfig, DefaultDatabase.LOGIC_NAME, dataSourceMap, Collections.singleton(dataNodeContainedRule)); + SingleRule singleRule = new SingleRule(ruleConfig, DefaultDatabase.LOGIC_NAME, new H2DatabaseType(), dataSourceMap, Collections.singleton(dataNodeContainedRule)); assertFalse(singleRule.isNeedAccumulate(Collections.emptyList())); } @Test void assertFindLogicTableByActualTable() { DataNodeContainedRule dataNodeContainedRule = mock(DataNodeContainedRule.class); - SingleRule singleRule = new SingleRule(ruleConfig, DefaultDatabase.LOGIC_NAME, dataSourceMap, Collections.singleton(dataNodeContainedRule)); + SingleRule singleRule = new SingleRule(ruleConfig, DefaultDatabase.LOGIC_NAME, new H2DatabaseType(), dataSourceMap, Collections.singleton(dataNodeContainedRule)); String actualTable = "student"; assertFalse(singleRule.findLogicTableByActualTable(actualTable).isPresent()); } @@ -253,7 +254,7 @@ void assertFindLogicTableByActualTable() { @Test void assertFindActualTableByCatalog() { DataNodeContainedRule dataNodeContainedRule = mock(DataNodeContainedRule.class); - SingleRule singleRule = new SingleRule(ruleConfig, DefaultDatabase.LOGIC_NAME, dataSourceMap, Collections.singleton(dataNodeContainedRule)); + SingleRule singleRule = new SingleRule(ruleConfig, DefaultDatabase.LOGIC_NAME, new H2DatabaseType(), dataSourceMap, Collections.singleton(dataNodeContainedRule)); String catalog = "employee"; String logicTable = "t_order_0"; assertFalse(singleRule.findActualTableByCatalog(catalog, logicTable).isPresent()); diff --git a/kernel/single/core/src/test/java/org/apache/shardingsphere/single/rule/builder/SingleRuleBuilderTest.java b/kernel/single/core/src/test/java/org/apache/shardingsphere/single/rule/builder/SingleRuleBuilderTest.java index e0714c9ca6d0d..40b3d9765aff6 100644 --- a/kernel/single/core/src/test/java/org/apache/shardingsphere/single/rule/builder/SingleRuleBuilderTest.java +++ b/kernel/single/core/src/test/java/org/apache/shardingsphere/single/rule/builder/SingleRuleBuilderTest.java @@ -17,6 +17,7 @@ package org.apache.shardingsphere.single.rule.builder; +import org.apache.shardingsphere.infra.database.mysql.type.MySQLDatabaseType; import org.apache.shardingsphere.infra.instance.InstanceContext; import org.apache.shardingsphere.infra.rule.ShardingSphereRule; import org.apache.shardingsphere.infra.rule.builder.database.DatabaseRuleBuilder; @@ -38,7 +39,8 @@ class SingleRuleBuilderTest { @Test void assertBuild() { DatabaseRuleBuilder builder = OrderedSPILoader.getServices(DatabaseRuleBuilder.class).iterator().next(); - DatabaseRule actual = builder.build(mock(SingleRuleConfiguration.class), "", Collections.emptyMap(), Collections.singleton(mock(ShardingSphereRule.class)), mock(InstanceContext.class)); + DatabaseRule actual = builder.build(mock(SingleRuleConfiguration.class), "", new MySQLDatabaseType(), Collections.emptyMap(), Collections.singleton(mock(ShardingSphereRule.class)), + mock(InstanceContext.class)); assertThat(actual, instanceOf(SingleRule.class)); } @@ -47,7 +49,8 @@ void assertBuild() { void assertBuildWithDefaultDataSource() { DatabaseRuleBuilder builder = OrderedSPILoader.getServices(DatabaseRuleBuilder.class).iterator().next(); DatabaseRule actual = builder.build( - new SingleRuleConfiguration(Collections.emptyList(), "foo_ds"), "", Collections.emptyMap(), Collections.singleton(mock(ShardingSphereRule.class)), mock(InstanceContext.class)); + new SingleRuleConfiguration(Collections.emptyList(), "foo_ds"), "", new MySQLDatabaseType(), Collections.emptyMap(), Collections.singleton(mock(ShardingSphereRule.class)), + mock(InstanceContext.class)); assertThat(actual, instanceOf(SingleRule.class)); } } diff --git a/kernel/single/distsql/handler/src/main/java/org/apache/shardingsphere/single/distsql/handler/query/ShowUnloadedSingleTableExecutor.java b/kernel/single/distsql/handler/src/main/java/org/apache/shardingsphere/single/distsql/handler/query/ShowUnloadedSingleTableExecutor.java index 8f23279e962c2..08d51d6bc9074 100644 --- a/kernel/single/distsql/handler/src/main/java/org/apache/shardingsphere/single/distsql/handler/query/ShowUnloadedSingleTableExecutor.java +++ b/kernel/single/distsql/handler/src/main/java/org/apache/shardingsphere/single/distsql/handler/query/ShowUnloadedSingleTableExecutor.java @@ -68,7 +68,7 @@ private Map> getActualDataNodes(final ShardingSpher .collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getDataSource(), (oldValue, currentValue) -> oldValue, LinkedHashMap::new)), database.getRuleMetaData().getRules()); Collection excludedTables = SingleTableLoadUtils.getExcludedTables(database.getRuleMetaData().getRules()); - return SingleTableDataNodeLoader.load(database.getName(), database.getProtocolType(), aggregateDataSourceMap, excludedTables); + return SingleTableDataNodeLoader.load(database.getName(), aggregateDataSourceMap, excludedTables); } @Override diff --git a/kernel/single/distsql/handler/src/main/java/org/apache/shardingsphere/single/distsql/handler/update/LoadSingleTableStatementUpdater.java b/kernel/single/distsql/handler/src/main/java/org/apache/shardingsphere/single/distsql/handler/update/LoadSingleTableStatementUpdater.java index 617082f91d8cf..3048f0a73a6f3 100644 --- a/kernel/single/distsql/handler/src/main/java/org/apache/shardingsphere/single/distsql/handler/update/LoadSingleTableStatementUpdater.java +++ b/kernel/single/distsql/handler/src/main/java/org/apache/shardingsphere/single/distsql/handler/update/LoadSingleTableStatementUpdater.java @@ -19,6 +19,7 @@ import org.apache.shardingsphere.distsql.handler.exception.storageunit.MissingRequiredStorageUnitsException; import org.apache.shardingsphere.distsql.handler.update.RuleDefinitionCreateUpdater; +import org.apache.shardingsphere.infra.database.DatabaseTypeEngine; import org.apache.shardingsphere.infra.database.core.metadata.database.DialectDatabaseMetaData; import org.apache.shardingsphere.infra.database.core.type.DatabaseTypeRegistry; import org.apache.shardingsphere.infra.exception.InvalidDataNodesFormatException; @@ -119,7 +120,8 @@ private void checkActualTableExist(final ShardingSphereDatabase database, final database.getRuleMetaData().getRules()); Map>> actualTableNodes = new LinkedHashMap<>(); for (String each : requiredDataSources) { - Map> schemaTableNames = SingleTableDataNodeLoader.loadSchemaTableNames(database.getName(), database.getProtocolType(), aggregateDataSourceMap.get(each), each); + DataSource dataSource = aggregateDataSourceMap.get(each); + Map> schemaTableNames = SingleTableDataNodeLoader.loadSchemaTableNames(database.getName(), DatabaseTypeEngine.getStorageType(dataSource), dataSource, each); if (!schemaTableNames.isEmpty()) { actualTableNodes.put(each, schemaTableNames); } diff --git a/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/context/ConfigurationContextManager.java b/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/context/ConfigurationContextManager.java index 40d9ba91e096e..d6db3fc95365c 100644 --- a/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/context/ConfigurationContextManager.java +++ b/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/context/ConfigurationContextManager.java @@ -172,7 +172,7 @@ public synchronized void alterRuleConfiguration(final String databaseName, final ShardingSphereDatabase database = metaDataContexts.get().getMetaData().getDatabase(databaseName); Collection rules = new LinkedList<>(database.getRuleMetaData().getRules()); rules.removeIf(each -> each.getConfiguration().getClass().isAssignableFrom(ruleConfig.getClass())); - rules.addAll(DatabaseRulesBuilder.build(databaseName, + rules.addAll(DatabaseRulesBuilder.build(databaseName, database.getProtocolType(), database.getResourceMetaData().getStorageUnits().entrySet().stream() .collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getDataSource(), (oldValue, currentValue) -> oldValue, LinkedHashMap::new)), database.getRuleMetaData().getRules(), ruleConfig, instanceContext)); @@ -194,7 +194,7 @@ public synchronized void dropRuleConfiguration(final String databaseName, final Collection rules = new LinkedList<>(database.getRuleMetaData().getRules()); rules.removeIf(each -> each.getConfiguration().getClass().isAssignableFrom(ruleConfig.getClass())); if (isNotEmptyConfig(ruleConfig)) { - rules.addAll(DatabaseRulesBuilder.build(databaseName, + rules.addAll(DatabaseRulesBuilder.build(databaseName, database.getProtocolType(), database.getResourceMetaData().getStorageUnits().entrySet().stream() .collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getDataSource(), (oldValue, currentValue) -> oldValue, LinkedHashMap::new)), database.getRuleMetaData().getRules(), ruleConfig, instanceContext)); diff --git a/mode/core/src/test/java/org/apache/shardingsphere/mode/fixture/ModeRuleBuilderFixture.java b/mode/core/src/test/java/org/apache/shardingsphere/mode/fixture/ModeRuleBuilderFixture.java index f6c671eff9f55..4509f8ff1107d 100644 --- a/mode/core/src/test/java/org/apache/shardingsphere/mode/fixture/ModeRuleBuilderFixture.java +++ b/mode/core/src/test/java/org/apache/shardingsphere/mode/fixture/ModeRuleBuilderFixture.java @@ -17,6 +17,7 @@ package org.apache.shardingsphere.mode.fixture; +import org.apache.shardingsphere.infra.database.core.type.DatabaseType; import org.apache.shardingsphere.infra.instance.InstanceContext; import org.apache.shardingsphere.infra.rule.ShardingSphereRule; import org.apache.shardingsphere.infra.rule.builder.database.DatabaseRuleBuilder; @@ -28,7 +29,7 @@ public final class ModeRuleBuilderFixture implements DatabaseRuleBuilder { @Override - public ModeRuleFixture build(final ModeRuleConfigurationFixture config, final String databaseName, final Map dataSources, + public ModeRuleFixture build(final ModeRuleConfigurationFixture config, final String databaseName, final DatabaseType databaseType, final Map dataSources, final Collection builtRules, final InstanceContext instanceContext) { return new ModeRuleFixture(); } diff --git a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/util/YamlDatabaseConfigurationImportExecutor.java b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/util/YamlDatabaseConfigurationImportExecutor.java index cdc0fa7455716..2adc0108c3c08 100644 --- a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/util/YamlDatabaseConfigurationImportExecutor.java +++ b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/util/YamlDatabaseConfigurationImportExecutor.java @@ -292,7 +292,7 @@ private void addBroadcastRuleConfiguration(final BroadcastRuleConfiguration broa private void addSingleRuleConfiguration(final SingleRuleConfiguration singleRuleConfig, final Collection allRuleConfigs, final ShardingSphereDatabase database) { allRuleConfigs.add(singleRuleConfig); database.getRuleMetaData().getRules().add( - new SingleRule(singleRuleConfig, database.getName(), + new SingleRule(singleRuleConfig, database.getName(), database.getProtocolType(), database.getResourceMetaData().getStorageUnits().entrySet().stream() .collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getDataSource(), (oldValue, currentValue) -> oldValue, LinkedHashMap::new)), database.getRuleMetaData().getRules())); diff --git a/proxy/bootstrap/src/main/java/org/apache/shardingsphere/proxy/Bootstrap.java b/proxy/bootstrap/src/main/java/org/apache/shardingsphere/proxy/Bootstrap.java index 0604d7675a005..620a6b5a0422b 100644 --- a/proxy/bootstrap/src/main/java/org/apache/shardingsphere/proxy/Bootstrap.java +++ b/proxy/bootstrap/src/main/java/org/apache/shardingsphere/proxy/Bootstrap.java @@ -56,8 +56,8 @@ public static void main(final String[] args) throws IOException, SQLException { Optional.ofNullable((Integer) yamlConfig.getServerConfiguration().getProps().get(ConfigurationPropertyKey.CDC_SERVER_PORT.getKey())) .ifPresent(optional -> new CDCServer(addresses, optional).start()); ProxySSLContext.init(); - ShardingSphereProxy shardingSphereProxy = new ShardingSphereProxy(); - bootstrapArgs.getSocketPath().ifPresent(shardingSphereProxy::start); - shardingSphereProxy.start(port, addresses); + ShardingSphereProxy proxy = new ShardingSphereProxy(); + bootstrapArgs.getSocketPath().ifPresent(proxy::start); + proxy.start(port, addresses); } } diff --git a/test/it/rewriter/src/test/java/org/apache/shardingsphere/test/it/rewrite/engine/SQLRewriterIT.java b/test/it/rewriter/src/test/java/org/apache/shardingsphere/test/it/rewrite/engine/SQLRewriterIT.java index aa641eadc8348..f21492e1e76a0 100644 --- a/test/it/rewriter/src/test/java/org/apache/shardingsphere/test/it/rewrite/engine/SQLRewriterIT.java +++ b/test/it/rewriter/src/test/java/org/apache/shardingsphere/test/it/rewrite/engine/SQLRewriterIT.java @@ -128,7 +128,7 @@ private Collection createSQLRewriteUnits(final SQLRewriteEngineT SQLStatementParserEngine sqlStatementParserEngine = new SQLStatementParserEngine(TypedSPILoader.getService(DatabaseType.class, testParams.getDatabaseType()), sqlParserRule.getSqlStatementCache(), sqlParserRule.getParseTreeCache(), sqlParserRule.isSqlCommentParseEnabled()); SQLStatement sqlStatement = sqlStatementParserEngine.parse(testParams.getInputSQL(), false); - Collection databaseRules = createDatabaseRules(databaseConfig, schemaName, sqlStatement); + Collection databaseRules = createDatabaseRules(databaseConfig, schemaName, sqlStatement, databaseType); RuleMetaData databaseRuleMetaData = new RuleMetaData(databaseRules); ShardingSphereDatabase database = new ShardingSphereDatabase(schemaName, databaseType, resourceMetaData, databaseRuleMetaData, mockSchemas(schemaName)); Map databases = new HashMap<>(2, 1F); @@ -155,8 +155,8 @@ private Collection createSQLRewriteUnits(final SQLRewriteEngineT : (((RouteSQLRewriteResult) sqlRewriteResult).getSqlRewriteUnits()).values(); } - private Collection createDatabaseRules(final DatabaseConfiguration databaseConfig, final String schemaName, final SQLStatement sqlStatement) { - Collection result = DatabaseRulesBuilder.build(DefaultDatabase.LOGIC_NAME, databaseConfig, mock(InstanceContext.class)); + private Collection createDatabaseRules(final DatabaseConfiguration databaseConfig, final String schemaName, final SQLStatement sqlStatement, final DatabaseType databaseType) { + Collection result = DatabaseRulesBuilder.build(DefaultDatabase.LOGIC_NAME, databaseType, databaseConfig, mock(InstanceContext.class)); mockRules(result, schemaName, sqlStatement); result.add(sqlParserRule); result.add(timestampServiceRule);