A reopen verifies its parts; the takeover bound is a field (H-A-481 addendum)
A reopen that lacked nothing finished from the held set without reading the chunks back, so a finish cut after its chunks were dropped, or after its store, stored wrong bytes or stored twice. Now each part taken is read back and hashed against its digest as a take does, a failing part is lacked again, and a transfer whose every part the holder already holds is dropped without a second store. The takeover bound is the receiver's field TakeOver, two minutes by default, as its sibling bounds. Builds: design 47 H2 H-A-481 addendum; review round 1 findings (1), (2)
This commit is contained in:
@@ -121,9 +121,10 @@ type Receiver[M any] struct {
|
||||
// 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 and Full override MaxReceived, IdleAfter and FullPercent when set.
|
||||
// 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
|
||||
@@ -176,6 +177,13 @@ func (r *Receiver[M]) CapNow() uint64 {
|
||||
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
|
||||
@@ -184,10 +192,11 @@ func (r *Receiver[M]) fullPct() uint64 {
|
||||
}
|
||||
|
||||
// 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): one opened again
|
||||
// with nothing lacked (its finish was cut) is finished, by its opener or by another caller with the same digest; one
|
||||
// that lacks a part is another caller's only once its last part is older than TakeOverAfter, 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
|
||||
// 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 every part; 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 {
|
||||
@@ -204,12 +213,28 @@ func (r *Receiver[M]) Open(id, opener string, parts []Want, shape M) (Received,
|
||||
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) < TakeOverAfter:
|
||||
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), TakeOverAfter)
|
||||
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 {
|
||||
@@ -248,6 +273,44 @@ func (r *Receiver[M]) Open(id, opener string, parts []Want, shape M) (Received,
|
||||
return Received{ID: id, Lacks: t.Lacks()}, r.arm(id)
|
||||
}
|
||||
|
||||
// heldWhole says the holder already holds every part of t (Holds), so a finish cut after its store stores nothing again.
|
||||
func (r *Receiver[M]) heldWhole(t Transfer[M]) bool {
|
||||
if r.Holds == nil {
|
||||
return false
|
||||
}
|
||||
for _, p := range t.Parts {
|
||||
if !r.Holds(t, p.Digest) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// 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.
|
||||
|
||||
@@ -298,32 +298,46 @@ func aHolderTakesAFileInParts(t *testing.T) {
|
||||
pkg.Arm = func(id string, _ time.Duration) error { order = append(order, "arm "+id); return nil }
|
||||
store := pkg.Store
|
||||
defer func() { pkg.Arm, pkg.Holds, pkg.Store = nil, nil, store }()
|
||||
cut := func(id string, lacked bool) {
|
||||
// cut leaves c1 as a cut finish does: "whole" all chunks there, "lacked" a part short, "gone" its chunks dropped
|
||||
// before the cut, "wrong" a chunk that does not hash.
|
||||
cut := func(id, how string) {
|
||||
tr := Transfer[packageShape]{ID: id, Opener: "builder", Parts: []Want{part}, Chunks: map[string]int{part.Digest: 1},
|
||||
Held: []string{part.Digest}, Opened: now, Last: now, Shape: packageShape{"m", "1"}}
|
||||
if lacked {
|
||||
tr.Held, tr.Chunks = nil, map[string]int{part.Digest: 1}
|
||||
objects[pkg.chunkName(id, part.Digest, 0)] = blob[:1000]
|
||||
} else {
|
||||
switch how {
|
||||
case "whole":
|
||||
objects[pkg.chunkName(id, part.Digest, 0)] = blob
|
||||
case "lacked":
|
||||
tr.Held = nil
|
||||
objects[pkg.chunkName(id, part.Digest, 0)] = blob[:1000]
|
||||
case "wrong":
|
||||
objects[pkg.chunkName(id, part.Digest, 0)] = []byte("other")
|
||||
}
|
||||
if err := pkg.Transfers.Put(tr); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
for _, c := range []struct {
|
||||
name, opener string
|
||||
lacked bool
|
||||
after time.Duration
|
||||
want string // "stored", "lacks" (taken over or gone on) or the refusal's words
|
||||
name, opener, how string
|
||||
after, takeOver time.Duration
|
||||
storeHolds bool
|
||||
want string // "stored", "dropped", "lacked again", "lacks" (taken over) or the refusal's words
|
||||
}{
|
||||
{"the opener opens a cut finish again", "builder", false, 0, "stored"},
|
||||
{"another caller opens a cut finish", "other", false, 0, "stored"},
|
||||
{"another caller while the opener sends", "other", true, time.Minute, "another caller, whose last part came 1m0s ago"},
|
||||
{"another caller once the last part is two minutes old", "other", true, TakeOverAfter, "lacks"},
|
||||
{name: "the opener opens a cut finish again", opener: "builder", how: "whole", want: "stored"},
|
||||
{name: "another caller opens a cut finish", opener: "other", how: "whole", want: "stored"},
|
||||
{name: "a cut after the chunks were dropped", opener: "builder", how: "gone", want: "lacked again"},
|
||||
{name: "a cut finish whose chunk does not hash", opener: "builder", how: "wrong", want: "lacked again"},
|
||||
{name: "a cut after the store", opener: "builder", how: "whole", storeHolds: true, want: "dropped"},
|
||||
{name: "another caller while the opener sends", opener: "other", how: "lacked", after: time.Minute, want: "another caller, whose last part came 1m0s ago"},
|
||||
{name: "another caller once the last part is two minutes old", opener: "other", how: "lacked", after: TakeOverAfter, want: "lacks"},
|
||||
{name: "the receiver's own takeover bound", opener: "other", how: "lacked", after: time.Minute, takeOver: 30 * time.Second, want: "lacks"},
|
||||
} {
|
||||
pkgLog = nil
|
||||
cut("c1", c.lacked)
|
||||
pkg.TakeOver = c.takeOver
|
||||
pkg.Holds = nil
|
||||
if c.storeHolds {
|
||||
pkg.Holds = func(Transfer[packageShape], string) bool { return true }
|
||||
}
|
||||
cut("c1", c.how)
|
||||
now = now.Add(c.after)
|
||||
got, err := pkg.Open("c1", c.opener, []Want{part}, packageShape{"m", "1"})
|
||||
kept, open, _ := pkg.Transfers.Get("c1")
|
||||
@@ -332,6 +346,15 @@ func aHolderTakesAFileInParts(t *testing.T) {
|
||||
if err != nil || !got.Ended || open || len(pkgLog) != 1 || pkgLog[0].outcome != "stored" || string(pkgLog[0].parts[part.Digest]) != string(blob) {
|
||||
t.Errorf("%s: open = %+v, %v, still open %v, log %+v; want it finished and stored", c.name, got, err, open, pkgLog)
|
||||
}
|
||||
case "dropped":
|
||||
if err != nil || !got.Ended || open || len(pkgLog) != 0 {
|
||||
t.Errorf("%s: open = %+v, %v, still open %v, log %+v; want it dropped, not stored again", c.name, got, err, open, pkgLog)
|
||||
}
|
||||
case "lacked again":
|
||||
if err != nil || got.Ended || !open || len(pkgLog) != 0 || !reflect.DeepEqual(got.Lacks, []string{part.Digest}) || kept.Taken(part.Digest) {
|
||||
t.Errorf("%s: open = %+v, %v, log %+v, kept %+v; want the part lacked again, nothing stored", c.name, got, err, pkgLog, kept)
|
||||
}
|
||||
_, _ = pkg.Drop("c1")
|
||||
case "lacks":
|
||||
if err != nil || got.Ended || !open || kept.Opener != c.opener || !reflect.DeepEqual(got.Lacks, []string{part.Digest}) {
|
||||
t.Errorf("%s: open = %+v, %v, opener %q; want it taken over by %s, lacking the part", c.name, got, err, kept.Opener, c.opener)
|
||||
@@ -349,6 +372,7 @@ func aHolderTakesAFileInParts(t *testing.T) {
|
||||
t.Errorf("%s: %d objects left", c.name, len(objects))
|
||||
}
|
||||
}
|
||||
pkg.TakeOver = 0
|
||||
order = nil
|
||||
pkg.Holds = func(Transfer[packageShape], string) bool { return true }
|
||||
pkg.Store = func(tr Transfer[packageShape], _ map[string][]byte) error {
|
||||
|
||||
Reference in New Issue
Block a user