Skip to content

Commit

Permalink
Refactor execute RAL statement reload metaData (#17720)
Browse files Browse the repository at this point in the history
* Refactor execute RAL  statement reload metaData

* Fix checkstyle.
  • Loading branch information
zhaojinchao95 authored May 17, 2022
1 parent 6b67aed commit 9742a12
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,16 @@ private void persistMetaData(final MetaDataPersistService metaDataPersistService
}));
}

private void persistMetaData(final MetaDataPersistService metaDataPersistService, final MetaDataContextsBuilder builder) {
builder.getDatabaseMap().forEach((databaseName, schemas) -> schemas.getSchemas().forEach((schemaName, tables) -> {
if (tables.getTables().isEmpty()) {
metaDataPersistService.getSchemaMetaDataService().persistSchema(databaseName, schemaName);
} else {
metaDataPersistService.getSchemaMetaDataService().persistTables(databaseName, schemaName, tables);
}
}));
}

private void alterTableSchema(final String databaseName, final String schemaName, final TableMetaData changedTableMetaData) {
ShardingSphereMetaData metaData = metaDataContexts.getMetaData(databaseName);
alterSingleTableDataNodes(databaseName, metaData, changedTableMetaData);
Expand Down Expand Up @@ -531,8 +541,7 @@ private MetaDataContexts buildChangedMetaDataContextWithAddedDataSource(final Sh
metaDataPersistService.ifPresent(optional -> persistTransactionConfiguration(databaseConfig, optional));
MetaDataContextsBuilder builder = new MetaDataContextsBuilder(
Collections.singletonMap(originalMetaData.getDatabaseName(), databaseConfig), metaDataContexts.getGlobalRuleMetaData().getConfigurations(), metaDataContexts.getProps());
metaDataContexts.getMetaDataPersistService().ifPresent(optional -> optional.getSchemaMetaDataService()
.persistTables(originalMetaData.getDatabaseName(), originalMetaData.getDatabaseName(), builder.getSchemaMap(originalMetaData.getDatabaseName())));
metaDataContexts.getMetaDataPersistService().ifPresent(optional -> persistMetaData(optional, builder));
return builder.build(metaDataContexts.getMetaDataPersistService().orElse(null));
}

Expand All @@ -546,8 +555,7 @@ private MetaDataContexts buildChangedMetaDataContext(final ShardingSphereMetaDat
MetaDataContextsBuilder builder = new MetaDataContextsBuilder(
Collections.singletonMap(originalMetaData.getDatabaseName(), new DataSourceProvidedDatabaseConfiguration(originalMetaData.getResource().getDataSources(), ruleConfigs)),
metaDataContexts.getGlobalRuleMetaData().getConfigurations(), metaDataContexts.getProps());
metaDataContexts.getMetaDataPersistService().ifPresent(optional -> optional.getSchemaMetaDataService()
.persistTables(originalMetaData.getDatabaseName(), originalMetaData.getDatabaseName(), builder.getSchemaMap(originalMetaData.getDatabaseName())));
metaDataContexts.getMetaDataPersistService().ifPresent(optional -> persistMetaData(optional, builder));
return builder.build(metaDataContexts.getMetaDataPersistService().orElse(null));
}

Expand All @@ -560,8 +568,7 @@ private MetaDataContexts buildChangedMetaDataContextWithChangedDataSource(final
originalMetaData.getRuleMetaData().getConfigurations());
MetaDataContextsBuilder builder = new MetaDataContextsBuilder(Collections.singletonMap(originalMetaData.getDatabaseName(), databaseConfig),
metaDataContexts.getGlobalRuleMetaData().getConfigurations(), metaDataContexts.getProps());
metaDataContexts.getMetaDataPersistService().ifPresent(optional -> optional.getSchemaMetaDataService()
.persistTables(originalMetaData.getDatabaseName(), originalMetaData.getDatabaseName(), builder.getSchemaMap(originalMetaData.getDatabaseName())));
metaDataContexts.getMetaDataPersistService().ifPresent(optional -> persistMetaData(optional, builder));
return builder.build(metaDataContexts.getMetaDataPersistService().orElse(null));
}

Expand All @@ -573,8 +580,7 @@ private MetaDataContexts buildChangedMetaDataContextWithChangedDataSourceAndRule
getAddedDataSources(originalMetaData, newDataSourceProps), changedDataSources, deletedDataSources), ruleConfigs);
MetaDataContextsBuilder builder = new MetaDataContextsBuilder(
Collections.singletonMap(originalMetaData.getDatabaseName(), databaseConfig), metaDataContexts.getGlobalRuleMetaData().getConfigurations(), metaDataContexts.getProps());
metaDataContexts.getMetaDataPersistService().ifPresent(optional -> optional.getSchemaMetaDataService()
.persistTables(originalMetaData.getDatabaseName(), originalMetaData.getDatabaseName(), builder.getSchemaMap(originalMetaData.getDatabaseName())));
metaDataContexts.getMetaDataPersistService().ifPresent(optional -> persistMetaData(optional, builder));
return builder.build(metaDataContexts.getMetaDataPersistService().orElse(null));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.metadata.database.loader.DatabaseLoader;
import org.apache.shardingsphere.infra.metadata.rule.ShardingSphereRuleMetaData;
import org.apache.shardingsphere.infra.metadata.schema.ShardingSphereSchema;
import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
import org.apache.shardingsphere.infra.rule.builder.global.GlobalRulesBuilder;
import org.apache.shardingsphere.infra.rule.builder.schema.SchemaRulesBuilder;
Expand Down Expand Up @@ -111,22 +110,10 @@ private Map<String, ShardingSphereMetaData> getMetaDataMap() throws SQLException
DatabaseType frontendDatabaseType = DatabaseTypeEngine.getFrontendDatabaseType(databaseConfigMap, props);
for (Entry<String, ShardingSphereDatabase> entry : databaseMap.entrySet()) {
String databaseName = entry.getKey();
// TODO support database and schema configuration separately
DatabaseConfiguration databaseConfig = databaseConfigMap.getOrDefault(databaseName, new DataSourceProvidedDatabaseConfiguration(new LinkedHashMap<>(), new LinkedList<>()));
Collection<ShardingSphereRule> rules = databaseRulesMap.getOrDefault(databaseName, new LinkedList<>());
result.put(databaseName, ShardingSphereMetaData.create(databaseName, frontendDatabaseType, entry.getValue().getSchemas(), databaseConfig, rules));
}
return result;
}

/**
* Get schema map.
*
* @param databaseName database name
* @return ShardingSphere schema
*/
public ShardingSphereSchema getSchemaMap(final String databaseName) {
// TODO remove these logic when mode support persist ShardingSphereDatabase
return databaseMap.get(databaseName).getSchemas().get(databaseName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import org.apache.shardingsphere.infra.instance.definition.InstanceDefinition;
import org.apache.shardingsphere.infra.instance.definition.InstanceType;
import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.mode.manager.ContextManager;
import org.apache.shardingsphere.mode.manager.ContextManagerBuilder;
import org.apache.shardingsphere.mode.manager.ContextManagerBuilderParameter;
Expand All @@ -53,7 +52,6 @@
import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
Expand All @@ -72,9 +70,8 @@ public ContextManager build(final ContextManagerBuilderParameter parameter) thro
persistConfigurations(metaDataPersistService, parameter);
RegistryCenter registryCenter = new RegistryCenter(repository);
MetaDataContextsBuilder builder = createMetaDataContextsBuilder(metaDataPersistService, parameter);
Map<String, ShardingSphereDatabase> databaseMap = builder.getDatabaseMap().isEmpty() ? Collections.emptyMap() : builder.getDatabaseMap();
persistMetaData(metaDataPersistService, databaseMap);
MetaDataContexts metaDataContexts = builder.build(metaDataPersistService);
persistMetaData(metaDataPersistService, metaDataContexts);
Properties transactionProps = getTransactionProperties(metaDataContexts);
persistTransactionConfiguration(parameter, metaDataPersistService, transactionProps);
ContextManager result = createContextManager(repository, metaDataPersistService, parameter.getInstanceDefinition(), metaDataContexts, transactionProps, parameter.getModeConfig());
Expand Down Expand Up @@ -146,8 +143,8 @@ private DatabaseConfiguration createDatabaseConfiguration(final String databaseN
return new DataSourceProvidedDatabaseConfiguration(dataSources, databaseRuleConfigs);
}

private void persistMetaData(final MetaDataPersistService metaDataPersistService, final Map<String, ShardingSphereDatabase> databaseMap) {
databaseMap.forEach((databaseName, schemas) -> schemas.getSchemas().forEach((schemaName, tables) -> {
private void persistMetaData(final MetaDataPersistService metaDataPersistService, final MetaDataContexts metaDataContexts) {
metaDataContexts.getMetaDataMap().forEach((databaseName, schemas) -> schemas.getSchemas().forEach((schemaName, tables) -> {
if (tables.getTables().isEmpty()) {
metaDataPersistService.getSchemaMetaDataService().persistSchema(databaseName, schemaName);
} else {
Expand Down

0 comments on commit 9742a12

Please sign in to comment.