Files
mesh-delivery/modules/mesh-delivery-indexer/internal/index/taken.go
T
jschoubben 71a35a6376 MV1: move contracts and sdk under modules/, importers follow
The delivery repository's contracts and SDK live under modules/ as
mesh-delivery-contracts and mesh-delivery-sdk, their Go module paths
renamed to match the folders (the TypeScript names already did), and
every importer in mesh-delivery, mesh-catalog, mesh-host, mesh-tools
and mesh-sdk follows in the same group: a design as it should be, one
path per module, checked by the group's check building every importer.

Builds: design 47 G §427, §88

Claude-Session: https://claude.ai/code/session_01NcbgfGKaxw984XyAPfLm4h
2026-11-12 05:09:56 +01:00

142 lines
5.4 KiB
Go

package index
import (
"context"
"errors"
"fmt"
"maps"
"slices"
"time"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-indexer/takein"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-forge-bridge/forgemsg"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/trunk"
"git.novox.be/novox/mesh-sdk/go/process"
)
// What the indexer took in (novox/hq ADR 0556 clause 1, issue 1280): after a merged message is applied whole, the key of
// its repository in the state `taken` names that merge, so a reader of the modules knows they say the repository at that
// merge or later. The indexer is the key's only writer. A merge heard again after a later one (a redelivery) does not put
// the key back: the key moves only to a merge that contains the one it names, as the forge says it; a forge that cannot
// say keeps the key as it is and says so.
// writeTaken writes the repository's taken key for the merge f carries, unless it names that merge or a later one.
func (ix *Indexer) writeTaken(ctx context.Context, f forgemsg.Forge) error {
return ix.writeTakenKnown(ctx, f, "")
}
// writeTakenKnown is writeTaken where the merge is known not to be older than notOlder (lateMerge asked it, issue 1126): a
// key naming that merge is written over without asking the forge again.
func (ix *Indexer) writeTakenKnown(ctx context.Context, f forgemsg.Forge, notOlder string) error {
if ix.Taken == nil || f.Merge == "" {
return nil
}
key, err := trunk.RepositoryKey(f.Repository)
if err != nil {
ix.logf("%s at %s: no taken key is written: %v", f.Repository, f.Merge, err)
return nil
}
err = write(ctx, ix.Taken, key, func(value []byte, found bool) ([]byte, bool, error) {
if found {
if cur, err := takein.DecodeTaken(value); err == nil {
if cur.Merge == f.Merge {
return nil, false, nil
}
if ix.Contains != nil && cur.Merge != notOlder {
later, err := ix.Contains(ctx, f.Repository, f.Merge, cur.Merge)
if err != nil {
ix.logf("%s: whether %s is in %s cannot be read, so taken.%s stays at %s: %v", f.Repository, f.Merge, cur.Merge, key, cur.Merge, err)
return nil, false, nil
}
if later {
return nil, false, nil // a merge heard again after a later one
}
}
}
}
now := time.Now
if ix.Now != nil {
now = ix.Now
}
data, err := takein.EncodeTaken(takein.Taken{Repository: trunk.RepositoryName(f.Repository), Merge: f.Merge, At: now().UTC()})
return data, false, err
})
if errors.Is(err, process.ErrNotYet) {
// The state is not on the bus yet (a delivery that adds it, bound when it appears as issue 1790 says): said, and
// the merge's modules are still taken in; FillTaken writes its key once the state appears.
ix.logf("%s at %s: its taken key is not written yet, FillTaken writes it when the state appears: %v", f.Repository, f.Merge, err)
ix.mu.Lock()
if ix.untaken == nil {
ix.untaken = map[string]string{}
}
ix.untaken[f.Repository] = f.Merge // the latest heard; FillTaken keeps a later key over an earlier merge
ix.mu.Unlock()
return nil
}
return err
}
// FillTaken writes the taken keys of the merges applied while the state was not on the bus (kept by writeTaken), through
// writeTaken as a live merge does (issue 1280), whenever the state is there. It never writes a merge the indexer has not
// applied whole (a key names a merge only after it was applied: ADR 0556 clause 1, and the view reads it as the upper bound
// of what is reached). The gate's source heads are not such merges: a drained forge stream does not say a merge was applied,
// since the forge bridge may not have written its merged message yet (issue 1126: mesh-catalog#535's merged message was
// still being retried by the bridge when the indexer started, and the source's head moved the key, so the view said the
// merge was on the machines while the indexer held mesh-vault at the merge before).
//
// It is idempotent: a key naming the merge or a later one is left. A state not on the bus yet writes nothing and says
// nothing (the fill runs again when it appears). The first error is returned after every repository was tried.
func (ix *Indexer) FillTaken(ctx context.Context) error {
if ix.Taken == nil {
return nil
}
if _, err := ix.Taken.Keys(ctx); errors.Is(err, process.ErrNotYet) {
return nil
}
ix.mu.Lock()
pending := maps.Clone(ix.untaken)
ix.mu.Unlock()
var first error
note := func(repository string, err error) {
if err != nil && first == nil {
first = fmt.Errorf("%s: %w", repository, err)
}
}
for _, repository := range slices.Sorted(maps.Keys(pending)) {
err := ix.writeTaken(ctx, forgemsg.Forge{Repository: repository, Merge: pending[repository]})
if err == nil {
ix.mu.Lock()
if ix.untaken[repository] == pending[repository] {
delete(ix.untaken, repository)
}
ix.mu.Unlock()
}
note(repository, err)
}
return first
}
// KickFill runs FillTaken in the background after a message was applied, one at a time; a kick that arrives while one runs
// makes it run once more.
func (ix *Indexer) KickFill(ctx context.Context) {
if ix.filling.Swap(true) {
ix.fillAgain.Store(true)
return
}
go func() {
for {
ix.fillAgain.Store(false)
if err := ix.FillTaken(ctx); err != nil {
ix.logf("filling the taken keys: %v", err)
}
if !ix.fillAgain.Load() {
ix.filling.Store(false)
if !ix.fillAgain.Load() || ix.filling.Swap(true) {
return
}
}
}
}()
}