Skip to content

Commit aa36952

Browse files
authored
balance, metrics: fix location-based balance sometimes doesn't work (#572)
1 parent 7247c02 commit aa36952

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed

pkg/balance/observer/backend_health.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func (bh *BackendHealth) setLocal(cfg *config.Config) {
4444
}
4545

4646
func (bh *BackendHealth) Equals(health BackendHealth) bool {
47-
return bh.Healthy == health.Healthy && bh.ServerVersion == health.ServerVersion
47+
return bh.Healthy == health.Healthy && bh.ServerVersion == health.ServerVersion && bh.Local == health.Local
4848
}
4949

5050
func (bh *BackendHealth) String() string {

pkg/balance/router/router_score_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,13 @@ func (tester *routerTester) updateBackendStatusByAddr(addr string, healthy bool)
114114
tester.notifyHealth()
115115
}
116116

117+
func (tester *routerTester) updateBackendLocalityByAddr(addr string, local bool) {
118+
health, ok := tester.backends[addr]
119+
require.True(tester.t, ok)
120+
health.Local = local
121+
tester.notifyHealth()
122+
}
123+
117124
func (tester *routerTester) getBackendByIndex(index int) *backendWrapper {
118125
addr := strconv.Itoa(index + 1)
119126
backend := tester.router.backends[addr]
@@ -758,6 +765,11 @@ func TestCloseRedirectingConns(t *testing.T) {
758765
func TestUpdateBackendHealth(t *testing.T) {
759766
tester := newRouterTester(t, nil)
760767
tester.addBackends(3)
768+
// Test locality of some backends are changed.
769+
tester.updateBackendLocalityByAddr(tester.getBackendByIndex(0).Addr(), false)
770+
tester.updateBackendLocalityByAddr(tester.getBackendByIndex(1).Addr(), true)
771+
require.Equal(t, false, tester.router.backends[tester.getBackendByIndex(0).Addr()].Local())
772+
require.Equal(t, true, tester.router.backends[tester.getBackendByIndex(1).Addr()].Local())
761773
// Test some backends are not in the list anymore.
762774
tester.removeBackends(1)
763775
tester.checkBackendNum(2)

pkg/metrics/metrics.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ func registerProxyMetrics() {
112112
prometheus.MustRegister(InboundPacketsCounter)
113113
prometheus.MustRegister(OutboundBytesCounter)
114114
prometheus.MustRegister(OutboundPacketsCounter)
115+
prometheus.MustRegister(CrossLocationBytesCounter)
115116
}
116117

117118
// ReadCounter reads the value from the counter. It is only used for testing.

0 commit comments

Comments
 (0)