Repository navigation
feat(cluster): relay requests for another server's monitor to that server - #283
Merged
Merged
Conversation
…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>
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
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 foundfor 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.
X-ZM-Forwarded-Byand 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 itsServers.Hostname.cluster.peersoverrides it per server id.GET /api/v3/serversgainsapi_urlandis_this_server. Both are additive.settings/base.toml, and a changelog entry.Two fixes found on the way
Worth a reviewer's eye
Servers.Protocol. That column describes ZoneMinder's web server, not zm-api.Serversrow'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.Limits, each documented in the guide
/daemonsand/system/*still act on the server that receives them.Checked locally
cargo fmt --all -- --checkandcargo clippy --all-targets --all-features -- -D warningsare cleancargo test --all-features: 1410 passed, 0 failedtests/it_cluster.rsscripts/check-openapi-compat.pyreports additive changes only.openapi.jsonis regeneratedChecked 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 commit4ae11cc.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 /serversgave node 1's address ashttps://although zm-api there serves plain HTTP. The scheme was read from whether a[server.tls]section exists, and the packaged settings carry one withenabled = false. It now comes from whether TLS is enabled, with a unit test.Not checked on the lab:
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