fix(storage): hold the creator lock through descriptor publication and ownership transfer

This commit is contained in:
pingqiu committed 2026-11-18 02:04:10 -08:00
1 parent 169923df83
commit 833a97925f
3 files changed
+82 -11

No files matched your search

@@ -79,17 +79,26 @@ func TestMigrateV3RefusesAnOpenV3Volume(t *testing.T) {
}
t.Cleanup(func() { _ = s.Close() })
s.DisableAutoFlushForRecoveryTest()
// Windows byte-range locks reject reads through a second handle. Snapshot
// only while no owner is open, then hold the real owner for the CLI call.
if err := s.Close(); err != nil {
t.Fatal(err)
}
before := snapshotFiles(t, path)
s, err = storage.OpenMappedWALStoreV3Test(path)
if err != nil {
t.Fatal(err)
}
code, msg := migrate(t, path)
if code != 1 || !strings.Contains(msg, "locked") {
t.Fatalf("migration must refuse an open v3 volume: exit=%d message=%s", code, msg)
}
if !sameFiles(before, snapshotFiles(t, path)) {
t.Fatal("refused migration modified the open volume")
}
if err := s.Close(); err != nil {
t.Fatal(err)
}
if !sameFiles(before, snapshotFiles(t, path)) {
t.Fatal("refused migration modified the open volume")
}
if code, msg := migrate(t, path); code != 0 {
t.Fatalf("migration after owner close: exit=%d message=%s", code, msg)
}
@@ -2,6 +2,8 @@ package storage
import (
"errors"
"os"
"path/filepath"
"testing"
)
@@ -35,3 +37,48 @@ func TestWALStoreV3OwnerExcludesOpenAndReadOnly(t *testing.T) {
}
_ = reopened.Close()
}
func TestWALStoreV3CreatorKeepsOwnershipThroughPublication(t *testing.T) {
path := filepath.Join(t.TempDir(), "volume")
var createdFD *os.File
var stages int
testCreateOwnerV3 = func(fd *os.File, published bool) {
createdFD = fd
stages++
if other, err := openWALStoreV3ForTest(path); !errors.Is(err, ErrWALStoreLocked) {
if other != nil {
_ = other.Close()
}
t.Errorf("creation published=%v writable contender: %v", published, err)
}
if other, err := openReadOnlyV3ForTest(path, WALStoreOptions{}); !errors.Is(err, ErrWALStoreLocked) {
if other != nil {
_ = other.Close()
}
t.Errorf("creation published=%v readonly contender: %v", published, err)
}
}
t.Cleanup(func() { testCreateOwnerV3 = nil })
s, err := createMappedWALStoreV3ForTest(path, 16, 4096, 65536, WModeLagged, 4)
if err != nil {
t.Fatal(err)
}
defer s.Close()
if stages != 2 || s.fd != createdFD {
t.Errorf("creator must retain the same descriptor: stages=%d samefd=%v", stages, s.fd == createdFD)
}
if other, err := openWALStoreV3ForTest(path); !errors.Is(err, ErrWALStoreLocked) {
if other != nil {
_ = other.Close()
}
t.Fatalf("returned owner lost lock: %v", err)
}
if err := s.Close(); err != nil {
t.Fatal(err)
}
other, err := openWALStoreV3ForTest(path)
if err != nil {
t.Fatal(err)
}
_ = other.Close()
}
+23 -8
View File
@@ -175,6 +175,10 @@ func createMappedWALStoreV3ForTest(path string, numBlocks, blockSize uint32, wal
}
return createWALStoreV3MapAtCursorForTest(path, numBlocks, blockSize, walSize, mode, 0, chunkBlocks)
}
// testCreateOwnerV3 parks creation at ownership boundaries; nil in production.
var testCreateOwnerV3 func(*os.File, bool)
func createWALStoreV3MapAtCursorForTest(path string, numBlocks, blockSize uint32, walSize uint64, mode WMode, cursor, chunkBlocks uint64) (*WALStore, error) {
d := descriptorV3{
ImplKind: ImplKindWALStore, ImplVersion: walStoreImplVersion3,
@@ -195,6 +199,13 @@ func createWALStoreV3MapAtCursorForTest(path string, numBlocks, blockSize uint32
if err != nil {
return nil, err
}
if err := LockWALStoreFile(fd); err != nil {
_ = fd.Close()
return nil, err
}
if h := testCreateOwnerV3; h != nil {
h(fd, false)
}
err = fd.Truncate(int64(d.WALOffset + d.WALSize + d.VolumeSize))
if err == nil && chunkBlocks != 0 {
var m *chunkLSNMapV3
@@ -225,18 +236,16 @@ func createWALStoreV3MapAtCursorForTest(path string, numBlocks, blockSize uint32
if err == nil {
err = fd.Sync()
}
if cerr := fd.Close(); err == nil {
err = cerr
if err == nil {
if h := testCreateOwnerV3; h != nil {
h(fd, true)
}
}
if err != nil {
_ = fd.Close()
return nil, err
}
var s *WALStore
if chunkBlocks == 0 {
s, err = openUnmappedWALStoreV3ForTest(path)
} else {
s, err = openWALStoreV3ForTest(path)
}
s, err := openWALStoreV3File(path, fd, chunkBlocks != 0)
if err != nil {
return nil, err
}
@@ -267,6 +276,12 @@ func openWALStoreV3MapInternalForTest(path string, requireMap bool) (*WALStore,
_ = f.Close()
return nil, err
}
return openWALStoreV3File(path, f, requireMap)
}
// openWALStoreV3File consumes an already locked descriptor, including on error.
// Creation transfers ownership directly: no selectable-slot close/reopen gap.
func openWALStoreV3File(path string, f *os.File, requireMap bool) (*WALStore, error) {
a := make([]byte, slotSizeV3)
b := make([]byte, slotSizeV3)
_, errA := f.ReadAt(a, slotAOffsetV3)