fix(controlplane): refresh a stale seeded resource and re-seed a removed one

A seed was consulted only for a resource kind the store did not hold, so a
document that changed in the build never reached a database an earlier
release had seeded. For workflow-definitions that is not a stale setting but
a total outage: the collection validates as one document, so a stored step
type this build no longer has fails every task's pin, whatever workflow the
task names. The seed's request ID was stable per kind and the store answers a
known request ID from resource_history, which outlives the resource, so the
documented recovery -- remove the resource and restart -- wrote nothing
either.

Seeds now carry one identity the reader can name, and a kind the store holds
is refreshed only when the seed itself wrote the newest revision: a
file-derived value stays the store's, and an operator's replacement is never
undone. The request ID is derived from the value, so an idempotent replay is
still one revision without making a second, changed document impossible. A
replay whose resource is gone re-creates it from the ledger's revision.

The shipped-vs-file-derived distinction is the seed's to make, so it is a
property of the definition (Defaults is set exactly for the kinds that ship a
document) rather than a check at each consumer.
This commit is contained in:
sam committed 2026-09-29 14:27:57 +10:00
1 parent 9bb3340dce
commit 64a4210b1f
10 files changed
+651 -41

No files matched your search

@@ -108,6 +108,43 @@ func (q *Queries) LockResourceWrite(ctx context.Context, hashtextextended string
return err
}
const reinsertResource = `-- name: ReinsertResource :one
INSERT INTO resources (kind, value, version, updated_at)
VALUES ($1, $2, $3, $4)
RETURNING kind, value, version, updated_at, org_id
`
type ReinsertResourceParams struct {
Kind string
Value []byte
Version int64
UpdatedAt time.Time
}
// Re-creates the current row for a revision resource_history already records.
// The ledger outlives the row it describes, so a resource an operator removed
// still has its revisions; putting one back needs the row re-created without a
// second ledger entry, which idx_resource_history_kind_request would refuse.
// The revision is the ledger's, not a fresh one: this is the row that write
// produced, restored.
func (q *Queries) ReinsertResource(ctx context.Context, arg ReinsertResourceParams) (Resource, error) {
row := q.db.QueryRow(ctx, reinsertResource,
arg.Kind,
arg.Value,
arg.Version,
arg.UpdatedAt,
)
var i Resource
err := row.Scan(
&i.Kind,
&i.Value,
&i.Version,
&i.UpdatedAt,
&i.OrgID,
)
return i, err
}
const resourceByKind = `-- name: ResourceByKind :one
SELECT kind, value, version, updated_at, org_id FROM resources WHERE kind = $1
`
@@ -13,6 +13,17 @@ SELECT kind, value, version, actor, source, request_id, expected_version,
FROM resource_history
WHERE kind = $1 AND request_id = $2;
-- name: ReinsertResource :one
-- Re-creates the current row for a revision resource_history already records.
-- The ledger outlives the row it describes, so a resource an operator removed
-- still has its revisions; putting one back needs the row re-created without a
-- second ledger entry, which idx_resource_history_kind_request would refuse.
-- The revision is the ledger's, not a fresh one: this is the row that write
-- produced, restored.
INSERT INTO resources (kind, value, version, updated_at)
VALUES ($1, $2, $3, $4)
RETURNING kind, value, version, updated_at, org_id;
-- name: ResourceHistory :many
SELECT kind, value, version, actor, source, request_id, expected_version,
current_version, at
+42 -4
View File
@@ -78,6 +78,17 @@ func (s *Resources) Audit(ctx context.Context, table string, keys []string, limi
return entries, nil
}
// PutResource applies one control-plane resource write.
//
// It is idempotent per request ID: a write whose (kind, request_id) is already
// in resource_history returns that revision instead of writing a second one, so a
// retry that reached the store twice is still one revision. The ledger is what
// carries the key -- resource_history outlives the row it describes -- so a
// replay of a revision whose resource an operator removed re-creates that row
// rather than answering with a revision the store no longer holds. The
// alternative is a write that reports a version for a kind that is absent: the
// caller cannot tell the difference, and a kind can stay gone while every
// process believes it holds a value.
func (s *Resources) PutResource(ctx context.Context, write storecontract.ResourceWrite) (_ storecontract.Resource, retErr error) {
tx, err := s.pool.Begin(ctx)
if err != nil {
@@ -91,10 +102,11 @@ func (s *Resources) PutResource(ctx context.Context, write storecontract.Resourc
if existing, err := queries.ResourceByRequest(ctx, postgresdb.ResourceByRequestParams{
Kind: write.Kind, RequestID: write.RequestID,
}); err == nil {
return resourceFromAudit(
existing.Kind, existing.Value, existing.Version, existing.Actor, existing.Source,
existing.RequestID, existing.ExpectedVersion, existing.CurrentVersion, existing.At,
), nil
resource, err := s.replayedResource(ctx, queries, tx, existing)
if err != nil {
return storecontract.Resource{}, err
}
return resource, nil
} else if !errors.Is(err, pgx.ErrNoRows) {
return storecontract.Resource{}, err
}
@@ -150,6 +162,32 @@ func (s *Resources) PutResource(ctx context.Context, write storecontract.Resourc
), nil
}
// replayedResource answers a write whose request ID resource_history already
// holds. The ledger's revision is the answer and a replay writes nothing, with
// one repair: when the resource that revision describes is no longer the current
// row, the row is re-created from it. That is the state an operator removing a
// resource leaves behind, and without the repair the store answers with a
// revision it does not hold, so the caller reads a version for a kind that is
// absent and the kind stays absent.
func (s *Resources) replayedResource(ctx context.Context, queries *postgresdb.Queries, tx pgx.Tx, existing postgresdb.ResourceByRequestRow) (storecontract.Resource, error) {
if _, err := queries.ResourceByKind(ctx, existing.Kind); errors.Is(err, pgx.ErrNoRows) {
if _, err := queries.ReinsertResource(ctx, postgresdb.ReinsertResourceParams{
Kind: existing.Kind, Value: existing.Value, Version: existing.Version, UpdatedAt: existing.At,
}); err != nil {
return storecontract.Resource{}, err
}
if err := tx.Commit(ctx); err != nil {
return storecontract.Resource{}, err
}
} else if err != nil {
return storecontract.Resource{}, err
}
return resourceFromAudit(
existing.Kind, existing.Value, existing.Version, existing.Actor, existing.Source,
existing.RequestID, existing.ExpectedVersion, existing.CurrentVersion, existing.At,
), nil
}
func resourceFromCurrent(resource postgresdb.Resource) storecontract.Resource {
return storecontract.Resource{
Kind: resource.Kind, Value: resource.Value, Version: resource.Version, At: resource.UpdatedAt,
@@ -267,6 +267,70 @@ func TestResourceWritesPreserveOptimisticAndIdempotentSemantics(t *testing.T) {
}
}
// A write is idempotent per request ID, and the ledger holding those IDs
// outlives the row it describes. So a replay whose resource is gone must
// re-create it: answering with the ledger's revision alone reports a version for
// a kind the store does not hold, and a caller cannot tell that apart from a
// resource that is there. An operator removing a resource and restarting the
// process that seeds it is the shape this repairs.
func TestResourceReplayRecreatesARemovedResource(t *testing.T) {
pool, _ := migrated(t)
resources := NewResources(pool)
at := time.Date(2026, 9, 27, 9, 0, 0, 0, time.UTC)
write := storecontract.ResourceWrite{
Kind: "workflow-definitions", Value: []byte(`{"definitions":[]}`), Actor: "system:migration",
Source: "legacy-config", RequestID: "import:workflow-definitions", ExpectedVersion: 0, At: at,
}
seeded, err := resources.PutResource(t.Context(), write)
if err != nil {
t.Fatalf("seed PutResource: %v", err)
}
if _, err := pool.Exec(t.Context(), `DELETE FROM resources WHERE kind = $1`, write.Kind); err != nil {
t.Fatalf("remove the resource: %v", err)
}
if _, err := resources.Resource(t.Context(), write.Kind); !errors.Is(err, storecontract.ErrResourceNotFound) {
t.Fatalf("resource after the removal = %v, want ErrResourceNotFound", err)
}
replayed, err := resources.PutResource(t.Context(), write)
if err != nil {
t.Fatalf("replay PutResource: %v", err)
}
if replayed.Version != seeded.Version || string(replayed.Value) != string(seeded.Value) {
t.Fatalf("replay = %+v, want the revision the ledger records, %+v", replayed, seeded)
}
live, err := resources.Resource(t.Context(), write.Kind)
if err != nil {
t.Fatalf("the replay did not re-create the resource: %v", err)
}
if live.Version != seeded.Version || string(live.Value) != string(seeded.Value) {
t.Fatalf("live resource after the replay = %+v, want the restored %+v", live, seeded)
}
// The repair re-creates the row the write produced; it does not append a
// second ledger entry, which idx_resource_history_kind_request would refuse.
history, err := resources.ResourceHistory(t.Context(), write.Kind, 0)
if err != nil {
t.Fatalf("ResourceHistory: %v", err)
}
if len(history) != 1 {
t.Fatalf("history after the replay = %d revisions, want the one the write made", len(history))
}
// A resource the store still holds is answered from the ledger, unchanged:
// the idempotent replay the index exists for.
again, err := resources.PutResource(t.Context(), storecontract.ResourceWrite{
Kind: write.Kind, Value: []byte(`{"definitions":[{"id":"different"}]}`), Actor: "other",
Source: "retry", RequestID: write.RequestID, ExpectedVersion: 0, At: at.Add(time.Minute),
})
if err != nil {
t.Fatalf("second replay: %v", err)
}
if again.Version != seeded.Version || string(again.Value) != string(seeded.Value) {
t.Fatalf("second replay = %+v, want the original %+v", again, seeded)
}
}
// A request ID is idempotent per kind: the store API, the resource_history
// UNIQUE(kind, request_id) index and every production caller treat it as one,
// so a caller that mints one request ID for two different kinds must get two