-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Copy pathtxn_state.go
443 lines (391 loc) · 14.7 KB
/
txn_state.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
// Copyright 2017 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package sql
import (
"context"
"time"
"github.com/cockroachdb/cockroach/pkg/internal/client"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/settings/cluster"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/util/contextutil"
"github.com/cockroachdb/cockroach/pkg/util/hlc"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/cockroach/pkg/util/metric"
"github.com/cockroachdb/cockroach/pkg/util/mon"
"github.com/cockroachdb/cockroach/pkg/util/syncutil"
"github.com/cockroachdb/cockroach/pkg/util/timeutil"
"github.com/cockroachdb/cockroach/pkg/util/tracing"
"github.com/cockroachdb/errors"
"github.com/cockroachdb/logtags"
opentracing "github.com/opentracing/opentracing-go"
)
// txnState contains state associated with an ongoing SQL txn; it constitutes
// the ExtendedState of a connExecutor's state machine (defined in conn_fsm.go).
// It contains fields that are mutated as side-effects of state transitions;
// notably the KV client.Txn. All mutations to txnState are performed through
// calling fsm.Machine.Apply(event); see conn_fsm.go for the definition of the
// state machine.
type txnState struct {
// Mutable fields accessed from goroutines not synchronized by this txn's
// session, such as when a SHOW SESSIONS statement is executed on another
// session.
//
// Note that reads of mu.txn from the session's main goroutine do not require
// acquiring a read lock - since only that goroutine will ever write to
// mu.txn. Writes to mu.txn do require a write lock to guarantee safety with
// reads by other goroutines.
mu struct {
syncutil.RWMutex
txn *client.Txn
}
// connCtx is the connection's context. This is the parent of Ctx.
connCtx context.Context
// Ctx is the context for everything running in this SQL txn.
// This is only set while the session's state is not stateNoTxn.
Ctx context.Context
// sp is the span corresponding to the SQL txn. These are often root spans, as
// SQL txns are frequently the level at which we do tracing.
sp opentracing.Span
// recordingThreshold, is not zero, indicates that sp is recording and that
// the recording should be dumped to the log if execution of the transaction
// took more than this.
recordingThreshold time.Duration
recordingStart time.Time
// cancel is Ctx's cancellation function. Called upon COMMIT/ROLLBACK of the
// transaction to release resources associated with the context. nil when no
// txn is in progress.
cancel context.CancelFunc
// The timestamp to report for current_timestamp(), now() etc.
// This must be constant for the lifetime of a SQL transaction.
sqlTimestamp time.Time
// The transaction's priority.
priority roachpb.UserPriority
// The transaction's read only state.
readOnly bool
// Set to true when the current transaction is using a historical timestamp
// through the use of AS OF SYSTEM TIME.
isHistorical bool
// mon tracks txn-bound objects like the running state of
// planNode in the midst of performing a computation.
mon *mon.BytesMonitor
// The schema change closures to run when this txn is done.
schemaChangers schemaChangerCollection
// adv is overwritten after every transition. It represents instructions for
// for moving the cursor over the stream of input statements to the next
// statement to be executed.
// Do not use directly; set through setAdvanceInfo() and read through
// consumeAdvanceInfo().
adv advanceInfo
// txnAbortCount is incremented whenever the state transitions to
// stateAborted.
txnAbortCount *metric.Counter
// activeRestartSavepointName stores the name of the active
// top-level restart savepoint, or is empty if no top-level restart
// savepoint is active.
activeRestartSavepointName tree.Name
}
// txnType represents the type of a SQL transaction.
type txnType int
//go:generate stringer -type=txnType
const (
// implicitTxn means that the txn was created for a (single) SQL statement
// executed outside of a transaction.
implicitTxn txnType = iota
// explicitTxn means that the txn was explicitly started with a BEGIN
// statement.
explicitTxn
)
// resetForNewSQLTxn (re)initializes the txnState for a new transaction.
// It creates a new client.Txn and initializes it using the session defaults.
//
// connCtx: The context in which the new transaction is started (usually a
// connection's context). ts.Ctx will be set to a child context and should be
// used for everything that happens within this SQL transaction.
// txnType: The type of the starting txn.
// sqlTimestamp: The timestamp to report for current_timestamp(), now() etc.
// historicalTimestamp: If non-nil indicates that the transaction is historical
// and should be fixed to this timestamp.
// priority: The transaction's priority.
// readOnly: The read-only character of the new txn.
// txn: If not nil, this txn will be used instead of creating a new txn. If so,
// all the other arguments need to correspond to the attributes of this txn.
// tranCtx: A bag of extra execution context.
func (ts *txnState) resetForNewSQLTxn(
connCtx context.Context,
txnType txnType,
sqlTimestamp time.Time,
historicalTimestamp *hlc.Timestamp,
priority roachpb.UserPriority,
readOnly tree.ReadWriteMode,
txn *client.Txn,
tranCtx transitionCtx,
) {
// Reset state vars to defaults.
ts.sqlTimestamp = sqlTimestamp
ts.isHistorical = false
// Create a context for this transaction. It will include a root span that
// will contain everything executed as part of the upcoming SQL txn, including
// (automatic or user-directed) retries. The span is closed by finishSQLTxn().
// TODO(andrei): figure out how to close these spans on server shutdown? Ties
// into a larger discussion about how to drain SQL and rollback open txns.
var sp opentracing.Span
opName := sqlTxnName
// Create a span for the new txn. The span is always Recordable to support the
// use of session tracing, which may start recording on it.
// TODO(andrei): We should use tracing.EnsureChildSpan() as that's much more
// efficient that StartSpan (and also it'd be simpler), but that interface
// doesn't current support the Recordable option.
if parentSp := opentracing.SpanFromContext(connCtx); parentSp != nil {
// Create a child span for this SQL txn.
sp = parentSp.Tracer().StartSpan(
opName,
opentracing.ChildOf(parentSp.Context()), tracing.Recordable,
tracing.LogTagsFromCtx(connCtx),
)
} else {
// Create a root span for this SQL txn.
sp = tranCtx.tracer.(*tracing.Tracer).StartRootSpan(
opName, logtags.FromContext(connCtx), tracing.RecordableSpan)
}
if txnType == implicitTxn {
sp.SetTag("implicit", "true")
}
alreadyRecording := tranCtx.sessionTracing.Enabled()
duration := traceTxnThreshold.Get(&tranCtx.settings.SV)
if !alreadyRecording && (duration > 0) {
tracing.StartRecording(sp, tracing.SnowballRecording)
ts.recordingThreshold = duration
ts.recordingStart = timeutil.Now()
}
// Put the new span in the context.
txnCtx := opentracing.ContextWithSpan(connCtx, sp)
if !tracing.IsRecordable(sp) {
log.Fatalf(connCtx, "non-recordable transaction span of type: %T", sp)
}
ts.sp = sp
ts.Ctx, ts.cancel = contextutil.WithCancel(txnCtx)
ts.mon.Start(ts.Ctx, tranCtx.connMon, mon.BoundAccount{} /* reserved */)
ts.mu.Lock()
if txn == nil {
ts.mu.txn = client.NewTxnWithSteppingEnabled(ts.Ctx, tranCtx.db, tranCtx.nodeID)
ts.mu.txn.SetDebugName(opName)
} else {
ts.mu.txn = txn
}
ts.mu.Unlock()
if historicalTimestamp != nil {
ts.setHistoricalTimestamp(ts.Ctx, *historicalTimestamp)
}
if err := ts.setPriority(priority); err != nil {
panic(err)
}
if err := ts.setReadOnlyMode(readOnly); err != nil {
panic(err)
}
// Discard the old schemaChangers, if any.
ts.schemaChangers = schemaChangerCollection{}
}
// finishSQLTxn finalizes a transaction's results and closes the root span for
// the current SQL txn. This needs to be called before resetForNewSQLTxn() is
// called for starting another SQL txn.
func (ts *txnState) finishSQLTxn() {
ts.mon.Stop(ts.Ctx)
if ts.cancel != nil {
ts.cancel()
ts.cancel = nil
}
if ts.sp == nil {
panic("No span in context? Was resetForNewSQLTxn() called previously?")
}
if ts.recordingThreshold > 0 {
if r := tracing.GetRecording(ts.sp); r != nil {
if elapsed := timeutil.Since(ts.recordingStart); elapsed >= ts.recordingThreshold {
dump := r.String()
if len(dump) > 0 {
log.Infof(ts.Ctx, "SQL txn took %s, exceeding tracing threshold of %s:\n%s",
elapsed, ts.recordingThreshold, dump)
}
}
} else {
log.Warning(ts.Ctx, "Missing trace when sampled was enabled.")
}
}
ts.sp.Finish()
ts.sp = nil
ts.Ctx = nil
ts.mu.Lock()
ts.mu.txn = nil
ts.mu.Unlock()
ts.recordingThreshold = 0
}
// finishExternalTxn is a stripped-down version of finishSQLTxn used by
// connExecutors that run within a higher-level transaction (through the
// InternalExecutor). These guys don't want to mess with the transaction per-se,
// but still want to clean up other stuff.
func (ts *txnState) finishExternalTxn() {
if ts.Ctx == nil {
ts.mon.Stop(ts.connCtx)
} else {
ts.mon.Stop(ts.Ctx)
}
if ts.cancel != nil {
ts.cancel()
ts.cancel = nil
}
if ts.sp != nil {
ts.sp.Finish()
}
ts.sp = nil
ts.Ctx = nil
ts.mu.Lock()
ts.mu.txn = nil
ts.mu.Unlock()
}
func (ts *txnState) setHistoricalTimestamp(ctx context.Context, historicalTimestamp hlc.Timestamp) {
ts.mu.Lock()
ts.mu.txn.SetFixedTimestamp(ctx, historicalTimestamp)
ts.mu.Unlock()
ts.isHistorical = true
}
// getReadTimestamp returns the transaction's current read timestamp.
func (ts *txnState) getReadTimestamp() hlc.Timestamp {
ts.mu.RLock()
defer ts.mu.RUnlock()
return ts.mu.txn.ReadTimestamp()
}
func (ts *txnState) setPriority(userPriority roachpb.UserPriority) error {
ts.mu.Lock()
err := ts.mu.txn.SetUserPriority(userPriority)
ts.mu.Unlock()
if err != nil {
return err
}
ts.priority = userPriority
return nil
}
func (ts *txnState) setReadOnlyMode(mode tree.ReadWriteMode) error {
switch mode {
case tree.UnspecifiedReadWriteMode:
return nil
case tree.ReadOnly:
ts.readOnly = true
case tree.ReadWrite:
if ts.isHistorical {
return tree.ErrAsOfSpecifiedWithReadWrite
}
ts.readOnly = false
default:
return errors.AssertionFailedf("unknown read mode: %s", errors.Safe(mode))
}
return nil
}
// advanceCode is part of advanceInfo; it instructs the module managing the
// statements buffer on what action to take.
type advanceCode int
//go:generate stringer -type=advanceCode
const (
advanceUnknown advanceCode = iota
// stayInPlace means that the cursor should remain where it is. The same
// statement will be executed next.
stayInPlace
// advanceOne means that the cursor should be advanced by one position. This
// is the code commonly used after a successful statement execution.
advanceOne
// skipBatch means that the cursor should skip over any remaining commands
// that are part of the current batch and be positioned on the first
// comamnd in the next batch.
skipBatch
// rewind means that the cursor should be moved back to the position indicated
// by rewCap.
rewind
)
// txnEvent is part of advanceInfo, informing the connExecutor about some
// transaction events. It is used by the connExecutor to clear state associated
// with a SQL transaction (other than the state encapsulated in TxnState; e.g.
// schema changes and portals).
//
//go:generate stringer -type=txnEvent
type txnEvent int
const (
noEvent txnEvent = iota
// txnStart means that the statement that just ran started a new transaction.
// Note that when a transaction is restarted, txnStart event is not emitted.
txnStart
// txnCommit means that the transaction has committed (successfully). This
// doesn't mean that the SQL txn is necessarily "finished" - this event can be
// generated by a RELEASE statement and the connection is still waiting for a
// COMMIT.
// This event is produced both when entering the CommitWait state and also
// when leaving it.
txnCommit
// txnRollback means that the transaction has rolled back.
txnRollback
// txnRestart means that the transaction is restarting. The iteration
// of the txn just finished will not commit.
txnRestart
)
// advanceInfo represents instructions for the connExecutor about what statement
// to execute next (how to move its cursor over the input statements) and how
// to handle the results produced so far - can they be delivered to the client
// ASAP or not. advanceInfo is the "output" of performing a state transition.
type advanceInfo struct {
code advanceCode
// txnEvent is filled in when the transaction commits, aborts or starts
// waiting for a retry.
txnEvent txnEvent
// Fields for the rewind code:
// rewCap is the capability to rewind to the beginning of the transaction.
// rewCap.rewindAndUnlock() needs to be called to perform the promised rewind.
//
// This field should not be set directly; buildRewindInstructions() should be
// used.
rewCap rewindCapability
}
// transitionCtx is a bag of fields needed by some state machine events.
type transitionCtx struct {
db *client.DB
nodeID roachpb.NodeID
clock *hlc.Clock
// connMon is the connExecutor's monitor. New transactions will create a child
// monitor tracking txn-scoped objects.
connMon *mon.BytesMonitor
// The Tracer used to create root spans for new txns if the parent ctx doesn't
// have a span.
tracer opentracing.Tracer
// sessionTracing provides access to the session's tracing interface. The
// state machine needs to see if session tracing is enabled.
sessionTracing *SessionTracing
settings *cluster.Settings
}
var noRewind = rewindCapability{}
// setAdvanceInfo sets the adv field. This has to be called as part of any state
// transition. The connExecutor is supposed to inspect adv after any transition
// and act on it.
func (ts *txnState) setAdvanceInfo(code advanceCode, rewCap rewindCapability, ev txnEvent) {
if ts.adv.code != advanceUnknown {
panic("previous advanceInfo has not been consume()d")
}
if code != rewind && rewCap != noRewind {
panic("if rewCap is specified, code needs to be rewind")
}
ts.adv = advanceInfo{
code: code,
rewCap: rewCap,
txnEvent: ev,
}
}
// consumerAdvanceInfo returns the advanceInfo set by the last transition and
// resets the state so that another transition can overwrite it.
func (ts *txnState) consumeAdvanceInfo() advanceInfo {
adv := ts.adv
ts.adv = advanceInfo{}
return adv
}