Skip to content

Commit

Permalink
Refactor MetaDataContextsFactory (#34396)
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu authored Jan 19, 2025
1 parent 106977b commit 1d89663
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 87 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,8 @@ private MetaDataContexts createMetaDataContexts(final ShardingSphereDatabase dat
Map<String, DataSourcePoolProperties> dataSourcePoolPropsFromRegCenter = metaDataPersistService.getDataSourceUnitService().load(database.getName());
SwitchingResource switchingResource = resourceSwitchManager.switchByAlterStorageUnit(database.getResourceMetaData(), dataSourcePoolPropsFromRegCenter);
Collection<RuleConfiguration> ruleConfigs = metaDataPersistService.getDatabaseRulePersistService().load(database.getName());
ShardingSphereDatabase changedDatabase = MetaDataContextsFactory
.createChangedDatabase(database.getName(), false, switchingResource, ruleConfigs, metaDataContexts, metaDataPersistService, computeNodeInstanceContext);
ShardingSphereDatabase changedDatabase = new MetaDataContextsFactory(metaDataPersistService)
.createChangedDatabase(database.getName(), false, switchingResource, ruleConfigs, metaDataContexts, computeNodeInstanceContext);
metaDataContexts.getMetaData().putDatabase(changedDatabase);
ConfigurationProperties props = new ConfigurationProperties(metaDataPersistService.getPropsService().load());
Collection<RuleConfiguration> globalRuleConfigs = metaDataPersistService.getGlobalRuleService().load();
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public synchronized void dropRuleConfiguration(final String databaseName, final
}

private void refreshMetadata(final String databaseName, final Collection<RuleConfiguration> ruleConfigurations) throws SQLException {
metaDataContexts.update(MetaDataContextsFactory.createByAlterRule(databaseName, false, ruleConfigurations, metaDataContexts, metaDataPersistService, computeNodeInstanceContext));
metaDataContexts.update(new MetaDataContextsFactory(metaDataPersistService).createByAlterRule(databaseName, false, ruleConfigurations, metaDataContexts, computeNodeInstanceContext));
}

private Collection<RuleConfiguration> getRuleConfigurations(final Collection<ShardingSphereRule> rules) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ public synchronized void unregisterStorageUnit(final String databaseName, final
}

private void buildNewMetaDataContext(final String databaseName, final SwitchingResource switchingResource) throws SQLException {
MetaDataContexts reloadMetaDataContexts = MetaDataContextsFactory.createBySwitchResource(
databaseName, true, switchingResource, metaDataContexts, metaDataPersistService, computeNodeInstanceContext);
MetaDataContexts reloadMetaDataContexts = new MetaDataContextsFactory(metaDataPersistService).createBySwitchResource(
databaseName, true, switchingResource, metaDataContexts, computeNodeInstanceContext);
metaDataContexts.update(reloadMetaDataContexts);
metaDataContexts.getMetaData().putDatabase(buildDatabase(reloadMetaDataContexts.getMetaData().getDatabase(databaseName)));
switchingResource.closeStaleDataSources();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ private GlobalRulePersistService mockGlobalRulePersistService() {
void assertCreateWithJDBCInstanceMetaData() throws SQLException {
ComputeNodeInstanceContext computeNodeInstanceContext = mock(ComputeNodeInstanceContext.class, RETURNS_DEEP_STUBS);
when(computeNodeInstanceContext.getInstance().getMetaData()).thenReturn(mock(JDBCInstanceMetaData.class));
MetaDataContexts actual = MetaDataContextsFactory.create(metaDataPersistService, createContextManagerBuilderParameter(), computeNodeInstanceContext);
MetaDataContexts actual = new MetaDataContextsFactory(metaDataPersistService).create(createContextManagerBuilderParameter(), computeNodeInstanceContext);
assertThat(actual.getMetaData().getGlobalRuleMetaData().getRules().size(), is(1));
assertThat(actual.getMetaData().getGlobalRuleMetaData().getRules().iterator().next(), instanceOf(MockedRule.class));
assertTrue(actual.getMetaData().containsDatabase("foo_db"));
Expand All @@ -120,7 +120,7 @@ void assertCreateWithJDBCInstanceMetaData() throws SQLException {
void assertCreateWithProxyInstanceMetaData() throws SQLException {
when(databaseMetaDataPersistFacade.getDatabase().loadAllDatabaseNames()).thenReturn(Collections.singletonList("foo_db"));
when(metaDataPersistService.getDatabaseMetaDataFacade()).thenReturn(databaseMetaDataPersistFacade);
MetaDataContexts actual = MetaDataContextsFactory.create(metaDataPersistService, createContextManagerBuilderParameter(), mock(ComputeNodeInstanceContext.class, RETURNS_DEEP_STUBS));
MetaDataContexts actual = new MetaDataContextsFactory(metaDataPersistService).create(createContextManagerBuilderParameter(), mock(ComputeNodeInstanceContext.class, RETURNS_DEEP_STUBS));
assertThat(actual.getMetaData().getGlobalRuleMetaData().getRules().size(), is(1));
assertThat(actual.getMetaData().getGlobalRuleMetaData().getRules().iterator().next(), instanceOf(MockedRule.class));
assertTrue(actual.getMetaData().containsDatabase("foo_db"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ public ContextManager build(final ContextManagerBuilderParameter param, final Ev
ClusterPersistRepository repository = getClusterPersistRepository(config, computeNodeInstanceContext);
LockContext lockContext = new ClusterLockContext(new GlobalLockPersistService(repository));
computeNodeInstanceContext.init(new ClusterWorkerIdGenerator(repository, param.getInstanceMetaData().getId()), lockContext);
MetaDataPersistService metaDataPersistService = new MetaDataPersistService(repository);
MetaDataContexts metaDataContexts = MetaDataContextsFactory.create(metaDataPersistService, param, computeNodeInstanceContext);
MetaDataContexts metaDataContexts = new MetaDataContextsFactory(new MetaDataPersistService(repository)).create(param, computeNodeInstanceContext);
ContextManager result = new ContextManager(metaDataContexts, computeNodeInstanceContext, repository);
registerOnline(computeNodeInstanceContext, param, result, repository);
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@ public void registerStorageUnits(final String databaseName, final Map<String, Da
SwitchingResource switchingResource = metaDataContextManager.getResourceSwitchManager()
.switchByRegisterStorageUnit(originalMetaDataContexts.getMetaData().getDatabase(databaseName).getResourceMetaData(), toBeRegisteredProps);
newDataSources.putAll(switchingResource.getNewDataSources());
MetaDataContexts reloadMetaDataContexts = MetaDataContextsFactory.createBySwitchResource(databaseName, false,
switchingResource, originalMetaDataContexts, metaDataPersistService, metaDataContextManager.getComputeNodeInstanceContext());
MetaDataContexts reloadMetaDataContexts = new MetaDataContextsFactory(metaDataPersistService).createBySwitchResource(
databaseName, false, switchingResource, originalMetaDataContexts, metaDataContextManager.getComputeNodeInstanceContext());
metaDataPersistService.getDataSourceUnitService().persist(databaseName, toBeRegisteredProps);
afterStorageUnitsAltered(databaseName, originalMetaDataContexts, reloadMetaDataContexts);
reloadMetaDataContexts.getMetaData().close();
Expand All @@ -143,8 +143,8 @@ public void alterStorageUnits(final String databaseName, final Map<String, DataS
SwitchingResource switchingResource = metaDataContextManager.getResourceSwitchManager()
.switchByAlterStorageUnit(originalMetaDataContexts.getMetaData().getDatabase(databaseName).getResourceMetaData(), toBeUpdatedProps);
newDataSources.putAll(switchingResource.getNewDataSources());
MetaDataContexts reloadMetaDataContexts = MetaDataContextsFactory.createBySwitchResource(databaseName, false,
switchingResource, originalMetaDataContexts, metaDataPersistService, metaDataContextManager.getComputeNodeInstanceContext());
MetaDataContexts reloadMetaDataContexts = new MetaDataContextsFactory(metaDataPersistService).createBySwitchResource(
databaseName, false, switchingResource, originalMetaDataContexts, metaDataContextManager.getComputeNodeInstanceContext());
DataSourceUnitPersistService dataSourceService = metaDataPersistService.getDataSourceUnitService();
metaDataPersistService.getMetaDataVersionPersistService()
.switchActiveVersion(dataSourceService.persist(databaseName, toBeUpdatedProps));
Expand All @@ -161,8 +161,8 @@ public void unregisterStorageUnits(final String databaseName, final Collection<S
MetaDataContexts originalMetaDataContexts = buildOriginalMetaDataContexts();
SwitchingResource switchingResource = metaDataContextManager.getResourceSwitchManager()
.createByUnregisterStorageUnit(originalMetaDataContexts.getMetaData().getDatabase(databaseName).getResourceMetaData(), Collections.singletonList(each));
MetaDataContexts reloadMetaDataContexts = MetaDataContextsFactory.createBySwitchResource(databaseName, false,
switchingResource, originalMetaDataContexts, metaDataPersistService, metaDataContextManager.getComputeNodeInstanceContext());
MetaDataContexts reloadMetaDataContexts = new MetaDataContextsFactory(metaDataPersistService).createBySwitchResource(
databaseName, false, switchingResource, originalMetaDataContexts, metaDataContextManager.getComputeNodeInstanceContext());
metaDataPersistService.getDataSourceUnitService().delete(databaseName, each);
afterStorageUnitsDropped(databaseName, originalMetaDataContexts, reloadMetaDataContexts);
reloadMetaDataContexts.getMetaData().close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@
import org.apache.shardingsphere.infra.instance.ComputeNodeInstanceContext;
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
import org.apache.shardingsphere.infra.util.eventbus.EventBusContext;
import org.apache.shardingsphere.mode.metadata.persist.MetaDataPersistService;
import org.apache.shardingsphere.mode.manager.ContextManager;
import org.apache.shardingsphere.mode.manager.ContextManagerBuilder;
import org.apache.shardingsphere.mode.manager.ContextManagerBuilderParameter;
import org.apache.shardingsphere.mode.manager.standalone.lock.StandaloneLockContext;
import org.apache.shardingsphere.mode.manager.standalone.workerid.StandaloneWorkerIdGenerator;
import org.apache.shardingsphere.mode.metadata.MetaDataContexts;
import org.apache.shardingsphere.mode.metadata.MetaDataContextsFactory;
import org.apache.shardingsphere.mode.metadata.persist.MetaDataPersistService;
import org.apache.shardingsphere.mode.repository.standalone.StandalonePersistRepository;

import java.sql.SQLException;
Expand All @@ -45,10 +45,9 @@ public ContextManager build(final ContextManagerBuilderParameter param, final Ev
PersistRepositoryConfiguration repositoryConfig = param.getModeConfiguration().getRepository();
StandalonePersistRepository repository = TypedSPILoader.getService(
StandalonePersistRepository.class, null == repositoryConfig ? null : repositoryConfig.getType(), null == repositoryConfig ? new Properties() : repositoryConfig.getProps());
MetaDataPersistService persistService = new MetaDataPersistService(repository);
ComputeNodeInstanceContext computeNodeInstanceContext = new ComputeNodeInstanceContext(new ComputeNodeInstance(param.getInstanceMetaData()), param.getModeConfiguration(), eventBusContext);
computeNodeInstanceContext.init(new StandaloneWorkerIdGenerator(), new StandaloneLockContext());
MetaDataContexts metaDataContexts = MetaDataContextsFactory.create(persistService, param, computeNodeInstanceContext);
MetaDataContexts metaDataContexts = new MetaDataContextsFactory(new MetaDataPersistService(repository)).create(param, computeNodeInstanceContext);
return new ContextManager(metaDataContexts, computeNodeInstanceContext, repository);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,8 @@ public void alterSchemaMetaData(final AlterSchemaMetaDataPOJO alterSchemaMetaDat
public void registerStorageUnits(final String databaseName, final Map<String, DataSourcePoolProperties> toBeRegisteredProps) throws SQLException {
SwitchingResource switchingResource = metaDataContextManager.getResourceSwitchManager().switchByRegisterStorageUnit(metaDataContextManager.getMetaDataContexts()
.getMetaData().getDatabase(databaseName).getResourceMetaData(), toBeRegisteredProps);
ShardingSphereDatabase changedDatabase = MetaDataContextsFactory.createChangedDatabase(databaseName, false, switchingResource, null,
metaDataContextManager.getMetaDataContexts(), metaDataPersistService, metaDataContextManager.getComputeNodeInstanceContext());
ShardingSphereDatabase changedDatabase = new MetaDataContextsFactory(metaDataPersistService).createChangedDatabase(databaseName, false, switchingResource, null,
metaDataContextManager.getMetaDataContexts(), metaDataContextManager.getComputeNodeInstanceContext());
metaDataContextManager.getMetaDataContexts().getMetaData().putDatabase(changedDatabase);
metaDataContextManager.getMetaDataContexts().getMetaData().getGlobalRuleMetaData().getRules()
.forEach(each -> ((GlobalRule) each).refresh(metaDataContextManager.getMetaDataContexts().getMetaData().getAllDatabases(), GlobalRuleChangedType.DATABASE_CHANGED));
Expand All @@ -246,8 +246,8 @@ public void registerStorageUnits(final String databaseName, final Map<String, Da
public void alterStorageUnits(final String databaseName, final Map<String, DataSourcePoolProperties> toBeUpdatedProps) throws SQLException {
SwitchingResource switchingResource = metaDataContextManager.getResourceSwitchManager().switchByAlterStorageUnit(metaDataContextManager.getMetaDataContexts().getMetaData()
.getDatabase(databaseName).getResourceMetaData(), toBeUpdatedProps);
ShardingSphereDatabase changedDatabase = MetaDataContextsFactory.createChangedDatabase(databaseName, true, switchingResource, null,
metaDataContextManager.getMetaDataContexts(), metaDataPersistService, metaDataContextManager.getComputeNodeInstanceContext());
ShardingSphereDatabase changedDatabase = new MetaDataContextsFactory(metaDataPersistService).createChangedDatabase(databaseName, true, switchingResource, null,
metaDataContextManager.getMetaDataContexts(), metaDataContextManager.getComputeNodeInstanceContext());
metaDataContextManager.getMetaDataContexts().getMetaData().putDatabase(changedDatabase);
metaDataContextManager.getMetaDataContexts().getMetaData().getGlobalRuleMetaData().getRules()
.forEach(each -> ((GlobalRule) each).refresh(metaDataContextManager.getMetaDataContexts().getMetaData().getAllDatabases(), GlobalRuleChangedType.DATABASE_CHANGED));
Expand All @@ -262,8 +262,8 @@ public void alterStorageUnits(final String databaseName, final Map<String, DataS
public void unregisterStorageUnits(final String databaseName, final Collection<String> toBeDroppedStorageUnitNames) throws SQLException {
SwitchingResource switchingResource = metaDataContextManager.getResourceSwitchManager().switchByUnregisterStorageUnit(metaDataContextManager.getMetaDataContexts().getMetaData()
.getDatabase(databaseName).getResourceMetaData(), toBeDroppedStorageUnitNames);
MetaDataContexts reloadMetaDataContexts = MetaDataContextsFactory.createBySwitchResource(databaseName, false, switchingResource,
metaDataContextManager.getMetaDataContexts(), metaDataPersistService, metaDataContextManager.getComputeNodeInstanceContext());
MetaDataContexts reloadMetaDataContexts = new MetaDataContextsFactory(metaDataPersistService).createBySwitchResource(databaseName, false, switchingResource,
metaDataContextManager.getMetaDataContexts(), metaDataContextManager.getComputeNodeInstanceContext());
metaDataPersistService.persistReloadDatabaseByDrop(databaseName, reloadMetaDataContexts.getMetaData().getDatabase(databaseName),
metaDataContextManager.getMetaDataContexts().getMetaData().getDatabase(databaseName));
metaDataContextManager.dropSchemas(databaseName, reloadMetaDataContexts.getMetaData().getDatabase(databaseName),
Expand Down

0 comments on commit 1d89663

Please sign in to comment.