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

Ledger db migrations #1358

Merged
merged 23 commits into from
Oct 23, 2024
Merged

Ledger db migrations #1358

merged 23 commits into from
Oct 23, 2024

Conversation

rakanalh
Copy link
Contributor

@rakanalh rakanalh commented Oct 20, 2024

Description

This PR implements a migrator which atomically executes ledger DB migrations. The process runs as follows:

  1. Create a temporary directory which contains rocksdb files from the current ledger DB.
  2. Execute all migrations on temporary directory
  3. If all are successful, mark migrations as such
  4. Move successful temporary files into the original ledger DB directory.

This happens of course, in case where no ledger DB instance holds the lock on the directory. Otherwise, the process will fail.

This also implements the possibility to execute different migrations for each of sequencer, full node and prover.

TODO

  • Implement an example migration to update table data
  • Implement an example migration to update table fields
  • Implement an example migration to delete table
  • Add tests

Linked Issues

@rakanalh rakanalh force-pushed the rakanalh/ledger-db-migrations branch from f91a0f0 to a68a4c9 Compare October 20, 2024 17:29
@rakanalh rakanalh marked this pull request as ready for review October 22, 2024 11:19
Copy link

codecov bot commented Oct 22, 2024

Codecov Report

Attention: Patch coverage is 94.47005% with 12 lines in your changes missing coverage. Please review.

Project coverage is 78.4%. Comparing base (07729cc) to head (b947778).
Report is 1 commits behind head on nightly.

Files with missing lines Patch % Lines
...dk/full-node/db/sov-db/src/ledger_db/migrations.rs 93.5% 5 Missing ⚠️
...reign-sdk/full-node/db/sov-db/src/ledger_db/mod.rs 85.0% 3 Missing ⚠️
...overeign-sdk/full-node/db/sov-schema-db/src/lib.rs 0.0% 3 Missing ⚠️
...ign-sdk/full-node/db/sov-db/src/ledger_db/tests.rs 98.8% 1 Missing ⚠️
Additional details and impacted files
Files with missing lines Coverage Δ
bin/citrea/src/rollup/mod.rs 97.1% <100.0%> (+0.2%) ⬆️
crates/fullnode/src/db_migrations/mod.rs 100.0% <100.0%> (ø)
crates/fullnode/src/lib.rs 50.0% <ø> (ø)
crates/prover/src/db_migrations/mod.rs 100.0% <100.0%> (ø)
crates/prover/src/lib.rs 50.0% <ø> (ø)
crates/sequencer/src/db_migrations/mod.rs 100.0% <100.0%> (ø)
crates/sequencer/src/lib.rs 64.2% <ø> (ø)
...reign-sdk/full-node/db/sov-db/src/schema/tables.rs 91.9% <ø> (ø)
...ign-sdk/full-node/db/sov-db/src/ledger_db/tests.rs 98.8% <98.8%> (ø)
...reign-sdk/full-node/db/sov-db/src/ledger_db/mod.rs 79.7% <85.0%> (+0.6%) ⬆️
... and 2 more

... and 2 files with indirect coverage changes

Comment on lines +34 to +36
for (index, value) in values.into_iter().enumerate() {
ledger_db.put_value(index as u64, (index as u64, value))?;
}
Copy link
Member

Choose a reason for hiding this comment

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

we could also create the new table here right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, TestTableNew is a new table and the call to put_value here is adding entries into this new table.

Copy link
Member

Choose a reason for hiding this comment

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

but isn't that table already in the schema when you start the process? I'm talking about creating a new table that we haven't defined in LEDGER_TABLES previously, but we do now, with the migration

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So tables are not "created" when you start the process. They are created as soon as you write to them.

When we create the inner: DB part of the LedgerDB, we pass the schema of those tables but it won't actually create them until they're written to.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The idea behind the TestTableNew is to demonstrate how we can define the new table's schema and we add it to the code and we get the code to use it. However, it doesn't mean that it has data to be used. That's what migrations are for.

@rakanalh rakanalh force-pushed the rakanalh/ledger-db-migrations branch from ac09758 to b947778 Compare October 23, 2024 11:13
Copy link
Member

@ercecan ercecan left a comment

Choose a reason for hiding this comment

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

LGTM

@eyusufatik eyusufatik merged commit ce41cfb into nightly Oct 23, 2024
13 of 14 checks passed
@eyusufatik eyusufatik deleted the rakanalh/ledger-db-migrations branch October 23, 2024 12:33
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.

LedgerDB supports migrations
3 participants