Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

*: improve log about stale-read query #52494

Merged
merged 5 commits into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions pkg/executor/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -1579,6 +1579,10 @@ func (a *ExecStmt) LogSlowQuery(txnTS uint64, succ bool, hasMoreResults bool) {
if !keyspace.IsKeyspaceNameEmpty(keyspaceName) {
keyspaceID = uint32(a.Ctx.GetStore().GetCodec().GetKeyspaceID())
}
if txnTS == 0 {
// TODO: txnTS maybe ambiguous, consider logging stale-read-ts with a new field in the slow log.
txnTS = sessVars.TxnCtx.StaleReadTs
}
crazycs520 marked this conversation as resolved.
Show resolved Hide resolved

slowItems := &variable.SlowQueryLogItems{
TxnTS: txnTS,
Expand Down
18 changes: 17 additions & 1 deletion pkg/executor/slow_query_sql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"math"
"os"
"testing"
"time"

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

func TestSlowQueryPrepared(t *testing.T) {
func TestSlowQueryMisc(t *testing.T) {
originCfg := config.GetGlobalConfig()
newCfg := *originCfg

Expand Down Expand Up @@ -158,6 +159,21 @@ func TestSlowQueryPrepared(t *testing.T) {
tk.MustQuery("SELECT Query FROM `information_schema`.`slow_query` " +
"where query like 'select%sleep%' order by time desc limit 1").
Check(testkit.Rows("select `sleep` ( ? ) , ?;"))

// Test 3 kinds of stale-read query.
tk.MustExec("create table test.t_stale_read (a int)")
time.Sleep(time.Second + time.Millisecond*10)
tk.MustExec("set tidb_redact_log=0;")
tk.MustExec("set @@tidb_read_staleness='-1'")
tk.MustQuery("select a from test.t_stale_read")
tk.MustExec("set @@tidb_read_staleness='0'")
t1 := time.Now()
tk.MustQuery(fmt.Sprintf("select a from test.t_stale_read as of timestamp '%s'", t1.Format("2006-1-2 15:04:05")))
tk.MustExec(fmt.Sprintf("start transaction read only as of timestamp '%v'", t1.Format("2006-1-2 15:04:05")))
tk.MustQuery("select a from test.t_stale_read")
tk.MustExec("commit")
require.Len(t, tk.MustQuery("SELECT query, txn_start_ts FROM `information_schema`.`slow_query` "+
"where query like 'select % from %t_stale_read%' and Txn_start_ts > 0").Rows(), 3)
crazycs520 marked this conversation as resolved.
Show resolved Hide resolved
}

func TestLogSlowLogIndex(t *testing.T) {
Expand Down
1 change: 1 addition & 0 deletions pkg/planner/core/preprocess.go
Original file line number Diff line number Diff line change
Expand Up @@ -1759,6 +1759,7 @@ func (p *preprocessor) updateStateFromStaleReadProcessor() error {
if err := txnManager.OnStmtStart(context.TODO(), txnManager.GetCurrentStmt()); err != nil {
return err
}
p.sctx.GetSessionVars().TxnCtx.StaleReadTs = p.LastSnapshotTS
}
}
}
Expand Down
10 changes: 7 additions & 3 deletions pkg/server/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -1139,17 +1139,21 @@ func (cc *clientConn) Run(ctx context.Context) {
if storeerr.ErrLockAcquireFailAndNoWaitSet.Equal(err) {
logutil.Logger(ctx).Debug("Expected error for FOR UPDATE NOWAIT", zap.Error(err))
} else {
var startTS uint64
var timestamp uint64
if ctx := cc.getCtx(); ctx != nil && ctx.GetSessionVars() != nil && ctx.GetSessionVars().TxnCtx != nil {
startTS = ctx.GetSessionVars().TxnCtx.StartTS
timestamp = ctx.GetSessionVars().TxnCtx.StartTS
if timestamp == 0 && ctx.GetSessionVars().TxnCtx.StaleReadTs > 0 {
// for state-read query.
timestamp = ctx.GetSessionVars().TxnCtx.StaleReadTs
}
}
logutil.Logger(ctx).Info("command dispatched failed",
zap.String("connInfo", cc.String()),
zap.String("command", mysql.Command2Str[data[0]]),
zap.String("status", cc.SessionStatusToString()),
zap.Stringer("sql", getLastStmtInConn{cc}),
zap.String("txn_mode", txnMode),
zap.Uint64("timestamp", startTS),
zap.Uint64("timestamp", timestamp),
zap.String("err", errStrForLog(err, cc.ctx.GetSessionVars().EnableRedactLog)),
)
}
Expand Down
1 change: 1 addition & 0 deletions pkg/session/txnmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,7 @@ func (m *txnManager) newProviderWithRequest(r *sessiontxn.EnterNewTxnRequest) (s
}

if r.StaleReadTS > 0 {
m.sctx.GetSessionVars().TxnCtx.StaleReadTs = r.StaleReadTS
return staleread.NewStalenessTxnContextProvider(m.sctx, r.StaleReadTS, nil), nil
}

Expand Down
1 change: 1 addition & 0 deletions pkg/sessionctx/variable/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ type TxnCtxNoNeedToRestore struct {
InfoSchema any
History any
StartTS uint64
StaleReadTs uint64

// ShardStep indicates the max size of continuous rowid shard in one transaction.
ShardStep int
Expand Down