Skip to content

Commit

Permalink
Merge branch 'master' into multi-op
Browse files Browse the repository at this point in the history
  • Loading branch information
HunDunDM authored May 31, 2022
2 parents 3fe5b14 + 294a016 commit c96f2a0
Show file tree
Hide file tree
Showing 89 changed files with 5,214 additions and 1,784 deletions.
5 changes: 3 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ ifneq "$(PD_EDITION)" "Enterprise"
endif
endif

ifneq ($(SWAGGER), 0)
ifeq ($(SWAGGER), 1)
BUILD_TAGS += swagger_server
endif

Expand Down Expand Up @@ -60,7 +60,7 @@ build: pd-server pd-ctl pd-recover
tools: pd-tso-bench pd-heartbeat-bench regions-dump stores-dump

PD_SERVER_DEP :=
ifneq ($(SWAGGER), 0)
ifeq ($(SWAGGER), 1)
PD_SERVER_DEP += swagger-spec
endif
ifneq ($(DASHBOARD_DISTRIBUTION_DIR),)
Expand Down Expand Up @@ -158,6 +158,7 @@ static: install-tools

tidy:
@ go mod tidy
git diff go.mod go.sum | cat
git diff --quiet go.mod go.sum

@ for mod in $(SUBMODULES); do cd $$mod && $(MAKE) tidy && cd - > /dev/null; done
Expand Down
1 change: 1 addition & 0 deletions client/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,6 @@ static: install-tools

tidy:
@ go mod tidy
git diff go.mod go.sum | cat
git diff --quiet go.mod go.sum

2 changes: 1 addition & 1 deletion client/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ require (
github.com/pingcap/check v0.0.0-20211026125417-57bd13f7b5f0
github.com/pingcap/errors v0.11.5-0.20211224045212-9687c2b0f87c
github.com/pingcap/failpoint v0.0.0-20210918120811-547c13e3eb00
github.com/pingcap/kvproto v0.0.0-20220330070404-8c4cd3f93748
github.com/pingcap/kvproto v0.0.0-20220510035547-0e2f26c0a46a
github.com/pingcap/log v0.0.0-20211215031037-e024ba4eb0ee
github.com/prometheus/client_golang v1.11.0
go.uber.org/goleak v1.1.11
Expand Down
4 changes: 2 additions & 2 deletions client/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ github.com/pingcap/errors v0.11.5-0.20211224045212-9687c2b0f87c h1:xpW9bvK+HuuTm
github.com/pingcap/errors v0.11.5-0.20211224045212-9687c2b0f87c/go.mod h1:X2r9ueLEUZgtx2cIogM0v4Zj5uvvzhuuiu7Pn8HzMPg=
github.com/pingcap/failpoint v0.0.0-20210918120811-547c13e3eb00 h1:C3N3itkduZXDZFh4N3vQ5HEtld3S+Y+StULhWVvumU0=
github.com/pingcap/failpoint v0.0.0-20210918120811-547c13e3eb00/go.mod h1:4qGtCB0QK0wBzKtFEGDhxXnSnbQApw1gc9siScUl8ew=
github.com/pingcap/kvproto v0.0.0-20220330070404-8c4cd3f93748 h1:i4MBe1zGq9/r3BH6rTRunizi4T59fpNk8hvBCrB5UAY=
github.com/pingcap/kvproto v0.0.0-20220330070404-8c4cd3f93748/go.mod h1:OYtxs0786qojVTmkVeufx93xe+jUgm56GUYRIKnmaGI=
github.com/pingcap/kvproto v0.0.0-20220510035547-0e2f26c0a46a h1:TxdHGOFeNa1q1mVv6TgReayf26iI4F8PQUm6RnZ/V/E=
github.com/pingcap/kvproto v0.0.0-20220510035547-0e2f26c0a46a/go.mod h1:OYtxs0786qojVTmkVeufx93xe+jUgm56GUYRIKnmaGI=
github.com/pingcap/log v0.0.0-20191012051959-b742a5d432e9/go.mod h1:4rbK1p9ILyIfb6hU7OG2CiWSqMXnp3JMbiaVJ6mvoY8=
github.com/pingcap/log v0.0.0-20211215031037-e024ba4eb0ee h1:VO2t6IBpfvW34TdtD/G10VvnGqjLic1jzOuHjUb5VqM=
github.com/pingcap/log v0.0.0-20211215031037-e024ba4eb0ee/go.mod h1:DWQW5jICDR7UJh4HtxXSM20Churx4CQL0fwL/SoOSA4=
Expand Down
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ go 1.16
require (
github.com/AlekSi/gocov-xml v1.0.0
github.com/BurntSushi/toml v0.3.1
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751
github.com/aws/aws-sdk-go v1.35.3
github.com/axw/gocov v1.0.0
github.com/cakturk/go-netstat v0.0.0-20200220111822-e5b49efee7a5
Expand All @@ -30,15 +29,16 @@ require (
github.com/pingcap/errcode v0.3.0
github.com/pingcap/errors v0.11.5-0.20211224045212-9687c2b0f87c
github.com/pingcap/failpoint v0.0.0-20200702092429-9f69995143ce
github.com/pingcap/kvproto v0.0.0-20220429093005-2839fa5a1ed6
github.com/pingcap/kvproto v0.0.0-20220510035547-0e2f26c0a46a
github.com/pingcap/log v0.0.0-20210906054005-afc726e70354
github.com/pingcap/sysutil v0.0.0-20211208032423-041a72e5860d
github.com/pingcap/tidb-dashboard v0.0.0-20220517152638-ae6b14e7065b
github.com/pingcap/tidb-dashboard v0.0.0-20220518164040-4d621864a9a0
github.com/prometheus/client_golang v1.1.0
github.com/prometheus/common v0.6.0
github.com/sasha-s/go-deadlock v0.2.0
github.com/spf13/cobra v1.0.0
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.7.0
github.com/swaggo/http-swagger v0.0.0-20200308142732-58ac5e232fba
github.com/swaggo/swag v1.6.6-0.20200529100950-7c765ddd0476
github.com/syndtr/goleveldb v1.0.1-0.20190318030020-c3a204f8e965
Expand Down
9 changes: 4 additions & 5 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ github.com/VividCortex/mysqlerr v1.0.0/go.mod h1:xERx8E4tBhLvpjzdUyQiSfUxeMcATEQ
github.com/Xeoncross/go-aesctr-with-hmac v0.0.0-20200623134604-12b17a7ff502 h1:L8IbaI/W6h5Cwgh0n4zGeZpVK78r/jBf9ASurHo9+/o=
github.com/Xeoncross/go-aesctr-with-hmac v0.0.0-20200623134604-12b17a7ff502/go.mod h1:pmnBM9bxWSiHvC/gSWunUIyDvGn33EkP2CUjxFKtTTM=
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 h1:JYp7IbQjafoB+tBA3gMyHYHrpOtNuDiK/uB5uXxq5wM=
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
github.com/alvaroloes/enumer v1.1.2/go.mod h1:FxrjvuXoDAx9isTJrv4c+T410zFi0DtXIT0m65DJ+Wo=
Expand Down Expand Up @@ -400,17 +399,17 @@ github.com/pingcap/failpoint v0.0.0-20200702092429-9f69995143ce h1:Y1kCxlCtlPTMt
github.com/pingcap/failpoint v0.0.0-20200702092429-9f69995143ce/go.mod h1:w4PEZ5y16LeofeeGwdgZB4ddv9bLyDuIX+ljstgKZyk=
github.com/pingcap/kvproto v0.0.0-20191211054548-3c6b38ea5107/go.mod h1:WWLmULLO7l8IOcQG+t+ItJ3fEcrL5FxF0Wu+HrMy26w=
github.com/pingcap/kvproto v0.0.0-20200411081810-b85805c9476c/go.mod h1:IOdRDPLyda8GX2hE/jO7gqaCV/PNFh8BZQCQZXfIOqI=
github.com/pingcap/kvproto v0.0.0-20220429093005-2839fa5a1ed6 h1:gT4uxwuZzTniXdzp4mPoZjhNkDNEuZBt7HESOuLRyMI=
github.com/pingcap/kvproto v0.0.0-20220429093005-2839fa5a1ed6/go.mod h1:OYtxs0786qojVTmkVeufx93xe+jUgm56GUYRIKnmaGI=
github.com/pingcap/kvproto v0.0.0-20220510035547-0e2f26c0a46a h1:TxdHGOFeNa1q1mVv6TgReayf26iI4F8PQUm6RnZ/V/E=
github.com/pingcap/kvproto v0.0.0-20220510035547-0e2f26c0a46a/go.mod h1:OYtxs0786qojVTmkVeufx93xe+jUgm56GUYRIKnmaGI=
github.com/pingcap/log v0.0.0-20191012051959-b742a5d432e9/go.mod h1:4rbK1p9ILyIfb6hU7OG2CiWSqMXnp3JMbiaVJ6mvoY8=
github.com/pingcap/log v0.0.0-20200511115504-543df19646ad/go.mod h1:4rbK1p9ILyIfb6hU7OG2CiWSqMXnp3JMbiaVJ6mvoY8=
github.com/pingcap/log v0.0.0-20210625125904-98ed8e2eb1c7/go.mod h1:8AanEdAHATuRurdGxZXBz0At+9avep+ub7U1AGYLIMM=
github.com/pingcap/log v0.0.0-20210906054005-afc726e70354 h1:SvWCbCPh1YeHd9yQLksvJYAgft6wLTY1aNG81tpyscQ=
github.com/pingcap/log v0.0.0-20210906054005-afc726e70354/go.mod h1:DWQW5jICDR7UJh4HtxXSM20Churx4CQL0fwL/SoOSA4=
github.com/pingcap/sysutil v0.0.0-20211208032423-041a72e5860d h1:k3/APKZjXOyJrFy8VyYwRlZhMelpD3qBLJNsw3bPl/g=
github.com/pingcap/sysutil v0.0.0-20211208032423-041a72e5860d/go.mod h1:7j18ezaWTao2LHOyMlsc2Dg1vW+mDY9dEbPzVyOlaeM=
github.com/pingcap/tidb-dashboard v0.0.0-20220517152638-ae6b14e7065b h1:1CfD9C7GGkIUvtMvq4SjKBL+blNarBZRmViw/xHLINw=
github.com/pingcap/tidb-dashboard v0.0.0-20220517152638-ae6b14e7065b/go.mod h1:Hc2LXf5Vs+KwyegHd6osyZ2+LfaVSfWEwuR86SNg7tk=
github.com/pingcap/tidb-dashboard v0.0.0-20220518164040-4d621864a9a0 h1:SNfoqt/qZ+tSnFcOIn6rvhmH06UGJ137Of+uK9q1oOk=
github.com/pingcap/tidb-dashboard v0.0.0-20220518164040-4d621864a9a0/go.mod h1:Hc2LXf5Vs+KwyegHd6osyZ2+LfaVSfWEwuR86SNg7tk=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
Expand Down
32 changes: 11 additions & 21 deletions pkg/apiutil/apiutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,11 @@ import (
"net/http/httptest"
"testing"

. "github.com/pingcap/check"
"github.com/stretchr/testify/require"
"github.com/unrolled/render"
)

func Test(t *testing.T) {
TestingT(t)
}

var _ = Suite(&testUtilSuite{})

type testUtilSuite struct{}

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

func (s *testUtilSuite) TestJsonRespondErrorBadInput(c *C) {
func TestJsonRespondErrorBadInput(t *testing.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)
c.Assert(err, NotNil)
c.Assert(err.Error(), Equals, "json: cannot unmarshal object into Go value of type []string")
require.EqualError(t, err, "json: cannot unmarshal object into Go value of type []string")
result := response.Result()
defer result.Body.Close()
c.Assert(result.StatusCode, Equals, 400)
require.Equal(t, 400, result.StatusCode)

{
body := io.NopCloser(bytes.NewBufferString("{\"zone\":\"cn\","))
var input []string
err := ReadJSONRespondError(rd, response, body, &input)
c.Assert(err, NotNil)
c.Assert(err.Error(), Equals, "unexpected end of JSON input")
require.EqualError(t, err, "unexpected end of JSON input")
result := response.Result()
defer result.Body.Close()
c.Assert(result.StatusCode, Equals, 400)
require.Equal(t, 400, result.StatusCode)
}
}
16 changes: 4 additions & 12 deletions pkg/assertutil/assertutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,15 @@ import (
"errors"
"testing"

"github.com/pingcap/check"
"github.com/stretchr/testify/require"
)

func Test(t *testing.T) {
check.TestingT(t)
}

var _ = check.Suite(&testAssertUtilSuite{})

type testAssertUtilSuite struct{}

func (s *testAssertUtilSuite) TestNilFail(c *check.C) {
func TestNilFail(t *testing.T) {
var failErr error
checker := NewChecker(func() {
failErr = errors.New("called assert func not exist")
})
c.Assert(checker.IsNil, check.IsNil)
require.Nil(t, checker.IsNil)
checker.AssertNil(nil)
c.Assert(failErr, check.NotNil)
require.NotNil(t, failErr)
}
50 changes: 22 additions & 28 deletions pkg/audit/audit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,32 +24,22 @@ import (
"testing"
"time"

. "github.com/pingcap/check"
"github.com/pingcap/log"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/stretchr/testify/require"
"github.com/tikv/pd/pkg/requestutil"
)

func Test(t *testing.T) {
TestingT(t)
}

var _ = Suite(&testAuditSuite{})

type testAuditSuite struct {
}

func (s *testAuditSuite) TestLabelMatcher(c *C) {
func TestLabelMatcher(t *testing.T) {
matcher := &LabelMatcher{"testSuccess"}
labels1 := &BackendLabels{Labels: []string{"testFail", "testSuccess"}}
c.Assert(matcher.Match(labels1), Equals, true)

require.True(t, matcher.Match(labels1))
labels2 := &BackendLabels{Labels: []string{"testFail"}}
c.Assert(matcher.Match(labels2), Equals, false)
require.False(t, matcher.Match(labels2))
}

func (s *testAuditSuite) TestPrometheusHistogramBackend(c *C) {
func TestPrometheusHistogramBackend(t *testing.T) {
serviceAuditHistogramTest := prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Namespace: "pd",
Expand All @@ -70,44 +60,48 @@ func (s *testAuditSuite) TestPrometheusHistogramBackend(c *C) {
info.ServiceLabel = "test"
info.Component = "user1"
req = req.WithContext(requestutil.WithRequestInfo(req.Context(), info))
c.Assert(backend.ProcessHTTPRequest(req), Equals, false)
require.False(t, backend.ProcessHTTPRequest(req))

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

c.Assert(backend.ProcessHTTPRequest(req), Equals, true)
c.Assert(backend.ProcessHTTPRequest(req), Equals, true)
require.True(t, backend.ProcessHTTPRequest(req))
require.True(t, backend.ProcessHTTPRequest(req))

info.Component = "user2"
req = req.WithContext(requestutil.WithRequestInfo(req.Context(), info))
c.Assert(backend.ProcessHTTPRequest(req), Equals, true)
require.True(t, 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)
c.Assert(err, IsNil)
require.NoError(t, err)
defer resp.Body.Close()
content, _ := io.ReadAll(resp.Body)
output := string(content)
c.Assert(strings.Contains(output, "pd_service_audit_handling_seconds_test_count{component=\"user1\",method=\"HTTP\",service=\"test\"} 2"), Equals, true)
c.Assert(strings.Contains(output, "pd_service_audit_handling_seconds_test_count{component=\"user2\",method=\"HTTP\",service=\"test\"} 1"), Equals, true)
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")
}

func (s *testAuditSuite) TestLocalLogBackendUsingFile(c *C) {
func TestLocalLogBackendUsingFile(t *testing.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"))
c.Assert(backend.ProcessHTTPRequest(req), Equals, false)
require.False(t, backend.ProcessHTTPRequest(req))
info := requestutil.GetRequestInfo(req)
req = req.WithContext(requestutil.WithRequestInfo(req.Context(), info))
c.Assert(backend.ProcessHTTPRequest(req), Equals, true)
require.True(t, backend.ProcessHTTPRequest(req))
b, _ := os.ReadFile(fname)
output := strings.SplitN(string(b), "]", 4)
c.Assert(output[3], Equals, 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()))
require.Equal(
t,
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()),
output[3],
)
}

func BenchmarkLocalLogAuditUsingTerminal(b *testing.B) {
Expand Down
Loading

0 comments on commit c96f2a0

Please sign in to comment.