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

precompute min safe ts #622

Merged
merged 4 commits into from
Dec 2, 2022
Merged

precompute min safe ts #622

merged 4 commits into from
Dec 2, 2022

Conversation

hihihuhu
Copy link
Contributor

compute the min safe ts on the query path is expensive because it has to allocate thousands of small objects with lock, when there are hundreds of tikv node. this makes stale read using session variable tidb_read_staleness noticeable slower than normal read.

the safe ts from tikv are only updated every 2 seconds, thus the min safe ts could be precomputed on the update path to avoid the expensive computation on query path.

result on our benchmark

  p99 p999 Cpu usage
Leader read + tidb_low_resolution_tso 4.0ms 14.1ms 413%
Stale read 10.1ms 40.3ms 486%
Stale read with precomputed min safe ts 5.5ms 13.6ms 416%

normal read:
Screen Shot 2022-11-23 at 10 22 26 AM

stale read before the change:
Screen Shot 2022-11-23 at 10 22 45 AM
zoom in:
Screen Shot 2022-11-23 at 10 23 00 AM

stale read after the change:
Screen Shot 2022-11-23 at 10 30 14 AM
zoom in:
Screen Shot 2022-11-23 at 10 31 05 AM

to further reduce the cpu usage, could remove the warning logging from https://github.com/pingcap/tidb/blob/v6.5.0-alpha/expression/builtin_time.go#L6576

@Connor1996
Copy link
Member

PTAL @Yisaer

@@ -571,6 +556,18 @@ func (s *KVStore) updateSafeTS(ctx context.Context) {
metrics.TiKVMinSafeTSGapSeconds.WithLabelValues(storeIDStr).Set(time.Since(safeTSTime).Seconds())
}(ctx, wg, storeID, storeAddr)
}

txnScopeMap := make(map[string][]uint64)
for _, store := range stores {
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 keep the original way

Copy link
Contributor Author

Choose a reason for hiding this comment

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

sorry, what do you mean by original here?

Copy link
Member

Choose a reason for hiding this comment

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

if store.IsLabelsMatch([]*metapb.StoreLabel{

Copy link
Contributor Author

Choose a reason for hiding this comment

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

IsLabelsMatch requires a specific value for the label, where here tries to precompute the min safe ts for all the labelled value

@Connor1996 Connor1996 requested a review from sticnarf November 30, 2022 18:18
Signed-off-by: hihihuhu <[email protected]>
Signed-off-by: hihihuhu <[email protected]>
Copy link
Member

@Connor1996 Connor1996 left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Collaborator

@sticnarf sticnarf left a comment

Choose a reason for hiding this comment

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

LGTM

@hihihuhu
Copy link
Contributor Author

hihihuhu commented Dec 1, 2022

the failed test seems related to tikv/tikv#13866, @Connor1996 do you know how often the tikv nightly image is updated?

@Connor1996
Copy link
Member

It should be updated every day, but not a promise

@sticnarf sticnarf merged commit 50651d6 into tikv:master Dec 2, 2022
@sticnarf
Copy link
Collaborator

sticnarf commented Dec 2, 2022

Thanks for your contribution!

@hihihuhu
Copy link
Contributor Author

hihihuhu commented Dec 2, 2022

hi @sticnarf, qq: is there a chance that we could port this to upcoming v6.5? if possible, do you know how is the process? thanks!

@Connor1996
Copy link
Member

Update tikv-client in TiDB. And v6.5 branch has already been check-out, you need to cherry-pick it to v6.5 branch. But not sure if the cherry-pick would be approved as usually only bugfix is allowed to be merged after code freeze。

@hihihuhu
Copy link
Contributor Author

hihihuhu commented Dec 2, 2022

i c, thanks for the info

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants