Files
lmxopcua/docs/AlarmHistorian.md
T

27 KiB

Alarm Historian — store-and-forward SQLite sink

Reference for ZB.MOM.WW.OtOpcUa.Core.AlarmHistorian (src/Core/ZB.MOM.WW.OtOpcUa.Core.AlarmHistorian/), the durable local queue that historizes alarm transitions to AVEVA Historian without ever blocking the alarm engine or operator actions.

This is the sink mechanics doc. For how the three alarm sources converge on the OPC UA Part 9 surface and which alarms route here, see AlarmTracking.md. For the historian client that drains this queue, see DriverLifecycle.md and ServiceHosting.md.


Why store-and-forward

Scripted alarms (and any future non-Galaxy IAlarmSource, e.g. AB CIP ALMD) must reach AVEVA Historian, but the historian gateway can be slow, busy, or disconnected. The sink decouples the alarm engine from historian reachability: every qualifying transition is committed to a local SQLite queue first, and a background drain worker forwards rows to the historian on a backoff-aware cadence. Operator acks and alarm-state transitions are never blocked waiting on the historian.

Galaxy-native alarms with $Alarm* extensions reach AVEVA Historian directly via System Platform's HistorizeToAveva toggle — they do not flow through this sink. This path is exclusively for non-Galaxy alarm producers.


Contracts

All in IAlarmHistorianSink.cs unless noted.

  • IAlarmHistorianSink — the intake contract. EnqueueAsync(evt, ct) durably enqueues an event and returns as soon as the queue row is committed (fire-and-forget from the engine's perspective; the sink must not block the emitting thread). GetStatus() returns a HistorianSinkStatus snapshot.
  • NullAlarmHistorianSink — the no-op default for tests and deployments that don't historize alarms. It is the default DI binding (registered in the Runtime's AddOtOpcUaRuntime); production overrides it with LocalDbStoreAndForwardSink.
  • AlarmHistorianEvent (AlarmHistorianEvent.cs) — the source-agnostic event record: AlarmId, EquipmentPath (UNS path, doubles as Historian's SourceNode), AlarmName, AlarmTypeName (Part 9 subtype), Severity, EventKind (free-form transition string — "Activated"/"Cleared"/"Acknowledged"/etc.), Message, User, Comment, TimestampUtc.
  • IAlarmHistorianWriter — what the drain worker delegates writes to. WriteBatchAsync(batch, ct) returns one HistorianWriteOutcome per event, in order. Production binds this to GatewayAlarmHistorianWriter (the HistorianGateway SendEvents batch path — up to 200 events per RPC, #746 — with SendEvent as the per-event fallback for a failed event, a refused batch, or a gateway without the batch RPC).
  • HistorianWriteOutcome — per-event drain result: Ack (persisted, remove from queue), RetryPlease (transient failure — leave queued, retry after backoff), PermanentFail (malformed/unrecoverable — move to dead-letter). Writers return PermanentFail only when the failure proves the EVENT bad; anything that could be an outage is RetryPlease (#976 — see "Outages never dead-letter" below).
  • HistorianSinkStatus — diagnostic snapshot surfaced to the AdminUI and /healthz: QueueDepth, DeadLetterDepth, LastDrainUtc, LastSuccessUtc, LastError, DrainState, and EvictedCount.
  • HistorianDrainState — Disabled / Idle / Draining / BackingOff / NotPrimary (ticking, but leaving the replicated queue for the node that holds the Primary role).

LocalDbStoreAndForwardSink

LocalDbStoreAndForwardSink.cs is the production IAlarmHistorianSink. Construction takes the node's ILocalDb, an IAlarmHistorianWriter, a logger, and optional batchSize (default 100), capacity (default 1,000,000), deadLetterRetention (default 30 days), a test clock, and a drainGate.

The drain gate

drainGate is a Func<bool> consulted at the top of every tick; false skips the tick entirely, leaving the queue untouched and reporting DrainState = NotPrimary.

It exists because the queue replicates. The Secondary holds a full copy of the Primary's rows, and an ungated drain there would re-deliver every event, continuously — not just at a failover. Runtime supplies the gate from IRedundancyRoleView, a singleton DriverHostActor publishes its PrimaryGatePolicy verdict to on every redundancy snapshot, so the drain agrees with the inbound-write and native-ack gates by construction.

Two postures are deliberate:

  • Unset (the default) means drain. So does an unpublished view. A deployment that runs no redundancy never publishes a role, and defaulting closed would silently stop its alarm history forever.
  • Only a shared queue is gated. Standing down is safe only because a peer holds the same rows, so the gate applies only when LocalDbReplicationPolicy.SharesRowsWithPeer holds: LocalDb:Replication:ApiKey AND (LocalDb:Replication:PeerAddress OR LocalDb:SyncListenPort > 0) — an authenticated sync path in either direction. Any other node always drains its own queue. This is deliberately stricter than the table-registration predicate (PeerAddress OR ApiKey): a key with no dial and no listener (e.g. a fleet-wide secret on an unpaired node) captures changes but shares them with nobody, and gating it would park its events behind a Primary that does not hold them (review R1 m3).
  • A gated node stands down only for the peer that holds its rows — and BOTH halves of a pair know it. The dialling half identifies its peer by the host of LocalDb:Replication:PeerAddress. The passive half (ApiKey + SyncListenPort, no PeerAddress) identifies it as its per-cluster mesh partner (the other member of its cluster-{ClusterId} role). Before review R6 MINOR-4 the passive half knew no peer at all, so as Secondary it kept draining beside its Primary partner and the historian received every alarm event twice. ⚠️ A passive half with no cluster-scoped role (a legacy, pre-Phase-6 node) still cannot identify its partner and keeps draining — it cannot prove which Primary holds its rows, and deferring to the wrong one would strand them.
  • A gate that throws means "not now", never permission. Draining on a gate fault would put a pair into dual delivery exactly when its redundancy signal is sick.

Queue table

The sink owns one table in the node's consolidated LocalDb — created and registered for replication by LocalDbSetup.OnReady, before any consumer can resolve the sink:

CREATE TABLE alarm_sf_events (
    id               TEXT    NOT NULL PRIMARY KEY,   -- SHA-256 of payload_json
    alarm_id         TEXT    NOT NULL,
    enqueued_at_utc  TEXT    NOT NULL,
    payload_json     TEXT    NOT NULL,   -- JSON-serialized AlarmHistorianEvent
    attempt_count    INTEGER NOT NULL DEFAULT 0,
    last_attempt_utc TEXT    NULL,
    last_error       TEXT    NULL,
    dead_lettered    INTEGER NOT NULL DEFAULT 0
);
CREATE INDEX ix_alarm_sf_events_drain
    ON alarm_sf_events (dead_lettered, enqueued_at_utc, id);

The primary key is a hash of the payload, not an autoincrement rowid. Two reasons. Replication converges last-writer-wins per primary key, so two nodes independently allocating rowid 7 to different alarms would silently overwrite one another. And an equal-payload key makes the same event arriving twice collapse into one row — which happens for real, because HistorianAdapterActor default-writes while its redundancy role is unknown and both nodes of a pair therefore accept the same fanned transition in every boot window. Two genuinely distinct events cannot collide: AlarmHistorianEvent carries a full-precision timestamp alongside the alarm id, kind, message and user.

Ordering follows from that: the drain reads in enqueued_at_utc order (with id as a tiebreak so the ordering is total), because a hashed key carries no insertion sequence.

Gateway delivery carries a separate nonzero DeliveryId GUID. Direct event instances create it once; retries and record copies preserve it. It is excluded from queue JSON and business equality, so the existing payload fingerprint and pair deduplication stay unchanged. The drain derives its delivery GUID solely from the persisted row id, including migrated legacy rows. It never hashes a reconstructed payload or mints an ID per send. The versioned encoding in AlarmDeliveryIds therefore survives retry, restart, payload edits and replicated failover without a schema migration.

Different business payloads at the same timestamp remain distinct; completely identical payloads deliberately converge. An explicitly empty direct delivery ID is rejected. The Gateway mapper sends this GUID as the wire event ID; the live alarm readback test requires that exact ID. SQL Server and QuestDB retain their existing event-ID schemes.

EnqueueAsync does a single INSERT … ON CONFLICT DO NOTHING on the hot path. To avoid a SELECT COUNT(*) on every enqueue, the sink keeps an in-memory non-dead-lettered row counter (seeded at startup, kept current by every mutation, and re-synced from storage every 10,000 enqueues to defend against drift — now doubly warranted, since replication applies the peer's rows straight into the table where no local code path can observe them). Pragmas and connection lifetime belong to LocalDb; the sink no longer manages either.

Drain worker

StartDrainLoop(tickInterval) starts a self-rescheduling one-shot System.Threading.Timer (not started automatically — tests drive DrainOnceAsync deterministically). Each tick:

  1. Consults the drain gate. A closed gate returns immediately, leaving the queue untouched and DrainState = NotPrimary.
  2. Purges aged dead-lettered rows past the retention window.
  3. Reads up to batchSize non-dead-lettered rows in enqueued_at_utc, id order.
  4. Rows with un-deserializable payloads are dead-lettered immediately (by their own id) so they can't stall the queue head.
  5. The remaining batch is handed to IAlarmHistorianWriter.WriteBatchAsync, and each outcome is applied in one transaction: Ack deletes the row (the delete replicates as a tombstone, so the peer drops its copy and a promoted standby cannot re-send it), PermanentFail flips its dead_lettered flag, RetryPlease leaves it queued — and, only on a drain where at least one event was Ack'd (the historian is demonstrably accepting writes), bumps its attempt count. A row whose AttemptCount has reached the configured MaxAttempts cap (default 10) is dead-lettered automatically on the next such drain rather than retried — this breaks infinite retry loops for poison events whose payload the historian will always reject (e.g. a malformed alarm record that triggers a permanent SDK error on every attempt). The dead-lettered row remains inspectable via RetryDeadLettered() for the configured retention window.
  6. The timer re-arms its next due-time to max(tickInterval, currentBackoff).

Backoff ladder (applied to the timer's next due-time, so a historian outage genuinely slows the drain cadence): 1s → 2s → 5s → 15s → 60s cap. Any RetryPlease outcome — or a writer exception, or a writer cardinality violation (outcome count ≠ event count) — bumps the backoff and sets DrainState = BackingOff; a clean batch resets it. The async-void timer callback is fully guarded: a fault is logged and recorded into GetStatus() rather than lost as an unobserved task exception.

Outages never dead-letter (#976)

Every production writer answers a systemic failure — historian, HistorianGateway or database down, an auth blip, a failed schema bootstrap, a gateway restart answering Cancelled, a dropped channel — with RetryPlease for every event of the batch. Until #976 the sink counted each of those against MaxAttempts, so an outage longer than ~6.5 minutes (1 + 2 + 5 + 15 + 60 s, then 60 s per drain, 10 attempts) dead-lettered the oldest 100 queued alarm events, and another 100 every ~10 minutes after that, with no automatic retry. Now:

  • A drain with no Ack is treated as an outage: RetryPlease rows are left exactly as they were (no attempt counted, nothing dead-lettered), the backoff climbs to its 60 s cap, DrainState = BackingOff and LastError reads "Historian unavailable: N event(s) deferred …". PermanentFail rows still dead-letter. When the historian returns, the queue drains in enqueued_at_utc order.

  • MaxAttempts therefore bounds only a row that keeps failing while its neighbours land — the finding-002 poison case it exists for. That only works if the writer lets the neighbours land, i.e. isolates an event it cannot classify instead of retrying its whole batch with it — which both writers now do (below).

  • Writers: GatewayAlarmHistorianWriter dead-letters only InvalidArgument, FailedPrecondition, OutOfRange, NotFound, AlreadyExists, Unimplemented (on a single send) and events that cannot be mapped to the wire; every other gRPC code and every non-RPC exception retries (the default used to be dead-letter). On the batch (SendEvents) path a retryable failure is split two ways (#976 review):

    • a known outage — Unavailable, DeadlineExceeded, Cancelled, ResourceExhausted, Aborted, Unauthenticated, PermissionDenied, or a status-less transport fault (socket / I/O / HTTP, a disposed channel, a timeout) — retries the chunk and every later chunk without sending more;
    • anything else — Unknown (the gateway's handler threw, e.g. on one bad event), Internal, DataLoss, an unlisted code, a non-transport exception — re-sends the chunk event by event, so the siblings Ack and only the culprit keeps RetryPlease (and dead-letters via MaxAttempts on those Ack'd drains). If not one single send Acks, it was not the event: later chunks retry as for an outage, so such an outage costs at most one chunk of single sends per drain. Before this, the gateway writer retried the whole batch on Unknown, the drain saw no Ack, and one such event at the queue head stalled all Gateway alarm history until capacity eviction dropped it silently. (The earlier version of this section said the writer behaved like the SQL writers here; it did not.)

    The SQL writers dead-letter only recognised data errors, and AlarmBatchWrite re-sends a multi-event chunk event by event when it fails with an error that is neither a recognised data error nor a known outage, so one unrecognised poison event cannot hold its chunk-mates. The isolation stops at the first single that fails with a known outage error (the rest of the chunk retries), and the SQL Server writer's outage set (SqlHistorianConnection.IsOutage) adds login failures (18456/18452), server unreachable / refusing connections (2, 40, 53, 64, 121, 10061, 10065, -1), fatal severity ≥ 20 and an exhausted connection pool to the retry-transient numbers — so an outage of that kind costs one round trip per chunk, not one per event.

  • Residual: a batch (Gateway: an isolated chunk) made up entirely of rows that always answer RetryPlease looks like an outage and is retried at the 60 s cap rather than dead-lettered. It shows as a queue that does not drain while LastError reports the deferral.

  • Rows dead-lettered as max attempts (N) exceeded before #976 may be outage casualties; see "Reviving rows the old cap dead-lettered (#1007)" below. Rows dead-lettered by a post-#976 node read … exceeded while the historian accepted other events.

Reviving rows the old cap dead-lettered (#1007)

A node that ran a pre-#976 build through a historian outage holds dead-lettered rows whose last_error is exactly max attempts (N) exceeded — outage casualties, not poison. They stay dead for the retention window (default 30 days, then they are purged and unrecoverable), so do this soon after upgrading. One-time, idempotent, safe:

  1. Upgrade to a #976+ build (the revived rows must meet the new outage-aware rule).
  2. Set AlarmHistorian__ReviveMaxAttemptsDeadLetters=true and restart the node (do the Primary, or both nodes of a pair — the second finds nothing left).
  3. Read the Warning requeued N alarm event(s) the pre-#976 'max attempts' cap dead-lettered (N = 0 means nothing to do). The rows rejoin the queue with attempt_count = 0 and drain in their original enqueue order. The revival goes through LocalDb, so it replicates to the peer.
  4. Remove the setting. Leaving it on is harmless (it re-runs and revives 0 each boot).

It touches only the exact old reason (last_error GLOB 'max attempts (*) exceeded'): PermanentFail, corrupt-payload and post-#976 poison dead letters are untouched (use RetryDeadLettered() for those). A revived row that is genuinely poison dead-letters again under the current rule with the new-style reason, so it cannot loop. Dead letters are visible as DeadLetterDepth before/after. Delivery is at-least-once, so a casualty the historian had in fact stored is simply re-written.

Stalled-queue detection (#1034)

A queue whose head is rows that always answer RetryPlease (or a long outage) does not drain, and nothing dead-letters it. AlarmHistorian:StallThresholdHours (default 2, 0 disables) reports the queue stalled once rows have been queued on the draining node for that long with no row delivered or dead-lettered:

  • HistorianSinkStatus.Stalled / LastProgressUtc (AdminUI /alarms/historian, actor status),
  • one Warning on entering the state and one Information on leaving it,
  • gauge otopcua.alarm_historian.queue.stalled (1/0) plus …queue.depth / …queue.dead_letter_depth,
  • the readiness check alarm-historian: Degraded (still 200, never Unhealthy).

Outage vs stuck (#1071). A queue whose head is retry-forever rows and a long historian outage both look like "no Ack". The three writers (Gateway, SqlServer, QuestDB) therefore report IAlarmHistorianWriter.LastReachableUtc — the last write attempt the historian answered (success or a data-level refusal; a transport/outage failure such as gRPC Unavailable/deadline/auth, or a transient SQL fault, does not count). When no progress has been made for the threshold: the historian answered within the last 5 minutes ⇒ Stalled (a stuck head); it has not ⇒ HistorianUnreachable (rows are merely held; Stalled stays false, the queue.stalled gauge reads 0, the Warning says "HELD by a historian outage", and the readiness check is still Degraded with an outage description). #1089: the AdminUI page (/alarms-historian) shows a red Historian unreachable badge for the outage and an amber Queue stalled badge for a stuck head (mutually exclusive); the gauge otopcua.alarm_historian.unreachable (1/0, same meter, next to queue.stalled) reads 1 only for the outage. The SqlServer writer counts a statement-level refusal as an answer but NOT a login failure / refused connection / pool exhaustion (SqlServerAlarmHistorianWriter.Answered, via IsOutage — it used !IsTransient, which counted those as reachable); QuestDB likewise excludes SQLSTATE class 08/28/53/57. The Gateway writer (#1096, GatewayAlarmHistorianWriter.IsGatewayAnswer) counts a failure as an answer only when it carries a gRPC status that is not a systemic code AND was not fabricated client-side: a status with a Status.DebugException (a connection reset / HTTP/2 RST_STREAM surfaced as Internal/Unknown) or a Grpc.Net.Client detail such as Bad gRPC response. HTTP status code: 5xx (a proxy) is not an answer; a genuine gateway Internal (historian rejection) / Unknown (throwing handler) is. A status-less exception is never an answer. The Unimplemented batch probe still counts (it is a real gateway reply). A writer that does not report reachability keeps the #1034 behaviour (stalled also fires on an outage; LastError says which). A node that is not the drain owner is never stalled and promotion restarts the clock.

Durability bound (important)

The durability guarantee is bounded by capacity (default 1,000,000 rows). When the non-dead-lettered queue reaches capacity, EnqueueAsync evicts the oldest non-dead-lettered rows (oldest enqueued_at_utc first) to make room, logs a WARN, and increments HistorianSinkStatus.EvictedCount. Under a sustained historian outage, accepted alarm events can therefore be dropped before delivery. A non-zero EvictedCount is a data-loss signal that requires operator attention — it surfaces silent loss without log scraping.

Dead-letter + operator recovery

PermanentFail and corrupt-payload rows are retained in-place with dead_lettered = 1 for the retention window (default 30 days) so operators can inspect them before the sweeper purges them. RetryDeadLettered() is the operator action (from the AdminUI) that clears the dead-letter flag and attempt count on every dead-lettered row, returning them to the regular queue with a fresh backoff.


Runtime wiring

HistorianAdapterActor (Runtime/Historian/HistorianAdapterActor.cs) subscribes to the cluster alerts DPS topic and translates each AlarmTransitionEvent into an AlarmHistorianEvent, then calls IAlarmHistorianSink.EnqueueAsync fire-and-forget so the actor loop is never blocked on historian reachability. The actor is Primary-gated: only the node whose RedundancyRole is Primary historizes, giving exactly-once writes across a redundant pair. AlarmTransitionEvent carries AlarmTypeName (the Part 9 subtype string) and Comment (the operator comment from the originating ack/shelve command) that populate the corresponding fields of AlarmHistorianEvent. GatewayAlarmHistorianWriter is the IAlarmHistorianWriter the drain worker delegates to (gateway SendEvents batches, SendEvent fallback). See ServiceHosting.md for the (external) HistorianGateway setup.

Scope: scripted alarms only. Galaxy-native alarms historize via System Platform's HistorizeToAveva toggle (not this actor); AB CIP ALMD is not on the alerts topic (future).

Configuration

The real sink is opt-in via the AlarmHistorian section of appsettings.json. When Enabled is false (the default), AddAlarmHistorian registers NullAlarmHistorianSink and the feature is dormant. When Enabled is true, AddAlarmHistorian constructs LocalDbStoreAndForwardSink and registers GatewayAlarmHistorianWriter as the IAlarmHistorianWriter. This section carries only the Enabled gate + the store-and-forward knobs — the downstream gateway connection (endpoint / key / TLS) is sourced from the ServerHistorian section (see Historian.md), and the queue's storage location is the node's consolidated LocalDb:Path database (see Configuration.md).

Where the queue lives (changed). The buffer is a table — alarm_sf_events — in the node's consolidated LocalDb, not a standalone alarm-historian.db. That is what lets it replicate to the redundant pair peer, so undelivered alarm history survives losing the node holding it. Two consequences follow, both covered in the pair-replication runbook: only the Primary drains (the Secondary holds a full replica and would otherwise re-send everything), and delivery is at-least-once across a failover by design. On first boot after the upgrade, AlarmSfLegacyMigrator copies any existing alarm-historian.db across and renames it .migrated.

{
  "AlarmHistorian": {
    "Enabled": true,
    "BatchSize": 100,
    "DrainIntervalSeconds": 5,
    "Capacity": 1000000,
    "DeadLetterRetentionDays": 30
  }
}
Key Type Default Description
Enabled bool false Enable the durable store-and-forward sink (drains to the HistorianGateway SendEvents / SendEvent path). false → NullAlarmHistorianSink. Requires LocalDb to be registered — an enabled sink with no ILocalDb fails at resolution rather than silently discarding events.
BatchSize int 100 Max rows per drain cycle handed to IAlarmHistorianWriter.WriteBatchAsync.
DrainIntervalSeconds int 5 Seconds between drain-worker ticks.
Capacity long 1000000 Max queued rows before the sink evicts the oldest (data-loss signal via EvictedCount).
DeadLetterRetentionDays int 30 Days to retain dead-lettered rows before purge.
MaxAttempts int 10 Maximum delivery attempts before a poison (perpetually-retrying) row is dead-lettered automatically. Must be > 0.
StallThresholdHours double 2 Hours of no queue progress (rows queued, none delivered/dead-lettered, on the draining node) before the queue is reported stalled (#1034). 0 disables.
ReviveMaxAttemptsDeadLetters bool false One-time recovery (#1007): requeue rows the pre-#976 cap dead-lettered as max attempts (N) exceeded. Idempotent; remove after one restart.

The downstream gateway connection lives in ServerHistorian (Endpoint + env ServerHistorian__ApiKey, UseTls, CaCertificatePath); alarm-history ReadEvents additionally requires the gateway running RuntimeDb:EventReadsEnabled=true. The old Wonderware connection keys (SharedSecret / AlarmHistorian:Host/Port/UseTls/ServerCertThumbprint) were pruned.

DatabasePath was removed. The queue no longer owns a file. The key is still read, by AlarmSfLegacyMigrator, purely to locate a pre-consolidation alarm-historian.db to migrate — so leave it in place through the upgrade, then delete it once every node has an alarm-historian.db.migrated sidecar. A leftover value configures nothing.

DrainState gained NotPrimary. A gated-off Secondary reports it instead of Idle, so a rising queue depth there is distinguishable from a stalled drain. Both nodes reporting it means nobody is draining — see the runbook.

Dev and docker-dev deployments leave Enabled unset (defaults to false) so alarm transitions historize to nowhere unless a HistorianGateway is configured.


See also