forked from tikv/client-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
txn.go
303 lines (266 loc) · 8.54 KB
/
txn.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
// 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.
package txnkv
import (
"context"
"fmt"
"time"
"github.com/pingcap/kvproto/pkg/kvrpcpb"
"github.com/pkg/errors"
"github.com/prometheus/common/log"
"github.com/tikv/client-go/key"
"github.com/tikv/client-go/metrics"
"github.com/tikv/client-go/txnkv/kv"
"github.com/tikv/client-go/txnkv/store"
)
// Transaction is a key-value transaction.
type Transaction struct {
tikvStore *store.TiKVStore
snapshot *store.TiKVSnapshot
us kv.UnionStore
startTS uint64
startTime time.Time // Monotonic timestamp for recording txn time consuming.
commitTS uint64
valid bool
lockKeys [][]byte
}
func newTransaction(tikvStore *store.TiKVStore, ts uint64) *Transaction {
metrics.TxnCounter.Inc()
snapshot := tikvStore.GetSnapshot(ts)
us := kv.NewUnionStore(&tikvStore.GetConfig().Txn, snapshot)
return &Transaction{
tikvStore: tikvStore,
snapshot: snapshot,
us: us,
startTS: ts,
startTime: time.Now(),
valid: true,
}
}
// Get implements transaction interface.
// kv.IsErrNotFound can be used to check the error is a not found error.
func (txn *Transaction) Get(ctx context.Context, k key.Key) ([]byte, error) {
start := time.Now()
defer func() { metrics.TxnCmdHistogram.WithLabelValues("get").Observe(time.Since(start).Seconds()) }()
ret, err := txn.us.Get(ctx, k)
if err != nil {
return nil, err
}
err = txn.tikvStore.CheckVisibility(txn.startTS)
if err != nil {
return nil, err
}
return ret, nil
}
// BatchGet gets a batch of values from TiKV server.
func (txn *Transaction) BatchGet(ctx context.Context, keys []key.Key) (map[string][]byte, error) {
start := time.Now()
defer func() { metrics.TxnCmdHistogram.WithLabelValues("batch_get").Observe(time.Since(start).Seconds()) }()
if txn.IsReadOnly() {
return txn.snapshot.BatchGet(ctx, keys)
}
bufferValues := make([][]byte, len(keys))
shrinkKeys := make([]key.Key, 0, len(keys))
for i, key := range keys {
val, err := txn.us.GetMemBuffer().Get(ctx, key)
if kv.IsErrNotFound(err) {
shrinkKeys = append(shrinkKeys, key)
continue
}
if err != nil {
return nil, err
}
if len(val) != 0 {
bufferValues[i] = val
}
}
storageValues, err := txn.snapshot.BatchGet(ctx, shrinkKeys)
if err != nil {
return nil, err
}
for i, key := range keys {
if bufferValues[i] == nil {
continue
}
storageValues[string(key)] = bufferValues[i]
}
return storageValues, nil
}
// Set sets the value for key k as v into kv store.
func (txn *Transaction) Set(k key.Key, v []byte) error {
start := time.Now()
defer func() { metrics.TxnCmdHistogram.WithLabelValues("set").Observe(time.Since(start).Seconds()) }()
return txn.us.Set(k, v)
}
func (txn *Transaction) String() string {
return fmt.Sprintf("txn-%d", txn.startTS)
}
// Iter creates an Iterator positioned on the first entry that k <= entry's key.
// If such entry is not found, it returns an invalid Iterator with no error.
// It yields only keys that < upperBound. If upperBound is nil, it means the upperBound is unbounded.
// The Iterator must be closed after use.
func (txn *Transaction) Iter(ctx context.Context, k key.Key, upperBound key.Key) (kv.Iterator, error) {
start := time.Now()
defer func() { metrics.TxnCmdHistogram.WithLabelValues("iter").Observe(time.Since(start).Seconds()) }()
return txn.us.Iter(ctx, k, upperBound)
}
// IterReverse creates a reversed Iterator positioned on the first entry which key is less than k.
func (txn *Transaction) IterReverse(ctx context.Context, k key.Key) (kv.Iterator, error) {
start := time.Now()
defer func() { metrics.TxnCmdHistogram.WithLabelValues("iter_reverse").Observe(time.Since(start).Seconds()) }()
return txn.us.IterReverse(ctx, k)
}
// IsReadOnly returns if there are pending key-value to commit in the transaction.
func (txn *Transaction) IsReadOnly() bool {
return txn.us.GetMemBuffer().Len() == 0 && len(txn.lockKeys) == 0
}
// Delete removes the entry for key k from kv store.
func (txn *Transaction) Delete(k key.Key) error {
start := time.Now()
defer func() { metrics.TxnCmdHistogram.WithLabelValues("delete").Observe(time.Since(start).Seconds()) }()
return txn.us.Delete(k)
}
// SetOption sets an option with a value, when val is nil, uses the default
// value of this option.
func (txn *Transaction) SetOption(opt kv.Option, val interface{}) {
txn.us.SetOption(opt, val)
switch opt {
case kv.Priority:
txn.snapshot.SetPriority(val.(int))
case kv.NotFillCache:
txn.snapshot.NotFillCache = val.(bool)
case kv.SyncLog:
txn.snapshot.SyncLog = val.(bool)
case kv.KeyOnly:
txn.snapshot.KeyOnly = val.(bool)
}
}
// DelOption deletes an option.
func (txn *Transaction) DelOption(opt kv.Option) {
txn.us.DelOption(opt)
}
func (txn *Transaction) close() {
txn.valid = false
}
// Commit commits the transaction operations to KV store.
func (txn *Transaction) Commit(ctx context.Context) error {
if !txn.valid {
return kv.ErrInvalidTxn
}
defer txn.close()
// gofail: var mockCommitError bool
// if mockCommitError && kv.IsMockCommitErrorEnable() {
// kv.MockCommitErrorDisable()
// return errors.New("mock commit error")
// }
start := time.Now()
defer func() {
metrics.TxnCmdHistogram.WithLabelValues("commit").Observe(time.Since(start).Seconds())
metrics.TxnHistogram.Observe(time.Since(txn.startTime).Seconds())
}()
mutations := make(map[string]*kvrpcpb.Mutation)
err := txn.us.WalkBuffer(func(k key.Key, v []byte) error {
op := kvrpcpb.Op_Put
if c := txn.us.LookupConditionPair(k); c != nil && c.ShouldNotExist() {
op = kvrpcpb.Op_Insert
}
if len(v) == 0 {
op = kvrpcpb.Op_Del
}
mutations[string(k)] = &kvrpcpb.Mutation{
Op: op,
Key: k,
Value: v,
}
return nil
})
if err != nil {
return err
}
for _, lockKey := range txn.lockKeys {
if _, ok := mutations[string(lockKey)]; !ok {
mutations[string(lockKey)] = &kvrpcpb.Mutation{
Op: kvrpcpb.Op_Lock,
Key: lockKey,
}
}
}
if len(mutations) == 0 {
return nil
}
committer, err := store.NewTxnCommitter(txn.tikvStore, txn.startTS, txn.startTime, mutations)
if err != nil || committer == nil {
return err
}
// latches disabled
if txn.tikvStore.GetTxnLatches() == nil {
err = committer.Execute(ctx)
log.Debug("[kv]", txn.startTS, " txnLatches disabled, 2pc directly:", err)
return err
}
// latches enabled
// for transactions which need to acquire latches
start = time.Now()
lock := txn.tikvStore.GetTxnLatches().Lock(txn.startTS, committer.GetKeys())
localLatchTime := time.Since(start)
if localLatchTime > 0 {
metrics.LocalLatchWaitTimeHistogram.Observe(localLatchTime.Seconds())
}
defer txn.tikvStore.GetTxnLatches().UnLock(lock)
if lock.IsStale() {
err = errors.Errorf("startTS %d is stale", txn.startTS)
return errors.WithMessage(err, store.TxnRetryableMark)
}
err = committer.Execute(ctx)
if err == nil {
lock.SetCommitTS(committer.GetCommitTS())
}
log.Debug("[kv]", txn.startTS, " txnLatches enabled while txn retryable:", err)
return err
}
// Rollback undoes the transaction operations to KV store.
func (txn *Transaction) Rollback() error {
if !txn.valid {
return kv.ErrInvalidTxn
}
start := time.Now()
defer func() {
metrics.TxnCmdHistogram.WithLabelValues("rollback").Observe(time.Since(start).Seconds())
metrics.TxnHistogram.Observe(time.Since(txn.startTime).Seconds())
}()
txn.close()
log.Debugf("[kv] Rollback txn %d", txn.startTS)
return nil
}
// LockKeys tries to lock the entries with the keys in KV store.
func (txn *Transaction) LockKeys(keys ...key.Key) error {
start := time.Now()
defer func() { metrics.TxnCmdHistogram.WithLabelValues("lock_keys").Observe(time.Since(start).Seconds()) }()
for _, key := range keys {
txn.lockKeys = append(txn.lockKeys, key)
}
return nil
}
// Valid returns if the transaction is valid.
// A transaction becomes invalid after commit or rollback.
func (txn *Transaction) Valid() bool {
return txn.valid
}
// Len returns the count of key-value pairs in the transaction's memory buffer.
func (txn *Transaction) Len() int {
return txn.us.Len()
}
// Size returns the length (in bytes) of the transaction's memory buffer.
func (txn *Transaction) Size() int {
return txn.us.Size()
}