diff --git a/beacon_node/beacon_chain/src/beacon_chain.rs b/beacon_node/beacon_chain/src/beacon_chain.rs index f73c17f79c..24f83179f6 100644 --- a/beacon_node/beacon_chain/src/beacon_chain.rs +++ b/beacon_node/beacon_chain/src/beacon_chain.rs @@ -1711,6 +1711,10 @@ impl BeaconChain { } } + pub fn manually_compact_database(&self) { + self.store_migrator.process_manual_compaction(); + } + pub fn manually_finalize_state( &self, state_root: Hash256, diff --git a/beacon_node/beacon_chain/src/migrate.rs b/beacon_node/beacon_chain/src/migrate.rs index d4ee0fc7c1..c7b4ba0796 100644 --- a/beacon_node/beacon_chain/src/migrate.rs +++ b/beacon_node/beacon_chain/src/migrate.rs @@ -125,6 +125,7 @@ pub enum Notification { Reconstruction, PruneBlobs(Epoch), ManualFinalization(ManualFinalizationNotification), + ManualCompaction, } pub struct ManualFinalizationNotification { @@ -198,6 +199,14 @@ impl, Cold: ItemStore> BackgroundMigrator, Cold: ItemStore> BackgroundMigrator>, log: &Logger) { + debug!(log, "Running manual compaction"); + if let Err(e) = db.compact() { + warn!(log, "Database compaction failed"; "error" => format!("{:?}", e)); + } else { + debug!(log, "Manual compaction completed"); + } + } + /// Spawn a new child thread to run the migration process. /// /// Return a channel handle for sending requests to the thread. @@ -460,17 +478,20 @@ impl, Cold: ItemStore> BackgroundMigrator reconstruction_notif = Some(notif), Notification::Finalization(fin) => finalization_notif = Some(fin), Notification::ManualFinalization(fin) => manual_finalization_notif = Some(fin), Notification::PruneBlobs(dab) => prune_blobs_notif = Some(dab), + Notification::ManualCompaction => manual_compaction_notif = Some(notif), } // Read the rest of the messages in the channel, taking the best of each type. for notif in rx.try_iter() { match notif { Notification::Reconstruction => reconstruction_notif = Some(notif), + Notification::ManualCompaction => manual_compaction_notif = Some(notif), Notification::ManualFinalization(fin) => { if let Some(current) = manual_finalization_notif.as_mut() { if fin.checkpoint.epoch > current.checkpoint.epoch { @@ -511,6 +532,9 @@ impl, Cold: ItemStore> BackgroundMigrator( }, ); + // POST lighthouse/compaction + let post_lighthouse_compaction = warp::path("lighthouse") + .and(warp::path("compaction")) + .and(warp::path::end()) + .and(task_spawner_filter.clone()) + .and(chain_filter.clone()) + .then( + |task_spawner: TaskSpawner, chain: Arc>| { + task_spawner.blocking_json_task(Priority::P0, move || { + chain.manually_compact_database(); + Ok(api_types::GenericResponse::from(String::from( + "Triggered manual compaction", + ))) + }) + }, + ); + // POST lighthouse/liveness let post_lighthouse_liveness = warp::path("lighthouse") .and(warp::path("liveness")) @@ -4878,6 +4895,7 @@ pub fn serve( .uor(post_lighthouse_ui_validator_metrics) .uor(post_lighthouse_ui_validator_info) .uor(post_lighthouse_finalize) + .uor(post_lighthouse_compaction) .recover(warp_utils::reject::handle_rejection), ), )