Files
mesh-delivery/modules/mesh-delivery-indexer/internal/index/takein_source.go
T
jschoubben 9b3a728f9e Design 47 H1 (8): a catalog revision's call names its kind, one of three constants
The seat mesh-catalog's CatalogRevision and catalog-revision gain call-kind, an enumeration the seat's schema validates (take-in, release, take-in-again), with module and version for a release and repository for a take-in again, beside call, the words for a person (regenerated by seatgen, additive). The indexer's three writers set them: a take-in's from the gate's call, a re-pin's on a module's release, a take-in again's; no reader parses the words.

Builds: design 47 H H-A-213 (3), H-A-214, H-A-215
2026-11-20 23:43:33 +01:00

218 lines
9.3 KiB
Go

package index
import (
"context"
"encoding/json"
"errors"
"fmt"
"maps"
"slices"
"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,
// the branch's next, <branch>.<n> of source <source>, carrying the movers and pinned for the take-in's pipeline when it has one
// (keepBranchEntries, H-A-213); 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 of that source (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()
// A source revision kept already is found by the revision's field source, never by its number (H-A-213 (1)).
if kept, found, err := takenOfSource(ctx, ix.Revisions, e.Branch, e.Source); err != nil {
return taken, err
} else if found {
taken.Revision, taken.Entries = kept.Revision, kept.Movers
ix.logf("take-in: source revision %d of %s is kept already as %s: take-in.taken said again from its %d movers", e.Source, e.Branch, RevisionObject(kept), len(kept.Movers))
return taken, ix.sayTaken(ctx, taken)
}
_, 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)
}
number, err := ix.keepBranchEntries(ctx, e.Branch, e.Source, takeInCall(e.Call), entries, movers)
if 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 = number, 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, BranchObject(e.Branch, number), 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")
}
// The gate's source a branch is taken in at is its newest revision's field source, never the revision's number (H-A-213 (1)).
newest, found, err := newestOf(ctx, ix.Revisions, branch)
if err != nil {
return e, false, fmt.Errorf("the newest catalog revision of %s: %w", branch, err)
}
if found && newest.Source >= a.Revision {
return e, false, nil
}
e.Start = !found
for _, r := range a.Repositories {
e.Heads = append(e.Heads, meshdelivery.SourceMovedEventHeadsItem{Repository: r.Repository, Commit: r.Head})
}
return e, true, nil
}
// takenOfSource is the revision a take-in of source src kept on branch: of the branch's revisions whose field source is src,
// the lowest numbered (a re-pin or a take-in again keeps the same source on a later number, H-A-211, H-A-213). The revisions
// are read from the newest down while their source is not below src; none is false.
func takenOfSource(ctx context.Context, o Objects, branch string, src int64) (meshcatalog.CatalogRevision, bool, error) {
names, err := o.Names(ctx)
if err != nil {
return meshcatalog.CatalogRevision{}, false, fmt.Errorf("the objects %s: %w", CatalogRevisionsObjects, err)
}
var numbers []int64
for _, n := range names {
if r, ok := BranchRevisionOf(branch, n); ok {
numbers = append(numbers, r)
}
}
slices.Sort(numbers)
var kept meshcatalog.CatalogRevision
found := false
for i := len(numbers) - 1; i >= 0; i-- {
name := BranchObject(branch, numbers[i])
data, err := o.Get(ctx, name)
if errors.Is(err, ErrNoObject) {
continue // removed under the read by the pin judge
}
if err != nil {
return kept, false, fmt.Errorf("revision %s: %w", name, err)
}
var rev meshcatalog.CatalogRevision
if err := json.Unmarshal(data, &rev); err != nil {
return kept, false, fmt.Errorf("%s: %w", name, err)
}
if rev.Source < src {
break
}
if rev.Source == src {
kept, found = rev, true
}
}
return kept, found, nil
}