forked from mtusi2026/mtusi2026lab5
fix: preserve attachment pipeline invariants
This commit is contained in:
@@ -14,7 +14,10 @@ import (
|
||||
"gitea.privcloud.tech/dizi/mtusi2026lab5/internal/outbox"
|
||||
)
|
||||
|
||||
const payloadField = "payload"
|
||||
const (
|
||||
payloadField = "payload"
|
||||
failedPayloadMaxBytes = 64 << 10
|
||||
)
|
||||
|
||||
var failureCodePattern = regexp.MustCompile(`^[a-z][a-z0-9_]{0,63}$`)
|
||||
|
||||
@@ -167,6 +170,7 @@ func (b *Broker) deliveries(streams []redis.XStream) ([]Delivery, error) {
|
||||
deliveries = append(deliveries, delivery)
|
||||
continue
|
||||
}
|
||||
delivery.payload = boundedPayload(payload)
|
||||
envelope, err := DecodeEnvelope(payload)
|
||||
if err != nil {
|
||||
delivery.DecodeError = fmt.Errorf("decode redis stream message %s: %w", message.ID, err)
|
||||
@@ -184,6 +188,13 @@ func (b *Broker) deliveries(streams []redis.XStream) ([]Delivery, error) {
|
||||
return deliveries, nil
|
||||
}
|
||||
|
||||
func boundedPayload(payload []byte) []byte {
|
||||
if len(payload) > failedPayloadMaxBytes {
|
||||
payload = payload[:failedPayloadMaxBytes]
|
||||
}
|
||||
return append([]byte(nil), payload...)
|
||||
}
|
||||
|
||||
func messagePayload(message redis.XMessage) ([]byte, error) {
|
||||
value, ok := message.Values[payloadField]
|
||||
if !ok {
|
||||
|
||||
@@ -47,7 +47,26 @@ func TestBrokerRejectsPayloadWithStorageMetadata(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
require.Len(t, deliveries, 1)
|
||||
require.Error(t, deliveries[0].DecodeError)
|
||||
require.Empty(t, deliveries[0].payload)
|
||||
require.Contains(t, string(deliveries[0].payload), `"storage_key":"private/object"`)
|
||||
}
|
||||
|
||||
func TestBrokerBoundsMalformedPayloadForFailedStream(t *testing.T) {
|
||||
payload := make([]byte, failedPayloadMaxBytes+1)
|
||||
for index := range payload {
|
||||
payload[index] = 'x'
|
||||
}
|
||||
broker := &Broker{config: Config{Stream: "attachment-jobs"}}
|
||||
deliveries, err := broker.deliveries([]redis.XStream{{
|
||||
Stream: "attachment-jobs",
|
||||
Messages: []redis.XMessage{{
|
||||
ID: "1-0",
|
||||
Values: map[string]any{payloadField: payload},
|
||||
}},
|
||||
}})
|
||||
require.NoError(t, err)
|
||||
require.Len(t, deliveries, 1)
|
||||
require.Error(t, deliveries[0].DecodeError)
|
||||
require.Len(t, deliveries[0].payload, failedPayloadMaxBytes)
|
||||
}
|
||||
|
||||
func TestPublishRejectsInvalidPayloadBeforeRedisCall(t *testing.T) {
|
||||
|
||||
@@ -361,7 +361,7 @@ func (s *Store) CompleteAttachmentJob(
|
||||
lease model.AttachmentJobLease,
|
||||
variants []model.Variant,
|
||||
) (model.JobMutationResult, error) {
|
||||
if !validCompletedVariants(variants) {
|
||||
if !validCompletedVariants(variants, lease.Job.PipelineVersion) {
|
||||
return "", model.ErrInvalidInput
|
||||
}
|
||||
tx, err := s.pool.BeginTx(ctx, pgx.TxOptions{})
|
||||
@@ -423,20 +423,23 @@ func (s *Store) CompleteAttachmentJob(
|
||||
return model.JobMutationApplied, nil
|
||||
}
|
||||
|
||||
func validCompletedVariants(variants []model.Variant) bool {
|
||||
seen := make(map[string]struct{}, len(variants))
|
||||
for _, variant := range variants {
|
||||
if variant.Name == "" || variant.ObjectKey == "" {
|
||||
return false
|
||||
}
|
||||
if _, duplicate := seen[variant.Name]; duplicate {
|
||||
return false
|
||||
}
|
||||
seen[variant.Name] = struct{}{}
|
||||
func validCompletedVariants(variants []model.Variant, pipelineVersion int) bool {
|
||||
if len(variants) != 4 || pipelineVersion < 1 {
|
||||
return false
|
||||
}
|
||||
_, hasOriginal := seen["original"]
|
||||
_, hasThumbnail := seen["thumbnail"]
|
||||
return hasOriginal && hasThumbnail
|
||||
required := map[string]struct{}{
|
||||
"original": {}, "thumbnail": {}, "crop": {}, "optimized": {},
|
||||
}
|
||||
for _, variant := range variants {
|
||||
if variant.ObjectKey == "" || variant.PipelineVersion != pipelineVersion {
|
||||
return false
|
||||
}
|
||||
if _, exists := required[variant.Name]; !exists {
|
||||
return false
|
||||
}
|
||||
delete(required, variant.Name)
|
||||
}
|
||||
return len(required) == 0
|
||||
}
|
||||
|
||||
func (s *Store) RetryAttachmentJob(
|
||||
|
||||
@@ -174,16 +174,51 @@ func (s *Store) AttachmentExistsByObjectKey(ctx context.Context, objectKey strin
|
||||
return exists, nil
|
||||
}
|
||||
|
||||
func (s *Store) AttachmentObjectKeys(ctx context.Context, attachmentID string) ([]string, error) {
|
||||
const query = `
|
||||
SELECT object_key
|
||||
FROM (
|
||||
SELECT object_key
|
||||
FROM attachments
|
||||
WHERE id = $1 AND object_key <> ''
|
||||
UNION
|
||||
SELECT object_key
|
||||
FROM attachment_variants
|
||||
WHERE attachment_id = $1
|
||||
) objects
|
||||
ORDER BY object_key`
|
||||
rows, err := s.pool.Query(ctx, query, attachmentID)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("list attachment object keys: %w", err)
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
keys, err := pgx.CollectRows(rows, pgx.RowTo[string])
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("scan attachment object keys: %w", err)
|
||||
}
|
||||
return keys, nil
|
||||
}
|
||||
|
||||
func (s *Store) ListAttachments(ctx context.Context, afterObjectKey string, limit int) ([]model.Attachment, string, error) {
|
||||
if limit < 1 || limit > 1000 {
|
||||
return nil, "", fmt.Errorf("attachment list limit must be between 1 and 1000")
|
||||
}
|
||||
query := `
|
||||
SELECT ` + attachmentColumnsWithAlias("a") + `,
|
||||
EXISTS (SELECT 1 FROM message_attachments ma WHERE ma.attachment_id = a.id)
|
||||
FROM attachments a
|
||||
WHERE a.object_key > $1
|
||||
ORDER BY a.object_key
|
||||
EXISTS (SELECT 1 FROM message_attachments ma WHERE ma.attachment_id = a.id),
|
||||
objects.object_key
|
||||
FROM (
|
||||
SELECT id AS attachment_id, object_key
|
||||
FROM attachments
|
||||
WHERE object_key <> ''
|
||||
UNION
|
||||
SELECT attachment_id, object_key
|
||||
FROM attachment_variants
|
||||
) objects
|
||||
JOIN attachments a ON a.id = objects.attachment_id
|
||||
WHERE objects.object_key > $1
|
||||
ORDER BY objects.object_key
|
||||
LIMIT $2`
|
||||
rows, err := s.pool.Query(ctx, query, afterObjectKey, limit+1)
|
||||
if err != nil {
|
||||
@@ -193,7 +228,9 @@ func (s *Store) ListAttachments(ctx context.Context, afterObjectKey string, limi
|
||||
|
||||
attachments, err := pgx.CollectRows(rows, pgx.RowToFunc[model.Attachment](func(row pgx.CollectableRow) (model.Attachment, error) {
|
||||
var attachment model.Attachment
|
||||
err := scanAttachmentWithReference(row, &attachment)
|
||||
var objectKey string
|
||||
err := scanAttachmentWithReferenceAndObjectKey(row, &attachment, &objectKey)
|
||||
attachment.ObjectKey = objectKey
|
||||
return publicAttachment(attachment), err
|
||||
}))
|
||||
if err != nil {
|
||||
@@ -207,6 +244,25 @@ func (s *Store) ListAttachments(ctx context.Context, afterObjectKey string, limi
|
||||
return attachments, next, nil
|
||||
}
|
||||
|
||||
func scanAttachmentWithReferenceAndObjectKey(
|
||||
row rowScanner,
|
||||
attachment *model.Attachment,
|
||||
objectKey *string,
|
||||
) error {
|
||||
var stagingKey *string
|
||||
err := row.Scan(
|
||||
&attachment.ID, &attachment.ChatID, &attachment.OwnerID, &attachment.Filename,
|
||||
&attachment.ContentType, &attachment.SizeBytes, &attachment.SHA256, &attachment.ObjectKey,
|
||||
&attachment.Status, &attachment.CreatedAt, &attachment.UpdatedAt, &attachment.ReadyAt,
|
||||
&attachment.PipelineVersion, &attachment.ErrorCode, &stagingKey,
|
||||
&attachment.Referenced, objectKey,
|
||||
)
|
||||
if stagingKey != nil {
|
||||
attachment.StagingKey = *stagingKey
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func scanAttachmentWithReference(row rowScanner, attachment *model.Attachment) error {
|
||||
var stagingKey *string
|
||||
err := row.Scan(
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"sync"
|
||||
@@ -204,18 +205,7 @@ func (s *PostgresSuite) TestAttachmentJobLifecycleAndOutboxFencing() {
|
||||
s.Equal(model.AttachmentProcessing, lease.Attachment.Status)
|
||||
s.NotEmpty(lease.Attachment.StagingKey)
|
||||
|
||||
variants := []model.Variant{
|
||||
{
|
||||
Name: "original", ContentType: "image/png", SizeBytes: 128,
|
||||
SHA256: strings.Repeat("a", 64), Width: 320, Height: 200,
|
||||
ObjectKey: "attachments/" + attachment.ID + "/v1/original.png",
|
||||
},
|
||||
{
|
||||
Name: "thumbnail", ContentType: "image/jpeg", SizeBytes: 64,
|
||||
SHA256: strings.Repeat("b", 64), Width: 256, Height: 160,
|
||||
ObjectKey: "attachments/" + attachment.ID + "/v1/thumbnail.jpg",
|
||||
},
|
||||
}
|
||||
variants := completedVariants(attachment.ID, 1)
|
||||
result, err := s.store.CompleteAttachmentJob(context.Background(), lease, variants)
|
||||
s.Require().NoError(err)
|
||||
s.Equal(model.JobMutationApplied, result)
|
||||
@@ -228,8 +218,26 @@ func (s *PostgresSuite) TestAttachmentJobLifecycleAndOutboxFencing() {
|
||||
)
|
||||
s.Require().NoError(err)
|
||||
s.Equal(model.AttachmentReady, metadata.Status)
|
||||
s.Len(metadata.Variants, 1)
|
||||
s.Len(metadata.Variants, 3)
|
||||
s.Equal(variants[1].ObjectKey, variant.ObjectKey)
|
||||
listedKeys := make(map[string]struct{})
|
||||
var after string
|
||||
for {
|
||||
listed, next, err := s.store.ListAttachments(context.Background(), after, 1000)
|
||||
s.Require().NoError(err)
|
||||
for _, candidate := range listed {
|
||||
if candidate.ID == attachment.ID {
|
||||
listedKeys[candidate.ObjectKey] = struct{}{}
|
||||
}
|
||||
}
|
||||
if next == "" {
|
||||
break
|
||||
}
|
||||
after = next
|
||||
}
|
||||
for _, candidate := range variants {
|
||||
s.Contains(listedKeys, candidate.ObjectKey)
|
||||
}
|
||||
|
||||
v2 := model.AttachmentJob{ID: uuid.NewString(), PipelineVersion: 2}
|
||||
queuedV2, err := s.store.QueueAttachmentPipelineVersion(
|
||||
@@ -243,6 +251,46 @@ func (s *PostgresSuite) TestAttachmentJobLifecycleAndOutboxFencing() {
|
||||
s.Equal(model.JobMutationStale, result)
|
||||
}
|
||||
|
||||
func (s *PostgresSuite) TestPublishedOutboxRetentionIsBatched() {
|
||||
owner, chatID := createOwnerAndChat(s.T(), s.store)
|
||||
_, firstJob := createQueuedAttachment(s.T(), s.store, owner.ID, chatID)
|
||||
_, secondJob := createQueuedAttachment(s.T(), s.store, owner.ID, chatID)
|
||||
events, err := s.store.ClaimOutboxEvents(context.Background(), 1000, time.Minute)
|
||||
s.Require().NoError(err)
|
||||
|
||||
eventIDs := make([]string, 0, 2)
|
||||
for _, event := range events {
|
||||
if event.JobID != firstJob.ID && event.JobID != secondJob.ID {
|
||||
continue
|
||||
}
|
||||
published, err := s.store.MarkOutboxPublished(context.Background(), event.ID, event.LeaseToken)
|
||||
s.Require().NoError(err)
|
||||
s.True(published)
|
||||
eventIDs = append(eventIDs, event.ID)
|
||||
}
|
||||
s.Require().Len(eventIDs, 2)
|
||||
_, err = s.pool.Exec(
|
||||
context.Background(),
|
||||
`UPDATE outbox_events SET published_at = clock_timestamp() - INTERVAL '2 days' WHERE id IN ($1, $2)`,
|
||||
eventIDs[0], eventIDs[1],
|
||||
)
|
||||
s.Require().NoError(err)
|
||||
|
||||
deleted, err := s.store.DeletePublishedOutboxEvents(context.Background(), time.Now().Add(-24*time.Hour), 1)
|
||||
s.Require().NoError(err)
|
||||
s.Equal(1, deleted)
|
||||
deleted, err = s.store.DeletePublishedOutboxEvents(context.Background(), time.Now().Add(-24*time.Hour), 1)
|
||||
s.Require().NoError(err)
|
||||
s.Equal(1, deleted)
|
||||
|
||||
var remaining int
|
||||
err = s.pool.QueryRow(
|
||||
context.Background(), `SELECT count(*) FROM outbox_events WHERE id IN ($1, $2)`, eventIDs[0], eventIDs[1],
|
||||
).Scan(&remaining)
|
||||
s.Require().NoError(err)
|
||||
s.Zero(remaining)
|
||||
}
|
||||
|
||||
func (s *PostgresSuite) TestFailedAttachmentReplayIsAudited() {
|
||||
owner, chatID := createOwnerAndChat(s.T(), s.store)
|
||||
attachment, job := createQueuedAttachment(s.T(), s.store, owner.ID, chatID)
|
||||
@@ -303,22 +351,34 @@ func (s *PostgresSuite) TestExpiredLeaseRecoveryFencesOldWorker() {
|
||||
s.Equal(2, current.Attempt)
|
||||
s.Equal(model.AttachmentJobQueued, current.Status)
|
||||
|
||||
result, err := s.store.CompleteAttachmentJob(context.Background(), lease, []model.Variant{
|
||||
{
|
||||
Name: "original", ContentType: "image/png", SizeBytes: 128,
|
||||
SHA256: strings.Repeat("a", 64), Width: 320, Height: 200,
|
||||
ObjectKey: "attachments/" + attachment.ID + "/v1/original.png",
|
||||
},
|
||||
{
|
||||
Name: "thumbnail", ContentType: "image/jpeg", SizeBytes: 64,
|
||||
SHA256: strings.Repeat("b", 64), Width: 256, Height: 160,
|
||||
ObjectKey: "attachments/" + attachment.ID + "/v1/thumbnail.jpg",
|
||||
},
|
||||
})
|
||||
result, err := s.store.CompleteAttachmentJob(context.Background(), lease, completedVariants(attachment.ID, 1))
|
||||
s.Require().NoError(err)
|
||||
s.Equal(model.JobMutationStale, result)
|
||||
}
|
||||
|
||||
func (s *PostgresSuite) TestCompleteAttachmentJobRejectsIncompleteOrMismatchedVariants() {
|
||||
owner, chatID := createOwnerAndChat(s.T(), s.store)
|
||||
attachment, job := createQueuedAttachment(s.T(), s.store, owner.ID, chatID)
|
||||
lease, err := s.store.ClaimAttachmentJob(
|
||||
context.Background(), job.ID, attachment.ID, job.PipelineVersion, job.Attempt, "worker-1", time.Minute,
|
||||
)
|
||||
s.Require().NoError(err)
|
||||
|
||||
complete := completedVariants(attachment.ID, job.PipelineVersion)
|
||||
_, err = s.store.CompleteAttachmentJob(context.Background(), lease, complete[:3])
|
||||
s.ErrorIs(err, model.ErrInvalidInput)
|
||||
|
||||
duplicate := append([]model.Variant(nil), complete...)
|
||||
duplicate[3].Name = duplicate[2].Name
|
||||
_, err = s.store.CompleteAttachmentJob(context.Background(), lease, duplicate)
|
||||
s.ErrorIs(err, model.ErrInvalidInput)
|
||||
|
||||
mismatched := append([]model.Variant(nil), complete...)
|
||||
mismatched[3].PipelineVersion++
|
||||
_, err = s.store.CompleteAttachmentJob(context.Background(), lease, mismatched)
|
||||
s.ErrorIs(err, model.ErrInvalidInput)
|
||||
}
|
||||
|
||||
func (s *PostgresSuite) TestAttachmentDeletionClaimChecksLifecycleUnderLock() {
|
||||
owner, chatID := createOwnerAndChat(s.T(), s.store)
|
||||
pending, err := s.store.CreatePendingAttachment(context.Background(), model.Attachment{
|
||||
@@ -533,6 +593,29 @@ func createQueuedAttachment(
|
||||
return queued, job
|
||||
}
|
||||
|
||||
func completedVariants(attachmentID string, pipelineVersion int) []model.Variant {
|
||||
types := []struct {
|
||||
name string
|
||||
contentType string
|
||||
extension string
|
||||
}{
|
||||
{name: "original", contentType: "image/png", extension: "png"},
|
||||
{name: "thumbnail", contentType: "image/jpeg", extension: "jpg"},
|
||||
{name: "crop", contentType: "image/jpeg", extension: "jpg"},
|
||||
{name: "optimized", contentType: "image/webp", extension: "webp"},
|
||||
}
|
||||
variants := make([]model.Variant, 0, len(types))
|
||||
for index, variant := range types {
|
||||
variants = append(variants, model.Variant{
|
||||
Name: variant.name, ContentType: variant.contentType, SizeBytes: int64(128 - index*16),
|
||||
SHA256: strings.Repeat(string(rune('a'+index)), 64), Width: 320 - index*32, Height: 200 - index*20,
|
||||
ObjectKey: fmt.Sprintf("attachments/%s/v%d/%s.%s", attachmentID, pipelineVersion, variant.name, variant.extension),
|
||||
PipelineVersion: pipelineVersion,
|
||||
})
|
||||
}
|
||||
return variants
|
||||
}
|
||||
|
||||
func messageCount(t *testing.T, pool *pgxpool.Pool, chatID string) int {
|
||||
t.Helper()
|
||||
var count int
|
||||
|
||||
@@ -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