mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-05 02:27:26 +01:00
storage: detach read waiters from bounded owned hydration flights
This commit is contained in:
1 parent
e789af2515
commit
18f28d480b
5 files changed
+233
-43
No files matched your search
@@ -0,0 +1,181 @@
|
||||
package storage
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
// ErrChunkHydrationPending means no guest bytes were admitted: hydration
|
||||
// is queued/in flight or the caller's wait expired. Owned work may continue.
|
||||
var ErrChunkHydrationPending = errors.New("storage: chunk hydration pending")
|
||||
|
||||
const defaultHydrationWorkers = 2
|
||||
const defaultHydrationQueueDepth = 64
|
||||
const defaultHydrationWait = 4 * time.Second
|
||||
|
||||
type chunkHydrationFlight struct {
|
||||
idx, gen uint64
|
||||
ref chunkRef
|
||||
ctx context.Context
|
||||
cancel context.CancelFunc
|
||||
done chan struct{}
|
||||
running bool
|
||||
waiters int
|
||||
err error
|
||||
}
|
||||
|
||||
func hydrationPending(cause error) error {
|
||||
if cause == nil {
|
||||
return ErrChunkHydrationPending
|
||||
}
|
||||
return fmt.Errorf("%w: %w", ErrChunkHydrationPending, cause)
|
||||
}
|
||||
|
||||
// The promise and worker lifetime belong to the layer, not any one caller.
|
||||
// stMu protects admission, queue membership and waiter counts. Nobody holds
|
||||
// it (or a store/capture/lifecycle lock) while awaiting or fetching bytes.
|
||||
func (l *chunkLayerImpl) OnReadMissContext(ctx context.Context, lba uint64) ([]byte, error) {
|
||||
idx := l.chunkOf(lba)
|
||||
for {
|
||||
l.stMu.Lock()
|
||||
if l.closed {
|
||||
l.stMu.Unlock()
|
||||
return nil, errChunkLayerClosed
|
||||
}
|
||||
if l.fenced {
|
||||
l.stMu.Unlock()
|
||||
return nil, errChunkLayerFenced
|
||||
}
|
||||
if !l.absent[idx] || l.localBlocks[lba] {
|
||||
l.stMu.Unlock()
|
||||
return nil, nil
|
||||
}
|
||||
ref, ok := l.head.Chunks[idx]
|
||||
if !ok {
|
||||
l.stMu.Unlock()
|
||||
return make([]byte, l.blockSize), nil
|
||||
}
|
||||
if err := ctx.Err(); err != nil {
|
||||
l.stMu.Unlock()
|
||||
return nil, hydrationPending(err)
|
||||
}
|
||||
gen := l.absentGen[idx]
|
||||
f := l.hydration[idx]
|
||||
stale := f != nil && (f.gen != gen || f.ref != ref)
|
||||
if stale {
|
||||
f.cancel()
|
||||
}
|
||||
if f == nil {
|
||||
if len(l.hydration) >= l.cfg.HydrationQueueDepth {
|
||||
l.stMu.Unlock()
|
||||
return nil, hydrationPending(nil)
|
||||
}
|
||||
if !l.hydrationStarted {
|
||||
l.hydration = make(map[uint64]*chunkHydrationFlight)
|
||||
l.hydrationWake = make(chan struct{}, 1)
|
||||
l.hydrationStarted = true
|
||||
l.wg.Add(l.cfg.HydrationWorkers)
|
||||
for i := 0; i < l.cfg.HydrationWorkers; i++ {
|
||||
go l.hydrationWorker()
|
||||
}
|
||||
}
|
||||
work, cancel := context.WithCancel(l.ctx)
|
||||
f = &chunkHydrationFlight{idx: idx, gen: gen, ref: ref, ctx: work, cancel: cancel, done: make(chan struct{})}
|
||||
l.hydration[idx] = f
|
||||
}
|
||||
f.waiters++
|
||||
l.wakeHydrationLocked()
|
||||
l.stMu.Unlock()
|
||||
select {
|
||||
case <-f.done:
|
||||
case <-ctx.Done():
|
||||
// A completed fault wins a simultaneous caller deadline.
|
||||
select {
|
||||
case <-f.done:
|
||||
default:
|
||||
l.stMu.Lock()
|
||||
f.waiters--
|
||||
l.stMu.Unlock()
|
||||
return nil, hydrationPending(ctx.Err())
|
||||
}
|
||||
}
|
||||
l.stMu.Lock()
|
||||
f.waiters--
|
||||
closed := l.closed
|
||||
changed := l.absentGen[idx] != f.gen || l.head.Chunks[idx] != f.ref
|
||||
err := f.err
|
||||
l.stMu.Unlock()
|
||||
if closed {
|
||||
return nil, errChunkLayerClosed
|
||||
}
|
||||
if changed {
|
||||
continue
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// A local override or an intervening HEAD/absence change is resolved
|
||||
// through the normal path, under the same original caller deadline.
|
||||
}
|
||||
}
|
||||
|
||||
func (l *chunkLayerImpl) wakeHydrationLocked() {
|
||||
select {
|
||||
case l.hydrationWake <- struct{}{}:
|
||||
default:
|
||||
}
|
||||
}
|
||||
|
||||
func (l *chunkLayerImpl) hydrationWorker() {
|
||||
defer l.wg.Done()
|
||||
for {
|
||||
l.stMu.Lock()
|
||||
if l.closed {
|
||||
l.stMu.Unlock()
|
||||
return
|
||||
}
|
||||
var next *chunkHydrationFlight
|
||||
for _, f := range l.hydration {
|
||||
if !f.running && (next == nil || (f.waiters > 0 && next.waiters == 0)) {
|
||||
next = f
|
||||
}
|
||||
}
|
||||
if next == nil {
|
||||
l.stMu.Unlock()
|
||||
select {
|
||||
case <-l.hydrationWake:
|
||||
continue
|
||||
case <-l.ctx.Done():
|
||||
return
|
||||
}
|
||||
}
|
||||
next.running = true
|
||||
l.wakeHydrationLocked()
|
||||
l.stMu.Unlock()
|
||||
fetch, cancel := context.WithTimeout(next.ctx, l.cfg.ReadMissTimeout)
|
||||
_, err := l.fillOnce(fetch, next.idx, next.gen, next.ref)
|
||||
cancel()
|
||||
next.cancel()
|
||||
l.stMu.Lock()
|
||||
next.err = err
|
||||
delete(l.hydration, next.idx)
|
||||
close(next.done)
|
||||
l.wakeHydrationLocked()
|
||||
l.stMu.Unlock()
|
||||
}
|
||||
}
|
||||
|
||||
// Caller holds stMu, before Close begins joining wg. Running fills remain
|
||||
// owned until their worker finishes; queued promises settle without I/O.
|
||||
func (l *chunkLayerImpl) closeHydrationLocked() {
|
||||
for idx, f := range l.hydration {
|
||||
f.cancel()
|
||||
if !f.running {
|
||||
f.err = errChunkLayerClosed
|
||||
delete(l.hydration, idx)
|
||||
close(f.done)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -74,7 +74,10 @@ type chunkLayerImpl struct {
|
||||
bmMu sync.Mutex // serializes bitmap store calls
|
||||
presMu sync.Mutex // serializes a whole presence save: snapshot, fsync, replace, version
|
||||
|
||||
closeDone chan struct{} // closed when Close has finished
|
||||
hydration map[uint64]*chunkHydrationFlight // under stMu
|
||||
hydrationWake chan struct{}
|
||||
hydrationStarted bool
|
||||
closeDone chan struct{} // closed when Close has finished
|
||||
|
||||
stMu sync.Mutex
|
||||
closed bool
|
||||
@@ -178,6 +181,15 @@ func attachChunkLayerImpl(s *WALStore, remote chunkRemote, cfg chunkLayerConfig)
|
||||
if cfg.UploadConcurrency == 0 {
|
||||
cfg.UploadConcurrency = 8
|
||||
}
|
||||
if cfg.HydrationWorkers < 0 || cfg.HydrationWorkers > 64 || cfg.HydrationQueueDepth < 0 || cfg.HydrationQueueDepth > 65536 {
|
||||
return nil, errors.New("storage: hydration workers must be 1..64 and queue depth 1..65536 (0 uses defaults)")
|
||||
}
|
||||
if cfg.HydrationWorkers == 0 {
|
||||
cfg.HydrationWorkers = defaultHydrationWorkers
|
||||
}
|
||||
if cfg.HydrationQueueDepth == 0 {
|
||||
cfg.HydrationQueueDepth = defaultHydrationQueueDepth
|
||||
}
|
||||
if cfg.ReadMissTimeout <= 0 {
|
||||
cfg.ReadMissTimeout = defaultReadMissTimeout
|
||||
}
|
||||
@@ -534,45 +546,9 @@ func sortedKeys(m map[uint64]bool) []uint64 {
|
||||
// block that has no newer local version, then lets the store read its
|
||||
// extent. It runs with no walstore lock held.
|
||||
func (l *chunkLayerImpl) OnReadMiss(lba uint64) ([]byte, error) {
|
||||
idx := l.chunkOf(lba)
|
||||
l.stMu.Lock()
|
||||
if l.closed || !l.absent[idx] || l.localBlocks[lba] {
|
||||
l.stMu.Unlock()
|
||||
return nil, nil
|
||||
}
|
||||
l.wg.Add(1)
|
||||
l.stMu.Unlock()
|
||||
defer l.wg.Done()
|
||||
// One deadline covers every attempt. Each attempt fetches the chunk as
|
||||
// the current HEAD names it; a fill that went stale (eviction generation
|
||||
// or HEAD ref changed meanwhile) installs nothing and the read retries,
|
||||
// so the extent is read only once the block is resolved: the chunk is no
|
||||
// longer absent, or the block has a local version.
|
||||
ctx, cancel := context.WithTimeout(l.ctx, l.cfg.ReadMissTimeout)
|
||||
ctx, cancel := context.WithTimeout(context.Background(), defaultHydrationWait)
|
||||
defer cancel()
|
||||
for {
|
||||
l.stMu.Lock()
|
||||
if !l.absent[idx] || l.localBlocks[lba] {
|
||||
l.stMu.Unlock()
|
||||
return nil, nil
|
||||
}
|
||||
ref, ok := l.head.Chunks[idx]
|
||||
gen := l.absentGen[idx]
|
||||
l.stMu.Unlock()
|
||||
if !ok { // ZERO in the current HEAD
|
||||
return make([]byte, l.blockSize), nil
|
||||
}
|
||||
resolved, err := l.fillOnce(ctx, idx, gen, ref)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if resolved {
|
||||
return nil, nil
|
||||
}
|
||||
if err := ctx.Err(); err != nil {
|
||||
return nil, fmt.Errorf("storage: chunk %d fetch: fill kept going stale: %w", idx, err)
|
||||
}
|
||||
}
|
||||
return l.OnReadMissContext(ctx, lba)
|
||||
}
|
||||
|
||||
// fillOnce fetches one chunk version and installs every block that has no
|
||||
@@ -2011,6 +1987,7 @@ func (l *chunkLayerImpl) Close() error {
|
||||
return nil
|
||||
}
|
||||
l.closed = true
|
||||
l.closeHydrationLocked()
|
||||
l.stMu.Unlock()
|
||||
l.cowMu.Lock()
|
||||
l.cowClosed = true
|
||||
|
||||
@@ -347,6 +347,10 @@ type chunkLayerConfig struct {
|
||||
CaptureAllowed func() error
|
||||
// ReadMissTimeout bounds one OnReadMiss remote fetch.
|
||||
ReadMissTimeout time.Duration
|
||||
// HydrationWorkers bounds layer-owned fetches (0 defaults to 2).
|
||||
HydrationWorkers int
|
||||
// HydrationQueueDepth bounds distinct queued/in-flight chunks (0 defaults to 64).
|
||||
HydrationQueueDepth int
|
||||
// Bitmap persists the legacy dirty-since-HEAD bitmap. Required for v2;
|
||||
// impl3 uses its owned LSN map and never calls this optional legacy object.
|
||||
Bitmap chunkBitmapStore
|
||||
|
||||
@@ -5,6 +5,12 @@ import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// ContextReader is an optional request-aware read seam. One caller context
|
||||
// may cover several blocks, so implementations must not reset its deadline.
|
||||
type ContextReader interface {
|
||||
ReadContext(context.Context, uint32) ([]byte, error)
|
||||
}
|
||||
|
||||
// LogicalStorage is the V3 persistence-seam contract. It separates the
|
||||
// "what data exists, what frontier is durable" question (storage) from
|
||||
// the "what does the engine decide" question (semantic core). The
|
||||
|
||||
@@ -1046,7 +1046,14 @@ func (s *WALStore) WriteInstrumentation() WriteInstrumentationStatus {
|
||||
|
||||
// Read returns the current bytes at lba. Dirty entries are served
|
||||
// from the WAL; clean LBAs are served from the extent.
|
||||
func (s *WALStore) Read(lba uint32) ([]byte, error) {
|
||||
func (s *WALStore) Read(lba uint32) ([]byte, error) { return s.ReadContext(context.Background(), lba) }
|
||||
|
||||
func (s *WALStore) ReadContext(ctx context.Context, lba uint32) ([]byte, error) {
|
||||
ctx, cancel := context.WithTimeout(ctx, defaultHydrationWait)
|
||||
defer cancel()
|
||||
if err := ctx.Err(); err != nil {
|
||||
return nil, hydrationPending(err)
|
||||
}
|
||||
maxLBA := uint32(s.sb.VolumeSize / uint64(s.sb.BlockSize))
|
||||
if lba >= maxLBA {
|
||||
return nil, fmt.Errorf("storage: LBA %d out of range", lba)
|
||||
@@ -1062,12 +1069,12 @@ func (s *WALStore) Read(lba uint32) ([]byte, error) {
|
||||
if data, handled, err := s.readDirtyV3(lba); handled {
|
||||
return data, err
|
||||
}
|
||||
return s.readFromExtent(lba)
|
||||
return s.readFromExtentContext(ctx, lba)
|
||||
}
|
||||
if walRelOff, dataOffset, _, _, ok := s.dm.get(uint64(lba)); ok {
|
||||
return s.readFromWAL(walRelOff, dataOffset)
|
||||
}
|
||||
return s.readFromExtent(lba)
|
||||
return s.readFromExtentContext(ctx, lba)
|
||||
}
|
||||
|
||||
// readFromWAL pulls a single block out of a WAL Write/Trim entry
|
||||
@@ -1129,10 +1136,22 @@ func (s *WALStore) DisableAutoFlushForRecoveryTest() {
|
||||
}
|
||||
|
||||
func (s *WALStore) readFromExtent(lba uint32) ([]byte, error) {
|
||||
return s.readFromExtentContext(context.Background(), lba)
|
||||
}
|
||||
|
||||
func (s *WALStore) readFromExtentContext(ctx context.Context, lba uint32) ([]byte, error) {
|
||||
// An attached chunk layer resolves blocks that are absent locally
|
||||
// before the extent lock is taken; nil data means read the extent.
|
||||
if h := s.chunk.Load(); h != nil {
|
||||
data, err := h.sink.OnReadMiss(uint64(lba))
|
||||
var data []byte
|
||||
var err error
|
||||
if r, ok := h.sink.(interface {
|
||||
OnReadMissContext(context.Context, uint64) ([]byte, error)
|
||||
}); ok {
|
||||
data, err = r.OnReadMissContext(ctx, uint64(lba))
|
||||
} else {
|
||||
data, err = h.sink.OnReadMiss(uint64(lba))
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -1150,6 +1169,9 @@ func (s *WALStore) readFromExtent(lba uint32) ([]byte, error) {
|
||||
return s.readFromWAL(walRelOff, dataOffset)
|
||||
}
|
||||
}
|
||||
if err := ctx.Err(); err != nil {
|
||||
return nil, hydrationPending(err)
|
||||
}
|
||||
return s.readExtentBlock(lba)
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user