-
Notifications
You must be signed in to change notification settings - Fork 351
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Roman Zavodskikh <roman.zavodskikh@zalando.de>
- Loading branch information
Roman Zavodskikh
committed
Feb 9, 2024
1 parent
be4ef50
commit c9abb70
Showing
8 changed files
with
430 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package proxy | ||
|
||
import ( | ||
"math/rand" | ||
|
||
"github.com/zalando/skipper/routing" | ||
) | ||
|
||
type healthyEndpoints struct { | ||
rnd *rand.Rand | ||
endpointRegistry *routing.EndpointRegistry | ||
} | ||
|
||
func (h *healthyEndpoints) filterHealthyEndpoints(endpoints []routing.LBEndpoint, rt *routing.Route) []routing.LBEndpoint { | ||
if h == nil { | ||
return endpoints | ||
} | ||
|
||
p := h.rnd.Float64() | ||
|
||
filtered := make([]routing.LBEndpoint, 0, len(endpoints)) | ||
for _, e := range endpoints { | ||
if p < e.Metrics.HealthCheckDropProbability() { | ||
/* drop */ | ||
} else { | ||
filtered = append(filtered, e) | ||
} | ||
} | ||
|
||
if len(filtered) == 0 { | ||
return endpoints | ||
} | ||
return filtered | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
package proxy | ||
|
||
import ( | ||
"fmt" | ||
"math/rand" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/zalando/skipper/eskip" | ||
"github.com/zalando/skipper/loadbalancer" | ||
"github.com/zalando/skipper/routing" | ||
) | ||
|
||
func initialize(hosts []string) (*routing.EndpointRegistry, *routing.Route) { | ||
endpointRegistry := routing.NewEndpointRegistry(routing.RegistryOptions{ | ||
PassiveHealthCheckEnabled: true, | ||
TotalRequestsThreshold: 10, | ||
FailedRoundTripsRatioThreshold: 0.1, | ||
UnhealthyEndpointsRequestsDiscount: 1.0, | ||
}) | ||
eskipRoute := eskip.Route{BackendType: eskip.LBBackend, LBAlgorithm: loadbalancer.PowerOfRandomNChoices.String()} | ||
for i := range hosts { | ||
eskipRoute.LBEndpoints = append(eskipRoute.LBEndpoints, fmt.Sprintf("http://%s", hosts[i])) | ||
} | ||
|
||
route := &routing.Route{ | ||
Route: eskipRoute, | ||
LBEndpoints: []routing.LBEndpoint{}, | ||
} | ||
rt := loadbalancer.NewAlgorithmProvider().Do([]*routing.Route{route}) | ||
route = rt[0] | ||
endpointRegistry.Do([]*routing.Route{route}) | ||
|
||
return endpointRegistry, route | ||
} | ||
|
||
func TestFilterHealthyEndpointsReturnsAllEndpointsWhenAllAreHealthy(t *testing.T) { | ||
hosts := []string{"host1.test:80", "host2.test:80", "host3.test:80"} | ||
endpointRegistry, route := initialize(hosts) | ||
|
||
for i := range hosts { | ||
metrics := endpointRegistry.GetMetrics(hosts[i]) | ||
for j := 0; j < 50; j++ { | ||
metrics.IncTotalRequests() | ||
} | ||
} | ||
endpointRegistry.Do([]*routing.Route{route}) | ||
|
||
proxy := &Proxy{ | ||
registry: endpointRegistry, | ||
heathlyEndpoints: &healthyEndpoints{rnd: rand.New(loadbalancer.NewLockedSource()), endpointRegistry: endpointRegistry}, | ||
} | ||
|
||
const nRequests = 10_000 | ||
nHosts := map[int]int{} | ||
for i := 0; i < nRequests; i++ { | ||
endpoints := proxy.heathlyEndpoints.filterHealthyEndpoints(route.LBEndpoints, route) | ||
v := nHosts[len(endpoints)] | ||
nHosts[len(endpoints)] = v + 1 | ||
} | ||
|
||
assert.Equal(t, nRequests, nHosts[3]) | ||
assert.Equal(t, 0, nHosts[2]) | ||
assert.Equal(t, 0, nHosts[1]) | ||
} | ||
|
||
func TestFilterHealthyEndpointsReturnsAllEndpointsWhenAllAreUnhealthy(t *testing.T) { | ||
hosts := []string{"host1.test:80", "host2.test:80", "host3.test:80"} | ||
endpointRegistry, route := initialize(hosts) | ||
|
||
for i := range hosts { | ||
metrics := endpointRegistry.GetMetrics(hosts[i]) | ||
for j := 0; j < 50; j++ { | ||
metrics.IncTotalRequests() | ||
} | ||
for j := 0; j < 30; j++ { | ||
metrics.IncTotalFailedRoundTrips() | ||
} | ||
} | ||
|
||
proxy := &Proxy{ | ||
registry: endpointRegistry, | ||
heathlyEndpoints: &healthyEndpoints{rnd: rand.New(loadbalancer.NewLockedSource()), endpointRegistry: endpointRegistry}, | ||
} | ||
|
||
const nRequests = 10_000 | ||
nHosts := map[int]int{} | ||
for i := 0; i < nRequests; i++ { | ||
endpoints := proxy.heathlyEndpoints.filterHealthyEndpoints(route.LBEndpoints, route) | ||
v := nHosts[len(endpoints)] | ||
nHosts[len(endpoints)] = v + 1 | ||
} | ||
|
||
assert.Equal(t, nRequests, nHosts[3]) | ||
assert.Equal(t, 0, nHosts[2]) | ||
assert.Equal(t, 0, nHosts[1]) | ||
} | ||
|
||
func TestFilterHealthyEndpointsFiltersUnhealthyEndpoints(t *testing.T) { | ||
hosts := []string{"host1.test:80", "host2.test:80", "host3.test:80"} | ||
endpointRegistry, route := initialize(hosts) | ||
|
||
for i := range hosts { | ||
metrics := endpointRegistry.GetMetrics(hosts[i]) | ||
for j := 0; j < 50; j++ { | ||
metrics.IncTotalRequests() | ||
} | ||
} | ||
for i := 0; i < 30; i++ { | ||
metrics := endpointRegistry.GetMetrics(hosts[0]) | ||
metrics.IncTotalFailedRoundTrips() | ||
} | ||
endpointRegistry.Do([]*routing.Route{route}) | ||
|
||
proxy := &Proxy{ | ||
registry: endpointRegistry, | ||
heathlyEndpoints: &healthyEndpoints{rnd: rand.New(loadbalancer.NewLockedSource()), endpointRegistry: endpointRegistry}, | ||
} | ||
|
||
const nRequests = 10_000 | ||
nHosts := map[int]int{} | ||
for i := 0; i < nRequests; i++ { | ||
endpoints := proxy.heathlyEndpoints.filterHealthyEndpoints(route.LBEndpoints, route) | ||
v := nHosts[len(endpoints)] | ||
nHosts[len(endpoints)] = v + 1 | ||
|
||
if len(endpoints) == 2 { | ||
assert.Equal(t, hosts[1], endpoints[0].Host) | ||
assert.Equal(t, hosts[2], endpoints[1].Host) | ||
} | ||
} | ||
|
||
assert.InDelta(t, 6*nRequests/10, nHosts[3], nRequests/10) | ||
assert.InDelta(t, 4*nRequests/10, nHosts[2], nRequests/10) | ||
assert.Equal(t, 0, nHosts[1]) | ||
} |
Oops, something went wrong.