diff --git a/seaweed-block/core/authority/workspace_renewal_slowstore_test.go b/seaweed-block/core/authority/workspace_renewal_slowstore_test.go new file mode 100644 index 0000000000..ad2e5ac154 --- /dev/null +++ b/seaweed-block/core/authority/workspace_renewal_slowstore_test.go @@ -0,0 +1,109 @@ +package authority + +import ( + "context" + "fmt" + "sync" + "sync/atomic" + "testing" + "time" +) + +// slowLeaseStore delays every workspace-lease Put once armed, standing in for +// a master disk whose fsync stalls under heavy I/O (issue #205). +type slowLeaseStore struct { + AuthorityStore + delayNS atomic.Int64 +} + +func (s *slowLeaseStore) Put(r DurableRecord) error { + if d := time.Duration(s.delayNS.Load()); d > 0 && r.Workspace != nil && r.Workspace.Lease != nil { + time.Sleep(d) + } + return s.AuthorityStore.Put(r) +} + +// A renewal of a live lease must not wait for the authority store's fsync: +// the host's renewal RPC times out after 2 s, so a slower store write on the +// renewal path turns into a host fence and a volume shutdown (issue #205). +func TestWorkspaceRenewalDoesNotWaitForSlowStore(t *testing.T) { + disk, _ := newTempStore(t) + slow := &slowLeaseStore{AuthorityStore: disk} + var nowNS atomic.Int64 + nowNS.Store(time.Unix(1000, 0).UnixNano()) + p := NewPublisher(nil, WithStore(slow), WithWorkspaceLeaseConfig(30*time.Second, func() time.Time { return time.Unix(0, nowNS.Load()) })) + if err := p.apply(AssignmentAsk{VolumeID: "v", ReplicaID: "old", DataAddr: "d", CtrlAddr: "c", Intent: IntentBind}); err != nil { + t.Fatal(err) + } + first, err := p.RenewWorkspaceLease(context.Background(), "v", "old", 1) + if err != nil { + t.Fatal(err) + } + slow.delayNS.Store(int64(2500 * time.Millisecond)) + t.Cleanup(func() { slow.delayNS.Store(0) }) + nowNS.Add(int64(10 * time.Second)) + start := time.Now() + second, err := p.RenewWorkspaceLease(context.Background(), "v", "old", 1) + took := time.Since(start) + if err != nil { + t.Fatal(err) + } + if second.ExpiresAtNS <= first.ExpiresAtNS { + t.Fatalf("renewal did not extend the grant: first=%d second=%d", first.ExpiresAtNS, second.ExpiresAtNS) + } + if took >= 2*time.Second { + t.Fatalf("renewal of a live lease took %s behind a 2.5 s store write; the host's renewal RPC times out at 2 s", took.Round(time.Millisecond)) + } +} + +// Renewals of different volumes must not queue behind each other's store +// writes: with six volumes and a 0.5 s write each, serializing them makes the +// last renewal wait about 3 s, past the host's 2 s renewal timeout. +func TestWorkspaceConcurrentRenewalsDoNotSerializeOnStore(t *testing.T) { + disk, _ := newTempStore(t) + slow := &slowLeaseStore{AuthorityStore: disk} + var nowNS atomic.Int64 + nowNS.Store(time.Unix(1000, 0).UnixNano()) + p := NewPublisher(nil, WithStore(slow), WithWorkspaceLeaseConfig(30*time.Second, func() time.Time { return time.Unix(0, nowNS.Load()) })) + const n = 6 + for i := 0; i < n; i++ { + v := fmt.Sprintf("v%d", i) + if err := p.apply(AssignmentAsk{VolumeID: v, ReplicaID: "r", DataAddr: "d", CtrlAddr: "c", Intent: IntentBind}); err != nil { + t.Fatal(err) + } + if _, err := p.RenewWorkspaceLease(context.Background(), v, "r", 1); err != nil { + t.Fatal(err) + } + } + slow.delayNS.Store(int64(500 * time.Millisecond)) + t.Cleanup(func() { slow.delayNS.Store(0) }) + nowNS.Add(int64(10 * time.Second)) + var wg sync.WaitGroup + took := make([]time.Duration, n) + errs := make([]error, n) + gate := make(chan struct{}) + for i := 0; i < n; i++ { + wg.Add(1) + go func(i int) { + defer wg.Done() + <-gate + start := time.Now() + _, errs[i] = p.RenewWorkspaceLease(context.Background(), fmt.Sprintf("v%d", i), "r", 1) + took[i] = time.Since(start) + }(i) + } + close(gate) + wg.Wait() + var slowest time.Duration + for i := 0; i < n; i++ { + if errs[i] != nil { + t.Fatalf("v%d: %v", i, errs[i]) + } + if took[i] > slowest { + slowest = took[i] + } + } + if slowest >= 2*time.Second { + t.Fatalf("slowest of %d concurrent renewals took %s (all: %v); renewals serialize behind each other's store writes", n, slowest.Round(time.Millisecond), took) + } +} diff --git a/seaweed-block/core/host/master/workspace_renewal_slowstore_test.go b/seaweed-block/core/host/master/workspace_renewal_slowstore_test.go new file mode 100644 index 0000000000..f22071c655 --- /dev/null +++ b/seaweed-block/core/host/master/workspace_renewal_slowstore_test.go @@ -0,0 +1,118 @@ +package master + +import ( + "context" + "sync" + "sync/atomic" + "testing" + "time" + + "github.com/seaweedfs/seaweed-block/core/authority" + "github.com/seaweedfs/seaweed-block/core/frontend" + "github.com/seaweedfs/seaweed-block/core/host/volume" + control "github.com/seaweedfs/seaweed-block/core/rpc/control" + "google.golang.org/grpc" +) + +// slowGuardStore delays every workspace-lease Put once armed (issue #205). +type slowGuardStore struct { + authority.AuthorityStore + delayNS atomic.Int64 +} + +func (s *slowGuardStore) Put(r authority.DurableRecord) error { + if d := time.Duration(s.delayNS.Load()); d > 0 && r.Workspace != nil && r.Workspace.Lease != nil { + time.Sleep(d) + } + return s.AuthorityStore.Put(r) +} + +// deadlineLeaseClient calls the publisher through the master's leaseFact and, +// like a gRPC client, returns at the caller's deadline even when the master +// call is still blocked. In-flight calls are joined before the store closes. +type deadlineLeaseClient struct { + p *authority.Publisher + inflight *sync.WaitGroup +} + +func (c deadlineLeaseClient) RenewWorkspaceLease(ctx context.Context, id *control.WorkspaceIdentity, _ ...grpc.CallOption) (*control.WorkspaceLeaseFact, error) { + type result struct { + fact *control.WorkspaceLeaseFact + err error + } + done := make(chan result, 1) + c.inflight.Add(1) + go func() { + defer c.inflight.Done() + l, err := c.p.RenewWorkspaceLease(context.Background(), id.VolumeId, id.ReplicaId, id.Epoch) + if err != nil { + done <- result{err: err} + return + } + done <- result{fact: leaseFact(id.VolumeId, l)} + }() + select { + case r := <-done: + return r.fact, r.err + case <-ctx.Done(): + return nil, ctx.Err() + } +} + +func (deadlineLeaseClient) AckWorkspaceRevoked(context.Context, *control.WorkspaceRevocationAck, ...grpc.CallOption) (*control.WorkspaceEmpty, error) { + panic("a slow store must not lead to a clean ACK") +} + +// A live host must keep its lease while the master's authority store is slow: +// a store write longer than the host's 2 s renewal timeout must not fence the +// guard and shut the volume down (issue #205). TTL 6 s gives the guard 4 s of +// validity (6 s minus half the default 4 s skew), renewed every ~1.3 s. +func TestWorkspaceLeaseGuardSurvivesSlowAuthorityStore(t *testing.T) { + disk, err := authority.NewFileAuthorityStore(t.TempDir()) + if err != nil { + t.Fatal(err) + } + slow := &slowGuardStore{AuthorityStore: disk} + var inflight sync.WaitGroup + t.Cleanup(func() { + slow.delayNS.Store(0) + inflight.Wait() + if err := disk.Close(); err != nil { + t.Error(err) + } + }) + dir := &skewDirective{asks: make(chan authority.AssignmentAsk, 1)} + p := authority.NewPublisher(dir, authority.WithStore(slow), authority.WithWorkspaceLeaseConfig(6*time.Second, nil)) + seedSkewPublisher(t, p, dir) + var stops atomic.Int32 + g, err := volume.NewWorkspaceLeaseGuard(workspaceRPCView{frontend.Projection{VolumeID: "v", ReplicaID: "r", Epoch: 1, Healthy: true}}, + deadlineLeaseClient{p: p, inflight: &inflight}, &control.WorkspaceIdentity{VolumeId: "v", ReplicaId: "r", Epoch: 1}, + func(context.Context) (uint64, error) { stops.Add(1); return 7, nil }, nil) + if err != nil { + t.Fatal(err) + } + ctx, cancel := context.WithCancel(context.Background()) + runDone := make(chan error, 1) + go func() { runDone <- g.Run(ctx) }() + t.Cleanup(func() { + cancel() + select { + case <-runDone: + case <-time.After(10 * time.Second): + t.Error("guard did not settle") + } + }) + for wait := time.Now().Add(3 * time.Second); !g.Projection().Healthy; time.Sleep(20 * time.Millisecond) { + if time.Now().After(wait) { + t.Fatal("first grant did not arrive") + } + } + slow.delayNS.Store(int64(2500 * time.Millisecond)) + start := time.Now() + for time.Since(start) < 10*time.Second { + if !g.Projection().Healthy || stops.Load() != 0 { + t.Fatalf("guard fenced %s after the store slowed to 2.5 s per write (stop calls %d)", time.Since(start).Round(time.Millisecond), stops.Load()) + } + time.Sleep(50 * time.Millisecond) + } +}