From 7c3fdd2762a586edec1431577d865da8fc77e62f Mon Sep 17 00:00:00 2001 From: pingqiu Date: Sat, 28 Nov 2026 22:20:26 -0800 Subject: [PATCH 1/2] fix(layout): guard the reader's volume id cache against concurrent reads A cut's parallel uploads share one Reader, and its volume id cache was a plain map read and written without a lock: a data race under -race. A leaf mutex now guards the map, held only around its read and write, never across a fetch. The abort test's blocking store no longer writes its channel field from the commit's goroutine while the test reads it; a sync.Once holds the first commit instead. Co-Authored-By: Claude Opus 5.5 --- .../core/frontend/chunkstore/layout/read.go | 17 ++++-- .../chunkstore/layout/read_concurrent_test.go | 52 +++++++++++++++++++ .../frontend/chunkstore/layout_remote_test.go | 12 +++-- 3 files changed, 74 insertions(+), 7 deletions(-) create mode 100644 seaweed-block/core/frontend/chunkstore/layout/read_concurrent_test.go diff --git a/seaweed-block/core/frontend/chunkstore/layout/read.go b/seaweed-block/core/frontend/chunkstore/layout/read.go index a6a2cece3e..9035314d43 100644 --- a/seaweed-block/core/frontend/chunkstore/layout/read.go +++ b/seaweed-block/core/frontend/chunkstore/layout/read.go @@ -6,6 +6,7 @@ import ( "fmt" "path" "sort" + "sync" ) // ErrMissing reports an object a root reaches that is not in the store: a @@ -29,7 +30,12 @@ type Fetch func(ctx context.Context, p string) (data []byte, found bool, err err type Reader struct { base, vol string fetch Fetch - ids map[string]VolumeID // VOLUME ids read so far, by volume name + + // mu guards ids: a Reader is shared by concurrent reads (a cut's + // parallel uploads). It is a leaf lock, held only around the map's read + // and write, never across a fetch. + mu sync.Mutex + ids map[string]VolumeID // VOLUME ids read so far, by volume name } // NewReader returns a Reader of vol under base. @@ -46,7 +52,10 @@ func NewReader(base, vol string, fetch Fetch) (*Reader, error) { // VolumeID returns the id in the VOLUME record of the named volume, read // once per Reader. A missing VOLUME is ErrMissing. func (r *Reader) VolumeID(ctx context.Context, vol string) (VolumeID, error) { - if id, ok := r.ids[vol]; ok { + r.mu.Lock() + id, ok := r.ids[vol] + r.mu.Unlock() + if ok { return id, nil } p := path.Join(r.base, vol, VolumeName) @@ -61,7 +70,9 @@ func (r *Reader) VolumeID(ctx context.Context, vol string) (VolumeID, error) { if err != nil { return VolumeID{}, fmt.Errorf("%w: %s: %v", ErrCorrupt, p, err) } - r.ids[vol] = v.ID + r.mu.Lock() + r.ids[vol] = v.ID // concurrent first reads store the same id + r.mu.Unlock() return v.ID, nil } diff --git a/seaweed-block/core/frontend/chunkstore/layout/read_concurrent_test.go b/seaweed-block/core/frontend/chunkstore/layout/read_concurrent_test.go new file mode 100644 index 0000000000..6c24e6374e --- /dev/null +++ b/seaweed-block/core/frontend/chunkstore/layout/read_concurrent_test.go @@ -0,0 +1,52 @@ +package layout + +import ( + "context" + "sync" + "testing" +) + +// A Reader is shared by concurrent reads (a cut's parallel uploads resolve +// chunk owners through it): concurrent first reads of a volume's id are +// safe and agree. Run under -race, an unguarded id cache fails here. +func TestReaderVolumeIDIsSafeForConcurrentReads(t *testing.T) { + vols := map[string]Volume{} + for _, name := range []string{"v1", "v2", "v3"} { + vols[name] = Volume{ID: idFor(name), Name: name, Geometry: Geometry{SizeBytes: 64 * 4096, BlockSize: 4096, ChunkBytes: 4 * 4096}, Format: VolumeFormat} + } + fetch := func(_ context.Context, p string) ([]byte, bool, error) { + for name, v := range vols { + if p == "/base/"+name+"/"+VolumeName { + b, err := v.Encode() + return b, err == nil, err + } + } + return nil, false, nil + } + r, err := NewReader("/base", "v1", fetch) + if err != nil { + t.Fatal(err) + } + var wg sync.WaitGroup + errs := make(chan error, 64) + for i := 0; i < 64; i++ { + name := []string{"v1", "v2", "v3"}[i%3] + wg.Add(1) + go func() { + defer wg.Done() + id, err := r.VolumeID(context.Background(), name) + if err != nil { + errs <- err + return + } + if id != vols[name].ID { + t.Errorf("volume %s: id %v, want %v", name, id, vols[name].ID) + } + }() + } + wg.Wait() + close(errs) + for err := range errs { + t.Fatal(err) + } +} diff --git a/seaweed-block/core/frontend/chunkstore/layout_remote_test.go b/seaweed-block/core/frontend/chunkstore/layout_remote_test.go index d7431f0cf6..67689105d5 100644 --- a/seaweed-block/core/frontend/chunkstore/layout_remote_test.go +++ b/seaweed-block/core/frontend/chunkstore/layout_remote_test.go @@ -6,6 +6,7 @@ import ( "errors" "path/filepath" "strings" + "sync" "testing" "time" @@ -671,13 +672,16 @@ func TestLayoutRemote_AbortOpenSession(t *testing.T) { type blockingCommitStore struct { sessionStore entered, release chan struct{} + once sync.Once // the first commit is held; the fields are never written after setup } func (s *blockingCommitStore) UpdateHead(ctx context.Context, gen uint64, next layout.Head, cause layout.HeadWriteCause) error { - if next.Activity.Kind == layout.ActIdle && s.entered != nil { - close(s.entered) - s.entered = nil - <-s.release + if next.Activity.Kind == layout.ActIdle { + held := false + s.once.Do(func() { close(s.entered); held = true }) + if held { + <-s.release + } } return s.sessionStore.UpdateHead(ctx, gen, next, cause) } From 9410227d80384fba68e4ecedc5dd890aa899d6df Mon Sep 17 00:00:00 2001 From: pingqiu Date: Sat, 28 Nov 2026 22:22:04 -0800 Subject: [PATCH 2/2] docs(invariants): the reader is safe for concurrent reads Co-Authored-By: Claude Opus 5.5 --- seaweed-block/docs/design/invariants.md | 1 + 1 file changed, 1 insertion(+) diff --git a/seaweed-block/docs/design/invariants.md b/seaweed-block/docs/design/invariants.md index aa7a986d36..3ebab3a712 100644 --- a/seaweed-block/docs/design/invariants.md +++ b/seaweed-block/docs/design/invariants.md @@ -1136,6 +1136,7 @@ Notes: | `INV-SNAPK-START-POSITION-001` | A root carrying HEAD's claim id but synced below HEAD's published position is an old copy of that lineage and is refused as stale, at its own epoch and above, HEAD unchanged. | M9 start (before any transition) | `core/frontend/chunkstore/layout` `start.go` `Start` | `TestStart_OldCopyOfTheRightRootRefused`, `TestStart_FreshAcrossACrashContinuesTheRestore` | — (the snapshot kernel model is not in this repository) | ACTIVE | | `INV-SNAPK-START-CLAIM-001` | A claim is made only by Start, at an epoch above HEAD's, with the local root's claim id; a source scan refuses any other caller of RunClaim. | M9 any → idle (Claim) | `core/frontend/chunkstore/layout` `start.go` `Start`; (source scan) | `TestStart_FreshRootAtHigherEpochClaims`, `TestStart_Refusals`, `TestStart_InterruptAtTheClaim`, `TestGuard_Controls` | — (the snapshot kernel model is not in this repository) | ACTIVE | | `INV-SNAPK-STALE-HOLDER-001` | Every transition refuses an older epoch of the same server, the same epoch held by another server, and the same epoch and server with another claim id, in idle, snap and clean; a new transition must be added to the table. | M9 every transition | `core/frontend/chunkstore/layout` `Head.heldBy` and each transition | `TestTransitions_RefuseAStaleHolder` | — (the snapshot kernel model is not in this repository) | ACTIVE | +| `INV-SNAPK-READER-CONCURRENT-001` | A `Reader` is safe for concurrent reads: its cache of volume ids is guarded by a leaf lock held only around the cache, never across a fetch, so concurrent first reads of a volume's id agree and cannot fault the process. | Content-named layout (no state transition) | `core/frontend/chunkstore/layout` `read.go` `Reader.VolumeID` | `TestReaderVolumeIDIsSafeForConcurrentReads` (under `-race`) | — | ACTIVE | Notes: