diff --git a/internal/infrastructure/postgres/events_order_test.go b/internal/infrastructure/postgres/events_order_test.go new file mode 100644 index 000000000..054c255c1 --- /dev/null +++ b/internal/infrastructure/postgres/events_order_test.go @@ -0,0 +1,69 @@ +package postgres + +import ( + "testing" + "time" + + "github.com/samcharles93/archie-core/internal/events" + "github.com/samcharles93/archie-core/internal/infrastructure/postgres/postgresdb" +) + +// TestEventInsertsCommitInIDOrder pins the event-append lock: an insert +// waits while another transaction holds an uncommitted event, so a reader +// paging by (at, id) never sees a later id commit before an earlier one. +func TestEventInsertsCommitInIDOrder(t *testing.T) { + pool, _ := migrated(t) + st := New(pool) + + tx, err := pool.Begin(t.Context()) + if err != nil { + t.Fatalf("Begin: %v", err) + } + defer func() { _ = tx.Rollback(t.Context()) }() + firstID, err := insertEventQ(t.Context(), postgresdb.New(tx), events.Event{Kind: "first"}) + if err != nil { + t.Fatalf("insert in tx: %v", err) + } + + type result struct { + id int64 + err error + } + done := make(chan result, 1) + go func() { + id, err := st.InsertEvent(t.Context(), events.Event{Kind: "second"}) + done <- result{id, err} + }() + + deadline := time.Now().Add(10 * time.Second) + for { + var waiting int + if err := pool.QueryRow(t.Context(), + "SELECT count(*) FROM pg_locks WHERE locktype = 'advisory' AND NOT granted").Scan(&waiting); err != nil { + t.Fatalf("pg_locks: %v", err) + } + if waiting > 0 { + break + } + select { + case r := <-done: + t.Fatalf("second insert returned (id %d, err %v) while the first was uncommitted; want it to wait", r.id, r.err) + default: + } + if time.Now().After(deadline) { + t.Fatal("second insert never waited on the event-append lock") + } + time.Sleep(10 * time.Millisecond) + } + + if err := tx.Commit(t.Context()); err != nil { + t.Fatalf("Commit: %v", err) + } + r := <-done + if r.err != nil { + t.Fatalf("second insert: %v", r.err) + } + if r.id <= firstID { + t.Fatalf("second id %d, want greater than first id %d", r.id, firstID) + } +} diff --git a/internal/infrastructure/postgres/postgresdb/events.sql.go b/internal/infrastructure/postgres/postgresdb/events.sql.go index 4a3c3683c..7256fba79 100644 --- a/internal/infrastructure/postgres/postgresdb/events.sql.go +++ b/internal/infrastructure/postgres/postgresdb/events.sql.go @@ -12,8 +12,12 @@ import ( const insertEvent = `-- name: InsertEvent :one +WITH append_lock AS ( + SELECT pg_advisory_xact_lock(hashtextextended('archie.events.append', 0)) +) INSERT INTO events (at, kind, task_id, repo, issue, workflow, stage, attempt, actor_id, actor_kind, principal_id, detail, data) -VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13) +SELECT $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13 +FROM append_lock RETURNING id ` @@ -37,6 +41,10 @@ type InsertEventParams struct { // fixed-width sort key and id only breaks ties, so a reader pages by // "after (at, id)" and never skips or repeats a row. See // internal/domain/storecontract/cursor.go. +// InsertEvent takes the event-append lock before the row draws its id, and +// holds it until the enclosing transaction commits, so ids become visible in +// the order they were drawn. The lock is inside the statement so a standalone +// insert and one inside a longer transaction both take it. func (q *Queries) InsertEvent(ctx context.Context, arg InsertEventParams) (int64, error) { row := q.db.QueryRow(ctx, insertEvent, arg.At, diff --git a/internal/infrastructure/postgres/queries/events.sql b/internal/infrastructure/postgres/queries/events.sql index 85093e897..377d7b467 100644 --- a/internal/infrastructure/postgres/queries/events.sql +++ b/internal/infrastructure/postgres/queries/events.sql @@ -4,8 +4,16 @@ -- internal/domain/storecontract/cursor.go. -- name: InsertEvent :one +-- InsertEvent takes the event-append lock before the row draws its id, and +-- holds it until the enclosing transaction commits, so ids become visible in +-- the order they were drawn. The lock is inside the statement so a standalone +-- insert and one inside a longer transaction both take it. +WITH append_lock AS ( + SELECT pg_advisory_xact_lock(hashtextextended('archie.events.append', 0)) +) INSERT INTO events (at, kind, task_id, repo, issue, workflow, stage, attempt, actor_id, actor_kind, principal_id, detail, data) -VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13) +SELECT $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13 +FROM append_lock RETURNING id; -- name: ListEventsFromBeginning :many