Skip to content

Commit ed1b768

Browse files
authored
Manual compaction endpoint backport (#7104)
Backports: - #7072 To: - #7039 #7103 should be merged first This PR introduces an endpoint that allows users to manually trigger background compaction.
1 parent 27aabe8 commit ed1b768

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

beacon_node/beacon_chain/src/beacon_chain.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1711,6 +1711,10 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
17111711
}
17121712
}
17131713

1714+
pub fn manually_compact_database(&self) {
1715+
self.store_migrator.process_manual_compaction();
1716+
}
1717+
17141718
pub fn manually_finalize_state(
17151719
&self,
17161720
state_root: Hash256,

beacon_node/beacon_chain/src/migrate.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ pub enum Notification {
125125
Reconstruction,
126126
PruneBlobs(Epoch),
127127
ManualFinalization(ManualFinalizationNotification),
128+
ManualCompaction,
128129
}
129130

130131
pub struct ManualFinalizationNotification {
@@ -198,6 +199,14 @@ impl<E: EthSpec, Hot: ItemStore<E>, Cold: ItemStore<E>> BackgroundMigrator<E, Ho
198199
Ok(())
199200
}
200201

202+
pub fn process_manual_compaction(&self) {
203+
if let Some(Notification::ManualCompaction) =
204+
self.send_background_notification(Notification::ManualCompaction)
205+
{
206+
Self::run_manual_compaction(self.db.clone(), &self.log);
207+
}
208+
}
209+
201210
pub fn process_manual_finalization(&self, notif: ManualFinalizationNotification) {
202211
if let Some(Notification::ManualFinalization(notif)) =
203212
self.send_background_notification(Notification::ManualFinalization(notif))
@@ -446,6 +455,15 @@ impl<E: EthSpec, Hot: ItemStore<E>, Cold: ItemStore<E>> BackgroundMigrator<E, Ho
446455
debug!(log, "Database consolidation complete");
447456
}
448457

458+
fn run_manual_compaction(db: Arc<HotColdDB<E, Hot, Cold>>, log: &Logger) {
459+
debug!(log, "Running manual compaction");
460+
if let Err(e) = db.compact() {
461+
warn!(log, "Database compaction failed"; "error" => format!("{:?}", e));
462+
} else {
463+
debug!(log, "Manual compaction completed");
464+
}
465+
}
466+
449467
/// Spawn a new child thread to run the migration process.
450468
///
451469
/// Return a channel handle for sending requests to the thread.
@@ -460,17 +478,20 @@ impl<E: EthSpec, Hot: ItemStore<E>, Cold: ItemStore<E>> BackgroundMigrator<E, Ho
460478
let mut reconstruction_notif = None;
461479
let mut finalization_notif = None;
462480
let mut manual_finalization_notif = None;
481+
let mut manual_compaction_notif = None;
463482
let mut prune_blobs_notif = None;
464483
match notif {
465484
Notification::Reconstruction => reconstruction_notif = Some(notif),
466485
Notification::Finalization(fin) => finalization_notif = Some(fin),
467486
Notification::ManualFinalization(fin) => manual_finalization_notif = Some(fin),
468487
Notification::PruneBlobs(dab) => prune_blobs_notif = Some(dab),
488+
Notification::ManualCompaction => manual_compaction_notif = Some(notif),
469489
}
470490
// Read the rest of the messages in the channel, taking the best of each type.
471491
for notif in rx.try_iter() {
472492
match notif {
473493
Notification::Reconstruction => reconstruction_notif = Some(notif),
494+
Notification::ManualCompaction => manual_compaction_notif = Some(notif),
474495
Notification::ManualFinalization(fin) => {
475496
if let Some(current) = manual_finalization_notif.as_mut() {
476497
if fin.checkpoint.epoch > current.checkpoint.epoch {
@@ -511,6 +532,9 @@ impl<E: EthSpec, Hot: ItemStore<E>, Cold: ItemStore<E>> BackgroundMigrator<E, Ho
511532
if reconstruction_notif.is_some() {
512533
Self::run_reconstruction(db.clone(), Some(inner_tx.clone()), &log);
513534
}
535+
if manual_compaction_notif.is_some() {
536+
Self::run_manual_compaction(db.clone(), &log);
537+
}
514538
}
515539
});
516540
(tx, thread)

beacon_node/http_api/src/lib.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4105,6 +4105,23 @@ pub fn serve<T: BeaconChainTypes>(
41054105
},
41064106
);
41074107

4108+
// POST lighthouse/compaction
4109+
let post_lighthouse_compaction = warp::path("lighthouse")
4110+
.and(warp::path("compaction"))
4111+
.and(warp::path::end())
4112+
.and(task_spawner_filter.clone())
4113+
.and(chain_filter.clone())
4114+
.then(
4115+
|task_spawner: TaskSpawner<T::EthSpec>, chain: Arc<BeaconChain<T>>| {
4116+
task_spawner.blocking_json_task(Priority::P0, move || {
4117+
chain.manually_compact_database();
4118+
Ok(api_types::GenericResponse::from(String::from(
4119+
"Triggered manual compaction",
4120+
)))
4121+
})
4122+
},
4123+
);
4124+
41084125
// POST lighthouse/liveness
41094126
let post_lighthouse_liveness = warp::path("lighthouse")
41104127
.and(warp::path("liveness"))
@@ -4878,6 +4895,7 @@ pub fn serve<T: BeaconChainTypes>(
48784895
.uor(post_lighthouse_ui_validator_metrics)
48794896
.uor(post_lighthouse_ui_validator_info)
48804897
.uor(post_lighthouse_finalize)
4898+
.uor(post_lighthouse_compaction)
48814899
.recover(warp_utils::reject::handle_rejection),
48824900
),
48834901
)

0 commit comments

Comments
 (0)