Skip to content

Commit

Permalink
Add computeNodeInstanceContext as field of MetaDataContextsFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu committed Jan 19, 2025
1 parent 59b349c commit a2fdcef
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 42 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 = new MetaDataContextsFactory(metaDataPersistService)
.createChangedDatabase(database.getName(), false, switchingResource, ruleConfigs, metaDataContexts, computeNodeInstanceContext);
ShardingSphereDatabase changedDatabase = new MetaDataContextsFactory(metaDataPersistService, computeNodeInstanceContext)
.createChangedDatabase(database.getName(), false, switchingResource, ruleConfigs, metaDataContexts);
metaDataContexts.getMetaData().putDatabase(changedDatabase);
ConfigurationProperties props = new ConfigurationProperties(metaDataPersistService.getPropsService().load());
Collection<RuleConfiguration> globalRuleConfigs = metaDataPersistService.getGlobalRuleService().load();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,24 +64,25 @@ public final class MetaDataContextsFactory {

private final MetaDataPersistService persistService;

private final ComputeNodeInstanceContext instanceContext;

/**
* Create meta data contexts.
*
* @param param context manager builder parameter
* @param instanceContext compute node instance context
* @return meta data contexts
* @throws SQLException SQL exception
*/
public MetaDataContexts create(final ContextManagerBuilderParameter param, final ComputeNodeInstanceContext instanceContext) throws SQLException {
return containsRegisteredDatabases() ? createFromRegisterCenter(param, instanceContext) : createFromLocalConfiguration(param, instanceContext);
public MetaDataContexts create(final ContextManagerBuilderParameter param) throws SQLException {
return containsRegisteredDatabases() ? createFromRegisterCenter(param) : createFromLocalConfiguration(param);
}

private boolean containsRegisteredDatabases() {
return !persistService.getDatabaseMetaDataFacade().getDatabase().loadAllDatabaseNames().isEmpty();
}

private MetaDataContexts createFromRegisterCenter(final ContextManagerBuilderParameter param, final ComputeNodeInstanceContext instanceContext) {
Map<String, DatabaseConfiguration> effectiveDatabaseConfigs = createEffectiveDatabaseConfigurations(getDatabaseNames(param.getDatabaseConfigs(), instanceContext), param.getDatabaseConfigs());
private MetaDataContexts createFromRegisterCenter(final ContextManagerBuilderParameter param) {
Map<String, DatabaseConfiguration> effectiveDatabaseConfigs = createEffectiveDatabaseConfigurations(getDatabaseNames(param.getDatabaseConfigs()), param.getDatabaseConfigs());
Collection<RuleConfiguration> globalRuleConfigs = persistService.getGlobalRuleService().load();
// TODO load global data sources from persist service
Map<String, DataSource> globalDataSources = param.getGlobalDataSources();
Expand All @@ -91,7 +92,7 @@ private MetaDataContexts createFromRegisterCenter(final ContextManagerBuilderPar
return createMetaDataContexts(globalRuleConfigs, globalDataSources, databases, props);
}

private MetaDataContexts createFromLocalConfiguration(final ContextManagerBuilderParameter param, final ComputeNodeInstanceContext instanceContext) throws SQLException {
private MetaDataContexts createFromLocalConfiguration(final ContextManagerBuilderParameter param) throws SQLException {
ConfigurationProperties props = new ConfigurationProperties(param.getProps());
Collection<ShardingSphereDatabase> databases = ShardingSphereDatabasesFactory.create(param.getDatabaseConfigs(), props, instanceContext);
MetaDataContexts result = createMetaDataContexts(param.getGlobalRuleConfigs(), param.getGlobalDataSources(), databases, props);
Expand Down Expand Up @@ -135,7 +136,7 @@ private void persistMetaData(final MetaDataContexts metaDataContexts) {
}
}

private Collection<String> getDatabaseNames(final Map<String, DatabaseConfiguration> databaseConfigs, final ComputeNodeInstanceContext instanceContext) {
private Collection<String> getDatabaseNames(final Map<String, DatabaseConfiguration> databaseConfigs) {
return instanceContext.getInstance().getMetaData() instanceof JDBCInstanceMetaData
? databaseConfigs.keySet()
: persistService.getDatabaseMetaDataFacade().getDatabase().loadAllDatabaseNames();
Expand Down Expand Up @@ -169,14 +170,12 @@ private Map<String, Collection<ShardingSphereSchema>> loadSchemas(final Collecti
* @param internalLoadMetaData internal load meta data
* @param switchingResource switching resource
* @param originalMetaDataContexts original meta data contexts
* @param instanceContext compute node instance context
* @return meta data contexts
* @throws SQLException SQL exception
*/
public MetaDataContexts createBySwitchResource(final String databaseName, final boolean internalLoadMetaData, final SwitchingResource switchingResource,
final MetaDataContexts originalMetaDataContexts, final ComputeNodeInstanceContext instanceContext) throws SQLException {
ShardingSphereDatabase changedDatabase = createChangedDatabase(
databaseName, internalLoadMetaData, switchingResource, null, originalMetaDataContexts, instanceContext);
final MetaDataContexts originalMetaDataContexts) throws SQLException {
ShardingSphereDatabase changedDatabase = createChangedDatabase(databaseName, internalLoadMetaData, switchingResource, null, originalMetaDataContexts);
ConfigurationProperties props = originalMetaDataContexts.getMetaData().getProps();
ShardingSphereMetaData clonedMetaData = cloneMetaData(originalMetaDataContexts.getMetaData(), changedDatabase);
RuleMetaData changedGlobalMetaData = new RuleMetaData(
Expand All @@ -193,14 +192,12 @@ public MetaDataContexts createBySwitchResource(final String databaseName, final
* @param internalLoadMetaData internal load meta data
* @param ruleConfigs rule configs
* @param originalMetaDataContexts original meta data contexts
* @param instanceContext compute node instance context
* @return meta data contexts
* @throws SQLException SQL exception
*/
public MetaDataContexts createByAlterRule(final String databaseName, final boolean internalLoadMetaData, final Collection<RuleConfiguration> ruleConfigs,
final MetaDataContexts originalMetaDataContexts, final ComputeNodeInstanceContext instanceContext) throws SQLException {
ShardingSphereDatabase changedDatabase = createChangedDatabase(
databaseName, internalLoadMetaData, null, ruleConfigs, originalMetaDataContexts, instanceContext);
final MetaDataContexts originalMetaDataContexts) throws SQLException {
ShardingSphereDatabase changedDatabase = createChangedDatabase(databaseName, internalLoadMetaData, null, ruleConfigs, originalMetaDataContexts);
ShardingSphereMetaData clonedMetaData = cloneMetaData(originalMetaDataContexts.getMetaData(), changedDatabase);
ConfigurationProperties props = originalMetaDataContexts.getMetaData().getProps();
RuleMetaData changedGlobalMetaData = new RuleMetaData(
Expand All @@ -225,24 +222,22 @@ private ShardingSphereMetaData cloneMetaData(final ShardingSphereMetaData origin
* @param switchingResource switching resource
* @param ruleConfigs rule configurations
* @param originalMetaDataContext original meta data contexts
* @param instanceContext compute node instance context
* @return changed database
* @throws SQLException SQL exception
*/
public ShardingSphereDatabase createChangedDatabase(final String databaseName, final boolean internalLoadMetaData,
final SwitchingResource switchingResource, final Collection<RuleConfiguration> ruleConfigs,
final MetaDataContexts originalMetaDataContext, final ComputeNodeInstanceContext instanceContext) throws SQLException {
public ShardingSphereDatabase createChangedDatabase(final String databaseName, final boolean internalLoadMetaData, final SwitchingResource switchingResource,
final Collection<RuleConfiguration> ruleConfigs, final MetaDataContexts originalMetaDataContext) throws SQLException {
ResourceMetaData effectiveResourceMetaData = getEffectiveResourceMetaData(originalMetaDataContext.getMetaData().getDatabase(databaseName), switchingResource);
Collection<RuleConfiguration> toBeCreatedRuleConfigs = null == ruleConfigs
? originalMetaDataContext.getMetaData().getDatabase(databaseName).getRuleMetaData().getConfigurations()
: ruleConfigs;
DatabaseConfiguration toBeCreatedDatabaseConfig = getDatabaseConfiguration(effectiveResourceMetaData, switchingResource, toBeCreatedRuleConfigs);
return createChangedDatabase(originalMetaDataContext.getMetaData().getDatabase(databaseName).getName(), internalLoadMetaData,
toBeCreatedDatabaseConfig, originalMetaDataContext.getMetaData().getProps(), instanceContext);
toBeCreatedDatabaseConfig, originalMetaDataContext.getMetaData().getProps());
}

private ShardingSphereDatabase createChangedDatabase(final String databaseName, final boolean internalLoadMetaData, final DatabaseConfiguration databaseConfig,
final ConfigurationProperties props, final ComputeNodeInstanceContext instanceContext) throws SQLException {
final ConfigurationProperties props) throws SQLException {
DatabaseType protocolType = DatabaseTypeEngine.getProtocolType(databaseConfig, props);
return internalLoadMetaData
? ShardingSphereDatabase.create(databaseName, protocolType, databaseConfig, instanceContext, persistService.getDatabaseMetaDataFacade().getSchema().load(databaseName))
Expand Down
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(new MetaDataContextsFactory(metaDataPersistService).createByAlterRule(databaseName, false, ruleConfigurations, metaDataContexts, computeNodeInstanceContext));
metaDataContexts.update(new MetaDataContextsFactory(metaDataPersistService, computeNodeInstanceContext).createByAlterRule(databaseName, false, ruleConfigurations, metaDataContexts));
}

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 = new MetaDataContextsFactory(metaDataPersistService).createBySwitchResource(
databaseName, true, switchingResource, metaDataContexts, computeNodeInstanceContext);
MetaDataContexts reloadMetaDataContexts = new MetaDataContextsFactory(metaDataPersistService, computeNodeInstanceContext).createBySwitchResource(
databaseName, true, switchingResource, metaDataContexts);
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 = new MetaDataContextsFactory(metaDataPersistService).create(createContextManagerBuilderParameter(), computeNodeInstanceContext);
MetaDataContexts actual = new MetaDataContextsFactory(metaDataPersistService, computeNodeInstanceContext).create(createContextManagerBuilderParameter());
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 = new MetaDataContextsFactory(metaDataPersistService).create(createContextManagerBuilderParameter(), mock(ComputeNodeInstanceContext.class, RETURNS_DEEP_STUBS));
MetaDataContexts actual = new MetaDataContextsFactory(metaDataPersistService, mock(ComputeNodeInstanceContext.class, RETURNS_DEEP_STUBS)).create(createContextManagerBuilderParameter());
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,7 +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);
MetaDataContexts metaDataContexts = new MetaDataContextsFactory(new MetaDataPersistService(repository)).create(param, computeNodeInstanceContext);
MetaDataContexts metaDataContexts = new MetaDataContextsFactory(new MetaDataPersistService(repository), computeNodeInstanceContext).create(param);
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 = new MetaDataContextsFactory(metaDataPersistService).createBySwitchResource(
databaseName, false, switchingResource, originalMetaDataContexts, metaDataContextManager.getComputeNodeInstanceContext());
MetaDataContexts reloadMetaDataContexts = new MetaDataContextsFactory(metaDataPersistService, metaDataContextManager.getComputeNodeInstanceContext()).createBySwitchResource(
databaseName, false, switchingResource, originalMetaDataContexts);
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 = new MetaDataContextsFactory(metaDataPersistService).createBySwitchResource(
databaseName, false, switchingResource, originalMetaDataContexts, metaDataContextManager.getComputeNodeInstanceContext());
MetaDataContexts reloadMetaDataContexts = new MetaDataContextsFactory(metaDataPersistService, metaDataContextManager.getComputeNodeInstanceContext()).createBySwitchResource(
databaseName, false, switchingResource, originalMetaDataContexts);
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 = new MetaDataContextsFactory(metaDataPersistService).createBySwitchResource(
databaseName, false, switchingResource, originalMetaDataContexts, metaDataContextManager.getComputeNodeInstanceContext());
MetaDataContexts reloadMetaDataContexts = new MetaDataContextsFactory(metaDataPersistService, metaDataContextManager.getComputeNodeInstanceContext()).createBySwitchResource(
databaseName, false, switchingResource, originalMetaDataContexts);
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 @@ -47,7 +47,7 @@ public ContextManager build(final ContextManagerBuilderParameter param, final Ev
StandalonePersistRepository.class, null == repositoryConfig ? null : repositoryConfig.getType(), null == repositoryConfig ? new Properties() : repositoryConfig.getProps());
ComputeNodeInstanceContext computeNodeInstanceContext = new ComputeNodeInstanceContext(new ComputeNodeInstance(param.getInstanceMetaData()), param.getModeConfiguration(), eventBusContext);
computeNodeInstanceContext.init(new StandaloneWorkerIdGenerator(), new StandaloneLockContext());
MetaDataContexts metaDataContexts = new MetaDataContextsFactory(new MetaDataPersistService(repository)).create(param, computeNodeInstanceContext);
MetaDataContexts metaDataContexts = new MetaDataContextsFactory(new MetaDataPersistService(repository), computeNodeInstanceContext).create(param);
return new ContextManager(metaDataContexts, computeNodeInstanceContext, repository);
}

Expand Down
Loading

0 comments on commit a2fdcef

Please sign in to comment.