Skip to content

Commit

Permalink
sql: add num_stmts, num_{auto_,}retries to txn info
Browse files Browse the repository at this point in the history
Add some new information to the transaction info protobuf, and to the
transactions internal tables.

Release note (sql change): add 3 new columns to the
crdb_internal.node_transactions and cluster_transactions tables:
num_stmts, num_retries, and num_auto_retries. These keep track of the
total number of statements executed on the transaction, the number of
retries encountered, and the number of automatic retries encountered.
  • Loading branch information
jordanlewis committed Aug 25, 2020
1 parent f3aa8d2 commit bae332d
Showing 7 changed files with 750 additions and 584 deletions.
1,288 changes: 712 additions & 576 deletions pkg/server/serverpb/status.pb.go

Large diffs are not rendered by default.

15 changes: 15 additions & 0 deletions pkg/server/serverpb/status.proto
Original file line number Diff line number Diff line change
@@ -476,6 +476,21 @@ message TxnInfo {
// txn_description is a text description of the underlying kv.Txn, intended
// for troubleshooting purposes.
string txn_description = 3;

// num_statements_executed is the number of statements that were executed so
// far on this transaction.
int32 num_statements_executed = 4;

// num_retries is the number of times that this transaction was retried.
int32 num_retries = 5;

// num_retries is the number of times that this transaction was automatically
// retried by the SQL executor.
int32 num_auto_retries = 6;

// The deadline by which the transaction must be committed.
google.protobuf.Timestamp deadline = 7
[ (gogoproto.nullable) = false, (gogoproto.stdtime) = true ];
}

// ActiveQuery represents a query in flight on some Session.
9 changes: 6 additions & 3 deletions pkg/sql/conn_executor.go
Original file line number Diff line number Diff line change
@@ -2342,9 +2342,12 @@ func (ex *connExecutor) serialize() serverpb.Session {
if txn != nil {
id := txn.ID()
activeTxnInfo = &serverpb.TxnInfo{
ID: id,
Start: ex.state.mu.txnStart,
TxnDescription: txn.String(),
ID: id,
Start: ex.state.mu.txnStart,
NumStatementsExecuted: int32(ex.state.mu.stmtCount),
NumRetries: int32(txn.Epoch()),
NumAutoRetries: int32(ex.extraTxnState.autoRetryCounter),
TxnDescription: txn.String(),
}
}

1 change: 1 addition & 0 deletions pkg/sql/conn_executor_exec.go
Original file line number Diff line number Diff line change
@@ -244,6 +244,7 @@ func (ex *connExecutor) execStmtInOpenState(
ex.incrementExecutedStmtCounter(stmt)
}
}()
ex.state.mu.stmtCount++

os := ex.machine.CurState().(stateOpen)

8 changes: 7 additions & 1 deletion pkg/sql/crdb_internal.go
Original file line number Diff line number Diff line change
@@ -1005,7 +1005,10 @@ CREATE TABLE crdb_internal.%s (
session_id STRING, -- the ID of the session
start TIMESTAMP, -- the start time of the transaction
txn_string STRING, -- the string representation of the transcation
application_name STRING -- the name of the application as per SET application_name
application_name STRING, -- the name of the application as per SET application_name
num_stmts INT, -- the number of statements executed so far
num_retries INT, -- the number of times the transaction was restarted
num_auto_retries INT -- the number of times the transaction was automatically restarted
)`

var crdbInternalLocalTxnsTable = virtualSchemaTable{
@@ -1071,6 +1074,9 @@ func populateTransactionsTable(
ts,
tree.NewDString(txn.TxnDescription),
tree.NewDString(session.ApplicationName),
tree.NewDInt(tree.DInt(txn.NumStatementsExecuted)),
tree.NewDInt(tree.DInt(txn.NumRetries)),
tree.NewDInt(tree.DInt(txn.NumAutoRetries)),
); err != nil {
return err
}
8 changes: 4 additions & 4 deletions pkg/sql/logictest/testdata/logic_test/crdb_internal
Original file line number Diff line number Diff line change
@@ -163,15 +163,15 @@ SELECT * FROM crdb_internal.cluster_queries WHERE node_id < 0
----
query_id txn_id node_id session_id user_name start query client_address application_name distributed phase

query TITTTT colnames
query TITTTTIII colnames
SELECT * FROM crdb_internal.node_transactions WHERE node_id < 0
----
id node_id session_id start txn_string application_name
id node_id session_id start txn_string application_name num_stmts num_retries num_auto_retries

query TITTTT colnames
query TITTTTIII colnames
SELECT * FROM crdb_internal.cluster_transactions WHERE node_id < 0
----
id node_id session_id start txn_string application_name
id node_id session_id start txn_string application_name num_stmts num_retries num_auto_retries

query ITTTTTTTTTTT colnames
SELECT * FROM crdb_internal.node_sessions WHERE node_id < 0
5 changes: 5 additions & 0 deletions pkg/sql/txn_state.go
Original file line number Diff line number Diff line change
@@ -53,6 +53,10 @@ type txnState struct {

// txnStart records the time that txn started.
txnStart time.Time

// stmtCount keeps track of the number of statements that the transaction
// has executed.
stmtCount int
}

// connCtx is the connection's context. This is the parent of Ctx.
@@ -198,6 +202,7 @@ func (ts *txnState) resetForNewSQLTxn(

ts.mon.Start(ts.Ctx, tranCtx.connMon, mon.BoundAccount{} /* reserved */)
ts.mu.Lock()
ts.mu.stmtCount = 0
if txn == nil {
ts.mu.txn = kv.NewTxnWithSteppingEnabled(ts.Ctx, tranCtx.db, tranCtx.nodeIDOrZero)
ts.mu.txn.SetDebugName(opName)

0 comments on commit bae332d

Please sign in to comment.