com.taosdata.jdbc.ws.loadbalance

## Class RebalanceManager

- java.lang.Object
- - com.taosdata.jdbc.ws.loadbalance.RebalanceManager

    public class RebalanceManager
    extends Object

Rebalance Manager for TDengine cluster endpoints Manages endpoint connection count, health status, and automatic load rebalancing logic Implements singleton pattern with static inner class (lazy loading + thread-safe initialization) This class is responsible for cluster registration, endpoint state maintenance, and rebalancing trigger

Version:
  1.0

### Method Summary

All Methods Static Methods Instance Methods Concrete Methods
`void` `addBgHealthCheckInstance()`
`void` `clearAllForTest()`
`void` `connected(List<Endpoint> endpoints, int index)` Mark the specified endpoint as online when connection is successful
`void` `decrementConnectionCount(Endpoint endpoint)` Decrement the connection count of the specified endpoint Called when a connection is closed to the endpoint
`void` `disconnected(ConnectionParam original, int index, InFlightRequest inFlightRequest)` Handle endpoint disconnection event 1.
`void` `disconnectedBySelf(ConnectionParam original, int index)` Handle self-initiated disconnection (only decrement connection count)
`void` `endpointUp(ConnectionParam param, Endpoint endpoint)` Mark endpoint as online and trigger rebalancing if conditions are met
`int` `getBgHealthCheckInstanceCount()`
`int[]` `getConnectCountsAsc(List<Endpoint> endpoints)` Get endpoint indexes sorted by connection count in ascending order Used for load balancing to select the least loaded endpoint
`EndpointInfo` `getEndpointInfo(Endpoint endpoint)` Get the metadata (connection count + health status) of the specified endpoint
`static RebalanceManager` `getInstance()` Get the singleton instance of RebalanceManager This is the only way to access the manager's functionality
`int` `getMinConnectionEndpointIndex(ConnectionParam param)` Get the index of the endpoint with the minimum connection count Used to select the least loaded endpoint for rebalancing
`boolean` `handleRebalancing(ConnectionParam param, Endpoint currentEndpoint)` Handle rebalancing logic for the current connection 1.
`void` `incrementConnectionCount(Endpoint endpoint)` Increment the connection count of the specified endpoint Called when a new connection is established to the endpoint
`boolean` `isRebalancing()` Check if there is any cluster in global rebalancing state
`boolean` `isRebalancing(Endpoint endpoint)` Check if the cluster of the specified endpoint is in rebalancing state
`void` `newCluster(List<Endpoint> endpoints)` Register a new cluster and initialize endpoint metadata Idempotent operation: no effect if the cluster is already registered
`void` `removeBgHealthCheck(BgHealthCheck bgHealthCheck)`
`void` `removeBgHealthCheckInstance()`
`void` `startBackgroundHealthCheck(ConnectionParam original, int index, InFlightRequest inFlightRequest)` Start background health check for offline endpoints Monitors endpoint recovery status and updates state when online

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

### Method Detail

#### getInstance

    public static RebalanceManager getInstance()

Get the singleton instance of RebalanceManager This is the only way to access the manager's functionality

Returns:
  Singleton instance of RebalanceManager

#### isRebalancing

    public boolean isRebalancing(Endpoint endpoint)

Check if the cluster of the specified endpoint is in rebalancing state

Parameters:
  `endpoint` - Target endpoint to check
Returns:
  true if the cluster is rebalancing, false otherwise

#### isRebalancing

    public boolean isRebalancing()

Check if there is any cluster in global rebalancing state

Returns:
  true if global rebalancing is ongoing, false otherwise

#### incrementConnectionCount

    public void incrementConnectionCount(Endpoint endpoint)

Increment the connection count of the specified endpoint Called when a new connection is established to the endpoint

Parameters:
  `endpoint` - Target endpoint

#### decrementConnectionCount

    public void decrementConnectionCount(Endpoint endpoint)

Decrement the connection count of the specified endpoint Called when a connection is closed to the endpoint

Parameters:
  `endpoint` - Target endpoint

#### connected

    public void connected(List<Endpoint> endpoints,
                          int index)

Mark the specified endpoint as online when connection is successful

Parameters:
  `endpoints` - List of cluster endpoints
  `index` - Index of the connected endpoint in the list

#### disconnected

    public void disconnected(ConnectionParam original,
                             int index,
                             InFlightRequest inFlightRequest)

Handle endpoint disconnection event 1. Decrement connection count via disconnectedBySelf 2. Start background health check if auto-connect is enabled and endpoint is first marked as offline

Parameters:
  `original` - Original connection parameters
  `index` - Index of the disconnected endpoint in the list
  `inFlightRequest` - In-flight request container for health check response handling

#### disconnectedBySelf

    public void disconnectedBySelf(ConnectionParam original,
                                   int index)

Handle self-initiated disconnection (only decrement connection count)

Parameters:
  `original` - Original connection parameters
  `index` - Index of the disconnected endpoint in the list

#### newCluster

    public void newCluster(List<Endpoint> endpoints)

Register a new cluster and initialize endpoint metadata Idempotent operation: no effect if the cluster is already registered

Parameters:
  `endpoints` - List of endpoints in the new cluster

#### endpointUp

    public void endpointUp(ConnectionParam param,
                           Endpoint endpoint)

Mark endpoint as online and trigger rebalancing if conditions are met

Parameters:
  `param` - Connection parameters (contains rebalance thresholds)
  `endpoint` - The endpoint that recovered to online state

#### getEndpointInfo

    public EndpointInfo getEndpointInfo(Endpoint endpoint)

Get the metadata (connection count + health status) of the specified endpoint

Parameters:
  `endpoint` - Target endpoint
Returns:
  EndpointInfo object containing endpoint metadata

#### getConnectCountsAsc

    public int[] getConnectCountsAsc(List<Endpoint> endpoints)

Get endpoint indexes sorted by connection count in ascending order Used for load balancing to select the least loaded endpoint

Parameters:
  `endpoints` - List of cluster endpoints
Returns:
  Sorted array of endpoint indexes (ascending by connection count)

#### startBackgroundHealthCheck

    public void startBackgroundHealthCheck(ConnectionParam original,
                                           int index,
                                           InFlightRequest inFlightRequest)

Start background health check for offline endpoints Monitors endpoint recovery status and updates state when online

Parameters:
  `original` - Original connection parameters
  `index` - Index of the offline endpoint in the list
  `inFlightRequest` - In-flight request container for health check responses

#### handleRebalancing

    public boolean handleRebalancing(ConnectionParam param,
                                     Endpoint currentEndpoint)

Handle rebalancing logic for the current connection 1. Update cluster/global rebalancing state if conditions are no longer met 2. Determine if the current connection needs to be rebalanced to a less loaded endpoint

Parameters:
  `param` - Connection parameters with rebalance thresholds
  `currentEndpoint` - The endpoint currently connected to
Returns:
  true if rebalancing is required for this connection, false otherwise

#### getMinConnectionEndpointIndex

    public int getMinConnectionEndpointIndex(ConnectionParam param)

Get the index of the endpoint with the minimum connection count Used to select the least loaded endpoint for rebalancing

Parameters:
  `param` - Connection parameters with endpoint list
Returns:
  Index of the endpoint with the least connections (-1 if endpoint list is empty)

#### addBgHealthCheckInstance

    public void addBgHealthCheckInstance()

#### removeBgHealthCheckInstance

    public void removeBgHealthCheckInstance()

#### getBgHealthCheckInstanceCount

    public int getBgHealthCheckInstanceCount()

#### removeBgHealthCheck

    public void removeBgHealthCheck(BgHealthCheck bgHealthCheck)

#### clearAllForTest

    public void clearAllForTest()