From fcf5dc72d7ead9e0b46b530ba7d3df7334cfbf96 Mon Sep 17 00:00:00 2001 From: pingqiu Date: Thu, 19 Nov 2026 02:49:51 -0800 Subject: [PATCH] storage: decide unheld on one locked descriptor Fold of the #258 review: - the probe opens the volume once, locks that descriptor first, then identifies the same open file as v3: a path replaced between two opens can no longer yield a false unheld, and a locked file is held whatever it is; - locking before reading also fixes Windows, where an owner's byte lock refused the probe's earlier read (it reported unknown, not held); - the test reaps its holder process on every path; the CLI test closes its store unconditionally; - the usage text adds that the probe is momentary and reserves nothing. Co-Authored-By: Claude Opus 5.5 --- seaweed-block/cmd/sw-block/volume_unheld.go | 4 +++ .../cmd/sw-block/volume_unheld_test.go | 9 ++++- seaweed-block/core/storage/volume_unheld.go | 30 +++++++++++----- .../core/storage/volume_unheld_test.go | 36 +++++++++++++++++-- seaweed-block/core/storage/walstore_v3.go | 11 ++++-- 5 files changed, 75 insertions(+), 15 deletions(-) diff --git a/seaweed-block/cmd/sw-block/volume_unheld.go b/seaweed-block/cmd/sw-block/volume_unheld.go index 8d72c84b1f..81d2c12d0e 100644 --- a/seaweed-block/cmd/sw-block/volume_unheld.go +++ b/seaweed-block/cmd/sw-block/volume_unheld.go @@ -21,6 +21,10 @@ This proof is per host and for local filesystems only. It says nothing about another host, and nothing on a network or shared mount. It does not prove the owner process is dead: host cleanup must require BOTH this command's exit 0 AND the host tooling's own check that the recorded owner process is gone. + +The probe holds the lock only for an instant and reserves nothing: it does not +keep a new owner out after it returns, and an owner opening the volume at that +instant is refused. Run it only when the owner is believed gone. ` // runOpsVolumeUnheld is `sw-block ops volume-unheld`. diff --git a/seaweed-block/cmd/sw-block/volume_unheld_test.go b/seaweed-block/cmd/sw-block/volume_unheld_test.go index bedae3cef7..868f276778 100644 --- a/seaweed-block/cmd/sw-block/volume_unheld_test.go +++ b/seaweed-block/cmd/sw-block/volume_unheld_test.go @@ -19,6 +19,12 @@ func TestOpsVolumeUnheldExitCodes(t *testing.T) { if err != nil { t.Fatal(err) } + closed := false + t.Cleanup(func() { + if !closed { + _ = s.Close() + } + }) junk := filepath.Join(t.TempDir(), "junk.bin") if err := os.WriteFile(junk, make([]byte, 64<<10), 0o644); err != nil { t.Fatal(err) @@ -31,6 +37,7 @@ func TestOpsVolumeUnheldExitCodes(t *testing.T) { if code, out, _ := runIt("--path", path); code != 1 || !strings.HasPrefix(out, "held:") { t.Fatalf("open store: exit %d %q; want 1 held", code, out) } + closed = true if err := s.Close(); err != nil { t.Fatal(err) } @@ -48,7 +55,7 @@ func TestOpsVolumeUnheldExitCodes(t *testing.T) { } } _, _, usage := runIt() - for _, want := range []string{"per host", "local filesystems only", "network or shared mount", "BOTH"} { + for _, want := range []string{"per host", "local filesystems only", "network or shared mount", "BOTH", "reserves nothing", "is refused"} { if !strings.Contains(usage, want) { t.Fatalf("usage lacks %q:\n%s", want, usage) } diff --git a/seaweed-block/core/storage/volume_unheld.go b/seaweed-block/core/storage/volume_unheld.go index c42e74b58e..b3f9709a9e 100644 --- a/seaweed-block/core/storage/volume_unheld.go +++ b/seaweed-block/core/storage/volume_unheld.go @@ -19,26 +19,38 @@ var ErrVolumeHeld = errors.New("storage: volume held by a live owner on this hos // // The proof is per host and for local filesystems only: it says nothing // about a process on another host, and nothing on a network or shared mount, -// where advisory lock semantics vary. It is not proof the owner process is -// dead; host cleanup must check that separately. The probe holds the lock -// only for an instant, but an owner opening the volume in that instant is +// where advisory lock semantics vary. It covers the file at path when the +// probe opens it, not a file renamed away earlier. It is not proof the owner +// process is dead; host cleanup must check that separately. The probe holds +// the lock only for an instant and leaves no reservation behind, so it does +// not keep a new owner out; an owner opening the volume in that instant is // refused with ErrWALStoreLocked: probe only when the owner is believed gone. func ProbeVolumeUnheld(path string) error { - // Only a v3 owner takes the lock; for any other file a free lock proves - // nothing. - if err := ProbeWALStoreV3(path); err != nil { - return fmt.Errorf("storage: %s is not a v3 volume, unheld cannot be proven: %w", path, err) - } + // One descriptor for both steps, locked first: the answer is about the + // file this descriptor opened, whatever the path names afterwards, and + // on Windows an owner's byte lock would refuse a read made before ours. f, err := os.Open(path) if err != nil { return err } defer f.Close() // closing releases the probe's lock + if hook := testProbeUnheldAfterOpen; hook != nil { + hook() + } switch err := LockWALStoreFile(f); { case errors.Is(err, ErrWALStoreLocked): - return ErrVolumeHeld + return ErrVolumeHeld // held, whatever the file is: the safe answer case err != nil: return err } + // Only a v3 owner takes the lock; for any other file a free lock proves + // nothing. + if err := probeWALStoreV3At(f); err != nil { + return fmt.Errorf("storage: %s is not a v3 volume, unheld cannot be proven: %w", path, err) + } return nil } + +// testProbeUnheldAfterOpen, test-only, runs in ProbeVolumeUnheld after the +// open, before the lock. +var testProbeUnheldAfterOpen func() diff --git a/seaweed-block/core/storage/volume_unheld_test.go b/seaweed-block/core/storage/volume_unheld_test.go index 97fcee13aa..43014a98b6 100644 --- a/seaweed-block/core/storage/volume_unheld_test.go +++ b/seaweed-block/core/storage/volume_unheld_test.go @@ -5,6 +5,7 @@ import ( "os" "os/exec" "path/filepath" + "runtime" "strings" "testing" "time" @@ -107,7 +108,8 @@ func TestProbeVolumeUnheldHolderProcessDies(t *testing.T) { t.Fatal(err) } exited := make(chan struct{}) - t.Cleanup(func() { // kill and reap the holder, bounded + go func() { _ = cmd.Wait(); close(exited) }() // reaped on every path + t.Cleanup(func() { // kill the holder and join its reaper, bounded _ = cmd.Process.Kill() select { case <-exited: @@ -132,7 +134,6 @@ func TestProbeVolumeUnheldHolderProcessDies(t *testing.T) { if err := ProbeVolumeUnheld(path); !errors.Is(err, ErrVolumeHeld) { t.Fatalf("held by a live process: %v; want ErrVolumeHeld", err) } - go func() { _ = cmd.Wait(); close(exited) }() if err := cmd.Process.Kill(); err != nil { t.Fatal(err) } @@ -145,3 +146,34 @@ func TestProbeVolumeUnheldHolderProcessDies(t *testing.T) { t.Fatalf("after the holder died: %v; want unheld", err) } } + +// The answer is about the file the probe opened: a held volume whose path is +// replaced (by an unlocked non-v3 file) between the probe's open and its lock +// is still reported held, never unheld. +func TestProbeVolumeUnheldDecidesOnTheOpenedFile(t *testing.T) { + if runtime.GOOS == "windows" { + t.Skip("an open file cannot be renamed over on Windows") + } + path := createClosedV3Volume(t) + s, err := OpenWALStore(path) + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { _ = s.Close() }) + junk := filepath.Join(filepath.Dir(path), "junk.bin") + if err := os.WriteFile(junk, make([]byte, 64<<10), 0o644); err != nil { + t.Fatal(err) + } + replaced := false + testProbeUnheldAfterOpen = func() { + replaced = os.Rename(junk, path) == nil + } + t.Cleanup(func() { testProbeUnheldAfterOpen = nil }) + err = ProbeVolumeUnheld(path) + if !replaced { + t.Fatal("fixture: the path was not replaced after the open") + } + if !errors.Is(err, ErrVolumeHeld) { + t.Fatalf("held volume replaced at its path mid-probe: %v; want ErrVolumeHeld", err) + } +} diff --git a/seaweed-block/core/storage/walstore_v3.go b/seaweed-block/core/storage/walstore_v3.go index ea7f8d68fd..5b395c8fff 100644 --- a/seaweed-block/core/storage/walstore_v3.go +++ b/seaweed-block/core/storage/walstore_v3.go @@ -165,14 +165,19 @@ func ProbeWALStoreV3(path string) error { return err } defer f.Close() + return probeWALStoreV3At(f) +} + +// probeWALStoreV3At is ProbeWALStoreV3 on an already open file. +func probeWALStoreV3At(r io.ReaderAt) error { a, b := make([]byte, slotSizeV3), make([]byte, slotSizeV3) - if _, err := f.ReadAt(a, 0); err != nil { + if _, err := r.ReadAt(a, 0); err != nil { return err } - if _, err := f.ReadAt(b, slotBOffsetV3); err != nil { + if _, err := r.ReadAt(b, slotBOffsetV3); err != nil { return err } - _, _, err = selectSlotV3(a, b, nil) + _, _, err := selectSlotV3(a, b, nil) return err }