//go:build unix package host import ( "context" "encoding/json" "testing" "time" "github.com/carlosframework/platform/internal/hostbox" "github.com/carlosframework/platform/internal/hostclaim" "github.com/carlosframework/platform/internal/hostguard" "github.com/carlosframework/platform/internal/registry" ) // Carry 9 (Task 9 review, ruling 82): a relocation completes on box-1 with // region HA on; box-1 restarts with CARLOS_REGION_HA off. One scenario, // with no agent restart between its halves, over one control bucket, one // guard journal, one registry and one activator: // // 1. the probe stop: the production probe-stop provider fences the probe // session under its persisted owner and root's writer-session stop // lands in the guard journal; the serving CAS records the completion; // 2. the completion pass: NewRelocationRuntime with claim mode alone // builds the completion-only runtime, whose pass discharges through // main's own DischargeCompleted, releases the hold, lifts the owned // fence and marks the record, so the barrier opens; // 3. the first serving wake: the wake runtime (ClaimWakeAssembly.Build) // wired as OpenProductionClaimAssembly wires it with moves C1 off // (wireProductionAdmission, N6 in front) takes the predecessor stop // Prerequisites takes, which re-stops nothing, then admits through // the runtime's own Admit, which hands root the probe's receipt as the // prior. // // The production constructor itself cannot run here in agent mode: it // dials root at the fixed controlsock.Path, chowns its state directory to // the carlos uid and needs a cgroup manager. Checklist item 12 is that run. func TestCompletionPassThenFirstServingWakeWithRegionHAOff(t *testing.T) { t.Parallel() ctx := t.Context() f := newRecoveryFixture(t) const box, op = "box-1", "rel-1" local := claimStopLocal{Actor: hostbox.Actor{Box: box, Enrollment: "enrollment-1", Revision: 7, Binding: "7", Region: "ie"}, BootID: "boot-0", ControllerID: "controller-0", PID: 4000, ProcessStart: "start-0"} // The relocating claim at probing on box-1, its probe passed with // session 12 at install stamp 9. // Written whole, as the stop-probe fixture's claim is: the transitions // that lead here are hostrelocate's and are tested there. probe, _ := json.Marshal(hostclaim.Probe{Box: box, Session: 12, At: f.now}) created := f.read() c := *created.Claim holder := *c.Holder holder.Stamp, holder.Session, holder.Generation, holder.WriterBox = 9, 12, "P1", box holder.Admissions = []hostclaim.AdmissionRecord{{Session: 12, Box: box, Generation: "P1", Mode: "cold", Region: "ie"}} c.Holder = &holder c.Gen = 30 // the history behind stamp 9 and session 12 c.Op = hostclaim.Op{Kind: hostclaim.KindRelocating, ID: op, Phase: hostclaim.PhaseRelocatingProbing, Exec: box, Participants: []string{"src", box}, Intent: map[string]string{"region": "ie", "source_box": "src", "target_box": box}, Journal: map[string]string{"probe": string(probe)}} key, _ := hostclaim.Key(recoveryHost) if _, err := f.claims.Put(ctx, key, mustJSON(t, &c), created.ETag); err != nil { t.Fatal(err) } o := f.read() if err := f.guard.Activate(ctx, recoveryHost, box, nil); err != nil { t.Fatal(err) } // 1. The probe stop, root's writer-session stop standing as Wake.Stop. h := o.Claim.Holder target := hostguard.FenceTarget{FenceID: "fence-1", Region: "ie", Host: recoveryHost, Incarnation: h.Incarnation, Stamp: 9, Session: 12, Op: op, Phase: hostclaim.PhaseRelocatingProbing, ObligationID: hostguard.WriterStopObligation(12), Box: box, Enrollment: local.Actor.Enrollment, BootID: local.BootID, ControllerID: local.ControllerID, ClaimETag: o.ETag, DirectoryRevision: "7", RosterRevision: 7} at := f.now.Add(time.Minute) receipt := mustMarshalStopReceipt(StopReceipt{V: 1, ID: "r1", Kind: StopReceiptStopped, Region: "ie", ClaimETag: o.ETag, ObligationID: target.ObligationID, FenceID: "fence-1", Host: recoveryHost, Incarnation: h.Incarnation, InstallationStamp: 9, WriterSession: 12, OperationID: op, Phase: hostclaim.PhaseRelocatingProbing, Box: box, Enrollment: local.Actor.Enrollment, BootID: local.BootID, ControllerID: local.ControllerID, DirectoryRevision: testReceiptNumeral(target), Binding: "7", EvidenceAt: at}) var order []string act := newStopProbeActivator(&order) probeAssembly := ClaimAssembly{Claims: f.claims, Guard: f.guard, stopLocal: func(context.Context) (claimStopLocal, error) { return local, nil }, Wake: &ClaimWakeAssembly{Stop: func(ctx context.Context, _ hostclaim.Observed) error { if _, err := f.guard.BeginFence(ctx, target); err != nil { return err } return f.guard.CompleteFence(ctx, target, receipt, at, func(context.Context, hostguard.FenceTarget, []byte, time.Time) error { return nil }) }}} stop, err := probeAssembly.relocationStopProbe(RelocationAssemblyConfig{Activator: act, DataDir: f.guard.DataDir})(ctx, o) if err != nil { t.Fatal(err) } if !act.svc.Fenced(recoveryHost) { t.Fatal("the probe stop installed no fence") } // The serving CAS, as CompleteRelocation leaves it. done, _ := json.Marshal(hostclaim.CompletedRelocation{Op: op, Stop: stop, Stamp: 9, Phase: hostclaim.PhaseRelocatingProbing, Generation: "P1"}) c.Gen = 31 c.Op = hostclaim.Op{Kind: hostclaim.KindServing, Exec: box, Participants: []string{box}, Journal: map[string]string{hostclaim.JournalCompleted: string(done)}} if _, err := f.claims.Put(ctx, key, mustJSON(t, &c), o.ETag); err != nil { t.Fatal(err) } reg, err := registry.Open(t.TempDir() + "/registry.db") if err != nil { t.Fatal(err) } t.Cleanup(func() { reg.Close() }) if err := reg.Upsert(registry.Route{Host: recoveryHost, Status: "relocating", HoldOp: op, Socket: "/run/shop.sock"}); err != nil { t.Fatal(err) } // 2. The completion pass after the restart with region HA off: claim // mode alone, main's own participation owner and discharge. identity := handoverRecoveryIdentity(t, local.Actor) bound := &ClaimAssembly{Claims: f.claims, Guard: f.guard, stopLocal: func(context.Context) (claimStopLocal, error) { return local, nil }} owner := &productionClaimLocal{legacyParticipation: true, Resources: ClaimRuntimeResources{Mode: "agent", DataDir: f.guard.DataDir}, Identity: identity, Store: f.claims, slots: map[string]*claimParticipation{}, assembly: bound, lifetime: ctx} claimOnly := func(k string) string { if k == "CARLOS_HOST_CLAIM" { return "1" } return "" } r, err := NewRelocationRuntime(ctx, claimOnly, f.claims, owner, ClaimProviders{}, reg, act, relocationRuntimeInputs{}) if err != nil || r == nil || r.Worker != nil || r.Participant != nil || r.Convert != nil { t.Fatalf("not the completion-only runtime: %+v %v", r, err) } r.once(ctx, f.claims) prior := f.read() marked, ok, err := hostclaim.ReadCompletedRelocation(prior.Claim) if err != nil || !ok || !marked.Discharged { t.Fatalf("the completion was not marked: %+v %v %v", marked, ok, err) } if row, err := reg.Get(recoveryHost); err != nil || row == nil || row.HoldOp != "" { t.Fatalf("the hold was not released: %+v %v", row, err) } if act.svc.Fenced(recoveryHost) { t.Fatal("the probe's owned fence was not lifted") } if err := hostclaim.StartGuard(prior.Claim, []string{"uk"}, []string{"spa"}, ""); err != nil { t.Fatalf("the barrier did not open: %v", err) } // 3. The first serving wake, N6 in front as the production // constructor wires it with moves C1 off. w := f.controller("boot-1", "controller-1", 4101, "start-1") socket, posted := handoverRoot(t) locals := 0 w.wake.AfterAdmission = func(context.Context, hostclaim.Observed, hostclaim.Observed) error { locals++; return nil } wireProductionAdmission(w.wake, box, false, claimCrossBoxHandover{Box: box}, nil, &relocationCompletedHandover{Guard: f.guard, Identity: identity, Socket: socket, Box: box}) // Prerequisites' predecessor stop: the probe's receipt is found and // verified; root is asked to stop nothing. if err := w.wake.stopPredecessor(ctx, prior); err != nil || len(f.requests) != 0 { t.Fatalf("the predecessor stop: %v (root asked %d stops)", err, len(f.requests)) } admitted := w.admit(t, f) if admitted.Claim.Holder.Session <= 12 || admitted.Claim.Holder.WriterBox != box { t.Fatalf("not a new writer session on %s: %+v", box, admitted.Claim.Holder) } if len(*posted) != 1 || (*posted)[0].Prior != target || (*posted)[0].Next.Session != admitted.Claim.Holder.Session || locals != 0 || len(f.requests) != 0 { t.Fatalf("root was handed %+v (main's local handover %d, stops %d)", *posted, locals, len(f.requests)) } }