Skip to content

fix: reconnect the sync host monitor and make failover tests survive slow failovers - #1405

Open
JuanLeee wants to merge 1 commit into
mainfrom
fix/slow-failover-mitigations
Open

JuanLeee wants to merge 1 commit into
mainfrom
fix/slow-failover-mitigations

Conversation

@JuanLeee

@JuanLeee JuanLeee commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

Description

In the sync failover path, a host monitor whose connection dies keeps that connection and never sees its host become the writer, so a writer failover in failover_v2 or gdb_failover can wait out failover_timeout_sec (default 300 s) and fail. In the integration test harness, the failover helper can use 420 s of a test's 600 s pytest timeout, which leaves 180 s for the wrapper's own 300 s failover, and a targeted failover that RDS sends to another instance, or any assertion after a slow failover, ends in an AssertionError the conftest doesn't rerun. main's Integration Tests run 45785990375 failed 2 of its 8 LTS jobs this way, 3.13 pg and 3.14 pg, each after a slow failover (383 s and 395 s). This branch's runs then hit RDS API throttling in test setup, as main's run 46485152797 also did.

This PR makes the sync host monitor drop a failed connection and connect again, as JDBC's host worker does, and makes the harness fit each failover into the test's timeout, check where a targeted failover went, rerun the failures a slow failover causes and retry RDS throttling. Only the first changes the wrapper; the rest is under tests/.

  1. TopologyUtils.get_writer_id_if_connected caught every exception and returned None, its value for a reader, so the except in HostMonitor.__call__ that closes the connection never ran, and a worker whose connection died or timed out kept checking that connection. In run 45785990375's 3.13 pg job the workers for -3, -4 and -5 connected once and never again until pytest-timeout ended the session (from the job log). The error now reaches the caller: the worker logs it at DEBUG, closes the connection, waits 0.1 s and connects again, as JDBC's host worker does after an SQLException. Live since 0b1ba85 (feat: failover v2 plugin (#1079), 2026-01-27), so 2.1.0, 3.0.0 and 3.1.0 have it. The aio monitor isn't affected: it drops its connection on any query error.
  2. RdsTestUtility.failover_cluster_and_wait_until_writer_changed had a fixed 420 s deadline that didn't know the test's timeout, so a slow failover let pytest-timeout fire during the wrapper's own failover, and with --timeout-method=thread that ends the whole session. The conftest now stores each attempt's end time in AttemptState.deadline (a new module, tests/integration/container/utils/attempt_state.py, because the container imports the test utilities under two package names), and the helper stops 330 s before it, so a 600 s test gives it at most 270 s. With 330 s or less left it fails at once with "No time left for a failover", an outage text the conftest reruns once.
  3. The reissue check raised the plain "Failover cluster request was not successful.", which isn't an outage, both when no reissue was left and when less than 120 s were left. A request that RDS accepts but never runs is found only after 240 s (the 180 s wait for a new writer plus 60 s in which the old writer keeps its role), so under the 270 s cap it always has less than 120 s left. With reissues left the helper now fails with "did not finish within ... kept the writer role", an outage, and the plain error stays for the case with no reissue left.
  4. TestCustomEndpointAsync ran under the 600 s default while its sync twin has had @pytest.mark.timeout(1800) since feat: asyncio support (async wrapper, plugins, and SQLAlchemy dialects) #1257. It now has the same marker, so its targeted failovers keep the full 420 s.
  5. A call with target_id returned as soon as the initial writer stopped being the writer. In run 45785990375's 3.14 pg job the failover to the target -1 never completed: -4 restarted as a reader about every 68 s, RDS started a failover back to -4 and then completed its own failover to -2, so the test's role assert failed with no rerun (from the job's RDS events). _writer_instead_of_target now polls every 2 s until the target answers as the writer; when RDS names another writer the helper requests the target again while a reissue and 120 s are left, and otherwise fails with "Failover to did not finish within s", an outage. JDBC's helper treats the target as a suggestion, so this goes further than JDBC.
  6. A failover over 120 s now marks the attempt and logs "Failover from took s, more than 120 s, so a failure of this attempt is rerun", and RdsTestUtility.is_rerun_cause reruns any failure of that attempt once, where the conftest reran only cluster outages. A product failure in such an attempt shows only if it fails twice; reruns stay at 1 per test and 5 per session.
  7. Every RDS client in the harness except the RDS events lookup used botocore's default retry, 3 attempts in these runs, so calls failed with "(Throttling) when calling the DescribeDBClusters operation (reached max retries: 2): Rate exceeded" in this branch's runs and in 74 of the 76 failed cases of main's run 46485152797, 72 of them in setup. The conftest's setup client, the custom endpoint clients and the Django custom endpoint fixture now use RDS_API_RETRY_CONFIG: adaptive mode with 13 total attempts, as JDBC's harness uses since fix: retry RDS/EC2 throttling in integration test harness aws-advanced-jdbc-wrapper#2089, and a 10 s connect and read timeout so one call's 13 attempts end inside the test's timeout. The failover helper's own clients keep botocore's default, so a throttled call there backs off for at most 3 s, and so does the SQLAlchemy custom endpoint fixture, whose teardown runs in its last test's 600 s timer after a failover.

Behaviour changes

  1. With DEBUG on, a failed writer check in a sync host monitor logs "[HostMonitor] The writer check on host '' failed, so the host monitor connects to it again." with the error.
  2. A sync host monitor now connects again after any failed writer check, a writer-query syntax error included, while JDBC ends the worker on SQLSyntaxErrorException; mysql-connector raises ProgrammingError for a dead cursor too, and pgjdbc never raises SQLSyntaxErrorException (both from reading the drivers). When the writer query keeps failing, each monitor connects and closes once per 0.1 s plus connect time, about 8.4 times per second against a PostgreSQL container. Host monitors run only in panic mode.
  3. MultiAzTopologyUtils and GlobalAuroraTopologyUtils inherit get_writer_id_if_connected, so their host monitors reconnect the same way.
  4. AbstractMonitoringConnectionHandler.attempt_connection_upgrade now drops an upgrade connection whose writer check fails. With a monitoring_connection_priority that ranks a reader above the current connection, such as "strict-reader,strict-writer", the dead connection could replace the live monitoring connection before; the default "strict-writer" already dropped it.

Not in scope

  1. Python's Multi-AZ topology treats a returned row as the writer, where JDBC treats an empty result as the writer. No Multi-AZ test ran, so it's left for a separate change.
  2. JDBC's later host monitor changes, the region-aware shape of feat: gdb aws regions aws-advanced-jdbc-wrapper#1940 and the removed panic-mode cooldown of chore: remove panic mode cooldown aws-advanced-jdbc-wrapper#1708, aren't needed for these failures and aren't ported.
  3. This repo's Aurora workflows don't share a concurrency group, so a branch run, main's push run and the Blue/Green workflow can run at once on the same account's RDS quota. JDBC runs its GitHub-hosted RDS workflows one at a time in one shared group and its CodeBuild ones in another. That needs a change under .github/.
  4. The Java host harness builds its RDS and EC2 clients with no retry config, where JDBC's has 13 adaptive attempts. That needs a change to the Java host harness.

Tests

The 9 new unit test cases for the host monitor (5 for HostMonitor reconnects, 1 for the upgrade check, 1 for the error leaving get_writer_id_if_connected, and 2 guards for the empty result and the initial check) include 7 that fail on the base code on their assertions and pass with this change. In the helper tests, 26 are new and 1 is renamed; 9 fail on the base helper on their assertions and 5 on the new is_rerun_cause name. pytest tests/unit -Werror passes 2873 tests on Python 3.12, and mypy, flake8 and isort are clean on Python 3.12 and 3.10. Reproduced against a PostgreSQL container: the base monitor didn't report the new writer within 60 s of the promotion and the changed one did after 0.1 s, and the same held for a writer query blocked past its timeout. No GitHub run or Aurora test showed the reconnect itself; in the 8 failed writer checks in the logs of runs 46489036631 and 46539299828, another worker found the writer first. The product unit tests use the PostgreSQL dialect only, and no unit test covers the conftest wiring or the call sites of RDS_API_RETRY_CONFIG.

Live checks outside this PR, on Aurora PostgreSQL with fault patches applied: a failover held back past its 260 s deadline failed as an outage and passed on rerun, a failover sent to -3 instead of -1 was requested again and passed, a dropped request failed with "kept the writer role" and passed on rerun and 8 throttled setup reads passed. The slow and target faults ran at earlier commits of this branch, before the changes in items 3 and 7. Integration Tests run 46599971953 on this code passed all 8 LTS jobs, with 2 failovers over 120 s (168 s and 175 s) and no session ended by pytest-timeout; 2 stalled failovers failed at their 248 s deadline and 2 requests that RDS dropped on Aurora MySQL failed with "kept the writer role", and all 4 reruns passed. Its 8 Latest jobs failed on the CI account's DB cluster quota with 0 failed test cases. Run 46780260985 failed after its idle 5-instance test clusters were deleted from outside the run, and 7 of its setup calls were throttled through all 13 attempts during that storm. One new message key, for the debug line; no CHANGELOG entry or docs change.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

…slow failovers

In the sync failover path, a host monitor whose connection dies keeps
that connection and never sees its host become the writer, so a writer
failover in `failover_v2` or `gdb_failover` can wait out
`failover_timeout_sec` (default 300 s) and fail. In the integration test
harness, the failover helper can use 420 s of a test's 600 s pytest
timeout, which leaves 180 s for the wrapper's own 300 s failover, and a
targeted failover that RDS sends to another instance, or any assertion
after a slow failover, ends in an `AssertionError` the conftest doesn't
rerun. `main`'s Integration Tests run 45785990375 failed 2 of its 8 LTS
jobs this way, 3.13 pg and 3.14 pg, each after a slow failover (383 s
and 395 s). This branch's runs then hit RDS API throttling in test
setup, as `main`'s run 46485152797 also did.

This PR makes the sync host monitor drop a failed connection and connect
again, as JDBC's host worker does, and makes the harness fit each
failover into the test's timeout, check where a targeted failover went,
rerun the failures a slow failover causes and retry RDS throttling. Only
the first changes the wrapper; the rest is under `tests/`.

1. `TopologyUtils.get_writer_id_if_connected` caught every exception and
   returned `None`, its value for a reader, so the `except` in
   `HostMonitor.__call__` that closes the connection never ran, and a
   worker whose connection died or timed out kept checking that
   connection. In run 45785990375's 3.13 pg job the workers for `-3`,
   `-4` and `-5` connected once and never again until pytest-timeout
   ended the session (from the job log). The error now reaches the
   caller: the worker logs it at DEBUG, closes the connection, waits 0.1
   s and connects again, as JDBC's host worker does after an
   `SQLException`. Live since `0b1ba85` (`feat: failover v2 plugin
   (#1079)`, 2026-01-27), so 2.1.0, 3.0.0 and 3.1.0 have it. The aio
   monitor isn't affected: it drops its connection on any query error.
2. `RdsTestUtility.failover_cluster_and_wait_until_writer_changed` had a
   fixed 420 s deadline that didn't know the test's timeout, so a slow
   failover let pytest-timeout fire during the wrapper's own failover,
   and with `--timeout-method=thread` that ends the whole session. The
   conftest now stores each attempt's end time in
   `AttemptState.deadline` (a new module,
   `tests/integration/container/utils/attempt_state.py`, because the
   container imports the test utilities under two package names), and
   the helper stops 330 s before it, so a 600 s test gives it at most
   270 s. With 330 s or less left it fails at once with "No time left
   for a failover", an outage text the conftest reruns once.
3. The reissue check raised the plain "Failover cluster request was not
   successful.", which isn't an outage, both when no reissue was left
   and when less than 120 s were left. A request that RDS accepts but
   never runs is found only after 240 s (the 180 s wait for a new writer
   plus 60 s in which the old writer keeps its role), so under the 270 s
   cap it always has less than 120 s left. With reissues left the helper
   now fails with "did not finish within ... kept the writer role", an
   outage, and the plain error stays for the case with no reissue left.
4. `TestCustomEndpointAsync` ran under the 600 s default while its sync
   twin has had `@pytest.mark.timeout(1800)` since #1257. It now has the
   same marker, so its targeted failovers keep the full 420 s.
5. A call with `target_id` returned as soon as the initial writer
   stopped being the writer. In run 45785990375's 3.14 pg job the
   failover to the target `-1` never completed: `-4` restarted as a
   reader about every 68 s, RDS started a failover back to `-4` and then
   completed its own failover to `-2`, so the test's role assert failed
   with no rerun (from the job's RDS events).
   `_writer_instead_of_target` now polls every 2 s until the target
   answers as the writer; when RDS names another writer the helper
   requests the target again while a reissue and 120 s are left, and
   otherwise fails with "Failover to <target> did not finish within <n>
   s", an outage. JDBC's helper treats the target as a suggestion, so
   this goes further than JDBC.
6. A failover over 120 s now marks the attempt and logs "Failover from
   <id> took <n> s, more than 120 s, so a failure of this attempt is
   rerun", and `RdsTestUtility.is_rerun_cause` reruns any failure of
   that attempt once, where the conftest reran only cluster outages. A
   product failure in such an attempt shows only if it fails twice;
   reruns stay at 1 per test and 5 per session.
7. Every RDS client in the harness except the RDS events lookup used
   botocore's default retry, 3 attempts in these runs, so calls failed
   with "(Throttling) when calling the DescribeDBClusters operation
   (reached max retries: 2): Rate exceeded" in this branch's runs and in
   74 of the 76 failed cases of `main`'s run 46485152797, 72 of them in
   setup. The conftest's setup client, the custom endpoint clients and
   the Django custom endpoint fixture now use `RDS_API_RETRY_CONFIG`:
   adaptive mode with 13 total attempts, as JDBC's harness uses since
   aws/aws-advanced-jdbc-wrapper#2089, and a 10 s connect and read
   timeout so one call's 13 attempts end inside the test's timeout. The
   failover helper's own clients keep botocore's default, so a throttled
   call there backs off for at most 3 s, and so does the SQLAlchemy
   custom endpoint fixture, whose teardown runs in its last test's 600 s
   timer after a failover.

Behaviour changes:

1. With DEBUG on, a failed writer check in a sync host monitor logs
   "[HostMonitor] The writer check on host '<host>' failed, so the host
   monitor connects to it again." with the error.
2. A sync host monitor now connects again after any failed writer check,
   a writer-query syntax error included, while JDBC ends the worker on
   `SQLSyntaxErrorException`; mysql-connector raises `ProgrammingError`
   for a dead cursor too, and pgjdbc never raises
   `SQLSyntaxErrorException` (both from reading the drivers). When the
   writer query keeps failing, each monitor connects and closes once per
   0.1 s plus connect time, about 8.4 times per second against a
   PostgreSQL container. Host monitors run only in panic mode.
3. `MultiAzTopologyUtils` and `GlobalAuroraTopologyUtils` inherit
   `get_writer_id_if_connected`, so their host monitors reconnect the
   same way.
4. `AbstractMonitoringConnectionHandler.attempt_connection_upgrade` now
   drops an upgrade connection whose writer check fails. With a
   `monitoring_connection_priority` that ranks a reader above the
   current connection, such as `"strict-reader,strict-writer"`, the dead
   connection could replace the live monitoring connection before; the
   default `"strict-writer"` already dropped it.

Not in scope:

1. Python's Multi-AZ topology treats a returned row as the writer, where
   JDBC treats an empty result as the writer. No Multi-AZ test ran, so
   it's left for a separate change.
2. JDBC's later host monitor changes, the region-aware shape of
   aws/aws-advanced-jdbc-wrapper#1940 and the removed panic-mode
   cooldown of aws/aws-advanced-jdbc-wrapper#1708, aren't needed for
   these failures and aren't ported.
3. This repo's Aurora workflows don't share a concurrency group, so a
   branch run, `main`'s push run and the Blue/Green workflow can run at
   once on the same account's RDS quota. JDBC runs its GitHub-hosted RDS
   workflows one at a time in one shared group and its CodeBuild ones in
   another. That needs a change under `.github/`.
4. The Java host harness builds its RDS and EC2 clients with no retry
   config, where JDBC's has 13 adaptive attempts. That needs a change to
   the Java host harness.

Tests:

The 9 new unit test cases for the host monitor (5 for `HostMonitor`
reconnects, 1 for the upgrade check, 1 for the error leaving
`get_writer_id_if_connected`, and 2 guards for the empty result and the
initial check) include 7 that fail on the base code on their assertions
and pass with this change. In the helper tests, 26 are new and 1 is
renamed; 9 fail on the base helper on their assertions and 5 on the new
`is_rerun_cause` name. `pytest tests/unit -Werror` passes 2873 tests on
Python 3.12, and mypy, flake8 and isort are clean on Python 3.12 and
3.10. Reproduced against a PostgreSQL container: the base monitor didn't
report the new writer within 60 s of the promotion and the changed one
did after 0.1 s, and the same held for a writer query blocked past its
timeout. No GitHub run or Aurora test showed the reconnect itself; in
the 8 failed writer checks in the logs of runs 46489036631 and
46539299828, another worker found the writer first. The product unit
tests use the PostgreSQL dialect only, and no unit test covers the
conftest wiring or the call sites of `RDS_API_RETRY_CONFIG`.

Live checks outside this PR, on Aurora PostgreSQL with fault patches
applied: a failover held back past its 260 s deadline failed as an
outage and passed on rerun, a failover sent to `-3` instead of `-1` was
requested again and passed, a dropped request failed with "kept the
writer role" and passed on rerun and 8 throttled setup reads passed. The
slow and target faults ran at earlier commits of this branch, before the
changes in items 3 and 7. Integration Tests run 46599971953 on this code
passed all 8 LTS jobs, with 2 failovers over 120 s (168 s and 175 s) and
no session ended by pytest-timeout; 2 stalled failovers failed at their
248 s deadline and 2 requests that RDS dropped on Aurora MySQL failed
with "kept the writer role", and all 4 reruns passed. Its 8 Latest jobs
failed on the CI account's DB cluster quota with 0 failed test cases.
Run 46780260985 failed after its idle 5-instance test clusters were
deleted from outside the run, and 7 of its setup calls were throttled
through all 13 attempts during that storm. One new message key, for the
debug line; no CHANGELOG entry or docs change.
@JuanLeee
JuanLeee force-pushed the fix/slow-failover-mitigations branch from 2e6379d to 0981a43 Compare December 2, 2026 17:26
@JuanLeee JuanLeee changed the title Fix: slow failover mitigations fix: reconnect the sync host monitor and make failover tests survive slow failovers Dec 2, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant