Fix GetShardLeader returns old leader (#22887)

Signed-off-by: yah01 <yang.cen@zilliz.com>
pull/22900/head
yah01 2023-03-21 16:57:57 +08:00 committed by GitHub
parent fd974df09f
commit 68b9cabb87
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 40 additions and 0 deletions

View File

@ -195,10 +195,19 @@ func (dh *distHandler) updateLeaderView(resp *querypb.GetDataDistributionRespons
}
}
var version int64
for _, channel := range resp.GetChannels() {
if channel.GetChannel() == lview.GetChannel() {
version = channel.GetVersion()
break
}
}
view := &meta.LeaderView{
ID: resp.GetNodeID(),
CollectionID: lview.GetCollection(),
Channel: lview.GetChannel(),
Version: version,
Segments: lview.GetSegmentDist(),
GrowingSegments: segments,
}

View File

@ -374,3 +374,31 @@ func checkNodeAvailable(nodeID int64, info *session.NodeInfo) error {
}
return nil
}
func filterDupLeaders(replicaManager *meta.ReplicaManager, leaders map[int64]*meta.LeaderView) map[int64]*meta.LeaderView {
type leaderID struct {
ReplicaID int64
Shard string
}
newLeaders := make(map[leaderID]*meta.LeaderView)
for _, view := range leaders {
replica := replicaManager.GetByCollectionAndNode(view.CollectionID, view.ID)
if replica == nil {
continue
}
id := leaderID{replica.GetID(), view.Channel}
if old, ok := newLeaders[id]; ok && old.Version > view.Version {
continue
}
newLeaders[id] = view
}
result := make(map[int64]*meta.LeaderView)
for _, v := range newLeaders {
result[v.ID] = v
}
return result
}

View File

@ -27,6 +27,7 @@ type LeaderView struct {
ID int64
CollectionID int64
Channel string
Version int64
Segments map[int64]*querypb.SegmentDist
GrowingSegments map[int64]*Segment
}
@ -46,6 +47,7 @@ func (view *LeaderView) Clone() *LeaderView {
ID: view.ID,
CollectionID: view.CollectionID,
Channel: view.Channel,
Version: view.Version,
Segments: segments,
GrowingSegments: growings,
}

View File

@ -859,6 +859,7 @@ func (s *Server) GetShardLeaders(ctx context.Context, req *querypb.GetShardLeade
log := log.With(zap.String("channel", channel.GetChannelName()))
leaders := s.dist.LeaderViewManager.GetLeadersByShard(channel.GetChannelName())
leaders = filterDupLeaders(s.meta.ReplicaManager, leaders)
ids := make([]int64, 0, len(leaders))
addrs := make([]string, 0, len(leaders))