Skip to content

feat(cluster): daemon and system control reach every server - #285

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

SteveGilvarry merged 2 commits into
masterfrom
feat/cluster-system

Conversation

@SteveGilvarry

Copy link
Copy Markdown
Member

Why

Third PR for multi-server installs (#230). Stacked on #284: until that merges, this diff shows its commit too. Review the last commit, feat(cluster): daemon and system control reach every server.

/api/v3/daemons and /api/v3/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 through the first.
  • Applying a run state rewrote every monitor's row in the shared database but restarted daemons on one server. The others kept running the old configuration.

What changes

Every daemon and system route takes an optional server_id.

server_id Behaviour
absent This server. Same response as before.
a number That server. The request is relayed with the caller's own token and its answer comes back unchanged. An unknown id is 404.
all Every server, concurrently. One result per server.

server_id=all answers 207 Multi-Status, not 200. The body is { "servers": [ { server_id, name, ok, status, result | error } ] }. One server being down does not fail the others: its entry has ok: false and an error naming it. I chose 207 because these routes already document a 200 body, and returning a different shape under 200 would be a breaking change the OpenAPI compatibility check rejects.

POST /system/state reaches every server by default. It writes the database once, on the server that got the request. It then sends each other server the same route with a new daemons_only: true flag. A server that receives that writes nothing and passes nothing on. It only restarts its own daemons for the monitors the state names. The response keeps success and message and gains an optional servers list. A numeric server_id on this route is 400, because the change is global.

Loop guard. A request that carries X-ZM-Forwarded-By is always handled locally, whatever server_id says.

Why not relay the same state request, or a restart

Both were tried on paper and neither works in passive mode:

  • Relaying the plain POST /system/state: the other server compares rows before and after to decide what to restart. The rows have already changed, so it restarts nothing. It would also write them a second time.
  • POST /system/restart answers 503 in passive mode, where ZoneMinder supervises the daemons.

Worth a reviewer's eye

  • Passive-mode servers restart every monitor of theirs that the state names, not only the changed ones, because the rows are already new when they act. Passing the changed ids would avoid that at the cost of a larger request.
  • A server on an older build ignores daemons_only. It rewrites the same state, which is idempotent, and in passive mode restarts nothing.
  • No per-server cap on a fan-out. A stuck server is bounded by the cluster client's connect and read timeouts.
  • Single-server installs. server_id=all returns one entry with server_id: null. A number that is not this host is 404.

Checked

  • cargo fmt --all -- --check and cargo clippy --all-targets --all-features -- -D warnings are clean
  • Library tests: 1344 passed, 0 failed
  • Database-backed suite: 581 passed, 0 failed, including 17 new tests in tests/it_cluster_system.rs
  • The new tests cover a single target, all with a server down, the loop guard, permission refused before anything is relayed, a single-server install, the state fan-out, daemons_only writing nothing, and a passive server sending zmdc stop and start for its own monitors against a fake zmdc.sock
  • scripts/check-openapi-compat.py: no breaking changes, four additive items

Not checked: the takeover-mode path for a server receiving daemons_only, and a real two-server run. Both are covered by reading only.

One known flaky test, streaming::rtsp::session::tests::a_connection_must_authorize_before_the_deadline, failed once in a full run under load and passed on every rerun. It is unrelated to this change.

Refs #230.

🤖 Generated with Claude Code

SteveGilvarry and others added 2 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>
@SteveGilvarry
SteveGilvarry merged commit 0222d51 into master Nov 18, 2026
15 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