carlos / platform Public

fix: exclude stateless databases from box replication

Do not claim stateless database paths from flat or nested discovery, explicit unit paths, or overlapping unit trees. Keep their files untouched and out of replication configuration and per-host database maintenance.

The Litestream witness failed before the fix and passes with targeted regressions. The full Task 3 TestStateless command and all three cross-builds pass.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Paul Campbell pushed by claude agent, operated by paul@keymail.dev 49abb3dd8317c0333397f2ca26fc9c80435c64cc parent db9fe8c
2 files changed, +115 −2
  • internal/ops/litestream.go +24 −2
  • internal/ops/stateless_test.go +91 −0
diff --git a/internal/ops/litestream.go b/internal/ops/litestream.go
index 32d1eae4f..5e8850d2c 100644
--- a/internal/ops/litestream.go
+++ b/internal/ops/litestream.go
@@ -341,8 +341,15 @@ func FleetDBsWithSkipped(reg *registry.Registry, dataDir string, fenced func(hos
return nil, nil, err
}
hibernating := map[string]bool{}
+ stateless := map[string]bool{}
unitPaths := map[string]bool{}
for _, r := range routes {
+ if r.Stateless {
+ if r.DBPath != "" {
+ stateless[filepath.Clean(r.DBPath)] = true
+ }
+ continue
+ }
if r.Backing == registry.BackingUnit {
// Unit tenants' data lives outside dataDir (their systemd
// StateDirectory), so the glob below can never find them on its
@@ -363,6 +370,16 @@ func FleetDBsWithSkipped(reg *registry.Registry, dataDir string, fenced func(hos
}
}
+ // A different route's tree discovery must not reclaim an inactive DB.
+ inactive := func(p string) bool {
+ p = filepath.Clean(p)
+ for root := range stateless {
+ if p == root || strings.HasPrefix(p, root+string(filepath.Separator)) {
+ return true
+ }
+ }
+ return false
+ }
matches, err := filepath.Glob(filepath.Join(dataDir, "*.db"))
if err != nil {
return nil, nil, err
@@ -396,14 +413,14 @@ func FleetDBsWithSkipped(reg *registry.Registry, dataDir string, fenced func(hos
filepath.Clean(consoleDBPath(dataDir)): true,
}
for _, r := range routes {
- if r.DBPath == "" || (fenced != nil && fenced(r.Host)) {
+ if r.Stateless || r.DBPath == "" || (fenced != nil && fenced(r.Host)) {
continue
}
claimed[filepath.Clean(r.DBPath)] = true
}
var globbed []string
for _, p := range matches {
- if claimed[filepath.Clean(p)] {
+ if !inactive(p) && claimed[filepath.Clean(p)] {
globbed = append(globbed, p)
continue
}
@@ -437,6 +454,11 @@ func FleetDBsWithSkipped(reg *registry.Registry, dataDir string, fenced func(hos
if seen[p] {
continue // a unit DBPath that happens to also sit under dataDir
}
+ if inactive(p) {
+ skipped = append(skipped, p)
+ seen[p] = true
+ continue
+ }
fi, err := os.Stat(p)
if err != nil || !fi.Mode().IsRegular() || fi.Size() == 0 {
continue // gone, odd, or an empty placeholder — nothing to replicate
diff --git a/internal/ops/stateless_test.go b/internal/ops/stateless_test.go
new file mode 100644
index 000000000..2c1daeb15
--- /dev/null
+++ b/internal/ops/stateless_test.go
@@ -0,0 +1,91 @@
+package ops
+
+import (
+ "os"
+ "path/filepath"
+ "slices"
+ "strings"
+ "testing"
+
+ "github.com/carlosframework/platform/internal/provision"
+ "github.com/carlosframework/platform/internal/registry"
+)
+
+func TestStatelessExcludedFromBoxLitestream(t *testing.T) {
+ dir := t.TempDir()
+ reg, err := registry.Open(filepath.Join(dir, "registry.db"))
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer reg.Close()
+ inactive := []registry.Route{
+ {Host: "flat.example.com", DBPath: filepath.Join(dir, "flat.db")},
+ {Host: "nested.example.com", DBPath: provision.NestedDBPath(dir, "nested.example.com")},
+ {Host: "asleep.example.com", DBPath: filepath.Join(dir, "asleep.db"), Hibernate: true},
+ {Host: "rootfs.example.com", DBPath: provision.NestedDBPath(dir, "rootfs.example.com"), Runtime: registry.RuntimeRootfsV1},
+ {Host: "unit.example.com", DBPath: filepath.Join(t.TempDir(), "unit.db"), Backing: registry.BackingUnit},
+ {Host: "tree.example.com", DBPath: t.TempDir(), Backing: registry.BackingUnit},
+ }
+ snapshots := map[string]string{}
+ for i, rt := range inactive {
+ rt.Kind = "instance"
+ rt.Stateless = true
+ path := rt.DBPath
+ if i == len(inactive)-1 {
+ path = filepath.Join(path, "child.db")
+ }
+ if err := os.MkdirAll(filepath.Dir(path), 0700); err != nil {
+ t.Fatal(err)
+ }
+ for _, suffix := range []string{"", "-wal", "-shm"} {
+ body := "preserved" + suffix
+ if err := os.WriteFile(path+suffix, []byte(body), 0600); err != nil {
+ t.Fatal(err)
+ }
+ snapshots[path+suffix] = body
+ }
+ if err := reg.Upsert(rt); err != nil {
+ t.Fatal(err)
+ }
+ }
+ // An active unit tree can overlap another route's inactive database.
+ if err := reg.Upsert(registry.Route{Host: "parent.example.com", Backing: registry.BackingUnit, DBPath: filepath.Join(dir, "instances")}); err != nil {
+ t.Fatal(err)
+ }
+ active := filepath.Join(dir, "active.db")
+ if err := os.WriteFile(active, []byte("active"), 0600); err != nil {
+ t.Fatal(err)
+ }
+ if err := reg.Upsert(registry.Route{Host: "active.example.com", DBPath: active}); err != nil {
+ t.Fatal(err)
+ }
+ dbs, skipped, err := FleetDBsWithSkipped(reg, dir, nil)
+ if err != nil {
+ t.Fatal(err)
+ }
+ config := LitestreamConfig(dbs, "bucket", "prefix", "region")
+ for path := range snapshots {
+ if slices.Contains(dbs, path) || strings.Contains(config, path) {
+ t.Errorf("stateless database entered replication config: %s", path)
+ }
+ }
+ if !slices.Contains(dbs, active) || !slices.Contains(dbs, filepath.Join(dir, "registry.db")) {
+ t.Fatal("active or registry database excluded")
+ }
+ if !slices.Contains(skipped, inactive[0].DBPath) || !slices.Contains(skipped, inactive[1].DBPath) {
+ t.Fatalf("old databases not reported as skipped: %v", skipped)
+ }
+ maintenance, err := HibernatingHosts(reg)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if len(maintenance) != 0 {
+ t.Fatalf("stateless databases entered maintenance: %+v", maintenance)
+ }
+ for path, want := range snapshots {
+ raw, err := os.ReadFile(path)
+ if err != nil || string(raw) != want {
+ t.Fatalf("unused bytes changed: %s: %v", path, err)
+ }
+ }
+}