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 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pkg/executor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -1993,6 +1993,7 @@ func ResetContextOfStmt(ctx sessionctx.Context, s ast.StmtNode) (err error) {
sc.TaskID = stmtctx.AllocateTaskID()
sc.CTEStorageMap = map[int]*CTEStorages{}
sc.IsStaleness = false
sc.StaleReadTs = 0
sc.LockTableIDs = make(map[int64]struct{})
sc.EnableOptimizeTrace = false
sc.OptimizeTracer = nil
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 @@ -1748,6 +1748,7 @@ func (p *preprocessor) updateStateFromStaleReadProcessor() error {
if p.flag&initTxnContextProvider != 0 {
p.sctx.GetSessionVars().StmtCtx.IsStaleness = true
if !p.sctx.GetSessionVars().InTxn() {
p.sctx.GetSessionVars().StmtCtx.StaleReadTs = p.LastSnapshotTS
txnManager := sessiontxn.GetTxnManager(p.sctx)
newTxnRequest := &sessiontxn.EnterNewTxnRequest{
Type: sessiontxn.EnterNewTxnWithReplaceProvider,
Expand Down
14 changes: 10 additions & 4 deletions pkg/server/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -1139,17 +1139,23 @@ 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
if ctx := cc.getCtx(); ctx != nil && ctx.GetSessionVars() != nil && ctx.GetSessionVars().TxnCtx != nil {
startTS = ctx.GetSessionVars().TxnCtx.StartTS
var timestamp uint64
if ctx := cc.getCtx(); ctx != nil && ctx.GetSessionVars() != nil {
if ctx.GetSessionVars().TxnCtx != nil {
timestamp = ctx.GetSessionVars().TxnCtx.StartTS
}
if timestamp == 0 && ctx.GetSessionVars().StmtCtx != nil && ctx.GetSessionVars().StmtCtx.StaleReadTs > 0 {
// for state-read query.
timestamp = ctx.GetSessionVars().StmtCtx.StaleReadTs
crazycs520 marked this conversation as resolved.
Show resolved Hide resolved
}
}
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().StmtCtx.StaleReadTs = r.StaleReadTS
return staleread.NewStalenessTxnContextProvider(m.sctx, r.StaleReadTS, nil), nil
}

Expand Down
1 change: 1 addition & 0 deletions pkg/sessionctx/stmtctx/stmtctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ type StatementContext struct {
// or is affected by the tidb_read_staleness session variable, then the statement will be makred as isStaleness
// in stmtCtx
IsStaleness bool
StaleReadTs uint64
InRestrictedSQL bool
ViewDepth int32
// mu struct holds variables that change during execution.
Expand Down