instancectl: prove a skipped reload by the tier's memory lines too (Astra P2)
NeedDaemonReload compares mtimes only, so a reused generation still loaded with another tier, whose rewritten drop-in is not newer (a clock that went back), would read as applied. The unit's name fixes every other line of the drop-in, so the check now also requires systemd's MemoryMax, MemoryHigh and MemorySwapMax to be the tier's; anything else reloads.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2 files changed,
+50
−24
internal/instancectl/gen.go+16 −9internal/instancectl/server_gen_test.go+34 −15
CI failed — run details
diff --git a/internal/instancectl/gen.go b/internal/instancectl/gen.go| index 5ae64baa2..c23172173 100644 |
| --- a/internal/instancectl/gen.go |
| +++ b/internal/instancectl/gen.go |
| @@ -114,15 +114,23 @@ func (s *Server) slicePopulated(host string) bool { |
| } |
| // genDropInApplied reports whether systemd's view of unit is the drop-in |
| -// just written: the host's slice, and NeedDaemonReload=no, so no file it |
| -// was loaded from has changed since. Any failure to answer is a no. |
| -func (s *Server) genDropInApplied(ctx context.Context, host, unit string) (slice string, ok bool) { |
| - out, err := s.Systemctl(ctx, "show", "-p", "Slice,NeedDaemonReload", unit) |
| +// just written (GenDropIn). The unit's name fixes the host and generation, |
| +// and so every line of the drop-in but the tier's: the slice says systemd |
| +// read a drop-in at all, and the memory lines say it was this tier's. |
| +// NeedDaemonReload=no says no file changed since systemd loaded the unit, |
| +// though only by mtime, which is why the tier is checked as well (Astra |
| +// review: a reused, still-loaded generation and a clock that went back). |
| +// Any failure to answer is a no. |
| +func (s *Server) genDropInApplied(ctx context.Context, host, unit string, tier cgroup.Limit) (slice string, ok bool) { |
| + out, err := s.Systemctl(ctx, "show", "-p", "Slice,NeedDaemonReload,MemoryMax,MemoryHigh,MemorySwapMax", unit) |
| if err != nil { |
| return "", false |
| } |
| m := parseShow(out) |
| - return m["Slice"], m["Slice"] == SliceName(host) && m["NeedDaemonReload"] == "no" |
| + max := strconv.FormatInt(tier.MemoryMax, 10) |
| + return m["Slice"], m["Slice"] == SliceName(host) && m["NeedDaemonReload"] == "no" && |
| + m["MemoryMax"] == max && m["MemorySwapMax"] == max && |
| + m["MemoryHigh"] == strconv.FormatInt(cgroup.HighOf(tier.MemoryMax), 10) |
| } |
| // startGen is /start with a generation. The host slice is the parent (spec |
| @@ -168,16 +176,15 @@ func (s *Server) startGen(w http.ResponseWriter, r *http.Request, req request) { |
| // files. A generation's unit is new and an inactive one is unloaded |
| // straight away, so the next reference reads the drop-in fresh: the |
| // common case needs no reload, which on a small box was a third of the |
| - // whole wake. The check is the same either way: the drop-in's slice, |
| - // and systemd's own word that nothing changed since it loaded the unit. |
| - slice, applied := s.genDropInApplied(ctx, req.host, unit) |
| + // whole wake. The check is the same either way (genDropInApplied). |
| + slice, applied := s.genDropInApplied(ctx, req.host, unit, req.tier) |
| if !applied { |
| reloadStart := time.Now() |
| if out, err := s.Systemctl(ctx, "daemon-reload"); err != nil { |
| fail(http.StatusBadGateway, "daemon-reload: "+strings.TrimSpace(out+" "+err.Error())) |
| return |
| } |
| - if slice, applied = s.genDropInApplied(ctx, req.host, unit); !applied { |
| + if slice, applied = s.genDropInApplied(ctx, req.host, unit, req.tier); !applied { |
| fail(http.StatusBadGateway, "not starting "+unit+": its drop-in did not apply (slice "+strconv.Quote(slice)+")") |
| return |
| } |
diff --git a/internal/instancectl/server_gen_test.go b/internal/instancectl/server_gen_test.go| index 399f765ce..6f2f61937 100644 |
| --- a/internal/instancectl/server_gen_test.go |
| +++ b/internal/instancectl/server_gen_test.go |
| @@ -19,6 +19,9 @@ const genHost = "bill-2.example.com" |
| var genUnit = GenUnitName(genHost, 7) |
| +// genTierShow is systemd's answer for the default tier's memory lines. |
| +const genTierShow = "MemoryMax=536870912\nMemoryHigh=483183819\nMemorySwapMax=536870912\n" |
| + |
| func genServer(t *testing.T, ctl *fakeCtl) (*Server, string, string) { |
| t.Helper() |
| dropins, exe := t.TempDir(), t.TempDir() |
| @@ -28,11 +31,11 @@ func genServer(t *testing.T, ctl *fakeCtl) (*Server, string, string) { |
| func genCtl() *fakeCtl { |
| return &fakeCtl{answers: map[string]string{ |
| - "show -p Slice,NeedDaemonReload": "Slice=" + SliceName(genHost) + "\nNeedDaemonReload=no\n", |
| - "show -p InvocationID --value": "abc123\n", |
| - "list-units": "", |
| - "list-jobs": "", |
| - "show -p ActiveState --value": "inactive\n", |
| + "show -p Slice,NeedDaemonReload,MemoryMax,MemoryHigh,MemorySwapMax": "Slice=" + SliceName(genHost) + "\nNeedDaemonReload=no\n" + genTierShow, |
| + "show -p InvocationID --value": "abc123\n", |
| + "list-units": "", |
| + "list-jobs": "", |
| + "show -p ActiveState --value": "inactive\n", |
| }} |
| } |
| @@ -71,7 +74,7 @@ func TestGenStartWritesTheDropInChecksItThenStartsWithoutAReload(t *testing.T) { |
| t.Fatalf("exe app link = %q, %v", target, err) |
| } |
| want := strings.Join([]string{ |
| - "show -p Slice,NeedDaemonReload " + genUnit, |
| + "show -p Slice,NeedDaemonReload,MemoryMax,MemoryHigh,MemorySwapMax " + genUnit, |
| `set-property --runtime carlos-instances-bill\x2d2.example.com.slice MemoryMax=536870912 MemoryHigh=483183819 MemorySwapMax=536870912 CPUWeight=100 CPUQuota=100% IOWeight=100`, |
| "start " + genUnit, |
| "show -p InvocationID --value " + genUnit, |
| @@ -84,11 +87,11 @@ func TestGenStartWritesTheDropInChecksItThenStartsWithoutAReload(t *testing.T) { |
| // staleUntilReload makes systemd hold a stale copy of the unit (answer) |
| // until a daemon-reload, after which the drop-in reads as applied. |
| func staleUntilReload(ctl *fakeCtl, answer string) { |
| - ctl.answers["show -p Slice,NeedDaemonReload"] = answer |
| + ctl.answers["show -p Slice,NeedDaemonReload,MemoryMax,MemoryHigh,MemorySwapMax"] = answer |
| ctl.before = map[string]func(){"daemon-reload": func() { |
| ctl.mu.Lock() |
| defer ctl.mu.Unlock() |
| - ctl.answers["show -p Slice,NeedDaemonReload"] = "Slice=" + SliceName(genHost) + "\nNeedDaemonReload=no\n" |
| + ctl.answers["show -p Slice,NeedDaemonReload,MemoryMax,MemoryHigh,MemorySwapMax"] = "Slice=" + SliceName(genHost) + "\nNeedDaemonReload=no\n" + genTierShow |
| }} |
| } |
| @@ -97,8 +100,8 @@ func staleUntilReload(ctl *fakeCtl, answer string) { |
| // the start reloads, checks again, and only then starts. |
| func TestGenStartReloadsWhenSystemdHoldsAStaleCopy(t *testing.T) { |
| for name, answer := range map[string]string{ |
| - "files changed since load": "Slice=" + SliceName(genHost) + "\nNeedDaemonReload=yes\n", |
| - "loaded without drop-in": `Slice=system-carlos\x2dinstance\x2dgen.slice` + "\nNeedDaemonReload=no\n", |
| + "files changed since load": "Slice=" + SliceName(genHost) + "\nNeedDaemonReload=yes\n" + genTierShow, |
| + "loaded without drop-in": `Slice=system-carlos\x2dinstance\x2dgen.slice` + "\nNeedDaemonReload=no\n" + genTierShow, |
| "show failed to answer": "", |
| } { |
| t.Run(name, func(t *testing.T) { |
| @@ -109,9 +112,9 @@ func TestGenStartReloadsWhenSystemdHoldsAStaleCopy(t *testing.T) { |
| t.Fatal(err) |
| } |
| want := strings.Join([]string{ |
| - "show -p Slice,NeedDaemonReload " + genUnit, |
| + "show -p Slice,NeedDaemonReload,MemoryMax,MemoryHigh,MemorySwapMax " + genUnit, |
| "daemon-reload", |
| - "show -p Slice,NeedDaemonReload " + genUnit, |
| + "show -p Slice,NeedDaemonReload,MemoryMax,MemoryHigh,MemorySwapMax " + genUnit, |
| }, "\n") |
| if got := ctl.joined(); !strings.HasPrefix(got, want+"\n") || !strings.Contains(got, "\nstart "+genUnit) { |
| t.Errorf("calls =\n%s\nwant them to begin\n%s\nand then start", got, want) |
| @@ -120,11 +123,27 @@ func TestGenStartReloadsWhenSystemdHoldsAStaleCopy(t *testing.T) { |
| } |
| } |
| +// systemd's change check is by mtime, so a reused generation still loaded |
| +// with an older tier, whose rewritten drop-in is not newer (a clock that |
| +// went back), reads NeedDaemonReload=no. The drop-in's other lines follow |
| +// from the unit's name; its memory lines are checked against the tier. |
| +func TestGenStartReloadsWhenSystemdHoldsAnotherTier(t *testing.T) { |
| + ctl := genCtl() |
| + staleUntilReload(ctl, "Slice="+SliceName(genHost)+"\nNeedDaemonReload=no\nMemoryMax=1073741824\nMemoryHigh=966367641\nMemorySwapMax=1073741824\n") |
| + _, sock, _ := genServer(t, ctl) |
| + if _, err := NewClient(sock).StartGen(context.Background(), genHost, 7, cgroup.TierDefault.MemoryMax); err != nil { |
| + t.Fatal(err) |
| + } |
| + if got := ctl.joined(); !strings.Contains(got, "daemon-reload\n") { |
| + t.Errorf("started on systemd's stale tier without a reload:\n%s", got) |
| + } |
| +} |
| + |
| // A reload that leaves systemd still saying its files changed proves |
| // nothing about what it loaded: refuse rather than start on a guess. |
| func TestGenStartRefusesWhenTheReloadDidNotSettle(t *testing.T) { |
| ctl := genCtl() |
| - ctl.answers["show -p Slice,NeedDaemonReload"] = "Slice=" + SliceName(genHost) + "\nNeedDaemonReload=yes\n" |
| + ctl.answers["show -p Slice,NeedDaemonReload,MemoryMax,MemoryHigh,MemorySwapMax"] = "Slice=" + SliceName(genHost) + "\nNeedDaemonReload=yes\n" + genTierShow |
| s, sock, dropins := genServer(t, ctl) |
| if _, err := NewClient(sock).StartGen(context.Background(), genHost, 7, cgroup.TierDefault.MemoryMax); err == nil { |
| t.Fatal("started a generation systemd still held a stale copy of") |
| @@ -180,7 +199,7 @@ func TestGenStartResetsAnEmptySlice(t *testing.T) { |
| // and an empty /data. Refuse, and leave nothing behind. |
| func TestGenStartRefusesWhenTheDropInDidNotApply(t *testing.T) { |
| ctl := genCtl() |
| - ctl.answers["show -p Slice,NeedDaemonReload"] = `Slice=system-carlos\x2dinstance\x2dgen.slice` + "\nNeedDaemonReload=no\n" |
| + ctl.answers["show -p Slice,NeedDaemonReload,MemoryMax,MemoryHigh,MemorySwapMax"] = `Slice=system-carlos\x2dinstance\x2dgen.slice` + "\nNeedDaemonReload=no\n" + genTierShow |
| s, sock, dropins := genServer(t, ctl) |
| if _, err := NewClient(sock).StartGen(context.Background(), genHost, 7, cgroup.TierDefault.MemoryMax); err == nil { |
| t.Fatal("started a generation whose drop-in did not apply") |
| @@ -445,7 +464,7 @@ func TestGenStartDropInWriteFailureRemovesTheFiles(t *testing.T) { |
| func TestGenStartDaemonReloadFailureRemovesTheFiles(t *testing.T) { |
| ctl := genCtl() |
| - ctl.answers["show -p Slice,NeedDaemonReload"] = "Slice=" + SliceName(genHost) + "\nNeedDaemonReload=yes\n" |
| + ctl.answers["show -p Slice,NeedDaemonReload,MemoryMax,MemoryHigh,MemorySwapMax"] = "Slice=" + SliceName(genHost) + "\nNeedDaemonReload=yes\n" + genTierShow |
| ctl.fail = map[string]error{"daemon-reload": errors.New("bus gone")} |
| s, sock, dropins := genServer(t, ctl) |
| _, err := NewClient(sock).StartGen(context.Background(), genHost, 7, cgroup.TierDefault.MemoryMax) |