From e7cbfdcc44afdaed11a27e2dd9c04020e65ee212 Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Sat, 28 Nov 2026 15:31:57 -0800 Subject: [PATCH] s3: blame no filer for a deadline gRPC reports before ctx.Err() (#13098) The filer walk told the caller's expiry apart from a filer failure by ctx.Err() alone. A server-side DeadlineExceeded can reach the client before the context's own timer runs, so the walk recorded a failure against each filer and moved on with no budget left, the same race LookupVolumeIds already guards against. Co-authored-by: Claude Opus 5.5 --- weed/s3api/s3api_handlers.go | 17 ++++++++++- weed/s3api/s3api_handlers_failover_test.go | 33 ++++++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/weed/s3api/s3api_handlers.go b/weed/s3api/s3api_handlers.go index fdaa7361a1..740d704e45 100644 --- a/weed/s3api/s3api_handlers.go +++ b/weed/s3api/s3api_handlers.go @@ -13,7 +13,9 @@ import ( "github.com/seaweedfs/seaweedfs/weed/s3api/s3err" "github.com/seaweedfs/seaweedfs/weed/security" "google.golang.org/grpc" + "google.golang.org/grpc/codes" "google.golang.org/grpc/connectivity" + "google.golang.org/grpc/status" "github.com/seaweedfs/seaweedfs/weed/pb" "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb" @@ -139,7 +141,7 @@ func (s3a *S3ApiServer) withFilerClientFailover(ctx context.Context, preferred p // the next one has no time left to answer either. Recorded as a failure, a // slow master upstream would flag every filer in the walk, and the three // that open the circuit take unrelated object reads down with them. - if ctx.Err() != nil { + if callerContextEnded(ctx, err) { glog.V(2).Infof("WithFilerClient: giving up on %s, the caller's context ended: %v", filer, err) return err } @@ -175,6 +177,19 @@ func (s3a *S3ApiServer) withFilerClientFailover(ctx context.Context, preferred p // the request was provably never sent and the walk may continue. var errFilerConnNotReady = errors.New("filer connection not ready") +// callerContextEnded reports whether ctx, not the filer, ended the call. gRPC +// can surface its own DeadlineExceeded a tick before ctx.Err() becomes visible. +func callerContextEnded(ctx context.Context, err error) bool { + if ctx.Err() != nil { + return true + } + if !errors.Is(err, context.DeadlineExceeded) && status.Code(err) != codes.DeadlineExceeded { + return false + } + deadline, ok := ctx.Deadline() + return ok && !deadline.After(time.Now()) +} + // filerConnReadyWait bounds how long a candidate gets to bring its channel up // before the walk tries the next filer. const filerConnReadyWait = 2 * time.Second diff --git a/weed/s3api/s3api_handlers_failover_test.go b/weed/s3api/s3api_handlers_failover_test.go index c5afc39370..ac498f3a64 100644 --- a/weed/s3api/s3api_handlers_failover_test.go +++ b/weed/s3api/s3api_handlers_failover_test.go @@ -188,6 +188,39 @@ func TestFailoverDoesNotBlameFilersForTheCallersExpiredBudget(t *testing.T) { } } +// pastDeadlineCtx is the window after gRPC has reported the deadline but before +// the context's own timer has run: the deadline is behind it, Err is still nil. +type pastDeadlineCtx struct{ context.Context } + +func (pastDeadlineCtx) Deadline() (time.Time, bool) { return time.Now().Add(-time.Millisecond), true } + +func TestFailoverDoesNotBlameFilersForADeadlineReportedBeforeCtxErr(t *testing.T) { + firstAddr := startFakeFiler(t, &hangingCollectionFiler{}) + secondAddr := startFakeFiler(t, &hangingCollectionFiler{}) + s3a := newFailoverTestServer(t, firstAddr, secondAddr) + ctx := pastDeadlineCtx{context.Background()} + + var fnCalls int + for attempt := 0; attempt < 5; attempt++ { + err := s3a.withFilerClient(ctx, false, func(filer_pb.SeaweedFilerClient) error { + fnCalls++ + return status.Error(codes.DeadlineExceeded, "context deadline exceeded") + }) + if status.Code(err) != codes.DeadlineExceeded { + t.Fatalf("attempt %d: want the caller's deadline surfaced, got %v", attempt, err) + } + } + + for _, addr := range []pb.ServerAddress{firstAddr, secondAddr} { + if s3a.filerClient.ShouldSkipUnhealthyFiler(addr) { + t.Errorf("filer %s was flagged unhealthy for the caller's own deadline", addr) + } + } + if fnCalls != 5 { + t.Errorf("fn ran %d times over 5 attempts; the walk went on to another filer with no budget left", fnCalls) + } +} + // hangingAssignFiler answers AssignVolume only when the caller gives up, // standing in for a filer still waiting on the master for a volume. type hangingAssignFiler struct {