mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-04 10:07:28 +01:00
storage: settle a held cut CAS by re-proposing it at the same base
A cut or detach CAS whose outcome is unknown while HEAD still reads its base generation may still be applied by the server later. Reading HEAD alone no longer settles it: the capture keeps the proposal pending, and the next cut, durability point or Detach re-issues the same proposal at the same base. Whichever lands first, HEAD ends at that proposal and the other fails its precondition, so a delayed detach can never land after a no-op cut. The fake remote gains a held CAS mode (casDeferred) and releaseDeferredCAS, which applies held CAS calls later only if HEAD is still at their base. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
d0e3107c57
commit
fe9df65af8
3 files changed
+162
-5
No files matched your search
@@ -36,8 +36,32 @@ const (
|
||||
casNormal fakeCASMode = iota
|
||||
casUnknownNotApplied // not applied, returns errFakeCASUnknown
|
||||
casAppliedLost // applied, returns errFakeCASResponseLost
|
||||
casDeferred // retained server-side, returns errFakeCASUnknown; applied later by releaseDeferredCAS
|
||||
)
|
||||
|
||||
type deferredCAS struct {
|
||||
expected chunkHeadVersion
|
||||
next chunkHead
|
||||
}
|
||||
|
||||
// releaseDeferredCAS lets the server finish every retained CAS now: each
|
||||
// applies only if HEAD is still at its expected version (as a real filer
|
||||
// would evaluate the precondition when it finally runs). It returns how
|
||||
// many applied.
|
||||
func (f *fakeChunkRemote) releaseDeferredCAS() int {
|
||||
f.mu.Lock()
|
||||
defer f.mu.Unlock()
|
||||
applied := 0
|
||||
for _, d := range f.deferred {
|
||||
if f.head.Gen == d.expected.Gen && f.head.Epoch == d.expected.Epoch {
|
||||
f.installLocked(copyChunkHead(d.next))
|
||||
applied++
|
||||
}
|
||||
}
|
||||
f.deferred = nil
|
||||
return applied
|
||||
}
|
||||
|
||||
// fakeChunkRemote enforces immutable keys, chunk hashes and the HEAD CAS,
|
||||
// logs every call in order, and injects faults on request. Every field is
|
||||
// guarded by mu.
|
||||
@@ -72,6 +96,7 @@ type fakeChunkRemote struct {
|
||||
beforeCAS func(f *fakeChunkRemote) // runs once, under mu, before the next CAS is decided
|
||||
afterCAS func(f *fakeChunkRemote) // runs once, under mu, after the next CAS is decided
|
||||
intents map[string]chunkIntent
|
||||
deferred []deferredCAS
|
||||
}
|
||||
|
||||
func newFakeChunkRemote() *fakeChunkRemote {
|
||||
@@ -243,6 +268,11 @@ func (f *fakeChunkRemote) CASHead(ctx context.Context, expected chunkHeadVersion
|
||||
outcome = "cas:unknown"
|
||||
return 0, errFakeCASUnknown
|
||||
}
|
||||
if mode == casDeferred {
|
||||
outcome = "cas:deferred"
|
||||
f.deferred = append(f.deferred, deferredCAS{expected: expected, next: copyChunkHead(next)})
|
||||
return 0, errFakeCASUnknown
|
||||
}
|
||||
if expected.Gen != f.head.Gen || expected.Epoch != f.head.Epoch {
|
||||
outcome = "cas:pre"
|
||||
return 0, errChunkHeadPrecondition
|
||||
|
||||
@@ -69,6 +69,9 @@ type chunkLayerImpl struct {
|
||||
// re-read shows it applied.
|
||||
pendingDetach bool
|
||||
pendingCut chunkCutResult
|
||||
// pendingBase is the version the unresolved CAS expected. While HEAD is
|
||||
// still at it, that CAS may still land, so a read alone never settles it.
|
||||
pendingBase chunkHeadVersion
|
||||
remoteLSN uint64
|
||||
hdr chunkBitmapHeader // last header written or loaded
|
||||
dirty map[uint64]bool // chunks dirtied since the cut in progress or the last HEAD
|
||||
@@ -668,16 +671,26 @@ func (l *chunkLayerImpl) captureLocked(ctx context.Context, detach bool) (chunkC
|
||||
}
|
||||
// Precondition or unknown outcome: resolve by re-reading HEAD, never by
|
||||
// a blind retry.
|
||||
cur, gerr := l.remote.GetHead(ctx)
|
||||
if gerr != nil {
|
||||
keepPending := func() {
|
||||
l.stMu.Lock()
|
||||
p := copyHead(next)
|
||||
l.pending = &p
|
||||
l.pendingDetach, l.pendingCut = detach, chunkCutResult{H: h, FullHash: all}
|
||||
l.pendingDetach, l.pendingCut, l.pendingBase = detach, chunkCutResult{H: h, FullHash: all}, expected
|
||||
l.stMu.Unlock()
|
||||
l.abortCut(all)
|
||||
}
|
||||
cur, gerr := l.remote.GetHead(ctx)
|
||||
if gerr != nil {
|
||||
keepPending()
|
||||
return chunkCutResult{}, fmt.Errorf("storage: capture CAS outcome unresolved (%v): %w: %w", casErr, errChunkCASOutcomeUnknown, gerr)
|
||||
}
|
||||
if !errors.Is(casErr, errChunkHeadPrecondition) && !sameProposal(cur, next) && cur.Gen == expected.Gen && cur.Epoch == expected.Epoch {
|
||||
// HEAD is still the CAS's base and the server gave no definite
|
||||
// refusal: the CAS may still land. Settle it before any later
|
||||
// capture or durability point (resolvePending re-proposes it).
|
||||
keepPending()
|
||||
return chunkCutResult{}, fmt.Errorf("storage: capture CAS outcome unresolved (%v): HEAD unchanged: %w", casErr, errChunkCASOutcomeUnknown)
|
||||
}
|
||||
if sameProposal(cur, next) {
|
||||
l.finishCut(cur, h, changed, newKeys)
|
||||
return l.cutDone(chunkCutResult{H: h, Head: copyHead(cur), FullHash: all}, detach), nil
|
||||
@@ -758,7 +771,7 @@ func sameProposal(cur, next chunkHead) bool {
|
||||
// resolvePending re-reads HEAD when an earlier CAS outcome is unresolved.
|
||||
func (l *chunkLayerImpl) resolvePending(ctx context.Context) error {
|
||||
l.stMu.Lock()
|
||||
pending := l.pending
|
||||
pending, base := l.pending, l.pendingBase
|
||||
l.stMu.Unlock()
|
||||
if pending == nil {
|
||||
return nil
|
||||
@@ -767,11 +780,26 @@ func (l *chunkLayerImpl) resolvePending(ctx context.Context) error {
|
||||
if err != nil {
|
||||
return fmt.Errorf("storage: capture: earlier CAS outcome still unresolved: %w: %w", errChunkCASOutcomeUnknown, err)
|
||||
}
|
||||
if !sameProposal(cur, *pending) && !l.supersededBy(cur) && cur.Gen == base.Gen && cur.Epoch == base.Epoch {
|
||||
// HEAD is still the unresolved CAS's base, so that CAS may still
|
||||
// land. Settle it by issuing the same proposal at the same base:
|
||||
// whichever lands first, HEAD ends at this proposal and the other
|
||||
// fails its precondition.
|
||||
gen, casErr := l.remote.CASHead(ctx, base, copyHead(*pending))
|
||||
if casErr == nil {
|
||||
cur = copyHead(*pending)
|
||||
cur.Gen = gen
|
||||
} else if cur, err = l.remote.GetHead(ctx); err != nil {
|
||||
return fmt.Errorf("storage: capture: earlier CAS outcome still unresolved (%v): %w: %w", casErr, errChunkCASOutcomeUnknown, err)
|
||||
} else if !sameProposal(cur, *pending) && !l.supersededBy(cur) && cur.Gen == base.Gen && cur.Epoch == base.Epoch {
|
||||
return fmt.Errorf("storage: capture: earlier CAS outcome still unresolved (%v): HEAD unchanged: %w", casErr, errChunkCASOutcomeUnknown)
|
||||
}
|
||||
}
|
||||
l.stMu.Lock()
|
||||
defer l.stMu.Unlock()
|
||||
l.pending = nil
|
||||
wasDetach, cut := l.pendingDetach, l.pendingCut
|
||||
l.pendingDetach, l.pendingCut = false, chunkCutResult{}
|
||||
l.pendingDetach, l.pendingCut, l.pendingBase = false, chunkCutResult{}, chunkHeadVersion{}
|
||||
switch {
|
||||
case sameProposal(cur, *pending):
|
||||
l.head, l.remoteLSN = copyHead(cur), cur.RemoteLSN
|
||||
@@ -785,6 +813,8 @@ func (l *chunkLayerImpl) resolvePending(ctx context.Context) error {
|
||||
l.head = copyHead(cur)
|
||||
return errChunkLayerFenced
|
||||
default:
|
||||
// HEAD moved past the unresolved CAS's base without it: that CAS
|
||||
// can never land.
|
||||
l.head = copyHead(cur)
|
||||
}
|
||||
return nil
|
||||
|
||||
@@ -0,0 +1,97 @@
|
||||
package storage
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// A detach CAS the server retains after the caller saw an unknown outcome
|
||||
// must never let a later cut succeed at the old generation, and must not be
|
||||
// able to land after the layer has moved on (second review of #111, P2).
|
||||
func TestChunkDetach_DeferredDetachSettledBeforeNextCut(t *testing.T) {
|
||||
for _, rereadFails := range []bool{false, true} {
|
||||
t.Run(fmt.Sprintf("rereadFails=%v", rereadFails), func(t *testing.T) { deferredDetachSchedule(t, rereadFails) })
|
||||
}
|
||||
}
|
||||
|
||||
func deferredDetachSchedule(t *testing.T, rereadFails bool) {
|
||||
s, _ := newChunkTestStore(t)
|
||||
f := newFakeChunkRemote()
|
||||
layer := mustAttach(t, s, f, ctConfig(1))
|
||||
w := newWriteLog()
|
||||
w.write(t, s, 2, 0x22)
|
||||
if _, err := layer.Capture(ctTimeout(t)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
base := f.stats().head
|
||||
f.mu.Lock()
|
||||
f.casModes = []fakeCASMode{casDeferred}
|
||||
if rereadFails {
|
||||
f.beforeCAS = func(f *fakeChunkRemote) { f.failGet = errFakeRemoteDown }
|
||||
}
|
||||
f.mu.Unlock()
|
||||
if _, err := layer.Detach(ctTimeout(t)); !errors.Is(err, errChunkCASOutcomeUnknown) {
|
||||
t.Fatalf("Detach with a retained CAS: %v, want errChunkCASOutcomeUnknown", err)
|
||||
}
|
||||
healRemote(f)
|
||||
// HEAD still reads as the old active generation; the retained CAS may
|
||||
// still land. The next cut must settle it, not take the no-op path.
|
||||
if r, err := layer.Cut(ctTimeout(t)); !errors.Is(err, errChunkLayerDetached) {
|
||||
t.Fatalf("Cut after a retained detach CAS: %+v %v, want errChunkLayerDetached", r.Head, err)
|
||||
}
|
||||
if n := f.releaseDeferredCAS(); n != 0 {
|
||||
t.Fatalf("the retained detach CAS still landed after settlement (%d applied)", n)
|
||||
}
|
||||
h := f.stats().head
|
||||
if h.State != chunkHeadDetached || h.Gen != base.Gen+1 || h.DetachedBy == nil || h.DetachedBy.Gen != h.Gen {
|
||||
t.Fatalf("HEAD after settlement %+v (base gen %d)", h, base.Gen)
|
||||
}
|
||||
r, err := layer.Detach(ctTimeout(t))
|
||||
if err != nil || !sameHead(r.Head, h) || r.Head.State != chunkHeadDetached {
|
||||
t.Fatalf("Detach after settlement: %+v %v", r.Head, err)
|
||||
}
|
||||
assertEveryHeadIsPrefixImage(t, f, w)
|
||||
}
|
||||
|
||||
// A normal cut's retained CAS: released before settlement it applies and is
|
||||
// adopted; released after settlement it cannot apply. HEAD stays a prefix
|
||||
// image either way.
|
||||
func TestChunkCut_DeferredCASSettled(t *testing.T) {
|
||||
for _, releaseFirst := range []bool{true, false} {
|
||||
t.Run(fmt.Sprintf("releaseFirst=%v", releaseFirst), func(t *testing.T) { deferredCutSchedule(t, releaseFirst) })
|
||||
}
|
||||
}
|
||||
|
||||
func deferredCutSchedule(t *testing.T, releaseFirst bool) {
|
||||
s, _ := newChunkTestStore(t)
|
||||
f := newFakeChunkRemote()
|
||||
layer := mustAttach(t, s, f, ctConfig(1))
|
||||
w := newWriteLog()
|
||||
w.write(t, s, 5, 0x55)
|
||||
f.mu.Lock()
|
||||
f.casModes = []fakeCASMode{casDeferred}
|
||||
f.mu.Unlock()
|
||||
if _, err := layer.Cut(ctTimeout(t)); !errors.Is(err, errChunkCASOutcomeUnknown) {
|
||||
t.Fatalf("Cut with a retained CAS: %v, want errChunkCASOutcomeUnknown", err)
|
||||
}
|
||||
if releaseFirst {
|
||||
if n := f.releaseDeferredCAS(); n != 1 {
|
||||
t.Fatalf("the retained CAS did not apply when released first (%d)", n)
|
||||
}
|
||||
}
|
||||
r, err := layer.Cut(ctTimeout(t))
|
||||
if err != nil {
|
||||
t.Fatalf("Cut after the retained CAS: %v", err)
|
||||
}
|
||||
if !releaseFirst {
|
||||
if n := f.releaseDeferredCAS(); n != 0 {
|
||||
t.Fatalf("the retained CAS landed after settlement (%d applied)", n)
|
||||
}
|
||||
}
|
||||
h := f.stats().head
|
||||
if h.RemoteLSN != s.NextLSN()-1 || !sameHead(r.Head, h) {
|
||||
t.Fatalf("HEAD %+v, cut %+v", h, r.Head)
|
||||
}
|
||||
assertEveryHeadIsPrefixImage(t, f, w)
|
||||
}
|
||||
Reference in new issue
Block a user