mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-05 02:27:26 +01:00
Merge pull request #301 from seaweedfs/test/300-timing-bounds
test: restore, detach-freeze and revocation-ACK tests no longer race wall-clock bounds
This commit is contained in:
commit
7fe86bfb94
8 files changed
+186
-46
No files matched your search
@@ -0,0 +1,64 @@
|
||||
package durable_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// hangGuard returns a context that only guards against a hung test. It ends
|
||||
// before the test binary's own deadline (go test -timeout), not at a fixed
|
||||
// wall-clock bound, so a loaded host slows these tests down instead of
|
||||
// failing them: what they assert is the order and outcome of the operations.
|
||||
func hangGuard(t *testing.T) context.Context {
|
||||
t.Helper()
|
||||
deadline, ok := t.Deadline()
|
||||
ctx, cancel := context.WithDeadline(context.Background(), hangGuardDeadline(time.Now(), deadline, ok))
|
||||
t.Cleanup(cancel)
|
||||
return ctx
|
||||
}
|
||||
|
||||
const (
|
||||
hangGuardNoDeadline = 10 * time.Minute // no -timeout: still bounded
|
||||
hangGuardMaxMargin = 30 * time.Second
|
||||
)
|
||||
|
||||
// hangGuardDeadline leaves a tenth of the remaining budget (at most 30 s)
|
||||
// before the binary's deadline, so the guard fires first and the test still
|
||||
// has time to report and clean up, however short the budget. An expired
|
||||
// budget gives an expired guard: the test fails at once instead of racing the
|
||||
// binary's own timeout.
|
||||
func hangGuardDeadline(now, deadline time.Time, ok bool) time.Time {
|
||||
if !ok {
|
||||
return now.Add(hangGuardNoDeadline)
|
||||
}
|
||||
remaining := deadline.Sub(now)
|
||||
if remaining <= 0 {
|
||||
return deadline
|
||||
}
|
||||
margin := remaining / 10
|
||||
if margin > hangGuardMaxMargin {
|
||||
margin = hangGuardMaxMargin
|
||||
}
|
||||
return deadline.Add(-margin)
|
||||
}
|
||||
|
||||
func TestHangGuardDeadline(t *testing.T) {
|
||||
now := time.Unix(1700000000, 0)
|
||||
for _, c := range []struct {
|
||||
name string
|
||||
deadline time.Time
|
||||
ok bool
|
||||
want time.Time
|
||||
}{
|
||||
{"no deadline", time.Time{}, false, now.Add(hangGuardNoDeadline)},
|
||||
{"long budget", now.Add(time.Hour), true, now.Add(time.Hour - hangGuardMaxMargin)},
|
||||
{"short budget", now.Add(20 * time.Second), true, now.Add(18 * time.Second)},
|
||||
{"nearly expired", now.Add(100 * time.Millisecond), true, now.Add(90 * time.Millisecond)},
|
||||
{"expired", now.Add(-time.Second), true, now.Add(-time.Second)},
|
||||
} {
|
||||
if got := hangGuardDeadline(now, c.deadline, c.ok); !got.Equal(c.want) {
|
||||
t.Errorf("%s: guard at %v, want %v", c.name, got.Sub(now), c.want.Sub(now))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -25,7 +25,7 @@ type faultRemote struct {
|
||||
*storage.MemChunkRemote
|
||||
mu sync.Mutex
|
||||
putErr error // returned by every Put while set
|
||||
casMode string // "", "precondition", "lost" (not applied, error), "applied-lost" (applied, error), "hold"
|
||||
casMode string // "", "precondition", "lost" (not applied, error), "applied-lost" (applied, error), "hold", "hold-detach"
|
||||
getHeadErr error // returned by every GetHead while set
|
||||
getHeadFail int // the next N GetHead calls fail
|
||||
getHeadHold bool // every GetHead blocks until its caller gives up
|
||||
@@ -33,6 +33,16 @@ type faultRemote struct {
|
||||
// remote only when release closes; its own result goes to delayedErr.
|
||||
release chan struct{}
|
||||
delayedErr chan error
|
||||
// held records the CAS held by "hold-detach": the deadline of the context
|
||||
// it was given and when that context ended.
|
||||
held heldCAS
|
||||
}
|
||||
|
||||
type heldCAS struct {
|
||||
entered bool
|
||||
deadline time.Time
|
||||
hasDeadline bool
|
||||
ended time.Time
|
||||
}
|
||||
|
||||
var errInjected = errors.New("injected remote fault")
|
||||
@@ -71,7 +81,12 @@ func (r *faultRemote) GetHead(ctx context.Context) (storage.ChunkHead, error) {
|
||||
func (r *faultRemote) CASHead(ctx context.Context, expected storage.ChunkHeadVersion, next storage.ChunkHead) (uint64, error) {
|
||||
r.mu.Lock()
|
||||
mode := r.casMode
|
||||
r.casMode = "" // one fault, on the next CAS only
|
||||
switch {
|
||||
case mode == "hold-detach" && next.State != storage.ChunkHeadDetached:
|
||||
mode = "" // only the detach CAS is held; others (a running chunk loop) pass
|
||||
default:
|
||||
r.casMode = "" // one fault, on the next CAS only
|
||||
}
|
||||
r.mu.Unlock()
|
||||
switch mode {
|
||||
case "precondition":
|
||||
@@ -81,6 +96,13 @@ func (r *faultRemote) CASHead(ctx context.Context, expected storage.ChunkHeadVer
|
||||
case "hold": // held until the caller gives up: not applied
|
||||
<-ctx.Done()
|
||||
return 0, ctx.Err()
|
||||
case "hold-detach": // the detach CAS, held until its caller gives up; witnessed
|
||||
dl, ok := ctx.Deadline()
|
||||
r.set(func(r *faultRemote) { r.held = heldCAS{entered: true, deadline: dl, hasDeadline: ok} })
|
||||
<-ctx.Done()
|
||||
ended := time.Now()
|
||||
r.set(func(r *faultRemote) { r.held.ended = ended })
|
||||
return 0, ctx.Err()
|
||||
case "delayed":
|
||||
r.mu.Lock()
|
||||
release, out := r.release, r.delayedErr
|
||||
@@ -224,10 +246,14 @@ func TestWALStoreChunk_UndecidableDetachStaysStopped(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// The freeze is bounded by DetachFreezeTimeout: a CAS that hangs ends there,
|
||||
// the outcome is settled (HEAD still at base: cancelled) and serving resumes,
|
||||
// all well inside bound + settle.
|
||||
// The freeze is bounded by DetachFreezeTimeout: a detach CAS that hangs ends at
|
||||
// the cut's deadline (three quarters of the bound), the outcome is settled
|
||||
// (HEAD still at base: cancelled) inside the bound, and serving resumes. The
|
||||
// test asserts against the deadlines the detach itself traced, so a loaded
|
||||
// host cannot turn the order into a timing failure; the settlement keeps a
|
||||
// quarter of a 2 s bound for its HEAD read and cancel.
|
||||
func TestWALStoreChunk_DetachFreezeIsBoundedThenResumes(t *testing.T) {
|
||||
const freeze = 2 * time.Second
|
||||
remote := &faultRemote{MemChunkRemote: storage.NewMemChunkRemote()}
|
||||
p, err := durable.NewDurableProvider(durable.ProviderConfig{
|
||||
Impl: durable.ImplWALStoreChunk,
|
||||
@@ -235,7 +261,7 @@ func TestWALStoreChunk_DetachFreezeIsBoundedThenResumes(t *testing.T) {
|
||||
BlockSize: 4096,
|
||||
NumBlocks: pcBlocks,
|
||||
ChunkBlocks: pcChunkBlocks,
|
||||
DetachFreezeTimeout: 300 * time.Millisecond,
|
||||
DetachFreezeTimeout: freeze,
|
||||
ChunkRemoteFor: func(string) (storage.ChunkRemote, error) { return remote, nil },
|
||||
}, v1View())
|
||||
if err != nil {
|
||||
@@ -256,16 +282,41 @@ func TestWALStoreChunk_DetachFreezeIsBoundedThenResumes(t *testing.T) {
|
||||
if _, err := b.Write(context.Background(), 8192, block(2)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
remote.set(func(r *faultRemote) { r.casMode = "hold" })
|
||||
start := time.Now()
|
||||
remote.set(func(r *faultRemote) { r.casMode = "hold-detach" })
|
||||
deadlines := map[string]time.Time{}
|
||||
durable.SetTestDetachTrace(p, func(stage string, d time.Time) { deadlines[stage] = d })
|
||||
_, _, err = p.DetachVolume(context.Background(), "v1")
|
||||
took := time.Since(start)
|
||||
end := time.Now()
|
||||
if !errors.Is(err, durable.ErrDetachAborted) || !errors.Is(err, context.DeadlineExceeded) {
|
||||
t.Fatalf("detach with a hung CAS: %v; want an aborted detach at the freeze bound", err)
|
||||
}
|
||||
// The cut gets three quarters of the bound, the settlement the rest.
|
||||
if took < 225*time.Millisecond || took > 300*time.Millisecond+200*time.Millisecond {
|
||||
t.Fatalf("detach with a hung CAS took %v; want the cut ended at 3/4 of the 300 ms freeze bound and the total within it", took)
|
||||
// One budget from one origin: the cut ends at three quarters of it, the
|
||||
// settlement at its end (exact, both derive from the same start).
|
||||
cut, settle := deadlines["cut"], deadlines["settle"]
|
||||
if gap := settle.Sub(cut); gap != freeze/4 {
|
||||
t.Fatalf("settlement deadline %v after the cut deadline; want a quarter of the %v bound", gap, freeze)
|
||||
}
|
||||
// The held operation is the detach CAS itself: it ran under exactly the
|
||||
// cut's deadline, and was released by that deadline expiring.
|
||||
var held heldCAS
|
||||
remote.set(func(r *faultRemote) { held = r.held })
|
||||
if !held.entered {
|
||||
t.Fatal("the detach CAS was never held")
|
||||
}
|
||||
// The cut may bound the CAS by a context deadline or by a timer that
|
||||
// cancels it: a deadline, when there is one, must be the cut's exactly;
|
||||
// either way the CAS must end no earlier than the cut deadline.
|
||||
if held.hasDeadline && !held.deadline.Equal(cut) {
|
||||
t.Fatalf("held detach CAS deadline %v; want the cut deadline %v", held.deadline, cut)
|
||||
}
|
||||
if held.ended.Before(cut) {
|
||||
t.Fatalf("held detach CAS ended %v before its deadline", cut.Sub(held.ended))
|
||||
}
|
||||
if end.Before(held.ended) {
|
||||
t.Fatal("detach returned before its held CAS ended")
|
||||
}
|
||||
if late := end.Sub(settle); late > 500*time.Millisecond {
|
||||
t.Fatalf("detach returned %v after the freeze bound", late)
|
||||
}
|
||||
assertServing(t, p, b, remote)
|
||||
}
|
||||
|
||||
@@ -25,8 +25,7 @@ func awaitRestoreCondition(t *testing.T, ctx context.Context, what string, ready
|
||||
}
|
||||
|
||||
func TestRestoreCheckpointBeforeAssignmentPreservesRemoteBytes(t *testing.T) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
ctx := hangGuard(t)
|
||||
remote := storage.NewMemChunkRemote()
|
||||
view := newStubView(frontend.Projection{VolumeID: "v1", ReplicaID: "r1"})
|
||||
p := chunkProvider(t, t.TempDir(), remote, view, 0)
|
||||
@@ -80,8 +79,7 @@ func (r *publishAfterAbsentRead) GetHead(ctx context.Context) (storage.ChunkHead
|
||||
}
|
||||
|
||||
func TestRestoreHEADAppearsBetweenClaimAndAttach(t *testing.T) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
ctx := hangGuard(t)
|
||||
remote := storage.NewMemChunkRemote()
|
||||
data := bytes.Repeat([]byte{0xA7}, pcChunkBlocks*4096)
|
||||
wrapped := &publishAfterAbsentRead{ChunkRemote: remote, beforeReturn: func() {
|
||||
|
||||
@@ -13,7 +13,6 @@ import (
|
||||
"sync"
|
||||
"syscall"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/seaweedfs/seaweed-block/core/frontend"
|
||||
"github.com/seaweedfs/seaweed-block/core/frontend/durable"
|
||||
@@ -87,8 +86,7 @@ func failSeedFD(t *testing.T, root string) func() {
|
||||
}
|
||||
|
||||
func TestRestoreFailedSeedSyncRetryCaptureAndReopen(t *testing.T) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
ctx := hangGuard(t)
|
||||
root := filepath.Join(t.TempDir(), "provider")
|
||||
remote := storage.NewMemChunkRemote()
|
||||
data := bytes.Repeat([]byte{0xA7}, 4*4096)
|
||||
@@ -191,8 +189,7 @@ func TestRestoreFailedSeedSyncRetryCaptureAndReopen(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestRestoreFailedSeedNeverTakesOwnership(t *testing.T) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
ctx := hangGuard(t)
|
||||
root := filepath.Join(t.TempDir(), "provider")
|
||||
remote := storage.NewMemChunkRemote()
|
||||
if _, err := remote.CASHead(ctx, storage.ChunkHeadVersion{}, storage.ChunkHead{Epoch: 1, Holder: "old", RemoteLSN: 7}); err != nil {
|
||||
|
||||
@@ -6,7 +6,6 @@ import (
|
||||
"errors"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/seaweedfs/seaweed-block/core/frontend"
|
||||
"github.com/seaweedfs/seaweed-block/core/frontend/durable"
|
||||
@@ -24,8 +23,7 @@ func (r *takeoverOrderRemote) CASHead(ctx context.Context, v storage.ChunkHeadVe
|
||||
}
|
||||
|
||||
func TestRestoreSeedPrecedesTakeoverCAS(t *testing.T) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
ctx := hangGuard(t)
|
||||
remote := storage.NewMemChunkRemote()
|
||||
if _, err := remote.CASHead(ctx, storage.ChunkHeadVersion{}, storage.ChunkHead{Epoch: 1, Holder: "old", RemoteLSN: 7}); err != nil {
|
||||
t.Fatal(err)
|
||||
@@ -81,8 +79,7 @@ func TestRestoreSeedPrecedesTakeoverCAS(t *testing.T) {
|
||||
// A restored restart under the same assignment must reactivate a detached
|
||||
// image before exposing I/O, not treat it as an already-settled active claim.
|
||||
func TestRestoreEqualOwnerDetachedHEADIsReactivated(t *testing.T) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
ctx := hangGuard(t)
|
||||
remote := storage.NewMemChunkRemote()
|
||||
makeProvider := func() *durable.DurableProvider {
|
||||
p, err := durable.NewDurableProvider(durable.ProviderConfig{Impl: durable.ImplWALStoreChunk, StorageRoot: t.TempDir(), NumBlocks: 64, BlockSize: 4096, ChunkBlocks: 4, ChunkHolder: "node-a/r1",
|
||||
|
||||
@@ -11,7 +11,6 @@ import (
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/seaweedfs/seaweed-block/core/frontend"
|
||||
"github.com/seaweedfs/seaweed-block/core/frontend/durable"
|
||||
@@ -49,8 +48,7 @@ func (o *countingRebaser) seen() []uint64 {
|
||||
// before the takeover CAS. Returns the recovery error.
|
||||
func restoreWithObserver(t *testing.T, obs *countingRebaser, atCAS func(p *durable.DurableProvider)) (storage.ChunkRemote, *durable.DurableProvider, *atomic.Int32, error) {
|
||||
t.Helper()
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
t.Cleanup(cancel)
|
||||
ctx := hangGuard(t)
|
||||
remote := storage.NewMemChunkRemote()
|
||||
if _, err := remote.CASHead(ctx, storage.ChunkHeadVersion{}, storage.ChunkHead{Epoch: 1, Holder: "old", RemoteLSN: 7}); err != nil {
|
||||
t.Fatal(err)
|
||||
@@ -104,8 +102,7 @@ func TestRestoreRefusedShipRebaseKeepsHeadAndIOClosed(t *testing.T) {
|
||||
if err == nil {
|
||||
t.Fatal("restore succeeded although the observer refused the post-seed ship-LSN rebase")
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
ctx := hangGuard(t)
|
||||
h, herr := remote.GetHead(ctx)
|
||||
if herr != nil || calls.Load() != 0 || h.Epoch != 1 || h.Holder != "old" {
|
||||
t.Fatalf("HEAD claimed despite a refused ship rebase: CAS calls=%d head=%+v err=%v; want 0 calls, epoch 1 holder old", calls.Load(), h, herr)
|
||||
|
||||
@@ -10,7 +10,6 @@ import (
|
||||
"github.com/seaweedfs/seaweed-block/core/storage"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
type bootstrapBlobs struct {
|
||||
@@ -36,8 +35,7 @@ func (b *bootstrapBlobs) Get(_ context.Context, id string) ([]byte, error) {
|
||||
}
|
||||
|
||||
func TestRestoreEnsureVolumeZeroHEADPreservesCheckpointedBootstrap(t *testing.T) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 4*time.Second)
|
||||
defer cancel()
|
||||
ctx := hangGuard(t)
|
||||
filer := chunkstore.NewMemFiler()
|
||||
catalog, err := chunkstore.NewCatalog(filer, "/bootstrap")
|
||||
if err != nil {
|
||||
|
||||
@@ -2,6 +2,8 @@ package master
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -234,6 +236,10 @@ func TestWorkspaceBindingChosenObservationMustMatchServerAndPair(t *testing.T) {
|
||||
}
|
||||
|
||||
// The ACK must complete while the unrelated remote reader/prober remains held.
|
||||
// errHeldNotJoined: the held remote operation's worker did not return within
|
||||
// its own bound after being released.
|
||||
var errHeldNotJoined = errors.New("held remote operation not joined")
|
||||
|
||||
func TestWorkspaceBindingRemoteReadDoesNotBlockAck(t *testing.T) {
|
||||
for _, mode := range []string{"head", "promotion"} {
|
||||
t.Run(mode, func(t *testing.T) {
|
||||
@@ -257,12 +263,44 @@ func TestWorkspaceBindingRemoteReadDoesNotBlockAck(t *testing.T) {
|
||||
t.Fatal(e)
|
||||
}
|
||||
done := make(chan error, 1)
|
||||
var releaseOnce sync.Once
|
||||
releaseHeld := func() { releaseOnce.Do(func() { close(release) }) }
|
||||
// Registered before either worker launches: every exit releases the
|
||||
// held call, and a worker that was launched is joined under its own
|
||||
// bound, not under the fixture context a blocked ACK has already
|
||||
// used up. A setup failure before the launch joins nothing.
|
||||
launched := false
|
||||
launch := func(worker func() error) {
|
||||
launched = true
|
||||
go func() { done <- worker() }()
|
||||
}
|
||||
var joinOnce sync.Once
|
||||
var joined error
|
||||
join := func() error {
|
||||
if !launched {
|
||||
return nil
|
||||
}
|
||||
joinOnce.Do(func() {
|
||||
select {
|
||||
case joined = <-done:
|
||||
case <-time.After(10 * time.Second):
|
||||
joined = errHeldNotJoined
|
||||
}
|
||||
})
|
||||
return joined
|
||||
}
|
||||
t.Cleanup(func() {
|
||||
releaseHeld()
|
||||
if err := join(); errors.Is(err, errHeldNotJoined) {
|
||||
t.Error(err)
|
||||
}
|
||||
})
|
||||
if mode == "head" {
|
||||
reviewLegacyBind(t, h, c, ctx, "probe")
|
||||
go func() {
|
||||
launch(func() error {
|
||||
_, e := client.MoveWorkspace(ctx, &control.WorkspaceMove{RequestId: "held", VolumeId: "probe", ExpectedReplicaId: "r1", ExpectedEpoch: 1, TargetReplicaId: "r1", TargetServerId: "node-b", TargetDataAddr: "127.0.0.1:9200", TargetCtrlAddr: "127.0.0.1:9100", Kind: "clean"})
|
||||
done <- e
|
||||
}()
|
||||
return e
|
||||
})
|
||||
} else {
|
||||
if _, e := h.Lifecycle().Volumes.CreateVolume(lifecycle.VolumeSpec{VolumeID: "vol-rf3", SizeBytes: 1 << 20, ReplicationFactor: 3}); e != nil {
|
||||
t.Fatal(e)
|
||||
@@ -275,21 +313,21 @@ func TestWorkspaceBindingRemoteReadDoesNotBlockAck(t *testing.T) {
|
||||
waitAuthorityLine(t, h.Publisher(), "vol-rf3")
|
||||
ingestRF3Observation(t, h, false, true, true)
|
||||
h.SetPromotionEvidenceProvider(reviewHeldPromotion{arrived, release})
|
||||
go func() { _, e := h.RunLifecycleProductTick(); done <- e }()
|
||||
launch(func() error { _, e := h.RunLifecycleProductTick(); return e })
|
||||
}
|
||||
select {
|
||||
case <-arrived:
|
||||
case <-ctx.Done():
|
||||
t.Fatal("remote call not reached", ctx.Err())
|
||||
}
|
||||
ackCtx, cancel := context.WithTimeout(ctx, time.Second)
|
||||
_, e := client.AckWorkspaceRevoked(ackCtx, &control.WorkspaceRevocationAck{Identity: id, HeadGen: 9})
|
||||
cancel()
|
||||
close(release)
|
||||
select {
|
||||
case <-done:
|
||||
case <-ctx.Done():
|
||||
t.Fatal("remote operation did not finish", ctx.Err())
|
||||
// The held remote call is released only after the ACK returns, so an
|
||||
// ACK that waited for it would run into the test's own deadline. No
|
||||
// separate 1 s bound: the ACK includes a durable persist, which a
|
||||
// loaded host can slow past that without anything being blocked.
|
||||
_, e := client.AckWorkspaceRevoked(ctx, &control.WorkspaceRevocationAck{Identity: id, HeadGen: 9})
|
||||
releaseHeld()
|
||||
if err := join(); errors.Is(err, errHeldNotJoined) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if e != nil {
|
||||
t.Fatal("held remote operation blocked ACK", e)
|
||||
|
||||
Reference in new issue
Block a user