Implementation:Apache Shardingsphere SessionConnectionReconnectListener Handle
| Knowledge Sources | |
|---|---|
| Domains | Cluster, ZooKeeper |
| Last Updated | 2026-02-10 00:00 GMT |
Overview
SessionConnectionReconnectListener handles ZooKeeper session reconnection by regenerating worker IDs and re-registering the compute node after connection loss.
Description
SessionConnectionReconnectListener implements Curator's ConnectionStateListener interface. When a ConnectionState.LOST event is received, it enters a retry loop calling reconnect until successful. The reconnect method blocks until the ZooKeeper client reconnects or times out. Upon successful reconnection, if the instance previously had a worker ID (not -1), it regenerates the worker ID via ComputeNodeInstanceContext.generateWorkerId. It then re-registers the instance as online using ClusterComputeNodePersistService.registerOnline. If the connection attempt fails, it sleeps for 5 seconds (configurable via RECONNECT_INTERVAL_SECONDS) before retrying. InterruptedException is handled by restoring the interrupt flag and exiting the retry loop.
Usage
This listener is registered with the Curator framework when initializing the ZooKeeper cluster repository. It ensures that after a ZooKeeper session loss (which causes ephemeral node deletion), the compute node re-establishes its presence in the cluster registry, maintaining proper worker ID assignment and online status.
Code Reference
Source Location
- Repository: Apache_Shardingsphere
- File: SessionConnectionReconnectListener.java
- Lines: 1-85
Signature
@Slf4j
public final class SessionConnectionReconnectListener implements ConnectionStateListener {
public SessionConnectionReconnectListener(final ComputeNodeInstanceContext computeNodeInstanceContext,
final ClusterPersistRepository repository)
public void stateChanged(final CuratorFramework client, final ConnectionState connectionState)
}
Import
import org.apache.shardingsphere.mode.repository.cluster.zookeeper.listener.SessionConnectionReconnectListener;
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| computeNodeInstanceContext | ComputeNodeInstanceContext | Yes | Context for the local compute node instance |
| repository | ClusterPersistRepository | Yes | Repository for creating ClusterComputeNodePersistService |
| client | CuratorFramework | Yes | Curator client passed in the stateChanged callback |
| connectionState | ConnectionState | Yes | Current connection state (only LOST triggers reconnection) |
Outputs
| Name | Type | Description |
|---|---|---|
| void | void | Side effects: regenerates worker ID and re-registers instance online upon reconnection |
Usage Examples
// Register the listener during ZooKeeper repository initialization
SessionConnectionReconnectListener listener = new SessionConnectionReconnectListener(
computeNodeInstanceContext, clusterPersistRepository);
curatorClient.getConnectionStateListenable().addListener(listener);
// On connection loss, the listener will automatically retry reconnection,
// regenerate worker ID if needed, and re-register the instance.