RG1: the registry takes a publish ask once by its id

A publish carrying an ask id is kept in the state taken for ask-taken-bound
(10m): a repeat while the act runs joins it, a repeat after it ended answers
the act's state read afresh (the transfer's lacking parts, a held version
ended, a transfer gone refused), never publishing twice.

Builds: design 47 H2 §775, §820; H-A-359, H-A-381
This commit is contained in:
2026-11-22 00:05:58 +01:00
parent d545299a71
commit babcd1bb31
7 changed files with 217 additions and 6 deletions
@@ -36,7 +36,7 @@ func TestTheVerbsSpeakTheSeatsContract(t *testing.T) {
reg.VerbVersions: reg.VersionsInput{}, reg.VerbRemove: reg.RemoveInput{}, reg.VerbHold: reg.HoldInput{},
reg.VerbRelease: reg.ReleaseInput{}, reg.VerbMirror: reg.MirrorInput{}, reg.VerbTags: reg.TagsInput{}}
seen := 0
for _, tool := range tools(&holder.Verbs{}, kept[holder.Window]{}) {
for _, tool := range tools(&holder.Verbs{}, kept[holder.Window]{}, nil) {
seat, verb, ok := strings.Cut(tool.Name, ".")
in, isVerb := inputs[verb]
if !ok || !isVerb || (seat != holder.ImageSeat && seat != holder.ArtifactSeat) {
@@ -244,7 +244,7 @@ func arg[T any](args map[string]any) (T, error) {
func str(args map[string]any, k string) string { s, _ := args[k].(string); return strings.TrimSpace(s) }
// tools are distribution's tools over v.
func tools(v *holder.Verbs, windows kept[holder.Window]) []stdio.Tool {
func tools(v *holder.Verbs, windows kept[holder.Window], taken *asks) []stdio.Tool {
obj := func(props map[string]any, req ...string) map[string]any {
return map[string]any{"type": "object", "required": req, "properties": props}
}
@@ -262,7 +262,9 @@ func tools(v *holder.Verbs, windows kept[holder.Window]) []stdio.Tool {
caller := stdio.CallerOf(args)
start := time.Now()
logf("%s.publish received from %s: %s %s (%s, %d parts)", seat, caller, in.Name, in.Version, in.Kind, len(in.Parts))
a, err := v.Publish(seat, caller, publishOf(in))
r := publishOf(in)
a, err := taken.once(seat, stdio.AskIDOf(args), func() (holder.PublishAnswer, error) { return v.Publish(seat, caller, r) },
func() (holder.PublishAnswer, error) { return v.Repeat(seat, caller, r) })
if err == nil {
// The window's learning asks the delivery view (up to 60 s, past the runner's 30 s call bound),
// so it never holds the answer.
@@ -503,7 +505,13 @@ func main() {
logf("transfer %s had no part for %s: dropped with its parts", id, holder.TransferIdle)
})
windows := kept[holder.Window]{state: "windows", key: func(w holder.Window) string { return w.Name }}
if err := stdio.Serve("", tools(v, windows)); err != nil {
bound, err := stdio.SettingDuration(askTakenBound, askTakenDefault)
if err != nil {
logf("the setting %s: %v", askTakenBound, err)
os.Exit(1)
}
taken := &asks{take: stdio.State(stdio.TakenState).TakeOnce, bound: bound}
if err := stdio.Serve("", tools(v, windows, taken)); err != nil {
logf("%v", err)
os.Exit(1)
}
@@ -0,0 +1,75 @@
package main
import (
"fmt"
"sync"
"time"
"distribution/internal/holder"
)
// askTakenBound is the setting the taken key of a publish ask lives (design 47 H2 §775, H-A-381 (2)): the holder never
// receives the caller's bound, so it keeps an ask's id this long, the largest caller's bound by default.
const askTakenBound = "ask-taken-bound"
// askTakenDefault is ask-taken-bound's default, the runner's ask-unanswered-bound (H-A-381 (2)).
const askTakenDefault = 10 * time.Minute
// asks takes each publish ask once by its id (design 47 H2 §775; ADR 0668 §2; H-A-381): the first call with an id is
// kept in the state taken and acts; a repeat while that act runs joins it and gets its answer; a repeat after it ended
// answers the act's state read afresh (holder.Repeat), never acting twice. No answer is kept in taken. A call that
// carries no ask id is no kept call, and acts as it did before.
type asks struct {
take func(seat, ask string, bound time.Duration) (bool, error)
bound time.Duration
mu sync.Mutex
running map[string]*flight
// joined, when set, is told each repeat that joins a running act (a test's hook).
joined func()
}
// flight is one act of a taken ask while it runs, which a repeat joins.
type flight struct {
done chan struct{}
a holder.PublishAnswer
err error
}
func (k *asks) once(seat, ask string, act, repeat func() (holder.PublishAnswer, error)) (holder.PublishAnswer, error) {
if ask == "" || k == nil || k.take == nil {
return act()
}
key := seat + "." + ask
k.mu.Lock()
if f, ok := k.running[key]; ok {
k.mu.Unlock()
if k.joined != nil {
k.joined()
}
<-f.done
return f.a, f.err
}
if k.running == nil {
k.running = map[string]*flight{}
}
f := &flight{done: make(chan struct{})}
k.running[key] = f
k.mu.Unlock()
defer func() {
k.mu.Lock()
delete(k.running, key)
k.mu.Unlock()
close(f.done)
}()
took, err := k.take(seat, ask, k.bound)
switch {
case err != nil:
f.err = fmt.Errorf("publish: the ask %s of %s cannot be taken: %w", ask, seat, err)
case took:
f.a, f.err = act()
default:
logf("%s.publish: the ask %s was taken before; it answers the act's state, acting not again", seat, ask)
f.a, f.err = repeat()
}
return f.a, f.err
}
@@ -0,0 +1,63 @@
package main
import (
"errors"
"sync"
"sync/atomic"
"testing"
"time"
"distribution/internal/holder"
)
// A publish ask given twice acts once (design 47 H2 §775; H-A-381): a repeat while the act runs joins it and gets its
// answer, a repeat after it ended answers the act's state, a call with no ask id acts as before, a take that fails is said.
func TestAPublishAskIsTakenOnce(t *testing.T) {
taken := map[string]bool{}
var mu sync.Mutex
k := &asks{bound: time.Minute, take: func(seat, ask string, bound time.Duration) (bool, error) {
mu.Lock()
defer mu.Unlock()
if ask == "broken" {
return false, errors.New("no responders")
}
if bound != time.Minute || taken[seat+"."+ask] {
return false, nil
}
taken[seat+"."+ask] = true
return true, nil
}}
var acts, repeats atomic.Int32
release, started, joined := make(chan struct{}), make(chan struct{}, 1), make(chan struct{}, 1)
k.joined = func() { joined <- struct{}{} }
act := func() (holder.PublishAnswer, error) {
acts.Add(1)
started <- struct{}{}
<-release
return holder.PublishAnswer{Transfer: "t", Missing: []string{"d"}}, nil
}
repeat := func() (holder.PublishAnswer, error) { repeats.Add(1); return holder.PublishAnswer{Transfer: "t"}, nil }
answers := make(chan holder.PublishAnswer, 2)
go func() { a, _ := k.once(holder.ArtifactSeat, "a1", act, repeat); answers <- a }()
<-started
go func() { a, _ := k.once(holder.ArtifactSeat, "a1", act, repeat); answers <- a }()
<-joined
close(release)
for range 2 {
if a := <-answers; len(a.Missing) != 1 {
t.Errorf("an answer of the running act = %+v", a)
}
}
if a, _ := k.once(holder.ArtifactSeat, "a1", act, repeat); acts.Load() != 1 || repeats.Load() != 1 || a.Transfer != "t" {
t.Errorf("acts %d, repeats %d after a repeat of an ended act", acts.Load(), repeats.Load())
}
release = make(chan struct{})
close(release)
act2 := func() (holder.PublishAnswer, error) { acts.Add(1); return holder.PublishAnswer{}, nil }
if _, err := k.once(holder.ArtifactSeat, "", act2, repeat); err != nil || acts.Load() != 2 {
t.Errorf("a call with no ask id: acts %d, %v", acts.Load(), err)
}
if _, err := k.once(holder.ArtifactSeat, "broken", act2, repeat); err == nil || acts.Load() != 2 {
t.Errorf("a failed take: acts %d, %v", acts.Load(), err)
}
}