From d031342b4e104a3ba21812253ad84f5576aea994 Mon Sep 17 00:00:00 2001 From: Ryan Leung Date: Fri, 8 Sep 2023 17:09:06 +0800 Subject: [PATCH] mcs: improve scheduling service log (#7060) ref tikv/pd#5839 Signed-off-by: Ryan Leung --- go.mod | 2 +- pkg/gc/safepoint_v2.go | 2 +- pkg/mcs/scheduling/server/server.go | 6 +++- pkg/mcs/tso/server/config.go | 2 +- pkg/mcs/tso/server/grpc_service.go | 2 +- pkg/mcs/utils/util.go | 2 +- pkg/mock/mockcluster/mockcluster.go | 2 +- pkg/schedule/config/config.go | 2 +- pkg/utils/configutil/configutil.go | 2 +- pkg/utils/grpcutil/grpcutil.go | 2 +- server/apiv2/handlers/tso_keyspace_group.go | 2 +- tests/scheduling_cluster.go | 2 ++ tools/pd-api-bench/Makefile | 34 +++++++++++++++++++++ tools/pd-api-bench/cases/cases.go | 11 ++++++- tools/pd-api-bench/go.mod | 1 - tools/pd-api-bench/main.go | 6 ++-- tools/pd-tso-bench/Makefile | 34 +++++++++++++++++++++ 17 files changed, 98 insertions(+), 16 deletions(-) create mode 100644 tools/pd-api-bench/Makefile create mode 100644 tools/pd-tso-bench/Makefile diff --git a/go.mod b/go.mod index 05f60191210..503ac53f33f 100644 --- a/go.mod +++ b/go.mod @@ -145,7 +145,7 @@ require ( github.com/pelletier/go-toml/v2 v2.0.1 // indirect github.com/petermattis/goid v0.0.0-20211229010228-4d14c490ee36 // indirect github.com/pingcap/tipb v0.0.0-20220718022156-3e2483c20a9e // indirect - github.com/pkg/errors v0.9.1 + github.com/pkg/errors v0.9.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect github.com/power-devops/perfstat v0.0.0-20221212215047-62379fc7944b // indirect github.com/prometheus/client_model v0.2.0 // indirect diff --git a/pkg/gc/safepoint_v2.go b/pkg/gc/safepoint_v2.go index f936601d2d9..665249bcab0 100644 --- a/pkg/gc/safepoint_v2.go +++ b/pkg/gc/safepoint_v2.go @@ -18,10 +18,10 @@ import ( "context" "time" + "github.com/pingcap/errors" "github.com/pingcap/failpoint" "github.com/pingcap/kvproto/pkg/keyspacepb" "github.com/pingcap/log" - "github.com/pkg/errors" "github.com/tikv/pd/pkg/keyspace" "github.com/tikv/pd/pkg/slice" "github.com/tikv/pd/pkg/storage/endpoint" diff --git a/pkg/mcs/scheduling/server/server.go b/pkg/mcs/scheduling/server/server.go index 43a8d093a87..cbc73f79dbf 100644 --- a/pkg/mcs/scheduling/server/server.go +++ b/pkg/mcs/scheduling/server/server.go @@ -155,6 +155,7 @@ func (s *Server) updateAPIServerMemberLoop() { ticker = time.NewTicker(100 * time.Millisecond) }) defer ticker.Stop() + var curLeader uint64 for { select { case <-ctx.Done(): @@ -180,7 +181,10 @@ func (s *Server) updateAPIServerMemberLoop() { log.Info("failed to get delegate client", errs.ZapError(err)) } if s.cluster.SwitchAPIServerLeader(pdpb.NewPDClient(cc)) { - log.Info("switch leader", zap.String("leader-id", fmt.Sprintf("%x", ep.ID)), zap.String("endpoint", ep.ClientURLs[0])) + if status.Leader != curLeader { + log.Info("switch leader", zap.String("leader-id", fmt.Sprintf("%x", ep.ID)), zap.String("endpoint", ep.ClientURLs[0])) + } + curLeader = ep.ID break } } diff --git a/pkg/mcs/tso/server/config.go b/pkg/mcs/tso/server/config.go index 23bff558239..eedf3a2f1b1 100644 --- a/pkg/mcs/tso/server/config.go +++ b/pkg/mcs/tso/server/config.go @@ -22,8 +22,8 @@ import ( "time" "github.com/BurntSushi/toml" + "github.com/pingcap/errors" "github.com/pingcap/log" - "github.com/pkg/errors" "github.com/spf13/pflag" "github.com/tikv/pd/pkg/mcs/utils" "github.com/tikv/pd/pkg/tso" diff --git a/pkg/mcs/tso/server/grpc_service.go b/pkg/mcs/tso/server/grpc_service.go index fb4d14ef797..40a308c72f8 100644 --- a/pkg/mcs/tso/server/grpc_service.go +++ b/pkg/mcs/tso/server/grpc_service.go @@ -22,9 +22,9 @@ import ( "strings" "time" + "github.com/pingcap/errors" "github.com/pingcap/kvproto/pkg/tsopb" "github.com/pingcap/log" - "github.com/pkg/errors" bs "github.com/tikv/pd/pkg/basicserver" "github.com/tikv/pd/pkg/mcs/registry" "github.com/tikv/pd/pkg/utils/apiutil" diff --git a/pkg/mcs/utils/util.go b/pkg/mcs/utils/util.go index 2390f5be4f0..682e73f20ae 100644 --- a/pkg/mcs/utils/util.go +++ b/pkg/mcs/utils/util.go @@ -24,10 +24,10 @@ import ( "time" "github.com/gin-gonic/gin" + "github.com/pingcap/errors" "github.com/pingcap/kvproto/pkg/diagnosticspb" "github.com/pingcap/kvproto/pkg/pdpb" "github.com/pingcap/log" - "github.com/pkg/errors" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promhttp" "github.com/soheilhy/cmux" diff --git a/pkg/mock/mockcluster/mockcluster.go b/pkg/mock/mockcluster/mockcluster.go index cf25bb89c3c..1ed7ab4eb9f 100644 --- a/pkg/mock/mockcluster/mockcluster.go +++ b/pkg/mock/mockcluster/mockcluster.go @@ -21,10 +21,10 @@ import ( "time" "github.com/docker/go-units" + "github.com/pingcap/errors" "github.com/pingcap/kvproto/pkg/metapb" "github.com/pingcap/kvproto/pkg/pdpb" "github.com/pingcap/log" - "github.com/pkg/errors" "github.com/tikv/pd/pkg/core" "github.com/tikv/pd/pkg/core/storelimit" "github.com/tikv/pd/pkg/errs" diff --git a/pkg/schedule/config/config.go b/pkg/schedule/config/config.go index f87b702e0b2..c8fa62b8aff 100644 --- a/pkg/schedule/config/config.go +++ b/pkg/schedule/config/config.go @@ -17,8 +17,8 @@ package config import ( "time" + "github.com/pingcap/errors" "github.com/pingcap/kvproto/pkg/metapb" - "github.com/pkg/errors" "github.com/tikv/pd/pkg/core/storelimit" "github.com/tikv/pd/pkg/utils/configutil" "github.com/tikv/pd/pkg/utils/syncutil" diff --git a/pkg/utils/configutil/configutil.go b/pkg/utils/configutil/configutil.go index 4349721ba78..978edce7764 100644 --- a/pkg/utils/configutil/configutil.go +++ b/pkg/utils/configutil/configutil.go @@ -21,7 +21,7 @@ import ( "time" "github.com/BurntSushi/toml" - "github.com/pkg/errors" + "github.com/pingcap/errors" "github.com/spf13/pflag" "github.com/tikv/pd/pkg/encryption" "github.com/tikv/pd/pkg/utils/grpcutil" diff --git a/pkg/utils/grpcutil/grpcutil.go b/pkg/utils/grpcutil/grpcutil.go index 6b5290bf31c..ee9d85a4ee1 100644 --- a/pkg/utils/grpcutil/grpcutil.go +++ b/pkg/utils/grpcutil/grpcutil.go @@ -21,8 +21,8 @@ import ( "net/url" "time" + "github.com/pingcap/errors" "github.com/pingcap/log" - "github.com/pkg/errors" "github.com/tikv/pd/pkg/errs" "github.com/tikv/pd/pkg/utils/logutil" "go.etcd.io/etcd/pkg/transport" diff --git a/server/apiv2/handlers/tso_keyspace_group.go b/server/apiv2/handlers/tso_keyspace_group.go index d16cd333e87..1e64e125038 100644 --- a/server/apiv2/handlers/tso_keyspace_group.go +++ b/server/apiv2/handlers/tso_keyspace_group.go @@ -21,7 +21,7 @@ import ( "sync" "github.com/gin-gonic/gin" - "github.com/pkg/errors" + "github.com/pingcap/errors" "github.com/tikv/pd/pkg/errs" "github.com/tikv/pd/pkg/mcs/utils" "github.com/tikv/pd/pkg/slice" diff --git a/tests/scheduling_cluster.go b/tests/scheduling_cluster.go index c74d73b0d4b..1768c4128cc 100644 --- a/tests/scheduling_cluster.go +++ b/tests/scheduling_cluster.go @@ -21,6 +21,7 @@ import ( "github.com/stretchr/testify/require" scheduling "github.com/tikv/pd/pkg/mcs/scheduling/server" sc "github.com/tikv/pd/pkg/mcs/scheduling/server/config" + "github.com/tikv/pd/pkg/schedule/schedulers" "github.com/tikv/pd/pkg/utils/tempurl" "github.com/tikv/pd/pkg/utils/testutil" ) @@ -36,6 +37,7 @@ type TestSchedulingCluster struct { // NewTestSchedulingCluster creates a new scheduling test cluster. func NewTestSchedulingCluster(ctx context.Context, initialServerCount int, backendEndpoints string) (tc *TestSchedulingCluster, err error) { + schedulers.Register() tc = &TestSchedulingCluster{ ctx: ctx, backendEndpoints: backendEndpoints, diff --git a/tools/pd-api-bench/Makefile b/tools/pd-api-bench/Makefile new file mode 100644 index 00000000000..7e517ef92b9 --- /dev/null +++ b/tools/pd-api-bench/Makefile @@ -0,0 +1,34 @@ +# Copyright 2023 TiKV Project Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +ROOT_PATH := ../.. +GO_TOOLS_BIN_PATH := $(ROOT_PATH)/.tools/bin +PATH := $(GO_TOOLS_BIN_PATH):$(PATH) +SHELL := env PATH='$(PATH)' GOBIN='$(GO_TOOLS_BIN_PATH)' $(shell which bash) + +install-tools: + cd $(ROOT_PATH) && $(MAKE) install-tools + +static: install-tools + @ echo "gofmt ..." + @ gofmt -s -l -d . 2>&1 | awk '{ print } END { if (NR > 0) { exit 1 } }' + @ echo "golangci-lint ..." + @ golangci-lint run -c $(ROOT_PATH)/.golangci.yml --verbose ./... --allow-parallel-runners + @ echo "revive ..." + @ revive -formatter friendly -config $(ROOT_PATH)/revive.toml ./... + +tidy: + @ go mod tidy + git diff go.mod go.sum | cat + git diff --quiet go.mod go.sum diff --git a/tools/pd-api-bench/cases/cases.go b/tools/pd-api-bench/cases/cases.go index 2b770805cd8..d431b6f325c 100644 --- a/tools/pd-api-bench/cases/cases.go +++ b/tools/pd-api-bench/cases/cases.go @@ -29,8 +29,10 @@ import ( ) var ( + // PDAddress is the address of PD server. PDAddress string - Debug bool + // Debug is the flag to print the output of api response for debug. + Debug bool ) var ( @@ -39,6 +41,7 @@ var ( storesID []uint64 ) +// InitCluster initializes the cluster. func InitCluster(ctx context.Context, cli pd.Client, httpClit *http.Client) error { req, _ := http.NewRequestWithContext(ctx, http.MethodGet, PDAddress+"/pd/api/v1/stats/region?start_key=&end_key=&count", nil) @@ -67,6 +70,7 @@ func InitCluster(ctx context.Context, cli pd.Client, httpClit *http.Client) erro return nil } +// Case is the interface for all cases. type Case interface { Name() string SetQPS(int64) @@ -101,11 +105,13 @@ func (c *baseCase) GetBurst() int64 { return c.burst } +// GRPCCase is the interface for all gRPC cases. type GRPCCase interface { Case Unary(context.Context, pd.Client) error } +// GRPCCaseMap is the map for all gRPC cases. var GRPCCaseMap = map[string]GRPCCase{ "GetRegion": newGetRegion(), "GetStore": newGetStore(), @@ -113,12 +119,14 @@ var GRPCCaseMap = map[string]GRPCCase{ "ScanRegions": newScanRegions(), } +// HTTPCase is the interface for all HTTP cases. type HTTPCase interface { Case Do(context.Context, *http.Client) error Params(string) } +// HTTPCaseMap is the map for all HTTP cases. var HTTPCaseMap = map[string]HTTPCase{ "GetRegionStatus": newRegionStats(), "GetMinResolvedTS": newMinResolvedTS(), @@ -319,6 +327,7 @@ func (c *getStores) Unary(ctx context.Context, cli pd.Client) error { return nil } +// nolint func generateKeyForSimulator(id int, keyLen int) []byte { k := make([]byte, keyLen) copy(k, fmt.Sprintf("%010d", id)) diff --git a/tools/pd-api-bench/go.mod b/tools/pd-api-bench/go.mod index 1a0f022403b..1cef635e20b 100644 --- a/tools/pd-api-bench/go.mod +++ b/tools/pd-api-bench/go.mod @@ -73,7 +73,6 @@ require ( github.com/pingcap/kvproto v0.0.0-20230905082026-5336fac26974 // indirect github.com/pingcap/log v1.1.1-0.20221110025148-ca232912c9f3 // indirect github.com/pingcap/sysutil v1.0.1-0.20230407040306-fb007c5aff21 // indirect - github.com/pkg/errors v0.9.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect github.com/power-devops/perfstat v0.0.0-20221212215047-62379fc7944b // indirect github.com/prometheus/client_golang v1.11.1 // indirect diff --git a/tools/pd-api-bench/main.go b/tools/pd-api-bench/main.go index a891f7d2318..3b413d4dd76 100644 --- a/tools/pd-api-bench/main.go +++ b/tools/pd-api-bench/main.go @@ -160,7 +160,7 @@ func main() { } httpClis := make([]*http.Client, 0) for i := 0; i < *client; i++ { - httpClis = append(httpClis, newHttpClient()) + httpClis = append(httpClis, newHTTPClient()) } err = cases.InitCluster(ctx, pdClis[0], httpClis[0]) if err != nil { @@ -248,8 +248,8 @@ func exit(code int) { os.Exit(code) } -// newHttpClient returns an HTTP(s) client. -func newHttpClient() *http.Client { +// newHTTPClient returns an HTTP(s) client. +func newHTTPClient() *http.Client { // defaultTimeout for non-context requests. const defaultTimeout = 30 * time.Second cli := &http.Client{Timeout: defaultTimeout} diff --git a/tools/pd-tso-bench/Makefile b/tools/pd-tso-bench/Makefile new file mode 100644 index 00000000000..7e517ef92b9 --- /dev/null +++ b/tools/pd-tso-bench/Makefile @@ -0,0 +1,34 @@ +# Copyright 2023 TiKV Project Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +ROOT_PATH := ../.. +GO_TOOLS_BIN_PATH := $(ROOT_PATH)/.tools/bin +PATH := $(GO_TOOLS_BIN_PATH):$(PATH) +SHELL := env PATH='$(PATH)' GOBIN='$(GO_TOOLS_BIN_PATH)' $(shell which bash) + +install-tools: + cd $(ROOT_PATH) && $(MAKE) install-tools + +static: install-tools + @ echo "gofmt ..." + @ gofmt -s -l -d . 2>&1 | awk '{ print } END { if (NR > 0) { exit 1 } }' + @ echo "golangci-lint ..." + @ golangci-lint run -c $(ROOT_PATH)/.golangci.yml --verbose ./... --allow-parallel-runners + @ echo "revive ..." + @ revive -formatter friendly -config $(ROOT_PATH)/revive.toml ./... + +tidy: + @ go mod tidy + git diff go.mod go.sum | cat + git diff --quiet go.mod go.sum