From 34f5c7656c167b9084e9771e7dc89da5a985c2d6 Mon Sep 17 00:00:00 2001 From: pingqiu Date: Sun, 22 Nov 2026 10:21:33 -0800 Subject: [PATCH] test: reproduce node inventory loss at publication rename --- .../core/lifecycle/node_inventory.go | 15 ++- .../lifecycle/node_inventory_crash_test.go | 100 ++++++++++++++++++ 2 files changed, 111 insertions(+), 4 deletions(-) create mode 100644 seaweed-block/core/lifecycle/node_inventory_crash_test.go diff --git a/seaweed-block/core/lifecycle/node_inventory.go b/seaweed-block/core/lifecycle/node_inventory.go index c241b644da..f5bd801584 100644 --- a/seaweed-block/core/lifecycle/node_inventory.go +++ b/seaweed-block/core/lifecycle/node_inventory.go @@ -64,10 +64,11 @@ type NodeRegistration struct { // NodeInventoryStore persists one JSON registration record per server. type NodeInventoryStore struct { - mu sync.Mutex - dir string - records map[string]NodeRegistration - published sync.Map // committed NodeRegistration values; holder host checks never take mu (INV-WS-RENEW-NONBLOCKING-001) + mu sync.Mutex + dir string + records map[string]NodeRegistration + published sync.Map // committed NodeRegistration values; holder host checks never take mu (INV-WS-RENEW-NONBLOCKING-001) + persistHookForTest func(string) // nil in production; crash injection at publication boundaries } // OpenNodeInventoryStore opens or creates a node inventory store. @@ -222,9 +223,15 @@ func (s *NodeInventoryStore) putLocked(rec NodeRegistration) error { if err := os.Remove(path); err != nil && !errors.Is(err, os.ErrNotExist) { return fmt.Errorf("lifecycle: replace node %s: %w", rec.ServerID, err) } + if s.persistHookForTest != nil { + s.persistHookForTest("before-rename") + } if err := os.Rename(tmpName, path); err != nil { return fmt.Errorf("lifecycle: rename node %s: %w", rec.ServerID, err) } + if s.persistHookForTest != nil { + s.persistHookForTest("after-rename") + } return nil } diff --git a/seaweed-block/core/lifecycle/node_inventory_crash_test.go b/seaweed-block/core/lifecycle/node_inventory_crash_test.go new file mode 100644 index 0000000000..4463d1f1be --- /dev/null +++ b/seaweed-block/core/lifecycle/node_inventory_crash_test.go @@ -0,0 +1,100 @@ +//go:build linux + +package lifecycle + +import ( + "context" + "encoding/json" + "errors" + "os" + "os/exec" + "path/filepath" + "reflect" + "strings" + "testing" + "time" +) + +func crashNodeRegistration(updated bool) NodeRegistration { + value := "old" + if updated { + value = "new" + } + return NodeRegistration{ + ServerID: "node-a", Addr: "127.0.0.1:9100", + SeenAt: time.Unix(1, 0).UTC(), + Labels: map[string]string{"payload": strings.Repeat(value, 4096)}, + } +} + +// RegisterNode must leave a complete old or new registration across a process +// crash at either side of the publication rename (V1-SPEC r2.46). +func TestNodeInventoryCrashKeepsCompleteRegistration(t *testing.T) { + for _, phase := range []string{"before-rename", "after-rename"} { + t.Run(phase, func(t *testing.T) { + dir := t.TempDir() + s, err := OpenNodeInventoryStore(dir) + if err != nil { + t.Fatal(err) + } + old, next := crashNodeRegistration(false), crashNodeRegistration(true) + if _, err := s.RegisterNode(old); err != nil { + t.Fatal(err) + } + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) + defer cancel() + cmd := exec.CommandContext(ctx, os.Args[0], "-test.run=^TestNodeInventoryCrashChild$", "-test.timeout=8s") + cmd.Env = append(os.Environ(), "NODE_INVENTORY_CRASH_DIR="+dir, "NODE_INVENTORY_CRASH_PHASE="+phase) + out, err := cmd.CombinedOutput() + var exit *exec.ExitError + if ctx.Err() != nil || !errors.As(err, &exit) || exit.ExitCode() != 73 { + t.Fatalf("crash point not reached: err=%v context=%v output=%s", err, ctx.Err(), out) + } + raw, err := os.ReadFile(filepath.Join(dir, "node-a.json")) + if err != nil { + t.Fatalf("crash lost the committed registration: %v", err) + } + var got NodeRegistration + if err := json.Unmarshal(raw, &got); err != nil { + t.Fatalf("crash left a partial registration: %v", err) + } + if !reflect.DeepEqual(got, old) && !reflect.DeepEqual(got, next) { + t.Fatal("crash left neither the complete old nor the complete new registration") + } + want := old + if phase == "after-rename" { + want = next + } + if !reflect.DeepEqual(got, want) { + t.Fatalf("wrong generation at %s", phase) + } + reopened, err := OpenNodeInventoryStore(dir) + if err != nil { + t.Fatal(err) + } + if got, ok := reopened.GetNodeSnapshot("node-a"); !ok || !reflect.DeepEqual(got, want) { + t.Fatal("reopened snapshot lost the complete registration") + } + }) + } +} + +func TestNodeInventoryCrashChild(t *testing.T) { + dir := os.Getenv("NODE_INVENTORY_CRASH_DIR") + if dir == "" { + t.Skip("subprocess crash injector") + } + s, err := OpenNodeInventoryStore(dir) + if err != nil { + t.Fatal(err) + } + s.persistHookForTest = func(phase string) { + if phase == os.Getenv("NODE_INVENTORY_CRASH_PHASE") { + os.Exit(73) // abrupt exit: no deferred cleanup or close + } + } + if _, err := s.RegisterNode(crashNodeRegistration(true)); err != nil { + t.Fatal(err) + } + t.Fatal("write returned without reaching the crash point") +}