ZB.MOM.WW.Auth.ApiKeys: debounce MarkUsedAsync (last_used_utc) and take it off the verification path #61

Open
opened 2026-12-03 09:34:30 -05:00 by dohertj2 · 0 comments
Owner

Cross-repo follow-up from historiangw#169 (HOST-01, 2026-12-02 performance & bug review of HistorianGateway; decision D3, 2026-12-03).

Problem

ApiKeyVerifier.VerifyAsync runs on every authenticated RPC in consuming gateways, and each successful verification calls IApiKeyStore.MarkUsedAsync (src/ZB.MOM.WW.Auth.ApiKeys/ApiKeyVerifier.cs:98) synchronously on the request path. SqliteApiKeyStore.MarkUsedAsync opens a fresh connection, re-runs the journal_mode=WAL / busy_timeout=5000 PRAGMAs and commits an UPDATE api_keys SET last_used_utc = ... (Sqlite/SqliteApiKeyStore.cs:27-40).

  • Microsoft.Data.Sqlite *Async is synchronous, so this blocks a thread-pool thread per call.
  • SQLite has a single writer, so the UPDATE serializes every RPC of every key through the database write lock. Throughput is bounded by the key-store volume's commit rate, not by the gateway or historian.
  • If anything else holds the write lock (dashboard/CLI key create/revoke, a slow fsync), each RPC waits up to the 5 s busy timeout before the failure is swallowed: added latency with no error and no metric.

Affects HistorianGateway (OtOpcUa sends one SendEvent per alarm) and any other consumer of the verifier (mxaccessgw).

Requested change (decided)

Debounce the last-used stamp and take it off the request path:

  • Record last_used_utc at most about once per minute per key (configurable interval; 0 = current per-call behaviour if a consumer needs it).
  • Perform the write off the request path (fire-and-forget into a small background flusher, or a coalescing in-memory map flushed on an interval and on dispose), so VerifyAsync does only the read + HMAC compare.
  • A failed stamp must never fail or delay verification; count/log it.
  • Optional, same change: reuse a connection / skip re-running the PRAGMAs per call.

last_used_utc becomes accurate to the debounce interval; document that.

Gateway side (not in this issue)

historiangw#169 step 2 adds a gateway-owned success cache (ApiKeys:VerificationCacheSeconds, default 0 = off) on top. The gateway picks up this change by bumping its ZB.MOM.WW.Auth.* package pins once published.

Tests

  • N verifications of one key within the interval → one MarkUsedAsync / one UPDATE.
  • Verification latency is independent of a held write lock (store wrapper that blocks MarkUsedAsync).
  • Stamp failure does not fail verification.
  • Pending stamps flushed on dispose.
Cross-repo follow-up from **historiangw#169** (HOST-01, 2026-12-02 performance & bug review of HistorianGateway; decision D3, 2026-12-03). ## Problem `ApiKeyVerifier.VerifyAsync` runs on every authenticated RPC in consuming gateways, and each successful verification calls `IApiKeyStore.MarkUsedAsync` (`src/ZB.MOM.WW.Auth.ApiKeys/ApiKeyVerifier.cs:98`) **synchronously on the request path**. `SqliteApiKeyStore.MarkUsedAsync` opens a fresh connection, re-runs the `journal_mode=WAL` / `busy_timeout=5000` PRAGMAs and commits an `UPDATE api_keys SET last_used_utc = ...` (`Sqlite/SqliteApiKeyStore.cs:27-40`). - Microsoft.Data.Sqlite `*Async` is synchronous, so this blocks a thread-pool thread per call. - SQLite has a single writer, so the `UPDATE` serializes **every RPC of every key** through the database write lock. Throughput is bounded by the key-store volume's commit rate, not by the gateway or historian. - If anything else holds the write lock (dashboard/CLI key create/revoke, a slow fsync), each RPC waits up to the 5 s busy timeout before the failure is swallowed: added latency with no error and no metric. Affects HistorianGateway (OtOpcUa sends one `SendEvent` per alarm) and any other consumer of the verifier (mxaccessgw). ## Requested change (decided) Debounce the last-used stamp and take it off the request path: - Record `last_used_utc` at most about **once per minute per key** (configurable interval; `0` = current per-call behaviour if a consumer needs it). - Perform the write **off the request path** (fire-and-forget into a small background flusher, or a coalescing in-memory map flushed on an interval and on dispose), so `VerifyAsync` does only the read + HMAC compare. - A failed stamp must never fail or delay verification; count/log it. - Optional, same change: reuse a connection / skip re-running the PRAGMAs per call. `last_used_utc` becomes accurate to the debounce interval; document that. ## Gateway side (not in this issue) historiangw#169 step 2 adds a gateway-owned success cache (`ApiKeys:VerificationCacheSeconds`, default 0 = off) on top. The gateway picks up this change by bumping its `ZB.MOM.WW.Auth.*` package pins once published. ## Tests - N verifications of one key within the interval → one `MarkUsedAsync` / one `UPDATE`. - Verification latency is independent of a held write lock (store wrapper that blocks `MarkUsedAsync`). - Stamp failure does not fail verification. - Pending stamps flushed on dispose.
dohertj2 added the type:featurepriority:p2cross-repo labels 2026-12-03 09:34:30 -05:00
Sign in to join this conversation.