/
txn.go
201 lines (168 loc) · 4.78 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
// 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 (
"fmt"
"time"
"github.com/coreos/etcd/pkg/monotime"
"github.com/juju/errors"
"github.com/ngaut/log"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tipb/go-binlog"
goctx "golang.org/x/net/context"
)
var (
_ kv.Transaction = (*tikvTxn)(nil)
)
// tikvTxn implements kv.Transaction.
type tikvTxn struct {
us kv.UnionStore
store *tikvStore // for connection to region.
startTS uint64
startTime monotime.Time // Monotonic timestamp for recording txn time consuming.
commitTS uint64
valid bool
lockKeys [][]byte
dirty bool
}
func newTiKVTxn(store *tikvStore) (*tikvTxn, error) {
bo := NewBackoffer(tsoMaxBackoff, goctx.Background())
startTS, err := store.getTimestampWithRetry(bo)
if err != nil {
return nil, errors.Trace(err)
}
ver := kv.NewVersion(startTS)
return &tikvTxn{
us: kv.NewUnionStore(newTiKVSnapshot(store, ver)),
store: store,
startTS: startTS,
startTime: monotime.Now(),
valid: true,
}, nil
}
// newTikvTxnWithStartTS creates a txn with startTS.
func newTikvTxnWithStartTS(store *tikvStore, startTS uint64) (*tikvTxn, error) {
ver := kv.NewVersion(startTS)
return &tikvTxn{
us: kv.NewUnionStore(newTiKVSnapshot(store, ver)),
store: store,
startTS: startTS,
startTime: monotime.Now(),
valid: true,
}, nil
}
// Implement transaction interface.
func (txn *tikvTxn) Get(k kv.Key) ([]byte, error) {
txnCmdCounter.WithLabelValues("get").Inc()
start := time.Now()
defer func() { txnCmdHistogram.WithLabelValues("get").Observe(time.Since(start).Seconds()) }()
ret, err := txn.us.Get(k)
if err != nil {
return nil, errors.Trace(err)
}
return ret, nil
}
func (txn *tikvTxn) Set(k kv.Key, v []byte) error {
txnCmdCounter.WithLabelValues("set").Inc()
txn.dirty = true
return txn.us.Set(k, v)
}
func (txn *tikvTxn) String() string {
return fmt.Sprintf("%d", txn.StartTS())
}
func (txn *tikvTxn) Seek(k kv.Key) (kv.Iterator, error) {
txnCmdCounter.WithLabelValues("seek").Inc()
start := time.Now()
defer func() { txnCmdHistogram.WithLabelValues("seek").Observe(time.Since(start).Seconds()) }()
return txn.us.Seek(k)
}
// SeekReverse creates a reversed Iterator positioned on the first entry which key is less than k.
func (txn *tikvTxn) SeekReverse(k kv.Key) (kv.Iterator, error) {
txnCmdCounter.WithLabelValues("seek_reverse").Inc()
start := time.Now()
defer func() { txnCmdHistogram.WithLabelValues("seek_reverse").Observe(time.Since(start).Seconds()) }()
return txn.us.SeekReverse(k)
}
func (txn *tikvTxn) Delete(k kv.Key) error {
txnCmdCounter.WithLabelValues("delete").Inc()
txn.dirty = true
return txn.us.Delete(k)
}
func (txn *tikvTxn) SetOption(opt kv.Option, val interface{}) {
txn.us.SetOption(opt, val)
}
func (txn *tikvTxn) DelOption(opt kv.Option) {
txn.us.DelOption(opt)
}
func (txn *tikvTxn) Commit() error {
if !txn.valid {
return kv.ErrInvalidTxn
}
defer txn.close()
txnCmdCounter.WithLabelValues("commit").Inc()
start := time.Now()
defer func() { txnCmdHistogram.WithLabelValues("commit").Observe(time.Since(start).Seconds()) }()
if err := txn.us.CheckLazyConditionPairs(); err != nil {
return errors.Trace(err)
}
committer, err := newTwoPhaseCommitter(txn)
if err != nil {
return errors.Trace(err)
}
if committer == nil {
return nil
}
err = committer.execute()
if err != nil {
committer.writeFinishBinlog(binlog.BinlogType_Rollback, 0)
return errors.Trace(err)
}
committer.writeFinishBinlog(binlog.BinlogType_Commit, int64(committer.commitTS))
txn.commitTS = committer.commitTS
return nil
}
func (txn *tikvTxn) close() error {
txn.valid = false
return nil
}
func (txn *tikvTxn) Rollback() error {
if !txn.valid {
return kv.ErrInvalidTxn
}
txn.close()
log.Infof("[kv] Rollback txn %d", txn.StartTS())
txnCmdCounter.WithLabelValues("rollback").Inc()
return nil
}
func (txn *tikvTxn) LockKeys(keys ...kv.Key) error {
txnCmdCounter.WithLabelValues("lock_keys").Inc()
for _, key := range keys {
txn.lockKeys = append(txn.lockKeys, key)
}
return nil
}
func (txn *tikvTxn) IsReadOnly() bool {
return !txn.dirty
}
func (txn *tikvTxn) StartTS() uint64 {
return txn.startTS
}
func (txn *tikvTxn) Valid() bool {
return txn.valid
}
func (txn *tikvTxn) Len() int {
return txn.us.Len()
}
func (txn *tikvTxn) Size() int {
return txn.us.Size()
}