Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

*: supports build pd-server with fail point #6946

Merged
merged 3 commits into from
Aug 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ else
BUILD_CGO_ENABLED := 1
endif

ifeq ($(FAILPOINT), 1)
BUILD_TAGS += with_fail
endif

ifeq ("$(WITH_RACE)", "1")
BUILD_FLAGS += -race
BUILD_CGO_ENABLED := 1
Expand Down Expand Up @@ -73,6 +77,11 @@ PD_SERVER_DEP += dashboard-ui
pd-server: ${PD_SERVER_DEP}
CGO_ENABLED=$(BUILD_CGO_ENABLED) go build $(BUILD_FLAGS) -gcflags '$(GCFLAGS)' -ldflags '$(LDFLAGS)' -tags "$(BUILD_TAGS)" -o $(BUILD_BIN_PATH)/pd-server cmd/pd-server/main.go

pd-server-failpoint:
@$(FAILPOINT_ENABLE)
FAILPOINT=1 $(MAKE) pd-server || { $(FAILPOINT_DISABLE); exit 1; }
@$(FAILPOINT_DISABLE)

pd-server-basic:
SWAGGER=0 DASHBOARD=0 $(MAKE) pd-server

Expand Down
8 changes: 8 additions & 0 deletions pkg/utils/etcdutil/etcdutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@

// ListEtcdMembers returns a list of internal etcd members.
func ListEtcdMembers(client *clientv3.Client) (*clientv3.MemberListResponse, error) {
failpoint.Inject("SlowEtcdMemberList", func(val failpoint.Value) {
d := val.(int)
time.Sleep(time.Duration(d) * time.Second)
})

Check warning on line 112 in pkg/utils/etcdutil/etcdutil.go

View check run for this annotation

Codecov / codecov/patch

pkg/utils/etcdutil/etcdutil.go#L110-L112

Added lines #L110 - L112 were not covered by tests
ctx, cancel := context.WithTimeout(client.Ctx(), DefaultRequestTimeout)
listResp, err := client.MemberList(ctx)
cancel()
Expand All @@ -132,6 +136,10 @@
defer cancel()

start := time.Now()
failpoint.Inject("SlowEtcdKVGet", func(val failpoint.Value) {
d := val.(int)
time.Sleep(time.Duration(d) * time.Second)
})

Check warning on line 142 in pkg/utils/etcdutil/etcdutil.go

View check run for this annotation

Codecov / codecov/patch

pkg/utils/etcdutil/etcdutil.go#L140-L142

Added lines #L140 - L142 were not covered by tests
resp, err := clientv3.NewKV(c).Get(ctx, key, opts...)
if cost := time.Since(start); cost > DefaultSlowRequestTime {
log.Warn("kv gets too slow", zap.String("request-key", key), zap.Duration("cost", cost), errs.ZapError(err))
Expand Down
22 changes: 22 additions & 0 deletions server/api/failpoint.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// 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.

//go:build with_fail
// +build with_fail

package api

func init() {
enableFailPointAPI = true
}
11 changes: 6 additions & 5 deletions server/api/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
"github.com/unrolled/render"
)

// enableFailPointAPI enable fail point API handler.
var enableFailPointAPI bool

// createRouteOption is used to register service for mux.Route
type createRouteOption func(route *mux.Route)

Expand Down Expand Up @@ -365,15 +368,13 @@
registerFunc(apiRouter, "/admin/reset-ts", tsoAdminHandler.ResetTS, setMethods(http.MethodPost), setAuditBackend(localLog, prometheus))

// API to set or unset failpoints
failpoint.Inject("enableFailpointAPI", func() {
// this function will be named to "func2". It may be used in test
if enableFailPointAPI {
registerPrefix(apiRouter, "/fail", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// The HTTP handler of failpoint requires the full path to be the failpoint path.
r.URL.Path = strings.TrimPrefix(r.URL.Path, prefix+apiPrefix+"/fail")
new(failpoint.HttpHandler).ServeHTTP(w, r)
}), setAuditBackend("test"))
})

}), setAuditBackend(localLog))

Check warning on line 376 in server/api/router.go

View check run for this annotation

Codecov / codecov/patch

server/api/router.go#L376

Added line #L376 was not covered by tests
}
// Deprecated: use /pd/api/v1/health instead.
rootRouter.HandleFunc("/health", healthHandler.GetHealthStatus).Methods(http.MethodGet)
// Deprecated: use /pd/api/v1/ping instead.
Expand Down
Loading