feat: harden session compaction job with retention policy - #794
Open
Bouynaty wants to merge 1 commit into
Open
Conversation
Refine the nightly session compaction job so the retention boundary is computed inside the database (NOW() - retention interval) rather than passed in from the application-server clock. A bad-clock event on the app server can no longer push the boundary forward and cause active rows to be deleted. Add a per-run row cap (SESSION_COMPACTION_MAX_ROWS_PER_RUN, default 100k) so an anomaly can never wipe the table in one cycle; emit session.compaction.cap_hit when the cap is reached. Validate batch and cap inputs, and log scheduled-run failures without killing the timer. Tests cover batched deletion, vacuum behavior, error metrics, bad-clock safety, cap enforcement, input validation, and the start/stop lifecycle (100% statement coverage on the service). Closes RevoraOrg#683
|
@Bouynaty Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits. You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀 |
Author
|
@thlpkee20-wq done closes #683 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Overview
This PR hardens the nightly Session Storage Compaction job so that revoked and expired session rows are deleted only after they have truly aged past the retention window, the table is vacuumed to reclaim space, and operators get actionable signals if a compaction run is under pressure.
Related Issue
Closes #683
Changes
🛡️ Bad-clock safe retention boundary
src/db/repositories/sessionRepository.tspurgeOlderThan(retentionDays, batchSize)now computes the retention boundary inside the database (NOW() - retention * INTERVAL '1 day') instead of accepting an application-server timestamp.getOldestCompactedSessionDate(retentionDays)uses the same DB-clock boundary so the retention-lag metric stays consistent with the purge predicate.expires_at, norevoked_at) can never match the delete predicate — even during a bad-clock event on the app server.🧱 Bounded blast radius
src/services/sessionCompactionService.tsSESSION_COMPACTION_MAX_ROWS_PER_RUN, default100_000) so an anomaly can never wipe the table in a single cycle; remaining rows stay behind for the next run.session.compaction.cap_hit(counter,status=warning) when the cap is reached.batchSize/maxRowsPerRun(positive integers) before touching the DB.⚙️ Configuration
src/config/env.tsSESSION_COMPACTION_MAX_ROWS_PER_RUN— max rows a single compaction run may delete (default100000).🧪 Tests & 📄 Docs
src/services/sessionCompactionService.test.ts— bad-clock safety, per-run cap enforcement, batch draining, error metrics, input validation, and start/stop lifecycle.src/db/repositories/sessionRepository.test.ts— purge predicates (DB-clock boundary, active-row exclusion) and lag query.docs/session-storage-compaction.md— design doc with security assumptions and failure-path analysis.Verification Results
LIMITbatchesVACUUM sessionson its own connection, only when rows were deletedsession.compaction.rowscountersession.compaction.retention_lag_dayshistogramNOW()boundary + per-run row cap; active rows can never match the predicateTimeline
feat/session-compaction-jobcreated from upstreammaster.Bouynaty <Bouynaty@users.noreply.github.com>.