stage 7 of 8 for issue 1389
This commit is contained in:
@@ -4,7 +4,6 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
|
||||
"github.com/nats-io/nats.go"
|
||||
|
||||
@@ -13,24 +12,12 @@ import (
|
||||
"github.com/novox/mesh-controller/internal/link"
|
||||
)
|
||||
|
||||
// seatPassing is what passing seats on needs of the outside: the record's compare-and-set, and the hand act.
|
||||
type seatPassing struct {
|
||||
pass func(ctx context.Context, p catalogue.SeatPass) (bool, error)
|
||||
act func(ctx context.Context, p catalogue.SeatPass) error
|
||||
say io.Writer
|
||||
}
|
||||
|
||||
// passSeatsOn records, in the composition that finds it, the new holder of each seat held once whose holder on
|
||||
// record is assigned nowhere and which has exactly one claimant able to hold it, and writes the controller's own
|
||||
// hand act for it (novox/hq ADR 0449 §4, issue 1317). It answers the holdings as they are after it, so the
|
||||
// resolution that follows reads the record it just moved. A failed write is said and leaves the record as it was:
|
||||
// a composition is never refused for it.
|
||||
func passSeatsOn(ctx context.Context, sp seatPassing, holdings []catalogue.Held,
|
||||
assignments []catalogue.Assignment) []catalogue.Held {
|
||||
passes, said := catalogue.SeatsToPassOn(holdings, assignments)
|
||||
for _, line := range said {
|
||||
fmt.Fprintln(sp.say, line)
|
||||
}
|
||||
// performSeatPasses records, in the send that finds them, the new holder of each seat held once whose holder on record is
|
||||
// assigned nowhere and which has exactly one claimant able to hold it (the composition names them: catalogue.SeatsToPassOn,
|
||||
// novox/hq ADR 0449 §4, issue 1317), and writes the controller's own hand act for each. A failed write is said and leaves the
|
||||
// record as it was: a send is never refused for it. It answers whether it recorded any.
|
||||
func performSeatPasses(ctx context.Context, sp seatPassing, passes []catalogue.SeatPass) bool {
|
||||
did := false
|
||||
for _, p := range passes {
|
||||
moved, err := sp.pass(ctx, p)
|
||||
if err != nil {
|
||||
@@ -41,21 +28,20 @@ func passSeatsOn(ctx context.Context, sp seatPassing, holdings []catalogue.Held,
|
||||
if !moved {
|
||||
continue // another composition passed it first, and wrote the act
|
||||
}
|
||||
did = true
|
||||
if err := sp.act(ctx, p); err != nil {
|
||||
fmt.Fprintf(sp.say, "%s passed from %s on %s to %s on %s, and the hand act could NOT be recorded: %v\n",
|
||||
p.Seat.Name, p.From.Module, p.From.Node, p.To.Module, p.To.Node, err)
|
||||
}
|
||||
var next []catalogue.Held
|
||||
for _, h := range holdings {
|
||||
if hs, ok := catalogue.SeatNamed(h.Claim); ok && hs.Name == p.Seat.Name {
|
||||
continue
|
||||
}
|
||||
next = append(next, h)
|
||||
}
|
||||
p.To.Site = p.From.Site
|
||||
holdings = append(next, p.To)
|
||||
}
|
||||
return holdings
|
||||
return did
|
||||
}
|
||||
|
||||
// seatPassing is what passing seats on needs of the outside: the record's compare-and-set, and the hand act.
|
||||
type seatPassing struct {
|
||||
pass func(ctx context.Context, p catalogue.SeatPass) (bool, error)
|
||||
act func(ctx context.Context, p catalogue.SeatPass) error
|
||||
say io.Writer
|
||||
}
|
||||
|
||||
// seatPassHandAct is the controller's own entry for a pass: it is the act that puts the holder on record,
|
||||
@@ -73,31 +59,10 @@ func seatPassHandAct(p catalogue.SeatPass) link.HandAct {
|
||||
}
|
||||
}
|
||||
|
||||
// assignmentsOf are the assignments of every machine, as the passing of a seat reads them. **A module assigned and
|
||||
// missing from the shelf is still assigned**: it is an assignment with no claims, so its holder is not taken for gone.
|
||||
func assignmentsOf(shelf map[string]catalogue.Manifest, assigned map[string][]string) []catalogue.Assignment {
|
||||
var out []catalogue.Assignment
|
||||
for node, modules := range assigned {
|
||||
for _, module := range modules {
|
||||
m, ok := shelf[module]
|
||||
if !ok {
|
||||
m = catalogue.Manifest{Module: module}
|
||||
}
|
||||
out = append(out, catalogue.Assignment{Node: node, Manifest: m})
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// seatPassingOn is how a composition on this inventory passes a seat: against the store's record with the hand act
|
||||
// on the bus, or, on a throwaway mesh (the merge gate's), in memory only, writing nothing anywhere.
|
||||
// seatPassingOn is how a send passes a seat: against the store's record, with the hand act on the bus; on a throwaway mesh
|
||||
// (the merge gate's) the record is kept in its store and no act is written, for there is no bus to write it to.
|
||||
func seatPassingOn(inv *inventory.Inventory, w io.Writer) seatPassing {
|
||||
if inv.Throwaway() {
|
||||
return seatPassing{say: w,
|
||||
pass: func(context.Context, catalogue.SeatPass) (bool, error) { return true, nil },
|
||||
act: func(context.Context, catalogue.SeatPass) error { return nil }}
|
||||
}
|
||||
return seatPassing{
|
||||
sp := seatPassing{
|
||||
say: w,
|
||||
pass: func(ctx context.Context, p catalogue.SeatPass) (bool, error) {
|
||||
return inv.PassSeat(ctx, p.Seat.Name, p.Seat.Scope, p.From, p.To)
|
||||
@@ -109,21 +74,8 @@ func seatPassingOn(inv *inventory.Inventory, w io.Writer) seatPassing {
|
||||
})
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// theMeshPassesSeatsOn is passSeatsOn as the composition runs it. **Only when every machine's assignments were
|
||||
// read** (assignedErr nil): a machine whose read failed would look as if its modules were assigned nowhere, and
|
||||
// its holder would lose the seat. Said to where the composition says its other lines (sayLeftOut).
|
||||
func theMeshPassesSeatsOn(ctx context.Context, inv *inventory.Inventory, shelf map[string]catalogue.Manifest,
|
||||
assigned map[string][]string, assignedErr error, holdings []catalogue.Held) []catalogue.Held {
|
||||
var w io.Writer = os.Stderr
|
||||
if lw, ok := ctx.Value(leftOutKey{}).(io.Writer); ok {
|
||||
w = lw
|
||||
if inv.Throwaway() {
|
||||
sp.act = func(context.Context, catalogue.SeatPass) error { return nil }
|
||||
}
|
||||
if assignedErr != nil {
|
||||
fmt.Fprintf(w, "no seat is passed on in this composition: a machine's assignments could not be read, so a "+
|
||||
"holder on record might look assigned nowhere: %v\n", assignedErr)
|
||||
return holdings
|
||||
}
|
||||
return passSeatsOn(ctx, seatPassingOn(inv, w), holdings, assignmentsOf(shelf, assigned))
|
||||
return sp
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user