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

change(state): Allow opening the database in a read-only mode #8079

Merged
merged 12 commits into from
Dec 13, 2023

Conversation

upbqdn
Copy link
Member

@upbqdn upbqdn commented Dec 8, 2023

Motivation

Close #8078.

Depends-On: #8080

PR Author Checklist

Check before marking the PR as ready for review:

  • Will the PR name make sense to users?
  • Does the PR have a priority label?
  • Have you added or updated tests?
  • Is the documentation up to date?

Specifications

RocksDB's "read-only" and "secondary" modes: https://github.com/facebook/rocksdb/wiki/Read-only-and-Secondary-instances

Rust bindings:

Solution

  • Add a read-only option when opening the database.

Notes

Testing

  • I manually accessed the database using the zebra-scan crate in a separate binary while Zebra was running.

Review

Reviewer Checklist

Check before approving the PR:

  • Does the PR scope match the ticket?
  • Are there enough tests to make sure it works? Do the tests cover the PR motivation?
  • Are all the PR blockers dealt with?
    PR blockers can be dealt with in new tickets or PRs.

And check the PR Author checklist is complete.

@upbqdn upbqdn added A-rust Area: Updates to Rust code C-enhancement Category: This is an improvement P-Optional ✨ A-state Area: State / database changes labels Dec 8, 2023
@upbqdn upbqdn requested a review from teor2345 December 8, 2023 18:02
@upbqdn upbqdn self-assigned this Dec 8, 2023
@teor2345
Copy link
Collaborator

  • Writing functionality is still exposed even though the database can be opened in read-only mode.

We can open another ticket to clean that up. It's not really a problem because trying to write will fail. (And either panic or return an error, depending on how we handle RocksDB write errors.)

One possible solution is having a type that implements all the read methods, but only implements the two write methods: write(batch) and spawn_format_change() when it is a read-write database. This can be implemented using a generic parameter that allows writing.

For example, the second generic parameter here is required to be DBWithThreadModeInner, we could do something similar with a ReadWriteDatabase unit struct:
https://docs.rs/rocksdb/latest/rocksdb/struct.DBCommon.html#impl-DBCommon%3CT,+DBWithThreadModeInner%3E

And here are the methods that work regardless of the generic type (for us that would be read methods):
https://docs.rs/rocksdb/latest/rocksdb/struct.DBCommon.html#impl-DBCommon%3CT,+D%3E

  • The "secondary" mode seems like a better alternative because the "read-only" mode seems only to open a snapshot of the database, but I didn't manage to figure out what the secondary_path in open_cf_descriptors_as_secondary should be.

I don't think the difference is that important, but a random temporary directory would avoid locking conflicts with multiple secondaries:
https://github.com/facebook/rocksdb/wiki/Read-only-and-Secondary-instances

When using ldb I've just been giving an empty string, which I assume uses the current directory, or a temporary directory, or no directory. It hasn't caused any errors or created any files yet.

Copy link
Collaborator

@teor2345 teor2345 left a comment

Choose a reason for hiding this comment

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

Thanks! This is some great work!

I'm not sure if it's out of scope for the MVP, happy to let the team make a call on that.

zebra-scan/src/storage/db.rs Outdated Show resolved Hide resolved
@upbqdn upbqdn changed the title change(state): Allow opening the database read-only mode change(state): Allow opening the database in read-only mode Dec 11, 2023
@upbqdn upbqdn changed the title change(state): Allow opening the database in read-only mode change(state): Allow opening the database in a read-only mode Dec 11, 2023
Co-authored-by: teor <teor@riseup.net>
@arya2 arya2 added the do-not-merge Tells Mergify not to merge this PR label Dec 11, 2023
@upbqdn
Copy link
Member Author

upbqdn commented Dec 11, 2023

I'm putting this PR out of the draft since it looks like we can merge it.

@upbqdn upbqdn marked this pull request as ready for review December 11, 2023 20:34
@upbqdn upbqdn requested a review from a team as a code owner December 11, 2023 20:34
@teor2345 teor2345 removed the do-not-merge Tells Mergify not to merge this PR label Dec 12, 2023
teor2345
teor2345 previously approved these changes Dec 12, 2023
Copy link
Collaborator

@teor2345 teor2345 left a comment

Choose a reason for hiding this comment

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

Thanks! I've scheduled this after the scanner tests merge, because they are blocking other work.

@upbqdn upbqdn requested review from a team as code owners December 12, 2023 13:08
@upbqdn upbqdn requested review from arya2 and removed request for a team December 12, 2023 13:08
@github-actions github-actions bot added the C-trivial Category: A trivial change that is not worth mentioning in the CHANGELOG label Dec 12, 2023
@upbqdn upbqdn force-pushed the open-db-for-read-only-access branch from 6edb581 to 0add33c Compare December 12, 2023 13:15
@upbqdn upbqdn removed the C-trivial Category: A trivial change that is not worth mentioning in the CHANGELOG label Dec 12, 2023
teor2345
teor2345 previously approved these changes Dec 12, 2023
teor2345
teor2345 previously approved these changes Dec 12, 2023
Copy link
Collaborator

@teor2345 teor2345 left a comment

Choose a reason for hiding this comment

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

I did a bit of a merge and cleanup, let's see how it goes

@upbqdn upbqdn removed the request for review from arya2 December 13, 2023 14:00
@upbqdn
Copy link
Member Author

upbqdn commented Dec 13, 2023

There was still an issue with path resolution that should be fixed now.

I also updated the description and labels. All should be good to go.

Copy link
Collaborator

@teor2345 teor2345 left a comment

Choose a reason for hiding this comment

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

Looks great! I've made this merge wait until after #8080, because that's blocking some final tests.

@upbqdn
Copy link
Member Author

upbqdn commented Dec 13, 2023

How did you do that?

mergify bot added a commit that referenced this pull request Dec 13, 2023
@teor2345
Copy link
Collaborator

Using Depends-On: #8080 in the PR description:
https://docs.mergify.com/workflow/schedule-merge/#using-depends-on-to-manage-merge-dependencies

They also have a Merge-After: time field now.

@teor2345
Copy link
Collaborator

@Mergifyio refresh

Copy link
Contributor

mergify bot commented Dec 13, 2023

refresh

✅ Pull request refreshed

@teor2345
Copy link
Collaborator

This only failed the merge queue due to #7898

@upbqdn upbqdn mentioned this pull request Dec 13, 2023
7 tasks
mergify bot added a commit that referenced this pull request Dec 13, 2023
@mergify mergify bot merged commit 22852bc into main Dec 13, 2023
102 checks passed
@mergify mergify bot deleted the open-db-for-read-only-access branch December 13, 2023 21:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-rust Area: Updates to Rust code A-state Area: State / database changes C-enhancement Category: This is an improvement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

feature: Consider allowing opening the database in "read-only" or "secondary" mode
3 participants