From bdc072aab7dda6e5c3dfbe2b59a0edc19c44f1ea Mon Sep 17 00:00:00 2001 From: kudesn1k Date: Fri, 20 Nov 2026 10:54:34 +0300 Subject: [PATCH] =?UTF-8?q?=D0=A0=D0=B0=D0=B7=D1=80=D0=B5=D1=88=D0=B8?= =?UTF-8?q?=D1=82=D0=B5=20=D0=BE=D1=82=D0=BF=D1=80=D0=B0=D0=B2=D0=BA=D1=83?= =?UTF-8?q?=20=D1=81=D0=BE=D0=BE=D0=B1=D1=89=D0=B5=D0=BD=D0=B8=D1=8F=20?= =?UTF-8?q?=D0=B2=D0=BE=20=D0=B2=D1=80=D0=B5=D0=BC=D1=8F=20=D0=BF=D1=83?= =?UTF-8?q?=D0=B1=D0=BB=D0=B8=D0=BA=D0=B0=D1=86=D0=B8=D0=B8=20=D0=B2=D0=BB?= =?UTF-8?q?=D0=BE=D0=B6=D0=B5=D0=BD=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/chat/store/attachments_test.go | 78 +++++++++++++++++++++++++ internal/chat/store/postgres.go | 10 ++-- internal/pipeline/store.go | 11 ++-- 3 files changed, 90 insertions(+), 9 deletions(-) diff --git a/internal/chat/store/attachments_test.go b/internal/chat/store/attachments_test.go index 15614795..d9cdc265 100644 --- a/internal/chat/store/attachments_test.go +++ b/internal/chat/store/attachments_test.go @@ -16,6 +16,7 @@ import ( "messenger/internal/chat" "messenger/internal/chat/store" + "messenger/internal/pipeline" "messenger/internal/platform/apperr" "messenger/internal/platform/ident" ) @@ -79,6 +80,83 @@ func TestAppendOnceRejectsUnknownAttachmentWithoutClaim(t *testing.T) { } } +func TestAppendOnceAcceptsQueuedAttachmentDuringPublish(t *testing.T) { + f := newFixture(t) + alice := f.mustUser(t) + c := f.mustChat(t, alice) + id := f.attachment(t, c.ID, alice, "queued") + m := attachedMessage(t, c.ID, alice, id) + found, err := pipeline.NewStore(db.Pool).PublishOne(f.ctx, func(ctx context.Context, j pipeline.Job) error { + if j.AttachmentID != id { + t.Fatalf("publisher selected %s, want %s", j.AttachmentID, id) + } + // The callback holds the publisher transaction open until AppendOnce returns. + ctx, cancel := context.WithTimeout(ctx, 2*time.Second) + defer cancel() + _, err := f.messages.AppendOnce(ctx, m, "during-publish", keyTTL) + return err + }) + if err != nil || !found { + t.Fatalf("linking queued attachment while awaiting broker confirm: found=%v, err=%v", found, err) + } + assertCounts(t, 1, 1, 1) +} + +func TestAppendOnceSeesFailureCommittedWhileValidating(t *testing.T) { + f := newFixture(t) + alice := f.mustUser(t) + c := f.mustChat(t, alice) + id := f.attachment(t, c.ID, alice, "queued") + if _, err := db.Pool.Exec(f.ctx, `UPDATE attachment_jobs SET state='processing', + lease_token=gen_random_uuid(),lease_until=now()+interval '1 hour' WHERE attachment_id=$1`, id); err != nil { + t.Fatal(err) + } + tx, err := db.Pool.Begin(f.ctx) + if err != nil { + t.Fatal(err) + } + defer tx.Rollback(f.ctx) + // Fail and lease recovery both take this lock before deciding the terminal state. + if _, err := tx.Exec(f.ctx, `SELECT job_id FROM attachment_jobs WHERE attachment_id=$1 FOR UPDATE`, id); err != nil { + t.Fatal(err) + } + if _, err := tx.Exec(f.ctx, `UPDATE attachment_jobs SET state='failed',error_code='retry_exhausted', + lease_token=NULL,lease_until=NULL WHERE attachment_id=$1`, id); err != nil { + t.Fatal(err) + } + m := attachedMessage(t, c.ID, alice, id) + ctx, cancel := context.WithTimeout(f.ctx, 5*time.Second) + defer cancel() + done := make(chan error, 1) + go func() { + _, err := f.messages.AppendOnce(ctx, m, "failing", keyTTL) + done <- err + }() + for { + var waiting bool + err := db.Pool.QueryRow(ctx, `SELECT EXISTS ( + SELECT FROM pg_stat_activity WHERE $1 = ANY(pg_blocking_pids(pid)))`, tx.Conn().PgConn().PID()).Scan(&waiting) + if err != nil { + t.Fatal(err) + } + if waiting { + break + } + select { + case err := <-done: + t.Fatalf("message finished before the terminal transition: %v", err) + case <-time.After(10 * time.Millisecond): + } + } + if err := tx.Commit(ctx); err != nil { + t.Fatal(err) + } + if err := <-done; err == nil || apperr.KindOf(err) != apperr.KindConflict { + t.Fatalf("message with concurrently failed attachment: %v; want conflict", err) + } + assertCounts(t, 0, 0, 0) +} + func TestAppendOnceAttachmentValidationIsAtomic(t *testing.T) { for _, status := range []string{"foreign", "failed", "mixed"} { t.Run(status, func(t *testing.T) { diff --git a/internal/chat/store/postgres.go b/internal/chat/store/postgres.go index c62c704d..e8a15d09 100644 --- a/internal/chat/store/postgres.go +++ b/internal/chat/store/postgres.go @@ -18,6 +18,7 @@ import ( "github.com/jackc/pgx/v5" "github.com/jackc/pgx/v5/pgxpool" + "messenger/internal/attachment" "messenger/internal/chat" "messenger/internal/platform/apperr" "messenger/internal/platform/postgres" @@ -209,12 +210,13 @@ func validateAttachments(ctx context.Context, tx pgx.Tx, chatID uuid.UUID, ids [ return apperr.NotFound("attachment not found") } // A separate statement sees the latest committed transition after a lock wait. - // Every referenced attachment must have a current job. - rows, err = tx.Query(ctx, `SELECT status FROM attachment_states WHERE id=ANY($1::uuid[]) ORDER BY id FOR SHARE`, ids) + // Terminal failures take FOR UPDATE on the job or file. KEY SHARE orders + // validation with those transitions while allowing an outbox publish in flight. + rows, err = tx.Query(ctx, `SELECT status FROM attachment_states WHERE id=ANY($1::uuid[]) ORDER BY id FOR KEY SHARE`, ids) if err != nil { return err } - states, err := pgx.CollectRows(rows, pgx.RowTo[string]) + states, err := pgx.CollectRows(rows, pgx.RowTo[attachment.FileStatus]) if err != nil { return err } @@ -223,7 +225,7 @@ func validateAttachments(ctx context.Context, tx pgx.Tx, chatID uuid.UUID, ids [ } for _, state := range states { switch state { - case "queued", "processing", "ready": + case attachment.FileQueued, attachment.FileProcessing, attachment.FileReady: default: return apperr.Conflict("attachment processing failed") } diff --git a/internal/pipeline/store.go b/internal/pipeline/store.go index 398239c1..da9ad4eb 100644 --- a/internal/pipeline/store.go +++ b/internal/pipeline/store.go @@ -18,13 +18,14 @@ func NewStore(pool *pgxpool.Pool) *Store { return &Store{pool: pool} } // The callback waits for a routed publisher confirm with a finite timeout. // Losing that confirm or the SQL commit leaves the same envelope deliverable. +// NO KEY UPDATE excludes other publishers but allows messages to link a queued attachment. func (s *Store) PublishOne(ctx context.Context, publish func(context.Context, Job) error) (bool, error) { var found bool err := pgx.BeginFunc(ctx, s.pool, func(tx pgx.Tx) error { j := Job{SchemaVersion: 1, Type: "attachment.process.v1"} err := tx.QueryRow(ctx, `SELECT job_id,attachment_id,pipeline_version,attempt,created_at FROM attachment_jobs WHERE state='queued' AND published_at IS NULL AND available_at<=clock_timestamp() - ORDER BY available_at,job_id LIMIT 1 FOR UPDATE SKIP LOCKED`). + ORDER BY available_at,job_id LIMIT 1 FOR NO KEY UPDATE SKIP LOCKED`). Scan(&j.JobID, &j.AttachmentID, &j.PipelineVersion, &j.Attempt, &j.CreatedAt) if errors.Is(err, pgx.ErrNoRows) { return nil @@ -60,12 +61,12 @@ func (s *Store) Claim(ctx context.Context, j Job, lease time.Duration) (*Claim, RETURNING j.source_key,f.hash,f.size_bytes,f.mime_type`, j.JobID, j.AttachmentID, j.PipelineVersion, j.Attempt, j.CreatedAt, c.Token, lease.Microseconds()). Scan(&c.SourceKey, &c.Hash, &c.SizeBytes, &c.MIMEType) - if !errors.Is(err, pgx.ErrNoRows) { - if err != nil { - return nil, err - } + if err == nil { return &c, nil } + if !errors.Is(err, pgx.ErrNoRows) { + return nil, err + } var incomplete bool err = s.pool.QueryRow(ctx, `SELECT EXISTS ( SELECT 1 FROM attachment_jobs j JOIN attachments a ON a.id=j.attachment_id