Skip to content
Merged
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
5 changes: 3 additions & 2 deletions docs/gateway/live-sharing.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,9 @@ channels, tasks, timers and locks; both speak GraphQL-WS through one codec,
`NativeDelivery::live_counts()` reports active groups, consumers, cumulative source
attempts, resets, upstream frames, exact duplicate frames and safe handoffs. Source
attempts include credential reconnects; they are not a count of active producers.
`GraphqlEngine::live_subscriber_count()` separately reports actual origin producer
subscriptions. No credentials or subject identifiers appear in these counters.
`GraphqlEngine::live_subscriber_count()` reports live subscriptions reaching the
origin and `live_producer_count()` its producers
(one per distinct origin live read; see `docs/live-query-delivery.md`). No credentials or subject identifiers appear in these counters.

Disable `live_sharing` and remove the live delivery resource to restore independent
subscriptions. Existing client resume/reset and response-sealing behavior remains
Expand Down
24 changes: 23 additions & 1 deletion docs/live-query-delivery.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,35 @@ Every live response declares `extensions.distributed.live.mode`:
A failed execution has no result and therefore no `live` metadata. It is sent
as the terminal failure frame described below.

## Shared execution at the origin

Subscribers whose live roots are the same read share one producer. A catalog
root is the same read when its role, compiled SQL and every bind (row policies
and claim values included) match and, with the causal protocol on, its cache
scope does too; an external root when its role, root, arguments and session
match. The first subscriber's frame is the producer's first execution; a later
one opens with its own read, which honours its own resume cursors. After that,
each change that touches the read is debounced (100ms) and executed once, and
the frame goes to every subscriber, each with its own hash gate and envelope.
A subscriber whose cursors are not where a frame replays from (it joined
between frames, or skipped one while slow) catches up with one read of its
own, so replay stays exact. The producer keeps only the latest frame, so a
slow subscriber never holds the others back.

At most `GraphqlEngineBuilder::live_concurrency` live executions (default
8) run at once per engine: first reads, refreshes and catch-ups together.
When the last subscriber of a read leaves, its producer stops.
`GraphqlEngine::live_producer_count()` counts producers (distinct reads);
`GraphqlEngine::live_subscriber_count()` counts subscription streams.

## Failed live executions

A live execution that fails (for example a storage error or statement timeout)
has no result. The server sends exactly one **failure frame** and then completes
the operation. The failure ends that subscription: the server sends no more
frames for it and never attaches a later execution's metadata to the failure
frame. A failure frame has:
frame. A failed shared execution ends every subscription that shares it. A
failure frame has:

- `data` that is `null` or absent;
- a nonempty `errors` array, where each entry is an object with a string
Expand Down
40 changes: 40 additions & 0 deletions src/graphql/compile/binds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,46 @@ pub enum BindValue {
Json(JsonValue),
}

/// Two binds are equal exactly when the driver receives the same value: an
/// `F64` by its bits, a `Json` by the text it is bound as. Live sharing keys
/// on this, so equal binds must never be told apart by SQL.
impl PartialEq for BindValue {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(Self::Null, Self::Null) => true,
(Self::Bool(a), Self::Bool(b)) => a == b,
(Self::I64(a), Self::I64(b)) => a == b,
(Self::F64(a), Self::F64(b)) => a.to_bits() == b.to_bits(),
(Self::Text(a), Self::Text(b)) => a == b,
(Self::Bytes(a), Self::Bytes(b)) => a == b,
(Self::Json(a), Self::Json(b)) => bound_text(a) == bound_text(b),
_ => false,
}
}
}

impl Eq for BindValue {}

/// The text a `Json` bind is sent as (see `apply_binds!`).
fn bound_text(value: &JsonValue) -> String {
value.to_string()
}

impl std::hash::Hash for BindValue {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
std::mem::discriminant(self).hash(state);
match self {
Self::Null => {}
Self::Bool(value) => value.hash(state),
Self::I64(value) => value.hash(state),
Self::F64(value) => value.to_bits().hash(state),
Self::Text(value) => value.hash(state),
Self::Bytes(value) => value.hash(state),
Self::Json(value) => bound_text(value).hash(state),
}
}
}

/// Give a numeric column one bind type.
///
/// The Postgres driver prepares a statement once per connection and reuses
Expand Down
16 changes: 16 additions & 0 deletions src/graphql/engine/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ impl GraphqlEngineBuilder {
pending_errors: Vec::new(),
identity: None,
websocket: WebsocketLimits::default(),
live_concurrency: crate::graphql::subscribe::DEFAULT_LIVE_CONCURRENCY,
read_stores: BTreeMap::new(),
external: Vec::new(),
}
Expand Down Expand Up @@ -628,6 +629,20 @@ impl GraphqlEngineBuilder {
self.websocket = limits;
self
}
/// Run at most `n` live executions at once (default
/// [`DEFAULT_LIVE_CONCURRENCY`](crate::graphql::subscribe::DEFAULT_LIVE_CONCURRENCY)).
/// Subscribers to the same read share one execution per change; this
/// bounds how many distinct reads (first reads, refreshes and catch-ups
/// together) hold a pool connection at a time, so a write burst cannot
/// starve one-shot queries. Must be at least 1.
pub fn live_concurrency(mut self, n: usize) -> Self {
if n == 0 {
self.pending_errors
.push("live_concurrency must be at least 1".into());
}
self.live_concurrency = n;
self
}
/// Serve an external read source's roots (queries, `@live`
/// subscriptions, the client manifest); see
/// [`ExternalReadSource`](crate::graphql::surface::ExternalReadSource).
Expand Down Expand Up @@ -1112,6 +1127,7 @@ impl GraphqlEngineBuilder {
schemas,
graphiql_schemas,
change_hub,
live: crate::graphql::subscribe::LiveRegistries::new(self.live_concurrency),
dialect,
identity: IdentityResolver::new(identity),
websocket: self.websocket,
Expand Down
3 changes: 3 additions & 0 deletions src/graphql/engine/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,8 @@ pub(crate) struct EngineInner {
/// manifest-fingerprinted execution limits.
pub graphiql_schemas: HashMap<String, async_graphql::dynamic::Schema>,
pub change_hub: crate::graphql::subscribe::ChangeHub,
/// Shared live reads ([`crate::graphql::subscribe`]).
pub(crate) live: crate::graphql::subscribe::LiveRegistries,
pub dialect: SqlDialect,
/// How HTTP and WebSocket requests become sessions (see `identity` module).
pub(crate) identity: IdentityResolver,
Expand Down Expand Up @@ -350,6 +352,7 @@ pub struct GraphqlEngineBuilder {
pub(crate) pending_errors: Vec<String>,
pub(crate) identity: Option<IdentityConfig>,
pub(crate) websocket: WebsocketLimits,
pub(crate) live_concurrency: usize,
pub(crate) read_stores: BTreeMap<String, crate::graphql::read_store::ReadStore>,
pub(crate) external: Vec<Arc<dyn crate::graphql::surface::ExternalReadExecutor>>,
}
5 changes: 0 additions & 5 deletions src/graphql/engine/delivery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,6 @@ fn ineligible() -> Response {
)
}
impl GraphqlEngine {
/// Active origin live-query producers for delivery diagnostics.
pub fn live_subscriber_count(&self) -> usize {
self.inner.change_hub.subscriber_count()
}

pub(super) fn enable_delivery_capture(
&self,
session: &Session,
Expand Down
12 changes: 12 additions & 0 deletions src/graphql/engine/public_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,4 +203,16 @@ impl GraphqlEngine {
pub fn change_hub(&self) -> &crate::graphql::subscribe::ChangeHub {
&self.inner.change_hub
}

/// Active live producers: one per distinct shared live read, however
/// many subscribers follow it.
pub fn live_producer_count(&self) -> usize {
self.inner.change_hub.subscriber_count()
}

/// Active live subscription streams. Identical reads share one producer
/// ([`live_producer_count`](Self::live_producer_count)).
pub fn live_subscriber_count(&self) -> usize {
self.inner.live.subscriber_count()
}
}
Loading
Loading