Files
FlySimOperationSystem/registry/README.md
T
lain 7f331565c7 Add terminal service implementation and integration tests
- Introduced terminal.h header file defining the terminal service API.
- Implemented integration tests for terminal service registration and health checks.
- Added unit tests for FFI layer, health checks, and terminal service functionality.
- Created a mock center for testing terminal registration and deregistration.
- Ensured proper error handling and response validation in tests.
2026-09-20 10:55:38 +08:00

121 lines
6.3 KiB
Markdown

# Registry
The registry implements the `control.Center` service over plaintext HTTP/2 gRPC
using ConnectRPC and Axum. It stores the active terminal directory and checks
each terminal through `grpc.health.v1.Health/Check` on its registered port.
## Run
Run from the workspace root:
```powershell
Copy-Item .env.example .env
cargo run -p registry
```
The `.env` file is optional and is read from the current working directory.
Process environment variables override file values. Relative data paths are
also resolved against the current working directory. Invalid configuration
stops startup. The actual `.env` and default data directory are git-ignored.
| Variable | Default |
| ------------------------------- | ----------------------------- |
| `REGISTRY_HOST` | `0.0.0.0` |
| `REGISTRY_PORT` | `50051` |
| `REGISTRY_DATA_PATH` | `registry/data/terminals.bin` |
| `REGISTRY_HEALTH_INTERVAL_SECS` | `10` |
| `REGISTRY_HEALTH_TIMEOUT_SECS` | `3` |
Ports must be in `1..=65535`; health durations must be positive integer seconds.
Ctrl+C stops new work, closes subscription streams, cancels active health checks,
and allows up to five seconds for HTTP connections to drain after directory
transactions finish.
## Directory Contract
- Registration requires a nonempty host, a reachable port in `1..=65535`, and a
nonempty MAC. Metadata is stored as provided; it is not interpreted here.
- MAC identity is `trim().to_ascii_lowercase()`. Separator characters are not
rewritten. Logical identifiers are accepted as well as physical MAC addresses.
- IDs are UUID v5 values in namespace `9a55cb17-4014-5712-8c2a-64d47c9b613a`, using
the normalized MAC's UTF-8 bytes. Do not change this namespace or normalization:
the same identity retains its ID across restarts and explicit deregistration.
- Identical registration retries return the existing ID without writing or
publishing. Changed information retains the ID, resets health to `UNKNOWN`,
and requests a new check. Deregistering an absent ID succeeds without publishing.
- Business failures use `success=false`. Registration errors are strings;
deregistration errors are `google.protobuf.StringValue` packed into `Any`.
- Subscribers immediately receive the full current directory, including an empty
directory. Later messages are complete replacement snapshots, not deltas.
Slow readers may skip intermediate snapshots and receive the latest state.
- Directory mutations serialize, persist first, then publish. Once accepted,
a mutation completes even if its requesting client disconnects, so a retry
can safely discover the committed outcome.
## Storage And Recovery
The data file contains a Protobuf `TerminalInfos` message. Health is always
written and restored as `UNKNOWN`; runtime health changes do not write to disk.
Missing snapshots start an empty directory. Undecodable or semantically invalid
snapshots are renamed to `terminals.bin.corrupt-<timestamp>-<suffix>` and reported
on stderr before starting empty. Ordinary I/O or backup failures stop startup.
Writes use a synchronized temporary file in the same directory and `tempfile`'s
atomic replacement API, including on Windows. There is no delete/rename gap and
no separate backup recovery state. A process crash leaves either the previous
or committed snapshot. Uncommitted `.registry-snapshot-*` files are ignored and
can be removed while the registry is stopped. This is not a guarantee against
filesystem damage or sudden power loss; keep operational backups separately.
Only one registry process may own a data path. Shared storage, replication,
configuration hot reload, authentication, and TLS are not implemented. Deploy
on a trusted network or behind an appropriately secured proxy; registration
authorizes outbound health connections to the supplied host and port.
## Health
Checks start immediately on startup and after directory changes, then run at the
configured interval with at most 32 concurrent probes. The timeout includes DNS,
TCP/HTTP2 connection setup and the RPC. Requests use an empty service name.
Responses update health; connection errors, RPC errors and timeouts become
`NOT_SERVING`. Offline terminals remain registered until explicitly removed.
Version checks discard results from a previous registration or address update.
Terminal exposes both Health RPCs on its existing port. Empty service names and
`control.Terminal` report `SERVING` only when the terminal lifecycle is `Ready`.
Unknown names return `NotFound` from Check and a persistent `SERVICE_UNKNOWN`
stream from Watch. Health describes service readiness, not simulation state.
## Verify
All test implementations and test-only helpers live under `tests/`:
- `tests/unit/{config,health,rpc,service,storage}.rs` contains module-level tests.
[tests/unit/mod.rs](tests/unit/mod.rs) declares these modules as `unit_tests`.
- [tests/integration/runtime.rs](tests/integration/runtime.rs) tests real RPC
calls and Terminal interoperability; [tests/integration/mod.rs](tests/integration/mod.rs)
declares it under `integration_tests`.
Like Terminal, [src/lib.rs](src/lib.rs) loads both test groups through `#[cfg(test)]`
and `#[path]`. Production modules do not declare individual test modules. Tests
use explicit crate imports and narrowly scoped `pub(crate)` functions, without
adding public library APIs or exposing the directory's private state.
The cancellation test uses a current-thread runtime to drop a polled caller
before its spawned transaction is scheduled, without accessing internal locks.
Both test groups run in the library test target; there is no standalone
`--test integration` target. Each test module is loaded exactly once.
```powershell
cargo test -p registry
cargo test -p registry --lib unit_tests
cargo test -p registry --lib integration_tests::runtime
cargo test -p registry -p terminal
cargo test --workspace
```
Tests cover configuration, identity, persistence failures and corruption,
request cancellation, stale probe results, subscriptions, health recovery and
timeouts, real CenterClient calls, and a real Terminal started through its C ABI
that remains online while the registry restarts.