Files
mesh-delivery/modules/mesh-delivery-indexer/internal/index/held.go
T
jschoubben 8bd66112f0 I2: a take-in says what it waits on
A source-moved take-in waiting over a minute on the seat git writes one
journal line and its waiting-on (part, verb, since) in the state takeins
under running.<source revision>, removed at the take-in's end; past five
minutes it raises take-in-waiting (scope merge), cleared when the wait
ends. indexer_pending answers the running take-ins beside the held ones.
The wait is measured by a context's bound, no timer (ADR 0437 §8).

Builds: design 47 H2 §803, H-A-409
2026-11-22 01:02:11 +01:00

325 lines
14 KiB
Go

package index
import (
"context"
"encoding/json"
"errors"
"fmt"
"slices"
"strings"
"time"
"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"
stdio "git.novox.be/novox/mesh-sdk/go"
"git.novox.be/novox/mesh-sdk/go/keys"
"git.novox.be/novox/mesh-sdk/go/process"
)
// HeldState is the indexer's state held (design 47 H §744, H-A-97, H-A-118): one key per watched branch, the branch escaped as
// a key's name is, holding the branch's held source-moved events in order, oldest first, each written when it is held (its
// message acknowledged once written) and taken out at its take-in or skip; the key goes with its last. indexer_pending reads it.
const HeldState = "held"
// The held take-in's tools (§744).
const (
PendingTool = "indexer_pending"
TakeInPendingTool = "indexer_take_in_pending"
TakeInSkipTool = "indexer_take_in_skip"
)
// HeldTakeIn is one held source-moved of the state held: its branch, source revision, why it is held and since when (H-A-97),
// and the event's call, heads and start, so a take-in after a restart reads nothing of any verb (H-A-118).
type HeldTakeIn struct {
Branch string `json:"branch"`
Source int64 `json:"source"`
Why string `json:"why"`
Since time.Time `json:"since"`
Call string `json:"call"`
Heads []meshdelivery.SourceMovedEventHeadsItem `json:"heads"`
Start bool `json:"start,omitempty"`
}
// Event is the held source-moved, as the gate said it.
func (h HeldTakeIn) Event() meshdelivery.SourceMovedEvent {
return meshdelivery.SourceMovedEvent{V: keys.Version, Branch: h.Branch, Call: h.Call, Heads: h.Heads, Source: h.Source, Start: h.Start}
}
// PendingAnswer is indexer_pending's answer (H-A-102): every branch's held take-ins, in the state held's form (since in RFC 3339).
type PendingAnswer struct {
Held []HeldTakeIn `json:"held"`
// Running are the take-ins that run, each with what it waits on (design 47 H2 §803, H-A-409).
Running []RunningTakeIn `json:"running,omitempty"`
}
// ErrHeldUnwritten says a source-moved that could not be taken in is not written into the state held either: its message
// stays unacknowledged, so the bus gives it again.
var ErrHeldUnwritten = errors.New("the held take-in is not written into the state held")
// decodeHeld reads a key of the state held: the branch's held events in order.
func decodeHeld(v []byte) ([]HeldTakeIn, error) {
var hs []HeldTakeIn
if err := json.Unmarshal(v, &hs); err != nil {
return nil, fmt.Errorf("not a value of the state %s: %w", HeldState, err)
}
return hs, nil
}
// HeldOf is the branch's held events in order; none when it holds none.
func (ix *Indexer) HeldOf(ctx context.Context, branch string) ([]HeldTakeIn, error) {
if ix.Held == nil {
return nil, nil
}
v, _, found, err := ix.Held.Get(ctx, keys.EscapeName(branch))
if err != nil || !found {
return nil, err
}
return decodeHeld(v)
}
// hold writes e into its branch's held events, after those held before it, and says take-in.pending once per source: a source
// already held is neither written nor said again, so its since stays. ErrHeldUnwritten when it is not written.
func (ix *Indexer) hold(ctx context.Context, e meshdelivery.SourceMovedEvent, why string) error {
if ix.Held == nil {
return fmt.Errorf("%w: the indexer keeps no state %s", ErrHeldUnwritten, HeldState)
}
key, added := keys.EscapeName(e.Branch), false
err := write(ctx, ix.Held, key, func(v []byte, found bool) ([]byte, bool, error) {
var hs []HeldTakeIn
if found {
var err error
if hs, err = decodeHeld(v); err != nil {
return nil, false, err
}
}
for _, h := range hs {
if h.Source == e.Source {
added = false
return nil, false, nil
}
}
added = true
hs = append(hs, HeldTakeIn{Branch: e.Branch, Source: e.Source, Why: why, Since: ix.now().UTC(), Call: e.Call, Heads: e.Heads, Start: e.Start})
data, err := json.Marshal(hs)
return data, false, err
})
if err != nil {
ix.logf("take-in: %s.%s is not written: %v", HeldState, key, err)
return fmt.Errorf("%w: %v", ErrHeldUnwritten, err)
}
if !added {
return nil
}
ix.logf("take-in: source revision %d of %s is held: %s", e.Source, e.Branch, why)
if ix.SayTakeIn == nil {
return nil
}
p := meshcatalog.TakeInPending{Branch: e.Branch, Source: e.Source, Why: why}
if err := ix.SayTakeIn(ctx, takein.TakeInPendingEvent, PendingID(e.Branch, e.Source), p); err != nil {
ix.logf("take-in: take-in.pending of %s is not said: %v", BranchObject(e.Branch, e.Source), err)
}
return nil
}
// HoldBehind writes e behind its branch's earlier held events when the branch holds any, so the branch's source revisions are
// taken in in their order (H-A-118); it answers whether it did. ErrHeldUnwritten when it is not written.
func (ix *Indexer) HoldBehind(ctx context.Context, e meshdelivery.SourceMovedEvent) (bool, error) {
hs, err := ix.HeldOf(ctx, e.Branch)
if errors.Is(err, process.ErrNotYet) {
return false, nil // a state held not there yet holds nothing to take in first
}
if err != nil {
return false, fmt.Errorf("%w: %v", ErrHeldUnwritten, err)
}
if len(hs) == 0 {
return false, nil
}
return true, ix.hold(ctx, e, fmt.Sprintf("held behind source revision %d of %s, taken in first", hs[0].Source, e.Branch))
}
// unhold takes the branch's held source revision out of the state held, once it is taken in or skipped; the key goes with its last.
func (ix *Indexer) unhold(ctx context.Context, branch string, source int64) {
if ix.Held == nil {
return
}
key := keys.EscapeName(branch)
err := write(ctx, ix.Held, key, func(v []byte, found bool) ([]byte, bool, error) {
if !found {
return nil, false, nil
}
hs, err := decodeHeld(v)
if err != nil {
return nil, false, err
}
left := slices.DeleteFunc(slices.Clone(hs), func(h HeldTakeIn) bool { return h.Source == source })
if len(left) == len(hs) {
return nil, false, nil
}
if len(left) == 0 {
return nil, true, nil
}
data, err := json.Marshal(left)
return data, false, err
})
if err != nil {
ix.logf("take-in: source revision %d is not taken out of %s.%s: %v", source, HeldState, key, err)
}
}
// TakeHeldIn takes every branch's held source revisions in, each branch in its order, on the indexer's own room and at its
// start (H-A-114, H-A-118): a branch stops at the first that still cannot be taken in, which stays held with those behind it.
func (ix *Indexer) TakeHeldIn(ctx context.Context) {
all, err := ReadHeld(ctx, ix.Held)
if err != nil {
ix.logf("take-in: the state %s is not read, so no held take-in is taken in again now: %v", HeldState, err)
return
}
stopped := map[string]bool{}
for _, h := range all {
if stopped[h.Branch] {
continue
}
if _, err := ix.TakeInSource(ctx, h.Event()); err != nil {
ix.logf("take-in: source revision %d of %s is still held: %v", h.Source, h.Branch, err)
stopped[h.Branch] = true
}
}
}
// SkipTakeIn ends a held source revision unwritten by hand (indexer_take_in_skip, §744): it says take-in.taken with no entries,
// skipped, by and why, and deletes the branch's held take-in; the caller acknowledges the source-moved.
func (ix *Indexer) SkipTakeIn(ctx context.Context, e meshdelivery.SourceMovedEvent, by, why string) error {
if err := ix.sayTaken(ctx, meshcatalog.TakeInTaken{Branch: e.Branch, Source: e.Source, Call: e.Call, Skipped: true, By: by, Why: why}); err != nil {
return err
}
ix.unhold(ctx, e.Branch, e.Source)
ix.logf("take-in: source revision %d of %s skipped by %s: %s", e.Source, e.Branch, by, why)
return nil
}
// ReadHeld is every branch's held take-ins, by branch, each branch's in its order.
func ReadHeld(ctx context.Context, st Store) ([]HeldTakeIn, error) {
out := []HeldTakeIn{}
if st == nil {
return out, nil
}
ks, err := st.Keys(ctx)
if err != nil {
return nil, err
}
slices.Sort(ks)
for _, k := range ks {
v, _, found, err := st.Get(ctx, k)
if err != nil {
return nil, err
}
if !found {
continue
}
hs, err := decodeHeld(v)
if err != nil {
return nil, fmt.Errorf("%s.%s: %w", HeldState, k, err)
}
out = append(out, hs...)
}
return out, nil
}
// HeldTools are the held take-in's tools (§744): indexer_pending reads the state held and the running take-ins' waiting-on in
// the state takeins (H2 §803, H-A-409); indexer_take_in_pending takes a branch's
// held source-moved again now and indexer_take_in_skip acknowledges it unwritten, each an ask in the state takeins the indexer
// acts on (H-A-97), the skip a hand act recorded through mesh-hand-acts before it is asked.
func HeldTools(held, takeIns Store, ask func(key string, value []byte) (TakeInAsk, error), now func() time.Time, record func(what, why string) error) []stdio.Tool {
str := func(args map[string]any, k string) string { s, _ := args[k].(string); return strings.TrimSpace(s) }
return []stdio.Tool{
{Name: PendingTool,
Description: "Each watched branch's held take-in (design 47 H §744): the source revision of the gate's source-moved the indexer " +
"could not take in (the catalog store full, a read of the forge unanswered), why and since when, in order, each kept here once " +
"acknowledged and taken in again on the indexer's room and at its start; and each running take-in that waits over a minute on another part, its waiting-on (part, verb, since). indexer_take_in_pending takes it again now, indexer_take_in_skip ends it unwritten.",
Input: map[string]any{"type": "object", "properties": map[string]any{}},
Run: func(map[string]any) (any, error) {
h, err := ReadHeld(context.Background(), held)
if err != nil {
return PendingAnswer{Held: h}, err
}
r, err := ReadRunning(context.Background(), takeIns)
return PendingAnswer{Held: h, Running: r}, err
}},
{Name: TakeInPendingTool,
Description: "Take a watched branch's held source-moved in again now (design 47 H §744), as the indexer's room would: " +
"answers whether it was taken in. indexer_pending names the held branches.",
Input: map[string]any{"type": "object", "required": []string{"branch"}, "properties": map[string]any{
"branch": map[string]any{"type": "string", "description": "the watched branch, as indexer_pending names it"},
"why": map[string]any{"type": "string", "description": "why it is taken again now"}}},
Run: func(args map[string]any) (any, error) {
branch := str(args, "branch")
if branch == "" {
return nil, errors.New("name the held branch (branch), as indexer_pending names it")
}
data, err := json.Marshal(TakeInAsk{V: 1, Held: branch, Why: str(args, "why"), By: stdio.CallerOf(args), At: now().UTC()})
if err != nil {
return nil, err
}
a, err := ask("held."+keys.EscapeName(branch)+".again", data)
if err != nil {
return nil, err
}
return a.Result, nil
}},
{Name: TakeInAgainTool,
Description: "Take a repository in again now at the merge commit the indexer keeps of it (design 47 H-A-208, H-A-210): its " +
"module graph is read again (each module's requirements, transitively) and the modules state recomputed as a " +
"package's publish re-pins it, so a changed take-in rule reaches a repository with no merge since; pins and " +
"fingerprints move, no catalog revision is kept and the gate is not asked. An unknown repository is refused.",
Input: map[string]any{"type": "object", "required": []string{"repository"}, "properties": map[string]any{
"repository": map[string]any{"type": "string", "description": "the repository as owner/name, such as novox/mesh-host"},
"why": map[string]any{"type": "string", "description": "why it is taken in again now"}}},
Run: func(args map[string]any) (any, error) {
repository := str(args, "repository")
if repository == "" {
return nil, errors.New("name the repository (repository) as owner/name")
}
data, err := json.Marshal(TakeInAsk{V: 1, Again: repository, Why: str(args, "why"), By: stdio.CallerOf(args), At: now().UTC()})
if err != nil {
return nil, err
}
a, err := ask("again."+keys.EscapeName(repository), data)
if err != nil {
return nil, err
}
if a.Result != nil && !a.Result.Stored {
return a.Result, fmt.Errorf("%s is not taken in again: %s", repository, a.Result.Answer)
}
return a.Result, nil
}},
{Name: TakeInSkipTool,
Description: "End a watched branch's held source-moved unwritten (design 47 H §744): it is acknowledged, take-in.taken is said " +
"with no entries and skipped, and the hand act is recorded through mesh-hand-acts before it is asked. indexer_pending " +
"names the held branch and source.",
Input: map[string]any{"type": "object", "required": []string{"branch", "source", "why"}, "properties": map[string]any{
"branch": map[string]any{"type": "string", "description": "the watched branch, as indexer_pending names it"},
"source": map[string]any{"type": "integer", "description": "the held source revision"},
"why": map[string]any{"type": "string", "description": "why it is skipped, in plain words"}}},
Run: func(args map[string]any) (any, error) {
branch, why := str(args, "branch"), str(args, "why")
source, _ := args["source"].(float64)
if branch == "" || source <= 0 || why == "" {
return nil, errors.New("name the held branch (branch) and source revision (source) and say why (why): a skip is a hand act, recorded through mesh-hand-acts")
}
data, err := json.Marshal(TakeInAsk{V: 1, Held: branch, HeldSource: int64(source), Skip: true, Why: why, By: stdio.CallerOf(args), At: now().UTC()})
if err != nil {
return nil, err
}
if err := record(fmt.Sprintf("skipped the held source revision %d of %s unwritten by hand", int64(source), branch), why); err != nil {
return nil, fmt.Errorf("the hand act was not recorded through mesh-hand-acts, so nothing was skipped: %w", err)
}
a, err := ask(fmt.Sprintf("held.%s.skip.%d", keys.EscapeName(branch), int64(source)), data)
if err != nil {
return nil, err
}
return a.Result, nil
}},
}
}