console: bind the legacy retire to its read; held location on any abort
Task 16 review, round 2: - P2-A: with lifetimes off, retireLegacyInstanceRecord deletes by release.DeleteIfMatch on the etag of the read the fleet seed hold was asked of. A record written in between answers ErrCASConflict, which the detach doors answer "try again"; an absent key is done. The domain-attach test store forwards conditional deletes. - P2-B: copy-in-storage lands on any aborted fleet file seed. It records Held and keeps the reason the seed has (FS-30 only over none); every other code on an aborted seed is 409. The delete door's FS-34 keys on Held, not on the reason text. Both orders tested. - P3: the claim-console retire branch notes why it may skip the hold.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
8 files changed,
+233
−79
internal/console/apifleetseeds.go+30 −15internal/console/apifleetseeds_test.go+63 −17internal/console/domainattach_test.go+8 −0internal/console/hostclaimops.go+40 −30internal/console/seedfile.go+11 −12internal/console/seedfilefleet_test.go+2 −0internal/console/seedholdreview_test.go+70 −0internal/provision/fleet_seeded_create_3_census_test.go+9 −5
CI cancelled — superseded by a newer push — run details
diff --git a/internal/console/apifleetseeds.go b/internal/console/apifleetseeds.go| index 3fc4f157c..23b3ff52d 100644 |
| --- a/internal/console/apifleetseeds.go |
| +++ b/internal/console/apifleetseeds.go |
| @@ -113,16 +113,29 @@ func fleetSeedReason(code string) (string, bool) { |
| return "", false |
| } |
| -// failRefreshesAborted is the one failed report an already-aborted fleet |
| -// file seed takes (decision D6, review P3-1): copy-in-storage, from the |
| -// rollback that found a copy, refreshing FS-30 and where it is held. It |
| -// lands only over no reason (a member's or the sweep's abort, which says |
| -// nothing) or over FS-30 itself, so it never overwrites a reason the |
| -// console set (FS-17, an upload's failure) or the box set before, and no |
| -// other code ever clears FS-30. Anything else is the record saying |
| -// otherwise: 409, nothing written. |
| -func failRefreshesAborted(seed *provision.SeedSource, code string) bool { |
| - return code == provision.SeedCodeCopyInStorage && (seed.Reason == "" || seed.Reason == fileSeedCopyInStorageCopy) |
| +// failedReason is the reason a failed report leaves on seed, and ok false |
| +// for a report the record refuses (409, nothing written). On a seed that is |
| +// not yet aborted, the code's own sentence (FS-27 to FS-30). On an |
| +// already-aborted one only copy-in-storage lands (decision D6; review |
| +// rounds 1 and 2), from the rollback that found a copy, whatever the |
| +// abort's reason: it records where the copy is held, which the delete |
| +// door's sentence keys on, and it keeps the reason the seed already has |
| +// (a console's FS-17 or an upload's failure, a box's earlier code), |
| +// setting FS-30 only over none. So a later report never overwrites why |
| +// the seed was abandoned, and never loses where its copy is. Any other |
| +// code on an aborted seed is refused, so nothing clears Held or FS-30. |
| +func failedReason(seed *provision.SeedSource, code string) (string, bool) { |
| + reason, _ := fleetSeedReason(code) |
| + if seed == nil || seed.State != seeds.StateAborted { |
| + return reason, true |
| + } |
| + if code != provision.SeedCodeCopyInStorage { |
| + return "", false |
| + } |
| + if seed.Reason != "" { |
| + return seed.Reason, true |
| + } |
| + return reason, true |
| } |
| // fleetSeedReportValid is the body's grammar for op (plan Task 15): a seed |
| @@ -250,16 +263,18 @@ func (s *Server) fleetSeedTransition(ctx context.Context, rec *provision.Record, |
| } |
| mut = provision.SeedPlace(b.ID) |
| case provision.FleetSeedFailed: |
| - reason, _ := fleetSeedReason(b.Code) |
| - if seed.State == seeds.StateAborted && seed.Reason == reason && seed.Held == b.Held { |
| + // A report the record already says (the same code again, or a held |
| + // copy re-reported every pass) is a no-op. |
| + if mapped, _ := fleetSeedReason(b.Code); seed.State == seeds.StateAborted && seed.Held == b.Held && |
| + (seed.Reason == mapped || b.Code == provision.SeedCodeCopyInStorage && seed.Reason != "") { |
| return false, nil |
| } |
| - fail := provision.SeedFail(b.ID, reason, b.Held) |
| mut = func(cur *provision.Record) error { |
| - if cur.Seed != nil && cur.Seed.State == seeds.StateAborted && !failRefreshesAborted(cur.Seed, b.Code) { |
| + reason, ok := failedReason(cur.Seed, b.Code) |
| + if !ok { |
| return provision.ErrSeedConflict |
| } |
| - return fail(cur) |
| + return provision.SeedFail(b.ID, reason, b.Held)(cur) |
| } |
| case provision.FleetSeedRolledBack: |
| if seed.State == seeds.StateRolledBack { |
diff --git a/internal/console/apifleetseeds_test.go b/internal/console/apifleetseeds_test.go| index 98bd8f5ef..f663bc3e8 100644 |
| --- a/internal/console/apifleetseeds_test.go |
| +++ b/internal/console/apifleetseeds_test.go |
| @@ -5,6 +5,7 @@ import ( |
| "context" |
| "encoding/json" |
| "errors" |
| + "fmt" |
| "io" |
| "net/http" |
| "os" |
| @@ -540,9 +541,10 @@ func TestFleetSeedDoorRechecksOwnershipUnderLock(t *testing.T) { |
| } |
| } |
| -// P3-1: an aborted seed takes only copy-in-storage, and only over no |
| -// reason or FS-30; nothing else is written over it. Another code is the |
| -// record saying otherwise: 409. |
| +// P3-1 and round 2's P2-B: an aborted seed takes only copy-in-storage, |
| +// over any reason. It records where the copy is held and keeps the reason |
| +// the seed has (FS-30 only over none). Any other code is 409, nothing |
| +// written, so nothing clears Held or FS-30. |
| func TestFleetSeedDoorFailedOnAbortedOnlyRefreshesStorage(t *testing.T) { |
| f := newFleetSeedFx(t, false) |
| n := 0 |
| @@ -563,19 +565,22 @@ func TestFleetSeedDoorFailedOnAbortedOnlyRefreshesStorage(t *testing.T) { |
| return func(id string) fsBody { return fsBody{"id": id, "code": c} } |
| } |
| for _, c := range []struct { |
| - what, reason, held string |
| - body func(id string) fsBody |
| - code int |
| - wantReason string |
| - unchanged bool |
| + what, reason, held string |
| + body func(id string) fsBody |
| + code int |
| + wantReason, wantHeld string |
| + unchanged bool |
| }{ |
| - {"copy-in-storage over a member's abort", "", "", storage(provision.SeedHeldBoth), http.StatusOK, fileSeedCopyInStorageCopy, false}, |
| - {"copy-in-storage refreshes FS-30", fileSeedCopyInStorageCopy, provision.SeedHeldReplica, storage(provision.SeedHeldStaging), http.StatusOK, fileSeedCopyInStorageCopy, false}, |
| - {"copy-in-storage over FS-17", fileSeedMismatchCopy, "", storage(provision.SeedHeldBoth), http.StatusConflict, fileSeedMismatchCopy, true}, |
| - {"copy-in-storage over a box's FS-29", fileSeedCopyExistsCopy, "", storage(provision.SeedHeldBoth), http.StatusConflict, fileSeedCopyExistsCopy, true}, |
| - {"mismatch over FS-30", fileSeedCopyInStorageCopy, provision.SeedHeldReplica, code(provision.SeedCodeMismatch), http.StatusConflict, fileSeedCopyInStorageCopy, true}, |
| - {"copy-exists over a member's abort", "", "", code(provision.SeedCodeCopyExists), http.StatusConflict, "", true}, |
| - {"the same mismatch again", fileSeedBoxMismatchCopy, "", code(provision.SeedCodeMismatch), http.StatusOK, fileSeedBoxMismatchCopy, true}, |
| + {"copy-in-storage over a member's abort", "", "", storage(provision.SeedHeldBoth), http.StatusOK, fileSeedCopyInStorageCopy, provision.SeedHeldBoth, false}, |
| + {"copy-in-storage refreshes FS-30", fileSeedCopyInStorageCopy, provision.SeedHeldReplica, storage(provision.SeedHeldStaging), http.StatusOK, fileSeedCopyInStorageCopy, provision.SeedHeldStaging, false}, |
| + {"copy-in-storage over FS-17", fileSeedMismatchCopy, "", storage(provision.SeedHeldBoth), http.StatusOK, fileSeedMismatchCopy, provision.SeedHeldBoth, false}, |
| + {"copy-in-storage over an upload failure", importNotADB, "", storage(provision.SeedHeldReplica), http.StatusOK, importNotADB, provision.SeedHeldReplica, false}, |
| + {"copy-in-storage over a box's FS-29", fileSeedCopyExistsCopy, "", storage(provision.SeedHeldBoth), http.StatusOK, fileSeedCopyExistsCopy, provision.SeedHeldBoth, false}, |
| + {"the same copy-in-storage again over FS-17", fileSeedMismatchCopy, provision.SeedHeldBoth, storage(provision.SeedHeldBoth), http.StatusOK, fileSeedMismatchCopy, provision.SeedHeldBoth, true}, |
| + {"mismatch over FS-30", fileSeedCopyInStorageCopy, provision.SeedHeldReplica, code(provision.SeedCodeMismatch), http.StatusConflict, fileSeedCopyInStorageCopy, provision.SeedHeldReplica, true}, |
| + {"copy-exists over a member's abort", "", "", code(provision.SeedCodeCopyExists), http.StatusConflict, "", "", true}, |
| + {"mismatch over FS-17 with a held copy", fileSeedMismatchCopy, provision.SeedHeldReplica, code(provision.SeedCodeMismatch), http.StatusConflict, fileSeedMismatchCopy, provision.SeedHeldReplica, true}, |
| + {"the same mismatch again", fileSeedBoxMismatchCopy, "", code(provision.SeedCodeMismatch), http.StatusOK, fileSeedBoxMismatchCopy, "", true}, |
| } { |
| host, id := aborted(t, c.reason, c.held) |
| before := f.raw(t, host) |
| @@ -585,8 +590,8 @@ func TestFleetSeedDoorFailedOnAbortedOnlyRefreshesStorage(t *testing.T) { |
| continue |
| } |
| rec := f.record(t, host) |
| - if rec.Seed.State != seeds.StateAborted || rec.Seed.Reason != c.wantReason { |
| - t.Errorf("%s: seed %+v, want aborted with %q", c.what, rec.Seed, c.wantReason) |
| + if rec.Seed.State != seeds.StateAborted || rec.Seed.Reason != c.wantReason || rec.Seed.Held != c.wantHeld { |
| + t.Errorf("%s: seed %+v, want aborted with %q held %q", c.what, rec.Seed, c.wantReason, c.wantHeld) |
| } |
| if c.unchanged && !bytes.Equal(before, f.raw(t, host)) { |
| t.Errorf("%s: the record changed", c.what) |
| @@ -594,6 +599,47 @@ func TestFleetSeedDoorFailedOnAbortedOnlyRefreshesStorage(t *testing.T) { |
| } |
| } |
| +// Round 2 P2-B, both orders: wherever the console's reason and the box's |
| +// held copy come from first, the delete door names the location. |
| +func TestFleetSeedHeldLocationSurvivesEitherOrder(t *testing.T) { |
| + f := newFleetSeedFx(t, false) |
| + ctx := context.Background() |
| + where := fmt.Sprintf(fileSeedHeldInStorageCopy, "%s", fmt.Sprintf(fileSeedHeldReplicaCopy, "%s")) |
| + // The console aborts first (FS-17), then the box's rollback finds a copy. |
| + first := "first.shop.example.com" |
| + id := f.seed(t, first, "fleet/home", "", seeds.KindFile, seeds.StateReserving) |
| + if _, err := provision.SetRecordSeed(ctx, f.fx.rel, f.fx.acct, "shop", first, provision.SeedAbortFileUploadBecause(id, fileSeedMismatchCopy)); err != nil { |
| + t.Fatal(err) |
| + } |
| + if code, raw := f.post(t, first, "failed", "home/b1", f.b1, fsBody{"id": id, "code": provision.SeedCodeCopyInStorage, "held": provision.SeedHeldReplica}); code != http.StatusOK { |
| + t.Fatalf("copy-in-storage after FS-17: %d %s", code, raw) |
| + } |
| + rec := f.record(t, first) |
| + if rec.Seed.Reason != fileSeedMismatchCopy || rec.Seed.Held != provision.SeedHeldReplica { |
| + t.Fatalf("after FS-17 then the copy: %+v", rec.Seed) |
| + } |
| + if got := fleetSeedHeldSentence(first, rec.Seed); got != fmt.Sprintf(where, first, first) { |
| + t.Fatalf("FS-17 then the copy: %q", got) |
| + } |
| + // The box reports the copy first, from ready; a console write after it |
| + // (a member's delete) aborts nothing more and changes neither. |
| + second := "second.shop.example.com" |
| + id = f.seed(t, second, "fleet/home", "", seeds.KindFile, seeds.StateReady) |
| + if code, raw := f.post(t, second, "failed", "home/b1", f.b1, fsBody{"id": id, "code": provision.SeedCodeCopyInStorage, "held": provision.SeedHeldReplica}); code != http.StatusOK { |
| + t.Fatalf("copy-in-storage from ready: %d %s", code, raw) |
| + } |
| + if _, err := provision.SetRecordSeed(ctx, f.fx.rel, f.fx.acct, "shop", second, provision.SeedAbortBecause(id, "")); err == nil { |
| + t.Fatal("a second abort of an aborted seed landed") |
| + } |
| + rec = f.record(t, second) |
| + if rec.Seed.Reason != fileSeedCopyInStorageCopy || rec.Seed.Held != provision.SeedHeldReplica { |
| + t.Fatalf("after the copy then a console abort: %+v", rec.Seed) |
| + } |
| + if got := fleetSeedHeldSentence(second, rec.Seed); got != fmt.Sprintf(where, second, second) { |
| + t.Fatalf("the copy then a console abort: %q", got) |
| + } |
| +} |
| + |
| // P3-4: a fleet file seed's record with a replica assignment is not the |
| // door's to own: the identical 404, nothing changed. |
| func TestFleetSeedDoorRefusesReplicaAssignment(t *testing.T) { |
diff --git a/internal/console/domainattach_test.go b/internal/console/domainattach_test.go| index 6fa64643c..572989958 100644 |
| --- a/internal/console/domainattach_test.go |
| +++ b/internal/console/domainattach_test.go |
| @@ -108,6 +108,14 @@ func (o *opStore) Delete(ctx context.Context, key string) error { |
| return o.Store.Delete(ctx, key) |
| } |
| +// DeleteIfMatch logs a conditional delete as a delete and passes it to the |
| +// store below: the legacy retire binds its delete to the read it asked the |
| +// fleet seed hold of (hostclaimops.go). |
| +func (o *opStore) DeleteIfMatch(ctx context.Context, key, etag string) error { |
| + o.ops = append(o.ops, "delete "+key) |
| + return release.DeleteIfMatch(ctx, o.Store, key, etag) |
| +} |
| + |
| // opIndex is where an op appears in the log, or -1. |
| func (o *opStore) opIndex(op string) int { |
| for i, got := range o.ops { |
diff --git a/internal/console/hostclaimops.go b/internal/console/hostclaimops.go| index ac47332a0..9f2940939 100644 |
| --- a/internal/console/hostclaimops.go |
| +++ b/internal/console/hostclaimops.go |
| @@ -11,6 +11,7 @@ import ( |
| "github.com/carlosframework/platform/internal/hostclaim" |
| "github.com/carlosframework/platform/internal/hostops" |
| "github.com/carlosframework/platform/internal/provision" |
| + "github.com/carlosframework/platform/internal/release" |
| ) |
| func (s *Server) retireHostRecord(ctx context.Context, ref provision.RetireRef) error { |
| @@ -21,6 +22,12 @@ func (s *Server) retireHostRecord(ctx context.Context, ref provision.RetireRef) |
| // deletes a record does the same. |
| s.noteRecordDeletion(ref.Account, ref.App, time.Now()) |
| if s.cfg.HostClaim { |
| + // The claim chain does not ask the fleet seed hold (fleetSeedHold). |
| + // That is safe only because a claim console never admits a seed: |
| + // every seed door refuses it (seedClaimConsoleRefusal, and |
| + // seedFleetReady asks first), so no fleet file seed's record is |
| + // ever retired here. A claim console that admits seeds must add |
| + // the hold to this branch first. |
| return s.retireClaimHostRecord(ctx, ref) |
| } |
| if ref.Family == "instance" && lifetimesUnit(ctx) { |
| @@ -32,45 +39,48 @@ func (s *Server) retireHostRecord(ctx context.Context, ref provision.RetireRef) |
| if s.cfg.HostClaimOps != nil { |
| claims = s.cfg.HostClaimOps |
| } |
| - opt := provision.RetireOptions{HostClaim: s.cfg.HostClaim, Claims: claims} |
| if ref.Family == "instance" { |
| - return s.retireLegacyInstanceRecord(ctx, ref, opt) |
| + return s.retireLegacyInstanceRecord(ctx, ref) |
| } |
| - return provision.RetireRecord(ctx, s.cfg.Releases, ref, opt) |
| + return provision.RetireRecord(ctx, s.cfg.Releases, ref, provision.RetireOptions{HostClaim: s.cfg.HostClaim, Claims: claims}) |
| } |
| // retireLegacyInstanceRecord is a claims-off instance retirement with |
| // lifetimes off: the fleet seed hold (fleetSeedHold, Task 16 review P1-1) |
| -// asked of the record as it reads now, then the legacy delete exactly as |
| -// before (provision.RetireRecord). A record gone already, or a body that |
| -// will not decode, is no seed, and the delete runs as it always has. |
| -// |
| -// The delete stays unconditional, as the legacy callers (detach, attach |
| -// rollback, a create's undo) and their stores expect, so a fleet file seed |
| -// written at the host between this read and that delete is not seen here. |
| -// That window is lifetimes-off only and needs a seed create racing a |
| -// detach of the same host; with lifetimes on the hold is asked under the |
| -// app and hostname locks every writer of the host takes |
| -// (retireRecordWithLifetimeLocked). |
| -func (s *Server) retireLegacyInstanceRecord(ctx context.Context, ref provision.RetireRef, opt provision.RetireOptions) error { |
| - raw, _, err := s.cfg.Releases.Get(ctx, provision.RecordKey(ref.Account, ref.App, ref.Host)) |
| - switch { |
| - case err == nil: |
| - // Only the fields the hold reads: the record's own decode is the |
| - // readers', and this is no copy of it. |
| - var body struct { |
| - Region string `json:"region"` |
| - Seed *provision.SeedSource `json:"seed"` |
| - } |
| - if json.Unmarshal(raw, &body) == nil { |
| - if err := fleetSeedHoldOf(ref.Host, body.Region, body.Seed); err != nil { |
| - return err |
| - } |
| +// asked of the record as it reads now, and the delete bound to that read |
| +// (review round 2, P2-A). With lifetimes off no lock orders it against a |
| +// create (the create door holds only the account lock, and a detach none), |
| +// so an unconditional delete could remove a fleet file seed written at the |
| +// host after the hold was asked. release.DeleteIfMatch on the read's etag |
| +// deletes only the body the hold saw: a record written since answers |
| +// release.ErrCASConflict, which every caller's failure branch answers as |
| +// "try again", and the next try asks the hold of it. An absent key is |
| +// done, as lifetimes on answers it. A body that will not decode is no seed |
| +// and is deleted as before, bound the same way. |
| +func (s *Server) retireLegacyInstanceRecord(ctx context.Context, ref provision.RetireRef) error { |
| + key := provision.RecordKey(ref.Account, ref.App, ref.Host) |
| + raw, etag, err := s.cfg.Releases.Get(ctx, key) |
| + if errors.Is(err, os.ErrNotExist) { |
| + return nil |
| + } |
| + if err != nil { |
| + return err |
| + } |
| + // Only the fields the hold reads: the record's own decode is the |
| + // readers', and this is no copy of it. |
| + var body struct { |
| + Region string `json:"region"` |
| + Seed *provision.SeedSource `json:"seed"` |
| + } |
| + if json.Unmarshal(raw, &body) == nil { |
| + if err := fleetSeedHoldOf(ref.Host, body.Region, body.Seed); err != nil { |
| + return err |
| } |
| - case !errors.Is(err, os.ErrNotExist): |
| + } |
| + if err := release.DeleteIfMatch(ctx, s.cfg.Releases, key, etag); err != nil && !errors.Is(err, os.ErrNotExist) { |
| return err |
| } |
| - return provision.RetireRecord(ctx, s.cfg.Releases, ref, opt) |
| + return nil |
| } |
| func (s *Server) adoptHostRecord(ctx context.Context, key string) (hostclaim.Observed, error) { |
| if !s.cfg.HostClaim || s.cfg.HostClaimOps == nil { |
diff --git a/internal/console/seedfile.go b/internal/console/seedfile.go| index 4fb7116c3..4f97dbdad 100644 |
| --- a/internal/console/seedfile.go |
| +++ b/internal/console/seedfile.go |
| @@ -571,23 +571,22 @@ func (s *Server) seedFleetReady(ctx context.Context, fleet string) *doorRefusal |
| } |
| // fleetSeedHeldSentence is the delete door's 409 for an aborted fleet file |
| -// seed (plan R1-5). FS-30 with a held place gets FS-34, whose location the |
| +// seed (plan R1-5). A seed with a held place gets FS-34, whose location the |
| // console builds from the host and the held enum alone (decision D12; |
| -// FS-36, FS-37, FS-38): a box never supplies text a member reads. Every |
| -// other reason, and FS-30 with no held (none is expected), gets FS-33. |
| +// FS-36, FS-37, FS-38): a box never supplies text a member reads. It keys |
| +// on Held, not on the reason, because a held copy is recorded whatever the |
| +// abort's reason (review round 2). Every other gets FS-33. |
| func fleetSeedHeldSentence(host string, seed *provision.SeedSource) string { |
| replica := fmt.Sprintf(fileSeedHeldReplicaCopy, host) |
| staging := fmt.Sprintf(fileSeedHeldStagingCopy, host) |
| where := "" |
| - if seed.Reason == fileSeedCopyInStorageCopy { |
| - switch seed.Held { |
| - case provision.SeedHeldReplica: |
| - where = replica |
| - case provision.SeedHeldStaging: |
| - where = staging |
| - case provision.SeedHeldBoth: |
| - where = fmt.Sprintf(fileSeedHeldBothCopy, replica, staging) |
| - } |
| + switch seed.Held { |
| + case provision.SeedHeldReplica: |
| + where = replica |
| + case provision.SeedHeldStaging: |
| + where = staging |
| + case provision.SeedHeldBoth: |
| + where = fmt.Sprintf(fileSeedHeldBothCopy, replica, staging) |
| } |
| if where == "" { |
| return fmt.Sprintf(fileSeedHeldCopy, host) |
diff --git a/internal/console/seedfilefleet_test.go b/internal/console/seedfilefleet_test.go| index 1acdef999..6eb365be0 100644 |
| --- a/internal/console/seedfilefleet_test.go |
| +++ b/internal/console/seedfilefleet_test.go |
| @@ -382,6 +382,8 @@ func TestDeleteHeldFleetFileSeedRefuses(t *testing.T) { |
| {"FS-30 with no held", fileSeedCopyInStorageCopy, "", fmt.Sprintf(fileSeedHeldCopy, fileHost)}, |
| {"an upload failure", importNotADB, "", fmt.Sprintf(fileSeedHeldCopy, fileHost)}, |
| {"FS-17", fileSeedMismatchCopy, "", fmt.Sprintf(fileSeedHeldCopy, fileHost)}, |
| + {"FS-17 with a held copy", fileSeedMismatchCopy, provision.SeedHeldReplica, held(replica)}, |
| + {"an upload failure with a held copy", importNotADB, provision.SeedHeldBoth, held(fmt.Sprintf(fileSeedHeldBothCopy, replica, staging))}, |
| } { |
| f.fleetSeedRecord(t, fileHost, seeds.StateAborted, c.reason, c.held, time.Now()) |
| before := f.rawRecord(t, fileHost) |
diff --git a/internal/console/seedholdreview_test.go b/internal/console/seedholdreview_test.go| index 9d90001c4..3b2a06445 100644 |
| --- a/internal/console/seedholdreview_test.go |
| +++ b/internal/console/seedholdreview_test.go |
| @@ -222,3 +222,73 @@ func TestSeedsPassWithoutSeedDataLeavesRegionSeeds(t *testing.T) { |
| } |
| } |
| } |
| + |
| +// readThenWrite is the deployment store with one write slipped in just |
| +// after the first Get of key: a record written between the legacy |
| +// retire's read and its delete. |
| +type readThenWrite struct { |
| + release.Store |
| + key string |
| + write func(ctx context.Context, s release.Store) |
| + done bool |
| +} |
| + |
| +func (r *readThenWrite) Get(ctx context.Context, key string) ([]byte, string, error) { |
| + raw, etag, err := r.Store.Get(ctx, key) |
| + if key == r.key && !r.done { |
| + r.done = true |
| + r.write(ctx, r.Store) |
| + } |
| + return raw, etag, err |
| +} |
| + |
| +func (r *readThenWrite) DeleteIfMatch(ctx context.Context, key, etag string) error { |
| + return release.DeleteIfMatch(ctx, r.Store, key, etag) |
| +} |
| + |
| +// Round 2 P2-A: with lifetimes off the retire's delete is bound to the read |
| +// the hold was asked of. A record written in between survives, and the |
| +// detach answers "try again". |
| +func TestLegacyRetireBindsDeleteToItsRead(t *testing.T) { |
| + fx, member := firstRouteFixture(t, "code-legacy-race") |
| + key := provision.RecordKey("bab", "titogo", holdHost) |
| + var written []byte |
| + fx.srv.cfg.Releases = &readThenWrite{Store: fx.rel, key: key, write: func(ctx context.Context, s release.Store) { |
| + raw, etag, err := s.Get(ctx, key) |
| + if err != nil { |
| + t.Error(err) |
| + return |
| + } |
| + written = []byte(strings.Replace(string(raw), `"channel":"edge"`, `"channel":"stable"`, 1)) |
| + if _, err := s.Put(ctx, key, written, etag); err != nil { |
| + t.Error(err) |
| + } |
| + }} |
| + code, _, out := rawReq(t, http.MethodDelete, domainsURL(fx, "bab", "titogo")+"/"+holdHost, member, "") |
| + if code != http.StatusBadGateway || errorOf(t, out) != "could not detach the hostname — try again" { |
| + t.Fatalf("detach = %d %s, want 502 try again", code, out) |
| + } |
| + raw, _, err := fx.rel.Get(context.Background(), key) |
| + if err != nil || written == nil || string(raw) != string(written) { |
| + t.Fatalf("record = %s, %v; want the one written between the read and the delete", raw, err) |
| + } |
| + if _, err := domains.GetClaim(context.Background(), fx.rel, holdHost); err != nil { |
| + t.Fatalf("the claim went: %v", err) |
| + } |
| + // The primitive answers the conflict itself. |
| + fx.srv.cfg.Releases = &readThenWrite{Store: fx.rel, key: key, write: func(ctx context.Context, s release.Store) { |
| + _, etag, _ := s.Get(ctx, key) |
| + changed := []byte(strings.Replace(string(raw), `"channel":"stable"`, `"channel":"edge"`, 1)) |
| + if _, err := s.Put(ctx, key, changed, etag); err != nil { |
| + t.Error(err) |
| + } |
| + }} |
| + if err := fx.srv.retireInstanceRecord(context.Background(), "bab", "titogo", holdHost, "test", "", retireAny); !errors.Is(err, release.ErrCASConflict) { |
| + t.Fatalf("retireInstanceRecord = %v, want ErrCASConflict", err) |
| + } |
| + // Absent is done. |
| + fx.srv.cfg.Releases = fx.rel |
| + if err := fx.srv.retireInstanceRecord(context.Background(), "bab", "titogo", "absent.example.com", "test", "", retireAny); err != nil { |
| + t.Fatalf("an absent record = %v, want nil", err) |
| + } |
| +} |
diff --git a/internal/provision/fleet_seeded_create_3_census_test.go b/internal/provision/fleet_seeded_create_3_census_test.go| index b0012b5bb..250744f6b 100644 |
| --- a/internal/provision/fleet_seeded_create_3_census_test.go |
| +++ b/internal/provision/fleet_seeded_create_3_census_test.go |
| @@ -21,7 +21,9 @@ package provision |
| // code maps to and held) or SeedRolledBack, each bound to the record's |
| // region and box as read under the lock. A record already where the op |
| // would put it is not written; an aborted seed takes only |
| -// copy-in-storage, over no reason or FS-30 (review P3-1). It then |
| +// copy-in-storage, over any reason, recording where the copy is held |
| +// and keeping the reason it has (FS-30 only over none; Task 16 review |
| +// round 2, P2-B). It then |
| // deletes the seed's fleet snapshot on placed (a rolled-back seed's |
| // goes with its finish) and bumps the serial. The finish that |
| // follows rolled-back runs after the lock is released, through |
| @@ -61,8 +63,10 @@ package provision |
| // - Server.retireLegacyInstanceRecord (console, new): retireHostRecord's |
| // claims-off, lifetimes-off instance branch. It reads the record's body |
| // (a Get of RecordKey), asks the same hold of its region and seed, and |
| -// then retires through provision.RetireRecord exactly as before. It |
| -// writes nothing itself. key-site / key:RecordKey. |
| +// deletes by release.DeleteIfMatch on the etag of that read (review |
| +// round 2, P2-A): a record written since answers ErrCASConflict, an |
| +// absent key is done. key-site / key:RecordKey, as deleteRead, its |
| +// sibling delete bound to a read. |
| // |
| // Not in the inventory, because they write no record: seedFleetReady, |
| // fleetSeedSnapshots (the sweep's fleet listing; the sweep's own record |
| @@ -99,7 +103,7 @@ func fleetSeededCreate3WriteCensusInventory() []censusEntry { |
| } |
| } |
| return append(entries, |
| - censusEntry{"internal/console/hostclaimops.go", "Server.retireLegacyInstanceRecord", "key-site", "key:RecordKey", "df655a77aa25b47e06185d3127b539c8b32257c42ec2e71c61393498c2bda34b"}, |
| - censusEntry{"internal/console/apifleetseeds.go", "Server.fleetSeedTransition", "record-writer", "key:SetRecordSeed", "360d42934163d3d07225d927ac7fee6fdd40a6d9d86658a829b587450dee3f7a"}, |
| + censusEntry{"internal/console/hostclaimops.go", "Server.retireLegacyInstanceRecord", "key-site", "key:RecordKey", "0b8733784111dc645f84a88163c4e8c1ad82c5b94a514abd3f3d944b516a717c"}, |
| + censusEntry{"internal/console/apifleetseeds.go", "Server.fleetSeedTransition", "record-writer", "key:SetRecordSeed", "44b66548aa5c3cafd85d240edbd4bd48e7f3ccd66dfce76facd5d20561738265"}, |
| ) |
| } |