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:
+116
-12
@@ -30,7 +30,8 @@ The C header declares functions and callbacks, not C Protobuf message structs.
|
||||
|
||||
## Host Integration
|
||||
|
||||
1. Call `terminal_service_create` to obtain an opaque handle.
|
||||
1. Call `terminal_service_create` to obtain an opaque handle. To enable built-in
|
||||
logs, call `terminal_logging_init` once before starting any terminal.
|
||||
2. Serialize `control.TerminalInfo` and call `terminal_service_configure`.
|
||||
3. Register command callbacks and event definitions/callbacks. Keep callback code
|
||||
and user data alive for every possible invocation.
|
||||
@@ -39,8 +40,9 @@ The C header declares functions and callbacks, not C Protobuf message structs.
|
||||
the listener and center registration are ready.
|
||||
5. Push simulation snapshots with `terminal_service_push_status`; report event
|
||||
completion with `terminal_service_complete_event` when host work finishes.
|
||||
6. Call `terminal_service_stop`, inspect any error, then destroy the stopped
|
||||
handle with `terminal_service_destroy`.
|
||||
6. Call `terminal_service_stop` and inspect any error. After all terminals and
|
||||
callbacks have stopped, call `terminal_logging_shutdown` if built-in logs were
|
||||
enabled, then destroy the remaining handle with `terminal_service_destroy`.
|
||||
|
||||
All functions are synchronous at the ABI boundary. In particular, start and stop
|
||||
perform blocking network work. Invoke them from a normal host/control thread,
|
||||
@@ -60,8 +62,8 @@ a handle while another thread may still use it.
|
||||
|
||||
Use an address that the center and control clients can reach. There is no separate
|
||||
bind/advertised address, NAT discovery, or automatic conversion of wildcard hosts.
|
||||
The center URI is built as `http://<center_host>:<center_port>`; bracket an IPv6
|
||||
literal in `center_host`, for example `[::1]`.
|
||||
The shared `RpcEndpoint` builds the center URI from host and port. IPv6 literals
|
||||
can be supplied raw (`::1`) or bracketed (`[::1]`).
|
||||
|
||||
When the requested terminal port is zero, the actual bound port is written into
|
||||
the stored `TerminalInfo` before registration and returned to the host. This also
|
||||
@@ -71,6 +73,79 @@ stopped to request another OS-assigned port.
|
||||
Command callback registration does not populate `meta_info.supported_type` or
|
||||
its command descriptions automatically. The host must supply matching metadata.
|
||||
|
||||
## Logging
|
||||
|
||||
The backend is provided by [flysim-log](../log/README.md). Terminal retains
|
||||
the module-wide FFI state, error mapping and explicit guard ownership; extracting
|
||||
the backend does not change any C ABI signature or DLL-global restrictions.
|
||||
|
||||
Terminal records low-frequency lifecycle events with `log` macros: listener and
|
||||
registration success, registration/deregistration failures and retries, shutdown
|
||||
timeouts, and local stop. It does not log command payloads or high-frequency
|
||||
status pushes. The same events reach stderr and daily rotating files through
|
||||
`log -> tracing-log -> tracing-subscriber`; native tracing events also work.
|
||||
Calling create/start/stop never installs or shuts down global logging implicitly.
|
||||
|
||||
For a C/C++ host, use the generated declarations in [terminal.h](terminal.h):
|
||||
|
||||
```c
|
||||
void *handle = terminal_service_create();
|
||||
const char *level = "info";
|
||||
const char *directory = "logs/terminal";
|
||||
int32_t result = terminal_logging_init(
|
||||
handle,
|
||||
(const uint8_t *)level, strlen(level),
|
||||
(const uint8_t *)directory, strlen(directory),
|
||||
10
|
||||
);
|
||||
```
|
||||
|
||||
Include `terminal.h` and `<string.h>` for this snippet. Check `result` against
|
||||
`TERMINAL_OK` and retrieve details through `terminal_service_last_error`.
|
||||
The handle supplies error storage only; logging is module-wide, not owned by
|
||||
that terminal instance. UTF-8 input buffers are borrowed only for the call.
|
||||
|
||||
The level accepts `off/error/warn/info/debug/trace` case-insensitively; the log
|
||||
directory must be nonempty and the maximum file count positive. Configuration
|
||||
is passed explicitly, not read from `.env` or `RUST_LOG`. Files are named
|
||||
`terminal.YYYY-MM-DD.log`, with UTC daily rotation and at most the specified
|
||||
number of matching files (the example uses 10). Retention may remove old matching
|
||||
files during initialization/rotation; it is not an exact number of days. Use a
|
||||
dedicated directory per host process and do not store unrelated `terminal*.log`
|
||||
files there. Relative paths resolve against the host's working directory.
|
||||
|
||||
Both outputs include UTC time, level and target; files disable ANSI colors.
|
||||
stderr is synchronous. File output runs on a worker with an 8192-entry queue;
|
||||
a full queue blocks the logging thread instead of dropping records. Slow
|
||||
storage or consoles can delay the host and RPC threads. Worker-guard release
|
||||
drains buffered records during normal shutdown, but cannot guarantee delivery
|
||||
after a crash, forced termination, disk failure, or a stalled writer.
|
||||
|
||||
After stopping every terminal and in-flight callback, call
|
||||
`terminal_logging_shutdown(handle)` on a normal host thread. Repeated shutdown
|
||||
is harmless, and shutdown before initialization is a no-op. Keep a handle alive
|
||||
for reporting shutdown errors. Do not close logging from inside a callback or
|
||||
while another thread is initializing/shutting it down.
|
||||
|
||||
Initialization is once per module's logging instance. An existing logger or
|
||||
subscriber is not replaced: initialization returns `TERMINAL_ERROR_INVALID_STATE`.
|
||||
Invalid parameters return `TERMINAL_ERROR_INVALID_ARGUMENT`, and file/worker
|
||||
initialization errors return `TERMINAL_ERROR_INTERNAL`. Ordinary parameter or I/O
|
||||
failures may be corrected and retried. Installed global logger/subscriber state
|
||||
cannot be uninstalled. Installation of the two globals is not atomic: serialize
|
||||
this call with all host logging setup; a concurrent subscriber installation can
|
||||
leave the bridge installed even though initialization fails. Such a failure is
|
||||
reported and no new writer thread is retained.
|
||||
|
||||
Shutdown closes only the owned file worker, not the global logger/subscriber;
|
||||
stderr may still receive late records. Do not expect file output afterward or
|
||||
attempt to initialize again. Keep the library loaded until host process exit;
|
||||
DLL hot unloading/reloading is not supported after global logging installation.
|
||||
Rust static linking can share a host's logger/subscriber, while a separately
|
||||
loaded cdylib may have independent globals. When the host already owns logging
|
||||
in the same Rust instance, omit these initialization/shutdown calls. A terminal
|
||||
can still run without built-in logging initialized.
|
||||
|
||||
## Runtime Lifecycle
|
||||
|
||||
[RunningServer](src/runtime.rs) owns one multithreaded Tokio runtime, the Axum
|
||||
@@ -117,9 +192,12 @@ registration failure the server is signaled to close.
|
||||
|
||||
Center communication uses ConnectRPC's gRPC protocol over plaintext HTTP/2.
|
||||
Registration and deregistration each make at most three attempts, with 250 ms
|
||||
and 500 ms waits before retries. Each RPC has a three-second timeout. Connection
|
||||
setup occurs outside that timeout, so these values do not bound the total time
|
||||
spent in start or stop. A new center connection is created for each attempt.
|
||||
and 500 ms waits before retries. The shared RPC channel explicitly limits connection
|
||||
establishment to three seconds, separately from each RPC's three-second deadline.
|
||||
These are per-attempt budgets, not a three-second total for start/stop. The
|
||||
transport already had default establishment bounds; the project now sets its
|
||||
own explicit value. A new center connection is created for each attempt. No
|
||||
short default deadline is imposed on status/Health subscriptions.
|
||||
|
||||
There is no background re-registration or center heartbeat after startup.
|
||||
The [Registry](../registry/README.md) persists registrations and probes Terminal's
|
||||
@@ -152,6 +230,10 @@ Terminal and standard Health RPCs share a single ConnectRPC router and Axum
|
||||
listener. [rpc.rs](src/rpc.rs) adapts generated RPC traits to the domain service;
|
||||
[callback.rs](src/callback.rs) bridges messages to the host ABI.
|
||||
|
||||
[proto::rpc](../proto/README.md) supplies client factories, server hosting,
|
||||
snapshot streams and Health protocol behavior. Terminal still owns the Tokio
|
||||
runtime, registration retries, lifecycle mapping and server shutdown timeout.
|
||||
|
||||
### Commands
|
||||
|
||||
`Execute` requires a ready service, a registered callback and a valid simulation
|
||||
@@ -258,6 +340,11 @@ For Health, empty service names and `control.Terminal` are known. `Check` report
|
||||
Streams terminate at shutdown. Health describes readiness, not simulation state,
|
||||
and does not actively test the host callback or simulation engine.
|
||||
|
||||
The shared HealthReporter is the only health-state store. Terminal updates empty
|
||||
name and `control.Terminal` together; each start constructs a new HealthService
|
||||
bound to that run's shutdown signal. The common service supports later registration
|
||||
of an unknown watched name, though Terminal currently registers only these two names.
|
||||
|
||||
TLS, authentication, terminal-side persistence, automatic callback cancellation,
|
||||
automatic re-registration and process signal handling are not implemented here.
|
||||
The host owns process lifetime and should deploy on a trusted network or use an
|
||||
@@ -271,21 +358,38 @@ appropriately secured transport boundary.
|
||||
| [src/runtime.rs](src/runtime.rs) | Tokio runtime, TCP binding, RPC hosting, center registration and stop. |
|
||||
| [src/terminal.rs](src/terminal.rs) | Lifecycle, simulation state, callbacks, event instances and snapshots. |
|
||||
| [src/rpc.rs](src/rpc.rs) | Terminal RPC adapters and shutdown-aware watch streams. |
|
||||
| [src/health.rs](src/health.rs) | Standard Health Check/Watch adapters. |
|
||||
| [src/callback.rs](src/callback.rs) | Host callback invocation and response sink validation. |
|
||||
| [src/error.rs](src/error.rs) | Business error and event-handle response encoding. |
|
||||
|
||||
All test implementations and helpers are under `tests/`. Existing unit and runtime
|
||||
integration modules are included by [src/lib.rs](src/lib.rs) through `#[cfg(test)]`
|
||||
and `#[path]`. [tests/unit/mod.rs](tests/unit/mod.rs) declares the FFI, terminal and
|
||||
Health unit tests. [tests/unit/health.rs](tests/unit/health.rs) uses explicit crate
|
||||
imports and the crate-visible `TerminalHealth::responses` method; it is not loaded
|
||||
separately by the production Health module. No public library API is added.
|
||||
Health unit tests. [tests/unit/health.rs](tests/unit/health.rs) verifies lifecycle
|
||||
mapping and restart isolation; common protocol semantics are tested in proto.
|
||||
|
||||
[src/logging.rs](src/logging.rs) delegates backend construction to flysim-log and
|
||||
owns the explicit FFI logging lifecycle. Common filtering/retention tests are in
|
||||
the shared log crate; [tests/integration/logging.rs](tests/integration/logging.rs)
|
||||
uses isolated child processes for global state and real Terminal runtime logs.
|
||||
|
||||
On Windows, [tests/integration/logging_smoke.ps1](tests/integration/logging_smoke.ps1)
|
||||
loads the actual DLL through a .NET host and verifies its exports, error codes,
|
||||
one-time initialization, and flushed daily log file. Run it in a fresh PowerShell
|
||||
7 process because installed module-wide logging cannot be initialized again:
|
||||
|
||||
```powershell
|
||||
cargo build -p terminal
|
||||
pwsh -NoProfile -File terminal/tests/integration/logging_smoke.ps1
|
||||
```
|
||||
|
||||
The script keeps the DLL loaded until the host exits and removes only its own
|
||||
temporary log directory. It does not start a terminal or contact a center.
|
||||
|
||||
```powershell
|
||||
cargo test -p terminal
|
||||
cargo test -p terminal --lib unit_tests::health
|
||||
cargo test -p terminal --lib integration_tests::runtime
|
||||
cargo test -p terminal logging
|
||||
cargo test -p registry --lib integration_tests::runtime
|
||||
```
|
||||
|
||||
|
||||
Reference in New Issue
Block a user