deploy / plan (push) Successful in 35s
deploy / archied-image (push) Canceled after 0s
deploy / agent-image (push) Canceled after 0s
deploy / dist-zip (push) Canceled after 0s
deploy / gate (push) Canceled after 25s
ci/woodpecker/push/deploy Pipeline was canceled
The retention sweep applied one window to a code-fixed allowlist, and the largest event kind (agent transcripts) was outside it, so the events table grew whatever the instance set. The audit-retention document now takes per-kind overrides, the dashboard offers each prunable kind its own window from the server's list, and a kind without an override follows the default. A security kind is refused where the document is written, and the sweep cuts each kind at its own cutoff in one bounded, locked transaction.
99 lines
3.2 KiB
Go
99 lines
3.2 KiB
Go
package postgres
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"maps"
|
|
"slices"
|
|
"time"
|
|
|
|
"github.com/samcharles93/archie-core/internal/domain/access"
|
|
"github.com/samcharles93/archie-core/internal/domain/storecontract"
|
|
"github.com/samcharles93/archie-core/internal/infrastructure/postgres/postgresdb"
|
|
)
|
|
|
|
const (
|
|
// auditPruneBatch is one delete's bound; auditPruneMaxBatches keeps a
|
|
// single sweep from owning the table for an unbounded time. The budget is
|
|
// shared across every kind the sweep prunes.
|
|
auditPruneBatch = 10_000
|
|
auditPruneMaxBatches = 100
|
|
|
|
auditRetentionActor = "archie-state-store"
|
|
auditRetentionSource = "audit retention"
|
|
)
|
|
|
|
// ErrAuditPruneBusy reports that another audit retention sweep holds the lock.
|
|
// The caller skips its tick rather than waiting.
|
|
var ErrAuditPruneBusy = errors.New("postgres: audit prune already running")
|
|
|
|
// auditPruneRecord is the value stored on the sweep's audit row.
|
|
type auditPruneRecord struct {
|
|
// Rules is the delete-before instant each pruned kind was cut at, so the
|
|
// audit row says what the setting asked for, not just how much went.
|
|
Rules map[string]time.Time `json:"rules"`
|
|
Rows int64 `json:"rows"`
|
|
}
|
|
|
|
// PruneAuditEvents deletes events of the given kinds older than each kind's
|
|
// cutoff in bounded batches and appends one sys_audit row recording the rules
|
|
// applied and the total removed, only when it is non-zero. The whole sweep is
|
|
// one transaction under the audit prune lock, so the lock covers every batch
|
|
// and a concurrent sweep gets ErrAuditPruneBusy without touching a row. The
|
|
// kinds are the caller's policy: this method deletes nothing else.
|
|
func (s *Store) PruneAuditEvents(ctx context.Context, cutoffs map[string]time.Time) (int64, error) {
|
|
if len(cutoffs) == 0 {
|
|
return 0, nil
|
|
}
|
|
tx, err := s.pool.Begin(ctx)
|
|
if err != nil {
|
|
return 0, fmt.Errorf("prune audit events: %w", err)
|
|
}
|
|
defer func() { _ = tx.Rollback(ctx) }()
|
|
|
|
q := postgresdb.New(tx)
|
|
locked, err := q.TryAuditPruneLock(ctx)
|
|
if err != nil {
|
|
return 0, fmt.Errorf("audit prune lock: %w", err)
|
|
}
|
|
if !locked {
|
|
return 0, ErrAuditPruneBusy
|
|
}
|
|
var total int64
|
|
budget := auditPruneMaxBatches
|
|
for _, kind := range slices.Sorted(maps.Keys(cutoffs)) {
|
|
cutoff := cutoffs[kind].UTC()
|
|
for budget > 0 {
|
|
removed, err := q.PruneAuditEventBatch(ctx, postgresdb.PruneAuditEventBatchParams{
|
|
Kinds: []string{kind}, Cutoff: cutoff, Batch: auditPruneBatch,
|
|
})
|
|
if err != nil {
|
|
return 0, fmt.Errorf("prune audit events: %w", err)
|
|
}
|
|
total += removed
|
|
budget--
|
|
if removed < auditPruneBatch {
|
|
break
|
|
}
|
|
}
|
|
}
|
|
if total > 0 {
|
|
rules := make(map[string]time.Time, len(cutoffs))
|
|
for kind, cutoff := range cutoffs {
|
|
rules[kind] = cutoff.UTC()
|
|
}
|
|
// The sweep is not an RPC, so it attributes its own row: the State
|
|
// Store pruned the events and the retention setting decided how many.
|
|
ctx = access.WithActor(access.WithSource(ctx, auditRetentionSource), auditRetentionActor)
|
|
if err := insertAuditValues(ctx, q, "audit_retention", "runtime_events", "pruned", nil,
|
|
auditPruneRecord{Rules: rules, Rows: total}, 0, storecontract.DefaultOrgID); err != nil {
|
|
return 0, err
|
|
}
|
|
}
|
|
if err := tx.Commit(ctx); err != nil {
|
|
return 0, fmt.Errorf("prune audit events: %w", err)
|
|
}
|
|
return total, nil
|
|
}
|