mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-06 02:57:37 +01:00
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 <noreply@anthropic.com>
This commit is contained in:
1 parent
28f983bb4c
commit
fcf5dc72d7
5 files changed
+75
-15
No files matched your search
@@ -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`.
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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()
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user