Implementation:Apache Shardingsphere MetaDataContextsFactory Create
| Knowledge Sources | |
|---|---|
| Domains | Cluster_Mode, Distributed_Coordination |
| Last Updated | 2026-02-10 00:00 GMT |
Overview
Concrete tool for creating the runtime metadata context by choosing between register-center-based and local-configuration-based initialization strategies, provided by the ShardingSphere mode module.
Description
MetaDataContextsFactory is a final class annotated with @RequiredArgsConstructor that holds a MetaDataPersistFacade and a ComputeNodeInstanceContext. Its primary method create(ContextManagerBuilderParameter) determines whether databases have already been registered in the distributed repository and delegates to the appropriate initialization factory:
- If the repository contains registered database names, it creates a RegisterCenterMetaDataContextsInitFactory to load metadata from the register center.
- If the repository is empty, it creates a LocalConfigurationMetaDataContextsInitFactory to initialize from the local bootstrap configuration.
The detection is performed by containsRegisteredDatabases(), which checks whether DatabaseMetaDataPersistService.loadAllDatabaseNames() returns a non-empty collection.
The factory also provides createBySwitchResource() and createByAlterRule() methods for dynamic reconfiguration. These methods create a changed ShardingSphereDatabase by merging the original metadata with the new resources or rules, cloning the overall metadata, rebuilding global rules, and producing a fresh MetaDataContexts object.
Usage
Constructed and invoked within ClusterContextManagerBuilder.build():
MetaDataContexts metaDataContexts = new MetaDataContextsFactory(
new MetaDataPersistFacade(repository), computeNodeInstanceContext).create(param);
Also used by the metadata context manager for runtime reconfiguration operations.
Code Reference
Source Location
- Repository: Apache ShardingSphere
- File:
mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/factory/MetaDataContextsFactory.java - Lines: 63-81 (class declaration and create method)
Signature
@RequiredArgsConstructor
public final class MetaDataContextsFactory {
private final MetaDataPersistFacade persistFacade;
private final ComputeNodeInstanceContext instanceContext;
public MetaDataContexts create(final ContextManagerBuilderParameter param) throws SQLException {
MetaDataContextsInitFactory initFactory = containsRegisteredDatabases(persistFacade.getRepository())
? new RegisterCenterMetaDataContextsInitFactory(persistFacade.getRepository(), instanceContext)
: new LocalConfigurationMetaDataContextsInitFactory(persistFacade.getRepository(), instanceContext, param.getProps());
return initFactory.create(param);
}
private static boolean containsRegisteredDatabases(final PersistRepository repository) {
return !new DatabaseMetaDataPersistService(repository).loadAllDatabaseNames().isEmpty();
}
}
Import
import org.apache.shardingsphere.mode.metadata.factory.MetaDataContextsFactory;
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| persistFacade | MetaDataPersistFacade | Yes | Facade providing access to all metadata persistence services (constructor parameter) |
| instanceContext | ComputeNodeInstanceContext | Yes | Compute node instance context for the current node (constructor parameter) |
| param | ContextManagerBuilderParameter | Yes | Bootstrap parameter containing database configs, global rules, properties, and instance metadata |
Outputs
| Name | Type | Description |
|---|---|---|
| return | MetaDataContexts | Fully assembled metadata context containing ShardingSphereMetaData and ShardingSphereStatistics |
Exceptions
| Type | Condition |
|---|---|
| SQLException | Thrown when database metadata loading or creation encounters errors |
Additional Methods
createBySwitchResource
public MetaDataContexts createBySwitchResource(final String databaseName, final boolean isLoadSchemasFromRegisterCenter,
final SwitchingResource switchingResource, final MetaDataContexts originalMetaDataContexts) throws SQLException
Creates a new MetaDataContexts by replacing data sources in a specified database while preserving other databases and global rules.
createByAlterRule
public MetaDataContexts createByAlterRule(final String databaseName, final boolean isLoadSchemasFromRegisterCenter,
final Collection<RuleConfiguration> ruleConfigs, final MetaDataContexts originalMetaDataContexts) throws SQLException
Creates a new MetaDataContexts by replacing rule configurations in a specified database while preserving other databases and global rules.
Usage Examples
// During cluster-mode bootstrap
MetaDataPersistFacade persistFacade = new MetaDataPersistFacade(repository);
MetaDataContextsFactory factory = new MetaDataContextsFactory(persistFacade, computeNodeInstanceContext);
// Initial creation -- will load from register center if databases exist, else from local config
MetaDataContexts metaDataContexts = factory.create(param);
// Dynamic reconfiguration -- switching data sources
MetaDataContexts newContexts = factory.createBySwitchResource(
"my_database", true, switchingResource, currentMetaDataContexts);
// Dynamic reconfiguration -- altering rules
MetaDataContexts alteredContexts = factory.createByAlterRule(
"my_database", true, newRuleConfigs, currentMetaDataContexts);