| 1 | //go:build unix |
| 2 | |
| 3 | package hostbox |
| 4 | |
| 5 | import ( |
| 6 | "context" |
| 7 | "encoding/json" |
| 8 | "errors" |
| 9 | "slices" |
| 10 | "strconv" |
| 11 | "strings" |
| 12 | "testing" |
| 13 | "time" |
| 14 | |
| 15 | "github.com/carlosframework/platform/internal/hostclaim" |
| 16 | ) |
| 17 | |
| 18 | // completeFromInstalling is the target finishing after the source's |
| 19 | // fence closed: its probe passes on "dst", root stops the probe session, |
| 20 | // and the real serving CAS (hostclaim.CompleteRelocation) queues the |
| 21 | // source's park-local. No reconcile runs on the source meanwhile. |
| 22 | func (s *sourceReconcile) completeFromInstalling(t *testing.T) hostclaim.Observed { |
| 23 | t.Helper() |
| 24 | s.update(t, func(c *hostclaim.Claim) error { |
| 25 | h := c.Holder |
| 26 | h.Session, h.Generation, h.WriterBox = h.Session+1, "P1", "dst" |
| 27 | h.Admissions = append(h.Admissions, hostclaim.AdmissionRecord{Session: h.Session, Box: "dst", Generation: "P1", Predecessor: "g1", Mode: "cold", Region: "ie"}) |
| 28 | probe, _ := json.Marshal(hostclaim.Probe{Box: "dst", Session: h.Session, At: time.Unix(90, 0).UTC()}) |
| 29 | stop, _ := json.Marshal(hostclaim.ProbeStop{Box: "dst", Session: h.Session, Receipt: "probe-fence-1", ReceiptSHA256: strings.Repeat("a", 64)}) |
| 30 | c.Op.Phase = hostclaim.PhaseRelocatingProbing |
| 31 | c.Op.Journal["probe"], c.Op.Journal["probe_stop"] = string(probe), string(stop) |
| 32 | return nil |
| 33 | }) |
| 34 | n := 0 |
| 35 | o := s.update(t, hostclaim.CompleteRelocation("rel-1", func() string { n++; return "id-" + strconv.Itoa(n) })) |
| 36 | if o.Claim.Op.Kind != hostclaim.KindServing || o.Claim.Op.Exec != "dst" || !hostclaim.OwesLocal(o.Claim, "box") { |
| 37 | t.Fatalf("after the completion: op %+v obligations %+v", o.Claim.Op, o.Claim.Obligations) |
| 38 | } |
| 39 | return o |
| 40 | } |
| 41 | |
| 42 | // L18 joint round, finding 3, through the production ReconcileAdapter, |
| 43 | // the real census (CheckHeld), park.Local and provision's pass: the |
| 44 | // source's fence closes in releasing, its reconcile is delayed past |
| 45 | // installing and the target's completion, and only then does it run. The |
| 46 | // claim now serves on "dst", so Task 10b's relocating scope is gone; the |
| 47 | // catch-up hold keeps the row held for the completed op, never ready, |
| 48 | // with its ownership and every other column as minted, the guard record |
| 49 | // and the claim unchanged. A replay changes nothing. |
| 50 | func TestReconcileCatchUpHoldsTheSourceAfterTheTargetCompleted(t *testing.T) { |
| 51 | s := newSourceReconcile(t) |
| 52 | minted := *s.row(t) |
| 53 | s.releasingThenInstalling(t) |
| 54 | o := s.completeFromInstalling(t) |
| 55 | guard := targetGuardFiles(t, s.f.data) |
| 56 | for i := 0; i < 2; i++ { |
| 57 | s.pass(t) |
| 58 | row := s.row(t) |
| 59 | if row == nil || row.Status != "relocating" || row.HoldOp != "rel-1" { |
| 60 | t.Fatalf("pass %d after the completion: the source's row is %+v (minted %+v)", i, row, minted) |
| 61 | } |
| 62 | held := *row |
| 63 | held.Status, held.HoldOp = minted.Status, minted.HoldOp |
| 64 | if held != minted { |
| 65 | t.Fatalf("pass %d: the hold changed more than status and hold_op: %+v", i, row) |
| 66 | } |
| 67 | if got := targetGuardFiles(t, s.f.data); got[s.f.host+".json"] != guard[s.f.host+".json"] { |
| 68 | t.Fatalf("pass %d: the guard record changed", i) |
| 69 | } |
| 70 | if after, err := hostclaim.Read(t.Context(), s.f.store, s.f.host); err != nil || after.ETag != o.ETag { |
| 71 | t.Fatalf("pass %d: the claim was written: %v", i, err) |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | // The catch-up hold through the pass: a row another op holds is never |
| 77 | // taken, and with region HA off (neither hook set) the pass is main's, |
| 78 | // the row ready and exactly as minted. |
| 79 | func TestReconcileCatchUpHoldGuardsThroughThePass(t *testing.T) { |
| 80 | t.Run("another op's row", func(t *testing.T) { |
| 81 | s := newSourceReconcile(t) |
| 82 | if ok, err := s.reg.SetHoldIf(s.f.host, "moving", "mv-9", ""); err != nil || !ok { |
| 83 | t.Fatalf("holding for another op: %v %v", ok, err) |
| 84 | } |
| 85 | before := *s.row(t) |
| 86 | s.releasingThenInstalling(t) |
| 87 | s.completeFromInstalling(t) |
| 88 | s.pass(t) |
| 89 | if got := s.row(t); *got != before { |
| 90 | t.Fatalf("another op's row was taken: %+v (was %+v)", got, before) |
| 91 | } |
| 92 | }) |
| 93 | t.Run("region HA off", func(t *testing.T) { |
| 94 | s := newSourceReconcile(t) |
| 95 | s.adapter.Reconciler.FenceClosed, s.adapter.Reconciler.HoldRow = nil, nil |
| 96 | s.adapter.Configure(&s.cfg) |
| 97 | minted := *s.row(t) |
| 98 | s.releasingThenInstalling(t) |
| 99 | s.completeFromInstalling(t) |
| 100 | s.pass(t) |
| 101 | if got := s.row(t); *got != minted { |
| 102 | t.Fatalf("region HA off: the row is %+v (was %+v)", got, minted) |
| 103 | } |
| 104 | }) |
| 105 | } |
| 106 | |
| 107 | // completedServing rewrites the fixture's claim as a completed relocation's |
| 108 | // serving op: exec on exec, the completion recorded on stopBox, and the |
| 109 | // park-local obligations given. |
| 110 | func completedServing(t *testing.T, r Reconciler, host, exec, stopBox string, owed ...hostclaim.Obligation) hostclaim.Observed { |
| 111 | t.Helper() |
| 112 | ctx := context.Background() |
| 113 | obs, err := hostclaim.Read(ctx, r.Claims, host) |
| 114 | if err != nil { |
| 115 | t.Fatal(err) |
| 116 | } |
| 117 | done, err := json.Marshal(hostclaim.CompletedRelocation{Op: "rel-1", Stop: hostclaim.ProbeStop{Box: stopBox, Session: 7, Receipt: "probe-fence-1", ReceiptSHA256: strings.Repeat("a", 64)}, Phase: hostclaim.PhaseRelocatingProbing}) |
| 118 | if err != nil { |
| 119 | t.Fatal(err) |
| 120 | } |
| 121 | obs, err = hostclaim.Update(ctx, r.Claims, obs, func(c *hostclaim.Claim) error { |
| 122 | c.Op = hostclaim.Op{Kind: hostclaim.KindServing, Exec: exec, Participants: []string{exec}, Journal: map[string]string{hostclaim.JournalCompleted: string(done)}} |
| 123 | c.Obligations = append(c.Obligations, owed...) |
| 124 | return nil |
| 125 | }) |
| 126 | if err != nil { |
| 127 | t.Fatal(err) |
| 128 | } |
| 129 | return obs |
| 130 | } |
| 131 | |
| 132 | func parkLocal(box, op string) hostclaim.Obligation { |
| 133 | return hostclaim.Obligation{ID: "ob-" + box + "-" + op, Op: op, Kind: "park-local", Box: box, Retirement: "rt-" + box + "-" + op, Owner: "box:" + box} |
| 134 | } |
| 135 | |
| 136 | // The catch-up hold's scope, guard by guard, on the stubbed reconciler |
| 137 | // (relocationReconciler, its HoldRow recording): held for the completed |
| 138 | // op on positive evidence only, hold only, and nowhere else. |
| 139 | func TestReconcileCatchUpHoldScope(t *testing.T) { |
| 140 | ctx := context.Background() |
| 141 | install := func(r Reconciler, selected RecordSelection, writes *int) error { |
| 142 | return r.Install(ctx, selected, func() error { *writes++; return nil }) |
| 143 | } |
| 144 | t.Run("the source owing the completed op's park-local is held, hold only", func(t *testing.T) { |
| 145 | r, selected, holds, _ := relocationReconciler(t) |
| 146 | obs := completedServing(t, r, selected.Host, "dst", "dst", parkLocal("box", "rel-1")) |
| 147 | writes := 0 |
| 148 | if err := install(r, selected, &writes); err != nil || writes != 0 || !slices.Equal(*holds, []string{"rel-1"}) { |
| 149 | t.Fatalf("the catch-up hold: %v writes %d holds %v", err, writes, *holds) |
| 150 | } |
| 151 | // No installation authority: the activation reader's and the |
| 152 | // inventory callback's check, which carry no Install decision, and |
| 153 | // an activation decision, are all refused. |
| 154 | if err := r.check(ctx, obs, selected); !errors.Is(err, ErrChanged) { |
| 155 | t.Fatalf("the activation path's check under the catch-up: %v", err) |
| 156 | } |
| 157 | d, ok, err := r.completedHold(obs.Claim) |
| 158 | if err != nil || !ok { |
| 159 | t.Fatalf("completedHold: %v %v", ok, err) |
| 160 | } |
| 161 | if err := r.checkOn(ctx, obs, selected, &decided{row: d}); !errors.Is(err, ErrChanged) { |
| 162 | t.Fatalf("an activation decision under the catch-up: %v", err) |
| 163 | } |
| 164 | if err := r.checkOn(ctx, obs, selected, &decided{row: RowDecision{Case: RowHeld, HoldOp: "rel-1"}, holdOnly: true}); !errors.Is(err, ErrChanged) { |
| 165 | t.Fatalf("a hold decision the catch-up did not make: %v", err) |
| 166 | } |
| 167 | }) |
| 168 | for name, tc := range map[string]struct { |
| 169 | exec, stop string |
| 170 | owed []hostclaim.Obligation |
| 171 | lost bool |
| 172 | wantWrites int // main's serving executor writes through activation |
| 173 | }{ |
| 174 | "this box serves": {exec: "box", stop: "dst", owed: []hostclaim.Obligation{parkLocal("box", "rel-1")}, wantWrites: 1}, |
| 175 | "completed on this box": {exec: "dst", stop: "box", owed: []hostclaim.Obligation{parkLocal("box", "rel-1")}}, |
| 176 | "no park-local for this box": {exec: "dst", stop: "dst", owed: []hostclaim.Obligation{parkLocal("other", "rel-1")}}, |
| 177 | "a park-local for another op": {exec: "dst", stop: "dst", owed: []hostclaim.Obligation{parkLocal("box", "rel-0")}}, |
| 178 | "this box on lost": {exec: "dst", stop: "dst", owed: []hostclaim.Obligation{parkLocal("box", "rel-1")}, lost: true}, |
| 179 | "another kind of obligation for box": {exec: "dst", stop: "dst", owed: []hostclaim.Obligation{{ID: "ob-1", Op: "rel-1", Kind: "relocation-release", Box: "box", Region: "ie", Retirement: "rt-1", Owner: "region:ie"}}}, |
| 180 | } { |
| 181 | t.Run(name, func(t *testing.T) { |
| 182 | r, selected, holds, _ := relocationReconciler(t) |
| 183 | completedServing(t, r, selected.Host, tc.exec, tc.stop, tc.owed...) |
| 184 | if tc.lost { |
| 185 | obs, err := hostclaim.Read(ctx, r.Claims, selected.Host) |
| 186 | if err != nil { |
| 187 | t.Fatal(err) |
| 188 | } |
| 189 | if _, err := hostclaim.Update(ctx, r.Claims, obs, func(c *hostclaim.Claim) error { c.Lost = append(c.Lost, "box"); return nil }); err != nil { |
| 190 | t.Fatal(err) |
| 191 | } |
| 192 | } |
| 193 | writes := 0 |
| 194 | err := install(r, selected, &writes) |
| 195 | if len(*holds) != 0 || writes != tc.wantWrites { |
| 196 | t.Fatalf("%v: holds %v writes %d (want %d)", err, *holds, writes, tc.wantWrites) |
| 197 | } |
| 198 | if tc.wantWrites == 0 && !errors.Is(err, ErrChanged) { |
| 199 | t.Fatalf("main's Serving refusal: %v", err) |
| 200 | } |
| 201 | }) |
| 202 | } |
| 203 | t.Run("region HA off", func(t *testing.T) { |
| 204 | r, selected, holds, _ := relocationReconciler(t) |
| 205 | r.FenceClosed, r.HoldRow = nil, nil |
| 206 | completedServing(t, r, selected.Host, "dst", "dst", parkLocal("box", "rel-1")) |
| 207 | writes := 0 |
| 208 | if err := install(r, selected, &writes); !errors.Is(err, ErrChanged) || writes != 0 || len(*holds) != 0 { |
| 209 | t.Fatalf("region HA off: %v writes %d holds %v", err, writes, *holds) |
| 210 | } |
| 211 | }) |
| 212 | t.Run("the decision is remade under the lock", func(t *testing.T) { |
| 213 | r, selected, _, _ := relocationReconciler(t) |
| 214 | completedServing(t, r, selected.Host, "dst", "dst", parkLocal("box", "rel-1")) |
| 215 | obs, err := hostclaim.Read(ctx, r.Claims, selected.Host) |
| 216 | if err != nil { |
| 217 | t.Fatal(err) |
| 218 | } |
| 219 | d, ok, err := r.completedHold(obs.Claim) |
| 220 | if err != nil || !ok { |
| 221 | t.Fatalf("completedHold: %v %v", ok, err) |
| 222 | } |
| 223 | // The park-local is discharged between the decision and the lock. |
| 224 | if _, err := hostclaim.Update(ctx, r.Claims, obs, func(c *hostclaim.Claim) error { c.Obligations = nil; return nil }); err != nil { |
| 225 | t.Fatal(err) |
| 226 | } |
| 227 | if err := r.holdOnly(ctx, selected, d); !errors.Is(err, ErrChanged) { |
| 228 | t.Fatalf("a catch-up decision the fresh read no longer makes: %v", err) |
| 229 | } |
| 230 | }) |
| 231 | } |
| 232 | |
| 233 | // RegistryHoldRow's guards hold for the catch-up as they do for Task 10b: |
| 234 | // a held row for the completed op is a replay with nothing written. |
| 235 | func TestReconcileCatchUpHoldReplayWritesNothing(t *testing.T) { |
| 236 | r, selected, reg, _ := sourceRows(t) |
| 237 | completedServing(t, r, selected.Host, "dst", "dst", parkLocal("box", "rel-1")) |
| 238 | if err := r.Install(context.Background(), selected, func() error { return errors.New("activated") }); err != nil { |
| 239 | t.Fatalf("the catch-up hold: %v", err) |
| 240 | } |
| 241 | held := *rowOf(t, reg, selected.Host) |
| 242 | if held.Status != "relocating" || held.HoldOp != "rel-1" { |
| 243 | t.Fatalf("held row %+v", held) |
| 244 | } |
| 245 | if err := r.Install(context.Background(), selected, func() error { return errors.New("activated") }); err != nil { |
| 246 | t.Fatalf("the replay: %v", err) |
| 247 | } |
| 248 | if got := rowOf(t, reg, selected.Host); *got != held { |
| 249 | t.Fatalf("the replay changed the row: %+v (was %+v)", got, held) |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | // Review M1: the catch-up hold is a hold only on this box's own existing |
| 254 | // installation (CheckHeld, as for Task 10b). Through the production |
| 255 | // adapter and the pass, a census that refuses (the source's row owned by |
| 256 | // another incarnation, or an inventory of another enrollment) holds |
| 257 | // nothing: the row stays exactly as it was. On the stubbed reconciler a |
| 258 | // refusing CheckHeld is Install's error and HoldRow is never called. |
| 259 | func TestReconcileCatchUpHoldNeedsCheckHeld(t *testing.T) { |
| 260 | t.Run("a row of another incarnation", func(t *testing.T) { |
| 261 | s := newSourceReconcile(t) |
| 262 | s.releasingThenInstalling(t) |
| 263 | s.completeFromInstalling(t) |
| 264 | stranger := *s.row(t) |
| 265 | stranger.ClaimIncarnation = "inc-other" |
| 266 | // Ownership is never rewritten in place: the row is replaced. |
| 267 | if err := s.reg.Delete(s.f.host); err != nil { |
| 268 | t.Fatal(err) |
| 269 | } |
| 270 | if err := s.reg.Upsert(stranger); err != nil { |
| 271 | t.Fatal(err) |
| 272 | } |
| 273 | before := *s.row(t) |
| 274 | s.pass(t) |
| 275 | if got := s.row(t); *got != before { |
| 276 | t.Fatalf("a row of another incarnation was held: %+v (was %+v)", got, before) |
| 277 | } |
| 278 | }) |
| 279 | t.Run("an inventory of another enrollment", func(t *testing.T) { |
| 280 | s := newSourceReconcile(t) |
| 281 | before := *s.row(t) |
| 282 | s.releasingThenInstalling(t) |
| 283 | s.completeFromInstalling(t) |
| 284 | contradicting := *s.census |
| 285 | contradicting.Enrollment = "en-2" |
| 286 | s.adapter.Census = &contradicting |
| 287 | s.adapter.Configure(&s.cfg) |
| 288 | s.pass(t) |
| 289 | if got := s.row(t); *got != before { |
| 290 | t.Fatalf("a contradicting inventory was held: %+v (was %+v)", got, before) |
| 291 | } |
| 292 | }) |
| 293 | t.Run("stubbed", func(t *testing.T) { |
| 294 | r, selected, holds, _ := relocationReconciler(t) |
| 295 | completedServing(t, r, selected.Host, "dst", "dst", parkLocal("box", "rel-1")) |
| 296 | r.CheckHeld = func(context.Context, hostclaim.Observed, RecordSelection) error { return ErrTypedInventory } |
| 297 | writes := 0 |
| 298 | if err := r.Install(context.Background(), selected, func() error { writes++; return nil }); !errors.Is(err, ErrTypedInventory) || writes != 0 || len(*holds) != 0 { |
| 299 | t.Fatalf("a refusing CheckHeld: %v writes %d holds %v", err, writes, *holds) |
| 300 | } |
| 301 | }) |
| 302 | } |
| 303 | |
| 304 | // Review M2: holdOnly makes the catch-up decision again on the fresh read |
| 305 | // under the host lock and holds nothing when it differs. Install's |
| 306 | // decision is made, then (inside CheckHeld, which runs after the decision |
| 307 | // and before the lock) the claim is rewritten to another completed |
| 308 | // relocation, rel-2, which this box also owes a park-local for: the fresh |
| 309 | // read is still in catch-up scope, but for rel-2, so the locked decision |
| 310 | // no longer holds and nothing is held. In production this state is not |
| 311 | // reachable while the row exists: CompleteRelocation queues no second |
| 312 | // park-local for a box that already owes one, RelocationStartGuard refuses |
| 313 | // a source that owes one, and once rel-1's park-local retires the row, |
| 314 | // RegistryHoldRow finds none. The test pins the re-decision directly. |
| 315 | func TestReconcileCatchUpHoldIsDecidedAgainUnderTheLock(t *testing.T) { |
| 316 | ctx := context.Background() |
| 317 | r, selected, holds, _ := relocationReconciler(t) |
| 318 | completedServing(t, r, selected.Host, "dst", "dst", parkLocal("box", "rel-1")) |
| 319 | done, err := json.Marshal(hostclaim.CompletedRelocation{Op: "rel-2", Stop: hostclaim.ProbeStop{Box: "dst", Session: 8, Receipt: "probe-fence-2", ReceiptSHA256: strings.Repeat("b", 64)}, Phase: hostclaim.PhaseRelocatingProbing}) |
| 320 | if err != nil { |
| 321 | t.Fatal(err) |
| 322 | } |
| 323 | moved := false |
| 324 | r.CheckHeld = func(ctx context.Context, _ hostclaim.Observed, _ RecordSelection) error { |
| 325 | obs, err := hostclaim.Read(ctx, r.Claims, selected.Host) |
| 326 | if err != nil { |
| 327 | return err |
| 328 | } |
| 329 | _, err = hostclaim.Update(ctx, r.Claims, obs, func(c *hostclaim.Claim) error { |
| 330 | c.Op.Journal[hostclaim.JournalCompleted] = string(done) |
| 331 | c.Obligations = []hostclaim.Obligation{parkLocal("box", "rel-2")} |
| 332 | return nil |
| 333 | }) |
| 334 | moved = err == nil |
| 335 | return err |
| 336 | } |
| 337 | writes := 0 |
| 338 | if err := r.Install(ctx, selected, func() error { writes++; return nil }); !errors.Is(err, ErrChanged) || !moved || writes != 0 || len(*holds) != 0 { |
| 339 | t.Fatalf("a catch-up decision the locked read no longer makes: %v moved %v writes %d holds %v", err, moved, writes, *holds) |
| 340 | } |
| 341 | obs, err := hostclaim.Read(ctx, r.Claims, selected.Host) |
| 342 | if err != nil { |
| 343 | t.Fatal(err) |
| 344 | } |
| 345 | if d, ok, err := r.completedHold(obs.Claim); err != nil || !ok || d != (RowDecision{Case: RowRetiring, HoldOp: "rel-2"}) { |
| 346 | t.Fatalf("the fresh read is not the rel-2 catch-up: %+v %v %v", d, ok, err) |
| 347 | } |
| 348 | } |
| 349 | |