Skip to content
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

added iwrr dispatch feature #30

Merged

Conversation

guowei-gong
Copy link
Contributor

交错式加权轮询负载均衡算法 interleaved weighted round robin (iwrr) ,时间复杂度O(1),空间复杂度O(n)

实现思路

1.实现中维护两个队列,分别是当前队列current queue和下一次队列next queue。队列中的每个元素entry都维护weight表示在当前队列中任选中次数权重

2.最初时weight与entry权重相同,每次被选中后都会减少step(使用GCD公约数来调整个周期)

3.当weight为0时表示当前周期内不可调度,将其放在下一次队列中。当当前队列current queue为empty时, 表示当前周期所有元素entry都被按权重选择结束,此时对换当前队列和下一次队列开始新的周期。

4.由于始终重复用链表节点,因此该负载均衡算法在初始化后实现可以保证是zero-copy的。

测试结果

基本测试结果如下。

=== RUN   TestDispatcher_WeightRoundRobin
    dispatcher_test.go:174: Distribution results:
    dispatcher_test.go:178: Server 127.0.0.1:8001: selected 40 times, ratio=0.571, expected=0.571
    dispatcher_test.go:178: Server 127.0.0.1:8002: selected 20 times, ratio=0.286, expected=0.286
    dispatcher_test.go:178: Server 127.0.0.1:8003: selected 10 times, ratio=0.143, expected=0.143
--- PASS: TestDispatcher_WeightRoundRobin (0.01s)
PASS
ok      github.com/dobyte/due/v2/internal/dispatcher    0.996s

基准测试结果如下。

goos: darwin
goarch: amd64
pkg: github.com/dobyte/due/v2/internal/dispatcher
cpu: Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
BenchmarkDispatcher_WeightRoundRobin/Concurrency1_Instances3-12         	 4335841	       240.7 ns/op	       0 B/op	       0 allocs/op
BenchmarkDispatcher_WeightRoundRobin/Concurrency10_Instances3-12        	 4871023	       236.7 ns/op	       0 B/op	       0 allocs/op
BenchmarkDispatcher_WeightRoundRobin/Concurrency100_Instances3-12       	 5136795	       229.3 ns/op	       0 B/op	       0 allocs/op
BenchmarkDispatcher_WeightRoundRobin/Concurrency1_Instances10-12        	 5352273	       227.8 ns/op	       0 B/op	       0 allocs/op
BenchmarkDispatcher_WeightRoundRobin/Concurrency10_Instances10-12       	 5308056	       238.7 ns/op	       0 B/op	       0 allocs/op
BenchmarkDispatcher_WeightRoundRobin/Concurrency100_Instances10-12      	 5323646	       233.7 ns/op	       0 B/op	       0 allocs/op

其他

  • 权重是针对 Server 的服务注册,这里的 Server 包括 Gate、Node 和 Mesh,所以在 ServiceInstance 中新增了 Weight 字段
  • 目前的负载均衡总是走默认的负载方式,并没有提供 WithOptions 的选项,所以本次修改仅进行了基本测试和基准测试

@dobyte dobyte merged commit cb881c5 into dobyte:main Dec 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants