Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(mysql): retry TiDB/TiProxy unavailability as a transient error (#92151)

Tom Owers2ec0b59f494e
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/mysql/mysql.py b/products/warehouse_sources/backend/temporal/data_imports/sources/mysql/mysql.py
index f12e49f78eba..3dd756ed2be1 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/mysql/mysql.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/mysql/mysql.py
@@ -606,6 +606,7 @@ def _connect_with_transient_retry(kwargs: dict[str, Any]) -> pymysql.Connection:
or _is_transient_connect_broken_pipe(e)
or _is_transient_packet_sequence_error(e)
or _is_transient_vitess_dial_timeout(e)
+ or _is_transient_tiproxy_unavailable(e)
or _is_transient_too_many_connections(e)
or _is_transient_cant_create_thread(e)
):
@@ -659,6 +660,22 @@ def _is_transient_vitess_reparent(e: BaseException) -> bool:
return _VITESS_REPARENT_TOKEN in " ".join(str(arg) for arg in e.args)
+# TiProxy (TiDB's connection proxy) surfaces this 1105 error when it cannot reach a TiDB
+# node due to a failover, a restart, or a momentary network blip. It arrives during the
+# MySQL auth handshake: TiProxy accepts the TCP connection but then cannot route to a
+# backend TiDB node and sends back ER_UNKNOWN_ERROR (1105) with this fixed message. Like
+# the Vitess `code = Unavailable` case above (same error code, same proxy-layer pattern),
+# a fresh attempt recovers once a healthy TiDB node is available.
+_TIPROXY_UNAVAILABLE_TOKEN = "TiProxy fails to connect to TiDB"
+
+
+def _is_transient_tiproxy_unavailable(e: BaseException) -> bool:
+ """Return True if TiProxy could not reach a TiDB backend due to a transient failure."""
+ if not isinstance(e, pymysql.err.OperationalError):
+ return False
+ return _TIPROXY_UNAVAILABLE_TOKEN in " ".join(str(arg) for arg in e.args)
+
+
def _is_transient_metadata_query_reset(e: BaseException) -> bool:
"""Return True if a metadata query's connection was reset mid-query — a transient blip.
@@ -706,6 +723,7 @@ def _retry_on_transient_tablet_unavailable(
if attempt >= max_attempts or not (
_is_transient_tablet_unavailable(e)
or _is_transient_vitess_reparent(e)
+ or _is_transient_tiproxy_unavailable(e)
or _is_transient_metadata_query_reset(e)
):
raise
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/mysql/source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/mysql/source.py
index 4abe0b1a8b49..8cd0dfd77140 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/mysql/source.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/mysql/source.py
@@ -403,6 +403,12 @@ class MySQLSource(SQLSource[MySQLSourceConfig], SSHTunnelMixin, ValidateDatabase
"Too many connections",
"Can't create a new thread",
"reparent operation in progress",
+ # TiProxy cannot reach a TiDB backend due to a failover, restart, or momentary
+ # network blip. `_connect_with_transient_retry` already retries it in-process (see
+ # `_is_transient_tiproxy_unavailable` in mysql.py). This entry is the backstop for
+ # the rare case where it exhausts that budget so Temporal's own activity retry
+ # can recover it rather than surfacing it as error-tracking noise.
+ "TiProxy fails to connect to TiDB",
}
def reconcile_schema_metadata(
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/mysql/tests/test_mysql.py b/products/warehouse_sources/backend/temporal/data_imports/sources/mysql/tests/test_mysql.py
index a0eca98f2f39..dd197438ff06 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/mysql/tests/test_mysql.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/mysql/tests/test_mysql.py
@@ -35,6 +35,7 @@ from products.warehouse_sources.backend.temporal.data_imports.sources.mysql.mysq
_is_transient_metadata_query_reset,
_is_transient_packet_sequence_error,
_is_transient_tablet_unavailable,
+ _is_transient_tiproxy_unavailable,
_is_transient_too_many_connections,
_is_transient_vitess_dial_timeout,
_is_transient_vitess_reparent,
@@ -1493,6 +1494,26 @@ class TestConnectTransientRetry:
assert mock_connect.call_count == 2
sleep.assert_called_once_with(2)
+ def test_retries_tiproxy_unavailable_then_succeeds(self, mocker):
+ sleep = mocker.patch("products.warehouse_sources.backend.temporal.data_imports.sources.mysql.mysql.time.sleep")
+ conn = MagicMock()
+ conn.__enter__.return_value = conn
+ mock_connect = mocker.patch(
+ "products.warehouse_sources.backend.temporal.data_imports.sources.mysql.mysql.pymysql.connect",
+ side_effect=[
+ pymysql.err.OperationalError(
+ 1105, "TiProxy fails to connect to TiDB, please make sure TiDB is available"
+ ),
+ conn,
+ ],
+ )
+
+ with MySQLImplementation().connect(_make_config()) as yielded:
+ assert yielded is conn
+
+ assert mock_connect.call_count == 2
+ sleep.assert_called_once_with(2)
+
def test_does_not_retry_connection_refused(self, mocker):
sleep = mocker.patch("products.warehouse_sources.backend.temporal.data_imports.sources.mysql.mysql.time.sleep")
mock_connect = mocker.patch(
@@ -1627,6 +1648,32 @@ class TestIsTransientVitessReparent:
assert not _is_transient_vitess_reparent(ValueError("reparent operation in progress"))
+class TestIsTransientTiproxyUnavailable:
+ def test_matches_tiproxy_unavailable(self):
+ assert _is_transient_tiproxy_unavailable(
+ pymysql.err.OperationalError(1105, "TiProxy fails to connect to TiDB, please make sure TiDB is available")
+ )
+
+ @pytest.mark.parametrize(
+ "code,message",
+ [
+ # Other 1105 payloads (Vitess cases, unrelated TiDB errors) are not this class.
+ (1105, "vttablet: rpc error: code = Unavailable desc = node is shutting down"),
+ (1105, "vttablet: rpc error: code = InvalidArgument desc = some bad request"),
+ (1045, "Access denied for user"),
+ (2003, "Can't connect to MySQL server on 'db.example.com'"),
+ ],
+ )
+ def test_does_not_match_other_errors(self, code, message):
+ assert not _is_transient_tiproxy_unavailable(pymysql.err.OperationalError(code, message))
+
+ def test_does_not_match_error_without_args(self):
+ assert not _is_transient_tiproxy_unavailable(pymysql.err.OperationalError())
+
+ def test_does_not_match_non_operational_error(self):
+ assert not _is_transient_tiproxy_unavailable(ValueError("TiProxy fails to connect to TiDB"))
+
+
class TestIsTransientMetadataQueryReset:
def test_matches_connection_reset_mid_query(self):
# A peer reset landing on an already-open connection while a metadata query (e.g.