Fix Kafka retry seek, bound buffers, and cut hot-path CPU/memory waste (Phases 41–44).
CI / changes (push) Successful in 17s
CI / frontend (push) Skipped
CI / backend (push) Successful in 15m10s

Stop consumers from committing past failed offsets, harden Rabbit/DataLake/gateway backpressure, dispose JsonDocument/MinIO correctly, and batch expensive DB/HTTP paths. Track progress in docs/performance.
This commit is contained in:
2026-11-30 21:32:43 +08:00
parent 1c1065938f
commit 68a2ab3204
54 changed files with 959 additions and 311 deletions
@@ -0,0 +1,96 @@
# VigilCare Clinical — CPU / Memory Remediation Roadmap
**Internal document | Phases 41–44**
**Prerequisites:** Phases 1–40 clinical platform, climate resilience (20–24), and alert optimization foundations in place.
**Source:** Static performance/reliability audit of `VigilCareClinicalAPI` and `VigilCare.WardGateway` (CPU, memory, EF, HttpClient, concurrency).
---
## Problem Statement
Under normal traffic the platform is largely correct. Under **peak observation storms**, **sustained outages** (MinIO / central unreachable), and **long-running process lifetime**, several defects cause:
| Risk | Symptom |
|------|---------|
| Silent Kafka loss | Transient consumer failures advance past failed offsets (“retry” never re-seeks) |
| Rabbit channel corruption | Prefetch > 1 + async handlers sharing one `IModel` |
| Unbounded memory / disk | DataLake in-memory buffer and gateway `buffered_sync_items` grow without a hard cap |
| GC / socket pressure | Undisposed `JsonDocument`, per-upload MinIO clients, per-message `JsonSerializerOptions` |
| Sync / API latency | Per-item `SaveChanges`, N+1 reconciliation and HTTP detail fetches, unbounded timeline loads |
---
## Design Principles
1. **Correctness before throughput** — never commit past a failed Kafka offset without intentional poison skip.
2. **Bound everything that can grow under outage** — buffers must plateau; lag is preferable to OOM.
3. **Dispose pooled / unmanaged resources** on every hot path.
4. **Measure with tools, not invented numbers** — `dotnet-counters`, lag metrics, existing unit tests.
---
## Phase Map
| Phase | Name | Audit findings | Status |
|-------|------|----------------|--------|
| **41** | Correctness & backpressure | 1, 2, 3, 7 | Complete |
| **42** | Resource leaks & GC | 4, 5, 8, 15 | Complete |
| **43** | Throughput hot paths | 6, 9, 10, 11, 12, 13 | Complete |
| **44** | Hardening | 14, 16, 17, 18, 19, 20 | Complete |
### Dependency graph
```
Phase 41 → Phase 42 → Phase 43 → Phase 44
```
Docs (this folder) are written first; checklist items flip to **Complete** as code lands.
### Phase plans
- [phase-41-correctness-backpressure.md](phase-41-correctness-backpressure.md)
- [phase-42-resource-leaks-gc.md](phase-42-resource-leaks-gc.md)
- [phase-43-throughput-hot-paths.md](phase-43-throughput-hot-paths.md)
- [phase-44-hardening.md](phase-44-hardening.md)
---
## Severity Matrix (audit reference)
| ID | Severity | Finding |
|----|----------|---------|
| 1 | Critical | Kafka failure path advances past failed offset |
| 2 | High | RabbitMQ `IModel` + prefetch > 1 |
| 3 | High | DataLake buffer unbounded on flush failure |
| 4 | High | Undisposed `JsonDocument` |
| 5 | High | MinIO client per upload, never disposed |
| 6 | High | Clinical sync per-observation SaveChanges |
| 7 | High | Gateway buffered sync unbounded |
| 8 | Medium–High | Per-message `JsonSerializerOptions` |
| 9 | High | Patient list PHI N+1 SaveChanges |
| 10 | Medium–High | Reconciliation N+1 |
| 11 | High | Encounter replica sync N+1 HTTP / HttpClient misuse |
| 12 | Medium | Encounter timeline unbounded |
| 13 | Medium | Ward list over-fetches score history |
| 14 | Medium | Simulation fire-and-forget Task.Run |
| 15 | Medium | Redis threshold batch tasks discarded |
| 16 | Medium | Trend Redis RMW race |
| 17 | Medium | Paging worker long poll hold |
| 18 | Medium | Parquet double allocation |
| 19 | Medium | PHI migration loads full table |
| 20 | Low | Health-check Task.Run blocking |
---
## Diagnostic Tools
| Symptom | Tools |
|---------|--------|
| Allocations / GC | `dotnet-counters`, `dotnet-gcdump` |
| Leaks | `dotnet-dump` + SOS |
| Kafka loss / lag | Consumer lag + `kafka_poison_pills_skipped_total` |
| DB amplification | EF logging, `pg_stat_statements` |
Related: [../ops/production-observability.md](../ops/production-observability.md)