forked from mtusi2026/mtusi2026lab5
feat(lab5): durable async attachment pipeline through grade 4
- RabbitMQ quorum queue, persistent messages and publisher confirms - S3 staging plus PostgreSQL attachments/jobs outbox transaction - worker variants (thumbnail, crop, optimized WebP), DB lease and lease sweeper - jittered retry, permanent failures, explicit replay and admission cap - deterministic crash hooks, resilience script and evidence for grade 4 - async-aware custom tests, oversized pixel case, docs and changelog
This commit is contained in:
@@ -1,12 +1,11 @@
|
||||
// Package reconcile repairs the two failure windows between PostgreSQL
|
||||
// metadata and S3 bytes:
|
||||
// Package reconcile repairs the two storage/metadata gaps that a crash can
|
||||
// leave behind:
|
||||
//
|
||||
// - a pending metadata row whose upload never finished or whose 201 was
|
||||
// never returned;
|
||||
// - an S3 object whose metadata transaction never committed.
|
||||
// - a staging object whose metadata row never committed;
|
||||
// - a terminal attachment whose staging copy was not deleted yet.
|
||||
//
|
||||
// Both repairs use a grace period so an upload that is still in flight is
|
||||
// never touched.
|
||||
// It never touches the final original or a variant while PostgreSQL still owns
|
||||
// it. The grace period protects in-flight work.
|
||||
package reconcile
|
||||
|
||||
import (
|
||||
@@ -19,7 +18,7 @@ import (
|
||||
"gitflic.ru/project/bulla-max/mtusi2026lab/internal/store"
|
||||
)
|
||||
|
||||
const objectPrefix = "attachments/"
|
||||
var prefixes = []string{"attachments/", "staging/"}
|
||||
|
||||
type Reconciler struct {
|
||||
store *store.Store
|
||||
@@ -29,9 +28,9 @@ type Reconciler struct {
|
||||
}
|
||||
|
||||
type Stats struct {
|
||||
StalePending int `json:"stale_pending"`
|
||||
StaleStaging int `json:"stale_staging"`
|
||||
OrphanObjects int `json:"orphan_objects"`
|
||||
DeletedRows int `json:"deleted_rows"`
|
||||
DeletedStaging int `json:"deleted_staging"`
|
||||
DeletedObjects int `json:"deleted_objects"`
|
||||
Skipped int `json:"skipped"`
|
||||
}
|
||||
@@ -47,68 +46,64 @@ func (r *Reconciler) RunOnce(ctx context.Context, dryRun bool) (Stats, error) {
|
||||
var stats Stats
|
||||
cutoff := time.Now().UTC().Add(-r.grace)
|
||||
|
||||
// Phase 1: rows that were committed as pending but never reached ready.
|
||||
stale, err := r.store.ListStalePendingAttachments(ctx, cutoff, 500)
|
||||
// Phase 1: ready/failed rows eventually lose their staging copy. The
|
||||
// worker usually deletes it immediately after ready commits; this covers a
|
||||
// crash or a transient S3 error.
|
||||
stale, err := r.store.ListStaleStagingAttachments(ctx, cutoff, 500)
|
||||
if err != nil {
|
||||
return stats, err
|
||||
}
|
||||
for _, attachment := range stale {
|
||||
stats.StalePending++
|
||||
stats.StaleStaging++
|
||||
if dryRun {
|
||||
r.logger.Info("reconcile: would remove stale pending attachment",
|
||||
"attachment_id", attachment.ID, "object_key", attachment.ObjectKey,
|
||||
"created_at", attachment.CreatedAt)
|
||||
r.logger.Info("reconcile: would remove stale staging",
|
||||
"attachment_id", attachment.ID, "staging_key", attachment.StagingKey)
|
||||
continue
|
||||
}
|
||||
|
||||
if err = r.objects.Delete(ctx, attachment.ObjectKey); err != nil && !errors.Is(err, objectstore.ErrNotFound) {
|
||||
// Keep the row for the next pass. Deleting it now would lose the
|
||||
// only pointer to an object that might still exist.
|
||||
r.logger.Error("reconcile: delete stale object",
|
||||
if err = r.objects.Delete(ctx, attachment.StagingKey); err != nil && !errors.Is(err, objectstore.ErrNotFound) {
|
||||
r.logger.Error("reconcile: delete staging",
|
||||
"err", err, "attachment_id", attachment.ID)
|
||||
stats.Skipped++
|
||||
continue
|
||||
}
|
||||
if err = r.store.DeletePendingAttachment(ctx, attachment.ID); err != nil {
|
||||
r.logger.Error("reconcile: delete stale row", "err", err, "attachment_id", attachment.ID)
|
||||
if err = r.store.ClearStagingKey(ctx, attachment.ID); err != nil {
|
||||
r.logger.Error("reconcile: clear staging key", "err", err, "attachment_id", attachment.ID)
|
||||
stats.Skipped++
|
||||
continue
|
||||
}
|
||||
stats.DeletedRows++
|
||||
stats.DeletedObjects++
|
||||
stats.DeletedStaging++
|
||||
}
|
||||
|
||||
// Phase 2: objects with no metadata row at all. The pending row is written
|
||||
// before the PUT in the normal path, so this only happens after a crash in
|
||||
// a narrow window (or manual bucket tampering).
|
||||
listed, err := r.objects.List(ctx, objectPrefix)
|
||||
// Phase 2: anything under our prefixes with no database owner. The API
|
||||
// writes staging before the metadata commit, so this window is real.
|
||||
known, err := r.store.ListStorageKeys(ctx)
|
||||
if err != nil {
|
||||
return stats, err
|
||||
}
|
||||
known, err := r.store.ListAttachmentObjectKeys(ctx)
|
||||
if err != nil {
|
||||
return stats, err
|
||||
}
|
||||
for _, object := range listed {
|
||||
if _, ok := known[object.Key]; ok {
|
||||
continue
|
||||
for _, prefix := range prefixes {
|
||||
listed, listErr := r.objects.List(ctx, prefix)
|
||||
if listErr != nil {
|
||||
return stats, listErr
|
||||
}
|
||||
// A missing LastModified must not be treated as ancient: skipping is
|
||||
// safer than deleting an object that may have just been created.
|
||||
if object.LastModified.IsZero() || object.LastModified.After(cutoff) {
|
||||
continue
|
||||
for _, object := range listed {
|
||||
if _, ok := known[object.Key]; ok {
|
||||
continue
|
||||
}
|
||||
if object.LastModified.IsZero() || object.LastModified.After(cutoff) {
|
||||
continue
|
||||
}
|
||||
stats.OrphanObjects++
|
||||
if dryRun {
|
||||
r.logger.Info("reconcile: would delete orphan object", "object_key", object.Key)
|
||||
continue
|
||||
}
|
||||
if err = r.objects.Delete(ctx, object.Key); err != nil && !errors.Is(err, objectstore.ErrNotFound) {
|
||||
r.logger.Error("reconcile: delete orphan object", "err", err, "object_key", object.Key)
|
||||
stats.Skipped++
|
||||
continue
|
||||
}
|
||||
stats.DeletedObjects++
|
||||
}
|
||||
stats.OrphanObjects++
|
||||
if dryRun {
|
||||
r.logger.Info("reconcile: would delete orphan object", "object_key", object.Key)
|
||||
continue
|
||||
}
|
||||
if err = r.objects.Delete(ctx, object.Key); err != nil && !errors.Is(err, objectstore.ErrNotFound) {
|
||||
r.logger.Error("reconcile: delete orphan object", "err", err, "object_key", object.Key)
|
||||
stats.Skipped++
|
||||
continue
|
||||
}
|
||||
stats.DeletedObjects++
|
||||
}
|
||||
|
||||
return stats, nil
|
||||
|
||||
Reference in New Issue
Block a user