Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@ recognisable path forward.

### Added

- **System control across servers** (multi-server installs). The daemon and
system routes (`/api/v3/daemons`, `/api/v3/system/*`) take
`?server_id=<n>` to act on another server, relayed to its zm-api with the
caller's own token, or `?server_id=all` to act on every server and get a 207
with one result per server. Applying a run state
(`POST /api/v3/system/state`) now also tells every other server to restart
its daemons, and the response gains an optional `servers` list. The request
gains an optional `daemons_only` flag, which restarts daemons and writes
nothing. Without `server_id`, everything behaves as before on a
single-server install.
- **Multi-server installs** (#230). Every ZoneMinder server runs a zm-api,
and a request for a monitor another server runs is relayed to the zm-api
there with the caller's own token: live HLS, WHEP, snapshots, alarms, PTZ,
Expand All @@ -18,7 +28,14 @@ recognisable path forward.
settings name the other servers where the default (this server's scheme and
port on `Servers.Hostname`) is wrong. `GET /api/v3/servers` gains `api_url`
and `is_this_server`. See the multi-server guide for the limits: WebSocket
signalling, system-wide control and recordings are not relayed yet.
signalling is not relayed.
- **Recordings on per-server storage** (multi-server installs). A storage
area with a `ServerId` holds its files on that server only. A request for
an event's video, frames, thumbnail, synopsis or generated videos, or to
delete the event, is now relayed to the server that holds the files, with
the caller's own token. Storage with no `ServerId` is shared and is never
relayed. Export and the storage list act on what this server can read, and
say so. Media jobs wait for the server that can read the event.
- **Live event stream**, `GET /api/v3/events/stream` (Server-Sent Events):
`event_start`, `event_end` and `event_updated` (notes changed, where object
detection writes) for every monitor the caller may see, and each monitor's
Expand Down Expand Up @@ -268,6 +285,14 @@ recognisable path forward.

### Fixed

- **The retention reaper and the maintenance audit no longer act on another
server's storage.** On a multi-server install they ran against every
`Storage` row. They could not see a per-server area's files, so the audit
could take every event on it for one with no media and delete the rows, and
the reaper measured the wrong disk. Both now skip a storage whose
`ServerId` is another server, and say so once in the log. The audit's
empty-event sweep and the event audit report also leave those events alone.

- **`POST /events` works.** It never set `Events.StateId`, which is NOT NULL
with no default, so every create failed with 500. It now records the
active run state, or 0, as zmc does.
Expand Down
46 changes: 43 additions & 3 deletions book/src/guide/multi-server.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,28 @@ What is relayed:
- a monitor's state (start, stop, restart), its pipeline, and edits to and deletion of its row
- running a monitor action

What stays on the server the client asked: anything that only reads or writes the database, such as listing monitors, reading a monitor's row, events and users.
- an event's recording and files (see [Recordings](#recordings))

What stays on the server the client asked: anything that only reads or writes the database, such as listing monitors, reading a monitor's row, listing events, reading or editing an event's row, tags and users.

## Recordings

ZoneMinder gives each storage area an optional `ServerId`:

- **No `ServerId` (or 0): shared storage.** Every server can read and delete its files, over NFS for example. Requests for these events are answered by whichever server was asked.
- **A `ServerId`: local storage.** Only that server can read its files. A request for one of its events' files goes to that server.

Relayed for an event on another server's storage: playback and HLS, the event's video, thumbnail and frame images, event info, synopsis, generated videos (list, create, download, delete), and `DELETE /api/v3/events/{id}`, so the server that has the files removes them. This server first checks that the caller may see the event's monitor.

Not relayed, because the database holds all they need: the event list, an event's row and its edits, tags, notes, and the signed link for playback (the link is valid on every server, and the requests that use it are relayed).

Background jobs leave another server's storage alone. The retention reaper, the maintenance audit (both its database and its filesystem checks) and the event audit report skip a storage area that names another server, and the log says so once. Each server's own jobs cover its own storage, so run them on every server. A media job for an event whose files are on another server stays queued until that server's worker takes it. The storage list shows no disk figures for another server's area, since the path on this server is not that disk.

Several events in one request are not split across servers:

- `POST /api/v3/events/export` packs the files of the events on storage this server can read. For an event on another server's storage it adds the details (`event.json`, `frames.json`) and a `files-unavailable.txt` naming the server. Ask that server for the files.

An event with no storage id is treated as shared.

## Setup

Expand Down Expand Up @@ -50,12 +71,31 @@ If the servers use certificates from a private CA, point `cluster.tls_ca_file` a
| 421 `belongs to server 2, which has no address` | The `Servers` row has no hostname and `cluster.peers` names none. |
| 421 `the servers disagree` | Two servers each think the other runs the monitor. Check `ZM_SERVER_ID` on both. |

## Daemons and system control

`/api/v3/daemons` and `/api/v3/system/*` act on one server's daemons. Add `?server_id=` to say which:

| `server_id` | What happens |
| --- | --- |
| absent | This server, as before. |
| a number | That server. A request for another server is relayed to its zm-api with your own token, and its answer comes back unchanged. An unknown number answers 404, and a server with no address answers 421. |
| `all` | Every server, at once. The answer is 207 with one entry per server: `server_id`, `name`, `ok`, `status`, and either `result` (what that server answered) or `error`. One server being down does not fail the request, so check each entry. This server is first. |

The permission check happens here before anything is relayed, and the other server checks it again. A request that another server relayed is always handled where it lands, whatever `server_id` says.

On an install with one server, `all` answers with a single entry whose `server_id` is `null`, and any number answers 404.

### Applying a run state

`POST /api/v3/system/state` changes monitor rows for every server in the shared database, so it reaches every server's daemons without a `server_id`. This server writes the rows once, then tells each other server to restart the daemons of its own monitors that the state names. It sends `{"state_name": ..., "daemons_only": true}`, which restarts daemons and writes nothing, so sending it twice is harmless. The response keeps its usual fields for this server and adds `servers`, one entry per other server in the same shape as above. A server that cannot be reached does not undo the change: its entry says `ok: false`, and its daemons pick up the new state at their next restart.

Run states are not aimed at one server. `server_id=all` is the same as leaving it out, and a server's number answers 400.

## Limits

- **WebRTC media goes direct.** WHEP signalling is relayed, but the video itself travels over UDP from the server that runs the camera. The viewer must be able to reach that server.
- **WebSocket signalling is not relayed.** A WebSocket for another server's monitor answers 421 with that server's address. Use WHEP, or connect to that server.
- **System-wide control is per server.** `/api/v3/daemons` and `/api/v3/system/*` act on the server that receives them.
- **Recordings are served by the server asked.** Event video and frame images are read from that server's filesystem, so they need storage every server can reach.
- **Whole-database views are not relayed.** The media job list (`/api/v3/jobs`) shows the queue this server sees: all jobs when the database holds the queue, only this server's when a file does. The range synopsis (`/api/v3/events/synopsis`) names no single event, so this server answers it.
- **A logout takes up to ten seconds to reach the other servers.** Each server re-reads the token floors from the database on that interval.
- **Rate limits see the relaying server.** A relayed request arrives from another server's address, with the client's own appended to `X-Forwarded-For`. The global limiter is off by default. If you turn it on, set `trust_proxy_headers = true` and list the other servers in `trusted_proxies`, so each client is counted on its own.

Expand Down
Loading
Loading