feat(rpc): implement health check service and error handling

- Added `RpcError` enum for comprehensive error handling in RPC operations.
- Introduced `HealthReporter` and `HealthService` for managing service health statuses.
- Implemented health check and watch functionality in the health service.
- Created integration tests for health check service, covering various scenarios.
- Added unit tests for client options validation and stream functionality.
- Established logging infrastructure for terminal and registry components.
- Implemented logging lifecycle management and conflict resolution in tests.
This commit is contained in:
2026-10-09 15:57:35 +08:00
parent 7f331565c7
commit bbb67a68ab
65 changed files with 3313 additions and 695 deletions
+71 -9
View File
@@ -18,19 +18,59 @@ 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` |
- `HOST`: `0.0.0.0`.
- `PORT`: `50051`.
- `DATA_PATH`: `registry/data/terminals.bin`.
- `HEALTH_INTERVAL_SECS`: `10`.
- `HEALTH_TIMEOUT_SECS`: `3`.
- `DATA_FILE_COUNT`: `10`, independent of logging retention.
- `LOG_LEVEL`: `info`.
- `LOG_DIR`: `logs/registry`.
- `LOG_MAX_FILES`: `10`.
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.
## Logging
The backend is provided by [flysim-log](../log/README.md). This component only
maps configuration/errors and controls the process-owned logging guard.
Business messages use `log::info!`, `log::warn!` and `log::error!`. `tracing-log`
forwards these records to `tracing-subscriber`; native tracing events/spans use
the same subscriber. `env_logger` is not initialized. Each event is formatted
once for stderr and once for the file, with UTC timestamp, level and target.
Native tracing fields are preserved. Files contain no ANSI color sequences;
stderr colors are enabled only when connected to a terminal.
`LOG_LEVEL` accepts `off`, `error`, `warn`, `info`, `debug` or `trace`, ignoring
case. `LOG_DIR` must be nonempty and `LOG_MAX_FILES` must be positive. Environment
variables override `.env` values; `RUST_LOG` is not used by this configuration.
Relative log paths resolve against the working directory.
Files are named `registry.YYYY-MM-DD.log` and roll daily on UTC boundaries when
new records arrive. The appender retains at most `LOG_MAX_FILES` matching files;
this is a file-count cap, not a guarantee of exactly that many files or days.
Old matching files can be removed on initialization and rotation. Use a dedicated
directory per process: do not share it with another registry or place unrelated
files with the same `registry` prefix and `.log` suffix there. The default `logs/`
directory is git-ignored. Rotation does not cap individual file size.
File writes use an 8192-entry bounded queue and a worker thread. The producer
blocks when the queue is full instead of dropping logs; stderr writes are
synchronous. Slow storage or consoles can therefore delay the calling thread.
The worker guard is retained until shutdown and dropped after the final log
record to drain buffered writes. This is not a durability guarantee for disk
errors, process termination, power loss, or a stalled writer at shutdown.
Logging is initialized before snapshot recovery, so corruption warnings reach
both outputs. Configuration/appender initialization failures are reported using
`log` through a temporary stderr-only subscriber and exit unsuccessfully.
Runtime failures are logged before releasing the file worker. The binary owns
global logging; library consumers must configure their own logging explicitly.
## Directory Contract
- Registration requires a nonempty host, a reachable port in `1..=65535`, and a
@@ -58,7 +98,7 @@ 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.
as warning logs 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
@@ -74,6 +114,19 @@ authorizes outbound health connections to the supplied host and port.
## Health
Registry exposes `grpc.health.v1.Health` on the same port as `control.Center`,
using [proto's shared Health service](../proto/README.md). Empty service name and
`control.Center` start as NOT_SERVING and become SERVING once the directory is
loaded, the listener is bound and the serving runtime starts. Shutdown marks
both NOT_SERVING before signaling streams to close. Readiness and shutdown use
the same state lock, so a concurrent startup cannot undo shutdown.
This describes Registry readiness, not downstream health: an empty directory
or offline terminals do not make Registry unhealthy. It does not continuously
probe disk writability. Background monitor/server failures trigger the existing
shutdown path. A slow watcher is not guaranteed to receive the final NOT_SERVING
message before the stream ends.
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.
@@ -81,6 +134,10 @@ 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.
Endpoint construction, the single Health call, server hosting and watch streams
come from `proto::rpc`. Polling, concurrency, error-to-NOT_SERVING mapping,
registration business logic and stale-result checks remain in Registry.
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`
@@ -90,7 +147,7 @@ stream from Watch. Health describes service readiness, not simulation state.
All test implementations and test-only helpers live under `tests/`:
- `tests/unit/{config,health,rpc,service,storage}.rs` contains module-level tests.
- `tests/unit/{config,health,logging,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)
@@ -109,6 +166,7 @@ Both test groups run in the library test target; there is no standalone
```powershell
cargo test -p registry
cargo test -p registry --lib unit_tests
cargo test -p registry --lib unit_tests::logging
cargo test -p registry --lib integration_tests::runtime
cargo test -p registry -p terminal
cargo test --workspace
@@ -118,3 +176,7 @@ 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.
Logging tests isolate global bridge/subscriber installation in child processes
and verify Registry's dual output and recovery messages. File retention,
reopen/append, filtering and backpressure tests live in the shared log crate.