Skip to content

Commit

Permalink
Do not use endpointregistry in the hotpath (zalando#2673)
Browse files Browse the repository at this point in the history
The performance of endpointregistry under circumstances of high concurenncy
is the very discussable topic, it does not seem to work perfectly at the moment.

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

choice := ep[len(ep)-1]
r := rnd.Float64() * sum
var upto float64
for i, epi := range ep {
detected := ctx.Registry.GetMetrics(epi.Host).DetectedTime()
upto += fadeIn(now, rt.LBFadeInDuration, rt.LBFadeInExponent, detected)
upto += fadeIn(now, rt.LBFadeInDuration, rt.LBFadeInExponent, epi.Detected)
if upto > r {
choice = ep[i]
break
Expand Down Expand Up @@ -114,21 +112,19 @@ 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,
detected,
ctx.LBEndpoints[choice].Detected,
)

if rnd.Float64() < f {
return ep[choice]
}
notFadingIndexes := make([]int, 0, len(ep))
for i := 0; i < len(ep); i++ {
detected := ctx.Registry.GetMetrics(ep[i].Host).DetectedTime()
if _, fadingIn := fadeInState(now, ctx.Route.LBFadeInDuration, detected); !fadingIn {
if _, fadingIn := fadeInState(now, ctx.Route.LBFadeInDuration, ep[i].Detected); !fadingIn {
notFadingIndexes = append(notFadingIndexes, i)
}
}
Expand Down Expand Up @@ -267,7 +263,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(ctx.Registry.GetMetrics(v.Host).InflightRequests())
sum += float64(v.Metrics.GetInflightRequests())
}
return sum / float64(len(endpoints))
}
Expand All @@ -284,10 +280,10 @@ func (ch *consistentHash) boundedLoadSearch(key string, balanceFactor float64, c
if skipEndpoint(endpointIndex) {
continue
}
load := ctx.Registry.GetMetrics(ctx.LBEndpoints[endpointIndex].Host).InflightRequests()
load := ctx.LBEndpoints[endpointIndex].Metrics.GetInflightRequests()
// 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 <= int64(targetLoad) {
if load <= int(targetLoad) {
break
}
ringIndex = (ringIndex + 1) % ch.Len()
Expand Down Expand Up @@ -369,17 +365,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(ctx, ce) > p.getScore(ctx, best) {
if p.getScore(ce) > p.getScore(best) {
best = ce
}
}
return best
}

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

type (
Expand Down
3 changes: 0 additions & 3 deletions proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -849,9 +849,6 @@ 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 9de6be2

Please sign in to comment.