Files
mesh-delivery/modules/mesh-delivery-sdk/parts/receive.go
T
jschoubben dc6dfef6f0 A reopen drops without a store only when the holder holds the file (H-A-481 second addendum)
The drop without a second store judged from the parts the holder holds,
and distribution's Holds answers per layer: a publish cut before its
store, whose layers were held, was dropped and never said as published.
The drop now asks the holder's own HoldsWhole (the file at its name and
version); nil never drops, and the reopen finishes as usual.

Builds: design 47 H2 H-A-481 second addendum
2026-11-22 19:25:40 +01:00

560 lines
20 KiB
Go

package parts
// The receiving side of a file in parts (novox/hq ADR 0611, ADR 0627 §2, design 51 *Publishing*): a holder opens a
// transfer naming every part, takes the chunks of the parts it lacks into its own objects, checks each whole part's
// hash, drops a transfer left idle when its idle bound ends and says when its objects fill. Like the sender it names no seat, no verb and no
// seat's type: the caller's own shape of an opening rides along as M, its objects and its state are its own, and the
// checks before storing, the storing and the saying of a refusal are the caller's functions. Each holder binds it to
// the seat it holds.
import (
"bytes"
"crypto/sha256"
"encoding/hex"
"errors"
"fmt"
"slices"
"sort"
"strings"
"time"
)
// The bounds design 51 gives the receiving side; a Receiver's own fields override them.
const (
// MaxReceived is the most bytes one chunk may carry when it arrives.
MaxReceived = 1 << 20
// IdleAfter is how long a transfer with no chunk stays open.
IdleAfter = 24 * time.Hour
// FullPercent is the fill of the objects past which the receiver says they fill.
FullPercent = 80
// TakeOverAfter is how long a transfer's last part must lie behind it before another caller opening it with the
// same digest takes it over (design 47 H2 H-A-481): a sender restarted mid-transfer goes on, a live one is not
// pushed aside.
TakeOverAfter = 2 * time.Minute
)
// Want is one part an opening names: its digest (sha256:<hex>) and its size.
type Want struct {
Digest string `json:"digest"`
Size int64 `json:"size"`
}
// Transfer is one open transfer, kept in the caller's state; its chunks are objects of the caller's object store. Shape
// is the caller's own body of the opening, kept as it came.
type Transfer[M any] struct {
ID string `json:"id"`
Opener string `json:"opener"`
Parts []Want `json:"parts"`
// Held are the parts whose every chunk arrived and hashed right, or that the holder held at the open; Chunks the
// chunks taken per part.
Held []string `json:"held,omitempty"`
Chunks map[string]int `json:"chunks,omitempty"`
// FillAtOpen is the objects' fill read from the store's figures when the transfer opened (or when chunks were dropped
// since), and TakenSince the bytes of the chunks taken after that read: the fill told per chunk is their sum, kept
// with the transfer, never read again per chunk (design 47 H lead answers H-A-227, H-A-228).
FillAtOpen uint64 `json:"fill_at_open,omitempty"`
TakenSince uint64 `json:"taken_since,omitempty"`
Opened time.Time `json:"opened"`
Last time.Time `json:"last"`
Shape M `json:"shape"`
}
// Lacks is the digests of the parts the transfer does not hold yet, sorted.
func (t Transfer[M]) Lacks() []string {
var out []string
for _, p := range t.Parts {
if !slices.Contains(t.Held, p.Digest) && !slices.Contains(out, p.Digest) {
out = append(out, p.Digest)
}
}
sort.Strings(out)
return out
}
// Taken says whether any chunk of the part was taken in this transfer (a part held at the open was not).
func (t Transfer[M]) Taken(digest string) bool {
_, ok := t.Chunks[digest]
return ok
}
// Objects is the holder's own object store the chunks go into.
type Objects interface {
Put(name string, data []byte) error
Get(name string) ([]byte, error)
Delete(name string) error
// Used is the bytes the store holds.
Used() (uint64, error)
}
// Transfers is the holder's own state of open transfers, keyed by Transfer.ID.
type Transfers[M any] interface {
Get(id string) (Transfer[M], bool, error)
Put(t Transfer[M]) error
Delete(id string) error
List() ([]Transfer[M], error)
}
// Received is the receiver's answer to an open or a chunk: the parts still lacked, and whether the transfer ended
// (stored or refused).
type Received struct {
ID string
Lacks []string
Ended bool
}
// AtCap opens the reason of a refusal for want of room: the objects would pass their cap. A holder tells it from
// the other refusals with RefusedAtCap, so the finding it raises for it can clear once it takes a file again.
const AtCap = "the objects are at their cap"
// RefusedAtCap says whether a refusal's reason is one for want of room (AtCap).
func RefusedAtCap(reason string) bool { return strings.HasPrefix(reason, AtCap) }
// Receiver takes transfers in parts into a holder's own objects.
type Receiver[M any] struct {
Objects Objects
Transfers Transfers[M]
// Prefix starts the name of every chunk object, so two receivers can share one object store.
Prefix string
// Cap is the bytes the objects may hold; an opening that would pass it is refused. Required: it is said where
// Bound answers no cap.
Cap uint64
// Bound reads the cap the objects' binding carries (BusCap): the bus's own bound, which then counts over Cap.
// Optional; an error or a zero falls back to Cap.
Bound func() (uint64, error)
// MaxChunk, Idle, TakeOver and Full override MaxReceived, IdleAfter, TakeOverAfter and FullPercent when set.
MaxChunk int
Idle time.Duration
TakeOver time.Duration
Full uint64
// Holds says the holder already holds a part, which then counts as held at the open and is not sent. Optional.
Holds func(t Transfer[M], digest string) bool
// HoldsWhole says the holder holds the file itself the transfer brings (its manifest or reference at the name and
// version its shape names), never from its parts alone (design 47 H2 H-A-481 second addendum): a reopen that lacks
// nothing is then dropped without a second store. Optional: nil never drops, and the reopen finishes as usual.
HoldsWhole func(t Transfer[M]) bool
// Check is the caller's check of a whole transfer before anything is stored; its error is the refusal's reason.
// Optional.
Check func(t Transfer[M]) error
// Store stores a whole, checked transfer: the parts taken in it by digest (not those held at the open). It says
// the outcome itself; its error is answered as it is.
Store func(t Transfer[M], parts map[string][]byte) error
// Refuse says a refusal by the receiver (the cap, the caller's check, a part it cannot read back). Its own error is
// joined to the refusal's.
Refuse func(t Transfer[M], reason string) error
// Filled is told the objects' fill after every change: used, the cap and whether it is past Full percent. Optional.
Filled func(used, cap uint64, full bool)
// Arm writes a transfer's idle key with its own time to live, once (BusArm over a state declared key-ttl); one
// already there is no fault. The key's end is the event that calls Expired, so an open transfer left with no
// chunk is dropped without a timer. Optional: nil arms nothing, and only DropIdle drops.
Arm func(id string, ttl time.Duration) error
Now func() time.Time
}
func (r *Receiver[M]) now() time.Time {
if r.Now != nil {
return r.Now().UTC()
}
return time.Now().UTC()
}
func (r *Receiver[M]) maxChunk() int {
if r.MaxChunk > 0 {
return r.MaxChunk
}
return MaxReceived
}
func (r *Receiver[M]) idle() time.Duration {
if r.Idle > 0 {
return r.Idle
}
return IdleAfter
}
// CapNow is the cap the receiver counts against: the binding's (Bound) when it answers one, else Cap.
func (r *Receiver[M]) CapNow() uint64 {
if r.Bound != nil {
if c, err := r.Bound(); err == nil && c > 0 {
return c
}
}
return r.Cap
}
func (r *Receiver[M]) takeOver() time.Duration {
if r.TakeOver > 0 {
return r.TakeOver
}
return TakeOverAfter
}
func (r *Receiver[M]) fullPct() uint64 {
if r.Full > 0 {
return r.Full
}
return FullPercent
}
// Open opens a transfer for opener, or opens it again: a transfer already open answers what it lacks, so a cut transfer
// goes on from there. A transfer is finished or taken over, never left open (design 47 H2 H-A-481 and its addendum):
// one opened again with nothing lacked (its finish was cut) is finished, by its opener or by another caller with the
// same digest, once each part taken in it is read back and hashes to its digest (a part that does not is lacked again),
// and dropped without a second store when the holder already holds the file itself (HoldsWhole); one that lacks a part is another
// caller's only once its last part is older than TakeOver, and is refused to it before. A new transfer that would pass the cap is refused; parts the holder holds count as held; when nothing is
// lacked the transfer ends at once, its idle bound armed before the finish, so a cut finish never leaves it open.
func (r *Receiver[M]) Open(id, opener string, parts []Want, shape M) (Received, error) {
switch {
case strings.TrimSpace(id) == "" || strings.ContainsAny(id, "/. *>"):
return Received{}, fmt.Errorf("transfer %q is no transfer id", id)
case len(parts) == 0:
return Received{}, errors.New("no parts")
}
for _, p := range parts {
if !strings.HasPrefix(p.Digest, "sha256:") || len(p.Digest) != len("sha256:")+64 {
return Received{}, fmt.Errorf("part digest %q is no sha256 digest", p.Digest)
}
}
if t, found, err := r.Transfers.Get(id); err != nil {
return Received{}, err
} else if found {
if len(t.Lacks()) == 0 {
if r.heldWhole(t) {
// The cut came after the store: the holder holds the file, so it is not stored a second time.
if _, err := r.Drop(id); err != nil {
return Received{}, fmt.Errorf("transfer %s is stored already and cannot be dropped: %w", id, err)
}
return Received{ID: id, Ended: true}, nil
}
if !r.verify(&t) {
return r.finishArmed(t)
}
if err := r.Transfers.Put(t); err != nil {
return Received{}, fmt.Errorf("transfer %s cannot be kept: %w", id, err)
}
r.tell(t.FillAtOpen)
}
switch {
case len(t.Lacks()) == 0:
return r.finishArmed(t)
case t.Opener != opener && r.now().Sub(t.Last) < r.takeOver():
return Received{}, fmt.Errorf("refused for %s: transfer %s was opened by another caller, whose last part came %s ago (taken over after %s)",
opener, id, r.now().Sub(t.Last).Round(time.Second), r.takeOver())
case t.Opener != opener:
t.Opener = opener
if err := r.Transfers.Put(t); err != nil {
return Received{}, fmt.Errorf("transfer %s cannot be taken over by %s: %w", id, opener, err)
}
}
return Received{ID: id, Lacks: t.Lacks()}, r.arm(id)
}
used, err := r.Objects.Used()
if err != nil {
return Received{}, fmt.Errorf("the objects' fill cannot be read: %w", err)
}
t := Transfer[M]{ID: id, Opener: opener, Parts: parts, Chunks: map[string]int{}, FillAtOpen: used, Opened: r.now(),
Last: r.now(), Shape: shape}
var size uint64
for _, p := range parts {
size += uint64(max(p.Size, 0))
}
if cap := r.CapNow(); used+size > cap {
return Received{ID: id, Ended: true}, r.refuse(t, fmt.Sprintf(AtCap+" of %d bytes (%d held, %d asked)", cap, used, size))
}
if r.Holds != nil {
for _, p := range parts {
if r.Holds(t, p.Digest) {
t.Held = append(t.Held, p.Digest)
}
}
}
if err := r.Transfers.Put(t); err != nil {
return Received{}, fmt.Errorf("transfer %s cannot be kept: %w", id, err)
}
r.tell(used)
if len(t.Lacks()) == 0 {
return r.finishArmed(t)
}
return Received{ID: id, Lacks: t.Lacks()}, r.arm(id)
}
// heldWhole says the holder already holds the file t brings (HoldsWhole), so a finish cut after its store stores nothing
// again; parts held alone never say it (H-A-481 second addendum), since a publish cut before its store must still be
// said as published or refused.
func (r *Receiver[M]) heldWhole(t Transfer[M]) bool {
return r.HoldsWhole != nil && r.HoldsWhole(t)
}
// verify reads each part taken in t back from the objects and hashes it against its digest, as Take does at a part's
// last chunk (H-A-481 addendum): a finish cut after its chunks were dropped reads them absent, which the runtime's
// object read answers as empty. A part that fails has its chunks dropped and is lacked again. It answers whether any
// part failed, t changed with it.
func (r *Receiver[M]) verify(t *Transfer[M]) bool {
failed := false
for _, p := range t.Parts {
if !t.Taken(p.Digest) || !slices.Contains(t.Held, p.Digest) {
continue
}
whole, err := r.part(*t, p.Digest)
if err == nil && "sha256:"+sum(whole) == p.Digest {
continue
}
r.dropChunks(*t, p.Digest)
delete(t.Chunks, p.Digest)
t.Held = slices.DeleteFunc(t.Held, func(d string) bool { return d == p.Digest })
failed = true
}
if failed {
r.reanchor(t)
}
return failed
}
// finishArmed finishes a transfer that lacks nothing at an open, its idle bound armed first (H-A-481): a finish cut
// by a restart leaves a kept transfer its key still drops, and the next open finishes it. A key not written fails the
// open loudly before the finish, the transfer kept for the next open.
func (r *Receiver[M]) finishArmed(t Transfer[M]) (Received, error) {
if err := r.arm(t.ID); err != nil {
return Received{}, err
}
return Received{ID: t.ID, Ended: true}, r.finish(t)
}
// arm bounds an open transfer by its idle key. A key that cannot be written fails the open loudly (the transfer is
// kept, so the opener's next open of it answers what is held and arms it again).
func (r *Receiver[M]) arm(id string) error {
if r.Arm == nil {
return nil
}
if err := r.Arm(id, r.idle()); err != nil {
return fmt.Errorf("transfer %s is open but its idle bound is not kept, so it would never be dropped: %w", id, err)
}
return nil
}
// Take takes one chunk of a part from the transfer's opener: chunks of a part arrive in sequence from 0, the last
// marked, and a chunk 0 again starts the part afresh (its earlier chunks are dropped); the whole part must hash to its
// digest, else its chunks are dropped and the part is lacked again. Belongs, when given, refuses a transfer the caller's own shape says is not this call's (another seat's, say). When the last
// lacked part arrives the transfer ends: the caller's check, then its store.
func (r *Receiver[M]) Take(id, opener, digest string, sequence int, last bool, data []byte, belongs func(Transfer[M]) error) (Received, error) {
t, found, err := r.Transfers.Get(id)
switch {
case err != nil:
return Received{}, err
case !found:
return Received{}, fmt.Errorf("transfer %s is not open (dropped after %s with no chunk, or never opened)", id, r.idle())
case t.Opener != opener:
return Received{}, fmt.Errorf("refused for %s: transfer %s was opened by another caller", opener, id)
}
if belongs != nil {
if err := belongs(t); err != nil {
return Received{}, err
}
}
switch {
case len(data) > r.maxChunk():
return Received{}, fmt.Errorf("a chunk of %d bytes is over the %d a chunk may carry", len(data), r.maxChunk())
case !slices.ContainsFunc(t.Parts, func(p Want) bool { return p.Digest == digest }):
return Received{}, fmt.Errorf("transfer %s names no part %s", id, digest)
case slices.Contains(t.Held, digest):
return Received{ID: id, Lacks: t.Lacks()}, nil
case sequence != t.Chunks[digest] && sequence != 0:
return Received{}, fmt.Errorf("part %s of transfer %s waits for chunk %d, not %d", digest, id, t.Chunks[digest], sequence)
}
if sequence == 0 && t.Chunks[digest] != 0 {
// A part the holder lacks travels again whole from chunk 0 (design 51 Publishing): its earlier chunks are dropped.
r.dropChunks(t, digest)
t.Chunks[digest] = 0
r.reanchor(&t)
}
if err := r.Objects.Put(r.chunkName(id, digest, sequence), data); err != nil {
return Received{}, fmt.Errorf("the objects did not take the chunk: %w", err)
}
// A transfer read back from a key-value state has no Chunks until one was taken: the empty map is kept omitted.
if t.Chunks == nil {
t.Chunks = map[string]int{}
}
t.Chunks[digest] = sequence + 1
t.TakenSince += uint64(len(data))
t.Last = r.now()
if last {
whole, err := r.part(t, digest)
if err != nil {
return Received{}, err
}
if got := "sha256:" + sum(whole); got != digest {
r.dropChunks(t, digest)
delete(t.Chunks, digest)
r.reanchor(&t)
_ = r.Transfers.Put(t)
r.tell(t.FillAtOpen)
return Received{}, fmt.Errorf("refused: the bytes of part %s hash to %s; send the part again", digest, got)
}
t.Held = append(t.Held, digest)
}
if err := r.Transfers.Put(t); err != nil {
return Received{}, err
}
r.tell(t.FillAtOpen + t.TakenSince)
if len(t.Lacks()) == 0 {
return Received{ID: id, Ended: true}, r.finish(t)
}
return Received{ID: id, Lacks: t.Lacks()}, nil
}
// finish checks and stores a whole transfer; its chunks and its key go either way.
func (r *Receiver[M]) finish(t Transfer[M]) error {
defer func() {
for _, p := range t.Parts {
r.dropChunks(t, p.Digest)
}
_ = r.Transfers.Delete(t.ID)
r.fill()
}()
if r.Check != nil {
if err := r.Check(t); err != nil {
return r.refuse(t, err.Error())
}
}
parts := map[string][]byte{}
for _, p := range t.Parts {
if !t.Taken(p.Digest) {
continue // held by the holder already
}
b, err := r.part(t, p.Digest)
if err != nil {
return r.refuse(t, "part "+p.Digest+" cannot be read from the objects: "+err.Error())
}
parts[p.Digest] = b
}
return r.Store(t, parts)
}
func (r *Receiver[M]) refuse(t Transfer[M], reason string) error {
err := errors.New("refused: " + reason)
if r.Refuse != nil {
if serr := r.Refuse(t, reason); serr != nil {
return fmt.Errorf("%w; and the refusal could not be said: %w", err, serr)
}
}
return err
}
func (r *Receiver[M]) part(t Transfer[M], digest string) ([]byte, error) {
var buf bytes.Buffer
for seq := 0; seq < t.Chunks[digest]; seq++ {
b, err := r.Objects.Get(r.chunkName(t.ID, digest, seq))
if err != nil {
return nil, err
}
buf.Write(b)
}
return buf.Bytes(), nil
}
func (r *Receiver[M]) dropChunks(t Transfer[M], digest string) {
for seq := 0; seq < t.Chunks[digest]; seq++ {
_ = r.Objects.Delete(r.chunkName(t.ID, digest, seq))
}
}
func (r *Receiver[M]) chunkName(id, digest string, seq int) string {
return fmt.Sprintf("%s%s/%s/%d", r.Prefix, id, strings.TrimPrefix(digest, "sha256:"), seq)
}
// fill reads the objects' fill from the store's figures and tells Filled: at a transfer's finish and drop, never per
// chunk (H-A-227).
func (r *Receiver[M]) fill() {
if r.Filled == nil {
return
}
used, err := r.Objects.Used()
if err != nil {
return
}
r.tell(used)
}
// tell tells Filled a fill already known: read at the open, or that read plus the bytes taken since.
func (r *Receiver[M]) tell(used uint64) {
cap := r.CapNow()
if r.Filled == nil || cap == 0 {
return
}
r.Filled(used, cap, used*100 >= cap*r.fullPct())
}
// reanchor reads the fill again after a transfer's chunks were dropped, whose sizes it does not keep: the read becomes
// the transfer's FillAtOpen and nothing is taken since. A read that fails keeps the figures it had.
func (r *Receiver[M]) reanchor(t *Transfer[M]) {
if used, err := r.Objects.Used(); err == nil {
t.FillAtOpen, t.TakenSince = used, 0
}
}
// Drop drops a transfer, by hand or because it was idle: its chunks and its key.
func (r *Receiver[M]) Drop(id string) (Transfer[M], error) {
t, found, err := r.Transfers.Get(id)
if err != nil {
return Transfer[M]{}, err
}
if !found {
return Transfer[M]{}, fmt.Errorf("transfer %s is not open", id)
}
for _, p := range t.Parts {
r.dropChunks(t, p.Digest)
}
if err := r.Transfers.Delete(id); err != nil {
return Transfer[M]{}, err
}
r.fill()
return t, nil
}
// Expired is told that a transfer's idle key ended (the bus's marker, FollowIdle): a transfer with no chunk for Idle
// is dropped with its chunks; one that took a chunk since is armed again for the rest of its bound; one already gone
// is nothing to do. It answers whether it dropped the transfer.
func (r *Receiver[M]) Expired(id string) (bool, error) {
t, found, err := r.Transfers.Get(id)
if err != nil {
return false, fmt.Errorf("transfer %s's idle bound ended, but it cannot be read: %w", id, err)
}
if !found {
return false, nil
}
idle := r.now().Sub(t.Last)
if idle >= r.idle() {
if _, err := r.Drop(id); err != nil {
return false, fmt.Errorf("transfer %s had no chunk for %s and is not dropped: %w", id, r.idle(), err)
}
return true, nil
}
if r.Arm == nil {
return false, nil
}
return false, r.Arm(id, max(r.idle()-idle, time.Second))
}
// DropIdle drops every transfer with no chunk for Idle and answers their ids: a look by hand (a listing tool). The
// drop itself follows each transfer's idle key (Arm, Expired), never a timer.
func (r *Receiver[M]) DropIdle() ([]string, error) {
all, err := r.Transfers.List()
if err != nil {
return nil, err
}
var out []string
for _, t := range all {
if r.now().Sub(t.Last) >= r.idle() {
if _, err := r.Drop(t.ID); err == nil {
out = append(out, t.ID)
}
}
}
sort.Strings(out)
return out, nil
}
func sum(b []byte) string {
s := sha256.Sum256(b)
return hex.EncodeToString(s[:])
}