Skip to content

feat(cluster): WebSockets, the live event stream and live lists span servers - #286

Merged
SteveGilvarry merged 3 commits into
masterfrom
feat/cluster-streams
Nov 18, 2026
Merged

SteveGilvarry merged 3 commits into
masterfrom
feat/cluster-streams

Conversation

@SteveGilvarry

Copy link
Copy Markdown
Member

Why

Fourth and last planned PR for multi-server installs (#230). Stacked on #285, which is stacked on #284. Until those merge, this diff shows their commits too. Review the last commit, feat(cluster): WebSockets, the live event stream and live lists span servers.

Three kinds of request were still tied to the server that received them.

What changes

WebRTC signalling over WebSocket is relayed

A WebSocket for another server's monitor answered 421. Now this server accepts the client's upgrade, opens its own WebSocket to the same path on the owning server, and copies messages both ways until either side closes.

  • It connects to the owner before accepting the client. An owner that is down gives 502. An owner that refuses passes its own status back. A 401 gives the keys-differ 502, as the HTTP relay does.
  • The same checks run first. The caller is authenticated and must be able to see the monitor, and a request that was already relayed is never relayed again.
  • Messages keep their type: text, binary, ping, pong, and close with its code and reason. An owner that vanishes closes the client with 1011.
  • It lives in the relay layer, so any upgrade request the layer sees is covered, not only the live handler. A request with an Upgrade header that is not a valid WebSocket handshake still gets 421.

WebRTC media still travels direct from the owning server over UDP.

The live event stream covers every server

GET /api/v3/events/stream sends each monitor's alarm_state from shared memory, which exists only for this server's monitors.

  • A bug this fixes. The watcher tried shared memory for every monitor, including other servers'. On a multi-server install those monitors always reported unknown. It now reads only the monitors this server runs.
  • It follows the other servers for theirs. For each subscriber, this server opens that server's stream with the subscriber's own token and passes on alarm_state only, and only for monitors that server runs.
  • Nothing is duplicated. event_start, event_end and event_updated come from the shared Events table, which every server already reads in full. A stream request that carries X-ZM-Forwarded-By is served alarms-only, with no table events, no replay and no further subscriptions.
  • A server that is down does not break the stream. It is retried at 1, 2, 4, 8, 16 then 30 s, with one warning per outage.
  • Replay, heartbeat and stream end are unchanged. Last-Event-ID and ?since= read the database, which covers every server. Alarm states are never replayed.

Status and live lists

Worth a reviewer's eye

  • One upstream stream per subscriber per server. Ten clients on a three-server install hold twenty upstream connections. The alternative is one shared upstream with a token a server mints for itself. That needs a credential that can see every monitor, which breaks the rule the rest of this work keeps: no server holds a credential that can act as anyone.
  • cluster.tls_ca_file and WebSockets. With the file set, the WebSocket connector trusts that file's certificates only. The HTTP client trusts the file plus the system roots. Adding to the system roots there needs a new direct dependency. Documented in the guide.
  • A feature flag was enabled on an existing dependency: rustls-tls-native-roots on tokio-tungstenite. No new crate enters the lockfile.
  • ServerResult.result (from feat(cluster): daemon and system control reach every server #285) loosened from an object to any JSON, because /live/sessions answers an array.
  • While a server is down, its monitors keep their last known alarm_state on the client. No unknown is sent.
  • Servers are resolved when a stream opens. One added later is picked up by the next connection.
  • A test in it_cluster.rs changed meaning. "A WebSocket is refused with the owner's address" became "an upgrade that is not a WebSocket handshake is refused", since a real handshake is now relayed.

OpenAPI

Checked against the previous openapi.json, scripts/check-openapi-compat.py reports one breaking change: GET /api/v3/live/sources, "response shape changed: was None, now AllServersResponse". I believe that is a false positive. The route's 200 never had a documented schema, so the checker now takes the new 207 as its first documented 2xx. The 200 response itself is unchanged. Everything else is additive. openapi.json is regenerated in this PR, so CI compares like with like.

Limits that remain

  • The list route GET /api/v3/monitor-status still lacks the shared-memory fields for other servers' monitors.

Checked

  • cargo fmt --all -- --check and cargo clippy --all-targets --all-features -- -D warnings are clean
  • cargo test --all-features: 1433 passed, 0 failed
  • Database-backed suite: 599 passed, 0 failed, including 18 new tests in tests/it_cluster_streams.rs
  • The WebSocket tests use a real listener and a real client: text and binary both ways, subprotocol echo, close from either side, a dropped client, an owner that is down, a 403 and a 401 from the owner, a caller who can't see the monitor, the loop guard, and this server's own monitor
  • The stream tests cover an alarm arriving from the other server, an alarm for a monitor it doesn't run being dropped, no duplicated table events, a hidden monitor filtered out, no upstream for a relayed request, the other server being down, and a single-server install

Not checked: wss:// and the tls_ca_file path, real browsers, sockets idle for a long time, token expiry on an upstream stream, and two real servers with zmc's shared memory. The two-node lab confirmed #283's HTTP relay, but it has not been run against this PR.

Refs #230.

🤖 Generated with Claude Code

SteveGilvarry and others added 3 commits November 18, 2026 21:15
Storage.ServerId says which server holds a storage area's files. NULL or
0 is shared storage every server reaches; any other value is local to
that server. zm-api ignored the column everywhere, so on a multi-server
install with per-server storage:

- the retention reaper and the audit ran against storages whose files
  this server cannot see. The audit could take every event there for
  media-less, and the reaper measured the wrong disk
- event video, frame images and thumbnails failed on the wrong server
- deleting an event there removed the row and left the files
- a media job could be claimed by a server that cannot read the event

Background jobs now leave another server's storage alone, files and rows
both: the reaper, the audit's filesystem pass and its removal of empty
events, the event audit's media check, and the media job worker. The
storage endpoints report no filesystem figures for it.

Requests that read or change an event's files are relayed to the server
that holds them, as monitor requests already are: playback, HLS, frame
images, thumbnails, synopsis, generated videos and DELETE /events/{id}.
Routes that only touch the event's row stay on the server asked. The
caller must be able to see the event's monitor before anything is
relayed.

An export that includes an event on another server's storage carries
its details and a note naming the server, without the media.

Refs #230.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
/daemons and /system/* acted only on the server that received the
request. On a multi-server install an admin could not see or control
another server's daemons, and applying a run state rewrote every
monitor's row but restarted daemons on one server only. The others kept
running the old configuration.

Every daemon and system route now takes an optional server_id:
- absent: this server, as before
- a number: that server. The request is relayed there with the caller's
  own token, and its answer returned unchanged
- all: every server, concurrently, with one result per server. The
  answer is 207, and one server failing does not fail the rest

POST /system/state writes the database once, then tells each other
server to bring its own daemons in line. That call carries a new
daemons_only flag: the receiving server writes nothing and passes
nothing on, so repeating it is harmless. The response keeps its fields
and gains an optional list of the other servers' outcomes.

A request another server already relayed is always handled locally. This
server authenticates the caller and checks System permission before
anything leaves it.

Refs #230.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…servers

Three kinds of request were still tied to the server that received them.

WebRTC signalling. A WebSocket for another server's monitor answered
421. The relay now accepts the client's upgrade and opens its own
WebSocket to the owning server, then copies messages both ways until
either side closes. It connects to the owner first, so an owner that is
down or refuses gives the client a normal HTTP error. The caller is
authenticated and must be able to see the monitor before anything is
sent. WebRTC media still travels direct from the owning server.

Live event stream. GET /events/stream reads alarm state from shared
memory, which only exists for this server's monitors, and it tried to
read it for every monitor. So another server's monitors always showed
"unknown". This server now reads only its own, and follows each other
server's stream for theirs, with the subscriber's own token. It takes
only alarm_state, and only for monitors that server runs, so events from
the shared Events table are never duplicated. A server that is down does
not break the stream: it is retried with a capped backoff.

Status and lists. GET /monitor-status/{id} is relayed to the monitor's
server. /live/sessions and /live/sources take the same server_id target
as the daemon routes.

Refs #230.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@SteveGilvarry
SteveGilvarry merged commit a61d809 into master Nov 18, 2026
23 checks passed
@github-actions github-actions Bot locked and limited conversation to collaborators Nov 18, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant