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

Update loadbalance.md #128

Merged
merged 1 commit into from
Jul 12, 2024
Merged

Update loadbalance.md #128

merged 1 commit into from
Jul 12, 2024

Conversation

guonaihong
Copy link

左看右看没看到排序的代码。

@kevwan
Copy link
Contributor

kevwan commented Jul 7, 2024

里面按照latency加权重的。

@guonaihong
Copy link
Author

@kevwan 先讨论超过2个节点为的情况,
根据代码
https://github.com/zeromicro/go-zero/blob/master/zrpc/internal/balancer/p2c/p2c.go#L90 https://github.com/zeromicro/go-zero/blob/master/zrpc/internal/balancer/p2c/p2c.go#L91 来看,
两个候选节点都是随机产生的,a挑前面的节点,b挑a后面的节点。

再根据选择函数 https://github.com/zeromicro/go-zero/blob/master/zrpc/internal/balancer/p2c/p2c.go#L150 来看,
选择两个节点负载小的节点。求的是局部最优解,和全局排序区别挺大的。
算力区别:
局部求最优,依赖的计算小,只需要两个node参与,全局排序需要所以节点参与排序。
概念的区别:
全局排序是要所以节点参与,p2c只要两个节点参与就行。

所以才觉得用排序这个词不太合适。

@kevwan
Copy link
Contributor

kevwan commented Jul 7, 2024

每次挑两个,次数多了就是负载排序了。

@guonaihong
Copy link
Author

guonaihong commented Jul 7, 2024

他们概率是不一样的,全局排序选到最小节点的概率是1. p2c选到最小节点的概率是2/node的个数。
用10000次验证下结论,可以用下面的代码试下

package main

import (
	"fmt"
	"time"

	"math/rand"
)

func main() {
	rand.Seed(time.Now().UnixNano())
	conns := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}
	for j := 0; j < 10000; j++ {

		for i := 0; i < 3; i++ {
			a := rand.Intn(len(conns))
			b := rand.Intn(len(conns) - 1)
			if b >= a {
				b++
			}

			fmt.Printf("%d\n", min(a, b))
			break
		}
	}
}

把输出保存到my.log
所以用 sort my.log|sort |uniq -c|sort -nr 算下频率。
我跑了一次的输出是

 910 0
 904 1
 839 2
 822 3
 739 4
 717 5
 668 6
 602 8
 600 7
 541 9
 492 10
 413 11
 383 12
 320 13
 295 14
 234 15
 176 16
 163 17
 109 18
  73 19

@kevwan
Copy link
Contributor

kevwan commented Jul 7, 2024

负载均衡本来就不需要特别精确的,不能用数学上的精确模型来衡量,更多的是概率模型来看。

@kevwan kevwan merged commit 8e19a14 into zeromicro:feat/v3 Jul 12, 2024
1 check failed
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