Skip to content

Commit 0d7661a

Browse files
authored
metricsreader, elect: read metrics from zone leader (#611)
1 parent d18577f commit 0d7661a

File tree

19 files changed

+868
-221
lines changed

19 files changed

+868
-221
lines changed

lib/config/label.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2024 PingCAP, Inc.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package config
5+
6+
const (
7+
// LocationLabelName indicates the label name that decides the location of TiProxy and backends.
8+
// We use `zone` because the follower read in TiDB also uses `zone` to decide location.
9+
LocationLabelName = "zone"
10+
)
11+
12+
func (cfg *Config) GetLocation() string {
13+
if len(cfg.Labels) == 0 {
14+
return ""
15+
}
16+
return cfg.Labels[LocationLabelName]
17+
}

lib/config/proxy.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313

1414
"github.com/BurntSushi/toml"
1515
"github.com/pingcap/tiproxy/lib/util/errors"
16-
"github.com/pingcap/tiproxy/lib/util/sys"
1716
)
1817

1918
var (
@@ -224,8 +223,17 @@ func (cfg *Config) GetIPPort() (ip, port, statusPort string, err error) {
224223
// reporting a non unicast IP makes no sense, try to find one
225224
// loopback/linklocal-unicast are not global unicast IP, but are valid local unicast IP
226225
if pip := net.ParseIP(ip); ip == "" || pip.Equal(net.IPv4bcast) || pip.IsUnspecified() || pip.IsMulticast() {
227-
if v := sys.GetGlobalUnicastIP(); v != "" {
228-
ip = v
226+
if addrs, err := net.InterfaceAddrs(); err == nil {
227+
for _, address := range addrs {
228+
if ipnet, ok := address.(*net.IPNet); ok && ipnet.IP.IsGlobalUnicast() {
229+
ipStr := ipnet.IP.String()
230+
// filter virtual IP
231+
if !strings.HasPrefix(cfg.HA.VirtualIP, ipStr) {
232+
ip = ipStr
233+
break
234+
}
235+
}
236+
}
229237
}
230238
}
231239
}

pkg/balance/factor/mock_test.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414
"github.com/pingcap/tiproxy/pkg/balance/observer"
1515
"github.com/pingcap/tiproxy/pkg/balance/policy"
1616
"github.com/prometheus/common/model"
17-
clientv3 "go.etcd.io/etcd/client/v3"
1817
)
1918

2019
var _ policy.BackendCtx = (*mockBackend)(nil)
@@ -103,7 +102,7 @@ func newMockMetricsReader() *mockMetricsReader {
103102
}
104103
}
105104

106-
func (mmr *mockMetricsReader) Start(ctx context.Context, etcdCli *clientv3.Client) error {
105+
func (mmr *mockMetricsReader) Start(ctx context.Context) error {
107106
return nil
108107
}
109108

@@ -117,6 +116,10 @@ func (mmr *mockMetricsReader) GetQueryResult(key string) metricsreader.QueryResu
117116
return mmr.qrs[key]
118117
}
119118

119+
func (mmr *mockMetricsReader) GetBackendMetrics() []byte {
120+
return nil
121+
}
122+
120123
func (mmr *mockMetricsReader) Close() {
121124
}
122125

0 commit comments

Comments
 (0)