activator: seed temp files carry their instance's database name
A flat fleet directory holds many instances, so a purge for one must not remove another's fetch in flight. Temp files are now .seed-<db>-<id>.tmp; the old .seed-sd_*.tmp names go only from a nested directory. PurgeSeedLocal and reconcileSeedMarker take the layout.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
9 files changed,
+157
−25
internal/activator/backend_exec.go+1 −1internal/activator/hawake.go+1 −1internal/activator/seedkey_linux_test.go+2 −2internal/activator/seedplace.go+29 −11internal/activator/seedplace_linux_test.go+116 −2internal/activator/seedpurge_linux_test.go+5 −5internal/activator/startrule.go+1 −1internal/host/seedwiring.go+1 −1internal/host/seedwiring_linux_test.go+1 −1
CI cancelled — superseded by a newer push — run details
diff --git a/internal/activator/backend_exec.go b/internal/activator/backend_exec.go| index a3b59fafc..4925f51b4 100644 |
| --- a/internal/activator/backend_exec.go |
| +++ b/internal/activator/backend_exec.go |
| @@ -1097,7 +1097,7 @@ func (b *ExecBackend) restorePlain(ctx context.Context, rt registry.Route) (bool |
| if err := b.reconcileAssignment(rt, dbPath); err != nil { |
| return false, err |
| } |
| - if err := reconcileSeedMarker(rt, dbPath); err != nil { |
| + if err := reconcileSeedMarker(rt, dbPath, b.pathsFor(rt).layout); err != nil { |
| return false, err |
| } |
| if _, err := os.Stat(dbPath); err == nil { |
diff --git a/internal/activator/hawake.go b/internal/activator/hawake.go| index 450613aa2..31029cb33 100644 |
| --- a/internal/activator/hawake.go |
| +++ b/internal/activator/hawake.go |
| @@ -590,7 +590,7 @@ func (b *ExecBackend) haRestorePrelude(ctx context.Context, rt registry.Route) e |
| if err := b.settleSeedObligation(ctx, rt, p.dbPath); err != nil { |
| return err |
| } |
| - return reconcileSeedMarker(rt, p.dbPath) |
| + return reconcileSeedMarker(rt, p.dbPath, p.layout) |
| } |
| // restoreHA is an HA host's restore when its lock is not in storage |
diff --git a/internal/activator/seedkey_linux_test.go b/internal/activator/seedkey_linux_test.go| index f0c86d276..658a40a13 100644 |
| --- a/internal/activator/seedkey_linux_test.go |
| +++ b/internal/activator/seedkey_linux_test.go |
| @@ -87,7 +87,7 @@ func TestAnAbortedImportsKeyDoesNotOutliveItsRollback(t *testing.T) { |
| if err := RetireSeedKey(rt.DBPath, rt.Host, before); err != nil { |
| t.Fatalf("RetireSeedKey: %v", err) |
| } |
| - if err := PurgeSeedLocal(rt.DBPath); err != nil { |
| + if err := PurgeSeedLocal(rt.DBPath, LayoutNested); err != nil { |
| t.Fatal(err) |
| } |
| for _, n := range []string{rt.Host + ".env", rt.Host + instancekey.AdoptionSuffix} { |
| @@ -156,7 +156,7 @@ func TestRetireSeedKeyLeavesWhatPredatesTheImport(t *testing.T) { |
| if err := RetireSeedKey(rt.DBPath, rt.Host, before); err != nil { |
| t.Fatal(err) |
| } |
| - if err := PurgeSeedLocal(rt.DBPath); err != nil { |
| + if err := PurgeSeedLocal(rt.DBPath, LayoutNested); err != nil { |
| t.Fatal(err) |
| } |
| if got, err := os.ReadFile(envPath); err != nil || string(got) != string(predating) { |
diff --git a/internal/activator/seedplace.go b/internal/activator/seedplace.go| index 7237d40fb..71ee80a42 100644 |
| --- a/internal/activator/seedplace.go |
| +++ b/internal/activator/seedplace.go |
| @@ -240,7 +240,7 @@ func (b *ExecBackend) seededRestore(ctx context.Context, rt registry.Route, src |
| // Anything else is purged before the fetch: another seed's database, |
| // one with no marker, their WAL and SHM, the marker, litestream's |
| // metadata, and an earlier fetch's leftovers. |
| - if err := purgeSeedTarget(dir, base); err != nil { |
| + if err := purgeSeedTarget(dir, base, paths.layout == LayoutNested); err != nil { |
| return fail(seedPlaceFailed(host, err)) |
| } |
| if err := b.placeSeed(ctx, host, src, dir, base); err != nil { |
| @@ -270,7 +270,7 @@ func (b *ExecBackend) seededRestore(ctx context.Context, rt registry.Route, src |
| // |
| // A relative path is no seeded path (seeded rows are always given the |
| // nested, absolute one), so it is left to the restore as before. |
| -func reconcileSeedMarker(rt registry.Route, dbPath string) error { |
| +func reconcileSeedMarker(rt registry.Route, dbPath string, layout Layout) error { |
| if !filepath.IsAbs(dbPath) { |
| return nil |
| } |
| @@ -287,7 +287,7 @@ func reconcileSeedMarker(rt registry.Route, dbPath string) error { |
| if err != nil { |
| return err |
| } |
| - if err := purgeSeedTarget(dir, base); err != nil { |
| + if err := purgeSeedTarget(dir, base, layout == LayoutNested); err != nil { |
| return fmt.Errorf("restore %s: purge what seed %s left at %s: %w", rt.Host, marker, dbPath, err) |
| } |
| log.Printf("activator: %s: %s held seed %s's marker, not this route's (seed %q): purged before the restore", rt.Host, dbPath, marker, own) |
| @@ -325,8 +325,11 @@ func notAPlacedFile(err error) bool { |
| } |
| // seedTempName is where a seed's snapshot is fetched to, in the instance |
| -// directory. The id is a seed id (seeds.Check), one clean path element. |
| -func seedTempName(id string) string { return ".seed-" + id + ".tmp" } |
| +// directory. base is the database's file name (<host>.db) and id a seed id |
| +// (seeds.Check), so the name is one clean path element. It carries base |
| +// because a flat fleet directory holds many instances: a purge for one must |
| +// not remove another's fetch in flight (fleet seeded create plan Task 9). |
| +func seedTempName(base, id string) string { return ".seed-" + base + "-" + id + ".tmp" } |
| // PurgeSeedLocal removes an instance's local database at dbPath and |
| // everything beside it that could stand in for one (purgeSeedTarget): the |
| @@ -335,7 +338,11 @@ func seedTempName(id string) string { return ".seed-" + id + ".tmp" } |
| // rollback's local removal (seeded-instances spec §5.6 step 3), run after |
| // the host is fenced and stopped. A directory that is not there holds |
| // nothing, so a second run, or one for an instance never woken, is done. |
| -func PurgeSeedLocal(dbPath string) error { |
| +// |
| +// layout is the instance's: only a nested directory holds a single instance, |
| +// so only there may a purge remove an old-style ".seed-sd_*.tmp" name, |
| +// which says no instance. |
| +func PurgeSeedLocal(dbPath string, layout Layout) error { |
| dir, base, err := splitDBPath(dbPath) |
| if err != nil { |
| return err |
| @@ -343,7 +350,13 @@ func PurgeSeedLocal(dbPath string) error { |
| if _, err := os.Lstat(dir); errors.Is(err, os.ErrNotExist) { |
| return nil |
| } |
| - return purgeSeedTarget(dir, base) |
| + return purgeSeedTarget(dir, base, layout == LayoutNested) |
| +} |
| + |
| +// PurgeSeedLocalFor is PurgeSeedLocal for rt's database, in the layout this |
| +// backend finds rt in. |
| +func (b *ExecBackend) PurgeSeedLocalFor(rt registry.Route) error { |
| + return PurgeSeedLocal(rt.DBPath, b.pathsFor(rt).layout) |
| } |
| // ReadSeedMarker is the seed id the <db>.seed marker beside dbPath names, |
| @@ -384,8 +397,11 @@ func splitDBPath(dbPath string) (dir, base string, err error) { |
| // purgeSeedTarget removes everything at and beside dir/base that could |
| // stand in for a seed's database: what PurgeLocalDBArtifacts removes in its |
| -// full form, the marker, and any seed temp file, through instancefs. |
| -func purgeSeedTarget(dir, base string) error { |
| +// full form, the marker, and any seed temp file of this database (any seed |
| +// id), through instancefs. legacyTemps also removes the old-style |
| +// ".seed-sd_*.tmp" names, which name no instance; the caller passes it only |
| +// for a nested directory, which holds one. |
| +func purgeSeedTarget(dir, base string, legacyTemps bool) error { |
| names := []string{base, base + "-wal", base + "-shm", base + SeedMarkerSuffix, |
| base + "-litestream", ".activator-" + base + "-litestream"} |
| ents, err := os.ReadDir(dir) |
| @@ -393,7 +409,9 @@ func purgeSeedTarget(dir, base string) error { |
| return err |
| } |
| for _, e := range ents { |
| - if n := e.Name(); strings.HasPrefix(n, ".seed-sd_") && strings.Contains(n, ".tmp") { |
| + n := e.Name() |
| + own := strings.HasPrefix(n, ".seed-"+base+"-sd_") && strings.Contains(n, ".tmp") |
| + if own || (legacyTemps && strings.HasPrefix(n, ".seed-sd_") && strings.Contains(n, ".tmp")) { |
| names = append(names, n) |
| } |
| } |
| @@ -418,7 +436,7 @@ func purgeSeedTarget(dir, base string) error { |
| // and a refusal after the rename removes the marker and the database, so no |
| // unchecked file is ever kept as this seed. |
| func (b *ExecBackend) placeSeed(ctx context.Context, host string, src *seeds.RowSeed, dir, base string) error { |
| - tmp := seedTempName(src.ID) |
| + tmp := seedTempName(base, src.ID) |
| defer func() { |
| for _, n := range []string{tmp, tmp + "-wal", tmp + "-shm"} { |
| _ = instancefs.RemoveAllBeneath(dir, n) |
diff --git a/internal/activator/seedplace_linux_test.go b/internal/activator/seedplace_linux_test.go| index 8e9fd7677..f08ac8886 100644 |
| --- a/internal/activator/seedplace_linux_test.go |
| +++ b/internal/activator/seedplace_linux_test.go |
| @@ -525,7 +525,7 @@ func TestSeededRestoreRefusesASwappedTempFile(t *testing.T) { |
| t.Fatal(err) |
| } |
| swap := func() { |
| - tmp := filepath.Join(dir, ".seed-"+id+".tmp") |
| + tmp := filepath.Join(dir, seedTempName(filepath.Base(rt.DBPath), id)) |
| os.Remove(tmp) |
| os.Symlink(other, tmp) |
| } |
| @@ -884,7 +884,7 @@ func TestPlainWakeOverAMarkerOnlyLeftoverKeepsTheReplacementsData(t *testing.T) |
| t.Fatal(err) |
| } |
| a, _ := seeds.NewID() |
| - tmp := filepath.Join(dir, seedTempName(a)) |
| + tmp := filepath.Join(dir, seedTempName(filepath.Base(rt.DBPath), a)) |
| for p, body := range map[string]string{rt.DBPath + SeedMarkerSuffix: a + "\n", tmp: "half a fetch"} { |
| if err := os.WriteFile(p, []byte(body), 0o600); err != nil { |
| t.Fatal(err) |
| @@ -1029,3 +1029,117 @@ func TestASeededRowMintsItsOwnInstanceKeyAtLaunch(t *testing.T) { |
| t.Fatal("a second launch changed the seeded instance's key") |
| } |
| } |
| + |
| +// A flat directory holds many instances, so a purge for one removes only the |
| +// temp files whose names carry its own database name (fleet seeded create |
| +// plan Task 9). The old ".seed-sd_*" names say no instance and go only from |
| +// a nested directory, which holds one. |
| +func TestPurgeSeedTargetRemovesOnlyOwnInstanceTemps(t *testing.T) { |
| + for _, legacy := range []bool{false, true} { |
| + dir := t.TempDir() |
| + a, b, z := ".seed-a.example.com.db-sd_x.tmp", ".seed-b.example.com.db-sd_y.tmp", ".seed-sd_z.tmp" |
| + for _, n := range []string{a, b, z, "a.example.com.db"} { |
| + if err := os.WriteFile(filepath.Join(dir, n), []byte("x"), 0o600); err != nil { |
| + t.Fatal(err) |
| + } |
| + } |
| + if err := purgeSeedTarget(dir, "a.example.com.db", legacy); err != nil { |
| + t.Fatal(err) |
| + } |
| + gone := func(n string) bool { _, err := os.Lstat(filepath.Join(dir, n)); return os.IsNotExist(err) } |
| + if !gone(a) || !gone("a.example.com.db") { |
| + t.Errorf("legacy=%v: the instance's own temp or database survived", legacy) |
| + } |
| + if gone(b) { |
| + t.Errorf("legacy=%v: another instance's temp was removed", legacy) |
| + } |
| + if gone(z) != legacy { |
| + t.Errorf("legacy=%v: the old-style temp's removal = %v", legacy, gone(z)) |
| + } |
| + } |
| +} |
| + |
| +func TestPurgeSeedTargetRemovesEarlierSeedsLeftover(t *testing.T) { |
| + dir := t.TempDir() |
| + first, _ := seeds.NewID() |
| + second, _ := seeds.NewID() |
| + var names []string |
| + for _, id := range []string{first, second} { |
| + names = append(names, seedTempName("a.example.com.db", id), seedTempName("a.example.com.db", id)+"-wal") |
| + } |
| + for _, n := range names { |
| + if err := os.WriteFile(filepath.Join(dir, n), []byte("x"), 0o600); err != nil { |
| + t.Fatal(err) |
| + } |
| + } |
| + if err := purgeSeedTarget(dir, "a.example.com.db", false); err != nil { |
| + t.Fatal(err) |
| + } |
| + for _, n := range names { |
| + if _, err := os.Lstat(filepath.Join(dir, n)); !os.IsNotExist(err) { |
| + t.Errorf("%s survived", n) |
| + } |
| + } |
| +} |
| + |
| +// holdingSeedObjects serves each id's snapshot but parks every read after its |
| +// first byte until release is closed, so two placements overlap with their |
| +// temp files on disk. |
| +type holdingSeedObjects struct { |
| + body map[string][]byte |
| + started chan struct{} |
| + release chan struct{} |
| +} |
| + |
| +func (h *holdingSeedObjects) Open(_ context.Context, _, id string) (io.ReadCloser, error) { |
| + b := h.body[id] |
| + return io.NopCloser(io.MultiReader(bytes.NewReader(b[:1]), |
| + readerFunc(func() { h.started <- struct{}{}; <-h.release }), bytes.NewReader(b[1:]))), nil |
| +} |
| + |
| +// readerFunc runs f once, on its first read, and is then at EOF. |
| +type readerFunc func() |
| + |
| +func (f readerFunc) Read([]byte) (int, error) { f(); return 0, io.EOF } |
| + |
| +func TestFlatFleetTwoSeedsPlaceConcurrently(t *testing.T) { |
| + b, _ := nestedBackend(t, &fakeControl{}, &fakeUnit{}) |
| + dir := t.TempDir() |
| + snap := filepath.Join(t.TempDir(), "snap.db") |
| + if err := writeMinimalSQLite(snap); err != nil { |
| + t.Fatal(err) |
| + } |
| + body, _ := os.ReadFile(snap) |
| + sum := sha256.Sum256(body) |
| + objs := &holdingSeedObjects{body: map[string][]byte{}, started: make(chan struct{}, 2), release: make(chan struct{})} |
| + b.Seeds = objs |
| + hosts := []string{"a.example.com", "b.example.com"} |
| + srcs := map[string]*seeds.RowSeed{} |
| + for _, h := range hosts { |
| + id, _ := seeds.NewID() |
| + objs.body[id] = body |
| + srcs[h] = &seeds.RowSeed{ID: id, Kind: seeds.KindFile, SHA256: hex.EncodeToString(sum[:]), Bytes: int64(len(body)), State: seeds.StateReady} |
| + } |
| + errs := make(chan error, 2) |
| + for _, h := range hosts { |
| + go func() { errs <- b.placeSeed(context.Background(), h, srcs[h], dir, h+".db") }() |
| + } |
| + <-objs.started |
| + <-objs.started |
| + // Both temp files exist. A third instance in the directory wakes and |
| + // purges its own leftovers; neither placement's temp may go with them. |
| + if err := purgeSeedTarget(dir, "c.example.com.db", false); err != nil { |
| + t.Fatal(err) |
| + } |
| + close(objs.release) |
| + for range hosts { |
| + if err := <-errs; err != nil { |
| + t.Fatalf("a placement failed: %v", err) |
| + } |
| + } |
| + for _, h := range hosts { |
| + if m, err := os.ReadFile(filepath.Join(dir, h+".db"+SeedMarkerSuffix)); err != nil || strings.TrimSpace(string(m)) != srcs[h].ID { |
| + t.Errorf("%s: marker %q, %v", h, m, err) |
| + } |
| + } |
| +} |
diff --git a/internal/activator/seedpurge_linux_test.go b/internal/activator/seedpurge_linux_test.go| index 4bbd21927..6f472fd43 100644 |
| --- a/internal/activator/seedpurge_linux_test.go |
| +++ b/internal/activator/seedpurge_linux_test.go |
| @@ -38,7 +38,7 @@ func TestPurgeSeedLocalRemovesTheDatabaseAndEverythingThatStandsInForIt(t *testi |
| t.Fatal(err) |
| } |
| for i := 0; i < 2; i++ { |
| - if err := PurgeSeedLocal(db); err != nil { |
| + if err := PurgeSeedLocal(db, LayoutNested); err != nil { |
| t.Fatalf("run %d: %v", i, err) |
| } |
| } |
| @@ -50,11 +50,11 @@ func TestPurgeSeedLocalRemovesTheDatabaseAndEverythingThatStandsInForIt(t *testi |
| if _, err := os.Stat(kept); err != nil { |
| t.Fatalf("an unrelated file went: %v", err) |
| } |
| - if err := PurgeSeedLocal(filepath.Join(t.TempDir(), "missing", "a.shop.example.com.db")); err != nil { |
| + if err := PurgeSeedLocal(filepath.Join(t.TempDir(), "missing", "a.shop.example.com.db"), LayoutNested); err != nil { |
| t.Fatalf("a missing directory: %v", err) |
| } |
| for _, bad := range []string{"a.shop.example.com.db", "/data/instances/x/../a.db", "/data/instances/x/"} { |
| - if err := PurgeSeedLocal(bad); err == nil { |
| + if err := PurgeSeedLocal(bad, LayoutNested); err == nil { |
| t.Errorf("%q: no error", bad) |
| } |
| } |
| @@ -75,7 +75,7 @@ func TestPurgeSeedLocalNeverFollowsASymlink(t *testing.T) { |
| if err := os.Symlink(target, db); err != nil { |
| t.Fatal(err) |
| } |
| - if err := PurgeSeedLocal(db); err != nil { |
| + if err := PurgeSeedLocal(db, LayoutNested); err != nil { |
| t.Fatal(err) |
| } |
| if _, err := os.Lstat(db); !os.IsNotExist(err) { |
| @@ -103,7 +103,7 @@ func TestAPlainWakeAfterPurgeSeedLocalFindsNoDatabase(t *testing.T) { |
| } |
| } |
| if purge { |
| - if err := PurgeSeedLocal(rt.DBPath); err != nil { |
| + if err := PurgeSeedLocal(rt.DBPath, LayoutNested); err != nil { |
| t.Fatal(err) |
| } |
| } |
diff --git a/internal/activator/startrule.go b/internal/activator/startrule.go| index e3371fc18..339116a9d 100644 |
| --- a/internal/activator/startrule.go |
| +++ b/internal/activator/startrule.go |
| @@ -160,7 +160,7 @@ func localCopyNames(dir, base string) (present []string, data bool, err error) { |
| return nil, false, err |
| } |
| for _, e := range ents { |
| - if n := e.Name(); strings.HasPrefix(n, ".seed-sd_") && strings.Contains(n, ".tmp") { |
| + if n := e.Name(); (strings.HasPrefix(n, ".seed-sd_") || strings.HasPrefix(n, ".seed-"+base+"-sd_")) && strings.Contains(n, ".tmp") { |
| names = append(names, n) |
| } |
| } |
diff --git a/internal/host/seedwiring.go b/internal/host/seedwiring.go| index a96ea3f81..00ef379f2 100644 |
| --- a/internal/host/seedwiring.go |
| +++ b/internal/host/seedwiring.go |
| @@ -173,7 +173,7 @@ func wireSeedPlacement(eb *activator.ExecBackend, claimArmed bool, openData, ope |
| fence := box.Fence |
| h.FenceForRollback = fenceForRollback(fence, box.Swept) |
| h.LiftFence = func(host string, gen uint64) { fence.LiftFence(host, gen) } |
| - h.RemoveLocal = func(rt registry.Route) error { return activator.PurgeSeedLocal(rt.DBPath) } |
| + h.RemoveLocal = func(rt registry.Route) error { return eb.PurgeSeedLocalFor(rt) } |
| h.LocalSeedID = activator.ReadSeedMarker |
| h.RetireKey = activator.RetireSeedKey |
| h.DeleteReplica = deleteReplica(data, box.Prefix) |
diff --git a/internal/host/seedwiring_linux_test.go b/internal/host/seedwiring_linux_test.go| index cd7d36002..1e80ebd32 100644 |
| --- a/internal/host/seedwiring_linux_test.go |
| +++ b/internal/host/seedwiring_linux_test.go |
| @@ -25,7 +25,7 @@ func TestWiredRollbackReadsAndRemovesTheLocalSeed(t *testing.T) { |
| t.Fatal(err) |
| } |
| db := filepath.Join(dir, "shop-1.example.com.db") |
| - gone := []string{db, db + "-wal", db + "-shm", db + activator.SeedMarkerSuffix, filepath.Join(dir, ".seed-sd_x.tmp")} |
| + gone := []string{db, db + "-wal", db + "-shm", db + activator.SeedMarkerSuffix, filepath.Join(dir, ".seed-shop-1.example.com.db-sd_x.tmp")} |
| for _, p := range gone { |
| body := "data" |
| if p == db+activator.SeedMarkerSuffix { |