-
Notifications
You must be signed in to change notification settings - Fork 5.9k
/
txn.go
357 lines (311 loc) · 9.73 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
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
// Copyright 2018 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 session
import (
"github.com/juju/errors"
"github.com/pingcap/tidb/table"
"runtime/debug"
"github.com/opentracing/opentracing-go"
"github.com/pingcap/tidb/executor"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/sessionctx"
"github.com/pingcap/tidb/sessionctx/binloginfo"
"github.com/pingcap/tidb/store/tikv/oracle"
"github.com/pingcap/tidb/types"
binlog "github.com/pingcap/tipb/go-binlog"
log "github.com/sirupsen/logrus"
"golang.org/x/net/context"
)
// TxnState wraps kv.Transaction to provide a new kv.Transaction.
// 1. It holds all statement related modification in the buffer before flush to the txn,
// so if execute statement meets error, the txn won't be made dirty.
// 2. It's a lazy transaction, that means it's a txnFuture before StartTS() is really need.
type TxnState struct {
// States of a TxnState should be one of the followings:
// Invalid: kv.Transaction == nil && txnFuture == nil
// Pending: kv.Transaction == nil && txnFuture != nil
// Valid: kv.Transaction != nil && txnFuture == nil
kv.Transaction
txnFuture *txnFuture
buf kv.MemBuffer
mutations map[int64]*binlog.TableMutation
dirtyTableOP []dirtyTableOperation
// If doNotCommit is not nil, Commit() will not commit the transaction.
// doNotCommit flag may be set when StmtCommit fail.
doNotCommit error
}
func (st *TxnState) init() {
st.buf = kv.NewMemDbBuffer(kv.DefaultTxnMembufCap)
st.mutations = make(map[int64]*binlog.TableMutation)
}
// Valid implements the kv.Transaction interface.
func (st *TxnState) Valid() bool {
return st.Transaction != nil && st.Transaction.Valid()
}
func (st *TxnState) pending() bool {
return st.Transaction == nil && st.txnFuture != nil
}
func (st *TxnState) validOrPending() bool {
return st.txnFuture != nil || st.Valid()
}
func (st *TxnState) String() string {
if st.Transaction != nil {
return st.Transaction.String()
}
if st.txnFuture != nil {
return "txnFuture"
}
return "invalid transaction"
}
func (st *TxnState) changeInvalidToValid(txn kv.Transaction) {
st.Transaction = txn
st.txnFuture = nil
}
func (st *TxnState) changeInvalidToPending(future *txnFuture) {
st.Transaction = nil
st.txnFuture = future
}
func (st *TxnState) changePendingToValid(txnCap int) error {
if st.txnFuture == nil {
return errors.New("transaction future is not set")
}
future := st.txnFuture
st.txnFuture = nil
txn, err := future.wait()
if err != nil {
st.Transaction = nil
return errors.Trace(err)
}
txn.SetCap(txnCap)
st.Transaction = txn
return nil
}
func (st *TxnState) changeToInvalid() {
st.Transaction = nil
st.txnFuture = nil
}
// dirtyTableOperation represents an operation to dirtyTable, we log the operation
// first and apply the operation log when statement commit.
type dirtyTableOperation struct {
kind int
tid int64
handle int64
row []types.Datum
}
// Commit overrides the Transaction interface.
func (st *TxnState) Commit(ctx context.Context) error {
defer st.reset()
if len(st.mutations) != 0 || len(st.dirtyTableOP) != 0 || st.buf.Len() != 0 {
log.Errorf("The code should never run here, TxnState=%#v, mutations=%#v, dirtyTableOP=%#v, buf=%#v something must be wrong: %s",
st,
st.mutations,
st.dirtyTableOP,
st.buf,
debug.Stack())
return errors.New("invalid transaction")
}
if st.doNotCommit != nil {
if err1 := st.Transaction.Rollback(); err1 != nil {
log.Error(err1)
}
return errors.Trace(st.doNotCommit)
}
return errors.Trace(st.Transaction.Commit(ctx))
}
// Rollback overrides the Transaction interface.
func (st *TxnState) Rollback() error {
defer st.reset()
return errors.Trace(st.Transaction.Rollback())
}
func (st *TxnState) reset() {
st.doNotCommit = nil
st.cleanup()
st.changeToInvalid()
}
// Get overrides the Transaction interface.
func (st *TxnState) Get(k kv.Key) ([]byte, error) {
val, err := st.buf.Get(k)
if kv.IsErrNotFound(err) {
val, err = st.Transaction.Get(k)
}
if err != nil {
return nil, errors.Trace(err)
}
if len(val) == 0 {
return nil, kv.ErrNotExist
}
return val, nil
}
// Set overrides the Transaction interface.
func (st *TxnState) Set(k kv.Key, v []byte) error {
return st.buf.Set(k, v)
}
// Delete overrides the Transaction interface.
func (st *TxnState) Delete(k kv.Key) error {
return st.buf.Delete(k)
}
// Iter overrides the Transaction interface.
func (st *TxnState) Iter(k kv.Key, upperBound kv.Key) (kv.Iterator, error) {
bufferIt, err := st.buf.Iter(k, upperBound)
if err != nil {
return nil, errors.Trace(err)
}
retrieverIt, err := st.Transaction.Iter(k, upperBound)
if err != nil {
return nil, errors.Trace(err)
}
return kv.NewUnionIter(bufferIt, retrieverIt, false)
}
// IterReverse overrides the Transaction interface.
func (st *TxnState) IterReverse(k kv.Key) (kv.Iterator, error) {
bufferIt, err := st.buf.IterReverse(k)
if err != nil {
return nil, errors.Trace(err)
}
retrieverIt, err := st.Transaction.IterReverse(k)
if err != nil {
return nil, errors.Trace(err)
}
return kv.NewUnionIter(bufferIt, retrieverIt, true)
}
func (st *TxnState) cleanup() {
st.buf.Reset()
for key := range st.mutations {
delete(st.mutations, key)
}
if st.dirtyTableOP != nil {
empty := dirtyTableOperation{}
for i := 0; i < len(st.dirtyTableOP); i++ {
st.dirtyTableOP[i] = empty
}
st.dirtyTableOP = st.dirtyTableOP[:0]
}
}
func getBinlogMutation(ctx sessionctx.Context, tableID int64) *binlog.TableMutation {
bin := binloginfo.GetPrewriteValue(ctx, true)
for i := range bin.Mutations {
if bin.Mutations[i].TableId == tableID {
return &bin.Mutations[i]
}
}
idx := len(bin.Mutations)
bin.Mutations = append(bin.Mutations, binlog.TableMutation{TableId: tableID})
return &bin.Mutations[idx]
}
func mergeToMutation(m1, m2 *binlog.TableMutation) {
m1.InsertedRows = append(m1.InsertedRows, m2.InsertedRows...)
m1.UpdatedRows = append(m1.UpdatedRows, m2.UpdatedRows...)
m1.DeletedIds = append(m1.DeletedIds, m2.DeletedIds...)
m1.DeletedPks = append(m1.DeletedPks, m2.DeletedPks...)
m1.DeletedRows = append(m1.DeletedRows, m2.DeletedRows...)
m1.Sequence = append(m1.Sequence, m2.Sequence...)
}
func mergeToDirtyDB(dirtyDB *executor.DirtyDB, op dirtyTableOperation) {
switch op.kind {
case table.DirtyTableAddRow:
dirtyDB.AddRow(op.tid, op.handle, op.row)
case table.DirtyTableDeleteRow:
dirtyDB.DeleteRow(op.tid, op.handle)
case table.DirtyTableTruncate:
dirtyDB.TruncateTable(op.tid)
}
}
// txnFuture is a promise, which promises to return a txn in future.
type txnFuture struct {
future oracle.Future
store kv.Storage
span opentracing.Span
mockFail bool
}
var mockGetTSErrorInRetryOnce = true
func (tf *txnFuture) wait() (kv.Transaction, error) {
if tf.mockFail {
return nil, errors.New("mock get timestamp fail")
}
// mockGetTSErrorInRetry should wait mockCommitErrorOnce first, then will run into retry() logic.
// Then mockGetTSErrorInRetry will return retryable error when first retry.
// Before PR #8743, we don't cleanup txn after meet error such as error like: PD server timeout[try again later]
// This may cause duplicate data to be written.
// gofail: var mockGetTSErrorInRetry bool
//if mockGetTSErrorInRetry && mockGetTSErrorInRetryOnce && !mockCommitErrorOnce {
// mockGetTSErrorInRetryOnce = false
// return nil, errors.Errorf("PD server timeout[try again later]")
//}
startTS, err := tf.future.Wait()
tf.span.Finish()
if err == nil {
return tf.store.BeginWithStartTS(startTS)
}
// It would retry get timestamp.
return tf.store.Begin()
}
func (s *session) getTxnFuture(ctx context.Context) *txnFuture {
span, ctx := opentracing.StartSpanFromContext(ctx, "session.getTxnFuture")
oracleStore := s.store.GetOracle()
tsFuture := oracleStore.GetTimestampAsync(ctx)
ret := &txnFuture{future: tsFuture, store: s.store, span: span}
if x := ctx.Value("mockGetTSFail"); x != nil {
ret.mockFail = true
}
return ret
}
// StmtCommit implements the sessionctx.Context interface.
func (s *session) StmtCommit() error {
defer s.txn.cleanup()
st := &s.txn
var count int
err := kv.WalkMemBuffer(st.buf, func(k kv.Key, v []byte) error {
// gofail: var mockStmtCommitError bool
// if mockStmtCommitError {
// count++
// }
if count > 3 {
return errors.New("mock stmt commit error")
}
if len(v) == 0 {
return errors.Trace(st.Transaction.Delete(k))
}
return errors.Trace(st.Transaction.Set(k, v))
})
if err != nil {
st.doNotCommit = err
return errors.Trace(err)
}
// Need to flush binlog.
for tableID, delta := range st.mutations {
mutation := getBinlogMutation(s, tableID)
mergeToMutation(mutation, delta)
}
if len(st.dirtyTableOP) > 0 {
dirtyDB := executor.GetDirtyDB(s)
for _, op := range st.dirtyTableOP {
mergeToDirtyDB(dirtyDB, op)
}
}
return nil
}
// StmtRollback implements the sessionctx.Context interface.
func (s *session) StmtRollback() {
s.txn.cleanup()
return
}
// StmtGetMutation implements the sessionctx.Context interface.
func (s *session) StmtGetMutation(tableID int64) *binlog.TableMutation {
st := &s.txn
if _, ok := st.mutations[tableID]; !ok {
st.mutations[tableID] = &binlog.TableMutation{TableId: tableID}
}
return st.mutations[tableID]
}
func (s *session) StmtAddDirtyTableOP(op int, tid int64, handle int64, row []types.Datum) {
s.txn.dirtyTableOP = append(s.txn.dirtyTableOP, dirtyTableOperation{op, tid, handle, row})
}