Files
mesh-delivery/modules/mesh-delivery-indexer/internal/index/takein_source.go
T
jschoubben 9cddb5921a indexer: take a watched branch's source revision in from the gate's source when its consumer cannot be bound
At a clean state the grants part makes the per-branch consumers of source-moved only from the modules view, which the
indexer answers only after its first take-in, so nothing ever took the trunk in. At its start, and again at each
failed bind, the indexer now reads the branch's current source revision through mesh-delivery.source and takes it in
as a heard source-moved of it would, when its view keeps no revision of the branch at or above it: start true exactly
when it keeps none, every repository the answer names a head. No timer; binding on registration-changed is unchanged.

Builds: design 47 H §743, §768, H-A-129, H-A-130

Claude-Session: https://claude.ai/code/session_01JG73KHUnBze48rdua8u2sy
2026-11-20 10:39:45 +01:00

179 lines
7.7 KiB
Go

package index
import (
"context"
"errors"
"fmt"
"maps"
"strconv"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshdelivery"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-indexer/takein"
"git.novox.be/novox/mesh-sdk/go/keys"
)
// ErrOldForm is a source-moved of the earlier form, one with no field its form requires (design 47 H §765, H-A-58): it is
// refused, said in the journal, and never taken in.
var ErrOldForm = errors.New("a source-moved of the old form")
// TakenID is the duplicate id take-in.taken of a branch's source revision is said under, so a take-in said again is heard once.
func TakenID(branch string, source int64) string {
return takein.TakeInTakenEvent + "." + keys.EscapeName(branch) + "." + strconv.FormatInt(source, 10)
}
// TakeInSource takes one source revision of a watched branch in, as the gate's source-moved names it (design 47 H §743): for each
// repository it moves, the trees at the named commit through the seat git (wholeAt) and the fingerprints of the modules
// registered for it; its movers (moversOf) against the branch's newest catalog revision; one catalog revision of the branch,
// <branch>.<source>, carrying the movers and pinned for the take-in's pipeline when it has one (KeepBranchEntries); and
// take-in.taken once, with branch, source, revision, call and one entry per mover; a branch's first revision (start) has no
// mover (§768). A revision already kept under that name (a
// restart before the acknowledgement) is said again from its kept movers, recomputing nothing (H-A-92). The first watched
// branch is taken in on the indexer's own view; another on its newest revision's, computed and kept only as that revision.
// An error leaves the source-moved held, written into the state held behind its branch's earlier ones (ErrHeldUnwritten when
// it is not) and said once per source as take-in.pending with branch, source and why (§744, H-A-118):
// ErrObjectsFull waits for the indexer's own room, any other is the failed read or write it says; ErrOldForm is never taken in
// and said as no take-in.
func (ix *Indexer) TakeInSource(ctx context.Context, e meshdelivery.SourceMovedEvent) (meshcatalog.TakeInTaken, error) {
taken, err := ix.takeInSource(ctx, e)
switch {
case err == nil:
ix.unhold(ctx, e.Branch, e.Source)
case !errors.Is(err, ErrOldForm):
if herr := ix.hold(ctx, e, err.Error()); herr != nil {
return taken, fmt.Errorf("%w; %w", err, herr)
}
}
return taken, err
}
func (ix *Indexer) takeInSource(ctx context.Context, e meshdelivery.SourceMovedEvent) (meshcatalog.TakeInTaken, error) {
taken := meshcatalog.TakeInTaken{Branch: e.Branch, Source: e.Source, Call: e.Call}
if e.Branch == "" || e.Source <= 0 || e.Call == "" {
return taken, fmt.Errorf("%w: branch %q, source %d, call %q", ErrOldForm, e.Branch, e.Source, e.Call)
}
if ix.Revisions == nil {
return taken, errors.New("the indexer keeps no catalog revisions")
}
ix.pendingMu.Lock()
defer ix.pendingMu.Unlock()
name := BranchObject(e.Branch, e.Source)
if kept, _, err := ReadRevision(ctx, ix.Revisions, name); err == nil {
taken.Revision, taken.Entries = kept.Revision, kept.Movers
ix.logf("take-in: %s is kept already: take-in.taken said again from its %d movers", name, len(kept.Movers))
return taken, ix.sayTaken(ctx, taken)
} else if !errors.Is(err, ErrNoObject) {
return taken, err
}
_, before, _, err := readNewestOf(ctx, ix.Revisions, e.Branch)
if err != nil {
return taken, fmt.Errorf("the newest catalog revision of %s: %w", e.Branch, err)
}
x := ix
if !isTrunkBranch(e.Branch) {
view, nodes, _, err := BranchView(ctx, ix.Revisions, e.Branch)
if err != nil {
return taken, err
}
d := ix.dry()
d.ix.Modules, d.ix.Graph, d.ix.Taken = view, nodes, newOverlay(nil)
d.ix.Logf = func(format string, args ...any) { ix.logf("take-in of "+e.Branch+": "+format, args...) }
x = d.ix
}
heads := map[string]string{}
var moved []string
for _, h := range e.Heads {
heads[h.Repository], moved = h.Commit, append(moved, h.Repository)
}
x.mu.Lock()
x.source = sourceNow{rev: uint64(e.Source), heads: mergedHeads(x.source.heads, heads)}
x.mu.Unlock()
for _, h := range e.Heads {
f, err := x.wholeAt(ctx, h.Repository, h.Commit)
if err != nil {
return taken, err
}
f.Branch = e.Branch
if _, err := x.ApplyMessage(ctx, f); err != nil {
return taken, fmt.Errorf("taking %s at %.12s in: %w", h.Repository, h.Commit, err)
}
}
entries, err := x.viewEntries(ctx)
if err != nil {
return taken, err
}
movers := moversOf(before, entries, moved)
if e.Start {
movers = nil // a branch's first revision moves nothing: its entries are the base later take-ins are held against (§768, H-A-19)
}
if err := ix.KeepBranchEntries(ctx, e.Branch, e.Source, entries, movers); err != nil {
if errors.Is(err, ErrObjectsFull) {
ix.heldFull.Store(true) // room is said once the store has room again (RoomAgain), and wakes the take-in (§744)
}
return taken, err
}
taken.Revision, taken.Entries = e.Source, movers
ix.logf("take-in: source revision %d of %s (call %s) taken in as %s, %d repositories, %d movers", e.Source, e.Branch, e.Call, name, len(e.Heads), len(movers))
return taken, ix.sayTaken(ctx, taken)
}
// mergedHeads is held with the heads given laid over it.
func mergedHeads(held, over map[string]string) map[string]string {
out := maps.Clone(held)
if out == nil {
out = map[string]string{}
}
maps.Copy(out, over)
return out
}
// sayTaken says take-in.taken under its duplicate id; nil SayTakeIn says nothing.
func (ix *Indexer) sayTaken(ctx context.Context, t meshcatalog.TakeInTaken) error {
if ix.SayTakeIn == nil {
return nil
}
if err := ix.SayTakeIn(ctx, takein.TakeInTakenEvent, TakenID(t.Branch, t.Source), t); err != nil {
return fmt.Errorf("take-in.taken of %s: %w", BranchObject(t.Branch, t.Source), err)
}
return nil
}
// PendingID is the duplicate id take-in.pending of a branch's source revision is said under: said once per source.
func PendingID(branch string, source int64) string {
return takein.TakeInPendingEvent + "." + keys.EscapeName(branch) + "." + strconv.FormatInt(source, 10)
}
// SourceToTake is the source-moved a read of the gate's source revision of a branch, through the seat mesh-delivery's verb
// source, takes in as a heard one of that revision would be (design 47 H H-A-129), and false when the view keeps a revision of
// the branch at or above it, so a revision is never taken in twice. It is the branch's first take-in, start true, exactly
// when the view keeps no revision of the branch; otherwise its movers are judged against the newest kept (H-A-130). Every
// repository the answer names is a head: the movers are the fingerprint changes alone.
func (ix *Indexer) SourceToTake(ctx context.Context, branch string, a meshdelivery.SourceAnswer) (meshdelivery.SourceMovedEvent, bool, error) {
e := meshdelivery.SourceMovedEvent{V: 1, Branch: branch, Source: a.Revision, Call: a.Call,
Heads: make([]meshdelivery.SourceMovedEventHeadsItem, 0, len(a.Repositories))}
if a.Revision <= 0 {
return e, false, nil
}
if ix.Revisions == nil {
return e, false, errors.New("the indexer keeps no catalog revisions")
}
names, err := ix.Revisions.Names(ctx)
if err != nil {
return e, false, fmt.Errorf("the objects %s: %w", CatalogRevisionsObjects, err)
}
var kept int64
for _, n := range names {
if r, ok := BranchRevisionOf(branch, n); ok && r > kept {
kept = r
}
}
if kept >= a.Revision {
return e, false, nil
}
e.Start = kept == 0
for _, r := range a.Repositories {
e.Heads = append(e.Heads, meshdelivery.SourceMovedEventHeadsItem{Repository: r.Repository, Commit: r.Head})
}
return e, true, nil
}