Skip to content

Commit

Permalink
Refactor build MetaDataContext to avoid modifying data sources when n…
Browse files Browse the repository at this point in the history
…ew metadata context not finish built (#31434)
  • Loading branch information
zhaojinchao95 authored May 29, 2024
1 parent 4b01f61 commit 40929eb
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,6 @@ private MetaDataContexts createMetaDataContexts(final ShardingSphereDatabase dat
ConfigurationManager configurationManager = metaDataContextManager.getConfigurationManager();
Map<String, DataSourcePoolProperties> dataSourcePoolPropsFromRegCenter = metaDataPersistService.getDataSourceUnitService().load(database.getName());
SwitchingResource switchingResource = new ResourceSwitchManager().alterStorageUnit(database.getResourceMetaData(), dataSourcePoolPropsFromRegCenter);
metaDataContexts.get().getMetaData().getDatabases().putAll(configurationManager.renewDatabase(database, switchingResource));
Collection<RuleConfiguration> ruleConfigs = metaDataPersistService.getDatabaseRulePersistService().load(database.getName());
Map<String, ShardingSphereDatabase> changedDatabases = configurationManager.createChangedDatabases(database.getName(), false, switchingResource, ruleConfigs);
ConfigurationProperties props = new ConfigurationProperties(metaDataPersistService.getPropsService().load());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,6 @@ public synchronized void unregisterStorageUnit(final String databaseName, final
}

private void buildNewMetaDataContext(final String databaseName, final SwitchingResource switchingResource, final boolean isDropConfig) throws SQLException {
metaDataContexts.get().getMetaData().getDatabases().putAll(renewDatabase(metaDataContexts.get().getMetaData().getDatabase(databaseName), switchingResource));
MetaDataContexts reloadMetaDataContexts = createMetaDataContexts(databaseName, false, switchingResource, null);
persistSchemaMetaData(databaseName, reloadMetaDataContexts, isDropConfig);
Optional.ofNullable(reloadMetaDataContexts.getStatistics().getDatabaseData().get(databaseName))
Expand Down Expand Up @@ -244,23 +243,22 @@ public void alterSchemaMetaData(final String databaseName, final ShardingSphereD
}

/**
* Renew ShardingSphere databases.
* Get effective resource meta data.
*
* @param database database
* @param resource resource
* @return ShardingSphere databases
* @return effective resource meta data
*/
public Map<String, ShardingSphereDatabase> renewDatabase(final ShardingSphereDatabase database, final SwitchingResource resource) {
public ResourceMetaData getEffectiveResourceMetaData(final ShardingSphereDatabase database, final SwitchingResource resource) {
Map<StorageNode, DataSource> storageNodes = getStorageNodes(database.getResourceMetaData().getDataSources(), resource);
Map<String, StorageUnit> storageUnits = getStorageUnits(database.getResourceMetaData().getStorageUnits(), resource);
return Collections.singletonMap(database.getName().toLowerCase(), new ShardingSphereDatabase(database.getName(), database.getProtocolType(),
new ResourceMetaData(storageNodes, storageUnits), database.getRuleMetaData(), database.getSchemas()));
return new ResourceMetaData(storageNodes, storageUnits);
}

private Map<StorageNode, DataSource> getStorageNodes(final Map<StorageNode, DataSource> currentStorageNodes, final SwitchingResource resource) {
Map<StorageNode, DataSource> result = new LinkedHashMap<>(currentStorageNodes.size(), 1F);
for (Entry<StorageNode, DataSource> entry : currentStorageNodes.entrySet()) {
if (!resource.getStaleDataSources().containsKey(entry.getKey())) {
if (null == resource || !resource.getStaleDataSources().containsKey(entry.getKey())) {
result.put(entry.getKey(), entry.getValue());
}
}
Expand All @@ -270,7 +268,7 @@ private Map<StorageNode, DataSource> getStorageNodes(final Map<StorageNode, Data
private Map<String, StorageUnit> getStorageUnits(final Map<String, StorageUnit> currentStorageUnits, final SwitchingResource resource) {
Map<String, StorageUnit> result = new LinkedHashMap<>(currentStorageUnits.size(), 1F);
for (Entry<String, StorageUnit> entry : currentStorageUnits.entrySet()) {
if (!resource.getStaleStorageUnitNames().contains(entry.getKey())) {
if (null == resource || !resource.getStaleStorageUnitNames().contains(entry.getKey())) {
result.put(entry.getKey(), entry.getValue());
}
}
Expand All @@ -296,8 +294,8 @@ private void closeStaleRules(final String databaseName) {
* @return MetaDataContexts meta data contexts
* @throws SQLException SQL exception
*/
public MetaDataContexts createMetaDataContexts(final String databaseName, final boolean internalLoadMetaData, final SwitchingResource switchingResource,
final Collection<RuleConfiguration> ruleConfigs) throws SQLException {
public MetaDataContexts createMetaDataContexts(final String databaseName, final boolean internalLoadMetaData,
final SwitchingResource switchingResource, final Collection<RuleConfiguration> ruleConfigs) throws SQLException {
Map<String, ShardingSphereDatabase> changedDatabases = createChangedDatabases(databaseName, internalLoadMetaData, switchingResource, ruleConfigs);
ConfigurationProperties props = metaDataContexts.get().getMetaData().getProps();
RuleMetaData changedGlobalMetaData = new RuleMetaData(
Expand All @@ -317,11 +315,11 @@ public MetaDataContexts createMetaDataContexts(final String databaseName, final
*/
public synchronized Map<String, ShardingSphereDatabase> createChangedDatabases(final String databaseName, final boolean internalLoadMetaData,
final SwitchingResource switchingResource, final Collection<RuleConfiguration> ruleConfigs) throws SQLException {
ResourceMetaData effectiveResourceMetaData = getEffectiveResourceMetaData(metaDataContexts.get().getMetaData().getDatabase(databaseName), switchingResource);
Collection<RuleConfiguration> toBeCreatedRuleConfigs = null == ruleConfigs
? metaDataContexts.get().getMetaData().getDatabase(databaseName).getRuleMetaData().getConfigurations()
: ruleConfigs;
DatabaseConfiguration toBeCreatedDatabaseConfig = getDatabaseConfiguration(
metaDataContexts.get().getMetaData().getDatabase(databaseName).getResourceMetaData(), switchingResource, toBeCreatedRuleConfigs);
DatabaseConfiguration toBeCreatedDatabaseConfig = getDatabaseConfiguration(effectiveResourceMetaData, switchingResource, toBeCreatedRuleConfigs);
ShardingSphereDatabase changedDatabase = createChangedDatabase(metaDataContexts.get().getMetaData().getDatabase(databaseName).getName(), internalLoadMetaData,
persistServiceFacade.getMetaDataPersistService(), toBeCreatedDatabaseConfig, metaDataContexts.get().getMetaData().getProps(), computeNodeInstanceContext);
Map<String, ShardingSphereDatabase> result = new LinkedHashMap<>(metaDataContexts.get().getMetaData().getDatabases());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,6 @@ public void unregisterStorageUnits(final String databaseName, final Collection<S
SwitchingResource switchingResource =
new ResourceSwitchManager().unregisterStorageUnit(contextManager.getMetaDataContexts().getMetaData().getDatabase(databaseName).getResourceMetaData(), toBeDroppedStorageUnitNames);
ConfigurationManager configurationManager = contextManager.getMetaDataContextManager().getConfigurationManager();
contextManager.getMetaDataContexts().getMetaData().getDatabases()
.putAll(configurationManager.renewDatabase(contextManager.getMetaDataContexts().getMetaData().getDatabase(databaseName), switchingResource));
MetaDataContexts reloadMetaDataContexts = configurationManager.createMetaDataContexts(databaseName, false, switchingResource, null);
configurationManager.alterSchemaMetaData(databaseName, reloadMetaDataContexts.getMetaData().getDatabase(databaseName),
contextManager.getMetaDataContexts().getMetaData().getDatabase(databaseName), true);
Expand Down

0 comments on commit 40929eb

Please sign in to comment.