Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get inflight requests number from endpointregistry instead of endpoint metrics #2645

Merged
merged 1 commit into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💥
This loop contents for the lock inside the registry

}
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