Cluster rehearsal 3: with PostgreSQL paused (docker pause, so its kernel still answered TCP keepalives), requests on connections already checked out hung until it came back, and /healthz/ready stayed 200 through the outage. #41 bounded getting a connection, not using one. Client-side query limits (store::backend::query_timeout). Every operation on a PostgreSQL or MySQL connection now runs under a time limit. A server-side statement_timeout (or MySQL's MAX_EXECUTION_TIME, which covers SELECTs only) can't do this: the server that would enforce it is the one not answering. When an operation runs out, its connection is closed instead of pooled, since a query may still be in flight on it or a transaction open: deadpool's Object::take on PostgreSQL; Conn::disconnect on MySQL, which marks the connection closed before it sends anything, so the pool discards it even when the server never answers. - query, 2 minutes: reads, writes (the whole transaction with its retries), blobs, SQL lookups, search queries and indexing. These take milliseconds; two minutes leaves room for a large blob over a slow link and still ends a hang. - maintenance, 30 minutes: range deletes (account removal, purges), unindexing, purge_store, and creating tables and indexes at startup, which can legitimately run long in one statement. Their existing chunked fallback for server-side statement timeouts is unchanged. - iterate (exports, reindexing, maintenance scans) can run for hours, so the query limit bounds each wait for the database (preparing, the query starting, the next row) rather than the whole scan. The limits are fixed, like the pool timeouts; the DataStore schema has no field for them. Tests set them with Store::with_query_timeouts (test_mode only). Readiness. /healthz/ready answered 200 whenever a data store was configured. It now reads one key from the data store with a 2 s limit and reuses the answer for 2 s, so probes can't load the database; while one probe runs, others get the last answer. The first failed probe of an outage is logged. /healthz/live stays 200: restarting a node doesn't bring its database back, and an orchestrator restarting on failed liveness would restart every node at once. The container HEALTHCHECK already uses /healthz/live. Tests, store::pool_timeout (a proxy that stops forwarding while keeping connections open plays the paused database): - postgres_query_timeout, mysql_query_timeout (new): with four pooled connections open, a read, a scan and a write each fail with "Query timed out" 2.0 s after the pause (2 s test limit); once the proxy forwards again the store answers. With the limits set to an hour (upstream's behavior), the read was still waiting at the test's 20 s limit. - postgres_readiness (new, STORE=PostgreSql): a node's data store goes through the proxy; /healthz/ready is 200, 503 about 4 s after the pause while /healthz/live stays 200, and 200 again about 2 s after it ends. - postgres_pool_timeout, mysql_pool_timeout: pass as before. store::store_tests (PostgreSql, MySql, including the MariaDB statement timeout step) and store::task_locks (PostgreSql) pass; store::search_tests (PostgreSql) fails at the same ordering assertion (query.rs:684) as on main.
87 lines
2.9 KiB
Rust
87 lines
2.9 KiB
Rust
/*
|
|
* SPDX-FileCopyrightText: 2020 Stalwart Labs LLC <[email protected]>
|
|
*
|
|
* SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-SEL
|
|
*
|
|
* Modified by Coffey Labs in 2026 for INBUXA.
|
|
*/
|
|
|
|
use std::ops::Range;
|
|
|
|
use crate::backend::postgres::into_pool_error;
|
|
|
|
use super::{PostgresStore, bounded, into_error};
|
|
|
|
impl PostgresStore {
|
|
pub(crate) async fn get_blob(
|
|
&self,
|
|
key: &[u8],
|
|
range: Range<usize>,
|
|
) -> trc::Result<Option<Vec<u8>>> {
|
|
let conn = self.conn_pool.get().await.map_err(into_pool_error)?;
|
|
let limit = self.timeouts.query;
|
|
let result = tokio::time::timeout(limit, async {
|
|
let s = conn
|
|
.prepare_cached("SELECT v FROM t WHERE k = $1")
|
|
.await
|
|
.map_err(into_error)?;
|
|
conn.query_opt(&s, &[&key])
|
|
.await
|
|
.and_then(|row| {
|
|
if let Some(row) = row {
|
|
Ok(Some(if range.start == 0 && range.end == usize::MAX {
|
|
row.try_get::<_, Vec<u8>>(0)?
|
|
} else {
|
|
let bytes = row.try_get::<_, &[u8]>(0)?;
|
|
bytes
|
|
.get(range.start..std::cmp::min(bytes.len(), range.end))
|
|
.unwrap_or_default()
|
|
.to_vec()
|
|
}))
|
|
} else {
|
|
Ok(None)
|
|
}
|
|
})
|
|
.map_err(into_error)
|
|
})
|
|
.await;
|
|
bounded(conn, result, limit)
|
|
}
|
|
|
|
pub(crate) async fn put_blob(&self, key: &[u8], data: &[u8]) -> trc::Result<()> {
|
|
let conn = self.conn_pool.get().await.map_err(into_pool_error)?;
|
|
let limit = self.timeouts.query;
|
|
let result = tokio::time::timeout(limit, async {
|
|
let s = conn
|
|
.prepare_cached(
|
|
"INSERT INTO t (k, v) VALUES ($1, $2) ON CONFLICT (k) DO UPDATE SET v = EXCLUDED.v",
|
|
)
|
|
.await
|
|
.map_err(into_error)?;
|
|
conn.execute(&s, &[&key, &data])
|
|
.await
|
|
.map_err(into_error)
|
|
.map(|_| ())
|
|
})
|
|
.await;
|
|
bounded(conn, result, limit)
|
|
}
|
|
|
|
pub(crate) async fn delete_blob(&self, key: &[u8]) -> trc::Result<bool> {
|
|
let conn = self.conn_pool.get().await.map_err(into_pool_error)?;
|
|
let limit = self.timeouts.query;
|
|
let result = tokio::time::timeout(limit, async {
|
|
let s = conn
|
|
.prepare_cached("DELETE FROM t WHERE k = $1")
|
|
.await
|
|
.map_err(into_error)?;
|
|
conn.execute(&s, &[&key])
|
|
.await
|
|
.map_err(into_error)
|
|
.map(|hits| hits > 0)
|
|
})
|
|
.await;
|
|
bounded(conn, result, limit)
|
|
}
|
|
}
|