activator: refuse a seed over a database it cannot attribute (H3-M6)
A seed with no replica of its own purged whatever database sat at its path before placing the snapshot. On an unlabelled row (a fleet file seed, or a --from or import seed in a region) that database can be a deleted record's local copy under the same host, another app's or account's, and the purge destroyed it. The wake now refuses instead, and leaves it in place: a database, its WAL or SHM, or a litestream meta directory at the path that this seed's own marker does not name. A labelled row already set such a copy aside and refused under the start rule. A marker alone, a fetch's temp file and a link are purged as before. TestSeededRestorePurges* become the refusal and a marker-only placement test.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
3 files changed,
+185
−18
internal/activator/seedplace.go+47 −0internal/activator/seedplace_h3m6_linux_test.go+114 −0internal/activator/seedplace_linux_test.go+24 −18
diff --git a/internal/activator/seedplace.go b/internal/activator/seedplace.go| index 0781dadca..c51390878 100644 |
| --- a/internal/activator/seedplace.go |
| +++ b/internal/activator/seedplace.go |
| @@ -90,6 +90,35 @@ func seedRefusedClaim(host string) error { |
| return &SeedPlacementError{Sentence: fmt.Sprintf("%s could not start from its seed: claim control is on for this box.", host)} |
| } |
| +// seedOverUnattributedData is a seed whose first wake found a database at |
| +// its path that is not this seed's (H3-M6). It carries no code: a box |
| +// reports nothing, and every wake refuses until an operator has looked. |
| +func seedOverUnattributedData(host, path string) error { |
| + return &SeedPlacementError{Sentence: fmt.Sprintf("%s could not start from its seed: this box already holds a database for it that is not this seed's. Ask an operator to check it.", host), |
| + Err: fmt.Errorf("%s holds data no marker attributes to this seed; refusing to purge it", path)} |
| +} |
| + |
| +// unattributedLocalData names the first entry at a seed's path that holds |
| +// a database's data: the database, its WAL or SHM, or either litestream |
| +// meta directory. A symlink is not data (the purge removes the link and |
| +// never follows it), nor is a marker or a fetch's temp file. "" when there |
| +// is none. An entry that cannot be examined is an error, never absence. |
| +func unattributedLocalData(dir, base string) (string, error) { |
| + for _, n := range []string{base, base + "-wal", base + "-shm", base + "-litestream", ".activator-" + base + "-litestream"} { |
| + fi, err := os.Lstat(filepath.Join(dir, n)) |
| + if errors.Is(err, os.ErrNotExist) { |
| + continue |
| + } |
| + if err != nil { |
| + return "", err |
| + } |
| + if fi.Mode()&os.ModeSymlink == 0 { |
| + return n, nil |
| + } |
| + } |
| + return "", nil |
| +} |
| + |
| func seedPlaceFailed(host string, err error) error { |
| return &SeedPlacementError{Sentence: fmt.Sprintf("%s could not start from its seed: it could not be put in place. The next wake tries again.", host), Err: err} |
| } |
| @@ -272,6 +301,24 @@ func (b *ExecBackend) seededRestore(ctx context.Context, rt registry.Route, src |
| log.Printf("activator: %s: keeping seed %s placed by an earlier wake", host, src.ID) |
| return false, nil |
| } |
| + // A database still at the path is not this seed's (placed above |
| + // would have kept it): another seed's, or the local copy of a deleted |
| + // record that held this host, whatever its app or account. Nothing |
| + // here says whose, so it is never purged to make room (hunt H3-M6): |
| + // on an unlabelled row the wake refuses and leaves it in place. A |
| + // labelled row's copy is set aside below and the start rule refuses |
| + // it the same way. Only data counts: a marker alone, an earlier |
| + // fetch's temp file, or a link (which the purge removes, never its |
| + // target) is purged as before. |
| + if rt.Assigned == "" { |
| + held, err := unattributedLocalData(dir, base) |
| + if err != nil { |
| + return fail(seedPlaceFailed(host, err)) |
| + } |
| + if held != "" { |
| + return fail(seedOverUnattributedData(host, filepath.Join(dir, held))) |
| + } |
| + } |
| // Placing the snapshot is a start from something other than the |
| // host's own copy: on a labelled row, the start rule decides it as it |
| // decides an empty start. Anything still at the path is a copy kept |
diff --git a/internal/activator/seedplace_h3m6_linux_test.go b/internal/activator/seedplace_h3m6_linux_test.go| new file mode 100644 |
| index 000000000..544aee48d |
| --- /dev/null |
| +++ b/internal/activator/seedplace_h3m6_linux_test.go |
| @@ -0,0 +1,114 @@ |
| +//go:build linux |
| + |
| +package activator |
| + |
| +// Hunt H3-M6, the fleet file seed route (Astra round 1, P1-3): a seed with |
| +// no replica of its own, placed on a box that still holds another app's |
| +// database at the same host's path (that app's record deleted, its local |
| +// copy left), must not purge that database to make room. Nothing on the |
| +// box says whose it is, so the wake refuses and leaves it where it is, on |
| +// an unlabelled row and a labelled one alike. |
| + |
| +import ( |
| + "bytes" |
| + "context" |
| + "os" |
| + "path/filepath" |
| + "strings" |
| + "testing" |
| + |
| + "github.com/carlosframework/platform/internal/seeds" |
| +) |
| + |
| +const otherAppsData = "another app's database, left when its record was deleted" |
| + |
| +// leaveOtherAppsDatabase writes a database with no marker at the row's |
| +// path, and optionally a marker naming another seed beside it. |
| +func leaveOtherAppsDatabase(t *testing.T, dbPath, marker string) { |
| + t.Helper() |
| + if err := os.MkdirAll(filepath.Dir(dbPath), 0o700); err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := os.WriteFile(dbPath, []byte(otherAppsData), 0o600); err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := os.WriteFile(dbPath+"-wal", []byte("w"), 0o600); err != nil { |
| + t.Fatal(err) |
| + } |
| + if marker != "" { |
| + if err := os.WriteFile(dbPath+SeedMarkerSuffix, []byte(marker+"\n"), 0o600); err != nil { |
| + t.Fatal(err) |
| + } |
| + } |
| +} |
| + |
| +// otherAppsDataSurvives reports whether the leftover's bytes are still on |
| +// the box: at the row's path, or set aside under .superseded. |
| +func otherAppsDataSurvives(t *testing.T, b *ExecBackend, dbPath string) bool { |
| + t.Helper() |
| + if got, err := os.ReadFile(dbPath); err == nil && bytes.Equal(got, []byte(otherAppsData)) { |
| + return true |
| + } |
| + found := false |
| + _ = filepath.WalkDir(filepath.Join(b.DataDir, ".superseded"), func(p string, d os.DirEntry, err error) error { |
| + if err == nil && !d.IsDir() { |
| + if got, rerr := os.ReadFile(p); rerr == nil && bytes.Equal(got, []byte(otherAppsData)) { |
| + found = true |
| + } |
| + } |
| + return nil |
| + }) |
| + return found |
| +} |
| + |
| +func TestH3M6FleetFileSeedRefusesAnotherAppsLocalDatabase(t *testing.T) { |
| + for _, c := range []struct { |
| + name, assigned, marker string |
| + }{ |
| + {"unlabelled", "", ""}, |
| + {"unlabelled-foreign-marker", "", "sd_bbbbbbbbbbbbbbbbbbbbbbbbbb"}, |
| + {"labelled", "asg-h3m6", ""}, |
| + } { |
| + t.Run(c.name, func(t *testing.T) { |
| + r := fleetFileSeedRig(t, false) |
| + r.rt.Assigned = c.assigned |
| + leaveOtherAppsDatabase(t, r.rt.DBPath, c.marker) |
| + _, err := r.b.Restore(context.Background(), r.rt) |
| + if err == nil { |
| + t.Fatal("the seed was placed over another app's database; want the wake refused") |
| + } |
| + if r.objs.opens != 0 { |
| + t.Fatalf("snapshot opened %d times; nothing may be fetched over data that is not this seed's", r.objs.opens) |
| + } |
| + if !otherAppsDataSurvives(t, r.b, r.rt.DBPath) { |
| + t.Fatal("the other app's database was deleted") |
| + } |
| + if len(r.placed) != 0 { |
| + t.Fatalf("reported placed: %v", r.placed) |
| + } |
| + }) |
| + } |
| +} |
| + |
| +// The same rule on a region box: a --from or import seed's first wake over |
| +// a database it cannot attribute refuses, with the member's sentence, and |
| +// keeps the database in place. |
| +func TestH3M6RegionSeedRefusesAnUnattributedDatabase(t *testing.T) { |
| + noOwnReplica(t) |
| + b, rt := nestedBackend(t, &fakeControl{}, &fakeUnit{}) |
| + rt, objs, _ := seededRoute(t, b, rt, seeds.StateReady) |
| + leaveOtherAppsDatabase(t, rt.DBPath, "") |
| + _, err := b.Restore(context.Background(), rt) |
| + if err == nil || !strings.Contains(err.Error(), "could not start from its seed: this box already holds a database for it that is not this seed's.") { |
| + t.Fatalf("Restore = %v; want the unattributed-data refusal", err) |
| + } |
| + if objs.opens != 0 { |
| + t.Fatalf("snapshot opened %d times", objs.opens) |
| + } |
| + if !otherAppsDataSurvives(t, b, rt.DBPath) { |
| + t.Fatal("the unattributed database was deleted") |
| + } |
| + if _, err := os.Stat(rt.DBPath + "-wal"); err != nil { |
| + t.Fatalf("its WAL was removed: %v", err) |
| + } |
| +} |
diff --git a/internal/activator/seedplace_linux_test.go b/internal/activator/seedplace_linux_test.go| index a7847e6df..42a455d68 100644 |
| --- a/internal/activator/seedplace_linux_test.go |
| +++ b/internal/activator/seedplace_linux_test.go |
| @@ -118,10 +118,14 @@ func TestSeededRestorePlacesTheSnapshot(t *testing.T) { |
| } |
| } |
| -func TestSeededRestorePurgesAnotherSeedsDatabase(t *testing.T) { |
| +// Another seed's database at the path (an aborted import's, or a deleted |
| +// record's under the same host) is not this seed's, and nothing says whose |
| +// it is: the wake refuses and purges none of it (hunt H3-M6). Before, it |
| +// was purged to make room. |
| +func TestSeededRestoreRefusesAnotherSeedsDatabase(t *testing.T) { |
| noOwnReplica(t) |
| b, rt := nestedBackend(t, &fakeControl{}, &fakeUnit{}) |
| - rt, _, id := seededRoute(t, b, rt, seeds.StateSwapped) |
| + rt, objs, _ := seededRoute(t, b, rt, seeds.StateSwapped) |
| os.MkdirAll(filepath.Dir(rt.DBPath), 0o700) |
| os.WriteFile(rt.DBPath, []byte("an aborted import's data"), 0o600) |
| os.WriteFile(rt.DBPath+"-wal", []byte("w"), 0o600) |
| @@ -129,33 +133,35 @@ func TestSeededRestorePurgesAnotherSeedsDatabase(t *testing.T) { |
| os.WriteFile(rt.DBPath+SeedMarkerSuffix, []byte("sd_bbbbbbbbbbbbbbbbbbbbbbbbbb\n"), 0o600) |
| meta := filepath.Join(filepath.Dir(rt.DBPath), ".activator-"+filepath.Base(rt.DBPath)+"-litestream") |
| os.MkdirAll(meta, 0o700) |
| - if _, err := b.Restore(context.Background(), rt); err != nil { |
| - t.Fatal(err) |
| + if _, err := b.Restore(context.Background(), rt); err == nil { |
| + t.Fatal("the seed was placed over another seed's database") |
| } |
| - for _, p := range []string{rt.DBPath + "-wal", rt.DBPath + "-shm", meta} { |
| - if _, err := os.Stat(p); !os.IsNotExist(err) { |
| - t.Errorf("%s survived the purge", p) |
| - } |
| + if objs.opens != 0 { |
| + t.Fatalf("snapshot opened %d times", objs.opens) |
| } |
| - if m, _ := os.ReadFile(rt.DBPath + SeedMarkerSuffix); strings.TrimSpace(string(m)) != id { |
| - t.Fatalf("marker = %q, want %s", m, id) |
| + for _, p := range []string{rt.DBPath, rt.DBPath + "-wal", rt.DBPath + "-shm", meta} { |
| + if _, err := os.Stat(p); err != nil { |
| + t.Errorf("%s was removed: %v", p, err) |
| + } |
| } |
| - if b, _ := os.ReadFile(rt.DBPath); bytes.Equal(b, []byte("an aborted import's data")) { |
| - t.Fatal("the other seed's database stood in for this one") |
| + if m, _ := os.ReadFile(rt.DBPath + SeedMarkerSuffix); strings.TrimSpace(string(m)) != "sd_bbbbbbbbbbbbbbbbbbbbbbbbbb" { |
| + t.Fatalf("marker = %q, want the other seed's, untouched", m) |
| } |
| } |
| -func TestSeededRestorePurgesAnUnmarkedDatabase(t *testing.T) { |
| +// A marker alone (this seed's placement cut short before its rename, or |
| +// another seed's) holds no data: it is purged and the snapshot placed. |
| +func TestSeededRestorePlacesOverAMarkerAlone(t *testing.T) { |
| noOwnReplica(t) |
| b, rt := nestedBackend(t, &fakeControl{}, &fakeUnit{}) |
| - rt, objs, _ := seededRoute(t, b, rt, seeds.StateReady) |
| + rt, objs, id := seededRoute(t, b, rt, seeds.StateReady) |
| os.MkdirAll(filepath.Dir(rt.DBPath), 0o700) |
| - os.WriteFile(rt.DBPath, []byte("an unmarked database"), 0o600) |
| + os.WriteFile(rt.DBPath+SeedMarkerSuffix, []byte("sd_bbbbbbbbbbbbbbbbbbbbbbbbbb\n"), 0o600) |
| if fresh, err := b.Restore(context.Background(), rt); err != nil || fresh || objs.opens != 1 { |
| - t.Fatalf("fresh %v err %v opens %d; want the unmarked database replaced by the snapshot", fresh, err, objs.opens) |
| + t.Fatalf("fresh %v err %v opens %d; want the snapshot placed", fresh, err, objs.opens) |
| } |
| - if got, _ := os.ReadFile(rt.DBPath); !bytes.Equal(got, objs.body[mustSeedID(t, rt)]) { |
| - t.Fatal("the unmarked database stood in for the seed") |
| + if m, _ := os.ReadFile(rt.DBPath + SeedMarkerSuffix); strings.TrimSpace(string(m)) != id { |
| + t.Fatalf("marker = %q, want %s", m, id) |
| } |
| } |