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

fix(keyviz): find nearest id if not found #1268

Open
wants to merge 9 commits into
base: master
Choose a base branch
from

Conversation

shhdgit
Copy link
Member

@shhdgit shhdgit commented May 19, 2022

No description provided.

@ti-chi-bot
Copy link
Member

[REVIEW NOTIFICATION]

This pull request has not been approved.

To complete the pull request process, please ask the reviewers in the list to review by filling /cc @reviewer in the comment.
After your PR has acquired the required number of LGTMs, you can assign this pull request to the committer in the list by filling /assign @committer in the comment to help you merge this pull request.

The full list of commands accepted by this bot can be found here.

Reviewer can indicate their review by submitting an approval review.
Reviewer can cancel approval by submitting a request changes review.

@codecov-commenter
Copy link

codecov-commenter commented May 19, 2022

Codecov Report

Attention: Patch coverage is 64.63415% with 29 lines in your changes missing coverage. Please review.

Project coverage is 31.94%. Comparing base (f3519f0) to head (cb475d4).
Report is 240 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1268      +/-   ##
==========================================
+ Coverage   31.77%   31.94%   +0.17%     
==========================================
  Files         310      310              
  Lines       17642    17712      +70     
  Branches      806      806              
==========================================
+ Hits         5605     5658      +53     
- Misses      11805    11821      +16     
- Partials      232      233       +1     
Flag Coverage Δ
be_integration_test_nightly 9.28% <0.00%> (-0.06%) ⬇️
be_unit_test 24.50% <64.63%> (+0.38%) ⬆️
e2e_test 69.65% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.


Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update f3519f0...cb475d4. Read the comment docs.

@shhdgit shhdgit force-pushed the fix/keyviz-invalid-border branch from 6c8dcb6 to 52d47d4 Compare May 19, 2022 01:23
@shhdgit shhdgit force-pushed the fix/keyviz-invalid-border branch from e99a6e6 to c67db19 Compare May 19, 2022 01:33
Comment on lines 85 to 132
func (inOrder *tableInOrder) findOne(fromID, toID int64) *tableDetail {
if fromID >= toID {
return nil
}

inOrder.rwMu.RLock()
defer inOrder.rwMu.RUnlock()

tLen := len(inOrder.tables)
pivot := tLen / 2
left := 0
right := tLen
for pivot > left {
prevID := inOrder.tables[pivot-1].ID
id := inOrder.tables[pivot].ID
// find approaching id near the fromId
// table_1 ------- table_3 ------- table_5
// ^
// search table_2
// approaching result: table_3
if prevID < fromID && id >= fromID {
break
}

if id < fromID {
left = pivot
} else {
right = pivot
}
pivot = (left + right) / 2
}

id := inOrder.tables[pivot].ID
if id < fromID || id >= toID {
return nil
}

return inOrder.tables[pivot]
}
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 simply use the binary search in std?

https://go.dev/src/sort/search.go

Copy link
Member

Choose a reason for hiding this comment

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

Ok, I got it, what you want to do is not a simple binary search.

// ^
// search table_2
// approaching result: table_3
if prevID < fromID && id >= fromID {
Copy link
Member

Choose a reason for hiding this comment

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

id >= toID?

Copy link
Member Author

Choose a reason for hiding this comment

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

Do you mean out of the range logic in L117-L120?😂

@shhdgit shhdgit force-pushed the fix/keyviz-invalid-border branch from 93cd068 to dd149e5 Compare May 19, 2022 07:26
@shhdgit shhdgit force-pushed the fix/keyviz-invalid-border branch from dd149e5 to 52a6f22 Compare May 19, 2022 09:02
@breezewish
Copy link
Member

Please fix the lint.

Also I believe you need more tests to cover the "findOne" function. For such complicated logic function, maybe better to ensure 100% branch coverage.

@shhdgit shhdgit force-pushed the fix/keyviz-invalid-border branch from 59261b5 to 9cd2564 Compare May 20, 2022 03:09
@breezewish
Copy link
Member

breezewish commented May 23, 2022

How about using sort.Search to discover a lower bound, and then check whether it meets upper bound? In this way you don't need to manually write the binary search or lower_bound algorithm.

Here is an example of using sort.Search to implement lower_bound: https://go-review.googlesource.com/c/go/+/29333/3/src/sort/example_search_test.go#45

@ti-chi-bot
Copy link
Member

@shhdgit: PR needs rebase.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

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

Successfully merging this pull request may close these issues.

4 participants