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:
Mikhail Verkovykh
2026-11-15 00:30:36 +03:00
parent 28d68dc30b
commit 9e43faa536
48 changed files with 4077 additions and 333 deletions
+161 -81
View File
@@ -3,6 +3,7 @@ package store
import (
"context"
"encoding/hex"
"encoding/json"
"errors"
"time"
@@ -12,10 +13,11 @@ import (
"github.com/jackc/pgx/v5"
)
// CreatePendingAttachment records the intent to store an object before the S3
// PUT happens. Committing this row first is what makes the reconciler able to
// distinguish "never uploaded" from "uploaded but process died".
func (s *Store) CreatePendingAttachment(ctx context.Context, att model.Attachment) error {
// CreateQueuedAttachment is the durability boundary of the upload endpoint.
// The attachment snapshot and the outbox job commit together, so there is no
// observable state in which the API promised work but PostgreSQL lost the
// intent. Publication to RabbitMQ happens later and may be retried safely.
func (s *Store) CreateQueuedAttachment(ctx context.Context, att model.Attachment, job model.JobRecord) error {
ctx, cancel := s.withTimeout(ctx)
defer cancel()
@@ -23,58 +25,74 @@ func (s *Store) CreatePendingAttachment(ctx context.Context, att model.Attachmen
if err != nil {
return common.BadRequest("invalid sha256")
}
if att.PipelineVersion < 1 {
att.PipelineVersion = 1
}
variants, err := json.Marshal([]model.Variant{})
if err != nil {
return common.Internal()
}
_, err = s.pool.Exec(ctx, `
tx, err := s.pool.Begin(ctx)
if err != nil {
return dbError(err)
}
defer rollback(tx)
_, err = tx.Exec(ctx, `
INSERT INTO attachments
(id, chat_id, owner_id, filename, content_type, size_bytes, sha256,
object_key, status, error_code, created_at, updated_at)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, 'pending', NULL, $9, $9)`,
object_key, staging_key, pipeline_version, variants, status, error_code,
created_at, updated_at)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, 'queued', NULL, $12, $12)`,
att.ID, att.ChatID, att.OwnerID, att.Filename, att.ContentType, att.SizeBytes,
hash, att.ObjectKey, time.Time(att.CreatedAt))
return dbError(err)
}
// MarkAttachmentReady publishes metadata only after the object bytes are known
// to be in S3. A 201 is therefore a promise about both stores.
func (s *Store) MarkAttachmentReady(ctx context.Context, id uuid.UUID) (time.Time, error) {
ctx, cancel := s.withTimeout(ctx)
defer cancel()
updatedAt := time.Now().UTC()
tag, err := s.pool.Exec(ctx, `
UPDATE attachments
SET status = 'ready', updated_at = $2
WHERE id = $1 AND status = 'pending'`, id, updatedAt)
hash, att.ObjectKey, att.StagingKey, att.PipelineVersion, variants, time.Time(att.CreatedAt))
if err != nil {
return time.Time{}, dbError(err)
return dbError(err)
}
if tag.RowsAffected() == 0 {
return time.Time{}, common.Conflict("attachment is not pending")
_, err = tx.Exec(ctx, `
INSERT INTO attachment_jobs
(job_id, attachment_id, type, pipeline_version, attempt, max_attempts,
status, error_code, available_at, published_at, lease_until,
processing_started_at, finished_at, replay_count, created_at, updated_at)
VALUES ($1, $2, $3, $4, 0, $5, 'queued', NULL, $6, NULL, NULL,
NULL, NULL, 0, $6, $6)`,
job.JobID, att.ID, model.JobTypeAttachmentProcessV1, att.PipelineVersion,
job.MaxAttempts, time.Time(att.CreatedAt))
if err != nil {
return dbError(err)
}
return updatedAt, nil
if err = tx.Commit(ctx); err != nil {
return dbError(err)
}
return nil
}
// DeletePendingAttachment removes a row only while it is still pending. The
// status guard is the safety property: if an upload completed concurrently, the
// compensation or reconciler pass must not delete a ready attachment.
func (s *Store) DeletePendingAttachment(ctx context.Context, id uuid.UUID) error {
// CountPendingJobs is an admission control signal, not a queue size proof.
// Two API replicas may observe slightly stale counts, but the durable cap is
// still useful: it stops an overload from growing staging and PostgreSQL
// without limit. Rejections are 503 because the client may retry later.
func (s *Store) CountPendingJobs(ctx context.Context) (int, error) {
ctx, cancel := s.withTimeout(ctx)
defer cancel()
_, err := s.pool.Exec(ctx,
`DELETE FROM attachments WHERE id = $1 AND status = 'pending'`, id)
return dbError(err)
var count int
err := s.pool.QueryRow(ctx, `
SELECT count(*) FROM attachment_jobs
WHERE status IN ('queued', 'processing')`).Scan(&count)
if err != nil {
return 0, dbError(err)
}
return count, nil
}
func (s *Store) GetAttachmentForUser(ctx context.Context, userID, attachmentID uuid.UUID) (model.Attachment, error) {
ctx, cancel := s.withTimeout(ctx)
defer cancel()
att, err := scanAttachment(s.pool.QueryRow(ctx, `
SELECT a.id, a.chat_id, a.owner_id, a.filename, a.content_type,
a.size_bytes, a.sha256, a.status, a.error_code,
a.created_at, a.updated_at, a.object_key
FROM attachments a
att, err := scanAttachment(s.pool.QueryRow(ctx, attachmentSelect+`
JOIN chat_members cm ON cm.chat_id = a.chat_id AND cm.user_id = $2
WHERE a.id = $1`, attachmentID, userID))
if err != nil {
@@ -83,20 +101,76 @@ func (s *Store) GetAttachmentForUser(ctx context.Context, userID, attachmentID u
return att, nil
}
// ListStalePendingAttachments is the entry point for crash recovery. cutoff is
// an instant, not a duration: rows created before it are eligible. The caller
// computes it as now - grace so the boundary can be tuned and tested.
func (s *Store) ListStalePendingAttachments(ctx context.Context, cutoff time.Time, limit int) ([]model.Attachment, error) {
// GetAttachment is the worker's trusted read. The worker never accepts bucket
// keys or filenames from the broker message.
func (s *Store) GetAttachment(ctx context.Context, attachmentID uuid.UUID) (model.Attachment, error) {
ctx, cancel := s.withTimeout(ctx)
defer cancel()
rows, err := s.pool.Query(ctx, `
SELECT a.id, a.chat_id, a.owner_id, a.filename, a.content_type,
a.size_bytes, a.sha256, a.status, a.error_code,
a.created_at, a.updated_at, a.object_key
FROM attachments a
WHERE a.status = 'pending' AND a.created_at < $1
ORDER BY a.created_at
att, err := scanAttachment(s.pool.QueryRow(ctx, attachmentSelect+` WHERE a.id = $1`, attachmentID))
if err != nil {
return model.Attachment{}, err
}
return att, nil
}
const attachmentSelect = `
SELECT a.id, a.chat_id, a.owner_id, a.filename, a.content_type,
a.size_bytes, a.sha256, a.status, a.error_code,
a.created_at, a.updated_at, a.object_key, COALESCE(a.staging_key, ''),
a.pipeline_version, a.variants
FROM attachments a`
func scanAttachment(row rowScanner) (model.Attachment, error) {
var (
att model.Attachment
hash []byte
errCode *string
createdAt time.Time
updatedAt time.Time
variants []byte
)
if err := row.Scan(
&att.ID, &att.ChatID, &att.OwnerID, &att.Filename, &att.ContentType,
&att.SizeBytes, &hash, &att.Status, &errCode,
&createdAt, &updatedAt, &att.ObjectKey, &att.StagingKey,
&att.PipelineVersion, &variants,
); err != nil {
if errors.Is(err, pgx.ErrNoRows) {
return model.Attachment{}, common.NotFound("attachment not found")
}
return model.Attachment{}, dbError(err)
}
att.SHA256 = hex.EncodeToString(hash)
att.ErrorCode = errCode
att.CreatedAt = common.RFCFormTime(createdAt.UTC())
att.UpdatedAt = common.RFCFormTime(updatedAt.UTC())
att.Variants = []model.Variant{}
if len(variants) > 0 {
if err := json.Unmarshal(variants, &att.Variants); err != nil {
return model.Attachment{}, dbError(err)
}
}
if att.Variants == nil {
att.Variants = []model.Variant{}
}
return att, nil
}
// ListStaleStagingAttachments returns terminal attachments whose staging copy
// may be removed. The grace period is applied to updated_at so a just-finished
// job is never cleaned while its commit is still settling.
func (s *Store) ListStaleStagingAttachments(ctx context.Context, cutoff time.Time, limit int) ([]model.Attachment, error) {
ctx, cancel := s.withTimeout(ctx)
defer cancel()
rows, err := s.pool.Query(ctx, attachmentSelect+`
WHERE a.status IN ('ready', 'failed')
AND a.updated_at < $1
AND a.staging_key IS NOT NULL
ORDER BY a.updated_at
LIMIT $2`, cutoff, limit)
if err != nil {
return nil, dbError(err)
@@ -107,7 +181,7 @@ func (s *Store) ListStalePendingAttachments(ctx context.Context, cutoff time.Tim
for rows.Next() {
att, scanErr := scanAttachment(rows)
if scanErr != nil {
return nil, dbError(scanErr)
return nil, scanErr
}
result = append(result, att)
}
@@ -117,11 +191,24 @@ func (s *Store) ListStalePendingAttachments(ctx context.Context, cutoff time.Tim
return result, nil
}
func (s *Store) ListAttachmentObjectKeys(ctx context.Context) (map[string]struct{}, error) {
func (s *Store) ClearStagingKey(ctx context.Context, attachmentID uuid.UUID) error {
ctx, cancel := s.withTimeout(ctx)
defer cancel()
rows, err := s.pool.Query(ctx, `SELECT object_key FROM attachments`)
_, err := s.pool.Exec(ctx,
`UPDATE attachments SET staging_key = NULL WHERE id = $1`, attachmentID)
return dbError(err)
}
// ListStorageKeys returns every object the database still owns. It includes
// variant keys derived from the original key; the reconciler must never delete
// a thumbnail that has no separate row.
func (s *Store) ListStorageKeys(ctx context.Context) (map[string]struct{}, error) {
ctx, cancel := s.withTimeout(ctx)
defer cancel()
rows, err := s.pool.Query(ctx,
`SELECT object_key, COALESCE(staging_key, ''), variants FROM attachments`)
if err != nil {
return nil, dbError(err)
}
@@ -129,11 +216,25 @@ func (s *Store) ListAttachmentObjectKeys(ctx context.Context) (map[string]struct
keys := make(map[string]struct{})
for rows.Next() {
var key string
if err = rows.Scan(&key); err != nil {
var (
objectKey string
staging string
variants []byte
)
if err = rows.Scan(&objectKey, &staging, &variants); err != nil {
return nil, dbError(err)
}
keys[key] = struct{}{}
keys[objectKey] = struct{}{}
if staging != "" {
keys[staging] = struct{}{}
}
var list []model.Variant
if err = json.Unmarshal(variants, &list); err != nil {
return nil, dbError(err)
}
for _, variant := range list {
keys[VariantObjectKey(objectKey, variant.Name)] = struct{}{}
}
}
if err = rows.Err(); err != nil {
return nil, dbError(err)
@@ -141,30 +242,9 @@ func (s *Store) ListAttachmentObjectKeys(ctx context.Context) (map[string]struct
return keys, nil
}
func scanAttachment(row rowScanner) (model.Attachment, error) {
var (
att model.Attachment
hash []byte
errCode *string
createdAt time.Time
updatedAt time.Time
)
if err := row.Scan(
&att.ID, &att.ChatID, &att.OwnerID, &att.Filename, &att.ContentType,
&att.SizeBytes, &hash, &att.Status, &errCode,
&createdAt, &updatedAt, &att.ObjectKey,
); err != nil {
if errors.Is(err, pgx.ErrNoRows) {
return model.Attachment{}, common.NotFound("attachment not found")
}
return model.Attachment{}, dbError(err)
}
att.SHA256 = hex.EncodeToString(hash)
att.ErrorCode = errCode
att.Variants = []model.Variant{}
att.CreatedAt = common.RFCFormTime(createdAt.UTC())
att.UpdatedAt = common.RFCFormTime(updatedAt.UTC())
return att, nil
// VariantObjectKey is the one place that names variant objects. Keeping it
// deterministic makes a retried job overwrite the same keys instead of
// creating a second variant set.
func VariantObjectKey(originalKey, variant string) string {
return originalKey + "/" + variant
}