Files
mesh-delivery/modules/mesh-delivery-indexer/internal/index/takein_source.go
T
jschoubben 1b8f377cad I5: a take-in that cannot be written is held, said once and skippable
A source-moved the indexer cannot take in (the catalog store full, a read unanswered) stays
unacknowledged on its branch's consumer, so that branch's next waits while another branch's
go on; it is written in the new state held, one key per branch {branch, source, why, since}
(H-A-97), and said once per source as take-in.pending {branch, source, why}. A full store
sets heldFull, so RoomAgain says room once it has room, and the indexer's own room (H-A-50),
heard on its events consumer, takes every held one again, never a timer. indexer_pending
reads the state held; indexer_take_in_pending takes a branch's held one again now and
indexer_take_in_skip, with branch, source and why, acknowledges it unwritten, says
take-in.taken with no entries, skipped, by and why, and is recorded in the hand-act log;
both are asks in the state takeins. The held key goes at the take-in or the skip.

Builds: design 47 H §744; H-A-12, H-A-50, H-A-97

Claude-Session: https://claude.ai/code/session_01JG73KHUnBze48rdua8u2sy
2026-11-20 04:56:27 +01:00

142 lines
6.0 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, said once per source as take-in.pending with branch, source and why (§744):
// 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)
case !errors.Is(err, ErrOldForm):
ix.hold(ctx, e, err.Error())
}
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)
}