Skip to content

feat(cluster): relay requests for another server's monitor to that server - #283

Merged
SteveGilvarry merged 2 commits into
masterfrom
feat/cluster-forwarding
Nov 18, 2026
Merged

SteveGilvarry merged 2 commits into
masterfrom
feat/cluster-forwarding

Conversation

@SteveGilvarry

@SteveGilvarry SteveGilvarry commented Nov 18, 2026 •

Copy link
Copy Markdown
Member

Why

On a multi-server install zm-api lists every monitor but can only serve the ones its own server runs. Snapshot and live HLS answer 404 MONITOR not found for the rest, and a forced alarm answers 503 (#230). Shared memory, zmc's stream socket and the daemon control socket exist only on the server that runs a monitor.

This is the first of four planned PRs. It covers everything addressed to one monitor, which is everything #230 reported.

How it works

Every server runs a zm-api. A request that reaches the wrong one is sent to the right one, and the answer is streamed back. Clients keep one address.

  • The servers talk over the v3 API itself. No new protocol or dependency.
  • The relayed request carries the caller's own token. The owning server authenticates the user and applies their permissions as it would for a direct request. No server holds a credential that can act as anyone. The cost is one setup step: every server must share the same JWT key files.
  • This server checks first. The relay layer sits behind the existing guards, so the caller is authenticated and must be able to see the monitor before anything leaves this server.
  • No loops. A relayed request carries X-ZM-Forwarded-By and is never relayed again. Two servers that disagree about an owner answer 421.

Relayed: live HLS, live start and stop, live stats, WHEP, snapshots, alarms, PTZ, monitor state, pipeline, action runs, and edits to and deletes of a monitor's row. Reading a monitor's row, and anything else that only touches the database, stays on the server asked.

What changes

  • src/cluster/ (new). Which server owns a monitor, where each server's zm-api is, and the relay middleware.
  • [cluster] settings. A server's address defaults to this server's scheme and port on its Servers.Hostname. cluster.peers overrides it per server id.
  • GET /api/v3/servers gains api_url and is_this_server. Both are additive.
  • New errors. 502 when the owner does not answer, or refuses a token this server accepted (the keys differ). 421 when the owner has no address or the request was already relayed.
  • Docs. A multi-server guide in the book, the settings in settings/base.toml, and a changelog entry.

Two fixes found on the way

  • Takeover mode could capture a camera twice. zm-api's daemon manager started any monitor it was asked to, whichever server owned it. A monitor created for, or moved to, another server is now stopped here and started by its owner.
  • A logout did not reach the other servers. Access tokens are checked against a floor held in memory, which only the server that handled the logout raised. On a multi-server install each server now re-reads the floors every 10 seconds.

Worth a reviewer's eye

  • The default peer address uses this server's own scheme, not Servers.Protocol. That column describes ZoneMinder's web server, not zm-api.
  • A System editor can point a Servers row's hostname anywhere, and relayed requests, with their tokens, go there. That needs the same privilege that already controls the whole install, but it is a new place a token can travel.
  • The owner lookup is cached for 5 seconds, so a moved monitor is followed within that time.
  • Single-server installs do no extra work. With no server id the layer returns at its first check.

Limits, each documented in the guide

  • WebSocket signalling is not relayed. It answers 421 with the owner's address. WHEP is relayed.
  • WebRTC media travels direct from the owning server over UDP.
  • /daemons and /system/* still act on the server that receives them.
  • Recordings are read from the filesystem of the server asked.
  • A monitor created through ONVIF onboarding for another server is not announced to its owner.

Checked locally

  • cargo fmt --all -- --check and cargo clippy --all-targets --all-features -- -D warnings are clean
  • cargo test --all-features: 1410 passed, 0 failed
  • Database-backed suite: 552 passed, 0 failed, including 17 new tests in tests/it_cluster.rs
  • One of those runs two real zm-api routers: server 1 relays an edit to server 2 over a real socket, and server 2 accepts the same token and saves it
  • scripts/check-openapi-compat.py reports additive changes only. openapi.json is regenerated

Checked on a two-node lab

The holodeck multi-server setup that produced #230: two ZoneMinder nodes (master, 1.39.41) on one database with simulated cameras, a zm-api beside each in passive mode, shared JWT keys, and each told the other's address through cluster.peers. Monitors 1, 3 and 5 run on node 1, and 2, 4 and 6 on node 2. Every request below went to node 1, on commit 4ae11cc.

Check Monitors 2, 4, 6 (run by node 2) Before this PR
Snapshot 200, a 1280x720 JPEG 404
Live start 200 404
HLS master and media playlist 200 404
HLS video init and a segment fetched, decoded as H.264 1280x720, 30 frames n/a
Alarm on, then off 200 503

Node 2's zm-api held the live sessions for 2, 4 and 6, and node 1 held only its own. Monitor 1, which node 1 runs, answered as before.

The lab found one bug, fixed in 7616444. GET /servers gave node 1's address as https:// although zm-api there serves plain HTTP. The scheme was read from whether a [server.tls] section exists, and the packaged settings carry one with enabled = false. It now comes from whether TLS is enabled, with a unit test.

Not checked on the lab:

  • the corrected address on a rebuilt image. The rebuild ran out of memory on the test machine. The unit test covers the logic
  • an edit relayed to node 2 restarting zmc there and nowhere else
  • a logout on one node being refused on the other within ten seconds
  • the WebSocket refusal

The integration tests cover the last three against a stub or a second router, but not against real daemons.

Refs #230. It does not close it: system-wide control and recordings are still per server.

🤖 Generated with Claude Code

SteveGilvarry and others added 2 commits November 18, 2026 19:33
…rver

On a multi-server install zm-api listed every monitor but could only
serve its own server's: shared memory, zmc's stream socket and the daemon
control socket exist only where a monitor runs. Snapshot and live HLS
answered 404 for the rest, and a forced alarm 503 (#230).

Every server now runs a zm-api, and a request that reaches the wrong one
is sent to the right one with the caller's own token, and the answer is
streamed back. Clients keep one address. The owning server authenticates
the user and applies their permissions itself, so no server holds a
credential that can act as anyone. Every server must share the same JWT
key files.

Relayed: live HLS, live start and stop, WHEP, snapshots, alarms, PTZ,
monitor state, pipeline, action runs, and edits to and deletes of a
monitor's row. This server authenticates the caller and checks they can
see the monitor before anything leaves it. A relayed request is never
relayed again.

A server's address defaults to this server's scheme and port on its
Servers.Hostname. The new [cluster] settings override it per server id.
GET /api/v3/servers gains api_url and is_this_server.

Also:
- In takeover mode the daemon manager started any monitor it was asked
  to. A monitor created for, or moved to, another server is now stopped
  here and started by its owner, so a camera is not captured twice.
- A logout only raised the token floor in the memory of the server that
  handled it. On a multi-server install each server re-reads the floors
  every 10 seconds.

Not relayed yet: WebSocket signalling (answers 421 with the owner's
address), system-wide daemon control, and recordings.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
A peer's default address takes this server's own scheme. That was read
from whether a [server.tls] or [server.acme] section exists, and the
packaged settings carry both with enabled = false. So a server on plain
HTTP gave its peers an https:// address, which nothing answers.

The scheme now comes from whether either is enabled.

Found on the two-node holodeck lab, where GET /servers showed
https://127.0.0.1:8080 for a zm-api listening on plain HTTP.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@SteveGilvarry
SteveGilvarry merged commit da75f23 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