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

BaseDeltaIterator: always check valid() before accessing key() #4702

Closed
wants to merge 9 commits into from

Conversation

miasantreble
Copy link
Contributor

@miasantreble miasantreble commented Nov 20, 2018

Current implementation of current_over_upper_bound_ fails to take into consideration that keys might be invalid in either base iterator or delta iterator. Calling key() in such scenario will lead to assertion failure and runtime errors.
This PR addresses the bug by adding check for valid keys before calling IsOverUpperBound(), also added test coverage for iterate_upper_bound usage in BaseDeltaIterator
Also recommit #4656 (It was reverted earlier due to bugs)

Copy link
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

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

@miasantreble has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

Copy link
Contributor

@ajkr ajkr left a comment

Choose a reason for hiding this comment

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

lgtm

@@ -262,6 +260,7 @@ class BaseDeltaIterator : public Iterator {
}
bool BaseValid() const { return base_iterator_->Valid(); }
bool DeltaValid() const { return delta_iterator_->Valid(); }
bool valid() const { return (current_at_base_ ? BaseValid() : DeltaValid()); }
Copy link
Contributor

Choose a reason for hiding this comment

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

how about a name that differs other than capitalization, like BaseDeltaValid?

@siying
Copy link
Contributor

siying commented Nov 20, 2018

As discussed offline, I suggest we revert the original change for now. We only check in the fix if we add the coverage of upper bound in WriteBatchWithIndexTest.TestRandomIteraratorWithBase

@miasantreble
Copy link
Contributor Author

accidentally hit land to master button but looks like land failed due to the network sev. Closing for now, will reopen once test coverage has been added

@facebook-github-bot
Copy link
Contributor

@miasantreble has updated the pull request. Re-import the pull request

@miasantreble
Copy link
Contributor Author

Added unit test for iterate_upper_bound in NewIteratorWithBase. Specifically for the case that crashed mysql test: seek to a key outside of upper bound.
Also tried to add test coverage in TestRandomIteraratorWithBase, but the problem is that the iterator KVIter used for validating the result does not take ReadOptions or iterate_upper_bound, making it hard to verify the correctness of the BaseDeltaIterator

Copy link
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

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

@miasantreble has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

@facebook-github-bot
Copy link
Contributor

@miasantreble has updated the pull request. Re-import the pull request

@miasantreble miasantreble requested a review from siying November 29, 2018 05:56
@miasantreble
Copy link
Contributor Author

@siying randomized test added, also fixed a bug uncovered by randomized test.
PTAL

Copy link
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

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

@miasantreble has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

Copy link
Contributor

@siying siying left a comment

Choose a reason for hiding this comment

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

SeekForPrev() using a key not smaller than upper bound is undefined behavior. I think we don't have to complicate the code to handle this case. Rather, we can avoid this in randomize test.

@@ -506,7 +507,17 @@ typedef std::map<std::string, std::string> KVMap;
class KVIter : public Iterator {
public:
explicit KVIter(const KVMap* map) : map_(map), iter_(map_->end()) {}
virtual bool Valid() const { return iter_ != map_->end(); }
explicit KVIter(const KVMap* map, const Slice* iterate_upper_bound) : map_(map), iter_(map_->end()), iterate_upper_bound_(iterate_upper_bound) {}
Copy link
Contributor

Choose a reason for hiding this comment

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

80-char

if (iterate_upper_bound_ == nullptr) {
return iter_ != map_->end();
}
else {
Copy link
Contributor

Choose a reason for hiding this comment

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

Formatting

@facebook-github-bot
Copy link
Contributor

@miasantreble has updated the pull request. Re-import the pull request

Copy link
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

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

@miasantreble has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

@facebook-github-bot
Copy link
Contributor

@miasantreble has updated the pull request. Re-import the pull request

Copy link
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

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

@miasantreble has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

@@ -293,6 +293,10 @@ class BaseDeltaIterator : public Iterator {
if (delta_entry.type == kDeleteRecord ||
delta_entry.type == kSingleDeleteRecord) {
AdvanceDelta();
current_over_upper_bound_ = BaseDeltaValid() && IsOverUpperBound();
if (current_over_upper_bound_) {
break;
Copy link
Contributor

Choose a reason for hiding this comment

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

Why not just return from the function here?

@@ -293,6 +293,10 @@ class BaseDeltaIterator : public Iterator {
if (delta_entry.type == kDeleteRecord ||
delta_entry.type == kSingleDeleteRecord) {
AdvanceDelta();
current_over_upper_bound_ = BaseDeltaValid() && IsOverUpperBound();
Copy link
Contributor

Choose a reason for hiding this comment

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

The use of BaseDeltaValid() here is somehow confusing. We are advancing delta only in the line above, without updating current_at_base_, we use BaseDeltaValid(). We know it has under if (!BaseValid()), current_at_base_ won't change, but still, someone trying to understand the code needs to make efforts to understand what's going on here.
It will be slightly better to assert(!current_at_base_) above this line, and add a comment to explain why.

@facebook-github-bot
Copy link
Contributor

@miasantreble has updated the pull request. Re-import the pull request

Copy link
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

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

@miasantreble has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

@facebook-github-bot
Copy link
Contributor

@miasantreble has updated the pull request. Re-import the pull request

Copy link
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

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

@miasantreble is landing this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

@miasantreble miasantreble deleted the fix-mysql-crash branch November 30, 2018 23:37
miasantreble added a commit to miasantreble/rocksdb that referenced this pull request Dec 3, 2018
facebook-github-bot pushed a commit that referenced this pull request Dec 4, 2018
#4744)

Summary:
…) (#4702)"

This reverts commit 3a18bb3.
Pull Request resolved: #4744

Differential Revision: D13311869

Pulled By: miasantreble

fbshipit-source-id: 6300b12cc34828d8b9274e907a3aef1506d5d553
miasantreble added a commit to miasantreble/rocksdb that referenced this pull request Mar 26, 2019
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