ErrOwnBuildUnknown, its say and the own-build-unknown wait, TakeIn.Unknown and Failed go; the build only names the writer. indexer_take_in_pending takes only a group in. The node-build-go accept.check and output invokes go. A module of another repository stays as held inside of(), so a base of this repository's module is not given a value that is never written (A-476 c, A-476a); one case in the toolchains test proves it. Builds: design 47 G A-476, A-476a
191 lines
7.1 KiB
Go
191 lines
7.1 KiB
Go
package index
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"maps"
|
|
"slices"
|
|
|
|
mcc "git.novox.be/novox/mesh-contracts/health-and-repair/go/clients/meshconditionsclient"
|
|
"git.novox.be/novox/mesh-sdk/go/process"
|
|
|
|
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-indexer/takein"
|
|
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/trunk"
|
|
)
|
|
|
|
// A merge the indexer never took in (design 47 G A-463, amended by A-464): at its start and on every source revision the
|
|
// gate's source names, the indexer compares each repository's `taken` merge with the head that source names. A repository
|
|
// whose head is newer than its taken merge (the taken merge is that head's ancestor, or no merge is taken) is taken in as
|
|
// its merged message would have been (the catch-up of indexer_take_in, for that repository), said in the log, with the
|
|
// finding merge-untaken raised while it stays behind and cleared once it is taken in. A head the taken merge contains (the
|
|
// source not yet moved to a merge just taken in) is left, and a head the taken merge neither contains nor is in is said.
|
|
|
|
// UntakenMergeKind is the finding of a repository whose head the gate's source names but whose merge the indexer never took in.
|
|
const UntakenMergeKind = "merge-untaken"
|
|
|
|
func untakenFindingID(own, key string) string { return own + ".untaken." + key }
|
|
|
|
// kickBehind runs the comparison in the background, one at a time: a source revision that arrives while one runs makes it
|
|
// run once more, on the latest heads. It runs after the first start's catch-up, which takes every head in itself.
|
|
func (ix *Indexer) kickBehind(ctx context.Context) {
|
|
if ix.Taken == nil || ix.Message == nil || ix.Modules == nil {
|
|
return
|
|
}
|
|
if ix.catchingUp.Load() {
|
|
ix.behindWaiting.Store(true) // compared again when that catch-up ends (A-463a)
|
|
return
|
|
}
|
|
if ix.behindRunning.Swap(true) {
|
|
ix.behindAgain.Store(true)
|
|
return
|
|
}
|
|
ix.catchUpWG.Add(1)
|
|
go func() {
|
|
defer ix.catchUpWG.Done()
|
|
for {
|
|
ix.behindAgain.Store(false)
|
|
ix.TakeInBehind(ctx)
|
|
if !ix.behindAgain.Load() {
|
|
ix.behindRunning.Store(false)
|
|
if !ix.behindAgain.Load() || ix.behindRunning.Swap(true) {
|
|
return
|
|
}
|
|
}
|
|
}
|
|
}()
|
|
}
|
|
|
|
// TakeInBehind compares each repository's taken merge with the head the latest source names, takes in each repository
|
|
// that is behind and answers the repositories it took in.
|
|
func (ix *Indexer) TakeInBehind(ctx context.Context) []string {
|
|
heads := ix.sourceHeads()
|
|
if ix.Taken == nil || ix.Message == nil || ix.Modules == nil || len(heads) == 0 {
|
|
return nil
|
|
}
|
|
behind := map[string]string{}
|
|
taken := map[string]string{}
|
|
held := ix.pendingHeads(ctx)
|
|
for _, repository := range slices.Sorted(maps.Keys(heads)) {
|
|
head := heads[repository]
|
|
key, err := trunk.RepositoryKey(repository)
|
|
if head == "" || err != nil {
|
|
continue
|
|
}
|
|
value, _, found, err := ix.Taken.Get(ctx, key)
|
|
if errors.Is(err, process.ErrNotYet) {
|
|
return nil // the state is not on the bus yet: the next source revision compares again
|
|
}
|
|
if err != nil {
|
|
ix.logf("merges: taken.%s cannot be read, so %s is not compared with its head: %v", key, repository, err)
|
|
continue
|
|
}
|
|
merge := ""
|
|
if found {
|
|
t, err := takein.DecodeTaken(value)
|
|
if err != nil {
|
|
ix.logf("merges: taken.%s holds what is not a taken merge, left: %v", key, err)
|
|
continue
|
|
}
|
|
merge = t.Merge
|
|
}
|
|
switch {
|
|
case held[head]:
|
|
// A head the pending holds (a member waiting for its group's end) is taken in by that path, whole with
|
|
// its group: it is not behind (A-463a).
|
|
ix.clearUntaken(ctx, key, "the head of "+repository+" waits in the indexer's pending")
|
|
case merge == head:
|
|
ix.clearUntaken(ctx, key, "the indexer took in the head of "+repository)
|
|
case merge == "":
|
|
behind[repository], taken[repository] = head, ""
|
|
case ix.Contains == nil:
|
|
ix.logf("merges: %s is taken in at %.12s and its head is %.12s, but the indexer reads no ancestry of the forge, so which is newer is not said", repository, merge, head)
|
|
default:
|
|
inHead, err := ix.Contains(ctx, repository, merge, head)
|
|
if err != nil {
|
|
ix.logf("merges: whether %.12s is in %.12s of %s cannot be read: %v", merge, head, repository, err)
|
|
continue
|
|
}
|
|
if inHead {
|
|
behind[repository], taken[repository] = head, merge
|
|
continue
|
|
}
|
|
ix.clearUntaken(ctx, key, "the head of "+repository+" is in the merge taken in")
|
|
if headIn, err := ix.Contains(ctx, repository, head, merge); err == nil && !headIn {
|
|
ix.logf("merges: %s is taken in at %.12s and its head is %.12s, and neither holds the other: left", repository, merge, head)
|
|
}
|
|
}
|
|
}
|
|
for _, repository := range slices.Sorted(maps.Keys(behind)) {
|
|
ix.sayUntaken(ctx, repository, taken[repository], behind[repository])
|
|
ix.logf("merges: %s is taken in at %.12s but the gate's source names %.12s: taken in now", repository, taken[repository], behind[repository])
|
|
}
|
|
if len(behind) == 0 {
|
|
return nil
|
|
}
|
|
unread := ix.takeInHeads(ctx, behind)
|
|
var done []string
|
|
for _, repository := range slices.Sorted(maps.Keys(behind)) {
|
|
if slices.Contains(unread, repository) {
|
|
continue
|
|
}
|
|
done = append(done, repository)
|
|
key, _ := trunk.RepositoryKey(repository)
|
|
ix.clearUntaken(ctx, key, fmt.Sprintf("%s taken in at %.12s", repository, behind[repository]))
|
|
ix.logf("merges: %s taken in at %.12s", repository, behind[repository])
|
|
}
|
|
if len(done) > 0 {
|
|
if err := ix.KeepView(ctx); err != nil {
|
|
ix.logf("merges: the view could not be kept after %v were taken in: %v", done, err)
|
|
}
|
|
}
|
|
return done
|
|
}
|
|
|
|
func (ix *Indexer) sayUntaken(ctx context.Context, repository, merge, head string) {
|
|
if ix.Findings == nil {
|
|
return
|
|
}
|
|
key, _ := trunk.RepositoryKey(repository)
|
|
taken := "no merge"
|
|
if merge != "" {
|
|
taken = fmt.Sprintf("%.12s", merge)
|
|
}
|
|
f := ix.observed(mcc.ScopeModule, untakenFindingID(ix.Module, key), UntakenMergeKind,
|
|
fmt.Sprintf("the indexer took in %s at %s, and the gate's source names the head %.12s", repository, taken, head),
|
|
"A merge the indexer never took in",
|
|
"A merge into a watched branch was never heard or applied, so no package or module of the repository follows its trunk. The indexer takes the head in itself and clears this; indexer_take_in with catch-up does it by hand.",
|
|
UntakenMergeKind)
|
|
if err := ix.Findings.Write(ctx, f); err != nil {
|
|
ix.logf("merges: the finding %s for %s is not written: %v", UntakenMergeKind, repository, err)
|
|
}
|
|
}
|
|
|
|
func (ix *Indexer) clearUntaken(ctx context.Context, key, why string) {
|
|
if ix.Findings == nil {
|
|
return
|
|
}
|
|
if err := ix.clearFinding(ctx, mcc.ScopeModule, untakenFindingID(ix.Module, key), UntakenMergeKind, why); err != nil {
|
|
ix.logf("merges: the finding %s for %s is not cleared: %v", UntakenMergeKind, key, err)
|
|
}
|
|
}
|
|
|
|
// pendingHeads are the merge commits the indexer's pending holds (A-463a).
|
|
func (ix *Indexer) pendingHeads(ctx context.Context) map[string]bool {
|
|
out := map[string]bool{}
|
|
if ix.Pending == nil || !ix.pendingOnBus(ctx) {
|
|
return out
|
|
}
|
|
entries, err := ReadPending(ctx, ix.Pending, ix.logf)
|
|
if err != nil {
|
|
ix.logf("merges: the pending state cannot be read, so every head is compared: %v", err)
|
|
return out
|
|
}
|
|
for _, e := range entries {
|
|
if e.Forge.Merge != "" {
|
|
out[e.Forge.Merge] = true
|
|
}
|
|
}
|
|
return out
|
|
}
|