mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-05 10:37:23 +01:00
wdclient: use a master only after it answers as the leader (#13415)
* wdclient: use a master only after it answers as the leader Since the announce gate was added, every client marked a master as current as soon as its KeepConnected stream opened, before the master answered. When that master is a follower, its answer only names the leader, but calls made in the meantime go to the follower, and leader-only ones (lock, volume.list, collection.*, ...) fail with "raft.Server: Not current leader". A weed shell fed a script runs its first command right in that window. Keep the early hand-out for clients behind an announce gate (a starting filer), and let other clients wait for the first answer as before. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * wdclient: start the follower check only after it got the registration Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * wdclient: hand out a master early only while the announce gate is closed A filer that already announced and reconnects after a failover kept the early hand-out, so calls waiting in GetMaster went to a follower that had not answered yet and failed with "Not current leader". Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * wdclient: stop handing out a master once it redirects to the leader A filer that reached a follower behind its announce gate kept that follower as the current master after the redirect, until the leader answered, so leader-only calls in that window failed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * wdclient: keep the redirecting master when canceled before the leader answers Clearing the current master on a redirect left it empty if the client was canceled before the hinted leader answered, so a later GetMaster without a deadline blocked forever. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
4a58968439
commit
39f568f614
2 files changed
+201
-8
No files matched your search
@@ -321,7 +321,14 @@ func (mc *MasterClient) resetVidMap() {
|
||||
}
|
||||
|
||||
func (mc *MasterClient) tryAllMasters(ctx context.Context) {
|
||||
var nextHintedLeader pb.ServerAddress
|
||||
var nextHintedLeader, redirectedFrom pb.ServerAddress
|
||||
// A canceled client keeps an address for unary queries (see below), so
|
||||
// give back the redirecting master if the hinted leader never answered.
|
||||
defer func() {
|
||||
if ctx.Err() != nil && redirectedFrom != "" && mc.getCurrentMaster() == "" {
|
||||
mc.setCurrentMaster(redirectedFrom)
|
||||
}
|
||||
}()
|
||||
failedMasters := make(map[pb.ServerAddress]struct{})
|
||||
mc.refreshMasters()
|
||||
masterAddrs := mc.masterInstances()
|
||||
@@ -340,6 +347,11 @@ func (mc *MasterClient) tryAllMasters(ctx context.Context) {
|
||||
return
|
||||
default:
|
||||
target := nextHintedLeader
|
||||
// the redirecting master cannot serve leader-only calls
|
||||
if current := mc.getCurrentMaster(); current != "" {
|
||||
redirectedFrom = current
|
||||
}
|
||||
mc.setCurrentMaster("")
|
||||
nextHintedLeader = mc.tryConnectToMaster(ctx, target)
|
||||
if nextHintedLeader == "" {
|
||||
// connection to target failed; remember it so we skip
|
||||
@@ -353,6 +365,7 @@ func (mc *MasterClient) tryAllMasters(ctx context.Context) {
|
||||
// only clear the serving address on an unintended disconnect.
|
||||
if ctx.Err() == nil {
|
||||
mc.setCurrentMaster("")
|
||||
redirectedFrom = ""
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -373,16 +386,22 @@ func (mc *MasterClient) tryConnectToMaster(ctx context.Context, master pb.Server
|
||||
}
|
||||
glog.V(1).Infof("%s.%s masterClient gRPC stream established to %s in %v", mc.FilerGroup, mc.clientType, master, time.Since(connectStartTime))
|
||||
|
||||
// The stream is usable for queries before registration, so callers
|
||||
// blocked in GetMaster (e.g. loading a chunked filer.conf during
|
||||
// startup) proceed while the announce gate still holds.
|
||||
mc.setCurrentMaster(master)
|
||||
|
||||
if mc.announceCh != nil {
|
||||
select {
|
||||
case <-mc.announceCh:
|
||||
case <-ctx.Done():
|
||||
return ctx.Err()
|
||||
default:
|
||||
// The stream is usable for queries before registration, so
|
||||
// callers blocked in GetMaster (e.g. loading a chunked
|
||||
// filer.conf during startup) proceed while the announce gate
|
||||
// still holds. Otherwise wait for the first answer: a follower
|
||||
// only names the leader, and queries sent to it fail with
|
||||
// "Not current leader".
|
||||
mc.setCurrentMaster(master)
|
||||
select {
|
||||
case <-mc.announceCh:
|
||||
case <-ctx.Done():
|
||||
return ctx.Err()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@ package wdclient
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -62,3 +63,176 @@ func TestAnnounceGateDelaysRegistration(t *testing.T) {
|
||||
t.Fatalf("registration ClientAddress = %q, want filer1:18888", req.ClientAddress)
|
||||
}
|
||||
}
|
||||
|
||||
// fakeFollowerServer answers once release is closed. With no leader set it
|
||||
// answers as the leader and keeps the stream open.
|
||||
type fakeFollowerServer struct {
|
||||
master_pb.UnimplementedSeaweedServer
|
||||
leader pb.ServerAddress
|
||||
registered chan struct{}
|
||||
release chan struct{}
|
||||
}
|
||||
|
||||
func (s *fakeFollowerServer) KeepConnected(stream master_pb.Seaweed_KeepConnectedServer) error {
|
||||
if _, err := stream.Recv(); err != nil {
|
||||
return err
|
||||
}
|
||||
select {
|
||||
case s.registered <- struct{}{}:
|
||||
default:
|
||||
}
|
||||
select {
|
||||
case <-s.release:
|
||||
case <-stream.Context().Done():
|
||||
return stream.Context().Err()
|
||||
}
|
||||
if err := stream.Send(&master_pb.KeepConnectedResponse{VolumeLocation: &master_pb.VolumeLocation{Leader: string(s.leader)}}); err != nil || s.leader != "" {
|
||||
return err
|
||||
}
|
||||
<-stream.Context().Done()
|
||||
return nil
|
||||
}
|
||||
|
||||
// A client without an announce gate, or one that already announced (e.g.
|
||||
// reconnecting after a failover), must not hand out a follower it is still
|
||||
// waiting on: leader-only calls sent there fail with "Not current leader".
|
||||
func TestGetMasterWaitsForLeaderAnswer(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
clientType string
|
||||
announced bool
|
||||
}{
|
||||
{"no announce gate", "shell", false},
|
||||
{"announce gate open", "filer", true},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
// distinct http ports, since masters compare by their http address
|
||||
leader := pb.ServerAddress(strings.Replace(string(startFakeMasterServer(t, &fakeKeepConnectedServer{requests: make(chan *master_pb.KeepConnectedRequest, 4)})), ":0.", ":1.", 1))
|
||||
follower := &fakeFollowerServer{leader: leader, registered: make(chan struct{}, 1), release: make(chan struct{})}
|
||||
followerAddr := pb.ServerAddress(strings.Replace(string(startFakeMasterServer(t, follower)), ":0.", ":2.", 1))
|
||||
mc := NewMasterClient(
|
||||
grpc.WithTransportCredentials(insecure.NewCredentials()),
|
||||
"", tc.clientType, "", "", "",
|
||||
*pb.NewServiceDiscoveryFromMap(map[string]pb.ServerAddress{"m": followerAddr}),
|
||||
)
|
||||
if tc.announced {
|
||||
gate := make(chan struct{})
|
||||
close(gate)
|
||||
mc.SetAnnounceCh(gate)
|
||||
}
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
t.Cleanup(cancel)
|
||||
go mc.KeepConnectedToMaster(ctx)
|
||||
|
||||
select {
|
||||
case <-follower.registered:
|
||||
case <-time.After(5 * time.Second):
|
||||
t.Fatal("follower received no KeepConnected registration")
|
||||
}
|
||||
waitCtx, waitCancel := context.WithTimeout(ctx, 500*time.Millisecond)
|
||||
defer waitCancel()
|
||||
if m := mc.GetMaster(waitCtx); m != "" {
|
||||
t.Fatalf("GetMaster = %q before any master answered", m)
|
||||
}
|
||||
|
||||
close(follower.release)
|
||||
waitCtx, waitCancel = context.WithTimeout(ctx, 5*time.Second)
|
||||
defer waitCancel()
|
||||
if m := mc.GetMaster(waitCtx); m != leader {
|
||||
t.Fatalf("GetMaster = %q, want leader %q", m, leader)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// A filer that reached a follower behind its announce gate must stop handing
|
||||
// it out once the follower redirects, until the leader answers.
|
||||
func TestGetMasterDropsRedirectingFollower(t *testing.T) {
|
||||
leaderSrv := &fakeFollowerServer{registered: make(chan struct{}, 1), release: make(chan struct{})}
|
||||
leader := pb.ServerAddress(strings.Replace(string(startFakeMasterServer(t, leaderSrv)), ":0.", ":1.", 1))
|
||||
follower := &fakeFollowerServer{leader: leader, registered: make(chan struct{}, 1), release: make(chan struct{})}
|
||||
close(follower.release)
|
||||
followerAddr := pb.ServerAddress(strings.Replace(string(startFakeMasterServer(t, follower)), ":0.", ":2.", 1))
|
||||
mc := NewMasterClient(
|
||||
grpc.WithTransportCredentials(insecure.NewCredentials()),
|
||||
"", "filer", "filer1:18888", "", "",
|
||||
*pb.NewServiceDiscoveryFromMap(map[string]pb.ServerAddress{"m": followerAddr}),
|
||||
)
|
||||
gate := make(chan struct{})
|
||||
mc.SetAnnounceCh(gate)
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
t.Cleanup(cancel)
|
||||
go mc.KeepConnectedToMaster(ctx)
|
||||
|
||||
waitCtx, waitCancel := context.WithTimeout(ctx, 3*time.Second)
|
||||
defer waitCancel()
|
||||
if m := mc.GetMaster(waitCtx); m != followerAddr {
|
||||
t.Fatalf("GetMaster behind the gate = %q, want %q", m, followerAddr)
|
||||
}
|
||||
|
||||
close(gate)
|
||||
select {
|
||||
case <-leaderSrv.registered:
|
||||
case <-time.After(5 * time.Second):
|
||||
t.Fatal("leader received no KeepConnected registration")
|
||||
}
|
||||
waitCtx, waitCancel = context.WithTimeout(ctx, 500*time.Millisecond)
|
||||
defer waitCancel()
|
||||
if m := mc.GetMaster(waitCtx); m != "" {
|
||||
t.Fatalf("GetMaster = %q after the follower redirected, before the leader answered", m)
|
||||
}
|
||||
|
||||
close(leaderSrv.release)
|
||||
waitCtx, waitCancel = context.WithTimeout(ctx, 5*time.Second)
|
||||
defer waitCancel()
|
||||
if m := mc.GetMaster(waitCtx); m != leader {
|
||||
t.Fatalf("GetMaster = %q, want leader %q", m, leader)
|
||||
}
|
||||
}
|
||||
|
||||
// Canceling the client while it waits on the hinted leader keeps an address,
|
||||
// so unary queries after the cancel do not block forever.
|
||||
func TestGetMasterAfterCancelDuringRedirect(t *testing.T) {
|
||||
leaderSrv := &fakeFollowerServer{registered: make(chan struct{}, 1), release: make(chan struct{})}
|
||||
leader := pb.ServerAddress(strings.Replace(string(startFakeMasterServer(t, leaderSrv)), ":0.", ":1.", 1))
|
||||
follower := &fakeFollowerServer{leader: leader, registered: make(chan struct{}, 1), release: make(chan struct{})}
|
||||
close(follower.release)
|
||||
followerAddr := pb.ServerAddress(strings.Replace(string(startFakeMasterServer(t, follower)), ":0.", ":2.", 1))
|
||||
mc := NewMasterClient(
|
||||
grpc.WithTransportCredentials(insecure.NewCredentials()),
|
||||
"", "filer", "filer1:18888", "", "",
|
||||
*pb.NewServiceDiscoveryFromMap(map[string]pb.ServerAddress{"m": followerAddr}),
|
||||
)
|
||||
gate := make(chan struct{})
|
||||
mc.SetAnnounceCh(gate)
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
t.Cleanup(cancel)
|
||||
done := make(chan struct{})
|
||||
go func() {
|
||||
mc.KeepConnectedToMaster(ctx)
|
||||
close(done)
|
||||
}()
|
||||
|
||||
waitCtx, waitCancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||
defer waitCancel()
|
||||
if m := mc.GetMaster(waitCtx); m != followerAddr {
|
||||
t.Fatalf("GetMaster behind the gate = %q, want %q", m, followerAddr)
|
||||
}
|
||||
close(gate)
|
||||
select {
|
||||
case <-leaderSrv.registered:
|
||||
case <-time.After(5 * time.Second):
|
||||
t.Fatal("leader received no KeepConnected registration")
|
||||
}
|
||||
cancel()
|
||||
select {
|
||||
case <-done:
|
||||
case <-time.After(5 * time.Second):
|
||||
t.Fatal("KeepConnectedToMaster did not stop after cancel")
|
||||
}
|
||||
waitCtx, waitCancel = context.WithTimeout(context.Background(), 500*time.Millisecond)
|
||||
defer waitCancel()
|
||||
if m := mc.GetMaster(waitCtx); m != followerAddr {
|
||||
t.Fatalf("GetMaster after cancel = %q, want %q", m, followerAddr)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user