From 6896463a4474fa855de4e95ce813d90e7f351068 Mon Sep 17 00:00:00 2001 From: pingqiu Date: Wed, 18 Nov 2026 03:50:08 -0800 Subject: [PATCH] volume: name the cause when the workspace lease guard fences The guard fenced silently: when renewals kept failing until the lease expired, it drained the volume through blockvolume's lease-stop path, which logged nothing, and returned the raw RPC error (issue #205). - Every fence records its first cause: lease expired without a successful renewal, expired after a failed renewal (with the renewal's latency and error), an invalid reply, a reply that arrived after the deadline, revocation by the master, or shutdown. The guard logs it once as "workspace lease: fenced ...", FenceCause() returns it, and the non-ACK drain error carries it. - Every failed renewal is logged with its latency and the lease deadline, also when the lease is still valid: a run of them is the warning before an expiry fence. - An expiry after a failed renewal returns "workspace lease expired: renewal failed after : ", wrapping the RPC error. - blockvolume logs when it stops because the guard requested the drain. The fence behaviour itself is unchanged. Co-Authored-By: Claude Opus 5.5 --- seaweed-block/cmd/blockvolume/main.go | 3 + .../core/host/volume/workspace_lease.go | 68 ++++++++++++++++--- 2 files changed, 62 insertions(+), 9 deletions(-) diff --git a/seaweed-block/cmd/blockvolume/main.go b/seaweed-block/cmd/blockvolume/main.go index e1fefb3756..96935591bd 100644 --- a/seaweed-block/cmd/blockvolume/main.go +++ b/seaweed-block/cmd/blockvolume/main.go @@ -1168,6 +1168,9 @@ func run(f flags) int { select { case <-shutdownCtx.Done(): case request := <-leaseRequests: + // The lease guard fenced and asks for the drain; it logs its cause as + // "workspace lease: fenced ...". Say here that this is why we stop (#205). + fmt.Fprintln(os.Stderr, "blockvolume: stopping: the workspace lease guard fenced and requested the drain") leaseStop = &request case err := <-leaseDone: fmt.Fprintln(os.Stderr, "blockvolume: workspace lease stopped:", err) diff --git a/seaweed-block/core/host/volume/workspace_lease.go b/seaweed-block/core/host/volume/workspace_lease.go index dad21b65fb..22a8bd671b 100644 --- a/seaweed-block/core/host/volume/workspace_lease.go +++ b/seaweed-block/core/host/volume/workspace_lease.go @@ -3,6 +3,7 @@ package volume import ( "context" "fmt" + "log" "sync" "time" @@ -34,6 +35,38 @@ type WorkspaceLeaseGuard struct { stopped bool headGen uint64 revoked bool + // cause says why the guard fenced (the first reason wins); logged once, + // because the shutdown it triggers is otherwise silent (issue #205). + cause string + logged bool +} + +// fenceLocked latches the fence and records its first cause. Caller holds mu. +func (g *WorkspaceLeaseGuard) fenceLocked(cause string) { + g.fenced = true + if g.cause == "" { + g.cause = cause + } +} + +// FenceCause is why the guard fenced, or "" while it has not. +func (g *WorkspaceLeaseGuard) FenceCause() string { + g.mu.Lock() + defer g.mu.Unlock() + return g.cause +} + +// logFence logs the fence and its cause once. Never called with mu held. +func (g *WorkspaceLeaseGuard) logFence() { + g.mu.Lock() + if !g.fenced || g.logged { + g.mu.Unlock() + return + } + g.logged = true + cause := g.cause + g.mu.Unlock() + log.Printf("workspace lease: fenced volume=%s replica=%s epoch=%d: %s", g.id.VolumeId, g.id.ReplicaId, g.id.Epoch, cause) } func NewWorkspaceLeaseGuard(base frontend.ProjectionView, client WorkspaceLeaseClient, id *control.WorkspaceIdentity, stop func(context.Context) (uint64, error), now func() time.Time) (*WorkspaceLeaseGuard, error) { @@ -58,7 +91,7 @@ func (g *WorkspaceLeaseGuard) permits(p frontend.Projection) bool { g.mu.Lock() defer g.mu.Unlock() if !g.until.IsZero() && !g.now().Before(g.until) { - g.fenced = true + g.fenceLocked(fmt.Sprintf("lease expired at %s without a successful renewal", g.until.UTC().Format(time.RFC3339Nano))) } return !g.fenced && !g.until.IsZero() && p.VolumeID == g.id.VolumeId && p.ReplicaID == g.id.ReplicaId && p.Epoch == g.id.Epoch } @@ -73,11 +106,12 @@ func (g *WorkspaceLeaseGuard) Refresh(ctx context.Context) error { start := g.now() g.mu.Lock() if !g.until.IsZero() && !start.Before(g.until) { - g.fenced = true + g.fenceLocked(fmt.Sprintf("lease expired at %s without a successful renewal", g.until.UTC().Format(time.RFC3339Nano))) } fenced, until := g.fenced, g.until g.mu.Unlock() if fenced { + g.logFence() return g.drain(ctx, g.revoked) } rpcCtx, cancel := context.WithTimeout(ctx, 2*time.Second) @@ -89,40 +123,52 @@ func (g *WorkspaceLeaseGuard) Refresh(ctx context.Context) error { defer cancel() reply, err := g.client.RenewWorkspaceLease(rpcCtx, &g.id) if err != nil { + elapsed := g.now().Sub(start).Round(time.Millisecond) g.mu.Lock() expired := !g.until.IsZero() && !g.now().Before(g.until) if expired { - g.fenced = true + g.fenceLocked(fmt.Sprintf("lease expired after a failed renewal (renewal failed after %s: %v)", elapsed, err)) } g.mu.Unlock() + // Every failed renewal is visible with its latency: a run of them is the + // warning before an expiry fence (issue #205). + log.Printf("workspace lease: renewal failed after %s volume=%s epoch=%d (lease valid until %s): %v", + elapsed, g.id.VolumeId, g.id.Epoch, until.UTC().Format(time.RFC3339Nano), err) if expired { + g.logFence() _ = g.drain(ctx, false) + return fmt.Errorf("workspace lease expired: renewal failed after %s: %w", elapsed, err) } return err } if reply.GetVolumeId() != g.id.VolumeId || reply.GetReplicaId() != g.id.ReplicaId || reply.GetEpoch() != g.id.Epoch || reply.GetValidForNs() <= 0 || reply.GetValidForNs() > int64(30*time.Second) { g.mu.Lock() - g.fenced = true + g.fenceLocked(fmt.Sprintf("invalid lease reply (volume=%q replica=%q epoch=%d validForNs=%d)", reply.GetVolumeId(), reply.GetReplicaId(), reply.GetEpoch(), reply.GetValidForNs())) g.mu.Unlock() + g.logFence() _ = g.drain(ctx, false) return fmt.Errorf("invalid workspace lease reply") } g.mu.Lock() // Check the OLD deadline before accepting a newer grant. In particular a // reply received after expiry cannot undo a fence latched by a frontend. - if ctx.Err() != nil || rpcCtx.Err() != nil || (!g.until.IsZero() && !g.now().Before(g.until)) || !g.now().Before(start.Add(time.Duration(reply.ValidForNs))) { - g.fenced = true + switch { + case ctx.Err() != nil: + g.fenceLocked("shutdown while a renewal was in flight") + case rpcCtx.Err() != nil || (!g.until.IsZero() && !g.now().Before(g.until)) || !g.now().Before(start.Add(time.Duration(reply.ValidForNs))): + g.fenceLocked(fmt.Sprintf("renewal reply arrived after the lease deadline (renewal took %s)", g.now().Sub(start).Round(time.Millisecond))) } if !g.fenced { g.until = start.Add(time.Duration(reply.ValidForNs)) } if reply.Revoking { - g.fenced = true + g.fenceLocked("revoked by the master") g.revoked = true } closed := g.fenced g.mu.Unlock() if closed { + g.logFence() return g.drain(ctx, reply.Revoking) } return nil @@ -159,7 +205,10 @@ func (g *WorkspaceLeaseGuard) drain(parent context.Context, ack bool) error { _, err := g.client.AckWorkspaceRevoked(ctx, &control.WorkspaceRevocationAck{Identity: &g.id, HeadGen: g.headGen}) return err } - return fmt.Errorf("workspace lease fenced") + g.mu.Lock() + cause := g.cause + g.mu.Unlock() + return fmt.Errorf("workspace lease fenced: %s", cause) } // Run owns renewal and physical close. Projection independently checks expiry, @@ -189,8 +238,9 @@ func (g *WorkspaceLeaseGuard) Run(ctx context.Context) error { case <-ctx.Done(): g.operation.Lock() g.mu.Lock() - g.fenced = true + g.fenceLocked("shutdown requested (context cancelled)") g.mu.Unlock() + g.logFence() err := g.drain(ctx, false) g.operation.Unlock() return err