Merge pull request #743 from seaweedfs/fix/reader-volume-id-race

fix(layout): guard the reader's volume id cache against concurrent reads
This commit is contained in:
Ping Qiu authored and GitHub committed 2026-11-28 23:57:06 -08:00
commit 58dd447b22
4 files changed
+75 -7

No files matched your search

@@ -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
}
@@ -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)
}
}
@@ -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)
}
+1
View File
@@ -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: