-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
tx_engine.go
334 lines (305 loc) · 9.84 KB
/
tx_engine.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
/*
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 tabletserver
import (
"sync"
"time"
"golang.org/x/net/context"
"vitess.io/vitess/go/timer"
"vitess.io/vitess/go/vt/concurrency"
"vitess.io/vitess/go/vt/dbconfigs"
"vitess.io/vitess/go/vt/dtids"
"vitess.io/vitess/go/vt/log"
"vitess.io/vitess/go/vt/vtgate/vtgateconn"
"vitess.io/vitess/go/vt/vttablet/tabletserver/connpool"
"vitess.io/vitess/go/vt/vttablet/tabletserver/tabletenv"
"vitess.io/vitess/go/vt/vttablet/tabletserver/txlimiter"
querypb "vitess.io/vitess/go/vt/proto/query"
)
// TxEngine handles transactions.
type TxEngine struct {
dbconfigs *dbconfigs.DBConfigs
isOpen, twopcEnabled bool
shutdownGracePeriod time.Duration
coordinatorAddress string
abandonAge time.Duration
ticks *timer.Timer
txPool *TxPool
preparedPool *TxPreparedPool
twoPC *TwoPC
}
// NewTxEngine creates a new TxEngine.
func NewTxEngine(checker connpool.MySQLChecker, config tabletenv.TabletConfig) *TxEngine {
te := &TxEngine{
shutdownGracePeriod: time.Duration(config.TxShutDownGracePeriod * 1e9),
}
limiter := txlimiter.New(
config.TransactionCap,
config.TransactionLimitPerUser,
config.EnableTransactionLimit,
config.EnableTransactionLimitDryRun,
config.TransactionLimitByUsername,
config.TransactionLimitByPrincipal,
config.TransactionLimitByComponent,
config.TransactionLimitBySubcomponent,
)
te.txPool = NewTxPool(
config.PoolNamePrefix,
config.TransactionCap,
config.FoundRowsPoolSize,
time.Duration(config.TransactionTimeout*1e9),
time.Duration(config.IdleTimeout*1e9),
config.TxPoolWaiterCap,
checker,
limiter,
)
te.twopcEnabled = config.TwoPCEnable
if te.twopcEnabled {
if config.TwoPCCoordinatorAddress == "" {
log.Error("Coordinator address not specified: Disabling 2PC")
te.twopcEnabled = false
}
if config.TwoPCAbandonAge <= 0 {
log.Error("2PC abandon age not specified: Disabling 2PC")
te.twopcEnabled = false
}
}
te.coordinatorAddress = config.TwoPCCoordinatorAddress
te.abandonAge = time.Duration(config.TwoPCAbandonAge * 1e9)
te.ticks = timer.NewTimer(te.abandonAge / 2)
// Set the prepared pool capacity to something lower than
// tx pool capacity. Those spare connections are needed to
// perform metadata state change operations. Without this,
// the system can deadlock if all connections get moved to
// the TxPreparedPool.
te.preparedPool = NewTxPreparedPool(config.TransactionCap - 2)
readPool := connpool.New(
config.PoolNamePrefix+"TxReadPool",
3,
time.Duration(config.IdleTimeout*1e9),
checker,
)
te.twoPC = NewTwoPC(readPool)
return te
}
// InitDBConfig must be called before Init.
func (te *TxEngine) InitDBConfig(dbcfgs *dbconfigs.DBConfigs) {
te.dbconfigs = dbcfgs
}
// Init must be called once when vttablet starts for setting
// up the metadata tables.
func (te *TxEngine) Init() error {
if te.twopcEnabled {
return te.twoPC.Init(te.dbconfigs.SidecarDBName.Get(), te.dbconfigs.DbaWithDB())
}
return nil
}
// Open opens the TxEngine. If 2pc is enabled, it restores
// all previously prepared transactions from the redo log.
func (te *TxEngine) Open() {
if te.isOpen {
return
}
te.txPool.Open(te.dbconfigs.AppWithDB(), te.dbconfigs.DbaWithDB(), te.dbconfigs.AppDebugWithDB())
if !te.twopcEnabled {
te.isOpen = true
return
}
te.twoPC.Open(te.dbconfigs)
if err := te.prepareFromRedo(); err != nil {
// If this operation fails, we choose to raise an alert and
// continue anyway. Serving traffic is considered more important
// than blocking everything for the sake of a few transactions.
tabletenv.InternalErrors.Add("TwopcResurrection", 1)
log.Errorf("Could not prepare transactions: %v", err)
}
te.startWatchdog()
te.isOpen = true
}
// Close closes the TxEngine. If the immediate flag is on,
// then all current transactions are immediately rolled back.
// Otherwise, the function waits for all current transactions
// to conclude. If a shutdown grace period was specified,
// the transactions are rolled back if they're not resolved
// by that time.
func (te *TxEngine) Close(immediate bool) {
if !te.isOpen {
return
}
// Shut down functions are idempotent.
// No need to check if 2pc is enabled.
te.stopWatchdog()
poolEmpty := make(chan bool)
rollbackDone := make(chan bool)
// This goroutine decides if transactions have to be
// forced to rollback, and if so, when. Once done,
// the function closes rollbackDone, which can be
// verified to make sure it won't kick in later.
go func() {
defer func() {
tabletenv.LogError()
close(rollbackDone)
}()
if immediate {
// Immediately rollback everything and return.
log.Info("Immediate shutdown: rolling back now.")
te.rollbackTransactions()
return
}
if te.shutdownGracePeriod <= 0 {
// No grace period was specified. Never rollback.
log.Info("No grace period specified: performing normal wait.")
return
}
tmr := time.NewTimer(te.shutdownGracePeriod)
defer tmr.Stop()
select {
case <-tmr.C:
// The grace period has passed. Rollback, but don't touch the 2pc transactions.
log.Info("Grace period exceeded: rolling back non-2pc transactions now.")
te.txPool.RollbackNonBusy(tabletenv.LocalContext())
case <-poolEmpty:
// The pool cleared before the timer kicked in. Just return.
log.Info("Transactions completed before grace period: shutting down.")
}
}()
te.txPool.WaitForEmpty()
// If the goroutine is still running, signal that it can exit.
close(poolEmpty)
// Make sure the goroutine has returned.
<-rollbackDone
te.txPool.Close()
te.twoPC.Close()
te.isOpen = false
}
// prepareFromRedo replays and prepares the transactions
// from the redo log, loads previously failed transactions
// into the reserved list, and adjusts the txPool LastID
// to ensure there are no future collisions.
func (te *TxEngine) prepareFromRedo() error {
ctx := tabletenv.LocalContext()
var allErr concurrency.AllErrorRecorder
prepared, failed, err := te.twoPC.ReadAllRedo(ctx)
if err != nil {
return err
}
maxid := int64(0)
outer:
for _, tx := range prepared {
txid, err := dtids.TransactionID(tx.Dtid)
if err != nil {
log.Errorf("Error extracting transaction ID from ditd: %v", err)
}
if txid > maxid {
maxid = txid
}
conn, err := te.txPool.LocalBegin(ctx, &querypb.ExecuteOptions{})
if err != nil {
allErr.RecordError(err)
continue
}
for _, stmt := range tx.Queries {
conn.RecordQuery(stmt)
_, err := conn.Exec(ctx, stmt, 1, false)
if err != nil {
allErr.RecordError(err)
te.txPool.LocalConclude(ctx, conn)
continue outer
}
}
// We should not use the external Prepare because
// we don't want to write again to the redo log.
err = te.preparedPool.Put(conn, tx.Dtid)
if err != nil {
allErr.RecordError(err)
continue
}
}
for _, tx := range failed {
txid, err := dtids.TransactionID(tx.Dtid)
if err != nil {
log.Errorf("Error extracting transaction ID from ditd: %v", err)
}
if txid > maxid {
maxid = txid
}
te.preparedPool.SetFailed(tx.Dtid)
}
te.txPool.AdjustLastID(maxid)
log.Infof("Prepared %d transactions, and registered %d failures.", len(prepared), len(failed))
return allErr.Error()
}
// rollbackTransactions rolls back all open transactions
// including the prepared ones.
// This is used for transitioning from a master to a non-master
// serving type.
func (te *TxEngine) rollbackTransactions() {
ctx := tabletenv.LocalContext()
// The order of rollbacks is currently not material because
// we don't allow new statements or commits during
// this function. In case of any such change, this will
// have to be revisited.
te.txPool.RollbackNonBusy(ctx)
for _, c := range te.preparedPool.FetchAll() {
te.txPool.LocalConclude(ctx, c)
}
}
// startWatchdog starts the watchdog goroutine, which looks for abandoned
// transactions and calls the notifier on them.
func (te *TxEngine) startWatchdog() {
te.ticks.Start(func() {
ctx, cancel := context.WithTimeout(tabletenv.LocalContext(), te.abandonAge/4)
defer cancel()
// Raise alerts on prepares that have been unresolved for too long.
// Use 5x abandonAge to give opportunity for watchdog to resolve these.
count, err := te.twoPC.CountUnresolvedRedo(ctx, time.Now().Add(-te.abandonAge*5))
if err != nil {
tabletenv.InternalErrors.Add("WatchdogFail", 1)
log.Errorf("Error reading unresolved prepares: '%v': %v", te.coordinatorAddress, err)
}
tabletenv.Unresolved.Set("Prepares", count)
// Resolve lingering distributed transactions.
txs, err := te.twoPC.ReadAbandoned(ctx, time.Now().Add(-te.abandonAge))
if err != nil {
tabletenv.InternalErrors.Add("WatchdogFail", 1)
log.Errorf("Error reading transactions for 2pc watchdog: %v", err)
return
}
if len(txs) == 0 {
return
}
coordConn, err := vtgateconn.Dial(ctx, te.coordinatorAddress)
if err != nil {
tabletenv.InternalErrors.Add("WatchdogFail", 1)
log.Errorf("Error connecting to coordinator '%v': %v", te.coordinatorAddress, err)
return
}
defer coordConn.Close()
var wg sync.WaitGroup
for tx := range txs {
wg.Add(1)
go func(dtid string) {
defer wg.Done()
if err := coordConn.ResolveTransaction(ctx, dtid); err != nil {
tabletenv.InternalErrors.Add("WatchdogFail", 1)
log.Errorf("Error notifying for dtid %s: %v", dtid, err)
}
}(tx)
}
wg.Wait()
})
}
// stopWatchdog stops the watchdog goroutine.
func (te *TxEngine) stopWatchdog() {
te.ticks.Stop()
}