Skip to content

Commit c8d0d82

Browse files
authored
*: remove go:linkname (#622)
1 parent dd66b34 commit c8d0d82

26 files changed

+132
-189
lines changed

pkg/balance/factor/factor_cpu.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99

1010
"github.com/pingcap/tiproxy/lib/config"
1111
"github.com/pingcap/tiproxy/pkg/balance/metricsreader"
12-
"github.com/pingcap/tiproxy/pkg/util/monotime"
1312
dto "github.com/prometheus/client_model/go"
1413
"github.com/prometheus/common/model"
1514
)
@@ -84,7 +83,7 @@ type FactorCPU struct {
8483
// The snapshot of backend statistics when the matrix was updated.
8584
snapshot map[string]cpuBackendSnapshot
8685
// The updated time of the metric that we've read last time.
87-
lastMetricTime monotime.Time
86+
lastMetricTime time.Time
8887
// The estimated average CPU usage used by one connection.
8988
usagePerConn float64
9089
mr metricsreader.MetricsReader
@@ -120,7 +119,7 @@ func (fc *FactorCPU) UpdateScore(backends []scoredBackend) {
120119
fc.updateSnapshot(qr, backends)
121120
fc.updateCpuPerConn()
122121
}
123-
if monotime.Since(fc.lastMetricTime) > cpuMetricExpDuration {
122+
if time.Since(fc.lastMetricTime) > cpuMetricExpDuration {
124123
// The metrics have not been updated for a long time (maybe Prometheus is unavailable).
125124
return
126125
}

pkg/balance/factor/factor_cpu_test.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"time"
1212

1313
"github.com/pingcap/tiproxy/pkg/balance/metricsreader"
14-
"github.com/pingcap/tiproxy/pkg/util/monotime"
1514
"github.com/prometheus/common/expfmt"
1615
"github.com/prometheus/common/model"
1716
"github.com/stretchr/testify/require"
@@ -85,7 +84,7 @@ func TestCPUBalanceOnce(t *testing.T) {
8584
mmr := &mockMetricsReader{
8685
qrs: map[string]metricsreader.QueryResult{
8786
"cpu": {
88-
UpdateTime: monotime.Now(),
87+
UpdateTime: time.Now(),
8988
Value: model.Matrix(values),
9089
},
9190
},
@@ -198,7 +197,7 @@ func TestCPUBalanceContinuously(t *testing.T) {
198197
}
199198
curTime = curTime.Add(time.Millisecond)
200199
mmr.qrs["cpu"] = metricsreader.QueryResult{
201-
UpdateTime: monotime.Now(),
200+
UpdateTime: time.Now(),
202201
Value: model.Matrix(values),
203202
}
204203
// Rebalance until it stops. The final scores should be newConnScores.
@@ -228,18 +227,18 @@ func TestCPUBalanceContinuously(t *testing.T) {
228227
func TestNoCPUMetric(t *testing.T) {
229228
tests := []struct {
230229
cpus [][]float64
231-
updateTime monotime.Time
230+
updateTime time.Time
232231
}{
233232
{
234233
cpus: nil,
235234
},
236235
{
237236
cpus: [][]float64{{1.0}, {0.0}},
238-
updateTime: monotime.Now().Add(-cpuMetricExpDuration * 2),
237+
updateTime: time.Now().Add(-cpuMetricExpDuration * 2),
239238
},
240239
{
241240
cpus: [][]float64{{math.NaN()}, {math.NaN()}},
242-
updateTime: monotime.Now(),
241+
updateTime: time.Now(),
243242
},
244243
}
245244
mmr := newMockMetricsReader()
@@ -248,7 +247,7 @@ func TestNoCPUMetric(t *testing.T) {
248247
for i, test := range tests {
249248
values := make([]*model.SampleStream, 0, len(test.cpus))
250249
for j := 0; j < len(test.cpus); j++ {
251-
ss := createSampleStream(test.cpus[j], j, model.Time(test.updateTime/monotime.Time(time.Millisecond)))
250+
ss := createSampleStream(test.cpus[j], j, model.TimeFromUnixNano(test.updateTime.UnixNano()))
252251
values = append(values, ss)
253252
}
254253
mmr.qrs["cpu"] = metricsreader.QueryResult{
@@ -291,7 +290,7 @@ func TestCPUResultNotUpdated(t *testing.T) {
291290
array := []float64{test.cpu}
292291
values := []*model.SampleStream{createSampleStream(array, 0, test.updateTime), createSampleStream(array, 1, test.updateTime)}
293292
mmr.qrs["cpu"] = metricsreader.QueryResult{
294-
UpdateTime: monotime.Now(),
293+
UpdateTime: time.Now(),
295294
Value: model.Matrix(values),
296295
}
297296
updateScore(fc, backends)

pkg/balance/factor/factor_health.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99

1010
"github.com/pingcap/tiproxy/lib/config"
1111
"github.com/pingcap/tiproxy/pkg/balance/metricsreader"
12-
"github.com/pingcap/tiproxy/pkg/util/monotime"
1312
dto "github.com/prometheus/client_model/go"
1413
"github.com/prometheus/common/model"
1514
)
@@ -146,7 +145,7 @@ var _ Factor = (*FactorHealth)(nil)
146145

147146
// The snapshot of backend statistics when the metric was updated.
148147
type healthBackendSnapshot struct {
149-
updatedTime monotime.Time
148+
updatedTime time.Time
150149
valueRange valueRange
151150
// Record the balance count when the backend becomes unhealthy so that it won't be smaller in the next rounds.
152151
balanceCount float64
@@ -203,7 +202,7 @@ func (fh *FactorHealth) UpdateScore(backends []scoredBackend) {
203202
if len(backends) <= 1 {
204203
return
205204
}
206-
needUpdateSnapshot, latestTime := false, monotime.Time(0)
205+
needUpdateSnapshot, latestTime := false, time.Time{}
207206
for i := 0; i < len(fh.indicators); i++ {
208207
qr := fh.mr.GetQueryResult(fh.indicators[i].key)
209208
if qr.Empty() {
@@ -213,11 +212,11 @@ func (fh *FactorHealth) UpdateScore(backends []scoredBackend) {
213212
fh.indicators[i].queryResult = qr
214213
needUpdateSnapshot = true
215214
}
216-
if qr.UpdateTime > latestTime {
215+
if qr.UpdateTime.After(latestTime) {
217216
latestTime = qr.UpdateTime
218217
}
219218
}
220-
if monotime.Since(latestTime) > errMetricExpDuration {
219+
if time.Since(latestTime) > errMetricExpDuration {
221220
// The metrics have not been updated for a long time (maybe Prometheus is unavailable).
222221
return
223222
}
@@ -234,14 +233,14 @@ func (fh *FactorHealth) updateSnapshot(backends []scoredBackend) {
234233
snapshots := make(map[string]healthBackendSnapshot, len(fh.snapshot))
235234
for _, backend := range backends {
236235
// Get the current value range.
237-
updatedTime, valueRange := monotime.Time(0), valueRangeNormal
236+
updatedTime, valueRange := time.Time{}, valueRangeNormal
238237
for i := 0; i < len(fh.indicators); i++ {
239238
ts := fh.indicators[i].queryResult.UpdateTime
240-
if monotime.Since(ts) > errMetricExpDuration {
239+
if time.Since(ts) > errMetricExpDuration {
241240
// The metrics have not been updated for a long time (maybe Prometheus is unavailable).
242241
continue
243242
}
244-
if ts > updatedTime {
243+
if ts.After(updatedTime) {
245244
updatedTime = ts
246245
}
247246
sample := fh.indicators[i].queryResult.GetSample4Backend(backend)
@@ -253,8 +252,8 @@ func (fh *FactorHealth) updateSnapshot(backends []scoredBackend) {
253252
// If the metric is unavailable, try to reuse the latest one.
254253
addr := backend.Addr()
255254
snapshot, existSnapshot := fh.snapshot[addr]
256-
if updatedTime == monotime.Time(0) {
257-
if existSnapshot && monotime.Since(snapshot.updatedTime) < errMetricExpDuration {
255+
if updatedTime.IsZero() {
256+
if existSnapshot && time.Since(snapshot.updatedTime) < errMetricExpDuration {
258257
snapshots[addr] = snapshot
259258
}
260259
continue

pkg/balance/factor/factor_health_test.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ import (
88
"sort"
99
"strings"
1010
"testing"
11+
"time"
1112

1213
"github.com/pingcap/tiproxy/pkg/balance/metricsreader"
13-
"github.com/pingcap/tiproxy/pkg/util/monotime"
1414
"github.com/prometheus/common/expfmt"
1515
"github.com/prometheus/common/model"
1616
"github.com/stretchr/testify/require"
@@ -66,11 +66,11 @@ func TestHealthScore(t *testing.T) {
6666
mmr := &mockMetricsReader{
6767
qrs: map[string]metricsreader.QueryResult{
6868
"health_pd": {
69-
UpdateTime: monotime.Now(),
69+
UpdateTime: time.Now(),
7070
Value: model.Vector(values1),
7171
},
7272
"health_tikv": {
73-
UpdateTime: monotime.Now(),
73+
UpdateTime: time.Now(),
7474
Value: model.Vector(values2),
7575
},
7676
},
@@ -142,11 +142,11 @@ func TestHealthBalance(t *testing.T) {
142142
mmr := &mockMetricsReader{
143143
qrs: map[string]metricsreader.QueryResult{
144144
"health_pd": {
145-
UpdateTime: monotime.Now(),
145+
UpdateTime: time.Now(),
146146
Value: model.Vector(values1),
147147
},
148148
"health_tikv": {
149-
UpdateTime: monotime.Now(),
149+
UpdateTime: time.Now(),
150150
Value: model.Vector(values2),
151151
},
152152
},
@@ -170,18 +170,18 @@ func TestHealthBalance(t *testing.T) {
170170
func TestNoHealthMetrics(t *testing.T) {
171171
tests := []struct {
172172
errCounts [][]float64
173-
updateTime monotime.Time
173+
updateTime time.Time
174174
}{
175175
{
176176
errCounts: [][]float64{nil, nil},
177177
},
178178
{
179179
errCounts: [][]float64{{1, 1}, {0, 0}},
180-
updateTime: monotime.Now().Sub(errMetricExpDuration * 2),
180+
updateTime: time.Now().Add(-errMetricExpDuration * 2),
181181
},
182182
{
183183
errCounts: [][]float64{{math.NaN(), math.NaN()}, {math.NaN(), math.NaN()}},
184-
updateTime: monotime.Now(),
184+
updateTime: time.Now(),
185185
},
186186
}
187187

@@ -268,7 +268,7 @@ func TestHealthBalanceCount(t *testing.T) {
268268
createSample(0, 1),
269269
}
270270
mmr.qrs["health_pd"] = metricsreader.QueryResult{
271-
UpdateTime: monotime.Now(),
271+
UpdateTime: time.Now(),
272272
Value: model.Vector(values),
273273
}
274274
mmr.qrs["health_tikv"] = mmr.qrs["health_pd"]

pkg/balance/factor/factor_memory.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99

1010
"github.com/pingcap/tiproxy/lib/config"
1111
"github.com/pingcap/tiproxy/pkg/balance/metricsreader"
12-
"github.com/pingcap/tiproxy/pkg/util/monotime"
1312
dto "github.com/prometheus/client_model/go"
1413
"github.com/prometheus/common/model"
1514
)
@@ -98,7 +97,7 @@ type FactorMemory struct {
9897
// The snapshot of backend statistics when the matrix was updated.
9998
snapshot map[string]memBackendSnapshot
10099
// The updated time of the metric that we've read last time.
101-
lastMetricTime monotime.Time
100+
lastMetricTime time.Time
102101
mr metricsreader.MetricsReader
103102
bitNum int
104103
}
@@ -138,7 +137,7 @@ func (fm *FactorMemory) UpdateScore(backends []scoredBackend) {
138137
fm.lastMetricTime = qr.UpdateTime
139138
fm.updateSnapshot(qr, backends)
140139
}
141-
if monotime.Since(fm.lastMetricTime) > memMetricExpDuration {
140+
if time.Since(fm.lastMetricTime) > memMetricExpDuration {
142141
// The metrics have not been updated for a long time (maybe Prometheus is unavailable).
143142
return
144143
}

pkg/balance/factor/factor_memory_test.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"time"
1212

1313
"github.com/pingcap/tiproxy/pkg/balance/metricsreader"
14-
"github.com/pingcap/tiproxy/pkg/util/monotime"
1514
"github.com/prometheus/common/expfmt"
1615
"github.com/prometheus/common/model"
1716
"github.com/stretchr/testify/require"
@@ -97,7 +96,7 @@ func TestMemoryScore(t *testing.T) {
9796
mmr := &mockMetricsReader{
9897
qrs: map[string]metricsreader.QueryResult{
9998
"memory": {
100-
UpdateTime: monotime.Now(),
99+
UpdateTime: time.Now(),
101100
Value: model.Matrix(values),
102101
},
103102
},
@@ -209,7 +208,7 @@ func TestMemoryBalance(t *testing.T) {
209208
mmr := &mockMetricsReader{
210209
qrs: map[string]metricsreader.QueryResult{
211210
"memory": {
212-
UpdateTime: monotime.Now(),
211+
UpdateTime: time.Now(),
213212
Value: model.Matrix(values),
214213
},
215214
},
@@ -233,18 +232,18 @@ func TestMemoryBalance(t *testing.T) {
233232
func TestNoMemMetrics(t *testing.T) {
234233
tests := []struct {
235234
mem [][]float64
236-
updateTime monotime.Time
235+
updateTime time.Time
237236
}{
238237
{
239238
mem: nil,
240239
},
241240
{
242241
mem: [][]float64{{1.0}, {0.0}},
243-
updateTime: monotime.Now().Sub(memMetricExpDuration * 2),
242+
updateTime: time.Now().Add(-memMetricExpDuration * 2),
244243
},
245244
{
246245
mem: [][]float64{{math.NaN()}, {math.NaN()}},
247-
updateTime: monotime.Now(),
246+
updateTime: time.Now(),
248247
},
249248
}
250249

@@ -258,7 +257,7 @@ func TestNoMemMetrics(t *testing.T) {
258257
values := make([]*model.SampleStream, 0, len(test.mem))
259258
for j := 0; j < len(test.mem); j++ {
260259
ss := createSampleStream(test.mem[j], j, model.Now())
261-
ss.Values[0].Timestamp = model.Time(test.updateTime / monotime.Time(time.Millisecond))
260+
ss.Values[0].Timestamp = model.TimeFromUnixNano(test.updateTime.UnixNano())
262261
values = append(values, ss)
263262
}
264263
mmr.qrs["memory"] = metricsreader.QueryResult{
@@ -328,7 +327,7 @@ func TestMemoryBalanceCount(t *testing.T) {
328327
createSampleStream([]float64{0, 0}, 1, ts),
329328
}
330329
mmr.qrs["memory"] = metricsreader.QueryResult{
331-
UpdateTime: monotime.Now(),
330+
UpdateTime: time.Now(),
332331
Value: model.Matrix(values),
333332
}
334333
}

pkg/balance/metricsreader/backend_reader.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323
"github.com/pingcap/tiproxy/pkg/manager/elect"
2424
"github.com/pingcap/tiproxy/pkg/util/etcd"
2525
"github.com/pingcap/tiproxy/pkg/util/http"
26-
"github.com/pingcap/tiproxy/pkg/util/monotime"
2726
dto "github.com/prometheus/client_model/go"
2827
"github.com/prometheus/common/expfmt"
2928
"github.com/prometheus/common/model"
@@ -369,7 +368,7 @@ func (br *BackendReader) metric2History(mfs map[string]*dto.MetricFamily, backen
369368

370369
// history2QueryResult generates new query results from the history.
371370
func (br *BackendReader) history2QueryResult() {
372-
now := monotime.Now()
371+
now := time.Now()
373372
br.Lock()
374373
defer br.Unlock()
375374

pkg/balance/metricsreader/backend_reader_test.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import (
2525
"github.com/pingcap/tiproxy/lib/util/waitgroup"
2626
"github.com/pingcap/tiproxy/pkg/manager/infosync"
2727
httputil "github.com/pingcap/tiproxy/pkg/util/http"
28-
"github.com/pingcap/tiproxy/pkg/util/monotime"
2928
dto "github.com/prometheus/client_model/go"
3029
"github.com/prometheus/common/model"
3130
"github.com/stretchr/testify/require"
@@ -574,7 +573,7 @@ func TestHistory2QueryResult(t *testing.T) {
574573
require.Len(t, br.queryResults, len(test.queryResult), "case %d", i)
575574
for ruleKey, expectedValue := range test.queryResult {
576575
qr := br.GetQueryResult(ruleKey)
577-
require.Greater(t, qr.UpdateTime, monotime.Time(0), "case %d", i)
576+
require.False(t, qr.UpdateTime.IsZero(), "case %d", i)
578577
require.NotEmpty(t, qr.Value, "case %d", i)
579578
require.Equal(t, expectedValue.Type(), qr.Value.Type(), "case %d", i)
580579
switch expectedValue.Type() {
@@ -926,7 +925,7 @@ func TestQueryBackendConcurrently(t *testing.T) {
926925
case <-time.After(1 * time.Millisecond):
927926
for i := 0; i < initialRules; i++ {
928927
qr := br.GetQueryResult(strconv.Itoa(i))
929-
require.Greater(t, qr.UpdateTime, monotime.Time(0))
928+
require.False(t, qr.UpdateTime.IsZero())
930929
require.NotNil(t, qr.Value)
931930
// read the values to ensure that they are not updated after the query results are returned
932931
if i%2 == 0 {
@@ -1274,7 +1273,7 @@ func TestElection(t *testing.T) {
12741273
t.Cleanup(br.Close)
12751274

12761275
// test not owner
1277-
ts := monotime.Now()
1276+
ts := time.Now()
12781277
err = br.ReadMetrics(context.Background())
12791278
require.NoError(t, err)
12801279
qr := br.GetQueryResult("rule_id1")

pkg/balance/metricsreader/metrics_reader_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
"github.com/pingcap/tiproxy/lib/config"
1414
"github.com/pingcap/tiproxy/lib/util/logger"
1515
httputil "github.com/pingcap/tiproxy/pkg/util/http"
16-
"github.com/pingcap/tiproxy/pkg/util/monotime"
1716
dto "github.com/prometheus/client_model/go"
1817
"github.com/prometheus/common/model"
1918
"github.com/stretchr/testify/require"
@@ -67,7 +66,7 @@ func TestFallback(t *testing.T) {
6766
t.Cleanup(mr.Close)
6867

6968
// read from prom
70-
ts := monotime.Now()
69+
ts := time.Now()
7170
mr.readMetrics(context.Background())
7271
qr := mr.GetQueryResult("rule_id1")
7372
require.False(t, qr.Empty())

0 commit comments

Comments
 (0)