Files
seaweedfs/weed/util/http/chunk_read_hedge.go
T
ec1032d642 filer.remote.sync: a source chunk gone for good no longer wedges the write-back (#13220)
* filer.remote.sync: a source chunk gone for good no longer wedges the write-back

An entry whose chunk the volume servers answer 404 for, even with
readDeleted=true, can never be uploaded: the bytes no longer exist in the
cluster. A volume lost with its disk leaves such entries behind (for
example a filer restored from a metadata backup onto a new volume server
while some chunks had not been written back yet). filer.remote.sync
retried the create event forever, every ~2 s, and since a failing event
holds the directory's processed watermark, no later change under the
mount was ever written back again. We hit this in production: one photo's
chunk answered 404 for hours and more than 11,000 later changes waited
behind it.

Now, once the filer confirms the entry is still the one the event
described (the existing supersession check), the upload ends with
errSourceGone when:

- every location answered 404 for sourceGoneGrace (5 min) over
  sourceGoneMinAnswers (3) attempts in a row, or
- the master cannot locate the chunk's volume at all ("failed to locate",
  the whole volume lost with its disk) for sourceGoneLookupGrace (30 min,
  the filer deletion retry's figure for "volume not found").

It logs one ERROR line naming the path, the chunk's fid and the
destination, with a per-process count, and every caller treats the event
as handled so the offset advances. A path found gone is remembered with
its fid (bounded to 10,000), so later events of the same entry are skipped
at once; any other outcome (success, superseded, another error, a failed
supersession lookup) forgets it and starts the grace again, so a volume
server that has not loaded its volume yet, or a stale location, is not
mistaken for lost data.

Only the source decides. The upload body is wrapped in a recorder that
keeps the first error the chunk reader returned, because remote clients
wrap a body read failure in their own errors (the AWS SDK's RequestError
has no Unwrap). The recorder forwards ReadAt and Seek, so s3manager keeps
reading parts in place. A 404, 5xx, timeout or any other error from the
remote itself never reaches the recorder and keeps retrying as before.

A rename of an entry that was written back now deletes the old key only
after the new one is written: when the entry's local data is gone, the old
key keeps the last remote copy (logged) instead of being deleted before an
upload that can never succeed. The gateway's rename path, which deletes
first, does not acknowledge errSourceGone and keeps failing visibly.

util/http:
- GetAuthenticated (the encrypted chunk read) reports a 404 as ErrNotFound,
  as the unencrypted read paths already do, with the same error text;
  readEncryptedUrl wraps with %w instead of %v and treats a 404 as
  retryable, like the plain read, so it tries the other locations.
- A chunk read is ErrNotFound only when no location of the pass failed
  differently (NotFoundEverywhere): a 404 from one replica while another is
  down, answers 5xx or is throttled does not mean the chunk is gone. This
  is applied once in hedgedChunkPass (both RetriedFetchChunkData paths) and
  in the manifest's retriedStreamFetchChunkData loop.

Tests:
- filer_remote_sync_gone_test.go: a plain and an encrypted chunk served 404
  by a test volume server end in errSourceGone on the third attempt and are
  counted once; a remote that answers 404 with readable chunks never does;
  the tracker's grace, answer count, reset, fid change and remembered path;
  the lookup-miss grace; ReaderAt kept by the recorder; a rename keeps the
  old key when the entry is gone and deletes it after the write otherwise.
- TestGetAuthenticatedNotFound; TestRetriedFetchChunkDataNotFoundEverywhere
  (plain direct, plain stream, encrypted; a 404 and a 503 in either order
  is not ErrNotFound).
- Mutation checks: each fails with readEncryptedUrl back on %v, with the
  write error classified instead of the body's read error, with
  NotFoundEverywhere dropped from hedgedChunkPass, with the old key deleted
  first, and with the recorder hiding ReadAt.
- go vet and go test of weed/util/http, weed/command, weed/filer and
  weed/replication/... pass; weed/util/http also with -race.

* filer.remote.sync: renames keep the old key until the new one holds the content; gone is per entry; missing volumes opt-in

- A sighting is counted per entry content (the sorted chunk file ids),
  not per failing chunk. An uploader reads the parts of a ReaderAt body in
  parallel, so the chunk that fails first changes between attempts; keyed
  on it, the grace restarted every time and a large file missing several
  chunks held the offset forever. The tracker's `seen` map is bounded, and
  the log names the first chunk "and N more" instead of every fid.
- On every rename the old key is deleted only after the new one holds the
  content: also when the event's old entry carries no stamp (an upload
  whose stamp a rename in between refused leaves an unstamped copy), and
  in the superseded branch, where the old key is what uploadCurrentEntry
  copies a remote-only entry from. uploadCurrentEntry reports when the
  current entry's data is gone, and the old key stays then. A multipart or
  system path whose old key cannot be deleted no longer skips the stamp.
- A volume the master cannot locate counts only with
  -sourceGoneVolumeMissing=<duration> (default 0: never): a volume server
  that is down or in maintenance looks the same, and an entry written off
  is not retried. It suits a deployment whose master and volume server run
  as one process.
- filer.remote.gateway's rename writes the new key first, deletes the old
  object after, and keeps it when the source data is gone.

Tests: TestRetriedWriteFileSourceGoneAcrossChunks (two lost chunks read in
turn), TestSupersededRenameUploadsCurrentEntry "uncached between the check
and the upload" and "current entry's chunks gone: the old key stays",
TestRenameOfGoneEntryKeepsTheOldKey "old entry unstamped",
TestSourceGoneGraceFor (lookup miss off by default, on with the flag);
each fails with its fix undone. The test remote's DeleteFile removes the
object, as a real remote does.

* filer.remote.sync: evidence kinds apart, a gone entry counted once, the copy owns its source's cleanup; gateway keeps the old object on a superseded rename

- Lookup misses and 404s no longer share one run of sightings
  (sightingKey = content + grace): with -sourceGoneVolumeMissing set, a few
  minutes of "failed to locate" followed by one 404 met the 404 grace with
  the misses' start and answers.
- observe reports whether this call found the entry gone; a replayed event
  of an entry already gone is skipped without another log line or count.
- A superseded rename whose current entry is remote-only leaves the old key
  to completeRemoteOnlyRename, which removes its source only while it is
  the object the stamp describes; an unconditional delete after it could
  remove a replacement.
- filer.remote.gateway: a superseded rename keeps the old object (logged)
  instead of deleting it before the superseding event has written the new
  key.

Tests: TestSourceGoneTrackerCountsOnceAndKeepsEvidenceApart,
TestSupersededRenameUploadsCurrentEntry "the old key replaced during the
copy"; each fails with its fix undone. The gateway change has no test of its
own (its rename path needs a reachable filer).

* filer.remote.sync: drop -sourceGoneVolumeMissing, a lookup miss keeps retrying

A volume the master cannot locate looks the same whether it was lost or
its server is down, so no default can write such an entry off safely, and
a flag for the one-process layout is not worth carrying. Only a 404 from
every location counts as gone; a lookup miss retries as before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* filer.remote.gateway: a superseded rename settles its old object

A rename the filer has moved past kept the renamed-from object for good,
so every rename followed by a delete or a rewrite before the gateway
reached it left an orphan on the remote. Settle it the way
filer.remote.sync's uploadCurrentEntry does: copy from it when the entry
has become remote-only, and otherwise delete it, since it no longer holds
the entry's content.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* filer.remote.sync, filer.remote.gateway: a superseded rename copied from a later rename's source settles its own old key

When the entry became remote-only through another rename onto it, the copy
reads that rename's source and removes it, and nothing read or removed the
event's own old key any more. Delete it then, in both commands.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* filer.remote.gateway: reset cached remote clients after the superseded-rename test

A cached client kept the previous run's recorder, so the test failed under
-count above one.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Chris Lu <chrislusf@users.noreply.github.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-01 14:15:37 -08:00

479 lines
14 KiB
Go

package http
import (
"context"
"errors"
"io"
"sync/atomic"
"time"
"github.com/seaweedfs/seaweedfs/weed/glog"
"github.com/seaweedfs/seaweedfs/weed/util/mem"
)
// A chunk read whose location goes quiet is asked of another location as
// well. A volume server sends a chunk without pauses once it has answered, so
// a body that stops arriving for chunkReadStallDelay is waiting for a lost TCP
// segment: the retransmission timeout is at least 200 ms and doubles with
// every further loss, so on a lossy path one chunk can hold up a sequential
// reader for seconds, while a fresh request takes milliseconds. Half the
// minimum retransmission timeout still gets ahead of every such wait, and
// lets the shorter pauses that recover on their own go by without a hedge.
// Before the response headers the server is still reading the needle, which
// can take a while on a busy disk, so a location gets chunkReadAnswerDelay to
// answer; one silent for that long is on a hung or dead host, or its answer
// was lost. Tests shorten both.
var (
chunkReadAnswerDelay = 200 * time.Millisecond
chunkReadStallDelay = 100 * time.Millisecond
)
// maxHedgesPerRead caps the extra requests a single chunk read can make.
const maxHedgesPerRead = 2
// errNoAnswer marks a location that had not even sent its response headers by
// the time another location delivered the chunk.
var errNoAnswer = errors.New("no response headers before another location delivered the chunk")
// Hedging is budgeted so that a cluster that is slow everywhere is not asked
// for every chunk twice: every chunk read earns a tenth of a token, every
// hedge spends a whole one, and at most hedgeTokenBurst tokens are saved up.
// Hedges therefore stay below a tenth of the reads, apart from a burst of ten.
const (
hedgeTokenScale = 1000 // tokens are counted in thousandths
hedgeTokenEarn = hedgeTokenScale / 10
hedgeTokenBurst = 10 * hedgeTokenScale
)
var hedgeTokens = func() *atomic.Int64 {
var tokens atomic.Int64
tokens.Store(hedgeTokenBurst)
return &tokens
}()
func earnHedgeToken() {
for {
tokens := hedgeTokens.Load()
if tokens >= hedgeTokenBurst || hedgeTokens.CompareAndSwap(tokens, min(tokens+hedgeTokenEarn, hedgeTokenBurst)) {
return
}
}
}
// refundHedgeToken gives back the token of a hedge that was not sent after all.
func refundHedgeToken() {
for {
tokens := hedgeTokens.Load()
if hedgeTokens.CompareAndSwap(tokens, min(tokens+hedgeTokenScale, hedgeTokenBurst)) {
return
}
}
}
func spendHedgeToken() bool {
for {
tokens := hedgeTokens.Load()
if tokens < hedgeTokenScale {
return false
}
if hedgeTokens.CompareAndSwap(tokens, tokens-hedgeTokenScale) {
return true
}
}
}
// chunkProgress is what a chunk read reports while it runs: when something
// last arrived, and whether the response headers did. A nil one ignores it.
type chunkProgress struct {
lastArrival atomic.Int64 // unix nanoseconds
answered atomic.Bool
}
func (p *chunkProgress) arrived() {
if p != nil {
p.lastArrival.Store(time.Now().UnixNano())
}
}
func (p *chunkProgress) answer() {
if p != nil {
p.answered.Store(true)
p.arrived()
}
}
// progressReader reports every piece of a response body that arrives.
type progressReader struct {
reader io.Reader
progress *chunkProgress
}
func (r progressReader) Read(p []byte) (int, error) {
n, err := r.reader.Read(p)
if n > 0 {
r.progress.arrived()
}
return n, err
}
// chunkAttempt reads a chunk from one location into dst and returns how many
// bytes it read. It reports to progress what arrives, and stops when ctx ends.
// A failure comes with whether another location is worth asking.
type chunkAttempt func(ctx context.Context, url string, dst []byte, progress *chunkProgress) (n int, retryable bool, err error)
// ScratchBudget accounts for the buffers that hedged chunk reads borrow. A
// caller that bounds the memory of its chunk buffers passes one in the
// context with WithScratchBudget, and a hedge that does not fit is not sent.
type ScratchBudget interface {
// TryReserveScratch takes size bytes without waiting, or reports that
// they do not fit.
TryReserveScratch(size int) bool
// ReleaseScratch returns what TryReserveScratch took.
ReleaseScratch(size int)
}
type scratchBudgetKey struct{}
// WithScratchBudget makes the chunk reads under ctx account the buffers of
// their hedges to budget.
func WithScratchBudget(ctx context.Context, budget ScratchBudget) context.Context {
return context.WithValue(ctx, scratchBudgetKey{}, budget)
}
// ScratchBudgetFrom returns the budget WithScratchBudget put in ctx, or nil.
func ScratchBudgetFrom(ctx context.Context) ScratchBudget {
budget, _ := ctx.Value(scratchBudgetKey{}).(ScratchBudget)
return budget
}
// readHedges is what one chunk read has spent on hedges, across the passes of
// its retries.
type readHedges struct {
earned bool // the read has added its share to the hedge budget
sent int
}
type hedgedAttempt struct {
url string
dst []byte
scratch bool // dst is not the caller's buffer and is freed when the attempt ends
hedge bool // sent because the requests before it went quiet
cancel context.CancelFunc
started time.Time
progress chunkProgress
finished bool
abandoned bool
}
type attemptResult struct {
attempt *hedgedAttempt
n int
retryable bool
err error
}
// hedgedChunkPass makes one pass over urls for a chunk read into buffer.
//
// Like a plain loop over the locations it asks them in order and moves on to
// the next one as soon as one fails. Unlike it, it does not wait out a location
// that goes quiet: once every request in flight has been quiet for longer than
// it may be (chunkReadAnswerDelay before its response headers arrive,
// chunkReadStallDelay after), the read is asked of the next location as well.
// Once every location has been asked, a location whose server answered and
// then went quiet is asked again over another connection: the server is up,
// and only that connection is stuck. A location that has not answered is left
// alone then, as it was before. Whichever request delivers the whole chunk
// first wins, and the others are cancelled. hedging carries what the read has
// spent on hedges over to the passes of its retries.
//
// Only one request at a time reads into buffer; the others read into buffers
// of their own, accounted to the ScratchBudget in ctx if there is one, and a
// winner's data is copied over once the request reading into buffer has
// ended. buffer is not written after hedgedChunkPass returns.
//
// It returns the number of bytes read, whether a location failed or did not
// answer at all on the way, which says the caller's location list may be
// stale, and the error of the pass with whether it is worth another pass. A
// failure that another location would not fix stops the asking, though a
// request already in flight may still deliver the chunk. A hedge throttled by
// its server is not such a failure: that server is only busy.
func hedgedChunkPass(ctx context.Context, urls []string, buffer []byte, hedging *readHedges, attempt chunkAttempt) (n int, failed bool, retryable bool, err error) {
if len(urls) == 0 {
return 0, false, false, nil
}
if !hedging.earned {
earnHedgeToken()
hedging.earned = true
}
budget := ScratchBudgetFrom(ctx)
results := make(chan attemptResult, len(urls)+maxHedgesPerRead)
var (
attempts []*hedgedAttempt
owner *hedgedAttempt // the request reading into buffer
running int
next int // the next location not asked yet
hedgeOff bool // no budget or memory left for a hedge in this pass
stopAsking bool
finalErr error
throttled error // a hedge's server was too busy to serve it
otherErr error // a location that failed other than with a 404
)
// destination is where a new request reads: the caller's buffer when no
// other request reads into it, otherwise a buffer of its own, if the
// scratch budget has room for one.
destination := func() (dst []byte, scratch bool, ok bool) {
if owner == nil {
return buffer, false, true
}
if budget != nil && !budget.TryReserveScratch(len(buffer)) {
return nil, false, false
}
return mem.Allocate(len(buffer)), true, true
}
size := len(buffer)
freeScratch := func(dst []byte) {
mem.Free(dst)
if budget != nil {
budget.ReleaseScratch(size)
}
}
start := func(url string, dst []byte, scratch, hedge bool) {
a := &hedgedAttempt{url: url, dst: dst, scratch: scratch, hedge: hedge, started: time.Now()}
if !scratch {
owner = a
}
attemptCtx, cancel := context.WithCancel(ctx)
a.cancel = cancel
a.progress.arrived()
attempts = append(attempts, a)
running++
go func() {
m, r, e := attempt(attemptCtx, url, a.dst, &a.progress)
results <- attemptResult{attempt: a, n: m, retryable: r, err: e}
}()
}
// askNext asks the next location not asked yet, as the plain loop did
// after a failure. When the request reading into buffer is still running
// and there is no room for another buffer, its own failure asks it later.
askNext := func() {
if stopAsking || next >= len(urls) {
return
}
dst, scratch, ok := destination()
if !ok {
return
}
start(urls[next], dst, scratch, false)
next++
}
// settle takes in the result of a request that ended.
settle := func(r attemptResult) {
running--
r.attempt.finished = true
r.attempt.cancel()
if r.attempt == owner {
owner = nil
}
}
// end cancels the requests still running and waits for the one reading
// into buffer. The others release their buffers in the background.
end := func() {
for _, a := range attempts {
if !a.finished {
a.abandoned = true
a.cancel()
}
}
for owner != nil {
r := <-results
settle(r)
if r.attempt.scratch {
freeScratch(r.attempt.dst)
}
}
if running > 0 {
go func(left int) {
for ; left > 0; left-- {
if r := <-results; r.attempt.scratch {
freeScratch(r.attempt.dst)
}
}
}(running)
}
}
// hedgeAt is when every request in flight will have been quiet for longer
// than it may be, unless something arrives first.
hedgeAt := func() time.Time {
var at time.Time
for _, a := range attempts {
if a.finished {
continue
}
quietLimit := chunkReadAnswerDelay
if a.progress.answered.Load() {
quietLimit = chunkReadStallDelay
}
if t := time.Unix(0, a.progress.lastArrival.Load()).Add(quietLimit); t.After(at) {
at = t
}
}
return at
}
// stuckAnswered is the request that answered and has been quiet the
// longest, if any: its location is worth asking again.
stuckAnswered := func() *hedgedAttempt {
var pick *hedgedAttempt
for _, a := range attempts {
if !a.finished && a.progress.answered.Load() &&
(pick == nil || a.progress.lastArrival.Load() < pick.progress.lastArrival.Load()) {
pick = a
}
}
return pick
}
askNext()
timer := time.NewTimer(chunkReadAnswerDelay)
defer timer.Stop()
for running > 0 {
var hedge <-chan time.Time
if !stopAsking && !hedgeOff && hedging.sent < maxHedgesPerRead {
// A request that answers meanwhile may be quiet only for the
// shorter chunkReadStallDelay from then on: look again by then.
wait := chunkReadStallDelay
if next < len(urls) || stuckAnswered() != nil {
wait = min(time.Until(hedgeAt()), wait)
}
timer.Reset(max(wait, time.Millisecond))
hedge = timer.C
}
select {
case <-ctx.Done():
end()
return 0, failed, false, ctx.Err()
case <-hedge:
if time.Now().Before(hedgeAt()) {
continue // something arrived meanwhile
}
// a location not asked yet, or again one whose answer got stuck
var url string
unasked := next < len(urls)
if unasked {
url = urls[next]
} else if stuck := stuckAnswered(); stuck != nil {
url = stuck.url
} else {
continue // nowhere worth asking yet
}
// The token comes first: room for a buffer may evict cached
// chunks, which is only worth it for a hedge that is sent.
if !spendHedgeToken() {
hedgeOff = true
continue
}
dst, scratch, ok := destination()
if !ok {
refundHedgeToken()
hedgeOff = true // no room for another buffer
continue
}
if unasked {
next++
}
hedging.sent++
glog.V(1).InfofCtx(ctx, "chunk read went quiet, asking %s as well", url)
start(url, dst, scratch, true)
case r := <-results:
settle(r)
a := r.attempt
if a.abandoned {
if a.scratch {
freeScratch(a.dst)
}
continue
}
if ctxErr := ctx.Err(); ctxErr != nil {
if a.scratch {
freeScratch(a.dst)
}
end()
return 0, failed, false, ctxErr
}
if r.err == nil {
// The chunk is here. A location that has not even answered by
// now is tried last for a while, like one that cannot be
// reached, and the location list it came from may be stale.
// A request asked again where the server answered is only
// waiting for a connection.
answered := make(map[string]bool, len(attempts))
for _, other := range attempts {
if other.progress.answered.Load() {
answered[other.url] = true
}
}
for _, other := range attempts {
if !other.finished && !answered[other.url] && time.Since(other.started) >= chunkReadAnswerDelay {
recordUnreachable(hostOf(other.url), errNoAnswer)
failed = true
}
}
end()
if a.scratch {
copy(buffer, a.dst[:r.n])
freeScratch(a.dst)
}
return r.n, failed, false, nil
}
if a.scratch {
freeScratch(a.dst)
}
if !r.retryable {
if a.hedge && errors.Is(r.err, ErrTooManyRequests) {
// that server is busy; another location may still serve it
if throttled == nil {
throttled = r.err
}
otherErr = r.err
askNext()
continue
}
if finalErr == nil {
finalErr = r.err
}
stopAsking = true
continue
}
failed = true
err, retryable = r.err, true
if !errors.Is(r.err, ErrNotFound) {
otherErr = r.err
}
glog.V(0).InfofCtx(ctx, "read %s failed, err: %v", a.url, r.err)
askNext()
}
}
if finalErr != nil {
return 0, failed, false, finalErr
}
if err == nil && throttled != nil {
return 0, failed, false, throttled
}
// a 404 from one location while another failed differently does not
// mean the chunk is gone
return 0, failed, retryable, NotFoundEverywhere(err, otherErr)
}