-
Notifications
You must be signed in to change notification settings - Fork 146
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #641 from drmingdrmer/20-snapshot-handler
refactor: move snapshot handling from Engine to SnapshotHandler
- Loading branch information
Showing
6 changed files
with
178 additions
and
140 deletions.
There are no files selected for viewing
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,2 @@ | ||
pub(crate) mod snapshot_handler; | ||
pub(crate) mod vote_handler; |
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,157 @@ | ||
use crate::engine::engine_impl::EngineOutput; | ||
use crate::raft_state::LogStateReader; | ||
use crate::summary::MessageSummary; | ||
use crate::Node; | ||
use crate::NodeId; | ||
use crate::RaftState; | ||
use crate::SnapshotMeta; | ||
|
||
/// Handle raft vote related operations | ||
pub(crate) struct SnapshotHandler<'st, 'out, NID, N> | ||
where | ||
NID: NodeId, | ||
N: Node, | ||
{ | ||
pub(crate) state: &'st mut RaftState<NID, N>, | ||
pub(crate) output: &'out mut EngineOutput<NID, N>, | ||
} | ||
|
||
impl<'st, 'out, NID, N> SnapshotHandler<'st, 'out, NID, N> | ||
where | ||
NID: NodeId, | ||
N: Node, | ||
{ | ||
/// Update engine state when a new snapshot is built or installed. | ||
/// | ||
/// Engine records only the metadata of a snapshot. Snapshot data is stored by RaftStorage implementation. | ||
#[tracing::instrument(level = "debug", skip_all)] | ||
pub(crate) fn update_snapshot(&mut self, meta: SnapshotMeta<NID, N>) -> bool { | ||
tracing::info!("update_snapshot: {:?}", meta); | ||
|
||
if meta.last_log_id <= self.state.snapshot_last_log_id().copied() { | ||
tracing::info!( | ||
"No need to install a smaller snapshot: current snapshot last_log_id({}), new snapshot last_log_id({})", | ||
self.state.snapshot_last_log_id().summary(), | ||
meta.last_log_id.summary() | ||
); | ||
return false; | ||
} | ||
|
||
self.state.snapshot_meta = meta; | ||
self.output.metrics_flags.set_data_changed(); | ||
|
||
true | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use maplit::btreeset; | ||
use pretty_assertions::assert_eq; | ||
|
||
use crate::engine::Engine; | ||
use crate::EffectiveMembership; | ||
use crate::LeaderId; | ||
use crate::LogId; | ||
use crate::Membership; | ||
use crate::MetricsChangeFlags; | ||
use crate::SnapshotMeta; | ||
|
||
fn log_id(term: u64, index: u64) -> LogId<u64> { | ||
LogId::<u64> { | ||
leader_id: LeaderId { term, node_id: 1 }, | ||
index, | ||
} | ||
} | ||
|
||
fn m12() -> Membership<u64, ()> { | ||
Membership::<u64, ()>::new(vec![btreeset! {1,2}], None) | ||
} | ||
|
||
fn m1234() -> Membership<u64, ()> { | ||
Membership::<u64, ()>::new(vec![btreeset! {1,2,3,4}], None) | ||
} | ||
|
||
fn eng() -> Engine<u64, ()> { | ||
let mut eng = Engine::<u64, ()> { ..Default::default() }; | ||
|
||
eng.state.snapshot_meta = SnapshotMeta { | ||
last_log_id: Some(log_id(2, 2)), | ||
last_membership: EffectiveMembership::new(Some(log_id(1, 1)), m12()), | ||
snapshot_id: "1-2-3-4".to_string(), | ||
}; | ||
eng | ||
} | ||
|
||
#[test] | ||
fn test_update_snapshot_no_update() -> anyhow::Result<()> { | ||
// snapshot will not be updated because of equal or less `last_log_id`. | ||
let mut eng = eng(); | ||
|
||
let got = eng.snapshot_handler().update_snapshot(SnapshotMeta { | ||
last_log_id: Some(log_id(2, 2)), | ||
last_membership: EffectiveMembership::new(Some(log_id(1, 1)), m1234()), | ||
snapshot_id: "1-2-3-4".to_string(), | ||
}); | ||
|
||
assert_eq!(false, got); | ||
|
||
assert_eq!( | ||
SnapshotMeta { | ||
last_log_id: Some(log_id(2, 2)), | ||
last_membership: EffectiveMembership::new(Some(log_id(1, 1)), m12()), | ||
snapshot_id: "1-2-3-4".to_string(), | ||
}, | ||
eng.state.snapshot_meta | ||
); | ||
|
||
assert_eq!( | ||
MetricsChangeFlags { | ||
replication: false, | ||
local_data: false, | ||
cluster: false, | ||
}, | ||
eng.output.metrics_flags | ||
); | ||
|
||
assert_eq!(0, eng.output.commands.len()); | ||
|
||
Ok(()) | ||
} | ||
|
||
#[test] | ||
fn test_update_snapshot_updated() -> anyhow::Result<()> { | ||
// snapshot will be updated to a new one with greater `last_log_id`. | ||
let mut eng = eng(); | ||
|
||
let got = eng.snapshot_handler().update_snapshot(SnapshotMeta { | ||
last_log_id: Some(log_id(2, 3)), | ||
last_membership: EffectiveMembership::new(Some(log_id(2, 2)), m1234()), | ||
snapshot_id: "1-2-3-4".to_string(), | ||
}); | ||
|
||
assert_eq!(true, got); | ||
|
||
assert_eq!( | ||
SnapshotMeta { | ||
last_log_id: Some(log_id(2, 3)), | ||
last_membership: EffectiveMembership::new(Some(log_id(2, 2)), m1234()), | ||
snapshot_id: "1-2-3-4".to_string(), | ||
}, | ||
eng.state.snapshot_meta | ||
); | ||
|
||
assert_eq!( | ||
MetricsChangeFlags { | ||
replication: false, | ||
local_data: true, | ||
cluster: false, | ||
}, | ||
eng.output.metrics_flags | ||
); | ||
|
||
assert_eq!(0, eng.output.commands.len()); | ||
|
||
Ok(()) | ||
} | ||
} |
File renamed without changes.
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 was deleted.
Oops, something went wrong.