From c270c91820e05fe710b9f4bf7fa21dd38699b599 Mon Sep 17 00:00:00 2001 From: Shuaipeng Yu Date: Wed, 10 Jun 2020 16:37:31 +0800 Subject: [PATCH 1/4] metrics: distinguish transactions by mode Signed-off-by: Shuaipeng Yu --- executor/simple.go | 10 +++++----- metrics/session.go | 2 ++ session/session.go | 34 +++++++++++++++++----------------- session/tidb.go | 6 +++--- 4 files changed, 27 insertions(+), 25 deletions(-) diff --git a/executor/simple.go b/executor/simple.go index 2e9cd90ef158b..4f006791d5915 100644 --- a/executor/simple.go +++ b/executor/simple.go @@ -45,8 +45,8 @@ import ( ) var ( - transactionDurationInternalRollback = metrics.TransactionDuration.WithLabelValues(metrics.LblInternal, metrics.LblRollback) - transactionDurationGeneralRollback = metrics.TransactionDuration.WithLabelValues(metrics.LblGeneral, metrics.LblRollback) + transactionDurationPessimisticRollback = metrics.TransactionDuration.WithLabelValues(metrics.LblPessimistic, metrics.LblRollback) + transactionDurationOptimisticRollback = metrics.TransactionDuration.WithLabelValues(metrics.LblOptimistic, metrics.LblRollback) ) // SimpleExec represents simple statement executor. @@ -649,10 +649,10 @@ func (e *SimpleExec) executeRollback(s *ast.RollbackStmt) error { } if txn.Valid() { duration := time.Since(sessVars.TxnCtx.CreateTime).Seconds() - if sessVars.InRestrictedSQL { - transactionDurationInternalRollback.Observe(duration) + if sessVars.TxnCtx.IsPessimistic { + transactionDurationPessimisticRollback.Observe(duration) } else { - transactionDurationGeneralRollback.Observe(duration) + transactionDurationOptimisticRollback.Observe(duration) } sessVars.TxnCtx.ClearDelta() return txn.Rollback() diff --git a/metrics/session.go b/metrics/session.go index 172236e03f8f1..b97cc1682dc97 100644 --- a/metrics/session.go +++ b/metrics/session.go @@ -135,6 +135,8 @@ const ( LblSQLType = "sql_type" LblGeneral = "general" LblInternal = "internal" + LblPessimistic = "pessimistic" + LblOptimistic = "optimistic" LblStore = "store" LblAddress = "address" ) diff --git a/session/session.go b/session/session.go index e66f329217090..208316b464e12 100644 --- a/session/session.go +++ b/session/session.go @@ -74,14 +74,14 @@ import ( ) var ( - statementPerTransactionInternalOK = metrics.StatementPerTransaction.WithLabelValues(metrics.LblInternal, "ok") - statementPerTransactionInternalError = metrics.StatementPerTransaction.WithLabelValues(metrics.LblInternal, "error") - statementPerTransactionGeneralOK = metrics.StatementPerTransaction.WithLabelValues(metrics.LblGeneral, "ok") - statementPerTransactionGeneralError = metrics.StatementPerTransaction.WithLabelValues(metrics.LblGeneral, "error") - transactionDurationInternalCommit = metrics.TransactionDuration.WithLabelValues(metrics.LblInternal, metrics.LblCommit) - transactionDurationInternalAbort = metrics.TransactionDuration.WithLabelValues(metrics.LblInternal, metrics.LblAbort) - transactionDurationGeneralCommit = metrics.TransactionDuration.WithLabelValues(metrics.LblGeneral, metrics.LblCommit) - transactionDurationGeneralAbort = metrics.TransactionDuration.WithLabelValues(metrics.LblGeneral, metrics.LblAbort) + statementPerTransactionPessimisticOK = metrics.StatementPerTransaction.WithLabelValues(metrics.LblPessimistic, metrics.LblOK) + statementPerTransactionPessimisticError = metrics.StatementPerTransaction.WithLabelValues(metrics.LblPessimistic, metrics.LblError) + statementPerTransactionOptimisticOK = metrics.StatementPerTransaction.WithLabelValues(metrics.LblOptimistic, metrics.LblOK) + statementPerTransactionOptimisticError = metrics.StatementPerTransaction.WithLabelValues(metrics.LblOptimistic, metrics.LblError) + transactionDurationPessimisticCommit = metrics.TransactionDuration.WithLabelValues(metrics.LblPessimistic, metrics.LblCommit) + transactionDurationPessimisticAbort = metrics.TransactionDuration.WithLabelValues(metrics.LblPessimistic, metrics.LblAbort) + transactionDurationOptimisticCommit = metrics.TransactionDuration.WithLabelValues(metrics.LblOptimistic, metrics.LblCommit) + transactionDurationOptimisticAbort = metrics.TransactionDuration.WithLabelValues(metrics.LblOptimistic, metrics.LblAbort) sessionExecuteCompileDurationInternal = metrics.SessionExecuteCompileDuration.WithLabelValues(metrics.LblInternal) sessionExecuteCompileDurationGeneral = metrics.SessionExecuteCompileDuration.WithLabelValues(metrics.LblGeneral) @@ -2240,21 +2240,21 @@ func logQuery(query string, vars *variable.SessionVars) { } func (s *session) recordOnTransactionExecution(err error, counter int, duration float64) { - if s.isInternal() { + if s.sessionVars.TxnCtx.IsPessimistic { if err != nil { - statementPerTransactionInternalError.Observe(float64(counter)) - transactionDurationInternalAbort.Observe(duration) + statementPerTransactionPessimisticError.Observe(float64(counter)) + transactionDurationPessimisticAbort.Observe(duration) } else { - statementPerTransactionInternalOK.Observe(float64(counter)) - transactionDurationInternalCommit.Observe(duration) + statementPerTransactionPessimisticOK.Observe(float64(counter)) + transactionDurationPessimisticCommit.Observe(duration) } } else { if err != nil { - statementPerTransactionGeneralError.Observe(float64(counter)) - transactionDurationGeneralAbort.Observe(duration) + statementPerTransactionOptimisticError.Observe(float64(counter)) + transactionDurationOptimisticAbort.Observe(duration) } else { - statementPerTransactionGeneralOK.Observe(float64(counter)) - transactionDurationGeneralCommit.Observe(duration) + statementPerTransactionOptimisticOK.Observe(float64(counter)) + transactionDurationOptimisticCommit.Observe(duration) } } } diff --git a/session/tidb.go b/session/tidb.go index 42b674e6e9090..aa6efbc4d0c76 100644 --- a/session/tidb.go +++ b/session/tidb.go @@ -185,10 +185,10 @@ func Compile(ctx context.Context, sctx sessionctx.Context, stmtNode ast.StmtNode func recordAbortTxnDuration(sessVars *variable.SessionVars) { duration := time.Since(sessVars.TxnCtx.CreateTime).Seconds() - if sessVars.InRestrictedSQL { - transactionDurationInternalAbort.Observe(duration) + if sessVars.TxnCtx.IsPessimistic { + transactionDurationPessimisticAbort.Observe(duration) } else { - transactionDurationGeneralAbort.Observe(duration) + transactionDurationOptimisticAbort.Observe(duration) } } From 19af4f35166ac64c4b810014b2a08a00789dc068 Mon Sep 17 00:00:00 2001 From: Shuaipeng Yu Date: Wed, 10 Jun 2020 17:02:33 +0800 Subject: [PATCH 2/4] update Signed-off-by: Shuaipeng Yu --- metrics/grafana/tidb.json | 28 ++++++++++++++-------------- metrics/grafana/tidb_summary.json | 18 +++++++++--------- metrics/session.go | 5 +++-- 3 files changed, 26 insertions(+), 25 deletions(-) diff --git a/metrics/grafana/tidb.json b/metrics/grafana/tidb.json index fda688fadfe79..ea7e713e95908 100644 --- a/metrics/grafana/tidb.json +++ b/metrics/grafana/tidb.json @@ -3375,7 +3375,7 @@ "dashLength": 10, "dashes": false, "datasource": "${DS_TEST-CLUSTER}", - "description": "TiDB transaction processing counts by type and source. Internal means TiDB inner transcation calls", + "description": "TiDB transaction processing counts by type and source.", "editable": true, "error": false, "fill": 1, @@ -3416,10 +3416,10 @@ "steppedLine": false, "targets": [ { - "expr": "sum(rate(tidb_session_transaction_duration_seconds_count[1m])) by (type, sql_type)", + "expr": "sum(rate(tidb_session_transaction_duration_seconds_count[1m])) by (type, txn_mode)", "format": "time_series", "intervalFactor": 2, - "legendFormat": "{{type}}-{{sql_type}}", + "legendFormat": "{{type}}-{{txn_mode}}", "refId": "A", "step": 10 } @@ -3510,24 +3510,24 @@ "steppedLine": false, "targets": [ { - "expr": "histogram_quantile(0.99, sum(rate(tidb_session_transaction_duration_seconds_bucket[1m])) by (le, sql_type))", + "expr": "histogram_quantile(0.99, sum(rate(tidb_session_transaction_duration_seconds_bucket[1m])) by (le, txn_mode))", "format": "time_series", "intervalFactor": 2, - "legendFormat": "99-{{sql_type}}", + "legendFormat": "99-{{txn_mode}}", "refId": "A" }, { - "expr": "histogram_quantile(0.95, sum(rate(tidb_session_transaction_duration_seconds_bucket[1m])) by (le, sql_type))", + "expr": "histogram_quantile(0.95, sum(rate(tidb_session_transaction_duration_seconds_bucket[1m])) by (le, txn_mode))", "format": "time_series", "intervalFactor": 2, - "legendFormat": "95-{{sql_type}}", + "legendFormat": "95-{{txn_mode}}", "refId": "B" }, { - "expr": "histogram_quantile(0.80, sum(rate(tidb_session_transaction_duration_seconds_bucket[1m])) by (le, sql_type))", + "expr": "histogram_quantile(0.80, sum(rate(tidb_session_transaction_duration_seconds_bucket[1m])) by (le, txn_mode))", "format": "time_series", "intervalFactor": 2, - "legendFormat": "80-{{sql_type}}", + "legendFormat": "80-{{txn_mode}}", "refId": "C" } ], @@ -3578,7 +3578,7 @@ "dashLength": 10, "dashes": false, "datasource": "${DS_TEST-CLUSTER}", - "description": "TiDB statements numbers within one transaction. Internal means TiDB inner transaction", + "description": "TiDB statements numbers within one transaction.", "fill": 1, "fillGradient": 0, "gridPos": { @@ -3616,17 +3616,17 @@ "steppedLine": false, "targets": [ { - "expr": "histogram_quantile(0.99, sum(rate(tidb_session_transaction_statement_num_bucket[30s])) by (le, sql_type))", + "expr": "histogram_quantile(0.99, sum(rate(tidb_session_transaction_statement_num_bucket[30s])) by (le, txn_mode))", "format": "time_series", "intervalFactor": 2, - "legendFormat": "99-{{sql_type}}", + "legendFormat": "99-{{txn_mode}}", "refId": "A" }, { - "expr": "histogram_quantile(0.80, sum(rate(tidb_session_transaction_statement_num_bucket[30s])) by (le, sql_type))", + "expr": "histogram_quantile(0.80, sum(rate(tidb_session_transaction_statement_num_bucket[30s])) by (le, txn_mode))", "format": "time_series", "intervalFactor": 2, - "legendFormat": "80-{{sql_type}}", + "legendFormat": "80-{{txn_mode}}", "refId": "B" } ], diff --git a/metrics/grafana/tidb_summary.json b/metrics/grafana/tidb_summary.json index 01312196fa277..b61ad2575e5e4 100644 --- a/metrics/grafana/tidb_summary.json +++ b/metrics/grafana/tidb_summary.json @@ -1632,10 +1632,10 @@ "steppedLine": false, "targets": [ { - "expr": "sum(rate(tidb_session_transaction_duration_seconds_count{sql_type=\"general\"}[1m])) by (type)", + "expr": "sum(rate(tidb_session_transaction_duration_seconds_count[1m])) by (type, txn_mode)", "format": "time_series", "intervalFactor": 2, - "legendFormat": "{{type}}", + "legendFormat": "{{type}}-{{txn_mode}}", "refId": "A", "step": 10 } @@ -1723,24 +1723,24 @@ "steppedLine": false, "targets": [ { - "expr": "histogram_quantile(0.99, sum(rate(tidb_session_transaction_duration_seconds_bucket{sql_type=\"general\"}[1m])) by (le))", + "expr": "histogram_quantile(0.99, sum(rate(tidb_session_transaction_duration_seconds_bucket{sql_type=\"general\"}[1m])) by (le, txn_mode))", "format": "time_series", "intervalFactor": 2, - "legendFormat": "99", + "legendFormat": "99-{{txn_mode}}", "refId": "A" }, { - "expr": "histogram_quantile(0.95, sum(rate(tidb_session_transaction_duration_seconds_bucket{sql_type=\"general\"}[1m])) by (le))", + "expr": "histogram_quantile(0.95, sum(rate(tidb_session_transaction_duration_seconds_bucket{sql_type=\"general\"}[1m])) by (le, txn_mode))", "format": "time_series", "intervalFactor": 2, - "legendFormat": "95", + "legendFormat": "95-{{txn_mode}}", "refId": "B" }, { - "expr": "histogram_quantile(0.80, sum(rate(tidb_session_transaction_duration_seconds_bucket{sql_type=\"general\"}[1m])) by (le))", + "expr": "histogram_quantile(0.80, sum(rate(tidb_session_transaction_duration_seconds_bucket{sql_type=\"general\"}[1m])) by (le, txn_mode))", "format": "time_series", "intervalFactor": 2, - "legendFormat": "80", + "legendFormat": "80-{{txn_mode}}", "refId": "C" } ], @@ -1825,7 +1825,7 @@ "steppedLine": false, "targets": [ { - "expr": "histogram_quantile(1, sum(rate(tidb_session_transaction_statement_num_bucket{sql_type=\"general\"}[30s])) by (le))", + "expr": "histogram_quantile(1, sum(rate(tidb_session_transaction_statement_num_bucket}[30s])) by (le))", "format": "time_series", "intervalFactor": 2, "legendFormat": "99", diff --git a/metrics/session.go b/metrics/session.go index b97cc1682dc97..63bd4d84a83a8 100644 --- a/metrics/session.go +++ b/metrics/session.go @@ -80,7 +80,7 @@ var ( Name: "transaction_statement_num", Help: "Bucketed histogram of statements count in each transaction.", Buckets: prometheus.ExponentialBuckets(1, 2, 16), // 1 ~ 32768 - }, []string{LblSQLType, LblType}) + }, []string{LbTxnMode, LblType}) TransactionDuration = prometheus.NewHistogramVec( prometheus.HistogramOpts{ @@ -89,7 +89,7 @@ var ( Name: "transaction_duration_seconds", Help: "Bucketed histogram of a transaction execution duration, including retry.", Buckets: prometheus.ExponentialBuckets(0.001, 2, 28), // 1ms ~ 1.5days - }, []string{LblSQLType, LblType}) + }, []string{LbTxnMode, LblType}) StatementDeadlockDetectDuration = prometheus.NewHistogram( prometheus.HistogramOpts{ @@ -135,6 +135,7 @@ const ( LblSQLType = "sql_type" LblGeneral = "general" LblInternal = "internal" + LbTxnMode = "txn_mode" LblPessimistic = "pessimistic" LblOptimistic = "optimistic" LblStore = "store" From 0b40e0bb3108b2b1c750701213a5831b8ccbe858 Mon Sep 17 00:00:00 2001 From: Shuaipeng Yu Date: Wed, 10 Jun 2020 17:20:52 +0800 Subject: [PATCH 3/4] fix Signed-off-by: Shuaipeng Yu --- metrics/grafana/tidb_summary.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/metrics/grafana/tidb_summary.json b/metrics/grafana/tidb_summary.json index b61ad2575e5e4..06e1d872b1fc8 100644 --- a/metrics/grafana/tidb_summary.json +++ b/metrics/grafana/tidb_summary.json @@ -1825,7 +1825,7 @@ "steppedLine": false, "targets": [ { - "expr": "histogram_quantile(1, sum(rate(tidb_session_transaction_statement_num_bucket}[30s])) by (le))", + "expr": "histogram_quantile(1, sum(rate(tidb_session_transaction_statement_num_bucket[30s])) by (le))", "format": "time_series", "intervalFactor": 2, "legendFormat": "99", From 225bdf31e3beb1342aefecf6e68130855720b1a7 Mon Sep 17 00:00:00 2001 From: Shuaipeng Yu Date: Wed, 10 Jun 2020 17:24:26 +0800 Subject: [PATCH 4/4] update Signed-off-by: Shuaipeng Yu --- metrics/grafana/tidb_summary.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/metrics/grafana/tidb_summary.json b/metrics/grafana/tidb_summary.json index 06e1d872b1fc8..6c86b3964f78d 100644 --- a/metrics/grafana/tidb_summary.json +++ b/metrics/grafana/tidb_summary.json @@ -1828,7 +1828,7 @@ "expr": "histogram_quantile(1, sum(rate(tidb_session_transaction_statement_num_bucket[30s])) by (le))", "format": "time_series", "intervalFactor": 2, - "legendFormat": "99", + "legendFormat": "max", "refId": "A" } ],