Files
seaweedfs/weed/operation/assign_file_id_retry_test.go
T
329ccb8bc0 master: tell a warming-up Assign or LookupVolume client when warm-up ends (#12386)
* master: tell a warming-up Assign client when warm-up ends

During warm-up the master refuses Assign with Unavailable and now attaches
the remaining warm-up as a gRPC RetryInfo detail. RetryWithBackoff may
shorten a backoff step to that hint plus 50 ms, never lengthen it, and only
for a valid, positive hint, so a writer retries right after warm-up ends
instead of up to one backoff step later. The warm-up itself is unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* master: jitter the retry-hint slack and hint LookupVolume too

Per review: the slack added to a server hint is now random in
[50ms, 300ms], so clients refused at the same moment do not all retry
in one instant. The warm-up refusal of LookupVolume carries the same
RetryInfo hint as Assign; the masterclient's lookup retry already goes
through RetryWithBackoff, so it lands at warm-up end too.

Tests: the slow guard tests are replaced by one sleep-free table test
of the step logic (zero, negative, huge, malformed, valid and wrapped
hints, a hint longer than the step, none). One timing test of the
shortened step stays, plus a jitter-bounds test and a LookupVolume hint
test. The no-hint control test, which was timing-sensitive, is dropped.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* master: name the shared warm-up test and file the lookup test by topic

Rename the no-remaining test to TestWarmingUpErrorWithoutRemainingHasNoHint,
since the helper now serves Assign and LookupVolume, and move the
LookupVolume hint test next to the other LookupVolume warm-up tests.
Tests only.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* util: read the retry hint through ServerStatus

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* master: bound the warm-up hint in tests by the remaining warm-up around the RPC

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* test: leave more scheduler margin in the retry-hint timing tests

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* util, master: trim retry-hint comments

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* test: widen the retry-hint timing bounds to just under the unhinted wait

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* test: require a refused warm-up call before the hinted assign retry

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* test: check the hinted retry wait through a test timer, not the wall clock

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* test: move the assign warm-up end to 2.6s for more hinted-retry headroom

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* test: require the hinted retry to wait out its timer

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Chris Lu <chrislusf@users.noreply.github.com>
2026-11-03 20:46:58 -08:00

150 lines
5.0 KiB
Go

package operation
import (
"context"
"errors"
"fmt"
"net"
"sync/atomic"
"testing"
"time"
"github.com/seaweedfs/seaweedfs/weed/pb"
"github.com/seaweedfs/seaweedfs/weed/pb/master_pb"
"google.golang.org/genproto/googleapis/rpc/errdetails"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/durationpb"
)
// fakeAssignServer returns Unavailable for the first N calls, then succeeds.
type fakeAssignServer struct {
master_pb.UnimplementedSeaweedServer
unavailableCount int32
callCount atomic.Int32
}
func (s *fakeAssignServer) Assign(_ context.Context, _ *master_pb.AssignRequest) (*master_pb.AssignResponse, error) {
n := s.callCount.Add(1)
if n <= s.unavailableCount {
return nil, status.Errorf(codes.Unavailable, "master is warming up")
}
return &master_pb.AssignResponse{
Fid: "1,abc",
Count: 1,
Location: &master_pb.Location{
Url: "127.0.0.1:8080",
PublicUrl: "127.0.0.1:8080",
},
}, nil
}
func startFakeServer(t *testing.T, srv master_pb.SeaweedServer) pb.ServerAddress {
t.Helper()
lis, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatalf("listen: %v", err)
}
grpcServer := grpc.NewServer()
master_pb.RegisterSeaweedServer(grpcServer, srv)
go func() { _ = grpcServer.Serve(lis) }()
t.Cleanup(grpcServer.GracefulStop)
_, port, _ := net.SplitHostPort(lis.Addr().String())
// Use "0.<grpcPort>" format so ToGrpcAddress() resolves to the actual port
return pb.ServerAddress(fmt.Sprintf("127.0.0.1:0.%s", port))
}
func TestAssignRetriesOnUnavailable(t *testing.T) {
srv := &fakeAssignServer{unavailableCount: 3}
addr := startFakeServer(t, srv)
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
ret, err := Assign(ctx, func(_ context.Context) pb.ServerAddress {
return addr
}, grpc.WithTransportCredentials(insecure.NewCredentials()), &VolumeAssignRequest{Count: 1})
if err != nil {
t.Fatalf("expected success after retries, got error: %v", err)
}
if ret.Fid != "1,abc" {
t.Errorf("expected fid '1,abc', got '%s'", ret.Fid)
}
if calls := srv.callCount.Load(); calls != 4 {
t.Errorf("expected 4 calls (3 unavailable + 1 success), got %d", calls)
}
}
func TestAssignStopsOnContextCancel(t *testing.T) {
srv := &fakeAssignServer{unavailableCount: 1000} // never succeeds
addr := startFakeServer(t, srv)
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
start := time.Now()
_, err := Assign(ctx, func(_ context.Context) pb.ServerAddress {
return addr
}, grpc.WithTransportCredentials(insecure.NewCredentials()), &VolumeAssignRequest{Count: 1})
elapsed := time.Since(start)
if err == nil {
t.Fatal("expected error from context cancellation")
}
// Should stop within a reasonable time after context deadline
if elapsed > 5*time.Second {
t.Errorf("took %v, expected to stop near context deadline of 2s", elapsed)
}
// Verify the loop actually retried (not just an immediate failure)
if calls := srv.callCount.Load(); calls <= 1 {
t.Errorf("expected multiple retry attempts, got %d calls", calls)
}
// Verify the error is from context deadline
if !errors.Is(err, context.DeadlineExceeded) {
t.Errorf("expected context.DeadlineExceeded, got: %v", err)
}
}
// warmingAssignServer refuses Assign until warmEnd, like a master in warm-up,
// telling the client the remaining warm-up.
type warmingAssignServer struct {
master_pb.UnimplementedSeaweedServer
warmEnd time.Time
callCount atomic.Int32
}
func (s *warmingAssignServer) Assign(_ context.Context, _ *master_pb.AssignRequest) (*master_pb.AssignResponse, error) {
s.callCount.Add(1)
if remaining := time.Until(s.warmEnd); remaining > 0 {
st, _ := status.New(codes.Unavailable, "master is warming up").WithDetails(&errdetails.RetryInfo{RetryDelay: durationpb.New(remaining)})
return nil, st.Err()
}
return &master_pb.AssignResponse{Fid: "1,abc", Count: 1, Location: &master_pb.Location{Url: "127.0.0.1:8080", PublicUrl: "127.0.0.1:8080"}}, nil
}
func TestAssignRetryLandsAtWarmupEndWithHint(t *testing.T) {
const warmup = 2600 * time.Millisecond
srv := &warmingAssignServer{}
addr := startFakeServer(t, srv)
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
start := time.Now()
srv.warmEnd = start.Add(warmup)
if _, err := Assign(ctx, func(_ context.Context) pb.ServerAddress { return addr },
grpc.WithTransportCredentials(insecure.NewCredentials()), &VolumeAssignRequest{Count: 1}); err != nil {
t.Fatalf("Assign: %v", err)
}
// The hint lands the retry just after warm-up ends (2.6s), not at the
// next backoff step (1s + 1.5s + 2.25s = 4.75s).
if elapsed := time.Since(start); elapsed > 4500*time.Millisecond {
t.Fatalf("assign took %v over %d calls, want about 2.75s (warm-up end)", elapsed, srv.callCount.Load())
}
if calls := srv.callCount.Load(); calls < 2 {
t.Fatalf("got %d calls, want a refused call during warm-up before the retry", calls)
}
}