Skip to content

Commit 224ae15

Browse files
authored
*: improve log about stale-read query (#52494)
close #52492
1 parent 6d52fa6 commit 224ae15

File tree

6 files changed

+31
-4
lines changed

6 files changed

+31
-4
lines changed

pkg/executor/adapter.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1579,6 +1579,10 @@ func (a *ExecStmt) LogSlowQuery(txnTS uint64, succ bool, hasMoreResults bool) {
15791579
if !keyspace.IsKeyspaceNameEmpty(keyspaceName) {
15801580
keyspaceID = uint32(a.Ctx.GetStore().GetCodec().GetKeyspaceID())
15811581
}
1582+
if txnTS == 0 {
1583+
// TODO: txnTS maybe ambiguous, consider logging stale-read-ts with a new field in the slow log.
1584+
txnTS = sessVars.TxnCtx.StaleReadTs
1585+
}
15821586

15831587
slowItems := &variable.SlowQueryLogItems{
15841588
TxnTS: txnTS,

pkg/executor/slow_query_sql_test.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"math"
2020
"os"
2121
"testing"
22+
"time"
2223

2324
"github.com/pingcap/tidb/pkg/config"
2425
"github.com/pingcap/tidb/pkg/executor"
@@ -122,7 +123,7 @@ func TestSlowQueryNonPrepared(t *testing.T) {
122123
`0 0 select * from t where a<3;`))
123124
}
124125

125-
func TestSlowQueryPrepared(t *testing.T) {
126+
func TestSlowQueryMisc(t *testing.T) {
126127
originCfg := config.GetGlobalConfig()
127128
newCfg := *originCfg
128129

@@ -158,6 +159,21 @@ func TestSlowQueryPrepared(t *testing.T) {
158159
tk.MustQuery("SELECT Query FROM `information_schema`.`slow_query` " +
159160
"where query like 'select%sleep%' order by time desc limit 1").
160161
Check(testkit.Rows("select `sleep` ( ? ) , ?;"))
162+
163+
// Test 3 kinds of stale-read query.
164+
tk.MustExec("create table test.t_stale_read (a int)")
165+
time.Sleep(time.Second + time.Millisecond*10)
166+
tk.MustExec("set tidb_redact_log=0;")
167+
tk.MustExec("set @@tidb_read_staleness='-1'")
168+
tk.MustQuery("select a from test.t_stale_read")
169+
tk.MustExec("set @@tidb_read_staleness='0'")
170+
t1 := time.Now()
171+
tk.MustQuery(fmt.Sprintf("select a from test.t_stale_read as of timestamp '%s'", t1.Format("2006-1-2 15:04:05")))
172+
tk.MustExec(fmt.Sprintf("start transaction read only as of timestamp '%v'", t1.Format("2006-1-2 15:04:05")))
173+
tk.MustQuery("select a from test.t_stale_read")
174+
tk.MustExec("commit")
175+
require.Len(t, tk.MustQuery("SELECT query, txn_start_ts FROM `information_schema`.`slow_query` "+
176+
"where (query = 'select a from test.t_stale_read;' or query like 'select a from test.t_stale_read as of timestamp %') and Txn_start_ts > 0").Rows(), 3)
161177
}
162178

163179
func TestLogSlowLogIndex(t *testing.T) {

pkg/planner/core/preprocess.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1759,6 +1759,7 @@ func (p *preprocessor) updateStateFromStaleReadProcessor() error {
17591759
if err := txnManager.OnStmtStart(context.TODO(), txnManager.GetCurrentStmt()); err != nil {
17601760
return err
17611761
}
1762+
p.sctx.GetSessionVars().TxnCtx.StaleReadTs = p.LastSnapshotTS
17621763
}
17631764
}
17641765
}

pkg/server/conn.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1139,17 +1139,21 @@ func (cc *clientConn) Run(ctx context.Context) {
11391139
if storeerr.ErrLockAcquireFailAndNoWaitSet.Equal(err) {
11401140
logutil.Logger(ctx).Debug("Expected error for FOR UPDATE NOWAIT", zap.Error(err))
11411141
} else {
1142-
var startTS uint64
1142+
var timestamp uint64
11431143
if ctx := cc.getCtx(); ctx != nil && ctx.GetSessionVars() != nil && ctx.GetSessionVars().TxnCtx != nil {
1144-
startTS = ctx.GetSessionVars().TxnCtx.StartTS
1144+
timestamp = ctx.GetSessionVars().TxnCtx.StartTS
1145+
if timestamp == 0 && ctx.GetSessionVars().TxnCtx.StaleReadTs > 0 {
1146+
// for state-read query.
1147+
timestamp = ctx.GetSessionVars().TxnCtx.StaleReadTs
1148+
}
11451149
}
11461150
logutil.Logger(ctx).Info("command dispatched failed",
11471151
zap.String("connInfo", cc.String()),
11481152
zap.String("command", mysql.Command2Str[data[0]]),
11491153
zap.String("status", cc.SessionStatusToString()),
11501154
zap.Stringer("sql", getLastStmtInConn{cc}),
11511155
zap.String("txn_mode", txnMode),
1152-
zap.Uint64("timestamp", startTS),
1156+
zap.Uint64("timestamp", timestamp),
11531157
zap.String("err", errStrForLog(err, cc.ctx.GetSessionVars().EnableRedactLog)),
11541158
)
11551159
}

pkg/session/txnmanager.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,7 @@ func (m *txnManager) newProviderWithRequest(r *sessiontxn.EnterNewTxnRequest) (s
333333
}
334334

335335
if r.StaleReadTS > 0 {
336+
m.sctx.GetSessionVars().TxnCtx.StaleReadTs = r.StaleReadTS
336337
return staleread.NewStalenessTxnContextProvider(m.sctx, r.StaleReadTS, nil), nil
337338
}
338339

pkg/sessionctx/variable/session.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ type TxnCtxNoNeedToRestore struct {
200200
InfoSchema any
201201
History any
202202
StartTS uint64
203+
StaleReadTs uint64
203204

204205
// ShardStep indicates the max size of continuous rowid shard in one transaction.
205206
ShardStep int

0 commit comments

Comments
 (0)