Skip to content

Commit

Permalink
Merge branch 'feature/bucket_grafana' of github.com:bufferflies/pd in…
Browse files Browse the repository at this point in the history
…to feature/bucket_grafana
  • Loading branch information
bufferflies committed Jun 1, 2022
2 parents 5ffbe2f + 17b4f84 commit 9b908c4
Show file tree
Hide file tree
Showing 23 changed files with 579 additions and 403 deletions.
1 change: 1 addition & 0 deletions .github/workflows/check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ concurrency:
jobs:
statics:
runs-on: ubuntu-latest
timeout-minutes: 8
steps:
- uses: actions/setup-go@v2
with:
Expand Down
18 changes: 10 additions & 8 deletions pkg/apiutil/apiutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
)

func TestJsonRespondErrorOk(t *testing.T) {
re := require.New(t)
rd := render.New(render.Options{
IndentJSON: true,
})
Expand All @@ -33,34 +34,35 @@ func TestJsonRespondErrorOk(t *testing.T) {
var input map[string]string
output := map[string]string{"zone": "cn", "host": "local"}
err := ReadJSONRespondError(rd, response, body, &input)
require.NoError(t, err)
require.Equal(t, output["zone"], input["zone"])
require.Equal(t, output["host"], input["host"])
re.NoError(err)
re.Equal(output["zone"], input["zone"])
re.Equal(output["host"], input["host"])
result := response.Result()
defer result.Body.Close()
require.Equal(t, 200, result.StatusCode)
re.Equal(200, result.StatusCode)
}

func TestJsonRespondErrorBadInput(t *testing.T) {
re := require.New(t)
rd := render.New(render.Options{
IndentJSON: true,
})
response := httptest.NewRecorder()
body := io.NopCloser(bytes.NewBufferString("{\"zone\":\"cn\", \"host\":\"local\"}"))
var input []string
err := ReadJSONRespondError(rd, response, body, &input)
require.EqualError(t, err, "json: cannot unmarshal object into Go value of type []string")
re.EqualError(err, "json: cannot unmarshal object into Go value of type []string")
result := response.Result()
defer result.Body.Close()
require.Equal(t, 400, result.StatusCode)
re.Equal(400, result.StatusCode)

{
body := io.NopCloser(bytes.NewBufferString("{\"zone\":\"cn\","))
var input []string
err := ReadJSONRespondError(rd, response, body, &input)
require.EqualError(t, err, "unexpected end of JSON input")
re.EqualError(err, "unexpected end of JSON input")
result := response.Result()
defer result.Body.Close()
require.Equal(t, 400, result.StatusCode)
re.Equal(400, result.StatusCode)
}
}
5 changes: 3 additions & 2 deletions pkg/assertutil/assertutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@ import (
)

func TestNilFail(t *testing.T) {
re := require.New(t)
var failErr error
checker := NewChecker(func() {
failErr = errors.New("called assert func not exist")
})
require.Nil(t, checker.IsNil)
re.Nil(checker.IsNil)
checker.AssertNil(nil)
require.NotNil(t, failErr)
re.NotNil(failErr)
}
28 changes: 15 additions & 13 deletions pkg/audit/audit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,16 @@ import (
)

func TestLabelMatcher(t *testing.T) {
re := require.New(t)
matcher := &LabelMatcher{"testSuccess"}
labels1 := &BackendLabels{Labels: []string{"testFail", "testSuccess"}}
require.True(t, matcher.Match(labels1))
re.True(matcher.Match(labels1))
labels2 := &BackendLabels{Labels: []string{"testFail"}}
require.False(t, matcher.Match(labels2))
re.False(matcher.Match(labels2))
}

func TestPrometheusHistogramBackend(t *testing.T) {
re := require.New(t)
serviceAuditHistogramTest := prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Namespace: "pd",
Expand All @@ -60,43 +62,43 @@ func TestPrometheusHistogramBackend(t *testing.T) {
info.ServiceLabel = "test"
info.Component = "user1"
req = req.WithContext(requestutil.WithRequestInfo(req.Context(), info))
require.False(t, backend.ProcessHTTPRequest(req))
re.False(backend.ProcessHTTPRequest(req))

endTime := time.Now().Unix() + 20
req = req.WithContext(requestutil.WithEndTime(req.Context(), endTime))

require.True(t, backend.ProcessHTTPRequest(req))
require.True(t, backend.ProcessHTTPRequest(req))
re.True(backend.ProcessHTTPRequest(req))
re.True(backend.ProcessHTTPRequest(req))

info.Component = "user2"
req = req.WithContext(requestutil.WithRequestInfo(req.Context(), info))
require.True(t, backend.ProcessHTTPRequest(req))
re.True(backend.ProcessHTTPRequest(req))

// For test, sleep time needs longer than the push interval
time.Sleep(1 * time.Second)
req, _ = http.NewRequest("GET", ts.URL, nil)
resp, err := http.DefaultClient.Do(req)
require.NoError(t, err)
re.NoError(err)
defer resp.Body.Close()
content, _ := io.ReadAll(resp.Body)
output := string(content)
require.Contains(t, output, "pd_service_audit_handling_seconds_test_count{component=\"user1\",method=\"HTTP\",service=\"test\"} 2")
require.Contains(t, output, "pd_service_audit_handling_seconds_test_count{component=\"user2\",method=\"HTTP\",service=\"test\"} 1")
re.Contains(output, "pd_service_audit_handling_seconds_test_count{component=\"user1\",method=\"HTTP\",service=\"test\"} 2")
re.Contains(output, "pd_service_audit_handling_seconds_test_count{component=\"user2\",method=\"HTTP\",service=\"test\"} 1")
}

func TestLocalLogBackendUsingFile(t *testing.T) {
re := require.New(t)
backend := NewLocalLogBackend(true)
fname := initLog()
defer os.Remove(fname)
req, _ := http.NewRequest("GET", "http://127.0.0.1:2379/test?test=test", strings.NewReader("testBody"))
require.False(t, backend.ProcessHTTPRequest(req))
re.False(backend.ProcessHTTPRequest(req))
info := requestutil.GetRequestInfo(req)
req = req.WithContext(requestutil.WithRequestInfo(req.Context(), info))
require.True(t, backend.ProcessHTTPRequest(req))
re.True(backend.ProcessHTTPRequest(req))
b, _ := os.ReadFile(fname)
output := strings.SplitN(string(b), "]", 4)
require.Equal(
t,
re.Equal(
fmt.Sprintf(" [\"Audit Log\"] [service-info=\"{ServiceLabel:, Method:HTTP/1.1/GET:/test, Component:anonymous, IP:, "+
"StartTime:%s, URLParam:{\\\"test\\\":[\\\"test\\\"]}, BodyParam:testBody}\"]\n",
time.Unix(info.StartTimeStamp, 0).String()),
Expand Down
33 changes: 19 additions & 14 deletions pkg/autoscaling/calculation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
)

func TestGetScaledTiKVGroups(t *testing.T) {
re := require.New(t)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// case1 indicates the tikv cluster with not any group existed
Expand Down Expand Up @@ -193,10 +194,10 @@ func TestGetScaledTiKVGroups(t *testing.T) {
t.Log(testCase.name)
plans, err := getScaledTiKVGroups(testCase.informer, testCase.healthyInstances)
if testCase.expectedPlan == nil {
require.Len(t, plans, 0)
require.Equal(t, testCase.noError, err == nil)
re.Len(plans, 0)
re.Equal(testCase.noError, err == nil)
} else {
require.True(t, reflect.DeepEqual(testCase.expectedPlan, plans))
re.True(reflect.DeepEqual(testCase.expectedPlan, plans))
}
}
}
Expand All @@ -213,6 +214,7 @@ func (q *mockQuerier) Query(options *QueryOptions) (QueryResult, error) {
}

func TestGetTotalCPUUseTime(t *testing.T) {
re := require.New(t)
querier := &mockQuerier{}
instances := []instance{
{
Expand All @@ -230,10 +232,11 @@ func TestGetTotalCPUUseTime(t *testing.T) {
}
totalCPUUseTime, _ := getTotalCPUUseTime(querier, TiDB, instances, time.Now(), 0)
expected := mockResultValue * float64(len(instances))
require.True(t, math.Abs(expected-totalCPUUseTime) < 1e-6)
re.True(math.Abs(expected-totalCPUUseTime) < 1e-6)
}

func TestGetTotalCPUQuota(t *testing.T) {
re := require.New(t)
querier := &mockQuerier{}
instances := []instance{
{
Expand All @@ -251,10 +254,11 @@ func TestGetTotalCPUQuota(t *testing.T) {
}
totalCPUQuota, _ := getTotalCPUQuota(querier, TiDB, instances, time.Now())
expected := uint64(mockResultValue * float64(len(instances)*milliCores))
require.Equal(t, expected, totalCPUQuota)
re.Equal(expected, totalCPUQuota)
}

func TestScaleOutGroupLabel(t *testing.T) {
re := require.New(t)
var jsonStr = []byte(`
{
"rules":[
Expand Down Expand Up @@ -288,14 +292,15 @@ func TestScaleOutGroupLabel(t *testing.T) {
}`)
strategy := &Strategy{}
err := json.Unmarshal(jsonStr, strategy)
require.NoError(t, err)
re.NoError(err)
plan := findBestGroupToScaleOut(strategy, nil, TiKV)
require.Equal(t, "hotRegion", plan.Labels["specialUse"])
re.Equal("hotRegion", plan.Labels["specialUse"])
plan = findBestGroupToScaleOut(strategy, nil, TiDB)
require.Equal(t, "", plan.Labels["specialUse"])
re.Equal("", plan.Labels["specialUse"])
}

func TestStrategyChangeCount(t *testing.T) {
re := require.New(t)
var count uint64 = 2
strategy := &Strategy{
Rules: []*Rule{
Expand Down Expand Up @@ -343,21 +348,21 @@ func TestStrategyChangeCount(t *testing.T) {

// exist two scaled TiKVs and plan does not change due to the limit of resource count
groups, err := getScaledTiKVGroups(cluster, instances)
require.NoError(t, err)
re.NoError(err)
plans := calculateScaleOutPlan(strategy, TiKV, scaleOutQuota, groups)
require.Equal(t, uint64(2), plans[0].Count)
re.Equal(uint64(2), plans[0].Count)

// change the resource count to 3 and plan increates one more tikv
groups, err = getScaledTiKVGroups(cluster, instances)
require.NoError(t, err)
re.NoError(err)
*strategy.Resources[0].Count = 3
plans = calculateScaleOutPlan(strategy, TiKV, scaleOutQuota, groups)
require.Equal(t, uint64(3), plans[0].Count)
re.Equal(uint64(3), plans[0].Count)

// change the resource count to 1 and plan decreases to 1 tikv due to the limit of resource count
groups, err = getScaledTiKVGroups(cluster, instances)
require.NoError(t, err)
re.NoError(err)
*strategy.Resources[0].Count = 1
plans = calculateScaleOutPlan(strategy, TiKV, scaleOutQuota, groups)
require.Equal(t, uint64(1), plans[0].Count)
re.Equal(uint64(1), plans[0].Count)
}
32 changes: 19 additions & 13 deletions pkg/autoscaling/prometheus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ func (c *normalClient) Do(_ context.Context, req *http.Request) (response *http.
}

func TestRetrieveCPUMetrics(t *testing.T) {
re := require.New(t)
client := &normalClient{
mockData: make(map[string]*response),
}
Expand All @@ -191,15 +192,15 @@ func TestRetrieveCPUMetrics(t *testing.T) {
for _, metric := range metrics {
options := NewQueryOptions(component, metric, addresses[:len(addresses)-1], time.Now(), mockDuration)
result, err := querier.Query(options)
require.NoError(t, err)
re.NoError(err)
for i := 0; i < len(addresses)-1; i++ {
value, ok := result[addresses[i]]
require.True(t, ok)
require.True(t, math.Abs(value-mockResultValue) < 1e-6)
re.True(ok)
re.True(math.Abs(value-mockResultValue) < 1e-6)
}

_, ok := result[addresses[len(addresses)-1]]
require.False(t, ok)
re.False(ok)
}
}
}
Expand All @@ -224,12 +225,13 @@ func (c *emptyResponseClient) Do(_ context.Context, req *http.Request) (r *http.
}

func TestEmptyResponse(t *testing.T) {
re := require.New(t)
client := &emptyResponseClient{}
querier := NewPrometheusQuerier(client)
options := NewQueryOptions(TiDB, CPUUsage, podAddresses[TiDB], time.Now(), mockDuration)
result, err := querier.Query(options)
require.Nil(t, result)
require.Error(t, err)
re.Nil(result)
re.Error(err)
}

type errorHTTPStatusClient struct{}
Expand All @@ -250,12 +252,13 @@ func (c *errorHTTPStatusClient) Do(_ context.Context, req *http.Request) (r *htt
}

func TestErrorHTTPStatus(t *testing.T) {
re := require.New(t)
client := &errorHTTPStatusClient{}
querier := NewPrometheusQuerier(client)
options := NewQueryOptions(TiDB, CPUUsage, podAddresses[TiDB], time.Now(), mockDuration)
result, err := querier.Query(options)
require.Nil(t, result)
require.Error(t, err)
re.Nil(result)
re.Error(err)
}

type errorPrometheusStatusClient struct{}
Expand All @@ -274,15 +277,17 @@ func (c *errorPrometheusStatusClient) Do(_ context.Context, req *http.Request) (
}

func TestErrorPrometheusStatus(t *testing.T) {
re := require.New(t)
client := &errorPrometheusStatusClient{}
querier := NewPrometheusQuerier(client)
options := NewQueryOptions(TiDB, CPUUsage, podAddresses[TiDB], time.Now(), mockDuration)
result, err := querier.Query(options)
require.Nil(t, result)
require.Error(t, err)
re.Nil(result)
re.Error(err)
}

func TestGetInstanceNameFromAddress(t *testing.T) {
re := require.New(t)
testCases := []struct {
address string
expectedInstanceName string
Expand Down Expand Up @@ -311,14 +316,15 @@ func TestGetInstanceNameFromAddress(t *testing.T) {
for _, testCase := range testCases {
instanceName, err := getInstanceNameFromAddress(testCase.address)
if testCase.expectedInstanceName == "" {
require.Error(t, err)
re.Error(err)
} else {
require.Equal(t, testCase.expectedInstanceName, instanceName)
re.Equal(testCase.expectedInstanceName, instanceName)
}
}
}

func TestGetDurationExpression(t *testing.T) {
re := require.New(t)
testCases := []struct {
duration time.Duration
expectedExpression string
Expand All @@ -343,6 +349,6 @@ func TestGetDurationExpression(t *testing.T) {

for _, testCase := range testCases {
expression := getDurationExpression(testCase.duration)
require.Equal(t, testCase.expectedExpression, expression)
re.Equal(testCase.expectedExpression, expression)
}
}
Loading

0 comments on commit 9b908c4

Please sign in to comment.