Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor execute RAL statement reload metaData #17720

Merged
merged 2 commits into from
May 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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