Skip to content

Commit 9de6be2

Browse files
RomanZavodskikhRoman Zavodskikh
and
Roman Zavodskikh
authored
Do not use endpointregistry in the hotpath (zalando#2673)
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]>
1 parent 707ea88 commit 9de6be2

File tree

2 files changed

+10
-17
lines changed

2 files changed

+10
-17
lines changed

loadbalancer/algorithm.go

+10-14
Original file line numberDiff line numberDiff line change
@@ -75,17 +75,15 @@ func shiftWeighted(rnd *rand.Rand, ctx *routing.LBContext, now time.Time) routin
7575
rt := ctx.Route
7676
ep := ctx.LBEndpoints
7777
for _, epi := range ep {
78-
detected := ctx.Registry.GetMetrics(epi.Host).DetectedTime()
79-
wi := fadeIn(now, rt.LBFadeInDuration, rt.LBFadeInExponent, detected)
78+
wi := fadeIn(now, rt.LBFadeInDuration, rt.LBFadeInExponent, epi.Detected)
8079
sum += wi
8180
}
8281

8382
choice := ep[len(ep)-1]
8483
r := rnd.Float64() * sum
8584
var upto float64
8685
for i, epi := range ep {
87-
detected := ctx.Registry.GetMetrics(epi.Host).DetectedTime()
88-
upto += fadeIn(now, rt.LBFadeInDuration, rt.LBFadeInExponent, detected)
86+
upto += fadeIn(now, rt.LBFadeInDuration, rt.LBFadeInExponent, epi.Detected)
8987
if upto > r {
9088
choice = ep[i]
9189
break
@@ -114,21 +112,19 @@ func shiftToRemaining(rnd *rand.Rand, ctx *routing.LBContext, wi []int, now time
114112
func withFadeIn(rnd *rand.Rand, ctx *routing.LBContext, choice int, algo routing.LBAlgorithm) routing.LBEndpoint {
115113
ep := ctx.LBEndpoints
116114
now := time.Now()
117-
detected := ctx.Registry.GetMetrics(ctx.LBEndpoints[choice].Host).DetectedTime()
118115
f := fadeIn(
119116
now,
120117
ctx.Route.LBFadeInDuration,
121118
ctx.Route.LBFadeInExponent,
122-
detected,
119+
ctx.LBEndpoints[choice].Detected,
123120
)
124121

125122
if rnd.Float64() < f {
126123
return ep[choice]
127124
}
128125
notFadingIndexes := make([]int, 0, len(ep))
129126
for i := 0; i < len(ep); i++ {
130-
detected := ctx.Registry.GetMetrics(ep[i].Host).DetectedTime()
131-
if _, fadingIn := fadeInState(now, ctx.Route.LBFadeInDuration, detected); !fadingIn {
127+
if _, fadingIn := fadeInState(now, ctx.Route.LBFadeInDuration, ep[i].Detected); !fadingIn {
132128
notFadingIndexes = append(notFadingIndexes, i)
133129
}
134130
}
@@ -267,7 +263,7 @@ func computeLoadAverage(ctx *routing.LBContext) float64 {
267263
sum := 1.0 // add 1 to include the request that just arrived
268264
endpoints := ctx.LBEndpoints
269265
for _, v := range endpoints {
270-
sum += float64(ctx.Registry.GetMetrics(v.Host).InflightRequests())
266+
sum += float64(v.Metrics.GetInflightRequests())
271267
}
272268
return sum / float64(len(endpoints))
273269
}
@@ -284,10 +280,10 @@ func (ch *consistentHash) boundedLoadSearch(key string, balanceFactor float64, c
284280
if skipEndpoint(endpointIndex) {
285281
continue
286282
}
287-
load := ctx.Registry.GetMetrics(ctx.LBEndpoints[endpointIndex].Host).InflightRequests()
283+
load := ctx.LBEndpoints[endpointIndex].Metrics.GetInflightRequests()
288284
// We know there must be an endpoint whose load <= average load.
289285
// Since targetLoad >= average load (balancerFactor >= 1), there must also be an endpoint with load <= targetLoad.
290-
if load <= int64(targetLoad) {
286+
if load <= int(targetLoad) {
291287
break
292288
}
293289
ringIndex = (ringIndex + 1) % ch.Len()
@@ -369,17 +365,17 @@ func (p *powerOfRandomNChoices) Apply(ctx *routing.LBContext) routing.LBEndpoint
369365
for i := 1; i < p.numberOfChoices; i++ {
370366
ce := ctx.LBEndpoints[p.rnd.Intn(ne)]
371367

372-
if p.getScore(ctx, ce) > p.getScore(ctx, best) {
368+
if p.getScore(ce) > p.getScore(best) {
373369
best = ce
374370
}
375371
}
376372
return best
377373
}
378374

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

385381
type (

proxy/proxy.go

-3
Original file line numberDiff line numberDiff line change
@@ -849,9 +849,6 @@ func (p *Proxy) makeBackendRequest(ctx *context, requestContext stdlibcontext.Co
849849
if endpoint != nil {
850850
endpoint.Metrics.IncInflightRequest()
851851
defer endpoint.Metrics.DecInflightRequest()
852-
853-
p.registry.IncInflightRequest(endpoint.Host)
854-
defer p.registry.DecInflightRequest(endpoint.Host)
855852
}
856853

857854
if p.experimentalUpgrade && isUpgradeRequest(req) {

0 commit comments

Comments
 (0)