Files
mesh-delivery/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/unput.go
T

85 lines
3.6 KiB
Go

package main
import (
"context"
"errors"
"fmt"
"time"
"github.com/nats-io/nats.go/jetstream"
"git.novox.be/novox/mesh-contracts/health-and-repair/go/clients/meshconditionsclient"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-builder/builds"
"mesh-delivery-builder/internal/build"
"mesh-delivery-builder/internal/versions"
)
// **A key left building unput is a finding after five minutes** (issue 1966, 1748 note 27; novox/hq ADR 0668 §3, §5). A
// look whose end could not be written leaves its key building with no look holding it: its ask put or not, nothing ends
// it but builder_build_again or the builder's next start. The look arms a key of the state bounds (UnputBoundKey) with
// its own time to live; the bus's marker of its end, heard on the watch, raises call-unanswered about the build key while
// the key is still building and no look holds it. The key's next written end deletes the bound and clears the finding.
// No timer.
type unputs struct {
bounds func() jetstream.KeyValue
store build.Store
looking func(key string) bool
findings *findings
bound time.Duration
now func() time.Time
logf func(string, ...any)
}
// unputCallee is what the finding names as the call left unanswered: the build seats' put of the key's asks and the
// builder's write of its end.
const unputCallee = "the put of its asks on the build seats and the write of its end"
// left arms key's bound; one that cannot be written would never end, so the finding is raised at once.
func (u *unputs) left(ctx context.Context, key, why string) {
var err error
if kv := u.bounds(); kv == nil {
err = fmt.Errorf("%s is not on the bus yet", versions.BoundsBucket)
} else if _, err = kv.Create(ctx, versions.UnputBoundKey(key), versions.EncodeBound("the build key "+key+" left building unput: "+why, u.now().Add(u.bound)), jetstream.KeyTTL(u.bound)); errors.Is(err, jetstream.ErrKeyExists) {
err = nil
}
if err == nil {
return
}
u.logf("the unput bound of %s is not written, so it is raised now: %v", key, err)
u.raise(ctx, key, fmt.Sprintf("its end could not be written (%s), nor its bound (%v)", why, err), u.now())
}
// ended deletes key's bound and clears its finding: its end was written.
func (u *unputs) ended(ctx context.Context, key string) {
if kv := u.bounds(); kv != nil {
if err := kv.Delete(ctx, versions.UnputBoundKey(key)); err != nil && !errors.Is(err, jetstream.ErrKeyNotFound) {
u.logf("the unput bound of %s is not deleted: %v", key, err)
}
}
u.findings.clear(ctx, meshconditionsclient.KindCallUnanswered, meshconditionsclient.ScopeCall, key)
}
// expired is told each change of the state bounds: the end of an unput bound raises call-unanswered while its key is
// still building and no look holds it.
func (u *unputs) expired(ctx context.Context, e jetstream.KeyValueEntry) {
key, ok := versions.UnputBoundOf(e.Key())
if !ok || e.Operation() != jetstream.KeyValuePurge || u.looking(key) {
return
}
raw, _, found, err := u.store.Get(ctx, key)
if err != nil || !found {
return
}
if v, err := builds.DecodeBuilderBuild(raw); err != nil || v.State != builds.BuildBuilding {
return
}
u.raise(ctx, key, fmt.Sprintf("its look ended without writing its end, and nothing ended it within %s", u.bound), u.now().Add(-u.bound))
}
func (u *unputs) raise(ctx context.Context, key, fault string, since time.Time) {
o := callUnanswered(key, unputCallee, "", fault, "builder."+meshconditionsclient.KindCallUnanswered, since)
o.needs = build.BuildAgainTool + " builds the key again: an ask its stream holds is waited for, one never put is put; " + build.StuckTool + " shows it."
u.findings.raise(ctx, o)
}