H-A-311 (1): a placing cut by a concurrent write is done again from the record read again

A placing whose compare-and-set was refused because the pipeline was
written meanwhile was dropped and never done again: main.137 stood
seventeen minutes with eleven steps placed until a restart.

Builds: design 47 H H-A-311 (1), H-A-288
This commit is contained in:
2026-11-21 20:41:29 +01:00
parent e196064e51
commit 6c66bf9a5a
2 changed files with 81 additions and 29 deletions
@@ -746,40 +746,15 @@ func (s *Starter) placeWaiting(store Updater, due func(seat.Pipeline) bool, reg
}
}
for _, key := range pkeys {
p, rev, err := store.Get(key)
p, asks, ended, notAsked, n, err := s.placeOne(store, key, due, reg, view, now)
if err != nil {
note(fmt.Errorf("the pipeline %s is not read: %w", key, err))
continue
note(err)
}
if p.Outcome != seat.PipelineOutcomeRunning || p.Run != "" || !due(p) {
continue
}
half, ok := view(p.Branch)
if !ok {
s.logf("the catalog half of %s is not held yet: %s waits on it for its steps, placed once it is held", p.Branch, key)
continue
}
if p.Revision > 0 && half.Revision < p.Revision {
s.logf("the catalog half of %s holds revision %d: %s waits for revision %d, which its take-in made, to place its steps (§200)", p.Branch, half.Revision, key, p.Revision)
continue
}
before := len(p.Steps)
asks, unregistered, ended, notAsked := s.placeMovers(&p, reg, half.Entries, now)
// Until its registration arrives, the pipeline says it waits on it (A-327): a mover with no registration has no step,
// and it holds the pipeline's end only while every placed mover's required steps passed (H-A-163, H-A-171).
for _, m := range unregistered {
s.logf("%s: %s has no registration yet: its steps wait on its registration, which places them (A-327)", key, m)
s.registrationMissing(m, key)
}
if len(p.Steps) == before {
continue
}
if _, err := store.Update(key, p, rev); err != nil {
s.logf("%s is written meanwhile, so nothing is placed in it now: %v", key, err)
if n == 0 {
continue
}
count++
s.logf("%s: %d steps placed from %s (A-327)", key, len(p.Steps)-before, from)
s.logf("%s: %d steps placed from %s (A-327)", key, n, from)
// A pipeline whose steps were never asked is started by this placing (A-446a); the pipeline-started of its start,
// said already, is said again harmlessly.
s.sayStartedPipeline(p, now)
@@ -799,6 +774,50 @@ func (s *Starter) placeWaiting(store Updater, due func(seat.Pipeline) bool, reg
return count, first
}
// placeOne places, in pipeline key, the steps of each mover with none, when the pipeline is running, not a run on request
// and due names it (placeWaiting). Its write is by compare-and-set: a write refused because the pipeline was written
// meanwhile is done again at once from the record as read again, until it lands or the pipeline no longer waits on a
// placing (it ended, or every mover due is placed), so a concurrent write never cuts a placing for good (H-A-311 (1)). It
// answers the pipeline as written, its asks, the steps ended or not asked at the placing, and how many steps it placed.
func (s *Starter) placeOne(store Updater, key string, due func(seat.Pipeline) bool, reg func(module string) (catalog.Registration, bool), view func(branch string) (Half, bool), now time.Time) (seat.Pipeline, []Asked, []int, []int, int, error) {
for {
p, rev, err := store.Get(key)
if err != nil {
return p, nil, nil, nil, 0, fmt.Errorf("the pipeline %s is not read: %w", key, err)
}
if p.Outcome != seat.PipelineOutcomeRunning || p.Run != "" || !due(p) {
return p, nil, nil, nil, 0, nil
}
half, ok := view(p.Branch)
if !ok {
s.logf("the catalog half of %s is not held yet: %s waits on it for its steps, placed once it is held", p.Branch, key)
return p, nil, nil, nil, 0, nil
}
if p.Revision > 0 && half.Revision < p.Revision {
s.logf("the catalog half of %s holds revision %d: %s waits for revision %d, which its take-in made, to place its steps (§200)", p.Branch, half.Revision, key, p.Revision)
return p, nil, nil, nil, 0, nil
}
before := len(p.Steps)
asks, unregistered, ended, notAsked := s.placeMovers(&p, reg, half.Entries, now)
// Until its registration arrives, the pipeline says it waits on it (A-327): a mover with no registration has no step,
// and it holds the pipeline's end only while every placed mover's required steps passed (H-A-163, H-A-171).
for _, m := range unregistered {
s.logf("%s: %s has no registration yet: its steps wait on its registration, which places them (A-327)", key, m)
s.registrationMissing(m, key)
}
if len(p.Steps) == before {
return p, nil, nil, nil, 0, nil
}
if _, err := store.Update(key, p, rev); errors.Is(err, ErrChanged) {
s.logf("%s is written meanwhile, so its placing is done again from the record read again (H-A-311): %v", key, err)
continue
} else if err != nil {
return p, nil, nil, nil, 0, fmt.Errorf("the placing of %s is not written: %w", key, err)
}
return p, asks, ended, notAsked, len(p.Steps) - before, nil
}
}
// recovered says whether the commit of pipeline key is named a recovery; a read that fails asks on the stream check, said, since
// a step is asked either way (a recovery's only goes ahead of the first round).
func (s *Starter) recovered(key, commit string) bool {
@@ -300,6 +300,25 @@ func TestAPipelineStartedBeforeItsRegistrationIsPlacedWhenItArrives(t *testing.T
t.Errorf("the pipeline a runner before this one set aside: placed %d (%v), %d steps, %d asks more, want it started as a newly started one", n, err, len(p.Steps), len(asks.asks)-before)
}
// H-A-311 (1): a placing whose write is refused because the pipeline was written meanwhile (a step's end, a publish) is
// done again at once from the record read again, never left unplaced until a restart.
held = false
taken = catalog.TakeInTaken{Branch: "next", Source: 7, Entries: []catalog.TakeInEntry{{Module: "app", Commit: "c4", Fingerprint: "src2:dd"}}}
if n, err := s.TakenIn(taken, late); n != 1 || err != nil {
t.Fatalf("the take-in of next.7 started %d (%v)", n, err)
}
held = true
raced := &racingStore{memStore: store.memStore, before: func(m memStore) {
e := m["next.7"]
e.p.Superseded, e.rev = append(e.p.Superseded, "other"), e.rev+1 // a concurrent write the placing did not read
m["next.7"] = e
}}
n, err = s.Waiting(raced, "next", late)
p, _, _ = store.Get("next.7")
if n != 1 || err != nil || len(p.Steps) == 0 || !slices.Contains(p.Superseded, "other") {
t.Errorf("a placing cut by a concurrent write placed %d (%v): %d steps, superseded %v, want its steps placed on the record read again (H-A-311)", n, err, len(p.Steps), p.Superseded)
}
// A-327: a mover with no registration, in a pipeline placed from the half held later (the queue's turn, the half held),
// is said to wait on its registration, so the pipeline that cannot end before it is placed says why.
var logged []string
@@ -411,3 +430,17 @@ func TestAPublishIsTakenIntoThePipelineOfItsRevision(t *testing.T) {
t.Errorf("a publish naming no revision (%v): main.7 %v, main.8 %v; want main.7's, the source's lowest", err, published("main.7"), published("main.8"))
}
}
// racingStore is memStore whose first Update is preceded by before, a write another part makes between a read and its write.
type racingStore struct {
memStore
before func(memStore)
}
func (r *racingStore) Update(key string, p seat.Pipeline, rev uint64) (uint64, error) {
if b := r.before; b != nil {
r.before = nil
b(r.memStore)
}
return r.memStore.Update(key, p, rev)
}