viewstate.ViewModule is gone; the process takes cred.Module for its reader and emitter, the tools the served-as module. Builds: design 47 G A-296, §428 Claude-Session: https://claude.ai/code/session_01NcbgfGKaxw984XyAPfLm4h
539 lines
21 KiB
Go
539 lines
21 KiB
Go
// Command mesh-delivery-view is the operator's view of delivery (novox/hq ADR 0429 §3): it watches the have, the seat's assignments
|
|
// and builds buckets on its own bus credential, derives one row per module and node in memory, and serves them on a
|
|
// local socket (for mesh-delivery-cli) and on a local web page. It starts, stops or retries nothing; the one thing it writes is the operator's answer to an ask that needs no proof of who
|
|
// answered, under `<ask key>.view` in its bucket answers (ADR 0528 clause 6).
|
|
package main
|
|
|
|
import (
|
|
"context"
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
mma "git.novox.be/novox/mesh-contracts/go/seats/meshmoduleassigner"
|
|
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshcatalogclient"
|
|
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshwants"
|
|
"io"
|
|
"log"
|
|
"maps"
|
|
"mesh-delivery-view/internal/view"
|
|
"net/http"
|
|
"os"
|
|
"os/signal"
|
|
"slices"
|
|
"strings"
|
|
"sync"
|
|
"syscall"
|
|
"time"
|
|
|
|
"git.novox.be/novox/mesh-contracts/go/seats/meshbroker"
|
|
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-builder/builds"
|
|
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog"
|
|
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/trunk"
|
|
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-view/viewstate"
|
|
"git.novox.be/novox/mesh-sdk/go/process"
|
|
"git.novox.be/novox/mesh-sdk/go/views"
|
|
"github.com/nats-io/nats.go"
|
|
"github.com/nats-io/nats.go/jetstream"
|
|
)
|
|
|
|
const (
|
|
defaultSocket = "/var/lib/mesh-delivery-view-cli/cli.sock"
|
|
defaultWeb = "127.0.0.1:8471"
|
|
)
|
|
|
|
func main() {
|
|
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
|
|
defer stop()
|
|
if err := run(ctx, os.Getenv, os.Stderr); err != nil {
|
|
fmt.Fprintln(os.Stderr, "mesh-delivery-view:", err)
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
|
|
// run is the program: it returns when ctx ends or a listener cannot be made.
|
|
func run(ctx context.Context, getenv func(string) string, logOut io.Writer) error {
|
|
logger := log.New(logOut, "mesh-delivery-view: ", log.LstdFlags)
|
|
cred, err := process.ReadBrokerAccount(getenv)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
socket := strings.TrimSpace(getenv("MESH_DELIVERY_SOCKET"))
|
|
if socket == "" {
|
|
socket = defaultSocket
|
|
}
|
|
web := strings.TrimSpace(getenv("MESH_DELIVERY_WEB"))
|
|
if web == "" {
|
|
web = defaultWeb
|
|
}
|
|
|
|
model := view.New(cred.Module)
|
|
own := viewstate.BucketsOf(cred.Module) // its own buckets, named from its credential's module (A-296)
|
|
l, err := view.ListenSocket(socket)
|
|
if err != nil {
|
|
return fmt.Errorf("cannot listen on %s: %w", socket, err)
|
|
}
|
|
srv := &http.Server{Addr: web, Handler: view.Web(model), ReadHeaderTimeout: 10 * time.Second}
|
|
webUp := make(chan error, 1)
|
|
go func() { webUp <- srv.ListenAndServe() }()
|
|
go view.ServeSocket(ctx, l, model)
|
|
|
|
conn, js, err := cred.Dial()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer conn.Close()
|
|
model.SetWriter(kvWriter{ctx: ctx, js: js, bucket: own.Answers})
|
|
model.SetMotionEvent(func(e view.MotionEvent) error {
|
|
err := emitEvent(ctx, js, cred, e.Type, e.Body, e.ID)
|
|
if err != nil {
|
|
logger.Printf("the %s of %s on %s could not be said (said again at its next change): %v", e.Type, e.Body.Module, e.Body.Node, err)
|
|
}
|
|
return err
|
|
})
|
|
model.SetDeliveredEvent(func(e view.DeliveredEvent) error {
|
|
id := fmt.Sprintf("delivered.%d", e.Revision)
|
|
if e.Repository != "" { // a repository's reached head (ADR 0556 clause 3)
|
|
key, _ := trunk.RepositoryKey(e.Repository)
|
|
id = fmt.Sprintf("reached.%s.%d.%d", key, e.Revision, len(e.NotYet))
|
|
if len(e.NotYet) > 0 {
|
|
sum := sha256.Sum256([]byte(strings.Join(e.NotYet, ",")))
|
|
id += "." + hex.EncodeToString(sum[:6])
|
|
}
|
|
}
|
|
err := emitEvent(ctx, js, cred, DeliveredEventType, e, id)
|
|
if err != nil {
|
|
logger.Printf("the delivery of source revision %d could not be said (said again at the next change the view takes in): %v", e.Revision, err)
|
|
}
|
|
return err
|
|
})
|
|
model.SetReachWriter(func(w view.ReachWrite) error {
|
|
rev, err := putReach(ctx, js, own.Reach, w)
|
|
if err != nil {
|
|
logger.Printf("the reach of %s could not be written (written again at the next change the view takes in): %v", w.Key, err)
|
|
return err
|
|
}
|
|
// What the view wrote it says (ADR 0596 §2): a reader hears the reach change and asks view_reach, never the bucket.
|
|
err = emitEvent(ctx, js, cred, ReachedEventType, ReachedEvent{Key: w.Key, Reach: w.Value}, fmt.Sprintf("reached.%s.%d", w.Key, rev))
|
|
if err != nil {
|
|
logger.Printf("the reach of %s is written and could not be said (said again at the next change the view takes in): %v", w.Key, err)
|
|
}
|
|
return err
|
|
})
|
|
model.SetBacklog(view.BacklogOver(js, 5*time.Second))
|
|
// The indexer's and the assigner's states are their owners': read by the first view of the owner's read tool and then
|
|
// its views family, never their buckets (ADR 0615 §1, DLV-M4). An owner that is down never holds the start: a watch of
|
|
// its picture waits for the first view on its own goroutine, and until then the unanswered first view is the view's
|
|
// finding (ADR 0615 §6).
|
|
calls, err := process.Connect(cred, nats.Name(cred.Node+"."+cred.Module+".calls"), nats.RetryOnFailedConnect(true))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer calls.Close()
|
|
// The module's one pull of its durable consumer: the reader's views events and the waits' events are handlers on it, and it is
|
|
// started once, after both (design 47 G §512, A-242).
|
|
hub := process.NewEventHub(logger.Printf)
|
|
defer hub.Stop()
|
|
reader := &views.ProcessReader{Module: cred.Module, Call: calls.Call, Logf: logger.Printf, Hub: hub}
|
|
// The view's finding stream is its `finding-events` entry, asked of the seat mesh-broker's show (design 47 G A-274,
|
|
// §529, §577): the writer says its events under that binding's subjects.
|
|
binding := process.NewBinding(meshbroker.Seat, cred.Module, calls.CallKept, logger.Printf, views.FindingEventsLocal)
|
|
if err := binding.Register(hub); err != nil {
|
|
return err
|
|
}
|
|
binding.Start()
|
|
writer, err := findingWriter(ctx, cred, conn, js, calls.State("findings"), binding, logger.Printf)
|
|
if err != nil {
|
|
return fmt.Errorf("the view's finding writer: %w", err)
|
|
}
|
|
defer writer.Close()
|
|
reader.Findings = func() views.WireWriter { return writer }
|
|
sayErrs := make(chan error, 1)
|
|
sayAnswers(ctx, js, conn, cred, writer, sayErrs, logger.Printf)
|
|
for _, o := range ownersFollowed {
|
|
if _, err := reader.Follow(o.owner, o.state, o.tool); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
// The modules are the catalog half's, per watched branch, through meshcatalogclient's branch follower adopted on the
|
|
// reader (design 47 G §532, §614): the registrations' branches, before the first registrations the gate's source's;
|
|
// each branch's first view, its status, its hand asks and its event catalog-revision are the reader's. A module's entry
|
|
// is the one Branches.Entry reads, the first branch of its own registration (A-103).
|
|
branches, err := meshcatalogclient.FollowBranchesProcess(reader, calls.Call)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
branches.Logf = logger.Printf
|
|
// The seats mesh-module-assigner's assignments and mesh-wants' wants and asks, each followed at the holder the
|
|
// seat-holders view of the catalog half's claimant names, and as it moves (design 47 G §717, A-298, A-300): each a picture
|
|
// in the seat form, its first view the holder's tool named for the state; no code names their holder.
|
|
holders := branches.SeatHolders(ctx)
|
|
for _, s := range []struct{ seat, state string }{{mma.Seat, view.AssignmentsState}, {meshwants.Seat, meshwants.StateWants}, {meshwants.Seat, meshwants.StateAsks}} {
|
|
if _, err := reader.FollowSeat(s.seat, s.state, holders); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
// The watchman's stuck keys name the wants' real bucket at their holder (A-303 (corrected)).
|
|
model.SetWantsHolder(func() string { return reader.HolderBucket(meshwants.Seat, meshwants.StateWants) })
|
|
open := buckets{js: js, reader: reader}
|
|
for _, bucket := range []string{view.HaveBucket, view.AssignmentsBucket, view.PresenceBucket, view.StuckBucket, view.CandidatesBucket, view.AnswersBucket, builds.BuildsBucket(builds.BuilderModule), view.WantsBucket, view.AsksBucket, own.Answers, view.TakenBucket, own.Reach} {
|
|
go follow(ctx, open, bucket, false, model, logger)
|
|
}
|
|
// The view's own state reach, as it stood at the start, is what the derivation lays its history over (design 47 G A-225);
|
|
// the modules' history is each revision of the catalog half the view takes, in its order (below), and each watched
|
|
// branch's source is the seat mesh-delivery's verb source per branch, followed by its source-moved (A-173).
|
|
statusBucket := process.AwaitBucket(ctx, js, conn, cred.Module+"_"+views.StatusState, "view_first_views reads nothing and view_first_view_again is not heard", logger.Printf)
|
|
sources := &branchSources{ctx: ctx, reader: reader, call: calls.Call, model: model, logger: logger}
|
|
modules := &catalogModules{ctx: ctx, branches: branches, sources: sources, model: model, logger: logger, applied: map[string]bool{}}
|
|
branches.Changed = modules.changed
|
|
reader.Start(ctx, js, cred, statusBucket)
|
|
if err := hearWaits(ctx, hub, js, own.Waits, logger.Printf); err != nil {
|
|
return fmt.Errorf("the waits' events are not handled: %w", err)
|
|
}
|
|
if err := hub.StartUntilBound(ctx, js, cred, nil, nil); err != nil {
|
|
logger.Printf("the consumer is not heard yet (tried again until it is): %v", err)
|
|
}
|
|
go func() {
|
|
select {
|
|
case <-branches.Registrations.Follower.Ready():
|
|
modules.settle()
|
|
case <-ctx.Done():
|
|
}
|
|
}()
|
|
var said error
|
|
select {
|
|
case <-ctx.Done():
|
|
case said = <-sayErrs:
|
|
case err := <-webUp:
|
|
if !errors.Is(err, http.ErrServerClosed) {
|
|
return fmt.Errorf("the web view stopped: %w", err)
|
|
}
|
|
}
|
|
shutdown, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
|
defer cancel()
|
|
srv.Shutdown(shutdown)
|
|
return said
|
|
}
|
|
|
|
// sayAnswers says each change of the view's own state answers on its views family (`views.answers`), from the revision its
|
|
// marks state view-marks says it said up to, as every owner says its states (design 47 G A-175, A-178): the teller follows it
|
|
// by its first-view tool answers and that family, never by reading the state. A watch that ends is sent on errs, and the program ends
|
|
// with it, loudly.
|
|
func sayAnswers(ctx context.Context, js jetstream.JetStream, nc process.Subscriber, cred process.Account, writer views.WireWriter, errs chan<- error, logf func(string, ...any)) {
|
|
marks := cred.Module + "_" + views.MarksState
|
|
process.AwaitBucket(ctx, js, nc, marks, "the view's answers are not said on its views family, so the teller waits on its first view", logf).Ready(func(mk jetstream.KeyValue) {
|
|
em := views.Emitter{Owner: cred.Module, Subject: cred.EventSubject, Publish: views.JetStreamPublish(js), Marks: mk, Findings: writer, Logf: logf}
|
|
process.AwaitBucket(ctx, js, nc, viewstate.BucketsOf(cred.Module).Answers, "the view's answers are not said on its views family", logf).Ready(func(kv jetstream.KeyValue) {
|
|
if err := em.Start(ctx, viewstate.ViewAnswersState, kv, errs); err != nil {
|
|
logf("%v: the teller asks the first-view tool answers at each change until the view starts again", err)
|
|
}
|
|
})
|
|
})
|
|
}
|
|
|
|
// kvWriter is the view's one write: a key of its own bucket answers (manifest state entry answers), which the teller reads.
|
|
// The bucket is the controller's to create; a bucket not there yet is an error the operator is told.
|
|
type kvWriter struct {
|
|
ctx context.Context
|
|
js jetstream.JetStream
|
|
bucket string
|
|
}
|
|
|
|
func (w kvWriter) Put(key string, value []byte) error {
|
|
kv, err := w.js.KeyValue(w.ctx, w.bucket)
|
|
if err != nil {
|
|
return fmt.Errorf("the bucket %s: %w", w.bucket, err)
|
|
}
|
|
_, err = kv.Put(w.ctx, key, value)
|
|
return err
|
|
}
|
|
|
|
// Delete removes a key of the view's own answers: the answer to an ask that left asks, which only the view deletes (design 47 G
|
|
// §717).
|
|
func (w kvWriter) Delete(key string) error {
|
|
kv, err := w.js.KeyValue(w.ctx, w.bucket)
|
|
if err != nil {
|
|
return fmt.Errorf("the bucket %s: %w", w.bucket, err)
|
|
}
|
|
return kv.Delete(w.ctx, key)
|
|
}
|
|
|
|
// putReach writes a reach value under its key in the view's own state reach (ADR 0556 clause 2), its only writer.
|
|
func putReach(ctx context.Context, js jetstream.JetStream, bucket string, w view.ReachWrite) (uint64, error) {
|
|
kv, err := js.KeyValue(ctx, bucket)
|
|
if err != nil {
|
|
return 0, fmt.Errorf("the bucket %s: %w", bucket, err)
|
|
}
|
|
return kv.Put(ctx, w.Key, w.Value)
|
|
}
|
|
|
|
// ReachedEventType is the event the view says after it writes a repository's reach: mesh.mod.mesh-delivery-view.event.reached
|
|
// (ADR 0596 §2). Its body is ReachedEvent.
|
|
const ReachedEventType = "reached"
|
|
|
|
// ReachedEvent is the body of the event reached: the key of the reach (the repository's) and the reach as the view wrote it.
|
|
type ReachedEvent struct {
|
|
Key string `json:"key"`
|
|
Reach json.RawMessage `json:"reach"`
|
|
}
|
|
|
|
// ownersFollowed are the indexer's, the assigner's and the teller's states the view's process follows by their first views, each by its
|
|
// owner's first-view or read tool (as the view's consumes or invokes names it) and views family (its events on the shared events stream); the
|
|
// view's tools ask the other six through the owners' read tools on each call.
|
|
var ownersFollowed = []struct{ owner, state, tool string }{
|
|
{view.IndexerModule, "candidates", view.IndexerModule + ".indexer_read_candidates"},
|
|
{view.IndexerModule, "taken", view.IndexerModule + ".indexer_read_taken"},
|
|
{view.TellerModule, view.AnswersState, view.TellerModule + ".teller_read_answers"},
|
|
}
|
|
|
|
// buckets opens what the view watches: an owner's state as its picture (firstview), any other bucket on the bus.
|
|
type buckets struct {
|
|
js jetstream.JetStream
|
|
reader *views.ProcessReader
|
|
}
|
|
|
|
func (b buckets) KeyValue(ctx context.Context, bucket string) (jetstream.KeyValue, error) {
|
|
if p := b.reader.Picture(bucket); p != nil {
|
|
return p, nil
|
|
}
|
|
if kv, err := b.reader.KeyValue(ctx, bucket); err == nil { // a seat's view, by its picture's name (§717)
|
|
return kv, nil
|
|
}
|
|
return b.js.KeyValue(ctx, bucket)
|
|
}
|
|
|
|
// follow watches a bucket with its last values first and then each change, for as long as ctx lives: a bucket that is
|
|
// not there yet, or a watch that ends, is tried again.
|
|
func follow(ctx context.Context, js process.BucketOpener, bucket string, history bool, model *view.Model, logger *log.Logger) {
|
|
for ctx.Err() == nil {
|
|
if err := watchOnce(ctx, js, bucket, history, model); err != nil && ctx.Err() == nil {
|
|
logger.Printf("cannot watch %s, trying again: %v", bucket, err)
|
|
}
|
|
select {
|
|
case <-ctx.Done():
|
|
case <-time.After(5 * time.Second):
|
|
}
|
|
}
|
|
}
|
|
|
|
func watchOnce(ctx context.Context, js process.BucketOpener, bucket string, history bool, model *view.Model) error {
|
|
kv, err := js.KeyValue(ctx, bucket)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
var opts []jetstream.WatchOpt
|
|
initial := bucket
|
|
if history {
|
|
opts, initial = append(opts, jetstream.IncludeHistory()), view.ReachHistory(bucket)
|
|
}
|
|
w, err := kv.WatchAll(ctx, opts...)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer w.Stop()
|
|
for {
|
|
select {
|
|
case <-ctx.Done():
|
|
return nil
|
|
case e, ok := <-w.Updates():
|
|
if !ok {
|
|
return errors.New("the watch ended")
|
|
}
|
|
if e == nil { // the last values have all been delivered
|
|
model.Initial(initial)
|
|
continue
|
|
}
|
|
model.Apply(view.Entry{Bucket: bucket, Key: e.Key(), Value: e.Value(), At: e.Created(),
|
|
Deleted: e.Operation() != jetstream.KeyValuePut, Revision: e.Revision(), History: history})
|
|
}
|
|
}
|
|
}
|
|
|
|
// DeliveredEventType is the event the view says when a source revision is delivered: mesh.mod.mesh-delivery-view.event.delivered
|
|
// (issue 1906 D9, ADR 0539 §17). Its body is view.DeliveredEvent.
|
|
const DeliveredEventType = "delivered"
|
|
|
|
// emitEvent publishes an event of the view's own namespace, mesh.mod.<module>.event.<type>, with the SDK's envelope headers
|
|
// (as mesh-sdk's process.Conn.Emit does) and the id as the bus's own dedup id, so the same revision said twice in the stream's
|
|
// duplicate window is kept once.
|
|
func emitEvent(ctx context.Context, js jetstream.JetStream, cred process.Account, eventType string, body any, id string) error {
|
|
data, err := json.Marshal(body)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
msg := nats.NewMsg(cred.EventSubject(eventType))
|
|
msg.Header.Set("x-event-id", id)
|
|
msg.Header.Set("x-source", cred.Module)
|
|
msg.Header.Set("x-node", cred.Node)
|
|
msg.Header.Set("x-time", time.Now().UTC().Format(time.RFC3339Nano))
|
|
msg.Header.Set("content-type", "application/json")
|
|
msg.Data = data
|
|
pub, cancel := context.WithTimeout(ctx, 10*time.Second)
|
|
defer cancel()
|
|
_, err = js.PublishMsg(pub, msg, jetstream.WithMsgID(id))
|
|
return err
|
|
}
|
|
|
|
// catalogModules hands the model the catalog half's modules, one value per module: the entry Branches.Entry reads. Until
|
|
// every watched branch has its first view (Branches.Ready) it hands nothing; once they all have, it hands every module
|
|
// whole and tells the model the modules are read (Initial), then each module a branch's change names. A branch adopted
|
|
// later without its first view stops the handing until it has one, when every module is handed whole again.
|
|
type catalogModules struct {
|
|
ctx context.Context
|
|
branches *meshcatalogclient.Branches
|
|
sources *branchSources // the source of each branch followed (A-173)
|
|
model *view.Model
|
|
logger *log.Logger
|
|
|
|
mu sync.Mutex
|
|
held bool // every module was handed whole since the last time a branch had no first view
|
|
waiting bool // a settle waits on the branches' first views
|
|
applied map[string]bool // the modules the model holds
|
|
}
|
|
|
|
// changed is the branch follower's Changed: a module of one branch changed.
|
|
func (c *catalogModules) changed(_, module string, _ json.RawMessage, _ bool, at time.Time) {
|
|
c.follow()
|
|
c.mu.Lock()
|
|
held := c.held
|
|
c.mu.Unlock()
|
|
if !held {
|
|
c.settle()
|
|
return
|
|
}
|
|
e, ok, err := c.branches.Entry(module)
|
|
if err != nil {
|
|
c.mu.Lock()
|
|
c.held = false
|
|
c.mu.Unlock()
|
|
c.settle()
|
|
return
|
|
}
|
|
c.apply(module, e, ok, at)
|
|
}
|
|
|
|
// settle waits, on its own goroutine, for the first view of every followed branch, then hands every module whole. It
|
|
// waits on the views' Ready, never on a timer; with no branch followed it ends, and the next change starts it again.
|
|
func (c *catalogModules) settle() {
|
|
c.mu.Lock()
|
|
if c.waiting || c.held {
|
|
c.mu.Unlock()
|
|
return
|
|
}
|
|
c.waiting = true
|
|
c.mu.Unlock()
|
|
go func() {
|
|
defer func() {
|
|
c.mu.Lock()
|
|
c.waiting = false
|
|
c.mu.Unlock()
|
|
}()
|
|
for c.ctx.Err() == nil {
|
|
followed := c.branches.Followed()
|
|
if len(followed) == 0 {
|
|
return
|
|
}
|
|
for _, branch := range followed {
|
|
if cat, ok := c.branches.Catalog(branch); ok {
|
|
select {
|
|
case <-cat.Ready():
|
|
case <-c.ctx.Done():
|
|
return
|
|
}
|
|
}
|
|
}
|
|
if c.branches.Ready() == nil {
|
|
c.whole()
|
|
return
|
|
}
|
|
}
|
|
}()
|
|
}
|
|
|
|
// whole hands every module the followed branches list, removes from the model each it holds that none lists any more,
|
|
// and tells the model the modules are read.
|
|
func (c *catalogModules) whole() {
|
|
c.follow()
|
|
at := time.Now()
|
|
listed := map[string]bool{}
|
|
for _, branch := range c.branches.Followed() {
|
|
if cat, ok := c.branches.Catalog(branch); ok {
|
|
half, _ := cat.View()
|
|
for module := range half.Entries {
|
|
listed[module] = true
|
|
}
|
|
}
|
|
}
|
|
c.mu.Lock()
|
|
for module := range c.applied {
|
|
if !listed[module] {
|
|
listed[module] = false
|
|
}
|
|
}
|
|
c.mu.Unlock()
|
|
for _, module := range slices.Sorted(maps.Keys(listed)) {
|
|
e, ok, err := c.branches.Entry(module)
|
|
if err != nil {
|
|
c.logger.Printf("the catalog half is handed again at its next change: %v", err)
|
|
return
|
|
}
|
|
c.apply(module, e, ok, at)
|
|
}
|
|
c.mu.Lock()
|
|
c.held = true
|
|
c.mu.Unlock()
|
|
c.model.Initial(view.ModulesBucket)
|
|
c.model.Initial(view.ReachHistory(view.ModulesBucket))
|
|
}
|
|
|
|
// follow has the source of every followed branch followed, and no other (A-173).
|
|
func (c *catalogModules) follow() {
|
|
if c.sources != nil {
|
|
c.sources.ensure(c.branches.Followed())
|
|
}
|
|
}
|
|
|
|
// branchOf is the watched branch a module's entry is read from: the first branch of its registration, before the first
|
|
// registrations the first followed branch that lists it (design 47 G A-103, A-173); "" when none.
|
|
func (c *catalogModules) branchOf(module string) string {
|
|
if reg, ok := c.branches.Registrations.Registration(module); ok && len(reg.Branches) > 0 {
|
|
return reg.Branches[0]
|
|
}
|
|
for _, branch := range c.branches.Followed() {
|
|
if cat, ok := c.branches.Catalog(branch); ok {
|
|
if _, ok := cat.Entry(module); ok {
|
|
return branch
|
|
}
|
|
}
|
|
}
|
|
return ""
|
|
}
|
|
|
|
// apply hands the model one module's entry, or its removal when no branch lists it.
|
|
func (c *catalogModules) apply(module string, entry meshcatalog.CatalogEntry, listed bool, at time.Time) {
|
|
e := view.Entry{Bucket: view.ModulesBucket, Key: module, At: at, Deleted: !listed}
|
|
if listed {
|
|
e.Branch = c.branchOf(module)
|
|
}
|
|
if listed {
|
|
raw, _ := json.Marshal(entry)
|
|
v, err := view.ModuleValue(raw)
|
|
if err != nil {
|
|
c.logger.Printf("the catalog entry of %s is not taken: %v", module, err)
|
|
return
|
|
}
|
|
e.Value = v
|
|
}
|
|
c.mu.Lock()
|
|
if listed {
|
|
c.applied[module] = true
|
|
} else {
|
|
delete(c.applied, module)
|
|
}
|
|
c.mu.Unlock()
|
|
c.model.Apply(e)
|
|
e.History = true
|
|
c.model.Apply(e)
|
|
}
|