From 743c32eb1dbd7b3cdb85d19e1c779cfd4648c65a Mon Sep 17 00:00:00 2001 From: Liang Zhang Date: Sun, 29 Sep 2024 23:48:48 +0800 Subject: [PATCH] Add DatabaseMetaDataPersistFacade (#33059) * Add DatabaseMetaDataPersistFacade * Add DatabaseMetaDataPersistFacade --- .../factory/InternalMetaDataFactory.java | 2 +- .../persist/MetaDataPersistService.java | 16 +++---- .../ShardingSphereDataPersistService.java | 2 +- .../DatabaseMetaDataPersistFacade.java | 48 +++++++++++++++++++ .../DatabaseMetaDataPersistService.java | 27 ++--------- .../schema/SchemaMetaDataPersistService.java | 11 ++--- .../table/TableMetaDataPersistService.java | 2 +- .../table/TableRowDataPersistService.java | 2 +- .../table/ViewMetaDataPersistService.java | 2 +- .../persist/MetaDataPersistServiceTest.java | 16 +++---- .../ShardingSphereDataPersistServiceTest.java | 2 +- .../DatabaseMetaDataPersistServiceTest.java | 19 ++------ .../SchemaMetaDataPersistServiceTest.java | 6 +-- .../TableMetaDataPersistServiceTest.java | 2 +- .../table/TableRowDataPersistServiceTest.java | 2 +- .../table/ViewMetaDataPersistServiceTest.java | 2 +- .../mode/manager/ContextManager.java | 8 ++-- .../mode/metadata/MetaDataContextManager.java | 8 ++-- .../metadata/MetaDataContextsFactory.java | 10 ++-- .../metadata/manager/StorageUnitManager.java | 5 +- .../metadata/MetaDataContextsFactoryTest.java | 12 ++--- .../cluster/ClusterContextManagerBuilder.java | 4 +- .../dispatch/MetaDataChangedSubscriber.java | 4 +- .../ClusterMetaDataManagerPersistService.java | 43 ++++++++--------- .../MetaDataChangedSubscriberTest.java | 4 +- ...sterMetaDataManagerPersistServiceTest.java | 26 +++++----- ...andaloneMetaDataManagerPersistService.java | 33 ++++++------- ...loneMetaDataManagerPersistServiceTest.java | 43 ++++++++--------- 28 files changed, 185 insertions(+), 176 deletions(-) create mode 100644 kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/metadata/DatabaseMetaDataPersistFacade.java rename kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/{ => metadata}/database/DatabaseMetaDataPersistService.java (57%) rename kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/{ => metadata}/schema/SchemaMetaDataPersistService.java (93%) rename kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/{ => metadata}/table/TableMetaDataPersistService.java (98%) rename kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/{ => metadata}/table/TableRowDataPersistService.java (98%) rename kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/{ => metadata}/table/ViewMetaDataPersistService.java (98%) rename kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/service/{ => metadata}/database/DatabaseMetaDataPersistServiceTest.java (66%) rename kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/service/{ => metadata}/schema/SchemaMetaDataPersistServiceTest.java (96%) rename kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/service/{ => metadata}/table/TableMetaDataPersistServiceTest.java (97%) rename kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/service/{ => metadata}/table/TableRowDataPersistServiceTest.java (98%) rename kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/service/{ => metadata}/table/ViewMetaDataPersistServiceTest.java (97%) 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 78f6e8c0a9bde..d19ba8b4aa794 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 @@ -51,7 +51,7 @@ public static ShardingSphereDatabase create(final String databaseName, final Met final ConfigurationProperties props, final ComputeNodeInstanceContext computeNodeInstanceContext) { DatabaseType protocolType = DatabaseTypeEngine.getProtocolType(databaseConfig, props); return ShardingSphereDatabase.create(databaseName, - protocolType, databaseConfig, computeNodeInstanceContext, persistService.getDatabaseMetaDataService().getSchemaMetaDataPersistService().load(databaseName)); + protocolType, databaseConfig, computeNodeInstanceContext, persistService.getDatabaseMetaDataFacade().getSchema().load(databaseName)); } /** 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 9b2fc18506c81..a05ab0cbb8db5 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 @@ -35,7 +35,7 @@ import org.apache.shardingsphere.metadata.persist.service.config.database.DatabaseRulePersistService; import org.apache.shardingsphere.metadata.persist.service.config.global.GlobalRulePersistService; import org.apache.shardingsphere.metadata.persist.service.config.global.PropertiesPersistService; -import org.apache.shardingsphere.metadata.persist.service.database.DatabaseMetaDataPersistService; +import org.apache.shardingsphere.metadata.persist.service.metadata.DatabaseMetaDataPersistFacade; import org.apache.shardingsphere.metadata.persist.service.version.MetaDataVersionPersistService; import org.apache.shardingsphere.mode.spi.PersistRepository; @@ -63,7 +63,7 @@ public final class MetaDataPersistService { private final DataSourceNodePersistService dataSourceNodeService; - private final DatabaseMetaDataPersistService databaseMetaDataService; + private final DatabaseMetaDataPersistFacade databaseMetaDataFacade; private final DatabaseRulePersistService databaseRulePersistService; @@ -78,7 +78,7 @@ public MetaDataPersistService(final PersistRepository repository) { metaDataVersionPersistService = new MetaDataVersionPersistService(repository); dataSourceUnitService = new DataSourceUnitPersistService(repository); dataSourceNodeService = new DataSourceNodePersistService(repository); - databaseMetaDataService = new DatabaseMetaDataPersistService(repository, metaDataVersionPersistService); + databaseMetaDataFacade = new DatabaseMetaDataPersistFacade(repository, metaDataVersionPersistService); databaseRulePersistService = new DatabaseRulePersistService(repository); globalRuleService = new GlobalRulePersistService(repository, metaDataVersionPersistService); propsService = new PropertiesPersistService(repository, metaDataVersionPersistService); @@ -107,7 +107,7 @@ public void persistGlobalRuleConfiguration(final Collection g public void persistConfigurations(final String databaseName, final DatabaseConfiguration databaseConfig, final Map dataSources, final Collection rules) { Map propsMap = getDataSourcePoolPropertiesMap(databaseConfig); if (propsMap.isEmpty() && databaseConfig.getRuleConfigurations().isEmpty()) { - databaseMetaDataService.add(databaseName); + databaseMetaDataFacade.getDatabase().add(databaseName); } else { dataSourceUnitService.persist(databaseName, propsMap); databaseRulePersistService.persist(databaseName, decorateRuleConfigs(databaseName, dataSources, rules)); @@ -151,8 +151,8 @@ public Map loadDataSourceConfigurations(final S public void persistReloadDatabaseByAlter(final String databaseName, final ShardingSphereDatabase reloadDatabase, final ShardingSphereDatabase currentDatabase) { Map toBeDeletedSchemas = GenericSchemaManager.getToBeDroppedTablesBySchemas(reloadDatabase.getSchemas(), currentDatabase.getSchemas()); Map toBeAddedSchemas = GenericSchemaManager.getToBeAddedTablesBySchemas(reloadDatabase.getSchemas(), currentDatabase.getSchemas()); - toBeAddedSchemas.forEach((key, value) -> databaseMetaDataService.getSchemaMetaDataPersistService().alterByRuleAltered(databaseName, value)); - toBeDeletedSchemas.forEach((key, value) -> databaseMetaDataService.getTableMetaDataPersistService().drop(databaseName, key, value.getTables())); + toBeAddedSchemas.forEach((key, value) -> databaseMetaDataFacade.getSchema().alterByRuleAltered(databaseName, value)); + toBeDeletedSchemas.forEach((key, value) -> databaseMetaDataFacade.getTable().drop(databaseName, key, value.getTables())); } /** @@ -165,7 +165,7 @@ public void persistReloadDatabaseByAlter(final String databaseName, final Shardi public void persistReloadDatabaseByDrop(final String databaseName, final ShardingSphereDatabase reloadDatabase, final ShardingSphereDatabase currentDatabase) { Map toBeAlterSchemas = GenericSchemaManager.getToBeDroppedTablesBySchemas(reloadDatabase.getSchemas(), currentDatabase.getSchemas()); Map toBeAddedSchemas = GenericSchemaManager.getToBeAddedTablesBySchemas(reloadDatabase.getSchemas(), currentDatabase.getSchemas()); - toBeAddedSchemas.forEach((key, value) -> databaseMetaDataService.getSchemaMetaDataPersistService().alterByRuleDropped(databaseName, key, value)); - toBeAlterSchemas.forEach((key, value) -> databaseMetaDataService.getTableMetaDataPersistService().drop(databaseName, key, value.getTables())); + toBeAddedSchemas.forEach((key, value) -> databaseMetaDataFacade.getSchema().alterByRuleDropped(databaseName, key, value)); + toBeAlterSchemas.forEach((key, value) -> databaseMetaDataFacade.getTable().drop(databaseName, key, value.getTables())); } } diff --git a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/data/ShardingSphereDataPersistService.java b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/data/ShardingSphereDataPersistService.java index 07074bf878285..c8a09ba052f5c 100644 --- a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/data/ShardingSphereDataPersistService.java +++ b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/data/ShardingSphereDataPersistService.java @@ -26,7 +26,7 @@ import org.apache.shardingsphere.infra.yaml.data.pojo.YamlShardingSphereRowData; import org.apache.shardingsphere.infra.yaml.data.swapper.YamlShardingSphereRowDataSwapper; import org.apache.shardingsphere.metadata.persist.node.ShardingSphereDataNode; -import org.apache.shardingsphere.metadata.persist.service.table.TableRowDataPersistService; +import org.apache.shardingsphere.metadata.persist.service.metadata.table.TableRowDataPersistService; import org.apache.shardingsphere.mode.spi.PersistRepository; import java.util.ArrayList; diff --git a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/metadata/DatabaseMetaDataPersistFacade.java b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/metadata/DatabaseMetaDataPersistFacade.java new file mode 100644 index 0000000000000..123b51aa9b292 --- /dev/null +++ b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/metadata/DatabaseMetaDataPersistFacade.java @@ -0,0 +1,48 @@ +/* + * 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.metadata; + +import lombok.Getter; +import org.apache.shardingsphere.metadata.persist.service.metadata.database.DatabaseMetaDataPersistService; +import org.apache.shardingsphere.metadata.persist.service.metadata.schema.SchemaMetaDataPersistService; +import org.apache.shardingsphere.metadata.persist.service.metadata.table.TableMetaDataPersistService; +import org.apache.shardingsphere.metadata.persist.service.metadata.table.ViewMetaDataPersistService; +import org.apache.shardingsphere.metadata.persist.service.version.MetaDataVersionPersistService; +import org.apache.shardingsphere.mode.spi.PersistRepository; + +/** + * Database meta data persist facade. + */ +@Getter +public final class DatabaseMetaDataPersistFacade { + + private final DatabaseMetaDataPersistService database; + + private final SchemaMetaDataPersistService schema; + + private final TableMetaDataPersistService table; + + private final ViewMetaDataPersistService view; + + public DatabaseMetaDataPersistFacade(final PersistRepository repository, final MetaDataVersionPersistService metaDataVersionPersistService) { + database = new DatabaseMetaDataPersistService(repository); + schema = new SchemaMetaDataPersistService(repository, metaDataVersionPersistService); + table = new TableMetaDataPersistService(repository, metaDataVersionPersistService); + view = new ViewMetaDataPersistService(repository, metaDataVersionPersistService); + } +} 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/metadata/database/DatabaseMetaDataPersistService.java similarity index 57% rename from kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/database/DatabaseMetaDataPersistService.java rename to kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/metadata/database/DatabaseMetaDataPersistService.java index 92735b974bf60..997d0156efe02 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/metadata/database/DatabaseMetaDataPersistService.java @@ -15,41 +15,22 @@ * limitations under the License. */ -package org.apache.shardingsphere.metadata.persist.service.database; +package org.apache.shardingsphere.metadata.persist.service.metadata.database; -import lombok.AccessLevel; -import lombok.Getter; +import lombok.RequiredArgsConstructor; import org.apache.shardingsphere.metadata.persist.node.DatabaseMetaDataNode; -import org.apache.shardingsphere.metadata.persist.service.schema.SchemaMetaDataPersistService; -import org.apache.shardingsphere.metadata.persist.service.table.TableMetaDataPersistService; -import org.apache.shardingsphere.metadata.persist.service.table.ViewMetaDataPersistService; -import org.apache.shardingsphere.metadata.persist.service.version.MetaDataVersionPersistService; import org.apache.shardingsphere.mode.spi.PersistRepository; import java.util.Collection; /** - * Database meta data registry service. + * Database meta data persist service. */ -@Getter +@RequiredArgsConstructor public final class DatabaseMetaDataPersistService { - @Getter(AccessLevel.NONE) private final PersistRepository repository; - private final SchemaMetaDataPersistService schemaMetaDataPersistService; - - private final TableMetaDataPersistService tableMetaDataPersistService; - - private final ViewMetaDataPersistService viewMetaDataPersistService; - - public DatabaseMetaDataPersistService(final PersistRepository repository, final MetaDataVersionPersistService metaDataVersionPersistService) { - this.repository = repository; - schemaMetaDataPersistService = new SchemaMetaDataPersistService(repository, metaDataVersionPersistService); - tableMetaDataPersistService = new TableMetaDataPersistService(repository, metaDataVersionPersistService); - viewMetaDataPersistService = new ViewMetaDataPersistService(repository, metaDataVersionPersistService); - } - /** * Add database. * diff --git a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/schema/SchemaMetaDataPersistService.java b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/metadata/schema/SchemaMetaDataPersistService.java similarity index 93% rename from kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/schema/SchemaMetaDataPersistService.java rename to kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/metadata/schema/SchemaMetaDataPersistService.java index 4593bb76ade32..8e7eaab2ca3d9 100644 --- a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/schema/SchemaMetaDataPersistService.java +++ b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/metadata/schema/SchemaMetaDataPersistService.java @@ -15,16 +15,14 @@ * limitations under the License. */ -package org.apache.shardingsphere.metadata.persist.service.schema; +package org.apache.shardingsphere.metadata.persist.service.metadata.schema; -import lombok.AccessLevel; -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.table.TableMetaDataPersistService; -import org.apache.shardingsphere.metadata.persist.service.table.ViewMetaDataPersistService; +import org.apache.shardingsphere.metadata.persist.service.metadata.table.TableMetaDataPersistService; +import org.apache.shardingsphere.metadata.persist.service.metadata.table.ViewMetaDataPersistService; import org.apache.shardingsphere.metadata.persist.service.version.MetaDataVersionPersistService; import org.apache.shardingsphere.mode.spi.PersistRepository; @@ -34,11 +32,10 @@ import java.util.stream.Collectors; /** - * Schema meta data registry service. + * Schema meta data persist service. */ public final class SchemaMetaDataPersistService { - @Getter(AccessLevel.NONE) private final PersistRepository repository; private final TableMetaDataPersistService tableMetaDataPersistService; diff --git a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/table/TableMetaDataPersistService.java b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/metadata/table/TableMetaDataPersistService.java similarity index 98% rename from kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/table/TableMetaDataPersistService.java rename to kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/metadata/table/TableMetaDataPersistService.java index b84b92d14dd8f..4e7dd91fb1843 100644 --- a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/table/TableMetaDataPersistService.java +++ b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/metadata/table/TableMetaDataPersistService.java @@ -15,7 +15,7 @@ * limitations under the License. */ -package org.apache.shardingsphere.metadata.persist.service.table; +package org.apache.shardingsphere.metadata.persist.service.metadata.table; import com.google.common.base.Strings; import lombok.RequiredArgsConstructor; diff --git a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/table/TableRowDataPersistService.java b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/metadata/table/TableRowDataPersistService.java similarity index 98% rename from kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/table/TableRowDataPersistService.java rename to kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/metadata/table/TableRowDataPersistService.java index 92d3928490213..e7a56ffd478e2 100644 --- a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/table/TableRowDataPersistService.java +++ b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/metadata/table/TableRowDataPersistService.java @@ -15,7 +15,7 @@ * limitations under the License. */ -package org.apache.shardingsphere.metadata.persist.service.table; +package org.apache.shardingsphere.metadata.persist.service.metadata.table; import com.google.common.base.Strings; import lombok.RequiredArgsConstructor; diff --git a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/table/ViewMetaDataPersistService.java b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/metadata/table/ViewMetaDataPersistService.java similarity index 98% rename from kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/table/ViewMetaDataPersistService.java rename to kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/metadata/table/ViewMetaDataPersistService.java index 23d2a36841374..0071f499c0d16 100644 --- a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/table/ViewMetaDataPersistService.java +++ b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/metadata/table/ViewMetaDataPersistService.java @@ -15,7 +15,7 @@ * limitations under the License. */ -package org.apache.shardingsphere.metadata.persist.service.table; +package org.apache.shardingsphere.metadata.persist.service.metadata.table; import com.google.common.base.Strings; import lombok.RequiredArgsConstructor; diff --git a/kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/MetaDataPersistServiceTest.java b/kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/MetaDataPersistServiceTest.java index 83dd8037d7092..72d6ab3c9636c 100644 --- a/kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/MetaDataPersistServiceTest.java +++ b/kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/MetaDataPersistServiceTest.java @@ -33,7 +33,7 @@ import org.apache.shardingsphere.metadata.persist.service.config.database.DatabaseRulePersistService; import org.apache.shardingsphere.metadata.persist.service.config.global.GlobalRulePersistService; import org.apache.shardingsphere.metadata.persist.service.config.global.PropertiesPersistService; -import org.apache.shardingsphere.metadata.persist.service.database.DatabaseMetaDataPersistService; +import org.apache.shardingsphere.metadata.persist.service.metadata.DatabaseMetaDataPersistFacade; import org.apache.shardingsphere.mode.spi.PersistRepository; import org.apache.shardingsphere.test.mock.AutoMockExtension; import org.apache.shardingsphere.test.mock.StaticMockSettings; @@ -67,7 +67,7 @@ class MetaDataPersistServiceTest { private DataSourceUnitPersistService dataSourceUnitService; @Mock(answer = Answers.RETURNS_DEEP_STUBS) - private DatabaseMetaDataPersistService databaseMetaDataService; + private DatabaseMetaDataPersistFacade databaseMetaDataFacade; @Mock private DatabaseRulePersistService databaseRulePersistService; @@ -84,7 +84,7 @@ class MetaDataPersistServiceTest { void setUp() throws ReflectiveOperationException { metaDataPersistService = new MetaDataPersistService(mock(PersistRepository.class)); setField("dataSourceUnitService", dataSourceUnitService); - setField("databaseMetaDataService", databaseMetaDataService); + setField("databaseMetaDataFacade", databaseMetaDataFacade); setField("databaseRulePersistService", databaseRulePersistService); setField("globalRuleService", globalRuleService); setField("propsService", propsService); @@ -106,7 +106,7 @@ void assertPersistGlobalRuleConfiguration() { @Test void assertPersistConfigurationsWithEmptyDatabase() { metaDataPersistService.persistConfigurations("foo_db", mock(DatabaseConfiguration.class), Collections.emptyMap(), Collections.emptyList()); - verify(databaseMetaDataService).add("foo_db"); + verify(databaseMetaDataFacade.getDatabase()).add("foo_db"); } @Test @@ -150,8 +150,8 @@ void assertPersistReloadDatabaseByAlter() { when(GenericSchemaManager.getToBeDroppedTablesBySchemas(any(), any())).thenReturn(Collections.singletonMap("to_be_deleted", toBeDeletedSchema)); when(GenericSchemaManager.getToBeAddedTablesBySchemas(any(), any())).thenReturn(Collections.singletonMap("to_be_added", toBeAddedSchema)); metaDataPersistService.persistReloadDatabaseByAlter("foo_db", mock(ShardingSphereDatabase.class), mock(ShardingSphereDatabase.class)); - verify(databaseMetaDataService.getSchemaMetaDataPersistService()).alterByRuleAltered("foo_db", toBeAddedSchema); - verify(databaseMetaDataService.getTableMetaDataPersistService()).drop("foo_db", "to_be_deleted", Collections.emptyMap()); + verify(databaseMetaDataFacade.getSchema()).alterByRuleAltered("foo_db", toBeAddedSchema); + verify(databaseMetaDataFacade.getTable()).drop("foo_db", "to_be_deleted", Collections.emptyMap()); } @Test @@ -161,7 +161,7 @@ void assertPersistReloadDatabaseByDrop() { when(GenericSchemaManager.getToBeDroppedTablesBySchemas(any(), any())).thenReturn(Collections.singletonMap("to_be_deleted", toBeDeletedSchema)); when(GenericSchemaManager.getToBeAddedTablesBySchemas(any(), any())).thenReturn(Collections.singletonMap("to_be_altered", toBeAlterSchema)); metaDataPersistService.persistReloadDatabaseByDrop("foo_db", mock(ShardingSphereDatabase.class), mock(ShardingSphereDatabase.class)); - verify(databaseMetaDataService.getSchemaMetaDataPersistService()).alterByRuleDropped("foo_db", "to_be_altered", toBeAlterSchema); - verify(databaseMetaDataService.getTableMetaDataPersistService()).drop("foo_db", "to_be_deleted", Collections.emptyMap()); + verify(databaseMetaDataFacade.getSchema()).alterByRuleDropped("foo_db", "to_be_altered", toBeAlterSchema); + verify(databaseMetaDataFacade.getTable()).drop("foo_db", "to_be_deleted", Collections.emptyMap()); } } diff --git a/kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/data/ShardingSphereDataPersistServiceTest.java b/kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/data/ShardingSphereDataPersistServiceTest.java index 77efc6408782a..83ae6736c6402 100644 --- a/kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/data/ShardingSphereDataPersistServiceTest.java +++ b/kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/data/ShardingSphereDataPersistServiceTest.java @@ -23,7 +23,7 @@ import org.apache.shardingsphere.infra.metadata.statistics.ShardingSphereSchemaData; import org.apache.shardingsphere.infra.metadata.statistics.ShardingSphereTableData; import org.apache.shardingsphere.infra.yaml.data.pojo.YamlShardingSphereRowData; -import org.apache.shardingsphere.metadata.persist.service.table.TableRowDataPersistService; +import org.apache.shardingsphere.metadata.persist.service.metadata.table.TableRowDataPersistService; import org.apache.shardingsphere.mode.spi.PersistRepository; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; diff --git a/kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/service/database/DatabaseMetaDataPersistServiceTest.java b/kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/service/metadata/database/DatabaseMetaDataPersistServiceTest.java similarity index 66% rename from kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/service/database/DatabaseMetaDataPersistServiceTest.java rename to kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/service/metadata/database/DatabaseMetaDataPersistServiceTest.java index ec41cdc9631a9..8547deb93072b 100644 --- a/kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/service/database/DatabaseMetaDataPersistServiceTest.java +++ b/kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/service/metadata/database/DatabaseMetaDataPersistServiceTest.java @@ -15,24 +15,19 @@ * limitations under the License. */ -package org.apache.shardingsphere.metadata.persist.service.database; +package org.apache.shardingsphere.metadata.persist.service.metadata.database; -import org.apache.shardingsphere.metadata.persist.service.table.TableMetaDataPersistService; -import org.apache.shardingsphere.metadata.persist.service.table.ViewMetaDataPersistService; -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 org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mock; -import org.mockito.internal.configuration.plugins.Plugins; import org.mockito.junit.jupiter.MockitoExtension; import java.util.Collections; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; -import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @@ -44,17 +39,9 @@ class DatabaseMetaDataPersistServiceTest { @Mock private PersistRepository repository; - @Mock - private TableMetaDataPersistService tableMetaDataPersistService; - - @Mock - private ViewMetaDataPersistService viewMetaDataPersistService; - @BeforeEach - void setUp() throws ReflectiveOperationException { - persistService = new DatabaseMetaDataPersistService(repository, mock(MetaDataVersionPersistService.class)); - Plugins.getMemberAccessor().set(DatabaseMetaDataPersistService.class.getDeclaredField("tableMetaDataPersistService"), persistService, tableMetaDataPersistService); - Plugins.getMemberAccessor().set(DatabaseMetaDataPersistService.class.getDeclaredField("viewMetaDataPersistService"), persistService, viewMetaDataPersistService); + void setUp() { + persistService = new DatabaseMetaDataPersistService(repository); } @Test diff --git a/kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/service/schema/SchemaMetaDataPersistServiceTest.java b/kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/service/metadata/schema/SchemaMetaDataPersistServiceTest.java similarity index 96% rename from kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/service/schema/SchemaMetaDataPersistServiceTest.java rename to kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/service/metadata/schema/SchemaMetaDataPersistServiceTest.java index c624a935b00ab..fd3fea4faf08f 100644 --- a/kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/service/schema/SchemaMetaDataPersistServiceTest.java +++ b/kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/service/metadata/schema/SchemaMetaDataPersistServiceTest.java @@ -15,13 +15,13 @@ * limitations under the License. */ -package org.apache.shardingsphere.metadata.persist.service.schema; +package org.apache.shardingsphere.metadata.persist.service.metadata.schema; 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.metadata.persist.service.table.TableMetaDataPersistService; -import org.apache.shardingsphere.metadata.persist.service.table.ViewMetaDataPersistService; +import org.apache.shardingsphere.metadata.persist.service.metadata.table.TableMetaDataPersistService; +import org.apache.shardingsphere.metadata.persist.service.metadata.table.ViewMetaDataPersistService; import org.apache.shardingsphere.metadata.persist.service.version.MetaDataVersionPersistService; import org.apache.shardingsphere.mode.spi.PersistRepository; import org.junit.jupiter.api.BeforeEach; diff --git a/kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/service/table/TableMetaDataPersistServiceTest.java b/kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/service/metadata/table/TableMetaDataPersistServiceTest.java similarity index 97% rename from kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/service/table/TableMetaDataPersistServiceTest.java rename to kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/service/metadata/table/TableMetaDataPersistServiceTest.java index f90544dc304ce..d7c4dc7c01ef8 100644 --- a/kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/service/table/TableMetaDataPersistServiceTest.java +++ b/kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/service/metadata/table/TableMetaDataPersistServiceTest.java @@ -15,7 +15,7 @@ * limitations under the License. */ -package org.apache.shardingsphere.metadata.persist.service.table; +package org.apache.shardingsphere.metadata.persist.service.metadata.table; import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereTable; import org.apache.shardingsphere.metadata.persist.service.version.MetaDataVersionPersistService; diff --git a/kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/service/table/TableRowDataPersistServiceTest.java b/kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/service/metadata/table/TableRowDataPersistServiceTest.java similarity index 98% rename from kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/service/table/TableRowDataPersistServiceTest.java rename to kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/service/metadata/table/TableRowDataPersistServiceTest.java index 4fab0fa68ff2c..5913fb9ae4bc7 100644 --- a/kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/service/table/TableRowDataPersistServiceTest.java +++ b/kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/service/metadata/table/TableRowDataPersistServiceTest.java @@ -15,7 +15,7 @@ * limitations under the License. */ -package org.apache.shardingsphere.metadata.persist.service.table; +package org.apache.shardingsphere.metadata.persist.service.metadata.table; import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereTable; import org.apache.shardingsphere.infra.metadata.statistics.ShardingSphereTableData; diff --git a/kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/service/table/ViewMetaDataPersistServiceTest.java b/kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/service/metadata/table/ViewMetaDataPersistServiceTest.java similarity index 97% rename from kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/service/table/ViewMetaDataPersistServiceTest.java rename to kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/service/metadata/table/ViewMetaDataPersistServiceTest.java index f61baeccd64c3..55925de5d8443 100644 --- a/kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/service/table/ViewMetaDataPersistServiceTest.java +++ b/kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/service/metadata/table/ViewMetaDataPersistServiceTest.java @@ -15,7 +15,7 @@ * limitations under the License. */ -package org.apache.shardingsphere.metadata.persist.service.table; +package org.apache.shardingsphere.metadata.persist.service.metadata.table; import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereView; import org.apache.shardingsphere.metadata.persist.service.version.MetaDataVersionPersistService; 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 2afea30357a5c..8fd5f189df8fd 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 @@ -129,10 +129,10 @@ public void reloadSchema(final ShardingSphereDatabase database, final String sch ShardingSphereSchema reloadedSchema = loadSchema(database, schemaName, dataSourceName); if (reloadedSchema.getTables().isEmpty()) { database.dropSchema(schemaName); - persistServiceFacade.getMetaDataPersistService().getDatabaseMetaDataService().getSchemaMetaDataPersistService().drop(database.getName(), schemaName); + persistServiceFacade.getMetaDataPersistService().getDatabaseMetaDataFacade().getSchema().drop(database.getName(), schemaName); } else { database.addSchema(schemaName, reloadedSchema); - persistServiceFacade.getMetaDataPersistService().getDatabaseMetaDataService().getSchemaMetaDataPersistService().alterByRefresh(database.getName(), reloadedSchema); + persistServiceFacade.getMetaDataPersistService().getDatabaseMetaDataFacade().getSchema().alterByRefresh(database.getName(), reloadedSchema); } } catch (final SQLException ex) { log.error("Reload meta data of database: {} schema: {} with data source: {} failed", database.getName(), schemaName, dataSourceName, ex); @@ -146,7 +146,7 @@ private ShardingSphereSchema loadSchema(final ShardingSphereDatabase database, f Collections.singletonMap(dataSourceName, database.getResourceMetaData().getStorageUnits().get(dataSourceName).getDataSource()), database.getRuleMetaData().getRules(), metaDataContexts.get().getMetaData().getProps(), schemaName); ShardingSphereSchema result = GenericSchemaBuilder.build(material).get(schemaName); - result.getViews().putAll(persistServiceFacade.getMetaDataPersistService().getDatabaseMetaDataService().getViewMetaDataPersistService().load(database.getName(), schemaName)); + result.getViews().putAll(persistServiceFacade.getMetaDataPersistService().getDatabaseMetaDataFacade().getView().load(database.getName(), schemaName)); return result; } @@ -189,7 +189,7 @@ public void reloadTable(final ShardingSphereDatabase database, final String sche private void persistTable(final ShardingSphereDatabase database, final String schemaName, final String tableName, final GenericSchemaBuilderMaterial material) throws SQLException { ShardingSphereSchema schema = GenericSchemaBuilder.build(Collections.singleton(tableName), material).getOrDefault(schemaName, new ShardingSphereSchema(schemaName)); - persistServiceFacade.getMetaDataPersistService().getDatabaseMetaDataService().getTableMetaDataPersistService() + persistServiceFacade.getMetaDataPersistService().getDatabaseMetaDataFacade().getTable() .persist(database.getName(), schemaName, Collections.singletonMap(tableName, schema.getTable(tableName))); } diff --git a/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/MetaDataContextManager.java b/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/MetaDataContextManager.java index 75098bc16a8a2..b556082f375b0 100644 --- a/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/MetaDataContextManager.java +++ b/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/MetaDataContextManager.java @@ -97,7 +97,7 @@ public MetaDataContextManager(final AtomicReference metaDataCo */ public void dropSchemas(final String databaseName, final ShardingSphereDatabase reloadDatabase, final ShardingSphereDatabase currentDatabase) { GenericSchemaManager.getToBeDroppedSchemas(reloadDatabase.getSchemas(), currentDatabase.getSchemas()) - .keySet().forEach(each -> metaDataPersistService.getDatabaseMetaDataService().getSchemaMetaDataPersistService().drop(databaseName, each)); + .keySet().forEach(each -> metaDataPersistService.getDatabaseMetaDataFacade().getSchema().drop(databaseName, each)); } /** @@ -121,9 +121,9 @@ public void forceRefreshDatabaseMetaData(final ShardingSphereDatabase database) metaDataContexts.get().getMetaData().getDatabase(database.getName()).getSchemas() .forEach((schemaName, schema) -> { if (schema.isEmpty()) { - metaDataPersistService.getDatabaseMetaDataService().getSchemaMetaDataPersistService().add(database.getName(), schemaName); + metaDataPersistService.getDatabaseMetaDataFacade().getSchema().add(database.getName(), schemaName); } - metaDataPersistService.getDatabaseMetaDataService().getTableMetaDataPersistService().persist(database.getName(), schemaName, schema.getTables()); + metaDataPersistService.getDatabaseMetaDataFacade().getTable().persist(database.getName(), schemaName, schema.getTables()); }); } catch (final SQLException ex) { log.error("Refresh database meta data: {} failed", database.getName(), ex); @@ -141,7 +141,7 @@ public void refreshTableMetaData(final ShardingSphereDatabase database) { dropSchemas(database.getName(), reloadedMetaDataContexts.getMetaData().getDatabase(database.getName()), database); metaDataContexts.set(reloadedMetaDataContexts); metaDataContexts.get().getMetaData().getDatabase(database.getName()).getSchemas() - .forEach((schemaName, schema) -> metaDataPersistService.getDatabaseMetaDataService().getSchemaMetaDataPersistService().alterByRefresh(database.getName(), schema)); + .forEach((schemaName, schema) -> metaDataPersistService.getDatabaseMetaDataFacade().getSchema().alterByRefresh(database.getName(), schema)); } catch (final SQLException ex) { log.error("Refresh table meta data: {} failed", database.getName(), ex); } 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 5954386f89271..122372c7b7851 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 @@ -83,7 +83,7 @@ public final class MetaDataContextsFactory { */ public static MetaDataContexts create(final MetaDataPersistService persistService, final ContextManagerBuilderParameter param, final ComputeNodeInstanceContext computeNodeInstanceContext) throws SQLException { - return persistService.getDatabaseMetaDataService().loadAllDatabaseNames().isEmpty() + return persistService.getDatabaseMetaDataFacade().getDatabase().loadAllDatabaseNames().isEmpty() ? createByLocal(persistService, param, computeNodeInstanceContext) : createByRepository(persistService, param, computeNodeInstanceContext); } @@ -143,7 +143,9 @@ private static MetaDataContexts newMetaDataContexts(final MetaDataPersistService private static Collection getDatabaseNames(final ComputeNodeInstanceContext computeNodeInstanceContext, final Map databaseConfigs, final MetaDataPersistService persistService) { - return computeNodeInstanceContext.getInstance().getMetaData() instanceof JDBCInstanceMetaData ? databaseConfigs.keySet() : persistService.getDatabaseMetaDataService().loadAllDatabaseNames(); + return computeNodeInstanceContext.getInstance().getMetaData() instanceof JDBCInstanceMetaData + ? databaseConfigs.keySet() + : persistService.getDatabaseMetaDataFacade().getDatabase().loadAllDatabaseNames(); } private static Map createEffectiveDatabaseConfigurations(final Collection databaseNames, @@ -238,9 +240,9 @@ private static void persistDatabaseConfigurations(final MetaDataContexts metadat private static void persistMetaData(final MetaDataContexts metaDataContexts, final MetaDataPersistService persistService) { metaDataContexts.getMetaData().getDatabases().values().forEach(each -> each.getSchemas().forEach((schemaName, schema) -> { if (schema.isEmpty()) { - persistService.getDatabaseMetaDataService().getSchemaMetaDataPersistService().add(each.getName(), schemaName); + persistService.getDatabaseMetaDataFacade().getSchema().add(each.getName(), schemaName); } - persistService.getDatabaseMetaDataService().getTableMetaDataPersistService().persist(each.getName(), schemaName, schema.getTables()); + persistService.getDatabaseMetaDataFacade().getTable().persist(each.getName(), schemaName, schema.getTables()); })); for (Entry databaseDataEntry : metaDataContexts.getStatistics().getDatabaseData().entrySet()) { for (Entry schemaDataEntry : databaseDataEntry.getValue().getSchemaData().entrySet()) { diff --git a/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/manager/StorageUnitManager.java b/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/manager/StorageUnitManager.java index 7674a691e26b0..1c55b920b4b83 100644 --- a/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/manager/StorageUnitManager.java +++ b/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/manager/StorageUnitManager.java @@ -121,9 +121,8 @@ private Map buildShardingSphereDatabase(final Sh private Map buildSchemas(final ShardingSphereDatabase originalDatabase) { Map result = new LinkedHashMap<>(originalDatabase.getSchemas().size(), 1F); - originalDatabase.getSchemas().keySet().forEach(schemaName -> result.put(schemaName.toLowerCase(), - new ShardingSphereSchema(schemaName, originalDatabase.getSchema(schemaName).getTables(), - metaDataPersistService.getDatabaseMetaDataService().getViewMetaDataPersistService().load(originalDatabase.getName(), schemaName)))); + originalDatabase.getSchemas().keySet().forEach(schemaName -> result.put(schemaName.toLowerCase(), new ShardingSphereSchema( + schemaName, originalDatabase.getSchema(schemaName).getTables(), metaDataPersistService.getDatabaseMetaDataFacade().getView().load(originalDatabase.getName(), schemaName)))); return result; } diff --git a/mode/core/src/test/java/org/apache/shardingsphere/mode/metadata/MetaDataContextsFactoryTest.java b/mode/core/src/test/java/org/apache/shardingsphere/mode/metadata/MetaDataContextsFactoryTest.java index 81b2d11df001d..23ce6e4f6795a 100644 --- a/mode/core/src/test/java/org/apache/shardingsphere/mode/metadata/MetaDataContextsFactoryTest.java +++ b/mode/core/src/test/java/org/apache/shardingsphere/mode/metadata/MetaDataContextsFactoryTest.java @@ -31,7 +31,7 @@ import org.apache.shardingsphere.metadata.persist.service.config.database.DatabaseRulePersistService; import org.apache.shardingsphere.metadata.persist.service.config.global.GlobalRulePersistService; import org.apache.shardingsphere.metadata.persist.service.config.global.PropertiesPersistService; -import org.apache.shardingsphere.metadata.persist.service.database.DatabaseMetaDataPersistService; +import org.apache.shardingsphere.metadata.persist.service.metadata.DatabaseMetaDataPersistFacade; import org.apache.shardingsphere.mode.manager.ContextManagerBuilderParameter; import org.apache.shardingsphere.test.fixture.infra.rule.MockedRule; import org.apache.shardingsphere.test.fixture.infra.rule.MockedRuleConfiguration; @@ -70,8 +70,8 @@ class MetaDataContextsFactoryTest { @Mock(answer = Answers.RETURNS_DEEP_STUBS) private MetaDataPersistService metaDataPersistService; - @Mock - private DatabaseMetaDataPersistService databaseMetaDataPersistService; + @Mock(answer = Answers.RETURNS_DEEP_STUBS) + private DatabaseMetaDataPersistFacade databaseMetaDataPersistFacade; @BeforeEach void setUp() throws SQLException { @@ -83,7 +83,7 @@ void setUp() throws SQLException { PropertiesPersistService propertiesPersistService = mock(PropertiesPersistService.class); when(propertiesPersistService.load()).thenReturn(new Properties()); when(metaDataPersistService.getPropsService()).thenReturn(propertiesPersistService); - when(metaDataPersistService.getDatabaseMetaDataService()).thenReturn(databaseMetaDataPersistService); + when(metaDataPersistService.getDatabaseMetaDataFacade()).thenReturn(databaseMetaDataPersistFacade); ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); when(database.getProtocolType()).thenReturn(TypedSPILoader.getService(DatabaseType.class, "FIXTURE")); when(database.getRuleMetaData().getRules()).thenReturn(Collections.emptyList()); @@ -117,8 +117,8 @@ void assertCreateWithJDBCInstanceMetaData() throws SQLException { @Test void assertCreateWithProxyInstanceMetaData() throws SQLException { - when(databaseMetaDataPersistService.loadAllDatabaseNames()).thenReturn(Collections.singletonList("foo_db")); - when(metaDataPersistService.getDatabaseMetaDataService()).thenReturn(databaseMetaDataPersistService); + when(databaseMetaDataPersistFacade.getDatabase().loadAllDatabaseNames()).thenReturn(Collections.singletonList("foo_db")); + when(metaDataPersistService.getDatabaseMetaDataFacade()).thenReturn(databaseMetaDataPersistFacade); try (MetaDataContexts actual = MetaDataContextsFactory.create(metaDataPersistService, createContextManagerBuilderParameter(), mock(ComputeNodeInstanceContext.class, RETURNS_DEEP_STUBS))) { assertThat(actual.getMetaData().getGlobalRuleMetaData().getRules().size(), is(1)); assertThat(actual.getMetaData().getGlobalRuleMetaData().getRules().iterator().next(), instanceOf(MockedRule.class)); 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 93e173d86feb9..2f33885699dea 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 @@ -81,7 +81,9 @@ private void registerOnline(final ComputeNodeInstanceContext computeNodeInstance } private Collection getDatabaseNames(final ContextManagerBuilderParameter param, final MetaDataPersistService metaDataPersistService) { - return param.getInstanceMetaData() instanceof JDBCInstanceMetaData ? param.getDatabaseConfigs().keySet() : metaDataPersistService.getDatabaseMetaDataService().loadAllDatabaseNames(); + return param.getInstanceMetaData() instanceof JDBCInstanceMetaData + ? param.getDatabaseConfigs().keySet() + : metaDataPersistService.getDatabaseMetaDataFacade().getDatabase().loadAllDatabaseNames(); } @Override diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/subscriber/dispatch/MetaDataChangedSubscriber.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/subscriber/dispatch/MetaDataChangedSubscriber.java index 1aa97490bc10c..83bc9c40765a7 100644 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/subscriber/dispatch/MetaDataChangedSubscriber.java +++ b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/subscriber/dispatch/MetaDataChangedSubscriber.java @@ -82,7 +82,7 @@ public synchronized void renew(final CreateOrAlterTableEvent event) { Preconditions.checkArgument(event.getActiveVersion().equals( contextManager.getPersistServiceFacade().getMetaDataPersistService().getMetaDataVersionPersistService().getActiveVersionByFullPath(event.getActiveVersionKey())), "Invalid active version: %s of key: %s", event.getActiveVersion(), event.getActiveVersionKey()); - ShardingSphereTable table = contextManager.getPersistServiceFacade().getMetaDataPersistService().getDatabaseMetaDataService().getTableMetaDataPersistService() + ShardingSphereTable table = contextManager.getPersistServiceFacade().getMetaDataPersistService().getDatabaseMetaDataFacade().getTable() .load(event.getDatabaseName(), event.getSchemaName(), event.getTableName()); contextManager.getMetaDataContextManager().getSchemaMetaDataManager().alterSchema(event.getDatabaseName(), event.getSchemaName(), table, null); refreshShardingSphereStatisticsData(); @@ -109,7 +109,7 @@ public synchronized void renew(final CreateOrAlterViewEvent event) { Preconditions.checkArgument(event.getActiveVersion().equals( contextManager.getPersistServiceFacade().getMetaDataPersistService().getMetaDataVersionPersistService().getActiveVersionByFullPath(event.getActiveVersionKey())), "Invalid active version: %s of key: %s", event.getActiveVersion(), event.getActiveVersionKey()); - ShardingSphereView view = contextManager.getPersistServiceFacade().getMetaDataPersistService().getDatabaseMetaDataService().getViewMetaDataPersistService() + ShardingSphereView view = contextManager.getPersistServiceFacade().getMetaDataPersistService().getDatabaseMetaDataFacade().getView() .load(event.getDatabaseName(), event.getSchemaName(), event.getViewName()); contextManager.getMetaDataContextManager().getSchemaMetaDataManager().alterSchema(event.getDatabaseName(), event.getSchemaName(), null, view); refreshShardingSphereStatisticsData(); diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/persist/ClusterMetaDataManagerPersistService.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/persist/ClusterMetaDataManagerPersistService.java index 3a30253be34eb..65ca998c91e6e 100644 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/persist/ClusterMetaDataManagerPersistService.java +++ b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/persist/ClusterMetaDataManagerPersistService.java @@ -28,7 +28,7 @@ import org.apache.shardingsphere.infra.metadata.version.MetaDataVersion; import org.apache.shardingsphere.metadata.persist.MetaDataPersistService; import org.apache.shardingsphere.metadata.persist.service.config.database.DataSourceUnitPersistService; -import org.apache.shardingsphere.metadata.persist.service.database.DatabaseMetaDataPersistService; +import org.apache.shardingsphere.metadata.persist.service.metadata.DatabaseMetaDataPersistFacade; import org.apache.shardingsphere.mode.metadata.MetaDataContextManager; import org.apache.shardingsphere.mode.metadata.MetaDataContexts; import org.apache.shardingsphere.mode.metadata.MetaDataContextsFactory; @@ -67,7 +67,7 @@ public ClusterMetaDataManagerPersistService(final PersistRepository repository, @Override public void createDatabase(final String databaseName) { - metaDataPersistService.getDatabaseMetaDataService().add(databaseName); + metaDataPersistService.getDatabaseMetaDataFacade().getDatabase().add(databaseName); listenerAssistedPersistService.persistDatabaseNameListenerAssisted(new ListenerAssisted(databaseName, ListenerAssistedType.CREATE_DATABASE)); } @@ -75,12 +75,12 @@ public void createDatabase(final String databaseName) { public void dropDatabase(final String databaseName) { String droppedDatabaseName = metaDataContextManager.getMetaDataContexts().get().getMetaData().getDatabase(databaseName).getName(); listenerAssistedPersistService.persistDatabaseNameListenerAssisted(new ListenerAssisted(droppedDatabaseName, ListenerAssistedType.DROP_DATABASE)); - metaDataPersistService.getDatabaseMetaDataService().drop(droppedDatabaseName); + metaDataPersistService.getDatabaseMetaDataFacade().getDatabase().drop(droppedDatabaseName); } @Override public void createSchema(final String databaseName, final String schemaName) { - metaDataPersistService.getDatabaseMetaDataService().getSchemaMetaDataPersistService().add(databaseName, schemaName); + metaDataPersistService.getDatabaseMetaDataFacade().getSchema().add(databaseName, schemaName); } @Override @@ -88,19 +88,17 @@ public void alterSchema(final AlterSchemaPOJO alterSchemaPOJO) { String databaseName = alterSchemaPOJO.getDatabaseName(); String schemaName = alterSchemaPOJO.getSchemaName(); ShardingSphereSchema schema = metaDataContextManager.getMetaDataContexts().get().getMetaData().getDatabase(databaseName).getSchema(schemaName); - DatabaseMetaDataPersistService databaseMetaDataService = metaDataPersistService.getDatabaseMetaDataService(); if (schema.isEmpty()) { - databaseMetaDataService.getSchemaMetaDataPersistService().add(databaseName, alterSchemaPOJO.getRenameSchemaName()); + metaDataPersistService.getDatabaseMetaDataFacade().getSchema().add(databaseName, alterSchemaPOJO.getRenameSchemaName()); } - databaseMetaDataService.getTableMetaDataPersistService().persist(databaseName, alterSchemaPOJO.getRenameSchemaName(), schema.getTables()); - databaseMetaDataService.getViewMetaDataPersistService().persist(databaseName, alterSchemaPOJO.getRenameSchemaName(), schema.getViews()); - databaseMetaDataService.getSchemaMetaDataPersistService().drop(databaseName, schemaName); + metaDataPersistService.getDatabaseMetaDataFacade().getTable().persist(databaseName, alterSchemaPOJO.getRenameSchemaName(), schema.getTables()); + metaDataPersistService.getDatabaseMetaDataFacade().getView().persist(databaseName, alterSchemaPOJO.getRenameSchemaName(), schema.getViews()); + metaDataPersistService.getDatabaseMetaDataFacade().getSchema().drop(databaseName, schemaName); } @Override public void dropSchema(final String databaseName, final Collection schemaNames) { - DatabaseMetaDataPersistService databaseMetaDataService = metaDataPersistService.getDatabaseMetaDataService(); - schemaNames.forEach(each -> databaseMetaDataService.getSchemaMetaDataPersistService().drop(databaseName, each)); + schemaNames.forEach(each -> metaDataPersistService.getDatabaseMetaDataFacade().getSchema().drop(databaseName, each)); } @Override @@ -109,11 +107,11 @@ public void alterSchemaMetaData(final AlterSchemaMetaDataPOJO alterSchemaMetaDat String schemaName = alterSchemaMetaDataPOJO.getSchemaName(); Map tables = alterSchemaMetaDataPOJO.getAlteredTables().stream().collect(Collectors.toMap(ShardingSphereTable::getName, table -> table)); Map views = alterSchemaMetaDataPOJO.getAlteredViews().stream().collect(Collectors.toMap(ShardingSphereView::getName, view -> view)); - DatabaseMetaDataPersistService databaseMetaDataService = metaDataPersistService.getDatabaseMetaDataService(); - databaseMetaDataService.getTableMetaDataPersistService().persist(databaseName, schemaName, tables); - databaseMetaDataService.getViewMetaDataPersistService().persist(databaseName, schemaName, views); - alterSchemaMetaDataPOJO.getDroppedTables().forEach(each -> databaseMetaDataService.getTableMetaDataPersistService().drop(databaseName, schemaName, each)); - alterSchemaMetaDataPOJO.getDroppedViews().forEach(each -> databaseMetaDataService.getViewMetaDataPersistService().delete(databaseName, schemaName, each)); + DatabaseMetaDataPersistFacade databaseMetaDataFacade = metaDataPersistService.getDatabaseMetaDataFacade(); + databaseMetaDataFacade.getTable().persist(databaseName, schemaName, tables); + databaseMetaDataFacade.getView().persist(databaseName, schemaName, views); + alterSchemaMetaDataPOJO.getDroppedTables().forEach(each -> databaseMetaDataFacade.getTable().drop(databaseName, schemaName, each)); + alterSchemaMetaDataPOJO.getDroppedViews().forEach(each -> databaseMetaDataFacade.getView().delete(databaseName, schemaName, each)); } @Override @@ -162,8 +160,8 @@ private Collection getToBeDroppedResourceNames(final String databaseName } private void afterStorageUnitsAltered(final String databaseName, final MetaDataContexts originalMetaDataContexts, final MetaDataContexts reloadMetaDataContexts) { - reloadMetaDataContexts.getMetaData().getDatabase(databaseName).getSchemas().forEach((schemaName, schema) -> metaDataPersistService.getDatabaseMetaDataService() - .getSchemaMetaDataPersistService().alterByRuleAltered(reloadMetaDataContexts.getMetaData().getDatabase(databaseName).getName(), schema)); + reloadMetaDataContexts.getMetaData().getDatabase(databaseName).getSchemas().forEach((schemaName, schema) -> metaDataPersistService.getDatabaseMetaDataFacade() + .getSchema().alterByRuleAltered(reloadMetaDataContexts.getMetaData().getDatabase(databaseName).getName(), schema)); Optional.ofNullable(reloadMetaDataContexts.getStatistics().getDatabaseData().get(databaseName)) .ifPresent(optional -> optional.getSchemaData().forEach((schemaName, schemaData) -> metaDataPersistService.getShardingSphereDataPersistService() .persist(originalMetaDataContexts.getMetaData().getDatabases().get(databaseName.toLowerCase()), schemaName, schemaData))); @@ -172,8 +170,8 @@ private void afterStorageUnitsAltered(final String databaseName, final MetaDataC } private void afterStorageUnitsDropped(final String databaseName, final MetaDataContexts originalMetaDataContexts, final MetaDataContexts reloadMetaDataContexts) { - reloadMetaDataContexts.getMetaData().getDatabase(databaseName).getSchemas().forEach((schemaName, schema) -> metaDataPersistService.getDatabaseMetaDataService() - .getSchemaMetaDataPersistService().alterByRuleDropped(reloadMetaDataContexts.getMetaData().getDatabase(databaseName).getName(), schemaName, schema)); + reloadMetaDataContexts.getMetaData().getDatabase(databaseName).getSchemas().forEach((schemaName, schema) -> metaDataPersistService.getDatabaseMetaDataFacade() + .getSchema().alterByRuleDropped(reloadMetaDataContexts.getMetaData().getDatabase(databaseName).getName(), schemaName, schema)); Optional.ofNullable(reloadMetaDataContexts.getStatistics().getDatabaseData().get(databaseName)) .ifPresent(optional -> optional.getSchemaData().forEach((schemaName, schemaData) -> metaDataPersistService.getShardingSphereDataPersistService() .persist(originalMetaDataContexts.getMetaData().getDatabases().get(databaseName.toLowerCase()), schemaName, schemaData))); @@ -237,12 +235,11 @@ public void alterProperties(final Properties props) { @Override public void createTable(final String databaseName, final String schemaName, final ShardingSphereTable table, final String logicDataSourceName) { - DatabaseMetaDataPersistService databaseMetaDataService = metaDataPersistService.getDatabaseMetaDataService(); - databaseMetaDataService.getTableMetaDataPersistService().persist(databaseName, schemaName, Maps.of(table.getName(), table)); + metaDataPersistService.getDatabaseMetaDataFacade().getTable().persist(databaseName, schemaName, Maps.of(table.getName(), table)); } @Override public void dropTables(final String databaseName, final String schemaName, final Collection tableNames) { - tableNames.forEach(each -> metaDataPersistService.getDatabaseMetaDataService().getTableMetaDataPersistService().drop(databaseName, schemaName, each)); + tableNames.forEach(each -> metaDataPersistService.getDatabaseMetaDataFacade().getTable().drop(databaseName, schemaName, each)); } } diff --git a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/subscriber/dispatch/MetaDataChangedSubscriberTest.java b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/subscriber/dispatch/MetaDataChangedSubscriberTest.java index fbc0033dbaaee..202e321fe0315 100644 --- a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/subscriber/dispatch/MetaDataChangedSubscriberTest.java +++ b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/subscriber/dispatch/MetaDataChangedSubscriberTest.java @@ -77,7 +77,7 @@ void assertRenewWithSchemaDeletedEvent() { void assertRenewWithCreateOrAlterTableEvent() { when(contextManager.getPersistServiceFacade().getMetaDataPersistService().getMetaDataVersionPersistService().getActiveVersionByFullPath("key")).thenReturn("value"); ShardingSphereTable table = mock(ShardingSphereTable.class); - when(contextManager.getPersistServiceFacade().getMetaDataPersistService().getDatabaseMetaDataService().getTableMetaDataPersistService().load("foo_db", "foo_schema", "foo_tbl")) + when(contextManager.getPersistServiceFacade().getMetaDataPersistService().getDatabaseMetaDataFacade().getTable().load("foo_db", "foo_schema", "foo_tbl")) .thenReturn(table); subscriber.renew(new CreateOrAlterTableEvent("foo_db", "foo_schema", "foo_tbl", "key", "value")); verify(contextManager.getMetaDataContextManager().getSchemaMetaDataManager()).alterSchema("foo_db", "foo_schema", table, null); @@ -93,7 +93,7 @@ void assertRenewWithDropTableEvent() { void assertRenewWithCreateOrAlterViewEvent() { when(contextManager.getPersistServiceFacade().getMetaDataPersistService().getMetaDataVersionPersistService().getActiveVersionByFullPath("key")).thenReturn("value"); ShardingSphereView view = mock(ShardingSphereView.class); - when(contextManager.getPersistServiceFacade().getMetaDataPersistService().getDatabaseMetaDataService().getViewMetaDataPersistService().load("foo_db", "foo_schema", "foo_view")) + when(contextManager.getPersistServiceFacade().getMetaDataPersistService().getDatabaseMetaDataFacade().getView().load("foo_db", "foo_schema", "foo_view")) .thenReturn(view); subscriber.renew(new CreateOrAlterViewEvent("foo_db", "foo_schema", "foo_view", "key", "value")); verify(contextManager.getMetaDataContextManager().getSchemaMetaDataManager()).alterSchema("foo_db", "foo_schema", null, view); diff --git a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/persist/ClusterMetaDataManagerPersistServiceTest.java b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/persist/ClusterMetaDataManagerPersistServiceTest.java index 99b8457f6a3a0..670042b24ab91 100644 --- a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/persist/ClusterMetaDataManagerPersistServiceTest.java +++ b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/persist/ClusterMetaDataManagerPersistServiceTest.java @@ -78,7 +78,7 @@ void setUp() { @Test void assertCreateDatabase() { metaDataManagerPersistService.createDatabase("foo_db"); - verify(metaDataPersistService.getDatabaseMetaDataService()).add("foo_db"); + verify(metaDataPersistService.getDatabaseMetaDataFacade().getDatabase()).add("foo_db"); verify(listenerAssistedPersistService).persistDatabaseNameListenerAssisted(any()); } @@ -87,19 +87,19 @@ void assertDropDatabase() { when(metaDataContextManager.getMetaDataContexts().get().getMetaData().getDatabase("foo_db").getName()).thenReturn("foo_db"); metaDataManagerPersistService.dropDatabase("foo_db"); verify(listenerAssistedPersistService).persistDatabaseNameListenerAssisted(any()); - verify(metaDataPersistService.getDatabaseMetaDataService()).drop("foo_db"); + verify(metaDataPersistService.getDatabaseMetaDataFacade().getDatabase()).drop("foo_db"); } @Test void assertCreateSchema() { metaDataManagerPersistService.createSchema("foo_db", "foo_schema"); - verify(metaDataPersistService.getDatabaseMetaDataService().getSchemaMetaDataPersistService()).add("foo_db", "foo_schema"); + verify(metaDataPersistService.getDatabaseMetaDataFacade().getSchema()).add("foo_db", "foo_schema"); } @Test void assertAlterNotEmptySchema() { assertAlterSchema(mock(ShardingSphereSchema.class)); - verify(metaDataPersistService.getDatabaseMetaDataService().getSchemaMetaDataPersistService(), times(0)).add("foo_db", "bar_schema"); + verify(metaDataPersistService.getDatabaseMetaDataFacade().getSchema(), times(0)).add("foo_db", "bar_schema"); } @Test @@ -107,28 +107,28 @@ void assertAlterEmptySchema() { ShardingSphereSchema schema = mock(ShardingSphereSchema.class); when(schema.isEmpty()).thenReturn(true); assertAlterSchema(schema); - verify(metaDataPersistService.getDatabaseMetaDataService().getSchemaMetaDataPersistService()).add("foo_db", "bar_schema"); + verify(metaDataPersistService.getDatabaseMetaDataFacade().getSchema()).add("foo_db", "bar_schema"); } private void assertAlterSchema(final ShardingSphereSchema schema) { when(metaDataContextManager.getMetaDataContexts().get().getMetaData().getDatabase("foo_db").getSchema("foo_schema")).thenReturn(schema); metaDataManagerPersistService.alterSchema(new AlterSchemaPOJO("foo_db", "foo_schema", "bar_schema", Collections.singleton("foo_ds"))); - verify(metaDataPersistService.getDatabaseMetaDataService().getTableMetaDataPersistService()).persist("foo_db", "bar_schema", Collections.emptyMap()); - verify(metaDataPersistService.getDatabaseMetaDataService().getViewMetaDataPersistService()).persist("foo_db", "bar_schema", Collections.emptyMap()); - verify(metaDataPersistService.getDatabaseMetaDataService().getSchemaMetaDataPersistService()).drop("foo_db", "foo_schema"); + verify(metaDataPersistService.getDatabaseMetaDataFacade().getTable()).persist("foo_db", "bar_schema", Collections.emptyMap()); + verify(metaDataPersistService.getDatabaseMetaDataFacade().getView()).persist("foo_db", "bar_schema", Collections.emptyMap()); + verify(metaDataPersistService.getDatabaseMetaDataFacade().getSchema()).drop("foo_db", "foo_schema"); } @Test void assertDropSchema() { metaDataManagerPersistService.dropSchema("foo_db", Collections.singleton("foo_schema")); - verify(metaDataPersistService.getDatabaseMetaDataService().getSchemaMetaDataPersistService()).drop("foo_db", "foo_schema"); + verify(metaDataPersistService.getDatabaseMetaDataFacade().getSchema()).drop("foo_db", "foo_schema"); } @Test void assertAlterSchemaMetaData() { metaDataManagerPersistService.alterSchemaMetaData(new AlterSchemaMetaDataPOJO("foo_db", "foo_schema", Collections.singleton("foo_ds"))); - verify(metaDataPersistService.getDatabaseMetaDataService().getTableMetaDataPersistService()).persist("foo_db", "foo_schema", Collections.emptyMap()); - verify(metaDataPersistService.getDatabaseMetaDataService().getViewMetaDataPersistService()).persist("foo_db", "foo_schema", Collections.emptyMap()); + verify(metaDataPersistService.getDatabaseMetaDataFacade().getTable()).persist("foo_db", "foo_schema", Collections.emptyMap()); + verify(metaDataPersistService.getDatabaseMetaDataFacade().getView()).persist("foo_db", "foo_schema", Collections.emptyMap()); } @Test @@ -191,12 +191,12 @@ void assertCreateTable() { ShardingSphereTable table = mock(ShardingSphereTable.class); when(table.getName()).thenReturn("foo_tbl"); metaDataManagerPersistService.createTable("foo_db", "foo_schema", table, "foo_ds"); - verify(metaDataPersistService.getDatabaseMetaDataService().getTableMetaDataPersistService()).persist("foo_db", "foo_schema", Maps.of("foo_tbl", table)); + verify(metaDataPersistService.getDatabaseMetaDataFacade().getTable()).persist("foo_db", "foo_schema", Maps.of("foo_tbl", table)); } @Test void assertDropTables() { metaDataManagerPersistService.dropTables("foo_db", "foo_schema", Collections.singleton("foo_tbl")); - verify(metaDataPersistService.getDatabaseMetaDataService().getTableMetaDataPersistService()).drop("foo_db", "foo_schema", "foo_tbl"); + verify(metaDataPersistService.getDatabaseMetaDataFacade().getTable()).drop("foo_db", "foo_schema", "foo_tbl"); } } diff --git a/mode/type/standalone/core/src/main/java/org/apache/shardingsphere/mode/manager/standalone/persist/StandaloneMetaDataManagerPersistService.java b/mode/type/standalone/core/src/main/java/org/apache/shardingsphere/mode/manager/standalone/persist/StandaloneMetaDataManagerPersistService.java index 2bdeb7e02d417..2665c2daadd32 100644 --- a/mode/type/standalone/core/src/main/java/org/apache/shardingsphere/mode/manager/standalone/persist/StandaloneMetaDataManagerPersistService.java +++ b/mode/type/standalone/core/src/main/java/org/apache/shardingsphere/mode/manager/standalone/persist/StandaloneMetaDataManagerPersistService.java @@ -35,7 +35,6 @@ import org.apache.shardingsphere.infra.spi.type.ordered.cache.OrderedServicesCache; import org.apache.shardingsphere.metadata.persist.MetaDataPersistService; import org.apache.shardingsphere.metadata.persist.service.config.database.DataSourceUnitPersistService; -import org.apache.shardingsphere.metadata.persist.service.database.DatabaseMetaDataPersistService; import org.apache.shardingsphere.mode.event.DataChangedEvent; import org.apache.shardingsphere.mode.event.DataChangedEvent.Type; import org.apache.shardingsphere.mode.event.builder.RuleConfigurationEventBuilder; @@ -81,14 +80,14 @@ public StandaloneMetaDataManagerPersistService(final PersistRepository repositor @Override public void createDatabase(final String databaseName) { metaDataContextManager.getSchemaMetaDataManager().addDatabase(databaseName); - metaDataPersistService.getDatabaseMetaDataService().add(databaseName); + metaDataPersistService.getDatabaseMetaDataFacade().getDatabase().add(databaseName); clearServiceCache(); } @Override public void dropDatabase(final String databaseName) { metaDataContextManager.getSchemaMetaDataManager().dropDatabase(databaseName); - metaDataPersistService.getDatabaseMetaDataService().drop(databaseName); + metaDataPersistService.getDatabaseMetaDataFacade().getDatabase().drop(databaseName); clearServiceCache(); } @@ -99,7 +98,7 @@ public void createSchema(final String databaseName, final String schemaName) { ShardingSphereDatabase database = metaData.getDatabase(databaseName); database.addSchema(schemaName, schema); metaData.getGlobalRuleMetaData().getRules().forEach(each -> ((GlobalRule) each).refresh(metaData.getDatabases(), GlobalRuleChangedType.SCHEMA_CHANGED)); - metaDataPersistService.getDatabaseMetaDataService().getSchemaMetaDataPersistService().add(databaseName, schemaName); + metaDataPersistService.getDatabaseMetaDataFacade().getSchema().add(databaseName, schemaName); } @Override @@ -109,16 +108,15 @@ public void alterSchema(final AlterSchemaPOJO alterSchemaPOJO) { putSchemaMetaData(database, alterSchemaPOJO.getSchemaName(), alterSchemaPOJO.getRenameSchemaName(), alterSchemaPOJO.getLogicDataSourceName()); removeSchemaMetaData(database, alterSchemaPOJO.getSchemaName()); metaData.getGlobalRuleMetaData().getRules().forEach(each -> ((GlobalRule) each).refresh(metaData.getDatabases(), GlobalRuleChangedType.SCHEMA_CHANGED)); - DatabaseMetaDataPersistService databaseMetaDataService = metaDataPersistService.getDatabaseMetaDataService(); ShardingSphereSchema alteredSchema = database.getSchema(alterSchemaPOJO.getRenameSchemaName()); String databaseName = alterSchemaPOJO.getDatabaseName(); String alteredSchemaName = alterSchemaPOJO.getRenameSchemaName(); if (alteredSchema.isEmpty()) { - databaseMetaDataService.getSchemaMetaDataPersistService().add(databaseName, alteredSchemaName); + metaDataPersistService.getDatabaseMetaDataFacade().getSchema().add(databaseName, alteredSchemaName); } - databaseMetaDataService.getTableMetaDataPersistService().persist(databaseName, alteredSchemaName, alteredSchema.getTables()); - databaseMetaDataService.getViewMetaDataPersistService().persist(databaseName, alteredSchemaName, alteredSchema.getViews()); - databaseMetaDataService.getSchemaMetaDataPersistService().drop(databaseName, alterSchemaPOJO.getSchemaName()); + metaDataPersistService.getDatabaseMetaDataFacade().getTable().persist(databaseName, alteredSchemaName, alteredSchema.getTables()); + metaDataPersistService.getDatabaseMetaDataFacade().getView().persist(databaseName, alteredSchemaName, alteredSchema.getViews()); + metaDataPersistService.getDatabaseMetaDataFacade().getSchema().drop(databaseName, alterSchemaPOJO.getSchemaName()); } private void putSchemaMetaData(final ShardingSphereDatabase database, final String schemaName, final String renameSchemaName, final String logicDataSourceName) { @@ -219,11 +217,10 @@ public void alterSchemaMetaData(final AlterSchemaMetaDataPOJO alterSchemaMetaDat addDataNode(database, alterSchemaMetaDataPOJO.getLogicDataSourceName(), schemaName, tables, views); removeDataNode(database, schemaName, alterSchemaMetaDataPOJO.getDroppedTables(), alterSchemaMetaDataPOJO.getDroppedViews()); metaData.getGlobalRuleMetaData().getRules().forEach(each -> ((GlobalRule) each).refresh(metaData.getDatabases(), GlobalRuleChangedType.SCHEMA_CHANGED)); - DatabaseMetaDataPersistService databaseMetaDataService = metaDataPersistService.getDatabaseMetaDataService(); - databaseMetaDataService.getTableMetaDataPersistService().persist(databaseName, schemaName, tables); - databaseMetaDataService.getViewMetaDataPersistService().persist(databaseName, schemaName, views); - alterSchemaMetaDataPOJO.getDroppedTables().forEach(each -> databaseMetaDataService.getTableMetaDataPersistService().drop(databaseName, schemaName, each)); - alterSchemaMetaDataPOJO.getDroppedViews().forEach(each -> databaseMetaDataService.getViewMetaDataPersistService().delete(databaseName, schemaName, each)); + metaDataPersistService.getDatabaseMetaDataFacade().getTable().persist(databaseName, schemaName, tables); + metaDataPersistService.getDatabaseMetaDataFacade().getView().persist(databaseName, schemaName, views); + alterSchemaMetaDataPOJO.getDroppedTables().forEach(each -> metaDataPersistService.getDatabaseMetaDataFacade().getTable().drop(databaseName, schemaName, each)); + alterSchemaMetaDataPOJO.getDroppedViews().forEach(each -> metaDataPersistService.getDatabaseMetaDataFacade().getView().delete(databaseName, schemaName, each)); } @Override @@ -238,9 +235,9 @@ public void registerStorageUnits(final String databaseName, final Map { if (schema.isEmpty()) { - metaDataPersistService.getDatabaseMetaDataService().getSchemaMetaDataPersistService().add(databaseName, schemaName); + metaDataPersistService.getDatabaseMetaDataFacade().getSchema().add(databaseName, schemaName); } - metaDataPersistService.getDatabaseMetaDataService().getTableMetaDataPersistService().persist(databaseName, schemaName, schema.getTables()); + metaDataPersistService.getDatabaseMetaDataFacade().getTable().persist(databaseName, schemaName, schema.getTables()); }); DataSourceUnitPersistService dataSourceService = metaDataPersistService.getDataSourceUnitService(); metaDataPersistService.getMetaDataVersionPersistService() @@ -353,7 +350,7 @@ public void createTable(final String databaseName, final String schemaName, fina ShardingSphereDatabase database = metaData.getDatabase(databaseName); addTableToDataNode(database, schemaName, logicDataSourceName, table); metaData.getGlobalRuleMetaData().getRules().forEach(each -> ((GlobalRule) each).refresh(metaData.getDatabases(), GlobalRuleChangedType.SCHEMA_CHANGED)); - metaDataPersistService.getDatabaseMetaDataService().getTableMetaDataPersistService().persist(databaseName, schemaName, Maps.of(table.getName(), table)); + metaDataPersistService.getDatabaseMetaDataFacade().getTable().persist(databaseName, schemaName, Maps.of(table.getName(), table)); } @Override @@ -362,7 +359,7 @@ public void dropTables(final String databaseName, final String schemaName, final ShardingSphereDatabase database = metaData.getDatabase(databaseName); removeTablesToDataNode(database, schemaName, tableNames); metaData.getGlobalRuleMetaData().getRules().forEach(each -> ((GlobalRule) each).refresh(metaData.getDatabases(), GlobalRuleChangedType.SCHEMA_CHANGED)); - tableNames.forEach(each -> metaDataPersistService.getDatabaseMetaDataService().getTableMetaDataPersistService().drop(databaseName, schemaName, each)); + tableNames.forEach(each -> metaDataPersistService.getDatabaseMetaDataFacade().getTable().drop(databaseName, schemaName, each)); } private void addTableToDataNode(final ShardingSphereDatabase database, final String schemaName, final String logicDataSourceName, final ShardingSphereTable table) { diff --git a/mode/type/standalone/core/src/test/java/org/apache/shardingsphere/mode/manager/standalone/persist/StandaloneMetaDataManagerPersistServiceTest.java b/mode/type/standalone/core/src/test/java/org/apache/shardingsphere/mode/manager/standalone/persist/StandaloneMetaDataManagerPersistServiceTest.java index 5bba3f1e6a81d..0ff1a6d6513f4 100644 --- a/mode/type/standalone/core/src/test/java/org/apache/shardingsphere/mode/manager/standalone/persist/StandaloneMetaDataManagerPersistServiceTest.java +++ b/mode/type/standalone/core/src/test/java/org/apache/shardingsphere/mode/manager/standalone/persist/StandaloneMetaDataManagerPersistServiceTest.java @@ -31,7 +31,7 @@ import org.apache.shardingsphere.infra.metadata.database.schema.pojo.AlterSchemaPOJO; import org.apache.shardingsphere.infra.metadata.version.MetaDataVersion; import org.apache.shardingsphere.metadata.persist.MetaDataPersistService; -import org.apache.shardingsphere.metadata.persist.service.database.DatabaseMetaDataPersistService; +import org.apache.shardingsphere.metadata.persist.service.metadata.DatabaseMetaDataPersistFacade; import org.apache.shardingsphere.mode.event.builder.RuleConfigurationEventBuilder; import org.apache.shardingsphere.mode.event.dispatch.rule.alter.AlterRuleItemEvent; import org.apache.shardingsphere.mode.event.dispatch.rule.drop.DropRuleItemEvent; @@ -85,20 +85,20 @@ void setUp() { void assertCreateDatabase() { metaDataManagerPersistService.createDatabase("foo_db"); verify(metaDataContextManager.getSchemaMetaDataManager()).addDatabase("foo_db"); - verify(metaDataPersistService.getDatabaseMetaDataService()).add("foo_db"); + verify(metaDataPersistService.getDatabaseMetaDataFacade().getDatabase()).add("foo_db"); } @Test void assertDropDatabase() { metaDataManagerPersistService.dropDatabase("foo_db"); verify(metaDataContextManager.getSchemaMetaDataManager()).dropDatabase("foo_db"); - verify(metaDataPersistService.getDatabaseMetaDataService()).drop("foo_db"); + verify(metaDataPersistService.getDatabaseMetaDataFacade().getDatabase()).drop("foo_db"); } @Test void assertCreateSchema() { metaDataManagerPersistService.createSchema("foo_db", "foo_schema"); - verify(metaDataPersistService.getDatabaseMetaDataService().getSchemaMetaDataPersistService()).add("foo_db", "foo_schema"); + verify(metaDataPersistService.getDatabaseMetaDataFacade().getSchema()).add("foo_db", "foo_schema"); } @Test @@ -107,13 +107,13 @@ void assertAlterSchemaWithEmptyAlteredSchema() { ShardingSphereMetaData metaData = new ShardingSphereMetaData( Collections.singletonMap("foo_db", database), mock(ResourceMetaData.class), mock(RuleMetaData.class), new ConfigurationProperties(new Properties())); when(metaDataContextManager.getMetaDataContexts().get().getMetaData()).thenReturn(metaData); - DatabaseMetaDataPersistService databaseMetaDataService = mock(DatabaseMetaDataPersistService.class, RETURNS_DEEP_STUBS); - when(metaDataPersistService.getDatabaseMetaDataService()).thenReturn(databaseMetaDataService); + DatabaseMetaDataPersistFacade databaseMetaDataFacade = mock(DatabaseMetaDataPersistFacade.class, RETURNS_DEEP_STUBS); + when(metaDataPersistService.getDatabaseMetaDataFacade()).thenReturn(databaseMetaDataFacade); metaDataManagerPersistService.alterSchema(new AlterSchemaPOJO("foo_db", "foo_schema", "bar_schema", Collections.singleton("foo_ds"))); - verify(databaseMetaDataService.getSchemaMetaDataPersistService(), times(0)).add("foo_db", "bar_schema"); - verify(databaseMetaDataService.getTableMetaDataPersistService()).persist("foo_db", "bar_schema", new HashMap<>()); - verify(databaseMetaDataService.getViewMetaDataPersistService()).persist("foo_db", "bar_schema", new HashMap<>()); - verify(databaseMetaDataService.getSchemaMetaDataPersistService()).drop("foo_db", "foo_schema"); + verify(databaseMetaDataFacade.getSchema(), times(0)).add("foo_db", "bar_schema"); + verify(databaseMetaDataFacade.getTable()).persist("foo_db", "bar_schema", new HashMap<>()); + verify(databaseMetaDataFacade.getView()).persist("foo_db", "bar_schema", new HashMap<>()); + verify(databaseMetaDataFacade.getSchema()).drop("foo_db", "foo_schema"); } @Test @@ -126,13 +126,13 @@ void assertAlterSchemaWithNotEmptyAlteredSchema() { ShardingSphereMetaData metaData = new ShardingSphereMetaData( Collections.singletonMap("foo_db", database), mock(ResourceMetaData.class), mock(RuleMetaData.class), new ConfigurationProperties(new Properties())); when(metaDataContextManager.getMetaDataContexts().get().getMetaData()).thenReturn(metaData); - DatabaseMetaDataPersistService databaseMetaDataService = mock(DatabaseMetaDataPersistService.class, RETURNS_DEEP_STUBS); - when(metaDataPersistService.getDatabaseMetaDataService()).thenReturn(databaseMetaDataService); + DatabaseMetaDataPersistFacade databaseMetaDataFacade = mock(DatabaseMetaDataPersistFacade.class, RETURNS_DEEP_STUBS); + when(metaDataPersistService.getDatabaseMetaDataFacade()).thenReturn(databaseMetaDataFacade); metaDataManagerPersistService.alterSchema(new AlterSchemaPOJO("foo_db", "foo_schema", "bar_schema", Collections.singleton("foo_ds"))); - verify(databaseMetaDataService.getSchemaMetaDataPersistService()).add("foo_db", "bar_schema"); - verify(databaseMetaDataService.getTableMetaDataPersistService()).persist("foo_db", "bar_schema", new HashMap<>()); - verify(databaseMetaDataService.getViewMetaDataPersistService()).persist("foo_db", "bar_schema", new HashMap<>()); - verify(databaseMetaDataService.getSchemaMetaDataPersistService()).drop("foo_db", "foo_schema"); + verify(databaseMetaDataFacade.getSchema()).add("foo_db", "bar_schema"); + verify(databaseMetaDataFacade.getTable()).persist("foo_db", "bar_schema", new HashMap<>()); + verify(databaseMetaDataFacade.getView()).persist("foo_db", "bar_schema", new HashMap<>()); + verify(databaseMetaDataFacade.getSchema()).drop("foo_db", "foo_schema"); } @Test @@ -147,11 +147,10 @@ void assertDropSchema() { @Test void assertAlterSchemaMetaData() { - DatabaseMetaDataPersistService databaseMetaDataService = mock(DatabaseMetaDataPersistService.class, RETURNS_DEEP_STUBS); - when(metaDataPersistService.getDatabaseMetaDataService()).thenReturn(databaseMetaDataService); + DatabaseMetaDataPersistFacade databaseMetaDataFacade = mock(DatabaseMetaDataPersistFacade.class, RETURNS_DEEP_STUBS); + when(metaDataPersistService.getDatabaseMetaDataFacade()).thenReturn(databaseMetaDataFacade); metaDataManagerPersistService.alterSchemaMetaData(new AlterSchemaMetaDataPOJO("foo_db", "foo_schema", Collections.singleton("foo_ds"))); - verify(databaseMetaDataService.getTableMetaDataPersistService()).persist("foo_db", "foo_schema", new HashMap<>()); - verify(databaseMetaDataService.getTableMetaDataPersistService()).persist("foo_db", "foo_schema", new HashMap<>()); + verify(databaseMetaDataFacade.getTable()).persist("foo_db", "foo_schema", new HashMap<>()); } @Test @@ -232,13 +231,13 @@ void assertAlterProperties() { void assertCreateTable() { ShardingSphereTable table = new ShardingSphereTable(); metaDataManagerPersistService.createTable("foo_db", "foo_schema", table, "foo_ds"); - verify(metaDataPersistService.getDatabaseMetaDataService().getTableMetaDataPersistService()).persist("foo_db", "foo_schema", Maps.of("", table)); + verify(metaDataPersistService.getDatabaseMetaDataFacade().getTable()).persist("foo_db", "foo_schema", Maps.of("", table)); } @Test void assertDropTables() { metaDataManagerPersistService.dropTables("foo_db", "foo_schema", Collections.singleton("foo_tbl")); - verify(metaDataPersistService.getDatabaseMetaDataService().getTableMetaDataPersistService()).drop("foo_db", "foo_schema", "foo_tbl"); + verify(metaDataPersistService.getDatabaseMetaDataFacade().getTable()).drop("foo_db", "foo_schema", "foo_tbl"); } @SneakyThrows(ReflectiveOperationException.class)