feat(mysql): expose pool cluster node state changes through the TypeORM logger
Feature Description
When using MySQL/MariaDB replication, mysql2 emits PoolCluster events when a node is temporarily excluded, recovers, or is removed. TypeORM does not forward these events to its logger, making it difficult to observe which replication node became unavailable and when it recovered through the application's existing logging setup.
The Solution
Register listeners when TypeORM creates the PoolCluster and forward offline and remove events at the warn level, and online events at the info level through the existing Logger.log method.
Include the node identifier and role. Track the first exclusion timestamp per node and include the recovery timestamp and elapsed exclusion time on recovery. Repeated failures should retain the initial timestamp, and recovery should only be reported after mysql2 emits online for a previously excluded node.
Users should be able to enable these messages with logging: ["warn", "info"], without changing connection retry behavior or adding a new logger API.
Considered Alternatives
An application can wrap mysql2.createPoolCluster in a Proxy and attach its own listeners at creation time. This works but requires application-side driver wrapping solely for logging. Accessing the cluster through the TypeORM driver after initialization is another workaround, but misses events during initialization.
Additional Context
With replication.restoreNodeTimeout > 0, an excluded node becomes eligible for another connection attempt after the timeout. Recovery should be logged on a successful connection, not on timeout expiry. With the default value of 0, nodes reaching the error threshold are removed instead, so removal should also be logged.
The elapsed time measures the observed exclusion interval, not the exact duration of the database outage.
Implementation: #12849.
Relevant Database Driver(s)
- mysql (including MariaDB through MysqlDriver)
Are you willing to resolve this issue by submitting a Pull Request?
Yes. Implementation and unit tests are provided in #12849.
Source: typeorm/typeorm