diff --git a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/pass.go b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/pass.go index 1c1df3caf..5c1c9a0ba 100644 --- a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/pass.go +++ b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/pass.go @@ -128,6 +128,9 @@ type passer struct { tierHolds map[string]tierHold // owed are the events of the seat mesh-wants the bus refused, owed in order and said again (owed.go, design 47 H §832). owed owedKept + // putHeld is the put waits whose wait-held is owed and not yet taken, by put key: their record is written once it is taken, and + // a put that ends meanwhile still owes its wait-ended behind it (design 47 H2b §832 1., 4.). + putHeld putHeldOwed // passStall is the setting pass-stall-seconds (passstall.go; zero: its default): a pass still running after it says which phase // it is in (design 47 H2 H-A-635). passStall time.Duration diff --git a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/putwaits.go b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/putwaits.go index 212378749..bd9f2a6f8 100644 --- a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/putwaits.go +++ b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/putwaits.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "fmt" + "sync" "time" meshwants "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshwants" @@ -48,6 +49,29 @@ type putWaiting struct { Put puts.Put `json:"put"` } +// putHeldOwed is the put waits whose wait-held is owed, by put key (in memory: a restart owes what the kept state implies). +type putHeldOwed struct { + mu sync.Mutex + m map[string]putWaiting +} + +func (h *putHeldOwed) set(key string, w putWaiting) { + h.mu.Lock() + defer h.mu.Unlock() + if h.m == nil { + h.m = map[string]putWaiting{} + } + h.m[key] = w +} + +func (h *putHeldOwed) drop(key, id string) { + h.mu.Lock() + defer h.mu.Unlock() + if h.m[key].ID == id { + delete(h.m, key) + } +} + // saidPutWaits is every put wait the state put-waits holds, by key; a value that does not decode is said and left. func (p *passer) saidPutWaits() map[string]putWaiting { out := map[string]putWaiting{} @@ -59,6 +83,13 @@ func (p *passer) saidPutWaits() map[string]putWaiting { } out[key] = w } + p.putHeld.mu.Lock() + for key, w := range p.putHeld.m { + if _, kept := out[key]; !kept { + out[key] = w // its wait-held is owed: its end is owed behind it + } + } + p.putHeld.mu.Unlock() return out } @@ -89,7 +120,10 @@ func (p *passer) putWaits(ctx context.Context, all map[string]map[string]puts.Pu body := putWaitBody(node, module, was.Put) body.How = how // The kept record stays until the bus has taken the end, so a restart says it again (§832 4.; H-A-830). - p.wantsOwe(putWaitOrder(node, module), meshwants.EventWaitEnded, body, was.ID+".put."+how, func() { p.remove(ctx, buckets.PutWaits(), key) }) + p.wantsOwe(putWaitOrder(node, module), meshwants.EventWaitEnded, body, was.ID+".put."+how, func() { + p.putHeld.drop(key, was.ID) + p.remove(ctx, buckets.PutWaits(), key) + }) delete(waiting, key) } for module, byNode := range all { @@ -102,9 +136,14 @@ func (p *passer) putWaits(ctx context.Context, all map[string]map[string]puts.Pu if w, said := waiting[key]; said && w.ID == id { continue } - p.wantsOwe(putWaitOrder(node, module), meshwants.EventWaitHeld, putWaitBody(node, module, put), id+".put", nil) - kept := putWaiting{ID: id, Put: put} - p.put(ctx, buckets.PutWaits(), key, func() ([]byte, error) { return json.Marshal(kept) }) + // The record is written once the bus has taken the wait-held, never while it is only owed: after a restart a record + // that stands reads as said (§832 4., 1.; H-A-830). + kept, done := putWaiting{ID: id, Put: put}, context.WithoutCancel(ctx) + p.putHeld.set(key, kept) + p.wantsOwe(putWaitOrder(node, module), meshwants.EventWaitHeld, putWaitBody(node, module, put), id+".put", func() { + p.putHeld.drop(key, id) + p.put(done, buckets.PutWaits(), key, func() ([]byte, error) { return json.Marshal(kept) }) + }) } } } diff --git a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/putwaits_test.go b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/putwaits_test.go index e36c14237..bfdd30e57 100644 --- a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/putwaits_test.go +++ b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/putwaits_test.go @@ -105,8 +105,8 @@ func TestAPutWithoutHoldIsSaidAsAWait(t *testing.T) { fresh := puts.Put{FP: fpTest, Why: "a try again", At: at.Add(3 * time.Hour)}.Said(at.Add(3*time.Hour), puts.Wanted, "the want is written") p.putWaits(ctx, map[string]map[string]puts.Put{"db": {"g14": fresh}}) p.putWaits(ctx, map[string]map[string]puts.Put{"db": {"g14": fresh.Said(at.Add(4*time.Hour), puts.Done, "runs")}}) - if len(events) != 0 || kept.values["g14.db"] == nil { - t.Fatalf("a refused say was kept as said: %v, record %q", events, kept.values["g14.db"]) + if len(events) != 0 || kept.values["g14.db"] != nil { + t.Fatalf("a refused say was kept as said: %v, record %q (the record is written once the bus took the wait-held, review r1 finding 3)", events, kept.values["g14.db"]) } refused = false p.after = nil