forked from mtusi2026/mtusi2026lab4
fix: harden attachment cleanup
This commit is contained in:
@@ -12,7 +12,7 @@ import (
|
||||
type MetadataStore interface {
|
||||
ListAttachments(context.Context, string, int) ([]model.Attachment, string, error)
|
||||
AttachmentExistsByObjectKey(context.Context, string) (bool, error)
|
||||
ClaimAttachmentForDeletion(context.Context, string, time.Time) (bool, error)
|
||||
ClaimAttachmentForDeletion(context.Context, model.Attachment, time.Time) (bool, error)
|
||||
DeleteAttachmentIfDeleting(context.Context, string) (bool, error)
|
||||
}
|
||||
|
||||
@@ -36,6 +36,7 @@ type Report struct {
|
||||
Consistent int `json:"consistent"`
|
||||
FreshPendingAttachments int `json:"fresh_pending_attachments"`
|
||||
OldPendingAttachments int `json:"old_pending_attachments"`
|
||||
DeletingAttachments int `json:"deleting_attachments"`
|
||||
FreshUnreferencedAttachments int `json:"fresh_unreferenced_attachments"`
|
||||
OldUnreferencedAttachments int `json:"old_unreferenced_attachments"`
|
||||
DeletedAttachmentObjects int `json:"deleted_attachment_objects"`
|
||||
@@ -124,19 +125,21 @@ func (r *Reconciler) handlePair(
|
||||
) error {
|
||||
report.MetadataScanned++
|
||||
report.ObjectsScanned++
|
||||
report.Consistent++
|
||||
switch metadata.Status {
|
||||
case model.AttachmentPending:
|
||||
if metadata.CreatedAt.After(anomalyCutoff) {
|
||||
report.FreshPendingAttachments++
|
||||
report.Consistent++
|
||||
return nil
|
||||
}
|
||||
report.OldPendingAttachments++
|
||||
return r.deletePair(ctx, metadata, anomalyCutoff, report)
|
||||
case model.AttachmentDeleting:
|
||||
report.DeletingAttachments++
|
||||
return r.deleteClaimedPair(ctx, metadata, report)
|
||||
case model.AttachmentReady:
|
||||
if metadata.Referenced {
|
||||
report.Consistent++
|
||||
return nil
|
||||
}
|
||||
age := metadata.CreatedAt
|
||||
@@ -145,6 +148,7 @@ func (r *Reconciler) handlePair(
|
||||
}
|
||||
if age.After(retentionCutoff) {
|
||||
report.FreshUnreferencedAttachments++
|
||||
report.Consistent++
|
||||
return nil
|
||||
}
|
||||
report.OldUnreferencedAttachments++
|
||||
@@ -158,7 +162,7 @@ func (r *Reconciler) deletePair(ctx context.Context, attachment model.Attachment
|
||||
if !r.config.Apply {
|
||||
return nil
|
||||
}
|
||||
claimed, err := r.metadata.ClaimAttachmentForDeletion(ctx, attachment.ID, cutoff)
|
||||
claimed, err := r.metadata.ClaimAttachmentForDeletion(ctx, attachment, cutoff)
|
||||
if err != nil {
|
||||
return fmt.Errorf("claim attachment %q: %w", attachment.ID, err)
|
||||
}
|
||||
@@ -222,6 +226,9 @@ func (r *Reconciler) handleMissingObject(
|
||||
report *Report,
|
||||
) error {
|
||||
report.MetadataScanned++
|
||||
if metadata.Status == model.AttachmentDeleting {
|
||||
return r.deleteClaimedMetadata(ctx, metadata, report)
|
||||
}
|
||||
if metadata.CreatedAt.After(cutoff) {
|
||||
report.FreshMissingObjects++
|
||||
return nil
|
||||
@@ -234,13 +241,17 @@ func (r *Reconciler) handleMissingObject(
|
||||
if !r.config.Apply {
|
||||
return nil
|
||||
}
|
||||
claimed, err := r.metadata.ClaimAttachmentForDeletion(ctx, metadata.ID, cutoff)
|
||||
claimed, err := r.metadata.ClaimAttachmentForDeletion(ctx, metadata, cutoff)
|
||||
if err != nil {
|
||||
return fmt.Errorf("claim missing attachment metadata %q: %w", metadata.ID, err)
|
||||
}
|
||||
if !claimed {
|
||||
return nil
|
||||
}
|
||||
return r.deleteClaimedMetadata(ctx, metadata, report)
|
||||
}
|
||||
|
||||
func (r *Reconciler) deleteClaimedMetadata(ctx context.Context, metadata model.Attachment, report *Report) error {
|
||||
deleted, err := r.metadata.DeleteAttachmentIfDeleting(ctx, metadata.ID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("delete missing attachment metadata %q: %w", metadata.ID, err)
|
||||
|
||||
@@ -121,13 +121,21 @@ func (s *metadataStub) AttachmentExistsByObjectKey(_ context.Context, key string
|
||||
return exists, nil
|
||||
}
|
||||
|
||||
func (s *metadataStub) ClaimAttachmentForDeletion(_ context.Context, id string, cutoff time.Time) (bool, error) {
|
||||
func (s *metadataStub) ClaimAttachmentForDeletion(_ context.Context, candidate model.Attachment, cutoff time.Time) (bool, error) {
|
||||
for key, item := range s.items {
|
||||
if item.ID == id && !item.CreatedAt.After(cutoff) && !item.Referenced {
|
||||
item.Status = model.AttachmentDeleting
|
||||
s.items[key] = item
|
||||
return true, nil
|
||||
if item.ID != candidate.ID || item.Status != candidate.Status || item.Referenced {
|
||||
continue
|
||||
}
|
||||
age := item.CreatedAt
|
||||
if item.Status == model.AttachmentReady && item.ReadyAt != nil {
|
||||
age = *item.ReadyAt
|
||||
}
|
||||
if age.After(cutoff) {
|
||||
return false, nil
|
||||
}
|
||||
item.Status = model.AttachmentDeleting
|
||||
s.items[key] = item
|
||||
return true, nil
|
||||
}
|
||||
return false, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user