From 8f1d3f677bab74eae3e61cb1d609511698f0a268 Mon Sep 17 00:00:00 2001 From: Roman Zavodskikh Date: Wed, 4 Oct 2023 11:45:32 +0200 Subject: [PATCH] Get inflight requests number from endpointregistry instead of endpoint metrics Signed-off-by: Roman Zavodskikh --- loadbalancer/algorithm.go | 12 ++++++------ loadbalancer/algorithm_test.go | 6 ++++++ 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/loadbalancer/algorithm.go b/loadbalancer/algorithm.go index 6972239e18..df599f2fbd 100644 --- a/loadbalancer/algorithm.go +++ b/loadbalancer/algorithm.go @@ -267,7 +267,7 @@ func computeLoadAverage(ctx *routing.LBContext) float64 { sum := 1.0 // add 1 to include the request that just arrived endpoints := ctx.Route.LBEndpoints for _, v := range endpoints { - sum += float64(v.Metrics.GetInflightRequests()) + sum += float64(ctx.Registry.GetMetrics(v.Host).InflightRequests()) } return sum / float64(len(endpoints)) } @@ -284,10 +284,10 @@ func (ch *consistentHash) boundedLoadSearch(key string, balanceFactor float64, c if skipEndpoint(endpointIndex) { continue } - load := ctx.Route.LBEndpoints[endpointIndex].Metrics.GetInflightRequests() + load := ctx.Registry.GetMetrics(ctx.Route.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 load <= int64(targetLoad) { break } ringIndex = (ringIndex + 1) % ch.Len() @@ -369,7 +369,7 @@ func (p *powerOfRandomNChoices) Apply(ctx *routing.LBContext) routing.LBEndpoint for i := 1; i < p.numberOfChoices; i++ { ce := ctx.Route.LBEndpoints[p.rnd.Intn(ne)] - if p.getScore(ce) > p.getScore(best) { + if p.getScore(ctx, ce) > p.getScore(ctx, best) { best = ce } } @@ -377,9 +377,9 @@ func (p *powerOfRandomNChoices) Apply(ctx *routing.LBContext) routing.LBEndpoint } // getScore returns negative value of inflightrequests count. -func (p *powerOfRandomNChoices) getScore(e routing.LBEndpoint) int { +func (p *powerOfRandomNChoices) getScore(ctx *routing.LBContext, e routing.LBEndpoint) int64 { // endpoints with higher inflight request should have lower score - return -e.Metrics.GetInflightRequests() + return -ctx.Registry.GetMetrics(e.Host).InflightRequests() } type ( diff --git a/loadbalancer/algorithm_test.go b/loadbalancer/algorithm_test.go index ca684ebc71..f5ec402bd2 100644 --- a/loadbalancer/algorithm_test.go +++ b/loadbalancer/algorithm_test.go @@ -6,6 +6,7 @@ import ( "net/http" "testing" + "github.com/stretchr/testify/assert" "github.com/zalando/skipper/eskip" "github.com/zalando/skipper/net" "github.com/zalando/skipper/routing" @@ -404,6 +405,11 @@ func TestConsistentHashBoundedLoadDistribution(t *testing.T) { ifr0 := route.LBEndpoints[0].Metrics.GetInflightRequests() ifr1 := route.LBEndpoints[1].Metrics.GetInflightRequests() ifr2 := route.LBEndpoints[2].Metrics.GetInflightRequests() + + assert.Equal(t, int64(ifr0), ctx.Registry.GetMetrics(route.LBEndpoints[0].Host).InflightRequests()) + assert.Equal(t, int64(ifr1), ctx.Registry.GetMetrics(route.LBEndpoints[1].Host).InflightRequests()) + assert.Equal(t, int64(ifr2), ctx.Registry.GetMetrics(route.LBEndpoints[2].Host).InflightRequests()) + avg := float64(ifr0+ifr1+ifr2) / 3.0 limit := int(avg*balanceFactor) + 1 if ifr0 > limit || ifr1 > limit || ifr2 > limit {