Skip to content

Commit

Permalink
Revert "Do not use endpointregistry in the hotpath (#2673)"
Browse files Browse the repository at this point in the history
This reverts commit 9de6be2.

Signed-off-by: Roman Zavodskikh <[email protected]>
  • Loading branch information
Roman Zavodskikh committed Dec 6, 2023
1 parent ca954e5 commit 339cacb
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
24 changes: 14 additions & 10 deletions loadbalancer/algorithm.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,17 @@ func shiftWeighted(rnd *rand.Rand, ctx *routing.LBContext, now time.Time) routin
rt := ctx.Route
ep := ctx.LBEndpoints
for _, epi := range ep {
wi := fadeIn(now, rt.LBFadeInDuration, rt.LBFadeInExponent, epi.Detected)
detected := ctx.Registry.GetMetrics(epi.Host).DetectedTime()
wi := fadeIn(now, rt.LBFadeInDuration, rt.LBFadeInExponent, detected)
sum += wi
}

choice := ep[len(ep)-1]
r := rnd.Float64() * sum
var upto float64
for i, epi := range ep {
upto += fadeIn(now, rt.LBFadeInDuration, rt.LBFadeInExponent, epi.Detected)
detected := ctx.Registry.GetMetrics(epi.Host).DetectedTime()
upto += fadeIn(now, rt.LBFadeInDuration, rt.LBFadeInExponent, detected)
if upto > r {
choice = ep[i]
break
Expand Down Expand Up @@ -112,19 +114,21 @@ func shiftToRemaining(rnd *rand.Rand, ctx *routing.LBContext, wi []int, now time
func withFadeIn(rnd *rand.Rand, ctx *routing.LBContext, choice int, algo routing.LBAlgorithm) routing.LBEndpoint {
ep := ctx.LBEndpoints
now := time.Now()
detected := ctx.Registry.GetMetrics(ctx.LBEndpoints[choice].Host).DetectedTime()
f := fadeIn(
now,
ctx.Route.LBFadeInDuration,
ctx.Route.LBFadeInExponent,
ctx.LBEndpoints[choice].Detected,
detected,
)

if rnd.Float64() < f {
return ep[choice]
}
notFadingIndexes := make([]int, 0, len(ep))
for i := 0; i < len(ep); i++ {
if _, fadingIn := fadeInState(now, ctx.Route.LBFadeInDuration, ep[i].Detected); !fadingIn {
detected := ctx.Registry.GetMetrics(ep[i].Host).DetectedTime()
if _, fadingIn := fadeInState(now, ctx.Route.LBFadeInDuration, detected); !fadingIn {
notFadingIndexes = append(notFadingIndexes, i)
}
}
Expand Down Expand Up @@ -263,7 +267,7 @@ func computeLoadAverage(ctx *routing.LBContext) float64 {
sum := 1.0 // add 1 to include the request that just arrived
endpoints := ctx.LBEndpoints
for _, v := range endpoints {
sum += float64(v.Metrics.GetInflightRequests())
sum += float64(ctx.Registry.GetMetrics(v.Host).InflightRequests())
}
return sum / float64(len(endpoints))
}
Expand All @@ -280,10 +284,10 @@ func (ch *consistentHash) boundedLoadSearch(key string, balanceFactor float64, c
if skipEndpoint(endpointIndex) {
continue
}
load := ctx.LBEndpoints[endpointIndex].Metrics.GetInflightRequests()
load := ctx.Registry.GetMetrics(ctx.LBEndpoints[endpointIndex].Host).InflightRequests()
// We know there must be an endpoint whose load <= average load.
// Since targetLoad >= average load (balancerFactor >= 1), there must also be an endpoint with load <= targetLoad.
if load <= int(targetLoad) {
if float64(load) <= targetLoad {
break
}
ringIndex = (ringIndex + 1) % ch.Len()
Expand Down Expand Up @@ -365,17 +369,17 @@ func (p *powerOfRandomNChoices) Apply(ctx *routing.LBContext) routing.LBEndpoint
for i := 1; i < p.numberOfChoices; i++ {
ce := ctx.LBEndpoints[p.rnd.Intn(ne)]

if p.getScore(ce) > p.getScore(best) {
if p.getScore(ctx, ce) > p.getScore(ctx, best) {
best = ce
}
}
return best
}

// getScore returns negative value of inflightrequests count.
func (p *powerOfRandomNChoices) getScore(e routing.LBEndpoint) int64 {
func (p *powerOfRandomNChoices) getScore(ctx *routing.LBContext, e routing.LBEndpoint) int64 {
// endpoints with higher inflight request should have lower score
return -int64(e.Metrics.GetInflightRequests())
return -ctx.Registry.GetMetrics(e.Host).InflightRequests()
}

type (
Expand Down
3 changes: 3 additions & 0 deletions proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -848,6 +848,9 @@ func (p *Proxy) makeBackendRequest(ctx *context, requestContext stdlibcontext.Co
if endpoint != nil {
endpoint.Metrics.IncInflightRequest()
defer endpoint.Metrics.DecInflightRequest()

p.registry.IncInflightRequest(endpoint.Host)
defer p.registry.DecInflightRequest(endpoint.Host)
}

if p.experimentalUpgrade && isUpgradeRequest(req) {
Expand Down

0 comments on commit 339cacb

Please sign in to comment.