Skip to content

Commit 5b42f40

Browse files
committed
manual compaction endpoint
1 parent 79cb31a commit 5b42f40

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
@@ -1702,6 +1702,10 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
17021702
}
17031703
}
17041704

1705+
pub fn manually_compact_database(&self) {
1706+
self.store_migrator.process_manual_compaction();
1707+
}
1708+
17051709
pub fn manually_finalize_state(
17061710
&self,
17071711
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))
@@ -445,6 +454,15 @@ impl<E: EthSpec, Hot: ItemStore<E>, Cold: ItemStore<E>> BackgroundMigrator<E, Ho
445454
debug!(log, "Database consolidation complete");
446455
}
447456

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

beacon_node/http_api/src/lib.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4039,6 +4039,23 @@ pub fn serve<T: BeaconChainTypes>(
40394039
},
40404040
);
40414041

4042+
// POST lighthouse/compaction
4043+
let post_lighthouse_compaction = warp::path("lighthouse")
4044+
.and(warp::path("compaction"))
4045+
.and(warp::path::end())
4046+
.and(task_spawner_filter.clone())
4047+
.and(chain_filter.clone())
4048+
.then(
4049+
|task_spawner: TaskSpawner<T::EthSpec>, chain: Arc<BeaconChain<T>>| {
4050+
task_spawner.blocking_json_task(Priority::P0, move || {
4051+
chain.manually_compact_database();
4052+
Ok(api_types::GenericResponse::from(String::from(
4053+
"Triggered manual compaction",
4054+
)))
4055+
})
4056+
},
4057+
);
4058+
40424059
// POST lighthouse/liveness
40434060
let post_lighthouse_liveness = warp::path("lighthouse")
40444061
.and(warp::path("liveness"))
@@ -4810,6 +4827,7 @@ pub fn serve<T: BeaconChainTypes>(
48104827
.uor(post_lighthouse_ui_validator_metrics)
48114828
.uor(post_lighthouse_ui_validator_info)
48124829
.uor(post_lighthouse_finalize)
4830+
.uor(post_lighthouse_compaction)
48134831
.recover(warp_utils::reject::handle_rejection),
48144832
),
48154833
)

0 commit comments

Comments
 (0)