83 lines
2.7 KiB
Go
83 lines
2.7 KiB
Go
package postgres
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"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.
|
|
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 {
|
|
Cutoff time.Time `json:"cutoff"`
|
|
Rows int64 `json:"rows"`
|
|
}
|
|
|
|
// PruneAuditEvents deletes events of the given kinds older than cutoff in
|
|
// bounded batches and appends one sys_audit row recording the cutoff and the
|
|
// total removed, even when that total is 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 kind
|
|
// list is the caller's allowlist: this method deletes nothing else.
|
|
func (s *Store) PruneAuditEvents(ctx context.Context, kinds []string, cutoff time.Time) (int64, error) {
|
|
if len(kinds) == 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
|
|
for range auditPruneMaxBatches {
|
|
removed, err := q.PruneAuditEventBatch(ctx, postgresdb.PruneAuditEventBatchParams{
|
|
Kinds: kinds, Cutoff: cutoff.UTC(), Batch: auditPruneBatch,
|
|
})
|
|
if err != nil {
|
|
return 0, fmt.Errorf("prune audit events: %w", err)
|
|
}
|
|
total += removed
|
|
if removed < auditPruneBatch {
|
|
break
|
|
}
|
|
}
|
|
// 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{Cutoff: cutoff.UTC(), 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
|
|
}
|