Files
ilkerandClaude Opus 5 6dc2826c17 hpcod: the life of an application, carried past the moment it is published
Publishing worked; everything after it did not. Four things were wrong
and each was silent in its own way.

Versions were compared as text, so asking for "gromacs" with 2024.9 and
2024.10 published returned 2024.9. The whole meaning of an unversioned
reference is "the newest one", and it was a wrong answer rather than a
missing one - invisible until somebody noticed they had spent a week on
last year's build.

A recipe could hold one version while the catalogue could hold many, so
an update either destroyed what it replaced or grew a parallel
directory sharing nothing with the first. Recipes now carry a list, and
the single-version form is read into the same shape, so nothing
downstream knows which was written.

An image was named by a mutable tag. Whoever repushes gromacs:2024.4
changed what every rebuilt cluster received, silently, and the record
meant to freeze the cluster froze only its opinion of it. Two hashes
are now recorded because they are checkable in different places: the
manifest digest answers "has this moved" from the control plane, and
the SIF hash is the only one a head can check with no network and no
credential - which matters, because the head is where a wrong image
does harm. Pulling by digest is deliberately not done: whether this
Apptainer accepts an @sha256: reference is not established, and a
provision failing at its last task over untested syntax would be worse
than the problem.

Module and binaries had been in the catalogue since it was written and
were read by nothing - Module was referenced nowhere in the program. So
a tenant was handed /opt/apps/gromacs.sif and left to work out
Apptainer. They now become a wrapper per binary and an Lmod modulefile,
and the answer is `module load gromacs`. Whether to pass --nv is
decided on the node at run time: passing it without a driver fails
outright, and omitting it where there is one leaves the job on the CPU
saying nothing at all, which is the dangerous one.

Four states replace `published:`. A withdrawn entry stays in the
catalogue file, because dropping it would make it indistinguishable
from one that never existed - and that difference is what an operator
needs when a cluster is found still holding it. A pinned reference to
one is refused with the recorded reason instead of "no application",
which would send the user hunting for a typo in a correct name.

The portal re-reads the catalogue when it changes, so publishing an
application no longer needs a restart - the step that is invisible when
forgotten. C20 ships the catalogue, which nothing did.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-09-22 10:25:54 +02:00

117 lines
3.5 KiB
Go

package catalog
import (
"log"
"os"
"sync"
"time"
)
// Watcher is a catalogue that notices when its file changes.
//
// Publishing an application is three steps - build the image, regenerate the
// file, put the file on the control plane - and without this there is a
// fourth that nobody remembers: restart the portal. A catalogue loaded once
// at startup means an operator who has just published GROMACS sees the old
// list, deploys again, still sees the old list, and concludes the generator
// is broken. The registry of tenant portals already re-reads itself for the
// same reason (see httpapi.ClusterRegistry); this is that idiom, applied to
// the other file the portal reads from disk.
//
// The mtime is the trigger rather than a timer, so a file that has not
// changed costs one stat and no parse.
type Watcher struct {
path string
mu sync.RWMutex
cat *Catalog
modTime time.Time
// loaded distinguishes "never read" from "read and found empty". Without
// it a missing file would be re-parsed on every request.
loaded bool
}
// FromFile returns a catalogue backed by path, re-read whenever the file
// changes. An empty path is a portal with no catalogue, which is a valid
// configuration: the site publishes no applications.
func FromFile(path string) *Watcher { return &Watcher{path: path} }
// Static returns a catalogue that never changes. It is what a test wants,
// and what a caller that already has the parsed value should pass.
func Static(c *Catalog) *Watcher {
if c == nil {
c = &Catalog{}
}
return &Watcher{cat: c, loaded: true}
}
// Current returns the catalogue as the file stands now.
//
// It never returns nil, and it never returns an error: a file that has
// become unreadable or unparseable leaves the previous contents in place and
// says so in the log. Taking every application off the plan page because
// somebody saved a broken YAML would be a larger outage than the mistake.
func (w *Watcher) Current() *Catalog {
if w == nil {
return &Catalog{}
}
if w.path == "" {
return w.snapshot()
}
st, err := os.Stat(w.path)
if err != nil {
// A file that was there and is now gone. Emptying the catalogue is
// right here and not in the parse-failure case below: the file is
// the whole statement of what is published, and its absence is a
// complete statement, whereas a half-written one is not.
w.mu.Lock()
if w.loaded && w.cat != nil && len(w.cat.Apps) > 0 {
log.Printf("application catalogue: %s is gone; nothing is published now", w.path)
}
w.cat, w.loaded = &Catalog{}, true
w.modTime = time.Time{}
w.mu.Unlock()
return w.snapshot()
}
w.mu.RLock()
fresh := w.loaded && st.ModTime().Equal(w.modTime)
w.mu.RUnlock()
if fresh {
return w.snapshot()
}
c, err := Load(w.path)
if err != nil {
log.Printf("application catalogue: %v (keeping the previous %d entries)",
err, len(w.snapshot().Apps))
// The mtime is deliberately not recorded, so the next request tries
// again. Recording it would mean a file fixed a second later stayed
// ignored until the portal restarted - which is the thing this type
// exists to stop.
w.mu.Lock()
w.loaded = true
w.mu.Unlock()
return w.snapshot()
}
w.mu.Lock()
changed := !w.loaded || len(w.cat.Apps) != len(c.Apps)
w.cat, w.modTime, w.loaded = c, st.ModTime(), true
w.mu.Unlock()
if changed {
log.Printf("application catalogue: %d entries from %s", len(c.Apps), w.path)
}
return w.snapshot()
}
func (w *Watcher) snapshot() *Catalog {
w.mu.RLock()
defer w.mu.RUnlock()
if w.cat == nil {
return &Catalog{}
}
return w.cat
}