From 40929eb7f3bdfc8e9db89972cab861670c7ad078 Mon Sep 17 00:00:00 2001 From: zhaojinchao Date: Wed, 29 May 2024 13:43:48 +0800 Subject: [PATCH] Refactor build MetaDataContext to avoid modifying data sources when new metadata context not finish built (#31434) --- .../mode/manager/ContextManager.java | 1 - .../service/manager/ConfigurationManager.java | 22 +++++++++---------- ...andaloneMetaDataManagerPersistService.java | 2 -- 3 files changed, 10 insertions(+), 15 deletions(-) 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 76ee61a239150..a6af4feacf171 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 @@ -173,7 +173,6 @@ private MetaDataContexts createMetaDataContexts(final ShardingSphereDatabase dat ConfigurationManager configurationManager = metaDataContextManager.getConfigurationManager(); Map dataSourcePoolPropsFromRegCenter = metaDataPersistService.getDataSourceUnitService().load(database.getName()); SwitchingResource switchingResource = new ResourceSwitchManager().alterStorageUnit(database.getResourceMetaData(), dataSourcePoolPropsFromRegCenter); - metaDataContexts.get().getMetaData().getDatabases().putAll(configurationManager.renewDatabase(database, switchingResource)); Collection ruleConfigs = metaDataPersistService.getDatabaseRulePersistService().load(database.getName()); Map changedDatabases = configurationManager.createChangedDatabases(database.getName(), false, switchingResource, ruleConfigs); ConfigurationProperties props = new ConfigurationProperties(metaDataPersistService.getPropsService().load()); diff --git a/mode/core/src/main/java/org/apache/shardingsphere/mode/service/manager/ConfigurationManager.java b/mode/core/src/main/java/org/apache/shardingsphere/mode/service/manager/ConfigurationManager.java index 5fb6face9553b..d90a65a6c69ec 100644 --- a/mode/core/src/main/java/org/apache/shardingsphere/mode/service/manager/ConfigurationManager.java +++ b/mode/core/src/main/java/org/apache/shardingsphere/mode/service/manager/ConfigurationManager.java @@ -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)) @@ -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 renewDatabase(final ShardingSphereDatabase database, final SwitchingResource resource) { + public ResourceMetaData getEffectiveResourceMetaData(final ShardingSphereDatabase database, final SwitchingResource resource) { Map storageNodes = getStorageNodes(database.getResourceMetaData().getDataSources(), resource); Map 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 getStorageNodes(final Map currentStorageNodes, final SwitchingResource resource) { Map result = new LinkedHashMap<>(currentStorageNodes.size(), 1F); for (Entry entry : currentStorageNodes.entrySet()) { - if (!resource.getStaleDataSources().containsKey(entry.getKey())) { + if (null == resource || !resource.getStaleDataSources().containsKey(entry.getKey())) { result.put(entry.getKey(), entry.getValue()); } } @@ -270,7 +268,7 @@ private Map getStorageNodes(final Map getStorageUnits(final Map currentStorageUnits, final SwitchingResource resource) { Map result = new LinkedHashMap<>(currentStorageUnits.size(), 1F); for (Entry entry : currentStorageUnits.entrySet()) { - if (!resource.getStaleStorageUnitNames().contains(entry.getKey())) { + if (null == resource || !resource.getStaleStorageUnitNames().contains(entry.getKey())) { result.put(entry.getKey(), entry.getValue()); } } @@ -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 ruleConfigs) throws SQLException { + public MetaDataContexts createMetaDataContexts(final String databaseName, final boolean internalLoadMetaData, + final SwitchingResource switchingResource, final Collection ruleConfigs) throws SQLException { Map changedDatabases = createChangedDatabases(databaseName, internalLoadMetaData, switchingResource, ruleConfigs); ConfigurationProperties props = metaDataContexts.get().getMetaData().getProps(); RuleMetaData changedGlobalMetaData = new RuleMetaData( @@ -317,11 +315,11 @@ public MetaDataContexts createMetaDataContexts(final String databaseName, final */ public synchronized Map createChangedDatabases(final String databaseName, final boolean internalLoadMetaData, final SwitchingResource switchingResource, final Collection ruleConfigs) throws SQLException { + ResourceMetaData effectiveResourceMetaData = getEffectiveResourceMetaData(metaDataContexts.get().getMetaData().getDatabase(databaseName), switchingResource); Collection 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 result = new LinkedHashMap<>(metaDataContexts.get().getMetaData().getDatabases()); diff --git a/mode/type/standalone/core/src/main/java/org/apache/shardingsphere/mode/manager/standalone/service/StandaloneMetaDataManagerPersistService.java b/mode/type/standalone/core/src/main/java/org/apache/shardingsphere/mode/manager/standalone/service/StandaloneMetaDataManagerPersistService.java index b526e0a0a0c4e..f560075922613 100644 --- a/mode/type/standalone/core/src/main/java/org/apache/shardingsphere/mode/manager/standalone/service/StandaloneMetaDataManagerPersistService.java +++ b/mode/type/standalone/core/src/main/java/org/apache/shardingsphere/mode/manager/standalone/service/StandaloneMetaDataManagerPersistService.java @@ -250,8 +250,6 @@ public void unregisterStorageUnits(final String databaseName, final Collection