Skip to content

Commit

Permalink
*: remove go:linkname (#622)
Browse files Browse the repository at this point in the history
  • Loading branch information
djshow832 authored Aug 5, 2024
1 parent dd66b34 commit c8d0d82
Show file tree
Hide file tree
Showing 26 changed files with 132 additions and 189 deletions.
5 changes: 2 additions & 3 deletions pkg/balance/factor/factor_cpu.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (

"github.com/pingcap/tiproxy/lib/config"
"github.com/pingcap/tiproxy/pkg/balance/metricsreader"
"github.com/pingcap/tiproxy/pkg/util/monotime"
dto "github.com/prometheus/client_model/go"
"github.com/prometheus/common/model"
)
Expand Down Expand Up @@ -84,7 +83,7 @@ type FactorCPU struct {
// The snapshot of backend statistics when the matrix was updated.
snapshot map[string]cpuBackendSnapshot
// The updated time of the metric that we've read last time.
lastMetricTime monotime.Time
lastMetricTime time.Time
// The estimated average CPU usage used by one connection.
usagePerConn float64
mr metricsreader.MetricsReader
Expand Down Expand Up @@ -120,7 +119,7 @@ func (fc *FactorCPU) UpdateScore(backends []scoredBackend) {
fc.updateSnapshot(qr, backends)
fc.updateCpuPerConn()
}
if monotime.Since(fc.lastMetricTime) > cpuMetricExpDuration {
if time.Since(fc.lastMetricTime) > cpuMetricExpDuration {
// The metrics have not been updated for a long time (maybe Prometheus is unavailable).
return
}
Expand Down
15 changes: 7 additions & 8 deletions pkg/balance/factor/factor_cpu_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"time"

"github.com/pingcap/tiproxy/pkg/balance/metricsreader"
"github.com/pingcap/tiproxy/pkg/util/monotime"
"github.com/prometheus/common/expfmt"
"github.com/prometheus/common/model"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -85,7 +84,7 @@ func TestCPUBalanceOnce(t *testing.T) {
mmr := &mockMetricsReader{
qrs: map[string]metricsreader.QueryResult{
"cpu": {
UpdateTime: monotime.Now(),
UpdateTime: time.Now(),
Value: model.Matrix(values),
},
},
Expand Down Expand Up @@ -198,7 +197,7 @@ func TestCPUBalanceContinuously(t *testing.T) {
}
curTime = curTime.Add(time.Millisecond)
mmr.qrs["cpu"] = metricsreader.QueryResult{
UpdateTime: monotime.Now(),
UpdateTime: time.Now(),
Value: model.Matrix(values),
}
// Rebalance until it stops. The final scores should be newConnScores.
Expand Down Expand Up @@ -228,18 +227,18 @@ func TestCPUBalanceContinuously(t *testing.T) {
func TestNoCPUMetric(t *testing.T) {
tests := []struct {
cpus [][]float64
updateTime monotime.Time
updateTime time.Time
}{
{
cpus: nil,
},
{
cpus: [][]float64{{1.0}, {0.0}},
updateTime: monotime.Now().Add(-cpuMetricExpDuration * 2),
updateTime: time.Now().Add(-cpuMetricExpDuration * 2),
},
{
cpus: [][]float64{{math.NaN()}, {math.NaN()}},
updateTime: monotime.Now(),
updateTime: time.Now(),
},
}
mmr := newMockMetricsReader()
Expand All @@ -248,7 +247,7 @@ func TestNoCPUMetric(t *testing.T) {
for i, test := range tests {
values := make([]*model.SampleStream, 0, len(test.cpus))
for j := 0; j < len(test.cpus); j++ {
ss := createSampleStream(test.cpus[j], j, model.Time(test.updateTime/monotime.Time(time.Millisecond)))
ss := createSampleStream(test.cpus[j], j, model.TimeFromUnixNano(test.updateTime.UnixNano()))
values = append(values, ss)
}
mmr.qrs["cpu"] = metricsreader.QueryResult{
Expand Down Expand Up @@ -291,7 +290,7 @@ func TestCPUResultNotUpdated(t *testing.T) {
array := []float64{test.cpu}
values := []*model.SampleStream{createSampleStream(array, 0, test.updateTime), createSampleStream(array, 1, test.updateTime)}
mmr.qrs["cpu"] = metricsreader.QueryResult{
UpdateTime: monotime.Now(),
UpdateTime: time.Now(),
Value: model.Matrix(values),
}
updateScore(fc, backends)
Expand Down
19 changes: 9 additions & 10 deletions pkg/balance/factor/factor_health.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (

"github.com/pingcap/tiproxy/lib/config"
"github.com/pingcap/tiproxy/pkg/balance/metricsreader"
"github.com/pingcap/tiproxy/pkg/util/monotime"
dto "github.com/prometheus/client_model/go"
"github.com/prometheus/common/model"
)
Expand Down Expand Up @@ -146,7 +145,7 @@ var _ Factor = (*FactorHealth)(nil)

// The snapshot of backend statistics when the metric was updated.
type healthBackendSnapshot struct {
updatedTime monotime.Time
updatedTime time.Time
valueRange valueRange
// Record the balance count when the backend becomes unhealthy so that it won't be smaller in the next rounds.
balanceCount float64
Expand Down Expand Up @@ -203,7 +202,7 @@ func (fh *FactorHealth) UpdateScore(backends []scoredBackend) {
if len(backends) <= 1 {
return
}
needUpdateSnapshot, latestTime := false, monotime.Time(0)
needUpdateSnapshot, latestTime := false, time.Time{}
for i := 0; i < len(fh.indicators); i++ {
qr := fh.mr.GetQueryResult(fh.indicators[i].key)
if qr.Empty() {
Expand All @@ -213,11 +212,11 @@ func (fh *FactorHealth) UpdateScore(backends []scoredBackend) {
fh.indicators[i].queryResult = qr
needUpdateSnapshot = true
}
if qr.UpdateTime > latestTime {
if qr.UpdateTime.After(latestTime) {
latestTime = qr.UpdateTime
}
}
if monotime.Since(latestTime) > errMetricExpDuration {
if time.Since(latestTime) > errMetricExpDuration {
// The metrics have not been updated for a long time (maybe Prometheus is unavailable).
return
}
Expand All @@ -234,14 +233,14 @@ func (fh *FactorHealth) updateSnapshot(backends []scoredBackend) {
snapshots := make(map[string]healthBackendSnapshot, len(fh.snapshot))
for _, backend := range backends {
// Get the current value range.
updatedTime, valueRange := monotime.Time(0), valueRangeNormal
updatedTime, valueRange := time.Time{}, valueRangeNormal
for i := 0; i < len(fh.indicators); i++ {
ts := fh.indicators[i].queryResult.UpdateTime
if monotime.Since(ts) > errMetricExpDuration {
if time.Since(ts) > errMetricExpDuration {
// The metrics have not been updated for a long time (maybe Prometheus is unavailable).
continue
}
if ts > updatedTime {
if ts.After(updatedTime) {
updatedTime = ts
}
sample := fh.indicators[i].queryResult.GetSample4Backend(backend)
Expand All @@ -253,8 +252,8 @@ func (fh *FactorHealth) updateSnapshot(backends []scoredBackend) {
// If the metric is unavailable, try to reuse the latest one.
addr := backend.Addr()
snapshot, existSnapshot := fh.snapshot[addr]
if updatedTime == monotime.Time(0) {
if existSnapshot && monotime.Since(snapshot.updatedTime) < errMetricExpDuration {
if updatedTime.IsZero() {
if existSnapshot && time.Since(snapshot.updatedTime) < errMetricExpDuration {
snapshots[addr] = snapshot
}
continue
Expand Down
18 changes: 9 additions & 9 deletions pkg/balance/factor/factor_health_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import (
"sort"
"strings"
"testing"
"time"

"github.com/pingcap/tiproxy/pkg/balance/metricsreader"
"github.com/pingcap/tiproxy/pkg/util/monotime"
"github.com/prometheus/common/expfmt"
"github.com/prometheus/common/model"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -66,11 +66,11 @@ func TestHealthScore(t *testing.T) {
mmr := &mockMetricsReader{
qrs: map[string]metricsreader.QueryResult{
"health_pd": {
UpdateTime: monotime.Now(),
UpdateTime: time.Now(),
Value: model.Vector(values1),
},
"health_tikv": {
UpdateTime: monotime.Now(),
UpdateTime: time.Now(),
Value: model.Vector(values2),
},
},
Expand Down Expand Up @@ -142,11 +142,11 @@ func TestHealthBalance(t *testing.T) {
mmr := &mockMetricsReader{
qrs: map[string]metricsreader.QueryResult{
"health_pd": {
UpdateTime: monotime.Now(),
UpdateTime: time.Now(),
Value: model.Vector(values1),
},
"health_tikv": {
UpdateTime: monotime.Now(),
UpdateTime: time.Now(),
Value: model.Vector(values2),
},
},
Expand All @@ -170,18 +170,18 @@ func TestHealthBalance(t *testing.T) {
func TestNoHealthMetrics(t *testing.T) {
tests := []struct {
errCounts [][]float64
updateTime monotime.Time
updateTime time.Time
}{
{
errCounts: [][]float64{nil, nil},
},
{
errCounts: [][]float64{{1, 1}, {0, 0}},
updateTime: monotime.Now().Sub(errMetricExpDuration * 2),
updateTime: time.Now().Add(-errMetricExpDuration * 2),
},
{
errCounts: [][]float64{{math.NaN(), math.NaN()}, {math.NaN(), math.NaN()}},
updateTime: monotime.Now(),
updateTime: time.Now(),
},
}

Expand Down Expand Up @@ -268,7 +268,7 @@ func TestHealthBalanceCount(t *testing.T) {
createSample(0, 1),
}
mmr.qrs["health_pd"] = metricsreader.QueryResult{
UpdateTime: monotime.Now(),
UpdateTime: time.Now(),
Value: model.Vector(values),
}
mmr.qrs["health_tikv"] = mmr.qrs["health_pd"]
Expand Down
5 changes: 2 additions & 3 deletions pkg/balance/factor/factor_memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (

"github.com/pingcap/tiproxy/lib/config"
"github.com/pingcap/tiproxy/pkg/balance/metricsreader"
"github.com/pingcap/tiproxy/pkg/util/monotime"
dto "github.com/prometheus/client_model/go"
"github.com/prometheus/common/model"
)
Expand Down Expand Up @@ -98,7 +97,7 @@ type FactorMemory struct {
// The snapshot of backend statistics when the matrix was updated.
snapshot map[string]memBackendSnapshot
// The updated time of the metric that we've read last time.
lastMetricTime monotime.Time
lastMetricTime time.Time
mr metricsreader.MetricsReader
bitNum int
}
Expand Down Expand Up @@ -138,7 +137,7 @@ func (fm *FactorMemory) UpdateScore(backends []scoredBackend) {
fm.lastMetricTime = qr.UpdateTime
fm.updateSnapshot(qr, backends)
}
if monotime.Since(fm.lastMetricTime) > memMetricExpDuration {
if time.Since(fm.lastMetricTime) > memMetricExpDuration {
// The metrics have not been updated for a long time (maybe Prometheus is unavailable).
return
}
Expand Down
15 changes: 7 additions & 8 deletions pkg/balance/factor/factor_memory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"time"

"github.com/pingcap/tiproxy/pkg/balance/metricsreader"
"github.com/pingcap/tiproxy/pkg/util/monotime"
"github.com/prometheus/common/expfmt"
"github.com/prometheus/common/model"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -97,7 +96,7 @@ func TestMemoryScore(t *testing.T) {
mmr := &mockMetricsReader{
qrs: map[string]metricsreader.QueryResult{
"memory": {
UpdateTime: monotime.Now(),
UpdateTime: time.Now(),
Value: model.Matrix(values),
},
},
Expand Down Expand Up @@ -209,7 +208,7 @@ func TestMemoryBalance(t *testing.T) {
mmr := &mockMetricsReader{
qrs: map[string]metricsreader.QueryResult{
"memory": {
UpdateTime: monotime.Now(),
UpdateTime: time.Now(),
Value: model.Matrix(values),
},
},
Expand All @@ -233,18 +232,18 @@ func TestMemoryBalance(t *testing.T) {
func TestNoMemMetrics(t *testing.T) {
tests := []struct {
mem [][]float64
updateTime monotime.Time
updateTime time.Time
}{
{
mem: nil,
},
{
mem: [][]float64{{1.0}, {0.0}},
updateTime: monotime.Now().Sub(memMetricExpDuration * 2),
updateTime: time.Now().Add(-memMetricExpDuration * 2),
},
{
mem: [][]float64{{math.NaN()}, {math.NaN()}},
updateTime: monotime.Now(),
updateTime: time.Now(),
},
}

Expand All @@ -258,7 +257,7 @@ func TestNoMemMetrics(t *testing.T) {
values := make([]*model.SampleStream, 0, len(test.mem))
for j := 0; j < len(test.mem); j++ {
ss := createSampleStream(test.mem[j], j, model.Now())
ss.Values[0].Timestamp = model.Time(test.updateTime / monotime.Time(time.Millisecond))
ss.Values[0].Timestamp = model.TimeFromUnixNano(test.updateTime.UnixNano())
values = append(values, ss)
}
mmr.qrs["memory"] = metricsreader.QueryResult{
Expand Down Expand Up @@ -328,7 +327,7 @@ func TestMemoryBalanceCount(t *testing.T) {
createSampleStream([]float64{0, 0}, 1, ts),
}
mmr.qrs["memory"] = metricsreader.QueryResult{
UpdateTime: monotime.Now(),
UpdateTime: time.Now(),
Value: model.Matrix(values),
}
}
Expand Down
3 changes: 1 addition & 2 deletions pkg/balance/metricsreader/backend_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"github.com/pingcap/tiproxy/pkg/manager/elect"
"github.com/pingcap/tiproxy/pkg/util/etcd"
"github.com/pingcap/tiproxy/pkg/util/http"
"github.com/pingcap/tiproxy/pkg/util/monotime"
dto "github.com/prometheus/client_model/go"
"github.com/prometheus/common/expfmt"
"github.com/prometheus/common/model"
Expand Down Expand Up @@ -369,7 +368,7 @@ func (br *BackendReader) metric2History(mfs map[string]*dto.MetricFamily, backen

// history2QueryResult generates new query results from the history.
func (br *BackendReader) history2QueryResult() {
now := monotime.Now()
now := time.Now()
br.Lock()
defer br.Unlock()

Expand Down
7 changes: 3 additions & 4 deletions pkg/balance/metricsreader/backend_reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"github.com/pingcap/tiproxy/lib/util/waitgroup"
"github.com/pingcap/tiproxy/pkg/manager/infosync"
httputil "github.com/pingcap/tiproxy/pkg/util/http"
"github.com/pingcap/tiproxy/pkg/util/monotime"
dto "github.com/prometheus/client_model/go"
"github.com/prometheus/common/model"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -574,7 +573,7 @@ func TestHistory2QueryResult(t *testing.T) {
require.Len(t, br.queryResults, len(test.queryResult), "case %d", i)
for ruleKey, expectedValue := range test.queryResult {
qr := br.GetQueryResult(ruleKey)
require.Greater(t, qr.UpdateTime, monotime.Time(0), "case %d", i)
require.False(t, qr.UpdateTime.IsZero(), "case %d", i)
require.NotEmpty(t, qr.Value, "case %d", i)
require.Equal(t, expectedValue.Type(), qr.Value.Type(), "case %d", i)
switch expectedValue.Type() {
Expand Down Expand Up @@ -926,7 +925,7 @@ func TestQueryBackendConcurrently(t *testing.T) {
case <-time.After(1 * time.Millisecond):
for i := 0; i < initialRules; i++ {
qr := br.GetQueryResult(strconv.Itoa(i))
require.Greater(t, qr.UpdateTime, monotime.Time(0))
require.False(t, qr.UpdateTime.IsZero())
require.NotNil(t, qr.Value)
// read the values to ensure that they are not updated after the query results are returned
if i%2 == 0 {
Expand Down Expand Up @@ -1274,7 +1273,7 @@ func TestElection(t *testing.T) {
t.Cleanup(br.Close)

// test not owner
ts := monotime.Now()
ts := time.Now()
err = br.ReadMetrics(context.Background())
require.NoError(t, err)
qr := br.GetQueryResult("rule_id1")
Expand Down
3 changes: 1 addition & 2 deletions pkg/balance/metricsreader/metrics_reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/pingcap/tiproxy/lib/config"
"github.com/pingcap/tiproxy/lib/util/logger"
httputil "github.com/pingcap/tiproxy/pkg/util/http"
"github.com/pingcap/tiproxy/pkg/util/monotime"
dto "github.com/prometheus/client_model/go"
"github.com/prometheus/common/model"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -67,7 +66,7 @@ func TestFallback(t *testing.T) {
t.Cleanup(mr.Close)

// read from prom
ts := monotime.Now()
ts := time.Now()
mr.readMetrics(context.Background())
qr := mr.GetQueryResult("rule_id1")
require.False(t, qr.Empty())
Expand Down
Loading

0 comments on commit c8d0d82

Please sign in to comment.