forked from ngaut/unistore
/
detector.go
193 lines (178 loc) · 5.17 KB
/
detector.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
// Copyright 2019 PingCAP, Inc.
//
// 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,
// See the License for the specific language governing permissions and
// limitations under the License.
// Copyright 2019-present PingCAP, Inc.
//
// 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,
// See the License for the specific language governing permissions and
// limitations under the License.
package tikv
import (
"container/list"
"sync"
"time"
"github.com/pingcap/log"
"go.uber.org/zap"
)
// Detector detects deadlock.
type Detector struct {
waitForMap map[uint64]*txnList
lock sync.Mutex
entryTTL time.Duration
totalSize uint64
lastActiveExpire time.Time
urgentSize uint64
expireInterval time.Duration
}
type txnList struct {
//txns []txnKeyHashPair
txns *list.List
}
type txnKeyHashPair struct {
txn uint64
keyHash uint64
registerTime time.Time
}
func (p *txnKeyHashPair) isExpired(ttl time.Duration, nowTime time.Time) bool {
if p.registerTime.Add(ttl).Before(nowTime) {
return true
}
return false
}
// NewDetector creates a new Detector.
func NewDetector(ttl time.Duration, urgentSize uint64, expireInterval time.Duration) *Detector {
return &Detector{
waitForMap: map[uint64]*txnList{},
entryTTL: ttl,
lastActiveExpire: time.Now(),
urgentSize: urgentSize,
expireInterval: expireInterval,
}
}
// Detect detects deadlock for the sourceTxn on a locked key.
func (d *Detector) Detect(sourceTxn, waitForTxn, keyHash uint64) *ErrDeadlock {
d.lock.Lock()
nowTime := time.Now()
d.activeExpire(nowTime)
err := d.doDetect(nowTime, sourceTxn, waitForTxn)
if err == nil {
d.register(sourceTxn, waitForTxn, keyHash)
}
d.lock.Unlock()
return err
}
func (d *Detector) doDetect(nowTime time.Time, sourceTxn, waitForTxn uint64) *ErrDeadlock {
val := d.waitForMap[waitForTxn]
if val == nil {
return nil
}
var nextVal *list.Element
for cur := val.txns.Front(); cur != nil; cur = nextVal {
nextVal = cur.Next()
keyHashPair := cur.Value.(*txnKeyHashPair)
// check if this edge is expired
if keyHashPair.isExpired(d.entryTTL, nowTime) {
val.txns.Remove(cur)
d.totalSize--
continue
}
if keyHashPair.txn == sourceTxn {
return &ErrDeadlock{DeadlockKeyHash: keyHashPair.keyHash}
}
if err := d.doDetect(nowTime, sourceTxn, keyHashPair.txn); err != nil {
return err
}
}
if val.txns.Len() == 0 {
delete(d.waitForMap, waitForTxn)
}
return nil
}
func (d *Detector) register(sourceTxn, waitForTxn, keyHash uint64) {
val := d.waitForMap[sourceTxn]
pair := txnKeyHashPair{txn: waitForTxn, keyHash: keyHash, registerTime: time.Now()}
if val == nil {
newList := &txnList{txns: list.New()}
newList.txns.PushBack(&pair)
d.waitForMap[sourceTxn] = newList
d.totalSize++
return
}
for cur := val.txns.Front(); cur != nil; cur = cur.Next() {
valuePair := cur.Value.(*txnKeyHashPair)
if valuePair.txn == waitForTxn && valuePair.keyHash == keyHash {
return
}
}
val.txns.PushBack(&pair)
d.totalSize++
}
// CleanUp removes the wait for entry for the transaction.
func (d *Detector) CleanUp(txn uint64) {
d.lock.Lock()
if l, ok := d.waitForMap[txn]; ok {
d.totalSize -= uint64(l.txns.Len())
}
delete(d.waitForMap, txn)
d.lock.Unlock()
}
// CleanUpWaitFor removes a key in the wait for entry for the transaction.
func (d *Detector) CleanUpWaitFor(txn, waitForTxn, keyHash uint64) {
d.lock.Lock()
l := d.waitForMap[txn]
if l != nil {
var nextVal *list.Element
for cur := l.txns.Front(); cur != nil; cur = nextVal {
nextVal = cur.Next()
valuePair := cur.Value.(*txnKeyHashPair)
if valuePair.txn == waitForTxn && valuePair.keyHash == keyHash {
l.txns.Remove(cur)
d.totalSize--
break
}
}
if l.txns.Len() == 0 {
delete(d.waitForMap, txn)
}
}
d.lock.Unlock()
}
// activeExpire removes expired entries, should be called under d.lock protection
func (d *Detector) activeExpire(nowTime time.Time) {
if nowTime.Sub(d.lastActiveExpire) > d.expireInterval &&
d.totalSize >= d.urgentSize {
log.Info("detector will do activeExpire", zap.Uint64("size", d.totalSize))
for txn, l := range d.waitForMap {
var nextVal *list.Element
for cur := l.txns.Front(); cur != nil; cur = nextVal {
nextVal = cur.Next()
valuePair := cur.Value.(*txnKeyHashPair)
if valuePair.isExpired(d.entryTTL, nowTime) {
l.txns.Remove(cur)
d.totalSize--
}
}
if l.txns.Len() == 0 {
delete(d.waitForMap, txn)
}
}
d.lastActiveExpire = nowTime
log.Info("detector activeExpire finished", zap.Uint64("size", d.totalSize))
}
}