Skip to content

feat(loadbalance): add loadbalancer using Alias Method to reduce the time complexity of weighted random load balancing algorithms#1199

Merged
ppzqh merged 10 commits into
cloudwego:developfrom
nickxudotme:feat/loadblance
Jan 23, 2024
Merged

feat(loadbalance): add loadbalancer using Alias Method to reduce the time complexity of weighted random load balancing algorithms#1199
ppzqh merged 10 commits into
cloudwego:developfrom
nickxudotme:feat/loadblance

Conversation

@nickxudotme
Copy link
Copy Markdown
Contributor

@nickxudotme nickxudotme commented Dec 10, 2023

What type of PR is this?

Check the PR title.

  • This PR title match the format: <type>(optional scope): <description>
  • The description of this PR title is user-oriented and clear enough for others to understand.
  • Attach the PR updating the user documentation if the current PR requires user awareness at the usage level. User docs repo

(Optional) Translate the PR title into Chinese.

添加一种使用 Alias Method 的负载均衡方法,来减少权重随机负载均衡算法的时间复杂度

(Optional) More detailed description for this PR(en: English/zh: Chinese).

en:

image

Alias Method:

https://en.wikipedia.org/wiki/Alias_method
http://www.keithschwarz.com/darts-dice-coins/

zh(optional):

https://nickxu.me/post/darts-dice-coins-chinese-version

(Optional) Which issue(s) this PR fixes:

#1184

(optional) The PR that updates user documentation:

@nickxudotme nickxudotme requested review from a team as code owners December 10, 2023 09:32
@CLAassistant
Copy link
Copy Markdown

CLAassistant commented Dec 10, 2023

CLA assistant check
All committers have signed the CLA.

@nickxudotme nickxudotme changed the title feat(lb): add loadbalancer using alias method (#1184) feat(lb): add loadbalancer using Alias Method (#1184) Dec 10, 2023
@codecov
Copy link
Copy Markdown

codecov Bot commented Dec 11, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (a6d5d90) 66.89% compared to head (b2ff131) 66.97%.

Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #1199      +/-   ##
===========================================
+ Coverage    66.89%   66.97%   +0.07%     
===========================================
  Files          256      257       +1     
  Lines        19479    19534      +55     
===========================================
+ Hits         13031    13082      +51     
- Misses        5271     5282      +11     
+ Partials      1177     1170       -7     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@ppzqh
Copy link
Copy Markdown
Contributor

ppzqh commented Dec 11, 2023

thanks for the contribution. Please fix the ci issue and add the reference of this algorithm in the pr description

Comment thread pkg/loadbalance/weighted_random_with_alias_method.go Outdated
Comment thread pkg/loadbalance/weighted_random_with_alias_method.go Outdated
@ppzqh
Copy link
Copy Markdown
Contributor

ppzqh commented Dec 13, 2023

Can we add a benchmark for init, which is also used when rebalance.

@nickxudotme
Copy link
Copy Markdown
Contributor Author

BenchmarkAliasMethodPickerInit

goos: darwin
goarch: arm64
pkg: github.com/cloudwego/kitex/pkg/loadbalance
BenchmarkAliasMethodPickerInit
BenchmarkAliasMethodPickerInit/10ins
BenchmarkAliasMethodPickerInit/10ins-8         	 9356866	       136.7 ns/op	     400 B/op	       5 allocs/op
BenchmarkAliasMethodPickerInit/100ins
BenchmarkAliasMethodPickerInit/100ins-8        	 1512398	       848.7 ns/op	    4480 B/op	       5 allocs/op
BenchmarkAliasMethodPickerInit/1000ins
BenchmarkAliasMethodPickerInit/1000ins-8       	  180366	      6993 ns/op	   40960 B/op	       5 allocs/op
BenchmarkAliasMethodPickerInit/10000ins
BenchmarkAliasMethodPickerInit/10000ins-8      	   13801	     92550 ns/op	  409604 B/op	       5 allocs/op
PASS

ppzqh
ppzqh previously approved these changes Dec 19, 2023
@ppzqh
Copy link
Copy Markdown
Contributor

ppzqh commented Dec 19, 2023

LGTM

Comment thread pkg/loadbalance/weighted_random_with_alias_method_test.go Outdated
@nickxudotme
Copy link
Copy Markdown
Contributor Author

我觉得这个 PR 已经可以和进去了(但是貌似会报一些我感觉奇怪的 CI 问题

@ppzqh
Copy link
Copy Markdown
Contributor

ppzqh commented Jan 8, 2024

我觉得这个 PR 已经可以和进去了(但是貌似会报一些我感觉奇怪的 CI 问题

什么问题?

@nickxudotme
Copy link
Copy Markdown
Contributor Author

我觉得这个 PR 已经可以和进去了(但是貌似会报一些我感觉奇怪的 CI 问题

什么问题?

review 通过一下看看👀

ppzqh
ppzqh previously approved these changes Jan 8, 2024
@AsterDY
Copy link
Copy Markdown
Contributor

AsterDY commented Jan 8, 2024

kitex/pkg/loadbalance/weighted_balancer_test.go at 834c15a5e6c7def209f257246122803ed1e77b92 · cloudw 发现这个 BenchmarkWeightedPicker_Next 测试结果有点奇怪:“weitghted_random” 的性能并没有随着n增大而裂化(明明是O(N)),导致新算法的收益看起来不是很明显。初步debug看了下传到weightedRandomPicker.Next()里面的instances一直都是10估计是有bug,cc @ppzqh 。 同学如果有兴趣也帮忙看看问题在哪吧 @NX-Official

@ppzqh
Copy link
Copy Markdown
Contributor

ppzqh commented Jan 8, 2024

这个单测问题我修了,辛苦rebase一下,再跑个测试

@nickxudotme nickxudotme requested a review from jizhuozhi January 20, 2024 07:25
Copy link
Copy Markdown
Contributor

@jizhuozhi jizhuozhi left a comment

Choose a reason for hiding this comment

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

LGTM

@ppzqh ppzqh merged commit 37fc52b into cloudwego:develop Jan 23, 2024
@YangruiEmma YangruiEmma changed the title feat(lb): add loadbalancer using Alias Method (#1184) feat(loadbalance): add loadbalancer using Alias Method (#1184) Mar 4, 2024
@YangruiEmma YangruiEmma changed the title feat(loadbalance): add loadbalancer using Alias Method (#1184) feat(loadbalance): add loadbalancer using Alias Method to reduce the time complexity of weighted random load balancing algorithms Mar 4, 2024
@ppzqh
Copy link
Copy Markdown
Contributor

ppzqh commented Mar 11, 2024

@NX-Official Hi, 这个alis method的功能要辛苦你帮忙补充一个介绍文档。可以参考官网文档进行添加,包括中英文两个文件。感谢你的支持!
官网:https://www.cloudwego.io/zh/docs/kitex/tutorials/service-governance/loadbalance/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

5 participants