Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Apache Shardingsphere ShadowRuleBuilder Build

From Leeroopedia


Knowledge Sources
Domains Configuration_Management, Shadow_Testing
Last Updated 2026-02-10 00:00 GMT

Overview

Concrete SPI-based rule builder for constructing a ShadowRule from validated configuration, provided by the ShardingSphere shadow module.

Description

ShadowRuleBuilder is a final class that implements DatabaseRuleBuilder<ShadowRuleConfiguration>. It serves as the SPI entry point for the shadow rule module, discovered by the framework when a ShadowRuleConfiguration is present among the database rule configurations.

The build() method (lines 37-40) is minimal by design. It receives the full set of parameters required by the DatabaseRuleBuilder interface -- rule configuration, database name, protocol type, resource metadata, already-built rules, and compute node instance context -- but only uses ruleConfig. The method delegates entirely to the ShadowRule constructor:

return new ShadowRule(ruleConfig);

This delegation pattern is consistent across many ShardingSphere rule builders: the builder's role is to be discoverable via SPI and to provide ordering semantics, while the actual construction logic resides in the rule class itself.

The class also implements:

  • getOrder(): Returns ShadowOrder.ORDER, establishing the shadow rule's position in the rule building sequence.
  • getTypeClass(): Returns ShadowRuleConfiguration.class, enabling the framework to match this builder to shadow rule configurations.

Usage

This builder is not typically invoked directly. The framework discovers it via SPI when processing rule configurations during database initialization. When a ShadowRuleConfiguration is present, the framework calls build() to produce the ShadowRule instance.

Code Reference

Source Location

  • Repository: Apache ShardingSphere
  • File: features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/rule/builder/ShadowRuleBuilder.java
  • Lines: 37-40

Signature

@Override
public ShadowRule build(final ShadowRuleConfiguration ruleConfig, final String databaseName, final DatabaseType protocolType,
                        final ResourceMetaData resourceMetaData, final Collection<ShardingSphereRule> builtRules,
                        final ComputeNodeInstanceContext computeNodeInstanceContext) {
    return new ShadowRule(ruleConfig);
}

Import

import org.apache.shardingsphere.shadow.rule.builder.ShadowRuleBuilder;

I/O Contract

Inputs

Name Type Required Description
ruleConfig ShadowRuleConfiguration Yes Validated shadow rule configuration containing data sources, tables, algorithms, and default algorithm name
databaseName String Yes Name of the database (not used by this builder, passed through by framework)
protocolType DatabaseType Yes Database protocol type (not used by this builder)
resourceMetaData ResourceMetaData Yes Resource metadata for the database (not used by this builder)
builtRules Collection<ShardingSphereRule> Yes Previously built rules in order (not used by this builder)
computeNodeInstanceContext ComputeNodeInstanceContext Yes Compute node context (not used by this builder)

Outputs

Name Type Description
return ShadowRule Fully initialized shadow rule object with materialized algorithms, data source rules, and table rules

Usage Examples

// Typically invoked by the framework, but can be called directly:
ShadowRuleBuilder builder = new ShadowRuleBuilder();
ShadowRule shadowRule = builder.build(
    shadowRuleConfig,
    "my_database",
    protocolType,
    resourceMetaData,
    builtRules,
    computeNodeInstanceContext
);

// The builder simply delegates to: new ShadowRule(shadowRuleConfig)
// The resulting ShadowRule contains:
//   - Instantiated shadow algorithm objects
//   - Data source rules mapping logical names to production/shadow pairs
//   - Table rules with classified hint and column shadow algorithms

Related Pages

Implements Principle

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment