| 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 |
| +} |