Skip to content

Commit

Permalink
Add DatabaseMetaDataPersistFacade (#33059)
Browse files Browse the repository at this point in the history
* Add DatabaseMetaDataPersistFacade

* Add DatabaseMetaDataPersistFacade
  • Loading branch information
terrymanu authored Sep 29, 2024
1 parent 2aef61f commit 743c32e
Show file tree
Hide file tree
Showing 28 changed files with 185 additions and 176 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -63,7 +63,7 @@ public final class MetaDataPersistService {

private final DataSourceNodePersistService dataSourceNodeService;

private final DatabaseMetaDataPersistService databaseMetaDataService;
private final DatabaseMetaDataPersistFacade databaseMetaDataFacade;

private final DatabaseRulePersistService databaseRulePersistService;

Expand All @@ -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);
Expand Down Expand Up @@ -107,7 +107,7 @@ public void persistGlobalRuleConfiguration(final Collection<RuleConfiguration> g
public void persistConfigurations(final String databaseName, final DatabaseConfiguration databaseConfig, final Map<String, DataSource> dataSources, final Collection<ShardingSphereRule> rules) {
Map<String, DataSourcePoolProperties> 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));
Expand Down Expand Up @@ -151,8 +151,8 @@ public Map<String, DataSourceConfiguration> loadDataSourceConfigurations(final S
public void persistReloadDatabaseByAlter(final String databaseName, final ShardingSphereDatabase reloadDatabase, final ShardingSphereDatabase currentDatabase) {
Map<String, ShardingSphereSchema> toBeDeletedSchemas = GenericSchemaManager.getToBeDroppedTablesBySchemas(reloadDatabase.getSchemas(), currentDatabase.getSchemas());
Map<String, ShardingSphereSchema> 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()));
}

/**
Expand All @@ -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<String, ShardingSphereSchema> toBeAlterSchemas = GenericSchemaManager.getToBeDroppedTablesBySchemas(reloadDatabase.getSchemas(), currentDatabase.getSchemas());
Map<String, ShardingSphereSchema> 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()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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
Expand Down
Loading

0 comments on commit 743c32e

Please sign in to comment.