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 ba8fbea96..f9d90dc89 100644 --- a/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/publishitem.go +++ b/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/publishitem.go @@ -421,8 +421,9 @@ func registryVersionsVia(call toolCaller) func(context.Context, string, string) } var a struct { // the four registry seats answer versions alike: each version with its digest Versions []struct { - Version string `json:"version"` - Digest string `json:"digest"` + Version string `json:"version"` + Digest string `json:"digest"` + Fingerprint string `json:"fingerprint"` } `json:"versions"` } if err := json.Unmarshal(raw, &a); err != nil { @@ -430,7 +431,7 @@ func registryVersionsVia(call toolCaller) func(context.Context, string, string) } out := make([]versions.HeldVersion, 0, len(a.Versions)) for _, v := range a.Versions { - out = append(out, versions.HeldVersion{Version: v.Version, Digest: v.Digest}) + out = append(out, versions.HeldVersion{Version: v.Version, Digest: v.Digest, Fingerprint: v.Fingerprint}) } return out, nil } diff --git a/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/reserve.go b/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/reserve.go index 16d5eff87..813e426a4 100644 --- a/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/reserve.go +++ b/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/reserve.go @@ -573,6 +573,9 @@ func (rv *reservations) ReleasedFor(ctx context.Context, module, fingerprint str // asked for ever. The version's release follows from the catalog as for any published. func (rv *reservations) markHeld(ctx context.Context, v versions.ReservedVersion, held []versions.HeldArtifact) { for _, h := range held { + if v.Published[h.Ref] { + continue // marked already: its package-published was said (design 47 H2 H-A-588 (4): once) + } rv.logf("%s %s: %s holds %s %s with the built digest %s: marked published from its record", v.Module, v.Version, h.Seat, h.Name, h.Version, h.Digest) rv.published(ctx, versions.Outcome{Name: h.Name, Version: h.Version, Digest: h.Digest, Holder: h.Seat}, nil) } diff --git a/modules/mesh-delivery-builder/internal/versions/republish.go b/modules/mesh-delivery-builder/internal/versions/republish.go index d2ab90a8f..c78549e4a 100644 --- a/modules/mesh-delivery-builder/internal/versions/republish.go +++ b/modules/mesh-delivery-builder/internal/versions/republish.go @@ -846,6 +846,8 @@ func (r *Reserver) MarkUnstored(ctx context.Context, module, version, artifact, // HeldVersion is one version a registry seat's verb versions answers of a name, with its digest. type HeldVersion struct { Version, Digest string + // Fingerprint is the fingerprint the holder keeps for the version (design 47 H2 H-A-589 (3)); empty where none is kept. + Fingerprint string } // HeldArtifact is an artifact of a reservation as a registry seat's record holds it at the reservation's version: its reference, @@ -854,32 +856,51 @@ type HeldArtifact struct { Ref, Seat, Name, Version, Digest string } -// Judged is what the registries' record says of a reserved version's missing artifacts (design 47 H H-A-272, H-A-274, H-A-275): -// those held at the version with the built digest (Held), those held at the version with another digest (Elsewhere), and, when an -// artifact is held at neither, the first one lacking (Lacks). All says every missing artifact is held with its built digest. +// Judged is what the registries' record says of a reserved version's artifacts (design 47 H H-A-272, H-A-274, H-A-275; H2 H-A-588 +// (4), H-A-616, H-A-663): those held at the version for the reservation's fingerprint (Held, with the digest the registry holds), +// those held at the version for another fingerprint or with another digest (Elsewhere), and, when an artifact is held at neither, +// the first one lacking (Lacks). All says every artifact is held for the fingerprint. type Judged struct { Held, Elsewhere []HeldArtifact Lacks string All bool } -// Held judges v's missing artifacts against the registries' record (design 47 H H-A-269 (2), H-A-272, H-A-274, H-A-275): each -// artifact's registry seat and name as a publish again sends them, its record read through the seat's verb versions (versionsOf). -// A version with no missing artifact is All. +// heldFor says whether the registry's record h of an artifact at v's version is held for v's fingerprint (design 47 H2 H-A-616, +// H-A-663): a holder that keeps a fingerprint says it, and it is the reservation's or another's; one that keeps none holds it for +// the reservation's fingerprint when its digest is the one the reservation records for the artifact (a kept published is such a +// record), and elsewhere when the reservation records another digest or none. +func heldFor(v ReservedVersion, ref string, h HeldVersion) bool { + if h.Fingerprint != "" { + return h.Fingerprint == v.Fingerprint + } + return v.Artifacts[ref] != "" && h.Digest == v.Artifacts[ref] +} + +// Held judges every artifact of v against the registries' record (design 47 H H-A-269 (2), H-A-272, H-A-274, H-A-275; H2 H-A-588 +// (4), H-A-663: an artifact already marked published counts as held when its registry still answers it): each artifact's registry +// seat and name as a publish again sends them, its record read through the seat's verb versions (versionsOf). An upstream artifact +// is never read. A version with no missing artifact is All. func (r *Republisher) Held(ctx context.Context, v ReservedVersion, versionsOf func(ctx context.Context, seat, name string) ([]HeldVersion, error)) (Judged, error) { - missing := v.Missing() - if len(missing) == 0 { + if len(v.Missing()) == 0 { return Judged{All: true}, nil } e, err := r.Entry(ctx, v.Fingerprint) if err != nil { return Judged{}, fmt.Errorf("the catalog entry %s of %s %s cannot be read: %w", v.Fingerprint, v.Module, v.Version, err) } + refs := make([]string, 0, len(v.Artifacts)) + for ref := range v.Artifacts { + if !slices.Contains(v.Upstream, ref) { + refs = append(refs, ref) + } + } + sort.Strings(refs) var out Judged - for _, ref := range missing { + for _, ref := range refs { seat, name, _, ok := publishTarget(e, v.Module, ref) if !ok { - if out.Lacks == "" { + if out.Lacks == "" && !v.Published[ref] { out.Lacks = ref + ": the catalog entry names no registry seat for it" } continue @@ -888,13 +909,13 @@ func (r *Republisher) Held(ctx context.Context, v ReservedVersion, versionsOf fu if err != nil { return Judged{}, fmt.Errorf("%s's versions of %s cannot be read: %w", seat, name, err) } - at, found := HeldArtifact{}, false + at, found, forF := HeldArtifact{}, false, false for _, h := range held { if strings.TrimPrefix(h.Version, "v") != v.Version { continue } - at, found = HeldArtifact{Ref: ref, Seat: seat, Name: name, Version: h.Version, Digest: h.Digest}, true - if h.Digest == v.Artifacts[ref] { + at, found, forF = HeldArtifact{Ref: ref, Seat: seat, Name: name, Version: h.Version, Digest: h.Digest}, true, heldFor(v, ref, h) + if forF { break } } @@ -903,13 +924,13 @@ func (r *Republisher) Held(ctx context.Context, v ReservedVersion, versionsOf fu if out.Lacks == "" { out.Lacks = fmt.Sprintf("%s holds no %s %s", seat, name, v.Version) } - case at.Digest == v.Artifacts[ref]: + case forF: out.Held = append(out.Held, at) default: out.Elsewhere = append(out.Elsewhere, at) } } - out.All = len(out.Held) == len(missing) + out.All = out.Lacks == "" && len(out.Elsewhere) == 0 && len(out.Held) > 0 return out, nil } diff --git a/modules/mesh-delivery-builder/internal/versions/republish_test.go b/modules/mesh-delivery-builder/internal/versions/republish_test.go index 0f78961f4..b58db0148 100644 --- a/modules/mesh-delivery-builder/internal/versions/republish_test.go +++ b/modules/mesh-delivery-builder/internal/versions/republish_test.go @@ -262,6 +262,11 @@ func TestTheRegistriesRecordSaysWhetherAVersionIsHeld(t *testing.T) { {name: "held in the Go form with the built digest", holds: []HeldVersion{{Version: "v0.1.36", Digest: "sha256:z"}, {Version: "v0.1.37", Digest: "sha256:a"}}, all: true}, {name: "another digest at the version", holds: []HeldVersion{{Version: "0.1.37", Digest: "sha256:b"}}, elsewhere: true}, {name: "the version lacked", holds: []HeldVersion{{Version: "0.1.36", Digest: "sha256:a"}}, lacks: true}, + // H2 H-A-616: no fingerprint kept: held for F when the digest is the reserved one, elsewhere with another + {name: "no fingerprint kept, the reserved digest", holds: []HeldVersion{{Version: "0.1.37", Digest: "sha256:a"}}, all: true}, + // H2 H-A-589 (3): a holder that keeps a fingerprint says whose it is, whatever the digest + {name: "the fingerprint kept is F's", holds: []HeldVersion{{Version: "0.1.37", Digest: "sha256:b", Fingerprint: "fp"}}, all: true}, + {name: "the fingerprint kept is another's", holds: []HeldVersion{{Version: "0.1.37", Digest: "sha256:a", Fingerprint: "other"}}, elsewhere: true}, } { t.Run(c.name, func(t *testing.T) { var seat, name string @@ -283,6 +288,20 @@ func TestTheRegistriesRecordSaysWhetherAVersionIsHeld(t *testing.T) { if j, err := r.Held(ctx, v, nil); err != nil || !j.All { t.Errorf("a version with no missing artifact is judged %+v (%v), want all held without a read", j, err) } + // H2 H-A-663: a kept published counts as held while its registry answers it at the version, and no longer when it does not + v.Published, v.Artifacts = map[string]bool{"m:lib": true, "m:other": true}, map[string]string{"m:lib": "sha256:a", "m:other": "sha256:o"} + v.Upstream = nil + entry.Artifacts = append(entry.Artifacts, meshcatalog.Artifact{Ref: "m:other", Kind: "package", Language: "go", Package: &meshcatalog.ArtifactPackage{Ecosystem: "go", Name: "git.example/o"}}) + v.Published["m:other"] = false + both := func(_ context.Context, _, n string) ([]HeldVersion, error) { + if n == "git.example/o" { + return nil, nil + } + return []HeldVersion{{Version: "0.1.37", Digest: "sha256:a"}}, nil + } + if j, err := r.Held(ctx, v, both); err != nil || j.All || len(j.Held) != 1 || j.Held[0].Ref != "m:lib" || !strings.Contains(j.Lacks, "git.example/o") { + t.Errorf("a kept published its registry answers is judged %+v (%v), want it held and the other lacking", j, err) + } } // queueAt writes a queued version of module m at holder oci directly, as a start finds it. diff --git a/modules/mesh-delivery-builder/internal/versions/reserve.go b/modules/mesh-delivery-builder/internal/versions/reserve.go index a6a3a55e9..377f2273c 100644 --- a/modules/mesh-delivery-builder/internal/versions/reserve.go +++ b/modules/mesh-delivery-builder/internal/versions/reserve.go @@ -416,6 +416,50 @@ func allSet(m map[string]string) bool { return true } +// MarkHeld writes on module's reservation of version each artifact a registry holds at the version for the reservation's +// fingerprint, with the held digest, and marks it published when it was not (design 47 H2 H-A-588 (4)), in one compare-and-set. +// It answers the artifacts it newly marked, and false when the reservation stands at another version. +func (r *Reserver) MarkHeld(ctx context.Context, module, version string, held []HeldArtifact) ([]HeldArtifact, bool, error) { + series, _, err := SplitVersion(version) + if err != nil { + return nil, false, err + } + key := ReservedKey(module, series) + for { + cur, rev, found, err := r.get(ctx, key) + if err != nil || !found || cur.Version != version { + return nil, false, err + } + was := cur + was.Artifacts, was.Published, was.PublishedAt = maps.Clone(cur.Artifacts), maps.Clone(cur.Published), maps.Clone(cur.PublishedAt) + var marked []HeldArtifact + changed := false + if cur.Artifacts == nil { + cur.Artifacts = map[string]string{} + } + for _, h := range held { + if cur.Artifacts[h.Ref] != h.Digest { + cur.Artifacts[h.Ref], changed = h.Digest, true + } + if !cur.Published[h.Ref] { + cur.markPublished(h.Ref, r.now()) + marked, changed = append(marked, h), true + } + } + if !changed { + return nil, true, nil + } + raw, _ := json.Marshal(cur) + if _, err := r.Store.Update(ctx, key, raw, rev); errors.Is(err, ErrConflict) { + continue + } else if err != nil { + return nil, false, fmt.Errorf("%s cannot be written: %w", key, err) + } + r.said(r.reindex(ctx, key, was, cur)) + return marked, true, nil + } +} + // DefaultReleasedKept is the setting released-kept unless set (design 47 G A-456b). const DefaultReleasedKept = 16 diff --git a/modules/mesh-delivery-builder/internal/versions/reserve_test.go b/modules/mesh-delivery-builder/internal/versions/reserve_test.go index 46616f5ed..cbb3538ee 100644 --- a/modules/mesh-delivery-builder/internal/versions/reserve_test.go +++ b/modules/mesh-delivery-builder/internal/versions/reserve_test.go @@ -174,6 +174,19 @@ func TestReservationIsReadAndMarkedPublished(t *testing.T) { if key, ok := ReservedBoundOf(ReservedBoundKey("m", "1.4")); !ok || key != ReservedKey("m", "1.4") || ReservedBoundFor(key) != ReservedBoundKey("m", "1.4") { t.Errorf("the bound key does not name the reservation back: %q %v", key, ok) } + // Design 47 H2 H-A-588 (4): MarkHeld writes the digest a registry holds on the reservation and marks the artifact published, once. + held := []HeldArtifact{{Ref: "m:b", Digest: "sha256:held"}} + markedHeld, ok, err := r.MarkHeld(ctx, "m", "1.4.3", held) + cur, _, _ = r.At(ctx, ReservedKey("m", "1.4")) + if err != nil || !ok || len(markedHeld) != 1 || cur.Artifacts["m:b"] != "sha256:held" || !cur.Published["m:b"] || cur.Artifacts["m:a"] != "sha256:aa" || cur.PublishedAt["m:b"].IsZero() { + t.Fatalf("marked %v (%v, %v), reservation %+v: want m:b with the held digest, published, m:a as it was", markedHeld, ok, err, cur) + } + if again, ok, err := r.MarkHeld(ctx, "m", "1.4.3", held); err != nil || !ok || len(again) != 0 { + t.Errorf("a second marking marked %v (%v, %v), want none: package-published is said once", again, ok, err) + } + if _, ok, _ := r.MarkHeld(ctx, "m", "1.4.99", held); ok { + t.Error("a reservation at another version was marked") + } } // TestAReleaseIsRecordedByFingerprintBeforeItsReservationGoes: design 47 G A-456a — the version-released of a version records