CAT-D5: the publishing builds are written with compare-and-set on the key's revision, so two first publishes at once both count
A blind write after a read could lose the build of the publish that wrote first until a bundle restart learned it again. Claude-Session: https://claude.ai/code/session_01JG73KJNrH5EgFXMKKYSrig
This commit is contained in:
@@ -154,23 +154,62 @@ func learnPublishing(windows kept[holder.Window], caller string) {
|
||||
logf("node_haves names no build of %s: the direct-write window counts it at its next publish", caller)
|
||||
return
|
||||
}
|
||||
pub := kept[holder.Publishing]{state: "windows", key: func(p holder.Publishing) string { return holder.PublishingKey }}
|
||||
cur, _, err := pub.Get(holder.PublishingKey)
|
||||
learnt, err := learnInto(stdio.State("windows"), module, a.Keys[caller].FP, learnAttempts)
|
||||
if err != nil {
|
||||
logf("the publishing builds cannot be read: %v", err)
|
||||
logf("the publishing build of %s is not kept: %v", caller, err)
|
||||
return
|
||||
}
|
||||
cur.Name = holder.PublishingKey
|
||||
if cur.Learn(module, a.Keys[caller].FP) {
|
||||
if err := pub.Put(cur); err != nil {
|
||||
logf("the publishing build of %s is not kept: %v", caller, err)
|
||||
return
|
||||
}
|
||||
if learnt {
|
||||
logf("%s's build %s publishes through the seats", module, a.Keys[caller].FP)
|
||||
}
|
||||
learned.Store(caller, true)
|
||||
}
|
||||
|
||||
// learnAttempts bounds learnInto's tries against other writers of the key.
|
||||
const learnAttempts = 8
|
||||
|
||||
// casState is the part of a kept state learnInto writes through: a read with its revision, a create, an update at a
|
||||
// revision (stdio.KeptState).
|
||||
type casState interface {
|
||||
Get(key string) (*stdio.StateEntry, error)
|
||||
Create(key string, value any) (uint64, error)
|
||||
Update(key string, value any, revision uint64) (uint64, error)
|
||||
}
|
||||
|
||||
// learnInto adds fp for module to the publishing builds kept at holder.PublishingKey with compare-and-set on the key's
|
||||
// revision: two first publishes at once each read, add and write only if nobody wrote meanwhile, else read again, so
|
||||
// neither is lost. It answers whether fp was new.
|
||||
func learnInto(state casState, module, fp string, attempts int) (bool, error) {
|
||||
for i := 0; i < attempts; i++ {
|
||||
e, err := state.Get(holder.PublishingKey)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
var cur holder.Publishing
|
||||
if e != nil {
|
||||
if err := json.Unmarshal(e.Value, &cur); err != nil {
|
||||
return false, fmt.Errorf("%s holds no publishing builds: %w", holder.PublishingKey, err)
|
||||
}
|
||||
}
|
||||
cur.Name = holder.PublishingKey
|
||||
if !cur.Learn(module, fp) {
|
||||
return false, nil
|
||||
}
|
||||
if e == nil {
|
||||
_, err = state.Create(holder.PublishingKey, cur)
|
||||
} else {
|
||||
_, err = state.Update(holder.PublishingKey, cur, e.Revision)
|
||||
}
|
||||
if err == nil {
|
||||
return true, nil
|
||||
}
|
||||
if !errors.Is(err, stdio.ErrChanged) {
|
||||
return false, err
|
||||
}
|
||||
}
|
||||
return false, fmt.Errorf("%s was written by another writer first %d times: %w", holder.PublishingKey, attempts, stdio.ErrChanged)
|
||||
}
|
||||
|
||||
func arg[T any](args map[string]any) (T, error) {
|
||||
var v T
|
||||
clean := map[string]any{}
|
||||
|
||||
Reference in New Issue
Block a user