Repository navigation
feat(cluster): WebSockets, the live event stream and live lists span servers - #286
Merged
Merged
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.
Upgradeheader 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/streamsends each monitor'salarm_statefrom shared memory, which exists only for this server's monitors.unknown. It now reads only the monitors this server runs.alarm_stateonly, and only for monitors that server runs.event_start,event_endandevent_updatedcome from the sharedEventstable, which every server already reads in full. A stream request that carriesX-ZM-Forwarded-Byis served alarms-only, with no table events, no replay and no further subscriptions.Last-Event-IDand?since=read the database, which covers every server. Alarm states are never replayed.Status and live lists
GET /api/v3/monitor-status/{id}is relayed to the monitor's server.GET /api/v3/live/sessionsand/live/sourcestake the sameserver_id=<n>|alltarget as the daemon routes in feat(cluster): daemon and system control reach every server #285, through the same middleware.Worth a reviewer's eye
cluster.tls_ca_fileand 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.rustls-tls-native-rootsontokio-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/sessionsanswers an array.alarm_stateon the client. Nounknownis sent.it_cluster.rschanged 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.pyreports 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.jsonis regenerated in this PR, so CI compares like with like.Limits that remain
GET /api/v3/monitor-statusstill lacks the shared-memory fields for other servers' monitors.Checked
cargo fmt --all -- --checkandcargo clippy --all-targets --all-features -- -D warningsare cleancargo test --all-features: 1433 passed, 0 failedtests/it_cluster_streams.rsNot checked:
wss://and thetls_ca_filepath, 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