diff --git a/agent/plugins/metrics/core/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/core/exporter/impl/proxy/ProxyMetaDataInfoExporterTest.java b/agent/plugins/metrics/core/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/core/exporter/impl/proxy/ProxyMetaDataInfoExporterTest.java index 36d0aab30527a..7008ec312e07b 100644 --- a/agent/plugins/metrics/core/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/core/exporter/impl/proxy/ProxyMetaDataInfoExporterTest.java +++ b/agent/plugins/metrics/core/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/core/exporter/impl/proxy/ProxyMetaDataInfoExporterTest.java @@ -30,6 +30,7 @@ import org.apache.shardingsphere.metadata.persist.MetaDataPersistService; import org.apache.shardingsphere.mode.manager.ContextManager; import org.apache.shardingsphere.mode.metadata.MetaDataContexts; +import org.apache.shardingsphere.mode.metadata.MetaDataContextsFactory; import org.apache.shardingsphere.proxy.backend.context.ProxyContext; import org.apache.shardingsphere.test.mock.AutoMockExtension; import org.apache.shardingsphere.test.mock.StaticMockSettings; @@ -79,7 +80,7 @@ private ContextManager mockContextManager() { when(database.getProtocolType()).thenReturn(TypedSPILoader.getService(DatabaseType.class, "FIXTURE")); ShardingSphereMetaData metaData = mock(ShardingSphereMetaData.class); when(metaData.getDatabases()).thenReturn(Collections.singletonMap("sharding_db", database)); - MetaDataContexts metaDataContexts = new MetaDataContexts(mock(MetaDataPersistService.class), metaData); + MetaDataContexts metaDataContexts = MetaDataContextsFactory.create(mock(MetaDataPersistService.class), metaData); ContextManager result = mock(ContextManager.class, RETURNS_DEEP_STUBS); when(result.getMetaDataContexts()).thenReturn(metaDataContexts); return result; diff --git a/agent/plugins/metrics/core/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/core/exporter/impl/proxy/ProxyStateExporterTest.java b/agent/plugins/metrics/core/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/core/exporter/impl/proxy/ProxyStateExporterTest.java index 74ecdf388021a..f2fc0fcdaf097 100644 --- a/agent/plugins/metrics/core/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/core/exporter/impl/proxy/ProxyStateExporterTest.java +++ b/agent/plugins/metrics/core/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/core/exporter/impl/proxy/ProxyStateExporterTest.java @@ -34,6 +34,8 @@ import org.apache.shardingsphere.mode.manager.ContextManager; import org.apache.shardingsphere.mode.manager.standalone.workerid.generator.StandaloneWorkerIdGenerator; import org.apache.shardingsphere.mode.metadata.MetaDataContexts; +import org.apache.shardingsphere.mode.metadata.MetaDataContextsFactory; +import org.apache.shardingsphere.mode.spi.PersistRepository; import org.apache.shardingsphere.proxy.backend.context.ProxyContext; import org.apache.shardingsphere.test.mock.AutoMockExtension; import org.apache.shardingsphere.test.mock.StaticMockSettings; @@ -79,10 +81,10 @@ void assertExportWithContextManager() { } private ContextManager mockContextManager() { - MetaDataContexts metaDataContexts = new MetaDataContexts(mock(MetaDataPersistService.class), new ShardingSphereMetaData()); + MetaDataContexts metaDataContexts = MetaDataContextsFactory.create(mock(MetaDataPersistService.class), new ShardingSphereMetaData()); ComputeNodeInstanceContext computeNodeInstanceContext = new ComputeNodeInstanceContext( new ComputeNodeInstance(mock(InstanceMetaData.class)), new StandaloneWorkerIdGenerator(), new ModeConfiguration("Standalone", null), mock(LockContext.class), new EventBusContext()); - return new ContextManager(metaDataContexts, computeNodeInstanceContext); + return new ContextManager(metaDataContexts, computeNodeInstanceContext, mock(PersistRepository.class)); } } diff --git a/agent/plugins/metrics/type/prometheus/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/prometheus/PrometheusPluginLifecycleServiceTest.java b/agent/plugins/metrics/type/prometheus/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/prometheus/PrometheusPluginLifecycleServiceTest.java index 36806776f008f..be934e121087a 100644 --- a/agent/plugins/metrics/type/prometheus/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/prometheus/PrometheusPluginLifecycleServiceTest.java +++ b/agent/plugins/metrics/type/prometheus/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/prometheus/PrometheusPluginLifecycleServiceTest.java @@ -29,6 +29,8 @@ import org.apache.shardingsphere.mode.manager.ContextManager; import org.apache.shardingsphere.mode.manager.standalone.workerid.generator.StandaloneWorkerIdGenerator; import org.apache.shardingsphere.mode.metadata.MetaDataContexts; +import org.apache.shardingsphere.mode.metadata.MetaDataContextsFactory; +import org.apache.shardingsphere.mode.spi.PersistRepository; import org.apache.shardingsphere.proxy.backend.context.ProxyContext; import org.apache.shardingsphere.test.mock.AutoMockExtension; import org.apache.shardingsphere.test.mock.StaticMockSettings; @@ -68,10 +70,10 @@ void assertStart() throws IOException { } private ContextManager mockContextManager() { - MetaDataContexts metaDataContexts = new MetaDataContexts(mock(MetaDataPersistService.class), new ShardingSphereMetaData()); + MetaDataContexts metaDataContexts = MetaDataContextsFactory.create(mock(MetaDataPersistService.class), new ShardingSphereMetaData()); ComputeNodeInstanceContext computeNodeInstanceContext = new ComputeNodeInstanceContext( new ComputeNodeInstance(mock(InstanceMetaData.class)), new StandaloneWorkerIdGenerator(), new ModeConfiguration("Standalone", null), mock(LockContext.class), new EventBusContext()); - return new ContextManager(metaDataContexts, computeNodeInstanceContext); + return new ContextManager(metaDataContexts, computeNodeInstanceContext, mock(PersistRepository.class)); } } diff --git a/docs/blog/content/material/2022_03_09_SQL_Parse_Format_Function_A_Technical_Deep_Dive_by_Apache_ShardingSphere.en.md b/docs/blog/content/material/2022_03_09_SQL_Parse_Format_Function_A_Technical_Deep_Dive_by_Apache_ShardingSphere.en.md index 3fd2488febd78..9bd23a0a6f61a 100644 --- a/docs/blog/content/material/2022_03_09_SQL_Parse_Format_Function_A_Technical_Deep_Dive_by_Apache_ShardingSphere.en.md +++ b/docs/blog/content/material/2022_03_09_SQL_Parse_Format_Function_A_Technical_Deep_Dive_by_Apache_ShardingSphere.en.md @@ -89,6 +89,7 @@ simpleSelect 2. We can use IDEA’s ANTLR4 plugin to easily view the syntax tree of the SQL statement. For more information of ANTLR4 , please refer to: [https://plugins.jetbrains.com/plugin/7358-antlr-v4](https://plugins.jetbrains.com/plugin/7358-antlr-v4.). + ![Image description](https://miro.medium.com/max/700/1*EiWkP_kYN3sLOH4qsPonDA.jpeg) ANTLR4 can compile the syntax file we define: it first performs lexical analysis on the SQL statement, splits it into indivisible parts, namely tokens, and divides these tokens into keywords, expressions, according to the dictionary values of different databases. @@ -157,6 +158,7 @@ At first, in terms of the above-mentioned `Visitor` method, the visitor to be us Next, let’s still take the SQL statement as an example and provide specific code to show how `Visitor` formats it. `MySQLFormatSQLVisitor` is used to visit SQL. Based on the `DEBUG` code, we can clearly see the execution path of this visit as shown in the figure below. Visitor traverses all parts of the syntax tree, and ANTLR4 generates default methods for visiting each node according to the defined grammar rules. Apache ShardingSphere leverages key methods and successfully develops complete the SQL formatting function. + ![Image description](https://miro.medium.com/max/700/1*xjjACczbInC-K4t8EX-pEw.jpeg) The following code can help us better understand how `Visitor` can format SQL. @@ -235,6 +237,7 @@ public static void main(String[] args) { ``` - Parameters Supported by Properties + ![Image description](https://miro.medium.com/max/550/1*1Ft7G0EKkayVy5vrbcsDBg.png) You can also use DistSQL in ShardingSphere-Proxy to perform operations on the SQL Parse Format function: @@ -279,6 +282,7 @@ Currently, Apache ShardingSphere’s Format function only supports [MySQL](https ## Author **Chen Chuxin** + ![Image description](https://miro.medium.com/max/634/1*smrIU5STVJsJRais0_Tghg.png) > SphereEx Middleware Engineer & Apache ShardingSphere Committer diff --git "a/docs/blog/content/material/2022_03_11_Asia\342\200\231s_E-Commerce_Giant_Dangdang_Increases_Order_Processing_Speed_by_30%_Saves_Over_Ten_Million_in_Technology_Budget_with_Apache_ShardingSphere.en.md" "b/docs/blog/content/material/2022_03_11_Asia\342\200\231s_E-Commerce_Giant_Dangdang_Increases_Order_Processing_Speed_by_30%_Saves_Over_Ten_Million_in_Technology_Budget_with_Apache_ShardingSphere.en.md" index e198916874919..861aa40dfe01a 100644 --- "a/docs/blog/content/material/2022_03_11_Asia\342\200\231s_E-Commerce_Giant_Dangdang_Increases_Order_Processing_Speed_by_30%_Saves_Over_Ten_Million_in_Technology_Budget_with_Apache_ShardingSphere.en.md" +++ "b/docs/blog/content/material/2022_03_11_Asia\342\200\231s_E-Commerce_Giant_Dangdang_Increases_Order_Processing_Speed_by_30%_Saves_Over_Ten_Million_in_Technology_Budget_with_Apache_ShardingSphere.en.md" @@ -8,7 +8,7 @@ chapter = true > - Li Yong, Head of WMS Technology, Dangdang -Ffollowing [Apache ShardingSphere 5.0.0 GA](https://medium.com/codex/apache-shardingsphere-5-0-0-new-features-middleware-to-ecosystem-evolution-e69de00bfb1b) release in November 2021, the [5.1.0 version](https://shardingsphere.medium.com/apache-shardingsphere-5-1-0-now-avaliable-4244ac470e77) was released last month. Having gone through over two years of polishing, ShardingSphere’s plugin-oriented ecosystem is beginning to take shape, and the project embarks on the evolution from a simple data sharding middleware to a mature distributed database ecosystem driven by the concept of [Database Plus](https://www.infoq.com/articles/next-evolution-of-database-sharding-architecture/). +Following [Apache ShardingSphere 5.0.0 GA](https://medium.com/codex/apache-shardingsphere-5-0-0-new-features-middleware-to-ecosystem-evolution-e69de00bfb1b) release in November 2021, the [5.1.0 version](https://shardingsphere.medium.com/apache-shardingsphere-5-1-0-now-avaliable-4244ac470e77) was released last month. Having gone through over two years of polishing, ShardingSphere’s plugin-oriented ecosystem is beginning to take shape, and the project embarks on the evolution from a simple data sharding middleware to a mature distributed database ecosystem driven by the concept of [Database Plus](https://www.infoq.com/articles/next-evolution-of-database-sharding-architecture/). Dangdang, established at the end of 1999, has become a leading e-commerce platform selling books of any kind, and by integrating new Internet technologies with the traditional book industry. Dangdang was founded during the surge in China’s Internet industry in the early 2000s. @@ -115,7 +115,7 @@ Recently, to celebrate the third anniversary of ShardingSphere entering Apache S - On November 10, 2021, Version 5.0.0 GA was released as a third-anniversity celebration with the whole Apache ShardingSphere community, and the distributed database industry. ![Apache ShardingSphere—Roadmap](https://miro.medium.com/max/1400/0*ejOCiszgebnrZ2kx) -Since Version 5.0.0, Apache ShardingSphere has embarked on its new journey: with the plugin oriented architect at its core, it evloved from a data sharding application to a comprehensive and enhanced data governance tool applicable to various complex application scenarios. Concurrently, Apache ShardingSphere also has more features, and big data solutions. +Since Version 5.0.0, Apache ShardingSphere has embarked on its new journey: With the plugin oriented architect at its core, it evloved from a data sharding application to a comprehensive and enhanced data governance tool applicable to various complex application scenarios. Concurrently, Apache ShardingSphere also has more features, and big data solutions. ## Conclusion diff --git a/docs/blog/content/material/2022_03_18_Executor_Engine_Performance_Optimization_Showcase_with_Apache_ShardingSphere_5.1.0.en.md b/docs/blog/content/material/2022_03_18_Executor_Engine_Performance_Optimization_Showcase_with_Apache_ShardingSphere_5.1.0.en.md index c8aa23b81fcdf..37b97f3f5db4b 100644 --- a/docs/blog/content/material/2022_03_18_Executor_Engine_Performance_Optimization_Showcase_with_Apache_ShardingSphere_5.1.0.en.md +++ b/docs/blog/content/material/2022_03_18_Executor_Engine_Performance_Optimization_Showcase_with_Apache_ShardingSphere_5.1.0.en.md @@ -431,7 +431,7 @@ rules: worker-id: 123 ``` -We use the JMH test program to test different CASEs: +We use the JMH test program to test different cases: ``` @State(Scope.Thread) diff --git a/docs/document/content/user-manual/error-code/sql-error-code.cn.md b/docs/document/content/user-manual/error-code/sql-error-code.cn.md index 408bd36cb141f..9f3990fdf3baa 100644 --- a/docs/document/content/user-manual/error-code/sql-error-code.cn.md +++ b/docs/document/content/user-manual/error-code/sql-error-code.cn.md @@ -192,6 +192,7 @@ SQL 错误码以标准的 SQL State,Vendor Code 和详细错误信息提供, |-------------|-----------|---------------------------------------------------------| | 20100 | 42000 | Unsupported SQL node conversion for SQL statement '%s'. | | 20101 | 42000 | SQL federation does not support SQL '%s'. | +| 20102 | 42S02 | SQL federation schema not found SQL '%s'. | ### 读写分离 diff --git a/docs/document/content/user-manual/error-code/sql-error-code.en.md b/docs/document/content/user-manual/error-code/sql-error-code.en.md index 32b027a75d8cd..36f50139c9f8b 100644 --- a/docs/document/content/user-manual/error-code/sql-error-code.en.md +++ b/docs/document/content/user-manual/error-code/sql-error-code.en.md @@ -188,10 +188,11 @@ SQL error codes provide by standard `SQL State`, `Vendor Code` and `Reason`, whi ### SQL Federation -| Vendor Code | SQL State | Reason | -|-------------|-----------|---------------------------------------------------------| +| Vendor Code | SQL State | Reason | +|-------------|-----------|--------------------------------------------------------| | 20100 | 42000 | Unsupported SQL node conversion for SQL statement '%s'. | -| 20101 | 42000 | SQL federation does not support SQL '%s'. | +| 20101 | 42000 | SQL federation does not support SQL '%s'. | +| 20102 | 42S02 | SQL federation schema not found SQL '%s'. | ### Readwrite-splitting diff --git a/features/readwrite-splitting/distsql/handler/src/main/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/update/AlterReadwriteSplittingStorageUnitStatusExecutor.java b/features/readwrite-splitting/distsql/handler/src/main/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/update/AlterReadwriteSplittingStorageUnitStatusExecutor.java index 9ba188fd2c32b..f8dcce5ec9c13 100644 --- a/features/readwrite-splitting/distsql/handler/src/main/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/update/AlterReadwriteSplittingStorageUnitStatusExecutor.java +++ b/features/readwrite-splitting/distsql/handler/src/main/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/update/AlterReadwriteSplittingStorageUnitStatusExecutor.java @@ -77,7 +77,7 @@ private void checkBeforeUpdate(final AlterReadwriteSplittingStorageUnitStatusSta private void updateStatus(final ContextManager contextManager, final AlterReadwriteSplittingStorageUnitStatusStatement sqlStatement) { DataSourceState status = sqlStatement.isEnable() ? DataSourceState.ENABLED : DataSourceState.DISABLED; - new QualifiedDataSourceStatusService(contextManager.getMetaDataContexts().getPersistService().getRepository()) + new QualifiedDataSourceStatusService(contextManager.getRepository()) .changeStatus(database.getName(), sqlStatement.getRuleName(), sqlStatement.getStorageUnitName(), status); } diff --git a/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/engine/update/rdl/rule/engine/database/DatabaseRuleDefinitionExecuteEngine.java b/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/engine/update/rdl/rule/engine/database/DatabaseRuleDefinitionExecuteEngine.java index 22525d5feca6a..20d07acebce6b 100644 --- a/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/engine/update/rdl/rule/engine/database/DatabaseRuleDefinitionExecuteEngine.java +++ b/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/engine/update/rdl/rule/engine/database/DatabaseRuleDefinitionExecuteEngine.java @@ -55,7 +55,7 @@ public void executeUpdate() { checkBeforeUpdate(); RuleConfiguration currentRuleConfig = rule.map(ShardingSphereRule::getConfiguration).orElse(null); if (getRefreshStatus(rule.isPresent())) { - contextManager.getMetaDataContexts().getPersistService().getMetaDataVersionPersistService() + contextManager.getPersistServiceFacade().getMetaDataPersistService().getMetaDataVersionPersistService() .switchActiveVersion(DatabaseRuleOperatorFactory.newInstance(contextManager, executor).operate(sqlStatement, database, currentRuleConfig)); } } diff --git a/infra/reachability-metadata/src/main/resources/META-INF/native-image/org.apache.shardingsphere/generated-reachability-metadata/reflect-config.json b/infra/reachability-metadata/src/main/resources/META-INF/native-image/org.apache.shardingsphere/generated-reachability-metadata/reflect-config.json index 3128052dd888e..49cc77934df2c 100644 --- a/infra/reachability-metadata/src/main/resources/META-INF/native-image/org.apache.shardingsphere/generated-reachability-metadata/reflect-config.json +++ b/infra/reachability-metadata/src/main/resources/META-INF/native-image/org.apache.shardingsphere/generated-reachability-metadata/reflect-config.json @@ -528,18 +528,18 @@ "name":"org.apache.shardingsphere.infra.instance.metadata.proxy.ProxyInstanceMetaDataBuilder" }, { - "condition":{"typeReachable":"org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.status.compute.service.ComputeNodeStatusService"}, + "condition":{"typeReachable":"org.apache.shardingsphere.mode.service.ComputeNodePersistService"}, "name":"org.apache.shardingsphere.infra.instance.yaml.YamlComputeNodeData", "allDeclaredFields":true, "queryAllPublicMethods":true, "methods":[{"name":"","parameterTypes":[] }, {"name":"getAttribute","parameterTypes":[] }, {"name":"getVersion","parameterTypes":[] }, {"name":"setAttribute","parameterTypes":["java.lang.String"] }, {"name":"setVersion","parameterTypes":["java.lang.String"] }] }, { - "condition":{"typeReachable":"org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.status.compute.service.ComputeNodeStatusService"}, + "condition":{"typeReachable":"org.apache.shardingsphere.mode.service.ComputeNodePersistService"}, "name":"org.apache.shardingsphere.infra.instance.yaml.YamlComputeNodeDataBeanInfo" }, { - "condition":{"typeReachable":"org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.status.compute.service.ComputeNodeStatusService"}, + "condition":{"typeReachable":"org.apache.shardingsphere.mode.service.ComputeNodePersistService"}, "name":"org.apache.shardingsphere.infra.instance.yaml.YamlComputeNodeDataCustomizer" }, { @@ -572,26 +572,26 @@ "queryAllDeclaredMethods":true }, { - "condition":{"typeReachable":"org.apache.shardingsphere.mode.subsciber.EventSubscriberRegistry$$Lambda/0x00007feecf3840e0"}, + "condition":{"typeReachable":"org.apache.shardingsphere.mode.subsciber.EventSubscriberRegistry$$Lambda/0x00007f12133825d0"}, "name":"org.apache.shardingsphere.infra.util.eventbus.EventSubscriber" }, { - "condition":{"typeReachable":"org.apache.shardingsphere.infra.connection.refresher.type.table.CreateTableStatementSchemaRefresher"}, + "condition":{"typeReachable":"org.apache.shardingsphere.infra.util.yaml.YamlEngine"}, "name":"org.apache.shardingsphere.infra.util.yaml.YamlConfiguration", "queryAllPublicMethods":true }, { - "condition":{"typeReachable":"org.apache.shardingsphere.infra.util.yaml.YamlEngine"}, + "condition":{"typeReachable":"org.apache.shardingsphere.metadata.persist.service.schema.ShardingSphereTableRowDataPersistService"}, "name":"org.apache.shardingsphere.infra.util.yaml.YamlConfiguration", "queryAllPublicMethods":true }, { - "condition":{"typeReachable":"org.apache.shardingsphere.metadata.persist.service.schema.ShardingSphereTableRowDataPersistService"}, + "condition":{"typeReachable":"org.apache.shardingsphere.mode.metadata.refresher.type.table.CreateTableStatementSchemaRefresher"}, "name":"org.apache.shardingsphere.infra.util.yaml.YamlConfiguration", "queryAllPublicMethods":true }, { - "condition":{"typeReachable":"org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.status.compute.service.ComputeNodeStatusService"}, + "condition":{"typeReachable":"org.apache.shardingsphere.mode.service.ComputeNodePersistService"}, "name":"org.apache.shardingsphere.infra.util.yaml.YamlConfiguration", "queryAllPublicMethods":true }, @@ -694,11 +694,6 @@ "allDeclaredFields":true, "methods":[{"name":"","parameterTypes":[] }, {"name":"setName","parameterTypes":["java.lang.String"] }, {"name":"setUnique","parameterTypes":["boolean"] }] }, -{ - "condition":{"typeReachable":"org.apache.shardingsphere.infra.connection.refresher.type.table.CreateTableStatementSchemaRefresher"}, - "name":"org.apache.shardingsphere.infra.yaml.schema.pojo.YamlShardingSphereTable", - "queryAllPublicMethods":true -}, { "condition":{"typeReachable":"org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase"}, "name":"org.apache.shardingsphere.infra.yaml.schema.pojo.YamlShardingSphereTable", @@ -717,11 +712,16 @@ "methods":[{"name":"","parameterTypes":[] }, {"name":"getColumns","parameterTypes":[] }, {"name":"getConstraints","parameterTypes":[] }, {"name":"getIndexes","parameterTypes":[] }, {"name":"getName","parameterTypes":[] }, {"name":"getType","parameterTypes":[] }, {"name":"setColumns","parameterTypes":["java.util.Map"] }, {"name":"setIndexes","parameterTypes":["java.util.Map"] }, {"name":"setName","parameterTypes":["java.lang.String"] }, {"name":"setType","parameterTypes":["org.apache.shardingsphere.infra.database.core.metadata.database.enums.TableType"] }] }, { - "condition":{"typeReachable":"org.apache.shardingsphere.infra.connection.refresher.type.table.CreateTableStatementSchemaRefresher"}, + "condition":{"typeReachable":"org.apache.shardingsphere.mode.metadata.refresher.type.table.CreateTableStatementSchemaRefresher"}, + "name":"org.apache.shardingsphere.infra.yaml.schema.pojo.YamlShardingSphereTable", + "queryAllPublicMethods":true +}, +{ + "condition":{"typeReachable":"org.apache.shardingsphere.mode.metadata.refresher.type.table.CreateTableStatementSchemaRefresher"}, "name":"org.apache.shardingsphere.infra.yaml.schema.pojo.YamlShardingSphereTableBeanInfo" }, { - "condition":{"typeReachable":"org.apache.shardingsphere.infra.connection.refresher.type.table.CreateTableStatementSchemaRefresher"}, + "condition":{"typeReachable":"org.apache.shardingsphere.mode.metadata.refresher.type.table.CreateTableStatementSchemaRefresher"}, "name":"org.apache.shardingsphere.infra.yaml.schema.pojo.YamlShardingSphereTableCustomizer" }, { @@ -910,20 +910,10 @@ "name":"org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.process.subscriber.ClusterProcessSubscriber", "queryAllDeclaredMethods":true }, -{ - "condition":{"typeReachable":"org.apache.shardingsphere.mode.manager.cluster.ClusterContextManagerBuilder"}, - "name":"org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.status.cluster.subscriber.ClusterStateSubscriber", - "queryAllDeclaredMethods":true -}, { "condition":{"typeReachable":"org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.GovernanceWatcherFactory"}, "name":"org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.status.cluster.watcher.ClusterStateChangedWatcher" }, -{ - "condition":{"typeReachable":"org.apache.shardingsphere.mode.manager.cluster.ClusterContextManagerBuilder"}, - "name":"org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.status.compute.subscriber.ComputeNodeStatusSubscriber", - "queryAllDeclaredMethods":true -}, { "condition":{"typeReachable":"org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.GovernanceWatcherFactory"}, "name":"org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.status.compute.watcher.ComputeNodeStateChangedWatcher" @@ -960,6 +950,11 @@ "name":"org.apache.shardingsphere.mode.manager.cluster.coordinator.subscriber.StateChangedSubscriber", "methods":[{"name":"renew","parameterTypes":["org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.status.cluster.event.ClusterStateEvent"] }] }, +{ + "condition":{"typeReachable":"org.apache.shardingsphere.mode.service.PersistServiceFacade"}, + "name":"org.apache.shardingsphere.mode.manager.cluster.service.ClusterMetaDataManagerPersistServiceBuilder", + "methods":[{"name":"","parameterTypes":[] }] +}, { "condition":{"typeReachable":"org.apache.shardingsphere.infra.yaml.config.swapper.mode.YamlModeConfigurationSwapper"}, "name":"org.apache.shardingsphere.mode.manager.cluster.yaml.ClusterYamlPersistRepositoryConfigurationSwapper" @@ -968,6 +963,11 @@ "condition":{"typeReachable":"org.apache.shardingsphere.driver.jdbc.core.datasource.ShardingSphereDataSource"}, "name":"org.apache.shardingsphere.mode.manager.standalone.StandaloneContextManagerBuilder" }, +{ + "condition":{"typeReachable":"org.apache.shardingsphere.mode.service.PersistServiceFacade"}, + "name":"org.apache.shardingsphere.mode.manager.standalone.service.StandaloneMetaDataManagerPersistServiceBuilder", + "methods":[{"name":"","parameterTypes":[] }] +}, { "condition":{"typeReachable":"org.apache.shardingsphere.infra.yaml.config.swapper.mode.YamlModeConfigurationSwapper"}, "name":"org.apache.shardingsphere.mode.manager.standalone.yaml.StandaloneYamlPersistRepositoryConfigurationSwapper" diff --git a/infra/reachability-metadata/src/main/resources/META-INF/native-image/org.apache.shardingsphere/generated-reachability-metadata/resource-config.json b/infra/reachability-metadata/src/main/resources/META-INF/native-image/org.apache.shardingsphere/generated-reachability-metadata/resource-config.json index 521d5461281b1..db067f61bfa95 100644 --- a/infra/reachability-metadata/src/main/resources/META-INF/native-image/org.apache.shardingsphere/generated-reachability-metadata/resource-config.json +++ b/infra/reachability-metadata/src/main/resources/META-INF/native-image/org.apache.shardingsphere/generated-reachability-metadata/resource-config.json @@ -258,6 +258,9 @@ }, { "condition":{"typeReachable":"org.apache.shardingsphere.mode.manager.standalone.StandaloneContextManagerBuilder"}, "pattern":"\\QMETA-INF/services/org.apache.shardingsphere.mode.repository.standalone.StandalonePersistRepository\\E" + }, { + "condition":{"typeReachable":"org.apache.shardingsphere.mode.manager.standalone.StandaloneContextManagerBuilder"}, + "pattern":"\\QMETA-INF/services/org.apache.shardingsphere.mode.service.MetaDataManagerPersistServiceBuilder\\E" }, { "condition":{"typeReachable":"org.apache.shardingsphere.mode.metadata.MetaDataContextsFactory"}, "pattern":"\\QMETA-INF/services/org.apache.shardingsphere.mode.spi.RuleNodePathProvider\\E" diff --git a/jdbc/src/main/java/org/apache/shardingsphere/driver/jdbc/core/connection/DriverDatabaseConnectionManager.java b/jdbc/src/main/java/org/apache/shardingsphere/driver/jdbc/core/connection/DriverDatabaseConnectionManager.java index 85e675c4cb3ba..f4472436f1462 100644 --- a/jdbc/src/main/java/org/apache/shardingsphere/driver/jdbc/core/connection/DriverDatabaseConnectionManager.java +++ b/jdbc/src/main/java/org/apache/shardingsphere/driver/jdbc/core/connection/DriverDatabaseConnectionManager.java @@ -109,7 +109,7 @@ private Map getTrafficDataSourceMap(final String databaseNam if (rule.getStrategyRules().isEmpty()) { return Collections.emptyMap(); } - MetaDataPersistService persistService = contextManager.getMetaDataContexts().getPersistService(); + MetaDataPersistService persistService = contextManager.getPersistServiceFacade().getMetaDataPersistService(); String actualDatabaseName = contextManager.getMetaDataContexts().getMetaData().getDatabase(databaseName).getName(); Map propsMap = persistService.getDataSourceUnitService().load(actualDatabaseName); Preconditions.checkState(!propsMap.isEmpty(), "Can not get data source properties from meta data."); diff --git a/jdbc/src/test/java/org/apache/shardingsphere/driver/jdbc/core/connection/DriverDatabaseConnectionManagerTest.java b/jdbc/src/test/java/org/apache/shardingsphere/driver/jdbc/core/connection/DriverDatabaseConnectionManagerTest.java index a611dc0e6337b..9391da3a3940b 100644 --- a/jdbc/src/test/java/org/apache/shardingsphere/driver/jdbc/core/connection/DriverDatabaseConnectionManagerTest.java +++ b/jdbc/src/test/java/org/apache/shardingsphere/driver/jdbc/core/connection/DriverDatabaseConnectionManagerTest.java @@ -78,7 +78,7 @@ private ContextManager mockContextManager() throws SQLException { Map storageUnits = mockStorageUnits(); when(result.getStorageUnits(DefaultDatabase.LOGIC_NAME)).thenReturn(storageUnits); MetaDataPersistService persistService = mockMetaDataPersistService(); - when(result.getMetaDataContexts().getPersistService()).thenReturn(persistService); + when(result.getPersistServiceFacade().getMetaDataPersistService()).thenReturn(persistService); when(result.getMetaDataContexts().getMetaData().getGlobalRuleMetaData()).thenReturn( new RuleMetaData(Arrays.asList(mock(AuthorityRule.class, RETURNS_DEEP_STUBS), mock(TransactionRule.class, RETURNS_DEEP_STUBS), mock(TrafficRule.class, RETURNS_DEEP_STUBS)))); diff --git a/jdbc/src/test/java/org/apache/shardingsphere/driver/state/DriverStateContextTest.java b/jdbc/src/test/java/org/apache/shardingsphere/driver/state/DriverStateContextTest.java index 2989663944100..e164b600a18c5 100644 --- a/jdbc/src/test/java/org/apache/shardingsphere/driver/state/DriverStateContextTest.java +++ b/jdbc/src/test/java/org/apache/shardingsphere/driver/state/DriverStateContextTest.java @@ -31,6 +31,7 @@ import org.apache.shardingsphere.metadata.persist.MetaDataPersistService; import org.apache.shardingsphere.mode.manager.ContextManager; import org.apache.shardingsphere.mode.metadata.MetaDataContexts; +import org.apache.shardingsphere.mode.metadata.MetaDataContextsFactory; import org.apache.shardingsphere.mode.state.StateContext; import org.apache.shardingsphere.traffic.rule.TrafficRule; import org.apache.shardingsphere.transaction.rule.TransactionRule; @@ -65,7 +66,7 @@ void setUp() { TrafficRule trafficRule = mock(TrafficRule.class); AuthorityRule authorityRule = mock(AuthorityRule.class); RuleMetaData globalRuleMetaData = new RuleMetaData(Arrays.asList(transactionRule, trafficRule, authorityRule)); - MetaDataContexts metaDataContexts = new MetaDataContexts( + MetaDataContexts metaDataContexts = MetaDataContextsFactory.create( mock(MetaDataPersistService.class), new ShardingSphereMetaData(databases, mock(ResourceMetaData.class), globalRuleMetaData, new ConfigurationProperties(new Properties()))); when(contextManager.getMetaDataContexts()).thenReturn(metaDataContexts); when(contextManager.getComputeNodeInstanceContext().getInstance().getState()).thenReturn(new InstanceStateContext()); diff --git a/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/job/api/PipelineAPIFactory.java b/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/job/api/PipelineAPIFactory.java index 2d169478de261..b8ad17805f071 100644 --- a/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/job/api/PipelineAPIFactory.java +++ b/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/job/api/PipelineAPIFactory.java @@ -63,7 +63,7 @@ public static PipelineGovernanceFacade getPipelineGovernanceFacade(final Pipelin @Override protected PipelineGovernanceFacade initialize() { ContextManager contextManager = PipelineContextManager.getContext(contextKey).getContextManager(); - return new PipelineGovernanceFacade((ClusterPersistRepository) contextManager.getMetaDataContexts().getPersistService().getRepository()); + return new PipelineGovernanceFacade((ClusterPersistRepository) contextManager.getPersistServiceFacade().getMetaDataPersistService().getRepository()); } }).get(); } diff --git a/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/util/PipelineDistributedBarrier.java b/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/util/PipelineDistributedBarrier.java index 70035e61e100e..88ddf2a17570e 100644 --- a/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/util/PipelineDistributedBarrier.java +++ b/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/util/PipelineDistributedBarrier.java @@ -163,7 +163,7 @@ private final class PersistRepositoryLazyInitializer extends LazyInitializer versions = repository.getChildrenKeys(TableMetaDataNode.getTableVersionsNode(databaseName, schemaName, tableName)); String nextActiveVersion = versions.isEmpty() ? DEFAULT_VERSION : String.valueOf(Integer.parseInt(versions.get(0)) + 1); - repository.persist(TableMetaDataNode.getTableVersionNode(databaseName, schemaName, tableName, nextActiveVersion), - YamlEngine.marshal(new YamlTableSwapper().swapToYamlConfiguration(entry.getValue()))); + if (entry.getValue() != null) { + repository.persist(TableMetaDataNode.getTableVersionNode(databaseName, schemaName, tableName, nextActiveVersion), + YamlEngine.marshal(new YamlTableSwapper().swapToYamlConfiguration(entry.getValue()))); + } if (Strings.isNullOrEmpty(getActiveVersion(databaseName, schemaName, tableName))) { repository.persist(TableMetaDataNode.getTableActiveVersionNode(databaseName, schemaName, tableName), DEFAULT_VERSION); } diff --git a/kernel/sql-federation/core/src/main/java/org/apache/shardingsphere/sqlfederation/engine/SQLFederationEngine.java b/kernel/sql-federation/core/src/main/java/org/apache/shardingsphere/sqlfederation/engine/SQLFederationEngine.java index c82b5c3109896..a81a1a08d5c54 100644 --- a/kernel/sql-federation/core/src/main/java/org/apache/shardingsphere/sqlfederation/engine/SQLFederationEngine.java +++ b/kernel/sql-federation/core/src/main/java/org/apache/shardingsphere/sqlfederation/engine/SQLFederationEngine.java @@ -59,6 +59,7 @@ import org.apache.shardingsphere.sqlfederation.optimizer.SQLFederationExecutionPlan; import org.apache.shardingsphere.sqlfederation.optimizer.context.OptimizerContext; import org.apache.shardingsphere.sqlfederation.optimizer.context.planner.OptimizerMetaData; +import org.apache.shardingsphere.sqlfederation.optimizer.exception.SQLFederationSchemaNotFoundException; import org.apache.shardingsphere.sqlfederation.optimizer.exception.SQLFederationUnsupportedSQLException; import org.apache.shardingsphere.sqlfederation.optimizer.metadata.schema.SQLFederationTable; import org.apache.shardingsphere.sqlfederation.optimizer.planner.cache.ExecutionPlanCacheKey; @@ -181,6 +182,7 @@ public ResultSet executeQuery(final DriverExecutionPrepareEngine new SQLFederationSchemaNotFoundException(federationContext.getQueryContext().getSql())); SQLFederationExecutionPlan executionPlan = compileQuery(prepareEngine, callback, federationContext, databaseName, schemaName, sqlFederationSchema, converter); resultSet = executePlan(federationContext, executionPlan, validator, converter, sqlFederationSchema); return resultSet; diff --git a/kernel/sql-federation/optimizer/src/main/java/org/apache/shardingsphere/sqlfederation/optimizer/exception/SQLFederationSchemaNotFoundException.java b/kernel/sql-federation/optimizer/src/main/java/org/apache/shardingsphere/sqlfederation/optimizer/exception/SQLFederationSchemaNotFoundException.java new file mode 100644 index 0000000000000..a2f8de499ec46 --- /dev/null +++ b/kernel/sql-federation/optimizer/src/main/java/org/apache/shardingsphere/sqlfederation/optimizer/exception/SQLFederationSchemaNotFoundException.java @@ -0,0 +1,32 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.shardingsphere.sqlfederation.optimizer.exception; + +import org.apache.shardingsphere.infra.exception.core.external.sql.sqlstate.XOpenSQLState; + +/** + * SQL federation schema not found exception. + */ +public final class SQLFederationSchemaNotFoundException extends SQLFederationSQLException { + + private static final long serialVersionUID = 1273654561551534516L; + + public SQLFederationSchemaNotFoundException(final String sql) { + super(XOpenSQLState.NOT_FOUND, 2, "SQL federation schema not found SQL '%s'.", sql); + } +} diff --git a/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/ContextManager.java b/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/ContextManager.java index 78905598a883f..159240a79501e 100644 --- a/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/ContextManager.java +++ b/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/ContextManager.java @@ -44,7 +44,9 @@ import org.apache.shardingsphere.mode.manager.switcher.ResourceSwitchManager; import org.apache.shardingsphere.mode.manager.switcher.SwitchingResource; import org.apache.shardingsphere.mode.metadata.MetaDataContexts; +import org.apache.shardingsphere.mode.metadata.MetaDataContextsFactory; import org.apache.shardingsphere.mode.service.PersistServiceFacade; +import org.apache.shardingsphere.mode.spi.PersistRepository; import org.apache.shardingsphere.mode.state.StateContext; import java.sql.SQLException; @@ -76,15 +78,18 @@ public final class ContextManager implements AutoCloseable { private final PersistServiceFacade persistServiceFacade; - public ContextManager(final MetaDataContexts metaDataContexts, final ComputeNodeInstanceContext computeNodeInstanceContext) { + private final PersistRepository repository; + + public ContextManager(final MetaDataContexts metaDataContexts, final ComputeNodeInstanceContext computeNodeInstanceContext, final PersistRepository repository) { this.metaDataContexts = new AtomicReference<>(metaDataContexts); this.computeNodeInstanceContext = computeNodeInstanceContext; - shardingSphereDatabaseContextManager = new ShardingSphereDatabaseContextManager(this.metaDataContexts); - configurationContextManager = new ConfigurationContextManager(this.metaDataContexts, computeNodeInstanceContext); - resourceMetaDataContextManager = new ResourceMetaDataContextManager(this.metaDataContexts); + this.repository = repository; + persistServiceFacade = new PersistServiceFacade(repository, computeNodeInstanceContext.getModeConfiguration(), this); + shardingSphereDatabaseContextManager = new ShardingSphereDatabaseContextManager(this.metaDataContexts, persistServiceFacade); + configurationContextManager = new ConfigurationContextManager(this.metaDataContexts, computeNodeInstanceContext, persistServiceFacade); + resourceMetaDataContextManager = new ResourceMetaDataContextManager(this.metaDataContexts, persistServiceFacade); executorEngine = ExecutorEngine.createExecutorEngineWithSize(metaDataContexts.getMetaData().getProps().getValue(ConfigurationPropertyKey.KERNEL_EXECUTOR_SIZE)); stateContext = new StateContext(); - persistServiceFacade = new PersistServiceFacade(metaDataContexts.getPersistService().getRepository(), computeNodeInstanceContext.getModeConfiguration(), this); } /** @@ -137,7 +142,7 @@ public Map getStorageUnits(final String databaseName) { public void refreshDatabaseMetaData(final ShardingSphereDatabase database, final boolean force) { try { MetaDataContexts reloadedMetaDataContexts = createMetaDataContexts(database); - MetaDataPersistService persistService = metaDataContexts.get().getPersistService(); + MetaDataPersistService persistService = persistServiceFacade.getMetaDataPersistService(); if (force) { metaDataContexts.set(reloadedMetaDataContexts); metaDataContexts.get().getMetaData().getDatabase(database.getName()).getSchemas() @@ -164,14 +169,14 @@ public void refreshTableMetaData(final ShardingSphereDatabase database) { deletedSchemaNames(database.getName(), reloadedMetaDataContexts.getMetaData().getDatabase(database.getName()), database); metaDataContexts.set(reloadedMetaDataContexts); metaDataContexts.get().getMetaData().getDatabase(database.getName()).getSchemas() - .forEach((schemaName, schema) -> metaDataContexts.get().getPersistService().getDatabaseMetaDataService().compareAndPersist(database.getName(), schemaName, schema)); + .forEach((schemaName, schema) -> persistServiceFacade.getMetaDataPersistService().getDatabaseMetaDataService().compareAndPersist(database.getName(), schemaName, schema)); } catch (final SQLException ex) { log.error("Refresh table meta data: {} failed", database.getName(), ex); } } private MetaDataContexts createMetaDataContexts(final ShardingSphereDatabase database) throws SQLException { - MetaDataPersistService metaDataPersistService = metaDataContexts.get().getPersistService(); + MetaDataPersistService metaDataPersistService = persistServiceFacade.getMetaDataPersistService(); Map dataSourcePoolPropsFromRegCenter = metaDataPersistService.getDataSourceUnitService().load(database.getName()); SwitchingResource switchingResource = new ResourceSwitchManager().alterStorageUnit(database.getResourceMetaData(), dataSourcePoolPropsFromRegCenter); metaDataContexts.get().getMetaData().getDatabases().putAll(configurationContextManager.renewDatabase(database, switchingResource)); @@ -180,7 +185,7 @@ private MetaDataContexts createMetaDataContexts(final ShardingSphereDatabase dat ConfigurationProperties props = new ConfigurationProperties(metaDataPersistService.getPropsService().load()); Collection globalRuleConfigs = metaDataPersistService.getGlobalRuleService().load(); RuleMetaData changedGlobalMetaData = new RuleMetaData(GlobalRulesBuilder.buildRules(globalRuleConfigs, changedDatabases, props)); - MetaDataContexts result = new MetaDataContexts(metaDataPersistService, + MetaDataContexts result = MetaDataContextsFactory.create(metaDataPersistService, new ShardingSphereMetaData(changedDatabases, metaDataContexts.get().getMetaData().getGlobalResourceMetaData(), changedGlobalMetaData, props)); switchingResource.closeStaleDataSources(); return result; @@ -195,7 +200,7 @@ private MetaDataContexts createMetaDataContexts(final ShardingSphereDatabase dat */ public void deletedSchemaNames(final String databaseName, final ShardingSphereDatabase reloadDatabase, final ShardingSphereDatabase currentDatabase) { GenericSchemaManager.getToBeDeletedSchemaNames(reloadDatabase.getSchemas(), currentDatabase.getSchemas()).keySet() - .forEach(each -> metaDataContexts.get().getPersistService().getDatabaseMetaDataService().dropSchema(databaseName, each)); + .forEach(each -> persistServiceFacade.getMetaDataPersistService().getDatabaseMetaDataService().dropSchema(databaseName, each)); } /** @@ -210,11 +215,11 @@ public void reloadSchema(final ShardingSphereDatabase database, final String sch ShardingSphereSchema reloadedSchema = loadSchema(database, schemaName, dataSourceName); if (reloadedSchema.getTables().isEmpty()) { database.dropSchema(schemaName); - metaDataContexts.get().getPersistService().getDatabaseMetaDataService().dropSchema(database.getName(), + persistServiceFacade.getMetaDataPersistService().getDatabaseMetaDataService().dropSchema(database.getName(), schemaName); } else { database.addSchema(schemaName, reloadedSchema); - metaDataContexts.get().getPersistService().getDatabaseMetaDataService() + persistServiceFacade.getMetaDataPersistService().getDatabaseMetaDataService() .compareAndPersist(database.getName(), schemaName, reloadedSchema); } } catch (final SQLException ex) { @@ -229,7 +234,7 @@ private ShardingSphereSchema loadSchema(final ShardingSphereDatabase database, f Collections.singletonMap(dataSourceName, database.getResourceMetaData().getStorageUnits().get(dataSourceName).getDataSource()), database.getRuleMetaData().getRules(), metaDataContexts.get().getMetaData().getProps(), schemaName); ShardingSphereSchema result = GenericSchemaBuilder.build(material).get(schemaName); - result.getViews().putAll(metaDataContexts.get().getPersistService().getDatabaseMetaDataService().getViewMetaDataPersistService().load(database.getName(), schemaName)); + result.getViews().putAll(persistServiceFacade.getMetaDataPersistService().getDatabaseMetaDataService().getViewMetaDataPersistService().load(database.getName(), schemaName)); return result; } @@ -272,7 +277,7 @@ public void reloadTable(final ShardingSphereDatabase database, final String sche private void persistTable(final ShardingSphereDatabase database, final String schemaName, final String tableName, final GenericSchemaBuilderMaterial material) throws SQLException { ShardingSphereSchema schema = GenericSchemaBuilder.build(Collections.singleton(tableName), material).getOrDefault(schemaName, new ShardingSphereSchema()); - metaDataContexts.get().getPersistService().getDatabaseMetaDataService().getTableMetaDataPersistService() + persistServiceFacade.getMetaDataPersistService().getDatabaseMetaDataService().getTableMetaDataPersistService() .persist(database.getName(), schemaName, Collections.singletonMap(tableName, schema.getTable(tableName))); } @@ -280,5 +285,6 @@ private void persistTable(final ShardingSphereDatabase database, final String sc public void close() { executorEngine.close(); metaDataContexts.get().close(); + repository.close(); } } 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 719f12fc278a1..4684a182c355c 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 @@ -40,14 +40,16 @@ import org.apache.shardingsphere.infra.rule.builder.database.DatabaseRulesBuilder; import org.apache.shardingsphere.infra.rule.builder.global.GlobalRulesBuilder; import org.apache.shardingsphere.infra.yaml.config.pojo.rule.YamlRuleConfiguration; -import org.apache.shardingsphere.metadata.persist.MetaDataPersistService; -import org.apache.shardingsphere.mode.tuple.annotation.RepositoryTupleEntity; import org.apache.shardingsphere.infra.yaml.config.swapper.rule.YamlRuleConfigurationSwapperEngine; import org.apache.shardingsphere.metadata.factory.ExternalMetaDataFactory; import org.apache.shardingsphere.metadata.factory.InternalMetaDataFactory; +import org.apache.shardingsphere.metadata.persist.MetaDataPersistService; import org.apache.shardingsphere.mode.manager.switcher.ResourceSwitchManager; import org.apache.shardingsphere.mode.manager.switcher.SwitchingResource; import org.apache.shardingsphere.mode.metadata.MetaDataContexts; +import org.apache.shardingsphere.mode.metadata.MetaDataContextsFactory; +import org.apache.shardingsphere.mode.service.PersistServiceFacade; +import org.apache.shardingsphere.mode.tuple.annotation.RepositoryTupleEntity; import org.apache.shardingsphere.transaction.rule.TransactionRule; import javax.sql.DataSource; @@ -75,6 +77,8 @@ public final class ConfigurationContextManager { private final ComputeNodeInstanceContext computeNodeInstanceContext; + private final PersistServiceFacade persistServiceFacade; + /** * Register storage unit. * @@ -131,7 +135,7 @@ private void buildNewMetaDataContext(final String databaseName, final SwitchingR MetaDataContexts reloadMetaDataContexts = createMetaDataContexts(databaseName, false, switchingResource, null); persistSchemaMetaData(databaseName, reloadMetaDataContexts, isDropConfig); Optional.ofNullable(reloadMetaDataContexts.getStatistics().getDatabaseData().get(databaseName)) - .ifPresent(optional -> optional.getSchemaData().forEach((schemaName, schemaData) -> reloadMetaDataContexts.getPersistService().getShardingSphereDataPersistService() + .ifPresent(optional -> optional.getSchemaData().forEach((schemaName, schemaData) -> persistServiceFacade.getMetaDataPersistService().getShardingSphereDataPersistService() .persist(databaseName, schemaName, schemaData, metaDataContexts.get().getMetaData().getDatabases()))); alterSchemaMetaData(databaseName, reloadMetaDataContexts.getMetaData().getDatabase(databaseName), metaDataContexts.get().getMetaData().getDatabase(databaseName), isDropConfig); metaDataContexts.set(reloadMetaDataContexts); @@ -141,10 +145,10 @@ private void buildNewMetaDataContext(final String databaseName, final SwitchingR private void persistSchemaMetaData(final String databaseName, final MetaDataContexts reloadMetaDataContexts, final boolean isDropConfig) { if (isDropConfig) { - reloadMetaDataContexts.getMetaData().getDatabase(databaseName).getSchemas().forEach((schemaName, schema) -> reloadMetaDataContexts.getPersistService().getDatabaseMetaDataService() + reloadMetaDataContexts.getMetaData().getDatabase(databaseName).getSchemas().forEach((schemaName, schema) -> persistServiceFacade.getMetaDataPersistService().getDatabaseMetaDataService() .persistByDropConfiguration(reloadMetaDataContexts.getMetaData().getDatabase(databaseName).getName(), schemaName, schema)); } else { - reloadMetaDataContexts.getMetaData().getDatabase(databaseName).getSchemas().forEach((schemaName, schema) -> reloadMetaDataContexts.getPersistService().getDatabaseMetaDataService() + reloadMetaDataContexts.getMetaData().getDatabase(databaseName).getSchemas().forEach((schemaName, schema) -> persistServiceFacade.getMetaDataPersistService().getDatabaseMetaDataService() .persistByAlterConfiguration(reloadMetaDataContexts.getMetaData().getDatabase(databaseName).getName(), schemaName, schema)); } } @@ -232,11 +236,11 @@ public void alterSchemaMetaData(final String databaseName, final ShardingSphereD Map toBeAlterSchemas = GenericSchemaManager.getToBeDeletedTablesBySchemas(reloadDatabase.getSchemas(), currentDatabase.getSchemas()); Map toBeAddedSchemas = GenericSchemaManager.getToBeAddedTablesBySchemas(reloadDatabase.getSchemas(), currentDatabase.getSchemas()); if (isDropConfig) { - toBeAddedSchemas.forEach((key, value) -> metaDataContexts.get().getPersistService().getDatabaseMetaDataService().persistByDropConfiguration(databaseName, key, value)); + toBeAddedSchemas.forEach((key, value) -> persistServiceFacade.getMetaDataPersistService().getDatabaseMetaDataService().persistByDropConfiguration(databaseName, key, value)); } else { - toBeAddedSchemas.forEach((key, value) -> metaDataContexts.get().getPersistService().getDatabaseMetaDataService().persistByAlterConfiguration(databaseName, key, value)); + toBeAddedSchemas.forEach((key, value) -> persistServiceFacade.getMetaDataPersistService().getDatabaseMetaDataService().persistByAlterConfiguration(databaseName, key, value)); } - toBeAlterSchemas.forEach((key, value) -> metaDataContexts.get().getPersistService().getDatabaseMetaDataService().delete(databaseName, key, value)); + toBeAlterSchemas.forEach((key, value) -> persistServiceFacade.getMetaDataPersistService().getDatabaseMetaDataService().delete(databaseName, key, value)); } /** @@ -319,7 +323,7 @@ public synchronized Map createChangedDatabases(f DatabaseConfiguration toBeCreatedDatabaseConfig = getDatabaseConfiguration( metaDataContexts.get().getMetaData().getDatabase(databaseName).getResourceMetaData(), switchingResource, toBeCreatedRuleConfigs); ShardingSphereDatabase changedDatabase = createChangedDatabase(metaDataContexts.get().getMetaData().getDatabase(databaseName).getName(), internalLoadMetaData, - metaDataContexts.get().getPersistService(), toBeCreatedDatabaseConfig, metaDataContexts.get().getMetaData().getProps(), computeNodeInstanceContext); + persistServiceFacade.getMetaDataPersistService(), toBeCreatedDatabaseConfig, metaDataContexts.get().getMetaData().getProps(), computeNodeInstanceContext); Map result = new LinkedHashMap<>(metaDataContexts.get().getMetaData().getDatabases()); result.put(databaseName.toLowerCase(), changedDatabase); return result; @@ -365,7 +369,7 @@ private Map newShardingSphereSchemas(final Shardin public Map newShardingSphereDatabase(final ShardingSphereDatabase originalDatabase) { return Collections.singletonMap(originalDatabase.getName().toLowerCase(), new ShardingSphereDatabase(originalDatabase.getName(), originalDatabase.getProtocolType(), originalDatabase.getResourceMetaData(), originalDatabase.getRuleMetaData(), - metaDataContexts.get().getPersistService().getDatabaseMetaDataService().loadSchemas(originalDatabase.getName()))); + persistServiceFacade.getMetaDataPersistService().getDatabaseMetaDataService().loadSchemas(originalDatabase.getName()))); } /** @@ -413,6 +417,6 @@ public synchronized void alterProperties(final Properties props) { } private MetaDataContexts newMetaDataContexts(final ShardingSphereMetaData metaData) { - return new MetaDataContexts(metaDataContexts.get().getPersistService(), metaData); + return MetaDataContextsFactory.create(persistServiceFacade.getMetaDataPersistService(), metaData); } } diff --git a/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/context/ResourceMetaDataContextManager.java b/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/context/ResourceMetaDataContextManager.java index a7d02ddbd3bd6..3463544e13db4 100644 --- a/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/context/ResourceMetaDataContextManager.java +++ b/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/context/ResourceMetaDataContextManager.java @@ -30,7 +30,9 @@ import org.apache.shardingsphere.infra.rule.scope.GlobalRule; import org.apache.shardingsphere.infra.rule.scope.GlobalRule.GlobalRuleChangedType; import org.apache.shardingsphere.mode.metadata.MetaDataContexts; +import org.apache.shardingsphere.mode.metadata.MetaDataContextsFactory; import org.apache.shardingsphere.mode.metadata.refresher.util.TableRefreshUtils; +import org.apache.shardingsphere.mode.service.PersistServiceFacade; import java.util.Collections; import java.util.Optional; @@ -44,6 +46,8 @@ public final class ResourceMetaDataContextManager { private final AtomicReference metaDataContexts; + private final PersistServiceFacade persistServiceFacade; + /** * Add database. * @@ -55,7 +59,7 @@ public synchronized void addDatabase(final String databaseName) { } DatabaseType protocolType = DatabaseTypeEngine.getProtocolType(Collections.emptyMap(), metaDataContexts.get().getMetaData().getProps()); metaDataContexts.get().getMetaData().addDatabase(databaseName, protocolType, metaDataContexts.get().getMetaData().getProps()); - metaDataContexts.set(new MetaDataContexts(metaDataContexts.get().getPersistService(), metaDataContexts.get().getMetaData())); + metaDataContexts.set(MetaDataContextsFactory.create(persistServiceFacade.getMetaDataPersistService(), metaDataContexts.get().getMetaData())); } /** diff --git a/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/context/ShardingSphereDatabaseContextManager.java b/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/context/ShardingSphereDatabaseContextManager.java index 09b2d2df2aaf4..ccf49cbc12f3a 100644 --- a/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/context/ShardingSphereDatabaseContextManager.java +++ b/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/context/ShardingSphereDatabaseContextManager.java @@ -25,6 +25,7 @@ import org.apache.shardingsphere.infra.yaml.data.pojo.YamlShardingSphereRowData; import org.apache.shardingsphere.infra.yaml.data.swapper.YamlShardingSphereRowDataSwapper; import org.apache.shardingsphere.mode.metadata.MetaDataContexts; +import org.apache.shardingsphere.mode.service.PersistServiceFacade; import java.util.ArrayList; import java.util.List; @@ -38,6 +39,8 @@ public final class ShardingSphereDatabaseContextManager { private final AtomicReference metaDataContexts; + private final PersistServiceFacade persistServiceFacade; + /** * Add ShardingSphere database data. * diff --git a/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/MetaDataContexts.java b/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/MetaDataContexts.java index b9a52ce3c9e69..e261e2354f7ef 100644 --- a/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/MetaDataContexts.java +++ b/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/MetaDataContexts.java @@ -19,24 +19,12 @@ import lombok.Getter; import lombok.SneakyThrows; -import org.apache.shardingsphere.infra.database.core.metadata.database.DialectDatabaseMetaData; -import org.apache.shardingsphere.infra.database.core.spi.DatabaseTypedSPILoader; -import org.apache.shardingsphere.infra.database.core.type.DatabaseType; -import org.apache.shardingsphere.infra.database.core.type.DatabaseTypeRegistry; import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData; -import org.apache.shardingsphere.infra.metadata.statistics.ShardingSphereDatabaseData; -import org.apache.shardingsphere.infra.metadata.statistics.ShardingSphereSchemaData; import org.apache.shardingsphere.infra.metadata.statistics.ShardingSphereStatistics; -import org.apache.shardingsphere.infra.metadata.statistics.ShardingSphereTableData; -import org.apache.shardingsphere.infra.metadata.statistics.builder.ShardingSphereStatisticsBuilder; import org.apache.shardingsphere.infra.rule.ShardingSphereRule; -import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader; -import org.apache.shardingsphere.metadata.persist.MetaDataPersistService; import java.util.Collection; import java.util.LinkedList; -import java.util.Map.Entry; -import java.util.Optional; /** * Meta data contexts. @@ -44,64 +32,18 @@ @Getter public final class MetaDataContexts implements AutoCloseable { - private final MetaDataPersistService persistService; - private final ShardingSphereMetaData metaData; private final ShardingSphereStatistics statistics; - public MetaDataContexts(final MetaDataPersistService persistService, final ShardingSphereMetaData metaData) { - this.persistService = persistService; + public MetaDataContexts(final ShardingSphereMetaData metaData, final ShardingSphereStatistics statistics) { this.metaData = metaData; - statistics = initStatistics(metaData); - } - - private ShardingSphereStatistics initStatistics(final ShardingSphereMetaData metaData) { - if (metaData.getDatabases().isEmpty()) { - return new ShardingSphereStatistics(); - } - DatabaseType protocolType = metaData.getDatabases().values().iterator().next().getProtocolType(); - DialectDatabaseMetaData dialectDatabaseMetaData = new DatabaseTypeRegistry(protocolType).getDialectDatabaseMetaData(); - // TODO can `protocolType instanceof SchemaSupportedDatabaseType ? "PostgreSQL" : protocolType.getType()` replace to trunk database type? - DatabaseType databaseType = dialectDatabaseMetaData.getDefaultSchema().isPresent() ? TypedSPILoader.getService(DatabaseType.class, "PostgreSQL") : protocolType; - Optional statisticsBuilder = DatabaseTypedSPILoader.findService(ShardingSphereStatisticsBuilder.class, databaseType); - if (!statisticsBuilder.isPresent()) { - return new ShardingSphereStatistics(); - } - ShardingSphereStatistics result = statisticsBuilder.get().build(metaData); - Optional loadedStatistics = persistService.getShardingSphereDataPersistService().load(metaData); - loadedStatistics.ifPresent(optional -> useLoadedToReplaceInit(result, optional)); - return result; - } - - private void useLoadedToReplaceInit(final ShardingSphereStatistics initStatistics, final ShardingSphereStatistics loadedStatistics) { - for (Entry entry : initStatistics.getDatabaseData().entrySet()) { - if (loadedStatistics.getDatabaseData().containsKey(entry.getKey())) { - useLoadedToReplaceInitByDatabaseData(entry.getValue(), loadedStatistics.getDatabaseData().get(entry.getKey())); - } - } - } - - private void useLoadedToReplaceInitByDatabaseData(final ShardingSphereDatabaseData initDatabaseData, final ShardingSphereDatabaseData loadedDatabaseData) { - for (Entry entry : initDatabaseData.getSchemaData().entrySet()) { - if (loadedDatabaseData.getSchemaData().containsKey(entry.getKey())) { - useLoadedToReplaceInitBySchemaData(entry.getValue(), loadedDatabaseData.getSchemaData().get(entry.getKey())); - } - } - } - - private void useLoadedToReplaceInitBySchemaData(final ShardingSphereSchemaData initSchemaData, final ShardingSphereSchemaData loadedSchemaData) { - for (Entry entry : initSchemaData.getTableData().entrySet()) { - if (loadedSchemaData.getTableData().containsKey(entry.getKey())) { - entry.setValue(loadedSchemaData.getTableData().get(entry.getKey())); - } - } + this.statistics = statistics; } @SneakyThrows(Exception.class) @Override public void close() { - persistService.getRepository().close(); for (ShardingSphereRule each : getAllRules()) { if (each instanceof AutoCloseable) { ((AutoCloseable) each).close(); 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 c46380f277bbc..a1217a7220248 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 @@ -25,6 +25,10 @@ import org.apache.shardingsphere.infra.config.database.impl.DataSourceGeneratedDatabaseConfiguration; import org.apache.shardingsphere.infra.config.props.ConfigurationProperties; import org.apache.shardingsphere.infra.config.rule.RuleConfiguration; +import org.apache.shardingsphere.infra.database.core.metadata.database.DialectDatabaseMetaData; +import org.apache.shardingsphere.infra.database.core.spi.DatabaseTypedSPILoader; +import org.apache.shardingsphere.infra.database.core.type.DatabaseType; +import org.apache.shardingsphere.infra.database.core.type.DatabaseTypeRegistry; import org.apache.shardingsphere.infra.datasource.pool.config.DataSourceConfiguration; import org.apache.shardingsphere.infra.datasource.pool.destroyer.DataSourcePoolDestroyer; import org.apache.shardingsphere.infra.instance.ComputeNodeInstanceContext; @@ -33,14 +37,20 @@ import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; import org.apache.shardingsphere.infra.metadata.database.resource.ResourceMetaData; import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData; +import org.apache.shardingsphere.infra.metadata.statistics.ShardingSphereDatabaseData; +import org.apache.shardingsphere.infra.metadata.statistics.ShardingSphereSchemaData; +import org.apache.shardingsphere.infra.metadata.statistics.ShardingSphereStatistics; +import org.apache.shardingsphere.infra.metadata.statistics.ShardingSphereTableData; +import org.apache.shardingsphere.infra.metadata.statistics.builder.ShardingSphereStatisticsBuilder; import org.apache.shardingsphere.infra.rule.builder.global.GlobalRulesBuilder; +import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader; import org.apache.shardingsphere.infra.state.datasource.DataSourceState; import org.apache.shardingsphere.infra.state.datasource.DataSourceStateManager; import org.apache.shardingsphere.metadata.factory.ExternalMetaDataFactory; import org.apache.shardingsphere.metadata.factory.InternalMetaDataFactory; import org.apache.shardingsphere.metadata.persist.MetaDataPersistService; -import org.apache.shardingsphere.mode.storage.QualifiedDataSourceStatus; import org.apache.shardingsphere.mode.manager.ContextManagerBuilderParameter; +import org.apache.shardingsphere.mode.storage.QualifiedDataSourceStatus; import javax.sql.DataSource; import java.sql.SQLException; @@ -51,6 +61,7 @@ import java.util.List; import java.util.Map; import java.util.Map.Entry; +import java.util.Optional; import java.util.stream.Collectors; /** @@ -99,14 +110,27 @@ public static MetaDataContexts create(final MetaDataPersistService persistServic : ExternalMetaDataFactory.create(effectiveDatabaseConfigs, props, computeNodeInstanceContext); ResourceMetaData globalResourceMetaData = new ResourceMetaData(globalDataSources); RuleMetaData globalRuleMetaData = new RuleMetaData(GlobalRulesBuilder.buildRules(globalRuleConfigs, databases, props)); - MetaDataContexts result = new MetaDataContexts(persistService, new ShardingSphereMetaData(databases, globalResourceMetaData, globalRuleMetaData, props)); + ShardingSphereMetaData shardingSphereMetaData = new ShardingSphereMetaData(databases, globalResourceMetaData, globalRuleMetaData, props); + ShardingSphereStatistics shardingSphereStatistics = initStatistics(persistService, shardingSphereMetaData); + MetaDataContexts result = new MetaDataContexts(shardingSphereMetaData, shardingSphereStatistics); if (!isDatabaseMetaDataExisted) { - persistDatabaseConfigurations(result, param); - persistMetaData(result); + persistDatabaseConfigurations(result, param, persistService); + persistMetaData(result, persistService); } return result; } + /** + * Create meta data contexts. + * + * @param persistService meta data persist service + * @param metaData shardingsphere meta data + * @return meta data contexts + */ + public static MetaDataContexts create(final MetaDataPersistService persistService, final ShardingSphereMetaData metaData) { + return new MetaDataContexts(metaData, initStatistics(persistService, metaData)); + } + private static Collection getDatabaseNames(final ComputeNodeInstanceContext computeNodeInstanceContext, final Map databaseConfigs, final MetaDataPersistService persistService) { return computeNodeInstanceContext.getInstance().getMetaData() instanceof JDBCInstanceMetaData ? databaseConfigs.keySet() : persistService.getDatabaseMetaDataService().loadAllDatabaseNames(); @@ -152,21 +176,63 @@ private static Map getStorageDataSourceStates(final Map return result; } - private static void persistDatabaseConfigurations(final MetaDataContexts metadataContexts, final ContextManagerBuilderParameter param) { - metadataContexts.getPersistService().persistGlobalRuleConfiguration(param.getGlobalRuleConfigs(), param.getProps()); + private static ShardingSphereStatistics initStatistics(final MetaDataPersistService persistService, final ShardingSphereMetaData metaData) { + if (metaData.getDatabases().isEmpty()) { + return new ShardingSphereStatistics(); + } + DatabaseType protocolType = metaData.getDatabases().values().iterator().next().getProtocolType(); + DialectDatabaseMetaData dialectDatabaseMetaData = new DatabaseTypeRegistry(protocolType).getDialectDatabaseMetaData(); + // TODO can `protocolType instanceof SchemaSupportedDatabaseType ? "PostgreSQL" : protocolType.getType()` replace to trunk database type? + DatabaseType databaseType = dialectDatabaseMetaData.getDefaultSchema().isPresent() ? TypedSPILoader.getService(DatabaseType.class, "PostgreSQL") : protocolType; + Optional statisticsBuilder = DatabaseTypedSPILoader.findService(ShardingSphereStatisticsBuilder.class, databaseType); + if (!statisticsBuilder.isPresent()) { + return new ShardingSphereStatistics(); + } + ShardingSphereStatistics result = statisticsBuilder.get().build(metaData); + Optional loadedStatistics = persistService.getShardingSphereDataPersistService().load(metaData); + loadedStatistics.ifPresent(optional -> useLoadedToReplaceInit(result, optional)); + return result; + } + + private static void useLoadedToReplaceInit(final ShardingSphereStatistics initStatistics, final ShardingSphereStatistics loadedStatistics) { + for (Entry entry : initStatistics.getDatabaseData().entrySet()) { + if (loadedStatistics.getDatabaseData().containsKey(entry.getKey())) { + useLoadedToReplaceInitByDatabaseData(entry.getValue(), loadedStatistics.getDatabaseData().get(entry.getKey())); + } + } + } + + private static void useLoadedToReplaceInitByDatabaseData(final ShardingSphereDatabaseData initDatabaseData, final ShardingSphereDatabaseData loadedDatabaseData) { + for (Entry entry : initDatabaseData.getSchemaData().entrySet()) { + if (loadedDatabaseData.getSchemaData().containsKey(entry.getKey())) { + useLoadedToReplaceInitBySchemaData(entry.getValue(), loadedDatabaseData.getSchemaData().get(entry.getKey())); + } + } + } + + private static void useLoadedToReplaceInitBySchemaData(final ShardingSphereSchemaData initSchemaData, final ShardingSphereSchemaData loadedSchemaData) { + for (Entry entry : initSchemaData.getTableData().entrySet()) { + if (loadedSchemaData.getTableData().containsKey(entry.getKey())) { + entry.setValue(loadedSchemaData.getTableData().get(entry.getKey())); + } + } + } + + private static void persistDatabaseConfigurations(final MetaDataContexts metadataContexts, final ContextManagerBuilderParameter param, final MetaDataPersistService persistService) { + persistService.persistGlobalRuleConfiguration(param.getGlobalRuleConfigs(), param.getProps()); for (Entry entry : param.getDatabaseConfigs().entrySet()) { String databaseName = entry.getKey(); - metadataContexts.getPersistService().persistConfigurations(entry.getKey(), entry.getValue(), + persistService.persistConfigurations(entry.getKey(), entry.getValue(), metadataContexts.getMetaData().getDatabase(databaseName).getResourceMetaData().getStorageUnits().entrySet().stream() .collect(Collectors.toMap(Entry::getKey, each -> each.getValue().getDataSource(), (oldValue, currentValue) -> oldValue, LinkedHashMap::new)), metadataContexts.getMetaData().getDatabase(databaseName).getRuleMetaData().getRules()); } } - private static void persistMetaData(final MetaDataContexts metaDataContexts) { - metaDataContexts.getMetaData().getDatabases().values().forEach(each -> each.getSchemas() - .forEach((schemaName, schema) -> metaDataContexts.getPersistService().getDatabaseMetaDataService().persistByAlterConfiguration(each.getName(), schemaName, schema))); - metaDataContexts.getStatistics().getDatabaseData().forEach((databaseName, databaseData) -> databaseData.getSchemaData().forEach((schemaName, schemaData) -> metaDataContexts - .getPersistService().getShardingSphereDataPersistService().persist(databaseName, schemaName, schemaData, metaDataContexts.getMetaData().getDatabases()))); + private static void persistMetaData(final MetaDataContexts metaDataContexts, final MetaDataPersistService persistService) { + metaDataContexts.getMetaData().getDatabases().values().forEach(each -> each.getSchemas().forEach((schemaName, schema) -> persistService.getDatabaseMetaDataService() + .persistByAlterConfiguration(each.getName(), schemaName, schema))); + metaDataContexts.getStatistics().getDatabaseData().forEach((databaseName, databaseData) -> databaseData.getSchemaData().forEach((schemaName, schemaData) -> persistService + .getShardingSphereDataPersistService().persist(databaseName, schemaName, schemaData, metaDataContexts.getMetaData().getDatabases()))); } } diff --git a/mode/core/src/main/java/org/apache/shardingsphere/mode/service/PersistServiceFacade.java b/mode/core/src/main/java/org/apache/shardingsphere/mode/service/PersistServiceFacade.java index de646661a2221..1f06cccb0e7b6 100644 --- a/mode/core/src/main/java/org/apache/shardingsphere/mode/service/PersistServiceFacade.java +++ b/mode/core/src/main/java/org/apache/shardingsphere/mode/service/PersistServiceFacade.java @@ -20,6 +20,7 @@ import lombok.Getter; import org.apache.shardingsphere.infra.config.mode.ModeConfiguration; import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader; +import org.apache.shardingsphere.metadata.persist.MetaDataPersistService; import org.apache.shardingsphere.mode.manager.ContextManager; import org.apache.shardingsphere.mode.spi.PersistRepository; import org.apache.shardingsphere.mode.state.StatePersistService; @@ -30,6 +31,8 @@ @Getter public final class PersistServiceFacade { + private final MetaDataPersistService metaDataPersistService; + private final ComputeNodePersistService computeNodePersistService; private final StatePersistService statePersistService; @@ -37,6 +40,7 @@ public final class PersistServiceFacade { private final MetaDataManagerPersistService metaDataManagerPersistService; public PersistServiceFacade(final PersistRepository repository, final ModeConfiguration modeConfiguration, final ContextManager contextManager) { + metaDataPersistService = new MetaDataPersistService(repository); computeNodePersistService = new ComputeNodePersistService(repository); statePersistService = new StatePersistService(repository); metaDataManagerPersistService = TypedSPILoader.getService(MetaDataManagerPersistServiceBuilder.class, modeConfiguration.getType()).build(contextManager); diff --git a/mode/core/src/main/java/org/apache/shardingsphere/mode/subsciber/RuleItemChangedSubscriber.java b/mode/core/src/main/java/org/apache/shardingsphere/mode/subsciber/RuleItemChangedSubscriber.java index 8c2cb2ad48478..3643dca1dff5f 100644 --- a/mode/core/src/main/java/org/apache/shardingsphere/mode/subsciber/RuleItemChangedSubscriber.java +++ b/mode/core/src/main/java/org/apache/shardingsphere/mode/subsciber/RuleItemChangedSubscriber.java @@ -43,12 +43,14 @@ public final class RuleItemChangedSubscriber implements EventSubscriber { @SuppressWarnings({"rawtypes", "unchecked", "unused"}) @Subscribe public void renew(final AlterRuleItemEvent event) { - if (!event.getActiveVersion().equals(contextManager.getMetaDataContexts().getPersistService().getMetaDataVersionPersistService().getActiveVersionByFullPath(event.getActiveVersionKey()))) { + if (!event.getActiveVersion().equals(contextManager.getPersistServiceFacade().getMetaDataPersistService().getMetaDataVersionPersistService() + .getActiveVersionByFullPath(event.getActiveVersionKey()))) { return; } RuleItemConfigurationChangedProcessor processor = TypedSPILoader.getService(RuleItemConfigurationChangedProcessor.class, event.getType()); String yamlContent = - contextManager.getMetaDataContexts().getPersistService().getMetaDataVersionPersistService().getVersionPathByActiveVersion(event.getActiveVersionKey(), event.getActiveVersion()); + contextManager.getPersistServiceFacade().getMetaDataPersistService().getMetaDataVersionPersistService() + .getVersionPathByActiveVersion(event.getActiveVersionKey(), event.getActiveVersion()); String databaseName = event.getDatabaseName(); RuleConfiguration currentRuleConfig = processor.findRuleConfiguration(contextManager.getMetaDataContexts().getMetaData().getDatabase(databaseName)); synchronized (this) { diff --git a/mode/core/src/test/java/org/apache/shardingsphere/mode/manager/ContextManagerTest.java b/mode/core/src/test/java/org/apache/shardingsphere/mode/manager/ContextManagerTest.java index b0e80fe334180..95e5ce0c18838 100644 --- a/mode/core/src/test/java/org/apache/shardingsphere/mode/manager/ContextManagerTest.java +++ b/mode/core/src/test/java/org/apache/shardingsphere/mode/manager/ContextManagerTest.java @@ -40,9 +40,9 @@ import org.apache.shardingsphere.infra.rule.attribute.RuleAttributes; import org.apache.shardingsphere.infra.rule.attribute.datanode.MutableDataNodeRuleAttribute; import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader; -import org.apache.shardingsphere.metadata.persist.MetaDataPersistService; -import org.apache.shardingsphere.metadata.persist.service.database.DatabaseMetaDataPersistService; +import org.apache.shardingsphere.metadata.persist.node.DatabaseMetaDataNode; import org.apache.shardingsphere.mode.metadata.MetaDataContexts; +import org.apache.shardingsphere.mode.spi.PersistRepository; import org.apache.shardingsphere.test.fixture.jdbc.MockedDataSource; import org.apache.shardingsphere.test.util.PropertiesBuilder; import org.apache.shardingsphere.test.util.PropertiesBuilder.Property; @@ -89,7 +89,7 @@ void setUp() { when(metaDataContexts.getMetaData().getDatabases().values()).thenReturn(Collections.singleton(database)); ComputeNodeInstanceContext computeNodeInstanceContext = mock(ComputeNodeInstanceContext.class); when(computeNodeInstanceContext.getModeConfiguration()).thenReturn(mock(ModeConfiguration.class)); - contextManager = new ContextManager(metaDataContexts, computeNodeInstanceContext); + contextManager = new ContextManager(metaDataContexts, computeNodeInstanceContext, mock(PersistRepository.class)); } private ShardingSphereDatabase mockDatabase() { @@ -243,21 +243,13 @@ void assertAlterProperties() { @Test void assertReloadSchema() { when(metaDataContexts.getMetaData().getDatabase("foo_db").getName()).thenReturn("foo_db"); - DatabaseMetaDataPersistService databaseMetaDataPersistService = mock(DatabaseMetaDataPersistService.class, RETURNS_DEEP_STUBS); - MetaDataPersistService persistService = mock(MetaDataPersistService.class); - when(persistService.getDatabaseMetaDataService()).thenReturn(databaseMetaDataPersistService); - when(metaDataContexts.getPersistService()).thenReturn(persistService); ShardingSphereDatabase database = mockDatabase(); contextManager.reloadSchema(database, "foo_schema", "foo_ds"); - verify(databaseMetaDataPersistService).dropSchema("foo_db", "foo_schema"); + verify(contextManager.getRepository()).delete(DatabaseMetaDataNode.getMetaDataSchemaPath("foo_db", "foo_schema")); } @Test void assertReloadTable() { - DatabaseMetaDataPersistService databaseMetaDataPersistService = mock(DatabaseMetaDataPersistService.class, RETURNS_DEEP_STUBS); - MetaDataPersistService persistService = mock(MetaDataPersistService.class); - when(persistService.getDatabaseMetaDataService()).thenReturn(databaseMetaDataPersistService); - when(metaDataContexts.getPersistService()).thenReturn(persistService); ShardingSphereDatabase database = mockDatabase(); contextManager.reloadTable(database, "foo_schema", "foo_table"); assertTrue(contextManager.getMetaDataContexts().getMetaData().getDatabase("foo_db").getResourceMetaData().getStorageUnits().containsKey("foo_ds")); 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 1bfb368c20f5f..de344bd39c24e 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 @@ -120,7 +120,6 @@ void assertCreateWithProxyInstanceMetaData() throws SQLException { when(databaseMetaDataPersistService.loadAllDatabaseNames()).thenReturn(Collections.singletonList("foo_db")); when(metaDataPersistService.getDatabaseMetaDataService()).thenReturn(databaseMetaDataPersistService); try (MetaDataContexts actual = MetaDataContextsFactory.create(metaDataPersistService, createContextManagerBuilderParameter(), mock(ComputeNodeInstanceContext.class, RETURNS_DEEP_STUBS))) { - assertThat(actual.getPersistService(), is(metaDataPersistService)); assertThat(actual.getMetaData().getGlobalRuleMetaData().getRules().size(), is(1)); assertThat(actual.getMetaData().getGlobalRuleMetaData().getRules().iterator().next(), instanceOf(MockedRule.class)); assertTrue(actual.getMetaData().getDatabases().containsKey("foo_db")); diff --git a/mode/core/src/test/java/org/apache/shardingsphere/mode/metadata/MetaDataContextsTest.java b/mode/core/src/test/java/org/apache/shardingsphere/mode/metadata/MetaDataContextsTest.java index b8d880c44ecfa..16d0c880bfef2 100644 --- a/mode/core/src/test/java/org/apache/shardingsphere/mode/metadata/MetaDataContextsTest.java +++ b/mode/core/src/test/java/org/apache/shardingsphere/mode/metadata/MetaDataContextsTest.java @@ -46,6 +46,6 @@ void assertGetDefaultMetaData() { Map databases = Collections.singletonMap(DefaultDatabase.LOGIC_NAME, database); ShardingSphereMetaData metaData = new ShardingSphereMetaData(databases, mock(ResourceMetaData.class), mock(RuleMetaData.class), new ConfigurationProperties(new Properties())); - assertThat(new MetaDataContexts(mock(MetaDataPersistService.class), metaData).getMetaData().getDatabase(DefaultDatabase.LOGIC_NAME), is(database)); + assertThat(MetaDataContextsFactory.create(mock(MetaDataPersistService.class), metaData).getMetaData().getDatabase(DefaultDatabase.LOGIC_NAME), is(database)); } } diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/ClusterContextManagerBuilder.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/ClusterContextManagerBuilder.java index 0d477c8b0e4be..a4d13f555ce68 100644 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/ClusterContextManagerBuilder.java +++ b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/ClusterContextManagerBuilder.java @@ -69,7 +69,7 @@ public ContextManager build(final ContextManagerBuilderParameter param, final Ev } MetaDataPersistService metaDataPersistService = new MetaDataPersistService(repository); MetaDataContexts metaDataContexts = MetaDataContextsFactory.create(metaDataPersistService, param, computeNodeInstanceContext, new QualifiedDataSourceStatusService(repository).loadStatus()); - ContextManager result = new ContextManager(metaDataContexts, computeNodeInstanceContext); + ContextManager result = new ContextManager(metaDataContexts, computeNodeInstanceContext, repository); createSubscribers(eventBusContext, repository); registerOnline(eventBusContext, computeNodeInstanceContext, repository, param, result); setClusterState(result); diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/subscriber/ConfigurationChangedSubscriber.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/subscriber/ConfigurationChangedSubscriber.java index bbe45c2fead68..0da5fe1907009 100644 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/subscriber/ConfigurationChangedSubscriber.java +++ b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/subscriber/ConfigurationChangedSubscriber.java @@ -43,11 +43,12 @@ public final class ConfigurationChangedSubscriber implements EventSubscriber { */ @Subscribe public void renew(final RegisterStorageUnitEvent event) { - if (!event.getActiveVersion().equals(contextManager.getMetaDataContexts().getPersistService().getMetaDataVersionPersistService().getActiveVersionByFullPath(event.getActiveVersionKey()))) { + if (!event.getActiveVersion().equals(contextManager.getPersistServiceFacade().getMetaDataPersistService().getMetaDataVersionPersistService() + .getActiveVersionByFullPath(event.getActiveVersionKey()))) { return; } contextManager.getConfigurationContextManager().registerStorageUnit(event.getDatabaseName(), - contextManager.getMetaDataContexts().getPersistService().getDataSourceUnitService().load(event.getDatabaseName(), event.getStorageUnitName())); + contextManager.getPersistServiceFacade().getMetaDataPersistService().getDataSourceUnitService().load(event.getDatabaseName(), event.getStorageUnitName())); } /** @@ -57,11 +58,12 @@ public void renew(final RegisterStorageUnitEvent event) { */ @Subscribe public void renew(final AlterStorageUnitEvent event) { - if (!event.getActiveVersion().equals(contextManager.getMetaDataContexts().getPersistService().getMetaDataVersionPersistService().getActiveVersionByFullPath(event.getActiveVersionKey()))) { + if (!event.getActiveVersion().equals(contextManager.getPersistServiceFacade().getMetaDataPersistService().getMetaDataVersionPersistService() + .getActiveVersionByFullPath(event.getActiveVersionKey()))) { return; } contextManager.getConfigurationContextManager().alterStorageUnit( - event.getDatabaseName(), contextManager.getMetaDataContexts().getPersistService().getDataSourceUnitService().load(event.getDatabaseName(), event.getStorageUnitName())); + event.getDatabaseName(), contextManager.getPersistServiceFacade().getMetaDataPersistService().getDataSourceUnitService().load(event.getDatabaseName(), event.getStorageUnitName())); } /** @@ -84,10 +86,11 @@ public void renew(final UnregisterStorageUnitEvent event) { */ @Subscribe public synchronized void renew(final AlterGlobalRuleConfigurationEvent event) { - if (!event.getActiveVersion().equals(contextManager.getMetaDataContexts().getPersistService().getMetaDataVersionPersistService().getActiveVersionByFullPath(event.getActiveVersionKey()))) { + if (!event.getActiveVersion().equals(contextManager.getPersistServiceFacade().getMetaDataPersistService().getMetaDataVersionPersistService() + .getActiveVersionByFullPath(event.getActiveVersionKey()))) { return; } - contextManager.getMetaDataContexts().getPersistService().getGlobalRuleService().load(event.getRuleSimpleName()) + contextManager.getPersistServiceFacade().getMetaDataPersistService().getGlobalRuleService().load(event.getRuleSimpleName()) .ifPresent(optional -> contextManager.getConfigurationContextManager().alterGlobalRuleConfiguration(optional)); } @@ -99,9 +102,10 @@ public synchronized void renew(final AlterGlobalRuleConfigurationEvent event) { */ @Subscribe public synchronized void renew(final AlterPropertiesEvent event) { - if (!event.getActiveVersion().equals(contextManager.getMetaDataContexts().getPersistService().getMetaDataVersionPersistService().getActiveVersionByFullPath(event.getActiveVersionKey()))) { + if (!event.getActiveVersion().equals(contextManager.getPersistServiceFacade().getMetaDataPersistService().getMetaDataVersionPersistService() + .getActiveVersionByFullPath(event.getActiveVersionKey()))) { return; } - contextManager.getConfigurationContextManager().alterProperties(contextManager.getMetaDataContexts().getPersistService().getPropsService().load()); + contextManager.getConfigurationContextManager().alterProperties(contextManager.getPersistServiceFacade().getMetaDataPersistService().getPropsService().load()); } } diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/subscriber/ResourceMetaDataChangedSubscriber.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/subscriber/ResourceMetaDataChangedSubscriber.java index 05afc49a5eab2..e8e234deba865 100644 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/subscriber/ResourceMetaDataChangedSubscriber.java +++ b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/subscriber/ResourceMetaDataChangedSubscriber.java @@ -90,10 +90,11 @@ public synchronized void renew(final SchemaDeletedEvent event) { */ @Subscribe public synchronized void renew(final CreateOrAlterTableEvent event) { - if (!event.getActiveVersion().equals(contextManager.getMetaDataContexts().getPersistService().getMetaDataVersionPersistService().getActiveVersionByFullPath(event.getActiveVersionKey()))) { + if (!event.getActiveVersion().equals(contextManager.getPersistServiceFacade().getMetaDataPersistService().getMetaDataVersionPersistService() + .getActiveVersionByFullPath(event.getActiveVersionKey()))) { return; } - Map tables = contextManager.getMetaDataContexts().getPersistService().getDatabaseMetaDataService() + Map tables = contextManager.getPersistServiceFacade().getMetaDataPersistService().getDatabaseMetaDataService() .getTableMetaDataPersistService().load(event.getDatabaseName(), event.getSchemaName(), event.getTableName()); contextManager.getResourceMetaDataContextManager().alterSchema(event.getDatabaseName(), event.getSchemaName(), tables.values().iterator().next(), null); } @@ -115,10 +116,11 @@ public synchronized void renew(final DropTableEvent event) { */ @Subscribe public synchronized void renew(final CreateOrAlterViewEvent event) { - if (!event.getActiveVersion().equals(contextManager.getMetaDataContexts().getPersistService().getMetaDataVersionPersistService().getActiveVersionByFullPath(event.getActiveVersionKey()))) { + if (!event.getActiveVersion().equals(contextManager.getPersistServiceFacade().getMetaDataPersistService().getMetaDataVersionPersistService() + .getActiveVersionByFullPath(event.getActiveVersionKey()))) { return; } - Map views = contextManager.getMetaDataContexts().getPersistService().getDatabaseMetaDataService() + Map views = contextManager.getPersistServiceFacade().getMetaDataPersistService().getDatabaseMetaDataService() .getViewMetaDataPersistService().load(event.getDatabaseName(), event.getSchemaName(), event.getViewName()); contextManager.getResourceMetaDataContextManager().alterSchema(event.getDatabaseName(), event.getSchemaName(), null, views.values().iterator().next()); } diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/service/ClusterMetaDataManagerPersistService.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/service/ClusterMetaDataManagerPersistService.java index eaddbaa387078..38d61ff5795a8 100644 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/service/ClusterMetaDataManagerPersistService.java +++ b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/service/ClusterMetaDataManagerPersistService.java @@ -49,17 +49,17 @@ public final class ClusterMetaDataManagerPersistService implements MetaDataManag @Override public void createDatabase(final String databaseName) { - contextManager.getMetaDataContexts().getPersistService().getDatabaseMetaDataService().addDatabase(databaseName); + contextManager.getPersistServiceFacade().getMetaDataPersistService().getDatabaseMetaDataService().addDatabase(databaseName); } @Override public void dropDatabase(final String databaseName) { - contextManager.getMetaDataContexts().getPersistService().getDatabaseMetaDataService().dropDatabase(databaseName); + contextManager.getPersistServiceFacade().getMetaDataPersistService().getDatabaseMetaDataService().dropDatabase(databaseName); } @Override public void createSchema(final String databaseName, final String schemaName) { - contextManager.getMetaDataContexts().getPersistService().getDatabaseMetaDataService().addSchema(databaseName, schemaName); + contextManager.getPersistServiceFacade().getMetaDataPersistService().getDatabaseMetaDataService().addSchema(databaseName, schemaName); } @Override @@ -67,7 +67,7 @@ public void alterSchema(final AlterSchemaPOJO alterSchemaPOJO) { String databaseName = alterSchemaPOJO.getDatabaseName(); String schemaName = alterSchemaPOJO.getSchemaName(); ShardingSphereSchema schema = contextManager.getMetaDataContexts().getMetaData().getDatabase(databaseName).getSchema(schemaName); - DatabaseMetaDataPersistService databaseMetaDataService = contextManager.getMetaDataContexts().getPersistService().getDatabaseMetaDataService(); + DatabaseMetaDataPersistService databaseMetaDataService = contextManager.getPersistServiceFacade().getMetaDataPersistService().getDatabaseMetaDataService(); databaseMetaDataService.persistByAlterConfiguration(databaseName, alterSchemaPOJO.getRenameSchemaName(), schema); databaseMetaDataService.getViewMetaDataPersistService().persist(databaseName, alterSchemaPOJO.getRenameSchemaName(), schema.getViews()); databaseMetaDataService.dropSchema(databaseName, schemaName); @@ -75,7 +75,7 @@ public void alterSchema(final AlterSchemaPOJO alterSchemaPOJO) { @Override public void dropSchema(final String databaseName, final Collection schemaNames) { - DatabaseMetaDataPersistService databaseMetaDataService = contextManager.getMetaDataContexts().getPersistService().getDatabaseMetaDataService(); + DatabaseMetaDataPersistService databaseMetaDataService = contextManager.getPersistServiceFacade().getMetaDataPersistService().getDatabaseMetaDataService(); schemaNames.forEach(each -> databaseMetaDataService.dropSchema(databaseName, each)); } @@ -85,7 +85,7 @@ public void alterSchemaMetaData(final AlterSchemaMetaDataPOJO alterSchemaMetaDat String schemaName = alterSchemaMetaDataPOJO.getSchemaName(); Map tables = alterSchemaMetaDataPOJO.getAlteredTables().stream().collect(Collectors.toMap(ShardingSphereTable::getName, table -> table)); Map views = alterSchemaMetaDataPOJO.getAlteredViews().stream().collect(Collectors.toMap(ShardingSphereView::getName, view -> view)); - DatabaseMetaDataPersistService databaseMetaDataService = contextManager.getMetaDataContexts().getPersistService().getDatabaseMetaDataService(); + DatabaseMetaDataPersistService databaseMetaDataService = contextManager.getPersistServiceFacade().getMetaDataPersistService().getDatabaseMetaDataService(); databaseMetaDataService.getTableMetaDataPersistService().persist(databaseName, schemaName, tables); databaseMetaDataService.getViewMetaDataPersistService().persist(databaseName, schemaName, views); alterSchemaMetaDataPOJO.getDroppedTables().forEach(each -> databaseMetaDataService.getTableMetaDataPersistService().delete(databaseName, schemaName, each)); @@ -94,19 +94,21 @@ public void alterSchemaMetaData(final AlterSchemaMetaDataPOJO alterSchemaMetaDat @Override public void registerStorageUnits(final String databaseName, final Map toBeRegisteredProps) { - contextManager.getMetaDataContexts().getPersistService().getDataSourceUnitService().persistConfigurations(databaseName, toBeRegisteredProps); + contextManager.getPersistServiceFacade().getMetaDataPersistService().getDataSourceUnitService().persistConfigurations(databaseName, toBeRegisteredProps); } @Override public void alterStorageUnits(final String databaseName, final Map toBeUpdatedProps) { - DatabaseBasedPersistService> dataSourceService = contextManager.getMetaDataContexts().getPersistService().getDataSourceUnitService(); - contextManager.getMetaDataContexts().getPersistService().getMetaDataVersionPersistService().switchActiveVersion(dataSourceService.persistConfigurations(databaseName, toBeUpdatedProps)); + DatabaseBasedPersistService> dataSourceService = contextManager.getPersistServiceFacade().getMetaDataPersistService().getDataSourceUnitService(); + contextManager.getPersistServiceFacade().getMetaDataPersistService().getMetaDataVersionPersistService() + .switchActiveVersion(dataSourceService.persistConfigurations(databaseName, toBeUpdatedProps)); } @Override public void unregisterStorageUnits(final String databaseName, final Collection toBeDroppedStorageUnitNames) { - contextManager.getMetaDataContexts().getPersistService().getDataSourceUnitService().deleteConfigurations(databaseName, - getToBeDroppedDataSourcePoolProperties(contextManager.getMetaDataContexts().getPersistService().getDataSourceUnitService().load(databaseName), toBeDroppedStorageUnitNames)); + contextManager.getPersistServiceFacade().getMetaDataPersistService().getDataSourceUnitService().deleteConfigurations(databaseName, + getToBeDroppedDataSourcePoolProperties(contextManager.getPersistServiceFacade().getMetaDataPersistService() + .getDataSourceUnitService().load(databaseName), toBeDroppedStorageUnitNames)); } private Map getToBeDroppedDataSourcePoolProperties(final Map propsMap, final Collection toBeDroppedResourceNames) { @@ -122,14 +124,15 @@ private Map getToBeDroppedDataSourcePoolProper @Override public void alterSingleRuleConfiguration(final String databaseName, final Collection ruleConfigs) { ruleConfigs.removeIf(each -> !each.getClass().isAssignableFrom(SingleRuleConfiguration.class)); - contextManager.getMetaDataContexts().getPersistService().getMetaDataVersionPersistService() - .switchActiveVersion(contextManager.getMetaDataContexts().getPersistService().getDatabaseRulePersistService().persistConfigurations(databaseName, ruleConfigs)); + contextManager.getPersistServiceFacade().getMetaDataPersistService().getMetaDataVersionPersistService() + .switchActiveVersion(contextManager.getPersistServiceFacade().getMetaDataPersistService().getDatabaseRulePersistService().persistConfigurations(databaseName, ruleConfigs)); } @Override public Collection alterRuleConfiguration(final String databaseName, final RuleConfiguration toBeAlteredRuleConfig) { if (null != toBeAlteredRuleConfig) { - return contextManager.getMetaDataContexts().getPersistService().getDatabaseRulePersistService().persistConfigurations(databaseName, Collections.singleton(toBeAlteredRuleConfig)); + return contextManager.getPersistServiceFacade().getMetaDataPersistService().getDatabaseRulePersistService() + .persistConfigurations(databaseName, Collections.singleton(toBeAlteredRuleConfig)); } return Collections.emptyList(); } @@ -137,22 +140,22 @@ public Collection alterRuleConfiguration(final String databaseN @Override public void removeRuleConfigurationItem(final String databaseName, final RuleConfiguration toBeRemovedRuleConfig) { if (null != toBeRemovedRuleConfig) { - contextManager.getMetaDataContexts().getPersistService().getDatabaseRulePersistService().deleteConfigurations(databaseName, Collections.singleton(toBeRemovedRuleConfig)); + contextManager.getPersistServiceFacade().getMetaDataPersistService().getDatabaseRulePersistService().deleteConfigurations(databaseName, Collections.singleton(toBeRemovedRuleConfig)); } } @Override public void removeRuleConfiguration(final String databaseName, final String ruleName) { - contextManager.getMetaDataContexts().getPersistService().getDatabaseRulePersistService().delete(databaseName, ruleName); + contextManager.getPersistServiceFacade().getMetaDataPersistService().getDatabaseRulePersistService().delete(databaseName, ruleName); } @Override public void alterGlobalRuleConfiguration(final RuleConfiguration toBeAlteredRuleConfig) { - contextManager.getMetaDataContexts().getPersistService().getGlobalRuleService().persist(Collections.singleton(toBeAlteredRuleConfig)); + contextManager.getPersistServiceFacade().getMetaDataPersistService().getGlobalRuleService().persist(Collections.singleton(toBeAlteredRuleConfig)); } @Override public void alterProperties(final Properties props) { - contextManager.getMetaDataContexts().getPersistService().getPropsService().persist(props); + contextManager.getPersistServiceFacade().getMetaDataPersistService().getPropsService().persist(props); } } diff --git a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/process/subscriber/ProcessListChangedSubscriberTest.java b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/process/subscriber/ProcessListChangedSubscriberTest.java index 5d6bdeab92295..50a6721557f1c 100644 --- a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/process/subscriber/ProcessListChangedSubscriberTest.java +++ b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/process/subscriber/ProcessListChangedSubscriberTest.java @@ -38,7 +38,7 @@ import org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.status.compute.event.KillLocalProcessEvent; import org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.status.compute.event.ReportLocalProcessesCompletedEvent; import org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.status.compute.event.ReportLocalProcessesEvent; -import org.apache.shardingsphere.mode.metadata.MetaDataContexts; +import org.apache.shardingsphere.mode.metadata.MetaDataContextsFactory; import org.apache.shardingsphere.mode.repository.cluster.ClusterPersistRepository; import org.apache.shardingsphere.mode.repository.cluster.ClusterPersistRepositoryConfiguration; import org.awaitility.Awaitility; @@ -83,7 +83,7 @@ class ProcessListChangedSubscriberTest { void setUp() throws SQLException { EventBusContext eventBusContext = new EventBusContext(); contextManager = new ClusterContextManagerBuilder().build(createContextManagerBuilderParameter(), eventBusContext); - contextManager.renewMetaDataContexts(new MetaDataContexts(contextManager.getMetaDataContexts().getPersistService(), new ShardingSphereMetaData(createDatabases(), + contextManager.renewMetaDataContexts(MetaDataContextsFactory.create(contextManager.getPersistServiceFacade().getMetaDataPersistService(), new ShardingSphereMetaData(createDatabases(), contextManager.getMetaDataContexts().getMetaData().getGlobalResourceMetaData(), contextManager.getMetaDataContexts().getMetaData().getGlobalRuleMetaData(), new ConfigurationProperties(new Properties())))); subscriber = new ProcessListChangedSubscriber(contextManager, repository); diff --git a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/subscriber/ResourceMetaDataChangedSubscriberTest.java b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/subscriber/ResourceMetaDataChangedSubscriberTest.java index 1c7a1afa91069..b003d9e79caa5 100644 --- a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/subscriber/ResourceMetaDataChangedSubscriberTest.java +++ b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/subscriber/ResourceMetaDataChangedSubscriberTest.java @@ -37,7 +37,7 @@ import org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.metadata.event.DatabaseDeletedEvent; import org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.metadata.event.SchemaAddedEvent; import org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.metadata.event.SchemaDeletedEvent; -import org.apache.shardingsphere.mode.metadata.MetaDataContexts; +import org.apache.shardingsphere.mode.metadata.MetaDataContextsFactory; import org.apache.shardingsphere.mode.repository.cluster.ClusterPersistRepositoryConfiguration; import org.apache.shardingsphere.test.fixture.jdbc.MockedDataSource; import org.junit.jupiter.api.BeforeEach; @@ -80,7 +80,7 @@ class ResourceMetaDataChangedSubscriberTest { @BeforeEach void setUp() throws SQLException { contextManager = new ClusterContextManagerBuilder().build(createContextManagerBuilderParameter(), new EventBusContext()); - contextManager.renewMetaDataContexts(new MetaDataContexts(contextManager.getMetaDataContexts().getPersistService(), new ShardingSphereMetaData(createDatabases(), + contextManager.renewMetaDataContexts(MetaDataContextsFactory.create(contextManager.getPersistServiceFacade().getMetaDataPersistService(), new ShardingSphereMetaData(createDatabases(), contextManager.getMetaDataContexts().getMetaData().getGlobalResourceMetaData(), contextManager.getMetaDataContexts().getMetaData().getGlobalRuleMetaData(), new ConfigurationProperties(new Properties())))); subscriber = new ResourceMetaDataChangedSubscriber(contextManager); diff --git a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/subscriber/StateChangedSubscriberTest.java b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/subscriber/StateChangedSubscriberTest.java index c344e148831dd..f708bbf6c4e7e 100644 --- a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/subscriber/StateChangedSubscriberTest.java +++ b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/subscriber/StateChangedSubscriberTest.java @@ -43,7 +43,7 @@ import org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.status.compute.event.LabelsEvent; import org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.status.compute.event.WorkerIdEvent; import org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.status.storage.event.StorageNodeChangedEvent; -import org.apache.shardingsphere.mode.metadata.MetaDataContexts; +import org.apache.shardingsphere.mode.metadata.MetaDataContextsFactory; import org.apache.shardingsphere.mode.repository.cluster.ClusterPersistRepositoryConfiguration; import org.apache.shardingsphere.mode.storage.QualifiedDataSourceStatus; import org.junit.jupiter.api.BeforeEach; @@ -86,7 +86,7 @@ class StateChangedSubscriberTest { void setUp() throws SQLException { EventBusContext eventBusContext = new EventBusContext(); contextManager = new ClusterContextManagerBuilder().build(createContextManagerBuilderParameter(), eventBusContext); - contextManager.renewMetaDataContexts(new MetaDataContexts(contextManager.getMetaDataContexts().getPersistService(), new ShardingSphereMetaData(createDatabases(), + contextManager.renewMetaDataContexts(MetaDataContextsFactory.create(contextManager.getPersistServiceFacade().getMetaDataPersistService(), new ShardingSphereMetaData(createDatabases(), contextManager.getMetaDataContexts().getMetaData().getGlobalResourceMetaData(), contextManager.getMetaDataContexts().getMetaData().getGlobalRuleMetaData(), new ConfigurationProperties(new Properties())))); subscriber = new StateChangedSubscriber(contextManager); diff --git a/mode/type/standalone/core/src/main/java/org/apache/shardingsphere/mode/manager/standalone/StandaloneContextManagerBuilder.java b/mode/type/standalone/core/src/main/java/org/apache/shardingsphere/mode/manager/standalone/StandaloneContextManagerBuilder.java index a6048e6690753..5c2b3da39b068 100644 --- a/mode/type/standalone/core/src/main/java/org/apache/shardingsphere/mode/manager/standalone/StandaloneContextManagerBuilder.java +++ b/mode/type/standalone/core/src/main/java/org/apache/shardingsphere/mode/manager/standalone/StandaloneContextManagerBuilder.java @@ -49,7 +49,7 @@ public ContextManager build(final ContextManagerBuilderParameter param, final Ev MetaDataPersistService persistService = new MetaDataPersistService(repository); ComputeNodeInstanceContext computeNodeInstanceContext = buildComputeNodeInstanceContext(param, eventBusContext); MetaDataContexts metaDataContexts = MetaDataContextsFactory.create(persistService, param, computeNodeInstanceContext); - ContextManager result = new ContextManager(metaDataContexts, computeNodeInstanceContext); + ContextManager result = new ContextManager(metaDataContexts, computeNodeInstanceContext, repository); new StandaloneEventSubscriberRegistry(result).register(); return result; } diff --git a/mode/type/standalone/core/src/main/java/org/apache/shardingsphere/mode/manager/standalone/service/StandaloneMetaDataManagerPersistService.java b/mode/type/standalone/core/src/main/java/org/apache/shardingsphere/mode/manager/standalone/service/StandaloneMetaDataManagerPersistService.java index 1099ebf8b87a8..0bfcd58c2874e 100644 --- a/mode/type/standalone/core/src/main/java/org/apache/shardingsphere/mode/manager/standalone/service/StandaloneMetaDataManagerPersistService.java +++ b/mode/type/standalone/core/src/main/java/org/apache/shardingsphere/mode/manager/standalone/service/StandaloneMetaDataManagerPersistService.java @@ -71,14 +71,14 @@ public StandaloneMetaDataManagerPersistService(final ContextManager contextManag @Override public void createDatabase(final String databaseName) { contextManager.getResourceMetaDataContextManager().addDatabase(databaseName); - contextManager.getMetaDataContexts().getPersistService().getDatabaseMetaDataService().addDatabase(databaseName); + contextManager.getPersistServiceFacade().getMetaDataPersistService().getDatabaseMetaDataService().addDatabase(databaseName); clearServiceCache(); } @Override public void dropDatabase(final String databaseName) { contextManager.getResourceMetaDataContextManager().dropDatabase(databaseName); - contextManager.getMetaDataContexts().getPersistService().getDatabaseMetaDataService().dropDatabase(databaseName); + contextManager.getPersistServiceFacade().getMetaDataPersistService().getDatabaseMetaDataService().dropDatabase(databaseName); clearServiceCache(); } @@ -89,7 +89,7 @@ public void createSchema(final String databaseName, final String schemaName) { ShardingSphereDatabase database = metaData.getDatabase(databaseName); database.addSchema(schemaName, schema); metaData.getGlobalRuleMetaData().getRules().forEach(each -> ((GlobalRule) each).refresh(metaData.getDatabases(), GlobalRuleChangedType.SCHEMA_CHANGED)); - contextManager.getMetaDataContexts().getPersistService().getDatabaseMetaDataService().persistByAlterConfiguration(databaseName, schemaName, schema); + contextManager.getPersistServiceFacade().getMetaDataPersistService().getDatabaseMetaDataService().persistByAlterConfiguration(databaseName, schemaName, schema); } @Override @@ -99,7 +99,7 @@ public void alterSchema(final AlterSchemaPOJO alterSchemaPOJO) { putSchemaMetaData(database, alterSchemaPOJO.getSchemaName(), alterSchemaPOJO.getRenameSchemaName(), alterSchemaPOJO.getLogicDataSourceName()); removeSchemaMetaData(database, alterSchemaPOJO.getSchemaName()); metaData.getGlobalRuleMetaData().getRules().forEach(each -> ((GlobalRule) each).refresh(metaData.getDatabases(), GlobalRuleChangedType.SCHEMA_CHANGED)); - DatabaseMetaDataPersistService databaseMetaDataService = contextManager.getMetaDataContexts().getPersistService().getDatabaseMetaDataService(); + DatabaseMetaDataPersistService databaseMetaDataService = contextManager.getPersistServiceFacade().getMetaDataPersistService().getDatabaseMetaDataService(); databaseMetaDataService.persistByAlterConfiguration(alterSchemaPOJO.getDatabaseName(), alterSchemaPOJO.getRenameSchemaName(), database.getSchema(alterSchemaPOJO.getRenameSchemaName())); databaseMetaDataService.getViewMetaDataPersistService().persist(alterSchemaPOJO.getDatabaseName(), alterSchemaPOJO.getRenameSchemaName(), database.getSchema(alterSchemaPOJO.getRenameSchemaName()).getViews()); @@ -204,7 +204,7 @@ public void alterSchemaMetaData(final AlterSchemaMetaDataPOJO alterSchemaMetaDat addDataNode(database, alterSchemaMetaDataPOJO.getLogicDataSourceName(), schemaName, tables, views); removeDataNode(database, schemaName, alterSchemaMetaDataPOJO.getDroppedTables(), alterSchemaMetaDataPOJO.getDroppedViews()); metaData.getGlobalRuleMetaData().getRules().forEach(each -> ((GlobalRule) each).refresh(metaData.getDatabases(), GlobalRuleChangedType.SCHEMA_CHANGED)); - DatabaseMetaDataPersistService databaseMetaDataService = contextManager.getMetaDataContexts().getPersistService().getDatabaseMetaDataService(); + DatabaseMetaDataPersistService databaseMetaDataService = contextManager.getPersistServiceFacade().getMetaDataPersistService().getDatabaseMetaDataService(); databaseMetaDataService.getTableMetaDataPersistService().persist(databaseName, schemaName, tables); databaseMetaDataService.getViewMetaDataPersistService().persist(databaseName, schemaName, views); alterSchemaMetaDataPOJO.getDroppedTables().forEach(each -> databaseMetaDataService.getTableMetaDataPersistService().delete(databaseName, schemaName, each)); @@ -215,14 +215,16 @@ public void alterSchemaMetaData(final AlterSchemaMetaDataPOJO alterSchemaMetaDat public void registerStorageUnits(final String databaseName, final Map toBeRegisteredProps) throws SQLException { SwitchingResource switchingResource = new ResourceSwitchManager().registerStorageUnit(contextManager.getMetaDataContexts().getMetaData().getDatabase(databaseName).getResourceMetaData(), toBeRegisteredProps); - contextManager.getMetaDataContexts().getMetaData().getDatabases().putAll(contextManager.getConfigurationContextManager().createChangedDatabases(databaseName, false, switchingResource, null)); + contextManager.getMetaDataContexts().getMetaData().getDatabases().putAll(contextManager.getConfigurationContextManager() + .createChangedDatabases(databaseName, false, switchingResource, null)); contextManager.getMetaDataContexts().getMetaData().getGlobalRuleMetaData().getRules() .forEach(each -> ((GlobalRule) each).refresh(contextManager.getMetaDataContexts().getMetaData().getDatabases(), GlobalRuleChangedType.DATABASE_CHANGED)); contextManager.getMetaDataContexts().getMetaData().getDatabase(databaseName).getSchemas() - .forEach((schemaName, schema) -> contextManager.getMetaDataContexts().getPersistService().getDatabaseMetaDataService() + .forEach((schemaName, schema) -> contextManager.getPersistServiceFacade().getMetaDataPersistService().getDatabaseMetaDataService() .persistByAlterConfiguration(contextManager.getMetaDataContexts().getMetaData().getDatabase(databaseName).getName(), schemaName, schema)); - DatabaseBasedPersistService> dataSourceService = contextManager.getMetaDataContexts().getPersistService().getDataSourceUnitService(); - contextManager.getMetaDataContexts().getPersistService().getMetaDataVersionPersistService().switchActiveVersion(dataSourceService.persistConfigurations(databaseName, toBeRegisteredProps)); + DatabaseBasedPersistService> dataSourceService = contextManager.getPersistServiceFacade().getMetaDataPersistService().getDataSourceUnitService(); + contextManager.getPersistServiceFacade().getMetaDataPersistService().getMetaDataVersionPersistService() + .switchActiveVersion(dataSourceService.persistConfigurations(databaseName, toBeRegisteredProps)); clearServiceCache(); } @@ -230,11 +232,13 @@ public void registerStorageUnits(final String databaseName, final Map toBeUpdatedProps) throws SQLException { SwitchingResource switchingResource = new ResourceSwitchManager().alterStorageUnit(contextManager.getMetaDataContexts().getMetaData().getDatabase(databaseName).getResourceMetaData(), toBeUpdatedProps); - contextManager.getMetaDataContexts().getMetaData().getDatabases().putAll(contextManager.getConfigurationContextManager().createChangedDatabases(databaseName, true, switchingResource, null)); + contextManager.getMetaDataContexts().getMetaData().getDatabases().putAll(contextManager.getConfigurationContextManager() + .createChangedDatabases(databaseName, true, switchingResource, null)); contextManager.getMetaDataContexts().getMetaData().getGlobalRuleMetaData().getRules() .forEach(each -> ((GlobalRule) each).refresh(contextManager.getMetaDataContexts().getMetaData().getDatabases(), GlobalRuleChangedType.DATABASE_CHANGED)); - DatabaseBasedPersistService> dataSourceService = contextManager.getMetaDataContexts().getPersistService().getDataSourceUnitService(); - contextManager.getMetaDataContexts().getPersistService().getMetaDataVersionPersistService().switchActiveVersion(dataSourceService.persistConfigurations(databaseName, toBeUpdatedProps)); + DatabaseBasedPersistService> dataSourceService = contextManager.getPersistServiceFacade().getMetaDataPersistService().getDataSourceUnitService(); + contextManager.getPersistServiceFacade().getMetaDataPersistService().getMetaDataVersionPersistService() + .switchActiveVersion(dataSourceService.persistConfigurations(databaseName, toBeUpdatedProps)); switchingResource.closeStaleDataSources(); clearServiceCache(); } @@ -257,9 +261,9 @@ public void unregisterStorageUnits(final String databaseName, final Collection ruleConfigs) { ruleConfigs.removeIf(each -> !each.getClass().isAssignableFrom(SingleRuleConfiguration.class)); - Collection metaDataVersions = contextManager.getMetaDataContexts().getPersistService().getDatabaseRulePersistService() + Collection metaDataVersions = contextManager.getPersistServiceFacade().getMetaDataPersistService().getDatabaseRulePersistService() .persistConfigurations(contextManager.getMetaDataContexts().getMetaData().getDatabase(databaseName).getName(), ruleConfigs); - contextManager.getMetaDataContexts().getPersistService().getMetaDataVersionPersistService().switchActiveVersion(metaDataVersions); + contextManager.getPersistServiceFacade().getMetaDataPersistService().getMetaDataVersionPersistService().switchActiveVersion(metaDataVersions); contextManager.getConfigurationContextManager().alterRuleConfiguration(databaseName, ruleConfigs.iterator().next()); clearServiceCache(); } @@ -267,9 +271,9 @@ public void alterSingleRuleConfiguration(final String databaseName, final Collec @Override public Collection alterRuleConfiguration(final String databaseName, final RuleConfiguration toBeAlteredRuleConfig) { if (null != toBeAlteredRuleConfig) { - Collection metaDataVersions = contextManager.getMetaDataContexts().getPersistService().getDatabaseRulePersistService() + Collection metaDataVersions = contextManager.getPersistServiceFacade().getMetaDataPersistService().getDatabaseRulePersistService() .persistConfigurations(contextManager.getMetaDataContexts().getMetaData().getDatabase(databaseName).getName(), Collections.singletonList(toBeAlteredRuleConfig)); - contextManager.getMetaDataContexts().getPersistService().getMetaDataVersionPersistService().switchActiveVersion(metaDataVersions); + contextManager.getPersistServiceFacade().getMetaDataPersistService().getMetaDataVersionPersistService().switchActiveVersion(metaDataVersions); sendDatabaseRuleChangedEvent(databaseName, metaDataVersions); clearServiceCache(); } @@ -291,28 +295,29 @@ private void sendDatabaseRuleChangedEvent(final String databaseName, final MetaD public void removeRuleConfigurationItem(final String databaseName, final RuleConfiguration toBeRemovedRuleConfig) { if (null != toBeRemovedRuleConfig) { sendDatabaseRuleChangedEvent(databaseName, - contextManager.getMetaDataContexts().getPersistService().getDatabaseRulePersistService().deleteConfigurations(databaseName, Collections.singleton(toBeRemovedRuleConfig))); + contextManager.getPersistServiceFacade().getMetaDataPersistService().getDatabaseRulePersistService() + .deleteConfigurations(databaseName, Collections.singleton(toBeRemovedRuleConfig))); clearServiceCache(); } } @Override public void removeRuleConfiguration(final String databaseName, final String ruleName) { - contextManager.getMetaDataContexts().getPersistService().getDatabaseRulePersistService().delete(databaseName, ruleName); + contextManager.getPersistServiceFacade().getMetaDataPersistService().getDatabaseRulePersistService().delete(databaseName, ruleName); clearServiceCache(); } @Override public void alterGlobalRuleConfiguration(final RuleConfiguration toBeAlteredRuleConfig) { contextManager.getConfigurationContextManager().alterGlobalRuleConfiguration(toBeAlteredRuleConfig); - contextManager.getMetaDataContexts().getPersistService().getGlobalRuleService().persist(Collections.singleton(toBeAlteredRuleConfig)); + contextManager.getPersistServiceFacade().getMetaDataPersistService().getGlobalRuleService().persist(Collections.singleton(toBeAlteredRuleConfig)); clearServiceCache(); } @Override public void alterProperties(final Properties props) { contextManager.getConfigurationContextManager().alterProperties(props); - contextManager.getMetaDataContexts().getPersistService().getPropsService().persist(props); + contextManager.getPersistServiceFacade().getMetaDataPersistService().getPropsService().persist(props); clearServiceCache(); } diff --git a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/SetDistVariableExecutor.java b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/SetDistVariableExecutor.java index f0384da4642e2..d3b04aef27394 100644 --- a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/SetDistVariableExecutor.java +++ b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/SetDistVariableExecutor.java @@ -25,6 +25,8 @@ import org.apache.shardingsphere.infra.config.props.ConfigurationPropertyKey; import org.apache.shardingsphere.infra.config.props.temporary.TemporaryConfigurationPropertyKey; import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions; +import org.apache.shardingsphere.infra.exception.kernel.syntax.InvalidVariableValueException; +import org.apache.shardingsphere.infra.exception.kernel.syntax.UnsupportedVariableException; import org.apache.shardingsphere.infra.props.TypedPropertyKey; import org.apache.shardingsphere.infra.props.TypedPropertyValue; import org.apache.shardingsphere.infra.props.exception.TypedPropertyValueException; @@ -33,8 +35,6 @@ import org.apache.shardingsphere.logging.util.LoggingUtils; import org.apache.shardingsphere.mode.manager.ContextManager; import org.apache.shardingsphere.mode.metadata.MetaDataContexts; -import org.apache.shardingsphere.infra.exception.kernel.syntax.InvalidVariableValueException; -import org.apache.shardingsphere.infra.exception.kernel.syntax.UnsupportedVariableException; import org.slf4j.LoggerFactory; import java.sql.SQLException; @@ -71,8 +71,8 @@ private void handleConfigurationProperty(final ContextManager contextManager, fi props.put(propertyKey.getKey(), getValue(propertyKey, value)); contextManager.getPersistServiceFacade().getMetaDataManagerPersistService().alterProperties(props); refreshRootLogger(props); - syncSQLShowToLoggingRule(propertyKey, metaDataContexts, value); - syncSQLSimpleToLoggingRule(propertyKey, metaDataContexts, value); + syncSQLShowToLoggingRule(propertyKey, metaDataContexts, value, contextManager); + syncSQLSimpleToLoggingRule(propertyKey, metaDataContexts, value, contextManager); } private Object getValue(final TypedPropertyKey propertyKey, final String value) { @@ -97,20 +97,20 @@ private void renewRootLoggerLevel(final Logger rootLogger, final Properties prop rootLogger.setLevel(Level.valueOf(props.getOrDefault(ConfigurationPropertyKey.SYSTEM_LOG_LEVEL.getKey(), ConfigurationPropertyKey.SYSTEM_LOG_LEVEL.getDefaultValue()).toString())); } - private void syncSQLShowToLoggingRule(final TypedPropertyKey propertyKey, final MetaDataContexts metaDataContexts, final String value) { + private void syncSQLShowToLoggingRule(final TypedPropertyKey propertyKey, final MetaDataContexts metaDataContexts, final String value, final ContextManager contextManager) { if (LoggingConstants.SQL_SHOW.equalsIgnoreCase(propertyKey.getKey())) { LoggingUtils.getSQLLogger(metaDataContexts.getMetaData().getGlobalRuleMetaData()).ifPresent(option -> { option.getProps().setProperty(LoggingConstants.SQL_LOG_ENABLE, value); - metaDataContexts.getPersistService().getGlobalRuleService().persist(metaDataContexts.getMetaData().getGlobalRuleMetaData().getConfigurations()); + contextManager.getPersistServiceFacade().getMetaDataPersistService().getGlobalRuleService().persist(metaDataContexts.getMetaData().getGlobalRuleMetaData().getConfigurations()); }); } } - private void syncSQLSimpleToLoggingRule(final TypedPropertyKey propertyKey, final MetaDataContexts metaDataContexts, final String value) { + private void syncSQLSimpleToLoggingRule(final TypedPropertyKey propertyKey, final MetaDataContexts metaDataContexts, final String value, final ContextManager contextManager) { if (LoggingConstants.SQL_SIMPLE.equalsIgnoreCase(propertyKey.getKey())) { LoggingUtils.getSQLLogger(metaDataContexts.getMetaData().getGlobalRuleMetaData()).ifPresent(option -> { option.getProps().setProperty(LoggingConstants.SQL_LOG_SIMPLE, value); - metaDataContexts.getPersistService().getGlobalRuleService().persist(metaDataContexts.getMetaData().getGlobalRuleMetaData().getConfigurations()); + contextManager.getPersistServiceFacade().getMetaDataPersistService().getGlobalRuleService().persist(metaDataContexts.getMetaData().getGlobalRuleMetaData().getConfigurations()); }); } } 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 7df940380bb26..565d8d7737ed6 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 @@ -130,7 +130,8 @@ private void importRules(final String databaseName, final Collection addRule(ruleConfigs, each, database)); - metaDataContexts.getPersistService().getDatabaseRulePersistService().persist(metaDataContexts.getMetaData().getDatabase(databaseName).getName(), ruleConfigs); + ProxyContext.getInstance().getContextManager().getPersistServiceFacade().getMetaDataPersistService().getDatabaseRulePersistService() + .persist(metaDataContexts.getMetaData().getDatabase(databaseName).getName(), ruleConfigs); } private void addRule(final Collection ruleConfigs, final RuleConfiguration ruleConfig, final ShardingSphereDatabase database) { diff --git a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/connector/DatabaseConnectorFactoryTest.java b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/connector/DatabaseConnectorFactoryTest.java index a062877b7d22d..e2d80476f57f8 100644 --- a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/connector/DatabaseConnectorFactoryTest.java +++ b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/connector/DatabaseConnectorFactoryTest.java @@ -30,6 +30,7 @@ import org.apache.shardingsphere.metadata.persist.MetaDataPersistService; import org.apache.shardingsphere.mode.manager.ContextManager; import org.apache.shardingsphere.mode.metadata.MetaDataContexts; +import org.apache.shardingsphere.mode.metadata.MetaDataContextsFactory; import org.apache.shardingsphere.proxy.backend.context.ProxyContext; import org.apache.shardingsphere.test.mock.AutoMockExtension; import org.apache.shardingsphere.test.mock.StaticMockSettings; @@ -83,7 +84,7 @@ void assertNewDatabaseConnectorWithParameters() { } private ContextManager mockContextManager(final ShardingSphereDatabase database) { - MetaDataContexts metaDataContexts = new MetaDataContexts(mock(MetaDataPersistService.class), + MetaDataContexts metaDataContexts = MetaDataContextsFactory.create(mock(MetaDataPersistService.class), new ShardingSphereMetaData(Collections.singletonMap("foo_db", database), mock(ResourceMetaData.class), mock(RuleMetaData.class), new ConfigurationProperties(new Properties()))); ContextManager result = mock(ContextManager.class, RETURNS_DEEP_STUBS); diff --git a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/connector/DatabaseConnectorTest.java b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/connector/DatabaseConnectorTest.java index f4e7beab42542..bdbd6cc904ad2 100644 --- a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/connector/DatabaseConnectorTest.java +++ b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/connector/DatabaseConnectorTest.java @@ -44,6 +44,7 @@ import org.apache.shardingsphere.metadata.persist.data.ShardingSphereDataPersistService; import org.apache.shardingsphere.mode.manager.ContextManager; import org.apache.shardingsphere.mode.metadata.MetaDataContexts; +import org.apache.shardingsphere.mode.metadata.MetaDataContextsFactory; import org.apache.shardingsphere.parser.config.SQLParserRuleConfiguration; import org.apache.shardingsphere.parser.rule.SQLParserRule; import org.apache.shardingsphere.proxy.backend.connector.jdbc.fixture.QueryHeaderBuilderFixture; @@ -122,7 +123,7 @@ private ContextManager mockContextManager() { when(shardingSphereDataPersistService.load(any())).thenReturn(Optional.empty()); when(metaDataPersistService.getShardingSphereDataPersistService()).thenReturn(shardingSphereDataPersistService); Map databases = mockDatabases(); - MetaDataContexts metaDataContexts = new MetaDataContexts(metaDataPersistService, + MetaDataContexts metaDataContexts = MetaDataContextsFactory.create(metaDataPersistService, new ShardingSphereMetaData(databases, mock(ResourceMetaData.class), globalRuleMetaData, new ConfigurationProperties(new Properties()))); ContextManager result = mock(ContextManager.class, RETURNS_DEEP_STUBS); when(result.getMetaDataContexts()).thenReturn(metaDataContexts); diff --git a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/connector/ProxyDatabaseConnectionManagerTest.java b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/connector/ProxyDatabaseConnectionManagerTest.java index d646d28e20d49..18d70ce4b9591 100644 --- a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/connector/ProxyDatabaseConnectionManagerTest.java +++ b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/connector/ProxyDatabaseConnectionManagerTest.java @@ -31,7 +31,8 @@ import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader; import org.apache.shardingsphere.metadata.persist.MetaDataPersistService; import org.apache.shardingsphere.mode.manager.ContextManager; -import org.apache.shardingsphere.mode.metadata.MetaDataContexts; +import org.apache.shardingsphere.mode.metadata.MetaDataContextsFactory; +import org.apache.shardingsphere.mode.spi.PersistRepository; import org.apache.shardingsphere.proxy.backend.connector.jdbc.connection.ConnectionPostProcessor; import org.apache.shardingsphere.proxy.backend.connector.jdbc.datasource.JDBCBackendDataSource; import org.apache.shardingsphere.proxy.backend.connector.jdbc.statement.JDBCBackendStatement; @@ -126,7 +127,7 @@ private ContextManager mockContextManager() { when(metaData.getGlobalRuleMetaData()).thenReturn(new RuleMetaData(Collections.singletonList(transactionRule))); ComputeNodeInstanceContext computeNodeInstanceContext = mock(ComputeNodeInstanceContext.class); when(computeNodeInstanceContext.getModeConfiguration()).thenReturn(mock(ModeConfiguration.class)); - return new ContextManager(new MetaDataContexts(mock(MetaDataPersistService.class), metaData), computeNodeInstanceContext); + return new ContextManager(MetaDataContextsFactory.create(mock(MetaDataPersistService.class), metaData), computeNodeInstanceContext, mock(PersistRepository.class)); } @AfterEach diff --git a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/connector/ProxySQLExecutorTest.java b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/connector/ProxySQLExecutorTest.java index ff1aca87b97d5..baf87a324d583 100644 --- a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/connector/ProxySQLExecutorTest.java +++ b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/connector/ProxySQLExecutorTest.java @@ -43,7 +43,8 @@ import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader; import org.apache.shardingsphere.metadata.persist.MetaDataPersistService; import org.apache.shardingsphere.mode.manager.ContextManager; -import org.apache.shardingsphere.mode.metadata.MetaDataContexts; +import org.apache.shardingsphere.mode.metadata.MetaDataContextsFactory; +import org.apache.shardingsphere.mode.spi.PersistRepository; import org.apache.shardingsphere.proxy.backend.context.ProxyContext; import org.apache.shardingsphere.proxy.backend.session.ConnectionSession; import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.item.ProjectionsSegment; @@ -111,7 +112,7 @@ void setUp() { when(metaData.getGlobalRuleMetaData()).thenReturn(new RuleMetaData(Arrays.asList(mock(SQLFederationRule.class), transactionRule))); ComputeNodeInstanceContext computeNodeInstanceContext = mock(ComputeNodeInstanceContext.class); when(computeNodeInstanceContext.getModeConfiguration()).thenReturn(mock(ModeConfiguration.class)); - ContextManager contextManager = new ContextManager(new MetaDataContexts(mock(MetaDataPersistService.class), metaData), computeNodeInstanceContext); + ContextManager contextManager = new ContextManager(MetaDataContextsFactory.create(mock(MetaDataPersistService.class), metaData), computeNodeInstanceContext, mock(PersistRepository.class)); when(ProxyContext.getInstance().getContextManager()).thenReturn(contextManager); } diff --git a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/connector/jdbc/datasource/JDBCBackendDataSourceTest.java b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/connector/jdbc/datasource/JDBCBackendDataSourceTest.java index 377abef1650e0..f2398095694aa 100644 --- a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/connector/jdbc/datasource/JDBCBackendDataSourceTest.java +++ b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/connector/jdbc/datasource/JDBCBackendDataSourceTest.java @@ -30,6 +30,7 @@ import org.apache.shardingsphere.metadata.persist.MetaDataPersistService; import org.apache.shardingsphere.mode.manager.ContextManager; import org.apache.shardingsphere.mode.metadata.MetaDataContexts; +import org.apache.shardingsphere.mode.metadata.MetaDataContextsFactory; import org.apache.shardingsphere.proxy.backend.connector.jdbc.datasource.fixture.CallTimeRecordDataSource; import org.apache.shardingsphere.proxy.backend.context.ProxyContext; import org.apache.shardingsphere.test.mock.AutoMockExtension; @@ -84,7 +85,7 @@ void setUp() { private ContextManager mockContextManager() { ContextManager result = mock(ContextManager.class, RETURNS_DEEP_STUBS); - MetaDataContexts metaDataContexts = new MetaDataContexts(mock(MetaDataPersistService.class), new ShardingSphereMetaData( + MetaDataContexts metaDataContexts = MetaDataContextsFactory.create(mock(MetaDataPersistService.class), new ShardingSphereMetaData( createDatabases(), mock(ResourceMetaData.class), new RuleMetaData(Collections.singleton(mock(TransactionRule.class, RETURNS_DEEP_STUBS))), new ConfigurationProperties(new Properties()))); when(result.getMetaDataContexts()).thenReturn(metaDataContexts); diff --git a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/context/BackendExecutorContextTest.java b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/context/BackendExecutorContextTest.java index eac07feec0688..c7f758f4f7343 100644 --- a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/context/BackendExecutorContextTest.java +++ b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/context/BackendExecutorContextTest.java @@ -21,6 +21,7 @@ import org.apache.shardingsphere.metadata.persist.MetaDataPersistService; import org.apache.shardingsphere.mode.manager.ContextManager; import org.apache.shardingsphere.mode.metadata.MetaDataContexts; +import org.apache.shardingsphere.mode.metadata.MetaDataContextsFactory; import org.apache.shardingsphere.test.mock.AutoMockExtension; import org.apache.shardingsphere.test.mock.StaticMockSettings; import org.junit.jupiter.api.Test; @@ -44,7 +45,7 @@ void assertGetInstance() { } private ContextManager mockContextManager() { - MetaDataContexts metaDataContexts = new MetaDataContexts(mock(MetaDataPersistService.class), new ShardingSphereMetaData()); + MetaDataContexts metaDataContexts = MetaDataContextsFactory.create(mock(MetaDataPersistService.class), new ShardingSphereMetaData()); ContextManager result = mock(ContextManager.class, RETURNS_DEEP_STUBS); when(result.getMetaDataContexts()).thenReturn(metaDataContexts); return result; diff --git a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/context/ProxyContextTest.java b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/context/ProxyContextTest.java index c36c59fdeabec..2951a46c86e54 100644 --- a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/context/ProxyContextTest.java +++ b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/context/ProxyContextTest.java @@ -29,6 +29,8 @@ import org.apache.shardingsphere.metadata.persist.MetaDataPersistService; import org.apache.shardingsphere.mode.manager.ContextManager; import org.apache.shardingsphere.mode.metadata.MetaDataContexts; +import org.apache.shardingsphere.mode.metadata.MetaDataContextsFactory; +import org.apache.shardingsphere.mode.spi.PersistRepository; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -67,8 +69,8 @@ void restorePreviousContextManager() { @Test void assertInit() { - MetaDataContexts metaDataContexts = new MetaDataContexts(mock(MetaDataPersistService.class), new ShardingSphereMetaData()); - ProxyContext.init(new ContextManager(metaDataContexts, mock(ComputeNodeInstanceContext.class, RETURNS_DEEP_STUBS))); + MetaDataContexts metaDataContexts = MetaDataContextsFactory.create(mock(MetaDataPersistService.class), new ShardingSphereMetaData()); + ProxyContext.init(new ContextManager(metaDataContexts, mock(ComputeNodeInstanceContext.class, RETURNS_DEEP_STUBS), mock(PersistRepository.class))); assertThat(ProxyContext.getInstance().getContextManager().getStateContext(), is(ProxyContext.getInstance().getContextManager().getStateContext())); assertThat(ProxyContext.getInstance().getContextManager().getStateContext().getCurrentClusterState(), is(ClusterState.OK)); assertThat(ProxyContext.getInstance().getContextManager().getMetaDataContexts(), is(ProxyContext.getInstance().getContextManager().getMetaDataContexts())); @@ -80,7 +82,7 @@ void assertInit() { void assertDatabaseExists() { Map databases = mockDatabases(); ContextManager contextManager = mock(ContextManager.class, RETURNS_DEEP_STUBS); - MetaDataContexts metaDataContexts = new MetaDataContexts(mock(MetaDataPersistService.class), + MetaDataContexts metaDataContexts = MetaDataContextsFactory.create(mock(MetaDataPersistService.class), new ShardingSphereMetaData(databases, mock(ResourceMetaData.class), mock(RuleMetaData.class), new ConfigurationProperties(new Properties()))); when(contextManager.getMetaDataContexts()).thenReturn(metaDataContexts); ProxyContext.init(contextManager); @@ -92,7 +94,7 @@ void assertDatabaseExists() { void assertGetAllDatabaseNames() { Map databases = createDatabases(); ContextManager contextManager = mock(ContextManager.class, RETURNS_DEEP_STUBS); - MetaDataContexts metaDataContexts = new MetaDataContexts(mock(MetaDataPersistService.class), + MetaDataContexts metaDataContexts = MetaDataContextsFactory.create(mock(MetaDataPersistService.class), new ShardingSphereMetaData(databases, mock(ResourceMetaData.class), mock(RuleMetaData.class), new ConfigurationProperties(new Properties()))); when(contextManager.getMetaDataContexts()).thenReturn(metaDataContexts); ProxyContext.init(contextManager); diff --git a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/admin/DatabaseAdminQueryBackendHandlerTest.java b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/admin/DatabaseAdminQueryBackendHandlerTest.java index 8be713fff8195..a2743076f67a2 100644 --- a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/admin/DatabaseAdminQueryBackendHandlerTest.java +++ b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/admin/DatabaseAdminQueryBackendHandlerTest.java @@ -19,6 +19,7 @@ import org.apache.shardingsphere.infra.config.mode.ModeConfiguration; import org.apache.shardingsphere.infra.config.props.ConfigurationProperties; +import org.apache.shardingsphere.infra.database.core.spi.DatabaseTypedSPILoader; import org.apache.shardingsphere.infra.database.core.type.DatabaseType; import org.apache.shardingsphere.infra.instance.ComputeNodeInstanceContext; import org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataMergedResult; @@ -27,11 +28,11 @@ import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; import org.apache.shardingsphere.infra.metadata.database.resource.ResourceMetaData; import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData; -import org.apache.shardingsphere.infra.database.core.spi.DatabaseTypedSPILoader; import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader; import org.apache.shardingsphere.metadata.persist.MetaDataPersistService; import org.apache.shardingsphere.mode.manager.ContextManager; -import org.apache.shardingsphere.mode.metadata.MetaDataContexts; +import org.apache.shardingsphere.mode.metadata.MetaDataContextsFactory; +import org.apache.shardingsphere.mode.spi.PersistRepository; import org.apache.shardingsphere.proxy.backend.context.ProxyContext; import org.apache.shardingsphere.proxy.backend.handler.admin.executor.DatabaseAdminQueryExecutor; import org.apache.shardingsphere.proxy.backend.response.header.query.QueryHeaderBuilder; @@ -117,6 +118,6 @@ private ContextManager mockContextManager() { mock(RuleMetaData.class), new ConfigurationProperties(new Properties())); ComputeNodeInstanceContext computeNodeInstanceContext = mock(ComputeNodeInstanceContext.class); when(computeNodeInstanceContext.getModeConfiguration()).thenReturn(mock(ModeConfiguration.class)); - return new ContextManager(new MetaDataContexts(mock(MetaDataPersistService.class), metaData), computeNodeInstanceContext); + return new ContextManager(MetaDataContextsFactory.create(mock(MetaDataPersistService.class), metaData), computeNodeInstanceContext, mock(PersistRepository.class)); } } diff --git a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/admin/executor/DefaultDatabaseMetaDataExecutorTest.java b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/admin/executor/DefaultDatabaseMetaDataExecutorTest.java index dc4bc475c7cac..5cdb9c92b9d01 100644 --- a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/admin/executor/DefaultDatabaseMetaDataExecutorTest.java +++ b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/admin/executor/DefaultDatabaseMetaDataExecutorTest.java @@ -31,6 +31,7 @@ import org.apache.shardingsphere.metadata.persist.MetaDataPersistService; import org.apache.shardingsphere.mode.manager.ContextManager; import org.apache.shardingsphere.mode.metadata.MetaDataContexts; +import org.apache.shardingsphere.mode.metadata.MetaDataContextsFactory; import org.apache.shardingsphere.proxy.backend.context.ProxyContext; import org.apache.shardingsphere.proxy.backend.handler.admin.executor.AbstractDatabaseMetaDataExecutor.DefaultDatabaseMetaDataExecutor; import org.apache.shardingsphere.proxy.backend.session.ConnectionSession; @@ -147,7 +148,7 @@ private ContextManager mockContextManager(final ShardingSphereDatabase database) AuthorityRule authorityRule = mock(AuthorityRule.class); when(authorityRule.findPrivileges(grantee)).thenReturn(Optional.of(new DatabasePermittedPrivileges(Collections.singleton("auth_db")))); ContextManager result = mock(ContextManager.class, RETURNS_DEEP_STUBS); - MetaDataContexts metaDataContexts = new MetaDataContexts(mock(MetaDataPersistService.class), new ShardingSphereMetaData(Collections.singletonMap("auth_db", database), + MetaDataContexts metaDataContexts = MetaDataContextsFactory.create(mock(MetaDataPersistService.class), new ShardingSphereMetaData(Collections.singletonMap("auth_db", database), mock(ResourceMetaData.class), new RuleMetaData(Collections.singleton(authorityRule)), new ConfigurationProperties(new Properties()))); when(result.getMetaDataContexts()).thenReturn(metaDataContexts); when(result.getDatabase("auth_db")).thenReturn(database); diff --git a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/data/impl/UnicastDatabaseBackendHandlerTest.java b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/data/impl/UnicastDatabaseBackendHandlerTest.java index 0280cc6de2a91..9a0be958f7547 100644 --- a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/data/impl/UnicastDatabaseBackendHandlerTest.java +++ b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/data/impl/UnicastDatabaseBackendHandlerTest.java @@ -31,6 +31,7 @@ import org.apache.shardingsphere.metadata.persist.MetaDataPersistService; import org.apache.shardingsphere.mode.manager.ContextManager; import org.apache.shardingsphere.mode.metadata.MetaDataContexts; +import org.apache.shardingsphere.mode.metadata.MetaDataContextsFactory; import org.apache.shardingsphere.proxy.backend.connector.DatabaseConnector; import org.apache.shardingsphere.proxy.backend.connector.DatabaseConnectorFactory; import org.apache.shardingsphere.proxy.backend.connector.ProxyDatabaseConnectionManager; @@ -130,7 +131,7 @@ void assertDatabaseUsingStream() throws SQLException { private ContextManager mockContextManager() { ContextManager result = mock(ContextManager.class, RETURNS_DEEP_STUBS); - MetaDataContexts metaDataContexts = new MetaDataContexts(mock(MetaDataPersistService.class), + MetaDataContexts metaDataContexts = MetaDataContextsFactory.create(mock(MetaDataPersistService.class), new ShardingSphereMetaData(createDatabases(), mock(ResourceMetaData.class), mock(RuleMetaData.class), new ConfigurationProperties(new Properties()))); when(result.getMetaDataContexts()).thenReturn(metaDataContexts); return result; diff --git a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/database/DatabaseOperateBackendHandlerFactoryTest.java b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/database/DatabaseOperateBackendHandlerFactoryTest.java index 59a0670298a7f..44e15ea89d87f 100644 --- a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/database/DatabaseOperateBackendHandlerFactoryTest.java +++ b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/database/DatabaseOperateBackendHandlerFactoryTest.java @@ -29,6 +29,7 @@ import org.apache.shardingsphere.metadata.persist.MetaDataPersistService; import org.apache.shardingsphere.mode.manager.ContextManager; import org.apache.shardingsphere.mode.metadata.MetaDataContexts; +import org.apache.shardingsphere.mode.metadata.MetaDataContextsFactory; import org.apache.shardingsphere.proxy.backend.context.ProxyContext; import org.apache.shardingsphere.proxy.backend.response.header.ResponseHeader; import org.apache.shardingsphere.proxy.backend.response.header.update.UpdateResponseHeader; @@ -78,7 +79,7 @@ class DatabaseOperateBackendHandlerFactoryTest { @BeforeEach void setUp() { - MetaDataContexts metaDataContexts = new MetaDataContexts(mock(MetaDataPersistService.class), + MetaDataContexts metaDataContexts = MetaDataContextsFactory.create(mock(MetaDataPersistService.class), new ShardingSphereMetaData(getDatabases(), mock(ResourceMetaData.class), mock(RuleMetaData.class), new ConfigurationProperties(new Properties()))); when(contextManager.getMetaDataContexts()).thenReturn(metaDataContexts); when(ProxyContext.getInstance().getContextManager()).thenReturn(contextManager); @@ -151,7 +152,7 @@ private Map getDatabases() { } private void setGovernanceMetaDataContexts(final boolean isGovernance) { - MetaDataContexts metaDataContexts = isGovernance ? mockMetaDataContexts() : new MetaDataContexts(mock(MetaDataPersistService.class), new ShardingSphereMetaData()); + MetaDataContexts metaDataContexts = isGovernance ? mockMetaDataContexts() : MetaDataContextsFactory.create(mock(MetaDataPersistService.class), new ShardingSphereMetaData()); when(contextManager.getMetaDataContexts()).thenReturn(metaDataContexts); } diff --git a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/QueryableRALBackendHandlerTest.java b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/QueryableRALBackendHandlerTest.java index be67ceb186837..b889c72c71345 100644 --- a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/QueryableRALBackendHandlerTest.java +++ b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/QueryableRALBackendHandlerTest.java @@ -36,6 +36,8 @@ import org.apache.shardingsphere.metadata.persist.MetaDataPersistService; import org.apache.shardingsphere.mode.manager.ContextManager; import org.apache.shardingsphere.mode.metadata.MetaDataContexts; +import org.apache.shardingsphere.mode.metadata.MetaDataContextsFactory; +import org.apache.shardingsphere.mode.spi.PersistRepository; import org.apache.shardingsphere.proxy.backend.context.ProxyContext; import org.apache.shardingsphere.proxy.backend.handler.distsql.DistSQLQueryBackendHandler; import org.apache.shardingsphere.proxy.backend.session.ConnectionSession; @@ -69,12 +71,12 @@ void assertExecuteWithNoDatabase() { @Test void assertExecuteWithUnknownDatabase() { ShardingSphereMetaData metaData = new ShardingSphereMetaData(); - MetaDataContexts metaDataContexts = new MetaDataContexts(mock(MetaDataPersistService.class), metaData); + MetaDataContexts metaDataContexts = MetaDataContextsFactory.create(mock(MetaDataPersistService.class), metaData); ConnectionSession connectionSession = mock(ConnectionSession.class, RETURNS_DEEP_STUBS); when(connectionSession.getDatabaseName()).thenReturn("unknown"); ComputeNodeInstanceContext computeNodeInstanceContext = mock(ComputeNodeInstanceContext.class); when(computeNodeInstanceContext.getModeConfiguration()).thenReturn(mock(ModeConfiguration.class)); - ContextManager contextManager = new ContextManager(metaDataContexts, computeNodeInstanceContext); + ContextManager contextManager = new ContextManager(metaDataContexts, computeNodeInstanceContext, mock(PersistRepository.class)); when(ProxyContext.getInstance().getContextManager()).thenReturn(contextManager); assertThrows(UnknownDatabaseException.class, () -> new DistSQLQueryBackendHandler(mock(ExportDatabaseConfigurationStatement.class), connectionSession).execute()); } diff --git a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/queryable/ExportMetaDataExecutorTest.java b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/queryable/ExportMetaDataExecutorTest.java index 602e04651f82e..8e181041a6f85 100644 --- a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/queryable/ExportMetaDataExecutorTest.java +++ b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/queryable/ExportMetaDataExecutorTest.java @@ -48,6 +48,7 @@ import org.apache.shardingsphere.mode.manager.ContextManager; import org.apache.shardingsphere.mode.manager.standalone.workerid.generator.StandaloneWorkerIdGenerator; import org.apache.shardingsphere.mode.metadata.MetaDataContexts; +import org.apache.shardingsphere.mode.metadata.MetaDataContextsFactory; import org.apache.shardingsphere.proxy.backend.config.yaml.YamlProxyDataSourceConfiguration; import org.apache.shardingsphere.proxy.backend.config.yaml.YamlProxyDatabaseConfiguration; import org.apache.shardingsphere.proxy.backend.config.yaml.YamlProxyServerConfiguration; @@ -125,7 +126,7 @@ void assertExecuteWithEmptyMetaData() { private ContextManager mockEmptyContextManager() { ContextManager result = mock(ContextManager.class, RETURNS_DEEP_STUBS); - MetaDataContexts metaDataContexts = new MetaDataContexts(mock(MetaDataPersistService.class), new ShardingSphereMetaData(new HashMap<>(), + MetaDataContexts metaDataContexts = MetaDataContextsFactory.create(mock(MetaDataPersistService.class), new ShardingSphereMetaData(new HashMap<>(), new ResourceMetaData(Collections.emptyMap()), new RuleMetaData(Collections.singletonList( new GlobalClockRule(new DefaultGlobalClockRuleConfigurationBuilder().build(), Collections.emptyMap()))), new ConfigurationProperties(new Properties()))); @@ -162,7 +163,7 @@ private Map createStorageUnits() { } private ContextManager mockContextManager() { - MetaDataContexts metaDataContexts = new MetaDataContexts(mock(MetaDataPersistService.class), new ShardingSphereMetaData(Collections.singletonMap(database.getName(), database), + MetaDataContexts metaDataContexts = MetaDataContextsFactory.create(mock(MetaDataPersistService.class), new ShardingSphereMetaData(Collections.singletonMap(database.getName(), database), new ResourceMetaData(Collections.emptyMap()), new RuleMetaData(Arrays.asList(new AuthorityRule(new DefaultAuthorityRuleConfigurationBuilder().build()), new GlobalClockRule(new DefaultGlobalClockRuleConfigurationBuilder().build(), Collections.singletonMap(database.getName(), database)))), diff --git a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/queryable/ExportStorageNodesExecutorTest.java b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/queryable/ExportStorageNodesExecutorTest.java index c8e8ccc07912e..12b966ea91962 100644 --- a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/queryable/ExportStorageNodesExecutorTest.java +++ b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/queryable/ExportStorageNodesExecutorTest.java @@ -35,6 +35,7 @@ import org.apache.shardingsphere.metadata.persist.MetaDataPersistService; import org.apache.shardingsphere.mode.manager.ContextManager; import org.apache.shardingsphere.mode.metadata.MetaDataContexts; +import org.apache.shardingsphere.mode.metadata.MetaDataContextsFactory; import org.apache.shardingsphere.proxy.backend.context.ProxyContext; import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration; import org.apache.shardingsphere.sharding.api.config.rule.ShardingTableRuleConfiguration; @@ -108,7 +109,7 @@ void assertExecuteWithEmptyMetaData() { private ContextManager mockEmptyContextManager() { ContextManager result = mock(ContextManager.class, RETURNS_DEEP_STUBS); - MetaDataContexts metaDataContexts = new MetaDataContexts(mock(MetaDataPersistService.class), new ShardingSphereMetaData(Collections.emptyMap(), + MetaDataContexts metaDataContexts = MetaDataContextsFactory.create(mock(MetaDataPersistService.class), new ShardingSphereMetaData(Collections.emptyMap(), new ResourceMetaData(Collections.emptyMap()), new RuleMetaData(Collections.emptyList()), new ConfigurationProperties(new Properties()))); when(result.getMetaDataContexts()).thenReturn(metaDataContexts); return result; @@ -144,7 +145,7 @@ void assertExecuteWithDatabaseName() { private ContextManager mockContextManager() { ContextManager result = mock(ContextManager.class, RETURNS_DEEP_STUBS); - MetaDataContexts metaDataContexts = new MetaDataContexts(mock(MetaDataPersistService.class), new ShardingSphereMetaData(Collections.singletonMap(database.getName(), database), + MetaDataContexts metaDataContexts = MetaDataContextsFactory.create(mock(MetaDataPersistService.class), new ShardingSphereMetaData(Collections.singletonMap(database.getName(), database), new ResourceMetaData(Collections.emptyMap()), new RuleMetaData(Collections.singleton(new AuthorityRule(new DefaultAuthorityRuleConfigurationBuilder().build()))), new ConfigurationProperties(PropertiesBuilder.build(new Property(ConfigurationPropertyKey.SQL_SHOW.getKey(), "true"))))); diff --git a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/SetDistVariableExecutorTest.java b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/SetDistVariableExecutorTest.java index ed0ab2f345766..37c543c9cd655 100644 --- a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/SetDistVariableExecutorTest.java +++ b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/SetDistVariableExecutorTest.java @@ -22,6 +22,7 @@ import org.apache.shardingsphere.infra.config.props.ConfigurationPropertyKey; import org.apache.shardingsphere.infra.config.props.temporary.TemporaryConfigurationPropertyKey; import org.apache.shardingsphere.infra.database.mysql.type.MySQLDatabaseType; +import org.apache.shardingsphere.infra.exception.kernel.syntax.InvalidVariableValueException; import org.apache.shardingsphere.infra.instance.ComputeNodeInstance; import org.apache.shardingsphere.infra.instance.ComputeNodeInstanceContext; import org.apache.shardingsphere.infra.instance.metadata.InstanceMetaData; @@ -32,9 +33,9 @@ import org.apache.shardingsphere.metadata.persist.MetaDataPersistService; import org.apache.shardingsphere.metadata.persist.service.config.global.PropertiesPersistService; import org.apache.shardingsphere.mode.manager.ContextManager; -import org.apache.shardingsphere.mode.metadata.MetaDataContexts; +import org.apache.shardingsphere.mode.metadata.MetaDataContextsFactory; +import org.apache.shardingsphere.mode.spi.PersistRepository; import org.apache.shardingsphere.proxy.backend.context.ProxyContext; -import org.apache.shardingsphere.infra.exception.kernel.syntax.InvalidVariableValueException; import org.apache.shardingsphere.test.mock.AutoMockExtension; import org.apache.shardingsphere.test.mock.StaticMockSettings; import org.junit.jupiter.api.Test; @@ -47,9 +48,9 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.junit.jupiter.api.Assertions.assertInstanceOf; import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.Mockito.RETURNS_DEEP_STUBS; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; -import static org.mockito.Mockito.RETURNS_DEEP_STUBS; @ExtendWith(AutoMockExtension.class) @StaticMockSettings(ProxyContext.class) @@ -107,9 +108,10 @@ void assertExecuteWithWrongSystemLogLevel() { private ContextManager mockContextManager() { MetaDataPersistService metaDataPersistService = mock(MetaDataPersistService.class, RETURNS_DEEP_STUBS); when(metaDataPersistService.getPropsService()).thenReturn(mock(PropertiesPersistService.class)); - ContextManager result = new ContextManager(new MetaDataContexts(metaDataPersistService, new ShardingSphereMetaData()), + ContextManager result = new ContextManager(MetaDataContextsFactory.create(metaDataPersistService, new ShardingSphereMetaData()), new ComputeNodeInstanceContext(new ComputeNodeInstance(mock(InstanceMetaData.class)), mock(WorkerIdGenerator.class), - new ModeConfiguration("Standalone", null), mock(LockContext.class), new EventBusContext())); + new ModeConfiguration("Standalone", null), mock(LockContext.class), new EventBusContext()), + mock(PersistRepository.class)); return result; } } diff --git a/proxy/backend/type/mysql/src/test/java/org/apache/shardingsphere/proxy/backend/mysql/handler/admin/MySQLAdminExecutorCreatorTest.java b/proxy/backend/type/mysql/src/test/java/org/apache/shardingsphere/proxy/backend/mysql/handler/admin/MySQLAdminExecutorCreatorTest.java index f73f2b24edad3..a84ec34397260 100644 --- a/proxy/backend/type/mysql/src/test/java/org/apache/shardingsphere/proxy/backend/mysql/handler/admin/MySQLAdminExecutorCreatorTest.java +++ b/proxy/backend/type/mysql/src/test/java/org/apache/shardingsphere/proxy/backend/mysql/handler/admin/MySQLAdminExecutorCreatorTest.java @@ -28,6 +28,7 @@ import org.apache.shardingsphere.metadata.persist.MetaDataPersistService; import org.apache.shardingsphere.mode.manager.ContextManager; import org.apache.shardingsphere.mode.metadata.MetaDataContexts; +import org.apache.shardingsphere.mode.metadata.MetaDataContextsFactory; import org.apache.shardingsphere.proxy.backend.context.ProxyContext; import org.apache.shardingsphere.proxy.backend.handler.admin.executor.AbstractDatabaseMetaDataExecutor.DefaultDatabaseMetaDataExecutor; import org.apache.shardingsphere.proxy.backend.handler.admin.executor.DatabaseAdminExecutor; @@ -354,7 +355,7 @@ void assertCreateWithSelectStatementFromPerformanceSchema() { private void initProxyContext(final Map databases) { ContextManager contextManager = mock(ContextManager.class, RETURNS_DEEP_STUBS); - MetaDataContexts metaDataContexts = new MetaDataContexts(mock(MetaDataPersistService.class), + MetaDataContexts metaDataContexts = MetaDataContextsFactory.create(mock(MetaDataPersistService.class), new ShardingSphereMetaData(databases, mock(ResourceMetaData.class), mock(RuleMetaData.class), new ConfigurationProperties(new Properties()))); when(contextManager.getMetaDataContexts()).thenReturn(metaDataContexts); when(ProxyContext.getInstance().getContextManager()).thenReturn(contextManager); diff --git a/proxy/backend/type/mysql/src/test/java/org/apache/shardingsphere/proxy/backend/mysql/handler/admin/executor/ShowCreateDatabaseExecutorTest.java b/proxy/backend/type/mysql/src/test/java/org/apache/shardingsphere/proxy/backend/mysql/handler/admin/executor/ShowCreateDatabaseExecutorTest.java index 2c8b650fe4004..eb560b6276b96 100644 --- a/proxy/backend/type/mysql/src/test/java/org/apache/shardingsphere/proxy/backend/mysql/handler/admin/executor/ShowCreateDatabaseExecutorTest.java +++ b/proxy/backend/type/mysql/src/test/java/org/apache/shardingsphere/proxy/backend/mysql/handler/admin/executor/ShowCreateDatabaseExecutorTest.java @@ -28,6 +28,7 @@ import org.apache.shardingsphere.metadata.persist.data.ShardingSphereDataPersistService; import org.apache.shardingsphere.mode.manager.ContextManager; import org.apache.shardingsphere.mode.metadata.MetaDataContexts; +import org.apache.shardingsphere.mode.metadata.MetaDataContextsFactory; import org.apache.shardingsphere.proxy.backend.context.ProxyContext; import org.apache.shardingsphere.proxy.backend.session.ConnectionSession; import org.apache.shardingsphere.sql.parser.sql.dialect.statement.mysql.dal.MySQLShowCreateDatabaseStatement; @@ -78,7 +79,7 @@ private ContextManager mockContextManager() { ShardingSphereDataPersistService shardingSphereDataPersistService = mock(ShardingSphereDataPersistService.class); when(shardingSphereDataPersistService.load(any())).thenReturn(Optional.empty()); when(metaDataPersistService.getShardingSphereDataPersistService()).thenReturn(shardingSphereDataPersistService); - MetaDataContexts metaDataContexts = new MetaDataContexts(metaDataPersistService, + MetaDataContexts metaDataContexts = MetaDataContextsFactory.create(metaDataPersistService, new ShardingSphereMetaData(databases, mock(ResourceMetaData.class), mock(RuleMetaData.class), new ConfigurationProperties(new Properties()))); ContextManager result = mock(ContextManager.class, RETURNS_DEEP_STUBS); when(result.getMetaDataContexts()).thenReturn(metaDataContexts); diff --git a/proxy/backend/type/mysql/src/test/java/org/apache/shardingsphere/proxy/backend/mysql/handler/admin/executor/ShowCurrentUserExecutorTest.java b/proxy/backend/type/mysql/src/test/java/org/apache/shardingsphere/proxy/backend/mysql/handler/admin/executor/ShowCurrentUserExecutorTest.java index fa5e4804bcda9..7892572ca27a9 100644 --- a/proxy/backend/type/mysql/src/test/java/org/apache/shardingsphere/proxy/backend/mysql/handler/admin/executor/ShowCurrentUserExecutorTest.java +++ b/proxy/backend/type/mysql/src/test/java/org/apache/shardingsphere/proxy/backend/mysql/handler/admin/executor/ShowCurrentUserExecutorTest.java @@ -27,6 +27,7 @@ import org.apache.shardingsphere.metadata.persist.MetaDataPersistService; import org.apache.shardingsphere.mode.manager.ContextManager; import org.apache.shardingsphere.mode.metadata.MetaDataContexts; +import org.apache.shardingsphere.mode.metadata.MetaDataContextsFactory; import org.apache.shardingsphere.proxy.backend.context.ProxyContext; import org.apache.shardingsphere.proxy.backend.session.ConnectionSession; import org.apache.shardingsphere.test.mock.AutoMockExtension; @@ -65,7 +66,7 @@ void assertExecute() throws SQLException { private ContextManager mockContextManager() { ContextManager result = mock(ContextManager.class, RETURNS_DEEP_STUBS); - MetaDataContexts metaDataContexts = new MetaDataContexts(mock(MetaDataPersistService.class), + MetaDataContexts metaDataContexts = MetaDataContextsFactory.create(mock(MetaDataPersistService.class), new ShardingSphereMetaData(Collections.emptyMap(), mock(ResourceMetaData.class), mockRuleMetaData(), new ConfigurationProperties(new Properties()))); when(result.getMetaDataContexts()).thenReturn(metaDataContexts); return result; diff --git a/proxy/backend/type/mysql/src/test/java/org/apache/shardingsphere/proxy/backend/mysql/handler/admin/executor/ShowDatabasesExecutorTest.java b/proxy/backend/type/mysql/src/test/java/org/apache/shardingsphere/proxy/backend/mysql/handler/admin/executor/ShowDatabasesExecutorTest.java index c7b09da8d7f52..ffa02896f53b4 100644 --- a/proxy/backend/type/mysql/src/test/java/org/apache/shardingsphere/proxy/backend/mysql/handler/admin/executor/ShowDatabasesExecutorTest.java +++ b/proxy/backend/type/mysql/src/test/java/org/apache/shardingsphere/proxy/backend/mysql/handler/admin/executor/ShowDatabasesExecutorTest.java @@ -32,6 +32,7 @@ import org.apache.shardingsphere.metadata.persist.data.ShardingSphereDataPersistService; import org.apache.shardingsphere.mode.manager.ContextManager; import org.apache.shardingsphere.mode.metadata.MetaDataContexts; +import org.apache.shardingsphere.mode.metadata.MetaDataContextsFactory; import org.apache.shardingsphere.proxy.backend.context.ProxyContext; import org.apache.shardingsphere.proxy.backend.session.ConnectionSession; import org.apache.shardingsphere.sql.parser.sql.common.segment.dal.ShowFilterSegment; @@ -188,7 +189,7 @@ private ContextManager mockContextManager() { ShardingSphereDataPersistService shardingSphereDataPersistService = mock(ShardingSphereDataPersistService.class); when(shardingSphereDataPersistService.load(any())).thenReturn(Optional.empty()); when(metaDataPersistService.getShardingSphereDataPersistService()).thenReturn(shardingSphereDataPersistService); - MetaDataContexts metaDataContexts = new MetaDataContexts(metaDataPersistService, new ShardingSphereMetaData(getDatabases(), + MetaDataContexts metaDataContexts = MetaDataContextsFactory.create(metaDataPersistService, new ShardingSphereMetaData(getDatabases(), mock(ResourceMetaData.class), globalRuleMetaData, new ConfigurationProperties(new Properties()))); when(result.getMetaDataContexts()).thenReturn(metaDataContexts); return result; diff --git a/proxy/backend/type/mysql/src/test/java/org/apache/shardingsphere/proxy/backend/mysql/handler/admin/executor/ShowFunctionStatusExecutorTest.java b/proxy/backend/type/mysql/src/test/java/org/apache/shardingsphere/proxy/backend/mysql/handler/admin/executor/ShowFunctionStatusExecutorTest.java index b894c4720acd5..11d5112e3a103 100644 --- a/proxy/backend/type/mysql/src/test/java/org/apache/shardingsphere/proxy/backend/mysql/handler/admin/executor/ShowFunctionStatusExecutorTest.java +++ b/proxy/backend/type/mysql/src/test/java/org/apache/shardingsphere/proxy/backend/mysql/handler/admin/executor/ShowFunctionStatusExecutorTest.java @@ -28,6 +28,7 @@ import org.apache.shardingsphere.metadata.persist.data.ShardingSphereDataPersistService; import org.apache.shardingsphere.mode.manager.ContextManager; import org.apache.shardingsphere.mode.metadata.MetaDataContexts; +import org.apache.shardingsphere.mode.metadata.MetaDataContextsFactory; import org.apache.shardingsphere.proxy.backend.context.ProxyContext; import org.apache.shardingsphere.proxy.backend.session.ConnectionSession; import org.apache.shardingsphere.sql.parser.sql.dialect.statement.mysql.dal.MySQLShowFunctionStatusStatement; @@ -70,7 +71,7 @@ private ContextManager mockContextManager() { ShardingSphereDataPersistService shardingSphereDataPersistService = mock(ShardingSphereDataPersistService.class); when(shardingSphereDataPersistService.load(any())).thenReturn(Optional.empty()); when(metaDataPersistService.getShardingSphereDataPersistService()).thenReturn(shardingSphereDataPersistService); - MetaDataContexts metaDataContexts = new MetaDataContexts(metaDataPersistService, + MetaDataContexts metaDataContexts = MetaDataContextsFactory.create(metaDataPersistService, new ShardingSphereMetaData(databases, mock(ResourceMetaData.class), mock(RuleMetaData.class), new ConfigurationProperties(new Properties()))); ContextManager result = mock(ContextManager.class, RETURNS_DEEP_STUBS); when(result.getMetaDataContexts()).thenReturn(metaDataContexts); diff --git a/proxy/backend/type/mysql/src/test/java/org/apache/shardingsphere/proxy/backend/mysql/handler/admin/executor/ShowProcedureStatusExecutorTest.java b/proxy/backend/type/mysql/src/test/java/org/apache/shardingsphere/proxy/backend/mysql/handler/admin/executor/ShowProcedureStatusExecutorTest.java index 926e2feb32efe..d5f753042c957 100644 --- a/proxy/backend/type/mysql/src/test/java/org/apache/shardingsphere/proxy/backend/mysql/handler/admin/executor/ShowProcedureStatusExecutorTest.java +++ b/proxy/backend/type/mysql/src/test/java/org/apache/shardingsphere/proxy/backend/mysql/handler/admin/executor/ShowProcedureStatusExecutorTest.java @@ -28,6 +28,7 @@ import org.apache.shardingsphere.metadata.persist.data.ShardingSphereDataPersistService; import org.apache.shardingsphere.mode.manager.ContextManager; import org.apache.shardingsphere.mode.metadata.MetaDataContexts; +import org.apache.shardingsphere.mode.metadata.MetaDataContextsFactory; import org.apache.shardingsphere.proxy.backend.context.ProxyContext; import org.apache.shardingsphere.proxy.backend.session.ConnectionSession; import org.apache.shardingsphere.sql.parser.sql.dialect.statement.mysql.dal.MySQLShowProcedureStatusStatement; @@ -70,7 +71,7 @@ private ContextManager mockContextManager() { ShardingSphereDataPersistService shardingSphereDataPersistService = mock(ShardingSphereDataPersistService.class); when(shardingSphereDataPersistService.load(any())).thenReturn(Optional.empty()); when(metaDataPersistService.getShardingSphereDataPersistService()).thenReturn(shardingSphereDataPersistService); - MetaDataContexts metaDataContexts = new MetaDataContexts(metaDataPersistService, + MetaDataContexts metaDataContexts = MetaDataContextsFactory.create(metaDataPersistService, new ShardingSphereMetaData(databases, mock(ResourceMetaData.class), mock(RuleMetaData.class), new ConfigurationProperties(new Properties()))); ContextManager result = mock(ContextManager.class, RETURNS_DEEP_STUBS); when(result.getMetaDataContexts()).thenReturn(metaDataContexts); diff --git a/proxy/backend/type/mysql/src/test/java/org/apache/shardingsphere/proxy/backend/mysql/handler/admin/executor/ShowTablesExecutorTest.java b/proxy/backend/type/mysql/src/test/java/org/apache/shardingsphere/proxy/backend/mysql/handler/admin/executor/ShowTablesExecutorTest.java index 9d34ccd60c2ef..da44464a64ee2 100644 --- a/proxy/backend/type/mysql/src/test/java/org/apache/shardingsphere/proxy/backend/mysql/handler/admin/executor/ShowTablesExecutorTest.java +++ b/proxy/backend/type/mysql/src/test/java/org/apache/shardingsphere/proxy/backend/mysql/handler/admin/executor/ShowTablesExecutorTest.java @@ -32,6 +32,7 @@ import org.apache.shardingsphere.metadata.persist.data.ShardingSphereDataPersistService; import org.apache.shardingsphere.mode.manager.ContextManager; import org.apache.shardingsphere.mode.metadata.MetaDataContexts; +import org.apache.shardingsphere.mode.metadata.MetaDataContextsFactory; import org.apache.shardingsphere.proxy.backend.context.ProxyContext; import org.apache.shardingsphere.proxy.backend.session.ConnectionSession; import org.apache.shardingsphere.sql.parser.sql.common.segment.dal.FromSchemaSegment; @@ -192,7 +193,7 @@ private ContextManager mockContextManager(final Map each)), mock(ResourceMetaData.class), new RuleMetaData(Collections.singleton(authorityRule)), new ConfigurationProperties(new Properties()))); when(result.getMetaDataContexts()).thenReturn(metaDataContexts); diff --git a/proxy/frontend/core/src/test/java/org/apache/shardingsphere/proxy/frontend/protocol/FrontDatabaseProtocolTypeFactoryTest.java b/proxy/frontend/core/src/test/java/org/apache/shardingsphere/proxy/frontend/protocol/FrontDatabaseProtocolTypeFactoryTest.java index f022bfb3bb8e7..dda70c3a2289f 100644 --- a/proxy/frontend/core/src/test/java/org/apache/shardingsphere/proxy/frontend/protocol/FrontDatabaseProtocolTypeFactoryTest.java +++ b/proxy/frontend/core/src/test/java/org/apache/shardingsphere/proxy/frontend/protocol/FrontDatabaseProtocolTypeFactoryTest.java @@ -31,6 +31,8 @@ import org.apache.shardingsphere.metadata.persist.data.ShardingSphereDataPersistService; import org.apache.shardingsphere.mode.manager.ContextManager; import org.apache.shardingsphere.mode.metadata.MetaDataContexts; +import org.apache.shardingsphere.mode.metadata.MetaDataContextsFactory; +import org.apache.shardingsphere.mode.spi.PersistRepository; import org.apache.shardingsphere.proxy.backend.context.ProxyContext; import org.apache.shardingsphere.test.mock.AutoMockExtension; import org.apache.shardingsphere.test.mock.StaticMockSettings; @@ -95,10 +97,10 @@ private ContextManager mockContextManager(final Map container = new GenericContainer<>(DockerImageName.parse("mysql:8.2.0-oracle")) + GenericContainer container = new GenericContainer<>(DockerImageName.parse("mysql:8.4.0-oracle")) .withEnv("MYSQL_DATABASE", DATABASE) .withEnv("MYSQL_ROOT_PASSWORD", PASSWORD) .withExposedPorts(3306)) { diff --git a/test/native/src/test/resources/test-native/yaml/databases/postgresql.yaml b/test/native/src/test/resources/test-native/yaml/databases/postgresql.yaml index 6011240591694..c3bc45ea435fa 100644 --- a/test/native/src/test/resources/test-native/yaml/databases/postgresql.yaml +++ b/test/native/src/test/resources/test-native/yaml/databases/postgresql.yaml @@ -24,15 +24,15 @@ dataSources: ds_0: dataSourceClassName: com.zaxxer.hikari.HikariDataSource driverClassName: org.testcontainers.jdbc.ContainerDatabaseDriver - jdbcUrl: jdbc:tc:postgresql:16.1-bookworm://test-native-databases-postgres/demo_ds_0?TC_DAEMON=true + jdbcUrl: jdbc:tc:postgresql:16.3-bookworm://test-native-databases-postgres/demo_ds_0 ds_1: dataSourceClassName: com.zaxxer.hikari.HikariDataSource driverClassName: org.testcontainers.jdbc.ContainerDatabaseDriver - jdbcUrl: jdbc:tc:postgresql:16.1-bookworm://test-native-databases-postgres/demo_ds_1?TC_DAEMON=true + jdbcUrl: jdbc:tc:postgresql:16.3-bookworm://test-native-databases-postgres/demo_ds_1 ds_2: dataSourceClassName: com.zaxxer.hikari.HikariDataSource driverClassName: org.testcontainers.jdbc.ContainerDatabaseDriver - jdbcUrl: jdbc:tc:postgresql:16.1-bookworm://test-native-databases-postgres/demo_ds_2?TC_DAEMON=true + jdbcUrl: jdbc:tc:postgresql:16.3-bookworm://test-native-databases-postgres/demo_ds_2 rules: - !SHARDING diff --git a/test/native/src/test/resources/test-native/yaml/databases/sqlserver.yaml b/test/native/src/test/resources/test-native/yaml/databases/sqlserver.yaml index df16a8fa3245c..a6e84a3b503e2 100644 --- a/test/native/src/test/resources/test-native/yaml/databases/sqlserver.yaml +++ b/test/native/src/test/resources/test-native/yaml/databases/sqlserver.yaml @@ -24,15 +24,15 @@ dataSources: ds_0: dataSourceClassName: com.zaxxer.hikari.HikariDataSource driverClassName: org.testcontainers.jdbc.ContainerDatabaseDriver - jdbcUrl: jdbc:tc:sqlserver:2022-CU10-ubuntu-22.04://test-native-databases-mssqlserver;databaseName=demo_ds_0;TC_DAEMON=true; + jdbcUrl: jdbc:tc:sqlserver:2022-CU10-ubuntu-22.04://test-native-databases-mssqlserver;databaseName=demo_ds_0; ds_1: dataSourceClassName: com.zaxxer.hikari.HikariDataSource driverClassName: org.testcontainers.jdbc.ContainerDatabaseDriver - jdbcUrl: jdbc:tc:sqlserver:2022-CU10-ubuntu-22.04://test-native-databases-mssqlserver;databaseName=demo_ds_1;TC_DAEMON=true; + jdbcUrl: jdbc:tc:sqlserver:2022-CU10-ubuntu-22.04://test-native-databases-mssqlserver;databaseName=demo_ds_1; ds_2: dataSourceClassName: com.zaxxer.hikari.HikariDataSource driverClassName: org.testcontainers.jdbc.ContainerDatabaseDriver - jdbcUrl: jdbc:tc:sqlserver:2022-CU10-ubuntu-22.04://test-native-databases-mssqlserver;databaseName=demo_ds_2;TC_DAEMON=true; + jdbcUrl: jdbc:tc:sqlserver:2022-CU10-ubuntu-22.04://test-native-databases-mssqlserver;databaseName=demo_ds_2; rules: - !SHARDING diff --git a/test/native/src/test/resources/test-native/yaml/transactions/base/seata.yaml b/test/native/src/test/resources/test-native/yaml/transactions/base/seata.yaml index eefd8ce8ff350..685a54bf261ab 100644 --- a/test/native/src/test/resources/test-native/yaml/transactions/base/seata.yaml +++ b/test/native/src/test/resources/test-native/yaml/transactions/base/seata.yaml @@ -24,15 +24,15 @@ dataSources: ds_0: dataSourceClassName: com.zaxxer.hikari.HikariDataSource driverClassName: org.testcontainers.jdbc.ContainerDatabaseDriver - jdbcUrl: jdbc:tc:postgresql:16.2-bookworm://test-native-transactions-base/demo_ds_0?TC_DAEMON=true?TC_INITSCRIPT=test-native/sql/seata-script-client-at-postgresql.sql + jdbcUrl: jdbc:tc:postgresql:16.3-bookworm://test-native-transactions-base/demo_ds_0?TC_INITSCRIPT=test-native/sql/seata-script-client-at-postgresql.sql ds_1: dataSourceClassName: com.zaxxer.hikari.HikariDataSource driverClassName: org.testcontainers.jdbc.ContainerDatabaseDriver - jdbcUrl: jdbc:tc:postgresql:16.2-bookworm://test-native-transactions-base/demo_ds_1?TC_DAEMON=true?TC_INITSCRIPT=test-native/sql/seata-script-client-at-postgresql.sql + jdbcUrl: jdbc:tc:postgresql:16.3-bookworm://test-native-transactions-base/demo_ds_1?TC_INITSCRIPT=test-native/sql/seata-script-client-at-postgresql.sql ds_2: dataSourceClassName: com.zaxxer.hikari.HikariDataSource driverClassName: org.testcontainers.jdbc.ContainerDatabaseDriver - jdbcUrl: jdbc:tc:postgresql:16.2-bookworm://test-native-transactions-base/demo_ds_2?TC_DAEMON=true?TC_INITSCRIPT=test-native/sql/seata-script-client-at-postgresql.sql + jdbcUrl: jdbc:tc:postgresql:16.3-bookworm://test-native-transactions-base/demo_ds_2?TC_INITSCRIPT=test-native/sql/seata-script-client-at-postgresql.sql rules: - !SHARDING