diff --git a/modules/distribution/internal/holder/holder_test.go b/modules/distribution/internal/holder/holder_test.go index 828bd3797..08d6aee84 100644 --- a/modules/distribution/internal/holder/holder_test.go +++ b/modules/distribution/internal/holder/holder_test.go @@ -71,6 +71,7 @@ func (s *fakeStore) Put(t PublishRequest, parts map[string][]byte) (string, erro return t.Name + "@" + t.Digest, nil } func (s *fakeStore) Held(string, string, string) (string, bool, error) { return "", false, nil } +func (s *fakeStore) Holds(r PublishRequest) (bool, error) { return s.has[r.Digest], nil } type said struct { seat, event string @@ -665,30 +666,37 @@ func TestTheHolderOfTheTwoImageSeats(t *testing.T) { } } }) - t.Run("a late publish names a held bundle with no size: tagged at the store's size, nothing sent", func(t *testing.T) { + t.Run("a publish with a version and no parts tags the held digest at the store's size; one not held is not stored", func(t *testing.T) { + // Design 47 G A-417, A-73, A-50: the build stored the bundle untagged; the builder's publish of its version with that + // digest and no parts tags it and says published, nothing sent; a digest the store holds none of is refused as not + // stored, naming the digest. store := newFakeRegistry() sts := httptest.NewServer(store) defer sts.Close() base, _ := url.Parse(sts.URL) - o := &OCI{Base: base} + b := newBed(true) + b.h.Store = &OCI{Base: base} blob := []byte(strings.Repeat("b", 4321)) store.blobs["m/tools@"+digestOf(blob)] = blob - ref, err := o.Put(PublishRequest{Name: "m/tools", Version: "0.1.4", Kind: KindBundle, Digest: digestOf(blob), - Parts: []Part{{Digest: digestOf(blob)}}}, map[string][]byte{}) - if err != nil || ref != "m/tools@"+digestOf(blob) { - t.Fatalf("Put = %q, %v", ref, err) + got, err := b.h.Publish(ArtifactSeat, "x.mesh-delivery-builder", PublishRequest{Name: "m/tools", Version: "0.1.4", Kind: KindBundle, Digest: digestOf(blob), + Transfer: "republish-m-0-1-4-tools"}) + if err != nil || !got.Ended || len(b.said) != 1 || b.said[0].event != EventPublished || b.said[0].body.Reference != SeatReference(ArtifactSeat, "m/tools@"+digestOf(blob)) { + t.Fatalf("Publish = %+v, %v; said %+v", got, err, b.said) } var man struct { Layers []struct { - Size int64 `json:"size"` + Digest string `json:"digest"` + Size int64 `json:"size"` } `json:"layers"` } - if err := json.Unmarshal(store.manifests["m/tools:0.1.4"], &man); err != nil || len(man.Layers) != 1 || man.Layers[0].Size != int64(len(blob)) { + if err := json.Unmarshal(store.manifests["m/tools:0.1.4"], &man); err != nil || len(man.Layers) != 1 || man.Layers[0].Size != int64(len(blob)) || man.Layers[0].Digest != digestOf(blob) { t.Errorf("the version's manifest %s, want one layer of %d bytes", store.manifests["m/tools:0.1.4"], len(blob)) } - if _, err := o.Put(PublishRequest{Name: "m/tools", Version: "0.1.5", Kind: KindBundle, Digest: "sha256:" + strings.Repeat("e", 64), - Parts: []Part{{Digest: "sha256:" + strings.Repeat("e", 64)}}}, map[string][]byte{}); err == nil { - t.Error("a digest the store does not hold was tagged") + gone := "sha256:" + strings.Repeat("e", 64) + _, err = b.h.Publish(ArtifactSeat, "x.mesh-delivery-builder", PublishRequest{Name: "m/tools", Version: "0.1.5", Kind: KindBundle, Digest: gone, Transfer: "republish-m-0-1-5-tools"}) + if err == nil || !strings.Contains(err.Error(), "not stored: ") || !strings.Contains(err.Error(), gone) || store.manifests["m/tools:0.1.5"] != nil || + len(b.said) != 2 || b.said[1].event != EventRefused || !b.findings["publish-refused m/tools@0.1.5"] { + t.Errorf("a digest not held: %v; said %+v; findings %v", err, b.said, b.findings) } }) t.Run("mirror answers at once and says the copy's end on the seat's events", func(t *testing.T) { diff --git a/modules/distribution/internal/holder/oci.go b/modules/distribution/internal/holder/oci.go index 7b41d9feb..65abad32f 100644 --- a/modules/distribution/internal/holder/oci.go +++ b/modules/distribution/internal/holder/oci.go @@ -219,6 +219,25 @@ func (o *OCI) Held(name, version, digest string) (string, bool, error) { return name + "@" + d, true, nil } +// Holds is whether the store holds what r's digest names under r's name: an image's manifest by its digest, or a bundle's +// or an archive's blob. +func (o *OCI) Holds(r PublishRequest) (bool, error) { + if r.Kind != KindImage && r.Kind != KindUpstream { + return o.Has(r.Name, r.Digest) + } + resp, _, err := o.do(http.MethodHead, "/v2/"+r.Name+"/manifests/"+r.Digest, "", nil, accepted...) + if err != nil { + return false, err + } + switch resp.StatusCode { + case http.StatusOK: + return true, nil + case http.StatusNotFound: + return false, nil + } + return false, fmt.Errorf("the store answered %d for the manifest %s of %s", resp.StatusCode, r.Digest, r.Name) +} + // Versions are name's tags. func (o *OCI) Versions(name string) ([]string, error) { resp, b, err := o.do(http.MethodGet, "/v2/"+name+"/tags/list", "", nil) diff --git a/modules/distribution/internal/holder/transfer.go b/modules/distribution/internal/holder/transfer.go index 857ec5841..7b3bcba67 100644 --- a/modules/distribution/internal/holder/transfer.go +++ b/modules/distribution/internal/holder/transfer.go @@ -143,6 +143,9 @@ type Store interface { // Held is the reference's path of name at version when the store already holds digest under it: a rebuild of a held // fingerprint answers it and stores nothing. Held(name, version, digest string) (reference string, ok bool, err error) + // Holds says whether the store holds what a publish's digest names under its name: an image's manifest, or a bundle's or + // an archive's blob. The store is the artifact store (A-50): `artifact-store:///blobs/` is that blob. + Holds(r PublishRequest) (bool, error) } // SeatReference is a reference in the form the seat answers (novox/hq ADR 0605 ยง9): the seat's name, then the path the @@ -277,6 +280,9 @@ func (h *Holder) Publish(seat, caller string, r PublishRequest) (PublishAnswer, }) } } + if r.Version != "" && len(r.Parts) == 0 { + return h.tagHeld(seat, r) + } wants := make([]parts.Want, 0, len(r.Parts)) for _, p := range r.Parts { wants = append(wants, parts.Want{Digest: p.Digest, Size: p.Size}) @@ -292,6 +298,51 @@ func (h *Holder) Publish(seat, caller string, r PublishRequest) (PublishAnswer, return answerOf(got), nil } +// tagHeld is a publish with a version, a digest and no parts (design 47 G A-417, A-73, A-50): what the store holds of that +// digest under the name (the build's untagged publish, the artifact store's blob) is checked against the builder's reservation +// and tagged with the version, and published is said; when the store holds none of it, the publish is refused as not stored, +// naming the digest. Nothing travels. +func (h *Holder) tagHeld(seat string, r PublishRequest) (PublishAnswer, error) { + var held bool + err := h.timed("the store's read of the digest it holds (the program's registry)", func() (err error) { + held, err = h.Store.Holds(r) + return err + }) + if err != nil { + return PublishAnswer{}, fmt.Errorf("publish: the store cannot say whether it holds %s of %s: %w", r.Digest, r.Name, err) + } + refuse := func(reason string) (PublishAnswer, error) { + h.Finding("publish-refused", r.Name+"@"+r.Version, reason, true) + if err := h.Say(seat, EventRefused, h.eventOf(seat, r, "", reason)); err != nil { + return PublishAnswer{}, fmt.Errorf("publish: %s, and the refused is not said: %w", reason, err) + } + return PublishAnswer{Transfer: r.Transfer, Ended: true}, errors.New("publish: " + reason) + } + if !held { + return refuse(fmt.Sprintf("not stored: the store holds no %s of %s, so version %s names nothing", r.Digest, r.Name, r.Version)) + } + ok, why, err := h.Builder.Recorded(r.Name, r.Version, r.Digest) + if err != nil { + return PublishAnswer{}, fmt.Errorf("publish: the reservation could not be read, so nothing is tagged: %w", err) + } + if !ok { + return refuse("the builder did not reserve it: " + why) + } + tag := r + tag.Parts = []Part{{Digest: r.Digest}} + var path string + if err := h.timed("the store's tag of the held digest (the program's registry)", func() (err error) { + path, err = h.Store.Put(tag, map[string][]byte{}) + return err + }); err != nil { + return refuse("the store did not tag it: " + err.Error()) + } + h.Finding("publish-refused", r.Name+"@"+r.Version, "", false) + return PublishAnswer{Transfer: r.Transfer, Ended: true}, h.timed("the published event on the seat", func() error { + return h.Say(seat, EventPublished, h.eventOf(seat, r, SeatReference(seat, path), "")) + }) +} + // PublishPart is `publish-part`: one chunk, from the caller that opened the transfer alone. func (h *Holder) PublishPart(seat, caller string, r PartRequest) (PublishAnswer, error) { got, err := h.receiver().Take(r.Transfer, caller, r.Digest, r.Sequence, r.Last, r.Data, func(t Transfer) error {