Keep a FedWiki site's certificate answer equal to its status
#247. The certificate endpoint (/domains/ask) lets a name have a certificate when its placement is servable, and for a FedWiki site that flag was a second copy of the site row's status. An archive or a restore wrote it best effort after the status committed, a swap did not write it at all, and only the sync, which is off by default, corrected it. A restored site could be refused a certificate and an archived one granted one. FedWiki migration 00008 adds a trigger on fedwiki.sites that writes the flag of the site's own FedWiki placements in the same transaction as every insert, status update and delete: true for an active or read-only site, false for an archived one and when the row is deleted. The certificate lookup still reads only the stored flag, so it involves no provider, and the reference points from FedWiki's schema into the domains registry, as the integration rule requires. Placements of other providers, and reservations with no site row yet, keep their stored answers. The per-writer flag writes it replaces (archive, restore, the sync's correction) and the registry setter they used are removed. The sync and boot, which create a placement for an existing site, read that one site FOR SHARE just before the placement change, so a status change cannot commit between the read and the placement; a status writer takes the site row and then the placement, and no transaction takes them the other way. The trigger refuses a site write under REPEATABLE READ or SERIALIZABLE, since a writer that waited behind a creator's read must see the placement that creator committed; every site writer runs at READ COMMITTED, the sync's metadata write now in an explicit transaction. docs/database-locks.md and docs/models/domains-registry.md say the same. A deleted site's name now answers no before its placement is released, so a release that fails no longer leaves a purged site's name authorized. The migration changes no row: a wrong flag is corrected on the site's next write, and a deployment fixes existing flags by hand. Out of scope: a create that fails before inserting its site and whose compensation also fails leaves its reservation's answer until boot. Tests: TestSitePlacementTrigger_DB, TestSitePlacementMigration_DB, TestSitePlacementPrivileges_DB, TestSitePlacementIsolation_DB, TestPlacementCreationLocks_DB, TestRecordPlacementServability_DB, TestSyncPlacementUsesLockedStatus_DB, TestSyncMetadataPlacementIsolation_DB, TestBootPlacementUsesLockedStatus_DB and TestDeletedSitePlacementRefuses_DB. Full suite on reset databases: 5337 passed, 16 skipped, none failed. No page changed. Breaks, all CAUGHT (29; the last is a command record that breaks the migration's DELETE branch, after which the test databases are reset): - omit-insert - readonly-false - archived-true - active-false - omit-delete-delete_archived - omit-delete-delete_active - metadata-not-corrected - omit-provider-predicate - omit-resource-predicate - definer-rights - swallowed-placement-error - extra-updated-at-privilege - down-noop - no-isolation-guard - key-share - no-share - activation-keeps-archive-restore - activation-keeps-archive-swap_archived_target - sync-stale-archived_after_listing - boot-stale-archived_after_listing - sync-stale-restored_after_listing - boot-stale-restored_after_listing - readonly-creator-false - boot-late-read-deleted_hosted_after_listing - boot-late-read-deleted_foreign_after_listing - boot-failed-read-fallback - sync-failed-read-fallback - metadata-default-isolation - omit-delete-deleted-site-refuses
This commit is contained in:
+34
-9
@@ -17,10 +17,9 @@ are left out because they move.
|
||||
|
||||
Every advisory lock is transaction-scoped (`pg_advisory_xact_lock`), so
|
||||
Postgres releases it at commit or rollback. No code takes a session-level
|
||||
advisory lock, `LOCK TABLE`, or an explicit `FOR SHARE`, except that a plan
|
||||
act's listing batch takes an explicit `FOR KEY SHARE` on the pool rows it
|
||||
lists (see the pool row below) and that the trigger on a tier insert takes the
|
||||
product row `FOR SHARE` (see the product row below).
|
||||
advisory lock or `LOCK TABLE`. A plan act's listing batch takes `FOR KEY SHARE`
|
||||
on its pool rows; a tier insert locks its product `FOR SHARE`; FedWiki placement
|
||||
creators lock exactly one site `FOR SHARE` (see Registry below).
|
||||
|
||||
## The locks
|
||||
|
||||
@@ -39,6 +38,11 @@ product row `FOR SHARE` (see the product row below).
|
||||
|
||||
### Row locks
|
||||
|
||||
FedWiki placement creation takes its site's `FOR SHARE` after the registry
|
||||
advisory lock and skip checks. A site write takes matching placement row locks
|
||||
through its trigger after the site row, before usage and request rows. These
|
||||
flag-only writes change neither claims nor allocation counts.
|
||||
|
||||
| Lock | Row | Mode | Taken by | Serializes |
|
||||
|---|---|---|---|---|
|
||||
| Pool row | `core.resource_pools` row of one pool | `FOR UPDATE`; `FOR KEY SHARE` in a plan act's listing batch | the functions `core.confer`, `core.end_conferral`, `core.align_conferral_shape`, `core.update_conferral_bounds` and `core.settle_obligation`; `LockPools` (through the `LockResourcePool` query), called by `RevokeGrantTx`, `ManageGrantTx`, `ResumeReplacedGrantsTx`, `ResumeSubscription`, `settlePool`, `reconcileItems`, `reconcileSuspended` and `reconcileEnded` (the subscription reconcile's active, suspended and ended branches), `runPlanAct` (`AddTier`, `RemoveTier` and `EnactObligation`, a default change's move), and `runPlanAct` for a reorder's move (`EnactObligation`); `LockPoolOfGrant` (through the `LockGrantPool` query), called by `ExpireGrantTx` and `ExtendWaitingGrantTx`; and `core.write_plan_act_obligations`, which takes the pool rows of one listing batch `FOR KEY SHARE` | Everything that changes a pool's positions or entitlements: conferral, endings, a subscription's suspension and resume, grant acts, tier removal, a default change's move, rule-change settlement, grant resumption and a member's Resume plan. A listing batch's key-share lock conflicts with `FOR UPDATE`, so a batch and a move or a conferral on one of its pools wait for each other, and two batches of one act share the rows. |
|
||||
@@ -49,7 +53,8 @@ product row `FOR SHARE` (see the product row below).
|
||||
| Org type row | `core.org_types` row of one type | `FOR NO KEY UPDATE` | the `LockOrgType` query, run by the default change's T0 | The T0 against the demonstration seed and an edit of the type's other columns, which write the row without the rendezvous. Every other writer of the default already waits on the exclusive rendezvous. `FOR NO KEY UPDATE` does not conflict with the `FOR KEY SHARE` an organization insert takes on its type's row. |
|
||||
| Grant lineage head | the `core.grants` row no other grant extends | `FOR UPDATE OF g` | the `GetGrantLineageHeadForUpdate` query, run by `RevokeGrantTx`, `ExtendWaitingGrantTx` and `ManageGrantTx` | Acts on one lineage, so an extension that committed while another act waited is visible to it. |
|
||||
| Usage row | `core.numeric_entitlement_usage` row of one (pool, resource key) | `FOR UPDATE` | the `LockNumericUsage` query, run by `usage.Recount` and by the slot gate, after the site rows the transaction writes and before the request rows it finishes (the hold rows have no place in the order, see rule 5); the count is a statement of its own after the lock | Two recounts of one pool, so each count reads every change committed before its lock was granted; two slot gates for one pool, so the second counts the first's hold and two creates cannot both take the last slot. |
|
||||
| FedWiki site row | `fedwiki.sites` row of one domain or one site id | `FOR UPDATE` | the `LockSiteStatusByDomain` query, run by `sweepLocally`, `RecordSiteStatusActivity`, `RecordSwapActivity` and the sync's status write (`writeStatus`), and the `LockSiteStatusByID` query, run by the slot gate for an activation, and `LockArchivedSiteForPurge`, run by retention | Paths that move one site (a member's request, the downgrade sweep and the farm sync), so each writes the status it finds under the lock and the recount that follows counts the row as it ended; the sync's reading of the site's requests, which therefore reflects every request that committed before the lock was granted; the gate's reading of an activation whose row the sweep already made active; and retention's eligibility check through deletion. |
|
||||
| FedWiki site row | `fedwiki.sites` row of one domain or one site id | `FOR UPDATE`; `FOR SHARE` by `GetSiteForPlacement` | the `LockSiteStatusByDomain` query, run by `sweepLocally`, `RecordSiteStatusActivity`, `RecordSwapActivity` and the sync's status write (`writeStatus`), and the `LockSiteStatusByID` query, run by the slot gate for an activation, and `LockArchivedSiteForPurge`, run by retention; `GetSiteForPlacement`, run by `reconcileSite` and `syncPlacement` inside `Registry.WithLock` immediately before the first claim or placement write | Paths that move one site (a member's request, the downgrade sweep and the farm sync), so each writes the status it finds under the lock and the recount that follows counts the row as it ended; the sync's reading of the site's requests, which therefore reflects every request that committed before the lock was granted; the gate's reading of an activation whose row the sweep already made active; retention's eligibility check through deletion; and a status write or deletion against creation of that site's placement, so the writer's trigger sees the placement. |
|
||||
| FedWiki placement row | `domains.placements` rows with provider `fedwiki` and the site's id | Row lock of the trigger's UPDATE, only where the flag changes | `fedwiki.set_site_placement_servability`, after the site row and before usage | Nothing beyond the site row; flag-only, outside the registry lock. |
|
||||
| Desired-state record | `core.desired_state_records` row | `FOR UPDATE` | the `LockDesiredStateRecord` query (after the address lock); `StampDesiredStateRecordsDispatched`, which locks the records in identifier order | The record's content and generation within one recompute; the dispatch stamp against another stamp. |
|
||||
| Desired-state notice | `core.desired_state_outbox` rows | `FOR UPDATE SKIP LOCKED` | the `ClaimDesiredStateOutbox` query | Two relay passes, so each claims different rows; a pass skips rows another still holds. Triggers write the rows through `core.insert_desired_state_notice` inside the transaction that changed the source, and they take no lock beyond the insert. |
|
||||
| Resource-key row | `core.resource_keys` row of one key | the update lock of `SetResourceKeyProvider` | `integration.RegisterProviders` at boot, stamping keys in byte order | A stamp that changes a key's provider against an open entitlement insert of that key, which holds the row's `FOR KEY SHARE` through the foreign key. The materializer writes a pool's keys in byte order too, so the two wait in one direction. |
|
||||
@@ -480,10 +485,10 @@ lifecycle instance lock or a workspace lock.
|
||||
that inserts the row (`InsertSiteFromFarm`, `ON CONFLICT DO NOTHING`, so a
|
||||
row a create recorded after the pass read its rows is left alone) and then
|
||||
recounts the pool.
|
||||
- **A listed status equal to the row's**: one statement in autocommit
|
||||
- **A listed status equal to the row's**: one READ COMMITTED transaction
|
||||
(`SetSiteObservedByDomain`) that writes the observed storage and
|
||||
last-modified time. It holds the row's lock for that statement only, writes
|
||||
no status and takes no usage row.
|
||||
last-modified time. It holds the row lock through commit, writes no status
|
||||
and takes no usage row; the trigger also corrects a wrong placement flag.
|
||||
- **A listed status different from the row's** (`writeStatus`): one
|
||||
transaction at READ COMMITTED that takes the site row
|
||||
(`LockSiteStatusByDomain`), then reads whether a request on the site is
|
||||
@@ -533,6 +538,25 @@ either side of the delete.
|
||||
|
||||
### Registry
|
||||
|
||||
FedWiki flag-only corrections bypass the registry advisory lock: its site
|
||||
trigger locks the site row, then matching placement rows whose flag changes.
|
||||
All site row writes use READ COMMITTED (or PostgreSQL's equivalent READ
|
||||
UNCOMMITTED); the trigger rejects transaction-snapshot isolation. The swap
|
||||
locks both sites before either write. No path holds a placement lock and then
|
||||
takes a site lock. Member deletion releases its name in a preceding transaction;
|
||||
retention and farm-absent sync release it after the site deletion commits false.
|
||||
|
||||
Boot and sync creators classify the listed immutable domain and workspace and
|
||||
perform all skip checks first. Immediately before the first claim or placement
|
||||
mutation, they read exactly one site FOR SHARE, using GetSiteForPlacement in
|
||||
the registry transaction. A missing site creates neither claim nor placement;
|
||||
a failed read rolls back. Each creator transaction must lock exactly one site,
|
||||
otherwise it could deadlock with a multi-site swap. FOR SHARE conflicts with
|
||||
both explicit FOR UPDATE and bare status UPDATE; FOR KEY SHARE does not.
|
||||
An eligible creator waits for that site while holding the registry lock. The
|
||||
retention purge may hold it through its farm call for 30 seconds, delaying other
|
||||
registry operations; skipped sites do not incur this wait.
|
||||
|
||||
`domains.Registry.WithLock` opens a plain transaction and takes the registry
|
||||
lock before anything else. Every composite operation, including the FedWiki
|
||||
create, release and sync projections, runs on the `*Tx` it passes, so the lock
|
||||
@@ -565,7 +589,8 @@ transaction that allocates or releases the name.
|
||||
and a plan act's move lock one pool per transaction.
|
||||
4. The grant lineage head row is locked after the pool row, never before.
|
||||
5. A FedWiki transaction takes its locks in this order, skipping those it
|
||||
does not need: the lifecycle instance locks, the site rows, the usage row,
|
||||
does not need: the lifecycle instance locks, all site rows, their placement
|
||||
rows (through the site trigger), the usage row,
|
||||
then the lifecycle request rows. Hold rows have no place in the order,
|
||||
because only the holder of their request's instance lock writes them. The
|
||||
count under the usage row is a statement of its own after the lock, so it
|
||||
|
||||
@@ -12,7 +12,7 @@ The registry decides which DNS names this deployment is allowed to serve, and it
|
||||
|
||||
Claims come in three kinds. An **operator_root** claim is deployment infrastructure (the hosted apex, say `wiki.example`), owned by the system tenant and never releasable. A **member** claim is **carved** from inside an operator root — a member taking `alice.wiki.example` — subject to name policy. An **external** claim is a member bringing their own domain: it must be proven by publishing a DNS TXT challenge token, is watched by a verification workflow until a deadline, and is governed by an anti-abuse apparatus — an **evidence latch** (did a probe ever see the challenge?), an **abandonment ledger** (walk-aways with no evidence are counted against the member), and **initiation budgets** (how many claims a workspace may open per window). Every budget keys on the workspace, so the abuse model rests on workspaces being scarce — an assumption this card makes explicit rather than one any document guarantees. Two probes watch a pending external claim: the **TXT probe** looks for the challenge token in the domain's DNS records, and the **connect probe** checks the name points at the deployment's **connect target** (the DNS name members are told to point their domain at).
|
||||
|
||||
Allocation writes funnel through one registry API under a single advisory lock, and every *allocation* — carving, claiming, placing — runs an evaluation ladder: validity, then occupancy, then name policy. (Releases, cancels, and the verification workflow's own probe, activation, and expiry writes are status-guarded single statements that run outside the lock and the ladder.) Refusals deliberately collapse into one uninformative member message — taken, reserved, blocked, and premium are indistinguishable from outside — with one exception: a budget refusal is verbose, because every fact in it is the caller's own history.
|
||||
Allocation writes funnel through one registry API under a single advisory lock, and every *allocation* — carving, claiming, placing — runs an evaluation ladder: validity, then occupancy, then name policy. (Releases, cancels, and the verification workflow's own probe, activation, and expiry writes are status-guarded single statements that run outside the lock and the ladder.) Refusals deliberately collapse into one uninformative member message — taken, reserved, blocked, and premium are indistinguishable from outside — with one exception: a budget refusal is verbose, because every fact in it is the caller's own history. FedWiki site triggers make flag-only writes without the allocation lock; they change no claim, name or ownership.
|
||||
|
||||
The claim lifecycle, in one pass: a member's external claim is born `pending`; the verification workflow moves it to `active` when a probe matches the challenge exactly; the workflow's final probe or the sweep moves an unproven claim to `expired`; a member (or the console compensating a failed saga, or an operator by force) moves it to `canceled`; and releasing an `active` claim moves it to `released`. Carved member claims skip verification and are born active.
|
||||
|
||||
@@ -27,7 +27,7 @@ The registry is a core module with its own schema (`domains`) and migration stre
|
||||
| `domains.claims` | Subtree ownership: kind, the status machine, the challenge token, the probe states (`unchecked` / `missing` / `mismatch` / `match` / `error`, per probe), and the three ledger columns (`evidence_at`, `abandoned_at`, `system_canceled_at`) | `internal/domains/migrations/00001_init.sql`; `00002_claim_moderation.sql` |
|
||||
| `domains.placements` | One exact name wired to one provider resource; hard-deleted, never tombstoned | `internal/domains/migrations/00001_init.sql` |
|
||||
| `domains.name_rules` | Exact-label operator name policy applied to carving — reserved, blocked, and premium labels all refuse a member's carve (the kinds differ for operator bookkeeping, not member outcome); single-letter labels are premium by a built-in rule. Seed-and-SQL only, no UI | `internal/domains/migrations/00001_init.sql` |
|
||||
| The registry API | The one write path: the advisory lock, the evaluation ladder, claim/carve/place/release, budgets, and the expiry entry points | `internal/domains/registry.go` |
|
||||
| The registry API | The allocation write path (flag-only provider triggers are separate): the advisory lock, the evaluation ladder, claim/carve/place/release, budgets, and the expiry entry points | `internal/domains/registry.go` |
|
||||
| Refusal policy | The collapsed unavailable message, the budget-refusal exception, the built-in single-letter premium rule | `internal/domains/policy.go` |
|
||||
| State-machine SQL | Every claim transition as a status-guarded UPDATE; ledger reads; subtree scans | `internal/domains/queries/claims.sql` |
|
||||
| `internal/dnsname` | The dependency-free leaf package: normalize, reverse labels, validators | `internal/dnsname/dnsname.go` |
|
||||
@@ -67,6 +67,6 @@ An invariant is a rule the model guarantees everywhere; code that would break on
|
||||
- **Count-scope vs clear-scope** — the ledger counts wide and clears narrow (invariant 10). The single most damaging misreading in this model.
|
||||
- **Status vs blame** — "canceled" says nothing about fault; the ledger columns say who walked away, who was rolled back, and who had published evidence.
|
||||
- **Verification vs evidence** — two predicates over the same DNS answer: verification is exact token equality and gates activation; evidence is a looser prefix match and gates only the ledger. Verification implies evidence, never the reverse.
|
||||
- **Servable vs allocated** — servability is a flag on the placement, kept in sync from the provider resource's state (an archived FedWiki site marks its placement unservable). An unservable placement still occupies the name, still blocks release of its claim, and still refuses the ask locally; the flag controls only the yes/no to the proxy.
|
||||
- **Servable vs allocated** — FedWiki's site trigger writes the stored placement flag in the same transaction as every row insert, update or delete: active and readonly serve, archived and deleted do not. Creators read an existing site FOR SHARE immediately before placing it. Other providers and never-created reservations retain their stored answers. The certificate lookup involves no provider. An unservable placement still occupies its name, still blocks release of its claim, and still refuses the ask locally; the flag controls only the yes/no to the proxy. This supersedes the Go-maintenance description in the initial registry migrations.
|
||||
- **Name policy vs abuse policy** — `name_rules` and the single-letter rule govern only carving inside operator roots; the latch, ledger, and budgets govern only external claims. The two subsystems share no scope.
|
||||
- **Place-intent vs carve-intent** — for the root's own workspace these legitimately get different answers, and ownership alone cannot distinguish them, which is why evaluation takes the intent explicitly.
|
||||
|
||||
@@ -332,25 +332,3 @@ func (q *Queries) ListPlacementsInSubtree(ctx context.Context, reversedRoot stri
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const setPlacementServableByResource = `-- name: SetPlacementServableByResource :execrows
|
||||
UPDATE domains.placements
|
||||
SET servable = $3, updated_at = NOW()
|
||||
WHERE provider = $1 AND resource_ref = $2
|
||||
`
|
||||
|
||||
type SetPlacementServableByResourceParams struct {
|
||||
Provider string `json:"provider"`
|
||||
ResourceRef string `json:"resource_ref"`
|
||||
Servable bool `json:"servable"`
|
||||
}
|
||||
|
||||
// Provider lifecycle transitions address placements by their own resource
|
||||
// identity, not by name (fedwiki: archive => FALSE, restore => TRUE).
|
||||
func (q *Queries) SetPlacementServableByResource(ctx context.Context, arg SetPlacementServableByResourceParams) (int64, error) {
|
||||
result, err := q.db.ExecContext(ctx, setPlacementServableByResource, arg.Provider, arg.ResourceRef, arg.Servable)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return result.RowsAffected()
|
||||
}
|
||||
|
||||
@@ -242,9 +242,6 @@ type Querier interface {
|
||||
// there. COALESCE makes the latch sticky and the write idempotent, so a lost
|
||||
// write is recovered by the next probe.
|
||||
RecordClaimProbe(ctx context.Context, arg RecordClaimProbeParams) (int64, error)
|
||||
// Provider lifecycle transitions address placements by their own resource
|
||||
// identity, not by name (fedwiki: archive => FALSE, restore => TRUE).
|
||||
SetPlacementServableByResource(ctx context.Context, arg SetPlacementServableByResourceParams) (int64, error)
|
||||
}
|
||||
|
||||
var _ Querier = (*Queries)(nil)
|
||||
|
||||
@@ -46,13 +46,6 @@ SELECT * FROM domains.placements
|
||||
WHERE reversed_labels LIKE @reversed_root::text || '.%'
|
||||
ORDER BY reversed_labels;
|
||||
|
||||
-- name: SetPlacementServableByResource :execrows
|
||||
-- Provider lifecycle transitions address placements by their own resource
|
||||
-- identity, not by name (fedwiki: archive => FALSE, restore => TRUE).
|
||||
UPDATE domains.placements
|
||||
SET servable = $3, updated_at = NOW()
|
||||
WHERE provider = $1 AND resource_ref = $2;
|
||||
|
||||
-- name: DeletePlacement :execrows
|
||||
DELETE FROM domains.placements
|
||||
WHERE placement_id = $1;
|
||||
|
||||
@@ -439,8 +439,26 @@ func (r *Registry) Policy() Policy { return r.policy.Effective() }
|
||||
// Its policy is already resolved (WithLock passes Registry.Policy()), so
|
||||
// allocation code reads the fields directly.
|
||||
type Tx struct {
|
||||
q Querier
|
||||
policy Policy
|
||||
q Querier
|
||||
policy Policy
|
||||
executor DBTX
|
||||
}
|
||||
|
||||
// DBTX exposes the allocation transaction for integration-owned queries.
|
||||
// Callers may run their own reads and locks only: they must not write domains
|
||||
// tables or commit or roll back the transaction.
|
||||
func (t *Tx) DBTX() DBTX { return t.executor }
|
||||
|
||||
// PlacementByResource finds an existing placement without changing its flag.
|
||||
func (t *Tx) PlacementByResource(ctx context.Context, provider, resourceRef string) (*Placement, error) {
|
||||
placement, err := t.q.GetPlacementByResource(ctx, GetPlacementByResourceParams{Provider: provider, ResourceRef: resourceRef})
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return nil, nil
|
||||
}
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("domains: look up placement for %s/%s: %w", provider, resourceRef, err)
|
||||
}
|
||||
return &placement, nil
|
||||
}
|
||||
|
||||
// WithLock runs fn inside a transaction holding the registry advisory lock
|
||||
@@ -459,7 +477,7 @@ func (r *Registry) WithLock(ctx context.Context, fn func(context.Context, *Tx) e
|
||||
if _, err := tx.ExecContext(ctx, `SELECT pg_advisory_xact_lock($1)`, registryAllocationLock); err != nil {
|
||||
return fmt.Errorf("domains: registry advisory lock: %w", err)
|
||||
}
|
||||
if err := fn(ctx, &Tx{q: New(tx), policy: r.Policy()}); err != nil {
|
||||
if err := fn(ctx, &Tx{q: New(tx), policy: r.Policy(), executor: tx}); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := tx.Commit(); err != nil {
|
||||
@@ -1611,37 +1629,6 @@ func (r *Registry) SweepExpiredClaims(ctx context.Context) (SweepResult, error)
|
||||
return result, errors.Join(failures...)
|
||||
}
|
||||
|
||||
// SetServableByResource flips a placement's servability, the write the owning
|
||||
// provider integration makes on lifecycle transitions (FedWiki: archive =>
|
||||
// false, restore => true). It reports whether a placement was found.
|
||||
//
|
||||
// Servability changes no hierarchy, so this does not need the invariants the
|
||||
// lock protects; it takes the lock anyway to keep "every registry write is
|
||||
// serialized here" true without exception, and because these writes are rare
|
||||
// and cheap.
|
||||
func (r *Registry) SetServableByResource(ctx context.Context, provider, resourceRef string, servable bool) (bool, error) {
|
||||
var found bool
|
||||
err := r.WithLock(ctx, func(ctx context.Context, tx *Tx) error {
|
||||
var err error
|
||||
found, err = tx.SetServableByResource(ctx, provider, resourceRef, servable)
|
||||
return err
|
||||
})
|
||||
return found, err
|
||||
}
|
||||
|
||||
// SetServableByResource flips servability under an already-held lock.
|
||||
func (t *Tx) SetServableByResource(ctx context.Context, provider, resourceRef string, servable bool) (bool, error) {
|
||||
rows, err := t.q.SetPlacementServableByResource(ctx, SetPlacementServableByResourceParams{
|
||||
Provider: provider,
|
||||
ResourceRef: resourceRef,
|
||||
Servable: servable,
|
||||
})
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("domains: set servable for %s/%s: %w", provider, resourceRef, err)
|
||||
}
|
||||
return rows > 0, nil
|
||||
}
|
||||
|
||||
// namePolicy selects whether an evaluation consults operator name policy.
|
||||
// Only the grandfathering path passes bypassNamePolicy; see
|
||||
// Tx.GrandfatherCarvedClaim for why.
|
||||
|
||||
@@ -729,38 +729,6 @@ func TestReleaseRacingPlaceCannotStrandPlacement(t *testing.T) {
|
||||
assertNoStrandedPlacements(t, database)
|
||||
}
|
||||
|
||||
func TestSetServableByResource(t *testing.T) {
|
||||
r, database := newRegistryTestEnv(t)
|
||||
ctx := context.Background()
|
||||
operator, alice := newWorkspace(t, database), newWorkspace(t, database)
|
||||
root := uniqueToken() + ".test"
|
||||
if _, err := r.EnsureOperatorRoot(ctx, operator, root); err != nil {
|
||||
t.Fatalf("ensure root: %v", err)
|
||||
}
|
||||
carved := "alpha." + root
|
||||
if _, err := r.ClaimCarved(ctx, alice, carved); err != nil {
|
||||
t.Fatalf("carve: %v", err)
|
||||
}
|
||||
if _, err := r.Place(ctx, PlaceParams{WorkspaceID: alice, FQDN: carved, Provider: "testprovider", ResourceRef: "res-serve", Servable: true}); err != nil {
|
||||
t.Fatalf("place: %v", err)
|
||||
}
|
||||
|
||||
found, err := r.SetServableByResource(ctx, "testprovider", "res-serve", false)
|
||||
if err != nil || !found {
|
||||
t.Fatalf("set servable = (%v, %v), want (true, nil)", found, err)
|
||||
}
|
||||
placement, err := New(database).GetPlacementByFQDN(ctx, carved)
|
||||
if err != nil {
|
||||
t.Fatalf("read placement: %v", err)
|
||||
}
|
||||
if placement.Servable {
|
||||
t.Error("placement is still servable after the archive flip")
|
||||
}
|
||||
if found, err := r.SetServableByResource(ctx, "testprovider", "res-missing", false); err != nil || found {
|
||||
t.Errorf("set servable for an unknown resource = (%v, %v), want (false, nil)", found, err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestCheckAvailabilityOutsideEveryClaim covers the external-territory read the
|
||||
// Domains surface makes before offering verification.
|
||||
func TestCheckAvailabilityOutsideEveryClaim(t *testing.T) {
|
||||
|
||||
@@ -396,8 +396,8 @@ func (Adapter) RegisterWorkflows(w worker.Worker, database *sql.DB, logger *slog
|
||||
Database: database,
|
||||
// The domains registry is the authority for every name FedWiki serves:
|
||||
// the create saga reserves through it, delete/purge release through it,
|
||||
// and lifecycle transitions flip placement servability on it (design
|
||||
// D8). Built here from the same pooled handle rather than threaded
|
||||
// and boot and sync create placements through it; the site trigger
|
||||
// maintains their servability. Built from the same pooled handle rather than threaded
|
||||
// through the generic parameters, matching how the other FedWiki-owned
|
||||
// dependencies above are constructed.
|
||||
Registry: domains.NewRegistry(database),
|
||||
|
||||
@@ -0,0 +1,105 @@
|
||||
// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Commercial
|
||||
// SPDX-FileCopyrightText: 2025-2026 Christian Galo
|
||||
|
||||
package fedwiki
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.coopcloud.tech/wiki-cafe/member-console/internal/domains"
|
||||
"git.coopcloud.tech/wiki-cafe/member-console/internal/integrations/fedwiki/fedwikitest"
|
||||
fwmod "git.coopcloud.tech/wiki-cafe/member-console/internal/integrations/fedwiki/store"
|
||||
"git.coopcloud.tech/wiki-cafe/member-console/internal/testkit/dbtest"
|
||||
)
|
||||
|
||||
func TestBootPlacementUsesLockedStatus_DB(t *testing.T) {
|
||||
for _, row := range []bootPlacementCase{
|
||||
{"archived after listing", "active", "archived", false, false, false, false},
|
||||
{"restored after listing", "archived", "active", false, false, true, false},
|
||||
{"readonly after listing", "archived", "readonly", false, false, true, false},
|
||||
{"deleted hosted after listing", "active", "", true, false, false, false},
|
||||
{"deleted foreign after listing", "active", "", true, true, false, false},
|
||||
{"failed read", "active", "active", false, false, false, true},
|
||||
} {
|
||||
t.Run(row.name, func(t *testing.T) {
|
||||
checkBootPlacement(t, row)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// bootPlacementCase describes a listed row and its state at adoption.
|
||||
type bootPlacementCase struct {
|
||||
name, initial, status string
|
||||
deleted, foreign, want, failed bool
|
||||
}
|
||||
|
||||
// checkBootPlacement checks the real creator after changing its listed row.
|
||||
func checkBootPlacement(t *testing.T, row bootPlacementCase) {
|
||||
t.Helper()
|
||||
|
||||
database := dbtest.Open(t)
|
||||
root, system, member := setupReconcileRoot(t, database, "locked")
|
||||
registry := domains.NewRegistry(database)
|
||||
if _, err := registry.EnsureOperatorRoot(t.Context(), system, root); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
name := "listed." + root
|
||||
if row.foreign {
|
||||
name = dbtest.Name("foreign") + ".example"
|
||||
}
|
||||
id := fedwikitest.Site(t, database, member, name, row.initial)
|
||||
site, err := fwmod.New(database).GetSiteForPlacement(t.Context(), id)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if row.deleted {
|
||||
dbtest.On(t, database).Exec(`DELETE FROM fedwiki.sites WHERE site_id=$1`, id)
|
||||
} else {
|
||||
dbtest.On(t, database).Exec(`UPDATE fedwiki.sites SET status=$2 WHERE site_id=$1`, id, row.status)
|
||||
}
|
||||
var logs bytes.Buffer
|
||||
logger := slog.New(slog.NewTextHandler(&logs, nil))
|
||||
counts := backfillPlacementCounts{}
|
||||
if row.failed {
|
||||
blocker := dbtest.Tx(t, database)
|
||||
blocker.Exec(`SELECT site_id FROM fedwiki.sites WHERE site_id=$1 FOR UPDATE`, id)
|
||||
ctx, cancel := context.WithTimeout(t.Context(), 20*time.Second)
|
||||
defer cancel()
|
||||
done := make(chan struct{})
|
||||
go func() { defer close(done); reconcileSite(ctx, registry, site, system, logger, &counts) }()
|
||||
t.Cleanup(func() { cancel(); _ = blocker.Tx().Rollback(); <-done })
|
||||
dbtest.AwaitLockWait(t, database, dbtest.Backend{Query: "-- name: GetSiteForPlacement", Abort: func() error {
|
||||
select {
|
||||
case <-done:
|
||||
return fmt.Errorf("creator did not attempt locked read")
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}})
|
||||
cancel()
|
||||
<-done
|
||||
_, _, exists := placementAt(t, database, name)
|
||||
if counts.failed != 1 || exists || !strings.Contains(logs.String(), "read site for placement:") {
|
||||
t.Error("failed site read created a placement or hid the error")
|
||||
}
|
||||
} else {
|
||||
reconcileSite(t.Context(), registry, site, system, logger, &counts)
|
||||
_, servable, exists := placementAt(t, database, name)
|
||||
if row.deleted {
|
||||
if exists {
|
||||
t.Error("creator placed a deleted site")
|
||||
}
|
||||
if claimAt(t, database, name) != nil {
|
||||
t.Error("creator left a claim for a deleted site")
|
||||
}
|
||||
} else if !exists || servable != row.want {
|
||||
t.Error("creator used stale site status")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
@@ -95,10 +96,9 @@ func (Adapter) ReconcileDomains(ctx context.Context, database *sql.DB, logger *s
|
||||
// names no site row — the half of the backstop that runs in the
|
||||
// placement→site direction (design D8).
|
||||
//
|
||||
// An orphan is not inert: the authorizer serves from placements alone, so one
|
||||
// keeps `/domains/ask` issuing certificates for a name nothing serves, and it
|
||||
// keeps the name unallocatable for the member who wanted it. The release goes
|
||||
// through the registry so an emptied member claim is auto-released with its
|
||||
// A deleted site's orphan is unservable; a failed create with no site write
|
||||
// can still authorize certificates. Both keep the name unallocatable. The
|
||||
// release goes through the registry so an emptied member claim is auto-released with its
|
||||
// last placement, exactly as a member-initiated delete would leave things.
|
||||
//
|
||||
// A single row's failure never aborts the pass, for the same reason the
|
||||
@@ -244,8 +244,8 @@ func backfillSitePlacements(
|
||||
// (this is how nested pre-registry sites join the claim the shorter
|
||||
// name already created).
|
||||
//
|
||||
// Servability is seeded from the site's own status (active/readonly serve,
|
||||
// archived does not), matching what the lifecycle paths maintain afterwards.
|
||||
// Servability is seeded from the site's locked status (active/readonly serve,
|
||||
// archived does not); the site trigger maintains it afterwards.
|
||||
func reconcileSite(
|
||||
ctx context.Context,
|
||||
registry *domains.Registry,
|
||||
@@ -257,7 +257,6 @@ func reconcileSite(
|
||||
if site.SiteID == "" || site.Domain == "" {
|
||||
return
|
||||
}
|
||||
servable := workflows.IsServableStatus(site.Status)
|
||||
isSystemRow := site.WorkspaceID == systemWorkspaceID
|
||||
|
||||
var outcome string
|
||||
@@ -292,9 +291,6 @@ func reconcileSite(
|
||||
return nil
|
||||
|
||||
case claim == nil:
|
||||
if _, err := tx.GrandfatherExternalClaim(ctx, site.WorkspaceID, site.Domain); err != nil {
|
||||
return err
|
||||
}
|
||||
outcome = "grandfathered"
|
||||
|
||||
case claim.Kind == domains.KindOperatorRoot && isSystemRow:
|
||||
@@ -307,9 +303,6 @@ func reconcileSite(
|
||||
// single-character label — or any rule an operator seeded later —
|
||||
// refuse adoption on every boot and keep a member's live site off
|
||||
// the air. The namespace invariants still apply.
|
||||
if _, err := tx.GrandfatherCarvedClaim(ctx, site.WorkspaceID, site.Domain); err != nil {
|
||||
return err
|
||||
}
|
||||
outcome = "member"
|
||||
|
||||
case claim.WorkspaceID != site.WorkspaceID:
|
||||
@@ -340,12 +333,29 @@ func reconcileSite(
|
||||
outcome = "joined"
|
||||
}
|
||||
|
||||
current, err := fwmod.New(tx.DBTX()).GetSiteForPlacement(ctx, site.SiteID)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
outcome = "skipped"
|
||||
return nil
|
||||
}
|
||||
if err != nil {
|
||||
return fmt.Errorf("read site for placement: %w", err)
|
||||
}
|
||||
switch outcome {
|
||||
case "grandfathered":
|
||||
_, err = tx.GrandfatherExternalClaim(ctx, site.WorkspaceID, site.Domain)
|
||||
case "member":
|
||||
_, err = tx.GrandfatherCarvedClaim(ctx, site.WorkspaceID, site.Domain)
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = tx.Place(ctx, domains.PlaceParams{
|
||||
WorkspaceID: site.WorkspaceID,
|
||||
FQDN: site.Domain,
|
||||
Provider: fwmod.ModuleName,
|
||||
ResourceRef: site.SiteID,
|
||||
Servable: servable,
|
||||
Servable: workflows.IsServableStatus(current.Status),
|
||||
})
|
||||
return err
|
||||
})
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
-- SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Commercial
|
||||
-- SPDX-FileCopyrightText: 2025-2026 Christian Galo
|
||||
|
||||
-- +goose Up
|
||||
-- +goose StatementBegin
|
||||
-- FedWiki keeps domains.placements.servable for its sites: on every site
|
||||
-- INSERT, UPDATE and DELETE the fedwiki placements whose resource_ref is the
|
||||
-- site's id store true for active and readonly, false otherwise and on
|
||||
-- delete, in the site write's transaction. A writer that waited behind a
|
||||
-- creator's FOR SHARE must see the placement that creator committed, which
|
||||
-- needs a statement snapshot; REPEATABLE READ and SERIALIZABLE are refused
|
||||
-- with 0A000. This migration changes no row.
|
||||
CREATE FUNCTION fedwiki.set_site_placement_servability()
|
||||
RETURNS trigger
|
||||
LANGUAGE plpgsql
|
||||
SET search_path = pg_catalog, pg_temp
|
||||
AS $$
|
||||
DECLARE
|
||||
site_identifier uuid;
|
||||
should_serve boolean;
|
||||
BEGIN
|
||||
IF pg_catalog.current_setting('transaction_isolation') NOT IN ('read committed', 'read uncommitted') THEN
|
||||
RAISE EXCEPTION 'site writes require statement snapshots' USING ERRCODE = '0A000';
|
||||
END IF;
|
||||
IF TG_OP = 'DELETE' THEN
|
||||
site_identifier := OLD.site_id;
|
||||
should_serve := false;
|
||||
ELSE
|
||||
site_identifier := NEW.site_id;
|
||||
should_serve := NEW.status IN ('active', 'readonly');
|
||||
END IF;
|
||||
UPDATE domains.placements
|
||||
SET servable = should_serve
|
||||
WHERE provider = 'fedwiki'
|
||||
AND resource_ref = site_identifier::text
|
||||
AND servable IS DISTINCT FROM should_serve;
|
||||
IF TG_OP = 'DELETE' THEN
|
||||
RETURN OLD;
|
||||
END IF;
|
||||
RETURN NEW;
|
||||
END;
|
||||
$$;
|
||||
|
||||
CREATE TRIGGER trigger_sites_placement_servability
|
||||
AFTER INSERT OR UPDATE OR DELETE ON fedwiki.sites
|
||||
FOR EACH ROW EXECUTE FUNCTION fedwiki.set_site_placement_servability();
|
||||
-- +goose StatementEnd
|
||||
|
||||
-- +goose Down
|
||||
-- Restore application flag writes before removing enforcement.
|
||||
-- +goose StatementBegin
|
||||
DROP TRIGGER trigger_sites_placement_servability ON fedwiki.sites;
|
||||
DROP FUNCTION fedwiki.set_site_placement_servability();
|
||||
-- +goose StatementEnd
|
||||
@@ -0,0 +1,542 @@
|
||||
// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Commercial
|
||||
// SPDX-FileCopyrightText: 2025-2026 Christian Galo
|
||||
|
||||
package fedwiki_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"math"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5/pgconn"
|
||||
|
||||
"git.coopcloud.tech/wiki-cafe/member-console/internal/db"
|
||||
"git.coopcloud.tech/wiki-cafe/member-console/internal/dnsname"
|
||||
"git.coopcloud.tech/wiki-cafe/member-console/internal/domains"
|
||||
"git.coopcloud.tech/wiki-cafe/member-console/internal/integrations/fedwiki/fedwikitest"
|
||||
fwmod "git.coopcloud.tech/wiki-cafe/member-console/internal/integrations/fedwiki/store"
|
||||
"git.coopcloud.tech/wiki-cafe/member-console/internal/testkit/dbtest"
|
||||
)
|
||||
|
||||
// placementVersion is settled against the migration stream at landing.
|
||||
const placementVersion int64 = 8
|
||||
|
||||
// placementFixture provides a namespace for raw SQL subjects on scratch databases.
|
||||
type placementFixture struct {
|
||||
database *sql.DB
|
||||
workspace, claim, root string
|
||||
}
|
||||
|
||||
// newPlacementFixture seeds the provider and namespace independently of site writes.
|
||||
func newPlacementFixture(t *testing.T, database *sql.DB) placementFixture {
|
||||
t.Helper()
|
||||
workspace := fedwikitest.New(t, database).Tenant("Placement").Workspace.ID
|
||||
query := dbtest.On(t, database)
|
||||
query.Exec(`INSERT INTO core.providers (provider, provider_kind, display_name) VALUES ('fedwiki', 'provisioning', 'FedWiki'), ('placementtest', 'provisioning', 'Placement test') ON CONFLICT DO NOTHING`)
|
||||
root := dbtest.Name("placement") + ".test"
|
||||
claim, err := domains.NewRegistry(database).EnsureOperatorRoot(t.Context(), workspace, root)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return placementFixture{database: database, workspace: workspace, claim: claim.ClaimID, root: root}
|
||||
}
|
||||
|
||||
// reserve stores the answer supplied by a provider before any site row exists.
|
||||
func (fixture placementFixture) reserve(t *testing.T, provider, reference string, flag bool) string {
|
||||
t.Helper()
|
||||
name := dbtest.Name("site") + "." + fixture.root
|
||||
dbtest.On(t, fixture.database).Exec(`INSERT INTO domains.placements (claim_id, fqdn, reversed_labels, provider, resource_ref, servable) VALUES ($1,$2,$3,$4,$5,$6)`, fixture.claim, name, dnsname.ReverseLabels(name), provider, reference, flag)
|
||||
return name
|
||||
}
|
||||
|
||||
// site inserts a site with the exact identity a reservation references.
|
||||
func (fixture placementFixture) site(t *testing.T, id, name, status string) {
|
||||
t.Helper()
|
||||
dbtest.On(t, fixture.database).Exec(`INSERT INTO fedwiki.sites (site_id, workspace_id, domain, status) VALUES ($1,$2,$3,$4)`, id, fixture.workspace, name, status)
|
||||
}
|
||||
|
||||
// placementFlag checks both the stored column and the certificate lookup query.
|
||||
func placementFlag(t *testing.T, database domains.DBTX, name string, want bool) {
|
||||
t.Helper()
|
||||
var raw bool
|
||||
dbtest.On(t, database).Scan([]any{&raw}, `SELECT servable FROM domains.placements WHERE fqdn=$1`, name)
|
||||
placement, err := domains.New(database).GetPlacementByFQDN(t.Context(), name)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if raw != want || placement.Servable != want {
|
||||
t.Errorf("%s: placement servable = %t, want %t", name, raw, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSitePlacementTrigger_DB(t *testing.T) {
|
||||
database := archiveDatabase(t, math.MaxInt64)
|
||||
fixture := newPlacementFixture(t, database)
|
||||
for _, row := range []placementWriteCase{
|
||||
{name: "insert active", initial: "active", insert: true, want: true},
|
||||
{name: "insert readonly", initial: "readonly", insert: true, want: true},
|
||||
{name: "insert archived", initial: "archived", insert: true},
|
||||
{name: "archive", initial: "active", statement: "UPDATE fedwiki.sites SET status='archived' WHERE site_id=$1"},
|
||||
{name: "restore active", initial: "archived", statement: "UPDATE fedwiki.sites SET status='active' WHERE site_id=$1", want: true, operator: true},
|
||||
{name: "restore readonly", initial: "archived", statement: "UPDATE fedwiki.sites SET status='readonly' WHERE site_id=$1", want: true},
|
||||
{name: "repeat status", initial: "active", statement: "UPDATE fedwiki.sites SET status=status WHERE site_id=$1", want: true},
|
||||
{name: "metadata correction", initial: "active", statement: "UPDATE fedwiki.sites SET storage_bytes=42 WHERE site_id=$1", want: true},
|
||||
{name: "delete active", initial: "active", statement: "DELETE FROM fedwiki.sites WHERE site_id=$1", deleted: true},
|
||||
{name: "delete readonly", initial: "readonly", statement: "DELETE FROM fedwiki.sites WHERE site_id=$1", deleted: true},
|
||||
{name: "delete archived", initial: "archived", statement: "DELETE FROM fedwiki.sites WHERE site_id=$1", deleted: true},
|
||||
{name: "rollback archive", initial: "active", deleted: false, statement: "UPDATE fedwiki.sites SET status='archived' WHERE site_id=$1", want: true, rollback: true},
|
||||
{name: "rollback delete", initial: "active", statement: "DELETE FROM fedwiki.sites WHERE site_id=$1", want: true, rollback: true, deleted: true},
|
||||
} {
|
||||
t.Run(row.name, func(t *testing.T) {
|
||||
for _, role := range []string{"fedwiki_writer", "console_runtime"} {
|
||||
t.Run(role, func(t *testing.T) {
|
||||
checkPlacementWrite(t, fixture, row, role)
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
for _, row := range []placementUnchangedCase{
|
||||
{name: "other provider true", provider: "placementtest", same: true, want: true},
|
||||
{name: "other provider false", provider: "placementtest"},
|
||||
{name: "other site", provider: "fedwiki", want: true, existing: true},
|
||||
{name: "noncanonical reference", provider: "fedwiki", same: true, uppercase: true, want: true},
|
||||
{name: "reservation without site", provider: "fedwiki", want: true},
|
||||
} {
|
||||
t.Run(row.name, func(t *testing.T) {
|
||||
for _, role := range []string{"fedwiki_writer", "console_runtime"} {
|
||||
t.Run(role, func(t *testing.T) { checkUnchangedPlacement(t, fixture, row, role) })
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestSitePlacementMigration_DB(t *testing.T) {
|
||||
database := archiveDatabase(t, 7)
|
||||
fixture := newPlacementFixture(t, database)
|
||||
for _, status := range []string{"archived", "active"} {
|
||||
id := uuid.NewString()
|
||||
old := status == "archived"
|
||||
name := fixture.reserve(t, "fedwiki", id, old)
|
||||
fixture.site(t, id, name, status)
|
||||
}
|
||||
dbtest.ApplyVersion(t, database, fwmod.MigrationSource(), placementVersion, true)
|
||||
wrong := `SELECT count(*) FROM domains.placements p JOIN fedwiki.sites s ON p.resource_ref=s.site_id::text AND p.provider='fedwiki' WHERE p.servable <> (s.status IN ('active','readonly'))`
|
||||
if dbtest.On(t, database).Int(wrong) != 2 {
|
||||
t.Error("migration rewrote placement data")
|
||||
}
|
||||
dbtest.On(t, database).Exec(`UPDATE fedwiki.sites SET updated_at=updated_at`)
|
||||
if dbtest.On(t, database).Int(wrong) != 0 {
|
||||
t.Error("next site write left the wrong flag")
|
||||
}
|
||||
dbtest.ApplyVersion(t, database, fwmod.MigrationSource(), placementVersion, false)
|
||||
if dbtest.On(t, database).Int(`SELECT count(*) FROM pg_trigger WHERE tgname='trigger_sites_placement_servability'`)+dbtest.On(t, database).Int(`SELECT count(*) FROM pg_proc WHERE proname='set_site_placement_servability'`) != 0 {
|
||||
t.Error("down left placement enforcement installed")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSitePlacementPrivileges_DB(t *testing.T) {
|
||||
database := archiveDatabase(t, math.MaxInt64)
|
||||
fixture := newPlacementFixture(t, database)
|
||||
for _, role := range []string{"fedwiki_writer", "console_runtime"} {
|
||||
for _, allowed := range []bool{true, false} {
|
||||
t.Run(fmt.Sprintf("%s/%t", role, allowed), func(t *testing.T) {
|
||||
checkPlacementPrivilege(t, fixture, role, allowed)
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestSitePlacementIsolation_DB(t *testing.T) {
|
||||
database := archiveDatabase(t, math.MaxInt64)
|
||||
fixture := newPlacementFixture(t, database)
|
||||
for _, row := range []struct {
|
||||
level sql.IsolationLevel
|
||||
allowed bool
|
||||
}{{sql.LevelReadCommitted, true}, {sql.LevelReadUncommitted, true}, {sql.LevelRepeatableRead, false}, {sql.LevelSerializable, false}} {
|
||||
t.Run(row.level.String(), func(t *testing.T) {
|
||||
for _, placed := range []bool{false, true} {
|
||||
t.Run(fmt.Sprintf("placed=%t", placed), func(t *testing.T) {
|
||||
checkPlacementIsolation(t, fixture, row.level, row.allowed, placed)
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// checkPlacementIsolation checks the guard with and without an existing placement.
|
||||
func checkPlacementIsolation(t *testing.T, fixture placementFixture, level sql.IsolationLevel, allowed, placed bool) {
|
||||
t.Helper()
|
||||
id, name := uuid.NewString(), dbtest.Name("isolation")+".test"
|
||||
if placed {
|
||||
name = fixture.reserve(t, "fedwiki", id, true)
|
||||
fixture.site(t, id, name, "active")
|
||||
}
|
||||
transaction, err := fixture.database.BeginTx(t.Context(), &sql.TxOptions{Isolation: level})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer transaction.Rollback()
|
||||
if placed {
|
||||
_, err = transaction.ExecContext(t.Context(), `UPDATE fedwiki.sites SET status='archived' WHERE site_id=$1`, id)
|
||||
} else {
|
||||
_, err = transaction.ExecContext(t.Context(), `INSERT INTO fedwiki.sites (site_id,workspace_id,domain) VALUES ($1,$2,$3)`, id, fixture.workspace, name)
|
||||
}
|
||||
if allowed && err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if !allowed {
|
||||
var pgError *pgconn.PgError
|
||||
if err == nil || !errors.As(err, &pgError) || pgError.Code != "0A000" {
|
||||
t.Errorf("transaction snapshot was allowed to write a site: %v", err)
|
||||
}
|
||||
}
|
||||
if err := transaction.Rollback(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if placed {
|
||||
placementFlag(t, fixture.database, name, true)
|
||||
if archiveRead(t, fixture.database, id).status != "active" {
|
||||
t.Error("isolation refusal changed site")
|
||||
}
|
||||
} else if dbtest.On(t, fixture.database).Int(`SELECT count(*) FROM fedwiki.sites WHERE site_id=$1`, id) != 0 {
|
||||
t.Error("isolation refusal inserted site")
|
||||
}
|
||||
}
|
||||
|
||||
// placementAsync joins a bounded operation and exposes early completion to lock probes.
|
||||
type placementAsync struct {
|
||||
done chan struct{}
|
||||
err error
|
||||
}
|
||||
|
||||
// startPlacement runs work without assertions in the worker goroutine.
|
||||
func startPlacement(t *testing.T, work func(context.Context) error) *placementAsync {
|
||||
t.Helper()
|
||||
ctx, cancel := context.WithTimeout(t.Context(), 20*time.Second)
|
||||
call := &placementAsync{done: make(chan struct{})}
|
||||
go func() { defer close(call.done); call.err = work(ctx) }()
|
||||
t.Cleanup(func() { cancel(); <-call.done })
|
||||
return call
|
||||
}
|
||||
|
||||
// abort supplies a mutation-specific message when a backend does not wait.
|
||||
func (call *placementAsync) abort(message string) func() error {
|
||||
return func() error {
|
||||
select {
|
||||
case <-call.done:
|
||||
return fmt.Errorf("%s: %v", message, call.err)
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestPlacementCreationLocks_DB(t *testing.T) {
|
||||
database := archiveDatabase(t, math.MaxInt64)
|
||||
fixture := newPlacementFixture(t, database)
|
||||
t.Run("rollback creation", func(t *testing.T) {
|
||||
id := uuid.NewString()
|
||||
name := dbtest.Name("rollback") + "." + fixture.root
|
||||
fixture.site(t, id, name, "active")
|
||||
rollback := errors.New("roll back creation")
|
||||
err := domains.NewRegistry(database).WithLock(t.Context(), func(ctx context.Context, transaction *domains.Tx) error {
|
||||
if _, err := fwmod.New(transaction.DBTX()).GetSiteForPlacement(ctx, id); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := transaction.Place(ctx, domains.PlaceParams{WorkspaceID: fixture.workspace, FQDN: name, Provider: "fedwiki", ResourceRef: id, Servable: true}); err != nil {
|
||||
return err
|
||||
}
|
||||
return rollback
|
||||
})
|
||||
if !errors.Is(err, rollback) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if dbtest.On(t, database).Int(`SELECT count(*) FROM domains.placements WHERE fqdn=$1`, name) != 0 {
|
||||
t.Error("rolled-back creation left a placement")
|
||||
}
|
||||
})
|
||||
for _, row := range []placementLockCase{
|
||||
{name: "creator then archive", initial: "active", target: "archived", creatorFirst: true, bare: false, deleted: false, want: false},
|
||||
{name: "creator then restore", initial: "archived", target: "active", creatorFirst: true, bare: false, deleted: false, want: true},
|
||||
{name: "creator then delete", initial: "active", target: "", creatorFirst: true, bare: false, deleted: true, want: false},
|
||||
{name: "creator then bare archive", initial: "active", target: "archived", creatorFirst: true, bare: true, deleted: false, want: false},
|
||||
{name: "archive then creator", initial: "active", target: "archived", creatorFirst: false, bare: false, deleted: false, want: false},
|
||||
{name: "restore then creator", initial: "archived", target: "active", creatorFirst: false, bare: false, deleted: false, want: true},
|
||||
{name: "delete then creator", initial: "active", target: "", creatorFirst: false, bare: false, deleted: true, want: false},
|
||||
} {
|
||||
t.Run(row.name, func(t *testing.T) {
|
||||
checkPlacementLock(t, fixture, row)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// placementWriteCase states the SQL subject and expected answer.
|
||||
type placementWriteCase struct {
|
||||
name, initial, statement string
|
||||
insert, want, rollback, operator, deleted bool
|
||||
}
|
||||
|
||||
// checkPlacementWrite exercises one runtime role without stopping before flag assertions.
|
||||
func checkPlacementWrite(t *testing.T, fixture placementFixture, row placementWriteCase, role string) {
|
||||
t.Helper()
|
||||
database := fixture.database
|
||||
|
||||
id := uuid.NewString()
|
||||
name := fixture.reserve(t, "fedwiki", id, !row.want)
|
||||
if !row.insert {
|
||||
fixture.site(t, id, name, row.initial)
|
||||
}
|
||||
flag := !row.want
|
||||
if row.rollback {
|
||||
flag = row.want
|
||||
}
|
||||
dbtest.On(t, database).Exec(`UPDATE domains.placements SET servable=$2 WHERE fqdn=$1`, name, flag)
|
||||
transaction, err := db.Begin(t.Context(), database, db.ReadCommitted)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer transaction.Rollback()
|
||||
query := dbtest.On(t, transaction)
|
||||
query.Exec("SET LOCAL ROLE " + role)
|
||||
if row.insert {
|
||||
query.Exec(`INSERT INTO fedwiki.sites (site_id,workspace_id,domain,status) VALUES ($1,$2,$3,$4)`, id, fixture.workspace, name, row.initial)
|
||||
} else {
|
||||
query.Exec(row.statement, id)
|
||||
}
|
||||
if row.rollback {
|
||||
err = transaction.Rollback()
|
||||
} else {
|
||||
err = transaction.Commit()
|
||||
}
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
placementFlag(t, database, name, row.want)
|
||||
count := dbtest.On(t, database).Int(`SELECT count(*) FROM fedwiki.sites WHERE site_id=$1`, id)
|
||||
wantCount := int64(1)
|
||||
if row.deleted && !row.rollback {
|
||||
wantCount = 0
|
||||
}
|
||||
if count != wantCount {
|
||||
t.Errorf("site count = %d, want %d", count, wantCount)
|
||||
}
|
||||
if row.operator {
|
||||
placements, err := domains.New(database).ListAllPlacementsForOperator(t.Context())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
found := false
|
||||
for _, placement := range placements {
|
||||
if placement.Fqdn == name && placement.Servable {
|
||||
found = true
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
t.Error("restored site is not servable in the operator list")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// checkPlacementPrivilege tests the inherited column grant or its denial.
|
||||
func checkPlacementPrivilege(t *testing.T, fixture placementFixture, role string, allowed bool) {
|
||||
t.Helper()
|
||||
database := fixture.database
|
||||
|
||||
id := uuid.NewString()
|
||||
name := fixture.reserve(t, "fedwiki", id, true)
|
||||
fixture.site(t, id, name, "active")
|
||||
transaction, err := db.Begin(t.Context(), database, db.ReadCommitted)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer transaction.Rollback()
|
||||
query := dbtest.On(t, transaction)
|
||||
query.Exec(`REVOKE UPDATE ON domains.placements FROM domains_writer`)
|
||||
if allowed {
|
||||
query.Exec(`GRANT UPDATE(servable) ON domains.placements TO domains_writer`)
|
||||
}
|
||||
query.Exec("SET LOCAL ROLE " + role)
|
||||
_, err = transaction.ExecContext(t.Context(), `UPDATE fedwiki.sites SET status='archived' WHERE site_id=$1`, id)
|
||||
if allowed && err != nil {
|
||||
t.Errorf("column-only update grant did not suffice: %v", err)
|
||||
}
|
||||
if !allowed {
|
||||
var pgError *pgconn.PgError
|
||||
if err == nil || !errors.As(err, &pgError) || pgError.Code != "42501" {
|
||||
t.Errorf("placement denial did not abort the site write: %v", err)
|
||||
}
|
||||
}
|
||||
if err := transaction.Rollback(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
placementFlag(t, database, name, true)
|
||||
if archiveRead(t, database, id).status != "active" {
|
||||
t.Error("denied write changed site")
|
||||
}
|
||||
}
|
||||
|
||||
// placementLockCase states the write order and expected placement.
|
||||
type placementLockCase struct {
|
||||
name, initial, target string
|
||||
creatorFirst, bare, deleted, want bool
|
||||
}
|
||||
|
||||
// checkPlacementLock orders the two real transactions using observed backend waits.
|
||||
func checkPlacementLock(t *testing.T, fixture placementFixture, row placementLockCase) {
|
||||
t.Helper()
|
||||
database := fixture.database
|
||||
|
||||
id := uuid.NewString()
|
||||
name := dbtest.Name("locked") + "." + fixture.root
|
||||
fixture.site(t, id, name, row.initial)
|
||||
ready, release := make(chan struct{}), make(chan struct{})
|
||||
creator := func(ctx context.Context) error {
|
||||
return createLockedPlacement(ctx, fixture, row, id, name, ready, release)
|
||||
}
|
||||
writer := func(ctx context.Context) error { return writeLockedSite(ctx, fixture, row, id, name, ready, release) }
|
||||
first, second := creator, writer
|
||||
marker := "status writer passed the creator's site lock"
|
||||
if !row.creatorFirst {
|
||||
first, second = writer, creator
|
||||
marker = "creator did not wait for the site writer"
|
||||
}
|
||||
firstCall := startPlacement(t, first)
|
||||
select {
|
||||
case <-ready:
|
||||
case <-firstCall.done:
|
||||
t.Fatalf("first operation: %v", firstCall.err)
|
||||
}
|
||||
secondCall := startPlacement(t, second)
|
||||
t.Cleanup(func() {
|
||||
select {
|
||||
case <-release:
|
||||
default:
|
||||
close(release)
|
||||
}
|
||||
})
|
||||
dbtest.AwaitLockWait(t, database, dbtest.Backend{Query: "fedwiki.sites", Abort: secondCall.abort(marker)})
|
||||
close(release)
|
||||
<-firstCall.done
|
||||
<-secondCall.done
|
||||
if firstCall.err != nil || secondCall.err != nil {
|
||||
t.Fatalf("operations: %v, %v", firstCall.err, secondCall.err)
|
||||
}
|
||||
placement, err := domains.New(database).GetPlacementByFQDN(t.Context(), name)
|
||||
if row.deleted && !row.creatorFirst {
|
||||
if !errors.Is(err, sql.ErrNoRows) {
|
||||
t.Errorf("%s: creator used stale site state", row.name)
|
||||
}
|
||||
} else if err != nil || placement.Servable != row.want {
|
||||
message := "writer left wrong placement flag"
|
||||
if !row.creatorFirst {
|
||||
message = "creator used stale site state"
|
||||
}
|
||||
t.Errorf("%s: %s: %v", row.name, message, err)
|
||||
}
|
||||
}
|
||||
|
||||
// createLockedPlacement runs one side of the placement creation interleaving.
|
||||
func createLockedPlacement(ctx context.Context, fixture placementFixture, row placementLockCase, id, name string, ready, release chan struct{}) error {
|
||||
database := fixture.database
|
||||
|
||||
return domains.NewRegistry(database).WithLock(ctx, func(ctx context.Context, transaction *domains.Tx) error {
|
||||
current, err := fwmod.New(transaction.DBTX()).GetSiteForPlacement(ctx, id)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return nil
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if row.creatorFirst {
|
||||
close(ready)
|
||||
select {
|
||||
case <-release:
|
||||
case <-ctx.Done():
|
||||
return ctx.Err()
|
||||
}
|
||||
}
|
||||
_, err = transaction.Place(ctx, domains.PlaceParams{WorkspaceID: fixture.workspace, FQDN: name, Provider: "fedwiki", ResourceRef: id, Servable: current.Status == "active" || current.Status == "readonly"})
|
||||
return err
|
||||
})
|
||||
}
|
||||
|
||||
// writeLockedSite runs the status or delete writer of the interleaving,
|
||||
// locking the row first unless the row is bare.
|
||||
func writeLockedSite(ctx context.Context, fixture placementFixture, row placementLockCase, id, name string, ready, release chan struct{}) error {
|
||||
database := fixture.database
|
||||
|
||||
transaction, err := db.Begin(ctx, database, db.ReadCommitted)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer transaction.Rollback()
|
||||
if !row.bare {
|
||||
if _, err := fwmod.New(transaction).LockSiteStatusByDomain(ctx, name); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if row.deleted {
|
||||
_, err = transaction.ExecContext(ctx, `DELETE FROM fedwiki.sites WHERE site_id=$1`, id)
|
||||
} else {
|
||||
_, err = fwmod.New(transaction).SetSiteStatusByDomain(ctx, fwmod.SetSiteStatusByDomainParams{Domain: name, Status: row.target})
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !row.creatorFirst {
|
||||
close(ready)
|
||||
select {
|
||||
case <-release:
|
||||
case <-ctx.Done():
|
||||
return ctx.Err()
|
||||
}
|
||||
}
|
||||
return transaction.Commit()
|
||||
}
|
||||
|
||||
// placementUnchangedCase names a placement outside the written site's ownership.
|
||||
type placementUnchangedCase struct {
|
||||
name, provider string
|
||||
same, uppercase, want, existing bool
|
||||
}
|
||||
|
||||
// checkUnchangedPlacement runs every subject write under one runtime role.
|
||||
func checkUnchangedPlacement(t *testing.T, fixture placementFixture, row placementUnchangedCase, role string) {
|
||||
t.Helper()
|
||||
id, reference := uuid.NewString(), uuid.NewString()
|
||||
if row.provider != "fedwiki" && !row.same {
|
||||
reference = "opaque-" + id
|
||||
}
|
||||
if row.same {
|
||||
reference = id
|
||||
}
|
||||
if row.uppercase {
|
||||
reference = strings.ToUpper(reference)
|
||||
}
|
||||
name := fixture.reserve(t, row.provider, reference, row.want)
|
||||
transaction, err := db.Begin(t.Context(), fixture.database, db.ReadCommitted)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer transaction.Rollback()
|
||||
query := dbtest.On(t, transaction)
|
||||
query.Exec("SET LOCAL ROLE " + role)
|
||||
if row.existing {
|
||||
query.Exec(`INSERT INTO fedwiki.sites (site_id,workspace_id,domain,status) VALUES ($1,$2,$3,'active')`, reference, fixture.workspace, name)
|
||||
}
|
||||
query.Exec(`INSERT INTO fedwiki.sites (site_id,workspace_id,domain,status) VALUES ($1,$2,$3,'active')`, id, fixture.workspace, dbtest.Name("written")+"."+fixture.root)
|
||||
placementFlag(t, transaction, name, row.want)
|
||||
query.Exec(`UPDATE fedwiki.sites SET status='archived' WHERE site_id=$1`, id)
|
||||
placementFlag(t, transaction, name, row.want)
|
||||
query.Exec(`DELETE FROM fedwiki.sites WHERE site_id=$1`, id)
|
||||
placementFlag(t, transaction, name, row.want)
|
||||
if err := transaction.Commit(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
@@ -50,6 +50,11 @@ type Querier interface {
|
||||
// render it and stay plain
|
||||
// SELECT * so they keep returning the Site model.
|
||||
GetSiteByID(ctx context.Context, siteID string) (GetSiteByIDRow, error)
|
||||
// The site a boot or sync creator places, read in the registry transaction
|
||||
// immediately before its first claim or placement write. FOR SHARE conflicts
|
||||
// with FOR UPDATE and with a bare status UPDATE (FOR KEY SHARE would not), so
|
||||
// a status write waits until the placement commits and its trigger updates it.
|
||||
GetSiteForPlacement(ctx context.Context, siteID string) (Site, error)
|
||||
GetSiteSwapPolicy(ctx context.Context, workspaceID string) (time.Time, error)
|
||||
// Sync projection for a farm site the console has no row for: insert it in the
|
||||
// default workspace, and leave a row that exists by now alone. A create can
|
||||
@@ -117,8 +122,8 @@ type Querier interface {
|
||||
// ListSitesWithoutPlacement: every `fedwiki` placement whose resource_ref
|
||||
// names no `fedwiki.sites` row. A create saga whose compensation never ran and
|
||||
// a delete interrupted between freeing the row and freeing the name both leave
|
||||
// one, and until it is released `/domains/ask` keeps authorizing certificates
|
||||
// for a name nothing serves. A placement whose resource_ref is the instance
|
||||
// one. Deletion stores false; a failed create with no site write can still
|
||||
// authorize certificates until release. A placement whose resource_ref is the instance
|
||||
// of a create request still `accepted` is not an orphan: the running create
|
||||
// workflow reserved it and has not inserted the row yet. Ordered by name for
|
||||
// a stable pass.
|
||||
@@ -146,9 +151,8 @@ type Querier interface {
|
||||
LockSiteStatusByID(ctx context.Context, siteID string) (string, error)
|
||||
SetForceReducedByDomain(ctx context.Context, domain string) error
|
||||
// The farm's storage and last-modified time for a site whose listed status
|
||||
// equals the row's. It writes no status, so no count changes; the row it
|
||||
// returns carries the status the row holds now, which the sync's placement
|
||||
// projection follows. No row is sql.ErrNoRows.
|
||||
// equals the row's. It writes no status, so no count changes; its trigger
|
||||
// corrects the placement flag. No row is sql.ErrNoRows.
|
||||
SetSiteObservedByDomain(ctx context.Context, arg SetSiteObservedByDomainParams) (Site, error)
|
||||
// Project a lifecycle status change locally. archived_at is maintained by
|
||||
// the trigger function fedwiki.set_site_archived_at on every site write.
|
||||
|
||||
@@ -1,3 +1,10 @@
|
||||
-- name: GetSiteForPlacement :one
|
||||
-- The site a boot or sync creator places, read in the registry transaction
|
||||
-- immediately before its first claim or placement write. FOR SHARE conflicts
|
||||
-- with FOR UPDATE and with a bare status UPDATE (FOR KEY SHARE would not), so
|
||||
-- a status write waits until the placement commits and its trigger updates it.
|
||||
SELECT * FROM fedwiki.sites WHERE site_id = $1 FOR SHARE;
|
||||
|
||||
-- name: CreateSite :one
|
||||
-- site_id is supplied by the caller, not defaulted: the creation saga
|
||||
-- pre-generates it so the registry placement can reserve the name
|
||||
@@ -172,9 +179,8 @@ SELECT clock_timestamp()::timestamptz AS now;
|
||||
|
||||
-- name: SetSiteObservedByDomain :one
|
||||
-- The farm's storage and last-modified time for a site whose listed status
|
||||
-- equals the row's. It writes no status, so no count changes; the row it
|
||||
-- returns carries the status the row holds now, which the sync's placement
|
||||
-- projection follows. No row is sql.ErrNoRows.
|
||||
-- equals the row's. It writes no status, so no count changes; its trigger
|
||||
-- corrects the placement flag. No row is sql.ErrNoRows.
|
||||
UPDATE fedwiki.sites
|
||||
SET storage_bytes = $2,
|
||||
last_modified_at = $3,
|
||||
@@ -289,8 +295,8 @@ ORDER BY length(domain) ASC, domain ASC;
|
||||
-- ListSitesWithoutPlacement: every `fedwiki` placement whose resource_ref
|
||||
-- names no `fedwiki.sites` row. A create saga whose compensation never ran and
|
||||
-- a delete interrupted between freeing the row and freeing the name both leave
|
||||
-- one, and until it is released `/domains/ask` keeps authorizing certificates
|
||||
-- for a name nothing serves. A placement whose resource_ref is the instance
|
||||
-- one. Deletion stores false; a failed create with no site write can still
|
||||
-- authorize certificates until release. A placement whose resource_ref is the instance
|
||||
-- of a create request still `accepted` is not an orphan: the running create
|
||||
-- workflow reserved it and has not inserted the row yet. Ordered by name for
|
||||
-- a stable pass.
|
||||
|
||||
@@ -244,6 +244,32 @@ func (q *Queries) GetSiteByID(ctx context.Context, siteID string) (GetSiteByIDRo
|
||||
return i, err
|
||||
}
|
||||
|
||||
const getSiteForPlacement = `-- name: GetSiteForPlacement :one
|
||||
SELECT site_id, workspace_id, domain, created_at, updated_at, status, storage_bytes, last_modified_at, force_reduced_at, archived_at FROM fedwiki.sites WHERE site_id = $1 FOR SHARE
|
||||
`
|
||||
|
||||
// The site a boot or sync creator places, read in the registry transaction
|
||||
// immediately before its first claim or placement write. FOR SHARE conflicts
|
||||
// with FOR UPDATE and with a bare status UPDATE (FOR KEY SHARE would not), so
|
||||
// a status write waits until the placement commits and its trigger updates it.
|
||||
func (q *Queries) GetSiteForPlacement(ctx context.Context, siteID string) (Site, error) {
|
||||
row := q.db.QueryRowContext(ctx, getSiteForPlacement, siteID)
|
||||
var i Site
|
||||
err := row.Scan(
|
||||
&i.SiteID,
|
||||
&i.WorkspaceID,
|
||||
&i.Domain,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
&i.Status,
|
||||
&i.StorageBytes,
|
||||
&i.LastModifiedAt,
|
||||
&i.ForceReducedAt,
|
||||
&i.ArchivedAt,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const getSiteSwapPolicy = `-- name: GetSiteSwapPolicy :one
|
||||
SELECT last_swap_at FROM fedwiki.site_swap_policy WHERE workspace_id = $1
|
||||
`
|
||||
@@ -770,8 +796,8 @@ type ListOrphanedPlacementsRow struct {
|
||||
// ListSitesWithoutPlacement: every `fedwiki` placement whose resource_ref
|
||||
// names no `fedwiki.sites` row. A create saga whose compensation never ran and
|
||||
// a delete interrupted between freeing the row and freeing the name both leave
|
||||
// one, and until it is released `/domains/ask` keeps authorizing certificates
|
||||
// for a name nothing serves. A placement whose resource_ref is the instance
|
||||
// one. Deletion stores false; a failed create with no site write can still
|
||||
// authorize certificates until release. A placement whose resource_ref is the instance
|
||||
// of a create request still `accepted` is not an orphan: the running create
|
||||
// workflow reserved it and has not inserted the row yet. Ordered by name for
|
||||
// a stable pass.
|
||||
@@ -1035,9 +1061,8 @@ type SetSiteObservedByDomainParams struct {
|
||||
}
|
||||
|
||||
// The farm's storage and last-modified time for a site whose listed status
|
||||
// equals the row's. It writes no status, so no count changes; the row it
|
||||
// returns carries the status the row holds now, which the sync's placement
|
||||
// projection follows. No row is sql.ErrNoRows.
|
||||
// equals the row's. It writes no status, so no count changes; its trigger
|
||||
// corrects the placement flag. No row is sql.ErrNoRows.
|
||||
func (q *Queries) SetSiteObservedByDomain(ctx context.Context, arg SetSiteObservedByDomainParams) (Site, error) {
|
||||
row := q.db.QueryRowContext(ctx, setSiteObservedByDomain, arg.Domain, arg.StorageBytes, arg.LastModifiedAt)
|
||||
var i Site
|
||||
|
||||
@@ -25,8 +25,9 @@ type Activities struct {
|
||||
EntitlementsQ entitlements.Querier
|
||||
Database *sql.DB
|
||||
// Registry is the domains registry: the authority for every name FedWiki
|
||||
// serves. Allocation, release, and servability writes go through it
|
||||
// (design D8). Nil in tests that exercise no name-bearing path; every
|
||||
// serves. Allocation and release go through it (design D8); the site
|
||||
// trigger in FedWiki migration 00008 writes placement servability.
|
||||
// Nil in tests that exercise no name-bearing path; every
|
||||
// registry touch below is nil-guarded so those keep working.
|
||||
Registry *domains.Registry
|
||||
Logger *slog.Logger
|
||||
@@ -324,29 +325,10 @@ type SetSiteStatusInput struct {
|
||||
Status string // active | readonly | archived
|
||||
}
|
||||
|
||||
// setPlacementServability keeps the site's placement in step with its
|
||||
// lifecycle: archived names stop being served, active and readonly ones serve.
|
||||
// Best-effort — the status change already landed on the farm and locally, so a
|
||||
// registry hiccup must not fail the transition; the next sync projection
|
||||
// re-asserts servability from the observed status.
|
||||
func (a *Activities) setPlacementServability(ctx context.Context, siteID, domain, status string) {
|
||||
if a.Registry == nil || siteID == "" {
|
||||
return
|
||||
}
|
||||
servable := IsServableStatus(status)
|
||||
if _, err := a.Registry.SetServableByResource(ctx, fwmod.ModuleName, siteID, servable); err != nil {
|
||||
a.Logger.Warn("failed to update placement servability",
|
||||
slog.String("domain", domain),
|
||||
slog.String("status", status),
|
||||
slog.Bool("servable", servable),
|
||||
slog.Any("error", err))
|
||||
}
|
||||
}
|
||||
|
||||
// IsServableStatus reports whether a site in this lifecycle state should have
|
||||
// its name served: `active` and `readonly` do (a read-only wiki is still a
|
||||
// wiki), `archived` does not.
|
||||
func IsServableStatus(status string) bool { return status != "archived" }
|
||||
func IsServableStatus(status string) bool { return status == "active" || status == "readonly" }
|
||||
|
||||
// ListFedWikiSitesInput is the input for the ListFedWikiSites activity.
|
||||
type ListFedWikiSitesInput struct{}
|
||||
|
||||
@@ -0,0 +1,231 @@
|
||||
// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Commercial
|
||||
// SPDX-FileCopyrightText: 2025-2026 Christian Galo
|
||||
|
||||
package workflows
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.coopcloud.tech/wiki-cafe/member-console/internal/domains"
|
||||
"git.coopcloud.tech/wiki-cafe/member-console/internal/integration"
|
||||
"git.coopcloud.tech/wiki-cafe/member-console/internal/integrations/fedwiki/fedwikitest"
|
||||
fwmod "git.coopcloud.tech/wiki-cafe/member-console/internal/integrations/fedwiki/store"
|
||||
"git.coopcloud.tech/wiki-cafe/member-console/internal/testkit/dbtest"
|
||||
)
|
||||
|
||||
func TestRecordPlacementServability_DB(t *testing.T) {
|
||||
for _, row := range []placementRecordCase{
|
||||
{"archive", "active", "archived", false, false},
|
||||
{"restore", "archived", "active", false, true},
|
||||
{"swap archived target", "archived", "active", true, true},
|
||||
{"swap readonly target", "readonly", "active", true, true},
|
||||
} {
|
||||
t.Run(row.name, func(t *testing.T) {
|
||||
checkRecordPlacement(t, row)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestSyncPlacementUsesLockedStatus_DB(t *testing.T) {
|
||||
for _, row := range []struct {
|
||||
name, initial, status string
|
||||
deleted, want bool
|
||||
}{
|
||||
{"archived after listing", "active", "archived", false, false},
|
||||
{"restored after listing", "archived", "active", false, true},
|
||||
{"readonly after listing", "archived", "readonly", false, true},
|
||||
{"deleted after listing", "active", "", true, false},
|
||||
} {
|
||||
t.Run(row.name, func(t *testing.T) {
|
||||
env := newReqEnv(t, 5, 0)
|
||||
name := "listed." + env.root
|
||||
system := dbtest.On(t, env.db).String(`SELECT workspace_id FROM domains.claims WHERE root_fqdn=$1`, env.root)
|
||||
id := fedwikitest.Site(t, env.db, system, name, row.initial)
|
||||
site, err := fwmod.New(env.db).GetSiteForPlacement(env.ctx, id)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if row.deleted {
|
||||
dbtest.On(t, env.db).Exec(`DELETE FROM fedwiki.sites WHERE site_id=$1`, id)
|
||||
} else {
|
||||
dbtest.On(t, env.db).Exec(`UPDATE fedwiki.sites SET status=$2 WHERE site_id=$1`, id, row.status)
|
||||
}
|
||||
env.acts.syncPlacement(env.ctx, site, system)
|
||||
placement := placementFor(t, env.db, name)
|
||||
if row.deleted {
|
||||
if placement != nil {
|
||||
t.Error("creator placed a deleted site")
|
||||
}
|
||||
} else if placement == nil || placement.Servable != row.want {
|
||||
t.Error("creator used stale site status")
|
||||
}
|
||||
})
|
||||
}
|
||||
t.Run("failed read", func(t *testing.T) {
|
||||
env := newReqEnv(t, 5, 0)
|
||||
name := "failed." + env.root
|
||||
system := dbtest.On(t, env.db).String(`SELECT workspace_id FROM domains.claims WHERE root_fqdn=$1`, env.root)
|
||||
id := fedwikitest.Site(t, env.db, system, name, "active")
|
||||
site, err := fwmod.New(env.db).GetSiteForPlacement(env.ctx, id)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
blocker := dbtest.Tx(t, env.db)
|
||||
blocker.Exec(`SELECT site_id FROM fedwiki.sites WHERE site_id=$1 FOR UPDATE`, id)
|
||||
var logs bytes.Buffer
|
||||
env.acts.Logger = slog.New(slog.NewTextHandler(&logs, nil))
|
||||
ctx, cancel := context.WithTimeout(env.ctx, 20*time.Second)
|
||||
defer cancel()
|
||||
done := make(chan struct{})
|
||||
go func() { defer close(done); env.acts.syncPlacement(ctx, site, system) }()
|
||||
t.Cleanup(func() { cancel(); _ = blocker.Tx().Rollback(); <-done })
|
||||
dbtest.AwaitLockWait(t, env.db, dbtest.Backend{Query: "-- name: GetSiteForPlacement", Abort: func() error {
|
||||
select {
|
||||
case <-done:
|
||||
return errors.New("creator did not attempt locked read")
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}})
|
||||
cancel()
|
||||
<-done
|
||||
if placementFor(t, env.db, name) != nil || !strings.Contains(logs.String(), "read site for placement:") {
|
||||
t.Error("failed site read created a placement or hid the error")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestSyncMetadataPlacementIsolation_DB(t *testing.T) {
|
||||
env := newReqEnv(t, 5, 0)
|
||||
name := "metadata." + env.root
|
||||
id := env.insertSite(name, "active", false)
|
||||
env.placeServable(name, id)
|
||||
dbtest.On(t, env.db).Exec(`UPDATE domains.placements SET servable=false WHERE resource_ref=$1`, id)
|
||||
env.acts.Database = purgeRepeatableRead(t)
|
||||
pass := syncPass{a: env.acts}
|
||||
_, written := pass.observe(env.ctx, SiteInfo{Name: name})
|
||||
placement := placementFor(t, env.db, name)
|
||||
if !written || placement == nil || !placement.Servable {
|
||||
t.Error("metadata correction used the database isolation default")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeletedSitePlacementRefuses_DB(t *testing.T) {
|
||||
for _, row := range []struct {
|
||||
name, status string
|
||||
purge bool
|
||||
}{{"purged archived", "archived", true}, {"sync archived", "archived", false}, {"sync active", "active", false}} {
|
||||
t.Run(row.name, func(t *testing.T) {
|
||||
env := newReqEnv(t, 5, 0)
|
||||
name := "deleted." + env.root
|
||||
id := env.insertSite(name, row.status, false)
|
||||
env.placeServable(name, id)
|
||||
dbtest.On(t, env.db).Exec(`UPDATE domains.placements SET servable=true WHERE resource_ref=$1`, id)
|
||||
if row.purge {
|
||||
observePurgeFarm(env, nil)
|
||||
site, err := fwmod.New(env.db).GetSiteForPlacement(env.ctx, id)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
deleted, err := env.acts.purgeArchivedSite(env.ctx, env.db, site, site.ArchivedAt.Time.Add(time.Microsecond))
|
||||
if err != nil || !deleted {
|
||||
t.Fatalf("purge: %t %v", deleted, err)
|
||||
}
|
||||
} else {
|
||||
broken := *env.acts
|
||||
broken.Registry = domains.NewRegistry(nil)
|
||||
pass := syncPass{a: &broken, input: SyncSitesToDBInput{DefaultWorkspaceID: env.ws}, localSites: []fwmod.ListAllSitesRow{{SiteID: id, Domain: name, WorkspaceID: env.ws}}}
|
||||
pass.deleteFarmAbsent(env.ctx)
|
||||
}
|
||||
var calls atomic.Int64
|
||||
fallback := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, _ *http.Request) { calls.Add(1); writer.WriteHeader(http.StatusOK) }))
|
||||
defer fallback.Close()
|
||||
allowed, err := domains.NewRegistryAuthorizer(domains.New(env.db), fallback.URL, env.acts.Logger).AuthorizeDomain(env.ctx, name)
|
||||
placement := placementFor(t, env.db, name)
|
||||
if err != nil || allowed || calls.Load() != 0 || placement == nil || placement.Servable {
|
||||
t.Errorf("deleted site still authorizes certificates: %t %v", allowed, err)
|
||||
}
|
||||
if dbtest.On(t, env.db).Int(`SELECT count(*) FROM fedwiki.sites WHERE site_id=$1`, id) != 0 {
|
||||
t.Error("deleted site remains")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// placementRecordCase states the lifecycle operation and expected flag.
|
||||
type placementRecordCase struct {
|
||||
name, initial, target string
|
||||
swap, want bool
|
||||
}
|
||||
|
||||
// checkRecordPlacement checks both the first commit and an idempotent retry.
|
||||
func checkRecordPlacement(t *testing.T, row placementRecordCase) {
|
||||
t.Helper()
|
||||
|
||||
env := newReqEnv(t, 5, 0)
|
||||
name := "target." + env.root
|
||||
id := env.insertSite(name, row.initial, false)
|
||||
env.placeServable(name, id)
|
||||
dbtest.On(t, env.db).Exec(`UPDATE domains.placements SET servable=$2 WHERE resource_ref=$1`, id, !row.want)
|
||||
var run func() error
|
||||
var generation int64
|
||||
parkedName := "parked." + env.root
|
||||
parkedID := ""
|
||||
var parkedGeneration int64
|
||||
if row.swap {
|
||||
parkedID = env.insertSite(parkedName, "active", false)
|
||||
env.placeServable(parkedName, parkedID)
|
||||
dbtest.On(t, env.db).Exec(`UPDATE domains.placements SET servable=false WHERE resource_ref=$1`, parkedID)
|
||||
generation, parkedGeneration = env.swapRequests(id, parkedID, "placement-"+id)
|
||||
run = func() error {
|
||||
return env.acts.RecordSwapActivity(env.ctx, RecordSwapInput{Activate: LifecycleRequestRef{InstanceID: id, Generation: generation}, Park: LifecycleRequestRef{InstanceID: parkedID, Generation: parkedGeneration}, ActivateDomain: name, ParkDomain: parkedName, FinishedAt: time.Now()})
|
||||
}
|
||||
} else {
|
||||
generation = env.record(id, integration.LifecycleSetStatus, "placement-"+id, map[string]any{"domain": name, "status": row.target})
|
||||
run = func() error {
|
||||
return env.acts.RecordSiteStatusActivity(env.ctx, RecordSiteStatusInput{InstanceID: id, Generation: generation, WorkspaceID: env.ws, Domain: name, Status: row.target, FinishedAt: time.Now()})
|
||||
}
|
||||
}
|
||||
env.acts.Registry = nil
|
||||
for range 2 {
|
||||
if err := run(); err != nil {
|
||||
t.Errorf("record failed: %v", err)
|
||||
}
|
||||
status := env.siteStatus(id)
|
||||
outcome, _ := env.request(id, generation)
|
||||
if status != row.target || outcome != "succeeded" {
|
||||
t.Errorf("record left wrong status or outcome: %s %s", status, outcome)
|
||||
}
|
||||
if archiveTimeOf(env, id).Valid != (row.target == "archived") {
|
||||
t.Errorf("archive time does not match status")
|
||||
}
|
||||
figure, rows := env.figureAndRows()
|
||||
if figure != rows {
|
||||
t.Errorf("record left wrong usage: %d != %d", figure, rows)
|
||||
}
|
||||
placement := placementFor(t, env.db, name)
|
||||
if placement == nil || placement.Servable != row.want {
|
||||
got := placement != nil && placement.Servable
|
||||
t.Errorf("record left servable = %t, want %t", got, row.want)
|
||||
}
|
||||
if row.swap {
|
||||
parkedOutcome, _ := env.request(parkedID, parkedGeneration)
|
||||
if env.siteStatus(parkedID) != "readonly" || parkedOutcome != "succeeded" || archiveTimeOf(env, parkedID).Valid {
|
||||
t.Errorf("record left wrong status or outcome")
|
||||
}
|
||||
parked := placementFor(t, env.db, parkedName)
|
||||
if parked == nil || !parked.Servable {
|
||||
t.Errorf("record left servable = false, want true")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -262,11 +262,8 @@ func (a *Activities) reconcilePoolQuota(ctx context.Context, poolID string) (par
|
||||
// (Open Questions 66). The site's slot is the row's own: it stops counting
|
||||
// when the row leaves active.
|
||||
//
|
||||
// Deliberately no registry write: park moves active→readonly and reactivateSite
|
||||
// below moves readonly→active, and BOTH states are servable (design D8 — a
|
||||
// read-only wiki still serves). Only the archived boundary changes servability,
|
||||
// and neither of these crosses it. The same holds for the swap in swap.go,
|
||||
// which is a park plus a reactivate.
|
||||
// The site trigger maintains placement servability in the status transaction;
|
||||
// both active and readonly serve, including after a concurrent archive.
|
||||
func (a *Activities) parkSite(ctx context.Context, site fwmod.Site) bool {
|
||||
if a.siteRequestOpen(ctx, site) {
|
||||
return false
|
||||
|
||||
@@ -370,14 +370,14 @@ type RecordSiteStatusInput struct {
|
||||
// RecordSiteStatusActivity writes the site's new status, the clearing of its
|
||||
// downgrade-parked mark, and the request's outcome in one transaction (design
|
||||
// D28); finishAllWith removes the request's hold and recounts the pool, so
|
||||
// the figure follows the row. Placement servability follows after the commit,
|
||||
// best-effort.
|
||||
// the figure follows the row. The site's trigger writes placement servability
|
||||
// in that same transaction.
|
||||
//
|
||||
// The site row is locked here, so the sweep that parked the site after the
|
||||
// member asked and this write order on the row; the figure is a recount, so
|
||||
// neither releases a slot by arithmetic (Open Questions 66).
|
||||
func (a *Activities) RecordSiteStatusActivity(ctx context.Context, in RecordSiteStatusInput) error {
|
||||
applied, err := a.finishWith(ctx, integration.LifecycleOutcome{
|
||||
_, err := a.finishWith(ctx, integration.LifecycleOutcome{
|
||||
InstanceID: in.InstanceID,
|
||||
Generation: in.Generation,
|
||||
Status: integration.LifecycleSucceeded,
|
||||
@@ -397,13 +397,7 @@ func (a *Activities) RecordSiteStatusActivity(ctx context.Context, in RecordSite
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if applied {
|
||||
a.setPlacementServability(ctx, in.InstanceID, in.Domain, in.Status)
|
||||
}
|
||||
return nil
|
||||
return err
|
||||
}
|
||||
|
||||
// RecordSiteDeletedInput is a permanent delete's local half.
|
||||
|
||||
@@ -209,11 +209,8 @@ type SyncSitesToDBOutput struct {
|
||||
// every pool's usage figure (usage.RecountAll), which corrects a figure that
|
||||
// went stale beside the observed-values writes.
|
||||
//
|
||||
// Each projection also reaches the domains registry (syncPlacement): placement
|
||||
// servability follows the status the row holds after the write — which is what
|
||||
// makes an out-of-band archive on the farm stop /domains/ask answering for the
|
||||
// name, and keeps a row the sync left alone servable as its status says — and
|
||||
// an operator placement is ensured for System-tenant sites sitting inside an
|
||||
// Each site write maintains existing placement servability through its trigger.
|
||||
// syncPlacement ensures an operator placement for System-tenant sites inside an
|
||||
// operator root and inside no deeper claim. Deleting a farm-absent site
|
||||
// releases its placement.
|
||||
func (a *Activities) SyncSitesToDBActivity(ctx context.Context, input SyncSitesToDBInput) (*SyncSitesToDBOutput, error) {
|
||||
@@ -374,11 +371,8 @@ func (p *syncPass) project(ctx context.Context, listed SiteInfo) {
|
||||
if !written {
|
||||
return
|
||||
}
|
||||
// Project the name into the registry alongside the row: servability
|
||||
// follows the status the row holds, wherever the site's placement lives,
|
||||
// and an operator placement is ensured only for names the console may
|
||||
// authoritatively own (see syncPlacement).
|
||||
if p.a.syncPlacement(ctx, site, p.input.DefaultWorkspaceID, IsServableStatus(site.Status)) {
|
||||
// Create a missing operator placement from the status read under its site lock.
|
||||
if p.a.syncPlacement(ctx, site, p.input.DefaultWorkspaceID) {
|
||||
p.outsideRoots++
|
||||
}
|
||||
if exists {
|
||||
@@ -482,12 +476,17 @@ func (p *syncPass) insert(ctx context.Context, entry SiteInfo, status string) (s
|
||||
}
|
||||
|
||||
// observe writes the entry's storage and last-modified time onto a row whose
|
||||
// status it shares, in autocommit: no count changes. The row it returns holds
|
||||
// status it shares, at READ COMMITTED because the site trigger refuses
|
||||
// transaction-snapshot isolation: no count changes. The returned row holds
|
||||
// the status the row has now.
|
||||
func (p *syncPass) observe(ctx context.Context, entry SiteInfo) (site fwmod.Site, written bool) {
|
||||
storage, lastMod := observedOf(entry)
|
||||
site, err := p.a.SiteQ.SetSiteObservedByDomain(ctx, fwmod.SetSiteObservedByDomainParams{
|
||||
Domain: entry.Name, StorageBytes: storage, LastModifiedAt: lastMod,
|
||||
err := db.InTx(ctx, p.a.Database, db.ReadCommitted, func(tx *sql.Tx) error {
|
||||
var err error
|
||||
site, err = fwmod.New(tx).SetSiteObservedByDomain(ctx, fwmod.SetSiteObservedByDomainParams{
|
||||
Domain: entry.Name, StorageBytes: storage, LastModifiedAt: lastMod,
|
||||
})
|
||||
return err
|
||||
})
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return fwmod.Site{}, false
|
||||
@@ -599,35 +598,25 @@ func (p *syncPass) releaseName(ctx context.Context, site fwmod.ListAllSitesRow)
|
||||
// and reports whether the name fell outside every operator root (the caller
|
||||
// counts those for a single end-of-run log line).
|
||||
//
|
||||
// Two distinct jobs, in one registry transaction:
|
||||
//
|
||||
// - Servability follows the resource. Whatever claim the site's placement
|
||||
// lives in — the member's own carved claim, their external claim, or an
|
||||
// operator root — an observed status crossing the archived boundary must
|
||||
// flip it. Addressing the placement by resource ref makes this a no-op
|
||||
// (zero rows) for a site that has none, which is exactly right.
|
||||
// - Placement CREATION is operator-scoped only (design D8, and the sync
|
||||
// requirement in the fedwiki-sites spec). The name must sit inside a live
|
||||
// operator root and inside no deeper live claim; a name inside a member or
|
||||
// external claim is the claim owner's to place, never the sync's, and a
|
||||
// name outside every root was never authorized by the console at all.
|
||||
// Creation is limited to System sites beneath an operator root with no deeper
|
||||
// claim. The site trigger maintains existing placements.
|
||||
//
|
||||
// The row's own workspace is checked as well: only System-tenant rows get an
|
||||
// operator placement here. A member's pre-registry site inside a root is the
|
||||
// boot reconciliation's to adopt into a member claim — placing it under the
|
||||
// operator root would silently transfer ownership of the member's name and
|
||||
// then make the backfill skip it as "already placed".
|
||||
func (a *Activities) syncPlacement(ctx context.Context, site fwmod.Site, systemWorkspaceID string, servable bool) (outsideRoots bool) {
|
||||
func (a *Activities) syncPlacement(ctx context.Context, site fwmod.Site, systemWorkspaceID string) (outsideRoots bool) {
|
||||
if a.Registry == nil || site.SiteID == "" {
|
||||
return false
|
||||
}
|
||||
err := a.Registry.WithLock(ctx, func(ctx context.Context, tx *domains.Tx) error {
|
||||
placed, err := tx.SetServableByResource(ctx, fwmod.ModuleName, site.SiteID, servable)
|
||||
placed, err := tx.PlacementByResource(ctx, fwmod.ModuleName, site.SiteID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if placed {
|
||||
return nil // already has a placement; servability is all sync owns
|
||||
if placed != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
claim, err := tx.DeepestLiveClaim(ctx, site.Domain)
|
||||
@@ -657,12 +646,19 @@ func (a *Activities) syncPlacement(ctx context.Context, site fwmod.Site, systemW
|
||||
return nil
|
||||
}
|
||||
|
||||
current, err := fwmod.New(tx.DBTX()).GetSiteForPlacement(ctx, site.SiteID)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return nil
|
||||
}
|
||||
if err != nil {
|
||||
return fmt.Errorf("read site for placement: %w", err)
|
||||
}
|
||||
if _, err := tx.Place(ctx, domains.PlaceParams{
|
||||
WorkspaceID: systemWorkspaceID,
|
||||
FQDN: site.Domain,
|
||||
Provider: fwmod.ModuleName,
|
||||
ResourceRef: site.SiteID,
|
||||
Servable: servable,
|
||||
Servable: IsServableStatus(current.Status),
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -15,9 +15,8 @@ import (
|
||||
// DomainAuthorizer answers the serving-authorization question behind
|
||||
// GET /domains/ask: does a servable placement exist at exactly this FQDN?
|
||||
// Implementations perform an exact-match lookup only — no ancestor or
|
||||
// wildcard reasoning (spec: domain-authorization). The FedWiki integration
|
||||
// supplies today's implementation; a future domains registry replaces it
|
||||
// behind this same interface without changing the HTTP contract.
|
||||
// wildcard reasoning (spec: domain-authorization). The domains registry
|
||||
// supplies the implementation without consulting a provider.
|
||||
type DomainAuthorizer interface {
|
||||
AuthorizeDomain(ctx context.Context, fqdn string) (bool, error)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
schema: spec-driven
|
||||
created: 2026-10-30
|
||||
@@ -0,0 +1,71 @@
|
||||
## Paths at HEAD
|
||||
|
||||
Baseline `45a78e88`. The retention purge's locking and archive-time rule are now in `openspec/specs/fedwiki-sites/spec.md`; their records are `openspec/changes/archive/2026-10-30-serialize-retention-purge-with-lifecycle/` and `openspec/changes/archive/2026-10-30-enforce-archive-time-in-database/`.
|
||||
|
||||
| Path | Current write or read |
|
||||
|---|---|
|
||||
| Create, import, sync | `CreateSite`, `InsertSiteFromFarm`, `UpsertSiteFromFarm` write status (`internal/integrations/fedwiki/store/queries/sites.sql:6`, `:94`, `:79`); callers are the create record and sync's insert/upsert steps. |
|
||||
| Status, swap, quota park/reactivate | All use `SetSiteStatusByDomain` (`sites.sql:106`). Status recording copies the flag after commit; swap and quota writes do not (`internal/integrations/fedwiki/workflows/request_activities.go:379`, `swap.go:165`, `reconcile.go:280`, `:319`). |
|
||||
| Keep active | Rejects only already-active targets; otherwise requests activation or swap. It writes neither status nor flag (`internal/integrations/fedwiki/web/site_actions.go:443`). An archived target can reach swap. |
|
||||
| Row deletion | Member delete calls `DeleteSite` after releasing the name (`workflows/activities.go:301`); retention calls it before release (`workflows/reconcile.go:172`); sync calls `DeleteSiteByDomain` before release (`workflows/sync.go:567`). |
|
||||
| Flag creation/update | `CreatePlacement` stores its input; `SetPlacementServableByResource` overwrites it (`internal/domains/queries/placements.sql:2`, `:53`). Reservation supplies true (`workflows/domains.go:69`); sync and boot supply an earlier status (`sync.go:660`, `internal/integrations/fedwiki/reconcile_domains.go:343`). Only the status helper and sync call the setter (`workflows/activities.go:332`, `sync.go:625`). |
|
||||
| Boot, metadata, migrations | Boot creates missing placements, leaving existing ones alone (`reconcile_domains.go:192`, `:269`). Metadata writes no status (`sites.sql:178`). Migration 1 defines status and the stored flag in their respective streams; FedWiki 00007 owns archive time. No migration repairs servability. |
|
||||
| Readers | Eight placement queries return the stored flag. The two consumers are `RegistryAuthorizer.AuthorizeDomain` (`internal/domains/authorizer.go:108`) and the operator list (`internal/server/operator_domains.go:385`). `DomainAskHandler` maps authorization to HTTP (`internal/server/domains.go:49`). Other placement readers use identity, claim or name. |
|
||||
|
||||
## One trigger owns the correction
|
||||
|
||||
Read-time derivation is dropped: it reverses the schema dependency required by invariant 12 in `docs/models/provider-integration.md:100`, `docs/database-management.md:61` and `docs/building-an-integration.md:752`. The domains-only lookup and its separate-module option (#254) remain intact. Copying in each application's transaction still lets a future status writer forget. A FedWiki trigger covers every writer while referencing domains in the allowed direction.
|
||||
|
||||
Add `fedwiki.set_site_placement_servability` and an AFTER INSERT OR UPDATE OR DELETE row trigger on `fedwiki.sites`, in a migration after 00007. Match `provider = 'fedwiki' AND resource_ref = site_id::text`, using NEW on insert/update and OLD on delete. Set only `servable`: active/readonly true, every other result false. Update all matching placements; no placement is a successful no-op. Fire on every UPDATE, including metadata and repeated status, so the next site write corrects an old wrong flag. Update only where the flag differs. The placement's existing timestamp trigger maintains `updated_at`.
|
||||
|
||||
The function uses the caller's rights, `LANGUAGE plpgsql`, default VOLATILE, `SET search_path = pg_catalog, pg_temp`, and qualified names, following FedWiki 00007 and the caller-rights trigger in `internal/db/migrations/00026_outgoing_default_rule.sql:48`. No exception is swallowed: a refused placement update aborts the site write and transaction. Return OLD on delete and NEW otherwise. No parsing, provider callback, SECURITY DEFINER, explicit registry lock or public callable serving function is added.
|
||||
|
||||
READ COMMITTED is necessary: a writer can begin its statement while a creator holds the site FOR SHARE, then wait while the creator inserts and commits a placement. Its trigger must see that new placement. VOLATILE supplies fresh statement snapshots at READ COMMITTED; a transaction snapshot at REPEATABLE READ can miss the inserted row even after the lock wait. The trigger therefore refuses REPEATABLE READ and SERIALIZABLE with SQLSTATE 0A000; PostgreSQL's READ UNCOMMITTED, which behaves as READ COMMITTED, is also safe. Existing transactional site writers already choose READ COMMITTED. Make sync's metadata-only `observe` explicit too; direct SQL writers must choose a supported isolation level. This small guard prevents a future writer from silently breaking the rule. See PostgreSQL's [snapshot rules](https://www.postgresql.org/docs/18/transaction-iso.html) and [VOLATILE functions](https://www.postgresql.org/docs/18/xfunc-volatility.html).
|
||||
|
||||
### Creation and lock order
|
||||
|
||||
Sync and boot classify the listed domain and workspace, which existing writers never change. After all skip checks, immediately before the first claim or placement mutation, read `GetSiteForPlacement` by immutable ID with FOR SHARE in the registry transaction. Use its status; missing means skip, and an error rolls back. Each creator locks exactly one site. FOR KEY SHARE permits a bare non-key UPDATE, although it conflicts with the explicit FOR UPDATE used by lifecycle writers. Tests cover both. A writer that wins first supplies the new status or no row; a creator that wins first holds the site through insertion, then the waiting writer's trigger updates that placement.
|
||||
|
||||
Expose the registry transaction through a narrow `domains.Tx.DBTX()` accessor returning the generated DBTX interface, without Commit/Rollback. FedWiki constructs its own queries on it; domains imports no provider. Existing `WithLock` keeps its isolation setting: at READ COMMITTED the locking read returns the current row after waiting; at REPEATABLE READ a changed row raises a serialization error and creates nothing. Neither can commit an old status. The trigger and this accessor require both schemas in one database. The separate-service/database option of #254 would need a replacement atomic protocol; a provider call committing the flag separately would reopen this gap.
|
||||
|
||||
| Path | Locks after this change | Placement then site in the same transaction? |
|
||||
|---|---|---|
|
||||
| Status record, quota writes, sync writes | Site → its placements → usage → request outcome where applicable; lifecycle instance first where already used | No |
|
||||
| Swap | Both site rows already locked, then their placement updates, then usage/outcomes (`swap.go:173`) | No |
|
||||
| Retention and sync deletion | Site → placements; retention has instance first; registry release is after commit | No |
|
||||
| Member deletion | Registry → placement during release; commit; separate site → placement transaction | No; the release lock is gone before the site write |
|
||||
| Boot/sync placement creation | Registry → exactly one site FOR SHARE, after skip checks and immediately before claim/placement mutations | No; preliminary lookups are ordinary SELECTs. An eligible creator can hold the registry lock while waiting for the purge's farm call (up to 30 seconds); skipped sites take no site lock. |
|
||||
| Create reservation and compensation | Registry → claim/placement; no site lock | No; site insertion is a later transaction |
|
||||
| Boot orphan cleanup, member/operator release, other registry mutations | Registry → placement/claim; no site lock (`reconcile_domains.go:123`, `internal/domains/registry.go:1188`) | No |
|
||||
|
||||
No current reverse edge exists. The trigger never waits for the registry advisory lock, and no site transaction acquires it after a site lock. Multi-site writers must lock all their sites before triggering placement updates, as swap already does. Record these edges in `docs/database-locks.md` rule 5, the site/placement row entries and Registry section. Flag-only updates are an explicit exception to registry serialization: they change no name, claim, ownership or allocation count.
|
||||
|
||||
### Privileges
|
||||
|
||||
The trigger needs schema USAGE, SELECT on `provider`, `resource_ref`, `servable`, and UPDATE on `servable` only. It does not assign `updated_at` or need INSERT, DELETE, TRIGGER or ownership on placements. These rights already reach `fedwiki_writer` through `domains_writer` (FedWiki 00003:89), and `console_runtime` through both writer memberships (FedWiki 00005:39; domains 00003:39). The writer-DML-only migrations (FedWiki 00006 and domains 00004) remove dangerous privileges without removing this UPDATE.
|
||||
|
||||
Add no redundant or broader grant. Retain the existing membership needed for allocation/release. A privilege test temporarily replaces the inherited table UPDATE with UPDATE(servable), proving that is sufficient; a denial test revokes UPDATE from `domains_writer`, the actual source, before trying the site write as each runtime role. Trigger installation uses the migration role; runtime needs no ability to install triggers. Readers and their grants remain unchanged.
|
||||
|
||||
## Cases, deployment and boundaries
|
||||
|
||||
| Case | Today with sync off | After |
|
||||
|---|---|---|
|
||||
| Status write or retry, including swap from archived | A completed request can leave a wrong flag forever | Status and flag commit together or both roll back; active/readonly stores true, archived false; a finished retry needs no correction |
|
||||
| Boot/sync places while status changes | An earlier status can seed the wrong flag | Site lock orders creation and status; rollback creates no claim/placement |
|
||||
| Existing wrong flag at deployment | Remains wrong | Migration leaves it wrong; the next site write corrects it |
|
||||
| Purge/sync deletes archived site; release pending or failed | Normally false, but an old wrong true can survive | Delete commits false; release failure leaves false |
|
||||
| Sync/raw SQL deletes active or readonly site; release fails | True can survive until restart | Delete commits false; this existing exposure is closed |
|
||||
| Member permanent delete | Release precedes row deletion | Same order; delete stores false on any remaining matching placement |
|
||||
| Create reserved, no row; failed create never inserted a row | Stored answer, usually true | Unchanged; no site DELETE occurs to fire the trigger |
|
||||
| Other provider, unrelated/malformed FedWiki ref, no placement | Stored answer, or registry miss | Unchanged; matching uses canonical text equality, as orphan detection does |
|
||||
| False hit, lookup error, absent placement | Local refusal, 500, or configured fallback respectively | Unchanged; certificate lookup involves no provider |
|
||||
|
||||
Up installs the function/trigger only, with no backfill, validation or data rewrite; it can deploy over wrong flags. A deployment fixes existing data by hand before depending on corrected answers. Writing each existing site after installation, even without changing its status, corrects its matching placement. Existing no-site orphans need manual cleanup or the existing boot sweep. Down drops only the trigger/function and requires the old application writes to be restored.
|
||||
|
||||
Remove the post-commit archive/restore copy, sync's corrective setter and the unused registry setters/query. Swap has no copy to remove. The Keep active action's guard stays: its archived target becomes servable when activated. Creators evaluate `IsServableStatus` on the locked row, naming active/readonly explicitly. Keep `TestSyncRestoresServabilityOnUnarchive`: sync still records the status that restores the flag through the trigger.
|
||||
|
||||
The record step's name-based site lookup and missing-row outcome stay for #284; the removed after-commit branch does not constrain that fix. Scheduling stays for #283. The retention purge still releases only after a successful commit, as its archived spec requires. Its comment at `reconcile.go:98` remains true for valid data; no rewrite is needed. Failed-create compensation without a site remains a separate orphan-cleanup issue, not covered by a delete trigger.
|
||||
|
||||
No user-visible strings or pages change; no hand captures are needed. The stored flag remains the answer for serving and `console_reader` (scripts/db-reader-role.sql). Historical migration comments remain unedited; the model card explains the flag-only exception to the registry's allocation write path.
|
||||
|
||||
The rule covers row INSERT, UPDATE and DELETE, with immutable site identifiers; not owner-only TRUNCATE or primary-key rewrites. Operators can count wrong flags with `SELECT count(*) FROM domains.placements p JOIN fedwiki.sites s ON p.provider = 'fedwiki' AND p.resource_ref = s.site_id::text WHERE p.servable <> (s.status IN ('active','readonly'))`. After deployment, a manual READ COMMITTED `UPDATE fedwiki.sites SET updated_at = updated_at WHERE site_id IN (…)` corrects selected rows; the migration does not.
|
||||
@@ -0,0 +1,28 @@
|
||||
## Why
|
||||
|
||||
Archive and restore commit a site's status before copying its certificate answer, best effort. Swap makes no copy. With sync disabled, a restored site can keep being refused a certificate and an archived site can keep being granted one (#247).
|
||||
|
||||
## What Changes
|
||||
|
||||
- A FedWiki-owned database trigger updates the site's placements in the same transaction as every site insert, update or delete: active and readonly serve; archived and deleted do not.
|
||||
- Sync and boot lock an existing site before reading its status and creating its placement. Remove the per-writer corrections and the setter API left unused.
|
||||
- Close the existing exposure where deleting a site and then failing to release its placement leaves that name authorizing certificates. A failed create that never inserted a site remains outside this rule.
|
||||
- Keep certificate and operator reads on the stored flag, with no provider lookup. Other providers and reservations without a site keep their answers.
|
||||
- Install no data rewrite. Existing wrong flags remain until a site write corrects them; a deployment corrects its data by hand before relying on the rule.
|
||||
|
||||
## Capabilities
|
||||
|
||||
### New Capabilities
|
||||
|
||||
None.
|
||||
|
||||
### Modified Capabilities
|
||||
|
||||
- `fedwiki-sites`: atomic status/servability writes, refusal after row deletion, and locked placement creation.
|
||||
- `domains-registry`: remove the unused servability setter from the allocation API; allow provider flag-only writes in their resource transaction without the registry advisory lock.
|
||||
|
||||
## Impact
|
||||
|
||||
One FedWiki migration after 00007, one locked site query, access to the registry transaction for that query, removal of redundant writes, tests, and updates to the lock guide and domains model card. The trigger needs no additional privileges beyond the existing DML grants. Site writers use READ COMMITTED so a trigger sees placements committed during a lock wait; the trigger rejects transaction-snapshot isolation.
|
||||
|
||||
No serving query, provider-neutral seam, page, UI string or workflow command changes. The retention purge's locking (#245) and the database's archive-time trigger (#246) are already archived and remain intact. #284 owns record-step lookup and missing-row outcomes; #283 owns scheduling.
|
||||
+47
@@ -0,0 +1,47 @@
|
||||
## MODIFIED Requirements
|
||||
|
||||
### Requirement: Allocation is exposed as a console-internal API with a structured verdict taxonomy
|
||||
|
||||
The registry SHALL expose a console-internal allocation API: availability checking,
|
||||
claim creation (carve and external), placement, and release. Placement creation SHALL accept the provider's initial servability. All
|
||||
hierarchical reasoning (ancestor/descendant walks, policy) SHALL live behind this API —
|
||||
never in the serving path. Every mutating allocation operation — claim creation,
|
||||
placement, release (including automatic release), and reconciliation writes — SHALL
|
||||
be serialized under the registry's transaction-scoped advisory lock, with placement
|
||||
counts for automatic release re-read under that lock. Provider-owned writes that
|
||||
change only a placement's servability MAY run in the provider resource's
|
||||
transaction without taking the registry advisory lock; they SHALL NOT change
|
||||
its name, claim, provider or resource reference. Serving reads SHALL remain
|
||||
provider-neutral and read the stored flag. Availability SHALL return a
|
||||
structured verdict from
|
||||
`{available, invalid, taken, reserved, blocked, premium}`. Member-facing surfaces SHALL
|
||||
collapse `reserved`, `blocked`, and `premium` (and namespace conflicts where revealing
|
||||
the holder would leak information) to a plain "unavailable" message; internal callers
|
||||
SHALL receive the structured verdict.
|
||||
|
||||
#### Scenario: Policy refusal does not leak its reason to members
|
||||
|
||||
- **WHEN** a member checks or requests a name refused by a `blocked` or `reserved` rule
|
||||
- **THEN** the member-facing response SHALL say the name is unavailable
|
||||
- **AND** SHALL NOT distinguish policy refusal from an existing-claim conflict
|
||||
|
||||
#### Scenario: Released placement frees serving immediately
|
||||
|
||||
- **WHEN** a placement is released
|
||||
- **THEN** the placement row SHALL be deleted and `/domains/ask` SHALL refuse the name
|
||||
on the next query
|
||||
|
||||
#### Scenario: Releasing the last placement auto-releases a carved claim
|
||||
|
||||
- **WHEN** the last placement inside a `member` claim is released
|
||||
- **THEN** the claim SHALL become `released` and its label SHALL be claimable again
|
||||
- **AND** an `external` claim in the same situation SHALL remain `active` until the
|
||||
member releases it explicitly
|
||||
|
||||
#### Scenario: Release racing a new placement cannot strand a live placement
|
||||
|
||||
- **WHEN** a release that would auto-release a `member` claim runs concurrently with
|
||||
a placement being added to the same claim
|
||||
- **THEN** the advisory-lock serialization SHALL order them so the claim is released
|
||||
only if the re-counted placement total is zero
|
||||
- **AND** no `released` claim SHALL ever own a live placement
|
||||
+288
@@ -0,0 +1,288 @@
|
||||
## MODIFIED Requirements
|
||||
|
||||
### Requirement: Site lifecycle drives placement servability
|
||||
|
||||
FedWiki SHALL maintain its placements' stored `servable` flag in the database
|
||||
transaction that inserts, updates or deletes the referenced site row. Active and
|
||||
readonly SHALL store true; archived SHALL store false; deletion SHALL store false
|
||||
on any placement still referencing that site. The rule SHALL apply to every row
|
||||
insert, update and delete with immutable site identifiers, including direct SQL, repeated status and metadata writes, with no
|
||||
per-writer correction or enabled sync required. A failed placement update SHALL
|
||||
abort the site write, and rollback SHALL restore both. Site writes SHALL use
|
||||
statement snapshots (READ COMMITTED, or PostgreSQL's equivalent READ UNCOMMITTED);
|
||||
the database SHALL refuse site writes under transaction-snapshot isolation.
|
||||
|
||||
Only placements with provider FedWiki and resource reference equal to the site's
|
||||
canonical identifier text SHALL be changed. Other providers and reservations
|
||||
whose site row has never existed SHALL keep their stored answers. The migration
|
||||
SHALL leave existing flags unchanged; the next site write SHALL correct its
|
||||
matching placements.
|
||||
|
||||
Permanent purge, including the 30-day retention sweep and the member's permanent
|
||||
delete action, SHALL release the placement and auto-release an emptied carved
|
||||
claim. A committed site deletion SHALL leave any placement awaiting release
|
||||
unservable, even if release fails. Certificate authorization SHALL continue to read
|
||||
only the stored placement flag, with no provider lookup.
|
||||
|
||||
#### Scenario: Archival stops serving
|
||||
|
||||
- **WHEN** a site is archived (member action or projection)
|
||||
- **THEN** its placement SHALL become `servable = false` and `/domains/ask` SHALL
|
||||
refuse the name
|
||||
|
||||
#### Scenario: Restore resumes serving
|
||||
|
||||
- **WHEN** an archived site is restored to active (quota-gated)
|
||||
- **THEN** its placement SHALL become `servable = true`
|
||||
|
||||
#### Scenario: A completed record needs no later flag write
|
||||
|
||||
- **WHEN** a status record commits and its worker stops before doing later work
|
||||
- **THEN** the placement flag SHALL already match the committed site status
|
||||
- **AND** retrying the finished request SHALL need no servability correction
|
||||
|
||||
#### Scenario: Swap activates an archived target
|
||||
|
||||
- **WHEN** a swap records active for a target that became archived after the page rendered
|
||||
- **THEN** the same transaction SHALL store true on its placement
|
||||
- **AND** the parked readonly site SHALL remain servable
|
||||
|
||||
#### Scenario: Deletion refuses while release waits or fails
|
||||
|
||||
- **WHEN** retention or sync commits deletion of a site and its placement release has not succeeded
|
||||
- **THEN** the retained placement SHALL have servability false and SHALL refuse certificates locally
|
||||
- **AND** a configured fallback SHALL NOT be consulted
|
||||
|
||||
#### Scenario: Placement failure rolls back the site write
|
||||
|
||||
- **WHEN** a site write cannot update its placement because UPDATE permission is denied
|
||||
- **THEN** the site write SHALL fail and neither change SHALL commit
|
||||
|
||||
#### Scenario: An old wrong flag is corrected by a site write
|
||||
|
||||
- **WHEN** the trigger is installed over an archived site's true placement flag
|
||||
- **THEN** installation SHALL leave that flag unchanged
|
||||
- **WHEN** the site is next updated, including a metadata-only update
|
||||
- **THEN** the placement flag SHALL become false in the same transaction
|
||||
|
||||
#### Scenario: Another provider and a never-created reservation keep their answers
|
||||
|
||||
- **WHEN** a FedWiki site is written while another provider has a placement with the same resource reference and a separate reservation has no site row
|
||||
- **THEN** neither unrelated placement's stored flag SHALL change
|
||||
|
||||
#### Scenario: A transaction snapshot cannot miss a newly created placement
|
||||
|
||||
- **WHEN** a writer attempts a site write at REPEATABLE READ or SERIALIZABLE
|
||||
- **THEN** the database SHALL refuse it without committing a site or placement change
|
||||
|
||||
### Requirement: Boot reconciliation seeds registry state for existing sites
|
||||
|
||||
FedWiki SHALL idempotently reconcile the registry at every boot — unconditionally,
|
||||
after migrations and provider registration, independent of Temporal configuration
|
||||
and of any sync-enablement flag: ensure `operator_root` claims for each
|
||||
configured farm domain; release orphaned placements — those whose `resource_ref`
|
||||
names no `fedwiki.sites` row and is not the instance identifier of a create
|
||||
lifecycle request that is still `accepted` — through the registry so an emptied
|
||||
carved claim auto-releases; then classify every placement-less `fedwiki.sites` row:
|
||||
System-workspace rows inside a root gain an operator placement; member-workspace
|
||||
rows inside a root gain a `member` claim (or join their containing claim) with a
|
||||
placement; **member-workspace rows at foreign domains only** gain an `external`
|
||||
claim born `active` (grandfathered — control was proven at creation under the
|
||||
previous flow) with a placement, processed shortest-name-first per workspace so a
|
||||
nested pre-registry site joins its already-created containing claim rather than
|
||||
minting an overlapping claim; a nested foreign row whose workspace differs from the
|
||||
containing claim's owner SHALL be left unplaced and logged. System-workspace rows at
|
||||
foreign domains SHALL remain unplaced (they are sync projections the console never
|
||||
authorized — re-placing them would silently grandfather unverified names).
|
||||
Adoption SHALL NOT apply name policy — policy governs member allocation, not
|
||||
sites that already exist. Boot and sync creation of a placement for an existing site SHALL read the site's
|
||||
current status by identifier under a site row lock conflicting with status
|
||||
updates and deletion, held in the placement's registry transaction through
|
||||
commit. After classification and skip checks, immediately before its first claim
|
||||
or placement mutation, the creator SHALL lock exactly one site and use its status,
|
||||
skip a missing row, and roll back on a failed read. A site status writer SHALL
|
||||
update the placement after any creator ahead of it commits. Existing placements
|
||||
SHALL NOT receive a separate status copy from boot or sync.
|
||||
Reconciliation SHALL be safe to run on every boot and is the backstop for
|
||||
compensation orphans.
|
||||
|
||||
#### Scenario: Orphaned placement is released
|
||||
|
||||
- **WHEN** boot reconciliation finds a `fedwiki` placement whose `resource_ref`
|
||||
matches no site row and no `accepted` create request (a crashed or uncompensated create)
|
||||
- **THEN** the placement SHALL be released and the name SHALL become claimable again
|
||||
- **AND** an emptied carved claim SHALL auto-release with it
|
||||
|
||||
#### Scenario: A running create keeps its reserved name across a restart
|
||||
|
||||
- **WHEN** the console restarts while a create workflow has reserved a placement whose `resource_ref` is the instance identifier of an `accepted` create request and has not yet inserted the site row
|
||||
- **THEN** boot reconciliation SHALL leave the placement in place
|
||||
- **AND** the name SHALL remain unavailable to any other workspace
|
||||
|
||||
#### Scenario: Adoption ignores name policy
|
||||
|
||||
- **WHEN** an existing site sits at a label that current name rules would refuse
|
||||
(for example a single-character label now treated as premium)
|
||||
- **THEN** reconciliation SHALL still adopt it with a claim and placement
|
||||
- **AND** its serving SHALL be unaffected
|
||||
|
||||
#### Scenario: Pre-registry custom-domain site is grandfathered
|
||||
|
||||
- **WHEN** boot reconciliation encounters a member's site at `wiki.example.org`
|
||||
with no placement
|
||||
- **THEN** an `active` external claim SHALL be ensured at `wiki.example.org` for the
|
||||
site's workspace with an apex placement, without re-verification
|
||||
|
||||
#### Scenario: Nested pre-registry sites share one claim
|
||||
|
||||
- **WHEN** the same workspace has placement-less sites at `example.org` and
|
||||
`blog.example.org`
|
||||
- **THEN** reconciliation SHALL create one external claim at `example.org` and place
|
||||
both sites inside it
|
||||
- **AND** no overlapping second claim SHALL be created
|
||||
|
||||
#### Scenario: System-workspace foreign rows stay unplaced
|
||||
|
||||
- **WHEN** boot reconciliation encounters a System-workspace site at a foreign
|
||||
domain (a sync projection)
|
||||
- **THEN** no claim or placement SHALL be created for it
|
||||
|
||||
#### Scenario: Reconciliation is idempotent
|
||||
|
||||
- **WHEN** boot reconciliation runs twice
|
||||
- **THEN** the second run SHALL create no duplicate claims or placements
|
||||
|
||||
#### Scenario: A creator cannot seed an earlier status
|
||||
|
||||
- **WHEN** a site is archived or restored after boot or sync listed it but before its placement is created
|
||||
- **THEN** creation SHALL use the status read under the site lock
|
||||
- **AND** a status writer arriving after that lock SHALL wait until creation commits, then update the new placement
|
||||
|
||||
#### Scenario: A listed site is deleted before placement creation
|
||||
|
||||
- **WHEN** the locked lookup finds the listed site's row deleted
|
||||
- **THEN** boot or sync SHALL create no placement or claim for it
|
||||
|
||||
### Requirement: Sync workflow does not delete user-created sites
|
||||
|
||||
The FedWiki sync workflow SHALL NOT delete sites that were created by users through the
|
||||
member-console. The sync SHALL **project** each site's observed state — a site present
|
||||
on the farm in a non-active state (`readonly` or `archived`) SHALL be retained with its
|
||||
local `status`, `storage_bytes`, and `last_modified_at` updated, NOT deleted. The
|
||||
delete path SHALL trigger only on genuine farm-absence (a site no longer present on the
|
||||
farm at all) and SHALL remain scoped to sites whose `workspace_id` matches **the System
|
||||
tenant's workspace**, resolved by natural key (see the `system-tenant` capability). New
|
||||
farm sites not yet in the database SHALL be projected into the System tenant's
|
||||
workspace; when the projected domain falls inside a live `operator_root` claim **and
|
||||
inside no deeper live claim**, the sync SHALL also ensure a registry placement for it
|
||||
(system workspace, directly inside the root); when the domain falls inside a live
|
||||
`member` or `external` claim, the sync SHALL NOT create any placement (it must never
|
||||
mis-own a member's name — only the claim's owner places there); when the domain falls
|
||||
outside every operator root, the site row SHALL be projected **without** a placement
|
||||
(the console never authorized the name — it stays unservable through `/domains/ask`
|
||||
until adopted; a configured ask fallback covers such names during migration).
|
||||
Deleting a sync-managed site SHALL also remove its placement.
|
||||
Placement creation SHALL follow the locked status read and missing-row rules in
|
||||
"Boot reconciliation seeds registry state for existing sites".
|
||||
The workflow SHALL resolve the System tenant's workspace at **execution time**, via an
|
||||
activity that queries by natural key (`org_type = 'system'`); workflow input (and any
|
||||
Temporal schedule args feeding it) SHALL NOT carry a workspace identifier for this
|
||||
purpose.
|
||||
|
||||
The sync SHALL NOT project a farm site whose domain equals the domain of a create
|
||||
lifecycle request that is still `accepted`: the create workflow owns that row's insert,
|
||||
under the request's instance identifier and the member's workspace. The sync SHALL
|
||||
project such a site only after the request has finished. Likewise the sync SHALL NOT
|
||||
write the status of a farm site whose `fedwiki.sites` row has a status change, swap or
|
||||
delete request still `accepted`, read again under the site row's lock: that request's own
|
||||
record step writes the site's status. A status the sync writes, a row it inserts and a
|
||||
row it deletes SHALL each commit in a transaction that recounts the pool. Before it
|
||||
writes the status of a site that has a lifecycle request updated at or after the moment
|
||||
the farm list was taken, by the database's clock, the sync SHALL read that domain's
|
||||
entry from the farm again and project the entry it reads; when that read fails, the sync
|
||||
SHALL leave the domain to its next pass. Under the site row's lock, immediately before it
|
||||
writes the status, the sync SHALL find no lifecycle request on the site updated at or
|
||||
after the moment the entry it projects was read (the moment the list was taken for a
|
||||
listed entry, the moment before the farm call for a re-read one), and SHALL otherwise
|
||||
leave the domain to its next pass.
|
||||
|
||||
#### Scenario: User-created site not on farm
|
||||
|
||||
- **WHEN** a sync runs and a user-created site (workspace_id belongs to a user's personal org) is not found on the farm
|
||||
- **THEN** the site is NOT deleted from the database
|
||||
|
||||
#### Scenario: Non-active farm site is projected, not deleted
|
||||
|
||||
- **WHEN** a sync runs and a site is present on the farm with `status = 'readonly'` or `status = 'archived'`
|
||||
- **THEN** the site SHALL be retained in the database
|
||||
- **AND** its local `status` (and `storage_bytes` / `last_modified_at`) SHALL be updated to the observed values
|
||||
|
||||
#### Scenario: New farm site under an operator root gains a placement
|
||||
|
||||
- **WHEN** a sync runs and a farm site at `tools.example.test` (inside the
|
||||
`example.test` operator root) has no corresponding row in the database
|
||||
- **THEN** the site SHALL be inserted under the System tenant's workspace resolved at
|
||||
execution time
|
||||
- **AND** a servability-bearing placement SHALL be ensured for it inside the root claim
|
||||
|
||||
#### Scenario: Farm site inside a member claim gets no operator placement
|
||||
|
||||
- **WHEN** a sync runs and a farm site's domain falls inside a live `member` or
|
||||
`external` claim
|
||||
- **THEN** the site row SHALL be projected but no placement SHALL be created
|
||||
- **AND** the name's servability SHALL remain governed solely by the claim owner's
|
||||
own placements
|
||||
|
||||
#### Scenario: Foreign-domain farm site is projected without a placement
|
||||
|
||||
- **WHEN** a sync runs and a farm site's domain falls outside every operator root
|
||||
- **THEN** the site row SHALL be projected into the System tenant's workspace
|
||||
- **AND** no placement SHALL be created, leaving `/domains/ask` refusing the name
|
||||
|
||||
#### Scenario: Genuinely absent System-tenant site removed from farm
|
||||
|
||||
- **WHEN** a sync runs and a site belonging to the System tenant's workspace is no longer present on the farm at all
|
||||
- **THEN** the site is deleted from the database
|
||||
- **AND** its placement, if any, SHALL be removed
|
||||
|
||||
#### Scenario: App database re-initialized while the Temporal schedule persists
|
||||
|
||||
- **WHEN** the application database is wiped and re-initialized (creating a new System tenant with a new workspace ID) while the Temporal sync schedule and its history remain from before the wipe
|
||||
- **THEN** the next scheduled sync run SHALL resolve the **current** System tenant workspace via the execution-time activity
|
||||
- **AND** the sync SHALL project farm sites into that current workspace successfully
|
||||
- **AND** the sync SHALL NOT fail with a foreign-key violation from a dangling workspace ID carried in schedule or workflow args
|
||||
|
||||
#### Scenario: A site whose create is still open is left to the create workflow
|
||||
|
||||
- **WHEN** a sync runs while the farm already holds `mysite.example.test` and a create request for `mysite.example.test` is still `accepted`, with no `fedwiki.sites` row yet
|
||||
- **THEN** the sync SHALL insert no row for `mysite.example.test`
|
||||
- **AND** when the create workflow then records the site, the row SHALL carry the request's instance identifier as its `site_id` and the member's workspace
|
||||
|
||||
#### Scenario: A swap's sites are left to the swap
|
||||
|
||||
- **WHEN** a pool's limit is 2 and its figure reads 2, a member's swap has parked one site and activated another on the farm, and a sync runs before the swap records its outcome
|
||||
- **THEN** the sync SHALL leave both sites' statuses and the figure as they were
|
||||
- **AND** after the swap succeeds the figure SHALL equal the pool's active row count
|
||||
- **AND** when the swap fails instead, the failed swap SHALL send the farm back to the statuses the sites had before it
|
||||
|
||||
#### Scenario: An archive that finishes after the list was taken is kept
|
||||
|
||||
- **WHEN** the sync has taken the farm list while a site was active, and a member's archive of that site then records its outcome before the sync reaches the site
|
||||
- **THEN** the sync SHALL read the site's entry from the farm again, find it archived, and leave the row `archived`
|
||||
- **AND** the pool's figure SHALL equal its active row count, with the archive's slot freed once
|
||||
|
||||
#### Scenario: A request that finishes between the pass's start and the row's lock is kept
|
||||
|
||||
- **WHEN** the sync holds a farm entry that shows a site active, a member's archive of that site records its outcome after the sync's pass began and before the sync takes the site row's lock, and the pass began with no request updated since the list
|
||||
- **THEN** the sync SHALL find the archive's request updated after the entry was read, SHALL write nothing for the site, and the row SHALL stay `archived`
|
||||
|
||||
#### Scenario: A restore that finishes after the list was taken is kept
|
||||
|
||||
- **WHEN** the sync has taken the farm list while a site was archived, and a member's restore of that site then records its outcome before the sync reaches the site
|
||||
- **THEN** the sync SHALL read the site's entry from the farm again, find it active, and leave the row `active`
|
||||
- **AND** the pool's figure SHALL equal its active row count
|
||||
|
||||
#### Scenario: A settled activation the farm made is counted once
|
||||
|
||||
- **WHEN** an activation's request was finished `failed` while the farm had made the site active, and a sync then projects the site's farm status
|
||||
- **THEN** the sync SHALL write the site `active` and recount the pool, and the site SHALL count once
|
||||
@@ -0,0 +1,74 @@
|
||||
Tests follow `docs/testing.md`; helpers have doc comments. No goldens or captures change.
|
||||
|
||||
The implementer completes sections 1–3 and prepares the breaks. The reviewer who lands the change runs `make test`, the mutation command and the commit. Write the break list to **a file outside the repository**, at the path supplied by the caller for implementation, using `scripts/mutation-check/main.go`'s format. Every record has a unique name, one exact replacement, its test and a failure-only `want_new`. Migration breaks use scratch tests; query breaks edit generated Go SQL with the query name in the match. Apply each break, run `go vet` for its package, restore it. The reviewer expects every record CAUGHT after green controls.
|
||||
|
||||
## 1. Trigger and placement creation
|
||||
|
||||
- [x] 1.1 Add the FedWiki migration after 00007, number settled at landing: `set_site_placement_servability`, AFTER INSERT OR UPDATE OR DELETE, caller rights, VOLATILE and fixed search path. Match provider plus canonical site ID text; update only differing `servable`, false on delete, active/readonly true otherwise. Reject transaction-snapshot isolation with SQLSTATE 0A000. No data rewrite, new privileges or swallowed errors. Both migration directions use goose statement blocks; Down drops the trigger/function.
|
||||
- [x] 1.2 Add `GetSiteForPlacement` to `store/queries/sites.sql`: SELECT the site by ID FOR SHARE. Expose the existing registry transaction through `domains.Tx.DBTX()`, returning only the DBTX query/exec interface, and add `Tx.PlacementByResource` using the existing query. Sync and boot must read the current site through that transaction after classification and skip checks, immediately before the first claim/placement mutation; lock exactly one site and use its status, skip a missing row, and propagate a failed read. Preserve existing-placement and ownership/collision guards. No new core-to-provider dependency.
|
||||
- [x] 1.3 Delete `setPlacementServability`, its after-commit caller, sync's setter call, both unused registry setters, their update query and dedicated setter test. Keep `IsServableStatus` for the locked creators, using explicit active/readonly membership. Keep status record/swap lookup and outcomes for #284 unchanged; the Keep active action's guard and workflow commands stay. Make sync `observe` use an explicit READ COMMITTED transaction, so a higher database default cannot reject metadata correction. Run `make sqlc-generate`, keeping only relevant changes.
|
||||
- [x] 1.4 Update `docs/database-locks.md` (site/placement rows, rule 5, Registry, sync and deletion paths, exactly one site per creator and the residual wait behind the purge farm call) for the design's lock edges and isolation requirement. Update `docs/models/domains-registry.md` at lines 15 and 30 for the flag-only exception and for trigger-owned correction; the stored column still answers for both runtime and column-level readers. Review comments in `activities.go`, `request_activities.go`, `swap.go`, `sync.go:212`, `:376`, `:598`, `reconcile_domains.go:98-100`, `:247`, `sites.sql:287`, `operator_domains.go:82`, `:191`, `reconcile.go:265-269`, and `server/domains.go:17`: remove claims about best-effort correction and distinguish deletion orphans from failed-create reservations. `reconcile.go:98` stays true and needs no change. Applied migration comments (domains 00001:123, FedWiki 00003:84) stay; the model card explains which Go-maintenance detail the trigger supersedes.
|
||||
- [x] 1.5 Run `./scripts/spdx-headers.sh <dir>` in each directory receiving new Go or SQL files. Keep test helpers documented. No source files are added during change-text revision.
|
||||
|
||||
## 2. Tests
|
||||
|
||||
- [x] 2.1 Add `store/placement_servability_db_test.go` in external package `fedwiki_test`. `TestSitePlacementTrigger_DB` runs the following rows against a fully migrated scratch database, as both `fedwiki_writer` and `console_runtime`. Seed `core.providers` rows for FedWiki and the test provider explicitly; schema tests use SQL for their subjects.
|
||||
|
||||
| Rows | Setup and expected result |
|
||||
|---|---|
|
||||
| `insert active`, `insert readonly`, `insert archived` | Reserve the supplied site ID first with the opposite flag; insert the site; want true, true, false |
|
||||
| `archive`, `restore active`, `restore readonly`, `repeat status`, `metadata correction` | Preseed the opposite flag; perform the named site write; want false, true, true, current status's answer, current status's answer |
|
||||
| `delete active`, `delete readonly`, `delete archived` | Leave a true placement, including a deliberately wrong archived one; DELETE site; want false and placement retained |
|
||||
| `other provider true`, `other provider false` | The true row uses the archived FedWiki site's UUID; the false row uses an opaque reference; a FedWiki archive/delete changes neither |
|
||||
| `other site`, `noncanonical reference`, `reservation without site` | An unrelated site ID, uppercase UUID text, and a never-inserted site's reservation keep their flags while another/matching canonical site is written |
|
||||
| `rollback archive`, `rollback delete` | Roll back the site operation; original row and flag remain |
|
||||
|
||||
Assert raw flag and real `GetPlacementByFQDN` agree; failure `%s: placement servable = %t, want %t`. Check row presence separately with `t.Errorf`. After `restore active`, also read `ListAllPlacementsForOperator`; failure `restored site is not servable in the operator list`. No change to those queries or their Go row types.
|
||||
|
||||
- [x] 2.2 `TestSitePlacementMigration_DB`: apply canonical sources with FedWiki through 7; seed archived/true and active/false; apply the new migration; assert raw flags unchanged, failure `migration rewrote placement data`. A metadata write then corrects each, failure `next site write left the wrong flag`. Down removes both trigger and function, failure `down left placement enforcement installed`.
|
||||
- [x] 2.3 `TestSitePlacementPrivileges_DB`, before Down, on scratch: in a rollback transaction revoke table UPDATE on placements FROM `domains_writer`; grant only UPDATE(servable) back to that role; writes under each runtime role must work (`column-only update grant did not suffice`). A separate transaction with that UPDATE revoked and no replacement column grant must reject a site write with 42501 (`placement denial did not abort the site write`), and leave both rows unchanged after rollback. Test the actual inherited source, not `console_runtime`. Use `t.Errorf` when the statement unexpectedly succeeds, then check SQLSTATE only when error is non-nil. Never stop before the wanted failure text.
|
||||
- [x] 2.4 `TestPlacementCreationLocks_DB` uses the real registry transaction and locked site query on scratch. Rows `creator then archive`, `creator then restore`, `creator then delete`: pause the creator after FOR SHARE, start the writer (FOR UPDATE then SetSiteStatusByDomain, as lifecycle writers do), observe its lock wait through `dbtest.AwaitLockWait`, insert the placement and commit, then join the writer. Want false, true, false; failure `%s: writer left wrong placement flag`. A writer that finishes before the creator commits fails `status writer passed the creator's site lock`. Reverse order in `archive then creator`, `restore then creator`, `delete then creator`: hold the site write uncommitted, require the creator's read to wait, then commit; expect false, true, no placement, respectively. Failure `creator did not wait for the site writer` or `%s: creator used stale site state`. Include rollback of creation and verify no placement survived (`rolled-back creation left a placement`). Add a creator-first bare SetSiteStatusByDomain UPDATE row, without the explicit FOR UPDATE, to catch FOR KEY SHARE: a real lock-first writer conflicts with either lock. Every AwaitLockWait uses Backend.Abort to print the stated failure text when the worker finishes early. Use channels and bounded contexts; release blockers before joining workers at cleanup.
|
||||
- [x] 2.5 `TestSitePlacementIsolation_DB`: direct site writes at READ COMMITTED and READ UNCOMMITTED succeed; REPEATABLE READ and SERIALIZABLE fail with 0A000 before changing either row, even if no placement exists (`transaction snapshot was allowed to write a site`). Reuse 2.4's creator-first test at READ COMMITTED to prove the trigger sees the placement committed during its wait. Add `TestSyncMetadataPlacementIsolation_DB` in workflows with a database handle whose default is REPEATABLE READ; the production `observe` must override it (`metadata correction used the database isolation default`).
|
||||
- [x] 2.6 Add `TestRecordPlacementServability_DB` in workflows using `reqEnv`, rows `archive`, `restore`, `swap archived target`, `swap readonly target`. Give affected placements the opposite flags, then set Activities.Registry nil. Run and repeat the real record step; assert statuses, succeeded outcomes, counted usage and both swap flags. **Every result check uses t.Errorf**, and servability is checked even after status/usage failures. Failures: `record left wrong status or outcome`, `record left wrong usage`, `record left servable = %t, want %t`. Assert archived_at is set exactly when the row is archived.
|
||||
- [x] 2.7 `TestSyncPlacementUsesLockedStatus_DB` (workflows) and `TestBootPlacementUsesLockedStatus_DB` (FedWiki module) each pass an earlier site value into the real placement creator after committing the opposite status, or deleting the row. Rows `archived after listing` → false, `restored after listing` → true, `readonly after listing` → true, `deleted after listing` → no placement. Failures `creator used stale site status` and `creator placed a deleted site`. Boot deletion rows cover both member hosted and foreign names and assert no live claim (`creator left a claim for a deleted site`). To fail the locked read itself, hold the row FOR UPDATE, start the creator, observe its SELECT lock wait, then cancel. Assert no placement and the surfaced site-read error (boot counts.failed and log; sync log), not a later cancelled placement error, with `failed site read created a placement or hid the error`. Never revoke shared grants. Retain raw-flag helpers, existing boot classification tests, `TestSyncProjectsPlacementsByClaimDepth`, and `TestSyncRestoresServabilityOnUnarchive` with its still-accurate name.
|
||||
- [x] 2.8 `TestDeletedSitePlacementRefuses_DB` in workflows: exercise `purgeArchivedSite` with release not yet run, and real farm-absent sync with release forced to fail. Rows `purged archived`, `sync archived`, `sync active`. Use `broken.Registry = domains.NewRegistry(nil)` for the release failure; the authorizer uses the real database. Seed archived placements true so the delete branch is necessary. After local commit require the site absent, placement present/false and the real registry authorizer to refuse without contacting a counting fallback server; failure `deleted site still authorizes certificates`. Keep existing member-delete release-before-row coverage. Failed creates with no site stay a separate case.
|
||||
- [x] 2.9 Retain `TestDomainAskHandler`, `TestRegistryAuthorizerLookupError`, `TestRegistryAuthorizerNeverForwards` and `TestOperatorDomainsPlacementsSectionListsWhatActuallyServes`. They pin the unchanged HTTP/render contract; 2.1/2.8 exercise its real database input. No new endpoint or projection tests are needed.
|
||||
|
||||
## 3. Deliberate breaks
|
||||
|
||||
The external list contains 29 records; each is applied, vetted against its package and restored before handoff. The list has not been run. Names below are unique; expand each explicitly named test/row into a separate record, suffixing its name with the row.
|
||||
|
||||
| Name and mutation | Test/row | `want_new` |
|
||||
|---|---|---|
|
||||
| Insert omitted from trigger events | `TestSitePlacementTrigger_DB/insert_active` | `placement servable = false, want true` |
|
||||
| Readonly mapped false | `TestSitePlacementTrigger_DB/insert_readonly` | `placement servable = false, want true` |
|
||||
| Archived mapped true | `TestSitePlacementTrigger_DB/archive` | `placement servable = true, want false` |
|
||||
| Active mapped false | `TestSitePlacementTrigger_DB/restore_active` | `placement servable = false, want true` |
|
||||
| Delete omitted from trigger events | `TestSitePlacementTrigger_DB/delete_archived` and `/delete_active` | `placement servable = true, want false` |
|
||||
| Only UPDATE OF status fires | `TestSitePlacementMigration_DB` | `next site write left the wrong flag` |
|
||||
| Provider predicate removed | `TestSitePlacementTrigger_DB/other_provider_true` | `placement servable = false, want true` |
|
||||
| Resource predicate removed | `TestSitePlacementTrigger_DB/other_site` (unrelated true, written site archived) | `placement servable = false, want true` |
|
||||
| Caller rights replaced with SECURITY DEFINER | `TestSitePlacementPrivileges_DB` | `placement denial did not abort the site write` |
|
||||
| Exception catches OTHERS and returns NULL | `TestSitePlacementPrivileges_DB` | `placement denial did not abort the site write` |
|
||||
| Trigger assigns updated_at explicitly too | `TestSitePlacementPrivileges_DB` | `column-only update grant did not suffice` |
|
||||
| Down replaced with valid no-op SQL | `TestSitePlacementMigration_DB` | `down left placement enforcement installed` |
|
||||
| Isolation guard removed | `TestSitePlacementIsolation_DB` | `transaction snapshot was allowed to write a site` |
|
||||
| FOR SHARE changed to FOR KEY SHARE in generated site query | `TestPlacementCreationLocks_DB/creator_then_bare_archive` | `status writer passed the creator's site lock` |
|
||||
| FOR SHARE removed from generated site query | `TestPlacementCreationLocks_DB/archive_then_creator` | `creator did not wait for the site writer` |
|
||||
| Status SQL retains archived on activation, preserving parameters | `TestRecordPlacementServability_DB/restore` and `/swap_archived_target` | `record left servable = false, want true` |
|
||||
| Sync uses the passed site's old status after its locked read | `TestSyncPlacementUsesLockedStatus_DB/archived_after_listing` and `/restored_after_listing` | `creator used stale site status` |
|
||||
| Boot uses the passed site's old status after its locked read | `TestBootPlacementUsesLockedStatus_DB/archived_after_listing` and `/restored_after_listing` | `creator used stale site status` |
|
||||
| IsServableStatus excludes readonly | `TestBootPlacementUsesLockedStatus_DB/readonly_after_listing` | `creator used stale site status` |
|
||||
| Boot locked read moved after claim mutation | `TestBootPlacementUsesLockedStatus_DB/deleted_hosted_after_listing` and `/deleted_foreign_after_listing` | `creator left a claim for a deleted site` |
|
||||
| Locked-read failure falls back to the passed site | both creator tests, `failed_read`; require the logged error to identify the site read, not a later cancelled placement write | `failed site read created a placement or hid the error` |
|
||||
| Metadata transaction uses db.Plain | `TestSyncMetadataPlacementIsolation_DB` | `metadata correction used the database isolation default` |
|
||||
| Delete omitted from trigger events, shared-database command record last | `TestDeletedSitePlacementRefuses_DB`; reset test databases before the test | `deleted site still authorizes certificates` |
|
||||
|
||||
|
||||
Rollback, migration no-rewrite, idempotent repeats, unrelated false/noncanonical/reservation fixtures and existing entry-path contracts are controls. No manufactured database failure seam or break of PostgreSQL atomicity is required. The stale-derived-query breaks from the former design are removed; these replacement breaks oppose each row's actual expected flag.
|
||||
|
||||
## 4. Validation and landing
|
||||
|
||||
- [x] 4.1 Implementer: reset only this worktree's test databases after the migration; run store, domains, workflows and FedWiki module tests separately twice, plus server's existing domain tests. Run concurrency tests with `-race`. Prepare and vet the external break records. Do not run the full suite or mutation command.
|
||||
- [x] 4.2 `openspec validate keep-placement-servability-equal-to-site-status --strict` and `make lint` pass, including `notebook-citations: ok`.
|
||||
- [x] 4.3 Reviewer who lands the change: run `make test` with the stack up, then `go run ./scripts/mutation-check -list <file> -side new`. After that command restores the SQL, run `test/reset-test-db.sh` regardless of its exit status: the final command record leaves the shared databases with the broken schema. Record results and control limits in the commit. Capture by hand only if a page changes. Keep this change open until archiving is requested; #245 and #246 are already archived under `2026-10-30-serialize-retention-purge-with-lifecycle` and `2026-10-30-enforce-archive-time-in-database`.
|
||||
Reference in New Issue
Block a user