forked from mtusi2026/mtusi2026lab6
feat: add bounded observability and verified SLO incident alerts
This commit is contained in:
@@ -0,0 +1,102 @@
|
||||
package media
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab6/internal/model/dto"
|
||||
)
|
||||
|
||||
// Inventory supports bounded, resumable scans and guarded state transitions.
|
||||
type Inventory interface {
|
||||
// Scan returns the next at most limit metadata records by ID.
|
||||
Scan(context.Context, uuid.UUID, int) ([]dto.Attachment, error)
|
||||
// Reclaim deletes only an old unfinished unreferenced reservation after claiming it.
|
||||
Reclaim(context.Context, uuid.UUID, time.Time, bool) (bool, error)
|
||||
// MarkMissing preserves metadata and references when an original disappears.
|
||||
MarkMissing(context.Context, uuid.UUID, bool) error
|
||||
}
|
||||
|
||||
// ObjectInventory exposes bounded inspection and explicit removal.
|
||||
type ObjectInventory interface {
|
||||
// Exists distinguishes absence from infrastructure failure.
|
||||
Exists(context.Context, string) (bool, error)
|
||||
// Remove deletes a single immutable object key.
|
||||
Remove(context.Context, string) error
|
||||
}
|
||||
|
||||
// ReconcileResult records observations and the continuation cursor without file contents.
|
||||
type ReconcileResult struct {
|
||||
// Scanned counts inspected metadata rows.
|
||||
Scanned int `json:"scanned"`
|
||||
// Missing counts published originals absent from S3.
|
||||
Missing int `json:"missing"`
|
||||
// Abandoned counts old incomplete reservations eligible for cleanup.
|
||||
Abandoned int `json:"abandoned"`
|
||||
// Changed counts successful applied repairs, zero for dry run.
|
||||
Changed int `json:"changed"`
|
||||
// Next continues scanning without retaining the full database in RAM.
|
||||
Next uuid.UUID `json:"next"`
|
||||
}
|
||||
|
||||
// Reconcile inspects at most 1000 records and protects active uploads with a one-hour minimum grace.
|
||||
func Reconcile(ctx context.Context, metadata Inventory, objects ObjectInventory, after uuid.UUID, grace time.Duration, apply bool) (ReconcileResult, error) {
|
||||
var result ReconcileResult
|
||||
if grace < time.Hour {
|
||||
return result, fmt.Errorf("reconciliation grace must be at least one hour")
|
||||
}
|
||||
records, err := metadata.Scan(ctx, after, 1000)
|
||||
if err != nil {
|
||||
return result, err
|
||||
}
|
||||
cutoff := time.Now().UTC().Add(-grace)
|
||||
for _, a := range records {
|
||||
result.Scanned++
|
||||
result.Next = a.ID
|
||||
if a.Status == "queued" || a.Status == "processing" || a.Status == "failed" || a.CreatedAt.After(cutoff) {
|
||||
continue
|
||||
}
|
||||
exists, err := objects.Exists(ctx, a.ObjectKey)
|
||||
if err != nil {
|
||||
return result, err
|
||||
}
|
||||
switch {
|
||||
case a.Status == "uploading" || a.Status == "deleting":
|
||||
result.Abandoned++
|
||||
if !apply {
|
||||
continue
|
||||
}
|
||||
claimed, err := metadata.Reclaim(ctx, a.ID, cutoff, false)
|
||||
if err != nil {
|
||||
return result, err
|
||||
}
|
||||
if !claimed {
|
||||
continue
|
||||
}
|
||||
if err := objects.Remove(ctx, a.ObjectKey); err != nil {
|
||||
return result, err
|
||||
}
|
||||
if _, err := metadata.Reclaim(ctx, a.ID, cutoff, true); err != nil {
|
||||
return result, err
|
||||
}
|
||||
result.Changed++
|
||||
case !exists:
|
||||
result.Missing++
|
||||
if apply && a.Status != "missing" {
|
||||
if err := metadata.MarkMissing(ctx, a.ID, true); err != nil {
|
||||
return result, err
|
||||
}
|
||||
result.Changed++
|
||||
}
|
||||
case a.Status == "missing" && apply:
|
||||
if err := metadata.MarkMissing(ctx, a.ID, false); err != nil {
|
||||
return result, err
|
||||
}
|
||||
result.Changed++
|
||||
}
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
Reference in New Issue
Block a user