Skip to content

Commit a33c109

Browse files
committed
fix: Delete HB metric unconditionally for non-HB endpoints
1 parent 0d0de85 commit a33c109

2 files changed

Lines changed: 37 additions & 4 deletions

File tree

src/code.cloudfoundry.org/gorouter/registry/registry.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -484,10 +484,9 @@ func (r *RouteRegistry) reportEndpointsPerPool(uri route.Uri, endpoint *route.En
484484
r.reporter.CaptureEndpointsPerPool(pool.NumEndpoints(), string(uri), config.LOAD_BALANCE_HB)
485485
} else {
486486
// Delete stale entries for routes that have switched from HB to another load balancing algorithm.
487-
pool := r.byURI.Find(uri.RouteKey())
488-
if pool != nil && pool.LoadBalancingAlgorithm != config.LOAD_BALANCE_HB {
489-
r.reporter.DeleteEndpointsPerPool(string(uri), config.LOAD_BALANCE_HB)
490-
}
487+
// This is called unconditionally because the pool may still report its algorithm as HB
488+
// even after the HB setting was removed (setPoolLoadBalancingAlgorithm ignores empty values).
489+
r.reporter.DeleteEndpointsPerPool(string(uri), config.LOAD_BALANCE_HB)
491490
}
492491
}
493492

src/code.cloudfoundry.org/gorouter/registry/registry_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -698,6 +698,40 @@ var _ = Describe("RouteRegistry", func() {
698698
Expect(found).To(BeTrue(), "expected DeleteEndpointsPerPool to be called for the switched route")
699699
})
700700

701+
It("deletes the HB metric when the HB algorithm is removed and endpoint registers with empty algorithm", func() {
702+
hbEndpoint := route.NewEndpoint(&route.EndpointOpts{
703+
Host: "192.168.1.1",
704+
Port: 8080,
705+
PrivateInstanceId: "id-1",
706+
LoadBalancingAlgorithm: config.LOAD_BALANCE_HB,
707+
})
708+
709+
r.Register("removed-hb.example.com", hbEndpoint)
710+
Expect(reporter.CaptureEndpointsPerPoolCallCount()).To(BeNumerically(">=", 1))
711+
712+
// Endpoint re-registers with empty LB algorithm (HB setting removed from route)
713+
noAlgoEndpoint := route.NewEndpoint(&route.EndpointOpts{
714+
Host: "192.168.1.1",
715+
Port: 8080,
716+
PrivateInstanceId: "id-1",
717+
LoadBalancingAlgorithm: "",
718+
})
719+
720+
deleteBefore := reporter.DeleteEndpointsPerPoolCallCount()
721+
r.Register("removed-hb.example.com", noAlgoEndpoint)
722+
723+
Expect(reporter.DeleteEndpointsPerPoolCallCount()).To(BeNumerically(">", deleteBefore))
724+
found := false
725+
for i := deleteBefore; i < reporter.DeleteEndpointsPerPoolCallCount(); i++ {
726+
uri, algo := reporter.DeleteEndpointsPerPoolArgsForCall(i)
727+
if uri == "removed-hb.example.com" && algo == config.LOAD_BALANCE_HB {
728+
found = true
729+
break
730+
}
731+
}
732+
Expect(found).To(BeTrue(), "expected DeleteEndpointsPerPool to be called when HB algorithm is removed")
733+
})
734+
701735
It("does not change the metric count when an existing HB endpoint is updated", func() {
702736
hbEndpoint := route.NewEndpoint(&route.EndpointOpts{
703737
Host: "192.168.1.1",

0 commit comments

Comments
 (0)