diff --git a/seaweed-block/core/frontend/durable/provider.go b/seaweed-block/core/frontend/durable/provider.go index fba409ccf4..eedba71cc7 100644 --- a/seaweed-block/core/frontend/durable/provider.go +++ b/seaweed-block/core/frontend/durable/provider.go @@ -33,6 +33,7 @@ import ( "os" "path/filepath" "sort" + "strings" "sync" "time" @@ -557,6 +558,19 @@ func (p *DurableProvider) openOrCreate(volumeID string) (*volHandle, error) { if err != nil { return nil, fmt.Errorf("durable: stat %s: %w", path, err) } + if p.cfg.Impl == ImplAuto { + // A walstore-chunk volume shares walstore's on-disk format, and its + // authoritative bytes may live only in the remote HEAD. Opened as + // plain walstore it would serve local (possibly absent) extents, so + // auto refuses any root carrying chunk metadata; it never deletes + // or converts it. + if side, err := chunkSidecars(path); err != nil { + return nil, err + } else if len(side) > 0 { + return nil, fmt.Errorf("%w: %s is a chunk-managed volume (%s present); auto cannot open it: set --durable-impl=walstore-chunk with its remote configuration", + ErrImplKindMismatch, path, strings.Join(side, ", ")) + } + } if exists { impl, err := p.verifyImplOnDisk(path) @@ -858,5 +872,29 @@ func fileExists(path string) (bool, error) { return false, err } +// chunkSidecars lists the walstore-chunk metadata files beside a volume +// file: everything named .chunk* (the fresh marker, the dirty +// bitmap and its deltas, the presence map). Directories are ignored. +func chunkSidecars(path string) ([]string, error) { + dir, base := filepath.Split(path) + if dir == "" { + dir = "." + } + entries, err := os.ReadDir(dir) + if err != nil { + if os.IsNotExist(err) { + return nil, nil + } + return nil, fmt.Errorf("durable: list %s for chunk metadata: %w", dir, err) + } + var out []string + for _, e := range entries { + if !e.IsDir() && strings.HasPrefix(e.Name(), base+".chunk") { + out = append(out, e.Name()) + } + } + return out, nil +} + // Compile-time check. var _ frontend.Provider = (*DurableProvider)(nil) diff --git a/seaweed-block/core/frontend/durable/provider_auto_chunk_test.go b/seaweed-block/core/frontend/durable/provider_auto_chunk_test.go new file mode 100644 index 0000000000..4c0286cb34 --- /dev/null +++ b/seaweed-block/core/frontend/durable/provider_auto_chunk_test.go @@ -0,0 +1,100 @@ +package durable_test + +import ( + "bytes" + "context" + "errors" + "os" + "path/filepath" + "testing" + + "github.com/seaweedfs/seaweed-block/core/frontend/durable" + "github.com/seaweedfs/seaweed-block/core/storage" +) + +// A walstore-chunk volume has walstore's on-disk format, and on a fresh host +// its published bytes may exist only in the remote HEAD. Auto must not open +// such a root as plain walstore (no read-miss or fencing hooks: it would +// serve the local, absent extent). It refuses, and the explicit +// walstore-chunk selector with the remote still reads the exact bytes. +func TestDurableProvider_AutoRefusesLazyChunkRoot(t *testing.T) { + remote := storage.NewMemChunkRemote() + view := v1View() + + // Host A publishes a nonzero block. + hostA := chunkProvider(t, t.TempDir(), remote, view, 0) + sA := openRecovered(t, hostA) + if _, err := sA.Write(33, block(0x33)); err != nil { + t.Fatal(err) + } + if err := hostA.MakeVolumeRemoteDurable(context.Background(), "v1", 1); err != nil { + t.Fatalf("MakeVolumeRemoteDurable: %v", err) + } + + // Host B attaches lazily from HEAD and closes before LBA 33 is fetched. + rootB := t.TempDir() + hostB := chunkProvider(t, rootB, remote, view, 0) + openRecovered(t, hostB) + if err := hostB.Close(); err != nil { + t.Fatalf("close host B: %v", err) + } + + // Restart host B with no selector: auto must refuse before any I/O. + auto := autoProvider(t, rootB, durable.ImplAuto) + if _, err := auto.EnsureStorage("v1"); err == nil { + got := []byte("not read") + if _, rerr := auto.RecoverVolume(context.Background(), "v1"); rerr == nil { + if s, serr := auto.EnsureStorage("v1"); serr == nil { + got, _ = s.Read(33) + } + } + t.Fatalf("auto opened a chunk-managed root as plain walstore; LBA 33 reads %x... (want HEAD byte 33)", got[:4]) + } else if !errors.Is(err, durable.ErrImplKindMismatch) { + t.Fatalf("auto on a chunk root: %v, want ErrImplKindMismatch", err) + } + _ = auto.Close() + + // The explicit selector with the remote reopens and reads HEAD's bytes. + explicit := chunkProvider(t, rootB, remote, view, 0) + s := openRecovered(t, explicit) + if got, err := s.Read(33); err != nil || !bytes.Equal(got, block(0x33)) { + t.Fatalf("explicit walstore-chunk reopen read: %v", err) + } +} + +// Any root with chunk metadata is refused under auto, not only a lazy one: +// a chunk volume created and attached on this host. +func TestDurableProvider_AutoRefusesAnyChunkRoot(t *testing.T) { + remote := storage.NewMemChunkRemote() + root := t.TempDir() + p := chunkProvider(t, root, remote, v1View(), 0) + openRecovered(t, p) + if err := p.Close(); err != nil { + t.Fatal(err) + } + auto := autoProvider(t, root, durable.ImplAuto) + if _, err := auto.EnsureStorage("v1"); !errors.Is(err, durable.ErrImplKindMismatch) { + t.Fatalf("auto on a root with chunk metadata: %v, want ErrImplKindMismatch", err) + } +} + +// A chunk fresh marker without a volume file (a chunk create interrupted +// before the file existed) is refused rather than turned into a new plain +// walstore volume, and the marker is left in place. +func TestDurableProvider_AutoRefusesStrayChunkFreshMarker(t *testing.T) { + root := t.TempDir() + marker := filepath.Join(root, "v1.bin.chunkfresh") + if err := os.WriteFile(marker, nil, 0o600); err != nil { + t.Fatal(err) + } + auto := autoProvider(t, root, durable.ImplAuto) + if _, err := auto.EnsureStorage("v1"); !errors.Is(err, durable.ErrImplKindMismatch) { + t.Fatalf("auto with a stray chunk fresh marker: %v, want ErrImplKindMismatch", err) + } + if _, err := os.Stat(filepath.Join(root, "v1.bin")); !os.IsNotExist(err) { + t.Fatalf("auto created a volume file beside a chunk marker: %v", err) + } + if _, err := os.Stat(marker); err != nil { + t.Fatalf("chunk fresh marker removed: %v", err) + } +} diff --git a/seaweed-block/core/frontend/durable/provider_auto_impl_test.go b/seaweed-block/core/frontend/durable/provider_auto_impl_test.go index 17025cb5f0..229997d61f 100644 --- a/seaweed-block/core/frontend/durable/provider_auto_impl_test.go +++ b/seaweed-block/core/frontend/durable/provider_auto_impl_test.go @@ -30,6 +30,7 @@ func autoProvider(t *testing.T, root string, impl durable.ImplName) *durable.Dur if err != nil { t.Fatalf("NewDurableProvider(%q): %v", impl, err) } + t.Cleanup(func() { _ = p.Close() }) return p }