rootfs apps plan 3: Task 5 round 3, the layout machinery never applies to a row with a runtime: one predicate, asked first
activator.LayoutExempt (Runtime != "") is the one predicate. Classify
asks it before reading a journal seam, so a journal of any shape never
fences a runtime row. Every internal/layout entry point that has a row
(convergeHost, restoreHold, recheck, shift, recoverHost, Held, and the
pass's exempt itself) asks it before it parses a journal or honours a
fence; recoverHost asks before ReadJournal, so a malformed or {}
journal is let go too. The pass's exempt replaces keptRootfs and
dropRootfsJournal: it removes a journal unread, lifts a fence this pass
kept, drops a hold and its marker, and leaves other operations' fences
alone. Converge lets runtime rows go even when the isolation gate
refuses. Held takes the row.
TestLayoutMachineryAsksLayoutExemptFirst enumerates every function in
internal/layout that reads a journal or holds a fence and every
activator function that reads a journal seam or answers LayoutFenced,
and fails unless each asks the predicate first; fixtures prove it
catches each violation.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
6 files changed,
+752
−119
internal/activator/layout.go+16 −1internal/host/layoutleg.go+1 −1internal/layout/layout_exempt_guard_test.go+412 −0internal/layout/pass.go+104 −60internal/layout/pass_linux_test.go+6 −6internal/layout/rootfs_pass_test.go+213 −51
diff --git a/internal/activator/layout.go b/internal/activator/layout.go| index f743512f4..81d27b0e4 100644 |
| --- a/internal/activator/layout.go |
| +++ b/internal/activator/layout.go |
| @@ -66,6 +66,18 @@ func ActivatorManaged(rt registry.Route) bool { |
| return rt.ActivatorOwned() && rt.Socket != "" && rt.Backing == "" && rt.Tunnel == "" |
| } |
| +// LayoutExempt is the one predicate of the layout machinery's invariant: a |
| +// row with a runtime (rootfs-v1, or a runtime this binary does not know) is |
| +// never the layout pass's. It is minted nested on every box and is never |
| +// moved, so no layout journal, hold or fence can be the pass's for it: a |
| +// journal naming it is planted or left by an earlier life on the host |
| +// name. Classify ignores such a journal for it, and every entry point of |
| +// internal/layout asks this (through Pass.exempt) before it parses a |
| +// journal or honours a fence. Fences of other operations (an instance |
| +// move's cut) are not the layout machinery's and are untouched. |
| +// TestLayoutMachineryAsksLayoutExemptFirst holds every reader to it. |
| +func LayoutExempt(rt registry.Route) bool { return rt.Runtime != "" } |
| + |
| // Classifier lets doWake ask a backend for a route's layout before it does |
| // anything the layout might forbid: acquiring the lease or restoring a |
| // database. A backend that does not implement it (a test double, say) is |
| @@ -77,8 +89,11 @@ type Classifier interface { |
| // Classify returns the layout and, for anything but flat and nested, the |
| // reason a member or operator sees. |
| +// |
| +// A row LayoutExempt names is never fenced by a layout journal: the journal |
| +// is not read for it at all. |
| func Classify(rt registry.Route, in LayoutInputs) (Layout, string) { |
| - if in.Journal != nil && in.Journal(rt.Host) { |
| + if !LayoutExempt(rt) && in.Journal != nil && in.Journal(rt.Host) { |
| return LayoutFenced, "layout migration in flight; see " + provision.LayoutJournalPath(in.DataDir, rt.Host) |
| } |
| if in.Peers != nil { |
diff --git a/internal/host/layoutleg.go b/internal/host/layoutleg.go| index 296bfc5ff..68a6b5ee2 100644 |
| --- a/internal/host/layoutleg.go |
| +++ b/internal/host/layoutleg.go |
| @@ -203,7 +203,7 @@ func layoutStatusReport(routes []registry.Route) *status.LayoutReport { |
| // A flat host the pass keeps fenced and down after a refused |
| // nested move classifies flat (no journal), but it does not |
| // wake: status says so, loudly, with the refusal. |
| - if why, held := pass.Held(rt.Host); held && word == activator.LayoutFlat { |
| + if why, held := pass.Held(rt); held && word == activator.LayoutFlat { |
| word, reason = activator.LayoutFenced, "held down after a refused move to nested: "+why |
| } |
| rows[rt.Host] = status.RowLayout{Layout: string(word), Reason: reason, Note: pass.Note(rt.Host)} |
diff --git a/internal/layout/layout_exempt_guard_test.go b/internal/layout/layout_exempt_guard_test.go| new file mode 100644 |
| index 000000000..fe90cbc14 |
| --- /dev/null |
| +++ b/internal/layout/layout_exempt_guard_test.go |
| @@ -0,0 +1,412 @@ |
| +package layout |
| + |
| +// The layout machinery's runtime invariant (task 5, round 3, Paul's ruling |
| +// under stopping rule 2): the layout machinery never applies to a row with |
| +// a runtime. One predicate, activator.LayoutExempt, asked first: before a |
| +// journal is parsed and before a fence is honoured. This guard holds every |
| +// reader to it, in the style of the activator's Plan 3 invariant guards: a |
| +// structural check over the package's own source, with fixtures that prove |
| +// it fails on each kind of violation. |
| +// |
| +// Its tables only grow. A function that comes to read a journal or hold a |
| +// fence is added to layoutGuarded (it asks the predicate first) or to |
| +// layoutInternal (every call of it is reached from a guarded function, |
| +// after the predicate); the completeness check fails until it is. |
| + |
| +import ( |
| + "go/ast" |
| + "go/parser" |
| + "go/token" |
| + "path/filepath" |
| + "sort" |
| + "strings" |
| + "testing" |
| +) |
| + |
| +// layoutPrimitiveFuncs are the package functions that read, write or act |
| +// on a journal, or move a host's files. |
| +var layoutPrimitiveFuncs = map[string]bool{ |
| + "ReadJournal": true, "JournalPresent": true, "ReplaceJournal": true, "UpdatePhase": true, |
| + "WriteJournal": true, "RemoveJournal": true, "Validate": true, "applyJournal": true, "EnsureDirs": true, |
| +} |
| + |
| +// layoutPrimitiveMethods are the pass's methods that honour, keep or lift |
| +// a fence, classify through a journal, or move a host. |
| +var layoutPrimitiveMethods = map[string]bool{ |
| + "quiesce": true, "keep": true, "keptFence": true, "release": true, "releaseHeld": true, |
| + "migrateHost": true, "commit": true, "commitWritten": true, "complete": true, "moveFiles": true, |
| + "Classify": true, "layoutInputs": true, |
| +} |
| + |
| +// layoutGuarded are the entry points that have a row: each asks the |
| +// predicate (activator.LayoutExempt, or the pass's exempt) before its first |
| +// primitive. |
| +var layoutGuarded = []string{"exempt", "convergeHost", "restoreHold", "recheck", "shift", "recoverHost", "Held"} |
| + |
| +// layoutInternal are the primitives' helpers that have no row: every call |
| +// of each is inside a guarded function, after its predicate, or inside |
| +// another internal helper. |
| +var layoutInternal = []string{"migrateHost", "releaseHeld", "quiesce", "keep", "keptFence", "release", "commit", "commitWritten", "complete", "moveFiles"} |
| + |
| +// layoutSeams hand a journal seam to activator.Classify, which asks the |
| +// predicate itself (checked by activatorFenceViolations): each may call |
| +// only the primitives listed for it. |
| +var layoutSeams = map[string][]string{ |
| + "Classify": {"layoutInputs", "activator.Classify"}, |
| + "layoutInputs": {"JournalPresent"}, |
| +} |
| + |
| +// layoutLibraryFiles are the journal's own file operations, the primitives |
| +// themselves. |
| +var layoutLibraryFiles = []string{"journal.go", "journal_linux.go", "journal_other.go"} |
| + |
| +type guardConfig struct { |
| + guarded, internal []string |
| + seams map[string][]string |
| + library []string |
| +} |
| + |
| +func layoutConfig() guardConfig { |
| + return guardConfig{guarded: layoutGuarded, internal: layoutInternal, seams: layoutSeams, library: layoutLibraryFiles} |
| +} |
| + |
| +// primitiveName names call c if it is a primitive, else "". |
| +func primitiveName(c *ast.CallExpr) string { |
| + switch fn := c.Fun.(type) { |
| + case *ast.Ident: |
| + if layoutPrimitiveFuncs[fn.Name] { |
| + return fn.Name |
| + } |
| + case *ast.SelectorExpr: |
| + if x, ok := fn.X.(*ast.Ident); ok { |
| + if x.Name == "activator" && fn.Sel.Name == "Classify" { |
| + return "activator.Classify" |
| + } |
| + if x.Name == "p" && layoutPrimitiveMethods[fn.Sel.Name] { |
| + return fn.Sel.Name |
| + } |
| + } |
| + if inner, ok := fn.X.(*ast.SelectorExpr); ok && inner.Sel.Name == "Fence" { |
| + return "Fence." + fn.Sel.Name |
| + } |
| + } |
| + return "" |
| +} |
| + |
| +// isPredicate reports whether c asks the invariant's predicate. |
| +func isPredicate(c *ast.CallExpr) bool { |
| + sel, ok := c.Fun.(*ast.SelectorExpr) |
| + if !ok { |
| + return false |
| + } |
| + x, ok := sel.X.(*ast.Ident) |
| + return ok && ((x.Name == "activator" && sel.Sel.Name == "LayoutExempt") || (x.Name == "p" && sel.Sel.Name == "exempt")) |
| +} |
| + |
| +// calleeName is the package function or pass method c calls, or "". |
| +func calleeName(c *ast.CallExpr) string { |
| + switch fn := c.Fun.(type) { |
| + case *ast.Ident: |
| + return fn.Name |
| + case *ast.SelectorExpr: |
| + if x, ok := fn.X.(*ast.Ident); ok && x.Name == "p" { |
| + return fn.Sel.Name |
| + } |
| + } |
| + return "" |
| +} |
| + |
| +type funcFacts struct { |
| + file string |
| + predicate token.Pos // first predicate call, NoPos if none |
| + primitives []token.Pos // every primitive call, in order |
| + primNames []string |
| + calls map[string][]token.Pos // callee -> call positions |
| +} |
| + |
| +func collectFacts(files map[string]*ast.File) map[string]*funcFacts { |
| + out := map[string]*funcFacts{} |
| + for name, f := range files { |
| + for _, d := range f.Decls { |
| + fd, ok := d.(*ast.FuncDecl) |
| + if !ok || fd.Body == nil { |
| + continue |
| + } |
| + ff := &funcFacts{file: name, calls: map[string][]token.Pos{}} |
| + ast.Inspect(fd.Body, func(n ast.Node) bool { |
| + c, ok := n.(*ast.CallExpr) |
| + if !ok { |
| + return true |
| + } |
| + if isPredicate(c) && ff.predicate == token.NoPos { |
| + ff.predicate = c.Pos() |
| + } |
| + if pn := primitiveName(c); pn != "" { |
| + ff.primitives = append(ff.primitives, c.Pos()) |
| + ff.primNames = append(ff.primNames, pn) |
| + } |
| + if cn := calleeName(c); cn != "" { |
| + ff.calls[cn] = append(ff.calls[cn], c.Pos()) |
| + } |
| + return true |
| + }) |
| + out[fd.Name.Name] = ff |
| + } |
| + } |
| + return out |
| +} |
| + |
| +// layoutGuardViolations checks files (name -> parsed file) against cfg. |
| +func layoutGuardViolations(files map[string]*ast.File, cfg guardConfig) []string { |
| + facts := collectFacts(files) |
| + var out []string |
| + in := func(list []string, s string) bool { |
| + for _, x := range list { |
| + if x == s { |
| + return true |
| + } |
| + } |
| + return false |
| + } |
| + // Each guarded function asks the predicate before its first primitive. |
| + for _, g := range cfg.guarded { |
| + ff, ok := facts[g] |
| + if !ok { |
| + out = append(out, g+": guarded entry point not found") |
| + continue |
| + } |
| + if ff.predicate == token.NoPos { |
| + out = append(out, g+": never asks activator.LayoutExempt (or the pass's exempt)") |
| + continue |
| + } |
| + for i, pos := range ff.primitives { |
| + if pos < ff.predicate { |
| + out = append(out, g+": calls "+ff.primNames[i]+" before it asks the predicate") |
| + } |
| + } |
| + } |
| + // Every call of an internal helper is inside a guarded function after |
| + // its predicate, or inside another internal helper. |
| + for _, h := range cfg.internal { |
| + if _, ok := facts[h]; !ok { |
| + out = append(out, h+": internal helper not found") |
| + continue |
| + } |
| + for caller, ff := range facts { |
| + for _, pos := range ff.calls[h] { |
| + switch { |
| + case in(cfg.internal, caller): |
| + case in(cfg.guarded, caller) && ff.predicate != token.NoPos && pos > ff.predicate: |
| + case in(cfg.guarded, caller): |
| + out = append(out, caller+": calls "+h+" before it asks the predicate") |
| + default: |
| + out = append(out, caller+": calls the internal helper "+h+" without asking the predicate") |
| + } |
| + } |
| + } |
| + } |
| + // Seams call only their listed primitives. |
| + for s, allowed := range cfg.seams { |
| + ff, ok := facts[s] |
| + if !ok { |
| + out = append(out, s+": seam not found") |
| + continue |
| + } |
| + for _, pn := range ff.primNames { |
| + if !in(allowed, pn) { |
| + out = append(out, s+": seam calls "+pn) |
| + } |
| + } |
| + } |
| + // Completeness: every function that calls a primitive is classified. |
| + for name, ff := range facts { |
| + if len(ff.primitives) == 0 || in(cfg.guarded, name) || in(cfg.internal, name) || in(cfg.library, ff.file) { |
| + continue |
| + } |
| + if _, seam := cfg.seams[name]; seam { |
| + continue |
| + } |
| + out = append(out, name+": calls "+strings.Join(ff.primNames, ", ")+" and is in no table (guard it, or list it internal)") |
| + } |
| + sort.Strings(out) |
| + return out |
| +} |
| + |
| +// activatorFenceViolations checks the activator's layout fence path: every |
| +// function that calls a LayoutInputs Journal seam, or returns LayoutFenced, |
| +// asks LayoutExempt first. found counts the functions it checked. |
| +func activatorFenceViolations(files map[string]*ast.File) (out []string, found int) { |
| + for _, f := range files { |
| + for _, d := range f.Decls { |
| + fd, ok := d.(*ast.FuncDecl) |
| + if !ok || fd.Body == nil { |
| + continue |
| + } |
| + pred := token.NoPos |
| + var reads []token.Pos |
| + ast.Inspect(fd.Body, func(n ast.Node) bool { |
| + switch n := n.(type) { |
| + case *ast.CallExpr: |
| + if id, ok := n.Fun.(*ast.Ident); ok && id.Name == "LayoutExempt" && pred == token.NoPos { |
| + pred = n.Pos() |
| + } |
| + if sel, ok := n.Fun.(*ast.SelectorExpr); ok && sel.Sel.Name == "Journal" { |
| + reads = append(reads, n.Pos()) |
| + } |
| + case *ast.ReturnStmt: |
| + for _, r := range n.Results { |
| + if id, ok := r.(*ast.Ident); ok && id.Name == "LayoutFenced" { |
| + reads = append(reads, n.Pos()) |
| + } |
| + } |
| + } |
| + return true |
| + }) |
| + if len(reads) == 0 { |
| + continue |
| + } |
| + found++ |
| + for _, pos := range reads { |
| + if pred == token.NoPos || pos < pred { |
| + out = append(out, fd.Name.Name+": reads a layout journal or answers LayoutFenced before it asks LayoutExempt") |
| + break |
| + } |
| + } |
| + } |
| + } |
| + sort.Strings(out) |
| + return out, found |
| +} |
| + |
| +func parseDir(t *testing.T, dir string) map[string]*ast.File { |
| + t.Helper() |
| + paths, err := filepath.Glob(filepath.Join(dir, "*.go")) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + fset := token.NewFileSet() |
| + out := map[string]*ast.File{} |
| + for _, path := range paths { |
| + if strings.HasSuffix(path, "_test.go") { |
| + continue |
| + } |
| + f, err := parser.ParseFile(fset, path, nil, 0) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + out[filepath.Base(path)] = f |
| + } |
| + return out |
| +} |
| + |
| +func parseSources(t *testing.T, srcs map[string]string) map[string]*ast.File { |
| + t.Helper() |
| + fset := token.NewFileSet() |
| + out := map[string]*ast.File{} |
| + for name, src := range srcs { |
| + f, err := parser.ParseFile(fset, name, "package layout\n"+src, 0) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + out[name] = f |
| + } |
| + return out |
| +} |
| + |
| +// TestLayoutMachineryAsksLayoutExemptFirst is the guard over the real |
| +// source of internal/layout and the activator's layout fence path. |
| +func TestLayoutMachineryAsksLayoutExemptFirst(t *testing.T) { |
| + if v := layoutGuardViolations(parseDir(t, "."), layoutConfig()); len(v) != 0 { |
| + t.Errorf("internal/layout:\n %s", strings.Join(v, "\n ")) |
| + } |
| + v, found := activatorFenceViolations(parseDir(t, filepath.Join("..", "activator"))) |
| + if found == 0 { |
| + t.Error("internal/activator: no layout fence path found (Classify reads the journal seam); the guard reads nothing") |
| + } |
| + if len(v) != 0 { |
| + t.Errorf("internal/activator:\n %s", strings.Join(v, "\n ")) |
| + } |
| +} |
| + |
| +// TestLayoutExemptGuardCatchesEachViolation proves the guard fails on each |
| +// kind of violation, on fixtures, and passes a compliant one. |
| +func TestLayoutExemptGuardCatchesEachViolation(t *testing.T) { |
| + cfg := guardConfig{guarded: []string{"entry"}, internal: []string{"quiesce"}, seams: map[string][]string{}, library: []string{"journal.go"}} |
| + compliant := map[string]string{ |
| + "pass.go": ` |
| +func (p *Pass) entry(rt Route) { |
| + if p.exempt(rt) { return } |
| + ReadJournal("d", rt.Host) |
| + p.quiesce() |
| +} |
| +func (p *Pass) quiesce() { p.Fence.LiftFence("h", 1) } |
| +func (p *Pass) other() { p.entry(Route{}) }`, |
| + "journal.go": `func ReplaceJournal() { ReadJournal("d", "h") }`, |
| + } |
| + if v := layoutGuardViolations(parseSources(t, compliant), cfg); len(v) != 0 { |
| + t.Fatalf("compliant fixture: %v", v) |
| + } |
| + for name, c := range map[string]struct { |
| + src string |
| + want string |
| + }{ |
| + "primitive before the predicate": {` |
| +func (p *Pass) entry(rt Route) { |
| + ReadJournal("d", rt.Host) |
| + if p.exempt(rt) { return } |
| +} |
| +func (p *Pass) quiesce() {}`, "entry: calls ReadJournal before it asks the predicate"}, |
| + "fence before the predicate": {` |
| +func (p *Pass) entry(rt Route) { |
| + p.Fence.FenceAndStopIfUnfenced(nil, rt.Host, 1) |
| + if activator.LayoutExempt(rt) { return } |
| +} |
| +func (p *Pass) quiesce() {}`, "entry: calls Fence.FenceAndStopIfUnfenced before it asks the predicate"}, |
| + "no predicate": {` |
| +func (p *Pass) entry(rt Route) { JournalPresent("d", rt.Host) } |
| +func (p *Pass) quiesce() {}`, "entry: never asks activator.LayoutExempt (or the pass's exempt)"}, |
| + "unclassified reader": {` |
| +func (p *Pass) entry(rt Route) { if p.exempt(rt) { return } } |
| +func (p *Pass) quiesce() {} |
| +func (p *Pass) sneak(host string) { ReadJournal("d", host) }`, "sneak: calls ReadJournal and is in no table (guard it, or list it internal)"}, |
| + "internal helper from an unguarded function": {` |
| +func (p *Pass) entry(rt Route) { if p.exempt(rt) { return } } |
| +func (p *Pass) quiesce() { p.Fence.LiftFence("h", 1) } |
| +func (p *Pass) sneak(host string) { p.quiesce() }`, "sneak: calls the internal helper quiesce without asking the predicate"}, |
| + "internal helper before the predicate": {` |
| +func (p *Pass) entry(rt Route) { |
| + p.quiesce() |
| + if p.exempt(rt) { return } |
| +} |
| +func (p *Pass) quiesce() { p.Fence.LiftFence("h", 1) }`, "entry: calls quiesce before it asks the predicate"}, |
| + } { |
| + v := layoutGuardViolations(parseSources(t, map[string]string{"pass.go": c.src}), cfg) |
| + ok := false |
| + for _, s := range v { |
| + ok = ok || s == c.want |
| + } |
| + if !ok { |
| + t.Errorf("%s: violations %v, want %q among them", name, v, c.want) |
| + } |
| + } |
| + |
| + act := func(src string) []string { |
| + v, _ := activatorFenceViolations(parseSources(t, map[string]string{"layout.go": src})) |
| + return v |
| + } |
| + if v := act(`func Classify(rt Route, in LayoutInputs) (Layout, string) { |
| + if !LayoutExempt(rt) && in.Journal != nil && in.Journal(rt.Host) { return LayoutFenced, "" } |
| + return LayoutFlat, "" |
| +}`); len(v) != 0 { |
| + t.Errorf("compliant activator fixture: %v", v) |
| + } |
| + if v := act(`func Classify(rt Route, in LayoutInputs) (Layout, string) { |
| + if in.Journal != nil && in.Journal(rt.Host) { return LayoutFenced, "" } |
| + return LayoutFlat, "" |
| +}`); len(v) == 0 { |
| + t.Error("an activator journal read with no LayoutExempt passed") |
| + } |
| + if v := act(`func held(rt Route) Layout { return LayoutFenced }`); len(v) == 0 { |
| + t.Error("an activator LayoutFenced answer with no LayoutExempt passed") |
| + } |
| +} |
diff --git a/internal/layout/pass.go b/internal/layout/pass.go| index 762c183fc..5fcc468eb 100644 |
| --- a/internal/layout/pass.go |
| +++ b/internal/layout/pass.go |
| @@ -113,8 +113,8 @@ type Pass struct { |
| // Result is what the pass says about one host. Outcome is one of |
| // migrated, reverted, recovered, unchanged, skipped, deferred, refused, |
| // held (refused after the host was stopped for a nested move, and kept |
| -// fenced and down; see migrateHost) or kept (a rootfs row, which the pass |
| -// never moves: keptRootfs). A Result with an empty Host is about |
| +// fenced and down; see migrateHost) or kept (a row with a runtime, which |
| +// the layout machinery never applies to: exempt). A Result with an empty Host is about |
| // the whole pass. |
| type Result struct { |
| Host string |
| @@ -224,17 +224,64 @@ func deadline(ctx context.Context) (context.Context, context.CancelFunc) { |
| return context.WithTimeout(ctx, quiesceTimeout) |
| } |
| -// keptRootfs is the pass's answer for a row with a runtime: kept where it |
| -// is, in both modes. A rootfs instance is always nested (anti-goal §2; |
| -// reconcile mints it so on every box), and ActivatorManaged names a |
| -// resident, so without this the pass would fence it and move it flat. An |
| -// unknown runtime is fixed and inert, and is not the pass's to move |
| -// either. |
| -func keptRootfs(rt registry.Route) (Result, bool) { |
| - if rt.Runtime == "" { |
| +// exempt is the pass's one answer for a row activator.LayoutExempt names |
| +// (a row with a runtime): the layout machinery never applies to it. Every |
| +// entry point that has a row calls it before it parses a journal or |
| +// honours a fence (TestLayoutMachineryAsksLayoutExemptFirst). It is kept |
| +// where it is, in both modes: a rootfs instance is always nested |
| +// (anti-goal §2; reconcile mints it so on every box), and an unknown |
| +// runtime is fixed and inert. |
| +// |
| +// What the layout machinery holds for the host name is let go without |
| +// being read or acted on: a journal is removed unread (the pass never |
| +// writes one for such a row, so it is planted or an earlier life's), a |
| +// fence this pass kept (keptFence: the activator still holds it at the |
| +// pass's generation) is lifted, and a hold and its marker are dropped. A |
| +// fence of another operation is not the pass's and is left alone. Each is |
| +// logged, since each is something an operator may want to know was there. |
| +func (p *Pass) exempt(rt registry.Route) (Result, bool) { |
| + if !activator.LayoutExempt(rt) { |
| return Result{}, false |
| } |
| - return Result{rt.Host, "kept", "rootfs instances are always nested"}, true |
| + host := rt.Host |
| + if JournalPresent(p.DataDir, host) { |
| + var err error |
| + p.serialize(func() { err = RemoveJournal(p.DataDir, host) }) |
| + if err != nil { |
| + p.logf("layout: %s: a layout journal names a row with a runtime; removing it unread failed: %v (it fences nothing: the row is exempt)", host, err) |
| + } else { |
| + p.logf("layout: %s: a layout journal named a row with a runtime; removed unread, nothing moved", host) |
| + } |
| + } |
| + if gen, ok := p.keptFence(host); ok { |
| + p.logf("layout: %s: the fence this pass kept is lifted: the row has a runtime", host) |
| + p.release(host, gen) |
| + } |
| + p.keptMu.Lock() |
| + _, held := p.held[host] |
| + delete(p.held, host) |
| + p.keptMu.Unlock() |
| + if held || HoldMarkerPresent(p.DataDir, host) { |
| + removeHoldMarker(p.DataDir, host) |
| + } |
| + p.forgetAwake(host) |
| + return Result{host, "kept", "rootfs instances are always nested"}, true |
| +} |
| + |
| +// exemptRows is exempt over every row of the box not already reported |
| +// (seen), which it marks. |
| +func (p *Pass) exemptRows(routes []registry.Route, seen map[string]bool) []Result { |
| + var out []Result |
| + for _, rt := range routes { |
| + if seen[rt.Host] { |
| + continue |
| + } |
| + if r, ok := p.exempt(rt); ok { |
| + seen[rt.Host] = true |
| + out = append(out, r) |
| + } |
| + } |
| + return out |
| } |
| // ready is the status a row must have for the pass to move it. Any other |
| @@ -258,30 +305,33 @@ func (p *Pass) Converge(ctx context.Context, mode Mode) []Result { |
| } |
| gate := p.nestedGate() |
| out := p.recover(ctx, mode, gate) |
| + seen := map[string]bool{} |
| + for _, r := range out { |
| + seen[r.Host] = true |
| + } |
| if mode == ModeNested { |
| if err := gate(); err != nil { |
| + // The rows with a runtime are let go even so: what the layout |
| + // machinery holds for them is not gated on isolation. |
| + if routes, lerr := p.Registry.List(); lerr == nil { |
| + sort.Slice(routes, func(i, j int) bool { return routes[i].Host < routes[j].Host }) |
| + out = append(out, p.exemptRows(routes, seen)...) |
| + } |
| return append(out, Result{Outcome: "refused", Reason: "isolation not ready: " + err.Error()}) |
| } |
| } |
| - seen := map[string]bool{} |
| - for _, r := range out { |
| - seen[r.Host] = true |
| - } |
| routes, err := p.Registry.List() |
| if err != nil { |
| p.logf("layout: converge: list routes: %v", err) |
| return append(out, Result{Outcome: "refused", Reason: "list routes: " + err.Error()}) |
| } |
| sort.Slice(routes, func(i, j int) bool { return routes[i].Host < routes[j].Host }) |
| + out = append(out, p.exemptRows(routes, seen)...) |
| migrated := false |
| for _, rt := range routes { |
| if !activator.ActivatorManaged(rt) || seen[rt.Host] { |
| continue |
| } |
| - if r, kept := keptRootfs(rt); kept { |
| - out = append(out, r) |
| - continue |
| - } |
| if ctx.Err() != nil { |
| break |
| } |
| @@ -302,6 +352,9 @@ func (p *Pass) Converge(ctx context.Context, mode Mode) []Result { |
| } |
| func (p *Pass) convergeHost(ctx context.Context, rt registry.Route, routes []registry.Route, mode Mode) Result { |
| + if r, ok := p.exempt(rt); ok { |
| + return r |
| + } |
| if !ready(rt) { |
| return Result{rt.Host, "deferred", "status " + rt.Status} |
| } |
| @@ -544,16 +597,17 @@ func (p *Pass) restoreHold(ctx context.Context, host string) Result { |
| if err != nil { |
| return Result{host, "refused", "list routes: " + err.Error()} |
| } |
| + if rt != nil { |
| + // The pass never holds a row with a runtime (it never moves one), |
| + // so a marker naming it is not the pass's and restores nothing. |
| + if r, ok := p.exempt(*rt); ok { |
| + return r |
| + } |
| + } |
| if rt == nil || !activator.ActivatorManaged(*rt) { |
| removeHoldMarker(p.DataDir, host) |
| return Result{host, "released", "hold marker dropped: the row is gone or no longer managed"} |
| } |
| - if r, kept := keptRootfs(*rt); kept { |
| - // The pass never holds a rootfs row (it never moves one), so a |
| - // marker naming it is not the pass's and restores nothing. |
| - removeHoldMarker(p.DataDir, host) |
| - return r |
| - } |
| if layout, _ := p.Classify(*rt, routes); layout != activator.LayoutFlat { |
| removeHoldMarker(p.DataDir, host) |
| return Result{host, "released", "hold marker dropped: the row is " + string(layout)} |
| @@ -582,8 +636,8 @@ func (p *Pass) restoreHold(ctx context.Context, host string) Result { |
| // would go ahead. |
| func (p *Pass) recheck(host string, routes []registry.Route) (reason string, plantable bool) { |
| for _, rt := range routes { |
| - if r, kept := keptRootfs(rt); kept && rt.Host == host { |
| - return r.Reason, false |
| + if rt.Host == host && activator.LayoutExempt(rt) { |
| + return "rootfs instances are always nested", false |
| } |
| } |
| plan, err := PlanFor(p.DataDir, p.socketDir(), host, ModeNested) |
| @@ -614,10 +668,15 @@ func hostsOf(routes []registry.Route) []string { |
| return hosts |
| } |
| -// Held reports whether the pass keeps host fenced and down after a |
| +// Held reports whether the pass keeps rt's host fenced and down after a |
| // refused nested move, and why. The fence must still be the pass's own: |
| -// a record the activator no longer matches is dropped. |
| -func (p *Pass) Held(host string) (string, bool) { |
| +// a record the activator no longer matches is dropped. A row with a |
| +// runtime is never held (exempt lets its hold go at the next pass). |
| +func (p *Pass) Held(rt registry.Route) (string, bool) { |
| + if activator.LayoutExempt(rt) { |
| + return "", false |
| + } |
| + host := rt.Host |
| if _, ok := p.keptFence(host); !ok { |
| p.keptMu.Lock() |
| delete(p.held, host) |
| @@ -770,15 +829,15 @@ func (p *Pass) shift(host string, mode Mode, prior *Journal) (res Result, h hand |
| if fresh == nil { |
| return Result{host, "refused", "row vanished under the fence"}, handoverNone, false |
| } |
| + if r, ok := p.exempt(*fresh); ok { |
| + return r, handoverNone, false |
| + } |
| if !ready(*fresh) { |
| return Result{host, "refused", "status " + fresh.Status + " under the fence"}, handoverNone, false |
| } |
| if !activator.ActivatorManaged(*fresh) { |
| return Result{host, "refused", "no longer activator-managed under the fence"}, handoverNone, false |
| } |
| - if r, kept := keptRootfs(*fresh); kept { |
| - return r, handoverNone, false |
| - } |
| routes, err := p.Registry.List() |
| if err != nil { |
| return Result{host, "refused", "list routes: " + err.Error()}, handoverNone, false |
| @@ -1070,25 +1129,6 @@ func (p *Pass) recover(ctx context.Context, intent Mode, gate func() error) []Re |
| return out |
| } |
| -// dropRootfsJournal is recovery's answer for a journal naming a row with a |
| -// runtime (keptRootfs): the pass never writes one, so it is planted or left |
| -// by another life, and honouring it would stop a rootfs host and move its |
| -// files. It is refused loudly and removed without moving anything, so the |
| -// journal does not keep the host fenced for good. |
| -func (p *Pass) dropRootfsJournal(host string, rt registry.Route) (Result, bool) { |
| - kept, ok := keptRootfs(rt) |
| - if !ok { |
| - return Result{}, false |
| - } |
| - var err error |
| - p.serialize(func() { err = RemoveJournal(p.DataDir, host) }) |
| - if err != nil { |
| - return Result{host, "refused", fmt.Sprintf("a layout journal names it, but %s, and the journal could not be removed: %v; not acting", kept.Reason, err)}, true |
| - } |
| - reason := fmt.Sprintf("a layout journal named it, but %s: the journal is removed and nothing moved", kept.Reason) |
| - return Result{host, "refused", reason}, true |
| -} |
| - |
| // nestedGate asks NestedOK at most once, on first use, and remembers the |
| // answer for the rest of one pass. |
| func (p *Pass) nestedGate() func() error { |
| @@ -1141,6 +1181,15 @@ func (p *Pass) recoverHost(ctx context.Context, host string, byHost map[string]r |
| if !controlname.ValidHostShape(host) { |
| return Result{host, "refused", "journal file name is not a host; not acting"} |
| } |
| + // A row with a runtime is never the layout machinery's, so its journal |
| + // is let go before it is read: a malformed or empty one too, which |
| + // would otherwise be refused here and stay. |
| + rt, ok := byHost[host] |
| + if ok { |
| + if r, exempt := p.exempt(rt); exempt { |
| + return r |
| + } |
| + } |
| j, found, err := ReadJournal(p.DataDir, host) |
| if err != nil { |
| return Result{host, "refused", notActing(err)} |
| @@ -1148,16 +1197,9 @@ func (p *Pass) recoverHost(ctx context.Context, host string, byHost map[string]r |
| if !found { |
| return Result{host, "unchanged", ""} |
| } |
| - rt, ok := byHost[host] |
| if !ok { |
| return Result{host, "refused", "journal names a host with no row; not acting"} |
| } |
| - // A row with a runtime is never the pass's to move, so no journal can |
| - // be one it wrote: checked before the stop, so a planted journal never |
| - // costs a rootfs host a stop, and again on the row under the fence. |
| - if r, kept := p.dropRootfsJournal(host, rt); kept { |
| - return r |
| - } |
| // Checked before the stop as well as after it, so a journal that |
| // cannot be honoured never costs its host a stop. |
| if _, err := Validate(j, p.DataDir, p.socketDir(), rt); err != nil { |
| @@ -1201,7 +1243,9 @@ func (p *Pass) recoverHost(ctx context.Context, host string, byHost map[string]r |
| if fresh == nil { |
| return Result{host, "refused", "row vanished under the fence; not acting"} |
| } |
| - if r, kept := p.dropRootfsJournal(host, *fresh); kept { |
| + // Again on the row as it stands under the fence: a new life minted |
| + // with a runtime while the journal waited is let go the same way. |
| + if r, exempt := p.exempt(*fresh); exempt { |
| return r |
| } |
| atFrom, err := Validate(j, p.DataDir, p.socketDir(), *fresh) |
diff --git a/internal/layout/pass_linux_test.go b/internal/layout/pass_linux_test.go| index 3ea217b01..e9a22a2e5 100644 |
| --- a/internal/layout/pass_linux_test.go |
| +++ b/internal/layout/pass_linux_test.go |
| @@ -1457,7 +1457,7 @@ func TestConvergeQuarantinesPlantedEntriesAndMigrates(t *testing.T) { |
| if n := p.Note(host); !strings.Contains(n, "kept aside") || !strings.Contains(n, "creds.q-") { |
| t.Errorf("note = %q", n) |
| } |
| - if _, held := p.Held(host); held { |
| + if _, held := p.Held(registry.Route{Host: host}); held { |
| t.Error("a quarantined entry became a hold") |
| } |
| } |
| @@ -1484,7 +1484,7 @@ func TestConvergeHoldsAFlatHostWhoseMoveWasRefusedAfterItsStop(t *testing.T) { |
| if f.fences[host] == 0 { |
| t.Fatal("the fence was lifted; the host would wake flat on its next request") |
| } |
| - if why, held := p.Held(host); !held || !strings.Contains(why, "2 names") { |
| + if why, held := p.Held(registry.Route{Host: host}); !held || !strings.Contains(why, "2 names") { |
| t.Fatalf("Held = %q %v", why, held) |
| } |
| // The tenant keeps the link alive: every pass meets the same refusal, |
| @@ -1508,7 +1508,7 @@ func TestConvergeHoldsAFlatHostWhoseMoveWasRefusedAfterItsStop(t *testing.T) { |
| if res[host].Outcome != "unchanged" || f.fences[host] != 0 { |
| t.Fatalf("flat intent: %+v fence=%d", res[host], f.fences[host]) |
| } |
| - if _, held := p.Held(host); held { |
| + if _, held := p.Held(registry.Route{Host: host}); held { |
| t.Fatal("still held under a flat intent") |
| } |
| // Back to nested, still linked: held again. Then the operator removes |
| @@ -1521,7 +1521,7 @@ func TestConvergeHoldsAFlatHostWhoseMoveWasRefusedAfterItsStop(t *testing.T) { |
| if res[host].Outcome != "migrated" || f.fences[host] != 0 { |
| t.Fatalf("after the link is gone: %+v fence=%d", res[host], f.fences[host]) |
| } |
| - if _, held := p.Held(host); held { |
| + if _, held := p.Held(registry.Route{Host: host}); held { |
| t.Fatal("still held after the move went through") |
| } |
| } |
| @@ -1634,7 +1634,7 @@ func TestRestoreHoldsReholdsFromTheMarkerWhileTheReasonPersists(t *testing.T) { |
| if f2.fences[host] == 0 { |
| t.Fatal("the restored hold installed no fence; the host would wake flat") |
| } |
| - if why, held := p2.Held(host); !held || !strings.Contains(why, "2 names") { |
| + if why, held := p2.Held(registry.Route{Host: host}); !held || !strings.Contains(why, "2 names") { |
| t.Fatalf("Held after restore = %q %v", why, held) |
| } |
| // Unreadable intent is the fail-closed side: held too. |
| @@ -1760,7 +1760,7 @@ func TestConvergeDisplacesABlockerAtTheAsideDirectory(t *testing.T) { |
| if blockers != 1 { |
| t.Fatalf("layout dir = %v, want the one displaced blocker", entries) |
| } |
| - if _, held := p.Held(host); held { |
| + if _, held := p.Held(registry.Route{Host: host}); held { |
| t.Fatal("the blocker became a hold") |
| } |
| }) |
diff --git a/internal/layout/rootfs_pass_test.go b/internal/layout/rootfs_pass_test.go| index f06b29cf2..2bed8564a 100644 |
| --- a/internal/layout/rootfs_pass_test.go |
| +++ b/internal/layout/rootfs_pass_test.go |
| @@ -11,6 +11,7 @@ import ( |
| "testing" |
| "time" |
| + "github.com/carlosframework/platform/internal/activator" |
| "github.com/carlosframework/platform/internal/registry" |
| ) |
| @@ -101,7 +102,7 @@ func TestRestoreHoldNeverHoldsARootfsRow(t *testing.T) { |
| if f.fences[rootfsHost] != 0 { |
| t.Error("a hold marker fenced a rootfs row") |
| } |
| - if _, held := p.Held(rootfsHost); held { |
| + if _, held := p.Held(rowOf(t, p, rootfsHost)); held { |
| t.Error("a rootfs row is held") |
| } |
| if _, err := os.Lstat(filepath.Join(p.DataDir, "layout", rootfsHost+".hold")); !errors.Is(err, os.ErrNotExist) { |
| @@ -120,16 +121,42 @@ func TestRestoreHoldNeverHoldsARootfsRow(t *testing.T) { |
| } |
| } |
| -// assertJournalDropped checks recovery's answer for a planted journal |
| -// naming a rootfs host: refused loudly, the journal removed, nothing |
| -// moved, and no fence left behind. |
| -func assertJournalDropped(t *testing.T, p *Pass, f *fakeFence, h string, at Endpoint, db string, r Result) { |
| +// rowOf is host's row as the registry has it now. |
| +func rowOf(t *testing.T, p *Pass, h string) registry.Route { |
| t.Helper() |
| - if r.Outcome != "refused" || !strings.Contains(r.Reason, "rootfs instances are always nested") || !strings.Contains(r.Reason, "nothing moved") { |
| - t.Errorf("%s: result %+v, want refused: the journal removed and nothing moved", h, r) |
| + rt, err := p.Registry.Get(h) |
| + if err != nil || rt == nil { |
| + t.Fatalf("row %s: %v %v", h, rt, err) |
| + } |
| + return *rt |
| +} |
| + |
| +// remint replaces host's row with a new life carrying runtime, at the same |
| +// paths, as a delete and a fresh mint would. |
| +func remint(t *testing.T, p *Pass, h, runtime string) { |
| + t.Helper() |
| + at := rowAt(t, p, h) |
| + if err := p.Registry.Delete(h); err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := p.Registry.Upsert(registry.Route{Host: h, Kind: "instance", Status: "ready", Runtime: runtime, |
| + Socket: at.Socket, DBPath: at.DB}); err != nil { |
| + t.Fatal(err) |
| + } |
| +} |
| + |
| +// assertLetGo checks the layout machinery let a row with a runtime go: the |
| +// result kept, no journal left, the row and its database where they were, |
| +// no fence of the pass's left installed, nothing held, and the activator's |
| +// classification not fenced, so the host wakes as it would with no |
| +// journal at all. |
| +func assertLetGo(t *testing.T, p *Pass, f *fakeFence, h string, at Endpoint, db string, r Result) { |
| + t.Helper() |
| + if r.Outcome != "kept" || r.Reason != "rootfs instances are always nested" { |
| + t.Errorf("%s: result %+v, want kept: rootfs instances are always nested", h, r) |
| } |
| if JournalPresent(p.DataDir, h) { |
| - t.Errorf("%s: the planted journal is still there, fencing the host", h) |
| + t.Errorf("%s: a layout journal is still there", h) |
| } |
| if got := rowAt(t, p, h); got != at { |
| t.Errorf("%s row moved: %+v, want %+v", h, got, at) |
| @@ -138,7 +165,20 @@ func assertJournalDropped(t *testing.T, p *Pass, f *fakeFence, h string, at Endp |
| t.Errorf("%s db: %q %v", h, b, err) |
| } |
| if f.fences[h] != 0 { |
| - t.Errorf("%s left fenced", h) |
| + t.Errorf("%s left fenced at %d", h, f.fences[h]) |
| + } |
| + if _, kept := p.keptFence(h); kept { |
| + t.Errorf("%s: the pass still records a kept fence", h) |
| + } |
| + if _, held := p.Held(rowOf(t, p, h)); held { |
| + t.Errorf("%s is held", h) |
| + } |
| + routes, err := p.Registry.List() |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if l, why := p.Classify(rowOf(t, p, h), routes); l == activator.LayoutFenced { |
| + t.Errorf("%s classifies fenced: %s", h, why) |
| } |
| } |
| @@ -151,56 +191,185 @@ func nestedHostWithRuntime(t *testing.T, p *Pass, runtime string) (Endpoint, str |
| if r := byHost(t, p.Converge(context.Background(), ModeNested))[host]; r.Outcome != "migrated" { |
| t.Fatalf("setup: %+v", r) |
| } |
| - at := rowAt(t, p, host) |
| if runtime != "" { |
| - if err := p.Registry.Delete(host); err != nil { |
| - t.Fatal(err) |
| - } |
| - if err := p.Registry.Upsert(registry.Route{Host: host, Kind: "instance", Status: "ready", Runtime: runtime, |
| - Socket: at.Socket, DBPath: at.DB}); err != nil { |
| - t.Fatal(err) |
| - } |
| + remint(t, p, host, runtime) |
| } |
| + at := rowAt(t, p, host) |
| return at, filepath.Join(rawInstanceDir(p.DataDir, host), filepath.Base(at.DB)) |
| } |
| -// TestRecoveryRefusesAJournalNamingARootfsRow (task 5 fix round 1, Astra |
| -// 3): a valid flat-mode journal planted for a nested rootfs host (anyone |
| -// with the agent's uid can write one) must not stop it, move its database |
| -// or rewrite its row flat, through Recover or through Converge's own |
| -// recovery, for rootfs-v1 and an unknown runtime alike. The journal is |
| -// refused and removed, so it does not fence the host for good. |
| -func TestRecoveryRefusesAJournalNamingARootfsRow(t *testing.T) { |
| - for _, runtime := range []string{registry.RuntimeRootfsV1, "rootfs-v9"} { |
| - for _, via := range []string{"Recover", "Converge flat", "Converge nested"} { |
| +// runVia runs recovery the way via names: Recover, or Converge's own |
| +// recovery toward flat or nested. |
| +func runVia(t *testing.T, p *Pass, via string) map[string]Result { |
| + t.Helper() |
| + switch via { |
| + case "Recover": |
| + return byHost(t, p.Recover(context.Background(), ModeNested)) |
| + case "Converge flat": |
| + return byHost(t, p.Converge(context.Background(), ModeFlat)) |
| + default: |
| + return byHost(t, p.Converge(context.Background(), ModeNested)) |
| + } |
| +} |
| + |
| +var ( |
| + runtimes = []string{registry.RuntimeRootfsV1, "rootfs-v9"} |
| + vias = []string{"Recover", "Converge flat", "Converge nested"} |
| +) |
| + |
| +func assertNeverFenced(t *testing.T, f *fakeFence, h string) { |
| + t.Helper() |
| + for _, e := range f.events { |
| + if e == "fence "+h { |
| + t.Errorf("%s was fenced and stopped: %v", h, f.events) |
| + } |
| + } |
| +} |
| + |
| +// TestRecoveryLetsAJournalNamingARootfsRowGo (task 5 fix rounds 1 and 2, |
| +// Astra 3): a valid flat-mode journal planted for a nested host with a |
| +// runtime (anyone with the agent's uid can write one) must not stop it, |
| +// move its database or rewrite its row flat, through Recover or Converge's |
| +// own recovery, for rootfs-v1 and an unknown runtime alike. The journal is |
| +// removed unread and the host is never fenced. |
| +func TestRecoveryLetsAJournalNamingARootfsRowGo(t *testing.T) { |
| + for _, runtime := range runtimes { |
| + for _, via := range vias { |
| t.Run(runtime+" "+via, func(t *testing.T) { |
| p, f := box(t) |
| at, db := nestedHostWithRuntime(t, p, runtime) |
| forgeFlatJournal(t, p, host) |
| f.events = nil |
| - var out map[string]Result |
| - switch via { |
| - case "Recover": |
| - out = byHost(t, p.Recover(context.Background(), ModeNested)) |
| - case "Converge flat": |
| - out = byHost(t, p.Converge(context.Background(), ModeFlat)) |
| - default: |
| - out = byHost(t, p.Converge(context.Background(), ModeNested)) |
| - } |
| - assertJournalDropped(t, p, f, host, at, db, out[host]) |
| - for _, e := range f.events { |
| - if e == "fence "+host { |
| - t.Errorf("the rootfs host was fenced and stopped: %v", f.events) |
| + assertLetGo(t, p, f, host, at, db, runVia(t, p, via)[host]) |
| + assertNeverFenced(t, f, host) |
| + }) |
| + } |
| + } |
| +} |
| + |
| +// TestUnreadableJournalsNeverFenceARootfsRow (task 5 round 2, Astra P1): a |
| +// malformed journal or `{}` at a runtime row's journal path fences |
| +// nothing, before any pass runs (the activator's Classify does not read it) |
| +// and after one (the pass removes it unread). For a binary row the same |
| +// files behave as on main: refused, kept, and the host classified fenced. |
| +func TestUnreadableJournalsNeverFenceARootfsRow(t *testing.T) { |
| + for name, body := range map[string]string{"malformed": "{not json", "empty object": "{}"} { |
| + plant := func(t *testing.T, p *Pass) { |
| + t.Helper() |
| + if err := os.MkdirAll(journalDir(p.DataDir), 0o700); err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := os.WriteFile(filepath.Join(journalDir(p.DataDir), host+".json"), []byte(body), 0o600); err != nil { |
| + t.Fatal(err) |
| + } |
| + } |
| + for _, runtime := range runtimes { |
| + for _, via := range vias { |
| + t.Run(name+" "+runtime+" "+via, func(t *testing.T) { |
| + p, f := box(t) |
| + at, db := nestedHostWithRuntime(t, p, runtime) |
| + plant(t, p) |
| + routes, err := p.Registry.List() |
| + if err != nil { |
| + t.Fatal(err) |
| } |
| + if l, why := p.Classify(rowOf(t, p, host), routes); l == activator.LayoutFenced { |
| + t.Errorf("before any pass the %s journal fences the row: %s", name, why) |
| + } |
| + f.events = nil |
| + assertLetGo(t, p, f, host, at, db, runVia(t, p, via)[host]) |
| + assertNeverFenced(t, f, host) |
| + }) |
| + } |
| + } |
| + t.Run(name+" binary, as on main", func(t *testing.T) { |
| + p, f := box(t) |
| + nestedHostWithRuntime(t, p, "") |
| + plant(t, p) |
| + f.events = nil |
| + r := byHost(t, p.Recover(context.Background(), ModeNested))[host] |
| + if r.Outcome != "refused" || !strings.Contains(r.Reason, "not acting") { |
| + t.Errorf("binary row: %+v, want refused, not acting", r) |
| + } |
| + if !JournalPresent(p.DataDir, host) { |
| + t.Error("binary row: the journal was removed") |
| + } |
| + routes, _ := p.Registry.List() |
| + if l, _ := p.Classify(rowOf(t, p, host), routes); l != activator.LayoutFenced { |
| + t.Errorf("binary row classifies %s, want fenced", l) |
| + } |
| + assertNeverFenced(t, f, host) |
| + }) |
| + } |
| +} |
| + |
| +// TestAKeptFenceIsLetGoWhenTheRowIsRemintedWithARuntime (task 5 round 2, |
| +// Astra P1): after a failed handover the pass keeps its fence and the |
| +// journal at done. If the host name is then minted again with a runtime, |
| +// the next pass lifts the fence it kept, removes the journal unread and |
| +// moves nothing, for rootfs-v1 and an unknown runtime alike. |
| +func TestAKeptFenceIsLetGoWhenTheRowIsRemintedWithARuntime(t *testing.T) { |
| + if os.Geteuid() == 0 { |
| + t.Skip("root ignores the directory mode this test relies on") |
| + } |
| + for _, runtime := range runtimes { |
| + for _, via := range vias { |
| + t.Run(runtime+" "+via, func(t *testing.T) { |
| + p, f := box(t) |
| + crashedRevert(t, p) |
| + dir := journalDir(p.DataDir) |
| + calls := 0 |
| + p.Serialize = func(fn func()) { |
| + calls++ |
| + if calls == 2 { |
| + if err := os.Chmod(dir, 0o500); err != nil { |
| + t.Fatal(err) |
| + } |
| + defer os.Chmod(dir, 0o700) |
| + } |
| + fn() |
| + } |
| + first := byHost(t, p.Recover(context.Background(), ModeNested))[host] |
| + if !strings.Contains(first.Reason, "held by this pass after a failed handover") || f.fences[host] == 0 { |
| + t.Fatalf("setup: %+v, fence %d", first, f.fences[host]) |
| } |
| + p.Serialize = nil |
| + remint(t, p, host, runtime) |
| + at := rowAt(t, p, host) |
| + f.events = nil |
| + assertLetGo(t, p, f, host, at, at.DB, runVia(t, p, via)[host]) |
| + assertNeverFenced(t, f, host) |
| }) |
| } |
| } |
| } |
| +// TestAnotherOperationsFenceIsLeftAlone: what the layout machinery lets go |
| +// for a row with a runtime is its own. A fence of another operation (an |
| +// instance move's cut) at the host is not the pass's, and stays. |
| +func TestAnotherOperationsFenceIsLeftAlone(t *testing.T) { |
| + p, f := box(t) |
| + at, db := nestedHostWithRuntime(t, p, registry.RuntimeRootfsV1) |
| + forgeFlatJournal(t, p, host) |
| + f.fences = map[string]uint64{host: 42} |
| + r := byHost(t, p.Recover(context.Background(), ModeNested))[host] |
| + if r.Outcome != "kept" || JournalPresent(p.DataDir, host) { |
| + t.Errorf("result %+v, journal present %v; want kept and removed", r, JournalPresent(p.DataDir, host)) |
| + } |
| + if f.fences[host] != 42 { |
| + t.Errorf("another operation's fence was touched: %d", f.fences[host]) |
| + } |
| + if got := rowAt(t, p, host); got != at { |
| + t.Errorf("row moved: %+v", got) |
| + } |
| + if b, err := os.ReadFile(db); err != nil || string(b) != "db "+host { |
| + t.Errorf("db: %q %v", b, err) |
| + } |
| +} |
| + |
| // TestRecoveryRechecksTheRuntimeUnderTheFence: a row that has a runtime by |
| // the time the pass holds its fence (a new life minted while the journal |
| -// waited) is refused the same way on the refreshed row, and the fence is |
| +// waited) is let go the same way on the refreshed row, and the fence is |
| // lifted. |
| func TestRecoveryRechecksTheRuntimeUnderTheFence(t *testing.T) { |
| p, f := box(t) |
| @@ -208,20 +377,13 @@ func TestRecoveryRechecksTheRuntimeUnderTheFence(t *testing.T) { |
| forgeFlatJournal(t, p, host) |
| f.events = nil |
| f.onFence = func(h string) { |
| - if h != host { |
| - return |
| - } |
| - if err := p.Registry.Delete(h); err != nil { |
| - t.Fatal(err) |
| - } |
| - if err := p.Registry.Upsert(registry.Route{Host: h, Kind: "instance", Status: "ready", Runtime: registry.RuntimeRootfsV1, |
| - Socket: at.Socket, DBPath: at.DB}); err != nil { |
| - t.Fatal(err) |
| + if h == host { |
| + remint(t, p, h, registry.RuntimeRootfsV1) |
| } |
| } |
| r := byHost(t, p.Recover(context.Background(), ModeNested))[host] |
| - assertJournalDropped(t, p, f, host, at, db, r) |
| + assertLetGo(t, p, f, host, at, db, r) |
| if !strings.Contains(strings.Join(f.events, " "), "fence "+host) { |
| - t.Fatalf("the binary row was refused before the fence (%v): this test proves nothing about the recheck", f.events) |
| + t.Fatalf("the binary row was let go before the fence (%v): this test proves nothing about the recheck", f.events) |
| } |
| } |