forked from mtusi2026/mtusi2026lab5
fix: preserve attachment pipeline invariants
This commit is contained in:
@@ -11,6 +11,7 @@ import (
|
||||
|
||||
type MetadataStore interface {
|
||||
ListAttachments(context.Context, string, int) ([]model.Attachment, string, error)
|
||||
AttachmentObjectKeys(context.Context, string) ([]string, error)
|
||||
AttachmentExistsByObjectKey(context.Context, string) (bool, error)
|
||||
ClaimAttachmentForDeletion(context.Context, model.Attachment, time.Time) (bool, error)
|
||||
DeleteAttachmentIfDeleting(context.Context, string) (bool, error)
|
||||
@@ -158,6 +159,9 @@ func (r *Reconciler) handlePair(
|
||||
report.MetadataScanned++
|
||||
report.ObjectsScanned++
|
||||
switch metadata.Status {
|
||||
case model.AttachmentQueued, model.AttachmentProcessing, model.AttachmentFailed:
|
||||
report.Consistent++
|
||||
return nil
|
||||
case model.AttachmentPending:
|
||||
if metadata.CreatedAt.After(anomalyCutoff) {
|
||||
report.FreshPendingAttachments++
|
||||
@@ -208,10 +212,16 @@ func (r *Reconciler) deleteClaimedPair(ctx context.Context, attachment model.Att
|
||||
if !r.config.Apply {
|
||||
return nil
|
||||
}
|
||||
if err := r.objects.Delete(ctx, attachment.ObjectKey); err != nil {
|
||||
return fmt.Errorf("delete attachment object %q: %w", attachment.ObjectKey, err)
|
||||
objectKeys, err := r.metadata.AttachmentObjectKeys(ctx, attachment.ID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("list attachment %q object keys: %w", attachment.ID, err)
|
||||
}
|
||||
for _, objectKey := range objectKeys {
|
||||
if err := r.objects.Delete(ctx, objectKey); err != nil {
|
||||
return fmt.Errorf("delete attachment object %q: %w", objectKey, err)
|
||||
}
|
||||
report.DeletedAttachmentObjects++
|
||||
}
|
||||
report.DeletedAttachmentObjects++
|
||||
deleted, err := r.metadata.DeleteAttachmentIfDeleting(ctx, attachment.ID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("delete attachment metadata %q: %w", attachment.ID, err)
|
||||
@@ -229,6 +239,14 @@ func (r *Reconciler) handleOrphanObject(
|
||||
report *Report,
|
||||
) error {
|
||||
report.ObjectsScanned++
|
||||
exists, err := r.metadata.AttachmentExistsByObjectKey(ctx, object.Key)
|
||||
if err != nil {
|
||||
return fmt.Errorf("recheck orphan object %q: %w", object.Key, err)
|
||||
}
|
||||
if exists {
|
||||
report.Consistent++
|
||||
return nil
|
||||
}
|
||||
if object.LastModified.After(cutoff) {
|
||||
report.FreshOrphanObjects++
|
||||
return nil
|
||||
@@ -237,13 +255,6 @@ func (r *Reconciler) handleOrphanObject(
|
||||
if !r.config.Apply {
|
||||
return nil
|
||||
}
|
||||
exists, err := r.metadata.AttachmentExistsByObjectKey(ctx, object.Key)
|
||||
if err != nil {
|
||||
return fmt.Errorf("recheck orphan object %q: %w", object.Key, err)
|
||||
}
|
||||
if exists {
|
||||
return nil
|
||||
}
|
||||
if err := r.objects.Delete(ctx, object.Key); err != nil {
|
||||
return fmt.Errorf("delete orphan object %q: %w", object.Key, err)
|
||||
}
|
||||
@@ -258,6 +269,11 @@ func (r *Reconciler) handleMissingObject(
|
||||
report *Report,
|
||||
) error {
|
||||
report.MetadataScanned++
|
||||
if metadata.Status == model.AttachmentQueued ||
|
||||
metadata.Status == model.AttachmentProcessing ||
|
||||
metadata.Status == model.AttachmentFailed {
|
||||
return nil
|
||||
}
|
||||
if metadata.Status == model.AttachmentPending {
|
||||
if metadata.CreatedAt.After(cutoff) {
|
||||
report.FreshPendingAttachments++
|
||||
|
||||
@@ -82,6 +82,98 @@ func TestReconcilerRechecksOrphanBeforeDelete(t *testing.T) {
|
||||
require.Contains(t, objects.items, "attachments/a/original")
|
||||
}
|
||||
|
||||
func TestReconcilerDoesNotReportTrackedStagingObjectAsOrphan(t *testing.T) {
|
||||
now := time.Now().UTC()
|
||||
key := "staging/a/original"
|
||||
metadata := &metadataStub{items: map[string]model.Attachment{
|
||||
key: attachment("a", key, model.AttachmentQueued, now.Add(-time.Hour), false),
|
||||
}}
|
||||
objects := &objectStub{items: map[string]model.StoredObject{
|
||||
key: {Key: key, LastModified: now.Add(-time.Hour)},
|
||||
}}
|
||||
reconciler, err := New(metadata, objects, Config{
|
||||
Prefix: "attachments/", OrphanPrefixes: []string{"staging/"}, PageSize: 1,
|
||||
GraceTime: time.Minute, UnreferencedRetention: time.Hour, Now: func() time.Time { return now },
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
report, err := reconciler.Run(context.Background())
|
||||
require.NoError(t, err)
|
||||
require.Zero(t, report.OldOrphanObjects)
|
||||
require.Zero(t, report.FreshOrphanObjects)
|
||||
require.Contains(t, objects.items, key)
|
||||
}
|
||||
|
||||
func TestReconcilerPreservesAttachmentDuringReprocessing(t *testing.T) {
|
||||
now := time.Date(2026, 10, 9, 12, 0, 0, 0, time.UTC)
|
||||
for _, status := range []string{
|
||||
model.AttachmentQueued,
|
||||
model.AttachmentProcessing,
|
||||
model.AttachmentFailed,
|
||||
} {
|
||||
t.Run(status, func(t *testing.T) {
|
||||
original := attachment("a", "attachments/a/v1/original.png", status, now.Add(-48*time.Hour), false)
|
||||
original.PipelineVersion = 2
|
||||
missing := attachment("a", "attachments/a/v1/optimized.webp", status, now.Add(-48*time.Hour), false)
|
||||
missing.PipelineVersion = 2
|
||||
metadata := &metadataStub{items: map[string]model.Attachment{
|
||||
original.ObjectKey: original,
|
||||
missing.ObjectKey: missing,
|
||||
}}
|
||||
objects := &objectStub{items: map[string]model.StoredObject{
|
||||
original.ObjectKey: {Key: original.ObjectKey, LastModified: now.Add(-48 * time.Hour)},
|
||||
}}
|
||||
reconciler, err := New(metadata, objects, Config{
|
||||
Prefix: "attachments/", PageSize: 10, GraceTime: time.Hour,
|
||||
UnreferencedRetention: 24 * time.Hour, Apply: true, Now: func() time.Time { return now },
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
report, err := reconciler.Run(context.Background())
|
||||
require.NoError(t, err)
|
||||
require.Zero(t, report.DeletedAttachmentObjects)
|
||||
require.Zero(t, report.DeletedUnreferencedRows)
|
||||
require.Contains(t, metadata.items, original.ObjectKey)
|
||||
require.Contains(t, metadata.items, missing.ObjectKey)
|
||||
require.Contains(t, objects.items, original.ObjectKey)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestReconcilerDeletesAllAttachmentVariantsOnce(t *testing.T) {
|
||||
now := time.Date(2026, 10, 9, 12, 0, 0, 0, time.UTC)
|
||||
old := now.Add(-48 * time.Hour)
|
||||
keys := []string{
|
||||
"attachments/a/v1/crop.jpg",
|
||||
"attachments/a/v1/optimized.webp",
|
||||
"attachments/a/v1/original.png",
|
||||
"attachments/a/v1/thumbnail.jpg",
|
||||
}
|
||||
metadata := &metadataStub{items: make(map[string]model.Attachment, len(keys))}
|
||||
objects := &objectStub{items: make(map[string]model.StoredObject, len(keys))}
|
||||
for _, key := range keys {
|
||||
metadata.items[key] = attachment("a", key, model.AttachmentReady, old, false)
|
||||
objects.items[key] = model.StoredObject{Key: key, LastModified: old}
|
||||
}
|
||||
reconciler, err := New(metadata, objects, Config{
|
||||
Prefix: "attachments/", PageSize: 10, GraceTime: time.Hour,
|
||||
UnreferencedRetention: 24 * time.Hour, Apply: true, Now: func() time.Time { return now },
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
report, err := reconciler.Run(context.Background())
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, len(keys), report.DeletedAttachmentObjects)
|
||||
require.Equal(t, 1, report.DeletedUnreferencedRows)
|
||||
require.Empty(t, metadata.items)
|
||||
require.Empty(t, objects.items)
|
||||
|
||||
repeated, err := reconciler.Run(context.Background())
|
||||
require.NoError(t, err)
|
||||
require.Zero(t, repeated.DeletedAttachmentObjects)
|
||||
require.Zero(t, repeated.DeletedUnreferencedRows)
|
||||
}
|
||||
|
||||
func TestNewRejectsInvalidRetention(t *testing.T) {
|
||||
_, err := New(&metadataStub{}, &objectStub{}, Config{PageSize: 1})
|
||||
require.Error(t, err)
|
||||
@@ -100,6 +192,17 @@ type metadataStub struct {
|
||||
appeared map[string]bool
|
||||
}
|
||||
|
||||
func (s *metadataStub) AttachmentObjectKeys(_ context.Context, attachmentID string) ([]string, error) {
|
||||
var keys []string
|
||||
for key, item := range s.items {
|
||||
if item.ID == attachmentID {
|
||||
keys = append(keys, key)
|
||||
}
|
||||
}
|
||||
sort.Strings(keys)
|
||||
return keys, nil
|
||||
}
|
||||
|
||||
func (s *metadataStub) ListAttachments(_ context.Context, after string, limit int) ([]model.Attachment, string, error) {
|
||||
keys := make([]string, 0, len(s.items))
|
||||
for key := range s.items {
|
||||
@@ -129,6 +232,7 @@ func (s *metadataStub) AttachmentExistsByObjectKey(_ context.Context, key string
|
||||
}
|
||||
|
||||
func (s *metadataStub) ClaimAttachmentForDeletion(_ context.Context, candidate model.Attachment, cutoff time.Time) (bool, error) {
|
||||
claim := false
|
||||
for key, item := range s.items {
|
||||
if item.ID != candidate.ID || item.Status != candidate.Status || item.Referenced {
|
||||
continue
|
||||
@@ -142,19 +246,20 @@ func (s *metadataStub) ClaimAttachmentForDeletion(_ context.Context, candidate m
|
||||
}
|
||||
item.Status = model.AttachmentDeleting
|
||||
s.items[key] = item
|
||||
return true, nil
|
||||
claim = true
|
||||
}
|
||||
return false, nil
|
||||
return claim, nil
|
||||
}
|
||||
|
||||
func (s *metadataStub) DeleteAttachmentIfDeleting(_ context.Context, id string) (bool, error) {
|
||||
deleted := false
|
||||
for key, item := range s.items {
|
||||
if item.ID == id && item.Status == model.AttachmentDeleting && !item.Referenced {
|
||||
delete(s.items, key)
|
||||
return true, nil
|
||||
deleted = true
|
||||
}
|
||||
}
|
||||
return false, nil
|
||||
return deleted, nil
|
||||
}
|
||||
|
||||
type objectStub struct {
|
||||
|
||||
Reference in New Issue
Block a user