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 reverse iterator in RocksDB #2398

Merged
merged 17 commits into from
Nov 14, 2024
Merged

Fix reverse iterator in RocksDB #2398

merged 17 commits into from
Nov 14, 2024

Conversation

AurelienFT
Copy link
Contributor

@AurelienFT AurelienFT commented Oct 25, 2024

Linked Issues/PRs

Closes #2044

Description

Problem analysis

Our database columns use the prefix extractor configuration which allow rocksdb to make optimizations on storage based on prefix. However, this breaks the lexicographic order between prefixs and we need to pass a special option to the read iterator to bypass the "prefix sharding" (source: https://github.com/facebook/rocksdb/wiki/Prefix-Seek/3a5e28faf6c2d0ec1bdb99763043e1e3322007e9#how-to-ignore-prefix-bloom-filters-in-read).

Current solution

I took the same approach as before and started iteration from the next prefix using the bypass read option argument.

However I change the behavior to make only iterator to simplify and optimize the solution.

Better solution for the future

I think that breaking the "prefix sharding" from RocksDB to iterate can make the iterator way more costly.

There is a way to avoid it because we can iterate in reverse order inside a prefix (source: https://github.com/facebook/rocksdb/wiki/SeekForPrev) but we need to get the maximum value for a key in a given prefix to use it as a starting point of our iteration which is not possible to get in the current code because we don't have any information about the key.

Happy to here your thoughts @FuelLabs/client :)

EDIT: Not done : #2405

Checklist

  • Breaking changes are clearly marked as such in the PR description and changelog
  • New behavior is reflected in tests
  • The specification matches the implemented behavior (link update PR if changes are needed)

Before requesting review

  • I have reviewed the code myself
  • I have created follow-up issues caused by this PR and linked them here

@AurelienFT AurelienFT changed the title Add first working version but not sure of the correctness. Fix reverse iterator in RocksDB Oct 25, 2024
Copy link
Collaborator

@xgreenx xgreenx left a comment

Choose a reason for hiding this comment

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

It would be nice to have tests for reverse iteration with prefix. If can use examples from the mainnet, would be nice

crates/fuel-core/src/state/rocks_db.rs Outdated Show resolved Hide resolved
@AurelienFT
Copy link
Contributor Author

@xgreenx I have added a test at rocksdb level and integration level.

@AurelienFT AurelienFT self-assigned this Oct 28, 2024
netrome
netrome previously approved these changes Oct 29, 2024
Copy link
Contributor

@netrome netrome left a comment

Choose a reason for hiding this comment

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

Nice stuff. Thanks for spotting and fixing this!

crates/fuel-core/src/state/rocks_db.rs Outdated Show resolved Hide resolved
netrome
netrome previously approved these changes Oct 29, 2024
xgreenx
xgreenx previously approved these changes Oct 29, 2024
Copy link
Collaborator

@xgreenx xgreenx left a comment

Choose a reason for hiding this comment

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

It seems that final solution doesn't match the description of the PR=D

Copy link
Collaborator

@xgreenx xgreenx left a comment

Choose a reason for hiding this comment

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

I think the current solution is more elegant. But it's still not clear to me why the old one was not working

@xgreenx
Copy link
Collaborator

xgreenx commented Oct 29, 2024

Ohh, I see. We used iter_store with the next prefix, not the key, and because of that, if it returns None, we will fall back into the End branch.

@AurelienFT
Copy link
Contributor Author

@xgreenx Yes the description had one sentence that was outdated, I removed.
It was failing before, essentially because we wasn't using the read option total_order_seek that is needed if you want to iterate correctly from outside of a prefix befined in column creation. We could change to not use this option anymore with my futur proposition.

@AurelienFT AurelienFT enabled auto-merge (squash) October 29, 2024 15:40
@xgreenx
Copy link
Collaborator

xgreenx commented Oct 29, 2024

All tests are passing even without total_order_seek, I think the problem were in the old logic=D

BTW, documentation says that it is better to use auto_prefix_mode
image

@AurelienFT
Copy link
Contributor Author

The auto_prefix_mode doesn't seems to exists in rust-rocksdb (rust-rocksdb/rust-rocksdb#624).

You are right all the tests are passing but the test on the playground on the mainnet data isn't working without total_order_seek. I think the unit test it's because they are not using a column with prefix set and the integration test maybe don't have enough addresses defined.
Will try to change some tests to fail without total_order_seek

@AurelienFT
Copy link
Contributor Author

I don't find a way to make one of the integration or unit test fails :/

@xgreenx
Copy link
Collaborator

xgreenx commented Oct 29, 2024

You are right all the tests are passing but the test on the playground on the mainnet data isn't working without total_order_seek

So, did I get right that If you use the current fix but without total_order_seek, the query from the issue will fail?

@AurelienFT
Copy link
Contributor Author

@xgreenx yes.

@AurelienFT AurelienFT requested a review from a team October 30, 2024 16:13
xgreenx
xgreenx previously approved these changes Oct 30, 2024
@AurelienFT AurelienFT requested a review from a team October 31, 2024 09:36
@AurelienFT AurelienFT enabled auto-merge (squash) November 14, 2024 16:22
@AurelienFT AurelienFT merged commit 524b7f0 into master Nov 14, 2024
31 of 32 checks passed
@AurelienFT AurelienFT deleted the fix_api_bug_last branch November 14, 2024 17:08
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.

transactionsByOwner graphql query doesn't work properly when providing last
4 participants