2020-04-23 18:36:12 +00:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
|
|
use tokio::sync::watch;
|
|
|
|
|
2020-07-07 11:59:22 +00:00
|
|
|
use garage_model::garage::Garage;
|
2022-05-10 11:16:57 +00:00
|
|
|
use garage_model::s3::block_ref_table::*;
|
|
|
|
use garage_model::s3::object_table::*;
|
|
|
|
use garage_model::s3::version_table::*;
|
2020-04-24 10:10:01 +00:00
|
|
|
use garage_table::*;
|
|
|
|
use garage_util::error::Error;
|
2020-04-23 18:36:12 +00:00
|
|
|
|
|
|
|
use crate::*;
|
|
|
|
|
|
|
|
pub struct Repair {
|
|
|
|
pub garage: Arc<Garage>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Repair {
|
2021-03-15 22:14:12 +00:00
|
|
|
pub async fn repair_worker(&self, opt: RepairOpt, must_exit: watch::Receiver<bool>) {
|
2021-03-15 19:09:44 +00:00
|
|
|
if let Err(e) = self.repair_worker_aux(opt, must_exit).await {
|
|
|
|
warn!("Repair worker failed with error: {}", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn repair_worker_aux(
|
|
|
|
&self,
|
|
|
|
opt: RepairOpt,
|
|
|
|
must_exit: watch::Receiver<bool>,
|
2020-04-23 18:36:12 +00:00
|
|
|
) -> Result<(), Error> {
|
2021-10-27 08:36:04 +00:00
|
|
|
match opt.what {
|
|
|
|
RepairWhat::Tables => {
|
|
|
|
info!("Launching a full sync of tables");
|
|
|
|
self.garage.bucket_table.syncer.add_full_sync();
|
|
|
|
self.garage.object_table.syncer.add_full_sync();
|
|
|
|
self.garage.version_table.syncer.add_full_sync();
|
|
|
|
self.garage.block_ref_table.syncer.add_full_sync();
|
|
|
|
self.garage.key_table.syncer.add_full_sync();
|
|
|
|
}
|
|
|
|
RepairWhat::Versions => {
|
|
|
|
info!("Repairing the versions table");
|
|
|
|
self.repair_versions(&must_exit).await?;
|
|
|
|
}
|
|
|
|
RepairWhat::BlockRefs => {
|
|
|
|
info!("Repairing the block refs table");
|
|
|
|
self.repair_block_ref(&must_exit).await?;
|
|
|
|
}
|
|
|
|
RepairWhat::Blocks => {
|
|
|
|
info!("Repairing the stored blocks");
|
|
|
|
self.garage
|
|
|
|
.block_manager
|
|
|
|
.repair_data_store(&must_exit)
|
|
|
|
.await?;
|
|
|
|
}
|
2021-11-03 17:28:43 +00:00
|
|
|
RepairWhat::Scrub { tranquility } => {
|
2021-10-27 08:36:04 +00:00
|
|
|
info!("Verifying integrity of stored blocks");
|
|
|
|
self.garage
|
|
|
|
.block_manager
|
2021-11-03 17:28:43 +00:00
|
|
|
.scrub_data_store(&must_exit, tranquility)
|
2021-10-27 08:36:04 +00:00
|
|
|
.await?;
|
|
|
|
}
|
2021-06-23 23:34:28 +00:00
|
|
|
}
|
2020-04-23 18:36:12 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn repair_versions(&self, must_exit: &watch::Receiver<bool>) -> Result<(), Error> {
|
|
|
|
let mut pos = vec![];
|
|
|
|
|
2022-06-03 10:35:12 +00:00
|
|
|
while *must_exit.borrow() {
|
|
|
|
let item_bytes = {
|
|
|
|
let (k, v) = match self.garage.version_table.data.store.get_gt(pos)? {
|
|
|
|
Some(pair) => pair,
|
|
|
|
None => break,
|
|
|
|
};
|
|
|
|
pos = k.into_vec();
|
|
|
|
v.into_vec()
|
|
|
|
};
|
2020-04-23 18:36:12 +00:00
|
|
|
|
2022-06-02 21:14:10 +00:00
|
|
|
let version = rmp_serde::decode::from_read_ref::<_, Version>(&item_bytes)?;
|
2021-03-10 15:21:56 +00:00
|
|
|
if version.deleted.get() {
|
2020-04-23 18:36:12 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
let object = self
|
|
|
|
.garage
|
|
|
|
.object_table
|
2021-12-14 12:55:11 +00:00
|
|
|
.get(&version.bucket_id, &version.key)
|
2020-04-23 18:36:12 +00:00
|
|
|
.await?;
|
|
|
|
let version_exists = match object {
|
2020-04-26 18:59:17 +00:00
|
|
|
Some(o) => o
|
|
|
|
.versions()
|
|
|
|
.iter()
|
|
|
|
.any(|x| x.uuid == version.uuid && x.state != ObjectVersionState::Aborted),
|
2021-03-15 14:26:29 +00:00
|
|
|
None => false,
|
2020-04-23 18:36:12 +00:00
|
|
|
};
|
|
|
|
if !version_exists {
|
|
|
|
info!("Repair versions: marking version as deleted: {:?}", version);
|
|
|
|
self.garage
|
|
|
|
.version_table
|
|
|
|
.insert(&Version::new(
|
|
|
|
version.uuid,
|
2021-12-14 12:55:11 +00:00
|
|
|
version.bucket_id,
|
2020-04-23 18:36:12 +00:00
|
|
|
version.key,
|
|
|
|
true,
|
|
|
|
))
|
|
|
|
.await?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-06-02 21:14:10 +00:00
|
|
|
async fn repair_block_ref(&self, must_exit: &watch::Receiver<bool>) -> Result<(), Error> {
|
|
|
|
let mut pos = vec![];
|
2020-04-23 18:36:12 +00:00
|
|
|
|
2022-06-03 10:35:12 +00:00
|
|
|
while *must_exit.borrow() {
|
|
|
|
let item_bytes = {
|
|
|
|
let (k, v) = match self.garage.block_ref_table.data.store.get_gt(pos)? {
|
|
|
|
Some(pair) => pair,
|
|
|
|
None => break,
|
|
|
|
};
|
|
|
|
pos = k.into_vec();
|
|
|
|
v.into_vec()
|
|
|
|
};
|
2022-06-02 21:14:10 +00:00
|
|
|
|
|
|
|
let block_ref = rmp_serde::decode::from_read_ref::<_, BlockRef>(&item_bytes)?;
|
2021-03-10 15:21:56 +00:00
|
|
|
if block_ref.deleted.get() {
|
2020-04-23 18:36:12 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
let version = self
|
|
|
|
.garage
|
|
|
|
.version_table
|
|
|
|
.get(&block_ref.version, &EmptyKey)
|
|
|
|
.await?;
|
2021-03-15 14:26:29 +00:00
|
|
|
// The version might not exist if it has been GC'ed
|
|
|
|
let ref_exists = version.map(|v| !v.deleted.get()).unwrap_or(false);
|
2020-04-23 18:36:12 +00:00
|
|
|
if !ref_exists {
|
|
|
|
info!(
|
|
|
|
"Repair block ref: marking block_ref as deleted: {:?}",
|
|
|
|
block_ref
|
|
|
|
);
|
|
|
|
self.garage
|
|
|
|
.block_ref_table
|
|
|
|
.insert(&BlockRef {
|
|
|
|
block: block_ref.block,
|
|
|
|
version: block_ref.version,
|
2021-03-10 15:21:56 +00:00
|
|
|
deleted: true.into(),
|
2020-04-23 18:36:12 +00:00
|
|
|
})
|
|
|
|
.await?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|