From 0aed3065b5d3a72d130037a27ccda6a78907da22 Mon Sep 17 00:00:00 2001 From: jochen Date: Wed, 25 Nov 2026 17:24:41 +0100 Subject: [PATCH] distribution: G3 held-version-answered, the fingerprints state and other-bytes A version held at another digest is answered other-bytes {digest, kept fingerprint}, the call not failing, nothing stored, no transfer opened; the reader compares the fingerprint. The state fingerprints (16 MiB, key . escaped) is written before the store takes a version (a write refused at its cap refuses the publish), deleted at removed and read by versions. No fill finding. Builds: design 47 H2 H-A-589 (1) (2) (3), H-A-612, H-A-593 (2), H-A-618 (1), H-A-667, H-A-729, H-A-732, H-A-738 (1) --- .../cmd/distribution-tools/contract.go | 10 +- .../cmd/distribution-tools/contract_test.go | 25 ++- .../cmd/distribution-tools/main.go | 21 ++- .../internal/holder/holder_test.go | 151 +++++++++++++++--- .../distribution/internal/holder/transfer.go | 123 ++++++++++++-- modules/distribution/internal/holder/verbs.go | 13 ++ modules/distribution/module.json | 4 + 7 files changed, 312 insertions(+), 35 deletions(-) diff --git a/modules/distribution/cmd/distribution-tools/contract.go b/modules/distribution/cmd/distribution-tools/contract.go index 6e4e08f1b..50cc8b3af 100644 --- a/modules/distribution/cmd/distribution-tools/contract.go +++ b/modules/distribution/cmd/distribution-tools/contract.go @@ -25,6 +25,10 @@ func publishOf(in reg.PublishInput) holder.PublishRequest { } func publishAnswer(a holder.PublishAnswer) reg.PublishAnswer { + if a.OtherBytes != nil { + // No transfer is opened, so none is named (design 47 H2 H-A-732). + return reg.PublishAnswer{OtherBytes: ®.PublishAnswerOtherBytes{Digest: a.OtherBytes.Digest, Fingerprint: a.OtherBytes.Fingerprint}} + } return reg.PublishAnswer{Transfer: a.Transfer, Lacks: a.Missing, Held: a.Ended && len(a.Missing) == 0} } @@ -49,7 +53,11 @@ func versionsAnswer(v *holder.Verbs, kind string, a holder.VersionsAnswer) reg.V // The contract's answer has no field for why; the version is answered with no digest and the reason said here. logf("versions: %s %s on %s answers no digest: %v", a.Name, ver, a.Seat, err) } - out.Versions = append(out.Versions, reg.Artifact{Name: a.Name, Version: ver, Digest: d, Kind: kind}) + fp, err := v.VersionFingerprint(a.Name, ver, d) + if err != nil { + logf("versions: the fingerprint kept for %s %s on %s cannot be read: %v", a.Name, ver, a.Seat, err) + } + out.Versions = append(out.Versions, reg.Artifact{Name: a.Name, Version: ver, Digest: d, Kind: kind, Fingerprint: fp}) } return out } diff --git a/modules/distribution/cmd/distribution-tools/contract_test.go b/modules/distribution/cmd/distribution-tools/contract_test.go index 436ff9566..c8df89af5 100644 --- a/modules/distribution/cmd/distribution-tools/contract_test.go +++ b/modules/distribution/cmd/distribution-tools/contract_test.go @@ -62,18 +62,37 @@ func TestTheVerbsSpeakTheSeatsContract(t *testing.T) { t.Errorf("%s's body %s is not the contract's: %v", event, b, err) } } + // an other-bytes answer carries its digest and fingerprint and names no transfer (H-A-729, H-A-732) + if a := publishAnswer(holder.PublishAnswer{OtherBytes: &holder.OtherBytes{Digest: "sha256:bb", Fingerprint: "f1"}}); a.Transfer != "" || a.OtherBytes == nil || a.OtherBytes.Digest != "sha256:bb" || a.OtherBytes.Fingerprint != "f1" { + t.Errorf("publish answers %+v for other bytes", a) + } else if b, _ := json.Marshal(a); strings.Contains(string(b), `"transfer"`) { + t.Errorf("the other-bytes answer %s names a transfer", b) + } if a := publishAnswer(holder.PublishAnswer{Transfer: "t", Missing: []string{"sha256:bb"}}); !reflect.DeepEqual(a.Lacks, []string{"sha256:bb"}) || a.Held { t.Errorf("publish answers %+v", a) } // versions answers the digest a reference names (design 47 G A-430a): the blob on the artifact seat, the manifest on the image seat. cat := twoDigests{} for _, c := range []struct{ seat, want string }{{holder.ArtifactSeat, "sha256:blob"}, {holder.ImageSeat, "sha256:manifest"}} { - got := versionsAnswer(&holder.Verbs{Catalog: cat}, "", holder.VersionsAnswer{Seat: c.seat, Name: "m/files", Versions: []string{"0.1.7"}}) - if len(got.Versions) != 1 || got.Versions[0].Digest != c.want || got.Versions[0].Version != "0.1.7" { - t.Errorf("versions on %s answers %+v, want digest %s", c.seat, got.Versions, c.want) + // and the fingerprint kept for it beside, none where the key names another digest or there is none (H-A-589 (3)) + kept := memKept{holder.FingerprintKey("m/files", "0.1.7"): {Digest: c.want, Fingerprint: "f1"}} + got := versionsAnswer(&holder.Verbs{Holder: &holder.Holder{Fingerprints: kept}, Catalog: cat}, "", holder.VersionsAnswer{Seat: c.seat, Name: "m/files", Versions: []string{"0.1.7"}}) + if len(got.Versions) != 1 || got.Versions[0].Digest != c.want || got.Versions[0].Version != "0.1.7" || got.Versions[0].Fingerprint != "f1" { + t.Errorf("versions on %s answers %+v, want digest %s, fingerprint f1", c.seat, got.Versions, c.want) + } + kept[holder.FingerprintKey("m/files", "0.1.7")] = holder.Kept{Digest: "sha256:other", Fingerprint: "f1"} + if got := versionsAnswer(&holder.Verbs{Holder: &holder.Holder{Fingerprints: kept}, Catalog: cat}, "", holder.VersionsAnswer{Seat: c.seat, Name: "m/files", Versions: []string{"0.1.7"}}); got.Versions[0].Fingerprint != "" { + t.Errorf("versions on %s answers fingerprint %q of a key naming another digest", c.seat, got.Versions[0].Fingerprint) } } if r, err := partOf(reg.PublishPartInput{Transfer: "t", Part: "sha256:bb", Data: "aGk=", Last: true}); err != nil || string(r.Data) != "hi" || r.Digest != "sha256:bb" { t.Errorf("publish-part read %+v, %v", r, err) } } + +// memKept is the state fingerprints in memory. +type memKept map[string]holder.Kept + +func (m memKept) Get(k string) (holder.Kept, bool, error) { v, ok := m[k]; return v, ok, nil } +func (m memKept) Put(k string, v holder.Kept) error { m[k] = v; return nil } +func (m memKept) Delete(k string) error { delete(m, k); return nil } diff --git a/modules/distribution/cmd/distribution-tools/main.go b/modules/distribution/cmd/distribution-tools/main.go index a28d28791..288bda11f 100644 --- a/modules/distribution/cmd/distribution-tools/main.go +++ b/modules/distribution/cmd/distribution-tools/main.go @@ -74,6 +74,25 @@ func (k kept[T]) List() ([]T, error) { return out, nil } +// fingerprints is the state fingerprints through the runtime: a write at its cap fails (design 47 H2 H-A-589 (2)). +type fingerprints struct{} + +func (fingerprints) Get(key string) (holder.Kept, bool, error) { + var k holder.Kept + e, err := stdio.State(holder.FingerprintsState).Get(key) + if err != nil || e == nil { + return k, false, err + } + return k, true, json.Unmarshal(e.Value, &k) +} +func (fingerprints) Put(key string, k holder.Kept) error { + _, err := stdio.State(holder.FingerprintsState).Put(key, k) + return err +} +func (fingerprints) Delete(key string) error { + return stdio.State(holder.FingerprintsState).Delete(key) +} + func say(seat, event string, body holder.Event) error { return stdio.SeatPublish("mesh.seat."+seat+".event."+event, eventBody(event, body), body.ID) } @@ -497,7 +516,7 @@ func main() { base, _ := url.Parse("http://127.0.0.1:5001") store := &holder.OCI{Base: base, Upstream: &holder.Upstream{}} h := &holder.Holder{Logf: logf, Objects: boundedObjects{parts.BusObjects(holder.IncomingObjects), busstate.ObjectsOf(holder.IncomingObjects).Purge}, Builder: holder.Reserved{Ask: stdio.Ask}, Store: store, Say: say, Finding: boundedFinding(findings.Say), - Transfers: boundedTransfers{parts.BusTransfers[holder.Opening](holder.TransfersState)}, Cap: boundedCap(parts.BusCap(holder.IncomingObjects)), Record: recordAct, Idle: boundedArm(parts.BusArm(holder.IdleState))} + Transfers: boundedTransfers{parts.BusTransfers[holder.Opening](holder.TransfersState)}, Fingerprints: fingerprints{}, Cap: boundedCap(parts.BusCap(holder.IncomingObjects)), Record: recordAct, Idle: boundedArm(parts.BusArm(holder.IdleState))} files := &holder.Files{Container: or(os.Getenv("MESH_REGISTRY_CONTAINER"), "mesh-registry"), Run: holder.ExecRunner, UID: os.Getuid()} v := &holder.Verbs{Holder: h, Catalog: store, Holds: kept[holder.Hold]{state: "holds", key: holder.Hold.Key}, Mirrors: mirrors{}, Files: files} diff --git a/modules/distribution/internal/holder/holder_test.go b/modules/distribution/internal/holder/holder_test.go index ec051f903..cc1c59c78 100644 --- a/modules/distribution/internal/holder/holder_test.go +++ b/modules/distribution/internal/holder/holder_test.go @@ -215,9 +215,25 @@ func (m memHolds) List() ([]Hold, error) { return out, nil } +// memFingerprints is the state fingerprints in memory; full makes a write fail as the state does at its cap. +type memFingerprints struct { + m map[string]Kept + full bool +} + +func (f *memFingerprints) Get(k string) (Kept, bool, error) { v, ok := f.m[k]; return v, ok, nil } +func (f *memFingerprints) Put(k string, v Kept) error { + if f.full { + return errors.New("the bucket is at its cap") + } + f.m[k] = v + return nil +} +func (f *memFingerprints) Delete(k string) error { delete(f.m, k); return nil } + func newBed(builderOK bool) *bed { b := &bed{store: &fakeStore{has: map[string]bool{}}, findings: map[string]bool{}, now: time.Unix(1000, 0), cat: &fakeCatalog{tags: map[string]string{}}} - b.h = &Holder{Objects: memObjects{}, Transfers: memTransfers{}, Builder: fakeBuilder{ok: builderOK}, + b.h = &Holder{Objects: memObjects{}, Transfers: memTransfers{}, Fingerprints: &memFingerprints{m: map[string]Kept{}}, Builder: fakeBuilder{ok: builderOK}, Store: b.store, Now: func() time.Time { return b.now }, Say: func(seat, event string, body Event) error { b.said = append(b.said, said{seat, event, body}) @@ -353,16 +369,15 @@ func TestTheHolderOfTheTwoImageSeats(t *testing.T) { _, err := b.h.Publish(ArtifactSeat, builder, req("t")) return err }, "the builder did not reserve it"}, - {"a version the store holds at another digest", func(b *bed) error { - b.store.held = map[string]heldAt{"m/program@0.1.4": {manifest: "sha256:cc", layer: "sha256:bb"}} - r := req("t") - r.Digest = "sha256:aa" - _, err := b.h.Publish(ArtifactSeat, builder, r) // the reservation names no digest yet: the bed's builder says yes - if !b.findings["publish-refused m/program@0.1.4"] || len(b.store.put) != 0 { - return errors.New("not refused loudly, or stored") + {"a version stored while the state fingerprints is at its cap", func(b *bed) error { + b.h.Fingerprints.(*memFingerprints).full = true + _, _ = b.h.Publish(ArtifactSeat, builder, req("t")) + _, err := b.h.PublishPart(ArtifactSeat, builder, PartRequest{Transfer: "t", Digest: part.Digest, Last: true, Data: blob}) + if len(b.store.put) != 0 || len(b.said) != 1 || b.said[0].event != EventRefused { + return errors.New("stored, or not refused: " + fmt.Sprint(b.said)) } return err - }, "the store holds m/program@0.1.4 at digest sha256:bb, not sha256:aa"}, + }, "fingerprints did not take the key"}, {"an image on the artifact seat", func(b *bed) error { r := req("t") r.Kind = KindImage @@ -424,8 +439,9 @@ func TestTheHolderOfTheTwoImageSeats(t *testing.T) { } other := req("other") other.Digest = digestOf([]byte("other bytes")) - if _, err := b.h.Publish(ArtifactSeat, builder, other); err == nil || !strings.Contains(err.Error(), "at digest "+part.Digest+", not "+other.Digest) { - t.Errorf("other bytes at the held version = %v; want refused naming the held layer %s", err, part.Digest) + // H-A-729, H-A-732: other bytes at the held version are answered, not failed, naming the held layer, and open no transfer. + if a, err := b.h.Publish(ArtifactSeat, builder, other); err != nil || a.OtherBytes == nil || a.OtherBytes.Digest != part.Digest || a.Transfer != "" || len(b.said) != 1 { + t.Errorf("other bytes at the held version = %+v, %v; want other-bytes naming the held layer %s, no transfer", a, err, part.Digest) } if len(reg.blobs) != uploads { t.Errorf("the store took %d blobs more; want none", len(reg.blobs)-uploads) @@ -437,6 +453,73 @@ func TestTheHolderOfTheTwoImageSeats(t *testing.T) { t.Errorf("an image held at its manifest = %q, %q, %v; want held", ref, other, err) } }) + // H-A-589, H-A-729, H-A-732, H-A-738 (1): a held version offered again is answered by the fingerprint the holder keeps for it. + t.Run("a held version is answered by its kept fingerprint", func(t *testing.T) { + held := map[string]heldAt{"m/program@0.1.4": {manifest: "sha256:cc", layer: "sha256:bb"}} + offer := func(b *bed, fp string) PublishRequest { + r := req("t") + r.Digest, r.Fingerprint = "sha256:aa", fp + b.store.held = held + return r + } + keep := func(b *bed, fp string) { + _ = b.h.Fingerprints.Put(FingerprintKey("m/program", "0.1.4"), Kept{Digest: "sha256:bb", Fingerprint: fp}) + } + for _, c := range []struct { + name string + kept, offer string // the fingerprint kept (none when empty or noKey), the one offered + noKey bool + want *OtherBytes + }{ + {name: "the offered fingerprint is the kept one: other-bytes carrying it", kept: "f1", offer: "f1", want: &OtherBytes{Digest: "sha256:bb", Fingerprint: "f1"}}, + {name: "another fingerprint is other-bytes with the kept one", kept: "f1", offer: "f2", want: &OtherBytes{Digest: "sha256:bb", Fingerprint: "f1"}}, + {name: "no fingerprint kept is other-bytes with none", noKey: true, offer: "f1", want: &OtherBytes{Digest: "sha256:bb"}}, + {name: "no fingerprint offered is other-bytes", kept: "f1", want: &OtherBytes{Digest: "sha256:bb", Fingerprint: "f1"}}, + } { + b := newBed(true) + r := offer(b, c.offer) + if !c.noKey { + keep(b, c.kept) + } + a, err := b.h.Publish(ArtifactSeat, builder, r) + if err != nil || !reflect.DeepEqual(a.OtherBytes, c.want) || a.Transfer != "" || a.Ended || len(b.store.put) != 0 || len(a.Missing) != 0 || len(b.h.Transfers.(memTransfers)) != 0 || len(b.said) != 0 { + t.Errorf("%s: answered %+v, %v; stored %d, transfers %d, said %+v", c.name, a, err, len(b.store.put), len(b.h.Transfers.(memTransfers)), b.said) + } + // the ask given again reads the same state afresh and answers the same + if a2, err := b.h.Repeat(ArtifactSeat, builder, r); err != nil || !reflect.DeepEqual(a2.OtherBytes, c.want) { + t.Errorf("%s: repeated ask answered %+v, %v", c.name, a2, err) + } + } + // the key is written when the version is stored, read by versions, and deleted at removed + b := newBed(true) + r := req("k") + r.Fingerprint = "f9" + if _, err := b.h.Publish(ArtifactSeat, builder, r); err != nil { + t.Fatal(err) + } + if _, err := b.h.PublishPart(ArtifactSeat, builder, PartRequest{Transfer: "k", Digest: part.Digest, Last: true, Data: blob}); err != nil { + t.Fatal(err) + } + if k, ok, _ := b.h.Fingerprints.Get(FingerprintKey("m/program", "0.1.4")); !ok || k != (Kept{Digest: part.Digest, Fingerprint: "f9"}) { + t.Errorf("the key after the store = %+v, %v", k, ok) + } + if fp, err := b.v.VersionFingerprint("m/program", "0.1.4", part.Digest); err != nil || fp != "f9" { + t.Errorf("versions answers fingerprint %q, %v", fp, err) + } + if fp, _ := b.v.VersionFingerprint("m/program", "0.1.4", "sha256:other"); fp != "" { + t.Errorf("a key naming another digest than the held one answered %q", fp) + } + b.cat.tags = map[string]string{"m/program:0.1.4-b": part.Digest} + r2 := req("k") + r2.Version = "0.1.4-b" + _ = b.h.Fingerprints.Put(FingerprintKey("m/program", "0.1.4-b"), Kept{Digest: part.Digest, Fingerprint: "f9"}) + if _, err := b.v.Remove(ArtifactSeat, "novox.mesh-delivery-builder", RemoveRequest{Name: "m/program", Version: "0.1.4-b", Reason: "branch ended"}); err != nil { + t.Fatal(err) + } + if _, ok, _ := b.h.Fingerprints.Get(FingerprintKey("m/program", "0.1.4-b")); ok { + t.Errorf("the key stayed after removed") + } + }) t.Run("only the missing layers are sent, and an idle transfer is dropped", func(t *testing.T) { b := newBed(true) other := []byte("layer two") @@ -750,13 +833,24 @@ func TestTheHolderOfTheTwoImageSeats(t *testing.T) { b := newBed(true) return publishconformance.Bed{ Hold: func(d string) { b.store.has[d] = true }, - Publish: func(name, version, digest string) publishconformance.Outcome { - got, err := b.h.Publish(ArtifactSeat, builder, PublishRequest{Name: name, Version: version, Kind: KindBundle, Digest: digest, Transfer: "t"}) - o := publishconformance.Outcome{Ended: got.Ended, Lacks: len(got.Missing), Err: err} - if len(b.said) > 0 { - o.Event, o.Reason = b.said[len(b.said)-1].event, b.said[len(b.said)-1].body.Reason + // the holder holds the version at digest d, keeping fingerprint fp for it + HoldWith: func(d, fp string) { + b.store.held = map[string]heldAt{publishconformance.Name + "@" + publishconformance.Version: {manifest: d, layer: d}} + b.store.has[d] = true + if fp != "" { + _ = b.h.Fingerprints.Put(FingerprintKey(publishconformance.Name, publishconformance.Version), Kept{Digest: d, Fingerprint: fp}) } - return o + }, + Held: func(name, version string) (string, string) { + at := b.store.held[name+"@"+version] + fp, _ := b.v.VersionFingerprint(name, version, at.layer) + return at.layer, fp + }, + Publish: func(name, version, digest string) publishconformance.Outcome { + return publish(b, name, version, digest, "") + }, + PublishWith: func(name, version, digest, fingerprint string) publishconformance.Outcome { + return publish(b, name, version, digest, fingerprint) }} }) }) @@ -1003,9 +1097,14 @@ func TestTheHolderOfTheTwoImageSeats(t *testing.T) { if m.Class != "core" { t.Errorf("class %q, want core", m.Class) } + // The state fingerprints names its own cap, 16 MiB (H-A-589 (2)). for _, st := range m.State { - if st.MaxMiB != 8 { - t.Errorf("the state %s names max-mib %d, want 8", st.Name, st.MaxMiB) + want := 8 + if st.Name == FingerprintsState { + want = 16 + } + if st.MaxMiB != want { + t.Errorf("the state %s names max-mib %d, want %d", st.Name, st.MaxMiB, want) } } // The reservation is asked through the seat mesh-builds, so the manifest takes no package of the builder (design 47 §381). @@ -1116,3 +1215,17 @@ func TestTheHolderOfTheTwoImageSeats(t *testing.T) { // registryForms reads a reference of the registry form, with the two seats' names this holder answers for. var registryForms = artifactref.Seats{Image: ImageSeat, Object: ArtifactSeat} + +// publish is a no-parts publish of a bundle through the holder as the publish conformance kit makes it, with the outcome the +// kit reads. +func publish(b *bed, name, version, digest, fingerprint string) publishconformance.Outcome { + got, err := b.h.Publish(ArtifactSeat, builder, PublishRequest{Name: name, Version: version, Kind: KindBundle, Digest: digest, Fingerprint: fingerprint, Transfer: "t"}) + o := publishconformance.Outcome{Ended: got.Ended, Lacks: len(got.Missing), Err: err} + if got.OtherBytes != nil { + o.Other = &publishconformance.OtherBytes{Digest: got.OtherBytes.Digest, Fingerprint: got.OtherBytes.Fingerprint} + } + if len(b.said) > 0 { + o.Event, o.Reason = b.said[len(b.said)-1].event, b.said[len(b.said)-1].body.Reason + } + return o +} diff --git a/modules/distribution/internal/holder/transfer.go b/modules/distribution/internal/holder/transfer.go index 4114ae5e3..2b95eafa4 100644 --- a/modules/distribution/internal/holder/transfer.go +++ b/modules/distribution/internal/holder/transfer.go @@ -11,6 +11,7 @@ import ( "git.novox.be/novox/mesh-contracts/health-and-repair/go/clients/meshhandactsclient" "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/parts" + keyparts "git.novox.be/novox/mesh-sdk/go/keys" ) // The bounds of incoming (design 51 *Publishing*, ADR 0611): a part is at most 1 MiB, a transfer with no part for 24 @@ -25,6 +26,9 @@ const ( // IncomingObjects and TransfersState are the object store and the state distribution asks as bus state. IncomingObjects = "incoming" TransfersState = "transfers" + // FingerprintsState is the state fingerprints: the digest and fingerprint each version this holder stored was published + // with (design 47 H2 H-A-589 (2)). + FingerprintsState = "fingerprints" // IdleState is the state of one key per open transfer with its own time to live (a state declared key-ttl): the // bus's marker of its end is the event that drops a transfer left with no part for TransferIdle (ADR 0611 §3), never // a timer. @@ -68,6 +72,36 @@ type PublishAnswer struct { Missing []string `json:"missing"` // Ended says the publish ended at once: everything was held, `published` or `refused` is said. Ended bool `json:"ended,omitempty"` + // OtherBytes is set when the holder holds the publish's name and version with another digest and keeps another + // fingerprint for it, or none: the call does not fail, and no transfer is opened (design 47 H2 H-A-729, H-A-732). + OtherBytes *OtherBytes `json:"other_bytes,omitempty"` +} + +// OtherBytes is the digest a holder holds for a name and version, and the fingerprint it keeps for them, empty where it +// keeps none. +type OtherBytes struct { + Digest string `json:"digest"` + Fingerprint string `json:"fingerprint,omitempty"` +} + +// Kept is one key of the state fingerprints: the digest and the fingerprint the publish of a version named. +type Kept struct { + Digest string `json:"digest"` + Fingerprint string `json:"fingerprint,omitempty"` +} + +// FingerprintKey is the key of name at version in the state fingerprints: `.`, each token escaped as a key's +// name is (design 47 H2 H-A-589 (2)). +func FingerprintKey(name, version string) string { + return keyparts.EscapeName(name) + "." + keyparts.EscapeName(version) +} + +// FingerprintState is distribution's own state fingerprints (bus state, template state; ADR 0595), reached by the holder +// alone; a write at its cap fails. +type FingerprintState interface { + Get(key string) (Kept, bool, error) + Put(key string, k Kept) error + Delete(key string) error } // PartRequest is the body of `publish-part` (ADR 0611 §2): one chunk of one part. @@ -165,11 +199,13 @@ type Finding func(kind, subject, words string, raised bool) type Holder struct { Objects Objects Transfers TransferState - Builder Builder - Store Store - Say Say - Finding Finding - Now func() time.Time + // Fingerprints keeps the fingerprint of each version the holder stored (H-A-589 (2)). + Fingerprints FingerprintState + Builder Builder + Store Store + Say Say + Finding Finding + Now func() time.Time // Cap reads incoming's cap from its binding (parts.BusCap); IncomingCap when nil or when the binding names none. Cap func() (uint64, error) // Record records a hand act through the seat mesh-hand-acts (meshhandactsclient.Recorder); a drop by hand is refused @@ -229,10 +265,15 @@ func (h *Holder) receiver() *parts.Receiver[Opening] { return nil }, Store: func(t Transfer, held map[string][]byte) error { + if err := h.keep(t.Shape.PublishRequest); err != nil { + reason := err.Error() + return errors.Join(errors.New("refused: "+reason), h.refused(t, reason)) + } path, err := h.Store.Put(t.Shape.PublishRequest, held) ref := SeatReference(t.Shape.Seat, path) if err != nil { reason := "the store did not take it: " + err.Error() + h.unkeep(t.Shape.PublishRequest) return errors.Join(errors.New("refused: "+reason), h.refused(t, reason)) } h.Finding("publish-refused", t.Shape.Name+"@"+t.Shape.Version, "", false) @@ -276,10 +317,9 @@ func (h *Holder) Publish(seat, caller string, r PublishRequest) (PublishAnswer, if err != nil { return PublishAnswer{}, fmt.Errorf("publish: the store cannot say whether it holds %s@%s: %w", r.Name, r.Version, err) } else if other != "" { - // A second publish of a held version with another digest is refused before the reservation is asked, as the forge - // holds the first (A-490j, H-A-393): while the reserved digest is still empty it would pass, and the store's Put - // would move the version's tag. - return h.refusePublish(seat, r, fmt.Sprintf("the store holds %s@%s at digest %s, not %s", r.Name, r.Version, other, r.Digest)) + // The version is held at another digest: answered with the held digest and the fingerprint kept for it, nothing + // stored, no transfer opened (H-A-589 (1), H-A-729, H-A-732, H-A-738 (1)). + return h.heldElsewhere(r, other) } else if ref != "" { 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, ref), "")) @@ -304,6 +344,60 @@ func (h *Holder) Publish(seat, caller string, r PublishRequest) (PublishAnswer, return answerOf(got), nil } +// keptFor is the fingerprint the holder keeps for name at version, empty when it keeps none; a key naming another digest +// than the one the store holds speaks for nothing. +func (h *Holder) keptFor(r PublishRequest, held string) (string, error) { + k, found, err := h.Fingerprints.Get(FingerprintKey(r.Name, r.Version)) + if err != nil { + return "", fmt.Errorf("publish: the fingerprint kept for %s@%s cannot be read: %w", r.Name, r.Version, err) + } + if !found || k.Digest != held { + return "", nil + } + return k.Fingerprint, nil +} + +// heldElsewhere answers a publish of a version the store holds at the digest other, not the publish's: other-bytes, the held +// digest and the fingerprint kept for it, the call not failing, nothing stored and no transfer opened. The reader compares the +// kept fingerprint with the publish's own (H-A-589 (1), H-A-729, H-A-732, H-A-738 (1)). The journal says both digests and both +// fingerprints. +func (h *Holder) heldElsewhere(r PublishRequest, other string) (PublishAnswer, error) { + kept, err := h.keptFor(r, other) + if err != nil { + return PublishAnswer{}, err + } + h.logf("publish: %s@%s is held at digest %s with fingerprint %q; the offer is digest %s with fingerprint %q: answered other-bytes, nothing stored", r.Name, r.Version, other, kept, r.Digest, r.Fingerprint) + return PublishAnswer{OtherBytes: &OtherBytes{Digest: other, Fingerprint: kept}}, nil +} + +// keep writes the key of a version about to be stored; a write the state refuses (at its cap) is the publish's refusal, so +// nothing is stored. A publish with no version keeps none. +func (h *Holder) keep(r PublishRequest) error { + if r.Version == "" { + return nil + } + if err := h.Fingerprints.Put(FingerprintKey(r.Name, r.Version), Kept{Digest: r.Digest, Fingerprint: r.Fingerprint}); err != nil { + return fmt.Errorf("the state %s did not take the key of %s@%s (it refuses at its cap), so nothing is stored: %w", FingerprintsState, r.Name, r.Version, err) + } + return nil +} + +// unkeep deletes the key keep wrote when the store did not take the version. +func (h *Holder) unkeep(r PublishRequest) { + if r.Version == "" { + return + } + if err := h.Fingerprints.Delete(FingerprintKey(r.Name, r.Version)); err != nil { + h.logf("publish: the key of %s@%s stays in the state %s: %v", r.Name, r.Version, FingerprintsState, err) + } +} + +func (h *Holder) logf(format string, a ...any) { + if h.Logf != nil { + h.Logf(format, a...) + } +} + // refusePublish ends a publish refused before anything is stored: its finding raised, refused said on the seat, the reason // answered. func (h *Holder) refusePublish(seat string, r PublishRequest, reason string) (PublishAnswer, error) { @@ -338,6 +432,9 @@ func (h *Holder) tagHeld(seat string, r PublishRequest) (PublishAnswer, error) { if !ok { return refuse("the builder did not reserve it: " + why) } + if err := h.keep(r); err != nil { + return refuse(err.Error()) + } tag := r tag.Parts = []Part{{Digest: r.Digest}} var path string @@ -345,6 +442,7 @@ func (h *Holder) tagHeld(seat string, r PublishRequest) (PublishAnswer, error) { path, err = h.Store.Put(tag, map[string][]byte{}) return err }); err != nil { + h.unkeep(r) return refuse("the store did not tag it: " + err.Error()) } h.Finding("publish-refused", r.Name+"@"+r.Version, "", false) @@ -416,15 +514,18 @@ func (h *Holder) Repeat(seat, caller string, r PublishRequest) (PublishAnswer, e return PublishAnswer{}, err } if r.Version != "" { - var ref string + var ref, other string err := h.timed("the store's read of what it holds (the program's registry)", func() (err error) { - ref, _, err = h.Store.Held(r) + ref, other, err = h.Store.Held(r) return err }) if err != nil { return PublishAnswer{}, fmt.Errorf("publish: the store cannot say whether it holds %s@%s: %w", r.Name, r.Version, err) } else if ref != "" { return PublishAnswer{Transfer: r.Transfer, Ended: true}, nil + } else if other != "" { + // The act's state as it stands: held at another digest, answered as the act answered it, saying nothing again. + return h.heldElsewhere(r, other) } } t, found, err := h.Transfers.Get(r.Transfer) diff --git a/modules/distribution/internal/holder/verbs.go b/modules/distribution/internal/holder/verbs.go index 0edb6caa4..cf206e917 100644 --- a/modules/distribution/internal/holder/verbs.go +++ b/modules/distribution/internal/holder/verbs.go @@ -201,6 +201,9 @@ func (v *Verbs) Remove(seat, caller string, r RemoveRequest) (Removal, error) { } } ev.Digest = d // a version already gone is said removed all the same, so the builder's retry ends + if err := v.Fingerprints.Delete(FingerprintKey(r.Name, r.Version)); err != nil { + return Removal{}, fmt.Errorf("remove: %s@%s is removed, but its key in the state %s is not deleted: %w", r.Name, r.Version, FingerprintsState, err) + } } return Removal{Removed: true}, v.Say(seat, EventRemoved, ev) } @@ -450,3 +453,13 @@ func (v *Verbs) HeldNow() ([]Hold, error) { slices.SortFunc(all, func(a, b Hold) int { return strings.Compare(a.Key(), b.Key()) }) return all, nil } + +// VersionFingerprint is the fingerprint kept for name at version when the key names the digest the store holds there, empty +// where none is kept (`versions`, design 47 H2 H-A-589 (3)). +func (v *Verbs) VersionFingerprint(name, version, digest string) (string, error) { + k, found, err := v.Fingerprints.Get(FingerprintKey(name, version)) + if err != nil || !found || k.Digest != digest { + return "", err + } + return k.Fingerprint, nil +} diff --git a/modules/distribution/module.json b/modules/distribution/module.json index 54d97d94d..5ec49763a 100644 --- a/modules/distribution/module.json +++ b/modules/distribution/module.json @@ -255,6 +255,10 @@ "name": "transfers", "max-mib": 8 }, + { + "name": "fingerprints", + "max-mib": 16 + }, { "name": "idle", "key-ttl": true,