Skip to content

Commit

Permalink
Delete inactive entries after staging commit
Browse files Browse the repository at this point in the history
  • Loading branch information
someone235 committed Oct 24, 2023
1 parent 34df5e6 commit a85268f
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 22 deletions.
12 changes: 11 additions & 1 deletion components/consensusmanager/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ pub trait ConsensusFactory: Sync + Send {

/// Close the factory and cleanup any shared resources used by it
fn close(&self);

fn clean_non_active_consensus_entries(&self);
}

/// Test-only mock factory
Expand All @@ -54,6 +56,10 @@ impl ConsensusFactory for MockFactory {
fn close(&self) {
unimplemented!()
}

fn clean_non_active_consensus_entries(&self) {
unimplemented!()
}
}

/// Defines a trait which handles consensus resets for external parts of the system. We avoid using
Expand Down Expand Up @@ -146,6 +152,10 @@ impl ConsensusManager {
debug!("[Consensus manager] all consensus threads exited");
self.factory.close();
}

pub fn clean_non_active_consensus_entries(&self) {
self.factory.clean_non_active_consensus_entries();
}
}

impl Service for ConsensusManager {
Expand Down Expand Up @@ -185,7 +195,7 @@ impl StagingConsensus {
for handler in handlers {
handler.handle_consensus_reset();
}
// TODO: Delete non active consensus entries
self.manager.clean_non_active_consensus_entries();
}

pub fn cancel(self) {
Expand Down
52 changes: 31 additions & 21 deletions consensus/src/consensus/factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use itertools::Itertools;
use kaspa_consensus_core::config::Config;
use kaspa_consensus_notify::root::ConsensusNotificationRoot;
use kaspa_consensusmanager::{ConsensusFactory, ConsensusInstance, DynConsensusCtl, SessionLock};
use kaspa_core::{debug, time::unix_now};
use kaspa_core::{debug, info, time::unix_now};
use kaspa_database::{
prelude::{BatchDbWriter, CachedDbAccess, CachedDbItem, DirectDbWriter, StoreError, StoreResult, StoreResultExtensions, DB},
registry::DatabaseStorePrefixes,
Expand Down Expand Up @@ -216,26 +216,6 @@ impl Factory {
factory.clean_non_active_consensus_entries();
factory
}

pub fn clean_non_active_consensus_entries(&self) {
if self.config.is_archival {
return;
}

let mut write_guard = self.management_store.write();
let entries_to_delete = write_guard.iterate_non_active().collect_vec();
for entry_result in entries_to_delete.iter() {
let entry = entry_result.as_ref().unwrap();
let dir = self.db_root_dir.join(entry.directory_name.clone());
if dir.exists() {
fs::remove_dir_all(dir).unwrap();
}
}

for entry_result in entries_to_delete {
write_guard.delete_entry(entry_result.unwrap()).unwrap();
}
}
}

impl ConsensusFactory for Factory {
Expand Down Expand Up @@ -307,4 +287,34 @@ impl ConsensusFactory for Factory {
debug!("Consensus factory: closing");
self.notification_root.close();
}

fn clean_non_active_consensus_entries(&self) {
if self.config.is_archival {
return;
}

let mut write_guard = self.management_store.write();
let entries_to_delete = write_guard
.iterate_non_active()
.filter_map(|entry_result| {
let entry = entry_result.unwrap();
let dir = self.db_root_dir.join(entry.directory_name.clone());
if dir.exists() {
match fs::remove_dir_all(dir) {
Ok(_) => Some(entry),
Err(e) => {
info!("Couldn't delete consensus entry {}: {}", entry.key, e);
None
}
}
} else {
Some(entry)
}
})
.collect_vec();

for entry in entries_to_delete {
write_guard.delete_entry(entry).unwrap();
}
}
}
4 changes: 4 additions & 0 deletions consensus/src/consensus/test_consensus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,4 +282,8 @@ impl ConsensusFactory for TestConsensusFactory {
fn close(&self) {
self.tc.notification_root().close();
}

fn clean_non_active_consensus_entries(&self) {
unimplemented!()
}
}

0 comments on commit a85268f

Please sign in to comment.