mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
feat: purge boundary logs past retention (#24815)
Add a periodic purge job for `boundary_logs` rows past their retention threshold, following the same pattern as the existing audit log and connection log purge jobs in `dbpurge`. Expose a `--boundary-log-retention` deployment flag (env `CODER_BOUNDARY_LOG_RETENTION`, YAML `retention.boundary_logs`). Default is `0` (keep indefinitely). When set to a positive duration, `purgeTick` deletes rows where `captured_at` is older than the threshold in batches of 10,000, matching other log purge operations. The `boundary_logs` label is added to the `records_purged_total` Prometheus counter. Also removes the random-UUID fallback for `OwnerID` in `dbgen.BoundarySession`. The previous fallback generated a UUID that could never satisfy the `boundary_sessions_owner_id_fkey` FK constraint, masking test setup bugs. Callers must now provide a valid user ID or accept NULL (the legitimate "user deleted" state).
This commit is contained in:
Generated
+31
@@ -3622,6 +3622,37 @@ func (q *sqlQuerier) DeleteOldBoundaryLogs(ctx context.Context, arg DeleteOldBou
|
||||
return result.RowsAffected()
|
||||
}
|
||||
|
||||
const deleteOldBoundarySessions = `-- name: DeleteOldBoundarySessions :execrows
|
||||
WITH old_sessions AS (
|
||||
SELECT bs.id
|
||||
FROM boundary_sessions bs
|
||||
WHERE bs.updated_at < $1::timestamptz
|
||||
AND NOT EXISTS (
|
||||
SELECT 1 FROM boundary_logs bl WHERE bl.session_id = bs.id
|
||||
)
|
||||
ORDER BY bs.updated_at ASC
|
||||
LIMIT $2
|
||||
)
|
||||
DELETE FROM boundary_sessions
|
||||
USING old_sessions
|
||||
WHERE boundary_sessions.id = old_sessions.id
|
||||
`
|
||||
|
||||
type DeleteOldBoundarySessionsParams struct {
|
||||
BeforeTime time.Time `db:"before_time" json:"before_time"`
|
||||
LimitCount int32 `db:"limit_count" json:"limit_count"`
|
||||
}
|
||||
|
||||
// Deletes boundary sessions that have aged past retention and no longer
|
||||
// have any associated logs.
|
||||
func (q *sqlQuerier) DeleteOldBoundarySessions(ctx context.Context, arg DeleteOldBoundarySessionsParams) (int64, error) {
|
||||
result, err := q.db.ExecContext(ctx, deleteOldBoundarySessions, arg.BeforeTime, arg.LimitCount)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return result.RowsAffected()
|
||||
}
|
||||
|
||||
const getBoundaryLogByID = `-- name: GetBoundaryLogByID :one
|
||||
SELECT id, session_id, sequence_number, captured_at, created_at, proto, method, detail, matched_rule FROM boundary_logs WHERE id = $1
|
||||
`
|
||||
|
||||
Reference in New Issue
Block a user