Skip to content

Commit

Permalink
Get inflight requests number from endpointregistry instead of endpoin…
Browse files Browse the repository at this point in the history
…t metrics (#2645)

Signed-off-by: Roman Zavodskikh <[email protected]>
Co-authored-by: Roman Zavodskikh <[email protected]>
  • Loading branch information
RomanZavodskikh and Roman Zavodskikh authored Oct 5, 2023
1 parent d5b5047 commit ca0973c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
12 changes: 6 additions & 6 deletions loadbalancer/algorithm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand All @@ -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()
Expand Down Expand Up @@ -369,17 +369,17 @@ 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
}
}
return best
}

// 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 (
Expand Down
6 changes: 6 additions & 0 deletions loadbalancer/algorithm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit ca0973c

Please sign in to comment.