forked from vitessio/vitess
-
Notifications
You must be signed in to change notification settings - Fork 1
/
tx_conn.go
224 lines (198 loc) · 6.74 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
// Copyright 2016, Google Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package vtgate
import (
"sync"
"golang.org/x/net/context"
log "github.com/golang/glog"
"github.com/youtube/vitess/go/vt/concurrency"
"github.com/youtube/vitess/go/vt/dtids"
"github.com/youtube/vitess/go/vt/vterrors"
"github.com/youtube/vitess/go/vt/vtgate/gateway"
querypb "github.com/youtube/vitess/go/vt/proto/query"
vtgatepb "github.com/youtube/vitess/go/vt/proto/vtgate"
vtrpcpb "github.com/youtube/vitess/go/vt/proto/vtrpc"
)
// TxConn is used for executing transactional requests.
type TxConn struct {
gateway gateway.Gateway
}
// NewTxConn builds a new TxConn.
func NewTxConn(gw gateway.Gateway) *TxConn {
return &TxConn{gateway: gw}
}
// Commit commits the current transaction. If twopc is true, then the 2PC protocol
// is used to ensure atomicity.
func (txc *TxConn) Commit(ctx context.Context, twopc bool, session *SafeSession) error {
if session == nil {
return vterrors.New(vtrpcpb.Code_INVALID_ARGUMENT, "cannot commit: empty session")
}
if !session.InTransaction() {
return vterrors.New(vtrpcpb.Code_ABORTED, "cannot commit: not in transaction")
}
defer session.Reset()
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)
}