carlos / platform Public

hafresh: legacy owners carry the boot id; PreviousBoot names a lock this box's earlier boot left (#451)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev 3c7cee22c0410a17a8142deeb4a1a09ed06488c7 parent 1762547
2 files changed, +112 −9
  • internal/hafresh/owner.go +37 −9
  • internal/hafresh/owner_boot_test.go +75 −0
diff --git a/internal/hafresh/owner.go b/internal/hafresh/owner.go
index 22a7c657b..e78e8c258 100644
--- a/internal/hafresh/owner.go
+++ b/internal/hafresh/owner.go
@@ -10,11 +10,13 @@ import (
// Self is this box's identity as a lease owner: its reference (the label
// it publishes metrics under), its cloud instance id ("" until known),
-// and the cloud account and region it runs in. OwnerFor writes only the
-// reference and the instance id.
-type Self struct{ Ref, IID, Account, Region string }
+// the cloud account and region it runs in, and the kernel boot id ("" when
+// it can't be read). OwnerFor writes the reference, the instance id and,
+// on a legacy lock only, the boot.
+type Self struct{ Ref, IID, Account, Region, Boot string }
-// Complete reports whether all four fields are known.
+// Complete reports whether the four cloud identity fields are known. The
+// boot is not one of them.
func (s Self) Complete() bool {
return s.Ref != "" && s.IID != "" && s.Account != "" && s.Region != ""
}
@@ -23,14 +25,18 @@ func (s Self) Complete() bool {
type Owner struct {
Ref, IID string
PID int
+ Boot string
HA bool
}
const ownerPrefix = "box/v1;"
// OwnerFor is the owner string a box writes into a lock:
-// box/v1;ref=<ref>;iid=<instance id>;pid=<pid>, with ;ha=1 on an HA
-// lock. iid= is left out while the instance id is unknown.
+// box/v1;ref=<ref>;iid=<instance id>;pid=<pid>, then ;boot=<boot id> on a
+// legacy lock or ;ha=1 on an HA lock. iid= is left out while the instance
+// id is unknown, boot= while the boot id is (#451). An HA owner never
+// carries a boot: a binary that predates the field cannot parse it and
+// would read the HA lock as a legacy one.
func OwnerFor(s Self, pid int, ha bool) string {
var b strings.Builder
b.WriteString(ownerPrefix + "ref=" + s.Ref)
@@ -38,6 +44,9 @@ func OwnerFor(s Self, pid int, ha bool) string {
b.WriteString(";iid=" + s.IID)
}
b.WriteString(";pid=" + strconv.Itoa(pid))
+ if !ha && s.Boot != "" {
+ b.WriteString(";boot=" + s.Boot)
+ }
if ha {
b.WriteString(";ha=1")
}
@@ -45,8 +54,8 @@ func OwnerFor(s Self, pid int, ha bool) string {
}
// ParseOwner reads an owner string OwnerFor wrote, strictly: ref, iid
-// (optional), pid and ha (optional, only "1"), in that order, each with a
-// non-empty value, pid a positive decimal with no sign or leading zero,
+// (optional), pid, then either boot (optional) or ha (optional, only "1"),
+// never both, in that order, each with a non-empty value, pid a positive decimal with no sign or leading zero,
// nothing else. Any other string (a legacy host:pid owner, a tombstone's
// moved:<id>, a release marker's owner) is not a box owner: ok is false.
func ParseOwner(s string) (Owner, bool) {
@@ -84,7 +93,12 @@ func ParseOwner(s string) (Owner, bool) {
return Owner{}, false
}
o.PID = n
- if len(fields) > 0 {
+ if len(fields) > 0 && strings.HasPrefix(fields[0], "boot=") {
+ if o.Boot, ok = next("boot"); !ok {
+ return Owner{}, false
+ }
+ }
+ if len(fields) > 0 && o.Boot == "" {
if v, ok := next("ha"); !ok || v != "1" {
return Owner{}, false
}
@@ -95,3 +109,17 @@ func ParseOwner(s string) (Owner, bool) {
}
return o, true
}
+
+// PreviousBoot reports whether o is a legacy lock this same box wrote under
+// an earlier kernel boot (#451): the same non-empty instance id and the
+// same reference, and two known boot ids that differ. Such a lock is dead
+// even while it is unexpired, since no process of an earlier boot survives
+// into a new kernel, so the box may take it at once instead of waiting out
+// the lease. The same boot (an agent restart, whose instance units may
+// still run), another instance (a box cloned from this one's disk shares
+// its machine id, not its instance id), an owner or a self with no boot
+// id, and every HA owner answer false.
+func PreviousBoot(o Owner, self Self) bool {
+ return !o.HA && self.IID != "" && o.IID == self.IID && o.Ref == self.Ref &&
+ o.Boot != "" && self.Boot != "" && o.Boot != self.Boot
+}
diff --git a/internal/hafresh/owner_boot_test.go b/internal/hafresh/owner_boot_test.go
new file mode 100644
index 000000000..daee95886
--- /dev/null
+++ b/internal/hafresh/owner_boot_test.go
@@ -0,0 +1,75 @@
+package hafresh_test
+
+import (
+ "testing"
+
+ "github.com/carlosframework/platform/internal/hafresh"
+)
+
+// #451: a legacy owner names the kernel boot it was written under, so a
+// box can recognise the lock its own previous boot left. An HA owner
+// never does: an older binary reading an HA owner with an extra field
+// would take it for a legacy one.
+func TestOwnerForWritesBootOnlyWhenLegacy(t *testing.T) {
+ s := hafresh.Self{Ref: "r", IID: "i-1", Boot: "b1"}
+ for _, c := range []struct {
+ self hafresh.Self
+ ha bool
+ want string
+ }{
+ {s, false, "box/v1;ref=r;iid=i-1;pid=7;boot=b1"},
+ {s, true, "box/v1;ref=r;iid=i-1;pid=7;ha=1"},
+ {hafresh.Self{Ref: "r"}, false, "box/v1;ref=r;pid=7"},
+ } {
+ if got := hafresh.OwnerFor(c.self, 7, c.ha); got != c.want {
+ t.Errorf("OwnerFor(%+v, ha=%v) = %q, want %q", c.self, c.ha, got, c.want)
+ }
+ }
+}
+
+func TestParseOwnerBoot(t *testing.T) {
+ s := "box/v1;ref=r;iid=i-1;pid=7;boot=b1"
+ o, ok := hafresh.ParseOwner(s)
+ if want := (hafresh.Owner{Ref: "r", IID: "i-1", PID: 7, Boot: "b1"}); !ok || o != want {
+ t.Fatalf("%q: %+v %v, want %+v", s, o, ok, want)
+ }
+ for _, bad := range []string{
+ "box/v1;ref=r;pid=7;boot=b1;ha=1", // boot only on legacy owners
+ "box/v1;ref=r;pid=7;ha=1;boot=b1",
+ "box/v1;ref=r;pid=7;boot=",
+ "box/v1;ref=r;pid=7;boot=b1;boot=b2",
+ "box/v1;ref=r;boot=b1;pid=7",
+ } {
+ if o, ok := hafresh.ParseOwner(bad); ok {
+ t.Errorf("parsed %q as %+v", bad, o)
+ }
+ }
+ v := hafresh.View{Lock: hafresh.Lock{Owner: s}}
+ if v.HAOn() {
+ t.Fatal("a legacy owner with a boot reads as HA")
+ }
+}
+
+func TestPreviousBoot(t *testing.T) {
+ self := hafresh.Self{Ref: "r", IID: "i-1", Boot: "b2"}
+ for _, c := range []struct {
+ name string
+ o hafresh.Owner
+ self hafresh.Self
+ want bool
+ }{
+ {"this box, earlier boot", hafresh.Owner{Ref: "r", IID: "i-1", PID: 41, Boot: "b1"}, self, true},
+ {"this boot (agent restart)", hafresh.Owner{Ref: "r", IID: "i-1", PID: 41, Boot: "b2"}, self, false},
+ {"another instance", hafresh.Owner{Ref: "r", IID: "i-2", PID: 41, Boot: "b1"}, self, false},
+ {"another ref", hafresh.Owner{Ref: "x", IID: "i-1", PID: 41, Boot: "b1"}, self, false},
+ {"owner without iid", hafresh.Owner{Ref: "r", PID: 41, Boot: "b1"}, self, false},
+ {"owner from an older binary", hafresh.Owner{Ref: "r", IID: "i-1", PID: 41}, self, false},
+ {"HA owner", hafresh.Owner{Ref: "r", IID: "i-1", PID: 41, Boot: "b1", HA: true}, self, false},
+ {"self without iid", hafresh.Owner{Ref: "r", IID: "i-1", PID: 41, Boot: "b1"}, hafresh.Self{Ref: "r", Boot: "b2"}, false},
+ {"self without boot", hafresh.Owner{Ref: "r", IID: "i-1", PID: 41, Boot: "b1"}, hafresh.Self{Ref: "r", IID: "i-1"}, false},
+ } {
+ if got := hafresh.PreviousBoot(c.o, c.self); got != c.want {
+ t.Errorf("%s: PreviousBoot = %v, want %v", c.name, got, c.want)
+ }
+ }
+}