-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
tx_engine.go
576 lines (508 loc) · 17.4 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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
/*
Copyright 2019 The Vitess Authors.
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 (
"fmt"
"sync"
"time"
"vitess.io/vitess/go/vt/servenv"
"vitess.io/vitess/go/vt/vttablet/tabletserver/tx"
"context"
"vitess.io/vitess/go/timer"
"vitess.io/vitess/go/trace"
"vitess.io/vitess/go/vt/concurrency"
"vitess.io/vitess/go/vt/dtids"
"vitess.io/vitess/go/vt/log"
"vitess.io/vitess/go/vt/proto/vtrpc"
"vitess.io/vitess/go/vt/vterrors"
"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"
)
type txEngineState int
// The TxEngine can be in any of these states
const (
NotServing txEngineState = iota
Transitioning
AcceptingReadAndWrite
AcceptingReadOnly
)
func (state txEngineState) String() string {
names := [...]string{
"NotServing",
"Transitioning",
"AcceptReadWrite",
"AcceptingReadOnly"}
if state < NotServing || state > AcceptingReadOnly {
return fmt.Sprintf("Unknown - %d", int(state))
}
return names[state]
}
// TxEngine is responsible for handling the tx-pool and keeping read-write, read-only or not-serving
// states. It will start and shut down the underlying tx-pool as required.
type TxEngine struct {
env tabletenv.Env
stateLock sync.Mutex
state txEngineState
// beginRequests is used to make sure that we do not make a state
// transition while creating new transactions
beginRequests sync.WaitGroup
twopcEnabled bool
shutdownGracePeriod time.Duration
coordinatorAddress string
abandonAge time.Duration
ticks *timer.Timer
// reservedConnStats keeps statistics about reserved connections
reservedConnStats *servenv.TimingsWrapper
txPool *TxPool
preparedPool *TxPreparedPool
twoPC *TwoPC
}
// NewTxEngine creates a new TxEngine.
func NewTxEngine(env tabletenv.Env) *TxEngine {
config := env.Config()
te := &TxEngine{
env: env,
shutdownGracePeriod: config.GracePeriods.ShutdownSeconds.Get(),
reservedConnStats: env.Exporter().NewTimings("ReservedConnections", "Reserved connections stats", "operation"),
}
limiter := txlimiter.New(env)
te.txPool = NewTxPool(env, 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 = config.TwoPCAbandonAge.Get()
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.TxPool.Size - 2)
readPool := connpool.NewPool(env, "TxReadPool", tabletenv.ConnPoolConfig{
Size: 3,
IdleTimeoutSeconds: env.Config().TxPool.IdleTimeoutSeconds,
})
te.twoPC = NewTwoPC(readPool)
te.state = NotServing
return te
}
// AcceptReadWrite will start accepting all transactions.
// If transitioning from RO mode, transactions are rolled
// back before accepting new transactions. This is to allow
// for 2PC state to be correctly initialized.
func (te *TxEngine) AcceptReadWrite() {
te.transition(AcceptingReadAndWrite)
}
// AcceptReadOnly transitions to read-only mode. If current state
// is read-write, then we wait for shutdown and then transition.
func (te *TxEngine) AcceptReadOnly() {
te.transition(AcceptingReadOnly)
}
func (te *TxEngine) transition(state txEngineState) {
te.stateLock.Lock()
defer te.stateLock.Unlock()
if te.state == state {
return
}
log.Infof("TxEngine transition: %v", state)
switch te.state {
case AcceptingReadOnly, AcceptingReadAndWrite:
te.shutdownLocked()
case NotServing:
// No special action.
}
te.state = state
te.txPool.Open(te.env.Config().DB.AppWithDB(), te.env.Config().DB.DbaWithDB(), te.env.Config().DB.AppDebugWithDB())
if te.twopcEnabled && te.state == AcceptingReadAndWrite {
// If there are errors, 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.
if err := te.twoPC.Open(te.env.Config().DB); err != nil {
te.env.Stats().InternalErrors.Add("TwopcOpen", 1)
log.Errorf("Could not open TwoPC engine: %v", err)
}
if err := te.prepareFromRedo(); err != nil {
te.env.Stats().InternalErrors.Add("TwopcResurrection", 1)
log.Errorf("Could not prepare transactions: %v", err)
}
te.startWatchdog()
}
}
// Close will disregard common rules for when to kill transactions
// and wait forever for transactions to wrap up
func (te *TxEngine) Close() {
te.stateLock.Lock()
defer func() {
te.state = NotServing
te.stateLock.Unlock()
}()
if te.state == NotServing {
return
}
te.shutdownLocked()
log.Info("TxEngine: closed")
}
// Begin begins a transaction, and returns the associated transaction id and the
// statement(s) used to execute the begin (if any).
//
// Subsequent statements can access the connection through the transaction id.
func (te *TxEngine) Begin(ctx context.Context, preQueries []string, reservedID int64, options *querypb.ExecuteOptions) (int64, string, error) {
span, ctx := trace.NewSpan(ctx, "TxEngine.Begin")
defer span.Finish()
te.stateLock.Lock()
canOpenTransactions := te.state == AcceptingReadOnly || te.state == AcceptingReadAndWrite
if !canOpenTransactions {
// We are not in a state where we can start new transactions. Abort.
te.stateLock.Unlock()
return 0, "", vterrors.Errorf(vtrpc.Code_UNAVAILABLE, "tx engine can't accept new transactions in state %v", te.state)
}
// By Add() to beginRequests, we block others from initiating state
// changes until we have finished adding this transaction
te.beginRequests.Add(1)
te.stateLock.Unlock()
defer te.beginRequests.Done()
conn, beginSQL, err := te.txPool.Begin(ctx, options, te.state == AcceptingReadOnly, reservedID, preQueries)
if err != nil {
return 0, "", err
}
defer conn.UnlockUpdateTime()
return conn.ReservedID(), beginSQL, err
}
// Commit commits the specified transaction and renews connection id if one exists.
func (te *TxEngine) Commit(ctx context.Context, transactionID int64) (int64, string, error) {
span, ctx := trace.NewSpan(ctx, "TxEngine.Commit")
defer span.Finish()
var query string
var err error
connID, err := te.txFinish(transactionID, tx.TxCommit, func(conn *StatefulConnection) error {
query, err = te.txPool.Commit(ctx, conn)
return err
})
return connID, query, err
}
// Rollback rolls back the specified transaction.
func (te *TxEngine) Rollback(ctx context.Context, transactionID int64) (int64, error) {
span, ctx := trace.NewSpan(ctx, "TxEngine.Rollback")
defer span.Finish()
return te.txFinish(transactionID, tx.TxRollback, func(conn *StatefulConnection) error {
return te.txPool.Rollback(ctx, conn)
})
}
func (te *TxEngine) txFinish(transactionID int64, reason tx.ReleaseReason, f func(*StatefulConnection) error) (int64, error) {
conn, err := te.txPool.GetAndLock(transactionID, reason.String())
if err != nil {
return 0, err
}
err = f(conn)
if err != nil || !conn.IsTainted() {
conn.Release(reason)
return 0, err
}
err = conn.Renew()
if err != nil {
conn.Release(tx.ConnRenewFail)
return 0, err
}
return conn.ConnID, nil
}
// shutdownLocked 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) shutdownLocked() {
immediate := true
if te.state == AcceptingReadAndWrite {
immediate = false
}
// Unlock, wait for all begin requests to complete, and relock.
te.state = Transitioning
te.stateLock.Unlock()
te.beginRequests.Wait()
te.stateLock.Lock()
// 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() {
te.env.LogError()
close(rollbackDone)
}()
if immediate {
// Immediately rollback everything and return.
log.Info("Immediate shutdown: rolling back now.")
te.txPool.scp.ShutdownNonTx()
te.shutdownTransactions()
return
}
// If not immediate, we start with shutting down non-tx (reserved)
// connections.
te.txPool.scp.ShutdownNonTx()
if te.shutdownGracePeriod <= 0 {
// No grace period was specified. Wait indefinitely for transactions to be concluded.
// TODO(sougou): invoking rollbackPrepared is incorrect here. Prepared statements should
// actually be rolled back last. But this will cause the shutdown to hang because the
// tx pool will never become empty, because the prepared pool is holding on to connections
// from the tx pool. But we plan to deprecate this approach to 2PC. So, this
// should eventually be deleted.
te.rollbackPrepared()
log.Info("No grace period specified: performing normal wait.")
return
}
tmr := time.NewTimer(te.shutdownGracePeriod)
defer tmr.Stop()
select {
case <-tmr.C:
log.Info("Grace period exceeded: rolling back now.")
te.shutdownTransactions()
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()
}
// 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 _, preparedTx := range prepared {
txid, err := dtids.TransactionID(preparedTx.Dtid)
if err != nil {
log.Errorf("Error extracting transaction ID from ditd: %v", err)
}
if txid > maxid {
maxid = txid
}
conn, _, err := te.txPool.Begin(ctx, &querypb.ExecuteOptions{}, false, 0, nil)
if err != nil {
allErr.RecordError(err)
continue
}
for _, stmt := range preparedTx.Queries {
conn.TxProperties().RecordQuery(stmt)
_, err := conn.Exec(ctx, stmt, 1, false)
if err != nil {
allErr.RecordError(err)
te.txPool.RollbackAndRelease(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, preparedTx.Dtid)
if err != nil {
allErr.RecordError(err)
continue
}
}
for _, preparedTx := range failed {
txid, err := dtids.TransactionID(preparedTx.Dtid)
if err != nil {
log.Errorf("Error extracting transaction ID from ditd: %v", err)
}
if txid > maxid {
maxid = txid
}
te.preparedPool.SetFailed(preparedTx.Dtid)
}
te.txPool.AdjustLastID(maxid)
log.Infof("Prepared %d transactions, and registered %d failures.", len(prepared), len(failed))
return allErr.Error()
}
// shutdownTransactions 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) shutdownTransactions() {
te.rollbackPrepared()
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.Shutdown(ctx)
}
func (te *TxEngine) rollbackPrepared() {
ctx := tabletenv.LocalContext()
for _, conn := range te.preparedPool.FetchAll() {
te.txPool.Rollback(ctx, conn)
conn.Release(tx.TxRollback)
}
}
// 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 {
te.env.Stats().InternalErrors.Add("WatchdogFail", 1)
log.Errorf("Error reading unresolved prepares: '%v': %v", te.coordinatorAddress, err)
}
te.env.Stats().Unresolved.Set("Prepares", count)
// Resolve lingering distributed transactions.
txs, err := te.twoPC.ReadAbandoned(ctx, time.Now().Add(-te.abandonAge))
if err != nil {
te.env.Stats().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 {
te.env.Stats().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 {
te.env.Stats().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()
}
// ReserveBegin creates a reserved connection, and in it opens a transaction
func (te *TxEngine) ReserveBegin(ctx context.Context, options *querypb.ExecuteOptions, preQueries []string) (int64, error) {
span, ctx := trace.NewSpan(ctx, "TxEngine.ReserveBegin")
defer span.Finish()
conn, err := te.reserve(ctx, options, preQueries)
if err != nil {
return 0, vterrors.Wrap(err, "TxEngine.ReserveBegin")
}
defer conn.UnlockUpdateTime()
_, err = te.txPool.begin(ctx, options, te.state == AcceptingReadOnly, conn, nil)
if err != nil {
conn.Close()
conn.Release(tx.ConnInitFail)
return 0, err
}
return conn.ReservedID(), nil
}
// Reserve creates a reserved connection and returns the id to it
func (te *TxEngine) Reserve(ctx context.Context, options *querypb.ExecuteOptions, txID int64, preQueries []string) (int64, error) {
span, ctx := trace.NewSpan(ctx, "TxEngine.Reserve")
defer span.Finish()
if txID == 0 {
conn, err := te.reserve(ctx, options, preQueries)
if err != nil {
return 0, vterrors.Wrap(err, "TxEngine.Reserve")
}
defer conn.Unlock()
return conn.ReservedID(), nil
}
conn, err := te.txPool.GetAndLock(txID, "to reserve")
if err != nil {
return 0, vterrors.Wrap(err, "TxEngine.Reserve")
}
defer conn.Unlock()
err = te.taintConn(ctx, conn, preQueries)
if err != nil {
return 0, vterrors.Wrap(err, "TxEngine.Reserve")
}
return conn.ReservedID(), nil
}
// Reserve creates a reserved connection and returns the id to it
func (te *TxEngine) reserve(ctx context.Context, options *querypb.ExecuteOptions, preQueries []string) (*StatefulConnection, error) {
te.stateLock.Lock()
canOpenTransactions := te.state == AcceptingReadOnly || te.state == AcceptingReadAndWrite
if !canOpenTransactions {
// We are not in a state where we can start new transactions. Abort.
te.stateLock.Unlock()
return nil, vterrors.Errorf(vtrpc.Code_UNAVAILABLE, "cannot provide new connection in state %v", te.state)
}
te.stateLock.Unlock()
conn, err := te.txPool.scp.NewConn(ctx, options)
if err != nil {
return nil, err
}
err = te.taintConn(ctx, conn, preQueries)
if err != nil {
return nil, err
}
return conn, err
}
func (te *TxEngine) taintConn(ctx context.Context, conn *StatefulConnection, preQueries []string) error {
err := conn.Taint(ctx, te.reservedConnStats)
if err != nil {
return err
}
for _, query := range preQueries {
_, err := conn.Exec(ctx, query, 0 /*maxrows*/, false /*wantFields*/)
if err != nil {
conn.Releasef("error during connection setup: %s\n%v", query, err)
return err
}
}
return nil
}
// Release closes the underlying connection.
func (te *TxEngine) Release(connID int64) error {
conn, err := te.txPool.GetAndLock(connID, "for release")
if err != nil {
return err
}
conn.Release(tx.ConnRelease)
return nil
}