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

executor: print arguments in execute statement in log files #7684

Merged
merged 17 commits into from
Oct 9, 2018

Conversation

jackysp
Copy link
Member

@jackysp jackysp commented Sep 13, 2018

What problem does this PR solve?

The log of the execute statement looks like

2018/09/13 13:52:31.659 session.go:1398: [info] [GENERAL_LOG] con:5 user:root@127.0.0.1 schema_ver:140 start_ts:0 sql:insert into t values (?)

We don't know what the exact argument value is.

What is changed and how it works?

Append arguments info into log files.

Check List

Tests

  • Unit test
  • Manual test
    For slow query log, general log and debug log:
2018/09/13 13:55:07.553 adapter.go:371: [warning] [SLOW_QUERY] cost_time:1.002700677s  succ:true con:1 user:root@127.0.0.1 txn_start_ts:402867645723443200 database:test sql:SELECT sleep(?) [arguments: 1]
2018/09/13 13:52:31.659 session.go:1398: [info] [GENERAL_LOG] con:5 user:root@127.0.0.1 schema_ver:140 start_ts:0 sql:insert into t values (?) [arguments: 1]
2018/09/13 13:52:31.659 adapter.go:367: [debug] [QUERY] cost_time:53.653µs  succ:true con:5 user:root@127.0.0.1 txn_start_ts:402867605119696898 database:test sql:insert into t values (?) [arguments: 1]

Code changes

  • Has exported function/method change

Side effects

  • Increased code complexity

Related changes

  • Need to cherry-pick to the release branch

PTAL @coocood @shenli

@jackysp jackysp added type/enhancement The issue or PR belongs to an enhancement. sig/execution SIG execution labels Sep 13, 2018
@jackysp
Copy link
Member Author

jackysp commented Sep 13, 2018

/run-all-tests

execStmt.UsingVars[i] = ast.NewValueExpr(val)
expr := ast.NewValueExpr(val)
execStmt.UsingVars[i] = expr
str, err := expr.ToString()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CI failed because expr is nil

@jackysp
Copy link
Member Author

jackysp commented Sep 13, 2018

/run-all-tests

types/datum.go Outdated
@@ -1501,6 +1501,8 @@ func (d *Datum) ToFloat64(sc *stmtctx.StatementContext) (float64, error) {
// ToString gets the string representation of the datum.
func (d *Datum) ToString() (string, error) {
switch d.Kind() {
case KindNull:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#6647
You may need to discuss with @coocood

types/datum.go Outdated
case KindMysqlDecimal:
return d.GetMysqlDecimal().String(), nil
case KindMysqlDuration:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't need this change?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've reordered this part to make it easier to find which kind has not been converted.

@jackysp
Copy link
Member Author

jackysp commented Sep 13, 2018

/run-all-tests

@jackysp
Copy link
Member Author

jackysp commented Sep 14, 2018

/run-all-tests

@coocood
Copy link
Member

coocood commented Sep 14, 2018

@jackysp
Can we print those args only when needed?
This allocates memory and hurts performance.

@jackysp
Copy link
Member Author

jackysp commented Sep 14, 2018

@coocood , it is easy to compose the arguments string only for the general log and the debug log, but it is not esay for the slow log.

@jackysp
Copy link
Member Author

jackysp commented Sep 15, 2018

/run-all-tests

@jackysp
Copy link
Member Author

jackysp commented Sep 15, 2018

PTAL @coocood

@@ -1060,3 +1063,104 @@ func (e *UnionExec) Close() error {
e.resourcePools = nil
return errors.Trace(e.baseExecutor.Close())
}

// ResetContextOfStmt resets the StmtContext and session variables.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's moved or changed?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The change at Line 1149.

@tiancaiamao
Copy link
Contributor

IMHO, move a function and change it is a bad habit. That will makes a PR harder to read.

@coocood
Copy link
Member

coocood commented Sep 17, 2018

LGTM

@@ -1418,7 +1418,7 @@ func logStmt(node ast.StmtNode, vars *variable.SessionVars) {

func logQuery(query string, vars *variable.SessionVars) {
if atomic.LoadUint32(&variable.ProcessGeneralLog) != 0 && !vars.InRestrictedSQL {
query = executor.QueryReplacer.Replace(query)
query = executor.QueryReplacer.Replace(query) + vars.GetExecuteArgumentsInfo()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think put vars.GetExecuteArgumentsInfo() to line 1422 is better. Reduce the processing of strings.

} else {
str, err := v.ToString()
if err != nil {
terror.Log(err)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add some details to this error log.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It has some details about it. Actually, it should not meet the error, just make the errcheck happy.

@@ -244,6 +238,9 @@ func (e *DeallocateExec) Next(ctx context.Context, chk *chunk.Chunk) error {
// CompileExecutePreparedStmt compiles a session Execute command to a stmt.Statement.
func CompileExecutePreparedStmt(ctx sessionctx.Context, ID uint32, args ...interface{}) (ast.Statement, error) {
execStmt := &ast.ExecuteStmt{ExecID: ID}
if err := ResetContextOfStmt(ctx, execStmt); err != nil {
return nil, err
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to add the error trace?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think no need now.

@jackysp
Copy link
Member Author

jackysp commented Sep 25, 2018

PTAL @zimulala

1 similar comment
@jackysp
Copy link
Member Author

jackysp commented Oct 8, 2018

PTAL @zimulala

zimulala
zimulala previously approved these changes Oct 9, 2018
Copy link
Contributor

@zimulala zimulala left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@zimulala zimulala added the status/LGT2 Indicates that a PR has LGTM 2. label Oct 9, 2018
@jackysp jackysp dismissed stale reviews from zimulala and ghost via 8faac5e October 9, 2018 06:26
@jackysp jackysp merged commit ee0d4d6 into pingcap:master Oct 9, 2018
@jackysp jackysp deleted the execute_args branch October 9, 2018 07:25
jackysp added a commit to jackysp/tidb that referenced this pull request Oct 25, 2018
jackysp added a commit to jackysp/tidb that referenced this pull request Oct 25, 2018
zz-jason pushed a commit that referenced this pull request Oct 29, 2018
XuHuaiyu pushed a commit to XuHuaiyu/tidb that referenced this pull request Dec 26, 2018
zimulala pushed a commit that referenced this pull request Dec 26, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
sig/execution SIG execution status/LGT2 Indicates that a PR has LGTM 2. type/enhancement The issue or PR belongs to an enhancement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants