Skip to content

Commit

Permalink
reuse metadata when rebuilding the data.
Browse files Browse the repository at this point in the history
SweepObject already have the metadata of the data,
we can reuse it for data rebuild instead of fetch the metadata again
  • Loading branch information
iwanbk authored and LeeSmet committed Dec 18, 2024
1 parent 1532396 commit 4df08d6
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 11 deletions.
1 change: 1 addition & 0 deletions zstor/src/actors/repairer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ impl Handler<SweepObjects> for RepairActor {
.send(Rebuild {
file: None,
key: Some(key),
metadata: Some(metadata),
})
.await
{
Expand Down
38 changes: 28 additions & 10 deletions zstor/src/actors/zstor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ pub struct Rebuild {
/// for this key. If it exists, the data will be reconstructed according to the new policy,
/// and the old metadata is replaced with the new metadata.
pub key: Option<String>,

/// metadata of the file/key to rebuild
pub metadata: Option<MetaData>,
}

#[derive(Serialize, Deserialize, Debug, Message, Clone)]
Expand Down Expand Up @@ -285,6 +288,7 @@ impl Handler<Rebuild> for ZstorActor {
let pipeline = self.pipeline.clone();
let config = self.cfg.clone();
let meta = self.meta.clone();

AtomicResponse::new(Box::pin(
async move {
let cfg = config.send(GetConfig).await?;
Expand All @@ -300,31 +304,41 @@ impl Handler<Rebuild> for ZstorActor {
std::io::Error::from(std::io::ErrorKind::InvalidInput),
));
}
let old_metadata = if let Some(ref file) = msg.file {
meta.send(LoadMeta { path: file.clone() })

let old_metadata = match (msg.metadata, &msg.file, &msg.key) {
(Some(metadata), _, _) => {
debug!(
"Using provided metadata for rebuild file:{:?} key: {:?}",
&msg.file, &msg.key
);
metadata
}
(None, Some(file), _) => meta
.send(LoadMeta { path: file.clone() })
.await??
.ok_or_else(|| {
ZstorError::new_io(
"no metadata found for file".to_string(),
std::io::Error::from(std::io::ErrorKind::NotFound),
)
})?
} else if let Some(ref key) = msg.key {
// key is set so the unwrap is safe
meta.send(LoadMetaByKey { key: key.clone() })
})?,
(None, None, Some(key)) => meta
.send(LoadMetaByKey { key: key.clone() })
.await??
.ok_or_else(|| {
ZstorError::new_io(
"no metadata found for file".to_string(),
std::io::Error::from(std::io::ErrorKind::NotFound),
)
})?
} else {
unreachable!();
})?,
_ => unreachable!(),
};

// load the data from the storage backends
let input = load_data(&old_metadata).await?;
let existing_data = input.clone();

// rebuild the data (in memory only)
let (mut metadata, shards) = pipeline
.send(RebuildData {
input,
Expand All @@ -333,7 +347,9 @@ impl Handler<Rebuild> for ZstorActor {
})
.await??;

// build a list of the key and the backend used for the shards
// build a list of (key, backend used for the shards)
// - if the shard still exists in the backend, we set the backend to the old backend
// - if the shard is missing, we set the backend to None
let mut used_backends = Vec::new();
for (i, data) in existing_data.iter().enumerate() {
let key = old_metadata.shards()[i].key().to_vec();
Expand Down Expand Up @@ -510,6 +526,8 @@ async fn check_backend_space(
}
}

// Find valid backends for the shards
// if the backend is part of the skip_backends, we don't need to check it again
async fn find_valid_backends(
cfg: &mut Config,
shard_len: usize,
Expand Down
10 changes: 9 additions & 1 deletion zstor/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,15 @@ async fn real_main() -> ZstorResult<()> {
handle_command(ZstorCommand::Retrieve(Retrieve { file }), opts.config).await?
}
Cmd::Rebuild { file, key } => {
handle_command(ZstorCommand::Rebuild(Rebuild { file, key }), opts.config).await?
handle_command(
ZstorCommand::Rebuild(Rebuild {
file,
key,
metadata: None,
}),
opts.config,
)
.await?
}
Cmd::Check { file } => {
handle_command(ZstorCommand::Check(Check { path: file }), opts.config).await?
Expand Down

0 comments on commit 4df08d6

Please sign in to comment.