diff --git a/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/main.go b/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/main.go index c69055128..ac96517f8 100644 --- a/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/main.go +++ b/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/main.go @@ -310,8 +310,16 @@ func run(ctx context.Context, getenv func(string) string, logOut io.Writer) erro return err } reasksKV := process.AwaitBucket(ctx, js, conn, builds.StateBucket(cred.Module, build.ReAsksState), "builder_ask_again is not heard; an ask that can no longer be answered is still asked again by itself", logger.Printf) - // A version's publish is the builder's own work until its release is written (design 47 G A-457a): an item of the re-ask state. - items := &publishItems{reasks: reasksKV.KV, now: time.Now, logf: logger.Printf, unreleased: republish.Unreleased, + // A version's publish is the builder's own work until its release is written (design 47 G A-457a); a publish dropped by hand is + // kept on its reservation (H-A-746). + items := &publishItems{now: time.Now, logf: logger.Printf, unreleased: republish.Unreleased, + dropMark: func(ctx context.Context, module, version, by, why string) error { + found, err := reserved.r.MarkDropped(ctx, module, version, by, why, time.Now()) + if err == nil && !found { + err = fmt.Errorf("no reservation of %s stands at %s: there is no publish to drop", module, version) + } + return err + }, reserved: func(ctx context.Context, module, version string) (versions.ReservedVersion, bool, error) { return reserved.r.Of(ctx, module, version) }, diff --git a/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/publishes_integration_test.go b/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/publishes_integration_test.go index 3fd5d84e2..8ce3d06ed 100644 --- a/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/publishes_integration_test.go +++ b/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/publishes_integration_test.go @@ -51,7 +51,6 @@ func TestThePublishQueueLosesNoItemAndNeverSendsADroppedItem(t *testing.T) { t.Run("one consumer", func(t *testing.T) { const holder = "paused" bound := testbus.SetupBound(t) - kv := &itemKV{vals: map[string][]byte{}, revs: map[string]uint64{}} type send struct { module string at time.Time @@ -59,7 +58,7 @@ func TestThePublishQueueLosesNoItemAndNeverSendsADroppedItem(t *testing.T) { var mu sync.Mutex published, failed := map[string]bool{}, map[string]bool{} sentCh := make(chan send, 16) - items := &publishItems{reasks: func() jetstream.KeyValue { return kv }, + items := &publishItems{dropMark: func(context.Context, string, string, string, string) error { return nil }, now: time.Now, logf: t.Logf, reserved: func(_ context.Context, m, v string) (versions.ReservedVersion, bool, error) { mu.Lock() @@ -225,12 +224,11 @@ func TestThePublishQueueLosesNoItemAndNeverSendsADroppedItem(t *testing.T) { }) t.Run("the queue, the move, tags and backlog", func(t *testing.T) { - kv := &itemKV{vals: map[string][]byte{}, revs: map[string]uint64{}} var mu sync.Mutex var sent []string published := map[string]bool{} // the modules whose publish is heard sentCh := make(chan string, 200) - items := &publishItems{reasks: func() jetstream.KeyValue { return kv }, + items := &publishItems{dropMark: func(context.Context, string, string, string, string) error { return nil }, now: time.Now, logf: t.Logf, reserved: func(_ context.Context, m, v string) (versions.ReservedVersion, bool, error) { mu.Lock() diff --git a/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/publishitem.go b/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/publishitem.go index d05442494..ae7b75c39 100644 --- a/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/publishitem.go +++ b/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/publishitem.go @@ -20,7 +20,6 @@ import ( "git.novox.be/novox/mesh-sdk/go/catalogue" "git.novox.be/novox/mesh-sdk/go/process" - "mesh-delivery-builder/internal/build" "mesh-delivery-builder/internal/versions" ) @@ -31,7 +30,9 @@ import ( // publishItems asks and takes the publishes of reserved versions. type publishItems struct { - reasks func() jetstream.KeyValue // the re-ask state: the record of a dropped version's publish (builder_publish_drop), nil until bound + // dropMark records on a version's reservation that its publish was dropped by hand, by whom and why (design 47 H2 H-A-746); nil + // drops none. + dropMark func(ctx context.Context, module, version, by, why string) error now func() time.Time reserved func(ctx context.Context, module, version string) (versions.ReservedVersion, bool, error) republish func(ctx context.Context, module, version string) ([]string, error) @@ -71,22 +72,6 @@ func (p *publishItems) Done(ctx context.Context, module, version string) { } } -// droppedWhy is the answer of a version's ended publish record (builder_publish_drop), empty when none ended it. -func (p *publishItems) droppedWhy(ctx context.Context, module, version string) string { - kv := p.reasks() - if kv == nil { - return "" - } - e, err := kv.Get(ctx, versions.PublishItemKey(module, version)) - if err != nil { - return "" - } - if r, err := build.DecodeReAsk(e.Value()); err == nil { - return r.Answer - } - return "" -} - // itemMsg is what take reads of an item pulled from the work queue (jetstream.Msg). type itemMsg interface { Data() []byte @@ -118,11 +103,6 @@ func (p *publishItems) take(ctx context.Context, m itemMsg) { _ = m.Term() return } - if why := p.droppedWhy(ctx, q.Module, q.Version); why != "" { - p.logf("%s %s at %s is terminated: its publish ended: %s", q.Module, q.Version, q.Holder, why) - _ = m.Term() - return - } v, found, err := p.reserved(ctx, q.Module, q.Version) if err != nil { p.logf("%s %s: its reservation cannot be read (%v); the item is given again after the ack wait", q.Module, q.Version, err) @@ -673,14 +653,17 @@ func (pq *publishQueue) reader(ctx context.Context) versions.Reader { }} } -// drop is builder_publish_drop (§793): the version's publish item is ended with who dropped it and why, so no item of it is ever +// drop is builder_publish_drop (§793): the version's reservation keeps who dropped its publish and why (H-A-746), so no item of it is ever // sent or put again; its items waiting on the work queue are removed, and one held is terminated. func (pq *publishQueue) drop(ctx context.Context, items *publishItems, module, version, by, why string) (string, error) { qs, st, err := pq.itemsOf(ctx, module, version, "") if err != nil { return "", fmt.Errorf("the work queue %s cannot be read: %w", versions.PublishesLocal, err) } - if err := items.endItem(ctx, module, version, fmt.Sprintf("dropped by %s: %s", by, why)); err != nil { + if items.dropMark == nil { + return "", errors.New("this builder keeps no reservations") + } + if err := items.dropMark(ctx, module, version, by, why); err != nil { return "", err } for subj := range qs { @@ -694,48 +677,6 @@ func (pq *publishQueue) drop(ctx context.Context, items *publishItems, module, v return fmt.Sprintf("the publish of %s %s is dropped: %d items terminated, and none is sent or put again", module, version, len(qs)), nil } -// endItem writes the version's publish item ended with answer, made when none stands: Try and take then leave it, and a start -// asks it no more. -func (p *publishItems) endItem(ctx context.Context, module, version, answer string) error { - kv := p.reasks() - if kv == nil { - return errors.New("the re-ask state is not on the bus yet") - } - key := versions.PublishItemKey(module, version) - for range 5 { - r := build.ReAsk{V: 1, Why: "the build ended with its version unreleased", By: "the builder", At: p.now().UTC(), Mode: build.ModePublish, Module: module, Version: version} - e, err := kv.Get(ctx, key) - var rev uint64 - switch { - case errors.Is(err, jetstream.ErrKeyNotFound): - case err != nil: - return err - default: - if r, err = build.DecodeReAsk(e.Value()); err != nil { - return err - } - rev = e.Revision() - } - r.Answer, r.Answered = answer, p.now().UTC() - data, err := json.Marshal(r) - if err != nil { - return err - } - if rev == 0 { - _, err = kv.Create(ctx, key, data) - } else { - _, err = kv.Update(ctx, key, data, rev) - } - if err == nil { - return nil - } - if !errors.Is(err, jetstream.ErrKeyExists) && !errors.Is(err, jetstream.ErrKeyRevisionMismatch) { - return err - } - } - return errors.New("the publish item of " + module + " " + version + " changed five times while it was ended") -} - // moveOld puts the items of the old state publish-queue on the work queue once at the start (§793): each under its message id, so // a move run twice puts each once within the duplicates window; then the builder, which made that bucket, removes it itself // (H-A-461 (1): the bus module's keeper holds no entry for it, so its drop refuses it). A builder whose bus holds no such diff --git a/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/publishitem_test.go b/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/publishitem_test.go index 5fc991d67..13228ca1f 100644 --- a/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/publishitem_test.go +++ b/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/publishitem_test.go @@ -135,7 +135,7 @@ func TestAPublishIsPutOnceAtABuildsEndAndNeverOnATimer(t *testing.T) { reserved := true v := versions.ReservedVersion{Module: "m", Version: "0.1.2", Artifacts: map[string]string{"m:a": "sha256:a"}, BuiltAt: now} fate := versions.SendFate{Failed: "no holder answers"} - p := &publishItems{reasks: func() jetstream.KeyValue { return kv }, now: func() time.Time { return now }, logf: t.Logf, + p := &publishItems{now: func() time.Time { return now }, logf: t.Logf, reserved: func(_ context.Context, _, ver string) (versions.ReservedVersion, bool, error) { if ver == "0.3.0" { // every artifact published, its release not written yet return versions.ReservedVersion{Module: "m", Version: ver, Artifacts: map[string]string{"m:a": "sha256:a"}, Published: map[string]bool{"m:a": true}, BuiltAt: now}, true, nil @@ -183,18 +183,18 @@ func TestAPublishIsPutOnceAtABuildsEndAndNeverOnATimer(t *testing.T) { t.Errorf("an item of a version no longer reserved settled %v, want term", m.settled) } reserved = true - r, _ := json.Marshal(build.ReAsk{V: 1, Mode: build.ModePublish, Module: "m", Version: "0.1.2", Answer: "dropped by a person: why"}) - kv.vals[versions.PublishItemKey("m", "0.1.2")], kv.revs[versions.PublishItemKey("m", "0.1.2")] = r, 1 + // design 47 H2 H-A-746: a publish dropped by hand is kept on the reservation, read as its standing: the item is terminated unsent. + v.Dropped = &versions.PublishDropped{By: "a person", Why: "why", At: now} before := len(sent) m = queuedMsg(t, "m", "0.1.2") p.take(ctx, m) if !slices.Equal(m.settled, []string{"term"}) || len(sent) != before { t.Errorf("a dropped publish: settled %v, sent %d more, want it terminated and not sent", m.settled, len(sent)-before) } + v.Dropped = nil // design 47 H2 H-A-653, H-A-703: the delivery where an item reaches the queue's ten ends it and raises version-unreleased. var words []string p.unreleased = func(_ context.Context, _ versions.ReservedVersion, why string) { words = append(words, why) } - kv.vals, kv.revs = map[string][]byte{}, map[string]uint64{} before = len(sent) m = queuedMsg(t, "m", "0.1.2") m.delivered = publishesMaxDeliver - 1 diff --git a/modules/mesh-delivery-builder/internal/versions/republish.go b/modules/mesh-delivery-builder/internal/versions/republish.go index 475727109..398d8deda 100644 --- a/modules/mesh-delivery-builder/internal/versions/republish.go +++ b/modules/mesh-delivery-builder/internal/versions/republish.go @@ -236,6 +236,8 @@ func NotStanding(v ReservedVersion, found bool) string { return "its reservation stands no more at the version (released, dropped or taken over)" case hasKey(v.Withdrawn, v.Version): return "its reservation was withdrawn" + case v.Dropped != nil: + return "its publish was dropped by " + v.Dropped.By + ": " + v.Dropped.Why } return "" } @@ -479,6 +481,16 @@ func (r *Republisher) Republish(ctx context.Context, module, version string) ([] vs = append(vs, v) } } + // builder_publish_again asks a dropped publish anew: the drop is cleared in the write before its put (H-A-746). + for i := range vs { + if vs[i].Dropped == nil { + continue + } + if _, err := r.Reserved.ClearDropped(ctx, vs[i].Module, vs[i].Version); err != nil { + return nil, err + } + vs[i].Dropped = nil + } if len(vs) == 0 { return nil, fmt.Errorf("%s holds no reserved version%s with an artifact unpublished", module, map[bool]string{true: " " + version, false: ""}[version != ""]) } @@ -945,6 +957,42 @@ func (r *Reserver) MarkUnstored(ctx context.Context, module, version, artifact, } } +// MarkDropped records on module's reserved version that its publish was dropped by hand, by whom, why and when, written by compare-and-set +// (design 47 H2 H-A-746). It answers whether the reservation stands at the version; a version not reserved is no publish to drop. +func (r *Reserver) MarkDropped(ctx context.Context, module, version, by, why string, at time.Time) (bool, error) { + return r.setDropped(ctx, module, version, &PublishDropped{By: by, Why: why, At: at.UTC()}) +} + +// ClearDropped clears the drop of module's version: builder_publish_again asks it anew (H-A-746). It answers whether one was cleared. +func (r *Reserver) ClearDropped(ctx context.Context, module, version string) (bool, error) { + return r.setDropped(ctx, module, version, nil) +} + +func (r *Reserver) setDropped(ctx context.Context, module, version string, d *PublishDropped) (bool, error) { + series, _, err := SplitVersion(version) + if err != nil { + return false, err + } + key := ReservedKey(module, series) + for { + cur, rev, found, err := r.get(ctx, key) + if err != nil || !found || cur.Version != version { + return false, err + } + if d == nil && cur.Dropped == nil { + return false, nil + } + cur.Dropped = d + raw, _ := json.Marshal(cur) + if _, err := r.Store.Update(ctx, key, raw, rev); errors.Is(err, ErrConflict) { + continue + } else if err != nil { + return false, fmt.Errorf("%s cannot be written: %w", key, err) + } + return true, nil + } +} + // HeldVersion is one version a registry seat's verb versions answers of a name, with its digest. type HeldVersion struct { Version, Digest string diff --git a/modules/mesh-delivery-builder/internal/versions/republish_test.go b/modules/mesh-delivery-builder/internal/versions/republish_test.go index 1e9f7479b..f6eb85c2d 100644 --- a/modules/mesh-delivery-builder/internal/versions/republish_test.go +++ b/modules/mesh-delivery-builder/internal/versions/republish_test.go @@ -483,6 +483,29 @@ func TestAQueuedItemIsSentWhileItsBuildStands(t *testing.T) { t.Errorf("an unreadable reservation answered %+v: want it unread", f) } }) + // design 47 H2 H-A-746: a publish dropped by hand is kept on its reservation, which its standing reads: an event does not put it, + // a send ends it unsent, and builder_publish_again puts it and clears the drop. + t.Run("a publish dropped by hand is passed over and asked anew by builder_publish_again", func(t *testing.T) { + b := newRepublishBed(t) + v := b.reserve(t, "d") + b.held = true + b.holders = map[string][]string{SeatArtifacts: {"oci"}} + if found, err := b.Reserved.MarkDropped(ctx, "d", v.Version, "op@node", "a test drops it", b.now); err != nil || !found { + t.Fatalf("drop: %v %v", found, err) + } + if _, err := b.Republish(ctx, "", ""); err != nil || len(b.puts) != 0 { + t.Errorf("an event put %d items (%v), want the dropped version passed over", len(b.puts), err) + } + if f := b.SendQueued(ctx, b.queueAt(v, PublishClassLive, b.now)); f.Ended == "" || !strings.Contains(f.Ended, "op@node") || len(b.sent) != 0 { + t.Errorf("a send of the dropped version answered %+v, sent %d: want it ended unsent, naming who dropped it", f, len(b.sent)) + } + if _, err := b.Republish(ctx, "d", v.Version); err != nil || len(b.puts) != 1 { + t.Errorf("builder_publish_again put %d items (%v), want the one", len(b.puts), err) + } + if cur, ok, err := b.Reserved.Of(ctx, "d", v.Version); err != nil || !ok || cur.Dropped != nil { + t.Errorf("after builder_publish_again the reservation is %+v (%v), want its drop cleared", cur, err) + } + }) } // answerOf is builder_publish_queue whole: the builder's verb publish-queue over its work queue (queue), then the tool over the diff --git a/modules/mesh-delivery-builder/internal/versions/reserve.go b/modules/mesh-delivery-builder/internal/versions/reserve.go index 64a345125..800f42199 100644 --- a/modules/mesh-delivery-builder/internal/versions/reserve.go +++ b/modules/mesh-delivery-builder/internal/versions/reserve.go @@ -75,6 +75,9 @@ type ReservedVersion struct { // Unstored are the artifacts a holder refused as not stored, with its words: said once, and not published again on // events (design 47 G §588); builder_build_again builds the key again, which reserves anew. Unstored map[string]string `json:"unstored,omitempty"` + // Dropped is who dropped the version's publish by hand (builder_publish_drop), why and when: a fate of the version, read as the + // rest of its standing (NotStanding), so no event, item or send passes it; builder_publish_again clears it (design 47 H2 H-A-746). + Dropped *PublishDropped `json:"dropped,omitempty"` // Branch and Source are the watched branch and the source revision of the catalog revision whose entry the build came from // (design 47 G §371, A-57): what `package-published` says of the publication. Branch string `json:"branch,omitempty"` @@ -93,6 +96,13 @@ type ReservedVersion struct { Withdrawn map[string]string `json:"withdrawn,omitempty"` } +// PublishDropped is a publish dropped by hand: who, why and when (design 47 H2 H-A-746). +type PublishDropped struct { + By string `json:"by"` + Why string `json:"why"` + At time.Time `json:"at"` +} + // markPublished marks artifact a published at at, the moment the builder heard or confirmed its publish (H-A-331). func (v *ReservedVersion) markPublished(a string, at time.Time) { if v.Published == nil {