package catalogue import ( "fmt" "sort" ) // An assignment as the passing of a seat reads it: a module on a node, with its placement's watched branch (design 47 G // §459). The build it runs is resolved by SeatsToPassOn from what the caller heard and its reader of entries. type Assignment struct { Node string Module string // Branch is the watched branch of the assignment's placement, whose newest entry of the module is taken where the node // said no `running` for it (§199). Branch string } // Heard is what the caller heard of a module on a node from the node-engine seat's `running`: a fingerprint, or Stopped // (`stopped: true` and no fingerprint, §404). Nothing heard since its consumer began is no Heard at all. type Heard struct { Fingerprint string Stopped bool } // EntryReader is the caller's view of the catalog half (design 47 G §459, §443): the entry's manifest by its // fingerprint, and a module's entry on a watched branch's newest revision. ok is false where it answers no entry. type EntryReader interface { ByFingerprint(fingerprint string) (m Manifest, ok bool) OnBranch(module, branch string) (m Manifest, ok bool) } // resolved is an assignment with the manifest of the build it runs. Unread is one whose entry the reader did not answer: // it is still assigned, so its holdings stand, and it claims nothing. type resolved struct { Node string Manifest Manifest Unread bool } // SeatPass is one seat whose holder on record has left it, and the only assignment that can take it. type SeatPass struct { Seat Seat From Held To Held } // SeatsToPassOn reads the mesh for seats held once whose holder on record has left (novox/hq ADR 0449 §4, ADR 0660). // // **A holder on record that is assigned on no machine, or whose every assignment's manifest no longer claims the seat // (or claims it and cannot hold it, by CanHold), holds nothing.** Of such a seat, exactly one assignment in the mesh // able to hold it takes it over: that is a pass, and the caller applies it and says it. An assignment is a module on // one node, so one module placed on two nodes is two claimants. With no such assignment the record stays; with two or // more nothing is passed and the answer says so, naming two and the `seat --to` that settles it. A holder with an // assignment whose manifest still claims the seat changes nothing (ADR 0131), and so does one whose manifest could not // be read. Only mesh seats held once are read: a replicated seat's holders are added by an act (ADR 0223), a kinded // bench is held by its claims, and a seat with several holders on record is a store that disagrees with the mesh. // Nothing here records a pass (ADR 0660 §4). // // The assignments' builds are resolved here, by one rule for both callers (design 47 G §199, §459, §404): the heard // fingerprint's entry; with nothing heard, the module's entry on its placement's branch; a `stopped` module claims nothing // on that node; and where the reader answers no entry, no seat that assignment could claim passes, the holder on record // stays, and the answer says it, naming the node, the module and the fingerprint. The function reads no bus. func SeatsToPassOn(holdings []Held, assignments []Assignment, heard func(node, module string) (Heard, bool), entries EntryReader) ([]SeatPass, []string) { var rs []resolved var said []string for _, a := range assignments { h, ok := Heard{}, false if heard != nil { h, ok = heard(a.Node, a.Module) } if ok && h.Stopped { rs = append(rs, resolved{Node: a.Node, Manifest: Manifest{Module: a.Module}}) continue } var m Manifest var found bool fp := "none heard, the newest on " + a.Branch if ok { fp = h.Fingerprint m, found = entries.ByFingerprint(h.Fingerprint) } else { m, found = entries.OnBranch(a.Module, a.Branch) } if !found { said = append(said, fmt.Sprintf("no entry is answered for %s on %s (%s): no seat it could claim passes and the holder on record stays", a.Module, a.Node, fp)) rs = append(rs, resolved{Node: a.Node, Manifest: Manifest{Module: a.Module}, Unread: true}) continue } rs = append(rs, resolved{Node: a.Node, Manifest: m}) } passes, more := seatsToPass(holdings, rs) return passes, append(said, more...) } func seatsToPass(holdings []Held, assignments []resolved) ([]SeatPass, []string) { bySeat := map[string][]Held{} var order []string for _, h := range holdings { s, ok := SeatNamed(h.Claim) if !ok || s.Replicated || s.Scope != ScopeMesh || h.Scope != ScopeMesh || KindedBenches[s.Name] { continue } if _, seen := bySeat[s.Name]; !seen { order = append(order, s.Name) } bySeat[s.Name] = append(bySeat[s.Name], h) } sort.Strings(order) var passes []SeatPass var said []string for _, name := range order { recs := bySeat[name] if len(recs) != 1 { continue } seat, _ := SeatNamed(name) holds := false for _, a := range assignments { if a.Manifest.Module != recs[0].Module { continue } if a.Unread || CanHold(a.Manifest, seat) == nil { holds = true } } if holds { continue } var claimants []Held for _, a := range assignments { if a.Unread || CanHold(a.Manifest, seat) != nil { continue } claimants = append(claimants, Held{Claim: seat.Name, Scope: seat.Scope, Node: a.Node, Module: a.Manifest.Module}) } sort.Slice(claimants, func(i, j int) bool { if claimants[i].Node != claimants[j].Node { return claimants[i].Node < claimants[j].Node } return claimants[i].Module < claimants[j].Module }) switch len(claimants) { case 0: case 1: passes = append(passes, SeatPass{Seat: seat, From: recs[0], To: claimants[0]}) default: said = append(said, fmt.Sprintf( "%s on %s is on record as the holder of %q and holds it nowhere, and %s on %s and %s on %s both claim it "+ "— nothing is passed; `seat %s --to /` records the one that holds it", recs[0].Module, recs[0].Node, seat.Name, claimants[0].Module, claimants[0].Node, claimants[1].Module, claimants[1].Node, seat.Name)) } } return passes, said }