Skip to content

Commit

Permalink
Make the script not write to db on invalid state.
Browse files Browse the repository at this point in the history
  • Loading branch information
MTRNord committed Apr 18, 2022
1 parent 096708e commit 427a37f
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 19 deletions.
10 changes: 6 additions & 4 deletions src/errors.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
use crate::StateMap;
use string_cache::{Atom, EmptyStaticAtomSet};
use thiserror::Error;

#[derive(Error, Debug)]
pub enum StateCompressorError {
#[error("Missing state_goup: {}", .0)]
#[error("Missing state_goup: {0}")]
MissingStateGroup(i64),
#[error("Missing prev_state_group: {}", .0)]
#[error("Missing prev_state_group: {0}")]
MissingPrevStateGroup(i64),
#[error("States for group {} do not match. Expected {:#?}, found {:#?}", .0, .1, .2)]
#[error("States for group {0} do not match. Expected {1:#?}, found {2:#?}")]
StateMissmatchedForGroup(
i64,
Box<StateMap<Atom<EmptyStaticAtomSet>>>,
Box<StateMap<Atom<EmptyStaticAtomSet>>>,
),
// This recursion is totally safe as we never have more than 2 levels of recursion
#[error("expected state to match: {0}")]
ExpectedStateMismatched(Box<StateCompressorError>),
}
45 changes: 30 additions & 15 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,9 @@ pub fn run(mut config: Config) {
}

if config.verify {
check_that_maps_match(&state_group_map, new_state_group_map);
if let Err(e) = check_that_maps_match(&state_group_map, new_state_group_map) {
panic!("Invalid state found: {}", e);
}
}

// If we are given an output file, we output the changes as SQL. If the
Expand Down Expand Up @@ -591,17 +593,29 @@ pub fn continue_run(
});
}

check_that_maps_match(&state_group_map, new_state_group_map);

database::send_changes_to_db(db_url, room_id, &state_group_map, new_state_group_map);

Some(ChunkStats {
new_level_info: compressor.get_level_info(),
last_compressed_group: max_group_found,
original_num_rows,
new_num_rows,
commited: true,
})
match check_that_maps_match(&state_group_map, new_state_group_map) {
Ok(_) => {
database::send_changes_to_db(db_url, room_id, &state_group_map, new_state_group_map);

Some(ChunkStats {
new_level_info: compressor.get_level_info(),
last_compressed_group: max_group_found,
original_num_rows,
new_num_rows,
commited: true,
})
}
Err(e) => {
warn!("Invalid state found: {}", e);
Some(ChunkStats {
new_level_info: compressor.get_level_info(),
last_compressed_group: max_group_found,
original_num_rows,
new_num_rows,
commited: false,
})
}
}
}

/// Compares two sets of state groups
Expand All @@ -621,7 +635,7 @@ pub fn continue_run(
fn check_that_maps_match(
old_map: &BTreeMap<i64, StateGroupEntry>,
new_map: &BTreeMap<i64, StateGroupEntry>,
) {
) -> std::result::Result<(), StateCompressorError> {
info!("Checking that state maps match...");

let pb: ProgressBar = if cfg!(feature = "no-progress-bars") {
Expand All @@ -646,7 +660,7 @@ fn check_that_maps_match(
pb.inc(1);

if expected != actual {
Err(crate::StateCompressorError::StateMissmatchedForGroup(
Err(StateCompressorError::StateMissmatchedForGroup(
*sg,
Box::new(expected),
Box::new(actual),
Expand All @@ -655,11 +669,12 @@ fn check_that_maps_match(
Ok(())
}
})
.expect("expected state to match");
.map_err(|x| StateCompressorError::ExpectedStateMismatched(Box::new(x)))?;

pb.finish();

info!("New state map matches old one");
Ok(())
}

/// Gets the full state for a given group from the map (of deltas)
Expand Down

0 comments on commit 427a37f

Please sign in to comment.