-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
A0-3137: Split backup into submodules (#338)
- Loading branch information
Showing
9 changed files
with
142 additions
and
125 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
use codec::{Decode, Encode}; | ||
use std::fmt::Debug; | ||
|
||
pub use loader::{BackupLoader, LoadedData}; | ||
pub use saver::BackupSaver; | ||
|
||
use crate::{alerts::AlertData, units::UncheckedSignedUnit, Data, Hasher, MultiKeychain}; | ||
|
||
mod loader; | ||
mod saver; | ||
|
||
#[derive(Clone, Debug, Decode, Encode, PartialEq)] | ||
pub enum BackupItem<H: Hasher, D: Data, MK: MultiKeychain> { | ||
Unit(UncheckedSignedUnit<H, D, MK::Signature>), | ||
AlertData(AlertData<H, D, MK>), | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
use crate::{ | ||
units::UncheckedSignedUnit, Data, Hasher, MultiKeychain, Receiver, Sender, Terminator, | ||
}; | ||
|
||
use crate::alerts::AlertData; | ||
use codec::Encode; | ||
use futures::{FutureExt, StreamExt}; | ||
|
||
use crate::backup::BackupItem; | ||
use log::{debug, error}; | ||
use std::io::Write; | ||
|
||
const LOG_TARGET: &str = "AlephBFT-backup-saver"; | ||
|
||
/// Component responsible for saving units and alert data into backup. | ||
/// It waits for items to appear on its receivers, and writes them to backup. | ||
/// It announces a successful write through an appropriate response sender. | ||
pub struct BackupSaver<H: Hasher, D: Data, MK: MultiKeychain, W: Write> { | ||
units_from_runway: Receiver<UncheckedSignedUnit<H, D, MK::Signature>>, | ||
data_from_alerter: Receiver<AlertData<H, D, MK>>, | ||
responses_for_runway: Sender<UncheckedSignedUnit<H, D, MK::Signature>>, | ||
responses_for_alerter: Sender<AlertData<H, D, MK>>, | ||
backup: W, | ||
} | ||
|
||
impl<H: Hasher, D: Data, MK: MultiKeychain, W: Write> BackupSaver<H, D, MK, W> { | ||
pub fn new( | ||
units_from_runway: Receiver<UncheckedSignedUnit<H, D, MK::Signature>>, | ||
data_from_alerter: Receiver<AlertData<H, D, MK>>, | ||
responses_for_runway: Sender<UncheckedSignedUnit<H, D, MK::Signature>>, | ||
responses_for_alerter: Sender<AlertData<H, D, MK>>, | ||
backup: W, | ||
) -> BackupSaver<H, D, MK, W> { | ||
BackupSaver { | ||
units_from_runway, | ||
data_from_alerter, | ||
responses_for_runway, | ||
responses_for_alerter, | ||
backup, | ||
} | ||
} | ||
|
||
pub fn save_item(&mut self, item: BackupItem<H, D, MK>) -> Result<(), std::io::Error> { | ||
self.backup.write_all(&item.encode())?; | ||
self.backup.flush()?; | ||
Ok(()) | ||
} | ||
|
||
pub async fn run(&mut self, mut terminator: Terminator) { | ||
let mut terminator_exit = false; | ||
loop { | ||
futures::select! { | ||
unit = self.units_from_runway.next() => { | ||
let unit = match unit { | ||
Some(unit) => unit, | ||
None => { | ||
error!(target: LOG_TARGET, "receiver of units to save closed early"); | ||
break; | ||
}, | ||
}; | ||
let item = BackupItem::Unit(unit.clone()); | ||
if let Err(e) = self.save_item(item) { | ||
error!(target: LOG_TARGET, "couldn't save item to backup: {:?}", e); | ||
break; | ||
} | ||
if self.responses_for_runway.unbounded_send(unit).is_err() { | ||
error!(target: LOG_TARGET, "couldn't respond with saved unit to runway"); | ||
break; | ||
} | ||
}, | ||
data = self.data_from_alerter.next() => { | ||
let data = match data { | ||
Some(data) => data, | ||
None => { | ||
error!(target: LOG_TARGET, "receiver of alert data to save closed early"); | ||
break; | ||
}, | ||
}; | ||
let item = BackupItem::AlertData(data.clone()); | ||
if let Err(e) = self.save_item(item) { | ||
error!(target: LOG_TARGET, "couldn't save item to backup: {:?}", e); | ||
break; | ||
} | ||
if self.responses_for_alerter.unbounded_send(data).is_err() { | ||
error!(target: LOG_TARGET, "couldn't respond with saved alert data to runway"); | ||
break; | ||
} | ||
} | ||
_ = terminator.get_exit().fuse() => { | ||
debug!(target: LOG_TARGET, "backup saver received exit signal."); | ||
terminator_exit = true; | ||
} | ||
} | ||
|
||
if terminator_exit { | ||
debug!(target: LOG_TARGET, "backup saver decided to exit."); | ||
terminator.terminate_sync().await; | ||
break; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ mod terminal; | |
mod terminator; | ||
mod units; | ||
|
||
mod backup; | ||
mod task_queue; | ||
#[cfg(test)] | ||
mod testing; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters