-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
tx_conn.go
270 lines (241 loc) · 8.39 KB
/
tx_conn.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
/*
Copyright 2017 Google 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,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package vtgate
import (
"sync"
"golang.org/x/net/context"
"vitess.io/vitess/go/vt/concurrency"
"vitess.io/vitess/go/vt/dtids"
"vitess.io/vitess/go/vt/log"
"vitess.io/vitess/go/vt/vterrors"
"vitess.io/vitess/go/vt/vtgate/gateway"
querypb "vitess.io/vitess/go/vt/proto/query"
vtgatepb "vitess.io/vitess/go/vt/proto/vtgate"
vtrpcpb "vitess.io/vitess/go/vt/proto/vtrpc"
)
// TxConn is used for executing transactional requests.
type TxConn struct {
gateway gateway.Gateway
mode vtgatepb.TransactionMode
}
// NewTxConn builds a new TxConn.
func NewTxConn(gw gateway.Gateway, txMode vtgatepb.TransactionMode) *TxConn {
return &TxConn{
gateway: gw,
mode: txMode,
}
}
// Begin begins a new transaction. If one is already in progress, it commmits it
// and starts a new one.
func (txc *TxConn) Begin(ctx context.Context, session *SafeSession) error {
if session.InTransaction() {
if err := txc.Commit(ctx, session); err != nil {
return err
}
}
// UNSPECIFIED & SINGLE mode are always allowed.
switch session.TransactionMode {
case vtgatepb.TransactionMode_MULTI:
if txc.mode == vtgatepb.TransactionMode_SINGLE {
return vterrors.Errorf(vtrpcpb.Code_FAILED_PRECONDITION, "requested transaction mode %v disallowed: vtgate must be started with --transaction_mode=MULTI (or TWOPC). Current transaction mode: %v", session.TransactionMode, txc.mode)
}
case vtgatepb.TransactionMode_TWOPC:
if txc.mode != vtgatepb.TransactionMode_TWOPC {
return vterrors.Errorf(vtrpcpb.Code_FAILED_PRECONDITION, "requested transaction mode %v disallowed: vtgate must be started with --transaction_mode=TWOPC. Current transaction mode: %v", session.TransactionMode, txc.mode)
}
}
session.Session.InTransaction = true
return nil
}
// Commit commits the current transaction. The type of commit can be
// best effort or 2pc depending on the session setting.
func (txc *TxConn) Commit(ctx context.Context, session *SafeSession) error {
defer session.Reset()
if !session.InTransaction() {
return nil
}
twopc := false
switch session.TransactionMode {
case vtgatepb.TransactionMode_TWOPC:
if txc.mode != vtgatepb.TransactionMode_TWOPC {
txc.Rollback(ctx, session)
return vterrors.New(vtrpcpb.Code_FAILED_PRECONDITION, "2pc transaction disallowed")
}
twopc = true
case vtgatepb.TransactionMode_UNSPECIFIED:
twopc = (txc.mode == vtgatepb.TransactionMode_TWOPC)
}
if twopc {
return txc.commit2PC(ctx, session)
}
return txc.commitNormal(ctx, session)
}
func (txc *TxConn) commitNormal(ctx context.Context, session *SafeSession) error {
var err error
committing := true
for _, shardSession := range session.ShardSessions {
if !committing {
txc.gateway.Rollback(ctx, shardSession.Target, shardSession.TransactionId)
continue
}
if err = txc.gateway.Commit(ctx, shardSession.Target, shardSession.TransactionId); err != nil {
committing = false
}
}
return err
}
func (txc *TxConn) commit2PC(ctx context.Context, session *SafeSession) error {
// If the number of participants is one or less, then it's a normal commit.
if len(session.ShardSessions) <= 1 {
return txc.commitNormal(ctx, session)
}
participants := make([]*querypb.Target, 0, len(session.ShardSessions)-1)
for _, s := range session.ShardSessions[1:] {
participants = append(participants, s.Target)
}
mmShard := session.ShardSessions[0]
dtid := dtids.New(mmShard)
err := txc.gateway.CreateTransaction(ctx, mmShard.Target, dtid, participants)
if err != nil {
// Normal rollback is safe because nothing was prepared yet.
txc.Rollback(ctx, session)
return err
}
err = txc.runSessions(session.ShardSessions[1:], func(s *vtgatepb.Session_ShardSession) error {
return txc.gateway.Prepare(ctx, s.Target, s.TransactionId, dtid)
})
if err != nil {
// TODO(sougou): Perform a more fine-grained cleanup
// including unprepared transactions.
if resumeErr := txc.Resolve(ctx, dtid); resumeErr != nil {
log.Warningf("Rollback failed after Prepare failure: %v", resumeErr)
}
// Return the original error even if the previous operation fails.
return err
}
err = txc.gateway.StartCommit(ctx, mmShard.Target, mmShard.TransactionId, dtid)
if err != nil {
return err
}
err = txc.runSessions(session.ShardSessions[1:], func(s *vtgatepb.Session_ShardSession) error {
return txc.gateway.CommitPrepared(ctx, s.Target, dtid)
})
if err != nil {
return err
}
return txc.gateway.ConcludeTransaction(ctx, mmShard.Target, dtid)
}
// Rollback rolls back the current transaction. There are no retries on this operation.
func (txc *TxConn) Rollback(ctx context.Context, session *SafeSession) error {
if !session.InTransaction() {
return nil
}
defer session.Reset()
return txc.runSessions(session.ShardSessions, func(s *vtgatepb.Session_ShardSession) error {
return txc.gateway.Rollback(ctx, s.Target, s.TransactionId)
})
}
// Resolve resolves the specified 2PC transaction.
func (txc *TxConn) Resolve(ctx context.Context, dtid string) error {
mmShard, err := dtids.ShardSession(dtid)
if err != nil {
return err
}
transaction, err := txc.gateway.ReadTransaction(ctx, mmShard.Target, dtid)
if err != nil {
return err
}
if transaction == nil || transaction.Dtid == "" {
// It was already resolved.
return nil
}
switch transaction.State {
case querypb.TransactionState_PREPARE:
// If state is PREPARE, make a decision to rollback and
// fallthrough to the rollback workflow.
if err := txc.gateway.SetRollback(ctx, mmShard.Target, transaction.Dtid, mmShard.TransactionId); err != nil {
return err
}
fallthrough
case querypb.TransactionState_ROLLBACK:
if err := txc.resumeRollback(ctx, mmShard.Target, transaction); err != nil {
return err
}
case querypb.TransactionState_COMMIT:
if err := txc.resumeCommit(ctx, mmShard.Target, transaction); err != nil {
return err
}
default:
// Should never happen.
return vterrors.Errorf(vtrpcpb.Code_INTERNAL, "invalid state: %v", transaction.State)
}
return nil
}
func (txc *TxConn) resumeRollback(ctx context.Context, target *querypb.Target, transaction *querypb.TransactionMetadata) error {
err := txc.runTargets(transaction.Participants, func(t *querypb.Target) error {
return txc.gateway.RollbackPrepared(ctx, t, transaction.Dtid, 0)
})
if err != nil {
return err
}
return txc.gateway.ConcludeTransaction(ctx, target, transaction.Dtid)
}
func (txc *TxConn) resumeCommit(ctx context.Context, target *querypb.Target, transaction *querypb.TransactionMetadata) error {
err := txc.runTargets(transaction.Participants, func(t *querypb.Target) error {
return txc.gateway.CommitPrepared(ctx, t, transaction.Dtid)
})
if err != nil {
return err
}
return txc.gateway.ConcludeTransaction(ctx, target, transaction.Dtid)
}
// runSessions executes the action for all shardSessions in parallel and returns a consolildated error.
func (txc *TxConn) runSessions(shardSessions []*vtgatepb.Session_ShardSession, action func(*vtgatepb.Session_ShardSession) error) error {
// Fastpath.
if len(shardSessions) == 1 {
return action(shardSessions[0])
}
allErrors := new(concurrency.AllErrorRecorder)
var wg sync.WaitGroup
for _, s := range shardSessions {
wg.Add(1)
go func(s *vtgatepb.Session_ShardSession) {
defer wg.Done()
if err := action(s); err != nil {
allErrors.RecordError(err)
}
}(s)
}
wg.Wait()
return allErrors.AggrError(vterrors.Aggregate)
}
// runTargets executes the action for all targets in parallel and returns a consolildated error.
// Flow is identical to runSessions.
func (txc *TxConn) runTargets(targets []*querypb.Target, action func(*querypb.Target) error) error {
if len(targets) == 1 {
return action(targets[0])
}
allErrors := new(concurrency.AllErrorRecorder)
var wg sync.WaitGroup
for _, t := range targets {
wg.Add(1)
go func(t *querypb.Target) {
defer wg.Done()
if err := action(t); err != nil {
allErrors.RecordError(err)
}
}(t)
}
wg.Wait()
return allErrors.AggrError(vterrors.Aggregate)
}