From 8b8fa8d260d1b86bbc58aa844796182a646e4e11 Mon Sep 17 00:00:00 2001 From: chrislu Date: Thu, 20 Nov 2025 13:16:15 -0800 Subject: [PATCH] fix: restore leader change detection in KeepConnected stream loop Critical fix: Leader change detection was accidentally removed from the streaming loop - Master can announce leader changes during an active KeepConnected stream - Without this check, client continues talking to non-leader until connection breaks - This can lead to stale data or operational errors The check needs to be in TWO places: 1. Initial response (lines 178-187): Detect redirect on first connect 2. Stream loop (lines 203-209): Detect leader changes during active stream Restored the loop check that was accidentally removed during refactoring. This ensures the client immediately reconnects to new leader when announced. --- weed/wdclient/masterclient.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/weed/wdclient/masterclient.go b/weed/wdclient/masterclient.go index 42016f416..696a4429f 100644 --- a/weed/wdclient/masterclient.go +++ b/weed/wdclient/masterclient.go @@ -199,6 +199,14 @@ func (mc *MasterClient) tryConnectToMaster(ctx context.Context, master pb.Server // } if resp.VolumeLocation != nil { + // Check for leader change during the stream + // If master announces a new leader, reconnect to it + if resp.VolumeLocation.Leader != "" && string(mc.GetMaster(ctx)) != resp.VolumeLocation.Leader { + glog.V(0).Infof("currentMaster %v redirected to leader %v", mc.GetMaster(ctx), resp.VolumeLocation.Leader) + nextHintedLeader = pb.ServerAddress(resp.VolumeLocation.Leader) + stats.MasterClientConnectCounter.WithLabelValues(stats.RedirectedToLeader).Inc() + return nil + } mc.updateVidMap(resp) } if resp.ClusterNodeUpdate != nil {