Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions docs/rust-server-port.md
Original file line number Diff line number Diff line change
Expand Up @@ -491,12 +491,15 @@ contract fixture before its correction.
instead: poll probes share the rest of the node's pool, leaving the reserve
to health, readiness and admin requests. Unset, all three leave native's
behavior unchanged. Whatever the setting, held polls' admission, replay,
probe, readiness and claim work takes its connections from a budget of the
request pool size minus one (at least one), and the maintenance loop has
its own connection, so neither occupies the request pool's last
connection. Other work, listed in the server reference, stays outside the
budget ([#1204](https://github.com/durable-workflow/server/issues/1204),
[#1355](https://github.com/durable-workflow/server/issues/1355)).
probe, readiness and claim work takes its connections from a budget: reads
take at most the request pool size minus two (two read slots on the
request pool of four) and the transaction holding the transition queue one
more. The maintenance loop has its own connection, so neither occupies the
request pool's last connection. Other work, listed in the server
reference, stays outside the budget
([#1204](https://github.com/durable-workflow/server/issues/1204),
[#1355](https://github.com/durable-workflow/server/issues/1355),
[#1372](https://github.com/durable-workflow/server/issues/1372)).
The per-namespace variants stay ignored
([#982](https://github.com/durable-workflow/server/issues/982),
[server reference](server-reference.md#native-long-poll-capacity)).
Expand Down
18 changes: 11 additions & 7 deletions docs/server-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -2415,20 +2415,23 @@ how many request workers are busy.
with `timeout_seconds: 0` never needs a slot. A slot is released when the
poll answers, fails or its client disconnects.
- `DW_WORKER_LONG_POLL_RESERVED_HTTP_WORKERS` reserves database connections.
A native node opens four database connections on MySQL, PostgreSQL and file
SQLite: a request pool of three, which health, readiness, admin and every
A native node opens five database connections on MySQL, PostgreSQL and file
SQLite: a request pool of four, which health, readiness, admin and every
other request share, and one connection of its own for the maintenance loop
(timers, schedules, repair, retention and the task-offer key window), so
maintenance never borrows a request connection
([#1355](https://github.com/durable-workflow/server/issues/1355)). With a
([#1355](https://github.com/durable-workflow/server/issues/1355),
[#1372](https://github.com/durable-workflow/server/issues/1372)). With a
positive value, every poll probe takes one of `request pool size - reserved`
probe permits (at least one) and returns it before waiting, so polls never
occupy the reserved connections. Unset or zero, probes take no permit.
occupy the reserved connections: `2`, the PHP default, gives two permits,
`3` gives one. Unset or zero, probes take no permit.
- Whatever the setting, a node-wide budget covers this held-poll work, so it
never occupies the request pool's last connection, which producers and
registrations keep. Its reads take at most the request pool size minus two
connections (at least one); the one transaction holding the transition
queue takes one more, so it never waits behind those reads:
connections (at least one), so two read slots on the request pool of four;
the one transaction holding the transition queue takes one more, so it
never waits behind those reads:
- a held workflow or activity poll's admission and replay reads, its
probes' lock-free reads (one connection per probe, returned before it
claims or waits), its claim transactions and, for a workflow poll, its
Expand Down Expand Up @@ -2879,7 +2882,8 @@ every operator-facing variable the server honors.
| `DW_POLLING_CACHE_PATH` | `storage/.../server-polling/<APP_ENV>` | Writable local coordination and wait-lock directory, also required with Redis or database polling caches. |
| `DW_WAKE_SIGNAL_TTL_SECONDS` | `max(DW_WORKER_POLL_TIMEOUT + 5, 60)` | TTL for per-queue wake signals. |
| `DW_WORKER_LONG_POLL_MAX_CONCURRENT` | (unset; derived for PHP CLI server) | Optional cap for concurrent held workflow/activity worker long-poll waits on this server node. Query-task polls use a separate wait budget so live workflow queries are not starved by idle workflow/activity waits. Native honors it as a node-wide cap with the same `429 long_poll_capacity_exhausted` refusal; unset, native waits are unbounded (see [Native long-poll capacity](#native-long-poll-capacity)). |
| `DW_WORKER_LONG_POLL_RESERVED_HTTP_WORKERS` | `2` | PHP CLI server workers reserved for health and control-plane requests when deriving the workflow/activity long-poll wait cap. Native has no request-worker pool; it reads this as the number of its request-pool database connections that long-poll probes leave free. Unset, native reserves none for it; the held-poll work listed under native long-poll capacity still leaves the request pool's last connection free. |
| `DW_WORKER_LONG_POLL_RESERVED_HTTP_WORKERS` | `2` | PHP CLI server workers reserved for health and control-plane requests when deriving the workflow/activity long-poll wait cap. Native has no request-worker pool; it reads this as the number of its request-pool database connections (four) that long-poll probes leave free, so `2` leaves two probe permits. Unset, native reserves none for it; the held-poll work listed under native long-poll capacity still leaves the request pool's last connection free. |
| `DW_NATIVE_DB_STATEMENT_CACHE_CAPACITY` | `192` | Native only. Server-side prepared statements each MySQL or PostgreSQL request connection keeps (0 to 4083; SQLx's `statement-cache-capacity` `DB_URL` parameter sets the same value and must agree). The maintenance connection keeps at most 48 (none at `0`). A node holds at most `4 × capacity + min(capacity, 48)`, so on MySQL keep `nodes × (4 × capacity + min(capacity, 48)) + other clients' statements ≤ max_prepared_stmt_count` (16382 by default): 20 nodes at the default. A request connection's hot path uses about 166 statements on the PHP SDK workload ([#1174](https://github.com/durable-workflow/server/issues/1174)) and about 126 on the in-process workload ([#1372](https://github.com/durable-workflow/server/issues/1372)). |
| `DW_MAX_TASKS_PER_POLL` | `1` | Maximum tasks returned per poll. |
| `DW_SQLITE_CLAIM_LOCK_TTL_SECONDS` | `10` | Seconds the SQLite quickstart backend holds the cache-backed worker poll claim gate before the lock expires. |
| `DW_SQLITE_CLAIM_LOCK_WAIT_SECONDS` | `5` | Seconds SQLite worker poll claims wait for the cache-backed claim gate before returning backend lock pressure. |
Expand Down
48 changes: 33 additions & 15 deletions rust/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -250,34 +250,51 @@ datum decoding or qualifying ingress.

## Prepared-statement cache

Each connection of the MySQL and PostgreSQL runtime pools (4 connections per
node) keeps up to `DW_NATIVE_DB_STATEMENT_CACHE_CAPACITY` server-side prepared
statements, default **200**. SQLx's `statement-cache-capacity` `DB_URL`
parameter sets the same value; if both are set they must agree. Values are
whole numbers from `0` (prepare and close every statement) to `4095`; anything
else, or two different values, stops startup. SQLite is
unaffected.
Each request connection of the MySQL and PostgreSQL runtime pools (a request
pool of 4 per node) keeps up to `DW_NATIVE_DB_STATEMENT_CACHE_CAPACITY`
server-side prepared statements, default **192**; the node's fifth
connection, the maintenance loop's, keeps at most 48 (fewer if the capacity is
smaller, none at `0`). SQLx's `statement-cache-capacity` `DB_URL` parameter
sets the same value; if both are set they must agree. Values are whole numbers
from `0` (prepare and close every statement) to `4083`; anything else, or two
different values, stops startup. SQLite is unaffected.

SQLx's own default of 100 is smaller than the hot path. On the #1078 workload
(200 workflows of five 20 ms activities, six PHP workers, MySQL 8.0) each pool
connection uses about 160 distinct statements. At 100 the cache evicted them,
so every workflow prepared about 12 statements again and closed about 11
connection uses about 160 to 166 distinct statements. At 100 the cache evicted
them, so every workflow prepared about 12 statements again and closed about 11
(Server #1174). At 200 the same workload prepares 0.2 statements per workflow
and closes none.

Server #1372 added the fifth connection. The maintenance loop uses few
statements, so its connection keeps a small cache and the request connections
keep 192, still above the #1174 working set. On the same shape of workload
driven in process (200 workflows of five 20 ms activities and a signal, six
workers, MariaDB 10.11; `tests/execution/hot_path_cache.rs`) each request
connection uses 123 to 126 distinct statements and the maintenance connection
about 8:

| Capacity | Prepares per workflow | Closes per workflow | Hit rate |
|---|---|---|---|
| 200 | 2.50 | 0 | 99.71 % |
| 160 | 2.52 | 0 | 99.70 % |
| 128 | 2.51 | 0.26 | 99.71 % |
| 100 | 17.42 | 15.70 | 97.96 % |

MySQL counts every prepared statement of every client toward the server-wide
`max_prepared_stmt_count` (default 16382). When a client reaches it, further
prepares fail with error 1461. A native node holds at most
`4 × capacity` statements, so keep
`4 × capacity + min(capacity, 48)` statements, so keep

```text
nodes × 4 × DW_NATIVE_DB_STATEMENT_CACHE_CAPACITY + other clients' statements ≤ max_prepared_stmt_count
nodes × (4 × DW_NATIVE_DB_STATEMENT_CACHE_CAPACITY + min(DW_NATIVE_DB_STATEMENT_CACHE_CAPACITY, 48))
+ other clients' statements ≤ max_prepared_stmt_count
```

At the default that is 800 per node and up to 20 nodes on a default MySQL
server. The hot path above fills about 650 per node. For a larger cluster,
At the default that is 816 per node and up to 20 nodes on a default MySQL
server. The hot path above fills about 510 per node. For a larger cluster,
raise `max_prepared_stmt_count` or lower the capacity. `Prepared_stmt_count`
in `SHOW GLOBAL STATUS` reports current use. The upper bound of 4095 keeps one
in `SHOW GLOBAL STATUS` reports current use. The upper bound of 4083 keeps one
node within the default limit. PostgreSQL has no server-wide limit; each cached
statement holds memory in its own backend.

Expand Down Expand Up @@ -405,7 +422,8 @@ across restarts. This opt-in does not authorize use as a production replacement.

The first slice uses Axum/Tokio for HTTP and SQLx for SQLite transactions, with
only the required features enabled and no build-time database or SQL macro
generation. Four connections and `BEGIN IMMEDIATE` serialize durable state
generation. Five connections (a request pool of four and the maintenance
loop's) and `BEGIN IMMEDIATE` serialize durable state
changes; SQLite runs on SQLx's connection workers. HTTP uses two Tokio executor
threads. WAL with full synchronous writes retains acknowledged state.
Namespaces within one runtime share a FIFO transition queue before pool
Expand Down
40 changes: 40 additions & 0 deletions rust/src/runtime/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,14 @@ pub(super) trait Backend: Database {
/// `statement_timeout` ends every statement there within 30 s. MySQL
/// drops it, so a healthy server is never cut off mid-delete.
fn lift_statement_reply_bound(_connection: &mut Self::Connection) {}
/// The maintenance connection's connect options, from the request pool's:
/// a statement cache of at most `statement_cache::MAINTENANCE` entries
/// (Server #1372). SQLite keeps no server-side statements.
fn maintenance_connect_options(
options: <Self::Connection as sqlx::Connection>::Options,
) -> <Self::Connection as sqlx::Connection>::Options {
options
}
const HISTORY_SEQUENCE_MAX: i64 = i64::MAX;
const HISTORY_ADVANCE_RETURNS_ROW: bool = true;
const HISTORY_ADVANCE_SQL: &str = "UPDATE workflow_runs SET last_history_sequence=last_history_sequence+1 WHERE id=$1 AND last_history_sequence>=0 AND last_history_sequence<$2 RETURNING last_history_sequence";
Expand Down Expand Up @@ -633,6 +641,12 @@ impl Encode<'_, Postgres> for PostgresDocument {
}

impl Backend for Postgres {
fn maintenance_connect_options(
options: <Self::Connection as sqlx::Connection>::Options,
) -> <Self::Connection as sqlx::Connection>::Options {
let capacity = crate::statement_cache::maintenance(options.get_statement_cache_capacity());
options.statement_cache_capacity(capacity)
}
const COMMIT_HANDOFF: bool = true;
const HISTORY_SEQUENCE_MAX: i64 = i32::MAX as i64;
const TASK_CANDIDATES_SQL: &str = super::postgres::TASK_CANDIDATES_SQL;
Expand Down Expand Up @@ -765,6 +779,12 @@ impl Backend for MySql {
fn lift_statement_reply_bound(connection: &mut Self::Connection) {
connection.set_reply_timeout(None);
}
fn maintenance_connect_options(
options: <Self::Connection as sqlx::Connection>::Options,
) -> <Self::Connection as sqlx::Connection>::Options {
let capacity = crate::statement_cache::maintenance(options.get_statement_cache_capacity());
options.statement_cache_capacity(capacity)
}
const COMMIT_HANDOFF: bool = true;
const HISTORY_SEQUENCE_MAX: i64 = u32::MAX as i64;
const HISTORY_ADVANCE_RETURNS_ROW: bool = false;
Expand Down Expand Up @@ -1249,3 +1269,23 @@ async fn mysql_ownership_lock(tx: &mut Transaction<'_, MySql>) -> Result<()> {
.bind(mysql::MARIADB_VERSION).bind(mysql::MYSQL_VERSION).fetch_one(&mut **tx).await?;
Ok(())
}

#[cfg(test)]
mod maintenance_cache_tests {
use super::Backend;

/// Server #1372: the maintenance connection keeps at most
/// `statement_cache::MAINTENANCE` statements, and none when the
/// configured capacity turns the cache off.
#[test]
fn the_maintenance_connection_keeps_a_small_cache_or_none() {
use crate::statement_cache::{DEFAULT, MAINTENANCE, MAX};
use sqlx::{mysql::MySqlConnectOptions, postgres::PgConnectOptions};
for (configured, kept) in [(DEFAULT, MAINTENANCE), (MAX, MAINTENANCE), (20, 20), (0, 0)] {
let mysql = sqlx::MySql::maintenance_connect_options(MySqlConnectOptions::new().statement_cache_capacity(configured));
assert_eq!(mysql.get_statement_cache_capacity(), kept, "{configured}");
let postgres = sqlx::Postgres::maintenance_connect_options(PgConnectOptions::new().statement_cache_capacity(configured));
assert_eq!(postgres.get_statement_cache_capacity(), kept, "{configured}");
}
}
}
3 changes: 3 additions & 0 deletions rust/src/runtime/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ mod transition_codec_tests;
#[cfg(test)]
#[path = "continuation_atomic_tests.rs"]
pub(super) mod continuation_atomic_tests;
#[cfg(test)]
#[path = "poll_wake_tests.rs"]
mod poll_wake_tests;

#[derive(Clone)]
pub struct Runtime {
Expand Down
1 change: 1 addition & 0 deletions rust/src/runtime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ mod test_reply_gate;
mod poll_sweeps;
mod plain_input;
mod poll_readiness;
mod poll_wakes;
pub mod postgres;
mod previews;
mod prometheus_metrics;
Expand Down
4 changes: 4 additions & 0 deletions rust/src/runtime/mysql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2689,6 +2689,10 @@ mod pool_tests;
#[path = "mysql_stall_tests.rs"]
mod stall_tests;

#[cfg(test)]
#[path = "mysql_held_poll_bench.rs"]
mod held_poll_bench;

#[cfg(test)]
mod tests {

Expand Down
Loading