The builder's version-reserved writes the series' numbered version with the contents of the module's entry commit, so acceptedSeries and releasedPins pin it; the indexer keeps each reservation in heard (reserved.<revision>.<module>, reserved-keep) and gathers a revision's reservations until its pipeline's start, its first step-ended or the expiry of settling.<revision> (reserved-settle), then keeps one re-pin revision of that branch. The release's package-published moves no fingerprint; a pipeline-ended not passed withdraws its reservations to the newest released version. Builds: design 47 H2 §805; H-A-418, H-A-420, H-A-424, H-A-436
433 lines
16 KiB
Go
433 lines
16 KiB
Go
package index
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"maps"
|
|
"slices"
|
|
"strings"
|
|
"time"
|
|
|
|
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshbuilds"
|
|
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog"
|
|
"git.novox.be/novox/mesh-sdk/go/keys"
|
|
)
|
|
|
|
// A release re-pins once, at its reservation (design 47 H2 §805; H-A-418, H-A-420, H-A-424). The builder says
|
|
// version-reserved when it reserves a package's number, naming the revision `<branch>.<n>` its pipeline runs. The indexer
|
|
// writes the series' numbered version with the contents read at the commit of the module's entry in that revision, so
|
|
// acceptedSeries and releasedPins pin it, and keeps the reservation in its state heard under
|
|
// `reserved.<revision>.<module>`, each key living reserved-keep. It gathers a pipeline's reservations per revision and
|
|
// writes one re-pin revision of that revision's branch when the gathering settles: at the first pipeline-started or
|
|
// step-ended of that pipeline heard after them, or at the expiry of the key settling.<revision>, written reserved-settle
|
|
// after the first. The release's package-published then
|
|
// moves no fingerprint. A pipeline-ended not passed whose revision holds reservations withdraws them: the series'
|
|
// numbered goes back to its newest released version and the importers are re-pinned to it.
|
|
|
|
// ReservedPrefix begins every key of the state heard holding a revision's reservations of one module.
|
|
const ReservedPrefix = "reserved."
|
|
|
|
// The defaults of the settings reserved-keep and reserved-settle (H-A-424 (1), (2)).
|
|
const (
|
|
DefaultReservedKeep = 24 * time.Hour
|
|
DefaultReservedSettle = 30 * time.Second
|
|
)
|
|
|
|
// ReservedKey is the key of module's reservations in revision, `reserved.<revision>.<module>`, the module escaped as a
|
|
// key's name is.
|
|
func ReservedKey(revision, module string) string {
|
|
return ReservedPrefix + revision + "." + keys.EscapeName(module)
|
|
}
|
|
|
|
// Reservation is one version-reserved as the indexer keeps it.
|
|
type Reservation struct {
|
|
Artifact string `json:"artifact"`
|
|
Path string `json:"path"`
|
|
Version string `json:"version"`
|
|
Fingerprint string `json:"fingerprint,omitempty"`
|
|
}
|
|
|
|
// Reserved is the value of a reserved key: a module's reservations in one revision.
|
|
type Reserved struct {
|
|
V int `json:"v"`
|
|
Revision string `json:"revision"`
|
|
Module string `json:"module"`
|
|
Reservations []Reservation `json:"reservations"`
|
|
At time.Time `json:"at"`
|
|
}
|
|
|
|
// TTLStore is a Store that writes a new key with its own time to live (a state declared `key-ttl`).
|
|
type TTLStore interface {
|
|
CreateTTL(ctx context.Context, key string, value []byte, ttl time.Duration) error
|
|
}
|
|
|
|
// SettlingPrefix begins the key of the state heard that holds a revision's gathering open, `settling.<revision>`, written
|
|
// with the key-ttl reserved-settle at its first reservation: its expiry, heard through the indexer's watch of heard
|
|
// (Expired), settles the gathering; the indexer holds no timer (ADR 0437 §8; H-A-436).
|
|
const SettlingPrefix = "settling."
|
|
|
|
// SettlingKey is the key holding revision's gathering open.
|
|
func SettlingKey(revision string) string { return SettlingPrefix + revision }
|
|
|
|
// HearVersionReserved takes one version-reserved in: the series' numbered version, the reservation kept and the revision's
|
|
// gathering begun. An error leaves the event for the bus to give again.
|
|
func (ix *Indexer) HearVersionReserved(ctx context.Context, e meshbuilds.VersionReservedEvent) error {
|
|
if ix.Heard == nil {
|
|
return errors.New("the indexer keeps no state " + HeardState)
|
|
}
|
|
_, ok, err := ix.branchOfRevision(ctx, e.Revision)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if !ok {
|
|
ix.logf("versions: the version-reserved of %s %s names the revision %q, which is of no watched branch: left", e.Artifact, e.Version, e.Revision)
|
|
return nil
|
|
}
|
|
if err := ix.keepNumbered(ctx, e); err != nil {
|
|
return err
|
|
}
|
|
if err := ix.keepReservation(ctx, e); err != nil {
|
|
return err
|
|
}
|
|
ix.logf("versions: %s reserved %s %s in %s; its importers are re-pinned once the pipeline's reservations settle", e.Module, e.Path, e.Version, e.Revision)
|
|
return ix.gather(ctx, e.Revision)
|
|
}
|
|
|
|
// branchOfRevision is the watched branch (the trunk among them) whose revision name is revision, `<branch>.<n>`.
|
|
func (ix *Indexer) branchOfRevision(ctx context.Context, revision string) (string, bool, error) {
|
|
branches, err := ix.watchedBranches(ctx)
|
|
if err != nil {
|
|
return "", false, err
|
|
}
|
|
if !slices.Contains(branches, TrunkBranch) {
|
|
branches = append(branches, TrunkBranch)
|
|
}
|
|
for _, b := range branches {
|
|
if _, ok := BranchRevisionOf(b, revision); ok {
|
|
return b, true, nil
|
|
}
|
|
}
|
|
return "", false, nil
|
|
}
|
|
|
|
// seriesOfPath is the series key and value of the package path names in series: the key whose name or Go module is path.
|
|
func (ix *Indexer) seriesOfPath(ctx context.Context, path, series string) (string, SeriesPackage, bool) {
|
|
all := ix.SeriesAll(ctx)
|
|
for _, key := range slices.Sorted(maps.Keys(all)) {
|
|
if s := all[key]; s.Series == series && (s.Name == path || s.GoModule == path) {
|
|
return key, s, true
|
|
}
|
|
}
|
|
return SeriesKey(path, series), SeriesPackage{}, false
|
|
}
|
|
|
|
// keepNumbered writes the reserved version as its series' numbered one, with the contents read at the commit of the
|
|
// module's entry in the event's revision (H-A-418 (2)); an older version than the numbered held changes nothing.
|
|
func (ix *Indexer) keepNumbered(ctx context.Context, e meshbuilds.VersionReservedEvent) error {
|
|
series, ok := SeriesOf(e.Version)
|
|
if ix.Packages == nil || e.Path == "" || !ok {
|
|
return nil
|
|
}
|
|
key, s, _ := ix.seriesOfPath(ctx, e.Path, series)
|
|
v := SeriesVersion{Version: strings.TrimPrefix(e.Version, "v"), Fingerprint: e.Fingerprint}
|
|
if ix.Revisions != nil {
|
|
_, entries, err := ReadRevision(ctx, ix.Revisions, e.Revision)
|
|
if err != nil {
|
|
return fmt.Errorf("the revision %s of %s's reservation: %w", e.Revision, e.Module, err)
|
|
}
|
|
if i := slices.IndexFunc(entries, func(x meshcatalog.CatalogEntry) bool { return x.Module == e.Module }); i >= 0 {
|
|
v.Commit = entries[i].Commit
|
|
if s.GoModule != "" && v.Commit != "" && ix.Files != nil {
|
|
got, err := ix.packagesAt(ctx, entries[i].Repository, v.Commit, []string{s.GoModule}, nil)
|
|
switch p, found := got[s.GoModule]; {
|
|
case err != nil:
|
|
ix.logf("versions: %s %s's contents at %.12s are not read, so its importers are not pinned to it before its publish: %v", e.Path, e.Version, v.Commit, err)
|
|
case !found:
|
|
ix.logf("versions: the forge's listing at %.12s holds no Go module %s, so %s's importers are not pinned to it before its publish", v.Commit, s.GoModule, e.Version)
|
|
default:
|
|
p.At = ptr(ix.now().UTC())
|
|
v.Contents = contentsOfPublished(p)
|
|
}
|
|
}
|
|
} else {
|
|
ix.logf("versions: the revision %s holds no entry of %s, so %s %s is numbered with no contents", e.Revision, e.Module, e.Path, e.Version)
|
|
}
|
|
}
|
|
return write(ctx, ix.Packages, key, func(value []byte, found bool) ([]byte, bool, error) {
|
|
held := SeriesPackage{V: keys.Version, Name: e.Path, Series: series}
|
|
if found {
|
|
if err := json.Unmarshal(value, &held); err != nil {
|
|
return nil, false, err
|
|
}
|
|
}
|
|
if n := held.Numbered; n != nil && (CompareVersions("v"+n.Version, "v"+v.Version) > 0 || n.Version == v.Version && n.Contents != nil) {
|
|
return nil, false, nil
|
|
}
|
|
held.Numbered = numberedOf(v)
|
|
held.At = ptr(ix.now())
|
|
next, err := json.Marshal(held)
|
|
return next, false, err
|
|
})
|
|
}
|
|
|
|
// keepReservation keeps e under its revision and module in the state heard, the key living reserved-keep: a module's
|
|
// further reservation in the revision is added to its key, which is written again with its time to live.
|
|
func (ix *Indexer) keepReservation(ctx context.Context, e meshbuilds.VersionReservedEvent) error {
|
|
key := ReservedKey(e.Revision, e.Module)
|
|
r := Reservation{Artifact: e.Artifact, Path: e.Path, Version: e.Version, Fingerprint: e.Fingerprint}
|
|
for range writeTries {
|
|
value, rev, found, err := ix.Heard.Get(ctx, key)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
held := Reserved{V: keys.Version, Revision: e.Revision, Module: e.Module, At: ix.now().UTC()}
|
|
if found {
|
|
if err := json.Unmarshal(value, &held); err != nil {
|
|
return fmt.Errorf("heard.%s: %w", key, err)
|
|
}
|
|
if slices.Contains(held.Reservations, r) {
|
|
return nil
|
|
}
|
|
}
|
|
held.Reservations = append(held.Reservations, r)
|
|
data, err := json.Marshal(held)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if found {
|
|
if err := ix.Heard.Delete(ctx, key, rev); errors.Is(err, ErrConflict) {
|
|
continue
|
|
} else if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
err = ix.createTTL(ctx, key, data)
|
|
if errors.Is(err, ErrConflict) {
|
|
continue
|
|
}
|
|
return err
|
|
}
|
|
return fmt.Errorf("heard.%s: %w", key, ErrConflict)
|
|
}
|
|
|
|
// createTTL creates key in the state heard living reserved-keep (createTTLFor).
|
|
func (ix *Indexer) createTTL(ctx context.Context, key string, data []byte) error {
|
|
keep := ix.ReservedKeep
|
|
if keep <= 0 {
|
|
keep = DefaultReservedKeep
|
|
}
|
|
return ix.createTTLFor(ctx, key, data, keep)
|
|
}
|
|
|
|
// createTTLFor creates key in the state heard living ttl; a store that keeps no time to live per key keeps it with none,
|
|
// said.
|
|
func (ix *Indexer) createTTLFor(ctx context.Context, key string, data []byte, ttl time.Duration) error {
|
|
if st, ok := ix.Heard.(TTLStore); ok {
|
|
return st.CreateTTL(ctx, key, data, ttl)
|
|
}
|
|
ix.logf("versions: the state %s keeps no time to live per key here, so %s is kept with none", HeardState, key)
|
|
return ix.Heard.Create(ctx, key, data)
|
|
}
|
|
|
|
// reservedOf are the reservations revision holds, by module.
|
|
func (ix *Indexer) reservedOf(ctx context.Context, revision string) (map[string]Reserved, map[string]uint64, error) {
|
|
if ix.Heard == nil {
|
|
return nil, nil, nil
|
|
}
|
|
all, err := ix.Heard.Keys(ctx)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
out, revs := map[string]Reserved{}, map[string]uint64{}
|
|
prefix := ReservedPrefix + revision + "."
|
|
for _, key := range all {
|
|
if !strings.HasPrefix(key, prefix) {
|
|
continue
|
|
}
|
|
value, rev, found, err := ix.Heard.Get(ctx, key)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
var r Reserved
|
|
if !found || json.Unmarshal(value, &r) != nil || r.Revision != revision {
|
|
continue
|
|
}
|
|
out[key], revs[key] = r, rev
|
|
}
|
|
return out, revs, nil
|
|
}
|
|
|
|
// gather begins revision's gathering, if none is open: the key settling.<revision>, living reserved-settle.
|
|
func (ix *Indexer) gather(ctx context.Context, revision string) error {
|
|
settle := ix.ReservedSettle
|
|
if settle <= 0 {
|
|
settle = DefaultReservedSettle
|
|
}
|
|
data, err := json.Marshal(map[string]any{"revision": revision, "at": ix.now().UTC()})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
err = ix.createTTLFor(ctx, SettlingKey(revision), data, settle)
|
|
if errors.Is(err, ErrConflict) {
|
|
return nil // a gathering is open already
|
|
}
|
|
return err
|
|
}
|
|
|
|
// GatherKept settles, at the start, every revision the state heard holds reservations of whose gathering is not open any
|
|
// more: a settle cut by a restart is made then (one that moves nothing keeps no revision). An open one settles at its
|
|
// expiry or its pipeline's next word.
|
|
func (ix *Indexer) GatherKept(ctx context.Context) error {
|
|
if ix.Heard == nil {
|
|
return nil
|
|
}
|
|
all, err := ix.Heard.Keys(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
revisions := map[string]bool{}
|
|
for _, key := range all {
|
|
if !strings.HasPrefix(key, ReservedPrefix) {
|
|
continue
|
|
}
|
|
value, _, found, err := ix.Heard.Get(ctx, key)
|
|
var r Reserved
|
|
if err != nil || !found || json.Unmarshal(value, &r) != nil || slices.Contains(all, SettlingKey(r.Revision)) {
|
|
continue
|
|
}
|
|
revisions[r.Revision] = true
|
|
}
|
|
var errs []error
|
|
for _, revision := range slices.Sorted(maps.Keys(revisions)) {
|
|
errs = append(errs, ix.settleNow(ctx, revision))
|
|
}
|
|
return errors.Join(errs...)
|
|
}
|
|
|
|
// Settle settles revision's gathering, if one is open (a pipeline-started or step-ended of its pipeline): its key goes, and
|
|
// every importer of the modules it reserved is re-pinned in one revision of its branch. None open settles nothing.
|
|
func (ix *Indexer) Settle(ctx context.Context, revision string) error {
|
|
if ix.Heard == nil {
|
|
return nil
|
|
}
|
|
key := SettlingKey(revision)
|
|
_, rev, found, err := ix.Heard.Get(ctx, key)
|
|
if err != nil || !found {
|
|
return err
|
|
}
|
|
if err := ix.Heard.Delete(ctx, key, rev); errors.Is(err, ErrConflict) {
|
|
return nil // settled by another word, or by its expiry
|
|
} else if err != nil {
|
|
return err
|
|
}
|
|
return ix.settleNow(ctx, revision)
|
|
}
|
|
|
|
// Expired takes in a key of the state heard that its time to live ended, as the watch of heard hands it over: a
|
|
// gathering's key settles that gathering (H-A-436); any other key is passed over.
|
|
func (ix *Indexer) Expired(ctx context.Context, key string) error {
|
|
revision, ok := strings.CutPrefix(key, SettlingPrefix)
|
|
if !ok {
|
|
return nil
|
|
}
|
|
return ix.settleNow(ctx, revision)
|
|
}
|
|
|
|
// settleNow re-pins every importer of the modules revision reserved in one revision of its branch.
|
|
func (ix *Indexer) settleNow(ctx context.Context, revision string) error {
|
|
held, _, err := ix.reservedOf(ctx, revision)
|
|
if err != nil || len(held) == 0 {
|
|
return err
|
|
}
|
|
branch, ok, err := ix.branchOfRevision(ctx, revision)
|
|
if err != nil || !ok {
|
|
return err
|
|
}
|
|
modules := modulesOf(held)
|
|
_, err = ix.repinModules(ctx, branch, modules, reservedCall(revision, len(modules), false))
|
|
return err
|
|
}
|
|
|
|
// Withdraw takes a pipeline-ended not passed of branch's revision in: each reservation the revision holds is withdrawn, its
|
|
// series' numbered set back to the newest released version, its importers re-pinned to that, and its key deleted.
|
|
func (ix *Indexer) Withdraw(ctx context.Context, branch string, revision int64) error {
|
|
if ix.Heard == nil {
|
|
return nil
|
|
}
|
|
name := BranchObject(branch, revision)
|
|
if _, rev, found, err := ix.Heard.Get(ctx, SettlingKey(name)); err == nil && found {
|
|
_ = ix.Heard.Delete(ctx, SettlingKey(name), rev) // the gathering is withdrawn with it
|
|
}
|
|
held, revs, err := ix.reservedOf(ctx, name)
|
|
if err != nil || len(held) == 0 {
|
|
return err
|
|
}
|
|
for _, r := range held {
|
|
for _, one := range r.Reservations {
|
|
if err := ix.withdrawNumbered(ctx, one); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
modules := modulesOf(held)
|
|
if _, err := ix.repinModules(ctx, branch, modules, reservedCall(name, len(modules), true)); err != nil {
|
|
return err
|
|
}
|
|
for key, rev := range revs {
|
|
if err := ix.Heard.Delete(ctx, key, rev); err != nil && !errors.Is(err, ErrConflict) {
|
|
return err
|
|
}
|
|
}
|
|
ix.logf("versions: the %d reservations of %s are withdrawn, their importers re-pinned to the newest released versions", len(held), name)
|
|
return nil
|
|
}
|
|
|
|
// withdrawNumbered sets the series' numbered version back to its newest released one while it is the version r reserved.
|
|
func (ix *Indexer) withdrawNumbered(ctx context.Context, r Reservation) error {
|
|
series, ok := SeriesOf(r.Version)
|
|
if ix.Packages == nil || !ok {
|
|
return nil
|
|
}
|
|
key, _, found := ix.seriesOfPath(ctx, r.Path, series)
|
|
if !found {
|
|
return nil
|
|
}
|
|
return write(ctx, ix.Packages, key, func(value []byte, found bool) ([]byte, bool, error) {
|
|
var s SeriesPackage
|
|
if !found || json.Unmarshal(value, &s) != nil || s.Numbered == nil || s.Numbered.Version != strings.TrimPrefix(r.Version, "v") {
|
|
return nil, false, nil
|
|
}
|
|
s.Numbered = nil
|
|
if s.Newest != nil {
|
|
s.Numbered = numberedOf(*s.Newest)
|
|
}
|
|
s.At = ptr(ix.now())
|
|
next, err := json.Marshal(s)
|
|
return next, false, err
|
|
})
|
|
}
|
|
|
|
// modulesOf are the modules of a revision's reservations, sorted.
|
|
func modulesOf(held map[string]Reserved) []string {
|
|
var out []string
|
|
for _, r := range held {
|
|
if !slices.Contains(out, r.Module) {
|
|
out = append(out, r.Module)
|
|
}
|
|
}
|
|
slices.Sort(out)
|
|
return out
|
|
}
|
|
|
|
// reservedCall is the call of the re-pin revision of a revision's reservations: a release's re-pin, made at its reservation
|
|
// (§805), or at their withdrawal.
|
|
func reservedCall(revision string, modules int, withdrawn bool) revisionCall {
|
|
call := fmt.Sprintf("release of %s's %d reserved modules", revision, modules)
|
|
if withdrawn {
|
|
call = fmt.Sprintf("release of %s's %d reserved modules withdrawn", revision, modules)
|
|
}
|
|
return revisionCall{Call: call, Kind: meshcatalog.CatalogRevisionCallKindRelease}
|
|
}
|