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: add recent slow queries queue and ShowSlowQuery() function #7692

Merged
merged 5 commits into from
Sep 26, 2018

Conversation

tiancaiamao
Copy link
Contributor

What problem does this PR solve?

Make user easier to debug slow queries.

What is changed and how it works?

  • Add a recent slow queries queue
  • Provide ShowSlowQuery() function

Check List

Tests

  • Unit test

Code changes

  • Has exported function domain.ShowSlowQuery()

Related changes

It's provided to support admin show log in #7688

@winkyao @coocood

@shenli
Copy link
Member

shenli commented Sep 13, 2018

Why put it in the domain package?

@tiancaiamao
Copy link
Contributor Author

Where should I put it ? @shenli

@tiancaiamao
Copy link
Contributor Author

PTAL @coocood

count = len(q.data)
}
ret := make([]*SlowQueryInfo, 0, count)
tail := (q.tail - 1 + len(q.data)) % len(q.data)
Copy link
Member

@coocood coocood Sep 17, 2018

Choose a reason for hiding this comment

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

This is simpler

if q.head > q.tail {
  ret = append(ret, q.data[head:]...)
  ret = append(ret, q.data[:head]...)
} else {
  ret = append(ret, q.data[head:tail+1]...)
}
ret = ret[len(ret)-count:]

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 not simpler, it brings more mental burden.

ret = append(ret, h.data[i])
}

// Sort breaks the data, recover it here.
Copy link
Member

Choose a reason for hiding this comment

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

The sorted array still maintains the heap property, we don't need to copy and recover.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There are two problems here:

  1. To keep top N slow queries, we maintains a min heap, but sort will use a reverse order
  2. There is a count argument, so the sorted[count] is not the same with heap[len]

@tiancaiamao
Copy link
Contributor Author

PTAL @coocood

domain/topn_slow_query.go Outdated Show resolved Hide resolved
@@ -102,8 +156,46 @@ func (q *topNSlowQueries) Append(info *SlowQueryInfo) {
}

func (q *topNSlowQueries) Refresh(now time.Time) {
Copy link
Member

Choose a reason for hiding this comment

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

I think Refresh may not be a good name for its functionality.
What it does is cleaning up outdated entries, but Refresh usually means getting something new.

How about RemoveExpired.

@tiancaiamao
Copy link
Contributor Author

PTAL @coocood

@coocood
Copy link
Member

coocood commented Sep 25, 2018

LGTM

@tiancaiamao
Copy link
Contributor Author

PTAL @winkyao

@tiancaiamao tiancaiamao added status/LGT1 Indicates that a PR has LGTM 1. priority/release-blocker This issue blocks a release. Please solve it ASAP. labels Sep 25, 2018
tmp1 := slowQueryHeap{tmp}
sort.Sort(&tmp1)
ret = make([]*SlowQueryInfo, 0, count)
for i := len(tmp) - 1; i >= 0 && len(ret) <= count; i-- {
Copy link
Contributor

Choose a reason for hiding this comment

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

len(ret) < count ?

ast/misc.go Outdated
@@ -626,6 +626,26 @@ type HandleRange struct {
End int64
}

// ShowLogType defines the type for SlowLog.
type ShowLogType int
Copy link
Member

Choose a reason for hiding this comment

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

s/ShowLogType/ShowSlowQueryType/

Copy link
Contributor Author

Choose a reason for hiding this comment

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

When I merge master, this would be solved.

ast/misc.go Outdated
// ShowLog is used for the following command:
// admin show log top [user | internal | all] N
// admin show log recent N
type ShowLog struct {
Copy link
Member

Choose a reason for hiding this comment

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

s/ShowLog/ShowQueries/

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ditto

@tiancaiamao
Copy link
Contributor Author

/run-all-tests

@tiancaiamao
Copy link
Contributor Author

PTAL @crazycs520 @zz-jason

@tiancaiamao
Copy link
Contributor Author

/run-all-tests

return
}

q.data = append(q.data, info)[1:]
Copy link
Contributor

Choose a reason for hiding this comment

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

If you just use slice here, the q.data will keep the continuous memory, it may cause oom. What about using a list?

Copy link
Contributor

Choose a reason for hiding this comment

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

Do you do a large amount insert test?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@coocood suggest me to use this.
The code is the simplest queue implementation, although it double the memory usage.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The queue size will be 500.
We just keep the most recent queries in the queue.

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
priority/release-blocker This issue blocks a release. Please solve it ASAP. status/LGT1 Indicates that a PR has LGTM 1.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants