-
Notifications
You must be signed in to change notification settings - Fork 5.9k
/
isolation_test.go
196 lines (168 loc) · 4.26 KB
/
isolation_test.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
194
195
196
// Copyright 2016 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.
// +build !race
package tikv
import (
"fmt"
"sort"
"sync"
"time"
. "github.com/pingcap/check"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/terror"
"golang.org/x/net/context"
)
// The test suite takes too long under the race detector.
type testIsolationSuite struct {
OneByOneSuite
store *tikvStore
}
var _ = Suite(&testIsolationSuite{})
func (s *testIsolationSuite) SetUpSuite(c *C) {
s.OneByOneSuite.SetUpSuite(c)
s.store = NewTestStore(c).(*tikvStore)
}
func (s *testIsolationSuite) TearDownSuite(c *C) {
s.store.Close()
s.OneByOneSuite.TearDownSuite(c)
}
type writeRecord struct {
startTS uint64
commitTS uint64
}
type writeRecords []writeRecord
func (r writeRecords) Len() int { return len(r) }
func (r writeRecords) Swap(i, j int) { r[i], r[j] = r[j], r[i] }
func (r writeRecords) Less(i, j int) bool { return r[i].startTS <= r[j].startTS }
func (s *testIsolationSuite) SetWithRetry(c *C, k, v []byte) writeRecord {
for {
txn, err := s.store.Begin()
c.Assert(err, IsNil)
err = txn.Set(k, v)
c.Assert(err, IsNil)
err = txn.Commit(context.Background())
if err == nil {
return writeRecord{
startTS: txn.StartTS(),
commitTS: txn.(*tikvTxn).commitTS,
}
}
c.Assert(kv.IsRetryableError(err) || terror.ErrorEqual(err, terror.ErrResultUndetermined), IsTrue)
}
}
type readRecord struct {
startTS uint64
value []byte
}
type readRecords []readRecord
func (r readRecords) Len() int { return len(r) }
func (r readRecords) Swap(i, j int) { r[i], r[j] = r[j], r[i] }
func (r readRecords) Less(i, j int) bool { return r[i].startTS <= r[j].startTS }
func (s *testIsolationSuite) GetWithRetry(c *C, k []byte) readRecord {
for {
txn, err := s.store.Begin()
c.Assert(err, IsNil)
val, err := txn.Get(k)
if err == nil {
return readRecord{
startTS: txn.StartTS(),
value: val,
}
}
c.Assert(kv.IsRetryableError(err), IsTrue)
}
}
func (s *testIsolationSuite) TestWriteWriteConflict(c *C) {
const (
threadCount = 10
setPerThread = 100
)
var (
mu sync.Mutex
writes []writeRecord
wg sync.WaitGroup
)
wg.Add(threadCount)
for i := 0; i < threadCount; i++ {
go func() {
defer wg.Done()
for j := 0; j < setPerThread; j++ {
w := s.SetWithRetry(c, []byte("k"), []byte("v"))
mu.Lock()
writes = append(writes, w)
mu.Unlock()
}
}()
}
wg.Wait()
// Check all transactions' [startTS, commitTS] are not overlapped.
sort.Sort(writeRecords(writes))
for i := 0; i < len(writes)-1; i++ {
c.Assert(writes[i].commitTS, Less, writes[i+1].startTS)
}
}
func (s *testIsolationSuite) TestReadWriteConflict(c *C) {
const (
readThreadCount = 10
writeCount = 10
)
var (
writes []writeRecord
mu sync.Mutex
reads []readRecord
wg sync.WaitGroup
)
s.SetWithRetry(c, []byte("k"), []byte("0"))
writeDone := make(chan struct{})
go func() {
for i := 1; i <= writeCount; i++ {
w := s.SetWithRetry(c, []byte("k"), []byte(fmt.Sprintf("%d", i)))
writes = append(writes, w)
time.Sleep(time.Microsecond * 10)
}
close(writeDone)
}()
wg.Add(readThreadCount)
for i := 0; i < readThreadCount; i++ {
go func() {
defer wg.Done()
for {
select {
case <-writeDone:
return
default:
}
r := s.GetWithRetry(c, []byte("k"))
mu.Lock()
reads = append(reads, r)
mu.Unlock()
}
}()
}
wg.Wait()
sort.Sort(readRecords(reads))
// Check all reads got the value committed before it's startTS.
var i, j int
for ; i < len(writes); i++ {
for ; j < len(reads); j++ {
w, r := writes[i], reads[j]
if r.startTS >= w.commitTS {
break
}
c.Assert(string(r.value), Equals, fmt.Sprintf("%d", i))
}
}
for ; j < len(reads); j++ {
c.Assert(string(reads[j].value), Equals, fmt.Sprintf("%d", len(writes)))
}
}