-
Notifications
You must be signed in to change notification settings - Fork 5.8k
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
domain,executor: store topN slow query in domain #7646
Conversation
Why put it in the |
If put it in domain, it may lead to cycle import? |
domain/domain.go
Outdated
@@ -471,6 +514,7 @@ func NewDomain(store kv.Storage, ddlLease time.Duration, statsLease time.Duratio | |||
sysSessionPool: pools.NewResourcePool(factory, capacity, capacity, resourceIdleTimeout), | |||
statsLease: statsLease, | |||
infoHandle: infoschema.NewHandle(store), | |||
slowQuery: newTopNSlowQuery(30, time.Hour*24*7), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be configurable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree, but we can do it in another PR.
domain/topn_slow_query.go
Outdated
} | ||
} | ||
|
||
func (q *topNSlowQuery) shiftUp(end int) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
siftUp?
This is not a performance hot spot, we can just use heap in the standard library. |
domain/topn_slow_query.go
Outdated
|
||
// topNSlowQuery maintains a heap to store recent slow queries. | ||
// N = 30, recent = 7 days by default. | ||
type topNSlowQuery struct { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are multiple query entries, so I think topNSlowQueries
is better.
domain/topn_slow_query.go
Outdated
close(q.ch) | ||
} | ||
|
||
func (q *topNSlowQuery) Push(info *slowQueryInfo) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not implement the Heap
interface in this type?
For the name confliction, we can change this Push
to Add
or Append
.
PTAL @coocood |
executor/adapter.go
Outdated
@@ -371,6 +372,13 @@ func (a *ExecStmt) logSlowQuery(txnTS uint64, succ bool) { | |||
logutil.SlowQueryLogger.Warnf( | |||
"[SLOW_QUERY] %vcost_time:%v %s succ:%v con:%v user:%s txn_start_ts:%v database:%v %v%vsql:%v", | |||
internal, costTime, sessVars.StmtCtx.GetExecDetails(), succ, connID, user, txnTS, currentDB, tableIDs, indexIDs, sql) | |||
if !sessVars.InRestrictedSQL { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just log general sql? I prefer to keep two heap to log the general sql and internal sql.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Internal SQL are always the same, it will not give us too much information.
domain/domain.go
Outdated
} | ||
|
||
func (do *Domain) topNSlowQueryLoop() { | ||
defer do.wg.Done() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
recover this goroutine.
domain/topn_slow_query.go
Outdated
} | ||
|
||
// Rebuild the heap. | ||
q.data = q.data[:idx] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You must use a lock to protect q.data, as long as you need to read the slice later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can do reading in the same goroutine.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll use copy on read, and they will be in one goroutine, no lock. @winkyao
domain/topn_slow_query.go
Outdated
q.data = append(q.data, x.(*slowQueryInfo)) | ||
} | ||
|
||
func (q *topNSlowQueries) Pop() interface{} { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pop can only return the minimum duration query, how can we implement topn, for example, the n is 30, and I wanna get top 3 query. and how can we just peek the heap?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe a b-tree is better?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The read operation is not implemented in this PR.
Reading doesn't need to call Pop()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Read will not be a frequent operation, so we just copy on read.
Get top 3 query is easy, copy the origin heap, Pop Pop Pop.
LGTM |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
domain/domain.go
Outdated
@@ -329,6 +331,48 @@ func (do *Domain) Reload() error { | |||
return nil | |||
} | |||
|
|||
// LogTopNSlowQuery keeps topN recent slow queries in domain. | |||
func (do *Domain) LogTopNSlowQuery(sql string, start time.Time, duration time.Duration, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function takes so many parameters, which makes it hard to read and maintain, could you extract a struct to store all the parameters and pass the struct as the parameter instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe slowQueryInfo
is 🐶
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
how about exporting slowQueryInfo
and use slowQueryInfo
instead?
domain/topn_slow_query.go
Outdated
} | ||
|
||
func (h *slowQueryHeap) Len() int { return len(h.data) } | ||
func (h *slowQueryHeap) Less(i, j int) bool { return h.data[i].duration < h.data[j].duration } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should it be h.data[i].duration > h.data[j].duration
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's Less
... @zz-jason
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use heap[len(heap)-1] to store the slowest query?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
heap[0]
is the fastest slow query, heap[len(heap)-1]
may not be the slowest query.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
has unresolved comments
c9db110
to
d88c674
Compare
PTAL @zz-jason |
LGTM |
/run-all-tests |
1 similar comment
/run-all-tests |
/run-all-tests |
/run-all-tests |
What problem does this PR solve?
Store topN slow query in domain, so later we can retrieve it later.
What is changed and how it works?
logSlowQuery in session will send a copy to domain, the domain maintains a heap to store the
topN recent slow queries.
Check List
Tests