Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor MetaDataContexts #34353

Merged
merged 3 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
import java.sql.SQLException;
import java.util.Collections;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;

/**
* Context manager.
Expand All @@ -53,7 +52,7 @@
@Slf4j
public final class ContextManager implements AutoCloseable {

private final AtomicReference<MetaDataContexts> metaDataContexts;
private final MetaDataContexts metaDataContexts;

private final ComputeNodeInstanceContext computeNodeInstanceContext;

Expand All @@ -68,9 +67,9 @@ public final class ContextManager implements AutoCloseable {
private final PersistCoordinatorFacade persistCoordinatorFacade;

public ContextManager(final MetaDataContexts metaDataContexts, final ComputeNodeInstanceContext computeNodeInstanceContext, final PersistRepository repository) {
this.metaDataContexts = new AtomicReference<>(metaDataContexts);
this.metaDataContexts = metaDataContexts;
this.computeNodeInstanceContext = computeNodeInstanceContext;
metaDataContextManager = new MetaDataContextManager(this.metaDataContexts, computeNodeInstanceContext, repository);
metaDataContextManager = new MetaDataContextManager(metaDataContexts, computeNodeInstanceContext, repository);
persistServiceFacade = new PersistServiceFacade(repository, computeNodeInstanceContext.getModeConfiguration(), metaDataContextManager);
stateContext = new ClusterStateContext(persistServiceFacade.getStatePersistService().load());
executorEngine = ExecutorEngine.createExecutorEngineWithSize(metaDataContexts.getMetaData().getProps().<Integer>getValue(ConfigurationPropertyKey.KERNEL_EXECUTOR_SIZE));
Expand All @@ -80,24 +79,6 @@ public ContextManager(final MetaDataContexts metaDataContexts, final ComputeNode
persistCoordinatorFacade = new PersistCoordinatorFacade(repository, computeNodeInstanceContext.getModeConfiguration());
}

/**
* Get meta data contexts.
*
* @return meta data contexts
*/
public MetaDataContexts getMetaDataContexts() {
return metaDataContexts.get();
}

/**
* Renew meta data contexts.
*
* @param metaDataContexts meta data contexts
*/
public void renewMetaDataContexts(final MetaDataContexts metaDataContexts) {
this.metaDataContexts.set(metaDataContexts);
}

/**
* Get database.
*
Expand Down Expand Up @@ -146,7 +127,7 @@ public void reloadSchema(final ShardingSphereDatabase database, final String sch
private ShardingSphereSchema loadSchema(final ShardingSphereDatabase database, final String schemaName, final String dataSourceName) throws SQLException {
database.reloadRules();
GenericSchemaBuilderMaterial material = new GenericSchemaBuilderMaterial(Collections.singletonMap(dataSourceName, database.getResourceMetaData().getStorageUnits().get(dataSourceName)),
database.getRuleMetaData().getRules(), metaDataContexts.get().getMetaData().getProps(), schemaName);
database.getRuleMetaData().getRules(), metaDataContexts.getMetaData().getProps(), schemaName);
ShardingSphereSchema result = GenericSchemaBuilder.build(database.getProtocolType(), material).get(schemaName);
persistServiceFacade.getMetaDataPersistService().getDatabaseMetaDataFacade().getView().load(database.getName(), schemaName).forEach(result::putView);
return result;
Expand All @@ -161,7 +142,7 @@ private ShardingSphereSchema loadSchema(final ShardingSphereDatabase database, f
*/
public void reloadTable(final ShardingSphereDatabase database, final String schemaName, final String tableName) {
GenericSchemaBuilderMaterial material = new GenericSchemaBuilderMaterial(
database.getResourceMetaData().getStorageUnits(), database.getRuleMetaData().getRules(), metaDataContexts.get().getMetaData().getProps(), schemaName);
database.getResourceMetaData().getStorageUnits(), database.getRuleMetaData().getRules(), metaDataContexts.getMetaData().getProps(), schemaName);
try {
persistTable(database, schemaName, tableName, material);
} catch (final SQLException ex) {
Expand All @@ -180,7 +161,7 @@ public void reloadTable(final ShardingSphereDatabase database, final String sche
public void reloadTable(final ShardingSphereDatabase database, final String schemaName, final String dataSourceName, final String tableName) {
StorageUnit storageUnit = database.getResourceMetaData().getStorageUnits().get(dataSourceName);
GenericSchemaBuilderMaterial material = new GenericSchemaBuilderMaterial(
Collections.singletonMap(dataSourceName, storageUnit), database.getRuleMetaData().getRules(), metaDataContexts.get().getMetaData().getProps(), schemaName);
Collections.singletonMap(dataSourceName, storageUnit), database.getRuleMetaData().getRules(), metaDataContexts.getMetaData().getProps(), schemaName);
try {
persistTable(database, schemaName, tableName, material);
} catch (final SQLException ex) {
Expand All @@ -199,7 +180,7 @@ private void persistTable(final ShardingSphereDatabase database, final String sc
* @return pre-selected database name
*/
public String getPreSelectedDatabaseName() {
return InstanceType.JDBC == computeNodeInstanceContext.getInstance().getMetaData().getType() ? metaDataContexts.get().getMetaData().getAllDatabases().iterator().next().getName() : null;
return InstanceType.JDBC == computeNodeInstanceContext.getInstance().getMetaData().getType() ? metaDataContexts.getMetaData().getAllDatabases().iterator().next().getName() : null;
}

@Override
Expand All @@ -208,7 +189,7 @@ public void close() {
each.onDestroyed(this);
}
executorEngine.close();
metaDataContexts.get().getMetaData().close();
metaDataContexts.getMetaData().close();
persistServiceFacade.close(computeNodeInstanceContext.getInstance());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
import java.sql.SQLException;
import java.util.Collection;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;

/**
* Meta data context manager..
Expand All @@ -51,7 +50,7 @@
@Slf4j
public class MetaDataContextManager {

private final AtomicReference<MetaDataContexts> metaDataContexts;
private final MetaDataContexts metaDataContexts;

private final ComputeNodeInstanceContext computeNodeInstanceContext;

Expand All @@ -71,7 +70,7 @@ public class MetaDataContextManager {

private final GlobalConfigurationManager globalConfigurationManager;

public MetaDataContextManager(final AtomicReference<MetaDataContexts> metaDataContexts, final ComputeNodeInstanceContext computeNodeInstanceContext, final PersistRepository repository) {
public MetaDataContextManager(final MetaDataContexts metaDataContexts, final ComputeNodeInstanceContext computeNodeInstanceContext, final PersistRepository repository) {
this.metaDataContexts = metaDataContexts;
this.computeNodeInstanceContext = computeNodeInstanceContext;
resourceSwitchManager = new ResourceSwitchManager();
Expand Down Expand Up @@ -101,7 +100,8 @@ public void dropSchemas(final String databaseName, final ShardingSphereDatabase
* @param metaDataContexts meta data contexts
*/
public void renewMetaDataContexts(final MetaDataContexts metaDataContexts) {
this.metaDataContexts.set(metaDataContexts);
this.metaDataContexts.setMetaData(metaDataContexts.getMetaData());
this.metaDataContexts.setStatistics(metaDataContexts.getStatistics());
}

/**
Expand All @@ -112,8 +112,9 @@ public void renewMetaDataContexts(final MetaDataContexts metaDataContexts) {
public void forceRefreshDatabaseMetaData(final ShardingSphereDatabase database) {
try {
MetaDataContexts reloadedMetaDataContexts = createMetaDataContexts(database);
metaDataContexts.set(reloadedMetaDataContexts);
metaDataContexts.get().getMetaData().getDatabase(database.getName()).getAllSchemas()
metaDataContexts.setMetaData(reloadedMetaDataContexts.getMetaData());
metaDataContexts.setStatistics(reloadedMetaDataContexts.getStatistics());
metaDataContexts.getMetaData().getDatabase(database.getName()).getAllSchemas()
.forEach(each -> {
if (each.isEmpty()) {
metaDataPersistService.getDatabaseMetaDataFacade().getSchema().add(database.getName(), each.getName());
Expand All @@ -134,8 +135,9 @@ public void refreshTableMetaData(final ShardingSphereDatabase database) {
try {
MetaDataContexts reloadedMetaDataContexts = createMetaDataContexts(database);
dropSchemas(database.getName(), reloadedMetaDataContexts.getMetaData().getDatabase(database.getName()), database);
metaDataContexts.set(reloadedMetaDataContexts);
metaDataContexts.get().getMetaData().getDatabase(database.getName()).getAllSchemas()
metaDataContexts.setMetaData(reloadedMetaDataContexts.getMetaData());
metaDataContexts.setStatistics(reloadedMetaDataContexts.getStatistics());
metaDataContexts.getMetaData().getDatabase(database.getName()).getAllSchemas()
.forEach(each -> metaDataPersistService.getDatabaseMetaDataFacade().getSchema().alterByRefresh(database.getName(), each));
} catch (final SQLException ex) {
log.error("Refresh table meta data: {} failed", database.getName(), ex);
Expand All @@ -147,13 +149,13 @@ private MetaDataContexts createMetaDataContexts(final ShardingSphereDatabase dat
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.get(), metaDataPersistService, computeNodeInstanceContext);
metaDataContexts.get().getMetaData().putDatabase(changedDatabase);
.createChangedDatabase(database.getName(), false, switchingResource, ruleConfigs, metaDataContexts, metaDataPersistService, computeNodeInstanceContext);
metaDataContexts.getMetaData().putDatabase(changedDatabase);
ConfigurationProperties props = new ConfigurationProperties(metaDataPersistService.getPropsService().load());
Collection<RuleConfiguration> globalRuleConfigs = metaDataPersistService.getGlobalRuleService().load();
RuleMetaData changedGlobalMetaData = new RuleMetaData(GlobalRulesBuilder.buildRules(globalRuleConfigs, metaDataContexts.get().getMetaData().getAllDatabases(), props));
RuleMetaData changedGlobalMetaData = new RuleMetaData(GlobalRulesBuilder.buildRules(globalRuleConfigs, metaDataContexts.getMetaData().getAllDatabases(), props));
ShardingSphereMetaData metaData = new ShardingSphereMetaData(
metaDataContexts.get().getMetaData().getAllDatabases(), metaDataContexts.get().getMetaData().getGlobalResourceMetaData(), changedGlobalMetaData, props);
metaDataContexts.getMetaData().getAllDatabases(), metaDataContexts.getMetaData().getGlobalResourceMetaData(), changedGlobalMetaData, props);
MetaDataContexts result = new MetaDataContexts(metaData, ShardingSphereStatisticsFactory.create(metaDataPersistService, metaData));
switchingResource.closeStaleDataSources();
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,58 @@

package org.apache.shardingsphere.mode.metadata;

import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
import org.apache.shardingsphere.infra.metadata.statistics.ShardingSphereStatistics;

import java.util.concurrent.atomic.AtomicReference;

/**
* Meta data contexts.
*/
@RequiredArgsConstructor
@Getter
public final class MetaDataContexts {

private final ShardingSphereMetaData metaData;
private final AtomicReference<ShardingSphereMetaData> metaData = new AtomicReference<>();

private final AtomicReference<ShardingSphereStatistics> statistics = new AtomicReference<>();

public MetaDataContexts(final ShardingSphereMetaData metaData, final ShardingSphereStatistics statistics) {
this.metaData.set(metaData);
this.statistics.set(statistics);
}

/**
* Get ShardingSphere meta data.
*
* @return got meta data
*/
public ShardingSphereMetaData getMetaData() {
return metaData.get();
}

/**
* Set ShardingSphere meta data.
*
* @param metaData meta data
*/
public void setMetaData(final ShardingSphereMetaData metaData) {
this.metaData.set(metaData);
}

/**
* Get ShardingSphere statistics.
*
* @return got meta data statistics
*/
public ShardingSphereStatistics getStatistics() {
return statistics.get();
}

private final ShardingSphereStatistics statistics;
/**
* Set ShardingSphere statistics.
*
* @param statistics statistics
*/
public void setStatistics(final ShardingSphereStatistics statistics) {
this.statistics.set(statistics);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,30 +26,29 @@
import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
import org.apache.shardingsphere.infra.rule.builder.database.DatabaseRulesBuilder;
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
import org.apache.shardingsphere.mode.metadata.persist.MetaDataPersistService;
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.spi.repository.PersistRepository;

import java.sql.SQLException;
import java.util.Collection;
import java.util.LinkedList;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;

/**
* Database rule configuration manager.
*/
public final class DatabaseRuleConfigurationManager {

private final AtomicReference<MetaDataContexts> metaDataContexts;
private final MetaDataContexts metaDataContexts;

private final ComputeNodeInstanceContext computeNodeInstanceContext;

private final MetaDataPersistService metaDataPersistService;

public DatabaseRuleConfigurationManager(final AtomicReference<MetaDataContexts> metaDataContexts, final ComputeNodeInstanceContext computeNodeInstanceContext,
public DatabaseRuleConfigurationManager(final MetaDataContexts metaDataContexts, final ComputeNodeInstanceContext computeNodeInstanceContext,
final PersistRepository repository) {
this.metaDataContexts = metaDataContexts;
this.computeNodeInstanceContext = computeNodeInstanceContext;
Expand All @@ -65,7 +64,7 @@ public DatabaseRuleConfigurationManager(final AtomicReference<MetaDataContexts>
*/
@SuppressWarnings({"unchecked", "rawtypes"})
public synchronized void alterRuleConfiguration(final String databaseName, final RuleConfiguration ruleConfig) throws SQLException {
ShardingSphereDatabase database = metaDataContexts.get().getMetaData().getDatabase(databaseName);
ShardingSphereDatabase database = metaDataContexts.getMetaData().getDatabase(databaseName);
Collection<ShardingSphereRule> rules = new LinkedList<>(database.getRuleMetaData().getRules());
Optional<ShardingSphereRule> toBeChangedRule = rules.stream().filter(each -> each.getConfiguration().getClass().equals(ruleConfig.getClass())).findFirst();
if (toBeChangedRule.isPresent() && toBeChangedRule.get() instanceof PartialRuleUpdateSupported && ((PartialRuleUpdateSupported) toBeChangedRule.get()).partialUpdate(ruleConfig)) {
Expand All @@ -87,7 +86,7 @@ public synchronized void alterRuleConfiguration(final String databaseName, final
*/
@SuppressWarnings({"unchecked", "rawtypes"})
public synchronized void dropRuleConfiguration(final String databaseName, final RuleConfiguration ruleConfig) throws SQLException {
ShardingSphereDatabase database = metaDataContexts.get().getMetaData().getDatabase(databaseName);
ShardingSphereDatabase database = metaDataContexts.getMetaData().getDatabase(databaseName);
Collection<ShardingSphereRule> rules = new LinkedList<>(database.getRuleMetaData().getRules());
Optional<ShardingSphereRule> toBeChangedRule = rules.stream().filter(each -> each.getConfiguration().getClass().equals(ruleConfig.getClass())).findFirst();
if (toBeChangedRule.isPresent() && toBeChangedRule.get() instanceof PartialRuleUpdateSupported && ((PartialRuleUpdateSupported) toBeChangedRule.get()).partialUpdate(ruleConfig)) {
Expand All @@ -104,8 +103,9 @@ public synchronized void dropRuleConfiguration(final String databaseName, final

private void refreshMetadata(final String databaseName, final Collection<RuleConfiguration> ruleConfigurations) throws SQLException {
MetaDataContexts reloadMetaDataContexts = MetaDataContextsFactory.createByAlterRule(databaseName, false,
ruleConfigurations, metaDataContexts.get(), metaDataPersistService, computeNodeInstanceContext);
metaDataContexts.set(reloadMetaDataContexts);
ruleConfigurations, metaDataContexts, metaDataPersistService, computeNodeInstanceContext);
metaDataContexts.setMetaData(reloadMetaDataContexts.getMetaData());
metaDataContexts.setStatistics(reloadMetaDataContexts.getStatistics());
}

private Collection<RuleConfiguration> getRuleConfigurations(final Collection<ShardingSphereRule> rules) {
Expand Down
Loading
Loading