Skip to content

Commit 45d7867

Browse files
rleungxti-chi-bot
andauthored
*: use build tag to manage deadlock check (#4863)
close #4867 Signed-off-by: Ryan Leung <rleungx@gmail.com> Co-authored-by: Ti Chi Robot <ti-community-prow-bot@tidb.io>
1 parent 6e615ed commit 45d7867

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+202
-149
lines changed

Makefile

+6-37
Original file line numberDiff line numberDiff line change
@@ -180,27 +180,6 @@ check-testing-t:
180180
FAILPOINT_ENABLE := $$(find $$PWD/ -type d | grep -vE "\.git" | xargs failpoint-ctl enable)
181181
FAILPOINT_DISABLE := $$(find $$PWD/ -type d | grep -vE "\.git" | xargs failpoint-ctl disable)
182182

183-
DEADLOCK_ENABLE := $$(\
184-
find . -name "*.go" \
185-
| xargs -n 1 sed -i.bak 's/sync\.RWMutex/deadlock.RWMutex/;s/sync\.Mutex/deadlock.Mutex/' && \
186-
find . -name "*.go" | xargs grep -lE "(deadlock\.RWMutex|deadlock\.Mutex)" \
187-
| xargs goimports -w)
188-
DEADLOCK_DISABLE := $$(\
189-
find . -name "*.go" \
190-
| xargs -n 1 sed -i.bak 's/deadlock\.RWMutex/sync.RWMutex/;s/deadlock\.Mutex/sync.Mutex/' && \
191-
find . -name "*.go" | xargs grep -lE "(sync\.RWMutex|sync\.Mutex)" \
192-
| xargs goimports -w && \
193-
find . -name "*.bak" | xargs rm && \
194-
go mod tidy)
195-
196-
deadlock-enable: install-tools
197-
# Enabling deadlock...
198-
@$(DEADLOCK_ENABLE)
199-
200-
deadlock-disable: install-tools
201-
# Disabling deadlock...
202-
@$(DEADLOCK_DISABLE)
203-
204183
failpoint-enable: install-tools
205184
# Converting failpoints...
206185
@$(FAILPOINT_ENABLE)
@@ -209,7 +188,7 @@ failpoint-disable: install-tools
209188
# Restoring failpoints...
210189
@$(FAILPOINT_DISABLE)
211190

212-
.PHONY: deadlock-enable deadlock-disable failpoint-enable failpoint-disable
191+
.PHONY: failpoint-enable failpoint-disable
213192

214193
#### Test ####
215194

@@ -223,11 +202,9 @@ SUBMODULES := $(filter $(shell find . -iname "go.mod" -exec dirname {} \;),\
223202

224203
test: install-tools
225204
# testing all pkgs...
226-
@$(DEADLOCK_ENABLE)
227205
@$(FAILPOINT_ENABLE)
228-
CGO_ENABLED=1 go test -tags tso_function_test -timeout 20m -race -cover $(TEST_PKGS) || { $(FAILPOINT_DISABLE); $(DEADLOCK_DISABLE); exit 1; }
206+
CGO_ENABLED=1 go test -tags tso_function_test,deadlock -timeout 20m -race -cover $(TEST_PKGS) || { $(FAILPOINT_DISABLE); exit 1; }
229207
@$(FAILPOINT_DISABLE)
230-
@$(DEADLOCK_DISABLE)
231208

232209
basic-test: install-tools
233210
# testing basic pkgs...
@@ -236,36 +213,28 @@ basic-test: install-tools
236213
@$(FAILPOINT_DISABLE)
237214

238215
ci-test-job: install-tools dashboard-ui
239-
@$(DEADLOCK_ENABLE)
240216
@$(FAILPOINT_ENABLE)
241-
CGO_ENABLED=1 go test -race -covermode=atomic -coverprofile=covprofile -coverpkg=./... $(shell ./scripts/ci-subtask.sh $(JOB_COUNT) $(JOB_INDEX))
217+
CGO_ENABLED=1 go test -tags deadlock -race -covermode=atomic -coverprofile=covprofile -coverpkg=./... $(shell ./scripts/ci-subtask.sh $(JOB_COUNT) $(JOB_INDEX))
242218
@$(FAILPOINT_DISABLE)
243-
@$(DEADLOCK_DISABLE)
244219

245220
ci-test-job-submod: install-tools dashboard-ui
246-
@$(DEADLOCK_ENABLE)
247221
@$(FAILPOINT_ENABLE)
248222
@ for mod in $(SUBMODULES); do cd $$mod && $(MAKE) ci-test-job && cd - > /dev/null && cat $$mod/covprofile >> covprofile; done
249223
@$(FAILPOINT_DISABLE)
250-
@$(DEADLOCK_DISABLE)
251224

252225
TSO_INTEGRATION_TEST_PKGS := $(PD_PKG)/tests/server/tso
253226

254227
test-tso-function: install-tools
255228
# testing TSO function...
256-
@$(DEADLOCK_ENABLE)
257229
@$(FAILPOINT_ENABLE)
258-
CGO_ENABLED=1 go test -race -tags without_dashboard,tso_function_test $(TSO_INTEGRATION_TEST_PKGS) || { $(FAILPOINT_DISABLE); $(DEADLOCK_DISABLE); exit 1; }
230+
CGO_ENABLED=1 go test -race -tags without_dashboard,tso_function_test,deadlock $(TSO_INTEGRATION_TEST_PKGS) || { $(FAILPOINT_DISABLE); exit 1; }
259231
@$(FAILPOINT_DISABLE)
260-
@$(DEADLOCK_DISABLE)
261232

262233
test-tso-consistency: install-tools
263234
# testing TSO consistency...
264-
@$(DEADLOCK_ENABLE)
265235
@$(FAILPOINT_ENABLE)
266-
CGO_ENABLED=1 go test -race -tags without_dashboard,tso_consistency_test $(TSO_INTEGRATION_TEST_PKGS) || { $(FAILPOINT_DISABLE); $(DEADLOCK_DISABLE); exit 1; }
236+
CGO_ENABLED=1 go test -race -tags without_dashboard,tso_consistency_test,deadlock $(TSO_INTEGRATION_TEST_PKGS) || { $(FAILPOINT_DISABLE); exit 1; }
267237
@$(FAILPOINT_DISABLE)
268-
@$(DEADLOCK_DISABLE)
269238

270239
.PHONY: test basic-test test-with-cover test-tso-function test-tso-consistency
271240

@@ -292,7 +261,7 @@ split:
292261

293262
#### Clean up ####
294263

295-
clean: failpoint-disable deadlock-disable clean-test clean-build
264+
clean: failpoint-disable clean-test clean-build
296265

297266
clean-test:
298267
# Cleaning test tmp...

pkg/btree/btree.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ package btree
5454

5555
import (
5656
"sort"
57-
"sync"
57+
58+
"github.com/tikv/pd/pkg/syncutil"
5859
)
5960

6061
// Item represents a single object in the tree.
@@ -82,7 +83,7 @@ var (
8283
// FreeList.
8384
// Two Btrees using the same freelist are safe for concurrent write access.
8485
type FreeList struct {
85-
mu sync.Mutex
86+
mu syncutil.Mutex
8687
freelist []*node
8788
}
8889

pkg/cache/cache.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
package cache
1616

17-
import "sync"
17+
import "github.com/tikv/pd/pkg/syncutil"
1818

1919
// Cache is an interface for cache system
2020
type Cache interface {
@@ -49,7 +49,7 @@ var (
4949

5050
type threadSafeCache struct {
5151
cache Cache
52-
lock sync.RWMutex
52+
lock syncutil.RWMutex
5353
}
5454

5555
func newThreadSafeCache(cache Cache) Cache {

pkg/cache/fifo.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,13 @@ package cache
1616

1717
import (
1818
"container/list"
19-
"sync"
19+
20+
"github.com/tikv/pd/pkg/syncutil"
2021
)
2122

2223
// FIFO is 'First-In-First-Out' cache.
2324
type FIFO struct {
24-
sync.RWMutex
25+
syncutil.RWMutex
2526

2627
// maxCount is the maximum number of items.
2728
// 0 means no limit.

pkg/cache/ttl.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ package cache
1616

1717
import (
1818
"context"
19-
"sync"
2019
"time"
2120

2221
"github.com/pingcap/log"
22+
"github.com/tikv/pd/pkg/syncutil"
2323
"go.uber.org/zap"
2424
)
2525

@@ -30,7 +30,7 @@ type ttlCacheItem struct {
3030

3131
// ttlCache is a cache that assigns TTL (Time-To-Live) for each items.
3232
type ttlCache struct {
33-
sync.RWMutex
33+
syncutil.RWMutex
3434
ctx context.Context
3535

3636
items map[interface{}]ttlCacheItem

pkg/dashboard/adapter/redirector.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ import (
1919
"net/http"
2020
"net/http/httputil"
2121
"net/url"
22-
"sync"
2322

2423
"github.com/pingcap/tidb-dashboard/pkg/apiserver"
2524
"github.com/pingcap/tidb-dashboard/pkg/utils"
25+
"github.com/tikv/pd/pkg/syncutil"
2626
)
2727

2828
const (
@@ -31,7 +31,7 @@ const (
3131

3232
// Redirector is used to redirect when the dashboard is started in another PD.
3333
type Redirector struct {
34-
mu sync.RWMutex
34+
mu syncutil.RWMutex
3535

3636
name string
3737
tlsConfig *tls.Config

pkg/movingaverage/queue.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,13 @@
1515
package movingaverage
1616

1717
import (
18-
"sync"
19-
2018
"github.com/phf/go-queue/queue"
19+
"github.com/tikv/pd/pkg/syncutil"
2120
)
2221

2322
// SafeQueue is a concurrency safe queue
2423
type SafeQueue struct {
25-
mu sync.Mutex
24+
mu syncutil.Mutex
2625
que *queue.Queue
2726
}
2827

pkg/progress/progress.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,14 @@ package progress
1616

1717
import (
1818
"math"
19-
"sync"
2019
"time"
20+
21+
"github.com/tikv/pd/pkg/syncutil"
2122
)
2223

2324
// Manager is used to maintain the progresses we care about.
2425
type Manager struct {
25-
sync.RWMutex
26+
syncutil.RWMutex
2627
progesses map[string]*progressIndicator
2728
}
2829

pkg/ratelimit/concurrency_limiter.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@
1414

1515
package ratelimit
1616

17-
import "sync"
17+
import "github.com/tikv/pd/pkg/syncutil"
1818

1919
type concurrencyLimiter struct {
20-
mu sync.RWMutex
20+
mu syncutil.RWMutex
2121
current uint64
2222
limit uint64
2323
}

pkg/ratelimit/ratelimiter.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,17 @@
1515
package ratelimit
1616

1717
import (
18-
"sync"
1918
"time"
2019

20+
"github.com/tikv/pd/pkg/syncutil"
2121
"golang.org/x/time/rate"
2222
)
2323

2424
// RateLimiter is a rate limiter based on `golang.org/x/time/rate`.
2525
// It implements `Available` function which is not included in `golang.org/x/time/rate`.
2626
// Note: AvailableN will increase the wait time of WaitN.
2727
type RateLimiter struct {
28-
mu sync.Mutex
28+
mu syncutil.Mutex
2929
*rate.Limiter
3030
}
3131

pkg/syncutil/mutex_deadlock.go

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright 2022 TiKV Project Authors.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
//go:build deadlock
16+
// +build deadlock
17+
18+
package syncutil
19+
20+
import "github.com/sasha-s/go-deadlock"
21+
22+
// Mutex is a mutual exclusion lock. The zero value for a Mutex is an unlocked mutex.
23+
//
24+
// Mutex must not be copied after first use.
25+
type Mutex struct {
26+
deadlock.Mutex
27+
}
28+
29+
// RWMutex is a reader/writer mutual exclusion lock.
30+
// The lock can be held by an arbitrary number of readers or a single writer.
31+
// The zero value for a RWMutex is an unlocked mutex.
32+
//
33+
// RWMutex must not be copied after first use.
34+
type RWMutex struct {
35+
deadlock.RWMutex
36+
}

pkg/syncutil/mutex_sync.go

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright 2022 TiKV Project Authors.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
//go:build !deadlock
16+
// +build !deadlock
17+
18+
package syncutil
19+
20+
import "sync"
21+
22+
// Mutex is a mutual exclusion lock. The zero value for a Mutex is an unlocked mutex.
23+
//
24+
// Mutex must not be copied after first use.
25+
type Mutex struct {
26+
sync.Mutex
27+
}
28+
29+
// RWMutex is a reader/writer mutual exclusion lock.
30+
// The lock can be held by an arbitrary number of readers or a single writer.
31+
// The zero value for a RWMutex is an unlocked mutex.
32+
//
33+
// RWMutex must not be copied after first use.
34+
type RWMutex struct {
35+
sync.RWMutex
36+
}

plugin/scheduler_example/evict_leader.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ import (
1818
"net/http"
1919
"net/url"
2020
"strconv"
21-
"sync"
2221

2322
"github.com/gorilla/mux"
2423
"github.com/pingcap/errors"
2524
"github.com/pingcap/log"
2625
"github.com/tikv/pd/pkg/apiutil"
2726
"github.com/tikv/pd/pkg/errs"
27+
"github.com/tikv/pd/pkg/syncutil"
2828
"github.com/tikv/pd/server/core"
2929
"github.com/tikv/pd/server/schedule"
3030
"github.com/tikv/pd/server/schedule/filter"
@@ -90,7 +90,7 @@ func SchedulerArgs() []string {
9090
}
9191

9292
type evictLeaderSchedulerConfig struct {
93-
mu sync.RWMutex
93+
mu syncutil.RWMutex
9494
storage endpoint.ConfigStorage
9595
StoreIDWitRanges map[uint64][]core.KeyRange `json:"store-id-ranges"`
9696
cluster schedule.Cluster

server/cluster/cluster.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import (
3535
"github.com/tikv/pd/pkg/etcdutil"
3636
"github.com/tikv/pd/pkg/logutil"
3737
"github.com/tikv/pd/pkg/progress"
38+
"github.com/tikv/pd/pkg/syncutil"
3839
"github.com/tikv/pd/pkg/typeutil"
3940
"github.com/tikv/pd/server/config"
4041
"github.com/tikv/pd/server/core"
@@ -96,7 +97,7 @@ type Server interface {
9697
// store 1 -> /1/raft/s/1, value is metapb.Store
9798
// region 1 -> /1/raft/r/1, value is metapb.Region
9899
type RaftCluster struct {
99-
sync.RWMutex
100+
syncutil.RWMutex
100101
wg sync.WaitGroup
101102

102103
serverCtx context.Context

server/cluster/cluster_stat.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ package cluster
1616

1717
import (
1818
"strings"
19-
"sync"
2019
"time"
2120

2221
"github.com/pingcap/kvproto/pkg/pdpb"
2322
"github.com/pingcap/log"
2423
"github.com/tikv/pd/pkg/movingaverage"
2524
"github.com/tikv/pd/pkg/slice"
25+
"github.com/tikv/pd/pkg/syncutil"
2626
"go.uber.org/zap"
2727
)
2828

@@ -161,7 +161,7 @@ func (s *CPUEntries) CPU() float64 {
161161

162162
// StatEntries saves the StatEntries for each store in the cluster
163163
type StatEntries struct {
164-
m sync.RWMutex
164+
m syncutil.RWMutex
165165
stats map[uint64]*CPUEntries
166166
size int // size of entries to keep for each store
167167
total int64 // total of StatEntry appended

0 commit comments

Comments
 (0)