forked from pingcap/tidb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kv.go
436 lines (380 loc) · 9.33 KB
/
kv.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
// Copyright 2015 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 localstore
import (
"net/url"
"path/filepath"
"runtime/debug"
"sync"
"time"
log "github.com/Sirupsen/logrus"
"github.com/juju/errors"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/store/localstore/engine"
"github.com/pingcap/tidb/store/tikv/oracle"
"github.com/pingcap/tidb/store/tikv/oracle/oracles"
"github.com/pingcap/tidb/util/segmentmap"
"github.com/twinj/uuid"
)
var (
_ kv.Storage = (*dbStore)(nil)
)
const (
lowerWaterMark = 10 // second
)
func (s *dbStore) prepareSeek(startTS uint64) error {
for {
var conflict bool
s.mu.RLock()
if s.closed {
s.mu.RUnlock()
return ErrDBClosed
}
if s.committingTS != 0 && s.committingTS < startTS {
// We not sure if we can read the committing value,
conflict = true
} else {
s.wg.Add(1)
}
s.mu.RUnlock()
if conflict {
// Wait for committing to be finished and try again.
time.Sleep(time.Microsecond)
continue
}
return nil
}
}
// Seek searches for the first key in the engine which is >= key in byte order, returns (nil, nil, ErrNotFound)
// if such key is not found.
func (s *dbStore) Seek(key []byte, startTS uint64) ([]byte, []byte, error) {
err := s.prepareSeek(startTS)
if err != nil {
return nil, nil, errors.Trace(err)
}
key, val, err := s.db.Seek(key)
s.wg.Done()
return key, val, err
}
// SeekReverse searches for the first key in the engine which is less than key in byte order.
// Returns (nil, nil, ErrNotFound) if such key is not found.
func (s *dbStore) SeekReverse(key []byte, startTS uint64) ([]byte, []byte, error) {
err := s.prepareSeek(startTS)
if err != nil {
return nil, nil, errors.Trace(err)
}
key, val, err := s.db.SeekReverse(key)
s.wg.Done()
return key, val, err
}
// Commit writes the changed data in Batch.
func (s *dbStore) CommitTxn(txn *dbTxn) error {
if len(txn.lockedKeys) == 0 {
return nil
}
return s.doCommit(txn)
}
func (s *dbStore) cleanRecentUpdates(segmentIndex int64) {
m, err := s.recentUpdates.GetSegment(segmentIndex)
if err != nil {
log.Error(err)
return
}
now := time.Now().Unix()
for k, v := range m {
dis := now - version2Second(v.(kv.Version))
if dis > lowerWaterMark {
delete(m, k)
}
}
}
func (s *dbStore) tryLock(txn *dbTxn) (err error) {
// check conflict
for k := range txn.lockedKeys {
if _, ok := s.keysLocked[k]; ok {
return errors.Trace(kv.ErrLockConflict)
}
lastVer, ok := s.recentUpdates.Get([]byte(k))
if !ok {
continue
}
// If there's newer version of this key, returns error.
if lastVer.(kv.Version).Cmp(kv.Version{Ver: txn.tid}) > 0 {
return errors.Trace(kv.ErrConditionNotMatch)
}
}
// record
for k := range txn.lockedKeys {
s.keysLocked[k] = txn.tid
}
return nil
}
func (s *dbStore) doCommit(txn *dbTxn) error {
var commitVer kv.Version
var err error
for {
// Atomically get commit version
s.mu.Lock()
closed := s.closed
committing := s.committingTS != 0
if !closed && !committing {
commitVer, err = globalVersionProvider.CurrentVersion()
if err != nil {
s.mu.Unlock()
return errors.Trace(err)
}
s.committingTS = commitVer.Ver
s.wg.Add(1)
}
s.mu.Unlock()
if closed {
return ErrDBClosed
}
if committing {
time.Sleep(time.Microsecond)
continue
}
break
}
defer func() {
s.mu.Lock()
s.committingTS = 0
s.wg.Done()
s.mu.Unlock()
}()
// Here we are sure no concurrent committing happens.
err = s.tryLock(txn)
if err != nil {
return errors.Trace(err)
}
b := s.db.NewBatch()
err = txn.us.WalkBuffer(func(k kv.Key, value []byte) error {
mvccKey := MvccEncodeVersionKey(k, commitVer)
if len(value) == 0 { // Deleted marker
b.Put(mvccKey, nil)
s.compactor.OnDelete(k)
} else {
b.Put(mvccKey, value)
s.compactor.OnSet(k)
}
return nil
})
if err != nil {
return errors.Trace(err)
}
err = s.writeBatch(b)
if err != nil {
return errors.Trace(err)
}
// Update commit version.
txn.version = commitVer
err = s.unLockKeys(txn)
if err != nil {
return errors.Trace(err)
}
// Clean recent updates.
now := time.Now()
if now.Sub(s.lastCleanTime) > time.Second {
s.cleanRecentUpdates(s.cleanIdx)
s.cleanIdx++
if s.cleanIdx == s.recentUpdates.SegmentCount() {
s.cleanIdx = 0
}
s.lastCleanTime = now
}
return nil
}
func (s *dbStore) NewBatch() engine.Batch {
return s.db.NewBatch()
}
type dbStore struct {
db engine.DB
txns map[uint64]*dbTxn
keysLocked map[string]uint64
recentUpdates *segmentmap.SegmentMap
cleanIdx int64
lastCleanTime time.Time
uuid string
path string
compactor *localstoreCompactor
wg sync.WaitGroup
mu sync.RWMutex
closed bool
committingTS uint64
pd localPD
oracle oracle.Oracle
}
type storeCache struct {
mu sync.Mutex
cache map[string]*dbStore
}
var (
globalVersionProvider kv.VersionProvider
mc storeCache
// ErrDBClosed is the error meaning db is closed and we can use it anymore.
ErrDBClosed = errors.New("db is closed")
)
func init() {
mc.cache = make(map[string]*dbStore)
globalVersionProvider = &LocalVersionProvider{}
}
// Driver implements kv.Driver interface.
type Driver struct {
// engine.Driver is the engine driver for different local db engine.
engine.Driver
}
// MockRemoteStore mocks remote store. It makes IsLocalStore return false.
var MockRemoteStore bool
// IsLocalStore checks whether a storage is local or not.
func IsLocalStore(s kv.Storage) bool {
if MockRemoteStore {
return false
}
_, ok := s.(*dbStore)
return ok
}
// Open opens or creates a storage with specific format for a local engine Driver.
// The path should be a URL format which is described in tidb package.
func (d Driver) Open(path string) (kv.Storage, error) {
mc.mu.Lock()
defer mc.mu.Unlock()
u, err := url.Parse(path)
if err != nil {
return nil, errors.Trace(err)
}
engineSchema := filepath.Join(u.Host, u.Path)
if store, ok := mc.cache[engineSchema]; ok {
// TODO: check the cache store has the same engine with this Driver.
log.Info("[kv] cache store", engineSchema)
return store, nil
}
db, err := d.Driver.Open(engineSchema)
if err != nil {
return nil, errors.Trace(err)
}
log.Info("[kv] New store", engineSchema)
s := &dbStore{
txns: make(map[uint64]*dbTxn),
keysLocked: make(map[string]uint64),
uuid: uuid.NewV4().String(),
path: engineSchema,
db: db,
compactor: newLocalCompactor(localCompactDefaultPolicy, db),
closed: false,
oracle: oracles.NewLocalOracle(),
}
s.recentUpdates, err = segmentmap.NewSegmentMap(100)
if err != nil {
return nil, errors.Trace(err)
}
regionServers := buildLocalRegionServers(s)
var infos []*regionInfo
for _, rs := range regionServers {
ri := ®ionInfo{startKey: rs.startKey, endKey: rs.endKey, rs: rs}
infos = append(infos, ri)
}
s.pd.SetRegionInfo(infos)
mc.cache[engineSchema] = s
s.compactor.Start()
return s, nil
}
func (s *dbStore) UUID() string {
return s.uuid
}
func (s *dbStore) GetSnapshot(ver kv.Version) (kv.Snapshot, error) {
s.mu.RLock()
if s.closed {
s.mu.RUnlock()
return nil, ErrDBClosed
}
s.mu.RUnlock()
currentVer, err := globalVersionProvider.CurrentVersion()
if err != nil {
return nil, errors.Trace(err)
}
if ver.Cmp(currentVer) > 0 {
ver = currentVer
}
return &dbSnapshot{
store: s,
version: ver,
}, nil
}
func (s *dbStore) GetClient() kv.Client {
return &dbClient{store: s, regionInfo: s.pd.GetRegionInfo()}
}
func (s *dbStore) GetOracle() oracle.Oracle {
return s.oracle
}
func (s *dbStore) CurrentVersion() (kv.Version, error) {
return globalVersionProvider.CurrentVersion()
}
// Begin transaction
func (s *dbStore) Begin() (kv.Transaction, error) {
s.mu.RLock()
if s.closed {
s.mu.RUnlock()
return nil, ErrDBClosed
}
s.mu.RUnlock()
beginVer, err := globalVersionProvider.CurrentVersion()
if err != nil {
return nil, errors.Trace(err)
}
return newTxn(s, beginVer), nil
}
// BeginWithStartTS begins transaction with startTS.
func (s *dbStore) BeginWithStartTS(startTS uint64) (kv.Transaction, error) {
return s.Begin()
}
func (s *dbStore) Close() error {
s.mu.Lock()
if s.closed {
s.mu.Unlock()
return ErrDBClosed
}
s.closed = true
s.mu.Unlock()
s.compactor.Stop()
s.wg.Wait()
delete(mc.cache, s.path)
return s.db.Close()
}
func (s *dbStore) writeBatch(b engine.Batch) error {
if b.Len() == 0 {
return nil
}
err := s.db.Commit(b)
if err != nil {
log.Error(err)
return errors.Trace(err)
}
return nil
}
func (s *dbStore) SupportDeleteRange() (supported bool) {
return false
}
func (s *dbStore) newBatch() engine.Batch {
return s.db.NewBatch()
}
func (s *dbStore) unLockKeys(txn *dbTxn) error {
for k := range txn.lockedKeys {
if tid, ok := s.keysLocked[k]; !ok || tid != txn.tid {
debug.PrintStack()
return errors.Errorf("should never happened:%v, %v", tid, txn.tid)
}
delete(s.keysLocked, k)
s.recentUpdates.Set([]byte(k), txn.version, true)
}
return nil
}