fix: harden attachment cleanup

This commit is contained in:
2026-10-09 13:27:58 +03:00
parent f8c31de7c0
commit 3fde1c324c
8 changed files with 134 additions and 24 deletions
+15 -4
View File
@@ -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)