-
Notifications
You must be signed in to change notification settings - Fork 5.9k
/
store_test.go
220 lines (183 loc) · 5.14 KB
/
store_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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
// 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.
package tikv
import (
"sync"
"time"
"github.com/juju/errors"
. "github.com/pingcap/check"
pb "github.com/pingcap/kvproto/pkg/kvrpcpb"
"github.com/pingcap/kvproto/pkg/metapb"
"github.com/pingcap/pd/pd-client"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/store/mockoracle"
"github.com/pingcap/tidb/store/tikv/tikvrpc"
"golang.org/x/net/context"
)
var errStopped = errors.New("stopped")
type testStoreSuite struct {
OneByOneSuite
store *tikvStore
}
var _ = Suite(&testStoreSuite{})
func (s *testStoreSuite) SetUpTest(c *C) {
s.store = NewTestStore(c).(*tikvStore)
}
func (s *testStoreSuite) TearDownTest(c *C) {
c.Assert(s.store.Close(), IsNil)
}
func (s *testStoreSuite) TestParsePath(c *C) {
etcdAddrs, disableGC, err := parsePath("tikv://node1:2379,node2:2379")
c.Assert(err, IsNil)
c.Assert(etcdAddrs, DeepEquals, []string{"node1:2379", "node2:2379"})
c.Assert(disableGC, IsFalse)
_, _, err = parsePath("tikv://node1:2379")
c.Assert(err, IsNil)
_, disableGC, err = parsePath("tikv://node1:2379?disableGC=true")
c.Assert(err, IsNil)
c.Assert(disableGC, IsTrue)
}
func (s *testStoreSuite) TestOracle(c *C) {
o := &mockoracle.MockOracle{}
s.store.oracle = o
ctx := context.Background()
t1, err := s.store.getTimestampWithRetry(NewBackoffer(ctx, 100))
c.Assert(err, IsNil)
t2, err := s.store.getTimestampWithRetry(NewBackoffer(ctx, 100))
c.Assert(err, IsNil)
c.Assert(t1, Less, t2)
// Check retry.
var wg sync.WaitGroup
wg.Add(2)
o.Disable()
go func() {
defer wg.Done()
time.Sleep(time.Millisecond * 100)
o.Enable()
}()
go func() {
defer wg.Done()
t3, err := s.store.getTimestampWithRetry(NewBackoffer(ctx, tsoMaxBackoff))
c.Assert(err, IsNil)
c.Assert(t2, Less, t3)
expired := s.store.oracle.IsExpired(t2, 50)
c.Assert(expired, IsTrue)
}()
wg.Wait()
}
type mockPDClient struct {
sync.RWMutex
client pd.Client
stop bool
}
func (c *mockPDClient) enable() {
c.Lock()
defer c.Unlock()
c.stop = false
}
func (c *mockPDClient) disable() {
c.Lock()
defer c.Unlock()
c.stop = true
}
func (c *mockPDClient) GetClusterID(context.Context) uint64 {
return 1
}
func (c *mockPDClient) GetTS(ctx context.Context) (int64, int64, error) {
c.RLock()
defer c.RUnlock()
if c.stop {
return 0, 0, errors.Trace(errStopped)
}
return c.client.GetTS(ctx)
}
func (c *mockPDClient) GetTSAsync(ctx context.Context) pd.TSFuture {
return nil
}
func (c *mockPDClient) GetRegion(ctx context.Context, key []byte) (*metapb.Region, *metapb.Peer, error) {
c.RLock()
defer c.RUnlock()
if c.stop {
return nil, nil, errors.Trace(errStopped)
}
return c.client.GetRegion(ctx, key)
}
func (c *mockPDClient) GetRegionByID(ctx context.Context, regionID uint64) (*metapb.Region, *metapb.Peer, error) {
c.RLock()
defer c.RUnlock()
if c.stop {
return nil, nil, errors.Trace(errStopped)
}
return c.client.GetRegionByID(ctx, regionID)
}
func (c *mockPDClient) GetStore(ctx context.Context, storeID uint64) (*metapb.Store, error) {
c.RLock()
defer c.RUnlock()
if c.stop {
return nil, errors.Trace(errStopped)
}
return c.client.GetStore(ctx, storeID)
}
func (c *mockPDClient) Close() {}
type checkRequestClient struct {
Client
priority pb.CommandPri
}
func (c *checkRequestClient) SendRequest(ctx context.Context, addr string, req *tikvrpc.Request, timeout time.Duration) (*tikvrpc.Response, error) {
resp, err := c.Client.SendRequest(ctx, addr, req, timeout)
if c.priority != req.Priority {
if resp.Get != nil {
resp.Get.Error = &pb.KeyError{
Abort: "request check error",
}
}
}
return resp, err
}
func (s *testStoreSuite) TestRequestPriority(c *C) {
client := &checkRequestClient{
Client: s.store.client,
}
s.store.client = client
// Cover 2PC commit.
txn, err := s.store.Begin()
c.Assert(err, IsNil)
client.priority = pb.CommandPri_High
txn.SetOption(kv.Priority, kv.PriorityHigh)
err = txn.Set([]byte("key"), []byte("value"))
c.Assert(err, IsNil)
err = txn.Commit(context.Background())
c.Assert(err, IsNil)
// Cover the basic Get request.
txn, err = s.store.Begin()
c.Assert(err, IsNil)
client.priority = pb.CommandPri_Low
txn.SetOption(kv.Priority, kv.PriorityLow)
_, err = txn.Get([]byte("key"))
c.Assert(err, IsNil)
// A counter example.
client.priority = pb.CommandPri_Low
txn.SetOption(kv.Priority, kv.PriorityNormal)
_, err = txn.Get([]byte("key"))
// err is translated to "try again later" by backoffer, so doesn't check error value here.
c.Assert(err, NotNil)
// Cover Seek request.
client.priority = pb.CommandPri_High
txn.SetOption(kv.Priority, kv.PriorityHigh)
iter, err := txn.Seek([]byte("key"))
c.Assert(err, IsNil)
for iter.Valid() {
c.Assert(iter.Next(), IsNil)
}
iter.Close()
}