Skip to content

Base the audit retention custom check on the oldest expired rows - #6208

Draft
ramonsmits wants to merge 1 commit into
audit-retention-timeout-hintfrom
audit-retention-check-oldest-row
Draft

ramonsmits wants to merge 1 commit into
audit-retention-timeout-hintfrom
audit-retention-check-oldest-row

Conversation

@ramonsmits

@ramonsmits ramonsmits commented Nov 30, 2026 •

Copy link
Copy Markdown
Member

The "Audit retention" custom check reads the oldest expired rows from the database. It fails when rows stay more than three hours after their retention period ended.

The check counted consecutive failed sweeps in memory and failed at three. That count misses the cases where the database grows without a failed sweep:

  • A sweep that succeeds but takes longer than the sweep interval, so rows expire faster than the sweep removes them.
  • A sweep that does not end.
  • An instance that restarts before three sweeps fail. The count starts at zero after each restart.

The sweep retries also changed the meaning of the count. Three failures took three hours, and with the retries they take less than a minute.

Changes

  • IAuditPartitionManager.OldestExpired returns the first value of ListExpired. SQL Server overrides it with one seek on the clustered key. PostgreSQL uses the default, which reads the partition names.
  • The check calls OldestExpired for each kind of row, with the retention period plus a tolerance of three hours. Three hours is three sweep intervals, the same tolerance as the count of three.
  • The failure message names each kind of row that is behind and the start of its oldest rows. It adds the reason of the last failed sweep when there is one.
  • AuditRetentionCustomCheck.State keeps only the reason of the last failed sweep. It does not affect the check result.

Notes for the review

  • The check fails while retention removes a large backlog, and after a decrease of the retention period, until the sweep catches up.
  • A sweep that is skipped because another audit instance holds the retention lock does not fail the check when the other instance removes the rows.
  • The tests cover both kinds of row on SQL Server and PostgreSQL: no expired row, expired rows inside the tolerance, overdue rows without a sweep, and overdue rows after each kind of failed sweep.

Docs: reviewed docs/telemetry.md and docs/message-body-storage.md. Updated the retention section of docs/telemetry.md to describe what the check reads. docs/message-body-storage.md needs no change.

Guidelines: "Prefer explicit container registration" followed, the check gets its new dependencies through the constructor. "Prefer explicit persistence operations" followed except for one rule: OldestExpired takes the AuditDbContext from the caller, as the other IAuditPartitionManager methods do. "Prefer Microsoft abstractions" and "Database schema and index changes" do not apply.

The check counted consecutive failed sweeps in memory. A sweep that
succeeds but takes longer than the sweep interval, a sweep that does not
end and a restart did not fail the check, and the sweep retries reached
the count of three in less than a minute.

The check reads the oldest expired rows from the database and fails when
rows stay more than three hours after their retention period ended.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant