From 37da1d36b4cac856daf62102e240f19be95c2df3 Mon Sep 17 00:00:00 2001 From: zhaojinchao Date: Mon, 18 Mar 2024 10:56:55 +0800 Subject: [PATCH] Remove old meta data structure code (#30469) * Remove old meta data structure code * Fix ci * Fix ci * Fix ci * Fix ci * Fix checkstyle * Fix e2e-sql * Fix bug * Remove code --- .../impl/proxy/ProxyStateExporterTest.java | 2 +- .../PrometheusPluginLifecycleServiceTest.java | 2 +- ...DataNodeRuleConfigurationSwapperTest.java} | 2 +- ...DataNodeRuleConfigurationSwapperTest.java} | 2 +- ...DataNodeRuleConfigurationSwapperTest.java} | 2 +- ...DataNodeRuleConfigurationSwapperTest.java} | 2 +- ...DataNodeRuleConfigurationSwapperTest.java} | 2 +- ...DataNodeRuleConfigurationSwapperTest.java} | 2 +- .../YamlDataNodeConfigurationSwapper.java | 3 +- ...DataNodeRuleConfigurationSwapperTest.java} | 3 +- ...DataNodeRuleConfigurationSwapperTest.java} | 3 +- ...DataNodeRuleConfigurationSwapperTest.java} | 3 +- .../factory/InternalMetaDataFactory.java | 5 +- .../factory/NewInternalMetaDataFactory.java | 86 --- .../persist/MetaDataBasedPersistService.java | 1 - .../persist/MetaDataPersistService.java | 29 +- .../persist/NewMetaDataPersistService.java | 130 ----- .../persist/node/DatabaseMetaDataNode.java | 501 +++++++++++++----- .../metadata/persist/node/GlobalNode.java | 85 ++- .../persist/node/NewDatabaseMetaDataNode.java | 498 ----------------- .../metadata/persist/node/NewGlobalNode.java | 124 ----- .../DataSourceNodePersistService.java | 86 +-- .../DataSourceUnitPersistService.java | 86 +-- .../NewDataSourceNodePersistService.java | 121 ----- .../NewDataSourceUnitPersistService.java | 121 ----- .../rule/DatabaseRulePersistService.java | 108 +++- .../rule/NewDatabaseRulePersistService.java | 136 ----- .../global/GlobalRulePersistService.java | 72 ++- .../global/NewGlobalRulePersistService.java | 122 ----- .../global/NewPropertiesPersistService.java | 76 --- .../global/PropertiesPersistService.java | 29 +- .../DatabaseMetaDataPersistService.java | 13 +- .../NewDatabaseMetaDataPersistService.java | 170 ------ .../NewTableMetaDataPersistService.java | 112 ---- .../schema/NewViewMetaDataPersistService.java | 112 ---- .../schema/TableMetaDataPersistService.java | 54 +- .../schema/ViewMetaDataPersistService.java | 56 +- .../MetaDataVersionPersistService.java | 4 +- .../node/DatabaseMetaDataNodeTest.java | 160 ++++-- .../metadata/persist/node/GlobalNodeTest.java | 28 +- .../node/NewDatabaseMetaDataNodeTest.java | 195 ------- .../persist/node/NewGlobalNodeTest.java | 57 -- .../DatabaseMetaDataPersistServiceTest.java | 141 ----- .../MetaDataVersionPersistServiceTest.java | 48 -- .../DataSourceUnitPersistServiceTest.java | 100 ---- .../DatabaseRulePersistServiceTest.java | 65 --- .../global/GlobalRulePersistServiceTest.java | 65 --- .../global/PropertiesPersistServiceTest.java | 47 -- .../TableMetaDataPersistServiceTest.java | 77 --- .../ViewMetaDataPersistServiceTest.java | 77 --- .../resources/yaml/persist/data-source.yaml | 9 - ...DataNodeRuleConfigurationSwapperTest.java} | 3 +- ...DataNodeRuleConfigurationSwapperTest.java} | 3 +- ...DataNodeRuleConfigurationSwapperTest.java} | 3 +- ...DataNodeRuleConfigurationSwapperTest.java} | 3 +- ...DataNodeRuleConfigurationSwapperTest.java} | 3 +- ...DataNodeRuleConfigurationSwapperTest.java} | 3 +- .../mode/manager/ContextManager.java | 2 +- .../context/ConfigurationContextManager.java | 35 +- .../switcher/NewResourceSwitchManager.java | 139 ----- .../switcher/ResourceSwitchManager.java | 181 +++---- .../metadata/MetaDataContextsFactory.java | 7 +- .../metadata/NewMetaDataContextsFactory.java | 172 ------ .../mode/manager/ContextManagerTest.java | 13 - .../switcher/ResourceSwitchManagerTest.java | 106 ---- .../cluster/ClusterContextManagerBuilder.java | 6 +- .../cluster/ClusterModeContextManager.java | 67 ++- .../NewClusterContextManagerBuilder.java | 99 ---- .../cluster/NewClusterModeContextManager.java | 175 ------ .../coordinator/NewRegistryCenter.java | 129 ----- .../cluster/coordinator/RegistryCenter.java | 2 +- .../registry/GovernanceWatcher.java | 2 +- .../registry/GovernanceWatcherFactory.java | 2 +- .../registry/NewGovernanceWatcher.java | 58 -- .../registry/NewGovernanceWatcherFactory.java | 60 --- .../watcher/GlobalRuleChangedWatcher.java | 32 +- .../watcher/NewGlobalRuleChangedWatcher.java | 56 -- .../watcher/NewPropertiesChangedWatcher.java | 55 -- .../watcher/PropertiesChangedWatcher.java | 20 +- .../ShardingSphereDataChangedWatcher.java | 3 +- .../watcher/MetaDataChangedWatcher.java | 203 +++---- .../watcher/NewMetaDataChangedWatcher.java | 170 ------ .../NewProcessListChangedSubscriber.java | 115 ---- .../ProcessListChangedSubscriber.java | 3 +- .../watcher/ClusterStateChangedWatcher.java | 3 +- .../ComputeNodeStatusSubscriber.java | 3 +- .../NewComputeNodeStatusSubscriber.java | 69 --- .../ComputeNodeStateChangedWatcher.java | 3 +- .../StorageNodeStateChangedWatcher.java | 3 +- .../generator/ClusterWorkerIdGenerator.java | 2 +- .../NewClusterWorkerIdGenerator.java | 95 ---- .../ConfigurationChangedSubscriber.java | 130 ++--- .../ContextManagerSubscriberFacade.java | 2 +- .../NewConfigurationChangedSubscriber.java | 136 ----- .../NewContextManagerSubscriberFacade.java | 38 -- .../NewResourceMetaDataChangedSubscriber.java | 138 ----- .../subscriber/NewStateChangedSubscriber.java | 144 ----- .../ResourceMetaDataChangedSubscriber.java | 59 ++- .../subscriber/StateChangedSubscriber.java | 1 + ...PersistRepositoryConfigurationSwapper.java | 4 +- ...PersistRepositoryConfigurationSwapper.java | 50 -- ....YamlPersistRepositoryConfigurationSwapper | 1 - ...gsphere.mode.manager.ContextManagerBuilder | 1 - ...ter.coordinator.registry.GovernanceWatcher | 6 +- ....coordinator.registry.NewGovernanceWatcher | 24 - .../ClusterModeContextManagerTest.java | 479 ----------------- .../watcher/GlobalRuleChangedWatcherTest.java | 50 -- .../watcher/PropertiesChangedWatcherTest.java | 47 -- .../watcher/MetaDataChangedWatcherTest.java | 139 ----- .../ConfigurationChangedSubscriberTest.java | 175 ------ ...ResourceMetaDataChangedSubscriberTest.java | 22 - .../NewStandaloneContextManagerBuilder.java | 83 --- .../NewStandaloneModeContextManager.java | 333 ------------ .../StandaloneContextManagerBuilder.java | 15 +- .../StandaloneModeContextManager.java | 81 ++- ...PersistRepositoryConfigurationSwapper.java | 47 -- ...PersistRepositoryConfigurationSwapper.java | 4 +- ....YamlPersistRepositoryConfigurationSwapper | 1 - ...gsphere.mode.manager.ContextManagerBuilder | 1 - .../StandaloneContextManagerBuilderTest.java | 66 --- .../context/BackendExecutorContextTest.java | 2 +- .../executor/ShowCurrentUserExecutorTest.java | 2 +- .../PostgreSQLAuthenticationEngineTest.java | 2 +- .../PostgreSQLCommandExecuteEngineTest.java | 2 +- .../empty_rules/mysql/show_storage_units.xml | 36 +- .../core/util/PipelineContextUtils.java | 3 +- 126 files changed, 1465 insertions(+), 7001 deletions(-) rename features/broadcast/core/src/test/java/org/apache/shardingsphere/broadcast/yaml/swapper/{NewYamlBroadcastRuleConfigurationSwapperTest.java => YamlBroadcastDataNodeRuleConfigurationSwapperTest.java} (98%) rename features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/yaml/swapper/{NewYamlEncryptRuleConfigurationSwapperTest.java => YamlEncryptDataNodeRuleConfigurationSwapperTest.java} (98%) rename features/mask/core/src/test/java/org/apache/shardingsphere/mask/yaml/swapper/{NewYamlMaskRuleConfigurationSwapperTest.java => YamlMaskDataNodeRuleConfigurationSwapperTest.java} (98%) rename features/readwrite-splitting/core/src/test/java/org/apache/shardingsphere/readwritesplitting/yaml/swapper/{NewYamlReadwriteSplittingRuleConfigurationSwapperTest.java => YamlReadwriteSplittingDataNodeRuleConfigurationSwapperTest.java} (98%) rename features/shadow/core/src/test/java/org/apache/shardingsphere/shadow/yaml/swapper/{NewYamlShadowRuleConfigurationSwapperTest.java => YamlShadowDataNodeRuleConfigurationSwapperTest.java} (99%) rename features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/yaml/swapper/{NewYamlShardingRuleConfigurationSwapperTest.java => YamlShardingDataNodeRuleConfigurationSwapperTest.java} (99%) rename kernel/authority/core/src/test/java/org/apache/shardingsphere/authority/yaml/swapper/{NewYamlAuthorityRuleConfigurationSwapperTest.java => YamlAuthorityDataNodeRuleConfigurationSwapperTest.java} (94%) rename kernel/global-clock/core/src/test/java/org/apache/shardingsphere/globalclock/core/yaml/swapper/{NewYamlGlobalClockRuleConfigurationSwapperTest.java => YamlGlobalClockDataNodeRuleConfigurationSwapperTest.java} (90%) rename kernel/logging/core/src/test/java/org/apache/shardingsphere/logging/yaml/swapper/{NewYamlLoggingRuleConfigurationSwapperTest.java => YamlLoggingDataNodeRuleConfigurationSwapperTest.java} (89%) delete mode 100644 kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/factory/NewInternalMetaDataFactory.java delete mode 100644 kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/NewMetaDataPersistService.java delete mode 100644 kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/node/NewDatabaseMetaDataNode.java delete mode 100644 kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/node/NewGlobalNode.java delete mode 100644 kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/config/database/datasource/NewDataSourceNodePersistService.java delete mode 100644 kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/config/database/datasource/NewDataSourceUnitPersistService.java delete mode 100644 kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/config/database/rule/NewDatabaseRulePersistService.java delete mode 100644 kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/config/global/NewGlobalRulePersistService.java delete mode 100644 kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/config/global/NewPropertiesPersistService.java delete mode 100644 kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/database/NewDatabaseMetaDataPersistService.java delete mode 100644 kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/schema/NewTableMetaDataPersistService.java delete mode 100644 kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/schema/NewViewMetaDataPersistService.java delete mode 100644 kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/node/NewDatabaseMetaDataNodeTest.java delete mode 100644 kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/node/NewGlobalNodeTest.java delete mode 100644 kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/service/DatabaseMetaDataPersistServiceTest.java delete mode 100644 kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/service/MetaDataVersionPersistServiceTest.java delete mode 100644 kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/service/config/database/DataSourceUnitPersistServiceTest.java delete mode 100644 kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/service/config/database/DatabaseRulePersistServiceTest.java delete mode 100644 kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/service/config/global/GlobalRulePersistServiceTest.java delete mode 100644 kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/service/config/global/PropertiesPersistServiceTest.java delete mode 100644 kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/service/schema/TableMetaDataPersistServiceTest.java delete mode 100644 kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/service/schema/ViewMetaDataPersistServiceTest.java rename kernel/single/core/src/test/java/org/apache/shardingsphere/single/yaml/config/swapper/{NewYamlSingleRuleConfigurationSwapperTest.java => YamlSingleDataNodeRuleConfigurationSwapperTest.java} (89%) rename kernel/sql-federation/core/src/test/java/org/apache/shardingsphere/sqlfederation/yaml/swapper/{NewYamlSQLFederationRuleConfigurationSwapperTest.java => YamlSQLFederationDataNodeRuleConfigurationSwapperTest.java} (89%) rename kernel/sql-parser/core/src/test/java/org/apache/shardingsphere/parser/yaml/swapper/{NewYamlSQLParserRuleConfigurationSwapperTest.java => YamlSQLParserDataNodeRuleConfigurationSwapperTest.java} (90%) rename kernel/sql-translator/core/src/test/java/org/apache/shardingsphere/sqltranslator/yaml/swapper/{NewYamlSQLTranslatorRuleConfigurationSwapperTest.java => YamlSQLTranslatorDataNodeRuleConfigurationSwapperTest.java} (89%) rename kernel/traffic/core/src/test/java/org/apache/shardingsphere/traffic/yaml/swapper/{NewYamlTrafficRuleConfigurationSwapperTest.java => YamlTrafficDataNodeRuleConfigurationSwapperTest.java} (89%) rename kernel/transaction/core/src/test/java/org/apache/shardingsphere/transaction/yaml/swapper/{NewYamlTransactionRuleConfigurationSwapperTest.java => YamlTransactionDataNodeRuleConfigurationSwapperTest.java} (90%) delete mode 100644 mode/core/src/main/java/org/apache/shardingsphere/mode/manager/switcher/NewResourceSwitchManager.java delete mode 100644 mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/NewMetaDataContextsFactory.java delete mode 100644 mode/core/src/test/java/org/apache/shardingsphere/mode/manager/switcher/ResourceSwitchManagerTest.java delete mode 100644 mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/NewClusterContextManagerBuilder.java delete mode 100644 mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/NewClusterModeContextManager.java delete mode 100644 mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/NewRegistryCenter.java delete mode 100644 mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/NewGovernanceWatcher.java delete mode 100644 mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/NewGovernanceWatcherFactory.java delete mode 100644 mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/config/watcher/NewGlobalRuleChangedWatcher.java delete mode 100644 mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/config/watcher/NewPropertiesChangedWatcher.java delete mode 100644 mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/metadata/watcher/NewMetaDataChangedWatcher.java delete mode 100644 mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/process/subscriber/NewProcessListChangedSubscriber.java delete mode 100644 mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/status/compute/subscriber/NewComputeNodeStatusSubscriber.java delete mode 100644 mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/workerid/generator/NewClusterWorkerIdGenerator.java delete mode 100644 mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/subscriber/NewConfigurationChangedSubscriber.java delete mode 100644 mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/subscriber/NewContextManagerSubscriberFacade.java delete mode 100644 mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/subscriber/NewResourceMetaDataChangedSubscriber.java delete mode 100644 mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/subscriber/NewStateChangedSubscriber.java delete mode 100644 mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/yaml/NewClusterYamlPersistRepositoryConfigurationSwapper.java delete mode 100644 mode/type/cluster/core/src/main/resources/META-INF/services/org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.NewGovernanceWatcher delete mode 100644 mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/ClusterModeContextManagerTest.java delete mode 100644 mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/config/watcher/GlobalRuleChangedWatcherTest.java delete mode 100644 mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/config/watcher/PropertiesChangedWatcherTest.java delete mode 100644 mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/metadata/watcher/MetaDataChangedWatcherTest.java delete mode 100644 mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/subscriber/ConfigurationChangedSubscriberTest.java delete mode 100644 mode/type/standalone/core/src/main/java/org/apache/shardingsphere/mode/manager/standalone/NewStandaloneContextManagerBuilder.java delete mode 100644 mode/type/standalone/core/src/main/java/org/apache/shardingsphere/mode/manager/standalone/NewStandaloneModeContextManager.java delete mode 100644 mode/type/standalone/core/src/main/java/org/apache/shardingsphere/mode/manager/standalone/yaml/NewStandaloneYamlPersistRepositoryConfigurationSwapper.java delete mode 100644 mode/type/standalone/core/src/test/java/org/apache/shardingsphere/mode/manager/standalone/StandaloneContextManagerBuilderTest.java 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 42a9b4f2aee07..aa6edc64e4c89 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 @@ -31,10 +31,10 @@ import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData; import org.apache.shardingsphere.infra.state.instance.InstanceStateContext; import org.apache.shardingsphere.infra.util.eventbus.EventBusContext; +import org.apache.shardingsphere.metadata.persist.MetaDataPersistService; 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.metadata.persist.MetaDataPersistService; import org.apache.shardingsphere.proxy.backend.context.ProxyContext; import org.apache.shardingsphere.test.mock.AutoMockExtension; import org.apache.shardingsphere.test.mock.StaticMockSettings; 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 f135775836444..95c9237598b05 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 @@ -26,10 +26,10 @@ import org.apache.shardingsphere.infra.lock.LockContext; import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData; import org.apache.shardingsphere.infra.util.eventbus.EventBusContext; +import org.apache.shardingsphere.metadata.persist.MetaDataPersistService; 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.metadata.persist.MetaDataPersistService; import org.apache.shardingsphere.proxy.backend.context.ProxyContext; import org.apache.shardingsphere.test.mock.AutoMockExtension; import org.apache.shardingsphere.test.mock.StaticMockSettings; diff --git a/features/broadcast/core/src/test/java/org/apache/shardingsphere/broadcast/yaml/swapper/NewYamlBroadcastRuleConfigurationSwapperTest.java b/features/broadcast/core/src/test/java/org/apache/shardingsphere/broadcast/yaml/swapper/YamlBroadcastDataNodeRuleConfigurationSwapperTest.java similarity index 98% rename from features/broadcast/core/src/test/java/org/apache/shardingsphere/broadcast/yaml/swapper/NewYamlBroadcastRuleConfigurationSwapperTest.java rename to features/broadcast/core/src/test/java/org/apache/shardingsphere/broadcast/yaml/swapper/YamlBroadcastDataNodeRuleConfigurationSwapperTest.java index bdd416961712f..f392728391338 100644 --- a/features/broadcast/core/src/test/java/org/apache/shardingsphere/broadcast/yaml/swapper/NewYamlBroadcastRuleConfigurationSwapperTest.java +++ b/features/broadcast/core/src/test/java/org/apache/shardingsphere/broadcast/yaml/swapper/YamlBroadcastDataNodeRuleConfigurationSwapperTest.java @@ -31,7 +31,7 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.junit.jupiter.api.Assertions.assertFalse; -class NewYamlBroadcastRuleConfigurationSwapperTest { +class YamlBroadcastDataNodeRuleConfigurationSwapperTest { private final YamlBroadcastDataNodeRuleConfigurationSwapper swapper = new YamlBroadcastDataNodeRuleConfigurationSwapper(); diff --git a/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/yaml/swapper/NewYamlEncryptRuleConfigurationSwapperTest.java b/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/yaml/swapper/YamlEncryptDataNodeRuleConfigurationSwapperTest.java similarity index 98% rename from features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/yaml/swapper/NewYamlEncryptRuleConfigurationSwapperTest.java rename to features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/yaml/swapper/YamlEncryptDataNodeRuleConfigurationSwapperTest.java index 15a5ba124b19f..213815c43fdfc 100644 --- a/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/yaml/swapper/NewYamlEncryptRuleConfigurationSwapperTest.java +++ b/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/yaml/swapper/YamlEncryptDataNodeRuleConfigurationSwapperTest.java @@ -35,7 +35,7 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.junit.jupiter.api.Assertions.assertFalse; -class NewYamlEncryptRuleConfigurationSwapperTest { +class YamlEncryptDataNodeRuleConfigurationSwapperTest { private final YamlEncryptDataNodeRuleConfigurationSwapper swapper = new YamlEncryptDataNodeRuleConfigurationSwapper(); diff --git a/features/mask/core/src/test/java/org/apache/shardingsphere/mask/yaml/swapper/NewYamlMaskRuleConfigurationSwapperTest.java b/features/mask/core/src/test/java/org/apache/shardingsphere/mask/yaml/swapper/YamlMaskDataNodeRuleConfigurationSwapperTest.java similarity index 98% rename from features/mask/core/src/test/java/org/apache/shardingsphere/mask/yaml/swapper/NewYamlMaskRuleConfigurationSwapperTest.java rename to features/mask/core/src/test/java/org/apache/shardingsphere/mask/yaml/swapper/YamlMaskDataNodeRuleConfigurationSwapperTest.java index 3607354ea9c23..d37658ba0601f 100644 --- a/features/mask/core/src/test/java/org/apache/shardingsphere/mask/yaml/swapper/NewYamlMaskRuleConfigurationSwapperTest.java +++ b/features/mask/core/src/test/java/org/apache/shardingsphere/mask/yaml/swapper/YamlMaskDataNodeRuleConfigurationSwapperTest.java @@ -34,7 +34,7 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.junit.jupiter.api.Assertions.assertFalse; -class NewYamlMaskRuleConfigurationSwapperTest { +class YamlMaskDataNodeRuleConfigurationSwapperTest { private final YamlMaskDataNodeRuleConfigurationSwapper swapper = new YamlMaskDataNodeRuleConfigurationSwapper(); diff --git a/features/readwrite-splitting/core/src/test/java/org/apache/shardingsphere/readwritesplitting/yaml/swapper/NewYamlReadwriteSplittingRuleConfigurationSwapperTest.java b/features/readwrite-splitting/core/src/test/java/org/apache/shardingsphere/readwritesplitting/yaml/swapper/YamlReadwriteSplittingDataNodeRuleConfigurationSwapperTest.java similarity index 98% rename from features/readwrite-splitting/core/src/test/java/org/apache/shardingsphere/readwritesplitting/yaml/swapper/NewYamlReadwriteSplittingRuleConfigurationSwapperTest.java rename to features/readwrite-splitting/core/src/test/java/org/apache/shardingsphere/readwritesplitting/yaml/swapper/YamlReadwriteSplittingDataNodeRuleConfigurationSwapperTest.java index 54e33f992e1c4..02f85045d4b16 100644 --- a/features/readwrite-splitting/core/src/test/java/org/apache/shardingsphere/readwritesplitting/yaml/swapper/NewYamlReadwriteSplittingRuleConfigurationSwapperTest.java +++ b/features/readwrite-splitting/core/src/test/java/org/apache/shardingsphere/readwritesplitting/yaml/swapper/YamlReadwriteSplittingDataNodeRuleConfigurationSwapperTest.java @@ -35,7 +35,7 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.junit.jupiter.api.Assertions.assertFalse; -class NewYamlReadwriteSplittingRuleConfigurationSwapperTest { +class YamlReadwriteSplittingDataNodeRuleConfigurationSwapperTest { private final YamlReadwriteSplittingDataNodeRuleConfigurationSwapper swapper = new YamlReadwriteSplittingDataNodeRuleConfigurationSwapper(); diff --git a/features/shadow/core/src/test/java/org/apache/shardingsphere/shadow/yaml/swapper/NewYamlShadowRuleConfigurationSwapperTest.java b/features/shadow/core/src/test/java/org/apache/shardingsphere/shadow/yaml/swapper/YamlShadowDataNodeRuleConfigurationSwapperTest.java similarity index 99% rename from features/shadow/core/src/test/java/org/apache/shardingsphere/shadow/yaml/swapper/NewYamlShadowRuleConfigurationSwapperTest.java rename to features/shadow/core/src/test/java/org/apache/shardingsphere/shadow/yaml/swapper/YamlShadowDataNodeRuleConfigurationSwapperTest.java index 3baa0bb261ce5..14b96366435a4 100644 --- a/features/shadow/core/src/test/java/org/apache/shardingsphere/shadow/yaml/swapper/NewYamlShadowRuleConfigurationSwapperTest.java +++ b/features/shadow/core/src/test/java/org/apache/shardingsphere/shadow/yaml/swapper/YamlShadowDataNodeRuleConfigurationSwapperTest.java @@ -36,7 +36,7 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.junit.jupiter.api.Assertions.assertFalse; -class NewYamlShadowRuleConfigurationSwapperTest { +class YamlShadowDataNodeRuleConfigurationSwapperTest { private final YamlShadowDataNodeRuleConfigurationSwapper swapper = new YamlShadowDataNodeRuleConfigurationSwapper(); diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/yaml/swapper/NewYamlShardingRuleConfigurationSwapperTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/yaml/swapper/YamlShardingDataNodeRuleConfigurationSwapperTest.java similarity index 99% rename from features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/yaml/swapper/NewYamlShardingRuleConfigurationSwapperTest.java rename to features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/yaml/swapper/YamlShardingDataNodeRuleConfigurationSwapperTest.java index 5d52d3cd42344..2f3401124f96b 100644 --- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/yaml/swapper/NewYamlShardingRuleConfigurationSwapperTest.java +++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/yaml/swapper/YamlShardingDataNodeRuleConfigurationSwapperTest.java @@ -43,7 +43,7 @@ import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertTrue; -class NewYamlShardingRuleConfigurationSwapperTest { +class YamlShardingDataNodeRuleConfigurationSwapperTest { private final YamlShardingDataNodeRuleConfigurationSwapper swapper = new YamlShardingDataNodeRuleConfigurationSwapper(); diff --git a/infra/util/src/main/java/org/apache/shardingsphere/infra/util/yaml/swapper/YamlDataNodeConfigurationSwapper.java b/infra/util/src/main/java/org/apache/shardingsphere/infra/util/yaml/swapper/YamlDataNodeConfigurationSwapper.java index e72ea68a39617..9ed5b0cb9c71f 100644 --- a/infra/util/src/main/java/org/apache/shardingsphere/infra/util/yaml/swapper/YamlDataNodeConfigurationSwapper.java +++ b/infra/util/src/main/java/org/apache/shardingsphere/infra/util/yaml/swapper/YamlDataNodeConfigurationSwapper.java @@ -23,8 +23,7 @@ import java.util.Optional; /** - * TODO Rename YamlConfigurationSwapper when metadata structure adjustment completed. #25485 - * YAML configuration swapper. + * YAML data node configuration swapper. * * @param type of swapped object */ diff --git a/kernel/authority/core/src/test/java/org/apache/shardingsphere/authority/yaml/swapper/NewYamlAuthorityRuleConfigurationSwapperTest.java b/kernel/authority/core/src/test/java/org/apache/shardingsphere/authority/yaml/swapper/YamlAuthorityDataNodeRuleConfigurationSwapperTest.java similarity index 94% rename from kernel/authority/core/src/test/java/org/apache/shardingsphere/authority/yaml/swapper/NewYamlAuthorityRuleConfigurationSwapperTest.java rename to kernel/authority/core/src/test/java/org/apache/shardingsphere/authority/yaml/swapper/YamlAuthorityDataNodeRuleConfigurationSwapperTest.java index 09668934760d9..e726e301c9606 100644 --- a/kernel/authority/core/src/test/java/org/apache/shardingsphere/authority/yaml/swapper/NewYamlAuthorityRuleConfigurationSwapperTest.java +++ b/kernel/authority/core/src/test/java/org/apache/shardingsphere/authority/yaml/swapper/YamlAuthorityDataNodeRuleConfigurationSwapperTest.java @@ -31,8 +31,7 @@ import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; -// TODO Rename YamlAuthorityRuleConfigurationSwapperTest when metadata structure adjustment completed. #25485 -class NewYamlAuthorityRuleConfigurationSwapperTest { +class YamlAuthorityDataNodeRuleConfigurationSwapperTest { private final YamlAuthorityDataNodeRuleConfigurationSwapper swapper = new YamlAuthorityDataNodeRuleConfigurationSwapper(); diff --git a/kernel/global-clock/core/src/test/java/org/apache/shardingsphere/globalclock/core/yaml/swapper/NewYamlGlobalClockRuleConfigurationSwapperTest.java b/kernel/global-clock/core/src/test/java/org/apache/shardingsphere/globalclock/core/yaml/swapper/YamlGlobalClockDataNodeRuleConfigurationSwapperTest.java similarity index 90% rename from kernel/global-clock/core/src/test/java/org/apache/shardingsphere/globalclock/core/yaml/swapper/NewYamlGlobalClockRuleConfigurationSwapperTest.java rename to kernel/global-clock/core/src/test/java/org/apache/shardingsphere/globalclock/core/yaml/swapper/YamlGlobalClockDataNodeRuleConfigurationSwapperTest.java index efc36d9ebcde6..40898a8b6686d 100644 --- a/kernel/global-clock/core/src/test/java/org/apache/shardingsphere/globalclock/core/yaml/swapper/NewYamlGlobalClockRuleConfigurationSwapperTest.java +++ b/kernel/global-clock/core/src/test/java/org/apache/shardingsphere/globalclock/core/yaml/swapper/YamlGlobalClockDataNodeRuleConfigurationSwapperTest.java @@ -27,8 +27,7 @@ import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; -// TODO Rename YamlGlobalClockRuleConfigurationSwapperTest when metadata structure adjustment completed. #25485 -class NewYamlGlobalClockRuleConfigurationSwapperTest { +class YamlGlobalClockDataNodeRuleConfigurationSwapperTest { private final YamlGlobalClockDataNodeRuleConfigurationSwapper swapper = new YamlGlobalClockDataNodeRuleConfigurationSwapper(); diff --git a/kernel/logging/core/src/test/java/org/apache/shardingsphere/logging/yaml/swapper/NewYamlLoggingRuleConfigurationSwapperTest.java b/kernel/logging/core/src/test/java/org/apache/shardingsphere/logging/yaml/swapper/YamlLoggingDataNodeRuleConfigurationSwapperTest.java similarity index 89% rename from kernel/logging/core/src/test/java/org/apache/shardingsphere/logging/yaml/swapper/NewYamlLoggingRuleConfigurationSwapperTest.java rename to kernel/logging/core/src/test/java/org/apache/shardingsphere/logging/yaml/swapper/YamlLoggingDataNodeRuleConfigurationSwapperTest.java index 9c51f43b20f5d..376ede6f01f3c 100644 --- a/kernel/logging/core/src/test/java/org/apache/shardingsphere/logging/yaml/swapper/NewYamlLoggingRuleConfigurationSwapperTest.java +++ b/kernel/logging/core/src/test/java/org/apache/shardingsphere/logging/yaml/swapper/YamlLoggingDataNodeRuleConfigurationSwapperTest.java @@ -23,8 +23,7 @@ import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; -// TODO Rename YamlLoggingRuleConfigurationSwapperTest when metadata structure adjustment completed. #25485 -class NewYamlLoggingRuleConfigurationSwapperTest { +class YamlLoggingDataNodeRuleConfigurationSwapperTest { private final YamlLoggingDataNodeRuleConfigurationSwapper swapper = new YamlLoggingDataNodeRuleConfigurationSwapper(); diff --git a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/factory/InternalMetaDataFactory.java b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/factory/InternalMetaDataFactory.java index 45cc2e6b93362..ed551fdbe454d 100644 --- a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/factory/InternalMetaDataFactory.java +++ b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/factory/InternalMetaDataFactory.java @@ -27,6 +27,7 @@ import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; import org.apache.shardingsphere.infra.rule.builder.database.DatabaseRulesBuilder; import org.apache.shardingsphere.metadata.persist.MetaDataBasedPersistService; +import org.apache.shardingsphere.metadata.persist.MetaDataPersistService; import java.util.Map; import java.util.Map.Entry; @@ -64,12 +65,12 @@ public static ShardingSphereDatabase create(final String databaseName, final Met * @param instanceContext instance context * @return databases */ - public static Map create(final MetaDataBasedPersistService persistService, final Map databaseConfigMap, + public static Map create(final MetaDataPersistService persistService, final Map databaseConfigMap, final ConfigurationProperties props, final InstanceContext instanceContext) { return createDatabases(persistService, databaseConfigMap, DatabaseTypeEngine.getProtocolType(databaseConfigMap, props), props, instanceContext); } - private static Map createDatabases(final MetaDataBasedPersistService persistService, final Map databaseConfigMap, + private static Map createDatabases(final MetaDataPersistService persistService, final Map databaseConfigMap, final DatabaseType protocolType, final ConfigurationProperties props, final InstanceContext instanceContext) { Map result = new ConcurrentHashMap<>(databaseConfigMap.size(), 1F); for (Entry entry : databaseConfigMap.entrySet()) { diff --git a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/factory/NewInternalMetaDataFactory.java b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/factory/NewInternalMetaDataFactory.java deleted file mode 100644 index f64a9ae9e669f..0000000000000 --- a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/factory/NewInternalMetaDataFactory.java +++ /dev/null @@ -1,86 +0,0 @@ -/* - * 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.metadata.factory; - -import lombok.AccessLevel; -import lombok.NoArgsConstructor; -import org.apache.shardingsphere.infra.config.database.DatabaseConfiguration; -import org.apache.shardingsphere.infra.config.props.ConfigurationProperties; -import org.apache.shardingsphere.infra.database.DatabaseTypeEngine; -import org.apache.shardingsphere.infra.database.core.type.DatabaseType; -import org.apache.shardingsphere.infra.instance.InstanceContext; -import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; -import org.apache.shardingsphere.infra.rule.builder.database.DatabaseRulesBuilder; -import org.apache.shardingsphere.metadata.persist.NewMetaDataPersistService; - -import java.util.Map; -import java.util.Map.Entry; -import java.util.concurrent.ConcurrentHashMap; - -/** - * TODO replace the old implementation after meta data refactor completed - * New internal meta data factory. - */ -@NoArgsConstructor(access = AccessLevel.PRIVATE) -public final class NewInternalMetaDataFactory { - - /** - * Create database meta data for governance center. - * - * @param databaseName database name - * @param persistService meta data persist service - * @param databaseConfig database configuration - * @param props configuration properties - * @param instanceContext instance context - * @return database meta data - */ - public static ShardingSphereDatabase create(final String databaseName, final NewMetaDataPersistService persistService, final DatabaseConfiguration databaseConfig, - final ConfigurationProperties props, final InstanceContext instanceContext) { - DatabaseType protocolType = DatabaseTypeEngine.getProtocolType(databaseName, databaseConfig, props); - return ShardingSphereDatabase.create(databaseName, protocolType, databaseConfig, DatabaseRulesBuilder.build(databaseName, protocolType, databaseConfig, instanceContext), - persistService.getDatabaseMetaDataService().loadSchemas(databaseName)); - } - - /** - * Create databases meta data for governance center. - * - * @param persistService meta data persist service - * @param databaseConfigMap database configuration map - * @param props properties - * @param instanceContext instance context - * @return databases - */ - public static Map create(final NewMetaDataPersistService persistService, final Map databaseConfigMap, - final ConfigurationProperties props, final InstanceContext instanceContext) { - return createDatabases(persistService, databaseConfigMap, DatabaseTypeEngine.getProtocolType(databaseConfigMap, props), props, instanceContext); - } - - private static Map createDatabases(final NewMetaDataPersistService persistService, final Map databaseConfigMap, - final DatabaseType protocolType, final ConfigurationProperties props, final InstanceContext instanceContext) { - Map result = new ConcurrentHashMap<>(databaseConfigMap.size(), 1F); - for (Entry entry : databaseConfigMap.entrySet()) { - String databaseName = entry.getKey(); - if (entry.getValue().getStorageUnits().isEmpty()) { - result.put(databaseName.toLowerCase(), ShardingSphereDatabase.create(databaseName, protocolType, props)); - } else { - result.put(databaseName.toLowerCase(), create(databaseName, persistService, entry.getValue(), props, instanceContext)); - } - } - return result; - } -} diff --git a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/MetaDataBasedPersistService.java b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/MetaDataBasedPersistService.java index f4adc504550ab..58ea367569f01 100644 --- a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/MetaDataBasedPersistService.java +++ b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/MetaDataBasedPersistService.java @@ -35,7 +35,6 @@ import java.util.Properties; /** - * TODO replace the old implementation after meta data refactor completed * Abstract meta data persist service. */ public interface MetaDataBasedPersistService { diff --git a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/MetaDataPersistService.java b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/MetaDataPersistService.java index c0a581bd684d4..6553944e7d46b 100644 --- a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/MetaDataPersistService.java +++ b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/MetaDataPersistService.java @@ -47,13 +47,15 @@ import java.util.stream.Collectors; /** - * Meta data persist service. + * New meta data persist service. */ @Getter public final class MetaDataPersistService implements MetaDataBasedPersistService { private final PersistRepository repository; + private final MetaDataVersionPersistService metaDataVersionPersistService; + private final DataSourceUnitPersistService dataSourceUnitService; private final DataSourceNodePersistService dataSourceNodeService; @@ -66,22 +68,26 @@ public final class MetaDataPersistService implements MetaDataBasedPersistService private final PropertiesPersistService propsService; - private final MetaDataVersionPersistService metaDataVersionPersistService; - private final ShardingSphereDataPersistService shardingSphereDataPersistService; public MetaDataPersistService(final PersistRepository repository) { this.repository = repository; + metaDataVersionPersistService = new MetaDataVersionPersistService(repository); dataSourceUnitService = new DataSourceUnitPersistService(repository); dataSourceNodeService = new DataSourceNodePersistService(repository); - databaseMetaDataService = new DatabaseMetaDataPersistService(repository); + databaseMetaDataService = new DatabaseMetaDataPersistService(repository, metaDataVersionPersistService); databaseRulePersistService = new DatabaseRulePersistService(repository); globalRuleService = new GlobalRulePersistService(repository); propsService = new PropertiesPersistService(repository); - metaDataVersionPersistService = new MetaDataVersionPersistService(repository); shardingSphereDataPersistService = new ShardingSphereDataPersistService(repository); } + /** + * Persist global rule configurations. + * + * @param globalRuleConfigs global rule configurations + * @param props properties + */ @Override public void persistGlobalRuleConfiguration(final Collection globalRuleConfigs, final Properties props) { globalRuleService.persist(globalRuleConfigs); @@ -89,8 +95,7 @@ public void persistGlobalRuleConfiguration(final Collection g } @Override - public void persistConfigurations(final String databaseName, final DatabaseConfiguration databaseConfigs, - final Map dataSources, final Collection rules) { + public void persistConfigurations(final String databaseName, final DatabaseConfiguration databaseConfigs, final Map dataSources, final Collection rules) { Map propsMap = getDataSourcePoolPropertiesMap(databaseConfigs); if (propsMap.isEmpty() && databaseConfigs.getRuleConfigurations().isEmpty()) { databaseMetaDataService.addDatabase(databaseName); @@ -100,11 +105,6 @@ public void persistConfigurations(final String databaseName, final DatabaseConfi } } - private Map getDataSourcePoolPropertiesMap(final DatabaseConfiguration databaseConfigs) { - return databaseConfigs.getStorageUnits().entrySet().stream() - .collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getDataSourcePoolProperties(), (oldValue, currentValue) -> oldValue, LinkedHashMap::new)); - } - @SuppressWarnings({"unchecked", "rawtypes"}) private Collection decorateRuleConfigs(final String databaseName, final Map dataSources, final Collection rules) { Collection result = new LinkedList<>(); @@ -116,6 +116,11 @@ private Collection decorateRuleConfigs(final String databaseN return result; } + private Map getDataSourcePoolPropertiesMap(final DatabaseConfiguration databaseConfigs) { + return databaseConfigs.getStorageUnits().entrySet().stream() + .collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getDataSourcePoolProperties(), (oldValue, currentValue) -> oldValue, LinkedHashMap::new)); + } + @Override public Map loadDataSourceConfigurations(final String databaseName) { return dataSourceUnitService.load(databaseName).entrySet().stream().collect(Collectors.toMap(Entry::getKey, diff --git a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/NewMetaDataPersistService.java b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/NewMetaDataPersistService.java deleted file mode 100644 index af93d8ae189ec..0000000000000 --- a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/NewMetaDataPersistService.java +++ /dev/null @@ -1,130 +0,0 @@ -/* - * 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.metadata.persist; - -import lombok.Getter; -import org.apache.shardingsphere.infra.config.database.DatabaseConfiguration; -import org.apache.shardingsphere.infra.config.rule.RuleConfiguration; -import org.apache.shardingsphere.infra.config.rule.decorator.RuleConfigurationDecorator; -import org.apache.shardingsphere.infra.datasource.pool.config.DataSourceConfiguration; -import org.apache.shardingsphere.infra.datasource.pool.props.creator.DataSourcePoolPropertiesCreator; -import org.apache.shardingsphere.infra.datasource.pool.props.domain.DataSourcePoolProperties; -import org.apache.shardingsphere.infra.rule.ShardingSphereRule; -import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader; -import org.apache.shardingsphere.metadata.persist.data.ShardingSphereDataPersistService; -import org.apache.shardingsphere.metadata.persist.service.config.database.datasource.NewDataSourceNodePersistService; -import org.apache.shardingsphere.metadata.persist.service.config.database.datasource.NewDataSourceUnitPersistService; -import org.apache.shardingsphere.metadata.persist.service.config.database.rule.NewDatabaseRulePersistService; -import org.apache.shardingsphere.metadata.persist.service.config.global.NewGlobalRulePersistService; -import org.apache.shardingsphere.metadata.persist.service.config.global.NewPropertiesPersistService; -import org.apache.shardingsphere.metadata.persist.service.database.NewDatabaseMetaDataPersistService; -import org.apache.shardingsphere.metadata.persist.service.version.MetaDataVersionPersistService; -import org.apache.shardingsphere.mode.spi.PersistRepository; - -import javax.sql.DataSource; -import java.util.Collection; -import java.util.LinkedHashMap; -import java.util.LinkedList; -import java.util.Map; -import java.util.Map.Entry; -import java.util.Optional; -import java.util.Properties; -import java.util.stream.Collectors; - -/** - * TODO replace the old implementation after meta data refactor completed - * New meta data persist service. - */ -@Getter -public final class NewMetaDataPersistService implements MetaDataBasedPersistService { - - private final PersistRepository repository; - - private final MetaDataVersionPersistService metaDataVersionPersistService; - - private final NewDataSourceUnitPersistService dataSourceUnitService; - - private final NewDataSourceNodePersistService dataSourceNodeService; - - private final NewDatabaseMetaDataPersistService databaseMetaDataService; - - private final NewDatabaseRulePersistService databaseRulePersistService; - - private final NewGlobalRulePersistService globalRuleService; - - private final NewPropertiesPersistService propsService; - - private final ShardingSphereDataPersistService shardingSphereDataPersistService; - - public NewMetaDataPersistService(final PersistRepository repository) { - this.repository = repository; - metaDataVersionPersistService = new MetaDataVersionPersistService(repository); - dataSourceUnitService = new NewDataSourceUnitPersistService(repository); - dataSourceNodeService = new NewDataSourceNodePersistService(repository); - databaseMetaDataService = new NewDatabaseMetaDataPersistService(repository, metaDataVersionPersistService); - databaseRulePersistService = new NewDatabaseRulePersistService(repository); - globalRuleService = new NewGlobalRulePersistService(repository); - propsService = new NewPropertiesPersistService(repository); - shardingSphereDataPersistService = new ShardingSphereDataPersistService(repository); - } - - /** - * Persist global rule configurations. - * - * @param globalRuleConfigs global rule configurations - * @param props properties - */ - @Override - public void persistGlobalRuleConfiguration(final Collection globalRuleConfigs, final Properties props) { - globalRuleService.persist(globalRuleConfigs); - propsService.persist(props); - } - - @Override - public void persistConfigurations(final String databaseName, final DatabaseConfiguration databaseConfigs, final Map dataSources, final Collection rules) { - Map propsMap = getDataSourcePoolPropertiesMap(databaseConfigs); - if (propsMap.isEmpty() && databaseConfigs.getRuleConfigurations().isEmpty()) { - databaseMetaDataService.addDatabase(databaseName); - } else { - dataSourceUnitService.persist(databaseName, propsMap); - databaseRulePersistService.persist(databaseName, decorateRuleConfigs(databaseName, dataSources, rules)); - } - } - - @SuppressWarnings({"unchecked", "rawtypes"}) - private Collection decorateRuleConfigs(final String databaseName, final Map dataSources, final Collection rules) { - Collection result = new LinkedList<>(); - for (ShardingSphereRule each : rules) { - RuleConfiguration ruleConfig = each.getConfiguration(); - Optional decorator = TypedSPILoader.findService(RuleConfigurationDecorator.class, ruleConfig.getClass()); - result.add(decorator.map(optional -> optional.decorate(databaseName, dataSources, rules, ruleConfig)).orElse(ruleConfig)); - } - return result; - } - - private Map getDataSourcePoolPropertiesMap(final DatabaseConfiguration databaseConfigs) { - return databaseConfigs.getStorageUnits().entrySet().stream() - .collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getDataSourcePoolProperties(), (oldValue, currentValue) -> oldValue, LinkedHashMap::new)); - } - - @Override - public Map loadDataSourceConfigurations(final String databaseName) { - return dataSourceUnitService.load(databaseName).entrySet().stream().collect(Collectors.toMap(Entry::getKey, - entry -> DataSourcePoolPropertiesCreator.createConfiguration(entry.getValue()), (oldValue, currentValue) -> oldValue, LinkedHashMap::new)); - } -} diff --git a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/node/DatabaseMetaDataNode.java b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/node/DatabaseMetaDataNode.java index 99cce02855c7e..1b5cd7f7c88ba 100644 --- a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/node/DatabaseMetaDataNode.java +++ b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/node/DatabaseMetaDataNode.java @@ -32,9 +32,7 @@ public final class DatabaseMetaDataNode { private static final String ROOT_NODE = "metadata"; - private static final String SCHEMAS_NODE = "schemas"; - - private static final String DATA_SOURCE_NODE = "data_sources"; + private static final String DATA_SOURCES_NODE = "data_sources"; private static final String DATA_SOURCE_NODES_NODE = "nodes"; @@ -42,6 +40,8 @@ public final class DatabaseMetaDataNode { private static final String RULE_NODE = "rules"; + private static final String SCHEMAS_NODE = "schemas"; + private static final String TABLES_NODE = "tables"; private static final String VIEWS_NODE = "views"; @@ -50,255 +50,506 @@ public final class DatabaseMetaDataNode { private static final String VERSIONS = "versions"; + private static final String TABLES_PATTERN = "/([\\w\\-]+)/schemas/([\\w\\-]+)/tables"; + + private static final String VIEWS_PATTERN = "/([\\w\\-]+)/schemas/([\\w\\-]+)/views"; + + private static final String ACTIVE_VERSION_SUFFIX = "/([\\w\\-]+)/active_version"; + /** - * Get meta data data source nodes path. + * Get database name path. * * @param databaseName database name - * @param version data source version - * @return data source path + * @return database name path */ - public static String getMetaDataDataSourceUnitsPath(final String databaseName, final String version) { - return String.join("/", getFullMetaDataPath(databaseName, VERSIONS), version, DATA_SOURCE_NODE, DATA_SOURCE_UNITS_NODE); + public static String getDatabaseNamePath(final String databaseName) { + return String.join("/", getMetaDataNode(), databaseName); } /** - * Get meta data data source units path. + * Get meta data tables path. * * @param databaseName database name - * @param version data source version - * @return data source path + * @param schemaName schema name + * @return tables path */ - public static String getMetaDataDataSourceNodesPath(final String databaseName, final String version) { - return String.join("/", getFullMetaDataPath(databaseName, VERSIONS), version, DATA_SOURCE_NODE, DATA_SOURCE_NODES_NODE); + public static String getMetaDataTablesPath(final String databaseName, final String schemaName) { + return String.join("/", getMetaDataSchemaPath(databaseName, schemaName), TABLES_NODE); } /** - * Get meta data node path. + * Get meta data views path. * - * @return meta data node path + * @param databaseName database name + * @param schemaName schema name + * @return views path */ - public static String getMetaDataNodePath() { - return String.join("/", "", ROOT_NODE); + public static String getMetaDataViewsPath(final String databaseName, final String schemaName) { + return String.join("/", getMetaDataSchemaPath(databaseName, schemaName), VIEWS_NODE); } /** - * Get database name path. + * Get schema path. * * @param databaseName database name - * @return database name path + * @param schemaName schema name + * @return tables path */ - public static String getDatabaseNamePath(final String databaseName) { - return String.join("/", getMetaDataNodePath(), databaseName); + public static String getMetaDataSchemaPath(final String databaseName, final String schemaName) { + return String.join("/", getMetaDataSchemasPath(databaseName), schemaName); } /** - * Get rule path. + * Get meta data schemas path. * * @param databaseName database name - * @param version rule version - * @return rule path + * @return schemas path */ - public static String getRulePath(final String databaseName, final String version) { - return String.join("/", getFullMetaDataPath(databaseName, VERSIONS), version, RULE_NODE); + public static String getMetaDataSchemasPath(final String databaseName) { + return String.join("/", getDatabaseNamePath(databaseName), SCHEMAS_NODE); } /** - * Get meta data tables path. + * Get data source units node. + * + * @param databaseName database name + * @return data sources node + */ + public static String getDataSourceUnitsNode(final String databaseName) { + return String.join("/", getMetaDataNode(), databaseName, DATA_SOURCES_NODE, DATA_SOURCE_UNITS_NODE); + } + + /** + * Get data source nodes node. + * + * @param databaseName database name + * @return data sources node + */ + public static String getDataSourceNodesNode(final String databaseName) { + return String.join("/", getMetaDataNode(), databaseName, DATA_SOURCES_NODE, DATA_SOURCE_NODES_NODE); + } + + /** + * Get data source unit node. + * + * @param databaseName database name + * @param dataSourceName data source name + * @return data source node + */ + public static String getDataSourceUnitNode(final String databaseName, final String dataSourceName) { + return String.join("/", getDataSourceUnitsNode(databaseName), dataSourceName); + } + + /** + * Get data source unit node. + * + * @param databaseName database name + * @param dataSourceName data source name + * @return data source node + */ + public static String getDataSourceNode(final String databaseName, final String dataSourceName) { + return String.join("/", getDataSourceUnitsNode(databaseName), dataSourceName); + } + + /** + * Get data source unit node with version. + * + * @param databaseName database name + * @param dataSourceName data source name + * @param version version + * @return data source node with version + */ + public static String getDataSourceUnitNodeWithVersion(final String databaseName, final String dataSourceName, final String version) { + return String.join("/", getDataSourceUnitVersionsNode(databaseName, dataSourceName), version); + } + + /** + * Get data source node with version. + * + * @param databaseName database name + * @param dataSourceName data source name + * @param version version + * @return data source node with version + */ + public static String getDataSourceNodeWithVersion(final String databaseName, final String dataSourceName, final String version) { + return String.join("/", getDataSourceNodeVersionsNode(databaseName, dataSourceName), version); + } + + /** + * Get data source unit active version node. + * + * @param databaseName database name + * @param dataSourceName data source name + * @return data source active version node + */ + public static String getDataSourceUnitActiveVersionNode(final String databaseName, final String dataSourceName) { + return String.join("/", getDataSourceUnitsNode(databaseName), dataSourceName, ACTIVE_VERSION); + } + + /** + * Get data source node active version node. + * + * @param databaseName database name + * @param dataSourceName data source name + * @return data source active version node + */ + public static String getDataSourceNodeActiveVersionNode(final String databaseName, final String dataSourceName) { + return String.join("/", getDataSourceNodesNode(databaseName), dataSourceName, ACTIVE_VERSION); + } + + /** + * Get data source unit versions node. + * + * @param databaseName database name + * @param dataSourceName data source name + * @return data source versions node + */ + public static String getDataSourceUnitVersionsNode(final String databaseName, final String dataSourceName) { + return String.join("/", getDataSourceUnitsNode(databaseName), dataSourceName, VERSIONS); + } + + /** + * Get data source node versions node. + * + * @param databaseName database name + * @param dataSourceName data source name + * @return data source versions node + */ + public static String getDataSourceNodeVersionsNode(final String databaseName, final String dataSourceName) { + return String.join("/", getDataSourceNodesNode(databaseName), dataSourceName, VERSIONS); + } + + /** + * Get database rule active version node. + * + * @param databaseName database name + * @param ruleName rule name + * @param key key + * @return database rule active version node + */ + public static String getDatabaseRuleActiveVersionNode(final String databaseName, final String ruleName, final String key) { + return String.join("/", getDatabaseRuleNode(databaseName, ruleName), key, ACTIVE_VERSION); + } + + /** + * Get database rule versions node. + * + * @param databaseName database name + * @param ruleName rule name + * @param key key + * @return database rule versions node + */ + public static String getDatabaseRuleVersionsNode(final String databaseName, final String ruleName, final String key) { + return String.join("/", getDatabaseRuleNode(databaseName, ruleName), key, VERSIONS); + } + + /** + * Get database rule version node. + * + * @param databaseName database name + * @param ruleName rule name + * @param key key + * @param version version + * @return database rule next version + */ + public static String getDatabaseRuleVersionNode(final String databaseName, final String ruleName, final String key, final String version) { + return String.join("/", getDatabaseRuleNode(databaseName, ruleName), key, VERSIONS, version); + } + + /** + * Get database rule node. + * + * @param databaseName database name + * @param ruleName rule name + * @param key key + * @return database rule node without version + */ + public static String getDatabaseRuleNode(final String databaseName, final String ruleName, final String key) { + return String.join("/", getDatabaseRuleNode(databaseName, ruleName), key); + } + + /** + * Get database rule root node. + * + * @param databaseName database name + * @param ruleName rule name + * @return database rule root node + */ + public static String getDatabaseRuleNode(final String databaseName, final String ruleName) { + return String.join("/", getRulesNode(databaseName), ruleName); + } + + /** + * Get database rules node. + * + * @param databaseName database name + * @return database rules node + */ + public static String getRulesNode(final String databaseName) { + return String.join("/", getMetaDataNode(), databaseName, RULE_NODE); + } + + /** + * Get table active version node. * * @param databaseName database name * @param schemaName schema name - * @return tables path + * @param tableName table name + * @return tables active version node */ - public static String getMetaDataTablesPath(final String databaseName, final String schemaName) { - return String.join("/", getMetaDataSchemaPath(databaseName, schemaName), TABLES_NODE); + public static String getTableActiveVersionNode(final String databaseName, final String schemaName, final String tableName) { + return String.join("/", getTableNode(databaseName, schemaName, tableName), ACTIVE_VERSION); } /** - * Get meta data views path. + * Get table versions node. * * @param databaseName database name * @param schemaName schema name - * @return views path + * @param tableName table name + * @return tables versions node */ - public static String getMetaDataViewsPath(final String databaseName, final String schemaName) { - return String.join("/", getMetaDataSchemaPath(databaseName, schemaName), VIEWS_NODE); + public static String getTableVersionsNode(final String databaseName, final String schemaName, final String tableName) { + return String.join("/", getTableNode(databaseName, schemaName, tableName), VERSIONS); } /** - * Get schema path. + * Get table version node. * * @param databaseName database name * @param schemaName schema name - * @return tables path + * @param tableName table name + * @param version version + * @return table version node */ - public static String getMetaDataSchemaPath(final String databaseName, final String schemaName) { - return String.join("/", getMetaDataSchemasPath(databaseName), schemaName); + public static String getTableVersionNode(final String databaseName, final String schemaName, final String tableName, final String version) { + return String.join("/", getTableVersionsNode(databaseName, schemaName, tableName), version); } /** - * Get meta data schemas path. + * Get table node. * * @param databaseName database name - * @return schemas path + * @param schemaName schema name + * @param tableName table name + * @return table node */ - public static String getMetaDataSchemasPath(final String databaseName) { - return String.join("/", getDatabaseNamePath(databaseName), SCHEMAS_NODE); + public static String getTableNode(final String databaseName, final String schemaName, final String tableName) { + return String.join("/", getMetaDataNode(), databaseName, SCHEMAS_NODE, schemaName, TABLES_NODE, tableName); } /** - * Get table meta data path. + * Get view name active version node. * * @param databaseName database name * @param schemaName schema name - * @param table table name - * @return table meta data path + * @param viewName view name + * @return view active version node */ - public static String getTableMetaDataPath(final String databaseName, final String schemaName, final String table) { - return String.join("/", getMetaDataTablesPath(databaseName, schemaName), table); + public static String getViewActiveVersionNode(final String databaseName, final String schemaName, final String viewName) { + return String.join("/", getViewNode(databaseName, schemaName, viewName), ACTIVE_VERSION); } /** - * Get view meta data path. + * Get view versions node. * * @param databaseName database name * @param schemaName schema name - * @param view view name - * @return view meta data path + * @param viewName view name + * @return view versions node */ - public static String getViewMetaDataPath(final String databaseName, final String schemaName, final String view) { - return String.join("/", getMetaDataViewsPath(databaseName, schemaName), view); + public static String getViewVersionsNode(final String databaseName, final String schemaName, final String viewName) { + return String.join("/", getViewNode(databaseName, schemaName, viewName), VERSIONS); } - private static String getFullMetaDataPath(final String databaseName, final String node) { - return String.join("/", "", ROOT_NODE, databaseName, node); + /** + * Get view version node. + * + * @param databaseName database name + * @param schemaName schema name + * @param viewName view name + * @param version version + * @return view version node + */ + public static String getViewVersionNode(final String databaseName, final String schemaName, final String viewName, final String version) { + return String.join("/", getViewVersionsNode(databaseName, schemaName, viewName), version); } /** - * Get database name. + * Get view node. * - * @param configNodeFullPath config node full path - * @return database name + * @param databaseName database name + * @param schemaName schema name + * @param viewName view name + * @return tables path */ - public static Optional getDatabaseName(final String configNodeFullPath) { - Pattern pattern = Pattern.compile(getMetaDataNodePath() + "/([\\w\\-]+)$", Pattern.CASE_INSENSITIVE); - Matcher matcher = pattern.matcher(configNodeFullPath); - return matcher.find() ? Optional.of(matcher.group(1)) : Optional.empty(); + public static String getViewNode(final String databaseName, final String schemaName, final String viewName) { + return String.join("/", getMetaDataNode(), databaseName, SCHEMAS_NODE, schemaName, VIEWS_NODE, viewName); } /** - * Get schema name. + * Is data sources node. * - * @param configNodeFullPath config node full path - * @return schema name + * @param path path + * @return true or false + */ + public static boolean isDataSourcesNode(final String path) { + return Pattern.compile(getMetaDataNode() + "/([\\w\\-]+)/" + DATA_SOURCES_NODE + "/?", Pattern.CASE_INSENSITIVE).matcher(path).find(); + } + + /** + * Get data source name by data source unit node. + * + * @param path path + * @return data source name */ - public static Optional getSchemaName(final String configNodeFullPath) { - Pattern pattern = Pattern.compile(getMetaDataNodePath() + "/([\\w\\-]+)/schemas/([\\w\\-]+)$", Pattern.CASE_INSENSITIVE); - Matcher matcher = pattern.matcher(configNodeFullPath); + public static Optional getDataSourceNameByDataSourceUnitNode(final String path) { + Pattern pattern = Pattern.compile(getMetaDataNode() + "/([\\w\\-]+)/" + DATA_SOURCES_NODE + "/" + DATA_SOURCE_UNITS_NODE + "/([\\w\\-]+)?", Pattern.CASE_INSENSITIVE); + Matcher matcher = pattern.matcher(path); return matcher.find() ? Optional.of(matcher.group(2)) : Optional.empty(); } /** - * Get database name by database path. + * Get data source name by data source nodes node. * - * @param databasePath database path + * @param path path + * @return data source name + */ + public static Optional getDataSourceNameByDataSourceNode(final String path) { + Pattern pattern = Pattern.compile(getMetaDataNode() + "/([\\w\\-]+)/" + DATA_SOURCES_NODE + "/" + DATA_SOURCE_NODES_NODE + "/([\\w\\-]+)?", Pattern.CASE_INSENSITIVE); + Matcher matcher = pattern.matcher(path); + return matcher.find() ? Optional.of(matcher.group(2)) : Optional.empty(); + } + + /** + * Is data source unit active version node. + * + * @param path path + * @return true or false + */ + public static boolean isDataSourceUnitActiveVersionNode(final String path) { + return Pattern.compile(getMetaDataNode() + "/([\\w\\-]+)/" + DATA_SOURCES_NODE + "/" + DATA_SOURCE_UNITS_NODE + ACTIVE_VERSION_SUFFIX, Pattern.CASE_INSENSITIVE).matcher(path).find(); + } + + /** + * Is data source unit active version node. + * + * @param path path + * @return true or false + */ + public static boolean isDataSourceNodeActiveVersionNode(final String path) { + return Pattern.compile(getMetaDataNode() + "/([\\w\\-]+)/" + DATA_SOURCES_NODE + "/" + DATA_SOURCE_NODES_NODE + ACTIVE_VERSION_SUFFIX, Pattern.CASE_INSENSITIVE).matcher(path).find(); + } + + /** + * Get database name. + * + * @param path path * @return database name */ - public static Optional getDatabaseNameByDatabasePath(final String databasePath) { - Pattern pattern = Pattern.compile(getMetaDataNodePath() + "/([\\w\\-]+)?", Pattern.CASE_INSENSITIVE); - Matcher matcher = pattern.matcher(databasePath); + public static Optional getDatabaseName(final String path) { + Pattern pattern = Pattern.compile(getMetaDataNode() + "/([\\w\\-]+)$", Pattern.CASE_INSENSITIVE); + Matcher matcher = pattern.matcher(path); + return matcher.find() ? Optional.of(matcher.group(1)) : Optional.empty(); + } + + /** + * Get database name by schema path. + * + * @param schemaPath database path + * @return database name + */ + public static Optional getDatabaseNameBySchemaNode(final String schemaPath) { + Pattern pattern = Pattern.compile(getMetaDataNode() + "/([\\w\\-]+)?", Pattern.CASE_INSENSITIVE); + Matcher matcher = pattern.matcher(schemaPath); return matcher.find() ? Optional.of(matcher.group(1)) : Optional.empty(); } /** * Get schema name. * - * @param schemaPath schema path + * @param path path * @return schema name */ - public static Optional getSchemaNameBySchemaPath(final String schemaPath) { - Pattern pattern = Pattern.compile(getMetaDataNodePath() + "/([\\w\\-]+)/schemas/([\\w\\-]+)?", Pattern.CASE_INSENSITIVE); - Matcher matcher = pattern.matcher(schemaPath); + public static Optional getSchemaName(final String path) { + Pattern pattern = Pattern.compile(getMetaDataNode() + "/([\\w\\-]+)/schemas/([\\w\\-]+)$", Pattern.CASE_INSENSITIVE); + Matcher matcher = pattern.matcher(path); return matcher.find() ? Optional.of(matcher.group(2)) : Optional.empty(); } /** - * Get table meta data path. + * Get schema name by table path. * - * @param tableMetaDataPath table meta data path - * @return table name + * @param tablePath table path + * @return schema name */ - public static Optional getTableName(final String tableMetaDataPath) { - Pattern pattern = Pattern.compile(getMetaDataNodePath() + "/([\\w\\-]+)/([\\w\\-]+)/([\\w\\-]+)/tables" + "/([\\w\\-]+)$", Pattern.CASE_INSENSITIVE); - Matcher matcher = pattern.matcher(tableMetaDataPath); - return matcher.find() ? Optional.of(matcher.group(4)) : Optional.empty(); + public static Optional getSchemaNameByTableNode(final String tablePath) { + Pattern pattern = Pattern.compile(getMetaDataNode() + "/([\\w\\-]+)/schemas/([\\w\\-]+)?", Pattern.CASE_INSENSITIVE); + Matcher matcher = pattern.matcher(tablePath); + return matcher.find() ? Optional.of(matcher.group(2)) : Optional.empty(); } /** - * Get view meta data path. + * Get table name. * - * @param viewMetaDataPath view meta data path - * @return view name + * @param path path + * @return table name */ - public static Optional getViewName(final String viewMetaDataPath) { - Pattern pattern = Pattern.compile(getMetaDataNodePath() + "/([\\w\\-]+)/([\\w\\-]+)/([\\w\\-]+)/views" + "/([\\w\\-]+)$", Pattern.CASE_INSENSITIVE); - Matcher matcher = pattern.matcher(viewMetaDataPath); - return matcher.find() ? Optional.of(matcher.group(4)) : Optional.empty(); + public static Optional getTableName(final String path) { + Pattern pattern = Pattern.compile(getMetaDataNode() + TABLES_PATTERN + "/([\\w\\-]+)/([\\w\\-]+)$", Pattern.CASE_INSENSITIVE); + Matcher matcher = pattern.matcher(path); + return matcher.find() ? Optional.of(matcher.group(3)) : Optional.empty(); } /** - * Get active version path. - * - * @param databaseName database name - * @return active version path + * Is table active version node. + * + * @param path path + * @return true or false */ - public static String getActiveVersionPath(final String databaseName) { - return getFullMetaDataPath(databaseName, ACTIVE_VERSION); + public static boolean isTableActiveVersionNode(final String path) { + return Pattern.compile(getMetaDataNode() + TABLES_PATTERN + ACTIVE_VERSION_SUFFIX, Pattern.CASE_INSENSITIVE).matcher(path).find(); } /** - * Get database version path. - * - * @param databaseName database name - * @param version version - * @return database version path + * Get view name. + * + * @param path path + * @return view name */ - public static String getDatabaseVersionPath(final String databaseName, final String version) { - return String.join("/", getFullMetaDataPath(databaseName, VERSIONS), version); + public static Optional getViewName(final String path) { + Pattern pattern = Pattern.compile(getMetaDataNode() + VIEWS_PATTERN + "/([\\w\\-]+)?", Pattern.CASE_INSENSITIVE); + Matcher matcher = pattern.matcher(path); + return matcher.find() ? Optional.of(matcher.group(3)) : Optional.empty(); } /** - * Get version by data source units path. - * - * @param dataSourceNodeFullPath data sources unit node full path - * @return version + * Is view active version node. + * + * @param path path + * @return true or false */ - public static Optional getVersionByDataSourceUnitsPath(final String dataSourceNodeFullPath) { - Pattern pattern = Pattern.compile(getMetaDataNodePath() + "/([\\w\\-]+)/versions/([\\w\\-]+)/data_sources/units", Pattern.CASE_INSENSITIVE); - Matcher matcher = pattern.matcher(dataSourceNodeFullPath); - return matcher.find() ? Optional.of(matcher.group(2)) : Optional.empty(); + public static boolean isViewActiveVersionNode(final String path) { + return Pattern.compile(getMetaDataNode() + VIEWS_PATTERN + ACTIVE_VERSION_SUFFIX, Pattern.CASE_INSENSITIVE).matcher(path).find(); } /** - * Get version by data source nodes path. + * Get version node by active version path. * - * @param dataSourceNodeFullPath data sources node full path - * @return version + * @param rulePath rule path + * @param activeVersion active version + * @return active version node */ - public static Optional getVersionByDataSourceNodesPath(final String dataSourceNodeFullPath) { - Pattern pattern = Pattern.compile(getMetaDataNodePath() + "/([\\w\\-]+)/versions/([\\w\\-]+)/data_sources/nodes", Pattern.CASE_INSENSITIVE); - Matcher matcher = pattern.matcher(dataSourceNodeFullPath); - return matcher.find() ? Optional.of(matcher.group(2)) : Optional.empty(); + public static String getVersionNodeByActiveVersionPath(final String rulePath, final String activeVersion) { + return rulePath.replace(ACTIVE_VERSION, VERSIONS) + "/" + activeVersion; } /** - * Get version by rules path. + * Get meta data node. * - * @param rulesNodeFullPath rules node full path - * @return version + * @return meta data node */ - public static Optional getVersionByRulesPath(final String rulesNodeFullPath) { - Pattern pattern = Pattern.compile(getMetaDataNodePath() + "/([\\w\\-]+)/versions/([\\w\\-]+)/rules", Pattern.CASE_INSENSITIVE); - Matcher matcher = pattern.matcher(rulesNodeFullPath); - return matcher.find() ? Optional.of(matcher.group(2)) : Optional.empty(); + public static String getMetaDataNode() { + return String.join("/", "", ROOT_NODE); } } diff --git a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/node/GlobalNode.java b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/node/GlobalNode.java index a2fc89979805b..5da5ab0b4e7c6 100644 --- a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/node/GlobalNode.java +++ b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/node/GlobalNode.java @@ -30,21 +30,94 @@ public final class GlobalNode { private static final String PROPS_NODE = "props"; + private static final String ACTIVE_VERSION = "active_version"; + + private static final String VERSIONS = "versions"; + + /** + * Get global rule node. + * + * @param rulePath rule path + * @return global rule node + */ + public static String getGlobalRuleNode(final String rulePath) { + return String.join("/", getGlobalRuleRootNode(), rulePath); + } + /** - * Get global rule node path. + * Get global rule active version node. * - * @return global rule node path + * @param rulePath rule path + * @return global rule active version node */ - public static String getGlobalRuleNode() { + public static String getGlobalRuleActiveVersionNode(final String rulePath) { + return String.join("/", getGlobalRuleRootNode(), rulePath, ACTIVE_VERSION); + } + + /** + * Get global rule versions node. + * + * @param ruleName rule name + * @return global rule versions node + */ + public static String getGlobalRuleVersionsNode(final String ruleName) { + return String.join("/", getGlobalRuleRootNode(), ruleName, VERSIONS); + } + + /** + * Get global rule version node. + * + * @param ruleName rule name + * @param version version + * @return global rule version node + */ + public static String getGlobalRuleVersionNode(final String ruleName, final String version) { + return String.join("/", getGlobalRuleVersionsNode(ruleName), version); + } + + /** + * Get global rule root node. + * + * @return global rule root node + */ + public static String getGlobalRuleRootNode() { return String.join("/", "", RULE_NODE); } /** - * Get properties path. + * Get properties active version node. + * + * @return properties active version node + */ + public static String getPropsActiveVersionNode() { + return String.join("/", getPropsRootNode(), ACTIVE_VERSION); + } + + /** + * Get properties version node. + * + * @param version version + * @return properties version node + */ + public static String getPropsVersionNode(final String version) { + return String.join("/", getPropsVersionsNode(), version); + } + + /** + * Get properties versions node. + * + * @return properties versions node + */ + public static String getPropsVersionsNode() { + return String.join("/", getPropsRootNode(), VERSIONS); + } + + /** + * Get properties node. * - * @return properties path + * @return properties node */ - public static String getPropsPath() { + public static String getPropsRootNode() { return String.join("/", "", PROPS_NODE); } } diff --git a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/node/NewDatabaseMetaDataNode.java b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/node/NewDatabaseMetaDataNode.java deleted file mode 100644 index 1988aae0d382b..0000000000000 --- a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/node/NewDatabaseMetaDataNode.java +++ /dev/null @@ -1,498 +0,0 @@ -/* - * 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.metadata.persist.node; - -import lombok.AccessLevel; -import lombok.NoArgsConstructor; - -import java.util.Optional; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -/** - * TODO Rename DatabaseMetaDataNode when metadata structure adjustment completed. #25485 - * New database meta data node. - */ -@NoArgsConstructor(access = AccessLevel.PRIVATE) -public final class NewDatabaseMetaDataNode { - - private static final String ROOT_NODE = "metadata"; - - private static final String DATA_SOURCES_NODE = "data_sources"; - - private static final String DATA_SOURCE_NODES_NODE = "nodes"; - - private static final String DATA_SOURCE_UNITS_NODE = "units"; - - private static final String RULE_NODE = "rules"; - - private static final String SCHEMAS_NODE = "schemas"; - - private static final String TABLES_NODE = "tables"; - - private static final String VIEWS_NODE = "views"; - - private static final String ACTIVE_VERSION = "active_version"; - - private static final String VERSIONS = "versions"; - - private static final String TABLES_PATTERN = "/([\\w\\-]+)/schemas/([\\w\\-]+)/tables"; - - private static final String VIEWS_PATTERN = "/([\\w\\-]+)/schemas/([\\w\\-]+)/views"; - - private static final String ACTIVE_VERSION_SUFFIX = "/([\\w\\-]+)/active_version"; - - /** - * Get data source units node. - * - * @param databaseName database name - * @return data sources node - */ - public static String getDataSourceUnitsNode(final String databaseName) { - return String.join("/", getMetaDataNodeNode(), databaseName, DATA_SOURCES_NODE, DATA_SOURCE_UNITS_NODE); - } - - /** - * Get data source nodes node. - * - * @param databaseName database name - * @return data sources node - */ - public static String getDataSourceNodesNode(final String databaseName) { - return String.join("/", getMetaDataNodeNode(), databaseName, DATA_SOURCES_NODE, DATA_SOURCE_NODES_NODE); - } - - /** - * Get data source unit node. - * - * @param databaseName database name - * @param dataSourceName data source name - * @return data source node - */ - public static String getDataSourceUnitNode(final String databaseName, final String dataSourceName) { - return String.join("/", getDataSourceUnitsNode(databaseName), dataSourceName); - } - - /** - * Get data source unit node. - * - * @param databaseName database name - * @param dataSourceName data source name - * @return data source node - */ - public static String getDataSourceNode(final String databaseName, final String dataSourceName) { - return String.join("/", getDataSourceUnitsNode(databaseName), dataSourceName); - } - - /** - * Get data source unit node with version. - * - * @param databaseName database name - * @param dataSourceName data source name - * @param version version - * @return data source node with version - */ - public static String getDataSourceUnitNodeWithVersion(final String databaseName, final String dataSourceName, final String version) { - return String.join("/", getDataSourceUnitVersionsNode(databaseName, dataSourceName), version); - } - - /** - * Get data source node with version. - * - * @param databaseName database name - * @param dataSourceName data source name - * @param version version - * @return data source node with version - */ - public static String getDataSourceNodeWithVersion(final String databaseName, final String dataSourceName, final String version) { - return String.join("/", getDataSourceNodeVersionsNode(databaseName, dataSourceName), version); - } - - /** - * Get data source unit active version node. - * - * @param databaseName database name - * @param dataSourceName data source name - * @return data source active version node - */ - public static String getDataSourceUnitActiveVersionNode(final String databaseName, final String dataSourceName) { - return String.join("/", getDataSourceUnitsNode(databaseName), dataSourceName, ACTIVE_VERSION); - } - - /** - * Get data source node active version node. - * - * @param databaseName database name - * @param dataSourceName data source name - * @return data source active version node - */ - public static String getDataSourceNodeActiveVersionNode(final String databaseName, final String dataSourceName) { - return String.join("/", getDataSourceNodesNode(databaseName), dataSourceName, ACTIVE_VERSION); - } - - /** - * Get data source unit versions node. - * - * @param databaseName database name - * @param dataSourceName data source name - * @return data source versions node - */ - public static String getDataSourceUnitVersionsNode(final String databaseName, final String dataSourceName) { - return String.join("/", getDataSourceUnitsNode(databaseName), dataSourceName, VERSIONS); - } - - /** - * Get data source node versions node. - * - * @param databaseName database name - * @param dataSourceName data source name - * @return data source versions node - */ - public static String getDataSourceNodeVersionsNode(final String databaseName, final String dataSourceName) { - return String.join("/", getDataSourceNodesNode(databaseName), dataSourceName, VERSIONS); - } - - /** - * Get database rule active version node. - * - * @param databaseName database name - * @param ruleName rule name - * @param key key - * @return database rule active version node - */ - public static String getDatabaseRuleActiveVersionNode(final String databaseName, final String ruleName, final String key) { - return String.join("/", getDatabaseRuleNode(databaseName, ruleName), key, ACTIVE_VERSION); - } - - /** - * Get database rule versions node. - * - * @param databaseName database name - * @param ruleName rule name - * @param key key - * @return database rule versions node - */ - public static String getDatabaseRuleVersionsNode(final String databaseName, final String ruleName, final String key) { - return String.join("/", getDatabaseRuleNode(databaseName, ruleName), key, VERSIONS); - } - - /** - * Get database rule version node. - * - * @param databaseName database name - * @param ruleName rule name - * @param key key - * @param version version - * @return database rule next version - */ - public static String getDatabaseRuleVersionNode(final String databaseName, final String ruleName, final String key, final String version) { - return String.join("/", getDatabaseRuleNode(databaseName, ruleName), key, VERSIONS, version); - } - - /** - * Get database rule node. - * - * @param databaseName database name - * @param ruleName rule name - * @param key key - * @return database rule node without version - */ - public static String getDatabaseRuleNode(final String databaseName, final String ruleName, final String key) { - return String.join("/", getDatabaseRuleNode(databaseName, ruleName), key); - } - - /** - * Get database rule root node. - * - * @param databaseName database name - * @param ruleName rule name - * @return database rule root node - */ - public static String getDatabaseRuleNode(final String databaseName, final String ruleName) { - return String.join("/", getRulesNode(databaseName), ruleName); - } - - /** - * Get database rules node. - * - * @param databaseName database name - * @return database rules node - */ - public static String getRulesNode(final String databaseName) { - return String.join("/", getMetaDataNodeNode(), databaseName, RULE_NODE); - } - - /** - * Get table active version node. - * - * @param databaseName database name - * @param schemaName schema name - * @param tableName table name - * @return tables active version node - */ - public static String getTableActiveVersionNode(final String databaseName, final String schemaName, final String tableName) { - return String.join("/", getTableNode(databaseName, schemaName, tableName), ACTIVE_VERSION); - } - - /** - * Get table versions node. - * - * @param databaseName database name - * @param schemaName schema name - * @param tableName table name - * @return tables versions node - */ - public static String getTableVersionsNode(final String databaseName, final String schemaName, final String tableName) { - return String.join("/", getTableNode(databaseName, schemaName, tableName), VERSIONS); - } - - /** - * Get table version node. - * - * @param databaseName database name - * @param schemaName schema name - * @param tableName table name - * @param version version - * @return table version node - */ - public static String getTableVersionNode(final String databaseName, final String schemaName, final String tableName, final String version) { - return String.join("/", getTableVersionsNode(databaseName, schemaName, tableName), version); - } - - /** - * Get table node. - * - * @param databaseName database name - * @param schemaName schema name - * @param tableName table name - * @return table node - */ - public static String getTableNode(final String databaseName, final String schemaName, final String tableName) { - return String.join("/", getMetaDataNodeNode(), databaseName, SCHEMAS_NODE, schemaName, TABLES_NODE, tableName); - } - - /** - * Get view name active version node. - * - * @param databaseName database name - * @param schemaName schema name - * @param viewName view name - * @return view active version node - */ - public static String getViewActiveVersionNode(final String databaseName, final String schemaName, final String viewName) { - return String.join("/", getViewNode(databaseName, schemaName, viewName), ACTIVE_VERSION); - } - - /** - * Get view versions node. - * - * @param databaseName database name - * @param schemaName schema name - * @param viewName view name - * @return view versions node - */ - public static String getViewVersionsNode(final String databaseName, final String schemaName, final String viewName) { - return String.join("/", getViewNode(databaseName, schemaName, viewName), VERSIONS); - } - - /** - * Get view version node. - * - * @param databaseName database name - * @param schemaName schema name - * @param viewName view name - * @param version version - * @return view version node - */ - public static String getViewVersionNode(final String databaseName, final String schemaName, final String viewName, final String version) { - return String.join("/", getViewVersionsNode(databaseName, schemaName, viewName), version); - } - - /** - * Get view node. - * - * @param databaseName database name - * @param schemaName schema name - * @param viewName view name - * @return tables path - */ - public static String getViewNode(final String databaseName, final String schemaName, final String viewName) { - return String.join("/", getMetaDataNodeNode(), databaseName, SCHEMAS_NODE, schemaName, VIEWS_NODE, viewName); - } - - /** - * Is data sources node. - * - * @param path path - * @return true or false - */ - public static boolean isDataSourcesNode(final String path) { - return Pattern.compile(getMetaDataNodeNode() + "/([\\w\\-]+)/" + DATA_SOURCES_NODE + "/?", Pattern.CASE_INSENSITIVE).matcher(path).find(); - } - - /** - * Get data source name by data source unit node. - * - * @param path path - * @return data source name - */ - public static Optional getDataSourceNameByDataSourceUnitNode(final String path) { - Pattern pattern = Pattern.compile(getMetaDataNodeNode() + "/([\\w\\-]+)/" + DATA_SOURCES_NODE + "/" + DATA_SOURCE_UNITS_NODE + "/([\\w\\-]+)?", Pattern.CASE_INSENSITIVE); - Matcher matcher = pattern.matcher(path); - return matcher.find() ? Optional.of(matcher.group(2)) : Optional.empty(); - } - - /** - * Get data source name by data source nodes node. - * - * @param path path - * @return data source name - */ - public static Optional getDataSourceNameByDataSourceNode(final String path) { - Pattern pattern = Pattern.compile(getMetaDataNodeNode() + "/([\\w\\-]+)/" + DATA_SOURCES_NODE + "/" + DATA_SOURCE_NODES_NODE + "/([\\w\\-]+)?", Pattern.CASE_INSENSITIVE); - Matcher matcher = pattern.matcher(path); - return matcher.find() ? Optional.of(matcher.group(2)) : Optional.empty(); - } - - /** - * Is data source unit active version node. - * - * @param path path - * @return true or false - */ - public static boolean isDataSourceUnitActiveVersionNode(final String path) { - return Pattern.compile(getMetaDataNodeNode() + "/([\\w\\-]+)/" + DATA_SOURCES_NODE + "/" + DATA_SOURCE_UNITS_NODE + ACTIVE_VERSION_SUFFIX, Pattern.CASE_INSENSITIVE).matcher(path).find(); - } - - /** - * Is data source unit active version node. - * - * @param path path - * @return true or false - */ - public static boolean isDataSourceNodeActiveVersionNode(final String path) { - return Pattern.compile(getMetaDataNodeNode() + "/([\\w\\-]+)/" + DATA_SOURCES_NODE + "/" + DATA_SOURCE_NODES_NODE + ACTIVE_VERSION_SUFFIX, Pattern.CASE_INSENSITIVE).matcher(path).find(); - } - - /** - * Get database name. - * - * @param path path - * @return database name - */ - public static Optional getDatabaseName(final String path) { - Pattern pattern = Pattern.compile(getMetaDataNodeNode() + "/([\\w\\-]+)$", Pattern.CASE_INSENSITIVE); - Matcher matcher = pattern.matcher(path); - return matcher.find() ? Optional.of(matcher.group(1)) : Optional.empty(); - } - - /** - * Get database name by schema path. - * - * @param schemaPath database path - * @return database name - */ - public static Optional getDatabaseNameBySchemaNode(final String schemaPath) { - Pattern pattern = Pattern.compile(getMetaDataNodeNode() + "/([\\w\\-]+)?", Pattern.CASE_INSENSITIVE); - Matcher matcher = pattern.matcher(schemaPath); - return matcher.find() ? Optional.of(matcher.group(1)) : Optional.empty(); - } - - /** - * Get schema name. - * - * @param path path - * @return schema name - */ - public static Optional getSchemaName(final String path) { - Pattern pattern = Pattern.compile(getMetaDataNodeNode() + "/([\\w\\-]+)/schemas/([\\w\\-]+)$", Pattern.CASE_INSENSITIVE); - Matcher matcher = pattern.matcher(path); - return matcher.find() ? Optional.of(matcher.group(2)) : Optional.empty(); - } - - /** - * Get schema name by table path. - * - * @param tablePath table path - * @return schema name - */ - public static Optional getSchemaNameByTableNode(final String tablePath) { - Pattern pattern = Pattern.compile(getMetaDataNodeNode() + "/([\\w\\-]+)/schemas/([\\w\\-]+)?", Pattern.CASE_INSENSITIVE); - Matcher matcher = pattern.matcher(tablePath); - return matcher.find() ? Optional.of(matcher.group(2)) : Optional.empty(); - } - - /** - * Get table name. - * - * @param path path - * @return table name - */ - public static Optional getTableName(final String path) { - Pattern pattern = Pattern.compile(getMetaDataNodeNode() + TABLES_PATTERN + "/([\\w\\-]+)/([\\w\\-]+)$", Pattern.CASE_INSENSITIVE); - Matcher matcher = pattern.matcher(path); - return matcher.find() ? Optional.of(matcher.group(3)) : Optional.empty(); - } - - /** - * Is table active version node. - * - * @param path path - * @return true or false - */ - public static boolean isTableActiveVersionNode(final String path) { - return Pattern.compile(getMetaDataNodeNode() + TABLES_PATTERN + ACTIVE_VERSION_SUFFIX, Pattern.CASE_INSENSITIVE).matcher(path).find(); - } - - /** - * Get view name. - * - * @param path path - * @return view name - */ - public static Optional getViewName(final String path) { - Pattern pattern = Pattern.compile(getMetaDataNodeNode() + VIEWS_PATTERN + "/([\\w\\-]+)?", Pattern.CASE_INSENSITIVE); - Matcher matcher = pattern.matcher(path); - return matcher.find() ? Optional.of(matcher.group(3)) : Optional.empty(); - } - - /** - * Is view active version node. - * - * @param path path - * @return true or false - */ - public static boolean isViewActiveVersionNode(final String path) { - return Pattern.compile(getMetaDataNodeNode() + VIEWS_PATTERN + ACTIVE_VERSION_SUFFIX, Pattern.CASE_INSENSITIVE).matcher(path).find(); - } - - /** - * Get version node by active version path. - * - * @param rulePath rule path - * @param activeVersion active version - * @return active version node - */ - public static String getVersionNodeByActiveVersionPath(final String rulePath, final String activeVersion) { - return rulePath.replace(ACTIVE_VERSION, VERSIONS) + "/" + activeVersion; - } - - private static String getMetaDataNodeNode() { - return String.join("/", "", ROOT_NODE); - } -} diff --git a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/node/NewGlobalNode.java b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/node/NewGlobalNode.java deleted file mode 100644 index 8fd67bacf44cb..0000000000000 --- a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/node/NewGlobalNode.java +++ /dev/null @@ -1,124 +0,0 @@ -/* - * 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.metadata.persist.node; - -import lombok.AccessLevel; -import lombok.NoArgsConstructor; - -/** - * TODO Rename GlobalNode when metadata structure adjustment completed. #25485 - * Global node. - */ -@NoArgsConstructor(access = AccessLevel.PRIVATE) -public final class NewGlobalNode { - - private static final String RULE_NODE = "rules"; - - private static final String PROPS_NODE = "props"; - - private static final String ACTIVE_VERSION = "active_version"; - - private static final String VERSIONS = "versions"; - - /** - * Get global rule node. - * - * @param rulePath rule path - * @return global rule node - */ - public static String getGlobalRuleNode(final String rulePath) { - return String.join("/", getGlobalRuleRootNode(), rulePath); - } - - /** - * Get global rule active version node. - * - * @param rulePath rule path - * @return global rule active version node - */ - public static String getGlobalRuleActiveVersionNode(final String rulePath) { - return String.join("/", getGlobalRuleRootNode(), rulePath, ACTIVE_VERSION); - } - - /** - * Get global rule versions node. - * - * @param ruleName rule name - * @return global rule versions node - */ - public static String getGlobalRuleVersionsNode(final String ruleName) { - return String.join("/", getGlobalRuleRootNode(), ruleName, VERSIONS); - } - - /** - * Get global rule version node. - * - * @param ruleName rule name - * @param version version - * @return global rule version node - */ - public static String getGlobalRuleVersionNode(final String ruleName, final String version) { - return String.join("/", getGlobalRuleVersionsNode(ruleName), version); - } - - /** - * Get global rule root node. - * - * @return global rule root node - */ - public static String getGlobalRuleRootNode() { - return String.join("/", "", RULE_NODE); - } - - /** - * Get properties active version node. - * - * @return properties active version node - */ - public static String getPropsActiveVersionNode() { - return String.join("/", getPropsRootNode(), ACTIVE_VERSION); - } - - /** - * Get properties version node. - * - * @param version version - * @return properties version node - */ - public static String getPropsVersionNode(final String version) { - return String.join("/", getPropsVersionsNode(), version); - } - - /** - * Get properties versions node. - * - * @return properties versions node - */ - public static String getPropsVersionsNode() { - return String.join("/", getPropsRootNode(), VERSIONS); - } - - /** - * Get properties node. - * - * @return properties node - */ - public static String getPropsRootNode() { - return String.join("/", "", PROPS_NODE); - } -} diff --git a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/config/database/datasource/DataSourceNodePersistService.java b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/config/database/datasource/DataSourceNodePersistService.java index f706213e71ad0..37b612b394bfb 100644 --- a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/config/database/datasource/DataSourceNodePersistService.java +++ b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/config/database/datasource/DataSourceNodePersistService.java @@ -20,16 +20,19 @@ import com.google.common.base.Strings; import lombok.RequiredArgsConstructor; import org.apache.shardingsphere.infra.datasource.pool.props.domain.DataSourcePoolProperties; +import org.apache.shardingsphere.infra.metadata.version.MetaDataVersion; import org.apache.shardingsphere.infra.util.yaml.YamlEngine; import org.apache.shardingsphere.infra.yaml.config.swapper.resource.YamlDataSourceConfigurationSwapper; import org.apache.shardingsphere.metadata.persist.node.DatabaseMetaDataNode; import org.apache.shardingsphere.metadata.persist.service.config.database.DatabaseBasedPersistService; import org.apache.shardingsphere.mode.spi.PersistRepository; +import java.util.Collection; import java.util.LinkedHashMap; +import java.util.LinkedList; +import java.util.List; import java.util.Map; import java.util.Map.Entry; -import java.util.stream.Collectors; /** * Data source node persist service. @@ -43,54 +46,75 @@ public final class DataSourceNodePersistService implements DatabaseBasedPersistS @Override public void persist(final String databaseName, final Map dataSourceConfigs) { - if (Strings.isNullOrEmpty(getDatabaseActiveVersion(databaseName))) { - repository.persist(DatabaseMetaDataNode.getActiveVersionPath(databaseName), DEFAULT_VERSION); + for (Entry entry : dataSourceConfigs.entrySet()) { + String activeVersion = getDataSourceActiveVersion(databaseName, entry.getKey()); + List versions = repository.getChildrenKeys(DatabaseMetaDataNode.getDataSourceNodeVersionsNode(databaseName, entry.getKey())); + repository.persist(DatabaseMetaDataNode.getDataSourceNodeWithVersion(databaseName, entry.getKey(), versions.isEmpty() + ? DEFAULT_VERSION + : String.valueOf(Integer.parseInt(versions.get(0)) + 1)), YamlEngine.marshal(new YamlDataSourceConfigurationSwapper().swapToMap(entry.getValue()))); + if (Strings.isNullOrEmpty(activeVersion)) { + repository.persist(DatabaseMetaDataNode.getDataSourceNodeActiveVersionNode(databaseName, entry.getKey()), DEFAULT_VERSION); + } } - repository.persist(DatabaseMetaDataNode.getMetaDataDataSourceNodesPath(databaseName, getDatabaseActiveVersion(databaseName)), - YamlEngine.marshal(swapYamlDataSourceConfiguration(dataSourceConfigs))); } - private Map> swapYamlDataSourceConfiguration(final Map propsMap) { - return propsMap.entrySet().stream() - .collect(Collectors.toMap(Entry::getKey, entry -> new YamlDataSourceConfigurationSwapper().swapToMap(entry.getValue()), (oldValue, currentValue) -> oldValue, LinkedHashMap::new)); + @Override + public Collection deleteConfig(final String databaseName, final Map dataSourceConfigs) { + Collection result = new LinkedList<>(); + for (Entry entry : dataSourceConfigs.entrySet()) { + String delKey = DatabaseMetaDataNode.getDataSourceNode(databaseName, entry.getKey()); + repository.delete(delKey); + result.add(new MetaDataVersion(delKey)); + } + return result; } @Override - public Map load(final String databaseName) { - return isExisted(databaseName) ? getDataSourcePoolProperties(repository.getDirectly( - DatabaseMetaDataNode.getMetaDataDataSourceNodesPath(databaseName, getDatabaseActiveVersion(databaseName)))) : new LinkedHashMap<>(); + public Collection persistConfig(final String databaseName, final Map dataSourceConfigs) { + Collection result = new LinkedList<>(); + for (Entry entry : dataSourceConfigs.entrySet()) { + List versions = repository.getChildrenKeys(DatabaseMetaDataNode.getDataSourceNodeVersionsNode(databaseName, entry.getKey())); + String nextActiveVersion = versions.isEmpty() ? DEFAULT_VERSION : String.valueOf(Integer.parseInt(versions.get(0)) + 1); + repository.persist(DatabaseMetaDataNode.getDataSourceNodeWithVersion(databaseName, entry.getKey(), nextActiveVersion), + YamlEngine.marshal(new YamlDataSourceConfigurationSwapper().swapToMap(entry.getValue()))); + if (Strings.isNullOrEmpty(getDataSourceActiveVersion(databaseName, entry.getKey()))) { + repository.persist(DatabaseMetaDataNode.getDataSourceNodeActiveVersionNode(databaseName, entry.getKey()), DEFAULT_VERSION); + } + result.add(new MetaDataVersion(DatabaseMetaDataNode.getDataSourceNode(databaseName, entry.getKey()), getDataSourceActiveVersion(databaseName, entry.getKey()), nextActiveVersion)); + } + return result; } - private boolean isExisted(final String databaseName) { - return !Strings.isNullOrEmpty(getDatabaseActiveVersion(databaseName)) && !Strings.isNullOrEmpty(repository.getDirectly(DatabaseMetaDataNode.getMetaDataDataSourceUnitsPath(databaseName, - getDatabaseActiveVersion(databaseName)))); + @SuppressWarnings("unchecked") + @Override + public Map load(final String databaseName) { + Map result = new LinkedHashMap<>(); + for (String each : repository.getChildrenKeys(DatabaseMetaDataNode.getDataSourceNodesNode(databaseName))) { + String dataSourceValue = repository.getDirectly(DatabaseMetaDataNode.getDataSourceNodeWithVersion(databaseName, each, getDataSourceActiveVersion(databaseName, each))); + if (!Strings.isNullOrEmpty(dataSourceValue)) { + result.put(each, new YamlDataSourceConfigurationSwapper().swapToDataSourcePoolProperties(YamlEngine.unmarshal(dataSourceValue, Map.class))); + } + } + return result; } @SuppressWarnings("unchecked") - private Map getDataSourcePoolProperties(final String yamlContent) { - Map> yamlDataSources = YamlEngine.unmarshal(yamlContent, Map.class); - if (yamlDataSources.isEmpty()) { - return new LinkedHashMap<>(); + @Override + public Map load(final String databaseName, final String name) { + Map result = new LinkedHashMap<>(); + String dataSourceValue = repository.getDirectly(DatabaseMetaDataNode.getDataSourceNodeWithVersion(databaseName, name, getDataSourceActiveVersion(databaseName, name))); + if (!Strings.isNullOrEmpty(dataSourceValue)) { + result.put(name, new YamlDataSourceConfigurationSwapper().swapToDataSourcePoolProperties(YamlEngine.unmarshal(dataSourceValue, Map.class))); } - Map result = new LinkedHashMap<>(yamlDataSources.size()); - yamlDataSources.forEach((key, value) -> result.put(key, new YamlDataSourceConfigurationSwapper().swapToDataSourcePoolProperties(value))); return result; } - /** - * Append data source properties map. - * - * @param databaseName database name - * @param toBeAppendedPropsMap data source properties map to be appended - */ @Override public void append(final String databaseName, final Map toBeAppendedPropsMap) { - Map dataSourceConfigs = load(databaseName); - dataSourceConfigs.putAll(toBeAppendedPropsMap); - persist(databaseName, dataSourceConfigs); + persist(databaseName, toBeAppendedPropsMap); } - private String getDatabaseActiveVersion(final String databaseName) { - return repository.getDirectly(DatabaseMetaDataNode.getActiveVersionPath(databaseName)); + private String getDataSourceActiveVersion(final String databaseName, final String dataSourceName) { + return repository.getDirectly(DatabaseMetaDataNode.getDataSourceNodeActiveVersionNode(databaseName, dataSourceName)); } } diff --git a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/config/database/datasource/DataSourceUnitPersistService.java b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/config/database/datasource/DataSourceUnitPersistService.java index 1aadc20d0bdec..bf7f285e23e5f 100644 --- a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/config/database/datasource/DataSourceUnitPersistService.java +++ b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/config/database/datasource/DataSourceUnitPersistService.java @@ -20,16 +20,19 @@ import com.google.common.base.Strings; import lombok.RequiredArgsConstructor; import org.apache.shardingsphere.infra.datasource.pool.props.domain.DataSourcePoolProperties; +import org.apache.shardingsphere.infra.metadata.version.MetaDataVersion; import org.apache.shardingsphere.infra.util.yaml.YamlEngine; import org.apache.shardingsphere.infra.yaml.config.swapper.resource.YamlDataSourceConfigurationSwapper; import org.apache.shardingsphere.metadata.persist.node.DatabaseMetaDataNode; import org.apache.shardingsphere.metadata.persist.service.config.database.DatabaseBasedPersistService; import org.apache.shardingsphere.mode.spi.PersistRepository; +import java.util.Collection; import java.util.LinkedHashMap; +import java.util.LinkedList; +import java.util.List; import java.util.Map; import java.util.Map.Entry; -import java.util.stream.Collectors; /** * Data source unit persist service. @@ -43,54 +46,75 @@ public final class DataSourceUnitPersistService implements DatabaseBasedPersistS @Override public void persist(final String databaseName, final Map dataSourceConfigs) { - if (Strings.isNullOrEmpty(getDatabaseActiveVersion(databaseName))) { - repository.persist(DatabaseMetaDataNode.getActiveVersionPath(databaseName), DEFAULT_VERSION); + for (Entry entry : dataSourceConfigs.entrySet()) { + String activeVersion = getDataSourceActiveVersion(databaseName, entry.getKey()); + List versions = repository.getChildrenKeys(DatabaseMetaDataNode.getDataSourceUnitVersionsNode(databaseName, entry.getKey())); + repository.persist(DatabaseMetaDataNode.getDataSourceUnitNodeWithVersion(databaseName, entry.getKey(), versions.isEmpty() + ? DEFAULT_VERSION + : String.valueOf(Integer.parseInt(versions.get(0)) + 1)), YamlEngine.marshal(new YamlDataSourceConfigurationSwapper().swapToMap(entry.getValue()))); + if (Strings.isNullOrEmpty(activeVersion)) { + repository.persist(DatabaseMetaDataNode.getDataSourceUnitActiveVersionNode(databaseName, entry.getKey()), DEFAULT_VERSION); + } } - repository.persist(DatabaseMetaDataNode.getMetaDataDataSourceUnitsPath(databaseName, getDatabaseActiveVersion(databaseName)), - YamlEngine.marshal(swapYamlDataSourceConfiguration(dataSourceConfigs))); } - private Map> swapYamlDataSourceConfiguration(final Map propsMap) { - return propsMap.entrySet().stream() - .collect(Collectors.toMap(Entry::getKey, entry -> new YamlDataSourceConfigurationSwapper().swapToMap(entry.getValue()), (oldValue, currentValue) -> oldValue, LinkedHashMap::new)); + @Override + public Collection deleteConfig(final String databaseName, final Map dataSourceConfigs) { + Collection result = new LinkedList<>(); + for (Entry entry : dataSourceConfigs.entrySet()) { + String delKey = DatabaseMetaDataNode.getDataSourceUnitNode(databaseName, entry.getKey()); + repository.delete(delKey); + result.add(new MetaDataVersion(delKey)); + } + return result; } @Override - public Map load(final String databaseName) { - return isExisted(databaseName) ? getDataSourcePoolProperties(repository.getDirectly( - DatabaseMetaDataNode.getMetaDataDataSourceUnitsPath(databaseName, getDatabaseActiveVersion(databaseName)))) : new LinkedHashMap<>(); + public Collection persistConfig(final String databaseName, final Map dataSourceConfigs) { + Collection result = new LinkedList<>(); + for (Entry entry : dataSourceConfigs.entrySet()) { + List versions = repository.getChildrenKeys(DatabaseMetaDataNode.getDataSourceUnitVersionsNode(databaseName, entry.getKey())); + String nextActiveVersion = versions.isEmpty() ? DEFAULT_VERSION : String.valueOf(Integer.parseInt(versions.get(0)) + 1); + repository.persist(DatabaseMetaDataNode.getDataSourceUnitNodeWithVersion(databaseName, entry.getKey(), nextActiveVersion), + YamlEngine.marshal(new YamlDataSourceConfigurationSwapper().swapToMap(entry.getValue()))); + if (Strings.isNullOrEmpty(getDataSourceActiveVersion(databaseName, entry.getKey()))) { + repository.persist(DatabaseMetaDataNode.getDataSourceUnitActiveVersionNode(databaseName, entry.getKey()), DEFAULT_VERSION); + } + result.add(new MetaDataVersion(DatabaseMetaDataNode.getDataSourceUnitNode(databaseName, entry.getKey()), getDataSourceActiveVersion(databaseName, entry.getKey()), nextActiveVersion)); + } + return result; } - private boolean isExisted(final String databaseName) { - return !Strings.isNullOrEmpty(getDatabaseActiveVersion(databaseName)) && !Strings.isNullOrEmpty(repository.getDirectly(DatabaseMetaDataNode.getMetaDataDataSourceUnitsPath(databaseName, - getDatabaseActiveVersion(databaseName)))); + @SuppressWarnings("unchecked") + @Override + public Map load(final String databaseName) { + Map result = new LinkedHashMap<>(); + for (String each : repository.getChildrenKeys(DatabaseMetaDataNode.getDataSourceUnitsNode(databaseName))) { + String dataSourceValue = repository.getDirectly(DatabaseMetaDataNode.getDataSourceUnitNodeWithVersion(databaseName, each, getDataSourceActiveVersion(databaseName, each))); + if (!Strings.isNullOrEmpty(dataSourceValue)) { + result.put(each, new YamlDataSourceConfigurationSwapper().swapToDataSourcePoolProperties(YamlEngine.unmarshal(dataSourceValue, Map.class))); + } + } + return result; } @SuppressWarnings("unchecked") - private Map getDataSourcePoolProperties(final String yamlContent) { - Map> yamlDataSources = YamlEngine.unmarshal(yamlContent, Map.class); - if (yamlDataSources.isEmpty()) { - return new LinkedHashMap<>(); + @Override + public Map load(final String databaseName, final String name) { + Map result = new LinkedHashMap<>(); + String dataSourceValue = repository.getDirectly(DatabaseMetaDataNode.getDataSourceUnitNodeWithVersion(databaseName, name, getDataSourceActiveVersion(databaseName, name))); + if (!Strings.isNullOrEmpty(dataSourceValue)) { + result.put(name, new YamlDataSourceConfigurationSwapper().swapToDataSourcePoolProperties(YamlEngine.unmarshal(dataSourceValue, Map.class))); } - Map result = new LinkedHashMap<>(yamlDataSources.size()); - yamlDataSources.forEach((key, value) -> result.put(key, new YamlDataSourceConfigurationSwapper().swapToDataSourcePoolProperties(value))); return result; } - /** - * Append data source properties map. - * - * @param databaseName database name - * @param toBeAppendedPropsMap data source properties map to be appended - */ @Override public void append(final String databaseName, final Map toBeAppendedPropsMap) { - Map dataSourceConfigs = load(databaseName); - dataSourceConfigs.putAll(toBeAppendedPropsMap); - persist(databaseName, dataSourceConfigs); + persist(databaseName, toBeAppendedPropsMap); } - private String getDatabaseActiveVersion(final String databaseName) { - return repository.getDirectly(DatabaseMetaDataNode.getActiveVersionPath(databaseName)); + private String getDataSourceActiveVersion(final String databaseName, final String dataSourceName) { + return repository.getDirectly(DatabaseMetaDataNode.getDataSourceUnitActiveVersionNode(databaseName, dataSourceName)); } } diff --git a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/config/database/datasource/NewDataSourceNodePersistService.java b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/config/database/datasource/NewDataSourceNodePersistService.java deleted file mode 100644 index 3cb906cfd4223..0000000000000 --- a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/config/database/datasource/NewDataSourceNodePersistService.java +++ /dev/null @@ -1,121 +0,0 @@ -/* - * 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.metadata.persist.service.config.database.datasource; - -import com.google.common.base.Strings; -import lombok.RequiredArgsConstructor; -import org.apache.shardingsphere.infra.datasource.pool.props.domain.DataSourcePoolProperties; -import org.apache.shardingsphere.infra.metadata.version.MetaDataVersion; -import org.apache.shardingsphere.infra.util.yaml.YamlEngine; -import org.apache.shardingsphere.infra.yaml.config.swapper.resource.YamlDataSourceConfigurationSwapper; -import org.apache.shardingsphere.metadata.persist.node.NewDatabaseMetaDataNode; -import org.apache.shardingsphere.metadata.persist.service.config.database.DatabaseBasedPersistService; -import org.apache.shardingsphere.mode.spi.PersistRepository; - -import java.util.Collection; -import java.util.LinkedHashMap; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; -import java.util.Map.Entry; - -/** - * TODO Rename DataSourcePersistService when metadata structure adjustment completed. #25485 - * New Data source node persist service. - */ -@RequiredArgsConstructor -public final class NewDataSourceNodePersistService implements DatabaseBasedPersistService> { - - private static final String DEFAULT_VERSION = "0"; - - private final PersistRepository repository; - - @Override - public void persist(final String databaseName, final Map dataSourceConfigs) { - for (Entry entry : dataSourceConfigs.entrySet()) { - String activeVersion = getDataSourceActiveVersion(databaseName, entry.getKey()); - List versions = repository.getChildrenKeys(NewDatabaseMetaDataNode.getDataSourceNodeVersionsNode(databaseName, entry.getKey())); - repository.persist(NewDatabaseMetaDataNode.getDataSourceNodeWithVersion(databaseName, entry.getKey(), versions.isEmpty() - ? DEFAULT_VERSION - : String.valueOf(Integer.parseInt(versions.get(0)) + 1)), YamlEngine.marshal(new YamlDataSourceConfigurationSwapper().swapToMap(entry.getValue()))); - if (Strings.isNullOrEmpty(activeVersion)) { - repository.persist(NewDatabaseMetaDataNode.getDataSourceNodeActiveVersionNode(databaseName, entry.getKey()), DEFAULT_VERSION); - } - } - } - - @Override - public Collection deleteConfig(final String databaseName, final Map dataSourceConfigs) { - Collection result = new LinkedList<>(); - for (Entry entry : dataSourceConfigs.entrySet()) { - String delKey = NewDatabaseMetaDataNode.getDataSourceNode(databaseName, entry.getKey()); - repository.delete(delKey); - result.add(new MetaDataVersion(delKey)); - } - return result; - } - - @Override - public Collection persistConfig(final String databaseName, final Map dataSourceConfigs) { - Collection result = new LinkedList<>(); - for (Entry entry : dataSourceConfigs.entrySet()) { - List versions = repository.getChildrenKeys(NewDatabaseMetaDataNode.getDataSourceNodeVersionsNode(databaseName, entry.getKey())); - String nextActiveVersion = versions.isEmpty() ? DEFAULT_VERSION : String.valueOf(Integer.parseInt(versions.get(0)) + 1); - repository.persist(NewDatabaseMetaDataNode.getDataSourceNodeWithVersion(databaseName, entry.getKey(), nextActiveVersion), - YamlEngine.marshal(new YamlDataSourceConfigurationSwapper().swapToMap(entry.getValue()))); - if (Strings.isNullOrEmpty(getDataSourceActiveVersion(databaseName, entry.getKey()))) { - repository.persist(NewDatabaseMetaDataNode.getDataSourceNodeActiveVersionNode(databaseName, entry.getKey()), DEFAULT_VERSION); - } - result.add(new MetaDataVersion(NewDatabaseMetaDataNode.getDataSourceNode(databaseName, entry.getKey()), getDataSourceActiveVersion(databaseName, entry.getKey()), nextActiveVersion)); - } - return result; - } - - @SuppressWarnings("unchecked") - @Override - public Map load(final String databaseName) { - Map result = new LinkedHashMap<>(); - for (String each : repository.getChildrenKeys(NewDatabaseMetaDataNode.getDataSourceNodesNode(databaseName))) { - String dataSourceValue = repository.getDirectly(NewDatabaseMetaDataNode.getDataSourceNodeWithVersion(databaseName, each, getDataSourceActiveVersion(databaseName, each))); - if (!Strings.isNullOrEmpty(dataSourceValue)) { - result.put(each, new YamlDataSourceConfigurationSwapper().swapToDataSourcePoolProperties(YamlEngine.unmarshal(dataSourceValue, Map.class))); - } - } - return result; - } - - @SuppressWarnings("unchecked") - @Override - public Map load(final String databaseName, final String name) { - Map result = new LinkedHashMap<>(); - String dataSourceValue = repository.getDirectly(NewDatabaseMetaDataNode.getDataSourceNodeWithVersion(databaseName, name, getDataSourceActiveVersion(databaseName, name))); - if (!Strings.isNullOrEmpty(dataSourceValue)) { - result.put(name, new YamlDataSourceConfigurationSwapper().swapToDataSourcePoolProperties(YamlEngine.unmarshal(dataSourceValue, Map.class))); - } - return result; - } - - @Override - public void append(final String databaseName, final Map toBeAppendedPropsMap) { - persist(databaseName, toBeAppendedPropsMap); - } - - private String getDataSourceActiveVersion(final String databaseName, final String dataSourceName) { - return repository.getDirectly(NewDatabaseMetaDataNode.getDataSourceNodeActiveVersionNode(databaseName, dataSourceName)); - } -} diff --git a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/config/database/datasource/NewDataSourceUnitPersistService.java b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/config/database/datasource/NewDataSourceUnitPersistService.java deleted file mode 100644 index 98c5c6946a3d8..0000000000000 --- a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/config/database/datasource/NewDataSourceUnitPersistService.java +++ /dev/null @@ -1,121 +0,0 @@ -/* - * 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.metadata.persist.service.config.database.datasource; - -import com.google.common.base.Strings; -import lombok.RequiredArgsConstructor; -import org.apache.shardingsphere.infra.datasource.pool.props.domain.DataSourcePoolProperties; -import org.apache.shardingsphere.infra.metadata.version.MetaDataVersion; -import org.apache.shardingsphere.infra.util.yaml.YamlEngine; -import org.apache.shardingsphere.infra.yaml.config.swapper.resource.YamlDataSourceConfigurationSwapper; -import org.apache.shardingsphere.metadata.persist.node.NewDatabaseMetaDataNode; -import org.apache.shardingsphere.metadata.persist.service.config.database.DatabaseBasedPersistService; -import org.apache.shardingsphere.mode.spi.PersistRepository; - -import java.util.Collection; -import java.util.LinkedHashMap; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; -import java.util.Map.Entry; - -/** - * TODO Rename DataSourcePersistService when metadata structure adjustment completed. #25485 - * New Data source unit persist service. - */ -@RequiredArgsConstructor -public final class NewDataSourceUnitPersistService implements DatabaseBasedPersistService> { - - private static final String DEFAULT_VERSION = "0"; - - private final PersistRepository repository; - - @Override - public void persist(final String databaseName, final Map dataSourceConfigs) { - for (Entry entry : dataSourceConfigs.entrySet()) { - String activeVersion = getDataSourceActiveVersion(databaseName, entry.getKey()); - List versions = repository.getChildrenKeys(NewDatabaseMetaDataNode.getDataSourceUnitVersionsNode(databaseName, entry.getKey())); - repository.persist(NewDatabaseMetaDataNode.getDataSourceUnitNodeWithVersion(databaseName, entry.getKey(), versions.isEmpty() - ? DEFAULT_VERSION - : String.valueOf(Integer.parseInt(versions.get(0)) + 1)), YamlEngine.marshal(new YamlDataSourceConfigurationSwapper().swapToMap(entry.getValue()))); - if (Strings.isNullOrEmpty(activeVersion)) { - repository.persist(NewDatabaseMetaDataNode.getDataSourceUnitActiveVersionNode(databaseName, entry.getKey()), DEFAULT_VERSION); - } - } - } - - @Override - public Collection deleteConfig(final String databaseName, final Map dataSourceConfigs) { - Collection result = new LinkedList<>(); - for (Entry entry : dataSourceConfigs.entrySet()) { - String delKey = NewDatabaseMetaDataNode.getDataSourceUnitNode(databaseName, entry.getKey()); - repository.delete(delKey); - result.add(new MetaDataVersion(delKey)); - } - return result; - } - - @Override - public Collection persistConfig(final String databaseName, final Map dataSourceConfigs) { - Collection result = new LinkedList<>(); - for (Entry entry : dataSourceConfigs.entrySet()) { - List versions = repository.getChildrenKeys(NewDatabaseMetaDataNode.getDataSourceUnitVersionsNode(databaseName, entry.getKey())); - String nextActiveVersion = versions.isEmpty() ? DEFAULT_VERSION : String.valueOf(Integer.parseInt(versions.get(0)) + 1); - repository.persist(NewDatabaseMetaDataNode.getDataSourceUnitNodeWithVersion(databaseName, entry.getKey(), nextActiveVersion), - YamlEngine.marshal(new YamlDataSourceConfigurationSwapper().swapToMap(entry.getValue()))); - if (Strings.isNullOrEmpty(getDataSourceActiveVersion(databaseName, entry.getKey()))) { - repository.persist(NewDatabaseMetaDataNode.getDataSourceUnitActiveVersionNode(databaseName, entry.getKey()), DEFAULT_VERSION); - } - result.add(new MetaDataVersion(NewDatabaseMetaDataNode.getDataSourceUnitNode(databaseName, entry.getKey()), getDataSourceActiveVersion(databaseName, entry.getKey()), nextActiveVersion)); - } - return result; - } - - @SuppressWarnings("unchecked") - @Override - public Map load(final String databaseName) { - Map result = new LinkedHashMap<>(); - for (String each : repository.getChildrenKeys(NewDatabaseMetaDataNode.getDataSourceUnitsNode(databaseName))) { - String dataSourceValue = repository.getDirectly(NewDatabaseMetaDataNode.getDataSourceUnitNodeWithVersion(databaseName, each, getDataSourceActiveVersion(databaseName, each))); - if (!Strings.isNullOrEmpty(dataSourceValue)) { - result.put(each, new YamlDataSourceConfigurationSwapper().swapToDataSourcePoolProperties(YamlEngine.unmarshal(dataSourceValue, Map.class))); - } - } - return result; - } - - @SuppressWarnings("unchecked") - @Override - public Map load(final String databaseName, final String name) { - Map result = new LinkedHashMap<>(); - String dataSourceValue = repository.getDirectly(NewDatabaseMetaDataNode.getDataSourceUnitNodeWithVersion(databaseName, name, getDataSourceActiveVersion(databaseName, name))); - if (!Strings.isNullOrEmpty(dataSourceValue)) { - result.put(name, new YamlDataSourceConfigurationSwapper().swapToDataSourcePoolProperties(YamlEngine.unmarshal(dataSourceValue, Map.class))); - } - return result; - } - - @Override - public void append(final String databaseName, final Map toBeAppendedPropsMap) { - persist(databaseName, toBeAppendedPropsMap); - } - - private String getDataSourceActiveVersion(final String databaseName, final String dataSourceName) { - return repository.getDirectly(NewDatabaseMetaDataNode.getDataSourceUnitActiveVersionNode(databaseName, dataSourceName)); - } -} diff --git a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/config/database/rule/DatabaseRulePersistService.java b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/config/database/rule/DatabaseRulePersistService.java index f94581f6c969e..1fd019483db32 100644 --- a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/config/database/rule/DatabaseRulePersistService.java +++ b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/config/database/rule/DatabaseRulePersistService.java @@ -18,56 +18,118 @@ package org.apache.shardingsphere.metadata.persist.service.config.database.rule; import com.google.common.base.Strings; -import lombok.RequiredArgsConstructor; import org.apache.shardingsphere.infra.config.rule.RuleConfiguration; -import org.apache.shardingsphere.infra.util.yaml.YamlEngine; -import org.apache.shardingsphere.infra.yaml.config.pojo.rule.YamlRuleConfiguration; -import org.apache.shardingsphere.infra.yaml.config.swapper.rule.YamlRuleConfigurationSwapperEngine; +import org.apache.shardingsphere.infra.metadata.version.MetaDataVersion; +import org.apache.shardingsphere.infra.util.yaml.datanode.YamlDataNode; +import org.apache.shardingsphere.infra.yaml.config.swapper.rule.YamlDataNodeRuleConfigurationSwapper; +import org.apache.shardingsphere.infra.yaml.config.swapper.rule.YamlDataNodeRuleConfigurationSwapperEngine; import org.apache.shardingsphere.metadata.persist.node.DatabaseMetaDataNode; +import org.apache.shardingsphere.metadata.persist.service.config.AbstractPersistService; import org.apache.shardingsphere.metadata.persist.service.config.database.DatabaseBasedPersistService; import org.apache.shardingsphere.mode.spi.PersistRepository; import java.util.Collection; +import java.util.Collections; import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; /** * Database rule persist service. */ -@RequiredArgsConstructor -public final class DatabaseRulePersistService implements DatabaseBasedPersistService> { +public final class DatabaseRulePersistService extends AbstractPersistService implements DatabaseBasedPersistService> { private static final String DEFAULT_VERSION = "0"; private final PersistRepository repository; + public DatabaseRulePersistService(final PersistRepository repository) { + super(repository); + this.repository = repository; + } + + @SuppressWarnings({"unchecked", "rawtypes"}) @Override public void persist(final String databaseName, final Collection configs) { - if (Strings.isNullOrEmpty(getDatabaseActiveVersion(databaseName))) { - repository.persist(DatabaseMetaDataNode.getActiveVersionPath(databaseName), DEFAULT_VERSION); + Map yamlConfigs = new YamlDataNodeRuleConfigurationSwapperEngine().swapToYamlRuleConfigurations(configs); + for (Entry entry : yamlConfigs.entrySet()) { + Collection dataNodes = entry.getValue().swapToDataNodes(entry.getKey()); + if (dataNodes.isEmpty()) { + continue; + } + persistDataNodes(databaseName, entry.getValue().getRuleTagName().toLowerCase(), dataNodes); + } + } + + @SuppressWarnings({"unchecked", "rawtypes"}) + @Override + public Collection persistConfig(final String databaseName, final Collection configs) { + Collection result = new LinkedList<>(); + Map yamlConfigs = new YamlDataNodeRuleConfigurationSwapperEngine().swapToYamlRuleConfigurations(configs); + for (Entry entry : yamlConfigs.entrySet()) { + Collection dataNodes = entry.getValue().swapToDataNodes(entry.getKey()); + if (dataNodes.isEmpty()) { + continue; + } + result.addAll(persistDataNodes(databaseName, entry.getValue().getRuleTagName().toLowerCase(), dataNodes)); + } + return result; + } + + private Collection persistDataNodes(final String databaseName, final String ruleName, final Collection dataNodes) { + Collection result = new LinkedList<>(); + for (YamlDataNode each : dataNodes) { + List versions = repository.getChildrenKeys(DatabaseMetaDataNode.getDatabaseRuleVersionsNode(databaseName, ruleName, each.getKey())); + String nextVersion = versions.isEmpty() ? DEFAULT_VERSION : String.valueOf(Integer.parseInt(versions.get(0)) + 1); + repository.persist(DatabaseMetaDataNode.getDatabaseRuleVersionNode(databaseName, ruleName, each.getKey(), nextVersion), each.getValue()); + if (Strings.isNullOrEmpty(getActiveVersion(databaseName, ruleName, each.getKey()))) { + repository.persist(DatabaseMetaDataNode.getDatabaseRuleActiveVersionNode(databaseName, ruleName, each.getKey()), DEFAULT_VERSION); + } + result.add(new MetaDataVersion(DatabaseMetaDataNode.getDatabaseRuleNode(databaseName, ruleName, each.getKey()), getActiveVersion(databaseName, ruleName, each.getKey()), nextVersion)); } - repository.persist(DatabaseMetaDataNode.getRulePath(databaseName, getDatabaseActiveVersion(databaseName)), - YamlEngine.marshal(createYamlRuleConfigurations(configs))); + return result; } - private Collection createYamlRuleConfigurations(final Collection ruleConfigs) { - return new YamlRuleConfigurationSwapperEngine().swapToYamlRuleConfigurations(ruleConfigs); + private String getActiveVersion(final String databaseName, final String ruleName, final String key) { + return repository.getDirectly(DatabaseMetaDataNode.getDatabaseRuleActiveVersionNode(databaseName, ruleName, key)); } - @SuppressWarnings("unchecked") + @SuppressWarnings({"unchecked", "rawtypes"}) @Override - public Collection load(final String databaseName) { - return isExisted(databaseName) - ? new YamlRuleConfigurationSwapperEngine().swapToRuleConfigurations(YamlEngine.unmarshal(repository.getDirectly(DatabaseMetaDataNode.getRulePath(databaseName, - getDatabaseActiveVersion(databaseName))), Collection.class, true)) - : new LinkedList<>(); + public Collection deleteConfig(final String databaseName, final Collection configs) { + Collection result = new LinkedList<>(); + Map yamlConfigs = new YamlDataNodeRuleConfigurationSwapperEngine().swapToYamlRuleConfigurations(configs); + for (Entry entry : yamlConfigs.entrySet()) { + Collection dataNodes = entry.getValue().swapToDataNodes(entry.getKey()); + if (dataNodes.isEmpty()) { + continue; + } + List newDataNodes = new LinkedList<>(dataNodes); + Collections.reverse(newDataNodes); + result.addAll(deleteDataNodes(databaseName, entry.getValue().getRuleTagName().toLowerCase(), newDataNodes)); + } + return result; + } + + @Override + public void delete(final String databaseName, final String ruleName) { + repository.delete(DatabaseMetaDataNode.getDatabaseRuleNode(databaseName, ruleName)); } - private boolean isExisted(final String databaseName) { - return !Strings.isNullOrEmpty(getDatabaseActiveVersion(databaseName)) - && !Strings.isNullOrEmpty(repository.getDirectly(DatabaseMetaDataNode.getRulePath(databaseName, getDatabaseActiveVersion(databaseName)))); + private Collection deleteDataNodes(final String databaseName, final String ruleName, final Collection dataNodes) { + Collection result = new LinkedList<>(); + for (YamlDataNode each : dataNodes) { + String delKey = DatabaseMetaDataNode.getDatabaseRuleNode(databaseName, ruleName, each.getKey()); + repository.delete(delKey); + result.add(new MetaDataVersion(delKey)); + } + return result; } - private String getDatabaseActiveVersion(final String databaseName) { - return repository.getDirectly(DatabaseMetaDataNode.getActiveVersionPath(databaseName)); + @Override + public Collection load(final String databaseName) { + Collection dataNodes = getDataNodes(DatabaseMetaDataNode.getRulesNode(databaseName)); + return dataNodes.isEmpty() ? Collections.emptyList() : new YamlDataNodeRuleConfigurationSwapperEngine().swapToRuleConfigurations(dataNodes); } } diff --git a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/config/database/rule/NewDatabaseRulePersistService.java b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/config/database/rule/NewDatabaseRulePersistService.java deleted file mode 100644 index 367231124a275..0000000000000 --- a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/config/database/rule/NewDatabaseRulePersistService.java +++ /dev/null @@ -1,136 +0,0 @@ -/* - * 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.metadata.persist.service.config.database.rule; - -import com.google.common.base.Strings; -import org.apache.shardingsphere.infra.config.rule.RuleConfiguration; -import org.apache.shardingsphere.infra.metadata.version.MetaDataVersion; -import org.apache.shardingsphere.infra.util.yaml.datanode.YamlDataNode; -import org.apache.shardingsphere.infra.yaml.config.swapper.rule.YamlDataNodeRuleConfigurationSwapper; -import org.apache.shardingsphere.infra.yaml.config.swapper.rule.YamlDataNodeRuleConfigurationSwapperEngine; -import org.apache.shardingsphere.metadata.persist.node.NewDatabaseMetaDataNode; -import org.apache.shardingsphere.metadata.persist.service.config.AbstractPersistService; -import org.apache.shardingsphere.metadata.persist.service.config.database.DatabaseBasedPersistService; -import org.apache.shardingsphere.mode.spi.PersistRepository; - -import java.util.Collection; -import java.util.Collections; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; -import java.util.Map.Entry; - -/** - * TODO Rename DatabaseRulePersistService when metadata structure adjustment completed. #25485 - * Database rule persist service. - */ -public final class NewDatabaseRulePersistService extends AbstractPersistService implements DatabaseBasedPersistService> { - - private static final String DEFAULT_VERSION = "0"; - - private final PersistRepository repository; - - public NewDatabaseRulePersistService(final PersistRepository repository) { - super(repository); - this.repository = repository; - } - - @SuppressWarnings({"unchecked", "rawtypes"}) - @Override - public void persist(final String databaseName, final Collection configs) { - Map yamlConfigs = new YamlDataNodeRuleConfigurationSwapperEngine().swapToYamlRuleConfigurations(configs); - for (Entry entry : yamlConfigs.entrySet()) { - Collection dataNodes = entry.getValue().swapToDataNodes(entry.getKey()); - if (dataNodes.isEmpty()) { - continue; - } - persistDataNodes(databaseName, entry.getValue().getRuleTagName().toLowerCase(), dataNodes); - } - } - - @SuppressWarnings({"unchecked", "rawtypes"}) - @Override - public Collection persistConfig(final String databaseName, final Collection configs) { - Collection result = new LinkedList<>(); - Map yamlConfigs = new YamlDataNodeRuleConfigurationSwapperEngine().swapToYamlRuleConfigurations(configs); - for (Entry entry : yamlConfigs.entrySet()) { - Collection dataNodes = entry.getValue().swapToDataNodes(entry.getKey()); - if (dataNodes.isEmpty()) { - continue; - } - result.addAll(persistDataNodes(databaseName, entry.getValue().getRuleTagName().toLowerCase(), dataNodes)); - } - return result; - } - - private Collection persistDataNodes(final String databaseName, final String ruleName, final Collection dataNodes) { - Collection result = new LinkedList<>(); - for (YamlDataNode each : dataNodes) { - List versions = repository.getChildrenKeys(NewDatabaseMetaDataNode.getDatabaseRuleVersionsNode(databaseName, ruleName, each.getKey())); - String nextVersion = versions.isEmpty() ? DEFAULT_VERSION : String.valueOf(Integer.parseInt(versions.get(0)) + 1); - repository.persist(NewDatabaseMetaDataNode.getDatabaseRuleVersionNode(databaseName, ruleName, each.getKey(), nextVersion), each.getValue()); - if (Strings.isNullOrEmpty(getActiveVersion(databaseName, ruleName, each.getKey()))) { - repository.persist(NewDatabaseMetaDataNode.getDatabaseRuleActiveVersionNode(databaseName, ruleName, each.getKey()), DEFAULT_VERSION); - } - result.add(new MetaDataVersion(NewDatabaseMetaDataNode.getDatabaseRuleNode(databaseName, ruleName, each.getKey()), getActiveVersion(databaseName, ruleName, each.getKey()), nextVersion)); - } - return result; - } - - private String getActiveVersion(final String databaseName, final String ruleName, final String key) { - return repository.getDirectly(NewDatabaseMetaDataNode.getDatabaseRuleActiveVersionNode(databaseName, ruleName, key)); - } - - @SuppressWarnings({"unchecked", "rawtypes"}) - @Override - public Collection deleteConfig(final String databaseName, final Collection configs) { - Collection result = new LinkedList<>(); - Map yamlConfigs = new YamlDataNodeRuleConfigurationSwapperEngine().swapToYamlRuleConfigurations(configs); - for (Entry entry : yamlConfigs.entrySet()) { - Collection dataNodes = entry.getValue().swapToDataNodes(entry.getKey()); - if (dataNodes.isEmpty()) { - continue; - } - List newDataNodes = new LinkedList<>(dataNodes); - Collections.reverse(newDataNodes); - result.addAll(deleteDataNodes(databaseName, entry.getValue().getRuleTagName().toLowerCase(), newDataNodes)); - } - return result; - } - - @Override - public void delete(final String databaseName, final String ruleName) { - repository.delete(NewDatabaseMetaDataNode.getDatabaseRuleNode(databaseName, ruleName)); - } - - private Collection deleteDataNodes(final String databaseName, final String ruleName, final Collection dataNodes) { - Collection result = new LinkedList<>(); - for (YamlDataNode each : dataNodes) { - String delKey = NewDatabaseMetaDataNode.getDatabaseRuleNode(databaseName, ruleName, each.getKey()); - repository.delete(delKey); - result.add(new MetaDataVersion(delKey)); - } - return result; - } - - @Override - public Collection load(final String databaseName) { - Collection dataNodes = getDataNodes(NewDatabaseMetaDataNode.getRulesNode(databaseName)); - return dataNodes.isEmpty() ? Collections.emptyList() : new YamlDataNodeRuleConfigurationSwapperEngine().swapToRuleConfigurations(dataNodes); - } -} diff --git a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/config/global/GlobalRulePersistService.java b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/config/global/GlobalRulePersistService.java index e3c2e0338768b..f30dd33d8e497 100644 --- a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/config/global/GlobalRulePersistService.java +++ b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/config/global/GlobalRulePersistService.java @@ -18,48 +18,96 @@ package org.apache.shardingsphere.metadata.persist.service.config.global; import com.google.common.base.Strings; -import lombok.RequiredArgsConstructor; import org.apache.shardingsphere.authority.config.AuthorityRuleConfiguration; import org.apache.shardingsphere.infra.config.rule.RuleConfiguration; import org.apache.shardingsphere.infra.metadata.user.ShardingSphereUser; import org.apache.shardingsphere.infra.metadata.version.MetaDataVersion; -import org.apache.shardingsphere.infra.util.yaml.YamlEngine; -import org.apache.shardingsphere.infra.yaml.config.swapper.rule.YamlRuleConfigurationSwapperEngine; +import org.apache.shardingsphere.infra.util.yaml.datanode.YamlDataNode; +import org.apache.shardingsphere.infra.yaml.config.swapper.rule.YamlDataNodeGlobalRuleConfigurationSwapper; +import org.apache.shardingsphere.infra.yaml.config.swapper.rule.YamlDataNodeGlobalRuleConfigurationSwapperEngine; import org.apache.shardingsphere.metadata.persist.node.GlobalNode; +import org.apache.shardingsphere.metadata.persist.service.config.AbstractPersistService; import org.apache.shardingsphere.mode.spi.PersistRepository; import java.util.Collection; import java.util.Collections; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; import java.util.Optional; /** * Global rule persist service. */ -@RequiredArgsConstructor -public final class GlobalRulePersistService implements GlobalPersistService> { +public final class GlobalRulePersistService extends AbstractPersistService implements GlobalPersistService> { + + private static final String DEFAULT_VERSION = "0"; private final PersistRepository repository; + public GlobalRulePersistService(final PersistRepository repository) { + super(repository); + this.repository = repository; + } + + @SuppressWarnings({"unchecked", "rawtypes"}) @Override public void persist(final Collection globalRuleConfigs) { - repository.persist(GlobalNode.getGlobalRuleNode(), YamlEngine.marshal(new YamlRuleConfigurationSwapperEngine().swapToYamlRuleConfigurations(globalRuleConfigs))); + Map yamlConfigs = new YamlDataNodeGlobalRuleConfigurationSwapperEngine().swapToYamlRuleConfigurations(globalRuleConfigs); + for (Entry entry : yamlConfigs.entrySet()) { + Collection dataNodes = entry.getValue().swapToDataNodes(entry.getKey()); + if (dataNodes.isEmpty()) { + continue; + } + persistDataNodes(dataNodes); + } } + @SuppressWarnings({"unchecked", "rawtypes"}) @Override public Collection persistConfig(final Collection globalRuleConfigs) { - return Collections.emptyList(); + Collection result = new LinkedList<>(); + Map yamlConfigs = new YamlDataNodeGlobalRuleConfigurationSwapperEngine().swapToYamlRuleConfigurations(globalRuleConfigs); + for (Entry entry : yamlConfigs.entrySet()) { + Collection dataNodes = entry.getValue().swapToDataNodes(entry.getKey()); + if (dataNodes.isEmpty()) { + continue; + } + result.addAll(persistDataNodes(dataNodes)); + } + return result; + } + + private Collection persistDataNodes(final Collection dataNodes) { + Collection result = new LinkedList<>(); + for (YamlDataNode each : dataNodes) { + List versions = repository.getChildrenKeys(GlobalNode.getGlobalRuleVersionsNode(each.getKey())); + String nextActiveVersion = versions.isEmpty() ? DEFAULT_VERSION : String.valueOf(Integer.parseInt(versions.get(0)) + 1); + String persistKey = GlobalNode.getGlobalRuleVersionNode(each.getKey(), nextActiveVersion); + repository.persist(persistKey, each.getValue()); + if (Strings.isNullOrEmpty(getActiveVersion(GlobalNode.getGlobalRuleActiveVersionNode(each.getKey())))) { + repository.persist(GlobalNode.getGlobalRuleActiveVersionNode(each.getKey()), DEFAULT_VERSION); + } + result.add(new MetaDataVersion(GlobalNode.getGlobalRuleNode(each.getKey()), getActiveVersion(GlobalNode.getGlobalRuleActiveVersionNode(each.getKey())), nextActiveVersion)); + } + return result; } @Override - @SuppressWarnings("unchecked") public Collection load() { - String globalRule = repository.getDirectly(GlobalNode.getGlobalRuleNode()); - return Strings.isNullOrEmpty(globalRule) - ? Collections.emptyList() - : new YamlRuleConfigurationSwapperEngine().swapToRuleConfigurations(YamlEngine.unmarshal(globalRule, Collection.class)); + Collection dataNodes = getDataNodes(GlobalNode.getGlobalRuleRootNode()); + return dataNodes.isEmpty() ? Collections.emptyList() : new YamlDataNodeGlobalRuleConfigurationSwapperEngine().swapToRuleConfigurations(dataNodes); + } + + @Override + public RuleConfiguration load(final String ruleName) { + Collection dataNodes = getDataNodes(GlobalNode.getGlobalRuleNode(ruleName)); + return new YamlDataNodeGlobalRuleConfigurationSwapperEngine().swapSingleRuleToRuleConfiguration(ruleName, dataNodes).orElse(null); } /** + * TODO Avoid load all keys. * Load all users. * * @return collection of user diff --git a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/config/global/NewGlobalRulePersistService.java b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/config/global/NewGlobalRulePersistService.java deleted file mode 100644 index 765ed8cf6ba9d..0000000000000 --- a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/config/global/NewGlobalRulePersistService.java +++ /dev/null @@ -1,122 +0,0 @@ -/* - * 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.metadata.persist.service.config.global; - -import com.google.common.base.Strings; -import org.apache.shardingsphere.authority.config.AuthorityRuleConfiguration; -import org.apache.shardingsphere.infra.config.rule.RuleConfiguration; -import org.apache.shardingsphere.infra.metadata.user.ShardingSphereUser; -import org.apache.shardingsphere.infra.metadata.version.MetaDataVersion; -import org.apache.shardingsphere.infra.util.yaml.datanode.YamlDataNode; -import org.apache.shardingsphere.infra.yaml.config.swapper.rule.YamlDataNodeGlobalRuleConfigurationSwapper; -import org.apache.shardingsphere.infra.yaml.config.swapper.rule.YamlDataNodeGlobalRuleConfigurationSwapperEngine; -import org.apache.shardingsphere.metadata.persist.node.NewGlobalNode; -import org.apache.shardingsphere.metadata.persist.service.config.AbstractPersistService; -import org.apache.shardingsphere.mode.spi.PersistRepository; - -import java.util.Collection; -import java.util.Collections; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; -import java.util.Map.Entry; -import java.util.Optional; - -/** - * TODO Rename GlobalRulePersistService when metadata structure adjustment completed. #25485 - * New Global rule persist service. - */ -public final class NewGlobalRulePersistService extends AbstractPersistService implements GlobalPersistService> { - - private static final String DEFAULT_VERSION = "0"; - - private final PersistRepository repository; - - public NewGlobalRulePersistService(final PersistRepository repository) { - super(repository); - this.repository = repository; - } - - @SuppressWarnings({"unchecked", "rawtypes"}) - @Override - public void persist(final Collection globalRuleConfigs) { - Map yamlConfigs = new YamlDataNodeGlobalRuleConfigurationSwapperEngine().swapToYamlRuleConfigurations(globalRuleConfigs); - for (Entry entry : yamlConfigs.entrySet()) { - Collection dataNodes = entry.getValue().swapToDataNodes(entry.getKey()); - if (dataNodes.isEmpty()) { - continue; - } - persistDataNodes(dataNodes); - } - } - - @SuppressWarnings({"unchecked", "rawtypes"}) - @Override - public Collection persistConfig(final Collection globalRuleConfigs) { - Collection result = new LinkedList<>(); - Map yamlConfigs = new YamlDataNodeGlobalRuleConfigurationSwapperEngine().swapToYamlRuleConfigurations(globalRuleConfigs); - for (Entry entry : yamlConfigs.entrySet()) { - Collection dataNodes = entry.getValue().swapToDataNodes(entry.getKey()); - if (dataNodes.isEmpty()) { - continue; - } - result.addAll(persistDataNodes(dataNodes)); - } - return result; - } - - private Collection persistDataNodes(final Collection dataNodes) { - Collection result = new LinkedList<>(); - for (YamlDataNode each : dataNodes) { - List versions = repository.getChildrenKeys(NewGlobalNode.getGlobalRuleVersionsNode(each.getKey())); - String nextActiveVersion = versions.isEmpty() ? DEFAULT_VERSION : String.valueOf(Integer.parseInt(versions.get(0)) + 1); - String persistKey = NewGlobalNode.getGlobalRuleVersionNode(each.getKey(), nextActiveVersion); - repository.persist(persistKey, each.getValue()); - if (Strings.isNullOrEmpty(getActiveVersion(NewGlobalNode.getGlobalRuleActiveVersionNode(each.getKey())))) { - repository.persist(NewGlobalNode.getGlobalRuleActiveVersionNode(each.getKey()), DEFAULT_VERSION); - } - result.add(new MetaDataVersion(NewGlobalNode.getGlobalRuleNode(each.getKey()), getActiveVersion(NewGlobalNode.getGlobalRuleActiveVersionNode(each.getKey())), nextActiveVersion)); - } - return result; - } - - @Override - public Collection load() { - Collection dataNodes = getDataNodes(NewGlobalNode.getGlobalRuleRootNode()); - return dataNodes.isEmpty() ? Collections.emptyList() : new YamlDataNodeGlobalRuleConfigurationSwapperEngine().swapToRuleConfigurations(dataNodes); - } - - @Override - public RuleConfiguration load(final String ruleName) { - Collection dataNodes = getDataNodes(NewGlobalNode.getGlobalRuleNode(ruleName)); - return new YamlDataNodeGlobalRuleConfigurationSwapperEngine().swapSingleRuleToRuleConfiguration(ruleName, dataNodes).orElse(null); - } - - /** - * TODO Avoid load all keys. - * Load all users. - * - * @return collection of user - */ - @Override - public Collection loadUsers() { - Optional authorityRuleConfig = load().stream() - .filter(AuthorityRuleConfiguration.class::isInstance).map(AuthorityRuleConfiguration.class::cast).findFirst(); - return authorityRuleConfig.isPresent() ? authorityRuleConfig.get().getUsers() : Collections.emptyList(); - } -} diff --git a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/config/global/NewPropertiesPersistService.java b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/config/global/NewPropertiesPersistService.java deleted file mode 100644 index 46e475cdd0283..0000000000000 --- a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/config/global/NewPropertiesPersistService.java +++ /dev/null @@ -1,76 +0,0 @@ -/* - * 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.metadata.persist.service.config.global; - -import com.google.common.base.Strings; -import lombok.RequiredArgsConstructor; -import org.apache.shardingsphere.infra.metadata.version.MetaDataVersion; -import org.apache.shardingsphere.infra.util.yaml.YamlEngine; -import org.apache.shardingsphere.metadata.persist.node.NewGlobalNode; -import org.apache.shardingsphere.mode.spi.PersistRepository; - -import java.util.Collection; -import java.util.Collections; -import java.util.List; -import java.util.Properties; - -/** - * TODO Rename PropertiesPersistService when metadata structure adjustment completed. #25485 - * Properties persist service. - */ -@RequiredArgsConstructor -public final class NewPropertiesPersistService implements GlobalPersistService { - - private static final String DEFAULT_VERSION = "0"; - - private final PersistRepository repository; - - @Override - public void persist(final Properties props) { - if (Strings.isNullOrEmpty(getActiveVersion())) { - repository.persist(NewGlobalNode.getPropsActiveVersionNode(), DEFAULT_VERSION); - } - List versions = repository.getChildrenKeys(NewGlobalNode.getPropsVersionsNode()); - repository.persist(NewGlobalNode.getPropsVersionNode(versions.isEmpty() - ? DEFAULT_VERSION - : String.valueOf(Integer.parseInt(versions.get(0)) + 1)), YamlEngine.marshal(props)); - - } - - @Override - public Collection persistConfig(final Properties props) { - List versions = repository.getChildrenKeys(NewGlobalNode.getPropsVersionsNode()); - String nextActiveVersion = versions.isEmpty() ? DEFAULT_VERSION : String.valueOf(Integer.parseInt(versions.get(0)) + 1); - String persistKey = NewGlobalNode.getPropsVersionNode(nextActiveVersion); - repository.persist(persistKey, YamlEngine.marshal(props)); - if (Strings.isNullOrEmpty(getActiveVersion())) { - repository.persist(NewGlobalNode.getPropsActiveVersionNode(), DEFAULT_VERSION); - } - return Collections.singletonList(new MetaDataVersion(NewGlobalNode.getPropsRootNode(), getActiveVersion(), nextActiveVersion)); - } - - @Override - public Properties load() { - String yamlContent = repository.getDirectly(NewGlobalNode.getPropsVersionNode(getActiveVersion())); - return Strings.isNullOrEmpty(yamlContent) ? new Properties() : YamlEngine.unmarshal(yamlContent, Properties.class); - } - - private String getActiveVersion() { - return repository.getDirectly(NewGlobalNode.getPropsActiveVersionNode()); - } -} diff --git a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/config/global/PropertiesPersistService.java b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/config/global/PropertiesPersistService.java index de248ed1d4ed5..cdad56251d121 100644 --- a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/config/global/PropertiesPersistService.java +++ b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/config/global/PropertiesPersistService.java @@ -26,6 +26,7 @@ import java.util.Collection; import java.util.Collections; +import java.util.List; import java.util.Properties; /** @@ -34,21 +35,41 @@ @RequiredArgsConstructor public final class PropertiesPersistService implements GlobalPersistService { + private static final String DEFAULT_VERSION = "0"; + private final PersistRepository repository; @Override public void persist(final Properties props) { - repository.persist(GlobalNode.getPropsPath(), YamlEngine.marshal(props)); + if (Strings.isNullOrEmpty(getActiveVersion())) { + repository.persist(GlobalNode.getPropsActiveVersionNode(), DEFAULT_VERSION); + } + List versions = repository.getChildrenKeys(GlobalNode.getPropsVersionsNode()); + repository.persist(GlobalNode.getPropsVersionNode(versions.isEmpty() + ? DEFAULT_VERSION + : String.valueOf(Integer.parseInt(versions.get(0)) + 1)), YamlEngine.marshal(props)); + } @Override public Collection persistConfig(final Properties props) { - return Collections.emptyList(); + List versions = repository.getChildrenKeys(GlobalNode.getPropsVersionsNode()); + String nextActiveVersion = versions.isEmpty() ? DEFAULT_VERSION : String.valueOf(Integer.parseInt(versions.get(0)) + 1); + String persistKey = GlobalNode.getPropsVersionNode(nextActiveVersion); + repository.persist(persistKey, YamlEngine.marshal(props)); + if (Strings.isNullOrEmpty(getActiveVersion())) { + repository.persist(GlobalNode.getPropsActiveVersionNode(), DEFAULT_VERSION); + } + return Collections.singletonList(new MetaDataVersion(GlobalNode.getPropsRootNode(), getActiveVersion(), nextActiveVersion)); } @Override public Properties load() { - String props = repository.getDirectly(GlobalNode.getPropsPath()); - return Strings.isNullOrEmpty(props) ? new Properties() : YamlEngine.unmarshal(props, Properties.class); + String yamlContent = repository.getDirectly(GlobalNode.getPropsVersionNode(getActiveVersion())); + return Strings.isNullOrEmpty(yamlContent) ? new Properties() : YamlEngine.unmarshal(yamlContent, Properties.class); + } + + private String getActiveVersion() { + return repository.getDirectly(GlobalNode.getPropsActiveVersionNode()); } } diff --git a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/database/DatabaseMetaDataPersistService.java b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/database/DatabaseMetaDataPersistService.java index 8def74b28ef05..76bf98f49793b 100644 --- a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/database/DatabaseMetaDataPersistService.java +++ b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/database/DatabaseMetaDataPersistService.java @@ -24,6 +24,7 @@ import org.apache.shardingsphere.metadata.persist.node.DatabaseMetaDataNode; import org.apache.shardingsphere.metadata.persist.service.schema.TableMetaDataPersistService; import org.apache.shardingsphere.metadata.persist.service.schema.ViewMetaDataPersistService; +import org.apache.shardingsphere.metadata.persist.service.version.MetaDataVersionPersistService; import org.apache.shardingsphere.mode.spi.PersistRepository; import java.util.Collection; @@ -42,10 +43,13 @@ public final class DatabaseMetaDataPersistService implements DatabaseMetaDataBas private final ViewMetaDataPersistService viewMetaDataPersistService; - public DatabaseMetaDataPersistService(final PersistRepository repository) { + private final MetaDataVersionPersistService metaDataVersionPersistService; + + public DatabaseMetaDataPersistService(final PersistRepository repository, final MetaDataVersionPersistService metaDataVersionPersistService) { this.repository = repository; this.tableMetaDataPersistService = new TableMetaDataPersistService(repository); this.viewMetaDataPersistService = new ViewMetaDataPersistService(repository); + this.metaDataVersionPersistService = metaDataVersionPersistService; } /** @@ -75,7 +79,7 @@ public void dropDatabase(final String databaseName) { */ @Override public Collection loadAllDatabaseNames() { - return repository.getChildrenKeys(DatabaseMetaDataNode.getMetaDataNodePath()); + return repository.getChildrenKeys(DatabaseMetaDataNode.getMetaDataNode()); } /** @@ -113,7 +117,8 @@ public void compareAndPersist(final String databaseName, final String schemaName addSchema(databaseName, schemaName); } Map currentTables = tableMetaDataPersistService.load(databaseName, schemaName); - tableMetaDataPersistService.persist(databaseName, schemaName, GenericSchemaManager.getToBeAddedTables(schema.getTables(), currentTables)); + metaDataVersionPersistService.switchActiveVersion(tableMetaDataPersistService + .persistSchemaMetaData(databaseName, schemaName, GenericSchemaManager.getToBeAddedTables(schema.getTables(), currentTables))); GenericSchemaManager.getToBeDeletedTables(schema.getTables(), currentTables).forEach((key, value) -> tableMetaDataPersistService.delete(databaseName, schemaName, key)); } @@ -129,7 +134,7 @@ public void persist(final String databaseName, final String schemaName, final Sh if (schema.getTables().isEmpty() && schema.getViews().isEmpty()) { addSchema(databaseName, schemaName); } - tableMetaDataPersistService.persist(databaseName, schemaName, schema.getTables()); + metaDataVersionPersistService.switchActiveVersion(tableMetaDataPersistService.persistSchemaMetaData(databaseName, schemaName, schema.getTables())); } /** diff --git a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/database/NewDatabaseMetaDataPersistService.java b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/database/NewDatabaseMetaDataPersistService.java deleted file mode 100644 index 48473e39cf281..0000000000000 --- a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/database/NewDatabaseMetaDataPersistService.java +++ /dev/null @@ -1,170 +0,0 @@ -/* - * 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.metadata.persist.service.database; - -import lombok.Getter; -import org.apache.shardingsphere.infra.metadata.database.schema.manager.GenericSchemaManager; -import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema; -import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereTable; -import org.apache.shardingsphere.metadata.persist.node.DatabaseMetaDataNode; -import org.apache.shardingsphere.metadata.persist.service.schema.NewTableMetaDataPersistService; -import org.apache.shardingsphere.metadata.persist.service.schema.NewViewMetaDataPersistService; -import org.apache.shardingsphere.metadata.persist.service.version.MetaDataVersionPersistService; -import org.apache.shardingsphere.mode.spi.PersistRepository; - -import java.util.Collection; -import java.util.LinkedHashMap; -import java.util.Map; - -/** - * Database meta data registry service. - */ -@Getter -public final class NewDatabaseMetaDataPersistService implements DatabaseMetaDataBasedPersistService { - - private final PersistRepository repository; - - private final NewTableMetaDataPersistService tableMetaDataPersistService; - - private final NewViewMetaDataPersistService viewMetaDataPersistService; - - private final MetaDataVersionPersistService metaDataVersionPersistService; - - public NewDatabaseMetaDataPersistService(final PersistRepository repository, final MetaDataVersionPersistService metaDataVersionPersistService) { - this.repository = repository; - this.tableMetaDataPersistService = new NewTableMetaDataPersistService(repository); - this.viewMetaDataPersistService = new NewViewMetaDataPersistService(repository); - this.metaDataVersionPersistService = metaDataVersionPersistService; - } - - /** - * Add database name. - * - * @param databaseName database name - */ - @Override - public void addDatabase(final String databaseName) { - repository.persist(DatabaseMetaDataNode.getDatabaseNamePath(databaseName), ""); - } - - /** - * Drop database. - * - * @param databaseName database name to be deleted - */ - @Override - public void dropDatabase(final String databaseName) { - repository.delete(DatabaseMetaDataNode.getDatabaseNamePath(databaseName)); - } - - /** - * Load all database names. - * - * @return all database names - */ - @Override - public Collection loadAllDatabaseNames() { - return repository.getChildrenKeys(DatabaseMetaDataNode.getMetaDataNodePath()); - } - - /** - * Add schema. - * - * @param databaseName database name - * @param schemaName schema name - */ - @Override - public void addSchema(final String databaseName, final String schemaName) { - repository.persist(DatabaseMetaDataNode.getMetaDataTablesPath(databaseName, schemaName), ""); - } - - /** - * Drop schema. - * - * @param databaseName database name - * @param schemaName schema name - */ - @Override - public void dropSchema(final String databaseName, final String schemaName) { - repository.delete(DatabaseMetaDataNode.getMetaDataSchemaPath(databaseName, schemaName)); - } - - /** - * Compare and persist schema meta data. - * - * @param databaseName database name - * @param schemaName schema name - * @param schema schema meta data - */ - @Override - public void compareAndPersist(final String databaseName, final String schemaName, final ShardingSphereSchema schema) { - if (schema.getTables().isEmpty() && schema.getViews().isEmpty()) { - addSchema(databaseName, schemaName); - } - Map currentTables = tableMetaDataPersistService.load(databaseName, schemaName); - metaDataVersionPersistService.switchActiveVersion(tableMetaDataPersistService - .persistSchemaMetaData(databaseName, schemaName, GenericSchemaManager.getToBeAddedTables(schema.getTables(), currentTables))); - GenericSchemaManager.getToBeDeletedTables(schema.getTables(), currentTables).forEach((key, value) -> tableMetaDataPersistService.delete(databaseName, schemaName, key)); - } - - /** - * Persist schema meta data. - * - * @param databaseName database name - * @param schemaName schema name - * @param schema schema meta data - */ - @Override - public void persist(final String databaseName, final String schemaName, final ShardingSphereSchema schema) { - if (schema.getTables().isEmpty() && schema.getViews().isEmpty()) { - addSchema(databaseName, schemaName); - } - metaDataVersionPersistService.switchActiveVersion(tableMetaDataPersistService.persistSchemaMetaData(databaseName, schemaName, schema.getTables())); - } - - /** - * Delete schema meta data. - * - * @param databaseName database name - * @param schemaName schema name - * @param schema schema meta data - */ - @Override - public void delete(final String databaseName, final String schemaName, final ShardingSphereSchema schema) { - schema.getTables().forEach((key, value) -> tableMetaDataPersistService.delete(databaseName, schemaName, key)); - } - - /** - * Load schema meta data. - * - * @param databaseName database name - * @return schema meta data - */ - @Override - public Map loadSchemas(final String databaseName) { - Collection schemaNames = loadAllSchemaNames(databaseName); - Map result = new LinkedHashMap<>(schemaNames.size(), 1F); - schemaNames.forEach(each -> result.put(each.toLowerCase(), - new ShardingSphereSchema(tableMetaDataPersistService.load(databaseName, each), viewMetaDataPersistService.load(databaseName, each)))); - return result; - } - - private Collection loadAllSchemaNames(final String databaseName) { - return repository.getChildrenKeys(DatabaseMetaDataNode.getMetaDataSchemasPath(databaseName)); - } -} diff --git a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/schema/NewTableMetaDataPersistService.java b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/schema/NewTableMetaDataPersistService.java deleted file mode 100644 index a0911e21b552b..0000000000000 --- a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/schema/NewTableMetaDataPersistService.java +++ /dev/null @@ -1,112 +0,0 @@ -/* - * 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.metadata.persist.service.schema; - -import com.google.common.base.Strings; -import lombok.RequiredArgsConstructor; -import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereTable; -import org.apache.shardingsphere.infra.metadata.version.MetaDataVersion; -import org.apache.shardingsphere.infra.util.yaml.YamlEngine; -import org.apache.shardingsphere.infra.yaml.schema.pojo.YamlShardingSphereTable; -import org.apache.shardingsphere.infra.yaml.schema.swapper.YamlTableSwapper; -import org.apache.shardingsphere.metadata.persist.node.DatabaseMetaDataNode; -import org.apache.shardingsphere.metadata.persist.node.NewDatabaseMetaDataNode; -import org.apache.shardingsphere.mode.spi.PersistRepository; - -import java.util.Collection; -import java.util.Collections; -import java.util.LinkedHashMap; -import java.util.LinkedList; -import java.util.Map; -import java.util.List; -import java.util.Map.Entry; - -/** - * TODO Rename TableMetaDataPersistService when metadata structure adjustment completed. #25485 - * Table meta data persist service. - */ -@RequiredArgsConstructor -public final class NewTableMetaDataPersistService implements SchemaMetaDataPersistService> { - - private static final String DEFAULT_VERSION = "0"; - - private final PersistRepository repository; - - @Override - public void persist(final String databaseName, final String schemaName, final Map tables) { - for (Entry entry : tables.entrySet()) { - String tableName = entry.getKey().toLowerCase(); - List versions = repository.getChildrenKeys(NewDatabaseMetaDataNode.getTableVersionsNode(databaseName, schemaName, tableName)); - repository.persist(NewDatabaseMetaDataNode.getTableVersionNode(databaseName, schemaName, tableName, versions.isEmpty() - ? DEFAULT_VERSION - : String.valueOf(Integer.parseInt(versions.get(0)) + 1)), YamlEngine.marshal(new YamlTableSwapper().swapToYamlConfiguration(entry.getValue()))); - if (Strings.isNullOrEmpty(repository.getDirectly(NewDatabaseMetaDataNode.getTableActiveVersionNode(databaseName, schemaName, tableName)))) { - repository.persist(NewDatabaseMetaDataNode.getTableActiveVersionNode(databaseName, schemaName, tableName), DEFAULT_VERSION); - } - } - } - - @Override - public Collection persistSchemaMetaData(final String databaseName, final String schemaName, final Map tables) { - Collection result = new LinkedList<>(); - for (Entry entry : tables.entrySet()) { - String tableName = entry.getKey().toLowerCase(); - List versions = repository.getChildrenKeys(NewDatabaseMetaDataNode.getTableVersionsNode(databaseName, schemaName, tableName)); - String nextActiveVersion = versions.isEmpty() ? DEFAULT_VERSION : String.valueOf(Integer.parseInt(versions.get(0)) + 1); - repository.persist(NewDatabaseMetaDataNode.getTableVersionNode(databaseName, schemaName, tableName, nextActiveVersion), - YamlEngine.marshal(new YamlTableSwapper().swapToYamlConfiguration(entry.getValue()))); - if (Strings.isNullOrEmpty(getActiveVersion(databaseName, schemaName, tableName))) { - repository.persist(NewDatabaseMetaDataNode.getTableActiveVersionNode(databaseName, schemaName, tableName), DEFAULT_VERSION); - } - result.add(new MetaDataVersion(NewDatabaseMetaDataNode.getTableNode(databaseName, schemaName, tableName), getActiveVersion(databaseName, schemaName, tableName), nextActiveVersion)); - } - return result; - } - - private String getActiveVersion(final String databaseName, final String schemaName, final String tableName) { - return repository.getDirectly(NewDatabaseMetaDataNode.getTableActiveVersionNode(databaseName, schemaName, tableName)); - } - - @Override - public Map load(final String databaseName, final String schemaName) { - Collection tableNames = repository.getChildrenKeys(DatabaseMetaDataNode.getMetaDataTablesPath(databaseName, schemaName)); - return tableNames.isEmpty() ? Collections.emptyMap() : getTableMetaDataByTableNames(databaseName, schemaName, tableNames); - } - - @Override - public Map load(final String databaseName, final String schemaName, final String tableName) { - return getTableMetaDataByTableNames(databaseName, schemaName, Collections.singletonList(tableName)); - } - - private Map getTableMetaDataByTableNames(final String databaseName, final String schemaName, final Collection tableNames) { - Map result = new LinkedHashMap<>(tableNames.size(), 1F); - tableNames.forEach(each -> { - String table = repository.getDirectly(NewDatabaseMetaDataNode.getTableVersionNode(databaseName, schemaName, each, - repository.getDirectly(NewDatabaseMetaDataNode.getTableActiveVersionNode(databaseName, schemaName, each)))); - if (!Strings.isNullOrEmpty(table)) { - result.put(each.toLowerCase(), new YamlTableSwapper().swapToObject(YamlEngine.unmarshal(table, YamlShardingSphereTable.class))); - } - }); - return result; - } - - @Override - public void delete(final String databaseName, final String schemaName, final String tableName) { - repository.delete(NewDatabaseMetaDataNode.getTableNode(databaseName, schemaName, tableName.toLowerCase())); - } -} diff --git a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/schema/NewViewMetaDataPersistService.java b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/schema/NewViewMetaDataPersistService.java deleted file mode 100644 index c20818d96c091..0000000000000 --- a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/schema/NewViewMetaDataPersistService.java +++ /dev/null @@ -1,112 +0,0 @@ -/* - * 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.metadata.persist.service.schema; - -import com.google.common.base.Strings; -import lombok.RequiredArgsConstructor; -import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereView; -import org.apache.shardingsphere.infra.metadata.version.MetaDataVersion; -import org.apache.shardingsphere.infra.util.yaml.YamlEngine; -import org.apache.shardingsphere.infra.yaml.schema.pojo.YamlShardingSphereView; -import org.apache.shardingsphere.infra.yaml.schema.swapper.YamlViewSwapper; -import org.apache.shardingsphere.metadata.persist.node.DatabaseMetaDataNode; -import org.apache.shardingsphere.metadata.persist.node.NewDatabaseMetaDataNode; -import org.apache.shardingsphere.mode.spi.PersistRepository; - -import java.util.Collection; -import java.util.Collections; -import java.util.LinkedHashMap; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; -import java.util.Map.Entry; - -/** - * TODO Rename ViewMetaDataPersistService when metadata structure adjustment completed. #25485 - * View meta data persist service. - */ -@RequiredArgsConstructor -public final class NewViewMetaDataPersistService implements SchemaMetaDataPersistService> { - - private static final String DEFAULT_VERSION = "0"; - - private final PersistRepository repository; - - @Override - public void persist(final String databaseName, final String schemaName, final Map views) { - for (Entry entry : views.entrySet()) { - String viewName = entry.getKey().toLowerCase(); - List versions = repository.getChildrenKeys(NewDatabaseMetaDataNode.getViewVersionsNode(databaseName, schemaName, viewName)); - repository.persist(NewDatabaseMetaDataNode.getViewVersionNode(databaseName, schemaName, viewName, versions.isEmpty() - ? DEFAULT_VERSION - : String.valueOf(Integer.parseInt(versions.get(0)) + 1)), YamlEngine.marshal(new YamlViewSwapper().swapToYamlConfiguration(entry.getValue()))); - if (Strings.isNullOrEmpty(repository.getDirectly(NewDatabaseMetaDataNode.getViewActiveVersionNode(databaseName, schemaName, viewName)))) { - repository.persist(NewDatabaseMetaDataNode.getViewActiveVersionNode(databaseName, schemaName, viewName), DEFAULT_VERSION); - } - } - } - - @Override - public Collection persistSchemaMetaData(final String databaseName, final String schemaName, final Map views) { - Collection result = new LinkedList<>(); - for (Entry entry : views.entrySet()) { - String viewName = entry.getKey().toLowerCase(); - List versions = repository.getChildrenKeys(NewDatabaseMetaDataNode.getViewVersionsNode(databaseName, schemaName, viewName)); - String nextActiveVersion = versions.isEmpty() ? DEFAULT_VERSION : String.valueOf(Integer.parseInt(versions.get(0)) + 1); - repository.persist(NewDatabaseMetaDataNode.getViewVersionNode(databaseName, schemaName, viewName, nextActiveVersion), - YamlEngine.marshal(new YamlViewSwapper().swapToYamlConfiguration(entry.getValue()))); - if (Strings.isNullOrEmpty(getActiveVersion(databaseName, schemaName, viewName))) { - repository.persist(NewDatabaseMetaDataNode.getViewActiveVersionNode(databaseName, schemaName, viewName), DEFAULT_VERSION); - } - result.add(new MetaDataVersion(NewDatabaseMetaDataNode.getViewNode(databaseName, schemaName, viewName), getActiveVersion(databaseName, schemaName, viewName), nextActiveVersion)); - } - return result; - } - - private String getActiveVersion(final String databaseName, final String schemaName, final String viewName) { - return repository.getDirectly(NewDatabaseMetaDataNode.getViewActiveVersionNode(databaseName, schemaName, viewName)); - } - - @Override - public Map load(final String databaseName, final String schemaName) { - Collection viewNames = repository.getChildrenKeys(DatabaseMetaDataNode.getMetaDataViewsPath(databaseName, schemaName)); - return viewNames.isEmpty() ? Collections.emptyMap() : getViewMetaDataByViewNames(databaseName, schemaName, viewNames); - } - - @Override - public Map load(final String databaseName, final String schemaName, final String viewName) { - return getViewMetaDataByViewNames(databaseName, schemaName, Collections.singletonList(viewName)); - } - - private Map getViewMetaDataByViewNames(final String databaseName, final String schemaName, final Collection viewNames) { - Map result = new LinkedHashMap<>(viewNames.size(), 1F); - viewNames.forEach(each -> { - String view = repository.getDirectly(NewDatabaseMetaDataNode.getViewVersionNode(databaseName, schemaName, each, - repository.getDirectly(NewDatabaseMetaDataNode.getViewActiveVersionNode(databaseName, schemaName, each)))); - if (!Strings.isNullOrEmpty(view)) { - result.put(each.toLowerCase(), new YamlViewSwapper().swapToObject(YamlEngine.unmarshal(view, YamlShardingSphereView.class))); - } - }); - return result; - } - - @Override - public void delete(final String databaseName, final String schemaName, final String viewName) { - repository.delete(NewDatabaseMetaDataNode.getViewNode(databaseName, schemaName, viewName.toLowerCase())); - } -} diff --git a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/schema/TableMetaDataPersistService.java b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/schema/TableMetaDataPersistService.java index f02c7e307eb05..0cac6e01166c2 100644 --- a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/schema/TableMetaDataPersistService.java +++ b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/schema/TableMetaDataPersistService.java @@ -30,7 +30,10 @@ import java.util.Collection; import java.util.Collections; import java.util.LinkedHashMap; +import java.util.LinkedList; import java.util.Map; +import java.util.List; +import java.util.Map.Entry; /** * Table meta data persist service. @@ -38,18 +41,43 @@ @RequiredArgsConstructor public final class TableMetaDataPersistService implements SchemaMetaDataPersistService> { + private static final String DEFAULT_VERSION = "0"; + private final PersistRepository repository; @Override public void persist(final String databaseName, final String schemaName, final Map tables) { - tables.forEach((key, value) -> repository.persist(DatabaseMetaDataNode.getTableMetaDataPath(databaseName, schemaName, key.toLowerCase()), - YamlEngine.marshal(new YamlTableSwapper().swapToYamlConfiguration(value)))); + for (Entry entry : tables.entrySet()) { + String tableName = entry.getKey().toLowerCase(); + List versions = repository.getChildrenKeys(DatabaseMetaDataNode.getTableVersionsNode(databaseName, schemaName, tableName)); + repository.persist(DatabaseMetaDataNode.getTableVersionNode(databaseName, schemaName, tableName, versions.isEmpty() + ? DEFAULT_VERSION + : String.valueOf(Integer.parseInt(versions.get(0)) + 1)), YamlEngine.marshal(new YamlTableSwapper().swapToYamlConfiguration(entry.getValue()))); + if (Strings.isNullOrEmpty(repository.getDirectly(DatabaseMetaDataNode.getTableActiveVersionNode(databaseName, schemaName, tableName)))) { + repository.persist(DatabaseMetaDataNode.getTableActiveVersionNode(databaseName, schemaName, tableName), DEFAULT_VERSION); + } + } } - // TODO Remove this when metadata structure adjustment completed. #25485 @Override - public Collection persistSchemaMetaData(final String databaseName, final String schemaName, final Map schema) { - return Collections.emptyList(); + public Collection persistSchemaMetaData(final String databaseName, final String schemaName, final Map tables) { + Collection result = new LinkedList<>(); + for (Entry entry : tables.entrySet()) { + String tableName = entry.getKey().toLowerCase(); + List versions = repository.getChildrenKeys(DatabaseMetaDataNode.getTableVersionsNode(databaseName, schemaName, tableName)); + String nextActiveVersion = versions.isEmpty() ? DEFAULT_VERSION : String.valueOf(Integer.parseInt(versions.get(0)) + 1); + repository.persist(DatabaseMetaDataNode.getTableVersionNode(databaseName, schemaName, tableName, nextActiveVersion), + YamlEngine.marshal(new YamlTableSwapper().swapToYamlConfiguration(entry.getValue()))); + if (Strings.isNullOrEmpty(getActiveVersion(databaseName, schemaName, tableName))) { + repository.persist(DatabaseMetaDataNode.getTableActiveVersionNode(databaseName, schemaName, tableName), DEFAULT_VERSION); + } + result.add(new MetaDataVersion(DatabaseMetaDataNode.getTableNode(databaseName, schemaName, tableName), getActiveVersion(databaseName, schemaName, tableName), nextActiveVersion)); + } + return result; + } + + private String getActiveVersion(final String databaseName, final String schemaName, final String tableName) { + return repository.getDirectly(DatabaseMetaDataNode.getTableActiveVersionNode(databaseName, schemaName, tableName)); } @Override @@ -58,25 +86,25 @@ public Map load(final String databaseName, final St return tableNames.isEmpty() ? Collections.emptyMap() : getTableMetaDataByTableNames(databaseName, schemaName, tableNames); } - // TODO Remove this when metadata structure adjustment completed. #25485 @Override public Map load(final String databaseName, final String schemaName, final String tableName) { - return Collections.emptyMap(); - } - - @Override - public void delete(final String databaseName, final String schemaName, final String tableName) { - repository.delete(DatabaseMetaDataNode.getTableMetaDataPath(databaseName, schemaName, tableName.toLowerCase())); + return getTableMetaDataByTableNames(databaseName, schemaName, Collections.singletonList(tableName)); } private Map getTableMetaDataByTableNames(final String databaseName, final String schemaName, final Collection tableNames) { Map result = new LinkedHashMap<>(tableNames.size(), 1F); tableNames.forEach(each -> { - String table = repository.getDirectly(DatabaseMetaDataNode.getTableMetaDataPath(databaseName, schemaName, each)); + String table = repository.getDirectly(DatabaseMetaDataNode.getTableVersionNode(databaseName, schemaName, each, + repository.getDirectly(DatabaseMetaDataNode.getTableActiveVersionNode(databaseName, schemaName, each)))); if (!Strings.isNullOrEmpty(table)) { result.put(each.toLowerCase(), new YamlTableSwapper().swapToObject(YamlEngine.unmarshal(table, YamlShardingSphereTable.class))); } }); return result; } + + @Override + public void delete(final String databaseName, final String schemaName, final String tableName) { + repository.delete(DatabaseMetaDataNode.getTableNode(databaseName, schemaName, tableName.toLowerCase())); + } } diff --git a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/schema/ViewMetaDataPersistService.java b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/schema/ViewMetaDataPersistService.java index 7487f1eb7abd9..72b32bf273c5a 100644 --- a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/schema/ViewMetaDataPersistService.java +++ b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/schema/ViewMetaDataPersistService.java @@ -30,7 +30,10 @@ import java.util.Collection; import java.util.Collections; import java.util.LinkedHashMap; +import java.util.LinkedList; +import java.util.List; import java.util.Map; +import java.util.Map.Entry; /** * View meta data persist service. @@ -38,18 +41,43 @@ @RequiredArgsConstructor public final class ViewMetaDataPersistService implements SchemaMetaDataPersistService> { + private static final String DEFAULT_VERSION = "0"; + private final PersistRepository repository; @Override public void persist(final String databaseName, final String schemaName, final Map views) { - views.forEach((key, value) -> repository.persist(DatabaseMetaDataNode.getViewMetaDataPath(databaseName, schemaName, key.toLowerCase()), - YamlEngine.marshal(new YamlViewSwapper().swapToYamlConfiguration(value)))); + for (Entry entry : views.entrySet()) { + String viewName = entry.getKey().toLowerCase(); + List versions = repository.getChildrenKeys(DatabaseMetaDataNode.getViewVersionsNode(databaseName, schemaName, viewName)); + repository.persist(DatabaseMetaDataNode.getViewVersionNode(databaseName, schemaName, viewName, versions.isEmpty() + ? DEFAULT_VERSION + : String.valueOf(Integer.parseInt(versions.get(0)) + 1)), YamlEngine.marshal(new YamlViewSwapper().swapToYamlConfiguration(entry.getValue()))); + if (Strings.isNullOrEmpty(repository.getDirectly(DatabaseMetaDataNode.getViewActiveVersionNode(databaseName, schemaName, viewName)))) { + repository.persist(DatabaseMetaDataNode.getViewActiveVersionNode(databaseName, schemaName, viewName), DEFAULT_VERSION); + } + } } - // TODO Remove this when metadata structure adjustment completed. #25485 @Override - public Collection persistSchemaMetaData(final String databaseName, final String schemaName, final Map schema) { - return Collections.emptyList(); + public Collection persistSchemaMetaData(final String databaseName, final String schemaName, final Map views) { + Collection result = new LinkedList<>(); + for (Entry entry : views.entrySet()) { + String viewName = entry.getKey().toLowerCase(); + List versions = repository.getChildrenKeys(DatabaseMetaDataNode.getViewVersionsNode(databaseName, schemaName, viewName)); + String nextActiveVersion = versions.isEmpty() ? DEFAULT_VERSION : String.valueOf(Integer.parseInt(versions.get(0)) + 1); + repository.persist(DatabaseMetaDataNode.getViewVersionNode(databaseName, schemaName, viewName, nextActiveVersion), + YamlEngine.marshal(new YamlViewSwapper().swapToYamlConfiguration(entry.getValue()))); + if (Strings.isNullOrEmpty(getActiveVersion(databaseName, schemaName, viewName))) { + repository.persist(DatabaseMetaDataNode.getViewActiveVersionNode(databaseName, schemaName, viewName), DEFAULT_VERSION); + } + result.add(new MetaDataVersion(DatabaseMetaDataNode.getViewNode(databaseName, schemaName, viewName), getActiveVersion(databaseName, schemaName, viewName), nextActiveVersion)); + } + return result; + } + + private String getActiveVersion(final String databaseName, final String schemaName, final String viewName) { + return repository.getDirectly(DatabaseMetaDataNode.getViewActiveVersionNode(databaseName, schemaName, viewName)); } @Override @@ -58,25 +86,25 @@ public Map load(final String databaseName, final Str return viewNames.isEmpty() ? Collections.emptyMap() : getViewMetaDataByViewNames(databaseName, schemaName, viewNames); } - // TODO Remove this when metadata structure adjustment completed. #25485 - @Override - public Map load(final String databaseName, final String schemaName, final String tableName) { - return Collections.emptyMap(); - } - @Override - public void delete(final String databaseName, final String schemaName, final String viewName) { - repository.delete(DatabaseMetaDataNode.getViewMetaDataPath(databaseName, schemaName, viewName.toLowerCase())); + public Map load(final String databaseName, final String schemaName, final String viewName) { + return getViewMetaDataByViewNames(databaseName, schemaName, Collections.singletonList(viewName)); } private Map getViewMetaDataByViewNames(final String databaseName, final String schemaName, final Collection viewNames) { Map result = new LinkedHashMap<>(viewNames.size(), 1F); viewNames.forEach(each -> { - String view = repository.getDirectly(DatabaseMetaDataNode.getViewMetaDataPath(databaseName, schemaName, each)); + String view = repository.getDirectly(DatabaseMetaDataNode.getViewVersionNode(databaseName, schemaName, each, + repository.getDirectly(DatabaseMetaDataNode.getViewActiveVersionNode(databaseName, schemaName, each)))); if (!Strings.isNullOrEmpty(view)) { result.put(each.toLowerCase(), new YamlViewSwapper().swapToObject(YamlEngine.unmarshal(view, YamlShardingSphereView.class))); } }); return result; } + + @Override + public void delete(final String databaseName, final String schemaName, final String viewName) { + repository.delete(DatabaseMetaDataNode.getViewNode(databaseName, schemaName, viewName.toLowerCase())); + } } diff --git a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/version/MetaDataVersionPersistService.java b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/version/MetaDataVersionPersistService.java index 7902ee961e9e3..b923016b201a2 100644 --- a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/version/MetaDataVersionPersistService.java +++ b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/version/MetaDataVersionPersistService.java @@ -19,7 +19,7 @@ import lombok.RequiredArgsConstructor; import org.apache.shardingsphere.infra.metadata.version.MetaDataVersion; -import org.apache.shardingsphere.metadata.persist.node.NewDatabaseMetaDataNode; +import org.apache.shardingsphere.metadata.persist.node.DatabaseMetaDataNode; import org.apache.shardingsphere.mode.identifier.NodePathTransactionAware; import org.apache.shardingsphere.mode.identifier.NodePathTransactionOperation; import org.apache.shardingsphere.mode.spi.PersistRepository; @@ -85,6 +85,6 @@ public String getActiveVersionByFullPath(final String fullPath) { @Override public String getVersionPathByActiveVersion(final String path, final String activeVersion) { - return repository.getDirectly(NewDatabaseMetaDataNode.getVersionNodeByActiveVersionPath(path, activeVersion)); + return repository.getDirectly(DatabaseMetaDataNode.getVersionNodeByActiveVersionPath(path, activeVersion)); } } diff --git a/kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/node/DatabaseMetaDataNodeTest.java b/kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/node/DatabaseMetaDataNodeTest.java index 83836fe3b5e0e..645e1cad74b2b 100644 --- a/kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/node/DatabaseMetaDataNodeTest.java +++ b/kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/node/DatabaseMetaDataNodeTest.java @@ -17,7 +17,6 @@ package org.apache.shardingsphere.metadata.persist.node; -import org.apache.shardingsphere.infra.database.core.DefaultDatabase; import org.junit.jupiter.api.Test; import java.util.Optional; @@ -29,120 +28,167 @@ class DatabaseMetaDataNodeTest { @Test - void assertGetRulePath() { - assertThat(DatabaseMetaDataNode.getRulePath(DefaultDatabase.LOGIC_NAME, "0"), is("/metadata/logic_db/versions/0/rules")); + void assertIsDataSourcesNode() { + assertTrue(DatabaseMetaDataNode.isDataSourcesNode("/metadata/logic_db/data_sources/foo_ds")); } @Test - void assertGetDatabaseName() { - Optional actualSchemaName = DatabaseMetaDataNode.getDatabaseName("/metadata/logic_db"); - assertTrue(actualSchemaName.isPresent()); - assertThat(actualSchemaName.get(), is("logic_db")); + void assertGetDataSourceNameByDataSourceUnitNode() { + Optional actual = DatabaseMetaDataNode.getDataSourceNameByDataSourceUnitNode("/metadata/logic_db/data_sources/units/foo_ds/versions/0"); + assertTrue(actual.isPresent()); + assertThat(actual.get(), is("foo_ds")); } @Test - void assertGetDatabaseNameWithLine() { - Optional actualSchemaName = DatabaseMetaDataNode.getDatabaseNameByDatabasePath("/metadata/logic-db-test/schemas/logic-db-schema"); - assertTrue(actualSchemaName.isPresent()); - assertThat(actualSchemaName.get(), is("logic-db-test")); + void assertIsDataSourceUnitActiveVersionNode() { + assertTrue(DatabaseMetaDataNode.isDataSourceUnitActiveVersionNode("/metadata/logic_db/data_sources/units/foo_ds/active_version")); } @Test - void assertGetDatabaseNamePath() { - assertThat(DatabaseMetaDataNode.getDatabaseNamePath("sharding_db"), is("/metadata/sharding_db")); + void assertIsDataSourceNodeActiveVersionNode() { + assertTrue(DatabaseMetaDataNode.isDataSourceNodeActiveVersionNode("/metadata/logic_db/data_sources/nodes/foo_ds/active_version")); } @Test - void assertGetMetaDataTablesPath() { - assertThat(DatabaseMetaDataNode.getMetaDataTablesPath("sharding_db", "sharding_schema"), is("/metadata/sharding_db/schemas/sharding_schema/tables")); + void assertGetDatabaseName() { + Optional actual = DatabaseMetaDataNode.getDatabaseName("/metadata/foo_db"); + assertTrue(actual.isPresent()); + assertThat(actual.get(), is("foo_db")); } @Test - void assertGetMetaDataViewsPath() { - assertThat(DatabaseMetaDataNode.getMetaDataViewsPath("sharding_db", "sharding_schema"), is("/metadata/sharding_db/schemas/sharding_schema/views")); + void assertGetDatabaseNameBySchemaNode() { + Optional actual = DatabaseMetaDataNode.getDatabaseNameBySchemaNode("/metadata/foo_db/schemas/foo_schema"); + assertTrue(actual.isPresent()); + assertThat(actual.get(), is("foo_db")); } @Test - void assertGetDatabaseNameByDatabasePath() { - Optional actualSchemaName = DatabaseMetaDataNode.getDatabaseNameByDatabasePath("/metadata/logic_db/schemas/logic_schema"); - assertTrue(actualSchemaName.isPresent()); - assertThat(actualSchemaName.get(), is("logic_db")); + void assertGetSchemaName() { + Optional actual = DatabaseMetaDataNode.getSchemaName("/metadata/foo_db/schemas/foo_schema"); + assertTrue(actual.isPresent()); + assertThat(actual.get(), is("foo_schema")); + } + + @Test + void assertGetSchemaNameByTableNode() { + Optional actual = DatabaseMetaDataNode.getSchemaNameByTableNode("/metadata/foo_db/schemas/foo_schema/tables"); + assertTrue(actual.isPresent()); + assertThat(actual.get(), is("foo_schema")); } @Test void assertGetTableName() { - Optional actualTableName = DatabaseMetaDataNode.getTableName("/metadata/logic_db/schemas/logic_schema/tables/t_order"); - assertTrue(actualTableName.isPresent()); - assertThat(actualTableName.get(), is("t_order")); + Optional actual = DatabaseMetaDataNode.getTableName("/metadata/foo_db/schemas/foo_schema/tables/foo_table/active_version"); + assertTrue(actual.isPresent()); + assertThat(actual.get(), is("foo_table")); + } + + @Test + void assertIsTableActiveVersionNode() { + assertTrue(DatabaseMetaDataNode.isTableActiveVersionNode("/metadata/foo_db/schemas/foo_schema/tables/foo_table/active_version")); } @Test void assertGetViewName() { - Optional actualViewName = DatabaseMetaDataNode.getViewName("/metadata/logic_db/schemas/logic_schema/views/foo_view"); - assertTrue(actualViewName.isPresent()); - assertThat(actualViewName.get(), is("foo_view")); + Optional actual = DatabaseMetaDataNode.getViewName("/metadata/foo_db/schemas/foo_schema/views/foo_view"); + assertTrue(actual.isPresent()); + assertThat(actual.get(), is("foo_view")); } @Test - void assertGetSchemaName() { - Optional actualSchemaName = DatabaseMetaDataNode.getSchemaName("/metadata/logic_db/schemas/logic_schema"); - assertTrue(actualSchemaName.isPresent()); - assertThat(actualSchemaName.get(), is("logic_schema")); + void assertIsViewActiveVersionNode() { + assertTrue(DatabaseMetaDataNode.isViewActiveVersionNode("/metadata/foo_db/schemas/foo_schema/views/foo_view/active_version")); + } + + @Test + void assertGetVersionNodeByActiveVersionPath() { + assertThat(DatabaseMetaDataNode.getVersionNodeByActiveVersionPath("/metadata/foo_db/schemas/foo_schema/views/foo_view/active_version", "0"), + is("/metadata/foo_db/schemas/foo_schema/views/foo_view/versions/0")); + } + + @Test + void assertGetMetaDataDataSourcesNode() { + assertThat(DatabaseMetaDataNode.getDataSourceUnitsNode("foo_db"), is("/metadata/foo_db/data_sources/units")); + } + + @Test + void assertGetMetaDataDataSourceNode() { + assertThat(DatabaseMetaDataNode.getDataSourceUnitNode("foo_db", "foo_ds"), is("/metadata/foo_db/data_sources/units/foo_ds")); + } + + @Test + void assertGetDataSourceUnitNodeWithVersion() { + assertThat(DatabaseMetaDataNode.getDataSourceUnitNodeWithVersion("foo_db", "foo_ds", "0"), is("/metadata/foo_db/data_sources/units/foo_ds/versions/0")); + } + + @Test + void assertGetDataSourceUnitVersionsNode() { + assertThat(DatabaseMetaDataNode.getDataSourceUnitVersionsNode("foo_db", "foo_ds"), is("/metadata/foo_db/data_sources/units/foo_ds/versions")); + } + + @Test + void assertGetDataSourceUnitActiveVersionNode() { + assertThat(DatabaseMetaDataNode.getDataSourceUnitActiveVersionNode("foo_db", "foo_ds"), is("/metadata/foo_db/data_sources/units/foo_ds/active_version")); + } + + @Test + void assertGetDatabaseRuleActiveVersionNode() { + assertThat(DatabaseMetaDataNode.getDatabaseRuleActiveVersionNode("foo_db", "foo_rule", "foo_tables"), is("/metadata/foo_db/rules/foo_rule/foo_tables/active_version")); + } + + @Test + void assertGetDatabaseRuleVersionsNode() { + assertThat(DatabaseMetaDataNode.getDatabaseRuleVersionsNode("foo_db", "sharding", "foo_key"), is("/metadata/foo_db/rules/sharding/foo_key/versions")); } @Test - void assertGetVersionByDataSourceUnitsPath() { - Optional actualVersion = DatabaseMetaDataNode.getVersionByDataSourceUnitsPath("/metadata/logic_db/versions/0/data_sources/units"); - assertTrue(actualVersion.isPresent()); - assertThat(actualVersion.get(), is("0")); + void assertGetDatabaseRuleVersionNode() { + assertThat(DatabaseMetaDataNode.getDatabaseRuleVersionNode("foo_db", "foo_rule", "foo_tables", "1"), is("/metadata/foo_db/rules/foo_rule/foo_tables/versions/1")); } @Test - void assertGetVersionByDataSourceNodesPath() { - Optional actualVersion = DatabaseMetaDataNode.getVersionByDataSourceNodesPath("/metadata/logic_db/versions/0/data_sources/nodes"); - assertTrue(actualVersion.isPresent()); - assertThat(actualVersion.get(), is("0")); + void assertGetTableActiveVersionNode() { + assertThat(DatabaseMetaDataNode.getTableActiveVersionNode("foo_db", "foo_schema", "foo_table"), is("/metadata/foo_db/schemas/foo_schema/tables/foo_table/active_version")); } @Test - void assertGetActiveVersionPath() { - assertThat(DatabaseMetaDataNode.getActiveVersionPath("logic_db"), is("/metadata/logic_db/active_version")); + void assertGetTableVersionsNode() { + assertThat(DatabaseMetaDataNode.getTableVersionsNode("foo_db", "foo_schema", "foo_table"), is("/metadata/foo_db/schemas/foo_schema/tables/foo_table/versions")); } @Test - void assertGetVersionByRulesPath() { - Optional actualVersion = DatabaseMetaDataNode.getVersionByRulesPath("/metadata/logic_db/versions/0/rules"); - assertTrue(actualVersion.isPresent()); - assertThat(actualVersion.get(), is("0")); + void assertGetTableVersionNode() { + assertThat(DatabaseMetaDataNode.getTableVersionNode("foo_db", "foo_schema", "foo_table", "0"), is("/metadata/foo_db/schemas/foo_schema/tables/foo_table/versions/0")); } @Test - void assertGetDatabaseVersionPath() { - assertThat(DatabaseMetaDataNode.getDatabaseVersionPath("logic_db", "0"), is("/metadata/logic_db/versions/0")); + void assertGetTableNode() { + assertThat(DatabaseMetaDataNode.getTableNode("foo_db", "foo_schema", "foo_table"), is("/metadata/foo_db/schemas/foo_schema/tables/foo_table")); } @Test - void assertGetTableMetaDataPath() { - assertThat(DatabaseMetaDataNode.getTableMetaDataPath("logic_db", "logic_schema", "order"), is("/metadata/logic_db/schemas/logic_schema/tables/order")); + void assertGetViewActiveVersionNode() { + assertThat(DatabaseMetaDataNode.getViewActiveVersionNode("foo_db", "foo_schema", "foo_view"), is("/metadata/foo_db/schemas/foo_schema/views/foo_view/active_version")); } @Test - void assertGetViewMetaDataPath() { - assertThat(DatabaseMetaDataNode.getViewMetaDataPath("logic_db", "logic_schema", "order_view"), is("/metadata/logic_db/schemas/logic_schema/views/order_view")); + void assertGetViewVersionsNode() { + assertThat(DatabaseMetaDataNode.getViewVersionsNode("foo_db", "foo_schema", "foo_view"), is("/metadata/foo_db/schemas/foo_schema/views/foo_view/versions")); } @Test - void assertGetMetaDataNodePath() { - assertThat(DatabaseMetaDataNode.getMetaDataNodePath(), is("/metadata")); + void assertGetViewVersionNode() { + assertThat(DatabaseMetaDataNode.getViewVersionNode("foo_db", "foo_schema", "foo_view", "0"), is("/metadata/foo_db/schemas/foo_schema/views/foo_view/versions/0")); } @Test - void assertGetMetaDataDataSourceNodesPath() { - assertThat(DatabaseMetaDataNode.getMetaDataDataSourceNodesPath(DefaultDatabase.LOGIC_NAME, "0"), is("/metadata/logic_db/versions/0/data_sources/nodes")); + void assertGetViewNode() { + assertThat(DatabaseMetaDataNode.getViewNode("foo_db", "foo_schema", "foo_view"), is("/metadata/foo_db/schemas/foo_schema/views/foo_view")); } @Test - void assertGetMetaDataDataSourceUnitsPath() { - assertThat(DatabaseMetaDataNode.getMetaDataDataSourceUnitsPath(DefaultDatabase.LOGIC_NAME, "0"), is("/metadata/logic_db/versions/0/data_sources/units")); + void assertGetDataSourceNodeVersionsNode() { + assertThat(DatabaseMetaDataNode.getDataSourceNodeVersionsNode("foo_db", "foo_ds"), is("/metadata/foo_db/data_sources/nodes/foo_ds/versions")); } } diff --git a/kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/node/GlobalNodeTest.java b/kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/node/GlobalNodeTest.java index e0f13a1049332..8f08a6037fef8 100644 --- a/kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/node/GlobalNodeTest.java +++ b/kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/node/GlobalNodeTest.java @@ -25,12 +25,32 @@ class GlobalNodeTest { @Test - void assertGetGlobalRuleNodePath() { - assertThat(GlobalNode.getGlobalRuleNode(), is("/rules")); + void assertGetGlobalRuleRootNode() { + assertThat(GlobalNode.getGlobalRuleRootNode(), is("/rules")); } @Test - void assertGetPropsPath() { - assertThat(GlobalNode.getPropsPath(), is("/props")); + void assertGetPropsActiveVersionNode() { + assertThat(GlobalNode.getPropsActiveVersionNode(), is("/props/active_version")); + } + + @Test + void assertGetPropsVersionNode() { + assertThat(GlobalNode.getPropsVersionNode("0"), is("/props/versions/0")); + } + + @Test + void assertGetGlobalRuleActiveVersionNode() { + assertThat(GlobalNode.getGlobalRuleActiveVersionNode("transaction"), is("/rules/transaction/active_version")); + } + + @Test + void assertGetGlobalRuleVersionsNode() { + assertThat(GlobalNode.getGlobalRuleVersionsNode("transaction"), is("/rules/transaction/versions")); + } + + @Test + void assertGetGlobalRuleVersionNode() { + assertThat(GlobalNode.getGlobalRuleVersionNode("transaction", "0"), is("/rules/transaction/versions/0")); } } diff --git a/kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/node/NewDatabaseMetaDataNodeTest.java b/kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/node/NewDatabaseMetaDataNodeTest.java deleted file mode 100644 index 85b07c73315ac..0000000000000 --- a/kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/node/NewDatabaseMetaDataNodeTest.java +++ /dev/null @@ -1,195 +0,0 @@ -/* - * 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.metadata.persist.node; - -import org.junit.jupiter.api.Test; - -import java.util.Optional; - -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.junit.jupiter.api.Assertions.assertTrue; - -// TODO Rename DatabaseMetaDataNodeTest when metadata structure adjustment completed. #25485 -class NewDatabaseMetaDataNodeTest { - - @Test - void assertIsDataSourcesNode() { - assertTrue(NewDatabaseMetaDataNode.isDataSourcesNode("/metadata/logic_db/data_sources/foo_ds")); - } - - @Test - void assertGetDataSourceNameByDataSourceUnitNode() { - Optional actual = NewDatabaseMetaDataNode.getDataSourceNameByDataSourceUnitNode("/metadata/logic_db/data_sources/units/foo_ds/versions/0"); - assertTrue(actual.isPresent()); - assertThat(actual.get(), is("foo_ds")); - } - - @Test - void assertIsDataSourceUnitActiveVersionNode() { - assertTrue(NewDatabaseMetaDataNode.isDataSourceUnitActiveVersionNode("/metadata/logic_db/data_sources/units/foo_ds/active_version")); - } - - @Test - void assertIsDataSourceNodeActiveVersionNode() { - assertTrue(NewDatabaseMetaDataNode.isDataSourceNodeActiveVersionNode("/metadata/logic_db/data_sources/nodes/foo_ds/active_version")); - } - - @Test - void assertGetDatabaseName() { - Optional actual = NewDatabaseMetaDataNode.getDatabaseName("/metadata/foo_db"); - assertTrue(actual.isPresent()); - assertThat(actual.get(), is("foo_db")); - } - - @Test - void assertGetDatabaseNameBySchemaNode() { - Optional actual = NewDatabaseMetaDataNode.getDatabaseNameBySchemaNode("/metadata/foo_db/schemas/foo_schema"); - assertTrue(actual.isPresent()); - assertThat(actual.get(), is("foo_db")); - } - - @Test - void assertGetSchemaName() { - Optional actual = NewDatabaseMetaDataNode.getSchemaName("/metadata/foo_db/schemas/foo_schema"); - assertTrue(actual.isPresent()); - assertThat(actual.get(), is("foo_schema")); - } - - @Test - void assertGetSchemaNameByTableNode() { - Optional actual = NewDatabaseMetaDataNode.getSchemaNameByTableNode("/metadata/foo_db/schemas/foo_schema/tables"); - assertTrue(actual.isPresent()); - assertThat(actual.get(), is("foo_schema")); - } - - @Test - void assertGetTableName() { - Optional actual = NewDatabaseMetaDataNode.getTableName("/metadata/foo_db/schemas/foo_schema/tables/foo_table/active_version"); - assertTrue(actual.isPresent()); - assertThat(actual.get(), is("foo_table")); - } - - @Test - void assertIsTableActiveVersionNode() { - assertTrue(NewDatabaseMetaDataNode.isTableActiveVersionNode("/metadata/foo_db/schemas/foo_schema/tables/foo_table/active_version")); - } - - @Test - void assertGetViewName() { - Optional actual = NewDatabaseMetaDataNode.getViewName("/metadata/foo_db/schemas/foo_schema/views/foo_view"); - assertTrue(actual.isPresent()); - assertThat(actual.get(), is("foo_view")); - } - - @Test - void assertIsViewActiveVersionNode() { - assertTrue(NewDatabaseMetaDataNode.isViewActiveVersionNode("/metadata/foo_db/schemas/foo_schema/views/foo_view/active_version")); - } - - @Test - void assertGetVersionNodeByActiveVersionPath() { - assertThat(NewDatabaseMetaDataNode.getVersionNodeByActiveVersionPath("/metadata/foo_db/schemas/foo_schema/views/foo_view/active_version", "0"), - is("/metadata/foo_db/schemas/foo_schema/views/foo_view/versions/0")); - } - - @Test - void assertGetMetaDataDataSourcesNode() { - assertThat(NewDatabaseMetaDataNode.getDataSourceUnitsNode("foo_db"), is("/metadata/foo_db/data_sources/units")); - } - - @Test - void assertGetMetaDataDataSourceNode() { - assertThat(NewDatabaseMetaDataNode.getDataSourceUnitNode("foo_db", "foo_ds"), is("/metadata/foo_db/data_sources/units/foo_ds")); - } - - @Test - void assertGetDataSourceUnitNodeWithVersion() { - assertThat(NewDatabaseMetaDataNode.getDataSourceUnitNodeWithVersion("foo_db", "foo_ds", "0"), is("/metadata/foo_db/data_sources/units/foo_ds/versions/0")); - } - - @Test - void assertGetDataSourceUnitVersionsNode() { - assertThat(NewDatabaseMetaDataNode.getDataSourceUnitVersionsNode("foo_db", "foo_ds"), is("/metadata/foo_db/data_sources/units/foo_ds/versions")); - } - - @Test - void assertGetDataSourceUnitActiveVersionNode() { - assertThat(NewDatabaseMetaDataNode.getDataSourceUnitActiveVersionNode("foo_db", "foo_ds"), is("/metadata/foo_db/data_sources/units/foo_ds/active_version")); - } - - @Test - void assertGetDatabaseRuleActiveVersionNode() { - assertThat(NewDatabaseMetaDataNode.getDatabaseRuleActiveVersionNode("foo_db", "foo_rule", "foo_tables"), is("/metadata/foo_db/rules/foo_rule/foo_tables/active_version")); - } - - @Test - void assertGetDatabaseRuleVersionsNode() { - assertThat(NewDatabaseMetaDataNode.getDatabaseRuleVersionsNode("foo_db", "sharding", "foo_key"), is("/metadata/foo_db/rules/sharding/foo_key/versions")); - } - - @Test - void assertGetDatabaseRuleVersionNode() { - assertThat(NewDatabaseMetaDataNode.getDatabaseRuleVersionNode("foo_db", "foo_rule", "foo_tables", "1"), is("/metadata/foo_db/rules/foo_rule/foo_tables/versions/1")); - } - - @Test - void assertGetTableActiveVersionNode() { - assertThat(NewDatabaseMetaDataNode.getTableActiveVersionNode("foo_db", "foo_schema", "foo_table"), is("/metadata/foo_db/schemas/foo_schema/tables/foo_table/active_version")); - } - - @Test - void assertGetTableVersionsNode() { - assertThat(NewDatabaseMetaDataNode.getTableVersionsNode("foo_db", "foo_schema", "foo_table"), is("/metadata/foo_db/schemas/foo_schema/tables/foo_table/versions")); - } - - @Test - void assertGetTableVersionNode() { - assertThat(NewDatabaseMetaDataNode.getTableVersionNode("foo_db", "foo_schema", "foo_table", "0"), is("/metadata/foo_db/schemas/foo_schema/tables/foo_table/versions/0")); - } - - @Test - void assertGetTableNode() { - assertThat(NewDatabaseMetaDataNode.getTableNode("foo_db", "foo_schema", "foo_table"), is("/metadata/foo_db/schemas/foo_schema/tables/foo_table")); - } - - @Test - void assertGetViewActiveVersionNode() { - assertThat(NewDatabaseMetaDataNode.getViewActiveVersionNode("foo_db", "foo_schema", "foo_view"), is("/metadata/foo_db/schemas/foo_schema/views/foo_view/active_version")); - } - - @Test - void assertGetViewVersionsNode() { - assertThat(NewDatabaseMetaDataNode.getViewVersionsNode("foo_db", "foo_schema", "foo_view"), is("/metadata/foo_db/schemas/foo_schema/views/foo_view/versions")); - } - - @Test - void assertGetViewVersionNode() { - assertThat(NewDatabaseMetaDataNode.getViewVersionNode("foo_db", "foo_schema", "foo_view", "0"), is("/metadata/foo_db/schemas/foo_schema/views/foo_view/versions/0")); - } - - @Test - void assertGetViewNode() { - assertThat(NewDatabaseMetaDataNode.getViewNode("foo_db", "foo_schema", "foo_view"), is("/metadata/foo_db/schemas/foo_schema/views/foo_view")); - } - - @Test - void assertGetDataSourceNodeVersionsNode() { - assertThat(NewDatabaseMetaDataNode.getDataSourceNodeVersionsNode("foo_db", "foo_ds"), is("/metadata/foo_db/data_sources/nodes/foo_ds/versions")); - } -} diff --git a/kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/node/NewGlobalNodeTest.java b/kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/node/NewGlobalNodeTest.java deleted file mode 100644 index 0426017c6e34e..0000000000000 --- a/kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/node/NewGlobalNodeTest.java +++ /dev/null @@ -1,57 +0,0 @@ -/* - * 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.metadata.persist.node; - -import org.junit.jupiter.api.Test; - -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.MatcherAssert.assertThat; - -// TODO Rename GlobalNodeTest when metadata structure adjustment completed. #25485 -class NewGlobalNodeTest { - - @Test - void assertGetGlobalRuleRootNode() { - assertThat(NewGlobalNode.getGlobalRuleRootNode(), is("/rules")); - } - - @Test - void assertGetPropsActiveVersionNode() { - assertThat(NewGlobalNode.getPropsActiveVersionNode(), is("/props/active_version")); - } - - @Test - void assertGetPropsVersionNode() { - assertThat(NewGlobalNode.getPropsVersionNode("0"), is("/props/versions/0")); - } - - @Test - void assertGetGlobalRuleActiveVersionNode() { - assertThat(NewGlobalNode.getGlobalRuleActiveVersionNode("transaction"), is("/rules/transaction/active_version")); - } - - @Test - void assertGetGlobalRuleVersionsNode() { - assertThat(NewGlobalNode.getGlobalRuleVersionsNode("transaction"), is("/rules/transaction/versions")); - } - - @Test - void assertGetGlobalRuleVersionNode() { - assertThat(NewGlobalNode.getGlobalRuleVersionNode("transaction", "0"), is("/rules/transaction/versions/0")); - } -} diff --git a/kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/service/DatabaseMetaDataPersistServiceTest.java b/kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/service/DatabaseMetaDataPersistServiceTest.java deleted file mode 100644 index 1f5fd3970fa10..0000000000000 --- a/kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/service/DatabaseMetaDataPersistServiceTest.java +++ /dev/null @@ -1,141 +0,0 @@ -/* - * 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.metadata.persist.service; - -import lombok.SneakyThrows; -import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema; -import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereTable; -import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereView; -import org.apache.shardingsphere.infra.util.yaml.YamlEngine; -import org.apache.shardingsphere.infra.yaml.schema.pojo.YamlShardingSphereTable; -import org.apache.shardingsphere.infra.yaml.schema.swapper.YamlTableSwapper; -import org.apache.shardingsphere.metadata.persist.service.database.DatabaseMetaDataPersistService; -import org.apache.shardingsphere.mode.spi.PersistRepository; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.Mock; -import org.mockito.junit.jupiter.MockitoExtension; - -import java.io.IOException; -import java.net.URISyntaxException; -import java.nio.file.Files; -import java.nio.file.Paths; -import java.util.Collection; -import java.util.Collections; -import java.util.Map; -import java.util.stream.Collectors; - -import static org.hamcrest.CoreMatchers.hasItems; -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.mockito.ArgumentMatchers.anyString; -import static org.mockito.ArgumentMatchers.eq; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -@ExtendWith(MockitoExtension.class) -class DatabaseMetaDataPersistServiceTest { - - @Mock - private PersistRepository repository; - - @Test - void assertPersistEmptySchemas() { - new DatabaseMetaDataPersistService(repository).persist("foo_db", "foo_schema", new ShardingSphereSchema()); - verify(repository).persist(eq("/metadata/foo_db/schemas/foo_schema/tables"), anyString()); - } - - @Test - void assertCompareAndPersistEmptySchemas() { - new DatabaseMetaDataPersistService(repository).compareAndPersist("foo_db", "foo_schema", new ShardingSphereSchema()); - verify(repository).persist(eq("/metadata/foo_db/schemas/foo_schema/tables"), anyString()); - } - - @Test - void assertPersist() { - ShardingSphereTable table = new YamlTableSwapper().swapToObject(YamlEngine.unmarshal(readYAML(), YamlShardingSphereTable.class)); - ShardingSphereSchema schema = new ShardingSphereSchema(); - schema.getTables().put("t_order", table); - new DatabaseMetaDataPersistService(repository).persist("foo_db", "foo_schema", schema); - verify(repository).persist(eq("/metadata/foo_db/schemas/foo_schema/tables/t_order"), anyString()); - } - - @Test - void assertAddDatabase() { - new DatabaseMetaDataPersistService(repository).addDatabase("foo_db"); - verify(repository).persist("/metadata/foo_db", ""); - } - - @Test - void assertDropDatabase() { - new DatabaseMetaDataPersistService(repository).dropDatabase("foo_db"); - verify(repository).delete("/metadata/foo_db"); - } - - @Test - void assertLoadAllDatabaseNames() { - when(repository.getChildrenKeys("/metadata")).thenReturn(Collections.singletonList("foo_db")); - Collection actual = new DatabaseMetaDataPersistService(repository).loadAllDatabaseNames(); - assertThat(actual.size(), is(1)); - assertThat(actual, hasItems("foo_db")); - } - - @Test - void assertAddSchema() { - new DatabaseMetaDataPersistService(repository).addSchema("foo_db", "foo_schema"); - verify(repository).persist("/metadata/foo_db/schemas/foo_schema/tables", ""); - } - - @Test - void assertDropSchema() { - new DatabaseMetaDataPersistService(repository).dropSchema("foo_db", "foo_schema"); - verify(repository).delete("/metadata/foo_db/schemas/foo_schema"); - } - - @Test - void assertPersistSchemaMetaData() { - ShardingSphereTable table = new ShardingSphereTable("FOO_TABLE", Collections.emptyList(), Collections.emptyList(), Collections.emptyList()); - ShardingSphereView view = new ShardingSphereView("FOO_VIEW", "select id from foo_table"); - new DatabaseMetaDataPersistService(repository).persist("foo_db", "foo_schema", - new ShardingSphereSchema(Collections.singletonMap("FOO_TABLE", table), Collections.singletonMap("FOO_VIEW", view))); - verify(repository).persist(eq("/metadata/foo_db/schemas/foo_schema/tables/foo_table"), anyString()); - } - - @Test - void assertLoadSchemas() { - DatabaseMetaDataPersistService databaseMetaDataPersistService = new DatabaseMetaDataPersistService(repository); - when(repository.getChildrenKeys("/metadata/foo_db/schemas")).thenReturn(Collections.singletonList("foo_schema")); - when(repository.getChildrenKeys("/metadata/foo_db/schemas/foo_schema/tables")).thenReturn(Collections.singletonList("t_order")); - when(repository.getDirectly("/metadata/foo_db/schemas/foo_schema/tables/t_order")).thenReturn(readYAML()); - Map schema = databaseMetaDataPersistService.loadSchemas("foo_db"); - assertThat(schema.size(), is(1)); - assertTrue(databaseMetaDataPersistService.loadSchemas("test").isEmpty()); - assertThat(schema.get("foo_schema").getAllTableNames(), is(Collections.singleton("t_order"))); - assertThat(schema.get("foo_schema").getTable("t_order").getIndexValues().size(), is(1)); - assertThat(schema.get("foo_schema").getTable("t_order").getIndexValues().iterator().next().getName(), is("PRIMARY")); - assertThat(schema.get("foo_schema").getAllColumnNames("t_order").size(), is(1)); - assertThat(schema.get("foo_schema").getTable("t_order").getColumnValues().size(), is(1)); - assertThat(schema.get("foo_schema").getTable("t_order").getColumnValues().iterator().next().getName(), is("id")); - } - - @SneakyThrows({IOException.class, URISyntaxException.class}) - private String readYAML() { - return Files.readAllLines(Paths.get(ClassLoader.getSystemResource("yaml/schema/table.yaml").toURI())).stream().map(each -> each + System.lineSeparator()).collect(Collectors.joining()); - } -} diff --git a/kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/service/MetaDataVersionPersistServiceTest.java b/kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/service/MetaDataVersionPersistServiceTest.java deleted file mode 100644 index d157f23a60462..0000000000000 --- a/kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/service/MetaDataVersionPersistServiceTest.java +++ /dev/null @@ -1,48 +0,0 @@ -/* - * 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.metadata.persist.service; - -import org.apache.shardingsphere.infra.metadata.version.MetaDataVersion; -import org.apache.shardingsphere.metadata.persist.service.version.MetaDataVersionPersistService; -import org.apache.shardingsphere.mode.spi.PersistRepository; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; - -import java.util.Collections; - -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; - -class MetaDataVersionPersistServiceTest { - - private PersistRepository repository; - - private MetaDataVersionPersistService metaDataVersionPersistService; - - @BeforeEach - void setUp() { - repository = mock(PersistRepository.class); - metaDataVersionPersistService = new MetaDataVersionPersistService(repository); - } - - @Test - void assertSwitchActiveVersion() { - metaDataVersionPersistService.switchActiveVersion(Collections.singletonList(new MetaDataVersion("foo_key", "0", "1"))); - verify(repository).persist("foo_key/active_version", "1"); - } -} diff --git a/kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/service/config/database/DataSourceUnitPersistServiceTest.java b/kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/service/config/database/DataSourceUnitPersistServiceTest.java deleted file mode 100644 index eb502feb2b879..0000000000000 --- a/kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/service/config/database/DataSourceUnitPersistServiceTest.java +++ /dev/null @@ -1,100 +0,0 @@ -/* - * 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.metadata.persist.service.config.database; - -import lombok.SneakyThrows; -import org.apache.shardingsphere.infra.datasource.pool.props.domain.DataSourcePoolProperties; -import org.apache.shardingsphere.infra.datasource.pool.props.creator.DataSourcePoolPropertiesCreator; -import org.apache.shardingsphere.metadata.persist.service.config.database.datasource.DataSourceUnitPersistService; -import org.apache.shardingsphere.mode.spi.PersistRepository; -import org.apache.shardingsphere.test.fixture.jdbc.MockedDataSource; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.Mock; -import org.mockito.junit.jupiter.MockitoExtension; - -import javax.sql.DataSource; -import java.io.IOException; -import java.net.URISyntaxException; -import java.nio.file.Files; -import java.nio.file.Paths; -import java.util.Arrays; -import java.util.Collections; -import java.util.Map; -import java.util.stream.Collectors; - -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -@ExtendWith(MockitoExtension.class) -class DataSourceUnitPersistServiceTest { - - @Mock - private PersistRepository repository; - - @Test - void assertLoad() { - when(repository.getDirectly("/metadata/foo_db/active_version")).thenReturn("0"); - when(repository.getDirectly("/metadata/foo_db/versions/0/data_sources/units")).thenReturn(readDataSourceYaml("yaml/persist/data-source.yaml")); - Map actual = new DataSourceUnitPersistService(repository).load("foo_db"); - assertThat(actual.size(), is(2)); - assertDataSourcePoolProperties(actual.get("ds_0"), DataSourcePoolPropertiesCreator.create(createDataSource("ds_0"))); - assertDataSourcePoolProperties(actual.get("ds_1"), DataSourcePoolPropertiesCreator.create(createDataSource("ds_1"))); - } - - @SneakyThrows({IOException.class, URISyntaxException.class}) - private String readDataSourceYaml(final String path) { - return Files.readAllLines(Paths.get(ClassLoader.getSystemResource(path).toURI())) - .stream().filter(each -> !"".equals(each.trim()) && !each.startsWith("#")).map(each -> each + System.lineSeparator()).collect(Collectors.joining()); - } - - private void assertDataSourcePoolProperties(final DataSourcePoolProperties actual, final DataSourcePoolProperties expected) { - assertThat(actual.getPoolClassName(), is(expected.getPoolClassName())); - assertThat(actual.getAllLocalProperties().get("url"), is(expected.getAllLocalProperties().get("url"))); - assertThat(actual.getAllLocalProperties().get("username"), is(expected.getAllLocalProperties().get("username"))); - assertThat(actual.getAllLocalProperties().get("password"), is(expected.getAllLocalProperties().get("password"))); - assertThat(actual.getAllLocalProperties().get("connectionInitSqls"), is(expected.getAllLocalProperties().get("connectionInitSqls"))); - } - - @Test - void assertLoadWithoutPath() { - when(repository.getDirectly("/metadata/foo_db/active_version")).thenReturn("0"); - Map actual = new DataSourceUnitPersistService(repository).load("foo_db"); - assertTrue(actual.isEmpty()); - } - - @Test - void assertAppend() { - when(repository.getDirectly("/metadata/foo_db/active_version")).thenReturn("0"); - new DataSourceUnitPersistService(repository).append("foo_db", Collections.singletonMap("foo_ds", DataSourcePoolPropertiesCreator.create(createDataSource("foo_ds")))); - String expected = readDataSourceYaml("yaml/persist/data-source-foo.yaml"); - verify(repository).persist("/metadata/foo_db/versions/0/data_sources/units", expected); - } - - private DataSource createDataSource(final String name) { - MockedDataSource result = new MockedDataSource(); - result.setUrl("jdbc:mysql://localhost:3306/" + name); - result.setUsername("root"); - result.setPassword("root"); - result.setConnectionInitSqls(Arrays.asList("set names utf8mb4;", "set names utf8;")); - return result; - } -} diff --git a/kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/service/config/database/DatabaseRulePersistServiceTest.java b/kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/service/config/database/DatabaseRulePersistServiceTest.java deleted file mode 100644 index 30d5c394de293..0000000000000 --- a/kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/service/config/database/DatabaseRulePersistServiceTest.java +++ /dev/null @@ -1,65 +0,0 @@ -/* - * 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.metadata.persist.service.config.database; - -import lombok.SneakyThrows; -import org.apache.shardingsphere.infra.config.rule.RuleConfiguration; -import org.apache.shardingsphere.metadata.persist.service.config.database.rule.DatabaseRulePersistService; -import org.apache.shardingsphere.mode.spi.PersistRepository; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.Mock; -import org.mockito.junit.jupiter.MockitoExtension; - -import java.io.IOException; -import java.net.URISyntaxException; -import java.nio.file.Files; -import java.nio.file.Paths; -import java.util.Collection; -import java.util.stream.Collectors; - -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.mockito.Mockito.when; - -@ExtendWith(MockitoExtension.class) -class DatabaseRulePersistServiceTest { - - @Mock - private PersistRepository repository; - - @Test - void assertLoadWithoutExistedNode() { - assertTrue(new DatabaseRulePersistService(repository).load("foo_db").isEmpty()); - } - - @Test - void assertLoadWithExistedNode() { - when(repository.getDirectly("/metadata/foo_db/active_version")).thenReturn("0"); - when(repository.getDirectly("/metadata/foo_db/versions/0/rules")).thenReturn(readYAML()); - Collection actual = new DatabaseRulePersistService(repository).load("foo_db"); - assertThat(actual.size(), is(1)); - } - - @SneakyThrows({IOException.class, URISyntaxException.class}) - private String readYAML() { - return Files.readAllLines(Paths.get(ClassLoader.getSystemResource("yaml/persist/data-database-rule.yaml").toURI())) - .stream().map(each -> each + System.lineSeparator()).collect(Collectors.joining()); - } -} diff --git a/kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/service/config/global/GlobalRulePersistServiceTest.java b/kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/service/config/global/GlobalRulePersistServiceTest.java deleted file mode 100644 index 5b5bcdf6a5467..0000000000000 --- a/kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/service/config/global/GlobalRulePersistServiceTest.java +++ /dev/null @@ -1,65 +0,0 @@ -/* - * 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.metadata.persist.service.config.global; - -import lombok.SneakyThrows; -import org.apache.shardingsphere.infra.config.rule.RuleConfiguration; -import org.apache.shardingsphere.infra.metadata.user.ShardingSphereUser; -import org.apache.shardingsphere.mode.spi.PersistRepository; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.Mock; -import org.mockito.junit.jupiter.MockitoExtension; - -import java.io.IOException; -import java.net.URISyntaxException; -import java.nio.file.Files; -import java.nio.file.Paths; -import java.util.Collection; -import java.util.stream.Collectors; - -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.mockito.Mockito.when; - -@ExtendWith(MockitoExtension.class) -class GlobalRulePersistServiceTest { - - @Mock - private PersistRepository repository; - - @Test - void assertLoad() { - when(repository.getDirectly("/rules")).thenReturn(readYAML()); - Collection actual = new GlobalRulePersistService(repository).load(); - assertThat(actual.size(), is(1)); - } - - @Test - void assertLoadUsers() { - when(repository.getDirectly("/rules")).thenReturn(readYAML()); - Collection actual = new GlobalRulePersistService(repository).loadUsers(); - assertThat(actual.size(), is(2)); - } - - @SneakyThrows({IOException.class, URISyntaxException.class}) - private String readYAML() { - return Files.readAllLines(Paths.get(ClassLoader.getSystemResource("yaml/persist/data-global-rule.yaml").toURI())) - .stream().map(each -> each + System.lineSeparator()).collect(Collectors.joining()); - } -} diff --git a/kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/service/config/global/PropertiesPersistServiceTest.java b/kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/service/config/global/PropertiesPersistServiceTest.java deleted file mode 100644 index c37c34e4b1755..0000000000000 --- a/kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/service/config/global/PropertiesPersistServiceTest.java +++ /dev/null @@ -1,47 +0,0 @@ -/* - * 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.metadata.persist.service.config.global; - -import org.apache.shardingsphere.infra.config.props.ConfigurationPropertyKey; -import org.apache.shardingsphere.mode.spi.PersistRepository; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.Mock; -import org.mockito.junit.jupiter.MockitoExtension; - -import java.util.Properties; - -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.mockito.Mockito.when; - -@ExtendWith(MockitoExtension.class) -class PropertiesPersistServiceTest { - - private static final String PROPS_YAML = ConfigurationPropertyKey.SQL_SHOW.getKey() + ": false\n"; - - @Mock - private PersistRepository repository; - - @Test - void assertLoad() { - when(repository.getDirectly("/props")).thenReturn(PROPS_YAML); - Properties actual = new PropertiesPersistService(repository).load(); - assertThat(actual.get(ConfigurationPropertyKey.SQL_SHOW.getKey()), is(Boolean.FALSE)); - } -} diff --git a/kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/service/schema/TableMetaDataPersistServiceTest.java b/kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/service/schema/TableMetaDataPersistServiceTest.java deleted file mode 100644 index fd1c5d75ddf0e..0000000000000 --- a/kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/service/schema/TableMetaDataPersistServiceTest.java +++ /dev/null @@ -1,77 +0,0 @@ -/* - * 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.metadata.persist.service.schema; - -import lombok.SneakyThrows; -import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereTable; -import org.apache.shardingsphere.mode.spi.PersistRepository; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.Mock; -import org.mockito.junit.jupiter.MockitoExtension; - -import java.io.IOException; -import java.net.URISyntaxException; -import java.nio.file.Files; -import java.nio.file.Paths; -import java.util.Collections; -import java.util.Map; -import java.util.stream.Collectors; - -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -@ExtendWith(MockitoExtension.class) -class TableMetaDataPersistServiceTest { - - @Mock - private PersistRepository repository; - - @Test - void assertPersist() { - ShardingSphereTable table = new ShardingSphereTable("foo_table", Collections.emptyList(), Collections.emptyList(), Collections.emptyList()); - new TableMetaDataPersistService(repository).persist("foo_db", "foo_schema", Collections.singletonMap("foo_table", table)); - verify(repository).persist("/metadata/foo_db/schemas/foo_schema/tables/foo_table", "name: foo_table" + System.lineSeparator() + "type: TABLE" + System.lineSeparator()); - } - - @Test - void assertLoad() { - TableMetaDataPersistService tableMetaDataPersistService = new TableMetaDataPersistService(repository); - when(repository.getChildrenKeys("/metadata/foo_db/schemas/foo_schema/tables")).thenReturn(Collections.singletonList("t_order")); - when(repository.getDirectly("/metadata/foo_db/schemas/foo_schema/tables/t_order")).thenReturn(readYAML()); - Map tables = tableMetaDataPersistService.load("foo_db", "foo_schema"); - assertThat(tables.size(), is(1)); - assertThat(tables.get("t_order").getIndexValues().size(), is(1)); - assertThat(tables.get("t_order").getIndexValues().iterator().next().getName(), is("PRIMARY")); - assertThat(tables.get("t_order").getColumnValues().size(), is(1)); - assertThat(tables.get("t_order").getColumnValues().iterator().next().getName(), is("id")); - } - - @Test - void assertDelete() { - new TableMetaDataPersistService(repository).delete("foo_db", "foo_schema", "foo_table"); - verify(repository).delete("/metadata/foo_db/schemas/foo_schema/tables/foo_table"); - } - - @SneakyThrows({IOException.class, URISyntaxException.class}) - private String readYAML() { - return Files.readAllLines(Paths.get(ClassLoader.getSystemResource("yaml/schema/table.yaml").toURI())).stream().map(each -> each + System.lineSeparator()).collect(Collectors.joining()); - } -} diff --git a/kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/service/schema/ViewMetaDataPersistServiceTest.java b/kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/service/schema/ViewMetaDataPersistServiceTest.java deleted file mode 100644 index 74038b771b293..0000000000000 --- a/kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/service/schema/ViewMetaDataPersistServiceTest.java +++ /dev/null @@ -1,77 +0,0 @@ -/* - * 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.metadata.persist.service.schema; - -import lombok.SneakyThrows; -import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereView; -import org.apache.shardingsphere.mode.spi.PersistRepository; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.Mock; -import org.mockito.junit.jupiter.MockitoExtension; - -import java.io.IOException; -import java.net.URISyntaxException; -import java.nio.file.Files; -import java.nio.file.Paths; -import java.util.Collections; -import java.util.Map; -import java.util.stream.Collectors; - -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -@ExtendWith(MockitoExtension.class) -class ViewMetaDataPersistServiceTest { - - @Mock - private PersistRepository repository; - - @Test - void assertPersist() { - ShardingSphereView view = new ShardingSphereView("foo_view", "select `db`.`db`.`id` AS `id`,`db`.`db`.`order_id` AS `order_id` from `db`.`db`"); - new ViewMetaDataPersistService(repository).persist("foo_db", "foo_schema", Collections.singletonMap("foo_view", view)); - verify(repository).persist("/metadata/foo_db/schemas/foo_schema/views/foo_view", "name: foo_view" + System.lineSeparator() - + "viewDefinition: select `db`.`db`.`id` AS `id`,`db`.`db`.`order_id` AS `order_id` from" + System.lineSeparator() - + " `db`.`db`" + System.lineSeparator()); - } - - @Test - void assertLoad() { - ViewMetaDataPersistService viewMetaDataPersistService = new ViewMetaDataPersistService(repository); - when(repository.getChildrenKeys("/metadata/foo_db/schemas/foo_schema/views")).thenReturn(Collections.singletonList("foo_view")); - when(repository.getDirectly("/metadata/foo_db/schemas/foo_schema/views/foo_view")).thenReturn(readYAML()); - Map views = viewMetaDataPersistService.load("foo_db", "foo_schema"); - assertThat(views.size(), is(1)); - assertThat(views.get("foo_view").getName(), is("foo_view")); - assertThat(views.get("foo_view").getViewDefinition(), is("select `db`.`db`.`id` AS `id`,`db`.`db`.`order_id` AS `order_id` from `db`.`db`")); - } - - @Test - void assertDelete() { - new ViewMetaDataPersistService(repository).delete("foo_db", "foo_schema", "foo_view"); - verify(repository).delete("/metadata/foo_db/schemas/foo_schema/views/foo_view"); - } - - @SneakyThrows({IOException.class, URISyntaxException.class}) - private String readYAML() { - return Files.readAllLines(Paths.get(ClassLoader.getSystemResource("yaml/schema/view.yaml").toURI())).stream().map(each -> each + System.lineSeparator()).collect(Collectors.joining()); - } -} diff --git a/kernel/metadata/core/src/test/resources/yaml/persist/data-source.yaml b/kernel/metadata/core/src/test/resources/yaml/persist/data-source.yaml index 59f2b431318a2..1bd6330fbce1d 100644 --- a/kernel/metadata/core/src/test/resources/yaml/persist/data-source.yaml +++ b/kernel/metadata/core/src/test/resources/yaml/persist/data-source.yaml @@ -24,12 +24,3 @@ ds_0: connectionInitSqls: - set names utf8mb4; - set names utf8; -ds_1: - dataSourceClassName: org.apache.shardingsphere.test.fixture.jdbc.MockedDataSource - driverClassName: com.mysql.jdbc.Driver - url: jdbc:mysql://localhost:3306/ds_1 - username: root - password: root - connectionInitSqls: - - set names utf8mb4; - - set names utf8; diff --git a/kernel/single/core/src/test/java/org/apache/shardingsphere/single/yaml/config/swapper/NewYamlSingleRuleConfigurationSwapperTest.java b/kernel/single/core/src/test/java/org/apache/shardingsphere/single/yaml/config/swapper/YamlSingleDataNodeRuleConfigurationSwapperTest.java similarity index 89% rename from kernel/single/core/src/test/java/org/apache/shardingsphere/single/yaml/config/swapper/NewYamlSingleRuleConfigurationSwapperTest.java rename to kernel/single/core/src/test/java/org/apache/shardingsphere/single/yaml/config/swapper/YamlSingleDataNodeRuleConfigurationSwapperTest.java index e705017949949..ea518b7f886b1 100644 --- a/kernel/single/core/src/test/java/org/apache/shardingsphere/single/yaml/config/swapper/NewYamlSingleRuleConfigurationSwapperTest.java +++ b/kernel/single/core/src/test/java/org/apache/shardingsphere/single/yaml/config/swapper/YamlSingleDataNodeRuleConfigurationSwapperTest.java @@ -23,8 +23,7 @@ import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; -// TODO Rename YamlSingleRuleConfigurationSwapperTest when metadata structure adjustment completed. #25485 -class NewYamlSingleRuleConfigurationSwapperTest { +class YamlSingleDataNodeRuleConfigurationSwapperTest { private final YamlSingleDataNodeRuleConfigurationSwapper swapper = new YamlSingleDataNodeRuleConfigurationSwapper(); diff --git a/kernel/sql-federation/core/src/test/java/org/apache/shardingsphere/sqlfederation/yaml/swapper/NewYamlSQLFederationRuleConfigurationSwapperTest.java b/kernel/sql-federation/core/src/test/java/org/apache/shardingsphere/sqlfederation/yaml/swapper/YamlSQLFederationDataNodeRuleConfigurationSwapperTest.java similarity index 89% rename from kernel/sql-federation/core/src/test/java/org/apache/shardingsphere/sqlfederation/yaml/swapper/NewYamlSQLFederationRuleConfigurationSwapperTest.java rename to kernel/sql-federation/core/src/test/java/org/apache/shardingsphere/sqlfederation/yaml/swapper/YamlSQLFederationDataNodeRuleConfigurationSwapperTest.java index c4d62c57c5e61..31beae7b2ade9 100644 --- a/kernel/sql-federation/core/src/test/java/org/apache/shardingsphere/sqlfederation/yaml/swapper/NewYamlSQLFederationRuleConfigurationSwapperTest.java +++ b/kernel/sql-federation/core/src/test/java/org/apache/shardingsphere/sqlfederation/yaml/swapper/YamlSQLFederationDataNodeRuleConfigurationSwapperTest.java @@ -24,8 +24,7 @@ import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; -// TODO Rename YamlSQLFederationRuleConfigurationSwapperTest when metadata structure adjustment completed. #25485 -class NewYamlSQLFederationRuleConfigurationSwapperTest { +class YamlSQLFederationDataNodeRuleConfigurationSwapperTest { private final YamlSQLFederationDataNodeRuleConfigurationSwapper swapper = new YamlSQLFederationDataNodeRuleConfigurationSwapper(); diff --git a/kernel/sql-parser/core/src/test/java/org/apache/shardingsphere/parser/yaml/swapper/NewYamlSQLParserRuleConfigurationSwapperTest.java b/kernel/sql-parser/core/src/test/java/org/apache/shardingsphere/parser/yaml/swapper/YamlSQLParserDataNodeRuleConfigurationSwapperTest.java similarity index 90% rename from kernel/sql-parser/core/src/test/java/org/apache/shardingsphere/parser/yaml/swapper/NewYamlSQLParserRuleConfigurationSwapperTest.java rename to kernel/sql-parser/core/src/test/java/org/apache/shardingsphere/parser/yaml/swapper/YamlSQLParserDataNodeRuleConfigurationSwapperTest.java index 61342c14b15df..e40f2d0b98d57 100644 --- a/kernel/sql-parser/core/src/test/java/org/apache/shardingsphere/parser/yaml/swapper/NewYamlSQLParserRuleConfigurationSwapperTest.java +++ b/kernel/sql-parser/core/src/test/java/org/apache/shardingsphere/parser/yaml/swapper/YamlSQLParserDataNodeRuleConfigurationSwapperTest.java @@ -24,8 +24,7 @@ import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; -// TODO Rename YamlSQLParserRuleConfigurationSwapperTest when metadata structure adjustment completed. #25485 -class NewYamlSQLParserRuleConfigurationSwapperTest { +class YamlSQLParserDataNodeRuleConfigurationSwapperTest { private final YamlSQLParserDataNodeRuleConfigurationSwapper swapper = new YamlSQLParserDataNodeRuleConfigurationSwapper(); diff --git a/kernel/sql-translator/core/src/test/java/org/apache/shardingsphere/sqltranslator/yaml/swapper/NewYamlSQLTranslatorRuleConfigurationSwapperTest.java b/kernel/sql-translator/core/src/test/java/org/apache/shardingsphere/sqltranslator/yaml/swapper/YamlSQLTranslatorDataNodeRuleConfigurationSwapperTest.java similarity index 89% rename from kernel/sql-translator/core/src/test/java/org/apache/shardingsphere/sqltranslator/yaml/swapper/NewYamlSQLTranslatorRuleConfigurationSwapperTest.java rename to kernel/sql-translator/core/src/test/java/org/apache/shardingsphere/sqltranslator/yaml/swapper/YamlSQLTranslatorDataNodeRuleConfigurationSwapperTest.java index fdcd435d178b8..b4592aa135bb9 100644 --- a/kernel/sql-translator/core/src/test/java/org/apache/shardingsphere/sqltranslator/yaml/swapper/NewYamlSQLTranslatorRuleConfigurationSwapperTest.java +++ b/kernel/sql-translator/core/src/test/java/org/apache/shardingsphere/sqltranslator/yaml/swapper/YamlSQLTranslatorDataNodeRuleConfigurationSwapperTest.java @@ -23,8 +23,7 @@ import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; -// TODO Rename YamlSQLTranslatorRuleConfigurationSwapperTest when metadata structure adjustment completed. #25485 -class NewYamlSQLTranslatorRuleConfigurationSwapperTest { +class YamlSQLTranslatorDataNodeRuleConfigurationSwapperTest { private final YamlSQLTranslatorDataNodeRuleConfigurationSwapper swapper = new YamlSQLTranslatorDataNodeRuleConfigurationSwapper(); diff --git a/kernel/traffic/core/src/test/java/org/apache/shardingsphere/traffic/yaml/swapper/NewYamlTrafficRuleConfigurationSwapperTest.java b/kernel/traffic/core/src/test/java/org/apache/shardingsphere/traffic/yaml/swapper/YamlTrafficDataNodeRuleConfigurationSwapperTest.java similarity index 89% rename from kernel/traffic/core/src/test/java/org/apache/shardingsphere/traffic/yaml/swapper/NewYamlTrafficRuleConfigurationSwapperTest.java rename to kernel/traffic/core/src/test/java/org/apache/shardingsphere/traffic/yaml/swapper/YamlTrafficDataNodeRuleConfigurationSwapperTest.java index 1e874d868bdf9..8914ce2329a9f 100644 --- a/kernel/traffic/core/src/test/java/org/apache/shardingsphere/traffic/yaml/swapper/NewYamlTrafficRuleConfigurationSwapperTest.java +++ b/kernel/traffic/core/src/test/java/org/apache/shardingsphere/traffic/yaml/swapper/YamlTrafficDataNodeRuleConfigurationSwapperTest.java @@ -23,8 +23,7 @@ import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; -// TODO Rename YamlTrafficRuleConfigurationSwapperTest when metadata structure adjustment completed. #25485 -class NewYamlTrafficRuleConfigurationSwapperTest { +class YamlTrafficDataNodeRuleConfigurationSwapperTest { private final YamlTrafficDataNodeRuleConfigurationSwapper swapper = new YamlTrafficDataNodeRuleConfigurationSwapper(); diff --git a/kernel/transaction/core/src/test/java/org/apache/shardingsphere/transaction/yaml/swapper/NewYamlTransactionRuleConfigurationSwapperTest.java b/kernel/transaction/core/src/test/java/org/apache/shardingsphere/transaction/yaml/swapper/YamlTransactionDataNodeRuleConfigurationSwapperTest.java similarity index 90% rename from kernel/transaction/core/src/test/java/org/apache/shardingsphere/transaction/yaml/swapper/NewYamlTransactionRuleConfigurationSwapperTest.java rename to kernel/transaction/core/src/test/java/org/apache/shardingsphere/transaction/yaml/swapper/YamlTransactionDataNodeRuleConfigurationSwapperTest.java index a1c96d4569a98..fa180a57ddabb 100644 --- a/kernel/transaction/core/src/test/java/org/apache/shardingsphere/transaction/yaml/swapper/NewYamlTransactionRuleConfigurationSwapperTest.java +++ b/kernel/transaction/core/src/test/java/org/apache/shardingsphere/transaction/yaml/swapper/YamlTransactionDataNodeRuleConfigurationSwapperTest.java @@ -27,8 +27,7 @@ import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; -// TODO Rename YamlTransactionRuleConfigurationSwapperTest when metadata structure adjustment completed. #25485 -class NewYamlTransactionRuleConfigurationSwapperTest { +class YamlTransactionDataNodeRuleConfigurationSwapperTest { private final YamlTransactionDataNodeRuleConfigurationSwapper swapper = new YamlTransactionDataNodeRuleConfigurationSwapper(); 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 5a8d08951aef2..f7b34c1f323db 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 @@ -167,7 +167,7 @@ public void refreshTableMetaData(final ShardingSphereDatabase database) { private MetaDataContexts createMetaDataContexts(final ShardingSphereDatabase database) throws SQLException { MetaDataBasedPersistService metaDataPersistService = metaDataContexts.get().getPersistService(); Map dataSourcePoolPropsFromRegCenter = metaDataPersistService.getDataSourceUnitService().load(database.getName()); - SwitchingResource switchingResource = new ResourceSwitchManager().createByAlterDataSourcePoolProperties(database.getResourceMetaData(), dataSourcePoolPropsFromRegCenter); + SwitchingResource switchingResource = new ResourceSwitchManager().alterStorageUnit(database.getResourceMetaData(), dataSourcePoolPropsFromRegCenter); metaDataContexts.get().getMetaData().getDatabases().putAll(configurationContextManager.renewDatabase(database, switchingResource)); Collection ruleConfigs = metaDataPersistService.getDatabaseRulePersistService().load(database.getName()); Map changedDatabases = configurationContextManager.createChangedDatabases(database.getName(), false, switchingResource, ruleConfigs); 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 acebea5d8f498..4d308dd881ea4 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 @@ -41,7 +41,6 @@ import org.apache.shardingsphere.metadata.factory.ExternalMetaDataFactory; import org.apache.shardingsphere.metadata.factory.InternalMetaDataFactory; import org.apache.shardingsphere.metadata.persist.MetaDataBasedPersistService; -import org.apache.shardingsphere.mode.manager.switcher.NewResourceSwitchManager; import org.apache.shardingsphere.mode.manager.switcher.ResourceSwitchManager; import org.apache.shardingsphere.mode.manager.switcher.SwitchingResource; import org.apache.shardingsphere.mode.metadata.MetaDataContexts; @@ -80,7 +79,7 @@ public synchronized void registerStorageUnit(final String databaseName, final Ma try { closeStaleRules(databaseName); SwitchingResource switchingResource = - new NewResourceSwitchManager().registerStorageUnit(metaDataContexts.get().getMetaData().getDatabase(databaseName).getResourceMetaData(), propsMap); + new ResourceSwitchManager().registerStorageUnit(metaDataContexts.get().getMetaData().getDatabase(databaseName).getResourceMetaData(), propsMap); buildNewMetaDataContext(databaseName, switchingResource); } catch (final SQLException ex) { log.error("Alter database: {} register storage unit failed", databaseName, ex); @@ -97,7 +96,7 @@ public synchronized void alterStorageUnit(final String databaseName, final Map propsMap) { - try { - closeStaleRules(databaseName); - SwitchingResource switchingResource = - new ResourceSwitchManager().createByAlterDataSourcePoolProperties(metaDataContexts.get().getMetaData().getDatabase(databaseName).getResourceMetaData(), propsMap); - metaDataContexts.get().getMetaData().getDatabases().putAll(renewDatabase(metaDataContexts.get().getMetaData().getDatabase(databaseName), switchingResource)); - // TODO Remove this logic when issue #22887 are finished. - MetaDataContexts reloadMetaDataContexts = createMetaDataContexts(databaseName, false, switchingResource, null); - reloadMetaDataContexts.getMetaData().getDatabase(databaseName).getSchemas().forEach((schemaName, schema) -> reloadMetaDataContexts.getPersistService().getDatabaseMetaDataService() - .persist(reloadMetaDataContexts.getMetaData().getDatabase(databaseName).getName(), schemaName, schema)); - Optional.ofNullable(reloadMetaDataContexts.getStatistics().getDatabaseData().get(databaseName)) - .ifPresent(optional -> optional.getSchemaData().forEach((schemaName, schemaData) -> reloadMetaDataContexts.getPersistService().getShardingSphereDataPersistService() - .persist(databaseName, schemaName, schemaData, metaDataContexts.get().getMetaData().getDatabases()))); - alterSchemaMetaData(databaseName, reloadMetaDataContexts.getMetaData().getDatabase(databaseName), metaDataContexts.get().getMetaData().getDatabase(databaseName)); - metaDataContexts.set(reloadMetaDataContexts); - metaDataContexts.get().getMetaData().getDatabases().putAll(newShardingSphereDatabase(metaDataContexts.get().getMetaData().getDatabase(databaseName))); - switchingResource.closeStaleDataSources(); - } catch (final SQLException ex) { - log.error("Alter database: {} data source configuration failed", databaseName, ex); - } - } - /** * Alter schema meta data. * diff --git a/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/switcher/NewResourceSwitchManager.java b/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/switcher/NewResourceSwitchManager.java deleted file mode 100644 index 991629f750617..0000000000000 --- a/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/switcher/NewResourceSwitchManager.java +++ /dev/null @@ -1,139 +0,0 @@ -/* - * 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.mode.manager.switcher; - -import org.apache.shardingsphere.infra.datasource.pool.creator.DataSourcePoolCreator; -import org.apache.shardingsphere.infra.datasource.pool.props.domain.DataSourcePoolProperties; -import org.apache.shardingsphere.infra.metadata.database.resource.ResourceMetaData; -import org.apache.shardingsphere.infra.metadata.database.resource.node.StorageNode; -import org.apache.shardingsphere.infra.metadata.database.resource.node.StorageNodeAggregator; -import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnitNodeMapCreator; - -import javax.sql.DataSource; -import java.util.Collection; -import java.util.Collections; -import java.util.LinkedHashMap; -import java.util.LinkedHashSet; -import java.util.Map; -import java.util.Map.Entry; -import java.util.stream.Collectors; - -/** - * TODO Rename ResourceSwitchManager when metadata structure adjustment completed. #25485 - * Resource switch manager. - */ -public final class NewResourceSwitchManager { - - /** - * Register storage unit. - * - * @param resourceMetaData resource meta data - * @param storageUnitDataSourcePoolPropsMap storage unit grouped data source pool properties map - * @return created switching resource - */ - public SwitchingResource registerStorageUnit(final ResourceMetaData resourceMetaData, final Map storageUnitDataSourcePoolPropsMap) { - Map mergedPropsMap = new LinkedHashMap<>(resourceMetaData.getStorageUnits().entrySet().stream() - .collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getDataSourcePoolProperties(), (oldValue, currentValue) -> oldValue, LinkedHashMap::new))); - mergedPropsMap.putAll(storageUnitDataSourcePoolPropsMap); - Map toBeCreatedStorageUintNodeMap = StorageUnitNodeMapCreator.create(storageUnitDataSourcePoolPropsMap); - Map dataSourcePoolPropsMap = StorageNodeAggregator.aggregateDataSourcePoolProperties(storageUnitDataSourcePoolPropsMap); - return new SwitchingResource(getNewDataSources(resourceMetaData, toBeCreatedStorageUintNodeMap, dataSourcePoolPropsMap), Collections.emptyMap(), Collections.emptyList(), mergedPropsMap); - } - - private Map getNewDataSources(final ResourceMetaData resourceMetaData, - final Map storageUintNodeMap, final Map dataSourcePoolPropsMap) { - Map result = new LinkedHashMap<>(storageUintNodeMap.size(), 1F); - for (StorageNode each : storageUintNodeMap.values()) { - if (!resourceMetaData.getDataSources().containsKey(each)) { - result.put(each, DataSourcePoolCreator.create(dataSourcePoolPropsMap.get(each))); - } - } - return result; - } - - /** - * Alter storage unit. - * - * @param resourceMetaData resource meta data - * @param propsMap data source pool properties map - * @return created switching resource - */ - public SwitchingResource alterStorageUnit(final ResourceMetaData resourceMetaData, final Map propsMap) { - Map mergedDataSourcePoolPropsMap = new LinkedHashMap<>(resourceMetaData.getStorageUnits().entrySet().stream() - .collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getDataSourcePoolProperties(), (oldValue, currentValue) -> oldValue, LinkedHashMap::new))); - mergedDataSourcePoolPropsMap.putAll(propsMap); - Map toBeAlteredStorageUintNodeMap = StorageUnitNodeMapCreator.create(mergedDataSourcePoolPropsMap); - Map dataSourcePoolPropsMap = StorageNodeAggregator.aggregateDataSourcePoolProperties(mergedDataSourcePoolPropsMap); - return new SwitchingResource(getAlterNewDataSources(toBeAlteredStorageUintNodeMap, dataSourcePoolPropsMap), - getStaleDataSources(resourceMetaData, toBeAlteredStorageUintNodeMap.values()), new LinkedHashSet<>(toBeAlteredStorageUintNodeMap.keySet()), mergedDataSourcePoolPropsMap); - } - - private Map getAlterNewDataSources(final Map storageUintNodeMap, final Map dataSourcePoolPropsMap) { - Map result = new LinkedHashMap<>(storageUintNodeMap.size(), 1F); - for (StorageNode each : storageUintNodeMap.values()) { - result.put(each, DataSourcePoolCreator.create(dataSourcePoolPropsMap.get(each))); - } - return result; - } - - private Map getStaleDataSources(final ResourceMetaData resourceMetaData, final Collection storageNodes) { - Map result = new LinkedHashMap<>(storageNodes.size(), 1F); - for (Entry entry : resourceMetaData.getDataSources().entrySet()) { - if (storageNodes.contains(entry.getKey())) { - result.put(entry.getKey(), entry.getValue()); - } - } - return result; - } - - /** - * Unregister storage unit. - * - * @param resourceMetaData resource meta data - * @param storageUnitNames storage unit names - * @return created switching resource - */ - public SwitchingResource unregisterStorageUnit(final ResourceMetaData resourceMetaData, final Collection storageUnitNames) { - Map mergedDataSourcePoolPropertiesMap = new LinkedHashMap<>(resourceMetaData.getStorageUnits().entrySet().stream() - .collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getDataSourcePoolProperties(), (oldValue, currentValue) -> oldValue, LinkedHashMap::new))); - SwitchingResource result = new SwitchingResource(Collections.emptyMap(), - getToBeRemovedStaleDataSource(resourceMetaData, storageUnitNames), storageUnitNames, mergedDataSourcePoolPropertiesMap); - removeToBeRemovedStorageUnitNames(resourceMetaData, mergedDataSourcePoolPropertiesMap, storageUnitNames); - return result; - } - - private Map getToBeRemovedStaleDataSource(final ResourceMetaData resourceMetaData, final Collection storageUnitNames) { - Map result = new LinkedHashMap<>(); - for (String each : storageUnitNames) { - if (!resourceMetaData.getStorageUnits().containsKey(each)) { - return Collections.emptyMap(); - } - StorageNode storageNode = resourceMetaData.getStorageUnits().get(each).getStorageNode(); - result.put(storageNode, resourceMetaData.getDataSources().get(storageNode)); - } - return result; - } - - private void removeToBeRemovedStorageUnitNames(final ResourceMetaData resourceMetaData, final Map dataSourcePoolPropsMap, - final Collection storageUnitNames) { - for (String each : storageUnitNames) { - dataSourcePoolPropsMap.remove(each); - resourceMetaData.getStorageUnits().remove(each); - } - } -} diff --git a/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/switcher/ResourceSwitchManager.java b/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/switcher/ResourceSwitchManager.java index 671d3d976d725..ae29706d9a1d3 100644 --- a/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/switcher/ResourceSwitchManager.java +++ b/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/switcher/ResourceSwitchManager.java @@ -18,12 +18,10 @@ package org.apache.shardingsphere.mode.manager.switcher; import org.apache.shardingsphere.infra.datasource.pool.creator.DataSourcePoolCreator; -import org.apache.shardingsphere.infra.datasource.pool.props.creator.DataSourcePoolPropertiesCreator; import org.apache.shardingsphere.infra.datasource.pool.props.domain.DataSourcePoolProperties; import org.apache.shardingsphere.infra.metadata.database.resource.ResourceMetaData; import org.apache.shardingsphere.infra.metadata.database.resource.node.StorageNode; import org.apache.shardingsphere.infra.metadata.database.resource.node.StorageNodeAggregator; -import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnit; import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnitNodeMapCreator; import javax.sql.DataSource; @@ -41,151 +39,100 @@ public final class ResourceSwitchManager { /** - * Create switching resource. - * - * @param resourceMetaData resource meta data - * @param toBeChangedPropsMap to be changed data source pool properties map - * @return created switching resource - */ - public SwitchingResource create(final ResourceMetaData resourceMetaData, final Map toBeChangedPropsMap) { - Map toBeChangedStorageUnitNodeMap = StorageUnitNodeMapCreator.create(toBeChangedPropsMap); - Map dataSourcePoolPropsMap = StorageNodeAggregator.aggregateDataSourcePoolProperties(toBeChangedPropsMap); - Map newDataSources = createNewDataSources(resourceMetaData, toBeChangedStorageUnitNodeMap, dataSourcePoolPropsMap); - Map staleDataSources = getStaleDataSources(resourceMetaData, toBeChangedPropsMap); - return new SwitchingResource(newDataSources, staleDataSources, getStaleStorageUnitNames(resourceMetaData, toBeChangedStorageUnitNodeMap), toBeChangedPropsMap); - } - - /** - * Create switching resource by drop resource. + * Register storage unit. * * @param resourceMetaData resource meta data - * @param toBeDeletedPropsMap to be deleted data source pool properties map + * @param storageUnitDataSourcePoolPropsMap storage unit grouped data source pool properties map * @return created switching resource */ - public SwitchingResource createByDropResource(final ResourceMetaData resourceMetaData, final Map toBeDeletedPropsMap) { - Map mergedDataSourcePoolPropertiesMap = new LinkedHashMap<>(resourceMetaData.getStorageUnits().entrySet().stream() + public SwitchingResource registerStorageUnit(final ResourceMetaData resourceMetaData, final Map storageUnitDataSourcePoolPropsMap) { + Map mergedPropsMap = new LinkedHashMap<>(resourceMetaData.getStorageUnits().entrySet().stream() .collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getDataSourcePoolProperties(), (oldValue, currentValue) -> oldValue, LinkedHashMap::new))); - mergedDataSourcePoolPropertiesMap.keySet().removeIf(toBeDeletedPropsMap::containsKey); - Map toRemovedStorageUnitNodeMap = StorageUnitNodeMapCreator.create(toBeDeletedPropsMap); - return new SwitchingResource(Collections.emptyMap(), getToBeRemovedStaleDataSources(resourceMetaData, toRemovedStorageUnitNodeMap), - getToBeRemovedStaleStorageUnitNames(resourceMetaData, toRemovedStorageUnitNodeMap), mergedDataSourcePoolPropertiesMap); + mergedPropsMap.putAll(storageUnitDataSourcePoolPropsMap); + Map toBeCreatedStorageUintNodeMap = StorageUnitNodeMapCreator.create(storageUnitDataSourcePoolPropsMap); + Map dataSourcePoolPropsMap = StorageNodeAggregator.aggregateDataSourcePoolProperties(storageUnitDataSourcePoolPropsMap); + return new SwitchingResource(getNewDataSources(resourceMetaData, toBeCreatedStorageUintNodeMap, dataSourcePoolPropsMap), Collections.emptyMap(), Collections.emptyList(), mergedPropsMap); + } + + private Map getNewDataSources(final ResourceMetaData resourceMetaData, + final Map storageUintNodeMap, final Map dataSourcePoolPropsMap) { + Map result = new LinkedHashMap<>(storageUintNodeMap.size(), 1F); + for (StorageNode each : storageUintNodeMap.values()) { + if (!resourceMetaData.getDataSources().containsKey(each)) { + result.put(each, DataSourcePoolCreator.create(dataSourcePoolPropsMap.get(each))); + } + } + return result; } /** - * Create switching resource by alter data source pool properties. + * Alter storage unit. * * @param resourceMetaData resource meta data - * @param toBeChangedPropsMap to be changed data source pool properties map + * @param propsMap data source pool properties map * @return created switching resource */ - public SwitchingResource createByAlterDataSourcePoolProperties(final ResourceMetaData resourceMetaData, final Map toBeChangedPropsMap) { - Map toBeChangedStorageUnitNodeMap = StorageUnitNodeMapCreator.create(toBeChangedPropsMap); - Map staleStorageDataSources = getStaleDataSources(resourceMetaData, toBeChangedPropsMap); - staleStorageDataSources.putAll(getToBeDeletedDataSources(resourceMetaData.getDataSources(), toBeChangedStorageUnitNodeMap.values())); - Collection staleStorageUnitNames = getStaleStorageUnitNames(resourceMetaData, toBeChangedStorageUnitNodeMap); - staleStorageUnitNames.addAll(getToBeDeletedStorageUnitNames(resourceMetaData.getStorageUnits(), toBeChangedStorageUnitNodeMap.keySet())); - Map dataSourcePoolPropsMap = StorageNodeAggregator.aggregateDataSourcePoolProperties(toBeChangedPropsMap); - return new SwitchingResource( - createNewDataSources(resourceMetaData, toBeChangedStorageUnitNodeMap, dataSourcePoolPropsMap), staleStorageDataSources, staleStorageUnitNames, toBeChangedPropsMap); - } - - private Map createNewDataSources(final ResourceMetaData resourceMetaData, final Map toBeChangedStorageUnitNodeMap, - final Map propsMap) { - Map result = new LinkedHashMap<>(resourceMetaData.getDataSources()); - Collection toBeChangedStorageNodes = toBeChangedStorageUnitNodeMap.values(); - result.keySet().removeAll(getToBeDeletedDataSources(resourceMetaData.getDataSources(), toBeChangedStorageNodes).keySet()); - result.putAll(getChangedDataSources(resourceMetaData.getDataSources(), toBeChangedStorageNodes, propsMap)); - result.putAll(getToBeAddedDataSources(resourceMetaData.getDataSources(), toBeChangedStorageNodes, propsMap)); - return result; + public SwitchingResource alterStorageUnit(final ResourceMetaData resourceMetaData, final Map propsMap) { + Map mergedDataSourcePoolPropsMap = new LinkedHashMap<>(resourceMetaData.getStorageUnits().entrySet().stream() + .collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getDataSourcePoolProperties(), (oldValue, currentValue) -> oldValue, LinkedHashMap::new))); + mergedDataSourcePoolPropsMap.putAll(propsMap); + Map toBeAlteredStorageUintNodeMap = StorageUnitNodeMapCreator.create(mergedDataSourcePoolPropsMap); + Map dataSourcePoolPropsMap = StorageNodeAggregator.aggregateDataSourcePoolProperties(mergedDataSourcePoolPropsMap); + return new SwitchingResource(getAlterNewDataSources(toBeAlteredStorageUintNodeMap, dataSourcePoolPropsMap), + getStaleDataSources(resourceMetaData, toBeAlteredStorageUintNodeMap.values()), new LinkedHashSet<>(toBeAlteredStorageUintNodeMap.keySet()), mergedDataSourcePoolPropsMap); } - private Map getChangedDataSources(final Map storageNodes, - final Collection toBeChangedStorageNodes, final Map propsMap) { - Collection toBeChangedDataSourceNames = toBeChangedStorageNodes.stream() - .filter(each -> isModifiedDataSource(storageNodes, each, propsMap.get(each))).collect(Collectors.toList()); - Map result = new LinkedHashMap<>(toBeChangedStorageNodes.size(), 1F); - for (StorageNode each : toBeChangedDataSourceNames) { - result.put(each, DataSourcePoolCreator.create(propsMap.get(each))); + private Map getAlterNewDataSources(final Map storageUintNodeMap, final Map dataSourcePoolPropsMap) { + Map result = new LinkedHashMap<>(storageUintNodeMap.size(), 1F); + for (StorageNode each : storageUintNodeMap.values()) { + result.put(each, DataSourcePoolCreator.create(dataSourcePoolPropsMap.get(each))); } return result; } - private boolean isModifiedDataSource(final Map originalDataSources, - final StorageNode storageNode, final DataSourcePoolProperties propsMap) { - return originalDataSources.containsKey(storageNode) && !propsMap.equals(DataSourcePoolPropertiesCreator.create(originalDataSources.get(storageNode))); - } - - private Map getToBeAddedDataSources(final Map storageNodes, - final Collection toBeChangedStorageNodes, final Map propsMap) { - Collection toBeAddedDataSourceNames = toBeChangedStorageNodes.stream().filter(each -> !storageNodes.containsKey(each)).collect(Collectors.toList()); - Map result = new LinkedHashMap<>(); - for (StorageNode each : toBeAddedDataSourceNames) { - result.put(each, DataSourcePoolCreator.create(propsMap.get(each))); + private Map getStaleDataSources(final ResourceMetaData resourceMetaData, final Collection storageNodes) { + Map result = new LinkedHashMap<>(storageNodes.size(), 1F); + for (Entry entry : resourceMetaData.getDataSources().entrySet()) { + if (storageNodes.contains(entry.getKey())) { + result.put(entry.getKey(), entry.getValue()); + } } return result; } - private Map getToBeRemovedStaleDataSources(final ResourceMetaData resourceMetaData, final Map toRemovedStorageUnitNodeMap) { - Map reservedStorageUnitNodeMap = resourceMetaData.getStorageUnits().entrySet().stream() - .filter(entry -> !toRemovedStorageUnitNodeMap.containsKey(entry.getKey())) - .collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getStorageNode())); - return resourceMetaData.getDataSources().entrySet().stream() - .filter(entry -> toRemovedStorageUnitNodeMap.containsValue(entry.getKey()) && !reservedStorageUnitNodeMap.containsValue(entry.getKey())) - .collect(Collectors.toMap(Entry::getKey, Entry::getValue)); - } - - private Collection getToBeRemovedStaleStorageUnitNames(final ResourceMetaData resourceMetaData, final Map toRemovedStorageUnitNodeMap) { - Map reservedStorageUnitNodeMap = resourceMetaData.getStorageUnits().entrySet().stream() - .filter(entry -> !toRemovedStorageUnitNodeMap.containsKey(entry.getKey())) - .collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getStorageNode())); - Map staleStorageUnitNodeMap = resourceMetaData.getStorageUnits().entrySet().stream() - .filter(entry -> !reservedStorageUnitNodeMap.containsKey(entry.getKey())).collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getStorageNode())); - return new LinkedHashSet<>(staleStorageUnitNodeMap.keySet()); - } - - private Map getStaleDataSources(final ResourceMetaData resourceMetaData, final Map storageUnitDataSourcePoolPropsMap) { - Map result = new LinkedHashMap<>(resourceMetaData.getDataSources().size(), 1F); - result.putAll(getToBeChangedDataSources(resourceMetaData.getDataSources(), StorageNodeAggregator.aggregateDataSourcePoolProperties(storageUnitDataSourcePoolPropsMap))); + /** + * Unregister storage unit. + * + * @param resourceMetaData resource meta data + * @param storageUnitNames storage unit names + * @return created switching resource + */ + public SwitchingResource unregisterStorageUnit(final ResourceMetaData resourceMetaData, final Collection storageUnitNames) { + Map mergedDataSourcePoolPropertiesMap = new LinkedHashMap<>(resourceMetaData.getStorageUnits().entrySet().stream() + .collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getDataSourcePoolProperties(), (oldValue, currentValue) -> oldValue, LinkedHashMap::new))); + SwitchingResource result = new SwitchingResource(Collections.emptyMap(), + getToBeRemovedStaleDataSource(resourceMetaData, storageUnitNames), storageUnitNames, mergedDataSourcePoolPropertiesMap); + removeToBeRemovedStorageUnitNames(resourceMetaData, mergedDataSourcePoolPropertiesMap, storageUnitNames); return result; } - private Collection getStaleStorageUnitNames(final ResourceMetaData resourceMetaData, final Map toBeChangedStorageUnitNodeMap) { - Map storageUnitNodeMap = new LinkedHashMap<>(resourceMetaData.getStorageUnits().size(), 1F); - storageUnitNodeMap.putAll(getChangedStorageUnitNodeMap(resourceMetaData.getStorageUnits(), toBeChangedStorageUnitNodeMap)); - return new LinkedHashSet<>(storageUnitNodeMap.keySet()); - } - - private Map getToBeChangedDataSources(final Map storageNodes, final Map propsMap) { - Map result = new LinkedHashMap<>(storageNodes.size(), 1F); - for (Entry entry : propsMap.entrySet()) { - StorageNode storageNode = entry.getKey(); - if (isModifiedDataSource(storageNodes, storageNode, entry.getValue())) { - result.put(storageNode, storageNodes.get(storageNode)); + private Map getToBeRemovedStaleDataSource(final ResourceMetaData resourceMetaData, final Collection storageUnitNames) { + Map result = new LinkedHashMap<>(); + for (String each : storageUnitNames) { + if (!resourceMetaData.getStorageUnits().containsKey(each)) { + return Collections.emptyMap(); } + StorageNode storageNode = resourceMetaData.getStorageUnits().get(each).getStorageNode(); + result.put(storageNode, resourceMetaData.getDataSources().get(storageNode)); } return result; } - private Map getToBeDeletedDataSources(final Map storageNodes, final Collection toBeChangedDataSourceNames) { - Map result = new LinkedHashMap<>(storageNodes.size(), 1F); - for (Entry entry : storageNodes.entrySet()) { - if (!toBeChangedDataSourceNames.contains(entry.getKey())) { - result.put(entry.getKey(), entry.getValue()); - } + private void removeToBeRemovedStorageUnitNames(final ResourceMetaData resourceMetaData, final Map dataSourcePoolPropsMap, + final Collection storageUnitNames) { + for (String each : storageUnitNames) { + dataSourcePoolPropsMap.remove(each); + resourceMetaData.getStorageUnits().remove(each); } - return result; - } - - private Collection getToBeDeletedStorageUnitNames(final Map storageUnits, final Collection toBeChangedStorageUnitNames) { - return storageUnits.keySet().stream().filter(storageUnit -> !toBeChangedStorageUnitNames.contains(storageUnit)).collect(Collectors.toList()); - } - - private Map getChangedStorageUnitNodeMap(final Map storageUnits, final Map toBeChangedStorageUnitNodeMap) { - return toBeChangedStorageUnitNodeMap.entrySet().stream().filter(entry -> isModifiedStorageUnitNodeMap(storageUnits, entry.getKey(), entry.getValue())) - .collect(Collectors.toMap(Entry::getKey, Entry::getValue, (oldValue, currentValue) -> oldValue, LinkedHashMap::new)); - } - - private boolean isModifiedStorageUnitNodeMap(final Map originalStorageUnits, final String dataSourceName, final StorageNode storageNode) { - return originalStorageUnits.containsKey(dataSourceName) && !storageNode.equals(originalStorageUnits.get(dataSourceName).getStorageNode()); } } 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 164b709e9cd64..708180c3ab6a4 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 @@ -54,7 +54,7 @@ import java.util.stream.Collectors; /** - * Meta data contexts. + * Meta data contexts factory. */ @NoArgsConstructor(access = AccessLevel.PRIVATE) public final class MetaDataContextsFactory { @@ -74,7 +74,7 @@ public static MetaDataContexts create(final MetaDataPersistService persistServic /** * Create meta data contexts. - * + * * @param persistService persist service * @param param context manager builder parameter * @param instanceContext instance context @@ -106,7 +106,8 @@ public static MetaDataContexts create(final MetaDataPersistService persistServic return result; } - private static Collection getDatabaseNames(final InstanceContext instanceContext, final Map databaseConfigs, final MetaDataPersistService persistService) { + private static Collection getDatabaseNames(final InstanceContext instanceContext, + final Map databaseConfigs, final MetaDataPersistService persistService) { return instanceContext.getInstance().getMetaData() instanceof JDBCInstanceMetaData ? databaseConfigs.keySet() : persistService.getDatabaseMetaDataService().loadAllDatabaseNames(); } diff --git a/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/NewMetaDataContextsFactory.java b/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/NewMetaDataContextsFactory.java deleted file mode 100644 index 915ef28f0180b..0000000000000 --- a/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/NewMetaDataContextsFactory.java +++ /dev/null @@ -1,172 +0,0 @@ -/* - * 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.mode.metadata; - -import com.google.common.base.Preconditions; -import com.google.common.base.Splitter; -import lombok.AccessLevel; -import lombok.NoArgsConstructor; -import org.apache.shardingsphere.infra.config.database.DatabaseConfiguration; -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.datasource.pool.config.DataSourceConfiguration; -import org.apache.shardingsphere.infra.datasource.pool.destroyer.DataSourcePoolDestroyer; -import org.apache.shardingsphere.infra.instance.InstanceContext; -import org.apache.shardingsphere.infra.instance.metadata.jdbc.JDBCInstanceMetaData; -import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData; -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.rule.builder.global.GlobalRulesBuilder; -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.NewInternalMetaDataFactory; -import org.apache.shardingsphere.metadata.persist.NewMetaDataPersistService; -import org.apache.shardingsphere.mode.event.storage.StorageNodeDataSource; -import org.apache.shardingsphere.mode.manager.ContextManagerBuilderParameter; - -import javax.sql.DataSource; -import java.sql.SQLException; -import java.util.Collection; -import java.util.Collections; -import java.util.HashMap; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; -import java.util.Map.Entry; -import java.util.stream.Collectors; - -/** - * TODO replace the old implementation after meta data refactor completed - * New meta data contexts factory. - */ -@NoArgsConstructor(access = AccessLevel.PRIVATE) -public final class NewMetaDataContextsFactory { - - /** - * Create meta data contexts. - * - * @param persistService persist service - * @param param context manager builder parameter - * @param instanceContext instance context - * @return meta data contexts - * @throws SQLException SQL exception - */ - public static MetaDataContexts create(final NewMetaDataPersistService persistService, final ContextManagerBuilderParameter param, final InstanceContext instanceContext) throws SQLException { - return create(persistService, param, instanceContext, Collections.emptyMap()); - } - - /** - * Create meta data contexts. - * - * @param persistService persist service - * @param param context manager builder parameter - * @param instanceContext instance context - * @param storageNodes storage nodes - * @return meta data contexts - * @throws SQLException SQL exception - */ - public static MetaDataContexts create(final NewMetaDataPersistService persistService, final ContextManagerBuilderParameter param, - final InstanceContext instanceContext, final Map storageNodes) throws SQLException { - boolean isDatabaseMetaDataExisted = !persistService.getDatabaseMetaDataService().loadAllDatabaseNames().isEmpty(); - Map effectiveDatabaseConfigs = isDatabaseMetaDataExisted - ? createEffectiveDatabaseConfigurations(getDatabaseNames(instanceContext, param.getDatabaseConfigs(), persistService), param.getDatabaseConfigs(), persistService) - : param.getDatabaseConfigs(); - checkDataSourceStates(effectiveDatabaseConfigs, storageNodes, param.isForce()); - // TODO load global data sources from persist service - Map globalDataSources = param.getGlobalDataSources(); - Collection globalRuleConfigs = isDatabaseMetaDataExisted ? persistService.getGlobalRuleService().load() : param.getGlobalRuleConfigs(); - ConfigurationProperties props = isDatabaseMetaDataExisted ? new ConfigurationProperties(persistService.getPropsService().load()) : new ConfigurationProperties(param.getProps()); - Map databases = isDatabaseMetaDataExisted - ? NewInternalMetaDataFactory.create(persistService, effectiveDatabaseConfigs, props, instanceContext) - : ExternalMetaDataFactory.create(effectiveDatabaseConfigs, props, instanceContext); - 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)); - if (!isDatabaseMetaDataExisted) { - persistDatabaseConfigurations(result, param); - persistMetaData(result); - } - return result; - } - - private static Collection getDatabaseNames(final InstanceContext instanceContext, - final Map databaseConfigs, final NewMetaDataPersistService persistService) { - return instanceContext.getInstance().getMetaData() instanceof JDBCInstanceMetaData ? databaseConfigs.keySet() : persistService.getDatabaseMetaDataService().loadAllDatabaseNames(); - } - - private static Map createEffectiveDatabaseConfigurations(final Collection databaseNames, - final Map databaseConfigs, final NewMetaDataPersistService persistService) { - return databaseNames.stream().collect(Collectors.toMap(each -> each, each -> createEffectiveDatabaseConfiguration(each, databaseConfigs, persistService))); - } - - private static DatabaseConfiguration createEffectiveDatabaseConfiguration(final String databaseName, - final Map databaseConfigs, final NewMetaDataPersistService persistService) { - closeGeneratedDataSources(databaseName, databaseConfigs); - Map dataSources = persistService.loadDataSourceConfigurations(databaseName); - Collection databaseRuleConfigs = persistService.getDatabaseRulePersistService().load(databaseName); - return new DataSourceGeneratedDatabaseConfiguration(dataSources, databaseRuleConfigs); - } - - private static void closeGeneratedDataSources(final String databaseName, final Map databaseConfigs) { - if (databaseConfigs.containsKey(databaseName) && !databaseConfigs.get(databaseName).getStorageUnits().isEmpty()) { - databaseConfigs.get(databaseName).getDataSources().values().forEach(each -> new DataSourcePoolDestroyer(each).asyncDestroy()); - } - } - - private static void checkDataSourceStates(final Map databaseConfigs, final Map storageNodes, final boolean force) { - Map storageDataSourceStates = getStorageDataSourceStates(storageNodes); - databaseConfigs.forEach((key, value) -> { - if (!value.getStorageUnits().isEmpty()) { - DataSourceStateManager.getInstance().initStates(key, value.getStorageUnits(), storageDataSourceStates, force); - } - }); - } - - private static Map getStorageDataSourceStates(final Map storageDataSourceStates) { - Map result = new HashMap<>(storageDataSourceStates.size(), 1F); - storageDataSourceStates.forEach((key, value) -> { - List values = Splitter.on(".").splitToList(key); - Preconditions.checkArgument(3 == values.size(), "Illegal data source of storage node."); - String databaseName = values.get(0); - String dataSourceName = values.get(2); - result.put(databaseName + "." + dataSourceName, DataSourceState.valueOf(value.getStatus().name())); - }); - return result; - } - - private static void persistDatabaseConfigurations(final MetaDataContexts metadataContexts, final ContextManagerBuilderParameter param) { - metadataContexts.getPersistService().persistGlobalRuleConfiguration(param.getGlobalRuleConfigs(), param.getProps()); - for (Entry entry : param.getDatabaseConfigs().entrySet()) { - String databaseName = entry.getKey(); - metadataContexts.getPersistService().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().persist(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()))); - } -} 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 b06f0b54855ab..7faf7229f9b57 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 @@ -263,19 +263,6 @@ void assertAlterRuleConfiguration() { assertThat(contextManager.getMetaDataContexts().getMetaData().getDatabase("foo_db").getRuleMetaData().getConfigurations().size(), is(1)); } - @Test - void assertAlterDataSourceConfiguration() { - ShardingSphereDatabase originalDatabaseMetaData = new ShardingSphereDatabase( - "foo_db", TypedSPILoader.getService(DatabaseType.class, "FIXTURE"), createOriginalResource(), createOriginalRuleMetaData(), Collections.emptyMap()); - when(metaDataContexts.getMetaData().getDatabase("foo_db")).thenReturn(originalDatabaseMetaData); - when(metaDataContexts.getMetaData().getGlobalRuleMetaData()).thenReturn(new RuleMetaData(Collections.emptyList())); - Map dataSourcePoolPropsMap = getToBeAlteredDataSourcePoolPropertiesMap(); - contextManager.getConfigurationContextManager().alterDataSourceUnitsConfiguration("foo_db", dataSourcePoolPropsMap); - assertThat(contextManager.getMetaDataContexts().getMetaData().getDatabase("foo_db").getResourceMetaData().getStorageUnits().size(), is(3)); - assertAlteredDataSource((MockedDataSource) contextManager.getMetaDataContexts().getMetaData().getDatabase("foo_db") - .getResourceMetaData().getDataSources().get(new StorageNode("foo_ds"))); - } - private ResourceMetaData createOriginalResource() { Map originalDataSources = new LinkedHashMap<>(2, 1F); originalDataSources.put("ds_1", new MockedDataSource()); diff --git a/mode/core/src/test/java/org/apache/shardingsphere/mode/manager/switcher/ResourceSwitchManagerTest.java b/mode/core/src/test/java/org/apache/shardingsphere/mode/manager/switcher/ResourceSwitchManagerTest.java deleted file mode 100644 index b6c31cfea8a3c..0000000000000 --- a/mode/core/src/test/java/org/apache/shardingsphere/mode/manager/switcher/ResourceSwitchManagerTest.java +++ /dev/null @@ -1,106 +0,0 @@ -/* - * 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.mode.manager.switcher; - -import org.apache.shardingsphere.infra.datasource.pool.props.domain.DataSourcePoolProperties; -import org.apache.shardingsphere.infra.metadata.database.resource.ResourceMetaData; -import org.apache.shardingsphere.infra.metadata.database.resource.node.StorageNode; -import org.apache.shardingsphere.test.fixture.jdbc.MockedDataSource; -import org.awaitility.Awaitility; -import org.junit.jupiter.api.Test; - -import javax.sql.DataSource; -import java.util.Collections; -import java.util.HashMap; -import java.util.LinkedHashMap; -import java.util.Map; -import java.util.concurrent.TimeUnit; - -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertTrue; - -class ResourceSwitchManagerTest { - - @Test - void assertCreate() { - Map dataSourceMap = createDataSourceMap(); - SwitchingResource actual = new ResourceSwitchManager().create(new ResourceMetaData(dataSourceMap), createToBeChangedDataSourcePoolPropertiesMap()); - assertNewDataSources(actual); - actual.closeStaleDataSources(); - assertStaleDataSources(dataSourceMap); - } - - @Test - void assertCreateByAlterDataSourcePoolProperties() { - Map dataSourceMap = new HashMap<>(3, 1F); - dataSourceMap.put("ds_0", new MockedDataSource()); - dataSourceMap.put("ds_1", new MockedDataSource()); - SwitchingResource actual = new ResourceSwitchManager().createByAlterDataSourcePoolProperties(new ResourceMetaData(dataSourceMap), Collections.emptyMap()); - assertTrue(actual.getNewDataSources().isEmpty()); - assertThat(actual.getStaleDataSources().size(), is(2)); - actual.closeStaleDataSources(); - assertStaleDataSource((MockedDataSource) dataSourceMap.get("ds_0")); - assertStaleDataSource((MockedDataSource) dataSourceMap.get("ds_1")); - } - - private Map createDataSourceMap() { - Map result = new HashMap<>(3, 1F); - result.put("not_change", new MockedDataSource()); - result.put("replace", new MockedDataSource()); - return result; - } - - private Map createToBeChangedDataSourcePoolPropertiesMap() { - Map result = new HashMap<>(3, 1F); - result.put("new", new DataSourcePoolProperties(MockedDataSource.class.getName(), getDataSourcePoolProperties(2))); - result.put("not_change", new DataSourcePoolProperties(MockedDataSource.class.getName(), getDataSourcePoolProperties(2))); - Map replaceProps = getDataSourcePoolProperties(3); - replaceProps.put("password", "new_pwd"); - result.put("replace", new DataSourcePoolProperties(MockedDataSource.class.getName(), replaceProps)); - return result; - } - - private Map getDataSourcePoolProperties(final int initialCapacity) { - Map result = new LinkedHashMap<>(initialCapacity, 1F); - result.put("url", new MockedDataSource().getUrl()); - result.put("username", "root"); - return result; - } - - private void assertNewDataSources(final SwitchingResource actual) { - assertThat(actual.getNewDataSources().size(), is(3)); - assertTrue(actual.getNewDataSources().containsKey(new StorageNode("not_change"))); - assertTrue(actual.getNewDataSources().containsKey(new StorageNode("new"))); - assertTrue(actual.getNewDataSources().containsKey(new StorageNode("replace"))); - } - - private void assertStaleDataSources(final Map originalDataSourceMap) { - assertStaleDataSource((MockedDataSource) originalDataSourceMap.get("replace")); - assertNotStaleDataSource((MockedDataSource) originalDataSourceMap.get("not_change")); - } - - private void assertStaleDataSource(final MockedDataSource dataSource) { - Awaitility.await().atMost(1L, TimeUnit.MINUTES).pollInterval(10L, TimeUnit.MILLISECONDS).until(dataSource::isClosed); - } - - private void assertNotStaleDataSource(final MockedDataSource dataSource) { - assertFalse(dataSource.isClosed()); - } -} 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 a4b14c10f2ca1..16966cf98fcb4 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 @@ -23,6 +23,7 @@ import org.apache.shardingsphere.infra.instance.InstanceContextAware; import org.apache.shardingsphere.infra.util.eventbus.EventBusContext; import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader; +import org.apache.shardingsphere.metadata.persist.MetaDataPersistService; import org.apache.shardingsphere.mode.lock.GlobalLockContext; import org.apache.shardingsphere.mode.manager.ContextManager; import org.apache.shardingsphere.mode.manager.ContextManagerAware; @@ -33,9 +34,9 @@ import org.apache.shardingsphere.mode.manager.cluster.coordinator.subscriber.ContextManagerSubscriberFacade; import org.apache.shardingsphere.mode.metadata.MetaDataContexts; import org.apache.shardingsphere.mode.metadata.MetaDataContextsFactory; -import org.apache.shardingsphere.metadata.persist.MetaDataPersistService; import org.apache.shardingsphere.mode.repository.cluster.ClusterPersistRepository; import org.apache.shardingsphere.mode.repository.cluster.ClusterPersistRepositoryConfiguration; +import org.apache.shardingsphere.mode.subsciber.RuleItemChangedSubscriber; import java.sql.SQLException; @@ -81,6 +82,7 @@ private void registerOnline(final RegistryCenter registryCenter, final ContextMa loadClusterStatus(registryCenter, contextManager); contextManager.getInstanceContext().getInstance().setLabels(param.getLabels()); contextManager.getInstanceContext().getAllClusterInstances().addAll(registryCenter.getComputeNodeStatusService().loadAllComputeNodeInstances()); + contextManager.getInstanceContext().getEventBusContext().register(new RuleItemChangedSubscriber(contextManager)); new ContextManagerSubscriberFacade(registryCenter, contextManager); } @@ -91,6 +93,6 @@ private void loadClusterStatus(final RegistryCenter registryCenter, final Contex @Override public String getType() { - return "Compatible_Cluster"; + return "Cluster"; } } diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/ClusterModeContextManager.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/ClusterModeContextManager.java index 2e25594130c5b..3fa8e85019ef7 100644 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/ClusterModeContextManager.java +++ b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/ClusterModeContextManager.java @@ -25,13 +25,19 @@ import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereView; import org.apache.shardingsphere.infra.metadata.database.schema.pojo.AlterSchemaMetaDataPOJO; import org.apache.shardingsphere.infra.metadata.database.schema.pojo.AlterSchemaPOJO; +import org.apache.shardingsphere.infra.metadata.version.MetaDataVersion; +import org.apache.shardingsphere.metadata.persist.service.config.database.DatabaseBasedPersistService; +import org.apache.shardingsphere.metadata.persist.service.config.global.GlobalPersistService; import org.apache.shardingsphere.metadata.persist.service.database.DatabaseMetaDataBasedPersistService; +import org.apache.shardingsphere.metadata.persist.service.version.MetaDataVersionBasedPersistService; import org.apache.shardingsphere.mode.manager.ContextManager; import org.apache.shardingsphere.mode.manager.ContextManagerAware; +import org.apache.shardingsphere.single.api.config.SingleRuleConfiguration; import java.util.Collection; +import java.util.Collections; +import java.util.LinkedHashMap; import java.util.Map; -import java.util.Map.Entry; import java.util.Properties; import java.util.stream.Collectors; @@ -49,6 +55,8 @@ public void createDatabase(final String databaseName) { @Override public void dropDatabase(final String databaseName) { + // TODO Avoid drop database to generate child node events + contextManager.getMetaDataContexts().getMetaData().dropDatabase(databaseName); contextManager.getMetaDataContexts().getPersistService().getDatabaseMetaDataService().dropDatabase(databaseName); } @@ -81,35 +89,65 @@ public void alterSchemaMetaData(final AlterSchemaMetaDataPOJO alterSchemaMetaDat Map tables = alterSchemaMetaDataPOJO.getAlteredTables().stream().collect(Collectors.toMap(ShardingSphereTable::getName, table -> table)); Map views = alterSchemaMetaDataPOJO.getAlteredViews().stream().collect(Collectors.toMap(ShardingSphereView::getName, view -> view)); DatabaseMetaDataBasedPersistService databaseMetaDataService = contextManager.getMetaDataContexts().getPersistService().getDatabaseMetaDataService(); - databaseMetaDataService.getTableMetaDataPersistService().persist(databaseName, schemaName, tables); - databaseMetaDataService.getViewMetaDataPersistService().persist(databaseName, schemaName, views); + MetaDataVersionBasedPersistService metaDataVersionBasedPersistService = contextManager.getMetaDataContexts().getPersistService().getMetaDataVersionPersistService(); + metaDataVersionBasedPersistService.switchActiveVersion(databaseMetaDataService.getTableMetaDataPersistService().persistSchemaMetaData(databaseName, schemaName, tables)); + metaDataVersionBasedPersistService.switchActiveVersion(databaseMetaDataService.getViewMetaDataPersistService().persistSchemaMetaData(databaseName, schemaName, views)); alterSchemaMetaDataPOJO.getDroppedTables().forEach(each -> databaseMetaDataService.getTableMetaDataPersistService().delete(databaseName, schemaName, each)); alterSchemaMetaDataPOJO.getDroppedViews().forEach(each -> databaseMetaDataService.getViewMetaDataPersistService().delete(databaseName, schemaName, each)); } @Override public void registerStorageUnits(final String databaseName, final Map toBeRegisteredProps) { - contextManager.getMetaDataContexts().getPersistService().getDataSourceUnitService().append(databaseName, toBeRegisteredProps); + contextManager.getMetaDataContexts().getPersistService().getDataSourceUnitService().persistConfig(databaseName, toBeRegisteredProps); } @Override public void alterStorageUnits(final String databaseName, final Map toBeUpdatedProps) { - contextManager.getMetaDataContexts().getPersistService().getDataSourceUnitService().append(databaseName, toBeUpdatedProps); + DatabaseBasedPersistService> dataSourceService = contextManager.getMetaDataContexts().getPersistService().getDataSourceUnitService(); + contextManager.getMetaDataContexts().getPersistService().getMetaDataVersionPersistService().switchActiveVersion(dataSourceService.persistConfig(databaseName, toBeUpdatedProps)); } @Override public void unregisterStorageUnits(final String databaseName, final Collection toBeDroppedStorageUnitNames) { - contextManager.getMetaDataContexts().getPersistService().getDataSourceUnitService().persist(databaseName, - getToBeReversedDataSourcePoolPropertiesMap(contextManager.getMetaDataContexts().getPersistService().getDataSourceUnitService().load(databaseName), toBeDroppedStorageUnitNames)); + contextManager.getMetaDataContexts().getPersistService().getDataSourceUnitService().deleteConfig(databaseName, + getToBeDroppedDataSourcePoolProperties(contextManager.getMetaDataContexts().getPersistService().getDataSourceUnitService().load(databaseName), toBeDroppedStorageUnitNames)); } - private Map getToBeReversedDataSourcePoolPropertiesMap(final Map propsMap, final Collection toBeDroppedResourceNames) { - return propsMap.entrySet().stream().filter(entry -> !toBeDroppedResourceNames.contains(entry.getKey())).collect(Collectors.toMap(Entry::getKey, Entry::getValue)); + private Map getToBeDroppedDataSourcePoolProperties(final Map propsMap, final Collection toBeDroppedResourceNames) { + Map result = new LinkedHashMap<>(); + for (String each : toBeDroppedResourceNames) { + if (propsMap.containsKey(each)) { + result.put(each, propsMap.get(each)); + } + } + return result; } @Override public void alterRuleConfiguration(final String databaseName, final Collection ruleConfigs) { - contextManager.getMetaDataContexts().getPersistService().getDatabaseRulePersistService().persist(databaseName, ruleConfigs); + ruleConfigs.removeIf(each -> !each.getClass().isAssignableFrom(SingleRuleConfiguration.class)); + contextManager.getMetaDataContexts().getPersistService().getMetaDataVersionPersistService() + .switchActiveVersion(contextManager.getMetaDataContexts().getPersistService().getDatabaseRulePersistService().persistConfig(databaseName, ruleConfigs)); + } + + @Override + public Collection alterRuleConfiguration(final String databaseName, final RuleConfiguration toBeAlteredRuleConfig) { + if (null != toBeAlteredRuleConfig) { + return contextManager.getMetaDataContexts().getPersistService().getDatabaseRulePersistService().persistConfig(databaseName, Collections.singleton(toBeAlteredRuleConfig)); + } + return Collections.emptyList(); + } + + @Override + public void removeRuleConfigurationItem(final String databaseName, final RuleConfiguration toBeRemovedRuleConfig) { + if (null != toBeRemovedRuleConfig) { + contextManager.getMetaDataContexts().getPersistService().getDatabaseRulePersistService().deleteConfig(databaseName, Collections.singleton(toBeRemovedRuleConfig)); + } + } + + @Override + public void removeRuleConfiguration(final String databaseName, final String ruleName) { + contextManager.getMetaDataContexts().getPersistService().getDatabaseRulePersistService().delete(databaseName, ruleName); } @Override @@ -117,9 +155,16 @@ public void alterGlobalRuleConfiguration(final Collection glo contextManager.getMetaDataContexts().getPersistService().getGlobalRuleService().persist(globalRuleConfigs); } + @Override + public void alterGlobalRuleConfiguration(final RuleConfiguration toBeAlteredRuleConfig) { + GlobalPersistService> globalRuleService = contextManager.getMetaDataContexts().getPersistService().getGlobalRuleService(); + contextManager.getMetaDataContexts().getPersistService().getMetaDataVersionPersistService().switchActiveVersion(globalRuleService.persistConfig(Collections.singleton(toBeAlteredRuleConfig))); + } + @Override public void alterProperties(final Properties props) { - contextManager.getMetaDataContexts().getPersistService().getPropsService().persist(props); + Collection versions = contextManager.getMetaDataContexts().getPersistService().getPropsService().persistConfig(props); + contextManager.getMetaDataContexts().getPersistService().getMetaDataVersionPersistService().switchActiveVersion(versions); } @Override diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/NewClusterContextManagerBuilder.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/NewClusterContextManagerBuilder.java deleted file mode 100644 index e459af5e5343f..0000000000000 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/NewClusterContextManagerBuilder.java +++ /dev/null @@ -1,99 +0,0 @@ -/* - * 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.mode.manager.cluster; - -import com.google.common.base.Preconditions; -import org.apache.shardingsphere.infra.instance.ComputeNodeInstance; -import org.apache.shardingsphere.infra.instance.InstanceContext; -import org.apache.shardingsphere.infra.instance.InstanceContextAware; -import org.apache.shardingsphere.infra.util.eventbus.EventBusContext; -import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader; -import org.apache.shardingsphere.metadata.persist.NewMetaDataPersistService; -import org.apache.shardingsphere.mode.lock.GlobalLockContext; -import org.apache.shardingsphere.mode.manager.ContextManager; -import org.apache.shardingsphere.mode.manager.ContextManagerAware; -import org.apache.shardingsphere.mode.manager.ContextManagerBuilder; -import org.apache.shardingsphere.mode.manager.ContextManagerBuilderParameter; -import org.apache.shardingsphere.mode.manager.cluster.coordinator.NewRegistryCenter; -import org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.workerid.generator.NewClusterWorkerIdGenerator; -import org.apache.shardingsphere.mode.manager.cluster.coordinator.subscriber.NewContextManagerSubscriberFacade; -import org.apache.shardingsphere.mode.metadata.MetaDataContexts; -import org.apache.shardingsphere.mode.metadata.NewMetaDataContextsFactory; -import org.apache.shardingsphere.mode.repository.cluster.ClusterPersistRepository; -import org.apache.shardingsphere.mode.repository.cluster.ClusterPersistRepositoryConfiguration; -import org.apache.shardingsphere.mode.subsciber.RuleItemChangedSubscriber; - -import java.sql.SQLException; - -/** - * TODO Rename ClusterContextManagerBuilder when metadata structure adjustment completed. #25485 - * New cluster context manager builder. - */ -public final class NewClusterContextManagerBuilder implements ContextManagerBuilder { - - @Override - public ContextManager build(final ContextManagerBuilderParameter param) throws SQLException { - ClusterPersistRepository repository = getClusterPersistRepository((ClusterPersistRepositoryConfiguration) param.getModeConfiguration().getRepository()); - NewRegistryCenter registryCenter = new NewRegistryCenter(repository, new EventBusContext(), param.getInstanceMetaData(), param.getDatabaseConfigs()); - InstanceContext instanceContext = buildInstanceContext(registryCenter, param); - if (registryCenter.getRepository() instanceof InstanceContextAware) { - ((InstanceContextAware) registryCenter.getRepository()).setInstanceContext(instanceContext); - } - NewMetaDataPersistService persistService = new NewMetaDataPersistService(repository); - MetaDataContexts metaDataContexts = NewMetaDataContextsFactory.create(persistService, param, instanceContext, registryCenter.getStorageNodeStatusService().loadStorageNodes()); - ContextManager result = new ContextManager(metaDataContexts, instanceContext); - setContextManagerAware(result); - registerOnline(registryCenter, param, result); - return result; - } - - private ClusterPersistRepository getClusterPersistRepository(final ClusterPersistRepositoryConfiguration config) { - Preconditions.checkNotNull(config, "Cluster persist repository configuration cannot be null."); - ClusterPersistRepository result = TypedSPILoader.getService(ClusterPersistRepository.class, config.getType(), config.getProps()); - result.init(config); - return result; - } - - private InstanceContext buildInstanceContext(final NewRegistryCenter registryCenter, final ContextManagerBuilderParameter param) { - return new InstanceContext(new ComputeNodeInstance(param.getInstanceMetaData()), new NewClusterWorkerIdGenerator(registryCenter, param.getInstanceMetaData()), - param.getModeConfiguration(), new NewClusterModeContextManager(), new GlobalLockContext(registryCenter.getGlobalLockPersistService()), registryCenter.getEventBusContext()); - } - - private void setContextManagerAware(final ContextManager contextManager) { - ((ContextManagerAware) contextManager.getInstanceContext().getModeContextManager()).setContextManagerAware(contextManager); - } - - private void registerOnline(final NewRegistryCenter registryCenter, final ContextManagerBuilderParameter param, final ContextManager contextManager) { - registryCenter.onlineInstance(contextManager.getInstanceContext().getInstance()); - loadClusterStatus(registryCenter, contextManager); - contextManager.getInstanceContext().getInstance().setLabels(param.getLabels()); - contextManager.getInstanceContext().getAllClusterInstances().addAll(registryCenter.getComputeNodeStatusService().loadAllComputeNodeInstances()); - contextManager.getInstanceContext().getEventBusContext().register(new RuleItemChangedSubscriber(contextManager)); - new NewContextManagerSubscriberFacade(registryCenter, contextManager); - } - - private void loadClusterStatus(final NewRegistryCenter registryCenter, final ContextManager contextManager) { - registryCenter.persistClusterState(contextManager); - contextManager.updateClusterState(registryCenter.getClusterStatusService().loadClusterStatus()); - } - - @Override - public String getType() { - return "Cluster"; - } -} diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/NewClusterModeContextManager.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/NewClusterModeContextManager.java deleted file mode 100644 index 306730e536e06..0000000000000 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/NewClusterModeContextManager.java +++ /dev/null @@ -1,175 +0,0 @@ -/* - * 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.mode.manager.cluster; - -import org.apache.shardingsphere.infra.config.rule.RuleConfiguration; -import org.apache.shardingsphere.infra.datasource.pool.props.domain.DataSourcePoolProperties; -import org.apache.shardingsphere.infra.instance.mode.ModeContextManager; -import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema; -import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereTable; -import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereView; -import org.apache.shardingsphere.infra.metadata.database.schema.pojo.AlterSchemaMetaDataPOJO; -import org.apache.shardingsphere.infra.metadata.database.schema.pojo.AlterSchemaPOJO; -import org.apache.shardingsphere.infra.metadata.version.MetaDataVersion; -import org.apache.shardingsphere.metadata.persist.service.config.database.DatabaseBasedPersistService; -import org.apache.shardingsphere.metadata.persist.service.config.global.GlobalPersistService; -import org.apache.shardingsphere.metadata.persist.service.database.DatabaseMetaDataBasedPersistService; -import org.apache.shardingsphere.metadata.persist.service.version.MetaDataVersionBasedPersistService; -import org.apache.shardingsphere.mode.manager.ContextManager; -import org.apache.shardingsphere.mode.manager.ContextManagerAware; -import org.apache.shardingsphere.single.api.config.SingleRuleConfiguration; - -import java.util.Collection; -import java.util.Collections; -import java.util.LinkedHashMap; -import java.util.Map; -import java.util.Properties; -import java.util.stream.Collectors; - -/** - * TODO Rename to ClusterModeContextManager after meta data refactor completed - * New cluster mode context manager. - */ -public final class NewClusterModeContextManager implements ModeContextManager, ContextManagerAware { - - private ContextManager contextManager; - - @Override - public void createDatabase(final String databaseName) { - contextManager.getMetaDataContexts().getPersistService().getDatabaseMetaDataService().addDatabase(databaseName); - } - - @Override - public void dropDatabase(final String databaseName) { - // TODO Avoid drop database to generate child node events - contextManager.getMetaDataContexts().getMetaData().dropDatabase(databaseName); - contextManager.getMetaDataContexts().getPersistService().getDatabaseMetaDataService().dropDatabase(databaseName); - } - - @Override - public void createSchema(final String databaseName, final String schemaName) { - contextManager.getMetaDataContexts().getPersistService().getDatabaseMetaDataService().addSchema(databaseName, schemaName); - } - - @Override - public void alterSchema(final AlterSchemaPOJO alterSchemaPOJO) { - String databaseName = alterSchemaPOJO.getDatabaseName(); - String schemaName = alterSchemaPOJO.getSchemaName(); - ShardingSphereSchema schema = contextManager.getMetaDataContexts().getMetaData().getDatabase(databaseName).getSchema(schemaName); - DatabaseMetaDataBasedPersistService databaseMetaDataService = contextManager.getMetaDataContexts().getPersistService().getDatabaseMetaDataService(); - databaseMetaDataService.persist(databaseName, alterSchemaPOJO.getRenameSchemaName(), schema); - databaseMetaDataService.getViewMetaDataPersistService().persist(databaseName, alterSchemaPOJO.getRenameSchemaName(), schema.getViews()); - databaseMetaDataService.dropSchema(databaseName, schemaName); - } - - @Override - public void dropSchema(final String databaseName, final Collection schemaNames) { - DatabaseMetaDataBasedPersistService databaseMetaDataService = contextManager.getMetaDataContexts().getPersistService().getDatabaseMetaDataService(); - schemaNames.forEach(each -> databaseMetaDataService.dropSchema(databaseName, each)); - } - - @Override - public void alterSchemaMetaData(final AlterSchemaMetaDataPOJO alterSchemaMetaDataPOJO) { - String databaseName = alterSchemaMetaDataPOJO.getDatabaseName(); - 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)); - DatabaseMetaDataBasedPersistService databaseMetaDataService = contextManager.getMetaDataContexts().getPersistService().getDatabaseMetaDataService(); - MetaDataVersionBasedPersistService metaDataVersionBasedPersistService = contextManager.getMetaDataContexts().getPersistService().getMetaDataVersionPersistService(); - metaDataVersionBasedPersistService.switchActiveVersion(databaseMetaDataService.getTableMetaDataPersistService().persistSchemaMetaData(databaseName, schemaName, tables)); - metaDataVersionBasedPersistService.switchActiveVersion(databaseMetaDataService.getViewMetaDataPersistService().persistSchemaMetaData(databaseName, schemaName, views)); - alterSchemaMetaDataPOJO.getDroppedTables().forEach(each -> databaseMetaDataService.getTableMetaDataPersistService().delete(databaseName, schemaName, each)); - alterSchemaMetaDataPOJO.getDroppedViews().forEach(each -> databaseMetaDataService.getViewMetaDataPersistService().delete(databaseName, schemaName, each)); - } - - @Override - public void registerStorageUnits(final String databaseName, final Map toBeRegisteredProps) { - contextManager.getMetaDataContexts().getPersistService().getDataSourceUnitService().persistConfig(databaseName, toBeRegisteredProps); - } - - @Override - public void alterStorageUnits(final String databaseName, final Map toBeUpdatedProps) { - DatabaseBasedPersistService> dataSourceService = contextManager.getMetaDataContexts().getPersistService().getDataSourceUnitService(); - contextManager.getMetaDataContexts().getPersistService().getMetaDataVersionPersistService().switchActiveVersion(dataSourceService.persistConfig(databaseName, toBeUpdatedProps)); - } - - @Override - public void unregisterStorageUnits(final String databaseName, final Collection toBeDroppedStorageUnitNames) { - contextManager.getMetaDataContexts().getPersistService().getDataSourceUnitService().deleteConfig(databaseName, - getToBeDroppedDataSourcePoolProperties(contextManager.getMetaDataContexts().getPersistService().getDataSourceUnitService().load(databaseName), toBeDroppedStorageUnitNames)); - } - - private Map getToBeDroppedDataSourcePoolProperties(final Map propsMap, final Collection toBeDroppedResourceNames) { - Map result = new LinkedHashMap<>(); - for (String each : toBeDroppedResourceNames) { - if (propsMap.containsKey(each)) { - result.put(each, propsMap.get(each)); - } - } - return result; - } - - @Override - public void alterRuleConfiguration(final String databaseName, final Collection ruleConfigs) { - ruleConfigs.removeIf(each -> !each.getClass().isAssignableFrom(SingleRuleConfiguration.class)); - contextManager.getMetaDataContexts().getPersistService().getMetaDataVersionPersistService() - .switchActiveVersion(contextManager.getMetaDataContexts().getPersistService().getDatabaseRulePersistService().persistConfig(databaseName, ruleConfigs)); - } - - @Override - public Collection alterRuleConfiguration(final String databaseName, final RuleConfiguration toBeAlteredRuleConfig) { - if (null != toBeAlteredRuleConfig) { - return contextManager.getMetaDataContexts().getPersistService().getDatabaseRulePersistService().persistConfig(databaseName, Collections.singleton(toBeAlteredRuleConfig)); - } - return Collections.emptyList(); - } - - @Override - public void removeRuleConfigurationItem(final String databaseName, final RuleConfiguration toBeRemovedRuleConfig) { - if (null != toBeRemovedRuleConfig) { - contextManager.getMetaDataContexts().getPersistService().getDatabaseRulePersistService().deleteConfig(databaseName, Collections.singleton(toBeRemovedRuleConfig)); - } - } - - @Override - public void removeRuleConfiguration(final String databaseName, final String ruleName) { - contextManager.getMetaDataContexts().getPersistService().getDatabaseRulePersistService().delete(databaseName, ruleName); - } - - @Override - public void alterGlobalRuleConfiguration(final Collection globalRuleConfigs) { - contextManager.getMetaDataContexts().getPersistService().getGlobalRuleService().persist(globalRuleConfigs); - } - - @Override - public void alterGlobalRuleConfiguration(final RuleConfiguration toBeAlteredRuleConfig) { - GlobalPersistService> globalRuleService = contextManager.getMetaDataContexts().getPersistService().getGlobalRuleService(); - contextManager.getMetaDataContexts().getPersistService().getMetaDataVersionPersistService().switchActiveVersion(globalRuleService.persistConfig(Collections.singleton(toBeAlteredRuleConfig))); - } - - @Override - public void alterProperties(final Properties props) { - Collection versions = contextManager.getMetaDataContexts().getPersistService().getPropsService().persistConfig(props); - contextManager.getMetaDataContexts().getPersistService().getMetaDataVersionPersistService().switchActiveVersion(versions); - } - - @Override - public void setContextManagerAware(final ContextManager contextManager) { - this.contextManager = contextManager; - } -} diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/NewRegistryCenter.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/NewRegistryCenter.java deleted file mode 100644 index fb4dc1ae407a9..0000000000000 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/NewRegistryCenter.java +++ /dev/null @@ -1,129 +0,0 @@ -/* - * 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.mode.manager.cluster.coordinator; - -import com.google.common.base.Strings; -import lombok.Getter; -import org.apache.shardingsphere.infra.config.database.DatabaseConfiguration; -import org.apache.shardingsphere.infra.instance.ComputeNodeInstance; -import org.apache.shardingsphere.infra.instance.metadata.InstanceMetaData; -import org.apache.shardingsphere.infra.instance.metadata.jdbc.JDBCInstanceMetaData; -import org.apache.shardingsphere.infra.util.eventbus.EventBusContext; -import org.apache.shardingsphere.metadata.persist.node.ComputeNode; -import org.apache.shardingsphere.mode.manager.ContextManager; -import org.apache.shardingsphere.mode.manager.cluster.coordinator.lock.GlobalLockPersistService; -import org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.NewGovernanceWatcherFactory; -import org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.metadata.subscriber.ShardingSphereSchemaDataRegistrySubscriber; -import org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.process.subscriber.ClusterProcessSubscriber; -import org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.status.cluster.service.ClusterStatusService; -import org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.status.cluster.subscriber.ClusterStatusSubscriber; -import org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.status.compute.service.ComputeNodeStatusService; -import org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.status.compute.subscriber.NewComputeNodeStatusSubscriber; -import org.apache.shardingsphere.mode.storage.service.StorageNodeStatusService; -import org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.status.storage.subscriber.StorageNodeStatusSubscriber; -import org.apache.shardingsphere.mode.repository.cluster.ClusterPersistRepository; -import org.apache.shardingsphere.mode.repository.cluster.lock.holder.DistributedLockHolder; -import org.apache.shardingsphere.mode.repository.cluster.lock.impl.props.DefaultLockTypedProperties; - -import java.util.Map; -import java.util.Properties; - -/** - * TODO replace the old RegistryCenter after meta data refactor completed - * New registry center. - */ -public final class NewRegistryCenter { - - @Getter - private final ClusterPersistRepository repository; - - @Getter - private final StorageNodeStatusService storageNodeStatusService; - - @Getter - private final ClusterStatusService clusterStatusService; - - @Getter - private final ComputeNodeStatusService computeNodeStatusService; - - @Getter - private final GlobalLockPersistService globalLockPersistService; - - @Getter - private final EventBusContext eventBusContext; - - private final InstanceMetaData instanceMetaData; - - private final Map databaseConfigs; - - private final NewGovernanceWatcherFactory listenerFactory; - - public NewRegistryCenter(final ClusterPersistRepository repository, final EventBusContext eventBusContext, - final InstanceMetaData instanceMetaData, final Map databaseConfigs) { - this.repository = repository; - this.eventBusContext = eventBusContext; - this.instanceMetaData = instanceMetaData; - this.databaseConfigs = databaseConfigs; - storageNodeStatusService = new StorageNodeStatusService(repository); - clusterStatusService = new ClusterStatusService(repository); - computeNodeStatusService = new ComputeNodeStatusService(repository); - globalLockPersistService = new GlobalLockPersistService(initDistributedLockHolder(repository)); - listenerFactory = new NewGovernanceWatcherFactory(repository, eventBusContext, getJDBCDatabaseName()); - createSubscribers(repository); - } - - private DistributedLockHolder initDistributedLockHolder(final ClusterPersistRepository repository) { - DistributedLockHolder distributedLockHolder = repository.getDistributedLockHolder(); - return null == distributedLockHolder ? new DistributedLockHolder("default", repository, new DefaultLockTypedProperties(new Properties())) : distributedLockHolder; - } - - private String getJDBCDatabaseName() { - return instanceMetaData instanceof JDBCInstanceMetaData ? databaseConfigs.keySet().stream().findFirst().orElse(null) : null; - } - - private void createSubscribers(final ClusterPersistRepository repository) { - new NewComputeNodeStatusSubscriber(this, repository); - new ClusterStatusSubscriber(repository, eventBusContext); - new StorageNodeStatusSubscriber(repository, eventBusContext); - new ClusterProcessSubscriber(repository, eventBusContext); - new ShardingSphereSchemaDataRegistrySubscriber(repository, globalLockPersistService, eventBusContext); - } - - /** - * Online instance. - * - * @param computeNodeInstance compute node instance - */ - public void onlineInstance(final ComputeNodeInstance computeNodeInstance) { - computeNodeStatusService.registerOnline(computeNodeInstance.getMetaData()); - computeNodeStatusService.persistInstanceLabels(computeNodeInstance.getCurrentInstanceId(), computeNodeInstance.getLabels()); - computeNodeStatusService.persistInstanceState(computeNodeInstance.getCurrentInstanceId(), computeNodeInstance.getState()); - listenerFactory.watchListeners(); - } - - /** - * Persist cluster state. - * - * @param contextManager context manager - */ - public void persistClusterState(final ContextManager contextManager) { - if (Strings.isNullOrEmpty(repository.getDirectly(ComputeNode.getClusterStatusNodePath()))) { - clusterStatusService.persistClusterState(contextManager.getClusterStateContext()); - } - } -} diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/RegistryCenter.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/RegistryCenter.java index 26bfe66821f40..08f2909502418 100644 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/RegistryCenter.java +++ b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/RegistryCenter.java @@ -29,13 +29,13 @@ import org.apache.shardingsphere.mode.manager.cluster.coordinator.lock.GlobalLockPersistService; import org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.GovernanceWatcherFactory; import org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.metadata.subscriber.ShardingSphereSchemaDataRegistrySubscriber; +import org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.process.subscriber.ClusterProcessSubscriber; import org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.status.cluster.service.ClusterStatusService; import org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.status.cluster.subscriber.ClusterStatusSubscriber; import org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.status.compute.service.ComputeNodeStatusService; import org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.status.compute.subscriber.ComputeNodeStatusSubscriber; import org.apache.shardingsphere.mode.storage.service.StorageNodeStatusService; import org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.status.storage.subscriber.StorageNodeStatusSubscriber; -import org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.process.subscriber.ClusterProcessSubscriber; import org.apache.shardingsphere.mode.repository.cluster.ClusterPersistRepository; import org.apache.shardingsphere.mode.repository.cluster.lock.holder.DistributedLockHolder; import org.apache.shardingsphere.mode.repository.cluster.lock.impl.props.DefaultLockTypedProperties; diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/GovernanceWatcher.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/GovernanceWatcher.java index 767be9958fdca..bde52b668abf0 100644 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/GovernanceWatcher.java +++ b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/GovernanceWatcher.java @@ -17,9 +17,9 @@ package org.apache.shardingsphere.mode.manager.cluster.coordinator.registry; +import org.apache.shardingsphere.infra.spi.annotation.SingletonSPI; import org.apache.shardingsphere.mode.event.DataChangedEvent; import org.apache.shardingsphere.mode.event.DataChangedEvent.Type; -import org.apache.shardingsphere.infra.spi.annotation.SingletonSPI; import java.util.Collection; import java.util.Optional; diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/GovernanceWatcherFactory.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/GovernanceWatcherFactory.java index 6703efe6cecdc..6b235b3a6c163 100644 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/GovernanceWatcherFactory.java +++ b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/GovernanceWatcherFactory.java @@ -19,8 +19,8 @@ import lombok.RequiredArgsConstructor; import org.apache.shardingsphere.infra.util.eventbus.EventBusContext; -import org.apache.shardingsphere.mode.repository.cluster.ClusterPersistRepository; import org.apache.shardingsphere.infra.spi.ShardingSphereServiceLoader; +import org.apache.shardingsphere.mode.repository.cluster.ClusterPersistRepository; /** * Governance watcher factory. diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/NewGovernanceWatcher.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/NewGovernanceWatcher.java deleted file mode 100644 index f2f9350b6d87d..0000000000000 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/NewGovernanceWatcher.java +++ /dev/null @@ -1,58 +0,0 @@ -/* - * 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.mode.manager.cluster.coordinator.registry; - -import org.apache.shardingsphere.infra.spi.annotation.SingletonSPI; -import org.apache.shardingsphere.mode.event.DataChangedEvent; -import org.apache.shardingsphere.mode.event.DataChangedEvent.Type; - -import java.util.Collection; -import java.util.Optional; - -/** - * TODO Rename GovernanceWatcher when metadata structure adjustment completed. #25485 - * Governance watcher. - * - * @param type of event - */ -@SingletonSPI -public interface NewGovernanceWatcher { - - /** - * Get watching keys. - * - * @param databaseName database name - * @return watching keys - */ - Collection getWatchingKeys(String databaseName); - - /** - * Get watching types. - * - * @return watching types - */ - Collection getWatchingTypes(); - - /** - * Create governance event. - * - * @param event registry center data changed event - * @return governance event - */ - Optional createGovernanceEvent(DataChangedEvent event); -} diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/NewGovernanceWatcherFactory.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/NewGovernanceWatcherFactory.java deleted file mode 100644 index 398e657c28df5..0000000000000 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/NewGovernanceWatcherFactory.java +++ /dev/null @@ -1,60 +0,0 @@ -/* - * 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.mode.manager.cluster.coordinator.registry; - -import lombok.RequiredArgsConstructor; -import org.apache.shardingsphere.infra.util.eventbus.EventBusContext; -import org.apache.shardingsphere.infra.spi.ShardingSphereServiceLoader; -import org.apache.shardingsphere.mode.repository.cluster.ClusterPersistRepository; - -/** - * TODO replace the old GovernanceWatcherFactory after meta data refactor completed - * New governance watcher factory. - */ -@RequiredArgsConstructor -public final class NewGovernanceWatcherFactory { - - private final ClusterPersistRepository repository; - - private final EventBusContext eventBusContext; - - private final String databaseName; - - /** - * Watch listeners. - */ - public void watchListeners() { - for (NewGovernanceWatcher each : ShardingSphereServiceLoader.getServiceInstances(NewGovernanceWatcher.class)) { - watch(each); - } - } - - private void watch(final NewGovernanceWatcher listener) { - for (String each : listener.getWatchingKeys(databaseName)) { - watch(each, listener); - } - } - - private void watch(final String watchingKey, final NewGovernanceWatcher listener) { - repository.watch(watchingKey, dataChangedEventListener -> { - if (listener.getWatchingTypes().contains(dataChangedEventListener.getType())) { - listener.createGovernanceEvent(dataChangedEventListener).ifPresent(eventBusContext::post); - } - }); - } -} diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/config/watcher/GlobalRuleChangedWatcher.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/config/watcher/GlobalRuleChangedWatcher.java index ca7c0a4e04db2..7ddf0605718f1 100644 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/config/watcher/GlobalRuleChangedWatcher.java +++ b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/config/watcher/GlobalRuleChangedWatcher.java @@ -17,17 +17,15 @@ package org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.config.watcher; -import com.google.common.base.Preconditions; -import org.apache.shardingsphere.infra.config.rule.RuleConfiguration; -import org.apache.shardingsphere.infra.yaml.config.pojo.rule.YamlRuleConfiguration; -import org.apache.shardingsphere.infra.yaml.config.swapper.rule.YamlRuleConfigurationSwapperEngine; -import org.apache.shardingsphere.infra.util.yaml.YamlEngine; -import org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.GovernanceWatcher; -import org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.config.event.rule.GlobalRuleConfigurationsChangedEvent; +import org.apache.shardingsphere.infra.rule.event.GovernanceEvent; +import org.apache.shardingsphere.infra.config.nodepath.GlobalNodePath; import org.apache.shardingsphere.metadata.persist.node.GlobalNode; import org.apache.shardingsphere.mode.event.DataChangedEvent; import org.apache.shardingsphere.mode.event.DataChangedEvent.Type; +import org.apache.shardingsphere.mode.event.config.global.AlterGlobalRuleConfigurationEvent; +import org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.GovernanceWatcher; +import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.Optional; @@ -35,27 +33,23 @@ /** * Global rule changed watcher. */ -public final class GlobalRuleChangedWatcher implements GovernanceWatcher { +public final class GlobalRuleChangedWatcher implements GovernanceWatcher { @Override public Collection getWatchingKeys(final String databaseName) { - return Collections.singleton(GlobalNode.getGlobalRuleNode()); + return Collections.singleton(GlobalNode.getGlobalRuleRootNode()); } @Override public Collection getWatchingTypes() { - return Collections.singleton(Type.UPDATED); + return Arrays.asList(Type.ADDED, Type.UPDATED); } @Override - public Optional createGovernanceEvent(final DataChangedEvent event) { - return GlobalNode.getGlobalRuleNode().equals(event.getKey()) ? Optional.of(new GlobalRuleConfigurationsChangedEvent(getGlobalRuleConfigurations(event))) : Optional.empty(); - } - - @SuppressWarnings("unchecked") - private Collection getGlobalRuleConfigurations(final DataChangedEvent event) { - Collection globalRuleConfigs = YamlEngine.unmarshal(event.getValue(), Collection.class); - Preconditions.checkState(!globalRuleConfigs.isEmpty(), "No available global rule to load for governance."); - return new YamlRuleConfigurationSwapperEngine().swapToRuleConfigurations(globalRuleConfigs); + public Optional createGovernanceEvent(final DataChangedEvent event) { + if (GlobalNodePath.isRuleActiveVersionPath(event.getKey())) { + return GlobalNodePath.getRuleName(event.getKey()).map(optional -> new AlterGlobalRuleConfigurationEvent(optional, event.getKey(), event.getValue())); + } + return Optional.empty(); } } diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/config/watcher/NewGlobalRuleChangedWatcher.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/config/watcher/NewGlobalRuleChangedWatcher.java deleted file mode 100644 index c90fea39c8fa7..0000000000000 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/config/watcher/NewGlobalRuleChangedWatcher.java +++ /dev/null @@ -1,56 +0,0 @@ -/* - * 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.mode.manager.cluster.coordinator.registry.config.watcher; - -import org.apache.shardingsphere.infra.rule.event.GovernanceEvent; -import org.apache.shardingsphere.infra.config.nodepath.GlobalNodePath; -import org.apache.shardingsphere.metadata.persist.node.GlobalNode; -import org.apache.shardingsphere.mode.event.DataChangedEvent; -import org.apache.shardingsphere.mode.event.DataChangedEvent.Type; -import org.apache.shardingsphere.mode.event.config.global.AlterGlobalRuleConfigurationEvent; -import org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.NewGovernanceWatcher; - -import java.util.Arrays; -import java.util.Collection; -import java.util.Collections; -import java.util.Optional; - -/** - * TODO Rename GlobalRuleChangedWatcher when metadata structure adjustment completed. #25485 - * Global rule changed watcher. - */ -public final class NewGlobalRuleChangedWatcher implements NewGovernanceWatcher { - - @Override - public Collection getWatchingKeys(final String databaseName) { - return Collections.singleton(GlobalNode.getGlobalRuleNode()); - } - - @Override - public Collection getWatchingTypes() { - return Arrays.asList(Type.ADDED, Type.UPDATED); - } - - @Override - public Optional createGovernanceEvent(final DataChangedEvent event) { - if (GlobalNodePath.isRuleActiveVersionPath(event.getKey())) { - return GlobalNodePath.getRuleName(event.getKey()).map(optional -> new AlterGlobalRuleConfigurationEvent(optional, event.getKey(), event.getValue())); - } - return Optional.empty(); - } -} diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/config/watcher/NewPropertiesChangedWatcher.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/config/watcher/NewPropertiesChangedWatcher.java deleted file mode 100644 index fbe8f756f6e18..0000000000000 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/config/watcher/NewPropertiesChangedWatcher.java +++ /dev/null @@ -1,55 +0,0 @@ -/* - * 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.mode.manager.cluster.coordinator.registry.config.watcher; - -import org.apache.shardingsphere.infra.config.nodepath.GlobalNodePath; -import org.apache.shardingsphere.metadata.persist.node.GlobalNode; -import org.apache.shardingsphere.mode.event.DataChangedEvent; -import org.apache.shardingsphere.mode.event.DataChangedEvent.Type; -import org.apache.shardingsphere.mode.event.config.global.AlterPropertiesEvent; -import org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.NewGovernanceWatcher; - -import java.util.Arrays; -import java.util.Collections; -import java.util.Collection; -import java.util.Optional; - -/** - * TODO Rename PropertiesChangedWatcher when metadata structure adjustment completed. #25485 - * Properties changed watcher. - */ -public final class NewPropertiesChangedWatcher implements NewGovernanceWatcher { - - @Override - public Collection getWatchingKeys(final String databaseName) { - return Collections.singleton(GlobalNode.getPropsPath()); - } - - @Override - public Collection getWatchingTypes() { - return Arrays.asList(Type.ADDED, Type.UPDATED); - } - - @Override - public Optional createGovernanceEvent(final DataChangedEvent event) { - if (GlobalNodePath.isPropsActiveVersionPath(event.getKey())) { - return Optional.of(new AlterPropertiesEvent(event.getKey(), event.getValue())); - } - return Optional.empty(); - } -} diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/config/watcher/PropertiesChangedWatcher.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/config/watcher/PropertiesChangedWatcher.java index d8e4041d6eaee..0bef3501fdf09 100644 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/config/watcher/PropertiesChangedWatcher.java +++ b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/config/watcher/PropertiesChangedWatcher.java @@ -17,27 +17,26 @@ package org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.config.watcher; +import org.apache.shardingsphere.infra.config.nodepath.GlobalNodePath; import org.apache.shardingsphere.metadata.persist.node.GlobalNode; -import org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.GovernanceWatcher; -import org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.config.event.props.PropertiesChangedEvent; import org.apache.shardingsphere.mode.event.DataChangedEvent; import org.apache.shardingsphere.mode.event.DataChangedEvent.Type; -import org.apache.shardingsphere.infra.util.yaml.YamlEngine; +import org.apache.shardingsphere.mode.event.config.global.AlterPropertiesEvent; +import org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.GovernanceWatcher; import java.util.Arrays; -import java.util.Collection; import java.util.Collections; +import java.util.Collection; import java.util.Optional; -import java.util.Properties; /** * Properties changed watcher. */ -public final class PropertiesChangedWatcher implements GovernanceWatcher { +public final class PropertiesChangedWatcher implements GovernanceWatcher { @Override public Collection getWatchingKeys(final String databaseName) { - return Collections.singleton(GlobalNode.getPropsPath()); + return Collections.singleton(GlobalNode.getPropsRootNode()); } @Override @@ -46,7 +45,10 @@ public Collection getWatchingTypes() { } @Override - public Optional createGovernanceEvent(final DataChangedEvent event) { - return GlobalNode.getPropsPath().equals(event.getKey()) ? Optional.of(new PropertiesChangedEvent(YamlEngine.unmarshal(event.getValue(), Properties.class))) : Optional.empty(); + public Optional createGovernanceEvent(final DataChangedEvent event) { + if (GlobalNodePath.isPropsActiveVersionPath(event.getKey())) { + return Optional.of(new AlterPropertiesEvent(event.getKey(), event.getValue())); + } + return Optional.empty(); } } diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/data/ShardingSphereDataChangedWatcher.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/data/ShardingSphereDataChangedWatcher.java index e28ed0704eb6d..04d6a87f9eb87 100644 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/data/ShardingSphereDataChangedWatcher.java +++ b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/data/ShardingSphereDataChangedWatcher.java @@ -24,7 +24,6 @@ import org.apache.shardingsphere.metadata.persist.node.ShardingSphereDataNode; import org.apache.shardingsphere.infra.rule.event.GovernanceEvent; import org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.GovernanceWatcher; -import org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.NewGovernanceWatcher; import org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.data.event.DatabaseDataAddedEvent; import org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.data.event.DatabaseDataDeletedEvent; import org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.data.event.SchemaDataAddedEvent; @@ -43,7 +42,7 @@ /** * ShardingSphere data changed watcher. */ -public final class ShardingSphereDataChangedWatcher implements GovernanceWatcher, NewGovernanceWatcher { +public final class ShardingSphereDataChangedWatcher implements GovernanceWatcher { @Override public Collection getWatchingKeys(final String databaseName) { diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/metadata/watcher/MetaDataChangedWatcher.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/metadata/watcher/MetaDataChangedWatcher.java index 2831ea8029f8a..d95c1655948f1 100644 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/metadata/watcher/MetaDataChangedWatcher.java +++ b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/metadata/watcher/MetaDataChangedWatcher.java @@ -18,52 +18,42 @@ package org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.metadata.watcher; import com.google.common.base.Preconditions; -import com.google.common.base.Strings; -import org.apache.shardingsphere.infra.config.rule.RuleConfiguration; -import org.apache.shardingsphere.infra.datasource.pool.props.domain.DataSourcePoolProperties; -import org.apache.shardingsphere.infra.metadata.database.schema.manager.SystemSchemaManager; import org.apache.shardingsphere.infra.rule.event.GovernanceEvent; -import org.apache.shardingsphere.infra.util.yaml.YamlEngine; -import org.apache.shardingsphere.infra.yaml.config.pojo.rule.YamlRuleConfiguration; -import org.apache.shardingsphere.infra.yaml.config.swapper.resource.YamlDataSourceConfigurationSwapper; -import org.apache.shardingsphere.infra.yaml.config.swapper.rule.YamlRuleConfigurationSwapperEngine; -import org.apache.shardingsphere.infra.yaml.schema.pojo.YamlShardingSphereTable; -import org.apache.shardingsphere.infra.yaml.schema.pojo.YamlShardingSphereView; -import org.apache.shardingsphere.infra.yaml.schema.swapper.YamlTableSwapper; -import org.apache.shardingsphere.infra.yaml.schema.swapper.YamlViewSwapper; import org.apache.shardingsphere.metadata.persist.node.DatabaseMetaDataNode; import org.apache.shardingsphere.mode.event.DataChangedEvent; import org.apache.shardingsphere.mode.event.DataChangedEvent.Type; -import org.apache.shardingsphere.mode.event.schema.TableMetaDataChangedEvent; -import org.apache.shardingsphere.mode.event.schema.ViewMetaDataChangedEvent; +import org.apache.shardingsphere.mode.event.datasource.nodes.AlterStorageNodeEvent; +import org.apache.shardingsphere.mode.event.datasource.nodes.RegisterStorageNodeEvent; +import org.apache.shardingsphere.mode.event.datasource.nodes.UnregisterStorageNodeEvent; +import org.apache.shardingsphere.mode.event.datasource.unit.AlterStorageUnitEvent; +import org.apache.shardingsphere.mode.event.datasource.unit.RegisterStorageUnitEvent; +import org.apache.shardingsphere.mode.event.datasource.unit.UnregisterStorageUnitEvent; +import org.apache.shardingsphere.mode.event.schema.table.AlterTableEvent; +import org.apache.shardingsphere.mode.event.schema.table.DropTableEvent; +import org.apache.shardingsphere.mode.event.schema.view.AlterViewEvent; +import org.apache.shardingsphere.mode.event.schema.view.DropViewEvent; import org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.GovernanceWatcher; -import org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.config.event.datasource.DataSourceNodesChangedEvent; -import org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.config.event.datasource.DataSourceUnitsChangedEvent; -import org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.config.event.rule.RuleConfigurationsChangedEvent; import org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.metadata.event.DatabaseAddedEvent; 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.builder.RuleConfigurationEventBuilder; import java.util.Arrays; import java.util.Collection; import java.util.Collections; -import java.util.HashMap; -import java.util.LinkedHashMap; -import java.util.LinkedList; -import java.util.Map; -import java.util.Map.Entry; import java.util.Optional; -import java.util.stream.Collectors; /** * Meta data changed watcher. */ public final class MetaDataChangedWatcher implements GovernanceWatcher { + private final RuleConfigurationEventBuilder ruleConfigurationEventBuilder = new RuleConfigurationEventBuilder(); + @Override public Collection getWatchingKeys(final String databaseName) { - return null == databaseName ? Collections.singleton(DatabaseMetaDataNode.getMetaDataNodePath()) + return null == databaseName ? Collections.singleton(DatabaseMetaDataNode.getMetaDataNode()) : Collections.singleton(DatabaseMetaDataNode.getDatabaseNamePath(databaseName)); } @@ -74,134 +64,105 @@ public Collection getWatchingTypes() { @Override public Optional createGovernanceEvent(final DataChangedEvent event) { - if (databaseChanged(event)) { - return createDatabaseChangedEvent(event); + String key = event.getKey(); + Optional databaseName = DatabaseMetaDataNode.getDatabaseName(key); + if (databaseName.isPresent()) { + return createDatabaseChangedEvent(databaseName.get(), event); } - if (schemaChanged(event)) { - return createSchemaChangedEvent(event); + databaseName = DatabaseMetaDataNode.getDatabaseNameBySchemaNode(key); + Optional schemaName = DatabaseMetaDataNode.getSchemaName(key); + if (databaseName.isPresent() && schemaName.isPresent()) { + return createSchemaChangedEvent(databaseName.get(), schemaName.get(), event); } - if (schemaMetaDataChanged(event)) { - return createSchemaMetaDataChangedEvent(event); + schemaName = DatabaseMetaDataNode.getSchemaNameByTableNode(key); + if (databaseName.isPresent() && schemaName.isPresent() && DatabaseMetaDataNode.isTableActiveVersionNode(event.getKey())) { + return createTableChangedEvent(databaseName.get(), schemaName.get(), event); } - return createRuleAndDataSourceChangedEvent(event); - } - - private boolean databaseChanged(final DataChangedEvent event) { - return DatabaseMetaDataNode.getDatabaseName(event.getKey()).isPresent(); - } - - private boolean schemaChanged(final DataChangedEvent event) { - return DatabaseMetaDataNode.getDatabaseNameByDatabasePath(event.getKey()).isPresent() && DatabaseMetaDataNode.getSchemaName(event.getKey()).isPresent(); - } - - private boolean schemaMetaDataChanged(final DataChangedEvent event) { - if (Strings.isNullOrEmpty(event.getValue())) { - return false; + if (databaseName.isPresent() && schemaName.isPresent() && DatabaseMetaDataNode.isViewActiveVersionNode(event.getKey())) { + return createViewChangedEvent(databaseName.get(), schemaName.get(), event); } - Optional databaseName = DatabaseMetaDataNode.getDatabaseNameByDatabasePath(event.getKey()); if (!databaseName.isPresent()) { - return false; + return Optional.empty(); } - Optional schemaName = DatabaseMetaDataNode.getSchemaNameBySchemaPath(event.getKey()); - if (!schemaName.isPresent()) { - return false; + if (DatabaseMetaDataNode.isDataSourcesNode(key)) { + return createDataSourceEvent(databaseName.get(), event); } - Optional tableName = DatabaseMetaDataNode.getTableName(event.getKey()); - Optional viewName = DatabaseMetaDataNode.getViewName(event.getKey()); - return tableName.isPresent() && !SystemSchemaManager.isSystemTable(databaseName.get(), tableName.get()) || viewName.isPresent(); + return ruleConfigurationEventBuilder.build(databaseName.get(), event); } - private Optional createDatabaseChangedEvent(final DataChangedEvent event) { - Optional databaseName = DatabaseMetaDataNode.getDatabaseName(event.getKey()); - Preconditions.checkState(databaseName.isPresent()); + private Optional createDatabaseChangedEvent(final String databaseName, final DataChangedEvent event) { if (Type.ADDED == event.getType() || Type.UPDATED == event.getType()) { - return Optional.of(new DatabaseAddedEvent(databaseName.get())); + return Optional.of(new DatabaseAddedEvent(databaseName)); } if (Type.DELETED == event.getType()) { - return Optional.of(new DatabaseDeletedEvent(databaseName.get())); + return Optional.of(new DatabaseDeletedEvent(databaseName)); } return Optional.empty(); } - private Optional createSchemaChangedEvent(final DataChangedEvent event) { - Optional databaseName = DatabaseMetaDataNode.getDatabaseNameByDatabasePath(event.getKey()); - Preconditions.checkState(databaseName.isPresent()); - Optional schemaName = DatabaseMetaDataNode.getSchemaName(event.getKey()); - Preconditions.checkState(schemaName.isPresent()); + private Optional createSchemaChangedEvent(final String databaseName, final String schemaName, final DataChangedEvent event) { if (Type.ADDED == event.getType() || Type.UPDATED == event.getType()) { - return Optional.of(new SchemaAddedEvent(databaseName.get(), schemaName.get())); + return Optional.of(new SchemaAddedEvent(databaseName, schemaName)); } if (Type.DELETED == event.getType()) { - return Optional.of(new SchemaDeletedEvent(databaseName.get(), schemaName.get())); + return Optional.of(new SchemaDeletedEvent(databaseName, schemaName)); } return Optional.empty(); } - private Optional createRuleAndDataSourceChangedEvent(final DataChangedEvent event) { - Optional databaseName = DatabaseMetaDataNode.getDatabaseNameByDatabasePath(event.getKey()); - if (!databaseName.isPresent() || Strings.isNullOrEmpty(event.getValue())) { - return Optional.empty(); - } - Optional databaseVersion = DatabaseMetaDataNode.getVersionByDataSourceUnitsPath(event.getKey()); - if (databaseVersion.isPresent() && event.getType() != Type.DELETED) { - return Optional.of(createDataSourceUnitsChangedEvent(databaseName.get(), databaseVersion.get(), event)); - } - databaseVersion = DatabaseMetaDataNode.getVersionByDataSourceNodesPath(event.getKey()); - if (databaseVersion.isPresent() && event.getType() != Type.DELETED) { - return Optional.of(createDataSourceNodesChangedEvent(databaseName.get(), databaseVersion.get(), event)); - } - databaseVersion = DatabaseMetaDataNode.getVersionByRulesPath(event.getKey()); - if (databaseVersion.isPresent() && event.getType() != Type.DELETED) { - return Optional.of(new RuleConfigurationsChangedEvent(databaseName.get(), databaseVersion.get(), getRuleConfigurations(event.getValue()))); + private Optional createTableChangedEvent(final String databaseName, final String schemaName, final DataChangedEvent event) { + Optional tableName = DatabaseMetaDataNode.getTableName(event.getKey()); + Preconditions.checkState(tableName.isPresent(), "Not found table name."); + if (Type.DELETED == event.getType()) { + return Optional.of(new DropTableEvent(databaseName, schemaName, tableName.get())); } - return Optional.empty(); + return Optional.of(new AlterTableEvent(databaseName, schemaName, tableName.get(), event.getKey(), event.getValue())); } - @SuppressWarnings("unchecked") - private DataSourceUnitsChangedEvent createDataSourceUnitsChangedEvent(final String databaseName, final String databaseVersion, final DataChangedEvent event) { - Map> yamlDataSources = YamlEngine.unmarshal(event.getValue(), Map.class); - Map propsMap = yamlDataSources.isEmpty() - ? new HashMap<>() - : yamlDataSources.entrySet().stream().collect(Collectors.toMap( - Entry::getKey, entry -> new YamlDataSourceConfigurationSwapper().swapToDataSourcePoolProperties(entry.getValue()), (oldValue, currentValue) -> oldValue, LinkedHashMap::new)); - return new DataSourceUnitsChangedEvent(databaseName, databaseVersion, propsMap); - } - - @SuppressWarnings("unchecked") - private DataSourceNodesChangedEvent createDataSourceNodesChangedEvent(final String databaseName, final String databaseVersion, final DataChangedEvent event) { - Map> yamlDataSources = YamlEngine.unmarshal(event.getValue(), Map.class); - Map propsMap = yamlDataSources.isEmpty() - ? new HashMap<>() - : yamlDataSources.entrySet().stream().collect(Collectors.toMap( - Entry::getKey, entry -> new YamlDataSourceConfigurationSwapper().swapToDataSourcePoolProperties(entry.getValue()), (oldValue, currentValue) -> oldValue, LinkedHashMap::new)); - return new DataSourceNodesChangedEvent(databaseName, databaseVersion, propsMap); + private Optional createViewChangedEvent(final String databaseName, final String schemaName, final DataChangedEvent event) { + Optional viewName = DatabaseMetaDataNode.getViewName(event.getKey()); + Preconditions.checkState(viewName.isPresent(), "Not found view name."); + if (Type.DELETED == event.getType()) { + return Optional.of(new DropViewEvent(databaseName, schemaName, viewName.get(), event.getKey(), event.getValue())); + } + return Optional.of(new AlterViewEvent(databaseName, schemaName, viewName.get(), event.getKey(), event.getValue())); } - @SuppressWarnings("unchecked") - private Collection getRuleConfigurations(final String yamlContent) { - Collection rules = Strings.isNullOrEmpty(yamlContent) ? new LinkedList<>() : YamlEngine.unmarshal(yamlContent, Collection.class, true); - return new YamlRuleConfigurationSwapperEngine().swapToRuleConfigurations(rules); + private Optional createDataSourceEvent(final String databaseName, final DataChangedEvent event) { + if (DatabaseMetaDataNode.isDataSourceUnitActiveVersionNode(event.getKey())) { + return createStorageUnitChangedEvent(databaseName, event); + } + if (DatabaseMetaDataNode.isDataSourceNodeActiveVersionNode(event.getKey())) { + return createStorageNodeChangedEvent(databaseName, event); + } + return Optional.empty(); } - private Optional createSchemaMetaDataChangedEvent(final DataChangedEvent event) { - Optional databaseName = DatabaseMetaDataNode.getDatabaseNameByDatabasePath(event.getKey()); - Preconditions.checkState(databaseName.isPresent()); - Optional schemaName = DatabaseMetaDataNode.getSchemaNameBySchemaPath(event.getKey()); - Preconditions.checkState(schemaName.isPresent()); - return Optional.of(createSchemaMetaDataChangedEvent(event, databaseName.get(), schemaName.get())); + private Optional createStorageUnitChangedEvent(final String databaseName, final DataChangedEvent event) { + Optional dataSourceUnitName = DatabaseMetaDataNode.getDataSourceNameByDataSourceUnitNode(event.getKey()); + if (!dataSourceUnitName.isPresent()) { + return Optional.empty(); + } + if (Type.ADDED == event.getType()) { + return Optional.of(new RegisterStorageUnitEvent(databaseName, dataSourceUnitName.get(), event.getKey(), event.getValue())); + } + if (Type.UPDATED == event.getType()) { + return Optional.of(new AlterStorageUnitEvent(databaseName, dataSourceUnitName.get(), event.getKey(), event.getValue())); + } + return Optional.of(new UnregisterStorageUnitEvent(databaseName, dataSourceUnitName.get())); } - private GovernanceEvent createSchemaMetaDataChangedEvent(final DataChangedEvent event, final String databaseName, final String schemaName) { - Optional tableName = DatabaseMetaDataNode.getTableName(event.getKey()); - Optional viewName = DatabaseMetaDataNode.getViewName(event.getKey()); - Preconditions.checkState(tableName.isPresent() || viewName.isPresent()); - if (tableName.isPresent()) { - return Type.DELETED == event.getType() - ? new TableMetaDataChangedEvent(databaseName, schemaName, null, tableName.get()) - : new TableMetaDataChangedEvent(databaseName, schemaName, new YamlTableSwapper().swapToObject(YamlEngine.unmarshal(event.getValue(), YamlShardingSphereTable.class)), null); - } - return Type.DELETED == event.getType() - ? new ViewMetaDataChangedEvent(databaseName, schemaName, null, viewName.get()) - : new ViewMetaDataChangedEvent(databaseName, schemaName, new YamlViewSwapper().swapToObject(YamlEngine.unmarshal(event.getValue(), YamlShardingSphereView.class)), null); + private Optional createStorageNodeChangedEvent(final String databaseName, final DataChangedEvent event) { + Optional dataSourceNodeName = DatabaseMetaDataNode.getDataSourceNameByDataSourceNode(event.getKey()); + if (!dataSourceNodeName.isPresent()) { + return Optional.empty(); + } + if (Type.ADDED == event.getType()) { + return Optional.of(new RegisterStorageNodeEvent(databaseName, dataSourceNodeName.get(), event.getKey(), event.getValue())); + } + if (Type.UPDATED == event.getType()) { + return Optional.of(new AlterStorageNodeEvent(databaseName, dataSourceNodeName.get(), event.getKey(), event.getValue())); + } + return Optional.of(new UnregisterStorageNodeEvent(databaseName, dataSourceNodeName.get())); } } diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/metadata/watcher/NewMetaDataChangedWatcher.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/metadata/watcher/NewMetaDataChangedWatcher.java deleted file mode 100644 index ed5c1d9e84cd2..0000000000000 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/metadata/watcher/NewMetaDataChangedWatcher.java +++ /dev/null @@ -1,170 +0,0 @@ -/* - * 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.mode.manager.cluster.coordinator.registry.metadata.watcher; - -import com.google.common.base.Preconditions; -import org.apache.shardingsphere.infra.rule.event.GovernanceEvent; -import org.apache.shardingsphere.metadata.persist.node.DatabaseMetaDataNode; -import org.apache.shardingsphere.metadata.persist.node.NewDatabaseMetaDataNode; -import org.apache.shardingsphere.mode.event.DataChangedEvent; -import org.apache.shardingsphere.mode.event.DataChangedEvent.Type; -import org.apache.shardingsphere.mode.event.datasource.nodes.AlterStorageNodeEvent; -import org.apache.shardingsphere.mode.event.datasource.nodes.RegisterStorageNodeEvent; -import org.apache.shardingsphere.mode.event.datasource.nodes.UnregisterStorageNodeEvent; -import org.apache.shardingsphere.mode.event.datasource.unit.AlterStorageUnitEvent; -import org.apache.shardingsphere.mode.event.datasource.unit.RegisterStorageUnitEvent; -import org.apache.shardingsphere.mode.event.datasource.unit.UnregisterStorageUnitEvent; -import org.apache.shardingsphere.mode.event.schema.table.AlterTableEvent; -import org.apache.shardingsphere.mode.event.schema.table.DropTableEvent; -import org.apache.shardingsphere.mode.event.schema.view.AlterViewEvent; -import org.apache.shardingsphere.mode.event.schema.view.DropViewEvent; -import org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.NewGovernanceWatcher; -import org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.metadata.event.DatabaseAddedEvent; -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.builder.RuleConfigurationEventBuilder; - -import java.util.Arrays; -import java.util.Collection; -import java.util.Collections; -import java.util.Optional; - -/** - * TODO Rename MetaDataChangedWatcher when metadata structure adjustment completed. #25485 - * Meta data changed watcher. - */ -public final class NewMetaDataChangedWatcher implements NewGovernanceWatcher { - - private final RuleConfigurationEventBuilder ruleConfigurationEventBuilder = new RuleConfigurationEventBuilder(); - - @Override - public Collection getWatchingKeys(final String databaseName) { - return null == databaseName ? Collections.singleton(DatabaseMetaDataNode.getMetaDataNodePath()) - : Collections.singleton(DatabaseMetaDataNode.getDatabaseNamePath(databaseName)); - } - - @Override - public Collection getWatchingTypes() { - return Arrays.asList(Type.ADDED, Type.UPDATED, Type.DELETED); - } - - @Override - public Optional createGovernanceEvent(final DataChangedEvent event) { - String key = event.getKey(); - Optional databaseName = NewDatabaseMetaDataNode.getDatabaseName(key); - if (databaseName.isPresent()) { - return createDatabaseChangedEvent(databaseName.get(), event); - } - databaseName = NewDatabaseMetaDataNode.getDatabaseNameBySchemaNode(key); - Optional schemaName = NewDatabaseMetaDataNode.getSchemaName(key); - if (databaseName.isPresent() && schemaName.isPresent()) { - return createSchemaChangedEvent(databaseName.get(), schemaName.get(), event); - } - schemaName = NewDatabaseMetaDataNode.getSchemaNameByTableNode(key); - if (databaseName.isPresent() && schemaName.isPresent() && NewDatabaseMetaDataNode.isTableActiveVersionNode(event.getKey())) { - return createTableChangedEvent(databaseName.get(), schemaName.get(), event); - } - if (databaseName.isPresent() && schemaName.isPresent() && NewDatabaseMetaDataNode.isViewActiveVersionNode(event.getKey())) { - return createViewChangedEvent(databaseName.get(), schemaName.get(), event); - } - if (!databaseName.isPresent()) { - return Optional.empty(); - } - if (NewDatabaseMetaDataNode.isDataSourcesNode(key)) { - return createDataSourceEvent(databaseName.get(), event); - } - return ruleConfigurationEventBuilder.build(databaseName.get(), event); - } - - private Optional createDatabaseChangedEvent(final String databaseName, final DataChangedEvent event) { - if (Type.ADDED == event.getType() || Type.UPDATED == event.getType()) { - return Optional.of(new DatabaseAddedEvent(databaseName)); - } - if (Type.DELETED == event.getType()) { - return Optional.of(new DatabaseDeletedEvent(databaseName)); - } - return Optional.empty(); - } - - private Optional createSchemaChangedEvent(final String databaseName, final String schemaName, final DataChangedEvent event) { - if (Type.ADDED == event.getType() || Type.UPDATED == event.getType()) { - return Optional.of(new SchemaAddedEvent(databaseName, schemaName)); - } - if (Type.DELETED == event.getType()) { - return Optional.of(new SchemaDeletedEvent(databaseName, schemaName)); - } - return Optional.empty(); - } - - private Optional createTableChangedEvent(final String databaseName, final String schemaName, final DataChangedEvent event) { - Optional tableName = NewDatabaseMetaDataNode.getTableName(event.getKey()); - Preconditions.checkState(tableName.isPresent(), "Not found table name."); - if (Type.DELETED == event.getType()) { - return Optional.of(new DropTableEvent(databaseName, schemaName, tableName.get())); - } - return Optional.of(new AlterTableEvent(databaseName, schemaName, tableName.get(), event.getKey(), event.getValue())); - } - - private Optional createViewChangedEvent(final String databaseName, final String schemaName, final DataChangedEvent event) { - Optional viewName = NewDatabaseMetaDataNode.getViewName(event.getKey()); - Preconditions.checkState(viewName.isPresent(), "Not found view name."); - if (Type.DELETED == event.getType()) { - return Optional.of(new DropViewEvent(databaseName, schemaName, viewName.get(), event.getKey(), event.getValue())); - } - return Optional.of(new AlterViewEvent(databaseName, schemaName, viewName.get(), event.getKey(), event.getValue())); - } - - private Optional createDataSourceEvent(final String databaseName, final DataChangedEvent event) { - if (NewDatabaseMetaDataNode.isDataSourceUnitActiveVersionNode(event.getKey())) { - return createStorageUnitChangedEvent(databaseName, event); - } - if (NewDatabaseMetaDataNode.isDataSourceNodeActiveVersionNode(event.getKey())) { - return createStorageNodeChangedEvent(databaseName, event); - } - return Optional.empty(); - } - - private Optional createStorageUnitChangedEvent(final String databaseName, final DataChangedEvent event) { - Optional dataSourceUnitName = NewDatabaseMetaDataNode.getDataSourceNameByDataSourceUnitNode(event.getKey()); - if (!dataSourceUnitName.isPresent()) { - return Optional.empty(); - } - if (Type.ADDED == event.getType()) { - return Optional.of(new RegisterStorageUnitEvent(databaseName, dataSourceUnitName.get(), event.getKey(), event.getValue())); - } - if (Type.UPDATED == event.getType()) { - return Optional.of(new AlterStorageUnitEvent(databaseName, dataSourceUnitName.get(), event.getKey(), event.getValue())); - } - return Optional.of(new UnregisterStorageUnitEvent(databaseName, dataSourceUnitName.get())); - } - - private Optional createStorageNodeChangedEvent(final String databaseName, final DataChangedEvent event) { - Optional dataSourceNodeName = NewDatabaseMetaDataNode.getDataSourceNameByDataSourceNode(event.getKey()); - if (!dataSourceNodeName.isPresent()) { - return Optional.empty(); - } - if (Type.ADDED == event.getType()) { - return Optional.of(new RegisterStorageNodeEvent(databaseName, dataSourceNodeName.get(), event.getKey(), event.getValue())); - } - if (Type.UPDATED == event.getType()) { - return Optional.of(new AlterStorageNodeEvent(databaseName, dataSourceNodeName.get(), event.getKey(), event.getValue())); - } - return Optional.of(new UnregisterStorageNodeEvent(databaseName, dataSourceNodeName.get())); - } -} diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/process/subscriber/NewProcessListChangedSubscriber.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/process/subscriber/NewProcessListChangedSubscriber.java deleted file mode 100644 index df07fd4b769d1..0000000000000 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/process/subscriber/NewProcessListChangedSubscriber.java +++ /dev/null @@ -1,115 +0,0 @@ -/* - * 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.mode.manager.cluster.coordinator.registry.process.subscriber; - -import com.google.common.eventbus.Subscribe; -import org.apache.shardingsphere.infra.executor.sql.process.Process; -import org.apache.shardingsphere.infra.executor.sql.process.ProcessRegistry; -import org.apache.shardingsphere.infra.executor.sql.process.lock.ProcessOperationLockRegistry; -import org.apache.shardingsphere.infra.executor.sql.process.yaml.swapper.YamlProcessListSwapper; -import org.apache.shardingsphere.infra.util.yaml.YamlEngine; -import org.apache.shardingsphere.metadata.persist.node.ComputeNode; -import org.apache.shardingsphere.metadata.persist.node.ProcessNode; -import org.apache.shardingsphere.mode.manager.ContextManager; -import org.apache.shardingsphere.mode.manager.cluster.coordinator.NewRegistryCenter; -import org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.status.compute.event.KillLocalProcessCompletedEvent; -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 java.sql.SQLException; -import java.sql.Statement; -import java.util.Collection; - -/** - * TODO replace the old ProcessListChangedSubscriber after meta data refactor completed - * New process list changed subscriber. - */ -public final class NewProcessListChangedSubscriber { - - private final NewRegistryCenter registryCenter; - - private final ContextManager contextManager; - - private final YamlProcessListSwapper swapper = new YamlProcessListSwapper(); - - public NewProcessListChangedSubscriber(final NewRegistryCenter registryCenter, final ContextManager contextManager) { - this.registryCenter = registryCenter; - this.contextManager = contextManager; - contextManager.getInstanceContext().getEventBusContext().register(this); - } - - /** - * Report local processes. - * - * @param event show process list trigger event - */ - @Subscribe - public void reportLocalProcesses(final ReportLocalProcessesEvent event) { - if (!event.getInstanceId().equals(contextManager.getInstanceContext().getInstance().getMetaData().getId())) { - return; - } - Collection processes = ProcessRegistry.getInstance().listAll(); - if (!processes.isEmpty()) { - registryCenter.getRepository().persist( - ProcessNode.getProcessListInstancePath(event.getTaskId(), event.getInstanceId()), YamlEngine.marshal(swapper.swapToYamlConfiguration(processes))); - } - registryCenter.getRepository().delete(ComputeNode.getProcessTriggerInstanceNodePath(event.getInstanceId(), event.getTaskId())); - } - - /** - * Complete to report local processes. - * - * @param event report local processes completed event - */ - @Subscribe - public synchronized void completeToReportLocalProcesses(final ReportLocalProcessesCompletedEvent event) { - ProcessOperationLockRegistry.getInstance().notify(event.getTaskId()); - } - - /** - * Kill local process. - * - * @param event kill local process event - * @throws SQLException SQL exception - */ - @Subscribe - public synchronized void killLocalProcess(final KillLocalProcessEvent event) throws SQLException { - if (!event.getInstanceId().equals(contextManager.getInstanceContext().getInstance().getMetaData().getId())) { - return; - } - Process process = ProcessRegistry.getInstance().get(event.getProcessId()); - if (null != process) { - process.setInterrupted(true); - for (Statement each : process.getProcessStatements().values()) { - each.cancel(); - } - } - registryCenter.getRepository().delete(ComputeNode.getProcessKillInstanceIdNodePath(event.getInstanceId(), event.getProcessId())); - } - - /** - * Complete to kill local process. - * - * @param event kill local process completed event - */ - @Subscribe - public synchronized void completeToKillLocalProcess(final KillLocalProcessCompletedEvent event) { - ProcessOperationLockRegistry.getInstance().notify(event.getProcessId()); - } -} diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/process/subscriber/ProcessListChangedSubscriber.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/process/subscriber/ProcessListChangedSubscriber.java index 4c094f18a6106..e8fc86dbc9fd9 100644 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/process/subscriber/ProcessListChangedSubscriber.java +++ b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/process/subscriber/ProcessListChangedSubscriber.java @@ -37,7 +37,8 @@ import java.util.Collection; /** - * Process list changed subscriber. + * TODO replace the old ProcessListChangedSubscriber after meta data refactor completed + * New process list changed subscriber. */ public final class ProcessListChangedSubscriber { diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/status/cluster/watcher/ClusterStateChangedWatcher.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/status/cluster/watcher/ClusterStateChangedWatcher.java index 758ea1daeb2d7..5d60abb8f4b41 100644 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/status/cluster/watcher/ClusterStateChangedWatcher.java +++ b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/status/cluster/watcher/ClusterStateChangedWatcher.java @@ -21,7 +21,6 @@ import org.apache.shardingsphere.metadata.persist.node.ComputeNode; import org.apache.shardingsphere.infra.rule.event.GovernanceEvent; import org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.GovernanceWatcher; -import org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.NewGovernanceWatcher; import org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.status.cluster.event.ClusterStateEvent; import org.apache.shardingsphere.mode.event.DataChangedEvent; import org.apache.shardingsphere.mode.event.DataChangedEvent.Type; @@ -34,7 +33,7 @@ /** * Cluster state changed watcher. */ -public final class ClusterStateChangedWatcher implements GovernanceWatcher, NewGovernanceWatcher { +public final class ClusterStateChangedWatcher implements GovernanceWatcher { @Override public Collection getWatchingKeys(final String databaseName) { diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/status/compute/subscriber/ComputeNodeStatusSubscriber.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/status/compute/subscriber/ComputeNodeStatusSubscriber.java index 37927e240660d..99c52b1e88b67 100644 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/status/compute/subscriber/ComputeNodeStatusSubscriber.java +++ b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/status/compute/subscriber/ComputeNodeStatusSubscriber.java @@ -19,8 +19,8 @@ import com.google.common.eventbus.Subscribe; import org.apache.shardingsphere.metadata.persist.node.ComputeNode; -import org.apache.shardingsphere.mode.manager.cluster.coordinator.RegistryCenter; import org.apache.shardingsphere.mode.event.compute.ComputeNodeStatusChangedEvent; +import org.apache.shardingsphere.mode.manager.cluster.coordinator.RegistryCenter; import org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.status.compute.event.LabelsChangedEvent; import org.apache.shardingsphere.mode.repository.cluster.ClusterPersistRepository; @@ -29,7 +29,6 @@ /** * Compute node status subscriber. */ -@SuppressWarnings("UnstableApiUsage") public final class ComputeNodeStatusSubscriber { private final RegistryCenter registryCenter; diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/status/compute/subscriber/NewComputeNodeStatusSubscriber.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/status/compute/subscriber/NewComputeNodeStatusSubscriber.java deleted file mode 100644 index 53dee90fc41aa..0000000000000 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/status/compute/subscriber/NewComputeNodeStatusSubscriber.java +++ /dev/null @@ -1,69 +0,0 @@ -/* - * 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.mode.manager.cluster.coordinator.registry.status.compute.subscriber; - -import com.google.common.eventbus.Subscribe; -import org.apache.shardingsphere.metadata.persist.node.ComputeNode; -import org.apache.shardingsphere.mode.event.compute.ComputeNodeStatusChangedEvent; -import org.apache.shardingsphere.mode.manager.cluster.coordinator.NewRegistryCenter; -import org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.status.compute.event.LabelsChangedEvent; -import org.apache.shardingsphere.mode.repository.cluster.ClusterPersistRepository; - -import java.util.Collections; - -/** - * TODO replace the old ComputeNodeStatusSubscriber after meta data refactor completed - * New compute node status subscriber. - */ -@SuppressWarnings("UnstableApiUsage") -public final class NewComputeNodeStatusSubscriber { - - private final NewRegistryCenter registryCenter; - - private final ClusterPersistRepository repository; - - public NewComputeNodeStatusSubscriber(final NewRegistryCenter registryCenter, final ClusterPersistRepository repository) { - this.registryCenter = registryCenter; - this.repository = repository; - registryCenter.getEventBusContext().register(this); - } - - /** - * Update compute node status. - * - * @param event compute node status changed event - */ - @Subscribe - public void update(final ComputeNodeStatusChangedEvent event) { - repository.persistEphemeral(ComputeNode.getInstanceStatusNodePath(event.getInstanceId()), event.getState().name()); - } - - /** - * Update compute node labels. - * - * @param event labels changed event - */ - @Subscribe - public void update(final LabelsChangedEvent event) { - if (event.getLabels().isEmpty()) { - registryCenter.getComputeNodeStatusService().persistInstanceLabels(event.getInstanceId(), Collections.emptyList()); - } else { - registryCenter.getComputeNodeStatusService().persistInstanceLabels(event.getInstanceId(), event.getLabels()); - } - } -} diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/status/compute/watcher/ComputeNodeStateChangedWatcher.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/status/compute/watcher/ComputeNodeStateChangedWatcher.java index 38dcb2f976b52..57b3e7c747172 100644 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/status/compute/watcher/ComputeNodeStateChangedWatcher.java +++ b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/status/compute/watcher/ComputeNodeStateChangedWatcher.java @@ -26,7 +26,6 @@ import org.apache.shardingsphere.metadata.persist.node.ComputeNode; import org.apache.shardingsphere.infra.rule.event.GovernanceEvent; import org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.GovernanceWatcher; -import org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.NewGovernanceWatcher; import org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.status.compute.event.InstanceOfflineEvent; import org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.status.compute.event.InstanceOnlineEvent; import org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.status.compute.event.KillLocalProcessEvent; @@ -50,7 +49,7 @@ /** * Compute node state changed watcher. */ -public final class ComputeNodeStateChangedWatcher implements GovernanceWatcher, NewGovernanceWatcher { +public final class ComputeNodeStateChangedWatcher implements GovernanceWatcher { @Override public Collection getWatchingKeys(final String databaseName) { diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/status/storage/watcher/StorageNodeStateChangedWatcher.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/status/storage/watcher/StorageNodeStateChangedWatcher.java index ca12b7665b9d5..7de3c72c5cfd1 100644 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/status/storage/watcher/StorageNodeStateChangedWatcher.java +++ b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/status/storage/watcher/StorageNodeStateChangedWatcher.java @@ -22,7 +22,6 @@ import org.apache.shardingsphere.infra.util.yaml.YamlEngine; import org.apache.shardingsphere.infra.rule.event.GovernanceEvent; import org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.GovernanceWatcher; -import org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.NewGovernanceWatcher; import org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.status.storage.event.StorageNodeChangedEvent; import org.apache.shardingsphere.mode.storage.node.StorageNode; import org.apache.shardingsphere.mode.storage.yaml.YamlStorageNodeDataSource; @@ -39,7 +38,7 @@ /** * Storage node state changed watcher. */ -public final class StorageNodeStateChangedWatcher implements GovernanceWatcher, NewGovernanceWatcher { +public final class StorageNodeStateChangedWatcher implements GovernanceWatcher { @Override public Collection getWatchingKeys(final String databaseName) { diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/workerid/generator/ClusterWorkerIdGenerator.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/workerid/generator/ClusterWorkerIdGenerator.java index c2ef0d46aa113..8fc242f2b9c62 100644 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/workerid/generator/ClusterWorkerIdGenerator.java +++ b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/workerid/generator/ClusterWorkerIdGenerator.java @@ -35,7 +35,7 @@ import java.util.Properties; /** - * Worker id generator for cluster mode. + * Cluster worker id generator for cluster mode. */ @RequiredArgsConstructor @Slf4j diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/workerid/generator/NewClusterWorkerIdGenerator.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/workerid/generator/NewClusterWorkerIdGenerator.java deleted file mode 100644 index ebfa331305ea1..0000000000000 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/workerid/generator/NewClusterWorkerIdGenerator.java +++ /dev/null @@ -1,95 +0,0 @@ -/* - * 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.mode.manager.cluster.coordinator.registry.workerid.generator; - -import com.google.common.base.Preconditions; -import lombok.RequiredArgsConstructor; -import lombok.extern.slf4j.Slf4j; -import org.apache.shardingsphere.infra.instance.metadata.InstanceMetaData; -import org.apache.shardingsphere.infra.instance.workerid.WorkerIdGenerator; -import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions; -import org.apache.shardingsphere.mode.manager.cluster.coordinator.NewRegistryCenter; -import org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.workerid.exception.WorkIdAssignedException; -import org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.workerid.node.WorkerIdNode; -import org.apache.shardingsphere.mode.repository.cluster.exception.ClusterPersistRepositoryException; - -import java.util.Collection; -import java.util.LinkedList; -import java.util.Optional; -import java.util.PriorityQueue; -import java.util.Properties; - -/** - * TODO replace the old implementation after meta data refactor completed - * New worker id generator for cluster mode. - */ -@RequiredArgsConstructor -@Slf4j -public final class NewClusterWorkerIdGenerator implements WorkerIdGenerator { - - private final NewRegistryCenter registryCenter; - - private final InstanceMetaData instanceMetaData; - - private volatile boolean isWarned; - - @Override - public int generate(final Properties props) { - int result = registryCenter.getComputeNodeStatusService().loadInstanceWorkerId(instanceMetaData.getId()).orElseGet(this::reGenerate); - checkIneffectiveConfiguration(result, props); - return result; - } - - private Integer reGenerate() { - Optional result; - do { - result = generateAvailableWorkerId(); - } while (!result.isPresent()); - Integer generatedWorkId = result.get(); - registryCenter.getComputeNodeStatusService().persistInstanceWorkerId(instanceMetaData.getId(), generatedWorkId); - return generatedWorkId; - } - - private Optional generateAvailableWorkerId() { - Collection assignedWorkerIds = registryCenter.getComputeNodeStatusService().getAssignedWorkerIds(); - ShardingSpherePreconditions.checkState(assignedWorkerIds.size() <= 1024, WorkIdAssignedException::new); - Collection availableWorkerIds = new LinkedList<>(); - for (int i = 0; i < 1024; i++) { - availableWorkerIds.add(i); - } - PriorityQueue priorityQueue = new PriorityQueue<>(availableWorkerIds); - for (Integer each : assignedWorkerIds) { - priorityQueue.remove(each); - } - Integer preselectedWorkerId = priorityQueue.poll(); - Preconditions.checkState(null != preselectedWorkerId, "Preselected worker-id can not be null."); - try { - registryCenter.getRepository().persistExclusiveEphemeral(WorkerIdNode.getWorkerIdGeneratorPath(preselectedWorkerId.toString()), instanceMetaData.getId()); - return Optional.of(preselectedWorkerId); - } catch (final ClusterPersistRepositoryException ignore) { - return Optional.empty(); - } - } - - private void checkIneffectiveConfiguration(final long generatedWorkerId, final Properties props) { - if (!isWarned && null != props && props.containsKey(WORKER_ID_KEY)) { - isWarned = true; - log.warn("No need to configured {} in cluster mode, system assigned {} was {}", WORKER_ID_KEY, WORKER_ID_KEY, generatedWorkerId); - } - } -} 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 18f9c03059bd0..e5eb35fe22f34 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 @@ -18,110 +18,118 @@ package org.apache.shardingsphere.mode.manager.cluster.coordinator.subscriber; import com.google.common.eventbus.Subscribe; -import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; -import org.apache.shardingsphere.infra.metadata.database.schema.QualifiedDatabase; -import org.apache.shardingsphere.infra.rule.attribute.datasource.StaticDataSourceRuleAttribute; -import org.apache.shardingsphere.infra.state.datasource.DataSourceState; -import org.apache.shardingsphere.mode.event.storage.StorageNodeDataSource; -import org.apache.shardingsphere.mode.event.storage.StorageNodeDataSourceChangedEvent; +import org.apache.shardingsphere.mode.event.config.AlterDatabaseRuleConfigurationEvent; +import org.apache.shardingsphere.mode.event.config.DropDatabaseRuleConfigurationEvent; +import org.apache.shardingsphere.mode.event.config.global.AlterGlobalRuleConfigurationEvent; +import org.apache.shardingsphere.mode.event.config.global.AlterPropertiesEvent; +import org.apache.shardingsphere.mode.event.datasource.unit.AlterStorageUnitEvent; +import org.apache.shardingsphere.mode.event.datasource.unit.RegisterStorageUnitEvent; +import org.apache.shardingsphere.mode.event.datasource.unit.UnregisterStorageUnitEvent; import org.apache.shardingsphere.mode.manager.ContextManager; -import org.apache.shardingsphere.mode.manager.cluster.coordinator.RegistryCenter; -import org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.config.event.datasource.DataSourceUnitsChangedEvent; -import org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.config.event.props.PropertiesChangedEvent; -import org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.config.event.rule.GlobalRuleConfigurationsChangedEvent; -import org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.config.event.rule.RuleConfigurationsChangedEvent; - -import java.util.Collection; -import java.util.Map; -import java.util.Map.Entry; -import java.util.stream.Collectors; /** * Configuration changed subscriber. */ +@SuppressWarnings("unused") public final class ConfigurationChangedSubscriber { - private final RegistryCenter registryCenter; - private final ContextManager contextManager; - public ConfigurationChangedSubscriber(final RegistryCenter registryCenter, final ContextManager contextManager) { - this.registryCenter = registryCenter; + public ConfigurationChangedSubscriber(final ContextManager contextManager) { this.contextManager = contextManager; contextManager.getInstanceContext().getEventBusContext().register(this); - disableDataSources(); } /** - * Renew data source units configuration. + * Renew for register storage unit. * - * @param event data source changed event. + * @param event register storage unit event */ @Subscribe - public synchronized void renew(final DataSourceUnitsChangedEvent event) { - contextManager.getConfigurationContextManager().alterDataSourceUnitsConfiguration(event.getDatabaseName(), event.getDataSourcePoolPropertiesMap()); - disableDataSources(); + public void renew(final RegisterStorageUnitEvent event) { + if (!event.getActiveVersion().equals(contextManager.getMetaDataContexts().getPersistService().getMetaDataVersionPersistService().getActiveVersionByFullPath(event.getActiveVersionKey()))) { + return; + } + contextManager.getConfigurationContextManager().registerStorageUnit(event.getDatabaseName(), + contextManager.getMetaDataContexts().getPersistService().getDataSourceUnitService().load(event.getDatabaseName(), event.getStorageUnitName())); } /** - * Renew rule configurations. + * Renew for alter storage unit. * - * @param event rule configurations changed event + * @param event register storage unit event */ @Subscribe - public synchronized void renew(final RuleConfigurationsChangedEvent event) { - contextManager.getConfigurationContextManager().alterRuleConfiguration(event.getDatabaseName(), event.getRuleConfigs()); - disableDataSources(); + public void renew(final AlterStorageUnitEvent event) { + if (!event.getActiveVersion().equals(contextManager.getMetaDataContexts().getPersistService().getMetaDataVersionPersistService().getActiveVersionByFullPath(event.getActiveVersionKey()))) { + return; + } + contextManager.getConfigurationContextManager().alterStorageUnit( + event.getDatabaseName(), contextManager.getMetaDataContexts().getPersistService().getDataSourceUnitService().load(event.getDatabaseName(), event.getStorageUnitName())); } /** - * Renew global rule configurations. + * Renew for unregister storage unit. * - * @param event global rule configurations changed event + * @param event register storage unit event */ @Subscribe - public synchronized void renew(final GlobalRuleConfigurationsChangedEvent event) { - contextManager.getConfigurationContextManager().alterGlobalRuleConfiguration(event.getRuleConfigs()); - disableDataSources(); + public void renew(final UnregisterStorageUnitEvent event) { + if (!contextManager.getMetaDataContexts().getMetaData().containsDatabase(event.getDatabaseName())) { + return; + } + contextManager.getConfigurationContextManager().unregisterStorageUnit(event.getDatabaseName(), event.getStorageUnitName()); } /** - * Renew properties. + * Renew for database rule configuration. * - * @param event properties changed event + * @param event database rule changed event */ @Subscribe - public synchronized void renew(final PropertiesChangedEvent event) { - contextManager.getConfigurationContextManager().alterProperties(event.getProps()); - } - - private void disableDataSources() { - Map storageNodes = getDisabledDataSources(); - for (Entry entry : contextManager.getMetaDataContexts().getMetaData().getDatabases().entrySet()) { - entry.getValue().getRuleMetaData().getAttributes(StaticDataSourceRuleAttribute.class).forEach(each -> disableDataSources(entry.getKey(), each, storageNodes)); + public synchronized void renew(final AlterDatabaseRuleConfigurationEvent event) { + if (!contextManager.getMetaDataContexts().getMetaData().containsDatabase(event.getDatabaseName())) { + return; } + contextManager.getConfigurationContextManager().alterRuleConfiguration(event.getDatabaseName(), event.getRuleConfig()); } - private void disableDataSources(final String databaseName, final StaticDataSourceRuleAttribute ruleAttribute, final Map storageNodes) { - for (Entry entry : storageNodes.entrySet()) { - QualifiedDatabase database = new QualifiedDatabase(entry.getKey()); - if (!database.getDatabaseName().equals(databaseName)) { - continue; - } - disableDataSources(entry.getValue(), ruleAttribute, database); + /** + * Renew for database rule configuration. + * + * @param event database rule changed event + */ + @Subscribe + public synchronized void renew(final DropDatabaseRuleConfigurationEvent event) { + if (!contextManager.getMetaDataContexts().getMetaData().containsDatabase(event.getDatabaseName())) { + return; } + contextManager.getConfigurationContextManager().dropRuleConfiguration(event.getDatabaseName(), event.getRuleConfig()); } - private void disableDataSources(final StorageNodeDataSource storageNodeDataSource, final StaticDataSourceRuleAttribute ruleAttribute, final QualifiedDatabase database) { - for (Entry> entry : ruleAttribute.getDataSourceMapper().entrySet()) { - if (database.getGroupName().equals(entry.getKey())) { - entry.getValue().forEach(each -> ruleAttribute.updateStatus(new StorageNodeDataSourceChangedEvent(database, storageNodeDataSource))); - } + /** + * Renew for global rule configuration. + * + * @param event global rule alter event + */ + @Subscribe + public synchronized void renew(final AlterGlobalRuleConfigurationEvent event) { + if (!event.getActiveVersion().equals(contextManager.getMetaDataContexts().getPersistService().getMetaDataVersionPersistService().getActiveVersionByFullPath(event.getActiveVersionKey()))) { + return; } + contextManager.getConfigurationContextManager().alterGlobalRuleConfiguration(contextManager.getMetaDataContexts().getPersistService().getGlobalRuleService().load(event.getRuleSimpleName())); } - private Map getDisabledDataSources() { - return registryCenter.getStorageNodeStatusService().loadStorageNodes().entrySet() - .stream().filter(entry -> DataSourceState.DISABLED == entry.getValue().getStatus()).collect(Collectors.toMap(Entry::getKey, Entry::getValue)); + /** + * Renew for global properties. + * + * @param event global properties alter event + */ + @Subscribe + public synchronized void renew(final AlterPropertiesEvent event) { + if (!event.getActiveVersion().equals(contextManager.getMetaDataContexts().getPersistService().getMetaDataVersionPersistService().getActiveVersionByFullPath(event.getActiveVersionKey()))) { + return; + } + contextManager.getConfigurationContextManager().alterProperties(contextManager.getMetaDataContexts().getPersistService().getPropsService().load()); } } diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/subscriber/ContextManagerSubscriberFacade.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/subscriber/ContextManagerSubscriberFacade.java index 8b061ae691c80..53b06867127c2 100644 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/subscriber/ContextManagerSubscriberFacade.java +++ b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/subscriber/ContextManagerSubscriberFacade.java @@ -27,7 +27,7 @@ public final class ContextManagerSubscriberFacade { public ContextManagerSubscriberFacade(final RegistryCenter registryCenter, final ContextManager contextManager) { - new ConfigurationChangedSubscriber(registryCenter, contextManager); + new ConfigurationChangedSubscriber(contextManager); new ResourceMetaDataChangedSubscriber(contextManager); new DatabaseChangedSubscriber(contextManager); new StateChangedSubscriber(registryCenter, contextManager); diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/subscriber/NewConfigurationChangedSubscriber.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/subscriber/NewConfigurationChangedSubscriber.java deleted file mode 100644 index 0d08e888894a1..0000000000000 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/subscriber/NewConfigurationChangedSubscriber.java +++ /dev/null @@ -1,136 +0,0 @@ -/* - * 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.mode.manager.cluster.coordinator.subscriber; - -import com.google.common.eventbus.Subscribe; -import org.apache.shardingsphere.mode.event.config.AlterDatabaseRuleConfigurationEvent; -import org.apache.shardingsphere.mode.event.config.DropDatabaseRuleConfigurationEvent; -import org.apache.shardingsphere.mode.event.config.global.AlterGlobalRuleConfigurationEvent; -import org.apache.shardingsphere.mode.event.config.global.AlterPropertiesEvent; -import org.apache.shardingsphere.mode.event.datasource.unit.AlterStorageUnitEvent; -import org.apache.shardingsphere.mode.event.datasource.unit.RegisterStorageUnitEvent; -import org.apache.shardingsphere.mode.event.datasource.unit.UnregisterStorageUnitEvent; -import org.apache.shardingsphere.mode.manager.ContextManager; - -/** - * TODO Rename ConfigurationChangedSubscriber when metadata structure adjustment completed. #25485 - * New configuration changed subscriber. - */ -@SuppressWarnings("unused") -public final class NewConfigurationChangedSubscriber { - - private final ContextManager contextManager; - - public NewConfigurationChangedSubscriber(final ContextManager contextManager) { - this.contextManager = contextManager; - contextManager.getInstanceContext().getEventBusContext().register(this); - } - - /** - * Renew for register storage unit. - * - * @param event register storage unit event - */ - @Subscribe - public void renew(final RegisterStorageUnitEvent event) { - if (!event.getActiveVersion().equals(contextManager.getMetaDataContexts().getPersistService().getMetaDataVersionPersistService().getActiveVersionByFullPath(event.getActiveVersionKey()))) { - return; - } - contextManager.getConfigurationContextManager().registerStorageUnit(event.getDatabaseName(), - contextManager.getMetaDataContexts().getPersistService().getDataSourceUnitService().load(event.getDatabaseName(), event.getStorageUnitName())); - } - - /** - * Renew for alter storage unit. - * - * @param event register storage unit event - */ - @Subscribe - public void renew(final AlterStorageUnitEvent event) { - if (!event.getActiveVersion().equals(contextManager.getMetaDataContexts().getPersistService().getMetaDataVersionPersistService().getActiveVersionByFullPath(event.getActiveVersionKey()))) { - return; - } - contextManager.getConfigurationContextManager().alterStorageUnit( - event.getDatabaseName(), contextManager.getMetaDataContexts().getPersistService().getDataSourceUnitService().load(event.getDatabaseName(), event.getStorageUnitName())); - } - - /** - * Renew for unregister storage unit. - * - * @param event register storage unit event - */ - @Subscribe - public void renew(final UnregisterStorageUnitEvent event) { - if (!contextManager.getMetaDataContexts().getMetaData().containsDatabase(event.getDatabaseName())) { - return; - } - contextManager.getConfigurationContextManager().unregisterStorageUnit(event.getDatabaseName(), event.getStorageUnitName()); - } - - /** - * Renew for database rule configuration. - * - * @param event database rule changed event - */ - @Subscribe - public synchronized void renew(final AlterDatabaseRuleConfigurationEvent event) { - if (!contextManager.getMetaDataContexts().getMetaData().containsDatabase(event.getDatabaseName())) { - return; - } - contextManager.getConfigurationContextManager().alterRuleConfiguration(event.getDatabaseName(), event.getRuleConfig()); - } - - /** - * Renew for database rule configuration. - * - * @param event database rule changed event - */ - @Subscribe - public synchronized void renew(final DropDatabaseRuleConfigurationEvent event) { - if (!contextManager.getMetaDataContexts().getMetaData().containsDatabase(event.getDatabaseName())) { - return; - } - contextManager.getConfigurationContextManager().dropRuleConfiguration(event.getDatabaseName(), event.getRuleConfig()); - } - - /** - * Renew for global rule configuration. - * - * @param event global rule alter event - */ - @Subscribe - public synchronized void renew(final AlterGlobalRuleConfigurationEvent event) { - if (!event.getActiveVersion().equals(contextManager.getMetaDataContexts().getPersistService().getMetaDataVersionPersistService().getActiveVersionByFullPath(event.getActiveVersionKey()))) { - return; - } - contextManager.getConfigurationContextManager().alterGlobalRuleConfiguration(contextManager.getMetaDataContexts().getPersistService().getGlobalRuleService().load(event.getRuleSimpleName())); - } - - /** - * Renew for global properties. - * - * @param event global properties alter event - */ - @Subscribe - public synchronized void renew(final AlterPropertiesEvent event) { - if (!event.getActiveVersion().equals(contextManager.getMetaDataContexts().getPersistService().getMetaDataVersionPersistService().getActiveVersionByFullPath(event.getActiveVersionKey()))) { - return; - } - contextManager.getConfigurationContextManager().alterProperties(contextManager.getMetaDataContexts().getPersistService().getPropsService().load()); - } -} diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/subscriber/NewContextManagerSubscriberFacade.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/subscriber/NewContextManagerSubscriberFacade.java deleted file mode 100644 index 169b7221e1580..0000000000000 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/subscriber/NewContextManagerSubscriberFacade.java +++ /dev/null @@ -1,38 +0,0 @@ -/* - * 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.mode.manager.cluster.coordinator.subscriber; - -import org.apache.shardingsphere.mode.manager.ContextManager; -import org.apache.shardingsphere.mode.manager.cluster.coordinator.NewRegistryCenter; -import org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.process.subscriber.NewProcessListChangedSubscriber; - -/** - * TODO replace the old implementation after meta data refactor completed - * New context manager subscriber facade. - */ -public final class NewContextManagerSubscriberFacade { - - public NewContextManagerSubscriberFacade(final NewRegistryCenter registryCenter, final ContextManager contextManager) { - new NewConfigurationChangedSubscriber(contextManager); - new NewResourceMetaDataChangedSubscriber(contextManager); - new DatabaseChangedSubscriber(contextManager); - new NewStateChangedSubscriber(registryCenter, contextManager); - new NewProcessListChangedSubscriber(registryCenter, contextManager); - new CacheEvictedSubscriber(contextManager.getInstanceContext().getEventBusContext()); - } -} diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/subscriber/NewResourceMetaDataChangedSubscriber.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/subscriber/NewResourceMetaDataChangedSubscriber.java deleted file mode 100644 index 21e7ebe643794..0000000000000 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/subscriber/NewResourceMetaDataChangedSubscriber.java +++ /dev/null @@ -1,138 +0,0 @@ -/* - * 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.mode.manager.cluster.coordinator.subscriber; - -import com.google.common.eventbus.Subscribe; -import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereTable; -import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereView; -import org.apache.shardingsphere.mode.event.schema.table.AlterTableEvent; -import org.apache.shardingsphere.mode.event.schema.table.DropTableEvent; -import org.apache.shardingsphere.mode.event.schema.view.AlterViewEvent; -import org.apache.shardingsphere.mode.event.schema.view.DropViewEvent; -import org.apache.shardingsphere.mode.manager.ContextManager; -import org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.metadata.event.DatabaseAddedEvent; -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 java.util.Map; - -/** - * TODO Rename ResourceMetaDataChangedSubscriber when metadata structure adjustment completed. #25485 - * Resource meta data changed subscriber. - */ -@SuppressWarnings("unused") -public final class NewResourceMetaDataChangedSubscriber { - - private final ContextManager contextManager; - - public NewResourceMetaDataChangedSubscriber(final ContextManager contextManager) { - this.contextManager = contextManager; - contextManager.getInstanceContext().getEventBusContext().register(this); - } - - /** - * Renew to persist meta data. - * - * @param event database added event - */ - @Subscribe - public synchronized void renew(final DatabaseAddedEvent event) { - contextManager.getResourceMetaDataContextManager().addDatabase(event.getDatabaseName()); - } - - /** - * Renew to delete database. - * - * @param event database delete event - */ - @Subscribe - public synchronized void renew(final DatabaseDeletedEvent event) { - contextManager.getResourceMetaDataContextManager().dropDatabase(event.getDatabaseName()); - } - - /** - * Renew to added schema. - * - * @param event schema added event - */ - @Subscribe - public synchronized void renew(final SchemaAddedEvent event) { - contextManager.getResourceMetaDataContextManager().addSchema(event.getDatabaseName(), event.getSchemaName()); - } - - /** - * Renew to delete schema. - * - * @param event schema delete event - */ - @Subscribe - public synchronized void renew(final SchemaDeletedEvent event) { - contextManager.getResourceMetaDataContextManager().dropSchema(event.getDatabaseName(), event.getSchemaName()); - } - - /** - * Renew table. - * - * @param event alter table event - */ - @Subscribe - public synchronized void renew(final AlterTableEvent event) { - if (!event.getActiveVersion().equals(contextManager.getMetaDataContexts().getPersistService().getMetaDataVersionPersistService().getActiveVersionByFullPath(event.getActiveVersionKey()))) { - return; - } - Map tables = contextManager.getMetaDataContexts().getPersistService().getDatabaseMetaDataService() - .getTableMetaDataPersistService().load(event.getDatabaseName(), event.getSchemaName(), event.getTableName()); - contextManager.getResourceMetaDataContextManager().alterSchema(event.getDatabaseName(), event.getSchemaName(), tables.values().iterator().next(), null); - } - - /** - * Renew table. - * - * @param event drop table event - */ - @Subscribe - public synchronized void renew(final DropTableEvent event) { - contextManager.getResourceMetaDataContextManager().alterSchema(event.getDatabaseName(), event.getSchemaName(), event.getTableName(), null); - } - - /** - * Renew view. - * - * @param event alter view event - */ - @Subscribe - public synchronized void renew(final AlterViewEvent event) { - if (!event.getActiveVersion().equals(contextManager.getMetaDataContexts().getPersistService().getMetaDataVersionPersistService().getActiveVersionByFullPath(event.getActiveVersionKey()))) { - return; - } - Map views = contextManager.getMetaDataContexts().getPersistService().getDatabaseMetaDataService() - .getViewMetaDataPersistService().load(event.getDatabaseName(), event.getSchemaName(), event.getViewName()); - contextManager.getResourceMetaDataContextManager().alterSchema(event.getDatabaseName(), event.getSchemaName(), null, views.values().iterator().next()); - } - - /** - * Renew view. - * - * @param event drop view event - */ - @Subscribe - public synchronized void renew(final DropViewEvent event) { - contextManager.getResourceMetaDataContextManager().alterSchema(event.getDatabaseName(), event.getSchemaName(), null, event.getViewName()); - } -} diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/subscriber/NewStateChangedSubscriber.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/subscriber/NewStateChangedSubscriber.java deleted file mode 100644 index 899e628df75ab..0000000000000 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/subscriber/NewStateChangedSubscriber.java +++ /dev/null @@ -1,144 +0,0 @@ -/* - * 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.mode.manager.cluster.coordinator.subscriber; - -import com.google.common.eventbus.Subscribe; -import org.apache.shardingsphere.infra.instance.ComputeNodeInstance; -import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData; -import org.apache.shardingsphere.infra.rule.attribute.datasource.StaticDataSourceRuleAttribute; -import org.apache.shardingsphere.infra.state.datasource.DataSourceState; -import org.apache.shardingsphere.infra.state.datasource.DataSourceStateManager; -import org.apache.shardingsphere.mode.event.storage.StorageNodeDataSourceChangedEvent; -import org.apache.shardingsphere.mode.manager.ContextManager; -import org.apache.shardingsphere.mode.manager.cluster.coordinator.NewRegistryCenter; -import org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.status.cluster.event.ClusterLockDeletedEvent; -import org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.status.cluster.event.ClusterStateEvent; -import org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.status.cluster.event.ClusterStatusChangedEvent; -import org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.status.compute.event.InstanceOfflineEvent; -import org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.status.compute.event.InstanceOnlineEvent; -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.StateEvent; -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; - -/** - * TODO replace the old StateChangedSubscriber after meta data refactor completed - * New state changed subscriber. - */ -@SuppressWarnings("unused") -public final class NewStateChangedSubscriber { - - private final NewRegistryCenter registryCenter; - - private final ContextManager contextManager; - - public NewStateChangedSubscriber(final NewRegistryCenter registryCenter, final ContextManager contextManager) { - this.registryCenter = registryCenter; - this.contextManager = contextManager; - contextManager.getInstanceContext().getEventBusContext().register(this); - } - - /** - * Renew disabled data source names. - * - * @param event Storage node changed event - */ - @Subscribe - public synchronized void renew(final StorageNodeChangedEvent event) { - ShardingSphereMetaData metaData = contextManager.getMetaDataContexts().getMetaData(); - if (!metaData.containsDatabase(event.getQualifiedDatabase().getDatabaseName())) { - return; - } - for (StaticDataSourceRuleAttribute each : metaData.getDatabase(event.getQualifiedDatabase().getDatabaseName()).getRuleMetaData().getAttributes(StaticDataSourceRuleAttribute.class)) { - each.updateStatus(new StorageNodeDataSourceChangedEvent(event.getQualifiedDatabase(), event.getDataSource())); - } - DataSourceStateManager.getInstance().updateState( - event.getQualifiedDatabase().getDatabaseName(), event.getQualifiedDatabase().getDataSourceName(), DataSourceState.valueOf(event.getDataSource().getStatus().name())); - } - - /** - * Reset cluster state. - * - * @param event cluster lock deleted event - */ - @Subscribe - public synchronized void renew(final ClusterLockDeletedEvent event) { - contextManager.getInstanceContext().getEventBusContext().post(new ClusterStatusChangedEvent(event.getState())); - } - - /** - * Renew cluster state. - * - * @param event cluster state event - */ - @Subscribe - public synchronized void renew(final ClusterStateEvent event) { - contextManager.updateClusterState(event.getStatus()); - } - - /** - * Renew instance status. - * - * @param event state event - */ - @Subscribe - public synchronized void renew(final StateEvent event) { - contextManager.getInstanceContext().updateInstanceStatus(event.getInstanceId(), event.getStatus()); - } - - /** - * Renew instance worker id. - * - * @param event worker id event - */ - @Subscribe - public synchronized void renew(final WorkerIdEvent event) { - contextManager.getInstanceContext().updateWorkerId(event.getInstanceId(), event.getWorkerId()); - } - - /** - * Renew instance labels. - * - * @param event label event - */ - @Subscribe - public synchronized void renew(final LabelsEvent event) { - // TODO labels may be empty - contextManager.getInstanceContext().updateLabel(event.getInstanceId(), event.getLabels()); - } - - /** - * Renew instance list. - * - * @param event compute node online event - */ - @Subscribe - public synchronized void renew(final InstanceOnlineEvent event) { - contextManager.getInstanceContext().addComputeNodeInstance(registryCenter.getComputeNodeStatusService().loadComputeNodeInstance(event.getInstanceMetaData())); - } - - /** - * Renew instance list. - * - * @param event compute node offline event - */ - @Subscribe - public synchronized void renew(final InstanceOfflineEvent event) { - contextManager.getInstanceContext().deleteComputeNodeInstance(new ComputeNodeInstance(event.getInstanceMetaData())); - } -} 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 7aa655b1f0a20..437dee217251e 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 @@ -18,17 +18,24 @@ package org.apache.shardingsphere.mode.manager.cluster.coordinator.subscriber; import com.google.common.eventbus.Subscribe; +import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereTable; +import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereView; +import org.apache.shardingsphere.mode.event.schema.table.AlterTableEvent; +import org.apache.shardingsphere.mode.event.schema.table.DropTableEvent; +import org.apache.shardingsphere.mode.event.schema.view.AlterViewEvent; +import org.apache.shardingsphere.mode.event.schema.view.DropViewEvent; import org.apache.shardingsphere.mode.manager.ContextManager; -import org.apache.shardingsphere.mode.event.schema.TableMetaDataChangedEvent; -import org.apache.shardingsphere.mode.event.schema.ViewMetaDataChangedEvent; import org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.metadata.event.DatabaseAddedEvent; 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 java.util.Map; + /** * Resource meta data changed subscriber. */ +@SuppressWarnings("unused") public final class ResourceMetaDataChangedSubscriber { private final ContextManager contextManager; @@ -79,24 +86,52 @@ public synchronized void renew(final SchemaDeletedEvent event) { } /** - * Renew meta data of the table. + * Renew table. + * + * @param event alter table event + */ + @Subscribe + public synchronized void renew(final AlterTableEvent event) { + if (!event.getActiveVersion().equals(contextManager.getMetaDataContexts().getPersistService().getMetaDataVersionPersistService().getActiveVersionByFullPath(event.getActiveVersionKey()))) { + return; + } + Map tables = contextManager.getMetaDataContexts().getPersistService().getDatabaseMetaDataService() + .getTableMetaDataPersistService().load(event.getDatabaseName(), event.getSchemaName(), event.getTableName()); + contextManager.getResourceMetaDataContextManager().alterSchema(event.getDatabaseName(), event.getSchemaName(), tables.values().iterator().next(), null); + } + + /** + * Renew table. + * + * @param event drop table event + */ + @Subscribe + public synchronized void renew(final DropTableEvent event) { + contextManager.getResourceMetaDataContextManager().alterSchema(event.getDatabaseName(), event.getSchemaName(), event.getTableName(), null); + } + + /** + * Renew view. * - * @param event table meta data changed event + * @param event alter view event */ @Subscribe - public synchronized void renew(final TableMetaDataChangedEvent event) { - contextManager.getResourceMetaDataContextManager().alterSchema(event.getDatabaseName(), event.getSchemaName(), event.getChangedTableMetaData(), null); - contextManager.getResourceMetaDataContextManager().alterSchema(event.getDatabaseName(), event.getSchemaName(), event.getDeletedTable(), null); + public synchronized void renew(final AlterViewEvent event) { + if (!event.getActiveVersion().equals(contextManager.getMetaDataContexts().getPersistService().getMetaDataVersionPersistService().getActiveVersionByFullPath(event.getActiveVersionKey()))) { + return; + } + Map views = contextManager.getMetaDataContexts().getPersistService().getDatabaseMetaDataService() + .getViewMetaDataPersistService().load(event.getDatabaseName(), event.getSchemaName(), event.getViewName()); + contextManager.getResourceMetaDataContextManager().alterSchema(event.getDatabaseName(), event.getSchemaName(), null, views.values().iterator().next()); } /** - * Renew meta data of the view. + * Renew view. * - * @param event view meta data changed event + * @param event drop view event */ @Subscribe - public synchronized void renew(final ViewMetaDataChangedEvent event) { - contextManager.getResourceMetaDataContextManager().alterSchema(event.getDatabaseName(), event.getSchemaName(), null, event.getChangedViewMetaData()); - contextManager.getResourceMetaDataContextManager().alterSchema(event.getDatabaseName(), event.getSchemaName(), null, event.getDeletedView()); + public synchronized void renew(final DropViewEvent event) { + contextManager.getResourceMetaDataContextManager().alterSchema(event.getDatabaseName(), event.getSchemaName(), null, event.getViewName()); } } diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/subscriber/StateChangedSubscriber.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/subscriber/StateChangedSubscriber.java index 4f1a2b9de8c19..7b824faa4a282 100644 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/subscriber/StateChangedSubscriber.java +++ b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/subscriber/StateChangedSubscriber.java @@ -39,6 +39,7 @@ /** * State changed subscriber. */ +@SuppressWarnings("unused") public final class StateChangedSubscriber { private final RegistryCenter registryCenter; diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/yaml/ClusterYamlPersistRepositoryConfigurationSwapper.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/yaml/ClusterYamlPersistRepositoryConfigurationSwapper.java index b9f53ce60e1eb..2bf6e24936a68 100644 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/yaml/ClusterYamlPersistRepositoryConfigurationSwapper.java +++ b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/yaml/ClusterYamlPersistRepositoryConfigurationSwapper.java @@ -17,9 +17,9 @@ package org.apache.shardingsphere.mode.manager.cluster.yaml; -import org.apache.shardingsphere.mode.repository.cluster.ClusterPersistRepositoryConfiguration; import org.apache.shardingsphere.infra.yaml.config.pojo.mode.YamlPersistRepositoryConfiguration; import org.apache.shardingsphere.infra.yaml.config.swapper.mode.YamlPersistRepositoryConfigurationSwapper; +import org.apache.shardingsphere.mode.repository.cluster.ClusterPersistRepositoryConfiguration; /** * Cluster YAML persist repository configuration swapper. @@ -44,6 +44,6 @@ public ClusterPersistRepositoryConfiguration swapToObject(final YamlPersistRepos @Override public String getType() { - return "Compatible_Cluster"; + return "Cluster"; } } diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/yaml/NewClusterYamlPersistRepositoryConfigurationSwapper.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/yaml/NewClusterYamlPersistRepositoryConfigurationSwapper.java deleted file mode 100644 index c0203fc79c21d..0000000000000 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/yaml/NewClusterYamlPersistRepositoryConfigurationSwapper.java +++ /dev/null @@ -1,50 +0,0 @@ -/* - * 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.mode.manager.cluster.yaml; - -import org.apache.shardingsphere.infra.yaml.config.pojo.mode.YamlPersistRepositoryConfiguration; -import org.apache.shardingsphere.infra.yaml.config.swapper.mode.YamlPersistRepositoryConfigurationSwapper; -import org.apache.shardingsphere.mode.repository.cluster.ClusterPersistRepositoryConfiguration; - -/** - * TODO replace the old implementation after meta data refactor completed - * New cluster YAML persist repository configuration swapper. - */ -public final class NewClusterYamlPersistRepositoryConfigurationSwapper implements YamlPersistRepositoryConfigurationSwapper { - - @Override - public YamlPersistRepositoryConfiguration swapToYamlConfiguration(final ClusterPersistRepositoryConfiguration data) { - YamlPersistRepositoryConfiguration result = new YamlPersistRepositoryConfiguration(); - result.setType(data.getType()); - result.setProps(data.getProps()); - result.getProps().setProperty("namespace", data.getNamespace()); - result.getProps().setProperty("server-lists", data.getServerLists()); - return result; - } - - @Override - public ClusterPersistRepositoryConfiguration swapToObject(final YamlPersistRepositoryConfiguration yamlConfig) { - return new ClusterPersistRepositoryConfiguration( - yamlConfig.getType(), yamlConfig.getProps().getProperty("namespace"), yamlConfig.getProps().getProperty("server-lists"), yamlConfig.getProps()); - } - - @Override - public String getType() { - return "Cluster"; - } -} diff --git a/mode/type/cluster/core/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.yaml.config.swapper.mode.YamlPersistRepositoryConfigurationSwapper b/mode/type/cluster/core/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.yaml.config.swapper.mode.YamlPersistRepositoryConfigurationSwapper index 32547afa5a670..c99b74c9e2822 100644 --- a/mode/type/cluster/core/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.yaml.config.swapper.mode.YamlPersistRepositoryConfigurationSwapper +++ b/mode/type/cluster/core/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.yaml.config.swapper.mode.YamlPersistRepositoryConfigurationSwapper @@ -16,4 +16,3 @@ # org.apache.shardingsphere.mode.manager.cluster.yaml.ClusterYamlPersistRepositoryConfigurationSwapper -org.apache.shardingsphere.mode.manager.cluster.yaml.NewClusterYamlPersistRepositoryConfigurationSwapper diff --git a/mode/type/cluster/core/src/main/resources/META-INF/services/org.apache.shardingsphere.mode.manager.ContextManagerBuilder b/mode/type/cluster/core/src/main/resources/META-INF/services/org.apache.shardingsphere.mode.manager.ContextManagerBuilder index ba7c524d7eefb..953967ea3f16d 100644 --- a/mode/type/cluster/core/src/main/resources/META-INF/services/org.apache.shardingsphere.mode.manager.ContextManagerBuilder +++ b/mode/type/cluster/core/src/main/resources/META-INF/services/org.apache.shardingsphere.mode.manager.ContextManagerBuilder @@ -16,4 +16,3 @@ # org.apache.shardingsphere.mode.manager.cluster.ClusterContextManagerBuilder -org.apache.shardingsphere.mode.manager.cluster.NewClusterContextManagerBuilder diff --git a/mode/type/cluster/core/src/main/resources/META-INF/services/org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.GovernanceWatcher b/mode/type/cluster/core/src/main/resources/META-INF/services/org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.GovernanceWatcher index 2fa942d0c4814..9c73806ae28b4 100644 --- a/mode/type/cluster/core/src/main/resources/META-INF/services/org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.GovernanceWatcher +++ b/mode/type/cluster/core/src/main/resources/META-INF/services/org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.GovernanceWatcher @@ -16,9 +16,9 @@ # org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.status.storage.watcher.StorageNodeStateChangedWatcher -org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.metadata.watcher.MetaDataChangedWatcher -org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.config.watcher.GlobalRuleChangedWatcher -org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.config.watcher.PropertiesChangedWatcher org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.status.compute.watcher.ComputeNodeStateChangedWatcher org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.status.cluster.watcher.ClusterStateChangedWatcher org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.data.ShardingSphereDataChangedWatcher +org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.metadata.watcher.MetaDataChangedWatcher +org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.config.watcher.GlobalRuleChangedWatcher +org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.config.watcher.PropertiesChangedWatcher diff --git a/mode/type/cluster/core/src/main/resources/META-INF/services/org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.NewGovernanceWatcher b/mode/type/cluster/core/src/main/resources/META-INF/services/org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.NewGovernanceWatcher deleted file mode 100644 index 474d36e9d3553..0000000000000 --- a/mode/type/cluster/core/src/main/resources/META-INF/services/org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.NewGovernanceWatcher +++ /dev/null @@ -1,24 +0,0 @@ -# -# 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. -# - -org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.status.storage.watcher.StorageNodeStateChangedWatcher -org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.status.compute.watcher.ComputeNodeStateChangedWatcher -org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.status.cluster.watcher.ClusterStateChangedWatcher -org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.data.ShardingSphereDataChangedWatcher -org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.metadata.watcher.NewMetaDataChangedWatcher -org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.config.watcher.NewGlobalRuleChangedWatcher -org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.config.watcher.NewPropertiesChangedWatcher diff --git a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/ClusterModeContextManagerTest.java b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/ClusterModeContextManagerTest.java deleted file mode 100644 index ff36d8962f2d7..0000000000000 --- a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/ClusterModeContextManagerTest.java +++ /dev/null @@ -1,479 +0,0 @@ -/* - * 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.mode.manager.cluster; - -import org.apache.shardingsphere.infra.config.mode.ModeConfiguration; -import org.apache.shardingsphere.infra.config.props.ConfigurationProperties; -import org.apache.shardingsphere.infra.config.rule.RuleConfiguration; -import org.apache.shardingsphere.infra.database.core.type.DatabaseType; -import org.apache.shardingsphere.infra.datasource.pool.props.domain.DataSourcePoolProperties; -import org.apache.shardingsphere.infra.instance.ComputeNodeInstance; -import org.apache.shardingsphere.infra.instance.InstanceContext; -import org.apache.shardingsphere.infra.instance.metadata.jdbc.JDBCInstanceMetaData; -import org.apache.shardingsphere.infra.instance.workerid.WorkerIdGenerator; -import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData; -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.database.schema.pojo.AlterSchemaMetaDataPOJO; -import org.apache.shardingsphere.infra.metadata.database.schema.pojo.AlterSchemaPOJO; -import org.apache.shardingsphere.infra.util.eventbus.EventBusContext; -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.manager.cluster.coordinator.fixture.ClusterPersistRepositoryFixture; -import org.apache.shardingsphere.mode.manager.cluster.coordinator.fixture.RuleConfigurationFixture; -import org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.process.ProcessListClusterPersistRepositoryFixture; -import org.apache.shardingsphere.mode.metadata.MetaDataContexts; -import org.apache.shardingsphere.mode.repository.cluster.ClusterPersistRepositoryConfiguration; -import org.junit.jupiter.api.Test; - -import java.util.Collection; -import java.util.Collections; -import java.util.HashMap; -import java.util.LinkedList; -import java.util.Map; -import java.util.Properties; -import java.util.TreeMap; - -import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; -import static org.junit.jupiter.api.Assertions.assertNull; -import static org.mockito.Mockito.mock; - -class ClusterModeContextManagerTest { - - @Test - void assertCreateDatabase() { - ClusterModeContextManager clusterModeContextManager = new ClusterModeContextManager(); - MetaDataPersistService persistService = new MetaDataPersistService(new ClusterPersistRepositoryFixture()); - MetaDataContexts metaDataContexts = new MetaDataContexts(persistService, new ShardingSphereMetaData()); - ComputeNodeInstance instance = new ComputeNodeInstance(new JDBCInstanceMetaData("42")); - WorkerIdGenerator workerIdGenerator = mock(WorkerIdGenerator.class); - ModeConfiguration modeConfig = new ModeConfiguration("Type", new ClusterPersistRepositoryConfiguration("Type", "Namespace", "Server Lists", new Properties())); - ClusterModeContextManager modeContextManager = new ClusterModeContextManager(); - clusterModeContextManager.setContextManagerAware( - new ContextManager(metaDataContexts, new InstanceContext(instance, workerIdGenerator, modeConfig, modeContextManager, null, new EventBusContext()))); - assertDoesNotThrow(() -> clusterModeContextManager.createDatabase("db")); - } - - @Test - void assertDropDatabase() { - ClusterModeContextManager clusterModeContextManager = new ClusterModeContextManager(); - MetaDataPersistService persistService = new MetaDataPersistService(new ClusterPersistRepositoryFixture()); - MetaDataContexts metaDataContexts = new MetaDataContexts(persistService, new ShardingSphereMetaData()); - ComputeNodeInstance instance = new ComputeNodeInstance(new JDBCInstanceMetaData("42")); - WorkerIdGenerator workerIdGenerator = mock(WorkerIdGenerator.class); - ModeConfiguration modeConfig = new ModeConfiguration("Type", new ClusterPersistRepositoryConfiguration("Type", "Namespace", "Server Lists", new Properties())); - ClusterModeContextManager modeContextManager = new ClusterModeContextManager(); - clusterModeContextManager.setContextManagerAware( - new ContextManager(metaDataContexts, new InstanceContext(instance, workerIdGenerator, modeConfig, modeContextManager, null, new EventBusContext()))); - assertDoesNotThrow(() -> clusterModeContextManager.dropDatabase("db")); - } - - @Test - void assertCreateSchema() { - ClusterModeContextManager clusterModeContextManager = new ClusterModeContextManager(); - MetaDataPersistService persistService = new MetaDataPersistService(new ClusterPersistRepositoryFixture()); - MetaDataContexts metaDataContexts = new MetaDataContexts(persistService, new ShardingSphereMetaData()); - ComputeNodeInstance instance = new ComputeNodeInstance(new JDBCInstanceMetaData("42")); - WorkerIdGenerator workerIdGenerator = mock(WorkerIdGenerator.class); - ModeConfiguration modeConfig = new ModeConfiguration("Type", new ClusterPersistRepositoryConfiguration("Type", "Namespace", "Server Lists", new Properties())); - ClusterModeContextManager modeContextManager = new ClusterModeContextManager(); - clusterModeContextManager.setContextManagerAware( - new ContextManager(metaDataContexts, new InstanceContext(instance, workerIdGenerator, modeConfig, modeContextManager, null, new EventBusContext()))); - assertDoesNotThrow(() -> clusterModeContextManager.createSchema("db", "Schema Name")); - } - - @Test - void assertAlterSchema() { - ShardingSphereMetaData shardingSphereMetaData = new ShardingSphereMetaData(); - shardingSphereMetaData.addDatabase("db", TypedSPILoader.getService(DatabaseType.class, "FIXTURE"), new ConfigurationProperties(new Properties())); - MetaDataContexts metaDataContexts = new MetaDataContexts(new MetaDataPersistService(new ClusterPersistRepositoryFixture()), shardingSphereMetaData); - ComputeNodeInstance instance = new ComputeNodeInstance(new JDBCInstanceMetaData("42")); - WorkerIdGenerator workerIdGenerator = mock(WorkerIdGenerator.class); - ModeConfiguration modeConfig = new ModeConfiguration("Type", new ClusterPersistRepositoryConfiguration("Type", "Namespace", "Server Lists", new Properties())); - ClusterModeContextManager modeContextManager = new ClusterModeContextManager(); - ContextManager contextManager = new ContextManager(metaDataContexts, new InstanceContext(instance, workerIdGenerator, modeConfig, modeContextManager, null, new EventBusContext())); - contextManager.getResourceMetaDataContextManager().addSchema("db", "Schema Name"); - ClusterModeContextManager clusterModeContextManager = new ClusterModeContextManager(); - clusterModeContextManager.setContextManagerAware(contextManager); - assertDoesNotThrow(() -> clusterModeContextManager.alterSchema(new AlterSchemaPOJO("db", "Schema Name", "Rename Schema Name", new LinkedList<>()))); - } - - @Test - void assertDropSchema() { - ClusterModeContextManager clusterModeContextManager = new ClusterModeContextManager(); - MetaDataPersistService persistService = new MetaDataPersistService(new ClusterPersistRepositoryFixture()); - MetaDataContexts metaDataContexts = new MetaDataContexts(persistService, new ShardingSphereMetaData()); - ComputeNodeInstance instance = new ComputeNodeInstance(new JDBCInstanceMetaData("42")); - WorkerIdGenerator workerIdGenerator = mock(WorkerIdGenerator.class); - ModeConfiguration modeConfig = new ModeConfiguration("Type", new ClusterPersistRepositoryConfiguration("Type", "Namespace", "Server Lists", new Properties())); - ClusterModeContextManager modeContextManager = new ClusterModeContextManager(); - clusterModeContextManager.setContextManagerAware( - new ContextManager(metaDataContexts, new InstanceContext(instance, workerIdGenerator, modeConfig, modeContextManager, null, new EventBusContext()))); - assertDoesNotThrow(() -> clusterModeContextManager.dropSchema("db", new LinkedList<>())); - } - - @Test - void assertDropSchemaWithEmptyList() { - ClusterModeContextManager clusterModeContextManager = new ClusterModeContextManager(); - MetaDataPersistService persistService = new MetaDataPersistService(new ClusterPersistRepositoryFixture()); - MetaDataContexts metaDataContexts = new MetaDataContexts(persistService, new ShardingSphereMetaData()); - ComputeNodeInstance instance = new ComputeNodeInstance(new JDBCInstanceMetaData("42")); - WorkerIdGenerator workerIdGenerator = mock(WorkerIdGenerator.class); - ModeConfiguration modeConfig = new ModeConfiguration("Type", new ClusterPersistRepositoryConfiguration("Type", "Namespace", "Server Lists", new Properties())); - ClusterModeContextManager modeContextManager = new ClusterModeContextManager(); - clusterModeContextManager.setContextManagerAware( - new ContextManager(metaDataContexts, new InstanceContext(instance, workerIdGenerator, modeConfig, modeContextManager, null, new EventBusContext()))); - assertDoesNotThrow(() -> clusterModeContextManager.dropSchema("db", Collections.singleton("foo"))); - } - - @Test - void assertDropSchemaWithListContainingRootAndNonRoot() { - ClusterModeContextManager clusterModeContextManager = new ClusterModeContextManager(); - MetaDataPersistService persistService = new MetaDataPersistService(new ClusterPersistRepositoryFixture()); - MetaDataContexts metaDataContexts = new MetaDataContexts(persistService, new ShardingSphereMetaData()); - ComputeNodeInstance instance = new ComputeNodeInstance(new JDBCInstanceMetaData("42")); - WorkerIdGenerator workerIdGenerator = mock(WorkerIdGenerator.class); - ModeConfiguration modeConfig = new ModeConfiguration("Type", new ClusterPersistRepositoryConfiguration("Type", "Namespace", "Server Lists", new Properties())); - ClusterModeContextManager modeContextManager = new ClusterModeContextManager(); - clusterModeContextManager.setContextManagerAware( - new ContextManager(metaDataContexts, new InstanceContext(instance, workerIdGenerator, modeConfig, modeContextManager, null, new EventBusContext()))); - Collection stringList = new LinkedList<>(); - stringList.add("/"); - stringList.add("foo"); - assertDoesNotThrow(() -> clusterModeContextManager.dropSchema("db", stringList)); - } - - @Test - void assertAlterSchemaMetaData() { - ClusterModeContextManager clusterModeContextManager = new ClusterModeContextManager(); - MetaDataPersistService persistService = new MetaDataPersistService(new ClusterPersistRepositoryFixture()); - MetaDataContexts metaDataContexts = new MetaDataContexts(persistService, new ShardingSphereMetaData()); - ComputeNodeInstance instance = new ComputeNodeInstance(new JDBCInstanceMetaData("42")); - WorkerIdGenerator workerIdGenerator = mock(WorkerIdGenerator.class); - ModeConfiguration modeConfig = new ModeConfiguration("Type", new ClusterPersistRepositoryConfiguration("Type", "Namespace", "Server Lists", new Properties())); - ClusterModeContextManager modeContextManager = new ClusterModeContextManager(); - clusterModeContextManager.setContextManagerAware( - new ContextManager(metaDataContexts, new InstanceContext(instance, workerIdGenerator, modeConfig, modeContextManager, null, new EventBusContext()))); - AlterSchemaMetaDataPOJO alterSchemaMetaDataPOJO = new AlterSchemaMetaDataPOJO("db", "Schema Name"); - clusterModeContextManager.alterSchemaMetaData(alterSchemaMetaDataPOJO); - assertNull(alterSchemaMetaDataPOJO.getLogicDataSourceName()); - } - - @Test - void assertRegisterStorageUnits() { - ClusterModeContextManager clusterModeContextManager = new ClusterModeContextManager(); - MetaDataPersistService persistService = new MetaDataPersistService(new ClusterPersistRepositoryFixture()); - MetaDataContexts metaDataContexts = new MetaDataContexts(persistService, new ShardingSphereMetaData()); - ComputeNodeInstance instance = new ComputeNodeInstance(new JDBCInstanceMetaData("42")); - WorkerIdGenerator workerIdGenerator = mock(WorkerIdGenerator.class); - ModeConfiguration modeConfig = new ModeConfiguration("Type", new ClusterPersistRepositoryConfiguration("Type", "Namespace", "Server Lists", new Properties())); - ClusterModeContextManager modeContextManager = new ClusterModeContextManager(); - clusterModeContextManager.setContextManagerAware( - new ContextManager(metaDataContexts, new InstanceContext(instance, workerIdGenerator, modeConfig, modeContextManager, null, new EventBusContext()))); - assertDoesNotThrow(() -> clusterModeContextManager.registerStorageUnits("db", new TreeMap<>())); - } - - @Test - void assertAlterStorageUnits() { - ClusterModeContextManager clusterModeContextManager = new ClusterModeContextManager(); - MetaDataPersistService persistService = new MetaDataPersistService(new ClusterPersistRepositoryFixture()); - MetaDataContexts metaDataContexts = new MetaDataContexts(persistService, new ShardingSphereMetaData()); - ComputeNodeInstance instance = new ComputeNodeInstance(new JDBCInstanceMetaData("42")); - WorkerIdGenerator workerIdGenerator = mock(WorkerIdGenerator.class); - ModeConfiguration modeConfig = new ModeConfiguration("Type", - new ClusterPersistRepositoryConfiguration("Type", "Namespace", "Server Lists", new Properties())); - ClusterModeContextManager modeContextManager = new ClusterModeContextManager(); - clusterModeContextManager - .setContextManagerAware(new ContextManager(metaDataContexts, new InstanceContext(instance, workerIdGenerator, - modeConfig, modeContextManager, null, new EventBusContext()))); - assertDoesNotThrow(() -> clusterModeContextManager.alterStorageUnits("db", new TreeMap<>())); - } - - @Test - void assertAlterStorageUnitsWithProcessListClusterPerRepoFix() { - ClusterModeContextManager clusterModeContextManager = new ClusterModeContextManager(); - MetaDataPersistService persistService = new MetaDataPersistService( - new ProcessListClusterPersistRepositoryFixture()); - MetaDataContexts metaDataContexts = new MetaDataContexts(persistService, new ShardingSphereMetaData()); - ComputeNodeInstance instance = new ComputeNodeInstance(new JDBCInstanceMetaData("42")); - WorkerIdGenerator workerIdGenerator = mock(WorkerIdGenerator.class); - ModeConfiguration modeConfig = new ModeConfiguration("Type", - new ClusterPersistRepositoryConfiguration("Type", "Namespace", "Server Lists", new Properties())); - ClusterModeContextManager modeContextManager = new ClusterModeContextManager(); - clusterModeContextManager - .setContextManagerAware(new ContextManager(metaDataContexts, new InstanceContext(instance, workerIdGenerator, - modeConfig, modeContextManager, null, new EventBusContext()))); - assertDoesNotThrow(() -> clusterModeContextManager.alterStorageUnits("db", new TreeMap<>())); - } - - @Test - void assertAlterStorageUnitsWithDataSourcePoolProperties() { - ClusterModeContextManager clusterModeContextManager = new ClusterModeContextManager(); - MetaDataPersistService persistService = new MetaDataPersistService(new ClusterPersistRepositoryFixture()); - MetaDataContexts metaDataContexts = new MetaDataContexts(persistService, new ShardingSphereMetaData()); - ComputeNodeInstance instance = new ComputeNodeInstance(new JDBCInstanceMetaData("42")); - WorkerIdGenerator workerIdGenerator = mock(WorkerIdGenerator.class); - ModeConfiguration modeConfig = new ModeConfiguration("Type", - new ClusterPersistRepositoryConfiguration("Type", "Namespace", "Server Lists", new Properties())); - ClusterModeContextManager modeContextManager = new ClusterModeContextManager(); - clusterModeContextManager.setContextManagerAware( - new ContextManager(metaDataContexts, new InstanceContext(instance, workerIdGenerator, modeConfig, modeContextManager, null, new EventBusContext()))); - Map propsMap = new HashMap<>(); - propsMap.put("active_version", new DataSourcePoolProperties("active_version", new HashMap<>())); - assertDoesNotThrow(() -> clusterModeContextManager.alterStorageUnits("db", propsMap)); - } - - @Test - void assertAlterStorageUnitsInvalidName() { - ClusterModeContextManager clusterModeContextManager = new ClusterModeContextManager(); - MetaDataPersistService persistService = new MetaDataPersistService(new ClusterPersistRepositoryFixture()); - MetaDataContexts metaDataContexts = new MetaDataContexts(persistService, new ShardingSphereMetaData()); - ComputeNodeInstance instance = new ComputeNodeInstance(new JDBCInstanceMetaData("42")); - WorkerIdGenerator workerIdGenerator = mock(WorkerIdGenerator.class); - ModeConfiguration modeConfig = new ModeConfiguration("Type", - new ClusterPersistRepositoryConfiguration("Type", "Namespace", "Server Lists", new Properties())); - ClusterModeContextManager modeContextManager = new ClusterModeContextManager(); - clusterModeContextManager.setContextManagerAware( - new ContextManager(metaDataContexts, new InstanceContext(instance, workerIdGenerator, modeConfig, modeContextManager, null, new EventBusContext()))); - Map propsMap = new HashMap<>(); - propsMap.put("\n", new DataSourcePoolProperties("\n", new HashMap<>())); - propsMap.put("active_version", new DataSourcePoolProperties("active_version", new HashMap<>())); - assertDoesNotThrow(() -> clusterModeContextManager.alterStorageUnits("db", propsMap)); - } - - @Test - void assertAlterStorageUnitsWithoutDataSourcePoolProperties() { - ClusterModeContextManager clusterModeContextManager = new ClusterModeContextManager(); - MetaDataPersistService persistService = new MetaDataPersistService(new ProcessListClusterPersistRepositoryFixture()); - Map databases = new HashMap<>(); - RuleMetaData globalRuleMetaData = new RuleMetaData(new LinkedList<>()); - MetaDataContexts metaDataContexts = new MetaDataContexts( - persistService, new ShardingSphereMetaData(databases, mock(ResourceMetaData.class), globalRuleMetaData, new ConfigurationProperties(new Properties()))); - ComputeNodeInstance instance = new ComputeNodeInstance(new JDBCInstanceMetaData("42")); - WorkerIdGenerator workerIdGenerator = mock(WorkerIdGenerator.class); - ModeConfiguration modeConfig = new ModeConfiguration("Type", new ClusterPersistRepositoryConfiguration("Type", "Namespace", "Server Lists", new Properties())); - ClusterModeContextManager modeContextManager = new ClusterModeContextManager(); - clusterModeContextManager.setContextManagerAware( - new ContextManager(metaDataContexts, new InstanceContext(instance, workerIdGenerator, modeConfig, modeContextManager, null, new EventBusContext()))); - assertDoesNotThrow(() -> clusterModeContextManager.alterStorageUnits("db", new TreeMap<>())); - } - - @Test - void assertAlterStorageUnitsWithEmptyDataSourcePoolPropertiesMap() { - ClusterModeContextManager clusterModeContextManager = new ClusterModeContextManager(); - MetaDataPersistService persistService = new MetaDataPersistService(new ProcessListClusterPersistRepositoryFixture()); - MetaDataContexts metaDataContexts = new MetaDataContexts(persistService, new ShardingSphereMetaData()); - ComputeNodeInstance instance = new ComputeNodeInstance(new JDBCInstanceMetaData("42")); - WorkerIdGenerator workerIdGenerator = mock(WorkerIdGenerator.class); - ModeConfiguration modeConfig = new ModeConfiguration("Type", new ClusterPersistRepositoryConfiguration("Type", "Namespace", "Server Lists", new Properties())); - ClusterModeContextManager modeContextManager = new ClusterModeContextManager(); - clusterModeContextManager.setContextManagerAware( - new ContextManager(metaDataContexts, new InstanceContext(instance, workerIdGenerator, modeConfig, modeContextManager, null, new EventBusContext()))); - Map propsMap = new HashMap<>(new TreeMap<>()); - assertDoesNotThrow(() -> clusterModeContextManager.alterStorageUnits("db", propsMap)); - } - - @Test - void assertAlterStorageUnitsWithOneDataSourcePoolProperties() { - ClusterModeContextManager clusterModeContextManager = new ClusterModeContextManager(); - MetaDataPersistService persistService = new MetaDataPersistService(new ProcessListClusterPersistRepositoryFixture()); - MetaDataContexts metaDataContexts = new MetaDataContexts(persistService, new ShardingSphereMetaData()); - ComputeNodeInstance instance = new ComputeNodeInstance(new JDBCInstanceMetaData("42")); - WorkerIdGenerator workerIdGenerator = mock(WorkerIdGenerator.class); - ModeConfiguration modeConfig = new ModeConfiguration("Type", new ClusterPersistRepositoryConfiguration("Type", "Namespace", "Server Lists", new Properties())); - ClusterModeContextManager modeContextManager = new ClusterModeContextManager(); - clusterModeContextManager.setContextManagerAware( - new ContextManager(metaDataContexts, new InstanceContext(instance, workerIdGenerator, modeConfig, modeContextManager, null, new EventBusContext()))); - Map propsMap = new HashMap<>(); - propsMap.put("42", new DataSourcePoolProperties("active_version", new HashMap<>())); - assertDoesNotThrow(() -> clusterModeContextManager.alterStorageUnits("db", propsMap)); - } - - @Test - void assertUnregisterStorageUnits() { - ClusterModeContextManager clusterModeContextManager = new ClusterModeContextManager(); - MetaDataPersistService persistService = new MetaDataPersistService(new ClusterPersistRepositoryFixture()); - MetaDataContexts metaDataContexts = new MetaDataContexts(persistService, createShardingSphereMetaData()); - ComputeNodeInstance instance = new ComputeNodeInstance(new JDBCInstanceMetaData("42")); - WorkerIdGenerator workerIdGenerator = mock(WorkerIdGenerator.class); - ModeConfiguration modeConfig = new ModeConfiguration("Type", new ClusterPersistRepositoryConfiguration("Type", "Namespace", "Server Lists", new Properties())); - ClusterModeContextManager modeContextManager = new ClusterModeContextManager(); - clusterModeContextManager.setContextManagerAware( - new ContextManager(metaDataContexts, new InstanceContext(instance, workerIdGenerator, modeConfig, modeContextManager, null, new EventBusContext()))); - assertDoesNotThrow(() -> clusterModeContextManager.unregisterStorageUnits("db", new LinkedList<>())); - } - - @Test - void assertUnregisterStorageUnitsWithProcessListClusterPersistRepoFixture() { - ClusterModeContextManager clusterModeContextManager = new ClusterModeContextManager(); - MetaDataPersistService persistService = new MetaDataPersistService(new ProcessListClusterPersistRepositoryFixture()); - MetaDataContexts metaDataContexts = new MetaDataContexts(persistService, createShardingSphereMetaData()); - ComputeNodeInstance instance = new ComputeNodeInstance(new JDBCInstanceMetaData("42")); - WorkerIdGenerator workerIdGenerator = mock(WorkerIdGenerator.class); - ModeConfiguration modeConfig = new ModeConfiguration("Type", new ClusterPersistRepositoryConfiguration("Type", "Namespace", "Server Lists", new Properties())); - ClusterModeContextManager modeContextManager = new ClusterModeContextManager(); - clusterModeContextManager.setContextManagerAware( - new ContextManager(metaDataContexts, new InstanceContext(instance, workerIdGenerator, modeConfig, modeContextManager, null, new EventBusContext()))); - assertDoesNotThrow(() -> clusterModeContextManager.unregisterStorageUnits("db", new LinkedList<>())); - } - - @Test - void assertUnregisterStorageUnitsWithClusterPersistRepoFixture() { - ClusterModeContextManager clusterModeContextManager = new ClusterModeContextManager(); - MetaDataPersistService persistService = new MetaDataPersistService(new ClusterPersistRepositoryFixture()); - MetaDataContexts metaDataContexts = new MetaDataContexts(persistService, createShardingSphereMetaData()); - ComputeNodeInstance instance = new ComputeNodeInstance(new JDBCInstanceMetaData("42")); - WorkerIdGenerator workerIdGenerator = mock(WorkerIdGenerator.class); - ModeConfiguration modeConfig = new ModeConfiguration("Type", new ClusterPersistRepositoryConfiguration("Type", "Namespace", "Server Lists", new Properties())); - ClusterModeContextManager modeContextManager = new ClusterModeContextManager(); - clusterModeContextManager.setContextManagerAware( - new ContextManager(metaDataContexts, new InstanceContext(instance, workerIdGenerator, modeConfig, modeContextManager, null, new EventBusContext()))); - assertDoesNotThrow(() -> clusterModeContextManager.alterRuleConfiguration("db", new LinkedList<>())); - } - - @Test - void assertAlterRuleConfiguration() { - ClusterModeContextManager clusterModeContextManager = new ClusterModeContextManager(); - MetaDataPersistService persistService = new MetaDataPersistService(new ClusterPersistRepositoryFixture()); - MetaDataContexts metaDataContexts = new MetaDataContexts(persistService, createShardingSphereMetaData()); - ComputeNodeInstance instance = new ComputeNodeInstance(new JDBCInstanceMetaData("42")); - WorkerIdGenerator workerIdGenerator = mock(WorkerIdGenerator.class); - ModeConfiguration modeConfig = new ModeConfiguration("Type", new ClusterPersistRepositoryConfiguration("Type", "Namespace", "Server Lists", new Properties())); - ClusterModeContextManager modeContextManager = new ClusterModeContextManager(); - clusterModeContextManager.setContextManagerAware( - new ContextManager(metaDataContexts, new InstanceContext(instance, workerIdGenerator, modeConfig, modeContextManager, null, new EventBusContext()))); - Collection ruleConfigs = new LinkedList<>(); - ruleConfigs.add(new RuleConfigurationFixture()); - assertDoesNotThrow(() -> clusterModeContextManager.alterRuleConfiguration("db", ruleConfigs)); - } - - private ShardingSphereMetaData createShardingSphereMetaData() { - return new ShardingSphereMetaData(Collections.singletonMap("db", new ShardingSphereDatabase("db", TypedSPILoader.getService(DatabaseType.class, "FIXTURE"), - new ResourceMetaData(Collections.emptyMap()), new RuleMetaData(Collections.emptyList()), Collections.emptyMap())), - mock(ResourceMetaData.class), new RuleMetaData(Collections.emptyList()), new ConfigurationProperties(new Properties())); - } - - @Test - void assertAlterRuleConfigurationMultiple() { - ClusterModeContextManager clusterModeContextManager = new ClusterModeContextManager(); - MetaDataPersistService persistService = new MetaDataPersistService(new ClusterPersistRepositoryFixture()); - MetaDataContexts metaDataContexts = new MetaDataContexts(persistService, createShardingSphereMetaData()); - ComputeNodeInstance instance = new ComputeNodeInstance(new JDBCInstanceMetaData("42")); - WorkerIdGenerator workerIdGenerator = mock(WorkerIdGenerator.class); - ModeConfiguration modeConfig = new ModeConfiguration("Type", new ClusterPersistRepositoryConfiguration("Type", "Namespace", "Server Lists", new Properties())); - ClusterModeContextManager modeContextManager = new ClusterModeContextManager(); - clusterModeContextManager.setContextManagerAware( - new ContextManager(metaDataContexts, new InstanceContext(instance, workerIdGenerator, modeConfig, modeContextManager, null, new EventBusContext()))); - Collection ruleConfigurationList = new LinkedList<>(); - ruleConfigurationList.add(new RuleConfigurationFixture()); - ruleConfigurationList.add(new RuleConfigurationFixture()); - assertDoesNotThrow(() -> clusterModeContextManager.alterRuleConfiguration("db", ruleConfigurationList)); - } - - @Test - void assertAlterRuleConfigurationWithPersistService() { - ClusterModeContextManager clusterModeContextManager = new ClusterModeContextManager(); - MetaDataPersistService persistService = new MetaDataPersistService(new ProcessListClusterPersistRepositoryFixture()); - MetaDataContexts metaDataContexts = new MetaDataContexts(persistService, createShardingSphereMetaData()); - ComputeNodeInstance instance = new ComputeNodeInstance(new JDBCInstanceMetaData("42")); - WorkerIdGenerator workerIdGenerator = mock(WorkerIdGenerator.class); - ModeConfiguration modeConfig = new ModeConfiguration("Type", new ClusterPersistRepositoryConfiguration("Type", "Namespace", "Server Lists", new Properties())); - ClusterModeContextManager modeContextManager = new ClusterModeContextManager(); - clusterModeContextManager.setContextManagerAware( - new ContextManager(metaDataContexts, new InstanceContext(instance, workerIdGenerator, modeConfig, modeContextManager, null, new EventBusContext()))); - Collection ruleConfigs = new LinkedList<>(); - ruleConfigs.add(new RuleConfigurationFixture()); - assertDoesNotThrow(() -> clusterModeContextManager.alterRuleConfiguration("db", ruleConfigs)); - } - - @Test - void assertAlterGlobalRuleConfigurationWithEmptyRuleConfigurations() { - ClusterModeContextManager clusterModeContextManager = new ClusterModeContextManager(); - MetaDataPersistService persistService = new MetaDataPersistService(new ClusterPersistRepositoryFixture()); - MetaDataContexts metaDataContexts = new MetaDataContexts(persistService, createShardingSphereMetaData()); - ComputeNodeInstance instance = new ComputeNodeInstance(new JDBCInstanceMetaData("42")); - WorkerIdGenerator workerIdGenerator = mock(WorkerIdGenerator.class); - ModeConfiguration modeConfig = new ModeConfiguration("Type", new ClusterPersistRepositoryConfiguration("Type", "Namespace", "Server Lists", new Properties())); - ClusterModeContextManager modeContextManager = new ClusterModeContextManager(); - clusterModeContextManager.setContextManagerAware( - new ContextManager(metaDataContexts, new InstanceContext(instance, workerIdGenerator, modeConfig, modeContextManager, null, new EventBusContext()))); - assertDoesNotThrow(() -> clusterModeContextManager.alterGlobalRuleConfiguration(new LinkedList<>())); - } - - @Test - void assertAlterGlobalRuleConfigurationWithSingleRuleConfigurations() { - ClusterModeContextManager clusterModeContextManager = new ClusterModeContextManager(); - MetaDataPersistService persistService = new MetaDataPersistService(new ClusterPersistRepositoryFixture()); - MetaDataContexts metaDataContexts = new MetaDataContexts(persistService, new ShardingSphereMetaData()); - ComputeNodeInstance instance = new ComputeNodeInstance(new JDBCInstanceMetaData("42")); - WorkerIdGenerator workerIdGenerator = mock(WorkerIdGenerator.class); - ModeConfiguration modeConfig = new ModeConfiguration("Type", new ClusterPersistRepositoryConfiguration("Type", "Namespace", "Server Lists", new Properties())); - ClusterModeContextManager modeContextManager = new ClusterModeContextManager(); - clusterModeContextManager.setContextManagerAware( - new ContextManager(metaDataContexts, new InstanceContext(instance, workerIdGenerator, modeConfig, modeContextManager, null, new EventBusContext()))); - Collection ruleConfigs = new LinkedList<>(); - ruleConfigs.add(new RuleConfigurationFixture()); - assertDoesNotThrow(() -> clusterModeContextManager.alterGlobalRuleConfiguration(ruleConfigs)); - } - - @Test - void assertAlterGlobalRuleConfigurationWithMultipleRuleConfigurations() { - ClusterModeContextManager clusterModeContextManager = new ClusterModeContextManager(); - MetaDataPersistService persistService = new MetaDataPersistService(new ClusterPersistRepositoryFixture()); - MetaDataContexts metaDataContexts = new MetaDataContexts(persistService, new ShardingSphereMetaData()); - ComputeNodeInstance instance = new ComputeNodeInstance(new JDBCInstanceMetaData("42")); - WorkerIdGenerator workerIdGenerator = mock(WorkerIdGenerator.class); - ModeConfiguration modeConfig = new ModeConfiguration("Type", new ClusterPersistRepositoryConfiguration("Type", "Namespace", "Server Lists", new Properties())); - ClusterModeContextManager modeContextManager = new ClusterModeContextManager(); - clusterModeContextManager.setContextManagerAware( - new ContextManager(metaDataContexts, new InstanceContext(instance, workerIdGenerator, modeConfig, modeContextManager, null, new EventBusContext()))); - Collection ruleConfigs = new LinkedList<>(); - ruleConfigs.add(new RuleConfigurationFixture()); - ruleConfigs.add(new RuleConfigurationFixture()); - assertDoesNotThrow(() -> clusterModeContextManager.alterGlobalRuleConfiguration(ruleConfigs)); - } - - @Test - void assertAlterProperties() { - ClusterModeContextManager clusterModeContextManager = new ClusterModeContextManager(); - MetaDataPersistService persistService = new MetaDataPersistService(new ClusterPersistRepositoryFixture()); - MetaDataContexts metaDataContexts = new MetaDataContexts(persistService, new ShardingSphereMetaData()); - ComputeNodeInstance instance = new ComputeNodeInstance(new JDBCInstanceMetaData("42")); - WorkerIdGenerator workerIdGenerator = mock(WorkerIdGenerator.class); - ModeConfiguration modeConfig = new ModeConfiguration("Type", new ClusterPersistRepositoryConfiguration("Type", "Namespace", "Server Lists", new Properties())); - ClusterModeContextManager modeContextManager = new ClusterModeContextManager(); - clusterModeContextManager.setContextManagerAware( - new ContextManager(metaDataContexts, new InstanceContext(instance, workerIdGenerator, modeConfig, modeContextManager, null, new EventBusContext()))); - assertDoesNotThrow(() -> clusterModeContextManager.alterProperties(new Properties())); - } - - @Test - void assertConstructor() { - MetaDataPersistService persistService = new MetaDataPersistService(new ClusterPersistRepositoryFixture()); - try (MetaDataContexts metaDataContexts = new MetaDataContexts(persistService, new ShardingSphereMetaData())) { - ComputeNodeInstance instance = new ComputeNodeInstance(new JDBCInstanceMetaData("42")); - WorkerIdGenerator workerIdGenerator = mock(WorkerIdGenerator.class); - ModeConfiguration modeConfig = new ModeConfiguration("Type", new ClusterPersistRepositoryConfiguration("Type", "Namespace", "Server Lists", new Properties())); - ClusterModeContextManager modeContextManager = new ClusterModeContextManager(); - assertDoesNotThrow(() -> new ClusterModeContextManager().setContextManagerAware( - new ContextManager(metaDataContexts, new InstanceContext(instance, workerIdGenerator, modeConfig, modeContextManager, null, new EventBusContext())))); - } - } -} diff --git a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/config/watcher/GlobalRuleChangedWatcherTest.java b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/config/watcher/GlobalRuleChangedWatcherTest.java deleted file mode 100644 index 554038fa017d8..0000000000000 --- a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/config/watcher/GlobalRuleChangedWatcherTest.java +++ /dev/null @@ -1,50 +0,0 @@ -/* - * 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.mode.manager.cluster.coordinator.registry.config.watcher; - -import lombok.SneakyThrows; -import org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.config.event.rule.GlobalRuleConfigurationsChangedEvent; -import org.apache.shardingsphere.mode.event.DataChangedEvent; -import org.apache.shardingsphere.mode.event.DataChangedEvent.Type; -import org.junit.jupiter.api.Test; - -import java.io.IOException; -import java.net.URISyntaxException; -import java.nio.file.Files; -import java.nio.file.Paths; -import java.util.Optional; -import java.util.stream.Collectors; - -import static org.hamcrest.CoreMatchers.instanceOf; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.junit.jupiter.api.Assertions.assertTrue; - -class GlobalRuleChangedWatcherTest { - - @Test - void assertCreateEvent() { - Optional event = new GlobalRuleChangedWatcher().createGovernanceEvent(new DataChangedEvent("/rules", readYAML(), Type.UPDATED)); - assertTrue(event.isPresent()); - assertThat(event.get(), instanceOf(GlobalRuleConfigurationsChangedEvent.class)); - } - - @SneakyThrows({IOException.class, URISyntaxException.class}) - private String readYAML() { - return Files.readAllLines(Paths.get(ClassLoader.getSystemResource("yaml/authority-rule.yaml").toURI())).stream().map(each -> each + System.lineSeparator()).collect(Collectors.joining()); - } -} diff --git a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/config/watcher/PropertiesChangedWatcherTest.java b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/config/watcher/PropertiesChangedWatcherTest.java deleted file mode 100644 index 0f0d2e1301915..0000000000000 --- a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/config/watcher/PropertiesChangedWatcherTest.java +++ /dev/null @@ -1,47 +0,0 @@ -/* - * 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.mode.manager.cluster.coordinator.registry.config.watcher; - -import org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.config.event.props.PropertiesChangedEvent; -import org.apache.shardingsphere.mode.event.DataChangedEvent; -import org.apache.shardingsphere.mode.event.DataChangedEvent.Type; -import org.apache.shardingsphere.infra.config.props.ConfigurationPropertyKey; -import org.junit.jupiter.api.Test; - -import java.util.Optional; - -import static org.junit.jupiter.api.Assertions.assertTrue; - -class PropertiesChangedWatcherTest { - - private static final String PROPERTIES_YAML = ConfigurationPropertyKey.SQL_SHOW.getKey() + ": true"; - - @Test - void assertCreateEvent() { - Optional actual = new PropertiesChangedWatcher().createGovernanceEvent(new DataChangedEvent("/props", PROPERTIES_YAML, Type.UPDATED)); - assertTrue(actual.isPresent()); - assertTrue((boolean) actual.get().getProps().get(ConfigurationPropertyKey.SQL_SHOW.getKey())); - } - - @Test - void assertCreateAddEvent() { - Optional actual = new PropertiesChangedWatcher().createGovernanceEvent(new DataChangedEvent("/props", PROPERTIES_YAML, Type.ADDED)); - assertTrue(actual.isPresent()); - assertTrue((boolean) actual.get().getProps().get(ConfigurationPropertyKey.SQL_SHOW.getKey())); - } -} diff --git a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/metadata/watcher/MetaDataChangedWatcherTest.java b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/metadata/watcher/MetaDataChangedWatcherTest.java deleted file mode 100644 index 9c4325c22e1b2..0000000000000 --- a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/metadata/watcher/MetaDataChangedWatcherTest.java +++ /dev/null @@ -1,139 +0,0 @@ -/* - * 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.mode.manager.cluster.coordinator.registry.metadata.watcher; - -import org.apache.shardingsphere.infra.rule.event.GovernanceEvent; -import org.apache.shardingsphere.mode.event.schema.TableMetaDataChangedEvent; -import org.apache.shardingsphere.mode.event.DataChangedEvent; -import org.apache.shardingsphere.mode.event.DataChangedEvent.Type; -import org.junit.jupiter.api.Test; - -import java.util.Optional; - -import static org.hamcrest.CoreMatchers.is; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.junit.jupiter.api.Assertions.assertTrue; - -class MetaDataChangedWatcherTest { - - @Test - void assertCreateEventWithInvalidPath() { - String key = "/metadata_invalid/sharding_db/sharding_schema"; - String value = "encrypt_db"; - Optional actual = createEvent(key, value, Type.UPDATED); - assertFalse(actual.isPresent()); - actual = createEvent(key, value, Type.ADDED); - assertFalse(actual.isPresent()); - actual = createEvent(key, value, Type.DELETED); - assertFalse(actual.isPresent()); - actual = createEvent(key, value, Type.IGNORED); - assertFalse(actual.isPresent()); - } - - @Test - void assertCreateAddedEvent() { - String key = "/metadata/sharding_db"; - String value = "encrypt_db"; - Optional actual = createEvent(key, value, Type.UPDATED); - assertTrue(actual.isPresent()); - actual = createEvent(key, value, Type.ADDED); - assertTrue(actual.isPresent()); - actual = createEvent(key, value, Type.IGNORED); - assertFalse(actual.isPresent()); - } - - @Test - void assertEmptyValue() { - String key = "/metadata/sharding_db/data_sources"; - Optional actual = createEvent(key, null, Type.UPDATED); - assertFalse(actual.isPresent()); - } - - @Test - void assertCreateDatabaseDeletedEvent() { - String key = "/metadata/sharding_db"; - String value = "encrypt_db"; - Optional actual = createEvent(key, value, Type.DELETED); - assertTrue(actual.isPresent()); - } - - @Test - void assertCreateDataSourceChangedEvent() { - String key = "/metadata/sharding_db/versions/0/data_sources/units"; - String value = "{}"; - Optional actual = createEvent(key, value, Type.UPDATED); - assertTrue(actual.isPresent()); - } - - @Test - void assertCreateDataSourceChangedEventWithAddEvent() { - String key = "/metadata/sharding_db/versions/0/data_sources/units"; - String value = "{}"; - Optional actual = createEvent(key, value, Type.ADDED); - assertTrue(actual.isPresent()); - } - - @Test - void assertCreateRuleChangedEvent() { - String key = "/metadata/sharding_db/versions/0/rules"; - Optional actual = createEvent(key, "[]", Type.UPDATED); - assertTrue(actual.isPresent()); - } - - @Test - void assertCreateRuleChangedEventWithAddEvent() { - String key = "/metadata/sharding_db/versions/0/rules"; - Optional actual = createEvent(key, "[]", Type.ADDED); - assertTrue(actual.isPresent()); - } - - @Test - void assertCreateTableSchemaChangedEvent() { - String key = "/metadata/sharding_db/schemas/sharding_schema/tables/t_order"; - Optional actual = createEvent(key, "{}", Type.UPDATED); - assertTrue(actual.isPresent()); - } - - @Test - void assertCreateTableSchemaDeletedEvent() { - String key = "/metadata/sharding_db/schemas/sharding_schema/tables/t_order"; - Optional actual = createEvent(key, "{}", Type.DELETED); - assertTrue(actual.isPresent()); - assertThat(((TableMetaDataChangedEvent) actual.get()).getDeletedTable(), is("t_order")); - } - - @Test - void assertCreateViewMetaDataChangedEvent() { - String key = "/metadata/sharding_db/schemas/sharding_schema/views/foo_view"; - Optional actual = createEvent(key, "{}", Type.UPDATED); - assertTrue(actual.isPresent()); - } - - @Test - void assertCreateViewMetaDataDeletedEvent() { - String key = "/metadata/sharding_db/schemas/sharding_schema/views/foo_view"; - Optional actual = createEvent(key, "{}", Type.DELETED); - assertTrue(actual.isPresent()); - } - - private Optional createEvent(final String key, final String value, final Type type) { - DataChangedEvent dataChangedEvent = new DataChangedEvent(key, value, type); - return new MetaDataChangedWatcher().createGovernanceEvent(dataChangedEvent); - } -} diff --git a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/subscriber/ConfigurationChangedSubscriberTest.java b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/subscriber/ConfigurationChangedSubscriberTest.java deleted file mode 100644 index 55ed9f13379dd..0000000000000 --- a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/subscriber/ConfigurationChangedSubscriberTest.java +++ /dev/null @@ -1,175 +0,0 @@ -/* - * 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.mode.manager.cluster.coordinator.subscriber; - -import org.apache.shardingsphere.authority.config.AuthorityRuleConfiguration; -import org.apache.shardingsphere.authority.rule.AuthorityRule; -import org.apache.shardingsphere.infra.algorithm.core.config.AlgorithmConfiguration; -import org.apache.shardingsphere.infra.config.mode.ModeConfiguration; -import org.apache.shardingsphere.infra.config.props.ConfigurationProperties; -import org.apache.shardingsphere.infra.config.props.ConfigurationPropertyKey; -import org.apache.shardingsphere.infra.config.rule.RuleConfiguration; -import org.apache.shardingsphere.infra.database.core.type.DatabaseType; -import org.apache.shardingsphere.infra.datasource.pool.props.creator.DataSourcePoolPropertiesCreator; -import org.apache.shardingsphere.infra.datasource.pool.props.domain.DataSourcePoolProperties; -import org.apache.shardingsphere.infra.instance.metadata.InstanceMetaData; -import org.apache.shardingsphere.infra.instance.metadata.proxy.ProxyInstanceMetaData; -import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData; -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.database.schema.model.ShardingSphereSchema; -import org.apache.shardingsphere.infra.metadata.user.ShardingSphereUser; -import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader; -import org.apache.shardingsphere.infra.util.eventbus.EventBusContext; -import org.apache.shardingsphere.mode.manager.ContextManager; -import org.apache.shardingsphere.mode.manager.ContextManagerBuilderParameter; -import org.apache.shardingsphere.mode.manager.cluster.ClusterContextManagerBuilder; -import org.apache.shardingsphere.mode.manager.cluster.coordinator.RegistryCenter; -import org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.config.event.datasource.DataSourceUnitsChangedEvent; -import org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.config.event.props.PropertiesChangedEvent; -import org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.config.event.rule.GlobalRuleConfigurationsChangedEvent; -import org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.config.event.rule.RuleConfigurationsChangedEvent; -import org.apache.shardingsphere.mode.metadata.MetaDataContexts; -import org.apache.shardingsphere.mode.repository.cluster.ClusterPersistRepository; -import org.apache.shardingsphere.mode.repository.cluster.ClusterPersistRepositoryConfiguration; -import org.apache.shardingsphere.sqltranslator.rule.SQLTranslatorRule; -import org.apache.shardingsphere.test.fixture.jdbc.MockedDataSource; -import org.apache.shardingsphere.test.util.PropertiesBuilder; -import org.apache.shardingsphere.test.util.PropertiesBuilder.Property; -import org.apache.shardingsphere.transaction.rule.TransactionRule; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.Answers; -import org.mockito.Mock; -import org.mockito.junit.jupiter.MockitoExtension; -import org.mockito.junit.jupiter.MockitoSettings; -import org.mockito.quality.Strictness; - -import java.sql.SQLException; -import java.util.Collection; -import java.util.Collections; -import java.util.LinkedHashMap; -import java.util.LinkedList; -import java.util.Map; -import java.util.Properties; - -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.CoreMatchers.not; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.mockito.Mockito.RETURNS_DEEP_STUBS; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -@ExtendWith(MockitoExtension.class) -@MockitoSettings(strictness = Strictness.LENIENT) -class ConfigurationChangedSubscriberTest { - - private ConfigurationChangedSubscriber subscriber; - - private ContextManager contextManager; - - @Mock(answer = Answers.RETURNS_DEEP_STUBS) - private ShardingSphereDatabase database; - - @Mock - private RuleMetaData globalRuleMetaData; - - @BeforeEach - void setUp() throws SQLException { - contextManager = new ClusterContextManagerBuilder().build(createContextManagerBuilderParameter()); - contextManager.renewMetaDataContexts(new MetaDataContexts(contextManager.getMetaDataContexts().getPersistService(), new ShardingSphereMetaData(createDatabases(), - contextManager.getMetaDataContexts().getMetaData().getGlobalResourceMetaData(), contextManager.getMetaDataContexts().getMetaData().getGlobalRuleMetaData(), - new ConfigurationProperties(new Properties())))); - subscriber = new ConfigurationChangedSubscriber(new RegistryCenter(mock(ClusterPersistRepository.class), - new EventBusContext(), mock(ProxyInstanceMetaData.class), null), contextManager); - } - - private ContextManagerBuilderParameter createContextManagerBuilderParameter() { - ModeConfiguration modeConfig = new ModeConfiguration("Cluster", new ClusterPersistRepositoryConfiguration("FIXTURE", "", "", new Properties())); - InstanceMetaData instanceMetaData = new ProxyInstanceMetaData("foo_instance_id", 3307); - return new ContextManagerBuilderParameter(modeConfig, Collections.emptyMap(), Collections.emptyMap(), Collections.emptyList(), - new Properties(), Collections.emptyList(), instanceMetaData, false); - } - - private Map createDatabases() { - when(database.getName()).thenReturn("db"); - ResourceMetaData resourceMetaData = mock(ResourceMetaData.class, RETURNS_DEEP_STUBS); - when(resourceMetaData.getStorageUnits()).thenReturn(Collections.emptyMap()); - when(database.getResourceMetaData()).thenReturn(resourceMetaData); - when(database.getSchemas()).thenReturn(Collections.singletonMap("foo_schema", new ShardingSphereSchema())); - when(database.getProtocolType()).thenReturn(TypedSPILoader.getService(DatabaseType.class, "FIXTURE")); - when(database.getSchema("foo_schema")).thenReturn(mock(ShardingSphereSchema.class)); - when(database.getRuleMetaData().getRules()).thenReturn(new LinkedList<>()); - when(database.getRuleMetaData().getConfigurations()).thenReturn(Collections.emptyList()); - return Collections.singletonMap("db", database); - } - - @Test - void assertRenewForRuleConfigurationsChanged() { - assertThat(contextManager.getMetaDataContexts().getMetaData().getDatabase("db"), is(database)); - subscriber.renew(new RuleConfigurationsChangedEvent("db", "0", Collections.emptyList())); - assertThat(contextManager.getMetaDataContexts().getMetaData().getDatabase("db"), not(database)); - } - - @Test - void assertRenewForDataSourceChanged() { - subscriber.renew(new DataSourceUnitsChangedEvent("db", "0", createChangedDataSourcePoolPropertiesMap())); - assertTrue(contextManager.getMetaDataContexts().getMetaData().getDatabase("db").getResourceMetaData().getStorageUnits().containsKey("ds_2")); - } - - private Map createChangedDataSourcePoolPropertiesMap() { - MockedDataSource dataSource = new MockedDataSource(); - Map result = new LinkedHashMap<>(3, 1F); - result.put("primary_ds", DataSourcePoolPropertiesCreator.create(dataSource)); - result.put("ds_1", DataSourcePoolPropertiesCreator.create(dataSource)); - result.put("ds_2", DataSourcePoolPropertiesCreator.create(dataSource)); - return result; - } - - @Test - void assertRenewForGlobalRuleConfigurationsChanged() { - GlobalRuleConfigurationsChangedEvent event = new GlobalRuleConfigurationsChangedEvent(getChangedGlobalRuleConfigurations()); - subscriber.renew(event); - assertThat(contextManager.getMetaDataContexts().getMetaData().getGlobalRuleMetaData(), not(globalRuleMetaData)); - assertThat(contextManager.getMetaDataContexts().getMetaData().getGlobalRuleMetaData().getRules().size(), is(3)); - assertThat(contextManager.getMetaDataContexts().getMetaData().getGlobalRuleMetaData().findRules(AuthorityRule.class).size(), is(1)); - assertThat(contextManager.getMetaDataContexts().getMetaData().getGlobalRuleMetaData().findRules(TransactionRule.class).size(), is(1)); - assertThat(contextManager.getMetaDataContexts().getMetaData().getGlobalRuleMetaData().findRules(SQLTranslatorRule.class).size(), is(1)); - } - - private Collection getChangedGlobalRuleConfigurations() { - RuleConfiguration authorityRuleConfig = new AuthorityRuleConfiguration(getShardingSphereUsers(), new AlgorithmConfiguration("ALL_PERMITTED", new Properties()), Collections.emptyMap(), null); - return Collections.singleton(authorityRuleConfig); - } - - private Collection getShardingSphereUsers() { - Collection result = new LinkedList<>(); - result.add(new ShardingSphereUser("root", "root", "%")); - result.add(new ShardingSphereUser("sharding", "sharding", "localhost")); - return result; - } - - @Test - void assertRenewProperties() { - subscriber.renew(new PropertiesChangedEvent(PropertiesBuilder.build(new Property(ConfigurationPropertyKey.SQL_SHOW.getKey(), Boolean.TRUE.toString())))); - assertThat(contextManager.getMetaDataContexts().getMetaData().getProps().getProps().getProperty(ConfigurationPropertyKey.SQL_SHOW.getKey()), is(Boolean.TRUE.toString())); - } -} 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 19b62baffed8d..6ca87662d910c 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 @@ -27,12 +27,8 @@ import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData; import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema; -import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereTable; -import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereView; import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader; import org.apache.shardingsphere.metadata.persist.MetaDataPersistService; -import org.apache.shardingsphere.mode.event.schema.TableMetaDataChangedEvent; -import org.apache.shardingsphere.mode.event.schema.ViewMetaDataChangedEvent; import org.apache.shardingsphere.mode.manager.ContextManager; import org.apache.shardingsphere.mode.manager.ContextManagerBuilderParameter; import org.apache.shardingsphere.mode.manager.cluster.ClusterContextManagerBuilder; @@ -140,22 +136,4 @@ void assertRenewForSchemaDeleted() { subscriber.renew(new SchemaDeletedEvent("db", "foo_schema")); verify(contextManager.getMetaDataContexts().getMetaData().getDatabase("db")).dropSchema("foo_schema"); } - - @Test - void assertRenewForTableMetaDataChangedChanged() { - when(contextManager.getMetaDataContexts().getMetaData().getDatabase("db").containsSchema("db")).thenReturn(true); - ShardingSphereTable changedTableMetaData = new ShardingSphereTable("t_order", Collections.emptyList(), Collections.emptyList(), Collections.emptyList()); - TableMetaDataChangedEvent event = new TableMetaDataChangedEvent("db", "db", changedTableMetaData, null); - subscriber.renew(event); - verify(contextManager.getMetaDataContexts().getMetaData().getDatabase("db").getSchema("db")).putTable("t_order", event.getChangedTableMetaData()); - } - - @Test - void assertRenewForViewMetaDataChanged() { - when(contextManager.getMetaDataContexts().getMetaData().getDatabase("db").containsSchema("db")).thenReturn(true); - ShardingSphereView changedViewMetaData = new ShardingSphereView("t_order_view", ""); - ViewMetaDataChangedEvent event = new ViewMetaDataChangedEvent("db", "db", changedViewMetaData, null); - subscriber.renew(event); - verify(contextManager.getMetaDataContexts().getMetaData().getDatabase("db").getSchema("db")).putView("t_order_view", event.getChangedViewMetaData()); - } } diff --git a/mode/type/standalone/core/src/main/java/org/apache/shardingsphere/mode/manager/standalone/NewStandaloneContextManagerBuilder.java b/mode/type/standalone/core/src/main/java/org/apache/shardingsphere/mode/manager/standalone/NewStandaloneContextManagerBuilder.java deleted file mode 100644 index 5615fd18bc5af..0000000000000 --- a/mode/type/standalone/core/src/main/java/org/apache/shardingsphere/mode/manager/standalone/NewStandaloneContextManagerBuilder.java +++ /dev/null @@ -1,83 +0,0 @@ -/* - * 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.mode.manager.standalone; - -import org.apache.shardingsphere.infra.config.mode.PersistRepositoryConfiguration; -import org.apache.shardingsphere.infra.instance.ComputeNodeInstance; -import org.apache.shardingsphere.infra.instance.InstanceContext; -import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader; -import org.apache.shardingsphere.infra.util.eventbus.EventBusContext; -import org.apache.shardingsphere.metadata.persist.NewMetaDataPersistService; -import org.apache.shardingsphere.mode.lock.GlobalLockContext; -import org.apache.shardingsphere.mode.manager.ContextManager; -import org.apache.shardingsphere.mode.manager.ContextManagerBuilder; -import org.apache.shardingsphere.mode.manager.ContextManagerBuilderParameter; -import org.apache.shardingsphere.mode.manager.standalone.subscriber.StandaloneProcessSubscriber; -import org.apache.shardingsphere.mode.manager.standalone.workerid.generator.StandaloneWorkerIdGenerator; -import org.apache.shardingsphere.mode.metadata.MetaDataContexts; -import org.apache.shardingsphere.mode.metadata.NewMetaDataContextsFactory; -import org.apache.shardingsphere.mode.repository.standalone.StandalonePersistRepository; -import org.apache.shardingsphere.mode.subsciber.RuleItemChangedSubscriber; - -import java.sql.SQLException; -import java.util.Properties; - -/** - * TODO Rename StandaloneContextManagerBuilder when metadata structure adjustment completed. #25485 - * New Standalone context manager builder. - */ -public final class NewStandaloneContextManagerBuilder implements ContextManagerBuilder { - - @Override - public ContextManager build(final ContextManagerBuilderParameter param) throws SQLException { - PersistRepositoryConfiguration repositoryConfig = param.getModeConfiguration().getRepository(); - StandalonePersistRepository repository = TypedSPILoader.getService( - StandalonePersistRepository.class, null == repositoryConfig ? null : repositoryConfig.getType(), null == repositoryConfig ? new Properties() : repositoryConfig.getProps()); - NewMetaDataPersistService persistService = new NewMetaDataPersistService(repository); - InstanceContext instanceContext = buildInstanceContext(param); - new StandaloneProcessSubscriber(instanceContext.getEventBusContext()); - MetaDataContexts metaDataContexts = NewMetaDataContextsFactory.create(persistService, param, instanceContext); - ContextManager result = new ContextManager(metaDataContexts, instanceContext); - registerSubscriber(result); - setContextManagerAware(result); - return result; - } - - private InstanceContext buildInstanceContext(final ContextManagerBuilderParameter param) { - return new InstanceContext(new ComputeNodeInstance(param.getInstanceMetaData()), - new StandaloneWorkerIdGenerator(), param.getModeConfiguration(), new NewStandaloneModeContextManager(), new GlobalLockContext(null), new EventBusContext()); - } - - private void registerSubscriber(final ContextManager contextManager) { - contextManager.getInstanceContext().getEventBusContext().register(new RuleItemChangedSubscriber(contextManager)); - } - - private void setContextManagerAware(final ContextManager contextManager) { - ((NewStandaloneModeContextManager) contextManager.getInstanceContext().getModeContextManager()).setContextManagerAware(contextManager); - } - - @Override - public String getType() { - return "Standalone"; - } - - @Override - public boolean isDefault() { - return true; - } -} diff --git a/mode/type/standalone/core/src/main/java/org/apache/shardingsphere/mode/manager/standalone/NewStandaloneModeContextManager.java b/mode/type/standalone/core/src/main/java/org/apache/shardingsphere/mode/manager/standalone/NewStandaloneModeContextManager.java deleted file mode 100644 index e711ad0fc2c4c..0000000000000 --- a/mode/type/standalone/core/src/main/java/org/apache/shardingsphere/mode/manager/standalone/NewStandaloneModeContextManager.java +++ /dev/null @@ -1,333 +0,0 @@ -/* - * 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.mode.manager.standalone; - -import com.google.common.base.Strings; -import org.apache.shardingsphere.infra.config.rule.RuleConfiguration; -import org.apache.shardingsphere.infra.connection.refresher.util.TableRefreshUtils; -import org.apache.shardingsphere.infra.datasource.pool.props.domain.DataSourcePoolProperties; -import org.apache.shardingsphere.infra.instance.mode.ModeContextManager; -import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; -import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema; -import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereTable; -import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereView; -import org.apache.shardingsphere.infra.metadata.database.schema.pojo.AlterSchemaMetaDataPOJO; -import org.apache.shardingsphere.infra.metadata.database.schema.pojo.AlterSchemaPOJO; -import org.apache.shardingsphere.infra.metadata.version.MetaDataVersion; -import org.apache.shardingsphere.infra.rule.attribute.datanode.MutableDataNodeRuleAttribute; -import org.apache.shardingsphere.infra.rule.attribute.metadata.MetaDataHeldRuleAttribute; -import org.apache.shardingsphere.infra.rule.scope.GlobalRule; -import org.apache.shardingsphere.infra.spi.type.ordered.cache.OrderedServicesCache; -import org.apache.shardingsphere.metadata.persist.service.config.global.GlobalPersistService; -import org.apache.shardingsphere.metadata.persist.service.database.DatabaseMetaDataBasedPersistService; -import org.apache.shardingsphere.mode.event.DataChangedEvent; -import org.apache.shardingsphere.mode.event.DataChangedEvent.Type; -import org.apache.shardingsphere.mode.manager.ContextManager; -import org.apache.shardingsphere.mode.manager.ContextManagerAware; -import org.apache.shardingsphere.mode.manager.switcher.NewResourceSwitchManager; -import org.apache.shardingsphere.mode.manager.switcher.SwitchingResource; -import org.apache.shardingsphere.mode.metadata.MetaDataContexts; -import org.apache.shardingsphere.mode.metadata.builder.RuleConfigurationEventBuilder; - -import java.sql.SQLException; -import java.util.Collection; -import java.util.Collections; -import java.util.LinkedHashSet; -import java.util.Map; -import java.util.Map.Entry; -import java.util.Optional; -import java.util.Properties; -import java.util.stream.Collectors; - -/** - * TODO Rename StandaloneModeContextManager when metadata structure adjustment completed. #25485 - * New Standalone mode context manager. - */ -public final class NewStandaloneModeContextManager implements ModeContextManager, ContextManagerAware { - - private final RuleConfigurationEventBuilder ruleConfigurationEventBuilder = new RuleConfigurationEventBuilder(); - - private ContextManager contextManager; - - @Override - public void createDatabase(final String databaseName) { - contextManager.getResourceMetaDataContextManager().addDatabase(databaseName); - contextManager.getMetaDataContexts().getPersistService().getDatabaseMetaDataService().addDatabase(databaseName); - clearServiceCache(); - } - - @Override - public void dropDatabase(final String databaseName) { - contextManager.getResourceMetaDataContextManager().dropDatabase(databaseName); - contextManager.getMetaDataContexts().getPersistService().getDatabaseMetaDataService().dropDatabase(databaseName); - clearServiceCache(); - } - - @Override - public void createSchema(final String databaseName, final String schemaName) { - ShardingSphereSchema schema = new ShardingSphereSchema(); - ShardingSphereDatabase database = contextManager.getMetaDataContexts().getMetaData().getDatabase(databaseName); - database.addSchema(schemaName, schema); - refreshMetaDataHeldRule(database); - contextManager.getMetaDataContexts().getPersistService().getDatabaseMetaDataService().persist(databaseName, schemaName, schema); - } - - @Override - public void alterSchema(final AlterSchemaPOJO alterSchemaPOJO) { - ShardingSphereDatabase database = contextManager.getMetaDataContexts().getMetaData().getDatabase(alterSchemaPOJO.getDatabaseName()); - putSchemaMetaData(database, alterSchemaPOJO.getSchemaName(), alterSchemaPOJO.getRenameSchemaName(), alterSchemaPOJO.getLogicDataSourceName()); - removeSchemaMetaData(database, alterSchemaPOJO.getSchemaName()); - refreshMetaDataHeldRule(database); - DatabaseMetaDataBasedPersistService databaseMetaDataService = contextManager.getMetaDataContexts().getPersistService().getDatabaseMetaDataService(); - databaseMetaDataService.persist(alterSchemaPOJO.getDatabaseName(), alterSchemaPOJO.getRenameSchemaName(), database.getSchema(alterSchemaPOJO.getRenameSchemaName())); - databaseMetaDataService.getViewMetaDataPersistService().persist(alterSchemaPOJO.getDatabaseName(), alterSchemaPOJO.getRenameSchemaName(), - database.getSchema(alterSchemaPOJO.getRenameSchemaName()).getViews()); - databaseMetaDataService.dropSchema(alterSchemaPOJO.getDatabaseName(), alterSchemaPOJO.getSchemaName()); - } - - private void putSchemaMetaData(final ShardingSphereDatabase database, final String schemaName, final String renameSchemaName, final String logicDataSourceName) { - ShardingSphereSchema schema = database.getSchema(schemaName); - database.addSchema(renameSchemaName, schema); - addDataNode(database, logicDataSourceName, schemaName, schema.getAllTableNames()); - } - - private void addDataNode(final ShardingSphereDatabase database, final String logicDataSourceName, final String schemaName, final Collection tobeAddedTableNames) { - tobeAddedTableNames.forEach(each -> { - if (!Strings.isNullOrEmpty(logicDataSourceName) && TableRefreshUtils.isSingleTable(each, database)) { - database.getRuleMetaData().getAttributes(MutableDataNodeRuleAttribute.class).forEach(rule -> rule.put(logicDataSourceName, schemaName, each)); - } - }); - } - - private void addDataNode(final ShardingSphereDatabase database, final String logicDataSourceName, final String schemaName, final Map toBeAddedTables, - final Map toBeAddedViews) { - addTablesToDataNode(database, schemaName, logicDataSourceName, toBeAddedTables); - addViewsToDataNode(database, schemaName, logicDataSourceName, toBeAddedTables, toBeAddedViews); - } - - private void addTablesToDataNode(final ShardingSphereDatabase database, final String schemaName, final String logicDataSourceName, final Map toBeAddedTables) { - for (Entry entry : toBeAddedTables.entrySet()) { - if (!Strings.isNullOrEmpty(logicDataSourceName) && TableRefreshUtils.isSingleTable(entry.getKey(), database)) { - database.getRuleMetaData().getAttributes(MutableDataNodeRuleAttribute.class).forEach(rule -> rule.put(logicDataSourceName, schemaName, entry.getKey())); - } - database.getSchema(schemaName).putTable(entry.getKey(), entry.getValue()); - } - } - - private void addViewsToDataNode(final ShardingSphereDatabase database, final String schemaName, final String logicDataSourceName, - final Map toBeAddedTables, final Map toBeAddedViews) { - for (Entry entry : toBeAddedViews.entrySet()) { - if (!Strings.isNullOrEmpty(logicDataSourceName) && TableRefreshUtils.isSingleTable(entry.getKey(), database)) { - database.getRuleMetaData().getAttributes(MutableDataNodeRuleAttribute.class).forEach(each -> each.put(logicDataSourceName, schemaName, entry.getKey())); - } - database.getSchema(schemaName).putTable(entry.getKey(), toBeAddedTables.get(entry.getKey().toLowerCase())); - database.getSchema(schemaName).putView(entry.getKey(), entry.getValue()); - } - } - - private void removeSchemaMetaData(final ShardingSphereDatabase database, final String schemaName) { - ShardingSphereSchema schema = new ShardingSphereSchema(database.getSchema(schemaName).getTables(), database.getSchema(schemaName).getViews()); - database.dropSchema(schemaName); - removeDataNode(database.getRuleMetaData().getAttributes(MutableDataNodeRuleAttribute.class), Collections.singletonList(schemaName), schema.getAllTableNames()); - } - - private void removeDataNode(final Collection ruleAttributes, final Collection schemaNames, final Collection tobeRemovedTables) { - tobeRemovedTables.forEach(each -> ruleAttributes.forEach(rule -> rule.remove(schemaNames, each))); - } - - private void removeDataNode(final ShardingSphereDatabase database, final String schemaName, final Collection tobeRemovedTables, final Collection tobeRemovedViews) { - removeTablesToDataNode(database, schemaName, tobeRemovedTables); - removeViewsToDataNode(database, schemaName, tobeRemovedTables, tobeRemovedViews); - } - - private void removeDataNode(final Collection ruleAttributes, final String schemaName, final Collection tobeRemovedTables) { - tobeRemovedTables.forEach(each -> ruleAttributes.forEach(rule -> rule.remove(schemaName, each))); - } - - private void removeTablesToDataNode(final ShardingSphereDatabase database, final String schemaName, final Collection toBeDroppedTables) { - removeDataNode(database.getRuleMetaData().getAttributes(MutableDataNodeRuleAttribute.class), schemaName, toBeDroppedTables); - toBeDroppedTables.forEach(each -> database.getSchema(schemaName).removeTable(each)); - } - - private void removeViewsToDataNode(final ShardingSphereDatabase database, final String schemaName, final Collection toBeDroppedTables, final Collection toBeDroppedViews) { - removeDataNode(database.getRuleMetaData().getAttributes(MutableDataNodeRuleAttribute.class), schemaName, toBeDroppedViews); - ShardingSphereSchema schema = database.getSchema(schemaName); - toBeDroppedTables.forEach(schema::removeTable); - toBeDroppedViews.forEach(schema::removeView); - } - - @Override - public void dropSchema(final String databaseName, final Collection schemaNames) { - Collection tobeRemovedTables = new LinkedHashSet<>(); - Collection tobeRemovedSchemas = new LinkedHashSet<>(); - ShardingSphereDatabase database = contextManager.getMetaDataContexts().getMetaData().getDatabase(databaseName); - for (String each : schemaNames) { - ShardingSphereSchema schema = new ShardingSphereSchema(database.getSchema(each).getTables(), database.getSchema(each).getViews()); - database.dropSchema(each); - Optional.of(schema).ifPresent(optional -> tobeRemovedTables.addAll(optional.getAllTableNames())); - tobeRemovedSchemas.add(each.toLowerCase()); - } - removeDataNode(database.getRuleMetaData().getAttributes(MutableDataNodeRuleAttribute.class), tobeRemovedSchemas, tobeRemovedTables); - refreshMetaDataHeldRule(database); - } - - @Override - public void alterSchemaMetaData(final AlterSchemaMetaDataPOJO alterSchemaMetaDataPOJO) { - String databaseName = alterSchemaMetaDataPOJO.getDatabaseName(); - String schemaName = alterSchemaMetaDataPOJO.getSchemaName(); - ShardingSphereDatabase database = contextManager.getMetaDataContexts().getMetaData().getDatabase(databaseName); - Map tables = alterSchemaMetaDataPOJO.getAlteredTables().stream().collect(Collectors.toMap(ShardingSphereTable::getName, table -> table)); - Map views = alterSchemaMetaDataPOJO.getAlteredViews().stream().collect(Collectors.toMap(ShardingSphereView::getName, view -> view)); - addDataNode(database, alterSchemaMetaDataPOJO.getLogicDataSourceName(), schemaName, tables, views); - removeDataNode(database, schemaName, alterSchemaMetaDataPOJO.getDroppedTables(), alterSchemaMetaDataPOJO.getDroppedViews()); - refreshMetaDataHeldRule(database); - DatabaseMetaDataBasedPersistService databaseMetaDataService = contextManager.getMetaDataContexts().getPersistService().getDatabaseMetaDataService(); - databaseMetaDataService.getTableMetaDataPersistService().persist(databaseName, schemaName, tables); - databaseMetaDataService.getViewMetaDataPersistService().persist(databaseName, schemaName, views); - alterSchemaMetaDataPOJO.getDroppedTables().forEach(each -> databaseMetaDataService.getTableMetaDataPersistService().delete(databaseName, schemaName, each)); - alterSchemaMetaDataPOJO.getDroppedViews().forEach(each -> databaseMetaDataService.getViewMetaDataPersistService().delete(databaseName, schemaName, each)); - } - - private void refreshMetaDataHeldRule(final ShardingSphereDatabase database) { - contextManager.getMetaDataContexts().getMetaData().getGlobalRuleMetaData().getAttributes(MetaDataHeldRuleAttribute.class).forEach(each -> each.alterDatabase(database)); - } - - @Override - public void registerStorageUnits(final String databaseName, final Map toBeRegisteredProps) throws SQLException { - SwitchingResource switchingResource = - new NewResourceSwitchManager().registerStorageUnit(contextManager.getMetaDataContexts().getMetaData().getDatabase(databaseName).getResourceMetaData(), toBeRegisteredProps); - 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())); - contextManager.getMetaDataContexts().getMetaData().getDatabase(databaseName).getSchemas() - .forEach((schemaName, schema) -> contextManager.getMetaDataContexts().getPersistService().getDatabaseMetaDataService() - .persist(contextManager.getMetaDataContexts().getMetaData().getDatabase(databaseName).getName(), schemaName, schema)); - contextManager.getMetaDataContexts().getPersistService().getDataSourceUnitService().append( - contextManager.getMetaDataContexts().getMetaData().getDatabase(databaseName).getName(), toBeRegisteredProps); - clearServiceCache(); - } - - @Override - public void alterStorageUnits(final String databaseName, final Map toBeUpdatedProps) throws SQLException { - SwitchingResource switchingResource = - new NewResourceSwitchManager().alterStorageUnit(contextManager.getMetaDataContexts().getMetaData().getDatabase(databaseName).getResourceMetaData(), toBeUpdatedProps); - 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())); - contextManager.getMetaDataContexts().getPersistService().getDataSourceUnitService().append( - contextManager.getMetaDataContexts().getMetaData().getDatabase(databaseName).getName(), toBeUpdatedProps); - switchingResource.closeStaleDataSources(); - clearServiceCache(); - } - - @Override - public void unregisterStorageUnits(final String databaseName, final Collection toBeDroppedStorageUnitNames) throws SQLException { - SwitchingResource switchingResource = - new NewResourceSwitchManager().unregisterStorageUnit(contextManager.getMetaDataContexts().getMetaData().getDatabase(databaseName).getResourceMetaData(), toBeDroppedStorageUnitNames); - contextManager.getMetaDataContexts().getMetaData().getDatabases() - .putAll(contextManager.getConfigurationContextManager().renewDatabase(contextManager.getMetaDataContexts().getMetaData().getDatabase(databaseName), switchingResource)); - MetaDataContexts reloadMetaDataContexts = contextManager.getConfigurationContextManager().createMetaDataContexts(databaseName, false, switchingResource, null); - contextManager.getConfigurationContextManager().alterSchemaMetaData(databaseName, reloadMetaDataContexts.getMetaData().getDatabase(databaseName), - contextManager.getMetaDataContexts().getMetaData().getDatabase(databaseName)); - contextManager.deletedSchemaNames(databaseName, reloadMetaDataContexts.getMetaData().getDatabase(databaseName), contextManager.getMetaDataContexts().getMetaData().getDatabase(databaseName)); - contextManager.renewMetaDataContexts(reloadMetaDataContexts); - switchingResource.closeStaleDataSources(); - clearServiceCache(); - } - - @Override - public void alterRuleConfiguration(final String databaseName, final Collection ruleConfigs) { - contextManager.getConfigurationContextManager().alterRuleConfiguration(databaseName, ruleConfigs); - contextManager.getMetaDataContexts().getPersistService() - .getDatabaseRulePersistService().persist(contextManager.getMetaDataContexts().getMetaData().getDatabase(databaseName).getName(), ruleConfigs); - clearServiceCache(); - } - - @Override - public Collection alterRuleConfiguration(final String databaseName, final RuleConfiguration toBeAlteredRuleConfig) { - if (null != toBeAlteredRuleConfig) { - sendDatabaseRuleChangedEvent(databaseName, - contextManager.getMetaDataContexts().getPersistService().getDatabaseRulePersistService() - .persistConfig(contextManager.getMetaDataContexts().getMetaData().getDatabase(databaseName).getName(), Collections.singletonList(toBeAlteredRuleConfig))); - clearServiceCache(); - } - return Collections.emptyList(); - } - - private void sendDatabaseRuleChangedEvent(final String databaseName, final Collection metaDataVersions) { - for (MetaDataVersion each : metaDataVersions) { - sendDatabaseRuleChangedEvent(databaseName, each); - } - } - - private void sendDatabaseRuleChangedEvent(final String databaseName, final MetaDataVersion metaDataVersion) { - for (String each : metaDataVersion.getActiveVersionKeys()) { - ruleConfigurationEventBuilder.build(databaseName, new DataChangedEvent(each, metaDataVersion.getCurrentActiveVersion(), Type.UPDATED)) - .ifPresent(optional -> contextManager.getInstanceContext().getEventBusContext().post(optional)); - } - } - - @Override - public void removeRuleConfigurationItem(final String databaseName, final RuleConfiguration toBeRemovedRuleConfig) { - if (null != toBeRemovedRuleConfig) { - sendDatabaseRuleChangedEvent(databaseName, - contextManager.getMetaDataContexts().getPersistService().getDatabaseRulePersistService().deleteConfig(databaseName, Collections.singleton(toBeRemovedRuleConfig))); - clearServiceCache(); - } - } - - @Override - public void removeRuleConfiguration(final String databaseName, final String ruleName) { - contextManager.getMetaDataContexts().getPersistService().getDatabaseRulePersistService().delete(databaseName, ruleName); - clearServiceCache(); - } - - @Override - public void alterGlobalRuleConfiguration(final Collection globalRuleConfigs) { - contextManager.getConfigurationContextManager().alterGlobalRuleConfiguration(globalRuleConfigs); - contextManager.getMetaDataContexts().getPersistService().getGlobalRuleService().persist(contextManager.getMetaDataContexts().getMetaData().getGlobalRuleMetaData().getConfigurations()); - clearServiceCache(); - } - - @Override - public void alterGlobalRuleConfiguration(final RuleConfiguration toBeAlteredRuleConfig) { - contextManager.getConfigurationContextManager().alterGlobalRuleConfiguration(toBeAlteredRuleConfig); - GlobalPersistService> globalRuleService = contextManager.getMetaDataContexts().getPersistService().getGlobalRuleService(); - contextManager.getMetaDataContexts().getPersistService().getMetaDataVersionPersistService().switchActiveVersion(globalRuleService.persistConfig(Collections.singleton(toBeAlteredRuleConfig))); - clearServiceCache(); - } - - @Override - public void alterProperties(final Properties props) { - contextManager.getConfigurationContextManager().alterProperties(props); - if (null != contextManager.getMetaDataContexts().getPersistService().getPropsService()) { - Collection versions = contextManager.getMetaDataContexts().getPersistService().getPropsService().persistConfig(props); - contextManager.getMetaDataContexts().getPersistService().getMetaDataVersionPersistService().switchActiveVersion(versions); - } - clearServiceCache(); - } - - private void clearServiceCache() { - OrderedServicesCache.clearCache(); - } - - @Override - public void setContextManagerAware(final ContextManager contextManager) { - this.contextManager = 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 10d41830418ed..a47e92bc809ef 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 @@ -20,8 +20,8 @@ import org.apache.shardingsphere.infra.config.mode.PersistRepositoryConfiguration; import org.apache.shardingsphere.infra.instance.ComputeNodeInstance; import org.apache.shardingsphere.infra.instance.InstanceContext; -import org.apache.shardingsphere.infra.util.eventbus.EventBusContext; import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader; +import org.apache.shardingsphere.infra.util.eventbus.EventBusContext; import org.apache.shardingsphere.metadata.persist.MetaDataPersistService; import org.apache.shardingsphere.mode.lock.GlobalLockContext; import org.apache.shardingsphere.mode.manager.ContextManager; @@ -32,6 +32,7 @@ import org.apache.shardingsphere.mode.metadata.MetaDataContexts; import org.apache.shardingsphere.mode.metadata.MetaDataContextsFactory; import org.apache.shardingsphere.mode.repository.standalone.StandalonePersistRepository; +import org.apache.shardingsphere.mode.subsciber.RuleItemChangedSubscriber; import java.sql.SQLException; import java.util.Properties; @@ -51,6 +52,7 @@ public ContextManager build(final ContextManagerBuilderParameter param) throws S new StandaloneProcessSubscriber(instanceContext.getEventBusContext()); MetaDataContexts metaDataContexts = MetaDataContextsFactory.create(persistService, param, instanceContext); ContextManager result = new ContextManager(metaDataContexts, instanceContext); + registerSubscriber(result); setContextManagerAware(result); return result; } @@ -60,12 +62,21 @@ private InstanceContext buildInstanceContext(final ContextManagerBuilderParamete new StandaloneWorkerIdGenerator(), param.getModeConfiguration(), new StandaloneModeContextManager(), new GlobalLockContext(null), new EventBusContext()); } + private void registerSubscriber(final ContextManager contextManager) { + contextManager.getInstanceContext().getEventBusContext().register(new RuleItemChangedSubscriber(contextManager)); + } + private void setContextManagerAware(final ContextManager contextManager) { ((StandaloneModeContextManager) contextManager.getInstanceContext().getModeContextManager()).setContextManagerAware(contextManager); } @Override public String getType() { - return "Compatible_Standalone"; + return "Standalone"; + } + + @Override + public boolean isDefault() { + return true; } } diff --git a/mode/type/standalone/core/src/main/java/org/apache/shardingsphere/mode/manager/standalone/StandaloneModeContextManager.java b/mode/type/standalone/core/src/main/java/org/apache/shardingsphere/mode/manager/standalone/StandaloneModeContextManager.java index 646d770fed73f..e1b0ad6cce644 100644 --- a/mode/type/standalone/core/src/main/java/org/apache/shardingsphere/mode/manager/standalone/StandaloneModeContextManager.java +++ b/mode/type/standalone/core/src/main/java/org/apache/shardingsphere/mode/manager/standalone/StandaloneModeContextManager.java @@ -28,16 +28,21 @@ import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereView; import org.apache.shardingsphere.infra.metadata.database.schema.pojo.AlterSchemaMetaDataPOJO; import org.apache.shardingsphere.infra.metadata.database.schema.pojo.AlterSchemaPOJO; +import org.apache.shardingsphere.infra.metadata.version.MetaDataVersion; import org.apache.shardingsphere.infra.rule.attribute.datanode.MutableDataNodeRuleAttribute; import org.apache.shardingsphere.infra.rule.attribute.metadata.MetaDataHeldRuleAttribute; import org.apache.shardingsphere.infra.rule.scope.GlobalRule; import org.apache.shardingsphere.infra.spi.type.ordered.cache.OrderedServicesCache; +import org.apache.shardingsphere.metadata.persist.service.config.global.GlobalPersistService; import org.apache.shardingsphere.metadata.persist.service.database.DatabaseMetaDataBasedPersistService; +import org.apache.shardingsphere.mode.event.DataChangedEvent; +import org.apache.shardingsphere.mode.event.DataChangedEvent.Type; import org.apache.shardingsphere.mode.manager.ContextManager; import org.apache.shardingsphere.mode.manager.ContextManagerAware; 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.builder.RuleConfigurationEventBuilder; import java.sql.SQLException; import java.util.Collection; @@ -54,6 +59,8 @@ */ public final class StandaloneModeContextManager implements ModeContextManager, ContextManagerAware { + private final RuleConfigurationEventBuilder ruleConfigurationEventBuilder = new RuleConfigurationEventBuilder(); + private ContextManager contextManager; @Override @@ -125,7 +132,7 @@ private void addViewsToDataNode(final ShardingSphereDatabase database, final Str final Map toBeAddedTables, final Map toBeAddedViews) { for (Entry entry : toBeAddedViews.entrySet()) { if (!Strings.isNullOrEmpty(logicDataSourceName) && TableRefreshUtils.isSingleTable(entry.getKey(), database)) { - database.getRuleMetaData().getAttributes(MutableDataNodeRuleAttribute.class).forEach(rule -> rule.put(logicDataSourceName, schemaName, entry.getKey())); + database.getRuleMetaData().getAttributes(MutableDataNodeRuleAttribute.class).forEach(each -> each.put(logicDataSourceName, schemaName, entry.getKey())); } database.getSchema(schemaName).putTable(entry.getKey(), toBeAddedTables.get(entry.getKey().toLowerCase())); database.getSchema(schemaName).putView(entry.getKey(), entry.getValue()); @@ -135,7 +142,7 @@ private void addViewsToDataNode(final ShardingSphereDatabase database, final Str private void removeSchemaMetaData(final ShardingSphereDatabase database, final String schemaName) { ShardingSphereSchema schema = new ShardingSphereSchema(database.getSchema(schemaName).getTables(), database.getSchema(schemaName).getViews()); database.dropSchema(schemaName); - removeDataNode(database.getRuleMetaData().getAttributes(MutableDataNodeRuleAttribute.class), Collections.singleton(schemaName), schema.getAllTableNames()); + removeDataNode(database.getRuleMetaData().getAttributes(MutableDataNodeRuleAttribute.class), Collections.singletonList(schemaName), schema.getAllTableNames()); } private void removeDataNode(final Collection ruleAttributes, final Collection schemaNames, final Collection tobeRemovedTables) { @@ -202,7 +209,7 @@ private void refreshMetaDataHeldRule(final ShardingSphereDatabase database) { @Override public void registerStorageUnits(final String databaseName, final Map toBeRegisteredProps) throws SQLException { SwitchingResource switchingResource = - new ResourceSwitchManager().create(contextManager.getMetaDataContexts().getMetaData().getDatabase(databaseName).getResourceMetaData(), toBeRegisteredProps); + 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().getGlobalRuleMetaData().getRules() .forEach(each -> ((GlobalRule) each).refresh(contextManager.getMetaDataContexts().getMetaData().getDatabases())); @@ -217,7 +224,7 @@ public void registerStorageUnits(final String databaseName, final Map toBeUpdatedProps) throws SQLException { SwitchingResource switchingResource = - new ResourceSwitchManager().create(contextManager.getMetaDataContexts().getMetaData().getDatabase(databaseName).getResourceMetaData(), toBeUpdatedProps); + 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().getGlobalRuleMetaData().getRules() .forEach(each -> ((GlobalRule) each).refresh(contextManager.getMetaDataContexts().getMetaData().getDatabases())); @@ -229,11 +236,8 @@ public void alterStorageUnits(final String databaseName, final Map toBeDroppedStorageUnitNames) throws SQLException { - Map propsMap = contextManager.getMetaDataContexts().getPersistService().getDataSourceUnitService() - .load(contextManager.getMetaDataContexts().getMetaData().getDatabase(databaseName).getName()); - Map toBeDeletedPropsMap = getToBeDeletedDataSourcePoolPropertiesMap(propsMap, toBeDroppedStorageUnitNames); SwitchingResource switchingResource = - new ResourceSwitchManager().createByDropResource(contextManager.getMetaDataContexts().getMetaData().getDatabase(databaseName).getResourceMetaData(), toBeDeletedPropsMap); + new ResourceSwitchManager().unregisterStorageUnit(contextManager.getMetaDataContexts().getMetaData().getDatabase(databaseName).getResourceMetaData(), toBeDroppedStorageUnitNames); contextManager.getMetaDataContexts().getMetaData().getDatabases() .putAll(contextManager.getConfigurationContextManager().renewDatabase(contextManager.getMetaDataContexts().getMetaData().getDatabase(databaseName), switchingResource)); MetaDataContexts reloadMetaDataContexts = contextManager.getConfigurationContextManager().createMetaDataContexts(databaseName, false, switchingResource, null); @@ -241,21 +245,10 @@ public void unregisterStorageUnits(final String databaseName, final Collection toBeReversedPropsMap = getToBeReversedDataSourcePoolPropertiesMap(propsMap, toBeDroppedStorageUnitNames); - contextManager.getMetaDataContexts().getPersistService().getDataSourceUnitService().persist(contextManager.getMetaDataContexts().getMetaData().getDatabase(databaseName).getName(), - toBeReversedPropsMap); switchingResource.closeStaleDataSources(); clearServiceCache(); } - private Map getToBeDeletedDataSourcePoolPropertiesMap(final Map propsMap, final Collection toBeDroppedResourceNames) { - return propsMap.entrySet().stream().filter(entry -> toBeDroppedResourceNames.contains(entry.getKey())).collect(Collectors.toMap(Entry::getKey, Entry::getValue)); - } - - private Map getToBeReversedDataSourcePoolPropertiesMap(final Map propsMap, final Collection toBeDroppedResourceNames) { - return propsMap.entrySet().stream().filter(entry -> !toBeDroppedResourceNames.contains(entry.getKey())).collect(Collectors.toMap(Entry::getKey, Entry::getValue)); - } - @Override public void alterRuleConfiguration(final String databaseName, final Collection ruleConfigs) { contextManager.getConfigurationContextManager().alterRuleConfiguration(databaseName, ruleConfigs); @@ -264,6 +257,45 @@ public void alterRuleConfiguration(final String databaseName, final Collection alterRuleConfiguration(final String databaseName, final RuleConfiguration toBeAlteredRuleConfig) { + if (null != toBeAlteredRuleConfig) { + sendDatabaseRuleChangedEvent(databaseName, + contextManager.getMetaDataContexts().getPersistService().getDatabaseRulePersistService() + .persistConfig(contextManager.getMetaDataContexts().getMetaData().getDatabase(databaseName).getName(), Collections.singletonList(toBeAlteredRuleConfig))); + clearServiceCache(); + } + return Collections.emptyList(); + } + + private void sendDatabaseRuleChangedEvent(final String databaseName, final Collection metaDataVersions) { + for (MetaDataVersion each : metaDataVersions) { + sendDatabaseRuleChangedEvent(databaseName, each); + } + } + + private void sendDatabaseRuleChangedEvent(final String databaseName, final MetaDataVersion metaDataVersion) { + for (String each : metaDataVersion.getActiveVersionKeys()) { + ruleConfigurationEventBuilder.build(databaseName, new DataChangedEvent(each, metaDataVersion.getCurrentActiveVersion(), Type.UPDATED)) + .ifPresent(optional -> contextManager.getInstanceContext().getEventBusContext().post(optional)); + } + } + + @Override + public void removeRuleConfigurationItem(final String databaseName, final RuleConfiguration toBeRemovedRuleConfig) { + if (null != toBeRemovedRuleConfig) { + sendDatabaseRuleChangedEvent(databaseName, + contextManager.getMetaDataContexts().getPersistService().getDatabaseRulePersistService().deleteConfig(databaseName, Collections.singleton(toBeRemovedRuleConfig))); + clearServiceCache(); + } + } + + @Override + public void removeRuleConfiguration(final String databaseName, final String ruleName) { + contextManager.getMetaDataContexts().getPersistService().getDatabaseRulePersistService().delete(databaseName, ruleName); + clearServiceCache(); + } + @Override public void alterGlobalRuleConfiguration(final Collection globalRuleConfigs) { contextManager.getConfigurationContextManager().alterGlobalRuleConfiguration(globalRuleConfigs); @@ -271,11 +303,20 @@ public void alterGlobalRuleConfiguration(final Collection glo clearServiceCache(); } + @Override + public void alterGlobalRuleConfiguration(final RuleConfiguration toBeAlteredRuleConfig) { + contextManager.getConfigurationContextManager().alterGlobalRuleConfiguration(toBeAlteredRuleConfig); + GlobalPersistService> globalRuleService = contextManager.getMetaDataContexts().getPersistService().getGlobalRuleService(); + contextManager.getMetaDataContexts().getPersistService().getMetaDataVersionPersistService().switchActiveVersion(globalRuleService.persistConfig(Collections.singleton(toBeAlteredRuleConfig))); + clearServiceCache(); + } + @Override public void alterProperties(final Properties props) { contextManager.getConfigurationContextManager().alterProperties(props); if (null != contextManager.getMetaDataContexts().getPersistService().getPropsService()) { - contextManager.getMetaDataContexts().getPersistService().getPropsService().persist(props); + Collection versions = contextManager.getMetaDataContexts().getPersistService().getPropsService().persistConfig(props); + contextManager.getMetaDataContexts().getPersistService().getMetaDataVersionPersistService().switchActiveVersion(versions); } clearServiceCache(); } diff --git a/mode/type/standalone/core/src/main/java/org/apache/shardingsphere/mode/manager/standalone/yaml/NewStandaloneYamlPersistRepositoryConfigurationSwapper.java b/mode/type/standalone/core/src/main/java/org/apache/shardingsphere/mode/manager/standalone/yaml/NewStandaloneYamlPersistRepositoryConfigurationSwapper.java deleted file mode 100644 index 7d0fd7ce34db7..0000000000000 --- a/mode/type/standalone/core/src/main/java/org/apache/shardingsphere/mode/manager/standalone/yaml/NewStandaloneYamlPersistRepositoryConfigurationSwapper.java +++ /dev/null @@ -1,47 +0,0 @@ -/* - * 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.mode.manager.standalone.yaml; - -import org.apache.shardingsphere.infra.yaml.config.pojo.mode.YamlPersistRepositoryConfiguration; -import org.apache.shardingsphere.infra.yaml.config.swapper.mode.YamlPersistRepositoryConfigurationSwapper; -import org.apache.shardingsphere.mode.repository.standalone.StandalonePersistRepositoryConfiguration; - -/** - * TODO Rename StandaloneYamlPersistRepositoryConfigurationSwapper when metadata structure adjustment completed. #25485 - * New Standalone YAML persist repository configuration swapper. - */ -public final class NewStandaloneYamlPersistRepositoryConfigurationSwapper implements YamlPersistRepositoryConfigurationSwapper { - - @Override - public YamlPersistRepositoryConfiguration swapToYamlConfiguration(final StandalonePersistRepositoryConfiguration data) { - YamlPersistRepositoryConfiguration result = new YamlPersistRepositoryConfiguration(); - result.setType(data.getType()); - result.setProps(data.getProps()); - return result; - } - - @Override - public StandalonePersistRepositoryConfiguration swapToObject(final YamlPersistRepositoryConfiguration yamlConfig) { - return new StandalonePersistRepositoryConfiguration(yamlConfig.getType(), yamlConfig.getProps()); - } - - @Override - public String getType() { - return "Standalone"; - } -} diff --git a/mode/type/standalone/core/src/main/java/org/apache/shardingsphere/mode/manager/standalone/yaml/StandaloneYamlPersistRepositoryConfigurationSwapper.java b/mode/type/standalone/core/src/main/java/org/apache/shardingsphere/mode/manager/standalone/yaml/StandaloneYamlPersistRepositoryConfigurationSwapper.java index 847cdf5bdeeaf..f20998373d1cf 100644 --- a/mode/type/standalone/core/src/main/java/org/apache/shardingsphere/mode/manager/standalone/yaml/StandaloneYamlPersistRepositoryConfigurationSwapper.java +++ b/mode/type/standalone/core/src/main/java/org/apache/shardingsphere/mode/manager/standalone/yaml/StandaloneYamlPersistRepositoryConfigurationSwapper.java @@ -17,9 +17,9 @@ package org.apache.shardingsphere.mode.manager.standalone.yaml; -import org.apache.shardingsphere.mode.repository.standalone.StandalonePersistRepositoryConfiguration; import org.apache.shardingsphere.infra.yaml.config.pojo.mode.YamlPersistRepositoryConfiguration; import org.apache.shardingsphere.infra.yaml.config.swapper.mode.YamlPersistRepositoryConfigurationSwapper; +import org.apache.shardingsphere.mode.repository.standalone.StandalonePersistRepositoryConfiguration; /** * Standalone YAML persist repository configuration swapper. @@ -41,6 +41,6 @@ public StandalonePersistRepositoryConfiguration swapToObject(final YamlPersistRe @Override public String getType() { - return "Compatible_Standalone"; + return "Standalone"; } } diff --git a/mode/type/standalone/core/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.yaml.config.swapper.mode.YamlPersistRepositoryConfigurationSwapper b/mode/type/standalone/core/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.yaml.config.swapper.mode.YamlPersistRepositoryConfigurationSwapper index 846ad4f18d6e7..a88aa27daa70d 100644 --- a/mode/type/standalone/core/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.yaml.config.swapper.mode.YamlPersistRepositoryConfigurationSwapper +++ b/mode/type/standalone/core/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.yaml.config.swapper.mode.YamlPersistRepositoryConfigurationSwapper @@ -16,4 +16,3 @@ # org.apache.shardingsphere.mode.manager.standalone.yaml.StandaloneYamlPersistRepositoryConfigurationSwapper -org.apache.shardingsphere.mode.manager.standalone.yaml.NewStandaloneYamlPersistRepositoryConfigurationSwapper diff --git a/mode/type/standalone/core/src/main/resources/META-INF/services/org.apache.shardingsphere.mode.manager.ContextManagerBuilder b/mode/type/standalone/core/src/main/resources/META-INF/services/org.apache.shardingsphere.mode.manager.ContextManagerBuilder index 71671fd356b60..ee1dd93b9fa03 100644 --- a/mode/type/standalone/core/src/main/resources/META-INF/services/org.apache.shardingsphere.mode.manager.ContextManagerBuilder +++ b/mode/type/standalone/core/src/main/resources/META-INF/services/org.apache.shardingsphere.mode.manager.ContextManagerBuilder @@ -16,4 +16,3 @@ # org.apache.shardingsphere.mode.manager.standalone.StandaloneContextManagerBuilder -org.apache.shardingsphere.mode.manager.standalone.NewStandaloneContextManagerBuilder diff --git a/mode/type/standalone/core/src/test/java/org/apache/shardingsphere/mode/manager/standalone/StandaloneContextManagerBuilderTest.java b/mode/type/standalone/core/src/test/java/org/apache/shardingsphere/mode/manager/standalone/StandaloneContextManagerBuilderTest.java deleted file mode 100644 index 403c22087254c..0000000000000 --- a/mode/type/standalone/core/src/test/java/org/apache/shardingsphere/mode/manager/standalone/StandaloneContextManagerBuilderTest.java +++ /dev/null @@ -1,66 +0,0 @@ -/* - * 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.mode.manager.standalone; - -import org.apache.shardingsphere.infra.config.database.DatabaseConfiguration; -import org.apache.shardingsphere.infra.config.database.impl.DataSourceProvidedDatabaseConfiguration; -import org.apache.shardingsphere.infra.config.mode.ModeConfiguration; -import org.apache.shardingsphere.infra.config.rule.RuleConfiguration; -import org.apache.shardingsphere.infra.instance.metadata.InstanceMetaData; -import org.apache.shardingsphere.infra.instance.metadata.proxy.ProxyInstanceMetaData; -import org.apache.shardingsphere.metadata.persist.node.DatabaseMetaDataNode; -import org.apache.shardingsphere.metadata.persist.node.GlobalNode; -import org.apache.shardingsphere.mode.manager.ContextManager; -import org.apache.shardingsphere.mode.manager.ContextManagerBuilderParameter; -import org.apache.shardingsphere.mode.repository.standalone.StandalonePersistRepositoryConfiguration; -import org.apache.shardingsphere.mode.spi.PersistRepository; -import org.apache.shardingsphere.test.fixture.jdbc.MockedDataSource; -import org.junit.jupiter.api.Test; - -import java.sql.SQLException; -import java.util.Collection; -import java.util.Collections; -import java.util.Map; -import java.util.Properties; -import java.util.UUID; - -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.mockito.Mockito.mock; - -class StandaloneContextManagerBuilderTest { - - @Test - void assertBuild() throws SQLException { - try (ContextManager actual = new StandaloneContextManagerBuilder().build(createContextManagerBuilderParameter())) { - assertNotNull(actual.getMetaDataContexts().getMetaData().getDatabase("foo_db")); - PersistRepository repository = actual.getMetaDataContexts().getPersistService().getRepository(); - assertNotNull(repository.getDirectly(GlobalNode.getGlobalRuleNode())); - assertNotNull(repository.getDirectly(DatabaseMetaDataNode.getMetaDataDataSourceUnitsPath("foo_db", "0"))); - assertNotNull(repository.getDirectly(DatabaseMetaDataNode.getRulePath("foo_db", "0"))); - } - } - - private ContextManagerBuilderParameter createContextManagerBuilderParameter() { - ModeConfiguration modeConfig = new ModeConfiguration("Standalone", new StandalonePersistRepositoryConfiguration("FIXTURE", new Properties())); - Map databaseConfigs = Collections.singletonMap( - "foo_db", new DataSourceProvidedDatabaseConfiguration(Collections.singletonMap("foo_ds", new MockedDataSource()), Collections.singleton(mock(RuleConfiguration.class)))); - Collection globalRuleConfigs = Collections.singleton(mock(RuleConfiguration.class)); - InstanceMetaData instanceMetaData = new ProxyInstanceMetaData(UUID.fromString("00000000-000-0000-0000-000000000001").toString(), 3307); - return new ContextManagerBuilderParameter(modeConfig, databaseConfigs, Collections.emptyMap(), globalRuleConfigs, new Properties(), Collections.emptyList(), instanceMetaData, false); - } -} 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 4ebbf5eb950ff..eac07feec0688 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 @@ -18,9 +18,9 @@ package org.apache.shardingsphere.proxy.backend.context; import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData; +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.metadata.persist.MetaDataPersistService; import org.apache.shardingsphere.test.mock.AutoMockExtension; import org.apache.shardingsphere.test.mock.StaticMockSettings; import org.junit.jupiter.api.Test; 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 b313a9b566d61..5265909ac26d2 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 @@ -24,9 +24,9 @@ import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData; import org.apache.shardingsphere.infra.metadata.user.Grantee; import org.apache.shardingsphere.infra.metadata.user.ShardingSphereUser; +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.metadata.persist.MetaDataPersistService; import org.apache.shardingsphere.proxy.backend.context.ProxyContext; import org.apache.shardingsphere.proxy.backend.session.ConnectionSession; import org.apache.shardingsphere.test.mock.AutoMockExtension; diff --git a/proxy/frontend/type/postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/authentication/PostgreSQLAuthenticationEngineTest.java b/proxy/frontend/type/postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/authentication/PostgreSQLAuthenticationEngineTest.java index 0ab3c94263cdc..49c2b6bc72cd5 100644 --- a/proxy/frontend/type/postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/authentication/PostgreSQLAuthenticationEngineTest.java +++ b/proxy/frontend/type/postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/authentication/PostgreSQLAuthenticationEngineTest.java @@ -42,9 +42,9 @@ import org.apache.shardingsphere.infra.metadata.database.resource.ResourceMetaData; import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData; import org.apache.shardingsphere.infra.metadata.user.ShardingSphereUser; +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.metadata.persist.MetaDataPersistService; import org.apache.shardingsphere.proxy.backend.context.ProxyContext; import org.apache.shardingsphere.authentication.result.AuthenticationResult; import org.apache.shardingsphere.proxy.frontend.postgresql.authentication.authenticator.impl.PostgreSQLMD5PasswordAuthenticator; diff --git a/proxy/frontend/type/postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/PostgreSQLCommandExecuteEngineTest.java b/proxy/frontend/type/postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/PostgreSQLCommandExecuteEngineTest.java index d1981c8bb182f..1d4deb29e77a8 100644 --- a/proxy/frontend/type/postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/PostgreSQLCommandExecuteEngineTest.java +++ b/proxy/frontend/type/postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/PostgreSQLCommandExecuteEngineTest.java @@ -24,9 +24,9 @@ import org.apache.shardingsphere.db.protocol.postgresql.packet.generic.PostgreSQLReadyForQueryPacket; import org.apache.shardingsphere.infra.instance.InstanceContext; import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData; +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.metadata.persist.MetaDataPersistService; import org.apache.shardingsphere.proxy.backend.connector.ProxyDatabaseConnectionManager; import org.apache.shardingsphere.proxy.backend.connector.jdbc.connection.ResourceLock; import org.apache.shardingsphere.proxy.backend.context.ProxyContext; diff --git a/test/e2e/sql/src/test/resources/cases/ral/dataset/empty_rules/mysql/show_storage_units.xml b/test/e2e/sql/src/test/resources/cases/ral/dataset/empty_rules/mysql/show_storage_units.xml index 3684fecfbb963..6f48415ff6e7d 100644 --- a/test/e2e/sql/src/test/resources/cases/ral/dataset/empty_rules/mysql/show_storage_units.xml +++ b/test/e2e/sql/src/test/resources/cases/ral/dataset/empty_rules/mysql/show_storage_units.xml @@ -30,25 +30,25 @@ - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + diff --git a/test/it/pipeline/src/test/java/org/apache/shardingsphere/test/it/data/pipeline/core/util/PipelineContextUtils.java b/test/it/pipeline/src/test/java/org/apache/shardingsphere/test/it/data/pipeline/core/util/PipelineContextUtils.java index 492839173b66d..584725cb46baf 100644 --- a/test/it/pipeline/src/test/java/org/apache/shardingsphere/test/it/data/pipeline/core/util/PipelineContextUtils.java +++ b/test/it/pipeline/src/test/java/org/apache/shardingsphere/test/it/data/pipeline/core/util/PipelineContextUtils.java @@ -62,7 +62,6 @@ import org.apache.shardingsphere.infra.yaml.config.swapper.rule.YamlRuleConfigurationSwapperEngine; import org.apache.shardingsphere.metadata.persist.MetaDataBasedPersistService; import org.apache.shardingsphere.metadata.persist.MetaDataPersistService; -import org.apache.shardingsphere.metadata.persist.NewMetaDataPersistService; import org.apache.shardingsphere.mode.manager.ContextManager; import org.apache.shardingsphere.mode.metadata.MetaDataContexts; import org.apache.shardingsphere.mode.repository.cluster.ClusterPersistRepository; @@ -107,7 +106,7 @@ public static void mockModeConfigAndContextManager() { ModeConfiguration modeConfig = new YamlModeConfigurationSwapper().swapToObject(rootConfig.getMode()); ContextManager contextManager = getContextManager(rootConfig); ClusterPersistRepository persistRepository = getClusterPersistRepository((ClusterPersistRepositoryConfiguration) modeConfig.getRepository()); - MetaDataBasedPersistService persistService = "Cluster".equals(modeConfig.getType()) ? new NewMetaDataPersistService(persistRepository) : new MetaDataPersistService(persistRepository); + MetaDataBasedPersistService persistService = "Cluster".equals(modeConfig.getType()) ? new MetaDataPersistService(persistRepository) : new MetaDataPersistService(persistRepository); MetaDataContexts metaDataContexts = renewMetaDataContexts(contextManager.getMetaDataContexts(), persistService); PipelineContext pipelineContext = new PipelineContext(modeConfig, new ContextManager(metaDataContexts, contextManager.getInstanceContext())); PipelineContextManager.putContext(contextKey, pipelineContext);