H-A-749: the publish item records go with their readers; a drop made before is carried onto its reservation at the start

builder_publish_queue no longer lists publish items, the re-ask watch no longer reads mode publish, and at its start the builder carries each answered publish-drop ask onto its reservation as dropped, then deletes each record of mode publish, saying the counts once.

Builds: design 47 H2 H-A-749 (1), (2), H-A-746
This commit is contained in:
2026-11-25 18:37:09 +01:00
parent ed846d58ab
commit 070f62ceca
10 changed files with 120 additions and 85 deletions
@@ -400,7 +400,7 @@ func main() {
tools = append(tools, reservedTools(rr, buildAgain, time.Now, meshhandactsclient.Recorder{Ask: stdio.Ask})...)
as := stdio.State(versions.ActedState)
tools = append(tools, publishActTools(buildAgain, time.Now, meshhandactsclient.Recorder{Ask: stdio.Ask})...)
tools = append(tools, republishTools(publishQueueVia(stdio.Ask), readerOf(as), readerOf(reasks), buildAgain, time.Now, meshhandactsclient.Recorder{Ask: stdio.Ask})...)
tools = append(tools, republishTools(publishQueueVia(stdio.Ask), readerOf(as), buildAgain, time.Now, meshhandactsclient.Recorder{Ask: stdio.Ask})...)
ws := stdio.State(build.WaitsState)
wr := versions.Reader{Keys: ws.Keys, Get: func(key string) (json.RawMessage, bool, error) {
e, err := ws.Get(key)
@@ -114,7 +114,7 @@ func publishActTools(ask queueAsker, now func() time.Time, acts meshhandactsclie
// republishTools are builder_publish_again and builder_publish_queue (design 47 G §588). The publish again is a hand act:
// it is recorded in the hand-act log first, then written to reasks for the builder, which owns the queue, to answer
// with the versions it queued.
func republishTools(queue func(module, version string) (meshbuilds.PublishQueueAnswer, error), acted, reasks versions.Reader, ask queueAsker, now func() time.Time, acts meshhandactsclient.Recorder) []stdio.Tool {
func republishTools(queue func(module, version string) (meshbuilds.PublishQueueAnswer, error), acted versions.Reader, ask queueAsker, now func() time.Time, acts meshhandactsclient.Recorder) []stdio.Tool {
str := func(args map[string]any, k string) string { s, _ := args[k].(string); return strings.TrimSpace(s) }
return []stdio.Tool{
{Name: versions.PublishAgainTool, Description: versions.PublishAgainDescription,
@@ -153,7 +153,7 @@ func republishTools(queue func(module, version string) (meshbuilds.PublishQueueA
if err != nil {
return nil, err
}
return versions.AnswerQueue(q, acted, reasks)
return versions.AnswerQueue(q, acted)
}},
}
}
@@ -90,7 +90,7 @@ func TestAStateIsReadInOnePage(t *testing.T) {
Queue: []meshbuilds.PublishQueueAnswerQueueItem{{Holder: "oci", Module: "m", Version: "0.1.0", Class: versions.PublishClassStart}}})
}
none := versions.Reader{Keys: func() ([]string, error) { return nil, nil }}
tools := republishTools(publishQueueVia(ask), none, none, nil, time.Now, meshhandactsclient.Recorder{})
tools := republishTools(publishQueueVia(ask), none, nil, time.Now, meshhandactsclient.Recorder{})
raw, err := tools[1].Run(map[string]any{"module": "m"})
a, _ := raw.(versions.Queued)
if err != nil || fmt.Sprint(asked) != "[seat:mesh-builds.publish-queue m]" || a.Total != 9000 || len(a.Queue) != 1 || a.Queue[0].Module != "m" ||
@@ -335,7 +335,8 @@ func run(ctx context.Context, getenv func(string) string, logOut io.Writer) erro
pq.take = items.take
// At the start, a built reservation the catalog holds released is taken in as its version-released would (H-A-484 addendum),
// before the publish items of the rest are asked.
reasksKV.Ready(func(jetstream.KeyValue) {
reasksKV.Ready(func(kv jetstream.KeyValue) {
carryDrops(ctx, kv, reserved.r, logger.Printf) // before any event is heard: a drop made before is kept (H-A-749)
reserved.releasedAtStart(ctx)
})
wirePublishItems(reserved, items, awaitedReserved{awaitedState{b: actedKV, bucket: own.acted}}, releasedKept)
@@ -84,6 +84,23 @@ func (k *itemKV) Delete(_ context.Context, key string, _ ...jetstream.KVDeleteOp
return nil
}
// ListKeys lists the keys the state holds, as the bus's key lister does.
func (k *itemKV) ListKeys(context.Context, ...jetstream.WatchOpt) (jetstream.KeyLister, error) {
k.mu.Lock()
defer k.mu.Unlock()
ch := make(chan string, len(k.vals))
for key := range k.vals {
ch <- key
}
close(ch)
return itemKeys{ch}, nil
}
type itemKeys struct{ ch chan string }
func (l itemKeys) Keys() <-chan string { return l.ch }
func (l itemKeys) Stop() error { return nil }
// itemMsgFake is an item pulled from the work queue: what take settled it with.
type itemMsgFake struct {
data []byte
@@ -208,6 +225,34 @@ func TestAPublishIsPutOnceAtABuildsEndAndNeverOnATimer(t *testing.T) {
if !slices.Equal(m.settled, []string{"term"}) || len(sent) != before+1 || len(words) != 1 || words[0] != "given up after 10 deliveries" {
t.Errorf("a tenth delivery: settled %v, said %v, want it terminated unsent with %q", m.settled, words, "given up after 10 deliveries")
}
// design 47 H2 H-A-749 (2): at the start an answered publish-drop ask is carried onto its version's reservation as dropped, where
// it stands and holds none; a refused one is not; each record of mode publish is deleted, the drop's own ask staying.
res := &versions.Reserver{Store: &memReserve{vals: map[string][]byte{}, revs: map[string]uint64{}}, Now: func() time.Time { return now },
ReleasedOf: func(context.Context, string) ([]string, error) { return nil, nil }}
for _, m := range []string{"m", "n"} {
rv, err := res.Reserve(ctx, m, "0.1", []string{m + ":a"}, versions.Source{Fingerprint: "src2:" + m})
if err != nil {
t.Fatal(err)
}
if _, _, err := res.Built(ctx, m, rv.Version, map[string]string{m + ":a": "sha256:a"}, nil); err != nil {
t.Fatal(err)
}
}
old := &itemKV{vals: map[string][]byte{}, revs: map[string]uint64{}}
put := func(key string, r build.ReAsk) {
raw, _ := json.Marshal(r)
old.vals[key], old.revs[key] = raw, 1
}
put("publish-drop.1", build.ReAsk{V: 1, Mode: build.ModePublishDrop, Module: "m", Version: "0.1.0", By: "op@node", Why: "a person's drop", At: now, Answered: now})
put("publish-drop.2", build.ReAsk{V: 1, Mode: build.ModePublishDrop, Module: "n", Version: "0.1.0", By: "op@node", Why: "refused", At: now, Answered: now, Refused: true})
put("publish.m.0.1.0", build.ReAsk{V: 1, Mode: "publish", Module: "m", Version: "0.1.0", Answer: "dropped by op@node: a person's drop", Answered: now})
carryDrops(ctx, old, res, t.Logf)
if m0, _, _ := res.Of(ctx, "m", "0.1.0"); m0.Dropped == nil || m0.Dropped.By != "op@node" || m0.Dropped.Why != "a person's drop" || !m0.Dropped.At.Equal(now) {
t.Errorf("m's reservation reads dropped %+v, want the earlier drop carried onto it", m0.Dropped)
}
if n0, _, _ := res.Of(ctx, "n", "0.1.0"); n0.Dropped != nil || old.has("publish.m.0.1.0") || !old.has("publish-drop.1") || !old.has("publish-drop.2") {
t.Errorf("n dropped %+v, publish record left %v: want a refused drop not carried, the record gone and the asks kept", n0.Dropped, old.has("publish.m.0.1.0"))
}
}
// Design 47 H2 H-A-483 (third addendum): a holder is a module name, so one ending in -live or -again, the class durables of an
@@ -45,11 +45,6 @@ func reAsked(ctx context.Context, b askAgainer, acts versions.Acts, items *publi
if r.Answer != "" {
return
}
if r.Mode == build.ModePublish {
// A version's publish record (builder_publish_drop's, or one an earlier builder wrote): read by take, never tried on a timer
// (design 47 H2 H-A-606).
return
}
if r.Mode == build.ModeBuild {
// The answer comes when the claim and the asks are written, so it is made beside the watch, not in it.
go builtAgain(ctx, b, kv, e, r, logf)
@@ -5,6 +5,7 @@ import (
"encoding/json"
"errors"
"fmt"
"slices"
"strconv"
"time"
@@ -13,6 +14,7 @@ import (
"git.novox.be/novox/mesh-sdk/go/process"
"mesh-delivery-builder/internal/build"
"mesh-delivery-builder/internal/versions"
)
@@ -213,3 +215,56 @@ func deleteUnknown(ctx context.Context, kv jetstream.KeyValue, logf func(string,
logf("the state %s: %d keys of no form the builder writes were deleted at the start", versions.ActedState, n)
}
}
// carryDrops is the builder's start of the re-ask state (design 47 H2 H-A-749 (2)): each answered publish-drop ask is carried onto
// its version's reservation as dropped (the ask's by, why and at) where the reservation stands and holds none, so a drop made before
// is kept; then each record of mode publish, whose readers are gone, is deleted. It says the counts once.
func carryDrops(ctx context.Context, kv jetstream.KeyValue, r *versions.Reserver, logf func(string, ...any)) {
l, err := kv.ListKeys(ctx)
if err != nil {
logf("the keys of the state %s are not listed, so no earlier drop is carried and no publish record deleted: %v", build.ReAsksState, err)
return
}
var keys []string
for k := range l.Keys() {
keys = append(keys, k)
}
_ = l.Stop()
slices.Sort(keys)
var carried, deleted int
var records []string
for _, k := range keys {
e, err := kv.Get(ctx, k)
if err != nil {
continue
}
ask, err := build.DecodeReAsk(e.Value())
if err != nil {
continue
}
switch {
case ask.Mode == "publish": // the record of a version's publish item, written before H-A-746
records = append(records, k)
case ask.Mode == build.ModePublishDrop && !ask.Answered.IsZero() && !ask.Refused:
v, found, err := r.Of(ctx, ask.Module, ask.Version)
if err != nil || !found || v.Dropped != nil {
continue
}
if ok, err := r.MarkDropped(ctx, ask.Module, ask.Version, ask.By, ask.Why, ask.At); err != nil {
logf("the drop of %s %s by %s is not carried onto its reservation: %v", ask.Module, ask.Version, ask.By, err)
} else if ok {
carried++
}
}
}
for _, k := range records {
if err := kv.Delete(ctx, k); err != nil {
logf("the publish record %s of the state %s is not deleted: %v", k, build.ReAsksState, err)
continue
}
deleted++
}
if carried > 0 || deleted > 0 {
logf("the state %s at the start: %d earlier drops carried onto their reservations as dropped, %d publish records deleted", build.ReAsksState, carried, deleted)
}
}
@@ -61,15 +61,8 @@ type ReAsk struct {
Name string `json:"name,omitempty"`
Version string `json:"version,omitempty"`
Queued []string `json:"queued,omitempty"`
// Tries and NextTry are a ModePublish value's (design 47 G A-457a): the publishes tried so far, and when the next is.
Tries int `json:"tries,omitempty"`
NextTry time.Time `json:"next-try,omitzero"`
}
// ModePublish is the Mode of a reasks value that is a version's publish item (design 47 G A-457a): Module and Version name it,
// Tries and NextTry say where it stands; it is written by the builder itself, when a build ends and at its start.
const ModePublish = "publish"
// ModeRepublish is the Mode of a reasks value of builder_publish_again (design 47 G §588): Module and Version, each
// optional, name what to publish again; the builder answers with the versions it queued (Queued).
const ModeRepublish = "republish"
@@ -500,7 +500,7 @@ func (r *Republisher) Republish(ctx context.Context, module, version string) ([]
// Queued is builder_publish_queue's answer, small at any size of the queue (design 47 H H-A-290 (5)): the queued versions'
// count per holder and class with the oldest and newest queue time, the first PublishQueueListCap of them in the order they are
// sent (each with its class), the acted keys with their sequences, and the publish items, counted and capped alike.
// sent (each with its class) and the acted keys with their sequences, counted and capped alike.
type Queued struct {
Total int `json:"total"`
Counts []QueueCount `json:"counts"`
@@ -510,10 +510,6 @@ type Queued struct {
// Listed says how many of Total the answer lists.
Listed int `json:"listed"`
Acted map[string]uint64 `json:"acted"`
// ItemCount is how many publish items stand; Items the first of them by their next try (design 47 G A-457a), each with its
// tries and its next try; an item that ended says why.
ItemCount int `json:"item-count"`
Items []PublishItem `json:"items"`
// Capped says a state held more than the one page read of it (H-A-293); Read names each such state, the keys read and its
// size. Every count above is then of the keys read.
Capped bool `json:"capped,omitempty"`
@@ -537,27 +533,6 @@ type QueueCount struct {
Newest time.Time `json:"newest"`
}
// PublishItemKeyPrefix opens the key of a publish item in the builder's re-ask state.
const PublishItemKeyPrefix = "publish."
// PublishItem is the builder's own record of a version's publish (design 47 G A-457a): a value of the re-ask state, its tries and
// its next try. It is asked when the build ends and at the builder's start, rewritten with one more try and a longer pause after
// each, and deleted when the version is released; after the setting's number of tries it ends, saying so (Answer), and stays.
type PublishItem struct {
Module string `json:"module"`
Version string `json:"version"`
Tries int `json:"tries"`
NextTry time.Time `json:"next-try"`
Why string `json:"why,omitempty"`
Answer string `json:"answer,omitempty"`
Answered time.Time `json:"answered,omitzero"`
}
// PublishItemKey is the key of module's version's publish item.
func PublishItemKey(module, version string) string {
return PublishItemKeyPrefix + EscapeToken(module) + "." + EscapeToken(version)
}
// PublishQueueOf is the seat mesh-builds' verb publish-queue (design 47 H2 §793; H-A-454): the builder's read of its own work queue
// publishes (queue, its items read in bulk, capped past its bound), small at any size of the queue (H-A-290 (5)): the items' count
// per holder and class with the oldest and newest queue time, and the first PublishQueueListCap in stream order, the order the
@@ -625,10 +600,10 @@ func PublishQueueOf(queue Reader, module, version string) (meshbuilds.PublishQue
return out, nil
}
// AnswerQueue is builder_publish_queue: the seat mesh-builds' publish-queue answer the builder gave (q, §793, H-A-454) and the two
// states as the tools bundle reads them (H-A-290 (5)), each read in bulk (Reader.All), capped at PublishQueueListCap.
func AnswerQueue(q meshbuilds.PublishQueueAnswer, acted, reasks Reader) (Queued, error) {
out := Queued{Total: int(q.Total), Counts: []QueueCount{}, Queue: []QueuedPublish{}, Listed: int(q.Listed), Acted: map[string]uint64{}, Items: []PublishItem{}}
// AnswerQueue is builder_publish_queue: the seat mesh-builds' publish-queue answer the builder gave (q, §793, H-A-454) and the state
// acted as the tools bundle reads it (H-A-290 (5)), read in bulk (Reader.All), capped at PublishQueueListCap.
func AnswerQueue(q meshbuilds.PublishQueueAnswer, acted Reader) (Queued, error) {
out := Queued{Total: int(q.Total), Counts: []QueueCount{}, Queue: []QueuedPublish{}, Listed: int(q.Listed), Acted: map[string]uint64{}}
if q.Capped {
out.capped(&StateCapped{State: PublishesLocal, KeysRead: int(q.ItemsRead), StateKeys: uint64(q.QueueItems), StateBytes: uint64(q.QueueBytes)})
}
@@ -649,25 +624,6 @@ func AnswerQueue(q meshbuilds.PublishQueueAnswer, acted, reasks Reader) (Queued,
out.Acted[k] = a.Sequence
}
}
rvals, c, err := reasks.values("reasks")
out.capped(c)
if err != nil {
return out, fmt.Errorf("the re-ask state cannot be read: %w", err)
}
for k, raw := range rvals {
if !strings.HasPrefix(k, PublishItemKeyPrefix) {
continue
}
var it PublishItem
if json.Unmarshal(raw, &it) == nil {
out.Items = append(out.Items, it)
}
}
out.ItemCount = len(out.Items)
sort.SliceStable(out.Items, func(i, j int) bool { return out.Items[i].NextTry.Before(out.Items[j].NextTry) })
if len(out.Items) > PublishQueueListCap {
out.Items = out.Items[:PublishQueueListCap]
}
return out, nil
}
@@ -676,13 +632,12 @@ const PublishQueueDescription = "The versions the builder will publish again (it
"how many each registry seat holder's items hold per class (live, a build a pipeline's step waits on, or again, asked again at a start) " +
"with the oldest and newest queue time; the first 50 in stream order, the order each holder's one consumer pulls them, each with its class, its artifacts' digests and when it was " +
"queued (module, and version, list only that module's, so one item's class can be read); the keys of the state " + ActedState + ", the last stream " +
"sequence the builder acted on for each subject; and the publish items (design 47 G A-457a), counted, the first 50 by next try, each version's " +
"tries and next try, and why one ended (its tries spent, or dropped). A read past its pages is answered capped, naming the items read and the " +
"sequence the builder acted on for each subject. A read past its pages is answered capped, naming the items read and the " +
"queue's size (H-A-293). Read-only."
// PublishDropDescription is builder_publish_drop's (§793).
const PublishDropDescription = "Drop one version's publish (design 47 H2 section 793): its items on the work queue " + PublishesLocal + " are " +
"terminated at every holder, and its publish item ended with who dropped it and why, so it is never sent or put again; builder_publish_again " +
"terminated at every holder, and its reservation says who dropped it and why, so it is never sent or put again (design 47 H2 H-A-746); builder_publish_again " +
"asks it anew. A hand act, recorded in the hand-act log first."
// PublishAgainDescription is builder_publish_again's description.
@@ -300,15 +300,6 @@ func TestRepublish(t *testing.T) {
}
b.holders = map[string][]string{SeatArtifacts: {"oci"}}
_, _ = b.Republish(ctx, "", "")
items := Reader{Keys: func() ([]string, error) { return []string{"publish.m.0.1.0", "reserve-done.m.x"}, nil },
Get: func(k string) (json.RawMessage, bool, error) {
return json.RawMessage(`{"mode":"publish","module":"m","version":"0.1.0","tries":3,"next-try":"2026-11-17T01:00:00Z","why":"w"}`), true, nil
}}
none := Reader{Keys: func() ([]string, error) { return nil, nil }}
answer, err := answerOf(none, none, items, "", "")
if err != nil || len(answer.Items) != 1 || answer.Items[0].Tries != 3 || answer.Items[0].Version != "0.1.0" {
t.Errorf("builder_publish_queue items = %+v (%v), want the one publish item with its tries", answer.Items, err)
}
})
}
@@ -509,8 +500,8 @@ func TestAQueuedItemIsSentWhileItsBuildStands(t *testing.T) {
}
// answerOf is builder_publish_queue whole: the builder's verb publish-queue over its work queue (queue), then the tool over the
// verb's answer and the two states (design 47 H2 §793, H-A-454).
func answerOf(queue, acted, reasks Reader, module, version string) (Queued, error) {
// verb's answer and the state (design 47 H2 §793, H-A-454).
func answerOf(queue, acted Reader, module, version string) (Queued, error) {
q, err := PublishQueueOf(queue, module, version)
if err != nil {
return Queued{}, err
@@ -523,7 +514,7 @@ func answerOf(queue, acted, reasks Reader, module, version string) (Queued, erro
if err := json.Unmarshal(raw, &heard); err != nil {
return Queued{}, err
}
return AnswerQueue(heard, acted, reasks)
return AnswerQueue(heard, acted)
}
// TestThePublishQueueToolAnswersAtAnySize: design 47 H H-A-290 (5) — builder_publish_queue over two thousand queued versions answers
@@ -550,7 +541,7 @@ func TestThePublishQueueToolAnswersAtAnySize(t *testing.T) {
Get: func(k string) (json.RawMessage, bool, error) { gets++; v, ok := vals[k]; return v, ok, nil }}
none := Reader{Keys: func() ([]string, error) { return nil, nil }}
start := time.Now()
a, err := answerOf(queue, none, none, "", "")
a, err := answerOf(queue, none, "", "")
took := time.Since(start)
raw, _ := json.Marshal(a)
if err != nil || took > time.Second || a.Total != 2000 || len(a.Queue) != PublishQueueListCap || a.Listed != PublishQueueListCap || len(raw) > 64<<10 || gets != 0 {
@@ -564,7 +555,7 @@ func TestThePublishQueueToolAnswersAtAnySize(t *testing.T) {
t.Fatalf("the list holds %s at %d, want m%d: the items in stream order, whatever their holder or class", q.Module, i, i)
}
}
one, err := answerOf(queue, none, none, "m1995", "")
one, err := answerOf(queue, none, "m1995", "")
if err != nil || len(one.Queue) != 1 || one.Queue[0].Class != PublishClassLive || one.Total != 2000 {
t.Errorf("one module's answer %+v (%v), want its item with its class", one.Queue, err)
}
@@ -572,12 +563,12 @@ func TestThePublishQueueToolAnswersAtAnySize(t *testing.T) {
t.Errorf("a state one page holds is answered capped: %+v", a.Read)
}
queue.All = func() (StatePage, error) { return StatePage{Values: vals, More: true, Keys: 5000, Bytes: 3 << 20}, nil }
if c, err := answerOf(queue, none, none, "", ""); err != nil || !c.Capped || len(c.Read) != 1 || c.Read[0].State != PublishesLocal ||
if c, err := answerOf(queue, none, "", ""); err != nil || !c.Capped || len(c.Read) != 1 || c.Read[0].State != PublishesLocal ||
c.Read[0].KeysRead != 2000 || c.Read[0].StateKeys != 5000 || c.Read[0].StateBytes != 3<<20 || c.Total != 2000 {
t.Errorf("a state past its one page answered capped %v, read %+v (%v): want capped, 2000 keys read of 5000, its size said (H-A-293)", c.Capped, c.Read, err)
}
queue.All = func() (StatePage, error) { return StatePage{}, errors.ErrUnsupported }
if b, err := answerOf(queue, none, none, "", ""); err != nil || b.Total != 2000 || gets != 2000 {
if b, err := answerOf(queue, none, "", ""); err != nil || b.Total != 2000 || gets != 2000 {
t.Errorf("a reader with no bulk read answered %d (%v) with %d single reads, want every key read one by one", b.Total, err, gets)
}
}