forked from mtusi2026/mtusi2026lab4
feat: restricted S3 credentials, orphan reconciler, upload limits (grade 4)
This commit is contained in:
@@ -0,0 +1,97 @@
|
||||
package reconcile
|
||||
|
||||
import (
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
"uuid"
|
||||
)
|
||||
|
||||
type Object struct {
|
||||
Key string
|
||||
Size int64
|
||||
LastModified time.Time
|
||||
}
|
||||
|
||||
type Metadata struct {
|
||||
ID uuid.UUID
|
||||
ObjectKey string
|
||||
ChatID uuid.UUID
|
||||
CreatedAt time.Time
|
||||
}
|
||||
|
||||
type Orphan struct {
|
||||
Key string `json:"key"`
|
||||
SizeBytes int64 `json:"size_bytes"`
|
||||
LastModified string `json:"last_modified"`
|
||||
AgeSeconds int64 `json:"age_seconds"`
|
||||
}
|
||||
|
||||
type Missing struct {
|
||||
ID string `json:"id"`
|
||||
ObjectKey string `json:"object_key"`
|
||||
ChatID string `json:"chat_id"`
|
||||
CreatedAt string `json:"created_at"`
|
||||
}
|
||||
|
||||
type Report struct {
|
||||
Prefix string `json:"prefix"`
|
||||
GraceSeconds int64 `json:"grace_seconds"`
|
||||
DryRun bool `json:"dry_run"`
|
||||
ScannedObjects int `json:"scanned_objects"`
|
||||
ScannedRows int `json:"scanned_rows"`
|
||||
Truncated bool `json:"truncated"`
|
||||
Orphans []Orphan `json:"orphans"`
|
||||
Missing []Missing `json:"missing"`
|
||||
Deleted int `json:"deleted_objects"`
|
||||
DeleteFailures int `json:"delete_failures"`
|
||||
DurationMS int64 `json:"duration_ms"`
|
||||
}
|
||||
|
||||
func Plan(objects []Object, rows []Metadata, prefix string, now time.Time, grace time.Duration) ([]Orphan, []Missing) {
|
||||
referenced := make(map[string]struct{}, len(rows))
|
||||
existing := make(map[string]struct{}, len(objects))
|
||||
for _, row := range rows {
|
||||
referenced[row.ObjectKey] = struct{}{}
|
||||
}
|
||||
for _, object := range objects {
|
||||
existing[object.Key] = struct{}{}
|
||||
}
|
||||
|
||||
orphans := make([]Orphan, 0)
|
||||
for _, object := range objects {
|
||||
if !strings.HasPrefix(object.Key, prefix) {
|
||||
continue
|
||||
}
|
||||
if _, ok := referenced[object.Key]; ok {
|
||||
continue
|
||||
}
|
||||
age := now.Sub(object.LastModified)
|
||||
if age < grace {
|
||||
continue
|
||||
}
|
||||
orphans = append(orphans, Orphan{
|
||||
Key: object.Key,
|
||||
SizeBytes: object.Size,
|
||||
LastModified: object.LastModified.UTC().Format(time.RFC3339),
|
||||
AgeSeconds: int64(age.Seconds()),
|
||||
})
|
||||
}
|
||||
|
||||
missing := make([]Missing, 0)
|
||||
for _, row := range rows {
|
||||
if _, ok := existing[row.ObjectKey]; ok {
|
||||
continue
|
||||
}
|
||||
missing = append(missing, Missing{
|
||||
ID: row.ID.String(),
|
||||
ObjectKey: row.ObjectKey,
|
||||
ChatID: row.ChatID.String(),
|
||||
CreatedAt: row.CreatedAt.UTC().Format(time.RFC3339),
|
||||
})
|
||||
}
|
||||
|
||||
sort.Slice(orphans, func(i, j int) bool { return orphans[i].Key < orphans[j].Key })
|
||||
sort.Slice(missing, func(i, j int) bool { return missing[i].ObjectKey < missing[j].ObjectKey })
|
||||
return orphans, missing
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
package reconcile
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
"uuid"
|
||||
)
|
||||
|
||||
func TestPlanDetectsOrphansAndMissing(t *testing.T) {
|
||||
now := time.Date(2026, 11, 20, 12, 0, 0, 0, time.UTC)
|
||||
usedID, missingID := uuid.New(), uuid.New()
|
||||
objects := []Object{
|
||||
{Key: "chats/a/used", Size: 10, LastModified: now.Add(-2 * time.Hour)},
|
||||
{Key: "chats/a/orphan-old", Size: 20, LastModified: now.Add(-2 * time.Hour)},
|
||||
{Key: "chats/a/orphan-fresh", Size: 30, LastModified: now.Add(-1 * time.Minute)},
|
||||
{Key: "other/prefix/ignored", Size: 40, LastModified: now.Add(-2 * time.Hour)},
|
||||
}
|
||||
rows := []Metadata{
|
||||
{ID: usedID, ObjectKey: "chats/a/used", ChatID: uuid.New(), CreatedAt: now.Add(-2 * time.Hour)},
|
||||
{ID: missingID, ObjectKey: "chats/a/missing", ChatID: uuid.New(), CreatedAt: now.Add(-2 * time.Hour)},
|
||||
}
|
||||
|
||||
orphans, missing := Plan(objects, rows, "chats/", now, time.Hour)
|
||||
|
||||
if len(orphans) != 1 || orphans[0].Key != "chats/a/orphan-old" {
|
||||
t.Fatalf("orphans = %+v, want only chats/a/orphan-old", orphans)
|
||||
}
|
||||
if len(missing) != 1 || missing[0].ObjectKey != "chats/a/missing" {
|
||||
t.Fatalf("missing = %+v, want only chats/a/missing", missing)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPlanHonoursGraceAndPrefix(t *testing.T) {
|
||||
now := time.Date(2026, 11, 20, 12, 0, 0, 0, time.UTC)
|
||||
objects := []Object{
|
||||
{Key: "chats/a/fresh", Size: 1, LastModified: now.Add(-30 * time.Second)},
|
||||
{Key: "outside/key", Size: 1, LastModified: now.Add(-48 * time.Hour)},
|
||||
}
|
||||
orphans, _ := Plan(objects, nil, "chats/", now, time.Hour)
|
||||
if len(orphans) != 0 {
|
||||
t.Fatalf("orphans = %+v, want none", orphans)
|
||||
}
|
||||
|
||||
orphans, _ = Plan(objects, nil, "chats/", now, 0)
|
||||
if len(orphans) != 1 || orphans[0].Key != "chats/a/fresh" {
|
||||
t.Fatalf("orphans with zero grace = %+v, want chats/a/fresh", orphans)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPlanNeverDeletesReferencedObjects(t *testing.T) {
|
||||
now := time.Date(2026, 11, 20, 12, 0, 0, 0, time.UTC)
|
||||
objects := []Object{{Key: "chats/a/used", Size: 1, LastModified: now.Add(-100 * time.Hour)}}
|
||||
rows := []Metadata{{ID: uuid.New(), ObjectKey: "chats/a/used", ChatID: uuid.New(), CreatedAt: now.Add(-100 * time.Hour)}}
|
||||
|
||||
orphans, missing := Plan(objects, rows, "chats/", now, 0)
|
||||
if len(orphans) != 0 {
|
||||
t.Fatalf("orphans = %+v, want none for referenced object", orphans)
|
||||
}
|
||||
if len(missing) != 0 {
|
||||
t.Fatalf("missing = %+v, want none", missing)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPlanOutputIsSorted(t *testing.T) {
|
||||
now := time.Date(2026, 11, 20, 12, 0, 0, 0, time.UTC)
|
||||
objects := []Object{
|
||||
{Key: "chats/a/z", LastModified: now.Add(-time.Hour)},
|
||||
{Key: "chats/a/b", LastModified: now.Add(-time.Hour)},
|
||||
{Key: "chats/a/m", LastModified: now.Add(-time.Hour)},
|
||||
}
|
||||
orphans, _ := Plan(objects, nil, "chats/", now, 0)
|
||||
if len(orphans) != 3 || orphans[0].Key != "chats/a/b" || orphans[2].Key != "chats/a/z" {
|
||||
t.Fatalf("orphans are not sorted: %+v", orphans)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user