Implementation:Apache Shardingsphere StatisticsChangedHandler Handle
| Knowledge Sources | |
|---|---|
| Domains | Cluster, Statistics |
| Last Updated | 2026-02-10 00:00 GMT |
Overview
StatisticsChangedHandler hierarchically processes statistics change events at database, schema, table, and row levels from cluster registry events.
Description
StatisticsChangedHandler implements GlobalDataChangedEventHandler and subscribes to StatisticsDatabaseNodePath for ADDED, UPDATED, and DELETED events. The handle method performs a cascading path resolution: it first attempts to match a database-level path, then schema-level, then table-level, and finally row-level. At each level, it delegates to the corresponding method on StatisticsManager -- adding or dropping database/schema/table statistics on ADDED/UPDATED vs DELETED events. For row-level changes, it deserializes YamlRowStatistics from the event value on ADDED/UPDATED events and calls alterRowStatistics, or calls deleteRowStatistics on DELETED events.
Usage
This handler is auto-loaded via SPI in cluster mode. It ensures that statistics changes persisted by any cluster node are propagated to all other nodes' in-memory StatisticsManager, keeping the statistics view consistent across the cluster.
Code Reference
Source Location
- Repository: Apache_Shardingsphere
- File: StatisticsChangedHandler.java
- Lines: 1-137
Signature
public final class StatisticsChangedHandler implements GlobalDataChangedEventHandler {
public NodePath getSubscribedNodePath()
public Collection<Type> getSubscribedTypes()
public void handle(final ContextManager contextManager, final DataChangedEvent event)
}
Import
import org.apache.shardingsphere.mode.manager.cluster.dispatch.handler.global.statistics.StatisticsChangedHandler;
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| contextManager | ContextManager | Yes | Provides access to StatisticsManager via MetaDataContextManager |
| event | DataChangedEvent | Yes | Contains the statistics path key, value (YAML for row data), and event type |
Outputs
| Name | Type | Description |
|---|---|---|
| void | void | Side effects: adds/drops/alters statistics at the appropriate hierarchy level |
Usage Examples
// The handler automatically processes events at different levels:
// Database level: adds or drops DatabaseStatistics
// Schema level: adds or drops SchemaStatistics
// Table level: adds or drops TableStatistics
// Row level: alters or deletes individual RowStatistics from YamlRowStatistics