From 08ea1fadd6e5cf5e2dffd838140e2bc274485509 Mon Sep 17 00:00:00 2001 From: Ryan Leung Date: Mon, 26 Aug 2019 14:03:00 +0800 Subject: [PATCH 1/2] support deadlock tests Signed-off-by: Ryan Leung --- .gitignore | 1 + Makefile | 21 +++++++++++++++++++-- pkg/cache/cache.go | 4 +--- scripts/retool-install.sh | 2 ++ tools.json | 4 ++++ 5 files changed, 27 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index 81e2de79eb9..7f16d2e8a40 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,4 @@ tags /.retools/ vendor default* +*.bak diff --git a/Makefile b/Makefile index 01b8afa284f..88a5c3fd5af 100644 --- a/Makefile +++ b/Makefile @@ -55,10 +55,12 @@ pd-recover: CGO_ENABLED=0 go build -o bin/pd-recover tools/pd-recover/main.go test: retool-setup - # testing.. + # testing... + make deadlock-enable @$(FAILPOINT_ENABLE) - CGO_ENABLED=1 GO111MODULE=on go test -race -cover $(TEST_PKGS) || { $(FAILPOINT_DISABLE); exit 1; } + CGO_ENABLED=1 GO111MODULE=on go test -race -cover $(TEST_PKGS) || { $(FAILPOINT_DISABLE); make deadlock-disable; exit 1; } @$(FAILPOINT_DISABLE) + make deadlock-disable basic-test: @$(FAILPOINT_ENABLE) @@ -126,6 +128,21 @@ clean-test: rm -rf /tmp/pd-tests* rm -rf /tmp/test_etcd* +deadlock-setup: export GO111MODULE=off +deadlock-setup: + go get github.com/sasha-s/go-deadlock + +deadlock-enable: deadlock-setup + find . -name "*.go" | grep -vE "(vendor|\.retools)" | xargs -n 1 sed -i.bak 's/sync.RWMutex/deadlock.RWMutex/' + find . -name "*.go" | grep -vE "(vendor|\.retools)" | xargs -n 1 sed -i.bak 's/sync.Mutex/deadlock.Mutex/' + find . -name "*.go" | grep -vE "(vendor|\.retools)" | xargs -n 1 ./scripts/retool do goimports -w + +deadlock-disable: + find . -name "*.go" | grep -vE "(vendor|\.retools)" | xargs -n 1 sed -i.bak 's/deadlock.RWMutex/sync.RWMutex/' + find . -name "*.go" | grep -vE "(vendor|\.retools)" | xargs -n 1 sed -i.bak 's/deadlock.Mutex/sync.Mutex/' + find . -name "*.go" | grep -vE "(vendor|\.retools)" | xargs -n 1 ./scripts/retool do goimports -w + find . -name "*.bak" | grep -vE "(vendor|\.retools)" | xargs rm + failpoint-enable: # Converting failpoints... @$(FAILPOINT_ENABLE) diff --git a/pkg/cache/cache.go b/pkg/cache/cache.go index 7784d16e17b..7e07f16dbae 100644 --- a/pkg/cache/cache.go +++ b/pkg/cache/cache.go @@ -13,9 +13,7 @@ package cache -import ( - "sync" -) +import "sync" // Cache is an interface for cache system type Cache interface { diff --git a/scripts/retool-install.sh b/scripts/retool-install.sh index c72e09af658..75287bab469 100755 --- a/scripts/retool-install.sh +++ b/scripts/retool-install.sh @@ -23,3 +23,5 @@ which retool >/dev/null || go get github.com/twitchtv/retool ./scripts/retool add github.com/securego/gosec/cmd/gosec 1.0.0 # go fail ./scripts/retool add github.com/pingcap/failpoint/failpoint-ctl master +# deadlock detection +./scripts/retool add golang.org/x/tools/cmd/goimports 04b5d21e00f1f47bd824a6ade581e7189bacde87 diff --git a/tools.json b/tools.json index 98cb31eed52..25cc8d486d1 100644 --- a/tools.json +++ b/tools.json @@ -19,6 +19,10 @@ { "Repository": "github.com/golangci/golangci-lint/cmd/golangci-lint", "Commit": "4ba2155996359eabd8800d1fbf3e3a9777c80490" + }, + { + "Repository": "golang.org/x/tools/cmd/goimports", + "Commit": "04b5d21e00f1f47bd824a6ade581e7189bacde87" } ], "RetoolVersion": "1.3.7" From 8e873e1279e9a15f2f8aacd7c3a30a32380b1657 Mon Sep 17 00:00:00 2001 From: Ryan Leung Date: Tue, 27 Aug 2019 16:24:23 +0800 Subject: [PATCH 2/2] address comments Signed-off-by: Ryan Leung --- Makefile | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/Makefile b/Makefile index 88a5c3fd5af..3ce7f484df2 100644 --- a/Makefile +++ b/Makefile @@ -15,6 +15,14 @@ OVERALLS := overalls FAILPOINT_ENABLE := $$(find $$PWD/ -type d | grep -vE "(\.git|\.retools)" | xargs ./scripts/retool do failpoint-ctl enable) FAILPOINT_DISABLE := $$(find $$PWD/ -type d | grep -vE "(\.git|\.retools)" | xargs ./scripts/retool do failpoint-ctl disable) +DEADLOCK_ENABLE := $$(find . -name "*.go" | grep -vE "(vendor|\.retools)" | xargs -n 1 sed -i.bak 's/sync.RWMutex/deadlock.RWMutex/' && \ + find . -name "*.go" | grep -vE "(vendor|\.retools)" | xargs -n 1 sed -i.bak 's/sync.Mutex/deadlock.Mutex/' && \ + find . -name "*.go" | grep -vE "(vendor|\.retools)" | xargs -n 1 ./scripts/retool do goimports -w) +DEADLOCK_DISABLE := $$(find . -name "*.go" | grep -vE "(vendor|\.retools)" | xargs -n 1 sed -i.bak 's/deadlock.RWMutex/sync.RWMutex/' && \ + find . -name "*.go" | grep -vE "(vendor|\.retools)" | xargs -n 1 sed -i.bak 's/deadlock.Mutex/sync.Mutex/' && \ + find . -name "*.go" | grep -vE "(vendor|\.retools)" | xargs -n 1 ./scripts/retool do goimports -w && \ + find . -name "*.bak" | grep -vE "(vendor|\.retools)" | xargs rm) + LDFLAGS += -X "$(PD_PKG)/server.PDReleaseVersion=$(shell git describe --tags --dirty)" LDFLAGS += -X "$(PD_PKG)/server.PDBuildTS=$(shell date -u '+%Y-%m-%d %I:%M:%S')" LDFLAGS += -X "$(PD_PKG)/server.PDGitHash=$(shell git rev-parse HEAD)" @@ -56,11 +64,11 @@ pd-recover: test: retool-setup # testing... - make deadlock-enable + @$(DEADLOCK_ENABLE) @$(FAILPOINT_ENABLE) - CGO_ENABLED=1 GO111MODULE=on go test -race -cover $(TEST_PKGS) || { $(FAILPOINT_DISABLE); make deadlock-disable; exit 1; } + CGO_ENABLED=1 GO111MODULE=on go test -race -cover $(TEST_PKGS) || { $(FAILPOINT_DISABLE); $(DEADLOCK_DISABLE); exit 1; } @$(FAILPOINT_DISABLE) - make deadlock-disable + @$(DEADLOCK_DISABLE) basic-test: @$(FAILPOINT_ENABLE) @@ -133,15 +141,10 @@ deadlock-setup: go get github.com/sasha-s/go-deadlock deadlock-enable: deadlock-setup - find . -name "*.go" | grep -vE "(vendor|\.retools)" | xargs -n 1 sed -i.bak 's/sync.RWMutex/deadlock.RWMutex/' - find . -name "*.go" | grep -vE "(vendor|\.retools)" | xargs -n 1 sed -i.bak 's/sync.Mutex/deadlock.Mutex/' - find . -name "*.go" | grep -vE "(vendor|\.retools)" | xargs -n 1 ./scripts/retool do goimports -w + @$(DEADLOCK_ENABLE) deadlock-disable: - find . -name "*.go" | grep -vE "(vendor|\.retools)" | xargs -n 1 sed -i.bak 's/deadlock.RWMutex/sync.RWMutex/' - find . -name "*.go" | grep -vE "(vendor|\.retools)" | xargs -n 1 sed -i.bak 's/deadlock.Mutex/sync.Mutex/' - find . -name "*.go" | grep -vE "(vendor|\.retools)" | xargs -n 1 ./scripts/retool do goimports -w - find . -name "*.bak" | grep -vE "(vendor|\.retools)" | xargs rm + @$(DEADLOCK_DISABLE) failpoint-enable: # Converting failpoints...