Skip to content

Separate ledgerdb tables #1729

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

Merged
merged 11 commits into from
Jan 23, 2025
Merged
55 changes: 47 additions & 8 deletions bin/citrea/src/rollup/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ use sov_db::ledger_db::migrations::LedgerDBMigrator;
use sov_db::ledger_db::{LedgerDB, SharedLedgerOps};
use sov_db::mmr_db::MmrDB;
use sov_db::rocks_db_config::RocksdbConfig;
use sov_db::schema::tables::{
BATCH_PROVER_LEDGER_TABLES, FULL_NODE_LEDGER_TABLES, LIGHT_CLIENT_PROVER_LEDGER_TABLES,
SEQUENCER_LEDGER_TABLES,
};
use sov_db::schema::types::SoftConfirmationNumber;
use sov_modules_api::Spec;
use sov_modules_rollup_blueprint::RollupBlueprint;
Expand Down Expand Up @@ -66,12 +70,21 @@ pub trait CitreaRollupBlueprint: RollupBlueprint {
rollup_config.storage.path.as_path(),
citrea_sequencer::db_migrations::migrations(),
);
migrator.migrate(rollup_config.storage.db_max_open_files)?;

let sequencer_tables = SEQUENCER_LEDGER_TABLES
.iter()
.map(|table| table.to_string())
.collect::<Vec<_>>();

migrator.migrate(
rollup_config.storage.db_max_open_files,
sequencer_tables.clone(),
)?;

let rocksdb_config = RocksdbConfig::new(
rollup_config.storage.path.as_path(),
rollup_config.storage.db_max_open_files,
None,
Some(sequencer_tables),
);
let ledger_db = self.create_ledger_db(&rocksdb_config);
let genesis_config = self.create_genesis_config(runtime_genesis_paths, &rollup_config)?;
Expand Down Expand Up @@ -188,12 +201,20 @@ pub trait CitreaRollupBlueprint: RollupBlueprint {
citrea_fullnode::db_migrations::migrations(),
);

migrator.migrate(rollup_config.storage.db_max_open_files)?;
let full_node_tables = FULL_NODE_LEDGER_TABLES
.iter()
.map(|table| table.to_string())
.collect::<Vec<_>>();

migrator.migrate(
rollup_config.storage.db_max_open_files,
full_node_tables.clone(),
)?;

let rocksdb_config = RocksdbConfig::new(
rollup_config.storage.path.as_path(),
rollup_config.storage.db_max_open_files,
None,
Some(full_node_tables),
);

let ledger_db = self.create_ledger_db(&rocksdb_config);
Expand Down Expand Up @@ -315,12 +336,21 @@ pub trait CitreaRollupBlueprint: RollupBlueprint {
rollup_config.storage.path.as_path(),
citrea_batch_prover::db_migrations::migrations(),
);
migrator.migrate(rollup_config.storage.db_max_open_files)?;

let batch_prover_tables = BATCH_PROVER_LEDGER_TABLES
.iter()
.map(|table| table.to_string())
.collect::<Vec<_>>();

migrator.migrate(
rollup_config.storage.db_max_open_files,
batch_prover_tables.clone(),
)?;

let rocksdb_config = RocksdbConfig::new(
rollup_config.storage.path.as_path(),
rollup_config.storage.db_max_open_files,
None,
Some(batch_prover_tables),
);
let ledger_db = self.create_ledger_db(&rocksdb_config);

Expand Down Expand Up @@ -437,7 +467,16 @@ pub trait CitreaRollupBlueprint: RollupBlueprint {
rollup_config.storage.path.as_path(),
citrea_light_client_prover::db_migrations::migrations(),
);
migrator.migrate(rollup_config.storage.db_max_open_files)?;

let light_client_prover_tables = LIGHT_CLIENT_PROVER_LEDGER_TABLES
.iter()
.map(|table| table.to_string())
.collect::<Vec<_>>();

migrator.migrate(
rollup_config.storage.db_max_open_files,
light_client_prover_tables.clone(),
)?;

let mut task_manager = TaskManager::default();
let da_service = self
Expand All @@ -447,7 +486,7 @@ pub trait CitreaRollupBlueprint: RollupBlueprint {
let rocksdb_config = RocksdbConfig::new(
rollup_config.storage.path.as_path(),
rollup_config.storage.db_max_open_files,
None,
Some(light_client_prover_tables),
);
let ledger_db = self.create_ledger_db(&rocksdb_config);
let mmr_db = MmrDB::new(&rocksdb_config)?;
Expand Down
3 changes: 3 additions & 0 deletions crates/batch-prover/src/db_migrations/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ use std::sync::OnceLock;
use sov_db::ledger_db::migrations::LedgerMigration;

use crate::db_migrations::batch_and_slot_by_number::MigrateBatchAndSlotByNumber;
use crate::db_migrations::remove_unused_common_tables::RemoveUnusedTables;
use crate::db_migrations::verified_proofs::MigrateVerifiedProofsBySlotNumber;

mod batch_and_slot_by_number;
mod remove_unused_common_tables;
mod verified_proofs;

pub fn migrations() -> &'static Vec<Box<dyn LedgerMigration + Send + Sync + 'static>> {
Expand All @@ -15,6 +17,7 @@ pub fn migrations() -> &'static Vec<Box<dyn LedgerMigration + Send + Sync + 'sta
vec![
Box::new(MigrateVerifiedProofsBySlotNumber {}),
Box::new(MigrateBatchAndSlotByNumber {}),
Box::new(RemoveUnusedTables {}),
]
})
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use std::sync::Arc;

use sov_db::ledger_db::migrations::{LedgerMigration, MigrationName, MigrationVersion};
use sov_db::ledger_db::LedgerDB;
use sov_db::schema::tables::{BATCH_PROVER_LEDGER_TABLES, LEDGER_TABLES};

/// Table removal migration
/// tables BatchByNumber and SlotByNumber are removed
pub(crate) struct RemoveUnusedTables {}

impl LedgerMigration for RemoveUnusedTables {
fn identifier(&self) -> (MigrationName, MigrationVersion) {
("RemoveUnusedTables".to_owned(), 3)
}

fn execute(
&self,
ledger_db: Arc<LedgerDB>,
tables_to_drop: &mut Vec<String>,
) -> anyhow::Result<()> {
// Get difference of LEDGER_TABLES and SEQUENCER_LEDGER_TABLES and drop them
let mut diff = LEDGER_TABLES.to_vec();
diff.retain(|x| !BATCH_PROVER_LEDGER_TABLES.contains(x));
let diff_tables = diff.iter().map(|x| x.to_string()).collect::<Vec<_>>();
for table in diff_tables {
// Check if table exists in the database
if let Ok(_cf_handle) = ledger_db.get_cf_handle(&table) {
tables_to_drop.push(table);
}
}
Ok(())
}
}
3 changes: 3 additions & 0 deletions crates/fullnode/src/db_migrations/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ use std::sync::OnceLock;
use sov_db::ledger_db::migrations::LedgerMigration;

use crate::db_migrations::batch_and_slot_by_number::MigrateBatchAndSlotByNumber;
use crate::db_migrations::remove_unused_common_tables::RemoveUnusedTables;
use crate::db_migrations::verified_proofs::MigrateVerifiedProofsBySlotNumber;

mod batch_and_slot_by_number;
mod remove_unused_common_tables;
mod verified_proofs;

pub fn migrations() -> &'static Vec<Box<dyn LedgerMigration + Send + Sync + 'static>> {
Expand All @@ -15,6 +17,7 @@ pub fn migrations() -> &'static Vec<Box<dyn LedgerMigration + Send + Sync + 'sta
vec![
Box::new(MigrateVerifiedProofsBySlotNumber {}),
Box::new(MigrateBatchAndSlotByNumber {}),
Box::new(RemoveUnusedTables {}),
]
})
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use std::sync::Arc;

use sov_db::ledger_db::migrations::{LedgerMigration, MigrationName, MigrationVersion};
use sov_db::ledger_db::LedgerDB;
use sov_db::schema::tables::{FULL_NODE_LEDGER_TABLES, LEDGER_TABLES};

/// Table removal migration
/// tables BatchByNumber and SlotByNumber are removed
pub(crate) struct RemoveUnusedTables {}

impl LedgerMigration for RemoveUnusedTables {
fn identifier(&self) -> (MigrationName, MigrationVersion) {
("RemoveUnusedTables".to_owned(), 3)
}

fn execute(
&self,
ledger_db: Arc<LedgerDB>,
tables_to_drop: &mut Vec<String>,
) -> anyhow::Result<()> {
// Get difference of LEDGER_TABLES and SEQUENCER_LEDGER_TABLES and drop them
let mut diff = LEDGER_TABLES.to_vec();
diff.retain(|x| !FULL_NODE_LEDGER_TABLES.contains(x));
let diff_tables = diff.iter().map(|x| x.to_string()).collect::<Vec<_>>();
for table in diff_tables {
// Check if table exists in the database
if let Ok(_cf_handle) = ledger_db.get_cf_handle(&table) {
tables_to_drop.push(table);
}
}
Ok(())
}
}
6 changes: 5 additions & 1 deletion crates/light-client-prover/src/db_migrations/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@ use std::sync::OnceLock;

use sov_db::ledger_db::migrations::LedgerMigration;

mod remove_unused_common_tables;

use remove_unused_common_tables::RemoveUnusedTables;

pub fn migrations() -> &'static Vec<Box<dyn LedgerMigration + Send + Sync + 'static>> {
static MIGRATIONS: OnceLock<Vec<Box<dyn LedgerMigration + Send + Sync + 'static>>> =
OnceLock::new();
MIGRATIONS.get_or_init(Vec::new)
MIGRATIONS.get_or_init(|| vec![Box::new(RemoveUnusedTables {})])
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use std::sync::Arc;

use sov_db::ledger_db::migrations::{LedgerMigration, MigrationName, MigrationVersion};
use sov_db::ledger_db::LedgerDB;
use sov_db::schema::tables::{LEDGER_TABLES, LIGHT_CLIENT_PROVER_LEDGER_TABLES};

/// Table removal migration
/// tables BatchByNumber and SlotByNumber are removed
pub(crate) struct RemoveUnusedTables {}

impl LedgerMigration for RemoveUnusedTables {
fn identifier(&self) -> (MigrationName, MigrationVersion) {
("RemoveUnusedTables".to_owned(), 3)
}

fn execute(
&self,
ledger_db: Arc<LedgerDB>,
tables_to_drop: &mut Vec<String>,
) -> anyhow::Result<()> {
// Get difference of LEDGER_TABLES and SEQUENCER_LEDGER_TABLES and drop them
let mut diff = LEDGER_TABLES.to_vec();
diff.retain(|x| !LIGHT_CLIENT_PROVER_LEDGER_TABLES.contains(x));
let diff_tables = diff.iter().map(|x| x.to_string()).collect::<Vec<_>>();
for table in diff_tables {
// Check if table exists in the database
if let Ok(_cf_handle) = ledger_db.get_cf_handle(&table) {
tables_to_drop.push(table);
}
}
Ok(())
}
}
3 changes: 3 additions & 0 deletions crates/sequencer/src/db_migrations/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ use std::sync::OnceLock;
use sov_db::ledger_db::migrations::LedgerMigration;

use crate::db_migrations::batch_and_slot_by_number::MigrateBatchAndSlotByNumber;
use crate::db_migrations::remove_unused_common_tables::RemoveUnusedTables;
use crate::db_migrations::verified_proofs::MigrateVerifiedProofsBySlotNumber;

mod batch_and_slot_by_number;
mod remove_unused_common_tables;
mod verified_proofs;

pub fn migrations() -> &'static Vec<Box<dyn LedgerMigration + Send + Sync + 'static>> {
Expand All @@ -15,6 +17,7 @@ pub fn migrations() -> &'static Vec<Box<dyn LedgerMigration + Send + Sync + 'sta
vec![
Box::new(MigrateVerifiedProofsBySlotNumber {}),
Box::new(MigrateBatchAndSlotByNumber {}),
Box::new(RemoveUnusedTables {}),
]
})
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use std::sync::Arc;

use sov_db::ledger_db::migrations::{LedgerMigration, MigrationName, MigrationVersion};
use sov_db::ledger_db::LedgerDB;
use sov_db::schema::tables::{LEDGER_TABLES, SEQUENCER_LEDGER_TABLES};

/// Table removal migration
/// tables BatchByNumber and SlotByNumber are removed
pub(crate) struct RemoveUnusedTables {}

impl LedgerMigration for RemoveUnusedTables {
fn identifier(&self) -> (MigrationName, MigrationVersion) {
("RemoveUnusedTables".to_owned(), 3)
}

fn execute(
&self,
ledger_db: Arc<LedgerDB>,
tables_to_drop: &mut Vec<String>,
) -> anyhow::Result<()> {
// Get difference of LEDGER_TABLES and SEQUENCER_LEDGER_TABLES and drop them
let mut diff = LEDGER_TABLES.to_vec();
diff.retain(|x| !SEQUENCER_LEDGER_TABLES.contains(x));
let diff_tables = diff.iter().map(|x| x.to_string()).collect::<Vec<_>>();
for table in diff_tables {
// Check if table exists in the database
if let Ok(_cf_handle) = ledger_db.get_cf_handle(&table) {
tables_to_drop.push(table);
}
}
Ok(())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,11 @@ impl<'a> LedgerDBMigrator<'a> {
}

/// Run migrations
pub fn migrate(&self, max_open_files: Option<i32>) -> anyhow::Result<()> {
pub fn migrate(
&self,
max_open_files: Option<i32>,
node_column_families: Vec<String>,
) -> anyhow::Result<()> {
if self.migrations.is_empty() {
return Ok(());
}
Expand All @@ -68,8 +72,11 @@ impl<'a> LedgerDBMigrator<'a> {
if !dbs_path.join(LEDGER_DB_PATH_SUFFIX).exists() {
// If this is the first time the ledger db is being created, then we don't need to run migrations
// all migrations up to this point are considered successful
let ledger_db =
LedgerDB::with_config(&RocksdbConfig::new(self.ledger_path, max_open_files, None))?;
let ledger_db = LedgerDB::with_config(&RocksdbConfig::new(
self.ledger_path,
max_open_files,
Some(node_column_families),
))?;

for migration in self.migrations.iter() {
ledger_db
Expand All @@ -83,7 +90,8 @@ impl<'a> LedgerDBMigrator<'a> {

let column_families_in_db = list_column_families(self.ledger_path);

let all_column_families = merge_column_families(column_families_in_db);
let all_column_families =
merge_column_families(column_families_in_db, node_column_families);

let ledger_db = LedgerDB::with_config(&RocksdbConfig::new(
self.ledger_path,
Expand Down Expand Up @@ -210,10 +218,13 @@ pub fn copy_db_dir_recursive(src: &Path, dst: &Path) -> std::io::Result<()> {
Ok(())
}

fn merge_column_families(column_families_in_db: Vec<String>) -> Vec<String> {
let column_families: HashSet<String> = LEDGER_TABLES
fn merge_column_families(
column_families_in_db: Vec<String>,
node_column_families: Vec<String>,
) -> Vec<String> {
let column_families: HashSet<String> = node_column_families
.iter()
.map(|&table_name| table_name.to_string())
.map(|table_name| table_name.to_string())
.chain(column_families_in_db)
.collect();
column_families.into_iter().collect()
Expand Down
Loading
Loading