Skip to content

fix stale entries before the each pick operation #3690

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

akoserwal
Copy link
Contributor

The original implementation had a critical flaw in the currentWeight map management:

  • No Cleanup Logic: The currentWeight map accumulated entries for every node address that was ever encountered
  • Stale Entries Persist: When service discovery updated the node list (nodes added/removed), old entries remained in the map
  • Weight Pollution: Stale entries could have accumulated very high or very low weights, affecting future calculations

Description (what this PR does / why we need it):

Which issue(s) this PR fixes (resolves / be part of):

Other special notes for the reviewers:

@dosubot dosubot bot added the size:M This PR changes 30-99 lines, ignoring generated files. label Jul 22, 2025
shenqidebaozi
shenqidebaozi previously approved these changes Jul 31, 2025
@dosubot dosubot bot added the LGTM label Jul 31, 2025
@shenqidebaozi shenqidebaozi requested a review from Copilot July 31, 2025 08:32
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fixes a memory leak issue in the weighted round-robin (WRR) load balancer by implementing cleanup logic for stale entries in the currentWeight map. When service discovery updates the node list, old entries were persisting in the map and could affect future weight calculations.

Key changes:

  • Added cleanup logic to remove stale entries from the currentWeight map before each pick operation
  • Moved mutex locking to encompass the entire cleanup and selection process
  • Added comprehensive test coverage to verify the cleanup functionality

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
selector/wrr/wrr.go Implements cleanup logic for stale entries and reorganizes mutex usage
selector/wrr/wrr_test.go Adds test coverage for the cleanup functionality with mock weighted nodes

@shenqidebaozi
Copy link
Member

@tonybase PTAL

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@dosubot dosubot bot removed the size:M This PR changes 30-99 lines, ignoring generated files. label Aug 4, 2025
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@dosubot dosubot bot added the size:L This PR changes 100-499 lines, ignoring generated files. label Aug 4, 2025
@go-kratos go-kratos deleted a comment from kratos-ci-bot Aug 4, 2025
@shenqidebaozi
Copy link
Member

@Windfarer @tonybase

@kratos-ci-bot
Copy link
Collaborator

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


@Windfarer @tonybase

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
LGTM size:L This PR changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants