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

domain,executor: store topN slow query in domain #7646

Merged
merged 13 commits into from
Sep 12, 2018

Conversation

tiancaiamao
Copy link
Contributor

@tiancaiamao tiancaiamao commented Sep 8, 2018

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

  • Unit test

@shenli
Copy link
Member

shenli commented Sep 9, 2018

Why put it in the domain?

@winkyao
Copy link
Contributor

winkyao commented Sep 10, 2018

If put it in domain, it may lead to cycle import?

@tiancaiamao
Copy link
Contributor Author

There is no cycle import. executor imports domain, domain doesn't imports executor. @winkyao

So if we don't put it in domain, where should we put it? @shenli

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),
Copy link
Member

Choose a reason for hiding this comment

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

This should be configurable.

Copy link
Contributor Author

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.

}
}

func (q *topNSlowQuery) shiftUp(end int) {
Copy link
Member

Choose a reason for hiding this comment

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

siftUp?

@coocood
Copy link
Member

coocood commented Sep 10, 2018

This is not a performance hot spot, we can just use heap in the standard library.

@tiancaiamao
Copy link
Contributor Author

PTAL @coocood @winkyao


// topNSlowQuery maintains a heap to store recent slow queries.
// N = 30, recent = 7 days by default.
type topNSlowQuery struct {
Copy link
Member

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.

close(q.ch)
}

func (q *topNSlowQuery) Push(info *slowQueryInfo) {
Copy link
Member

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.

@tiancaiamao
Copy link
Contributor Author

PTAL @coocood

@@ -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 {
Copy link
Contributor

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.

Copy link
Contributor Author

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()
Copy link
Contributor

Choose a reason for hiding this comment

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

recover this goroutine.

}

// Rebuild the heap.
q.data = q.data[:idx]
Copy link
Contributor

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.

Copy link
Member

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.

Copy link
Contributor Author

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

q.data = append(q.data, x.(*slowQueryInfo))
}

func (q *topNSlowQueries) Pop() interface{} {
Copy link
Contributor

@winkyao winkyao Sep 11, 2018

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?

Copy link
Contributor

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?

Copy link
Member

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()

Copy link
Contributor Author

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.

@tiancaiamao
Copy link
Contributor Author

PTAL @shenli @winkyao

@coocood
Copy link
Member

coocood commented Sep 11, 2018

LGTM

winkyao
winkyao previously approved these changes Sep 11, 2018
Copy link
Contributor

@winkyao winkyao left a comment

Choose a reason for hiding this comment

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

LGTM

zhexuany
zhexuany previously approved these changes Sep 11, 2018
Copy link
Contributor

@zhexuany zhexuany left a 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,
Copy link
Member

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?

Copy link
Contributor

Choose a reason for hiding this comment

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

maybe slowQueryInfo is 🐶

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 exporting slowQueryInfo and use slowQueryInfo instead?

}

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 }
Copy link
Member

@zz-jason zz-jason Sep 11, 2018

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?

Copy link
Contributor Author

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

Copy link
Member

@zz-jason zz-jason Sep 11, 2018

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?

Copy link
Member

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.

Copy link
Member

@zz-jason zz-jason left a comment

Choose a reason for hiding this comment

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

has unresolved comments

@zz-jason zz-jason added type/enhancement The issue or PR belongs to an enhancement. status/LGT2 Indicates that a PR has LGTM 2. labels Sep 11, 2018
@tiancaiamao
Copy link
Contributor Author

PTAL @zz-jason

@zz-jason
Copy link
Member

LGTM

@zz-jason
Copy link
Member

/run-all-tests

1 similar comment
@zimulala
Copy link
Contributor

/run-all-tests

@tiancaiamao
Copy link
Contributor Author

/run-all-tests

@zz-jason
Copy link
Member

/run-all-tests

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.

8 participants