diff --git a/modules/mesh-delivery-sdk/steprun/pull.go b/modules/mesh-delivery-sdk/steprun/pull.go index 490d2ee26..f38cb81ad 100644 --- a/modules/mesh-delivery-sdk/steprun/pull.go +++ b/modules/mesh-delivery-sdk/steprun/pull.go @@ -172,6 +172,11 @@ type Puller struct { // declined are the kinds whose worker handed an ask this node declined, left out of the pulls without a wait until // the next pull's wait ends. declined map[string]bool + // takesWhy and takesAt are what the last judgement found keeps the puller from taking asks ("" for none) and since when + // (takesSince), once hasTakes. + takesWhy string + takesAt time.Time + hasTakes bool // turn is the kind the next pull asks first (next): the kinds take turns, so a queue that never empties holds back none. turn int } @@ -267,12 +272,45 @@ func (p *Puller) InHand() int { return p.inHand } -// RunningAsk is an ask a puller runs now, as its seat's current verb says it. +// RunningAsk is an ask a puller runs now, as its seat's current verb says it: the part's CurrentAsk (design 47 H2 §801), +// whoever asked it. type RunningAsk struct { ID string `json:"id"` Kind string `json:"kind"` Module string `json:"module,omitempty"` Started time.Time `json:"started"` + Asker string `json:"asker"` +} + +// The reasons a holder takes no ask, as the seat's verb current says them (design 47 H2 §801, H-A-367 (2)). +const ( + NotTakingPaused = "paused" + NotTakingCapacity0 = "capacity 0" + NotTakingUnhealthy = "unhealthy" +) + +// whyNot is why the puller takes no ask by c, "" when it takes them: paused, its node unable to run the seat's asks +// (Eligible, unhealthy), or a capacity of 0. +func (p *Puller) whyNot(c sdkstep.Capacity) string { + switch { + case p.Paused(): + return NotTakingPaused + case c.Ineligible != "": + return NotTakingUnhealthy + case c.Effective <= 0: + return NotTakingCapacity0 + } + return "" +} + +// takesSince is since when the puller takes asks, or takes none for why, as it judged them: the first judgement that met why. +func (p *Puller) takesSince(why string) time.Time { + p.mu.Lock() + defer p.mu.Unlock() + if !p.hasTakes || p.takesWhy != why { + p.takesWhy, p.takesAt, p.hasTakes = why, now().UTC(), true + } + return p.takesAt } // Running are the asks the puller runs now, oldest first. @@ -295,7 +333,9 @@ func (p *Puller) room() bool { // roomNow is room, with the capacity and the asks in hand it was judged on. func (p *Puller) roomNow() (ok bool, capacity, inHand int) { - capacity = p.CapacityDetail().Effective + c := p.CapacityDetail() + capacity = c.Effective + p.takesSince(p.whyNot(c)) // each judgement keeps since when the holder takes asks or not (§801) p.mu.Lock() defer p.mu.Unlock() return !p.paused && p.inHand < capacity, capacity, p.inHand @@ -743,7 +783,7 @@ func (p *Puller) take(ctx context.Context, msg *Pulled) { if p.running == nil { p.running = map[string]RunningAsk{} } - p.running[ask.ID] = RunningAsk{ID: ask.ID, Kind: ask.Kind, Module: module, Started: step.Started} + p.running[ask.ID] = RunningAsk{ID: ask.ID, Kind: ask.Kind, Module: module, Started: step.Started, Asker: askerOf(msg, ask)} p.mu.Unlock() p.logf("the %s ask %s starts on %s: %s", ask.Kind, ask.ID, p.Node, step.What) p.say("ask-taken", askTaken{Ask: ask.ID, Asker: askerOf(msg, ask), Node: p.Node, At: now().UTC()}, "ask-taken."+ask.ID) diff --git a/modules/mesh-delivery-sdk/steprun/seat_tools.go b/modules/mesh-delivery-sdk/steprun/seat_tools.go index 1c423783d..46b76a499 100644 --- a/modules/mesh-delivery-sdk/steprun/seat_tools.go +++ b/modules/mesh-delivery-sdk/steprun/seat_tools.go @@ -18,9 +18,15 @@ func SeatTools(seat, what string, p *Puller) []stdio.Tool { Run: func(map[string]any) (any, error) { return run() }} } current := func() (any, error) { + // The part's current at form 2 (design 47 H2 §801, H-A-367 (2)): whether it takes asks, why not since when, its + // capacity and every ask it runs, whoever asked it. c := p.CapacityDetail() - return map[string]any{"node": p.Node, "capacity": c.Effective, "capacity_why": c.Why, "capacity_detail": c, - "running": p.Running(), "paused": p.Paused()}, nil + why := p.whyNot(c) + out := map[string]any{"node": p.Node, "takes": why == "", "since": p.takesSince(why), "capacity": c.Effective, "asks": p.Running()} + if why != "" { + out["why-not"] = why + } + return out, nil } drop := stdio.Tool{Name: seat + ".drop", Description: "End an " + what + " ask its caller no longer waits for: it is kept as dropped on this node " + "(so a bus that gives it again has it refused), and a step running here for it is ended, written refused naming who dropped it and why.", @@ -36,9 +42,8 @@ func SeatTools(seat, what string, p *Puller) []stdio.Tool { return map[string]any{}, nil }} return []stdio.Tool{drop, - verb("current", "The "+what+" asks this node runs now (each one's id, kind and when it started), its build capacity (the configured one, "+ - "bounded by the cores its node's build slice gives over the cores a step needs) and why, "+ - "and whether it is paused.", current), + verb("current", "Whether this node takes "+what+" asks now and why not (paused, capacity 0, unhealthy) since when, its build capacity, "+ + "and every ask it runs (its id, kind, module, when it started and who asked it), whoever asked it.", current), verb("pause", "Take no new "+what+" ask on this node until resumed; an ask running here ends in its place. Kept "+ "across a restart (the node's pause key).", func() (any, error) { if err := p.Pause("the seat's pause verb"); err != nil { diff --git a/modules/mesh-delivery-sdk/steprun/seat_tools_test.go b/modules/mesh-delivery-sdk/steprun/seat_tools_test.go index ddd615da7..b93acf54c 100644 --- a/modules/mesh-delivery-sdk/steprun/seat_tools_test.go +++ b/modules/mesh-delivery-sdk/steprun/seat_tools_test.go @@ -32,11 +32,13 @@ func TestSeatToolsCurrentPauseResume(t *testing.T) { waitFor(t, "the ask running", func() bool { return p.InHand() == 1 }) out, _ := tools["node-build-go.current"](nil) cur := out.(map[string]any) - running := cur["running"].([]RunningAsk) - if len(running) != 1 || running[0].ID != a.ID || running[0].Module != "m" || cur["capacity"] != 1 || cur["paused"] != false { + // Design 47 H2 §801: current answers whether it takes asks, since when, and every ask it runs with its asker. + running := cur["asks"].([]RunningAsk) + if len(running) != 1 || running[0].ID != a.ID || running[0].Module != "m" || running[0].Asker != a.Asker || cur["capacity"] != 1 || + cur["takes"] != true || cur["why-not"] != nil || cur["since"].(time.Time).IsZero() { t.Fatalf("current = %+v", cur) } - if out, err := tools["node-build-go.pause"](nil); err != nil || out.(map[string]any)["paused"] != true { + if out, err := tools["node-build-go.pause"](nil); err != nil || out.(map[string]any)["why-not"] != "paused" || out.(map[string]any)["takes"] != false { t.Fatalf("pause = %v, %v", out, err) } close(release) @@ -49,6 +51,25 @@ func TestSeatToolsCurrentPauseResume(t *testing.T) { t.Fatal(err) } waitFor(t, "the ask taken after the resume", func() bool { return src.Waiting(sdkstep.AskDelivery) == 0 }) + // §801: a node at capacity 0 takes none for capacity 0, one its module finds unable to run the seat's asks for unhealthy. + for _, c := range []struct { + p *Puller + want string + }{ + {&Puller{Node: "ace", Language: sdkstep.AskGo, Capacity: func() int { return 0 }}, NotTakingCapacity0}, + {&Puller{Node: "ace", Language: sdkstep.AskGo, Capacity: func() int { return 2 }, Eligible: func() string { return "no /dev/net/tun" }}, NotTakingUnhealthy}, + } { + var cur map[string]any + for _, tool := range SeatTools("node-build-go", "Go", c.p) { + if tool.Name == "node-build-go.current" { + out, _ := tool.Run(nil) + cur = out.(map[string]any) + } + } + if cur["why-not"] != c.want || cur["takes"] != false { + t.Errorf("current = %+v, want why-not %q", cur, c.want) + } + } } // Design 47 G A-254: `.output` answers a run's answer by its ask as the whole file (base64), which the tool runner sends in