mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-04 18:17:16 +01:00
Add neutral metadata factory admission and recording-hook RED controls
This commit is contained in:
1 parent
9c29ee9c42
commit
7583fbcb18
4 files changed
+299
-4
No files matched your search
@@ -0,0 +1,82 @@
|
||||
package storage
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"math"
|
||||
"os"
|
||||
)
|
||||
|
||||
// BlockMetadata is one optional companion to extent data. Write and verify
|
||||
// run under the existing extent stripe; the checkpoint barrier runs under
|
||||
// checkpoint locking. Hooks must not re-enter the store or acquire its locks.
|
||||
// A successful barrier promises metadata is durable before WAL recycling.
|
||||
type BlockMetadata interface {
|
||||
OnExtentWrite(lba uint32, data []byte) error
|
||||
VerifyExtent(lba uint32, data []byte) error
|
||||
BeforeCheckpointAdvance(lsn uint64) error
|
||||
}
|
||||
|
||||
// BlockMetadataFile is the store's own descriptor, not a separately opened
|
||||
// file. In read-only mode writes and Sync are forbidden by the caller contract
|
||||
// (the underlying descriptor is also O_RDONLY). The factory must not close it.
|
||||
type BlockMetadataFile interface {
|
||||
io.ReaderAt
|
||||
io.WriterAt
|
||||
Sync() error
|
||||
}
|
||||
|
||||
type BlockMetadataLayout struct {
|
||||
ImplVersion uint32
|
||||
BlockSize uint32
|
||||
NumBlocks uint32
|
||||
ExtentEnd int64
|
||||
Creating bool
|
||||
ReadOnly bool
|
||||
}
|
||||
|
||||
type BlockMetadataBinding struct {
|
||||
ImplVersion uint32
|
||||
AppendedBytes uint64
|
||||
Hook BlockMetadata
|
||||
}
|
||||
|
||||
// BlockMetadataFactory binds a single implementation before background work
|
||||
// or recovery begins. On create it declares a layout without writing bytes;
|
||||
// on open it must accept exactly the stored version. No registry or hook chain.
|
||||
type BlockMetadataFactory func(BlockMetadataFile, BlockMetadataLayout) (BlockMetadataBinding, error)
|
||||
type WALStoreOptions struct{ MetadataFactory BlockMetadataFactory }
|
||||
|
||||
func bindBlockMetadata(f *os.File, sb *superblock, opts WALStoreOptions, creating, readonly bool) (BlockMetadata, int64, error) {
|
||||
if sb.WALOffset > math.MaxInt64 || sb.WALSize > math.MaxInt64-sb.WALOffset || sb.VolumeSize > math.MaxInt64-sb.WALOffset-sb.WALSize {
|
||||
return nil, 0, fmt.Errorf("storage: extent size overflow")
|
||||
}
|
||||
base := sb.WALOffset + sb.WALSize + sb.VolumeSize
|
||||
binding := BlockMetadataBinding{ImplVersion: WALStoreImplVersion}
|
||||
if opts.MetadataFactory != nil {
|
||||
var err error
|
||||
binding, err = opts.MetadataFactory(f, BlockMetadataLayout{ImplVersion: sb.ImplVersion, BlockSize: sb.BlockSize, NumBlocks: uint32(sb.VolumeSize / uint64(sb.BlockSize)), ExtentEnd: int64(base), Creating: creating, ReadOnly: readonly})
|
||||
if err != nil {
|
||||
return nil, 0, fmt.Errorf("storage: bind block metadata: %w", err)
|
||||
}
|
||||
}
|
||||
if binding.ImplVersion == 0 || (!creating && binding.ImplVersion != sb.ImplVersion) || (binding.ImplVersion != WALStoreImplVersion && binding.Hook == nil) {
|
||||
return nil, 0, fmt.Errorf("storage: unsupported walstore implementation version %d without accepting metadata factory", sb.ImplVersion)
|
||||
}
|
||||
if binding.AppendedBytes > math.MaxInt64-base {
|
||||
return nil, 0, fmt.Errorf("storage: metadata region size overflow")
|
||||
}
|
||||
size := int64(base + binding.AppendedBytes)
|
||||
if creating {
|
||||
sb.ImplVersion = binding.ImplVersion
|
||||
} else {
|
||||
info, err := f.Stat()
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
if info.Size() < size {
|
||||
return nil, 0, fmt.Errorf("storage: incomplete metadata layout: %w", io.ErrUnexpectedEOF)
|
||||
}
|
||||
}
|
||||
return binding.Hook, size, nil
|
||||
}
|
||||
@@ -0,0 +1,178 @@
|
||||
package storage
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type recordingMetadata struct {
|
||||
file BlockMetadataFile
|
||||
end int64
|
||||
writes, reads, barriers int
|
||||
writeErr, readErr, barrierErr error
|
||||
before func(uint64)
|
||||
}
|
||||
|
||||
func (h *recordingMetadata) OnExtentWrite(uint32, []byte) error { h.writes++; return h.writeErr }
|
||||
func (h *recordingMetadata) VerifyExtent(uint32, []byte) error { h.reads++; return h.readErr }
|
||||
func (h *recordingMetadata) BeforeCheckpointAdvance(lsn uint64) error {
|
||||
h.barriers++
|
||||
if h.before != nil {
|
||||
h.before(lsn)
|
||||
}
|
||||
if h.barrierErr != nil {
|
||||
return h.barrierErr
|
||||
}
|
||||
if _, err := h.file.WriteAt([]byte{1}, h.end); err != nil {
|
||||
return err
|
||||
}
|
||||
return h.file.Sync()
|
||||
}
|
||||
func recordingOptions(h *recordingMetadata, version uint32) WALStoreOptions {
|
||||
return WALStoreOptions{MetadataFactory: func(f BlockMetadataFile, l BlockMetadataLayout) (BlockMetadataBinding, error) {
|
||||
h.file = f
|
||||
h.end = l.ExtentEnd
|
||||
return BlockMetadataBinding{ImplVersion: version, AppendedBytes: 1, Hook: h}, nil
|
||||
}}
|
||||
}
|
||||
func metadataStore(t *testing.T, h *recordingMetadata) (*WALStore, string) {
|
||||
t.Helper()
|
||||
p := filepath.Join(t.TempDir(), "store")
|
||||
s, err := CreateWALStoreWithOptions(p, 16, 4096, recordingOptions(h, WALStoreImplVersion))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { s.Close() })
|
||||
s.flusher.Stop()
|
||||
return s, p
|
||||
}
|
||||
func TestBlockMetadata_DurableBeforeTail(t *testing.T) {
|
||||
h := &recordingMetadata{}
|
||||
s, _ := metadataStore(t, h)
|
||||
if _, err := s.Write(3, makeBlock(4096, 0x31)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := s.Sync(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
cp, tail := s.CheckpointLSN(), walLogicalTail(s)
|
||||
h.before = func(uint64) {
|
||||
if s.checkpointLSN != cp || s.wal.logicalTail != tail {
|
||||
t.Error("tail/checkpoint advanced before hook durability")
|
||||
}
|
||||
}
|
||||
if err := s.flusher.flushOnce(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if h.writes != 1 || h.barriers != 1 || s.CheckpointLSN() <= cp || walLogicalTail(s) <= tail {
|
||||
t.Fatalf("hook not ordered: writes=%d barriers=%d checkpoint=%d", h.writes, h.barriers, s.CheckpointLSN())
|
||||
}
|
||||
b := []byte{0}
|
||||
if _, err := s.fd.ReadAt(b, h.end); err != nil || b[0] != 1 {
|
||||
t.Fatalf("durable marker absent: %v %v", b, err)
|
||||
}
|
||||
}
|
||||
func TestBlockMetadata_BarrierErrorBlocksCheckpointAndDirectFrontier(t *testing.T) {
|
||||
for _, direct := range []bool{false, true} {
|
||||
t.Run(map[bool]string{false: "WAL", true: "BASE"}[direct], func(t *testing.T) {
|
||||
sentinel := errors.New("metadata persistence refused")
|
||||
h := &recordingMetadata{barrierErr: sentinel}
|
||||
s, _ := metadataStore(t, h)
|
||||
cp, tail := s.CheckpointLSN(), walLogicalTail(s)
|
||||
if direct {
|
||||
if err := s.WriteExtentDirect(3, makeBlock(4096, 0x32)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
s.AdvanceFrontier(7)
|
||||
} else {
|
||||
if _, err := s.Write(3, makeBlock(4096, 0x32)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := s.Sync(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
var err error
|
||||
if direct {
|
||||
_, err = s.Sync()
|
||||
} else {
|
||||
err = s.flusher.flushOnce()
|
||||
}
|
||||
if !errors.Is(err, sentinel) || s.CheckpointLSN() != cp || walLogicalTail(s) != tail {
|
||||
t.Fatalf("hook error recycled: err=%v checkpoint=%d tail=%d", err, s.CheckpointLSN(), walLogicalTail(s))
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
func TestBlockMetadata_WriteAndVerifyRouting(t *testing.T) {
|
||||
h := &recordingMetadata{}
|
||||
s, p := metadataStore(t, h)
|
||||
sentinel := errors.New("metadata refusal")
|
||||
h.writeErr = sentinel
|
||||
if err := s.WriteExtentDirect(3, makeBlock(4096, 0x33)); !errors.Is(err, sentinel) {
|
||||
t.Fatalf("write hook bypassed: %v", err)
|
||||
}
|
||||
h.writeErr = nil
|
||||
if err := s.WriteExtentDirect(3, makeBlock(4096, 0x33)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
h.readErr = sentinel
|
||||
if got, err := s.Read(3); !errors.Is(err, sentinel) || len(got) != 0 {
|
||||
t.Fatalf("verify bypassed: %d %v", len(got), err)
|
||||
}
|
||||
h.readErr = nil
|
||||
if got, err := s.Read(3); err != nil || !bytes.Equal(got, makeBlock(4096, 0x33)) {
|
||||
t.Fatalf("positive read: %v", err)
|
||||
}
|
||||
if err := s.Close(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
rHook := &recordingMetadata{readErr: sentinel}
|
||||
r, err := OpenReadOnlyWithOptions(p, recordingOptions(rHook, WALStoreImplVersion))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer r.Close()
|
||||
if got, err := r.Read(3); !errors.Is(err, sentinel) || len(got) != 0 || rHook.reads != 1 {
|
||||
t.Fatalf("readonly verify bypassed: %d %v calls=%d", len(got), err, rHook.reads)
|
||||
}
|
||||
}
|
||||
func TestBlockMetadata_UnsupportedFormatRequiresFactory(t *testing.T) {
|
||||
p := filepath.Join(t.TempDir(), "store")
|
||||
h := &recordingMetadata{}
|
||||
s, err := CreateWALStoreWithOptions(p, 16, 4096, recordingOptions(h, 77))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err = s.Close(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
before, err := os.ReadFile(p)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if x, err := OpenWALStore(p); err == nil {
|
||||
x.Close()
|
||||
t.Fatal("unknown format silently opened without hook")
|
||||
}
|
||||
if x, err := OpenReadOnly(p); err == nil {
|
||||
x.Close()
|
||||
t.Fatal("unknown readonly format silently opened without hook")
|
||||
}
|
||||
after, _ := os.ReadFile(p)
|
||||
if !bytes.Equal(before, after) {
|
||||
t.Fatal("format refusal mutated file")
|
||||
}
|
||||
x, err := OpenWALStoreWithOptions(p, recordingOptions(&recordingMetadata{}, 77))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
x.Close()
|
||||
if x, err := OpenWALStoreWithOptions(p, recordingOptions(&recordingMetadata{}, 78)); err == nil {
|
||||
x.Close()
|
||||
t.Fatal("factory accepted a different stored version")
|
||||
}
|
||||
}
|
||||
@@ -43,6 +43,8 @@ import (
|
||||
// - Online resize
|
||||
// - Compaction (the WAL is bounded but periodic checkpoints free space)
|
||||
type WALStore struct {
|
||||
metadata BlockMetadata // immutable before publication
|
||||
|
||||
path string
|
||||
fd *os.File
|
||||
sb *superblock
|
||||
@@ -142,6 +144,10 @@ type WALStore struct {
|
||||
// unit (default 4096 if zero). The on-disk file is preallocated to
|
||||
// hold the superblock + WAL region + a fully addressable extent.
|
||||
func CreateWALStore(path string, numBlocks uint32, blockSize int) (*WALStore, error) {
|
||||
return CreateWALStoreWithOptions(path, numBlocks, blockSize, WALStoreOptions{})
|
||||
}
|
||||
|
||||
func CreateWALStoreWithOptions(path string, numBlocks uint32, blockSize int, opts WALStoreOptions) (*WALStore, error) {
|
||||
if blockSize == 0 {
|
||||
blockSize = DefaultBlockSize
|
||||
}
|
||||
@@ -166,7 +172,11 @@ func CreateWALStore(path string, numBlocks uint32, blockSize int) (*WALStore, er
|
||||
}
|
||||
sb.CreatedAt = uint64(time.Now().UnixNano())
|
||||
|
||||
totalSize := int64(superblockSize) + int64(sb.WALSize) + int64(sb.VolumeSize)
|
||||
metadata, totalSize, err := bindBlockMetadata(f, &sb, opts, true, false)
|
||||
if err != nil {
|
||||
_ = f.Close()
|
||||
return nil, err
|
||||
}
|
||||
if err := f.Truncate(totalSize); err != nil {
|
||||
_ = f.Close()
|
||||
return nil, fmt.Errorf("storage: preallocate %d bytes: %w", totalSize, err)
|
||||
@@ -183,12 +193,16 @@ func CreateWALStore(path string, numBlocks uint32, blockSize int) (*WALStore, er
|
||||
_ = f.Close()
|
||||
return nil, fmt.Errorf("storage: fsync after create: %w", err)
|
||||
}
|
||||
return openInitialized(path, f, &sb)
|
||||
return openInitializedWithMetadata(path, f, &sb, metadata)
|
||||
}
|
||||
|
||||
// OpenWALStore opens an existing store file WITHOUT recovering it.
|
||||
// Call Recover() before any Read/Write to replay the WAL.
|
||||
func OpenWALStore(path string) (*WALStore, error) {
|
||||
return OpenWALStoreWithOptions(path, WALStoreOptions{})
|
||||
}
|
||||
|
||||
func OpenWALStoreWithOptions(path string, opts WALStoreOptions) (*WALStore, error) {
|
||||
f, err := os.OpenFile(path, os.O_RDWR, 0o644)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("storage: open %s: %w", path, err)
|
||||
@@ -206,7 +220,12 @@ func OpenWALStore(path string) (*WALStore, error) {
|
||||
_ = f.Close()
|
||||
return nil, err
|
||||
}
|
||||
return openInitialized(path, f, &sb)
|
||||
metadata, _, err := bindBlockMetadata(f, &sb, opts, false, false)
|
||||
if err != nil {
|
||||
_ = f.Close()
|
||||
return nil, err
|
||||
}
|
||||
return openInitializedWithMetadata(path, f, &sb, metadata)
|
||||
}
|
||||
|
||||
// openInitialized is the shared constructor body used by both Create
|
||||
@@ -214,9 +233,14 @@ func OpenWALStore(path string) (*WALStore, error) {
|
||||
// the dirty map, and the group committer; it does NOT replay the WAL
|
||||
// (that is Recover's job).
|
||||
func openInitialized(path string, f *os.File, sb *superblock) (*WALStore, error) {
|
||||
return openInitializedWithMetadata(path, f, sb, nil)
|
||||
}
|
||||
|
||||
func openInitializedWithMetadata(path string, f *os.File, sb *superblock, metadata BlockMetadata) (*WALStore, error) {
|
||||
dm := newDirtyMap(64) // 64 shards is plenty for Phase 07 demo workloads
|
||||
|
||||
s := &WALStore{
|
||||
metadata: metadata,
|
||||
path: path,
|
||||
fd: f,
|
||||
sb: sb,
|
||||
|
||||
@@ -36,6 +36,8 @@ import (
|
||||
// methods (Write, Sync, Recover, Trim) are exposed; callers that
|
||||
// need them must use OpenWALStore + Recover.
|
||||
type WALStoreReader struct {
|
||||
metadata BlockMetadata
|
||||
|
||||
mu sync.Mutex
|
||||
fd *os.File
|
||||
sb *superblock
|
||||
@@ -57,6 +59,10 @@ type WALStoreReader struct {
|
||||
// would race the dirty-map snapshot. Typical use: stop the daemon
|
||||
// (or pause writes) before invoking OpenReadOnly.
|
||||
func OpenReadOnly(path string) (*WALStoreReader, error) {
|
||||
return OpenReadOnlyWithOptions(path, WALStoreOptions{})
|
||||
}
|
||||
|
||||
func OpenReadOnlyWithOptions(path string, opts WALStoreOptions) (*WALStoreReader, error) {
|
||||
f, err := os.OpenFile(path, os.O_RDONLY, 0)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("storage: OpenReadOnly %s: %w", path, err)
|
||||
@@ -74,6 +80,11 @@ func OpenReadOnly(path string) (*WALStoreReader, error) {
|
||||
_ = f.Close()
|
||||
return nil, err
|
||||
}
|
||||
metadata, _, err := bindBlockMetadata(f, &sb, opts, false, true)
|
||||
if err != nil {
|
||||
_ = f.Close()
|
||||
return nil, err
|
||||
}
|
||||
dm := newDirtyMap(64)
|
||||
// Replay WAL into the dirty map. recoverWAL is a pure read pass
|
||||
// over the WAL frames — no writes back to the fd.
|
||||
@@ -81,7 +92,7 @@ func OpenReadOnly(path string) (*WALStoreReader, error) {
|
||||
_ = f.Close()
|
||||
return nil, fmt.Errorf("storage: OpenReadOnly recover: %w", err)
|
||||
}
|
||||
return &WALStoreReader{fd: f, sb: &sb, dm: dm}, nil
|
||||
return &WALStoreReader{metadata: metadata, fd: f, sb: &sb, dm: dm}, nil
|
||||
}
|
||||
|
||||
// Read returns the current bytes at lba — same merge logic the
|
||||
|
||||
Reference in new issue
Block a user