package console import ( "context" "encoding/json" "net/http" "net/url" "testing" "time" "github.com/carlosframework/platform/internal/hostclaim" "github.com/carlosframework/platform/internal/hostops" "github.com/carlosframework/platform/internal/moves" ) // Plan 6 Task 3 fix round 5, ruling 2: an instance incarnation's move log // outbox rows go when the incarnation is retired or purged, in that step, // not when the drainer next finds the claim gone (its drop stays a // backstop). Another incarnation's rows stay. // queueTestMoveLog queues one confirmed row for host, op and incarnation. func queueTestMoveLog(t *testing.T, st *Store, host, op, name, inc string) { t.Helper() body, err := json.Marshal(moves.LogEvent{V: 1, Op: op, Host: host, Event: "x", Identity: name, Actor: "console", At: time.Now().UTC()}) if err != nil { t.Fatal(err) } if err := st.QueueMoveLog(host, op, name, inc, body, true, time.Now()); err != nil { t.Fatal(err) } } func moveLogRows(t *testing.T, st *Store, host, op string) int { t.Helper() got, err := st.MoveLogPending(host, op) if err != nil { t.Fatal(err) } return len(got) } func holderIncarnation(t *testing.T, cx *claimCase, host string) string { t.Helper() o, err := hostclaim.Read(context.Background(), cx.rel, host) if err != nil || o.Absent || o.Claim.Holder == nil { t.Fatalf("the claim of %s: %+v %v", host, o, err) } return o.Claim.Holder.Incarnation } func TestRetiringAnInstancePurgesItsMoveLogRows(t *testing.T) { cx := newClaimCaseWith(t) cx.instanceWorld(t) cx.instance(t, `{"host":"acme.go.tito.io","region":"ie"}`) cx.quiesce() inc := holderIncarnation(t, cx, "acme.go.tito.io") queueTestMoveLog(t, cx.st, "acme.go.tito.io", "mv_retired", "done", inc) queueTestMoveLog(t, cx.st, "acme.go.tito.io", "mv_earlier", "done", "an-earlier-incarnation") // A row the writer refuses (the record never named its move) is held, // not deleted (final fix, ruling 1): the drainer runs once, as the moves // switch would wire it, and the row stays, held, for the retire's purge. r := *cx.srv.cfg.HostClaimRecovery r.MoveLogs = cx.srv.drainMoveLog if err := cx.srv.cfg.ClaimAuthority.Control.RecoveryPass(context.Background(), r); err != nil { t.Logf("the recovery pass: %v", err) } if got, err := cx.st.MoveLogPending("acme.go.tito.io", "mv_retired"); err != nil || len(got) != 1 || !got[0].Held { t.Fatalf("the refused row is not held: %+v %v", got, err) } if status := claimRetireDoor("/api/cli/apps/bab/titogo/instances/acme.go.tito.io", "acme.go.tito.io")(t, cx); status != http.StatusAccepted { t.Fatalf("delete answered %d", status) } if n := moveLogRows(t, cx.st, "acme.go.tito.io", "mv_retired"); n != 1 { t.Fatalf("the rows went before the incarnation was retired: %d", n) } // The boxes park the instance (world staging: its obligations are // discharged), and the delete's retry frees the claim and retires the // incarnation. for i := 0; i < 3; i++ { obs, err := hostclaim.Read(context.Background(), cx.rel, "acme.go.tito.io") if err != nil { t.Fatal(err) } if obs.Absent || obs.Claim.Free() { break } if obs.Claim.Op.Kind == hostclaim.KindParking && len(obs.Claim.Obligations) != 0 { if _, err := hostclaim.Update(context.Background(), cx.rel, obs, func(c *hostclaim.Claim) error { c.Obligations = nil; return nil }); err != nil { t.Fatal(err) } } rawReq(t, http.MethodDelete, cx.ts.URL+"/api/cli/apps/bab/titogo/instances/acme.go.tito.io", cx.bearer, "") } if o, err := hostclaim.Read(context.Background(), cx.rel, "acme.go.tito.io"); err != nil || !(o.Absent || o.Claim.Free()) { t.Fatalf("the instance was not retired: op %+v holder %+v obligations %+v %v", o.Claim.Op, o.Claim.Holder, o.Claim.Obligations, err) } if n := moveLogRows(t, cx.st, "acme.go.tito.io", "mv_retired"); n != 0 { t.Fatalf("%d rows of the retired incarnation remain", n) } if n := moveLogRows(t, cx.st, "acme.go.tito.io", "mv_earlier"); n != 1 { t.Fatalf("another incarnation's rows: %d, want 1", n) } } // The app purge retires its instances' incarnations itself (RetirePurgeHosts, // not this door), and records each in its journal; its last stage drops their // rows. The fixture world cannot drive a purge of an app with instances to // completion (its hosts' purge needs the boxes), so the stage is tested on a // journal. func TestPurgingAnAppPurgesItsInstancesMoveLogRows(t *testing.T) { fx := newConsoleFixture(t) j := hostops.PurgeJournal{Hosts: map[string]hostops.PurgeHost{ "a.example.com": {Holder: &hostclaim.Holder{Incarnation: "inc-a"}}, "b.example.com": {Holder: &hostclaim.Holder{Incarnation: "inc-b"}}, "c.example.com": {}, }} queueTestMoveLog(t, fx.st, "a.example.com", "mv_1", "done", "inc-a") queueTestMoveLog(t, fx.st, "b.example.com", "mv_2", "done", "inc-b") queueTestMoveLog(t, fx.st, "b.example.com", "mv_3", "done", "inc-b-earlier") queueTestMoveLog(t, fx.st, "d.example.com", "mv_4", "done", "inc-d") if err := fx.srv.purgeMoveLogOfPurgedHosts(j); err != nil { t.Fatal(err) } for _, c := range []struct { host, op string want int }{{"a.example.com", "mv_1", 0}, {"b.example.com", "mv_2", 0}, {"b.example.com", "mv_3", 1}, {"d.example.com", "mv_4", 1}} { if n := moveLogRows(t, fx.st, c.host, c.op); n != c.want { t.Errorf("%s %s: %d rows, want %d", c.host, c.op, n, c.want) } } } // Final fix, ruling 2: the purge's own run (Resume) drops the rows of the // incarnations its journal records, from a retired journal on. The world's // app has no instance the fixture can purge (an instance's host purge needs // the boxes), so the journal gains one, already retired, the moment it is // "retired"; Resume then runs from there to done. func TestPurgeResumeDropsItsHostsMoveLogRows(t *testing.T) { cx := newClaimCaseWith(t) cx.purgeWorld(t) cx.quiesce() queueTestMoveLog(t, cx.st, "gone.example.com", "mv_1", "done", "inc-gone") queueTestMoveLog(t, cx.st, "gone.example.com", "mv_2", "done", "inc-earlier") v := url.Values{"csrf": {cx.csrf}, "app": {"titogo"}, "confirm": {"titogo"}} if status, _, out := postGated(t, cx.consoleFixture, cx.browser, cx.ts.URL+"/a/bab/trash/delete", v); status != http.StatusAccepted { t.Fatalf("purge answered %d: %s", status, out) } a, err := hostops.ReadCanonicalAdmission(context.Background(), cx.rel, "bab", "titogo") if err != nil || a.Value.Op == nil { t.Fatalf("no accepted purge: %v", err) } op, appID := a.Value.Op.ID, a.Value.AppID cx.publishPurgeCensus(t, op) injected := false for i := 0; i < 12 && cx.ownsPurge(t, "bab", "titogo", op); i++ { if j, tag, err := hostops.ReadPurgeJournal(context.Background(), cx.rel, "bab", "titogo", op, appID); err == nil && j.Phase == "retired" && !injected { if j.Hosts == nil { j.Hosts = map[string]hostops.PurgeHost{} } j.Hosts["gone.example.com"] = hostops.PurgeHost{Holder: &hostclaim.Holder{Incarnation: "inc-gone"}, Retired: true} if _, err := cx.srv.cfg.ClaimAuthority.Control.SavePurgeJournal(context.Background(), j, tag); err != nil { t.Fatalf("record the retired host in the journal: %v", err) } injected = true } ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) if err := cx.srv.RunClaimRecoveryPass(ctx); err != nil { t.Logf("purge %s recovery pass %d: %v", op, i, err) } cancel() } if !injected { t.Fatal("the purge never stood at retired") } if cx.ownsPurge(t, "bab", "titogo", op) { t.Fatalf("purge %s did not finish", op) } if n := moveLogRows(t, cx.st, "gone.example.com", "mv_1"); n != 0 { t.Fatalf("%d rows of the purged incarnation remain", n) } if n := moveLogRows(t, cx.st, "gone.example.com", "mv_2"); n != 1 { t.Fatalf("another incarnation's rows: %d, want 1", n) } }