Repository navigation
[connectors] Retry a Postgres batch on a read-only session - #8026
Conversation
| // for example because of a lock on the table. A wait here | ||
| // for the server to come back has no end, so the failure | ||
| // counts as an attempt to write the batch. | ||
| if server_answered { |
There was a problem hiding this comment.
pitfall (medium): server_answered is computed for errors from connect() too, not only from PreparedStatements::new. Any startup-phase ErrorResponse ends the connection, whatever its severity. But session_stays_open() only treats it as ended when the severity is FATAL/PANIC. PgBouncer before roughly 1.19 always sent ERROR severity (send_pooler_error), and so do some other poolers. That covers 08P01 server login has been failing, try again later and no more connections allowed. With those poolers, the "server/pooler not available" case now uses the 10 attempts (about 36 s) and drops the batch, where it used to wait with no limit. The PgBouncer 1.26.1 manual run doesn't exercise this. Suggest returning Reconnect::Failed only for a prepare failure (e.g. tag it in retry_connecting), and keeping every connect() error on the unbounded backoff path.
There was a problem hiding this comment.
Good catch, fixed: only a failed prepare can use an attempt now, a failed login always waits with no limit. A class 08 error also counts as the end of the session whatever its severity, for the old PgBouncer that sends ERROR and closes the session at the first statement. Small correction: PgBouncer switched to FATAL in 1.16, not 1.19. Two new suite cases, one for each (refused login with ERROR 53300, and ERROR 08P01 at the prepare).
|
Review: Postgres sink retries on read-only and server-side errors
Gates: tests ✅ (unit tests plus suite cases with revert checks), docs and changelog ✅,
|
| /// [`PostgresWorker::retry_connecting_with_backoff`], and is not bounded. | ||
| /// | ||
| /// The second attempt starts at once. Each later attempt waits one second | ||
| /// longer than the one before it, so the waits add up to 36 seconds. See |
There was a problem hiding this comment.
Other output connectors have the max_retries setting. We need the same option here, and we need to make sure it has the same behavior and the same defaults across all connectors. Today the default behavior is not consistent: delta retries forever by default, while dynamodb defaults to 10 retries. I think the delta default is better. I will create a separate issues to unify the max_retries behavior.
/// Maximum number of retries for failed operations.
///
/// <skipped delta-specific details>
///
/// When not specified, the connector performs infinite retries. When set to 0, the connector doesn't
/// retry failed operations.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub max_retries: Option<u32>,
There was a problem hiding this comment.
36 s of waits for the 10 attempts (0, 0, 1 to 8 s), plus the time of each attempt. The wait for a server that doesn't answer isn't in it, and has no limit. Your chaos test agrees: the loss starts after about 36 s of rejected writes. With #8098 there's no limit by default, and the wait grows to 60 s max.
There was a problem hiding this comment.
Agreed, done in #8098: max_retries with the Delta semantics (not set = no limit, the default; 0 = no retry), plus unhealthy/healthy reports while it retries.
| // pooler can limit the connections of the role, and the old session | ||
| // then holds the slot that the new one needs. | ||
| self.client = None; | ||
| let mut client = connect(&self.config, &self.endpoint_name)?; |
There was a problem hiding this comment.
how does this interact with other open connections? do they all end up making a new client?
There was a problem hiding this comment.
No. Each worker thread owns its client, there's no shared pool. A worker makes a new client only before a new attempt on its own part of a batch; a worker whose part committed keeps its session. In a failover each worker fails its own write, so each one connects again, and as each closes its old session first, we never have more than threads connections.
Chaos test of #8017, #8018 and #8026 on KubernetesWe ran the three PRs merged together (#8017 at 82708ab, #8018 at d4a286d, #8026 at aa3d756, no merge conflicts) against faults in PostgreSQL and PgBouncer, and ran the same faults against v0.385.0, which is the merge base. Result: with the PRs, no run lost data without a report. 41 of 44 runs lost nothing. The 3 other runs had a server that rejected writes for 75 s, longer than the 10 attempts of the connector. The connector dropped 2 batches in each of them, and the completion tokens of exactly those batches returned
Setup
CheckThe driver keeps the completion token and the changed keys of each ingress request, and the rows that each sink table must have at the end. After the fault ends, it asks for the status of each token and reads each sink table on the primary.
Each run also sends a small batch before and after the scenario and checks that it reaches each sink, so a connector that stays broken after a fault fails the run. This did not occur. ScenariosThe numbers are the keys that differ from the expected rows in the worst sink table. The error column is from the pipeline log and the PgBouncer log of the run with the PRs.
Notes on the table:
Observations
Limits
|
After a failover, a connection pooler can hold a session on the old primary, which is then a read-only replica. The server rejects each write with SQLSTATE 25006 and the session stays open. The Postgres output connector classified this error as permanent and dropped the batch, and it dropped each later batch for as long as the session stayed open. These changes correct this: - The classifier also treats these errors as temporary: 25006, 55P03 (lock not available), and the SQLSTATE classes 40 (serialization failure, deadlock), 53 (insufficient resources), 57 (operator intervention, which now includes a cancelled query), 58 (system error) and XX (internal error). The statement and its rows usually do not cause them. Where they do, the limit of 10 attempts bounds the cost. - Each new attempt to write a batch opens a new connection. Before, the worker kept a session that passed a validity check, and a read-only session passes it. The worker closes the old session before the new login, because the old session can hold the only connection slot of the role. It keeps the new client only together with its prepared statements. - The worker waits between the attempts: the second attempt starts at once, and each later attempt waits one second longer, 36 seconds in total for the 10 attempts. Before, the attempts on an open session ran with no wait. - A server that answers and does not prepare the statements after a new connection uses one of the 10 attempts. The wait with no limit stays for a server or a pooler that is not available: a failed login, and a connection error (class 08) of any severity, which ends the session also when a pooler sends it as ERROR (PgBouncer before 1.16). - The error for a batch that the worker gives up on states why the transaction was lost. The test proxy can now answer a statement as a server does that cannot run it, with the session left open. Four new suite cases use it. Each fails if the change that it covers is removed: the classifier, the new connection, the order of logout and login (with a role that has one connection slot), the wait, and the attempt limit for a failed prepare. Two more cases check that a failed login with severity ERROR and a pooler that sends ERROR 08P01 at the prepare do not use the attempts. A unit test covers the wait before each attempt. Document the new cases, and add the fix to the changelog. Related: #8014 Signed-off-by: Xavier Bestel <xavier.bestel@feldera.com>
aa3d756 to
f5cd2c6
Compare
|
|
Thanks, that's a great test. On the observations:
|
Linked Issue
Related to #8014. Stacked on #8017. #8098, stacked on this PR, removes the limit of 10 attempts. This PR covers a failure of the same kind that #8017 does not handle: a session that stays open on a server that cannot write.
What changes
After a failover, a connection pooler can hold a session on the old primary, which is then a read-only replica. The server rejects each write with SQLSTATE
25006and the session stays open. With #8017, the connector classifies this error as permanent and drops the batch. It also drops each later batch for as long as the session stays open.08and for57P01,57P02,57P0325006,55P03(lock not available) and for the classes40(serialization failure, deadlock),53(insufficient resources),57(all of the class, which adds a cancelled statement),58(system error),XX(internal error)08) also with severityERROR, which PgBouncer before 1.16 sends before it closes the sessionA statement that the server rejects for its rows (a constraint, a type, a permission) stays permanent.
The statement and its rows usually do not cause the errors that are now temporary, but some of them come back each time: a statement that is always slower than the statement timeout, a role that is configured as read-only, an internal error that a trigger raises. The cost is at most 10 attempts, which can take 10 times the statement timeout. This cost must be examined again if the number of attempts becomes unbounded.
Not changed:
Describe Manual Test Plan
No manual test with a real failover. The suite cases use the TCP proxy of #8017, which now can answer a statement as a server does that cannot run it (
ERROR, session left open).I ran the outage test of #8017 again on this tree: PgBouncer 1.26.1 (transaction pooling) in front of PostgreSQL 15, PostgreSQL stopped while a batch of 3000 rows was pending, then started again. Result: 3000 of 3000 rows, no fatal error, one
pg_batch_starterror and 6pg_conn_retryerrors while the server was down.57014)ERROR 08P01at the transaction start and at 9 prepares, then recoversERROR 5330057014case fails (it does not end)08with severityERRORkeeps the session openERROR 08P01case fails (rows lost). Checked on #8098Checklist
Breaking Changes?
None. The connector now writes a batch again in more cases where it dropped the batch before. A retry of a batch that used an open session now opens a new connection.