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, session: replace new line and add user in query log #6748

Merged
merged 7 commits into from
Jun 11, 2018

Conversation

coocood
Copy link
Member

@coocood coocood commented Jun 4, 2018

So we can grep a keyword to get the query, without the need to look up the query in the full log.
user name is also needed for auditing purpose.

Fixes #6673

So we can grep a keyword to get the query, without the need to look up the query in the full log.
user name is also needed for auditing purpose.
"[QUERY] cost_time:%v succ:%v connection_id:%v txn_start_ts:%v database:%v table_ids:%v index_ids:%v sql:%v",
costTime, succ, connID, txnTS, currentDB, tableIDs, indexIDs, sql)
"[QUERY] cost_time:%v succ:%v connection_id:%v user:%s txn_start_ts:%v database:%v %v%vsql:%v",
costTime, succ, connID, user, txnTS, currentDB, tableIDs, indexIDs, sql)
Copy link
Member

Choose a reason for hiding this comment

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

Does the user contain host-ip?

@@ -324,6 +324,9 @@ func (a *ExecStmt) buildExecutor(ctx sessionctx.Context) (Executor, error) {
return e, nil
}

// QueryReplacer replaces new line and tab for grep result including query string.
var QueryReplacer = strings.NewReplacer("\r", "", "\n", "", "\t", " ")
Copy link
Member

Choose a reason for hiding this comment

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

How about there is a newline or tab in the string literal?

Copy link
Member Author

@coocood coocood Jun 5, 2018

Choose a reason for hiding this comment

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

It will be escaped as three characters `\' '\' 'n' instead of a single character '\n'.

Copy link
Member Author

@coocood coocood Jun 5, 2018

Choose a reason for hiding this comment

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

This query is used for logging, it's ok even if we replace new line in the string.

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

func logQuery(query string, vars *variable.SessionVars) {
if atomic.LoadUint32(&variable.ProcessGeneralLog) != 0 && !vars.InRestrictedSQL {
log.Infof("[con:%d][schema ver:%d][txn:%d] %s", vars.ConnectionID, vars.TxnCtx.SchemaVersion, vars.TxnCtx.StartTS, query)
query = executor.QueryReplacer.Replace(query)
log.Infof("[con:%d][user:%s][schema_ver:%d][txn:%d] %s", vars.ConnectionID, vars.User, vars.TxnCtx.SchemaVersion, vars.TxnCtx.StartTS, query)
Copy link
Member

Choose a reason for hiding this comment

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

Could you post an example of the new log?

@coocood
Copy link
Member Author

coocood commented Jun 5, 2018

general log example:

2018/06/05 10:19:29.944 session.go:1472: [info] [GENERAL_LOG] con:2 user:root@127.0.0.1 schema_ver:15 start_ts:0 sql:select 1

@@ -1457,9 +1457,9 @@ func logStmt(node ast.StmtNode, vars *variable.SessionVars) {
user := vars.User
schemaVersion := vars.TxnCtx.SchemaVersion
if ss, ok := node.(ast.SensitiveStmtNode); ok {
log.Infof("[CRUCIAL OPERATION] [con:%d][schema ver:%d] %s (by %s).", vars.ConnectionID, schemaVersion, ss.SecureText(), user)
log.Infof("[CRUCIAL OPERATION] con:%d schema_ver:%d %s (by %s).", vars.ConnectionID, schemaVersion, ss.SecureText(), user)
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 make the same changes to unify the log format in line 503?

@coocood
Copy link
Member Author

coocood commented Jun 5, 2018

@zimulala PTAL

currentDB := sessVars.CurrentDB
var tableIDs, indexIDs string
if len(sessVars.StmtCtx.TableIDs) > 0 {
tableIDs = strings.Replace(fmt.Sprintf("table_ids:%v ", a.Ctx.GetSessionVars().StmtCtx.TableIDs), " ", ",", -1)
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do you want to replace spaces with commas? I think it has a certain loss of performance. If it is not necessary, it may not be done.

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 is useful when we need to use ' ' as fields separator.

Copy link
Contributor

@lysu lysu Jun 6, 2018

Choose a reason for hiding this comment

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

or should we Itoa to []string and strings.Join if performance is critial?

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 performance is not critical here, we better use a simpler solution.

@jackysp
Copy link
Member

jackysp commented Jun 6, 2018

mysql> select 
    -> sleep(1);

will look like

[SLOW_QUERY] cost_time:1.001480917s succ:true con:2 user:root@127.0.0.1 txn_start_ts:400627245054951425 database:test sql:selectsleep(1)

@coocood
Copy link
Member Author

coocood commented Jun 6, 2018

@jackysp PTAL

@@ -324,6 +324,9 @@ func (a *ExecStmt) buildExecutor(ctx sessionctx.Context) (Executor, error) {
return e, nil
}

// QueryReplacer replaces new line and tab for grep result including query string.
var QueryReplacer = strings.NewReplacer("\r", "", "\n", " ", "\t", " ")
Copy link
Contributor

Choose a reason for hiding this comment

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

maybe use var QueryReplacer = strings.NewReplacer("\r", " ", "\n", " ", "\t", " ") although give more 1 space ?
if len(new) == len(old) == 1 replace will be more quicker~

func BenchmarkPureBinary(b *testing.B) {
	var r = strings.NewReplacer(",", " ", "-", " ")
	for i := 0; i < b.N; i++ {
		a := r.Replace("sadfsadfa,sdfsd,dsfsa,sdfsad,dsfsa-sdfa-sdf-dsfad-fsadf-sa,sadfas")
		a = a
	}
}

func BenchmarkBinaryString(b *testing.B) {
	var r = strings.NewReplacer(",", "", "-", "")
	for i := 0; i < b.N; i++ {
		a := r.Replace("sadfsadfa,sdfsd,dsfsa,sdfsad,dsfsa-sdfa-sdf-dsfad-fsadf-sa,sadfas")
		a = a
	}
}

BenchmarkPureBinary-8     	10000000	       144 ns/op	     160 B/op	       2 allocs/op
BenchmarkBinaryString-8   	 5000000	       267 ns/op	     128 B/op	       2 allocs/op

currentDB := sessVars.CurrentDB
var tableIDs, indexIDs string
if len(sessVars.StmtCtx.TableIDs) > 0 {
tableIDs = strings.Replace(fmt.Sprintf("table_ids:%v ", a.Ctx.GetSessionVars().StmtCtx.TableIDs), " ", ",", -1)
Copy link
Contributor

@lysu lysu Jun 6, 2018

Choose a reason for hiding this comment

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

or should we Itoa to []string and strings.Join if performance is critial?

if costTime < threshold {
logutil.SlowQueryLogger.Debugf(
"[QUERY] cost_time:%v succ:%v connection_id:%v txn_start_ts:%v database:%v table_ids:%v index_ids:%v sql:%v",
costTime, succ, connID, txnTS, currentDB, tableIDs, indexIDs, sql)
"[QUERY] cost_time:%v succ:%v con:%v user:%s txn_start_ts:%v database:%v %v%vsql:%v",
Copy link
Contributor

Choose a reason for hiding this comment

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

maybe not use %v if it know real type~

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 it doesn't matter much.

@coocood
Copy link
Member Author

coocood commented Jun 11, 2018

@lysu PTAL

@coocood
Copy link
Member Author

coocood commented Jun 11, 2018

@zimulala PTAL

Copy link
Member

@jackysp jackysp left a comment

Choose a reason for hiding this comment

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

LGTM

@lysu
Copy link
Contributor

lysu commented Jun 11, 2018

LGTM

@zimulala zimulala added the status/LGT2 Indicates that a PR has LGTM 2. label Jun 11, 2018
@coocood
Copy link
Member Author

coocood commented Jun 11, 2018

/run-all-tests

@coocood
Copy link
Member Author

coocood commented Jun 11, 2018

/run-unit-test

@coocood coocood merged commit d85334d into pingcap:master Jun 11, 2018
@coocood coocood deleted the refine-query-log branch June 11, 2018 12:38
coocood pushed a commit that referenced this pull request Jun 27, 2018
…#6920)

So we can grep a keyword to get the query, without the need to look up the query in the full log.
user name is also needed for auditing purpose.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status/LGT2 Indicates that a PR has LGTM 2.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants