Bound list/PDF payloads, stream MinIO I/O with cancellation, paginate history and FHIR $everything, tighten OCR lifetimes/buffers, and add phase tracking docs.
3.9 KiB
3.9 KiB
Phase 20 — Memory Safety (MinIO + Unbounded Clinical Payloads)
What exists
DocumentStorageService.DownloadAsyncbuffers the full object and uses syncCopyToin the MinIO callback; noCancellationTokenon the interface.- Upload copies the full stream then hashes;
EnsureBucketAsyncruns on every upload. GetDocumentcallsObjectExistsAsyncthen downloads.DigitizationHistoryService.GetPatientHistoryAsyncloads all batches + drafts + events (tracked).FhirService.GetPatientEverythingAsyncloads all encounters and observations.WorkQueueService.GetOverviewAsyncmaterializes every pendingUpdatedAt.
Requires Phase 19. Tracker: performance-hardening-overview.md.
What needs to be built
Reduce peak RAM for document I/O and history/FHIR dumps. Keep returning MemoryStream from download for OCR compatibility; true HTTP pipe-through is out of scope unless required after measuring.
Do not start Phase 21 until success criteria below pass.
Step 1 — Async MinIO download + CancellationToken
- Add
CancellationTokentoIDocumentStorageServicedownload/upload (and call sites). - In
DownloadAsync, use async copy inside the MinIO callback (CopyToAsync+ CT). - Propagate CT from controllers / OCR hosted service where already available.
Step 2 — Upload hashing and bucket ensure
- Hash while buffering once (single
MemoryStreampass) where practical. - Ensure bucket once at bootstrap (
AppBootstrapHostedService) and drop per-uploadEnsureBucketAsync, or cache a process-lifetime “bucket ready” flag.
Step 3 — GetDocument without redundant StatObject
- Remove
ObjectExistsAsyncwhen missing objects can be expressed as 404 from download failure / MinIO not-found.
Step 4 — Paginate patient digitization history
- Add
page/pageSize(default 20, max 50) to history API + service. - Use
AsNoTracking; project summaries. - Cap audit trail size per entry (e.g. last N events) or defer full trail so payload stays bounded.
- Update DTO / controller / web client callers if they assume an unpaged full list.
Step 5 — FHIR $everything paging
- Honor
_count(default 100, max 500) with Bundlenextlink. - Do not load the entire observation history in one query/response.
Step 6 — Work-queue overview SQL aggregates
- Replace pending
UpdatedAtToListAsyncwith EF aggregates for average age and oldest pending (avg / minUpdatedAt).
Success criteria
- Download path uses async copy and accepts CT
- Bucket ensure is not invoked on every upload
- History returns
Entries.Count ≤ pageSizewith accurate total count metadata $everything?_count=10returns ≤ 10 resource entries (plus paging metadata as designed)- Overview no longer materializes all pending timestamps in memory
- GetDocument / OCR download still work in integration tests
dotnet testpasses; Phase 11 FHIR script still green if run
Out of scope
- Tesseract engine reuse / Azure singleton (Phase 21)
- Fuzzy match / seed N+1 (Phase 22)
Verification
dotnet test VigilCareRecordsAPI.Tests/VigilCareRecordsAPI.Tests.csproj./scripts/run-vigilcare-records-phase-11-verification.sh(optional but recommended)- Update overview findings 1, 3, 4, 7 (storage CT), 12, 13 → Done when this phase merges
Gate
Do not start Phase 21 until success criteria above are met.