From 915049a729f9b1e3571fb5f453e21127d0c76ae6 Mon Sep 17 00:00:00 2001 From: jochen Date: Wed, 25 Nov 2026 17:14:32 +0100 Subject: [PATCH] H7 fingerprint-no-change: a difference of MESH_FINGERPRINT alone in a container is no change, in the judgement and in the apply Builds: design 47 H2 H-A-710 (2), (3) (the node-engine's side) (hq 27369077) --- internal/agent/agent_test.go | 29 +++++++++++++++++++- internal/apply/apply.go | 51 ++++++++++++++++++++++++++++++++++++ internal/apply/hold_test.go | 7 +++++ internal/apply/reads_test.go | 40 ++++++++++++++++++++++++++++ internal/judge/judge.go | 31 ++++++++++++++++++++-- internal/judge/judge_test.go | 7 +++++ 6 files changed, 162 insertions(+), 3 deletions(-) diff --git a/internal/agent/agent_test.go b/internal/agent/agent_test.go index 5c7412a4..4fd175a9 100644 --- a/internal/agent/agent_test.go +++ b/internal/agent/agent_test.go @@ -1827,7 +1827,7 @@ func testTheNodesOwnCompositionIsAppliedWhenItDiffersAndARecordMoveWaitsForAYes( b := newBed(t) b.env.SelfComposed = true b.env.Policy = func(_ context.Context, m string) string { - if m == "db" || m == "rec" { + if m == "db" || m == "rec" || m == "fpd" { return catalogue.PolicyRecord } return catalogue.PolicyRoll @@ -2209,6 +2209,33 @@ func testTheNodesOwnCompositionIsAppliedWhenItDiffersAndARecordMoveWaitsForAYes( return len(b.appliedList()) == applied+2 && h.Phase == string(nodeengineclient.PhaseRunning) && h.FP == fpB && r.Phase == string(nodeengineclient.PhaseRunning) && r.FP == fpB && r.Composed == rdig }) + // Design 47 H2 H-A-710 (2), (3): a record module whose container an earlier node-engine composed with MESH_FINGERPRINT, + // moved to the composition of another build that differs by that value alone, is no move: it is applied with no yes, and + // its have never waits (before H2 the same pair was judged to recreate the container and waited for a person). + fpContainer := func(env map[string]string) json.RawMessage { + raw, _ := json.Marshal(map[string]any{"id": "fpd.server", "type": "container", "module": "fpd", "name": "fpd", + "image": "fpd@sha256:" + strings.Repeat("4", 64), "env": env}) + return raw + } + fpA, fpC := fpOf("a"), fpOf("c") + k1, raw1, _ := unsigned("fpd", fpA, fpContainer(map[string]string{"MESH_FINGERPRINT": fpA})) + b.comps.put(k1, raw1) + want("fpd", fpA, "") + eventually(t, "fpd's first composition was not applied", func() bool { + return len(b.appliedList()) == applied+3 && b.haves.last("fpd").Phase == string(nodeengineclient.PhaseRunning) + }) + k2, raw2, dig2fp := unsigned("fpd", fpC, json.RawMessage(`{"id":"fpd.server","type":"container","module":"fpd","name":"fpd","image":"fpd@sha256:`+strings.Repeat("4", 64)+`"}`)) + b.comps.put(k2, raw2) + want("fpd", fpC, "") + eventually(t, "a move that changes the container's fingerprint alone was not applied with no yes", func() bool { + h := b.haves.last("fpd") + return len(b.appliedList()) == applied+4 && h.Phase == string(nodeengineclient.PhaseRunning) && h.Composed == dig2fp + }) + for _, h := range b.haves.log { + if h.Module == "fpd" && h.Phase == string(nodeengineclient.PhaseWaitingYes) { + t.Errorf("fpd waited for a yes: %+v", h) + } + } } // 1957/AC1 and AC4 (unit; ADR 0568 §1, §4): an engine start takes a module from what the machine shows, not only from the diff --git a/internal/apply/apply.go b/internal/apply/apply.go index 0424d02a..15c40798 100644 --- a/internal/apply/apply.go +++ b/internal/apply/apply.go @@ -26,6 +26,7 @@ import ( "sort" "strconv" "strings" + "sync" "time" "git.novox.be/novox/mesh-sdk/go/declaration" @@ -2920,6 +2921,45 @@ func (in inputs) reads(r *declaration.Container) map[string]string { return out } +// fingerprintVar is the value the node-engine handed a container before H2 and hands no container now (H-A-710). +const fingerprintVar = "MESH_FINGERPRINT" + +// keptFingerprintSaid is each container a kept fingerprint was said for, once per node-engine process. +var keptFingerprintSaid sync.Map + +// labelledFingerprint says whether the running container name was created with the value fingerprintVar in its env and +// nothing else differing from r's composed spec: its spec label (a digest) equals r's spec with that value put back +// (H-A-710 (2)). It answers the value. False for a container that declares the value itself, that holds none, or whose +// env cannot be read. +func labelledFingerprint(ctx context.Context, run Runner, cri string, r *declaration.Container, label string, declares, reads map[string]string) (string, bool) { + if _, declared := r.Env[fingerprintVar]; declared { + return "", false + } + out, err := run(ctx, cri, "container", "inspect", "--format", "{{json .Config.Env}}", r.Name) + if err != nil { + return "", false + } + var held []string + if json.Unmarshal([]byte(strings.TrimSpace(out)), &held) != nil { + return "", false + } + fp := "" + for _, e := range held { + if v, ok := strings.CutPrefix(e, fingerprintVar+"="); ok { + fp = v + } + } + if fp == "" { + return "", false + } + with := *r + with.Env = map[string]string{fingerprintVar: fp} + for k, v := range r.Env { + with.Env[k] = v + } + return fp, containerSpecReading(&with, declares, reads) == label +} + // containerSpec is the identity of a declared container: everything that, if changed, means // the running container is no longer what was asked for. func containerSpec(r *declaration.Container, in inputs) string { @@ -3200,6 +3240,17 @@ func applyContainer(ctx context.Context, r *declaration.Container, run Runner, // not be by the alternative either, which recreates it without knowing whether it needed to. legacy := len(reads) > 0 && before.Spec == containerSpecReading(r, in.declares, nil) && (wasReading == nil || sameReads(wasReading, reads)) + // **A container labelled with the fingerprint an earlier node-engine handed it is accepted, not recreated** (design 47 + // H2, H-A-710 (2)): a container is handed none now, so its label differs from the composed spec by that value alone; + // the label is written again at its next recreate for another cause, which takes the value out. + if existed && !legacy && before.Spec != want { + if fp, keeps := labelledFingerprint(ctx, run, cri, r, before.Spec, in.declares, reads); keeps { + legacy = true + if _, said := keptFingerprintSaid.LoadOrStore(r.Name, true); !said { + journal(fmt.Sprintf("%s keeps %s %s: a container is handed none, H-A-710", r.Name, fingerprintVar, fp)) + } + } + } // **A container a maintenance window holds still is left as it is** (novox/hq issue 224, ADR // 0189). Everywhere else stopped means broken, and the rule below replaces it; during a window diff --git a/internal/apply/hold_test.go b/internal/apply/hold_test.go index 4b0a8fde..53dd615c 100644 --- a/internal/apply/hold_test.go +++ b/internal/apply/hold_test.go @@ -2,6 +2,7 @@ package apply import ( "context" + "encoding/json" "errors" "os" "path/filepath" @@ -105,6 +106,8 @@ type fakeContainer struct { // What a take compares (ADR 0163), answered in the long inspect form when set. image, imageID string networks, mounts, ports []string + // env is what the container's configuration holds, as `{{json .Config.Env}}` answers it. + env []string } // oldShape is a systemctl call as these fakes were written to read it: the verb, the unit, then the @@ -171,6 +174,10 @@ func (m *machine) run(_ context.Context, name string, args ...string) (string, e if c.running { running = "true" } + if strings.Contains(args[3], ".Config.Env") { + raw, _ := json.Marshal(c.env) + return string(raw) + "\n", nil + } if strings.HasPrefix(args[3], "{{.Id}}") { line := c.id + "\t" + running + "\t" + c.spec if c.image != "" { diff --git a/internal/apply/reads_test.go b/internal/apply/reads_test.go index b318c02a..32ead3de 100644 --- a/internal/apply/reads_test.go +++ b/internal/apply/reads_test.go @@ -247,6 +247,45 @@ func testAContainerLabelledBeforeTheHostReadItsFilesIsAcceptedNotRecreated(t *te } } +func testAContainerLabelledWithTheFingerprintItWasHandedIsAcceptedNotRecreated(t *testing.T) { + // Design 47 H2 H-A-710 (2): the node-engine before H2 handed every container MESH_FINGERPRINT, so a running container's + // label holds it and the composed spec does not. That difference alone recreates nothing, said once per container and + // process; a changed image still recreates, and its new label holds none. + declare := func(image string) *declaration.Declaration { + return parseTrusted(t, `{"declaration":1,"resources":[ + {"id":"forge.server","type":"container","name":"forge","image":"`+image+`","env":{"A":"1"}}]}`) + } + was := declare(pinned).Resources[0].(*declaration.Container) + handed := *was + handed.Env = map[string]string{"A": "1", "MESH_FINGERPRINT": "src2:" + strings.Repeat("a", 64)} + known := store.State{} + known.Record(store.Applied{ID: "forge.server", Type: "container", Origin: store.OriginCarried, Target: "forge"}) + m := &machine{containers: map[string]*fakeContainer{"forge": {id: "made-before", running: true, spec: containerSpecReading(&handed, nil, nil), + env: []string{"A=1", "MESH_FINGERPRINT=" + handed.Env["MESH_FINGERPRINT"]}}}} + var said []string + wasJournal := journal + t.Cleanup(func() { journal = wasJournal }) + journal = func(line string) { said = append(said, line) } + keptFingerprintSaid.Clear() + + for range 2 { + report, _ := applyCarried(t, declare(pinned), known, m, nil) + if m.did("docker rm") || m.did("docker run") || report.Changed() { + t.Fatalf("a container labelled with the fingerprint it was handed was recreated: %v %+v", m.asked, report.Outcomes) + } + } + if len(said) != 1 || !strings.Contains(said[0], "forge keeps MESH_FINGERPRINT src2:") || !strings.Contains(said[0], "a container is handed none, H-A-710") { + t.Errorf("said %q, want one line for the container", said) + } + // A changed image recreates it, and the label written holds no fingerprint. + m.asked = nil + other := "registry.example/other@sha256:" + strings.Repeat("9", 64) + applyCarried(t, declare(other), known, m, nil) + if !m.removed("forge") || !m.did("docker run") || strings.Contains(m.containers["forge"].spec, "MESH_FINGERPRINT") { + t.Fatalf("a changed image did not recreate the container with a clean label: %v", m.asked) + } +} + func testAContainerAdoptedUnderANewIdStillSaysWhichFileChanged(t *testing.T) { // The bundle's `store` becomes the postgres module's `postgres.server`: the same container by // name, under a new id with no record of its own. A file change on that day is a real change, @@ -322,6 +361,7 @@ func TestReads(t *testing.T) { t.Run("AContainerIsRecreatedWhenAMountedSecretChanged", testAContainerIsRecreatedWhenAMountedSecretChanged) t.Run("AMountedDirectoryIsNotLookedInside", testAMountedDirectoryIsNotLookedInside) t.Run("AContainerLabelledBeforeTheHostReadItsFilesIsAcceptedNotRecreated", testAContainerLabelledBeforeTheHostReadItsFilesIsAcceptedNotRecreated) + t.Run("AContainerLabelledWithTheFingerprintItWasHandedIsAcceptedNotRecreated", testAContainerLabelledWithTheFingerprintItWasHandedIsAcceptedNotRecreated) t.Run("AContainerAdoptedUnderANewIdStillSaysWhichFileChanged", testAContainerAdoptedUnderANewIdStillSaysWhichFileChanged) t.Run("AHeldContainerIsNotRecreatedByAChangedHeldFile", testAHeldContainerIsNotRecreatedByAChangedHeldFile) } diff --git a/internal/judge/judge.go b/internal/judge/judge.go index 99ab051f..e1d623a2 100644 --- a/internal/judge/judge.go +++ b/internal/judge/judge.go @@ -9,6 +9,7 @@ import ( "strings" "git.novox.be/novox/mesh-sdk/go/declaration" + engcomp "github.com/novox/mesh-host/internal/compositions" ) // The judgement of a move (ADR 0377 §3), as a pure function over two compositions of one module, so the node agent that @@ -269,7 +270,7 @@ func judgedResourcesByID(c Composed) map[string]*judgedResource { if err := json.Unmarshal(raw, &head); err != nil { continue } - r := &judgedResource{kind: head.Type, path: head.Path, raw: compactResource(raw)} + r := &judgedResource{kind: head.Type, path: head.Path, raw: compactJudged(raw)} for _, v := range head.Volumes { source, _, _ := strings.Cut(v, ":") r.mounts = append(r.mounts, source) @@ -295,6 +296,32 @@ func compactResource(raw []byte) []byte { return out } +// compactJudged is a resource as the judgement compares it: compactResource, and for a container with the value +// MESH_FINGERPRINT taken out of its env on whichever side it stands, so two compositions whose containers differ by that +// value alone are no change and ask nothing (design 47 H2, H-A-710 (2)): a container is handed none now, and one composed +// by an earlier node-engine holds it. +func compactJudged(raw []byte) []byte { + var v map[string]any + d := json.NewDecoder(bytes.NewReader(raw)) + d.UseNumber() + if err := d.Decode(&v); err != nil || v["type"] != string(declaration.TypeContainer) { + return compactResource(raw) + } + if env, ok := v["env"].(map[string]any); ok { + if _, has := env[engcomp.FingerprintVar]; has { + delete(env, engcomp.FingerprintVar) + if len(env) == 0 { + delete(v, "env") + } + } + } + out, err := json.Marshal(v) + if err != nil { + return compactResource(raw) + } + return out +} + // changedOf are the ids of the resources of the kinds counted that are changed, added or removed, sorted. func changedOf(before, after map[string]*judgedResource, counted func(kind string) bool) []string { var out []string @@ -357,7 +384,7 @@ func SameComposedResources(a, b Composed) bool { return false } for i := range a.Resources { - if !bytes.Equal(compactResource(a.Resources[i]), compactResource(b.Resources[i])) { + if !bytes.Equal(compactJudged(a.Resources[i]), compactJudged(b.Resources[i])) { return false } } diff --git a/internal/judge/judge_test.go b/internal/judge/judge_test.go index 9548f17d..c7b9f8fe 100644 --- a/internal/judge/judge_test.go +++ b/internal/judge/judge_test.go @@ -22,6 +22,10 @@ func TestJudgeMove(t *testing.T) { const ( container = `{"id":"mesh-mail.server","type":"container","module":"mesh-mail","name":"mail","image":"mail@sha256:1111111111111111111111111111111111111111111111111111111111111111","volumes":["/var/lib/mesh-mail/spool:/spool"]}` container2 = `{"id":"mesh-mail.server","type":"container","module":"mesh-mail","name":"mail","image":"mail@sha256:2222222222222222222222222222222222222222222222222222222222222222","volumes":["/var/lib/mesh-mail/spool:/spool"]}` + // H-A-710 (2): a container an earlier node-engine composed holds MESH_FINGERPRINT in its env, a new one none. + withFP = `{"id":"mesh-mail.server","type":"container","module":"mesh-mail","name":"mail","image":"mail@sha256:1111111111111111111111111111111111111111111111111111111111111111","volumes":["/var/lib/mesh-mail/spool:/spool"],"env":{"MESH_FINGERPRINT":"src2:aaaa"}}` + withFP2 = `{"id":"mesh-mail.server","type":"container","module":"mesh-mail","name":"mail","image":"mail@sha256:1111111111111111111111111111111111111111111111111111111111111111","volumes":["/var/lib/mesh-mail/spool:/spool"],"env":{"MESH_FINGERPRINT":"src2:bbbb"}}` + withFPAndA = `{"id":"mesh-mail.server","type":"container","module":"mesh-mail","name":"mail","image":"mail@sha256:1111111111111111111111111111111111111111111111111111111111111111","volumes":["/var/lib/mesh-mail/spool:/spool"],"env":{"A":"1","MESH_FINGERPRINT":"src2:bbbb"}}` spool = `{"id":"mesh-mail.spool","type":"directory","module":"mesh-mail","path":"/var/lib/mesh-mail/spool"}` spoolMoved = `{"id":"mesh-mail.spool","type":"directory","module":"mesh-mail","path":"/srv/mesh-mail/spool"}` tools = `{"id":"mesh-mail.tools","type":"file","module":"mesh-mail","path":"/usr/lib/mesh-mail/tools.json","content":"a"}` @@ -59,6 +63,9 @@ func TestJudgeMove(t *testing.T) { "consumers join or leave: cloud on novox leaves smtp, wiki on ace joins smtp"}, {"a consumer leaves and a file moves", mail('a', true, container, spool, received, config), mail('b', true, container, spool, received2, config2), JudgedMovesData, false, "it changes where its data is kept: mesh-mail.conf; consumers join or leave: cloud on novox leaves smtp, wiki on ace joins smtp"}, + {"a container's fingerprint taken out", mail('a', true, withFP, spool), mail('b', true, container, spool), JudgedUnchanged, false, ""}, + {"a container's fingerprint changes", mail('a', true, withFP, spool), mail('b', true, withFP2, spool), JudgedUnchanged, false, ""}, + {"a container's fingerprint taken out and one more env", mail('a', true, withFP, spool), mail('b', true, withFPAndA, spool), JudgedRecreates, false, ""}, {"its data flag alone", mail('a', false, container, spool), mail('b', true, container, spool), JudgedNeither, false, ""}, {"its data flag and its spool", mail('a', false, container, spool), mail('b', true, container, spoolMoved), JudgedMovesData, false, ""}, {"a process of a module with data", mail('a', true, spool, process), mail('b', true, spool, process2), JudgedMovesData, false, ""},