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 code comments #4219

Merged
merged 3 commits into from
Jul 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 16 additions & 7 deletions zrpc/internal/balancer/p2c/p2c.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ const (
Name = "p2c_ewma"

decayTime = int64(time.Second * 10) // default value from finagle
forcePick = int64(time.Second)
initSuccess = 1000
throttleSuccess = initSuccess / 2
penalty = int64(math.MaxInt32)
pickTimes = 3
logInterval = time.Minute
forcePick = int64(time.Second) // If a node is not selected for a period of time, it is forcibly selected.
initSuccess = 1000 // Initial success count
throttleSuccess = initSuccess / 2 // Success count to trigger throttling
penalty = int64(math.MaxInt32) // Penalty value for load calculation
pickTimes = 3 // Number of pick attempts
logInterval = time.Minute // Log interval for statistics
)

var emptyPickResult balancer.PickResult
Expand Down Expand Up @@ -121,6 +121,10 @@ func (p *p2cPicker) buildDoneFunc(c *subConn) func(info balancer.DoneInfo) {
if td < 0 {
td = 0
}

// As the td/decayTime value increases, indicating an increase in delay, the value of w (y axis) will decrease, inversely proportional.
// The function curve of y = x^(-x) is as follows.
// https://github.com/zeromicro/zero-doc/blob/main/doc/images/y_e_x.png?raw=true
w := math.Exp(float64(-td) / float64(decayTime))
lag := int64(now) - start
if lag < 0 {
Expand All @@ -130,6 +134,8 @@ func (p *p2cPicker) buildDoneFunc(c *subConn) func(info balancer.DoneInfo) {
if olag == 0 {
w = 0
}

// The smaller the value of w, the lower the impact of historical data.
atomic.StoreUint64(&c.lag, uint64(float64(olag)*w+float64(lag)*(1-w)))
success := initSuccess
if info.Err != nil && !codes.Acceptable(info.Err) {
Expand Down Expand Up @@ -182,7 +188,10 @@ func (p *p2cPicker) logStats() {
}

type subConn struct {
lag uint64
// The request latency measured by the weighted moving average algorithm.
lag uint64

// The value represents the number of requests that are either pending or just starting at the current node, and it is obtained through atomic addition.
inflight int64
success uint64
requests int64
Expand Down