Skip to content

Commit 0b4e1de

Browse files
authored
Reload table meta data after rule altered for standalone (#34948)
1 parent 0c8c4c9 commit 0b4e1de

File tree

2 files changed

+59
-3
lines changed

2 files changed

+59
-3
lines changed

mode/type/standalone/core/src/main/java/org/apache/shardingsphere/mode/manager/standalone/persist/service/StandaloneMetaDataManagerPersistService.java

+40-1
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,23 @@
1717

1818
package org.apache.shardingsphere.mode.manager.standalone.persist.service;
1919

20+
import lombok.extern.slf4j.Slf4j;
2021
import org.apache.shardingsphere.infra.config.rule.RuleConfiguration;
22+
import org.apache.shardingsphere.infra.database.core.type.DatabaseTypeRegistry;
2123
import org.apache.shardingsphere.infra.datasource.pool.props.domain.DataSourcePoolProperties;
2224
import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
2325
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
2426
import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData;
27+
import org.apache.shardingsphere.infra.metadata.database.schema.builder.GenericSchemaBuilder;
28+
import org.apache.shardingsphere.infra.metadata.database.schema.builder.GenericSchemaBuilderMaterial;
29+
import org.apache.shardingsphere.infra.metadata.database.schema.manager.GenericSchemaManager;
2530
import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema;
2631
import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereTable;
2732
import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereView;
28-
import org.apache.shardingsphere.mode.node.path.version.MetaDataVersion;
2933
import org.apache.shardingsphere.infra.rule.scope.GlobalRule;
3034
import org.apache.shardingsphere.infra.rule.scope.GlobalRule.GlobalRuleChangedType;
3135
import org.apache.shardingsphere.infra.spi.type.ordered.cache.OrderedServicesCache;
36+
import org.apache.shardingsphere.mode.exception.LoadTableMetaDataFailedException;
3237
import org.apache.shardingsphere.mode.metadata.MetaDataContexts;
3338
import org.apache.shardingsphere.mode.metadata.changed.RuleItemChangedNodePathBuilder;
3439
import org.apache.shardingsphere.mode.metadata.manager.ActiveVersionChecker;
@@ -37,6 +42,7 @@
3742
import org.apache.shardingsphere.mode.metadata.persist.metadata.DatabaseMetaDataPersistFacade;
3843
import org.apache.shardingsphere.mode.metadata.refresher.metadata.util.TableRefreshUtils;
3944
import org.apache.shardingsphere.mode.node.path.type.database.metadata.rule.DatabaseRuleNodePath;
45+
import org.apache.shardingsphere.mode.node.path.version.MetaDataVersion;
4046
import org.apache.shardingsphere.mode.node.path.version.VersionNodePath;
4147
import org.apache.shardingsphere.mode.persist.service.MetaDataManagerPersistService;
4248
import org.apache.shardingsphere.single.config.SingleRuleConfiguration;
@@ -46,13 +52,15 @@
4652
import java.util.Collection;
4753
import java.util.Collections;
4854
import java.util.Map;
55+
import java.util.Map.Entry;
4956
import java.util.Optional;
5057
import java.util.Properties;
5158
import java.util.stream.Collectors;
5259

5360
/**
5461
* Standalone meta data manager persist service.
5562
*/
63+
@Slf4j
5664
public final class StandaloneMetaDataManagerPersistService implements MetaDataManagerPersistService {
5765

5866
private final MetaDataContextManager metaDataContextManager;
@@ -217,13 +225,15 @@ public void alterRuleConfiguration(final ShardingSphereDatabase database, final
217225
if (null == toBeAlteredRuleConfig) {
218226
return;
219227
}
228+
Collection<String> needReloadTables = getNeedReloadTables(database, toBeAlteredRuleConfig);
220229
for (MetaDataVersion each : metaDataPersistFacade.getDatabaseRuleService().persist(database.getName(), Collections.singleton(toBeAlteredRuleConfig))) {
221230
Optional<DatabaseRuleNodePath> databaseRuleNodePath = ruleItemChangedNodePathBuilder.build(database.getName(), new VersionNodePath(each.getNodePath()).getActiveVersionPath());
222231
if (databaseRuleNodePath.isPresent()
223232
&& new ActiveVersionChecker(metaDataPersistFacade.getRepository()).checkSame(new VersionNodePath(databaseRuleNodePath.get()), each.getActiveVersion())) {
224233
metaDataContextManager.getDatabaseRuleItemManager().alter(databaseRuleNodePath.get());
225234
}
226235
}
236+
reloadAlteredTables(database.getName(), needReloadTables);
227237
clearServiceCache();
228238
}
229239

@@ -232,16 +242,45 @@ public void removeRuleConfigurationItem(final ShardingSphereDatabase database, f
232242
if (null == toBeRemovedRuleConfig) {
233243
return;
234244
}
245+
Collection<String> needReloadTables = getNeedReloadTables(database, toBeRemovedRuleConfig);
235246
Collection<MetaDataVersion> metaDataVersions = metaDataPersistFacade.getDatabaseRuleService().delete(database.getName(), Collections.singleton(toBeRemovedRuleConfig));
236247
for (MetaDataVersion each : metaDataVersions) {
237248
Optional<DatabaseRuleNodePath> databaseRuleNodePath = ruleItemChangedNodePathBuilder.build(database.getName(), new VersionNodePath(each.getNodePath()).getActiveVersionPath());
238249
if (databaseRuleNodePath.isPresent()) {
239250
metaDataContextManager.getDatabaseRuleItemManager().drop(databaseRuleNodePath.get());
240251
}
241252
}
253+
reloadAlteredTables(database.getName(), needReloadTables);
242254
clearServiceCache();
243255
}
244256

257+
private void reloadAlteredTables(final String databaseName, final Collection<String> needReloadTables) {
258+
MetaDataContexts reloadMetaDataContexts = metaDataContextManager.getMetaDataContexts();
259+
ShardingSphereDatabase database = reloadMetaDataContexts.getMetaData().getDatabase(databaseName);
260+
GenericSchemaBuilderMaterial material = new GenericSchemaBuilderMaterial(database.getResourceMetaData().getStorageUnits(),
261+
database.getRuleMetaData().getRules(), reloadMetaDataContexts.getMetaData().getProps(),
262+
new DatabaseTypeRegistry(database.getProtocolType()).getDefaultSchemaName(databaseName));
263+
try {
264+
Map<String, ShardingSphereSchema> schemas = GenericSchemaBuilder.build(needReloadTables, database.getProtocolType(), material);
265+
for (Entry<String, ShardingSphereSchema> entry : schemas.entrySet()) {
266+
Collection<ShardingSphereTable> tables = GenericSchemaManager.getToBeAddedTables(entry.getValue(), database.getSchema(entry.getKey()));
267+
metaDataPersistFacade.getDatabaseMetaDataFacade().getTable().persist(databaseName, entry.getKey(), tables);
268+
tables.forEach(each -> metaDataContextManager.getDatabaseMetaDataManager().alterTable(databaseName, entry.getKey(), each));
269+
}
270+
} catch (final SQLException ex) {
271+
log.error("Load table meta failed, databaseName:{}, needReloadTables:{}", databaseName, needReloadTables, ex);
272+
throw new LoadTableMetaDataFailedException();
273+
}
274+
}
275+
276+
private Collection<String> getNeedReloadTables(final ShardingSphereDatabase originalShardingDatabase, final RuleConfiguration toBeAlteredRuleConfig) {
277+
if (toBeAlteredRuleConfig instanceof SingleRuleConfiguration) {
278+
Collection<String> originalSingleTables = originalShardingDatabase.getRuleMetaData().getSingleRule(SingleRule.class).getConfiguration().getLogicTableNames();
279+
return toBeAlteredRuleConfig.getLogicTableNames().stream().filter(each -> !originalSingleTables.contains(each)).collect(Collectors.toList());
280+
}
281+
return toBeAlteredRuleConfig.getLogicTableNames();
282+
}
283+
245284
@Override
246285
public void removeRuleConfiguration(final ShardingSphereDatabase database, final String ruleType) {
247286
metaDataPersistFacade.getDatabaseRuleService().delete(database.getName(), ruleType);

mode/type/standalone/core/src/test/java/org/apache/shardingsphere/mode/manager/standalone/persist/service/StandaloneMetaDataManagerPersistServiceTest.java

+19-2
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,16 @@
2121
import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
2222
import org.apache.shardingsphere.infra.config.rule.RuleConfiguration;
2323
import org.apache.shardingsphere.infra.database.core.metadata.database.enums.TableType;
24+
import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
2425
import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
2526
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
2627
import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData;
2728
import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema;
2829
import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereTable;
30+
import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
31+
import org.apache.shardingsphere.infra.rule.attribute.RuleAttributes;
32+
import org.apache.shardingsphere.infra.rule.attribute.table.TableMapperRuleAttribute;
33+
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
2934
import org.apache.shardingsphere.mode.metadata.changed.RuleItemChangedNodePathBuilder;
3035
import org.apache.shardingsphere.mode.metadata.manager.MetaDataContextManager;
3136
import org.apache.shardingsphere.mode.metadata.persist.MetaDataPersistFacade;
@@ -165,8 +170,12 @@ void assertAlterNullRuleConfiguration() throws SQLException {
165170

166171
@Test
167172
void assertAlterRuleConfiguration() throws SQLException {
168-
ShardingSphereDatabase database = mock(ShardingSphereDatabase.class);
173+
ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS);
169174
when(database.getName()).thenReturn("foo_db");
175+
when(database.getProtocolType()).thenReturn(TypedSPILoader.getService(DatabaseType.class, "FIXTURE"));
176+
ShardingSphereRule rule = mock(ShardingSphereRule.class);
177+
when(rule.getAttributes()).thenReturn(new RuleAttributes(mock(TableMapperRuleAttribute.class)));
178+
when(database.getRuleMetaData().getRules()).thenReturn(Collections.singleton(rule));
170179
ShardingSphereMetaData metaData = new ShardingSphereMetaData(Collections.singleton(database), mock(), mock(), new ConfigurationProperties(new Properties()));
171180
when(metaDataContextManager.getMetaDataContexts().getMetaData()).thenReturn(metaData);
172181
RuleConfiguration ruleConfig = mock(RuleConfiguration.class, RETURNS_DEEP_STUBS);
@@ -190,6 +199,14 @@ void assertRemoveNullRuleConfigurationItem() throws SQLException {
190199

191200
@Test
192201
void assertRemoveRuleConfigurationItem() throws SQLException {
202+
ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS);
203+
when(database.getName()).thenReturn("foo_db");
204+
when(database.getProtocolType()).thenReturn(TypedSPILoader.getService(DatabaseType.class, "FIXTURE"));
205+
ShardingSphereRule rule = mock(ShardingSphereRule.class);
206+
when(rule.getAttributes()).thenReturn(new RuleAttributes(mock(TableMapperRuleAttribute.class)));
207+
when(database.getRuleMetaData().getRules()).thenReturn(Collections.singleton(rule));
208+
ShardingSphereMetaData metaData = new ShardingSphereMetaData(Collections.singleton(database), mock(), mock(), new ConfigurationProperties(new Properties()));
209+
when(metaDataContextManager.getMetaDataContexts().getMetaData()).thenReturn(metaData);
193210
RuleConfiguration ruleConfig = mock(RuleConfiguration.class, RETURNS_DEEP_STUBS);
194211
MetaDataVersion metaDataVersion = mock(MetaDataVersion.class);
195212
when(metaDataPersistFacade.getDatabaseRuleService().delete("foo_db", Collections.singleton(ruleConfig))).thenReturn(Collections.singleton(metaDataVersion));
@@ -198,7 +215,7 @@ void assertRemoveRuleConfigurationItem() throws SQLException {
198215
when(metaDataVersion.getNodePath()).thenReturn(databaseRuleNodePath);
199216
when(ruleItemChangedNodePathBuilder.build(eq("foo_db"), any())).thenReturn(Optional.of(databaseRuleNodePath));
200217
setRuleItemChangedBuildExecutor(ruleItemChangedNodePathBuilder);
201-
metaDataManagerPersistService.removeRuleConfigurationItem(new ShardingSphereDatabase("foo_db", mock(), mock(), mock(), Collections.emptyList()), ruleConfig);
218+
metaDataManagerPersistService.removeRuleConfigurationItem(database, ruleConfig);
202219
verify(metaDataContextManager.getDatabaseRuleItemManager()).drop(databaseRuleNodePath);
203220
}
204221

0 commit comments

Comments
 (0)