-
Notifications
You must be signed in to change notification settings - Fork 18
feat: Add methods for repairing hot/cold repositories #255
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
aawsome
wants to merge
2
commits into
main
Choose a base branch
from
repair-hotcold
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
pub mod hotcold; | ||
pub mod index; | ||
pub mod snapshots; |
This file contains hidden or 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,253 @@ | ||
use std::collections::{BTreeMap, BTreeSet}; | ||
|
||
use log::{debug, info, warn}; | ||
|
||
use crate::{ | ||
backend::decrypt::DecryptReadBackend, | ||
repofile::{BlobType, IndexFile, PackId}, | ||
repository::Open, | ||
ErrorKind, FileType, Id, Progress, ProgressBars, ReadBackend, Repository, RusticError, | ||
RusticResult, WriteBackend, ALL_FILE_TYPES, | ||
}; | ||
|
||
/// Repairs a hot/cold repository by copying missing files (except pack files) over from one to the other part. | ||
/// | ||
/// # Type Parameters | ||
/// | ||
/// * `P` - The progress bar type. | ||
/// * `S` - The state the repository is in. | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `repo` - The repository | ||
/// * `dry_run` - Do a dry run | ||
pub(crate) fn repair_hotcold<P: ProgressBars, S>( | ||
aawsome marked this conversation as resolved.
Show resolved
Hide resolved
|
||
repo: &Repository<P, S>, | ||
dry_run: bool, | ||
) -> RusticResult<()> { | ||
for file_type in ALL_FILE_TYPES { | ||
if file_type != FileType::Pack { | ||
correct_missing_files(repo, file_type, |_| true, dry_run)?; | ||
} | ||
} | ||
Ok(()) | ||
} | ||
|
||
/// Repairs a hot/cold repository by copying missing tree pack files over from one to the other part. | ||
/// | ||
/// # Type Parameters | ||
/// | ||
/// * `P` - The progress bar type. | ||
/// * `S` - The state the repository is in. | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `repo` - The repository | ||
/// * `dry_run` - Do a dry run | ||
pub(crate) fn repair_hotcold_packs<P: ProgressBars, S: Open>( | ||
aawsome marked this conversation as resolved.
Show resolved
Hide resolved
|
||
repo: &Repository<P, S>, | ||
dry_run: bool, | ||
) -> RusticResult<()> { | ||
let tree_packs = get_tree_packs(repo)?; | ||
correct_missing_files( | ||
repo, | ||
FileType::Pack, | ||
|id| tree_packs.contains(&PackId::from(*id)), | ||
dry_run, | ||
) | ||
} | ||
|
||
/// Copy relevant+misssing files in a hot/cold repository from one to the other part. | ||
/// | ||
/// # Type Parameters | ||
/// | ||
/// * `P` - The progress bar type. | ||
/// * `S` - The state the repository is in. | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `repo` - The repository | ||
/// * `file_type` - The filetype to copy | ||
/// * `is_relevalt` - A closure to determine whether the id is relevat | ||
/// * `dry_run` - Do a dry run | ||
pub(crate) fn correct_missing_files<P: ProgressBars, S>( | ||
aawsome marked this conversation as resolved.
Show resolved
Hide resolved
|
||
repo: &Repository<P, S>, | ||
file_type: FileType, | ||
is_relevant: impl Fn(&Id) -> bool, | ||
dry_run: bool, | ||
) -> RusticResult<()> { | ||
let Some(repo_hot) = &repo.be_hot else { | ||
return Err(RusticError::new( | ||
ErrorKind::Repository, | ||
"Repository is no hot/cold repository.", | ||
)); | ||
}; | ||
|
||
let (missing_hot, missing_hot_size, missing_cold, missing_cold_size) = | ||
get_missing_files(repo, file_type, is_relevant)?; | ||
|
||
// copy missing files from hot to cold repo | ||
if !missing_cold.is_empty() { | ||
if dry_run { | ||
info!( | ||
"would have copied {} hot {file_type:?} files to cold", | ||
missing_cold.len() | ||
); | ||
debug!("files: {missing_cold:?}"); | ||
} else { | ||
let p = repo | ||
.pb | ||
.progress_bytes(format!("copying missing cold {file_type:?} files...")); | ||
p.set_length(missing_cold_size); | ||
copy(missing_cold, file_type, repo_hot, &repo.be_cold)?; | ||
p.finish(); | ||
} | ||
} | ||
|
||
if !missing_hot.is_empty() { | ||
if dry_run { | ||
info!( | ||
"would have copied {} cold {file_type:?} files to hot", | ||
missing_hot.len() | ||
); | ||
debug!("files: {missing_hot:?}"); | ||
} else { | ||
// TODO: warm-up | ||
// copy missing files from cold to hot repo | ||
let p = repo | ||
.pb | ||
.progress_bytes(format!("copying missing hot {file_type:?} files...")); | ||
p.set_length(missing_hot_size); | ||
copy(missing_hot, file_type, &repo.be_cold, repo_hot)?; | ||
p.finish(); | ||
} | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
/// Copy a list of files from one repo to another. | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `files` - The list of file ids to copy | ||
/// * `file_type` - The filetype to copy | ||
/// * `from` - The backend to read from | ||
/// * `to` - The backend to write to | ||
fn copy( | ||
files: Vec<Id>, | ||
file_type: FileType, | ||
from: &impl ReadBackend, | ||
to: &impl WriteBackend, | ||
) -> RusticResult<()> { | ||
for id in files { | ||
let file = from.read_full(file_type, &id)?; | ||
to.write_bytes(file_type, &id, false, file)?; | ||
} | ||
Ok(()) | ||
} | ||
|
||
/// Get all tree packs from from within the repository. | ||
/// | ||
/// # Type Parameters | ||
/// | ||
/// * `P` - The progress bar type. | ||
/// * `S` - The state the repository is in. | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `repo` - The repository | ||
/// | ||
/// # Returns | ||
/// | ||
/// The set of pack ids. | ||
pub(crate) fn get_tree_packs<P: ProgressBars, S: Open>( | ||
aawsome marked this conversation as resolved.
Show resolved
Hide resolved
|
||
repo: &Repository<P, S>, | ||
) -> RusticResult<BTreeSet<PackId>> { | ||
let p = repo.pb.progress_counter("reading index..."); | ||
let mut tree_packs = BTreeSet::new(); | ||
for index in repo.dbe().stream_all::<IndexFile>(&p)? { | ||
let index = index?.1; | ||
for (p, _) in index.all_packs() { | ||
let blob_type = p.blob_type(); | ||
if blob_type == BlobType::Tree { | ||
_ = tree_packs.insert(p.id); | ||
} | ||
} | ||
} | ||
Ok(tree_packs) | ||
} | ||
|
||
/// Find missing files in the hot or cold part of the repository. | ||
/// | ||
/// # Type Parameters | ||
/// | ||
/// * `P` - The progress bar type. | ||
/// * `S` - The state the repository is in. | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `repo` - The repository | ||
/// * `file_type` - The filetype to use | ||
/// * `is_relevalt` - A closure to determine whether the id is relevat | ||
/// | ||
/// # Returns | ||
/// | ||
/// A tuple containing ids missing in hot part, the total size, ids missing in cold part and the corresponding total size. | ||
pub(crate) fn get_missing_files<P: ProgressBars, S>( | ||
aawsome marked this conversation as resolved.
Show resolved
Hide resolved
|
||
repo: &Repository<P, S>, | ||
file_type: FileType, | ||
is_relevant: impl Fn(&Id) -> bool, | ||
) -> RusticResult<(Vec<Id>, u64, Vec<Id>, u64)> { | ||
let Some(repo_hot) = &repo.be_hot else { | ||
return Err(RusticError::new( | ||
ErrorKind::Repository, | ||
"Repository is no hot/cold repository.", | ||
)); | ||
}; | ||
|
||
let p = repo | ||
.pb | ||
.progress_spinner(format!("listing hot {file_type:?} files...")); | ||
let hot_files: BTreeMap<_, _> = repo_hot.list_with_size(file_type)?.into_iter().collect(); | ||
p.finish(); | ||
|
||
let p = repo | ||
.pb | ||
.progress_spinner(format!("listing cold {file_type:?} files...")); | ||
let cold_files: BTreeMap<_, _> = repo | ||
.be_cold | ||
.list_with_size(file_type)? | ||
.into_iter() | ||
.collect(); | ||
p.finish(); | ||
|
||
let common: BTreeSet<_> = hot_files | ||
.iter() | ||
.filter_map(|(id, size_hot)| match cold_files.get(id) { | ||
Some(size_cold) if size_cold == size_hot => Some(*id), | ||
Some(size_cold) => { | ||
warn!("sizes mismatch: type {file_type:?}, id: {id}, size hot: {size_hot}, size cold: {size_cold}. Ignoring..."); | ||
None | ||
} | ||
None => None, | ||
}) | ||
.collect(); | ||
|
||
let retain = |files: BTreeMap<_, _>| { | ||
let mut retain_size: u64 = 0; | ||
let only: Vec<_> = files | ||
.into_iter() | ||
.filter(|(id, _)| !common.contains(id) && is_relevant(id)) | ||
.map(|(id, size)| { | ||
retain_size += u64::from(size); | ||
id | ||
}) | ||
.collect(); | ||
(only, retain_size) | ||
}; | ||
|
||
let (cold_only, cold_only_size) = retain(cold_files); | ||
let (hot_only, hot_only_size) = retain(hot_files); | ||
Ok((cold_only, cold_only_size, hot_only, hot_only_size)) | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.