Files
mesh-delivery/modules/mesh-delivery-indexer/cmd/mesh-delivery-indexer/source.go
T
jschoubben 042800cb2a IX15: the indexer reads a branch's source revision through the seat mesh-delivery's verb source and its event source-moved
The catch-up of the heads the source names runs before a source-moved is read, once per branch; the gate's source bucket is no longer read.
internal/index compiles against the new contracts again (SourceByMainValue is gone); tests of earlier subtasks that
the broken compile hid are fixed (the validator's MAJOR.MINOR version in their manifests).

Builds: design 47 G §15, §115, §362

Claude-Session: https://claude.ai/code/session_01JG73KEq4zoDfM9WtYpcQgZ
2026-11-11 11:41:21 +01:00

102 lines
3.4 KiB
Go

package main
import (
"context"
"encoding/json"
"fmt"
"strings"
"sync"
"git.novox.be/novox/mesh-delivery/contracts/go/seats/meshdelivery"
"github.com/nats-io/nats.go/jetstream"
)
// sourceInvoke is the seat mesh-delivery's verb source as the manifest's invokes names it: a watched branch's source
// revision, asked of the seat and never read from the gate's state (design 47 G §15, §115; ADR 0596 §2).
const sourceInvoke = "seat:" + meshdelivery.Seat + "." + meshdelivery.VerbSource
// sourceVia asks the seat for one branch's source revision through call (process.Conn.CallKept, a read) and reads its
// declared answer; an answer that carries an error is that error.
func sourceVia(call toolCaller) func(branch string) (meshdelivery.SourceAnswer, error) {
return func(branch string) (meshdelivery.SourceAnswer, error) {
raw, err := call(sourceInvoke, meshdelivery.SourceInput{Branch: branch})
if err != nil {
return meshdelivery.SourceAnswer{}, err
}
var a meshdelivery.SourceAnswer
if err := json.Unmarshal(raw, &a); err != nil {
return a, fmt.Errorf("the answer of %s is not a source revision: %w", sourceInvoke, err)
}
if a.Error != "" {
return a, fmt.Errorf("%s: %s", sourceInvoke, a.Error)
}
return a, nil
}
}
// sourceFollow takes the seat's source revisions into the indexer: first the catch-up of the heads the source names, then
// each source-moved the seat says (design 47 G §362). A source-moved heard before the catch-up ended is applied after it,
// once per branch.
type sourceFollow struct {
read func(branch string) (meshdelivery.SourceAnswer, error)
apply func(context.Context, meshdelivery.SourceAnswer) error
logf func(string, ...any)
mu sync.Mutex
caught bool
waiting map[string]bool
}
// catchUp reads the trunk branch's source revision and applies it, then the branches a source-moved named meanwhile. A
// read that fails is said; the catch-up ends all the same, and the next source-moved reads again.
func (s *sourceFollow) catchUp(ctx context.Context) {
s.take(ctx, trunkBranch)
s.mu.Lock()
s.caught = true
branches := s.waiting
s.waiting = nil
s.mu.Unlock()
for branch := range branches {
s.take(ctx, branch)
}
}
// moved is the handler of one source-moved on branch: held until the catch-up ends, read at once after.
func (s *sourceFollow) moved(ctx context.Context, branch string) {
s.mu.Lock()
if !s.caught {
if s.waiting == nil {
s.waiting = map[string]bool{}
}
s.waiting[branch] = true
s.mu.Unlock()
return
}
s.mu.Unlock()
s.take(ctx, branch)
}
// take reads one branch's source revision and applies it. A branch other than the trunk's is left until the indexer keeps
// a catalog graph per watched branch.
func (s *sourceFollow) take(ctx context.Context, branch string) {
if branch != trunkBranch {
return
}
a, err := s.read(branch)
if err != nil {
s.logf("source.%s is not read, and is read again at its next source-moved: %v", branch, err)
return
}
if err := s.apply(ctx, a); err != nil {
s.logf("source.%s at revision %d could not be taken into the modules: %v", branch, a.Revision, err)
}
}
// heard is the hub's handler of source-moved: the branch is the last token of the subject. The message is acknowledged at once.
func (s *sourceFollow) heard(ctx context.Context, m jetstream.Msg) {
defer func() { _ = m.Ack() }()
if branch := m.Subject()[strings.LastIndex(m.Subject(), ".")+1:]; branch != "" {
s.moved(ctx, branch)
}
}