diff --git a/seaweed-block/core/authority/workspace_validity_test.go b/seaweed-block/core/authority/workspace_validity_test.go new file mode 100644 index 0000000000..dcce84f5b4 --- /dev/null +++ b/seaweed-block/core/authority/workspace_validity_test.go @@ -0,0 +1,43 @@ +package authority + +import ( + "context" + "testing" + "time" +) + +func TestWorkspaceAckUsesGrantedHalfSkewBoundary(t *testing.T) { + for _, tc := range []struct { + name string + elapsed time.Duration + allowed bool + }{ + {"before", 28*time.Second - time.Nanosecond, true}, + {"at", 28 * time.Second, false}, + {"after", 29 * time.Second, false}, + } { + t.Run(tc.name, func(t *testing.T) { + disk, _ := newTempStore(t) + now := time.Unix(1000, 0) + p := NewPublisher(nil, WithStore(disk), WithWorkspaceLeaseConfig(30*time.Second, func() time.Time { return now }), WithWorkspaceMaxClockSkew(4*time.Second)) + if err := p.apply(AssignmentAsk{VolumeID: "v", ReplicaID: "r", DataAddr: "d", CtrlAddr: "c", Intent: IntentBind}); err != nil { + t.Fatal(err) + } + ctx := context.Background() + if _, err := p.RenewWorkspaceLease(ctx, "v", "r", 1); err != nil { + t.Fatal(err) + } + if err := p.BeginWorkspaceRevocation(ctx, "v", "r", 1); err != nil { + t.Fatal(err) + } + now = now.Add(tc.elapsed) + err := p.AckWorkspaceRevocation(ctx, "v", WorkspaceRevokeAck{ReplicaID: "r", Epoch: 1, HeadGen: 7}) + if (err == nil) != tc.allowed { + t.Fatalf("ACK allowed=%t, want %t: %v", err == nil, tc.allowed, err) + } + if (p.WorkspaceState("v").Ack != nil) != tc.allowed { + t.Fatal("ACK persistence differs from admission") + } + }) + } +} diff --git a/seaweed-block/core/host/master/workspace_skew_test.go b/seaweed-block/core/host/master/workspace_skew_test.go index a2db589ec8..a26ec75701 100644 --- a/seaweed-block/core/host/master/workspace_skew_test.go +++ b/seaweed-block/core/host/master/workspace_skew_test.go @@ -145,3 +145,30 @@ func TestWorkspaceClockSkewConfigReachesLeaseReply(t *testing.T) { t.Fatal("configuration not applied to host validity", lease, reply) } } + +// The lease reply uses the publisher clock even when it differs from this +// process's wall clock; configured skew also covers legacy leases with no bound. +func TestWorkspaceLeaseFactUsesPublisherClockAndSkew(t *testing.T) { + now := time.Unix(1000, 0) + p := authority.NewPublisher(nil, authority.WithWorkspaceLeaseConfig(12*time.Second, func() time.Time { return now }), authority.WithWorkspaceMaxClockSkew(4*time.Second)) + _ = p + for _, tc := range []struct { + name string + expiry time.Duration + skew int64 + want time.Duration + }{ + {"legacy-configured-bound", 10 * time.Second, 0, 8 * time.Second}, + {"persisted-larger-bound", 10 * time.Second, int64(6 * time.Second), 7 * time.Second}, + {"configured-ttl-cap", 40 * time.Second, 0, 12 * time.Second}, + {"expired", time.Second, 0, 0}, + } { + t.Run(tc.name, func(t *testing.T) { + l := authority.WorkspaceLease{ReplicaID: "r", Epoch: 1, ExpiresAtNS: now.Add(tc.expiry).UnixNano(), MaxClockSkewNS: tc.skew} + fact := leaseFact("v", l) + if fact.ValidForNs != int64(tc.want) { + t.Fatalf("validity=%s, want %s", time.Duration(fact.ValidForNs), tc.want) + } + }) + } +}